| | @@ -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-11-11 19:07:56 1412fcc480799ecbd68d44dd18d5bad40e20ccf1" |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| | @@ -1626,29 +1626,31 @@ |
| 1626 | 1626 | ** it is not possible to set the Serialized [threading mode] and |
| 1627 | 1627 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1628 | 1628 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1629 | 1629 | ** |
| 1630 | 1630 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1631 | | -** <dd> ^(This option takes a single argument which is a pointer to an |
| 1632 | | -** instance of the [sqlite3_mem_methods] structure. The argument specifies |
| 1631 | +** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is |
| 1632 | +** a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1633 | +** The argument specifies |
| 1633 | 1634 | ** alternative low-level memory allocation routines to be used in place of |
| 1634 | 1635 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1635 | 1636 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1636 | 1637 | ** before the [sqlite3_config()] call returns.</dd> |
| 1637 | 1638 | ** |
| 1638 | 1639 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1639 | | -** <dd> ^(This option takes a single argument which is a pointer to an |
| 1640 | | -** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] |
| 1640 | +** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which |
| 1641 | +** is a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1642 | +** The [sqlite3_mem_methods] |
| 1641 | 1643 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1642 | 1644 | ** This option can be used to overload the default memory allocation |
| 1643 | 1645 | ** routines with a wrapper that simulations memory allocation failure or |
| 1644 | 1646 | ** tracks memory usage, for example. </dd> |
| 1645 | 1647 | ** |
| 1646 | 1648 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1647 | | -** <dd> ^This option takes single argument of type int, interpreted as a |
| 1648 | | -** boolean, which enables or disables the collection of memory allocation |
| 1649 | | -** statistics. ^(When memory allocation statistics are disabled, the |
| 1649 | +** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, |
| 1650 | +** interpreted as a boolean, which enables or disables the collection of |
| 1651 | +** memory allocation statistics. ^(When memory allocation statistics are disabled, the |
| 1650 | 1652 | ** following SQLite interfaces become non-operational: |
| 1651 | 1653 | ** <ul> |
| 1652 | 1654 | ** <li> [sqlite3_memory_used()] |
| 1653 | 1655 | ** <li> [sqlite3_memory_highwater()] |
| 1654 | 1656 | ** <li> [sqlite3_soft_heap_limit64()] |
| | @@ -1658,78 +1660,90 @@ |
| 1658 | 1660 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1659 | 1661 | ** allocation statistics are disabled by default. |
| 1660 | 1662 | ** </dd> |
| 1661 | 1663 | ** |
| 1662 | 1664 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1663 | | -** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1664 | | -** scratch memory. There are three arguments: A pointer an 8-byte |
| 1665 | +** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer |
| 1666 | +** that SQLite can use for scratch memory. ^(There are three arguments |
| 1667 | +** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte |
| 1665 | 1668 | ** aligned memory buffer from which the scratch allocations will be |
| 1666 | 1669 | ** drawn, the size of each scratch allocation (sz), |
| 1667 | | -** and the maximum number of scratch allocations (N). The sz |
| 1668 | | -** argument must be a multiple of 16. |
| 1670 | +** and the maximum number of scratch allocations (N).)^ |
| 1669 | 1671 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1670 | 1672 | ** of at least sz*N bytes of memory. |
| 1671 | | -** ^SQLite will use no more than two scratch buffers per thread. So |
| 1672 | | -** N should be set to twice the expected maximum number of threads. |
| 1673 | | -** ^SQLite will never require a scratch buffer that is more than 6 |
| 1674 | | -** times the database page size. ^If SQLite needs needs additional |
| 1673 | +** ^SQLite will not use more than one scratch buffers per thread. |
| 1674 | +** ^SQLite will never request a scratch buffer that is more than 6 |
| 1675 | +** times the database page size. |
| 1676 | +** ^If SQLite needs needs additional |
| 1675 | 1677 | ** scratch memory beyond what is provided by this configuration option, then |
| 1676 | | -** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1678 | +** [sqlite3_malloc()] will be used to obtain the memory needed.<p> |
| 1679 | +** ^When the application provides any amount of scratch memory using |
| 1680 | +** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large |
| 1681 | +** [sqlite3_malloc|heap allocations]. |
| 1682 | +** This can help [Robson proof|prevent memory allocation failures] due to heap |
| 1683 | +** fragmentation in low-memory embedded systems. |
| 1684 | +** </dd> |
| 1677 | 1685 | ** |
| 1678 | 1686 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1679 | | -** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1680 | | -** the database page cache with the default page cache implementation. |
| 1687 | +** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer |
| 1688 | +** that SQLite can use for the database page cache with the default page |
| 1689 | +** cache implementation. |
| 1681 | 1690 | ** This configuration should not be used if an application-define page |
| 1682 | | -** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option. |
| 1683 | | -** There are three arguments to this option: A pointer to 8-byte aligned |
| 1691 | +** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2] |
| 1692 | +** configuration option. |
| 1693 | +** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned |
| 1684 | 1694 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1685 | 1695 | ** The sz argument should be the size of the largest database page |
| 1686 | | -** (a power of two between 512 and 32768) plus a little extra for each |
| 1687 | | -** page header. ^The page header size is 20 to 40 bytes depending on |
| 1688 | | -** the host architecture. ^It is harmless, apart from the wasted memory, |
| 1689 | | -** to make sz a little too large. The first |
| 1690 | | -** argument should point to an allocation of at least sz*N bytes of memory. |
| 1696 | +** (a power of two between 512 and 32768) plus some extra bytes for each |
| 1697 | +** page header. ^The number of extra bytes needed by the page header |
| 1698 | +** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option |
| 1699 | +** to [sqlite3_config()]. |
| 1700 | +** ^It is harmless, apart from the wasted memory, |
| 1701 | +** for the sz parameter to be larger than necessary. The first |
| 1702 | +** argument should pointer to an 8-byte aligned block of memory that |
| 1703 | +** is at least sz*N bytes of memory, otherwise subsequent behavior is |
| 1704 | +** undefined. |
| 1691 | 1705 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1692 | 1706 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1693 | 1707 | ** page cache memory is needed beyond what is provided by this option, then |
| 1694 | | -** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1695 | | -** The pointer in the first argument must |
| 1696 | | -** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1697 | | -** will be undefined.</dd> |
| 1708 | +** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> |
| 1698 | 1709 | ** |
| 1699 | 1710 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1700 | | -** <dd> ^This option specifies a static memory buffer that SQLite will use |
| 1701 | | -** for all of its dynamic memory allocation needs beyond those provided |
| 1702 | | -** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1703 | | -** There are three arguments: An 8-byte aligned pointer to the memory, |
| 1711 | +** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer |
| 1712 | +** that SQLite will use for all of its dynamic memory allocation needs |
| 1713 | +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1714 | +** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled |
| 1715 | +** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns |
| 1716 | +** [SQLITE_ERROR] if invoked otherwise. |
| 1717 | +** ^There are three arguments to SQLITE_CONFIG_HEAP: |
| 1718 | +** An 8-byte aligned pointer to the memory, |
| 1704 | 1719 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1705 | 1720 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1706 | 1721 | ** to using its default memory allocator (the system malloc() implementation), |
| 1707 | 1722 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1708 | | -** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or |
| 1709 | | -** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory |
| 1723 | +** memory pointer is not NULL then the alternative memory |
| 1710 | 1724 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1711 | 1725 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1712 | 1726 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1713 | 1727 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1714 | 1728 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1715 | 1729 | ** |
| 1716 | 1730 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1717 | | -** <dd> ^(This option takes a single argument which is a pointer to an |
| 1718 | | -** instance of the [sqlite3_mutex_methods] structure. The argument specifies |
| 1719 | | -** alternative low-level mutex routines to be used in place |
| 1731 | +** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a |
| 1732 | +** pointer to an instance of the [sqlite3_mutex_methods] structure. |
| 1733 | +** The argument specifies alternative low-level mutex routines to be used in place |
| 1720 | 1734 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1721 | 1735 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1722 | 1736 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1723 | 1737 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1724 | 1738 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1725 | 1739 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1726 | 1740 | ** return [SQLITE_ERROR].</dd> |
| 1727 | 1741 | ** |
| 1728 | 1742 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1729 | | -** <dd> ^(This option takes a single argument which is a pointer to an |
| 1730 | | -** instance of the [sqlite3_mutex_methods] structure. The |
| 1743 | +** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which |
| 1744 | +** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The |
| 1731 | 1745 | ** [sqlite3_mutex_methods] |
| 1732 | 1746 | ** structure is filled with the currently defined mutex routines.)^ |
| 1733 | 1747 | ** This option can be used to overload the default mutex allocation |
| 1734 | 1748 | ** routines with a wrapper used to track mutex usage for performance |
| 1735 | 1749 | ** profiling or testing, for example. ^If SQLite is compiled with |
| | @@ -1737,28 +1751,28 @@ |
| 1737 | 1751 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1738 | 1752 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1739 | 1753 | ** return [SQLITE_ERROR].</dd> |
| 1740 | 1754 | ** |
| 1741 | 1755 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1742 | | -** <dd> ^(This option takes two arguments that determine the default |
| 1743 | | -** memory allocation for the lookaside memory allocator on each |
| 1744 | | -** [database connection]. The first argument is the |
| 1756 | +** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine |
| 1757 | +** the default size of lookaside memory on each [database connection]. |
| 1758 | +** The first argument is the |
| 1745 | 1759 | ** size of each lookaside buffer slot and the second is the number of |
| 1746 | | -** slots allocated to each database connection.)^ ^(This option sets the |
| 1747 | | -** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1748 | | -** verb to [sqlite3_db_config()] can be used to change the lookaside |
| 1760 | +** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE |
| 1761 | +** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1762 | +** option to [sqlite3_db_config()] can be used to change the lookaside |
| 1749 | 1763 | ** configuration on individual connections.)^ </dd> |
| 1750 | 1764 | ** |
| 1751 | 1765 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1752 | | -** <dd> ^(This option takes a single argument which is a pointer to |
| 1753 | | -** an [sqlite3_pcache_methods2] object. This object specifies the interface |
| 1754 | | -** to a custom page cache implementation.)^ ^SQLite makes a copy of the |
| 1755 | | -** object and uses it for page cache memory allocations.</dd> |
| 1766 | +** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is |
| 1767 | +** a pointer to an [sqlite3_pcache_methods2] object. This object specifies |
| 1768 | +** the interface to a custom page cache implementation.)^ |
| 1769 | +** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> |
| 1756 | 1770 | ** |
| 1757 | 1771 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1758 | | -** <dd> ^(This option takes a single argument which is a pointer to an |
| 1759 | | -** [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1772 | +** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which |
| 1773 | +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1760 | 1774 | ** page cache implementation into that object.)^ </dd> |
| 1761 | 1775 | ** |
| 1762 | 1776 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1763 | 1777 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1764 | 1778 | ** global [error log]. |
| | @@ -1778,26 +1792,27 @@ |
| 1778 | 1792 | ** supplied by the application must not invoke any SQLite interface. |
| 1779 | 1793 | ** In a multi-threaded application, the application-defined logger |
| 1780 | 1794 | ** function must be threadsafe. </dd> |
| 1781 | 1795 | ** |
| 1782 | 1796 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1783 | | -** <dd>^(This option takes a single argument of type int. If non-zero, then |
| 1784 | | -** URI handling is globally enabled. If the parameter is zero, then URI handling |
| 1785 | | -** is globally disabled.)^ ^If URI handling is globally enabled, all filenames |
| 1786 | | -** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1797 | +** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. |
| 1798 | +** If non-zero, then URI handling is globally enabled. If the parameter is zero, |
| 1799 | +** then URI handling is globally disabled.)^ ^If URI handling is globally enabled, |
| 1800 | +** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1787 | 1801 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1788 | 1802 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1789 | 1803 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1790 | 1804 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1791 | 1805 | ** database connection is opened. ^(By default, URI handling is globally |
| 1792 | 1806 | ** disabled. The default value may be changed by compiling with the |
| 1793 | 1807 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1794 | 1808 | ** |
| 1795 | 1809 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1796 | | -** <dd>^This option takes a single integer argument which is interpreted as |
| 1797 | | -** a boolean in order to enable or disable the use of covering indices for |
| 1798 | | -** full table scans in the query optimizer. ^The default setting is determined |
| 1810 | +** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer |
| 1811 | +** argument which is interpreted as a boolean in order to enable or disable |
| 1812 | +** the use of covering indices for full table scans in the query optimizer. |
| 1813 | +** ^The default setting is determined |
| 1799 | 1814 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1800 | 1815 | ** if that compile-time option is omitted. |
| 1801 | 1816 | ** The ability to disable the use of covering indices for full table scans |
| 1802 | 1817 | ** is because some incorrectly coded legacy applications might malfunction |
| 1803 | 1818 | ** when the optimization is enabled. Providing the ability to |
| | @@ -1833,23 +1848,32 @@ |
| 1833 | 1848 | ** that are the default mmap size limit (the default setting for |
| 1834 | 1849 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1835 | 1850 | ** ^The default setting can be overridden by each database connection using |
| 1836 | 1851 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1837 | 1852 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1838 | | -** cannot be changed at run-time. Nor may the maximum allowed mmap size |
| 1839 | | -** exceed the compile-time maximum mmap size set by the |
| 1853 | +** will be silently truncated if necessary so that it does not exceed the |
| 1854 | +** compile-time maximum mmap size set by the |
| 1840 | 1855 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1841 | 1856 | ** ^If either argument to this option is negative, then that argument is |
| 1842 | 1857 | ** changed to its compile-time default. |
| 1843 | 1858 | ** |
| 1844 | 1859 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1845 | 1860 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1846 | | -** <dd>^This option is only available if SQLite is compiled for Windows |
| 1847 | | -** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1848 | | -** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1861 | +** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is |
| 1862 | +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1863 | +** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1849 | 1864 | ** that specifies the maximum size of the created heap. |
| 1850 | 1865 | ** </dl> |
| 1866 | +** |
| 1867 | +** [[SQLITE_CONFIG_PCACHE_HDRSZ]] |
| 1868 | +** <dt>SQLITE_CONFIG_PCACHE_HDRSZ |
| 1869 | +** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which |
| 1870 | +** is a pointer to an integer and writes into that integer the number of extra |
| 1871 | +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of |
| 1872 | +** extra space required can change depending on the compiler, |
| 1873 | +** target platform, and SQLite version. |
| 1874 | +** </dl> |
| 1851 | 1875 | */ |
| 1852 | 1876 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1853 | 1877 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1854 | 1878 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1855 | 1879 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| | @@ -1870,10 +1894,11 @@ |
| 1870 | 1894 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1871 | 1895 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1872 | 1896 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1873 | 1897 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1874 | 1898 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1899 | +#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ |
| 1875 | 1900 | |
| 1876 | 1901 | /* |
| 1877 | 1902 | ** CAPI3REF: Database Connection Configuration Options |
| 1878 | 1903 | ** |
| 1879 | 1904 | ** These constants are the available integer configuration options that |
| | @@ -1997,51 +2022,49 @@ |
| 1997 | 2022 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1998 | 2023 | |
| 1999 | 2024 | /* |
| 2000 | 2025 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2001 | 2026 | ** |
| 2002 | | -** ^This function returns the number of database rows that were changed |
| 2003 | | -** or inserted or deleted by the most recently completed SQL statement |
| 2004 | | -** on the [database connection] specified by the first parameter. |
| 2005 | | -** ^(Only changes that are directly specified by the [INSERT], [UPDATE], |
| 2006 | | -** or [DELETE] statement are counted. Auxiliary changes caused by |
| 2007 | | -** triggers or [foreign key actions] are not counted.)^ Use the |
| 2008 | | -** [sqlite3_total_changes()] function to find the total number of changes |
| 2009 | | -** including changes caused by triggers and foreign key actions. |
| 2010 | | -** |
| 2011 | | -** ^Changes to a view that are simulated by an [INSTEAD OF trigger] |
| 2012 | | -** are not counted. Only real table changes are counted. |
| 2013 | | -** |
| 2014 | | -** ^(A "row change" is a change to a single row of a single table |
| 2015 | | -** caused by an INSERT, DELETE, or UPDATE statement. Rows that |
| 2016 | | -** are changed as side effects of [REPLACE] constraint resolution, |
| 2017 | | -** rollback, ABORT processing, [DROP TABLE], or by any other |
| 2018 | | -** mechanisms do not count as direct row changes.)^ |
| 2019 | | -** |
| 2020 | | -** A "trigger context" is a scope of execution that begins and |
| 2021 | | -** ends with the script of a [CREATE TRIGGER | trigger]. |
| 2022 | | -** Most SQL statements are |
| 2023 | | -** evaluated outside of any trigger. This is the "top level" |
| 2024 | | -** trigger context. If a trigger fires from the top level, a |
| 2025 | | -** new trigger context is entered for the duration of that one |
| 2026 | | -** trigger. Subtriggers create subcontexts for their duration. |
| 2027 | | -** |
| 2028 | | -** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does |
| 2029 | | -** not create a new trigger context. |
| 2030 | | -** |
| 2031 | | -** ^This function returns the number of direct row changes in the |
| 2032 | | -** most recent INSERT, UPDATE, or DELETE statement within the same |
| 2033 | | -** trigger context. |
| 2034 | | -** |
| 2035 | | -** ^Thus, when called from the top level, this function returns the |
| 2036 | | -** number of changes in the most recent INSERT, UPDATE, or DELETE |
| 2037 | | -** that also occurred at the top level. ^(Within the body of a trigger, |
| 2038 | | -** the sqlite3_changes() interface can be called to find the number of |
| 2039 | | -** changes in the most recently completed INSERT, UPDATE, or DELETE |
| 2040 | | -** statement within the body of the same trigger. |
| 2041 | | -** However, the number returned does not include changes |
| 2042 | | -** caused by subtriggers since those have their own context.)^ |
| 2027 | +** ^This function returns the number of rows modified, inserted or |
| 2028 | +** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 2029 | +** statement on the database connection specified by the only parameter. |
| 2030 | +** ^Executing any other type of SQL statement does not modify the value |
| 2031 | +** returned by this function. |
| 2032 | +** |
| 2033 | +** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are |
| 2034 | +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], |
| 2035 | +** [foreign key actions] or [REPLACE] constraint resolution are not counted. |
| 2036 | +** |
| 2037 | +** Changes to a view that are intercepted by |
| 2038 | +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value |
| 2039 | +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or |
| 2040 | +** DELETE statement run on a view is always zero. Only changes made to real |
| 2041 | +** tables are counted. |
| 2042 | +** |
| 2043 | +** Things are more complicated if the sqlite3_changes() function is |
| 2044 | +** executed while a trigger program is running. This may happen if the |
| 2045 | +** program uses the [changes() SQL function], or if some other callback |
| 2046 | +** function invokes sqlite3_changes() directly. Essentially: |
| 2047 | +** |
| 2048 | +** <ul> |
| 2049 | +** <li> ^(Before entering a trigger program the value returned by |
| 2050 | +** sqlite3_changes() function is saved. After the trigger program |
| 2051 | +** has finished, the original value is restored.)^ |
| 2052 | +** |
| 2053 | +** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE |
| 2054 | +** statement sets the value returned by sqlite3_changes() |
| 2055 | +** upon completion as normal. Of course, this value will not include |
| 2056 | +** any changes performed by sub-triggers, as the sqlite3_changes() |
| 2057 | +** value will be saved and restored after each sub-trigger has run.)^ |
| 2058 | +** </ul> |
| 2059 | +** |
| 2060 | +** ^This means that if the changes() SQL function (or similar) is used |
| 2061 | +** by the first INSERT, UPDATE or DELETE statement within a trigger, it |
| 2062 | +** returns the value as set when the calling statement began executing. |
| 2063 | +** ^If it is used by the second or subsequent such statement within a trigger |
| 2064 | +** program, the value returned reflects the number of rows modified by the |
| 2065 | +** previous INSERT, UPDATE or DELETE statement within the same trigger. |
| 2043 | 2066 | ** |
| 2044 | 2067 | ** See also the [sqlite3_total_changes()] interface, the |
| 2045 | 2068 | ** [count_changes pragma], and the [changes() SQL function]. |
| 2046 | 2069 | ** |
| 2047 | 2070 | ** If a separate thread makes changes on the same database connection |
| | @@ -2051,24 +2074,21 @@ |
| 2051 | 2074 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 2052 | 2075 | |
| 2053 | 2076 | /* |
| 2054 | 2077 | ** CAPI3REF: Total Number Of Rows Modified |
| 2055 | 2078 | ** |
| 2056 | | -** ^This function returns the number of row changes caused by [INSERT], |
| 2057 | | -** [UPDATE] or [DELETE] statements since the [database connection] was opened. |
| 2058 | | -** ^(The count returned by sqlite3_total_changes() includes all changes |
| 2059 | | -** from all [CREATE TRIGGER | trigger] contexts and changes made by |
| 2060 | | -** [foreign key actions]. However, |
| 2061 | | -** the count does not include changes used to implement [REPLACE] constraints, |
| 2062 | | -** do rollbacks or ABORT processing, or [DROP TABLE] processing. The |
| 2063 | | -** count does not include rows of views that fire an [INSTEAD OF trigger], |
| 2064 | | -** though if the INSTEAD OF trigger makes changes of its own, those changes |
| 2065 | | -** are counted.)^ |
| 2066 | | -** ^The sqlite3_total_changes() function counts the changes as soon as |
| 2067 | | -** the statement that makes them is completed (when the statement handle |
| 2068 | | -** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). |
| 2069 | | -** |
| 2079 | +** ^This function returns the total number of rows inserted, modified or |
| 2080 | +** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed |
| 2081 | +** since the database connection was opened, including those executed as |
| 2082 | +** part of trigger programs. ^Executing any other type of SQL statement |
| 2083 | +** does not affect the value returned by sqlite3_total_changes(). |
| 2084 | +** |
| 2085 | +** ^Changes made as part of [foreign key actions] are included in the |
| 2086 | +** count, but those made as part of REPLACE constraint resolution are |
| 2087 | +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers |
| 2088 | +** are not counted. |
| 2089 | +** |
| 2070 | 2090 | ** See also the [sqlite3_changes()] interface, the |
| 2071 | 2091 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 2072 | 2092 | ** |
| 2073 | 2093 | ** If a separate thread makes changes on the same database connection |
| 2074 | 2094 | ** while [sqlite3_total_changes()] is running then the value |
| | @@ -2542,17 +2562,18 @@ |
| 2542 | 2562 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2543 | 2563 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2544 | 2564 | ** applications to access the same PRNG for other purposes. |
| 2545 | 2565 | ** |
| 2546 | 2566 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2547 | | -** ^If N is less than one, then P can be a NULL pointer. |
| 2567 | +** ^The P parameter can be a NULL pointer. |
| 2548 | 2568 | ** |
| 2549 | 2569 | ** ^If this routine has not been previously called or if the previous |
| 2550 | | -** call had N less than one, then the PRNG is seeded using randomness |
| 2551 | | -** obtained from the xRandomness method of the default [sqlite3_vfs] object. |
| 2552 | | -** ^If the previous call to this routine had an N of 1 or more then |
| 2553 | | -** the pseudo-randomness is generated |
| 2570 | +** call had N less than one or a NULL pointer for P, then the PRNG is |
| 2571 | +** seeded using randomness obtained from the xRandomness method of |
| 2572 | +** the default [sqlite3_vfs] object. |
| 2573 | +** ^If the previous call to this routine had an N of 1 or more and a |
| 2574 | +** non-NULL P then the pseudo-randomness is generated |
| 2554 | 2575 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2555 | 2576 | ** method. |
| 2556 | 2577 | */ |
| 2557 | 2578 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2558 | 2579 | |
| | @@ -5762,31 +5783,47 @@ |
| 5762 | 5783 | ** in other words, the same BLOB that would be selected by: |
| 5763 | 5784 | ** |
| 5764 | 5785 | ** <pre> |
| 5765 | 5786 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5766 | 5787 | ** </pre>)^ |
| 5788 | +** |
| 5789 | +** ^(Parameter zDb is not the filename that contains the database, but |
| 5790 | +** rather the symbolic name of the database. For attached databases, this is |
| 5791 | +** the name that appears after the AS keyword in the [ATTACH] statement. |
| 5792 | +** For the main database file, the database name is "main". For TEMP |
| 5793 | +** tables, the database name is "temp".)^ |
| 5767 | 5794 | ** |
| 5768 | 5795 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5769 | | -** and write access. ^If it is zero, the BLOB is opened for read access. |
| 5770 | | -** ^It is not possible to open a column that is part of an index or primary |
| 5771 | | -** key for writing. ^If [foreign key constraints] are enabled, it is |
| 5772 | | -** not possible to open a column that is part of a [child key] for writing. |
| 5773 | | -** |
| 5774 | | -** ^Note that the database name is not the filename that contains |
| 5775 | | -** the database but rather the symbolic name of the database that |
| 5776 | | -** appears after the AS keyword when the database is connected using [ATTACH]. |
| 5777 | | -** ^For the main database file, the database name is "main". |
| 5778 | | -** ^For TEMP tables, the database name is "temp". |
| 5779 | | -** |
| 5780 | | -** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written |
| 5781 | | -** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set |
| 5782 | | -** to be a null pointer.)^ |
| 5783 | | -** ^This function sets the [database connection] error code and message |
| 5784 | | -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related |
| 5785 | | -** functions. ^Note that the *ppBlob variable is always initialized in a |
| 5786 | | -** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob |
| 5787 | | -** regardless of the success or failure of this routine. |
| 5796 | +** and write access. ^If the flags parameter is zero, the BLOB is opened for |
| 5797 | +** read-only access. |
| 5798 | +** |
| 5799 | +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored |
| 5800 | +** in *ppBlob. Otherwise an [error code] is returned and, unless the error |
| 5801 | +** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided |
| 5802 | +** the API is not misused, it is always safe to call [sqlite3_blob_close()] |
| 5803 | +** on *ppBlob after this function it returns. |
| 5804 | +** |
| 5805 | +** This function fails with SQLITE_ERROR if any of the following are true: |
| 5806 | +** <ul> |
| 5807 | +** <li> ^(Database zDb does not exist)^, |
| 5808 | +** <li> ^(Table zTable does not exist within database zDb)^, |
| 5809 | +** <li> ^(Table zTable is a WITHOUT ROWID table)^, |
| 5810 | +** <li> ^(Column zColumn does not exist)^, |
| 5811 | +** <li> ^(Row iRow is not present in the table)^, |
| 5812 | +** <li> ^(The specified column of row iRow contains a value that is not |
| 5813 | +** a TEXT or BLOB value)^, |
| 5814 | +** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE |
| 5815 | +** constraint and the blob is being opened for read/write access)^, |
| 5816 | +** <li> ^([foreign key constraints | Foreign key constraints] are enabled, |
| 5817 | +** column zColumn is part of a [child key] definition and the blob is |
| 5818 | +** being opened for read/write access)^. |
| 5819 | +** </ul> |
| 5820 | +** |
| 5821 | +** ^Unless it returns SQLITE_MISUSE, this function sets the |
| 5822 | +** [database connection] error code and message accessible via |
| 5823 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5824 | +** |
| 5788 | 5825 | ** |
| 5789 | 5826 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5790 | 5827 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5791 | 5828 | ** then the BLOB handle is marked as "expired". |
| 5792 | 5829 | ** This is true if any column of the row is changed, even a column |
| | @@ -5800,17 +5837,13 @@ |
| 5800 | 5837 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5801 | 5838 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5802 | 5839 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5803 | 5840 | ** blob. |
| 5804 | 5841 | ** |
| 5805 | | -** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID] |
| 5806 | | -** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables. |
| 5807 | | -** |
| 5808 | 5842 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5809 | | -** and the built-in [zeroblob] SQL function can be used, if desired, |
| 5810 | | -** to create an empty, zero-filled blob in which to read or write using |
| 5811 | | -** this interface. |
| 5843 | +** and the built-in [zeroblob] SQL function may be used to create a |
| 5844 | +** zero-filled blob to read or write using the incremental-blob interface. |
| 5812 | 5845 | ** |
| 5813 | 5846 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5814 | 5847 | ** be released by a call to [sqlite3_blob_close()]. |
| 5815 | 5848 | */ |
| 5816 | 5849 | SQLITE_API int sqlite3_blob_open( |
| | @@ -5848,28 +5881,26 @@ |
| 5848 | 5881 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5849 | 5882 | |
| 5850 | 5883 | /* |
| 5851 | 5884 | ** CAPI3REF: Close A BLOB Handle |
| 5852 | 5885 | ** |
| 5853 | | -** ^Closes an open [BLOB handle]. |
| 5854 | | -** |
| 5855 | | -** ^Closing a BLOB shall cause the current transaction to commit |
| 5856 | | -** if there are no other BLOBs, no pending prepared statements, and the |
| 5857 | | -** database connection is in [autocommit mode]. |
| 5858 | | -** ^If any writes were made to the BLOB, they might be held in cache |
| 5859 | | -** until the close operation if they will fit. |
| 5860 | | -** |
| 5861 | | -** ^(Closing the BLOB often forces the changes |
| 5862 | | -** out to disk and so if any I/O errors occur, they will likely occur |
| 5863 | | -** at the time when the BLOB is closed. Any errors that occur during |
| 5864 | | -** closing are reported as a non-zero return value.)^ |
| 5865 | | -** |
| 5866 | | -** ^(The BLOB is closed unconditionally. Even if this routine returns |
| 5867 | | -** an error code, the BLOB is still closed.)^ |
| 5868 | | -** |
| 5869 | | -** ^Calling this routine with a null pointer (such as would be returned |
| 5870 | | -** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. |
| 5886 | +** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed |
| 5887 | +** unconditionally. Even if this routine returns an error code, the |
| 5888 | +** handle is still closed.)^ |
| 5889 | +** |
| 5890 | +** ^If the blob handle being closed was opened for read-write access, and if |
| 5891 | +** the database is in auto-commit mode and there are no other open read-write |
| 5892 | +** blob handles or active write statements, the current transaction is |
| 5893 | +** committed. ^If an error occurs while committing the transaction, an error |
| 5894 | +** code is returned and the transaction rolled back. |
| 5895 | +** |
| 5896 | +** Calling this function with an argument that is not a NULL pointer or an |
| 5897 | +** open blob handle results in undefined behaviour. ^Calling this routine |
| 5898 | +** with a null pointer (such as would be returned by a failed call to |
| 5899 | +** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function |
| 5900 | +** is passed a valid open blob handle, the values returned by the |
| 5901 | +** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. |
| 5871 | 5902 | */ |
| 5872 | 5903 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5873 | 5904 | |
| 5874 | 5905 | /* |
| 5875 | 5906 | ** CAPI3REF: Return The Size Of An Open BLOB |
| | @@ -5915,36 +5946,39 @@ |
| 5915 | 5946 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5916 | 5947 | |
| 5917 | 5948 | /* |
| 5918 | 5949 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5919 | 5950 | ** |
| 5920 | | -** ^This function is used to write data into an open [BLOB handle] from a |
| 5921 | | -** caller-supplied buffer. ^N bytes of data are copied from the buffer Z |
| 5922 | | -** into the open BLOB, starting at offset iOffset. |
| 5951 | +** ^(This function is used to write data into an open [BLOB handle] from a |
| 5952 | +** caller-supplied buffer. N bytes of data are copied from the buffer Z |
| 5953 | +** into the open BLOB, starting at offset iOffset.)^ |
| 5954 | +** |
| 5955 | +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5956 | +** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5957 | +** ^Unless SQLITE_MISUSE is returned, this function sets the |
| 5958 | +** [database connection] error code and message accessible via |
| 5959 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5923 | 5960 | ** |
| 5924 | 5961 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5925 | 5962 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5926 | 5963 | ** this function returns [SQLITE_READONLY]. |
| 5927 | 5964 | ** |
| 5928 | | -** ^This function may only modify the contents of the BLOB; it is |
| 5965 | +** This function may only modify the contents of the BLOB; it is |
| 5929 | 5966 | ** not possible to increase the size of a BLOB using this API. |
| 5930 | 5967 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5931 | | -** [SQLITE_ERROR] is returned and no data is written. ^If N is |
| 5932 | | -** less than zero [SQLITE_ERROR] is returned and no data is written. |
| 5933 | | -** The size of the BLOB (and hence the maximum value of N+iOffset) |
| 5934 | | -** can be determined using the [sqlite3_blob_bytes()] interface. |
| 5968 | +** [SQLITE_ERROR] is returned and no data is written. The size of the |
| 5969 | +** BLOB (and hence the maximum value of N+iOffset) can be determined |
| 5970 | +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less |
| 5971 | +** than zero [SQLITE_ERROR] is returned and no data is written. |
| 5935 | 5972 | ** |
| 5936 | 5973 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5937 | 5974 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5938 | 5975 | ** before the [BLOB handle] expired are not rolled back by the |
| 5939 | 5976 | ** expiration of the handle, though of course those changes might |
| 5940 | 5977 | ** have been overwritten by the statement that expired the BLOB handle |
| 5941 | 5978 | ** or by other independent statements. |
| 5942 | 5979 | ** |
| 5943 | | -** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5944 | | -** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5945 | | -** |
| 5946 | 5980 | ** This routine only works on a [BLOB handle] which has been created |
| 5947 | 5981 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5948 | 5982 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5949 | 5983 | ** to this routine results in undefined and probably undesirable behavior. |
| 5950 | 5984 | ** |
| | @@ -7533,10 +7567,102 @@ |
| 7533 | 7567 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7534 | 7568 | #define SQLITE_FAIL 3 |
| 7535 | 7569 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7536 | 7570 | #define SQLITE_REPLACE 5 |
| 7537 | 7571 | |
| 7572 | +/* |
| 7573 | +** CAPI3REF: Prepared Statement Scan Status Opcodes |
| 7574 | +** KEYWORDS: {scanstatus options} |
| 7575 | +** |
| 7576 | +** The following constants can be used for the T parameter to the |
| 7577 | +** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a |
| 7578 | +** different metric for sqlite3_stmt_scanstatus() to return. |
| 7579 | +** |
| 7580 | +** <dl> |
| 7581 | +** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> |
| 7582 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7583 | +** total number of times that the X-th loop has run.</dd> |
| 7584 | +** |
| 7585 | +** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> |
| 7586 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7587 | +** total number of rows examined by all iterations of the X-th loop.</dd> |
| 7588 | +** |
| 7589 | +** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> |
| 7590 | +** <dd>^The "double" variable pointed to by the T parameter will be set to the |
| 7591 | +** query planner's estimate for the average number of rows output from each |
| 7592 | +** iteration of the X-th loop. If the query planner's estimates was accurate, |
| 7593 | +** then this value will approximate the quotient NVISIT/NLOOP and the |
| 7594 | +** product of this value for all prior loops with the same SELECTID will |
| 7595 | +** be the NLOOP value for the current loop. |
| 7596 | +** |
| 7597 | +** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> |
| 7598 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7599 | +** a zero-terminated UTF-8 string containing the name of the index or table used |
| 7600 | +** for the X-th loop. |
| 7601 | +** |
| 7602 | +** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> |
| 7603 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7604 | +** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description |
| 7605 | +** for the X-th loop. |
| 7606 | +** |
| 7607 | +** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> |
| 7608 | +** <dd>^The "int" variable pointed to by the T parameter will be set to the |
| 7609 | +** "select-id" for the X-th loop. The select-id identifies which query or |
| 7610 | +** subquery the loop is part of. The main query has a select-id of zero. |
| 7611 | +** The select-id is the same value as is output in the first column |
| 7612 | +** of an [EXPLAIN QUERY PLAN] query. |
| 7613 | +** </dl> |
| 7614 | +*/ |
| 7615 | +#define SQLITE_SCANSTAT_NLOOP 0 |
| 7616 | +#define SQLITE_SCANSTAT_NVISIT 1 |
| 7617 | +#define SQLITE_SCANSTAT_EST 2 |
| 7618 | +#define SQLITE_SCANSTAT_NAME 3 |
| 7619 | +#define SQLITE_SCANSTAT_EXPLAIN 4 |
| 7620 | +#define SQLITE_SCANSTAT_SELECTID 5 |
| 7621 | + |
| 7622 | +/* |
| 7623 | +** CAPI3REF: Prepared Statement Scan Status |
| 7624 | +** |
| 7625 | +** Return status data for a single loop within query pStmt. |
| 7626 | +** |
| 7627 | +** The "iScanStatusOp" parameter determines which status information to return. |
| 7628 | +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of |
| 7629 | +** this interface is undefined. |
| 7630 | +** ^The requested measurement is written into a variable pointed to by |
| 7631 | +** the "pOut" parameter. |
| 7632 | +** Parameter "idx" identifies the specific loop to retrieve statistics for. |
| 7633 | +** Loops are numbered starting from zero. ^If idx is out of range - less than |
| 7634 | +** zero or greater than or equal to the total number of loops used to implement |
| 7635 | +** the statement - a non-zero value is returned and the variable that pOut |
| 7636 | +** points to is unchanged. |
| 7637 | +** |
| 7638 | +** ^Statistics might not be available for all loops in all statements. ^In cases |
| 7639 | +** where there exist loops with no available statistics, this function behaves |
| 7640 | +** as if the loop did not exist - it returns non-zero and leave the variable |
| 7641 | +** that pOut points to unchanged. |
| 7642 | +** |
| 7643 | +** This API is only available if the library is built with pre-processor |
| 7644 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7645 | +** |
| 7646 | +** See also: [sqlite3_stmt_scanstatus_reset()] |
| 7647 | +*/ |
| 7648 | +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( |
| 7649 | + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ |
| 7650 | + int idx, /* Index of loop to report on */ |
| 7651 | + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ |
| 7652 | + void *pOut /* Result written here */ |
| 7653 | +); |
| 7654 | + |
| 7655 | +/* |
| 7656 | +** CAPI3REF: Zero Scan-Status Counters |
| 7657 | +** |
| 7658 | +** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7659 | +** |
| 7660 | +** This API is only available if the library is built with pre-processor |
| 7661 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7662 | +*/ |
| 7663 | +SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7538 | 7664 | |
| 7539 | 7665 | |
| 7540 | 7666 | /* |
| 7541 | 7667 | ** Undo the hack that converts floating point types to integer for |
| 7542 | 7668 | ** builds on processors without floating point support. |
| | @@ -7978,14 +8104,13 @@ |
| 7978 | 8104 | #ifndef SQLITE_POWERSAFE_OVERWRITE |
| 7979 | 8105 | # define SQLITE_POWERSAFE_OVERWRITE 1 |
| 7980 | 8106 | #endif |
| 7981 | 8107 | |
| 7982 | 8108 | /* |
| 7983 | | -** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. |
| 7984 | | -** It determines whether or not the features related to |
| 7985 | | -** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can |
| 7986 | | -** be overridden at runtime using the sqlite3_config() API. |
| 8109 | +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by |
| 8110 | +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in |
| 8111 | +** which case memory allocation statistics are disabled by default. |
| 7987 | 8112 | */ |
| 7988 | 8113 | #if !defined(SQLITE_DEFAULT_MEMSTATUS) |
| 7989 | 8114 | # define SQLITE_DEFAULT_MEMSTATUS 1 |
| 7990 | 8115 | #endif |
| 7991 | 8116 | |
| | @@ -8611,11 +8736,11 @@ |
| 8611 | 8736 | ** Estimated quantities used for query planning are stored as 16-bit |
| 8612 | 8737 | ** logarithms. For quantity X, the value stored is 10*log2(X). This |
| 8613 | 8738 | ** gives a possible range of values of approximately 1.0e986 to 1e-986. |
| 8614 | 8739 | ** But the allowed values are "grainy". Not every value is representable. |
| 8615 | 8740 | ** For example, quantities 16 and 17 are both represented by a LogEst |
| 8616 | | -** of 40. However, since LogEst quantaties are suppose to be estimates, |
| 8741 | +** of 40. However, since LogEst quantities are suppose to be estimates, |
| 8617 | 8742 | ** not exact values, this imprecision is not a problem. |
| 8618 | 8743 | ** |
| 8619 | 8744 | ** "LogEst" is short for "Logarithmic Estimate". |
| 8620 | 8745 | ** |
| 8621 | 8746 | ** Examples: |
| | @@ -9011,11 +9136,11 @@ |
| 9011 | 9136 | SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); |
| 9012 | 9137 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); |
| 9013 | 9138 | SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); |
| 9014 | 9139 | SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int); |
| 9015 | 9140 | SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*); |
| 9016 | | -SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int); |
| 9141 | +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int); |
| 9017 | 9142 | SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int); |
| 9018 | 9143 | SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags); |
| 9019 | 9144 | SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*); |
| 9020 | 9145 | SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*); |
| 9021 | 9146 | SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*); |
| | @@ -9044,11 +9169,11 @@ |
| 9044 | 9169 | #define BTREE_BLOBKEY 2 /* Table has keys only - no data */ |
| 9045 | 9170 | |
| 9046 | 9171 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); |
| 9047 | 9172 | SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); |
| 9048 | 9173 | SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*); |
| 9049 | | -SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); |
| 9174 | +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int, int); |
| 9050 | 9175 | |
| 9051 | 9176 | SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); |
| 9052 | 9177 | SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); |
| 9053 | 9178 | |
| 9054 | 9179 | SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p); |
| | @@ -9124,10 +9249,11 @@ |
| 9124 | 9249 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9125 | 9250 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9126 | 9251 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9127 | 9252 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9128 | 9253 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9254 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 9129 | 9255 | |
| 9130 | 9256 | #ifndef NDEBUG |
| 9131 | 9257 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 9132 | 9258 | #endif |
| 9133 | 9259 | |
| | @@ -9666,10 +9792,16 @@ |
| 9666 | 9792 | # define VdbeCoverageAlwaysTaken(v) |
| 9667 | 9793 | # define VdbeCoverageNeverTaken(v) |
| 9668 | 9794 | # define VDBE_OFFSET_LINENO(x) 0 |
| 9669 | 9795 | #endif |
| 9670 | 9796 | |
| 9797 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9798 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); |
| 9799 | +#else |
| 9800 | +# define sqlite3VdbeScanStatus(a,b,c,d,e) |
| 9801 | +#endif |
| 9802 | + |
| 9671 | 9803 | #endif |
| 9672 | 9804 | |
| 9673 | 9805 | /************** End of vdbe.h ************************************************/ |
| 9674 | 9806 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9675 | 9807 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| | @@ -9862,10 +9994,12 @@ |
| 9862 | 9994 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 9863 | 9995 | |
| 9864 | 9996 | /* Functions used to truncate the database file. */ |
| 9865 | 9997 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 9866 | 9998 | |
| 9999 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); |
| 10000 | + |
| 9867 | 10001 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 9868 | 10002 | SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 9869 | 10003 | #endif |
| 9870 | 10004 | |
| 9871 | 10005 | /* Functions to support testing and debugging. */ |
| | @@ -10049,10 +10183,14 @@ |
| 10049 | 10183 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 10050 | 10184 | #endif |
| 10051 | 10185 | |
| 10052 | 10186 | SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); |
| 10053 | 10187 | |
| 10188 | +/* Return the header size */ |
| 10189 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); |
| 10190 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); |
| 10191 | + |
| 10054 | 10192 | #endif /* _PCACHE_H_ */ |
| 10055 | 10193 | |
| 10056 | 10194 | /************** End of pcache.h **********************************************/ |
| 10057 | 10195 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 10058 | 10196 | |
| | @@ -10735,11 +10873,11 @@ |
| 10735 | 10873 | #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ |
| 10736 | 10874 | #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ |
| 10737 | 10875 | #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ |
| 10738 | 10876 | #define SQLITE_Transitive 0x0200 /* Transitive constraints */ |
| 10739 | 10877 | #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ |
| 10740 | | -#define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */ |
| 10878 | +#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ |
| 10741 | 10879 | #define SQLITE_AllOpts 0xffff /* All optimizations */ |
| 10742 | 10880 | |
| 10743 | 10881 | /* |
| 10744 | 10882 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 10745 | 10883 | */ |
| | @@ -11322,11 +11460,12 @@ |
| 11322 | 11460 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11323 | 11461 | int nSample; /* Number of elements in aSample[] */ |
| 11324 | 11462 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11325 | 11463 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11326 | 11464 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | | - tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ |
| 11465 | + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ |
| 11466 | + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ |
| 11328 | 11467 | #endif |
| 11329 | 11468 | }; |
| 11330 | 11469 | |
| 11331 | 11470 | /* |
| 11332 | 11471 | ** Allowed values for Index.idxType |
| | @@ -11520,11 +11659,11 @@ |
| 11520 | 11659 | int nHeight; /* Height of the tree headed by this node */ |
| 11521 | 11660 | #endif |
| 11522 | 11661 | int iTable; /* TK_COLUMN: cursor number of table holding column |
| 11523 | 11662 | ** TK_REGISTER: register number |
| 11524 | 11663 | ** TK_TRIGGER: 1 -> new, 0 -> old |
| 11525 | | - ** EP_Unlikely: 1000 times likelihood */ |
| 11664 | + ** EP_Unlikely: 134217728 times likelihood */ |
| 11526 | 11665 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 11527 | 11666 | ** TK_VARIABLE: variable number (always >= 1). */ |
| 11528 | 11667 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 11529 | 11668 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 11530 | 11669 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| | @@ -12412,13 +12551,15 @@ |
| 12412 | 12551 | int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ |
| 12413 | 12552 | int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ |
| 12414 | 12553 | void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ |
| 12415 | 12554 | Parse *pParse; /* Parser context. */ |
| 12416 | 12555 | int walkerDepth; /* Number of subqueries */ |
| 12556 | + u8 eCode; /* A small processing code */ |
| 12417 | 12557 | union { /* Extra data for callback */ |
| 12418 | 12558 | NameContext *pNC; /* Naming context */ |
| 12419 | | - int i; /* Integer value */ |
| 12559 | + int n; /* A counter */ |
| 12560 | + int iCur; /* A cursor number */ |
| 12420 | 12561 | SrcList *pSrcList; /* FROM clause */ |
| 12421 | 12562 | struct SrcCount *pSrcCount; /* Counting column references */ |
| 12422 | 12563 | } u; |
| 12423 | 12564 | }; |
| 12424 | 12565 | |
| | @@ -12815,10 +12956,11 @@ |
| 12815 | 12956 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12816 | 12957 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12817 | 12958 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12818 | 12959 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12819 | 12960 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12961 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); |
| 12820 | 12962 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12821 | 12963 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12822 | 12964 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12823 | 12965 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12824 | 12966 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| | @@ -13060,11 +13202,11 @@ |
| 13060 | 13202 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 13061 | 13203 | |
| 13062 | 13204 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 13063 | 13205 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| 13064 | 13206 | SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*); |
| 13065 | | -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int); |
| 13207 | +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char); |
| 13066 | 13208 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); |
| 13067 | 13209 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); |
| 13068 | 13210 | SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); |
| 13069 | 13211 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); |
| 13070 | 13212 | |
| | @@ -13472,15 +13614,23 @@ |
| 13472 | 13614 | ** compatibility for legacy applications, the URI filename capability is |
| 13473 | 13615 | ** disabled by default. |
| 13474 | 13616 | ** |
| 13475 | 13617 | ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13476 | 13618 | ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13619 | +** |
| 13620 | +** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally |
| 13621 | +** disabled. The default value may be changed by compiling with the |
| 13622 | +** SQLITE_USE_URI symbol defined. |
| 13477 | 13623 | */ |
| 13478 | 13624 | #ifndef SQLITE_USE_URI |
| 13479 | 13625 | # define SQLITE_USE_URI 0 |
| 13480 | 13626 | #endif |
| 13481 | 13627 | |
| 13628 | +/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the |
| 13629 | +** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if |
| 13630 | +** that compile-time option is omitted. |
| 13631 | +*/ |
| 13482 | 13632 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 13483 | 13633 | # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 |
| 13484 | 13634 | #endif |
| 13485 | 13635 | |
| 13486 | 13636 | /* |
| | @@ -13566,12 +13716,12 @@ |
| 13566 | 13716 | ** than 1 GiB. The sqlite3_test_control() interface can be used to |
| 13567 | 13717 | ** move the pending byte. |
| 13568 | 13718 | ** |
| 13569 | 13719 | ** IMPORTANT: Changing the pending byte to any value other than |
| 13570 | 13720 | ** 0x40000000 results in an incompatible database file format! |
| 13571 | | -** Changing the pending byte during operating results in undefined |
| 13572 | | -** and dileterious behavior. |
| 13721 | +** Changing the pending byte during operation will result in undefined |
| 13722 | +** and incorrect behavior. |
| 13573 | 13723 | */ |
| 13574 | 13724 | #ifndef SQLITE_OMIT_WSD |
| 13575 | 13725 | SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; |
| 13576 | 13726 | #endif |
| 13577 | 13727 | |
| | @@ -13646,10 +13796,13 @@ |
| 13646 | 13796 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 13647 | 13797 | "DISABLE_DIRSYNC", |
| 13648 | 13798 | #endif |
| 13649 | 13799 | #ifdef SQLITE_DISABLE_LFS |
| 13650 | 13800 | "DISABLE_LFS", |
| 13801 | +#endif |
| 13802 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 13803 | + "ENABLE_API_ARMOR", |
| 13651 | 13804 | #endif |
| 13652 | 13805 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 13653 | 13806 | "ENABLE_ATOMIC_WRITE", |
| 13654 | 13807 | #endif |
| 13655 | 13808 | #ifdef SQLITE_ENABLE_CEROD |
| | @@ -13972,10 +14125,17 @@ |
| 13972 | 14125 | ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix |
| 13973 | 14126 | ** is not required for a match. |
| 13974 | 14127 | */ |
| 13975 | 14128 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName){ |
| 13976 | 14129 | int i, n; |
| 14130 | + |
| 14131 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14132 | + if( zOptName==0 ){ |
| 14133 | + (void)SQLITE_MISUSE_BKPT; |
| 14134 | + return 0; |
| 14135 | + } |
| 14136 | +#endif |
| 13977 | 14137 | if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; |
| 13978 | 14138 | n = sqlite3Strlen30(zOptName); |
| 13979 | 14139 | |
| 13980 | 14140 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13981 | 14141 | ** linear search is adequate. No need for a binary search. */ |
| | @@ -14153,10 +14313,11 @@ |
| 14153 | 14313 | typedef struct VdbeFrame VdbeFrame; |
| 14154 | 14314 | struct VdbeFrame { |
| 14155 | 14315 | Vdbe *v; /* VM this frame belongs to */ |
| 14156 | 14316 | VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ |
| 14157 | 14317 | Op *aOp; /* Program instructions for parent frame */ |
| 14318 | + i64 *anExec; /* Event counters from parent frame */ |
| 14158 | 14319 | Mem *aMem; /* Array of memory cells for parent frame */ |
| 14159 | 14320 | u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ |
| 14160 | 14321 | VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ |
| 14161 | 14322 | void *token; /* Copy of SubProgram.token */ |
| 14162 | 14323 | i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| | @@ -14165,11 +14326,12 @@ |
| 14165 | 14326 | int nOp; /* Size of aOp array */ |
| 14166 | 14327 | int nMem; /* Number of entries in aMem */ |
| 14167 | 14328 | int nOnceFlag; /* Number of entries in aOnceFlag */ |
| 14168 | 14329 | int nChildMem; /* Number of memory cells for child frame */ |
| 14169 | 14330 | int nChildCsr; /* Number of cursors for child frame */ |
| 14170 | | - int nChange; /* Statement changes (Vdbe.nChanges) */ |
| 14331 | + int nChange; /* Statement changes (Vdbe.nChange) */ |
| 14332 | + int nDbChange; /* Value of db->nChange */ |
| 14171 | 14333 | }; |
| 14172 | 14334 | |
| 14173 | 14335 | #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 14174 | 14336 | |
| 14175 | 14337 | /* |
| | @@ -14316,10 +14478,20 @@ |
| 14316 | 14478 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 14317 | 14479 | ** N is the number of bits. |
| 14318 | 14480 | */ |
| 14319 | 14481 | typedef unsigned bft; /* Bit Field Type */ |
| 14320 | 14482 | |
| 14483 | +typedef struct ScanStatus ScanStatus; |
| 14484 | +struct ScanStatus { |
| 14485 | + int addrExplain; /* OP_Explain for loop */ |
| 14486 | + int addrLoop; /* Address of "loops" counter */ |
| 14487 | + int addrVisit; /* Address of "rows visited" counter */ |
| 14488 | + int iSelectID; /* The "Select-ID" for this loop */ |
| 14489 | + LogEst nEst; /* Estimated output rows per loop */ |
| 14490 | + char *zName; /* Name of table or index */ |
| 14491 | +}; |
| 14492 | + |
| 14321 | 14493 | /* |
| 14322 | 14494 | ** An instance of the virtual machine. This structure contains the complete |
| 14323 | 14495 | ** state of the virtual machine. |
| 14324 | 14496 | ** |
| 14325 | 14497 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| | @@ -14388,10 +14560,15 @@ |
| 14388 | 14560 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14389 | 14561 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 14390 | 14562 | int nOnceFlag; /* Size of array aOnceFlag[] */ |
| 14391 | 14563 | u8 *aOnceFlag; /* Flags for OP_Once */ |
| 14392 | 14564 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 14565 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 14566 | + i64 *anExec; /* Number of times each op has been executed */ |
| 14567 | + int nScan; /* Entries in aScan[] */ |
| 14568 | + ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ |
| 14569 | +#endif |
| 14393 | 14570 | }; |
| 14394 | 14571 | |
| 14395 | 14572 | /* |
| 14396 | 14573 | ** The following are allowed values for Vdbe.magic |
| 14397 | 14574 | */ |
| | @@ -14577,10 +14754,13 @@ |
| 14577 | 14754 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 14578 | 14755 | wsdStatInit; |
| 14579 | 14756 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14580 | 14757 | return SQLITE_MISUSE_BKPT; |
| 14581 | 14758 | } |
| 14759 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14760 | + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 14761 | +#endif |
| 14582 | 14762 | *pCurrent = wsdStat.nowValue[op]; |
| 14583 | 14763 | *pHighwater = wsdStat.mxValue[op]; |
| 14584 | 14764 | if( resetFlag ){ |
| 14585 | 14765 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14586 | 14766 | } |
| | @@ -14596,10 +14776,15 @@ |
| 14596 | 14776 | int *pCurrent, /* Write current value here */ |
| 14597 | 14777 | int *pHighwater, /* Write high-water mark here */ |
| 14598 | 14778 | int resetFlag /* Reset high-water mark if true */ |
| 14599 | 14779 | ){ |
| 14600 | 14780 | int rc = SQLITE_OK; /* Return code */ |
| 14781 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14782 | + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ |
| 14783 | + return SQLITE_MISUSE_BKPT; |
| 14784 | + } |
| 14785 | +#endif |
| 14601 | 14786 | sqlite3_mutex_enter(db->mutex); |
| 14602 | 14787 | switch( op ){ |
| 14603 | 14788 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 14604 | 14789 | *pCurrent = db->lookaside.nOut; |
| 14605 | 14790 | *pHighwater = db->lookaside.mxOut; |
| | @@ -14774,11 +14959,11 @@ |
| 14774 | 14959 | ** |
| 14775 | 14960 | ** There is only one exported symbol in this file - the function |
| 14776 | 14961 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 14777 | 14962 | ** All other code has file scope. |
| 14778 | 14963 | ** |
| 14779 | | -** SQLite processes all times and dates as Julian Day numbers. The |
| 14964 | +** SQLite processes all times and dates as julian day numbers. The |
| 14780 | 14965 | ** dates and times are stored as the number of days since noon |
| 14781 | 14966 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 14782 | 14967 | ** calendar system. |
| 14783 | 14968 | ** |
| 14784 | 14969 | ** 1970-01-01 00:00:00 is JD 2440587.5 |
| | @@ -14789,11 +14974,11 @@ |
| 14789 | 14974 | ** be represented, even though julian day numbers allow a much wider |
| 14790 | 14975 | ** range of dates. |
| 14791 | 14976 | ** |
| 14792 | 14977 | ** The Gregorian calendar system is used for all dates and times, |
| 14793 | 14978 | ** even those that predate the Gregorian calendar. Historians usually |
| 14794 | | -** use the Julian calendar for dates prior to 1582-10-15 and for some |
| 14979 | +** use the julian calendar for dates prior to 1582-10-15 and for some |
| 14795 | 14980 | ** dates afterwards, depending on locale. Beware of this difference. |
| 14796 | 14981 | ** |
| 14797 | 14982 | ** The conversion algorithms are implemented based on descriptions |
| 14798 | 14983 | ** in the following text: |
| 14799 | 14984 | ** |
| | @@ -15061,11 +15246,11 @@ |
| 15061 | 15246 | return 1; |
| 15062 | 15247 | } |
| 15063 | 15248 | } |
| 15064 | 15249 | |
| 15065 | 15250 | /* |
| 15066 | | -** Attempt to parse the given string into a Julian Day Number. Return |
| 15251 | +** Attempt to parse the given string into a julian day number. Return |
| 15067 | 15252 | ** the number of errors. |
| 15068 | 15253 | ** |
| 15069 | 15254 | ** The following are acceptable forms for the input string: |
| 15070 | 15255 | ** |
| 15071 | 15256 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| | @@ -15632,11 +15817,11 @@ |
| 15632 | 15817 | ** |
| 15633 | 15818 | ** %d day of month |
| 15634 | 15819 | ** %f ** fractional seconds SS.SSS |
| 15635 | 15820 | ** %H hour 00-24 |
| 15636 | 15821 | ** %j day of year 000-366 |
| 15637 | | -** %J ** Julian day number |
| 15822 | +** %J ** julian day number |
| 15638 | 15823 | ** %m month 01-12 |
| 15639 | 15824 | ** %M minute 00-59 |
| 15640 | 15825 | ** %s seconds since 1970-01-01 |
| 15641 | 15826 | ** %S seconds 00-59 |
| 15642 | 15827 | ** %w day of week 0-6 sunday==0 |
| | @@ -16257,10 +16442,14 @@ |
| 16257 | 16442 | MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 16258 | 16443 | #ifndef SQLITE_OMIT_AUTOINIT |
| 16259 | 16444 | int rc = sqlite3_initialize(); |
| 16260 | 16445 | if( rc ) return rc; |
| 16261 | 16446 | #endif |
| 16447 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 16448 | + if( pVfs==0 ) return SQLITE_MISUSE_BKPT; |
| 16449 | +#endif |
| 16450 | + |
| 16262 | 16451 | MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 16263 | 16452 | sqlite3_mutex_enter(mutex); |
| 16264 | 16453 | vfsUnlink(pVfs); |
| 16265 | 16454 | if( makeDflt || vfsList==0 ){ |
| 16266 | 16455 | pVfs->pNext = vfsList; |
| | @@ -18614,10 +18803,11 @@ |
| 18614 | 18803 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 18615 | 18804 | */ |
| 18616 | 18805 | SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| 18617 | 18806 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18618 | 18807 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 18808 | + if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; |
| 18619 | 18809 | #endif |
| 18620 | 18810 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
| 18621 | 18811 | } |
| 18622 | 18812 | |
| 18623 | 18813 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ |
| | @@ -19070,12 +19260,16 @@ |
| 19070 | 19260 | pthread_mutex_init(&p->mutex, 0); |
| 19071 | 19261 | } |
| 19072 | 19262 | break; |
| 19073 | 19263 | } |
| 19074 | 19264 | default: { |
| 19075 | | - assert( iType-2 >= 0 ); |
| 19076 | | - assert( iType-2 < ArraySize(staticMutexes) ); |
| 19265 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 19266 | + if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ |
| 19267 | + (void)SQLITE_MISUSE_BKPT; |
| 19268 | + return 0; |
| 19269 | + } |
| 19270 | +#endif |
| 19077 | 19271 | p = &staticMutexes[iType-2]; |
| 19078 | 19272 | #if SQLITE_MUTEX_NREF |
| 19079 | 19273 | p->id = iType; |
| 19080 | 19274 | #endif |
| 19081 | 19275 | break; |
| | @@ -20293,15 +20487,16 @@ |
| 20293 | 20487 | } |
| 20294 | 20488 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 20295 | 20489 | |
| 20296 | 20490 | |
| 20297 | 20491 | #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 ); |
| 20492 | + /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch |
| 20493 | + ** buffers per thread. |
| 20494 | + ** |
| 20495 | + ** This can only be checked in single-threaded mode. |
| 20496 | + */ |
| 20497 | + assert( scratchAllocOut==0 ); |
| 20303 | 20498 | if( p ) scratchAllocOut++; |
| 20304 | 20499 | #endif |
| 20305 | 20500 | |
| 20306 | 20501 | return p; |
| 20307 | 20502 | } |
| | @@ -20945,11 +21140,11 @@ |
| 20945 | 21140 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 20946 | 21141 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 20947 | 21142 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 20948 | 21143 | char *zOut; /* Rendering buffer */ |
| 20949 | 21144 | int nOut; /* Size of the rendering buffer */ |
| 20950 | | - char *zExtra; /* Malloced memory used by some conversion */ |
| 21145 | + char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 20951 | 21146 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 20952 | 21147 | int exp, e2; /* exponent of real numbers */ |
| 20953 | 21148 | int nsd; /* Number of significant digits returned */ |
| 20954 | 21149 | double rounder; /* Used for rounding floating point values */ |
| 20955 | 21150 | etByte flag_dp; /* True if decimal point should be shown */ |
| | @@ -20956,10 +21151,17 @@ |
| 20956 | 21151 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 20957 | 21152 | #endif |
| 20958 | 21153 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 20959 | 21154 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 20960 | 21155 | |
| 21156 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21157 | + if( ap==0 ){ |
| 21158 | + (void)SQLITE_MISUSE_BKPT; |
| 21159 | + sqlite3StrAccumReset(pAccum); |
| 21160 | + return; |
| 21161 | + } |
| 21162 | +#endif |
| 20961 | 21163 | bufpt = 0; |
| 20962 | 21164 | if( bFlags ){ |
| 20963 | 21165 | if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 20964 | 21166 | pArgList = va_arg(ap, PrintfArguments*); |
| 20965 | 21167 | } |
| | @@ -21062,11 +21264,10 @@ |
| 21062 | 21264 | return; |
| 21063 | 21265 | } |
| 21064 | 21266 | break; |
| 21065 | 21267 | } |
| 21066 | 21268 | } |
| 21067 | | - zExtra = 0; |
| 21068 | 21269 | |
| 21069 | 21270 | /* |
| 21070 | 21271 | ** At this point, variables are initialized as follows: |
| 21071 | 21272 | ** |
| 21072 | 21273 | ** flag_alternateform TRUE if a '#' is present. |
| | @@ -21353,17 +21554,20 @@ |
| 21353 | 21554 | bufpt = getTextArg(pArgList); |
| 21354 | 21555 | c = bufpt ? bufpt[0] : 0; |
| 21355 | 21556 | }else{ |
| 21356 | 21557 | c = va_arg(ap,int); |
| 21357 | 21558 | } |
| 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; |
| 21559 | + if( precision>1 ){ |
| 21560 | + width -= precision-1; |
| 21561 | + if( width>1 && !flag_leftjustify ){ |
| 21562 | + sqlite3AppendChar(pAccum, width-1, ' '); |
| 21563 | + width = 0; |
| 21564 | + } |
| 21565 | + sqlite3AppendChar(pAccum, precision-1, c); |
| 21364 | 21566 | } |
| 21567 | + length = 1; |
| 21568 | + buf[0] = c; |
| 21365 | 21569 | bufpt = buf; |
| 21366 | 21570 | break; |
| 21367 | 21571 | case etSTRING: |
| 21368 | 21572 | case etDYNSTRING: |
| 21369 | 21573 | if( bArgList ){ |
| | @@ -21460,15 +21664,18 @@ |
| 21460 | 21664 | ** The text of the conversion is pointed to by "bufpt" and is |
| 21461 | 21665 | ** "length" characters long. The field width is "width". Do |
| 21462 | 21666 | ** the output. |
| 21463 | 21667 | */ |
| 21464 | 21668 | width -= length; |
| 21465 | | - if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width); |
| 21669 | + if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 21466 | 21670 | sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 21467 | | - if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width); |
| 21671 | + if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 21468 | 21672 | |
| 21469 | | - if( zExtra ) sqlite3_free(zExtra); |
| 21673 | + if( zExtra ){ |
| 21674 | + sqlite3_free(zExtra); |
| 21675 | + zExtra = 0; |
| 21676 | + } |
| 21470 | 21677 | }/* End for loop over the format string */ |
| 21471 | 21678 | } /* End of function */ |
| 21472 | 21679 | |
| 21473 | 21680 | /* |
| 21474 | 21681 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| | @@ -21491,10 +21698,15 @@ |
| 21491 | 21698 | return N; |
| 21492 | 21699 | }else{ |
| 21493 | 21700 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 21494 | 21701 | i64 szNew = p->nChar; |
| 21495 | 21702 | szNew += N + 1; |
| 21703 | + if( szNew+p->nChar<=p->mxAlloc ){ |
| 21704 | + /* Force exponential buffer size growth as long as it does not overflow, |
| 21705 | + ** to avoid having to call this routine too often */ |
| 21706 | + szNew += p->nChar; |
| 21707 | + } |
| 21496 | 21708 | if( szNew > p->mxAlloc ){ |
| 21497 | 21709 | sqlite3StrAccumReset(p); |
| 21498 | 21710 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 21499 | 21711 | return 0; |
| 21500 | 21712 | }else{ |
| | @@ -21507,10 +21719,11 @@ |
| 21507 | 21719 | } |
| 21508 | 21720 | if( zNew ){ |
| 21509 | 21721 | assert( p->zText!=0 || p->nChar==0 ); |
| 21510 | 21722 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 21511 | 21723 | p->zText = zNew; |
| 21724 | + p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 21512 | 21725 | }else{ |
| 21513 | 21726 | sqlite3StrAccumReset(p); |
| 21514 | 21727 | setStrAccumError(p, STRACCUM_NOMEM); |
| 21515 | 21728 | return 0; |
| 21516 | 21729 | } |
| | @@ -21517,15 +21730,15 @@ |
| 21517 | 21730 | } |
| 21518 | 21731 | return N; |
| 21519 | 21732 | } |
| 21520 | 21733 | |
| 21521 | 21734 | /* |
| 21522 | | -** Append N space characters to the given string buffer. |
| 21735 | +** Append N copies of character c to the given string buffer. |
| 21523 | 21736 | */ |
| 21524 | | -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){ |
| 21737 | +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 21525 | 21738 | if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return; |
| 21526 | | - while( (N--)>0 ) p->zText[p->nChar++] = ' '; |
| 21739 | + while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 21527 | 21740 | } |
| 21528 | 21741 | |
| 21529 | 21742 | /* |
| 21530 | 21743 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 21531 | 21744 | ** So enlarge if first, then do the append. |
| | @@ -21676,10 +21889,17 @@ |
| 21676 | 21889 | */ |
| 21677 | 21890 | SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 21678 | 21891 | char *z; |
| 21679 | 21892 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 21680 | 21893 | StrAccum acc; |
| 21894 | + |
| 21895 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21896 | + if( zFormat==0 ){ |
| 21897 | + (void)SQLITE_MISUSE_BKPT; |
| 21898 | + return 0; |
| 21899 | + } |
| 21900 | +#endif |
| 21681 | 21901 | #ifndef SQLITE_OMIT_AUTOINIT |
| 21682 | 21902 | if( sqlite3_initialize() ) return 0; |
| 21683 | 21903 | #endif |
| 21684 | 21904 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 21685 | 21905 | acc.useMalloc = 2; |
| | @@ -21718,10 +21938,17 @@ |
| 21718 | 21938 | ** sqlite3_vsnprintf() is the varargs version. |
| 21719 | 21939 | */ |
| 21720 | 21940 | SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 21721 | 21941 | StrAccum acc; |
| 21722 | 21942 | if( n<=0 ) return zBuf; |
| 21943 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21944 | + if( zBuf==0 || zFormat==0 ) { |
| 21945 | + (void)SQLITE_MISUSE_BKPT; |
| 21946 | + if( zBuf && n>0 ) zBuf[0] = 0; |
| 21947 | + return zBuf; |
| 21948 | + } |
| 21949 | +#endif |
| 21723 | 21950 | sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 21724 | 21951 | acc.useMalloc = 0; |
| 21725 | 21952 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21726 | 21953 | return sqlite3StrAccumFinish(&acc); |
| 21727 | 21954 | } |
| | @@ -21909,15 +22136,23 @@ |
| 21909 | 22136 | #else |
| 21910 | 22137 | # define wsdPrng sqlite3Prng |
| 21911 | 22138 | #endif |
| 21912 | 22139 | |
| 21913 | 22140 | #if SQLITE_THREADSAFE |
| 21914 | | - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 22141 | + sqlite3_mutex *mutex; |
| 22142 | +#endif |
| 22143 | + |
| 22144 | +#ifndef SQLITE_OMIT_AUTOINIT |
| 22145 | + if( sqlite3_initialize() ) return; |
| 22146 | +#endif |
| 22147 | + |
| 22148 | +#if SQLITE_THREADSAFE |
| 22149 | + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 22150 | +#endif |
| 22151 | + |
| 21915 | 22152 | sqlite3_mutex_enter(mutex); |
| 21916 | | -#endif |
| 21917 | | - |
| 21918 | | - if( N<=0 ){ |
| 22153 | + if( N<=0 || pBuf==0 ){ |
| 21919 | 22154 | wsdPrng.isInit = 0; |
| 21920 | 22155 | sqlite3_mutex_leave(mutex); |
| 21921 | 22156 | return; |
| 21922 | 22157 | } |
| 21923 | 22158 | |
| | @@ -23035,17 +23270,27 @@ |
| 23035 | 23270 | ** case-independent fashion, using the same definition of "case |
| 23036 | 23271 | ** independence" that SQLite uses internally when comparing identifiers. |
| 23037 | 23272 | */ |
| 23038 | 23273 | SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 23039 | 23274 | register unsigned char *a, *b; |
| 23275 | + if( zLeft==0 ){ |
| 23276 | + return zRight ? -1 : 0; |
| 23277 | + }else if( zRight==0 ){ |
| 23278 | + return 1; |
| 23279 | + } |
| 23040 | 23280 | a = (unsigned char *)zLeft; |
| 23041 | 23281 | b = (unsigned char *)zRight; |
| 23042 | 23282 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23043 | 23283 | return UpperToLower[*a] - UpperToLower[*b]; |
| 23044 | 23284 | } |
| 23045 | 23285 | SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 23046 | 23286 | register unsigned char *a, *b; |
| 23287 | + if( zLeft==0 ){ |
| 23288 | + return zRight ? -1 : 0; |
| 23289 | + }else if( zRight==0 ){ |
| 23290 | + return 1; |
| 23291 | + } |
| 23047 | 23292 | a = (unsigned char *)zLeft; |
| 23048 | 23293 | b = (unsigned char *)zRight; |
| 23049 | 23294 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23050 | 23295 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 23051 | 23296 | } |
| | @@ -32574,10 +32819,15 @@ |
| 32574 | 32819 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 32575 | 32820 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 32576 | 32821 | with SQLITE_OMIT_WAL." |
| 32577 | 32822 | #endif |
| 32578 | 32823 | |
| 32824 | +#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 |
| 32825 | +# error "Memory mapped files require support from the Windows NT kernel,\ |
| 32826 | + compile with SQLITE_MAX_MMAP_SIZE=0." |
| 32827 | +#endif |
| 32828 | + |
| 32579 | 32829 | /* |
| 32580 | 32830 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 32581 | 32831 | ** based on the sub-platform)? |
| 32582 | 32832 | */ |
| 32583 | 32833 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| | @@ -32703,14 +32953,15 @@ |
| 32703 | 32953 | # define winGetDirSep() '\\' |
| 32704 | 32954 | #endif |
| 32705 | 32955 | |
| 32706 | 32956 | /* |
| 32707 | 32957 | ** 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)? |
| 32958 | +** mode or memory mapped files (e.g. these APIs are available in the Windows |
| 32959 | +** CE SDK; however, they are not present in the header file)? |
| 32710 | 32960 | */ |
| 32711 | | -#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) |
| 32961 | +#if SQLITE_WIN32_FILEMAPPING_API && \ |
| 32962 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 32712 | 32963 | /* |
| 32713 | 32964 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 32714 | 32965 | ** set we need. |
| 32715 | 32966 | */ |
| 32716 | 32967 | #if SQLITE_OS_WINRT |
| | @@ -32734,11 +32985,11 @@ |
| 32734 | 32985 | |
| 32735 | 32986 | /* |
| 32736 | 32987 | ** This file mapping API is common to both Win32 and WinRT. |
| 32737 | 32988 | */ |
| 32738 | 32989 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 32739 | | -#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 32990 | +#endif /* SQLITE_WIN32_FILEMAPPING_API */ |
| 32740 | 32991 | |
| 32741 | 32992 | /* |
| 32742 | 32993 | ** Some Microsoft compilers lack this definition. |
| 32743 | 32994 | */ |
| 32744 | 32995 | #ifndef INVALID_FILE_ATTRIBUTES |
| | @@ -33027,21 +33278,21 @@ |
| 33027 | 33278 | |
| 33028 | 33279 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 33029 | 33280 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 33030 | 33281 | |
| 33031 | 33282 | #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 33032 | | - !defined(SQLITE_OMIT_WAL)) |
| 33283 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33033 | 33284 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 33034 | 33285 | #else |
| 33035 | 33286 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 33036 | 33287 | #endif |
| 33037 | 33288 | |
| 33038 | 33289 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 33039 | 33290 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 33040 | 33291 | |
| 33041 | 33292 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 33042 | | - !defined(SQLITE_OMIT_WAL)) |
| 33293 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33043 | 33294 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 33044 | 33295 | #else |
| 33045 | 33296 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 33046 | 33297 | #endif |
| 33047 | 33298 | |
| | @@ -33377,11 +33628,12 @@ |
| 33377 | 33628 | #ifndef osLockFileEx |
| 33378 | 33629 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 33379 | 33630 | LPOVERLAPPED))aSyscall[48].pCurrent) |
| 33380 | 33631 | #endif |
| 33381 | 33632 | |
| 33382 | | -#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)) |
| 33633 | +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ |
| 33634 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33383 | 33635 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 33384 | 33636 | #else |
| 33385 | 33637 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 33386 | 33638 | #endif |
| 33387 | 33639 | |
| | @@ -33447,11 +33699,11 @@ |
| 33447 | 33699 | #endif |
| 33448 | 33700 | |
| 33449 | 33701 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 33450 | 33702 | LPOVERLAPPED))aSyscall[58].pCurrent) |
| 33451 | 33703 | |
| 33452 | | -#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) |
| 33704 | +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 33453 | 33705 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 33454 | 33706 | #else |
| 33455 | 33707 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 33456 | 33708 | #endif |
| 33457 | 33709 | |
| | @@ -33510,11 +33762,11 @@ |
| 33510 | 33762 | #endif |
| 33511 | 33763 | |
| 33512 | 33764 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 33513 | 33765 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 33514 | 33766 | |
| 33515 | | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33767 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33516 | 33768 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 33517 | 33769 | #else |
| 33518 | 33770 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 33519 | 33771 | #endif |
| 33520 | 33772 | |
| | @@ -33574,11 +33826,11 @@ |
| 33574 | 33826 | |
| 33575 | 33827 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 33576 | 33828 | |
| 33577 | 33829 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 33578 | 33830 | |
| 33579 | | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33831 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33580 | 33832 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 33581 | 33833 | #else |
| 33582 | 33834 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 33583 | 33835 | #endif |
| 33584 | 33836 | |
| | @@ -39150,10 +39402,17 @@ |
| 39150 | 39402 | */ |
| 39151 | 39403 | SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 39152 | 39404 | assert( pCache->pCache!=0 ); |
| 39153 | 39405 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 39154 | 39406 | } |
| 39407 | + |
| 39408 | +/* |
| 39409 | +** Return the size of the header added by this middleware layer |
| 39410 | +** in the page-cache hierarchy. |
| 39411 | +*/ |
| 39412 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); } |
| 39413 | + |
| 39155 | 39414 | |
| 39156 | 39415 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 39157 | 39416 | /* |
| 39158 | 39417 | ** For all dirty pages currently in the cache, invoke the specified |
| 39159 | 39418 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| | @@ -40149,10 +40408,15 @@ |
| 40149 | 40408 | pcache1Shrink /* xShrink */ |
| 40150 | 40409 | }; |
| 40151 | 40410 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 40152 | 40411 | } |
| 40153 | 40412 | |
| 40413 | +/* |
| 40414 | +** Return the size of the header on each page of this PCACHE implementation. |
| 40415 | +*/ |
| 40416 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); } |
| 40417 | + |
| 40154 | 40418 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40155 | 40419 | /* |
| 40156 | 40420 | ** This function is called to free superfluous dynamically allocated memory |
| 40157 | 40421 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 40158 | 40422 | ** by the current thread may be sqlite3_free()ed. |
| | @@ -47705,10 +47969,22 @@ |
| 47705 | 47969 | } |
| 47706 | 47970 | |
| 47707 | 47971 | return SQLITE_OK; |
| 47708 | 47972 | } |
| 47709 | 47973 | #endif |
| 47974 | + |
| 47975 | +/* |
| 47976 | +** The page handle passed as the first argument refers to a dirty page |
| 47977 | +** with a page number other than iNew. This function changes the page's |
| 47978 | +** page number to iNew and sets the value of the PgHdr.flags field to |
| 47979 | +** the value passed as the third parameter. |
| 47980 | +*/ |
| 47981 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ |
| 47982 | + assert( pPg->pgno!=iNew ); |
| 47983 | + pPg->flags = flags; |
| 47984 | + sqlite3PcacheMove(pPg, iNew); |
| 47985 | +} |
| 47710 | 47986 | |
| 47711 | 47987 | /* |
| 47712 | 47988 | ** Return a pointer to the data for the specified page. |
| 47713 | 47989 | */ |
| 47714 | 47990 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ |
| | @@ -48103,10 +48379,11 @@ |
| 48103 | 48379 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ |
| 48104 | 48380 | assert( pPager->eState>=PAGER_READER ); |
| 48105 | 48381 | return sqlite3WalFramesize(pPager->pWal); |
| 48106 | 48382 | } |
| 48107 | 48383 | #endif |
| 48384 | + |
| 48108 | 48385 | |
| 48109 | 48386 | #endif /* SQLITE_OMIT_DISKIO */ |
| 48110 | 48387 | |
| 48111 | 48388 | /************** End of pager.c ***********************************************/ |
| 48112 | 48389 | /************** Begin file wal.c *********************************************/ |
| | @@ -49613,11 +49890,11 @@ |
| 49613 | 49890 | |
| 49614 | 49891 | /* |
| 49615 | 49892 | ** Free an iterator allocated by walIteratorInit(). |
| 49616 | 49893 | */ |
| 49617 | 49894 | static void walIteratorFree(WalIterator *p){ |
| 49618 | | - sqlite3ScratchFree(p); |
| 49895 | + sqlite3_free(p); |
| 49619 | 49896 | } |
| 49620 | 49897 | |
| 49621 | 49898 | /* |
| 49622 | 49899 | ** Construct a WalInterator object that can be used to loop over all |
| 49623 | 49900 | ** pages in the WAL in ascending order. The caller must hold the checkpoint |
| | @@ -49648,21 +49925,21 @@ |
| 49648 | 49925 | /* Allocate space for the WalIterator object. */ |
| 49649 | 49926 | nSegment = walFramePage(iLast) + 1; |
| 49650 | 49927 | nByte = sizeof(WalIterator) |
| 49651 | 49928 | + (nSegment-1)*sizeof(struct WalSegment) |
| 49652 | 49929 | + iLast*sizeof(ht_slot); |
| 49653 | | - p = (WalIterator *)sqlite3ScratchMalloc(nByte); |
| 49930 | + p = (WalIterator *)sqlite3_malloc(nByte); |
| 49654 | 49931 | if( !p ){ |
| 49655 | 49932 | return SQLITE_NOMEM; |
| 49656 | 49933 | } |
| 49657 | 49934 | memset(p, 0, nByte); |
| 49658 | 49935 | p->nSegment = nSegment; |
| 49659 | 49936 | |
| 49660 | 49937 | /* Allocate temporary space used by the merge-sort routine. This block |
| 49661 | 49938 | ** of memory will be freed before this function returns. |
| 49662 | 49939 | */ |
| 49663 | | - aTmp = (ht_slot *)sqlite3ScratchMalloc( |
| 49940 | + aTmp = (ht_slot *)sqlite3_malloc( |
| 49664 | 49941 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 49665 | 49942 | ); |
| 49666 | 49943 | if( !aTmp ){ |
| 49667 | 49944 | rc = SQLITE_NOMEM; |
| 49668 | 49945 | } |
| | @@ -49695,11 +49972,11 @@ |
| 49695 | 49972 | p->aSegment[i].nEntry = nEntry; |
| 49696 | 49973 | p->aSegment[i].aIndex = aIndex; |
| 49697 | 49974 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 49698 | 49975 | } |
| 49699 | 49976 | } |
| 49700 | | - sqlite3ScratchFree(aTmp); |
| 49977 | + sqlite3_free(aTmp); |
| 49701 | 49978 | |
| 49702 | 49979 | if( rc!=SQLITE_OK ){ |
| 49703 | 49980 | walIteratorFree(p); |
| 49704 | 49981 | } |
| 49705 | 49982 | *pp = p; |
| | @@ -50615,11 +50892,11 @@ |
| 50615 | 50892 | ** was in before the client began writing to the database. |
| 50616 | 50893 | */ |
| 50617 | 50894 | memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); |
| 50618 | 50895 | |
| 50619 | 50896 | for(iFrame=pWal->hdr.mxFrame+1; |
| 50620 | | - ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; |
| 50897 | + rc==SQLITE_OK && iFrame<=iMax; |
| 50621 | 50898 | iFrame++ |
| 50622 | 50899 | ){ |
| 50623 | 50900 | /* This call cannot fail. Unless the page for which the page number |
| 50624 | 50901 | ** is passed as the second argument is (a) in the cache and |
| 50625 | 50902 | ** (b) has an outstanding reference, then xUndo is either a no-op |
| | @@ -50634,11 +50911,10 @@ |
| 50634 | 50911 | assert( walFramePgno(pWal, iFrame)!=1 ); |
| 50635 | 50912 | rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); |
| 50636 | 50913 | } |
| 50637 | 50914 | if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); |
| 50638 | 50915 | } |
| 50639 | | - assert( rc==SQLITE_OK ); |
| 50640 | 50916 | return rc; |
| 50641 | 50917 | } |
| 50642 | 50918 | |
| 50643 | 50919 | /* |
| 50644 | 50920 | ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 |
| | @@ -53333,28 +53609,27 @@ |
| 53333 | 53609 | int cellOffset; /* Offset to the cell pointer array */ |
| 53334 | 53610 | int cbrk; /* Offset to the cell content area */ |
| 53335 | 53611 | int nCell; /* Number of cells on the page */ |
| 53336 | 53612 | unsigned char *data; /* The page data */ |
| 53337 | 53613 | unsigned char *temp; /* Temp area for cell content */ |
| 53614 | + unsigned char *src; /* Source of content */ |
| 53338 | 53615 | int iCellFirst; /* First allowable cell index */ |
| 53339 | 53616 | int iCellLast; /* Last possible cell index */ |
| 53340 | 53617 | |
| 53341 | 53618 | |
| 53342 | 53619 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53343 | 53620 | assert( pPage->pBt!=0 ); |
| 53344 | 53621 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 53345 | 53622 | assert( pPage->nOverflow==0 ); |
| 53346 | 53623 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53347 | | - temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53348 | | - data = pPage->aData; |
| 53624 | + temp = 0; |
| 53625 | + src = data = pPage->aData; |
| 53349 | 53626 | hdr = pPage->hdrOffset; |
| 53350 | 53627 | cellOffset = pPage->cellOffset; |
| 53351 | 53628 | nCell = pPage->nCell; |
| 53352 | 53629 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 53353 | 53630 | usableSize = pPage->pBt->usableSize; |
| 53354 | | - cbrk = get2byte(&data[hdr+5]); |
| 53355 | | - memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 53356 | 53631 | cbrk = usableSize; |
| 53357 | 53632 | iCellFirst = cellOffset + 2*nCell; |
| 53358 | 53633 | iCellLast = usableSize - 4; |
| 53359 | 53634 | for(i=0; i<nCell; i++){ |
| 53360 | 53635 | u8 *pAddr; /* The i-th cell pointer */ |
| | @@ -53369,11 +53644,11 @@ |
| 53369 | 53644 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53370 | 53645 | return SQLITE_CORRUPT_BKPT; |
| 53371 | 53646 | } |
| 53372 | 53647 | #endif |
| 53373 | 53648 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 53374 | | - size = cellSizePtr(pPage, &temp[pc]); |
| 53649 | + size = cellSizePtr(pPage, &src[pc]); |
| 53375 | 53650 | cbrk -= size; |
| 53376 | 53651 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 53377 | 53652 | if( cbrk<iCellFirst ){ |
| 53378 | 53653 | return SQLITE_CORRUPT_BKPT; |
| 53379 | 53654 | } |
| | @@ -53383,12 +53658,20 @@ |
| 53383 | 53658 | } |
| 53384 | 53659 | #endif |
| 53385 | 53660 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 53386 | 53661 | testcase( cbrk+size==usableSize ); |
| 53387 | 53662 | testcase( pc+size==usableSize ); |
| 53388 | | - memcpy(&data[cbrk], &temp[pc], size); |
| 53389 | 53663 | put2byte(pAddr, cbrk); |
| 53664 | + if( temp==0 ){ |
| 53665 | + int x; |
| 53666 | + if( cbrk==pc ) continue; |
| 53667 | + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53668 | + x = get2byte(&data[hdr+5]); |
| 53669 | + memcpy(&temp[x], &data[x], (cbrk+size) - x); |
| 53670 | + src = temp; |
| 53671 | + } |
| 53672 | + memcpy(&data[cbrk], &src[pc], size); |
| 53390 | 53673 | } |
| 53391 | 53674 | assert( cbrk>=iCellFirst ); |
| 53392 | 53675 | put2byte(&data[hdr+5], cbrk); |
| 53393 | 53676 | data[hdr+1] = 0; |
| 53394 | 53677 | data[hdr+2] = 0; |
| | @@ -53398,10 +53681,66 @@ |
| 53398 | 53681 | if( cbrk-iCellFirst!=pPage->nFree ){ |
| 53399 | 53682 | return SQLITE_CORRUPT_BKPT; |
| 53400 | 53683 | } |
| 53401 | 53684 | return SQLITE_OK; |
| 53402 | 53685 | } |
| 53686 | + |
| 53687 | +/* |
| 53688 | +** Search the free-list on page pPg for space to store a cell nByte bytes in |
| 53689 | +** size. If one can be found, return a pointer to the space and remove it |
| 53690 | +** from the free-list. |
| 53691 | +** |
| 53692 | +** If no suitable space can be found on the free-list, return NULL. |
| 53693 | +** |
| 53694 | +** This function may detect corruption within pPg. If corruption is |
| 53695 | +** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
| 53696 | +** |
| 53697 | +** If a slot of at least nByte bytes is found but cannot be used because |
| 53698 | +** there are already at least 60 fragmented bytes on the page, return NULL. |
| 53699 | +** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. |
| 53700 | +*/ |
| 53701 | +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ |
| 53702 | + const int hdr = pPg->hdrOffset; |
| 53703 | + u8 * const aData = pPg->aData; |
| 53704 | + int iAddr; |
| 53705 | + int pc; |
| 53706 | + int usableSize = pPg->pBt->usableSize; |
| 53707 | + |
| 53708 | + for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ |
| 53709 | + int size; /* Size of the free slot */ |
| 53710 | + if( pc>usableSize-4 || pc<iAddr+4 ){ |
| 53711 | + *pRc = SQLITE_CORRUPT_BKPT; |
| 53712 | + return 0; |
| 53713 | + } |
| 53714 | + size = get2byte(&aData[pc+2]); |
| 53715 | + if( size>=nByte ){ |
| 53716 | + int x = size - nByte; |
| 53717 | + testcase( x==4 ); |
| 53718 | + testcase( x==3 ); |
| 53719 | + if( x<4 ){ |
| 53720 | + if( aData[hdr+7]>=60 ){ |
| 53721 | + if( pbDefrag ) *pbDefrag = 1; |
| 53722 | + return 0; |
| 53723 | + } |
| 53724 | + /* Remove the slot from the free-list. Update the number of |
| 53725 | + ** fragmented bytes within the page. */ |
| 53726 | + memcpy(&aData[iAddr], &aData[pc], 2); |
| 53727 | + aData[hdr+7] += (u8)x; |
| 53728 | + }else if( size+pc > usableSize ){ |
| 53729 | + *pRc = SQLITE_CORRUPT_BKPT; |
| 53730 | + return 0; |
| 53731 | + }else{ |
| 53732 | + /* The slot remains on the free-list. Reduce its size to account |
| 53733 | + ** for the portion used by the new allocation. */ |
| 53734 | + put2byte(&aData[pc+2], x); |
| 53735 | + } |
| 53736 | + return &aData[pc + x]; |
| 53737 | + } |
| 53738 | + } |
| 53739 | + |
| 53740 | + return 0; |
| 53741 | +} |
| 53403 | 53742 | |
| 53404 | 53743 | /* |
| 53405 | 53744 | ** Allocate nByte bytes of space from within the B-Tree page passed |
| 53406 | 53745 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 53407 | 53746 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| | @@ -53416,22 +53755,20 @@ |
| 53416 | 53755 | */ |
| 53417 | 53756 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 53418 | 53757 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 53419 | 53758 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 53420 | 53759 | int top; /* First byte of cell content area */ |
| 53760 | + int rc = SQLITE_OK; /* Integer return code */ |
| 53421 | 53761 | 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 | 53762 | |
| 53425 | 53763 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53426 | 53764 | assert( pPage->pBt ); |
| 53427 | 53765 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53428 | 53766 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 53429 | 53767 | assert( pPage->nFree>=nByte ); |
| 53430 | 53768 | assert( pPage->nOverflow==0 ); |
| 53431 | | - usableSize = pPage->pBt->usableSize; |
| 53432 | | - assert( nByte < usableSize-8 ); |
| 53769 | + assert( nByte < (int)(pPage->pBt->usableSize-8) ); |
| 53433 | 53770 | |
| 53434 | 53771 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 53435 | 53772 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 53436 | 53773 | assert( gap<=65536 ); |
| 53437 | 53774 | top = get2byte(&data[hdr+5]); |
| | @@ -53449,46 +53786,27 @@ |
| 53449 | 53786 | */ |
| 53450 | 53787 | testcase( gap+2==top ); |
| 53451 | 53788 | testcase( gap+1==top ); |
| 53452 | 53789 | testcase( gap==top ); |
| 53453 | 53790 | 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 | | - } |
| 53791 | + int bDefrag = 0; |
| 53792 | + u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); |
| 53793 | + if( rc ) return rc; |
| 53794 | + if( bDefrag ) goto defragment_page; |
| 53795 | + if( pSpace ){ |
| 53796 | + assert( pSpace>=data && (pSpace - data)<65536 ); |
| 53797 | + *pIdx = (int)(pSpace - data); |
| 53798 | + return SQLITE_OK; |
| 53481 | 53799 | } |
| 53482 | 53800 | } |
| 53483 | 53801 | |
| 53484 | 53802 | /* The request could not be fulfilled using a freelist slot. Check |
| 53485 | 53803 | ** to see if defragmentation is necessary. |
| 53486 | 53804 | */ |
| 53487 | 53805 | testcase( gap+2+nByte==top ); |
| 53488 | 53806 | if( gap+2+nByte>top ){ |
| 53489 | | -defragment_page: |
| 53807 | + defragment_page: |
| 53490 | 53808 | testcase( pPage->nCell==0 ); |
| 53491 | 53809 | rc = defragmentPage(pPage); |
| 53492 | 53810 | if( rc ) return rc; |
| 53493 | 53811 | top = get2byteNotZero(&data[hdr+5]); |
| 53494 | 53812 | assert( gap+nByte<=top ); |
| | @@ -53532,11 +53850,11 @@ |
| 53532 | 53850 | unsigned char *data = pPage->aData; /* Page content */ |
| 53533 | 53851 | |
| 53534 | 53852 | assert( pPage->pBt!=0 ); |
| 53535 | 53853 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53536 | 53854 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 53537 | | - assert( iEnd <= pPage->pBt->usableSize ); |
| 53855 | + assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| 53538 | 53856 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53539 | 53857 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 53540 | 53858 | assert( iStart<=iLast ); |
| 53541 | 53859 | |
| 53542 | 53860 | /* Overwrite deleted information with zeros when the secure_delete |
| | @@ -54353,11 +54671,11 @@ |
| 54353 | 54671 | |
| 54354 | 54672 | /* Rollback any active transaction and free the handle structure. |
| 54355 | 54673 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 54356 | 54674 | ** this handle. |
| 54357 | 54675 | */ |
| 54358 | | - sqlite3BtreeRollback(p, SQLITE_OK); |
| 54676 | + sqlite3BtreeRollback(p, SQLITE_OK, 0); |
| 54359 | 54677 | sqlite3BtreeLeave(p); |
| 54360 | 54678 | |
| 54361 | 54679 | /* If there are still other outstanding references to the shared-btree |
| 54362 | 54680 | ** structure, return now. The remainder of this procedure cleans |
| 54363 | 54681 | ** up the shared-btree. |
| | @@ -55646,31 +55964,32 @@ |
| 55646 | 55964 | return rc; |
| 55647 | 55965 | } |
| 55648 | 55966 | |
| 55649 | 55967 | /* |
| 55650 | 55968 | ** 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){ |
| 55969 | +** code to errCode for every cursor on any BtShared that pBtree |
| 55970 | +** references. Or if the writeOnly flag is set to 1, then only |
| 55971 | +** trip write cursors and leave read cursors unchanged. |
| 55972 | +** |
| 55973 | +** Every cursor is a candidate to be tripped, including cursors |
| 55974 | +** that belong to other database connections that happen to be |
| 55975 | +** sharing the cache with pBtree. |
| 55976 | +** |
| 55977 | +** This routine gets called when a rollback occurs. The writeOnly |
| 55978 | +** flag is set to 1 if the transaction did not make any schema |
| 55979 | +** changes, in which case the read cursors can continue operating. |
| 55980 | +** If schema changes did occur in the transaction, then both read |
| 55981 | +** and write cursors must both be tripped. |
| 55982 | +*/ |
| 55983 | +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ |
| 55667 | 55984 | BtCursor *p; |
| 55985 | + assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); |
| 55668 | 55986 | if( pBtree==0 ) return; |
| 55669 | 55987 | sqlite3BtreeEnter(pBtree); |
| 55670 | 55988 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 55671 | 55989 | int i; |
| 55990 | + if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ) continue; |
| 55672 | 55991 | sqlite3BtreeClearCursor(p); |
| 55673 | 55992 | p->eState = CURSOR_FAULT; |
| 55674 | 55993 | p->skipNext = errCode; |
| 55675 | 55994 | for(i=0; i<=p->iPage; i++){ |
| 55676 | 55995 | releasePage(p->apPage[i]); |
| | @@ -55679,31 +55998,36 @@ |
| 55679 | 55998 | } |
| 55680 | 55999 | sqlite3BtreeLeave(pBtree); |
| 55681 | 56000 | } |
| 55682 | 56001 | |
| 55683 | 56002 | /* |
| 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. |
| 56003 | +** Rollback the transaction in progress. |
| 56004 | +** |
| 56005 | +** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). |
| 56006 | +** Only write cursors are tripped if writeOnly is true but all cursors are |
| 56007 | +** tripped if writeOnly is false. Any attempt to use |
| 56008 | +** a tripped cursor will result in an error. |
| 55688 | 56009 | ** |
| 55689 | 56010 | ** This will release the write lock on the database file. If there |
| 55690 | 56011 | ** are no active cursors, it also releases the read lock. |
| 55691 | 56012 | */ |
| 55692 | | -SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){ |
| 56013 | +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ |
| 55693 | 56014 | int rc; |
| 55694 | 56015 | BtShared *pBt = p->pBt; |
| 55695 | 56016 | MemPage *pPage1; |
| 55696 | 56017 | |
| 56018 | + assert( writeOnly==1 || writeOnly==0 ); |
| 56019 | + assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); |
| 55697 | 56020 | sqlite3BtreeEnter(p); |
| 55698 | 56021 | if( tripCode==SQLITE_OK ){ |
| 55699 | 56022 | rc = tripCode = saveAllCursors(pBt, 0, 0); |
| 56023 | + if( rc ) writeOnly = 0; |
| 55700 | 56024 | }else{ |
| 55701 | 56025 | rc = SQLITE_OK; |
| 55702 | 56026 | } |
| 55703 | 56027 | if( tripCode ){ |
| 55704 | | - sqlite3BtreeTripAllCursors(p, tripCode); |
| 56028 | + sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); |
| 55705 | 56029 | } |
| 55706 | 56030 | btreeIntegrity(p); |
| 55707 | 56031 | |
| 55708 | 56032 | if( p->inTrans==TRANS_WRITE ){ |
| 55709 | 56033 | int rc2; |
| | @@ -58120,49 +58444,266 @@ |
| 58120 | 58444 | #endif |
| 58121 | 58445 | } |
| 58122 | 58446 | } |
| 58123 | 58447 | |
| 58124 | 58448 | /* |
| 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; |
| 58449 | +** Array apCell[] contains pointers to nCell b-tree page cells. The |
| 58450 | +** szCell[] array contains the size in bytes of each cell. This function |
| 58451 | +** replaces the current contents of page pPg with the contents of the cell |
| 58452 | +** array. |
| 58453 | +** |
| 58454 | +** Some of the cells in apCell[] may currently be stored in pPg. This |
| 58455 | +** function works around problems caused by this by making a copy of any |
| 58456 | +** such cells before overwriting the page data. |
| 58457 | +** |
| 58458 | +** The MemPage.nFree field is invalidated by this function. It is the |
| 58459 | +** responsibility of the caller to set it correctly. |
| 58460 | +*/ |
| 58461 | +static void rebuildPage( |
| 58462 | + MemPage *pPg, /* Edit this page */ |
| 58463 | + int nCell, /* Final number of cells on page */ |
| 58464 | + u8 **apCell, /* Array of cells */ |
| 58465 | + u16 *szCell /* Array of cell sizes */ |
| 58466 | +){ |
| 58467 | + const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 58468 | + u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 58469 | + const int usableSize = pPg->pBt->usableSize; |
| 58470 | + u8 * const pEnd = &aData[usableSize]; |
| 58471 | + int i; |
| 58472 | + u8 *pCellptr = pPg->aCellIdx; |
| 58473 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58474 | + u8 *pData; |
| 58475 | + |
| 58476 | + i = get2byte(&aData[hdr+5]); |
| 58477 | + memcpy(&pTmp[i], &aData[i], usableSize - i); |
| 58478 | + |
| 58479 | + pData = pEnd; |
| 58480 | + for(i=0; i<nCell; i++){ |
| 58481 | + u8 *pCell = apCell[i]; |
| 58482 | + if( pCell>aData && pCell<pEnd ){ |
| 58483 | + pCell = &pTmp[pCell - aData]; |
| 58484 | + } |
| 58485 | + pData -= szCell[i]; |
| 58486 | + memcpy(pData, pCell, szCell[i]); |
| 58487 | + put2byte(pCellptr, (pData - aData)); |
| 58488 | + pCellptr += 2; |
| 58489 | + assert( szCell[i]==cellSizePtr(pPg, pCell) ); |
| 58490 | + } |
| 58491 | + |
| 58492 | + /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 58493 | + pPg->nCell = nCell; |
| 58494 | + pPg->nOverflow = 0; |
| 58495 | + |
| 58496 | + put2byte(&aData[hdr+1], 0); |
| 58497 | + put2byte(&aData[hdr+3], pPg->nCell); |
| 58498 | + put2byte(&aData[hdr+5], pData - aData); |
| 58499 | + aData[hdr+7] = 0x00; |
| 58500 | +} |
| 58501 | + |
| 58502 | +/* |
| 58503 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58504 | +** contains the size in bytes of each such cell. This function attempts to |
| 58505 | +** add the cells stored in the array to page pPg. If it cannot (because |
| 58506 | +** the page needs to be defragmented before the cells will fit), non-zero |
| 58507 | +** is returned. Otherwise, if the cells are added successfully, zero is |
| 58508 | +** returned. |
| 58509 | +** |
| 58510 | +** Argument pCellptr points to the first entry in the cell-pointer array |
| 58511 | +** (part of page pPg) to populate. After cell apCell[0] is written to the |
| 58512 | +** page body, a 16-bit offset is written to pCellptr. And so on, for each |
| 58513 | +** cell in the array. It is the responsibility of the caller to ensure |
| 58514 | +** that it is safe to overwrite this part of the cell-pointer array. |
| 58515 | +** |
| 58516 | +** When this function is called, *ppData points to the start of the |
| 58517 | +** content area on page pPg. If the size of the content area is extended, |
| 58518 | +** *ppData is updated to point to the new start of the content area |
| 58519 | +** before returning. |
| 58520 | +** |
| 58521 | +** Finally, argument pBegin points to the byte immediately following the |
| 58522 | +** end of the space required by this page for the cell-pointer area (for |
| 58523 | +** all cells - not just those inserted by the current call). If the content |
| 58524 | +** area must be extended to before this point in order to accomodate all |
| 58525 | +** cells in apCell[], then the cells do not fit and non-zero is returned. |
| 58526 | +*/ |
| 58527 | +static int pageInsertArray( |
| 58528 | + MemPage *pPg, /* Page to add cells to */ |
| 58529 | + u8 *pBegin, /* End of cell-pointer array */ |
| 58530 | + u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 58531 | + u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 58532 | + int nCell, /* Number of cells to add to pPg */ |
| 58533 | + u8 **apCell, /* Array of cells */ |
| 58534 | + u16 *szCell /* Array of cell sizes */ |
| 58535 | +){ |
| 58536 | + int i; |
| 58537 | + u8 *aData = pPg->aData; |
| 58538 | + u8 *pData = *ppData; |
| 58539 | + const int bFreelist = aData[1] || aData[2]; |
| 58540 | + assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
| 58541 | + for(i=0; i<nCell; i++){ |
| 58542 | + int sz = szCell[i]; |
| 58543 | + int rc; |
| 58544 | + u8 *pSlot; |
| 58545 | + if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ |
| 58546 | + pData -= sz; |
| 58547 | + if( pData<pBegin ) return 1; |
| 58548 | + pSlot = pData; |
| 58549 | + } |
| 58550 | + memcpy(pSlot, apCell[i], sz); |
| 58551 | + put2byte(pCellptr, (pSlot - aData)); |
| 58552 | + pCellptr += 2; |
| 58553 | + } |
| 58554 | + *ppData = pData; |
| 58555 | + return 0; |
| 58556 | +} |
| 58557 | + |
| 58558 | +/* |
| 58559 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58560 | +** contains the size in bytes of each such cell. This function adds the |
| 58561 | +** space associated with each cell in the array that is currently stored |
| 58562 | +** within the body of pPg to the pPg free-list. The cell-pointers and other |
| 58563 | +** fields of the page are not updated. |
| 58564 | +** |
| 58565 | +** This function returns the total number of cells added to the free-list. |
| 58566 | +*/ |
| 58567 | +static int pageFreeArray( |
| 58568 | + MemPage *pPg, /* Page to edit */ |
| 58569 | + int nCell, /* Cells to delete */ |
| 58570 | + u8 **apCell, /* Array of cells */ |
| 58571 | + u16 *szCell /* Array of cell sizes */ |
| 58572 | +){ |
| 58573 | + u8 * const aData = pPg->aData; |
| 58574 | + u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
| 58575 | + u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
| 58576 | + int nRet = 0; |
| 58577 | + int i; |
| 58578 | + u8 *pFree = 0; |
| 58579 | + int szFree = 0; |
| 58580 | + |
| 58581 | + for(i=0; i<nCell; i++){ |
| 58582 | + u8 *pCell = apCell[i]; |
| 58583 | + if( pCell>=pStart && pCell<pEnd ){ |
| 58584 | + int sz = szCell[i]; |
| 58585 | + if( pFree!=(pCell + sz) ){ |
| 58586 | + if( pFree ){ |
| 58587 | + assert( pFree>aData && (pFree - aData)<65536 ); |
| 58588 | + freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58589 | + } |
| 58590 | + pFree = pCell; |
| 58591 | + szFree = sz; |
| 58592 | + if( pFree+sz>pEnd ) return 0; |
| 58593 | + }else{ |
| 58594 | + pFree = pCell; |
| 58595 | + szFree += sz; |
| 58596 | + } |
| 58597 | + nRet++; |
| 58598 | + } |
| 58599 | + } |
| 58600 | + if( pFree ){ |
| 58601 | + assert( pFree>aData && (pFree - aData)<65536 ); |
| 58602 | + freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58603 | + } |
| 58604 | + return nRet; |
| 58605 | +} |
| 58606 | + |
| 58607 | +/* |
| 58608 | +** The pPg->nFree field is invalid when this function returns. It is the |
| 58609 | +** responsibility of the caller to set it correctly. |
| 58610 | +*/ |
| 58611 | +static void editPage( |
| 58612 | + MemPage *pPg, /* Edit this page */ |
| 58613 | + int iOld, /* Index of first cell currently on page */ |
| 58614 | + int iNew, /* Index of new first cell on page */ |
| 58615 | + int nNew, /* Final number of cells on page */ |
| 58616 | + u8 **apCell, /* Array of cells */ |
| 58617 | + u16 *szCell /* Array of cell sizes */ |
| 58618 | +){ |
| 58619 | + u8 * const aData = pPg->aData; |
| 58620 | + const int hdr = pPg->hdrOffset; |
| 58621 | + u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
| 58622 | + int nCell = pPg->nCell; /* Cells stored on pPg */ |
| 58623 | + u8 *pData; |
| 58624 | + u8 *pCellptr; |
| 58625 | + int i; |
| 58626 | + int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; |
| 58627 | + int iNewEnd = iNew + nNew; |
| 58628 | + |
| 58629 | +#ifdef SQLITE_DEBUG |
| 58630 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58631 | + memcpy(pTmp, aData, pPg->pBt->usableSize); |
| 58632 | +#endif |
| 58633 | + |
| 58634 | + /* Remove cells from the start and end of the page */ |
| 58635 | + if( iOld<iNew ){ |
| 58636 | + int nShift = pageFreeArray( |
| 58637 | + pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] |
| 58638 | + ); |
| 58639 | + memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 58640 | + nCell -= nShift; |
| 58641 | + } |
| 58642 | + if( iNewEnd < iOldEnd ){ |
| 58643 | + nCell -= pageFreeArray( |
| 58644 | + pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] |
| 58645 | + ); |
| 58646 | + } |
| 58647 | + |
| 58648 | + pData = &aData[get2byte(&aData[hdr+5])]; |
| 58649 | + if( pData<pBegin ) goto editpage_fail; |
| 58650 | + |
| 58651 | + /* Add cells to the start of the page */ |
| 58652 | + if( iNew<iOld ){ |
| 58653 | + int nAdd = iOld-iNew; |
| 58654 | + pCellptr = pPg->aCellIdx; |
| 58655 | + memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 58656 | + if( pageInsertArray( |
| 58657 | + pPg, pBegin, &pData, pCellptr, |
| 58658 | + nAdd, &apCell[iNew], &szCell[iNew] |
| 58659 | + ) ) goto editpage_fail; |
| 58660 | + nCell += nAdd; |
| 58661 | + } |
| 58662 | + |
| 58663 | + /* Add any overflow cells */ |
| 58664 | + for(i=0; i<pPg->nOverflow; i++){ |
| 58665 | + int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
| 58666 | + if( iCell>=0 && iCell<nNew ){ |
| 58667 | + pCellptr = &pPg->aCellIdx[iCell * 2]; |
| 58668 | + memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 58669 | + nCell++; |
| 58670 | + if( pageInsertArray( |
| 58671 | + pPg, pBegin, &pData, pCellptr, |
| 58672 | + 1, &apCell[iCell + iNew], &szCell[iCell + iNew] |
| 58673 | + ) ) goto editpage_fail; |
| 58674 | + } |
| 58675 | + } |
| 58676 | + |
| 58677 | + /* Append cells to the end of the page */ |
| 58678 | + pCellptr = &pPg->aCellIdx[nCell*2]; |
| 58679 | + if( pageInsertArray( |
| 58680 | + pPg, pBegin, &pData, pCellptr, |
| 58681 | + nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] |
| 58682 | + ) ) goto editpage_fail; |
| 58683 | + |
| 58684 | + pPg->nCell = nNew; |
| 58685 | + pPg->nOverflow = 0; |
| 58686 | + |
| 58687 | + put2byte(&aData[hdr+3], pPg->nCell); |
| 58688 | + put2byte(&aData[hdr+5], pData - aData); |
| 58689 | + |
| 58690 | +#ifdef SQLITE_DEBUG |
| 58691 | + for(i=0; i<nNew && !CORRUPT_DB; i++){ |
| 58692 | + u8 *pCell = apCell[i+iNew]; |
| 58693 | + int iOff = get2byte(&pPg->aCellIdx[i*2]); |
| 58694 | + if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
| 58695 | + pCell = &pTmp[pCell - aData]; |
| 58696 | + } |
| 58697 | + assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); |
| 58698 | + } |
| 58699 | +#endif |
| 58700 | + |
| 58701 | + return; |
| 58702 | + editpage_fail: |
| 58703 | + /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 58704 | + rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); |
| 58164 | 58705 | } |
| 58165 | 58706 | |
| 58166 | 58707 | /* |
| 58167 | 58708 | ** The following parameters determine how many adjacent pages get involved |
| 58168 | 58709 | ** in a balancing operation. NN is the number of neighbors on either side |
| | @@ -58230,11 +58771,12 @@ |
| 58230 | 58771 | u8 *pStop; |
| 58231 | 58772 | |
| 58232 | 58773 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 58233 | 58774 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 58234 | 58775 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 58235 | | - assemblePage(pNew, 1, &pCell, &szCell); |
| 58776 | + rebuildPage(pNew, 1, &pCell, &szCell); |
| 58777 | + pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
| 58236 | 58778 | |
| 58237 | 58779 | /* If this is an auto-vacuum database, update the pointer map |
| 58238 | 58780 | ** with entries for the new page, and any pointer from the |
| 58239 | 58781 | ** cell on the page to an overflow page. If either of these |
| 58240 | 58782 | ** operations fails, the return code is set, but the contents |
| | @@ -58449,21 +58991,26 @@ |
| 58449 | 58991 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 58450 | 58992 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 58451 | 58993 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 58452 | 58994 | int szScratch; /* Size of scratch memory requested */ |
| 58453 | 58995 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 58454 | | - MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
| 58455 | 58996 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 58456 | 58997 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 58457 | 58998 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 58458 | 58999 | 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 */ |
| 59000 | + int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ |
| 59001 | + int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
| 58460 | 59002 | u8 **apCell = 0; /* All cells begin balanced */ |
| 58461 | 59003 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 58462 | 59004 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 58463 | 59005 | Pgno pgno; /* Temp var to store a page number in */ |
| 59006 | + u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 59007 | + Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
| 59008 | + Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
| 59009 | + u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
| 58464 | 59010 | |
| 59011 | + memset(abDone, 0, sizeof(abDone)); |
| 58465 | 59012 | pBt = pParent->pBt; |
| 58466 | 59013 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 58467 | 59014 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58468 | 59015 | |
| 58469 | 59016 | #if 0 |
| | @@ -58568,16 +59115,18 @@ |
| 58568 | 59115 | nMaxCells = (nMaxCells + 3)&~3; |
| 58569 | 59116 | |
| 58570 | 59117 | /* |
| 58571 | 59118 | ** Allocate space for memory structures |
| 58572 | 59119 | */ |
| 58573 | | - k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
| 58574 | 59120 | szScratch = |
| 58575 | 59121 | nMaxCells*sizeof(u8*) /* apCell */ |
| 58576 | 59122 | + nMaxCells*sizeof(u16) /* szCell */ |
| 58577 | | - + pBt->pageSize /* aSpace1 */ |
| 58578 | | - + k*nOld; /* Page copies (apCopy) */ |
| 59123 | + + pBt->pageSize; /* aSpace1 */ |
| 59124 | + |
| 59125 | + /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 59126 | + ** that is more than 6 times the database page size. */ |
| 59127 | + assert( szScratch<=6*pBt->pageSize ); |
| 58579 | 59128 | apCell = sqlite3ScratchMalloc( szScratch ); |
| 58580 | 59129 | if( apCell==0 ){ |
| 58581 | 59130 | rc = SQLITE_NOMEM; |
| 58582 | 59131 | goto balance_cleanup; |
| 58583 | 59132 | } |
| | @@ -58586,12 +59135,12 @@ |
| 58586 | 59135 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 58587 | 59136 | |
| 58588 | 59137 | /* |
| 58589 | 59138 | ** Load pointers to all cells on sibling pages and the divider cells |
| 58590 | 59139 | ** 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. |
| 59140 | + ** into space obtained from aSpace1[]. The divider cells have already |
| 59141 | + ** been removed from pParent. |
| 58593 | 59142 | ** |
| 58594 | 59143 | ** If the siblings are on leaf pages, then the child pointers of the |
| 58595 | 59144 | ** divider cells are stripped from the cells before they are copied |
| 58596 | 59145 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 58597 | 59146 | ** child pointers. If siblings are not leaves, then all cell in |
| | @@ -58603,19 +59152,11 @@ |
| 58603 | 59152 | */ |
| 58604 | 59153 | leafCorrection = apOld[0]->leaf*4; |
| 58605 | 59154 | leafData = apOld[0]->intKeyLeaf; |
| 58606 | 59155 | for(i=0; i<nOld; i++){ |
| 58607 | 59156 | 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); |
| 59157 | + MemPage *pOld = apOld[i]; |
| 58617 | 59158 | |
| 58618 | 59159 | limit = pOld->nCell+pOld->nOverflow; |
| 58619 | 59160 | if( pOld->nOverflow>0 ){ |
| 58620 | 59161 | for(j=0; j<limit; j++){ |
| 58621 | 59162 | assert( nCell<nMaxCells ); |
| | @@ -58632,10 +59173,11 @@ |
| 58632 | 59173 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 58633 | 59174 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 58634 | 59175 | nCell++; |
| 58635 | 59176 | } |
| 58636 | 59177 | } |
| 59178 | + cntOld[i] = nCell; |
| 58637 | 59179 | if( i<nOld-1 && !leafData){ |
| 58638 | 59180 | u16 sz = (u16)szNew[i]; |
| 58639 | 59181 | u8 *pTemp; |
| 58640 | 59182 | assert( nCell<nMaxCells ); |
| 58641 | 59183 | szCell[nCell] = sz; |
| | @@ -58683,11 +59225,11 @@ |
| 58683 | 59225 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 58684 | 59226 | for(subtotal=k=i=0; i<nCell; i++){ |
| 58685 | 59227 | assert( i<nMaxCells ); |
| 58686 | 59228 | subtotal += szCell[i] + 2; |
| 58687 | 59229 | if( subtotal > usableSpace ){ |
| 58688 | | - szNew[k] = subtotal - szCell[i]; |
| 59230 | + szNew[k] = subtotal - szCell[i] - 2; |
| 58689 | 59231 | cntNew[k] = i; |
| 58690 | 59232 | if( leafData ){ i--; } |
| 58691 | 59233 | subtotal = 0; |
| 58692 | 59234 | k++; |
| 58693 | 59235 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| | @@ -58697,13 +59239,14 @@ |
| 58697 | 59239 | cntNew[k] = nCell; |
| 58698 | 59240 | k++; |
| 58699 | 59241 | |
| 58700 | 59242 | /* |
| 58701 | 59243 | ** 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. |
| 59244 | + ** on the left side (siblings with smaller keys). The left siblings are |
| 59245 | + ** always nearly full, while the right-most sibling might be nearly empty. |
| 59246 | + ** The next block of code attempts to adjust the packing of siblings to |
| 59247 | + ** get a better balance. |
| 58705 | 59248 | ** |
| 58706 | 59249 | ** This adjustment is more than an optimization. The packing above might |
| 58707 | 59250 | ** be so out of balance as to be illegal. For example, the right-most |
| 58708 | 59251 | ** sibling might be completely empty. This adjustment is not optional. |
| 58709 | 59252 | */ |
| | @@ -58728,26 +59271,22 @@ |
| 58728 | 59271 | } |
| 58729 | 59272 | szNew[i] = szRight; |
| 58730 | 59273 | szNew[i-1] = szLeft; |
| 58731 | 59274 | } |
| 58732 | 59275 | |
| 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. |
| 59276 | + /* Sanity check: For a non-corrupt database file one of the follwing |
| 59277 | + ** must be true: |
| 59278 | + ** (1) We found one or more cells (cntNew[0])>0), or |
| 59279 | + ** (2) pPage is a virtual root page. A virtual root page is when |
| 59280 | + ** the real root page is page 1 and we are the only child of |
| 59281 | + ** that page. |
| 58740 | 59282 | */ |
| 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 |
| 59283 | + assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
| 59284 | + TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
| 59285 | + apOld[0]->pgno, apOld[0]->nCell, |
| 59286 | + nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
| 59287 | + nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
| 58749 | 59288 | )); |
| 58750 | 59289 | |
| 58751 | 59290 | /* |
| 58752 | 59291 | ** Allocate k new pages. Reuse old pages where possible. |
| 58753 | 59292 | */ |
| | @@ -58766,12 +59305,14 @@ |
| 58766 | 59305 | if( rc ) goto balance_cleanup; |
| 58767 | 59306 | }else{ |
| 58768 | 59307 | assert( i>0 ); |
| 58769 | 59308 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 58770 | 59309 | if( rc ) goto balance_cleanup; |
| 59310 | + zeroPage(pNew, pageFlags); |
| 58771 | 59311 | apNew[i] = pNew; |
| 58772 | 59312 | nNew++; |
| 59313 | + cntOld[i] = nCell; |
| 58773 | 59314 | |
| 58774 | 59315 | /* Set the pointer-map entry for the new sibling page. */ |
| 58775 | 59316 | if( ISAUTOVACUUM ){ |
| 58776 | 59317 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 58777 | 59318 | if( rc!=SQLITE_OK ){ |
| | @@ -58779,139 +59320,247 @@ |
| 58779 | 59320 | } |
| 58780 | 59321 | } |
| 58781 | 59322 | } |
| 58782 | 59323 | } |
| 58783 | 59324 | |
| 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 | 59325 | /* |
| 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], |
| 59326 | + ** Reassign page numbers so that the new pages are in ascending order. |
| 59327 | + ** This helps to keep entries in the disk file in order so that a scan |
| 59328 | + ** of the table is closer to a linear scan through the file. That in turn |
| 59329 | + ** helps the operating system to deliver pages from the disk more rapidly. |
| 59330 | + ** |
| 59331 | + ** An O(n^2) insertion sort algorithm is used, but since n is never more |
| 59332 | + ** than (NB+2) (a small constant), that should not be a problem. |
| 59333 | + ** |
| 59334 | + ** When NB==3, this one optimization makes the database about 25% faster |
| 59335 | + ** for large insertions and deletions. |
| 59336 | + */ |
| 59337 | + for(i=0; i<nNew; i++){ |
| 59338 | + aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
| 59339 | + aPgFlags[i] = apNew[i]->pDbPage->flags; |
| 59340 | + for(j=0; j<i; j++){ |
| 59341 | + if( aPgno[j]==aPgno[i] ){ |
| 59342 | + /* This branch is taken if the set of sibling pages somehow contains |
| 59343 | + ** duplicate entries. This can happen if the database is corrupt. |
| 59344 | + ** It would be simpler to detect this as part of the loop below, but |
| 59345 | + ** we do the detection here in order to avoid populating the pager |
| 59346 | + ** cache with two separate objects associated with the same |
| 59347 | + ** page number. */ |
| 59348 | + assert( CORRUPT_DB ); |
| 59349 | + rc = SQLITE_CORRUPT_BKPT; |
| 59350 | + goto balance_cleanup; |
| 59351 | + } |
| 59352 | + } |
| 59353 | + } |
| 59354 | + for(i=0; i<nNew; i++){ |
| 59355 | + int iBest = 0; /* aPgno[] index of page number to use */ |
| 59356 | + for(j=1; j<nNew; j++){ |
| 59357 | + if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
| 59358 | + } |
| 59359 | + pgno = aPgOrder[iBest]; |
| 59360 | + aPgOrder[iBest] = 0xffffffff; |
| 59361 | + if( iBest!=i ){ |
| 59362 | + if( iBest>i ){ |
| 59363 | + sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
| 59364 | + } |
| 59365 | + sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
| 59366 | + apNew[i]->pgno = pgno; |
| 59367 | + } |
| 59368 | + } |
| 59369 | + |
| 59370 | + TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 59371 | + "%d(%d nc=%d) %d(%d nc=%d)\n", |
| 59372 | + apNew[0]->pgno, szNew[0], cntNew[0], |
| 58826 | 59373 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 59374 | + nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
| 58827 | 59375 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 59376 | + nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
| 58828 | 59377 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 58829 | | - nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
| 59378 | + nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, |
| 59379 | + nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, |
| 59380 | + nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 |
| 59381 | + )); |
| 58830 | 59382 | |
| 58831 | 59383 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58832 | 59384 | put4byte(pRight, apNew[nNew-1]->pgno); |
| 58833 | 59385 | |
| 58834 | | - /* |
| 58835 | | - ** Evenly distribute the data in apCell[] across the new pages. |
| 58836 | | - ** Insert divider cells into pParent as necessary. |
| 59386 | + /* If the sibling pages are not leaves, ensure that the right-child pointer |
| 59387 | + ** of the right-most new sibling page is set to the value that was |
| 59388 | + ** originally in the same field of the right-most old sibling page. */ |
| 59389 | + if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ |
| 59390 | + MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; |
| 59391 | + memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); |
| 59392 | + } |
| 59393 | + |
| 59394 | + /* Make any required updates to pointer map entries associated with |
| 59395 | + ** cells stored on sibling pages following the balance operation. Pointer |
| 59396 | + ** map entries associated with divider cells are set by the insertCell() |
| 59397 | + ** routine. The associated pointer map entries are: |
| 59398 | + ** |
| 59399 | + ** a) if the cell contains a reference to an overflow chain, the |
| 59400 | + ** entry associated with the first page in the overflow chain, and |
| 59401 | + ** |
| 59402 | + ** b) if the sibling pages are not leaves, the child page associated |
| 59403 | + ** with the cell. |
| 59404 | + ** |
| 59405 | + ** If the sibling pages are not leaves, then the pointer map entry |
| 59406 | + ** associated with the right-child of each sibling may also need to be |
| 59407 | + ** updated. This happens below, after the sibling pages have been |
| 59408 | + ** populated, not here. |
| 58837 | 59409 | */ |
| 58838 | | - j = 0; |
| 58839 | | - for(i=0; i<nNew; i++){ |
| 58840 | | - /* Assemble the new sibling page. */ |
| 59410 | + if( ISAUTOVACUUM ){ |
| 59411 | + MemPage *pNew = apNew[0]; |
| 59412 | + u8 *aOld = pNew->aData; |
| 59413 | + int cntOldNext = pNew->nCell + pNew->nOverflow; |
| 59414 | + int usableSize = pBt->usableSize; |
| 59415 | + int iNew = 0; |
| 59416 | + int iOld = 0; |
| 59417 | + |
| 59418 | + for(i=0; i<nCell; i++){ |
| 59419 | + u8 *pCell = apCell[i]; |
| 59420 | + if( i==cntOldNext ){ |
| 59421 | + MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
| 59422 | + cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 59423 | + aOld = pOld->aData; |
| 59424 | + } |
| 59425 | + if( i==cntNew[iNew] ){ |
| 59426 | + pNew = apNew[++iNew]; |
| 59427 | + if( !leafData ) continue; |
| 59428 | + } |
| 59429 | + |
| 59430 | + /* Cell pCell is destined for new sibling page pNew. Originally, it |
| 59431 | + ** was either part of sibling page iOld (possibly an overflow cell), |
| 59432 | + ** or else the divider cell to the left of sibling page iOld. So, |
| 59433 | + ** if sibling page iOld had the same page number as pNew, and if |
| 59434 | + ** pCell really was a part of sibling page iOld (not a divider or |
| 59435 | + ** overflow cell), we can skip updating the pointer map entries. */ |
| 59436 | + if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){ |
| 59437 | + if( !leafCorrection ){ |
| 59438 | + ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 59439 | + } |
| 59440 | + if( szCell[i]>pNew->minLocal ){ |
| 59441 | + ptrmapPutOvflPtr(pNew, pCell, &rc); |
| 59442 | + } |
| 59443 | + } |
| 59444 | + } |
| 59445 | + } |
| 59446 | + |
| 59447 | + /* Insert new divider cells into pParent. */ |
| 59448 | + for(i=0; i<nNew-1; i++){ |
| 59449 | + u8 *pCell; |
| 59450 | + u8 *pTemp; |
| 59451 | + int sz; |
| 58841 | 59452 | 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 | 59453 | j = cntNew[i]; |
| 58849 | 59454 | |
| 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 ); |
| 59455 | + assert( j<nMaxCells ); |
| 59456 | + pCell = apCell[j]; |
| 59457 | + sz = szCell[j] + leafCorrection; |
| 59458 | + pTemp = &aOvflSpace[iOvflSpace]; |
| 59459 | + if( !pNew->leaf ){ |
| 59460 | + memcpy(&pNew->aData[8], pCell, 4); |
| 59461 | + }else if( leafData ){ |
| 59462 | + /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 59463 | + ** then there is no divider cell in apCell[]. Instead, the divider |
| 59464 | + ** cell consists of the integer key for the right-most cell of |
| 59465 | + ** the sibling-page assembled above only. |
| 59466 | + */ |
| 59467 | + CellInfo info; |
| 59468 | + j--; |
| 59469 | + btreeParseCellPtr(pNew, apCell[j], &info); |
| 59470 | + pCell = pTemp; |
| 59471 | + sz = 4 + putVarint(&pCell[4], info.nKey); |
| 59472 | + pTemp = 0; |
| 59473 | + }else{ |
| 59474 | + pCell -= 4; |
| 59475 | + /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 59476 | + ** previously stored on a leaf node, and its reported size was 4 |
| 59477 | + ** bytes, then it may actually be smaller than this |
| 59478 | + ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 59479 | + ** any cell). But it is important to pass the correct size to |
| 59480 | + ** insertCell(), so reparse the cell now. |
| 59481 | + ** |
| 59482 | + ** Note that this can never happen in an SQLite data file, as all |
| 59483 | + ** cells are at least 4 bytes. It only happens in b-trees used |
| 59484 | + ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 59485 | + */ |
| 59486 | + if( szCell[j]==4 ){ |
| 59487 | + assert(leafCorrection==4); |
| 59488 | + sz = cellSizePtr(pParent, pCell); |
| 59489 | + } |
| 59490 | + } |
| 59491 | + iOvflSpace += sz; |
| 59492 | + assert( sz<=pBt->maxLocal+23 ); |
| 59493 | + assert( iOvflSpace <= (int)pBt->pageSize ); |
| 59494 | + insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
| 59495 | + if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 59496 | + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59497 | + } |
| 59498 | + |
| 59499 | + /* Now update the actual sibling pages. The order in which they are updated |
| 59500 | + ** is important, as this code needs to avoid disrupting any page from which |
| 59501 | + ** cells may still to be read. In practice, this means: |
| 59502 | + ** |
| 59503 | + ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) |
| 59504 | + ** then it is not safe to update page apNew[iPg] until after |
| 59505 | + ** the left-hand sibling apNew[iPg-1] has been updated. |
| 59506 | + ** |
| 59507 | + ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) |
| 59508 | + ** then it is not safe to update page apNew[iPg] until after |
| 59509 | + ** the right-hand sibling apNew[iPg+1] has been updated. |
| 59510 | + ** |
| 59511 | + ** If neither of the above apply, the page is safe to update. |
| 59512 | + ** |
| 59513 | + ** The iPg value in the following loop starts at nNew-1 goes down |
| 59514 | + ** to 0, then back up to nNew-1 again, thus making two passes over |
| 59515 | + ** the pages. On the initial downward pass, only condition (1) above |
| 59516 | + ** needs to be tested because (2) will always be true from the previous |
| 59517 | + ** step. On the upward pass, both conditions are always true, so the |
| 59518 | + ** upwards pass simply processes pages that were missed on the downward |
| 59519 | + ** pass. |
| 59520 | + */ |
| 59521 | + for(i=1-nNew; i<nNew; i++){ |
| 59522 | + int iPg = i<0 ? -i : i; |
| 59523 | + assert( iPg>=0 && iPg<nNew ); |
| 59524 | + if( abDone[iPg] ) continue; /* Skip pages already processed */ |
| 59525 | + if( i>=0 /* On the upwards pass, or... */ |
| 59526 | + || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ |
| 59527 | + ){ |
| 59528 | + int iNew; |
| 59529 | + int iOld; |
| 59530 | + int nNewCell; |
| 59531 | + |
| 59532 | + /* Verify condition (1): If cells are moving left, update iPg |
| 59533 | + ** only after iPg-1 has already been updated. */ |
| 59534 | + assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); |
| 59535 | + |
| 59536 | + /* Verify condition (2): If cells are moving right, update iPg |
| 59537 | + ** only after iPg+1 has already been updated. */ |
| 59538 | + assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); |
| 59539 | + |
| 59540 | + if( iPg==0 ){ |
| 59541 | + iNew = iOld = 0; |
| 59542 | + nNewCell = cntNew[0]; |
| 59543 | + }else{ |
| 59544 | + iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; |
| 59545 | + iNew = cntNew[iPg-1] + !leafData; |
| 59546 | + nNewCell = cntNew[iPg] - iNew; |
| 59547 | + } |
| 59548 | + |
| 59549 | + editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); |
| 59550 | + abDone[iPg]++; |
| 59551 | + apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
| 59552 | + assert( apNew[iPg]->nOverflow==0 ); |
| 59553 | + assert( apNew[iPg]->nCell==nNewCell ); |
| 59554 | + } |
| 59555 | + } |
| 59556 | + |
| 59557 | + /* All pages have been processed exactly once */ |
| 59558 | + assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); |
| 59559 | + |
| 58907 | 59560 | assert( nOld>0 ); |
| 58908 | 59561 | 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 | 59562 | |
| 58914 | 59563 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 58915 | 59564 | /* The root page of the b-tree now contains no cells. The only sibling |
| 58916 | 59565 | ** page is the right-child of the parent. Copy the contents of the |
| 58917 | 59566 | ** child page into the parent, decreasing the overall height of the |
| | @@ -58920,130 +59569,54 @@ |
| 58920 | 59569 | ** |
| 58921 | 59570 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 58922 | 59571 | ** sets all pointer-map entries corresponding to database image pages |
| 58923 | 59572 | ** for which the pointer is stored within the content being copied. |
| 58924 | 59573 | ** |
| 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. */ |
| 59574 | + ** It is critical that the child page be defragmented before being |
| 59575 | + ** copied into the parent, because if the parent is page 1 then it will |
| 59576 | + ** by smaller than the child due to the database header, and so all the |
| 59577 | + ** free space needs to be up front. |
| 59578 | + */ |
| 58929 | 59579 | assert( nNew==1 ); |
| 59580 | + rc = defragmentPage(apNew[0]); |
| 59581 | + testcase( rc!=SQLITE_OK ); |
| 58930 | 59582 | assert( apNew[0]->nFree == |
| 58931 | | - (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 59583 | + (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 59584 | + || rc!=SQLITE_OK |
| 58932 | 59585 | ); |
| 58933 | 59586 | copyNodeContent(apNew[0], pParent, &rc); |
| 58934 | 59587 | 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 | | - } |
| 59588 | + }else if( ISAUTOVACUUM && !leafCorrection ){ |
| 59589 | + /* Fix the pointer map entries associated with the right-child of each |
| 59590 | + ** sibling page. All other pointer map entries have already been taken |
| 59591 | + ** care of. */ |
| 59592 | + for(i=0; i<nNew; i++){ |
| 59593 | + u32 key = get4byte(&apNew[i]->aData[8]); |
| 59594 | + ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 59595 | + } |
| 59596 | + } |
| 59597 | + |
| 59598 | + assert( pParent->isInit ); |
| 59599 | + TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59600 | + nOld, nNew, nCell)); |
| 59601 | + |
| 59602 | + /* Free any old pages that were not reused as new pages. |
| 59603 | + */ |
| 59604 | + for(i=nNew; i<nOld; i++){ |
| 59605 | + freePage(apOld[i], &rc); |
| 59606 | + } |
| 59031 | 59607 | |
| 59032 | 59608 | #if 0 |
| 59609 | + if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ |
| 59033 | 59610 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 59034 | 59611 | ** all pointer map pages are set correctly. This is helpful while |
| 59035 | 59612 | ** debugging. This is usually disabled because a corrupt database may |
| 59036 | 59613 | ** cause an assert() statement to fail. */ |
| 59037 | 59614 | ptrmapCheckPages(apNew, nNew); |
| 59038 | 59615 | ptrmapCheckPages(&pParent, 1); |
| 59616 | + } |
| 59039 | 59617 | #endif |
| 59040 | | - } |
| 59041 | | - |
| 59042 | | - assert( pParent->isInit ); |
| 59043 | | - TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59044 | | - nOld, nNew, nCell)); |
| 59045 | 59618 | |
| 59046 | 59619 | /* |
| 59047 | 59620 | ** Cleanup before returning. |
| 59048 | 59621 | */ |
| 59049 | 59622 | balance_cleanup: |
| | @@ -60865,10 +61438,15 @@ |
| 60865 | 61438 | */ |
| 60866 | 61439 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| 60867 | 61440 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 60868 | 61441 | } |
| 60869 | 61442 | |
| 61443 | +/* |
| 61444 | +** Return the size of the header added to each page by this module. |
| 61445 | +*/ |
| 61446 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); } |
| 61447 | + |
| 60870 | 61448 | /************** End of btree.c ***********************************************/ |
| 60871 | 61449 | /************** Begin file backup.c ******************************************/ |
| 60872 | 61450 | /* |
| 60873 | 61451 | ** 2009 January 28 |
| 60874 | 61452 | ** |
| | @@ -61004,10 +61582,17 @@ |
| 61004 | 61582 | const char *zDestDb, /* Name of database within pDestDb */ |
| 61005 | 61583 | sqlite3* pSrcDb, /* Database connection to read from */ |
| 61006 | 61584 | const char *zSrcDb /* Name of database within pSrcDb */ |
| 61007 | 61585 | ){ |
| 61008 | 61586 | sqlite3_backup *p; /* Value to return */ |
| 61587 | + |
| 61588 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 61589 | + if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ |
| 61590 | + (void)SQLITE_MISUSE_BKPT; |
| 61591 | + return 0; |
| 61592 | + } |
| 61593 | +#endif |
| 61009 | 61594 | |
| 61010 | 61595 | /* Lock the source database handle. The destination database |
| 61011 | 61596 | ** handle is not locked in this routine, but it is locked in |
| 61012 | 61597 | ** sqlite3_backup_step(). The user is required to ensure that no |
| 61013 | 61598 | ** other thread accesses the destination handle for the duration |
| | @@ -61201,10 +61786,13 @@ |
| 61201 | 61786 | int rc; |
| 61202 | 61787 | int destMode; /* Destination journal mode */ |
| 61203 | 61788 | int pgszSrc = 0; /* Source page size */ |
| 61204 | 61789 | int pgszDest = 0; /* Destination page size */ |
| 61205 | 61790 | |
| 61791 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 61792 | + if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 61793 | +#endif |
| 61206 | 61794 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 61207 | 61795 | sqlite3BtreeEnter(p->pSrc); |
| 61208 | 61796 | if( p->pDestDb ){ |
| 61209 | 61797 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 61210 | 61798 | } |
| | @@ -61464,11 +62052,11 @@ |
| 61464 | 62052 | } |
| 61465 | 62053 | *pp = p->pNext; |
| 61466 | 62054 | } |
| 61467 | 62055 | |
| 61468 | 62056 | /* If a transaction is still open on the Btree, roll it back. */ |
| 61469 | | - sqlite3BtreeRollback(p->pDest, SQLITE_OK); |
| 62057 | + sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0); |
| 61470 | 62058 | |
| 61471 | 62059 | /* Set the error code of the destination database handle. */ |
| 61472 | 62060 | rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; |
| 61473 | 62061 | if( p->pDestDb ){ |
| 61474 | 62062 | sqlite3Error(p->pDestDb, rc); |
| | @@ -61490,18 +62078,30 @@ |
| 61490 | 62078 | /* |
| 61491 | 62079 | ** Return the number of pages still to be backed up as of the most recent |
| 61492 | 62080 | ** call to sqlite3_backup_step(). |
| 61493 | 62081 | */ |
| 61494 | 62082 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){ |
| 62083 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 62084 | + if( p==0 ){ |
| 62085 | + (void)SQLITE_MISUSE_BKPT; |
| 62086 | + return 0; |
| 62087 | + } |
| 62088 | +#endif |
| 61495 | 62089 | return p->nRemaining; |
| 61496 | 62090 | } |
| 61497 | 62091 | |
| 61498 | 62092 | /* |
| 61499 | 62093 | ** Return the total number of pages in the source database as of the most |
| 61500 | 62094 | ** recent call to sqlite3_backup_step(). |
| 61501 | 62095 | */ |
| 61502 | 62096 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){ |
| 62097 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 62098 | + if( p==0 ){ |
| 62099 | + (void)SQLITE_MISUSE_BKPT; |
| 62100 | + return 0; |
| 62101 | + } |
| 62102 | +#endif |
| 61503 | 62103 | return p->nPagecount; |
| 61504 | 62104 | } |
| 61505 | 62105 | |
| 61506 | 62106 | /* |
| 61507 | 62107 | ** This function is called after the contents of page iPage of the |
| | @@ -63788,10 +64388,38 @@ |
| 63788 | 64388 | } |
| 63789 | 64389 | p->nOp += nOp; |
| 63790 | 64390 | } |
| 63791 | 64391 | return addr; |
| 63792 | 64392 | } |
| 64393 | + |
| 64394 | +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 64395 | +/* |
| 64396 | +** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). |
| 64397 | +*/ |
| 64398 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus( |
| 64399 | + Vdbe *p, /* VM to add scanstatus() to */ |
| 64400 | + int addrExplain, /* Address of OP_Explain (or 0) */ |
| 64401 | + int addrLoop, /* Address of loop counter */ |
| 64402 | + int addrVisit, /* Address of rows visited counter */ |
| 64403 | + LogEst nEst, /* Estimated number of output rows */ |
| 64404 | + const char *zName /* Name of table or index being scanned */ |
| 64405 | +){ |
| 64406 | + int nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 64407 | + ScanStatus *aNew; |
| 64408 | + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 64409 | + if( aNew ){ |
| 64410 | + ScanStatus *pNew = &aNew[p->nScan++]; |
| 64411 | + pNew->addrExplain = addrExplain; |
| 64412 | + pNew->addrLoop = addrLoop; |
| 64413 | + pNew->addrVisit = addrVisit; |
| 64414 | + pNew->nEst = nEst; |
| 64415 | + pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 64416 | + p->aScan = aNew; |
| 64417 | + } |
| 64418 | +} |
| 64419 | +#endif |
| 64420 | + |
| 63793 | 64421 | |
| 63794 | 64422 | /* |
| 63795 | 64423 | ** Change the value of the P1 operand for a specific instruction. |
| 63796 | 64424 | ** This routine is useful when a large program is loaded from a |
| 63797 | 64425 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| | @@ -64887,10 +65515,13 @@ |
| 64887 | 65515 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 64888 | 65516 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 64889 | 65517 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 64890 | 65518 | &zCsr, zEnd, &nByte); |
| 64891 | 65519 | p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 65520 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65521 | + p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte); |
| 65522 | +#endif |
| 64892 | 65523 | if( nByte ){ |
| 64893 | 65524 | p->pFree = sqlite3DbMallocZero(db, nByte); |
| 64894 | 65525 | } |
| 64895 | 65526 | zCsr = p->pFree; |
| 64896 | 65527 | zEnd = &zCsr[nByte]; |
| | @@ -64954,10 +65585,13 @@ |
| 64954 | 65585 | ** is used, for example, when a trigger sub-program is halted to restore |
| 64955 | 65586 | ** control to the main program. |
| 64956 | 65587 | */ |
| 64957 | 65588 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 64958 | 65589 | Vdbe *v = pFrame->v; |
| 65590 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65591 | + v->anExec = pFrame->anExec; |
| 65592 | +#endif |
| 64959 | 65593 | v->aOnceFlag = pFrame->aOnceFlag; |
| 64960 | 65594 | v->nOnceFlag = pFrame->nOnceFlag; |
| 64961 | 65595 | v->aOp = pFrame->aOp; |
| 64962 | 65596 | v->nOp = pFrame->nOp; |
| 64963 | 65597 | v->aMem = pFrame->aMem; |
| | @@ -64964,10 +65598,11 @@ |
| 64964 | 65598 | v->nMem = pFrame->nMem; |
| 64965 | 65599 | v->apCsr = pFrame->apCsr; |
| 64966 | 65600 | v->nCursor = pFrame->nCursor; |
| 64967 | 65601 | v->db->lastRowid = pFrame->lastRowid; |
| 64968 | 65602 | v->nChange = pFrame->nChange; |
| 65603 | + v->db->nChange = pFrame->nDbChange; |
| 64969 | 65604 | return pFrame->pc; |
| 64970 | 65605 | } |
| 64971 | 65606 | |
| 64972 | 65607 | /* |
| 64973 | 65608 | ** Close all cursors. |
| | @@ -65531,10 +66166,11 @@ |
| 65531 | 66166 | ** so, abort any other statements this handle currently has active. |
| 65532 | 66167 | */ |
| 65533 | 66168 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65534 | 66169 | sqlite3CloseSavepoints(db); |
| 65535 | 66170 | db->autoCommit = 1; |
| 66171 | + p->nChange = 0; |
| 65536 | 66172 | } |
| 65537 | 66173 | } |
| 65538 | 66174 | } |
| 65539 | 66175 | |
| 65540 | 66176 | /* Check for immediate foreign key violations. */ |
| | @@ -65571,18 +66207,20 @@ |
| 65571 | 66207 | sqlite3VdbeLeave(p); |
| 65572 | 66208 | return SQLITE_BUSY; |
| 65573 | 66209 | }else if( rc!=SQLITE_OK ){ |
| 65574 | 66210 | p->rc = rc; |
| 65575 | 66211 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66212 | + p->nChange = 0; |
| 65576 | 66213 | }else{ |
| 65577 | 66214 | db->nDeferredCons = 0; |
| 65578 | 66215 | db->nDeferredImmCons = 0; |
| 65579 | 66216 | db->flags &= ~SQLITE_DeferFKs; |
| 65580 | 66217 | sqlite3CommitInternalChanges(db); |
| 65581 | 66218 | } |
| 65582 | 66219 | }else{ |
| 65583 | 66220 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66221 | + p->nChange = 0; |
| 65584 | 66222 | } |
| 65585 | 66223 | db->nStatement = 0; |
| 65586 | 66224 | }else if( eStatementOp==0 ){ |
| 65587 | 66225 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 65588 | 66226 | eStatementOp = SAVEPOINT_RELEASE; |
| | @@ -65590,10 +66228,11 @@ |
| 65590 | 66228 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 65591 | 66229 | }else{ |
| 65592 | 66230 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65593 | 66231 | sqlite3CloseSavepoints(db); |
| 65594 | 66232 | db->autoCommit = 1; |
| 66233 | + p->nChange = 0; |
| 65595 | 66234 | } |
| 65596 | 66235 | } |
| 65597 | 66236 | |
| 65598 | 66237 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 65599 | 66238 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| | @@ -65610,10 +66249,11 @@ |
| 65610 | 66249 | p->zErrMsg = 0; |
| 65611 | 66250 | } |
| 65612 | 66251 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65613 | 66252 | sqlite3CloseSavepoints(db); |
| 65614 | 66253 | db->autoCommit = 1; |
| 66254 | + p->nChange = 0; |
| 65615 | 66255 | } |
| 65616 | 66256 | } |
| 65617 | 66257 | |
| 65618 | 66258 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 65619 | 66259 | ** has been rolled back, update the database connection change-counter. |
| | @@ -65871,10 +66511,16 @@ |
| 65871 | 66511 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65872 | 66512 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65873 | 66513 | sqlite3DbFree(db, p->aColName); |
| 65874 | 66514 | sqlite3DbFree(db, p->zSql); |
| 65875 | 66515 | sqlite3DbFree(db, p->pFree); |
| 66516 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 66517 | + for(i=0; i<p->nScan; i++){ |
| 66518 | + sqlite3DbFree(db, p->aScan[i].zName); |
| 66519 | + } |
| 66520 | + sqlite3DbFree(db, p->aScan); |
| 66521 | +#endif |
| 65876 | 66522 | } |
| 65877 | 66523 | |
| 65878 | 66524 | /* |
| 65879 | 66525 | ** Delete an entire VDBE. |
| 65880 | 66526 | */ |
| | @@ -68238,15 +68884,23 @@ |
| 68238 | 68884 | sqlite3_stmt *pStmt, |
| 68239 | 68885 | int N, |
| 68240 | 68886 | const void *(*xFunc)(Mem*), |
| 68241 | 68887 | int useType |
| 68242 | 68888 | ){ |
| 68243 | | - const void *ret = 0; |
| 68244 | | - Vdbe *p = (Vdbe *)pStmt; |
| 68889 | + const void *ret; |
| 68890 | + Vdbe *p; |
| 68245 | 68891 | int n; |
| 68246 | | - sqlite3 *db = p->db; |
| 68247 | | - |
| 68892 | + sqlite3 *db; |
| 68893 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 68894 | + if( pStmt==0 ){ |
| 68895 | + (void)SQLITE_MISUSE_BKPT; |
| 68896 | + return 0; |
| 68897 | + } |
| 68898 | +#endif |
| 68899 | + ret = 0; |
| 68900 | + p = (Vdbe *)pStmt; |
| 68901 | + db = p->db; |
| 68248 | 68902 | assert( db!=0 ); |
| 68249 | 68903 | n = sqlite3_column_count(pStmt); |
| 68250 | 68904 | if( N<n && N>=0 ){ |
| 68251 | 68905 | N += useType*n; |
| 68252 | 68906 | sqlite3_mutex_enter(db->mutex); |
| | @@ -68707,10 +69361,16 @@ |
| 68707 | 69361 | ** prepared statement for the database connection. Return NULL if there |
| 68708 | 69362 | ** are no more. |
| 68709 | 69363 | */ |
| 68710 | 69364 | SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 68711 | 69365 | sqlite3_stmt *pNext; |
| 69366 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 69367 | + if( !sqlite3SafetyCheckOk(pDb) ){ |
| 69368 | + (void)SQLITE_MISUSE_BKPT; |
| 69369 | + return 0; |
| 69370 | + } |
| 69371 | +#endif |
| 68712 | 69372 | sqlite3_mutex_enter(pDb->mutex); |
| 68713 | 69373 | if( pStmt==0 ){ |
| 68714 | 69374 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 68715 | 69375 | }else{ |
| 68716 | 69376 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| | @@ -68722,15 +69382,91 @@ |
| 68722 | 69382 | /* |
| 68723 | 69383 | ** Return the value of a status counter for a prepared statement |
| 68724 | 69384 | */ |
| 68725 | 69385 | SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 68726 | 69386 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 68727 | | - u32 v = pVdbe->aCounter[op]; |
| 69387 | + u32 v; |
| 69388 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 69389 | + if( !pStmt ){ |
| 69390 | + (void)SQLITE_MISUSE_BKPT; |
| 69391 | + return 0; |
| 69392 | + } |
| 69393 | +#endif |
| 69394 | + v = pVdbe->aCounter[op]; |
| 68728 | 69395 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 68729 | 69396 | return (int)v; |
| 68730 | 69397 | } |
| 68731 | 69398 | |
| 69399 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 69400 | +/* |
| 69401 | +** Return status data for a single loop within query pStmt. |
| 69402 | +*/ |
| 69403 | +SQLITE_API int sqlite3_stmt_scanstatus( |
| 69404 | + sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
| 69405 | + int idx, /* Index of loop to report on */ |
| 69406 | + int iScanStatusOp, /* Which metric to return */ |
| 69407 | + void *pOut /* OUT: Write the answer here */ |
| 69408 | +){ |
| 69409 | + Vdbe *p = (Vdbe*)pStmt; |
| 69410 | + ScanStatus *pScan; |
| 69411 | + if( idx<0 || idx>=p->nScan ) return 1; |
| 69412 | + pScan = &p->aScan[idx]; |
| 69413 | + switch( iScanStatusOp ){ |
| 69414 | + case SQLITE_SCANSTAT_NLOOP: { |
| 69415 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; |
| 69416 | + break; |
| 69417 | + } |
| 69418 | + case SQLITE_SCANSTAT_NVISIT: { |
| 69419 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; |
| 69420 | + break; |
| 69421 | + } |
| 69422 | + case SQLITE_SCANSTAT_EST: { |
| 69423 | + double r = 1.0; |
| 69424 | + LogEst x = pScan->nEst; |
| 69425 | + while( x<100 ){ |
| 69426 | + x += 10; |
| 69427 | + r *= 0.5; |
| 69428 | + } |
| 69429 | + *(double*)pOut = r*sqlite3LogEstToInt(x); |
| 69430 | + break; |
| 69431 | + } |
| 69432 | + case SQLITE_SCANSTAT_NAME: { |
| 69433 | + *(const char**)pOut = pScan->zName; |
| 69434 | + break; |
| 69435 | + } |
| 69436 | + case SQLITE_SCANSTAT_EXPLAIN: { |
| 69437 | + if( pScan->addrExplain ){ |
| 69438 | + *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; |
| 69439 | + }else{ |
| 69440 | + *(const char**)pOut = 0; |
| 69441 | + } |
| 69442 | + break; |
| 69443 | + } |
| 69444 | + case SQLITE_SCANSTAT_SELECTID: { |
| 69445 | + if( pScan->addrExplain ){ |
| 69446 | + *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; |
| 69447 | + }else{ |
| 69448 | + *(int*)pOut = -1; |
| 69449 | + } |
| 69450 | + break; |
| 69451 | + } |
| 69452 | + default: { |
| 69453 | + return 1; |
| 69454 | + } |
| 69455 | + } |
| 69456 | + return 0; |
| 69457 | +} |
| 69458 | + |
| 69459 | +/* |
| 69460 | +** Zero all counters associated with the sqlite3_stmt_scanstatus() data. |
| 69461 | +*/ |
| 69462 | +SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ |
| 69463 | + Vdbe *p = (Vdbe*)pStmt; |
| 69464 | + memset(p->anExec, 0, p->nOp * sizeof(i64)); |
| 69465 | +} |
| 69466 | +#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |
| 69467 | + |
| 68732 | 69468 | /************** End of vdbeapi.c *********************************************/ |
| 68733 | 69469 | /************** Begin file vdbetrace.c ***************************************/ |
| 68734 | 69470 | /* |
| 68735 | 69471 | ** 2009 November 25 |
| 68736 | 69472 | ** |
| | @@ -69612,10 +70348,13 @@ |
| 69612 | 70348 | #ifdef VDBE_PROFILE |
| 69613 | 70349 | start = sqlite3Hwtime(); |
| 69614 | 70350 | #endif |
| 69615 | 70351 | nVmStep++; |
| 69616 | 70352 | pOp = &aOp[pc]; |
| 70353 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 70354 | + if( p->anExec ) p->anExec[pc]++; |
| 70355 | +#endif |
| 69617 | 70356 | |
| 69618 | 70357 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 69619 | 70358 | */ |
| 69620 | 70359 | #ifdef SQLITE_DEBUG |
| 69621 | 70360 | if( db->flags & SQLITE_VdbeTrace ){ |
| | @@ -71302,11 +72041,11 @@ |
| 71302 | 72041 | assert( pReg->flags & MEM_Blob ); |
| 71303 | 72042 | assert( memIsValid(pReg) ); |
| 71304 | 72043 | pC->payloadSize = pC->szRow = avail = pReg->n; |
| 71305 | 72044 | pC->aRow = (u8*)pReg->z; |
| 71306 | 72045 | }else{ |
| 71307 | | - MemSetTypeFlag(pDest, MEM_Null); |
| 72046 | + sqlite3VdbeMemSetNull(pDest); |
| 71308 | 72047 | goto op_column_out; |
| 71309 | 72048 | } |
| 71310 | 72049 | }else{ |
| 71311 | 72050 | assert( pCrsr ); |
| 71312 | 72051 | if( pC->isTable==0 ){ |
| | @@ -71826,23 +72565,28 @@ |
| 71826 | 72565 | goto vdbe_return; |
| 71827 | 72566 | } |
| 71828 | 72567 | db->isTransactionSavepoint = 0; |
| 71829 | 72568 | rc = p->rc; |
| 71830 | 72569 | }else{ |
| 72570 | + int isSchemaChange; |
| 71831 | 72571 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 71832 | 72572 | if( p1==SAVEPOINT_ROLLBACK ){ |
| 72573 | + isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; |
| 71833 | 72574 | for(ii=0; ii<db->nDb; ii++){ |
| 71834 | | - sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT); |
| 72575 | + sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT, |
| 72576 | + isSchemaChange==0); |
| 71835 | 72577 | } |
| 72578 | + }else{ |
| 72579 | + isSchemaChange = 0; |
| 71836 | 72580 | } |
| 71837 | 72581 | for(ii=0; ii<db->nDb; ii++){ |
| 71838 | 72582 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 71839 | 72583 | if( rc!=SQLITE_OK ){ |
| 71840 | 72584 | goto abort_due_to_error; |
| 71841 | 72585 | } |
| 71842 | 72586 | } |
| 71843 | | - if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
| 72587 | + if( isSchemaChange ){ |
| 71844 | 72588 | sqlite3ExpirePreparedStatements(db); |
| 71845 | 72589 | sqlite3ResetAllSchemasOfConnection(db); |
| 71846 | 72590 | db->flags = (db->flags | SQLITE_InternChanges); |
| 71847 | 72591 | } |
| 71848 | 72592 | } |
| | @@ -72235,11 +72979,11 @@ |
| 72235 | 72979 | assert( p->bIsReader ); |
| 72236 | 72980 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 72237 | 72981 | || p->readOnly==0 ); |
| 72238 | 72982 | |
| 72239 | 72983 | if( p->expired ){ |
| 72240 | | - rc = SQLITE_ABORT; |
| 72984 | + rc = SQLITE_ABORT_ROLLBACK; |
| 72241 | 72985 | break; |
| 72242 | 72986 | } |
| 72243 | 72987 | |
| 72244 | 72988 | nField = 0; |
| 72245 | 72989 | pKeyInfo = 0; |
| | @@ -72799,14 +73543,15 @@ |
| 72799 | 73543 | } |
| 72800 | 73544 | pIdxKey = &r; |
| 72801 | 73545 | }else{ |
| 72802 | 73546 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 72803 | 73547 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 72804 | | - ); |
| 73548 | + ); |
| 72805 | 73549 | if( pIdxKey==0 ) goto no_mem; |
| 72806 | 73550 | assert( pIn3->flags & MEM_Blob ); |
| 72807 | | - assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 73551 | + /* assert( (pIn3->flags & MEM_Zero)==0 ); // zeroblobs already expanded */ |
| 73552 | + ExpandBlob(pIn3); |
| 72808 | 73553 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 72809 | 73554 | } |
| 72810 | 73555 | pIdxKey->default_rc = 0; |
| 72811 | 73556 | if( pOp->opcode==OP_NoConflict ){ |
| 72812 | 73557 | /* For the OP_NoConflict opcode, take the jump if any of the |
| | @@ -73492,13 +74237,13 @@ |
| 73492 | 74237 | } |
| 73493 | 74238 | /* Opcode: Rewind P1 P2 * * * |
| 73494 | 74239 | ** |
| 73495 | 74240 | ** The next use of the Rowid or Column or Next instruction for P1 |
| 73496 | 74241 | ** 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. |
| 74242 | +** If the table or index is empty, jump immediately to P2. |
| 74243 | +** If the table or index is not empty, fall through to the following |
| 74244 | +** instruction. |
| 73500 | 74245 | ** |
| 73501 | 74246 | ** This opcode leaves the cursor configured to move in forward order, |
| 73502 | 74247 | ** from the beginning toward the end. In other words, the cursor is |
| 73503 | 74248 | ** configured to use Next, not Prev. |
| 73504 | 74249 | */ |
| | @@ -74410,10 +75155,13 @@ |
| 74410 | 75155 | pFrame->aOp = p->aOp; |
| 74411 | 75156 | pFrame->nOp = p->nOp; |
| 74412 | 75157 | pFrame->token = pProgram->token; |
| 74413 | 75158 | pFrame->aOnceFlag = p->aOnceFlag; |
| 74414 | 75159 | pFrame->nOnceFlag = p->nOnceFlag; |
| 75160 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75161 | + pFrame->anExec = p->anExec; |
| 75162 | +#endif |
| 74415 | 75163 | |
| 74416 | 75164 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 74417 | 75165 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 74418 | 75166 | pMem->flags = MEM_Undefined; |
| 74419 | 75167 | pMem->db = db; |
| | @@ -74427,10 +75175,11 @@ |
| 74427 | 75175 | |
| 74428 | 75176 | p->nFrame++; |
| 74429 | 75177 | pFrame->pParent = p->pFrame; |
| 74430 | 75178 | pFrame->lastRowid = lastRowid; |
| 74431 | 75179 | pFrame->nChange = p->nChange; |
| 75180 | + pFrame->nDbChange = p->db->nChange; |
| 74432 | 75181 | p->nChange = 0; |
| 74433 | 75182 | p->pFrame = pFrame; |
| 74434 | 75183 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
| 74435 | 75184 | p->nMem = pFrame->nChildMem; |
| 74436 | 75185 | p->nCursor = (u16)pFrame->nChildCsr; |
| | @@ -74437,10 +75186,13 @@ |
| 74437 | 75186 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 74438 | 75187 | p->aOp = aOp = pProgram->aOp; |
| 74439 | 75188 | p->nOp = pProgram->nOp; |
| 74440 | 75189 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 74441 | 75190 | p->nOnceFlag = pProgram->nOnce; |
| 75191 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75192 | + p->anExec = 0; |
| 75193 | +#endif |
| 74442 | 75194 | pc = -1; |
| 74443 | 75195 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 74444 | 75196 | |
| 74445 | 75197 | break; |
| 74446 | 75198 | } |
| | @@ -75625,10 +76377,15 @@ |
| 75625 | 76377 | char *zErr = 0; |
| 75626 | 76378 | Table *pTab; |
| 75627 | 76379 | Parse *pParse = 0; |
| 75628 | 76380 | Incrblob *pBlob = 0; |
| 75629 | 76381 | |
| 76382 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 76383 | + if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){ |
| 76384 | + return SQLITE_MISUSE_BKPT; |
| 76385 | + } |
| 76386 | +#endif |
| 75630 | 76387 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 75631 | 76388 | *ppBlob = 0; |
| 75632 | 76389 | |
| 75633 | 76390 | sqlite3_mutex_enter(db->mutex); |
| 75634 | 76391 | |
| | @@ -75843,11 +76600,10 @@ |
| 75843 | 76600 | v = (Vdbe*)p->pStmt; |
| 75844 | 76601 | |
| 75845 | 76602 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 75846 | 76603 | /* Request is out of range. Return a transient error. */ |
| 75847 | 76604 | rc = SQLITE_ERROR; |
| 75848 | | - sqlite3Error(db, SQLITE_ERROR); |
| 75849 | 76605 | }else if( v==0 ){ |
| 75850 | 76606 | /* If there is no statement handle, then the blob-handle has |
| 75851 | 76607 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 75852 | 76608 | */ |
| 75853 | 76609 | rc = SQLITE_ABORT; |
| | @@ -75861,14 +76617,14 @@ |
| 75861 | 76617 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 75862 | 76618 | if( rc==SQLITE_ABORT ){ |
| 75863 | 76619 | sqlite3VdbeFinalize(v); |
| 75864 | 76620 | p->pStmt = 0; |
| 75865 | 76621 | }else{ |
| 75866 | | - db->errCode = rc; |
| 75867 | 76622 | v->rc = rc; |
| 75868 | 76623 | } |
| 75869 | 76624 | } |
| 76625 | + sqlite3Error(db, rc); |
| 75870 | 76626 | rc = sqlite3ApiExit(db, rc); |
| 75871 | 76627 | sqlite3_mutex_leave(db->mutex); |
| 75872 | 76628 | return rc; |
| 75873 | 76629 | } |
| 75874 | 76630 | |
| | @@ -76041,11 +76797,11 @@ |
| 76041 | 76797 | ** itself. |
| 76042 | 76798 | ** |
| 76043 | 76799 | ** The sorter is running in multi-threaded mode if (a) the library was built |
| 76044 | 76800 | ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
| 76045 | 76801 | ** than zero, and (b) worker threads have been enabled at runtime by calling |
| 76046 | | -** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...). |
| 76802 | +** "PRAGMA threads=N" with some value of N greater than 0. |
| 76047 | 76803 | ** |
| 76048 | 76804 | ** When Rewind() is called, any data remaining in memory is flushed to a |
| 76049 | 76805 | ** final PMA. So at this point the data is stored in some number of sorted |
| 76050 | 76806 | ** PMAs within temporary files on disk. |
| 76051 | 76807 | ** |
| | @@ -76786,15 +77542,13 @@ |
| 76786 | 77542 | pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 76787 | 77543 | mxCache = db->aDb[0].pSchema->cache_size; |
| 76788 | 77544 | if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 76789 | 77545 | pSorter->mxPmaSize = mxCache * pgsz; |
| 76790 | 77546 | |
| 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. |
| 77547 | + /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of |
| 77548 | + ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary |
| 77549 | + ** large heap allocations. |
| 76796 | 77550 | */ |
| 76797 | 77551 | if( sqlite3GlobalConfig.pScratch==0 ){ |
| 76798 | 77552 | assert( pSorter->iMemory==0 ); |
| 76799 | 77553 | pSorter->nMemory = pgsz; |
| 76800 | 77554 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| | @@ -79162,19 +79916,19 @@ |
| 79162 | 79916 | ** |
| 79163 | 79917 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 79164 | 79918 | ** is a helper function - a callback for the tree walker. |
| 79165 | 79919 | */ |
| 79166 | 79920 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 79167 | | - if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; |
| 79921 | + if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 79168 | 79922 | return WRC_Continue; |
| 79169 | 79923 | } |
| 79170 | 79924 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 79171 | 79925 | if( N>0 ){ |
| 79172 | 79926 | Walker w; |
| 79173 | 79927 | memset(&w, 0, sizeof(w)); |
| 79174 | 79928 | w.xExprCallback = incrAggDepth; |
| 79175 | | - w.u.i = N; |
| 79929 | + w.u.n = N; |
| 79176 | 79930 | sqlite3WalkExpr(&w, pExpr); |
| 79177 | 79931 | } |
| 79178 | 79932 | } |
| 79179 | 79933 | |
| 79180 | 79934 | /* |
| | @@ -79718,11 +80472,11 @@ |
| 79718 | 80472 | double r = -1.0; |
| 79719 | 80473 | if( p->op!=TK_FLOAT ) return -1; |
| 79720 | 80474 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 79721 | 80475 | assert( r>=0.0 ); |
| 79722 | 80476 | if( r>1.0 ) return -1; |
| 79723 | | - return (int)(r*1000.0); |
| 80477 | + return (int)(r*134217728.0); |
| 79724 | 80478 | } |
| 79725 | 80479 | |
| 79726 | 80480 | /* |
| 79727 | 80481 | ** This routine is callback for sqlite3WalkExpr(). |
| 79728 | 80482 | ** |
| | @@ -79850,11 +80604,11 @@ |
| 79850 | 80604 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for |
| 79851 | 80605 | ** likelihood(X,0.9375). |
| 79852 | 80606 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to |
| 79853 | 80607 | ** likelihood(X,0.9375). */ |
| 79854 | 80608 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
| 79855 | | - pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938; |
| 80609 | + pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; |
| 79856 | 80610 | } |
| 79857 | 80611 | } |
| 79858 | 80612 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 79859 | 80613 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 79860 | 80614 | if( auth!=SQLITE_OK ){ |
| | @@ -81807,69 +82561,79 @@ |
| 81807 | 82561 | sqlite3DbFree(db, pList->a); |
| 81808 | 82562 | sqlite3DbFree(db, pList); |
| 81809 | 82563 | } |
| 81810 | 82564 | |
| 81811 | 82565 | /* |
| 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. |
| 82566 | +** These routines are Walker callbacks used to check expressions to |
| 82567 | +** see if they are "constant" for some definition of constant. The |
| 82568 | +** Walker.eCode value determines the type of "constant" we are looking |
| 82569 | +** for. |
| 81816 | 82570 | ** |
| 81817 | 82571 | ** These callback routines are used to implement the following: |
| 81818 | 82572 | ** |
| 81819 | | -** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 81820 | | -** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 81821 | | -** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 82573 | +** sqlite3ExprIsConstant() pWalker->eCode==1 |
| 82574 | +** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
| 82575 | +** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 |
| 82576 | +** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
| 82577 | +** |
| 82578 | +** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
| 82579 | +** is found to not be a constant. |
| 81822 | 82580 | ** |
| 81823 | 82581 | ** 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 |
| 82582 | +** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing |
| 82583 | +** an existing schema and 4 when processing a new statement. A bound |
| 81826 | 82584 | ** parameter raises an error for new statements, but is silently converted |
| 81827 | 82585 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 81828 | 82586 | ** contain a bound parameter because they were generated by older versions |
| 81829 | 82587 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81830 | 82588 | ** malformed schema error. |
| 81831 | 82589 | */ |
| 81832 | 82590 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81833 | 82591 | |
| 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 |
| 82592 | + /* If pWalker->eCode is 2 then any term of the expression that comes from |
| 82593 | + ** the ON or USING clauses of a left join disqualifies the expression |
| 81836 | 82594 | ** from being considered constant. */ |
| 81837 | | - if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81838 | | - pWalker->u.i = 0; |
| 82595 | + if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 82596 | + pWalker->eCode = 0; |
| 81839 | 82597 | return WRC_Abort; |
| 81840 | 82598 | } |
| 81841 | 82599 | |
| 81842 | 82600 | switch( pExpr->op ){ |
| 81843 | 82601 | /* 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. */ |
| 82602 | + ** and either pWalker->eCode==4 or 5 or the function has the |
| 82603 | + ** SQLITE_FUNC_CONST flag. */ |
| 81846 | 82604 | case TK_FUNCTION: |
| 81847 | | - if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 82605 | + if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81848 | 82606 | return WRC_Continue; |
| 82607 | + }else{ |
| 82608 | + pWalker->eCode = 0; |
| 82609 | + return WRC_Abort; |
| 81849 | 82610 | } |
| 81850 | | - /* Fall through */ |
| 81851 | 82611 | case TK_ID: |
| 81852 | 82612 | case TK_COLUMN: |
| 81853 | 82613 | case TK_AGG_FUNCTION: |
| 81854 | 82614 | case TK_AGG_COLUMN: |
| 81855 | 82615 | testcase( pExpr->op==TK_ID ); |
| 81856 | 82616 | testcase( pExpr->op==TK_COLUMN ); |
| 81857 | 82617 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81858 | 82618 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81859 | | - pWalker->u.i = 0; |
| 81860 | | - return WRC_Abort; |
| 82619 | + if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ |
| 82620 | + return WRC_Continue; |
| 82621 | + }else{ |
| 82622 | + pWalker->eCode = 0; |
| 82623 | + return WRC_Abort; |
| 82624 | + } |
| 81861 | 82625 | case TK_VARIABLE: |
| 81862 | | - if( pWalker->u.i==4 ){ |
| 82626 | + if( pWalker->eCode==5 ){ |
| 81863 | 82627 | /* Silently convert bound parameters that appear inside of CREATE |
| 81864 | 82628 | ** statements into a NULL when parsing the CREATE statement text out |
| 81865 | 82629 | ** of the sqlite_master table */ |
| 81866 | 82630 | pExpr->op = TK_NULL; |
| 81867 | | - }else if( pWalker->u.i==3 ){ |
| 82631 | + }else if( pWalker->eCode==4 ){ |
| 81868 | 82632 | /* A bound parameter in a CREATE statement that originates from |
| 81869 | 82633 | ** sqlite3_prepare() causes an error */ |
| 81870 | | - pWalker->u.i = 0; |
| 82634 | + pWalker->eCode = 0; |
| 81871 | 82635 | return WRC_Abort; |
| 81872 | 82636 | } |
| 81873 | 82637 | /* Fall through */ |
| 81874 | 82638 | default: |
| 81875 | 82639 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| | @@ -81877,57 +82641,68 @@ |
| 81877 | 82641 | return WRC_Continue; |
| 81878 | 82642 | } |
| 81879 | 82643 | } |
| 81880 | 82644 | static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 81881 | 82645 | UNUSED_PARAMETER(NotUsed); |
| 81882 | | - pWalker->u.i = 0; |
| 82646 | + pWalker->eCode = 0; |
| 81883 | 82647 | return WRC_Abort; |
| 81884 | 82648 | } |
| 81885 | | -static int exprIsConst(Expr *p, int initFlag){ |
| 82649 | +static int exprIsConst(Expr *p, int initFlag, int iCur){ |
| 81886 | 82650 | Walker w; |
| 81887 | 82651 | memset(&w, 0, sizeof(w)); |
| 81888 | | - w.u.i = initFlag; |
| 82652 | + w.eCode = initFlag; |
| 81889 | 82653 | w.xExprCallback = exprNodeIsConstant; |
| 81890 | 82654 | w.xSelectCallback = selectNodeIsConstant; |
| 82655 | + w.u.iCur = iCur; |
| 81891 | 82656 | sqlite3WalkExpr(&w, p); |
| 81892 | | - return w.u.i; |
| 82657 | + return w.eCode; |
| 81893 | 82658 | } |
| 81894 | 82659 | |
| 81895 | 82660 | /* |
| 81896 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82661 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81897 | 82662 | ** and 0 if it involves variables or function calls. |
| 81898 | 82663 | ** |
| 81899 | 82664 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81900 | 82665 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81901 | 82666 | ** a constant. |
| 81902 | 82667 | */ |
| 81903 | 82668 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
| 81904 | | - return exprIsConst(p, 1); |
| 82669 | + return exprIsConst(p, 1, 0); |
| 81905 | 82670 | } |
| 81906 | 82671 | |
| 81907 | 82672 | /* |
| 81908 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82673 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81909 | 82674 | ** that does no originate from the ON or USING clauses of a join. |
| 81910 | 82675 | ** Return 0 if it involves variables or function calls or terms from |
| 81911 | 82676 | ** an ON or USING clause. |
| 81912 | 82677 | */ |
| 81913 | 82678 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81914 | | - return exprIsConst(p, 2); |
| 82679 | + return exprIsConst(p, 2, 0); |
| 81915 | 82680 | } |
| 81916 | 82681 | |
| 81917 | 82682 | /* |
| 81918 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82683 | +** Walk an expression tree. Return non-zero if the expression constant |
| 82684 | +** for any single row of the table with cursor iCur. In other words, the |
| 82685 | +** expression must not refer to any non-deterministic function nor any |
| 82686 | +** table other than iCur. |
| 82687 | +*/ |
| 82688 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
| 82689 | + return exprIsConst(p, 3, iCur); |
| 82690 | +} |
| 82691 | + |
| 82692 | +/* |
| 82693 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81919 | 82694 | ** or a function call with constant arguments. Return and 0 if there |
| 81920 | 82695 | ** are any variables. |
| 81921 | 82696 | ** |
| 81922 | 82697 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81923 | 82698 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81924 | 82699 | ** a constant. |
| 81925 | 82700 | */ |
| 81926 | 82701 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81927 | 82702 | assert( isInit==0 || isInit==1 ); |
| 81928 | | - return exprIsConst(p, 3+isInit); |
| 82703 | + return exprIsConst(p, 4+isInit, 0); |
| 81929 | 82704 | } |
| 81930 | 82705 | |
| 81931 | 82706 | /* |
| 81932 | 82707 | ** If the expression p codes a constant integer that is small enough |
| 81933 | 82708 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| | @@ -87374,10 +88149,11 @@ |
| 87374 | 88149 | nSample--; |
| 87375 | 88150 | }else{ |
| 87376 | 88151 | nRow = pIdx->aiRowEst[0]; |
| 87377 | 88152 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87378 | 88153 | } |
| 88154 | + pIdx->nRowEst0 = nRow; |
| 87379 | 88155 | |
| 87380 | 88156 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87381 | 88157 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87382 | 88158 | ** the nEq values for column iCol for the same set (adding the value |
| 87383 | 88159 | ** only once where there exist duplicate prefixes). */ |
| | @@ -87635,11 +88411,11 @@ |
| 87635 | 88411 | } |
| 87636 | 88412 | |
| 87637 | 88413 | |
| 87638 | 88414 | /* Load the statistics from the sqlite_stat4 table. */ |
| 87639 | 88415 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87640 | | - if( rc==SQLITE_OK ){ |
| 88416 | + if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ |
| 87641 | 88417 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87642 | 88418 | db->lookaside.bEnabled = 0; |
| 87643 | 88419 | rc = loadStat4(db, sInfo.zDatabase); |
| 87644 | 88420 | db->lookaside.bEnabled = lookasideEnabled; |
| 87645 | 88421 | } |
| | @@ -88317,10 +89093,13 @@ |
| 88317 | 89093 | SQLITE_API int sqlite3_set_authorizer( |
| 88318 | 89094 | sqlite3 *db, |
| 88319 | 89095 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
| 88320 | 89096 | void *pArg |
| 88321 | 89097 | ){ |
| 89098 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 89099 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 89100 | +#endif |
| 88322 | 89101 | sqlite3_mutex_enter(db->mutex); |
| 88323 | 89102 | db->xAuth = (sqlite3_xauth)xAuth; |
| 88324 | 89103 | db->pAuthArg = pArg; |
| 88325 | 89104 | sqlite3ExpirePreparedStatements(db); |
| 88326 | 89105 | sqlite3_mutex_leave(db->mutex); |
| | @@ -88811,11 +89590,15 @@ |
| 88811 | 89590 | ** See also sqlite3LocateTable(). |
| 88812 | 89591 | */ |
| 88813 | 89592 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
| 88814 | 89593 | Table *p = 0; |
| 88815 | 89594 | int i; |
| 88816 | | - assert( zName!=0 ); |
| 89595 | + |
| 89596 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 89597 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0; |
| 89598 | +#endif |
| 89599 | + |
| 88817 | 89600 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 88818 | 89601 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 88819 | 89602 | #if SQLITE_USER_AUTHENTICATION |
| 88820 | 89603 | /* Only the admin user is allowed to know that the sqlite_user table |
| 88821 | 89604 | ** exists */ |
| | @@ -103834,13 +104617,16 @@ |
| 103834 | 104617 | Vdbe *pOld, /* VM being reprepared */ |
| 103835 | 104618 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 103836 | 104619 | const char **pzTail /* OUT: End of parsed string */ |
| 103837 | 104620 | ){ |
| 103838 | 104621 | int rc; |
| 103839 | | - assert( ppStmt!=0 ); |
| 104622 | + |
| 104623 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 104624 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 104625 | +#endif |
| 103840 | 104626 | *ppStmt = 0; |
| 103841 | | - if( !sqlite3SafetyCheckOk(db) ){ |
| 104627 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 103842 | 104628 | return SQLITE_MISUSE_BKPT; |
| 103843 | 104629 | } |
| 103844 | 104630 | sqlite3_mutex_enter(db->mutex); |
| 103845 | 104631 | sqlite3BtreeEnterAll(db); |
| 103846 | 104632 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| | @@ -103943,13 +104729,15 @@ |
| 103943 | 104729 | */ |
| 103944 | 104730 | char *zSql8; |
| 103945 | 104731 | const char *zTail8 = 0; |
| 103946 | 104732 | int rc = SQLITE_OK; |
| 103947 | 104733 | |
| 103948 | | - assert( ppStmt ); |
| 104734 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 104735 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 104736 | +#endif |
| 103949 | 104737 | *ppStmt = 0; |
| 103950 | | - if( !sqlite3SafetyCheckOk(db) ){ |
| 104738 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 103951 | 104739 | return SQLITE_MISUSE_BKPT; |
| 103952 | 104740 | } |
| 103953 | 104741 | if( nBytes>=0 ){ |
| 103954 | 104742 | int sz; |
| 103955 | 104743 | const char *z = (const char*)zSql; |
| | @@ -109658,10 +110446,13 @@ |
| 109658 | 110446 | char **pzErrMsg /* Write error messages here */ |
| 109659 | 110447 | ){ |
| 109660 | 110448 | int rc; |
| 109661 | 110449 | TabResult res; |
| 109662 | 110450 | |
| 110451 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 110452 | + if( pazResult==0 ) return SQLITE_MISUSE_BKPT; |
| 110453 | +#endif |
| 109663 | 110454 | *pazResult = 0; |
| 109664 | 110455 | if( pnColumn ) *pnColumn = 0; |
| 109665 | 110456 | if( pnRow ) *pnRow = 0; |
| 109666 | 110457 | if( pzErrMsg ) *pzErrMsg = 0; |
| 109667 | 110458 | res.zErrMsg = 0; |
| | @@ -111721,11 +112512,11 @@ |
| 111721 | 112512 | ** Two writes per page are required in step (3) because the original |
| 111722 | 112513 | ** database content must be written into the rollback journal prior to |
| 111723 | 112514 | ** overwriting the database with the vacuumed content. |
| 111724 | 112515 | ** |
| 111725 | 112516 | ** 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 |
| 112517 | +** the copy of step (3) were replaced by deleting the original database |
| 111727 | 112518 | ** and renaming the transient database as the original. But that will |
| 111728 | 112519 | ** not work if other processes are attached to the original database. |
| 111729 | 112520 | ** And a power loss in between deleting the original and renaming the |
| 111730 | 112521 | ** transient would cause the database file to appear to be deleted |
| 111731 | 112522 | ** following reboot. |
| | @@ -112079,10 +112870,13 @@ |
| 112079 | 112870 | sqlite3 *db, /* Database in which module is registered */ |
| 112080 | 112871 | const char *zName, /* Name assigned to this module */ |
| 112081 | 112872 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112082 | 112873 | void *pAux /* Context pointer for xCreate/xConnect */ |
| 112083 | 112874 | ){ |
| 112875 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 112876 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 112877 | +#endif |
| 112084 | 112878 | return createModule(db, zName, pModule, pAux, 0); |
| 112085 | 112879 | } |
| 112086 | 112880 | |
| 112087 | 112881 | /* |
| 112088 | 112882 | ** External API function used to create a new virtual-table module. |
| | @@ -112092,10 +112886,13 @@ |
| 112092 | 112886 | const char *zName, /* Name assigned to this module */ |
| 112093 | 112887 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112094 | 112888 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 112095 | 112889 | void (*xDestroy)(void *) /* Module destructor function */ |
| 112096 | 112890 | ){ |
| 112891 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 112892 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 112893 | +#endif |
| 112097 | 112894 | return createModule(db, zName, pModule, pAux, xDestroy); |
| 112098 | 112895 | } |
| 112099 | 112896 | |
| 112100 | 112897 | /* |
| 112101 | 112898 | ** Lock the virtual table so that it cannot be disconnected. |
| | @@ -112696,10 +113493,13 @@ |
| 112696 | 113493 | |
| 112697 | 113494 | int rc = SQLITE_OK; |
| 112698 | 113495 | Table *pTab; |
| 112699 | 113496 | char *zErr = 0; |
| 112700 | 113497 | |
| 113498 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113499 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 113500 | +#endif |
| 112701 | 113501 | sqlite3_mutex_enter(db->mutex); |
| 112702 | 113502 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 112703 | 113503 | sqlite3Error(db, SQLITE_MISUSE); |
| 112704 | 113504 | sqlite3_mutex_leave(db->mutex); |
| 112705 | 113505 | return SQLITE_MISUSE_BKPT; |
| | @@ -113052,10 +113852,13 @@ |
| 113052 | 113852 | */ |
| 113053 | 113853 | SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 113054 | 113854 | static const unsigned char aMap[] = { |
| 113055 | 113855 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 113056 | 113856 | }; |
| 113857 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113858 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 113859 | +#endif |
| 113057 | 113860 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 113058 | 113861 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 113059 | 113862 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 113060 | 113863 | return (int)aMap[db->vtabOnConflict-1]; |
| 113061 | 113864 | } |
| | @@ -113067,12 +113870,14 @@ |
| 113067 | 113870 | */ |
| 113068 | 113871 | SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 113069 | 113872 | va_list ap; |
| 113070 | 113873 | int rc = SQLITE_OK; |
| 113071 | 113874 | |
| 113875 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113876 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 113877 | +#endif |
| 113072 | 113878 | sqlite3_mutex_enter(db->mutex); |
| 113073 | | - |
| 113074 | 113879 | va_start(ap, op); |
| 113075 | 113880 | switch( op ){ |
| 113076 | 113881 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 113077 | 113882 | VtabCtx *p = db->pVtabCtx; |
| 113078 | 113883 | if( !p ){ |
| | @@ -113203,10 +114008,13 @@ |
| 113203 | 114008 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 113204 | 114009 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 113205 | 114010 | } u; |
| 113206 | 114011 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 113207 | 114012 | Bitmask notReady; /* FROM entries not usable at this level */ |
| 114013 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 114014 | + int addrVisit; /* Address at which row is visited */ |
| 114015 | +#endif |
| 113208 | 114016 | }; |
| 113209 | 114017 | |
| 113210 | 114018 | /* |
| 113211 | 114019 | ** Each instance of this object represents an algorithm for evaluating one |
| 113212 | 114020 | ** term of a join. Every term of the FROM clause will have at least |
| | @@ -113233,11 +114041,10 @@ |
| 113233 | 114041 | LogEst rRun; /* Cost of running each loop */ |
| 113234 | 114042 | LogEst nOut; /* Estimated number of output rows */ |
| 113235 | 114043 | union { |
| 113236 | 114044 | struct { /* Information for internal btree tables */ |
| 113237 | 114045 | u16 nEq; /* Number of equality constraints */ |
| 113238 | | - u16 nSkip; /* Number of initial index columns to skip */ |
| 113239 | 114046 | Index *pIndex; /* Index used, or NULL */ |
| 113240 | 114047 | } btree; |
| 113241 | 114048 | struct { /* Information for virtual tables */ |
| 113242 | 114049 | int idxNum; /* Index number */ |
| 113243 | 114050 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| | @@ -113246,16 +114053,17 @@ |
| 113246 | 114053 | char *idxStr; /* Index identifier string */ |
| 113247 | 114054 | } vtab; |
| 113248 | 114055 | } u; |
| 113249 | 114056 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 113250 | 114057 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 114058 | + u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| 113251 | 114059 | /**** whereLoopXfer() copies fields above ***********************/ |
| 113252 | 114060 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 113253 | 114061 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 113254 | 114062 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 113255 | 114063 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 113256 | | - WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 114064 | + WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
| 113257 | 114065 | }; |
| 113258 | 114066 | |
| 113259 | 114067 | /* This object holds the prerequisites and the cost of running a |
| 113260 | 114068 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 113261 | 114069 | ** See WhereOrSet for additional information |
| | @@ -113577,10 +114385,11 @@ |
| 113577 | 114385 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 113578 | 114386 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 113579 | 114387 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 113580 | 114388 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 113581 | 114389 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 114390 | +#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 113582 | 114391 | |
| 113583 | 114392 | /************** End of whereInt.h ********************************************/ |
| 113584 | 114393 | /************** Continuing where we left off in where.c **********************/ |
| 113585 | 114394 | |
| 113586 | 114395 | /* |
| | @@ -113787,11 +114596,11 @@ |
| 113787 | 114596 | } |
| 113788 | 114597 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 113789 | 114598 | } |
| 113790 | 114599 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 113791 | 114600 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 113792 | | - pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; |
| 114601 | + pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
| 113793 | 114602 | }else{ |
| 113794 | 114603 | pTerm->truthProb = 1; |
| 113795 | 114604 | } |
| 113796 | 114605 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 113797 | 114606 | pTerm->wtFlags = wtFlags; |
| | @@ -114317,10 +115126,19 @@ |
| 114317 | 115126 | if( pDerived ){ |
| 114318 | 115127 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 114319 | 115128 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 114320 | 115129 | } |
| 114321 | 115130 | } |
| 115131 | + |
| 115132 | +/* |
| 115133 | +** Mark term iChild as being a child of term iParent |
| 115134 | +*/ |
| 115135 | +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 115136 | + pWC->a[iChild].iParent = iParent; |
| 115137 | + pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 115138 | + pWC->a[iParent].nChild++; |
| 115139 | +} |
| 114322 | 115140 | |
| 114323 | 115141 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114324 | 115142 | /* |
| 114325 | 115143 | ** Analyze a term that consists of two or more OR-connected |
| 114326 | 115144 | ** subterms. So in: |
| | @@ -114615,12 +115433,11 @@ |
| 114615 | 115433 | pNew->x.pList = pList; |
| 114616 | 115434 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114617 | 115435 | testcase( idxNew==0 ); |
| 114618 | 115436 | exprAnalyze(pSrc, pWC, idxNew); |
| 114619 | 115437 | pTerm = &pWC->a[idxTerm]; |
| 114620 | | - pWC->a[idxNew].iParent = idxTerm; |
| 114621 | | - pTerm->nChild = 1; |
| 115438 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114622 | 115439 | }else{ |
| 114623 | 115440 | sqlite3ExprListDelete(db, pList); |
| 114624 | 115441 | } |
| 114625 | 115442 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 114626 | 115443 | } |
| | @@ -114718,13 +115535,12 @@ |
| 114718 | 115535 | return; |
| 114719 | 115536 | } |
| 114720 | 115537 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114721 | 115538 | if( idxNew==0 ) return; |
| 114722 | 115539 | pNew = &pWC->a[idxNew]; |
| 114723 | | - pNew->iParent = idxTerm; |
| 115540 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114724 | 115541 | pTerm = &pWC->a[idxTerm]; |
| 114725 | | - pTerm->nChild = 1; |
| 114726 | 115542 | pTerm->wtFlags |= TERM_COPIED; |
| 114727 | 115543 | if( pExpr->op==TK_EQ |
| 114728 | 115544 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 114729 | 115545 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 114730 | 115546 | ){ |
| | @@ -114777,13 +115593,12 @@ |
| 114777 | 115593 | transferJoinMarkings(pNewExpr, pExpr); |
| 114778 | 115594 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114779 | 115595 | testcase( idxNew==0 ); |
| 114780 | 115596 | exprAnalyze(pSrc, pWC, idxNew); |
| 114781 | 115597 | pTerm = &pWC->a[idxTerm]; |
| 114782 | | - pWC->a[idxNew].iParent = idxTerm; |
| 115598 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114783 | 115599 | } |
| 114784 | | - pTerm->nChild = 2; |
| 114785 | 115600 | } |
| 114786 | 115601 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 114787 | 115602 | |
| 114788 | 115603 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114789 | 115604 | /* Analyze a term that is composed of two or more subterms connected by |
| | @@ -114854,13 +115669,12 @@ |
| 114854 | 115669 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114855 | 115670 | testcase( idxNew2==0 ); |
| 114856 | 115671 | exprAnalyze(pSrc, pWC, idxNew2); |
| 114857 | 115672 | pTerm = &pWC->a[idxTerm]; |
| 114858 | 115673 | if( isComplete ){ |
| 114859 | | - pWC->a[idxNew1].iParent = idxTerm; |
| 114860 | | - pWC->a[idxNew2].iParent = idxTerm; |
| 114861 | | - pTerm->nChild = 2; |
| 115674 | + markTermAsChild(pWC, idxNew1, idxTerm); |
| 115675 | + markTermAsChild(pWC, idxNew2, idxTerm); |
| 114862 | 115676 | } |
| 114863 | 115677 | } |
| 114864 | 115678 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 114865 | 115679 | |
| 114866 | 115680 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -114889,13 +115703,12 @@ |
| 114889 | 115703 | pNewTerm = &pWC->a[idxNew]; |
| 114890 | 115704 | pNewTerm->prereqRight = prereqExpr; |
| 114891 | 115705 | pNewTerm->leftCursor = pLeft->iTable; |
| 114892 | 115706 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114893 | 115707 | pNewTerm->eOperator = WO_MATCH; |
| 114894 | | - pNewTerm->iParent = idxTerm; |
| 115708 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114895 | 115709 | pTerm = &pWC->a[idxTerm]; |
| 114896 | | - pTerm->nChild = 1; |
| 114897 | 115710 | pTerm->wtFlags |= TERM_COPIED; |
| 114898 | 115711 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114899 | 115712 | } |
| 114900 | 115713 | } |
| 114901 | 115714 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -114912,11 +115725,11 @@ |
| 114912 | 115725 | ** the start of the loop will prevent any results from being returned. |
| 114913 | 115726 | */ |
| 114914 | 115727 | if( pExpr->op==TK_NOTNULL |
| 114915 | 115728 | && pExpr->pLeft->op==TK_COLUMN |
| 114916 | 115729 | && pExpr->pLeft->iColumn>=0 |
| 114917 | | - && OptimizationEnabled(db, SQLITE_Stat3) |
| 115730 | + && OptimizationEnabled(db, SQLITE_Stat34) |
| 114918 | 115731 | ){ |
| 114919 | 115732 | Expr *pNewExpr; |
| 114920 | 115733 | Expr *pLeft = pExpr->pLeft; |
| 114921 | 115734 | int idxNew; |
| 114922 | 115735 | WhereTerm *pNewTerm; |
| | @@ -114931,13 +115744,12 @@ |
| 114931 | 115744 | pNewTerm = &pWC->a[idxNew]; |
| 114932 | 115745 | pNewTerm->prereqRight = 0; |
| 114933 | 115746 | pNewTerm->leftCursor = pLeft->iTable; |
| 114934 | 115747 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114935 | 115748 | pNewTerm->eOperator = WO_GT; |
| 114936 | | - pNewTerm->iParent = idxTerm; |
| 115749 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114937 | 115750 | pTerm = &pWC->a[idxTerm]; |
| 114938 | | - pTerm->nChild = 1; |
| 114939 | 115751 | pTerm->wtFlags |= TERM_COPIED; |
| 114940 | 115752 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114941 | 115753 | } |
| 114942 | 115754 | } |
| 114943 | 115755 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| | @@ -115153,10 +115965,12 @@ |
| 115153 | 115965 | WhereLoop *pLoop; /* The Loop object */ |
| 115154 | 115966 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 115155 | 115967 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 115156 | 115968 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 115157 | 115969 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 115970 | + Expr *pPartial = 0; /* Partial Index Expression */ |
| 115971 | + int iContinue = 0; /* Jump here to skip excluded rows */ |
| 115158 | 115972 | |
| 115159 | 115973 | /* Generate code to skip over the creation and initialization of the |
| 115160 | 115974 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 115161 | 115975 | v = pParse->pVdbe; |
| 115162 | 115976 | assert( v!=0 ); |
| | @@ -115168,10 +115982,16 @@ |
| 115168 | 115982 | pTable = pSrc->pTab; |
| 115169 | 115983 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 115170 | 115984 | pLoop = pLevel->pWLoop; |
| 115171 | 115985 | idxCols = 0; |
| 115172 | 115986 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 115987 | + if( pLoop->prereq==0 |
| 115988 | + && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
| 115989 | + && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ |
| 115990 | + pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 115991 | + sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); |
| 115992 | + } |
| 115173 | 115993 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 115174 | 115994 | int iCol = pTerm->u.leftColumn; |
| 115175 | 115995 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 115176 | 115996 | testcase( iCol==BMS ); |
| 115177 | 115997 | testcase( iCol==BMS-1 ); |
| | @@ -115180,11 +116000,13 @@ |
| 115180 | 116000 | "automatic index on %s(%s)", pTable->zName, |
| 115181 | 116001 | pTable->aCol[iCol].zName); |
| 115182 | 116002 | sentWarning = 1; |
| 115183 | 116003 | } |
| 115184 | 116004 | if( (idxCols & cMask)==0 ){ |
| 115185 | | - if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; |
| 116005 | + if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 116006 | + goto end_auto_index_create; |
| 116007 | + } |
| 115186 | 116008 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 115187 | 116009 | idxCols |= cMask; |
| 115188 | 116010 | } |
| 115189 | 116011 | } |
| 115190 | 116012 | } |
| | @@ -115200,24 +116022,23 @@ |
| 115200 | 116022 | ** be a covering index because the index will not be updated if the |
| 115201 | 116023 | ** original table changes and the index and table cannot both be used |
| 115202 | 116024 | ** if they go out of sync. |
| 115203 | 116025 | */ |
| 115204 | 116026 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 115205 | | - mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 116027 | + mxBitCol = MIN(BMS-1,pTable->nCol); |
| 115206 | 116028 | testcase( pTable->nCol==BMS-1 ); |
| 115207 | 116029 | testcase( pTable->nCol==BMS-2 ); |
| 115208 | 116030 | for(i=0; i<mxBitCol; i++){ |
| 115209 | 116031 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 115210 | 116032 | } |
| 115211 | 116033 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 115212 | 116034 | nKeyCol += pTable->nCol - BMS + 1; |
| 115213 | 116035 | } |
| 115214 | | - pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
| 115215 | 116036 | |
| 115216 | 116037 | /* Construct the Index object to describe this index */ |
| 115217 | 116038 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 115218 | | - if( pIdx==0 ) return; |
| 116039 | + if( pIdx==0 ) goto end_auto_index_create; |
| 115219 | 116040 | pLoop->u.btree.pIndex = pIdx; |
| 115220 | 116041 | pIdx->zName = "auto-index"; |
| 115221 | 116042 | pIdx->pTable = pTable; |
| 115222 | 116043 | n = 0; |
| 115223 | 116044 | idxCols = 0; |
| | @@ -115265,22 +116086,33 @@ |
| 115265 | 116086 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 115266 | 116087 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 115267 | 116088 | VdbeComment((v, "for %s", pTable->zName)); |
| 115268 | 116089 | |
| 115269 | 116090 | /* Fill the automatic index with content */ |
| 116091 | + sqlite3ExprCachePush(pParse); |
| 115270 | 116092 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 116093 | + if( pPartial ){ |
| 116094 | + iContinue = sqlite3VdbeMakeLabel(v); |
| 116095 | + sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
| 116096 | + pLoop->wsFlags |= WHERE_PARTIALIDX; |
| 116097 | + } |
| 115271 | 116098 | regRecord = sqlite3GetTempReg(pParse); |
| 115272 | 116099 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 115273 | 116100 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 115274 | 116101 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 116102 | + if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
| 115275 | 116103 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 115276 | 116104 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 115277 | 116105 | sqlite3VdbeJumpHere(v, addrTop); |
| 115278 | 116106 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 116107 | + sqlite3ExprCachePop(pParse); |
| 115279 | 116108 | |
| 115280 | 116109 | /* Jump here when skipping the initialization */ |
| 115281 | 116110 | sqlite3VdbeJumpHere(v, addrInit); |
| 116111 | + |
| 116112 | +end_auto_index_create: |
| 116113 | + sqlite3ExprDelete(pParse->db, pPartial); |
| 115282 | 116114 | } |
| 115283 | 116115 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 115284 | 116116 | |
| 115285 | 116117 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115286 | 116118 | /* |
| | @@ -115436,22 +116268,22 @@ |
| 115436 | 116268 | |
| 115437 | 116269 | return pParse->nErr; |
| 115438 | 116270 | } |
| 115439 | 116271 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 115440 | 116272 | |
| 115441 | | - |
| 115442 | 116273 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115443 | 116274 | /* |
| 115444 | 116275 | ** Estimate the location of a particular key among all keys in an |
| 115445 | 116276 | ** index. Store the results in aStat as follows: |
| 115446 | 116277 | ** |
| 115447 | 116278 | ** aStat[0] Est. number of rows less than pVal |
| 115448 | 116279 | ** aStat[1] Est. number of rows equal to pVal |
| 115449 | 116280 | ** |
| 115450 | | -** Return SQLITE_OK on success. |
| 116281 | +** Return the index of the sample that is the smallest sample that |
| 116282 | +** is greater than or equal to pRec. |
| 115451 | 116283 | */ |
| 115452 | | -static void whereKeyStats( |
| 116284 | +static int whereKeyStats( |
| 115453 | 116285 | Parse *pParse, /* Database connection */ |
| 115454 | 116286 | Index *pIdx, /* Index to consider domain of */ |
| 115455 | 116287 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| 115456 | 116288 | int roundUp, /* Round up if true. Round down if false */ |
| 115457 | 116289 | tRowcnt *aStat /* OUT: stats written here */ |
| | @@ -115529,10 +116361,11 @@ |
| 115529 | 116361 | }else{ |
| 115530 | 116362 | iGap = iGap/3; |
| 115531 | 116363 | } |
| 115532 | 116364 | aStat[0] = iLower + iGap; |
| 115533 | 116365 | } |
| 116366 | + return i; |
| 115534 | 116367 | } |
| 115535 | 116368 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 115536 | 116369 | |
| 115537 | 116370 | /* |
| 115538 | 116371 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| | @@ -115679,11 +116512,11 @@ |
| 115679 | 116512 | ** pLower pUpper |
| 115680 | 116513 | ** |
| 115681 | 116514 | ** If either of the upper or lower bound is not present, then NULL is passed in |
| 115682 | 116515 | ** place of the corresponding WhereTerm. |
| 115683 | 116516 | ** |
| 115684 | | -** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 116517 | +** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index |
| 115685 | 116518 | ** column subject to the range constraint. Or, equivalently, the number of |
| 115686 | 116519 | ** equality constraints optimized by the proposed index scan. For example, |
| 115687 | 116520 | ** assuming index p is on t1(a, b), and the SQL query is: |
| 115688 | 116521 | ** |
| 115689 | 116522 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| | @@ -115695,11 +116528,11 @@ |
| 115695 | 116528 | ** |
| 115696 | 116529 | ** then nEq is set to 0. |
| 115697 | 116530 | ** |
| 115698 | 116531 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 115699 | 116532 | ** 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 |
| 116533 | +** considering the range constraints. If nEq is 0, then *pnOut is the number of |
| 115701 | 116534 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 115702 | 116535 | ** to account for the range constraints pLower and pUpper. |
| 115703 | 116536 | ** |
| 115704 | 116537 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 115705 | 116538 | ** used, a single range inequality reduces the search space by a factor of 4. |
| | @@ -115719,14 +116552,11 @@ |
| 115719 | 116552 | |
| 115720 | 116553 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115721 | 116554 | Index *p = pLoop->u.btree.pIndex; |
| 115722 | 116555 | int nEq = pLoop->u.btree.nEq; |
| 115723 | 116556 | |
| 115724 | | - if( p->nSample>0 |
| 115725 | | - && nEq<p->nSampleCol |
| 115726 | | - && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 115727 | | - ){ |
| 116557 | + if( p->nSample>0 && nEq<p->nSampleCol ){ |
| 115728 | 116558 | if( nEq==pBuilder->nRecValid ){ |
| 115729 | 116559 | UnpackedRecord *pRec = pBuilder->pRec; |
| 115730 | 116560 | tRowcnt a[2]; |
| 115731 | 116561 | u8 aff; |
| 115732 | 116562 | |
| | @@ -115738,19 +116568,23 @@ |
| 115738 | 116568 | ** |
| 115739 | 116569 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 115740 | 116570 | ** is not a simple variable or literal value), the lower bound of the |
| 115741 | 116571 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 115742 | 116572 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 115743 | | - ** ($P:$L) and the larger of the two returned values used. |
| 116573 | + ** ($P:$L) and the larger of the two returned values is used. |
| 115744 | 116574 | ** |
| 115745 | 116575 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 115746 | 116576 | ** less than the upper bound of the range query. Where the upper bound |
| 115747 | 116577 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 115748 | 116578 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 116579 | + ** |
| 116580 | + ** The number of rows between the two bounds is then just iUpper-iLower. |
| 115749 | 116581 | */ |
| 115750 | | - tRowcnt iLower; |
| 115751 | | - tRowcnt iUpper; |
| 116582 | + tRowcnt iLower; /* Rows less than the lower bound */ |
| 116583 | + tRowcnt iUpper; /* Rows less than the upper bound */ |
| 116584 | + int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 116585 | + int iUprIdx = -1; /* aSample[] for the upper bound */ |
| 115752 | 116586 | |
| 115753 | 116587 | if( pRec ){ |
| 115754 | 116588 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 115755 | 116589 | pRec->nField = pBuilder->nRecValid; |
| 115756 | 116590 | } |
| | @@ -115760,11 +116594,11 @@ |
| 115760 | 116594 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 115761 | 116595 | } |
| 115762 | 116596 | /* Determine iLower and iUpper using ($P) only. */ |
| 115763 | 116597 | if( nEq==0 ){ |
| 115764 | 116598 | iLower = 0; |
| 115765 | | - iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
| 116599 | + iUpper = p->nRowEst0; |
| 115766 | 116600 | }else{ |
| 115767 | 116601 | /* Note: this call could be optimized away - since the same values must |
| 115768 | 116602 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115769 | 116603 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115770 | 116604 | iLower = a[0]; |
| | @@ -115784,11 +116618,11 @@ |
| 115784 | 116618 | int bOk; /* True if value is extracted from pExpr */ |
| 115785 | 116619 | Expr *pExpr = pLower->pExpr->pRight; |
| 115786 | 116620 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115787 | 116621 | if( rc==SQLITE_OK && bOk ){ |
| 115788 | 116622 | tRowcnt iNew; |
| 115789 | | - whereKeyStats(pParse, p, pRec, 0, a); |
| 116623 | + iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
| 115790 | 116624 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115791 | 116625 | if( iNew>iLower ) iLower = iNew; |
| 115792 | 116626 | nOut--; |
| 115793 | 116627 | pLower = 0; |
| 115794 | 116628 | } |
| | @@ -115799,11 +116633,11 @@ |
| 115799 | 116633 | int bOk; /* True if value is extracted from pExpr */ |
| 115800 | 116634 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115801 | 116635 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115802 | 116636 | if( rc==SQLITE_OK && bOk ){ |
| 115803 | 116637 | tRowcnt iNew; |
| 115804 | | - whereKeyStats(pParse, p, pRec, 1, a); |
| 116638 | + iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
| 115805 | 116639 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115806 | 116640 | if( iNew<iUpper ) iUpper = iNew; |
| 115807 | 116641 | nOut--; |
| 115808 | 116642 | pUpper = 0; |
| 115809 | 116643 | } |
| | @@ -115811,10 +116645,15 @@ |
| 115811 | 116645 | |
| 115812 | 116646 | pBuilder->pRec = pRec; |
| 115813 | 116647 | if( rc==SQLITE_OK ){ |
| 115814 | 116648 | if( iUpper>iLower ){ |
| 115815 | 116649 | nNew = sqlite3LogEst(iUpper - iLower); |
| 116650 | + /* TUNING: If both iUpper and iLower are derived from the same |
| 116651 | + ** sample, then assume they are 4x more selective. This brings |
| 116652 | + ** the estimated selectivity more in line with what it would be |
| 116653 | + ** if estimated without the use of STAT3/4 tables. */ |
| 116654 | + if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
| 115816 | 116655 | }else{ |
| 115817 | 116656 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 115818 | 116657 | } |
| 115819 | 116658 | if( nNew<nOut ){ |
| 115820 | 116659 | nOut = nNew; |
| | @@ -115835,16 +116674,19 @@ |
| 115835 | 116674 | #endif |
| 115836 | 116675 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 115837 | 116676 | nNew = whereRangeAdjust(pLower, nOut); |
| 115838 | 116677 | nNew = whereRangeAdjust(pUpper, nNew); |
| 115839 | 116678 | |
| 115840 | | - /* TUNING: If there is both an upper and lower limit, assume the range is |
| 116679 | + /* TUNING: If there is both an upper and lower limit and neither limit |
| 116680 | + ** has an application-defined likelihood(), assume the range is |
| 115841 | 116681 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 115842 | 116682 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 115843 | 116683 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 115844 | 116684 | ** match 1/64 of the index. */ |
| 115845 | | - if( pLower && pUpper ) nNew -= 20; |
| 116685 | + if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 116686 | + nNew -= 20; |
| 116687 | + } |
| 115846 | 116688 | |
| 115847 | 116689 | nOut -= (pLower!=0) + (pUpper!=0); |
| 115848 | 116690 | if( nNew<10 ) nNew = 10; |
| 115849 | 116691 | if( nNew<nOut ) nOut = nNew; |
| 115850 | 116692 | #if defined(WHERETRACE_ENABLED) |
| | @@ -116200,11 +117042,11 @@ |
| 116200 | 117042 | |
| 116201 | 117043 | /* This module is only called on query plans that use an index. */ |
| 116202 | 117044 | pLoop = pLevel->pWLoop; |
| 116203 | 117045 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 116204 | 117046 | nEq = pLoop->u.btree.nEq; |
| 116205 | | - nSkip = pLoop->u.btree.nSkip; |
| 117047 | + nSkip = pLoop->nSkip; |
| 116206 | 117048 | pIdx = pLoop->u.btree.pIndex; |
| 116207 | 117049 | assert( pIdx!=0 ); |
| 116208 | 117050 | |
| 116209 | 117051 | /* Figure out how many memory cells we will need then allocate them. |
| 116210 | 117052 | */ |
| | @@ -116314,11 +117156,11 @@ |
| 116314 | 117156 | ** "a=? AND b>?" |
| 116315 | 117157 | */ |
| 116316 | 117158 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116317 | 117159 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116318 | 117160 | u16 nEq = pLoop->u.btree.nEq; |
| 116319 | | - u16 nSkip = pLoop->u.btree.nSkip; |
| 117161 | + u16 nSkip = pLoop->nSkip; |
| 116320 | 117162 | int i, j; |
| 116321 | 117163 | Column *aCol = pTab->aCol; |
| 116322 | 117164 | i16 *aiColumn = pIndex->aiColumn; |
| 116323 | 117165 | |
| 116324 | 117166 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| | @@ -116345,23 +117187,27 @@ |
| 116345 | 117187 | sqlite3StrAccumAppend(pStr, ")", 1); |
| 116346 | 117188 | } |
| 116347 | 117189 | |
| 116348 | 117190 | /* |
| 116349 | 117191 | ** 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. |
| 117192 | +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 117193 | +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 117194 | +** is added to the output to describe the table scan strategy in pLevel. |
| 117195 | +** |
| 117196 | +** If an OP_Explain opcode is added to the VM, its address is returned. |
| 117197 | +** Otherwise, if no OP_Explain is coded, zero is returned. |
| 116353 | 117198 | */ |
| 116354 | | -static void explainOneScan( |
| 117199 | +static int explainOneScan( |
| 116355 | 117200 | Parse *pParse, /* Parse context */ |
| 116356 | 117201 | SrcList *pTabList, /* Table list this loop refers to */ |
| 116357 | 117202 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 116358 | 117203 | int iLevel, /* Value for "level" column of output */ |
| 116359 | 117204 | int iFrom, /* Value for "from" column of output */ |
| 116360 | 117205 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 116361 | 117206 | ){ |
| 116362 | | -#ifndef SQLITE_DEBUG |
| 117207 | + int ret = 0; |
| 117208 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 116363 | 117209 | if( pParse->explain==2 ) |
| 116364 | 117210 | #endif |
| 116365 | 117211 | { |
| 116366 | 117212 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116367 | 117213 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| | @@ -116374,11 +117220,11 @@ |
| 116374 | 117220 | StrAccum str; /* EQP output string */ |
| 116375 | 117221 | char zBuf[100]; /* Initial space for EQP output string */ |
| 116376 | 117222 | |
| 116377 | 117223 | pLoop = pLevel->pWLoop; |
| 116378 | 117224 | flags = pLoop->wsFlags; |
| 116379 | | - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 117225 | + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
| 116380 | 117226 | |
| 116381 | 117227 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116382 | 117228 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116383 | 117229 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116384 | 117230 | |
| | @@ -116403,10 +117249,12 @@ |
| 116403 | 117249 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116404 | 117250 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116405 | 117251 | if( isSearch ){ |
| 116406 | 117252 | zFmt = "PRIMARY KEY"; |
| 116407 | 117253 | } |
| 117254 | + }else if( flags & WHERE_PARTIALIDX ){ |
| 117255 | + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 116408 | 117256 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116409 | 117257 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 116410 | 117258 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116411 | 117259 | zFmt = "COVERING INDEX %s"; |
| 116412 | 117260 | }else{ |
| | @@ -116444,16 +117292,49 @@ |
| 116444 | 117292 | }else{ |
| 116445 | 117293 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116446 | 117294 | } |
| 116447 | 117295 | #endif |
| 116448 | 117296 | zMsg = sqlite3StrAccumFinish(&str); |
| 116449 | | - sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 117297 | + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 116450 | 117298 | } |
| 117299 | + return ret; |
| 116451 | 117300 | } |
| 116452 | 117301 | #else |
| 116453 | | -# define explainOneScan(u,v,w,x,y,z) |
| 117302 | +# define explainOneScan(u,v,w,x,y,z) 0 |
| 116454 | 117303 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 117304 | + |
| 117305 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 117306 | +/* |
| 117307 | +** Configure the VM passed as the first argument with an |
| 117308 | +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 117309 | +** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 117310 | +** clause that the scan reads data from. |
| 117311 | +** |
| 117312 | +** If argument addrExplain is not 0, it must be the address of an |
| 117313 | +** OP_Explain instruction that describes the same loop. |
| 117314 | +*/ |
| 117315 | +static void addScanStatus( |
| 117316 | + Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 117317 | + SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 117318 | + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 117319 | + int addrExplain /* Address of OP_Explain (or 0) */ |
| 117320 | +){ |
| 117321 | + const char *zObj = 0; |
| 117322 | + WhereLoop *pLoop = pLvl->pWLoop; |
| 117323 | + if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 117324 | + zObj = pLoop->u.btree.pIndex->zName; |
| 117325 | + }else{ |
| 117326 | + zObj = pSrclist->a[pLvl->iFrom].zName; |
| 117327 | + } |
| 117328 | + sqlite3VdbeScanStatus( |
| 117329 | + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 117330 | + ); |
| 117331 | +} |
| 117332 | +#else |
| 117333 | +# define addScanStatus(a, b, c, d) ((void)d) |
| 117334 | +#endif |
| 117335 | + |
| 116455 | 117336 | |
| 116456 | 117337 | |
| 116457 | 117338 | /* |
| 116458 | 117339 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 116459 | 117340 | ** implementation described by pWInfo. |
| | @@ -116751,11 +117632,11 @@ |
| 116751 | 117632 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 116752 | 117633 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 116753 | 117634 | |
| 116754 | 117635 | pIdx = pLoop->u.btree.pIndex; |
| 116755 | 117636 | iIdxCur = pLevel->iIdxCur; |
| 116756 | | - assert( nEq>=pLoop->u.btree.nSkip ); |
| 117637 | + assert( nEq>=pLoop->nSkip ); |
| 116757 | 117638 | |
| 116758 | 117639 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 116759 | 117640 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 116760 | 117641 | ** query, then the caller will only allow the loop to run for |
| 116761 | 117642 | ** a single iteration. This means that the first row returned |
| | @@ -116768,11 +117649,11 @@ |
| 116768 | 117649 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 116769 | 117650 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 116770 | 117651 | && pWInfo->nOBSat>0 |
| 116771 | 117652 | && (pIdx->nKeyCol>nEq) |
| 116772 | 117653 | ){ |
| 116773 | | - assert( pLoop->u.btree.nSkip==0 ); |
| 117654 | + assert( pLoop->nSkip==0 ); |
| 116774 | 117655 | bSeekPastNull = 1; |
| 116775 | 117656 | nExtraReg = 1; |
| 116776 | 117657 | } |
| 116777 | 117658 | |
| 116778 | 117659 | /* Find any inequality constraint terms for the start and end |
| | @@ -117117,13 +117998,15 @@ |
| 117117 | 117998 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 117118 | 117999 | wctrlFlags, iCovCur); |
| 117119 | 118000 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 117120 | 118001 | if( pSubWInfo ){ |
| 117121 | 118002 | WhereLoop *pSubLoop; |
| 117122 | | - explainOneScan( |
| 118003 | + int addrExplain = explainOneScan( |
| 117123 | 118004 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 117124 | 118005 | ); |
| 118006 | + addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 118007 | + |
| 117125 | 118008 | /* This is the sub-WHERE clause body. First skip over |
| 117126 | 118009 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 117127 | 118010 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 117128 | 118011 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 117129 | 118012 | */ |
| | @@ -117249,10 +118132,14 @@ |
| 117249 | 118132 | VdbeCoverageIf(v, bRev==0); |
| 117250 | 118133 | VdbeCoverageIf(v, bRev!=0); |
| 117251 | 118134 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 117252 | 118135 | } |
| 117253 | 118136 | } |
| 118137 | + |
| 118138 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 118139 | + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 118140 | +#endif |
| 117254 | 118141 | |
| 117255 | 118142 | /* Insert code to test every subexpression that can be completely |
| 117256 | 118143 | ** computed using the current set of tables. |
| 117257 | 118144 | */ |
| 117258 | 118145 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| | @@ -117389,11 +118276,11 @@ |
| 117389 | 118276 | } |
| 117390 | 118277 | sqlite3DebugPrintf(" %-19s", z); |
| 117391 | 118278 | sqlite3_free(z); |
| 117392 | 118279 | } |
| 117393 | 118280 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 117394 | | - sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); |
| 118281 | + sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
| 117395 | 118282 | }else{ |
| 117396 | 118283 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117397 | 118284 | } |
| 117398 | 118285 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117399 | 118286 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| | @@ -117500,34 +118387,41 @@ |
| 117500 | 118387 | sqlite3DbFree(db, pWInfo); |
| 117501 | 118388 | } |
| 117502 | 118389 | } |
| 117503 | 118390 | |
| 117504 | 118391 | /* |
| 117505 | | -** Return TRUE if both of the following are true: |
| 118392 | +** Return TRUE if all of the following are true: |
| 117506 | 118393 | ** |
| 117507 | 118394 | ** (1) X has the same or lower cost that Y |
| 117508 | 118395 | ** (2) X is a proper subset of Y |
| 118396 | +** (3) X skips at least as many columns as Y |
| 117509 | 118397 | ** |
| 117510 | 118398 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 117511 | 118399 | ** than Y and that every WHERE clause term used by X is also used |
| 117512 | 118400 | ** by Y. |
| 117513 | 118401 | ** |
| 117514 | 118402 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 117515 | 118403 | ** to have a lower cost. This routine returns TRUE when that cost |
| 117516 | | -** relationship is inverted and needs to be adjusted. |
| 118404 | +** relationship is inverted and needs to be adjusted. The third rule |
| 118405 | +** was added because if X uses skip-scan less than Y it still might |
| 118406 | +** deserve a lower cost even if it is a proper subset of Y. |
| 117517 | 118407 | */ |
| 117518 | 118408 | static int whereLoopCheaperProperSubset( |
| 117519 | 118409 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 117520 | 118410 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 117521 | 118411 | ){ |
| 117522 | 118412 | int i, j; |
| 117523 | | - if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */ |
| 118413 | + if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 118414 | + return 0; /* X is not a subset of Y */ |
| 118415 | + } |
| 118416 | + if( pY->nSkip > pX->nSkip ) return 0; |
| 117524 | 118417 | if( pX->rRun >= pY->rRun ){ |
| 117525 | 118418 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 117526 | 118419 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 117527 | 118420 | } |
| 117528 | 118421 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 118422 | + if( pX->aLTerm[i]==0 ) continue; |
| 117529 | 118423 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 117530 | 118424 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 117531 | 118425 | } |
| 117532 | 118426 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 117533 | 118427 | } |
| | @@ -117545,37 +118439,28 @@ |
| 117545 | 118439 | ** is a proper subset. |
| 117546 | 118440 | ** |
| 117547 | 118441 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 117548 | 118442 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 117549 | 118443 | ** 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 | 118444 | */ |
| 117562 | 118445 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 117563 | 118446 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 117564 | | - if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return; |
| 117565 | 118447 | for(; p; p=p->pNextLoop){ |
| 117566 | 118448 | if( p->iTab!=pTemplate->iTab ) continue; |
| 117567 | 118449 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 117568 | | - if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue; |
| 117569 | 118450 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 117570 | 118451 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 117571 | | - ** subset p */ |
| 118452 | + ** subset p. */ |
| 118453 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118454 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
| 117572 | 118455 | pTemplate->rRun = p->rRun; |
| 117573 | 118456 | pTemplate->nOut = p->nOut - 1; |
| 117574 | 118457 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 117575 | 118458 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 117576 | 118459 | ** pTemplate is a proper subset of p */ |
| 118460 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118461 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
| 117577 | 118462 | pTemplate->rRun = p->rRun; |
| 117578 | 118463 | pTemplate->nOut = p->nOut + 1; |
| 117579 | 118464 | } |
| 117580 | 118465 | } |
| 117581 | 118466 | } |
| | @@ -117857,11 +118742,11 @@ |
| 117857 | 118742 | int opMask; /* Valid operators for constraints */ |
| 117858 | 118743 | WhereScan scan; /* Iterator for WHERE terms */ |
| 117859 | 118744 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 117860 | 118745 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 117861 | 118746 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 117862 | | - u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ |
| 118747 | + u16 saved_nSkip; /* Original value of pNew->nSkip */ |
| 117863 | 118748 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 117864 | 118749 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 117865 | 118750 | int iCol; /* Index of the column in the table */ |
| 117866 | 118751 | int rc = SQLITE_OK; /* Return code */ |
| 117867 | 118752 | LogEst rSize; /* Number of rows in the table */ |
| | @@ -117886,56 +118771,18 @@ |
| 117886 | 118771 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 117887 | 118772 | |
| 117888 | 118773 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 117889 | 118774 | opMask, pProbe); |
| 117890 | 118775 | saved_nEq = pNew->u.btree.nEq; |
| 117891 | | - saved_nSkip = pNew->u.btree.nSkip; |
| 118776 | + saved_nSkip = pNew->nSkip; |
| 117892 | 118777 | saved_nLTerm = pNew->nLTerm; |
| 117893 | 118778 | saved_wsFlags = pNew->wsFlags; |
| 117894 | 118779 | saved_prereq = pNew->prereq; |
| 117895 | 118780 | saved_nOut = pNew->nOut; |
| 117896 | 118781 | pNew->rSetup = 0; |
| 117897 | 118782 | rSize = pProbe->aiRowLogEst[0]; |
| 117898 | 118783 | 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 | 118784 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 117938 | 118785 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 117939 | 118786 | LogEst rCostIdx; |
| 117940 | 118787 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 117941 | 118788 | int nIn = 0; |
| | @@ -118026,11 +118873,10 @@ |
| 118026 | 118873 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 118027 | 118874 | tRowcnt nOut = 0; |
| 118028 | 118875 | if( nInMul==0 |
| 118029 | 118876 | && pProbe->nSample |
| 118030 | 118877 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 118031 | | - && OptimizationEnabled(db, SQLITE_Stat3) |
| 118032 | 118878 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 118033 | 118879 | ){ |
| 118034 | 118880 | Expr *pExpr = pTerm->pExpr; |
| 118035 | 118881 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 118036 | 118882 | testcase( eOp & WO_EQ ); |
| | @@ -118094,14 +118940,48 @@ |
| 118094 | 118940 | pBuilder->nRecValid = nRecValid; |
| 118095 | 118941 | #endif |
| 118096 | 118942 | } |
| 118097 | 118943 | pNew->prereq = saved_prereq; |
| 118098 | 118944 | pNew->u.btree.nEq = saved_nEq; |
| 118099 | | - pNew->u.btree.nSkip = saved_nSkip; |
| 118945 | + pNew->nSkip = saved_nSkip; |
| 118100 | 118946 | pNew->wsFlags = saved_wsFlags; |
| 118101 | 118947 | pNew->nOut = saved_nOut; |
| 118102 | 118948 | pNew->nLTerm = saved_nLTerm; |
| 118949 | + |
| 118950 | + /* Consider using a skip-scan if there are no WHERE clause constraints |
| 118951 | + ** available for the left-most terms of the index, and if the average |
| 118952 | + ** number of repeats in the left-most terms is at least 18. |
| 118953 | + ** |
| 118954 | + ** The magic number 18 is selected on the basis that scanning 17 rows |
| 118955 | + ** is almost always quicker than an index seek (even though if the index |
| 118956 | + ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 118957 | + ** the code). And, even if it is not, it should not be too much slower. |
| 118958 | + ** On the other hand, the extra seeks could end up being significantly |
| 118959 | + ** more expensive. */ |
| 118960 | + assert( 42==sqlite3LogEst(18) ); |
| 118961 | + if( saved_nEq==saved_nSkip |
| 118962 | + && saved_nEq+1<pProbe->nKeyCol |
| 118963 | + && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 118964 | + && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 118965 | + ){ |
| 118966 | + LogEst nIter; |
| 118967 | + pNew->u.btree.nEq++; |
| 118968 | + pNew->nSkip++; |
| 118969 | + pNew->aLTerm[pNew->nLTerm++] = 0; |
| 118970 | + pNew->wsFlags |= WHERE_SKIPSCAN; |
| 118971 | + nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 118972 | + pNew->nOut -= nIter; |
| 118973 | + /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 118974 | + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 118975 | + nIter += 5; |
| 118976 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 118977 | + pNew->nOut = saved_nOut; |
| 118978 | + pNew->u.btree.nEq = saved_nEq; |
| 118979 | + pNew->nSkip = saved_nSkip; |
| 118980 | + pNew->wsFlags = saved_wsFlags; |
| 118981 | + } |
| 118982 | + |
| 118103 | 118983 | return rc; |
| 118104 | 118984 | } |
| 118105 | 118985 | |
| 118106 | 118986 | /* |
| 118107 | 118987 | ** Return True if it is possible that pIndex might be useful in |
| | @@ -118276,11 +119156,11 @@ |
| 118276 | 119156 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 118277 | 119157 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 118278 | 119158 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 118279 | 119159 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 118280 | 119160 | pNew->u.btree.nEq = 1; |
| 118281 | | - pNew->u.btree.nSkip = 0; |
| 119161 | + pNew->nSkip = 0; |
| 118282 | 119162 | pNew->u.btree.pIndex = 0; |
| 118283 | 119163 | pNew->nLTerm = 1; |
| 118284 | 119164 | pNew->aLTerm[0] = pTerm; |
| 118285 | 119165 | /* TUNING: One-time cost for computing the automatic index is |
| 118286 | 119166 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| | @@ -118317,11 +119197,11 @@ |
| 118317 | 119197 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 118318 | 119198 | continue; /* Partial index inappropriate for this query */ |
| 118319 | 119199 | } |
| 118320 | 119200 | rSize = pProbe->aiRowLogEst[0]; |
| 118321 | 119201 | pNew->u.btree.nEq = 0; |
| 118322 | | - pNew->u.btree.nSkip = 0; |
| 119202 | + pNew->nSkip = 0; |
| 118323 | 119203 | pNew->nLTerm = 0; |
| 118324 | 119204 | pNew->iSortIdx = 0; |
| 118325 | 119205 | pNew->rSetup = 0; |
| 118326 | 119206 | pNew->prereq = mExtra; |
| 118327 | 119207 | pNew->nOut = rSize; |
| | @@ -118867,11 +119747,11 @@ |
| 118867 | 119747 | for(j=0; j<nColumn; j++){ |
| 118868 | 119748 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 118869 | 119749 | |
| 118870 | 119750 | /* Skip over == and IS NULL terms */ |
| 118871 | 119751 | if( j<pLoop->u.btree.nEq |
| 118872 | | - && pLoop->u.btree.nSkip==0 |
| 119752 | + && pLoop->nSkip==0 |
| 118873 | 119753 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 118874 | 119754 | ){ |
| 118875 | 119755 | if( i & WO_ISNULL ){ |
| 118876 | 119756 | testcase( isOrderDistinct ); |
| 118877 | 119757 | isOrderDistinct = 0; |
| | @@ -119321,11 +120201,11 @@ |
| 119321 | 120201 | } |
| 119322 | 120202 | } |
| 119323 | 120203 | } |
| 119324 | 120204 | |
| 119325 | 120205 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 119326 | | - if( sqlite3WhereTrace>=2 ){ |
| 120206 | + if( sqlite3WhereTrace & 0x02 ){ |
| 119327 | 120207 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 119328 | 120208 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 119329 | 120209 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 119330 | 120210 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 119331 | 120211 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| | @@ -119440,11 +120320,11 @@ |
| 119440 | 120320 | if( pItem->zIndex ) return 0; |
| 119441 | 120321 | iCur = pItem->iCursor; |
| 119442 | 120322 | pWC = &pWInfo->sWC; |
| 119443 | 120323 | pLoop = pBuilder->pNew; |
| 119444 | 120324 | pLoop->wsFlags = 0; |
| 119445 | | - pLoop->u.btree.nSkip = 0; |
| 120325 | + pLoop->nSkip = 0; |
| 119446 | 120326 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 119447 | 120327 | if( pTerm ){ |
| 119448 | 120328 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 119449 | 120329 | pLoop->aLTerm[0] = pTerm; |
| 119450 | 120330 | pLoop->nLTerm = 1; |
| | @@ -119452,11 +120332,10 @@ |
| 119452 | 120332 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 119453 | 120333 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 119454 | 120334 | }else{ |
| 119455 | 120335 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 119456 | 120336 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 119457 | | - assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 119458 | 120337 | if( !IsUniqueIndex(pIdx) |
| 119459 | 120338 | || pIdx->pPartIdxWhere!=0 |
| 119460 | 120339 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 119461 | 120340 | ) continue; |
| 119462 | 120341 | for(j=0; j<pIdx->nKeyCol; j++){ |
| | @@ -119961,22 +120840,30 @@ |
| 119961 | 120840 | ** loop below generates code for a single nested loop of the VM |
| 119962 | 120841 | ** program. |
| 119963 | 120842 | */ |
| 119964 | 120843 | notReady = ~(Bitmask)0; |
| 119965 | 120844 | for(ii=0; ii<nTabList; ii++){ |
| 120845 | + int addrExplain; |
| 120846 | + int wsFlags; |
| 119966 | 120847 | pLevel = &pWInfo->a[ii]; |
| 120848 | + wsFlags = pLevel->pWLoop->wsFlags; |
| 119967 | 120849 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 119968 | 120850 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 119969 | 120851 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 119970 | 120852 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 119971 | 120853 | if( db->mallocFailed ) goto whereBeginError; |
| 119972 | 120854 | } |
| 119973 | 120855 | #endif |
| 119974 | | - explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 120856 | + addrExplain = explainOneScan( |
| 120857 | + pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 120858 | + ); |
| 119975 | 120859 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 119976 | 120860 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 119977 | 120861 | pWInfo->iContinue = pLevel->addrCont; |
| 120862 | + if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
| 120863 | + addScanStatus(v, pTabList, pLevel, addrExplain); |
| 120864 | + } |
| 119978 | 120865 | } |
| 119979 | 120866 | |
| 119980 | 120867 | /* Done. */ |
| 119981 | 120868 | VdbeModuleComment((v, "Begin WHERE-core")); |
| 119982 | 120869 | return pWInfo; |
| | @@ -124640,10 +125527,17 @@ |
| 124640 | 125527 | ** is look for a semicolon that is not part of an string or comment. |
| 124641 | 125528 | */ |
| 124642 | 125529 | SQLITE_API int sqlite3_complete(const char *zSql){ |
| 124643 | 125530 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 124644 | 125531 | u8 token; /* Value of the next token */ |
| 125532 | + |
| 125533 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 125534 | + if( zSql==0 ){ |
| 125535 | + (void)SQLITE_MISUSE_BKPT; |
| 125536 | + return 0; |
| 125537 | + } |
| 125538 | +#endif |
| 124645 | 125539 | |
| 124646 | 125540 | #ifndef SQLITE_OMIT_TRIGGER |
| 124647 | 125541 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 124648 | 125542 | ** statement. This is the normal case. |
| 124649 | 125543 | */ |
| | @@ -125238,74 +126132,106 @@ |
| 125238 | 126132 | |
| 125239 | 126133 | va_start(ap, op); |
| 125240 | 126134 | switch( op ){ |
| 125241 | 126135 | |
| 125242 | 126136 | /* Mutex configuration options are only available in a threadsafe |
| 125243 | | - ** compile. |
| 126137 | + ** compile. |
| 125244 | 126138 | */ |
| 125245 | | -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 |
| 126139 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ |
| 125246 | 126140 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 125247 | 126141 | /* Disable all mutexing */ |
| 125248 | 126142 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 125249 | 126143 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125250 | 126144 | break; |
| 125251 | 126145 | } |
| 126146 | +#endif |
| 126147 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ |
| 125252 | 126148 | case SQLITE_CONFIG_MULTITHREAD: { |
| 125253 | 126149 | /* Disable mutexing of database connections */ |
| 125254 | 126150 | /* Enable mutexing of core data structures */ |
| 125255 | 126151 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125256 | 126152 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125257 | 126153 | break; |
| 125258 | 126154 | } |
| 126155 | +#endif |
| 126156 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ |
| 125259 | 126157 | case SQLITE_CONFIG_SERIALIZED: { |
| 125260 | 126158 | /* Enable all mutexing */ |
| 125261 | 126159 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125262 | 126160 | sqlite3GlobalConfig.bFullMutex = 1; |
| 125263 | 126161 | break; |
| 125264 | 126162 | } |
| 126163 | +#endif |
| 126164 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ |
| 125265 | 126165 | case SQLITE_CONFIG_MUTEX: { |
| 125266 | 126166 | /* Specify an alternative mutex implementation */ |
| 125267 | 126167 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 125268 | 126168 | break; |
| 125269 | 126169 | } |
| 126170 | +#endif |
| 126171 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ |
| 125270 | 126172 | case SQLITE_CONFIG_GETMUTEX: { |
| 125271 | 126173 | /* Retrieve the current mutex implementation */ |
| 125272 | 126174 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 125273 | 126175 | break; |
| 125274 | 126176 | } |
| 125275 | 126177 | #endif |
| 125276 | 126178 | |
| 125277 | | - |
| 125278 | 126179 | case SQLITE_CONFIG_MALLOC: { |
| 125279 | | - /* Specify an alternative malloc implementation */ |
| 126180 | + /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a |
| 126181 | + ** single argument which is a pointer to an instance of the |
| 126182 | + ** sqlite3_mem_methods structure. The argument specifies alternative |
| 126183 | + ** low-level memory allocation routines to be used in place of the memory |
| 126184 | + ** allocation routines built into SQLite. */ |
| 125280 | 126185 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 125281 | 126186 | break; |
| 125282 | 126187 | } |
| 125283 | 126188 | case SQLITE_CONFIG_GETMALLOC: { |
| 125284 | | - /* Retrieve the current malloc() implementation */ |
| 126189 | + /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a |
| 126190 | + ** single argument which is a pointer to an instance of the |
| 126191 | + ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is |
| 126192 | + ** filled with the currently defined memory allocation routines. */ |
| 125285 | 126193 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 125286 | 126194 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 125287 | 126195 | break; |
| 125288 | 126196 | } |
| 125289 | 126197 | case SQLITE_CONFIG_MEMSTATUS: { |
| 125290 | | - /* Enable or disable the malloc status collection */ |
| 126198 | + /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes |
| 126199 | + ** single argument of type int, interpreted as a boolean, which enables |
| 126200 | + ** or disables the collection of memory allocation statistics. */ |
| 125291 | 126201 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 125292 | 126202 | break; |
| 125293 | 126203 | } |
| 125294 | 126204 | case SQLITE_CONFIG_SCRATCH: { |
| 125295 | | - /* Designate a buffer for scratch memory space */ |
| 126205 | + /* EVIDENCE-OF: R-08404-60887 There are three arguments to |
| 126206 | + ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from |
| 126207 | + ** which the scratch allocations will be drawn, the size of each scratch |
| 126208 | + ** allocation (sz), and the maximum number of scratch allocations (N). */ |
| 125296 | 126209 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 125297 | 126210 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 125298 | 126211 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 125299 | 126212 | break; |
| 125300 | 126213 | } |
| 125301 | 126214 | case SQLITE_CONFIG_PAGECACHE: { |
| 125302 | | - /* Designate a buffer for page cache memory space */ |
| 126215 | + /* EVIDENCE-OF: R-31408-40510 There are three arguments to |
| 126216 | + ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size |
| 126217 | + ** of each page buffer (sz), and the number of pages (N). */ |
| 125303 | 126218 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 125304 | 126219 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 125305 | 126220 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 125306 | 126221 | break; |
| 126222 | + } |
| 126223 | + case SQLITE_CONFIG_PCACHE_HDRSZ: { |
| 126224 | + /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes |
| 126225 | + ** a single parameter which is a pointer to an integer and writes into |
| 126226 | + ** that integer the number of extra bytes per page required for each page |
| 126227 | + ** in SQLITE_CONFIG_PAGECACHE. */ |
| 126228 | + *va_arg(ap, int*) = |
| 126229 | + sqlite3HeaderSizeBtree() + |
| 126230 | + sqlite3HeaderSizePcache() + |
| 126231 | + sqlite3HeaderSizePcache1(); |
| 126232 | + break; |
| 125307 | 126233 | } |
| 125308 | 126234 | |
| 125309 | 126235 | case SQLITE_CONFIG_PCACHE: { |
| 125310 | 126236 | /* no-op */ |
| 125311 | 126237 | break; |
| | @@ -125315,25 +126241,37 @@ |
| 125315 | 126241 | rc = SQLITE_ERROR; |
| 125316 | 126242 | break; |
| 125317 | 126243 | } |
| 125318 | 126244 | |
| 125319 | 126245 | case SQLITE_CONFIG_PCACHE2: { |
| 125320 | | - /* Specify an alternative page cache implementation */ |
| 126246 | + /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a |
| 126247 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126248 | + ** object. This object specifies the interface to a custom page cache |
| 126249 | + ** implementation. */ |
| 125321 | 126250 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 125322 | 126251 | break; |
| 125323 | 126252 | } |
| 125324 | 126253 | case SQLITE_CONFIG_GETPCACHE2: { |
| 126254 | + /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a |
| 126255 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126256 | + ** object. SQLite copies of the current page cache implementation into |
| 126257 | + ** that object. */ |
| 125325 | 126258 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 125326 | 126259 | sqlite3PCacheSetDefault(); |
| 125327 | 126260 | } |
| 125328 | 126261 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 125329 | 126262 | break; |
| 125330 | 126263 | } |
| 125331 | 126264 | |
| 126265 | +/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only |
| 126266 | +** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or |
| 126267 | +** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ |
| 125332 | 126268 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 125333 | 126269 | case SQLITE_CONFIG_HEAP: { |
| 125334 | | - /* Designate a buffer for heap memory space */ |
| 126270 | + /* EVIDENCE-OF: R-19854-42126 There are three arguments to |
| 126271 | + ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the |
| 126272 | + ** number of bytes in the memory buffer, and the minimum allocation size. */ |
| 125335 | 126273 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 125336 | 126274 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125337 | 126275 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 125338 | 126276 | |
| 125339 | 126277 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| | @@ -125342,21 +126280,23 @@ |
| 125342 | 126280 | /* cap min request size at 2^12 */ |
| 125343 | 126281 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 125344 | 126282 | } |
| 125345 | 126283 | |
| 125346 | 126284 | 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. |
| 126285 | + /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) |
| 126286 | + ** is NULL, then SQLite reverts to using its default memory allocator |
| 126287 | + ** (the system malloc() implementation), undoing any prior invocation of |
| 126288 | + ** SQLITE_CONFIG_MALLOC. |
| 126289 | + ** |
| 126290 | + ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to |
| 126291 | + ** revert to its default implementation when sqlite3_initialize() is run |
| 125351 | 126292 | */ |
| 125352 | 126293 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 125353 | 126294 | }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 | | - */ |
| 126295 | + /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the |
| 126296 | + ** alternative memory allocator is engaged to handle all of SQLites |
| 126297 | + ** memory allocation needs. */ |
| 125358 | 126298 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 125359 | 126299 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 125360 | 126300 | #endif |
| 125361 | 126301 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 125362 | 126302 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| | @@ -125391,15 +126331,23 @@ |
| 125391 | 126331 | ** can be changed at start-time using the |
| 125392 | 126332 | ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125393 | 126333 | ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125394 | 126334 | */ |
| 125395 | 126335 | case SQLITE_CONFIG_URI: { |
| 126336 | + /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single |
| 126337 | + ** argument of type int. If non-zero, then URI handling is globally |
| 126338 | + ** enabled. If the parameter is zero, then URI handling is globally |
| 126339 | + ** disabled. */ |
| 125396 | 126340 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125397 | 126341 | break; |
| 125398 | 126342 | } |
| 125399 | 126343 | |
| 125400 | 126344 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 126345 | + /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 126346 | + ** option takes a single integer argument which is interpreted as a |
| 126347 | + ** boolean in order to enable or disable the use of covering indices for |
| 126348 | + ** full table scans in the query optimizer. */ |
| 125401 | 126349 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 125402 | 126350 | break; |
| 125403 | 126351 | } |
| 125404 | 126352 | |
| 125405 | 126353 | #ifdef SQLITE_ENABLE_SQLLOG |
| | @@ -125410,24 +126358,37 @@ |
| 125410 | 126358 | break; |
| 125411 | 126359 | } |
| 125412 | 126360 | #endif |
| 125413 | 126361 | |
| 125414 | 126362 | case SQLITE_CONFIG_MMAP_SIZE: { |
| 126363 | + /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit |
| 126364 | + ** integer (sqlite3_int64) values that are the default mmap size limit |
| 126365 | + ** (the default setting for PRAGMA mmap_size) and the maximum allowed |
| 126366 | + ** mmap size limit. */ |
| 125415 | 126367 | sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 125416 | 126368 | 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; |
| 126369 | + /* EVIDENCE-OF: R-53367-43190 If either argument to this option is |
| 126370 | + ** negative, then that argument is changed to its compile-time default. |
| 126371 | + ** |
| 126372 | + ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be |
| 126373 | + ** silently truncated if necessary so that it does not exceed the |
| 126374 | + ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE |
| 126375 | + ** compile-time option. |
| 126376 | + */ |
| 126377 | + if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 125421 | 126378 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 125422 | 126379 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 126380 | + sqlite3GlobalConfig.mxMmap = mxMmap; |
| 125423 | 126381 | sqlite3GlobalConfig.szMmap = szMmap; |
| 125424 | 126382 | break; |
| 125425 | 126383 | } |
| 125426 | 126384 | |
| 125427 | | -#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) |
| 126385 | +#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ |
| 125428 | 126386 | case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 126387 | + /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit |
| 126388 | + ** unsigned integer value that specifies the maximum size of the created |
| 126389 | + ** heap. */ |
| 125429 | 126390 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125430 | 126391 | break; |
| 125431 | 126392 | } |
| 125432 | 126393 | #endif |
| 125433 | 126394 | |
| | @@ -125507,19 +126468,29 @@ |
| 125507 | 126468 | |
| 125508 | 126469 | /* |
| 125509 | 126470 | ** Return the mutex associated with a database connection. |
| 125510 | 126471 | */ |
| 125511 | 126472 | SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 126473 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126474 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126475 | + (void)SQLITE_MISUSE_BKPT; |
| 126476 | + return 0; |
| 126477 | + } |
| 126478 | +#endif |
| 125512 | 126479 | return db->mutex; |
| 125513 | 126480 | } |
| 125514 | 126481 | |
| 125515 | 126482 | /* |
| 125516 | 126483 | ** Free up as much memory as we can from the given database |
| 125517 | 126484 | ** connection. |
| 125518 | 126485 | */ |
| 125519 | 126486 | SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 125520 | 126487 | int i; |
| 126488 | + |
| 126489 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126490 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 126491 | +#endif |
| 125521 | 126492 | sqlite3_mutex_enter(db->mutex); |
| 125522 | 126493 | sqlite3BtreeEnterAll(db); |
| 125523 | 126494 | for(i=0; i<db->nDb; i++){ |
| 125524 | 126495 | Btree *pBt = db->aDb[i].pBt; |
| 125525 | 126496 | if( pBt ){ |
| | @@ -125646,24 +126617,42 @@ |
| 125646 | 126617 | |
| 125647 | 126618 | /* |
| 125648 | 126619 | ** Return the ROWID of the most recent insert |
| 125649 | 126620 | */ |
| 125650 | 126621 | SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 126622 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126623 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126624 | + (void)SQLITE_MISUSE_BKPT; |
| 126625 | + return 0; |
| 126626 | + } |
| 126627 | +#endif |
| 125651 | 126628 | return db->lastRowid; |
| 125652 | 126629 | } |
| 125653 | 126630 | |
| 125654 | 126631 | /* |
| 125655 | 126632 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 125656 | 126633 | */ |
| 125657 | 126634 | SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 126635 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126636 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126637 | + (void)SQLITE_MISUSE_BKPT; |
| 126638 | + return 0; |
| 126639 | + } |
| 126640 | +#endif |
| 125658 | 126641 | return db->nChange; |
| 125659 | 126642 | } |
| 125660 | 126643 | |
| 125661 | 126644 | /* |
| 125662 | 126645 | ** Return the number of changes since the database handle was opened. |
| 125663 | 126646 | */ |
| 125664 | 126647 | SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 126648 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126649 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126650 | + (void)SQLITE_MISUSE_BKPT; |
| 126651 | + return 0; |
| 126652 | + } |
| 126653 | +#endif |
| 125665 | 126654 | return db->nTotalChange; |
| 125666 | 126655 | } |
| 125667 | 126656 | |
| 125668 | 126657 | /* |
| 125669 | 126658 | ** Close all open savepoints. This function only manipulates fields of the |
| | @@ -125925,17 +126914,19 @@ |
| 125925 | 126914 | sqlite3_free(db); |
| 125926 | 126915 | } |
| 125927 | 126916 | |
| 125928 | 126917 | /* |
| 125929 | 126918 | ** Rollback all database files. If tripCode is not SQLITE_OK, then |
| 125930 | | -** any open cursors are invalidated ("tripped" - as in "tripping a circuit |
| 126919 | +** any write cursors are invalidated ("tripped" - as in "tripping a circuit |
| 125931 | 126920 | ** breaker") and made to return tripCode if there are any further |
| 125932 | | -** attempts to use that cursor. |
| 126921 | +** attempts to use that cursor. Read cursors remain open and valid |
| 126922 | +** but are "saved" in case the table pages are moved around. |
| 125933 | 126923 | */ |
| 125934 | 126924 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 125935 | 126925 | int i; |
| 125936 | 126926 | int inTrans = 0; |
| 126927 | + int schemaChange; |
| 125937 | 126928 | assert( sqlite3_mutex_held(db->mutex) ); |
| 125938 | 126929 | sqlite3BeginBenignMalloc(); |
| 125939 | 126930 | |
| 125940 | 126931 | /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 125941 | 126932 | ** This is important in case the transaction being rolled back has |
| | @@ -125942,18 +126933,19 @@ |
| 125942 | 126933 | ** modified the database schema. If the b-tree mutexes are not taken |
| 125943 | 126934 | ** here, then another shared-cache connection might sneak in between |
| 125944 | 126935 | ** the database rollback and schema reset, which can cause false |
| 125945 | 126936 | ** corruption reports in some cases. */ |
| 125946 | 126937 | sqlite3BtreeEnterAll(db); |
| 126938 | + schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; |
| 125947 | 126939 | |
| 125948 | 126940 | for(i=0; i<db->nDb; i++){ |
| 125949 | 126941 | Btree *p = db->aDb[i].pBt; |
| 125950 | 126942 | if( p ){ |
| 125951 | 126943 | if( sqlite3BtreeIsInTrans(p) ){ |
| 125952 | 126944 | inTrans = 1; |
| 125953 | 126945 | } |
| 125954 | | - sqlite3BtreeRollback(p, tripCode); |
| 126946 | + sqlite3BtreeRollback(p, tripCode, !schemaChange); |
| 125955 | 126947 | } |
| 125956 | 126948 | } |
| 125957 | 126949 | sqlite3VtabRollback(db); |
| 125958 | 126950 | sqlite3EndBenignMalloc(); |
| 125959 | 126951 | |
| | @@ -126205,10 +127197,13 @@ |
| 126205 | 127197 | SQLITE_API int sqlite3_busy_handler( |
| 126206 | 127198 | sqlite3 *db, |
| 126207 | 127199 | int (*xBusy)(void*,int), |
| 126208 | 127200 | void *pArg |
| 126209 | 127201 | ){ |
| 127202 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127203 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE; |
| 127204 | +#endif |
| 126210 | 127205 | sqlite3_mutex_enter(db->mutex); |
| 126211 | 127206 | db->busyHandler.xFunc = xBusy; |
| 126212 | 127207 | db->busyHandler.pArg = pArg; |
| 126213 | 127208 | db->busyHandler.nBusy = 0; |
| 126214 | 127209 | db->busyTimeout = 0; |
| | @@ -126226,10 +127221,16 @@ |
| 126226 | 127221 | sqlite3 *db, |
| 126227 | 127222 | int nOps, |
| 126228 | 127223 | int (*xProgress)(void*), |
| 126229 | 127224 | void *pArg |
| 126230 | 127225 | ){ |
| 127226 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127227 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127228 | + (void)SQLITE_MISUSE_BKPT; |
| 127229 | + return; |
| 127230 | + } |
| 127231 | +#endif |
| 126231 | 127232 | sqlite3_mutex_enter(db->mutex); |
| 126232 | 127233 | if( nOps>0 ){ |
| 126233 | 127234 | db->xProgress = xProgress; |
| 126234 | 127235 | db->nProgressOps = (unsigned)nOps; |
| 126235 | 127236 | db->pProgressArg = pArg; |
| | @@ -126246,10 +127247,13 @@ |
| 126246 | 127247 | /* |
| 126247 | 127248 | ** This routine installs a default busy handler that waits for the |
| 126248 | 127249 | ** specified number of milliseconds before returning 0. |
| 126249 | 127250 | */ |
| 126250 | 127251 | SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 127252 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127253 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127254 | +#endif |
| 126251 | 127255 | if( ms>0 ){ |
| 126252 | 127256 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 126253 | 127257 | db->busyTimeout = ms; |
| 126254 | 127258 | }else{ |
| 126255 | 127259 | sqlite3_busy_handler(db, 0, 0); |
| | @@ -126259,10 +127263,16 @@ |
| 126259 | 127263 | |
| 126260 | 127264 | /* |
| 126261 | 127265 | ** Cause any pending operation to stop at its earliest opportunity. |
| 126262 | 127266 | */ |
| 126263 | 127267 | SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 127268 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127269 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127270 | + (void)SQLITE_MISUSE_BKPT; |
| 127271 | + return; |
| 127272 | + } |
| 127273 | +#endif |
| 126264 | 127274 | db->u1.isInterrupted = 1; |
| 126265 | 127275 | } |
| 126266 | 127276 | |
| 126267 | 127277 | |
| 126268 | 127278 | /* |
| | @@ -126396,10 +127406,16 @@ |
| 126396 | 127406 | void (*xFinal)(sqlite3_context*), |
| 126397 | 127407 | void (*xDestroy)(void *) |
| 126398 | 127408 | ){ |
| 126399 | 127409 | int rc = SQLITE_ERROR; |
| 126400 | 127410 | FuncDestructor *pArg = 0; |
| 127411 | + |
| 127412 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127413 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127414 | + return SQLITE_MISUSE_BKPT; |
| 127415 | + } |
| 127416 | +#endif |
| 126401 | 127417 | sqlite3_mutex_enter(db->mutex); |
| 126402 | 127418 | if( xDestroy ){ |
| 126403 | 127419 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 126404 | 127420 | if( !pArg ){ |
| 126405 | 127421 | xDestroy(p); |
| | @@ -126432,10 +127448,14 @@ |
| 126432 | 127448 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 126433 | 127449 | void (*xFinal)(sqlite3_context*) |
| 126434 | 127450 | ){ |
| 126435 | 127451 | int rc; |
| 126436 | 127452 | char *zFunc8; |
| 127453 | + |
| 127454 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127455 | + if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; |
| 127456 | +#endif |
| 126437 | 127457 | sqlite3_mutex_enter(db->mutex); |
| 126438 | 127458 | assert( !db->mallocFailed ); |
| 126439 | 127459 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 126440 | 127460 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 126441 | 127461 | sqlite3DbFree(db, zFunc8); |
| | @@ -126463,10 +127483,16 @@ |
| 126463 | 127483 | const char *zName, |
| 126464 | 127484 | int nArg |
| 126465 | 127485 | ){ |
| 126466 | 127486 | int nName = sqlite3Strlen30(zName); |
| 126467 | 127487 | int rc = SQLITE_OK; |
| 127488 | + |
| 127489 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127490 | + if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ |
| 127491 | + return SQLITE_MISUSE_BKPT; |
| 127492 | + } |
| 127493 | +#endif |
| 126468 | 127494 | sqlite3_mutex_enter(db->mutex); |
| 126469 | 127495 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 126470 | 127496 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 126471 | 127497 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 126472 | 127498 | } |
| | @@ -126484,10 +127510,17 @@ |
| 126484 | 127510 | ** trace is a pointer to a function that is invoked at the start of each |
| 126485 | 127511 | ** SQL statement. |
| 126486 | 127512 | */ |
| 126487 | 127513 | SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
| 126488 | 127514 | void *pOld; |
| 127515 | + |
| 127516 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127517 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127518 | + (void)SQLITE_MISUSE_BKPT; |
| 127519 | + return 0; |
| 127520 | + } |
| 127521 | +#endif |
| 126489 | 127522 | sqlite3_mutex_enter(db->mutex); |
| 126490 | 127523 | pOld = db->pTraceArg; |
| 126491 | 127524 | db->xTrace = xTrace; |
| 126492 | 127525 | db->pTraceArg = pArg; |
| 126493 | 127526 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126505,10 +127538,17 @@ |
| 126505 | 127538 | sqlite3 *db, |
| 126506 | 127539 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 126507 | 127540 | void *pArg |
| 126508 | 127541 | ){ |
| 126509 | 127542 | void *pOld; |
| 127543 | + |
| 127544 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127545 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127546 | + (void)SQLITE_MISUSE_BKPT; |
| 127547 | + return 0; |
| 127548 | + } |
| 127549 | +#endif |
| 126510 | 127550 | sqlite3_mutex_enter(db->mutex); |
| 126511 | 127551 | pOld = db->pProfileArg; |
| 126512 | 127552 | db->xProfile = xProfile; |
| 126513 | 127553 | db->pProfileArg = pArg; |
| 126514 | 127554 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126525,10 +127565,17 @@ |
| 126525 | 127565 | sqlite3 *db, /* Attach the hook to this database */ |
| 126526 | 127566 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 126527 | 127567 | void *pArg /* Argument to the function */ |
| 126528 | 127568 | ){ |
| 126529 | 127569 | void *pOld; |
| 127570 | + |
| 127571 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127572 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127573 | + (void)SQLITE_MISUSE_BKPT; |
| 127574 | + return 0; |
| 127575 | + } |
| 127576 | +#endif |
| 126530 | 127577 | sqlite3_mutex_enter(db->mutex); |
| 126531 | 127578 | pOld = db->pCommitArg; |
| 126532 | 127579 | db->xCommitCallback = xCallback; |
| 126533 | 127580 | db->pCommitArg = pArg; |
| 126534 | 127581 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126543,10 +127590,17 @@ |
| 126543 | 127590 | sqlite3 *db, /* Attach the hook to this database */ |
| 126544 | 127591 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 126545 | 127592 | void *pArg /* Argument to the function */ |
| 126546 | 127593 | ){ |
| 126547 | 127594 | void *pRet; |
| 127595 | + |
| 127596 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127597 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127598 | + (void)SQLITE_MISUSE_BKPT; |
| 127599 | + return 0; |
| 127600 | + } |
| 127601 | +#endif |
| 126548 | 127602 | sqlite3_mutex_enter(db->mutex); |
| 126549 | 127603 | pRet = db->pUpdateArg; |
| 126550 | 127604 | db->xUpdateCallback = xCallback; |
| 126551 | 127605 | db->pUpdateArg = pArg; |
| 126552 | 127606 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126561,10 +127615,17 @@ |
| 126561 | 127615 | sqlite3 *db, /* Attach the hook to this database */ |
| 126562 | 127616 | void (*xCallback)(void*), /* Callback function */ |
| 126563 | 127617 | void *pArg /* Argument to the function */ |
| 126564 | 127618 | ){ |
| 126565 | 127619 | void *pRet; |
| 127620 | + |
| 127621 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127622 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127623 | + (void)SQLITE_MISUSE_BKPT; |
| 127624 | + return 0; |
| 127625 | + } |
| 127626 | +#endif |
| 126566 | 127627 | sqlite3_mutex_enter(db->mutex); |
| 126567 | 127628 | pRet = db->pRollbackArg; |
| 126568 | 127629 | db->xRollbackCallback = xCallback; |
| 126569 | 127630 | db->pRollbackArg = pArg; |
| 126570 | 127631 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126607,10 +127668,13 @@ |
| 126607 | 127668 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 126608 | 127669 | #ifdef SQLITE_OMIT_WAL |
| 126609 | 127670 | UNUSED_PARAMETER(db); |
| 126610 | 127671 | UNUSED_PARAMETER(nFrame); |
| 126611 | 127672 | #else |
| 127673 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127674 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127675 | +#endif |
| 126612 | 127676 | if( nFrame>0 ){ |
| 126613 | 127677 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 126614 | 127678 | }else{ |
| 126615 | 127679 | sqlite3_wal_hook(db, 0, 0); |
| 126616 | 127680 | } |
| | @@ -126627,10 +127691,16 @@ |
| 126627 | 127691 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 126628 | 127692 | void *pArg /* First argument passed to xCallback() */ |
| 126629 | 127693 | ){ |
| 126630 | 127694 | #ifndef SQLITE_OMIT_WAL |
| 126631 | 127695 | void *pRet; |
| 127696 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127697 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127698 | + (void)SQLITE_MISUSE_BKPT; |
| 127699 | + return 0; |
| 127700 | + } |
| 127701 | +#endif |
| 126632 | 127702 | sqlite3_mutex_enter(db->mutex); |
| 126633 | 127703 | pRet = db->pWalArg; |
| 126634 | 127704 | db->xWalCallback = xCallback; |
| 126635 | 127705 | db->pWalArg = pArg; |
| 126636 | 127706 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126653,10 +127723,14 @@ |
| 126653 | 127723 | #ifdef SQLITE_OMIT_WAL |
| 126654 | 127724 | return SQLITE_OK; |
| 126655 | 127725 | #else |
| 126656 | 127726 | int rc; /* Return code */ |
| 126657 | 127727 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 127728 | + |
| 127729 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127730 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127731 | +#endif |
| 126658 | 127732 | |
| 126659 | 127733 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 126660 | 127734 | if( pnLog ) *pnLog = -1; |
| 126661 | 127735 | if( pnCkpt ) *pnCkpt = -1; |
| 126662 | 127736 | |
| | @@ -127050,10 +128124,16 @@ |
| 127050 | 128124 | ** from forming. |
| 127051 | 128125 | */ |
| 127052 | 128126 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 127053 | 128127 | int oldLimit; |
| 127054 | 128128 | |
| 128129 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128130 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 128131 | + (void)SQLITE_MISUSE_BKPT; |
| 128132 | + return -1; |
| 128133 | + } |
| 128134 | +#endif |
| 127055 | 128135 | |
| 127056 | 128136 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 127057 | 128137 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 127058 | 128138 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 127059 | 128139 | ** "_MAX_".) |
| | @@ -127126,11 +128206,12 @@ |
| 127126 | 128206 | char c; |
| 127127 | 128207 | int nUri = sqlite3Strlen30(zUri); |
| 127128 | 128208 | |
| 127129 | 128209 | assert( *pzErrMsg==0 ); |
| 127130 | 128210 | |
| 127131 | | - if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 128211 | + if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ |
| 128212 | + || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ |
| 127132 | 128213 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 127133 | 128214 | ){ |
| 127134 | 128215 | char *zOpt; |
| 127135 | 128216 | int eState; /* Parser state when parsing URI */ |
| 127136 | 128217 | int iIn; /* Input character index */ |
| | @@ -127335,10 +128416,13 @@ |
| 127335 | 128416 | int rc; /* Return code */ |
| 127336 | 128417 | int isThreadsafe; /* True for threadsafe connections */ |
| 127337 | 128418 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 127338 | 128419 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 127339 | 128420 | |
| 128421 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128422 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 128423 | +#endif |
| 127340 | 128424 | *ppDb = 0; |
| 127341 | 128425 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127342 | 128426 | rc = sqlite3_initialize(); |
| 127343 | 128427 | if( rc ) return rc; |
| 127344 | 128428 | #endif |
| | @@ -127624,17 +128708,19 @@ |
| 127624 | 128708 | ){ |
| 127625 | 128709 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 127626 | 128710 | sqlite3_value *pVal; |
| 127627 | 128711 | int rc; |
| 127628 | 128712 | |
| 127629 | | - assert( zFilename ); |
| 127630 | | - assert( ppDb ); |
| 128713 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128714 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 128715 | +#endif |
| 127631 | 128716 | *ppDb = 0; |
| 127632 | 128717 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127633 | 128718 | rc = sqlite3_initialize(); |
| 127634 | 128719 | if( rc ) return rc; |
| 127635 | 128720 | #endif |
| 128721 | + if( zFilename==0 ) zFilename = "\000\000"; |
| 127636 | 128722 | pVal = sqlite3ValueNew(0); |
| 127637 | 128723 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 127638 | 128724 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 127639 | 128725 | if( zFilename8 ){ |
| 127640 | 128726 | rc = openDatabase(zFilename8, ppDb, |
| | @@ -127660,17 +128746,11 @@ |
| 127660 | 128746 | const char *zName, |
| 127661 | 128747 | int enc, |
| 127662 | 128748 | void* pCtx, |
| 127663 | 128749 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127664 | 128750 | ){ |
| 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; |
| 128751 | + return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); |
| 127672 | 128752 | } |
| 127673 | 128753 | |
| 127674 | 128754 | /* |
| 127675 | 128755 | ** Register a new collation sequence with the database handle db. |
| 127676 | 128756 | */ |
| | @@ -127681,10 +128761,14 @@ |
| 127681 | 128761 | void* pCtx, |
| 127682 | 128762 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 127683 | 128763 | void(*xDel)(void*) |
| 127684 | 128764 | ){ |
| 127685 | 128765 | int rc; |
| 128766 | + |
| 128767 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128768 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 128769 | +#endif |
| 127686 | 128770 | sqlite3_mutex_enter(db->mutex); |
| 127687 | 128771 | assert( !db->mallocFailed ); |
| 127688 | 128772 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 127689 | 128773 | rc = sqlite3ApiExit(db, rc); |
| 127690 | 128774 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127702,10 +128786,14 @@ |
| 127702 | 128786 | void* pCtx, |
| 127703 | 128787 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127704 | 128788 | ){ |
| 127705 | 128789 | int rc = SQLITE_OK; |
| 127706 | 128790 | char *zName8; |
| 128791 | + |
| 128792 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128793 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 128794 | +#endif |
| 127707 | 128795 | sqlite3_mutex_enter(db->mutex); |
| 127708 | 128796 | assert( !db->mallocFailed ); |
| 127709 | 128797 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 127710 | 128798 | if( zName8 ){ |
| 127711 | 128799 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| | @@ -127724,10 +128812,13 @@ |
| 127724 | 128812 | SQLITE_API int sqlite3_collation_needed( |
| 127725 | 128813 | sqlite3 *db, |
| 127726 | 128814 | void *pCollNeededArg, |
| 127727 | 128815 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 127728 | 128816 | ){ |
| 128817 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128818 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 128819 | +#endif |
| 127729 | 128820 | sqlite3_mutex_enter(db->mutex); |
| 127730 | 128821 | db->xCollNeeded = xCollNeeded; |
| 127731 | 128822 | db->xCollNeeded16 = 0; |
| 127732 | 128823 | db->pCollNeededArg = pCollNeededArg; |
| 127733 | 128824 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127742,10 +128833,13 @@ |
| 127742 | 128833 | SQLITE_API int sqlite3_collation_needed16( |
| 127743 | 128834 | sqlite3 *db, |
| 127744 | 128835 | void *pCollNeededArg, |
| 127745 | 128836 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 127746 | 128837 | ){ |
| 128838 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128839 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 128840 | +#endif |
| 127747 | 128841 | sqlite3_mutex_enter(db->mutex); |
| 127748 | 128842 | db->xCollNeeded = 0; |
| 127749 | 128843 | db->xCollNeeded16 = xCollNeeded16; |
| 127750 | 128844 | db->pCollNeededArg = pCollNeededArg; |
| 127751 | 128845 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127768,10 +128862,16 @@ |
| 127768 | 128862 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 127769 | 128863 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 127770 | 128864 | ** by the next COMMIT or ROLLBACK. |
| 127771 | 128865 | */ |
| 127772 | 128866 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 128867 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128868 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 128869 | + (void)SQLITE_MISUSE_BKPT; |
| 128870 | + return 0; |
| 128871 | + } |
| 128872 | +#endif |
| 127773 | 128873 | return db->autoCommit; |
| 127774 | 128874 | } |
| 127775 | 128875 | |
| 127776 | 128876 | /* |
| 127777 | 128877 | ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| | @@ -127950,10 +129050,13 @@ |
| 127950 | 129050 | |
| 127951 | 129051 | /* |
| 127952 | 129052 | ** Enable or disable the extended result codes. |
| 127953 | 129053 | */ |
| 127954 | 129054 | SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 129055 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129056 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129057 | +#endif |
| 127955 | 129058 | sqlite3_mutex_enter(db->mutex); |
| 127956 | 129059 | db->errMask = onoff ? 0xffffffff : 0xff; |
| 127957 | 129060 | sqlite3_mutex_leave(db->mutex); |
| 127958 | 129061 | return SQLITE_OK; |
| 127959 | 129062 | } |
| | @@ -127963,10 +129066,13 @@ |
| 127963 | 129066 | */ |
| 127964 | 129067 | SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 127965 | 129068 | int rc = SQLITE_ERROR; |
| 127966 | 129069 | Btree *pBtree; |
| 127967 | 129070 | |
| 129071 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129072 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129073 | +#endif |
| 127968 | 129074 | sqlite3_mutex_enter(db->mutex); |
| 127969 | 129075 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 127970 | 129076 | if( pBtree ){ |
| 127971 | 129077 | Pager *pPager; |
| 127972 | 129078 | sqlite3_file *fd; |
| | @@ -128305,11 +129411,11 @@ |
| 128305 | 129411 | ** query parameter we seek. This routine returns the value of the zParam |
| 128306 | 129412 | ** parameter if it exists. If the parameter does not exist, this routine |
| 128307 | 129413 | ** returns a NULL pointer. |
| 128308 | 129414 | */ |
| 128309 | 129415 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 128310 | | - if( zFilename==0 ) return 0; |
| 129416 | + if( zFilename==0 || zParam==0 ) return 0; |
| 128311 | 129417 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128312 | 129418 | while( zFilename[0] ){ |
| 128313 | 129419 | int x = strcmp(zFilename, zParam); |
| 128314 | 129420 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128315 | 129421 | if( x==0 ) return zFilename; |
| | @@ -128361,19 +129467,31 @@ |
| 128361 | 129467 | /* |
| 128362 | 129468 | ** Return the filename of the database associated with a database |
| 128363 | 129469 | ** connection. |
| 128364 | 129470 | */ |
| 128365 | 129471 | SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 129472 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129473 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 129474 | + (void)SQLITE_MISUSE_BKPT; |
| 129475 | + return 0; |
| 129476 | + } |
| 129477 | +#endif |
| 128366 | 129478 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128367 | 129479 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 128368 | 129480 | } |
| 128369 | 129481 | |
| 128370 | 129482 | /* |
| 128371 | 129483 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 128372 | 129484 | ** no such database exists. |
| 128373 | 129485 | */ |
| 128374 | 129486 | SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 129487 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129488 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 129489 | + (void)SQLITE_MISUSE_BKPT; |
| 129490 | + return -1; |
| 129491 | + } |
| 129492 | +#endif |
| 128375 | 129493 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128376 | 129494 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 128377 | 129495 | } |
| 128378 | 129496 | |
| 128379 | 129497 | /************** End of main.c ************************************************/ |
| 128380 | 129498 | |