Fossil SCM
merge trunk
Commit
e410d40b1ba10bf462126bf2cff092131b344523
Parent
8fce80ea99931e7…
2 files changed
+2156
-1025
+469
-251
+2156
-1025
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | -** version 3.7.2. By combining all the individual C code files into this | |
| 3 | +** version 3.7.3. By combining all the individual C code files into this | |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| @@ -352,19 +352,25 @@ | ||
| 352 | 352 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 353 | 353 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 354 | 354 | #endif |
| 355 | 355 | |
| 356 | 356 | /* |
| 357 | -** The SQLITE_THREADSAFE macro must be defined as either 0 or 1. | |
| 357 | +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. | |
| 358 | +** 0 means mutexes are permanently disable and the library is never | |
| 359 | +** threadsafe. 1 means the library is serialized which is the highest | |
| 360 | +** level of threadsafety. 2 means the libary is multithreaded - multiple | |
| 361 | +** threads can use SQLite as long as no two threads try to use the same | |
| 362 | +** database connection at the same time. | |
| 363 | +** | |
| 358 | 364 | ** Older versions of SQLite used an optional THREADSAFE macro. |
| 359 | -** We support that for legacy | |
| 365 | +** We support that for legacy. | |
| 360 | 366 | */ |
| 361 | 367 | #if !defined(SQLITE_THREADSAFE) |
| 362 | 368 | #if defined(THREADSAFE) |
| 363 | 369 | # define SQLITE_THREADSAFE THREADSAFE |
| 364 | 370 | #else |
| 365 | -# define SQLITE_THREADSAFE 1 | |
| 371 | +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */ | |
| 366 | 372 | #endif |
| 367 | 373 | #endif |
| 368 | 374 | |
| 369 | 375 | /* |
| 370 | 376 | ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. |
| @@ -642,13 +648,13 @@ | ||
| 642 | 648 | ** |
| 643 | 649 | ** See also: [sqlite3_libversion()], |
| 644 | 650 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 645 | 651 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 646 | 652 | */ |
| 647 | -#define SQLITE_VERSION "3.7.2" | |
| 648 | -#define SQLITE_VERSION_NUMBER 3007002 | |
| 649 | -#define SQLITE_SOURCE_ID "2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3" | |
| 653 | +#define SQLITE_VERSION "3.7.3" | |
| 654 | +#define SQLITE_VERSION_NUMBER 3007003 | |
| 655 | +#define SQLITE_SOURCE_ID "2010-09-29 23:09:23 1ef0dc9328f47506cb2dcd142150e96cb4755216" | |
| 650 | 656 | |
| 651 | 657 | /* |
| 652 | 658 | ** CAPI3REF: Run-Time Library Version Numbers |
| 653 | 659 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 654 | 660 | ** |
| @@ -1292,19 +1298,23 @@ | ||
| 1292 | 1298 | ** object once the object has been registered. |
| 1293 | 1299 | ** |
| 1294 | 1300 | ** The zName field holds the name of the VFS module. The name must |
| 1295 | 1301 | ** be unique across all VFS modules. |
| 1296 | 1302 | ** |
| 1297 | -** SQLite will guarantee that the zFilename parameter to xOpen | |
| 1303 | +** ^SQLite guarantees that the zFilename parameter to xOpen | |
| 1298 | 1304 | ** is either a NULL pointer or string obtained |
| 1299 | -** from xFullPathname(). SQLite further guarantees that | |
| 1305 | +** from xFullPathname() with an optional suffix added. | |
| 1306 | +** ^If a suffix is added to the zFilename parameter, it will | |
| 1307 | +** consist of a single "-" character followed by no more than | |
| 1308 | +** 10 alphanumeric and/or "-" characters. | |
| 1309 | +** ^SQLite further guarantees that | |
| 1300 | 1310 | ** the string will be valid and unchanged until xClose() is |
| 1301 | 1311 | ** called. Because of the previous sentence, |
| 1302 | 1312 | ** the [sqlite3_file] can safely store a pointer to the |
| 1303 | 1313 | ** filename if it needs to remember the filename for some reason. |
| 1304 | -** If the zFilename parameter is xOpen is a NULL pointer then xOpen | |
| 1305 | -** must invent its own temporary name for the file. Whenever the | |
| 1314 | +** If the zFilename parameter to xOpen is a NULL pointer then xOpen | |
| 1315 | +** must invent its own temporary name for the file. ^Whenever the | |
| 1306 | 1316 | ** xFilename parameter is NULL it will also be the case that the |
| 1307 | 1317 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 1308 | 1318 | ** |
| 1309 | 1319 | ** The flags argument to xOpen() includes all bits set in |
| 1310 | 1320 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -1311,11 +1321,11 @@ | ||
| 1311 | 1321 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 1312 | 1322 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 1313 | 1323 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 1314 | 1324 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 1315 | 1325 | ** |
| 1316 | -** SQLite will also add one of the following flags to the xOpen() | |
| 1326 | +** ^(SQLite will also add one of the following flags to the xOpen() | |
| 1317 | 1327 | ** call, depending on the object being opened: |
| 1318 | 1328 | ** |
| 1319 | 1329 | ** <ul> |
| 1320 | 1330 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 1321 | 1331 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -1322,11 +1332,12 @@ | ||
| 1322 | 1332 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 1323 | 1333 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 1324 | 1334 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 1325 | 1335 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 1326 | 1336 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 1327 | -** </ul> | |
| 1337 | +** <li> [SQLITE_OPEN_WAL] | |
| 1338 | +** </ul>)^ | |
| 1328 | 1339 | ** |
| 1329 | 1340 | ** The file I/O implementation can use the object type flags to |
| 1330 | 1341 | ** change the way it deals with files. For example, an application |
| 1331 | 1342 | ** that does not care about crash recovery or rollback might make |
| 1332 | 1343 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -1341,39 +1352,40 @@ | ||
| 1341 | 1352 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 1342 | 1353 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 1343 | 1354 | ** </ul> |
| 1344 | 1355 | ** |
| 1345 | 1356 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 1346 | -** deleted when it is closed. The [SQLITE_OPEN_DELETEONCLOSE] | |
| 1347 | -** will be set for TEMP databases, journals and for subjournals. | |
| 1357 | +** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] | |
| 1358 | +** will be set for TEMP databases and their journals, transient | |
| 1359 | +** databases, and subjournals. | |
| 1348 | 1360 | ** |
| 1349 | -** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction | |
| 1361 | +** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction | |
| 1350 | 1362 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 1351 | 1363 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 1352 | 1364 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 1353 | 1365 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 1354 | 1366 | ** be created, and that it is an error if it already exists. |
| 1355 | 1367 | ** It is <i>not</i> used to indicate the file should be opened |
| 1356 | 1368 | ** for exclusive access. |
| 1357 | 1369 | ** |
| 1358 | -** At least szOsFile bytes of memory are allocated by SQLite | |
| 1370 | +** ^At least szOsFile bytes of memory are allocated by SQLite | |
| 1359 | 1371 | ** to hold the [sqlite3_file] structure passed as the third |
| 1360 | 1372 | ** argument to xOpen. The xOpen method does not have to |
| 1361 | 1373 | ** allocate the structure; it should just fill it in. Note that |
| 1362 | 1374 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 1363 | 1375 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 1364 | 1376 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 1365 | 1377 | ** element will be valid after xOpen returns regardless of the success |
| 1366 | 1378 | ** or failure of the xOpen call. |
| 1367 | 1379 | ** |
| 1368 | -** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] | |
| 1380 | +** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] | |
| 1369 | 1381 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 1370 | 1382 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 1371 | 1383 | ** to test whether a file is at least readable. The file can be a |
| 1372 | 1384 | ** directory. |
| 1373 | 1385 | ** |
| 1374 | -** SQLite will always allocate at least mxPathname+1 bytes for the | |
| 1386 | +** ^SQLite will always allocate at least mxPathname+1 bytes for the | |
| 1375 | 1387 | ** output buffer xFullPathname. The exact size of the output buffer |
| 1376 | 1388 | ** is also passed as a parameter to both methods. If the output buffer |
| 1377 | 1389 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 1378 | 1390 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 1379 | 1391 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -1383,14 +1395,14 @@ | ||
| 1383 | 1395 | ** included in the VFS structure for completeness. |
| 1384 | 1396 | ** The xRandomness() function attempts to return nBytes bytes |
| 1385 | 1397 | ** of good-quality randomness into zOut. The return value is |
| 1386 | 1398 | ** the actual number of bytes of randomness obtained. |
| 1387 | 1399 | ** The xSleep() method causes the calling thread to sleep for at |
| 1388 | -** least the number of microseconds given. The xCurrentTime() | |
| 1400 | +** least the number of microseconds given. ^The xCurrentTime() | |
| 1389 | 1401 | ** method returns a Julian Day Number for the current date and time as |
| 1390 | 1402 | ** a floating point value. |
| 1391 | -** The xCurrentTimeInt64() method returns, as an integer, the Julian | |
| 1403 | +** ^The xCurrentTimeInt64() method returns, as an integer, the Julian | |
| 1392 | 1404 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 1393 | 1405 | ** a 24-hour day). |
| 1394 | 1406 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 1395 | 1407 | ** date and time if that method is available (if iVersion is 2 or |
| 1396 | 1408 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1783,11 +1795,11 @@ | ||
| 1783 | 1795 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1784 | 1796 | ** following SQLite interfaces become non-operational: |
| 1785 | 1797 | ** <ul> |
| 1786 | 1798 | ** <li> [sqlite3_memory_used()] |
| 1787 | 1799 | ** <li> [sqlite3_memory_highwater()] |
| 1788 | -** <li> [sqlite3_soft_heap_limit()] | |
| 1800 | +** <li> [sqlite3_soft_heap_limit64()] | |
| 1789 | 1801 | ** <li> [sqlite3_status()] |
| 1790 | 1802 | ** </ul>)^ |
| 1791 | 1803 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1792 | 1804 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1793 | 1805 | ** allocation statistics are disabled by default. |
| @@ -1797,19 +1809,18 @@ | ||
| 1797 | 1809 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1798 | 1810 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1799 | 1811 | ** aligned memory buffer from which the scrach allocations will be |
| 1800 | 1812 | ** drawn, the size of each scratch allocation (sz), |
| 1801 | 1813 | ** and the maximum number of scratch allocations (N). The sz |
| 1802 | -** argument must be a multiple of 16. The sz parameter should be a few bytes | |
| 1803 | -** larger than the actual scratch space required due to internal overhead. | |
| 1814 | +** argument must be a multiple of 16. | |
| 1804 | 1815 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1805 | 1816 | ** of at least sz*N bytes of memory. |
| 1806 | -** ^SQLite will use no more than one scratch buffer per thread. So | |
| 1807 | -** N should be set to the expected maximum number of threads. ^SQLite will | |
| 1808 | -** never require a scratch buffer that is more than 6 times the database | |
| 1809 | -** page size. ^If SQLite needs needs additional scratch memory beyond | |
| 1810 | -** what is provided by this configuration option, then | |
| 1817 | +** ^SQLite will use no more than two scratch buffers per thread. So | |
| 1818 | +** N should be set to twice the expected maximum number of threads. | |
| 1819 | +** ^SQLite will never require a scratch buffer that is more than 6 | |
| 1820 | +** times the database page size. ^If SQLite needs needs additional | |
| 1821 | +** scratch memory beyond what is provided by this configuration option, then | |
| 1811 | 1822 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1812 | 1823 | ** |
| 1813 | 1824 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1814 | 1825 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1815 | 1826 | ** the database page cache with the default page cache implemenation. |
| @@ -1825,12 +1836,11 @@ | ||
| 1825 | 1836 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1826 | 1837 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1827 | 1838 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1828 | 1839 | ** page cache memory is needed beyond what is provided by this option, then |
| 1829 | 1840 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1830 | -** ^The implementation might use one or more of the N buffers to hold | |
| 1831 | -** memory accounting information. The pointer in the first argument must | |
| 1841 | +** The pointer in the first argument must | |
| 1832 | 1842 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1833 | 1843 | ** will be undefined.</dd> |
| 1834 | 1844 | ** |
| 1835 | 1845 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1836 | 1846 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1955,12 +1965,18 @@ | ||
| 1955 | 1965 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1956 | 1966 | ** slots. The size of the buffer in the first argument must be greater than |
| 1957 | 1967 | ** or equal to the product of the second and third arguments. The buffer |
| 1958 | 1968 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1959 | 1969 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1960 | -** rounded down to the next smaller | |
| 1961 | -** multiple of 8. See also: [SQLITE_CONFIG_LOOKASIDE]</dd> | |
| 1970 | +** rounded down to the next smaller multiple of 8. ^(The lookaside memory | |
| 1971 | +** configuration for a database connection can only be changed when that | |
| 1972 | +** connection is not currently using lookaside memory, or in other words | |
| 1973 | +** when the "current value" returned by | |
| 1974 | +** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. | |
| 1975 | +** Any attempt to change the lookaside memory configuration when lookaside | |
| 1976 | +** memory is in use leaves the configuration unchanged and returns | |
| 1977 | +** [SQLITE_BUSY].)^</dd> | |
| 1962 | 1978 | ** |
| 1963 | 1979 | ** </dl> |
| 1964 | 1980 | */ |
| 1965 | 1981 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1966 | 1982 | |
| @@ -2260,10 +2276,13 @@ | ||
| 2260 | 2276 | */ |
| 2261 | 2277 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 2262 | 2278 | |
| 2263 | 2279 | /* |
| 2264 | 2280 | ** CAPI3REF: Convenience Routines For Running Queries |
| 2281 | +** | |
| 2282 | +** This is a legacy interface that is preserved for backwards compatibility. | |
| 2283 | +** Use of this interface is not recommended. | |
| 2265 | 2284 | ** |
| 2266 | 2285 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 2267 | 2286 | ** [sqlite3_get_table()] interface. A result table records the |
| 2268 | 2287 | ** complete query results from one or more queries. |
| 2269 | 2288 | ** |
| @@ -2281,11 +2300,11 @@ | ||
| 2281 | 2300 | ** |
| 2282 | 2301 | ** A result table might consist of one or more memory allocations. |
| 2283 | 2302 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 2284 | 2303 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 2285 | 2304 | ** |
| 2286 | -** As an example of the result table format, suppose a query result | |
| 2305 | +** ^(As an example of the result table format, suppose a query result | |
| 2287 | 2306 | ** is as follows: |
| 2288 | 2307 | ** |
| 2289 | 2308 | ** <blockquote><pre> |
| 2290 | 2309 | ** Name | Age |
| 2291 | 2310 | ** ----------------------- |
| @@ -2305,31 +2324,31 @@ | ||
| 2305 | 2324 | ** azResult[3] = "43"; |
| 2306 | 2325 | ** azResult[4] = "Bob"; |
| 2307 | 2326 | ** azResult[5] = "28"; |
| 2308 | 2327 | ** azResult[6] = "Cindy"; |
| 2309 | 2328 | ** azResult[7] = "21"; |
| 2310 | -** </pre></blockquote> | |
| 2329 | +** </pre></blockquote>)^ | |
| 2311 | 2330 | ** |
| 2312 | 2331 | ** ^The sqlite3_get_table() function evaluates one or more |
| 2313 | 2332 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 2314 | 2333 | ** string of its 2nd parameter and returns a result table to the |
| 2315 | 2334 | ** pointer given in its 3rd parameter. |
| 2316 | 2335 | ** |
| 2317 | 2336 | ** After the application has finished with the result from sqlite3_get_table(), |
| 2318 | -** it should pass the result table pointer to sqlite3_free_table() in order to | |
| 2337 | +** it must pass the result table pointer to sqlite3_free_table() in order to | |
| 2319 | 2338 | ** release the memory that was malloced. Because of the way the |
| 2320 | 2339 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 2321 | 2340 | ** function must not try to call [sqlite3_free()] directly. Only |
| 2322 | 2341 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 2323 | 2342 | ** |
| 2324 | -** ^(The sqlite3_get_table() interface is implemented as a wrapper around | |
| 2343 | +** The sqlite3_get_table() interface is implemented as a wrapper around | |
| 2325 | 2344 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 2326 | 2345 | ** to any internal data structures of SQLite. It uses only the public |
| 2327 | 2346 | ** interface defined here. As a consequence, errors that occur in the |
| 2328 | 2347 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 2329 | 2348 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 2330 | -** [sqlite3_errmsg()].)^ | |
| 2349 | +** [sqlite3_errmsg()]. | |
| 2331 | 2350 | */ |
| 2332 | 2351 | SQLITE_API int sqlite3_get_table( |
| 2333 | 2352 | sqlite3 *db, /* An open database */ |
| 2334 | 2353 | const char *zSql, /* SQL to be evaluated */ |
| 2335 | 2354 | char ***pazResult, /* Results of the query */ |
| @@ -2477,11 +2496,13 @@ | ||
| 2477 | 2496 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 2478 | 2497 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 2479 | 2498 | ** is not freed. |
| 2480 | 2499 | ** |
| 2481 | 2500 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 2482 | -** is always aligned to at least an 8 byte boundary. | |
| 2501 | +** is always aligned to at least an 8 byte boundary, or to a | |
| 2502 | +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time | |
| 2503 | +** option is used. | |
| 2483 | 2504 | ** |
| 2484 | 2505 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 2485 | 2506 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 2486 | 2507 | ** implementation of these routines to be omitted. That capability |
| 2487 | 2508 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2735,21 +2756,32 @@ | ||
| 2735 | 2756 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2736 | 2757 | |
| 2737 | 2758 | /* |
| 2738 | 2759 | ** CAPI3REF: Query Progress Callbacks |
| 2739 | 2760 | ** |
| 2740 | -** ^This routine configures a callback function - the | |
| 2741 | -** progress callback - that is invoked periodically during long | |
| 2742 | -** running calls to [sqlite3_exec()], [sqlite3_step()] and | |
| 2743 | -** [sqlite3_get_table()]. An example use for this | |
| 2761 | +** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback | |
| 2762 | +** function X to be invoked periodically during long running calls to | |
| 2763 | +** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for | |
| 2764 | +** database connection D. An example use for this | |
| 2744 | 2765 | ** interface is to keep a GUI updated during a large query. |
| 2766 | +** | |
| 2767 | +** ^The parameter P is passed through as the only parameter to the | |
| 2768 | +** callback function X. ^The parameter N is the number of | |
| 2769 | +** [virtual machine instructions] that are evaluated between successive | |
| 2770 | +** invocations of the callback X. | |
| 2771 | +** | |
| 2772 | +** ^Only a single progress handler may be defined at one time per | |
| 2773 | +** [database connection]; setting a new progress handler cancels the | |
| 2774 | +** old one. ^Setting parameter X to NULL disables the progress handler. | |
| 2775 | +** ^The progress handler is also disabled by setting N to a value less | |
| 2776 | +** than 1. | |
| 2745 | 2777 | ** |
| 2746 | 2778 | ** ^If the progress callback returns non-zero, the operation is |
| 2747 | 2779 | ** interrupted. This feature can be used to implement a |
| 2748 | 2780 | ** "Cancel" button on a GUI progress dialog box. |
| 2749 | 2781 | ** |
| 2750 | -** The progress handler must not do anything that will modify | |
| 2782 | +** The progress handler callback must not do anything that will modify | |
| 2751 | 2783 | ** the database connection that invoked the progress handler. |
| 2752 | 2784 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2753 | 2785 | ** database connections for the meaning of "modify" in this paragraph. |
| 2754 | 2786 | ** |
| 2755 | 2787 | */ |
| @@ -2804,11 +2836,11 @@ | ||
| 2804 | 2836 | ** </dl> |
| 2805 | 2837 | ** |
| 2806 | 2838 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2807 | 2839 | ** combinations shown above or one of the combinations shown above combined |
| 2808 | 2840 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2809 | -** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags, | |
| 2841 | +** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags, | |
| 2810 | 2842 | ** then the behavior is undefined. |
| 2811 | 2843 | ** |
| 2812 | 2844 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2813 | 2845 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2814 | 2846 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2929,21 +2961,26 @@ | ||
| 2929 | 2961 | ** ^(This interface allows the size of various constructs to be limited |
| 2930 | 2962 | ** on a connection by connection basis. The first parameter is the |
| 2931 | 2963 | ** [database connection] whose limit is to be set or queried. The |
| 2932 | 2964 | ** second parameter is one of the [limit categories] that define a |
| 2933 | 2965 | ** class of constructs to be size limited. The third parameter is the |
| 2934 | -** new limit for that construct. The function returns the old limit.)^ | |
| 2966 | +** new limit for that construct.)^ | |
| 2935 | 2967 | ** |
| 2936 | 2968 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2937 | -** ^(For the limit category of SQLITE_LIMIT_XYZ there is a | |
| 2969 | +** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a | |
| 2938 | 2970 | ** [limits | hard upper bound] |
| 2939 | -** set by a compile-time C preprocessor macro named | |
| 2940 | -** [limits | SQLITE_MAX_XYZ]. | |
| 2971 | +** set at compile-time by a C preprocessor macro called | |
| 2972 | +** [limits | SQLITE_MAX_<i>NAME</i>]. | |
| 2941 | 2973 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2942 | 2974 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2943 | 2975 | ** silently truncated to the hard upper bound. |
| 2944 | 2976 | ** |
| 2977 | +** ^Regardless of whether or not the limit was changed, the | |
| 2978 | +** [sqlite3_limit()] interface returns the prior value of the limit. | |
| 2979 | +** ^Hence, to find the current value of a limit without changing it, | |
| 2980 | +** simply invoke this interface with the third parameter set to -1. | |
| 2981 | +** | |
| 2945 | 2982 | ** Run-time limits are intended for use in applications that manage |
| 2946 | 2983 | ** both their own internal database and also databases that are controlled |
| 2947 | 2984 | ** by untrusted external sources. An example application might be a |
| 2948 | 2985 | ** web browser that has its own databases for storing history and |
| 2949 | 2986 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2968,11 +3005,11 @@ | ||
| 2968 | 3005 | ** The synopsis of the meanings of the various limits is shown below. |
| 2969 | 3006 | ** Additional information is available at [limits | Limits in SQLite]. |
| 2970 | 3007 | ** |
| 2971 | 3008 | ** <dl> |
| 2972 | 3009 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 2973 | -** <dd>The maximum size of any string or BLOB or table row.<dd>)^ | |
| 3010 | +** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^ | |
| 2974 | 3011 | ** |
| 2975 | 3012 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 2976 | 3013 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 2977 | 3014 | ** |
| 2978 | 3015 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2986,11 +3023,13 @@ | ||
| 2986 | 3023 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 2987 | 3024 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 2988 | 3025 | ** |
| 2989 | 3026 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 2990 | 3027 | ** <dd>The maximum number of instructions in a virtual machine program |
| 2991 | -** used to implement an SQL statement.</dd>)^ | |
| 3028 | +** used to implement an SQL statement. This limit is not currently | |
| 3029 | +** enforced, though that might be added in some future release of | |
| 3030 | +** SQLite.</dd>)^ | |
| 2992 | 3031 | ** |
| 2993 | 3032 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 2994 | 3033 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 2995 | 3034 | ** |
| 2996 | 3035 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2999,12 +3038,11 @@ | ||
| 2999 | 3038 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 3000 | 3039 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 3001 | 3040 | ** [GLOB] operators.</dd>)^ |
| 3002 | 3041 | ** |
| 3003 | 3042 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 3004 | -** <dd>The maximum number of variables in an SQL statement that can | |
| 3005 | -** be bound.</dd>)^ | |
| 3043 | +** <dd>The maximum index number of any [parameter] in an SQL statement.)^ | |
| 3006 | 3044 | ** |
| 3007 | 3045 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 3008 | 3046 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 3009 | 3047 | ** </dl> |
| 3010 | 3048 | */ |
| @@ -3072,16 +3110,11 @@ | ||
| 3072 | 3110 | ** |
| 3073 | 3111 | ** <ol> |
| 3074 | 3112 | ** <li> |
| 3075 | 3113 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 3076 | 3114 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 3077 | -** statement and try to run it again. ^If the schema has changed in | |
| 3078 | -** a way that makes the statement no longer valid, [sqlite3_step()] will still | |
| 3079 | -** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is | |
| 3080 | -** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the | |
| 3081 | -** error go away. Note: use [sqlite3_errmsg()] to find the text | |
| 3082 | -** of the parsing error that results in an [SQLITE_SCHEMA] return. | |
| 3115 | +** statement and try to run it again. | |
| 3083 | 3116 | ** </li> |
| 3084 | 3117 | ** |
| 3085 | 3118 | ** <li> |
| 3086 | 3119 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 3087 | 3120 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -3090,15 +3123,20 @@ | ||
| 3090 | 3123 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 3091 | 3124 | ** interfaces, the underlying reason for the error is returned immediately. |
| 3092 | 3125 | ** </li> |
| 3093 | 3126 | ** |
| 3094 | 3127 | ** <li> |
| 3095 | -** ^If the value of a [parameter | host parameter] in the WHERE clause might | |
| 3096 | -** change the query plan for a statement, then the statement may be | |
| 3097 | -** automatically recompiled (as if there had been a schema change) on the first | |
| 3098 | -** [sqlite3_step()] call following any change to the | |
| 3099 | -** [sqlite3_bind_text | bindings] of the [parameter]. | |
| 3128 | +** ^If the specific value bound to [parameter | host parameter] in the | |
| 3129 | +** WHERE clause might influence the choice of query plan for a statement, | |
| 3130 | +** then the statement will be automatically recompiled, as if there had been | |
| 3131 | +** a schema change, on the first [sqlite3_step()] call following any change | |
| 3132 | +** to the [sqlite3_bind_text | bindings] of that [parameter]. | |
| 3133 | +** ^The specific value of WHERE-clause [parameter] might influence the | |
| 3134 | +** choice of query plan if the parameter is the left-hand side of a [LIKE] | |
| 3135 | +** or [GLOB] operator or if the parameter is compared to an indexed column | |
| 3136 | +** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. | |
| 3137 | +** the | |
| 3100 | 3138 | ** </li> |
| 3101 | 3139 | ** </ol> |
| 3102 | 3140 | */ |
| 3103 | 3141 | SQLITE_API int sqlite3_prepare( |
| 3104 | 3142 | sqlite3 *db, /* Database handle */ |
| @@ -3161,11 +3199,11 @@ | ||
| 3161 | 3199 | ** or if SQLite is run in one of reduced mutex modes |
| 3162 | 3200 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 3163 | 3201 | ** then there is no distinction between protected and unprotected |
| 3164 | 3202 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 3165 | 3203 | ** for maximum code portability it is recommended that applications |
| 3166 | -** still make the distinction between between protected and unprotected | |
| 3204 | +** still make the distinction between protected and unprotected | |
| 3167 | 3205 | ** sqlite3_value objects even when not strictly required. |
| 3168 | 3206 | ** |
| 3169 | 3207 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 3170 | 3208 | ** implementation of [application-defined SQL functions] are protected. |
| 3171 | 3209 | ** ^The sqlite3_value object returned by |
| @@ -3356,10 +3394,12 @@ | ||
| 3356 | 3394 | ** CAPI3REF: Number Of Columns In A Result Set |
| 3357 | 3395 | ** |
| 3358 | 3396 | ** ^Return the number of columns in the result set returned by the |
| 3359 | 3397 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 3360 | 3398 | ** statement that does not return data (for example an [UPDATE]). |
| 3399 | +** | |
| 3400 | +** See also: [sqlite3_data_count()] | |
| 3361 | 3401 | */ |
| 3362 | 3402 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 3363 | 3403 | |
| 3364 | 3404 | /* |
| 3365 | 3405 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3546,12 +3586,18 @@ | ||
| 3546 | 3586 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3547 | 3587 | |
| 3548 | 3588 | /* |
| 3549 | 3589 | ** CAPI3REF: Number of columns in a result set |
| 3550 | 3590 | ** |
| 3551 | -** ^The sqlite3_data_count(P) the number of columns in the | |
| 3552 | -** of the result set of [prepared statement] P. | |
| 3591 | +** ^The sqlite3_data_count(P) interface returns the number of columns in the | |
| 3592 | +** current row of the result set of [prepared statement] P. | |
| 3593 | +** ^If prepared statement P does not have results ready to return | |
| 3594 | +** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of | |
| 3595 | +** interfaces) then sqlite3_data_count(P) returns 0. | |
| 3596 | +** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. | |
| 3597 | +** | |
| 3598 | +** See also: [sqlite3_column_count()] | |
| 3553 | 3599 | */ |
| 3554 | 3600 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3555 | 3601 | |
| 3556 | 3602 | /* |
| 3557 | 3603 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3627,22 +3673,30 @@ | ||
| 3627 | 3673 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3628 | 3674 | ** the string to UTF-8 and then returns the number of bytes. |
| 3629 | 3675 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3630 | 3676 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3631 | 3677 | ** the number of bytes in that string. |
| 3632 | -** ^The value returned does not include the zero terminator at the end | |
| 3633 | -** of the string. ^For clarity: the value returned is the number of | |
| 3678 | +** ^If the result is NULL, then sqlite3_column_bytes() returns zero. | |
| 3679 | +** | |
| 3680 | +** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() | |
| 3681 | +** routine returns the number of bytes in that BLOB or string. | |
| 3682 | +** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts | |
| 3683 | +** the string to UTF-16 and then returns the number of bytes. | |
| 3684 | +** ^If the result is a numeric value then sqlite3_column_bytes16() uses | |
| 3685 | +** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns | |
| 3686 | +** the number of bytes in that string. | |
| 3687 | +** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. | |
| 3688 | +** | |
| 3689 | +** ^The values returned by [sqlite3_column_bytes()] and | |
| 3690 | +** [sqlite3_column_bytes16()] do not include the zero terminators at the end | |
| 3691 | +** of the string. ^For clarity: the values returned by | |
| 3692 | +** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of | |
| 3634 | 3693 | ** bytes in the string, not the number of characters. |
| 3635 | 3694 | ** |
| 3636 | 3695 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3637 | 3696 | ** even empty strings, are always zero terminated. ^The return |
| 3638 | -** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary | |
| 3639 | -** pointer, possibly even a NULL pointer. | |
| 3640 | -** | |
| 3641 | -** ^The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() | |
| 3642 | -** but leaves the result in UTF-16 in native byte order instead of UTF-8. | |
| 3643 | -** ^The zero terminator is not included in this count. | |
| 3697 | +** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. | |
| 3644 | 3698 | ** |
| 3645 | 3699 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3646 | 3700 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3647 | 3701 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3648 | 3702 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3683,14 +3737,14 @@ | ||
| 3683 | 3737 | ** and atof(). SQLite does not really use these functions. It has its |
| 3684 | 3738 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3685 | 3739 | ** used in the table for brevity and because they are familiar to most |
| 3686 | 3740 | ** C programmers. |
| 3687 | 3741 | ** |
| 3688 | -** ^Note that when type conversions occur, pointers returned by prior | |
| 3742 | +** Note that when type conversions occur, pointers returned by prior | |
| 3689 | 3743 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3690 | 3744 | ** sqlite3_column_text16() may be invalidated. |
| 3691 | -** ^(Type conversions and pointer invalidations might occur | |
| 3745 | +** Type conversions and pointer invalidations might occur | |
| 3692 | 3746 | ** in the following cases: |
| 3693 | 3747 | ** |
| 3694 | 3748 | ** <ul> |
| 3695 | 3749 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3696 | 3750 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3699,26 +3753,26 @@ | ||
| 3699 | 3753 | ** sqlite3_column_text16() is called. The content must be converted |
| 3700 | 3754 | ** to UTF-16.</li> |
| 3701 | 3755 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3702 | 3756 | ** sqlite3_column_text() is called. The content must be converted |
| 3703 | 3757 | ** to UTF-8.</li> |
| 3704 | -** </ul>)^ | |
| 3758 | +** </ul> | |
| 3705 | 3759 | ** |
| 3706 | 3760 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3707 | 3761 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3708 | -** that the prior pointer points to will have been modified. Other kinds | |
| 3762 | +** that the prior pointer references will have been modified. Other kinds | |
| 3709 | 3763 | ** of conversion are done in place when it is possible, but sometimes they |
| 3710 | 3764 | ** are not possible and in those cases prior pointers are invalidated. |
| 3711 | 3765 | ** |
| 3712 | -** ^(The safest and easiest to remember policy is to invoke these routines | |
| 3766 | +** The safest and easiest to remember policy is to invoke these routines | |
| 3713 | 3767 | ** in one of the following ways: |
| 3714 | 3768 | ** |
| 3715 | 3769 | ** <ul> |
| 3716 | 3770 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3717 | 3771 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3718 | 3772 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3719 | -** </ul>)^ | |
| 3773 | +** </ul> | |
| 3720 | 3774 | ** |
| 3721 | 3775 | ** In other words, you should call sqlite3_column_text(), |
| 3722 | 3776 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3723 | 3777 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3724 | 3778 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3752,21 +3806,30 @@ | ||
| 3752 | 3806 | |
| 3753 | 3807 | /* |
| 3754 | 3808 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3755 | 3809 | ** |
| 3756 | 3810 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3757 | -** ^If the statement was executed successfully or not executed at all, then | |
| 3758 | -** SQLITE_OK is returned. ^If execution of the statement failed then an | |
| 3759 | -** [error code] or [extended error code] is returned. | |
| 3811 | +** ^If the most recent evaluation of the statement encountered no errors or | |
| 3812 | +** or if the statement is never been evaluated, then sqlite3_finalize() returns | |
| 3813 | +** SQLITE_OK. ^If the most recent evaluation of statement S failed, then | |
| 3814 | +** sqlite3_finalize(S) returns the appropriate [error code] or | |
| 3815 | +** [extended error code]. | |
| 3760 | 3816 | ** |
| 3761 | -** ^This routine can be called at any point during the execution of the | |
| 3762 | -** [prepared statement]. ^If the virtual machine has not | |
| 3763 | -** completed execution when this routine is called, that is like | |
| 3764 | -** encountering an error or an [sqlite3_interrupt | interrupt]. | |
| 3765 | -** ^Incomplete updates may be rolled back and transactions canceled, | |
| 3766 | -** depending on the circumstances, and the | |
| 3767 | -** [error code] returned will be [SQLITE_ABORT]. | |
| 3817 | +** ^The sqlite3_finalize(S) routine can be called at any point during | |
| 3818 | +** the life cycle of [prepared statement] S: | |
| 3819 | +** before statement S is ever evaluated, after | |
| 3820 | +** one or more calls to [sqlite3_reset()], or after any call | |
| 3821 | +** to [sqlite3_step()] regardless of whether or not the statement has | |
| 3822 | +** completed execution. | |
| 3823 | +** | |
| 3824 | +** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. | |
| 3825 | +** | |
| 3826 | +** The application must finalize every [prepared statement] in order to avoid | |
| 3827 | +** resource leaks. It is a grievous error for the application to try to use | |
| 3828 | +** a prepared statement after it has been finalized. Any use of a prepared | |
| 3829 | +** statement after it has been finalized can result in undefined and | |
| 3830 | +** undesirable behavior such as segfaults and heap corruption. | |
| 3768 | 3831 | */ |
| 3769 | 3832 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3770 | 3833 | |
| 3771 | 3834 | /* |
| 3772 | 3835 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3798,40 +3861,42 @@ | ||
| 3798 | 3861 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3799 | 3862 | ** KEYWORDS: {function creation routines} |
| 3800 | 3863 | ** KEYWORDS: {application-defined SQL function} |
| 3801 | 3864 | ** KEYWORDS: {application-defined SQL functions} |
| 3802 | 3865 | ** |
| 3803 | -** ^These two functions (collectively known as "function creation routines") | |
| 3866 | +** ^These functions (collectively known as "function creation routines") | |
| 3804 | 3867 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3805 | -** of existing SQL functions or aggregates. The only difference between the | |
| 3806 | -** two is that the second parameter, the name of the (scalar) function or | |
| 3807 | -** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16 | |
| 3808 | -** for sqlite3_create_function16(). | |
| 3868 | +** of existing SQL functions or aggregates. The only differences between | |
| 3869 | +** these routines are the text encoding expected for | |
| 3870 | +** the the second parameter (the name of the function being created) | |
| 3871 | +** and the presence or absence of a destructor callback for | |
| 3872 | +** the application data pointer. | |
| 3809 | 3873 | ** |
| 3810 | 3874 | ** ^The first parameter is the [database connection] to which the SQL |
| 3811 | 3875 | ** function is to be added. ^If an application uses more than one database |
| 3812 | 3876 | ** connection then application-defined SQL functions must be added |
| 3813 | 3877 | ** to each database connection separately. |
| 3814 | 3878 | ** |
| 3815 | -** The second parameter is the name of the SQL function to be created or | |
| 3816 | -** redefined. ^The length of the name is limited to 255 bytes, exclusive of | |
| 3817 | -** the zero-terminator. Note that the name length limit is in bytes, not | |
| 3818 | -** characters. ^Any attempt to create a function with a longer name | |
| 3819 | -** will result in [SQLITE_ERROR] being returned. | |
| 3879 | +** ^The second parameter is the name of the SQL function to be created or | |
| 3880 | +** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 | |
| 3881 | +** representation, exclusive of the zero-terminator. ^Note that the name | |
| 3882 | +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. | |
| 3883 | +** ^Any attempt to create a function with a longer name | |
| 3884 | +** will result in [SQLITE_MISUSE] being returned. | |
| 3820 | 3885 | ** |
| 3821 | 3886 | ** ^The third parameter (nArg) |
| 3822 | 3887 | ** is the number of arguments that the SQL function or |
| 3823 | 3888 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3824 | 3889 | ** aggregate may take any number of arguments between 0 and the limit |
| 3825 | 3890 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3826 | 3891 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3827 | 3892 | ** undefined. |
| 3828 | 3893 | ** |
| 3829 | -** The fourth parameter, eTextRep, specifies what | |
| 3894 | +** ^The fourth parameter, eTextRep, specifies what | |
| 3830 | 3895 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3831 | -** its parameters. Any SQL function implementation should be able to work | |
| 3832 | -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be | |
| 3896 | +** its parameters. Every SQL function implementation must be able to work | |
| 3897 | +** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be | |
| 3833 | 3898 | ** more efficient with one encoding than another. ^An application may |
| 3834 | 3899 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3835 | 3900 | ** times with the same function but with different values of eTextRep. |
| 3836 | 3901 | ** ^When multiple implementations of the same function are available, SQLite |
| 3837 | 3902 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3839,17 +3904,25 @@ | ||
| 3839 | 3904 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3840 | 3905 | ** |
| 3841 | 3906 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3842 | 3907 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3843 | 3908 | ** |
| 3844 | -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are | |
| 3909 | +** ^The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are | |
| 3845 | 3910 | ** pointers to C-language functions that implement the SQL function or |
| 3846 | 3911 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3847 | -** callback only; NULL pointers should be passed as the xStep and xFinal | |
| 3912 | +** callback only; NULL pointers must be passed as the xStep and xFinal | |
| 3848 | 3913 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3849 | -** and xFinal and NULL should be passed for xFunc. ^To delete an existing | |
| 3850 | -** SQL function or aggregate, pass NULL for all three function callbacks. | |
| 3914 | +** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing | |
| 3915 | +** SQL function or aggregate, pass NULL poiners for all three function | |
| 3916 | +** callbacks. | |
| 3917 | +** | |
| 3918 | +** ^If the tenth parameter to sqlite3_create_function_v2() is not NULL, | |
| 3919 | +** then it is invoked when the function is deleted, either by being | |
| 3920 | +** overloaded or when the database connection closes. | |
| 3921 | +** ^When the destructure callback of the tenth parameter is invoked, it | |
| 3922 | +** is passed a single argument which is a copy of the pointer which was | |
| 3923 | +** the fifth parameter to sqlite3_create_function_v2(). | |
| 3851 | 3924 | ** |
| 3852 | 3925 | ** ^It is permitted to register multiple implementations of the same |
| 3853 | 3926 | ** functions with the same name but with either differing numbers of |
| 3854 | 3927 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3855 | 3928 | ** the implementation that most closely matches the way in which the |
| @@ -3861,15 +3934,10 @@ | ||
| 3861 | 3934 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3862 | 3935 | ** is a closer match than a function where the encoding difference is |
| 3863 | 3936 | ** between UTF8 and UTF16. |
| 3864 | 3937 | ** |
| 3865 | 3938 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3866 | -** ^The first application-defined function with a given name overrides all | |
| 3867 | -** built-in functions in the same [database connection] with the same name. | |
| 3868 | -** ^Subsequent application-defined functions of the same name only override | |
| 3869 | -** prior application-defined functions that are an exact match for the | |
| 3870 | -** number of parameters and preferred encoding. | |
| 3871 | 3939 | ** |
| 3872 | 3940 | ** ^An application-defined function is permitted to call other |
| 3873 | 3941 | ** SQLite interfaces. However, such calls must not |
| 3874 | 3942 | ** close the database connection nor finalize or reset the prepared |
| 3875 | 3943 | ** statement in which the function is running. |
| @@ -3891,10 +3959,21 @@ | ||
| 3891 | 3959 | int eTextRep, |
| 3892 | 3960 | void *pApp, |
| 3893 | 3961 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3894 | 3962 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3895 | 3963 | void (*xFinal)(sqlite3_context*) |
| 3964 | +); | |
| 3965 | +SQLITE_API int sqlite3_create_function_v2( | |
| 3966 | + sqlite3 *db, | |
| 3967 | + const char *zFunctionName, | |
| 3968 | + int nArg, | |
| 3969 | + int eTextRep, | |
| 3970 | + void *pApp, | |
| 3971 | + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), | |
| 3972 | + void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |
| 3973 | + void (*xFinal)(sqlite3_context*), | |
| 3974 | + void(*xDestroy)(void*) | |
| 3896 | 3975 | ); |
| 3897 | 3976 | |
| 3898 | 3977 | /* |
| 3899 | 3978 | ** CAPI3REF: Text Encodings |
| 3900 | 3979 | ** |
| @@ -4238,73 +4317,97 @@ | ||
| 4238 | 4317 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 4239 | 4318 | |
| 4240 | 4319 | /* |
| 4241 | 4320 | ** CAPI3REF: Define New Collating Sequences |
| 4242 | 4321 | ** |
| 4243 | -** These functions are used to add new collation sequences to the | |
| 4244 | -** [database connection] specified as the first argument. | |
| 4322 | +** ^These functions add, remove, or modify a [collation] associated | |
| 4323 | +** with the [database connection] specified as the first argument. | |
| 4245 | 4324 | ** |
| 4246 | -** ^The name of the new collation sequence is specified as a UTF-8 string | |
| 4325 | +** ^The name of the collation is a UTF-8 string | |
| 4247 | 4326 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 4248 | -** and a UTF-16 string for sqlite3_create_collation16(). ^In all cases | |
| 4249 | -** the name is passed as the second function argument. | |
| 4250 | -** | |
| 4251 | -** ^The third argument may be one of the constants [SQLITE_UTF8], | |
| 4252 | -** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied | |
| 4253 | -** routine expects to be passed pointers to strings encoded using UTF-8, | |
| 4254 | -** UTF-16 little-endian, or UTF-16 big-endian, respectively. ^The | |
| 4255 | -** third argument might also be [SQLITE_UTF16] to indicate that the routine | |
| 4256 | -** expects pointers to be UTF-16 strings in the native byte order, or the | |
| 4257 | -** argument can be [SQLITE_UTF16_ALIGNED] if the | |
| 4258 | -** the routine expects pointers to 16-bit word aligned strings | |
| 4259 | -** of UTF-16 in the native byte order. | |
| 4260 | -** | |
| 4261 | -** A pointer to the user supplied routine must be passed as the fifth | |
| 4262 | -** argument. ^If it is NULL, this is the same as deleting the collation | |
| 4263 | -** sequence (so that SQLite cannot call it any more). | |
| 4264 | -** ^Each time the application supplied function is invoked, it is passed | |
| 4265 | -** as its first parameter a copy of the void* passed as the fourth argument | |
| 4266 | -** to sqlite3_create_collation() or sqlite3_create_collation16(). | |
| 4267 | -** | |
| 4268 | -** ^The remaining arguments to the application-supplied routine are two strings, | |
| 4269 | -** each represented by a (length, data) pair and encoded in the encoding | |
| 4270 | -** that was passed as the third argument when the collation sequence was | |
| 4271 | -** registered. The application defined collation routine should | |
| 4272 | -** return negative, zero or positive if the first string is less than, | |
| 4273 | -** equal to, or greater than the second string. i.e. (STRING1 - STRING2). | |
| 4327 | +** and a UTF-16 string in native byte order for sqlite3_create_collation16(). | |
| 4328 | +** ^Collation names that compare equal according to [sqlite3_strnicmp()] are | |
| 4329 | +** considered to be the same name. | |
| 4330 | +** | |
| 4331 | +** ^(The third argument (eTextRep) must be one of the constants: | |
| 4332 | +** <ul> | |
| 4333 | +** <li> [SQLITE_UTF8], | |
| 4334 | +** <li> [SQLITE_UTF16LE], | |
| 4335 | +** <li> [SQLITE_UTF16BE], | |
| 4336 | +** <li> [SQLITE_UTF16], or | |
| 4337 | +** <li> [SQLITE_UTF16_ALIGNED]. | |
| 4338 | +** </ul>)^ | |
| 4339 | +** ^The eTextRep argument determines the encoding of strings passed | |
| 4340 | +** to the collating function callback, xCallback. | |
| 4341 | +** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep | |
| 4342 | +** force strings to be UTF16 with native byte order. | |
| 4343 | +** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin | |
| 4344 | +** on an even byte address. | |
| 4345 | +** | |
| 4346 | +** ^The fourth argument, pArg, is a application data pointer that is passed | |
| 4347 | +** through as the first argument to the collating function callback. | |
| 4348 | +** | |
| 4349 | +** ^The fifth argument, xCallback, is a pointer to the collating function. | |
| 4350 | +** ^Multiple collating functions can be registered using the same name but | |
| 4351 | +** with different eTextRep parameters and SQLite will use whichever | |
| 4352 | +** function requires the least amount of data transformation. | |
| 4353 | +** ^If the xCallback argument is NULL then the collating function is | |
| 4354 | +** deleted. ^When all collating functions having the same name are deleted, | |
| 4355 | +** that collation is no longer usable. | |
| 4356 | +** | |
| 4357 | +** ^The collating function callback is invoked with a copy of the pArg | |
| 4358 | +** application data pointer and with two strings in the encoding specified | |
| 4359 | +** by the eTextRep argument. The collating function must return an | |
| 4360 | +** integer that is negative, zero, or positive | |
| 4361 | +** if the first string is less than, equal to, or greater than the second, | |
| 4362 | +** respectively. A collating function must alway return the same answer | |
| 4363 | +** given the same inputs. If two or more collating functions are registered | |
| 4364 | +** to the same collation name (using different eTextRep values) then all | |
| 4365 | +** must give an equivalent answer when invoked with equivalent strings. | |
| 4366 | +** The collating function must obey the following properties for all | |
| 4367 | +** strings A, B, and C: | |
| 4368 | +** | |
| 4369 | +** <ol> | |
| 4370 | +** <li> If A==B then B==A. | |
| 4371 | +** <li> If A==B and B==C then A==C. | |
| 4372 | +** <li> If A<B THEN B>A. | |
| 4373 | +** <li> If A<B and B<C then A<C. | |
| 4374 | +** </ol> | |
| 4375 | +** | |
| 4376 | +** If a collating function fails any of the above constraints and that | |
| 4377 | +** collating function is registered and used, then the behavior of SQLite | |
| 4378 | +** is undefined. | |
| 4274 | 4379 | ** |
| 4275 | 4380 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 4276 | -** except that it takes an extra argument which is a destructor for | |
| 4277 | -** the collation. ^The destructor is called when the collation is | |
| 4278 | -** destroyed and is passed a copy of the fourth parameter void* pointer | |
| 4279 | -** of the sqlite3_create_collation_v2(). | |
| 4280 | -** ^Collations are destroyed when they are overridden by later calls to the | |
| 4281 | -** collation creation functions or when the [database connection] is closed | |
| 4282 | -** using [sqlite3_close()]. | |
| 4381 | +** with the addition that the xDestroy callback is invoked on pArg when | |
| 4382 | +** the collating function is deleted. | |
| 4383 | +** ^Collating functions are deleted when they are overridden by later | |
| 4384 | +** calls to the collation creation functions or when the | |
| 4385 | +** [database connection] is closed using [sqlite3_close()]. | |
| 4283 | 4386 | ** |
| 4284 | 4387 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 4285 | 4388 | */ |
| 4286 | 4389 | SQLITE_API int sqlite3_create_collation( |
| 4287 | 4390 | sqlite3*, |
| 4288 | 4391 | const char *zName, |
| 4289 | 4392 | int eTextRep, |
| 4290 | - void*, | |
| 4393 | + void *pArg, | |
| 4291 | 4394 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4292 | 4395 | ); |
| 4293 | 4396 | SQLITE_API int sqlite3_create_collation_v2( |
| 4294 | 4397 | sqlite3*, |
| 4295 | 4398 | const char *zName, |
| 4296 | 4399 | int eTextRep, |
| 4297 | - void*, | |
| 4400 | + void *pArg, | |
| 4298 | 4401 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 4299 | 4402 | void(*xDestroy)(void*) |
| 4300 | 4403 | ); |
| 4301 | 4404 | SQLITE_API int sqlite3_create_collation16( |
| 4302 | 4405 | sqlite3*, |
| 4303 | 4406 | const void *zName, |
| 4304 | 4407 | int eTextRep, |
| 4305 | - void*, | |
| 4408 | + void *pArg, | |
| 4306 | 4409 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4307 | 4410 | ); |
| 4308 | 4411 | |
| 4309 | 4412 | /* |
| 4310 | 4413 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -4389,20 +4492,23 @@ | ||
| 4389 | 4492 | #endif |
| 4390 | 4493 | |
| 4391 | 4494 | /* |
| 4392 | 4495 | ** CAPI3REF: Suspend Execution For A Short Time |
| 4393 | 4496 | ** |
| 4394 | -** ^The sqlite3_sleep() function causes the current thread to suspend execution | |
| 4497 | +** The sqlite3_sleep() function causes the current thread to suspend execution | |
| 4395 | 4498 | ** for at least a number of milliseconds specified in its parameter. |
| 4396 | 4499 | ** |
| 4397 | -** ^If the operating system does not support sleep requests with | |
| 4500 | +** If the operating system does not support sleep requests with | |
| 4398 | 4501 | ** millisecond time resolution, then the time will be rounded up to |
| 4399 | -** the nearest second. ^The number of milliseconds of sleep actually | |
| 4502 | +** the nearest second. The number of milliseconds of sleep actually | |
| 4400 | 4503 | ** requested from the operating system is returned. |
| 4401 | 4504 | ** |
| 4402 | 4505 | ** ^SQLite implements this interface by calling the xSleep() |
| 4403 | -** method of the default [sqlite3_vfs] object. | |
| 4506 | +** method of the default [sqlite3_vfs] object. If the xSleep() method | |
| 4507 | +** of the default VFS is not implemented correctly, or not implemented at | |
| 4508 | +** all, then the behavior of sqlite3_sleep() may deviate from the description | |
| 4509 | +** in the previous paragraphs. | |
| 4404 | 4510 | */ |
| 4405 | 4511 | SQLITE_API int sqlite3_sleep(int); |
| 4406 | 4512 | |
| 4407 | 4513 | /* |
| 4408 | 4514 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4620,44 +4726,77 @@ | ||
| 4620 | 4726 | ** of heap memory by deallocating non-essential memory allocations |
| 4621 | 4727 | ** held by the database library. Memory used to cache database |
| 4622 | 4728 | ** pages to improve performance is an example of non-essential memory. |
| 4623 | 4729 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4624 | 4730 | ** which might be more or less than the amount requested. |
| 4731 | +** ^The sqlite3_release_memory() routine is a no-op returning zero | |
| 4732 | +** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. | |
| 4625 | 4733 | */ |
| 4626 | 4734 | SQLITE_API int sqlite3_release_memory(int); |
| 4627 | 4735 | |
| 4628 | 4736 | /* |
| 4629 | 4737 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4630 | 4738 | ** |
| 4631 | -** ^The sqlite3_soft_heap_limit() interface places a "soft" limit | |
| 4632 | -** on the amount of heap memory that may be allocated by SQLite. | |
| 4633 | -** ^If an internal allocation is requested that would exceed the | |
| 4634 | -** soft heap limit, [sqlite3_release_memory()] is invoked one or | |
| 4635 | -** more times to free up some space before the allocation is performed. | |
| 4636 | -** | |
| 4637 | -** ^The limit is called "soft" because if [sqlite3_release_memory()] | |
| 4638 | -** cannot free sufficient memory to prevent the limit from being exceeded, | |
| 4639 | -** the memory is allocated anyway and the current operation proceeds. | |
| 4640 | -** | |
| 4641 | -** ^A negative or zero value for N means that there is no soft heap limit and | |
| 4642 | -** [sqlite3_release_memory()] will only be called when memory is exhausted. | |
| 4643 | -** ^The default value for the soft heap limit is zero. | |
| 4644 | -** | |
| 4645 | -** ^(SQLite makes a best effort to honor the soft heap limit. | |
| 4646 | -** But if the soft heap limit cannot be honored, execution will | |
| 4647 | -** continue without error or notification.)^ This is why the limit is | |
| 4648 | -** called a "soft" limit. It is advisory only. | |
| 4649 | -** | |
| 4650 | -** Prior to SQLite version 3.5.0, this routine only constrained the memory | |
| 4651 | -** allocated by a single thread - the same thread in which this routine | |
| 4652 | -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is | |
| 4653 | -** applied to all threads. The value specified for the soft heap limit | |
| 4654 | -** is an upper bound on the total memory allocation for all threads. In | |
| 4655 | -** version 3.5.0 there is no mechanism for limiting the heap usage for | |
| 4656 | -** individual threads. | |
| 4657 | -*/ | |
| 4658 | -SQLITE_API void sqlite3_soft_heap_limit(int); | |
| 4739 | +** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the | |
| 4740 | +** soft limit on the amount of heap memory that may be allocated by SQLite. | |
| 4741 | +** ^SQLite strives to keep heap memory utilization below the soft heap | |
| 4742 | +** limit by reducing the number of pages held in the page cache | |
| 4743 | +** as heap memory usages approaches the limit. | |
| 4744 | +** ^The soft heap limit is "soft" because even though SQLite strives to stay | |
| 4745 | +** below the limit, it will exceed the limit rather than generate | |
| 4746 | +** an [SQLITE_NOMEM] error. In other words, the soft heap limit | |
| 4747 | +** is advisory only. | |
| 4748 | +** | |
| 4749 | +** ^The return value from sqlite3_soft_heap_limit64() is the size of | |
| 4750 | +** the soft heap limit prior to the call. ^If the argument N is negative | |
| 4751 | +** then no change is made to the soft heap limit. Hence, the current | |
| 4752 | +** size of the soft heap limit can be determined by invoking | |
| 4753 | +** sqlite3_soft_heap_limit64() with a negative argument. | |
| 4754 | +** | |
| 4755 | +** ^If the argument N is zero then the soft heap limit is disabled. | |
| 4756 | +** | |
| 4757 | +** ^(The soft heap limit is not enforced in the current implementation | |
| 4758 | +** if one or more of following conditions are true: | |
| 4759 | +** | |
| 4760 | +** <ul> | |
| 4761 | +** <li> The soft heap limit is set to zero. | |
| 4762 | +** <li> Memory accounting is disabled using a combination of the | |
| 4763 | +** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and | |
| 4764 | +** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. | |
| 4765 | +** <li> An alternative page cache implementation is specifed using | |
| 4766 | +** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...). | |
| 4767 | +** <li> The page cache allocates from its own memory pool supplied | |
| 4768 | +** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than | |
| 4769 | +** from the heap. | |
| 4770 | +** </ul>)^ | |
| 4771 | +** | |
| 4772 | +** Beginning with SQLite version 3.7.3, the soft heap limit is enforced | |
| 4773 | +** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] | |
| 4774 | +** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], | |
| 4775 | +** the soft heap limit is enforced on every memory allocation. Without | |
| 4776 | +** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced | |
| 4777 | +** when memory is allocated by the page cache. Testing suggests that because | |
| 4778 | +** the page cache is the predominate memory user in SQLite, most | |
| 4779 | +** applications will achieve adequate soft heap limit enforcement without | |
| 4780 | +** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. | |
| 4781 | +** | |
| 4782 | +** The circumstances under which SQLite will enforce the soft heap limit may | |
| 4783 | +** changes in future releases of SQLite. | |
| 4784 | +*/ | |
| 4785 | +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); | |
| 4786 | + | |
| 4787 | +/* | |
| 4788 | +** CAPI3REF: Deprecated Soft Heap Limit Interface | |
| 4789 | +** DEPRECATED | |
| 4790 | +** | |
| 4791 | +** This is a deprecated version of the [sqlite3_soft_heap_limit64()] | |
| 4792 | +** interface. This routine is provided for historical compatibility | |
| 4793 | +** only. All new applications should use the | |
| 4794 | +** [sqlite3_soft_heap_limit64()] interface rather than this one. | |
| 4795 | +*/ | |
| 4796 | +SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); | |
| 4797 | + | |
| 4659 | 4798 | |
| 4660 | 4799 | /* |
| 4661 | 4800 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4662 | 4801 | ** |
| 4663 | 4802 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4777,38 +4916,51 @@ | ||
| 4777 | 4916 | ** it back off again. |
| 4778 | 4917 | */ |
| 4779 | 4918 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4780 | 4919 | |
| 4781 | 4920 | /* |
| 4782 | -** CAPI3REF: Automatically Load An Extensions | |
| 4783 | -** | |
| 4784 | -** ^This API can be invoked at program startup in order to register | |
| 4785 | -** one or more statically linked extensions that will be available | |
| 4786 | -** to all new [database connections]. | |
| 4787 | -** | |
| 4788 | -** ^(This routine stores a pointer to the extension entry point | |
| 4789 | -** in an array that is obtained from [sqlite3_malloc()]. That memory | |
| 4790 | -** is deallocated by [sqlite3_reset_auto_extension()].)^ | |
| 4791 | -** | |
| 4792 | -** ^This function registers an extension entry point that is | |
| 4793 | -** automatically invoked whenever a new [database connection] | |
| 4794 | -** is opened using [sqlite3_open()], [sqlite3_open16()], | |
| 4795 | -** or [sqlite3_open_v2()]. | |
| 4796 | -** ^Duplicate extensions are detected so calling this routine | |
| 4797 | -** multiple times with the same extension is harmless. | |
| 4798 | -** ^Automatic extensions apply across all threads. | |
| 4921 | +** CAPI3REF: Automatically Load Statically Linked Extensions | |
| 4922 | +** | |
| 4923 | +** ^This interface causes the xEntryPoint() function to be invoked for | |
| 4924 | +** each new [database connection] that is created. The idea here is that | |
| 4925 | +** xEntryPoint() is the entry point for a statically linked SQLite extension | |
| 4926 | +** that is to be automatically loaded into all new database connections. | |
| 4927 | +** | |
| 4928 | +** ^(Even though the function prototype shows that xEntryPoint() takes | |
| 4929 | +** no arguments and returns void, SQLite invokes xEntryPoint() with three | |
| 4930 | +** arguments and expects and integer result as if the signature of the | |
| 4931 | +** entry point where as follows: | |
| 4932 | +** | |
| 4933 | +** <blockquote><pre> | |
| 4934 | +** int xEntryPoint( | |
| 4935 | +** sqlite3 *db, | |
| 4936 | +** const char **pzErrMsg, | |
| 4937 | +** const struct sqlite3_api_routines *pThunk | |
| 4938 | +** ); | |
| 4939 | +** </pre></blockquote>)^ | |
| 4940 | +** | |
| 4941 | +** If the xEntryPoint routine encounters an error, it should make *pzErrMsg | |
| 4942 | +** point to an appropriate error message (obtained from [sqlite3_mprintf()]) | |
| 4943 | +** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg | |
| 4944 | +** is NULL before calling the xEntryPoint(). ^SQLite will invoke | |
| 4945 | +** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any | |
| 4946 | +** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], | |
| 4947 | +** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. | |
| 4948 | +** | |
| 4949 | +** ^Calling sqlite3_auto_extension(X) with an entry point X that is already | |
| 4950 | +** on the list of automatic extensions is a harmless no-op. ^No entry point | |
| 4951 | +** will be called more than once for each database connection that is opened. | |
| 4952 | +** | |
| 4953 | +** See also: [sqlite3_reset_auto_extension()]. | |
| 4799 | 4954 | */ |
| 4800 | 4955 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4801 | 4956 | |
| 4802 | 4957 | /* |
| 4803 | 4958 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4804 | 4959 | ** |
| 4805 | -** ^(This function disables all previously registered automatic | |
| 4806 | -** extensions. It undoes the effect of all prior | |
| 4807 | -** [sqlite3_auto_extension()] calls.)^ | |
| 4808 | -** | |
| 4809 | -** ^This function disables automatic extensions in all threads. | |
| 4960 | +** ^This interface disables all automatic extensions previously | |
| 4961 | +** registered using [sqlite3_auto_extension()]. | |
| 4810 | 4962 | */ |
| 4811 | 4963 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4812 | 4964 | |
| 4813 | 4965 | /* |
| 4814 | 4966 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -5443,11 +5595,11 @@ | ||
| 5443 | 5595 | ** output variable when querying the system for the current mutex |
| 5444 | 5596 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 5445 | 5597 | ** |
| 5446 | 5598 | ** ^The xMutexInit method defined by this structure is invoked as |
| 5447 | 5599 | ** part of system initialization by the sqlite3_initialize() function. |
| 5448 | -** ^The xMutexInit routine is calle by SQLite exactly once for each | |
| 5600 | +** ^The xMutexInit routine is called by SQLite exactly once for each | |
| 5449 | 5601 | ** effective call to [sqlite3_initialize()]. |
| 5450 | 5602 | ** |
| 5451 | 5603 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 5452 | 5604 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 5453 | 5605 | ** implementation of this method is expected to release all outstanding |
| @@ -5640,11 +5792,12 @@ | ||
| 5640 | 5792 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5641 | 5793 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5642 | 5794 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5643 | 5795 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5644 | 5796 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5645 | -#define SQLITE_TESTCTRL_LAST 17 | |
| 5797 | +#define SQLITE_TESTCTRL_SCRATCHMALLOC 18 | |
| 5798 | +#define SQLITE_TESTCTRL_LAST 18 | |
| 5646 | 5799 | |
| 5647 | 5800 | /* |
| 5648 | 5801 | ** CAPI3REF: SQLite Runtime Status |
| 5649 | 5802 | ** |
| 5650 | 5803 | ** ^This interface is used to retrieve runtime status information |
| @@ -5659,11 +5812,11 @@ | ||
| 5659 | 5812 | ** value. For those parameters |
| 5660 | 5813 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5661 | 5814 | ** ^(Other parameters record only the highwater mark and not the current |
| 5662 | 5815 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5663 | 5816 | ** |
| 5664 | -** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a | |
| 5817 | +** ^The sqlite3_status() routine returns SQLITE_OK on success and a | |
| 5665 | 5818 | ** non-zero [error code] on failure. |
| 5666 | 5819 | ** |
| 5667 | 5820 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5668 | 5821 | ** called while other threads are running the same or different SQLite |
| 5669 | 5822 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5709,11 +5862,11 @@ | ||
| 5709 | 5862 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5710 | 5863 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5711 | 5864 | ** |
| 5712 | 5865 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5713 | 5866 | ** <dd>This parameter returns the number of bytes of page cache |
| 5714 | -** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE] | |
| 5867 | +** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] | |
| 5715 | 5868 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5716 | 5869 | ** returned value includes allocations that overflowed because they |
| 5717 | 5870 | ** where too large (they were larger than the "sz" parameter to |
| 5718 | 5871 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5719 | 5872 | ** no space was left in the page cache.</dd>)^ |
| @@ -5732,11 +5885,11 @@ | ||
| 5732 | 5885 | ** outstanding at time, this parameter also reports the number of threads |
| 5733 | 5886 | ** using scratch memory at the same time.</dd>)^ |
| 5734 | 5887 | ** |
| 5735 | 5888 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5736 | 5889 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5737 | -** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH] | |
| 5890 | +** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] | |
| 5738 | 5891 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5739 | 5892 | ** returned include overflows because the requested allocation was too |
| 5740 | 5893 | ** larger (that is, because the requested allocation was larger than the |
| 5741 | 5894 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5742 | 5895 | ** slots were available. |
| @@ -5780,10 +5933,13 @@ | ||
| 5780 | 5933 | ** |
| 5781 | 5934 | ** ^The current value of the requested parameter is written into *pCur |
| 5782 | 5935 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5783 | 5936 | ** the resetFlg is true, then the highest instantaneous value is |
| 5784 | 5937 | ** reset back down to the current value. |
| 5938 | +** | |
| 5939 | +** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a | |
| 5940 | +** non-zero [error code] on failure. | |
| 5785 | 5941 | ** |
| 5786 | 5942 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5787 | 5943 | */ |
| 5788 | 5944 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5789 | 5945 | |
| @@ -5907,122 +6063,134 @@ | ||
| 5907 | 6063 | ** CAPI3REF: Application Defined Page Cache. |
| 5908 | 6064 | ** KEYWORDS: {page cache} |
| 5909 | 6065 | ** |
| 5910 | 6066 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5911 | 6067 | ** register an alternative page cache implementation by passing in an |
| 5912 | -** instance of the sqlite3_pcache_methods structure.)^ The majority of the | |
| 5913 | -** heap memory used by SQLite is used by the page cache to cache data read | |
| 5914 | -** from, or ready to be written to, the database file. By implementing a | |
| 5915 | -** custom page cache using this API, an application can control more | |
| 5916 | -** precisely the amount of memory consumed by SQLite, the way in which | |
| 6068 | +** instance of the sqlite3_pcache_methods structure.)^ | |
| 6069 | +** In many applications, most of the heap memory allocated by | |
| 6070 | +** SQLite is used for the page cache. | |
| 6071 | +** By implementing a | |
| 6072 | +** custom page cache using this API, an application can better control | |
| 6073 | +** the amount of memory consumed by SQLite, the way in which | |
| 5917 | 6074 | ** that memory is allocated and released, and the policies used to |
| 5918 | 6075 | ** determine exactly which parts of a database file are cached and for |
| 5919 | 6076 | ** how long. |
| 5920 | 6077 | ** |
| 6078 | +** The alternative page cache mechanism is an | |
| 6079 | +** extreme measure that is only needed by the most demanding applications. | |
| 6080 | +** The built-in page cache is recommended for most uses. | |
| 6081 | +** | |
| 5921 | 6082 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 5922 | 6083 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 5923 | 6084 | ** the application may discard the parameter after the call to |
| 5924 | 6085 | ** [sqlite3_config()] returns.)^ |
| 5925 | 6086 | ** |
| 5926 | -** ^The xInit() method is called once for each call to [sqlite3_initialize()] | |
| 6087 | +** ^(The xInit() method is called once for each effective | |
| 6088 | +** call to [sqlite3_initialize()])^ | |
| 5927 | 6089 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 5928 | 6090 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 5929 | -** ^The xInit() method can set up up global structures and/or any mutexes | |
| 6091 | +** The intent of the xInit() method is to set up global data structures | |
| 5930 | 6092 | ** required by the custom page cache implementation. |
| 6093 | +** ^(If the xInit() method is NULL, then the | |
| 6094 | +** built-in default page cache is used instead of the application defined | |
| 6095 | +** page cache.)^ | |
| 5931 | 6096 | ** |
| 5932 | -** ^The xShutdown() method is called from within [sqlite3_shutdown()], | |
| 5933 | -** if the application invokes this API. It can be used to clean up | |
| 6097 | +** ^The xShutdown() method is called by [sqlite3_shutdown()]. | |
| 6098 | +** It can be used to clean up | |
| 5934 | 6099 | ** any outstanding resources before process shutdown, if required. |
| 6100 | +** ^The xShutdown() method may be NULL. | |
| 5935 | 6101 | ** |
| 5936 | -** ^SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes | |
| 5937 | -** the xInit method, so the xInit method need not be threadsafe. ^The | |
| 6102 | +** ^SQLite automatically serializes calls to the xInit method, | |
| 6103 | +** so the xInit method need not be threadsafe. ^The | |
| 5938 | 6104 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 5939 | 6105 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 5940 | 6106 | ** in multithreaded applications. |
| 5941 | 6107 | ** |
| 5942 | 6108 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 5943 | 6109 | ** call to xShutdown(). |
| 5944 | 6110 | ** |
| 5945 | -** ^The xCreate() method is used to construct a new cache instance. SQLite | |
| 5946 | -** will typically create one cache instance for each open database file, | |
| 6111 | +** ^SQLite invokes the xCreate() method to construct a new cache instance. | |
| 6112 | +** SQLite will typically create one cache instance for each open database file, | |
| 5947 | 6113 | ** though this is not guaranteed. ^The |
| 5948 | 6114 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 5949 | 6115 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 5950 | 6116 | ** will the page size of the database file that is to be cached plus an |
| 5951 | -** increment (here called "R") of about 100 or 200. ^SQLite will use the | |
| 6117 | +** increment (here called "R") of about 100 or 200. SQLite will use the | |
| 5952 | 6118 | ** extra R bytes on each page to store metadata about the underlying |
| 5953 | 6119 | ** database page on disk. The value of R depends |
| 5954 | 6120 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 5955 | 6121 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 5956 | 6122 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 5957 | 6123 | ** be used to cache database pages of a file stored on disk, or |
| 5958 | -** false if it is used for an in-memory database. ^The cache implementation | |
| 6124 | +** false if it is used for an in-memory database. The cache implementation | |
| 5959 | 6125 | ** does not have to do anything special based with the value of bPurgeable; |
| 5960 | 6126 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 5961 | 6127 | ** never invoke xUnpin() except to deliberately delete a page. |
| 5962 | -** ^In other words, a cache created with bPurgeable set to false will | |
| 6128 | +** ^In other words, calls to xUnpin() on a cache with bPurgeable set to | |
| 6129 | +** false will always have the "discard" flag set to true. | |
| 6130 | +** ^Hence, a cache created with bPurgeable false will | |
| 5963 | 6131 | ** never contain any unpinned pages. |
| 5964 | 6132 | ** |
| 5965 | 6133 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 5966 | 6134 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 5967 | 6135 | ** instance passed as the first argument. This is the value configured using |
| 5968 | -** the SQLite "[PRAGMA cache_size]" command.)^ ^As with the bPurgeable | |
| 6136 | +** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable | |
| 5969 | 6137 | ** parameter, the implementation is not required to do anything with this |
| 5970 | 6138 | ** value; it is advisory only. |
| 5971 | 6139 | ** |
| 5972 | -** ^The xPagecount() method should return the number of pages currently | |
| 5973 | -** stored in the cache. | |
| 6140 | +** The xPagecount() method must return the number of pages currently | |
| 6141 | +** stored in the cache, both pinned and unpinned. | |
| 5974 | 6142 | ** |
| 5975 | -** ^The xFetch() method is used to fetch a page and return a pointer to it. | |
| 5976 | -** ^A 'page', in this context, is a buffer of szPage bytes aligned at an | |
| 5977 | -** 8-byte boundary. ^The page to be fetched is determined by the key. ^The | |
| 5978 | -** mimimum key value is 1. After it has been retrieved using xFetch, the page | |
| 6143 | +** The xFetch() method locates a page in the cache and returns a pointer to | |
| 6144 | +** the page, or a NULL pointer. | |
| 6145 | +** A "page", in this context, means a buffer of szPage bytes aligned at an | |
| 6146 | +** 8-byte boundary. The page to be fetched is determined by the key. ^The | |
| 6147 | +** mimimum key value is 1. After it has been retrieved using xFetch, the page | |
| 5979 | 6148 | ** is considered to be "pinned". |
| 5980 | 6149 | ** |
| 5981 | -** ^If the requested page is already in the page cache, then the page cache | |
| 6150 | +** If the requested page is already in the page cache, then the page cache | |
| 5982 | 6151 | ** implementation must return a pointer to the page buffer with its content |
| 5983 | -** intact. ^(If the requested page is not already in the cache, then the | |
| 5984 | -** behavior of the cache implementation is determined by the value of the | |
| 5985 | -** createFlag parameter passed to xFetch, according to the following table: | |
| 6152 | +** intact. If the requested page is not already in the cache, then the | |
| 6153 | +** behavior of the cache implementation should use the value of the createFlag | |
| 6154 | +** parameter to help it determined what action to take: | |
| 5986 | 6155 | ** |
| 5987 | 6156 | ** <table border=1 width=85% align=center> |
| 5988 | 6157 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 5989 | 6158 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 5990 | 6159 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 5991 | 6160 | ** Otherwise return NULL. |
| 5992 | 6161 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 5993 | 6162 | ** NULL if allocating a new page is effectively impossible. |
| 5994 | -** </table>)^ | |
| 6163 | +** </table> | |
| 5995 | 6164 | ** |
| 5996 | -** SQLite will normally invoke xFetch() with a createFlag of 0 or 1. If | |
| 5997 | -** a call to xFetch() with createFlag==1 returns NULL, then SQLite will | |
| 6165 | +** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite | |
| 6166 | +** will only use a createFlag of 2 after a prior call with a createFlag of 1 | |
| 6167 | +** failed.)^ In between the to xFetch() calls, SQLite may | |
| 5998 | 6168 | ** attempt to unpin one or more cache pages by spilling the content of |
| 5999 | -** pinned pages to disk and synching the operating system disk cache. After | |
| 6000 | -** attempting to unpin pages, the xFetch() method will be invoked again with | |
| 6001 | -** a createFlag of 2. | |
| 6169 | +** pinned pages to disk and synching the operating system disk cache. | |
| 6002 | 6170 | ** |
| 6003 | 6171 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 6004 | -** as its second argument. ^(If the third parameter, discard, is non-zero, | |
| 6005 | -** then the page should be evicted from the cache. In this case SQLite | |
| 6006 | -** assumes that the next time the page is retrieved from the cache using | |
| 6007 | -** the xFetch() method, it will be zeroed.)^ ^If the discard parameter is | |
| 6008 | -** zero, then the page is considered to be unpinned. ^The cache implementation | |
| 6172 | +** as its second argument. If the third parameter, discard, is non-zero, | |
| 6173 | +** then the page must be evicted from the cache. | |
| 6174 | +** ^If the discard parameter is | |
| 6175 | +** zero, then the page may be discarded or retained at the discretion of | |
| 6176 | +** page cache implementation. ^The page cache implementation | |
| 6009 | 6177 | ** may choose to evict unpinned pages at any time. |
| 6010 | 6178 | ** |
| 6011 | -** ^(The cache is not required to perform any reference counting. A single | |
| 6179 | +** The cache must not perform any reference counting. A single | |
| 6012 | 6180 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 6013 | -** to xFetch().)^ | |
| 6181 | +** to xFetch(). | |
| 6014 | 6182 | ** |
| 6015 | -** ^The xRekey() method is used to change the key value associated with the | |
| 6016 | -** page passed as the second argument from oldKey to newKey. ^If the cache | |
| 6017 | -** previously contains an entry associated with newKey, it should be | |
| 6183 | +** The xRekey() method is used to change the key value associated with the | |
| 6184 | +** page passed as the second argument. If the cache | |
| 6185 | +** previously contains an entry associated with newKey, it must be | |
| 6018 | 6186 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 6019 | 6187 | ** to be pinned. |
| 6020 | 6188 | ** |
| 6021 | -** ^When SQLite calls the xTruncate() method, the cache must discard all | |
| 6189 | +** When SQLite calls the xTruncate() method, the cache must discard all | |
| 6022 | 6190 | ** existing cache entries with page numbers (keys) greater than or equal |
| 6023 | -** to the value of the iLimit parameter passed to xTruncate(). ^If any | |
| 6191 | +** to the value of the iLimit parameter passed to xTruncate(). If any | |
| 6024 | 6192 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 6025 | 6193 | ** they can be safely discarded. |
| 6026 | 6194 | ** |
| 6027 | 6195 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 6028 | 6196 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -6496,10 +6664,66 @@ | ||
| 6496 | 6664 | #if 0 |
| 6497 | 6665 | } /* End of the 'extern "C"' block */ |
| 6498 | 6666 | #endif |
| 6499 | 6667 | #endif |
| 6500 | 6668 | |
| 6669 | +/* | |
| 6670 | +** 2010 August 30 | |
| 6671 | +** | |
| 6672 | +** The author disclaims copyright to this source code. In place of | |
| 6673 | +** a legal notice, here is a blessing: | |
| 6674 | +** | |
| 6675 | +** May you do good and not evil. | |
| 6676 | +** May you find forgiveness for yourself and forgive others. | |
| 6677 | +** May you share freely, never taking more than you give. | |
| 6678 | +** | |
| 6679 | +************************************************************************* | |
| 6680 | +*/ | |
| 6681 | + | |
| 6682 | +#ifndef _SQLITE3RTREE_H_ | |
| 6683 | +#define _SQLITE3RTREE_H_ | |
| 6684 | + | |
| 6685 | + | |
| 6686 | +#if 0 | |
| 6687 | +extern "C" { | |
| 6688 | +#endif | |
| 6689 | + | |
| 6690 | +typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; | |
| 6691 | + | |
| 6692 | +/* | |
| 6693 | +** Register a geometry callback named zGeom that can be used as part of an | |
| 6694 | +** R-Tree geometry query as follows: | |
| 6695 | +** | |
| 6696 | +** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...) | |
| 6697 | +*/ | |
| 6698 | +SQLITE_API int sqlite3_rtree_geometry_callback( | |
| 6699 | + sqlite3 *db, | |
| 6700 | + const char *zGeom, | |
| 6701 | + int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), | |
| 6702 | + void *pContext | |
| 6703 | +); | |
| 6704 | + | |
| 6705 | + | |
| 6706 | +/* | |
| 6707 | +** A pointer to a structure of the following type is passed as the first | |
| 6708 | +** argument to callbacks registered using rtree_geometry_callback(). | |
| 6709 | +*/ | |
| 6710 | +struct sqlite3_rtree_geometry { | |
| 6711 | + void *pContext; /* Copy of pContext passed to s_r_g_c() */ | |
| 6712 | + int nParam; /* Size of array aParam[] */ | |
| 6713 | + double *aParam; /* Parameters passed to SQL geom function */ | |
| 6714 | + void *pUser; /* Callback implementation user data */ | |
| 6715 | + void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ | |
| 6716 | +}; | |
| 6717 | + | |
| 6718 | + | |
| 6719 | +#if 0 | |
| 6720 | +} /* end of the 'extern "C"' block */ | |
| 6721 | +#endif | |
| 6722 | + | |
| 6723 | +#endif /* ifndef _SQLITE3RTREE_H_ */ | |
| 6724 | + | |
| 6501 | 6725 | |
| 6502 | 6726 | /************** End of sqlite3.h *********************************************/ |
| 6503 | 6727 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 6504 | 6728 | /************** Include hash.h in the middle of sqliteInt.h ******************/ |
| 6505 | 6729 | /************** Begin file hash.h ********************************************/ |
| @@ -7066,10 +7290,11 @@ | ||
| 7066 | 7290 | typedef struct Schema Schema; |
| 7067 | 7291 | typedef struct Expr Expr; |
| 7068 | 7292 | typedef struct ExprList ExprList; |
| 7069 | 7293 | typedef struct ExprSpan ExprSpan; |
| 7070 | 7294 | typedef struct FKey FKey; |
| 7295 | +typedef struct FuncDestructor FuncDestructor; | |
| 7071 | 7296 | typedef struct FuncDef FuncDef; |
| 7072 | 7297 | typedef struct FuncDefHash FuncDefHash; |
| 7073 | 7298 | typedef struct IdList IdList; |
| 7074 | 7299 | typedef struct Index Index; |
| 7075 | 7300 | typedef struct IndexSample IndexSample; |
| @@ -7172,16 +7397,15 @@ | ||
| 7172 | 7397 | ** following values. |
| 7173 | 7398 | ** |
| 7174 | 7399 | ** NOTE: These values must match the corresponding PAGER_ values in |
| 7175 | 7400 | ** pager.h. |
| 7176 | 7401 | */ |
| 7177 | -#define BTREE_OMIT_JOURNAL 1 /* Do not use journal. No argument */ | |
| 7402 | +#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ | |
| 7178 | 7403 | #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ |
| 7179 | -#define BTREE_MEMORY 4 /* In-memory DB. No argument */ | |
| 7180 | -#define BTREE_READONLY 8 /* Open the database in read-only mode */ | |
| 7181 | -#define BTREE_READWRITE 16 /* Open for both reading and writing */ | |
| 7182 | -#define BTREE_CREATE 32 /* Create the database if it does not exist */ | |
| 7404 | +#define BTREE_MEMORY 4 /* This is an in-memory DB */ | |
| 7405 | +#define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */ | |
| 7406 | +#define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */ | |
| 7183 | 7407 | |
| 7184 | 7408 | SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); |
| 7185 | 7409 | SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); |
| 7186 | 7410 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int); |
| 7187 | 7411 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| @@ -7213,15 +7437,21 @@ | ||
| 7213 | 7437 | SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *); |
| 7214 | 7438 | |
| 7215 | 7439 | SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *); |
| 7216 | 7440 | |
| 7217 | 7441 | /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR |
| 7218 | -** of the following flags: | |
| 7442 | +** of the flags shown below. | |
| 7443 | +** | |
| 7444 | +** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. | |
| 7445 | +** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data | |
| 7446 | +** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With | |
| 7447 | +** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored | |
| 7448 | +** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL | |
| 7449 | +** indices.) | |
| 7219 | 7450 | */ |
| 7220 | 7451 | #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ |
| 7221 | -#define BTREE_ZERODATA 2 /* Table has keys only - no data */ | |
| 7222 | -#define BTREE_LEAFDATA 4 /* Data stored in leaves only. Implies INTKEY */ | |
| 7452 | +#define BTREE_BLOBKEY 2 /* Table has keys only - no data */ | |
| 7223 | 7453 | |
| 7224 | 7454 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); |
| 7225 | 7455 | SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); |
| 7226 | 7456 | SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); |
| 7227 | 7457 | |
| @@ -7838,10 +8068,11 @@ | ||
| 7838 | 8068 | ** |
| 7839 | 8069 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| 7840 | 8070 | */ |
| 7841 | 8071 | #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ |
| 7842 | 8072 | #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ |
| 8073 | +#define PAGER_MEMORY 0x0004 /* In-memory database */ | |
| 7843 | 8074 | |
| 7844 | 8075 | /* |
| 7845 | 8076 | ** Valid values for the second argument to sqlite3PagerLockingMode(). |
| 7846 | 8077 | */ |
| 7847 | 8078 | #define PAGER_LOCKINGMODE_QUERY -1 |
| @@ -8472,12 +8703,12 @@ | ||
| 8472 | 8703 | #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) |
| 8473 | 8704 | #define sqlite3_mutex_free(X) |
| 8474 | 8705 | #define sqlite3_mutex_enter(X) |
| 8475 | 8706 | #define sqlite3_mutex_try(X) SQLITE_OK |
| 8476 | 8707 | #define sqlite3_mutex_leave(X) |
| 8477 | -#define sqlite3_mutex_held(X) 1 | |
| 8478 | -#define sqlite3_mutex_notheld(X) 1 | |
| 8708 | +#define sqlite3_mutex_held(X) ((void)(X),1) | |
| 8709 | +#define sqlite3_mutex_notheld(X) ((void)(X),1) | |
| 8479 | 8710 | #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) |
| 8480 | 8711 | #define sqlite3MutexInit() SQLITE_OK |
| 8481 | 8712 | #define sqlite3MutexEnd() |
| 8482 | 8713 | #endif /* defined(SQLITE_MUTEX_OMIT) */ |
| 8483 | 8714 | |
| @@ -8795,10 +9026,31 @@ | ||
| 8795 | 9026 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ |
| 8796 | 9027 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ |
| 8797 | 9028 | void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ |
| 8798 | 9029 | char *zName; /* SQL name of the function. */ |
| 8799 | 9030 | FuncDef *pHash; /* Next with a different name but the same hash */ |
| 9031 | + FuncDestructor *pDestructor; /* Reference counted destructor function */ | |
| 9032 | +}; | |
| 9033 | + | |
| 9034 | +/* | |
| 9035 | +** This structure encapsulates a user-function destructor callback (as | |
| 9036 | +** configured using create_function_v2()) and a reference counter. When | |
| 9037 | +** create_function_v2() is called to create a function with a destructor, | |
| 9038 | +** a single object of this type is allocated. FuncDestructor.nRef is set to | |
| 9039 | +** the number of FuncDef objects created (either 1 or 3, depending on whether | |
| 9040 | +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor | |
| 9041 | +** member of each of the new FuncDef objects is set to point to the allocated | |
| 9042 | +** FuncDestructor. | |
| 9043 | +** | |
| 9044 | +** Thereafter, when one of the FuncDef objects is deleted, the reference | |
| 9045 | +** count on this object is decremented. When it reaches 0, the destructor | |
| 9046 | +** is invoked and the FuncDestructor structure freed. | |
| 9047 | +*/ | |
| 9048 | +struct FuncDestructor { | |
| 9049 | + int nRef; | |
| 9050 | + void (*xDestroy)(void *); | |
| 9051 | + void *pUserData; | |
| 8800 | 9052 | }; |
| 8801 | 9053 | |
| 8802 | 9054 | /* |
| 8803 | 9055 | ** Possible values for FuncDef.flags |
| 8804 | 9056 | */ |
| @@ -8835,19 +9087,19 @@ | ||
| 8835 | 9087 | ** FuncDef.flags variable is set to the value passed as the flags |
| 8836 | 9088 | ** parameter. |
| 8837 | 9089 | */ |
| 8838 | 9090 | #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ |
| 8839 | 9091 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 8840 | - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0} | |
| 9092 | + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} | |
| 8841 | 9093 | #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ |
| 8842 | 9094 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 8843 | - pArg, 0, xFunc, 0, 0, #zName, 0} | |
| 9095 | + pArg, 0, xFunc, 0, 0, #zName, 0, 0} | |
| 8844 | 9096 | #define LIKEFUNC(zName, nArg, arg, flags) \ |
| 8845 | - {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0} | |
| 9097 | + {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0} | |
| 8846 | 9098 | #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ |
| 8847 | 9099 | {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \ |
| 8848 | - SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0} | |
| 9100 | + SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} | |
| 8849 | 9101 | |
| 8850 | 9102 | /* |
| 8851 | 9103 | ** All current savepoints are stored in a linked list starting at |
| 8852 | 9104 | ** sqlite3.pSavepoint. The first element in the list is the most recently |
| 8853 | 9105 | ** opened savepoint. Savepoints are added to the list by the vdbe |
| @@ -9063,10 +9315,11 @@ | ||
| 9063 | 9315 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9064 | 9316 | int nCol; /* Number of columns in this table */ |
| 9065 | 9317 | Column *aCol; /* Information about each column */ |
| 9066 | 9318 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9067 | 9319 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9320 | + unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ | |
| 9068 | 9321 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9069 | 9322 | u16 nRef; /* Number of pointers to this Table */ |
| 9070 | 9323 | u8 tabFlags; /* Mask of TF_* values */ |
| 9071 | 9324 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9072 | 9325 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| @@ -10341,11 +10594,11 @@ | ||
| 10341 | 10594 | SQLITE_PRIVATE void sqlite3ScratchFree(void*); |
| 10342 | 10595 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 10343 | 10596 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 10344 | 10597 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| 10345 | 10598 | SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); |
| 10346 | -SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64); | |
| 10599 | +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); | |
| 10347 | 10600 | |
| 10348 | 10601 | /* |
| 10349 | 10602 | ** On systems with ample stack space and that support alloca(), make |
| 10350 | 10603 | ** use of alloca() to obtain space for large automatic objects. By default, |
| 10351 | 10604 | ** obtain space from malloc(). |
| @@ -10512,11 +10765,10 @@ | ||
| 10512 | 10765 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| 10513 | 10766 | SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int); |
| 10514 | 10767 | SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int); |
| 10515 | 10768 | SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); |
| 10516 | 10769 | SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); |
| 10517 | -SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse*,int,int); | |
| 10518 | 10770 | SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int); |
| 10519 | 10771 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); |
| 10520 | 10772 | SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); |
| 10521 | 10773 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 10522 | 10774 | SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*); |
| @@ -10632,12 +10884,10 @@ | ||
| 10632 | 10884 | # define sqlite3AuthContextPush(a,b,c) |
| 10633 | 10885 | # define sqlite3AuthContextPop(a) ((void)(a)) |
| 10634 | 10886 | #endif |
| 10635 | 10887 | SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*); |
| 10636 | 10888 | SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*); |
| 10637 | -SQLITE_PRIVATE int sqlite3BtreeFactory(sqlite3 *db, const char *zFilename, | |
| 10638 | - int omitJournal, int nCache, int flags, Btree **ppBtree); | |
| 10639 | 10889 | SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); |
| 10640 | 10890 | SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); |
| 10641 | 10891 | SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); |
| 10642 | 10892 | SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); |
| 10643 | 10893 | SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*); |
| @@ -10759,11 +11009,13 @@ | ||
| 10759 | 11009 | SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *); |
| 10760 | 11010 | SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *); |
| 10761 | 11011 | SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *); |
| 10762 | 11012 | SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, |
| 10763 | 11013 | void (*)(sqlite3_context*,int,sqlite3_value **), |
| 10764 | - void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*)); | |
| 11014 | + void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), | |
| 11015 | + FuncDestructor *pDestructor | |
| 11016 | +); | |
| 10765 | 11017 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 10766 | 11018 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 10767 | 11019 | |
| 10768 | 11020 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 10769 | 11021 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| @@ -11679,10 +11931,11 @@ | ||
| 11679 | 11931 | Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 11680 | 11932 | Bool nullRow; /* True if pointing to a row with no data */ |
| 11681 | 11933 | Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 11682 | 11934 | Bool isTable; /* True if a table requiring integer keys */ |
| 11683 | 11935 | Bool isIndex; /* True if an index containing keys only - no data */ |
| 11936 | + Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */ | |
| 11684 | 11937 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 11685 | 11938 | Btree *pBt; /* Separate file holding temporary table */ |
| 11686 | 11939 | int pseudoTableReg; /* Register holding pseudotable content. */ |
| 11687 | 11940 | KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 11688 | 11941 | int nField; /* Number of fields in the header */ |
| @@ -11773,10 +12026,14 @@ | ||
| 11773 | 12026 | char *z; /* String or BLOB value */ |
| 11774 | 12027 | int n; /* Number of characters in string value, excluding '\0' */ |
| 11775 | 12028 | u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 11776 | 12029 | u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ |
| 11777 | 12030 | u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 12031 | +#ifdef SQLITE_DEBUG | |
| 12032 | + Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ | |
| 12033 | + void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ | |
| 12034 | +#endif | |
| 11778 | 12035 | void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ |
| 11779 | 12036 | char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ |
| 11780 | 12037 | }; |
| 11781 | 12038 | |
| 11782 | 12039 | /* One or more of the following flags are set to indicate the validOK |
| @@ -11799,10 +12056,11 @@ | ||
| 11799 | 12056 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 11800 | 12057 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 11801 | 12058 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 11802 | 12059 | #define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 11803 | 12060 | #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 12061 | +#define MEM_Invalid 0x0080 /* Value is undefined */ | |
| 11804 | 12062 | #define MEM_TypeMask 0x00ff /* Mask of type bits */ |
| 11805 | 12063 | |
| 11806 | 12064 | /* Whenever Mem contains a valid string or blob representation, one of |
| 11807 | 12065 | ** the following flags must be set to determine the memory management |
| 11808 | 12066 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| @@ -11812,23 +12070,29 @@ | ||
| 11812 | 12070 | #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 11813 | 12071 | #define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 11814 | 12072 | #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 11815 | 12073 | #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 11816 | 12074 | #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 11817 | - | |
| 11818 | 12075 | #ifdef SQLITE_OMIT_INCRBLOB |
| 11819 | 12076 | #undef MEM_Zero |
| 11820 | 12077 | #define MEM_Zero 0x0000 |
| 11821 | 12078 | #endif |
| 11822 | - | |
| 11823 | 12079 | |
| 11824 | 12080 | /* |
| 11825 | 12081 | ** Clear any existing type flags from a Mem and replace them with f |
| 11826 | 12082 | */ |
| 11827 | 12083 | #define MemSetTypeFlag(p, f) \ |
| 11828 | 12084 | ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) |
| 11829 | 12085 | |
| 12086 | +/* | |
| 12087 | +** Return true if a memory cell is not marked as invalid. This macro | |
| 12088 | +** is for use inside assert() statements only. | |
| 12089 | +*/ | |
| 12090 | +#ifdef SQLITE_DEBUG | |
| 12091 | +#define memIsValid(M) ((M)->flags & MEM_Invalid)==0 | |
| 12092 | +#endif | |
| 12093 | + | |
| 11830 | 12094 | |
| 11831 | 12095 | /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains |
| 11832 | 12096 | ** additional information about auxiliary information bound to arguments |
| 11833 | 12097 | ** of the function. This is used to implement the sqlite3_get_auxdata() |
| 11834 | 12098 | ** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data |
| @@ -12012,10 +12276,14 @@ | ||
| 12012 | 12276 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12013 | 12277 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12014 | 12278 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12015 | 12279 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12016 | 12280 | SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12281 | + | |
| 12282 | +#ifdef SQLITE_DEBUG | |
| 12283 | +SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*); | |
| 12284 | +#endif | |
| 12017 | 12285 | |
| 12018 | 12286 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 12019 | 12287 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 12020 | 12288 | #else |
| 12021 | 12289 | # define sqlite3VdbeCheckFk(p,i) 0 |
| @@ -13518,10 +13786,16 @@ | ||
| 13518 | 13786 | SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 13519 | 13787 | return pVfs->xSleep(pVfs, nMicro); |
| 13520 | 13788 | } |
| 13521 | 13789 | SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 13522 | 13790 | int rc; |
| 13791 | + /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() | |
| 13792 | + ** method to get the current date and time if that method is available | |
| 13793 | + ** (if iVersion is 2 or greater and the function pointer is not NULL) and | |
| 13794 | + ** will fall back to xCurrentTime() if xCurrentTimeInt64() is | |
| 13795 | + ** unavailable. | |
| 13796 | + */ | |
| 13523 | 13797 | if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ |
| 13524 | 13798 | rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); |
| 13525 | 13799 | }else{ |
| 13526 | 13800 | double r; |
| 13527 | 13801 | rc = pVfs->xCurrentTime(pVfs, &r); |
| @@ -13901,11 +14175,11 @@ | ||
| 13901 | 14175 | ** routines and redirected to xFree. |
| 13902 | 14176 | */ |
| 13903 | 14177 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 13904 | 14178 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
| 13905 | 14179 | assert( pPrior!=0 && nByte>0 ); |
| 13906 | - nByte = ROUND8(nByte); | |
| 14180 | + assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ | |
| 13907 | 14181 | p--; |
| 13908 | 14182 | p = realloc(p, nByte+8 ); |
| 13909 | 14183 | if( p ){ |
| 13910 | 14184 | p[0] = nByte; |
| 13911 | 14185 | p++; |
| @@ -14307,10 +14581,11 @@ | ||
| 14307 | 14581 | */ |
| 14308 | 14582 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 14309 | 14583 | struct MemBlockHdr *pOldHdr; |
| 14310 | 14584 | void *pNew; |
| 14311 | 14585 | assert( mem.disallow==0 ); |
| 14586 | + assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ | |
| 14312 | 14587 | pOldHdr = sqlite3MemsysGetHeader(pPrior); |
| 14313 | 14588 | pNew = sqlite3MemMalloc(nByte); |
| 14314 | 14589 | if( pNew ){ |
| 14315 | 14590 | memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize); |
| 14316 | 14591 | if( nByte>pOldHdr->iSize ){ |
| @@ -15576,11 +15851,11 @@ | ||
| 15576 | 15851 | */ |
| 15577 | 15852 | static void *memsys5Realloc(void *pPrior, int nBytes){ |
| 15578 | 15853 | int nOld; |
| 15579 | 15854 | void *p; |
| 15580 | 15855 | assert( pPrior!=0 ); |
| 15581 | - assert( (nBytes&(nBytes-1))==0 ); | |
| 15856 | + assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ | |
| 15582 | 15857 | assert( nBytes>=0 ); |
| 15583 | 15858 | if( nBytes==0 ){ |
| 15584 | 15859 | return 0; |
| 15585 | 15860 | } |
| 15586 | 15861 | nOld = memsys5Size(pPrior); |
| @@ -17100,10 +17375,70 @@ | ||
| 17100 | 17375 | ************************************************************************* |
| 17101 | 17376 | ** |
| 17102 | 17377 | ** Memory allocation functions used throughout sqlite. |
| 17103 | 17378 | */ |
| 17104 | 17379 | |
| 17380 | +/* | |
| 17381 | +** Attempt to release up to n bytes of non-essential memory currently | |
| 17382 | +** held by SQLite. An example of non-essential memory is memory used to | |
| 17383 | +** cache database pages that are not currently in use. | |
| 17384 | +*/ | |
| 17385 | +SQLITE_API int sqlite3_release_memory(int n){ | |
| 17386 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT | |
| 17387 | + return sqlite3PcacheReleaseMemory(n); | |
| 17388 | +#else | |
| 17389 | + /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine | |
| 17390 | + ** is a no-op returning zero if SQLite is not compiled with | |
| 17391 | + ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ | |
| 17392 | + UNUSED_PARAMETER(n); | |
| 17393 | + return 0; | |
| 17394 | +#endif | |
| 17395 | +} | |
| 17396 | + | |
| 17397 | +/* | |
| 17398 | +** An instance of the following object records the location of | |
| 17399 | +** each unused scratch buffer. | |
| 17400 | +*/ | |
| 17401 | +typedef struct ScratchFreeslot { | |
| 17402 | + struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ | |
| 17403 | +} ScratchFreeslot; | |
| 17404 | + | |
| 17405 | +/* | |
| 17406 | +** State information local to the memory allocation subsystem. | |
| 17407 | +*/ | |
| 17408 | +static SQLITE_WSD struct Mem0Global { | |
| 17409 | + sqlite3_mutex *mutex; /* Mutex to serialize access */ | |
| 17410 | + | |
| 17411 | + /* | |
| 17412 | + ** The alarm callback and its arguments. The mem0.mutex lock will | |
| 17413 | + ** be held while the callback is running. Recursive calls into | |
| 17414 | + ** the memory subsystem are allowed, but no new callbacks will be | |
| 17415 | + ** issued. | |
| 17416 | + */ | |
| 17417 | + sqlite3_int64 alarmThreshold; | |
| 17418 | + void (*alarmCallback)(void*, sqlite3_int64,int); | |
| 17419 | + void *alarmArg; | |
| 17420 | + | |
| 17421 | + /* | |
| 17422 | + ** Pointers to the end of sqlite3GlobalConfig.pScratch memory | |
| 17423 | + ** (so that a range test can be used to determine if an allocation | |
| 17424 | + ** being freed came from pScratch) and a pointer to the list of | |
| 17425 | + ** unused scratch allocations. | |
| 17426 | + */ | |
| 17427 | + void *pScratchEnd; | |
| 17428 | + ScratchFreeslot *pScratchFree; | |
| 17429 | + u32 nScratchFree; | |
| 17430 | + | |
| 17431 | + /* | |
| 17432 | + ** True if heap is nearly "full" where "full" is defined by the | |
| 17433 | + ** sqlite3_soft_heap_limit() setting. | |
| 17434 | + */ | |
| 17435 | + int nearlyFull; | |
| 17436 | +} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
| 17437 | + | |
| 17438 | +#define mem0 GLOBAL(struct Mem0Global, mem0) | |
| 17439 | + | |
| 17105 | 17440 | /* |
| 17106 | 17441 | ** This routine runs when the memory allocator sees that the |
| 17107 | 17442 | ** total memory allocation is about to exceed the soft heap |
| 17108 | 17443 | ** limit. |
| 17109 | 17444 | */ |
| @@ -17114,82 +17449,70 @@ | ||
| 17114 | 17449 | ){ |
| 17115 | 17450 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 17116 | 17451 | sqlite3_release_memory(allocSize); |
| 17117 | 17452 | } |
| 17118 | 17453 | |
| 17454 | +/* | |
| 17455 | +** Change the alarm callback | |
| 17456 | +*/ | |
| 17457 | +static int sqlite3MemoryAlarm( | |
| 17458 | + void(*xCallback)(void *pArg, sqlite3_int64 used,int N), | |
| 17459 | + void *pArg, | |
| 17460 | + sqlite3_int64 iThreshold | |
| 17461 | +){ | |
| 17462 | + int nUsed; | |
| 17463 | + sqlite3_mutex_enter(mem0.mutex); | |
| 17464 | + mem0.alarmCallback = xCallback; | |
| 17465 | + mem0.alarmArg = pArg; | |
| 17466 | + mem0.alarmThreshold = iThreshold; | |
| 17467 | + nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); | |
| 17468 | + mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed); | |
| 17469 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17470 | + return SQLITE_OK; | |
| 17471 | +} | |
| 17472 | + | |
| 17473 | +#ifndef SQLITE_OMIT_DEPRECATED | |
| 17474 | +/* | |
| 17475 | +** Deprecated external interface. Internal/core SQLite code | |
| 17476 | +** should call sqlite3MemoryAlarm. | |
| 17477 | +*/ | |
| 17478 | +SQLITE_API int sqlite3_memory_alarm( | |
| 17479 | + void(*xCallback)(void *pArg, sqlite3_int64 used,int N), | |
| 17480 | + void *pArg, | |
| 17481 | + sqlite3_int64 iThreshold | |
| 17482 | +){ | |
| 17483 | + return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); | |
| 17484 | +} | |
| 17485 | +#endif | |
| 17486 | + | |
| 17119 | 17487 | /* |
| 17120 | 17488 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 17121 | 17489 | ** negative value indicates no limit. |
| 17122 | 17490 | */ |
| 17123 | -SQLITE_API void sqlite3_soft_heap_limit(int n){ | |
| 17124 | - sqlite3_uint64 iLimit; | |
| 17125 | - int overage; | |
| 17126 | - if( n<0 ){ | |
| 17127 | - iLimit = 0; | |
| 17128 | - }else{ | |
| 17129 | - iLimit = n; | |
| 17130 | - } | |
| 17491 | +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ | |
| 17492 | + sqlite3_int64 priorLimit; | |
| 17493 | + sqlite3_int64 excess; | |
| 17131 | 17494 | #ifndef SQLITE_OMIT_AUTOINIT |
| 17132 | 17495 | sqlite3_initialize(); |
| 17133 | 17496 | #endif |
| 17134 | - if( iLimit>0 ){ | |
| 17135 | - sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); | |
| 17497 | + sqlite3_mutex_enter(mem0.mutex); | |
| 17498 | + priorLimit = mem0.alarmThreshold; | |
| 17499 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17500 | + if( n<0 ) return priorLimit; | |
| 17501 | + if( n>0 ){ | |
| 17502 | + sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n); | |
| 17136 | 17503 | }else{ |
| 17137 | 17504 | sqlite3MemoryAlarm(0, 0, 0); |
| 17138 | 17505 | } |
| 17139 | - overage = (int)(sqlite3_memory_used() - (i64)n); | |
| 17140 | - if( overage>0 ){ | |
| 17141 | - sqlite3_release_memory(overage); | |
| 17142 | - } | |
| 17143 | -} | |
| 17144 | - | |
| 17145 | -/* | |
| 17146 | -** Attempt to release up to n bytes of non-essential memory currently | |
| 17147 | -** held by SQLite. An example of non-essential memory is memory used to | |
| 17148 | -** cache database pages that are not currently in use. | |
| 17149 | -*/ | |
| 17150 | -SQLITE_API int sqlite3_release_memory(int n){ | |
| 17151 | -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT | |
| 17152 | - int nRet = 0; | |
| 17153 | - nRet += sqlite3PcacheReleaseMemory(n-nRet); | |
| 17154 | - return nRet; | |
| 17155 | -#else | |
| 17156 | - UNUSED_PARAMETER(n); | |
| 17157 | - return SQLITE_OK; | |
| 17158 | -#endif | |
| 17159 | -} | |
| 17160 | - | |
| 17161 | -/* | |
| 17162 | -** State information local to the memory allocation subsystem. | |
| 17163 | -*/ | |
| 17164 | -static SQLITE_WSD struct Mem0Global { | |
| 17165 | - /* Number of free pages for scratch and page-cache memory */ | |
| 17166 | - u32 nScratchFree; | |
| 17167 | - u32 nPageFree; | |
| 17168 | - | |
| 17169 | - sqlite3_mutex *mutex; /* Mutex to serialize access */ | |
| 17170 | - | |
| 17171 | - /* | |
| 17172 | - ** The alarm callback and its arguments. The mem0.mutex lock will | |
| 17173 | - ** be held while the callback is running. Recursive calls into | |
| 17174 | - ** the memory subsystem are allowed, but no new callbacks will be | |
| 17175 | - ** issued. | |
| 17176 | - */ | |
| 17177 | - sqlite3_int64 alarmThreshold; | |
| 17178 | - void (*alarmCallback)(void*, sqlite3_int64,int); | |
| 17179 | - void *alarmArg; | |
| 17180 | - | |
| 17181 | - /* | |
| 17182 | - ** Pointers to the end of sqlite3GlobalConfig.pScratch and | |
| 17183 | - ** sqlite3GlobalConfig.pPage to a block of memory that records | |
| 17184 | - ** which pages are available. | |
| 17185 | - */ | |
| 17186 | - u32 *aScratchFree; | |
| 17187 | - u32 *aPageFree; | |
| 17188 | -} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
| 17189 | - | |
| 17190 | -#define mem0 GLOBAL(struct Mem0Global, mem0) | |
| 17506 | + excess = sqlite3_memory_used() - n; | |
| 17507 | + if( excess>0 ) sqlite3_release_memory(excess & 0x7fffffff); | |
| 17508 | + return priorLimit; | |
| 17509 | +} | |
| 17510 | +SQLITE_API void sqlite3_soft_heap_limit(int n){ | |
| 17511 | + if( n<0 ) n = 0; | |
| 17512 | + sqlite3_soft_heap_limit64(n); | |
| 17513 | +} | |
| 17191 | 17514 | |
| 17192 | 17515 | /* |
| 17193 | 17516 | ** Initialize the memory allocation subsystem. |
| 17194 | 17517 | */ |
| 17195 | 17518 | SQLITE_PRIVATE int sqlite3MallocInit(void){ |
| @@ -17199,39 +17522,48 @@ | ||
| 17199 | 17522 | memset(&mem0, 0, sizeof(mem0)); |
| 17200 | 17523 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 17201 | 17524 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
| 17202 | 17525 | } |
| 17203 | 17526 | if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 |
| 17204 | - && sqlite3GlobalConfig.nScratch>=0 ){ | |
| 17205 | - int i; | |
| 17206 | - sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4); | |
| 17207 | - mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) | |
| 17208 | - [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; | |
| 17209 | - for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } | |
| 17210 | - mem0.nScratchFree = sqlite3GlobalConfig.nScratch; | |
| 17527 | + && sqlite3GlobalConfig.nScratch>0 ){ | |
| 17528 | + int i, n, sz; | |
| 17529 | + ScratchFreeslot *pSlot; | |
| 17530 | + sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); | |
| 17531 | + sqlite3GlobalConfig.szScratch = sz; | |
| 17532 | + pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; | |
| 17533 | + n = sqlite3GlobalConfig.nScratch; | |
| 17534 | + mem0.pScratchFree = pSlot; | |
| 17535 | + mem0.nScratchFree = n; | |
| 17536 | + for(i=0; i<n-1; i++){ | |
| 17537 | + pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); | |
| 17538 | + pSlot = pSlot->pNext; | |
| 17539 | + } | |
| 17540 | + pSlot->pNext = 0; | |
| 17541 | + mem0.pScratchEnd = (void*)&pSlot[1]; | |
| 17211 | 17542 | }else{ |
| 17543 | + mem0.pScratchEnd = 0; | |
| 17212 | 17544 | sqlite3GlobalConfig.pScratch = 0; |
| 17213 | 17545 | sqlite3GlobalConfig.szScratch = 0; |
| 17214 | - } | |
| 17215 | - if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 | |
| 17216 | - && sqlite3GlobalConfig.nPage>=1 ){ | |
| 17217 | - int i; | |
| 17218 | - int overhead; | |
| 17219 | - int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage); | |
| 17220 | - int n = sqlite3GlobalConfig.nPage; | |
| 17221 | - overhead = (4*n + sz - 1)/sz; | |
| 17222 | - sqlite3GlobalConfig.nPage -= overhead; | |
| 17223 | - mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) | |
| 17224 | - [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; | |
| 17225 | - for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } | |
| 17226 | - mem0.nPageFree = sqlite3GlobalConfig.nPage; | |
| 17227 | - }else{ | |
| 17546 | + sqlite3GlobalConfig.nScratch = 0; | |
| 17547 | + } | |
| 17548 | + if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 | |
| 17549 | + || sqlite3GlobalConfig.nPage<1 ){ | |
| 17228 | 17550 | sqlite3GlobalConfig.pPage = 0; |
| 17229 | 17551 | sqlite3GlobalConfig.szPage = 0; |
| 17552 | + sqlite3GlobalConfig.nPage = 0; | |
| 17230 | 17553 | } |
| 17231 | 17554 | return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
| 17232 | 17555 | } |
| 17556 | + | |
| 17557 | +/* | |
| 17558 | +** Return true if the heap is currently under memory pressure - in other | |
| 17559 | +** words if the amount of heap used is close to the limit set by | |
| 17560 | +** sqlite3_soft_heap_limit(). | |
| 17561 | +*/ | |
| 17562 | +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ | |
| 17563 | + return mem0.nearlyFull; | |
| 17564 | +} | |
| 17233 | 17565 | |
| 17234 | 17566 | /* |
| 17235 | 17567 | ** Deinitialize the memory allocation subsystem. |
| 17236 | 17568 | */ |
| 17237 | 17569 | SQLITE_PRIVATE void sqlite3MallocEnd(void){ |
| @@ -17263,40 +17595,10 @@ | ||
| 17263 | 17595 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
| 17264 | 17596 | res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ |
| 17265 | 17597 | return res; |
| 17266 | 17598 | } |
| 17267 | 17599 | |
| 17268 | -/* | |
| 17269 | -** Change the alarm callback | |
| 17270 | -*/ | |
| 17271 | -SQLITE_PRIVATE int sqlite3MemoryAlarm( | |
| 17272 | - void(*xCallback)(void *pArg, sqlite3_int64 used,int N), | |
| 17273 | - void *pArg, | |
| 17274 | - sqlite3_int64 iThreshold | |
| 17275 | -){ | |
| 17276 | - sqlite3_mutex_enter(mem0.mutex); | |
| 17277 | - mem0.alarmCallback = xCallback; | |
| 17278 | - mem0.alarmArg = pArg; | |
| 17279 | - mem0.alarmThreshold = iThreshold; | |
| 17280 | - sqlite3_mutex_leave(mem0.mutex); | |
| 17281 | - return SQLITE_OK; | |
| 17282 | -} | |
| 17283 | - | |
| 17284 | -#ifndef SQLITE_OMIT_DEPRECATED | |
| 17285 | -/* | |
| 17286 | -** Deprecated external interface. Internal/core SQLite code | |
| 17287 | -** should call sqlite3MemoryAlarm. | |
| 17288 | -*/ | |
| 17289 | -SQLITE_API int sqlite3_memory_alarm( | |
| 17290 | - void(*xCallback)(void *pArg, sqlite3_int64 used,int N), | |
| 17291 | - void *pArg, | |
| 17292 | - sqlite3_int64 iThreshold | |
| 17293 | -){ | |
| 17294 | - return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); | |
| 17295 | -} | |
| 17296 | -#endif | |
| 17297 | - | |
| 17298 | 17600 | /* |
| 17299 | 17601 | ** Trigger the alarm |
| 17300 | 17602 | */ |
| 17301 | 17603 | static void sqlite3MallocAlarm(int nByte){ |
| 17302 | 17604 | void (*xCallback)(void*,sqlite3_int64,int); |
| @@ -17325,18 +17627,23 @@ | ||
| 17325 | 17627 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 17326 | 17628 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 17327 | 17629 | if( mem0.alarmCallback!=0 ){ |
| 17328 | 17630 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 17329 | 17631 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 17632 | + mem0.nearlyFull = 1; | |
| 17330 | 17633 | sqlite3MallocAlarm(nFull); |
| 17634 | + }else{ | |
| 17635 | + mem0.nearlyFull = 0; | |
| 17331 | 17636 | } |
| 17332 | 17637 | } |
| 17333 | 17638 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17639 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT | |
| 17334 | 17640 | if( p==0 && mem0.alarmCallback ){ |
| 17335 | 17641 | sqlite3MallocAlarm(nFull); |
| 17336 | 17642 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17337 | 17643 | } |
| 17644 | +#endif | |
| 17338 | 17645 | if( p ){ |
| 17339 | 17646 | nFull = sqlite3MallocSize(p); |
| 17340 | 17647 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 17341 | 17648 | sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 17342 | 17649 | } |
| @@ -17348,11 +17655,13 @@ | ||
| 17348 | 17655 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 17349 | 17656 | ** assumes the memory subsystem has already been initialized. |
| 17350 | 17657 | */ |
| 17351 | 17658 | SQLITE_PRIVATE void *sqlite3Malloc(int n){ |
| 17352 | 17659 | void *p; |
| 17353 | - if( n<=0 || n>=0x7fffff00 ){ | |
| 17660 | + if( n<=0 /* IMP: R-65312-04917 */ | |
| 17661 | + || n>=0x7fffff00 | |
| 17662 | + ){ | |
| 17354 | 17663 | /* A memory allocation of a number of bytes which is near the maximum |
| 17355 | 17664 | ** signed integer value might cause an integer overflow inside of the |
| 17356 | 17665 | ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 17357 | 17666 | ** 255 bytes of overhead. SQLite itself will never use anything near |
| 17358 | 17667 | ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
| @@ -17362,10 +17671,11 @@ | ||
| 17362 | 17671 | mallocWithAlarm(n, &p); |
| 17363 | 17672 | sqlite3_mutex_leave(mem0.mutex); |
| 17364 | 17673 | }else{ |
| 17365 | 17674 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 17366 | 17675 | } |
| 17676 | + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ | |
| 17367 | 17677 | return p; |
| 17368 | 17678 | } |
| 17369 | 17679 | |
| 17370 | 17680 | /* |
| 17371 | 17681 | ** This version of the memory allocation is for use by the application. |
| @@ -17399,64 +17709,70 @@ | ||
| 17399 | 17709 | ** embedded processor. |
| 17400 | 17710 | */ |
| 17401 | 17711 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ |
| 17402 | 17712 | void *p; |
| 17403 | 17713 | assert( n>0 ); |
| 17714 | + | |
| 17715 | + sqlite3_mutex_enter(mem0.mutex); | |
| 17716 | + if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ | |
| 17717 | + p = mem0.pScratchFree; | |
| 17718 | + mem0.pScratchFree = mem0.pScratchFree->pNext; | |
| 17719 | + mem0.nScratchFree--; | |
| 17720 | + sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); | |
| 17721 | + sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | |
| 17722 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17723 | + }else{ | |
| 17724 | + if( sqlite3GlobalConfig.bMemstat ){ | |
| 17725 | + sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | |
| 17726 | + n = mallocWithAlarm(n, &p); | |
| 17727 | + if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); | |
| 17728 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17729 | + }else{ | |
| 17730 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17731 | + p = sqlite3GlobalConfig.m.xMalloc(n); | |
| 17732 | + } | |
| 17733 | + sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); | |
| 17734 | + } | |
| 17735 | + assert( sqlite3_mutex_notheld(mem0.mutex) ); | |
| 17736 | + | |
| 17404 | 17737 | |
| 17405 | 17738 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17406 | - /* Verify that no more than two scratch allocation per thread | |
| 17407 | - ** is outstanding at one time. (This is only checked in the | |
| 17739 | + /* Verify that no more than two scratch allocations per thread | |
| 17740 | + ** are outstanding at one time. (This is only checked in the | |
| 17408 | 17741 | ** single-threaded case since checking in the multi-threaded case |
| 17409 | 17742 | ** would be much more complicated.) */ |
| 17410 | 17743 | assert( scratchAllocOut<=1 ); |
| 17411 | -#endif | |
| 17412 | - | |
| 17413 | - if( sqlite3GlobalConfig.szScratch<n ){ | |
| 17414 | - goto scratch_overflow; | |
| 17415 | - }else{ | |
| 17416 | - sqlite3_mutex_enter(mem0.mutex); | |
| 17417 | - if( mem0.nScratchFree==0 ){ | |
| 17418 | - sqlite3_mutex_leave(mem0.mutex); | |
| 17419 | - goto scratch_overflow; | |
| 17420 | - }else{ | |
| 17421 | - int i; | |
| 17422 | - i = mem0.aScratchFree[--mem0.nScratchFree]; | |
| 17423 | - i *= sqlite3GlobalConfig.szScratch; | |
| 17424 | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); | |
| 17425 | - sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | |
| 17426 | - sqlite3_mutex_leave(mem0.mutex); | |
| 17427 | - p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; | |
| 17428 | - assert( (((u8*)p - (u8*)0) & 7)==0 ); | |
| 17429 | - } | |
| 17430 | - } | |
| 17431 | -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | |
| 17432 | - scratchAllocOut = p!=0; | |
| 17433 | -#endif | |
| 17434 | - | |
| 17435 | - return p; | |
| 17436 | - | |
| 17437 | -scratch_overflow: | |
| 17438 | - if( sqlite3GlobalConfig.bMemstat ){ | |
| 17439 | - sqlite3_mutex_enter(mem0.mutex); | |
| 17440 | - sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | |
| 17441 | - n = mallocWithAlarm(n, &p); | |
| 17442 | - if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); | |
| 17443 | - sqlite3_mutex_leave(mem0.mutex); | |
| 17444 | - }else{ | |
| 17445 | - p = sqlite3GlobalConfig.m.xMalloc(n); | |
| 17446 | - } | |
| 17447 | - sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); | |
| 17448 | -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | |
| 17449 | - scratchAllocOut = p!=0; | |
| 17450 | -#endif | |
| 17451 | - return p; | |
| 17744 | + if( p ) scratchAllocOut++; | |
| 17745 | +#endif | |
| 17746 | + | |
| 17747 | + return p; | |
| 17452 | 17748 | } |
| 17453 | 17749 | SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ |
| 17454 | 17750 | if( p ){ |
| 17455 | - if( sqlite3GlobalConfig.pScratch==0 | |
| 17456 | - || p<sqlite3GlobalConfig.pScratch | |
| 17457 | - || p>=(void*)mem0.aScratchFree ){ | |
| 17751 | + | |
| 17752 | +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | |
| 17753 | + /* Verify that no more than two scratch allocation per thread | |
| 17754 | + ** is outstanding at one time. (This is only checked in the | |
| 17755 | + ** single-threaded case since checking in the multi-threaded case | |
| 17756 | + ** would be much more complicated.) */ | |
| 17757 | + assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); | |
| 17758 | + scratchAllocOut--; | |
| 17759 | +#endif | |
| 17760 | + | |
| 17761 | + if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ | |
| 17762 | + /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ | |
| 17763 | + ScratchFreeslot *pSlot; | |
| 17764 | + pSlot = (ScratchFreeslot*)p; | |
| 17765 | + sqlite3_mutex_enter(mem0.mutex); | |
| 17766 | + pSlot->pNext = mem0.pScratchFree; | |
| 17767 | + mem0.pScratchFree = pSlot; | |
| 17768 | + mem0.nScratchFree++; | |
| 17769 | + assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch ); | |
| 17770 | + sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); | |
| 17771 | + sqlite3_mutex_leave(mem0.mutex); | |
| 17772 | + }else{ | |
| 17773 | + /* Release memory back to the heap */ | |
| 17458 | 17774 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 17459 | 17775 | assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 17460 | 17776 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 17461 | 17777 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17462 | 17778 | int iSize = sqlite3MallocSize(p); |
| @@ -17467,30 +17783,10 @@ | ||
| 17467 | 17783 | sqlite3GlobalConfig.m.xFree(p); |
| 17468 | 17784 | sqlite3_mutex_leave(mem0.mutex); |
| 17469 | 17785 | }else{ |
| 17470 | 17786 | sqlite3GlobalConfig.m.xFree(p); |
| 17471 | 17787 | } |
| 17472 | - }else{ | |
| 17473 | - int i; | |
| 17474 | - i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); | |
| 17475 | - i /= sqlite3GlobalConfig.szScratch; | |
| 17476 | - assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); | |
| 17477 | - sqlite3_mutex_enter(mem0.mutex); | |
| 17478 | - assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); | |
| 17479 | - mem0.aScratchFree[mem0.nScratchFree++] = i; | |
| 17480 | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); | |
| 17481 | - sqlite3_mutex_leave(mem0.mutex); | |
| 17482 | - | |
| 17483 | -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | |
| 17484 | - /* Verify that no more than two scratch allocation per thread | |
| 17485 | - ** is outstanding at one time. (This is only checked in the | |
| 17486 | - ** single-threaded case since checking in the multi-threaded case | |
| 17487 | - ** would be much more complicated.) */ | |
| 17488 | - assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); | |
| 17489 | - scratchAllocOut = 0; | |
| 17490 | -#endif | |
| 17491 | - | |
| 17492 | 17788 | } |
| 17493 | 17789 | } |
| 17494 | 17790 | } |
| 17495 | 17791 | |
| 17496 | 17792 | /* |
| @@ -17527,11 +17823,11 @@ | ||
| 17527 | 17823 | |
| 17528 | 17824 | /* |
| 17529 | 17825 | ** Free memory previously obtained from sqlite3Malloc(). |
| 17530 | 17826 | */ |
| 17531 | 17827 | SQLITE_API void sqlite3_free(void *p){ |
| 17532 | - if( p==0 ) return; | |
| 17828 | + if( p==0 ) return; /* IMP: R-49053-54554 */ | |
| 17533 | 17829 | assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 17534 | 17830 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17535 | 17831 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17536 | 17832 | sqlite3_mutex_enter(mem0.mutex); |
| 17537 | 17833 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| @@ -17574,21 +17870,24 @@ | ||
| 17574 | 17870 | */ |
| 17575 | 17871 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){ |
| 17576 | 17872 | int nOld, nNew; |
| 17577 | 17873 | void *pNew; |
| 17578 | 17874 | if( pOld==0 ){ |
| 17579 | - return sqlite3Malloc(nBytes); | |
| 17875 | + return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */ | |
| 17580 | 17876 | } |
| 17581 | 17877 | if( nBytes<=0 ){ |
| 17582 | - sqlite3_free(pOld); | |
| 17878 | + sqlite3_free(pOld); /* IMP: R-31593-10574 */ | |
| 17583 | 17879 | return 0; |
| 17584 | 17880 | } |
| 17585 | 17881 | if( nBytes>=0x7fffff00 ){ |
| 17586 | 17882 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 17587 | 17883 | return 0; |
| 17588 | 17884 | } |
| 17589 | 17885 | nOld = sqlite3MallocSize(pOld); |
| 17886 | + /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second | |
| 17887 | + ** argument to xRealloc is always a value returned by a prior call to | |
| 17888 | + ** xRoundup. */ | |
| 17590 | 17889 | nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); |
| 17591 | 17890 | if( nOld==nNew ){ |
| 17592 | 17891 | pNew = pOld; |
| 17593 | 17892 | }else if( sqlite3GlobalConfig.bMemstat ){ |
| 17594 | 17893 | sqlite3_mutex_enter(mem0.mutex); |
| @@ -17610,10 +17909,11 @@ | ||
| 17610 | 17909 | } |
| 17611 | 17910 | sqlite3_mutex_leave(mem0.mutex); |
| 17612 | 17911 | }else{ |
| 17613 | 17912 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 17614 | 17913 | } |
| 17914 | + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */ | |
| 17615 | 17915 | return pNew; |
| 17616 | 17916 | } |
| 17617 | 17917 | |
| 17618 | 17918 | /* |
| 17619 | 17919 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| @@ -19773,10 +20073,16 @@ | ||
| 19773 | 20073 | #define UpperToLower sqlite3UpperToLower |
| 19774 | 20074 | |
| 19775 | 20075 | /* |
| 19776 | 20076 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 19777 | 20077 | ** there is no consistency, we will define our own. |
| 20078 | +** | |
| 20079 | +** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows | |
| 20080 | +** applications and extensions to compare the contents of two buffers | |
| 20081 | +** containing UTF-8 strings in a case-independent fashion, using the same | |
| 20082 | +** definition of case independence that SQLite uses internally when | |
| 20083 | +** comparing identifiers. | |
| 19778 | 20084 | */ |
| 19779 | 20085 | SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 19780 | 20086 | register unsigned char *a, *b; |
| 19781 | 20087 | a = (unsigned char *)zLeft; |
| 19782 | 20088 | b = (unsigned char *)zRight; |
| @@ -26177,11 +26483,11 @@ | ||
| 26177 | 26483 | goto shmpage_out; |
| 26178 | 26484 | } |
| 26179 | 26485 | pShmNode->apRegion = apNew; |
| 26180 | 26486 | while(pShmNode->nRegion<=iRegion){ |
| 26181 | 26487 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
| 26182 | - MAP_SHARED, pShmNode->h, iRegion*szRegion | |
| 26488 | + MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion | |
| 26183 | 26489 | ); |
| 26184 | 26490 | if( pMem==MAP_FAILED ){ |
| 26185 | 26491 | rc = SQLITE_IOERR; |
| 26186 | 26492 | goto shmpage_out; |
| 26187 | 26493 | } |
| @@ -28000,17 +28306,20 @@ | ||
| 28000 | 28306 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 28001 | 28307 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 28002 | 28308 | |
| 28003 | 28309 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 28004 | 28310 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 28311 | +#if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ | |
| 28312 | + && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 | |
| 28005 | 28313 | if( gethostuuid(pHostID, &timeout) ){ |
| 28006 | 28314 | int err = errno; |
| 28007 | 28315 | if( pError ){ |
| 28008 | 28316 | *pError = err; |
| 28009 | 28317 | } |
| 28010 | 28318 | return SQLITE_IOERR; |
| 28011 | 28319 | } |
| 28320 | +#endif | |
| 28012 | 28321 | #ifdef SQLITE_TEST |
| 28013 | 28322 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 28014 | 28323 | if( sqlite3_hostid_num != 0){ |
| 28015 | 28324 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 28016 | 28325 | } |
| @@ -30339,10 +30648,18 @@ | ||
| 30339 | 30648 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN; |
| 30340 | 30649 | } |
| 30341 | 30650 | |
| 30342 | 30651 | #ifndef SQLITE_OMIT_WAL |
| 30343 | 30652 | |
| 30653 | +/* | |
| 30654 | +** Windows will only let you create file view mappings | |
| 30655 | +** on allocation size granularity boundaries. | |
| 30656 | +** During sqlite3_os_init() we do a GetSystemInfo() | |
| 30657 | +** to get the granularity size. | |
| 30658 | +*/ | |
| 30659 | +SYSTEM_INFO winSysInfo; | |
| 30660 | + | |
| 30344 | 30661 | /* |
| 30345 | 30662 | ** Helper functions to obtain and relinquish the global mutex. The |
| 30346 | 30663 | ** global mutex is used to protect the winLockInfo objects used by |
| 30347 | 30664 | ** this file, all of which may be shared by multiple threads. |
| 30348 | 30665 | ** |
| @@ -30507,19 +30824,26 @@ | ||
| 30507 | 30824 | ** by VFS shared-memory methods. |
| 30508 | 30825 | */ |
| 30509 | 30826 | static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ |
| 30510 | 30827 | winShmNode **pp; |
| 30511 | 30828 | winShmNode *p; |
| 30829 | + BOOL bRc; | |
| 30512 | 30830 | assert( winShmMutexHeld() ); |
| 30513 | 30831 | pp = &winShmNodeList; |
| 30514 | 30832 | while( (p = *pp)!=0 ){ |
| 30515 | 30833 | if( p->nRef==0 ){ |
| 30516 | 30834 | int i; |
| 30517 | 30835 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
| 30518 | 30836 | for(i=0; i<p->nRegion; i++){ |
| 30519 | - UnmapViewOfFile(p->aRegion[i].pMap); | |
| 30520 | - CloseHandle(p->aRegion[i].hMap); | |
| 30837 | + bRc = UnmapViewOfFile(p->aRegion[i].pMap); | |
| 30838 | + OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n", | |
| 30839 | + (int)GetCurrentProcessId(), i, | |
| 30840 | + bRc ? "ok" : "failed")); | |
| 30841 | + bRc = CloseHandle(p->aRegion[i].hMap); | |
| 30842 | + OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n", | |
| 30843 | + (int)GetCurrentProcessId(), i, | |
| 30844 | + bRc ? "ok" : "failed")); | |
| 30521 | 30845 | } |
| 30522 | 30846 | if( p->hFile.h != INVALID_HANDLE_VALUE ){ |
| 30523 | 30847 | SimulateIOErrorBenign(1); |
| 30524 | 30848 | winClose((sqlite3_file *)&p->hFile); |
| 30525 | 30849 | SimulateIOErrorBenign(0); |
| @@ -30592,14 +30916,15 @@ | ||
| 30592 | 30916 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 30593 | 30917 | if( pShmNode->mutex==0 ){ |
| 30594 | 30918 | rc = SQLITE_NOMEM; |
| 30595 | 30919 | goto shm_open_err; |
| 30596 | 30920 | } |
| 30921 | + | |
| 30597 | 30922 | rc = winOpen(pDbFd->pVfs, |
| 30598 | 30923 | pShmNode->zFilename, /* Name of the file (UTF-8) */ |
| 30599 | 30924 | (sqlite3_file*)&pShmNode->hFile, /* File handle here */ |
| 30600 | - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */ | |
| 30925 | + SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */ | |
| 30601 | 30926 | 0); |
| 30602 | 30927 | if( SQLITE_OK!=rc ){ |
| 30603 | 30928 | rc = SQLITE_CANTOPEN_BKPT; |
| 30604 | 30929 | goto shm_open_err; |
| 30605 | 30930 | } |
| @@ -30903,14 +31228,22 @@ | ||
| 30903 | 31228 | void *pMap = 0; /* Mapped memory region */ |
| 30904 | 31229 | |
| 30905 | 31230 | hMap = CreateFileMapping(pShmNode->hFile.h, |
| 30906 | 31231 | NULL, PAGE_READWRITE, 0, nByte, NULL |
| 30907 | 31232 | ); |
| 31233 | + OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n", | |
| 31234 | + (int)GetCurrentProcessId(), pShmNode->nRegion, nByte, | |
| 31235 | + hMap ? "ok" : "failed")); | |
| 30908 | 31236 | if( hMap ){ |
| 31237 | + int iOffset = pShmNode->nRegion*szRegion; | |
| 31238 | + int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; | |
| 30909 | 31239 | pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, |
| 30910 | - 0, 0, nByte | |
| 31240 | + 0, iOffset - iOffsetShift, szRegion + iOffsetShift | |
| 30911 | 31241 | ); |
| 31242 | + OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n", | |
| 31243 | + (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion, | |
| 31244 | + pMap ? "ok" : "failed")); | |
| 30912 | 31245 | } |
| 30913 | 31246 | if( !pMap ){ |
| 30914 | 31247 | pShmNode->lastErrno = GetLastError(); |
| 30915 | 31248 | rc = SQLITE_IOERR; |
| 30916 | 31249 | if( hMap ) CloseHandle(hMap); |
| @@ -30923,12 +31256,14 @@ | ||
| 30923 | 31256 | } |
| 30924 | 31257 | } |
| 30925 | 31258 | |
| 30926 | 31259 | shmpage_out: |
| 30927 | 31260 | if( pShmNode->nRegion>iRegion ){ |
| 31261 | + int iOffset = iRegion*szRegion; | |
| 31262 | + int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; | |
| 30928 | 31263 | char *p = (char *)pShmNode->aRegion[iRegion].pMap; |
| 30929 | - *pp = (void *)&p[iRegion*szRegion]; | |
| 31264 | + *pp = (void *)&p[iOffsetShift]; | |
| 30930 | 31265 | }else{ |
| 30931 | 31266 | *pp = 0; |
| 30932 | 31267 | } |
| 30933 | 31268 | sqlite3_mutex_leave(pShmNode->mutex); |
| 30934 | 31269 | return rc; |
| @@ -31151,13 +31486,64 @@ | ||
| 31151 | 31486 | DWORD dwFlagsAndAttributes = 0; |
| 31152 | 31487 | #if SQLITE_OS_WINCE |
| 31153 | 31488 | int isTemp = 0; |
| 31154 | 31489 | #endif |
| 31155 | 31490 | winFile *pFile = (winFile*)id; |
| 31156 | - void *zConverted; /* Filename in OS encoding */ | |
| 31157 | - const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ | |
| 31158 | - char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */ | |
| 31491 | + void *zConverted; /* Filename in OS encoding */ | |
| 31492 | + const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ | |
| 31493 | + | |
| 31494 | + /* If argument zPath is a NULL pointer, this function is required to open | |
| 31495 | + ** a temporary file. Use this buffer to store the file name in. | |
| 31496 | + */ | |
| 31497 | + char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */ | |
| 31498 | + | |
| 31499 | + int rc = SQLITE_OK; /* Function Return Code */ | |
| 31500 | +#if !defined(NDEBUG) || SQLITE_OS_WINCE | |
| 31501 | + int eType = flags&0xFFFFFF00; /* Type of file to open */ | |
| 31502 | +#endif | |
| 31503 | + | |
| 31504 | + int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); | |
| 31505 | + int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); | |
| 31506 | + int isCreate = (flags & SQLITE_OPEN_CREATE); | |
| 31507 | +#ifndef NDEBUG | |
| 31508 | + int isReadonly = (flags & SQLITE_OPEN_READONLY); | |
| 31509 | +#endif | |
| 31510 | + int isReadWrite = (flags & SQLITE_OPEN_READWRITE); | |
| 31511 | + | |
| 31512 | +#ifndef NDEBUG | |
| 31513 | + int isOpenJournal = (isCreate && ( | |
| 31514 | + eType==SQLITE_OPEN_MASTER_JOURNAL | |
| 31515 | + || eType==SQLITE_OPEN_MAIN_JOURNAL | |
| 31516 | + || eType==SQLITE_OPEN_WAL | |
| 31517 | + )); | |
| 31518 | +#endif | |
| 31519 | + | |
| 31520 | + /* Check the following statements are true: | |
| 31521 | + ** | |
| 31522 | + ** (a) Exactly one of the READWRITE and READONLY flags must be set, and | |
| 31523 | + ** (b) if CREATE is set, then READWRITE must also be set, and | |
| 31524 | + ** (c) if EXCLUSIVE is set, then CREATE must also be set. | |
| 31525 | + ** (d) if DELETEONCLOSE is set, then CREATE must also be set. | |
| 31526 | + */ | |
| 31527 | + assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); | |
| 31528 | + assert(isCreate==0 || isReadWrite); | |
| 31529 | + assert(isExclusive==0 || isCreate); | |
| 31530 | + assert(isDelete==0 || isCreate); | |
| 31531 | + | |
| 31532 | + /* The main DB, main journal, WAL file and master journal are never | |
| 31533 | + ** automatically deleted. Nor are they ever temporary files. */ | |
| 31534 | + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); | |
| 31535 | + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); | |
| 31536 | + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); | |
| 31537 | + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); | |
| 31538 | + | |
| 31539 | + /* Assert that the upper layer has set one of the "file-type" flags. */ | |
| 31540 | + assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB | |
| 31541 | + || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL | |
| 31542 | + || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL | |
| 31543 | + || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL | |
| 31544 | + ); | |
| 31159 | 31545 | |
| 31160 | 31546 | assert( id!=0 ); |
| 31161 | 31547 | UNUSED_PARAMETER(pVfs); |
| 31162 | 31548 | |
| 31163 | 31549 | pFile->h = INVALID_HANDLE_VALUE; |
| @@ -31164,11 +31550,12 @@ | ||
| 31164 | 31550 | |
| 31165 | 31551 | /* If the second argument to this function is NULL, generate a |
| 31166 | 31552 | ** temporary file name to use |
| 31167 | 31553 | */ |
| 31168 | 31554 | if( !zUtf8Name ){ |
| 31169 | - int rc = getTempname(MAX_PATH+1, zTmpname); | |
| 31555 | + assert(isDelete && !isOpenJournal); | |
| 31556 | + rc = getTempname(MAX_PATH+1, zTmpname); | |
| 31170 | 31557 | if( rc!=SQLITE_OK ){ |
| 31171 | 31558 | return rc; |
| 31172 | 31559 | } |
| 31173 | 31560 | zUtf8Name = zTmpname; |
| 31174 | 31561 | } |
| @@ -31177,33 +31564,35 @@ | ||
| 31177 | 31564 | zConverted = convertUtf8Filename(zUtf8Name); |
| 31178 | 31565 | if( zConverted==0 ){ |
| 31179 | 31566 | return SQLITE_NOMEM; |
| 31180 | 31567 | } |
| 31181 | 31568 | |
| 31182 | - if( flags & SQLITE_OPEN_READWRITE ){ | |
| 31569 | + if( isReadWrite ){ | |
| 31183 | 31570 | dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 31184 | 31571 | }else{ |
| 31185 | 31572 | dwDesiredAccess = GENERIC_READ; |
| 31186 | 31573 | } |
| 31574 | + | |
| 31187 | 31575 | /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is |
| 31188 | 31576 | ** created. SQLite doesn't use it to indicate "exclusive access" |
| 31189 | 31577 | ** as it is usually understood. |
| 31190 | 31578 | */ |
| 31191 | - assert(!(flags & SQLITE_OPEN_EXCLUSIVE) || (flags & SQLITE_OPEN_CREATE)); | |
| 31192 | - if( flags & SQLITE_OPEN_EXCLUSIVE ){ | |
| 31579 | + if( isExclusive ){ | |
| 31193 | 31580 | /* Creates a new file, only if it does not already exist. */ |
| 31194 | 31581 | /* If the file exists, it fails. */ |
| 31195 | 31582 | dwCreationDisposition = CREATE_NEW; |
| 31196 | - }else if( flags & SQLITE_OPEN_CREATE ){ | |
| 31583 | + }else if( isCreate ){ | |
| 31197 | 31584 | /* Open existing file, or create if it doesn't exist */ |
| 31198 | 31585 | dwCreationDisposition = OPEN_ALWAYS; |
| 31199 | 31586 | }else{ |
| 31200 | 31587 | /* Opens a file, only if it exists. */ |
| 31201 | 31588 | dwCreationDisposition = OPEN_EXISTING; |
| 31202 | 31589 | } |
| 31590 | + | |
| 31203 | 31591 | dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 31204 | - if( flags & SQLITE_OPEN_DELETEONCLOSE ){ | |
| 31592 | + | |
| 31593 | + if( isDelete ){ | |
| 31205 | 31594 | #if SQLITE_OS_WINCE |
| 31206 | 31595 | dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
| 31207 | 31596 | isTemp = 1; |
| 31208 | 31597 | #else |
| 31209 | 31598 | dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| @@ -31216,10 +31605,11 @@ | ||
| 31216 | 31605 | /* Reports from the internet are that performance is always |
| 31217 | 31606 | ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
| 31218 | 31607 | #if SQLITE_OS_WINCE |
| 31219 | 31608 | dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
| 31220 | 31609 | #endif |
| 31610 | + | |
| 31221 | 31611 | if( isNT() ){ |
| 31222 | 31612 | h = CreateFileW((WCHAR*)zConverted, |
| 31223 | 31613 | dwDesiredAccess, |
| 31224 | 31614 | dwShareMode, |
| 31225 | 31615 | NULL, |
| @@ -31241,41 +31631,45 @@ | ||
| 31241 | 31631 | dwFlagsAndAttributes, |
| 31242 | 31632 | NULL |
| 31243 | 31633 | ); |
| 31244 | 31634 | #endif |
| 31245 | 31635 | } |
| 31636 | + | |
| 31246 | 31637 | OSTRACE(("OPEN %d %s 0x%lx %s\n", |
| 31247 | 31638 | h, zName, dwDesiredAccess, |
| 31248 | 31639 | h==INVALID_HANDLE_VALUE ? "failed" : "ok")); |
| 31640 | + | |
| 31249 | 31641 | if( h==INVALID_HANDLE_VALUE ){ |
| 31250 | 31642 | pFile->lastErrno = GetLastError(); |
| 31251 | 31643 | free(zConverted); |
| 31252 | - if( flags & SQLITE_OPEN_READWRITE ){ | |
| 31644 | + if( isReadWrite ){ | |
| 31253 | 31645 | return winOpen(pVfs, zName, id, |
| 31254 | - ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags); | |
| 31646 | + ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); | |
| 31255 | 31647 | }else{ |
| 31256 | 31648 | return SQLITE_CANTOPEN_BKPT; |
| 31257 | 31649 | } |
| 31258 | 31650 | } |
| 31651 | + | |
| 31259 | 31652 | if( pOutFlags ){ |
| 31260 | - if( flags & SQLITE_OPEN_READWRITE ){ | |
| 31653 | + if( isReadWrite ){ | |
| 31261 | 31654 | *pOutFlags = SQLITE_OPEN_READWRITE; |
| 31262 | 31655 | }else{ |
| 31263 | 31656 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 31264 | 31657 | } |
| 31265 | 31658 | } |
| 31659 | + | |
| 31266 | 31660 | memset(pFile, 0, sizeof(*pFile)); |
| 31267 | 31661 | pFile->pMethod = &winIoMethod; |
| 31268 | 31662 | pFile->h = h; |
| 31269 | 31663 | pFile->lastErrno = NO_ERROR; |
| 31270 | 31664 | pFile->pVfs = pVfs; |
| 31271 | 31665 | pFile->pShm = 0; |
| 31272 | 31666 | pFile->zPath = zName; |
| 31273 | 31667 | pFile->sectorSize = getSectorSize(pVfs, zUtf8Name); |
| 31668 | + | |
| 31274 | 31669 | #if SQLITE_OS_WINCE |
| 31275 | - if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) == | |
| 31276 | - (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB) | |
| 31670 | + if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB | |
| 31277 | 31671 | && !winceCreateLock(zName, pFile) |
| 31278 | 31672 | ){ |
| 31279 | 31673 | CloseHandle(h); |
| 31280 | 31674 | free(zConverted); |
| 31281 | 31675 | return SQLITE_CANTOPEN_BKPT; |
| @@ -31285,12 +31679,13 @@ | ||
| 31285 | 31679 | }else |
| 31286 | 31680 | #endif |
| 31287 | 31681 | { |
| 31288 | 31682 | free(zConverted); |
| 31289 | 31683 | } |
| 31684 | + | |
| 31290 | 31685 | OpenCounter(+1); |
| 31291 | - return SQLITE_OK; | |
| 31686 | + return rc; | |
| 31292 | 31687 | } |
| 31293 | 31688 | |
| 31294 | 31689 | /* |
| 31295 | 31690 | ** Delete the named file. |
| 31296 | 31691 | ** |
| @@ -31804,10 +32199,17 @@ | ||
| 31804 | 32199 | winSleep, /* xSleep */ |
| 31805 | 32200 | winCurrentTime, /* xCurrentTime */ |
| 31806 | 32201 | winGetLastError, /* xGetLastError */ |
| 31807 | 32202 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 31808 | 32203 | }; |
| 32204 | + | |
| 32205 | +#ifndef SQLITE_OMIT_WAL | |
| 32206 | + /* get memory map allocation granularity */ | |
| 32207 | + memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); | |
| 32208 | + GetSystemInfo(&winSysInfo); | |
| 32209 | + assert(winSysInfo.dwAllocationGranularity > 0); | |
| 32210 | +#endif | |
| 31809 | 32211 | |
| 31810 | 32212 | sqlite3_vfs_register(&winVfs, 1); |
| 31811 | 32213 | return SQLITE_OK; |
| 31812 | 32214 | } |
| 31813 | 32215 | SQLITE_API int sqlite3_os_end(void){ |
| @@ -32369,16 +32771,20 @@ | ||
| 32369 | 32771 | ** Initialize and shutdown the page cache subsystem. Neither of these |
| 32370 | 32772 | ** functions are threadsafe. |
| 32371 | 32773 | */ |
| 32372 | 32774 | SQLITE_PRIVATE int sqlite3PcacheInitialize(void){ |
| 32373 | 32775 | if( sqlite3GlobalConfig.pcache.xInit==0 ){ |
| 32776 | + /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the | |
| 32777 | + ** built-in default page cache is used instead of the application defined | |
| 32778 | + ** page cache. */ | |
| 32374 | 32779 | sqlite3PCacheSetDefault(); |
| 32375 | 32780 | } |
| 32376 | 32781 | return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg); |
| 32377 | 32782 | } |
| 32378 | 32783 | SQLITE_PRIVATE void sqlite3PcacheShutdown(void){ |
| 32379 | 32784 | if( sqlite3GlobalConfig.pcache.xShutdown ){ |
| 32785 | + /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ | |
| 32380 | 32786 | sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg); |
| 32381 | 32787 | } |
| 32382 | 32788 | } |
| 32383 | 32789 | |
| 32384 | 32790 | /* |
| @@ -32835,12 +33241,17 @@ | ||
| 32835 | 33241 | |
| 32836 | 33242 | typedef struct PCache1 PCache1; |
| 32837 | 33243 | typedef struct PgHdr1 PgHdr1; |
| 32838 | 33244 | typedef struct PgFreeslot PgFreeslot; |
| 32839 | 33245 | |
| 32840 | -/* Pointers to structures of this type are cast and returned as | |
| 32841 | -** opaque sqlite3_pcache* handles | |
| 33246 | +/* Each page cache is an instance of the following object. Every | |
| 33247 | +** open database file (including each in-memory database and each | |
| 33248 | +** temporary or transient database) has a single page cache which | |
| 33249 | +** is an instance of this object. | |
| 33250 | +** | |
| 33251 | +** Pointers to structures of this type are cast and returned as | |
| 33252 | +** opaque sqlite3_pcache* handles. | |
| 32842 | 33253 | */ |
| 32843 | 33254 | struct PCache1 { |
| 32844 | 33255 | /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 32845 | 33256 | ** flag (bPurgeable) are set when the cache is created. nMax may be |
| 32846 | 33257 | ** modified at any time by a call to the pcache1CacheSize() method. |
| @@ -32896,10 +33307,13 @@ | ||
| 32896 | 33307 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 32897 | 33308 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 32898 | 33309 | |
| 32899 | 33310 | /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */ |
| 32900 | 33311 | int szSlot; /* Size of each free slot */ |
| 33312 | + int nSlot; /* The number of pcache slots */ | |
| 33313 | + int nFreeSlot; /* Number of unused pcache slots */ | |
| 33314 | + int nReserve; /* Try to keep nFreeSlot above this */ | |
| 32901 | 33315 | void *pStart, *pEnd; /* Bounds of pagecache malloc range */ |
| 32902 | 33316 | PgFreeslot *pFree; /* Free page blocks */ |
| 32903 | 33317 | int isInit; /* True if initialized */ |
| 32904 | 33318 | } pcache1_g; |
| 32905 | 33319 | |
| @@ -32943,10 +33357,12 @@ | ||
| 32943 | 33357 | SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
| 32944 | 33358 | if( pcache1.isInit ){ |
| 32945 | 33359 | PgFreeslot *p; |
| 32946 | 33360 | sz = ROUNDDOWN8(sz); |
| 32947 | 33361 | pcache1.szSlot = sz; |
| 33362 | + pcache1.nSlot = pcache1.nFreeSlot = n; | |
| 33363 | + pcache1.nReserve = n>90 ? 10 : (n/10 + 1); | |
| 32948 | 33364 | pcache1.pStart = pBuf; |
| 32949 | 33365 | pcache1.pFree = 0; |
| 32950 | 33366 | while( n-- ){ |
| 32951 | 33367 | p = (PgFreeslot*)pBuf; |
| 32952 | 33368 | p->pNext = pcache1.pFree; |
| @@ -32969,10 +33385,12 @@ | ||
| 32969 | 33385 | sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 32970 | 33386 | if( nByte<=pcache1.szSlot && pcache1.pFree ){ |
| 32971 | 33387 | assert( pcache1.isInit ); |
| 32972 | 33388 | p = (PgHdr1 *)pcache1.pFree; |
| 32973 | 33389 | pcache1.pFree = pcache1.pFree->pNext; |
| 33390 | + pcache1.nFreeSlot--; | |
| 33391 | + assert( pcache1.nFreeSlot>=0 ); | |
| 32974 | 33392 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 32975 | 33393 | }else{ |
| 32976 | 33394 | |
| 32977 | 33395 | /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the |
| 32978 | 33396 | ** global pcache mutex and unlock the pager-cache object pCache. This is |
| @@ -33002,10 +33420,12 @@ | ||
| 33002 | 33420 | PgFreeslot *pSlot; |
| 33003 | 33421 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 33004 | 33422 | pSlot = (PgFreeslot*)p; |
| 33005 | 33423 | pSlot->pNext = pcache1.pFree; |
| 33006 | 33424 | pcache1.pFree = pSlot; |
| 33425 | + pcache1.nFreeSlot++; | |
| 33426 | + assert( pcache1.nFreeSlot<=pcache1.nSlot ); | |
| 33007 | 33427 | }else{ |
| 33008 | 33428 | int iSize; |
| 33009 | 33429 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 33010 | 33430 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 33011 | 33431 | iSize = sqlite3MallocSize(p); |
| @@ -33014,11 +33434,11 @@ | ||
| 33014 | 33434 | } |
| 33015 | 33435 | } |
| 33016 | 33436 | |
| 33017 | 33437 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 33018 | 33438 | /* |
| 33019 | -** Return the size of a pache allocation | |
| 33439 | +** Return the size of a pcache allocation | |
| 33020 | 33440 | */ |
| 33021 | 33441 | static int pcache1MemSize(void *p){ |
| 33022 | 33442 | assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33023 | 33443 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33024 | 33444 | return pcache1.szSlot; |
| @@ -33087,10 +33507,36 @@ | ||
| 33087 | 33507 | pcache1EnterMutex(); |
| 33088 | 33508 | pcache1Free(p); |
| 33089 | 33509 | pcache1LeaveMutex(); |
| 33090 | 33510 | } |
| 33091 | 33511 | |
| 33512 | + | |
| 33513 | +/* | |
| 33514 | +** Return true if it desirable to avoid allocating a new page cache | |
| 33515 | +** entry. | |
| 33516 | +** | |
| 33517 | +** If memory was allocated specifically to the page cache using | |
| 33518 | +** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then | |
| 33519 | +** it is desirable to avoid allocating a new page cache entry because | |
| 33520 | +** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient | |
| 33521 | +** for all page cache needs and we should not need to spill the | |
| 33522 | +** allocation onto the heap. | |
| 33523 | +** | |
| 33524 | +** Or, the heap is used for all page cache memory put the heap is | |
| 33525 | +** under memory pressure, then again it is desirable to avoid | |
| 33526 | +** allocating a new page cache entry in order to avoid stressing | |
| 33527 | +** the heap even further. | |
| 33528 | +*/ | |
| 33529 | +static int pcache1UnderMemoryPressure(PCache1 *pCache){ | |
| 33530 | + assert( sqlite3_mutex_held(pcache1.mutex) ); | |
| 33531 | + if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){ | |
| 33532 | + return pcache1.nFreeSlot<pcache1.nReserve; | |
| 33533 | + }else{ | |
| 33534 | + return sqlite3HeapNearlyFull(); | |
| 33535 | + } | |
| 33536 | +} | |
| 33537 | + | |
| 33092 | 33538 | /******************************************************************************/ |
| 33093 | 33539 | /******** General Implementation Functions ************************************/ |
| 33094 | 33540 | |
| 33095 | 33541 | /* |
| 33096 | 33542 | ** This function is used to resize the hash table used by the cache passed |
| @@ -33328,18 +33774,20 @@ | ||
| 33328 | 33774 | ** copy of the requested page. If one is found, it is returned. |
| 33329 | 33775 | ** |
| 33330 | 33776 | ** 2. If createFlag==0 and the page is not already in the cache, NULL is |
| 33331 | 33777 | ** returned. |
| 33332 | 33778 | ** |
| 33333 | -** 3. If createFlag is 1, and the page is not already in the cache, | |
| 33334 | -** and if either of the following are true, return NULL: | |
| 33779 | +** 3. If createFlag is 1, and the page is not already in the cache, then | |
| 33780 | +** return NULL (do not allocate a new page) if any of the following | |
| 33781 | +** conditions are true: | |
| 33335 | 33782 | ** |
| 33336 | 33783 | ** (a) the number of pages pinned by the cache is greater than |
| 33337 | 33784 | ** PCache1.nMax, or |
| 33785 | +** | |
| 33338 | 33786 | ** (b) the number of pages pinned by the cache is greater than |
| 33339 | 33787 | ** the sum of nMax for all purgeable caches, less the sum of |
| 33340 | -** nMin for all other purgeable caches. | |
| 33788 | +** nMin for all other purgeable caches, or | |
| 33341 | 33789 | ** |
| 33342 | 33790 | ** 4. If none of the first three conditions apply and the cache is marked |
| 33343 | 33791 | ** as purgeable, and if one of the following is true: |
| 33344 | 33792 | ** |
| 33345 | 33793 | ** (a) The number of pages allocated for the cache is already |
| @@ -33346,10 +33794,13 @@ | ||
| 33346 | 33794 | ** PCache1.nMax, or |
| 33347 | 33795 | ** |
| 33348 | 33796 | ** (b) The number of pages allocated for all purgeable caches is |
| 33349 | 33797 | ** already equal to or greater than the sum of nMax for all |
| 33350 | 33798 | ** purgeable caches, |
| 33799 | +** | |
| 33800 | +** (c) The system is under memory pressure and wants to avoid | |
| 33801 | +** unnecessary pages cache entry allocations | |
| 33351 | 33802 | ** |
| 33352 | 33803 | ** then attempt to recycle a page from the LRU list. If it is the right |
| 33353 | 33804 | ** size, return the recycled buffer. Otherwise, free the buffer and |
| 33354 | 33805 | ** proceed to step 5. |
| 33355 | 33806 | ** |
| @@ -33378,10 +33829,11 @@ | ||
| 33378 | 33829 | /* Step 3 of header comment. */ |
| 33379 | 33830 | nPinned = pCache->nPage - pCache->nRecyclable; |
| 33380 | 33831 | if( createFlag==1 && ( |
| 33381 | 33832 | nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage) |
| 33382 | 33833 | || nPinned>=(pCache->nMax * 9 / 10) |
| 33834 | + || pcache1UnderMemoryPressure(pCache) | |
| 33383 | 33835 | )){ |
| 33384 | 33836 | goto fetch_out; |
| 33385 | 33837 | } |
| 33386 | 33838 | |
| 33387 | 33839 | if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){ |
| @@ -33388,11 +33840,13 @@ | ||
| 33388 | 33840 | goto fetch_out; |
| 33389 | 33841 | } |
| 33390 | 33842 | |
| 33391 | 33843 | /* Step 4. Try to recycle a page buffer if appropriate. */ |
| 33392 | 33844 | if( pCache->bPurgeable && pcache1.pLruTail && ( |
| 33393 | - (pCache->nPage+1>=pCache->nMax) || pcache1.nCurrentPage>=pcache1.nMaxPage | |
| 33845 | + (pCache->nPage+1>=pCache->nMax) | |
| 33846 | + || pcache1.nCurrentPage>=pcache1.nMaxPage | |
| 33847 | + || pcache1UnderMemoryPressure(pCache) | |
| 33394 | 33848 | )){ |
| 33395 | 33849 | pPage = pcache1.pLruTail; |
| 33396 | 33850 | pcache1RemoveFromHash(pPage); |
| 33397 | 33851 | pcache1PinPage(pPage); |
| 33398 | 33852 | if( pPage->pCache->szPage!=pCache->szPage ){ |
| @@ -33531,10 +33985,11 @@ | ||
| 33531 | 33985 | ** |
| 33532 | 33986 | ** Destroy a cache allocated using pcache1Create(). |
| 33533 | 33987 | */ |
| 33534 | 33988 | static void pcache1Destroy(sqlite3_pcache *p){ |
| 33535 | 33989 | PCache1 *pCache = (PCache1 *)p; |
| 33990 | + assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); | |
| 33536 | 33991 | pcache1EnterMutex(); |
| 33537 | 33992 | pcache1TruncateUnsafe(pCache, 0); |
| 33538 | 33993 | pcache1.nMaxPage -= pCache->nMax; |
| 33539 | 33994 | pcache1.nMinPage -= pCache->nMin; |
| 33540 | 33995 | pcache1EnforceMaxPage(); |
| @@ -33578,11 +34033,11 @@ | ||
| 33578 | 34033 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 33579 | 34034 | int nFree = 0; |
| 33580 | 34035 | if( pcache1.pStart==0 ){ |
| 33581 | 34036 | PgHdr1 *p; |
| 33582 | 34037 | pcache1EnterMutex(); |
| 33583 | - while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){ | |
| 34038 | + while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){ | |
| 33584 | 34039 | nFree += pcache1MemSize(PGHDR1_TO_PAGE(p)); |
| 33585 | 34040 | pcache1PinPage(p); |
| 33586 | 34041 | pcache1RemoveFromHash(p); |
| 33587 | 34042 | pcache1FreePage(p); |
| 33588 | 34043 | } |
| @@ -37120,16 +37575,17 @@ | ||
| 37120 | 37575 | ** the duplicate call is harmless. |
| 37121 | 37576 | */ |
| 37122 | 37577 | sqlite3WalEndReadTransaction(pPager->pWal); |
| 37123 | 37578 | |
| 37124 | 37579 | rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed); |
| 37125 | - if( rc==SQLITE_OK && changed ){ | |
| 37580 | + if( rc!=SQLITE_OK || changed ){ | |
| 37126 | 37581 | pager_reset(pPager); |
| 37127 | 37582 | } |
| 37128 | 37583 | |
| 37129 | 37584 | return rc; |
| 37130 | 37585 | } |
| 37586 | +#endif | |
| 37131 | 37587 | |
| 37132 | 37588 | /* |
| 37133 | 37589 | ** This function is called as part of the transition from PAGER_OPEN |
| 37134 | 37590 | ** to PAGER_READER state to determine the size of the database file |
| 37135 | 37591 | ** in pages (assuming the page size currently stored in Pager.pageSize). |
| @@ -37182,11 +37638,11 @@ | ||
| 37182 | 37638 | |
| 37183 | 37639 | *pnPage = nPage; |
| 37184 | 37640 | return SQLITE_OK; |
| 37185 | 37641 | } |
| 37186 | 37642 | |
| 37187 | - | |
| 37643 | +#ifndef SQLITE_OMIT_WAL | |
| 37188 | 37644 | /* |
| 37189 | 37645 | ** Check if the *-wal file that corresponds to the database opened by pPager |
| 37190 | 37646 | ** exists if the database is not empy, or verify that the *-wal file does |
| 37191 | 37647 | ** not exist (by deleting it) if the database file is empty. |
| 37192 | 37648 | ** |
| @@ -38374,10 +38830,17 @@ | ||
| 38374 | 38830 | journalFileSize = ROUND8(sqlite3MemJournalSize()); |
| 38375 | 38831 | } |
| 38376 | 38832 | |
| 38377 | 38833 | /* Set the output variable to NULL in case an error occurs. */ |
| 38378 | 38834 | *ppPager = 0; |
| 38835 | + | |
| 38836 | +#ifndef SQLITE_OMIT_MEMORYDB | |
| 38837 | + if( flags & PAGER_MEMORY ){ | |
| 38838 | + memDb = 1; | |
| 38839 | + zFilename = 0; | |
| 38840 | + } | |
| 38841 | +#endif | |
| 38379 | 38842 | |
| 38380 | 38843 | /* Compute and store the full pathname in an allocated buffer pointed |
| 38381 | 38844 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 38382 | 38845 | ** leave both nPathname and zPathname set to 0. |
| 38383 | 38846 | */ |
| @@ -38385,21 +38848,12 @@ | ||
| 38385 | 38848 | nPathname = pVfs->mxPathname+1; |
| 38386 | 38849 | zPathname = sqlite3Malloc(nPathname*2); |
| 38387 | 38850 | if( zPathname==0 ){ |
| 38388 | 38851 | return SQLITE_NOMEM; |
| 38389 | 38852 | } |
| 38390 | -#ifndef SQLITE_OMIT_MEMORYDB | |
| 38391 | - if( strcmp(zFilename,":memory:")==0 ){ | |
| 38392 | - memDb = 1; | |
| 38393 | - zPathname[0] = 0; | |
| 38394 | - }else | |
| 38395 | -#endif | |
| 38396 | - { | |
| 38397 | - zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ | |
| 38398 | - rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); | |
| 38399 | - } | |
| 38400 | - | |
| 38853 | + zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ | |
| 38854 | + rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); | |
| 38401 | 38855 | nPathname = sqlite3Strlen30(zPathname); |
| 38402 | 38856 | if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ |
| 38403 | 38857 | /* This branch is taken when the journal path required by |
| 38404 | 38858 | ** the database being opened will be more than pVfs->mxPathname |
| 38405 | 38859 | ** bytes in length. This means the database cannot be opened, |
| @@ -38450,34 +38904,31 @@ | ||
| 38450 | 38904 | pPager->zFilename = (char*)(pPtr += journalFileSize); |
| 38451 | 38905 | assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 38452 | 38906 | |
| 38453 | 38907 | /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ |
| 38454 | 38908 | if( zPathname ){ |
| 38909 | + assert( nPathname>0 ); | |
| 38455 | 38910 | pPager->zJournal = (char*)(pPtr += nPathname + 1); |
| 38456 | 38911 | memcpy(pPager->zFilename, zPathname, nPathname); |
| 38457 | 38912 | memcpy(pPager->zJournal, zPathname, nPathname); |
| 38458 | 38913 | memcpy(&pPager->zJournal[nPathname], "-journal", 8); |
| 38459 | - if( pPager->zFilename[0]==0 ){ | |
| 38460 | - pPager->zJournal[0] = 0; | |
| 38461 | - } | |
| 38462 | 38914 | #ifndef SQLITE_OMIT_WAL |
| 38463 | - else{ | |
| 38464 | - pPager->zWal = &pPager->zJournal[nPathname+8+1]; | |
| 38465 | - memcpy(pPager->zWal, zPathname, nPathname); | |
| 38466 | - memcpy(&pPager->zWal[nPathname], "-wal", 4); | |
| 38467 | - } | |
| 38915 | + pPager->zWal = &pPager->zJournal[nPathname+8+1]; | |
| 38916 | + memcpy(pPager->zWal, zPathname, nPathname); | |
| 38917 | + memcpy(&pPager->zWal[nPathname], "-wal", 4); | |
| 38468 | 38918 | #endif |
| 38469 | 38919 | sqlite3_free(zPathname); |
| 38470 | 38920 | } |
| 38471 | 38921 | pPager->pVfs = pVfs; |
| 38472 | 38922 | pPager->vfsFlags = vfsFlags; |
| 38473 | 38923 | |
| 38474 | 38924 | /* Open the pager file. |
| 38475 | 38925 | */ |
| 38476 | - if( zFilename && zFilename[0] && !memDb ){ | |
| 38926 | + if( zFilename && zFilename[0] ){ | |
| 38477 | 38927 | int fout = 0; /* VFS flags returned by xOpen() */ |
| 38478 | 38928 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); |
| 38929 | + assert( !memDb ); | |
| 38479 | 38930 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 38480 | 38931 | |
| 38481 | 38932 | /* If the file was successfully opened for read/write access, |
| 38482 | 38933 | ** choose a default page size in case we have to create the |
| 38483 | 38934 | ** database file. The default page size is the maximum of: |
| @@ -38927,11 +39378,13 @@ | ||
| 38927 | 39378 | |
| 38928 | 39379 | /* If there is a WAL file in the file-system, open this database in WAL |
| 38929 | 39380 | ** mode. Otherwise, the following function call is a no-op. |
| 38930 | 39381 | */ |
| 38931 | 39382 | rc = pagerOpenWalIfPresent(pPager); |
| 39383 | +#ifndef SQLITE_OMIT_WAL | |
| 38932 | 39384 | assert( pPager->pWal==0 || rc==SQLITE_OK ); |
| 39385 | +#endif | |
| 38933 | 39386 | } |
| 38934 | 39387 | |
| 38935 | 39388 | if( pagerUseWal(pPager) ){ |
| 38936 | 39389 | assert( rc==SQLITE_OK ); |
| 38937 | 39390 | rc = pagerBeginReadTransaction(pPager); |
| @@ -43292,11 +43745,11 @@ | ||
| 43292 | 43745 | WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); |
| 43293 | 43746 | if( rc!=SQLITE_OK ){ |
| 43294 | 43747 | return rc; |
| 43295 | 43748 | } |
| 43296 | 43749 | } |
| 43297 | - assert( pWal->szPage==szPage ); | |
| 43750 | + assert( (int)pWal->szPage==szPage ); | |
| 43298 | 43751 | |
| 43299 | 43752 | /* Write the log file. */ |
| 43300 | 43753 | for(p=pList; p; p=p->pDirty){ |
| 43301 | 43754 | u32 nDbsize; /* Db-size field for frame header */ |
| 43302 | 43755 | i64 iOffset; /* Write offset in log file */ |
| @@ -43952,10 +44405,11 @@ | ||
| 43952 | 44405 | MemPage *pPage1; /* First page of the database */ |
| 43953 | 44406 | u8 readOnly; /* True if the underlying file is readonly */ |
| 43954 | 44407 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
| 43955 | 44408 | u8 secureDelete; /* True if secure_delete is enabled */ |
| 43956 | 44409 | u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 44410 | + u8 openFlags; /* Flags to sqlite3BtreeOpen() */ | |
| 43957 | 44411 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43958 | 44412 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 43959 | 44413 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 43960 | 44414 | #endif |
| 43961 | 44415 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| @@ -46202,15 +46656,24 @@ | ||
| 46202 | 46656 | |
| 46203 | 46657 | /* |
| 46204 | 46658 | ** Open a database file. |
| 46205 | 46659 | ** |
| 46206 | 46660 | ** zFilename is the name of the database file. If zFilename is NULL |
| 46207 | -** a new database with a random name is created. This randomly named | |
| 46208 | -** database file will be deleted when sqlite3BtreeClose() is called. | |
| 46661 | +** then an ephemeral database is created. The ephemeral database might | |
| 46662 | +** be exclusively in memory, or it might use a disk-based memory cache. | |
| 46663 | +** Either way, the ephemeral database will be automatically deleted | |
| 46664 | +** when sqlite3BtreeClose() is called. | |
| 46665 | +** | |
| 46209 | 46666 | ** If zFilename is ":memory:" then an in-memory database is created |
| 46210 | 46667 | ** that is automatically destroyed when it is closed. |
| 46211 | 46668 | ** |
| 46669 | +** The "flags" parameter is a bitmask that might contain bits | |
| 46670 | +** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK. The BTREE_NO_READLOCK | |
| 46671 | +** bit is also set if the SQLITE_NoReadlock flags is set in db->flags. | |
| 46672 | +** These flags are passed through into sqlite3PagerOpen() and must | |
| 46673 | +** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK. | |
| 46674 | +** | |
| 46212 | 46675 | ** If the database is already opened in the same database connection |
| 46213 | 46676 | ** and we are in shared cache mode, then the open will fail with an |
| 46214 | 46677 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 46215 | 46678 | ** objects in the same database connection since doing so will lead |
| 46216 | 46679 | ** to problems with locking. |
| @@ -46227,10 +46690,13 @@ | ||
| 46227 | 46690 | Btree *p; /* Handle to return */ |
| 46228 | 46691 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 46229 | 46692 | int rc = SQLITE_OK; /* Result code from this function */ |
| 46230 | 46693 | u8 nReserve; /* Byte of unused space on each page */ |
| 46231 | 46694 | unsigned char zDbHeader[100]; /* Database header content */ |
| 46695 | + | |
| 46696 | + /* True if opening an ephemeral, temporary database */ | |
| 46697 | + const int isTempDb = zFilename==0 || zFilename[0]==0; | |
| 46232 | 46698 | |
| 46233 | 46699 | /* Set the variable isMemdb to true for an in-memory database, or |
| 46234 | 46700 | ** false for a file-based database. This symbol is only required if |
| 46235 | 46701 | ** either of the shared-data or autovacuum features are compiled |
| 46236 | 46702 | ** into the library. |
| @@ -46237,17 +46703,34 @@ | ||
| 46237 | 46703 | */ |
| 46238 | 46704 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 46239 | 46705 | #ifdef SQLITE_OMIT_MEMORYDB |
| 46240 | 46706 | const int isMemdb = 0; |
| 46241 | 46707 | #else |
| 46242 | - const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); | |
| 46708 | + const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) | |
| 46709 | + || (isTempDb && sqlite3TempInMemory(db)); | |
| 46243 | 46710 | #endif |
| 46244 | 46711 | #endif |
| 46245 | 46712 | |
| 46246 | 46713 | assert( db!=0 ); |
| 46247 | 46714 | assert( sqlite3_mutex_held(db->mutex) ); |
| 46715 | + assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ | |
| 46248 | 46716 | |
| 46717 | + /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ | |
| 46718 | + assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); | |
| 46719 | + | |
| 46720 | + /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ | |
| 46721 | + assert( (flags & BTREE_SINGLE)==0 || isTempDb ); | |
| 46722 | + | |
| 46723 | + if( db->flags & SQLITE_NoReadlock ){ | |
| 46724 | + flags |= BTREE_NO_READLOCK; | |
| 46725 | + } | |
| 46726 | + if( isMemdb ){ | |
| 46727 | + flags |= BTREE_MEMORY; | |
| 46728 | + } | |
| 46729 | + if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ | |
| 46730 | + vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; | |
| 46731 | + } | |
| 46249 | 46732 | pVfs = db->pVfs; |
| 46250 | 46733 | p = sqlite3MallocZero(sizeof(Btree)); |
| 46251 | 46734 | if( !p ){ |
| 46252 | 46735 | return SQLITE_NOMEM; |
| 46253 | 46736 | } |
| @@ -46261,11 +46744,11 @@ | ||
| 46261 | 46744 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 46262 | 46745 | /* |
| 46263 | 46746 | ** If this Btree is a candidate for shared cache, try to find an |
| 46264 | 46747 | ** existing BtShared object that we can share with |
| 46265 | 46748 | */ |
| 46266 | - if( isMemdb==0 && zFilename && zFilename[0] ){ | |
| 46749 | + if( isMemdb==0 && isTempDb==0 ){ | |
| 46267 | 46750 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
| 46268 | 46751 | int nFullPathname = pVfs->mxPathname+1; |
| 46269 | 46752 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
| 46270 | 46753 | sqlite3_mutex *mutexShared; |
| 46271 | 46754 | p->sharable = 1; |
| @@ -46336,10 +46819,11 @@ | ||
| 46336 | 46819 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 46337 | 46820 | } |
| 46338 | 46821 | if( rc!=SQLITE_OK ){ |
| 46339 | 46822 | goto btree_open_out; |
| 46340 | 46823 | } |
| 46824 | + pBt->openFlags = (u8)flags; | |
| 46341 | 46825 | pBt->db = db; |
| 46342 | 46826 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
| 46343 | 46827 | p->pBt = pBt; |
| 46344 | 46828 | |
| 46345 | 46829 | pBt->pCursor = 0; |
| @@ -46440,10 +46924,18 @@ | ||
| 46440 | 46924 | sqlite3PagerClose(pBt->pPager); |
| 46441 | 46925 | } |
| 46442 | 46926 | sqlite3_free(pBt); |
| 46443 | 46927 | sqlite3_free(p); |
| 46444 | 46928 | *ppBtree = 0; |
| 46929 | + }else{ | |
| 46930 | + /* If the B-Tree was successfully opened, set the pager-cache size to the | |
| 46931 | + ** default value. Except, when opening on an existing shared pager-cache, | |
| 46932 | + ** do not change the pager-cache size. | |
| 46933 | + */ | |
| 46934 | + if( sqlite3BtreeSchema(p, 0, 0)==0 ){ | |
| 46935 | + sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); | |
| 46936 | + } | |
| 46445 | 46937 | } |
| 46446 | 46938 | if( mutexOpen ){ |
| 46447 | 46939 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 46448 | 46940 | sqlite3_mutex_leave(mutexOpen); |
| 46449 | 46941 | } |
| @@ -51390,15 +51882,16 @@ | ||
| 51390 | 51882 | ** flags might not work: |
| 51391 | 51883 | ** |
| 51392 | 51884 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 51393 | 51885 | ** BTREE_ZERODATA Used for SQL indices |
| 51394 | 51886 | */ |
| 51395 | -static int btreeCreateTable(Btree *p, int *piTable, int flags){ | |
| 51887 | +static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ | |
| 51396 | 51888 | BtShared *pBt = p->pBt; |
| 51397 | 51889 | MemPage *pRoot; |
| 51398 | 51890 | Pgno pgnoRoot; |
| 51399 | 51891 | int rc; |
| 51892 | + int ptfFlags; /* Page-type flage for the root page of new table */ | |
| 51400 | 51893 | |
| 51401 | 51894 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 51402 | 51895 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 51403 | 51896 | assert( !pBt->readOnly ); |
| 51404 | 51897 | |
| @@ -51513,12 +52006,18 @@ | ||
| 51513 | 52006 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
| 51514 | 52007 | if( rc ) return rc; |
| 51515 | 52008 | } |
| 51516 | 52009 | #endif |
| 51517 | 52010 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 51518 | - zeroPage(pRoot, flags | PTF_LEAF); | |
| 52011 | + if( createTabFlags & BTREE_INTKEY ){ | |
| 52012 | + ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; | |
| 52013 | + }else{ | |
| 52014 | + ptfFlags = PTF_ZERODATA | PTF_LEAF; | |
| 52015 | + } | |
| 52016 | + zeroPage(pRoot, ptfFlags); | |
| 51519 | 52017 | sqlite3PagerUnref(pRoot->pDbPage); |
| 52018 | + assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); | |
| 51520 | 52019 | *piTable = (int)pgnoRoot; |
| 51521 | 52020 | return SQLITE_OK; |
| 51522 | 52021 | } |
| 51523 | 52022 | SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 51524 | 52023 | int rc; |
| @@ -52774,11 +53273,14 @@ | ||
| 52774 | 53273 | sqlite3Error( |
| 52775 | 53274 | pDestDb, SQLITE_ERROR, "source and destination must be distinct" |
| 52776 | 53275 | ); |
| 52777 | 53276 | p = 0; |
| 52778 | 53277 | }else { |
| 52779 | - /* Allocate space for a new sqlite3_backup object */ | |
| 53278 | + /* Allocate space for a new sqlite3_backup object... | |
| 53279 | + ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | |
| 53280 | + ** call to sqlite3_backup_init() and is destroyed by a call to | |
| 53281 | + ** sqlite3_backup_finish(). */ | |
| 52780 | 53282 | p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); |
| 52781 | 53283 | if( !p ){ |
| 52782 | 53284 | sqlite3Error(pDestDb, SQLITE_NOMEM, 0); |
| 52783 | 53285 | } |
| 52784 | 53286 | } |
| @@ -53157,10 +53659,13 @@ | ||
| 53157 | 53659 | if( p->pDestDb ){ |
| 53158 | 53660 | sqlite3_mutex_leave(p->pDestDb->mutex); |
| 53159 | 53661 | } |
| 53160 | 53662 | sqlite3BtreeLeave(p->pSrc); |
| 53161 | 53663 | if( p->pDestDb ){ |
| 53664 | + /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | |
| 53665 | + ** call to sqlite3_backup_init() and is destroyed by a call to | |
| 53666 | + ** sqlite3_backup_finish(). */ | |
| 53162 | 53667 | sqlite3_free(p); |
| 53163 | 53668 | } |
| 53164 | 53669 | sqlite3_mutex_leave(mutex); |
| 53165 | 53670 | return rc; |
| 53166 | 53671 | } |
| @@ -53408,10 +53913,13 @@ | ||
| 53408 | 53913 | return SQLITE_NOMEM; |
| 53409 | 53914 | } |
| 53410 | 53915 | pMem->z[pMem->n] = 0; |
| 53411 | 53916 | pMem->z[pMem->n+1] = 0; |
| 53412 | 53917 | pMem->flags |= MEM_Term; |
| 53918 | +#ifdef SQLITE_DEBUG | |
| 53919 | + pMem->pScopyFrom = 0; | |
| 53920 | +#endif | |
| 53413 | 53921 | } |
| 53414 | 53922 | |
| 53415 | 53923 | return SQLITE_OK; |
| 53416 | 53924 | } |
| 53417 | 53925 | |
| @@ -53528,11 +54036,11 @@ | ||
| 53528 | 54036 | memset(&ctx, 0, sizeof(ctx)); |
| 53529 | 54037 | ctx.s.flags = MEM_Null; |
| 53530 | 54038 | ctx.s.db = pMem->db; |
| 53531 | 54039 | ctx.pMem = pMem; |
| 53532 | 54040 | ctx.pFunc = pFunc; |
| 53533 | - pFunc->xFinalize(&ctx); | |
| 54041 | + pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ | |
| 53534 | 54042 | assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); |
| 53535 | 54043 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 53536 | 54044 | memcpy(pMem, &ctx.s, sizeof(ctx.s)); |
| 53537 | 54045 | rc = ctx.isError; |
| 53538 | 54046 | } |
| @@ -53869,10 +54377,32 @@ | ||
| 53869 | 54377 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 53870 | 54378 | } |
| 53871 | 54379 | return 0; |
| 53872 | 54380 | } |
| 53873 | 54381 | |
| 54382 | +#ifdef SQLITE_DEBUG | |
| 54383 | +/* | |
| 54384 | +** This routine prepares a memory cell for modication by breaking | |
| 54385 | +** its link to a shallow copy and by marking any current shallow | |
| 54386 | +** copies of this cell as invalid. | |
| 54387 | +** | |
| 54388 | +** This is used for testing and debugging only - to make sure shallow | |
| 54389 | +** copies are not misused. | |
| 54390 | +*/ | |
| 54391 | +SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){ | |
| 54392 | + int i; | |
| 54393 | + Mem *pX; | |
| 54394 | + for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){ | |
| 54395 | + if( pX->pScopyFrom==pMem ){ | |
| 54396 | + pX->flags |= MEM_Invalid; | |
| 54397 | + pX->pScopyFrom = 0; | |
| 54398 | + } | |
| 54399 | + } | |
| 54400 | + pMem->pScopyFrom = 0; | |
| 54401 | +} | |
| 54402 | +#endif /* SQLITE_DEBUG */ | |
| 54403 | + | |
| 53874 | 54404 | /* |
| 53875 | 54405 | ** Size of struct Mem not including the Mem.zMalloc member. |
| 53876 | 54406 | */ |
| 53877 | 54407 | #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) |
| 53878 | 54408 | |
| @@ -54237,11 +54767,11 @@ | ||
| 54237 | 54767 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); |
| 54238 | 54768 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ |
| 54239 | 54769 | return 0; |
| 54240 | 54770 | } |
| 54241 | 54771 | } |
| 54242 | - sqlite3VdbeMemNulTerminate(pVal); | |
| 54772 | + sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */ | |
| 54243 | 54773 | }else{ |
| 54244 | 54774 | assert( (pVal->flags&MEM_Blob)==0 ); |
| 54245 | 54775 | sqlite3VdbeMemStringify(pVal, enc); |
| 54246 | 54776 | assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); |
| 54247 | 54777 | } |
| @@ -56152,13 +56682,14 @@ | ||
| 56152 | 56682 | */ |
| 56153 | 56683 | for(i=0; i<db->nDb; i++){ |
| 56154 | 56684 | Btree *pBt = db->aDb[i].pBt; |
| 56155 | 56685 | if( sqlite3BtreeIsInTrans(pBt) ){ |
| 56156 | 56686 | char const *zFile = sqlite3BtreeGetJournalname(pBt); |
| 56157 | - if( zFile==0 || zFile[0]==0 ){ | |
| 56687 | + if( zFile==0 ){ | |
| 56158 | 56688 | continue; /* Ignore TEMP and :memory: databases */ |
| 56159 | 56689 | } |
| 56690 | + assert( zFile[0]!=0 ); | |
| 56160 | 56691 | if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
| 56161 | 56692 | needSync = 1; |
| 56162 | 56693 | } |
| 56163 | 56694 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 56164 | 56695 | offset += sqlite3Strlen30(zFile)+1; |
| @@ -57618,10 +58149,12 @@ | ||
| 57618 | 58149 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 57619 | 58150 | */ |
| 57620 | 58151 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 57621 | 58152 | int rc; |
| 57622 | 58153 | if( pStmt==0 ){ |
| 58154 | + /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL | |
| 58155 | + ** pointer is a harmless no-op. */ | |
| 57623 | 58156 | rc = SQLITE_OK; |
| 57624 | 58157 | }else{ |
| 57625 | 58158 | Vdbe *v = (Vdbe*)pStmt; |
| 57626 | 58159 | sqlite3 *db = v->db; |
| 57627 | 58160 | #if SQLITE_THREADSAFE |
| @@ -57694,11 +58227,11 @@ | ||
| 57694 | 58227 | Mem *p = (Mem*)pVal; |
| 57695 | 58228 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 57696 | 58229 | sqlite3VdbeMemExpandBlob(p); |
| 57697 | 58230 | p->flags &= ~MEM_Str; |
| 57698 | 58231 | p->flags |= MEM_Blob; |
| 57699 | - return p->z; | |
| 58232 | + return p->n ? p->z : 0; | |
| 57700 | 58233 | }else{ |
| 57701 | 58234 | return sqlite3_value_text(pVal); |
| 57702 | 58235 | } |
| 57703 | 58236 | } |
| 57704 | 58237 | SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){ |
| @@ -58048,10 +58581,16 @@ | ||
| 58048 | 58581 | } |
| 58049 | 58582 | |
| 58050 | 58583 | /* |
| 58051 | 58584 | ** Extract the user data from a sqlite3_context structure and return a |
| 58052 | 58585 | ** pointer to it. |
| 58586 | +** | |
| 58587 | +** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface | |
| 58588 | +** returns a copy of the pointer to the database connection (the 1st | |
| 58589 | +** parameter) of the sqlite3_create_function() and | |
| 58590 | +** sqlite3_create_function16() routines that originally registered the | |
| 58591 | +** application defined function. | |
| 58053 | 58592 | */ |
| 58054 | 58593 | SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
| 58055 | 58594 | assert( p && p->pFunc ); |
| 58056 | 58595 | return p->s.db; |
| 58057 | 58596 | } |
| @@ -58257,12 +58796,11 @@ | ||
| 58257 | 58796 | ** sqlite3_column_text() |
| 58258 | 58797 | ** sqlite3_column_text16() |
| 58259 | 58798 | ** sqlite3_column_real() |
| 58260 | 58799 | ** sqlite3_column_bytes() |
| 58261 | 58800 | ** sqlite3_column_bytes16() |
| 58262 | -** | |
| 58263 | -** But not for sqlite3_column_blob(), which never calls malloc(). | |
| 58801 | +** sqiite3_column_blob() | |
| 58264 | 58802 | */ |
| 58265 | 58803 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 58266 | 58804 | { |
| 58267 | 58805 | /* If malloc() failed during an encoding conversion within an |
| 58268 | 58806 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| @@ -58526,10 +59064,16 @@ | ||
| 58526 | 59064 | pVar->flags = MEM_Null; |
| 58527 | 59065 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 58528 | 59066 | |
| 58529 | 59067 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 58530 | 59068 | ** binding a new value to this variable invalidates the current query plan. |
| 59069 | + ** | |
| 59070 | + ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host | |
| 59071 | + ** parameter in the WHERE clause might influence the choice of query plan | |
| 59072 | + ** for a statement, then the statement will be automatically recompiled, | |
| 59073 | + ** as if there had been a schema change, on the first sqlite3_step() call | |
| 59074 | + ** following any change to the bindings of that parameter. | |
| 58531 | 59075 | */ |
| 58532 | 59076 | if( p->isPrepareV2 && |
| 58533 | 59077 | ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) |
| 58534 | 59078 | ){ |
| 58535 | 59079 | p->expired = 1; |
| @@ -59023,10 +59567,21 @@ | ||
| 59023 | 59567 | ** of the code in this file is, therefore, important. See other comments |
| 59024 | 59568 | ** in this file for details. If in doubt, do not deviate from existing |
| 59025 | 59569 | ** commenting and indentation practices when changing or adding code. |
| 59026 | 59570 | */ |
| 59027 | 59571 | |
| 59572 | +/* | |
| 59573 | +** Invoke this macro on memory cells just prior to changing the | |
| 59574 | +** value of the cell. This macro verifies that shallow copies are | |
| 59575 | +** not misused. | |
| 59576 | +*/ | |
| 59577 | +#ifdef SQLITE_DEBUG | |
| 59578 | +# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M) | |
| 59579 | +#else | |
| 59580 | +# define memAboutToChange(P,M) | |
| 59581 | +#endif | |
| 59582 | + | |
| 59028 | 59583 | /* |
| 59029 | 59584 | ** The following global variable is incremented every time a cursor |
| 59030 | 59585 | ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test |
| 59031 | 59586 | ** procedures use this information to make sure that indices are |
| 59032 | 59587 | ** working correctly. This variable has no function other than to |
| @@ -60132,38 +60687,44 @@ | ||
| 60132 | 60687 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
| 60133 | 60688 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 60134 | 60689 | assert( pOp->p2>0 ); |
| 60135 | 60690 | assert( pOp->p2<=p->nMem ); |
| 60136 | 60691 | pOut = &aMem[pOp->p2]; |
| 60692 | + memAboutToChange(p, pOut); | |
| 60137 | 60693 | sqlite3VdbeMemReleaseExternal(pOut); |
| 60138 | 60694 | pOut->flags = MEM_Int; |
| 60139 | 60695 | } |
| 60140 | 60696 | |
| 60141 | 60697 | /* Sanity checking on other operands */ |
| 60142 | 60698 | #ifdef SQLITE_DEBUG |
| 60143 | 60699 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 60144 | 60700 | assert( pOp->p1>0 ); |
| 60145 | 60701 | assert( pOp->p1<=p->nMem ); |
| 60702 | + assert( memIsValid(&aMem[pOp->p1]) ); | |
| 60146 | 60703 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 60147 | 60704 | } |
| 60148 | 60705 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 60149 | 60706 | assert( pOp->p2>0 ); |
| 60150 | 60707 | assert( pOp->p2<=p->nMem ); |
| 60708 | + assert( memIsValid(&aMem[pOp->p2]) ); | |
| 60151 | 60709 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 60152 | 60710 | } |
| 60153 | 60711 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 60154 | 60712 | assert( pOp->p3>0 ); |
| 60155 | 60713 | assert( pOp->p3<=p->nMem ); |
| 60714 | + assert( memIsValid(&aMem[pOp->p3]) ); | |
| 60156 | 60715 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 60157 | 60716 | } |
| 60158 | 60717 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 60159 | 60718 | assert( pOp->p2>0 ); |
| 60160 | 60719 | assert( pOp->p2<=p->nMem ); |
| 60720 | + memAboutToChange(p, &aMem[pOp->p2]); | |
| 60161 | 60721 | } |
| 60162 | 60722 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 60163 | 60723 | assert( pOp->p3>0 ); |
| 60164 | 60724 | assert( pOp->p3<=p->nMem ); |
| 60725 | + memAboutToChange(p, &aMem[pOp->p3]); | |
| 60165 | 60726 | } |
| 60166 | 60727 | #endif |
| 60167 | 60728 | |
| 60168 | 60729 | switch( pOp->opcode ){ |
| 60169 | 60730 | |
| @@ -60221,10 +60782,11 @@ | ||
| 60221 | 60782 | ** and then jump to address P2. |
| 60222 | 60783 | */ |
| 60223 | 60784 | case OP_Gosub: { /* jump, in1 */ |
| 60224 | 60785 | pIn1 = &aMem[pOp->p1]; |
| 60225 | 60786 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 60787 | + memAboutToChange(p, pIn1); | |
| 60226 | 60788 | pIn1->flags = MEM_Int; |
| 60227 | 60789 | pIn1->u.i = pc; |
| 60228 | 60790 | REGISTER_TRACE(pOp->p1, pIn1); |
| 60229 | 60791 | pc = pOp->p2 - 1; |
| 60230 | 60792 | break; |
| @@ -60428,15 +60990,11 @@ | ||
| 60428 | 60990 | |
| 60429 | 60991 | |
| 60430 | 60992 | /* Opcode: Blob P1 P2 * P4 |
| 60431 | 60993 | ** |
| 60432 | 60994 | ** P4 points to a blob of data P1 bytes long. Store this |
| 60433 | -** blob in register P2. This instruction is not coded directly | |
| 60434 | -** by the compiler. Instead, the compiler layer specifies | |
| 60435 | -** an OP_HexBlob opcode, with the hex string representation of | |
| 60436 | -** the blob as P4. This opcode is transformed to an OP_Blob | |
| 60437 | -** the first time it is executed. | |
| 60995 | +** blob in register P2. | |
| 60438 | 60996 | */ |
| 60439 | 60997 | case OP_Blob: { /* out2-prerelease */ |
| 60440 | 60998 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
| 60441 | 60999 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 60442 | 61000 | pOut->enc = encoding; |
| @@ -60490,10 +61048,12 @@ | ||
| 60490 | 61048 | pIn1 = &aMem[u.ac.p1]; |
| 60491 | 61049 | pOut = &aMem[u.ac.p2]; |
| 60492 | 61050 | while( u.ac.n-- ){ |
| 60493 | 61051 | assert( pOut<=&aMem[p->nMem] ); |
| 60494 | 61052 | assert( pIn1<=&aMem[p->nMem] ); |
| 61053 | + assert( memIsValid(pIn1) ); | |
| 61054 | + memAboutToChange(p, pOut); | |
| 60495 | 61055 | u.ac.zMalloc = pOut->zMalloc; |
| 60496 | 61056 | pOut->zMalloc = 0; |
| 60497 | 61057 | sqlite3VdbeMemMove(pOut, pIn1); |
| 60498 | 61058 | pIn1->zMalloc = u.ac.zMalloc; |
| 60499 | 61059 | REGISTER_TRACE(u.ac.p2++, pOut); |
| @@ -60535,10 +61095,13 @@ | ||
| 60535 | 61095 | case OP_SCopy: { /* in1, out2 */ |
| 60536 | 61096 | pIn1 = &aMem[pOp->p1]; |
| 60537 | 61097 | pOut = &aMem[pOp->p2]; |
| 60538 | 61098 | assert( pOut!=pIn1 ); |
| 60539 | 61099 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 61100 | +#ifdef SQLITE_DEBUG | |
| 61101 | + if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; | |
| 61102 | +#endif | |
| 60540 | 61103 | REGISTER_TRACE(pOp->p2, pOut); |
| 60541 | 61104 | break; |
| 60542 | 61105 | } |
| 60543 | 61106 | |
| 60544 | 61107 | /* Opcode: ResultRow P1 P2 * * * |
| @@ -60595,10 +61158,14 @@ | ||
| 60595 | 61158 | ** and have an assigned type. The results are de-ephemeralized as |
| 60596 | 61159 | ** as side effect. |
| 60597 | 61160 | */ |
| 60598 | 61161 | u.ad.pMem = p->pResultSet = &aMem[pOp->p1]; |
| 60599 | 61162 | for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){ |
| 61163 | + assert( memIsValid(&u.ad.pMem[u.ad.i]) ); | |
| 61164 | + Deephemeralize(&u.ad.pMem[u.ad.i]); | |
| 61165 | + assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0 | |
| 61166 | + || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 ); | |
| 60600 | 61167 | sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]); |
| 60601 | 61168 | sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]); |
| 60602 | 61169 | REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]); |
| 60603 | 61170 | } |
| 60604 | 61171 | if( db->mallocFailed ) goto no_mem; |
| @@ -60826,16 +61393,21 @@ | ||
| 60826 | 61393 | #endif /* local variables moved into u.ag */ |
| 60827 | 61394 | |
| 60828 | 61395 | u.ag.n = pOp->p5; |
| 60829 | 61396 | u.ag.apVal = p->apArg; |
| 60830 | 61397 | assert( u.ag.apVal || u.ag.n==0 ); |
| 61398 | + assert( pOp->p3>0 && pOp->p3<=p->nMem ); | |
| 61399 | + pOut = &aMem[pOp->p3]; | |
| 61400 | + memAboutToChange(p, pOut); | |
| 60831 | 61401 | |
| 60832 | 61402 | assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) ); |
| 60833 | 61403 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n ); |
| 60834 | 61404 | u.ag.pArg = &aMem[pOp->p2]; |
| 60835 | 61405 | for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){ |
| 61406 | + assert( memIsValid(u.ag.pArg) ); | |
| 60836 | 61407 | u.ag.apVal[u.ag.i] = u.ag.pArg; |
| 61408 | + Deephemeralize(u.ag.pArg); | |
| 60837 | 61409 | sqlite3VdbeMemStoreType(u.ag.pArg); |
| 60838 | 61410 | REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg); |
| 60839 | 61411 | } |
| 60840 | 61412 | |
| 60841 | 61413 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| @@ -60845,12 +61417,10 @@ | ||
| 60845 | 61417 | }else{ |
| 60846 | 61418 | u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
| 60847 | 61419 | u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc; |
| 60848 | 61420 | } |
| 60849 | 61421 | |
| 60850 | - assert( pOp->p3>0 && pOp->p3<=p->nMem ); | |
| 60851 | - pOut = &aMem[pOp->p3]; | |
| 60852 | 61422 | u.ag.ctx.s.flags = MEM_Null; |
| 60853 | 61423 | u.ag.ctx.s.db = db; |
| 60854 | 61424 | u.ag.ctx.s.xDel = 0; |
| 60855 | 61425 | u.ag.ctx.s.zMalloc = 0; |
| 60856 | 61426 | |
| @@ -60866,11 +61436,11 @@ | ||
| 60866 | 61436 | assert( pOp>aOp ); |
| 60867 | 61437 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 60868 | 61438 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 60869 | 61439 | u.ag.ctx.pColl = pOp[-1].p4.pColl; |
| 60870 | 61440 | } |
| 60871 | - (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); | |
| 61441 | + (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */ | |
| 60872 | 61442 | if( db->mallocFailed ){ |
| 60873 | 61443 | /* Even though a malloc() has failed, the implementation of the |
| 60874 | 61444 | ** user function may have called an sqlite3_result_XXX() function |
| 60875 | 61445 | ** to return a value. The following call releases any resources |
| 60876 | 61446 | ** associated with such a value. |
| @@ -60918,11 +61488,11 @@ | ||
| 60918 | 61488 | ** If either input is NULL, the result is NULL. |
| 60919 | 61489 | */ |
| 60920 | 61490 | /* Opcode: ShiftLeft P1 P2 P3 * * |
| 60921 | 61491 | ** |
| 60922 | 61492 | ** Shift the integer value in register P2 to the left by the |
| 60923 | -** number of bits specified by the integer in regiser P1. | |
| 61493 | +** number of bits specified by the integer in register P1. | |
| 60924 | 61494 | ** Store the result in register P3. |
| 60925 | 61495 | ** If either input is NULL, the result is NULL. |
| 60926 | 61496 | */ |
| 60927 | 61497 | /* Opcode: ShiftRight P1 P2 P3 * * |
| 60928 | 61498 | ** |
| @@ -60968,10 +61538,11 @@ | ||
| 60968 | 61538 | ** |
| 60969 | 61539 | ** To force any register to be an integer, just add 0. |
| 60970 | 61540 | */ |
| 60971 | 61541 | case OP_AddImm: { /* in1 */ |
| 60972 | 61542 | pIn1 = &aMem[pOp->p1]; |
| 61543 | + memAboutToChange(p, pIn1); | |
| 60973 | 61544 | sqlite3VdbeMemIntegerify(pIn1); |
| 60974 | 61545 | pIn1->u.i += pOp->p2; |
| 60975 | 61546 | break; |
| 60976 | 61547 | } |
| 60977 | 61548 | |
| @@ -60982,10 +61553,11 @@ | ||
| 60982 | 61553 | ** without data loss, then jump immediately to P2, or if P2==0 |
| 60983 | 61554 | ** raise an SQLITE_MISMATCH exception. |
| 60984 | 61555 | */ |
| 60985 | 61556 | case OP_MustBeInt: { /* jump, in1 */ |
| 60986 | 61557 | pIn1 = &aMem[pOp->p1]; |
| 61558 | + memAboutToChange(p, pIn1); | |
| 60987 | 61559 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 60988 | 61560 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 60989 | 61561 | if( pOp->p2==0 ){ |
| 60990 | 61562 | rc = SQLITE_MISMATCH; |
| 60991 | 61563 | goto abort_due_to_error; |
| @@ -61008,10 +61580,11 @@ | ||
| 61008 | 61580 | ** integers, for space efficiency, but after extraction we want them |
| 61009 | 61581 | ** to have only a real value. |
| 61010 | 61582 | */ |
| 61011 | 61583 | case OP_RealAffinity: { /* in1 */ |
| 61012 | 61584 | pIn1 = &aMem[pOp->p1]; |
| 61585 | + memAboutToChange(p, pIn1); | |
| 61013 | 61586 | if( pIn1->flags & MEM_Int ){ |
| 61014 | 61587 | sqlite3VdbeMemRealify(pIn1); |
| 61015 | 61588 | } |
| 61016 | 61589 | break; |
| 61017 | 61590 | } |
| @@ -61027,10 +61600,11 @@ | ||
| 61027 | 61600 | ** |
| 61028 | 61601 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61029 | 61602 | */ |
| 61030 | 61603 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
| 61031 | 61604 | pIn1 = &aMem[pOp->p1]; |
| 61605 | + memAboutToChange(p, pIn1); | |
| 61032 | 61606 | if( pIn1->flags & MEM_Null ) break; |
| 61033 | 61607 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 61034 | 61608 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 61035 | 61609 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61036 | 61610 | rc = ExpandBlob(pIn1); |
| @@ -61049,10 +61623,11 @@ | ||
| 61049 | 61623 | ** |
| 61050 | 61624 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61051 | 61625 | */ |
| 61052 | 61626 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
| 61053 | 61627 | pIn1 = &aMem[pOp->p1]; |
| 61628 | + memAboutToChange(p, pIn1); | |
| 61054 | 61629 | if( pIn1->flags & MEM_Null ) break; |
| 61055 | 61630 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 61056 | 61631 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61057 | 61632 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
| 61058 | 61633 | MemSetTypeFlag(pIn1, MEM_Blob); |
| @@ -61073,28 +61648,30 @@ | ||
| 61073 | 61648 | ** |
| 61074 | 61649 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61075 | 61650 | */ |
| 61076 | 61651 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
| 61077 | 61652 | pIn1 = &aMem[pOp->p1]; |
| 61653 | + memAboutToChange(p, pIn1); | |
| 61078 | 61654 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 61079 | 61655 | sqlite3VdbeMemNumerify(pIn1); |
| 61080 | 61656 | } |
| 61081 | 61657 | break; |
| 61082 | 61658 | } |
| 61083 | 61659 | #endif /* SQLITE_OMIT_CAST */ |
| 61084 | 61660 | |
| 61085 | 61661 | /* Opcode: ToInt P1 * * * * |
| 61086 | 61662 | ** |
| 61087 | -** Force the value in register P1 be an integer. If | |
| 61663 | +** Force the value in register P1 to be an integer. If | |
| 61088 | 61664 | ** The value is currently a real number, drop its fractional part. |
| 61089 | 61665 | ** If the value is text or blob, try to convert it to an integer using the |
| 61090 | 61666 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 61091 | 61667 | ** |
| 61092 | 61668 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61093 | 61669 | */ |
| 61094 | 61670 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
| 61095 | 61671 | pIn1 = &aMem[pOp->p1]; |
| 61672 | + memAboutToChange(p, pIn1); | |
| 61096 | 61673 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61097 | 61674 | sqlite3VdbeMemIntegerify(pIn1); |
| 61098 | 61675 | } |
| 61099 | 61676 | break; |
| 61100 | 61677 | } |
| @@ -61109,10 +61686,11 @@ | ||
| 61109 | 61686 | ** |
| 61110 | 61687 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61111 | 61688 | */ |
| 61112 | 61689 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
| 61113 | 61690 | pIn1 = &aMem[pOp->p1]; |
| 61691 | + memAboutToChange(p, pIn1); | |
| 61114 | 61692 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61115 | 61693 | sqlite3VdbeMemRealify(pIn1); |
| 61116 | 61694 | } |
| 61117 | 61695 | break; |
| 61118 | 61696 | } |
| @@ -61123,11 +61701,11 @@ | ||
| 61123 | 61701 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 61124 | 61702 | ** jump to address P2. |
| 61125 | 61703 | ** |
| 61126 | 61704 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 61127 | 61705 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 61128 | -** bit is clear then fall thru if either operand is NULL. | |
| 61706 | +** bit is clear then fall through if either operand is NULL. | |
| 61129 | 61707 | ** |
| 61130 | 61708 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 61131 | 61709 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 61132 | 61710 | ** to coerce both inputs according to this affinity before the |
| 61133 | 61711 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| @@ -61203,10 +61781,12 @@ | ||
| 61203 | 61781 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
| 61204 | 61782 | #endif /* local variables moved into u.ai */ |
| 61205 | 61783 | |
| 61206 | 61784 | pIn1 = &aMem[pOp->p1]; |
| 61207 | 61785 | pIn3 = &aMem[pOp->p3]; |
| 61786 | + memAboutToChange(p, pIn1); | |
| 61787 | + memAboutToChange(p, pIn3); | |
| 61208 | 61788 | u.ai.flags1 = pIn1->flags; |
| 61209 | 61789 | u.ai.flags3 = pIn3->flags; |
| 61210 | 61790 | if( (pIn1->flags | pIn3->flags)&MEM_Null ){ |
| 61211 | 61791 | /* One or both operands are NULL */ |
| 61212 | 61792 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| @@ -61253,10 +61833,11 @@ | ||
| 61253 | 61833 | default: u.ai.res = u.ai.res>=0; break; |
| 61254 | 61834 | } |
| 61255 | 61835 | |
| 61256 | 61836 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 61257 | 61837 | pOut = &aMem[pOp->p2]; |
| 61838 | + memAboutToChange(p, pOut); | |
| 61258 | 61839 | MemSetTypeFlag(pOut, MEM_Int); |
| 61259 | 61840 | pOut->u.i = u.ai.res; |
| 61260 | 61841 | REGISTER_TRACE(pOp->p2, pOut); |
| 61261 | 61842 | }else if( u.ai.res ){ |
| 61262 | 61843 | pc = pOp->p2-1; |
| @@ -61284,12 +61865,12 @@ | ||
| 61284 | 61865 | break; |
| 61285 | 61866 | } |
| 61286 | 61867 | |
| 61287 | 61868 | /* Opcode: Compare P1 P2 P3 P4 * |
| 61288 | 61869 | ** |
| 61289 | -** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this | |
| 61290 | -** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of | |
| 61870 | +** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this | |
| 61871 | +** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of | |
| 61291 | 61872 | ** the comparison for use by the next OP_Jump instruct. |
| 61292 | 61873 | ** |
| 61293 | 61874 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 61294 | 61875 | ** orders for the comparison. The permutation applies to registers |
| 61295 | 61876 | ** only. The KeyInfo elements are used sequentially. |
| @@ -61327,10 +61908,12 @@ | ||
| 61327 | 61908 | assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 ); |
| 61328 | 61909 | } |
| 61329 | 61910 | #endif /* SQLITE_DEBUG */ |
| 61330 | 61911 | for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){ |
| 61331 | 61912 | u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i; |
| 61913 | + assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) ); | |
| 61914 | + assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) ); | |
| 61332 | 61915 | REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]); |
| 61333 | 61916 | REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]); |
| 61334 | 61917 | assert( u.aj.i<u.aj.pKeyInfo->nField ); |
| 61335 | 61918 | u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i]; |
| 61336 | 61919 | u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i]; |
| @@ -61558,10 +62141,11 @@ | ||
| 61558 | 62141 | u.am.pC = 0; |
| 61559 | 62142 | memset(&u.am.sMem, 0, sizeof(u.am.sMem)); |
| 61560 | 62143 | assert( u.am.p1<p->nCursor ); |
| 61561 | 62144 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 61562 | 62145 | u.am.pDest = &aMem[pOp->p3]; |
| 62146 | + memAboutToChange(p, u.am.pDest); | |
| 61563 | 62147 | MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 61564 | 62148 | u.am.zRec = 0; |
| 61565 | 62149 | |
| 61566 | 62150 | /* This block sets the variable u.am.payloadSize to be the total number of |
| 61567 | 62151 | ** bytes in the record. |
| @@ -61605,10 +62189,11 @@ | ||
| 61605 | 62189 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 61606 | 62190 | } |
| 61607 | 62191 | }else if( u.am.pC->pseudoTableReg>0 ){ |
| 61608 | 62192 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 61609 | 62193 | assert( u.am.pReg->flags & MEM_Blob ); |
| 62194 | + assert( memIsValid(u.am.pReg) ); | |
| 61610 | 62195 | u.am.payloadSize = u.am.pReg->n; |
| 61611 | 62196 | u.am.zRec = u.am.pReg->z; |
| 61612 | 62197 | u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr; |
| 61613 | 62198 | assert( u.am.payloadSize==0 || u.am.zRec!=0 ); |
| 61614 | 62199 | }else{ |
| @@ -61829,25 +62414,24 @@ | ||
| 61829 | 62414 | assert( u.an.zAffinity!=0 ); |
| 61830 | 62415 | assert( u.an.zAffinity[pOp->p2]==0 ); |
| 61831 | 62416 | pIn1 = &aMem[pOp->p1]; |
| 61832 | 62417 | while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){ |
| 61833 | 62418 | assert( pIn1 <= &p->aMem[p->nMem] ); |
| 62419 | + assert( memIsValid(pIn1) ); | |
| 62420 | + memAboutToChange(p, pIn1); | |
| 61834 | 62421 | ExpandBlob(pIn1); |
| 61835 | 62422 | applyAffinity(pIn1, u.an.cAff, encoding); |
| 61836 | 62423 | pIn1++; |
| 61837 | 62424 | } |
| 61838 | 62425 | break; |
| 61839 | 62426 | } |
| 61840 | 62427 | |
| 61841 | 62428 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
| 61842 | 62429 | ** |
| 61843 | -** Convert P2 registers beginning with P1 into a single entry | |
| 61844 | -** suitable for use as a data record in a database table or as a key | |
| 61845 | -** in an index. The details of the format are irrelevant as long as | |
| 61846 | -** the OP_Column opcode can decode the record later. | |
| 61847 | -** Refer to source code comments for the details of the record | |
| 61848 | -** format. | |
| 62430 | +** Convert P2 registers beginning with P1 into the [record format] | |
| 62431 | +** use as a data record in a database table or as a key | |
| 62432 | +** in an index. The OP_Column opcode can decode the record later. | |
| 61849 | 62433 | ** |
| 61850 | 62434 | ** P4 may be a string that is P2 characters long. The nth character of the |
| 61851 | 62435 | ** string indicates the column affinity that should be used for the nth |
| 61852 | 62436 | ** field of the index key. |
| 61853 | 62437 | ** |
| @@ -61899,16 +62483,23 @@ | ||
| 61899 | 62483 | assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 ); |
| 61900 | 62484 | u.ao.pData0 = &aMem[u.ao.nField]; |
| 61901 | 62485 | u.ao.nField = pOp->p2; |
| 61902 | 62486 | u.ao.pLast = &u.ao.pData0[u.ao.nField-1]; |
| 61903 | 62487 | u.ao.file_format = p->minWriteFileFormat; |
| 62488 | + | |
| 62489 | + /* Identify the output register */ | |
| 62490 | + assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); | |
| 62491 | + pOut = &aMem[pOp->p3]; | |
| 62492 | + memAboutToChange(p, pOut); | |
| 61904 | 62493 | |
| 61905 | 62494 | /* Loop through the elements that will make up the record to figure |
| 61906 | 62495 | ** out how much space is required for the new record. |
| 61907 | 62496 | */ |
| 61908 | 62497 | for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ |
| 62498 | + assert( memIsValid(u.ao.pRec) ); | |
| 61909 | 62499 | if( u.ao.zAffinity ){ |
| 62500 | + memAboutToChange(p, u.ao.pRec); | |
| 61910 | 62501 | applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding); |
| 61911 | 62502 | } |
| 61912 | 62503 | if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){ |
| 61913 | 62504 | sqlite3VdbeMemExpandBlob(u.ao.pRec); |
| 61914 | 62505 | } |
| @@ -61938,12 +62529,10 @@ | ||
| 61938 | 62529 | /* Make sure the output register has a buffer large enough to store |
| 61939 | 62530 | ** the new record. The output register (pOp->p3) is not allowed to |
| 61940 | 62531 | ** be one of the input registers (because the following call to |
| 61941 | 62532 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 61942 | 62533 | */ |
| 61943 | - assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); | |
| 61944 | - pOut = &aMem[pOp->p3]; | |
| 61945 | 62534 | if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){ |
| 61946 | 62535 | goto no_mem; |
| 61947 | 62536 | } |
| 61948 | 62537 | u.ao.zNewRecord = (u8 *)pOut->z; |
| 61949 | 62538 | |
| @@ -62112,10 +62701,11 @@ | ||
| 62112 | 62701 | } |
| 62113 | 62702 | } |
| 62114 | 62703 | if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
| 62115 | 62704 | sqlite3ExpirePreparedStatements(db); |
| 62116 | 62705 | sqlite3ResetInternalSchema(db, 0); |
| 62706 | + db->flags = (db->flags | SQLITE_InternChanges); | |
| 62117 | 62707 | } |
| 62118 | 62708 | } |
| 62119 | 62709 | |
| 62120 | 62710 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 62121 | 62711 | ** savepoints nested inside of the savepoint being operated on. */ |
| @@ -62502,10 +63092,12 @@ | ||
| 62502 | 63092 | } |
| 62503 | 63093 | if( pOp->p5 ){ |
| 62504 | 63094 | assert( u.aw.p2>0 ); |
| 62505 | 63095 | assert( u.aw.p2<=p->nMem ); |
| 62506 | 63096 | pIn2 = &aMem[u.aw.p2]; |
| 63097 | + assert( memIsValid(pIn2) ); | |
| 63098 | + assert( (pIn2->flags & MEM_Int)!=0 ); | |
| 62507 | 63099 | sqlite3VdbeMemIntegerify(pIn2); |
| 62508 | 63100 | u.aw.p2 = (int)pIn2->u.i; |
| 62509 | 63101 | /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and |
| 62510 | 63102 | ** that opcode will always set the u.aw.p2 value to 2 or more or else fail. |
| 62511 | 63103 | ** If there were a failure, the prepared statement would have halted |
| @@ -62524,10 +63116,11 @@ | ||
| 62524 | 63116 | } |
| 62525 | 63117 | assert( pOp->p1>=0 ); |
| 62526 | 63118 | u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1); |
| 62527 | 63119 | if( u.aw.pCur==0 ) goto no_mem; |
| 62528 | 63120 | u.aw.pCur->nullRow = 1; |
| 63121 | + u.aw.pCur->isOrdered = 1; | |
| 62529 | 63122 | rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor); |
| 62530 | 63123 | u.aw.pCur->pKeyInfo = u.aw.pKeyInfo; |
| 62531 | 63124 | |
| 62532 | 63125 | /* Since it performs no memory allocation or IO, the only values that |
| 62533 | 63126 | ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. |
| @@ -62576,11 +63169,11 @@ | ||
| 62576 | 63169 | case OP_OpenAutoindex: |
| 62577 | 63170 | case OP_OpenEphemeral: { |
| 62578 | 63171 | #if 0 /* local variables moved into u.ax */ |
| 62579 | 63172 | VdbeCursor *pCx; |
| 62580 | 63173 | #endif /* local variables moved into u.ax */ |
| 62581 | - static const int openFlags = | |
| 63174 | + static const int vfsFlags = | |
| 62582 | 63175 | SQLITE_OPEN_READWRITE | |
| 62583 | 63176 | SQLITE_OPEN_CREATE | |
| 62584 | 63177 | SQLITE_OPEN_EXCLUSIVE | |
| 62585 | 63178 | SQLITE_OPEN_DELETEONCLOSE | |
| 62586 | 63179 | SQLITE_OPEN_TRANSIENT_DB; |
| @@ -62587,25 +63180,25 @@ | ||
| 62587 | 63180 | |
| 62588 | 63181 | assert( pOp->p1>=0 ); |
| 62589 | 63182 | u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 62590 | 63183 | if( u.ax.pCx==0 ) goto no_mem; |
| 62591 | 63184 | u.ax.pCx->nullRow = 1; |
| 62592 | - rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, | |
| 62593 | - &u.ax.pCx->pBt); | |
| 63185 | + rc = sqlite3BtreeOpen(0, db, &u.ax.pCx->pBt, | |
| 63186 | + BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); | |
| 62594 | 63187 | if( rc==SQLITE_OK ){ |
| 62595 | 63188 | rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1); |
| 62596 | 63189 | } |
| 62597 | 63190 | if( rc==SQLITE_OK ){ |
| 62598 | 63191 | /* If a transient index is required, create it by calling |
| 62599 | - ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before | |
| 63192 | + ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before | |
| 62600 | 63193 | ** opening it. If a transient table is required, just use the |
| 62601 | - ** automatically created table with root-page 1 (an INTKEY table). | |
| 63194 | + ** automatically created table with root-page 1 (an BLOB_INTKEY table). | |
| 62602 | 63195 | */ |
| 62603 | 63196 | if( pOp->p4.pKeyInfo ){ |
| 62604 | 63197 | int pgno; |
| 62605 | 63198 | assert( pOp->p4type==P4_KEYINFO ); |
| 62606 | - rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_ZERODATA); | |
| 63199 | + rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY); | |
| 62607 | 63200 | if( rc==SQLITE_OK ){ |
| 62608 | 63201 | assert( pgno==MASTER_ROOT+1 ); |
| 62609 | 63202 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1, |
| 62610 | 63203 | (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor); |
| 62611 | 63204 | u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo; |
| @@ -62615,10 +63208,11 @@ | ||
| 62615 | 63208 | }else{ |
| 62616 | 63209 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor); |
| 62617 | 63210 | u.ax.pCx->isTable = 1; |
| 62618 | 63211 | } |
| 62619 | 63212 | } |
| 63213 | + u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); | |
| 62620 | 63214 | u.ax.pCx->isIndex = !u.ax.pCx->isTable; |
| 62621 | 63215 | break; |
| 62622 | 63216 | } |
| 62623 | 63217 | |
| 62624 | 63218 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| @@ -62734,10 +63328,11 @@ | ||
| 62734 | 63328 | assert( u.az.pC!=0 ); |
| 62735 | 63329 | assert( u.az.pC->pseudoTableReg==0 ); |
| 62736 | 63330 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 62737 | 63331 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 62738 | 63332 | assert( OP_SeekGt == OP_SeekLt+3 ); |
| 63333 | + assert( u.az.pC->isOrdered ); | |
| 62739 | 63334 | if( u.az.pC->pCursor!=0 ){ |
| 62740 | 63335 | u.az.oc = pOp->opcode; |
| 62741 | 63336 | u.az.pC->nullRow = 0; |
| 62742 | 63337 | if( u.az.pC->isTable ){ |
| 62743 | 63338 | /* The input value in P3 might be of any type: integer, real, string, |
| @@ -62816,10 +63411,13 @@ | ||
| 62816 | 63411 | assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY ); |
| 62817 | 63412 | assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 ); |
| 62818 | 63413 | assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 ); |
| 62819 | 63414 | |
| 62820 | 63415 | u.az.r.aMem = &aMem[pOp->p3]; |
| 63416 | +#ifdef SQLITE_DEBUG | |
| 63417 | + { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); } | |
| 63418 | +#endif | |
| 62821 | 63419 | ExpandBlob(u.az.r.aMem); |
| 62822 | 63420 | rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res); |
| 62823 | 63421 | if( rc!=SQLITE_OK ){ |
| 62824 | 63422 | goto abort_due_to_error; |
| 62825 | 63423 | } |
| @@ -62944,15 +63542,18 @@ | ||
| 62944 | 63542 | assert( u.bb.pC->isTable==0 ); |
| 62945 | 63543 | if( pOp->p4.i>0 ){ |
| 62946 | 63544 | u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo; |
| 62947 | 63545 | u.bb.r.nField = (u16)pOp->p4.i; |
| 62948 | 63546 | u.bb.r.aMem = pIn3; |
| 63547 | +#ifdef SQLITE_DEBUG | |
| 63548 | + { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); } | |
| 63549 | +#endif | |
| 62949 | 63550 | u.bb.r.flags = UNPACKED_PREFIX_MATCH; |
| 62950 | 63551 | u.bb.pIdxKey = &u.bb.r; |
| 62951 | 63552 | }else{ |
| 62952 | 63553 | assert( pIn3->flags & MEM_Blob ); |
| 62953 | - ExpandBlob(pIn3); | |
| 63554 | + assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ | |
| 62954 | 63555 | u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z, |
| 62955 | 63556 | u.bb.aTempRec, sizeof(u.bb.aTempRec)); |
| 62956 | 63557 | if( u.bb.pIdxKey==0 ){ |
| 62957 | 63558 | goto no_mem; |
| 62958 | 63559 | } |
| @@ -63043,10 +63644,13 @@ | ||
| 63043 | 63644 | /* Populate the index search key. */ |
| 63044 | 63645 | u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo; |
| 63045 | 63646 | u.bc.r.nField = u.bc.nField + 1; |
| 63046 | 63647 | u.bc.r.flags = UNPACKED_PREFIX_SEARCH; |
| 63047 | 63648 | u.bc.r.aMem = u.bc.aMx; |
| 63649 | +#ifdef SQLITE_DEBUG | |
| 63650 | + { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); } | |
| 63651 | +#endif | |
| 63048 | 63652 | |
| 63049 | 63653 | /* Extract the value of u.bc.R from register P3. */ |
| 63050 | 63654 | sqlite3VdbeMemIntegerify(pIn3); |
| 63051 | 63655 | u.bc.R = pIn3->u.i; |
| 63052 | 63656 | |
| @@ -63065,11 +63669,11 @@ | ||
| 63065 | 63669 | |
| 63066 | 63670 | /* Opcode: NotExists P1 P2 P3 * * |
| 63067 | 63671 | ** |
| 63068 | 63672 | ** Use the content of register P3 as a integer key. If a record |
| 63069 | 63673 | ** with that key does not exist in table of P1, then jump to P2. |
| 63070 | -** If the record does exist, then fall thru. The cursor is left | |
| 63674 | +** If the record does exist, then fall through. The cursor is left | |
| 63071 | 63675 | ** pointing to the record if it exists. |
| 63072 | 63676 | ** |
| 63073 | 63677 | ** The difference between this operation and NotFound is that this |
| 63074 | 63678 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 63075 | 63679 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| @@ -63223,11 +63827,13 @@ | ||
| 63223 | 63827 | u.be.pMem = &u.be.pFrame->aMem[pOp->p3]; |
| 63224 | 63828 | }else{ |
| 63225 | 63829 | /* Assert that P3 is a valid memory cell. */ |
| 63226 | 63830 | assert( pOp->p3<=p->nMem ); |
| 63227 | 63831 | u.be.pMem = &aMem[pOp->p3]; |
| 63832 | + memAboutToChange(p, u.be.pMem); | |
| 63228 | 63833 | } |
| 63834 | + assert( memIsValid(u.be.pMem) ); | |
| 63229 | 63835 | |
| 63230 | 63836 | REGISTER_TRACE(pOp->p3, u.be.pMem); |
| 63231 | 63837 | sqlite3VdbeMemIntegerify(u.be.pMem); |
| 63232 | 63838 | assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 63233 | 63839 | if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){ |
| @@ -63242,33 +63848,40 @@ | ||
| 63242 | 63848 | #endif |
| 63243 | 63849 | |
| 63244 | 63850 | sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0); |
| 63245 | 63851 | } |
| 63246 | 63852 | if( u.be.pC->useRandomRowid ){ |
| 63247 | - /* IMPLEMENTATION-OF: R-48598-02938 If the largest ROWID is equal to the | |
| 63853 | + /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the | |
| 63248 | 63854 | ** largest possible integer (9223372036854775807) then the database |
| 63249 | - ** engine starts picking candidate ROWIDs at random until it finds one | |
| 63250 | - ** that is not previously used. | |
| 63251 | - */ | |
| 63855 | + ** engine starts picking positive candidate ROWIDs at random until | |
| 63856 | + ** it finds one that is not previously used. */ | |
| 63252 | 63857 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 63253 | 63858 | ** an AUTOINCREMENT table. */ |
| 63859 | + /* on the first attempt, simply do one more than previous */ | |
| 63254 | 63860 | u.be.v = db->lastRowid; |
| 63861 | + u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ | |
| 63862 | + u.be.v++; /* ensure non-zero */ | |
| 63255 | 63863 | u.be.cnt = 0; |
| 63256 | - do{ | |
| 63257 | - if( u.be.cnt==0 && (u.be.v&0xffffff)==u.be.v ){ | |
| 63258 | - u.be.v++; | |
| 63864 | + while( ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, | |
| 63865 | + 0, &u.be.res))==SQLITE_OK) | |
| 63866 | + && (u.be.res==0) | |
| 63867 | + && (++u.be.cnt<100)){ | |
| 63868 | + /* collision - try another random rowid */ | |
| 63869 | + sqlite3_randomness(sizeof(u.be.v), &u.be.v); | |
| 63870 | + if( u.be.cnt<5 ){ | |
| 63871 | + /* try "small" random rowids for the initial attempts */ | |
| 63872 | + u.be.v &= 0xffffff; | |
| 63259 | 63873 | }else{ |
| 63260 | - sqlite3_randomness(sizeof(u.be.v), &u.be.v); | |
| 63261 | - if( u.be.cnt<5 ) u.be.v &= 0xffffff; | |
| 63874 | + u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ | |
| 63262 | 63875 | } |
| 63263 | - rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, 0, &u.be.res); | |
| 63264 | - u.be.cnt++; | |
| 63265 | - }while( u.be.cnt<100 && rc==SQLITE_OK && u.be.res==0 ); | |
| 63876 | + u.be.v++; /* ensure non-zero */ | |
| 63877 | + } | |
| 63266 | 63878 | if( rc==SQLITE_OK && u.be.res==0 ){ |
| 63267 | 63879 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 63268 | 63880 | goto abort_due_to_error; |
| 63269 | 63881 | } |
| 63882 | + assert( u.be.v>0 ); /* EV: R-40812-03570 */ | |
| 63270 | 63883 | } |
| 63271 | 63884 | u.be.pC->rowidIsValid = 0; |
| 63272 | 63885 | u.be.pC->deferredMoveto = 0; |
| 63273 | 63886 | u.be.pC->cacheStatus = CACHE_STALE; |
| 63274 | 63887 | } |
| @@ -63334,10 +63947,11 @@ | ||
| 63334 | 63947 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
| 63335 | 63948 | #endif /* local variables moved into u.bf */ |
| 63336 | 63949 | |
| 63337 | 63950 | u.bf.pData = &aMem[pOp->p2]; |
| 63338 | 63951 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63952 | + assert( memIsValid(u.bf.pData) ); | |
| 63339 | 63953 | u.bf.pC = p->apCsr[pOp->p1]; |
| 63340 | 63954 | assert( u.bf.pC!=0 ); |
| 63341 | 63955 | assert( u.bf.pC->pCursor!=0 ); |
| 63342 | 63956 | assert( u.bf.pC->pseudoTableReg==0 ); |
| 63343 | 63957 | assert( u.bf.pC->isTable ); |
| @@ -63344,10 +63958,11 @@ | ||
| 63344 | 63958 | REGISTER_TRACE(pOp->p2, u.bf.pData); |
| 63345 | 63959 | |
| 63346 | 63960 | if( pOp->opcode==OP_Insert ){ |
| 63347 | 63961 | u.bf.pKey = &aMem[pOp->p3]; |
| 63348 | 63962 | assert( u.bf.pKey->flags & MEM_Int ); |
| 63963 | + assert( memIsValid(u.bf.pKey) ); | |
| 63349 | 63964 | REGISTER_TRACE(pOp->p3, u.bf.pKey); |
| 63350 | 63965 | u.bf.iKey = u.bf.pKey->u.i; |
| 63351 | 63966 | }else{ |
| 63352 | 63967 | assert( pOp->opcode==OP_InsertInt ); |
| 63353 | 63968 | u.bf.iKey = pOp->p3; |
| @@ -63495,10 +64110,11 @@ | ||
| 63495 | 64110 | u32 n; |
| 63496 | 64111 | i64 n64; |
| 63497 | 64112 | #endif /* local variables moved into u.bh */ |
| 63498 | 64113 | |
| 63499 | 64114 | pOut = &aMem[pOp->p2]; |
| 64115 | + memAboutToChange(p, pOut); | |
| 63500 | 64116 | |
| 63501 | 64117 | /* Note that RowKey and RowData are really exactly the same instruction */ |
| 63502 | 64118 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63503 | 64119 | u.bh.pC = p->apCsr[pOp->p1]; |
| 63504 | 64120 | assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey ); |
| @@ -63837,10 +64453,13 @@ | ||
| 63837 | 64453 | if( ALWAYS(u.bo.pCrsr!=0) ){ |
| 63838 | 64454 | u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo; |
| 63839 | 64455 | u.bo.r.nField = (u16)pOp->p3; |
| 63840 | 64456 | u.bo.r.flags = 0; |
| 63841 | 64457 | u.bo.r.aMem = &aMem[pOp->p2]; |
| 64458 | +#ifdef SQLITE_DEBUG | |
| 64459 | + { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); } | |
| 64460 | +#endif | |
| 63842 | 64461 | rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res); |
| 63843 | 64462 | if( rc==SQLITE_OK && u.bo.res==0 ){ |
| 63844 | 64463 | rc = sqlite3BtreeDelete(u.bo.pCrsr); |
| 63845 | 64464 | } |
| 63846 | 64465 | assert( u.bo.pC->deferredMoveto==0 ); |
| @@ -63921,10 +64540,11 @@ | ||
| 63921 | 64540 | #endif /* local variables moved into u.bq */ |
| 63922 | 64541 | |
| 63923 | 64542 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63924 | 64543 | u.bq.pC = p->apCsr[pOp->p1]; |
| 63925 | 64544 | assert( u.bq.pC!=0 ); |
| 64545 | + assert( u.bq.pC->isOrdered ); | |
| 63926 | 64546 | if( ALWAYS(u.bq.pC->pCursor!=0) ){ |
| 63927 | 64547 | assert( u.bq.pC->deferredMoveto==0 ); |
| 63928 | 64548 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 63929 | 64549 | assert( pOp->p4type==P4_INT32 ); |
| 63930 | 64550 | u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo; |
| @@ -63933,10 +64553,13 @@ | ||
| 63933 | 64553 | u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 63934 | 64554 | }else{ |
| 63935 | 64555 | u.bq.r.flags = UNPACKED_IGNORE_ROWID; |
| 63936 | 64556 | } |
| 63937 | 64557 | u.bq.r.aMem = &aMem[pOp->p3]; |
| 64558 | +#ifdef SQLITE_DEBUG | |
| 64559 | + { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); } | |
| 64560 | +#endif | |
| 63938 | 64561 | rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res); |
| 63939 | 64562 | if( pOp->opcode==OP_IdxLT ){ |
| 63940 | 64563 | u.bq.res = -u.bq.res; |
| 63941 | 64564 | }else{ |
| 63942 | 64565 | assert( pOp->opcode==OP_IdxGE ); |
| @@ -64036,10 +64659,12 @@ | ||
| 64036 | 64659 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0) |
| 64037 | 64660 | ); |
| 64038 | 64661 | if( pOp->p3 ){ |
| 64039 | 64662 | p->nChange += u.bs.nChange; |
| 64040 | 64663 | if( pOp->p3>0 ){ |
| 64664 | + assert( memIsValid(&aMem[pOp->p3]) ); | |
| 64665 | + memAboutToChange(p, &aMem[pOp->p3]); | |
| 64041 | 64666 | aMem[pOp->p3].u.i += u.bs.nChange; |
| 64042 | 64667 | } |
| 64043 | 64668 | } |
| 64044 | 64669 | break; |
| 64045 | 64670 | } |
| @@ -64079,13 +64704,13 @@ | ||
| 64079 | 64704 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
| 64080 | 64705 | u.bt.pDb = &db->aDb[pOp->p1]; |
| 64081 | 64706 | assert( u.bt.pDb->pBt!=0 ); |
| 64082 | 64707 | if( pOp->opcode==OP_CreateTable ){ |
| 64083 | 64708 | /* u.bt.flags = BTREE_INTKEY; */ |
| 64084 | - u.bt.flags = BTREE_LEAFDATA|BTREE_INTKEY; | |
| 64709 | + u.bt.flags = BTREE_INTKEY; | |
| 64085 | 64710 | }else{ |
| 64086 | - u.bt.flags = BTREE_ZERODATA; | |
| 64711 | + u.bt.flags = BTREE_BLOBKEY; | |
| 64087 | 64712 | } |
| 64088 | 64713 | rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags); |
| 64089 | 64714 | pOut->u.i = u.bt.pgno; |
| 64090 | 64715 | break; |
| 64091 | 64716 | } |
| @@ -64410,10 +65035,11 @@ | ||
| 64410 | 65035 | void *t; /* Token identifying trigger */ |
| 64411 | 65036 | #endif /* local variables moved into u.by */ |
| 64412 | 65037 | |
| 64413 | 65038 | u.by.pProgram = pOp->p4.pProgram; |
| 64414 | 65039 | u.by.pRt = &aMem[pOp->p3]; |
| 65040 | + assert( memIsValid(u.by.pRt) ); | |
| 64415 | 65041 | assert( u.by.pProgram->nOp>0 ); |
| 64416 | 65042 | |
| 64417 | 65043 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 64418 | 65044 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 64419 | 65045 | ** is really a trigger, not a foreign key action, and the flag set |
| @@ -64583,10 +65209,11 @@ | ||
| 64583 | 65209 | for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent); |
| 64584 | 65210 | u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1]; |
| 64585 | 65211 | }else{ |
| 64586 | 65212 | u.ca.pIn1 = &aMem[pOp->p1]; |
| 64587 | 65213 | } |
| 65214 | + assert( memIsValid(u.ca.pIn1) ); | |
| 64588 | 65215 | sqlite3VdbeMemIntegerify(u.ca.pIn1); |
| 64589 | 65216 | pIn2 = &aMem[pOp->p2]; |
| 64590 | 65217 | sqlite3VdbeMemIntegerify(pIn2); |
| 64591 | 65218 | if( u.ca.pIn1->u.i<pIn2->u.i){ |
| 64592 | 65219 | u.ca.pIn1->u.i = pIn2->u.i; |
| @@ -64669,11 +65296,13 @@ | ||
| 64669 | 65296 | assert( u.cb.n>=0 ); |
| 64670 | 65297 | u.cb.pRec = &aMem[pOp->p2]; |
| 64671 | 65298 | u.cb.apVal = p->apArg; |
| 64672 | 65299 | assert( u.cb.apVal || u.cb.n==0 ); |
| 64673 | 65300 | for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){ |
| 65301 | + assert( memIsValid(u.cb.pRec) ); | |
| 64674 | 65302 | u.cb.apVal[u.cb.i] = u.cb.pRec; |
| 65303 | + memAboutToChange(p, u.cb.pRec); | |
| 64675 | 65304 | sqlite3VdbeMemStoreType(u.cb.pRec); |
| 64676 | 65305 | } |
| 64677 | 65306 | u.cb.ctx.pFunc = pOp->p4.pFunc; |
| 64678 | 65307 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 64679 | 65308 | u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3]; |
| @@ -64689,11 +65318,11 @@ | ||
| 64689 | 65318 | assert( pOp>p->aOp ); |
| 64690 | 65319 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 64691 | 65320 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 64692 | 65321 | u.cb.ctx.pColl = pOp[-1].p4.pColl; |
| 64693 | 65322 | } |
| 64694 | - (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); | |
| 65323 | + (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */ | |
| 64695 | 65324 | if( u.cb.ctx.isError ){ |
| 64696 | 65325 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s)); |
| 64697 | 65326 | rc = u.cb.ctx.isError; |
| 64698 | 65327 | } |
| 64699 | 65328 | sqlite3VdbeMemRelease(&u.cb.ctx.s); |
| @@ -65076,10 +65705,11 @@ | ||
| 65076 | 65705 | #endif /* local variables moved into u.ch */ |
| 65077 | 65706 | |
| 65078 | 65707 | u.ch.pQuery = &aMem[pOp->p3]; |
| 65079 | 65708 | u.ch.pArgc = &u.ch.pQuery[1]; |
| 65080 | 65709 | u.ch.pCur = p->apCsr[pOp->p1]; |
| 65710 | + assert( memIsValid(u.ch.pQuery) ); | |
| 65081 | 65711 | REGISTER_TRACE(pOp->p3, u.ch.pQuery); |
| 65082 | 65712 | assert( u.ch.pCur->pVtabCursor ); |
| 65083 | 65713 | u.ch.pVtabCursor = u.ch.pCur->pVtabCursor; |
| 65084 | 65714 | u.ch.pVtab = u.ch.pVtabCursor->pVtab; |
| 65085 | 65715 | u.ch.pModule = u.ch.pVtab->pModule; |
| @@ -65133,10 +65763,11 @@ | ||
| 65133 | 65763 | |
| 65134 | 65764 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 65135 | 65765 | assert( pCur->pVtabCursor ); |
| 65136 | 65766 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65137 | 65767 | u.ci.pDest = &aMem[pOp->p3]; |
| 65768 | + memAboutToChange(p, u.ci.pDest); | |
| 65138 | 65769 | if( pCur->nullRow ){ |
| 65139 | 65770 | sqlite3VdbeMemSetNull(u.ci.pDest); |
| 65140 | 65771 | break; |
| 65141 | 65772 | } |
| 65142 | 65773 | u.ci.pVtab = pCur->pVtabCursor->pVtab; |
| @@ -65235,14 +65866,16 @@ | ||
| 65235 | 65866 | #endif /* local variables moved into u.ck */ |
| 65236 | 65867 | |
| 65237 | 65868 | u.ck.pVtab = pOp->p4.pVtab->pVtab; |
| 65238 | 65869 | u.ck.pName = &aMem[pOp->p1]; |
| 65239 | 65870 | assert( u.ck.pVtab->pModule->xRename ); |
| 65871 | + assert( memIsValid(u.ck.pName) ); | |
| 65240 | 65872 | REGISTER_TRACE(pOp->p1, u.ck.pName); |
| 65241 | 65873 | assert( u.ck.pName->flags & MEM_Str ); |
| 65242 | 65874 | rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z); |
| 65243 | 65875 | importVtabErrMsg(p, u.ck.pVtab); |
| 65876 | + p->expired = 0; | |
| 65244 | 65877 | |
| 65245 | 65878 | break; |
| 65246 | 65879 | } |
| 65247 | 65880 | #endif |
| 65248 | 65881 | |
| @@ -65287,10 +65920,12 @@ | ||
| 65287 | 65920 | assert( pOp->p4type==P4_VTAB ); |
| 65288 | 65921 | if( ALWAYS(u.cl.pModule->xUpdate) ){ |
| 65289 | 65922 | u.cl.apArg = p->apArg; |
| 65290 | 65923 | u.cl.pX = &aMem[pOp->p3]; |
| 65291 | 65924 | for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){ |
| 65925 | + assert( memIsValid(u.cl.pX) ); | |
| 65926 | + memAboutToChange(p, u.cl.pX); | |
| 65292 | 65927 | sqlite3VdbeMemStoreType(u.cl.pX); |
| 65293 | 65928 | u.cl.apArg[u.cl.i] = u.cl.pX; |
| 65294 | 65929 | u.cl.pX++; |
| 65295 | 65930 | } |
| 65296 | 65931 | rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid); |
| @@ -66341,12 +66976,11 @@ | ||
| 66341 | 66976 | SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
| 66342 | 66977 | return pJfd->pMethods==&MemJournalMethods; |
| 66343 | 66978 | } |
| 66344 | 66979 | |
| 66345 | 66980 | /* |
| 66346 | -** Return the number of bytes required to store a MemJournal that uses vfs | |
| 66347 | -** pVfs to create the underlying on-disk files. | |
| 66981 | +** Return the number of bytes required to store a MemJournal file descriptor. | |
| 66348 | 66982 | */ |
| 66349 | 66983 | SQLITE_PRIVATE int sqlite3MemJournalSize(void){ |
| 66350 | 66984 | return sizeof(MemJournal); |
| 66351 | 66985 | } |
| 66352 | 66986 | |
| @@ -69226,12 +69860,12 @@ | ||
| 69226 | 69860 | return eType; |
| 69227 | 69861 | } |
| 69228 | 69862 | #endif |
| 69229 | 69863 | |
| 69230 | 69864 | /* |
| 69231 | -** Generate code for scalar subqueries used as an expression | |
| 69232 | -** and IN operators. Examples: | |
| 69865 | +** Generate code for scalar subqueries used as a subquery expression, EXISTS, | |
| 69866 | +** or IN operators. Examples: | |
| 69233 | 69867 | ** |
| 69234 | 69868 | ** (SELECT a FROM b) -- subquery |
| 69235 | 69869 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 69236 | 69870 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 69237 | 69871 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
| @@ -69290,14 +69924,14 @@ | ||
| 69290 | 69924 | assert( testAddr>0 || pParse->db->mallocFailed ); |
| 69291 | 69925 | } |
| 69292 | 69926 | |
| 69293 | 69927 | switch( pExpr->op ){ |
| 69294 | 69928 | case TK_IN: { |
| 69295 | - char affinity; | |
| 69296 | - KeyInfo keyInfo; | |
| 69297 | - int addr; /* Address of OP_OpenEphemeral instruction */ | |
| 69298 | - Expr *pLeft = pExpr->pLeft; | |
| 69929 | + char affinity; /* Affinity of the LHS of the IN */ | |
| 69930 | + KeyInfo keyInfo; /* Keyinfo for the generated table */ | |
| 69931 | + int addr; /* Address of OP_OpenEphemeral instruction */ | |
| 69932 | + Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ | |
| 69299 | 69933 | |
| 69300 | 69934 | if( rMayHaveNull ){ |
| 69301 | 69935 | sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
| 69302 | 69936 | } |
| 69303 | 69937 | |
| @@ -69316,10 +69950,11 @@ | ||
| 69316 | 69950 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 69317 | 69951 | ** is used. |
| 69318 | 69952 | */ |
| 69319 | 69953 | pExpr->iTable = pParse->nTab++; |
| 69320 | 69954 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); |
| 69955 | + if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
| 69321 | 69956 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 69322 | 69957 | keyInfo.nField = 1; |
| 69323 | 69958 | |
| 69324 | 69959 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 69325 | 69960 | /* Case 1: expr IN (SELECT ...) |
| @@ -69924,77 +70559,10 @@ | ||
| 69924 | 70559 | } |
| 69925 | 70560 | return 0; |
| 69926 | 70561 | } |
| 69927 | 70562 | #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
| 69928 | 70563 | |
| 69929 | -/* | |
| 69930 | -** If the last instruction coded is an ephemeral copy of any of | |
| 69931 | -** the registers in the nReg registers beginning with iReg, then | |
| 69932 | -** convert the last instruction from OP_SCopy to OP_Copy. | |
| 69933 | -*/ | |
| 69934 | -SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ | |
| 69935 | - VdbeOp *pOp; | |
| 69936 | - Vdbe *v; | |
| 69937 | - | |
| 69938 | - assert( pParse->db->mallocFailed==0 ); | |
| 69939 | - v = pParse->pVdbe; | |
| 69940 | - assert( v!=0 ); | |
| 69941 | - pOp = sqlite3VdbeGetOp(v, -1); | |
| 69942 | - assert( pOp!=0 ); | |
| 69943 | - if( pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ | |
| 69944 | - pOp->opcode = OP_Copy; | |
| 69945 | - } | |
| 69946 | -} | |
| 69947 | - | |
| 69948 | -/* | |
| 69949 | -** Generate code to store the value of the iAlias-th alias in register | |
| 69950 | -** target. The first time this is called, pExpr is evaluated to compute | |
| 69951 | -** the value of the alias. The value is stored in an auxiliary register | |
| 69952 | -** and the number of that register is returned. On subsequent calls, | |
| 69953 | -** the register number is returned without generating any code. | |
| 69954 | -** | |
| 69955 | -** Note that in order for this to work, code must be generated in the | |
| 69956 | -** same order that it is executed. | |
| 69957 | -** | |
| 69958 | -** Aliases are numbered starting with 1. So iAlias is in the range | |
| 69959 | -** of 1 to pParse->nAlias inclusive. | |
| 69960 | -** | |
| 69961 | -** pParse->aAlias[iAlias-1] records the register number where the value | |
| 69962 | -** of the iAlias-th alias is stored. If zero, that means that the | |
| 69963 | -** alias has not yet been computed. | |
| 69964 | -*/ | |
| 69965 | -static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){ | |
| 69966 | -#if 0 | |
| 69967 | - sqlite3 *db = pParse->db; | |
| 69968 | - int iReg; | |
| 69969 | - if( pParse->nAliasAlloc<pParse->nAlias ){ | |
| 69970 | - pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias, | |
| 69971 | - sizeof(pParse->aAlias[0])*pParse->nAlias ); | |
| 69972 | - testcase( db->mallocFailed && pParse->nAliasAlloc>0 ); | |
| 69973 | - if( db->mallocFailed ) return 0; | |
| 69974 | - memset(&pParse->aAlias[pParse->nAliasAlloc], 0, | |
| 69975 | - (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0])); | |
| 69976 | - pParse->nAliasAlloc = pParse->nAlias; | |
| 69977 | - } | |
| 69978 | - assert( iAlias>0 && iAlias<=pParse->nAlias ); | |
| 69979 | - iReg = pParse->aAlias[iAlias-1]; | |
| 69980 | - if( iReg==0 ){ | |
| 69981 | - if( pParse->iCacheLevel>0 ){ | |
| 69982 | - iReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 69983 | - }else{ | |
| 69984 | - iReg = ++pParse->nMem; | |
| 69985 | - sqlite3ExprCode(pParse, pExpr, iReg); | |
| 69986 | - pParse->aAlias[iAlias-1] = iReg; | |
| 69987 | - } | |
| 69988 | - } | |
| 69989 | - return iReg; | |
| 69990 | -#else | |
| 69991 | - UNUSED_PARAMETER(iAlias); | |
| 69992 | - return sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 69993 | -#endif | |
| 69994 | -} | |
| 69995 | - | |
| 69996 | 70564 | /* |
| 69997 | 70565 | ** Generate code into the current Vdbe to evaluate the given |
| 69998 | 70566 | ** expression. Attempt to store the results in register "target". |
| 69999 | 70567 | ** Return the register where results are stored. |
| 70000 | 70568 | ** |
| @@ -70099,11 +70667,11 @@ | ||
| 70099 | 70667 | case TK_REGISTER: { |
| 70100 | 70668 | inReg = pExpr->iTable; |
| 70101 | 70669 | break; |
| 70102 | 70670 | } |
| 70103 | 70671 | case TK_AS: { |
| 70104 | - inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target); | |
| 70672 | + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | |
| 70105 | 70673 | break; |
| 70106 | 70674 | } |
| 70107 | 70675 | #ifndef SQLITE_OMIT_CAST |
| 70108 | 70676 | case TK_CAST: { |
| 70109 | 70677 | /* Expressions of the form: CAST(pLeft AS token) */ |
| @@ -70531,10 +71099,15 @@ | ||
| 70531 | 71099 | testcase( regFree1==0 ); |
| 70532 | 71100 | cacheX.op = TK_REGISTER; |
| 70533 | 71101 | opCompare.op = TK_EQ; |
| 70534 | 71102 | opCompare.pLeft = &cacheX; |
| 70535 | 71103 | pTest = &opCompare; |
| 71104 | + /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: | |
| 71105 | + ** The value in regFree1 might get SCopy-ed into the file result. | |
| 71106 | + ** So make sure that the regFree1 register is not reused for other | |
| 71107 | + ** purposes and possibly overwritten. */ | |
| 71108 | + regFree1 = 0; | |
| 70536 | 71109 | } |
| 70537 | 71110 | for(i=0; i<nExpr; i=i+2){ |
| 70538 | 71111 | sqlite3ExprCachePush(pParse); |
| 70539 | 71112 | if( pX ){ |
| 70540 | 71113 | assert( pTest!=0 ); |
| @@ -70624,14 +71197,18 @@ | ||
| 70624 | 71197 | */ |
| 70625 | 71198 | SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
| 70626 | 71199 | int inReg; |
| 70627 | 71200 | |
| 70628 | 71201 | assert( target>0 && target<=pParse->nMem ); |
| 70629 | - inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 70630 | - assert( pParse->pVdbe || pParse->db->mallocFailed ); | |
| 70631 | - if( inReg!=target && pParse->pVdbe ){ | |
| 70632 | - sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); | |
| 71202 | + if( pExpr && pExpr->op==TK_REGISTER ){ | |
| 71203 | + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); | |
| 71204 | + }else{ | |
| 71205 | + inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 71206 | + assert( pParse->pVdbe || pParse->db->mallocFailed ); | |
| 71207 | + if( inReg!=target && pParse->pVdbe ){ | |
| 71208 | + sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); | |
| 71209 | + } | |
| 70633 | 71210 | } |
| 70634 | 71211 | return target; |
| 70635 | 71212 | } |
| 70636 | 71213 | |
| 70637 | 71214 | /* |
| @@ -70800,23 +71377,18 @@ | ||
| 70800 | 71377 | ){ |
| 70801 | 71378 | struct ExprList_item *pItem; |
| 70802 | 71379 | int i, n; |
| 70803 | 71380 | assert( pList!=0 ); |
| 70804 | 71381 | assert( target>0 ); |
| 71382 | + assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ | |
| 70805 | 71383 | n = pList->nExpr; |
| 70806 | 71384 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 70807 | - if( pItem->iAlias ){ | |
| 70808 | - int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i); | |
| 70809 | - Vdbe *v = sqlite3GetVdbe(pParse); | |
| 70810 | - if( iReg!=target+i ){ | |
| 70811 | - sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i); | |
| 70812 | - } | |
| 70813 | - }else{ | |
| 70814 | - sqlite3ExprCode(pParse, pItem->pExpr, target+i); | |
| 70815 | - } | |
| 70816 | - if( doHardCopy && !pParse->db->mallocFailed ){ | |
| 70817 | - sqlite3ExprHardCopy(pParse, target, n); | |
| 71385 | + Expr *pExpr = pItem->pExpr; | |
| 71386 | + int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); | |
| 71387 | + if( inReg!=target+i ){ | |
| 71388 | + sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy, | |
| 71389 | + inReg, target+i); | |
| 70818 | 71390 | } |
| 70819 | 71391 | } |
| 70820 | 71392 | return n; |
| 70821 | 71393 | } |
| 70822 | 71394 | |
| @@ -71794,10 +72366,15 @@ | ||
| 71794 | 72366 | if( pTrig->pSchema==pTempSchema ){ |
| 71795 | 72367 | zWhere = whereOrName(db, zWhere, pTrig->zName); |
| 71796 | 72368 | } |
| 71797 | 72369 | } |
| 71798 | 72370 | } |
| 72371 | + if( zWhere ){ | |
| 72372 | + char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); | |
| 72373 | + sqlite3DbFree(pParse->db, zWhere); | |
| 72374 | + zWhere = zNew; | |
| 72375 | + } | |
| 71799 | 72376 | return zWhere; |
| 71800 | 72377 | } |
| 71801 | 72378 | |
| 71802 | 72379 | /* |
| 71803 | 72380 | ** Generate code to drop and reload the internal representation of table |
| @@ -72401,11 +72978,12 @@ | ||
| 72401 | 72978 | int iIdxCur; /* Cursor open on index being analyzed */ |
| 72402 | 72979 | Vdbe *v; /* The virtual machine being built up */ |
| 72403 | 72980 | int i; /* Loop counter */ |
| 72404 | 72981 | int topOfLoop; /* The top of the loop */ |
| 72405 | 72982 | int endOfLoop; /* The end of the loop */ |
| 72406 | - int addr; /* The address of an instruction */ | |
| 72983 | + int addr = 0; /* The address of an instruction */ | |
| 72984 | + int jZeroRows = 0; /* Jump from here if number of rows is zero */ | |
| 72407 | 72985 | int iDb; /* Index of database containing pTab */ |
| 72408 | 72986 | int regTabname = iMem++; /* Register containing table name */ |
| 72409 | 72987 | int regIdxname = iMem++; /* Register containing index name */ |
| 72410 | 72988 | int regSampleno = iMem++; /* Register containing next sample number */ |
| 72411 | 72989 | int regCol = iMem++; /* Content of a column analyzed table */ |
| @@ -72420,12 +72998,19 @@ | ||
| 72420 | 72998 | int regLast = iMem++; /* Index of last sample to record */ |
| 72421 | 72999 | int regFirst = iMem++; /* Index of first sample to record */ |
| 72422 | 73000 | #endif |
| 72423 | 73001 | |
| 72424 | 73002 | v = sqlite3GetVdbe(pParse); |
| 72425 | - if( v==0 || NEVER(pTab==0) || pTab->pIndex==0 ){ | |
| 72426 | - /* Do no analysis for tables that have no indices */ | |
| 73003 | + if( v==0 || NEVER(pTab==0) ){ | |
| 73004 | + return; | |
| 73005 | + } | |
| 73006 | + if( pTab->tnum==0 ){ | |
| 73007 | + /* Do not gather statistics on views or virtual tables */ | |
| 73008 | + return; | |
| 73009 | + } | |
| 73010 | + if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ | |
| 73011 | + /* Do not gather statistics on system tables */ | |
| 72427 | 73012 | return; |
| 72428 | 73013 | } |
| 72429 | 73014 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 72430 | 73015 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 72431 | 73016 | assert( iDb>=0 ); |
| @@ -72438,10 +73023,11 @@ | ||
| 72438 | 73023 | |
| 72439 | 73024 | /* Establish a read-lock on the table at the shared-cache level. */ |
| 72440 | 73025 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 72441 | 73026 | |
| 72442 | 73027 | iIdxCur = pParse->nTab++; |
| 73028 | + sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); | |
| 72443 | 73029 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 72444 | 73030 | int nCol = pIdx->nColumn; |
| 72445 | 73031 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 72446 | 73032 | |
| 72447 | 73033 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| @@ -72452,14 +73038,11 @@ | ||
| 72452 | 73038 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 72453 | 73039 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
| 72454 | 73040 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| 72455 | 73041 | VdbeComment((v, "%s", pIdx->zName)); |
| 72456 | 73042 | |
| 72457 | - /* Populate the registers containing the table and index names. */ | |
| 72458 | - if( pTab->pIndex==pIdx ){ | |
| 72459 | - sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); | |
| 72460 | - } | |
| 73043 | + /* Populate the register containing the index name. */ | |
| 72461 | 73044 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 72462 | 73045 | |
| 72463 | 73046 | #ifdef SQLITE_ENABLE_STAT2 |
| 72464 | 73047 | |
| 72465 | 73048 | /* If this iteration of the loop is generating code to analyze the |
| @@ -72590,12 +73173,14 @@ | ||
| 72590 | 73173 | ** |
| 72591 | 73174 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 72592 | 73175 | ** If K>0 then it is always the case the D>0 so division by zero |
| 72593 | 73176 | ** is never possible. |
| 72594 | 73177 | */ |
| 72595 | - addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); | |
| 72596 | 73178 | sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 73179 | + if( jZeroRows==0 ){ | |
| 73180 | + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); | |
| 73181 | + } | |
| 72597 | 73182 | for(i=0; i<nCol; i++){ |
| 72598 | 73183 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 72599 | 73184 | sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 72600 | 73185 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 72601 | 73186 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| @@ -72605,17 +73190,39 @@ | ||
| 72605 | 73190 | } |
| 72606 | 73191 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 72607 | 73192 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 72608 | 73193 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 72609 | 73194 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 73195 | + } | |
| 73196 | + | |
| 73197 | + /* If the table has no indices, create a single sqlite_stat1 entry | |
| 73198 | + ** containing NULL as the index name and the row count as the content. | |
| 73199 | + */ | |
| 73200 | + if( pTab->pIndex==0 ){ | |
| 73201 | + sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); | |
| 73202 | + VdbeComment((v, "%s", pTab->zName)); | |
| 73203 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); | |
| 73204 | + sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); | |
| 73205 | + }else{ | |
| 73206 | + assert( jZeroRows>0 ); | |
| 73207 | + addr = sqlite3VdbeAddOp0(v, OP_Goto); | |
| 73208 | + sqlite3VdbeJumpHere(v, jZeroRows); | |
| 73209 | + } | |
| 73210 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); | |
| 73211 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); | |
| 73212 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); | |
| 73213 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); | |
| 73214 | + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | |
| 73215 | + if( pParse->nMem<regRec ) pParse->nMem = regRec; | |
| 73216 | + if( jZeroRows ){ | |
| 72610 | 73217 | sqlite3VdbeJumpHere(v, addr); |
| 72611 | 73218 | } |
| 72612 | 73219 | } |
| 72613 | 73220 | |
| 72614 | 73221 | /* |
| 72615 | 73222 | ** Generate code that will cause the most recent index analysis to |
| 72616 | -** be laoded into internal hash tables where is can be used. | |
| 73223 | +** be loaded into internal hash tables where is can be used. | |
| 72617 | 73224 | */ |
| 72618 | 73225 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 72619 | 73226 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 72620 | 73227 | if( v ){ |
| 72621 | 73228 | sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
| @@ -72741,37 +73348,50 @@ | ||
| 72741 | 73348 | |
| 72742 | 73349 | /* |
| 72743 | 73350 | ** This callback is invoked once for each index when reading the |
| 72744 | 73351 | ** sqlite_stat1 table. |
| 72745 | 73352 | ** |
| 72746 | -** argv[0] = name of the index | |
| 72747 | -** argv[1] = results of analysis - on integer for each column | |
| 73353 | +** argv[0] = name of the table | |
| 73354 | +** argv[1] = name of the index (might be NULL) | |
| 73355 | +** argv[2] = results of analysis - on integer for each column | |
| 73356 | +** | |
| 73357 | +** Entries for which argv[1]==NULL simply record the number of rows in | |
| 73358 | +** the table. | |
| 72748 | 73359 | */ |
| 72749 | 73360 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 72750 | 73361 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 72751 | 73362 | Index *pIndex; |
| 72752 | - int i, c; | |
| 73363 | + Table *pTable; | |
| 73364 | + int i, c, n; | |
| 72753 | 73365 | unsigned int v; |
| 72754 | 73366 | const char *z; |
| 72755 | 73367 | |
| 72756 | - assert( argc==2 ); | |
| 73368 | + assert( argc==3 ); | |
| 72757 | 73369 | UNUSED_PARAMETER2(NotUsed, argc); |
| 72758 | 73370 | |
| 72759 | - if( argv==0 || argv[0]==0 || argv[1]==0 ){ | |
| 73371 | + if( argv==0 || argv[0]==0 || argv[2]==0 ){ | |
| 72760 | 73372 | return 0; |
| 72761 | 73373 | } |
| 72762 | - pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase); | |
| 72763 | - if( pIndex==0 ){ | |
| 73374 | + pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); | |
| 73375 | + if( pTable==0 ){ | |
| 72764 | 73376 | return 0; |
| 72765 | 73377 | } |
| 72766 | - z = argv[1]; | |
| 72767 | - for(i=0; *z && i<=pIndex->nColumn; i++){ | |
| 73378 | + if( argv[1] ){ | |
| 73379 | + pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); | |
| 73380 | + }else{ | |
| 73381 | + pIndex = 0; | |
| 73382 | + } | |
| 73383 | + n = pIndex ? pIndex->nColumn : 0; | |
| 73384 | + z = argv[2]; | |
| 73385 | + for(i=0; *z && i<=n; i++){ | |
| 72768 | 73386 | v = 0; |
| 72769 | 73387 | while( (c=z[0])>='0' && c<='9' ){ |
| 72770 | 73388 | v = v*10 + c - '0'; |
| 72771 | 73389 | z++; |
| 72772 | 73390 | } |
| 73391 | + if( i==0 ) pTable->nRowEst = v; | |
| 73392 | + if( pIndex==0 ) break; | |
| 72773 | 73393 | pIndex->aiRowEst[i] = v; |
| 72774 | 73394 | if( *z==' ' ) z++; |
| 72775 | 73395 | } |
| 72776 | 73396 | return 0; |
| 72777 | 73397 | } |
| @@ -72843,11 +73463,11 @@ | ||
| 72843 | 73463 | return SQLITE_ERROR; |
| 72844 | 73464 | } |
| 72845 | 73465 | |
| 72846 | 73466 | /* Load new statistics out of the sqlite_stat1 table */ |
| 72847 | 73467 | zSql = sqlite3MPrintf(db, |
| 72848 | - "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); | |
| 73468 | + "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); | |
| 72849 | 73469 | if( zSql==0 ){ |
| 72850 | 73470 | rc = SQLITE_NOMEM; |
| 72851 | 73471 | }else{ |
| 72852 | 73472 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 72853 | 73473 | sqlite3DbFree(db, zSql); |
| @@ -73060,13 +73680,12 @@ | ||
| 73060 | 73680 | |
| 73061 | 73681 | /* Open the database file. If the btree is successfully opened, use |
| 73062 | 73682 | ** it to obtain the database schema. At this point the schema may |
| 73063 | 73683 | ** or may not be initialised. |
| 73064 | 73684 | */ |
| 73065 | - rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, | |
| 73066 | - db->openFlags | SQLITE_OPEN_MAIN_DB, | |
| 73067 | - &aNew->pBt); | |
| 73685 | + rc = sqlite3BtreeOpen(zFile, db, &aNew->pBt, 0, | |
| 73686 | + db->openFlags | SQLITE_OPEN_MAIN_DB); | |
| 73068 | 73687 | db->nDb++; |
| 73069 | 73688 | if( rc==SQLITE_CONSTRAINT ){ |
| 73070 | 73689 | rc = SQLITE_ERROR; |
| 73071 | 73690 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| 73072 | 73691 | }else if( rc==SQLITE_OK ){ |
| @@ -73303,11 +73922,12 @@ | ||
| 73303 | 73922 | 0, /* pNext */ |
| 73304 | 73923 | detachFunc, /* xFunc */ |
| 73305 | 73924 | 0, /* xStep */ |
| 73306 | 73925 | 0, /* xFinalize */ |
| 73307 | 73926 | "sqlite_detach", /* zName */ |
| 73308 | - 0 /* pHash */ | |
| 73927 | + 0, /* pHash */ | |
| 73928 | + 0 /* pDestructor */ | |
| 73309 | 73929 | }; |
| 73310 | 73930 | codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); |
| 73311 | 73931 | } |
| 73312 | 73932 | |
| 73313 | 73933 | /* |
| @@ -73324,11 +73944,12 @@ | ||
| 73324 | 73944 | 0, /* pNext */ |
| 73325 | 73945 | attachFunc, /* xFunc */ |
| 73326 | 73946 | 0, /* xStep */ |
| 73327 | 73947 | 0, /* xFinalize */ |
| 73328 | 73948 | "sqlite_attach", /* zName */ |
| 73329 | - 0 /* pHash */ | |
| 73949 | + 0, /* pHash */ | |
| 73950 | + 0 /* pDestructor */ | |
| 73330 | 73951 | }; |
| 73331 | 73952 | codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); |
| 73332 | 73953 | } |
| 73333 | 73954 | #endif /* SQLITE_OMIT_ATTACH */ |
| 73334 | 73955 | |
| @@ -74453,12 +75074,13 @@ | ||
| 74453 | 75074 | ** set to the index of the database that the table or view is to be |
| 74454 | 75075 | ** created in. |
| 74455 | 75076 | */ |
| 74456 | 75077 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 74457 | 75078 | if( iDb<0 ) return; |
| 74458 | - if( !OMIT_TEMPDB && isTemp && iDb>1 ){ | |
| 74459 | - /* If creating a temp table, the name may not be qualified */ | |
| 75079 | + if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ | |
| 75080 | + /* If creating a temp table, the name may not be qualified. Unless | |
| 75081 | + ** the database name is "temp" anyway. */ | |
| 74460 | 75082 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 74461 | 75083 | return; |
| 74462 | 75084 | } |
| 74463 | 75085 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 74464 | 75086 | |
| @@ -74502,21 +75124,22 @@ | ||
| 74502 | 75124 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 74503 | 75125 | ** and types will be used, so there is no need to test for namespace |
| 74504 | 75126 | ** collisions. |
| 74505 | 75127 | */ |
| 74506 | 75128 | if( !IN_DECLARE_VTAB ){ |
| 75129 | + char *zDb = db->aDb[iDb].zName; | |
| 74507 | 75130 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 74508 | 75131 | goto begin_table_error; |
| 74509 | 75132 | } |
| 74510 | - pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName); | |
| 75133 | + pTable = sqlite3FindTable(db, zName, zDb); | |
| 74511 | 75134 | if( pTable ){ |
| 74512 | 75135 | if( !noErr ){ |
| 74513 | 75136 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 74514 | 75137 | } |
| 74515 | 75138 | goto begin_table_error; |
| 74516 | 75139 | } |
| 74517 | - if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){ | |
| 75140 | + if( sqlite3FindIndex(db, zName, zDb)!=0 ){ | |
| 74518 | 75141 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 74519 | 75142 | goto begin_table_error; |
| 74520 | 75143 | } |
| 74521 | 75144 | } |
| 74522 | 75145 | |
| @@ -74529,10 +75152,11 @@ | ||
| 74529 | 75152 | } |
| 74530 | 75153 | pTable->zName = zName; |
| 74531 | 75154 | pTable->iPKey = -1; |
| 74532 | 75155 | pTable->pSchema = db->aDb[iDb].pSchema; |
| 74533 | 75156 | pTable->nRef = 1; |
| 75157 | + pTable->nRowEst = 1000000; | |
| 74534 | 75158 | assert( pParse->pNewTable==0 ); |
| 74535 | 75159 | pParse->pNewTable = pTable; |
| 74536 | 75160 | |
| 74537 | 75161 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 74538 | 75162 | ** then record a pointer to this table in the main database structure |
| @@ -75375,16 +75999,14 @@ | ||
| 75375 | 75999 | sqlite3SelectDelete(db, pSelect); |
| 75376 | 76000 | return; |
| 75377 | 76001 | } |
| 75378 | 76002 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
| 75379 | 76003 | p = pParse->pNewTable; |
| 75380 | - if( p==0 ){ | |
| 76004 | + if( p==0 || pParse->nErr ){ | |
| 75381 | 76005 | sqlite3SelectDelete(db, pSelect); |
| 75382 | 76006 | return; |
| 75383 | 76007 | } |
| 75384 | - assert( pParse->nErr==0 ); /* If sqlite3StartTable return non-NULL then | |
| 75385 | - ** there could not have been an error */ | |
| 75386 | 76008 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 75387 | 76009 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 75388 | 76010 | if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
| 75389 | 76011 | && sqlite3FixSelect(&sFix, pSelect) |
| 75390 | 76012 | ){ |
| @@ -76498,11 +77120,12 @@ | ||
| 76498 | 77120 | */ |
| 76499 | 77121 | if( pTblName ){ |
| 76500 | 77122 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 76501 | 77123 | sqlite3ChangeCookie(pParse, iDb); |
| 76502 | 77124 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, |
| 76503 | - sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC); | |
| 77125 | + sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), | |
| 77126 | + P4_DYNAMIC); | |
| 76504 | 77127 | sqlite3VdbeAddOp1(v, OP_Expire, 0); |
| 76505 | 77128 | } |
| 76506 | 77129 | } |
| 76507 | 77130 | |
| 76508 | 77131 | /* When adding an index to the list of indices for a table, make |
| @@ -76559,18 +77182,18 @@ | ||
| 76559 | 77182 | ** are based on typical values found in actual indices. |
| 76560 | 77183 | */ |
| 76561 | 77184 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 76562 | 77185 | unsigned *a = pIdx->aiRowEst; |
| 76563 | 77186 | int i; |
| 77187 | + unsigned n; | |
| 76564 | 77188 | assert( a!=0 ); |
| 76565 | - a[0] = 1000000; | |
| 76566 | - for(i=pIdx->nColumn; i>=5; i--){ | |
| 76567 | - a[i] = 5; | |
| 76568 | - } | |
| 76569 | - while( i>=1 ){ | |
| 76570 | - a[i] = 11 - i; | |
| 76571 | - i--; | |
| 77189 | + a[0] = pIdx->pTable->nRowEst; | |
| 77190 | + if( a[0]<10 ) a[0] = 10; | |
| 77191 | + n = 10; | |
| 77192 | + for(i=1; i<=pIdx->nColumn; i++){ | |
| 77193 | + a[i] = n; | |
| 77194 | + if( n>5 ) n--; | |
| 76572 | 77195 | } |
| 76573 | 77196 | if( pIdx->onError!=OE_None ){ |
| 76574 | 77197 | a[pIdx->nColumn] = 1; |
| 76575 | 77198 | } |
| 76576 | 77199 | } |
| @@ -76626,11 +77249,11 @@ | ||
| 76626 | 77249 | /* Generate code to remove the index and from the master table */ |
| 76627 | 77250 | v = sqlite3GetVdbe(pParse); |
| 76628 | 77251 | if( v ){ |
| 76629 | 77252 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 76630 | 77253 | sqlite3NestedParse(pParse, |
| 76631 | - "DELETE FROM %Q.%s WHERE name=%Q", | |
| 77254 | + "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", | |
| 76632 | 77255 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 76633 | 77256 | pIndex->zName |
| 76634 | 77257 | ); |
| 76635 | 77258 | if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ |
| 76636 | 77259 | sqlite3NestedParse(pParse, |
| @@ -77118,11 +77741,11 @@ | ||
| 77118 | 77741 | SQLITE_OPEN_CREATE | |
| 77119 | 77742 | SQLITE_OPEN_EXCLUSIVE | |
| 77120 | 77743 | SQLITE_OPEN_DELETEONCLOSE | |
| 77121 | 77744 | SQLITE_OPEN_TEMP_DB; |
| 77122 | 77745 | |
| 77123 | - rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags, &pBt); | |
| 77746 | + rc = sqlite3BtreeOpen(0, db, &pBt, 0, flags); | |
| 77124 | 77747 | if( rc!=SQLITE_OK ){ |
| 77125 | 77748 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 77126 | 77749 | "file for storing temporary tables"); |
| 77127 | 77750 | pParse->rc = rc; |
| 77128 | 77751 | return 1; |
| @@ -77775,11 +78398,11 @@ | ||
| 77775 | 78398 | ** If the SQLITE_PreferBuiltin flag is set, then search the built-in |
| 77776 | 78399 | ** functions even if a prior app-defined function was found. And give |
| 77777 | 78400 | ** priority to built-in functions. |
| 77778 | 78401 | ** |
| 77779 | 78402 | ** Except, if createFlag is true, that means that we are trying to |
| 77780 | - ** install a new function. Whatever FuncDef structure is returned will | |
| 78403 | + ** install a new function. Whatever FuncDef structure is returned it will | |
| 77781 | 78404 | ** have fields overwritten with new information appropriate for the |
| 77782 | 78405 | ** new function. But the FuncDefs for built-in functions are read-only. |
| 77783 | 78406 | ** So we must not search for built-ins when creating a new function. |
| 77784 | 78407 | */ |
| 77785 | 78408 | if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ |
| @@ -79956,14 +80579,14 @@ | ||
| 79956 | 80579 | if( caseSensitive ){ |
| 79957 | 80580 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 79958 | 80581 | }else{ |
| 79959 | 80582 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 79960 | 80583 | } |
| 79961 | - sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0); | |
| 79962 | - sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0); | |
| 80584 | + sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0); | |
| 80585 | + sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0); | |
| 79963 | 80586 | sqlite3CreateFunc(db, "glob", 2, SQLITE_ANY, |
| 79964 | - (struct compareInfo*)&globInfo, likeFunc, 0,0); | |
| 80587 | + (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); | |
| 79965 | 80588 | setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 79966 | 80589 | setLikeOptFlag(db, "like", |
| 79967 | 80590 | caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
| 79968 | 80591 | } |
| 79969 | 80592 | |
| @@ -80043,14 +80666,14 @@ | ||
| 80043 | 80666 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 80044 | 80667 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 80045 | 80668 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 80046 | 80669 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 80047 | 80670 | /* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */ |
| 80048 | - {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0}, | |
| 80671 | + {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0}, | |
| 80049 | 80672 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 80050 | 80673 | /* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */ |
| 80051 | - {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0}, | |
| 80674 | + {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0}, | |
| 80052 | 80675 | FUNCTION(random, 0, 0, 0, randomFunc ), |
| 80053 | 80676 | FUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 80054 | 80677 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 80055 | 80678 | FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 80056 | 80679 | FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| @@ -80073,11 +80696,11 @@ | ||
| 80073 | 80696 | #endif |
| 80074 | 80697 | AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 80075 | 80698 | AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 80076 | 80699 | AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 80077 | 80700 | /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */ |
| 80078 | - {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0}, | |
| 80701 | + {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0}, | |
| 80079 | 80702 | AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
| 80080 | 80703 | AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80081 | 80704 | AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80082 | 80705 | |
| 80083 | 80706 | LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| @@ -80484,11 +81107,11 @@ | ||
| 80484 | 81107 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 80485 | 81108 | |
| 80486 | 81109 | sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); |
| 80487 | 81110 | sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF); |
| 80488 | 81111 | for(i=0; i<nCol; i++){ |
| 80489 | - sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[i]+1+regData, regTemp+i); | |
| 81112 | + sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i); | |
| 80490 | 81113 | } |
| 80491 | 81114 | |
| 80492 | 81115 | /* If the parent table is the same as the child table, and we are about |
| 80493 | 81116 | ** to increment the constraint-counter (i.e. this is an INSERT operation), |
| 80494 | 81117 | ** then check if the row being inserted matches itself. If so, do not |
| @@ -87131,15 +87754,17 @@ | ||
| 87131 | 87754 | sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 87132 | 87755 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); |
| 87133 | 87756 | sqlite3ReleaseTempReg(pParse, r1); |
| 87134 | 87757 | } |
| 87135 | 87758 | |
| 87759 | +#ifndef SQLITE_OMIT_SUBQUERY | |
| 87136 | 87760 | /* |
| 87137 | 87761 | ** Generate an error message when a SELECT is used within a subexpression |
| 87138 | 87762 | ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result |
| 87139 | -** column. We do this in a subroutine because the error occurs in multiple | |
| 87140 | -** places. | |
| 87763 | +** column. We do this in a subroutine because the error used to occur | |
| 87764 | +** in multiple places. (The error only occurs in one place now, but we | |
| 87765 | +** retain the subroutine to minimize code disruption.) | |
| 87141 | 87766 | */ |
| 87142 | 87767 | static int checkForMultiColumnSelectError( |
| 87143 | 87768 | Parse *pParse, /* Parse context. */ |
| 87144 | 87769 | SelectDest *pDest, /* Destination of SELECT results */ |
| 87145 | 87770 | int nExpr /* Number of result columns returned by SELECT */ |
| @@ -87151,10 +87776,11 @@ | ||
| 87151 | 87776 | return 1; |
| 87152 | 87777 | }else{ |
| 87153 | 87778 | return 0; |
| 87154 | 87779 | } |
| 87155 | 87780 | } |
| 87781 | +#endif | |
| 87156 | 87782 | |
| 87157 | 87783 | /* |
| 87158 | 87784 | ** This routine generates the code for the inside of the inner loop |
| 87159 | 87785 | ** of a SELECT. |
| 87160 | 87786 | ** |
| @@ -87230,14 +87856,10 @@ | ||
| 87230 | 87856 | if( pOrderBy==0 ){ |
| 87231 | 87857 | codeOffset(v, p, iContinue); |
| 87232 | 87858 | } |
| 87233 | 87859 | } |
| 87234 | 87860 | |
| 87235 | - if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ | |
| 87236 | - return; | |
| 87237 | - } | |
| 87238 | - | |
| 87239 | 87861 | switch( eDest ){ |
| 87240 | 87862 | /* In this mode, write each query result to the key of the temporary |
| 87241 | 87863 | ** table iParm. |
| 87242 | 87864 | */ |
| 87243 | 87865 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| @@ -88143,10 +88765,11 @@ | ||
| 88143 | 88765 | /* Create the destination temporary table if necessary |
| 88144 | 88766 | */ |
| 88145 | 88767 | if( dest.eDest==SRT_EphemTab ){ |
| 88146 | 88768 | assert( p->pEList ); |
| 88147 | 88769 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr); |
| 88770 | + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
| 88148 | 88771 | dest.eDest = SRT_Table; |
| 88149 | 88772 | } |
| 88150 | 88773 | |
| 88151 | 88774 | /* Make sure all SELECTs in the statement have the same number of elements |
| 88152 | 88775 | ** in their result sets. |
| @@ -90105,11 +90728,11 @@ | ||
| 90105 | 90728 | ExprList *pList = pF->pExpr->x.pList; |
| 90106 | 90729 | assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); |
| 90107 | 90730 | if( pList ){ |
| 90108 | 90731 | nArg = pList->nExpr; |
| 90109 | 90732 | regAgg = sqlite3GetTempRange(pParse, nArg); |
| 90110 | - sqlite3ExprCodeExprList(pParse, pList, regAgg, 0); | |
| 90733 | + sqlite3ExprCodeExprList(pParse, pList, regAgg, 1); | |
| 90111 | 90734 | }else{ |
| 90112 | 90735 | nArg = 0; |
| 90113 | 90736 | regAgg = 0; |
| 90114 | 90737 | } |
| 90115 | 90738 | if( pF->iDistinct>=0 ){ |
| @@ -90264,10 +90887,19 @@ | ||
| 90264 | 90887 | |
| 90265 | 90888 | /* Begin generating code. |
| 90266 | 90889 | */ |
| 90267 | 90890 | v = sqlite3GetVdbe(pParse); |
| 90268 | 90891 | if( v==0 ) goto select_end; |
| 90892 | + | |
| 90893 | + /* If writing to memory or generating a set | |
| 90894 | + ** only a single column may be output. | |
| 90895 | + */ | |
| 90896 | +#ifndef SQLITE_OMIT_SUBQUERY | |
| 90897 | + if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ | |
| 90898 | + goto select_end; | |
| 90899 | + } | |
| 90900 | +#endif | |
| 90269 | 90901 | |
| 90270 | 90902 | /* Generate code for all sub-queries in the FROM clause |
| 90271 | 90903 | */ |
| 90272 | 90904 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 90273 | 90905 | for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| @@ -90338,19 +90970,10 @@ | ||
| 90338 | 90970 | } |
| 90339 | 90971 | return multiSelect(pParse, p, pDest); |
| 90340 | 90972 | } |
| 90341 | 90973 | #endif |
| 90342 | 90974 | |
| 90343 | - /* If writing to memory or generating a set | |
| 90344 | - ** only a single column may be output. | |
| 90345 | - */ | |
| 90346 | -#ifndef SQLITE_OMIT_SUBQUERY | |
| 90347 | - if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ | |
| 90348 | - goto select_end; | |
| 90349 | - } | |
| 90350 | -#endif | |
| 90351 | - | |
| 90352 | 90975 | /* If possible, rewrite the query to use GROUP BY instead of DISTINCT. |
| 90353 | 90976 | ** GROUP BY might use an index, DISTINCT never does. |
| 90354 | 90977 | */ |
| 90355 | 90978 | assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 ); |
| 90356 | 90979 | if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){ |
| @@ -90409,10 +91032,11 @@ | ||
| 90409 | 91032 | assert( isAgg || pGroupBy ); |
| 90410 | 91033 | distinct = pParse->nTab++; |
| 90411 | 91034 | pKeyInfo = keyInfoFromExprList(pParse, p->pEList); |
| 90412 | 91035 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0, |
| 90413 | 91036 | (char*)pKeyInfo, P4_KEYINFO_HANDOFF); |
| 91037 | + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
| 90414 | 91038 | }else{ |
| 90415 | 91039 | distinct = -1; |
| 90416 | 91040 | } |
| 90417 | 91041 | |
| 90418 | 91042 | /* Aggregate and non-aggregate queries are handled differently */ |
| @@ -92884,10 +93508,11 @@ | ||
| 92884 | 93508 | ** be stored. |
| 92885 | 93509 | */ |
| 92886 | 93510 | assert( v ); |
| 92887 | 93511 | ephemTab = pParse->nTab++; |
| 92888 | 93512 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); |
| 93513 | + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
| 92889 | 93514 | |
| 92890 | 93515 | /* fill the ephemeral table |
| 92891 | 93516 | */ |
| 92892 | 93517 | sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); |
| 92893 | 93518 | sqlite3Select(pParse, pSelect, &dest); |
| @@ -93022,10 +93647,14 @@ | ||
| 93022 | 93647 | int nDb; /* Number of attached databases */ |
| 93023 | 93648 | |
| 93024 | 93649 | if( !db->autoCommit ){ |
| 93025 | 93650 | sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); |
| 93026 | 93651 | return SQLITE_ERROR; |
| 93652 | + } | |
| 93653 | + if( db->activeVdbeCnt>1 ){ | |
| 93654 | + sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); | |
| 93655 | + return SQLITE_ERROR; | |
| 93027 | 93656 | } |
| 93028 | 93657 | |
| 93029 | 93658 | /* Save the current value of the database flags so that it can be |
| 93030 | 93659 | ** restored before returning. Then set the writable-schema flag, and |
| 93031 | 93660 | ** disable CHECK and foreign key constraints. */ |
| @@ -93624,11 +94253,11 @@ | ||
| 93624 | 94253 | sqlite3DbFree(db, zStmt); |
| 93625 | 94254 | v = sqlite3GetVdbe(pParse); |
| 93626 | 94255 | sqlite3ChangeCookie(pParse, iDb); |
| 93627 | 94256 | |
| 93628 | 94257 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 93629 | - zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName); | |
| 94258 | + zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); | |
| 93630 | 94259 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); |
| 93631 | 94260 | sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 93632 | 94261 | pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
| 93633 | 94262 | } |
| 93634 | 94263 | |
| @@ -94864,15 +95493,16 @@ | ||
| 94864 | 95493 | if( op==TK_REGISTER ){ |
| 94865 | 95494 | op = pRight->op2; |
| 94866 | 95495 | } |
| 94867 | 95496 | if( op==TK_VARIABLE ){ |
| 94868 | 95497 | Vdbe *pReprepare = pParse->pReprepare; |
| 94869 | - pVal = sqlite3VdbeGetValue(pReprepare, pRight->iColumn, SQLITE_AFF_NONE); | |
| 95498 | + int iCol = pRight->iColumn; | |
| 95499 | + pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); | |
| 94870 | 95500 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 94871 | 95501 | z = (char *)sqlite3_value_text(pVal); |
| 94872 | 95502 | } |
| 94873 | - sqlite3VdbeSetVarmask(pParse->pVdbe, pRight->iColumn); | |
| 95503 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */ | |
| 94874 | 95504 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 94875 | 95505 | }else if( op==TK_STRING ){ |
| 94876 | 95506 | z = pRight->u.zToken; |
| 94877 | 95507 | } |
| 94878 | 95508 | if( z ){ |
| @@ -94886,11 +95516,11 @@ | ||
| 94886 | 95516 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 94887 | 95517 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 94888 | 95518 | *ppPrefix = pPrefix; |
| 94889 | 95519 | if( op==TK_VARIABLE ){ |
| 94890 | 95520 | Vdbe *v = pParse->pVdbe; |
| 94891 | - sqlite3VdbeSetVarmask(v, pRight->iColumn); | |
| 95521 | + sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */ | |
| 94892 | 95522 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 94893 | 95523 | /* If the rhs of the LIKE expression is a variable, and the current |
| 94894 | 95524 | ** value of the variable means there is no need to invoke the LIKE |
| 94895 | 95525 | ** function, then no OP_Variable will be added to the program. |
| 94896 | 95526 | ** This causes problems for the sqlite3_bind_parameter_name() |
| @@ -95900,11 +96530,11 @@ | ||
| 95900 | 96530 | return; |
| 95901 | 96531 | } |
| 95902 | 96532 | |
| 95903 | 96533 | assert( pParse->nQueryLoop >= (double)1 ); |
| 95904 | 96534 | pTable = pSrc->pTab; |
| 95905 | - nTableRow = pTable->pIndex ? pTable->pIndex->aiRowEst[0] : 1000000; | |
| 96535 | + nTableRow = pTable->nRowEst; | |
| 95906 | 96536 | logN = estLog(nTableRow); |
| 95907 | 96537 | costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); |
| 95908 | 96538 | if( costTempIdx>=pCost->rCost ){ |
| 95909 | 96539 | /* The cost of creating the transient table would be greater than |
| 95910 | 96540 | ** doing the full table scan */ |
| @@ -96510,11 +97140,11 @@ | ||
| 96510 | 97140 | /* The evalConstExpr() function will have already converted any TK_VARIABLE |
| 96511 | 97141 | ** expression involved in an comparison into a TK_REGISTER. */ |
| 96512 | 97142 | assert( pExpr->op!=TK_VARIABLE ); |
| 96513 | 97143 | if( pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE ){ |
| 96514 | 97144 | int iVar = pExpr->iColumn; |
| 96515 | - sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); | |
| 97145 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */ | |
| 96516 | 97146 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 96517 | 97147 | return SQLITE_OK; |
| 96518 | 97148 | } |
| 96519 | 97149 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 96520 | 97150 | } |
| @@ -96707,27 +97337,18 @@ | ||
| 96707 | 97337 | Index *pFirst; /* Any other index on the table */ |
| 96708 | 97338 | memset(&sPk, 0, sizeof(Index)); |
| 96709 | 97339 | sPk.nColumn = 1; |
| 96710 | 97340 | sPk.aiColumn = &aiColumnPk; |
| 96711 | 97341 | sPk.aiRowEst = aiRowEstPk; |
| 96712 | - aiRowEstPk[1] = 1; | |
| 96713 | 97342 | sPk.onError = OE_Replace; |
| 96714 | 97343 | sPk.pTable = pSrc->pTab; |
| 97344 | + aiRowEstPk[0] = pSrc->pTab->nRowEst; | |
| 97345 | + aiRowEstPk[1] = 1; | |
| 96715 | 97346 | pFirst = pSrc->pTab->pIndex; |
| 96716 | 97347 | if( pSrc->notIndexed==0 ){ |
| 96717 | 97348 | sPk.pNext = pFirst; |
| 96718 | 97349 | } |
| 96719 | - /* The aiRowEstPk[0] is an estimate of the total number of rows in the | |
| 96720 | - ** table. Get this information from the ANALYZE information if it is | |
| 96721 | - ** available. If not available, assume the table 1 million rows in size. | |
| 96722 | - */ | |
| 96723 | - if( pFirst ){ | |
| 96724 | - assert( pFirst->aiRowEst!=0 ); /* Allocated together with pFirst */ | |
| 96725 | - aiRowEstPk[0] = pFirst->aiRowEst[0]; | |
| 96726 | - }else{ | |
| 96727 | - aiRowEstPk[0] = 1000000; | |
| 96728 | - } | |
| 96729 | 97350 | pProbe = &sPk; |
| 96730 | 97351 | wsFlagMask = ~( |
| 96731 | 97352 | WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE |
| 96732 | 97353 | ); |
| 96733 | 97354 | eqTermMask = WO_EQ|WO_IN; |
| @@ -98297,11 +98918,11 @@ | ||
| 98297 | 98918 | ** CREATE TABLE t2(c, d); |
| 98298 | 98919 | ** SELECT * FROM t2, t1 WHERE t2.rowid = t1.a; |
| 98299 | 98920 | ** |
| 98300 | 98921 | ** The best strategy is to iterate through table t1 first. However it |
| 98301 | 98922 | ** is not possible to determine this with a simple greedy algorithm. |
| 98302 | - ** However, since the cost of a linear scan through table t2 is the same | |
| 98923 | + ** Since the cost of a linear scan through table t2 is the same | |
| 98303 | 98924 | ** as the cost of a linear scan through table t1, a simple greedy |
| 98304 | 98925 | ** algorithm may choose to use t2 for the outer loop, which is a much |
| 98305 | 98926 | ** costlier approach. |
| 98306 | 98927 | */ |
| 98307 | 98928 | nUnconstrained = 0; |
| @@ -103366,19 +103987,37 @@ | ||
| 103366 | 103987 | |
| 103367 | 103988 | /************** End of sqliteicu.h *******************************************/ |
| 103368 | 103989 | /************** Continuing where we left off in main.c ***********************/ |
| 103369 | 103990 | #endif |
| 103370 | 103991 | |
| 103371 | -/* | |
| 103372 | -** The version of the library | |
| 103373 | -*/ | |
| 103374 | 103992 | #ifndef SQLITE_AMALGAMATION |
| 103993 | +/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant | |
| 103994 | +** contains the text of SQLITE_VERSION macro. | |
| 103995 | +*/ | |
| 103375 | 103996 | SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 103376 | 103997 | #endif |
| 103998 | + | |
| 103999 | +/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns | |
| 104000 | +** a pointer to the to the sqlite3_version[] string constant. | |
| 104001 | +*/ | |
| 103377 | 104002 | SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } |
| 104003 | + | |
| 104004 | +/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a | |
| 104005 | +** pointer to a string constant whose value is the same as the | |
| 104006 | +** SQLITE_SOURCE_ID C preprocessor macro. | |
| 104007 | +*/ | |
| 103378 | 104008 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 104009 | + | |
| 104010 | +/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function | |
| 104011 | +** returns an integer equal to SQLITE_VERSION_NUMBER. | |
| 104012 | +*/ | |
| 103379 | 104013 | SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
| 104014 | + | |
| 104015 | +/* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns | |
| 104016 | +** zero if and only if SQLite was compiled mutexing code omitted due to | |
| 104017 | +** the SQLITE_THREADSAFE compile-time option being set to 0. | |
| 104018 | +*/ | |
| 103380 | 104019 | SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
| 103381 | 104020 | |
| 103382 | 104021 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 103383 | 104022 | /* |
| 103384 | 104023 | ** If the following function pointer is not NULL and if |
| @@ -103495,10 +104134,17 @@ | ||
| 103495 | 104134 | /* Do the rest of the initialization under the recursive mutex so |
| 103496 | 104135 | ** that we will be able to handle recursive calls into |
| 103497 | 104136 | ** sqlite3_initialize(). The recursive calls normally come through |
| 103498 | 104137 | ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 103499 | 104138 | ** recursive calls might also be possible. |
| 104139 | + ** | |
| 104140 | + ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls | |
| 104141 | + ** to the xInit method, so the xInit method need not be threadsafe. | |
| 104142 | + ** | |
| 104143 | + ** The following mutex is what serializes access to the appdef pcache xInit | |
| 104144 | + ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the | |
| 104145 | + ** call to sqlite3PcacheInitialize(). | |
| 103500 | 104146 | */ |
| 103501 | 104147 | sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
| 103502 | 104148 | if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
| 103503 | 104149 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 103504 | 104150 | sqlite3GlobalConfig.inProgress = 1; |
| @@ -103775,16 +104421,16 @@ | ||
| 103775 | 104421 | if( cnt<0 ) cnt = 0; |
| 103776 | 104422 | if( sz==0 || cnt==0 ){ |
| 103777 | 104423 | sz = 0; |
| 103778 | 104424 | pStart = 0; |
| 103779 | 104425 | }else if( pBuf==0 ){ |
| 103780 | - sz = ROUND8(sz); | |
| 104426 | + sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ | |
| 103781 | 104427 | sqlite3BeginBenignMalloc(); |
| 103782 | - pStart = sqlite3Malloc( sz*cnt ); | |
| 104428 | + pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ | |
| 103783 | 104429 | sqlite3EndBenignMalloc(); |
| 103784 | 104430 | }else{ |
| 103785 | - sz = ROUNDDOWN8(sz); | |
| 104431 | + sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ | |
| 103786 | 104432 | pStart = pBuf; |
| 103787 | 104433 | } |
| 103788 | 104434 | db->lookaside.pStart = pStart; |
| 103789 | 104435 | db->lookaside.pFree = 0; |
| 103790 | 104436 | db->lookaside.sz = (u16)sz; |
| @@ -103823,18 +104469,18 @@ | ||
| 103823 | 104469 | va_list ap; |
| 103824 | 104470 | int rc; |
| 103825 | 104471 | va_start(ap, op); |
| 103826 | 104472 | switch( op ){ |
| 103827 | 104473 | case SQLITE_DBCONFIG_LOOKASIDE: { |
| 103828 | - void *pBuf = va_arg(ap, void*); | |
| 103829 | - int sz = va_arg(ap, int); | |
| 103830 | - int cnt = va_arg(ap, int); | |
| 104474 | + void *pBuf = va_arg(ap, void*); /* IMP: R-21112-12275 */ | |
| 104475 | + int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ | |
| 104476 | + int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ | |
| 103831 | 104477 | rc = setupLookaside(db, pBuf, sz, cnt); |
| 103832 | 104478 | break; |
| 103833 | 104479 | } |
| 103834 | 104480 | default: { |
| 103835 | - rc = SQLITE_ERROR; | |
| 104481 | + rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ | |
| 103836 | 104482 | break; |
| 103837 | 104483 | } |
| 103838 | 104484 | } |
| 103839 | 104485 | va_end(ap); |
| 103840 | 104486 | return rc; |
| @@ -103934,16 +104580,33 @@ | ||
| 103934 | 104580 | } |
| 103935 | 104581 | db->nSavepoint = 0; |
| 103936 | 104582 | db->nStatement = 0; |
| 103937 | 104583 | db->isTransactionSavepoint = 0; |
| 103938 | 104584 | } |
| 104585 | + | |
| 104586 | +/* | |
| 104587 | +** Invoke the destructor function associated with FuncDef p, if any. Except, | |
| 104588 | +** if this is not the last copy of the function, do not invoke it. Multiple | |
| 104589 | +** copies of a single function are created when create_function() is called | |
| 104590 | +** with SQLITE_ANY as the encoding. | |
| 104591 | +*/ | |
| 104592 | +static void functionDestroy(sqlite3 *db, FuncDef *p){ | |
| 104593 | + FuncDestructor *pDestructor = p->pDestructor; | |
| 104594 | + if( pDestructor ){ | |
| 104595 | + pDestructor->nRef--; | |
| 104596 | + if( pDestructor->nRef==0 ){ | |
| 104597 | + pDestructor->xDestroy(pDestructor->pUserData); | |
| 104598 | + sqlite3DbFree(db, pDestructor); | |
| 104599 | + } | |
| 104600 | + } | |
| 104601 | +} | |
| 103939 | 104602 | |
| 103940 | 104603 | /* |
| 103941 | 104604 | ** Close an existing SQLite database |
| 103942 | 104605 | */ |
| 103943 | 104606 | SQLITE_API int sqlite3_close(sqlite3 *db){ |
| 103944 | - HashElem *i; | |
| 104607 | + HashElem *i; /* Hash table iterator */ | |
| 103945 | 104608 | int j; |
| 103946 | 104609 | |
| 103947 | 104610 | if( !db ){ |
| 103948 | 104611 | return SQLITE_OK; |
| 103949 | 104612 | } |
| @@ -104007,10 +104670,11 @@ | ||
| 104007 | 104670 | for(j=0; j<ArraySize(db->aFunc.a); j++){ |
| 104008 | 104671 | FuncDef *pNext, *pHash, *p; |
| 104009 | 104672 | for(p=db->aFunc.a[j]; p; p=pHash){ |
| 104010 | 104673 | pHash = p->pHash; |
| 104011 | 104674 | while( p ){ |
| 104675 | + functionDestroy(db, p); | |
| 104012 | 104676 | pNext = p->pNext; |
| 104013 | 104677 | sqlite3DbFree(db, p); |
| 104014 | 104678 | p = pNext; |
| 104015 | 104679 | } |
| 104016 | 104680 | } |
| @@ -104281,11 +104945,12 @@ | ||
| 104281 | 104945 | int nArg, |
| 104282 | 104946 | int enc, |
| 104283 | 104947 | void *pUserData, |
| 104284 | 104948 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 104285 | 104949 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 104286 | - void (*xFinal)(sqlite3_context*) | |
| 104950 | + void (*xFinal)(sqlite3_context*), | |
| 104951 | + FuncDestructor *pDestructor | |
| 104287 | 104952 | ){ |
| 104288 | 104953 | FuncDef *p; |
| 104289 | 104954 | int nName; |
| 104290 | 104955 | |
| 104291 | 104956 | assert( sqlite3_mutex_held(db->mutex) ); |
| @@ -104309,14 +104974,14 @@ | ||
| 104309 | 104974 | if( enc==SQLITE_UTF16 ){ |
| 104310 | 104975 | enc = SQLITE_UTF16NATIVE; |
| 104311 | 104976 | }else if( enc==SQLITE_ANY ){ |
| 104312 | 104977 | int rc; |
| 104313 | 104978 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
| 104314 | - pUserData, xFunc, xStep, xFinal); | |
| 104979 | + pUserData, xFunc, xStep, xFinal, pDestructor); | |
| 104315 | 104980 | if( rc==SQLITE_OK ){ |
| 104316 | 104981 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
| 104317 | - pUserData, xFunc, xStep, xFinal); | |
| 104982 | + pUserData, xFunc, xStep, xFinal, pDestructor); | |
| 104318 | 104983 | } |
| 104319 | 104984 | if( rc!=SQLITE_OK ){ |
| 104320 | 104985 | return rc; |
| 104321 | 104986 | } |
| 104322 | 104987 | enc = SQLITE_UTF16BE; |
| @@ -104345,10 +105010,19 @@ | ||
| 104345 | 105010 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
| 104346 | 105011 | assert(p || db->mallocFailed); |
| 104347 | 105012 | if( !p ){ |
| 104348 | 105013 | return SQLITE_NOMEM; |
| 104349 | 105014 | } |
| 105015 | + | |
| 105016 | + /* If an older version of the function with a configured destructor is | |
| 105017 | + ** being replaced invoke the destructor function here. */ | |
| 105018 | + functionDestroy(db, p); | |
| 105019 | + | |
| 105020 | + if( pDestructor ){ | |
| 105021 | + pDestructor->nRef++; | |
| 105022 | + } | |
| 105023 | + p->pDestructor = pDestructor; | |
| 104350 | 105024 | p->flags = 0; |
| 104351 | 105025 | p->xFunc = xFunc; |
| 104352 | 105026 | p->xStep = xStep; |
| 104353 | 105027 | p->xFinalize = xFinal; |
| 104354 | 105028 | p->pUserData = pUserData; |
| @@ -104359,21 +105033,53 @@ | ||
| 104359 | 105033 | /* |
| 104360 | 105034 | ** Create new user functions. |
| 104361 | 105035 | */ |
| 104362 | 105036 | SQLITE_API int sqlite3_create_function( |
| 104363 | 105037 | sqlite3 *db, |
| 104364 | - const char *zFunctionName, | |
| 105038 | + const char *zFunc, | |
| 104365 | 105039 | int nArg, |
| 104366 | 105040 | int enc, |
| 104367 | 105041 | void *p, |
| 104368 | 105042 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 104369 | 105043 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 104370 | 105044 | void (*xFinal)(sqlite3_context*) |
| 104371 | 105045 | ){ |
| 104372 | - int rc; | |
| 105046 | + return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep, | |
| 105047 | + xFinal, 0); | |
| 105048 | +} | |
| 105049 | + | |
| 105050 | +SQLITE_API int sqlite3_create_function_v2( | |
| 105051 | + sqlite3 *db, | |
| 105052 | + const char *zFunc, | |
| 105053 | + int nArg, | |
| 105054 | + int enc, | |
| 105055 | + void *p, | |
| 105056 | + void (*xFunc)(sqlite3_context*,int,sqlite3_value **), | |
| 105057 | + void (*xStep)(sqlite3_context*,int,sqlite3_value **), | |
| 105058 | + void (*xFinal)(sqlite3_context*), | |
| 105059 | + void (*xDestroy)(void *) | |
| 105060 | +){ | |
| 105061 | + int rc = SQLITE_ERROR; | |
| 105062 | + FuncDestructor *pArg = 0; | |
| 104373 | 105063 | sqlite3_mutex_enter(db->mutex); |
| 104374 | - rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); | |
| 105064 | + if( xDestroy ){ | |
| 105065 | + pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); | |
| 105066 | + if( !pArg ){ | |
| 105067 | + xDestroy(p); | |
| 105068 | + goto out; | |
| 105069 | + } | |
| 105070 | + pArg->xDestroy = xDestroy; | |
| 105071 | + pArg->pUserData = p; | |
| 105072 | + } | |
| 105073 | + rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg); | |
| 105074 | + if( pArg && pArg->nRef==0 ){ | |
| 105075 | + assert( rc!=SQLITE_OK ); | |
| 105076 | + xDestroy(p); | |
| 105077 | + sqlite3DbFree(db, pArg); | |
| 105078 | + } | |
| 105079 | + | |
| 105080 | + out: | |
| 104375 | 105081 | rc = sqlite3ApiExit(db, rc); |
| 104376 | 105082 | sqlite3_mutex_leave(db->mutex); |
| 104377 | 105083 | return rc; |
| 104378 | 105084 | } |
| 104379 | 105085 | |
| @@ -104391,11 +105097,11 @@ | ||
| 104391 | 105097 | int rc; |
| 104392 | 105098 | char *zFunc8; |
| 104393 | 105099 | sqlite3_mutex_enter(db->mutex); |
| 104394 | 105100 | assert( !db->mallocFailed ); |
| 104395 | 105101 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 104396 | - rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); | |
| 105102 | + rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); | |
| 104397 | 105103 | sqlite3DbFree(db, zFunc8); |
| 104398 | 105104 | rc = sqlite3ApiExit(db, rc); |
| 104399 | 105105 | sqlite3_mutex_leave(db->mutex); |
| 104400 | 105106 | return rc; |
| 104401 | 105107 | } |
| @@ -104422,11 +105128,11 @@ | ||
| 104422 | 105128 | int nName = sqlite3Strlen30(zName); |
| 104423 | 105129 | int rc; |
| 104424 | 105130 | sqlite3_mutex_enter(db->mutex); |
| 104425 | 105131 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 104426 | 105132 | sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 104427 | - 0, sqlite3InvalidFunction, 0, 0); | |
| 105133 | + 0, sqlite3InvalidFunction, 0, 0, 0); | |
| 104428 | 105134 | } |
| 104429 | 105135 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 104430 | 105136 | sqlite3_mutex_leave(db->mutex); |
| 104431 | 105137 | return rc; |
| 104432 | 105138 | } |
| @@ -104560,11 +105266,14 @@ | ||
| 104560 | 105266 | ** registered using sqlite3_wal_hook(). Likewise, registering a callback |
| 104561 | 105267 | ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism |
| 104562 | 105268 | ** configured by this function. |
| 104563 | 105269 | */ |
| 104564 | 105270 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 104565 | -#ifndef SQLITE_OMIT_WAL | |
| 105271 | +#ifdef SQLITE_OMIT_WAL | |
| 105272 | + UNUSED_PARAMETER(db); | |
| 105273 | + UNUSED_PARAMETER(nFrame); | |
| 105274 | +#else | |
| 104566 | 105275 | if( nFrame>0 ){ |
| 104567 | 105276 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 104568 | 105277 | }else{ |
| 104569 | 105278 | sqlite3_wal_hook(db, 0, 0); |
| 104570 | 105279 | } |
| @@ -104690,64 +105399,10 @@ | ||
| 104690 | 105399 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
| 104691 | 105400 | return 0; |
| 104692 | 105401 | #endif |
| 104693 | 105402 | } |
| 104694 | 105403 | |
| 104695 | -/* | |
| 104696 | -** This routine is called to create a connection to a database BTree | |
| 104697 | -** driver. If zFilename is the name of a file, then that file is | |
| 104698 | -** opened and used. If zFilename is the magic name ":memory:" then | |
| 104699 | -** the database is stored in memory (and is thus forgotten as soon as | |
| 104700 | -** the connection is closed.) If zFilename is NULL then the database | |
| 104701 | -** is a "virtual" database for transient use only and is deleted as | |
| 104702 | -** soon as the connection is closed. | |
| 104703 | -** | |
| 104704 | -** A virtual database can be either a disk file (that is automatically | |
| 104705 | -** deleted when the file is closed) or it an be held entirely in memory. | |
| 104706 | -** The sqlite3TempInMemory() function is used to determine which. | |
| 104707 | -*/ | |
| 104708 | -SQLITE_PRIVATE int sqlite3BtreeFactory( | |
| 104709 | - sqlite3 *db, /* Main database when opening aux otherwise 0 */ | |
| 104710 | - const char *zFilename, /* Name of the file containing the BTree database */ | |
| 104711 | - int omitJournal, /* if TRUE then do not journal this file */ | |
| 104712 | - int nCache, /* How many pages in the page cache */ | |
| 104713 | - int vfsFlags, /* Flags passed through to vfsOpen */ | |
| 104714 | - Btree **ppBtree /* Pointer to new Btree object written here */ | |
| 104715 | -){ | |
| 104716 | - int btFlags = 0; | |
| 104717 | - int rc; | |
| 104718 | - | |
| 104719 | - assert( sqlite3_mutex_held(db->mutex) ); | |
| 104720 | - assert( ppBtree != 0); | |
| 104721 | - if( omitJournal ){ | |
| 104722 | - btFlags |= BTREE_OMIT_JOURNAL; | |
| 104723 | - } | |
| 104724 | - if( db->flags & SQLITE_NoReadlock ){ | |
| 104725 | - btFlags |= BTREE_NO_READLOCK; | |
| 104726 | - } | |
| 104727 | -#ifndef SQLITE_OMIT_MEMORYDB | |
| 104728 | - if( zFilename==0 && sqlite3TempInMemory(db) ){ | |
| 104729 | - zFilename = ":memory:"; | |
| 104730 | - } | |
| 104731 | -#endif | |
| 104732 | - | |
| 104733 | - if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){ | |
| 104734 | - vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; | |
| 104735 | - } | |
| 104736 | - rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags); | |
| 104737 | - | |
| 104738 | - /* If the B-Tree was successfully opened, set the pager-cache size to the | |
| 104739 | - ** default value. Except, if the call to BtreeOpen() returned a handle | |
| 104740 | - ** open on an existing shared pager-cache, do not change the pager-cache | |
| 104741 | - ** size. | |
| 104742 | - */ | |
| 104743 | - if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){ | |
| 104744 | - sqlite3BtreeSetCacheSize(*ppBtree, nCache); | |
| 104745 | - } | |
| 104746 | - return rc; | |
| 104747 | -} | |
| 104748 | - | |
| 104749 | 105404 | /* |
| 104750 | 105405 | ** Return UTF-8 encoded English language explanation of the most recent |
| 104751 | 105406 | ** error. |
| 104752 | 105407 | */ |
| 104753 | 105408 | SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ |
| @@ -104986,21 +105641,43 @@ | ||
| 104986 | 105641 | ** It merely prevents new constructs that exceed the limit |
| 104987 | 105642 | ** from forming. |
| 104988 | 105643 | */ |
| 104989 | 105644 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 104990 | 105645 | int oldLimit; |
| 105646 | + | |
| 105647 | + | |
| 105648 | + /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME | |
| 105649 | + ** there is a hard upper bound set at compile-time by a C preprocessor | |
| 105650 | + ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to | |
| 105651 | + ** "_MAX_".) | |
| 105652 | + */ | |
| 105653 | + assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); | |
| 105654 | + assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); | |
| 105655 | + assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); | |
| 105656 | + assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); | |
| 105657 | + assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); | |
| 105658 | + assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); | |
| 105659 | + assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); | |
| 105660 | + assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); | |
| 105661 | + assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== | |
| 105662 | + SQLITE_MAX_LIKE_PATTERN_LENGTH ); | |
| 105663 | + assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); | |
| 105664 | + assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); | |
| 105665 | + assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) ); | |
| 105666 | + | |
| 105667 | + | |
| 104991 | 105668 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 104992 | 105669 | return -1; |
| 104993 | 105670 | } |
| 104994 | 105671 | oldLimit = db->aLimit[limitId]; |
| 104995 | - if( newLimit>=0 ){ | |
| 105672 | + if( newLimit>=0 ){ /* IMP: R-52476-28732 */ | |
| 104996 | 105673 | if( newLimit>aHardLimit[limitId] ){ |
| 104997 | - newLimit = aHardLimit[limitId]; | |
| 105674 | + newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ | |
| 104998 | 105675 | } |
| 104999 | 105676 | db->aLimit[limitId] = newLimit; |
| 105000 | 105677 | } |
| 105001 | - return oldLimit; | |
| 105678 | + return oldLimit; /* IMP: R-53341-35419 */ | |
| 105002 | 105679 | } |
| 105003 | 105680 | |
| 105004 | 105681 | /* |
| 105005 | 105682 | ** This routine does the work of opening a database on behalf of |
| 105006 | 105683 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| @@ -105019,10 +105696,28 @@ | ||
| 105019 | 105696 | *ppDb = 0; |
| 105020 | 105697 | #ifndef SQLITE_OMIT_AUTOINIT |
| 105021 | 105698 | rc = sqlite3_initialize(); |
| 105022 | 105699 | if( rc ) return rc; |
| 105023 | 105700 | #endif |
| 105701 | + | |
| 105702 | + /* Only allow sensible combinations of bits in the flags argument. | |
| 105703 | + ** Throw an error if any non-sense combination is used. If we | |
| 105704 | + ** do not block illegal combinations here, it could trigger | |
| 105705 | + ** assert() statements in deeper layers. Sensible combinations | |
| 105706 | + ** are: | |
| 105707 | + ** | |
| 105708 | + ** 1: SQLITE_OPEN_READONLY | |
| 105709 | + ** 2: SQLITE_OPEN_READWRITE | |
| 105710 | + ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 105711 | + */ | |
| 105712 | + assert( SQLITE_OPEN_READONLY == 0x01 ); | |
| 105713 | + assert( SQLITE_OPEN_READWRITE == 0x02 ); | |
| 105714 | + assert( SQLITE_OPEN_CREATE == 0x04 ); | |
| 105715 | + testcase( (1<<(flags&7))==0x02 ); /* READONLY */ | |
| 105716 | + testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ | |
| 105717 | + testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ | |
| 105718 | + if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE; | |
| 105024 | 105719 | |
| 105025 | 105720 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 105026 | 105721 | isThreadsafe = 0; |
| 105027 | 105722 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 105028 | 105723 | isThreadsafe = 0; |
| @@ -105053,11 +105748,12 @@ | ||
| 105053 | 105748 | SQLITE_OPEN_MAIN_JOURNAL | |
| 105054 | 105749 | SQLITE_OPEN_TEMP_JOURNAL | |
| 105055 | 105750 | SQLITE_OPEN_SUBJOURNAL | |
| 105056 | 105751 | SQLITE_OPEN_MASTER_JOURNAL | |
| 105057 | 105752 | SQLITE_OPEN_NOMUTEX | |
| 105058 | - SQLITE_OPEN_FULLMUTEX | |
| 105753 | + SQLITE_OPEN_FULLMUTEX | | |
| 105754 | + SQLITE_OPEN_WAL | |
| 105059 | 105755 | ); |
| 105060 | 105756 | |
| 105061 | 105757 | /* Allocate the sqlite data structure */ |
| 105062 | 105758 | db = sqlite3MallocZero( sizeof(sqlite3) ); |
| 105063 | 105759 | if( db==0 ) goto opendb_out; |
| @@ -105125,13 +105821,12 @@ | ||
| 105125 | 105821 | createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, |
| 105126 | 105822 | nocaseCollatingFunc, 0); |
| 105127 | 105823 | |
| 105128 | 105824 | /* Open the backend database driver */ |
| 105129 | 105825 | db->openFlags = flags; |
| 105130 | - rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, | |
| 105131 | - flags | SQLITE_OPEN_MAIN_DB, | |
| 105132 | - &db->aDb[0].pBt); | |
| 105826 | + rc = sqlite3BtreeOpen(zFilename, db, &db->aDb[0].pBt, 0, | |
| 105827 | + flags | SQLITE_OPEN_MAIN_DB); | |
| 105133 | 105828 | if( rc!=SQLITE_OK ){ |
| 105134 | 105829 | if( rc==SQLITE_IOERR_NOMEM ){ |
| 105135 | 105830 | rc = SQLITE_NOMEM; |
| 105136 | 105831 | } |
| 105137 | 105832 | sqlite3Error(db, rc, 0); |
| @@ -105833,10 +106528,26 @@ | ||
| 105833 | 106528 | */ |
| 105834 | 106529 | case SQLITE_TESTCTRL_PGHDRSZ: { |
| 105835 | 106530 | rc = sizeof(PgHdr); |
| 105836 | 106531 | break; |
| 105837 | 106532 | } |
| 106533 | + | |
| 106534 | + /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); | |
| 106535 | + ** | |
| 106536 | + ** Pass pFree into sqlite3ScratchFree(). | |
| 106537 | + ** If sz>0 then allocate a scratch buffer into pNew. | |
| 106538 | + */ | |
| 106539 | + case SQLITE_TESTCTRL_SCRATCHMALLOC: { | |
| 106540 | + void *pFree, **ppNew; | |
| 106541 | + int sz; | |
| 106542 | + sz = va_arg(ap, int); | |
| 106543 | + ppNew = va_arg(ap, void**); | |
| 106544 | + pFree = va_arg(ap, void*); | |
| 106545 | + if( sz ) *ppNew = sqlite3ScratchMalloc(sz); | |
| 106546 | + sqlite3ScratchFree(pFree); | |
| 106547 | + break; | |
| 106548 | + } | |
| 105838 | 106549 | |
| 105839 | 106550 | } |
| 105840 | 106551 | va_end(ap); |
| 105841 | 106552 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
| 105842 | 106553 | return rc; |
| @@ -107022,10 +107733,11 @@ | ||
| 107022 | 107733 | ); |
| 107023 | 107734 | SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char const**, int*); |
| 107024 | 107735 | SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **); |
| 107025 | 107736 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeLocal(Fts3Cursor*, u32*); |
| 107026 | 107737 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeGlobal(Fts3Cursor*, u32*); |
| 107738 | +SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *); | |
| 107027 | 107739 | |
| 107028 | 107740 | /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */ |
| 107029 | 107741 | #define FTS3_SEGMENT_REQUIRE_POS 0x00000001 |
| 107030 | 107742 | #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 |
| 107031 | 107743 | #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 |
| @@ -108971,10 +109683,13 @@ | ||
| 108971 | 109683 | zQuery); |
| 108972 | 109684 | } |
| 108973 | 109685 | return rc; |
| 108974 | 109686 | } |
| 108975 | 109687 | |
| 109688 | + rc = sqlite3Fts3ReadLock(p); | |
| 109689 | + if( rc!=SQLITE_OK ) return rc; | |
| 109690 | + | |
| 108976 | 109691 | rc = evalFts3Expr(p, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0); |
| 108977 | 109692 | pCsr->pNextId = pCsr->aDoclist; |
| 108978 | 109693 | pCsr->iPrevId = 0; |
| 108979 | 109694 | } |
| 108980 | 109695 | |
| @@ -109349,15 +110064,18 @@ | ||
| 109349 | 110064 | static int fts3RenameMethod( |
| 109350 | 110065 | sqlite3_vtab *pVtab, /* Virtual table handle */ |
| 109351 | 110066 | const char *zName /* New name of table */ |
| 109352 | 110067 | ){ |
| 109353 | 110068 | Fts3Table *p = (Fts3Table *)pVtab; |
| 109354 | - sqlite3 *db; /* Database connection */ | |
| 110069 | + sqlite3 *db = p->db; /* Database connection */ | |
| 109355 | 110070 | int rc; /* Return Code */ |
| 109356 | - | |
| 109357 | - db = p->db; | |
| 109358 | - rc = SQLITE_OK; | |
| 110071 | + | |
| 110072 | + rc = sqlite3Fts3PendingTermsFlush(p); | |
| 110073 | + if( rc!=SQLITE_OK ){ | |
| 110074 | + return rc; | |
| 110075 | + } | |
| 110076 | + | |
| 109359 | 110077 | fts3DbExec(&rc, db, |
| 109360 | 110078 | "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 109361 | 110079 | p->zDb, p->zName, zName |
| 109362 | 110080 | ); |
| 109363 | 110081 | if( rc==SQLITE_ERROR ) rc = SQLITE_OK; |
| @@ -112512,10 +113230,40 @@ | ||
| 112512 | 113230 | return SQLITE_CORRUPT; |
| 112513 | 113231 | } |
| 112514 | 113232 | } |
| 112515 | 113233 | return SQLITE_OK; |
| 112516 | 113234 | } |
| 113235 | + | |
| 113236 | +/* | |
| 113237 | +** This function ensures that the caller has obtained a shared-cache | |
| 113238 | +** table-lock on the %_content table. This is required before reading | |
| 113239 | +** data from the fts3 table. If this lock is not acquired first, then | |
| 113240 | +** the caller may end up holding read-locks on the %_segments and %_segdir | |
| 113241 | +** tables, but no read-lock on the %_content table. If this happens | |
| 113242 | +** a second connection will be able to write to the fts3 table, but | |
| 113243 | +** attempting to commit those writes might return SQLITE_LOCKED or | |
| 113244 | +** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain | |
| 113245 | +** write-locks on the %_segments and %_segdir ** tables). | |
| 113246 | +** | |
| 113247 | +** We try to avoid this because if FTS3 returns any error when committing | |
| 113248 | +** a transaction, the whole transaction will be rolled back. And this is | |
| 113249 | +** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can | |
| 113250 | +** still happen if the user reads data directly from the %_segments or | |
| 113251 | +** %_segdir tables instead of going through FTS3 though. | |
| 113252 | +*/ | |
| 113253 | +SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){ | |
| 113254 | + int rc; /* Return code */ | |
| 113255 | + sqlite3_stmt *pStmt; /* Statement used to obtain lock */ | |
| 113256 | + | |
| 113257 | + rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0); | |
| 113258 | + if( rc==SQLITE_OK ){ | |
| 113259 | + sqlite3_bind_null(pStmt, 1); | |
| 113260 | + sqlite3_step(pStmt); | |
| 113261 | + rc = sqlite3_reset(pStmt); | |
| 113262 | + } | |
| 113263 | + return rc; | |
| 113264 | +} | |
| 112517 | 113265 | |
| 112518 | 113266 | /* |
| 112519 | 113267 | ** Set *ppStmt to a statement handle that may be used to iterate through |
| 112520 | 113268 | ** all rows in the %_segdir table, from oldest to newest. If successful, |
| 112521 | 113269 | ** return SQLITE_OK. If an error occurs while preparing the statement, |
| @@ -116003,10 +116751,49 @@ | ||
| 116003 | 116751 | ** |
| 116004 | 116752 | ************************************************************************* |
| 116005 | 116753 | ** This file contains code for implementations of the r-tree and r*-tree |
| 116006 | 116754 | ** algorithms packaged as an SQLite virtual table module. |
| 116007 | 116755 | */ |
| 116756 | + | |
| 116757 | +/* | |
| 116758 | +** Database Format of R-Tree Tables | |
| 116759 | +** -------------------------------- | |
| 116760 | +** | |
| 116761 | +** The data structure for a single virtual r-tree table is stored in three | |
| 116762 | +** native SQLite tables declared as follows. In each case, the '%' character | |
| 116763 | +** in the table name is replaced with the user-supplied name of the r-tree | |
| 116764 | +** table. | |
| 116765 | +** | |
| 116766 | +** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) | |
| 116767 | +** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) | |
| 116768 | +** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) | |
| 116769 | +** | |
| 116770 | +** The data for each node of the r-tree structure is stored in the %_node | |
| 116771 | +** table. For each node that is not the root node of the r-tree, there is | |
| 116772 | +** an entry in the %_parent table associating the node with its parent. | |
| 116773 | +** And for each row of data in the table, there is an entry in the %_rowid | |
| 116774 | +** table that maps from the entries rowid to the id of the node that it | |
| 116775 | +** is stored on. | |
| 116776 | +** | |
| 116777 | +** The root node of an r-tree always exists, even if the r-tree table is | |
| 116778 | +** empty. The nodeno of the root node is always 1. All other nodes in the | |
| 116779 | +** table must be the same size as the root node. The content of each node | |
| 116780 | +** is formatted as follows: | |
| 116781 | +** | |
| 116782 | +** 1. If the node is the root node (node 1), then the first 2 bytes | |
| 116783 | +** of the node contain the tree depth as a big-endian integer. | |
| 116784 | +** For non-root nodes, the first 2 bytes are left unused. | |
| 116785 | +** | |
| 116786 | +** 2. The next 2 bytes contain the number of entries currently | |
| 116787 | +** stored in the node. | |
| 116788 | +** | |
| 116789 | +** 3. The remainder of the node contains the node entries. Each entry | |
| 116790 | +** consists of a single 8-byte integer followed by an even number | |
| 116791 | +** of 4-byte coordinates. For leaf nodes the integer is the rowid | |
| 116792 | +** of a record. For internal nodes it is the node number of a | |
| 116793 | +** child page. | |
| 116794 | +*/ | |
| 116008 | 116795 | |
| 116009 | 116796 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) |
| 116010 | 116797 | |
| 116011 | 116798 | /* |
| 116012 | 116799 | ** This file contains an implementation of a couple of different variants |
| @@ -116044,18 +116831,22 @@ | ||
| 116044 | 116831 | #endif |
| 116045 | 116832 | #if VARIANT_RSTARTREE_SPLIT |
| 116046 | 116833 | #define AssignCells splitNodeStartree |
| 116047 | 116834 | #endif |
| 116048 | 116835 | |
| 116836 | +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) | |
| 116837 | +# define NDEBUG 1 | |
| 116838 | +#endif | |
| 116049 | 116839 | |
| 116050 | 116840 | #ifndef SQLITE_CORE |
| 116051 | 116841 | SQLITE_EXTENSION_INIT1 |
| 116052 | 116842 | #else |
| 116053 | 116843 | #endif |
| 116054 | 116844 | |
| 116055 | 116845 | |
| 116056 | 116846 | #ifndef SQLITE_AMALGAMATION |
| 116847 | +#include "sqlite3rtree.h" | |
| 116057 | 116848 | typedef sqlite3_int64 i64; |
| 116058 | 116849 | typedef unsigned char u8; |
| 116059 | 116850 | typedef unsigned int u32; |
| 116060 | 116851 | #endif |
| 116061 | 116852 | |
| @@ -116062,10 +116853,12 @@ | ||
| 116062 | 116853 | typedef struct Rtree Rtree; |
| 116063 | 116854 | typedef struct RtreeCursor RtreeCursor; |
| 116064 | 116855 | typedef struct RtreeNode RtreeNode; |
| 116065 | 116856 | typedef struct RtreeCell RtreeCell; |
| 116066 | 116857 | typedef struct RtreeConstraint RtreeConstraint; |
| 116858 | +typedef struct RtreeMatchArg RtreeMatchArg; | |
| 116859 | +typedef struct RtreeGeomCallback RtreeGeomCallback; | |
| 116067 | 116860 | typedef union RtreeCoord RtreeCoord; |
| 116068 | 116861 | |
| 116069 | 116862 | /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ |
| 116070 | 116863 | #define RTREE_MAX_DIMENSIONS 5 |
| 116071 | 116864 | |
| @@ -116131,10 +116924,19 @@ | ||
| 116131 | 116924 | */ |
| 116132 | 116925 | #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) |
| 116133 | 116926 | #define RTREE_REINSERT(p) RTREE_MINCELLS(p) |
| 116134 | 116927 | #define RTREE_MAXCELLS 51 |
| 116135 | 116928 | |
| 116929 | +/* | |
| 116930 | +** The smallest possible node-size is (512-64)==448 bytes. And the largest | |
| 116931 | +** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). | |
| 116932 | +** Therefore all non-root nodes must contain at least 3 entries. Since | |
| 116933 | +** 2^40 is greater than 2^64, an r-tree structure always has a depth of | |
| 116934 | +** 40 or less. | |
| 116935 | +*/ | |
| 116936 | +#define RTREE_MAX_DEPTH 40 | |
| 116937 | + | |
| 116136 | 116938 | /* |
| 116137 | 116939 | ** An rtree cursor object. |
| 116138 | 116940 | */ |
| 116139 | 116941 | struct RtreeCursor { |
| 116140 | 116942 | sqlite3_vtab_cursor base; |
| @@ -116163,39 +116965,27 @@ | ||
| 116163 | 116965 | |
| 116164 | 116966 | /* |
| 116165 | 116967 | ** A search constraint. |
| 116166 | 116968 | */ |
| 116167 | 116969 | struct RtreeConstraint { |
| 116168 | - int iCoord; /* Index of constrained coordinate */ | |
| 116169 | - int op; /* Constraining operation */ | |
| 116170 | - double rValue; /* Constraint value. */ | |
| 116970 | + int iCoord; /* Index of constrained coordinate */ | |
| 116971 | + int op; /* Constraining operation */ | |
| 116972 | + double rValue; /* Constraint value. */ | |
| 116973 | + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); | |
| 116974 | + sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */ | |
| 116171 | 116975 | }; |
| 116172 | 116976 | |
| 116173 | 116977 | /* Possible values for RtreeConstraint.op */ |
| 116174 | -#define RTREE_EQ 0x41 | |
| 116175 | -#define RTREE_LE 0x42 | |
| 116176 | -#define RTREE_LT 0x43 | |
| 116177 | -#define RTREE_GE 0x44 | |
| 116178 | -#define RTREE_GT 0x45 | |
| 116978 | +#define RTREE_EQ 0x41 | |
| 116979 | +#define RTREE_LE 0x42 | |
| 116980 | +#define RTREE_LT 0x43 | |
| 116981 | +#define RTREE_GE 0x44 | |
| 116982 | +#define RTREE_GT 0x45 | |
| 116983 | +#define RTREE_MATCH 0x46 | |
| 116179 | 116984 | |
| 116180 | 116985 | /* |
| 116181 | 116986 | ** An rtree structure node. |
| 116182 | -** | |
| 116183 | -** Data format (RtreeNode.zData): | |
| 116184 | -** | |
| 116185 | -** 1. If the node is the root node (node 1), then the first 2 bytes | |
| 116186 | -** of the node contain the tree depth as a big-endian integer. | |
| 116187 | -** For non-root nodes, the first 2 bytes are left unused. | |
| 116188 | -** | |
| 116189 | -** 2. The next 2 bytes contain the number of entries currently | |
| 116190 | -** stored in the node. | |
| 116191 | -** | |
| 116192 | -** 3. The remainder of the node contains the node entries. Each entry | |
| 116193 | -** consists of a single 8-byte integer followed by an even number | |
| 116194 | -** of 4-byte coordinates. For leaf nodes the integer is the rowid | |
| 116195 | -** of a record. For internal nodes it is the node number of a | |
| 116196 | -** child page. | |
| 116197 | 116987 | */ |
| 116198 | 116988 | struct RtreeNode { |
| 116199 | 116989 | RtreeNode *pParent; /* Parent node */ |
| 116200 | 116990 | i64 iNode; |
| 116201 | 116991 | int nRef; |
| @@ -116211,10 +117001,44 @@ | ||
| 116211 | 117001 | struct RtreeCell { |
| 116212 | 117002 | i64 iRowid; |
| 116213 | 117003 | RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; |
| 116214 | 117004 | }; |
| 116215 | 117005 | |
| 117006 | + | |
| 117007 | +/* | |
| 117008 | +** Value for the first field of every RtreeMatchArg object. The MATCH | |
| 117009 | +** operator tests that the first field of a blob operand matches this | |
| 117010 | +** value to avoid operating on invalid blobs (which could cause a segfault). | |
| 117011 | +*/ | |
| 117012 | +#define RTREE_GEOMETRY_MAGIC 0x891245AB | |
| 117013 | + | |
| 117014 | +/* | |
| 117015 | +** An instance of this structure must be supplied as a blob argument to | |
| 117016 | +** the right-hand-side of an SQL MATCH operator used to constrain an | |
| 117017 | +** r-tree query. | |
| 117018 | +*/ | |
| 117019 | +struct RtreeMatchArg { | |
| 117020 | + u32 magic; /* Always RTREE_GEOMETRY_MAGIC */ | |
| 117021 | + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); | |
| 117022 | + void *pContext; | |
| 117023 | + int nParam; | |
| 117024 | + double aParam[1]; | |
| 117025 | +}; | |
| 117026 | + | |
| 117027 | +/* | |
| 117028 | +** When a geometry callback is created (see sqlite3_rtree_geometry_callback), | |
| 117029 | +** a single instance of the following structure is allocated. It is used | |
| 117030 | +** as the context for the user-function created by by s_r_g_c(). The object | |
| 117031 | +** is eventually deleted by the destructor mechanism provided by | |
| 117032 | +** sqlite3_create_function_v2() (which is called by s_r_g_c() to create | |
| 117033 | +** the geometry callback function). | |
| 117034 | +*/ | |
| 117035 | +struct RtreeGeomCallback { | |
| 117036 | + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); | |
| 117037 | + void *pContext; | |
| 117038 | +}; | |
| 117039 | + | |
| 116216 | 117040 | #ifndef MAX |
| 116217 | 117041 | # define MAX(x,y) ((x) < (y) ? (y) : (x)) |
| 116218 | 117042 | #endif |
| 116219 | 117043 | #ifndef MIN |
| 116220 | 117044 | # define MIN(x,y) ((x) > (y) ? (y) : (x)) |
| @@ -116293,14 +117117,12 @@ | ||
| 116293 | 117117 | |
| 116294 | 117118 | /* |
| 116295 | 117119 | ** Clear the content of node p (set all bytes to 0x00). |
| 116296 | 117120 | */ |
| 116297 | 117121 | static void nodeZero(Rtree *pRtree, RtreeNode *p){ |
| 116298 | - if( p ){ | |
| 116299 | - memset(&p->zData[2], 0, pRtree->iNodeSize-2); | |
| 116300 | - p->isDirty = 1; | |
| 116301 | - } | |
| 117122 | + memset(&p->zData[2], 0, pRtree->iNodeSize-2); | |
| 117123 | + p->isDirty = 1; | |
| 116302 | 117124 | } |
| 116303 | 117125 | |
| 116304 | 117126 | /* |
| 116305 | 117127 | ** Given a node number iNode, return the corresponding key to use |
| 116306 | 117128 | ** in the Rtree.aHash table. |
| @@ -116316,26 +117138,23 @@ | ||
| 116316 | 117138 | ** Search the node hash table for node iNode. If found, return a pointer |
| 116317 | 117139 | ** to it. Otherwise, return 0. |
| 116318 | 117140 | */ |
| 116319 | 117141 | static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ |
| 116320 | 117142 | RtreeNode *p; |
| 116321 | - assert( iNode!=0 ); | |
| 116322 | 117143 | for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); |
| 116323 | 117144 | return p; |
| 116324 | 117145 | } |
| 116325 | 117146 | |
| 116326 | 117147 | /* |
| 116327 | 117148 | ** Add node pNode to the node hash table. |
| 116328 | 117149 | */ |
| 116329 | 117150 | static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ |
| 116330 | - if( pNode ){ | |
| 116331 | - int iHash; | |
| 116332 | - assert( pNode->pNext==0 ); | |
| 116333 | - iHash = nodeHash(pNode->iNode); | |
| 116334 | - pNode->pNext = pRtree->aHash[iHash]; | |
| 116335 | - pRtree->aHash[iHash] = pNode; | |
| 116336 | - } | |
| 117151 | + int iHash; | |
| 117152 | + assert( pNode->pNext==0 ); | |
| 117153 | + iHash = nodeHash(pNode->iNode); | |
| 117154 | + pNode->pNext = pRtree->aHash[iHash]; | |
| 117155 | + pRtree->aHash[iHash] = pNode; | |
| 116337 | 117156 | } |
| 116338 | 117157 | |
| 116339 | 117158 | /* |
| 116340 | 117159 | ** Remove node pNode from the node hash table. |
| 116341 | 117160 | */ |
| @@ -116353,15 +117172,15 @@ | ||
| 116353 | 117172 | ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), |
| 116354 | 117173 | ** indicating that node has not yet been assigned a node number. It is |
| 116355 | 117174 | ** assigned a node number when nodeWrite() is called to write the |
| 116356 | 117175 | ** node contents out to the database. |
| 116357 | 117176 | */ |
| 116358 | -static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent, int zero){ | |
| 117177 | +static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){ | |
| 116359 | 117178 | RtreeNode *pNode; |
| 116360 | 117179 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); |
| 116361 | 117180 | if( pNode ){ |
| 116362 | - memset(pNode, 0, sizeof(RtreeNode) + (zero?pRtree->iNodeSize:0)); | |
| 117181 | + memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); | |
| 116363 | 117182 | pNode->zData = (u8 *)&pNode[1]; |
| 116364 | 117183 | pNode->nRef = 1; |
| 116365 | 117184 | pNode->pParent = pParent; |
| 116366 | 117185 | pNode->isDirty = 1; |
| 116367 | 117186 | nodeReference(pParent); |
| @@ -116378,10 +117197,11 @@ | ||
| 116378 | 117197 | i64 iNode, /* Node number to load */ |
| 116379 | 117198 | RtreeNode *pParent, /* Either the parent node or NULL */ |
| 116380 | 117199 | RtreeNode **ppNode /* OUT: Acquired node */ |
| 116381 | 117200 | ){ |
| 116382 | 117201 | int rc; |
| 117202 | + int rc2 = SQLITE_OK; | |
| 116383 | 117203 | RtreeNode *pNode; |
| 116384 | 117204 | |
| 116385 | 117205 | /* Check if the requested node is already in the hash table. If so, |
| 116386 | 117206 | ** increase its reference count and return it. |
| 116387 | 117207 | */ |
| @@ -116394,43 +117214,67 @@ | ||
| 116394 | 117214 | pNode->nRef++; |
| 116395 | 117215 | *ppNode = pNode; |
| 116396 | 117216 | return SQLITE_OK; |
| 116397 | 117217 | } |
| 116398 | 117218 | |
| 116399 | - pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); | |
| 116400 | - if( !pNode ){ | |
| 116401 | - *ppNode = 0; | |
| 116402 | - return SQLITE_NOMEM; | |
| 116403 | - } | |
| 116404 | - pNode->pParent = pParent; | |
| 116405 | - pNode->zData = (u8 *)&pNode[1]; | |
| 116406 | - pNode->nRef = 1; | |
| 116407 | - pNode->iNode = iNode; | |
| 116408 | - pNode->isDirty = 0; | |
| 116409 | - pNode->pNext = 0; | |
| 116410 | - | |
| 116411 | 117219 | sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); |
| 116412 | 117220 | rc = sqlite3_step(pRtree->pReadNode); |
| 116413 | 117221 | if( rc==SQLITE_ROW ){ |
| 116414 | 117222 | const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); |
| 116415 | - assert( sqlite3_column_bytes(pRtree->pReadNode, 0)==pRtree->iNodeSize ); | |
| 116416 | - memcpy(pNode->zData, zBlob, pRtree->iNodeSize); | |
| 116417 | - nodeReference(pParent); | |
| 117223 | + if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){ | |
| 117224 | + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); | |
| 117225 | + if( !pNode ){ | |
| 117226 | + rc2 = SQLITE_NOMEM; | |
| 117227 | + }else{ | |
| 117228 | + pNode->pParent = pParent; | |
| 117229 | + pNode->zData = (u8 *)&pNode[1]; | |
| 117230 | + pNode->nRef = 1; | |
| 117231 | + pNode->iNode = iNode; | |
| 117232 | + pNode->isDirty = 0; | |
| 117233 | + pNode->pNext = 0; | |
| 117234 | + memcpy(pNode->zData, zBlob, pRtree->iNodeSize); | |
| 117235 | + nodeReference(pParent); | |
| 117236 | + } | |
| 117237 | + } | |
| 117238 | + } | |
| 117239 | + rc = sqlite3_reset(pRtree->pReadNode); | |
| 117240 | + if( rc==SQLITE_OK ) rc = rc2; | |
| 117241 | + | |
| 117242 | + /* If the root node was just loaded, set pRtree->iDepth to the height | |
| 117243 | + ** of the r-tree structure. A height of zero means all data is stored on | |
| 117244 | + ** the root node. A height of one means the children of the root node | |
| 117245 | + ** are the leaves, and so on. If the depth as specified on the root node | |
| 117246 | + ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. | |
| 117247 | + */ | |
| 117248 | + if( pNode && iNode==1 ){ | |
| 117249 | + pRtree->iDepth = readInt16(pNode->zData); | |
| 117250 | + if( pRtree->iDepth>RTREE_MAX_DEPTH ){ | |
| 117251 | + rc = SQLITE_CORRUPT; | |
| 117252 | + } | |
| 117253 | + } | |
| 117254 | + | |
| 117255 | + /* If no error has occurred so far, check if the "number of entries" | |
| 117256 | + ** field on the node is too large. If so, set the return code to | |
| 117257 | + ** SQLITE_CORRUPT. | |
| 117258 | + */ | |
| 117259 | + if( pNode && rc==SQLITE_OK ){ | |
| 117260 | + if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){ | |
| 117261 | + rc = SQLITE_CORRUPT; | |
| 117262 | + } | |
| 117263 | + } | |
| 117264 | + | |
| 117265 | + if( rc==SQLITE_OK ){ | |
| 117266 | + if( pNode!=0 ){ | |
| 117267 | + nodeHashInsert(pRtree, pNode); | |
| 117268 | + }else{ | |
| 117269 | + rc = SQLITE_CORRUPT; | |
| 117270 | + } | |
| 117271 | + *ppNode = pNode; | |
| 116418 | 117272 | }else{ |
| 116419 | 117273 | sqlite3_free(pNode); |
| 116420 | - pNode = 0; | |
| 116421 | - } | |
| 116422 | - | |
| 116423 | - *ppNode = pNode; | |
| 116424 | - rc = sqlite3_reset(pRtree->pReadNode); | |
| 116425 | - | |
| 116426 | - if( rc==SQLITE_OK && iNode==1 ){ | |
| 116427 | - pRtree->iDepth = readInt16(pNode->zData); | |
| 116428 | - } | |
| 116429 | - | |
| 116430 | - assert( (rc==SQLITE_OK && pNode) || (pNode==0 && rc!=SQLITE_OK) ); | |
| 116431 | - nodeHashInsert(pRtree, pNode); | |
| 117274 | + *ppNode = 0; | |
| 117275 | + } | |
| 116432 | 117276 | |
| 116433 | 117277 | return rc; |
| 116434 | 117278 | } |
| 116435 | 117279 | |
| 116436 | 117280 | /* |
| @@ -116479,12 +117323,11 @@ | ||
| 116479 | 117323 | int nMaxCell; /* Maximum number of cells for pNode */ |
| 116480 | 117324 | |
| 116481 | 117325 | nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; |
| 116482 | 117326 | nCell = NCELL(pNode); |
| 116483 | 117327 | |
| 116484 | - assert(nCell<=nMaxCell); | |
| 116485 | - | |
| 117328 | + assert( nCell<=nMaxCell ); | |
| 116486 | 117329 | if( nCell<nMaxCell ){ |
| 116487 | 117330 | nodeOverwriteCell(pRtree, pNode, pCell, nCell); |
| 116488 | 117331 | writeInt16(&pNode->zData[2], nCell+1); |
| 116489 | 117332 | pNode->isDirty = 1; |
| 116490 | 117333 | } |
| @@ -116700,18 +117543,37 @@ | ||
| 116700 | 117543 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 116701 | 117544 | |
| 116702 | 117545 | return rc; |
| 116703 | 117546 | } |
| 116704 | 117547 | |
| 117548 | + | |
| 117549 | +/* | |
| 117550 | +** Free the RtreeCursor.aConstraint[] array and its contents. | |
| 117551 | +*/ | |
| 117552 | +static void freeCursorConstraints(RtreeCursor *pCsr){ | |
| 117553 | + if( pCsr->aConstraint ){ | |
| 117554 | + int i; /* Used to iterate through constraint array */ | |
| 117555 | + for(i=0; i<pCsr->nConstraint; i++){ | |
| 117556 | + sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom; | |
| 117557 | + if( pGeom ){ | |
| 117558 | + if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser); | |
| 117559 | + sqlite3_free(pGeom); | |
| 117560 | + } | |
| 117561 | + } | |
| 117562 | + sqlite3_free(pCsr->aConstraint); | |
| 117563 | + pCsr->aConstraint = 0; | |
| 117564 | + } | |
| 117565 | +} | |
| 117566 | + | |
| 116705 | 117567 | /* |
| 116706 | 117568 | ** Rtree virtual table module xClose method. |
| 116707 | 117569 | */ |
| 116708 | 117570 | static int rtreeClose(sqlite3_vtab_cursor *cur){ |
| 116709 | 117571 | Rtree *pRtree = (Rtree *)(cur->pVtab); |
| 116710 | 117572 | int rc; |
| 116711 | 117573 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 116712 | - sqlite3_free(pCsr->aConstraint); | |
| 117574 | + freeCursorConstraints(pCsr); | |
| 116713 | 117575 | rc = nodeRelease(pRtree, pCsr->pNode); |
| 116714 | 117576 | sqlite3_free(pCsr); |
| 116715 | 117577 | return rc; |
| 116716 | 117578 | } |
| 116717 | 117579 | |
| @@ -116723,18 +117585,44 @@ | ||
| 116723 | 117585 | */ |
| 116724 | 117586 | static int rtreeEof(sqlite3_vtab_cursor *cur){ |
| 116725 | 117587 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 116726 | 117588 | return (pCsr->pNode==0); |
| 116727 | 117589 | } |
| 117590 | + | |
| 117591 | +/* | |
| 117592 | +** The r-tree constraint passed as the second argument to this function is | |
| 117593 | +** guaranteed to be a MATCH constraint. | |
| 117594 | +*/ | |
| 117595 | +static int testRtreeGeom( | |
| 117596 | + Rtree *pRtree, /* R-Tree object */ | |
| 117597 | + RtreeConstraint *pConstraint, /* MATCH constraint to test */ | |
| 117598 | + RtreeCell *pCell, /* Cell to test */ | |
| 117599 | + int *pbRes /* OUT: Test result */ | |
| 117600 | +){ | |
| 117601 | + int i; | |
| 117602 | + double aCoord[RTREE_MAX_DIMENSIONS*2]; | |
| 117603 | + int nCoord = pRtree->nDim*2; | |
| 117604 | + | |
| 117605 | + assert( pConstraint->op==RTREE_MATCH ); | |
| 117606 | + assert( pConstraint->pGeom ); | |
| 117607 | + | |
| 117608 | + for(i=0; i<nCoord; i++){ | |
| 117609 | + aCoord[i] = DCOORD(pCell->aCoord[i]); | |
| 117610 | + } | |
| 117611 | + return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes); | |
| 117612 | +} | |
| 116728 | 117613 | |
| 116729 | 117614 | /* |
| 116730 | 117615 | ** Cursor pCursor currently points to a cell in a non-leaf page. |
| 116731 | -** Return true if the sub-tree headed by the cell is filtered | |
| 117616 | +** Set *pbEof to true if the sub-tree headed by the cell is filtered | |
| 116732 | 117617 | ** (excluded) by the constraints in the pCursor->aConstraint[] |
| 116733 | 117618 | ** array, or false otherwise. |
| 117619 | +** | |
| 117620 | +** Return SQLITE_OK if successful or an SQLite error code if an error | |
| 117621 | +** occurs within a geometry callback. | |
| 116734 | 117622 | */ |
| 116735 | -static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor){ | |
| 117623 | +static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ | |
| 116736 | 117624 | RtreeCell cell; |
| 116737 | 117625 | int ii; |
| 116738 | 117626 | int bRes = 0; |
| 116739 | 117627 | |
| 116740 | 117628 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| @@ -116742,56 +117630,92 @@ | ||
| 116742 | 117630 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 116743 | 117631 | double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]); |
| 116744 | 117632 | double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]); |
| 116745 | 117633 | |
| 116746 | 117634 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 116747 | - || p->op==RTREE_GT || p->op==RTREE_EQ | |
| 117635 | + || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH | |
| 116748 | 117636 | ); |
| 116749 | 117637 | |
| 116750 | 117638 | switch( p->op ){ |
| 116751 | - case RTREE_LE: case RTREE_LT: bRes = p->rValue<cell_min; break; | |
| 116752 | - case RTREE_GE: case RTREE_GT: bRes = p->rValue>cell_max; break; | |
| 116753 | - case RTREE_EQ: | |
| 117639 | + case RTREE_LE: case RTREE_LT: | |
| 117640 | + bRes = p->rValue<cell_min; | |
| 117641 | + break; | |
| 117642 | + | |
| 117643 | + case RTREE_GE: case RTREE_GT: | |
| 117644 | + bRes = p->rValue>cell_max; | |
| 117645 | + break; | |
| 117646 | + | |
| 117647 | + case RTREE_EQ: | |
| 116754 | 117648 | bRes = (p->rValue>cell_max || p->rValue<cell_min); |
| 116755 | 117649 | break; |
| 117650 | + | |
| 117651 | + default: { | |
| 117652 | + int rc; | |
| 117653 | + assert( p->op==RTREE_MATCH ); | |
| 117654 | + rc = testRtreeGeom(pRtree, p, &cell, &bRes); | |
| 117655 | + if( rc!=SQLITE_OK ){ | |
| 117656 | + return rc; | |
| 117657 | + } | |
| 117658 | + bRes = !bRes; | |
| 117659 | + break; | |
| 117660 | + } | |
| 116756 | 117661 | } |
| 116757 | 117662 | } |
| 116758 | 117663 | |
| 116759 | - return bRes; | |
| 117664 | + *pbEof = bRes; | |
| 117665 | + return SQLITE_OK; | |
| 116760 | 117666 | } |
| 116761 | 117667 | |
| 116762 | 117668 | /* |
| 116763 | -** Return true if the cell that cursor pCursor currently points to | |
| 117669 | +** Test if the cell that cursor pCursor currently points to | |
| 116764 | 117670 | ** would be filtered (excluded) by the constraints in the |
| 116765 | -** pCursor->aConstraint[] array, or false otherwise. | |
| 117671 | +** pCursor->aConstraint[] array. If so, set *pbEof to true before | |
| 117672 | +** returning. If the cell is not filtered (excluded) by the constraints, | |
| 117673 | +** set pbEof to zero. | |
| 117674 | +** | |
| 117675 | +** Return SQLITE_OK if successful or an SQLite error code if an error | |
| 117676 | +** occurs within a geometry callback. | |
| 116766 | 117677 | ** |
| 116767 | 117678 | ** This function assumes that the cell is part of a leaf node. |
| 116768 | 117679 | */ |
| 116769 | -static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor){ | |
| 117680 | +static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ | |
| 116770 | 117681 | RtreeCell cell; |
| 116771 | 117682 | int ii; |
| 117683 | + *pbEof = 0; | |
| 116772 | 117684 | |
| 116773 | 117685 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| 116774 | 117686 | for(ii=0; ii<pCursor->nConstraint; ii++){ |
| 116775 | 117687 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 116776 | 117688 | double coord = DCOORD(cell.aCoord[p->iCoord]); |
| 116777 | 117689 | int res; |
| 116778 | 117690 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 116779 | - || p->op==RTREE_GT || p->op==RTREE_EQ | |
| 117691 | + || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH | |
| 116780 | 117692 | ); |
| 116781 | 117693 | switch( p->op ){ |
| 116782 | 117694 | case RTREE_LE: res = (coord<=p->rValue); break; |
| 116783 | 117695 | case RTREE_LT: res = (coord<p->rValue); break; |
| 116784 | 117696 | case RTREE_GE: res = (coord>=p->rValue); break; |
| 116785 | 117697 | case RTREE_GT: res = (coord>p->rValue); break; |
| 116786 | 117698 | case RTREE_EQ: res = (coord==p->rValue); break; |
| 117699 | + default: { | |
| 117700 | + int rc; | |
| 117701 | + assert( p->op==RTREE_MATCH ); | |
| 117702 | + rc = testRtreeGeom(pRtree, p, &cell, &res); | |
| 117703 | + if( rc!=SQLITE_OK ){ | |
| 117704 | + return rc; | |
| 117705 | + } | |
| 117706 | + break; | |
| 117707 | + } | |
| 116787 | 117708 | } |
| 116788 | 117709 | |
| 116789 | - if( !res ) return 1; | |
| 117710 | + if( !res ){ | |
| 117711 | + *pbEof = 1; | |
| 117712 | + return SQLITE_OK; | |
| 117713 | + } | |
| 116790 | 117714 | } |
| 116791 | 117715 | |
| 116792 | - return 0; | |
| 117716 | + return SQLITE_OK; | |
| 116793 | 117717 | } |
| 116794 | 117718 | |
| 116795 | 117719 | /* |
| 116796 | 117720 | ** Cursor pCursor currently points at a node that heads a sub-tree of |
| 116797 | 117721 | ** height iHeight (if iHeight==0, then the node is a leaf). Descend |
| @@ -116814,17 +117738,17 @@ | ||
| 116814 | 117738 | int iSavedCell = pCursor->iCell; |
| 116815 | 117739 | |
| 116816 | 117740 | assert( iHeight>=0 ); |
| 116817 | 117741 | |
| 116818 | 117742 | if( iHeight==0 ){ |
| 116819 | - isEof = testRtreeEntry(pRtree, pCursor); | |
| 117743 | + rc = testRtreeEntry(pRtree, pCursor, &isEof); | |
| 116820 | 117744 | }else{ |
| 116821 | - isEof = testRtreeCell(pRtree, pCursor); | |
| 117745 | + rc = testRtreeCell(pRtree, pCursor, &isEof); | |
| 116822 | 117746 | } |
| 116823 | - if( isEof || iHeight==0 ){ | |
| 117747 | + if( rc!=SQLITE_OK || isEof || iHeight==0 ){ | |
| 116824 | 117748 | *pEof = isEof; |
| 116825 | - return SQLITE_OK; | |
| 117749 | + return rc; | |
| 116826 | 117750 | } |
| 116827 | 117751 | |
| 116828 | 117752 | iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell); |
| 116829 | 117753 | rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild); |
| 116830 | 117754 | if( rc!=SQLITE_OK ){ |
| @@ -116856,45 +117780,59 @@ | ||
| 116856 | 117780 | |
| 116857 | 117781 | /* |
| 116858 | 117782 | ** One of the cells in node pNode is guaranteed to have a 64-bit |
| 116859 | 117783 | ** integer value equal to iRowid. Return the index of this cell. |
| 116860 | 117784 | */ |
| 116861 | -static int nodeRowidIndex(Rtree *pRtree, RtreeNode *pNode, i64 iRowid){ | |
| 117785 | +static int nodeRowidIndex( | |
| 117786 | + Rtree *pRtree, | |
| 117787 | + RtreeNode *pNode, | |
| 117788 | + i64 iRowid, | |
| 117789 | + int *piIndex | |
| 117790 | +){ | |
| 116862 | 117791 | int ii; |
| 116863 | - for(ii=0; nodeGetRowid(pRtree, pNode, ii)!=iRowid; ii++){ | |
| 116864 | - assert( ii<(NCELL(pNode)-1) ); | |
| 117792 | + int nCell = NCELL(pNode); | |
| 117793 | + for(ii=0; ii<nCell; ii++){ | |
| 117794 | + if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){ | |
| 117795 | + *piIndex = ii; | |
| 117796 | + return SQLITE_OK; | |
| 117797 | + } | |
| 116865 | 117798 | } |
| 116866 | - return ii; | |
| 117799 | + return SQLITE_CORRUPT; | |
| 116867 | 117800 | } |
| 116868 | 117801 | |
| 116869 | 117802 | /* |
| 116870 | 117803 | ** Return the index of the cell containing a pointer to node pNode |
| 116871 | 117804 | ** in its parent. If pNode is the root node, return -1. |
| 116872 | 117805 | */ |
| 116873 | -static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode){ | |
| 117806 | +static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){ | |
| 116874 | 117807 | RtreeNode *pParent = pNode->pParent; |
| 116875 | 117808 | if( pParent ){ |
| 116876 | - return nodeRowidIndex(pRtree, pParent, pNode->iNode); | |
| 117809 | + return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex); | |
| 116877 | 117810 | } |
| 116878 | - return -1; | |
| 117811 | + *piIndex = -1; | |
| 117812 | + return SQLITE_OK; | |
| 116879 | 117813 | } |
| 116880 | 117814 | |
| 116881 | 117815 | /* |
| 116882 | 117816 | ** Rtree virtual table module xNext method. |
| 116883 | 117817 | */ |
| 116884 | 117818 | static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ |
| 116885 | 117819 | Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab); |
| 116886 | 117820 | RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; |
| 116887 | 117821 | int rc = SQLITE_OK; |
| 117822 | + | |
| 117823 | + /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is | |
| 117824 | + ** already at EOF. It is against the rules to call the xNext() method of | |
| 117825 | + ** a cursor that has already reached EOF. | |
| 117826 | + */ | |
| 117827 | + assert( pCsr->pNode ); | |
| 116888 | 117828 | |
| 116889 | 117829 | if( pCsr->iStrategy==1 ){ |
| 116890 | 117830 | /* This "scan" is a direct lookup by rowid. There is no next entry. */ |
| 116891 | 117831 | nodeRelease(pRtree, pCsr->pNode); |
| 116892 | 117832 | pCsr->pNode = 0; |
| 116893 | - } | |
| 116894 | - | |
| 116895 | - else if( pCsr->pNode ){ | |
| 117833 | + }else{ | |
| 116896 | 117834 | /* Move to the next entry that matches the configured constraints. */ |
| 116897 | 117835 | int iHeight = 0; |
| 116898 | 117836 | while( pCsr->pNode ){ |
| 116899 | 117837 | RtreeNode *pNode = pCsr->pNode; |
| 116900 | 117838 | int nCell = NCELL(pNode); |
| @@ -116904,11 +117842,14 @@ | ||
| 116904 | 117842 | if( rc!=SQLITE_OK || !isEof ){ |
| 116905 | 117843 | return rc; |
| 116906 | 117844 | } |
| 116907 | 117845 | } |
| 116908 | 117846 | pCsr->pNode = pNode->pParent; |
| 116909 | - pCsr->iCell = nodeParentIndex(pRtree, pNode); | |
| 117847 | + rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell); | |
| 117848 | + if( rc!=SQLITE_OK ){ | |
| 117849 | + return rc; | |
| 117850 | + } | |
| 116910 | 117851 | nodeReference(pCsr->pNode); |
| 116911 | 117852 | nodeRelease(pRtree, pNode); |
| 116912 | 117853 | iHeight++; |
| 116913 | 117854 | } |
| 116914 | 117855 | } |
| @@ -116972,10 +117913,55 @@ | ||
| 116972 | 117913 | rc = sqlite3_reset(pRtree->pReadRowid); |
| 116973 | 117914 | } |
| 116974 | 117915 | return rc; |
| 116975 | 117916 | } |
| 116976 | 117917 | |
| 117918 | +/* | |
| 117919 | +** This function is called to configure the RtreeConstraint object passed | |
| 117920 | +** as the second argument for a MATCH constraint. The value passed as the | |
| 117921 | +** first argument to this function is the right-hand operand to the MATCH | |
| 117922 | +** operator. | |
| 117923 | +*/ | |
| 117924 | +static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){ | |
| 117925 | + RtreeMatchArg *p; | |
| 117926 | + sqlite3_rtree_geometry *pGeom; | |
| 117927 | + int nBlob; | |
| 117928 | + | |
| 117929 | + /* Check that value is actually a blob. */ | |
| 117930 | + if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR; | |
| 117931 | + | |
| 117932 | + /* Check that the blob is roughly the right size. */ | |
| 117933 | + nBlob = sqlite3_value_bytes(pValue); | |
| 117934 | + if( nBlob<sizeof(RtreeMatchArg) | |
| 117935 | + || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0 | |
| 117936 | + ){ | |
| 117937 | + return SQLITE_ERROR; | |
| 117938 | + } | |
| 117939 | + | |
| 117940 | + pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc( | |
| 117941 | + sizeof(sqlite3_rtree_geometry) + nBlob | |
| 117942 | + ); | |
| 117943 | + if( !pGeom ) return SQLITE_NOMEM; | |
| 117944 | + memset(pGeom, 0, sizeof(sqlite3_rtree_geometry)); | |
| 117945 | + p = (RtreeMatchArg *)&pGeom[1]; | |
| 117946 | + | |
| 117947 | + memcpy(p, sqlite3_value_blob(pValue), nBlob); | |
| 117948 | + if( p->magic!=RTREE_GEOMETRY_MAGIC | |
| 117949 | + || nBlob!=(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double)) | |
| 117950 | + ){ | |
| 117951 | + sqlite3_free(pGeom); | |
| 117952 | + return SQLITE_ERROR; | |
| 117953 | + } | |
| 117954 | + | |
| 117955 | + pGeom->pContext = p->pContext; | |
| 117956 | + pGeom->nParam = p->nParam; | |
| 117957 | + pGeom->aParam = p->aParam; | |
| 117958 | + | |
| 117959 | + pCons->xGeom = p->xGeom; | |
| 117960 | + pCons->pGeom = pGeom; | |
| 117961 | + return SQLITE_OK; | |
| 117962 | +} | |
| 116977 | 117963 | |
| 116978 | 117964 | /* |
| 116979 | 117965 | ** Rtree virtual table module xFilter method. |
| 116980 | 117966 | */ |
| 116981 | 117967 | static int rtreeFilter( |
| @@ -116990,22 +117976,22 @@ | ||
| 116990 | 117976 | int ii; |
| 116991 | 117977 | int rc = SQLITE_OK; |
| 116992 | 117978 | |
| 116993 | 117979 | rtreeReference(pRtree); |
| 116994 | 117980 | |
| 116995 | - sqlite3_free(pCsr->aConstraint); | |
| 116996 | - pCsr->aConstraint = 0; | |
| 117981 | + freeCursorConstraints(pCsr); | |
| 116997 | 117982 | pCsr->iStrategy = idxNum; |
| 116998 | 117983 | |
| 116999 | 117984 | if( idxNum==1 ){ |
| 117000 | 117985 | /* Special case - lookup by rowid. */ |
| 117001 | 117986 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| 117002 | 117987 | i64 iRowid = sqlite3_value_int64(argv[0]); |
| 117003 | 117988 | rc = findLeafNode(pRtree, iRowid, &pLeaf); |
| 117004 | 117989 | pCsr->pNode = pLeaf; |
| 117005 | - if( pLeaf && rc==SQLITE_OK ){ | |
| 117006 | - pCsr->iCell = nodeRowidIndex(pRtree, pLeaf, iRowid); | |
| 117990 | + if( pLeaf ){ | |
| 117991 | + assert( rc==SQLITE_OK ); | |
| 117992 | + rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell); | |
| 117007 | 117993 | } |
| 117008 | 117994 | }else{ |
| 117009 | 117995 | /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array |
| 117010 | 117996 | ** with the configured constraints. |
| 117011 | 117997 | */ |
| @@ -117013,16 +117999,28 @@ | ||
| 117013 | 117999 | pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); |
| 117014 | 118000 | pCsr->nConstraint = argc; |
| 117015 | 118001 | if( !pCsr->aConstraint ){ |
| 117016 | 118002 | rc = SQLITE_NOMEM; |
| 117017 | 118003 | }else{ |
| 118004 | + memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); | |
| 117018 | 118005 | assert( (idxStr==0 && argc==0) || strlen(idxStr)==argc*2 ); |
| 117019 | 118006 | for(ii=0; ii<argc; ii++){ |
| 117020 | 118007 | RtreeConstraint *p = &pCsr->aConstraint[ii]; |
| 117021 | 118008 | p->op = idxStr[ii*2]; |
| 117022 | 118009 | p->iCoord = idxStr[ii*2+1]-'a'; |
| 117023 | - p->rValue = sqlite3_value_double(argv[ii]); | |
| 118010 | + if( p->op==RTREE_MATCH ){ | |
| 118011 | + /* A MATCH operator. The right-hand-side must be a blob that | |
| 118012 | + ** can be cast into an RtreeMatchArg object. One created using | |
| 118013 | + ** an sqlite3_rtree_geometry_callback() SQL user function. | |
| 118014 | + */ | |
| 118015 | + rc = deserializeGeometry(argv[ii], p); | |
| 118016 | + if( rc!=SQLITE_OK ){ | |
| 118017 | + break; | |
| 118018 | + } | |
| 118019 | + }else{ | |
| 118020 | + p->rValue = sqlite3_value_double(argv[ii]); | |
| 118021 | + } | |
| 117024 | 118022 | } |
| 117025 | 118023 | } |
| 117026 | 118024 | } |
| 117027 | 118025 | |
| 117028 | 118026 | if( rc==SQLITE_OK ){ |
| @@ -117078,10 +118076,11 @@ | ||
| 117078 | 118076 | ** = 0x41 ('A') |
| 117079 | 118077 | ** <= 0x42 ('B') |
| 117080 | 118078 | ** < 0x43 ('C') |
| 117081 | 118079 | ** >= 0x44 ('D') |
| 117082 | 118080 | ** > 0x45 ('E') |
| 118081 | +** MATCH 0x46 ('F') | |
| 117083 | 118082 | ** ---------------------- |
| 117084 | 118083 | ** |
| 117085 | 118084 | ** The second of each pair of bytes identifies the coordinate column |
| 117086 | 118085 | ** to which the constraint applies. The leftmost coordinate column |
| 117087 | 118086 | ** is 'a', the second from the left 'b' etc. |
| @@ -117116,43 +118115,47 @@ | ||
| 117116 | 118115 | */ |
| 117117 | 118116 | pIdxInfo->estimatedCost = 10.0; |
| 117118 | 118117 | return SQLITE_OK; |
| 117119 | 118118 | } |
| 117120 | 118119 | |
| 117121 | - if( p->usable && p->iColumn>0 ){ | |
| 118120 | + if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ | |
| 118121 | + int j, opmsk; | |
| 118122 | + static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 }; | |
| 117122 | 118123 | u8 op = 0; |
| 117123 | 118124 | switch( p->op ){ |
| 117124 | 118125 | case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; |
| 117125 | 118126 | case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; |
| 117126 | 118127 | case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; |
| 117127 | 118128 | case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; |
| 117128 | 118129 | case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; |
| 117129 | - } | |
| 117130 | - if( op ){ | |
| 117131 | - /* Make sure this particular constraint has not been used before. | |
| 117132 | - ** If it has been used before, ignore it. | |
| 117133 | - ** | |
| 117134 | - ** A <= or < can be used if there is a prior >= or >. | |
| 117135 | - ** A >= or > can be used if there is a prior < or <=. | |
| 117136 | - ** A <= or < is disqualified if there is a prior <=, <, or ==. | |
| 117137 | - ** A >= or > is disqualified if there is a prior >=, >, or ==. | |
| 117138 | - ** A == is disqualifed if there is any prior constraint. | |
| 117139 | - */ | |
| 117140 | - int j, opmsk; | |
| 117141 | - static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 }; | |
| 117142 | - assert( compatible[RTREE_EQ & 7]==0 ); | |
| 117143 | - assert( compatible[RTREE_LT & 7]==1 ); | |
| 117144 | - assert( compatible[RTREE_LE & 7]==1 ); | |
| 117145 | - assert( compatible[RTREE_GT & 7]==2 ); | |
| 117146 | - assert( compatible[RTREE_GE & 7]==2 ); | |
| 117147 | - cCol = p->iColumn - 1 + 'a'; | |
| 117148 | - opmsk = compatible[op & 7]; | |
| 117149 | - for(j=0; j<iIdx; j+=2){ | |
| 117150 | - if( zIdxStr[j+1]==cCol && (compatible[zIdxStr[j] & 7] & opmsk)!=0 ){ | |
| 117151 | - op = 0; | |
| 117152 | - break; | |
| 117153 | - } | |
| 118130 | + default: | |
| 118131 | + assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); | |
| 118132 | + op = RTREE_MATCH; | |
| 118133 | + break; | |
| 118134 | + } | |
| 118135 | + assert( op!=0 ); | |
| 118136 | + | |
| 118137 | + /* Make sure this particular constraint has not been used before. | |
| 118138 | + ** If it has been used before, ignore it. | |
| 118139 | + ** | |
| 118140 | + ** A <= or < can be used if there is a prior >= or >. | |
| 118141 | + ** A >= or > can be used if there is a prior < or <=. | |
| 118142 | + ** A <= or < is disqualified if there is a prior <=, <, or ==. | |
| 118143 | + ** A >= or > is disqualified if there is a prior >=, >, or ==. | |
| 118144 | + ** A == is disqualifed if there is any prior constraint. | |
| 118145 | + */ | |
| 118146 | + assert( compatible[RTREE_EQ & 7]==0 ); | |
| 118147 | + assert( compatible[RTREE_LT & 7]==1 ); | |
| 118148 | + assert( compatible[RTREE_LE & 7]==1 ); | |
| 118149 | + assert( compatible[RTREE_GT & 7]==2 ); | |
| 118150 | + assert( compatible[RTREE_GE & 7]==2 ); | |
| 118151 | + cCol = p->iColumn - 1 + 'a'; | |
| 118152 | + opmsk = compatible[op & 7]; | |
| 118153 | + for(j=0; j<iIdx; j+=2){ | |
| 118154 | + if( zIdxStr[j+1]==cCol && (compatible[zIdxStr[j] & 7] & opmsk)!=0 ){ | |
| 118155 | + op = 0; | |
| 118156 | + break; | |
| 117154 | 118157 | } |
| 117155 | 118158 | } |
| 117156 | 118159 | if( op ){ |
| 117157 | 118160 | assert( iIdx<sizeof(zIdxStr)-1 ); |
| 117158 | 118161 | zIdxStr[iIdx++] = op; |
| @@ -117256,11 +118259,16 @@ | ||
| 117256 | 118259 | int iExclude |
| 117257 | 118260 | ){ |
| 117258 | 118261 | int ii; |
| 117259 | 118262 | float overlap = 0.0; |
| 117260 | 118263 | for(ii=0; ii<nCell; ii++){ |
| 117261 | - if( ii!=iExclude ){ | |
| 118264 | +#if VARIANT_RSTARTREE_CHOOSESUBTREE | |
| 118265 | + if( ii!=iExclude ) | |
| 118266 | +#else | |
| 118267 | + assert( iExclude==-1 ); | |
| 118268 | +#endif | |
| 118269 | + { | |
| 117262 | 118270 | int jj; |
| 117263 | 118271 | float o = 1.0; |
| 117264 | 118272 | for(jj=0; jj<(pRtree->nDim*2); jj+=2){ |
| 117265 | 118273 | double x1; |
| 117266 | 118274 | double x2; |
| @@ -117349,26 +118357,35 @@ | ||
| 117349 | 118357 | /* Select the child node which will be enlarged the least if pCell |
| 117350 | 118358 | ** is inserted into it. Resolve ties by choosing the entry with |
| 117351 | 118359 | ** the smallest area. |
| 117352 | 118360 | */ |
| 117353 | 118361 | for(iCell=0; iCell<nCell; iCell++){ |
| 118362 | + int bBest = 0; | |
| 117354 | 118363 | float growth; |
| 117355 | 118364 | float area; |
| 117356 | 118365 | float overlap = 0.0; |
| 117357 | 118366 | nodeGetCell(pRtree, pNode, iCell, &cell); |
| 117358 | 118367 | growth = cellGrowth(pRtree, &cell, pCell); |
| 117359 | 118368 | area = cellArea(pRtree, &cell); |
| 118369 | + | |
| 117360 | 118370 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 117361 | 118371 | if( ii==(pRtree->iDepth-1) ){ |
| 117362 | 118372 | overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); |
| 117363 | 118373 | } |
| 117364 | -#endif | |
| 117365 | 118374 | if( (iCell==0) |
| 117366 | 118375 | || (overlap<fMinOverlap) |
| 117367 | 118376 | || (overlap==fMinOverlap && growth<fMinGrowth) |
| 117368 | 118377 | || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea) |
| 117369 | 118378 | ){ |
| 118379 | + bBest = 1; | |
| 118380 | + } | |
| 118381 | +#else | |
| 118382 | + if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){ | |
| 118383 | + bBest = 1; | |
| 118384 | + } | |
| 118385 | +#endif | |
| 118386 | + if( bBest ){ | |
| 117370 | 118387 | fMinOverlap = overlap; |
| 117371 | 118388 | fMinGrowth = growth; |
| 117372 | 118389 | fMinArea = area; |
| 117373 | 118390 | iBest = cell.iRowid; |
| 117374 | 118391 | } |
| @@ -117387,29 +118404,34 @@ | ||
| 117387 | 118404 | /* |
| 117388 | 118405 | ** A cell with the same content as pCell has just been inserted into |
| 117389 | 118406 | ** the node pNode. This function updates the bounding box cells in |
| 117390 | 118407 | ** all ancestor elements. |
| 117391 | 118408 | */ |
| 117392 | -static void AdjustTree( | |
| 118409 | +static int AdjustTree( | |
| 117393 | 118410 | Rtree *pRtree, /* Rtree table */ |
| 117394 | 118411 | RtreeNode *pNode, /* Adjust ancestry of this node. */ |
| 117395 | 118412 | RtreeCell *pCell /* This cell was just inserted */ |
| 117396 | 118413 | ){ |
| 117397 | 118414 | RtreeNode *p = pNode; |
| 117398 | 118415 | while( p->pParent ){ |
| 117399 | - RtreeCell cell; | |
| 117400 | 118416 | RtreeNode *pParent = p->pParent; |
| 117401 | - int iCell = nodeParentIndex(pRtree, p); | |
| 118417 | + RtreeCell cell; | |
| 118418 | + int iCell; | |
| 118419 | + | |
| 118420 | + if( nodeParentIndex(pRtree, p, &iCell) ){ | |
| 118421 | + return SQLITE_CORRUPT; | |
| 118422 | + } | |
| 117402 | 118423 | |
| 117403 | 118424 | nodeGetCell(pRtree, pParent, iCell, &cell); |
| 117404 | 118425 | if( !cellContains(pRtree, &cell, pCell) ){ |
| 117405 | 118426 | cellUnion(pRtree, &cell, pCell); |
| 117406 | 118427 | nodeOverwriteCell(pRtree, pParent, &cell, iCell); |
| 117407 | 118428 | } |
| 117408 | 118429 | |
| 117409 | 118430 | p = pParent; |
| 117410 | 118431 | } |
| 118432 | + return SQLITE_OK; | |
| 117411 | 118433 | } |
| 117412 | 118434 | |
| 117413 | 118435 | /* |
| 117414 | 118436 | ** Write mapping (iRowid->iNode) to the <rtree>_rowid table. |
| 117415 | 118437 | */ |
| @@ -117934,18 +118956,18 @@ | ||
| 117934 | 118956 | nodeZero(pRtree, pNode); |
| 117935 | 118957 | memcpy(&aCell[nCell], pCell, sizeof(RtreeCell)); |
| 117936 | 118958 | nCell++; |
| 117937 | 118959 | |
| 117938 | 118960 | if( pNode->iNode==1 ){ |
| 117939 | - pRight = nodeNew(pRtree, pNode, 1); | |
| 117940 | - pLeft = nodeNew(pRtree, pNode, 1); | |
| 118961 | + pRight = nodeNew(pRtree, pNode); | |
| 118962 | + pLeft = nodeNew(pRtree, pNode); | |
| 117941 | 118963 | pRtree->iDepth++; |
| 117942 | 118964 | pNode->isDirty = 1; |
| 117943 | 118965 | writeInt16(pNode->zData, pRtree->iDepth); |
| 117944 | 118966 | }else{ |
| 117945 | 118967 | pLeft = pNode; |
| 117946 | - pRight = nodeNew(pRtree, pLeft->pParent, 1); | |
| 118968 | + pRight = nodeNew(pRtree, pLeft->pParent); | |
| 117947 | 118969 | nodeReference(pLeft); |
| 117948 | 118970 | } |
| 117949 | 118971 | |
| 117950 | 118972 | if( !pLeft || !pRight ){ |
| 117951 | 118973 | rc = SQLITE_NOMEM; |
| @@ -117958,12 +118980,16 @@ | ||
| 117958 | 118980 | rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox); |
| 117959 | 118981 | if( rc!=SQLITE_OK ){ |
| 117960 | 118982 | goto splitnode_out; |
| 117961 | 118983 | } |
| 117962 | 118984 | |
| 117963 | - /* Ensure both child nodes have node numbers assigned to them. */ | |
| 117964 | - if( (0==pRight->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))) | |
| 118985 | + /* Ensure both child nodes have node numbers assigned to them by calling | |
| 118986 | + ** nodeWrite(). Node pRight always needs a node number, as it was created | |
| 118987 | + ** by nodeNew() above. But node pLeft sometimes already has a node number. | |
| 118988 | + ** In this case avoid the all to nodeWrite(). | |
| 118989 | + */ | |
| 118990 | + if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)) | |
| 117965 | 118991 | || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) |
| 117966 | 118992 | ){ |
| 117967 | 118993 | goto splitnode_out; |
| 117968 | 118994 | } |
| 117969 | 118995 | |
| @@ -117975,13 +119001,19 @@ | ||
| 117975 | 119001 | if( rc!=SQLITE_OK ){ |
| 117976 | 119002 | goto splitnode_out; |
| 117977 | 119003 | } |
| 117978 | 119004 | }else{ |
| 117979 | 119005 | RtreeNode *pParent = pLeft->pParent; |
| 117980 | - int iCell = nodeParentIndex(pRtree, pLeft); | |
| 117981 | - nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); | |
| 117982 | - AdjustTree(pRtree, pParent, &leftbbox); | |
| 119006 | + int iCell; | |
| 119007 | + rc = nodeParentIndex(pRtree, pLeft, &iCell); | |
| 119008 | + if( rc==SQLITE_OK ){ | |
| 119009 | + nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); | |
| 119010 | + rc = AdjustTree(pRtree, pParent, &leftbbox); | |
| 119011 | + } | |
| 119012 | + if( rc!=SQLITE_OK ){ | |
| 119013 | + goto splitnode_out; | |
| 119014 | + } | |
| 117983 | 119015 | } |
| 117984 | 119016 | if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ |
| 117985 | 119017 | goto splitnode_out; |
| 117986 | 119018 | } |
| 117987 | 119019 | |
| @@ -118021,44 +119053,73 @@ | ||
| 118021 | 119053 | nodeRelease(pRtree, pLeft); |
| 118022 | 119054 | sqlite3_free(aCell); |
| 118023 | 119055 | return rc; |
| 118024 | 119056 | } |
| 118025 | 119057 | |
| 119058 | +/* | |
| 119059 | +** If node pLeaf is not the root of the r-tree and its pParent pointer is | |
| 119060 | +** still NULL, load all ancestor nodes of pLeaf into memory and populate | |
| 119061 | +** the pLeaf->pParent chain all the way up to the root node. | |
| 119062 | +** | |
| 119063 | +** This operation is required when a row is deleted (or updated - an update | |
| 119064 | +** is implemented as a delete followed by an insert). SQLite provides the | |
| 119065 | +** rowid of the row to delete, which can be used to find the leaf on which | |
| 119066 | +** the entry resides (argument pLeaf). Once the leaf is located, this | |
| 119067 | +** function is called to determine its ancestry. | |
| 119068 | +*/ | |
| 118026 | 119069 | static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ |
| 118027 | 119070 | int rc = SQLITE_OK; |
| 118028 | - if( pLeaf->iNode!=1 && pLeaf->pParent==0 ){ | |
| 118029 | - sqlite3_bind_int64(pRtree->pReadParent, 1, pLeaf->iNode); | |
| 118030 | - if( sqlite3_step(pRtree->pReadParent)==SQLITE_ROW ){ | |
| 118031 | - i64 iNode = sqlite3_column_int64(pRtree->pReadParent, 0); | |
| 118032 | - rc = nodeAcquire(pRtree, iNode, 0, &pLeaf->pParent); | |
| 118033 | - }else{ | |
| 118034 | - rc = SQLITE_ERROR; | |
| 118035 | - } | |
| 118036 | - sqlite3_reset(pRtree->pReadParent); | |
| 118037 | - if( rc==SQLITE_OK ){ | |
| 118038 | - rc = fixLeafParent(pRtree, pLeaf->pParent); | |
| 118039 | - } | |
| 119071 | + RtreeNode *pChild = pLeaf; | |
| 119072 | + while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){ | |
| 119073 | + int rc2 = SQLITE_OK; /* sqlite3_reset() return code */ | |
| 119074 | + sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode); | |
| 119075 | + rc = sqlite3_step(pRtree->pReadParent); | |
| 119076 | + if( rc==SQLITE_ROW ){ | |
| 119077 | + RtreeNode *pTest; /* Used to test for reference loops */ | |
| 119078 | + i64 iNode; /* Node number of parent node */ | |
| 119079 | + | |
| 119080 | + /* Before setting pChild->pParent, test that we are not creating a | |
| 119081 | + ** loop of references (as we would if, say, pChild==pParent). We don't | |
| 119082 | + ** want to do this as it leads to a memory leak when trying to delete | |
| 119083 | + ** the referenced counted node structures. | |
| 119084 | + */ | |
| 119085 | + iNode = sqlite3_column_int64(pRtree->pReadParent, 0); | |
| 119086 | + for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent); | |
| 119087 | + if( !pTest ){ | |
| 119088 | + rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent); | |
| 119089 | + } | |
| 119090 | + } | |
| 119091 | + rc = sqlite3_reset(pRtree->pReadParent); | |
| 119092 | + if( rc==SQLITE_OK ) rc = rc2; | |
| 119093 | + if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT; | |
| 119094 | + pChild = pChild->pParent; | |
| 118040 | 119095 | } |
| 118041 | 119096 | return rc; |
| 118042 | 119097 | } |
| 118043 | 119098 | |
| 118044 | 119099 | static int deleteCell(Rtree *, RtreeNode *, int, int); |
| 118045 | 119100 | |
| 118046 | 119101 | static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ |
| 118047 | 119102 | int rc; |
| 119103 | + int rc2; | |
| 118048 | 119104 | RtreeNode *pParent; |
| 118049 | 119105 | int iCell; |
| 118050 | 119106 | |
| 118051 | 119107 | assert( pNode->nRef==1 ); |
| 118052 | 119108 | |
| 118053 | 119109 | /* Remove the entry in the parent cell. */ |
| 118054 | - iCell = nodeParentIndex(pRtree, pNode); | |
| 118055 | - pParent = pNode->pParent; | |
| 118056 | - pNode->pParent = 0; | |
| 118057 | - if( SQLITE_OK!=(rc = deleteCell(pRtree, pParent, iCell, iHeight+1)) | |
| 118058 | - || SQLITE_OK!=(rc = nodeRelease(pRtree, pParent)) | |
| 118059 | - ){ | |
| 119110 | + rc = nodeParentIndex(pRtree, pNode, &iCell); | |
| 119111 | + if( rc==SQLITE_OK ){ | |
| 119112 | + pParent = pNode->pParent; | |
| 119113 | + pNode->pParent = 0; | |
| 119114 | + rc = deleteCell(pRtree, pParent, iCell, iHeight+1); | |
| 119115 | + } | |
| 119116 | + rc2 = nodeRelease(pRtree, pParent); | |
| 119117 | + if( rc==SQLITE_OK ){ | |
| 119118 | + rc = rc2; | |
| 119119 | + } | |
| 119120 | + if( rc!=SQLITE_OK ){ | |
| 118060 | 119121 | return rc; |
| 118061 | 119122 | } |
| 118062 | 119123 | |
| 118063 | 119124 | /* Remove the xxx_node entry. */ |
| 118064 | 119125 | sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); |
| @@ -118084,12 +119145,13 @@ | ||
| 118084 | 119145 | pRtree->pDeleted = pNode; |
| 118085 | 119146 | |
| 118086 | 119147 | return SQLITE_OK; |
| 118087 | 119148 | } |
| 118088 | 119149 | |
| 118089 | -static void fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ | |
| 119150 | +static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ | |
| 118090 | 119151 | RtreeNode *pParent = pNode->pParent; |
| 119152 | + int rc = SQLITE_OK; | |
| 118091 | 119153 | if( pParent ){ |
| 118092 | 119154 | int ii; |
| 118093 | 119155 | int nCell = NCELL(pNode); |
| 118094 | 119156 | RtreeCell box; /* Bounding box for pNode */ |
| 118095 | 119157 | nodeGetCell(pRtree, pNode, 0, &box); |
| @@ -118097,21 +119159,25 @@ | ||
| 118097 | 119159 | RtreeCell cell; |
| 118098 | 119160 | nodeGetCell(pRtree, pNode, ii, &cell); |
| 118099 | 119161 | cellUnion(pRtree, &box, &cell); |
| 118100 | 119162 | } |
| 118101 | 119163 | box.iRowid = pNode->iNode; |
| 118102 | - ii = nodeParentIndex(pRtree, pNode); | |
| 118103 | - nodeOverwriteCell(pRtree, pParent, &box, ii); | |
| 118104 | - fixBoundingBox(pRtree, pParent); | |
| 119164 | + rc = nodeParentIndex(pRtree, pNode, &ii); | |
| 119165 | + if( rc==SQLITE_OK ){ | |
| 119166 | + nodeOverwriteCell(pRtree, pParent, &box, ii); | |
| 119167 | + rc = fixBoundingBox(pRtree, pParent); | |
| 119168 | + } | |
| 118105 | 119169 | } |
| 119170 | + return rc; | |
| 118106 | 119171 | } |
| 118107 | 119172 | |
| 118108 | 119173 | /* |
| 118109 | 119174 | ** Delete the cell at index iCell of node pNode. After removing the |
| 118110 | 119175 | ** cell, adjust the r-tree data structure if required. |
| 118111 | 119176 | */ |
| 118112 | 119177 | static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ |
| 119178 | + RtreeNode *pParent; | |
| 118113 | 119179 | int rc; |
| 118114 | 119180 | |
| 118115 | 119181 | if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ |
| 118116 | 119182 | return rc; |
| 118117 | 119183 | } |
| @@ -118124,18 +119190,17 @@ | ||
| 118124 | 119190 | /* If the node is not the tree root and now has less than the minimum |
| 118125 | 119191 | ** number of cells, remove it from the tree. Otherwise, update the |
| 118126 | 119192 | ** cell in the parent node so that it tightly contains the updated |
| 118127 | 119193 | ** node. |
| 118128 | 119194 | */ |
| 118129 | - if( pNode->iNode!=1 ){ | |
| 118130 | - RtreeNode *pParent = pNode->pParent; | |
| 118131 | - if( (pParent->iNode!=1 || NCELL(pParent)!=1) | |
| 118132 | - && (NCELL(pNode)<RTREE_MINCELLS(pRtree)) | |
| 118133 | - ){ | |
| 119195 | + pParent = pNode->pParent; | |
| 119196 | + assert( pParent || pNode->iNode==1 ); | |
| 119197 | + if( pParent ){ | |
| 119198 | + if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){ | |
| 118134 | 119199 | rc = removeNode(pRtree, pNode, iHeight); |
| 118135 | 119200 | }else{ |
| 118136 | - fixBoundingBox(pRtree, pNode); | |
| 119201 | + rc = fixBoundingBox(pRtree, pNode); | |
| 118137 | 119202 | } |
| 118138 | 119203 | } |
| 118139 | 119204 | |
| 118140 | 119205 | return rc; |
| 118141 | 119206 | } |
| @@ -118214,11 +119279,11 @@ | ||
| 118214 | 119279 | rc = parentWrite(pRtree, p->iRowid, pNode->iNode); |
| 118215 | 119280 | } |
| 118216 | 119281 | } |
| 118217 | 119282 | } |
| 118218 | 119283 | if( rc==SQLITE_OK ){ |
| 118219 | - fixBoundingBox(pRtree, pNode); | |
| 119284 | + rc = fixBoundingBox(pRtree, pNode); | |
| 118220 | 119285 | } |
| 118221 | 119286 | for(; rc==SQLITE_OK && ii<nCell; ii++){ |
| 118222 | 119287 | /* Find a node to store this cell in. pNode->iNode currently contains |
| 118223 | 119288 | ** the height of the sub-tree headed by the cell. |
| 118224 | 119289 | */ |
| @@ -118268,15 +119333,17 @@ | ||
| 118268 | 119333 | } |
| 118269 | 119334 | #else |
| 118270 | 119335 | rc = SplitNode(pRtree, pNode, pCell, iHeight); |
| 118271 | 119336 | #endif |
| 118272 | 119337 | }else{ |
| 118273 | - AdjustTree(pRtree, pNode, pCell); | |
| 118274 | - if( iHeight==0 ){ | |
| 118275 | - rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); | |
| 118276 | - }else{ | |
| 118277 | - rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); | |
| 119338 | + rc = AdjustTree(pRtree, pNode, pCell); | |
| 119339 | + if( rc==SQLITE_OK ){ | |
| 119340 | + if( iHeight==0 ){ | |
| 119341 | + rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); | |
| 119342 | + }else{ | |
| 119343 | + rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); | |
| 119344 | + } | |
| 118278 | 119345 | } |
| 118279 | 119346 | } |
| 118280 | 119347 | return rc; |
| 118281 | 119348 | } |
| 118282 | 119349 | |
| @@ -118342,11 +119409,10 @@ | ||
| 118342 | 119409 | int rc = SQLITE_OK; |
| 118343 | 119410 | |
| 118344 | 119411 | rtreeReference(pRtree); |
| 118345 | 119412 | |
| 118346 | 119413 | assert(nData>=1); |
| 118347 | - assert(hashIsEmpty(pRtree)); | |
| 118348 | 119414 | |
| 118349 | 119415 | /* If azData[0] is not an SQL NULL value, it is the rowid of a |
| 118350 | 119416 | ** record to delete from the r-tree table. The following block does |
| 118351 | 119417 | ** just that. |
| 118352 | 119418 | */ |
| @@ -118368,12 +119434,14 @@ | ||
| 118368 | 119434 | } |
| 118369 | 119435 | |
| 118370 | 119436 | /* Delete the cell in question from the leaf node. */ |
| 118371 | 119437 | if( rc==SQLITE_OK ){ |
| 118372 | 119438 | int rc2; |
| 118373 | - iCell = nodeRowidIndex(pRtree, pLeaf, iDelete); | |
| 118374 | - rc = deleteCell(pRtree, pLeaf, iCell, 0); | |
| 119439 | + rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell); | |
| 119440 | + if( rc==SQLITE_OK ){ | |
| 119441 | + rc = deleteCell(pRtree, pLeaf, iCell, 0); | |
| 119442 | + } | |
| 118375 | 119443 | rc2 = nodeRelease(pRtree, pLeaf); |
| 118376 | 119444 | if( rc==SQLITE_OK ){ |
| 118377 | 119445 | rc = rc2; |
| 118378 | 119446 | } |
| 118379 | 119447 | } |
| @@ -118391,23 +119459,24 @@ | ||
| 118391 | 119459 | ** |
| 118392 | 119460 | ** This is equivalent to copying the contents of the child into |
| 118393 | 119461 | ** the root node (the operation that Gutman's paper says to perform |
| 118394 | 119462 | ** in this scenario). |
| 118395 | 119463 | */ |
| 118396 | - if( rc==SQLITE_OK && pRtree->iDepth>0 ){ | |
| 118397 | - if( rc==SQLITE_OK && NCELL(pRoot)==1 ){ | |
| 118398 | - RtreeNode *pChild; | |
| 118399 | - i64 iChild = nodeGetRowid(pRtree, pRoot, 0); | |
| 118400 | - rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); | |
| 118401 | - if( rc==SQLITE_OK ){ | |
| 118402 | - rc = removeNode(pRtree, pChild, pRtree->iDepth-1); | |
| 118403 | - } | |
| 118404 | - if( rc==SQLITE_OK ){ | |
| 118405 | - pRtree->iDepth--; | |
| 118406 | - writeInt16(pRoot->zData, pRtree->iDepth); | |
| 118407 | - pRoot->isDirty = 1; | |
| 118408 | - } | |
| 119464 | + if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){ | |
| 119465 | + int rc2; | |
| 119466 | + RtreeNode *pChild; | |
| 119467 | + i64 iChild = nodeGetRowid(pRtree, pRoot, 0); | |
| 119468 | + rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); | |
| 119469 | + if( rc==SQLITE_OK ){ | |
| 119470 | + rc = removeNode(pRtree, pChild, pRtree->iDepth-1); | |
| 119471 | + } | |
| 119472 | + rc2 = nodeRelease(pRtree, pChild); | |
| 119473 | + if( rc==SQLITE_OK ) rc = rc2; | |
| 119474 | + if( rc==SQLITE_OK ){ | |
| 119475 | + pRtree->iDepth--; | |
| 119476 | + writeInt16(pRoot->zData, pRtree->iDepth); | |
| 119477 | + pRoot->isDirty = 1; | |
| 118409 | 119478 | } |
| 118410 | 119479 | } |
| 118411 | 119480 | |
| 118412 | 119481 | /* Re-insert the contents of any underfull nodes removed from the tree. */ |
| 118413 | 119482 | for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ |
| @@ -118693,11 +119762,11 @@ | ||
| 118693 | 119762 | ){ |
| 118694 | 119763 | int rc = SQLITE_OK; |
| 118695 | 119764 | Rtree *pRtree; |
| 118696 | 119765 | int nDb; /* Length of string argv[1] */ |
| 118697 | 119766 | int nName; /* Length of string argv[2] */ |
| 118698 | - int eCoordType = (int)pAux; | |
| 119767 | + int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); | |
| 118699 | 119768 | |
| 118700 | 119769 | const char *aErrMsg[] = { |
| 118701 | 119770 | 0, /* 0 */ |
| 118702 | 119771 | "Wrong number of columns for an rtree table", /* 1 */ |
| 118703 | 119772 | "Too few columns for an rtree table", /* 2 */ |
| @@ -118839,16 +119908,14 @@ | ||
| 118839 | 119908 | ** Register the r-tree module with database handle db. This creates the |
| 118840 | 119909 | ** virtual table module "rtree" and the debugging/analysis scalar |
| 118841 | 119910 | ** function "rtreenode". |
| 118842 | 119911 | */ |
| 118843 | 119912 | SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){ |
| 118844 | - int rc = SQLITE_OK; | |
| 119913 | + const int utf8 = SQLITE_UTF8; | |
| 119914 | + int rc; | |
| 118845 | 119915 | |
| 118846 | - if( rc==SQLITE_OK ){ | |
| 118847 | - int utf8 = SQLITE_UTF8; | |
| 118848 | - rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); | |
| 118849 | - } | |
| 119916 | + rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); | |
| 118850 | 119917 | if( rc==SQLITE_OK ){ |
| 118851 | 119918 | int utf8 = SQLITE_UTF8; |
| 118852 | 119919 | rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0); |
| 118853 | 119920 | } |
| 118854 | 119921 | if( rc==SQLITE_OK ){ |
| @@ -118860,10 +119927,74 @@ | ||
| 118860 | 119927 | rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0); |
| 118861 | 119928 | } |
| 118862 | 119929 | |
| 118863 | 119930 | return rc; |
| 118864 | 119931 | } |
| 119932 | + | |
| 119933 | +/* | |
| 119934 | +** A version of sqlite3_free() that can be used as a callback. This is used | |
| 119935 | +** in two places - as the destructor for the blob value returned by the | |
| 119936 | +** invocation of a geometry function, and as the destructor for the geometry | |
| 119937 | +** functions themselves. | |
| 119938 | +*/ | |
| 119939 | +static void doSqlite3Free(void *p){ | |
| 119940 | + sqlite3_free(p); | |
| 119941 | +} | |
| 119942 | + | |
| 119943 | +/* | |
| 119944 | +** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite | |
| 119945 | +** scalar user function. This C function is the callback used for all such | |
| 119946 | +** registered SQL functions. | |
| 119947 | +** | |
| 119948 | +** The scalar user functions return a blob that is interpreted by r-tree | |
| 119949 | +** table MATCH operators. | |
| 119950 | +*/ | |
| 119951 | +static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ | |
| 119952 | + RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx); | |
| 119953 | + RtreeMatchArg *pBlob; | |
| 119954 | + int nBlob; | |
| 119955 | + | |
| 119956 | + nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double); | |
| 119957 | + pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob); | |
| 119958 | + if( !pBlob ){ | |
| 119959 | + sqlite3_result_error_nomem(ctx); | |
| 119960 | + }else{ | |
| 119961 | + int i; | |
| 119962 | + pBlob->magic = RTREE_GEOMETRY_MAGIC; | |
| 119963 | + pBlob->xGeom = pGeomCtx->xGeom; | |
| 119964 | + pBlob->pContext = pGeomCtx->pContext; | |
| 119965 | + pBlob->nParam = nArg; | |
| 119966 | + for(i=0; i<nArg; i++){ | |
| 119967 | + pBlob->aParam[i] = sqlite3_value_double(aArg[i]); | |
| 119968 | + } | |
| 119969 | + sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free); | |
| 119970 | + } | |
| 119971 | +} | |
| 119972 | + | |
| 119973 | +/* | |
| 119974 | +** Register a new geometry function for use with the r-tree MATCH operator. | |
| 119975 | +*/ | |
| 119976 | +SQLITE_API int sqlite3_rtree_geometry_callback( | |
| 119977 | + sqlite3 *db, | |
| 119978 | + const char *zGeom, | |
| 119979 | + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *), | |
| 119980 | + void *pContext | |
| 119981 | +){ | |
| 119982 | + RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ | |
| 119983 | + | |
| 119984 | + /* Allocate and populate the context object. */ | |
| 119985 | + pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); | |
| 119986 | + if( !pGeomCtx ) return SQLITE_NOMEM; | |
| 119987 | + pGeomCtx->xGeom = xGeom; | |
| 119988 | + pGeomCtx->pContext = pContext; | |
| 119989 | + | |
| 119990 | + /* Create the new user-function. Register a destructor function to delete | |
| 119991 | + ** the context object when it is no longer required. */ | |
| 119992 | + return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, | |
| 119993 | + (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free | |
| 119994 | + ); | |
| 119995 | +} | |
| 118865 | 119996 | |
| 118866 | 119997 | #if !SQLITE_CORE |
| 118867 | 119998 | SQLITE_API int sqlite3_extension_init( |
| 118868 | 119999 | sqlite3 *db, |
| 118869 | 120000 | char **pzErrMsg, |
| 118870 | 120001 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | /****************************************************************************** |
| 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | ** version 3.7.2. By combining all the individual C code files into this |
| 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | ** translation unit. |
| @@ -352,19 +352,25 @@ | |
| 352 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 353 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 354 | #endif |
| 355 | |
| 356 | /* |
| 357 | ** The SQLITE_THREADSAFE macro must be defined as either 0 or 1. |
| 358 | ** Older versions of SQLite used an optional THREADSAFE macro. |
| 359 | ** We support that for legacy |
| 360 | */ |
| 361 | #if !defined(SQLITE_THREADSAFE) |
| 362 | #if defined(THREADSAFE) |
| 363 | # define SQLITE_THREADSAFE THREADSAFE |
| 364 | #else |
| 365 | # define SQLITE_THREADSAFE 1 |
| 366 | #endif |
| 367 | #endif |
| 368 | |
| 369 | /* |
| 370 | ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. |
| @@ -642,13 +648,13 @@ | |
| 642 | ** |
| 643 | ** See also: [sqlite3_libversion()], |
| 644 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 645 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 646 | */ |
| 647 | #define SQLITE_VERSION "3.7.2" |
| 648 | #define SQLITE_VERSION_NUMBER 3007002 |
| 649 | #define SQLITE_SOURCE_ID "2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3" |
| 650 | |
| 651 | /* |
| 652 | ** CAPI3REF: Run-Time Library Version Numbers |
| 653 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 654 | ** |
| @@ -1292,19 +1298,23 @@ | |
| 1292 | ** object once the object has been registered. |
| 1293 | ** |
| 1294 | ** The zName field holds the name of the VFS module. The name must |
| 1295 | ** be unique across all VFS modules. |
| 1296 | ** |
| 1297 | ** SQLite will guarantee that the zFilename parameter to xOpen |
| 1298 | ** is either a NULL pointer or string obtained |
| 1299 | ** from xFullPathname(). SQLite further guarantees that |
| 1300 | ** the string will be valid and unchanged until xClose() is |
| 1301 | ** called. Because of the previous sentence, |
| 1302 | ** the [sqlite3_file] can safely store a pointer to the |
| 1303 | ** filename if it needs to remember the filename for some reason. |
| 1304 | ** If the zFilename parameter is xOpen is a NULL pointer then xOpen |
| 1305 | ** must invent its own temporary name for the file. Whenever the |
| 1306 | ** xFilename parameter is NULL it will also be the case that the |
| 1307 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 1308 | ** |
| 1309 | ** The flags argument to xOpen() includes all bits set in |
| 1310 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -1311,11 +1321,11 @@ | |
| 1311 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 1312 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 1313 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 1314 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 1315 | ** |
| 1316 | ** SQLite will also add one of the following flags to the xOpen() |
| 1317 | ** call, depending on the object being opened: |
| 1318 | ** |
| 1319 | ** <ul> |
| 1320 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 1321 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -1322,11 +1332,12 @@ | |
| 1322 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 1323 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 1324 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 1325 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 1326 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 1327 | ** </ul> |
| 1328 | ** |
| 1329 | ** The file I/O implementation can use the object type flags to |
| 1330 | ** change the way it deals with files. For example, an application |
| 1331 | ** that does not care about crash recovery or rollback might make |
| 1332 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -1341,39 +1352,40 @@ | |
| 1341 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 1342 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 1343 | ** </ul> |
| 1344 | ** |
| 1345 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 1346 | ** deleted when it is closed. The [SQLITE_OPEN_DELETEONCLOSE] |
| 1347 | ** will be set for TEMP databases, journals and for subjournals. |
| 1348 | ** |
| 1349 | ** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction |
| 1350 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 1351 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 1352 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 1353 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 1354 | ** be created, and that it is an error if it already exists. |
| 1355 | ** It is <i>not</i> used to indicate the file should be opened |
| 1356 | ** for exclusive access. |
| 1357 | ** |
| 1358 | ** At least szOsFile bytes of memory are allocated by SQLite |
| 1359 | ** to hold the [sqlite3_file] structure passed as the third |
| 1360 | ** argument to xOpen. The xOpen method does not have to |
| 1361 | ** allocate the structure; it should just fill it in. Note that |
| 1362 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 1363 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 1364 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 1365 | ** element will be valid after xOpen returns regardless of the success |
| 1366 | ** or failure of the xOpen call. |
| 1367 | ** |
| 1368 | ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] |
| 1369 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 1370 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 1371 | ** to test whether a file is at least readable. The file can be a |
| 1372 | ** directory. |
| 1373 | ** |
| 1374 | ** SQLite will always allocate at least mxPathname+1 bytes for the |
| 1375 | ** output buffer xFullPathname. The exact size of the output buffer |
| 1376 | ** is also passed as a parameter to both methods. If the output buffer |
| 1377 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 1378 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 1379 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -1383,14 +1395,14 @@ | |
| 1383 | ** included in the VFS structure for completeness. |
| 1384 | ** The xRandomness() function attempts to return nBytes bytes |
| 1385 | ** of good-quality randomness into zOut. The return value is |
| 1386 | ** the actual number of bytes of randomness obtained. |
| 1387 | ** The xSleep() method causes the calling thread to sleep for at |
| 1388 | ** least the number of microseconds given. The xCurrentTime() |
| 1389 | ** method returns a Julian Day Number for the current date and time as |
| 1390 | ** a floating point value. |
| 1391 | ** The xCurrentTimeInt64() method returns, as an integer, the Julian |
| 1392 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 1393 | ** a 24-hour day). |
| 1394 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 1395 | ** date and time if that method is available (if iVersion is 2 or |
| 1396 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1783,11 +1795,11 @@ | |
| 1783 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1784 | ** following SQLite interfaces become non-operational: |
| 1785 | ** <ul> |
| 1786 | ** <li> [sqlite3_memory_used()] |
| 1787 | ** <li> [sqlite3_memory_highwater()] |
| 1788 | ** <li> [sqlite3_soft_heap_limit()] |
| 1789 | ** <li> [sqlite3_status()] |
| 1790 | ** </ul>)^ |
| 1791 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1792 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1793 | ** allocation statistics are disabled by default. |
| @@ -1797,19 +1809,18 @@ | |
| 1797 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1798 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1799 | ** aligned memory buffer from which the scrach allocations will be |
| 1800 | ** drawn, the size of each scratch allocation (sz), |
| 1801 | ** and the maximum number of scratch allocations (N). The sz |
| 1802 | ** argument must be a multiple of 16. The sz parameter should be a few bytes |
| 1803 | ** larger than the actual scratch space required due to internal overhead. |
| 1804 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1805 | ** of at least sz*N bytes of memory. |
| 1806 | ** ^SQLite will use no more than one scratch buffer per thread. So |
| 1807 | ** N should be set to the expected maximum number of threads. ^SQLite will |
| 1808 | ** never require a scratch buffer that is more than 6 times the database |
| 1809 | ** page size. ^If SQLite needs needs additional scratch memory beyond |
| 1810 | ** what is provided by this configuration option, then |
| 1811 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1812 | ** |
| 1813 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1814 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1815 | ** the database page cache with the default page cache implemenation. |
| @@ -1825,12 +1836,11 @@ | |
| 1825 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1826 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1827 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1828 | ** page cache memory is needed beyond what is provided by this option, then |
| 1829 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1830 | ** ^The implementation might use one or more of the N buffers to hold |
| 1831 | ** memory accounting information. The pointer in the first argument must |
| 1832 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1833 | ** will be undefined.</dd> |
| 1834 | ** |
| 1835 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1836 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1955,12 +1965,18 @@ | |
| 1955 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1956 | ** slots. The size of the buffer in the first argument must be greater than |
| 1957 | ** or equal to the product of the second and third arguments. The buffer |
| 1958 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1959 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1960 | ** rounded down to the next smaller |
| 1961 | ** multiple of 8. See also: [SQLITE_CONFIG_LOOKASIDE]</dd> |
| 1962 | ** |
| 1963 | ** </dl> |
| 1964 | */ |
| 1965 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1966 | |
| @@ -2260,10 +2276,13 @@ | |
| 2260 | */ |
| 2261 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 2262 | |
| 2263 | /* |
| 2264 | ** CAPI3REF: Convenience Routines For Running Queries |
| 2265 | ** |
| 2266 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 2267 | ** [sqlite3_get_table()] interface. A result table records the |
| 2268 | ** complete query results from one or more queries. |
| 2269 | ** |
| @@ -2281,11 +2300,11 @@ | |
| 2281 | ** |
| 2282 | ** A result table might consist of one or more memory allocations. |
| 2283 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 2284 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 2285 | ** |
| 2286 | ** As an example of the result table format, suppose a query result |
| 2287 | ** is as follows: |
| 2288 | ** |
| 2289 | ** <blockquote><pre> |
| 2290 | ** Name | Age |
| 2291 | ** ----------------------- |
| @@ -2305,31 +2324,31 @@ | |
| 2305 | ** azResult[3] = "43"; |
| 2306 | ** azResult[4] = "Bob"; |
| 2307 | ** azResult[5] = "28"; |
| 2308 | ** azResult[6] = "Cindy"; |
| 2309 | ** azResult[7] = "21"; |
| 2310 | ** </pre></blockquote> |
| 2311 | ** |
| 2312 | ** ^The sqlite3_get_table() function evaluates one or more |
| 2313 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 2314 | ** string of its 2nd parameter and returns a result table to the |
| 2315 | ** pointer given in its 3rd parameter. |
| 2316 | ** |
| 2317 | ** After the application has finished with the result from sqlite3_get_table(), |
| 2318 | ** it should pass the result table pointer to sqlite3_free_table() in order to |
| 2319 | ** release the memory that was malloced. Because of the way the |
| 2320 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 2321 | ** function must not try to call [sqlite3_free()] directly. Only |
| 2322 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 2323 | ** |
| 2324 | ** ^(The sqlite3_get_table() interface is implemented as a wrapper around |
| 2325 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 2326 | ** to any internal data structures of SQLite. It uses only the public |
| 2327 | ** interface defined here. As a consequence, errors that occur in the |
| 2328 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 2329 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 2330 | ** [sqlite3_errmsg()].)^ |
| 2331 | */ |
| 2332 | SQLITE_API int sqlite3_get_table( |
| 2333 | sqlite3 *db, /* An open database */ |
| 2334 | const char *zSql, /* SQL to be evaluated */ |
| 2335 | char ***pazResult, /* Results of the query */ |
| @@ -2477,11 +2496,13 @@ | |
| 2477 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 2478 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 2479 | ** is not freed. |
| 2480 | ** |
| 2481 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 2482 | ** is always aligned to at least an 8 byte boundary. |
| 2483 | ** |
| 2484 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 2485 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 2486 | ** implementation of these routines to be omitted. That capability |
| 2487 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2735,21 +2756,32 @@ | |
| 2735 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2736 | |
| 2737 | /* |
| 2738 | ** CAPI3REF: Query Progress Callbacks |
| 2739 | ** |
| 2740 | ** ^This routine configures a callback function - the |
| 2741 | ** progress callback - that is invoked periodically during long |
| 2742 | ** running calls to [sqlite3_exec()], [sqlite3_step()] and |
| 2743 | ** [sqlite3_get_table()]. An example use for this |
| 2744 | ** interface is to keep a GUI updated during a large query. |
| 2745 | ** |
| 2746 | ** ^If the progress callback returns non-zero, the operation is |
| 2747 | ** interrupted. This feature can be used to implement a |
| 2748 | ** "Cancel" button on a GUI progress dialog box. |
| 2749 | ** |
| 2750 | ** The progress handler must not do anything that will modify |
| 2751 | ** the database connection that invoked the progress handler. |
| 2752 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2753 | ** database connections for the meaning of "modify" in this paragraph. |
| 2754 | ** |
| 2755 | */ |
| @@ -2804,11 +2836,11 @@ | |
| 2804 | ** </dl> |
| 2805 | ** |
| 2806 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2807 | ** combinations shown above or one of the combinations shown above combined |
| 2808 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2809 | ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags, |
| 2810 | ** then the behavior is undefined. |
| 2811 | ** |
| 2812 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2813 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2814 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2929,21 +2961,26 @@ | |
| 2929 | ** ^(This interface allows the size of various constructs to be limited |
| 2930 | ** on a connection by connection basis. The first parameter is the |
| 2931 | ** [database connection] whose limit is to be set or queried. The |
| 2932 | ** second parameter is one of the [limit categories] that define a |
| 2933 | ** class of constructs to be size limited. The third parameter is the |
| 2934 | ** new limit for that construct. The function returns the old limit.)^ |
| 2935 | ** |
| 2936 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2937 | ** ^(For the limit category of SQLITE_LIMIT_XYZ there is a |
| 2938 | ** [limits | hard upper bound] |
| 2939 | ** set by a compile-time C preprocessor macro named |
| 2940 | ** [limits | SQLITE_MAX_XYZ]. |
| 2941 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2942 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2943 | ** silently truncated to the hard upper bound. |
| 2944 | ** |
| 2945 | ** Run-time limits are intended for use in applications that manage |
| 2946 | ** both their own internal database and also databases that are controlled |
| 2947 | ** by untrusted external sources. An example application might be a |
| 2948 | ** web browser that has its own databases for storing history and |
| 2949 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2968,11 +3005,11 @@ | |
| 2968 | ** The synopsis of the meanings of the various limits is shown below. |
| 2969 | ** Additional information is available at [limits | Limits in SQLite]. |
| 2970 | ** |
| 2971 | ** <dl> |
| 2972 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 2973 | ** <dd>The maximum size of any string or BLOB or table row.<dd>)^ |
| 2974 | ** |
| 2975 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 2976 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 2977 | ** |
| 2978 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2986,11 +3023,13 @@ | |
| 2986 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 2987 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 2988 | ** |
| 2989 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 2990 | ** <dd>The maximum number of instructions in a virtual machine program |
| 2991 | ** used to implement an SQL statement.</dd>)^ |
| 2992 | ** |
| 2993 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 2994 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 2995 | ** |
| 2996 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2999,12 +3038,11 @@ | |
| 2999 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 3000 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 3001 | ** [GLOB] operators.</dd>)^ |
| 3002 | ** |
| 3003 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 3004 | ** <dd>The maximum number of variables in an SQL statement that can |
| 3005 | ** be bound.</dd>)^ |
| 3006 | ** |
| 3007 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 3008 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 3009 | ** </dl> |
| 3010 | */ |
| @@ -3072,16 +3110,11 @@ | |
| 3072 | ** |
| 3073 | ** <ol> |
| 3074 | ** <li> |
| 3075 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 3076 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 3077 | ** statement and try to run it again. ^If the schema has changed in |
| 3078 | ** a way that makes the statement no longer valid, [sqlite3_step()] will still |
| 3079 | ** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is |
| 3080 | ** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the |
| 3081 | ** error go away. Note: use [sqlite3_errmsg()] to find the text |
| 3082 | ** of the parsing error that results in an [SQLITE_SCHEMA] return. |
| 3083 | ** </li> |
| 3084 | ** |
| 3085 | ** <li> |
| 3086 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 3087 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -3090,15 +3123,20 @@ | |
| 3090 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 3091 | ** interfaces, the underlying reason for the error is returned immediately. |
| 3092 | ** </li> |
| 3093 | ** |
| 3094 | ** <li> |
| 3095 | ** ^If the value of a [parameter | host parameter] in the WHERE clause might |
| 3096 | ** change the query plan for a statement, then the statement may be |
| 3097 | ** automatically recompiled (as if there had been a schema change) on the first |
| 3098 | ** [sqlite3_step()] call following any change to the |
| 3099 | ** [sqlite3_bind_text | bindings] of the [parameter]. |
| 3100 | ** </li> |
| 3101 | ** </ol> |
| 3102 | */ |
| 3103 | SQLITE_API int sqlite3_prepare( |
| 3104 | sqlite3 *db, /* Database handle */ |
| @@ -3161,11 +3199,11 @@ | |
| 3161 | ** or if SQLite is run in one of reduced mutex modes |
| 3162 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 3163 | ** then there is no distinction between protected and unprotected |
| 3164 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 3165 | ** for maximum code portability it is recommended that applications |
| 3166 | ** still make the distinction between between protected and unprotected |
| 3167 | ** sqlite3_value objects even when not strictly required. |
| 3168 | ** |
| 3169 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 3170 | ** implementation of [application-defined SQL functions] are protected. |
| 3171 | ** ^The sqlite3_value object returned by |
| @@ -3356,10 +3394,12 @@ | |
| 3356 | ** CAPI3REF: Number Of Columns In A Result Set |
| 3357 | ** |
| 3358 | ** ^Return the number of columns in the result set returned by the |
| 3359 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 3360 | ** statement that does not return data (for example an [UPDATE]). |
| 3361 | */ |
| 3362 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 3363 | |
| 3364 | /* |
| 3365 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3546,12 +3586,18 @@ | |
| 3546 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3547 | |
| 3548 | /* |
| 3549 | ** CAPI3REF: Number of columns in a result set |
| 3550 | ** |
| 3551 | ** ^The sqlite3_data_count(P) the number of columns in the |
| 3552 | ** of the result set of [prepared statement] P. |
| 3553 | */ |
| 3554 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3555 | |
| 3556 | /* |
| 3557 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3627,22 +3673,30 @@ | |
| 3627 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3628 | ** the string to UTF-8 and then returns the number of bytes. |
| 3629 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3630 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3631 | ** the number of bytes in that string. |
| 3632 | ** ^The value returned does not include the zero terminator at the end |
| 3633 | ** of the string. ^For clarity: the value returned is the number of |
| 3634 | ** bytes in the string, not the number of characters. |
| 3635 | ** |
| 3636 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3637 | ** even empty strings, are always zero terminated. ^The return |
| 3638 | ** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary |
| 3639 | ** pointer, possibly even a NULL pointer. |
| 3640 | ** |
| 3641 | ** ^The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() |
| 3642 | ** but leaves the result in UTF-16 in native byte order instead of UTF-8. |
| 3643 | ** ^The zero terminator is not included in this count. |
| 3644 | ** |
| 3645 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3646 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3647 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3648 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3683,14 +3737,14 @@ | |
| 3683 | ** and atof(). SQLite does not really use these functions. It has its |
| 3684 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3685 | ** used in the table for brevity and because they are familiar to most |
| 3686 | ** C programmers. |
| 3687 | ** |
| 3688 | ** ^Note that when type conversions occur, pointers returned by prior |
| 3689 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3690 | ** sqlite3_column_text16() may be invalidated. |
| 3691 | ** ^(Type conversions and pointer invalidations might occur |
| 3692 | ** in the following cases: |
| 3693 | ** |
| 3694 | ** <ul> |
| 3695 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3696 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3699,26 +3753,26 @@ | |
| 3699 | ** sqlite3_column_text16() is called. The content must be converted |
| 3700 | ** to UTF-16.</li> |
| 3701 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3702 | ** sqlite3_column_text() is called. The content must be converted |
| 3703 | ** to UTF-8.</li> |
| 3704 | ** </ul>)^ |
| 3705 | ** |
| 3706 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3707 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3708 | ** that the prior pointer points to will have been modified. Other kinds |
| 3709 | ** of conversion are done in place when it is possible, but sometimes they |
| 3710 | ** are not possible and in those cases prior pointers are invalidated. |
| 3711 | ** |
| 3712 | ** ^(The safest and easiest to remember policy is to invoke these routines |
| 3713 | ** in one of the following ways: |
| 3714 | ** |
| 3715 | ** <ul> |
| 3716 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3717 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3718 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3719 | ** </ul>)^ |
| 3720 | ** |
| 3721 | ** In other words, you should call sqlite3_column_text(), |
| 3722 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3723 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3724 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3752,21 +3806,30 @@ | |
| 3752 | |
| 3753 | /* |
| 3754 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3755 | ** |
| 3756 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3757 | ** ^If the statement was executed successfully or not executed at all, then |
| 3758 | ** SQLITE_OK is returned. ^If execution of the statement failed then an |
| 3759 | ** [error code] or [extended error code] is returned. |
| 3760 | ** |
| 3761 | ** ^This routine can be called at any point during the execution of the |
| 3762 | ** [prepared statement]. ^If the virtual machine has not |
| 3763 | ** completed execution when this routine is called, that is like |
| 3764 | ** encountering an error or an [sqlite3_interrupt | interrupt]. |
| 3765 | ** ^Incomplete updates may be rolled back and transactions canceled, |
| 3766 | ** depending on the circumstances, and the |
| 3767 | ** [error code] returned will be [SQLITE_ABORT]. |
| 3768 | */ |
| 3769 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3770 | |
| 3771 | /* |
| 3772 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3798,40 +3861,42 @@ | |
| 3798 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3799 | ** KEYWORDS: {function creation routines} |
| 3800 | ** KEYWORDS: {application-defined SQL function} |
| 3801 | ** KEYWORDS: {application-defined SQL functions} |
| 3802 | ** |
| 3803 | ** ^These two functions (collectively known as "function creation routines") |
| 3804 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3805 | ** of existing SQL functions or aggregates. The only difference between the |
| 3806 | ** two is that the second parameter, the name of the (scalar) function or |
| 3807 | ** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16 |
| 3808 | ** for sqlite3_create_function16(). |
| 3809 | ** |
| 3810 | ** ^The first parameter is the [database connection] to which the SQL |
| 3811 | ** function is to be added. ^If an application uses more than one database |
| 3812 | ** connection then application-defined SQL functions must be added |
| 3813 | ** to each database connection separately. |
| 3814 | ** |
| 3815 | ** The second parameter is the name of the SQL function to be created or |
| 3816 | ** redefined. ^The length of the name is limited to 255 bytes, exclusive of |
| 3817 | ** the zero-terminator. Note that the name length limit is in bytes, not |
| 3818 | ** characters. ^Any attempt to create a function with a longer name |
| 3819 | ** will result in [SQLITE_ERROR] being returned. |
| 3820 | ** |
| 3821 | ** ^The third parameter (nArg) |
| 3822 | ** is the number of arguments that the SQL function or |
| 3823 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3824 | ** aggregate may take any number of arguments between 0 and the limit |
| 3825 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3826 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3827 | ** undefined. |
| 3828 | ** |
| 3829 | ** The fourth parameter, eTextRep, specifies what |
| 3830 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3831 | ** its parameters. Any SQL function implementation should be able to work |
| 3832 | ** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be |
| 3833 | ** more efficient with one encoding than another. ^An application may |
| 3834 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3835 | ** times with the same function but with different values of eTextRep. |
| 3836 | ** ^When multiple implementations of the same function are available, SQLite |
| 3837 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3839,17 +3904,25 @@ | |
| 3839 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3840 | ** |
| 3841 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3842 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3843 | ** |
| 3844 | ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are |
| 3845 | ** pointers to C-language functions that implement the SQL function or |
| 3846 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3847 | ** callback only; NULL pointers should be passed as the xStep and xFinal |
| 3848 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3849 | ** and xFinal and NULL should be passed for xFunc. ^To delete an existing |
| 3850 | ** SQL function or aggregate, pass NULL for all three function callbacks. |
| 3851 | ** |
| 3852 | ** ^It is permitted to register multiple implementations of the same |
| 3853 | ** functions with the same name but with either differing numbers of |
| 3854 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3855 | ** the implementation that most closely matches the way in which the |
| @@ -3861,15 +3934,10 @@ | |
| 3861 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3862 | ** is a closer match than a function where the encoding difference is |
| 3863 | ** between UTF8 and UTF16. |
| 3864 | ** |
| 3865 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3866 | ** ^The first application-defined function with a given name overrides all |
| 3867 | ** built-in functions in the same [database connection] with the same name. |
| 3868 | ** ^Subsequent application-defined functions of the same name only override |
| 3869 | ** prior application-defined functions that are an exact match for the |
| 3870 | ** number of parameters and preferred encoding. |
| 3871 | ** |
| 3872 | ** ^An application-defined function is permitted to call other |
| 3873 | ** SQLite interfaces. However, such calls must not |
| 3874 | ** close the database connection nor finalize or reset the prepared |
| 3875 | ** statement in which the function is running. |
| @@ -3891,10 +3959,21 @@ | |
| 3891 | int eTextRep, |
| 3892 | void *pApp, |
| 3893 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3894 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3895 | void (*xFinal)(sqlite3_context*) |
| 3896 | ); |
| 3897 | |
| 3898 | /* |
| 3899 | ** CAPI3REF: Text Encodings |
| 3900 | ** |
| @@ -4238,73 +4317,97 @@ | |
| 4238 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 4239 | |
| 4240 | /* |
| 4241 | ** CAPI3REF: Define New Collating Sequences |
| 4242 | ** |
| 4243 | ** These functions are used to add new collation sequences to the |
| 4244 | ** [database connection] specified as the first argument. |
| 4245 | ** |
| 4246 | ** ^The name of the new collation sequence is specified as a UTF-8 string |
| 4247 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 4248 | ** and a UTF-16 string for sqlite3_create_collation16(). ^In all cases |
| 4249 | ** the name is passed as the second function argument. |
| 4250 | ** |
| 4251 | ** ^The third argument may be one of the constants [SQLITE_UTF8], |
| 4252 | ** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied |
| 4253 | ** routine expects to be passed pointers to strings encoded using UTF-8, |
| 4254 | ** UTF-16 little-endian, or UTF-16 big-endian, respectively. ^The |
| 4255 | ** third argument might also be [SQLITE_UTF16] to indicate that the routine |
| 4256 | ** expects pointers to be UTF-16 strings in the native byte order, or the |
| 4257 | ** argument can be [SQLITE_UTF16_ALIGNED] if the |
| 4258 | ** the routine expects pointers to 16-bit word aligned strings |
| 4259 | ** of UTF-16 in the native byte order. |
| 4260 | ** |
| 4261 | ** A pointer to the user supplied routine must be passed as the fifth |
| 4262 | ** argument. ^If it is NULL, this is the same as deleting the collation |
| 4263 | ** sequence (so that SQLite cannot call it any more). |
| 4264 | ** ^Each time the application supplied function is invoked, it is passed |
| 4265 | ** as its first parameter a copy of the void* passed as the fourth argument |
| 4266 | ** to sqlite3_create_collation() or sqlite3_create_collation16(). |
| 4267 | ** |
| 4268 | ** ^The remaining arguments to the application-supplied routine are two strings, |
| 4269 | ** each represented by a (length, data) pair and encoded in the encoding |
| 4270 | ** that was passed as the third argument when the collation sequence was |
| 4271 | ** registered. The application defined collation routine should |
| 4272 | ** return negative, zero or positive if the first string is less than, |
| 4273 | ** equal to, or greater than the second string. i.e. (STRING1 - STRING2). |
| 4274 | ** |
| 4275 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 4276 | ** except that it takes an extra argument which is a destructor for |
| 4277 | ** the collation. ^The destructor is called when the collation is |
| 4278 | ** destroyed and is passed a copy of the fourth parameter void* pointer |
| 4279 | ** of the sqlite3_create_collation_v2(). |
| 4280 | ** ^Collations are destroyed when they are overridden by later calls to the |
| 4281 | ** collation creation functions or when the [database connection] is closed |
| 4282 | ** using [sqlite3_close()]. |
| 4283 | ** |
| 4284 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 4285 | */ |
| 4286 | SQLITE_API int sqlite3_create_collation( |
| 4287 | sqlite3*, |
| 4288 | const char *zName, |
| 4289 | int eTextRep, |
| 4290 | void*, |
| 4291 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4292 | ); |
| 4293 | SQLITE_API int sqlite3_create_collation_v2( |
| 4294 | sqlite3*, |
| 4295 | const char *zName, |
| 4296 | int eTextRep, |
| 4297 | void*, |
| 4298 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 4299 | void(*xDestroy)(void*) |
| 4300 | ); |
| 4301 | SQLITE_API int sqlite3_create_collation16( |
| 4302 | sqlite3*, |
| 4303 | const void *zName, |
| 4304 | int eTextRep, |
| 4305 | void*, |
| 4306 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4307 | ); |
| 4308 | |
| 4309 | /* |
| 4310 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -4389,20 +4492,23 @@ | |
| 4389 | #endif |
| 4390 | |
| 4391 | /* |
| 4392 | ** CAPI3REF: Suspend Execution For A Short Time |
| 4393 | ** |
| 4394 | ** ^The sqlite3_sleep() function causes the current thread to suspend execution |
| 4395 | ** for at least a number of milliseconds specified in its parameter. |
| 4396 | ** |
| 4397 | ** ^If the operating system does not support sleep requests with |
| 4398 | ** millisecond time resolution, then the time will be rounded up to |
| 4399 | ** the nearest second. ^The number of milliseconds of sleep actually |
| 4400 | ** requested from the operating system is returned. |
| 4401 | ** |
| 4402 | ** ^SQLite implements this interface by calling the xSleep() |
| 4403 | ** method of the default [sqlite3_vfs] object. |
| 4404 | */ |
| 4405 | SQLITE_API int sqlite3_sleep(int); |
| 4406 | |
| 4407 | /* |
| 4408 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4620,44 +4726,77 @@ | |
| 4620 | ** of heap memory by deallocating non-essential memory allocations |
| 4621 | ** held by the database library. Memory used to cache database |
| 4622 | ** pages to improve performance is an example of non-essential memory. |
| 4623 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4624 | ** which might be more or less than the amount requested. |
| 4625 | */ |
| 4626 | SQLITE_API int sqlite3_release_memory(int); |
| 4627 | |
| 4628 | /* |
| 4629 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4630 | ** |
| 4631 | ** ^The sqlite3_soft_heap_limit() interface places a "soft" limit |
| 4632 | ** on the amount of heap memory that may be allocated by SQLite. |
| 4633 | ** ^If an internal allocation is requested that would exceed the |
| 4634 | ** soft heap limit, [sqlite3_release_memory()] is invoked one or |
| 4635 | ** more times to free up some space before the allocation is performed. |
| 4636 | ** |
| 4637 | ** ^The limit is called "soft" because if [sqlite3_release_memory()] |
| 4638 | ** cannot free sufficient memory to prevent the limit from being exceeded, |
| 4639 | ** the memory is allocated anyway and the current operation proceeds. |
| 4640 | ** |
| 4641 | ** ^A negative or zero value for N means that there is no soft heap limit and |
| 4642 | ** [sqlite3_release_memory()] will only be called when memory is exhausted. |
| 4643 | ** ^The default value for the soft heap limit is zero. |
| 4644 | ** |
| 4645 | ** ^(SQLite makes a best effort to honor the soft heap limit. |
| 4646 | ** But if the soft heap limit cannot be honored, execution will |
| 4647 | ** continue without error or notification.)^ This is why the limit is |
| 4648 | ** called a "soft" limit. It is advisory only. |
| 4649 | ** |
| 4650 | ** Prior to SQLite version 3.5.0, this routine only constrained the memory |
| 4651 | ** allocated by a single thread - the same thread in which this routine |
| 4652 | ** runs. Beginning with SQLite version 3.5.0, the soft heap limit is |
| 4653 | ** applied to all threads. The value specified for the soft heap limit |
| 4654 | ** is an upper bound on the total memory allocation for all threads. In |
| 4655 | ** version 3.5.0 there is no mechanism for limiting the heap usage for |
| 4656 | ** individual threads. |
| 4657 | */ |
| 4658 | SQLITE_API void sqlite3_soft_heap_limit(int); |
| 4659 | |
| 4660 | /* |
| 4661 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4662 | ** |
| 4663 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4777,38 +4916,51 @@ | |
| 4777 | ** it back off again. |
| 4778 | */ |
| 4779 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4780 | |
| 4781 | /* |
| 4782 | ** CAPI3REF: Automatically Load An Extensions |
| 4783 | ** |
| 4784 | ** ^This API can be invoked at program startup in order to register |
| 4785 | ** one or more statically linked extensions that will be available |
| 4786 | ** to all new [database connections]. |
| 4787 | ** |
| 4788 | ** ^(This routine stores a pointer to the extension entry point |
| 4789 | ** in an array that is obtained from [sqlite3_malloc()]. That memory |
| 4790 | ** is deallocated by [sqlite3_reset_auto_extension()].)^ |
| 4791 | ** |
| 4792 | ** ^This function registers an extension entry point that is |
| 4793 | ** automatically invoked whenever a new [database connection] |
| 4794 | ** is opened using [sqlite3_open()], [sqlite3_open16()], |
| 4795 | ** or [sqlite3_open_v2()]. |
| 4796 | ** ^Duplicate extensions are detected so calling this routine |
| 4797 | ** multiple times with the same extension is harmless. |
| 4798 | ** ^Automatic extensions apply across all threads. |
| 4799 | */ |
| 4800 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4801 | |
| 4802 | /* |
| 4803 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4804 | ** |
| 4805 | ** ^(This function disables all previously registered automatic |
| 4806 | ** extensions. It undoes the effect of all prior |
| 4807 | ** [sqlite3_auto_extension()] calls.)^ |
| 4808 | ** |
| 4809 | ** ^This function disables automatic extensions in all threads. |
| 4810 | */ |
| 4811 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4812 | |
| 4813 | /* |
| 4814 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -5443,11 +5595,11 @@ | |
| 5443 | ** output variable when querying the system for the current mutex |
| 5444 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 5445 | ** |
| 5446 | ** ^The xMutexInit method defined by this structure is invoked as |
| 5447 | ** part of system initialization by the sqlite3_initialize() function. |
| 5448 | ** ^The xMutexInit routine is calle by SQLite exactly once for each |
| 5449 | ** effective call to [sqlite3_initialize()]. |
| 5450 | ** |
| 5451 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 5452 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 5453 | ** implementation of this method is expected to release all outstanding |
| @@ -5640,11 +5792,12 @@ | |
| 5640 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5641 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5642 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5643 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5644 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5645 | #define SQLITE_TESTCTRL_LAST 17 |
| 5646 | |
| 5647 | /* |
| 5648 | ** CAPI3REF: SQLite Runtime Status |
| 5649 | ** |
| 5650 | ** ^This interface is used to retrieve runtime status information |
| @@ -5659,11 +5812,11 @@ | |
| 5659 | ** value. For those parameters |
| 5660 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5661 | ** ^(Other parameters record only the highwater mark and not the current |
| 5662 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5663 | ** |
| 5664 | ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a |
| 5665 | ** non-zero [error code] on failure. |
| 5666 | ** |
| 5667 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5668 | ** called while other threads are running the same or different SQLite |
| 5669 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5709,11 +5862,11 @@ | |
| 5709 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5710 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5711 | ** |
| 5712 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5713 | ** <dd>This parameter returns the number of bytes of page cache |
| 5714 | ** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE] |
| 5715 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5716 | ** returned value includes allocations that overflowed because they |
| 5717 | ** where too large (they were larger than the "sz" parameter to |
| 5718 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5719 | ** no space was left in the page cache.</dd>)^ |
| @@ -5732,11 +5885,11 @@ | |
| 5732 | ** outstanding at time, this parameter also reports the number of threads |
| 5733 | ** using scratch memory at the same time.</dd>)^ |
| 5734 | ** |
| 5735 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5736 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5737 | ** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH] |
| 5738 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5739 | ** returned include overflows because the requested allocation was too |
| 5740 | ** larger (that is, because the requested allocation was larger than the |
| 5741 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5742 | ** slots were available. |
| @@ -5780,10 +5933,13 @@ | |
| 5780 | ** |
| 5781 | ** ^The current value of the requested parameter is written into *pCur |
| 5782 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5783 | ** the resetFlg is true, then the highest instantaneous value is |
| 5784 | ** reset back down to the current value. |
| 5785 | ** |
| 5786 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5787 | */ |
| 5788 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5789 | |
| @@ -5907,122 +6063,134 @@ | |
| 5907 | ** CAPI3REF: Application Defined Page Cache. |
| 5908 | ** KEYWORDS: {page cache} |
| 5909 | ** |
| 5910 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5911 | ** register an alternative page cache implementation by passing in an |
| 5912 | ** instance of the sqlite3_pcache_methods structure.)^ The majority of the |
| 5913 | ** heap memory used by SQLite is used by the page cache to cache data read |
| 5914 | ** from, or ready to be written to, the database file. By implementing a |
| 5915 | ** custom page cache using this API, an application can control more |
| 5916 | ** precisely the amount of memory consumed by SQLite, the way in which |
| 5917 | ** that memory is allocated and released, and the policies used to |
| 5918 | ** determine exactly which parts of a database file are cached and for |
| 5919 | ** how long. |
| 5920 | ** |
| 5921 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 5922 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 5923 | ** the application may discard the parameter after the call to |
| 5924 | ** [sqlite3_config()] returns.)^ |
| 5925 | ** |
| 5926 | ** ^The xInit() method is called once for each call to [sqlite3_initialize()] |
| 5927 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 5928 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 5929 | ** ^The xInit() method can set up up global structures and/or any mutexes |
| 5930 | ** required by the custom page cache implementation. |
| 5931 | ** |
| 5932 | ** ^The xShutdown() method is called from within [sqlite3_shutdown()], |
| 5933 | ** if the application invokes this API. It can be used to clean up |
| 5934 | ** any outstanding resources before process shutdown, if required. |
| 5935 | ** |
| 5936 | ** ^SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes |
| 5937 | ** the xInit method, so the xInit method need not be threadsafe. ^The |
| 5938 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 5939 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 5940 | ** in multithreaded applications. |
| 5941 | ** |
| 5942 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 5943 | ** call to xShutdown(). |
| 5944 | ** |
| 5945 | ** ^The xCreate() method is used to construct a new cache instance. SQLite |
| 5946 | ** will typically create one cache instance for each open database file, |
| 5947 | ** though this is not guaranteed. ^The |
| 5948 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 5949 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 5950 | ** will the page size of the database file that is to be cached plus an |
| 5951 | ** increment (here called "R") of about 100 or 200. ^SQLite will use the |
| 5952 | ** extra R bytes on each page to store metadata about the underlying |
| 5953 | ** database page on disk. The value of R depends |
| 5954 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 5955 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 5956 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 5957 | ** be used to cache database pages of a file stored on disk, or |
| 5958 | ** false if it is used for an in-memory database. ^The cache implementation |
| 5959 | ** does not have to do anything special based with the value of bPurgeable; |
| 5960 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 5961 | ** never invoke xUnpin() except to deliberately delete a page. |
| 5962 | ** ^In other words, a cache created with bPurgeable set to false will |
| 5963 | ** never contain any unpinned pages. |
| 5964 | ** |
| 5965 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 5966 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 5967 | ** instance passed as the first argument. This is the value configured using |
| 5968 | ** the SQLite "[PRAGMA cache_size]" command.)^ ^As with the bPurgeable |
| 5969 | ** parameter, the implementation is not required to do anything with this |
| 5970 | ** value; it is advisory only. |
| 5971 | ** |
| 5972 | ** ^The xPagecount() method should return the number of pages currently |
| 5973 | ** stored in the cache. |
| 5974 | ** |
| 5975 | ** ^The xFetch() method is used to fetch a page and return a pointer to it. |
| 5976 | ** ^A 'page', in this context, is a buffer of szPage bytes aligned at an |
| 5977 | ** 8-byte boundary. ^The page to be fetched is determined by the key. ^The |
| 5978 | ** mimimum key value is 1. After it has been retrieved using xFetch, the page |
| 5979 | ** is considered to be "pinned". |
| 5980 | ** |
| 5981 | ** ^If the requested page is already in the page cache, then the page cache |
| 5982 | ** implementation must return a pointer to the page buffer with its content |
| 5983 | ** intact. ^(If the requested page is not already in the cache, then the |
| 5984 | ** behavior of the cache implementation is determined by the value of the |
| 5985 | ** createFlag parameter passed to xFetch, according to the following table: |
| 5986 | ** |
| 5987 | ** <table border=1 width=85% align=center> |
| 5988 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 5989 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 5990 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 5991 | ** Otherwise return NULL. |
| 5992 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 5993 | ** NULL if allocating a new page is effectively impossible. |
| 5994 | ** </table>)^ |
| 5995 | ** |
| 5996 | ** SQLite will normally invoke xFetch() with a createFlag of 0 or 1. If |
| 5997 | ** a call to xFetch() with createFlag==1 returns NULL, then SQLite will |
| 5998 | ** attempt to unpin one or more cache pages by spilling the content of |
| 5999 | ** pinned pages to disk and synching the operating system disk cache. After |
| 6000 | ** attempting to unpin pages, the xFetch() method will be invoked again with |
| 6001 | ** a createFlag of 2. |
| 6002 | ** |
| 6003 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 6004 | ** as its second argument. ^(If the third parameter, discard, is non-zero, |
| 6005 | ** then the page should be evicted from the cache. In this case SQLite |
| 6006 | ** assumes that the next time the page is retrieved from the cache using |
| 6007 | ** the xFetch() method, it will be zeroed.)^ ^If the discard parameter is |
| 6008 | ** zero, then the page is considered to be unpinned. ^The cache implementation |
| 6009 | ** may choose to evict unpinned pages at any time. |
| 6010 | ** |
| 6011 | ** ^(The cache is not required to perform any reference counting. A single |
| 6012 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 6013 | ** to xFetch().)^ |
| 6014 | ** |
| 6015 | ** ^The xRekey() method is used to change the key value associated with the |
| 6016 | ** page passed as the second argument from oldKey to newKey. ^If the cache |
| 6017 | ** previously contains an entry associated with newKey, it should be |
| 6018 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 6019 | ** to be pinned. |
| 6020 | ** |
| 6021 | ** ^When SQLite calls the xTruncate() method, the cache must discard all |
| 6022 | ** existing cache entries with page numbers (keys) greater than or equal |
| 6023 | ** to the value of the iLimit parameter passed to xTruncate(). ^If any |
| 6024 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 6025 | ** they can be safely discarded. |
| 6026 | ** |
| 6027 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 6028 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -6496,10 +6664,66 @@ | |
| 6496 | #if 0 |
| 6497 | } /* End of the 'extern "C"' block */ |
| 6498 | #endif |
| 6499 | #endif |
| 6500 | |
| 6501 | |
| 6502 | /************** End of sqlite3.h *********************************************/ |
| 6503 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 6504 | /************** Include hash.h in the middle of sqliteInt.h ******************/ |
| 6505 | /************** Begin file hash.h ********************************************/ |
| @@ -7066,10 +7290,11 @@ | |
| 7066 | typedef struct Schema Schema; |
| 7067 | typedef struct Expr Expr; |
| 7068 | typedef struct ExprList ExprList; |
| 7069 | typedef struct ExprSpan ExprSpan; |
| 7070 | typedef struct FKey FKey; |
| 7071 | typedef struct FuncDef FuncDef; |
| 7072 | typedef struct FuncDefHash FuncDefHash; |
| 7073 | typedef struct IdList IdList; |
| 7074 | typedef struct Index Index; |
| 7075 | typedef struct IndexSample IndexSample; |
| @@ -7172,16 +7397,15 @@ | |
| 7172 | ** following values. |
| 7173 | ** |
| 7174 | ** NOTE: These values must match the corresponding PAGER_ values in |
| 7175 | ** pager.h. |
| 7176 | */ |
| 7177 | #define BTREE_OMIT_JOURNAL 1 /* Do not use journal. No argument */ |
| 7178 | #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ |
| 7179 | #define BTREE_MEMORY 4 /* In-memory DB. No argument */ |
| 7180 | #define BTREE_READONLY 8 /* Open the database in read-only mode */ |
| 7181 | #define BTREE_READWRITE 16 /* Open for both reading and writing */ |
| 7182 | #define BTREE_CREATE 32 /* Create the database if it does not exist */ |
| 7183 | |
| 7184 | SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); |
| 7185 | SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); |
| 7186 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int); |
| 7187 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| @@ -7213,15 +7437,21 @@ | |
| 7213 | SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *); |
| 7214 | |
| 7215 | SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *); |
| 7216 | |
| 7217 | /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR |
| 7218 | ** of the following flags: |
| 7219 | */ |
| 7220 | #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ |
| 7221 | #define BTREE_ZERODATA 2 /* Table has keys only - no data */ |
| 7222 | #define BTREE_LEAFDATA 4 /* Data stored in leaves only. Implies INTKEY */ |
| 7223 | |
| 7224 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); |
| 7225 | SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); |
| 7226 | SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); |
| 7227 | |
| @@ -7838,10 +8068,11 @@ | |
| 7838 | ** |
| 7839 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| 7840 | */ |
| 7841 | #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ |
| 7842 | #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ |
| 7843 | |
| 7844 | /* |
| 7845 | ** Valid values for the second argument to sqlite3PagerLockingMode(). |
| 7846 | */ |
| 7847 | #define PAGER_LOCKINGMODE_QUERY -1 |
| @@ -8472,12 +8703,12 @@ | |
| 8472 | #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) |
| 8473 | #define sqlite3_mutex_free(X) |
| 8474 | #define sqlite3_mutex_enter(X) |
| 8475 | #define sqlite3_mutex_try(X) SQLITE_OK |
| 8476 | #define sqlite3_mutex_leave(X) |
| 8477 | #define sqlite3_mutex_held(X) 1 |
| 8478 | #define sqlite3_mutex_notheld(X) 1 |
| 8479 | #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) |
| 8480 | #define sqlite3MutexInit() SQLITE_OK |
| 8481 | #define sqlite3MutexEnd() |
| 8482 | #endif /* defined(SQLITE_MUTEX_OMIT) */ |
| 8483 | |
| @@ -8795,10 +9026,31 @@ | |
| 8795 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ |
| 8796 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ |
| 8797 | void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ |
| 8798 | char *zName; /* SQL name of the function. */ |
| 8799 | FuncDef *pHash; /* Next with a different name but the same hash */ |
| 8800 | }; |
| 8801 | |
| 8802 | /* |
| 8803 | ** Possible values for FuncDef.flags |
| 8804 | */ |
| @@ -8835,19 +9087,19 @@ | |
| 8835 | ** FuncDef.flags variable is set to the value passed as the flags |
| 8836 | ** parameter. |
| 8837 | */ |
| 8838 | #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ |
| 8839 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 8840 | SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0} |
| 8841 | #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ |
| 8842 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 8843 | pArg, 0, xFunc, 0, 0, #zName, 0} |
| 8844 | #define LIKEFUNC(zName, nArg, arg, flags) \ |
| 8845 | {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0} |
| 8846 | #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ |
| 8847 | {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \ |
| 8848 | SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0} |
| 8849 | |
| 8850 | /* |
| 8851 | ** All current savepoints are stored in a linked list starting at |
| 8852 | ** sqlite3.pSavepoint. The first element in the list is the most recently |
| 8853 | ** opened savepoint. Savepoints are added to the list by the vdbe |
| @@ -9063,10 +9315,11 @@ | |
| 9063 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9064 | int nCol; /* Number of columns in this table */ |
| 9065 | Column *aCol; /* Information about each column */ |
| 9066 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9067 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9068 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9069 | u16 nRef; /* Number of pointers to this Table */ |
| 9070 | u8 tabFlags; /* Mask of TF_* values */ |
| 9071 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9072 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| @@ -10341,11 +10594,11 @@ | |
| 10341 | SQLITE_PRIVATE void sqlite3ScratchFree(void*); |
| 10342 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 10343 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 10344 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| 10345 | SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); |
| 10346 | SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64); |
| 10347 | |
| 10348 | /* |
| 10349 | ** On systems with ample stack space and that support alloca(), make |
| 10350 | ** use of alloca() to obtain space for large automatic objects. By default, |
| 10351 | ** obtain space from malloc(). |
| @@ -10512,11 +10765,10 @@ | |
| 10512 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| 10513 | SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int); |
| 10514 | SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int); |
| 10515 | SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); |
| 10516 | SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); |
| 10517 | SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse*,int,int); |
| 10518 | SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int); |
| 10519 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); |
| 10520 | SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); |
| 10521 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 10522 | SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*); |
| @@ -10632,12 +10884,10 @@ | |
| 10632 | # define sqlite3AuthContextPush(a,b,c) |
| 10633 | # define sqlite3AuthContextPop(a) ((void)(a)) |
| 10634 | #endif |
| 10635 | SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*); |
| 10636 | SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*); |
| 10637 | SQLITE_PRIVATE int sqlite3BtreeFactory(sqlite3 *db, const char *zFilename, |
| 10638 | int omitJournal, int nCache, int flags, Btree **ppBtree); |
| 10639 | SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); |
| 10640 | SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); |
| 10641 | SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); |
| 10642 | SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); |
| 10643 | SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*); |
| @@ -10759,11 +11009,13 @@ | |
| 10759 | SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *); |
| 10760 | SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *); |
| 10761 | SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *); |
| 10762 | SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, |
| 10763 | void (*)(sqlite3_context*,int,sqlite3_value **), |
| 10764 | void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*)); |
| 10765 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 10766 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 10767 | |
| 10768 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 10769 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| @@ -11679,10 +11931,11 @@ | |
| 11679 | Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 11680 | Bool nullRow; /* True if pointing to a row with no data */ |
| 11681 | Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 11682 | Bool isTable; /* True if a table requiring integer keys */ |
| 11683 | Bool isIndex; /* True if an index containing keys only - no data */ |
| 11684 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 11685 | Btree *pBt; /* Separate file holding temporary table */ |
| 11686 | int pseudoTableReg; /* Register holding pseudotable content. */ |
| 11687 | KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 11688 | int nField; /* Number of fields in the header */ |
| @@ -11773,10 +12026,14 @@ | |
| 11773 | char *z; /* String or BLOB value */ |
| 11774 | int n; /* Number of characters in string value, excluding '\0' */ |
| 11775 | u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 11776 | u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ |
| 11777 | u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 11778 | void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ |
| 11779 | char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ |
| 11780 | }; |
| 11781 | |
| 11782 | /* One or more of the following flags are set to indicate the validOK |
| @@ -11799,10 +12056,11 @@ | |
| 11799 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 11800 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 11801 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 11802 | #define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 11803 | #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 11804 | #define MEM_TypeMask 0x00ff /* Mask of type bits */ |
| 11805 | |
| 11806 | /* Whenever Mem contains a valid string or blob representation, one of |
| 11807 | ** the following flags must be set to determine the memory management |
| 11808 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| @@ -11812,23 +12070,29 @@ | |
| 11812 | #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 11813 | #define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 11814 | #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 11815 | #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 11816 | #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 11817 | |
| 11818 | #ifdef SQLITE_OMIT_INCRBLOB |
| 11819 | #undef MEM_Zero |
| 11820 | #define MEM_Zero 0x0000 |
| 11821 | #endif |
| 11822 | |
| 11823 | |
| 11824 | /* |
| 11825 | ** Clear any existing type flags from a Mem and replace them with f |
| 11826 | */ |
| 11827 | #define MemSetTypeFlag(p, f) \ |
| 11828 | ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) |
| 11829 | |
| 11830 | |
| 11831 | /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains |
| 11832 | ** additional information about auxiliary information bound to arguments |
| 11833 | ** of the function. This is used to implement the sqlite3_get_auxdata() |
| 11834 | ** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data |
| @@ -12012,10 +12276,14 @@ | |
| 12012 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12013 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12014 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12015 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12016 | SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12017 | |
| 12018 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 12019 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 12020 | #else |
| 12021 | # define sqlite3VdbeCheckFk(p,i) 0 |
| @@ -13518,10 +13786,16 @@ | |
| 13518 | SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 13519 | return pVfs->xSleep(pVfs, nMicro); |
| 13520 | } |
| 13521 | SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 13522 | int rc; |
| 13523 | if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ |
| 13524 | rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); |
| 13525 | }else{ |
| 13526 | double r; |
| 13527 | rc = pVfs->xCurrentTime(pVfs, &r); |
| @@ -13901,11 +14175,11 @@ | |
| 13901 | ** routines and redirected to xFree. |
| 13902 | */ |
| 13903 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 13904 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
| 13905 | assert( pPrior!=0 && nByte>0 ); |
| 13906 | nByte = ROUND8(nByte); |
| 13907 | p--; |
| 13908 | p = realloc(p, nByte+8 ); |
| 13909 | if( p ){ |
| 13910 | p[0] = nByte; |
| 13911 | p++; |
| @@ -14307,10 +14581,11 @@ | |
| 14307 | */ |
| 14308 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 14309 | struct MemBlockHdr *pOldHdr; |
| 14310 | void *pNew; |
| 14311 | assert( mem.disallow==0 ); |
| 14312 | pOldHdr = sqlite3MemsysGetHeader(pPrior); |
| 14313 | pNew = sqlite3MemMalloc(nByte); |
| 14314 | if( pNew ){ |
| 14315 | memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize); |
| 14316 | if( nByte>pOldHdr->iSize ){ |
| @@ -15576,11 +15851,11 @@ | |
| 15576 | */ |
| 15577 | static void *memsys5Realloc(void *pPrior, int nBytes){ |
| 15578 | int nOld; |
| 15579 | void *p; |
| 15580 | assert( pPrior!=0 ); |
| 15581 | assert( (nBytes&(nBytes-1))==0 ); |
| 15582 | assert( nBytes>=0 ); |
| 15583 | if( nBytes==0 ){ |
| 15584 | return 0; |
| 15585 | } |
| 15586 | nOld = memsys5Size(pPrior); |
| @@ -17100,10 +17375,70 @@ | |
| 17100 | ************************************************************************* |
| 17101 | ** |
| 17102 | ** Memory allocation functions used throughout sqlite. |
| 17103 | */ |
| 17104 | |
| 17105 | /* |
| 17106 | ** This routine runs when the memory allocator sees that the |
| 17107 | ** total memory allocation is about to exceed the soft heap |
| 17108 | ** limit. |
| 17109 | */ |
| @@ -17114,82 +17449,70 @@ | |
| 17114 | ){ |
| 17115 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 17116 | sqlite3_release_memory(allocSize); |
| 17117 | } |
| 17118 | |
| 17119 | /* |
| 17120 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 17121 | ** negative value indicates no limit. |
| 17122 | */ |
| 17123 | SQLITE_API void sqlite3_soft_heap_limit(int n){ |
| 17124 | sqlite3_uint64 iLimit; |
| 17125 | int overage; |
| 17126 | if( n<0 ){ |
| 17127 | iLimit = 0; |
| 17128 | }else{ |
| 17129 | iLimit = n; |
| 17130 | } |
| 17131 | #ifndef SQLITE_OMIT_AUTOINIT |
| 17132 | sqlite3_initialize(); |
| 17133 | #endif |
| 17134 | if( iLimit>0 ){ |
| 17135 | sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); |
| 17136 | }else{ |
| 17137 | sqlite3MemoryAlarm(0, 0, 0); |
| 17138 | } |
| 17139 | overage = (int)(sqlite3_memory_used() - (i64)n); |
| 17140 | if( overage>0 ){ |
| 17141 | sqlite3_release_memory(overage); |
| 17142 | } |
| 17143 | } |
| 17144 | |
| 17145 | /* |
| 17146 | ** Attempt to release up to n bytes of non-essential memory currently |
| 17147 | ** held by SQLite. An example of non-essential memory is memory used to |
| 17148 | ** cache database pages that are not currently in use. |
| 17149 | */ |
| 17150 | SQLITE_API int sqlite3_release_memory(int n){ |
| 17151 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 17152 | int nRet = 0; |
| 17153 | nRet += sqlite3PcacheReleaseMemory(n-nRet); |
| 17154 | return nRet; |
| 17155 | #else |
| 17156 | UNUSED_PARAMETER(n); |
| 17157 | return SQLITE_OK; |
| 17158 | #endif |
| 17159 | } |
| 17160 | |
| 17161 | /* |
| 17162 | ** State information local to the memory allocation subsystem. |
| 17163 | */ |
| 17164 | static SQLITE_WSD struct Mem0Global { |
| 17165 | /* Number of free pages for scratch and page-cache memory */ |
| 17166 | u32 nScratchFree; |
| 17167 | u32 nPageFree; |
| 17168 | |
| 17169 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 17170 | |
| 17171 | /* |
| 17172 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 17173 | ** be held while the callback is running. Recursive calls into |
| 17174 | ** the memory subsystem are allowed, but no new callbacks will be |
| 17175 | ** issued. |
| 17176 | */ |
| 17177 | sqlite3_int64 alarmThreshold; |
| 17178 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 17179 | void *alarmArg; |
| 17180 | |
| 17181 | /* |
| 17182 | ** Pointers to the end of sqlite3GlobalConfig.pScratch and |
| 17183 | ** sqlite3GlobalConfig.pPage to a block of memory that records |
| 17184 | ** which pages are available. |
| 17185 | */ |
| 17186 | u32 *aScratchFree; |
| 17187 | u32 *aPageFree; |
| 17188 | } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 17189 | |
| 17190 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
| 17191 | |
| 17192 | /* |
| 17193 | ** Initialize the memory allocation subsystem. |
| 17194 | */ |
| 17195 | SQLITE_PRIVATE int sqlite3MallocInit(void){ |
| @@ -17199,39 +17522,48 @@ | |
| 17199 | memset(&mem0, 0, sizeof(mem0)); |
| 17200 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 17201 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
| 17202 | } |
| 17203 | if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 |
| 17204 | && sqlite3GlobalConfig.nScratch>=0 ){ |
| 17205 | int i; |
| 17206 | sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4); |
| 17207 | mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) |
| 17208 | [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; |
| 17209 | for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } |
| 17210 | mem0.nScratchFree = sqlite3GlobalConfig.nScratch; |
| 17211 | }else{ |
| 17212 | sqlite3GlobalConfig.pScratch = 0; |
| 17213 | sqlite3GlobalConfig.szScratch = 0; |
| 17214 | } |
| 17215 | if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 |
| 17216 | && sqlite3GlobalConfig.nPage>=1 ){ |
| 17217 | int i; |
| 17218 | int overhead; |
| 17219 | int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage); |
| 17220 | int n = sqlite3GlobalConfig.nPage; |
| 17221 | overhead = (4*n + sz - 1)/sz; |
| 17222 | sqlite3GlobalConfig.nPage -= overhead; |
| 17223 | mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) |
| 17224 | [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; |
| 17225 | for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } |
| 17226 | mem0.nPageFree = sqlite3GlobalConfig.nPage; |
| 17227 | }else{ |
| 17228 | sqlite3GlobalConfig.pPage = 0; |
| 17229 | sqlite3GlobalConfig.szPage = 0; |
| 17230 | } |
| 17231 | return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
| 17232 | } |
| 17233 | |
| 17234 | /* |
| 17235 | ** Deinitialize the memory allocation subsystem. |
| 17236 | */ |
| 17237 | SQLITE_PRIVATE void sqlite3MallocEnd(void){ |
| @@ -17263,40 +17595,10 @@ | |
| 17263 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
| 17264 | res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ |
| 17265 | return res; |
| 17266 | } |
| 17267 | |
| 17268 | /* |
| 17269 | ** Change the alarm callback |
| 17270 | */ |
| 17271 | SQLITE_PRIVATE int sqlite3MemoryAlarm( |
| 17272 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 17273 | void *pArg, |
| 17274 | sqlite3_int64 iThreshold |
| 17275 | ){ |
| 17276 | sqlite3_mutex_enter(mem0.mutex); |
| 17277 | mem0.alarmCallback = xCallback; |
| 17278 | mem0.alarmArg = pArg; |
| 17279 | mem0.alarmThreshold = iThreshold; |
| 17280 | sqlite3_mutex_leave(mem0.mutex); |
| 17281 | return SQLITE_OK; |
| 17282 | } |
| 17283 | |
| 17284 | #ifndef SQLITE_OMIT_DEPRECATED |
| 17285 | /* |
| 17286 | ** Deprecated external interface. Internal/core SQLite code |
| 17287 | ** should call sqlite3MemoryAlarm. |
| 17288 | */ |
| 17289 | SQLITE_API int sqlite3_memory_alarm( |
| 17290 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 17291 | void *pArg, |
| 17292 | sqlite3_int64 iThreshold |
| 17293 | ){ |
| 17294 | return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); |
| 17295 | } |
| 17296 | #endif |
| 17297 | |
| 17298 | /* |
| 17299 | ** Trigger the alarm |
| 17300 | */ |
| 17301 | static void sqlite3MallocAlarm(int nByte){ |
| 17302 | void (*xCallback)(void*,sqlite3_int64,int); |
| @@ -17325,18 +17627,23 @@ | |
| 17325 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 17326 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 17327 | if( mem0.alarmCallback!=0 ){ |
| 17328 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 17329 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 17330 | sqlite3MallocAlarm(nFull); |
| 17331 | } |
| 17332 | } |
| 17333 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17334 | if( p==0 && mem0.alarmCallback ){ |
| 17335 | sqlite3MallocAlarm(nFull); |
| 17336 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17337 | } |
| 17338 | if( p ){ |
| 17339 | nFull = sqlite3MallocSize(p); |
| 17340 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 17341 | sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 17342 | } |
| @@ -17348,11 +17655,13 @@ | |
| 17348 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 17349 | ** assumes the memory subsystem has already been initialized. |
| 17350 | */ |
| 17351 | SQLITE_PRIVATE void *sqlite3Malloc(int n){ |
| 17352 | void *p; |
| 17353 | if( n<=0 || n>=0x7fffff00 ){ |
| 17354 | /* A memory allocation of a number of bytes which is near the maximum |
| 17355 | ** signed integer value might cause an integer overflow inside of the |
| 17356 | ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 17357 | ** 255 bytes of overhead. SQLite itself will never use anything near |
| 17358 | ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
| @@ -17362,10 +17671,11 @@ | |
| 17362 | mallocWithAlarm(n, &p); |
| 17363 | sqlite3_mutex_leave(mem0.mutex); |
| 17364 | }else{ |
| 17365 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 17366 | } |
| 17367 | return p; |
| 17368 | } |
| 17369 | |
| 17370 | /* |
| 17371 | ** This version of the memory allocation is for use by the application. |
| @@ -17399,64 +17709,70 @@ | |
| 17399 | ** embedded processor. |
| 17400 | */ |
| 17401 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ |
| 17402 | void *p; |
| 17403 | assert( n>0 ); |
| 17404 | |
| 17405 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17406 | /* Verify that no more than two scratch allocation per thread |
| 17407 | ** is outstanding at one time. (This is only checked in the |
| 17408 | ** single-threaded case since checking in the multi-threaded case |
| 17409 | ** would be much more complicated.) */ |
| 17410 | assert( scratchAllocOut<=1 ); |
| 17411 | #endif |
| 17412 | |
| 17413 | if( sqlite3GlobalConfig.szScratch<n ){ |
| 17414 | goto scratch_overflow; |
| 17415 | }else{ |
| 17416 | sqlite3_mutex_enter(mem0.mutex); |
| 17417 | if( mem0.nScratchFree==0 ){ |
| 17418 | sqlite3_mutex_leave(mem0.mutex); |
| 17419 | goto scratch_overflow; |
| 17420 | }else{ |
| 17421 | int i; |
| 17422 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
| 17423 | i *= sqlite3GlobalConfig.szScratch; |
| 17424 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 17425 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 17426 | sqlite3_mutex_leave(mem0.mutex); |
| 17427 | p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; |
| 17428 | assert( (((u8*)p - (u8*)0) & 7)==0 ); |
| 17429 | } |
| 17430 | } |
| 17431 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17432 | scratchAllocOut = p!=0; |
| 17433 | #endif |
| 17434 | |
| 17435 | return p; |
| 17436 | |
| 17437 | scratch_overflow: |
| 17438 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17439 | sqlite3_mutex_enter(mem0.mutex); |
| 17440 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 17441 | n = mallocWithAlarm(n, &p); |
| 17442 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 17443 | sqlite3_mutex_leave(mem0.mutex); |
| 17444 | }else{ |
| 17445 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 17446 | } |
| 17447 | sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 17448 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17449 | scratchAllocOut = p!=0; |
| 17450 | #endif |
| 17451 | return p; |
| 17452 | } |
| 17453 | SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ |
| 17454 | if( p ){ |
| 17455 | if( sqlite3GlobalConfig.pScratch==0 |
| 17456 | || p<sqlite3GlobalConfig.pScratch |
| 17457 | || p>=(void*)mem0.aScratchFree ){ |
| 17458 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 17459 | assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 17460 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 17461 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17462 | int iSize = sqlite3MallocSize(p); |
| @@ -17467,30 +17783,10 @@ | |
| 17467 | sqlite3GlobalConfig.m.xFree(p); |
| 17468 | sqlite3_mutex_leave(mem0.mutex); |
| 17469 | }else{ |
| 17470 | sqlite3GlobalConfig.m.xFree(p); |
| 17471 | } |
| 17472 | }else{ |
| 17473 | int i; |
| 17474 | i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); |
| 17475 | i /= sqlite3GlobalConfig.szScratch; |
| 17476 | assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); |
| 17477 | sqlite3_mutex_enter(mem0.mutex); |
| 17478 | assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); |
| 17479 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
| 17480 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 17481 | sqlite3_mutex_leave(mem0.mutex); |
| 17482 | |
| 17483 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17484 | /* Verify that no more than two scratch allocation per thread |
| 17485 | ** is outstanding at one time. (This is only checked in the |
| 17486 | ** single-threaded case since checking in the multi-threaded case |
| 17487 | ** would be much more complicated.) */ |
| 17488 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 17489 | scratchAllocOut = 0; |
| 17490 | #endif |
| 17491 | |
| 17492 | } |
| 17493 | } |
| 17494 | } |
| 17495 | |
| 17496 | /* |
| @@ -17527,11 +17823,11 @@ | |
| 17527 | |
| 17528 | /* |
| 17529 | ** Free memory previously obtained from sqlite3Malloc(). |
| 17530 | */ |
| 17531 | SQLITE_API void sqlite3_free(void *p){ |
| 17532 | if( p==0 ) return; |
| 17533 | assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 17534 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17535 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17536 | sqlite3_mutex_enter(mem0.mutex); |
| 17537 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| @@ -17574,21 +17870,24 @@ | |
| 17574 | */ |
| 17575 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){ |
| 17576 | int nOld, nNew; |
| 17577 | void *pNew; |
| 17578 | if( pOld==0 ){ |
| 17579 | return sqlite3Malloc(nBytes); |
| 17580 | } |
| 17581 | if( nBytes<=0 ){ |
| 17582 | sqlite3_free(pOld); |
| 17583 | return 0; |
| 17584 | } |
| 17585 | if( nBytes>=0x7fffff00 ){ |
| 17586 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 17587 | return 0; |
| 17588 | } |
| 17589 | nOld = sqlite3MallocSize(pOld); |
| 17590 | nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); |
| 17591 | if( nOld==nNew ){ |
| 17592 | pNew = pOld; |
| 17593 | }else if( sqlite3GlobalConfig.bMemstat ){ |
| 17594 | sqlite3_mutex_enter(mem0.mutex); |
| @@ -17610,10 +17909,11 @@ | |
| 17610 | } |
| 17611 | sqlite3_mutex_leave(mem0.mutex); |
| 17612 | }else{ |
| 17613 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 17614 | } |
| 17615 | return pNew; |
| 17616 | } |
| 17617 | |
| 17618 | /* |
| 17619 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| @@ -19773,10 +20073,16 @@ | |
| 19773 | #define UpperToLower sqlite3UpperToLower |
| 19774 | |
| 19775 | /* |
| 19776 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 19777 | ** there is no consistency, we will define our own. |
| 19778 | */ |
| 19779 | SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 19780 | register unsigned char *a, *b; |
| 19781 | a = (unsigned char *)zLeft; |
| 19782 | b = (unsigned char *)zRight; |
| @@ -26177,11 +26483,11 @@ | |
| 26177 | goto shmpage_out; |
| 26178 | } |
| 26179 | pShmNode->apRegion = apNew; |
| 26180 | while(pShmNode->nRegion<=iRegion){ |
| 26181 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
| 26182 | MAP_SHARED, pShmNode->h, iRegion*szRegion |
| 26183 | ); |
| 26184 | if( pMem==MAP_FAILED ){ |
| 26185 | rc = SQLITE_IOERR; |
| 26186 | goto shmpage_out; |
| 26187 | } |
| @@ -28000,17 +28306,20 @@ | |
| 28000 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 28001 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 28002 | |
| 28003 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 28004 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 28005 | if( gethostuuid(pHostID, &timeout) ){ |
| 28006 | int err = errno; |
| 28007 | if( pError ){ |
| 28008 | *pError = err; |
| 28009 | } |
| 28010 | return SQLITE_IOERR; |
| 28011 | } |
| 28012 | #ifdef SQLITE_TEST |
| 28013 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 28014 | if( sqlite3_hostid_num != 0){ |
| 28015 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 28016 | } |
| @@ -30339,10 +30648,18 @@ | |
| 30339 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN; |
| 30340 | } |
| 30341 | |
| 30342 | #ifndef SQLITE_OMIT_WAL |
| 30343 | |
| 30344 | /* |
| 30345 | ** Helper functions to obtain and relinquish the global mutex. The |
| 30346 | ** global mutex is used to protect the winLockInfo objects used by |
| 30347 | ** this file, all of which may be shared by multiple threads. |
| 30348 | ** |
| @@ -30507,19 +30824,26 @@ | |
| 30507 | ** by VFS shared-memory methods. |
| 30508 | */ |
| 30509 | static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ |
| 30510 | winShmNode **pp; |
| 30511 | winShmNode *p; |
| 30512 | assert( winShmMutexHeld() ); |
| 30513 | pp = &winShmNodeList; |
| 30514 | while( (p = *pp)!=0 ){ |
| 30515 | if( p->nRef==0 ){ |
| 30516 | int i; |
| 30517 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
| 30518 | for(i=0; i<p->nRegion; i++){ |
| 30519 | UnmapViewOfFile(p->aRegion[i].pMap); |
| 30520 | CloseHandle(p->aRegion[i].hMap); |
| 30521 | } |
| 30522 | if( p->hFile.h != INVALID_HANDLE_VALUE ){ |
| 30523 | SimulateIOErrorBenign(1); |
| 30524 | winClose((sqlite3_file *)&p->hFile); |
| 30525 | SimulateIOErrorBenign(0); |
| @@ -30592,14 +30916,15 @@ | |
| 30592 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 30593 | if( pShmNode->mutex==0 ){ |
| 30594 | rc = SQLITE_NOMEM; |
| 30595 | goto shm_open_err; |
| 30596 | } |
| 30597 | rc = winOpen(pDbFd->pVfs, |
| 30598 | pShmNode->zFilename, /* Name of the file (UTF-8) */ |
| 30599 | (sqlite3_file*)&pShmNode->hFile, /* File handle here */ |
| 30600 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */ |
| 30601 | 0); |
| 30602 | if( SQLITE_OK!=rc ){ |
| 30603 | rc = SQLITE_CANTOPEN_BKPT; |
| 30604 | goto shm_open_err; |
| 30605 | } |
| @@ -30903,14 +31228,22 @@ | |
| 30903 | void *pMap = 0; /* Mapped memory region */ |
| 30904 | |
| 30905 | hMap = CreateFileMapping(pShmNode->hFile.h, |
| 30906 | NULL, PAGE_READWRITE, 0, nByte, NULL |
| 30907 | ); |
| 30908 | if( hMap ){ |
| 30909 | pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, |
| 30910 | 0, 0, nByte |
| 30911 | ); |
| 30912 | } |
| 30913 | if( !pMap ){ |
| 30914 | pShmNode->lastErrno = GetLastError(); |
| 30915 | rc = SQLITE_IOERR; |
| 30916 | if( hMap ) CloseHandle(hMap); |
| @@ -30923,12 +31256,14 @@ | |
| 30923 | } |
| 30924 | } |
| 30925 | |
| 30926 | shmpage_out: |
| 30927 | if( pShmNode->nRegion>iRegion ){ |
| 30928 | char *p = (char *)pShmNode->aRegion[iRegion].pMap; |
| 30929 | *pp = (void *)&p[iRegion*szRegion]; |
| 30930 | }else{ |
| 30931 | *pp = 0; |
| 30932 | } |
| 30933 | sqlite3_mutex_leave(pShmNode->mutex); |
| 30934 | return rc; |
| @@ -31151,13 +31486,64 @@ | |
| 31151 | DWORD dwFlagsAndAttributes = 0; |
| 31152 | #if SQLITE_OS_WINCE |
| 31153 | int isTemp = 0; |
| 31154 | #endif |
| 31155 | winFile *pFile = (winFile*)id; |
| 31156 | void *zConverted; /* Filename in OS encoding */ |
| 31157 | const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ |
| 31158 | char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */ |
| 31159 | |
| 31160 | assert( id!=0 ); |
| 31161 | UNUSED_PARAMETER(pVfs); |
| 31162 | |
| 31163 | pFile->h = INVALID_HANDLE_VALUE; |
| @@ -31164,11 +31550,12 @@ | |
| 31164 | |
| 31165 | /* If the second argument to this function is NULL, generate a |
| 31166 | ** temporary file name to use |
| 31167 | */ |
| 31168 | if( !zUtf8Name ){ |
| 31169 | int rc = getTempname(MAX_PATH+1, zTmpname); |
| 31170 | if( rc!=SQLITE_OK ){ |
| 31171 | return rc; |
| 31172 | } |
| 31173 | zUtf8Name = zTmpname; |
| 31174 | } |
| @@ -31177,33 +31564,35 @@ | |
| 31177 | zConverted = convertUtf8Filename(zUtf8Name); |
| 31178 | if( zConverted==0 ){ |
| 31179 | return SQLITE_NOMEM; |
| 31180 | } |
| 31181 | |
| 31182 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 31183 | dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 31184 | }else{ |
| 31185 | dwDesiredAccess = GENERIC_READ; |
| 31186 | } |
| 31187 | /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is |
| 31188 | ** created. SQLite doesn't use it to indicate "exclusive access" |
| 31189 | ** as it is usually understood. |
| 31190 | */ |
| 31191 | assert(!(flags & SQLITE_OPEN_EXCLUSIVE) || (flags & SQLITE_OPEN_CREATE)); |
| 31192 | if( flags & SQLITE_OPEN_EXCLUSIVE ){ |
| 31193 | /* Creates a new file, only if it does not already exist. */ |
| 31194 | /* If the file exists, it fails. */ |
| 31195 | dwCreationDisposition = CREATE_NEW; |
| 31196 | }else if( flags & SQLITE_OPEN_CREATE ){ |
| 31197 | /* Open existing file, or create if it doesn't exist */ |
| 31198 | dwCreationDisposition = OPEN_ALWAYS; |
| 31199 | }else{ |
| 31200 | /* Opens a file, only if it exists. */ |
| 31201 | dwCreationDisposition = OPEN_EXISTING; |
| 31202 | } |
| 31203 | dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 31204 | if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 31205 | #if SQLITE_OS_WINCE |
| 31206 | dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
| 31207 | isTemp = 1; |
| 31208 | #else |
| 31209 | dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| @@ -31216,10 +31605,11 @@ | |
| 31216 | /* Reports from the internet are that performance is always |
| 31217 | ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
| 31218 | #if SQLITE_OS_WINCE |
| 31219 | dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
| 31220 | #endif |
| 31221 | if( isNT() ){ |
| 31222 | h = CreateFileW((WCHAR*)zConverted, |
| 31223 | dwDesiredAccess, |
| 31224 | dwShareMode, |
| 31225 | NULL, |
| @@ -31241,41 +31631,45 @@ | |
| 31241 | dwFlagsAndAttributes, |
| 31242 | NULL |
| 31243 | ); |
| 31244 | #endif |
| 31245 | } |
| 31246 | OSTRACE(("OPEN %d %s 0x%lx %s\n", |
| 31247 | h, zName, dwDesiredAccess, |
| 31248 | h==INVALID_HANDLE_VALUE ? "failed" : "ok")); |
| 31249 | if( h==INVALID_HANDLE_VALUE ){ |
| 31250 | pFile->lastErrno = GetLastError(); |
| 31251 | free(zConverted); |
| 31252 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 31253 | return winOpen(pVfs, zName, id, |
| 31254 | ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags); |
| 31255 | }else{ |
| 31256 | return SQLITE_CANTOPEN_BKPT; |
| 31257 | } |
| 31258 | } |
| 31259 | if( pOutFlags ){ |
| 31260 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 31261 | *pOutFlags = SQLITE_OPEN_READWRITE; |
| 31262 | }else{ |
| 31263 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 31264 | } |
| 31265 | } |
| 31266 | memset(pFile, 0, sizeof(*pFile)); |
| 31267 | pFile->pMethod = &winIoMethod; |
| 31268 | pFile->h = h; |
| 31269 | pFile->lastErrno = NO_ERROR; |
| 31270 | pFile->pVfs = pVfs; |
| 31271 | pFile->pShm = 0; |
| 31272 | pFile->zPath = zName; |
| 31273 | pFile->sectorSize = getSectorSize(pVfs, zUtf8Name); |
| 31274 | #if SQLITE_OS_WINCE |
| 31275 | if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) == |
| 31276 | (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB) |
| 31277 | && !winceCreateLock(zName, pFile) |
| 31278 | ){ |
| 31279 | CloseHandle(h); |
| 31280 | free(zConverted); |
| 31281 | return SQLITE_CANTOPEN_BKPT; |
| @@ -31285,12 +31679,13 @@ | |
| 31285 | }else |
| 31286 | #endif |
| 31287 | { |
| 31288 | free(zConverted); |
| 31289 | } |
| 31290 | OpenCounter(+1); |
| 31291 | return SQLITE_OK; |
| 31292 | } |
| 31293 | |
| 31294 | /* |
| 31295 | ** Delete the named file. |
| 31296 | ** |
| @@ -31804,10 +32199,17 @@ | |
| 31804 | winSleep, /* xSleep */ |
| 31805 | winCurrentTime, /* xCurrentTime */ |
| 31806 | winGetLastError, /* xGetLastError */ |
| 31807 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 31808 | }; |
| 31809 | |
| 31810 | sqlite3_vfs_register(&winVfs, 1); |
| 31811 | return SQLITE_OK; |
| 31812 | } |
| 31813 | SQLITE_API int sqlite3_os_end(void){ |
| @@ -32369,16 +32771,20 @@ | |
| 32369 | ** Initialize and shutdown the page cache subsystem. Neither of these |
| 32370 | ** functions are threadsafe. |
| 32371 | */ |
| 32372 | SQLITE_PRIVATE int sqlite3PcacheInitialize(void){ |
| 32373 | if( sqlite3GlobalConfig.pcache.xInit==0 ){ |
| 32374 | sqlite3PCacheSetDefault(); |
| 32375 | } |
| 32376 | return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg); |
| 32377 | } |
| 32378 | SQLITE_PRIVATE void sqlite3PcacheShutdown(void){ |
| 32379 | if( sqlite3GlobalConfig.pcache.xShutdown ){ |
| 32380 | sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg); |
| 32381 | } |
| 32382 | } |
| 32383 | |
| 32384 | /* |
| @@ -32835,12 +33241,17 @@ | |
| 32835 | |
| 32836 | typedef struct PCache1 PCache1; |
| 32837 | typedef struct PgHdr1 PgHdr1; |
| 32838 | typedef struct PgFreeslot PgFreeslot; |
| 32839 | |
| 32840 | /* Pointers to structures of this type are cast and returned as |
| 32841 | ** opaque sqlite3_pcache* handles |
| 32842 | */ |
| 32843 | struct PCache1 { |
| 32844 | /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 32845 | ** flag (bPurgeable) are set when the cache is created. nMax may be |
| 32846 | ** modified at any time by a call to the pcache1CacheSize() method. |
| @@ -32896,10 +33307,13 @@ | |
| 32896 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 32897 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 32898 | |
| 32899 | /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */ |
| 32900 | int szSlot; /* Size of each free slot */ |
| 32901 | void *pStart, *pEnd; /* Bounds of pagecache malloc range */ |
| 32902 | PgFreeslot *pFree; /* Free page blocks */ |
| 32903 | int isInit; /* True if initialized */ |
| 32904 | } pcache1_g; |
| 32905 | |
| @@ -32943,10 +33357,12 @@ | |
| 32943 | SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
| 32944 | if( pcache1.isInit ){ |
| 32945 | PgFreeslot *p; |
| 32946 | sz = ROUNDDOWN8(sz); |
| 32947 | pcache1.szSlot = sz; |
| 32948 | pcache1.pStart = pBuf; |
| 32949 | pcache1.pFree = 0; |
| 32950 | while( n-- ){ |
| 32951 | p = (PgFreeslot*)pBuf; |
| 32952 | p->pNext = pcache1.pFree; |
| @@ -32969,10 +33385,12 @@ | |
| 32969 | sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 32970 | if( nByte<=pcache1.szSlot && pcache1.pFree ){ |
| 32971 | assert( pcache1.isInit ); |
| 32972 | p = (PgHdr1 *)pcache1.pFree; |
| 32973 | pcache1.pFree = pcache1.pFree->pNext; |
| 32974 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 32975 | }else{ |
| 32976 | |
| 32977 | /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the |
| 32978 | ** global pcache mutex and unlock the pager-cache object pCache. This is |
| @@ -33002,10 +33420,12 @@ | |
| 33002 | PgFreeslot *pSlot; |
| 33003 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 33004 | pSlot = (PgFreeslot*)p; |
| 33005 | pSlot->pNext = pcache1.pFree; |
| 33006 | pcache1.pFree = pSlot; |
| 33007 | }else{ |
| 33008 | int iSize; |
| 33009 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 33010 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 33011 | iSize = sqlite3MallocSize(p); |
| @@ -33014,11 +33434,11 @@ | |
| 33014 | } |
| 33015 | } |
| 33016 | |
| 33017 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 33018 | /* |
| 33019 | ** Return the size of a pache allocation |
| 33020 | */ |
| 33021 | static int pcache1MemSize(void *p){ |
| 33022 | assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33023 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33024 | return pcache1.szSlot; |
| @@ -33087,10 +33507,36 @@ | |
| 33087 | pcache1EnterMutex(); |
| 33088 | pcache1Free(p); |
| 33089 | pcache1LeaveMutex(); |
| 33090 | } |
| 33091 | |
| 33092 | /******************************************************************************/ |
| 33093 | /******** General Implementation Functions ************************************/ |
| 33094 | |
| 33095 | /* |
| 33096 | ** This function is used to resize the hash table used by the cache passed |
| @@ -33328,18 +33774,20 @@ | |
| 33328 | ** copy of the requested page. If one is found, it is returned. |
| 33329 | ** |
| 33330 | ** 2. If createFlag==0 and the page is not already in the cache, NULL is |
| 33331 | ** returned. |
| 33332 | ** |
| 33333 | ** 3. If createFlag is 1, and the page is not already in the cache, |
| 33334 | ** and if either of the following are true, return NULL: |
| 33335 | ** |
| 33336 | ** (a) the number of pages pinned by the cache is greater than |
| 33337 | ** PCache1.nMax, or |
| 33338 | ** (b) the number of pages pinned by the cache is greater than |
| 33339 | ** the sum of nMax for all purgeable caches, less the sum of |
| 33340 | ** nMin for all other purgeable caches. |
| 33341 | ** |
| 33342 | ** 4. If none of the first three conditions apply and the cache is marked |
| 33343 | ** as purgeable, and if one of the following is true: |
| 33344 | ** |
| 33345 | ** (a) The number of pages allocated for the cache is already |
| @@ -33346,10 +33794,13 @@ | |
| 33346 | ** PCache1.nMax, or |
| 33347 | ** |
| 33348 | ** (b) The number of pages allocated for all purgeable caches is |
| 33349 | ** already equal to or greater than the sum of nMax for all |
| 33350 | ** purgeable caches, |
| 33351 | ** |
| 33352 | ** then attempt to recycle a page from the LRU list. If it is the right |
| 33353 | ** size, return the recycled buffer. Otherwise, free the buffer and |
| 33354 | ** proceed to step 5. |
| 33355 | ** |
| @@ -33378,10 +33829,11 @@ | |
| 33378 | /* Step 3 of header comment. */ |
| 33379 | nPinned = pCache->nPage - pCache->nRecyclable; |
| 33380 | if( createFlag==1 && ( |
| 33381 | nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage) |
| 33382 | || nPinned>=(pCache->nMax * 9 / 10) |
| 33383 | )){ |
| 33384 | goto fetch_out; |
| 33385 | } |
| 33386 | |
| 33387 | if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){ |
| @@ -33388,11 +33840,13 @@ | |
| 33388 | goto fetch_out; |
| 33389 | } |
| 33390 | |
| 33391 | /* Step 4. Try to recycle a page buffer if appropriate. */ |
| 33392 | if( pCache->bPurgeable && pcache1.pLruTail && ( |
| 33393 | (pCache->nPage+1>=pCache->nMax) || pcache1.nCurrentPage>=pcache1.nMaxPage |
| 33394 | )){ |
| 33395 | pPage = pcache1.pLruTail; |
| 33396 | pcache1RemoveFromHash(pPage); |
| 33397 | pcache1PinPage(pPage); |
| 33398 | if( pPage->pCache->szPage!=pCache->szPage ){ |
| @@ -33531,10 +33985,11 @@ | |
| 33531 | ** |
| 33532 | ** Destroy a cache allocated using pcache1Create(). |
| 33533 | */ |
| 33534 | static void pcache1Destroy(sqlite3_pcache *p){ |
| 33535 | PCache1 *pCache = (PCache1 *)p; |
| 33536 | pcache1EnterMutex(); |
| 33537 | pcache1TruncateUnsafe(pCache, 0); |
| 33538 | pcache1.nMaxPage -= pCache->nMax; |
| 33539 | pcache1.nMinPage -= pCache->nMin; |
| 33540 | pcache1EnforceMaxPage(); |
| @@ -33578,11 +34033,11 @@ | |
| 33578 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 33579 | int nFree = 0; |
| 33580 | if( pcache1.pStart==0 ){ |
| 33581 | PgHdr1 *p; |
| 33582 | pcache1EnterMutex(); |
| 33583 | while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){ |
| 33584 | nFree += pcache1MemSize(PGHDR1_TO_PAGE(p)); |
| 33585 | pcache1PinPage(p); |
| 33586 | pcache1RemoveFromHash(p); |
| 33587 | pcache1FreePage(p); |
| 33588 | } |
| @@ -37120,16 +37575,17 @@ | |
| 37120 | ** the duplicate call is harmless. |
| 37121 | */ |
| 37122 | sqlite3WalEndReadTransaction(pPager->pWal); |
| 37123 | |
| 37124 | rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed); |
| 37125 | if( rc==SQLITE_OK && changed ){ |
| 37126 | pager_reset(pPager); |
| 37127 | } |
| 37128 | |
| 37129 | return rc; |
| 37130 | } |
| 37131 | |
| 37132 | /* |
| 37133 | ** This function is called as part of the transition from PAGER_OPEN |
| 37134 | ** to PAGER_READER state to determine the size of the database file |
| 37135 | ** in pages (assuming the page size currently stored in Pager.pageSize). |
| @@ -37182,11 +37638,11 @@ | |
| 37182 | |
| 37183 | *pnPage = nPage; |
| 37184 | return SQLITE_OK; |
| 37185 | } |
| 37186 | |
| 37187 | |
| 37188 | /* |
| 37189 | ** Check if the *-wal file that corresponds to the database opened by pPager |
| 37190 | ** exists if the database is not empy, or verify that the *-wal file does |
| 37191 | ** not exist (by deleting it) if the database file is empty. |
| 37192 | ** |
| @@ -38374,10 +38830,17 @@ | |
| 38374 | journalFileSize = ROUND8(sqlite3MemJournalSize()); |
| 38375 | } |
| 38376 | |
| 38377 | /* Set the output variable to NULL in case an error occurs. */ |
| 38378 | *ppPager = 0; |
| 38379 | |
| 38380 | /* Compute and store the full pathname in an allocated buffer pointed |
| 38381 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 38382 | ** leave both nPathname and zPathname set to 0. |
| 38383 | */ |
| @@ -38385,21 +38848,12 @@ | |
| 38385 | nPathname = pVfs->mxPathname+1; |
| 38386 | zPathname = sqlite3Malloc(nPathname*2); |
| 38387 | if( zPathname==0 ){ |
| 38388 | return SQLITE_NOMEM; |
| 38389 | } |
| 38390 | #ifndef SQLITE_OMIT_MEMORYDB |
| 38391 | if( strcmp(zFilename,":memory:")==0 ){ |
| 38392 | memDb = 1; |
| 38393 | zPathname[0] = 0; |
| 38394 | }else |
| 38395 | #endif |
| 38396 | { |
| 38397 | zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ |
| 38398 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
| 38399 | } |
| 38400 | |
| 38401 | nPathname = sqlite3Strlen30(zPathname); |
| 38402 | if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ |
| 38403 | /* This branch is taken when the journal path required by |
| 38404 | ** the database being opened will be more than pVfs->mxPathname |
| 38405 | ** bytes in length. This means the database cannot be opened, |
| @@ -38450,34 +38904,31 @@ | |
| 38450 | pPager->zFilename = (char*)(pPtr += journalFileSize); |
| 38451 | assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 38452 | |
| 38453 | /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ |
| 38454 | if( zPathname ){ |
| 38455 | pPager->zJournal = (char*)(pPtr += nPathname + 1); |
| 38456 | memcpy(pPager->zFilename, zPathname, nPathname); |
| 38457 | memcpy(pPager->zJournal, zPathname, nPathname); |
| 38458 | memcpy(&pPager->zJournal[nPathname], "-journal", 8); |
| 38459 | if( pPager->zFilename[0]==0 ){ |
| 38460 | pPager->zJournal[0] = 0; |
| 38461 | } |
| 38462 | #ifndef SQLITE_OMIT_WAL |
| 38463 | else{ |
| 38464 | pPager->zWal = &pPager->zJournal[nPathname+8+1]; |
| 38465 | memcpy(pPager->zWal, zPathname, nPathname); |
| 38466 | memcpy(&pPager->zWal[nPathname], "-wal", 4); |
| 38467 | } |
| 38468 | #endif |
| 38469 | sqlite3_free(zPathname); |
| 38470 | } |
| 38471 | pPager->pVfs = pVfs; |
| 38472 | pPager->vfsFlags = vfsFlags; |
| 38473 | |
| 38474 | /* Open the pager file. |
| 38475 | */ |
| 38476 | if( zFilename && zFilename[0] && !memDb ){ |
| 38477 | int fout = 0; /* VFS flags returned by xOpen() */ |
| 38478 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); |
| 38479 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 38480 | |
| 38481 | /* If the file was successfully opened for read/write access, |
| 38482 | ** choose a default page size in case we have to create the |
| 38483 | ** database file. The default page size is the maximum of: |
| @@ -38927,11 +39378,13 @@ | |
| 38927 | |
| 38928 | /* If there is a WAL file in the file-system, open this database in WAL |
| 38929 | ** mode. Otherwise, the following function call is a no-op. |
| 38930 | */ |
| 38931 | rc = pagerOpenWalIfPresent(pPager); |
| 38932 | assert( pPager->pWal==0 || rc==SQLITE_OK ); |
| 38933 | } |
| 38934 | |
| 38935 | if( pagerUseWal(pPager) ){ |
| 38936 | assert( rc==SQLITE_OK ); |
| 38937 | rc = pagerBeginReadTransaction(pPager); |
| @@ -43292,11 +43745,11 @@ | |
| 43292 | WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); |
| 43293 | if( rc!=SQLITE_OK ){ |
| 43294 | return rc; |
| 43295 | } |
| 43296 | } |
| 43297 | assert( pWal->szPage==szPage ); |
| 43298 | |
| 43299 | /* Write the log file. */ |
| 43300 | for(p=pList; p; p=p->pDirty){ |
| 43301 | u32 nDbsize; /* Db-size field for frame header */ |
| 43302 | i64 iOffset; /* Write offset in log file */ |
| @@ -43952,10 +44405,11 @@ | |
| 43952 | MemPage *pPage1; /* First page of the database */ |
| 43953 | u8 readOnly; /* True if the underlying file is readonly */ |
| 43954 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
| 43955 | u8 secureDelete; /* True if secure_delete is enabled */ |
| 43956 | u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 43957 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43958 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 43959 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 43960 | #endif |
| 43961 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| @@ -46202,15 +46656,24 @@ | |
| 46202 | |
| 46203 | /* |
| 46204 | ** Open a database file. |
| 46205 | ** |
| 46206 | ** zFilename is the name of the database file. If zFilename is NULL |
| 46207 | ** a new database with a random name is created. This randomly named |
| 46208 | ** database file will be deleted when sqlite3BtreeClose() is called. |
| 46209 | ** If zFilename is ":memory:" then an in-memory database is created |
| 46210 | ** that is automatically destroyed when it is closed. |
| 46211 | ** |
| 46212 | ** If the database is already opened in the same database connection |
| 46213 | ** and we are in shared cache mode, then the open will fail with an |
| 46214 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 46215 | ** objects in the same database connection since doing so will lead |
| 46216 | ** to problems with locking. |
| @@ -46227,10 +46690,13 @@ | |
| 46227 | Btree *p; /* Handle to return */ |
| 46228 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 46229 | int rc = SQLITE_OK; /* Result code from this function */ |
| 46230 | u8 nReserve; /* Byte of unused space on each page */ |
| 46231 | unsigned char zDbHeader[100]; /* Database header content */ |
| 46232 | |
| 46233 | /* Set the variable isMemdb to true for an in-memory database, or |
| 46234 | ** false for a file-based database. This symbol is only required if |
| 46235 | ** either of the shared-data or autovacuum features are compiled |
| 46236 | ** into the library. |
| @@ -46237,17 +46703,34 @@ | |
| 46237 | */ |
| 46238 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 46239 | #ifdef SQLITE_OMIT_MEMORYDB |
| 46240 | const int isMemdb = 0; |
| 46241 | #else |
| 46242 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
| 46243 | #endif |
| 46244 | #endif |
| 46245 | |
| 46246 | assert( db!=0 ); |
| 46247 | assert( sqlite3_mutex_held(db->mutex) ); |
| 46248 | |
| 46249 | pVfs = db->pVfs; |
| 46250 | p = sqlite3MallocZero(sizeof(Btree)); |
| 46251 | if( !p ){ |
| 46252 | return SQLITE_NOMEM; |
| 46253 | } |
| @@ -46261,11 +46744,11 @@ | |
| 46261 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 46262 | /* |
| 46263 | ** If this Btree is a candidate for shared cache, try to find an |
| 46264 | ** existing BtShared object that we can share with |
| 46265 | */ |
| 46266 | if( isMemdb==0 && zFilename && zFilename[0] ){ |
| 46267 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
| 46268 | int nFullPathname = pVfs->mxPathname+1; |
| 46269 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
| 46270 | sqlite3_mutex *mutexShared; |
| 46271 | p->sharable = 1; |
| @@ -46336,10 +46819,11 @@ | |
| 46336 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 46337 | } |
| 46338 | if( rc!=SQLITE_OK ){ |
| 46339 | goto btree_open_out; |
| 46340 | } |
| 46341 | pBt->db = db; |
| 46342 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
| 46343 | p->pBt = pBt; |
| 46344 | |
| 46345 | pBt->pCursor = 0; |
| @@ -46440,10 +46924,18 @@ | |
| 46440 | sqlite3PagerClose(pBt->pPager); |
| 46441 | } |
| 46442 | sqlite3_free(pBt); |
| 46443 | sqlite3_free(p); |
| 46444 | *ppBtree = 0; |
| 46445 | } |
| 46446 | if( mutexOpen ){ |
| 46447 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 46448 | sqlite3_mutex_leave(mutexOpen); |
| 46449 | } |
| @@ -51390,15 +51882,16 @@ | |
| 51390 | ** flags might not work: |
| 51391 | ** |
| 51392 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 51393 | ** BTREE_ZERODATA Used for SQL indices |
| 51394 | */ |
| 51395 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
| 51396 | BtShared *pBt = p->pBt; |
| 51397 | MemPage *pRoot; |
| 51398 | Pgno pgnoRoot; |
| 51399 | int rc; |
| 51400 | |
| 51401 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 51402 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 51403 | assert( !pBt->readOnly ); |
| 51404 | |
| @@ -51513,12 +52006,18 @@ | |
| 51513 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
| 51514 | if( rc ) return rc; |
| 51515 | } |
| 51516 | #endif |
| 51517 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 51518 | zeroPage(pRoot, flags | PTF_LEAF); |
| 51519 | sqlite3PagerUnref(pRoot->pDbPage); |
| 51520 | *piTable = (int)pgnoRoot; |
| 51521 | return SQLITE_OK; |
| 51522 | } |
| 51523 | SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 51524 | int rc; |
| @@ -52774,11 +53273,14 @@ | |
| 52774 | sqlite3Error( |
| 52775 | pDestDb, SQLITE_ERROR, "source and destination must be distinct" |
| 52776 | ); |
| 52777 | p = 0; |
| 52778 | }else { |
| 52779 | /* Allocate space for a new sqlite3_backup object */ |
| 52780 | p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); |
| 52781 | if( !p ){ |
| 52782 | sqlite3Error(pDestDb, SQLITE_NOMEM, 0); |
| 52783 | } |
| 52784 | } |
| @@ -53157,10 +53659,13 @@ | |
| 53157 | if( p->pDestDb ){ |
| 53158 | sqlite3_mutex_leave(p->pDestDb->mutex); |
| 53159 | } |
| 53160 | sqlite3BtreeLeave(p->pSrc); |
| 53161 | if( p->pDestDb ){ |
| 53162 | sqlite3_free(p); |
| 53163 | } |
| 53164 | sqlite3_mutex_leave(mutex); |
| 53165 | return rc; |
| 53166 | } |
| @@ -53408,10 +53913,13 @@ | |
| 53408 | return SQLITE_NOMEM; |
| 53409 | } |
| 53410 | pMem->z[pMem->n] = 0; |
| 53411 | pMem->z[pMem->n+1] = 0; |
| 53412 | pMem->flags |= MEM_Term; |
| 53413 | } |
| 53414 | |
| 53415 | return SQLITE_OK; |
| 53416 | } |
| 53417 | |
| @@ -53528,11 +54036,11 @@ | |
| 53528 | memset(&ctx, 0, sizeof(ctx)); |
| 53529 | ctx.s.flags = MEM_Null; |
| 53530 | ctx.s.db = pMem->db; |
| 53531 | ctx.pMem = pMem; |
| 53532 | ctx.pFunc = pFunc; |
| 53533 | pFunc->xFinalize(&ctx); |
| 53534 | assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); |
| 53535 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 53536 | memcpy(pMem, &ctx.s, sizeof(ctx.s)); |
| 53537 | rc = ctx.isError; |
| 53538 | } |
| @@ -53869,10 +54377,32 @@ | |
| 53869 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 53870 | } |
| 53871 | return 0; |
| 53872 | } |
| 53873 | |
| 53874 | /* |
| 53875 | ** Size of struct Mem not including the Mem.zMalloc member. |
| 53876 | */ |
| 53877 | #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) |
| 53878 | |
| @@ -54237,11 +54767,11 @@ | |
| 54237 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); |
| 54238 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ |
| 54239 | return 0; |
| 54240 | } |
| 54241 | } |
| 54242 | sqlite3VdbeMemNulTerminate(pVal); |
| 54243 | }else{ |
| 54244 | assert( (pVal->flags&MEM_Blob)==0 ); |
| 54245 | sqlite3VdbeMemStringify(pVal, enc); |
| 54246 | assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); |
| 54247 | } |
| @@ -56152,13 +56682,14 @@ | |
| 56152 | */ |
| 56153 | for(i=0; i<db->nDb; i++){ |
| 56154 | Btree *pBt = db->aDb[i].pBt; |
| 56155 | if( sqlite3BtreeIsInTrans(pBt) ){ |
| 56156 | char const *zFile = sqlite3BtreeGetJournalname(pBt); |
| 56157 | if( zFile==0 || zFile[0]==0 ){ |
| 56158 | continue; /* Ignore TEMP and :memory: databases */ |
| 56159 | } |
| 56160 | if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
| 56161 | needSync = 1; |
| 56162 | } |
| 56163 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 56164 | offset += sqlite3Strlen30(zFile)+1; |
| @@ -57618,10 +58149,12 @@ | |
| 57618 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 57619 | */ |
| 57620 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 57621 | int rc; |
| 57622 | if( pStmt==0 ){ |
| 57623 | rc = SQLITE_OK; |
| 57624 | }else{ |
| 57625 | Vdbe *v = (Vdbe*)pStmt; |
| 57626 | sqlite3 *db = v->db; |
| 57627 | #if SQLITE_THREADSAFE |
| @@ -57694,11 +58227,11 @@ | |
| 57694 | Mem *p = (Mem*)pVal; |
| 57695 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 57696 | sqlite3VdbeMemExpandBlob(p); |
| 57697 | p->flags &= ~MEM_Str; |
| 57698 | p->flags |= MEM_Blob; |
| 57699 | return p->z; |
| 57700 | }else{ |
| 57701 | return sqlite3_value_text(pVal); |
| 57702 | } |
| 57703 | } |
| 57704 | SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){ |
| @@ -58048,10 +58581,16 @@ | |
| 58048 | } |
| 58049 | |
| 58050 | /* |
| 58051 | ** Extract the user data from a sqlite3_context structure and return a |
| 58052 | ** pointer to it. |
| 58053 | */ |
| 58054 | SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
| 58055 | assert( p && p->pFunc ); |
| 58056 | return p->s.db; |
| 58057 | } |
| @@ -58257,12 +58796,11 @@ | |
| 58257 | ** sqlite3_column_text() |
| 58258 | ** sqlite3_column_text16() |
| 58259 | ** sqlite3_column_real() |
| 58260 | ** sqlite3_column_bytes() |
| 58261 | ** sqlite3_column_bytes16() |
| 58262 | ** |
| 58263 | ** But not for sqlite3_column_blob(), which never calls malloc(). |
| 58264 | */ |
| 58265 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 58266 | { |
| 58267 | /* If malloc() failed during an encoding conversion within an |
| 58268 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| @@ -58526,10 +59064,16 @@ | |
| 58526 | pVar->flags = MEM_Null; |
| 58527 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 58528 | |
| 58529 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 58530 | ** binding a new value to this variable invalidates the current query plan. |
| 58531 | */ |
| 58532 | if( p->isPrepareV2 && |
| 58533 | ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) |
| 58534 | ){ |
| 58535 | p->expired = 1; |
| @@ -59023,10 +59567,21 @@ | |
| 59023 | ** of the code in this file is, therefore, important. See other comments |
| 59024 | ** in this file for details. If in doubt, do not deviate from existing |
| 59025 | ** commenting and indentation practices when changing or adding code. |
| 59026 | */ |
| 59027 | |
| 59028 | /* |
| 59029 | ** The following global variable is incremented every time a cursor |
| 59030 | ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test |
| 59031 | ** procedures use this information to make sure that indices are |
| 59032 | ** working correctly. This variable has no function other than to |
| @@ -60132,38 +60687,44 @@ | |
| 60132 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
| 60133 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 60134 | assert( pOp->p2>0 ); |
| 60135 | assert( pOp->p2<=p->nMem ); |
| 60136 | pOut = &aMem[pOp->p2]; |
| 60137 | sqlite3VdbeMemReleaseExternal(pOut); |
| 60138 | pOut->flags = MEM_Int; |
| 60139 | } |
| 60140 | |
| 60141 | /* Sanity checking on other operands */ |
| 60142 | #ifdef SQLITE_DEBUG |
| 60143 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 60144 | assert( pOp->p1>0 ); |
| 60145 | assert( pOp->p1<=p->nMem ); |
| 60146 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 60147 | } |
| 60148 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 60149 | assert( pOp->p2>0 ); |
| 60150 | assert( pOp->p2<=p->nMem ); |
| 60151 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 60152 | } |
| 60153 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 60154 | assert( pOp->p3>0 ); |
| 60155 | assert( pOp->p3<=p->nMem ); |
| 60156 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 60157 | } |
| 60158 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 60159 | assert( pOp->p2>0 ); |
| 60160 | assert( pOp->p2<=p->nMem ); |
| 60161 | } |
| 60162 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 60163 | assert( pOp->p3>0 ); |
| 60164 | assert( pOp->p3<=p->nMem ); |
| 60165 | } |
| 60166 | #endif |
| 60167 | |
| 60168 | switch( pOp->opcode ){ |
| 60169 | |
| @@ -60221,10 +60782,11 @@ | |
| 60221 | ** and then jump to address P2. |
| 60222 | */ |
| 60223 | case OP_Gosub: { /* jump, in1 */ |
| 60224 | pIn1 = &aMem[pOp->p1]; |
| 60225 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 60226 | pIn1->flags = MEM_Int; |
| 60227 | pIn1->u.i = pc; |
| 60228 | REGISTER_TRACE(pOp->p1, pIn1); |
| 60229 | pc = pOp->p2 - 1; |
| 60230 | break; |
| @@ -60428,15 +60990,11 @@ | |
| 60428 | |
| 60429 | |
| 60430 | /* Opcode: Blob P1 P2 * P4 |
| 60431 | ** |
| 60432 | ** P4 points to a blob of data P1 bytes long. Store this |
| 60433 | ** blob in register P2. This instruction is not coded directly |
| 60434 | ** by the compiler. Instead, the compiler layer specifies |
| 60435 | ** an OP_HexBlob opcode, with the hex string representation of |
| 60436 | ** the blob as P4. This opcode is transformed to an OP_Blob |
| 60437 | ** the first time it is executed. |
| 60438 | */ |
| 60439 | case OP_Blob: { /* out2-prerelease */ |
| 60440 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
| 60441 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 60442 | pOut->enc = encoding; |
| @@ -60490,10 +61048,12 @@ | |
| 60490 | pIn1 = &aMem[u.ac.p1]; |
| 60491 | pOut = &aMem[u.ac.p2]; |
| 60492 | while( u.ac.n-- ){ |
| 60493 | assert( pOut<=&aMem[p->nMem] ); |
| 60494 | assert( pIn1<=&aMem[p->nMem] ); |
| 60495 | u.ac.zMalloc = pOut->zMalloc; |
| 60496 | pOut->zMalloc = 0; |
| 60497 | sqlite3VdbeMemMove(pOut, pIn1); |
| 60498 | pIn1->zMalloc = u.ac.zMalloc; |
| 60499 | REGISTER_TRACE(u.ac.p2++, pOut); |
| @@ -60535,10 +61095,13 @@ | |
| 60535 | case OP_SCopy: { /* in1, out2 */ |
| 60536 | pIn1 = &aMem[pOp->p1]; |
| 60537 | pOut = &aMem[pOp->p2]; |
| 60538 | assert( pOut!=pIn1 ); |
| 60539 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 60540 | REGISTER_TRACE(pOp->p2, pOut); |
| 60541 | break; |
| 60542 | } |
| 60543 | |
| 60544 | /* Opcode: ResultRow P1 P2 * * * |
| @@ -60595,10 +61158,14 @@ | |
| 60595 | ** and have an assigned type. The results are de-ephemeralized as |
| 60596 | ** as side effect. |
| 60597 | */ |
| 60598 | u.ad.pMem = p->pResultSet = &aMem[pOp->p1]; |
| 60599 | for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){ |
| 60600 | sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]); |
| 60601 | sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]); |
| 60602 | REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]); |
| 60603 | } |
| 60604 | if( db->mallocFailed ) goto no_mem; |
| @@ -60826,16 +61393,21 @@ | |
| 60826 | #endif /* local variables moved into u.ag */ |
| 60827 | |
| 60828 | u.ag.n = pOp->p5; |
| 60829 | u.ag.apVal = p->apArg; |
| 60830 | assert( u.ag.apVal || u.ag.n==0 ); |
| 60831 | |
| 60832 | assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) ); |
| 60833 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n ); |
| 60834 | u.ag.pArg = &aMem[pOp->p2]; |
| 60835 | for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){ |
| 60836 | u.ag.apVal[u.ag.i] = u.ag.pArg; |
| 60837 | sqlite3VdbeMemStoreType(u.ag.pArg); |
| 60838 | REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg); |
| 60839 | } |
| 60840 | |
| 60841 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| @@ -60845,12 +61417,10 @@ | |
| 60845 | }else{ |
| 60846 | u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
| 60847 | u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc; |
| 60848 | } |
| 60849 | |
| 60850 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 60851 | pOut = &aMem[pOp->p3]; |
| 60852 | u.ag.ctx.s.flags = MEM_Null; |
| 60853 | u.ag.ctx.s.db = db; |
| 60854 | u.ag.ctx.s.xDel = 0; |
| 60855 | u.ag.ctx.s.zMalloc = 0; |
| 60856 | |
| @@ -60866,11 +61436,11 @@ | |
| 60866 | assert( pOp>aOp ); |
| 60867 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 60868 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 60869 | u.ag.ctx.pColl = pOp[-1].p4.pColl; |
| 60870 | } |
| 60871 | (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); |
| 60872 | if( db->mallocFailed ){ |
| 60873 | /* Even though a malloc() has failed, the implementation of the |
| 60874 | ** user function may have called an sqlite3_result_XXX() function |
| 60875 | ** to return a value. The following call releases any resources |
| 60876 | ** associated with such a value. |
| @@ -60918,11 +61488,11 @@ | |
| 60918 | ** If either input is NULL, the result is NULL. |
| 60919 | */ |
| 60920 | /* Opcode: ShiftLeft P1 P2 P3 * * |
| 60921 | ** |
| 60922 | ** Shift the integer value in register P2 to the left by the |
| 60923 | ** number of bits specified by the integer in regiser P1. |
| 60924 | ** Store the result in register P3. |
| 60925 | ** If either input is NULL, the result is NULL. |
| 60926 | */ |
| 60927 | /* Opcode: ShiftRight P1 P2 P3 * * |
| 60928 | ** |
| @@ -60968,10 +61538,11 @@ | |
| 60968 | ** |
| 60969 | ** To force any register to be an integer, just add 0. |
| 60970 | */ |
| 60971 | case OP_AddImm: { /* in1 */ |
| 60972 | pIn1 = &aMem[pOp->p1]; |
| 60973 | sqlite3VdbeMemIntegerify(pIn1); |
| 60974 | pIn1->u.i += pOp->p2; |
| 60975 | break; |
| 60976 | } |
| 60977 | |
| @@ -60982,10 +61553,11 @@ | |
| 60982 | ** without data loss, then jump immediately to P2, or if P2==0 |
| 60983 | ** raise an SQLITE_MISMATCH exception. |
| 60984 | */ |
| 60985 | case OP_MustBeInt: { /* jump, in1 */ |
| 60986 | pIn1 = &aMem[pOp->p1]; |
| 60987 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 60988 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 60989 | if( pOp->p2==0 ){ |
| 60990 | rc = SQLITE_MISMATCH; |
| 60991 | goto abort_due_to_error; |
| @@ -61008,10 +61580,11 @@ | |
| 61008 | ** integers, for space efficiency, but after extraction we want them |
| 61009 | ** to have only a real value. |
| 61010 | */ |
| 61011 | case OP_RealAffinity: { /* in1 */ |
| 61012 | pIn1 = &aMem[pOp->p1]; |
| 61013 | if( pIn1->flags & MEM_Int ){ |
| 61014 | sqlite3VdbeMemRealify(pIn1); |
| 61015 | } |
| 61016 | break; |
| 61017 | } |
| @@ -61027,10 +61600,11 @@ | |
| 61027 | ** |
| 61028 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61029 | */ |
| 61030 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
| 61031 | pIn1 = &aMem[pOp->p1]; |
| 61032 | if( pIn1->flags & MEM_Null ) break; |
| 61033 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 61034 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 61035 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61036 | rc = ExpandBlob(pIn1); |
| @@ -61049,10 +61623,11 @@ | |
| 61049 | ** |
| 61050 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61051 | */ |
| 61052 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
| 61053 | pIn1 = &aMem[pOp->p1]; |
| 61054 | if( pIn1->flags & MEM_Null ) break; |
| 61055 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 61056 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61057 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
| 61058 | MemSetTypeFlag(pIn1, MEM_Blob); |
| @@ -61073,28 +61648,30 @@ | |
| 61073 | ** |
| 61074 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61075 | */ |
| 61076 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
| 61077 | pIn1 = &aMem[pOp->p1]; |
| 61078 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 61079 | sqlite3VdbeMemNumerify(pIn1); |
| 61080 | } |
| 61081 | break; |
| 61082 | } |
| 61083 | #endif /* SQLITE_OMIT_CAST */ |
| 61084 | |
| 61085 | /* Opcode: ToInt P1 * * * * |
| 61086 | ** |
| 61087 | ** Force the value in register P1 be an integer. If |
| 61088 | ** The value is currently a real number, drop its fractional part. |
| 61089 | ** If the value is text or blob, try to convert it to an integer using the |
| 61090 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 61091 | ** |
| 61092 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61093 | */ |
| 61094 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
| 61095 | pIn1 = &aMem[pOp->p1]; |
| 61096 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61097 | sqlite3VdbeMemIntegerify(pIn1); |
| 61098 | } |
| 61099 | break; |
| 61100 | } |
| @@ -61109,10 +61686,11 @@ | |
| 61109 | ** |
| 61110 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61111 | */ |
| 61112 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
| 61113 | pIn1 = &aMem[pOp->p1]; |
| 61114 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61115 | sqlite3VdbeMemRealify(pIn1); |
| 61116 | } |
| 61117 | break; |
| 61118 | } |
| @@ -61123,11 +61701,11 @@ | |
| 61123 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 61124 | ** jump to address P2. |
| 61125 | ** |
| 61126 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 61127 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 61128 | ** bit is clear then fall thru if either operand is NULL. |
| 61129 | ** |
| 61130 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 61131 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 61132 | ** to coerce both inputs according to this affinity before the |
| 61133 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| @@ -61203,10 +61781,12 @@ | |
| 61203 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
| 61204 | #endif /* local variables moved into u.ai */ |
| 61205 | |
| 61206 | pIn1 = &aMem[pOp->p1]; |
| 61207 | pIn3 = &aMem[pOp->p3]; |
| 61208 | u.ai.flags1 = pIn1->flags; |
| 61209 | u.ai.flags3 = pIn3->flags; |
| 61210 | if( (pIn1->flags | pIn3->flags)&MEM_Null ){ |
| 61211 | /* One or both operands are NULL */ |
| 61212 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| @@ -61253,10 +61833,11 @@ | |
| 61253 | default: u.ai.res = u.ai.res>=0; break; |
| 61254 | } |
| 61255 | |
| 61256 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 61257 | pOut = &aMem[pOp->p2]; |
| 61258 | MemSetTypeFlag(pOut, MEM_Int); |
| 61259 | pOut->u.i = u.ai.res; |
| 61260 | REGISTER_TRACE(pOp->p2, pOut); |
| 61261 | }else if( u.ai.res ){ |
| 61262 | pc = pOp->p2-1; |
| @@ -61284,12 +61865,12 @@ | |
| 61284 | break; |
| 61285 | } |
| 61286 | |
| 61287 | /* Opcode: Compare P1 P2 P3 P4 * |
| 61288 | ** |
| 61289 | ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this |
| 61290 | ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 61291 | ** the comparison for use by the next OP_Jump instruct. |
| 61292 | ** |
| 61293 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 61294 | ** orders for the comparison. The permutation applies to registers |
| 61295 | ** only. The KeyInfo elements are used sequentially. |
| @@ -61327,10 +61908,12 @@ | |
| 61327 | assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 ); |
| 61328 | } |
| 61329 | #endif /* SQLITE_DEBUG */ |
| 61330 | for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){ |
| 61331 | u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i; |
| 61332 | REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]); |
| 61333 | REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]); |
| 61334 | assert( u.aj.i<u.aj.pKeyInfo->nField ); |
| 61335 | u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i]; |
| 61336 | u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i]; |
| @@ -61558,10 +62141,11 @@ | |
| 61558 | u.am.pC = 0; |
| 61559 | memset(&u.am.sMem, 0, sizeof(u.am.sMem)); |
| 61560 | assert( u.am.p1<p->nCursor ); |
| 61561 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 61562 | u.am.pDest = &aMem[pOp->p3]; |
| 61563 | MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 61564 | u.am.zRec = 0; |
| 61565 | |
| 61566 | /* This block sets the variable u.am.payloadSize to be the total number of |
| 61567 | ** bytes in the record. |
| @@ -61605,10 +62189,11 @@ | |
| 61605 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 61606 | } |
| 61607 | }else if( u.am.pC->pseudoTableReg>0 ){ |
| 61608 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 61609 | assert( u.am.pReg->flags & MEM_Blob ); |
| 61610 | u.am.payloadSize = u.am.pReg->n; |
| 61611 | u.am.zRec = u.am.pReg->z; |
| 61612 | u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr; |
| 61613 | assert( u.am.payloadSize==0 || u.am.zRec!=0 ); |
| 61614 | }else{ |
| @@ -61829,25 +62414,24 @@ | |
| 61829 | assert( u.an.zAffinity!=0 ); |
| 61830 | assert( u.an.zAffinity[pOp->p2]==0 ); |
| 61831 | pIn1 = &aMem[pOp->p1]; |
| 61832 | while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){ |
| 61833 | assert( pIn1 <= &p->aMem[p->nMem] ); |
| 61834 | ExpandBlob(pIn1); |
| 61835 | applyAffinity(pIn1, u.an.cAff, encoding); |
| 61836 | pIn1++; |
| 61837 | } |
| 61838 | break; |
| 61839 | } |
| 61840 | |
| 61841 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
| 61842 | ** |
| 61843 | ** Convert P2 registers beginning with P1 into a single entry |
| 61844 | ** suitable for use as a data record in a database table or as a key |
| 61845 | ** in an index. The details of the format are irrelevant as long as |
| 61846 | ** the OP_Column opcode can decode the record later. |
| 61847 | ** Refer to source code comments for the details of the record |
| 61848 | ** format. |
| 61849 | ** |
| 61850 | ** P4 may be a string that is P2 characters long. The nth character of the |
| 61851 | ** string indicates the column affinity that should be used for the nth |
| 61852 | ** field of the index key. |
| 61853 | ** |
| @@ -61899,16 +62483,23 @@ | |
| 61899 | assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 ); |
| 61900 | u.ao.pData0 = &aMem[u.ao.nField]; |
| 61901 | u.ao.nField = pOp->p2; |
| 61902 | u.ao.pLast = &u.ao.pData0[u.ao.nField-1]; |
| 61903 | u.ao.file_format = p->minWriteFileFormat; |
| 61904 | |
| 61905 | /* Loop through the elements that will make up the record to figure |
| 61906 | ** out how much space is required for the new record. |
| 61907 | */ |
| 61908 | for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ |
| 61909 | if( u.ao.zAffinity ){ |
| 61910 | applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding); |
| 61911 | } |
| 61912 | if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){ |
| 61913 | sqlite3VdbeMemExpandBlob(u.ao.pRec); |
| 61914 | } |
| @@ -61938,12 +62529,10 @@ | |
| 61938 | /* Make sure the output register has a buffer large enough to store |
| 61939 | ** the new record. The output register (pOp->p3) is not allowed to |
| 61940 | ** be one of the input registers (because the following call to |
| 61941 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 61942 | */ |
| 61943 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 61944 | pOut = &aMem[pOp->p3]; |
| 61945 | if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){ |
| 61946 | goto no_mem; |
| 61947 | } |
| 61948 | u.ao.zNewRecord = (u8 *)pOut->z; |
| 61949 | |
| @@ -62112,10 +62701,11 @@ | |
| 62112 | } |
| 62113 | } |
| 62114 | if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
| 62115 | sqlite3ExpirePreparedStatements(db); |
| 62116 | sqlite3ResetInternalSchema(db, 0); |
| 62117 | } |
| 62118 | } |
| 62119 | |
| 62120 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 62121 | ** savepoints nested inside of the savepoint being operated on. */ |
| @@ -62502,10 +63092,12 @@ | |
| 62502 | } |
| 62503 | if( pOp->p5 ){ |
| 62504 | assert( u.aw.p2>0 ); |
| 62505 | assert( u.aw.p2<=p->nMem ); |
| 62506 | pIn2 = &aMem[u.aw.p2]; |
| 62507 | sqlite3VdbeMemIntegerify(pIn2); |
| 62508 | u.aw.p2 = (int)pIn2->u.i; |
| 62509 | /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and |
| 62510 | ** that opcode will always set the u.aw.p2 value to 2 or more or else fail. |
| 62511 | ** If there were a failure, the prepared statement would have halted |
| @@ -62524,10 +63116,11 @@ | |
| 62524 | } |
| 62525 | assert( pOp->p1>=0 ); |
| 62526 | u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1); |
| 62527 | if( u.aw.pCur==0 ) goto no_mem; |
| 62528 | u.aw.pCur->nullRow = 1; |
| 62529 | rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor); |
| 62530 | u.aw.pCur->pKeyInfo = u.aw.pKeyInfo; |
| 62531 | |
| 62532 | /* Since it performs no memory allocation or IO, the only values that |
| 62533 | ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. |
| @@ -62576,11 +63169,11 @@ | |
| 62576 | case OP_OpenAutoindex: |
| 62577 | case OP_OpenEphemeral: { |
| 62578 | #if 0 /* local variables moved into u.ax */ |
| 62579 | VdbeCursor *pCx; |
| 62580 | #endif /* local variables moved into u.ax */ |
| 62581 | static const int openFlags = |
| 62582 | SQLITE_OPEN_READWRITE | |
| 62583 | SQLITE_OPEN_CREATE | |
| 62584 | SQLITE_OPEN_EXCLUSIVE | |
| 62585 | SQLITE_OPEN_DELETEONCLOSE | |
| 62586 | SQLITE_OPEN_TRANSIENT_DB; |
| @@ -62587,25 +63180,25 @@ | |
| 62587 | |
| 62588 | assert( pOp->p1>=0 ); |
| 62589 | u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 62590 | if( u.ax.pCx==0 ) goto no_mem; |
| 62591 | u.ax.pCx->nullRow = 1; |
| 62592 | rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, |
| 62593 | &u.ax.pCx->pBt); |
| 62594 | if( rc==SQLITE_OK ){ |
| 62595 | rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1); |
| 62596 | } |
| 62597 | if( rc==SQLITE_OK ){ |
| 62598 | /* If a transient index is required, create it by calling |
| 62599 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 62600 | ** opening it. If a transient table is required, just use the |
| 62601 | ** automatically created table with root-page 1 (an INTKEY table). |
| 62602 | */ |
| 62603 | if( pOp->p4.pKeyInfo ){ |
| 62604 | int pgno; |
| 62605 | assert( pOp->p4type==P4_KEYINFO ); |
| 62606 | rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_ZERODATA); |
| 62607 | if( rc==SQLITE_OK ){ |
| 62608 | assert( pgno==MASTER_ROOT+1 ); |
| 62609 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1, |
| 62610 | (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor); |
| 62611 | u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo; |
| @@ -62615,10 +63208,11 @@ | |
| 62615 | }else{ |
| 62616 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor); |
| 62617 | u.ax.pCx->isTable = 1; |
| 62618 | } |
| 62619 | } |
| 62620 | u.ax.pCx->isIndex = !u.ax.pCx->isTable; |
| 62621 | break; |
| 62622 | } |
| 62623 | |
| 62624 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| @@ -62734,10 +63328,11 @@ | |
| 62734 | assert( u.az.pC!=0 ); |
| 62735 | assert( u.az.pC->pseudoTableReg==0 ); |
| 62736 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 62737 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 62738 | assert( OP_SeekGt == OP_SeekLt+3 ); |
| 62739 | if( u.az.pC->pCursor!=0 ){ |
| 62740 | u.az.oc = pOp->opcode; |
| 62741 | u.az.pC->nullRow = 0; |
| 62742 | if( u.az.pC->isTable ){ |
| 62743 | /* The input value in P3 might be of any type: integer, real, string, |
| @@ -62816,10 +63411,13 @@ | |
| 62816 | assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY ); |
| 62817 | assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 ); |
| 62818 | assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 ); |
| 62819 | |
| 62820 | u.az.r.aMem = &aMem[pOp->p3]; |
| 62821 | ExpandBlob(u.az.r.aMem); |
| 62822 | rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res); |
| 62823 | if( rc!=SQLITE_OK ){ |
| 62824 | goto abort_due_to_error; |
| 62825 | } |
| @@ -62944,15 +63542,18 @@ | |
| 62944 | assert( u.bb.pC->isTable==0 ); |
| 62945 | if( pOp->p4.i>0 ){ |
| 62946 | u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo; |
| 62947 | u.bb.r.nField = (u16)pOp->p4.i; |
| 62948 | u.bb.r.aMem = pIn3; |
| 62949 | u.bb.r.flags = UNPACKED_PREFIX_MATCH; |
| 62950 | u.bb.pIdxKey = &u.bb.r; |
| 62951 | }else{ |
| 62952 | assert( pIn3->flags & MEM_Blob ); |
| 62953 | ExpandBlob(pIn3); |
| 62954 | u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z, |
| 62955 | u.bb.aTempRec, sizeof(u.bb.aTempRec)); |
| 62956 | if( u.bb.pIdxKey==0 ){ |
| 62957 | goto no_mem; |
| 62958 | } |
| @@ -63043,10 +63644,13 @@ | |
| 63043 | /* Populate the index search key. */ |
| 63044 | u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo; |
| 63045 | u.bc.r.nField = u.bc.nField + 1; |
| 63046 | u.bc.r.flags = UNPACKED_PREFIX_SEARCH; |
| 63047 | u.bc.r.aMem = u.bc.aMx; |
| 63048 | |
| 63049 | /* Extract the value of u.bc.R from register P3. */ |
| 63050 | sqlite3VdbeMemIntegerify(pIn3); |
| 63051 | u.bc.R = pIn3->u.i; |
| 63052 | |
| @@ -63065,11 +63669,11 @@ | |
| 63065 | |
| 63066 | /* Opcode: NotExists P1 P2 P3 * * |
| 63067 | ** |
| 63068 | ** Use the content of register P3 as a integer key. If a record |
| 63069 | ** with that key does not exist in table of P1, then jump to P2. |
| 63070 | ** If the record does exist, then fall thru. The cursor is left |
| 63071 | ** pointing to the record if it exists. |
| 63072 | ** |
| 63073 | ** The difference between this operation and NotFound is that this |
| 63074 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 63075 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| @@ -63223,11 +63827,13 @@ | |
| 63223 | u.be.pMem = &u.be.pFrame->aMem[pOp->p3]; |
| 63224 | }else{ |
| 63225 | /* Assert that P3 is a valid memory cell. */ |
| 63226 | assert( pOp->p3<=p->nMem ); |
| 63227 | u.be.pMem = &aMem[pOp->p3]; |
| 63228 | } |
| 63229 | |
| 63230 | REGISTER_TRACE(pOp->p3, u.be.pMem); |
| 63231 | sqlite3VdbeMemIntegerify(u.be.pMem); |
| 63232 | assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 63233 | if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){ |
| @@ -63242,33 +63848,40 @@ | |
| 63242 | #endif |
| 63243 | |
| 63244 | sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0); |
| 63245 | } |
| 63246 | if( u.be.pC->useRandomRowid ){ |
| 63247 | /* IMPLEMENTATION-OF: R-48598-02938 If the largest ROWID is equal to the |
| 63248 | ** largest possible integer (9223372036854775807) then the database |
| 63249 | ** engine starts picking candidate ROWIDs at random until it finds one |
| 63250 | ** that is not previously used. |
| 63251 | */ |
| 63252 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 63253 | ** an AUTOINCREMENT table. */ |
| 63254 | u.be.v = db->lastRowid; |
| 63255 | u.be.cnt = 0; |
| 63256 | do{ |
| 63257 | if( u.be.cnt==0 && (u.be.v&0xffffff)==u.be.v ){ |
| 63258 | u.be.v++; |
| 63259 | }else{ |
| 63260 | sqlite3_randomness(sizeof(u.be.v), &u.be.v); |
| 63261 | if( u.be.cnt<5 ) u.be.v &= 0xffffff; |
| 63262 | } |
| 63263 | rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, 0, &u.be.res); |
| 63264 | u.be.cnt++; |
| 63265 | }while( u.be.cnt<100 && rc==SQLITE_OK && u.be.res==0 ); |
| 63266 | if( rc==SQLITE_OK && u.be.res==0 ){ |
| 63267 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 63268 | goto abort_due_to_error; |
| 63269 | } |
| 63270 | } |
| 63271 | u.be.pC->rowidIsValid = 0; |
| 63272 | u.be.pC->deferredMoveto = 0; |
| 63273 | u.be.pC->cacheStatus = CACHE_STALE; |
| 63274 | } |
| @@ -63334,10 +63947,11 @@ | |
| 63334 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
| 63335 | #endif /* local variables moved into u.bf */ |
| 63336 | |
| 63337 | u.bf.pData = &aMem[pOp->p2]; |
| 63338 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63339 | u.bf.pC = p->apCsr[pOp->p1]; |
| 63340 | assert( u.bf.pC!=0 ); |
| 63341 | assert( u.bf.pC->pCursor!=0 ); |
| 63342 | assert( u.bf.pC->pseudoTableReg==0 ); |
| 63343 | assert( u.bf.pC->isTable ); |
| @@ -63344,10 +63958,11 @@ | |
| 63344 | REGISTER_TRACE(pOp->p2, u.bf.pData); |
| 63345 | |
| 63346 | if( pOp->opcode==OP_Insert ){ |
| 63347 | u.bf.pKey = &aMem[pOp->p3]; |
| 63348 | assert( u.bf.pKey->flags & MEM_Int ); |
| 63349 | REGISTER_TRACE(pOp->p3, u.bf.pKey); |
| 63350 | u.bf.iKey = u.bf.pKey->u.i; |
| 63351 | }else{ |
| 63352 | assert( pOp->opcode==OP_InsertInt ); |
| 63353 | u.bf.iKey = pOp->p3; |
| @@ -63495,10 +64110,11 @@ | |
| 63495 | u32 n; |
| 63496 | i64 n64; |
| 63497 | #endif /* local variables moved into u.bh */ |
| 63498 | |
| 63499 | pOut = &aMem[pOp->p2]; |
| 63500 | |
| 63501 | /* Note that RowKey and RowData are really exactly the same instruction */ |
| 63502 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63503 | u.bh.pC = p->apCsr[pOp->p1]; |
| 63504 | assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey ); |
| @@ -63837,10 +64453,13 @@ | |
| 63837 | if( ALWAYS(u.bo.pCrsr!=0) ){ |
| 63838 | u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo; |
| 63839 | u.bo.r.nField = (u16)pOp->p3; |
| 63840 | u.bo.r.flags = 0; |
| 63841 | u.bo.r.aMem = &aMem[pOp->p2]; |
| 63842 | rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res); |
| 63843 | if( rc==SQLITE_OK && u.bo.res==0 ){ |
| 63844 | rc = sqlite3BtreeDelete(u.bo.pCrsr); |
| 63845 | } |
| 63846 | assert( u.bo.pC->deferredMoveto==0 ); |
| @@ -63921,10 +64540,11 @@ | |
| 63921 | #endif /* local variables moved into u.bq */ |
| 63922 | |
| 63923 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63924 | u.bq.pC = p->apCsr[pOp->p1]; |
| 63925 | assert( u.bq.pC!=0 ); |
| 63926 | if( ALWAYS(u.bq.pC->pCursor!=0) ){ |
| 63927 | assert( u.bq.pC->deferredMoveto==0 ); |
| 63928 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 63929 | assert( pOp->p4type==P4_INT32 ); |
| 63930 | u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo; |
| @@ -63933,10 +64553,13 @@ | |
| 63933 | u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 63934 | }else{ |
| 63935 | u.bq.r.flags = UNPACKED_IGNORE_ROWID; |
| 63936 | } |
| 63937 | u.bq.r.aMem = &aMem[pOp->p3]; |
| 63938 | rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res); |
| 63939 | if( pOp->opcode==OP_IdxLT ){ |
| 63940 | u.bq.res = -u.bq.res; |
| 63941 | }else{ |
| 63942 | assert( pOp->opcode==OP_IdxGE ); |
| @@ -64036,10 +64659,12 @@ | |
| 64036 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0) |
| 64037 | ); |
| 64038 | if( pOp->p3 ){ |
| 64039 | p->nChange += u.bs.nChange; |
| 64040 | if( pOp->p3>0 ){ |
| 64041 | aMem[pOp->p3].u.i += u.bs.nChange; |
| 64042 | } |
| 64043 | } |
| 64044 | break; |
| 64045 | } |
| @@ -64079,13 +64704,13 @@ | |
| 64079 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
| 64080 | u.bt.pDb = &db->aDb[pOp->p1]; |
| 64081 | assert( u.bt.pDb->pBt!=0 ); |
| 64082 | if( pOp->opcode==OP_CreateTable ){ |
| 64083 | /* u.bt.flags = BTREE_INTKEY; */ |
| 64084 | u.bt.flags = BTREE_LEAFDATA|BTREE_INTKEY; |
| 64085 | }else{ |
| 64086 | u.bt.flags = BTREE_ZERODATA; |
| 64087 | } |
| 64088 | rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags); |
| 64089 | pOut->u.i = u.bt.pgno; |
| 64090 | break; |
| 64091 | } |
| @@ -64410,10 +65035,11 @@ | |
| 64410 | void *t; /* Token identifying trigger */ |
| 64411 | #endif /* local variables moved into u.by */ |
| 64412 | |
| 64413 | u.by.pProgram = pOp->p4.pProgram; |
| 64414 | u.by.pRt = &aMem[pOp->p3]; |
| 64415 | assert( u.by.pProgram->nOp>0 ); |
| 64416 | |
| 64417 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 64418 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 64419 | ** is really a trigger, not a foreign key action, and the flag set |
| @@ -64583,10 +65209,11 @@ | |
| 64583 | for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent); |
| 64584 | u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1]; |
| 64585 | }else{ |
| 64586 | u.ca.pIn1 = &aMem[pOp->p1]; |
| 64587 | } |
| 64588 | sqlite3VdbeMemIntegerify(u.ca.pIn1); |
| 64589 | pIn2 = &aMem[pOp->p2]; |
| 64590 | sqlite3VdbeMemIntegerify(pIn2); |
| 64591 | if( u.ca.pIn1->u.i<pIn2->u.i){ |
| 64592 | u.ca.pIn1->u.i = pIn2->u.i; |
| @@ -64669,11 +65296,13 @@ | |
| 64669 | assert( u.cb.n>=0 ); |
| 64670 | u.cb.pRec = &aMem[pOp->p2]; |
| 64671 | u.cb.apVal = p->apArg; |
| 64672 | assert( u.cb.apVal || u.cb.n==0 ); |
| 64673 | for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){ |
| 64674 | u.cb.apVal[u.cb.i] = u.cb.pRec; |
| 64675 | sqlite3VdbeMemStoreType(u.cb.pRec); |
| 64676 | } |
| 64677 | u.cb.ctx.pFunc = pOp->p4.pFunc; |
| 64678 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 64679 | u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3]; |
| @@ -64689,11 +65318,11 @@ | |
| 64689 | assert( pOp>p->aOp ); |
| 64690 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 64691 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 64692 | u.cb.ctx.pColl = pOp[-1].p4.pColl; |
| 64693 | } |
| 64694 | (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); |
| 64695 | if( u.cb.ctx.isError ){ |
| 64696 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s)); |
| 64697 | rc = u.cb.ctx.isError; |
| 64698 | } |
| 64699 | sqlite3VdbeMemRelease(&u.cb.ctx.s); |
| @@ -65076,10 +65705,11 @@ | |
| 65076 | #endif /* local variables moved into u.ch */ |
| 65077 | |
| 65078 | u.ch.pQuery = &aMem[pOp->p3]; |
| 65079 | u.ch.pArgc = &u.ch.pQuery[1]; |
| 65080 | u.ch.pCur = p->apCsr[pOp->p1]; |
| 65081 | REGISTER_TRACE(pOp->p3, u.ch.pQuery); |
| 65082 | assert( u.ch.pCur->pVtabCursor ); |
| 65083 | u.ch.pVtabCursor = u.ch.pCur->pVtabCursor; |
| 65084 | u.ch.pVtab = u.ch.pVtabCursor->pVtab; |
| 65085 | u.ch.pModule = u.ch.pVtab->pModule; |
| @@ -65133,10 +65763,11 @@ | |
| 65133 | |
| 65134 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 65135 | assert( pCur->pVtabCursor ); |
| 65136 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65137 | u.ci.pDest = &aMem[pOp->p3]; |
| 65138 | if( pCur->nullRow ){ |
| 65139 | sqlite3VdbeMemSetNull(u.ci.pDest); |
| 65140 | break; |
| 65141 | } |
| 65142 | u.ci.pVtab = pCur->pVtabCursor->pVtab; |
| @@ -65235,14 +65866,16 @@ | |
| 65235 | #endif /* local variables moved into u.ck */ |
| 65236 | |
| 65237 | u.ck.pVtab = pOp->p4.pVtab->pVtab; |
| 65238 | u.ck.pName = &aMem[pOp->p1]; |
| 65239 | assert( u.ck.pVtab->pModule->xRename ); |
| 65240 | REGISTER_TRACE(pOp->p1, u.ck.pName); |
| 65241 | assert( u.ck.pName->flags & MEM_Str ); |
| 65242 | rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z); |
| 65243 | importVtabErrMsg(p, u.ck.pVtab); |
| 65244 | |
| 65245 | break; |
| 65246 | } |
| 65247 | #endif |
| 65248 | |
| @@ -65287,10 +65920,12 @@ | |
| 65287 | assert( pOp->p4type==P4_VTAB ); |
| 65288 | if( ALWAYS(u.cl.pModule->xUpdate) ){ |
| 65289 | u.cl.apArg = p->apArg; |
| 65290 | u.cl.pX = &aMem[pOp->p3]; |
| 65291 | for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){ |
| 65292 | sqlite3VdbeMemStoreType(u.cl.pX); |
| 65293 | u.cl.apArg[u.cl.i] = u.cl.pX; |
| 65294 | u.cl.pX++; |
| 65295 | } |
| 65296 | rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid); |
| @@ -66341,12 +66976,11 @@ | |
| 66341 | SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
| 66342 | return pJfd->pMethods==&MemJournalMethods; |
| 66343 | } |
| 66344 | |
| 66345 | /* |
| 66346 | ** Return the number of bytes required to store a MemJournal that uses vfs |
| 66347 | ** pVfs to create the underlying on-disk files. |
| 66348 | */ |
| 66349 | SQLITE_PRIVATE int sqlite3MemJournalSize(void){ |
| 66350 | return sizeof(MemJournal); |
| 66351 | } |
| 66352 | |
| @@ -69226,12 +69860,12 @@ | |
| 69226 | return eType; |
| 69227 | } |
| 69228 | #endif |
| 69229 | |
| 69230 | /* |
| 69231 | ** Generate code for scalar subqueries used as an expression |
| 69232 | ** and IN operators. Examples: |
| 69233 | ** |
| 69234 | ** (SELECT a FROM b) -- subquery |
| 69235 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 69236 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 69237 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
| @@ -69290,14 +69924,14 @@ | |
| 69290 | assert( testAddr>0 || pParse->db->mallocFailed ); |
| 69291 | } |
| 69292 | |
| 69293 | switch( pExpr->op ){ |
| 69294 | case TK_IN: { |
| 69295 | char affinity; |
| 69296 | KeyInfo keyInfo; |
| 69297 | int addr; /* Address of OP_OpenEphemeral instruction */ |
| 69298 | Expr *pLeft = pExpr->pLeft; |
| 69299 | |
| 69300 | if( rMayHaveNull ){ |
| 69301 | sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
| 69302 | } |
| 69303 | |
| @@ -69316,10 +69950,11 @@ | |
| 69316 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 69317 | ** is used. |
| 69318 | */ |
| 69319 | pExpr->iTable = pParse->nTab++; |
| 69320 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); |
| 69321 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 69322 | keyInfo.nField = 1; |
| 69323 | |
| 69324 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 69325 | /* Case 1: expr IN (SELECT ...) |
| @@ -69924,77 +70559,10 @@ | |
| 69924 | } |
| 69925 | return 0; |
| 69926 | } |
| 69927 | #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
| 69928 | |
| 69929 | /* |
| 69930 | ** If the last instruction coded is an ephemeral copy of any of |
| 69931 | ** the registers in the nReg registers beginning with iReg, then |
| 69932 | ** convert the last instruction from OP_SCopy to OP_Copy. |
| 69933 | */ |
| 69934 | SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ |
| 69935 | VdbeOp *pOp; |
| 69936 | Vdbe *v; |
| 69937 | |
| 69938 | assert( pParse->db->mallocFailed==0 ); |
| 69939 | v = pParse->pVdbe; |
| 69940 | assert( v!=0 ); |
| 69941 | pOp = sqlite3VdbeGetOp(v, -1); |
| 69942 | assert( pOp!=0 ); |
| 69943 | if( pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ |
| 69944 | pOp->opcode = OP_Copy; |
| 69945 | } |
| 69946 | } |
| 69947 | |
| 69948 | /* |
| 69949 | ** Generate code to store the value of the iAlias-th alias in register |
| 69950 | ** target. The first time this is called, pExpr is evaluated to compute |
| 69951 | ** the value of the alias. The value is stored in an auxiliary register |
| 69952 | ** and the number of that register is returned. On subsequent calls, |
| 69953 | ** the register number is returned without generating any code. |
| 69954 | ** |
| 69955 | ** Note that in order for this to work, code must be generated in the |
| 69956 | ** same order that it is executed. |
| 69957 | ** |
| 69958 | ** Aliases are numbered starting with 1. So iAlias is in the range |
| 69959 | ** of 1 to pParse->nAlias inclusive. |
| 69960 | ** |
| 69961 | ** pParse->aAlias[iAlias-1] records the register number where the value |
| 69962 | ** of the iAlias-th alias is stored. If zero, that means that the |
| 69963 | ** alias has not yet been computed. |
| 69964 | */ |
| 69965 | static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){ |
| 69966 | #if 0 |
| 69967 | sqlite3 *db = pParse->db; |
| 69968 | int iReg; |
| 69969 | if( pParse->nAliasAlloc<pParse->nAlias ){ |
| 69970 | pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias, |
| 69971 | sizeof(pParse->aAlias[0])*pParse->nAlias ); |
| 69972 | testcase( db->mallocFailed && pParse->nAliasAlloc>0 ); |
| 69973 | if( db->mallocFailed ) return 0; |
| 69974 | memset(&pParse->aAlias[pParse->nAliasAlloc], 0, |
| 69975 | (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0])); |
| 69976 | pParse->nAliasAlloc = pParse->nAlias; |
| 69977 | } |
| 69978 | assert( iAlias>0 && iAlias<=pParse->nAlias ); |
| 69979 | iReg = pParse->aAlias[iAlias-1]; |
| 69980 | if( iReg==0 ){ |
| 69981 | if( pParse->iCacheLevel>0 ){ |
| 69982 | iReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 69983 | }else{ |
| 69984 | iReg = ++pParse->nMem; |
| 69985 | sqlite3ExprCode(pParse, pExpr, iReg); |
| 69986 | pParse->aAlias[iAlias-1] = iReg; |
| 69987 | } |
| 69988 | } |
| 69989 | return iReg; |
| 69990 | #else |
| 69991 | UNUSED_PARAMETER(iAlias); |
| 69992 | return sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 69993 | #endif |
| 69994 | } |
| 69995 | |
| 69996 | /* |
| 69997 | ** Generate code into the current Vdbe to evaluate the given |
| 69998 | ** expression. Attempt to store the results in register "target". |
| 69999 | ** Return the register where results are stored. |
| 70000 | ** |
| @@ -70099,11 +70667,11 @@ | |
| 70099 | case TK_REGISTER: { |
| 70100 | inReg = pExpr->iTable; |
| 70101 | break; |
| 70102 | } |
| 70103 | case TK_AS: { |
| 70104 | inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target); |
| 70105 | break; |
| 70106 | } |
| 70107 | #ifndef SQLITE_OMIT_CAST |
| 70108 | case TK_CAST: { |
| 70109 | /* Expressions of the form: CAST(pLeft AS token) */ |
| @@ -70531,10 +71099,15 @@ | |
| 70531 | testcase( regFree1==0 ); |
| 70532 | cacheX.op = TK_REGISTER; |
| 70533 | opCompare.op = TK_EQ; |
| 70534 | opCompare.pLeft = &cacheX; |
| 70535 | pTest = &opCompare; |
| 70536 | } |
| 70537 | for(i=0; i<nExpr; i=i+2){ |
| 70538 | sqlite3ExprCachePush(pParse); |
| 70539 | if( pX ){ |
| 70540 | assert( pTest!=0 ); |
| @@ -70624,14 +71197,18 @@ | |
| 70624 | */ |
| 70625 | SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
| 70626 | int inReg; |
| 70627 | |
| 70628 | assert( target>0 && target<=pParse->nMem ); |
| 70629 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 70630 | assert( pParse->pVdbe || pParse->db->mallocFailed ); |
| 70631 | if( inReg!=target && pParse->pVdbe ){ |
| 70632 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
| 70633 | } |
| 70634 | return target; |
| 70635 | } |
| 70636 | |
| 70637 | /* |
| @@ -70800,23 +71377,18 @@ | |
| 70800 | ){ |
| 70801 | struct ExprList_item *pItem; |
| 70802 | int i, n; |
| 70803 | assert( pList!=0 ); |
| 70804 | assert( target>0 ); |
| 70805 | n = pList->nExpr; |
| 70806 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 70807 | if( pItem->iAlias ){ |
| 70808 | int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i); |
| 70809 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 70810 | if( iReg!=target+i ){ |
| 70811 | sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i); |
| 70812 | } |
| 70813 | }else{ |
| 70814 | sqlite3ExprCode(pParse, pItem->pExpr, target+i); |
| 70815 | } |
| 70816 | if( doHardCopy && !pParse->db->mallocFailed ){ |
| 70817 | sqlite3ExprHardCopy(pParse, target, n); |
| 70818 | } |
| 70819 | } |
| 70820 | return n; |
| 70821 | } |
| 70822 | |
| @@ -71794,10 +72366,15 @@ | |
| 71794 | if( pTrig->pSchema==pTempSchema ){ |
| 71795 | zWhere = whereOrName(db, zWhere, pTrig->zName); |
| 71796 | } |
| 71797 | } |
| 71798 | } |
| 71799 | return zWhere; |
| 71800 | } |
| 71801 | |
| 71802 | /* |
| 71803 | ** Generate code to drop and reload the internal representation of table |
| @@ -72401,11 +72978,12 @@ | |
| 72401 | int iIdxCur; /* Cursor open on index being analyzed */ |
| 72402 | Vdbe *v; /* The virtual machine being built up */ |
| 72403 | int i; /* Loop counter */ |
| 72404 | int topOfLoop; /* The top of the loop */ |
| 72405 | int endOfLoop; /* The end of the loop */ |
| 72406 | int addr; /* The address of an instruction */ |
| 72407 | int iDb; /* Index of database containing pTab */ |
| 72408 | int regTabname = iMem++; /* Register containing table name */ |
| 72409 | int regIdxname = iMem++; /* Register containing index name */ |
| 72410 | int regSampleno = iMem++; /* Register containing next sample number */ |
| 72411 | int regCol = iMem++; /* Content of a column analyzed table */ |
| @@ -72420,12 +72998,19 @@ | |
| 72420 | int regLast = iMem++; /* Index of last sample to record */ |
| 72421 | int regFirst = iMem++; /* Index of first sample to record */ |
| 72422 | #endif |
| 72423 | |
| 72424 | v = sqlite3GetVdbe(pParse); |
| 72425 | if( v==0 || NEVER(pTab==0) || pTab->pIndex==0 ){ |
| 72426 | /* Do no analysis for tables that have no indices */ |
| 72427 | return; |
| 72428 | } |
| 72429 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 72430 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 72431 | assert( iDb>=0 ); |
| @@ -72438,10 +73023,11 @@ | |
| 72438 | |
| 72439 | /* Establish a read-lock on the table at the shared-cache level. */ |
| 72440 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 72441 | |
| 72442 | iIdxCur = pParse->nTab++; |
| 72443 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 72444 | int nCol = pIdx->nColumn; |
| 72445 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 72446 | |
| 72447 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| @@ -72452,14 +73038,11 @@ | |
| 72452 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 72453 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
| 72454 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| 72455 | VdbeComment((v, "%s", pIdx->zName)); |
| 72456 | |
| 72457 | /* Populate the registers containing the table and index names. */ |
| 72458 | if( pTab->pIndex==pIdx ){ |
| 72459 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 72460 | } |
| 72461 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 72462 | |
| 72463 | #ifdef SQLITE_ENABLE_STAT2 |
| 72464 | |
| 72465 | /* If this iteration of the loop is generating code to analyze the |
| @@ -72590,12 +73173,14 @@ | |
| 72590 | ** |
| 72591 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 72592 | ** If K>0 then it is always the case the D>0 so division by zero |
| 72593 | ** is never possible. |
| 72594 | */ |
| 72595 | addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 72596 | sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 72597 | for(i=0; i<nCol; i++){ |
| 72598 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 72599 | sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 72600 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 72601 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| @@ -72605,17 +73190,39 @@ | |
| 72605 | } |
| 72606 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 72607 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 72608 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 72609 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 72610 | sqlite3VdbeJumpHere(v, addr); |
| 72611 | } |
| 72612 | } |
| 72613 | |
| 72614 | /* |
| 72615 | ** Generate code that will cause the most recent index analysis to |
| 72616 | ** be laoded into internal hash tables where is can be used. |
| 72617 | */ |
| 72618 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 72619 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 72620 | if( v ){ |
| 72621 | sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
| @@ -72741,37 +73348,50 @@ | |
| 72741 | |
| 72742 | /* |
| 72743 | ** This callback is invoked once for each index when reading the |
| 72744 | ** sqlite_stat1 table. |
| 72745 | ** |
| 72746 | ** argv[0] = name of the index |
| 72747 | ** argv[1] = results of analysis - on integer for each column |
| 72748 | */ |
| 72749 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 72750 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 72751 | Index *pIndex; |
| 72752 | int i, c; |
| 72753 | unsigned int v; |
| 72754 | const char *z; |
| 72755 | |
| 72756 | assert( argc==2 ); |
| 72757 | UNUSED_PARAMETER2(NotUsed, argc); |
| 72758 | |
| 72759 | if( argv==0 || argv[0]==0 || argv[1]==0 ){ |
| 72760 | return 0; |
| 72761 | } |
| 72762 | pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase); |
| 72763 | if( pIndex==0 ){ |
| 72764 | return 0; |
| 72765 | } |
| 72766 | z = argv[1]; |
| 72767 | for(i=0; *z && i<=pIndex->nColumn; i++){ |
| 72768 | v = 0; |
| 72769 | while( (c=z[0])>='0' && c<='9' ){ |
| 72770 | v = v*10 + c - '0'; |
| 72771 | z++; |
| 72772 | } |
| 72773 | pIndex->aiRowEst[i] = v; |
| 72774 | if( *z==' ' ) z++; |
| 72775 | } |
| 72776 | return 0; |
| 72777 | } |
| @@ -72843,11 +73463,11 @@ | |
| 72843 | return SQLITE_ERROR; |
| 72844 | } |
| 72845 | |
| 72846 | /* Load new statistics out of the sqlite_stat1 table */ |
| 72847 | zSql = sqlite3MPrintf(db, |
| 72848 | "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 72849 | if( zSql==0 ){ |
| 72850 | rc = SQLITE_NOMEM; |
| 72851 | }else{ |
| 72852 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 72853 | sqlite3DbFree(db, zSql); |
| @@ -73060,13 +73680,12 @@ | |
| 73060 | |
| 73061 | /* Open the database file. If the btree is successfully opened, use |
| 73062 | ** it to obtain the database schema. At this point the schema may |
| 73063 | ** or may not be initialised. |
| 73064 | */ |
| 73065 | rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, |
| 73066 | db->openFlags | SQLITE_OPEN_MAIN_DB, |
| 73067 | &aNew->pBt); |
| 73068 | db->nDb++; |
| 73069 | if( rc==SQLITE_CONSTRAINT ){ |
| 73070 | rc = SQLITE_ERROR; |
| 73071 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| 73072 | }else if( rc==SQLITE_OK ){ |
| @@ -73303,11 +73922,12 @@ | |
| 73303 | 0, /* pNext */ |
| 73304 | detachFunc, /* xFunc */ |
| 73305 | 0, /* xStep */ |
| 73306 | 0, /* xFinalize */ |
| 73307 | "sqlite_detach", /* zName */ |
| 73308 | 0 /* pHash */ |
| 73309 | }; |
| 73310 | codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); |
| 73311 | } |
| 73312 | |
| 73313 | /* |
| @@ -73324,11 +73944,12 @@ | |
| 73324 | 0, /* pNext */ |
| 73325 | attachFunc, /* xFunc */ |
| 73326 | 0, /* xStep */ |
| 73327 | 0, /* xFinalize */ |
| 73328 | "sqlite_attach", /* zName */ |
| 73329 | 0 /* pHash */ |
| 73330 | }; |
| 73331 | codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); |
| 73332 | } |
| 73333 | #endif /* SQLITE_OMIT_ATTACH */ |
| 73334 | |
| @@ -74453,12 +75074,13 @@ | |
| 74453 | ** set to the index of the database that the table or view is to be |
| 74454 | ** created in. |
| 74455 | */ |
| 74456 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 74457 | if( iDb<0 ) return; |
| 74458 | if( !OMIT_TEMPDB && isTemp && iDb>1 ){ |
| 74459 | /* If creating a temp table, the name may not be qualified */ |
| 74460 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 74461 | return; |
| 74462 | } |
| 74463 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 74464 | |
| @@ -74502,21 +75124,22 @@ | |
| 74502 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 74503 | ** and types will be used, so there is no need to test for namespace |
| 74504 | ** collisions. |
| 74505 | */ |
| 74506 | if( !IN_DECLARE_VTAB ){ |
| 74507 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 74508 | goto begin_table_error; |
| 74509 | } |
| 74510 | pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName); |
| 74511 | if( pTable ){ |
| 74512 | if( !noErr ){ |
| 74513 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 74514 | } |
| 74515 | goto begin_table_error; |
| 74516 | } |
| 74517 | if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){ |
| 74518 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 74519 | goto begin_table_error; |
| 74520 | } |
| 74521 | } |
| 74522 | |
| @@ -74529,10 +75152,11 @@ | |
| 74529 | } |
| 74530 | pTable->zName = zName; |
| 74531 | pTable->iPKey = -1; |
| 74532 | pTable->pSchema = db->aDb[iDb].pSchema; |
| 74533 | pTable->nRef = 1; |
| 74534 | assert( pParse->pNewTable==0 ); |
| 74535 | pParse->pNewTable = pTable; |
| 74536 | |
| 74537 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 74538 | ** then record a pointer to this table in the main database structure |
| @@ -75375,16 +75999,14 @@ | |
| 75375 | sqlite3SelectDelete(db, pSelect); |
| 75376 | return; |
| 75377 | } |
| 75378 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
| 75379 | p = pParse->pNewTable; |
| 75380 | if( p==0 ){ |
| 75381 | sqlite3SelectDelete(db, pSelect); |
| 75382 | return; |
| 75383 | } |
| 75384 | assert( pParse->nErr==0 ); /* If sqlite3StartTable return non-NULL then |
| 75385 | ** there could not have been an error */ |
| 75386 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 75387 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 75388 | if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
| 75389 | && sqlite3FixSelect(&sFix, pSelect) |
| 75390 | ){ |
| @@ -76498,11 +77120,12 @@ | |
| 76498 | */ |
| 76499 | if( pTblName ){ |
| 76500 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 76501 | sqlite3ChangeCookie(pParse, iDb); |
| 76502 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, |
| 76503 | sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC); |
| 76504 | sqlite3VdbeAddOp1(v, OP_Expire, 0); |
| 76505 | } |
| 76506 | } |
| 76507 | |
| 76508 | /* When adding an index to the list of indices for a table, make |
| @@ -76559,18 +77182,18 @@ | |
| 76559 | ** are based on typical values found in actual indices. |
| 76560 | */ |
| 76561 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 76562 | unsigned *a = pIdx->aiRowEst; |
| 76563 | int i; |
| 76564 | assert( a!=0 ); |
| 76565 | a[0] = 1000000; |
| 76566 | for(i=pIdx->nColumn; i>=5; i--){ |
| 76567 | a[i] = 5; |
| 76568 | } |
| 76569 | while( i>=1 ){ |
| 76570 | a[i] = 11 - i; |
| 76571 | i--; |
| 76572 | } |
| 76573 | if( pIdx->onError!=OE_None ){ |
| 76574 | a[pIdx->nColumn] = 1; |
| 76575 | } |
| 76576 | } |
| @@ -76626,11 +77249,11 @@ | |
| 76626 | /* Generate code to remove the index and from the master table */ |
| 76627 | v = sqlite3GetVdbe(pParse); |
| 76628 | if( v ){ |
| 76629 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 76630 | sqlite3NestedParse(pParse, |
| 76631 | "DELETE FROM %Q.%s WHERE name=%Q", |
| 76632 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 76633 | pIndex->zName |
| 76634 | ); |
| 76635 | if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ |
| 76636 | sqlite3NestedParse(pParse, |
| @@ -77118,11 +77741,11 @@ | |
| 77118 | SQLITE_OPEN_CREATE | |
| 77119 | SQLITE_OPEN_EXCLUSIVE | |
| 77120 | SQLITE_OPEN_DELETEONCLOSE | |
| 77121 | SQLITE_OPEN_TEMP_DB; |
| 77122 | |
| 77123 | rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags, &pBt); |
| 77124 | if( rc!=SQLITE_OK ){ |
| 77125 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 77126 | "file for storing temporary tables"); |
| 77127 | pParse->rc = rc; |
| 77128 | return 1; |
| @@ -77775,11 +78398,11 @@ | |
| 77775 | ** If the SQLITE_PreferBuiltin flag is set, then search the built-in |
| 77776 | ** functions even if a prior app-defined function was found. And give |
| 77777 | ** priority to built-in functions. |
| 77778 | ** |
| 77779 | ** Except, if createFlag is true, that means that we are trying to |
| 77780 | ** install a new function. Whatever FuncDef structure is returned will |
| 77781 | ** have fields overwritten with new information appropriate for the |
| 77782 | ** new function. But the FuncDefs for built-in functions are read-only. |
| 77783 | ** So we must not search for built-ins when creating a new function. |
| 77784 | */ |
| 77785 | if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ |
| @@ -79956,14 +80579,14 @@ | |
| 79956 | if( caseSensitive ){ |
| 79957 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 79958 | }else{ |
| 79959 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 79960 | } |
| 79961 | sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0); |
| 79962 | sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0); |
| 79963 | sqlite3CreateFunc(db, "glob", 2, SQLITE_ANY, |
| 79964 | (struct compareInfo*)&globInfo, likeFunc, 0,0); |
| 79965 | setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 79966 | setLikeOptFlag(db, "like", |
| 79967 | caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
| 79968 | } |
| 79969 | |
| @@ -80043,14 +80666,14 @@ | |
| 80043 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 80044 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 80045 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 80046 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 80047 | /* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */ |
| 80048 | {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0}, |
| 80049 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 80050 | /* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */ |
| 80051 | {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0}, |
| 80052 | FUNCTION(random, 0, 0, 0, randomFunc ), |
| 80053 | FUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 80054 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 80055 | FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 80056 | FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| @@ -80073,11 +80696,11 @@ | |
| 80073 | #endif |
| 80074 | AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 80075 | AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 80076 | AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 80077 | /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */ |
| 80078 | {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0}, |
| 80079 | AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
| 80080 | AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80081 | AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80082 | |
| 80083 | LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| @@ -80484,11 +81107,11 @@ | |
| 80484 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 80485 | |
| 80486 | sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); |
| 80487 | sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF); |
| 80488 | for(i=0; i<nCol; i++){ |
| 80489 | sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[i]+1+regData, regTemp+i); |
| 80490 | } |
| 80491 | |
| 80492 | /* If the parent table is the same as the child table, and we are about |
| 80493 | ** to increment the constraint-counter (i.e. this is an INSERT operation), |
| 80494 | ** then check if the row being inserted matches itself. If so, do not |
| @@ -87131,15 +87754,17 @@ | |
| 87131 | sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 87132 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); |
| 87133 | sqlite3ReleaseTempReg(pParse, r1); |
| 87134 | } |
| 87135 | |
| 87136 | /* |
| 87137 | ** Generate an error message when a SELECT is used within a subexpression |
| 87138 | ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result |
| 87139 | ** column. We do this in a subroutine because the error occurs in multiple |
| 87140 | ** places. |
| 87141 | */ |
| 87142 | static int checkForMultiColumnSelectError( |
| 87143 | Parse *pParse, /* Parse context. */ |
| 87144 | SelectDest *pDest, /* Destination of SELECT results */ |
| 87145 | int nExpr /* Number of result columns returned by SELECT */ |
| @@ -87151,10 +87776,11 @@ | |
| 87151 | return 1; |
| 87152 | }else{ |
| 87153 | return 0; |
| 87154 | } |
| 87155 | } |
| 87156 | |
| 87157 | /* |
| 87158 | ** This routine generates the code for the inside of the inner loop |
| 87159 | ** of a SELECT. |
| 87160 | ** |
| @@ -87230,14 +87856,10 @@ | |
| 87230 | if( pOrderBy==0 ){ |
| 87231 | codeOffset(v, p, iContinue); |
| 87232 | } |
| 87233 | } |
| 87234 | |
| 87235 | if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ |
| 87236 | return; |
| 87237 | } |
| 87238 | |
| 87239 | switch( eDest ){ |
| 87240 | /* In this mode, write each query result to the key of the temporary |
| 87241 | ** table iParm. |
| 87242 | */ |
| 87243 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| @@ -88143,10 +88765,11 @@ | |
| 88143 | /* Create the destination temporary table if necessary |
| 88144 | */ |
| 88145 | if( dest.eDest==SRT_EphemTab ){ |
| 88146 | assert( p->pEList ); |
| 88147 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr); |
| 88148 | dest.eDest = SRT_Table; |
| 88149 | } |
| 88150 | |
| 88151 | /* Make sure all SELECTs in the statement have the same number of elements |
| 88152 | ** in their result sets. |
| @@ -90105,11 +90728,11 @@ | |
| 90105 | ExprList *pList = pF->pExpr->x.pList; |
| 90106 | assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); |
| 90107 | if( pList ){ |
| 90108 | nArg = pList->nExpr; |
| 90109 | regAgg = sqlite3GetTempRange(pParse, nArg); |
| 90110 | sqlite3ExprCodeExprList(pParse, pList, regAgg, 0); |
| 90111 | }else{ |
| 90112 | nArg = 0; |
| 90113 | regAgg = 0; |
| 90114 | } |
| 90115 | if( pF->iDistinct>=0 ){ |
| @@ -90264,10 +90887,19 @@ | |
| 90264 | |
| 90265 | /* Begin generating code. |
| 90266 | */ |
| 90267 | v = sqlite3GetVdbe(pParse); |
| 90268 | if( v==0 ) goto select_end; |
| 90269 | |
| 90270 | /* Generate code for all sub-queries in the FROM clause |
| 90271 | */ |
| 90272 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 90273 | for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| @@ -90338,19 +90970,10 @@ | |
| 90338 | } |
| 90339 | return multiSelect(pParse, p, pDest); |
| 90340 | } |
| 90341 | #endif |
| 90342 | |
| 90343 | /* If writing to memory or generating a set |
| 90344 | ** only a single column may be output. |
| 90345 | */ |
| 90346 | #ifndef SQLITE_OMIT_SUBQUERY |
| 90347 | if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ |
| 90348 | goto select_end; |
| 90349 | } |
| 90350 | #endif |
| 90351 | |
| 90352 | /* If possible, rewrite the query to use GROUP BY instead of DISTINCT. |
| 90353 | ** GROUP BY might use an index, DISTINCT never does. |
| 90354 | */ |
| 90355 | assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 ); |
| 90356 | if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){ |
| @@ -90409,10 +91032,11 @@ | |
| 90409 | assert( isAgg || pGroupBy ); |
| 90410 | distinct = pParse->nTab++; |
| 90411 | pKeyInfo = keyInfoFromExprList(pParse, p->pEList); |
| 90412 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0, |
| 90413 | (char*)pKeyInfo, P4_KEYINFO_HANDOFF); |
| 90414 | }else{ |
| 90415 | distinct = -1; |
| 90416 | } |
| 90417 | |
| 90418 | /* Aggregate and non-aggregate queries are handled differently */ |
| @@ -92884,10 +93508,11 @@ | |
| 92884 | ** be stored. |
| 92885 | */ |
| 92886 | assert( v ); |
| 92887 | ephemTab = pParse->nTab++; |
| 92888 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); |
| 92889 | |
| 92890 | /* fill the ephemeral table |
| 92891 | */ |
| 92892 | sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); |
| 92893 | sqlite3Select(pParse, pSelect, &dest); |
| @@ -93022,10 +93647,14 @@ | |
| 93022 | int nDb; /* Number of attached databases */ |
| 93023 | |
| 93024 | if( !db->autoCommit ){ |
| 93025 | sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); |
| 93026 | return SQLITE_ERROR; |
| 93027 | } |
| 93028 | |
| 93029 | /* Save the current value of the database flags so that it can be |
| 93030 | ** restored before returning. Then set the writable-schema flag, and |
| 93031 | ** disable CHECK and foreign key constraints. */ |
| @@ -93624,11 +94253,11 @@ | |
| 93624 | sqlite3DbFree(db, zStmt); |
| 93625 | v = sqlite3GetVdbe(pParse); |
| 93626 | sqlite3ChangeCookie(pParse, iDb); |
| 93627 | |
| 93628 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 93629 | zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName); |
| 93630 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); |
| 93631 | sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 93632 | pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
| 93633 | } |
| 93634 | |
| @@ -94864,15 +95493,16 @@ | |
| 94864 | if( op==TK_REGISTER ){ |
| 94865 | op = pRight->op2; |
| 94866 | } |
| 94867 | if( op==TK_VARIABLE ){ |
| 94868 | Vdbe *pReprepare = pParse->pReprepare; |
| 94869 | pVal = sqlite3VdbeGetValue(pReprepare, pRight->iColumn, SQLITE_AFF_NONE); |
| 94870 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 94871 | z = (char *)sqlite3_value_text(pVal); |
| 94872 | } |
| 94873 | sqlite3VdbeSetVarmask(pParse->pVdbe, pRight->iColumn); |
| 94874 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 94875 | }else if( op==TK_STRING ){ |
| 94876 | z = pRight->u.zToken; |
| 94877 | } |
| 94878 | if( z ){ |
| @@ -94886,11 +95516,11 @@ | |
| 94886 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 94887 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 94888 | *ppPrefix = pPrefix; |
| 94889 | if( op==TK_VARIABLE ){ |
| 94890 | Vdbe *v = pParse->pVdbe; |
| 94891 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
| 94892 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 94893 | /* If the rhs of the LIKE expression is a variable, and the current |
| 94894 | ** value of the variable means there is no need to invoke the LIKE |
| 94895 | ** function, then no OP_Variable will be added to the program. |
| 94896 | ** This causes problems for the sqlite3_bind_parameter_name() |
| @@ -95900,11 +96530,11 @@ | |
| 95900 | return; |
| 95901 | } |
| 95902 | |
| 95903 | assert( pParse->nQueryLoop >= (double)1 ); |
| 95904 | pTable = pSrc->pTab; |
| 95905 | nTableRow = pTable->pIndex ? pTable->pIndex->aiRowEst[0] : 1000000; |
| 95906 | logN = estLog(nTableRow); |
| 95907 | costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); |
| 95908 | if( costTempIdx>=pCost->rCost ){ |
| 95909 | /* The cost of creating the transient table would be greater than |
| 95910 | ** doing the full table scan */ |
| @@ -96510,11 +97140,11 @@ | |
| 96510 | /* The evalConstExpr() function will have already converted any TK_VARIABLE |
| 96511 | ** expression involved in an comparison into a TK_REGISTER. */ |
| 96512 | assert( pExpr->op!=TK_VARIABLE ); |
| 96513 | if( pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE ){ |
| 96514 | int iVar = pExpr->iColumn; |
| 96515 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
| 96516 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 96517 | return SQLITE_OK; |
| 96518 | } |
| 96519 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 96520 | } |
| @@ -96707,27 +97337,18 @@ | |
| 96707 | Index *pFirst; /* Any other index on the table */ |
| 96708 | memset(&sPk, 0, sizeof(Index)); |
| 96709 | sPk.nColumn = 1; |
| 96710 | sPk.aiColumn = &aiColumnPk; |
| 96711 | sPk.aiRowEst = aiRowEstPk; |
| 96712 | aiRowEstPk[1] = 1; |
| 96713 | sPk.onError = OE_Replace; |
| 96714 | sPk.pTable = pSrc->pTab; |
| 96715 | pFirst = pSrc->pTab->pIndex; |
| 96716 | if( pSrc->notIndexed==0 ){ |
| 96717 | sPk.pNext = pFirst; |
| 96718 | } |
| 96719 | /* The aiRowEstPk[0] is an estimate of the total number of rows in the |
| 96720 | ** table. Get this information from the ANALYZE information if it is |
| 96721 | ** available. If not available, assume the table 1 million rows in size. |
| 96722 | */ |
| 96723 | if( pFirst ){ |
| 96724 | assert( pFirst->aiRowEst!=0 ); /* Allocated together with pFirst */ |
| 96725 | aiRowEstPk[0] = pFirst->aiRowEst[0]; |
| 96726 | }else{ |
| 96727 | aiRowEstPk[0] = 1000000; |
| 96728 | } |
| 96729 | pProbe = &sPk; |
| 96730 | wsFlagMask = ~( |
| 96731 | WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE |
| 96732 | ); |
| 96733 | eqTermMask = WO_EQ|WO_IN; |
| @@ -98297,11 +98918,11 @@ | |
| 98297 | ** CREATE TABLE t2(c, d); |
| 98298 | ** SELECT * FROM t2, t1 WHERE t2.rowid = t1.a; |
| 98299 | ** |
| 98300 | ** The best strategy is to iterate through table t1 first. However it |
| 98301 | ** is not possible to determine this with a simple greedy algorithm. |
| 98302 | ** However, since the cost of a linear scan through table t2 is the same |
| 98303 | ** as the cost of a linear scan through table t1, a simple greedy |
| 98304 | ** algorithm may choose to use t2 for the outer loop, which is a much |
| 98305 | ** costlier approach. |
| 98306 | */ |
| 98307 | nUnconstrained = 0; |
| @@ -103366,19 +103987,37 @@ | |
| 103366 | |
| 103367 | /************** End of sqliteicu.h *******************************************/ |
| 103368 | /************** Continuing where we left off in main.c ***********************/ |
| 103369 | #endif |
| 103370 | |
| 103371 | /* |
| 103372 | ** The version of the library |
| 103373 | */ |
| 103374 | #ifndef SQLITE_AMALGAMATION |
| 103375 | SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 103376 | #endif |
| 103377 | SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } |
| 103378 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 103379 | SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
| 103380 | SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
| 103381 | |
| 103382 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 103383 | /* |
| 103384 | ** If the following function pointer is not NULL and if |
| @@ -103495,10 +104134,17 @@ | |
| 103495 | /* Do the rest of the initialization under the recursive mutex so |
| 103496 | ** that we will be able to handle recursive calls into |
| 103497 | ** sqlite3_initialize(). The recursive calls normally come through |
| 103498 | ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 103499 | ** recursive calls might also be possible. |
| 103500 | */ |
| 103501 | sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
| 103502 | if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
| 103503 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 103504 | sqlite3GlobalConfig.inProgress = 1; |
| @@ -103775,16 +104421,16 @@ | |
| 103775 | if( cnt<0 ) cnt = 0; |
| 103776 | if( sz==0 || cnt==0 ){ |
| 103777 | sz = 0; |
| 103778 | pStart = 0; |
| 103779 | }else if( pBuf==0 ){ |
| 103780 | sz = ROUND8(sz); |
| 103781 | sqlite3BeginBenignMalloc(); |
| 103782 | pStart = sqlite3Malloc( sz*cnt ); |
| 103783 | sqlite3EndBenignMalloc(); |
| 103784 | }else{ |
| 103785 | sz = ROUNDDOWN8(sz); |
| 103786 | pStart = pBuf; |
| 103787 | } |
| 103788 | db->lookaside.pStart = pStart; |
| 103789 | db->lookaside.pFree = 0; |
| 103790 | db->lookaside.sz = (u16)sz; |
| @@ -103823,18 +104469,18 @@ | |
| 103823 | va_list ap; |
| 103824 | int rc; |
| 103825 | va_start(ap, op); |
| 103826 | switch( op ){ |
| 103827 | case SQLITE_DBCONFIG_LOOKASIDE: { |
| 103828 | void *pBuf = va_arg(ap, void*); |
| 103829 | int sz = va_arg(ap, int); |
| 103830 | int cnt = va_arg(ap, int); |
| 103831 | rc = setupLookaside(db, pBuf, sz, cnt); |
| 103832 | break; |
| 103833 | } |
| 103834 | default: { |
| 103835 | rc = SQLITE_ERROR; |
| 103836 | break; |
| 103837 | } |
| 103838 | } |
| 103839 | va_end(ap); |
| 103840 | return rc; |
| @@ -103934,16 +104580,33 @@ | |
| 103934 | } |
| 103935 | db->nSavepoint = 0; |
| 103936 | db->nStatement = 0; |
| 103937 | db->isTransactionSavepoint = 0; |
| 103938 | } |
| 103939 | |
| 103940 | /* |
| 103941 | ** Close an existing SQLite database |
| 103942 | */ |
| 103943 | SQLITE_API int sqlite3_close(sqlite3 *db){ |
| 103944 | HashElem *i; |
| 103945 | int j; |
| 103946 | |
| 103947 | if( !db ){ |
| 103948 | return SQLITE_OK; |
| 103949 | } |
| @@ -104007,10 +104670,11 @@ | |
| 104007 | for(j=0; j<ArraySize(db->aFunc.a); j++){ |
| 104008 | FuncDef *pNext, *pHash, *p; |
| 104009 | for(p=db->aFunc.a[j]; p; p=pHash){ |
| 104010 | pHash = p->pHash; |
| 104011 | while( p ){ |
| 104012 | pNext = p->pNext; |
| 104013 | sqlite3DbFree(db, p); |
| 104014 | p = pNext; |
| 104015 | } |
| 104016 | } |
| @@ -104281,11 +104945,12 @@ | |
| 104281 | int nArg, |
| 104282 | int enc, |
| 104283 | void *pUserData, |
| 104284 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 104285 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 104286 | void (*xFinal)(sqlite3_context*) |
| 104287 | ){ |
| 104288 | FuncDef *p; |
| 104289 | int nName; |
| 104290 | |
| 104291 | assert( sqlite3_mutex_held(db->mutex) ); |
| @@ -104309,14 +104974,14 @@ | |
| 104309 | if( enc==SQLITE_UTF16 ){ |
| 104310 | enc = SQLITE_UTF16NATIVE; |
| 104311 | }else if( enc==SQLITE_ANY ){ |
| 104312 | int rc; |
| 104313 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
| 104314 | pUserData, xFunc, xStep, xFinal); |
| 104315 | if( rc==SQLITE_OK ){ |
| 104316 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
| 104317 | pUserData, xFunc, xStep, xFinal); |
| 104318 | } |
| 104319 | if( rc!=SQLITE_OK ){ |
| 104320 | return rc; |
| 104321 | } |
| 104322 | enc = SQLITE_UTF16BE; |
| @@ -104345,10 +105010,19 @@ | |
| 104345 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
| 104346 | assert(p || db->mallocFailed); |
| 104347 | if( !p ){ |
| 104348 | return SQLITE_NOMEM; |
| 104349 | } |
| 104350 | p->flags = 0; |
| 104351 | p->xFunc = xFunc; |
| 104352 | p->xStep = xStep; |
| 104353 | p->xFinalize = xFinal; |
| 104354 | p->pUserData = pUserData; |
| @@ -104359,21 +105033,53 @@ | |
| 104359 | /* |
| 104360 | ** Create new user functions. |
| 104361 | */ |
| 104362 | SQLITE_API int sqlite3_create_function( |
| 104363 | sqlite3 *db, |
| 104364 | const char *zFunctionName, |
| 104365 | int nArg, |
| 104366 | int enc, |
| 104367 | void *p, |
| 104368 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 104369 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 104370 | void (*xFinal)(sqlite3_context*) |
| 104371 | ){ |
| 104372 | int rc; |
| 104373 | sqlite3_mutex_enter(db->mutex); |
| 104374 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); |
| 104375 | rc = sqlite3ApiExit(db, rc); |
| 104376 | sqlite3_mutex_leave(db->mutex); |
| 104377 | return rc; |
| 104378 | } |
| 104379 | |
| @@ -104391,11 +105097,11 @@ | |
| 104391 | int rc; |
| 104392 | char *zFunc8; |
| 104393 | sqlite3_mutex_enter(db->mutex); |
| 104394 | assert( !db->mallocFailed ); |
| 104395 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 104396 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); |
| 104397 | sqlite3DbFree(db, zFunc8); |
| 104398 | rc = sqlite3ApiExit(db, rc); |
| 104399 | sqlite3_mutex_leave(db->mutex); |
| 104400 | return rc; |
| 104401 | } |
| @@ -104422,11 +105128,11 @@ | |
| 104422 | int nName = sqlite3Strlen30(zName); |
| 104423 | int rc; |
| 104424 | sqlite3_mutex_enter(db->mutex); |
| 104425 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 104426 | sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 104427 | 0, sqlite3InvalidFunction, 0, 0); |
| 104428 | } |
| 104429 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 104430 | sqlite3_mutex_leave(db->mutex); |
| 104431 | return rc; |
| 104432 | } |
| @@ -104560,11 +105266,14 @@ | |
| 104560 | ** registered using sqlite3_wal_hook(). Likewise, registering a callback |
| 104561 | ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism |
| 104562 | ** configured by this function. |
| 104563 | */ |
| 104564 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 104565 | #ifndef SQLITE_OMIT_WAL |
| 104566 | if( nFrame>0 ){ |
| 104567 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 104568 | }else{ |
| 104569 | sqlite3_wal_hook(db, 0, 0); |
| 104570 | } |
| @@ -104690,64 +105399,10 @@ | |
| 104690 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
| 104691 | return 0; |
| 104692 | #endif |
| 104693 | } |
| 104694 | |
| 104695 | /* |
| 104696 | ** This routine is called to create a connection to a database BTree |
| 104697 | ** driver. If zFilename is the name of a file, then that file is |
| 104698 | ** opened and used. If zFilename is the magic name ":memory:" then |
| 104699 | ** the database is stored in memory (and is thus forgotten as soon as |
| 104700 | ** the connection is closed.) If zFilename is NULL then the database |
| 104701 | ** is a "virtual" database for transient use only and is deleted as |
| 104702 | ** soon as the connection is closed. |
| 104703 | ** |
| 104704 | ** A virtual database can be either a disk file (that is automatically |
| 104705 | ** deleted when the file is closed) or it an be held entirely in memory. |
| 104706 | ** The sqlite3TempInMemory() function is used to determine which. |
| 104707 | */ |
| 104708 | SQLITE_PRIVATE int sqlite3BtreeFactory( |
| 104709 | sqlite3 *db, /* Main database when opening aux otherwise 0 */ |
| 104710 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 104711 | int omitJournal, /* if TRUE then do not journal this file */ |
| 104712 | int nCache, /* How many pages in the page cache */ |
| 104713 | int vfsFlags, /* Flags passed through to vfsOpen */ |
| 104714 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 104715 | ){ |
| 104716 | int btFlags = 0; |
| 104717 | int rc; |
| 104718 | |
| 104719 | assert( sqlite3_mutex_held(db->mutex) ); |
| 104720 | assert( ppBtree != 0); |
| 104721 | if( omitJournal ){ |
| 104722 | btFlags |= BTREE_OMIT_JOURNAL; |
| 104723 | } |
| 104724 | if( db->flags & SQLITE_NoReadlock ){ |
| 104725 | btFlags |= BTREE_NO_READLOCK; |
| 104726 | } |
| 104727 | #ifndef SQLITE_OMIT_MEMORYDB |
| 104728 | if( zFilename==0 && sqlite3TempInMemory(db) ){ |
| 104729 | zFilename = ":memory:"; |
| 104730 | } |
| 104731 | #endif |
| 104732 | |
| 104733 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){ |
| 104734 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 104735 | } |
| 104736 | rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags); |
| 104737 | |
| 104738 | /* If the B-Tree was successfully opened, set the pager-cache size to the |
| 104739 | ** default value. Except, if the call to BtreeOpen() returned a handle |
| 104740 | ** open on an existing shared pager-cache, do not change the pager-cache |
| 104741 | ** size. |
| 104742 | */ |
| 104743 | if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){ |
| 104744 | sqlite3BtreeSetCacheSize(*ppBtree, nCache); |
| 104745 | } |
| 104746 | return rc; |
| 104747 | } |
| 104748 | |
| 104749 | /* |
| 104750 | ** Return UTF-8 encoded English language explanation of the most recent |
| 104751 | ** error. |
| 104752 | */ |
| 104753 | SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ |
| @@ -104986,21 +105641,43 @@ | |
| 104986 | ** It merely prevents new constructs that exceed the limit |
| 104987 | ** from forming. |
| 104988 | */ |
| 104989 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 104990 | int oldLimit; |
| 104991 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 104992 | return -1; |
| 104993 | } |
| 104994 | oldLimit = db->aLimit[limitId]; |
| 104995 | if( newLimit>=0 ){ |
| 104996 | if( newLimit>aHardLimit[limitId] ){ |
| 104997 | newLimit = aHardLimit[limitId]; |
| 104998 | } |
| 104999 | db->aLimit[limitId] = newLimit; |
| 105000 | } |
| 105001 | return oldLimit; |
| 105002 | } |
| 105003 | |
| 105004 | /* |
| 105005 | ** This routine does the work of opening a database on behalf of |
| 105006 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| @@ -105019,10 +105696,28 @@ | |
| 105019 | *ppDb = 0; |
| 105020 | #ifndef SQLITE_OMIT_AUTOINIT |
| 105021 | rc = sqlite3_initialize(); |
| 105022 | if( rc ) return rc; |
| 105023 | #endif |
| 105024 | |
| 105025 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 105026 | isThreadsafe = 0; |
| 105027 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 105028 | isThreadsafe = 0; |
| @@ -105053,11 +105748,12 @@ | |
| 105053 | SQLITE_OPEN_MAIN_JOURNAL | |
| 105054 | SQLITE_OPEN_TEMP_JOURNAL | |
| 105055 | SQLITE_OPEN_SUBJOURNAL | |
| 105056 | SQLITE_OPEN_MASTER_JOURNAL | |
| 105057 | SQLITE_OPEN_NOMUTEX | |
| 105058 | SQLITE_OPEN_FULLMUTEX |
| 105059 | ); |
| 105060 | |
| 105061 | /* Allocate the sqlite data structure */ |
| 105062 | db = sqlite3MallocZero( sizeof(sqlite3) ); |
| 105063 | if( db==0 ) goto opendb_out; |
| @@ -105125,13 +105821,12 @@ | |
| 105125 | createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, |
| 105126 | nocaseCollatingFunc, 0); |
| 105127 | |
| 105128 | /* Open the backend database driver */ |
| 105129 | db->openFlags = flags; |
| 105130 | rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, |
| 105131 | flags | SQLITE_OPEN_MAIN_DB, |
| 105132 | &db->aDb[0].pBt); |
| 105133 | if( rc!=SQLITE_OK ){ |
| 105134 | if( rc==SQLITE_IOERR_NOMEM ){ |
| 105135 | rc = SQLITE_NOMEM; |
| 105136 | } |
| 105137 | sqlite3Error(db, rc, 0); |
| @@ -105833,10 +106528,26 @@ | |
| 105833 | */ |
| 105834 | case SQLITE_TESTCTRL_PGHDRSZ: { |
| 105835 | rc = sizeof(PgHdr); |
| 105836 | break; |
| 105837 | } |
| 105838 | |
| 105839 | } |
| 105840 | va_end(ap); |
| 105841 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
| 105842 | return rc; |
| @@ -107022,10 +107733,11 @@ | |
| 107022 | ); |
| 107023 | SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char const**, int*); |
| 107024 | SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **); |
| 107025 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeLocal(Fts3Cursor*, u32*); |
| 107026 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeGlobal(Fts3Cursor*, u32*); |
| 107027 | |
| 107028 | /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */ |
| 107029 | #define FTS3_SEGMENT_REQUIRE_POS 0x00000001 |
| 107030 | #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 |
| 107031 | #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 |
| @@ -108971,10 +109683,13 @@ | |
| 108971 | zQuery); |
| 108972 | } |
| 108973 | return rc; |
| 108974 | } |
| 108975 | |
| 108976 | rc = evalFts3Expr(p, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0); |
| 108977 | pCsr->pNextId = pCsr->aDoclist; |
| 108978 | pCsr->iPrevId = 0; |
| 108979 | } |
| 108980 | |
| @@ -109349,15 +110064,18 @@ | |
| 109349 | static int fts3RenameMethod( |
| 109350 | sqlite3_vtab *pVtab, /* Virtual table handle */ |
| 109351 | const char *zName /* New name of table */ |
| 109352 | ){ |
| 109353 | Fts3Table *p = (Fts3Table *)pVtab; |
| 109354 | sqlite3 *db; /* Database connection */ |
| 109355 | int rc; /* Return Code */ |
| 109356 | |
| 109357 | db = p->db; |
| 109358 | rc = SQLITE_OK; |
| 109359 | fts3DbExec(&rc, db, |
| 109360 | "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 109361 | p->zDb, p->zName, zName |
| 109362 | ); |
| 109363 | if( rc==SQLITE_ERROR ) rc = SQLITE_OK; |
| @@ -112512,10 +113230,40 @@ | |
| 112512 | return SQLITE_CORRUPT; |
| 112513 | } |
| 112514 | } |
| 112515 | return SQLITE_OK; |
| 112516 | } |
| 112517 | |
| 112518 | /* |
| 112519 | ** Set *ppStmt to a statement handle that may be used to iterate through |
| 112520 | ** all rows in the %_segdir table, from oldest to newest. If successful, |
| 112521 | ** return SQLITE_OK. If an error occurs while preparing the statement, |
| @@ -116003,10 +116751,49 @@ | |
| 116003 | ** |
| 116004 | ************************************************************************* |
| 116005 | ** This file contains code for implementations of the r-tree and r*-tree |
| 116006 | ** algorithms packaged as an SQLite virtual table module. |
| 116007 | */ |
| 116008 | |
| 116009 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) |
| 116010 | |
| 116011 | /* |
| 116012 | ** This file contains an implementation of a couple of different variants |
| @@ -116044,18 +116831,22 @@ | |
| 116044 | #endif |
| 116045 | #if VARIANT_RSTARTREE_SPLIT |
| 116046 | #define AssignCells splitNodeStartree |
| 116047 | #endif |
| 116048 | |
| 116049 | |
| 116050 | #ifndef SQLITE_CORE |
| 116051 | SQLITE_EXTENSION_INIT1 |
| 116052 | #else |
| 116053 | #endif |
| 116054 | |
| 116055 | |
| 116056 | #ifndef SQLITE_AMALGAMATION |
| 116057 | typedef sqlite3_int64 i64; |
| 116058 | typedef unsigned char u8; |
| 116059 | typedef unsigned int u32; |
| 116060 | #endif |
| 116061 | |
| @@ -116062,10 +116853,12 @@ | |
| 116062 | typedef struct Rtree Rtree; |
| 116063 | typedef struct RtreeCursor RtreeCursor; |
| 116064 | typedef struct RtreeNode RtreeNode; |
| 116065 | typedef struct RtreeCell RtreeCell; |
| 116066 | typedef struct RtreeConstraint RtreeConstraint; |
| 116067 | typedef union RtreeCoord RtreeCoord; |
| 116068 | |
| 116069 | /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ |
| 116070 | #define RTREE_MAX_DIMENSIONS 5 |
| 116071 | |
| @@ -116131,10 +116924,19 @@ | |
| 116131 | */ |
| 116132 | #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) |
| 116133 | #define RTREE_REINSERT(p) RTREE_MINCELLS(p) |
| 116134 | #define RTREE_MAXCELLS 51 |
| 116135 | |
| 116136 | /* |
| 116137 | ** An rtree cursor object. |
| 116138 | */ |
| 116139 | struct RtreeCursor { |
| 116140 | sqlite3_vtab_cursor base; |
| @@ -116163,39 +116965,27 @@ | |
| 116163 | |
| 116164 | /* |
| 116165 | ** A search constraint. |
| 116166 | */ |
| 116167 | struct RtreeConstraint { |
| 116168 | int iCoord; /* Index of constrained coordinate */ |
| 116169 | int op; /* Constraining operation */ |
| 116170 | double rValue; /* Constraint value. */ |
| 116171 | }; |
| 116172 | |
| 116173 | /* Possible values for RtreeConstraint.op */ |
| 116174 | #define RTREE_EQ 0x41 |
| 116175 | #define RTREE_LE 0x42 |
| 116176 | #define RTREE_LT 0x43 |
| 116177 | #define RTREE_GE 0x44 |
| 116178 | #define RTREE_GT 0x45 |
| 116179 | |
| 116180 | /* |
| 116181 | ** An rtree structure node. |
| 116182 | ** |
| 116183 | ** Data format (RtreeNode.zData): |
| 116184 | ** |
| 116185 | ** 1. If the node is the root node (node 1), then the first 2 bytes |
| 116186 | ** of the node contain the tree depth as a big-endian integer. |
| 116187 | ** For non-root nodes, the first 2 bytes are left unused. |
| 116188 | ** |
| 116189 | ** 2. The next 2 bytes contain the number of entries currently |
| 116190 | ** stored in the node. |
| 116191 | ** |
| 116192 | ** 3. The remainder of the node contains the node entries. Each entry |
| 116193 | ** consists of a single 8-byte integer followed by an even number |
| 116194 | ** of 4-byte coordinates. For leaf nodes the integer is the rowid |
| 116195 | ** of a record. For internal nodes it is the node number of a |
| 116196 | ** child page. |
| 116197 | */ |
| 116198 | struct RtreeNode { |
| 116199 | RtreeNode *pParent; /* Parent node */ |
| 116200 | i64 iNode; |
| 116201 | int nRef; |
| @@ -116211,10 +117001,44 @@ | |
| 116211 | struct RtreeCell { |
| 116212 | i64 iRowid; |
| 116213 | RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; |
| 116214 | }; |
| 116215 | |
| 116216 | #ifndef MAX |
| 116217 | # define MAX(x,y) ((x) < (y) ? (y) : (x)) |
| 116218 | #endif |
| 116219 | #ifndef MIN |
| 116220 | # define MIN(x,y) ((x) > (y) ? (y) : (x)) |
| @@ -116293,14 +117117,12 @@ | |
| 116293 | |
| 116294 | /* |
| 116295 | ** Clear the content of node p (set all bytes to 0x00). |
| 116296 | */ |
| 116297 | static void nodeZero(Rtree *pRtree, RtreeNode *p){ |
| 116298 | if( p ){ |
| 116299 | memset(&p->zData[2], 0, pRtree->iNodeSize-2); |
| 116300 | p->isDirty = 1; |
| 116301 | } |
| 116302 | } |
| 116303 | |
| 116304 | /* |
| 116305 | ** Given a node number iNode, return the corresponding key to use |
| 116306 | ** in the Rtree.aHash table. |
| @@ -116316,26 +117138,23 @@ | |
| 116316 | ** Search the node hash table for node iNode. If found, return a pointer |
| 116317 | ** to it. Otherwise, return 0. |
| 116318 | */ |
| 116319 | static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ |
| 116320 | RtreeNode *p; |
| 116321 | assert( iNode!=0 ); |
| 116322 | for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); |
| 116323 | return p; |
| 116324 | } |
| 116325 | |
| 116326 | /* |
| 116327 | ** Add node pNode to the node hash table. |
| 116328 | */ |
| 116329 | static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ |
| 116330 | if( pNode ){ |
| 116331 | int iHash; |
| 116332 | assert( pNode->pNext==0 ); |
| 116333 | iHash = nodeHash(pNode->iNode); |
| 116334 | pNode->pNext = pRtree->aHash[iHash]; |
| 116335 | pRtree->aHash[iHash] = pNode; |
| 116336 | } |
| 116337 | } |
| 116338 | |
| 116339 | /* |
| 116340 | ** Remove node pNode from the node hash table. |
| 116341 | */ |
| @@ -116353,15 +117172,15 @@ | |
| 116353 | ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), |
| 116354 | ** indicating that node has not yet been assigned a node number. It is |
| 116355 | ** assigned a node number when nodeWrite() is called to write the |
| 116356 | ** node contents out to the database. |
| 116357 | */ |
| 116358 | static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent, int zero){ |
| 116359 | RtreeNode *pNode; |
| 116360 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); |
| 116361 | if( pNode ){ |
| 116362 | memset(pNode, 0, sizeof(RtreeNode) + (zero?pRtree->iNodeSize:0)); |
| 116363 | pNode->zData = (u8 *)&pNode[1]; |
| 116364 | pNode->nRef = 1; |
| 116365 | pNode->pParent = pParent; |
| 116366 | pNode->isDirty = 1; |
| 116367 | nodeReference(pParent); |
| @@ -116378,10 +117197,11 @@ | |
| 116378 | i64 iNode, /* Node number to load */ |
| 116379 | RtreeNode *pParent, /* Either the parent node or NULL */ |
| 116380 | RtreeNode **ppNode /* OUT: Acquired node */ |
| 116381 | ){ |
| 116382 | int rc; |
| 116383 | RtreeNode *pNode; |
| 116384 | |
| 116385 | /* Check if the requested node is already in the hash table. If so, |
| 116386 | ** increase its reference count and return it. |
| 116387 | */ |
| @@ -116394,43 +117214,67 @@ | |
| 116394 | pNode->nRef++; |
| 116395 | *ppNode = pNode; |
| 116396 | return SQLITE_OK; |
| 116397 | } |
| 116398 | |
| 116399 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); |
| 116400 | if( !pNode ){ |
| 116401 | *ppNode = 0; |
| 116402 | return SQLITE_NOMEM; |
| 116403 | } |
| 116404 | pNode->pParent = pParent; |
| 116405 | pNode->zData = (u8 *)&pNode[1]; |
| 116406 | pNode->nRef = 1; |
| 116407 | pNode->iNode = iNode; |
| 116408 | pNode->isDirty = 0; |
| 116409 | pNode->pNext = 0; |
| 116410 | |
| 116411 | sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); |
| 116412 | rc = sqlite3_step(pRtree->pReadNode); |
| 116413 | if( rc==SQLITE_ROW ){ |
| 116414 | const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); |
| 116415 | assert( sqlite3_column_bytes(pRtree->pReadNode, 0)==pRtree->iNodeSize ); |
| 116416 | memcpy(pNode->zData, zBlob, pRtree->iNodeSize); |
| 116417 | nodeReference(pParent); |
| 116418 | }else{ |
| 116419 | sqlite3_free(pNode); |
| 116420 | pNode = 0; |
| 116421 | } |
| 116422 | |
| 116423 | *ppNode = pNode; |
| 116424 | rc = sqlite3_reset(pRtree->pReadNode); |
| 116425 | |
| 116426 | if( rc==SQLITE_OK && iNode==1 ){ |
| 116427 | pRtree->iDepth = readInt16(pNode->zData); |
| 116428 | } |
| 116429 | |
| 116430 | assert( (rc==SQLITE_OK && pNode) || (pNode==0 && rc!=SQLITE_OK) ); |
| 116431 | nodeHashInsert(pRtree, pNode); |
| 116432 | |
| 116433 | return rc; |
| 116434 | } |
| 116435 | |
| 116436 | /* |
| @@ -116479,12 +117323,11 @@ | |
| 116479 | int nMaxCell; /* Maximum number of cells for pNode */ |
| 116480 | |
| 116481 | nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; |
| 116482 | nCell = NCELL(pNode); |
| 116483 | |
| 116484 | assert(nCell<=nMaxCell); |
| 116485 | |
| 116486 | if( nCell<nMaxCell ){ |
| 116487 | nodeOverwriteCell(pRtree, pNode, pCell, nCell); |
| 116488 | writeInt16(&pNode->zData[2], nCell+1); |
| 116489 | pNode->isDirty = 1; |
| 116490 | } |
| @@ -116700,18 +117543,37 @@ | |
| 116700 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 116701 | |
| 116702 | return rc; |
| 116703 | } |
| 116704 | |
| 116705 | /* |
| 116706 | ** Rtree virtual table module xClose method. |
| 116707 | */ |
| 116708 | static int rtreeClose(sqlite3_vtab_cursor *cur){ |
| 116709 | Rtree *pRtree = (Rtree *)(cur->pVtab); |
| 116710 | int rc; |
| 116711 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 116712 | sqlite3_free(pCsr->aConstraint); |
| 116713 | rc = nodeRelease(pRtree, pCsr->pNode); |
| 116714 | sqlite3_free(pCsr); |
| 116715 | return rc; |
| 116716 | } |
| 116717 | |
| @@ -116723,18 +117585,44 @@ | |
| 116723 | */ |
| 116724 | static int rtreeEof(sqlite3_vtab_cursor *cur){ |
| 116725 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 116726 | return (pCsr->pNode==0); |
| 116727 | } |
| 116728 | |
| 116729 | /* |
| 116730 | ** Cursor pCursor currently points to a cell in a non-leaf page. |
| 116731 | ** Return true if the sub-tree headed by the cell is filtered |
| 116732 | ** (excluded) by the constraints in the pCursor->aConstraint[] |
| 116733 | ** array, or false otherwise. |
| 116734 | */ |
| 116735 | static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor){ |
| 116736 | RtreeCell cell; |
| 116737 | int ii; |
| 116738 | int bRes = 0; |
| 116739 | |
| 116740 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| @@ -116742,56 +117630,92 @@ | |
| 116742 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 116743 | double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]); |
| 116744 | double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]); |
| 116745 | |
| 116746 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 116747 | || p->op==RTREE_GT || p->op==RTREE_EQ |
| 116748 | ); |
| 116749 | |
| 116750 | switch( p->op ){ |
| 116751 | case RTREE_LE: case RTREE_LT: bRes = p->rValue<cell_min; break; |
| 116752 | case RTREE_GE: case RTREE_GT: bRes = p->rValue>cell_max; break; |
| 116753 | case RTREE_EQ: |
| 116754 | bRes = (p->rValue>cell_max || p->rValue<cell_min); |
| 116755 | break; |
| 116756 | } |
| 116757 | } |
| 116758 | |
| 116759 | return bRes; |
| 116760 | } |
| 116761 | |
| 116762 | /* |
| 116763 | ** Return true if the cell that cursor pCursor currently points to |
| 116764 | ** would be filtered (excluded) by the constraints in the |
| 116765 | ** pCursor->aConstraint[] array, or false otherwise. |
| 116766 | ** |
| 116767 | ** This function assumes that the cell is part of a leaf node. |
| 116768 | */ |
| 116769 | static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor){ |
| 116770 | RtreeCell cell; |
| 116771 | int ii; |
| 116772 | |
| 116773 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| 116774 | for(ii=0; ii<pCursor->nConstraint; ii++){ |
| 116775 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 116776 | double coord = DCOORD(cell.aCoord[p->iCoord]); |
| 116777 | int res; |
| 116778 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 116779 | || p->op==RTREE_GT || p->op==RTREE_EQ |
| 116780 | ); |
| 116781 | switch( p->op ){ |
| 116782 | case RTREE_LE: res = (coord<=p->rValue); break; |
| 116783 | case RTREE_LT: res = (coord<p->rValue); break; |
| 116784 | case RTREE_GE: res = (coord>=p->rValue); break; |
| 116785 | case RTREE_GT: res = (coord>p->rValue); break; |
| 116786 | case RTREE_EQ: res = (coord==p->rValue); break; |
| 116787 | } |
| 116788 | |
| 116789 | if( !res ) return 1; |
| 116790 | } |
| 116791 | |
| 116792 | return 0; |
| 116793 | } |
| 116794 | |
| 116795 | /* |
| 116796 | ** Cursor pCursor currently points at a node that heads a sub-tree of |
| 116797 | ** height iHeight (if iHeight==0, then the node is a leaf). Descend |
| @@ -116814,17 +117738,17 @@ | |
| 116814 | int iSavedCell = pCursor->iCell; |
| 116815 | |
| 116816 | assert( iHeight>=0 ); |
| 116817 | |
| 116818 | if( iHeight==0 ){ |
| 116819 | isEof = testRtreeEntry(pRtree, pCursor); |
| 116820 | }else{ |
| 116821 | isEof = testRtreeCell(pRtree, pCursor); |
| 116822 | } |
| 116823 | if( isEof || iHeight==0 ){ |
| 116824 | *pEof = isEof; |
| 116825 | return SQLITE_OK; |
| 116826 | } |
| 116827 | |
| 116828 | iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell); |
| 116829 | rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild); |
| 116830 | if( rc!=SQLITE_OK ){ |
| @@ -116856,45 +117780,59 @@ | |
| 116856 | |
| 116857 | /* |
| 116858 | ** One of the cells in node pNode is guaranteed to have a 64-bit |
| 116859 | ** integer value equal to iRowid. Return the index of this cell. |
| 116860 | */ |
| 116861 | static int nodeRowidIndex(Rtree *pRtree, RtreeNode *pNode, i64 iRowid){ |
| 116862 | int ii; |
| 116863 | for(ii=0; nodeGetRowid(pRtree, pNode, ii)!=iRowid; ii++){ |
| 116864 | assert( ii<(NCELL(pNode)-1) ); |
| 116865 | } |
| 116866 | return ii; |
| 116867 | } |
| 116868 | |
| 116869 | /* |
| 116870 | ** Return the index of the cell containing a pointer to node pNode |
| 116871 | ** in its parent. If pNode is the root node, return -1. |
| 116872 | */ |
| 116873 | static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode){ |
| 116874 | RtreeNode *pParent = pNode->pParent; |
| 116875 | if( pParent ){ |
| 116876 | return nodeRowidIndex(pRtree, pParent, pNode->iNode); |
| 116877 | } |
| 116878 | return -1; |
| 116879 | } |
| 116880 | |
| 116881 | /* |
| 116882 | ** Rtree virtual table module xNext method. |
| 116883 | */ |
| 116884 | static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ |
| 116885 | Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab); |
| 116886 | RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; |
| 116887 | int rc = SQLITE_OK; |
| 116888 | |
| 116889 | if( pCsr->iStrategy==1 ){ |
| 116890 | /* This "scan" is a direct lookup by rowid. There is no next entry. */ |
| 116891 | nodeRelease(pRtree, pCsr->pNode); |
| 116892 | pCsr->pNode = 0; |
| 116893 | } |
| 116894 | |
| 116895 | else if( pCsr->pNode ){ |
| 116896 | /* Move to the next entry that matches the configured constraints. */ |
| 116897 | int iHeight = 0; |
| 116898 | while( pCsr->pNode ){ |
| 116899 | RtreeNode *pNode = pCsr->pNode; |
| 116900 | int nCell = NCELL(pNode); |
| @@ -116904,11 +117842,14 @@ | |
| 116904 | if( rc!=SQLITE_OK || !isEof ){ |
| 116905 | return rc; |
| 116906 | } |
| 116907 | } |
| 116908 | pCsr->pNode = pNode->pParent; |
| 116909 | pCsr->iCell = nodeParentIndex(pRtree, pNode); |
| 116910 | nodeReference(pCsr->pNode); |
| 116911 | nodeRelease(pRtree, pNode); |
| 116912 | iHeight++; |
| 116913 | } |
| 116914 | } |
| @@ -116972,10 +117913,55 @@ | |
| 116972 | rc = sqlite3_reset(pRtree->pReadRowid); |
| 116973 | } |
| 116974 | return rc; |
| 116975 | } |
| 116976 | |
| 116977 | |
| 116978 | /* |
| 116979 | ** Rtree virtual table module xFilter method. |
| 116980 | */ |
| 116981 | static int rtreeFilter( |
| @@ -116990,22 +117976,22 @@ | |
| 116990 | int ii; |
| 116991 | int rc = SQLITE_OK; |
| 116992 | |
| 116993 | rtreeReference(pRtree); |
| 116994 | |
| 116995 | sqlite3_free(pCsr->aConstraint); |
| 116996 | pCsr->aConstraint = 0; |
| 116997 | pCsr->iStrategy = idxNum; |
| 116998 | |
| 116999 | if( idxNum==1 ){ |
| 117000 | /* Special case - lookup by rowid. */ |
| 117001 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| 117002 | i64 iRowid = sqlite3_value_int64(argv[0]); |
| 117003 | rc = findLeafNode(pRtree, iRowid, &pLeaf); |
| 117004 | pCsr->pNode = pLeaf; |
| 117005 | if( pLeaf && rc==SQLITE_OK ){ |
| 117006 | pCsr->iCell = nodeRowidIndex(pRtree, pLeaf, iRowid); |
| 117007 | } |
| 117008 | }else{ |
| 117009 | /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array |
| 117010 | ** with the configured constraints. |
| 117011 | */ |
| @@ -117013,16 +117999,28 @@ | |
| 117013 | pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); |
| 117014 | pCsr->nConstraint = argc; |
| 117015 | if( !pCsr->aConstraint ){ |
| 117016 | rc = SQLITE_NOMEM; |
| 117017 | }else{ |
| 117018 | assert( (idxStr==0 && argc==0) || strlen(idxStr)==argc*2 ); |
| 117019 | for(ii=0; ii<argc; ii++){ |
| 117020 | RtreeConstraint *p = &pCsr->aConstraint[ii]; |
| 117021 | p->op = idxStr[ii*2]; |
| 117022 | p->iCoord = idxStr[ii*2+1]-'a'; |
| 117023 | p->rValue = sqlite3_value_double(argv[ii]); |
| 117024 | } |
| 117025 | } |
| 117026 | } |
| 117027 | |
| 117028 | if( rc==SQLITE_OK ){ |
| @@ -117078,10 +118076,11 @@ | |
| 117078 | ** = 0x41 ('A') |
| 117079 | ** <= 0x42 ('B') |
| 117080 | ** < 0x43 ('C') |
| 117081 | ** >= 0x44 ('D') |
| 117082 | ** > 0x45 ('E') |
| 117083 | ** ---------------------- |
| 117084 | ** |
| 117085 | ** The second of each pair of bytes identifies the coordinate column |
| 117086 | ** to which the constraint applies. The leftmost coordinate column |
| 117087 | ** is 'a', the second from the left 'b' etc. |
| @@ -117116,43 +118115,47 @@ | |
| 117116 | */ |
| 117117 | pIdxInfo->estimatedCost = 10.0; |
| 117118 | return SQLITE_OK; |
| 117119 | } |
| 117120 | |
| 117121 | if( p->usable && p->iColumn>0 ){ |
| 117122 | u8 op = 0; |
| 117123 | switch( p->op ){ |
| 117124 | case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; |
| 117125 | case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; |
| 117126 | case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; |
| 117127 | case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; |
| 117128 | case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; |
| 117129 | } |
| 117130 | if( op ){ |
| 117131 | /* Make sure this particular constraint has not been used before. |
| 117132 | ** If it has been used before, ignore it. |
| 117133 | ** |
| 117134 | ** A <= or < can be used if there is a prior >= or >. |
| 117135 | ** A >= or > can be used if there is a prior < or <=. |
| 117136 | ** A <= or < is disqualified if there is a prior <=, <, or ==. |
| 117137 | ** A >= or > is disqualified if there is a prior >=, >, or ==. |
| 117138 | ** A == is disqualifed if there is any prior constraint. |
| 117139 | */ |
| 117140 | int j, opmsk; |
| 117141 | static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 }; |
| 117142 | assert( compatible[RTREE_EQ & 7]==0 ); |
| 117143 | assert( compatible[RTREE_LT & 7]==1 ); |
| 117144 | assert( compatible[RTREE_LE & 7]==1 ); |
| 117145 | assert( compatible[RTREE_GT & 7]==2 ); |
| 117146 | assert( compatible[RTREE_GE & 7]==2 ); |
| 117147 | cCol = p->iColumn - 1 + 'a'; |
| 117148 | opmsk = compatible[op & 7]; |
| 117149 | for(j=0; j<iIdx; j+=2){ |
| 117150 | if( zIdxStr[j+1]==cCol && (compatible[zIdxStr[j] & 7] & opmsk)!=0 ){ |
| 117151 | op = 0; |
| 117152 | break; |
| 117153 | } |
| 117154 | } |
| 117155 | } |
| 117156 | if( op ){ |
| 117157 | assert( iIdx<sizeof(zIdxStr)-1 ); |
| 117158 | zIdxStr[iIdx++] = op; |
| @@ -117256,11 +118259,16 @@ | |
| 117256 | int iExclude |
| 117257 | ){ |
| 117258 | int ii; |
| 117259 | float overlap = 0.0; |
| 117260 | for(ii=0; ii<nCell; ii++){ |
| 117261 | if( ii!=iExclude ){ |
| 117262 | int jj; |
| 117263 | float o = 1.0; |
| 117264 | for(jj=0; jj<(pRtree->nDim*2); jj+=2){ |
| 117265 | double x1; |
| 117266 | double x2; |
| @@ -117349,26 +118357,35 @@ | |
| 117349 | /* Select the child node which will be enlarged the least if pCell |
| 117350 | ** is inserted into it. Resolve ties by choosing the entry with |
| 117351 | ** the smallest area. |
| 117352 | */ |
| 117353 | for(iCell=0; iCell<nCell; iCell++){ |
| 117354 | float growth; |
| 117355 | float area; |
| 117356 | float overlap = 0.0; |
| 117357 | nodeGetCell(pRtree, pNode, iCell, &cell); |
| 117358 | growth = cellGrowth(pRtree, &cell, pCell); |
| 117359 | area = cellArea(pRtree, &cell); |
| 117360 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 117361 | if( ii==(pRtree->iDepth-1) ){ |
| 117362 | overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); |
| 117363 | } |
| 117364 | #endif |
| 117365 | if( (iCell==0) |
| 117366 | || (overlap<fMinOverlap) |
| 117367 | || (overlap==fMinOverlap && growth<fMinGrowth) |
| 117368 | || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea) |
| 117369 | ){ |
| 117370 | fMinOverlap = overlap; |
| 117371 | fMinGrowth = growth; |
| 117372 | fMinArea = area; |
| 117373 | iBest = cell.iRowid; |
| 117374 | } |
| @@ -117387,29 +118404,34 @@ | |
| 117387 | /* |
| 117388 | ** A cell with the same content as pCell has just been inserted into |
| 117389 | ** the node pNode. This function updates the bounding box cells in |
| 117390 | ** all ancestor elements. |
| 117391 | */ |
| 117392 | static void AdjustTree( |
| 117393 | Rtree *pRtree, /* Rtree table */ |
| 117394 | RtreeNode *pNode, /* Adjust ancestry of this node. */ |
| 117395 | RtreeCell *pCell /* This cell was just inserted */ |
| 117396 | ){ |
| 117397 | RtreeNode *p = pNode; |
| 117398 | while( p->pParent ){ |
| 117399 | RtreeCell cell; |
| 117400 | RtreeNode *pParent = p->pParent; |
| 117401 | int iCell = nodeParentIndex(pRtree, p); |
| 117402 | |
| 117403 | nodeGetCell(pRtree, pParent, iCell, &cell); |
| 117404 | if( !cellContains(pRtree, &cell, pCell) ){ |
| 117405 | cellUnion(pRtree, &cell, pCell); |
| 117406 | nodeOverwriteCell(pRtree, pParent, &cell, iCell); |
| 117407 | } |
| 117408 | |
| 117409 | p = pParent; |
| 117410 | } |
| 117411 | } |
| 117412 | |
| 117413 | /* |
| 117414 | ** Write mapping (iRowid->iNode) to the <rtree>_rowid table. |
| 117415 | */ |
| @@ -117934,18 +118956,18 @@ | |
| 117934 | nodeZero(pRtree, pNode); |
| 117935 | memcpy(&aCell[nCell], pCell, sizeof(RtreeCell)); |
| 117936 | nCell++; |
| 117937 | |
| 117938 | if( pNode->iNode==1 ){ |
| 117939 | pRight = nodeNew(pRtree, pNode, 1); |
| 117940 | pLeft = nodeNew(pRtree, pNode, 1); |
| 117941 | pRtree->iDepth++; |
| 117942 | pNode->isDirty = 1; |
| 117943 | writeInt16(pNode->zData, pRtree->iDepth); |
| 117944 | }else{ |
| 117945 | pLeft = pNode; |
| 117946 | pRight = nodeNew(pRtree, pLeft->pParent, 1); |
| 117947 | nodeReference(pLeft); |
| 117948 | } |
| 117949 | |
| 117950 | if( !pLeft || !pRight ){ |
| 117951 | rc = SQLITE_NOMEM; |
| @@ -117958,12 +118980,16 @@ | |
| 117958 | rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox); |
| 117959 | if( rc!=SQLITE_OK ){ |
| 117960 | goto splitnode_out; |
| 117961 | } |
| 117962 | |
| 117963 | /* Ensure both child nodes have node numbers assigned to them. */ |
| 117964 | if( (0==pRight->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))) |
| 117965 | || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) |
| 117966 | ){ |
| 117967 | goto splitnode_out; |
| 117968 | } |
| 117969 | |
| @@ -117975,13 +119001,19 @@ | |
| 117975 | if( rc!=SQLITE_OK ){ |
| 117976 | goto splitnode_out; |
| 117977 | } |
| 117978 | }else{ |
| 117979 | RtreeNode *pParent = pLeft->pParent; |
| 117980 | int iCell = nodeParentIndex(pRtree, pLeft); |
| 117981 | nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); |
| 117982 | AdjustTree(pRtree, pParent, &leftbbox); |
| 117983 | } |
| 117984 | if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ |
| 117985 | goto splitnode_out; |
| 117986 | } |
| 117987 | |
| @@ -118021,44 +119053,73 @@ | |
| 118021 | nodeRelease(pRtree, pLeft); |
| 118022 | sqlite3_free(aCell); |
| 118023 | return rc; |
| 118024 | } |
| 118025 | |
| 118026 | static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ |
| 118027 | int rc = SQLITE_OK; |
| 118028 | if( pLeaf->iNode!=1 && pLeaf->pParent==0 ){ |
| 118029 | sqlite3_bind_int64(pRtree->pReadParent, 1, pLeaf->iNode); |
| 118030 | if( sqlite3_step(pRtree->pReadParent)==SQLITE_ROW ){ |
| 118031 | i64 iNode = sqlite3_column_int64(pRtree->pReadParent, 0); |
| 118032 | rc = nodeAcquire(pRtree, iNode, 0, &pLeaf->pParent); |
| 118033 | }else{ |
| 118034 | rc = SQLITE_ERROR; |
| 118035 | } |
| 118036 | sqlite3_reset(pRtree->pReadParent); |
| 118037 | if( rc==SQLITE_OK ){ |
| 118038 | rc = fixLeafParent(pRtree, pLeaf->pParent); |
| 118039 | } |
| 118040 | } |
| 118041 | return rc; |
| 118042 | } |
| 118043 | |
| 118044 | static int deleteCell(Rtree *, RtreeNode *, int, int); |
| 118045 | |
| 118046 | static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ |
| 118047 | int rc; |
| 118048 | RtreeNode *pParent; |
| 118049 | int iCell; |
| 118050 | |
| 118051 | assert( pNode->nRef==1 ); |
| 118052 | |
| 118053 | /* Remove the entry in the parent cell. */ |
| 118054 | iCell = nodeParentIndex(pRtree, pNode); |
| 118055 | pParent = pNode->pParent; |
| 118056 | pNode->pParent = 0; |
| 118057 | if( SQLITE_OK!=(rc = deleteCell(pRtree, pParent, iCell, iHeight+1)) |
| 118058 | || SQLITE_OK!=(rc = nodeRelease(pRtree, pParent)) |
| 118059 | ){ |
| 118060 | return rc; |
| 118061 | } |
| 118062 | |
| 118063 | /* Remove the xxx_node entry. */ |
| 118064 | sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); |
| @@ -118084,12 +119145,13 @@ | |
| 118084 | pRtree->pDeleted = pNode; |
| 118085 | |
| 118086 | return SQLITE_OK; |
| 118087 | } |
| 118088 | |
| 118089 | static void fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ |
| 118090 | RtreeNode *pParent = pNode->pParent; |
| 118091 | if( pParent ){ |
| 118092 | int ii; |
| 118093 | int nCell = NCELL(pNode); |
| 118094 | RtreeCell box; /* Bounding box for pNode */ |
| 118095 | nodeGetCell(pRtree, pNode, 0, &box); |
| @@ -118097,21 +119159,25 @@ | |
| 118097 | RtreeCell cell; |
| 118098 | nodeGetCell(pRtree, pNode, ii, &cell); |
| 118099 | cellUnion(pRtree, &box, &cell); |
| 118100 | } |
| 118101 | box.iRowid = pNode->iNode; |
| 118102 | ii = nodeParentIndex(pRtree, pNode); |
| 118103 | nodeOverwriteCell(pRtree, pParent, &box, ii); |
| 118104 | fixBoundingBox(pRtree, pParent); |
| 118105 | } |
| 118106 | } |
| 118107 | |
| 118108 | /* |
| 118109 | ** Delete the cell at index iCell of node pNode. After removing the |
| 118110 | ** cell, adjust the r-tree data structure if required. |
| 118111 | */ |
| 118112 | static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ |
| 118113 | int rc; |
| 118114 | |
| 118115 | if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ |
| 118116 | return rc; |
| 118117 | } |
| @@ -118124,18 +119190,17 @@ | |
| 118124 | /* If the node is not the tree root and now has less than the minimum |
| 118125 | ** number of cells, remove it from the tree. Otherwise, update the |
| 118126 | ** cell in the parent node so that it tightly contains the updated |
| 118127 | ** node. |
| 118128 | */ |
| 118129 | if( pNode->iNode!=1 ){ |
| 118130 | RtreeNode *pParent = pNode->pParent; |
| 118131 | if( (pParent->iNode!=1 || NCELL(pParent)!=1) |
| 118132 | && (NCELL(pNode)<RTREE_MINCELLS(pRtree)) |
| 118133 | ){ |
| 118134 | rc = removeNode(pRtree, pNode, iHeight); |
| 118135 | }else{ |
| 118136 | fixBoundingBox(pRtree, pNode); |
| 118137 | } |
| 118138 | } |
| 118139 | |
| 118140 | return rc; |
| 118141 | } |
| @@ -118214,11 +119279,11 @@ | |
| 118214 | rc = parentWrite(pRtree, p->iRowid, pNode->iNode); |
| 118215 | } |
| 118216 | } |
| 118217 | } |
| 118218 | if( rc==SQLITE_OK ){ |
| 118219 | fixBoundingBox(pRtree, pNode); |
| 118220 | } |
| 118221 | for(; rc==SQLITE_OK && ii<nCell; ii++){ |
| 118222 | /* Find a node to store this cell in. pNode->iNode currently contains |
| 118223 | ** the height of the sub-tree headed by the cell. |
| 118224 | */ |
| @@ -118268,15 +119333,17 @@ | |
| 118268 | } |
| 118269 | #else |
| 118270 | rc = SplitNode(pRtree, pNode, pCell, iHeight); |
| 118271 | #endif |
| 118272 | }else{ |
| 118273 | AdjustTree(pRtree, pNode, pCell); |
| 118274 | if( iHeight==0 ){ |
| 118275 | rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); |
| 118276 | }else{ |
| 118277 | rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); |
| 118278 | } |
| 118279 | } |
| 118280 | return rc; |
| 118281 | } |
| 118282 | |
| @@ -118342,11 +119409,10 @@ | |
| 118342 | int rc = SQLITE_OK; |
| 118343 | |
| 118344 | rtreeReference(pRtree); |
| 118345 | |
| 118346 | assert(nData>=1); |
| 118347 | assert(hashIsEmpty(pRtree)); |
| 118348 | |
| 118349 | /* If azData[0] is not an SQL NULL value, it is the rowid of a |
| 118350 | ** record to delete from the r-tree table. The following block does |
| 118351 | ** just that. |
| 118352 | */ |
| @@ -118368,12 +119434,14 @@ | |
| 118368 | } |
| 118369 | |
| 118370 | /* Delete the cell in question from the leaf node. */ |
| 118371 | if( rc==SQLITE_OK ){ |
| 118372 | int rc2; |
| 118373 | iCell = nodeRowidIndex(pRtree, pLeaf, iDelete); |
| 118374 | rc = deleteCell(pRtree, pLeaf, iCell, 0); |
| 118375 | rc2 = nodeRelease(pRtree, pLeaf); |
| 118376 | if( rc==SQLITE_OK ){ |
| 118377 | rc = rc2; |
| 118378 | } |
| 118379 | } |
| @@ -118391,23 +119459,24 @@ | |
| 118391 | ** |
| 118392 | ** This is equivalent to copying the contents of the child into |
| 118393 | ** the root node (the operation that Gutman's paper says to perform |
| 118394 | ** in this scenario). |
| 118395 | */ |
| 118396 | if( rc==SQLITE_OK && pRtree->iDepth>0 ){ |
| 118397 | if( rc==SQLITE_OK && NCELL(pRoot)==1 ){ |
| 118398 | RtreeNode *pChild; |
| 118399 | i64 iChild = nodeGetRowid(pRtree, pRoot, 0); |
| 118400 | rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); |
| 118401 | if( rc==SQLITE_OK ){ |
| 118402 | rc = removeNode(pRtree, pChild, pRtree->iDepth-1); |
| 118403 | } |
| 118404 | if( rc==SQLITE_OK ){ |
| 118405 | pRtree->iDepth--; |
| 118406 | writeInt16(pRoot->zData, pRtree->iDepth); |
| 118407 | pRoot->isDirty = 1; |
| 118408 | } |
| 118409 | } |
| 118410 | } |
| 118411 | |
| 118412 | /* Re-insert the contents of any underfull nodes removed from the tree. */ |
| 118413 | for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ |
| @@ -118693,11 +119762,11 @@ | |
| 118693 | ){ |
| 118694 | int rc = SQLITE_OK; |
| 118695 | Rtree *pRtree; |
| 118696 | int nDb; /* Length of string argv[1] */ |
| 118697 | int nName; /* Length of string argv[2] */ |
| 118698 | int eCoordType = (int)pAux; |
| 118699 | |
| 118700 | const char *aErrMsg[] = { |
| 118701 | 0, /* 0 */ |
| 118702 | "Wrong number of columns for an rtree table", /* 1 */ |
| 118703 | "Too few columns for an rtree table", /* 2 */ |
| @@ -118839,16 +119908,14 @@ | |
| 118839 | ** Register the r-tree module with database handle db. This creates the |
| 118840 | ** virtual table module "rtree" and the debugging/analysis scalar |
| 118841 | ** function "rtreenode". |
| 118842 | */ |
| 118843 | SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){ |
| 118844 | int rc = SQLITE_OK; |
| 118845 | |
| 118846 | if( rc==SQLITE_OK ){ |
| 118847 | int utf8 = SQLITE_UTF8; |
| 118848 | rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); |
| 118849 | } |
| 118850 | if( rc==SQLITE_OK ){ |
| 118851 | int utf8 = SQLITE_UTF8; |
| 118852 | rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0); |
| 118853 | } |
| 118854 | if( rc==SQLITE_OK ){ |
| @@ -118860,10 +119927,74 @@ | |
| 118860 | rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0); |
| 118861 | } |
| 118862 | |
| 118863 | return rc; |
| 118864 | } |
| 118865 | |
| 118866 | #if !SQLITE_CORE |
| 118867 | SQLITE_API int sqlite3_extension_init( |
| 118868 | sqlite3 *db, |
| 118869 | char **pzErrMsg, |
| 118870 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | /****************************************************************************** |
| 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | ** version 3.7.3. By combining all the individual C code files into this |
| 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | ** translation unit. |
| @@ -352,19 +352,25 @@ | |
| 352 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 353 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 354 | #endif |
| 355 | |
| 356 | /* |
| 357 | ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. |
| 358 | ** 0 means mutexes are permanently disable and the library is never |
| 359 | ** threadsafe. 1 means the library is serialized which is the highest |
| 360 | ** level of threadsafety. 2 means the libary is multithreaded - multiple |
| 361 | ** threads can use SQLite as long as no two threads try to use the same |
| 362 | ** database connection at the same time. |
| 363 | ** |
| 364 | ** Older versions of SQLite used an optional THREADSAFE macro. |
| 365 | ** We support that for legacy. |
| 366 | */ |
| 367 | #if !defined(SQLITE_THREADSAFE) |
| 368 | #if defined(THREADSAFE) |
| 369 | # define SQLITE_THREADSAFE THREADSAFE |
| 370 | #else |
| 371 | # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */ |
| 372 | #endif |
| 373 | #endif |
| 374 | |
| 375 | /* |
| 376 | ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. |
| @@ -642,13 +648,13 @@ | |
| 648 | ** |
| 649 | ** See also: [sqlite3_libversion()], |
| 650 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 651 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 652 | */ |
| 653 | #define SQLITE_VERSION "3.7.3" |
| 654 | #define SQLITE_VERSION_NUMBER 3007003 |
| 655 | #define SQLITE_SOURCE_ID "2010-09-29 23:09:23 1ef0dc9328f47506cb2dcd142150e96cb4755216" |
| 656 | |
| 657 | /* |
| 658 | ** CAPI3REF: Run-Time Library Version Numbers |
| 659 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 660 | ** |
| @@ -1292,19 +1298,23 @@ | |
| 1298 | ** object once the object has been registered. |
| 1299 | ** |
| 1300 | ** The zName field holds the name of the VFS module. The name must |
| 1301 | ** be unique across all VFS modules. |
| 1302 | ** |
| 1303 | ** ^SQLite guarantees that the zFilename parameter to xOpen |
| 1304 | ** is either a NULL pointer or string obtained |
| 1305 | ** from xFullPathname() with an optional suffix added. |
| 1306 | ** ^If a suffix is added to the zFilename parameter, it will |
| 1307 | ** consist of a single "-" character followed by no more than |
| 1308 | ** 10 alphanumeric and/or "-" characters. |
| 1309 | ** ^SQLite further guarantees that |
| 1310 | ** the string will be valid and unchanged until xClose() is |
| 1311 | ** called. Because of the previous sentence, |
| 1312 | ** the [sqlite3_file] can safely store a pointer to the |
| 1313 | ** filename if it needs to remember the filename for some reason. |
| 1314 | ** If the zFilename parameter to xOpen is a NULL pointer then xOpen |
| 1315 | ** must invent its own temporary name for the file. ^Whenever the |
| 1316 | ** xFilename parameter is NULL it will also be the case that the |
| 1317 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 1318 | ** |
| 1319 | ** The flags argument to xOpen() includes all bits set in |
| 1320 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -1311,11 +1321,11 @@ | |
| 1321 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 1322 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 1323 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 1324 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 1325 | ** |
| 1326 | ** ^(SQLite will also add one of the following flags to the xOpen() |
| 1327 | ** call, depending on the object being opened: |
| 1328 | ** |
| 1329 | ** <ul> |
| 1330 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 1331 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -1322,11 +1332,12 @@ | |
| 1332 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 1333 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 1334 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 1335 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 1336 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 1337 | ** <li> [SQLITE_OPEN_WAL] |
| 1338 | ** </ul>)^ |
| 1339 | ** |
| 1340 | ** The file I/O implementation can use the object type flags to |
| 1341 | ** change the way it deals with files. For example, an application |
| 1342 | ** that does not care about crash recovery or rollback might make |
| 1343 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -1341,39 +1352,40 @@ | |
| 1352 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 1353 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 1354 | ** </ul> |
| 1355 | ** |
| 1356 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 1357 | ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] |
| 1358 | ** will be set for TEMP databases and their journals, transient |
| 1359 | ** databases, and subjournals. |
| 1360 | ** |
| 1361 | ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction |
| 1362 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 1363 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 1364 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 1365 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 1366 | ** be created, and that it is an error if it already exists. |
| 1367 | ** It is <i>not</i> used to indicate the file should be opened |
| 1368 | ** for exclusive access. |
| 1369 | ** |
| 1370 | ** ^At least szOsFile bytes of memory are allocated by SQLite |
| 1371 | ** to hold the [sqlite3_file] structure passed as the third |
| 1372 | ** argument to xOpen. The xOpen method does not have to |
| 1373 | ** allocate the structure; it should just fill it in. Note that |
| 1374 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 1375 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 1376 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 1377 | ** element will be valid after xOpen returns regardless of the success |
| 1378 | ** or failure of the xOpen call. |
| 1379 | ** |
| 1380 | ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] |
| 1381 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 1382 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 1383 | ** to test whether a file is at least readable. The file can be a |
| 1384 | ** directory. |
| 1385 | ** |
| 1386 | ** ^SQLite will always allocate at least mxPathname+1 bytes for the |
| 1387 | ** output buffer xFullPathname. The exact size of the output buffer |
| 1388 | ** is also passed as a parameter to both methods. If the output buffer |
| 1389 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 1390 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 1391 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -1383,14 +1395,14 @@ | |
| 1395 | ** included in the VFS structure for completeness. |
| 1396 | ** The xRandomness() function attempts to return nBytes bytes |
| 1397 | ** of good-quality randomness into zOut. The return value is |
| 1398 | ** the actual number of bytes of randomness obtained. |
| 1399 | ** The xSleep() method causes the calling thread to sleep for at |
| 1400 | ** least the number of microseconds given. ^The xCurrentTime() |
| 1401 | ** method returns a Julian Day Number for the current date and time as |
| 1402 | ** a floating point value. |
| 1403 | ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian |
| 1404 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 1405 | ** a 24-hour day). |
| 1406 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 1407 | ** date and time if that method is available (if iVersion is 2 or |
| 1408 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1783,11 +1795,11 @@ | |
| 1795 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1796 | ** following SQLite interfaces become non-operational: |
| 1797 | ** <ul> |
| 1798 | ** <li> [sqlite3_memory_used()] |
| 1799 | ** <li> [sqlite3_memory_highwater()] |
| 1800 | ** <li> [sqlite3_soft_heap_limit64()] |
| 1801 | ** <li> [sqlite3_status()] |
| 1802 | ** </ul>)^ |
| 1803 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1804 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1805 | ** allocation statistics are disabled by default. |
| @@ -1797,19 +1809,18 @@ | |
| 1809 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1810 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1811 | ** aligned memory buffer from which the scrach allocations will be |
| 1812 | ** drawn, the size of each scratch allocation (sz), |
| 1813 | ** and the maximum number of scratch allocations (N). The sz |
| 1814 | ** argument must be a multiple of 16. |
| 1815 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1816 | ** of at least sz*N bytes of memory. |
| 1817 | ** ^SQLite will use no more than two scratch buffers per thread. So |
| 1818 | ** N should be set to twice the expected maximum number of threads. |
| 1819 | ** ^SQLite will never require a scratch buffer that is more than 6 |
| 1820 | ** times the database page size. ^If SQLite needs needs additional |
| 1821 | ** scratch memory beyond what is provided by this configuration option, then |
| 1822 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1823 | ** |
| 1824 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1825 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1826 | ** the database page cache with the default page cache implemenation. |
| @@ -1825,12 +1836,11 @@ | |
| 1836 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1837 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1838 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1839 | ** page cache memory is needed beyond what is provided by this option, then |
| 1840 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1841 | ** The pointer in the first argument must |
| 1842 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1843 | ** will be undefined.</dd> |
| 1844 | ** |
| 1845 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1846 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1955,12 +1965,18 @@ | |
| 1965 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1966 | ** slots. The size of the buffer in the first argument must be greater than |
| 1967 | ** or equal to the product of the second and third arguments. The buffer |
| 1968 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1969 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1970 | ** rounded down to the next smaller multiple of 8. ^(The lookaside memory |
| 1971 | ** configuration for a database connection can only be changed when that |
| 1972 | ** connection is not currently using lookaside memory, or in other words |
| 1973 | ** when the "current value" returned by |
| 1974 | ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. |
| 1975 | ** Any attempt to change the lookaside memory configuration when lookaside |
| 1976 | ** memory is in use leaves the configuration unchanged and returns |
| 1977 | ** [SQLITE_BUSY].)^</dd> |
| 1978 | ** |
| 1979 | ** </dl> |
| 1980 | */ |
| 1981 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1982 | |
| @@ -2260,10 +2276,13 @@ | |
| 2276 | */ |
| 2277 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 2278 | |
| 2279 | /* |
| 2280 | ** CAPI3REF: Convenience Routines For Running Queries |
| 2281 | ** |
| 2282 | ** This is a legacy interface that is preserved for backwards compatibility. |
| 2283 | ** Use of this interface is not recommended. |
| 2284 | ** |
| 2285 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 2286 | ** [sqlite3_get_table()] interface. A result table records the |
| 2287 | ** complete query results from one or more queries. |
| 2288 | ** |
| @@ -2281,11 +2300,11 @@ | |
| 2300 | ** |
| 2301 | ** A result table might consist of one or more memory allocations. |
| 2302 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 2303 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 2304 | ** |
| 2305 | ** ^(As an example of the result table format, suppose a query result |
| 2306 | ** is as follows: |
| 2307 | ** |
| 2308 | ** <blockquote><pre> |
| 2309 | ** Name | Age |
| 2310 | ** ----------------------- |
| @@ -2305,31 +2324,31 @@ | |
| 2324 | ** azResult[3] = "43"; |
| 2325 | ** azResult[4] = "Bob"; |
| 2326 | ** azResult[5] = "28"; |
| 2327 | ** azResult[6] = "Cindy"; |
| 2328 | ** azResult[7] = "21"; |
| 2329 | ** </pre></blockquote>)^ |
| 2330 | ** |
| 2331 | ** ^The sqlite3_get_table() function evaluates one or more |
| 2332 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 2333 | ** string of its 2nd parameter and returns a result table to the |
| 2334 | ** pointer given in its 3rd parameter. |
| 2335 | ** |
| 2336 | ** After the application has finished with the result from sqlite3_get_table(), |
| 2337 | ** it must pass the result table pointer to sqlite3_free_table() in order to |
| 2338 | ** release the memory that was malloced. Because of the way the |
| 2339 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 2340 | ** function must not try to call [sqlite3_free()] directly. Only |
| 2341 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 2342 | ** |
| 2343 | ** The sqlite3_get_table() interface is implemented as a wrapper around |
| 2344 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 2345 | ** to any internal data structures of SQLite. It uses only the public |
| 2346 | ** interface defined here. As a consequence, errors that occur in the |
| 2347 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 2348 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 2349 | ** [sqlite3_errmsg()]. |
| 2350 | */ |
| 2351 | SQLITE_API int sqlite3_get_table( |
| 2352 | sqlite3 *db, /* An open database */ |
| 2353 | const char *zSql, /* SQL to be evaluated */ |
| 2354 | char ***pazResult, /* Results of the query */ |
| @@ -2477,11 +2496,13 @@ | |
| 2496 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 2497 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 2498 | ** is not freed. |
| 2499 | ** |
| 2500 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 2501 | ** is always aligned to at least an 8 byte boundary, or to a |
| 2502 | ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time |
| 2503 | ** option is used. |
| 2504 | ** |
| 2505 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 2506 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 2507 | ** implementation of these routines to be omitted. That capability |
| 2508 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2735,21 +2756,32 @@ | |
| 2756 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2757 | |
| 2758 | /* |
| 2759 | ** CAPI3REF: Query Progress Callbacks |
| 2760 | ** |
| 2761 | ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback |
| 2762 | ** function X to be invoked periodically during long running calls to |
| 2763 | ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for |
| 2764 | ** database connection D. An example use for this |
| 2765 | ** interface is to keep a GUI updated during a large query. |
| 2766 | ** |
| 2767 | ** ^The parameter P is passed through as the only parameter to the |
| 2768 | ** callback function X. ^The parameter N is the number of |
| 2769 | ** [virtual machine instructions] that are evaluated between successive |
| 2770 | ** invocations of the callback X. |
| 2771 | ** |
| 2772 | ** ^Only a single progress handler may be defined at one time per |
| 2773 | ** [database connection]; setting a new progress handler cancels the |
| 2774 | ** old one. ^Setting parameter X to NULL disables the progress handler. |
| 2775 | ** ^The progress handler is also disabled by setting N to a value less |
| 2776 | ** than 1. |
| 2777 | ** |
| 2778 | ** ^If the progress callback returns non-zero, the operation is |
| 2779 | ** interrupted. This feature can be used to implement a |
| 2780 | ** "Cancel" button on a GUI progress dialog box. |
| 2781 | ** |
| 2782 | ** The progress handler callback must not do anything that will modify |
| 2783 | ** the database connection that invoked the progress handler. |
| 2784 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2785 | ** database connections for the meaning of "modify" in this paragraph. |
| 2786 | ** |
| 2787 | */ |
| @@ -2804,11 +2836,11 @@ | |
| 2836 | ** </dl> |
| 2837 | ** |
| 2838 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2839 | ** combinations shown above or one of the combinations shown above combined |
| 2840 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2841 | ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags, |
| 2842 | ** then the behavior is undefined. |
| 2843 | ** |
| 2844 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2845 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2846 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2929,21 +2961,26 @@ | |
| 2961 | ** ^(This interface allows the size of various constructs to be limited |
| 2962 | ** on a connection by connection basis. The first parameter is the |
| 2963 | ** [database connection] whose limit is to be set or queried. The |
| 2964 | ** second parameter is one of the [limit categories] that define a |
| 2965 | ** class of constructs to be size limited. The third parameter is the |
| 2966 | ** new limit for that construct.)^ |
| 2967 | ** |
| 2968 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2969 | ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a |
| 2970 | ** [limits | hard upper bound] |
| 2971 | ** set at compile-time by a C preprocessor macro called |
| 2972 | ** [limits | SQLITE_MAX_<i>NAME</i>]. |
| 2973 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2974 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2975 | ** silently truncated to the hard upper bound. |
| 2976 | ** |
| 2977 | ** ^Regardless of whether or not the limit was changed, the |
| 2978 | ** [sqlite3_limit()] interface returns the prior value of the limit. |
| 2979 | ** ^Hence, to find the current value of a limit without changing it, |
| 2980 | ** simply invoke this interface with the third parameter set to -1. |
| 2981 | ** |
| 2982 | ** Run-time limits are intended for use in applications that manage |
| 2983 | ** both their own internal database and also databases that are controlled |
| 2984 | ** by untrusted external sources. An example application might be a |
| 2985 | ** web browser that has its own databases for storing history and |
| 2986 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2968,11 +3005,11 @@ | |
| 3005 | ** The synopsis of the meanings of the various limits is shown below. |
| 3006 | ** Additional information is available at [limits | Limits in SQLite]. |
| 3007 | ** |
| 3008 | ** <dl> |
| 3009 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 3010 | ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^ |
| 3011 | ** |
| 3012 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 3013 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 3014 | ** |
| 3015 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2986,11 +3023,13 @@ | |
| 3023 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 3024 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 3025 | ** |
| 3026 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 3027 | ** <dd>The maximum number of instructions in a virtual machine program |
| 3028 | ** used to implement an SQL statement. This limit is not currently |
| 3029 | ** enforced, though that might be added in some future release of |
| 3030 | ** SQLite.</dd>)^ |
| 3031 | ** |
| 3032 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 3033 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 3034 | ** |
| 3035 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2999,12 +3038,11 @@ | |
| 3038 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 3039 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 3040 | ** [GLOB] operators.</dd>)^ |
| 3041 | ** |
| 3042 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 3043 | ** <dd>The maximum index number of any [parameter] in an SQL statement.)^ |
| 3044 | ** |
| 3045 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 3046 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 3047 | ** </dl> |
| 3048 | */ |
| @@ -3072,16 +3110,11 @@ | |
| 3110 | ** |
| 3111 | ** <ol> |
| 3112 | ** <li> |
| 3113 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 3114 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 3115 | ** statement and try to run it again. |
| 3116 | ** </li> |
| 3117 | ** |
| 3118 | ** <li> |
| 3119 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 3120 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -3090,15 +3123,20 @@ | |
| 3123 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 3124 | ** interfaces, the underlying reason for the error is returned immediately. |
| 3125 | ** </li> |
| 3126 | ** |
| 3127 | ** <li> |
| 3128 | ** ^If the specific value bound to [parameter | host parameter] in the |
| 3129 | ** WHERE clause might influence the choice of query plan for a statement, |
| 3130 | ** then the statement will be automatically recompiled, as if there had been |
| 3131 | ** a schema change, on the first [sqlite3_step()] call following any change |
| 3132 | ** to the [sqlite3_bind_text | bindings] of that [parameter]. |
| 3133 | ** ^The specific value of WHERE-clause [parameter] might influence the |
| 3134 | ** choice of query plan if the parameter is the left-hand side of a [LIKE] |
| 3135 | ** or [GLOB] operator or if the parameter is compared to an indexed column |
| 3136 | ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. |
| 3137 | ** the |
| 3138 | ** </li> |
| 3139 | ** </ol> |
| 3140 | */ |
| 3141 | SQLITE_API int sqlite3_prepare( |
| 3142 | sqlite3 *db, /* Database handle */ |
| @@ -3161,11 +3199,11 @@ | |
| 3199 | ** or if SQLite is run in one of reduced mutex modes |
| 3200 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 3201 | ** then there is no distinction between protected and unprotected |
| 3202 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 3203 | ** for maximum code portability it is recommended that applications |
| 3204 | ** still make the distinction between protected and unprotected |
| 3205 | ** sqlite3_value objects even when not strictly required. |
| 3206 | ** |
| 3207 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 3208 | ** implementation of [application-defined SQL functions] are protected. |
| 3209 | ** ^The sqlite3_value object returned by |
| @@ -3356,10 +3394,12 @@ | |
| 3394 | ** CAPI3REF: Number Of Columns In A Result Set |
| 3395 | ** |
| 3396 | ** ^Return the number of columns in the result set returned by the |
| 3397 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 3398 | ** statement that does not return data (for example an [UPDATE]). |
| 3399 | ** |
| 3400 | ** See also: [sqlite3_data_count()] |
| 3401 | */ |
| 3402 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 3403 | |
| 3404 | /* |
| 3405 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3546,12 +3586,18 @@ | |
| 3586 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3587 | |
| 3588 | /* |
| 3589 | ** CAPI3REF: Number of columns in a result set |
| 3590 | ** |
| 3591 | ** ^The sqlite3_data_count(P) interface returns the number of columns in the |
| 3592 | ** current row of the result set of [prepared statement] P. |
| 3593 | ** ^If prepared statement P does not have results ready to return |
| 3594 | ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of |
| 3595 | ** interfaces) then sqlite3_data_count(P) returns 0. |
| 3596 | ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. |
| 3597 | ** |
| 3598 | ** See also: [sqlite3_column_count()] |
| 3599 | */ |
| 3600 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3601 | |
| 3602 | /* |
| 3603 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3627,22 +3673,30 @@ | |
| 3673 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3674 | ** the string to UTF-8 and then returns the number of bytes. |
| 3675 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3676 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3677 | ** the number of bytes in that string. |
| 3678 | ** ^If the result is NULL, then sqlite3_column_bytes() returns zero. |
| 3679 | ** |
| 3680 | ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() |
| 3681 | ** routine returns the number of bytes in that BLOB or string. |
| 3682 | ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts |
| 3683 | ** the string to UTF-16 and then returns the number of bytes. |
| 3684 | ** ^If the result is a numeric value then sqlite3_column_bytes16() uses |
| 3685 | ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns |
| 3686 | ** the number of bytes in that string. |
| 3687 | ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. |
| 3688 | ** |
| 3689 | ** ^The values returned by [sqlite3_column_bytes()] and |
| 3690 | ** [sqlite3_column_bytes16()] do not include the zero terminators at the end |
| 3691 | ** of the string. ^For clarity: the values returned by |
| 3692 | ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of |
| 3693 | ** bytes in the string, not the number of characters. |
| 3694 | ** |
| 3695 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3696 | ** even empty strings, are always zero terminated. ^The return |
| 3697 | ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. |
| 3698 | ** |
| 3699 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3700 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3701 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3702 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3683,14 +3737,14 @@ | |
| 3737 | ** and atof(). SQLite does not really use these functions. It has its |
| 3738 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3739 | ** used in the table for brevity and because they are familiar to most |
| 3740 | ** C programmers. |
| 3741 | ** |
| 3742 | ** Note that when type conversions occur, pointers returned by prior |
| 3743 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3744 | ** sqlite3_column_text16() may be invalidated. |
| 3745 | ** Type conversions and pointer invalidations might occur |
| 3746 | ** in the following cases: |
| 3747 | ** |
| 3748 | ** <ul> |
| 3749 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3750 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3699,26 +3753,26 @@ | |
| 3753 | ** sqlite3_column_text16() is called. The content must be converted |
| 3754 | ** to UTF-16.</li> |
| 3755 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3756 | ** sqlite3_column_text() is called. The content must be converted |
| 3757 | ** to UTF-8.</li> |
| 3758 | ** </ul> |
| 3759 | ** |
| 3760 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3761 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3762 | ** that the prior pointer references will have been modified. Other kinds |
| 3763 | ** of conversion are done in place when it is possible, but sometimes they |
| 3764 | ** are not possible and in those cases prior pointers are invalidated. |
| 3765 | ** |
| 3766 | ** The safest and easiest to remember policy is to invoke these routines |
| 3767 | ** in one of the following ways: |
| 3768 | ** |
| 3769 | ** <ul> |
| 3770 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3771 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3772 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3773 | ** </ul> |
| 3774 | ** |
| 3775 | ** In other words, you should call sqlite3_column_text(), |
| 3776 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3777 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3778 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3752,21 +3806,30 @@ | |
| 3806 | |
| 3807 | /* |
| 3808 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3809 | ** |
| 3810 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3811 | ** ^If the most recent evaluation of the statement encountered no errors or |
| 3812 | ** or if the statement is never been evaluated, then sqlite3_finalize() returns |
| 3813 | ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then |
| 3814 | ** sqlite3_finalize(S) returns the appropriate [error code] or |
| 3815 | ** [extended error code]. |
| 3816 | ** |
| 3817 | ** ^The sqlite3_finalize(S) routine can be called at any point during |
| 3818 | ** the life cycle of [prepared statement] S: |
| 3819 | ** before statement S is ever evaluated, after |
| 3820 | ** one or more calls to [sqlite3_reset()], or after any call |
| 3821 | ** to [sqlite3_step()] regardless of whether or not the statement has |
| 3822 | ** completed execution. |
| 3823 | ** |
| 3824 | ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. |
| 3825 | ** |
| 3826 | ** The application must finalize every [prepared statement] in order to avoid |
| 3827 | ** resource leaks. It is a grievous error for the application to try to use |
| 3828 | ** a prepared statement after it has been finalized. Any use of a prepared |
| 3829 | ** statement after it has been finalized can result in undefined and |
| 3830 | ** undesirable behavior such as segfaults and heap corruption. |
| 3831 | */ |
| 3832 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3833 | |
| 3834 | /* |
| 3835 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3798,40 +3861,42 @@ | |
| 3861 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3862 | ** KEYWORDS: {function creation routines} |
| 3863 | ** KEYWORDS: {application-defined SQL function} |
| 3864 | ** KEYWORDS: {application-defined SQL functions} |
| 3865 | ** |
| 3866 | ** ^These functions (collectively known as "function creation routines") |
| 3867 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3868 | ** of existing SQL functions or aggregates. The only differences between |
| 3869 | ** these routines are the text encoding expected for |
| 3870 | ** the the second parameter (the name of the function being created) |
| 3871 | ** and the presence or absence of a destructor callback for |
| 3872 | ** the application data pointer. |
| 3873 | ** |
| 3874 | ** ^The first parameter is the [database connection] to which the SQL |
| 3875 | ** function is to be added. ^If an application uses more than one database |
| 3876 | ** connection then application-defined SQL functions must be added |
| 3877 | ** to each database connection separately. |
| 3878 | ** |
| 3879 | ** ^The second parameter is the name of the SQL function to be created or |
| 3880 | ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 |
| 3881 | ** representation, exclusive of the zero-terminator. ^Note that the name |
| 3882 | ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. |
| 3883 | ** ^Any attempt to create a function with a longer name |
| 3884 | ** will result in [SQLITE_MISUSE] being returned. |
| 3885 | ** |
| 3886 | ** ^The third parameter (nArg) |
| 3887 | ** is the number of arguments that the SQL function or |
| 3888 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3889 | ** aggregate may take any number of arguments between 0 and the limit |
| 3890 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3891 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3892 | ** undefined. |
| 3893 | ** |
| 3894 | ** ^The fourth parameter, eTextRep, specifies what |
| 3895 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3896 | ** its parameters. Every SQL function implementation must be able to work |
| 3897 | ** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be |
| 3898 | ** more efficient with one encoding than another. ^An application may |
| 3899 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3900 | ** times with the same function but with different values of eTextRep. |
| 3901 | ** ^When multiple implementations of the same function are available, SQLite |
| 3902 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3839,17 +3904,25 @@ | |
| 3904 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3905 | ** |
| 3906 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3907 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3908 | ** |
| 3909 | ** ^The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are |
| 3910 | ** pointers to C-language functions that implement the SQL function or |
| 3911 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3912 | ** callback only; NULL pointers must be passed as the xStep and xFinal |
| 3913 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3914 | ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing |
| 3915 | ** SQL function or aggregate, pass NULL poiners for all three function |
| 3916 | ** callbacks. |
| 3917 | ** |
| 3918 | ** ^If the tenth parameter to sqlite3_create_function_v2() is not NULL, |
| 3919 | ** then it is invoked when the function is deleted, either by being |
| 3920 | ** overloaded or when the database connection closes. |
| 3921 | ** ^When the destructure callback of the tenth parameter is invoked, it |
| 3922 | ** is passed a single argument which is a copy of the pointer which was |
| 3923 | ** the fifth parameter to sqlite3_create_function_v2(). |
| 3924 | ** |
| 3925 | ** ^It is permitted to register multiple implementations of the same |
| 3926 | ** functions with the same name but with either differing numbers of |
| 3927 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3928 | ** the implementation that most closely matches the way in which the |
| @@ -3861,15 +3934,10 @@ | |
| 3934 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3935 | ** is a closer match than a function where the encoding difference is |
| 3936 | ** between UTF8 and UTF16. |
| 3937 | ** |
| 3938 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3939 | ** |
| 3940 | ** ^An application-defined function is permitted to call other |
| 3941 | ** SQLite interfaces. However, such calls must not |
| 3942 | ** close the database connection nor finalize or reset the prepared |
| 3943 | ** statement in which the function is running. |
| @@ -3891,10 +3959,21 @@ | |
| 3959 | int eTextRep, |
| 3960 | void *pApp, |
| 3961 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3962 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3963 | void (*xFinal)(sqlite3_context*) |
| 3964 | ); |
| 3965 | SQLITE_API int sqlite3_create_function_v2( |
| 3966 | sqlite3 *db, |
| 3967 | const char *zFunctionName, |
| 3968 | int nArg, |
| 3969 | int eTextRep, |
| 3970 | void *pApp, |
| 3971 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3972 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3973 | void (*xFinal)(sqlite3_context*), |
| 3974 | void(*xDestroy)(void*) |
| 3975 | ); |
| 3976 | |
| 3977 | /* |
| 3978 | ** CAPI3REF: Text Encodings |
| 3979 | ** |
| @@ -4238,73 +4317,97 @@ | |
| 4317 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 4318 | |
| 4319 | /* |
| 4320 | ** CAPI3REF: Define New Collating Sequences |
| 4321 | ** |
| 4322 | ** ^These functions add, remove, or modify a [collation] associated |
| 4323 | ** with the [database connection] specified as the first argument. |
| 4324 | ** |
| 4325 | ** ^The name of the collation is a UTF-8 string |
| 4326 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 4327 | ** and a UTF-16 string in native byte order for sqlite3_create_collation16(). |
| 4328 | ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are |
| 4329 | ** considered to be the same name. |
| 4330 | ** |
| 4331 | ** ^(The third argument (eTextRep) must be one of the constants: |
| 4332 | ** <ul> |
| 4333 | ** <li> [SQLITE_UTF8], |
| 4334 | ** <li> [SQLITE_UTF16LE], |
| 4335 | ** <li> [SQLITE_UTF16BE], |
| 4336 | ** <li> [SQLITE_UTF16], or |
| 4337 | ** <li> [SQLITE_UTF16_ALIGNED]. |
| 4338 | ** </ul>)^ |
| 4339 | ** ^The eTextRep argument determines the encoding of strings passed |
| 4340 | ** to the collating function callback, xCallback. |
| 4341 | ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep |
| 4342 | ** force strings to be UTF16 with native byte order. |
| 4343 | ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin |
| 4344 | ** on an even byte address. |
| 4345 | ** |
| 4346 | ** ^The fourth argument, pArg, is a application data pointer that is passed |
| 4347 | ** through as the first argument to the collating function callback. |
| 4348 | ** |
| 4349 | ** ^The fifth argument, xCallback, is a pointer to the collating function. |
| 4350 | ** ^Multiple collating functions can be registered using the same name but |
| 4351 | ** with different eTextRep parameters and SQLite will use whichever |
| 4352 | ** function requires the least amount of data transformation. |
| 4353 | ** ^If the xCallback argument is NULL then the collating function is |
| 4354 | ** deleted. ^When all collating functions having the same name are deleted, |
| 4355 | ** that collation is no longer usable. |
| 4356 | ** |
| 4357 | ** ^The collating function callback is invoked with a copy of the pArg |
| 4358 | ** application data pointer and with two strings in the encoding specified |
| 4359 | ** by the eTextRep argument. The collating function must return an |
| 4360 | ** integer that is negative, zero, or positive |
| 4361 | ** if the first string is less than, equal to, or greater than the second, |
| 4362 | ** respectively. A collating function must alway return the same answer |
| 4363 | ** given the same inputs. If two or more collating functions are registered |
| 4364 | ** to the same collation name (using different eTextRep values) then all |
| 4365 | ** must give an equivalent answer when invoked with equivalent strings. |
| 4366 | ** The collating function must obey the following properties for all |
| 4367 | ** strings A, B, and C: |
| 4368 | ** |
| 4369 | ** <ol> |
| 4370 | ** <li> If A==B then B==A. |
| 4371 | ** <li> If A==B and B==C then A==C. |
| 4372 | ** <li> If A<B THEN B>A. |
| 4373 | ** <li> If A<B and B<C then A<C. |
| 4374 | ** </ol> |
| 4375 | ** |
| 4376 | ** If a collating function fails any of the above constraints and that |
| 4377 | ** collating function is registered and used, then the behavior of SQLite |
| 4378 | ** is undefined. |
| 4379 | ** |
| 4380 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 4381 | ** with the addition that the xDestroy callback is invoked on pArg when |
| 4382 | ** the collating function is deleted. |
| 4383 | ** ^Collating functions are deleted when they are overridden by later |
| 4384 | ** calls to the collation creation functions or when the |
| 4385 | ** [database connection] is closed using [sqlite3_close()]. |
| 4386 | ** |
| 4387 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 4388 | */ |
| 4389 | SQLITE_API int sqlite3_create_collation( |
| 4390 | sqlite3*, |
| 4391 | const char *zName, |
| 4392 | int eTextRep, |
| 4393 | void *pArg, |
| 4394 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4395 | ); |
| 4396 | SQLITE_API int sqlite3_create_collation_v2( |
| 4397 | sqlite3*, |
| 4398 | const char *zName, |
| 4399 | int eTextRep, |
| 4400 | void *pArg, |
| 4401 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 4402 | void(*xDestroy)(void*) |
| 4403 | ); |
| 4404 | SQLITE_API int sqlite3_create_collation16( |
| 4405 | sqlite3*, |
| 4406 | const void *zName, |
| 4407 | int eTextRep, |
| 4408 | void *pArg, |
| 4409 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4410 | ); |
| 4411 | |
| 4412 | /* |
| 4413 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -4389,20 +4492,23 @@ | |
| 4492 | #endif |
| 4493 | |
| 4494 | /* |
| 4495 | ** CAPI3REF: Suspend Execution For A Short Time |
| 4496 | ** |
| 4497 | ** The sqlite3_sleep() function causes the current thread to suspend execution |
| 4498 | ** for at least a number of milliseconds specified in its parameter. |
| 4499 | ** |
| 4500 | ** If the operating system does not support sleep requests with |
| 4501 | ** millisecond time resolution, then the time will be rounded up to |
| 4502 | ** the nearest second. The number of milliseconds of sleep actually |
| 4503 | ** requested from the operating system is returned. |
| 4504 | ** |
| 4505 | ** ^SQLite implements this interface by calling the xSleep() |
| 4506 | ** method of the default [sqlite3_vfs] object. If the xSleep() method |
| 4507 | ** of the default VFS is not implemented correctly, or not implemented at |
| 4508 | ** all, then the behavior of sqlite3_sleep() may deviate from the description |
| 4509 | ** in the previous paragraphs. |
| 4510 | */ |
| 4511 | SQLITE_API int sqlite3_sleep(int); |
| 4512 | |
| 4513 | /* |
| 4514 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4620,44 +4726,77 @@ | |
| 4726 | ** of heap memory by deallocating non-essential memory allocations |
| 4727 | ** held by the database library. Memory used to cache database |
| 4728 | ** pages to improve performance is an example of non-essential memory. |
| 4729 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4730 | ** which might be more or less than the amount requested. |
| 4731 | ** ^The sqlite3_release_memory() routine is a no-op returning zero |
| 4732 | ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. |
| 4733 | */ |
| 4734 | SQLITE_API int sqlite3_release_memory(int); |
| 4735 | |
| 4736 | /* |
| 4737 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4738 | ** |
| 4739 | ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the |
| 4740 | ** soft limit on the amount of heap memory that may be allocated by SQLite. |
| 4741 | ** ^SQLite strives to keep heap memory utilization below the soft heap |
| 4742 | ** limit by reducing the number of pages held in the page cache |
| 4743 | ** as heap memory usages approaches the limit. |
| 4744 | ** ^The soft heap limit is "soft" because even though SQLite strives to stay |
| 4745 | ** below the limit, it will exceed the limit rather than generate |
| 4746 | ** an [SQLITE_NOMEM] error. In other words, the soft heap limit |
| 4747 | ** is advisory only. |
| 4748 | ** |
| 4749 | ** ^The return value from sqlite3_soft_heap_limit64() is the size of |
| 4750 | ** the soft heap limit prior to the call. ^If the argument N is negative |
| 4751 | ** then no change is made to the soft heap limit. Hence, the current |
| 4752 | ** size of the soft heap limit can be determined by invoking |
| 4753 | ** sqlite3_soft_heap_limit64() with a negative argument. |
| 4754 | ** |
| 4755 | ** ^If the argument N is zero then the soft heap limit is disabled. |
| 4756 | ** |
| 4757 | ** ^(The soft heap limit is not enforced in the current implementation |
| 4758 | ** if one or more of following conditions are true: |
| 4759 | ** |
| 4760 | ** <ul> |
| 4761 | ** <li> The soft heap limit is set to zero. |
| 4762 | ** <li> Memory accounting is disabled using a combination of the |
| 4763 | ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and |
| 4764 | ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. |
| 4765 | ** <li> An alternative page cache implementation is specifed using |
| 4766 | ** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...). |
| 4767 | ** <li> The page cache allocates from its own memory pool supplied |
| 4768 | ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than |
| 4769 | ** from the heap. |
| 4770 | ** </ul>)^ |
| 4771 | ** |
| 4772 | ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced |
| 4773 | ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] |
| 4774 | ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], |
| 4775 | ** the soft heap limit is enforced on every memory allocation. Without |
| 4776 | ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced |
| 4777 | ** when memory is allocated by the page cache. Testing suggests that because |
| 4778 | ** the page cache is the predominate memory user in SQLite, most |
| 4779 | ** applications will achieve adequate soft heap limit enforcement without |
| 4780 | ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. |
| 4781 | ** |
| 4782 | ** The circumstances under which SQLite will enforce the soft heap limit may |
| 4783 | ** changes in future releases of SQLite. |
| 4784 | */ |
| 4785 | SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); |
| 4786 | |
| 4787 | /* |
| 4788 | ** CAPI3REF: Deprecated Soft Heap Limit Interface |
| 4789 | ** DEPRECATED |
| 4790 | ** |
| 4791 | ** This is a deprecated version of the [sqlite3_soft_heap_limit64()] |
| 4792 | ** interface. This routine is provided for historical compatibility |
| 4793 | ** only. All new applications should use the |
| 4794 | ** [sqlite3_soft_heap_limit64()] interface rather than this one. |
| 4795 | */ |
| 4796 | SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); |
| 4797 | |
| 4798 | |
| 4799 | /* |
| 4800 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4801 | ** |
| 4802 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4777,38 +4916,51 @@ | |
| 4916 | ** it back off again. |
| 4917 | */ |
| 4918 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4919 | |
| 4920 | /* |
| 4921 | ** CAPI3REF: Automatically Load Statically Linked Extensions |
| 4922 | ** |
| 4923 | ** ^This interface causes the xEntryPoint() function to be invoked for |
| 4924 | ** each new [database connection] that is created. The idea here is that |
| 4925 | ** xEntryPoint() is the entry point for a statically linked SQLite extension |
| 4926 | ** that is to be automatically loaded into all new database connections. |
| 4927 | ** |
| 4928 | ** ^(Even though the function prototype shows that xEntryPoint() takes |
| 4929 | ** no arguments and returns void, SQLite invokes xEntryPoint() with three |
| 4930 | ** arguments and expects and integer result as if the signature of the |
| 4931 | ** entry point where as follows: |
| 4932 | ** |
| 4933 | ** <blockquote><pre> |
| 4934 | ** int xEntryPoint( |
| 4935 | ** sqlite3 *db, |
| 4936 | ** const char **pzErrMsg, |
| 4937 | ** const struct sqlite3_api_routines *pThunk |
| 4938 | ** ); |
| 4939 | ** </pre></blockquote>)^ |
| 4940 | ** |
| 4941 | ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg |
| 4942 | ** point to an appropriate error message (obtained from [sqlite3_mprintf()]) |
| 4943 | ** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg |
| 4944 | ** is NULL before calling the xEntryPoint(). ^SQLite will invoke |
| 4945 | ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any |
| 4946 | ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], |
| 4947 | ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. |
| 4948 | ** |
| 4949 | ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already |
| 4950 | ** on the list of automatic extensions is a harmless no-op. ^No entry point |
| 4951 | ** will be called more than once for each database connection that is opened. |
| 4952 | ** |
| 4953 | ** See also: [sqlite3_reset_auto_extension()]. |
| 4954 | */ |
| 4955 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4956 | |
| 4957 | /* |
| 4958 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4959 | ** |
| 4960 | ** ^This interface disables all automatic extensions previously |
| 4961 | ** registered using [sqlite3_auto_extension()]. |
| 4962 | */ |
| 4963 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4964 | |
| 4965 | /* |
| 4966 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -5443,11 +5595,11 @@ | |
| 5595 | ** output variable when querying the system for the current mutex |
| 5596 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 5597 | ** |
| 5598 | ** ^The xMutexInit method defined by this structure is invoked as |
| 5599 | ** part of system initialization by the sqlite3_initialize() function. |
| 5600 | ** ^The xMutexInit routine is called by SQLite exactly once for each |
| 5601 | ** effective call to [sqlite3_initialize()]. |
| 5602 | ** |
| 5603 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 5604 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 5605 | ** implementation of this method is expected to release all outstanding |
| @@ -5640,11 +5792,12 @@ | |
| 5792 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5793 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5794 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5795 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5796 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5797 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 18 |
| 5798 | #define SQLITE_TESTCTRL_LAST 18 |
| 5799 | |
| 5800 | /* |
| 5801 | ** CAPI3REF: SQLite Runtime Status |
| 5802 | ** |
| 5803 | ** ^This interface is used to retrieve runtime status information |
| @@ -5659,11 +5812,11 @@ | |
| 5812 | ** value. For those parameters |
| 5813 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5814 | ** ^(Other parameters record only the highwater mark and not the current |
| 5815 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5816 | ** |
| 5817 | ** ^The sqlite3_status() routine returns SQLITE_OK on success and a |
| 5818 | ** non-zero [error code] on failure. |
| 5819 | ** |
| 5820 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5821 | ** called while other threads are running the same or different SQLite |
| 5822 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5709,11 +5862,11 @@ | |
| 5862 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5863 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5864 | ** |
| 5865 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5866 | ** <dd>This parameter returns the number of bytes of page cache |
| 5867 | ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] |
| 5868 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5869 | ** returned value includes allocations that overflowed because they |
| 5870 | ** where too large (they were larger than the "sz" parameter to |
| 5871 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5872 | ** no space was left in the page cache.</dd>)^ |
| @@ -5732,11 +5885,11 @@ | |
| 5885 | ** outstanding at time, this parameter also reports the number of threads |
| 5886 | ** using scratch memory at the same time.</dd>)^ |
| 5887 | ** |
| 5888 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5889 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5890 | ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] |
| 5891 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5892 | ** returned include overflows because the requested allocation was too |
| 5893 | ** larger (that is, because the requested allocation was larger than the |
| 5894 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5895 | ** slots were available. |
| @@ -5780,10 +5933,13 @@ | |
| 5933 | ** |
| 5934 | ** ^The current value of the requested parameter is written into *pCur |
| 5935 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5936 | ** the resetFlg is true, then the highest instantaneous value is |
| 5937 | ** reset back down to the current value. |
| 5938 | ** |
| 5939 | ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a |
| 5940 | ** non-zero [error code] on failure. |
| 5941 | ** |
| 5942 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5943 | */ |
| 5944 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5945 | |
| @@ -5907,122 +6063,134 @@ | |
| 6063 | ** CAPI3REF: Application Defined Page Cache. |
| 6064 | ** KEYWORDS: {page cache} |
| 6065 | ** |
| 6066 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 6067 | ** register an alternative page cache implementation by passing in an |
| 6068 | ** instance of the sqlite3_pcache_methods structure.)^ |
| 6069 | ** In many applications, most of the heap memory allocated by |
| 6070 | ** SQLite is used for the page cache. |
| 6071 | ** By implementing a |
| 6072 | ** custom page cache using this API, an application can better control |
| 6073 | ** the amount of memory consumed by SQLite, the way in which |
| 6074 | ** that memory is allocated and released, and the policies used to |
| 6075 | ** determine exactly which parts of a database file are cached and for |
| 6076 | ** how long. |
| 6077 | ** |
| 6078 | ** The alternative page cache mechanism is an |
| 6079 | ** extreme measure that is only needed by the most demanding applications. |
| 6080 | ** The built-in page cache is recommended for most uses. |
| 6081 | ** |
| 6082 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 6083 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 6084 | ** the application may discard the parameter after the call to |
| 6085 | ** [sqlite3_config()] returns.)^ |
| 6086 | ** |
| 6087 | ** ^(The xInit() method is called once for each effective |
| 6088 | ** call to [sqlite3_initialize()])^ |
| 6089 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 6090 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 6091 | ** The intent of the xInit() method is to set up global data structures |
| 6092 | ** required by the custom page cache implementation. |
| 6093 | ** ^(If the xInit() method is NULL, then the |
| 6094 | ** built-in default page cache is used instead of the application defined |
| 6095 | ** page cache.)^ |
| 6096 | ** |
| 6097 | ** ^The xShutdown() method is called by [sqlite3_shutdown()]. |
| 6098 | ** It can be used to clean up |
| 6099 | ** any outstanding resources before process shutdown, if required. |
| 6100 | ** ^The xShutdown() method may be NULL. |
| 6101 | ** |
| 6102 | ** ^SQLite automatically serializes calls to the xInit method, |
| 6103 | ** so the xInit method need not be threadsafe. ^The |
| 6104 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 6105 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 6106 | ** in multithreaded applications. |
| 6107 | ** |
| 6108 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 6109 | ** call to xShutdown(). |
| 6110 | ** |
| 6111 | ** ^SQLite invokes the xCreate() method to construct a new cache instance. |
| 6112 | ** SQLite will typically create one cache instance for each open database file, |
| 6113 | ** though this is not guaranteed. ^The |
| 6114 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 6115 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 6116 | ** will the page size of the database file that is to be cached plus an |
| 6117 | ** increment (here called "R") of about 100 or 200. SQLite will use the |
| 6118 | ** extra R bytes on each page to store metadata about the underlying |
| 6119 | ** database page on disk. The value of R depends |
| 6120 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 6121 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 6122 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 6123 | ** be used to cache database pages of a file stored on disk, or |
| 6124 | ** false if it is used for an in-memory database. The cache implementation |
| 6125 | ** does not have to do anything special based with the value of bPurgeable; |
| 6126 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 6127 | ** never invoke xUnpin() except to deliberately delete a page. |
| 6128 | ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to |
| 6129 | ** false will always have the "discard" flag set to true. |
| 6130 | ** ^Hence, a cache created with bPurgeable false will |
| 6131 | ** never contain any unpinned pages. |
| 6132 | ** |
| 6133 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 6134 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 6135 | ** instance passed as the first argument. This is the value configured using |
| 6136 | ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable |
| 6137 | ** parameter, the implementation is not required to do anything with this |
| 6138 | ** value; it is advisory only. |
| 6139 | ** |
| 6140 | ** The xPagecount() method must return the number of pages currently |
| 6141 | ** stored in the cache, both pinned and unpinned. |
| 6142 | ** |
| 6143 | ** The xFetch() method locates a page in the cache and returns a pointer to |
| 6144 | ** the page, or a NULL pointer. |
| 6145 | ** A "page", in this context, means a buffer of szPage bytes aligned at an |
| 6146 | ** 8-byte boundary. The page to be fetched is determined by the key. ^The |
| 6147 | ** mimimum key value is 1. After it has been retrieved using xFetch, the page |
| 6148 | ** is considered to be "pinned". |
| 6149 | ** |
| 6150 | ** If the requested page is already in the page cache, then the page cache |
| 6151 | ** implementation must return a pointer to the page buffer with its content |
| 6152 | ** intact. If the requested page is not already in the cache, then the |
| 6153 | ** behavior of the cache implementation should use the value of the createFlag |
| 6154 | ** parameter to help it determined what action to take: |
| 6155 | ** |
| 6156 | ** <table border=1 width=85% align=center> |
| 6157 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 6158 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 6159 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 6160 | ** Otherwise return NULL. |
| 6161 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 6162 | ** NULL if allocating a new page is effectively impossible. |
| 6163 | ** </table> |
| 6164 | ** |
| 6165 | ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite |
| 6166 | ** will only use a createFlag of 2 after a prior call with a createFlag of 1 |
| 6167 | ** failed.)^ In between the to xFetch() calls, SQLite may |
| 6168 | ** attempt to unpin one or more cache pages by spilling the content of |
| 6169 | ** pinned pages to disk and synching the operating system disk cache. |
| 6170 | ** |
| 6171 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 6172 | ** as its second argument. If the third parameter, discard, is non-zero, |
| 6173 | ** then the page must be evicted from the cache. |
| 6174 | ** ^If the discard parameter is |
| 6175 | ** zero, then the page may be discarded or retained at the discretion of |
| 6176 | ** page cache implementation. ^The page cache implementation |
| 6177 | ** may choose to evict unpinned pages at any time. |
| 6178 | ** |
| 6179 | ** The cache must not perform any reference counting. A single |
| 6180 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 6181 | ** to xFetch(). |
| 6182 | ** |
| 6183 | ** The xRekey() method is used to change the key value associated with the |
| 6184 | ** page passed as the second argument. If the cache |
| 6185 | ** previously contains an entry associated with newKey, it must be |
| 6186 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 6187 | ** to be pinned. |
| 6188 | ** |
| 6189 | ** When SQLite calls the xTruncate() method, the cache must discard all |
| 6190 | ** existing cache entries with page numbers (keys) greater than or equal |
| 6191 | ** to the value of the iLimit parameter passed to xTruncate(). If any |
| 6192 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 6193 | ** they can be safely discarded. |
| 6194 | ** |
| 6195 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 6196 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -6496,10 +6664,66 @@ | |
| 6664 | #if 0 |
| 6665 | } /* End of the 'extern "C"' block */ |
| 6666 | #endif |
| 6667 | #endif |
| 6668 | |
| 6669 | /* |
| 6670 | ** 2010 August 30 |
| 6671 | ** |
| 6672 | ** The author disclaims copyright to this source code. In place of |
| 6673 | ** a legal notice, here is a blessing: |
| 6674 | ** |
| 6675 | ** May you do good and not evil. |
| 6676 | ** May you find forgiveness for yourself and forgive others. |
| 6677 | ** May you share freely, never taking more than you give. |
| 6678 | ** |
| 6679 | ************************************************************************* |
| 6680 | */ |
| 6681 | |
| 6682 | #ifndef _SQLITE3RTREE_H_ |
| 6683 | #define _SQLITE3RTREE_H_ |
| 6684 | |
| 6685 | |
| 6686 | #if 0 |
| 6687 | extern "C" { |
| 6688 | #endif |
| 6689 | |
| 6690 | typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; |
| 6691 | |
| 6692 | /* |
| 6693 | ** Register a geometry callback named zGeom that can be used as part of an |
| 6694 | ** R-Tree geometry query as follows: |
| 6695 | ** |
| 6696 | ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...) |
| 6697 | */ |
| 6698 | SQLITE_API int sqlite3_rtree_geometry_callback( |
| 6699 | sqlite3 *db, |
| 6700 | const char *zGeom, |
| 6701 | int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), |
| 6702 | void *pContext |
| 6703 | ); |
| 6704 | |
| 6705 | |
| 6706 | /* |
| 6707 | ** A pointer to a structure of the following type is passed as the first |
| 6708 | ** argument to callbacks registered using rtree_geometry_callback(). |
| 6709 | */ |
| 6710 | struct sqlite3_rtree_geometry { |
| 6711 | void *pContext; /* Copy of pContext passed to s_r_g_c() */ |
| 6712 | int nParam; /* Size of array aParam[] */ |
| 6713 | double *aParam; /* Parameters passed to SQL geom function */ |
| 6714 | void *pUser; /* Callback implementation user data */ |
| 6715 | void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ |
| 6716 | }; |
| 6717 | |
| 6718 | |
| 6719 | #if 0 |
| 6720 | } /* end of the 'extern "C"' block */ |
| 6721 | #endif |
| 6722 | |
| 6723 | #endif /* ifndef _SQLITE3RTREE_H_ */ |
| 6724 | |
| 6725 | |
| 6726 | /************** End of sqlite3.h *********************************************/ |
| 6727 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 6728 | /************** Include hash.h in the middle of sqliteInt.h ******************/ |
| 6729 | /************** Begin file hash.h ********************************************/ |
| @@ -7066,10 +7290,11 @@ | |
| 7290 | typedef struct Schema Schema; |
| 7291 | typedef struct Expr Expr; |
| 7292 | typedef struct ExprList ExprList; |
| 7293 | typedef struct ExprSpan ExprSpan; |
| 7294 | typedef struct FKey FKey; |
| 7295 | typedef struct FuncDestructor FuncDestructor; |
| 7296 | typedef struct FuncDef FuncDef; |
| 7297 | typedef struct FuncDefHash FuncDefHash; |
| 7298 | typedef struct IdList IdList; |
| 7299 | typedef struct Index Index; |
| 7300 | typedef struct IndexSample IndexSample; |
| @@ -7172,16 +7397,15 @@ | |
| 7397 | ** following values. |
| 7398 | ** |
| 7399 | ** NOTE: These values must match the corresponding PAGER_ values in |
| 7400 | ** pager.h. |
| 7401 | */ |
| 7402 | #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ |
| 7403 | #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ |
| 7404 | #define BTREE_MEMORY 4 /* This is an in-memory DB */ |
| 7405 | #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */ |
| 7406 | #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */ |
| 7407 | |
| 7408 | SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); |
| 7409 | SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); |
| 7410 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int); |
| 7411 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| @@ -7213,15 +7437,21 @@ | |
| 7437 | SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *); |
| 7438 | |
| 7439 | SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *); |
| 7440 | |
| 7441 | /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR |
| 7442 | ** of the flags shown below. |
| 7443 | ** |
| 7444 | ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. |
| 7445 | ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data |
| 7446 | ** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With |
| 7447 | ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored |
| 7448 | ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL |
| 7449 | ** indices.) |
| 7450 | */ |
| 7451 | #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ |
| 7452 | #define BTREE_BLOBKEY 2 /* Table has keys only - no data */ |
| 7453 | |
| 7454 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); |
| 7455 | SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); |
| 7456 | SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); |
| 7457 | |
| @@ -7838,10 +8068,11 @@ | |
| 8068 | ** |
| 8069 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| 8070 | */ |
| 8071 | #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ |
| 8072 | #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ |
| 8073 | #define PAGER_MEMORY 0x0004 /* In-memory database */ |
| 8074 | |
| 8075 | /* |
| 8076 | ** Valid values for the second argument to sqlite3PagerLockingMode(). |
| 8077 | */ |
| 8078 | #define PAGER_LOCKINGMODE_QUERY -1 |
| @@ -8472,12 +8703,12 @@ | |
| 8703 | #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) |
| 8704 | #define sqlite3_mutex_free(X) |
| 8705 | #define sqlite3_mutex_enter(X) |
| 8706 | #define sqlite3_mutex_try(X) SQLITE_OK |
| 8707 | #define sqlite3_mutex_leave(X) |
| 8708 | #define sqlite3_mutex_held(X) ((void)(X),1) |
| 8709 | #define sqlite3_mutex_notheld(X) ((void)(X),1) |
| 8710 | #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) |
| 8711 | #define sqlite3MutexInit() SQLITE_OK |
| 8712 | #define sqlite3MutexEnd() |
| 8713 | #endif /* defined(SQLITE_MUTEX_OMIT) */ |
| 8714 | |
| @@ -8795,10 +9026,31 @@ | |
| 9026 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ |
| 9027 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ |
| 9028 | void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ |
| 9029 | char *zName; /* SQL name of the function. */ |
| 9030 | FuncDef *pHash; /* Next with a different name but the same hash */ |
| 9031 | FuncDestructor *pDestructor; /* Reference counted destructor function */ |
| 9032 | }; |
| 9033 | |
| 9034 | /* |
| 9035 | ** This structure encapsulates a user-function destructor callback (as |
| 9036 | ** configured using create_function_v2()) and a reference counter. When |
| 9037 | ** create_function_v2() is called to create a function with a destructor, |
| 9038 | ** a single object of this type is allocated. FuncDestructor.nRef is set to |
| 9039 | ** the number of FuncDef objects created (either 1 or 3, depending on whether |
| 9040 | ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor |
| 9041 | ** member of each of the new FuncDef objects is set to point to the allocated |
| 9042 | ** FuncDestructor. |
| 9043 | ** |
| 9044 | ** Thereafter, when one of the FuncDef objects is deleted, the reference |
| 9045 | ** count on this object is decremented. When it reaches 0, the destructor |
| 9046 | ** is invoked and the FuncDestructor structure freed. |
| 9047 | */ |
| 9048 | struct FuncDestructor { |
| 9049 | int nRef; |
| 9050 | void (*xDestroy)(void *); |
| 9051 | void *pUserData; |
| 9052 | }; |
| 9053 | |
| 9054 | /* |
| 9055 | ** Possible values for FuncDef.flags |
| 9056 | */ |
| @@ -8835,19 +9087,19 @@ | |
| 9087 | ** FuncDef.flags variable is set to the value passed as the flags |
| 9088 | ** parameter. |
| 9089 | */ |
| 9090 | #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ |
| 9091 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 9092 | SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} |
| 9093 | #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ |
| 9094 | {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ |
| 9095 | pArg, 0, xFunc, 0, 0, #zName, 0, 0} |
| 9096 | #define LIKEFUNC(zName, nArg, arg, flags) \ |
| 9097 | {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0} |
| 9098 | #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ |
| 9099 | {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \ |
| 9100 | SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} |
| 9101 | |
| 9102 | /* |
| 9103 | ** All current savepoints are stored in a linked list starting at |
| 9104 | ** sqlite3.pSavepoint. The first element in the list is the most recently |
| 9105 | ** opened savepoint. Savepoints are added to the list by the vdbe |
| @@ -9063,10 +9315,11 @@ | |
| 9315 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9316 | int nCol; /* Number of columns in this table */ |
| 9317 | Column *aCol; /* Information about each column */ |
| 9318 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9319 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9320 | unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9321 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9322 | u16 nRef; /* Number of pointers to this Table */ |
| 9323 | u8 tabFlags; /* Mask of TF_* values */ |
| 9324 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9325 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| @@ -10341,11 +10594,11 @@ | |
| 10594 | SQLITE_PRIVATE void sqlite3ScratchFree(void*); |
| 10595 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 10596 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 10597 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| 10598 | SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); |
| 10599 | SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); |
| 10600 | |
| 10601 | /* |
| 10602 | ** On systems with ample stack space and that support alloca(), make |
| 10603 | ** use of alloca() to obtain space for large automatic objects. By default, |
| 10604 | ** obtain space from malloc(). |
| @@ -10512,11 +10765,10 @@ | |
| 10765 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| 10766 | SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int); |
| 10767 | SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int); |
| 10768 | SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); |
| 10769 | SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); |
| 10770 | SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int); |
| 10771 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); |
| 10772 | SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); |
| 10773 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 10774 | SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*); |
| @@ -10632,12 +10884,10 @@ | |
| 10884 | # define sqlite3AuthContextPush(a,b,c) |
| 10885 | # define sqlite3AuthContextPop(a) ((void)(a)) |
| 10886 | #endif |
| 10887 | SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*); |
| 10888 | SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*); |
| 10889 | SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); |
| 10890 | SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); |
| 10891 | SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); |
| 10892 | SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); |
| 10893 | SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*); |
| @@ -10759,11 +11009,13 @@ | |
| 11009 | SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *); |
| 11010 | SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *); |
| 11011 | SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *); |
| 11012 | SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, |
| 11013 | void (*)(sqlite3_context*,int,sqlite3_value **), |
| 11014 | void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), |
| 11015 | FuncDestructor *pDestructor |
| 11016 | ); |
| 11017 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 11018 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 11019 | |
| 11020 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 11021 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| @@ -11679,10 +11931,11 @@ | |
| 11931 | Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 11932 | Bool nullRow; /* True if pointing to a row with no data */ |
| 11933 | Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 11934 | Bool isTable; /* True if a table requiring integer keys */ |
| 11935 | Bool isIndex; /* True if an index containing keys only - no data */ |
| 11936 | Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */ |
| 11937 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 11938 | Btree *pBt; /* Separate file holding temporary table */ |
| 11939 | int pseudoTableReg; /* Register holding pseudotable content. */ |
| 11940 | KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 11941 | int nField; /* Number of fields in the header */ |
| @@ -11773,10 +12026,14 @@ | |
| 12026 | char *z; /* String or BLOB value */ |
| 12027 | int n; /* Number of characters in string value, excluding '\0' */ |
| 12028 | u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 12029 | u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ |
| 12030 | u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 12031 | #ifdef SQLITE_DEBUG |
| 12032 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 12033 | void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ |
| 12034 | #endif |
| 12035 | void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ |
| 12036 | char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ |
| 12037 | }; |
| 12038 | |
| 12039 | /* One or more of the following flags are set to indicate the validOK |
| @@ -11799,10 +12056,11 @@ | |
| 12056 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 12057 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 12058 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 12059 | #define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 12060 | #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 12061 | #define MEM_Invalid 0x0080 /* Value is undefined */ |
| 12062 | #define MEM_TypeMask 0x00ff /* Mask of type bits */ |
| 12063 | |
| 12064 | /* Whenever Mem contains a valid string or blob representation, one of |
| 12065 | ** the following flags must be set to determine the memory management |
| 12066 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| @@ -11812,23 +12070,29 @@ | |
| 12070 | #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 12071 | #define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 12072 | #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 12073 | #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 12074 | #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 12075 | #ifdef SQLITE_OMIT_INCRBLOB |
| 12076 | #undef MEM_Zero |
| 12077 | #define MEM_Zero 0x0000 |
| 12078 | #endif |
| 12079 | |
| 12080 | /* |
| 12081 | ** Clear any existing type flags from a Mem and replace them with f |
| 12082 | */ |
| 12083 | #define MemSetTypeFlag(p, f) \ |
| 12084 | ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) |
| 12085 | |
| 12086 | /* |
| 12087 | ** Return true if a memory cell is not marked as invalid. This macro |
| 12088 | ** is for use inside assert() statements only. |
| 12089 | */ |
| 12090 | #ifdef SQLITE_DEBUG |
| 12091 | #define memIsValid(M) ((M)->flags & MEM_Invalid)==0 |
| 12092 | #endif |
| 12093 | |
| 12094 | |
| 12095 | /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains |
| 12096 | ** additional information about auxiliary information bound to arguments |
| 12097 | ** of the function. This is used to implement the sqlite3_get_auxdata() |
| 12098 | ** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data |
| @@ -12012,10 +12276,14 @@ | |
| 12276 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12277 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12278 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12279 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12280 | SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12281 | |
| 12282 | #ifdef SQLITE_DEBUG |
| 12283 | SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*); |
| 12284 | #endif |
| 12285 | |
| 12286 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 12287 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 12288 | #else |
| 12289 | # define sqlite3VdbeCheckFk(p,i) 0 |
| @@ -13518,10 +13786,16 @@ | |
| 13786 | SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 13787 | return pVfs->xSleep(pVfs, nMicro); |
| 13788 | } |
| 13789 | SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 13790 | int rc; |
| 13791 | /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() |
| 13792 | ** method to get the current date and time if that method is available |
| 13793 | ** (if iVersion is 2 or greater and the function pointer is not NULL) and |
| 13794 | ** will fall back to xCurrentTime() if xCurrentTimeInt64() is |
| 13795 | ** unavailable. |
| 13796 | */ |
| 13797 | if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ |
| 13798 | rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); |
| 13799 | }else{ |
| 13800 | double r; |
| 13801 | rc = pVfs->xCurrentTime(pVfs, &r); |
| @@ -13901,11 +14175,11 @@ | |
| 14175 | ** routines and redirected to xFree. |
| 14176 | */ |
| 14177 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 14178 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
| 14179 | assert( pPrior!=0 && nByte>0 ); |
| 14180 | assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ |
| 14181 | p--; |
| 14182 | p = realloc(p, nByte+8 ); |
| 14183 | if( p ){ |
| 14184 | p[0] = nByte; |
| 14185 | p++; |
| @@ -14307,10 +14581,11 @@ | |
| 14581 | */ |
| 14582 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 14583 | struct MemBlockHdr *pOldHdr; |
| 14584 | void *pNew; |
| 14585 | assert( mem.disallow==0 ); |
| 14586 | assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ |
| 14587 | pOldHdr = sqlite3MemsysGetHeader(pPrior); |
| 14588 | pNew = sqlite3MemMalloc(nByte); |
| 14589 | if( pNew ){ |
| 14590 | memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize); |
| 14591 | if( nByte>pOldHdr->iSize ){ |
| @@ -15576,11 +15851,11 @@ | |
| 15851 | */ |
| 15852 | static void *memsys5Realloc(void *pPrior, int nBytes){ |
| 15853 | int nOld; |
| 15854 | void *p; |
| 15855 | assert( pPrior!=0 ); |
| 15856 | assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ |
| 15857 | assert( nBytes>=0 ); |
| 15858 | if( nBytes==0 ){ |
| 15859 | return 0; |
| 15860 | } |
| 15861 | nOld = memsys5Size(pPrior); |
| @@ -17100,10 +17375,70 @@ | |
| 17375 | ************************************************************************* |
| 17376 | ** |
| 17377 | ** Memory allocation functions used throughout sqlite. |
| 17378 | */ |
| 17379 | |
| 17380 | /* |
| 17381 | ** Attempt to release up to n bytes of non-essential memory currently |
| 17382 | ** held by SQLite. An example of non-essential memory is memory used to |
| 17383 | ** cache database pages that are not currently in use. |
| 17384 | */ |
| 17385 | SQLITE_API int sqlite3_release_memory(int n){ |
| 17386 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 17387 | return sqlite3PcacheReleaseMemory(n); |
| 17388 | #else |
| 17389 | /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine |
| 17390 | ** is a no-op returning zero if SQLite is not compiled with |
| 17391 | ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ |
| 17392 | UNUSED_PARAMETER(n); |
| 17393 | return 0; |
| 17394 | #endif |
| 17395 | } |
| 17396 | |
| 17397 | /* |
| 17398 | ** An instance of the following object records the location of |
| 17399 | ** each unused scratch buffer. |
| 17400 | */ |
| 17401 | typedef struct ScratchFreeslot { |
| 17402 | struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ |
| 17403 | } ScratchFreeslot; |
| 17404 | |
| 17405 | /* |
| 17406 | ** State information local to the memory allocation subsystem. |
| 17407 | */ |
| 17408 | static SQLITE_WSD struct Mem0Global { |
| 17409 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 17410 | |
| 17411 | /* |
| 17412 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 17413 | ** be held while the callback is running. Recursive calls into |
| 17414 | ** the memory subsystem are allowed, but no new callbacks will be |
| 17415 | ** issued. |
| 17416 | */ |
| 17417 | sqlite3_int64 alarmThreshold; |
| 17418 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 17419 | void *alarmArg; |
| 17420 | |
| 17421 | /* |
| 17422 | ** Pointers to the end of sqlite3GlobalConfig.pScratch memory |
| 17423 | ** (so that a range test can be used to determine if an allocation |
| 17424 | ** being freed came from pScratch) and a pointer to the list of |
| 17425 | ** unused scratch allocations. |
| 17426 | */ |
| 17427 | void *pScratchEnd; |
| 17428 | ScratchFreeslot *pScratchFree; |
| 17429 | u32 nScratchFree; |
| 17430 | |
| 17431 | /* |
| 17432 | ** True if heap is nearly "full" where "full" is defined by the |
| 17433 | ** sqlite3_soft_heap_limit() setting. |
| 17434 | */ |
| 17435 | int nearlyFull; |
| 17436 | } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 17437 | |
| 17438 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
| 17439 | |
| 17440 | /* |
| 17441 | ** This routine runs when the memory allocator sees that the |
| 17442 | ** total memory allocation is about to exceed the soft heap |
| 17443 | ** limit. |
| 17444 | */ |
| @@ -17114,82 +17449,70 @@ | |
| 17449 | ){ |
| 17450 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 17451 | sqlite3_release_memory(allocSize); |
| 17452 | } |
| 17453 | |
| 17454 | /* |
| 17455 | ** Change the alarm callback |
| 17456 | */ |
| 17457 | static int sqlite3MemoryAlarm( |
| 17458 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 17459 | void *pArg, |
| 17460 | sqlite3_int64 iThreshold |
| 17461 | ){ |
| 17462 | int nUsed; |
| 17463 | sqlite3_mutex_enter(mem0.mutex); |
| 17464 | mem0.alarmCallback = xCallback; |
| 17465 | mem0.alarmArg = pArg; |
| 17466 | mem0.alarmThreshold = iThreshold; |
| 17467 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 17468 | mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed); |
| 17469 | sqlite3_mutex_leave(mem0.mutex); |
| 17470 | return SQLITE_OK; |
| 17471 | } |
| 17472 | |
| 17473 | #ifndef SQLITE_OMIT_DEPRECATED |
| 17474 | /* |
| 17475 | ** Deprecated external interface. Internal/core SQLite code |
| 17476 | ** should call sqlite3MemoryAlarm. |
| 17477 | */ |
| 17478 | SQLITE_API int sqlite3_memory_alarm( |
| 17479 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 17480 | void *pArg, |
| 17481 | sqlite3_int64 iThreshold |
| 17482 | ){ |
| 17483 | return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); |
| 17484 | } |
| 17485 | #endif |
| 17486 | |
| 17487 | /* |
| 17488 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 17489 | ** negative value indicates no limit. |
| 17490 | */ |
| 17491 | SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ |
| 17492 | sqlite3_int64 priorLimit; |
| 17493 | sqlite3_int64 excess; |
| 17494 | #ifndef SQLITE_OMIT_AUTOINIT |
| 17495 | sqlite3_initialize(); |
| 17496 | #endif |
| 17497 | sqlite3_mutex_enter(mem0.mutex); |
| 17498 | priorLimit = mem0.alarmThreshold; |
| 17499 | sqlite3_mutex_leave(mem0.mutex); |
| 17500 | if( n<0 ) return priorLimit; |
| 17501 | if( n>0 ){ |
| 17502 | sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n); |
| 17503 | }else{ |
| 17504 | sqlite3MemoryAlarm(0, 0, 0); |
| 17505 | } |
| 17506 | excess = sqlite3_memory_used() - n; |
| 17507 | if( excess>0 ) sqlite3_release_memory(excess & 0x7fffffff); |
| 17508 | return priorLimit; |
| 17509 | } |
| 17510 | SQLITE_API void sqlite3_soft_heap_limit(int n){ |
| 17511 | if( n<0 ) n = 0; |
| 17512 | sqlite3_soft_heap_limit64(n); |
| 17513 | } |
| 17514 | |
| 17515 | /* |
| 17516 | ** Initialize the memory allocation subsystem. |
| 17517 | */ |
| 17518 | SQLITE_PRIVATE int sqlite3MallocInit(void){ |
| @@ -17199,39 +17522,48 @@ | |
| 17522 | memset(&mem0, 0, sizeof(mem0)); |
| 17523 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 17524 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
| 17525 | } |
| 17526 | if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 |
| 17527 | && sqlite3GlobalConfig.nScratch>0 ){ |
| 17528 | int i, n, sz; |
| 17529 | ScratchFreeslot *pSlot; |
| 17530 | sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); |
| 17531 | sqlite3GlobalConfig.szScratch = sz; |
| 17532 | pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; |
| 17533 | n = sqlite3GlobalConfig.nScratch; |
| 17534 | mem0.pScratchFree = pSlot; |
| 17535 | mem0.nScratchFree = n; |
| 17536 | for(i=0; i<n-1; i++){ |
| 17537 | pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); |
| 17538 | pSlot = pSlot->pNext; |
| 17539 | } |
| 17540 | pSlot->pNext = 0; |
| 17541 | mem0.pScratchEnd = (void*)&pSlot[1]; |
| 17542 | }else{ |
| 17543 | mem0.pScratchEnd = 0; |
| 17544 | sqlite3GlobalConfig.pScratch = 0; |
| 17545 | sqlite3GlobalConfig.szScratch = 0; |
| 17546 | sqlite3GlobalConfig.nScratch = 0; |
| 17547 | } |
| 17548 | if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 |
| 17549 | || sqlite3GlobalConfig.nPage<1 ){ |
| 17550 | sqlite3GlobalConfig.pPage = 0; |
| 17551 | sqlite3GlobalConfig.szPage = 0; |
| 17552 | sqlite3GlobalConfig.nPage = 0; |
| 17553 | } |
| 17554 | return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
| 17555 | } |
| 17556 | |
| 17557 | /* |
| 17558 | ** Return true if the heap is currently under memory pressure - in other |
| 17559 | ** words if the amount of heap used is close to the limit set by |
| 17560 | ** sqlite3_soft_heap_limit(). |
| 17561 | */ |
| 17562 | SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ |
| 17563 | return mem0.nearlyFull; |
| 17564 | } |
| 17565 | |
| 17566 | /* |
| 17567 | ** Deinitialize the memory allocation subsystem. |
| 17568 | */ |
| 17569 | SQLITE_PRIVATE void sqlite3MallocEnd(void){ |
| @@ -17263,40 +17595,10 @@ | |
| 17595 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
| 17596 | res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ |
| 17597 | return res; |
| 17598 | } |
| 17599 | |
| 17600 | /* |
| 17601 | ** Trigger the alarm |
| 17602 | */ |
| 17603 | static void sqlite3MallocAlarm(int nByte){ |
| 17604 | void (*xCallback)(void*,sqlite3_int64,int); |
| @@ -17325,18 +17627,23 @@ | |
| 17627 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 17628 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 17629 | if( mem0.alarmCallback!=0 ){ |
| 17630 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 17631 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 17632 | mem0.nearlyFull = 1; |
| 17633 | sqlite3MallocAlarm(nFull); |
| 17634 | }else{ |
| 17635 | mem0.nearlyFull = 0; |
| 17636 | } |
| 17637 | } |
| 17638 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17639 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 17640 | if( p==0 && mem0.alarmCallback ){ |
| 17641 | sqlite3MallocAlarm(nFull); |
| 17642 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 17643 | } |
| 17644 | #endif |
| 17645 | if( p ){ |
| 17646 | nFull = sqlite3MallocSize(p); |
| 17647 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 17648 | sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 17649 | } |
| @@ -17348,11 +17655,13 @@ | |
| 17655 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 17656 | ** assumes the memory subsystem has already been initialized. |
| 17657 | */ |
| 17658 | SQLITE_PRIVATE void *sqlite3Malloc(int n){ |
| 17659 | void *p; |
| 17660 | if( n<=0 /* IMP: R-65312-04917 */ |
| 17661 | || n>=0x7fffff00 |
| 17662 | ){ |
| 17663 | /* A memory allocation of a number of bytes which is near the maximum |
| 17664 | ** signed integer value might cause an integer overflow inside of the |
| 17665 | ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 17666 | ** 255 bytes of overhead. SQLite itself will never use anything near |
| 17667 | ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
| @@ -17362,10 +17671,11 @@ | |
| 17671 | mallocWithAlarm(n, &p); |
| 17672 | sqlite3_mutex_leave(mem0.mutex); |
| 17673 | }else{ |
| 17674 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 17675 | } |
| 17676 | assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ |
| 17677 | return p; |
| 17678 | } |
| 17679 | |
| 17680 | /* |
| 17681 | ** This version of the memory allocation is for use by the application. |
| @@ -17399,64 +17709,70 @@ | |
| 17709 | ** embedded processor. |
| 17710 | */ |
| 17711 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ |
| 17712 | void *p; |
| 17713 | assert( n>0 ); |
| 17714 | |
| 17715 | sqlite3_mutex_enter(mem0.mutex); |
| 17716 | if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ |
| 17717 | p = mem0.pScratchFree; |
| 17718 | mem0.pScratchFree = mem0.pScratchFree->pNext; |
| 17719 | mem0.nScratchFree--; |
| 17720 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 17721 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 17722 | sqlite3_mutex_leave(mem0.mutex); |
| 17723 | }else{ |
| 17724 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17725 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 17726 | n = mallocWithAlarm(n, &p); |
| 17727 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 17728 | sqlite3_mutex_leave(mem0.mutex); |
| 17729 | }else{ |
| 17730 | sqlite3_mutex_leave(mem0.mutex); |
| 17731 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 17732 | } |
| 17733 | sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 17734 | } |
| 17735 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 17736 | |
| 17737 | |
| 17738 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17739 | /* Verify that no more than two scratch allocations per thread |
| 17740 | ** are outstanding at one time. (This is only checked in the |
| 17741 | ** single-threaded case since checking in the multi-threaded case |
| 17742 | ** would be much more complicated.) */ |
| 17743 | assert( scratchAllocOut<=1 ); |
| 17744 | if( p ) scratchAllocOut++; |
| 17745 | #endif |
| 17746 | |
| 17747 | return p; |
| 17748 | } |
| 17749 | SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ |
| 17750 | if( p ){ |
| 17751 | |
| 17752 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 17753 | /* Verify that no more than two scratch allocation per thread |
| 17754 | ** is outstanding at one time. (This is only checked in the |
| 17755 | ** single-threaded case since checking in the multi-threaded case |
| 17756 | ** would be much more complicated.) */ |
| 17757 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 17758 | scratchAllocOut--; |
| 17759 | #endif |
| 17760 | |
| 17761 | if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ |
| 17762 | /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ |
| 17763 | ScratchFreeslot *pSlot; |
| 17764 | pSlot = (ScratchFreeslot*)p; |
| 17765 | sqlite3_mutex_enter(mem0.mutex); |
| 17766 | pSlot->pNext = mem0.pScratchFree; |
| 17767 | mem0.pScratchFree = pSlot; |
| 17768 | mem0.nScratchFree++; |
| 17769 | assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch ); |
| 17770 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 17771 | sqlite3_mutex_leave(mem0.mutex); |
| 17772 | }else{ |
| 17773 | /* Release memory back to the heap */ |
| 17774 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 17775 | assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 17776 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 17777 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17778 | int iSize = sqlite3MallocSize(p); |
| @@ -17467,30 +17783,10 @@ | |
| 17783 | sqlite3GlobalConfig.m.xFree(p); |
| 17784 | sqlite3_mutex_leave(mem0.mutex); |
| 17785 | }else{ |
| 17786 | sqlite3GlobalConfig.m.xFree(p); |
| 17787 | } |
| 17788 | } |
| 17789 | } |
| 17790 | } |
| 17791 | |
| 17792 | /* |
| @@ -17527,11 +17823,11 @@ | |
| 17823 | |
| 17824 | /* |
| 17825 | ** Free memory previously obtained from sqlite3Malloc(). |
| 17826 | */ |
| 17827 | SQLITE_API void sqlite3_free(void *p){ |
| 17828 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 17829 | assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 17830 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17831 | if( sqlite3GlobalConfig.bMemstat ){ |
| 17832 | sqlite3_mutex_enter(mem0.mutex); |
| 17833 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| @@ -17574,21 +17870,24 @@ | |
| 17870 | */ |
| 17871 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){ |
| 17872 | int nOld, nNew; |
| 17873 | void *pNew; |
| 17874 | if( pOld==0 ){ |
| 17875 | return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */ |
| 17876 | } |
| 17877 | if( nBytes<=0 ){ |
| 17878 | sqlite3_free(pOld); /* IMP: R-31593-10574 */ |
| 17879 | return 0; |
| 17880 | } |
| 17881 | if( nBytes>=0x7fffff00 ){ |
| 17882 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 17883 | return 0; |
| 17884 | } |
| 17885 | nOld = sqlite3MallocSize(pOld); |
| 17886 | /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second |
| 17887 | ** argument to xRealloc is always a value returned by a prior call to |
| 17888 | ** xRoundup. */ |
| 17889 | nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); |
| 17890 | if( nOld==nNew ){ |
| 17891 | pNew = pOld; |
| 17892 | }else if( sqlite3GlobalConfig.bMemstat ){ |
| 17893 | sqlite3_mutex_enter(mem0.mutex); |
| @@ -17610,10 +17909,11 @@ | |
| 17909 | } |
| 17910 | sqlite3_mutex_leave(mem0.mutex); |
| 17911 | }else{ |
| 17912 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 17913 | } |
| 17914 | assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */ |
| 17915 | return pNew; |
| 17916 | } |
| 17917 | |
| 17918 | /* |
| 17919 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| @@ -19773,10 +20073,16 @@ | |
| 20073 | #define UpperToLower sqlite3UpperToLower |
| 20074 | |
| 20075 | /* |
| 20076 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 20077 | ** there is no consistency, we will define our own. |
| 20078 | ** |
| 20079 | ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows |
| 20080 | ** applications and extensions to compare the contents of two buffers |
| 20081 | ** containing UTF-8 strings in a case-independent fashion, using the same |
| 20082 | ** definition of case independence that SQLite uses internally when |
| 20083 | ** comparing identifiers. |
| 20084 | */ |
| 20085 | SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 20086 | register unsigned char *a, *b; |
| 20087 | a = (unsigned char *)zLeft; |
| 20088 | b = (unsigned char *)zRight; |
| @@ -26177,11 +26483,11 @@ | |
| 26483 | goto shmpage_out; |
| 26484 | } |
| 26485 | pShmNode->apRegion = apNew; |
| 26486 | while(pShmNode->nRegion<=iRegion){ |
| 26487 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
| 26488 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
| 26489 | ); |
| 26490 | if( pMem==MAP_FAILED ){ |
| 26491 | rc = SQLITE_IOERR; |
| 26492 | goto shmpage_out; |
| 26493 | } |
| @@ -28000,17 +28306,20 @@ | |
| 28306 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 28307 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 28308 | |
| 28309 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 28310 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 28311 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 28312 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
| 28313 | if( gethostuuid(pHostID, &timeout) ){ |
| 28314 | int err = errno; |
| 28315 | if( pError ){ |
| 28316 | *pError = err; |
| 28317 | } |
| 28318 | return SQLITE_IOERR; |
| 28319 | } |
| 28320 | #endif |
| 28321 | #ifdef SQLITE_TEST |
| 28322 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 28323 | if( sqlite3_hostid_num != 0){ |
| 28324 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 28325 | } |
| @@ -30339,10 +30648,18 @@ | |
| 30648 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN; |
| 30649 | } |
| 30650 | |
| 30651 | #ifndef SQLITE_OMIT_WAL |
| 30652 | |
| 30653 | /* |
| 30654 | ** Windows will only let you create file view mappings |
| 30655 | ** on allocation size granularity boundaries. |
| 30656 | ** During sqlite3_os_init() we do a GetSystemInfo() |
| 30657 | ** to get the granularity size. |
| 30658 | */ |
| 30659 | SYSTEM_INFO winSysInfo; |
| 30660 | |
| 30661 | /* |
| 30662 | ** Helper functions to obtain and relinquish the global mutex. The |
| 30663 | ** global mutex is used to protect the winLockInfo objects used by |
| 30664 | ** this file, all of which may be shared by multiple threads. |
| 30665 | ** |
| @@ -30507,19 +30824,26 @@ | |
| 30824 | ** by VFS shared-memory methods. |
| 30825 | */ |
| 30826 | static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ |
| 30827 | winShmNode **pp; |
| 30828 | winShmNode *p; |
| 30829 | BOOL bRc; |
| 30830 | assert( winShmMutexHeld() ); |
| 30831 | pp = &winShmNodeList; |
| 30832 | while( (p = *pp)!=0 ){ |
| 30833 | if( p->nRef==0 ){ |
| 30834 | int i; |
| 30835 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
| 30836 | for(i=0; i<p->nRegion; i++){ |
| 30837 | bRc = UnmapViewOfFile(p->aRegion[i].pMap); |
| 30838 | OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n", |
| 30839 | (int)GetCurrentProcessId(), i, |
| 30840 | bRc ? "ok" : "failed")); |
| 30841 | bRc = CloseHandle(p->aRegion[i].hMap); |
| 30842 | OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n", |
| 30843 | (int)GetCurrentProcessId(), i, |
| 30844 | bRc ? "ok" : "failed")); |
| 30845 | } |
| 30846 | if( p->hFile.h != INVALID_HANDLE_VALUE ){ |
| 30847 | SimulateIOErrorBenign(1); |
| 30848 | winClose((sqlite3_file *)&p->hFile); |
| 30849 | SimulateIOErrorBenign(0); |
| @@ -30592,14 +30916,15 @@ | |
| 30916 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 30917 | if( pShmNode->mutex==0 ){ |
| 30918 | rc = SQLITE_NOMEM; |
| 30919 | goto shm_open_err; |
| 30920 | } |
| 30921 | |
| 30922 | rc = winOpen(pDbFd->pVfs, |
| 30923 | pShmNode->zFilename, /* Name of the file (UTF-8) */ |
| 30924 | (sqlite3_file*)&pShmNode->hFile, /* File handle here */ |
| 30925 | SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */ |
| 30926 | 0); |
| 30927 | if( SQLITE_OK!=rc ){ |
| 30928 | rc = SQLITE_CANTOPEN_BKPT; |
| 30929 | goto shm_open_err; |
| 30930 | } |
| @@ -30903,14 +31228,22 @@ | |
| 31228 | void *pMap = 0; /* Mapped memory region */ |
| 31229 | |
| 31230 | hMap = CreateFileMapping(pShmNode->hFile.h, |
| 31231 | NULL, PAGE_READWRITE, 0, nByte, NULL |
| 31232 | ); |
| 31233 | OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n", |
| 31234 | (int)GetCurrentProcessId(), pShmNode->nRegion, nByte, |
| 31235 | hMap ? "ok" : "failed")); |
| 31236 | if( hMap ){ |
| 31237 | int iOffset = pShmNode->nRegion*szRegion; |
| 31238 | int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
| 31239 | pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, |
| 31240 | 0, iOffset - iOffsetShift, szRegion + iOffsetShift |
| 31241 | ); |
| 31242 | OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n", |
| 31243 | (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion, |
| 31244 | pMap ? "ok" : "failed")); |
| 31245 | } |
| 31246 | if( !pMap ){ |
| 31247 | pShmNode->lastErrno = GetLastError(); |
| 31248 | rc = SQLITE_IOERR; |
| 31249 | if( hMap ) CloseHandle(hMap); |
| @@ -30923,12 +31256,14 @@ | |
| 31256 | } |
| 31257 | } |
| 31258 | |
| 31259 | shmpage_out: |
| 31260 | if( pShmNode->nRegion>iRegion ){ |
| 31261 | int iOffset = iRegion*szRegion; |
| 31262 | int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
| 31263 | char *p = (char *)pShmNode->aRegion[iRegion].pMap; |
| 31264 | *pp = (void *)&p[iOffsetShift]; |
| 31265 | }else{ |
| 31266 | *pp = 0; |
| 31267 | } |
| 31268 | sqlite3_mutex_leave(pShmNode->mutex); |
| 31269 | return rc; |
| @@ -31151,13 +31486,64 @@ | |
| 31486 | DWORD dwFlagsAndAttributes = 0; |
| 31487 | #if SQLITE_OS_WINCE |
| 31488 | int isTemp = 0; |
| 31489 | #endif |
| 31490 | winFile *pFile = (winFile*)id; |
| 31491 | void *zConverted; /* Filename in OS encoding */ |
| 31492 | const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ |
| 31493 | |
| 31494 | /* If argument zPath is a NULL pointer, this function is required to open |
| 31495 | ** a temporary file. Use this buffer to store the file name in. |
| 31496 | */ |
| 31497 | char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */ |
| 31498 | |
| 31499 | int rc = SQLITE_OK; /* Function Return Code */ |
| 31500 | #if !defined(NDEBUG) || SQLITE_OS_WINCE |
| 31501 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
| 31502 | #endif |
| 31503 | |
| 31504 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 31505 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 31506 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 31507 | #ifndef NDEBUG |
| 31508 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 31509 | #endif |
| 31510 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 31511 | |
| 31512 | #ifndef NDEBUG |
| 31513 | int isOpenJournal = (isCreate && ( |
| 31514 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 31515 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 31516 | || eType==SQLITE_OPEN_WAL |
| 31517 | )); |
| 31518 | #endif |
| 31519 | |
| 31520 | /* Check the following statements are true: |
| 31521 | ** |
| 31522 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 31523 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 31524 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
| 31525 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
| 31526 | */ |
| 31527 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
| 31528 | assert(isCreate==0 || isReadWrite); |
| 31529 | assert(isExclusive==0 || isCreate); |
| 31530 | assert(isDelete==0 || isCreate); |
| 31531 | |
| 31532 | /* The main DB, main journal, WAL file and master journal are never |
| 31533 | ** automatically deleted. Nor are they ever temporary files. */ |
| 31534 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 31535 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 31536 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
| 31537 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
| 31538 | |
| 31539 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 31540 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 31541 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 31542 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
| 31543 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
| 31544 | ); |
| 31545 | |
| 31546 | assert( id!=0 ); |
| 31547 | UNUSED_PARAMETER(pVfs); |
| 31548 | |
| 31549 | pFile->h = INVALID_HANDLE_VALUE; |
| @@ -31164,11 +31550,12 @@ | |
| 31550 | |
| 31551 | /* If the second argument to this function is NULL, generate a |
| 31552 | ** temporary file name to use |
| 31553 | */ |
| 31554 | if( !zUtf8Name ){ |
| 31555 | assert(isDelete && !isOpenJournal); |
| 31556 | rc = getTempname(MAX_PATH+1, zTmpname); |
| 31557 | if( rc!=SQLITE_OK ){ |
| 31558 | return rc; |
| 31559 | } |
| 31560 | zUtf8Name = zTmpname; |
| 31561 | } |
| @@ -31177,33 +31564,35 @@ | |
| 31564 | zConverted = convertUtf8Filename(zUtf8Name); |
| 31565 | if( zConverted==0 ){ |
| 31566 | return SQLITE_NOMEM; |
| 31567 | } |
| 31568 | |
| 31569 | if( isReadWrite ){ |
| 31570 | dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 31571 | }else{ |
| 31572 | dwDesiredAccess = GENERIC_READ; |
| 31573 | } |
| 31574 | |
| 31575 | /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is |
| 31576 | ** created. SQLite doesn't use it to indicate "exclusive access" |
| 31577 | ** as it is usually understood. |
| 31578 | */ |
| 31579 | if( isExclusive ){ |
| 31580 | /* Creates a new file, only if it does not already exist. */ |
| 31581 | /* If the file exists, it fails. */ |
| 31582 | dwCreationDisposition = CREATE_NEW; |
| 31583 | }else if( isCreate ){ |
| 31584 | /* Open existing file, or create if it doesn't exist */ |
| 31585 | dwCreationDisposition = OPEN_ALWAYS; |
| 31586 | }else{ |
| 31587 | /* Opens a file, only if it exists. */ |
| 31588 | dwCreationDisposition = OPEN_EXISTING; |
| 31589 | } |
| 31590 | |
| 31591 | dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 31592 | |
| 31593 | if( isDelete ){ |
| 31594 | #if SQLITE_OS_WINCE |
| 31595 | dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
| 31596 | isTemp = 1; |
| 31597 | #else |
| 31598 | dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| @@ -31216,10 +31605,11 @@ | |
| 31605 | /* Reports from the internet are that performance is always |
| 31606 | ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
| 31607 | #if SQLITE_OS_WINCE |
| 31608 | dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
| 31609 | #endif |
| 31610 | |
| 31611 | if( isNT() ){ |
| 31612 | h = CreateFileW((WCHAR*)zConverted, |
| 31613 | dwDesiredAccess, |
| 31614 | dwShareMode, |
| 31615 | NULL, |
| @@ -31241,41 +31631,45 @@ | |
| 31631 | dwFlagsAndAttributes, |
| 31632 | NULL |
| 31633 | ); |
| 31634 | #endif |
| 31635 | } |
| 31636 | |
| 31637 | OSTRACE(("OPEN %d %s 0x%lx %s\n", |
| 31638 | h, zName, dwDesiredAccess, |
| 31639 | h==INVALID_HANDLE_VALUE ? "failed" : "ok")); |
| 31640 | |
| 31641 | if( h==INVALID_HANDLE_VALUE ){ |
| 31642 | pFile->lastErrno = GetLastError(); |
| 31643 | free(zConverted); |
| 31644 | if( isReadWrite ){ |
| 31645 | return winOpen(pVfs, zName, id, |
| 31646 | ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); |
| 31647 | }else{ |
| 31648 | return SQLITE_CANTOPEN_BKPT; |
| 31649 | } |
| 31650 | } |
| 31651 | |
| 31652 | if( pOutFlags ){ |
| 31653 | if( isReadWrite ){ |
| 31654 | *pOutFlags = SQLITE_OPEN_READWRITE; |
| 31655 | }else{ |
| 31656 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 31657 | } |
| 31658 | } |
| 31659 | |
| 31660 | memset(pFile, 0, sizeof(*pFile)); |
| 31661 | pFile->pMethod = &winIoMethod; |
| 31662 | pFile->h = h; |
| 31663 | pFile->lastErrno = NO_ERROR; |
| 31664 | pFile->pVfs = pVfs; |
| 31665 | pFile->pShm = 0; |
| 31666 | pFile->zPath = zName; |
| 31667 | pFile->sectorSize = getSectorSize(pVfs, zUtf8Name); |
| 31668 | |
| 31669 | #if SQLITE_OS_WINCE |
| 31670 | if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB |
| 31671 | && !winceCreateLock(zName, pFile) |
| 31672 | ){ |
| 31673 | CloseHandle(h); |
| 31674 | free(zConverted); |
| 31675 | return SQLITE_CANTOPEN_BKPT; |
| @@ -31285,12 +31679,13 @@ | |
| 31679 | }else |
| 31680 | #endif |
| 31681 | { |
| 31682 | free(zConverted); |
| 31683 | } |
| 31684 | |
| 31685 | OpenCounter(+1); |
| 31686 | return rc; |
| 31687 | } |
| 31688 | |
| 31689 | /* |
| 31690 | ** Delete the named file. |
| 31691 | ** |
| @@ -31804,10 +32199,17 @@ | |
| 32199 | winSleep, /* xSleep */ |
| 32200 | winCurrentTime, /* xCurrentTime */ |
| 32201 | winGetLastError, /* xGetLastError */ |
| 32202 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 32203 | }; |
| 32204 | |
| 32205 | #ifndef SQLITE_OMIT_WAL |
| 32206 | /* get memory map allocation granularity */ |
| 32207 | memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); |
| 32208 | GetSystemInfo(&winSysInfo); |
| 32209 | assert(winSysInfo.dwAllocationGranularity > 0); |
| 32210 | #endif |
| 32211 | |
| 32212 | sqlite3_vfs_register(&winVfs, 1); |
| 32213 | return SQLITE_OK; |
| 32214 | } |
| 32215 | SQLITE_API int sqlite3_os_end(void){ |
| @@ -32369,16 +32771,20 @@ | |
| 32771 | ** Initialize and shutdown the page cache subsystem. Neither of these |
| 32772 | ** functions are threadsafe. |
| 32773 | */ |
| 32774 | SQLITE_PRIVATE int sqlite3PcacheInitialize(void){ |
| 32775 | if( sqlite3GlobalConfig.pcache.xInit==0 ){ |
| 32776 | /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the |
| 32777 | ** built-in default page cache is used instead of the application defined |
| 32778 | ** page cache. */ |
| 32779 | sqlite3PCacheSetDefault(); |
| 32780 | } |
| 32781 | return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg); |
| 32782 | } |
| 32783 | SQLITE_PRIVATE void sqlite3PcacheShutdown(void){ |
| 32784 | if( sqlite3GlobalConfig.pcache.xShutdown ){ |
| 32785 | /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ |
| 32786 | sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg); |
| 32787 | } |
| 32788 | } |
| 32789 | |
| 32790 | /* |
| @@ -32835,12 +33241,17 @@ | |
| 33241 | |
| 33242 | typedef struct PCache1 PCache1; |
| 33243 | typedef struct PgHdr1 PgHdr1; |
| 33244 | typedef struct PgFreeslot PgFreeslot; |
| 33245 | |
| 33246 | /* Each page cache is an instance of the following object. Every |
| 33247 | ** open database file (including each in-memory database and each |
| 33248 | ** temporary or transient database) has a single page cache which |
| 33249 | ** is an instance of this object. |
| 33250 | ** |
| 33251 | ** Pointers to structures of this type are cast and returned as |
| 33252 | ** opaque sqlite3_pcache* handles. |
| 33253 | */ |
| 33254 | struct PCache1 { |
| 33255 | /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 33256 | ** flag (bPurgeable) are set when the cache is created. nMax may be |
| 33257 | ** modified at any time by a call to the pcache1CacheSize() method. |
| @@ -32896,10 +33307,13 @@ | |
| 33307 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 33308 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 33309 | |
| 33310 | /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */ |
| 33311 | int szSlot; /* Size of each free slot */ |
| 33312 | int nSlot; /* The number of pcache slots */ |
| 33313 | int nFreeSlot; /* Number of unused pcache slots */ |
| 33314 | int nReserve; /* Try to keep nFreeSlot above this */ |
| 33315 | void *pStart, *pEnd; /* Bounds of pagecache malloc range */ |
| 33316 | PgFreeslot *pFree; /* Free page blocks */ |
| 33317 | int isInit; /* True if initialized */ |
| 33318 | } pcache1_g; |
| 33319 | |
| @@ -32943,10 +33357,12 @@ | |
| 33357 | SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
| 33358 | if( pcache1.isInit ){ |
| 33359 | PgFreeslot *p; |
| 33360 | sz = ROUNDDOWN8(sz); |
| 33361 | pcache1.szSlot = sz; |
| 33362 | pcache1.nSlot = pcache1.nFreeSlot = n; |
| 33363 | pcache1.nReserve = n>90 ? 10 : (n/10 + 1); |
| 33364 | pcache1.pStart = pBuf; |
| 33365 | pcache1.pFree = 0; |
| 33366 | while( n-- ){ |
| 33367 | p = (PgFreeslot*)pBuf; |
| 33368 | p->pNext = pcache1.pFree; |
| @@ -32969,10 +33385,12 @@ | |
| 33385 | sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 33386 | if( nByte<=pcache1.szSlot && pcache1.pFree ){ |
| 33387 | assert( pcache1.isInit ); |
| 33388 | p = (PgHdr1 *)pcache1.pFree; |
| 33389 | pcache1.pFree = pcache1.pFree->pNext; |
| 33390 | pcache1.nFreeSlot--; |
| 33391 | assert( pcache1.nFreeSlot>=0 ); |
| 33392 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 33393 | }else{ |
| 33394 | |
| 33395 | /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the |
| 33396 | ** global pcache mutex and unlock the pager-cache object pCache. This is |
| @@ -33002,10 +33420,12 @@ | |
| 33420 | PgFreeslot *pSlot; |
| 33421 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 33422 | pSlot = (PgFreeslot*)p; |
| 33423 | pSlot->pNext = pcache1.pFree; |
| 33424 | pcache1.pFree = pSlot; |
| 33425 | pcache1.nFreeSlot++; |
| 33426 | assert( pcache1.nFreeSlot<=pcache1.nSlot ); |
| 33427 | }else{ |
| 33428 | int iSize; |
| 33429 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 33430 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 33431 | iSize = sqlite3MallocSize(p); |
| @@ -33014,11 +33434,11 @@ | |
| 33434 | } |
| 33435 | } |
| 33436 | |
| 33437 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 33438 | /* |
| 33439 | ** Return the size of a pcache allocation |
| 33440 | */ |
| 33441 | static int pcache1MemSize(void *p){ |
| 33442 | assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33443 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33444 | return pcache1.szSlot; |
| @@ -33087,10 +33507,36 @@ | |
| 33507 | pcache1EnterMutex(); |
| 33508 | pcache1Free(p); |
| 33509 | pcache1LeaveMutex(); |
| 33510 | } |
| 33511 | |
| 33512 | |
| 33513 | /* |
| 33514 | ** Return true if it desirable to avoid allocating a new page cache |
| 33515 | ** entry. |
| 33516 | ** |
| 33517 | ** If memory was allocated specifically to the page cache using |
| 33518 | ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then |
| 33519 | ** it is desirable to avoid allocating a new page cache entry because |
| 33520 | ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient |
| 33521 | ** for all page cache needs and we should not need to spill the |
| 33522 | ** allocation onto the heap. |
| 33523 | ** |
| 33524 | ** Or, the heap is used for all page cache memory put the heap is |
| 33525 | ** under memory pressure, then again it is desirable to avoid |
| 33526 | ** allocating a new page cache entry in order to avoid stressing |
| 33527 | ** the heap even further. |
| 33528 | */ |
| 33529 | static int pcache1UnderMemoryPressure(PCache1 *pCache){ |
| 33530 | assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33531 | if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){ |
| 33532 | return pcache1.nFreeSlot<pcache1.nReserve; |
| 33533 | }else{ |
| 33534 | return sqlite3HeapNearlyFull(); |
| 33535 | } |
| 33536 | } |
| 33537 | |
| 33538 | /******************************************************************************/ |
| 33539 | /******** General Implementation Functions ************************************/ |
| 33540 | |
| 33541 | /* |
| 33542 | ** This function is used to resize the hash table used by the cache passed |
| @@ -33328,18 +33774,20 @@ | |
| 33774 | ** copy of the requested page. If one is found, it is returned. |
| 33775 | ** |
| 33776 | ** 2. If createFlag==0 and the page is not already in the cache, NULL is |
| 33777 | ** returned. |
| 33778 | ** |
| 33779 | ** 3. If createFlag is 1, and the page is not already in the cache, then |
| 33780 | ** return NULL (do not allocate a new page) if any of the following |
| 33781 | ** conditions are true: |
| 33782 | ** |
| 33783 | ** (a) the number of pages pinned by the cache is greater than |
| 33784 | ** PCache1.nMax, or |
| 33785 | ** |
| 33786 | ** (b) the number of pages pinned by the cache is greater than |
| 33787 | ** the sum of nMax for all purgeable caches, less the sum of |
| 33788 | ** nMin for all other purgeable caches, or |
| 33789 | ** |
| 33790 | ** 4. If none of the first three conditions apply and the cache is marked |
| 33791 | ** as purgeable, and if one of the following is true: |
| 33792 | ** |
| 33793 | ** (a) The number of pages allocated for the cache is already |
| @@ -33346,10 +33794,13 @@ | |
| 33794 | ** PCache1.nMax, or |
| 33795 | ** |
| 33796 | ** (b) The number of pages allocated for all purgeable caches is |
| 33797 | ** already equal to or greater than the sum of nMax for all |
| 33798 | ** purgeable caches, |
| 33799 | ** |
| 33800 | ** (c) The system is under memory pressure and wants to avoid |
| 33801 | ** unnecessary pages cache entry allocations |
| 33802 | ** |
| 33803 | ** then attempt to recycle a page from the LRU list. If it is the right |
| 33804 | ** size, return the recycled buffer. Otherwise, free the buffer and |
| 33805 | ** proceed to step 5. |
| 33806 | ** |
| @@ -33378,10 +33829,11 @@ | |
| 33829 | /* Step 3 of header comment. */ |
| 33830 | nPinned = pCache->nPage - pCache->nRecyclable; |
| 33831 | if( createFlag==1 && ( |
| 33832 | nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage) |
| 33833 | || nPinned>=(pCache->nMax * 9 / 10) |
| 33834 | || pcache1UnderMemoryPressure(pCache) |
| 33835 | )){ |
| 33836 | goto fetch_out; |
| 33837 | } |
| 33838 | |
| 33839 | if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){ |
| @@ -33388,11 +33840,13 @@ | |
| 33840 | goto fetch_out; |
| 33841 | } |
| 33842 | |
| 33843 | /* Step 4. Try to recycle a page buffer if appropriate. */ |
| 33844 | if( pCache->bPurgeable && pcache1.pLruTail && ( |
| 33845 | (pCache->nPage+1>=pCache->nMax) |
| 33846 | || pcache1.nCurrentPage>=pcache1.nMaxPage |
| 33847 | || pcache1UnderMemoryPressure(pCache) |
| 33848 | )){ |
| 33849 | pPage = pcache1.pLruTail; |
| 33850 | pcache1RemoveFromHash(pPage); |
| 33851 | pcache1PinPage(pPage); |
| 33852 | if( pPage->pCache->szPage!=pCache->szPage ){ |
| @@ -33531,10 +33985,11 @@ | |
| 33985 | ** |
| 33986 | ** Destroy a cache allocated using pcache1Create(). |
| 33987 | */ |
| 33988 | static void pcache1Destroy(sqlite3_pcache *p){ |
| 33989 | PCache1 *pCache = (PCache1 *)p; |
| 33990 | assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); |
| 33991 | pcache1EnterMutex(); |
| 33992 | pcache1TruncateUnsafe(pCache, 0); |
| 33993 | pcache1.nMaxPage -= pCache->nMax; |
| 33994 | pcache1.nMinPage -= pCache->nMin; |
| 33995 | pcache1EnforceMaxPage(); |
| @@ -33578,11 +34033,11 @@ | |
| 34033 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 34034 | int nFree = 0; |
| 34035 | if( pcache1.pStart==0 ){ |
| 34036 | PgHdr1 *p; |
| 34037 | pcache1EnterMutex(); |
| 34038 | while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){ |
| 34039 | nFree += pcache1MemSize(PGHDR1_TO_PAGE(p)); |
| 34040 | pcache1PinPage(p); |
| 34041 | pcache1RemoveFromHash(p); |
| 34042 | pcache1FreePage(p); |
| 34043 | } |
| @@ -37120,16 +37575,17 @@ | |
| 37575 | ** the duplicate call is harmless. |
| 37576 | */ |
| 37577 | sqlite3WalEndReadTransaction(pPager->pWal); |
| 37578 | |
| 37579 | rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed); |
| 37580 | if( rc!=SQLITE_OK || changed ){ |
| 37581 | pager_reset(pPager); |
| 37582 | } |
| 37583 | |
| 37584 | return rc; |
| 37585 | } |
| 37586 | #endif |
| 37587 | |
| 37588 | /* |
| 37589 | ** This function is called as part of the transition from PAGER_OPEN |
| 37590 | ** to PAGER_READER state to determine the size of the database file |
| 37591 | ** in pages (assuming the page size currently stored in Pager.pageSize). |
| @@ -37182,11 +37638,11 @@ | |
| 37638 | |
| 37639 | *pnPage = nPage; |
| 37640 | return SQLITE_OK; |
| 37641 | } |
| 37642 | |
| 37643 | #ifndef SQLITE_OMIT_WAL |
| 37644 | /* |
| 37645 | ** Check if the *-wal file that corresponds to the database opened by pPager |
| 37646 | ** exists if the database is not empy, or verify that the *-wal file does |
| 37647 | ** not exist (by deleting it) if the database file is empty. |
| 37648 | ** |
| @@ -38374,10 +38830,17 @@ | |
| 38830 | journalFileSize = ROUND8(sqlite3MemJournalSize()); |
| 38831 | } |
| 38832 | |
| 38833 | /* Set the output variable to NULL in case an error occurs. */ |
| 38834 | *ppPager = 0; |
| 38835 | |
| 38836 | #ifndef SQLITE_OMIT_MEMORYDB |
| 38837 | if( flags & PAGER_MEMORY ){ |
| 38838 | memDb = 1; |
| 38839 | zFilename = 0; |
| 38840 | } |
| 38841 | #endif |
| 38842 | |
| 38843 | /* Compute and store the full pathname in an allocated buffer pointed |
| 38844 | ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 38845 | ** leave both nPathname and zPathname set to 0. |
| 38846 | */ |
| @@ -38385,21 +38848,12 @@ | |
| 38848 | nPathname = pVfs->mxPathname+1; |
| 38849 | zPathname = sqlite3Malloc(nPathname*2); |
| 38850 | if( zPathname==0 ){ |
| 38851 | return SQLITE_NOMEM; |
| 38852 | } |
| 38853 | zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ |
| 38854 | rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
| 38855 | nPathname = sqlite3Strlen30(zPathname); |
| 38856 | if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ |
| 38857 | /* This branch is taken when the journal path required by |
| 38858 | ** the database being opened will be more than pVfs->mxPathname |
| 38859 | ** bytes in length. This means the database cannot be opened, |
| @@ -38450,34 +38904,31 @@ | |
| 38904 | pPager->zFilename = (char*)(pPtr += journalFileSize); |
| 38905 | assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 38906 | |
| 38907 | /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ |
| 38908 | if( zPathname ){ |
| 38909 | assert( nPathname>0 ); |
| 38910 | pPager->zJournal = (char*)(pPtr += nPathname + 1); |
| 38911 | memcpy(pPager->zFilename, zPathname, nPathname); |
| 38912 | memcpy(pPager->zJournal, zPathname, nPathname); |
| 38913 | memcpy(&pPager->zJournal[nPathname], "-journal", 8); |
| 38914 | #ifndef SQLITE_OMIT_WAL |
| 38915 | pPager->zWal = &pPager->zJournal[nPathname+8+1]; |
| 38916 | memcpy(pPager->zWal, zPathname, nPathname); |
| 38917 | memcpy(&pPager->zWal[nPathname], "-wal", 4); |
| 38918 | #endif |
| 38919 | sqlite3_free(zPathname); |
| 38920 | } |
| 38921 | pPager->pVfs = pVfs; |
| 38922 | pPager->vfsFlags = vfsFlags; |
| 38923 | |
| 38924 | /* Open the pager file. |
| 38925 | */ |
| 38926 | if( zFilename && zFilename[0] ){ |
| 38927 | int fout = 0; /* VFS flags returned by xOpen() */ |
| 38928 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); |
| 38929 | assert( !memDb ); |
| 38930 | readOnly = (fout&SQLITE_OPEN_READONLY); |
| 38931 | |
| 38932 | /* If the file was successfully opened for read/write access, |
| 38933 | ** choose a default page size in case we have to create the |
| 38934 | ** database file. The default page size is the maximum of: |
| @@ -38927,11 +39378,13 @@ | |
| 39378 | |
| 39379 | /* If there is a WAL file in the file-system, open this database in WAL |
| 39380 | ** mode. Otherwise, the following function call is a no-op. |
| 39381 | */ |
| 39382 | rc = pagerOpenWalIfPresent(pPager); |
| 39383 | #ifndef SQLITE_OMIT_WAL |
| 39384 | assert( pPager->pWal==0 || rc==SQLITE_OK ); |
| 39385 | #endif |
| 39386 | } |
| 39387 | |
| 39388 | if( pagerUseWal(pPager) ){ |
| 39389 | assert( rc==SQLITE_OK ); |
| 39390 | rc = pagerBeginReadTransaction(pPager); |
| @@ -43292,11 +43745,11 @@ | |
| 43745 | WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); |
| 43746 | if( rc!=SQLITE_OK ){ |
| 43747 | return rc; |
| 43748 | } |
| 43749 | } |
| 43750 | assert( (int)pWal->szPage==szPage ); |
| 43751 | |
| 43752 | /* Write the log file. */ |
| 43753 | for(p=pList; p; p=p->pDirty){ |
| 43754 | u32 nDbsize; /* Db-size field for frame header */ |
| 43755 | i64 iOffset; /* Write offset in log file */ |
| @@ -43952,10 +44405,11 @@ | |
| 44405 | MemPage *pPage1; /* First page of the database */ |
| 44406 | u8 readOnly; /* True if the underlying file is readonly */ |
| 44407 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
| 44408 | u8 secureDelete; /* True if secure_delete is enabled */ |
| 44409 | u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 44410 | u8 openFlags; /* Flags to sqlite3BtreeOpen() */ |
| 44411 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 44412 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 44413 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 44414 | #endif |
| 44415 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| @@ -46202,15 +46656,24 @@ | |
| 46656 | |
| 46657 | /* |
| 46658 | ** Open a database file. |
| 46659 | ** |
| 46660 | ** zFilename is the name of the database file. If zFilename is NULL |
| 46661 | ** then an ephemeral database is created. The ephemeral database might |
| 46662 | ** be exclusively in memory, or it might use a disk-based memory cache. |
| 46663 | ** Either way, the ephemeral database will be automatically deleted |
| 46664 | ** when sqlite3BtreeClose() is called. |
| 46665 | ** |
| 46666 | ** If zFilename is ":memory:" then an in-memory database is created |
| 46667 | ** that is automatically destroyed when it is closed. |
| 46668 | ** |
| 46669 | ** The "flags" parameter is a bitmask that might contain bits |
| 46670 | ** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK. The BTREE_NO_READLOCK |
| 46671 | ** bit is also set if the SQLITE_NoReadlock flags is set in db->flags. |
| 46672 | ** These flags are passed through into sqlite3PagerOpen() and must |
| 46673 | ** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK. |
| 46674 | ** |
| 46675 | ** If the database is already opened in the same database connection |
| 46676 | ** and we are in shared cache mode, then the open will fail with an |
| 46677 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 46678 | ** objects in the same database connection since doing so will lead |
| 46679 | ** to problems with locking. |
| @@ -46227,10 +46690,13 @@ | |
| 46690 | Btree *p; /* Handle to return */ |
| 46691 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 46692 | int rc = SQLITE_OK; /* Result code from this function */ |
| 46693 | u8 nReserve; /* Byte of unused space on each page */ |
| 46694 | unsigned char zDbHeader[100]; /* Database header content */ |
| 46695 | |
| 46696 | /* True if opening an ephemeral, temporary database */ |
| 46697 | const int isTempDb = zFilename==0 || zFilename[0]==0; |
| 46698 | |
| 46699 | /* Set the variable isMemdb to true for an in-memory database, or |
| 46700 | ** false for a file-based database. This symbol is only required if |
| 46701 | ** either of the shared-data or autovacuum features are compiled |
| 46702 | ** into the library. |
| @@ -46237,17 +46703,34 @@ | |
| 46703 | */ |
| 46704 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 46705 | #ifdef SQLITE_OMIT_MEMORYDB |
| 46706 | const int isMemdb = 0; |
| 46707 | #else |
| 46708 | const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) |
| 46709 | || (isTempDb && sqlite3TempInMemory(db)); |
| 46710 | #endif |
| 46711 | #endif |
| 46712 | |
| 46713 | assert( db!=0 ); |
| 46714 | assert( sqlite3_mutex_held(db->mutex) ); |
| 46715 | assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ |
| 46716 | |
| 46717 | /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ |
| 46718 | assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); |
| 46719 | |
| 46720 | /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ |
| 46721 | assert( (flags & BTREE_SINGLE)==0 || isTempDb ); |
| 46722 | |
| 46723 | if( db->flags & SQLITE_NoReadlock ){ |
| 46724 | flags |= BTREE_NO_READLOCK; |
| 46725 | } |
| 46726 | if( isMemdb ){ |
| 46727 | flags |= BTREE_MEMORY; |
| 46728 | } |
| 46729 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ |
| 46730 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 46731 | } |
| 46732 | pVfs = db->pVfs; |
| 46733 | p = sqlite3MallocZero(sizeof(Btree)); |
| 46734 | if( !p ){ |
| 46735 | return SQLITE_NOMEM; |
| 46736 | } |
| @@ -46261,11 +46744,11 @@ | |
| 46744 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 46745 | /* |
| 46746 | ** If this Btree is a candidate for shared cache, try to find an |
| 46747 | ** existing BtShared object that we can share with |
| 46748 | */ |
| 46749 | if( isMemdb==0 && isTempDb==0 ){ |
| 46750 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
| 46751 | int nFullPathname = pVfs->mxPathname+1; |
| 46752 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
| 46753 | sqlite3_mutex *mutexShared; |
| 46754 | p->sharable = 1; |
| @@ -46336,10 +46819,11 @@ | |
| 46819 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 46820 | } |
| 46821 | if( rc!=SQLITE_OK ){ |
| 46822 | goto btree_open_out; |
| 46823 | } |
| 46824 | pBt->openFlags = (u8)flags; |
| 46825 | pBt->db = db; |
| 46826 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
| 46827 | p->pBt = pBt; |
| 46828 | |
| 46829 | pBt->pCursor = 0; |
| @@ -46440,10 +46924,18 @@ | |
| 46924 | sqlite3PagerClose(pBt->pPager); |
| 46925 | } |
| 46926 | sqlite3_free(pBt); |
| 46927 | sqlite3_free(p); |
| 46928 | *ppBtree = 0; |
| 46929 | }else{ |
| 46930 | /* If the B-Tree was successfully opened, set the pager-cache size to the |
| 46931 | ** default value. Except, when opening on an existing shared pager-cache, |
| 46932 | ** do not change the pager-cache size. |
| 46933 | */ |
| 46934 | if( sqlite3BtreeSchema(p, 0, 0)==0 ){ |
| 46935 | sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); |
| 46936 | } |
| 46937 | } |
| 46938 | if( mutexOpen ){ |
| 46939 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 46940 | sqlite3_mutex_leave(mutexOpen); |
| 46941 | } |
| @@ -51390,15 +51882,16 @@ | |
| 51882 | ** flags might not work: |
| 51883 | ** |
| 51884 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 51885 | ** BTREE_ZERODATA Used for SQL indices |
| 51886 | */ |
| 51887 | static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ |
| 51888 | BtShared *pBt = p->pBt; |
| 51889 | MemPage *pRoot; |
| 51890 | Pgno pgnoRoot; |
| 51891 | int rc; |
| 51892 | int ptfFlags; /* Page-type flage for the root page of new table */ |
| 51893 | |
| 51894 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 51895 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 51896 | assert( !pBt->readOnly ); |
| 51897 | |
| @@ -51513,12 +52006,18 @@ | |
| 52006 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
| 52007 | if( rc ) return rc; |
| 52008 | } |
| 52009 | #endif |
| 52010 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 52011 | if( createTabFlags & BTREE_INTKEY ){ |
| 52012 | ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; |
| 52013 | }else{ |
| 52014 | ptfFlags = PTF_ZERODATA | PTF_LEAF; |
| 52015 | } |
| 52016 | zeroPage(pRoot, ptfFlags); |
| 52017 | sqlite3PagerUnref(pRoot->pDbPage); |
| 52018 | assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); |
| 52019 | *piTable = (int)pgnoRoot; |
| 52020 | return SQLITE_OK; |
| 52021 | } |
| 52022 | SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 52023 | int rc; |
| @@ -52774,11 +53273,14 @@ | |
| 53273 | sqlite3Error( |
| 53274 | pDestDb, SQLITE_ERROR, "source and destination must be distinct" |
| 53275 | ); |
| 53276 | p = 0; |
| 53277 | }else { |
| 53278 | /* Allocate space for a new sqlite3_backup object... |
| 53279 | ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a |
| 53280 | ** call to sqlite3_backup_init() and is destroyed by a call to |
| 53281 | ** sqlite3_backup_finish(). */ |
| 53282 | p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); |
| 53283 | if( !p ){ |
| 53284 | sqlite3Error(pDestDb, SQLITE_NOMEM, 0); |
| 53285 | } |
| 53286 | } |
| @@ -53157,10 +53659,13 @@ | |
| 53659 | if( p->pDestDb ){ |
| 53660 | sqlite3_mutex_leave(p->pDestDb->mutex); |
| 53661 | } |
| 53662 | sqlite3BtreeLeave(p->pSrc); |
| 53663 | if( p->pDestDb ){ |
| 53664 | /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a |
| 53665 | ** call to sqlite3_backup_init() and is destroyed by a call to |
| 53666 | ** sqlite3_backup_finish(). */ |
| 53667 | sqlite3_free(p); |
| 53668 | } |
| 53669 | sqlite3_mutex_leave(mutex); |
| 53670 | return rc; |
| 53671 | } |
| @@ -53408,10 +53913,13 @@ | |
| 53913 | return SQLITE_NOMEM; |
| 53914 | } |
| 53915 | pMem->z[pMem->n] = 0; |
| 53916 | pMem->z[pMem->n+1] = 0; |
| 53917 | pMem->flags |= MEM_Term; |
| 53918 | #ifdef SQLITE_DEBUG |
| 53919 | pMem->pScopyFrom = 0; |
| 53920 | #endif |
| 53921 | } |
| 53922 | |
| 53923 | return SQLITE_OK; |
| 53924 | } |
| 53925 | |
| @@ -53528,11 +54036,11 @@ | |
| 54036 | memset(&ctx, 0, sizeof(ctx)); |
| 54037 | ctx.s.flags = MEM_Null; |
| 54038 | ctx.s.db = pMem->db; |
| 54039 | ctx.pMem = pMem; |
| 54040 | ctx.pFunc = pFunc; |
| 54041 | pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ |
| 54042 | assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); |
| 54043 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 54044 | memcpy(pMem, &ctx.s, sizeof(ctx.s)); |
| 54045 | rc = ctx.isError; |
| 54046 | } |
| @@ -53869,10 +54377,32 @@ | |
| 54377 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 54378 | } |
| 54379 | return 0; |
| 54380 | } |
| 54381 | |
| 54382 | #ifdef SQLITE_DEBUG |
| 54383 | /* |
| 54384 | ** This routine prepares a memory cell for modication by breaking |
| 54385 | ** its link to a shallow copy and by marking any current shallow |
| 54386 | ** copies of this cell as invalid. |
| 54387 | ** |
| 54388 | ** This is used for testing and debugging only - to make sure shallow |
| 54389 | ** copies are not misused. |
| 54390 | */ |
| 54391 | SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){ |
| 54392 | int i; |
| 54393 | Mem *pX; |
| 54394 | for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){ |
| 54395 | if( pX->pScopyFrom==pMem ){ |
| 54396 | pX->flags |= MEM_Invalid; |
| 54397 | pX->pScopyFrom = 0; |
| 54398 | } |
| 54399 | } |
| 54400 | pMem->pScopyFrom = 0; |
| 54401 | } |
| 54402 | #endif /* SQLITE_DEBUG */ |
| 54403 | |
| 54404 | /* |
| 54405 | ** Size of struct Mem not including the Mem.zMalloc member. |
| 54406 | */ |
| 54407 | #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) |
| 54408 | |
| @@ -54237,11 +54767,11 @@ | |
| 54767 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); |
| 54768 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ |
| 54769 | return 0; |
| 54770 | } |
| 54771 | } |
| 54772 | sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */ |
| 54773 | }else{ |
| 54774 | assert( (pVal->flags&MEM_Blob)==0 ); |
| 54775 | sqlite3VdbeMemStringify(pVal, enc); |
| 54776 | assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); |
| 54777 | } |
| @@ -56152,13 +56682,14 @@ | |
| 56682 | */ |
| 56683 | for(i=0; i<db->nDb; i++){ |
| 56684 | Btree *pBt = db->aDb[i].pBt; |
| 56685 | if( sqlite3BtreeIsInTrans(pBt) ){ |
| 56686 | char const *zFile = sqlite3BtreeGetJournalname(pBt); |
| 56687 | if( zFile==0 ){ |
| 56688 | continue; /* Ignore TEMP and :memory: databases */ |
| 56689 | } |
| 56690 | assert( zFile[0]!=0 ); |
| 56691 | if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
| 56692 | needSync = 1; |
| 56693 | } |
| 56694 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 56695 | offset += sqlite3Strlen30(zFile)+1; |
| @@ -57618,10 +58149,12 @@ | |
| 58149 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 58150 | */ |
| 58151 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 58152 | int rc; |
| 58153 | if( pStmt==0 ){ |
| 58154 | /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL |
| 58155 | ** pointer is a harmless no-op. */ |
| 58156 | rc = SQLITE_OK; |
| 58157 | }else{ |
| 58158 | Vdbe *v = (Vdbe*)pStmt; |
| 58159 | sqlite3 *db = v->db; |
| 58160 | #if SQLITE_THREADSAFE |
| @@ -57694,11 +58227,11 @@ | |
| 58227 | Mem *p = (Mem*)pVal; |
| 58228 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 58229 | sqlite3VdbeMemExpandBlob(p); |
| 58230 | p->flags &= ~MEM_Str; |
| 58231 | p->flags |= MEM_Blob; |
| 58232 | return p->n ? p->z : 0; |
| 58233 | }else{ |
| 58234 | return sqlite3_value_text(pVal); |
| 58235 | } |
| 58236 | } |
| 58237 | SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){ |
| @@ -58048,10 +58581,16 @@ | |
| 58581 | } |
| 58582 | |
| 58583 | /* |
| 58584 | ** Extract the user data from a sqlite3_context structure and return a |
| 58585 | ** pointer to it. |
| 58586 | ** |
| 58587 | ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface |
| 58588 | ** returns a copy of the pointer to the database connection (the 1st |
| 58589 | ** parameter) of the sqlite3_create_function() and |
| 58590 | ** sqlite3_create_function16() routines that originally registered the |
| 58591 | ** application defined function. |
| 58592 | */ |
| 58593 | SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
| 58594 | assert( p && p->pFunc ); |
| 58595 | return p->s.db; |
| 58596 | } |
| @@ -58257,12 +58796,11 @@ | |
| 58796 | ** sqlite3_column_text() |
| 58797 | ** sqlite3_column_text16() |
| 58798 | ** sqlite3_column_real() |
| 58799 | ** sqlite3_column_bytes() |
| 58800 | ** sqlite3_column_bytes16() |
| 58801 | ** sqiite3_column_blob() |
| 58802 | */ |
| 58803 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 58804 | { |
| 58805 | /* If malloc() failed during an encoding conversion within an |
| 58806 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| @@ -58526,10 +59064,16 @@ | |
| 59064 | pVar->flags = MEM_Null; |
| 59065 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 59066 | |
| 59067 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 59068 | ** binding a new value to this variable invalidates the current query plan. |
| 59069 | ** |
| 59070 | ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host |
| 59071 | ** parameter in the WHERE clause might influence the choice of query plan |
| 59072 | ** for a statement, then the statement will be automatically recompiled, |
| 59073 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 59074 | ** following any change to the bindings of that parameter. |
| 59075 | */ |
| 59076 | if( p->isPrepareV2 && |
| 59077 | ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) |
| 59078 | ){ |
| 59079 | p->expired = 1; |
| @@ -59023,10 +59567,21 @@ | |
| 59567 | ** of the code in this file is, therefore, important. See other comments |
| 59568 | ** in this file for details. If in doubt, do not deviate from existing |
| 59569 | ** commenting and indentation practices when changing or adding code. |
| 59570 | */ |
| 59571 | |
| 59572 | /* |
| 59573 | ** Invoke this macro on memory cells just prior to changing the |
| 59574 | ** value of the cell. This macro verifies that shallow copies are |
| 59575 | ** not misused. |
| 59576 | */ |
| 59577 | #ifdef SQLITE_DEBUG |
| 59578 | # define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M) |
| 59579 | #else |
| 59580 | # define memAboutToChange(P,M) |
| 59581 | #endif |
| 59582 | |
| 59583 | /* |
| 59584 | ** The following global variable is incremented every time a cursor |
| 59585 | ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test |
| 59586 | ** procedures use this information to make sure that indices are |
| 59587 | ** working correctly. This variable has no function other than to |
| @@ -60132,38 +60687,44 @@ | |
| 60687 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
| 60688 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 60689 | assert( pOp->p2>0 ); |
| 60690 | assert( pOp->p2<=p->nMem ); |
| 60691 | pOut = &aMem[pOp->p2]; |
| 60692 | memAboutToChange(p, pOut); |
| 60693 | sqlite3VdbeMemReleaseExternal(pOut); |
| 60694 | pOut->flags = MEM_Int; |
| 60695 | } |
| 60696 | |
| 60697 | /* Sanity checking on other operands */ |
| 60698 | #ifdef SQLITE_DEBUG |
| 60699 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 60700 | assert( pOp->p1>0 ); |
| 60701 | assert( pOp->p1<=p->nMem ); |
| 60702 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 60703 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 60704 | } |
| 60705 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 60706 | assert( pOp->p2>0 ); |
| 60707 | assert( pOp->p2<=p->nMem ); |
| 60708 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 60709 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 60710 | } |
| 60711 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 60712 | assert( pOp->p3>0 ); |
| 60713 | assert( pOp->p3<=p->nMem ); |
| 60714 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 60715 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 60716 | } |
| 60717 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 60718 | assert( pOp->p2>0 ); |
| 60719 | assert( pOp->p2<=p->nMem ); |
| 60720 | memAboutToChange(p, &aMem[pOp->p2]); |
| 60721 | } |
| 60722 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 60723 | assert( pOp->p3>0 ); |
| 60724 | assert( pOp->p3<=p->nMem ); |
| 60725 | memAboutToChange(p, &aMem[pOp->p3]); |
| 60726 | } |
| 60727 | #endif |
| 60728 | |
| 60729 | switch( pOp->opcode ){ |
| 60730 | |
| @@ -60221,10 +60782,11 @@ | |
| 60782 | ** and then jump to address P2. |
| 60783 | */ |
| 60784 | case OP_Gosub: { /* jump, in1 */ |
| 60785 | pIn1 = &aMem[pOp->p1]; |
| 60786 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 60787 | memAboutToChange(p, pIn1); |
| 60788 | pIn1->flags = MEM_Int; |
| 60789 | pIn1->u.i = pc; |
| 60790 | REGISTER_TRACE(pOp->p1, pIn1); |
| 60791 | pc = pOp->p2 - 1; |
| 60792 | break; |
| @@ -60428,15 +60990,11 @@ | |
| 60990 | |
| 60991 | |
| 60992 | /* Opcode: Blob P1 P2 * P4 |
| 60993 | ** |
| 60994 | ** P4 points to a blob of data P1 bytes long. Store this |
| 60995 | ** blob in register P2. |
| 60996 | */ |
| 60997 | case OP_Blob: { /* out2-prerelease */ |
| 60998 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
| 60999 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 61000 | pOut->enc = encoding; |
| @@ -60490,10 +61048,12 @@ | |
| 61048 | pIn1 = &aMem[u.ac.p1]; |
| 61049 | pOut = &aMem[u.ac.p2]; |
| 61050 | while( u.ac.n-- ){ |
| 61051 | assert( pOut<=&aMem[p->nMem] ); |
| 61052 | assert( pIn1<=&aMem[p->nMem] ); |
| 61053 | assert( memIsValid(pIn1) ); |
| 61054 | memAboutToChange(p, pOut); |
| 61055 | u.ac.zMalloc = pOut->zMalloc; |
| 61056 | pOut->zMalloc = 0; |
| 61057 | sqlite3VdbeMemMove(pOut, pIn1); |
| 61058 | pIn1->zMalloc = u.ac.zMalloc; |
| 61059 | REGISTER_TRACE(u.ac.p2++, pOut); |
| @@ -60535,10 +61095,13 @@ | |
| 61095 | case OP_SCopy: { /* in1, out2 */ |
| 61096 | pIn1 = &aMem[pOp->p1]; |
| 61097 | pOut = &aMem[pOp->p2]; |
| 61098 | assert( pOut!=pIn1 ); |
| 61099 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 61100 | #ifdef SQLITE_DEBUG |
| 61101 | if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; |
| 61102 | #endif |
| 61103 | REGISTER_TRACE(pOp->p2, pOut); |
| 61104 | break; |
| 61105 | } |
| 61106 | |
| 61107 | /* Opcode: ResultRow P1 P2 * * * |
| @@ -60595,10 +61158,14 @@ | |
| 61158 | ** and have an assigned type. The results are de-ephemeralized as |
| 61159 | ** as side effect. |
| 61160 | */ |
| 61161 | u.ad.pMem = p->pResultSet = &aMem[pOp->p1]; |
| 61162 | for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){ |
| 61163 | assert( memIsValid(&u.ad.pMem[u.ad.i]) ); |
| 61164 | Deephemeralize(&u.ad.pMem[u.ad.i]); |
| 61165 | assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0 |
| 61166 | || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 ); |
| 61167 | sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]); |
| 61168 | sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]); |
| 61169 | REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]); |
| 61170 | } |
| 61171 | if( db->mallocFailed ) goto no_mem; |
| @@ -60826,16 +61393,21 @@ | |
| 61393 | #endif /* local variables moved into u.ag */ |
| 61394 | |
| 61395 | u.ag.n = pOp->p5; |
| 61396 | u.ag.apVal = p->apArg; |
| 61397 | assert( u.ag.apVal || u.ag.n==0 ); |
| 61398 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 61399 | pOut = &aMem[pOp->p3]; |
| 61400 | memAboutToChange(p, pOut); |
| 61401 | |
| 61402 | assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) ); |
| 61403 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n ); |
| 61404 | u.ag.pArg = &aMem[pOp->p2]; |
| 61405 | for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){ |
| 61406 | assert( memIsValid(u.ag.pArg) ); |
| 61407 | u.ag.apVal[u.ag.i] = u.ag.pArg; |
| 61408 | Deephemeralize(u.ag.pArg); |
| 61409 | sqlite3VdbeMemStoreType(u.ag.pArg); |
| 61410 | REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg); |
| 61411 | } |
| 61412 | |
| 61413 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| @@ -60845,12 +61417,10 @@ | |
| 61417 | }else{ |
| 61418 | u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
| 61419 | u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc; |
| 61420 | } |
| 61421 | |
| 61422 | u.ag.ctx.s.flags = MEM_Null; |
| 61423 | u.ag.ctx.s.db = db; |
| 61424 | u.ag.ctx.s.xDel = 0; |
| 61425 | u.ag.ctx.s.zMalloc = 0; |
| 61426 | |
| @@ -60866,11 +61436,11 @@ | |
| 61436 | assert( pOp>aOp ); |
| 61437 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 61438 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 61439 | u.ag.ctx.pColl = pOp[-1].p4.pColl; |
| 61440 | } |
| 61441 | (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */ |
| 61442 | if( db->mallocFailed ){ |
| 61443 | /* Even though a malloc() has failed, the implementation of the |
| 61444 | ** user function may have called an sqlite3_result_XXX() function |
| 61445 | ** to return a value. The following call releases any resources |
| 61446 | ** associated with such a value. |
| @@ -60918,11 +61488,11 @@ | |
| 61488 | ** If either input is NULL, the result is NULL. |
| 61489 | */ |
| 61490 | /* Opcode: ShiftLeft P1 P2 P3 * * |
| 61491 | ** |
| 61492 | ** Shift the integer value in register P2 to the left by the |
| 61493 | ** number of bits specified by the integer in register P1. |
| 61494 | ** Store the result in register P3. |
| 61495 | ** If either input is NULL, the result is NULL. |
| 61496 | */ |
| 61497 | /* Opcode: ShiftRight P1 P2 P3 * * |
| 61498 | ** |
| @@ -60968,10 +61538,11 @@ | |
| 61538 | ** |
| 61539 | ** To force any register to be an integer, just add 0. |
| 61540 | */ |
| 61541 | case OP_AddImm: { /* in1 */ |
| 61542 | pIn1 = &aMem[pOp->p1]; |
| 61543 | memAboutToChange(p, pIn1); |
| 61544 | sqlite3VdbeMemIntegerify(pIn1); |
| 61545 | pIn1->u.i += pOp->p2; |
| 61546 | break; |
| 61547 | } |
| 61548 | |
| @@ -60982,10 +61553,11 @@ | |
| 61553 | ** without data loss, then jump immediately to P2, or if P2==0 |
| 61554 | ** raise an SQLITE_MISMATCH exception. |
| 61555 | */ |
| 61556 | case OP_MustBeInt: { /* jump, in1 */ |
| 61557 | pIn1 = &aMem[pOp->p1]; |
| 61558 | memAboutToChange(p, pIn1); |
| 61559 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 61560 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 61561 | if( pOp->p2==0 ){ |
| 61562 | rc = SQLITE_MISMATCH; |
| 61563 | goto abort_due_to_error; |
| @@ -61008,10 +61580,11 @@ | |
| 61580 | ** integers, for space efficiency, but after extraction we want them |
| 61581 | ** to have only a real value. |
| 61582 | */ |
| 61583 | case OP_RealAffinity: { /* in1 */ |
| 61584 | pIn1 = &aMem[pOp->p1]; |
| 61585 | memAboutToChange(p, pIn1); |
| 61586 | if( pIn1->flags & MEM_Int ){ |
| 61587 | sqlite3VdbeMemRealify(pIn1); |
| 61588 | } |
| 61589 | break; |
| 61590 | } |
| @@ -61027,10 +61600,11 @@ | |
| 61600 | ** |
| 61601 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61602 | */ |
| 61603 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
| 61604 | pIn1 = &aMem[pOp->p1]; |
| 61605 | memAboutToChange(p, pIn1); |
| 61606 | if( pIn1->flags & MEM_Null ) break; |
| 61607 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 61608 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 61609 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61610 | rc = ExpandBlob(pIn1); |
| @@ -61049,10 +61623,11 @@ | |
| 61623 | ** |
| 61624 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61625 | */ |
| 61626 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
| 61627 | pIn1 = &aMem[pOp->p1]; |
| 61628 | memAboutToChange(p, pIn1); |
| 61629 | if( pIn1->flags & MEM_Null ) break; |
| 61630 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 61631 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 61632 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
| 61633 | MemSetTypeFlag(pIn1, MEM_Blob); |
| @@ -61073,28 +61648,30 @@ | |
| 61648 | ** |
| 61649 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61650 | */ |
| 61651 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
| 61652 | pIn1 = &aMem[pOp->p1]; |
| 61653 | memAboutToChange(p, pIn1); |
| 61654 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 61655 | sqlite3VdbeMemNumerify(pIn1); |
| 61656 | } |
| 61657 | break; |
| 61658 | } |
| 61659 | #endif /* SQLITE_OMIT_CAST */ |
| 61660 | |
| 61661 | /* Opcode: ToInt P1 * * * * |
| 61662 | ** |
| 61663 | ** Force the value in register P1 to be an integer. If |
| 61664 | ** The value is currently a real number, drop its fractional part. |
| 61665 | ** If the value is text or blob, try to convert it to an integer using the |
| 61666 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 61667 | ** |
| 61668 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61669 | */ |
| 61670 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
| 61671 | pIn1 = &aMem[pOp->p1]; |
| 61672 | memAboutToChange(p, pIn1); |
| 61673 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61674 | sqlite3VdbeMemIntegerify(pIn1); |
| 61675 | } |
| 61676 | break; |
| 61677 | } |
| @@ -61109,10 +61686,11 @@ | |
| 61686 | ** |
| 61687 | ** A NULL value is not changed by this routine. It remains NULL. |
| 61688 | */ |
| 61689 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
| 61690 | pIn1 = &aMem[pOp->p1]; |
| 61691 | memAboutToChange(p, pIn1); |
| 61692 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 61693 | sqlite3VdbeMemRealify(pIn1); |
| 61694 | } |
| 61695 | break; |
| 61696 | } |
| @@ -61123,11 +61701,11 @@ | |
| 61701 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 61702 | ** jump to address P2. |
| 61703 | ** |
| 61704 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 61705 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 61706 | ** bit is clear then fall through if either operand is NULL. |
| 61707 | ** |
| 61708 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 61709 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 61710 | ** to coerce both inputs according to this affinity before the |
| 61711 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| @@ -61203,10 +61781,12 @@ | |
| 61781 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
| 61782 | #endif /* local variables moved into u.ai */ |
| 61783 | |
| 61784 | pIn1 = &aMem[pOp->p1]; |
| 61785 | pIn3 = &aMem[pOp->p3]; |
| 61786 | memAboutToChange(p, pIn1); |
| 61787 | memAboutToChange(p, pIn3); |
| 61788 | u.ai.flags1 = pIn1->flags; |
| 61789 | u.ai.flags3 = pIn3->flags; |
| 61790 | if( (pIn1->flags | pIn3->flags)&MEM_Null ){ |
| 61791 | /* One or both operands are NULL */ |
| 61792 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| @@ -61253,10 +61833,11 @@ | |
| 61833 | default: u.ai.res = u.ai.res>=0; break; |
| 61834 | } |
| 61835 | |
| 61836 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 61837 | pOut = &aMem[pOp->p2]; |
| 61838 | memAboutToChange(p, pOut); |
| 61839 | MemSetTypeFlag(pOut, MEM_Int); |
| 61840 | pOut->u.i = u.ai.res; |
| 61841 | REGISTER_TRACE(pOp->p2, pOut); |
| 61842 | }else if( u.ai.res ){ |
| 61843 | pc = pOp->p2-1; |
| @@ -61284,12 +61865,12 @@ | |
| 61865 | break; |
| 61866 | } |
| 61867 | |
| 61868 | /* Opcode: Compare P1 P2 P3 P4 * |
| 61869 | ** |
| 61870 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 61871 | ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 61872 | ** the comparison for use by the next OP_Jump instruct. |
| 61873 | ** |
| 61874 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 61875 | ** orders for the comparison. The permutation applies to registers |
| 61876 | ** only. The KeyInfo elements are used sequentially. |
| @@ -61327,10 +61908,12 @@ | |
| 61908 | assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 ); |
| 61909 | } |
| 61910 | #endif /* SQLITE_DEBUG */ |
| 61911 | for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){ |
| 61912 | u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i; |
| 61913 | assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) ); |
| 61914 | assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) ); |
| 61915 | REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]); |
| 61916 | REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]); |
| 61917 | assert( u.aj.i<u.aj.pKeyInfo->nField ); |
| 61918 | u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i]; |
| 61919 | u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i]; |
| @@ -61558,10 +62141,11 @@ | |
| 62141 | u.am.pC = 0; |
| 62142 | memset(&u.am.sMem, 0, sizeof(u.am.sMem)); |
| 62143 | assert( u.am.p1<p->nCursor ); |
| 62144 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 62145 | u.am.pDest = &aMem[pOp->p3]; |
| 62146 | memAboutToChange(p, u.am.pDest); |
| 62147 | MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 62148 | u.am.zRec = 0; |
| 62149 | |
| 62150 | /* This block sets the variable u.am.payloadSize to be the total number of |
| 62151 | ** bytes in the record. |
| @@ -61605,10 +62189,11 @@ | |
| 62189 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 62190 | } |
| 62191 | }else if( u.am.pC->pseudoTableReg>0 ){ |
| 62192 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 62193 | assert( u.am.pReg->flags & MEM_Blob ); |
| 62194 | assert( memIsValid(u.am.pReg) ); |
| 62195 | u.am.payloadSize = u.am.pReg->n; |
| 62196 | u.am.zRec = u.am.pReg->z; |
| 62197 | u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr; |
| 62198 | assert( u.am.payloadSize==0 || u.am.zRec!=0 ); |
| 62199 | }else{ |
| @@ -61829,25 +62414,24 @@ | |
| 62414 | assert( u.an.zAffinity!=0 ); |
| 62415 | assert( u.an.zAffinity[pOp->p2]==0 ); |
| 62416 | pIn1 = &aMem[pOp->p1]; |
| 62417 | while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){ |
| 62418 | assert( pIn1 <= &p->aMem[p->nMem] ); |
| 62419 | assert( memIsValid(pIn1) ); |
| 62420 | memAboutToChange(p, pIn1); |
| 62421 | ExpandBlob(pIn1); |
| 62422 | applyAffinity(pIn1, u.an.cAff, encoding); |
| 62423 | pIn1++; |
| 62424 | } |
| 62425 | break; |
| 62426 | } |
| 62427 | |
| 62428 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
| 62429 | ** |
| 62430 | ** Convert P2 registers beginning with P1 into the [record format] |
| 62431 | ** use as a data record in a database table or as a key |
| 62432 | ** in an index. The OP_Column opcode can decode the record later. |
| 62433 | ** |
| 62434 | ** P4 may be a string that is P2 characters long. The nth character of the |
| 62435 | ** string indicates the column affinity that should be used for the nth |
| 62436 | ** field of the index key. |
| 62437 | ** |
| @@ -61899,16 +62483,23 @@ | |
| 62483 | assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 ); |
| 62484 | u.ao.pData0 = &aMem[u.ao.nField]; |
| 62485 | u.ao.nField = pOp->p2; |
| 62486 | u.ao.pLast = &u.ao.pData0[u.ao.nField-1]; |
| 62487 | u.ao.file_format = p->minWriteFileFormat; |
| 62488 | |
| 62489 | /* Identify the output register */ |
| 62490 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 62491 | pOut = &aMem[pOp->p3]; |
| 62492 | memAboutToChange(p, pOut); |
| 62493 | |
| 62494 | /* Loop through the elements that will make up the record to figure |
| 62495 | ** out how much space is required for the new record. |
| 62496 | */ |
| 62497 | for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ |
| 62498 | assert( memIsValid(u.ao.pRec) ); |
| 62499 | if( u.ao.zAffinity ){ |
| 62500 | memAboutToChange(p, u.ao.pRec); |
| 62501 | applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding); |
| 62502 | } |
| 62503 | if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){ |
| 62504 | sqlite3VdbeMemExpandBlob(u.ao.pRec); |
| 62505 | } |
| @@ -61938,12 +62529,10 @@ | |
| 62529 | /* Make sure the output register has a buffer large enough to store |
| 62530 | ** the new record. The output register (pOp->p3) is not allowed to |
| 62531 | ** be one of the input registers (because the following call to |
| 62532 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 62533 | */ |
| 62534 | if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){ |
| 62535 | goto no_mem; |
| 62536 | } |
| 62537 | u.ao.zNewRecord = (u8 *)pOut->z; |
| 62538 | |
| @@ -62112,10 +62701,11 @@ | |
| 62701 | } |
| 62702 | } |
| 62703 | if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
| 62704 | sqlite3ExpirePreparedStatements(db); |
| 62705 | sqlite3ResetInternalSchema(db, 0); |
| 62706 | db->flags = (db->flags | SQLITE_InternChanges); |
| 62707 | } |
| 62708 | } |
| 62709 | |
| 62710 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 62711 | ** savepoints nested inside of the savepoint being operated on. */ |
| @@ -62502,10 +63092,12 @@ | |
| 63092 | } |
| 63093 | if( pOp->p5 ){ |
| 63094 | assert( u.aw.p2>0 ); |
| 63095 | assert( u.aw.p2<=p->nMem ); |
| 63096 | pIn2 = &aMem[u.aw.p2]; |
| 63097 | assert( memIsValid(pIn2) ); |
| 63098 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 63099 | sqlite3VdbeMemIntegerify(pIn2); |
| 63100 | u.aw.p2 = (int)pIn2->u.i; |
| 63101 | /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and |
| 63102 | ** that opcode will always set the u.aw.p2 value to 2 or more or else fail. |
| 63103 | ** If there were a failure, the prepared statement would have halted |
| @@ -62524,10 +63116,11 @@ | |
| 63116 | } |
| 63117 | assert( pOp->p1>=0 ); |
| 63118 | u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1); |
| 63119 | if( u.aw.pCur==0 ) goto no_mem; |
| 63120 | u.aw.pCur->nullRow = 1; |
| 63121 | u.aw.pCur->isOrdered = 1; |
| 63122 | rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor); |
| 63123 | u.aw.pCur->pKeyInfo = u.aw.pKeyInfo; |
| 63124 | |
| 63125 | /* Since it performs no memory allocation or IO, the only values that |
| 63126 | ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. |
| @@ -62576,11 +63169,11 @@ | |
| 63169 | case OP_OpenAutoindex: |
| 63170 | case OP_OpenEphemeral: { |
| 63171 | #if 0 /* local variables moved into u.ax */ |
| 63172 | VdbeCursor *pCx; |
| 63173 | #endif /* local variables moved into u.ax */ |
| 63174 | static const int vfsFlags = |
| 63175 | SQLITE_OPEN_READWRITE | |
| 63176 | SQLITE_OPEN_CREATE | |
| 63177 | SQLITE_OPEN_EXCLUSIVE | |
| 63178 | SQLITE_OPEN_DELETEONCLOSE | |
| 63179 | SQLITE_OPEN_TRANSIENT_DB; |
| @@ -62587,25 +63180,25 @@ | |
| 63180 | |
| 63181 | assert( pOp->p1>=0 ); |
| 63182 | u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 63183 | if( u.ax.pCx==0 ) goto no_mem; |
| 63184 | u.ax.pCx->nullRow = 1; |
| 63185 | rc = sqlite3BtreeOpen(0, db, &u.ax.pCx->pBt, |
| 63186 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
| 63187 | if( rc==SQLITE_OK ){ |
| 63188 | rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1); |
| 63189 | } |
| 63190 | if( rc==SQLITE_OK ){ |
| 63191 | /* If a transient index is required, create it by calling |
| 63192 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
| 63193 | ** opening it. If a transient table is required, just use the |
| 63194 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
| 63195 | */ |
| 63196 | if( pOp->p4.pKeyInfo ){ |
| 63197 | int pgno; |
| 63198 | assert( pOp->p4type==P4_KEYINFO ); |
| 63199 | rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY); |
| 63200 | if( rc==SQLITE_OK ){ |
| 63201 | assert( pgno==MASTER_ROOT+1 ); |
| 63202 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1, |
| 63203 | (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor); |
| 63204 | u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo; |
| @@ -62615,10 +63208,11 @@ | |
| 63208 | }else{ |
| 63209 | rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor); |
| 63210 | u.ax.pCx->isTable = 1; |
| 63211 | } |
| 63212 | } |
| 63213 | u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 63214 | u.ax.pCx->isIndex = !u.ax.pCx->isTable; |
| 63215 | break; |
| 63216 | } |
| 63217 | |
| 63218 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| @@ -62734,10 +63328,11 @@ | |
| 63328 | assert( u.az.pC!=0 ); |
| 63329 | assert( u.az.pC->pseudoTableReg==0 ); |
| 63330 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 63331 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 63332 | assert( OP_SeekGt == OP_SeekLt+3 ); |
| 63333 | assert( u.az.pC->isOrdered ); |
| 63334 | if( u.az.pC->pCursor!=0 ){ |
| 63335 | u.az.oc = pOp->opcode; |
| 63336 | u.az.pC->nullRow = 0; |
| 63337 | if( u.az.pC->isTable ){ |
| 63338 | /* The input value in P3 might be of any type: integer, real, string, |
| @@ -62816,10 +63411,13 @@ | |
| 63411 | assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY ); |
| 63412 | assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 ); |
| 63413 | assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 ); |
| 63414 | |
| 63415 | u.az.r.aMem = &aMem[pOp->p3]; |
| 63416 | #ifdef SQLITE_DEBUG |
| 63417 | { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); } |
| 63418 | #endif |
| 63419 | ExpandBlob(u.az.r.aMem); |
| 63420 | rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res); |
| 63421 | if( rc!=SQLITE_OK ){ |
| 63422 | goto abort_due_to_error; |
| 63423 | } |
| @@ -62944,15 +63542,18 @@ | |
| 63542 | assert( u.bb.pC->isTable==0 ); |
| 63543 | if( pOp->p4.i>0 ){ |
| 63544 | u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo; |
| 63545 | u.bb.r.nField = (u16)pOp->p4.i; |
| 63546 | u.bb.r.aMem = pIn3; |
| 63547 | #ifdef SQLITE_DEBUG |
| 63548 | { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); } |
| 63549 | #endif |
| 63550 | u.bb.r.flags = UNPACKED_PREFIX_MATCH; |
| 63551 | u.bb.pIdxKey = &u.bb.r; |
| 63552 | }else{ |
| 63553 | assert( pIn3->flags & MEM_Blob ); |
| 63554 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 63555 | u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z, |
| 63556 | u.bb.aTempRec, sizeof(u.bb.aTempRec)); |
| 63557 | if( u.bb.pIdxKey==0 ){ |
| 63558 | goto no_mem; |
| 63559 | } |
| @@ -63043,10 +63644,13 @@ | |
| 63644 | /* Populate the index search key. */ |
| 63645 | u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo; |
| 63646 | u.bc.r.nField = u.bc.nField + 1; |
| 63647 | u.bc.r.flags = UNPACKED_PREFIX_SEARCH; |
| 63648 | u.bc.r.aMem = u.bc.aMx; |
| 63649 | #ifdef SQLITE_DEBUG |
| 63650 | { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); } |
| 63651 | #endif |
| 63652 | |
| 63653 | /* Extract the value of u.bc.R from register P3. */ |
| 63654 | sqlite3VdbeMemIntegerify(pIn3); |
| 63655 | u.bc.R = pIn3->u.i; |
| 63656 | |
| @@ -63065,11 +63669,11 @@ | |
| 63669 | |
| 63670 | /* Opcode: NotExists P1 P2 P3 * * |
| 63671 | ** |
| 63672 | ** Use the content of register P3 as a integer key. If a record |
| 63673 | ** with that key does not exist in table of P1, then jump to P2. |
| 63674 | ** If the record does exist, then fall through. The cursor is left |
| 63675 | ** pointing to the record if it exists. |
| 63676 | ** |
| 63677 | ** The difference between this operation and NotFound is that this |
| 63678 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 63679 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| @@ -63223,11 +63827,13 @@ | |
| 63827 | u.be.pMem = &u.be.pFrame->aMem[pOp->p3]; |
| 63828 | }else{ |
| 63829 | /* Assert that P3 is a valid memory cell. */ |
| 63830 | assert( pOp->p3<=p->nMem ); |
| 63831 | u.be.pMem = &aMem[pOp->p3]; |
| 63832 | memAboutToChange(p, u.be.pMem); |
| 63833 | } |
| 63834 | assert( memIsValid(u.be.pMem) ); |
| 63835 | |
| 63836 | REGISTER_TRACE(pOp->p3, u.be.pMem); |
| 63837 | sqlite3VdbeMemIntegerify(u.be.pMem); |
| 63838 | assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 63839 | if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){ |
| @@ -63242,33 +63848,40 @@ | |
| 63848 | #endif |
| 63849 | |
| 63850 | sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0); |
| 63851 | } |
| 63852 | if( u.be.pC->useRandomRowid ){ |
| 63853 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
| 63854 | ** largest possible integer (9223372036854775807) then the database |
| 63855 | ** engine starts picking positive candidate ROWIDs at random until |
| 63856 | ** it finds one that is not previously used. */ |
| 63857 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 63858 | ** an AUTOINCREMENT table. */ |
| 63859 | /* on the first attempt, simply do one more than previous */ |
| 63860 | u.be.v = db->lastRowid; |
| 63861 | u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 63862 | u.be.v++; /* ensure non-zero */ |
| 63863 | u.be.cnt = 0; |
| 63864 | while( ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, |
| 63865 | 0, &u.be.res))==SQLITE_OK) |
| 63866 | && (u.be.res==0) |
| 63867 | && (++u.be.cnt<100)){ |
| 63868 | /* collision - try another random rowid */ |
| 63869 | sqlite3_randomness(sizeof(u.be.v), &u.be.v); |
| 63870 | if( u.be.cnt<5 ){ |
| 63871 | /* try "small" random rowids for the initial attempts */ |
| 63872 | u.be.v &= 0xffffff; |
| 63873 | }else{ |
| 63874 | u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 63875 | } |
| 63876 | u.be.v++; /* ensure non-zero */ |
| 63877 | } |
| 63878 | if( rc==SQLITE_OK && u.be.res==0 ){ |
| 63879 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 63880 | goto abort_due_to_error; |
| 63881 | } |
| 63882 | assert( u.be.v>0 ); /* EV: R-40812-03570 */ |
| 63883 | } |
| 63884 | u.be.pC->rowidIsValid = 0; |
| 63885 | u.be.pC->deferredMoveto = 0; |
| 63886 | u.be.pC->cacheStatus = CACHE_STALE; |
| 63887 | } |
| @@ -63334,10 +63947,11 @@ | |
| 63947 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
| 63948 | #endif /* local variables moved into u.bf */ |
| 63949 | |
| 63950 | u.bf.pData = &aMem[pOp->p2]; |
| 63951 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 63952 | assert( memIsValid(u.bf.pData) ); |
| 63953 | u.bf.pC = p->apCsr[pOp->p1]; |
| 63954 | assert( u.bf.pC!=0 ); |
| 63955 | assert( u.bf.pC->pCursor!=0 ); |
| 63956 | assert( u.bf.pC->pseudoTableReg==0 ); |
| 63957 | assert( u.bf.pC->isTable ); |
| @@ -63344,10 +63958,11 @@ | |
| 63958 | REGISTER_TRACE(pOp->p2, u.bf.pData); |
| 63959 | |
| 63960 | if( pOp->opcode==OP_Insert ){ |
| 63961 | u.bf.pKey = &aMem[pOp->p3]; |
| 63962 | assert( u.bf.pKey->flags & MEM_Int ); |
| 63963 | assert( memIsValid(u.bf.pKey) ); |
| 63964 | REGISTER_TRACE(pOp->p3, u.bf.pKey); |
| 63965 | u.bf.iKey = u.bf.pKey->u.i; |
| 63966 | }else{ |
| 63967 | assert( pOp->opcode==OP_InsertInt ); |
| 63968 | u.bf.iKey = pOp->p3; |
| @@ -63495,10 +64110,11 @@ | |
| 64110 | u32 n; |
| 64111 | i64 n64; |
| 64112 | #endif /* local variables moved into u.bh */ |
| 64113 | |
| 64114 | pOut = &aMem[pOp->p2]; |
| 64115 | memAboutToChange(p, pOut); |
| 64116 | |
| 64117 | /* Note that RowKey and RowData are really exactly the same instruction */ |
| 64118 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 64119 | u.bh.pC = p->apCsr[pOp->p1]; |
| 64120 | assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey ); |
| @@ -63837,10 +64453,13 @@ | |
| 64453 | if( ALWAYS(u.bo.pCrsr!=0) ){ |
| 64454 | u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo; |
| 64455 | u.bo.r.nField = (u16)pOp->p3; |
| 64456 | u.bo.r.flags = 0; |
| 64457 | u.bo.r.aMem = &aMem[pOp->p2]; |
| 64458 | #ifdef SQLITE_DEBUG |
| 64459 | { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); } |
| 64460 | #endif |
| 64461 | rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res); |
| 64462 | if( rc==SQLITE_OK && u.bo.res==0 ){ |
| 64463 | rc = sqlite3BtreeDelete(u.bo.pCrsr); |
| 64464 | } |
| 64465 | assert( u.bo.pC->deferredMoveto==0 ); |
| @@ -63921,10 +64540,11 @@ | |
| 64540 | #endif /* local variables moved into u.bq */ |
| 64541 | |
| 64542 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 64543 | u.bq.pC = p->apCsr[pOp->p1]; |
| 64544 | assert( u.bq.pC!=0 ); |
| 64545 | assert( u.bq.pC->isOrdered ); |
| 64546 | if( ALWAYS(u.bq.pC->pCursor!=0) ){ |
| 64547 | assert( u.bq.pC->deferredMoveto==0 ); |
| 64548 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 64549 | assert( pOp->p4type==P4_INT32 ); |
| 64550 | u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo; |
| @@ -63933,10 +64553,13 @@ | |
| 64553 | u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 64554 | }else{ |
| 64555 | u.bq.r.flags = UNPACKED_IGNORE_ROWID; |
| 64556 | } |
| 64557 | u.bq.r.aMem = &aMem[pOp->p3]; |
| 64558 | #ifdef SQLITE_DEBUG |
| 64559 | { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); } |
| 64560 | #endif |
| 64561 | rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res); |
| 64562 | if( pOp->opcode==OP_IdxLT ){ |
| 64563 | u.bq.res = -u.bq.res; |
| 64564 | }else{ |
| 64565 | assert( pOp->opcode==OP_IdxGE ); |
| @@ -64036,10 +64659,12 @@ | |
| 64659 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0) |
| 64660 | ); |
| 64661 | if( pOp->p3 ){ |
| 64662 | p->nChange += u.bs.nChange; |
| 64663 | if( pOp->p3>0 ){ |
| 64664 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 64665 | memAboutToChange(p, &aMem[pOp->p3]); |
| 64666 | aMem[pOp->p3].u.i += u.bs.nChange; |
| 64667 | } |
| 64668 | } |
| 64669 | break; |
| 64670 | } |
| @@ -64079,13 +64704,13 @@ | |
| 64704 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
| 64705 | u.bt.pDb = &db->aDb[pOp->p1]; |
| 64706 | assert( u.bt.pDb->pBt!=0 ); |
| 64707 | if( pOp->opcode==OP_CreateTable ){ |
| 64708 | /* u.bt.flags = BTREE_INTKEY; */ |
| 64709 | u.bt.flags = BTREE_INTKEY; |
| 64710 | }else{ |
| 64711 | u.bt.flags = BTREE_BLOBKEY; |
| 64712 | } |
| 64713 | rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags); |
| 64714 | pOut->u.i = u.bt.pgno; |
| 64715 | break; |
| 64716 | } |
| @@ -64410,10 +65035,11 @@ | |
| 65035 | void *t; /* Token identifying trigger */ |
| 65036 | #endif /* local variables moved into u.by */ |
| 65037 | |
| 65038 | u.by.pProgram = pOp->p4.pProgram; |
| 65039 | u.by.pRt = &aMem[pOp->p3]; |
| 65040 | assert( memIsValid(u.by.pRt) ); |
| 65041 | assert( u.by.pProgram->nOp>0 ); |
| 65042 | |
| 65043 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 65044 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 65045 | ** is really a trigger, not a foreign key action, and the flag set |
| @@ -64583,10 +65209,11 @@ | |
| 65209 | for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent); |
| 65210 | u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1]; |
| 65211 | }else{ |
| 65212 | u.ca.pIn1 = &aMem[pOp->p1]; |
| 65213 | } |
| 65214 | assert( memIsValid(u.ca.pIn1) ); |
| 65215 | sqlite3VdbeMemIntegerify(u.ca.pIn1); |
| 65216 | pIn2 = &aMem[pOp->p2]; |
| 65217 | sqlite3VdbeMemIntegerify(pIn2); |
| 65218 | if( u.ca.pIn1->u.i<pIn2->u.i){ |
| 65219 | u.ca.pIn1->u.i = pIn2->u.i; |
| @@ -64669,11 +65296,13 @@ | |
| 65296 | assert( u.cb.n>=0 ); |
| 65297 | u.cb.pRec = &aMem[pOp->p2]; |
| 65298 | u.cb.apVal = p->apArg; |
| 65299 | assert( u.cb.apVal || u.cb.n==0 ); |
| 65300 | for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){ |
| 65301 | assert( memIsValid(u.cb.pRec) ); |
| 65302 | u.cb.apVal[u.cb.i] = u.cb.pRec; |
| 65303 | memAboutToChange(p, u.cb.pRec); |
| 65304 | sqlite3VdbeMemStoreType(u.cb.pRec); |
| 65305 | } |
| 65306 | u.cb.ctx.pFunc = pOp->p4.pFunc; |
| 65307 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65308 | u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3]; |
| @@ -64689,11 +65318,11 @@ | |
| 65318 | assert( pOp>p->aOp ); |
| 65319 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 65320 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 65321 | u.cb.ctx.pColl = pOp[-1].p4.pColl; |
| 65322 | } |
| 65323 | (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */ |
| 65324 | if( u.cb.ctx.isError ){ |
| 65325 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s)); |
| 65326 | rc = u.cb.ctx.isError; |
| 65327 | } |
| 65328 | sqlite3VdbeMemRelease(&u.cb.ctx.s); |
| @@ -65076,10 +65705,11 @@ | |
| 65705 | #endif /* local variables moved into u.ch */ |
| 65706 | |
| 65707 | u.ch.pQuery = &aMem[pOp->p3]; |
| 65708 | u.ch.pArgc = &u.ch.pQuery[1]; |
| 65709 | u.ch.pCur = p->apCsr[pOp->p1]; |
| 65710 | assert( memIsValid(u.ch.pQuery) ); |
| 65711 | REGISTER_TRACE(pOp->p3, u.ch.pQuery); |
| 65712 | assert( u.ch.pCur->pVtabCursor ); |
| 65713 | u.ch.pVtabCursor = u.ch.pCur->pVtabCursor; |
| 65714 | u.ch.pVtab = u.ch.pVtabCursor->pVtab; |
| 65715 | u.ch.pModule = u.ch.pVtab->pModule; |
| @@ -65133,10 +65763,11 @@ | |
| 65763 | |
| 65764 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 65765 | assert( pCur->pVtabCursor ); |
| 65766 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65767 | u.ci.pDest = &aMem[pOp->p3]; |
| 65768 | memAboutToChange(p, u.ci.pDest); |
| 65769 | if( pCur->nullRow ){ |
| 65770 | sqlite3VdbeMemSetNull(u.ci.pDest); |
| 65771 | break; |
| 65772 | } |
| 65773 | u.ci.pVtab = pCur->pVtabCursor->pVtab; |
| @@ -65235,14 +65866,16 @@ | |
| 65866 | #endif /* local variables moved into u.ck */ |
| 65867 | |
| 65868 | u.ck.pVtab = pOp->p4.pVtab->pVtab; |
| 65869 | u.ck.pName = &aMem[pOp->p1]; |
| 65870 | assert( u.ck.pVtab->pModule->xRename ); |
| 65871 | assert( memIsValid(u.ck.pName) ); |
| 65872 | REGISTER_TRACE(pOp->p1, u.ck.pName); |
| 65873 | assert( u.ck.pName->flags & MEM_Str ); |
| 65874 | rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z); |
| 65875 | importVtabErrMsg(p, u.ck.pVtab); |
| 65876 | p->expired = 0; |
| 65877 | |
| 65878 | break; |
| 65879 | } |
| 65880 | #endif |
| 65881 | |
| @@ -65287,10 +65920,12 @@ | |
| 65920 | assert( pOp->p4type==P4_VTAB ); |
| 65921 | if( ALWAYS(u.cl.pModule->xUpdate) ){ |
| 65922 | u.cl.apArg = p->apArg; |
| 65923 | u.cl.pX = &aMem[pOp->p3]; |
| 65924 | for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){ |
| 65925 | assert( memIsValid(u.cl.pX) ); |
| 65926 | memAboutToChange(p, u.cl.pX); |
| 65927 | sqlite3VdbeMemStoreType(u.cl.pX); |
| 65928 | u.cl.apArg[u.cl.i] = u.cl.pX; |
| 65929 | u.cl.pX++; |
| 65930 | } |
| 65931 | rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid); |
| @@ -66341,12 +66976,11 @@ | |
| 66976 | SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
| 66977 | return pJfd->pMethods==&MemJournalMethods; |
| 66978 | } |
| 66979 | |
| 66980 | /* |
| 66981 | ** Return the number of bytes required to store a MemJournal file descriptor. |
| 66982 | */ |
| 66983 | SQLITE_PRIVATE int sqlite3MemJournalSize(void){ |
| 66984 | return sizeof(MemJournal); |
| 66985 | } |
| 66986 | |
| @@ -69226,12 +69860,12 @@ | |
| 69860 | return eType; |
| 69861 | } |
| 69862 | #endif |
| 69863 | |
| 69864 | /* |
| 69865 | ** Generate code for scalar subqueries used as a subquery expression, EXISTS, |
| 69866 | ** or IN operators. Examples: |
| 69867 | ** |
| 69868 | ** (SELECT a FROM b) -- subquery |
| 69869 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 69870 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 69871 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
| @@ -69290,14 +69924,14 @@ | |
| 69924 | assert( testAddr>0 || pParse->db->mallocFailed ); |
| 69925 | } |
| 69926 | |
| 69927 | switch( pExpr->op ){ |
| 69928 | case TK_IN: { |
| 69929 | char affinity; /* Affinity of the LHS of the IN */ |
| 69930 | KeyInfo keyInfo; /* Keyinfo for the generated table */ |
| 69931 | int addr; /* Address of OP_OpenEphemeral instruction */ |
| 69932 | Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ |
| 69933 | |
| 69934 | if( rMayHaveNull ){ |
| 69935 | sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
| 69936 | } |
| 69937 | |
| @@ -69316,10 +69950,11 @@ | |
| 69950 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 69951 | ** is used. |
| 69952 | */ |
| 69953 | pExpr->iTable = pParse->nTab++; |
| 69954 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); |
| 69955 | if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 69956 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 69957 | keyInfo.nField = 1; |
| 69958 | |
| 69959 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 69960 | /* Case 1: expr IN (SELECT ...) |
| @@ -69924,77 +70559,10 @@ | |
| 70559 | } |
| 70560 | return 0; |
| 70561 | } |
| 70562 | #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
| 70563 | |
| 70564 | /* |
| 70565 | ** Generate code into the current Vdbe to evaluate the given |
| 70566 | ** expression. Attempt to store the results in register "target". |
| 70567 | ** Return the register where results are stored. |
| 70568 | ** |
| @@ -70099,11 +70667,11 @@ | |
| 70667 | case TK_REGISTER: { |
| 70668 | inReg = pExpr->iTable; |
| 70669 | break; |
| 70670 | } |
| 70671 | case TK_AS: { |
| 70672 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 70673 | break; |
| 70674 | } |
| 70675 | #ifndef SQLITE_OMIT_CAST |
| 70676 | case TK_CAST: { |
| 70677 | /* Expressions of the form: CAST(pLeft AS token) */ |
| @@ -70531,10 +71099,15 @@ | |
| 71099 | testcase( regFree1==0 ); |
| 71100 | cacheX.op = TK_REGISTER; |
| 71101 | opCompare.op = TK_EQ; |
| 71102 | opCompare.pLeft = &cacheX; |
| 71103 | pTest = &opCompare; |
| 71104 | /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
| 71105 | ** The value in regFree1 might get SCopy-ed into the file result. |
| 71106 | ** So make sure that the regFree1 register is not reused for other |
| 71107 | ** purposes and possibly overwritten. */ |
| 71108 | regFree1 = 0; |
| 71109 | } |
| 71110 | for(i=0; i<nExpr; i=i+2){ |
| 71111 | sqlite3ExprCachePush(pParse); |
| 71112 | if( pX ){ |
| 71113 | assert( pTest!=0 ); |
| @@ -70624,14 +71197,18 @@ | |
| 71197 | */ |
| 71198 | SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
| 71199 | int inReg; |
| 71200 | |
| 71201 | assert( target>0 && target<=pParse->nMem ); |
| 71202 | if( pExpr && pExpr->op==TK_REGISTER ){ |
| 71203 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); |
| 71204 | }else{ |
| 71205 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 71206 | assert( pParse->pVdbe || pParse->db->mallocFailed ); |
| 71207 | if( inReg!=target && pParse->pVdbe ){ |
| 71208 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
| 71209 | } |
| 71210 | } |
| 71211 | return target; |
| 71212 | } |
| 71213 | |
| 71214 | /* |
| @@ -70800,23 +71377,18 @@ | |
| 71377 | ){ |
| 71378 | struct ExprList_item *pItem; |
| 71379 | int i, n; |
| 71380 | assert( pList!=0 ); |
| 71381 | assert( target>0 ); |
| 71382 | assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ |
| 71383 | n = pList->nExpr; |
| 71384 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 71385 | Expr *pExpr = pItem->pExpr; |
| 71386 | int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 71387 | if( inReg!=target+i ){ |
| 71388 | sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy, |
| 71389 | inReg, target+i); |
| 71390 | } |
| 71391 | } |
| 71392 | return n; |
| 71393 | } |
| 71394 | |
| @@ -71794,10 +72366,15 @@ | |
| 72366 | if( pTrig->pSchema==pTempSchema ){ |
| 72367 | zWhere = whereOrName(db, zWhere, pTrig->zName); |
| 72368 | } |
| 72369 | } |
| 72370 | } |
| 72371 | if( zWhere ){ |
| 72372 | char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); |
| 72373 | sqlite3DbFree(pParse->db, zWhere); |
| 72374 | zWhere = zNew; |
| 72375 | } |
| 72376 | return zWhere; |
| 72377 | } |
| 72378 | |
| 72379 | /* |
| 72380 | ** Generate code to drop and reload the internal representation of table |
| @@ -72401,11 +72978,12 @@ | |
| 72978 | int iIdxCur; /* Cursor open on index being analyzed */ |
| 72979 | Vdbe *v; /* The virtual machine being built up */ |
| 72980 | int i; /* Loop counter */ |
| 72981 | int topOfLoop; /* The top of the loop */ |
| 72982 | int endOfLoop; /* The end of the loop */ |
| 72983 | int addr = 0; /* The address of an instruction */ |
| 72984 | int jZeroRows = 0; /* Jump from here if number of rows is zero */ |
| 72985 | int iDb; /* Index of database containing pTab */ |
| 72986 | int regTabname = iMem++; /* Register containing table name */ |
| 72987 | int regIdxname = iMem++; /* Register containing index name */ |
| 72988 | int regSampleno = iMem++; /* Register containing next sample number */ |
| 72989 | int regCol = iMem++; /* Content of a column analyzed table */ |
| @@ -72420,12 +72998,19 @@ | |
| 72998 | int regLast = iMem++; /* Index of last sample to record */ |
| 72999 | int regFirst = iMem++; /* Index of first sample to record */ |
| 73000 | #endif |
| 73001 | |
| 73002 | v = sqlite3GetVdbe(pParse); |
| 73003 | if( v==0 || NEVER(pTab==0) ){ |
| 73004 | return; |
| 73005 | } |
| 73006 | if( pTab->tnum==0 ){ |
| 73007 | /* Do not gather statistics on views or virtual tables */ |
| 73008 | return; |
| 73009 | } |
| 73010 | if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 73011 | /* Do not gather statistics on system tables */ |
| 73012 | return; |
| 73013 | } |
| 73014 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 73015 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 73016 | assert( iDb>=0 ); |
| @@ -72438,10 +73023,11 @@ | |
| 73023 | |
| 73024 | /* Establish a read-lock on the table at the shared-cache level. */ |
| 73025 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 73026 | |
| 73027 | iIdxCur = pParse->nTab++; |
| 73028 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 73029 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 73030 | int nCol = pIdx->nColumn; |
| 73031 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 73032 | |
| 73033 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| @@ -72452,14 +73038,11 @@ | |
| 73038 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 73039 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
| 73040 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| 73041 | VdbeComment((v, "%s", pIdx->zName)); |
| 73042 | |
| 73043 | /* Populate the register containing the index name. */ |
| 73044 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 73045 | |
| 73046 | #ifdef SQLITE_ENABLE_STAT2 |
| 73047 | |
| 73048 | /* If this iteration of the loop is generating code to analyze the |
| @@ -72590,12 +73173,14 @@ | |
| 73173 | ** |
| 73174 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 73175 | ** If K>0 then it is always the case the D>0 so division by zero |
| 73176 | ** is never possible. |
| 73177 | */ |
| 73178 | sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 73179 | if( jZeroRows==0 ){ |
| 73180 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 73181 | } |
| 73182 | for(i=0; i<nCol; i++){ |
| 73183 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 73184 | sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 73185 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 73186 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| @@ -72605,17 +73190,39 @@ | |
| 73190 | } |
| 73191 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 73192 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 73193 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 73194 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 73195 | } |
| 73196 | |
| 73197 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 73198 | ** containing NULL as the index name and the row count as the content. |
| 73199 | */ |
| 73200 | if( pTab->pIndex==0 ){ |
| 73201 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 73202 | VdbeComment((v, "%s", pTab->zName)); |
| 73203 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 73204 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 73205 | }else{ |
| 73206 | assert( jZeroRows>0 ); |
| 73207 | addr = sqlite3VdbeAddOp0(v, OP_Goto); |
| 73208 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 73209 | } |
| 73210 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 73211 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 73212 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 73213 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 73214 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 73215 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 73216 | if( jZeroRows ){ |
| 73217 | sqlite3VdbeJumpHere(v, addr); |
| 73218 | } |
| 73219 | } |
| 73220 | |
| 73221 | /* |
| 73222 | ** Generate code that will cause the most recent index analysis to |
| 73223 | ** be loaded into internal hash tables where is can be used. |
| 73224 | */ |
| 73225 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 73226 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 73227 | if( v ){ |
| 73228 | sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
| @@ -72741,37 +73348,50 @@ | |
| 73348 | |
| 73349 | /* |
| 73350 | ** This callback is invoked once for each index when reading the |
| 73351 | ** sqlite_stat1 table. |
| 73352 | ** |
| 73353 | ** argv[0] = name of the table |
| 73354 | ** argv[1] = name of the index (might be NULL) |
| 73355 | ** argv[2] = results of analysis - on integer for each column |
| 73356 | ** |
| 73357 | ** Entries for which argv[1]==NULL simply record the number of rows in |
| 73358 | ** the table. |
| 73359 | */ |
| 73360 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 73361 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 73362 | Index *pIndex; |
| 73363 | Table *pTable; |
| 73364 | int i, c, n; |
| 73365 | unsigned int v; |
| 73366 | const char *z; |
| 73367 | |
| 73368 | assert( argc==3 ); |
| 73369 | UNUSED_PARAMETER2(NotUsed, argc); |
| 73370 | |
| 73371 | if( argv==0 || argv[0]==0 || argv[2]==0 ){ |
| 73372 | return 0; |
| 73373 | } |
| 73374 | pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); |
| 73375 | if( pTable==0 ){ |
| 73376 | return 0; |
| 73377 | } |
| 73378 | if( argv[1] ){ |
| 73379 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 73380 | }else{ |
| 73381 | pIndex = 0; |
| 73382 | } |
| 73383 | n = pIndex ? pIndex->nColumn : 0; |
| 73384 | z = argv[2]; |
| 73385 | for(i=0; *z && i<=n; i++){ |
| 73386 | v = 0; |
| 73387 | while( (c=z[0])>='0' && c<='9' ){ |
| 73388 | v = v*10 + c - '0'; |
| 73389 | z++; |
| 73390 | } |
| 73391 | if( i==0 ) pTable->nRowEst = v; |
| 73392 | if( pIndex==0 ) break; |
| 73393 | pIndex->aiRowEst[i] = v; |
| 73394 | if( *z==' ' ) z++; |
| 73395 | } |
| 73396 | return 0; |
| 73397 | } |
| @@ -72843,11 +73463,11 @@ | |
| 73463 | return SQLITE_ERROR; |
| 73464 | } |
| 73465 | |
| 73466 | /* Load new statistics out of the sqlite_stat1 table */ |
| 73467 | zSql = sqlite3MPrintf(db, |
| 73468 | "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 73469 | if( zSql==0 ){ |
| 73470 | rc = SQLITE_NOMEM; |
| 73471 | }else{ |
| 73472 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 73473 | sqlite3DbFree(db, zSql); |
| @@ -73060,13 +73680,12 @@ | |
| 73680 | |
| 73681 | /* Open the database file. If the btree is successfully opened, use |
| 73682 | ** it to obtain the database schema. At this point the schema may |
| 73683 | ** or may not be initialised. |
| 73684 | */ |
| 73685 | rc = sqlite3BtreeOpen(zFile, db, &aNew->pBt, 0, |
| 73686 | db->openFlags | SQLITE_OPEN_MAIN_DB); |
| 73687 | db->nDb++; |
| 73688 | if( rc==SQLITE_CONSTRAINT ){ |
| 73689 | rc = SQLITE_ERROR; |
| 73690 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| 73691 | }else if( rc==SQLITE_OK ){ |
| @@ -73303,11 +73922,12 @@ | |
| 73922 | 0, /* pNext */ |
| 73923 | detachFunc, /* xFunc */ |
| 73924 | 0, /* xStep */ |
| 73925 | 0, /* xFinalize */ |
| 73926 | "sqlite_detach", /* zName */ |
| 73927 | 0, /* pHash */ |
| 73928 | 0 /* pDestructor */ |
| 73929 | }; |
| 73930 | codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); |
| 73931 | } |
| 73932 | |
| 73933 | /* |
| @@ -73324,11 +73944,12 @@ | |
| 73944 | 0, /* pNext */ |
| 73945 | attachFunc, /* xFunc */ |
| 73946 | 0, /* xStep */ |
| 73947 | 0, /* xFinalize */ |
| 73948 | "sqlite_attach", /* zName */ |
| 73949 | 0, /* pHash */ |
| 73950 | 0 /* pDestructor */ |
| 73951 | }; |
| 73952 | codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); |
| 73953 | } |
| 73954 | #endif /* SQLITE_OMIT_ATTACH */ |
| 73955 | |
| @@ -74453,12 +75074,13 @@ | |
| 75074 | ** set to the index of the database that the table or view is to be |
| 75075 | ** created in. |
| 75076 | */ |
| 75077 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 75078 | if( iDb<0 ) return; |
| 75079 | if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 75080 | /* If creating a temp table, the name may not be qualified. Unless |
| 75081 | ** the database name is "temp" anyway. */ |
| 75082 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 75083 | return; |
| 75084 | } |
| 75085 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 75086 | |
| @@ -74502,21 +75124,22 @@ | |
| 75124 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 75125 | ** and types will be used, so there is no need to test for namespace |
| 75126 | ** collisions. |
| 75127 | */ |
| 75128 | if( !IN_DECLARE_VTAB ){ |
| 75129 | char *zDb = db->aDb[iDb].zName; |
| 75130 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 75131 | goto begin_table_error; |
| 75132 | } |
| 75133 | pTable = sqlite3FindTable(db, zName, zDb); |
| 75134 | if( pTable ){ |
| 75135 | if( !noErr ){ |
| 75136 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 75137 | } |
| 75138 | goto begin_table_error; |
| 75139 | } |
| 75140 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
| 75141 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 75142 | goto begin_table_error; |
| 75143 | } |
| 75144 | } |
| 75145 | |
| @@ -74529,10 +75152,11 @@ | |
| 75152 | } |
| 75153 | pTable->zName = zName; |
| 75154 | pTable->iPKey = -1; |
| 75155 | pTable->pSchema = db->aDb[iDb].pSchema; |
| 75156 | pTable->nRef = 1; |
| 75157 | pTable->nRowEst = 1000000; |
| 75158 | assert( pParse->pNewTable==0 ); |
| 75159 | pParse->pNewTable = pTable; |
| 75160 | |
| 75161 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 75162 | ** then record a pointer to this table in the main database structure |
| @@ -75375,16 +75999,14 @@ | |
| 75999 | sqlite3SelectDelete(db, pSelect); |
| 76000 | return; |
| 76001 | } |
| 76002 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
| 76003 | p = pParse->pNewTable; |
| 76004 | if( p==0 || pParse->nErr ){ |
| 76005 | sqlite3SelectDelete(db, pSelect); |
| 76006 | return; |
| 76007 | } |
| 76008 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 76009 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 76010 | if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
| 76011 | && sqlite3FixSelect(&sFix, pSelect) |
| 76012 | ){ |
| @@ -76498,11 +77120,12 @@ | |
| 77120 | */ |
| 77121 | if( pTblName ){ |
| 77122 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 77123 | sqlite3ChangeCookie(pParse, iDb); |
| 77124 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, |
| 77125 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), |
| 77126 | P4_DYNAMIC); |
| 77127 | sqlite3VdbeAddOp1(v, OP_Expire, 0); |
| 77128 | } |
| 77129 | } |
| 77130 | |
| 77131 | /* When adding an index to the list of indices for a table, make |
| @@ -76559,18 +77182,18 @@ | |
| 77182 | ** are based on typical values found in actual indices. |
| 77183 | */ |
| 77184 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 77185 | unsigned *a = pIdx->aiRowEst; |
| 77186 | int i; |
| 77187 | unsigned n; |
| 77188 | assert( a!=0 ); |
| 77189 | a[0] = pIdx->pTable->nRowEst; |
| 77190 | if( a[0]<10 ) a[0] = 10; |
| 77191 | n = 10; |
| 77192 | for(i=1; i<=pIdx->nColumn; i++){ |
| 77193 | a[i] = n; |
| 77194 | if( n>5 ) n--; |
| 77195 | } |
| 77196 | if( pIdx->onError!=OE_None ){ |
| 77197 | a[pIdx->nColumn] = 1; |
| 77198 | } |
| 77199 | } |
| @@ -76626,11 +77249,11 @@ | |
| 77249 | /* Generate code to remove the index and from the master table */ |
| 77250 | v = sqlite3GetVdbe(pParse); |
| 77251 | if( v ){ |
| 77252 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 77253 | sqlite3NestedParse(pParse, |
| 77254 | "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", |
| 77255 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 77256 | pIndex->zName |
| 77257 | ); |
| 77258 | if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ |
| 77259 | sqlite3NestedParse(pParse, |
| @@ -77118,11 +77741,11 @@ | |
| 77741 | SQLITE_OPEN_CREATE | |
| 77742 | SQLITE_OPEN_EXCLUSIVE | |
| 77743 | SQLITE_OPEN_DELETEONCLOSE | |
| 77744 | SQLITE_OPEN_TEMP_DB; |
| 77745 | |
| 77746 | rc = sqlite3BtreeOpen(0, db, &pBt, 0, flags); |
| 77747 | if( rc!=SQLITE_OK ){ |
| 77748 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 77749 | "file for storing temporary tables"); |
| 77750 | pParse->rc = rc; |
| 77751 | return 1; |
| @@ -77775,11 +78398,11 @@ | |
| 78398 | ** If the SQLITE_PreferBuiltin flag is set, then search the built-in |
| 78399 | ** functions even if a prior app-defined function was found. And give |
| 78400 | ** priority to built-in functions. |
| 78401 | ** |
| 78402 | ** Except, if createFlag is true, that means that we are trying to |
| 78403 | ** install a new function. Whatever FuncDef structure is returned it will |
| 78404 | ** have fields overwritten with new information appropriate for the |
| 78405 | ** new function. But the FuncDefs for built-in functions are read-only. |
| 78406 | ** So we must not search for built-ins when creating a new function. |
| 78407 | */ |
| 78408 | if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ |
| @@ -79956,14 +80579,14 @@ | |
| 80579 | if( caseSensitive ){ |
| 80580 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 80581 | }else{ |
| 80582 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 80583 | } |
| 80584 | sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0); |
| 80585 | sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0); |
| 80586 | sqlite3CreateFunc(db, "glob", 2, SQLITE_ANY, |
| 80587 | (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); |
| 80588 | setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 80589 | setLikeOptFlag(db, "like", |
| 80590 | caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
| 80591 | } |
| 80592 | |
| @@ -80043,14 +80666,14 @@ | |
| 80666 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 80667 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 80668 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 80669 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 80670 | /* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */ |
| 80671 | {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0}, |
| 80672 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 80673 | /* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */ |
| 80674 | {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0}, |
| 80675 | FUNCTION(random, 0, 0, 0, randomFunc ), |
| 80676 | FUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 80677 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 80678 | FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 80679 | FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| @@ -80073,11 +80696,11 @@ | |
| 80696 | #endif |
| 80697 | AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 80698 | AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 80699 | AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 80700 | /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */ |
| 80701 | {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0}, |
| 80702 | AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
| 80703 | AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80704 | AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
| 80705 | |
| 80706 | LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| @@ -80484,11 +81107,11 @@ | |
| 81107 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 81108 | |
| 81109 | sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); |
| 81110 | sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF); |
| 81111 | for(i=0; i<nCol; i++){ |
| 81112 | sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i); |
| 81113 | } |
| 81114 | |
| 81115 | /* If the parent table is the same as the child table, and we are about |
| 81116 | ** to increment the constraint-counter (i.e. this is an INSERT operation), |
| 81117 | ** then check if the row being inserted matches itself. If so, do not |
| @@ -87131,15 +87754,17 @@ | |
| 87754 | sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 87755 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); |
| 87756 | sqlite3ReleaseTempReg(pParse, r1); |
| 87757 | } |
| 87758 | |
| 87759 | #ifndef SQLITE_OMIT_SUBQUERY |
| 87760 | /* |
| 87761 | ** Generate an error message when a SELECT is used within a subexpression |
| 87762 | ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result |
| 87763 | ** column. We do this in a subroutine because the error used to occur |
| 87764 | ** in multiple places. (The error only occurs in one place now, but we |
| 87765 | ** retain the subroutine to minimize code disruption.) |
| 87766 | */ |
| 87767 | static int checkForMultiColumnSelectError( |
| 87768 | Parse *pParse, /* Parse context. */ |
| 87769 | SelectDest *pDest, /* Destination of SELECT results */ |
| 87770 | int nExpr /* Number of result columns returned by SELECT */ |
| @@ -87151,10 +87776,11 @@ | |
| 87776 | return 1; |
| 87777 | }else{ |
| 87778 | return 0; |
| 87779 | } |
| 87780 | } |
| 87781 | #endif |
| 87782 | |
| 87783 | /* |
| 87784 | ** This routine generates the code for the inside of the inner loop |
| 87785 | ** of a SELECT. |
| 87786 | ** |
| @@ -87230,14 +87856,10 @@ | |
| 87856 | if( pOrderBy==0 ){ |
| 87857 | codeOffset(v, p, iContinue); |
| 87858 | } |
| 87859 | } |
| 87860 | |
| 87861 | switch( eDest ){ |
| 87862 | /* In this mode, write each query result to the key of the temporary |
| 87863 | ** table iParm. |
| 87864 | */ |
| 87865 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| @@ -88143,10 +88765,11 @@ | |
| 88765 | /* Create the destination temporary table if necessary |
| 88766 | */ |
| 88767 | if( dest.eDest==SRT_EphemTab ){ |
| 88768 | assert( p->pEList ); |
| 88769 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr); |
| 88770 | sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 88771 | dest.eDest = SRT_Table; |
| 88772 | } |
| 88773 | |
| 88774 | /* Make sure all SELECTs in the statement have the same number of elements |
| 88775 | ** in their result sets. |
| @@ -90105,11 +90728,11 @@ | |
| 90728 | ExprList *pList = pF->pExpr->x.pList; |
| 90729 | assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); |
| 90730 | if( pList ){ |
| 90731 | nArg = pList->nExpr; |
| 90732 | regAgg = sqlite3GetTempRange(pParse, nArg); |
| 90733 | sqlite3ExprCodeExprList(pParse, pList, regAgg, 1); |
| 90734 | }else{ |
| 90735 | nArg = 0; |
| 90736 | regAgg = 0; |
| 90737 | } |
| 90738 | if( pF->iDistinct>=0 ){ |
| @@ -90264,10 +90887,19 @@ | |
| 90887 | |
| 90888 | /* Begin generating code. |
| 90889 | */ |
| 90890 | v = sqlite3GetVdbe(pParse); |
| 90891 | if( v==0 ) goto select_end; |
| 90892 | |
| 90893 | /* If writing to memory or generating a set |
| 90894 | ** only a single column may be output. |
| 90895 | */ |
| 90896 | #ifndef SQLITE_OMIT_SUBQUERY |
| 90897 | if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ |
| 90898 | goto select_end; |
| 90899 | } |
| 90900 | #endif |
| 90901 | |
| 90902 | /* Generate code for all sub-queries in the FROM clause |
| 90903 | */ |
| 90904 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 90905 | for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| @@ -90338,19 +90970,10 @@ | |
| 90970 | } |
| 90971 | return multiSelect(pParse, p, pDest); |
| 90972 | } |
| 90973 | #endif |
| 90974 | |
| 90975 | /* If possible, rewrite the query to use GROUP BY instead of DISTINCT. |
| 90976 | ** GROUP BY might use an index, DISTINCT never does. |
| 90977 | */ |
| 90978 | assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 ); |
| 90979 | if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){ |
| @@ -90409,10 +91032,11 @@ | |
| 91032 | assert( isAgg || pGroupBy ); |
| 91033 | distinct = pParse->nTab++; |
| 91034 | pKeyInfo = keyInfoFromExprList(pParse, p->pEList); |
| 91035 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0, |
| 91036 | (char*)pKeyInfo, P4_KEYINFO_HANDOFF); |
| 91037 | sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 91038 | }else{ |
| 91039 | distinct = -1; |
| 91040 | } |
| 91041 | |
| 91042 | /* Aggregate and non-aggregate queries are handled differently */ |
| @@ -92884,10 +93508,11 @@ | |
| 93508 | ** be stored. |
| 93509 | */ |
| 93510 | assert( v ); |
| 93511 | ephemTab = pParse->nTab++; |
| 93512 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); |
| 93513 | sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 93514 | |
| 93515 | /* fill the ephemeral table |
| 93516 | */ |
| 93517 | sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); |
| 93518 | sqlite3Select(pParse, pSelect, &dest); |
| @@ -93022,10 +93647,14 @@ | |
| 93647 | int nDb; /* Number of attached databases */ |
| 93648 | |
| 93649 | if( !db->autoCommit ){ |
| 93650 | sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); |
| 93651 | return SQLITE_ERROR; |
| 93652 | } |
| 93653 | if( db->activeVdbeCnt>1 ){ |
| 93654 | sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); |
| 93655 | return SQLITE_ERROR; |
| 93656 | } |
| 93657 | |
| 93658 | /* Save the current value of the database flags so that it can be |
| 93659 | ** restored before returning. Then set the writable-schema flag, and |
| 93660 | ** disable CHECK and foreign key constraints. */ |
| @@ -93624,11 +94253,11 @@ | |
| 94253 | sqlite3DbFree(db, zStmt); |
| 94254 | v = sqlite3GetVdbe(pParse); |
| 94255 | sqlite3ChangeCookie(pParse, iDb); |
| 94256 | |
| 94257 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 94258 | zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
| 94259 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); |
| 94260 | sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 94261 | pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
| 94262 | } |
| 94263 | |
| @@ -94864,15 +95493,16 @@ | |
| 95493 | if( op==TK_REGISTER ){ |
| 95494 | op = pRight->op2; |
| 95495 | } |
| 95496 | if( op==TK_VARIABLE ){ |
| 95497 | Vdbe *pReprepare = pParse->pReprepare; |
| 95498 | int iCol = pRight->iColumn; |
| 95499 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 95500 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 95501 | z = (char *)sqlite3_value_text(pVal); |
| 95502 | } |
| 95503 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */ |
| 95504 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 95505 | }else if( op==TK_STRING ){ |
| 95506 | z = pRight->u.zToken; |
| 95507 | } |
| 95508 | if( z ){ |
| @@ -94886,11 +95516,11 @@ | |
| 95516 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 95517 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 95518 | *ppPrefix = pPrefix; |
| 95519 | if( op==TK_VARIABLE ){ |
| 95520 | Vdbe *v = pParse->pVdbe; |
| 95521 | sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */ |
| 95522 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 95523 | /* If the rhs of the LIKE expression is a variable, and the current |
| 95524 | ** value of the variable means there is no need to invoke the LIKE |
| 95525 | ** function, then no OP_Variable will be added to the program. |
| 95526 | ** This causes problems for the sqlite3_bind_parameter_name() |
| @@ -95900,11 +96530,11 @@ | |
| 96530 | return; |
| 96531 | } |
| 96532 | |
| 96533 | assert( pParse->nQueryLoop >= (double)1 ); |
| 96534 | pTable = pSrc->pTab; |
| 96535 | nTableRow = pTable->nRowEst; |
| 96536 | logN = estLog(nTableRow); |
| 96537 | costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); |
| 96538 | if( costTempIdx>=pCost->rCost ){ |
| 96539 | /* The cost of creating the transient table would be greater than |
| 96540 | ** doing the full table scan */ |
| @@ -96510,11 +97140,11 @@ | |
| 97140 | /* The evalConstExpr() function will have already converted any TK_VARIABLE |
| 97141 | ** expression involved in an comparison into a TK_REGISTER. */ |
| 97142 | assert( pExpr->op!=TK_VARIABLE ); |
| 97143 | if( pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE ){ |
| 97144 | int iVar = pExpr->iColumn; |
| 97145 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */ |
| 97146 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 97147 | return SQLITE_OK; |
| 97148 | } |
| 97149 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 97150 | } |
| @@ -96707,27 +97337,18 @@ | |
| 97337 | Index *pFirst; /* Any other index on the table */ |
| 97338 | memset(&sPk, 0, sizeof(Index)); |
| 97339 | sPk.nColumn = 1; |
| 97340 | sPk.aiColumn = &aiColumnPk; |
| 97341 | sPk.aiRowEst = aiRowEstPk; |
| 97342 | sPk.onError = OE_Replace; |
| 97343 | sPk.pTable = pSrc->pTab; |
| 97344 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 97345 | aiRowEstPk[1] = 1; |
| 97346 | pFirst = pSrc->pTab->pIndex; |
| 97347 | if( pSrc->notIndexed==0 ){ |
| 97348 | sPk.pNext = pFirst; |
| 97349 | } |
| 97350 | pProbe = &sPk; |
| 97351 | wsFlagMask = ~( |
| 97352 | WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE |
| 97353 | ); |
| 97354 | eqTermMask = WO_EQ|WO_IN; |
| @@ -98297,11 +98918,11 @@ | |
| 98918 | ** CREATE TABLE t2(c, d); |
| 98919 | ** SELECT * FROM t2, t1 WHERE t2.rowid = t1.a; |
| 98920 | ** |
| 98921 | ** The best strategy is to iterate through table t1 first. However it |
| 98922 | ** is not possible to determine this with a simple greedy algorithm. |
| 98923 | ** Since the cost of a linear scan through table t2 is the same |
| 98924 | ** as the cost of a linear scan through table t1, a simple greedy |
| 98925 | ** algorithm may choose to use t2 for the outer loop, which is a much |
| 98926 | ** costlier approach. |
| 98927 | */ |
| 98928 | nUnconstrained = 0; |
| @@ -103366,19 +103987,37 @@ | |
| 103987 | |
| 103988 | /************** End of sqliteicu.h *******************************************/ |
| 103989 | /************** Continuing where we left off in main.c ***********************/ |
| 103990 | #endif |
| 103991 | |
| 103992 | #ifndef SQLITE_AMALGAMATION |
| 103993 | /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant |
| 103994 | ** contains the text of SQLITE_VERSION macro. |
| 103995 | */ |
| 103996 | SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 103997 | #endif |
| 103998 | |
| 103999 | /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns |
| 104000 | ** a pointer to the to the sqlite3_version[] string constant. |
| 104001 | */ |
| 104002 | SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } |
| 104003 | |
| 104004 | /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a |
| 104005 | ** pointer to a string constant whose value is the same as the |
| 104006 | ** SQLITE_SOURCE_ID C preprocessor macro. |
| 104007 | */ |
| 104008 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 104009 | |
| 104010 | /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function |
| 104011 | ** returns an integer equal to SQLITE_VERSION_NUMBER. |
| 104012 | */ |
| 104013 | SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
| 104014 | |
| 104015 | /* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns |
| 104016 | ** zero if and only if SQLite was compiled mutexing code omitted due to |
| 104017 | ** the SQLITE_THREADSAFE compile-time option being set to 0. |
| 104018 | */ |
| 104019 | SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
| 104020 | |
| 104021 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 104022 | /* |
| 104023 | ** If the following function pointer is not NULL and if |
| @@ -103495,10 +104134,17 @@ | |
| 104134 | /* Do the rest of the initialization under the recursive mutex so |
| 104135 | ** that we will be able to handle recursive calls into |
| 104136 | ** sqlite3_initialize(). The recursive calls normally come through |
| 104137 | ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 104138 | ** recursive calls might also be possible. |
| 104139 | ** |
| 104140 | ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls |
| 104141 | ** to the xInit method, so the xInit method need not be threadsafe. |
| 104142 | ** |
| 104143 | ** The following mutex is what serializes access to the appdef pcache xInit |
| 104144 | ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the |
| 104145 | ** call to sqlite3PcacheInitialize(). |
| 104146 | */ |
| 104147 | sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
| 104148 | if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
| 104149 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 104150 | sqlite3GlobalConfig.inProgress = 1; |
| @@ -103775,16 +104421,16 @@ | |
| 104421 | if( cnt<0 ) cnt = 0; |
| 104422 | if( sz==0 || cnt==0 ){ |
| 104423 | sz = 0; |
| 104424 | pStart = 0; |
| 104425 | }else if( pBuf==0 ){ |
| 104426 | sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ |
| 104427 | sqlite3BeginBenignMalloc(); |
| 104428 | pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ |
| 104429 | sqlite3EndBenignMalloc(); |
| 104430 | }else{ |
| 104431 | sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ |
| 104432 | pStart = pBuf; |
| 104433 | } |
| 104434 | db->lookaside.pStart = pStart; |
| 104435 | db->lookaside.pFree = 0; |
| 104436 | db->lookaside.sz = (u16)sz; |
| @@ -103823,18 +104469,18 @@ | |
| 104469 | va_list ap; |
| 104470 | int rc; |
| 104471 | va_start(ap, op); |
| 104472 | switch( op ){ |
| 104473 | case SQLITE_DBCONFIG_LOOKASIDE: { |
| 104474 | void *pBuf = va_arg(ap, void*); /* IMP: R-21112-12275 */ |
| 104475 | int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ |
| 104476 | int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ |
| 104477 | rc = setupLookaside(db, pBuf, sz, cnt); |
| 104478 | break; |
| 104479 | } |
| 104480 | default: { |
| 104481 | rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
| 104482 | break; |
| 104483 | } |
| 104484 | } |
| 104485 | va_end(ap); |
| 104486 | return rc; |
| @@ -103934,16 +104580,33 @@ | |
| 104580 | } |
| 104581 | db->nSavepoint = 0; |
| 104582 | db->nStatement = 0; |
| 104583 | db->isTransactionSavepoint = 0; |
| 104584 | } |
| 104585 | |
| 104586 | /* |
| 104587 | ** Invoke the destructor function associated with FuncDef p, if any. Except, |
| 104588 | ** if this is not the last copy of the function, do not invoke it. Multiple |
| 104589 | ** copies of a single function are created when create_function() is called |
| 104590 | ** with SQLITE_ANY as the encoding. |
| 104591 | */ |
| 104592 | static void functionDestroy(sqlite3 *db, FuncDef *p){ |
| 104593 | FuncDestructor *pDestructor = p->pDestructor; |
| 104594 | if( pDestructor ){ |
| 104595 | pDestructor->nRef--; |
| 104596 | if( pDestructor->nRef==0 ){ |
| 104597 | pDestructor->xDestroy(pDestructor->pUserData); |
| 104598 | sqlite3DbFree(db, pDestructor); |
| 104599 | } |
| 104600 | } |
| 104601 | } |
| 104602 | |
| 104603 | /* |
| 104604 | ** Close an existing SQLite database |
| 104605 | */ |
| 104606 | SQLITE_API int sqlite3_close(sqlite3 *db){ |
| 104607 | HashElem *i; /* Hash table iterator */ |
| 104608 | int j; |
| 104609 | |
| 104610 | if( !db ){ |
| 104611 | return SQLITE_OK; |
| 104612 | } |
| @@ -104007,10 +104670,11 @@ | |
| 104670 | for(j=0; j<ArraySize(db->aFunc.a); j++){ |
| 104671 | FuncDef *pNext, *pHash, *p; |
| 104672 | for(p=db->aFunc.a[j]; p; p=pHash){ |
| 104673 | pHash = p->pHash; |
| 104674 | while( p ){ |
| 104675 | functionDestroy(db, p); |
| 104676 | pNext = p->pNext; |
| 104677 | sqlite3DbFree(db, p); |
| 104678 | p = pNext; |
| 104679 | } |
| 104680 | } |
| @@ -104281,11 +104945,12 @@ | |
| 104945 | int nArg, |
| 104946 | int enc, |
| 104947 | void *pUserData, |
| 104948 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 104949 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 104950 | void (*xFinal)(sqlite3_context*), |
| 104951 | FuncDestructor *pDestructor |
| 104952 | ){ |
| 104953 | FuncDef *p; |
| 104954 | int nName; |
| 104955 | |
| 104956 | assert( sqlite3_mutex_held(db->mutex) ); |
| @@ -104309,14 +104974,14 @@ | |
| 104974 | if( enc==SQLITE_UTF16 ){ |
| 104975 | enc = SQLITE_UTF16NATIVE; |
| 104976 | }else if( enc==SQLITE_ANY ){ |
| 104977 | int rc; |
| 104978 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
| 104979 | pUserData, xFunc, xStep, xFinal, pDestructor); |
| 104980 | if( rc==SQLITE_OK ){ |
| 104981 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
| 104982 | pUserData, xFunc, xStep, xFinal, pDestructor); |
| 104983 | } |
| 104984 | if( rc!=SQLITE_OK ){ |
| 104985 | return rc; |
| 104986 | } |
| 104987 | enc = SQLITE_UTF16BE; |
| @@ -104345,10 +105010,19 @@ | |
| 105010 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
| 105011 | assert(p || db->mallocFailed); |
| 105012 | if( !p ){ |
| 105013 | return SQLITE_NOMEM; |
| 105014 | } |
| 105015 | |
| 105016 | /* If an older version of the function with a configured destructor is |
| 105017 | ** being replaced invoke the destructor function here. */ |
| 105018 | functionDestroy(db, p); |
| 105019 | |
| 105020 | if( pDestructor ){ |
| 105021 | pDestructor->nRef++; |
| 105022 | } |
| 105023 | p->pDestructor = pDestructor; |
| 105024 | p->flags = 0; |
| 105025 | p->xFunc = xFunc; |
| 105026 | p->xStep = xStep; |
| 105027 | p->xFinalize = xFinal; |
| 105028 | p->pUserData = pUserData; |
| @@ -104359,21 +105033,53 @@ | |
| 105033 | /* |
| 105034 | ** Create new user functions. |
| 105035 | */ |
| 105036 | SQLITE_API int sqlite3_create_function( |
| 105037 | sqlite3 *db, |
| 105038 | const char *zFunc, |
| 105039 | int nArg, |
| 105040 | int enc, |
| 105041 | void *p, |
| 105042 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 105043 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 105044 | void (*xFinal)(sqlite3_context*) |
| 105045 | ){ |
| 105046 | return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep, |
| 105047 | xFinal, 0); |
| 105048 | } |
| 105049 | |
| 105050 | SQLITE_API int sqlite3_create_function_v2( |
| 105051 | sqlite3 *db, |
| 105052 | const char *zFunc, |
| 105053 | int nArg, |
| 105054 | int enc, |
| 105055 | void *p, |
| 105056 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 105057 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 105058 | void (*xFinal)(sqlite3_context*), |
| 105059 | void (*xDestroy)(void *) |
| 105060 | ){ |
| 105061 | int rc = SQLITE_ERROR; |
| 105062 | FuncDestructor *pArg = 0; |
| 105063 | sqlite3_mutex_enter(db->mutex); |
| 105064 | if( xDestroy ){ |
| 105065 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 105066 | if( !pArg ){ |
| 105067 | xDestroy(p); |
| 105068 | goto out; |
| 105069 | } |
| 105070 | pArg->xDestroy = xDestroy; |
| 105071 | pArg->pUserData = p; |
| 105072 | } |
| 105073 | rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg); |
| 105074 | if( pArg && pArg->nRef==0 ){ |
| 105075 | assert( rc!=SQLITE_OK ); |
| 105076 | xDestroy(p); |
| 105077 | sqlite3DbFree(db, pArg); |
| 105078 | } |
| 105079 | |
| 105080 | out: |
| 105081 | rc = sqlite3ApiExit(db, rc); |
| 105082 | sqlite3_mutex_leave(db->mutex); |
| 105083 | return rc; |
| 105084 | } |
| 105085 | |
| @@ -104391,11 +105097,11 @@ | |
| 105097 | int rc; |
| 105098 | char *zFunc8; |
| 105099 | sqlite3_mutex_enter(db->mutex); |
| 105100 | assert( !db->mallocFailed ); |
| 105101 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 105102 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 105103 | sqlite3DbFree(db, zFunc8); |
| 105104 | rc = sqlite3ApiExit(db, rc); |
| 105105 | sqlite3_mutex_leave(db->mutex); |
| 105106 | return rc; |
| 105107 | } |
| @@ -104422,11 +105128,11 @@ | |
| 105128 | int nName = sqlite3Strlen30(zName); |
| 105129 | int rc; |
| 105130 | sqlite3_mutex_enter(db->mutex); |
| 105131 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 105132 | sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 105133 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 105134 | } |
| 105135 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 105136 | sqlite3_mutex_leave(db->mutex); |
| 105137 | return rc; |
| 105138 | } |
| @@ -104560,11 +105266,14 @@ | |
| 105266 | ** registered using sqlite3_wal_hook(). Likewise, registering a callback |
| 105267 | ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism |
| 105268 | ** configured by this function. |
| 105269 | */ |
| 105270 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 105271 | #ifdef SQLITE_OMIT_WAL |
| 105272 | UNUSED_PARAMETER(db); |
| 105273 | UNUSED_PARAMETER(nFrame); |
| 105274 | #else |
| 105275 | if( nFrame>0 ){ |
| 105276 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 105277 | }else{ |
| 105278 | sqlite3_wal_hook(db, 0, 0); |
| 105279 | } |
| @@ -104690,64 +105399,10 @@ | |
| 105399 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
| 105400 | return 0; |
| 105401 | #endif |
| 105402 | } |
| 105403 | |
| 105404 | /* |
| 105405 | ** Return UTF-8 encoded English language explanation of the most recent |
| 105406 | ** error. |
| 105407 | */ |
| 105408 | SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ |
| @@ -104986,21 +105641,43 @@ | |
| 105641 | ** It merely prevents new constructs that exceed the limit |
| 105642 | ** from forming. |
| 105643 | */ |
| 105644 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 105645 | int oldLimit; |
| 105646 | |
| 105647 | |
| 105648 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 105649 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 105650 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 105651 | ** "_MAX_".) |
| 105652 | */ |
| 105653 | assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); |
| 105654 | assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); |
| 105655 | assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); |
| 105656 | assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); |
| 105657 | assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); |
| 105658 | assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); |
| 105659 | assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); |
| 105660 | assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); |
| 105661 | assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== |
| 105662 | SQLITE_MAX_LIKE_PATTERN_LENGTH ); |
| 105663 | assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
| 105664 | assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
| 105665 | assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) ); |
| 105666 | |
| 105667 | |
| 105668 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 105669 | return -1; |
| 105670 | } |
| 105671 | oldLimit = db->aLimit[limitId]; |
| 105672 | if( newLimit>=0 ){ /* IMP: R-52476-28732 */ |
| 105673 | if( newLimit>aHardLimit[limitId] ){ |
| 105674 | newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ |
| 105675 | } |
| 105676 | db->aLimit[limitId] = newLimit; |
| 105677 | } |
| 105678 | return oldLimit; /* IMP: R-53341-35419 */ |
| 105679 | } |
| 105680 | |
| 105681 | /* |
| 105682 | ** This routine does the work of opening a database on behalf of |
| 105683 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| @@ -105019,10 +105696,28 @@ | |
| 105696 | *ppDb = 0; |
| 105697 | #ifndef SQLITE_OMIT_AUTOINIT |
| 105698 | rc = sqlite3_initialize(); |
| 105699 | if( rc ) return rc; |
| 105700 | #endif |
| 105701 | |
| 105702 | /* Only allow sensible combinations of bits in the flags argument. |
| 105703 | ** Throw an error if any non-sense combination is used. If we |
| 105704 | ** do not block illegal combinations here, it could trigger |
| 105705 | ** assert() statements in deeper layers. Sensible combinations |
| 105706 | ** are: |
| 105707 | ** |
| 105708 | ** 1: SQLITE_OPEN_READONLY |
| 105709 | ** 2: SQLITE_OPEN_READWRITE |
| 105710 | ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
| 105711 | */ |
| 105712 | assert( SQLITE_OPEN_READONLY == 0x01 ); |
| 105713 | assert( SQLITE_OPEN_READWRITE == 0x02 ); |
| 105714 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 105715 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 105716 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 105717 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 105718 | if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE; |
| 105719 | |
| 105720 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 105721 | isThreadsafe = 0; |
| 105722 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 105723 | isThreadsafe = 0; |
| @@ -105053,11 +105748,12 @@ | |
| 105748 | SQLITE_OPEN_MAIN_JOURNAL | |
| 105749 | SQLITE_OPEN_TEMP_JOURNAL | |
| 105750 | SQLITE_OPEN_SUBJOURNAL | |
| 105751 | SQLITE_OPEN_MASTER_JOURNAL | |
| 105752 | SQLITE_OPEN_NOMUTEX | |
| 105753 | SQLITE_OPEN_FULLMUTEX | |
| 105754 | SQLITE_OPEN_WAL |
| 105755 | ); |
| 105756 | |
| 105757 | /* Allocate the sqlite data structure */ |
| 105758 | db = sqlite3MallocZero( sizeof(sqlite3) ); |
| 105759 | if( db==0 ) goto opendb_out; |
| @@ -105125,13 +105821,12 @@ | |
| 105821 | createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, |
| 105822 | nocaseCollatingFunc, 0); |
| 105823 | |
| 105824 | /* Open the backend database driver */ |
| 105825 | db->openFlags = flags; |
| 105826 | rc = sqlite3BtreeOpen(zFilename, db, &db->aDb[0].pBt, 0, |
| 105827 | flags | SQLITE_OPEN_MAIN_DB); |
| 105828 | if( rc!=SQLITE_OK ){ |
| 105829 | if( rc==SQLITE_IOERR_NOMEM ){ |
| 105830 | rc = SQLITE_NOMEM; |
| 105831 | } |
| 105832 | sqlite3Error(db, rc, 0); |
| @@ -105833,10 +106528,26 @@ | |
| 106528 | */ |
| 106529 | case SQLITE_TESTCTRL_PGHDRSZ: { |
| 106530 | rc = sizeof(PgHdr); |
| 106531 | break; |
| 106532 | } |
| 106533 | |
| 106534 | /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); |
| 106535 | ** |
| 106536 | ** Pass pFree into sqlite3ScratchFree(). |
| 106537 | ** If sz>0 then allocate a scratch buffer into pNew. |
| 106538 | */ |
| 106539 | case SQLITE_TESTCTRL_SCRATCHMALLOC: { |
| 106540 | void *pFree, **ppNew; |
| 106541 | int sz; |
| 106542 | sz = va_arg(ap, int); |
| 106543 | ppNew = va_arg(ap, void**); |
| 106544 | pFree = va_arg(ap, void*); |
| 106545 | if( sz ) *ppNew = sqlite3ScratchMalloc(sz); |
| 106546 | sqlite3ScratchFree(pFree); |
| 106547 | break; |
| 106548 | } |
| 106549 | |
| 106550 | } |
| 106551 | va_end(ap); |
| 106552 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
| 106553 | return rc; |
| @@ -107022,10 +107733,11 @@ | |
| 107733 | ); |
| 107734 | SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char const**, int*); |
| 107735 | SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **); |
| 107736 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeLocal(Fts3Cursor*, u32*); |
| 107737 | SQLITE_PRIVATE int sqlite3Fts3MatchinfoDocsizeGlobal(Fts3Cursor*, u32*); |
| 107738 | SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *); |
| 107739 | |
| 107740 | /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */ |
| 107741 | #define FTS3_SEGMENT_REQUIRE_POS 0x00000001 |
| 107742 | #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 |
| 107743 | #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 |
| @@ -108971,10 +109683,13 @@ | |
| 109683 | zQuery); |
| 109684 | } |
| 109685 | return rc; |
| 109686 | } |
| 109687 | |
| 109688 | rc = sqlite3Fts3ReadLock(p); |
| 109689 | if( rc!=SQLITE_OK ) return rc; |
| 109690 | |
| 109691 | rc = evalFts3Expr(p, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0); |
| 109692 | pCsr->pNextId = pCsr->aDoclist; |
| 109693 | pCsr->iPrevId = 0; |
| 109694 | } |
| 109695 | |
| @@ -109349,15 +110064,18 @@ | |
| 110064 | static int fts3RenameMethod( |
| 110065 | sqlite3_vtab *pVtab, /* Virtual table handle */ |
| 110066 | const char *zName /* New name of table */ |
| 110067 | ){ |
| 110068 | Fts3Table *p = (Fts3Table *)pVtab; |
| 110069 | sqlite3 *db = p->db; /* Database connection */ |
| 110070 | int rc; /* Return Code */ |
| 110071 | |
| 110072 | rc = sqlite3Fts3PendingTermsFlush(p); |
| 110073 | if( rc!=SQLITE_OK ){ |
| 110074 | return rc; |
| 110075 | } |
| 110076 | |
| 110077 | fts3DbExec(&rc, db, |
| 110078 | "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 110079 | p->zDb, p->zName, zName |
| 110080 | ); |
| 110081 | if( rc==SQLITE_ERROR ) rc = SQLITE_OK; |
| @@ -112512,10 +113230,40 @@ | |
| 113230 | return SQLITE_CORRUPT; |
| 113231 | } |
| 113232 | } |
| 113233 | return SQLITE_OK; |
| 113234 | } |
| 113235 | |
| 113236 | /* |
| 113237 | ** This function ensures that the caller has obtained a shared-cache |
| 113238 | ** table-lock on the %_content table. This is required before reading |
| 113239 | ** data from the fts3 table. If this lock is not acquired first, then |
| 113240 | ** the caller may end up holding read-locks on the %_segments and %_segdir |
| 113241 | ** tables, but no read-lock on the %_content table. If this happens |
| 113242 | ** a second connection will be able to write to the fts3 table, but |
| 113243 | ** attempting to commit those writes might return SQLITE_LOCKED or |
| 113244 | ** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain |
| 113245 | ** write-locks on the %_segments and %_segdir ** tables). |
| 113246 | ** |
| 113247 | ** We try to avoid this because if FTS3 returns any error when committing |
| 113248 | ** a transaction, the whole transaction will be rolled back. And this is |
| 113249 | ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can |
| 113250 | ** still happen if the user reads data directly from the %_segments or |
| 113251 | ** %_segdir tables instead of going through FTS3 though. |
| 113252 | */ |
| 113253 | SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){ |
| 113254 | int rc; /* Return code */ |
| 113255 | sqlite3_stmt *pStmt; /* Statement used to obtain lock */ |
| 113256 | |
| 113257 | rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0); |
| 113258 | if( rc==SQLITE_OK ){ |
| 113259 | sqlite3_bind_null(pStmt, 1); |
| 113260 | sqlite3_step(pStmt); |
| 113261 | rc = sqlite3_reset(pStmt); |
| 113262 | } |
| 113263 | return rc; |
| 113264 | } |
| 113265 | |
| 113266 | /* |
| 113267 | ** Set *ppStmt to a statement handle that may be used to iterate through |
| 113268 | ** all rows in the %_segdir table, from oldest to newest. If successful, |
| 113269 | ** return SQLITE_OK. If an error occurs while preparing the statement, |
| @@ -116003,10 +116751,49 @@ | |
| 116751 | ** |
| 116752 | ************************************************************************* |
| 116753 | ** This file contains code for implementations of the r-tree and r*-tree |
| 116754 | ** algorithms packaged as an SQLite virtual table module. |
| 116755 | */ |
| 116756 | |
| 116757 | /* |
| 116758 | ** Database Format of R-Tree Tables |
| 116759 | ** -------------------------------- |
| 116760 | ** |
| 116761 | ** The data structure for a single virtual r-tree table is stored in three |
| 116762 | ** native SQLite tables declared as follows. In each case, the '%' character |
| 116763 | ** in the table name is replaced with the user-supplied name of the r-tree |
| 116764 | ** table. |
| 116765 | ** |
| 116766 | ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) |
| 116767 | ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) |
| 116768 | ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) |
| 116769 | ** |
| 116770 | ** The data for each node of the r-tree structure is stored in the %_node |
| 116771 | ** table. For each node that is not the root node of the r-tree, there is |
| 116772 | ** an entry in the %_parent table associating the node with its parent. |
| 116773 | ** And for each row of data in the table, there is an entry in the %_rowid |
| 116774 | ** table that maps from the entries rowid to the id of the node that it |
| 116775 | ** is stored on. |
| 116776 | ** |
| 116777 | ** The root node of an r-tree always exists, even if the r-tree table is |
| 116778 | ** empty. The nodeno of the root node is always 1. All other nodes in the |
| 116779 | ** table must be the same size as the root node. The content of each node |
| 116780 | ** is formatted as follows: |
| 116781 | ** |
| 116782 | ** 1. If the node is the root node (node 1), then the first 2 bytes |
| 116783 | ** of the node contain the tree depth as a big-endian integer. |
| 116784 | ** For non-root nodes, the first 2 bytes are left unused. |
| 116785 | ** |
| 116786 | ** 2. The next 2 bytes contain the number of entries currently |
| 116787 | ** stored in the node. |
| 116788 | ** |
| 116789 | ** 3. The remainder of the node contains the node entries. Each entry |
| 116790 | ** consists of a single 8-byte integer followed by an even number |
| 116791 | ** of 4-byte coordinates. For leaf nodes the integer is the rowid |
| 116792 | ** of a record. For internal nodes it is the node number of a |
| 116793 | ** child page. |
| 116794 | */ |
| 116795 | |
| 116796 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) |
| 116797 | |
| 116798 | /* |
| 116799 | ** This file contains an implementation of a couple of different variants |
| @@ -116044,18 +116831,22 @@ | |
| 116831 | #endif |
| 116832 | #if VARIANT_RSTARTREE_SPLIT |
| 116833 | #define AssignCells splitNodeStartree |
| 116834 | #endif |
| 116835 | |
| 116836 | #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) |
| 116837 | # define NDEBUG 1 |
| 116838 | #endif |
| 116839 | |
| 116840 | #ifndef SQLITE_CORE |
| 116841 | SQLITE_EXTENSION_INIT1 |
| 116842 | #else |
| 116843 | #endif |
| 116844 | |
| 116845 | |
| 116846 | #ifndef SQLITE_AMALGAMATION |
| 116847 | #include "sqlite3rtree.h" |
| 116848 | typedef sqlite3_int64 i64; |
| 116849 | typedef unsigned char u8; |
| 116850 | typedef unsigned int u32; |
| 116851 | #endif |
| 116852 | |
| @@ -116062,10 +116853,12 @@ | |
| 116853 | typedef struct Rtree Rtree; |
| 116854 | typedef struct RtreeCursor RtreeCursor; |
| 116855 | typedef struct RtreeNode RtreeNode; |
| 116856 | typedef struct RtreeCell RtreeCell; |
| 116857 | typedef struct RtreeConstraint RtreeConstraint; |
| 116858 | typedef struct RtreeMatchArg RtreeMatchArg; |
| 116859 | typedef struct RtreeGeomCallback RtreeGeomCallback; |
| 116860 | typedef union RtreeCoord RtreeCoord; |
| 116861 | |
| 116862 | /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ |
| 116863 | #define RTREE_MAX_DIMENSIONS 5 |
| 116864 | |
| @@ -116131,10 +116924,19 @@ | |
| 116924 | */ |
| 116925 | #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) |
| 116926 | #define RTREE_REINSERT(p) RTREE_MINCELLS(p) |
| 116927 | #define RTREE_MAXCELLS 51 |
| 116928 | |
| 116929 | /* |
| 116930 | ** The smallest possible node-size is (512-64)==448 bytes. And the largest |
| 116931 | ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). |
| 116932 | ** Therefore all non-root nodes must contain at least 3 entries. Since |
| 116933 | ** 2^40 is greater than 2^64, an r-tree structure always has a depth of |
| 116934 | ** 40 or less. |
| 116935 | */ |
| 116936 | #define RTREE_MAX_DEPTH 40 |
| 116937 | |
| 116938 | /* |
| 116939 | ** An rtree cursor object. |
| 116940 | */ |
| 116941 | struct RtreeCursor { |
| 116942 | sqlite3_vtab_cursor base; |
| @@ -116163,39 +116965,27 @@ | |
| 116965 | |
| 116966 | /* |
| 116967 | ** A search constraint. |
| 116968 | */ |
| 116969 | struct RtreeConstraint { |
| 116970 | int iCoord; /* Index of constrained coordinate */ |
| 116971 | int op; /* Constraining operation */ |
| 116972 | double rValue; /* Constraint value. */ |
| 116973 | int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); |
| 116974 | sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */ |
| 116975 | }; |
| 116976 | |
| 116977 | /* Possible values for RtreeConstraint.op */ |
| 116978 | #define RTREE_EQ 0x41 |
| 116979 | #define RTREE_LE 0x42 |
| 116980 | #define RTREE_LT 0x43 |
| 116981 | #define RTREE_GE 0x44 |
| 116982 | #define RTREE_GT 0x45 |
| 116983 | #define RTREE_MATCH 0x46 |
| 116984 | |
| 116985 | /* |
| 116986 | ** An rtree structure node. |
| 116987 | */ |
| 116988 | struct RtreeNode { |
| 116989 | RtreeNode *pParent; /* Parent node */ |
| 116990 | i64 iNode; |
| 116991 | int nRef; |
| @@ -116211,10 +117001,44 @@ | |
| 117001 | struct RtreeCell { |
| 117002 | i64 iRowid; |
| 117003 | RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; |
| 117004 | }; |
| 117005 | |
| 117006 | |
| 117007 | /* |
| 117008 | ** Value for the first field of every RtreeMatchArg object. The MATCH |
| 117009 | ** operator tests that the first field of a blob operand matches this |
| 117010 | ** value to avoid operating on invalid blobs (which could cause a segfault). |
| 117011 | */ |
| 117012 | #define RTREE_GEOMETRY_MAGIC 0x891245AB |
| 117013 | |
| 117014 | /* |
| 117015 | ** An instance of this structure must be supplied as a blob argument to |
| 117016 | ** the right-hand-side of an SQL MATCH operator used to constrain an |
| 117017 | ** r-tree query. |
| 117018 | */ |
| 117019 | struct RtreeMatchArg { |
| 117020 | u32 magic; /* Always RTREE_GEOMETRY_MAGIC */ |
| 117021 | int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); |
| 117022 | void *pContext; |
| 117023 | int nParam; |
| 117024 | double aParam[1]; |
| 117025 | }; |
| 117026 | |
| 117027 | /* |
| 117028 | ** When a geometry callback is created (see sqlite3_rtree_geometry_callback), |
| 117029 | ** a single instance of the following structure is allocated. It is used |
| 117030 | ** as the context for the user-function created by by s_r_g_c(). The object |
| 117031 | ** is eventually deleted by the destructor mechanism provided by |
| 117032 | ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create |
| 117033 | ** the geometry callback function). |
| 117034 | */ |
| 117035 | struct RtreeGeomCallback { |
| 117036 | int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); |
| 117037 | void *pContext; |
| 117038 | }; |
| 117039 | |
| 117040 | #ifndef MAX |
| 117041 | # define MAX(x,y) ((x) < (y) ? (y) : (x)) |
| 117042 | #endif |
| 117043 | #ifndef MIN |
| 117044 | # define MIN(x,y) ((x) > (y) ? (y) : (x)) |
| @@ -116293,14 +117117,12 @@ | |
| 117117 | |
| 117118 | /* |
| 117119 | ** Clear the content of node p (set all bytes to 0x00). |
| 117120 | */ |
| 117121 | static void nodeZero(Rtree *pRtree, RtreeNode *p){ |
| 117122 | memset(&p->zData[2], 0, pRtree->iNodeSize-2); |
| 117123 | p->isDirty = 1; |
| 117124 | } |
| 117125 | |
| 117126 | /* |
| 117127 | ** Given a node number iNode, return the corresponding key to use |
| 117128 | ** in the Rtree.aHash table. |
| @@ -116316,26 +117138,23 @@ | |
| 117138 | ** Search the node hash table for node iNode. If found, return a pointer |
| 117139 | ** to it. Otherwise, return 0. |
| 117140 | */ |
| 117141 | static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ |
| 117142 | RtreeNode *p; |
| 117143 | for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); |
| 117144 | return p; |
| 117145 | } |
| 117146 | |
| 117147 | /* |
| 117148 | ** Add node pNode to the node hash table. |
| 117149 | */ |
| 117150 | static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ |
| 117151 | int iHash; |
| 117152 | assert( pNode->pNext==0 ); |
| 117153 | iHash = nodeHash(pNode->iNode); |
| 117154 | pNode->pNext = pRtree->aHash[iHash]; |
| 117155 | pRtree->aHash[iHash] = pNode; |
| 117156 | } |
| 117157 | |
| 117158 | /* |
| 117159 | ** Remove node pNode from the node hash table. |
| 117160 | */ |
| @@ -116353,15 +117172,15 @@ | |
| 117172 | ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), |
| 117173 | ** indicating that node has not yet been assigned a node number. It is |
| 117174 | ** assigned a node number when nodeWrite() is called to write the |
| 117175 | ** node contents out to the database. |
| 117176 | */ |
| 117177 | static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){ |
| 117178 | RtreeNode *pNode; |
| 117179 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); |
| 117180 | if( pNode ){ |
| 117181 | memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); |
| 117182 | pNode->zData = (u8 *)&pNode[1]; |
| 117183 | pNode->nRef = 1; |
| 117184 | pNode->pParent = pParent; |
| 117185 | pNode->isDirty = 1; |
| 117186 | nodeReference(pParent); |
| @@ -116378,10 +117197,11 @@ | |
| 117197 | i64 iNode, /* Node number to load */ |
| 117198 | RtreeNode *pParent, /* Either the parent node or NULL */ |
| 117199 | RtreeNode **ppNode /* OUT: Acquired node */ |
| 117200 | ){ |
| 117201 | int rc; |
| 117202 | int rc2 = SQLITE_OK; |
| 117203 | RtreeNode *pNode; |
| 117204 | |
| 117205 | /* Check if the requested node is already in the hash table. If so, |
| 117206 | ** increase its reference count and return it. |
| 117207 | */ |
| @@ -116394,43 +117214,67 @@ | |
| 117214 | pNode->nRef++; |
| 117215 | *ppNode = pNode; |
| 117216 | return SQLITE_OK; |
| 117217 | } |
| 117218 | |
| 117219 | sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); |
| 117220 | rc = sqlite3_step(pRtree->pReadNode); |
| 117221 | if( rc==SQLITE_ROW ){ |
| 117222 | const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); |
| 117223 | if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){ |
| 117224 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); |
| 117225 | if( !pNode ){ |
| 117226 | rc2 = SQLITE_NOMEM; |
| 117227 | }else{ |
| 117228 | pNode->pParent = pParent; |
| 117229 | pNode->zData = (u8 *)&pNode[1]; |
| 117230 | pNode->nRef = 1; |
| 117231 | pNode->iNode = iNode; |
| 117232 | pNode->isDirty = 0; |
| 117233 | pNode->pNext = 0; |
| 117234 | memcpy(pNode->zData, zBlob, pRtree->iNodeSize); |
| 117235 | nodeReference(pParent); |
| 117236 | } |
| 117237 | } |
| 117238 | } |
| 117239 | rc = sqlite3_reset(pRtree->pReadNode); |
| 117240 | if( rc==SQLITE_OK ) rc = rc2; |
| 117241 | |
| 117242 | /* If the root node was just loaded, set pRtree->iDepth to the height |
| 117243 | ** of the r-tree structure. A height of zero means all data is stored on |
| 117244 | ** the root node. A height of one means the children of the root node |
| 117245 | ** are the leaves, and so on. If the depth as specified on the root node |
| 117246 | ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. |
| 117247 | */ |
| 117248 | if( pNode && iNode==1 ){ |
| 117249 | pRtree->iDepth = readInt16(pNode->zData); |
| 117250 | if( pRtree->iDepth>RTREE_MAX_DEPTH ){ |
| 117251 | rc = SQLITE_CORRUPT; |
| 117252 | } |
| 117253 | } |
| 117254 | |
| 117255 | /* If no error has occurred so far, check if the "number of entries" |
| 117256 | ** field on the node is too large. If so, set the return code to |
| 117257 | ** SQLITE_CORRUPT. |
| 117258 | */ |
| 117259 | if( pNode && rc==SQLITE_OK ){ |
| 117260 | if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){ |
| 117261 | rc = SQLITE_CORRUPT; |
| 117262 | } |
| 117263 | } |
| 117264 | |
| 117265 | if( rc==SQLITE_OK ){ |
| 117266 | if( pNode!=0 ){ |
| 117267 | nodeHashInsert(pRtree, pNode); |
| 117268 | }else{ |
| 117269 | rc = SQLITE_CORRUPT; |
| 117270 | } |
| 117271 | *ppNode = pNode; |
| 117272 | }else{ |
| 117273 | sqlite3_free(pNode); |
| 117274 | *ppNode = 0; |
| 117275 | } |
| 117276 | |
| 117277 | return rc; |
| 117278 | } |
| 117279 | |
| 117280 | /* |
| @@ -116479,12 +117323,11 @@ | |
| 117323 | int nMaxCell; /* Maximum number of cells for pNode */ |
| 117324 | |
| 117325 | nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; |
| 117326 | nCell = NCELL(pNode); |
| 117327 | |
| 117328 | assert( nCell<=nMaxCell ); |
| 117329 | if( nCell<nMaxCell ){ |
| 117330 | nodeOverwriteCell(pRtree, pNode, pCell, nCell); |
| 117331 | writeInt16(&pNode->zData[2], nCell+1); |
| 117332 | pNode->isDirty = 1; |
| 117333 | } |
| @@ -116700,18 +117543,37 @@ | |
| 117543 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 117544 | |
| 117545 | return rc; |
| 117546 | } |
| 117547 | |
| 117548 | |
| 117549 | /* |
| 117550 | ** Free the RtreeCursor.aConstraint[] array and its contents. |
| 117551 | */ |
| 117552 | static void freeCursorConstraints(RtreeCursor *pCsr){ |
| 117553 | if( pCsr->aConstraint ){ |
| 117554 | int i; /* Used to iterate through constraint array */ |
| 117555 | for(i=0; i<pCsr->nConstraint; i++){ |
| 117556 | sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom; |
| 117557 | if( pGeom ){ |
| 117558 | if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser); |
| 117559 | sqlite3_free(pGeom); |
| 117560 | } |
| 117561 | } |
| 117562 | sqlite3_free(pCsr->aConstraint); |
| 117563 | pCsr->aConstraint = 0; |
| 117564 | } |
| 117565 | } |
| 117566 | |
| 117567 | /* |
| 117568 | ** Rtree virtual table module xClose method. |
| 117569 | */ |
| 117570 | static int rtreeClose(sqlite3_vtab_cursor *cur){ |
| 117571 | Rtree *pRtree = (Rtree *)(cur->pVtab); |
| 117572 | int rc; |
| 117573 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 117574 | freeCursorConstraints(pCsr); |
| 117575 | rc = nodeRelease(pRtree, pCsr->pNode); |
| 117576 | sqlite3_free(pCsr); |
| 117577 | return rc; |
| 117578 | } |
| 117579 | |
| @@ -116723,18 +117585,44 @@ | |
| 117585 | */ |
| 117586 | static int rtreeEof(sqlite3_vtab_cursor *cur){ |
| 117587 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 117588 | return (pCsr->pNode==0); |
| 117589 | } |
| 117590 | |
| 117591 | /* |
| 117592 | ** The r-tree constraint passed as the second argument to this function is |
| 117593 | ** guaranteed to be a MATCH constraint. |
| 117594 | */ |
| 117595 | static int testRtreeGeom( |
| 117596 | Rtree *pRtree, /* R-Tree object */ |
| 117597 | RtreeConstraint *pConstraint, /* MATCH constraint to test */ |
| 117598 | RtreeCell *pCell, /* Cell to test */ |
| 117599 | int *pbRes /* OUT: Test result */ |
| 117600 | ){ |
| 117601 | int i; |
| 117602 | double aCoord[RTREE_MAX_DIMENSIONS*2]; |
| 117603 | int nCoord = pRtree->nDim*2; |
| 117604 | |
| 117605 | assert( pConstraint->op==RTREE_MATCH ); |
| 117606 | assert( pConstraint->pGeom ); |
| 117607 | |
| 117608 | for(i=0; i<nCoord; i++){ |
| 117609 | aCoord[i] = DCOORD(pCell->aCoord[i]); |
| 117610 | } |
| 117611 | return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes); |
| 117612 | } |
| 117613 | |
| 117614 | /* |
| 117615 | ** Cursor pCursor currently points to a cell in a non-leaf page. |
| 117616 | ** Set *pbEof to true if the sub-tree headed by the cell is filtered |
| 117617 | ** (excluded) by the constraints in the pCursor->aConstraint[] |
| 117618 | ** array, or false otherwise. |
| 117619 | ** |
| 117620 | ** Return SQLITE_OK if successful or an SQLite error code if an error |
| 117621 | ** occurs within a geometry callback. |
| 117622 | */ |
| 117623 | static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ |
| 117624 | RtreeCell cell; |
| 117625 | int ii; |
| 117626 | int bRes = 0; |
| 117627 | |
| 117628 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| @@ -116742,56 +117630,92 @@ | |
| 117630 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 117631 | double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]); |
| 117632 | double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]); |
| 117633 | |
| 117634 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 117635 | || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH |
| 117636 | ); |
| 117637 | |
| 117638 | switch( p->op ){ |
| 117639 | case RTREE_LE: case RTREE_LT: |
| 117640 | bRes = p->rValue<cell_min; |
| 117641 | break; |
| 117642 | |
| 117643 | case RTREE_GE: case RTREE_GT: |
| 117644 | bRes = p->rValue>cell_max; |
| 117645 | break; |
| 117646 | |
| 117647 | case RTREE_EQ: |
| 117648 | bRes = (p->rValue>cell_max || p->rValue<cell_min); |
| 117649 | break; |
| 117650 | |
| 117651 | default: { |
| 117652 | int rc; |
| 117653 | assert( p->op==RTREE_MATCH ); |
| 117654 | rc = testRtreeGeom(pRtree, p, &cell, &bRes); |
| 117655 | if( rc!=SQLITE_OK ){ |
| 117656 | return rc; |
| 117657 | } |
| 117658 | bRes = !bRes; |
| 117659 | break; |
| 117660 | } |
| 117661 | } |
| 117662 | } |
| 117663 | |
| 117664 | *pbEof = bRes; |
| 117665 | return SQLITE_OK; |
| 117666 | } |
| 117667 | |
| 117668 | /* |
| 117669 | ** Test if the cell that cursor pCursor currently points to |
| 117670 | ** would be filtered (excluded) by the constraints in the |
| 117671 | ** pCursor->aConstraint[] array. If so, set *pbEof to true before |
| 117672 | ** returning. If the cell is not filtered (excluded) by the constraints, |
| 117673 | ** set pbEof to zero. |
| 117674 | ** |
| 117675 | ** Return SQLITE_OK if successful or an SQLite error code if an error |
| 117676 | ** occurs within a geometry callback. |
| 117677 | ** |
| 117678 | ** This function assumes that the cell is part of a leaf node. |
| 117679 | */ |
| 117680 | static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ |
| 117681 | RtreeCell cell; |
| 117682 | int ii; |
| 117683 | *pbEof = 0; |
| 117684 | |
| 117685 | nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); |
| 117686 | for(ii=0; ii<pCursor->nConstraint; ii++){ |
| 117687 | RtreeConstraint *p = &pCursor->aConstraint[ii]; |
| 117688 | double coord = DCOORD(cell.aCoord[p->iCoord]); |
| 117689 | int res; |
| 117690 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 117691 | || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH |
| 117692 | ); |
| 117693 | switch( p->op ){ |
| 117694 | case RTREE_LE: res = (coord<=p->rValue); break; |
| 117695 | case RTREE_LT: res = (coord<p->rValue); break; |
| 117696 | case RTREE_GE: res = (coord>=p->rValue); break; |
| 117697 | case RTREE_GT: res = (coord>p->rValue); break; |
| 117698 | case RTREE_EQ: res = (coord==p->rValue); break; |
| 117699 | default: { |
| 117700 | int rc; |
| 117701 | assert( p->op==RTREE_MATCH ); |
| 117702 | rc = testRtreeGeom(pRtree, p, &cell, &res); |
| 117703 | if( rc!=SQLITE_OK ){ |
| 117704 | return rc; |
| 117705 | } |
| 117706 | break; |
| 117707 | } |
| 117708 | } |
| 117709 | |
| 117710 | if( !res ){ |
| 117711 | *pbEof = 1; |
| 117712 | return SQLITE_OK; |
| 117713 | } |
| 117714 | } |
| 117715 | |
| 117716 | return SQLITE_OK; |
| 117717 | } |
| 117718 | |
| 117719 | /* |
| 117720 | ** Cursor pCursor currently points at a node that heads a sub-tree of |
| 117721 | ** height iHeight (if iHeight==0, then the node is a leaf). Descend |
| @@ -116814,17 +117738,17 @@ | |
| 117738 | int iSavedCell = pCursor->iCell; |
| 117739 | |
| 117740 | assert( iHeight>=0 ); |
| 117741 | |
| 117742 | if( iHeight==0 ){ |
| 117743 | rc = testRtreeEntry(pRtree, pCursor, &isEof); |
| 117744 | }else{ |
| 117745 | rc = testRtreeCell(pRtree, pCursor, &isEof); |
| 117746 | } |
| 117747 | if( rc!=SQLITE_OK || isEof || iHeight==0 ){ |
| 117748 | *pEof = isEof; |
| 117749 | return rc; |
| 117750 | } |
| 117751 | |
| 117752 | iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell); |
| 117753 | rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild); |
| 117754 | if( rc!=SQLITE_OK ){ |
| @@ -116856,45 +117780,59 @@ | |
| 117780 | |
| 117781 | /* |
| 117782 | ** One of the cells in node pNode is guaranteed to have a 64-bit |
| 117783 | ** integer value equal to iRowid. Return the index of this cell. |
| 117784 | */ |
| 117785 | static int nodeRowidIndex( |
| 117786 | Rtree *pRtree, |
| 117787 | RtreeNode *pNode, |
| 117788 | i64 iRowid, |
| 117789 | int *piIndex |
| 117790 | ){ |
| 117791 | int ii; |
| 117792 | int nCell = NCELL(pNode); |
| 117793 | for(ii=0; ii<nCell; ii++){ |
| 117794 | if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){ |
| 117795 | *piIndex = ii; |
| 117796 | return SQLITE_OK; |
| 117797 | } |
| 117798 | } |
| 117799 | return SQLITE_CORRUPT; |
| 117800 | } |
| 117801 | |
| 117802 | /* |
| 117803 | ** Return the index of the cell containing a pointer to node pNode |
| 117804 | ** in its parent. If pNode is the root node, return -1. |
| 117805 | */ |
| 117806 | static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){ |
| 117807 | RtreeNode *pParent = pNode->pParent; |
| 117808 | if( pParent ){ |
| 117809 | return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex); |
| 117810 | } |
| 117811 | *piIndex = -1; |
| 117812 | return SQLITE_OK; |
| 117813 | } |
| 117814 | |
| 117815 | /* |
| 117816 | ** Rtree virtual table module xNext method. |
| 117817 | */ |
| 117818 | static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ |
| 117819 | Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab); |
| 117820 | RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; |
| 117821 | int rc = SQLITE_OK; |
| 117822 | |
| 117823 | /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is |
| 117824 | ** already at EOF. It is against the rules to call the xNext() method of |
| 117825 | ** a cursor that has already reached EOF. |
| 117826 | */ |
| 117827 | assert( pCsr->pNode ); |
| 117828 | |
| 117829 | if( pCsr->iStrategy==1 ){ |
| 117830 | /* This "scan" is a direct lookup by rowid. There is no next entry. */ |
| 117831 | nodeRelease(pRtree, pCsr->pNode); |
| 117832 | pCsr->pNode = 0; |
| 117833 | }else{ |
| 117834 | /* Move to the next entry that matches the configured constraints. */ |
| 117835 | int iHeight = 0; |
| 117836 | while( pCsr->pNode ){ |
| 117837 | RtreeNode *pNode = pCsr->pNode; |
| 117838 | int nCell = NCELL(pNode); |
| @@ -116904,11 +117842,14 @@ | |
| 117842 | if( rc!=SQLITE_OK || !isEof ){ |
| 117843 | return rc; |
| 117844 | } |
| 117845 | } |
| 117846 | pCsr->pNode = pNode->pParent; |
| 117847 | rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell); |
| 117848 | if( rc!=SQLITE_OK ){ |
| 117849 | return rc; |
| 117850 | } |
| 117851 | nodeReference(pCsr->pNode); |
| 117852 | nodeRelease(pRtree, pNode); |
| 117853 | iHeight++; |
| 117854 | } |
| 117855 | } |
| @@ -116972,10 +117913,55 @@ | |
| 117913 | rc = sqlite3_reset(pRtree->pReadRowid); |
| 117914 | } |
| 117915 | return rc; |
| 117916 | } |
| 117917 | |
| 117918 | /* |
| 117919 | ** This function is called to configure the RtreeConstraint object passed |
| 117920 | ** as the second argument for a MATCH constraint. The value passed as the |
| 117921 | ** first argument to this function is the right-hand operand to the MATCH |
| 117922 | ** operator. |
| 117923 | */ |
| 117924 | static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){ |
| 117925 | RtreeMatchArg *p; |
| 117926 | sqlite3_rtree_geometry *pGeom; |
| 117927 | int nBlob; |
| 117928 | |
| 117929 | /* Check that value is actually a blob. */ |
| 117930 | if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR; |
| 117931 | |
| 117932 | /* Check that the blob is roughly the right size. */ |
| 117933 | nBlob = sqlite3_value_bytes(pValue); |
| 117934 | if( nBlob<sizeof(RtreeMatchArg) |
| 117935 | || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0 |
| 117936 | ){ |
| 117937 | return SQLITE_ERROR; |
| 117938 | } |
| 117939 | |
| 117940 | pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc( |
| 117941 | sizeof(sqlite3_rtree_geometry) + nBlob |
| 117942 | ); |
| 117943 | if( !pGeom ) return SQLITE_NOMEM; |
| 117944 | memset(pGeom, 0, sizeof(sqlite3_rtree_geometry)); |
| 117945 | p = (RtreeMatchArg *)&pGeom[1]; |
| 117946 | |
| 117947 | memcpy(p, sqlite3_value_blob(pValue), nBlob); |
| 117948 | if( p->magic!=RTREE_GEOMETRY_MAGIC |
| 117949 | || nBlob!=(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double)) |
| 117950 | ){ |
| 117951 | sqlite3_free(pGeom); |
| 117952 | return SQLITE_ERROR; |
| 117953 | } |
| 117954 | |
| 117955 | pGeom->pContext = p->pContext; |
| 117956 | pGeom->nParam = p->nParam; |
| 117957 | pGeom->aParam = p->aParam; |
| 117958 | |
| 117959 | pCons->xGeom = p->xGeom; |
| 117960 | pCons->pGeom = pGeom; |
| 117961 | return SQLITE_OK; |
| 117962 | } |
| 117963 | |
| 117964 | /* |
| 117965 | ** Rtree virtual table module xFilter method. |
| 117966 | */ |
| 117967 | static int rtreeFilter( |
| @@ -116990,22 +117976,22 @@ | |
| 117976 | int ii; |
| 117977 | int rc = SQLITE_OK; |
| 117978 | |
| 117979 | rtreeReference(pRtree); |
| 117980 | |
| 117981 | freeCursorConstraints(pCsr); |
| 117982 | pCsr->iStrategy = idxNum; |
| 117983 | |
| 117984 | if( idxNum==1 ){ |
| 117985 | /* Special case - lookup by rowid. */ |
| 117986 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| 117987 | i64 iRowid = sqlite3_value_int64(argv[0]); |
| 117988 | rc = findLeafNode(pRtree, iRowid, &pLeaf); |
| 117989 | pCsr->pNode = pLeaf; |
| 117990 | if( pLeaf ){ |
| 117991 | assert( rc==SQLITE_OK ); |
| 117992 | rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell); |
| 117993 | } |
| 117994 | }else{ |
| 117995 | /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array |
| 117996 | ** with the configured constraints. |
| 117997 | */ |
| @@ -117013,16 +117999,28 @@ | |
| 117999 | pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); |
| 118000 | pCsr->nConstraint = argc; |
| 118001 | if( !pCsr->aConstraint ){ |
| 118002 | rc = SQLITE_NOMEM; |
| 118003 | }else{ |
| 118004 | memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); |
| 118005 | assert( (idxStr==0 && argc==0) || strlen(idxStr)==argc*2 ); |
| 118006 | for(ii=0; ii<argc; ii++){ |
| 118007 | RtreeConstraint *p = &pCsr->aConstraint[ii]; |
| 118008 | p->op = idxStr[ii*2]; |
| 118009 | p->iCoord = idxStr[ii*2+1]-'a'; |
| 118010 | if( p->op==RTREE_MATCH ){ |
| 118011 | /* A MATCH operator. The right-hand-side must be a blob that |
| 118012 | ** can be cast into an RtreeMatchArg object. One created using |
| 118013 | ** an sqlite3_rtree_geometry_callback() SQL user function. |
| 118014 | */ |
| 118015 | rc = deserializeGeometry(argv[ii], p); |
| 118016 | if( rc!=SQLITE_OK ){ |
| 118017 | break; |
| 118018 | } |
| 118019 | }else{ |
| 118020 | p->rValue = sqlite3_value_double(argv[ii]); |
| 118021 | } |
| 118022 | } |
| 118023 | } |
| 118024 | } |
| 118025 | |
| 118026 | if( rc==SQLITE_OK ){ |
| @@ -117078,10 +118076,11 @@ | |
| 118076 | ** = 0x41 ('A') |
| 118077 | ** <= 0x42 ('B') |
| 118078 | ** < 0x43 ('C') |
| 118079 | ** >= 0x44 ('D') |
| 118080 | ** > 0x45 ('E') |
| 118081 | ** MATCH 0x46 ('F') |
| 118082 | ** ---------------------- |
| 118083 | ** |
| 118084 | ** The second of each pair of bytes identifies the coordinate column |
| 118085 | ** to which the constraint applies. The leftmost coordinate column |
| 118086 | ** is 'a', the second from the left 'b' etc. |
| @@ -117116,43 +118115,47 @@ | |
| 118115 | */ |
| 118116 | pIdxInfo->estimatedCost = 10.0; |
| 118117 | return SQLITE_OK; |
| 118118 | } |
| 118119 | |
| 118120 | if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ |
| 118121 | int j, opmsk; |
| 118122 | static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 }; |
| 118123 | u8 op = 0; |
| 118124 | switch( p->op ){ |
| 118125 | case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; |
| 118126 | case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; |
| 118127 | case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; |
| 118128 | case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; |
| 118129 | case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; |
| 118130 | default: |
| 118131 | assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); |
| 118132 | op = RTREE_MATCH; |
| 118133 | break; |
| 118134 | } |
| 118135 | assert( op!=0 ); |
| 118136 | |
| 118137 | /* Make sure this particular constraint has not been used before. |
| 118138 | ** If it has been used before, ignore it. |
| 118139 | ** |
| 118140 | ** A <= or < can be used if there is a prior >= or >. |
| 118141 | ** A >= or > can be used if there is a prior < or <=. |
| 118142 | ** A <= or < is disqualified if there is a prior <=, <, or ==. |
| 118143 | ** A >= or > is disqualified if there is a prior >=, >, or ==. |
| 118144 | ** A == is disqualifed if there is any prior constraint. |
| 118145 | */ |
| 118146 | assert( compatible[RTREE_EQ & 7]==0 ); |
| 118147 | assert( compatible[RTREE_LT & 7]==1 ); |
| 118148 | assert( compatible[RTREE_LE & 7]==1 ); |
| 118149 | assert( compatible[RTREE_GT & 7]==2 ); |
| 118150 | assert( compatible[RTREE_GE & 7]==2 ); |
| 118151 | cCol = p->iColumn - 1 + 'a'; |
| 118152 | opmsk = compatible[op & 7]; |
| 118153 | for(j=0; j<iIdx; j+=2){ |
| 118154 | if( zIdxStr[j+1]==cCol && (compatible[zIdxStr[j] & 7] & opmsk)!=0 ){ |
| 118155 | op = 0; |
| 118156 | break; |
| 118157 | } |
| 118158 | } |
| 118159 | if( op ){ |
| 118160 | assert( iIdx<sizeof(zIdxStr)-1 ); |
| 118161 | zIdxStr[iIdx++] = op; |
| @@ -117256,11 +118259,16 @@ | |
| 118259 | int iExclude |
| 118260 | ){ |
| 118261 | int ii; |
| 118262 | float overlap = 0.0; |
| 118263 | for(ii=0; ii<nCell; ii++){ |
| 118264 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 118265 | if( ii!=iExclude ) |
| 118266 | #else |
| 118267 | assert( iExclude==-1 ); |
| 118268 | #endif |
| 118269 | { |
| 118270 | int jj; |
| 118271 | float o = 1.0; |
| 118272 | for(jj=0; jj<(pRtree->nDim*2); jj+=2){ |
| 118273 | double x1; |
| 118274 | double x2; |
| @@ -117349,26 +118357,35 @@ | |
| 118357 | /* Select the child node which will be enlarged the least if pCell |
| 118358 | ** is inserted into it. Resolve ties by choosing the entry with |
| 118359 | ** the smallest area. |
| 118360 | */ |
| 118361 | for(iCell=0; iCell<nCell; iCell++){ |
| 118362 | int bBest = 0; |
| 118363 | float growth; |
| 118364 | float area; |
| 118365 | float overlap = 0.0; |
| 118366 | nodeGetCell(pRtree, pNode, iCell, &cell); |
| 118367 | growth = cellGrowth(pRtree, &cell, pCell); |
| 118368 | area = cellArea(pRtree, &cell); |
| 118369 | |
| 118370 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 118371 | if( ii==(pRtree->iDepth-1) ){ |
| 118372 | overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); |
| 118373 | } |
| 118374 | if( (iCell==0) |
| 118375 | || (overlap<fMinOverlap) |
| 118376 | || (overlap==fMinOverlap && growth<fMinGrowth) |
| 118377 | || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea) |
| 118378 | ){ |
| 118379 | bBest = 1; |
| 118380 | } |
| 118381 | #else |
| 118382 | if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){ |
| 118383 | bBest = 1; |
| 118384 | } |
| 118385 | #endif |
| 118386 | if( bBest ){ |
| 118387 | fMinOverlap = overlap; |
| 118388 | fMinGrowth = growth; |
| 118389 | fMinArea = area; |
| 118390 | iBest = cell.iRowid; |
| 118391 | } |
| @@ -117387,29 +118404,34 @@ | |
| 118404 | /* |
| 118405 | ** A cell with the same content as pCell has just been inserted into |
| 118406 | ** the node pNode. This function updates the bounding box cells in |
| 118407 | ** all ancestor elements. |
| 118408 | */ |
| 118409 | static int AdjustTree( |
| 118410 | Rtree *pRtree, /* Rtree table */ |
| 118411 | RtreeNode *pNode, /* Adjust ancestry of this node. */ |
| 118412 | RtreeCell *pCell /* This cell was just inserted */ |
| 118413 | ){ |
| 118414 | RtreeNode *p = pNode; |
| 118415 | while( p->pParent ){ |
| 118416 | RtreeNode *pParent = p->pParent; |
| 118417 | RtreeCell cell; |
| 118418 | int iCell; |
| 118419 | |
| 118420 | if( nodeParentIndex(pRtree, p, &iCell) ){ |
| 118421 | return SQLITE_CORRUPT; |
| 118422 | } |
| 118423 | |
| 118424 | nodeGetCell(pRtree, pParent, iCell, &cell); |
| 118425 | if( !cellContains(pRtree, &cell, pCell) ){ |
| 118426 | cellUnion(pRtree, &cell, pCell); |
| 118427 | nodeOverwriteCell(pRtree, pParent, &cell, iCell); |
| 118428 | } |
| 118429 | |
| 118430 | p = pParent; |
| 118431 | } |
| 118432 | return SQLITE_OK; |
| 118433 | } |
| 118434 | |
| 118435 | /* |
| 118436 | ** Write mapping (iRowid->iNode) to the <rtree>_rowid table. |
| 118437 | */ |
| @@ -117934,18 +118956,18 @@ | |
| 118956 | nodeZero(pRtree, pNode); |
| 118957 | memcpy(&aCell[nCell], pCell, sizeof(RtreeCell)); |
| 118958 | nCell++; |
| 118959 | |
| 118960 | if( pNode->iNode==1 ){ |
| 118961 | pRight = nodeNew(pRtree, pNode); |
| 118962 | pLeft = nodeNew(pRtree, pNode); |
| 118963 | pRtree->iDepth++; |
| 118964 | pNode->isDirty = 1; |
| 118965 | writeInt16(pNode->zData, pRtree->iDepth); |
| 118966 | }else{ |
| 118967 | pLeft = pNode; |
| 118968 | pRight = nodeNew(pRtree, pLeft->pParent); |
| 118969 | nodeReference(pLeft); |
| 118970 | } |
| 118971 | |
| 118972 | if( !pLeft || !pRight ){ |
| 118973 | rc = SQLITE_NOMEM; |
| @@ -117958,12 +118980,16 @@ | |
| 118980 | rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox); |
| 118981 | if( rc!=SQLITE_OK ){ |
| 118982 | goto splitnode_out; |
| 118983 | } |
| 118984 | |
| 118985 | /* Ensure both child nodes have node numbers assigned to them by calling |
| 118986 | ** nodeWrite(). Node pRight always needs a node number, as it was created |
| 118987 | ** by nodeNew() above. But node pLeft sometimes already has a node number. |
| 118988 | ** In this case avoid the all to nodeWrite(). |
| 118989 | */ |
| 118990 | if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)) |
| 118991 | || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) |
| 118992 | ){ |
| 118993 | goto splitnode_out; |
| 118994 | } |
| 118995 | |
| @@ -117975,13 +119001,19 @@ | |
| 119001 | if( rc!=SQLITE_OK ){ |
| 119002 | goto splitnode_out; |
| 119003 | } |
| 119004 | }else{ |
| 119005 | RtreeNode *pParent = pLeft->pParent; |
| 119006 | int iCell; |
| 119007 | rc = nodeParentIndex(pRtree, pLeft, &iCell); |
| 119008 | if( rc==SQLITE_OK ){ |
| 119009 | nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); |
| 119010 | rc = AdjustTree(pRtree, pParent, &leftbbox); |
| 119011 | } |
| 119012 | if( rc!=SQLITE_OK ){ |
| 119013 | goto splitnode_out; |
| 119014 | } |
| 119015 | } |
| 119016 | if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ |
| 119017 | goto splitnode_out; |
| 119018 | } |
| 119019 | |
| @@ -118021,44 +119053,73 @@ | |
| 119053 | nodeRelease(pRtree, pLeft); |
| 119054 | sqlite3_free(aCell); |
| 119055 | return rc; |
| 119056 | } |
| 119057 | |
| 119058 | /* |
| 119059 | ** If node pLeaf is not the root of the r-tree and its pParent pointer is |
| 119060 | ** still NULL, load all ancestor nodes of pLeaf into memory and populate |
| 119061 | ** the pLeaf->pParent chain all the way up to the root node. |
| 119062 | ** |
| 119063 | ** This operation is required when a row is deleted (or updated - an update |
| 119064 | ** is implemented as a delete followed by an insert). SQLite provides the |
| 119065 | ** rowid of the row to delete, which can be used to find the leaf on which |
| 119066 | ** the entry resides (argument pLeaf). Once the leaf is located, this |
| 119067 | ** function is called to determine its ancestry. |
| 119068 | */ |
| 119069 | static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ |
| 119070 | int rc = SQLITE_OK; |
| 119071 | RtreeNode *pChild = pLeaf; |
| 119072 | while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){ |
| 119073 | int rc2 = SQLITE_OK; /* sqlite3_reset() return code */ |
| 119074 | sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode); |
| 119075 | rc = sqlite3_step(pRtree->pReadParent); |
| 119076 | if( rc==SQLITE_ROW ){ |
| 119077 | RtreeNode *pTest; /* Used to test for reference loops */ |
| 119078 | i64 iNode; /* Node number of parent node */ |
| 119079 | |
| 119080 | /* Before setting pChild->pParent, test that we are not creating a |
| 119081 | ** loop of references (as we would if, say, pChild==pParent). We don't |
| 119082 | ** want to do this as it leads to a memory leak when trying to delete |
| 119083 | ** the referenced counted node structures. |
| 119084 | */ |
| 119085 | iNode = sqlite3_column_int64(pRtree->pReadParent, 0); |
| 119086 | for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent); |
| 119087 | if( !pTest ){ |
| 119088 | rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent); |
| 119089 | } |
| 119090 | } |
| 119091 | rc = sqlite3_reset(pRtree->pReadParent); |
| 119092 | if( rc==SQLITE_OK ) rc = rc2; |
| 119093 | if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT; |
| 119094 | pChild = pChild->pParent; |
| 119095 | } |
| 119096 | return rc; |
| 119097 | } |
| 119098 | |
| 119099 | static int deleteCell(Rtree *, RtreeNode *, int, int); |
| 119100 | |
| 119101 | static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ |
| 119102 | int rc; |
| 119103 | int rc2; |
| 119104 | RtreeNode *pParent; |
| 119105 | int iCell; |
| 119106 | |
| 119107 | assert( pNode->nRef==1 ); |
| 119108 | |
| 119109 | /* Remove the entry in the parent cell. */ |
| 119110 | rc = nodeParentIndex(pRtree, pNode, &iCell); |
| 119111 | if( rc==SQLITE_OK ){ |
| 119112 | pParent = pNode->pParent; |
| 119113 | pNode->pParent = 0; |
| 119114 | rc = deleteCell(pRtree, pParent, iCell, iHeight+1); |
| 119115 | } |
| 119116 | rc2 = nodeRelease(pRtree, pParent); |
| 119117 | if( rc==SQLITE_OK ){ |
| 119118 | rc = rc2; |
| 119119 | } |
| 119120 | if( rc!=SQLITE_OK ){ |
| 119121 | return rc; |
| 119122 | } |
| 119123 | |
| 119124 | /* Remove the xxx_node entry. */ |
| 119125 | sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); |
| @@ -118084,12 +119145,13 @@ | |
| 119145 | pRtree->pDeleted = pNode; |
| 119146 | |
| 119147 | return SQLITE_OK; |
| 119148 | } |
| 119149 | |
| 119150 | static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ |
| 119151 | RtreeNode *pParent = pNode->pParent; |
| 119152 | int rc = SQLITE_OK; |
| 119153 | if( pParent ){ |
| 119154 | int ii; |
| 119155 | int nCell = NCELL(pNode); |
| 119156 | RtreeCell box; /* Bounding box for pNode */ |
| 119157 | nodeGetCell(pRtree, pNode, 0, &box); |
| @@ -118097,21 +119159,25 @@ | |
| 119159 | RtreeCell cell; |
| 119160 | nodeGetCell(pRtree, pNode, ii, &cell); |
| 119161 | cellUnion(pRtree, &box, &cell); |
| 119162 | } |
| 119163 | box.iRowid = pNode->iNode; |
| 119164 | rc = nodeParentIndex(pRtree, pNode, &ii); |
| 119165 | if( rc==SQLITE_OK ){ |
| 119166 | nodeOverwriteCell(pRtree, pParent, &box, ii); |
| 119167 | rc = fixBoundingBox(pRtree, pParent); |
| 119168 | } |
| 119169 | } |
| 119170 | return rc; |
| 119171 | } |
| 119172 | |
| 119173 | /* |
| 119174 | ** Delete the cell at index iCell of node pNode. After removing the |
| 119175 | ** cell, adjust the r-tree data structure if required. |
| 119176 | */ |
| 119177 | static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ |
| 119178 | RtreeNode *pParent; |
| 119179 | int rc; |
| 119180 | |
| 119181 | if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ |
| 119182 | return rc; |
| 119183 | } |
| @@ -118124,18 +119190,17 @@ | |
| 119190 | /* If the node is not the tree root and now has less than the minimum |
| 119191 | ** number of cells, remove it from the tree. Otherwise, update the |
| 119192 | ** cell in the parent node so that it tightly contains the updated |
| 119193 | ** node. |
| 119194 | */ |
| 119195 | pParent = pNode->pParent; |
| 119196 | assert( pParent || pNode->iNode==1 ); |
| 119197 | if( pParent ){ |
| 119198 | if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){ |
| 119199 | rc = removeNode(pRtree, pNode, iHeight); |
| 119200 | }else{ |
| 119201 | rc = fixBoundingBox(pRtree, pNode); |
| 119202 | } |
| 119203 | } |
| 119204 | |
| 119205 | return rc; |
| 119206 | } |
| @@ -118214,11 +119279,11 @@ | |
| 119279 | rc = parentWrite(pRtree, p->iRowid, pNode->iNode); |
| 119280 | } |
| 119281 | } |
| 119282 | } |
| 119283 | if( rc==SQLITE_OK ){ |
| 119284 | rc = fixBoundingBox(pRtree, pNode); |
| 119285 | } |
| 119286 | for(; rc==SQLITE_OK && ii<nCell; ii++){ |
| 119287 | /* Find a node to store this cell in. pNode->iNode currently contains |
| 119288 | ** the height of the sub-tree headed by the cell. |
| 119289 | */ |
| @@ -118268,15 +119333,17 @@ | |
| 119333 | } |
| 119334 | #else |
| 119335 | rc = SplitNode(pRtree, pNode, pCell, iHeight); |
| 119336 | #endif |
| 119337 | }else{ |
| 119338 | rc = AdjustTree(pRtree, pNode, pCell); |
| 119339 | if( rc==SQLITE_OK ){ |
| 119340 | if( iHeight==0 ){ |
| 119341 | rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); |
| 119342 | }else{ |
| 119343 | rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); |
| 119344 | } |
| 119345 | } |
| 119346 | } |
| 119347 | return rc; |
| 119348 | } |
| 119349 | |
| @@ -118342,11 +119409,10 @@ | |
| 119409 | int rc = SQLITE_OK; |
| 119410 | |
| 119411 | rtreeReference(pRtree); |
| 119412 | |
| 119413 | assert(nData>=1); |
| 119414 | |
| 119415 | /* If azData[0] is not an SQL NULL value, it is the rowid of a |
| 119416 | ** record to delete from the r-tree table. The following block does |
| 119417 | ** just that. |
| 119418 | */ |
| @@ -118368,12 +119434,14 @@ | |
| 119434 | } |
| 119435 | |
| 119436 | /* Delete the cell in question from the leaf node. */ |
| 119437 | if( rc==SQLITE_OK ){ |
| 119438 | int rc2; |
| 119439 | rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell); |
| 119440 | if( rc==SQLITE_OK ){ |
| 119441 | rc = deleteCell(pRtree, pLeaf, iCell, 0); |
| 119442 | } |
| 119443 | rc2 = nodeRelease(pRtree, pLeaf); |
| 119444 | if( rc==SQLITE_OK ){ |
| 119445 | rc = rc2; |
| 119446 | } |
| 119447 | } |
| @@ -118391,23 +119459,24 @@ | |
| 119459 | ** |
| 119460 | ** This is equivalent to copying the contents of the child into |
| 119461 | ** the root node (the operation that Gutman's paper says to perform |
| 119462 | ** in this scenario). |
| 119463 | */ |
| 119464 | if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){ |
| 119465 | int rc2; |
| 119466 | RtreeNode *pChild; |
| 119467 | i64 iChild = nodeGetRowid(pRtree, pRoot, 0); |
| 119468 | rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); |
| 119469 | if( rc==SQLITE_OK ){ |
| 119470 | rc = removeNode(pRtree, pChild, pRtree->iDepth-1); |
| 119471 | } |
| 119472 | rc2 = nodeRelease(pRtree, pChild); |
| 119473 | if( rc==SQLITE_OK ) rc = rc2; |
| 119474 | if( rc==SQLITE_OK ){ |
| 119475 | pRtree->iDepth--; |
| 119476 | writeInt16(pRoot->zData, pRtree->iDepth); |
| 119477 | pRoot->isDirty = 1; |
| 119478 | } |
| 119479 | } |
| 119480 | |
| 119481 | /* Re-insert the contents of any underfull nodes removed from the tree. */ |
| 119482 | for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ |
| @@ -118693,11 +119762,11 @@ | |
| 119762 | ){ |
| 119763 | int rc = SQLITE_OK; |
| 119764 | Rtree *pRtree; |
| 119765 | int nDb; /* Length of string argv[1] */ |
| 119766 | int nName; /* Length of string argv[2] */ |
| 119767 | int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); |
| 119768 | |
| 119769 | const char *aErrMsg[] = { |
| 119770 | 0, /* 0 */ |
| 119771 | "Wrong number of columns for an rtree table", /* 1 */ |
| 119772 | "Too few columns for an rtree table", /* 2 */ |
| @@ -118839,16 +119908,14 @@ | |
| 119908 | ** Register the r-tree module with database handle db. This creates the |
| 119909 | ** virtual table module "rtree" and the debugging/analysis scalar |
| 119910 | ** function "rtreenode". |
| 119911 | */ |
| 119912 | SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){ |
| 119913 | const int utf8 = SQLITE_UTF8; |
| 119914 | int rc; |
| 119915 | |
| 119916 | rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); |
| 119917 | if( rc==SQLITE_OK ){ |
| 119918 | int utf8 = SQLITE_UTF8; |
| 119919 | rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0); |
| 119920 | } |
| 119921 | if( rc==SQLITE_OK ){ |
| @@ -118860,10 +119927,74 @@ | |
| 119927 | rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0); |
| 119928 | } |
| 119929 | |
| 119930 | return rc; |
| 119931 | } |
| 119932 | |
| 119933 | /* |
| 119934 | ** A version of sqlite3_free() that can be used as a callback. This is used |
| 119935 | ** in two places - as the destructor for the blob value returned by the |
| 119936 | ** invocation of a geometry function, and as the destructor for the geometry |
| 119937 | ** functions themselves. |
| 119938 | */ |
| 119939 | static void doSqlite3Free(void *p){ |
| 119940 | sqlite3_free(p); |
| 119941 | } |
| 119942 | |
| 119943 | /* |
| 119944 | ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite |
| 119945 | ** scalar user function. This C function is the callback used for all such |
| 119946 | ** registered SQL functions. |
| 119947 | ** |
| 119948 | ** The scalar user functions return a blob that is interpreted by r-tree |
| 119949 | ** table MATCH operators. |
| 119950 | */ |
| 119951 | static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 119952 | RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx); |
| 119953 | RtreeMatchArg *pBlob; |
| 119954 | int nBlob; |
| 119955 | |
| 119956 | nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double); |
| 119957 | pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob); |
| 119958 | if( !pBlob ){ |
| 119959 | sqlite3_result_error_nomem(ctx); |
| 119960 | }else{ |
| 119961 | int i; |
| 119962 | pBlob->magic = RTREE_GEOMETRY_MAGIC; |
| 119963 | pBlob->xGeom = pGeomCtx->xGeom; |
| 119964 | pBlob->pContext = pGeomCtx->pContext; |
| 119965 | pBlob->nParam = nArg; |
| 119966 | for(i=0; i<nArg; i++){ |
| 119967 | pBlob->aParam[i] = sqlite3_value_double(aArg[i]); |
| 119968 | } |
| 119969 | sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free); |
| 119970 | } |
| 119971 | } |
| 119972 | |
| 119973 | /* |
| 119974 | ** Register a new geometry function for use with the r-tree MATCH operator. |
| 119975 | */ |
| 119976 | SQLITE_API int sqlite3_rtree_geometry_callback( |
| 119977 | sqlite3 *db, |
| 119978 | const char *zGeom, |
| 119979 | int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *), |
| 119980 | void *pContext |
| 119981 | ){ |
| 119982 | RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ |
| 119983 | |
| 119984 | /* Allocate and populate the context object. */ |
| 119985 | pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); |
| 119986 | if( !pGeomCtx ) return SQLITE_NOMEM; |
| 119987 | pGeomCtx->xGeom = xGeom; |
| 119988 | pGeomCtx->pContext = pContext; |
| 119989 | |
| 119990 | /* Create the new user-function. Register a destructor function to delete |
| 119991 | ** the context object when it is no longer required. */ |
| 119992 | return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, |
| 119993 | (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free |
| 119994 | ); |
| 119995 | } |
| 119996 | |
| 119997 | #if !SQLITE_CORE |
| 119998 | SQLITE_API int sqlite3_extension_init( |
| 119999 | sqlite3 *db, |
| 120000 | char **pzErrMsg, |
| 120001 |
+469
-251
| --- src/sqlite3.h | ||
| +++ src/sqlite3.h | ||
| @@ -105,13 +105,13 @@ | ||
| 105 | 105 | ** |
| 106 | 106 | ** See also: [sqlite3_libversion()], |
| 107 | 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | 109 | */ |
| 110 | -#define SQLITE_VERSION "3.7.2" | |
| 111 | -#define SQLITE_VERSION_NUMBER 3007002 | |
| 112 | -#define SQLITE_SOURCE_ID "2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3" | |
| 110 | +#define SQLITE_VERSION "3.7.3" | |
| 111 | +#define SQLITE_VERSION_NUMBER 3007003 | |
| 112 | +#define SQLITE_SOURCE_ID "2010-09-29 23:09:23 1ef0dc9328f47506cb2dcd142150e96cb4755216" | |
| 113 | 113 | |
| 114 | 114 | /* |
| 115 | 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | 117 | ** |
| @@ -755,19 +755,23 @@ | ||
| 755 | 755 | ** object once the object has been registered. |
| 756 | 756 | ** |
| 757 | 757 | ** The zName field holds the name of the VFS module. The name must |
| 758 | 758 | ** be unique across all VFS modules. |
| 759 | 759 | ** |
| 760 | -** SQLite will guarantee that the zFilename parameter to xOpen | |
| 760 | +** ^SQLite guarantees that the zFilename parameter to xOpen | |
| 761 | 761 | ** is either a NULL pointer or string obtained |
| 762 | -** from xFullPathname(). SQLite further guarantees that | |
| 762 | +** from xFullPathname() with an optional suffix added. | |
| 763 | +** ^If a suffix is added to the zFilename parameter, it will | |
| 764 | +** consist of a single "-" character followed by no more than | |
| 765 | +** 10 alphanumeric and/or "-" characters. | |
| 766 | +** ^SQLite further guarantees that | |
| 763 | 767 | ** the string will be valid and unchanged until xClose() is |
| 764 | 768 | ** called. Because of the previous sentence, |
| 765 | 769 | ** the [sqlite3_file] can safely store a pointer to the |
| 766 | 770 | ** filename if it needs to remember the filename for some reason. |
| 767 | -** If the zFilename parameter is xOpen is a NULL pointer then xOpen | |
| 768 | -** must invent its own temporary name for the file. Whenever the | |
| 771 | +** If the zFilename parameter to xOpen is a NULL pointer then xOpen | |
| 772 | +** must invent its own temporary name for the file. ^Whenever the | |
| 769 | 773 | ** xFilename parameter is NULL it will also be the case that the |
| 770 | 774 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 771 | 775 | ** |
| 772 | 776 | ** The flags argument to xOpen() includes all bits set in |
| 773 | 777 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -774,11 +778,11 @@ | ||
| 774 | 778 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 775 | 779 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 776 | 780 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 777 | 781 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 778 | 782 | ** |
| 779 | -** SQLite will also add one of the following flags to the xOpen() | |
| 783 | +** ^(SQLite will also add one of the following flags to the xOpen() | |
| 780 | 784 | ** call, depending on the object being opened: |
| 781 | 785 | ** |
| 782 | 786 | ** <ul> |
| 783 | 787 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 784 | 788 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -785,11 +789,12 @@ | ||
| 785 | 789 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 786 | 790 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 787 | 791 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 788 | 792 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 789 | 793 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 790 | -** </ul> | |
| 794 | +** <li> [SQLITE_OPEN_WAL] | |
| 795 | +** </ul>)^ | |
| 791 | 796 | ** |
| 792 | 797 | ** The file I/O implementation can use the object type flags to |
| 793 | 798 | ** change the way it deals with files. For example, an application |
| 794 | 799 | ** that does not care about crash recovery or rollback might make |
| 795 | 800 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -804,39 +809,40 @@ | ||
| 804 | 809 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 805 | 810 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 806 | 811 | ** </ul> |
| 807 | 812 | ** |
| 808 | 813 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 809 | -** deleted when it is closed. The [SQLITE_OPEN_DELETEONCLOSE] | |
| 810 | -** will be set for TEMP databases, journals and for subjournals. | |
| 814 | +** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] | |
| 815 | +** will be set for TEMP databases and their journals, transient | |
| 816 | +** databases, and subjournals. | |
| 811 | 817 | ** |
| 812 | -** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction | |
| 818 | +** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction | |
| 813 | 819 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 814 | 820 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 815 | 821 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 816 | 822 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 817 | 823 | ** be created, and that it is an error if it already exists. |
| 818 | 824 | ** It is <i>not</i> used to indicate the file should be opened |
| 819 | 825 | ** for exclusive access. |
| 820 | 826 | ** |
| 821 | -** At least szOsFile bytes of memory are allocated by SQLite | |
| 827 | +** ^At least szOsFile bytes of memory are allocated by SQLite | |
| 822 | 828 | ** to hold the [sqlite3_file] structure passed as the third |
| 823 | 829 | ** argument to xOpen. The xOpen method does not have to |
| 824 | 830 | ** allocate the structure; it should just fill it in. Note that |
| 825 | 831 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 826 | 832 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 827 | 833 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 828 | 834 | ** element will be valid after xOpen returns regardless of the success |
| 829 | 835 | ** or failure of the xOpen call. |
| 830 | 836 | ** |
| 831 | -** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] | |
| 837 | +** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] | |
| 832 | 838 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 833 | 839 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 834 | 840 | ** to test whether a file is at least readable. The file can be a |
| 835 | 841 | ** directory. |
| 836 | 842 | ** |
| 837 | -** SQLite will always allocate at least mxPathname+1 bytes for the | |
| 843 | +** ^SQLite will always allocate at least mxPathname+1 bytes for the | |
| 838 | 844 | ** output buffer xFullPathname. The exact size of the output buffer |
| 839 | 845 | ** is also passed as a parameter to both methods. If the output buffer |
| 840 | 846 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 841 | 847 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 842 | 848 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -846,14 +852,14 @@ | ||
| 846 | 852 | ** included in the VFS structure for completeness. |
| 847 | 853 | ** The xRandomness() function attempts to return nBytes bytes |
| 848 | 854 | ** of good-quality randomness into zOut. The return value is |
| 849 | 855 | ** the actual number of bytes of randomness obtained. |
| 850 | 856 | ** The xSleep() method causes the calling thread to sleep for at |
| 851 | -** least the number of microseconds given. The xCurrentTime() | |
| 857 | +** least the number of microseconds given. ^The xCurrentTime() | |
| 852 | 858 | ** method returns a Julian Day Number for the current date and time as |
| 853 | 859 | ** a floating point value. |
| 854 | -** The xCurrentTimeInt64() method returns, as an integer, the Julian | |
| 860 | +** ^The xCurrentTimeInt64() method returns, as an integer, the Julian | |
| 855 | 861 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 856 | 862 | ** a 24-hour day). |
| 857 | 863 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 858 | 864 | ** date and time if that method is available (if iVersion is 2 or |
| 859 | 865 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1246,11 +1252,11 @@ | ||
| 1246 | 1252 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1247 | 1253 | ** following SQLite interfaces become non-operational: |
| 1248 | 1254 | ** <ul> |
| 1249 | 1255 | ** <li> [sqlite3_memory_used()] |
| 1250 | 1256 | ** <li> [sqlite3_memory_highwater()] |
| 1251 | -** <li> [sqlite3_soft_heap_limit()] | |
| 1257 | +** <li> [sqlite3_soft_heap_limit64()] | |
| 1252 | 1258 | ** <li> [sqlite3_status()] |
| 1253 | 1259 | ** </ul>)^ |
| 1254 | 1260 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1255 | 1261 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1256 | 1262 | ** allocation statistics are disabled by default. |
| @@ -1260,19 +1266,18 @@ | ||
| 1260 | 1266 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1261 | 1267 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1262 | 1268 | ** aligned memory buffer from which the scrach allocations will be |
| 1263 | 1269 | ** drawn, the size of each scratch allocation (sz), |
| 1264 | 1270 | ** and the maximum number of scratch allocations (N). The sz |
| 1265 | -** argument must be a multiple of 16. The sz parameter should be a few bytes | |
| 1266 | -** larger than the actual scratch space required due to internal overhead. | |
| 1271 | +** argument must be a multiple of 16. | |
| 1267 | 1272 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1268 | 1273 | ** of at least sz*N bytes of memory. |
| 1269 | -** ^SQLite will use no more than one scratch buffer per thread. So | |
| 1270 | -** N should be set to the expected maximum number of threads. ^SQLite will | |
| 1271 | -** never require a scratch buffer that is more than 6 times the database | |
| 1272 | -** page size. ^If SQLite needs needs additional scratch memory beyond | |
| 1273 | -** what is provided by this configuration option, then | |
| 1274 | +** ^SQLite will use no more than two scratch buffers per thread. So | |
| 1275 | +** N should be set to twice the expected maximum number of threads. | |
| 1276 | +** ^SQLite will never require a scratch buffer that is more than 6 | |
| 1277 | +** times the database page size. ^If SQLite needs needs additional | |
| 1278 | +** scratch memory beyond what is provided by this configuration option, then | |
| 1274 | 1279 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1275 | 1280 | ** |
| 1276 | 1281 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1277 | 1282 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1278 | 1283 | ** the database page cache with the default page cache implemenation. |
| @@ -1288,12 +1293,11 @@ | ||
| 1288 | 1293 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1289 | 1294 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1290 | 1295 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1291 | 1296 | ** page cache memory is needed beyond what is provided by this option, then |
| 1292 | 1297 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1293 | -** ^The implementation might use one or more of the N buffers to hold | |
| 1294 | -** memory accounting information. The pointer in the first argument must | |
| 1298 | +** The pointer in the first argument must | |
| 1295 | 1299 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1296 | 1300 | ** will be undefined.</dd> |
| 1297 | 1301 | ** |
| 1298 | 1302 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1299 | 1303 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1418,12 +1422,18 @@ | ||
| 1418 | 1422 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1419 | 1423 | ** slots. The size of the buffer in the first argument must be greater than |
| 1420 | 1424 | ** or equal to the product of the second and third arguments. The buffer |
| 1421 | 1425 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1422 | 1426 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1423 | -** rounded down to the next smaller | |
| 1424 | -** multiple of 8. See also: [SQLITE_CONFIG_LOOKASIDE]</dd> | |
| 1427 | +** rounded down to the next smaller multiple of 8. ^(The lookaside memory | |
| 1428 | +** configuration for a database connection can only be changed when that | |
| 1429 | +** connection is not currently using lookaside memory, or in other words | |
| 1430 | +** when the "current value" returned by | |
| 1431 | +** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. | |
| 1432 | +** Any attempt to change the lookaside memory configuration when lookaside | |
| 1433 | +** memory is in use leaves the configuration unchanged and returns | |
| 1434 | +** [SQLITE_BUSY].)^</dd> | |
| 1425 | 1435 | ** |
| 1426 | 1436 | ** </dl> |
| 1427 | 1437 | */ |
| 1428 | 1438 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1429 | 1439 | |
| @@ -1723,10 +1733,13 @@ | ||
| 1723 | 1733 | */ |
| 1724 | 1734 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 1725 | 1735 | |
| 1726 | 1736 | /* |
| 1727 | 1737 | ** CAPI3REF: Convenience Routines For Running Queries |
| 1738 | +** | |
| 1739 | +** This is a legacy interface that is preserved for backwards compatibility. | |
| 1740 | +** Use of this interface is not recommended. | |
| 1728 | 1741 | ** |
| 1729 | 1742 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 1730 | 1743 | ** [sqlite3_get_table()] interface. A result table records the |
| 1731 | 1744 | ** complete query results from one or more queries. |
| 1732 | 1745 | ** |
| @@ -1744,11 +1757,11 @@ | ||
| 1744 | 1757 | ** |
| 1745 | 1758 | ** A result table might consist of one or more memory allocations. |
| 1746 | 1759 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 1747 | 1760 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 1748 | 1761 | ** |
| 1749 | -** As an example of the result table format, suppose a query result | |
| 1762 | +** ^(As an example of the result table format, suppose a query result | |
| 1750 | 1763 | ** is as follows: |
| 1751 | 1764 | ** |
| 1752 | 1765 | ** <blockquote><pre> |
| 1753 | 1766 | ** Name | Age |
| 1754 | 1767 | ** ----------------------- |
| @@ -1768,31 +1781,31 @@ | ||
| 1768 | 1781 | ** azResult[3] = "43"; |
| 1769 | 1782 | ** azResult[4] = "Bob"; |
| 1770 | 1783 | ** azResult[5] = "28"; |
| 1771 | 1784 | ** azResult[6] = "Cindy"; |
| 1772 | 1785 | ** azResult[7] = "21"; |
| 1773 | -** </pre></blockquote> | |
| 1786 | +** </pre></blockquote>)^ | |
| 1774 | 1787 | ** |
| 1775 | 1788 | ** ^The sqlite3_get_table() function evaluates one or more |
| 1776 | 1789 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 1777 | 1790 | ** string of its 2nd parameter and returns a result table to the |
| 1778 | 1791 | ** pointer given in its 3rd parameter. |
| 1779 | 1792 | ** |
| 1780 | 1793 | ** After the application has finished with the result from sqlite3_get_table(), |
| 1781 | -** it should pass the result table pointer to sqlite3_free_table() in order to | |
| 1794 | +** it must pass the result table pointer to sqlite3_free_table() in order to | |
| 1782 | 1795 | ** release the memory that was malloced. Because of the way the |
| 1783 | 1796 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 1784 | 1797 | ** function must not try to call [sqlite3_free()] directly. Only |
| 1785 | 1798 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 1786 | 1799 | ** |
| 1787 | -** ^(The sqlite3_get_table() interface is implemented as a wrapper around | |
| 1800 | +** The sqlite3_get_table() interface is implemented as a wrapper around | |
| 1788 | 1801 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 1789 | 1802 | ** to any internal data structures of SQLite. It uses only the public |
| 1790 | 1803 | ** interface defined here. As a consequence, errors that occur in the |
| 1791 | 1804 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 1792 | 1805 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 1793 | -** [sqlite3_errmsg()].)^ | |
| 1806 | +** [sqlite3_errmsg()]. | |
| 1794 | 1807 | */ |
| 1795 | 1808 | SQLITE_API int sqlite3_get_table( |
| 1796 | 1809 | sqlite3 *db, /* An open database */ |
| 1797 | 1810 | const char *zSql, /* SQL to be evaluated */ |
| 1798 | 1811 | char ***pazResult, /* Results of the query */ |
| @@ -1940,11 +1953,13 @@ | ||
| 1940 | 1953 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 1941 | 1954 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 1942 | 1955 | ** is not freed. |
| 1943 | 1956 | ** |
| 1944 | 1957 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 1945 | -** is always aligned to at least an 8 byte boundary. | |
| 1958 | +** is always aligned to at least an 8 byte boundary, or to a | |
| 1959 | +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time | |
| 1960 | +** option is used. | |
| 1946 | 1961 | ** |
| 1947 | 1962 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 1948 | 1963 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 1949 | 1964 | ** implementation of these routines to be omitted. That capability |
| 1950 | 1965 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2198,21 +2213,32 @@ | ||
| 2198 | 2213 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2199 | 2214 | |
| 2200 | 2215 | /* |
| 2201 | 2216 | ** CAPI3REF: Query Progress Callbacks |
| 2202 | 2217 | ** |
| 2203 | -** ^This routine configures a callback function - the | |
| 2204 | -** progress callback - that is invoked periodically during long | |
| 2205 | -** running calls to [sqlite3_exec()], [sqlite3_step()] and | |
| 2206 | -** [sqlite3_get_table()]. An example use for this | |
| 2218 | +** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback | |
| 2219 | +** function X to be invoked periodically during long running calls to | |
| 2220 | +** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for | |
| 2221 | +** database connection D. An example use for this | |
| 2207 | 2222 | ** interface is to keep a GUI updated during a large query. |
| 2223 | +** | |
| 2224 | +** ^The parameter P is passed through as the only parameter to the | |
| 2225 | +** callback function X. ^The parameter N is the number of | |
| 2226 | +** [virtual machine instructions] that are evaluated between successive | |
| 2227 | +** invocations of the callback X. | |
| 2228 | +** | |
| 2229 | +** ^Only a single progress handler may be defined at one time per | |
| 2230 | +** [database connection]; setting a new progress handler cancels the | |
| 2231 | +** old one. ^Setting parameter X to NULL disables the progress handler. | |
| 2232 | +** ^The progress handler is also disabled by setting N to a value less | |
| 2233 | +** than 1. | |
| 2208 | 2234 | ** |
| 2209 | 2235 | ** ^If the progress callback returns non-zero, the operation is |
| 2210 | 2236 | ** interrupted. This feature can be used to implement a |
| 2211 | 2237 | ** "Cancel" button on a GUI progress dialog box. |
| 2212 | 2238 | ** |
| 2213 | -** The progress handler must not do anything that will modify | |
| 2239 | +** The progress handler callback must not do anything that will modify | |
| 2214 | 2240 | ** the database connection that invoked the progress handler. |
| 2215 | 2241 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2216 | 2242 | ** database connections for the meaning of "modify" in this paragraph. |
| 2217 | 2243 | ** |
| 2218 | 2244 | */ |
| @@ -2267,11 +2293,11 @@ | ||
| 2267 | 2293 | ** </dl> |
| 2268 | 2294 | ** |
| 2269 | 2295 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2270 | 2296 | ** combinations shown above or one of the combinations shown above combined |
| 2271 | 2297 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2272 | -** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags, | |
| 2298 | +** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags, | |
| 2273 | 2299 | ** then the behavior is undefined. |
| 2274 | 2300 | ** |
| 2275 | 2301 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2276 | 2302 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2277 | 2303 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2392,21 +2418,26 @@ | ||
| 2392 | 2418 | ** ^(This interface allows the size of various constructs to be limited |
| 2393 | 2419 | ** on a connection by connection basis. The first parameter is the |
| 2394 | 2420 | ** [database connection] whose limit is to be set or queried. The |
| 2395 | 2421 | ** second parameter is one of the [limit categories] that define a |
| 2396 | 2422 | ** class of constructs to be size limited. The third parameter is the |
| 2397 | -** new limit for that construct. The function returns the old limit.)^ | |
| 2423 | +** new limit for that construct.)^ | |
| 2398 | 2424 | ** |
| 2399 | 2425 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2400 | -** ^(For the limit category of SQLITE_LIMIT_XYZ there is a | |
| 2426 | +** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a | |
| 2401 | 2427 | ** [limits | hard upper bound] |
| 2402 | -** set by a compile-time C preprocessor macro named | |
| 2403 | -** [limits | SQLITE_MAX_XYZ]. | |
| 2428 | +** set at compile-time by a C preprocessor macro called | |
| 2429 | +** [limits | SQLITE_MAX_<i>NAME</i>]. | |
| 2404 | 2430 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2405 | 2431 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2406 | 2432 | ** silently truncated to the hard upper bound. |
| 2407 | 2433 | ** |
| 2434 | +** ^Regardless of whether or not the limit was changed, the | |
| 2435 | +** [sqlite3_limit()] interface returns the prior value of the limit. | |
| 2436 | +** ^Hence, to find the current value of a limit without changing it, | |
| 2437 | +** simply invoke this interface with the third parameter set to -1. | |
| 2438 | +** | |
| 2408 | 2439 | ** Run-time limits are intended for use in applications that manage |
| 2409 | 2440 | ** both their own internal database and also databases that are controlled |
| 2410 | 2441 | ** by untrusted external sources. An example application might be a |
| 2411 | 2442 | ** web browser that has its own databases for storing history and |
| 2412 | 2443 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2431,11 +2462,11 @@ | ||
| 2431 | 2462 | ** The synopsis of the meanings of the various limits is shown below. |
| 2432 | 2463 | ** Additional information is available at [limits | Limits in SQLite]. |
| 2433 | 2464 | ** |
| 2434 | 2465 | ** <dl> |
| 2435 | 2466 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 2436 | -** <dd>The maximum size of any string or BLOB or table row.<dd>)^ | |
| 2467 | +** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^ | |
| 2437 | 2468 | ** |
| 2438 | 2469 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 2439 | 2470 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 2440 | 2471 | ** |
| 2441 | 2472 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2449,11 +2480,13 @@ | ||
| 2449 | 2480 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 2450 | 2481 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 2451 | 2482 | ** |
| 2452 | 2483 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 2453 | 2484 | ** <dd>The maximum number of instructions in a virtual machine program |
| 2454 | -** used to implement an SQL statement.</dd>)^ | |
| 2485 | +** used to implement an SQL statement. This limit is not currently | |
| 2486 | +** enforced, though that might be added in some future release of | |
| 2487 | +** SQLite.</dd>)^ | |
| 2455 | 2488 | ** |
| 2456 | 2489 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 2457 | 2490 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 2458 | 2491 | ** |
| 2459 | 2492 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2462,12 +2495,11 @@ | ||
| 2462 | 2495 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 2463 | 2496 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 2464 | 2497 | ** [GLOB] operators.</dd>)^ |
| 2465 | 2498 | ** |
| 2466 | 2499 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 2467 | -** <dd>The maximum number of variables in an SQL statement that can | |
| 2468 | -** be bound.</dd>)^ | |
| 2500 | +** <dd>The maximum index number of any [parameter] in an SQL statement.)^ | |
| 2469 | 2501 | ** |
| 2470 | 2502 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 2471 | 2503 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 2472 | 2504 | ** </dl> |
| 2473 | 2505 | */ |
| @@ -2535,16 +2567,11 @@ | ||
| 2535 | 2567 | ** |
| 2536 | 2568 | ** <ol> |
| 2537 | 2569 | ** <li> |
| 2538 | 2570 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 2539 | 2571 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 2540 | -** statement and try to run it again. ^If the schema has changed in | |
| 2541 | -** a way that makes the statement no longer valid, [sqlite3_step()] will still | |
| 2542 | -** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is | |
| 2543 | -** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the | |
| 2544 | -** error go away. Note: use [sqlite3_errmsg()] to find the text | |
| 2545 | -** of the parsing error that results in an [SQLITE_SCHEMA] return. | |
| 2572 | +** statement and try to run it again. | |
| 2546 | 2573 | ** </li> |
| 2547 | 2574 | ** |
| 2548 | 2575 | ** <li> |
| 2549 | 2576 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 2550 | 2577 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -2553,15 +2580,20 @@ | ||
| 2553 | 2580 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 2554 | 2581 | ** interfaces, the underlying reason for the error is returned immediately. |
| 2555 | 2582 | ** </li> |
| 2556 | 2583 | ** |
| 2557 | 2584 | ** <li> |
| 2558 | -** ^If the value of a [parameter | host parameter] in the WHERE clause might | |
| 2559 | -** change the query plan for a statement, then the statement may be | |
| 2560 | -** automatically recompiled (as if there had been a schema change) on the first | |
| 2561 | -** [sqlite3_step()] call following any change to the | |
| 2562 | -** [sqlite3_bind_text | bindings] of the [parameter]. | |
| 2585 | +** ^If the specific value bound to [parameter | host parameter] in the | |
| 2586 | +** WHERE clause might influence the choice of query plan for a statement, | |
| 2587 | +** then the statement will be automatically recompiled, as if there had been | |
| 2588 | +** a schema change, on the first [sqlite3_step()] call following any change | |
| 2589 | +** to the [sqlite3_bind_text | bindings] of that [parameter]. | |
| 2590 | +** ^The specific value of WHERE-clause [parameter] might influence the | |
| 2591 | +** choice of query plan if the parameter is the left-hand side of a [LIKE] | |
| 2592 | +** or [GLOB] operator or if the parameter is compared to an indexed column | |
| 2593 | +** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. | |
| 2594 | +** the | |
| 2563 | 2595 | ** </li> |
| 2564 | 2596 | ** </ol> |
| 2565 | 2597 | */ |
| 2566 | 2598 | SQLITE_API int sqlite3_prepare( |
| 2567 | 2599 | sqlite3 *db, /* Database handle */ |
| @@ -2624,11 +2656,11 @@ | ||
| 2624 | 2656 | ** or if SQLite is run in one of reduced mutex modes |
| 2625 | 2657 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 2626 | 2658 | ** then there is no distinction between protected and unprotected |
| 2627 | 2659 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 2628 | 2660 | ** for maximum code portability it is recommended that applications |
| 2629 | -** still make the distinction between between protected and unprotected | |
| 2661 | +** still make the distinction between protected and unprotected | |
| 2630 | 2662 | ** sqlite3_value objects even when not strictly required. |
| 2631 | 2663 | ** |
| 2632 | 2664 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 2633 | 2665 | ** implementation of [application-defined SQL functions] are protected. |
| 2634 | 2666 | ** ^The sqlite3_value object returned by |
| @@ -2819,10 +2851,12 @@ | ||
| 2819 | 2851 | ** CAPI3REF: Number Of Columns In A Result Set |
| 2820 | 2852 | ** |
| 2821 | 2853 | ** ^Return the number of columns in the result set returned by the |
| 2822 | 2854 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 2823 | 2855 | ** statement that does not return data (for example an [UPDATE]). |
| 2856 | +** | |
| 2857 | +** See also: [sqlite3_data_count()] | |
| 2824 | 2858 | */ |
| 2825 | 2859 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 2826 | 2860 | |
| 2827 | 2861 | /* |
| 2828 | 2862 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3009,12 +3043,18 @@ | ||
| 3009 | 3043 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3010 | 3044 | |
| 3011 | 3045 | /* |
| 3012 | 3046 | ** CAPI3REF: Number of columns in a result set |
| 3013 | 3047 | ** |
| 3014 | -** ^The sqlite3_data_count(P) the number of columns in the | |
| 3015 | -** of the result set of [prepared statement] P. | |
| 3048 | +** ^The sqlite3_data_count(P) interface returns the number of columns in the | |
| 3049 | +** current row of the result set of [prepared statement] P. | |
| 3050 | +** ^If prepared statement P does not have results ready to return | |
| 3051 | +** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of | |
| 3052 | +** interfaces) then sqlite3_data_count(P) returns 0. | |
| 3053 | +** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. | |
| 3054 | +** | |
| 3055 | +** See also: [sqlite3_column_count()] | |
| 3016 | 3056 | */ |
| 3017 | 3057 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3018 | 3058 | |
| 3019 | 3059 | /* |
| 3020 | 3060 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3090,22 +3130,30 @@ | ||
| 3090 | 3130 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3091 | 3131 | ** the string to UTF-8 and then returns the number of bytes. |
| 3092 | 3132 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3093 | 3133 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3094 | 3134 | ** the number of bytes in that string. |
| 3095 | -** ^The value returned does not include the zero terminator at the end | |
| 3096 | -** of the string. ^For clarity: the value returned is the number of | |
| 3135 | +** ^If the result is NULL, then sqlite3_column_bytes() returns zero. | |
| 3136 | +** | |
| 3137 | +** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() | |
| 3138 | +** routine returns the number of bytes in that BLOB or string. | |
| 3139 | +** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts | |
| 3140 | +** the string to UTF-16 and then returns the number of bytes. | |
| 3141 | +** ^If the result is a numeric value then sqlite3_column_bytes16() uses | |
| 3142 | +** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns | |
| 3143 | +** the number of bytes in that string. | |
| 3144 | +** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. | |
| 3145 | +** | |
| 3146 | +** ^The values returned by [sqlite3_column_bytes()] and | |
| 3147 | +** [sqlite3_column_bytes16()] do not include the zero terminators at the end | |
| 3148 | +** of the string. ^For clarity: the values returned by | |
| 3149 | +** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of | |
| 3097 | 3150 | ** bytes in the string, not the number of characters. |
| 3098 | 3151 | ** |
| 3099 | 3152 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3100 | 3153 | ** even empty strings, are always zero terminated. ^The return |
| 3101 | -** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary | |
| 3102 | -** pointer, possibly even a NULL pointer. | |
| 3103 | -** | |
| 3104 | -** ^The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() | |
| 3105 | -** but leaves the result in UTF-16 in native byte order instead of UTF-8. | |
| 3106 | -** ^The zero terminator is not included in this count. | |
| 3154 | +** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. | |
| 3107 | 3155 | ** |
| 3108 | 3156 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3109 | 3157 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3110 | 3158 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3111 | 3159 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3146,14 +3194,14 @@ | ||
| 3146 | 3194 | ** and atof(). SQLite does not really use these functions. It has its |
| 3147 | 3195 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3148 | 3196 | ** used in the table for brevity and because they are familiar to most |
| 3149 | 3197 | ** C programmers. |
| 3150 | 3198 | ** |
| 3151 | -** ^Note that when type conversions occur, pointers returned by prior | |
| 3199 | +** Note that when type conversions occur, pointers returned by prior | |
| 3152 | 3200 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3153 | 3201 | ** sqlite3_column_text16() may be invalidated. |
| 3154 | -** ^(Type conversions and pointer invalidations might occur | |
| 3202 | +** Type conversions and pointer invalidations might occur | |
| 3155 | 3203 | ** in the following cases: |
| 3156 | 3204 | ** |
| 3157 | 3205 | ** <ul> |
| 3158 | 3206 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3159 | 3207 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3162,26 +3210,26 @@ | ||
| 3162 | 3210 | ** sqlite3_column_text16() is called. The content must be converted |
| 3163 | 3211 | ** to UTF-16.</li> |
| 3164 | 3212 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3165 | 3213 | ** sqlite3_column_text() is called. The content must be converted |
| 3166 | 3214 | ** to UTF-8.</li> |
| 3167 | -** </ul>)^ | |
| 3215 | +** </ul> | |
| 3168 | 3216 | ** |
| 3169 | 3217 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3170 | 3218 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3171 | -** that the prior pointer points to will have been modified. Other kinds | |
| 3219 | +** that the prior pointer references will have been modified. Other kinds | |
| 3172 | 3220 | ** of conversion are done in place when it is possible, but sometimes they |
| 3173 | 3221 | ** are not possible and in those cases prior pointers are invalidated. |
| 3174 | 3222 | ** |
| 3175 | -** ^(The safest and easiest to remember policy is to invoke these routines | |
| 3223 | +** The safest and easiest to remember policy is to invoke these routines | |
| 3176 | 3224 | ** in one of the following ways: |
| 3177 | 3225 | ** |
| 3178 | 3226 | ** <ul> |
| 3179 | 3227 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3180 | 3228 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3181 | 3229 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3182 | -** </ul>)^ | |
| 3230 | +** </ul> | |
| 3183 | 3231 | ** |
| 3184 | 3232 | ** In other words, you should call sqlite3_column_text(), |
| 3185 | 3233 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3186 | 3234 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3187 | 3235 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3215,21 +3263,30 @@ | ||
| 3215 | 3263 | |
| 3216 | 3264 | /* |
| 3217 | 3265 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3218 | 3266 | ** |
| 3219 | 3267 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3220 | -** ^If the statement was executed successfully or not executed at all, then | |
| 3221 | -** SQLITE_OK is returned. ^If execution of the statement failed then an | |
| 3222 | -** [error code] or [extended error code] is returned. | |
| 3268 | +** ^If the most recent evaluation of the statement encountered no errors or | |
| 3269 | +** or if the statement is never been evaluated, then sqlite3_finalize() returns | |
| 3270 | +** SQLITE_OK. ^If the most recent evaluation of statement S failed, then | |
| 3271 | +** sqlite3_finalize(S) returns the appropriate [error code] or | |
| 3272 | +** [extended error code]. | |
| 3223 | 3273 | ** |
| 3224 | -** ^This routine can be called at any point during the execution of the | |
| 3225 | -** [prepared statement]. ^If the virtual machine has not | |
| 3226 | -** completed execution when this routine is called, that is like | |
| 3227 | -** encountering an error or an [sqlite3_interrupt | interrupt]. | |
| 3228 | -** ^Incomplete updates may be rolled back and transactions canceled, | |
| 3229 | -** depending on the circumstances, and the | |
| 3230 | -** [error code] returned will be [SQLITE_ABORT]. | |
| 3274 | +** ^The sqlite3_finalize(S) routine can be called at any point during | |
| 3275 | +** the life cycle of [prepared statement] S: | |
| 3276 | +** before statement S is ever evaluated, after | |
| 3277 | +** one or more calls to [sqlite3_reset()], or after any call | |
| 3278 | +** to [sqlite3_step()] regardless of whether or not the statement has | |
| 3279 | +** completed execution. | |
| 3280 | +** | |
| 3281 | +** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. | |
| 3282 | +** | |
| 3283 | +** The application must finalize every [prepared statement] in order to avoid | |
| 3284 | +** resource leaks. It is a grievous error for the application to try to use | |
| 3285 | +** a prepared statement after it has been finalized. Any use of a prepared | |
| 3286 | +** statement after it has been finalized can result in undefined and | |
| 3287 | +** undesirable behavior such as segfaults and heap corruption. | |
| 3231 | 3288 | */ |
| 3232 | 3289 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3233 | 3290 | |
| 3234 | 3291 | /* |
| 3235 | 3292 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3261,40 +3318,42 @@ | ||
| 3261 | 3318 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3262 | 3319 | ** KEYWORDS: {function creation routines} |
| 3263 | 3320 | ** KEYWORDS: {application-defined SQL function} |
| 3264 | 3321 | ** KEYWORDS: {application-defined SQL functions} |
| 3265 | 3322 | ** |
| 3266 | -** ^These two functions (collectively known as "function creation routines") | |
| 3323 | +** ^These functions (collectively known as "function creation routines") | |
| 3267 | 3324 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3268 | -** of existing SQL functions or aggregates. The only difference between the | |
| 3269 | -** two is that the second parameter, the name of the (scalar) function or | |
| 3270 | -** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16 | |
| 3271 | -** for sqlite3_create_function16(). | |
| 3325 | +** of existing SQL functions or aggregates. The only differences between | |
| 3326 | +** these routines are the text encoding expected for | |
| 3327 | +** the the second parameter (the name of the function being created) | |
| 3328 | +** and the presence or absence of a destructor callback for | |
| 3329 | +** the application data pointer. | |
| 3272 | 3330 | ** |
| 3273 | 3331 | ** ^The first parameter is the [database connection] to which the SQL |
| 3274 | 3332 | ** function is to be added. ^If an application uses more than one database |
| 3275 | 3333 | ** connection then application-defined SQL functions must be added |
| 3276 | 3334 | ** to each database connection separately. |
| 3277 | 3335 | ** |
| 3278 | -** The second parameter is the name of the SQL function to be created or | |
| 3279 | -** redefined. ^The length of the name is limited to 255 bytes, exclusive of | |
| 3280 | -** the zero-terminator. Note that the name length limit is in bytes, not | |
| 3281 | -** characters. ^Any attempt to create a function with a longer name | |
| 3282 | -** will result in [SQLITE_ERROR] being returned. | |
| 3336 | +** ^The second parameter is the name of the SQL function to be created or | |
| 3337 | +** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 | |
| 3338 | +** representation, exclusive of the zero-terminator. ^Note that the name | |
| 3339 | +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. | |
| 3340 | +** ^Any attempt to create a function with a longer name | |
| 3341 | +** will result in [SQLITE_MISUSE] being returned. | |
| 3283 | 3342 | ** |
| 3284 | 3343 | ** ^The third parameter (nArg) |
| 3285 | 3344 | ** is the number of arguments that the SQL function or |
| 3286 | 3345 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3287 | 3346 | ** aggregate may take any number of arguments between 0 and the limit |
| 3288 | 3347 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3289 | 3348 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3290 | 3349 | ** undefined. |
| 3291 | 3350 | ** |
| 3292 | -** The fourth parameter, eTextRep, specifies what | |
| 3351 | +** ^The fourth parameter, eTextRep, specifies what | |
| 3293 | 3352 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3294 | -** its parameters. Any SQL function implementation should be able to work | |
| 3295 | -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be | |
| 3353 | +** its parameters. Every SQL function implementation must be able to work | |
| 3354 | +** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be | |
| 3296 | 3355 | ** more efficient with one encoding than another. ^An application may |
| 3297 | 3356 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3298 | 3357 | ** times with the same function but with different values of eTextRep. |
| 3299 | 3358 | ** ^When multiple implementations of the same function are available, SQLite |
| 3300 | 3359 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3302,17 +3361,25 @@ | ||
| 3302 | 3361 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3303 | 3362 | ** |
| 3304 | 3363 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3305 | 3364 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3306 | 3365 | ** |
| 3307 | -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are | |
| 3366 | +** ^The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are | |
| 3308 | 3367 | ** pointers to C-language functions that implement the SQL function or |
| 3309 | 3368 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3310 | -** callback only; NULL pointers should be passed as the xStep and xFinal | |
| 3369 | +** callback only; NULL pointers must be passed as the xStep and xFinal | |
| 3311 | 3370 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3312 | -** and xFinal and NULL should be passed for xFunc. ^To delete an existing | |
| 3313 | -** SQL function or aggregate, pass NULL for all three function callbacks. | |
| 3371 | +** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing | |
| 3372 | +** SQL function or aggregate, pass NULL poiners for all three function | |
| 3373 | +** callbacks. | |
| 3374 | +** | |
| 3375 | +** ^If the tenth parameter to sqlite3_create_function_v2() is not NULL, | |
| 3376 | +** then it is invoked when the function is deleted, either by being | |
| 3377 | +** overloaded or when the database connection closes. | |
| 3378 | +** ^When the destructure callback of the tenth parameter is invoked, it | |
| 3379 | +** is passed a single argument which is a copy of the pointer which was | |
| 3380 | +** the fifth parameter to sqlite3_create_function_v2(). | |
| 3314 | 3381 | ** |
| 3315 | 3382 | ** ^It is permitted to register multiple implementations of the same |
| 3316 | 3383 | ** functions with the same name but with either differing numbers of |
| 3317 | 3384 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3318 | 3385 | ** the implementation that most closely matches the way in which the |
| @@ -3324,15 +3391,10 @@ | ||
| 3324 | 3391 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3325 | 3392 | ** is a closer match than a function where the encoding difference is |
| 3326 | 3393 | ** between UTF8 and UTF16. |
| 3327 | 3394 | ** |
| 3328 | 3395 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3329 | -** ^The first application-defined function with a given name overrides all | |
| 3330 | -** built-in functions in the same [database connection] with the same name. | |
| 3331 | -** ^Subsequent application-defined functions of the same name only override | |
| 3332 | -** prior application-defined functions that are an exact match for the | |
| 3333 | -** number of parameters and preferred encoding. | |
| 3334 | 3396 | ** |
| 3335 | 3397 | ** ^An application-defined function is permitted to call other |
| 3336 | 3398 | ** SQLite interfaces. However, such calls must not |
| 3337 | 3399 | ** close the database connection nor finalize or reset the prepared |
| 3338 | 3400 | ** statement in which the function is running. |
| @@ -3354,10 +3416,21 @@ | ||
| 3354 | 3416 | int eTextRep, |
| 3355 | 3417 | void *pApp, |
| 3356 | 3418 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3357 | 3419 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3358 | 3420 | void (*xFinal)(sqlite3_context*) |
| 3421 | +); | |
| 3422 | +SQLITE_API int sqlite3_create_function_v2( | |
| 3423 | + sqlite3 *db, | |
| 3424 | + const char *zFunctionName, | |
| 3425 | + int nArg, | |
| 3426 | + int eTextRep, | |
| 3427 | + void *pApp, | |
| 3428 | + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), | |
| 3429 | + void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |
| 3430 | + void (*xFinal)(sqlite3_context*), | |
| 3431 | + void(*xDestroy)(void*) | |
| 3359 | 3432 | ); |
| 3360 | 3433 | |
| 3361 | 3434 | /* |
| 3362 | 3435 | ** CAPI3REF: Text Encodings |
| 3363 | 3436 | ** |
| @@ -3701,73 +3774,97 @@ | ||
| 3701 | 3774 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 3702 | 3775 | |
| 3703 | 3776 | /* |
| 3704 | 3777 | ** CAPI3REF: Define New Collating Sequences |
| 3705 | 3778 | ** |
| 3706 | -** These functions are used to add new collation sequences to the | |
| 3707 | -** [database connection] specified as the first argument. | |
| 3779 | +** ^These functions add, remove, or modify a [collation] associated | |
| 3780 | +** with the [database connection] specified as the first argument. | |
| 3708 | 3781 | ** |
| 3709 | -** ^The name of the new collation sequence is specified as a UTF-8 string | |
| 3782 | +** ^The name of the collation is a UTF-8 string | |
| 3710 | 3783 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 3711 | -** and a UTF-16 string for sqlite3_create_collation16(). ^In all cases | |
| 3712 | -** the name is passed as the second function argument. | |
| 3713 | -** | |
| 3714 | -** ^The third argument may be one of the constants [SQLITE_UTF8], | |
| 3715 | -** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied | |
| 3716 | -** routine expects to be passed pointers to strings encoded using UTF-8, | |
| 3717 | -** UTF-16 little-endian, or UTF-16 big-endian, respectively. ^The | |
| 3718 | -** third argument might also be [SQLITE_UTF16] to indicate that the routine | |
| 3719 | -** expects pointers to be UTF-16 strings in the native byte order, or the | |
| 3720 | -** argument can be [SQLITE_UTF16_ALIGNED] if the | |
| 3721 | -** the routine expects pointers to 16-bit word aligned strings | |
| 3722 | -** of UTF-16 in the native byte order. | |
| 3723 | -** | |
| 3724 | -** A pointer to the user supplied routine must be passed as the fifth | |
| 3725 | -** argument. ^If it is NULL, this is the same as deleting the collation | |
| 3726 | -** sequence (so that SQLite cannot call it any more). | |
| 3727 | -** ^Each time the application supplied function is invoked, it is passed | |
| 3728 | -** as its first parameter a copy of the void* passed as the fourth argument | |
| 3729 | -** to sqlite3_create_collation() or sqlite3_create_collation16(). | |
| 3730 | -** | |
| 3731 | -** ^The remaining arguments to the application-supplied routine are two strings, | |
| 3732 | -** each represented by a (length, data) pair and encoded in the encoding | |
| 3733 | -** that was passed as the third argument when the collation sequence was | |
| 3734 | -** registered. The application defined collation routine should | |
| 3735 | -** return negative, zero or positive if the first string is less than, | |
| 3736 | -** equal to, or greater than the second string. i.e. (STRING1 - STRING2). | |
| 3784 | +** and a UTF-16 string in native byte order for sqlite3_create_collation16(). | |
| 3785 | +** ^Collation names that compare equal according to [sqlite3_strnicmp()] are | |
| 3786 | +** considered to be the same name. | |
| 3787 | +** | |
| 3788 | +** ^(The third argument (eTextRep) must be one of the constants: | |
| 3789 | +** <ul> | |
| 3790 | +** <li> [SQLITE_UTF8], | |
| 3791 | +** <li> [SQLITE_UTF16LE], | |
| 3792 | +** <li> [SQLITE_UTF16BE], | |
| 3793 | +** <li> [SQLITE_UTF16], or | |
| 3794 | +** <li> [SQLITE_UTF16_ALIGNED]. | |
| 3795 | +** </ul>)^ | |
| 3796 | +** ^The eTextRep argument determines the encoding of strings passed | |
| 3797 | +** to the collating function callback, xCallback. | |
| 3798 | +** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep | |
| 3799 | +** force strings to be UTF16 with native byte order. | |
| 3800 | +** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin | |
| 3801 | +** on an even byte address. | |
| 3802 | +** | |
| 3803 | +** ^The fourth argument, pArg, is a application data pointer that is passed | |
| 3804 | +** through as the first argument to the collating function callback. | |
| 3805 | +** | |
| 3806 | +** ^The fifth argument, xCallback, is a pointer to the collating function. | |
| 3807 | +** ^Multiple collating functions can be registered using the same name but | |
| 3808 | +** with different eTextRep parameters and SQLite will use whichever | |
| 3809 | +** function requires the least amount of data transformation. | |
| 3810 | +** ^If the xCallback argument is NULL then the collating function is | |
| 3811 | +** deleted. ^When all collating functions having the same name are deleted, | |
| 3812 | +** that collation is no longer usable. | |
| 3813 | +** | |
| 3814 | +** ^The collating function callback is invoked with a copy of the pArg | |
| 3815 | +** application data pointer and with two strings in the encoding specified | |
| 3816 | +** by the eTextRep argument. The collating function must return an | |
| 3817 | +** integer that is negative, zero, or positive | |
| 3818 | +** if the first string is less than, equal to, or greater than the second, | |
| 3819 | +** respectively. A collating function must alway return the same answer | |
| 3820 | +** given the same inputs. If two or more collating functions are registered | |
| 3821 | +** to the same collation name (using different eTextRep values) then all | |
| 3822 | +** must give an equivalent answer when invoked with equivalent strings. | |
| 3823 | +** The collating function must obey the following properties for all | |
| 3824 | +** strings A, B, and C: | |
| 3825 | +** | |
| 3826 | +** <ol> | |
| 3827 | +** <li> If A==B then B==A. | |
| 3828 | +** <li> If A==B and B==C then A==C. | |
| 3829 | +** <li> If A<B THEN B>A. | |
| 3830 | +** <li> If A<B and B<C then A<C. | |
| 3831 | +** </ol> | |
| 3832 | +** | |
| 3833 | +** If a collating function fails any of the above constraints and that | |
| 3834 | +** collating function is registered and used, then the behavior of SQLite | |
| 3835 | +** is undefined. | |
| 3737 | 3836 | ** |
| 3738 | 3837 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 3739 | -** except that it takes an extra argument which is a destructor for | |
| 3740 | -** the collation. ^The destructor is called when the collation is | |
| 3741 | -** destroyed and is passed a copy of the fourth parameter void* pointer | |
| 3742 | -** of the sqlite3_create_collation_v2(). | |
| 3743 | -** ^Collations are destroyed when they are overridden by later calls to the | |
| 3744 | -** collation creation functions or when the [database connection] is closed | |
| 3745 | -** using [sqlite3_close()]. | |
| 3838 | +** with the addition that the xDestroy callback is invoked on pArg when | |
| 3839 | +** the collating function is deleted. | |
| 3840 | +** ^Collating functions are deleted when they are overridden by later | |
| 3841 | +** calls to the collation creation functions or when the | |
| 3842 | +** [database connection] is closed using [sqlite3_close()]. | |
| 3746 | 3843 | ** |
| 3747 | 3844 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 3748 | 3845 | */ |
| 3749 | 3846 | SQLITE_API int sqlite3_create_collation( |
| 3750 | 3847 | sqlite3*, |
| 3751 | 3848 | const char *zName, |
| 3752 | 3849 | int eTextRep, |
| 3753 | - void*, | |
| 3850 | + void *pArg, | |
| 3754 | 3851 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3755 | 3852 | ); |
| 3756 | 3853 | SQLITE_API int sqlite3_create_collation_v2( |
| 3757 | 3854 | sqlite3*, |
| 3758 | 3855 | const char *zName, |
| 3759 | 3856 | int eTextRep, |
| 3760 | - void*, | |
| 3857 | + void *pArg, | |
| 3761 | 3858 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 3762 | 3859 | void(*xDestroy)(void*) |
| 3763 | 3860 | ); |
| 3764 | 3861 | SQLITE_API int sqlite3_create_collation16( |
| 3765 | 3862 | sqlite3*, |
| 3766 | 3863 | const void *zName, |
| 3767 | 3864 | int eTextRep, |
| 3768 | - void*, | |
| 3865 | + void *pArg, | |
| 3769 | 3866 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3770 | 3867 | ); |
| 3771 | 3868 | |
| 3772 | 3869 | /* |
| 3773 | 3870 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -3852,20 +3949,23 @@ | ||
| 3852 | 3949 | #endif |
| 3853 | 3950 | |
| 3854 | 3951 | /* |
| 3855 | 3952 | ** CAPI3REF: Suspend Execution For A Short Time |
| 3856 | 3953 | ** |
| 3857 | -** ^The sqlite3_sleep() function causes the current thread to suspend execution | |
| 3954 | +** The sqlite3_sleep() function causes the current thread to suspend execution | |
| 3858 | 3955 | ** for at least a number of milliseconds specified in its parameter. |
| 3859 | 3956 | ** |
| 3860 | -** ^If the operating system does not support sleep requests with | |
| 3957 | +** If the operating system does not support sleep requests with | |
| 3861 | 3958 | ** millisecond time resolution, then the time will be rounded up to |
| 3862 | -** the nearest second. ^The number of milliseconds of sleep actually | |
| 3959 | +** the nearest second. The number of milliseconds of sleep actually | |
| 3863 | 3960 | ** requested from the operating system is returned. |
| 3864 | 3961 | ** |
| 3865 | 3962 | ** ^SQLite implements this interface by calling the xSleep() |
| 3866 | -** method of the default [sqlite3_vfs] object. | |
| 3963 | +** method of the default [sqlite3_vfs] object. If the xSleep() method | |
| 3964 | +** of the default VFS is not implemented correctly, or not implemented at | |
| 3965 | +** all, then the behavior of sqlite3_sleep() may deviate from the description | |
| 3966 | +** in the previous paragraphs. | |
| 3867 | 3967 | */ |
| 3868 | 3968 | SQLITE_API int sqlite3_sleep(int); |
| 3869 | 3969 | |
| 3870 | 3970 | /* |
| 3871 | 3971 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4083,44 +4183,77 @@ | ||
| 4083 | 4183 | ** of heap memory by deallocating non-essential memory allocations |
| 4084 | 4184 | ** held by the database library. Memory used to cache database |
| 4085 | 4185 | ** pages to improve performance is an example of non-essential memory. |
| 4086 | 4186 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4087 | 4187 | ** which might be more or less than the amount requested. |
| 4188 | +** ^The sqlite3_release_memory() routine is a no-op returning zero | |
| 4189 | +** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. | |
| 4088 | 4190 | */ |
| 4089 | 4191 | SQLITE_API int sqlite3_release_memory(int); |
| 4090 | 4192 | |
| 4091 | 4193 | /* |
| 4092 | 4194 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4093 | 4195 | ** |
| 4094 | -** ^The sqlite3_soft_heap_limit() interface places a "soft" limit | |
| 4095 | -** on the amount of heap memory that may be allocated by SQLite. | |
| 4096 | -** ^If an internal allocation is requested that would exceed the | |
| 4097 | -** soft heap limit, [sqlite3_release_memory()] is invoked one or | |
| 4098 | -** more times to free up some space before the allocation is performed. | |
| 4099 | -** | |
| 4100 | -** ^The limit is called "soft" because if [sqlite3_release_memory()] | |
| 4101 | -** cannot free sufficient memory to prevent the limit from being exceeded, | |
| 4102 | -** the memory is allocated anyway and the current operation proceeds. | |
| 4103 | -** | |
| 4104 | -** ^A negative or zero value for N means that there is no soft heap limit and | |
| 4105 | -** [sqlite3_release_memory()] will only be called when memory is exhausted. | |
| 4106 | -** ^The default value for the soft heap limit is zero. | |
| 4107 | -** | |
| 4108 | -** ^(SQLite makes a best effort to honor the soft heap limit. | |
| 4109 | -** But if the soft heap limit cannot be honored, execution will | |
| 4110 | -** continue without error or notification.)^ This is why the limit is | |
| 4111 | -** called a "soft" limit. It is advisory only. | |
| 4112 | -** | |
| 4113 | -** Prior to SQLite version 3.5.0, this routine only constrained the memory | |
| 4114 | -** allocated by a single thread - the same thread in which this routine | |
| 4115 | -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is | |
| 4116 | -** applied to all threads. The value specified for the soft heap limit | |
| 4117 | -** is an upper bound on the total memory allocation for all threads. In | |
| 4118 | -** version 3.5.0 there is no mechanism for limiting the heap usage for | |
| 4119 | -** individual threads. | |
| 4196 | +** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the | |
| 4197 | +** soft limit on the amount of heap memory that may be allocated by SQLite. | |
| 4198 | +** ^SQLite strives to keep heap memory utilization below the soft heap | |
| 4199 | +** limit by reducing the number of pages held in the page cache | |
| 4200 | +** as heap memory usages approaches the limit. | |
| 4201 | +** ^The soft heap limit is "soft" because even though SQLite strives to stay | |
| 4202 | +** below the limit, it will exceed the limit rather than generate | |
| 4203 | +** an [SQLITE_NOMEM] error. In other words, the soft heap limit | |
| 4204 | +** is advisory only. | |
| 4205 | +** | |
| 4206 | +** ^The return value from sqlite3_soft_heap_limit64() is the size of | |
| 4207 | +** the soft heap limit prior to the call. ^If the argument N is negative | |
| 4208 | +** then no change is made to the soft heap limit. Hence, the current | |
| 4209 | +** size of the soft heap limit can be determined by invoking | |
| 4210 | +** sqlite3_soft_heap_limit64() with a negative argument. | |
| 4211 | +** | |
| 4212 | +** ^If the argument N is zero then the soft heap limit is disabled. | |
| 4213 | +** | |
| 4214 | +** ^(The soft heap limit is not enforced in the current implementation | |
| 4215 | +** if one or more of following conditions are true: | |
| 4216 | +** | |
| 4217 | +** <ul> | |
| 4218 | +** <li> The soft heap limit is set to zero. | |
| 4219 | +** <li> Memory accounting is disabled using a combination of the | |
| 4220 | +** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and | |
| 4221 | +** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. | |
| 4222 | +** <li> An alternative page cache implementation is specifed using | |
| 4223 | +** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...). | |
| 4224 | +** <li> The page cache allocates from its own memory pool supplied | |
| 4225 | +** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than | |
| 4226 | +** from the heap. | |
| 4227 | +** </ul>)^ | |
| 4228 | +** | |
| 4229 | +** Beginning with SQLite version 3.7.3, the soft heap limit is enforced | |
| 4230 | +** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] | |
| 4231 | +** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], | |
| 4232 | +** the soft heap limit is enforced on every memory allocation. Without | |
| 4233 | +** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced | |
| 4234 | +** when memory is allocated by the page cache. Testing suggests that because | |
| 4235 | +** the page cache is the predominate memory user in SQLite, most | |
| 4236 | +** applications will achieve adequate soft heap limit enforcement without | |
| 4237 | +** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. | |
| 4238 | +** | |
| 4239 | +** The circumstances under which SQLite will enforce the soft heap limit may | |
| 4240 | +** changes in future releases of SQLite. | |
| 4241 | +*/ | |
| 4242 | +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); | |
| 4243 | + | |
| 4244 | +/* | |
| 4245 | +** CAPI3REF: Deprecated Soft Heap Limit Interface | |
| 4246 | +** DEPRECATED | |
| 4247 | +** | |
| 4248 | +** This is a deprecated version of the [sqlite3_soft_heap_limit64()] | |
| 4249 | +** interface. This routine is provided for historical compatibility | |
| 4250 | +** only. All new applications should use the | |
| 4251 | +** [sqlite3_soft_heap_limit64()] interface rather than this one. | |
| 4120 | 4252 | */ |
| 4121 | -SQLITE_API void sqlite3_soft_heap_limit(int); | |
| 4253 | +SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); | |
| 4254 | + | |
| 4122 | 4255 | |
| 4123 | 4256 | /* |
| 4124 | 4257 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4125 | 4258 | ** |
| 4126 | 4259 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4240,38 +4373,51 @@ | ||
| 4240 | 4373 | ** it back off again. |
| 4241 | 4374 | */ |
| 4242 | 4375 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4243 | 4376 | |
| 4244 | 4377 | /* |
| 4245 | -** CAPI3REF: Automatically Load An Extensions | |
| 4246 | -** | |
| 4247 | -** ^This API can be invoked at program startup in order to register | |
| 4248 | -** one or more statically linked extensions that will be available | |
| 4249 | -** to all new [database connections]. | |
| 4250 | -** | |
| 4251 | -** ^(This routine stores a pointer to the extension entry point | |
| 4252 | -** in an array that is obtained from [sqlite3_malloc()]. That memory | |
| 4253 | -** is deallocated by [sqlite3_reset_auto_extension()].)^ | |
| 4254 | -** | |
| 4255 | -** ^This function registers an extension entry point that is | |
| 4256 | -** automatically invoked whenever a new [database connection] | |
| 4257 | -** is opened using [sqlite3_open()], [sqlite3_open16()], | |
| 4258 | -** or [sqlite3_open_v2()]. | |
| 4259 | -** ^Duplicate extensions are detected so calling this routine | |
| 4260 | -** multiple times with the same extension is harmless. | |
| 4261 | -** ^Automatic extensions apply across all threads. | |
| 4378 | +** CAPI3REF: Automatically Load Statically Linked Extensions | |
| 4379 | +** | |
| 4380 | +** ^This interface causes the xEntryPoint() function to be invoked for | |
| 4381 | +** each new [database connection] that is created. The idea here is that | |
| 4382 | +** xEntryPoint() is the entry point for a statically linked SQLite extension | |
| 4383 | +** that is to be automatically loaded into all new database connections. | |
| 4384 | +** | |
| 4385 | +** ^(Even though the function prototype shows that xEntryPoint() takes | |
| 4386 | +** no arguments and returns void, SQLite invokes xEntryPoint() with three | |
| 4387 | +** arguments and expects and integer result as if the signature of the | |
| 4388 | +** entry point where as follows: | |
| 4389 | +** | |
| 4390 | +** <blockquote><pre> | |
| 4391 | +** int xEntryPoint( | |
| 4392 | +** sqlite3 *db, | |
| 4393 | +** const char **pzErrMsg, | |
| 4394 | +** const struct sqlite3_api_routines *pThunk | |
| 4395 | +** ); | |
| 4396 | +** </pre></blockquote>)^ | |
| 4397 | +** | |
| 4398 | +** If the xEntryPoint routine encounters an error, it should make *pzErrMsg | |
| 4399 | +** point to an appropriate error message (obtained from [sqlite3_mprintf()]) | |
| 4400 | +** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg | |
| 4401 | +** is NULL before calling the xEntryPoint(). ^SQLite will invoke | |
| 4402 | +** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any | |
| 4403 | +** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], | |
| 4404 | +** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. | |
| 4405 | +** | |
| 4406 | +** ^Calling sqlite3_auto_extension(X) with an entry point X that is already | |
| 4407 | +** on the list of automatic extensions is a harmless no-op. ^No entry point | |
| 4408 | +** will be called more than once for each database connection that is opened. | |
| 4409 | +** | |
| 4410 | +** See also: [sqlite3_reset_auto_extension()]. | |
| 4262 | 4411 | */ |
| 4263 | 4412 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4264 | 4413 | |
| 4265 | 4414 | /* |
| 4266 | 4415 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4267 | 4416 | ** |
| 4268 | -** ^(This function disables all previously registered automatic | |
| 4269 | -** extensions. It undoes the effect of all prior | |
| 4270 | -** [sqlite3_auto_extension()] calls.)^ | |
| 4271 | -** | |
| 4272 | -** ^This function disables automatic extensions in all threads. | |
| 4417 | +** ^This interface disables all automatic extensions previously | |
| 4418 | +** registered using [sqlite3_auto_extension()]. | |
| 4273 | 4419 | */ |
| 4274 | 4420 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4275 | 4421 | |
| 4276 | 4422 | /* |
| 4277 | 4423 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -4906,11 +5052,11 @@ | ||
| 4906 | 5052 | ** output variable when querying the system for the current mutex |
| 4907 | 5053 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 4908 | 5054 | ** |
| 4909 | 5055 | ** ^The xMutexInit method defined by this structure is invoked as |
| 4910 | 5056 | ** part of system initialization by the sqlite3_initialize() function. |
| 4911 | -** ^The xMutexInit routine is calle by SQLite exactly once for each | |
| 5057 | +** ^The xMutexInit routine is called by SQLite exactly once for each | |
| 4912 | 5058 | ** effective call to [sqlite3_initialize()]. |
| 4913 | 5059 | ** |
| 4914 | 5060 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 4915 | 5061 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 4916 | 5062 | ** implementation of this method is expected to release all outstanding |
| @@ -5103,11 +5249,12 @@ | ||
| 5103 | 5249 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5104 | 5250 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5105 | 5251 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5106 | 5252 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5107 | 5253 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5108 | -#define SQLITE_TESTCTRL_LAST 17 | |
| 5254 | +#define SQLITE_TESTCTRL_SCRATCHMALLOC 18 | |
| 5255 | +#define SQLITE_TESTCTRL_LAST 18 | |
| 5109 | 5256 | |
| 5110 | 5257 | /* |
| 5111 | 5258 | ** CAPI3REF: SQLite Runtime Status |
| 5112 | 5259 | ** |
| 5113 | 5260 | ** ^This interface is used to retrieve runtime status information |
| @@ -5122,11 +5269,11 @@ | ||
| 5122 | 5269 | ** value. For those parameters |
| 5123 | 5270 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5124 | 5271 | ** ^(Other parameters record only the highwater mark and not the current |
| 5125 | 5272 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5126 | 5273 | ** |
| 5127 | -** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a | |
| 5274 | +** ^The sqlite3_status() routine returns SQLITE_OK on success and a | |
| 5128 | 5275 | ** non-zero [error code] on failure. |
| 5129 | 5276 | ** |
| 5130 | 5277 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5131 | 5278 | ** called while other threads are running the same or different SQLite |
| 5132 | 5279 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5172,11 +5319,11 @@ | ||
| 5172 | 5319 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5173 | 5320 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5174 | 5321 | ** |
| 5175 | 5322 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5176 | 5323 | ** <dd>This parameter returns the number of bytes of page cache |
| 5177 | -** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE] | |
| 5324 | +** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] | |
| 5178 | 5325 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5179 | 5326 | ** returned value includes allocations that overflowed because they |
| 5180 | 5327 | ** where too large (they were larger than the "sz" parameter to |
| 5181 | 5328 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5182 | 5329 | ** no space was left in the page cache.</dd>)^ |
| @@ -5195,11 +5342,11 @@ | ||
| 5195 | 5342 | ** outstanding at time, this parameter also reports the number of threads |
| 5196 | 5343 | ** using scratch memory at the same time.</dd>)^ |
| 5197 | 5344 | ** |
| 5198 | 5345 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5199 | 5346 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5200 | -** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH] | |
| 5347 | +** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] | |
| 5201 | 5348 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5202 | 5349 | ** returned include overflows because the requested allocation was too |
| 5203 | 5350 | ** larger (that is, because the requested allocation was larger than the |
| 5204 | 5351 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5205 | 5352 | ** slots were available. |
| @@ -5243,10 +5390,13 @@ | ||
| 5243 | 5390 | ** |
| 5244 | 5391 | ** ^The current value of the requested parameter is written into *pCur |
| 5245 | 5392 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5246 | 5393 | ** the resetFlg is true, then the highest instantaneous value is |
| 5247 | 5394 | ** reset back down to the current value. |
| 5395 | +** | |
| 5396 | +** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a | |
| 5397 | +** non-zero [error code] on failure. | |
| 5248 | 5398 | ** |
| 5249 | 5399 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5250 | 5400 | */ |
| 5251 | 5401 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5252 | 5402 | |
| @@ -5370,122 +5520,134 @@ | ||
| 5370 | 5520 | ** CAPI3REF: Application Defined Page Cache. |
| 5371 | 5521 | ** KEYWORDS: {page cache} |
| 5372 | 5522 | ** |
| 5373 | 5523 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5374 | 5524 | ** register an alternative page cache implementation by passing in an |
| 5375 | -** instance of the sqlite3_pcache_methods structure.)^ The majority of the | |
| 5376 | -** heap memory used by SQLite is used by the page cache to cache data read | |
| 5377 | -** from, or ready to be written to, the database file. By implementing a | |
| 5378 | -** custom page cache using this API, an application can control more | |
| 5379 | -** precisely the amount of memory consumed by SQLite, the way in which | |
| 5525 | +** instance of the sqlite3_pcache_methods structure.)^ | |
| 5526 | +** In many applications, most of the heap memory allocated by | |
| 5527 | +** SQLite is used for the page cache. | |
| 5528 | +** By implementing a | |
| 5529 | +** custom page cache using this API, an application can better control | |
| 5530 | +** the amount of memory consumed by SQLite, the way in which | |
| 5380 | 5531 | ** that memory is allocated and released, and the policies used to |
| 5381 | 5532 | ** determine exactly which parts of a database file are cached and for |
| 5382 | 5533 | ** how long. |
| 5383 | 5534 | ** |
| 5535 | +** The alternative page cache mechanism is an | |
| 5536 | +** extreme measure that is only needed by the most demanding applications. | |
| 5537 | +** The built-in page cache is recommended for most uses. | |
| 5538 | +** | |
| 5384 | 5539 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 5385 | 5540 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 5386 | 5541 | ** the application may discard the parameter after the call to |
| 5387 | 5542 | ** [sqlite3_config()] returns.)^ |
| 5388 | 5543 | ** |
| 5389 | -** ^The xInit() method is called once for each call to [sqlite3_initialize()] | |
| 5544 | +** ^(The xInit() method is called once for each effective | |
| 5545 | +** call to [sqlite3_initialize()])^ | |
| 5390 | 5546 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 5391 | 5547 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 5392 | -** ^The xInit() method can set up up global structures and/or any mutexes | |
| 5548 | +** The intent of the xInit() method is to set up global data structures | |
| 5393 | 5549 | ** required by the custom page cache implementation. |
| 5550 | +** ^(If the xInit() method is NULL, then the | |
| 5551 | +** built-in default page cache is used instead of the application defined | |
| 5552 | +** page cache.)^ | |
| 5394 | 5553 | ** |
| 5395 | -** ^The xShutdown() method is called from within [sqlite3_shutdown()], | |
| 5396 | -** if the application invokes this API. It can be used to clean up | |
| 5554 | +** ^The xShutdown() method is called by [sqlite3_shutdown()]. | |
| 5555 | +** It can be used to clean up | |
| 5397 | 5556 | ** any outstanding resources before process shutdown, if required. |
| 5557 | +** ^The xShutdown() method may be NULL. | |
| 5398 | 5558 | ** |
| 5399 | -** ^SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes | |
| 5400 | -** the xInit method, so the xInit method need not be threadsafe. ^The | |
| 5559 | +** ^SQLite automatically serializes calls to the xInit method, | |
| 5560 | +** so the xInit method need not be threadsafe. ^The | |
| 5401 | 5561 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 5402 | 5562 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 5403 | 5563 | ** in multithreaded applications. |
| 5404 | 5564 | ** |
| 5405 | 5565 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 5406 | 5566 | ** call to xShutdown(). |
| 5407 | 5567 | ** |
| 5408 | -** ^The xCreate() method is used to construct a new cache instance. SQLite | |
| 5409 | -** will typically create one cache instance for each open database file, | |
| 5568 | +** ^SQLite invokes the xCreate() method to construct a new cache instance. | |
| 5569 | +** SQLite will typically create one cache instance for each open database file, | |
| 5410 | 5570 | ** though this is not guaranteed. ^The |
| 5411 | 5571 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 5412 | 5572 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 5413 | 5573 | ** will the page size of the database file that is to be cached plus an |
| 5414 | -** increment (here called "R") of about 100 or 200. ^SQLite will use the | |
| 5574 | +** increment (here called "R") of about 100 or 200. SQLite will use the | |
| 5415 | 5575 | ** extra R bytes on each page to store metadata about the underlying |
| 5416 | 5576 | ** database page on disk. The value of R depends |
| 5417 | 5577 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 5418 | 5578 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 5419 | 5579 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 5420 | 5580 | ** be used to cache database pages of a file stored on disk, or |
| 5421 | -** false if it is used for an in-memory database. ^The cache implementation | |
| 5581 | +** false if it is used for an in-memory database. The cache implementation | |
| 5422 | 5582 | ** does not have to do anything special based with the value of bPurgeable; |
| 5423 | 5583 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 5424 | 5584 | ** never invoke xUnpin() except to deliberately delete a page. |
| 5425 | -** ^In other words, a cache created with bPurgeable set to false will | |
| 5585 | +** ^In other words, calls to xUnpin() on a cache with bPurgeable set to | |
| 5586 | +** false will always have the "discard" flag set to true. | |
| 5587 | +** ^Hence, a cache created with bPurgeable false will | |
| 5426 | 5588 | ** never contain any unpinned pages. |
| 5427 | 5589 | ** |
| 5428 | 5590 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 5429 | 5591 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 5430 | 5592 | ** instance passed as the first argument. This is the value configured using |
| 5431 | -** the SQLite "[PRAGMA cache_size]" command.)^ ^As with the bPurgeable | |
| 5593 | +** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable | |
| 5432 | 5594 | ** parameter, the implementation is not required to do anything with this |
| 5433 | 5595 | ** value; it is advisory only. |
| 5434 | 5596 | ** |
| 5435 | -** ^The xPagecount() method should return the number of pages currently | |
| 5436 | -** stored in the cache. | |
| 5597 | +** The xPagecount() method must return the number of pages currently | |
| 5598 | +** stored in the cache, both pinned and unpinned. | |
| 5437 | 5599 | ** |
| 5438 | -** ^The xFetch() method is used to fetch a page and return a pointer to it. | |
| 5439 | -** ^A 'page', in this context, is a buffer of szPage bytes aligned at an | |
| 5440 | -** 8-byte boundary. ^The page to be fetched is determined by the key. ^The | |
| 5441 | -** mimimum key value is 1. After it has been retrieved using xFetch, the page | |
| 5600 | +** The xFetch() method locates a page in the cache and returns a pointer to | |
| 5601 | +** the page, or a NULL pointer. | |
| 5602 | +** A "page", in this context, means a buffer of szPage bytes aligned at an | |
| 5603 | +** 8-byte boundary. The page to be fetched is determined by the key. ^The | |
| 5604 | +** mimimum key value is 1. After it has been retrieved using xFetch, the page | |
| 5442 | 5605 | ** is considered to be "pinned". |
| 5443 | 5606 | ** |
| 5444 | -** ^If the requested page is already in the page cache, then the page cache | |
| 5607 | +** If the requested page is already in the page cache, then the page cache | |
| 5445 | 5608 | ** implementation must return a pointer to the page buffer with its content |
| 5446 | -** intact. ^(If the requested page is not already in the cache, then the | |
| 5447 | -** behavior of the cache implementation is determined by the value of the | |
| 5448 | -** createFlag parameter passed to xFetch, according to the following table: | |
| 5609 | +** intact. If the requested page is not already in the cache, then the | |
| 5610 | +** behavior of the cache implementation should use the value of the createFlag | |
| 5611 | +** parameter to help it determined what action to take: | |
| 5449 | 5612 | ** |
| 5450 | 5613 | ** <table border=1 width=85% align=center> |
| 5451 | 5614 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 5452 | 5615 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 5453 | 5616 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 5454 | 5617 | ** Otherwise return NULL. |
| 5455 | 5618 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 5456 | 5619 | ** NULL if allocating a new page is effectively impossible. |
| 5457 | -** </table>)^ | |
| 5620 | +** </table> | |
| 5458 | 5621 | ** |
| 5459 | -** SQLite will normally invoke xFetch() with a createFlag of 0 or 1. If | |
| 5460 | -** a call to xFetch() with createFlag==1 returns NULL, then SQLite will | |
| 5622 | +** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite | |
| 5623 | +** will only use a createFlag of 2 after a prior call with a createFlag of 1 | |
| 5624 | +** failed.)^ In between the to xFetch() calls, SQLite may | |
| 5461 | 5625 | ** attempt to unpin one or more cache pages by spilling the content of |
| 5462 | -** pinned pages to disk and synching the operating system disk cache. After | |
| 5463 | -** attempting to unpin pages, the xFetch() method will be invoked again with | |
| 5464 | -** a createFlag of 2. | |
| 5626 | +** pinned pages to disk and synching the operating system disk cache. | |
| 5465 | 5627 | ** |
| 5466 | 5628 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 5467 | -** as its second argument. ^(If the third parameter, discard, is non-zero, | |
| 5468 | -** then the page should be evicted from the cache. In this case SQLite | |
| 5469 | -** assumes that the next time the page is retrieved from the cache using | |
| 5470 | -** the xFetch() method, it will be zeroed.)^ ^If the discard parameter is | |
| 5471 | -** zero, then the page is considered to be unpinned. ^The cache implementation | |
| 5629 | +** as its second argument. If the third parameter, discard, is non-zero, | |
| 5630 | +** then the page must be evicted from the cache. | |
| 5631 | +** ^If the discard parameter is | |
| 5632 | +** zero, then the page may be discarded or retained at the discretion of | |
| 5633 | +** page cache implementation. ^The page cache implementation | |
| 5472 | 5634 | ** may choose to evict unpinned pages at any time. |
| 5473 | 5635 | ** |
| 5474 | -** ^(The cache is not required to perform any reference counting. A single | |
| 5636 | +** The cache must not perform any reference counting. A single | |
| 5475 | 5637 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 5476 | -** to xFetch().)^ | |
| 5638 | +** to xFetch(). | |
| 5477 | 5639 | ** |
| 5478 | -** ^The xRekey() method is used to change the key value associated with the | |
| 5479 | -** page passed as the second argument from oldKey to newKey. ^If the cache | |
| 5480 | -** previously contains an entry associated with newKey, it should be | |
| 5640 | +** The xRekey() method is used to change the key value associated with the | |
| 5641 | +** page passed as the second argument. If the cache | |
| 5642 | +** previously contains an entry associated with newKey, it must be | |
| 5481 | 5643 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 5482 | 5644 | ** to be pinned. |
| 5483 | 5645 | ** |
| 5484 | -** ^When SQLite calls the xTruncate() method, the cache must discard all | |
| 5646 | +** When SQLite calls the xTruncate() method, the cache must discard all | |
| 5485 | 5647 | ** existing cache entries with page numbers (keys) greater than or equal |
| 5486 | -** to the value of the iLimit parameter passed to xTruncate(). ^If any | |
| 5648 | +** to the value of the iLimit parameter passed to xTruncate(). If any | |
| 5487 | 5649 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 5488 | 5650 | ** they can be safely discarded. |
| 5489 | 5651 | ** |
| 5490 | 5652 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 5491 | 5653 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -5959,5 +6121,61 @@ | ||
| 5959 | 6121 | #ifdef __cplusplus |
| 5960 | 6122 | } /* End of the 'extern "C"' block */ |
| 5961 | 6123 | #endif |
| 5962 | 6124 | #endif |
| 5963 | 6125 | |
| 6126 | +/* | |
| 6127 | +** 2010 August 30 | |
| 6128 | +** | |
| 6129 | +** The author disclaims copyright to this source code. In place of | |
| 6130 | +** a legal notice, here is a blessing: | |
| 6131 | +** | |
| 6132 | +** May you do good and not evil. | |
| 6133 | +** May you find forgiveness for yourself and forgive others. | |
| 6134 | +** May you share freely, never taking more than you give. | |
| 6135 | +** | |
| 6136 | +************************************************************************* | |
| 6137 | +*/ | |
| 6138 | + | |
| 6139 | +#ifndef _SQLITE3RTREE_H_ | |
| 6140 | +#define _SQLITE3RTREE_H_ | |
| 6141 | + | |
| 6142 | + | |
| 6143 | +#ifdef __cplusplus | |
| 6144 | +extern "C" { | |
| 6145 | +#endif | |
| 6146 | + | |
| 6147 | +typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; | |
| 6148 | + | |
| 6149 | +/* | |
| 6150 | +** Register a geometry callback named zGeom that can be used as part of an | |
| 6151 | +** R-Tree geometry query as follows: | |
| 6152 | +** | |
| 6153 | +** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...) | |
| 6154 | +*/ | |
| 6155 | +SQLITE_API int sqlite3_rtree_geometry_callback( | |
| 6156 | + sqlite3 *db, | |
| 6157 | + const char *zGeom, | |
| 6158 | + int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), | |
| 6159 | + void *pContext | |
| 6160 | +); | |
| 6161 | + | |
| 6162 | + | |
| 6163 | +/* | |
| 6164 | +** A pointer to a structure of the following type is passed as the first | |
| 6165 | +** argument to callbacks registered using rtree_geometry_callback(). | |
| 6166 | +*/ | |
| 6167 | +struct sqlite3_rtree_geometry { | |
| 6168 | + void *pContext; /* Copy of pContext passed to s_r_g_c() */ | |
| 6169 | + int nParam; /* Size of array aParam[] */ | |
| 6170 | + double *aParam; /* Parameters passed to SQL geom function */ | |
| 6171 | + void *pUser; /* Callback implementation user data */ | |
| 6172 | + void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ | |
| 6173 | +}; | |
| 6174 | + | |
| 6175 | + | |
| 6176 | +#ifdef __cplusplus | |
| 6177 | +} /* end of the 'extern "C"' block */ | |
| 6178 | +#endif | |
| 6179 | + | |
| 6180 | +#endif /* ifndef _SQLITE3RTREE_H_ */ | |
| 6181 | + | |
| 5964 | 6182 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -105,13 +105,13 @@ | |
| 105 | ** |
| 106 | ** See also: [sqlite3_libversion()], |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.7.2" |
| 111 | #define SQLITE_VERSION_NUMBER 3007002 |
| 112 | #define SQLITE_SOURCE_ID "2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -755,19 +755,23 @@ | |
| 755 | ** object once the object has been registered. |
| 756 | ** |
| 757 | ** The zName field holds the name of the VFS module. The name must |
| 758 | ** be unique across all VFS modules. |
| 759 | ** |
| 760 | ** SQLite will guarantee that the zFilename parameter to xOpen |
| 761 | ** is either a NULL pointer or string obtained |
| 762 | ** from xFullPathname(). SQLite further guarantees that |
| 763 | ** the string will be valid and unchanged until xClose() is |
| 764 | ** called. Because of the previous sentence, |
| 765 | ** the [sqlite3_file] can safely store a pointer to the |
| 766 | ** filename if it needs to remember the filename for some reason. |
| 767 | ** If the zFilename parameter is xOpen is a NULL pointer then xOpen |
| 768 | ** must invent its own temporary name for the file. Whenever the |
| 769 | ** xFilename parameter is NULL it will also be the case that the |
| 770 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 771 | ** |
| 772 | ** The flags argument to xOpen() includes all bits set in |
| 773 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -774,11 +778,11 @@ | |
| 774 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 775 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 776 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 777 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 778 | ** |
| 779 | ** SQLite will also add one of the following flags to the xOpen() |
| 780 | ** call, depending on the object being opened: |
| 781 | ** |
| 782 | ** <ul> |
| 783 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 784 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -785,11 +789,12 @@ | |
| 785 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 786 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 787 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 788 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 789 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 790 | ** </ul> |
| 791 | ** |
| 792 | ** The file I/O implementation can use the object type flags to |
| 793 | ** change the way it deals with files. For example, an application |
| 794 | ** that does not care about crash recovery or rollback might make |
| 795 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -804,39 +809,40 @@ | |
| 804 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 805 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 806 | ** </ul> |
| 807 | ** |
| 808 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 809 | ** deleted when it is closed. The [SQLITE_OPEN_DELETEONCLOSE] |
| 810 | ** will be set for TEMP databases, journals and for subjournals. |
| 811 | ** |
| 812 | ** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction |
| 813 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 814 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 815 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 816 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 817 | ** be created, and that it is an error if it already exists. |
| 818 | ** It is <i>not</i> used to indicate the file should be opened |
| 819 | ** for exclusive access. |
| 820 | ** |
| 821 | ** At least szOsFile bytes of memory are allocated by SQLite |
| 822 | ** to hold the [sqlite3_file] structure passed as the third |
| 823 | ** argument to xOpen. The xOpen method does not have to |
| 824 | ** allocate the structure; it should just fill it in. Note that |
| 825 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 826 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 827 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 828 | ** element will be valid after xOpen returns regardless of the success |
| 829 | ** or failure of the xOpen call. |
| 830 | ** |
| 831 | ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] |
| 832 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 833 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 834 | ** to test whether a file is at least readable. The file can be a |
| 835 | ** directory. |
| 836 | ** |
| 837 | ** SQLite will always allocate at least mxPathname+1 bytes for the |
| 838 | ** output buffer xFullPathname. The exact size of the output buffer |
| 839 | ** is also passed as a parameter to both methods. If the output buffer |
| 840 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 841 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 842 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -846,14 +852,14 @@ | |
| 846 | ** included in the VFS structure for completeness. |
| 847 | ** The xRandomness() function attempts to return nBytes bytes |
| 848 | ** of good-quality randomness into zOut. The return value is |
| 849 | ** the actual number of bytes of randomness obtained. |
| 850 | ** The xSleep() method causes the calling thread to sleep for at |
| 851 | ** least the number of microseconds given. The xCurrentTime() |
| 852 | ** method returns a Julian Day Number for the current date and time as |
| 853 | ** a floating point value. |
| 854 | ** The xCurrentTimeInt64() method returns, as an integer, the Julian |
| 855 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 856 | ** a 24-hour day). |
| 857 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 858 | ** date and time if that method is available (if iVersion is 2 or |
| 859 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1246,11 +1252,11 @@ | |
| 1246 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1247 | ** following SQLite interfaces become non-operational: |
| 1248 | ** <ul> |
| 1249 | ** <li> [sqlite3_memory_used()] |
| 1250 | ** <li> [sqlite3_memory_highwater()] |
| 1251 | ** <li> [sqlite3_soft_heap_limit()] |
| 1252 | ** <li> [sqlite3_status()] |
| 1253 | ** </ul>)^ |
| 1254 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1255 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1256 | ** allocation statistics are disabled by default. |
| @@ -1260,19 +1266,18 @@ | |
| 1260 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1261 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1262 | ** aligned memory buffer from which the scrach allocations will be |
| 1263 | ** drawn, the size of each scratch allocation (sz), |
| 1264 | ** and the maximum number of scratch allocations (N). The sz |
| 1265 | ** argument must be a multiple of 16. The sz parameter should be a few bytes |
| 1266 | ** larger than the actual scratch space required due to internal overhead. |
| 1267 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1268 | ** of at least sz*N bytes of memory. |
| 1269 | ** ^SQLite will use no more than one scratch buffer per thread. So |
| 1270 | ** N should be set to the expected maximum number of threads. ^SQLite will |
| 1271 | ** never require a scratch buffer that is more than 6 times the database |
| 1272 | ** page size. ^If SQLite needs needs additional scratch memory beyond |
| 1273 | ** what is provided by this configuration option, then |
| 1274 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1275 | ** |
| 1276 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1277 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1278 | ** the database page cache with the default page cache implemenation. |
| @@ -1288,12 +1293,11 @@ | |
| 1288 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1289 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1290 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1291 | ** page cache memory is needed beyond what is provided by this option, then |
| 1292 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1293 | ** ^The implementation might use one or more of the N buffers to hold |
| 1294 | ** memory accounting information. The pointer in the first argument must |
| 1295 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1296 | ** will be undefined.</dd> |
| 1297 | ** |
| 1298 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1299 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1418,12 +1422,18 @@ | |
| 1418 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1419 | ** slots. The size of the buffer in the first argument must be greater than |
| 1420 | ** or equal to the product of the second and third arguments. The buffer |
| 1421 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1422 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1423 | ** rounded down to the next smaller |
| 1424 | ** multiple of 8. See also: [SQLITE_CONFIG_LOOKASIDE]</dd> |
| 1425 | ** |
| 1426 | ** </dl> |
| 1427 | */ |
| 1428 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1429 | |
| @@ -1723,10 +1733,13 @@ | |
| 1723 | */ |
| 1724 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 1725 | |
| 1726 | /* |
| 1727 | ** CAPI3REF: Convenience Routines For Running Queries |
| 1728 | ** |
| 1729 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 1730 | ** [sqlite3_get_table()] interface. A result table records the |
| 1731 | ** complete query results from one or more queries. |
| 1732 | ** |
| @@ -1744,11 +1757,11 @@ | |
| 1744 | ** |
| 1745 | ** A result table might consist of one or more memory allocations. |
| 1746 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 1747 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 1748 | ** |
| 1749 | ** As an example of the result table format, suppose a query result |
| 1750 | ** is as follows: |
| 1751 | ** |
| 1752 | ** <blockquote><pre> |
| 1753 | ** Name | Age |
| 1754 | ** ----------------------- |
| @@ -1768,31 +1781,31 @@ | |
| 1768 | ** azResult[3] = "43"; |
| 1769 | ** azResult[4] = "Bob"; |
| 1770 | ** azResult[5] = "28"; |
| 1771 | ** azResult[6] = "Cindy"; |
| 1772 | ** azResult[7] = "21"; |
| 1773 | ** </pre></blockquote> |
| 1774 | ** |
| 1775 | ** ^The sqlite3_get_table() function evaluates one or more |
| 1776 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 1777 | ** string of its 2nd parameter and returns a result table to the |
| 1778 | ** pointer given in its 3rd parameter. |
| 1779 | ** |
| 1780 | ** After the application has finished with the result from sqlite3_get_table(), |
| 1781 | ** it should pass the result table pointer to sqlite3_free_table() in order to |
| 1782 | ** release the memory that was malloced. Because of the way the |
| 1783 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 1784 | ** function must not try to call [sqlite3_free()] directly. Only |
| 1785 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 1786 | ** |
| 1787 | ** ^(The sqlite3_get_table() interface is implemented as a wrapper around |
| 1788 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 1789 | ** to any internal data structures of SQLite. It uses only the public |
| 1790 | ** interface defined here. As a consequence, errors that occur in the |
| 1791 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 1792 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 1793 | ** [sqlite3_errmsg()].)^ |
| 1794 | */ |
| 1795 | SQLITE_API int sqlite3_get_table( |
| 1796 | sqlite3 *db, /* An open database */ |
| 1797 | const char *zSql, /* SQL to be evaluated */ |
| 1798 | char ***pazResult, /* Results of the query */ |
| @@ -1940,11 +1953,13 @@ | |
| 1940 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 1941 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 1942 | ** is not freed. |
| 1943 | ** |
| 1944 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 1945 | ** is always aligned to at least an 8 byte boundary. |
| 1946 | ** |
| 1947 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 1948 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 1949 | ** implementation of these routines to be omitted. That capability |
| 1950 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2198,21 +2213,32 @@ | |
| 2198 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2199 | |
| 2200 | /* |
| 2201 | ** CAPI3REF: Query Progress Callbacks |
| 2202 | ** |
| 2203 | ** ^This routine configures a callback function - the |
| 2204 | ** progress callback - that is invoked periodically during long |
| 2205 | ** running calls to [sqlite3_exec()], [sqlite3_step()] and |
| 2206 | ** [sqlite3_get_table()]. An example use for this |
| 2207 | ** interface is to keep a GUI updated during a large query. |
| 2208 | ** |
| 2209 | ** ^If the progress callback returns non-zero, the operation is |
| 2210 | ** interrupted. This feature can be used to implement a |
| 2211 | ** "Cancel" button on a GUI progress dialog box. |
| 2212 | ** |
| 2213 | ** The progress handler must not do anything that will modify |
| 2214 | ** the database connection that invoked the progress handler. |
| 2215 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2216 | ** database connections for the meaning of "modify" in this paragraph. |
| 2217 | ** |
| 2218 | */ |
| @@ -2267,11 +2293,11 @@ | |
| 2267 | ** </dl> |
| 2268 | ** |
| 2269 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2270 | ** combinations shown above or one of the combinations shown above combined |
| 2271 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2272 | ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags, |
| 2273 | ** then the behavior is undefined. |
| 2274 | ** |
| 2275 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2276 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2277 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2392,21 +2418,26 @@ | |
| 2392 | ** ^(This interface allows the size of various constructs to be limited |
| 2393 | ** on a connection by connection basis. The first parameter is the |
| 2394 | ** [database connection] whose limit is to be set or queried. The |
| 2395 | ** second parameter is one of the [limit categories] that define a |
| 2396 | ** class of constructs to be size limited. The third parameter is the |
| 2397 | ** new limit for that construct. The function returns the old limit.)^ |
| 2398 | ** |
| 2399 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2400 | ** ^(For the limit category of SQLITE_LIMIT_XYZ there is a |
| 2401 | ** [limits | hard upper bound] |
| 2402 | ** set by a compile-time C preprocessor macro named |
| 2403 | ** [limits | SQLITE_MAX_XYZ]. |
| 2404 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2405 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2406 | ** silently truncated to the hard upper bound. |
| 2407 | ** |
| 2408 | ** Run-time limits are intended for use in applications that manage |
| 2409 | ** both their own internal database and also databases that are controlled |
| 2410 | ** by untrusted external sources. An example application might be a |
| 2411 | ** web browser that has its own databases for storing history and |
| 2412 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2431,11 +2462,11 @@ | |
| 2431 | ** The synopsis of the meanings of the various limits is shown below. |
| 2432 | ** Additional information is available at [limits | Limits in SQLite]. |
| 2433 | ** |
| 2434 | ** <dl> |
| 2435 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 2436 | ** <dd>The maximum size of any string or BLOB or table row.<dd>)^ |
| 2437 | ** |
| 2438 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 2439 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 2440 | ** |
| 2441 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2449,11 +2480,13 @@ | |
| 2449 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 2450 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 2451 | ** |
| 2452 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 2453 | ** <dd>The maximum number of instructions in a virtual machine program |
| 2454 | ** used to implement an SQL statement.</dd>)^ |
| 2455 | ** |
| 2456 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 2457 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 2458 | ** |
| 2459 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2462,12 +2495,11 @@ | |
| 2462 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 2463 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 2464 | ** [GLOB] operators.</dd>)^ |
| 2465 | ** |
| 2466 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 2467 | ** <dd>The maximum number of variables in an SQL statement that can |
| 2468 | ** be bound.</dd>)^ |
| 2469 | ** |
| 2470 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 2471 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 2472 | ** </dl> |
| 2473 | */ |
| @@ -2535,16 +2567,11 @@ | |
| 2535 | ** |
| 2536 | ** <ol> |
| 2537 | ** <li> |
| 2538 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 2539 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 2540 | ** statement and try to run it again. ^If the schema has changed in |
| 2541 | ** a way that makes the statement no longer valid, [sqlite3_step()] will still |
| 2542 | ** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is |
| 2543 | ** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the |
| 2544 | ** error go away. Note: use [sqlite3_errmsg()] to find the text |
| 2545 | ** of the parsing error that results in an [SQLITE_SCHEMA] return. |
| 2546 | ** </li> |
| 2547 | ** |
| 2548 | ** <li> |
| 2549 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 2550 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -2553,15 +2580,20 @@ | |
| 2553 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 2554 | ** interfaces, the underlying reason for the error is returned immediately. |
| 2555 | ** </li> |
| 2556 | ** |
| 2557 | ** <li> |
| 2558 | ** ^If the value of a [parameter | host parameter] in the WHERE clause might |
| 2559 | ** change the query plan for a statement, then the statement may be |
| 2560 | ** automatically recompiled (as if there had been a schema change) on the first |
| 2561 | ** [sqlite3_step()] call following any change to the |
| 2562 | ** [sqlite3_bind_text | bindings] of the [parameter]. |
| 2563 | ** </li> |
| 2564 | ** </ol> |
| 2565 | */ |
| 2566 | SQLITE_API int sqlite3_prepare( |
| 2567 | sqlite3 *db, /* Database handle */ |
| @@ -2624,11 +2656,11 @@ | |
| 2624 | ** or if SQLite is run in one of reduced mutex modes |
| 2625 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 2626 | ** then there is no distinction between protected and unprotected |
| 2627 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 2628 | ** for maximum code portability it is recommended that applications |
| 2629 | ** still make the distinction between between protected and unprotected |
| 2630 | ** sqlite3_value objects even when not strictly required. |
| 2631 | ** |
| 2632 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 2633 | ** implementation of [application-defined SQL functions] are protected. |
| 2634 | ** ^The sqlite3_value object returned by |
| @@ -2819,10 +2851,12 @@ | |
| 2819 | ** CAPI3REF: Number Of Columns In A Result Set |
| 2820 | ** |
| 2821 | ** ^Return the number of columns in the result set returned by the |
| 2822 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 2823 | ** statement that does not return data (for example an [UPDATE]). |
| 2824 | */ |
| 2825 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 2826 | |
| 2827 | /* |
| 2828 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3009,12 +3043,18 @@ | |
| 3009 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3010 | |
| 3011 | /* |
| 3012 | ** CAPI3REF: Number of columns in a result set |
| 3013 | ** |
| 3014 | ** ^The sqlite3_data_count(P) the number of columns in the |
| 3015 | ** of the result set of [prepared statement] P. |
| 3016 | */ |
| 3017 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3018 | |
| 3019 | /* |
| 3020 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3090,22 +3130,30 @@ | |
| 3090 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3091 | ** the string to UTF-8 and then returns the number of bytes. |
| 3092 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3093 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3094 | ** the number of bytes in that string. |
| 3095 | ** ^The value returned does not include the zero terminator at the end |
| 3096 | ** of the string. ^For clarity: the value returned is the number of |
| 3097 | ** bytes in the string, not the number of characters. |
| 3098 | ** |
| 3099 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3100 | ** even empty strings, are always zero terminated. ^The return |
| 3101 | ** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary |
| 3102 | ** pointer, possibly even a NULL pointer. |
| 3103 | ** |
| 3104 | ** ^The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() |
| 3105 | ** but leaves the result in UTF-16 in native byte order instead of UTF-8. |
| 3106 | ** ^The zero terminator is not included in this count. |
| 3107 | ** |
| 3108 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3109 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3110 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3111 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3146,14 +3194,14 @@ | |
| 3146 | ** and atof(). SQLite does not really use these functions. It has its |
| 3147 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3148 | ** used in the table for brevity and because they are familiar to most |
| 3149 | ** C programmers. |
| 3150 | ** |
| 3151 | ** ^Note that when type conversions occur, pointers returned by prior |
| 3152 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3153 | ** sqlite3_column_text16() may be invalidated. |
| 3154 | ** ^(Type conversions and pointer invalidations might occur |
| 3155 | ** in the following cases: |
| 3156 | ** |
| 3157 | ** <ul> |
| 3158 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3159 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3162,26 +3210,26 @@ | |
| 3162 | ** sqlite3_column_text16() is called. The content must be converted |
| 3163 | ** to UTF-16.</li> |
| 3164 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3165 | ** sqlite3_column_text() is called. The content must be converted |
| 3166 | ** to UTF-8.</li> |
| 3167 | ** </ul>)^ |
| 3168 | ** |
| 3169 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3170 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3171 | ** that the prior pointer points to will have been modified. Other kinds |
| 3172 | ** of conversion are done in place when it is possible, but sometimes they |
| 3173 | ** are not possible and in those cases prior pointers are invalidated. |
| 3174 | ** |
| 3175 | ** ^(The safest and easiest to remember policy is to invoke these routines |
| 3176 | ** in one of the following ways: |
| 3177 | ** |
| 3178 | ** <ul> |
| 3179 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3180 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3181 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3182 | ** </ul>)^ |
| 3183 | ** |
| 3184 | ** In other words, you should call sqlite3_column_text(), |
| 3185 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3186 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3187 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3215,21 +3263,30 @@ | |
| 3215 | |
| 3216 | /* |
| 3217 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3218 | ** |
| 3219 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3220 | ** ^If the statement was executed successfully or not executed at all, then |
| 3221 | ** SQLITE_OK is returned. ^If execution of the statement failed then an |
| 3222 | ** [error code] or [extended error code] is returned. |
| 3223 | ** |
| 3224 | ** ^This routine can be called at any point during the execution of the |
| 3225 | ** [prepared statement]. ^If the virtual machine has not |
| 3226 | ** completed execution when this routine is called, that is like |
| 3227 | ** encountering an error or an [sqlite3_interrupt | interrupt]. |
| 3228 | ** ^Incomplete updates may be rolled back and transactions canceled, |
| 3229 | ** depending on the circumstances, and the |
| 3230 | ** [error code] returned will be [SQLITE_ABORT]. |
| 3231 | */ |
| 3232 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3233 | |
| 3234 | /* |
| 3235 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3261,40 +3318,42 @@ | |
| 3261 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3262 | ** KEYWORDS: {function creation routines} |
| 3263 | ** KEYWORDS: {application-defined SQL function} |
| 3264 | ** KEYWORDS: {application-defined SQL functions} |
| 3265 | ** |
| 3266 | ** ^These two functions (collectively known as "function creation routines") |
| 3267 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3268 | ** of existing SQL functions or aggregates. The only difference between the |
| 3269 | ** two is that the second parameter, the name of the (scalar) function or |
| 3270 | ** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16 |
| 3271 | ** for sqlite3_create_function16(). |
| 3272 | ** |
| 3273 | ** ^The first parameter is the [database connection] to which the SQL |
| 3274 | ** function is to be added. ^If an application uses more than one database |
| 3275 | ** connection then application-defined SQL functions must be added |
| 3276 | ** to each database connection separately. |
| 3277 | ** |
| 3278 | ** The second parameter is the name of the SQL function to be created or |
| 3279 | ** redefined. ^The length of the name is limited to 255 bytes, exclusive of |
| 3280 | ** the zero-terminator. Note that the name length limit is in bytes, not |
| 3281 | ** characters. ^Any attempt to create a function with a longer name |
| 3282 | ** will result in [SQLITE_ERROR] being returned. |
| 3283 | ** |
| 3284 | ** ^The third parameter (nArg) |
| 3285 | ** is the number of arguments that the SQL function or |
| 3286 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3287 | ** aggregate may take any number of arguments between 0 and the limit |
| 3288 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3289 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3290 | ** undefined. |
| 3291 | ** |
| 3292 | ** The fourth parameter, eTextRep, specifies what |
| 3293 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3294 | ** its parameters. Any SQL function implementation should be able to work |
| 3295 | ** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be |
| 3296 | ** more efficient with one encoding than another. ^An application may |
| 3297 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3298 | ** times with the same function but with different values of eTextRep. |
| 3299 | ** ^When multiple implementations of the same function are available, SQLite |
| 3300 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3302,17 +3361,25 @@ | |
| 3302 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3303 | ** |
| 3304 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3305 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3306 | ** |
| 3307 | ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are |
| 3308 | ** pointers to C-language functions that implement the SQL function or |
| 3309 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3310 | ** callback only; NULL pointers should be passed as the xStep and xFinal |
| 3311 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3312 | ** and xFinal and NULL should be passed for xFunc. ^To delete an existing |
| 3313 | ** SQL function or aggregate, pass NULL for all three function callbacks. |
| 3314 | ** |
| 3315 | ** ^It is permitted to register multiple implementations of the same |
| 3316 | ** functions with the same name but with either differing numbers of |
| 3317 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3318 | ** the implementation that most closely matches the way in which the |
| @@ -3324,15 +3391,10 @@ | |
| 3324 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3325 | ** is a closer match than a function where the encoding difference is |
| 3326 | ** between UTF8 and UTF16. |
| 3327 | ** |
| 3328 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3329 | ** ^The first application-defined function with a given name overrides all |
| 3330 | ** built-in functions in the same [database connection] with the same name. |
| 3331 | ** ^Subsequent application-defined functions of the same name only override |
| 3332 | ** prior application-defined functions that are an exact match for the |
| 3333 | ** number of parameters and preferred encoding. |
| 3334 | ** |
| 3335 | ** ^An application-defined function is permitted to call other |
| 3336 | ** SQLite interfaces. However, such calls must not |
| 3337 | ** close the database connection nor finalize or reset the prepared |
| 3338 | ** statement in which the function is running. |
| @@ -3354,10 +3416,21 @@ | |
| 3354 | int eTextRep, |
| 3355 | void *pApp, |
| 3356 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3357 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3358 | void (*xFinal)(sqlite3_context*) |
| 3359 | ); |
| 3360 | |
| 3361 | /* |
| 3362 | ** CAPI3REF: Text Encodings |
| 3363 | ** |
| @@ -3701,73 +3774,97 @@ | |
| 3701 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 3702 | |
| 3703 | /* |
| 3704 | ** CAPI3REF: Define New Collating Sequences |
| 3705 | ** |
| 3706 | ** These functions are used to add new collation sequences to the |
| 3707 | ** [database connection] specified as the first argument. |
| 3708 | ** |
| 3709 | ** ^The name of the new collation sequence is specified as a UTF-8 string |
| 3710 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 3711 | ** and a UTF-16 string for sqlite3_create_collation16(). ^In all cases |
| 3712 | ** the name is passed as the second function argument. |
| 3713 | ** |
| 3714 | ** ^The third argument may be one of the constants [SQLITE_UTF8], |
| 3715 | ** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied |
| 3716 | ** routine expects to be passed pointers to strings encoded using UTF-8, |
| 3717 | ** UTF-16 little-endian, or UTF-16 big-endian, respectively. ^The |
| 3718 | ** third argument might also be [SQLITE_UTF16] to indicate that the routine |
| 3719 | ** expects pointers to be UTF-16 strings in the native byte order, or the |
| 3720 | ** argument can be [SQLITE_UTF16_ALIGNED] if the |
| 3721 | ** the routine expects pointers to 16-bit word aligned strings |
| 3722 | ** of UTF-16 in the native byte order. |
| 3723 | ** |
| 3724 | ** A pointer to the user supplied routine must be passed as the fifth |
| 3725 | ** argument. ^If it is NULL, this is the same as deleting the collation |
| 3726 | ** sequence (so that SQLite cannot call it any more). |
| 3727 | ** ^Each time the application supplied function is invoked, it is passed |
| 3728 | ** as its first parameter a copy of the void* passed as the fourth argument |
| 3729 | ** to sqlite3_create_collation() or sqlite3_create_collation16(). |
| 3730 | ** |
| 3731 | ** ^The remaining arguments to the application-supplied routine are two strings, |
| 3732 | ** each represented by a (length, data) pair and encoded in the encoding |
| 3733 | ** that was passed as the third argument when the collation sequence was |
| 3734 | ** registered. The application defined collation routine should |
| 3735 | ** return negative, zero or positive if the first string is less than, |
| 3736 | ** equal to, or greater than the second string. i.e. (STRING1 - STRING2). |
| 3737 | ** |
| 3738 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 3739 | ** except that it takes an extra argument which is a destructor for |
| 3740 | ** the collation. ^The destructor is called when the collation is |
| 3741 | ** destroyed and is passed a copy of the fourth parameter void* pointer |
| 3742 | ** of the sqlite3_create_collation_v2(). |
| 3743 | ** ^Collations are destroyed when they are overridden by later calls to the |
| 3744 | ** collation creation functions or when the [database connection] is closed |
| 3745 | ** using [sqlite3_close()]. |
| 3746 | ** |
| 3747 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 3748 | */ |
| 3749 | SQLITE_API int sqlite3_create_collation( |
| 3750 | sqlite3*, |
| 3751 | const char *zName, |
| 3752 | int eTextRep, |
| 3753 | void*, |
| 3754 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3755 | ); |
| 3756 | SQLITE_API int sqlite3_create_collation_v2( |
| 3757 | sqlite3*, |
| 3758 | const char *zName, |
| 3759 | int eTextRep, |
| 3760 | void*, |
| 3761 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 3762 | void(*xDestroy)(void*) |
| 3763 | ); |
| 3764 | SQLITE_API int sqlite3_create_collation16( |
| 3765 | sqlite3*, |
| 3766 | const void *zName, |
| 3767 | int eTextRep, |
| 3768 | void*, |
| 3769 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3770 | ); |
| 3771 | |
| 3772 | /* |
| 3773 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -3852,20 +3949,23 @@ | |
| 3852 | #endif |
| 3853 | |
| 3854 | /* |
| 3855 | ** CAPI3REF: Suspend Execution For A Short Time |
| 3856 | ** |
| 3857 | ** ^The sqlite3_sleep() function causes the current thread to suspend execution |
| 3858 | ** for at least a number of milliseconds specified in its parameter. |
| 3859 | ** |
| 3860 | ** ^If the operating system does not support sleep requests with |
| 3861 | ** millisecond time resolution, then the time will be rounded up to |
| 3862 | ** the nearest second. ^The number of milliseconds of sleep actually |
| 3863 | ** requested from the operating system is returned. |
| 3864 | ** |
| 3865 | ** ^SQLite implements this interface by calling the xSleep() |
| 3866 | ** method of the default [sqlite3_vfs] object. |
| 3867 | */ |
| 3868 | SQLITE_API int sqlite3_sleep(int); |
| 3869 | |
| 3870 | /* |
| 3871 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4083,44 +4183,77 @@ | |
| 4083 | ** of heap memory by deallocating non-essential memory allocations |
| 4084 | ** held by the database library. Memory used to cache database |
| 4085 | ** pages to improve performance is an example of non-essential memory. |
| 4086 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4087 | ** which might be more or less than the amount requested. |
| 4088 | */ |
| 4089 | SQLITE_API int sqlite3_release_memory(int); |
| 4090 | |
| 4091 | /* |
| 4092 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4093 | ** |
| 4094 | ** ^The sqlite3_soft_heap_limit() interface places a "soft" limit |
| 4095 | ** on the amount of heap memory that may be allocated by SQLite. |
| 4096 | ** ^If an internal allocation is requested that would exceed the |
| 4097 | ** soft heap limit, [sqlite3_release_memory()] is invoked one or |
| 4098 | ** more times to free up some space before the allocation is performed. |
| 4099 | ** |
| 4100 | ** ^The limit is called "soft" because if [sqlite3_release_memory()] |
| 4101 | ** cannot free sufficient memory to prevent the limit from being exceeded, |
| 4102 | ** the memory is allocated anyway and the current operation proceeds. |
| 4103 | ** |
| 4104 | ** ^A negative or zero value for N means that there is no soft heap limit and |
| 4105 | ** [sqlite3_release_memory()] will only be called when memory is exhausted. |
| 4106 | ** ^The default value for the soft heap limit is zero. |
| 4107 | ** |
| 4108 | ** ^(SQLite makes a best effort to honor the soft heap limit. |
| 4109 | ** But if the soft heap limit cannot be honored, execution will |
| 4110 | ** continue without error or notification.)^ This is why the limit is |
| 4111 | ** called a "soft" limit. It is advisory only. |
| 4112 | ** |
| 4113 | ** Prior to SQLite version 3.5.0, this routine only constrained the memory |
| 4114 | ** allocated by a single thread - the same thread in which this routine |
| 4115 | ** runs. Beginning with SQLite version 3.5.0, the soft heap limit is |
| 4116 | ** applied to all threads. The value specified for the soft heap limit |
| 4117 | ** is an upper bound on the total memory allocation for all threads. In |
| 4118 | ** version 3.5.0 there is no mechanism for limiting the heap usage for |
| 4119 | ** individual threads. |
| 4120 | */ |
| 4121 | SQLITE_API void sqlite3_soft_heap_limit(int); |
| 4122 | |
| 4123 | /* |
| 4124 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4125 | ** |
| 4126 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4240,38 +4373,51 @@ | |
| 4240 | ** it back off again. |
| 4241 | */ |
| 4242 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4243 | |
| 4244 | /* |
| 4245 | ** CAPI3REF: Automatically Load An Extensions |
| 4246 | ** |
| 4247 | ** ^This API can be invoked at program startup in order to register |
| 4248 | ** one or more statically linked extensions that will be available |
| 4249 | ** to all new [database connections]. |
| 4250 | ** |
| 4251 | ** ^(This routine stores a pointer to the extension entry point |
| 4252 | ** in an array that is obtained from [sqlite3_malloc()]. That memory |
| 4253 | ** is deallocated by [sqlite3_reset_auto_extension()].)^ |
| 4254 | ** |
| 4255 | ** ^This function registers an extension entry point that is |
| 4256 | ** automatically invoked whenever a new [database connection] |
| 4257 | ** is opened using [sqlite3_open()], [sqlite3_open16()], |
| 4258 | ** or [sqlite3_open_v2()]. |
| 4259 | ** ^Duplicate extensions are detected so calling this routine |
| 4260 | ** multiple times with the same extension is harmless. |
| 4261 | ** ^Automatic extensions apply across all threads. |
| 4262 | */ |
| 4263 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4264 | |
| 4265 | /* |
| 4266 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4267 | ** |
| 4268 | ** ^(This function disables all previously registered automatic |
| 4269 | ** extensions. It undoes the effect of all prior |
| 4270 | ** [sqlite3_auto_extension()] calls.)^ |
| 4271 | ** |
| 4272 | ** ^This function disables automatic extensions in all threads. |
| 4273 | */ |
| 4274 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4275 | |
| 4276 | /* |
| 4277 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -4906,11 +5052,11 @@ | |
| 4906 | ** output variable when querying the system for the current mutex |
| 4907 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 4908 | ** |
| 4909 | ** ^The xMutexInit method defined by this structure is invoked as |
| 4910 | ** part of system initialization by the sqlite3_initialize() function. |
| 4911 | ** ^The xMutexInit routine is calle by SQLite exactly once for each |
| 4912 | ** effective call to [sqlite3_initialize()]. |
| 4913 | ** |
| 4914 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 4915 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 4916 | ** implementation of this method is expected to release all outstanding |
| @@ -5103,11 +5249,12 @@ | |
| 5103 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5104 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5105 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5106 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5107 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5108 | #define SQLITE_TESTCTRL_LAST 17 |
| 5109 | |
| 5110 | /* |
| 5111 | ** CAPI3REF: SQLite Runtime Status |
| 5112 | ** |
| 5113 | ** ^This interface is used to retrieve runtime status information |
| @@ -5122,11 +5269,11 @@ | |
| 5122 | ** value. For those parameters |
| 5123 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5124 | ** ^(Other parameters record only the highwater mark and not the current |
| 5125 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5126 | ** |
| 5127 | ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a |
| 5128 | ** non-zero [error code] on failure. |
| 5129 | ** |
| 5130 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5131 | ** called while other threads are running the same or different SQLite |
| 5132 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5172,11 +5319,11 @@ | |
| 5172 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5173 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5174 | ** |
| 5175 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5176 | ** <dd>This parameter returns the number of bytes of page cache |
| 5177 | ** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE] |
| 5178 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5179 | ** returned value includes allocations that overflowed because they |
| 5180 | ** where too large (they were larger than the "sz" parameter to |
| 5181 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5182 | ** no space was left in the page cache.</dd>)^ |
| @@ -5195,11 +5342,11 @@ | |
| 5195 | ** outstanding at time, this parameter also reports the number of threads |
| 5196 | ** using scratch memory at the same time.</dd>)^ |
| 5197 | ** |
| 5198 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5199 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5200 | ** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH] |
| 5201 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5202 | ** returned include overflows because the requested allocation was too |
| 5203 | ** larger (that is, because the requested allocation was larger than the |
| 5204 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5205 | ** slots were available. |
| @@ -5243,10 +5390,13 @@ | |
| 5243 | ** |
| 5244 | ** ^The current value of the requested parameter is written into *pCur |
| 5245 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5246 | ** the resetFlg is true, then the highest instantaneous value is |
| 5247 | ** reset back down to the current value. |
| 5248 | ** |
| 5249 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5250 | */ |
| 5251 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5252 | |
| @@ -5370,122 +5520,134 @@ | |
| 5370 | ** CAPI3REF: Application Defined Page Cache. |
| 5371 | ** KEYWORDS: {page cache} |
| 5372 | ** |
| 5373 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5374 | ** register an alternative page cache implementation by passing in an |
| 5375 | ** instance of the sqlite3_pcache_methods structure.)^ The majority of the |
| 5376 | ** heap memory used by SQLite is used by the page cache to cache data read |
| 5377 | ** from, or ready to be written to, the database file. By implementing a |
| 5378 | ** custom page cache using this API, an application can control more |
| 5379 | ** precisely the amount of memory consumed by SQLite, the way in which |
| 5380 | ** that memory is allocated and released, and the policies used to |
| 5381 | ** determine exactly which parts of a database file are cached and for |
| 5382 | ** how long. |
| 5383 | ** |
| 5384 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 5385 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 5386 | ** the application may discard the parameter after the call to |
| 5387 | ** [sqlite3_config()] returns.)^ |
| 5388 | ** |
| 5389 | ** ^The xInit() method is called once for each call to [sqlite3_initialize()] |
| 5390 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 5391 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 5392 | ** ^The xInit() method can set up up global structures and/or any mutexes |
| 5393 | ** required by the custom page cache implementation. |
| 5394 | ** |
| 5395 | ** ^The xShutdown() method is called from within [sqlite3_shutdown()], |
| 5396 | ** if the application invokes this API. It can be used to clean up |
| 5397 | ** any outstanding resources before process shutdown, if required. |
| 5398 | ** |
| 5399 | ** ^SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes |
| 5400 | ** the xInit method, so the xInit method need not be threadsafe. ^The |
| 5401 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 5402 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 5403 | ** in multithreaded applications. |
| 5404 | ** |
| 5405 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 5406 | ** call to xShutdown(). |
| 5407 | ** |
| 5408 | ** ^The xCreate() method is used to construct a new cache instance. SQLite |
| 5409 | ** will typically create one cache instance for each open database file, |
| 5410 | ** though this is not guaranteed. ^The |
| 5411 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 5412 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 5413 | ** will the page size of the database file that is to be cached plus an |
| 5414 | ** increment (here called "R") of about 100 or 200. ^SQLite will use the |
| 5415 | ** extra R bytes on each page to store metadata about the underlying |
| 5416 | ** database page on disk. The value of R depends |
| 5417 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 5418 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 5419 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 5420 | ** be used to cache database pages of a file stored on disk, or |
| 5421 | ** false if it is used for an in-memory database. ^The cache implementation |
| 5422 | ** does not have to do anything special based with the value of bPurgeable; |
| 5423 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 5424 | ** never invoke xUnpin() except to deliberately delete a page. |
| 5425 | ** ^In other words, a cache created with bPurgeable set to false will |
| 5426 | ** never contain any unpinned pages. |
| 5427 | ** |
| 5428 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 5429 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 5430 | ** instance passed as the first argument. This is the value configured using |
| 5431 | ** the SQLite "[PRAGMA cache_size]" command.)^ ^As with the bPurgeable |
| 5432 | ** parameter, the implementation is not required to do anything with this |
| 5433 | ** value; it is advisory only. |
| 5434 | ** |
| 5435 | ** ^The xPagecount() method should return the number of pages currently |
| 5436 | ** stored in the cache. |
| 5437 | ** |
| 5438 | ** ^The xFetch() method is used to fetch a page and return a pointer to it. |
| 5439 | ** ^A 'page', in this context, is a buffer of szPage bytes aligned at an |
| 5440 | ** 8-byte boundary. ^The page to be fetched is determined by the key. ^The |
| 5441 | ** mimimum key value is 1. After it has been retrieved using xFetch, the page |
| 5442 | ** is considered to be "pinned". |
| 5443 | ** |
| 5444 | ** ^If the requested page is already in the page cache, then the page cache |
| 5445 | ** implementation must return a pointer to the page buffer with its content |
| 5446 | ** intact. ^(If the requested page is not already in the cache, then the |
| 5447 | ** behavior of the cache implementation is determined by the value of the |
| 5448 | ** createFlag parameter passed to xFetch, according to the following table: |
| 5449 | ** |
| 5450 | ** <table border=1 width=85% align=center> |
| 5451 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 5452 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 5453 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 5454 | ** Otherwise return NULL. |
| 5455 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 5456 | ** NULL if allocating a new page is effectively impossible. |
| 5457 | ** </table>)^ |
| 5458 | ** |
| 5459 | ** SQLite will normally invoke xFetch() with a createFlag of 0 or 1. If |
| 5460 | ** a call to xFetch() with createFlag==1 returns NULL, then SQLite will |
| 5461 | ** attempt to unpin one or more cache pages by spilling the content of |
| 5462 | ** pinned pages to disk and synching the operating system disk cache. After |
| 5463 | ** attempting to unpin pages, the xFetch() method will be invoked again with |
| 5464 | ** a createFlag of 2. |
| 5465 | ** |
| 5466 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 5467 | ** as its second argument. ^(If the third parameter, discard, is non-zero, |
| 5468 | ** then the page should be evicted from the cache. In this case SQLite |
| 5469 | ** assumes that the next time the page is retrieved from the cache using |
| 5470 | ** the xFetch() method, it will be zeroed.)^ ^If the discard parameter is |
| 5471 | ** zero, then the page is considered to be unpinned. ^The cache implementation |
| 5472 | ** may choose to evict unpinned pages at any time. |
| 5473 | ** |
| 5474 | ** ^(The cache is not required to perform any reference counting. A single |
| 5475 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 5476 | ** to xFetch().)^ |
| 5477 | ** |
| 5478 | ** ^The xRekey() method is used to change the key value associated with the |
| 5479 | ** page passed as the second argument from oldKey to newKey. ^If the cache |
| 5480 | ** previously contains an entry associated with newKey, it should be |
| 5481 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 5482 | ** to be pinned. |
| 5483 | ** |
| 5484 | ** ^When SQLite calls the xTruncate() method, the cache must discard all |
| 5485 | ** existing cache entries with page numbers (keys) greater than or equal |
| 5486 | ** to the value of the iLimit parameter passed to xTruncate(). ^If any |
| 5487 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 5488 | ** they can be safely discarded. |
| 5489 | ** |
| 5490 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 5491 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -5959,5 +6121,61 @@ | |
| 5959 | #ifdef __cplusplus |
| 5960 | } /* End of the 'extern "C"' block */ |
| 5961 | #endif |
| 5962 | #endif |
| 5963 | |
| 5964 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -105,13 +105,13 @@ | |
| 105 | ** |
| 106 | ** See also: [sqlite3_libversion()], |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.7.3" |
| 111 | #define SQLITE_VERSION_NUMBER 3007003 |
| 112 | #define SQLITE_SOURCE_ID "2010-09-29 23:09:23 1ef0dc9328f47506cb2dcd142150e96cb4755216" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -755,19 +755,23 @@ | |
| 755 | ** object once the object has been registered. |
| 756 | ** |
| 757 | ** The zName field holds the name of the VFS module. The name must |
| 758 | ** be unique across all VFS modules. |
| 759 | ** |
| 760 | ** ^SQLite guarantees that the zFilename parameter to xOpen |
| 761 | ** is either a NULL pointer or string obtained |
| 762 | ** from xFullPathname() with an optional suffix added. |
| 763 | ** ^If a suffix is added to the zFilename parameter, it will |
| 764 | ** consist of a single "-" character followed by no more than |
| 765 | ** 10 alphanumeric and/or "-" characters. |
| 766 | ** ^SQLite further guarantees that |
| 767 | ** the string will be valid and unchanged until xClose() is |
| 768 | ** called. Because of the previous sentence, |
| 769 | ** the [sqlite3_file] can safely store a pointer to the |
| 770 | ** filename if it needs to remember the filename for some reason. |
| 771 | ** If the zFilename parameter to xOpen is a NULL pointer then xOpen |
| 772 | ** must invent its own temporary name for the file. ^Whenever the |
| 773 | ** xFilename parameter is NULL it will also be the case that the |
| 774 | ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. |
| 775 | ** |
| 776 | ** The flags argument to xOpen() includes all bits set in |
| 777 | ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] |
| @@ -774,11 +778,11 @@ | |
| 778 | ** or [sqlite3_open16()] is used, then flags includes at least |
| 779 | ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. |
| 780 | ** If xOpen() opens a file read-only then it sets *pOutFlags to |
| 781 | ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. |
| 782 | ** |
| 783 | ** ^(SQLite will also add one of the following flags to the xOpen() |
| 784 | ** call, depending on the object being opened: |
| 785 | ** |
| 786 | ** <ul> |
| 787 | ** <li> [SQLITE_OPEN_MAIN_DB] |
| 788 | ** <li> [SQLITE_OPEN_MAIN_JOURNAL] |
| @@ -785,11 +789,12 @@ | |
| 789 | ** <li> [SQLITE_OPEN_TEMP_DB] |
| 790 | ** <li> [SQLITE_OPEN_TEMP_JOURNAL] |
| 791 | ** <li> [SQLITE_OPEN_TRANSIENT_DB] |
| 792 | ** <li> [SQLITE_OPEN_SUBJOURNAL] |
| 793 | ** <li> [SQLITE_OPEN_MASTER_JOURNAL] |
| 794 | ** <li> [SQLITE_OPEN_WAL] |
| 795 | ** </ul>)^ |
| 796 | ** |
| 797 | ** The file I/O implementation can use the object type flags to |
| 798 | ** change the way it deals with files. For example, an application |
| 799 | ** that does not care about crash recovery or rollback might make |
| 800 | ** the open of a journal file a no-op. Writes to this journal would |
| @@ -804,39 +809,40 @@ | |
| 809 | ** <li> [SQLITE_OPEN_DELETEONCLOSE] |
| 810 | ** <li> [SQLITE_OPEN_EXCLUSIVE] |
| 811 | ** </ul> |
| 812 | ** |
| 813 | ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be |
| 814 | ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] |
| 815 | ** will be set for TEMP databases and their journals, transient |
| 816 | ** databases, and subjournals. |
| 817 | ** |
| 818 | ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction |
| 819 | ** with the [SQLITE_OPEN_CREATE] flag, which are both directly |
| 820 | ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() |
| 821 | ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the |
| 822 | ** SQLITE_OPEN_CREATE, is used to indicate that file should always |
| 823 | ** be created, and that it is an error if it already exists. |
| 824 | ** It is <i>not</i> used to indicate the file should be opened |
| 825 | ** for exclusive access. |
| 826 | ** |
| 827 | ** ^At least szOsFile bytes of memory are allocated by SQLite |
| 828 | ** to hold the [sqlite3_file] structure passed as the third |
| 829 | ** argument to xOpen. The xOpen method does not have to |
| 830 | ** allocate the structure; it should just fill it in. Note that |
| 831 | ** the xOpen method must set the sqlite3_file.pMethods to either |
| 832 | ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do |
| 833 | ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods |
| 834 | ** element will be valid after xOpen returns regardless of the success |
| 835 | ** or failure of the xOpen call. |
| 836 | ** |
| 837 | ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] |
| 838 | ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to |
| 839 | ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] |
| 840 | ** to test whether a file is at least readable. The file can be a |
| 841 | ** directory. |
| 842 | ** |
| 843 | ** ^SQLite will always allocate at least mxPathname+1 bytes for the |
| 844 | ** output buffer xFullPathname. The exact size of the output buffer |
| 845 | ** is also passed as a parameter to both methods. If the output buffer |
| 846 | ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is |
| 847 | ** handled as a fatal error by SQLite, vfs implementations should endeavor |
| 848 | ** to prevent this by setting mxPathname to a sufficiently large value. |
| @@ -846,14 +852,14 @@ | |
| 852 | ** included in the VFS structure for completeness. |
| 853 | ** The xRandomness() function attempts to return nBytes bytes |
| 854 | ** of good-quality randomness into zOut. The return value is |
| 855 | ** the actual number of bytes of randomness obtained. |
| 856 | ** The xSleep() method causes the calling thread to sleep for at |
| 857 | ** least the number of microseconds given. ^The xCurrentTime() |
| 858 | ** method returns a Julian Day Number for the current date and time as |
| 859 | ** a floating point value. |
| 860 | ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian |
| 861 | ** Day Number multipled by 86400000 (the number of milliseconds in |
| 862 | ** a 24-hour day). |
| 863 | ** ^SQLite will use the xCurrentTimeInt64() method to get the current |
| 864 | ** date and time if that method is available (if iVersion is 2 or |
| 865 | ** greater and the function pointer is not NULL) and will fall back |
| @@ -1246,11 +1252,11 @@ | |
| 1252 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1253 | ** following SQLite interfaces become non-operational: |
| 1254 | ** <ul> |
| 1255 | ** <li> [sqlite3_memory_used()] |
| 1256 | ** <li> [sqlite3_memory_highwater()] |
| 1257 | ** <li> [sqlite3_soft_heap_limit64()] |
| 1258 | ** <li> [sqlite3_status()] |
| 1259 | ** </ul>)^ |
| 1260 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1261 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1262 | ** allocation statistics are disabled by default. |
| @@ -1260,19 +1266,18 @@ | |
| 1266 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1267 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1268 | ** aligned memory buffer from which the scrach allocations will be |
| 1269 | ** drawn, the size of each scratch allocation (sz), |
| 1270 | ** and the maximum number of scratch allocations (N). The sz |
| 1271 | ** argument must be a multiple of 16. |
| 1272 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1273 | ** of at least sz*N bytes of memory. |
| 1274 | ** ^SQLite will use no more than two scratch buffers per thread. So |
| 1275 | ** N should be set to twice the expected maximum number of threads. |
| 1276 | ** ^SQLite will never require a scratch buffer that is more than 6 |
| 1277 | ** times the database page size. ^If SQLite needs needs additional |
| 1278 | ** scratch memory beyond what is provided by this configuration option, then |
| 1279 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1280 | ** |
| 1281 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1282 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1283 | ** the database page cache with the default page cache implemenation. |
| @@ -1288,12 +1293,11 @@ | |
| 1293 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1294 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1295 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1296 | ** page cache memory is needed beyond what is provided by this option, then |
| 1297 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1298 | ** The pointer in the first argument must |
| 1299 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1300 | ** will be undefined.</dd> |
| 1301 | ** |
| 1302 | ** <dt>SQLITE_CONFIG_HEAP</dt> |
| 1303 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| @@ -1418,12 +1422,18 @@ | |
| 1422 | ** size of each lookaside buffer slot. ^The third argument is the number of |
| 1423 | ** slots. The size of the buffer in the first argument must be greater than |
| 1424 | ** or equal to the product of the second and third arguments. The buffer |
| 1425 | ** must be aligned to an 8-byte boundary. ^If the second argument to |
| 1426 | ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 1427 | ** rounded down to the next smaller multiple of 8. ^(The lookaside memory |
| 1428 | ** configuration for a database connection can only be changed when that |
| 1429 | ** connection is not currently using lookaside memory, or in other words |
| 1430 | ** when the "current value" returned by |
| 1431 | ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. |
| 1432 | ** Any attempt to change the lookaside memory configuration when lookaside |
| 1433 | ** memory is in use leaves the configuration unchanged and returns |
| 1434 | ** [SQLITE_BUSY].)^</dd> |
| 1435 | ** |
| 1436 | ** </dl> |
| 1437 | */ |
| 1438 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 1439 | |
| @@ -1723,10 +1733,13 @@ | |
| 1733 | */ |
| 1734 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 1735 | |
| 1736 | /* |
| 1737 | ** CAPI3REF: Convenience Routines For Running Queries |
| 1738 | ** |
| 1739 | ** This is a legacy interface that is preserved for backwards compatibility. |
| 1740 | ** Use of this interface is not recommended. |
| 1741 | ** |
| 1742 | ** Definition: A <b>result table</b> is memory data structure created by the |
| 1743 | ** [sqlite3_get_table()] interface. A result table records the |
| 1744 | ** complete query results from one or more queries. |
| 1745 | ** |
| @@ -1744,11 +1757,11 @@ | |
| 1757 | ** |
| 1758 | ** A result table might consist of one or more memory allocations. |
| 1759 | ** It is not safe to pass a result table directly to [sqlite3_free()]. |
| 1760 | ** A result table should be deallocated using [sqlite3_free_table()]. |
| 1761 | ** |
| 1762 | ** ^(As an example of the result table format, suppose a query result |
| 1763 | ** is as follows: |
| 1764 | ** |
| 1765 | ** <blockquote><pre> |
| 1766 | ** Name | Age |
| 1767 | ** ----------------------- |
| @@ -1768,31 +1781,31 @@ | |
| 1781 | ** azResult[3] = "43"; |
| 1782 | ** azResult[4] = "Bob"; |
| 1783 | ** azResult[5] = "28"; |
| 1784 | ** azResult[6] = "Cindy"; |
| 1785 | ** azResult[7] = "21"; |
| 1786 | ** </pre></blockquote>)^ |
| 1787 | ** |
| 1788 | ** ^The sqlite3_get_table() function evaluates one or more |
| 1789 | ** semicolon-separated SQL statements in the zero-terminated UTF-8 |
| 1790 | ** string of its 2nd parameter and returns a result table to the |
| 1791 | ** pointer given in its 3rd parameter. |
| 1792 | ** |
| 1793 | ** After the application has finished with the result from sqlite3_get_table(), |
| 1794 | ** it must pass the result table pointer to sqlite3_free_table() in order to |
| 1795 | ** release the memory that was malloced. Because of the way the |
| 1796 | ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling |
| 1797 | ** function must not try to call [sqlite3_free()] directly. Only |
| 1798 | ** [sqlite3_free_table()] is able to release the memory properly and safely. |
| 1799 | ** |
| 1800 | ** The sqlite3_get_table() interface is implemented as a wrapper around |
| 1801 | ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access |
| 1802 | ** to any internal data structures of SQLite. It uses only the public |
| 1803 | ** interface defined here. As a consequence, errors that occur in the |
| 1804 | ** wrapper layer outside of the internal [sqlite3_exec()] call are not |
| 1805 | ** reflected in subsequent calls to [sqlite3_errcode()] or |
| 1806 | ** [sqlite3_errmsg()]. |
| 1807 | */ |
| 1808 | SQLITE_API int sqlite3_get_table( |
| 1809 | sqlite3 *db, /* An open database */ |
| 1810 | const char *zSql, /* SQL to be evaluated */ |
| 1811 | char ***pazResult, /* Results of the query */ |
| @@ -1940,11 +1953,13 @@ | |
| 1953 | ** by sqlite3_realloc() and the prior allocation is freed. |
| 1954 | ** ^If sqlite3_realloc() returns NULL, then the prior allocation |
| 1955 | ** is not freed. |
| 1956 | ** |
| 1957 | ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() |
| 1958 | ** is always aligned to at least an 8 byte boundary, or to a |
| 1959 | ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time |
| 1960 | ** option is used. |
| 1961 | ** |
| 1962 | ** In SQLite version 3.5.0 and 3.5.1, it was possible to define |
| 1963 | ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in |
| 1964 | ** implementation of these routines to be omitted. That capability |
| 1965 | ** is no longer provided. Only built-in memory allocators can be used. |
| @@ -2198,21 +2213,32 @@ | |
| 2213 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2214 | |
| 2215 | /* |
| 2216 | ** CAPI3REF: Query Progress Callbacks |
| 2217 | ** |
| 2218 | ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback |
| 2219 | ** function X to be invoked periodically during long running calls to |
| 2220 | ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for |
| 2221 | ** database connection D. An example use for this |
| 2222 | ** interface is to keep a GUI updated during a large query. |
| 2223 | ** |
| 2224 | ** ^The parameter P is passed through as the only parameter to the |
| 2225 | ** callback function X. ^The parameter N is the number of |
| 2226 | ** [virtual machine instructions] that are evaluated between successive |
| 2227 | ** invocations of the callback X. |
| 2228 | ** |
| 2229 | ** ^Only a single progress handler may be defined at one time per |
| 2230 | ** [database connection]; setting a new progress handler cancels the |
| 2231 | ** old one. ^Setting parameter X to NULL disables the progress handler. |
| 2232 | ** ^The progress handler is also disabled by setting N to a value less |
| 2233 | ** than 1. |
| 2234 | ** |
| 2235 | ** ^If the progress callback returns non-zero, the operation is |
| 2236 | ** interrupted. This feature can be used to implement a |
| 2237 | ** "Cancel" button on a GUI progress dialog box. |
| 2238 | ** |
| 2239 | ** The progress handler callback must not do anything that will modify |
| 2240 | ** the database connection that invoked the progress handler. |
| 2241 | ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their |
| 2242 | ** database connections for the meaning of "modify" in this paragraph. |
| 2243 | ** |
| 2244 | */ |
| @@ -2267,11 +2293,11 @@ | |
| 2293 | ** </dl> |
| 2294 | ** |
| 2295 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 2296 | ** combinations shown above or one of the combinations shown above combined |
| 2297 | ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], |
| 2298 | ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags, |
| 2299 | ** then the behavior is undefined. |
| 2300 | ** |
| 2301 | ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection |
| 2302 | ** opens in the multi-thread [threading mode] as long as the single-thread |
| 2303 | ** mode has not been set at compile-time or start-time. ^If the |
| @@ -2392,21 +2418,26 @@ | |
| 2418 | ** ^(This interface allows the size of various constructs to be limited |
| 2419 | ** on a connection by connection basis. The first parameter is the |
| 2420 | ** [database connection] whose limit is to be set or queried. The |
| 2421 | ** second parameter is one of the [limit categories] that define a |
| 2422 | ** class of constructs to be size limited. The third parameter is the |
| 2423 | ** new limit for that construct.)^ |
| 2424 | ** |
| 2425 | ** ^If the new limit is a negative number, the limit is unchanged. |
| 2426 | ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a |
| 2427 | ** [limits | hard upper bound] |
| 2428 | ** set at compile-time by a C preprocessor macro called |
| 2429 | ** [limits | SQLITE_MAX_<i>NAME</i>]. |
| 2430 | ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ |
| 2431 | ** ^Attempts to increase a limit above its hard upper bound are |
| 2432 | ** silently truncated to the hard upper bound. |
| 2433 | ** |
| 2434 | ** ^Regardless of whether or not the limit was changed, the |
| 2435 | ** [sqlite3_limit()] interface returns the prior value of the limit. |
| 2436 | ** ^Hence, to find the current value of a limit without changing it, |
| 2437 | ** simply invoke this interface with the third parameter set to -1. |
| 2438 | ** |
| 2439 | ** Run-time limits are intended for use in applications that manage |
| 2440 | ** both their own internal database and also databases that are controlled |
| 2441 | ** by untrusted external sources. An example application might be a |
| 2442 | ** web browser that has its own databases for storing history and |
| 2443 | ** separate databases controlled by JavaScript applications downloaded |
| @@ -2431,11 +2462,11 @@ | |
| 2462 | ** The synopsis of the meanings of the various limits is shown below. |
| 2463 | ** Additional information is available at [limits | Limits in SQLite]. |
| 2464 | ** |
| 2465 | ** <dl> |
| 2466 | ** ^(<dt>SQLITE_LIMIT_LENGTH</dt> |
| 2467 | ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^ |
| 2468 | ** |
| 2469 | ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> |
| 2470 | ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ |
| 2471 | ** |
| 2472 | ** ^(<dt>SQLITE_LIMIT_COLUMN</dt> |
| @@ -2449,11 +2480,13 @@ | |
| 2480 | ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> |
| 2481 | ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ |
| 2482 | ** |
| 2483 | ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> |
| 2484 | ** <dd>The maximum number of instructions in a virtual machine program |
| 2485 | ** used to implement an SQL statement. This limit is not currently |
| 2486 | ** enforced, though that might be added in some future release of |
| 2487 | ** SQLite.</dd>)^ |
| 2488 | ** |
| 2489 | ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> |
| 2490 | ** <dd>The maximum number of arguments on a function.</dd>)^ |
| 2491 | ** |
| 2492 | ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt> |
| @@ -2462,12 +2495,11 @@ | |
| 2495 | ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> |
| 2496 | ** <dd>The maximum length of the pattern argument to the [LIKE] or |
| 2497 | ** [GLOB] operators.</dd>)^ |
| 2498 | ** |
| 2499 | ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> |
| 2500 | ** <dd>The maximum index number of any [parameter] in an SQL statement.)^ |
| 2501 | ** |
| 2502 | ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> |
| 2503 | ** <dd>The maximum depth of recursion for triggers.</dd>)^ |
| 2504 | ** </dl> |
| 2505 | */ |
| @@ -2535,16 +2567,11 @@ | |
| 2567 | ** |
| 2568 | ** <ol> |
| 2569 | ** <li> |
| 2570 | ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it |
| 2571 | ** always used to do, [sqlite3_step()] will automatically recompile the SQL |
| 2572 | ** statement and try to run it again. |
| 2573 | ** </li> |
| 2574 | ** |
| 2575 | ** <li> |
| 2576 | ** ^When an error occurs, [sqlite3_step()] will return one of the detailed |
| 2577 | ** [error codes] or [extended error codes]. ^The legacy behavior was that |
| @@ -2553,15 +2580,20 @@ | |
| 2580 | ** in order to find the underlying cause of the problem. With the "v2" prepare |
| 2581 | ** interfaces, the underlying reason for the error is returned immediately. |
| 2582 | ** </li> |
| 2583 | ** |
| 2584 | ** <li> |
| 2585 | ** ^If the specific value bound to [parameter | host parameter] in the |
| 2586 | ** WHERE clause might influence the choice of query plan for a statement, |
| 2587 | ** then the statement will be automatically recompiled, as if there had been |
| 2588 | ** a schema change, on the first [sqlite3_step()] call following any change |
| 2589 | ** to the [sqlite3_bind_text | bindings] of that [parameter]. |
| 2590 | ** ^The specific value of WHERE-clause [parameter] might influence the |
| 2591 | ** choice of query plan if the parameter is the left-hand side of a [LIKE] |
| 2592 | ** or [GLOB] operator or if the parameter is compared to an indexed column |
| 2593 | ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. |
| 2594 | ** the |
| 2595 | ** </li> |
| 2596 | ** </ol> |
| 2597 | */ |
| 2598 | SQLITE_API int sqlite3_prepare( |
| 2599 | sqlite3 *db, /* Database handle */ |
| @@ -2624,11 +2656,11 @@ | |
| 2656 | ** or if SQLite is run in one of reduced mutex modes |
| 2657 | ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] |
| 2658 | ** then there is no distinction between protected and unprotected |
| 2659 | ** sqlite3_value objects and they can be used interchangeably. However, |
| 2660 | ** for maximum code portability it is recommended that applications |
| 2661 | ** still make the distinction between protected and unprotected |
| 2662 | ** sqlite3_value objects even when not strictly required. |
| 2663 | ** |
| 2664 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 2665 | ** implementation of [application-defined SQL functions] are protected. |
| 2666 | ** ^The sqlite3_value object returned by |
| @@ -2819,10 +2851,12 @@ | |
| 2851 | ** CAPI3REF: Number Of Columns In A Result Set |
| 2852 | ** |
| 2853 | ** ^Return the number of columns in the result set returned by the |
| 2854 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 2855 | ** statement that does not return data (for example an [UPDATE]). |
| 2856 | ** |
| 2857 | ** See also: [sqlite3_data_count()] |
| 2858 | */ |
| 2859 | SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); |
| 2860 | |
| 2861 | /* |
| 2862 | ** CAPI3REF: Column Names In A Result Set |
| @@ -3009,12 +3043,18 @@ | |
| 3043 | SQLITE_API int sqlite3_step(sqlite3_stmt*); |
| 3044 | |
| 3045 | /* |
| 3046 | ** CAPI3REF: Number of columns in a result set |
| 3047 | ** |
| 3048 | ** ^The sqlite3_data_count(P) interface returns the number of columns in the |
| 3049 | ** current row of the result set of [prepared statement] P. |
| 3050 | ** ^If prepared statement P does not have results ready to return |
| 3051 | ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of |
| 3052 | ** interfaces) then sqlite3_data_count(P) returns 0. |
| 3053 | ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. |
| 3054 | ** |
| 3055 | ** See also: [sqlite3_column_count()] |
| 3056 | */ |
| 3057 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3058 | |
| 3059 | /* |
| 3060 | ** CAPI3REF: Fundamental Datatypes |
| @@ -3090,22 +3130,30 @@ | |
| 3130 | ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts |
| 3131 | ** the string to UTF-8 and then returns the number of bytes. |
| 3132 | ** ^If the result is a numeric value then sqlite3_column_bytes() uses |
| 3133 | ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns |
| 3134 | ** the number of bytes in that string. |
| 3135 | ** ^If the result is NULL, then sqlite3_column_bytes() returns zero. |
| 3136 | ** |
| 3137 | ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() |
| 3138 | ** routine returns the number of bytes in that BLOB or string. |
| 3139 | ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts |
| 3140 | ** the string to UTF-16 and then returns the number of bytes. |
| 3141 | ** ^If the result is a numeric value then sqlite3_column_bytes16() uses |
| 3142 | ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns |
| 3143 | ** the number of bytes in that string. |
| 3144 | ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. |
| 3145 | ** |
| 3146 | ** ^The values returned by [sqlite3_column_bytes()] and |
| 3147 | ** [sqlite3_column_bytes16()] do not include the zero terminators at the end |
| 3148 | ** of the string. ^For clarity: the values returned by |
| 3149 | ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of |
| 3150 | ** bytes in the string, not the number of characters. |
| 3151 | ** |
| 3152 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 3153 | ** even empty strings, are always zero terminated. ^The return |
| 3154 | ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. |
| 3155 | ** |
| 3156 | ** ^The object returned by [sqlite3_column_value()] is an |
| 3157 | ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object |
| 3158 | ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| 3159 | ** If the [unprotected sqlite3_value] object returned by |
| @@ -3146,14 +3194,14 @@ | |
| 3194 | ** and atof(). SQLite does not really use these functions. It has its |
| 3195 | ** own equivalent internal routines. The atoi() and atof() names are |
| 3196 | ** used in the table for brevity and because they are familiar to most |
| 3197 | ** C programmers. |
| 3198 | ** |
| 3199 | ** Note that when type conversions occur, pointers returned by prior |
| 3200 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 3201 | ** sqlite3_column_text16() may be invalidated. |
| 3202 | ** Type conversions and pointer invalidations might occur |
| 3203 | ** in the following cases: |
| 3204 | ** |
| 3205 | ** <ul> |
| 3206 | ** <li> The initial content is a BLOB and sqlite3_column_text() or |
| 3207 | ** sqlite3_column_text16() is called. A zero-terminator might |
| @@ -3162,26 +3210,26 @@ | |
| 3210 | ** sqlite3_column_text16() is called. The content must be converted |
| 3211 | ** to UTF-16.</li> |
| 3212 | ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or |
| 3213 | ** sqlite3_column_text() is called. The content must be converted |
| 3214 | ** to UTF-8.</li> |
| 3215 | ** </ul> |
| 3216 | ** |
| 3217 | ** ^Conversions between UTF-16be and UTF-16le are always done in place and do |
| 3218 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 3219 | ** that the prior pointer references will have been modified. Other kinds |
| 3220 | ** of conversion are done in place when it is possible, but sometimes they |
| 3221 | ** are not possible and in those cases prior pointers are invalidated. |
| 3222 | ** |
| 3223 | ** The safest and easiest to remember policy is to invoke these routines |
| 3224 | ** in one of the following ways: |
| 3225 | ** |
| 3226 | ** <ul> |
| 3227 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 3228 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| 3229 | ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> |
| 3230 | ** </ul> |
| 3231 | ** |
| 3232 | ** In other words, you should call sqlite3_column_text(), |
| 3233 | ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result |
| 3234 | ** into the desired format, then invoke sqlite3_column_bytes() or |
| 3235 | ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls |
| @@ -3215,21 +3263,30 @@ | |
| 3263 | |
| 3264 | /* |
| 3265 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 3266 | ** |
| 3267 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 3268 | ** ^If the most recent evaluation of the statement encountered no errors or |
| 3269 | ** or if the statement is never been evaluated, then sqlite3_finalize() returns |
| 3270 | ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then |
| 3271 | ** sqlite3_finalize(S) returns the appropriate [error code] or |
| 3272 | ** [extended error code]. |
| 3273 | ** |
| 3274 | ** ^The sqlite3_finalize(S) routine can be called at any point during |
| 3275 | ** the life cycle of [prepared statement] S: |
| 3276 | ** before statement S is ever evaluated, after |
| 3277 | ** one or more calls to [sqlite3_reset()], or after any call |
| 3278 | ** to [sqlite3_step()] regardless of whether or not the statement has |
| 3279 | ** completed execution. |
| 3280 | ** |
| 3281 | ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. |
| 3282 | ** |
| 3283 | ** The application must finalize every [prepared statement] in order to avoid |
| 3284 | ** resource leaks. It is a grievous error for the application to try to use |
| 3285 | ** a prepared statement after it has been finalized. Any use of a prepared |
| 3286 | ** statement after it has been finalized can result in undefined and |
| 3287 | ** undesirable behavior such as segfaults and heap corruption. |
| 3288 | */ |
| 3289 | SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 3290 | |
| 3291 | /* |
| 3292 | ** CAPI3REF: Reset A Prepared Statement Object |
| @@ -3261,40 +3318,42 @@ | |
| 3318 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 3319 | ** KEYWORDS: {function creation routines} |
| 3320 | ** KEYWORDS: {application-defined SQL function} |
| 3321 | ** KEYWORDS: {application-defined SQL functions} |
| 3322 | ** |
| 3323 | ** ^These functions (collectively known as "function creation routines") |
| 3324 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 3325 | ** of existing SQL functions or aggregates. The only differences between |
| 3326 | ** these routines are the text encoding expected for |
| 3327 | ** the the second parameter (the name of the function being created) |
| 3328 | ** and the presence or absence of a destructor callback for |
| 3329 | ** the application data pointer. |
| 3330 | ** |
| 3331 | ** ^The first parameter is the [database connection] to which the SQL |
| 3332 | ** function is to be added. ^If an application uses more than one database |
| 3333 | ** connection then application-defined SQL functions must be added |
| 3334 | ** to each database connection separately. |
| 3335 | ** |
| 3336 | ** ^The second parameter is the name of the SQL function to be created or |
| 3337 | ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 |
| 3338 | ** representation, exclusive of the zero-terminator. ^Note that the name |
| 3339 | ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. |
| 3340 | ** ^Any attempt to create a function with a longer name |
| 3341 | ** will result in [SQLITE_MISUSE] being returned. |
| 3342 | ** |
| 3343 | ** ^The third parameter (nArg) |
| 3344 | ** is the number of arguments that the SQL function or |
| 3345 | ** aggregate takes. ^If this parameter is -1, then the SQL function or |
| 3346 | ** aggregate may take any number of arguments between 0 and the limit |
| 3347 | ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third |
| 3348 | ** parameter is less than -1 or greater than 127 then the behavior is |
| 3349 | ** undefined. |
| 3350 | ** |
| 3351 | ** ^The fourth parameter, eTextRep, specifies what |
| 3352 | ** [SQLITE_UTF8 | text encoding] this SQL function prefers for |
| 3353 | ** its parameters. Every SQL function implementation must be able to work |
| 3354 | ** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be |
| 3355 | ** more efficient with one encoding than another. ^An application may |
| 3356 | ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple |
| 3357 | ** times with the same function but with different values of eTextRep. |
| 3358 | ** ^When multiple implementations of the same function are available, SQLite |
| 3359 | ** will pick the one that involves the least amount of data conversion. |
| @@ -3302,17 +3361,25 @@ | |
| 3361 | ** encoding is used, then the fourth argument should be [SQLITE_ANY]. |
| 3362 | ** |
| 3363 | ** ^(The fifth parameter is an arbitrary pointer. The implementation of the |
| 3364 | ** function can gain access to this pointer using [sqlite3_user_data()].)^ |
| 3365 | ** |
| 3366 | ** ^The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are |
| 3367 | ** pointers to C-language functions that implement the SQL function or |
| 3368 | ** aggregate. ^A scalar SQL function requires an implementation of the xFunc |
| 3369 | ** callback only; NULL pointers must be passed as the xStep and xFinal |
| 3370 | ** parameters. ^An aggregate SQL function requires an implementation of xStep |
| 3371 | ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing |
| 3372 | ** SQL function or aggregate, pass NULL poiners for all three function |
| 3373 | ** callbacks. |
| 3374 | ** |
| 3375 | ** ^If the tenth parameter to sqlite3_create_function_v2() is not NULL, |
| 3376 | ** then it is invoked when the function is deleted, either by being |
| 3377 | ** overloaded or when the database connection closes. |
| 3378 | ** ^When the destructure callback of the tenth parameter is invoked, it |
| 3379 | ** is passed a single argument which is a copy of the pointer which was |
| 3380 | ** the fifth parameter to sqlite3_create_function_v2(). |
| 3381 | ** |
| 3382 | ** ^It is permitted to register multiple implementations of the same |
| 3383 | ** functions with the same name but with either differing numbers of |
| 3384 | ** arguments or differing preferred text encodings. ^SQLite will use |
| 3385 | ** the implementation that most closely matches the way in which the |
| @@ -3324,15 +3391,10 @@ | |
| 3391 | ** ^A function where the encoding difference is between UTF16le and UTF16be |
| 3392 | ** is a closer match than a function where the encoding difference is |
| 3393 | ** between UTF8 and UTF16. |
| 3394 | ** |
| 3395 | ** ^Built-in functions may be overloaded by new application-defined functions. |
| 3396 | ** |
| 3397 | ** ^An application-defined function is permitted to call other |
| 3398 | ** SQLite interfaces. However, such calls must not |
| 3399 | ** close the database connection nor finalize or reset the prepared |
| 3400 | ** statement in which the function is running. |
| @@ -3354,10 +3416,21 @@ | |
| 3416 | int eTextRep, |
| 3417 | void *pApp, |
| 3418 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3419 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3420 | void (*xFinal)(sqlite3_context*) |
| 3421 | ); |
| 3422 | SQLITE_API int sqlite3_create_function_v2( |
| 3423 | sqlite3 *db, |
| 3424 | const char *zFunctionName, |
| 3425 | int nArg, |
| 3426 | int eTextRep, |
| 3427 | void *pApp, |
| 3428 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 3429 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 3430 | void (*xFinal)(sqlite3_context*), |
| 3431 | void(*xDestroy)(void*) |
| 3432 | ); |
| 3433 | |
| 3434 | /* |
| 3435 | ** CAPI3REF: Text Encodings |
| 3436 | ** |
| @@ -3701,73 +3774,97 @@ | |
| 3774 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 3775 | |
| 3776 | /* |
| 3777 | ** CAPI3REF: Define New Collating Sequences |
| 3778 | ** |
| 3779 | ** ^These functions add, remove, or modify a [collation] associated |
| 3780 | ** with the [database connection] specified as the first argument. |
| 3781 | ** |
| 3782 | ** ^The name of the collation is a UTF-8 string |
| 3783 | ** for sqlite3_create_collation() and sqlite3_create_collation_v2() |
| 3784 | ** and a UTF-16 string in native byte order for sqlite3_create_collation16(). |
| 3785 | ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are |
| 3786 | ** considered to be the same name. |
| 3787 | ** |
| 3788 | ** ^(The third argument (eTextRep) must be one of the constants: |
| 3789 | ** <ul> |
| 3790 | ** <li> [SQLITE_UTF8], |
| 3791 | ** <li> [SQLITE_UTF16LE], |
| 3792 | ** <li> [SQLITE_UTF16BE], |
| 3793 | ** <li> [SQLITE_UTF16], or |
| 3794 | ** <li> [SQLITE_UTF16_ALIGNED]. |
| 3795 | ** </ul>)^ |
| 3796 | ** ^The eTextRep argument determines the encoding of strings passed |
| 3797 | ** to the collating function callback, xCallback. |
| 3798 | ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep |
| 3799 | ** force strings to be UTF16 with native byte order. |
| 3800 | ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin |
| 3801 | ** on an even byte address. |
| 3802 | ** |
| 3803 | ** ^The fourth argument, pArg, is a application data pointer that is passed |
| 3804 | ** through as the first argument to the collating function callback. |
| 3805 | ** |
| 3806 | ** ^The fifth argument, xCallback, is a pointer to the collating function. |
| 3807 | ** ^Multiple collating functions can be registered using the same name but |
| 3808 | ** with different eTextRep parameters and SQLite will use whichever |
| 3809 | ** function requires the least amount of data transformation. |
| 3810 | ** ^If the xCallback argument is NULL then the collating function is |
| 3811 | ** deleted. ^When all collating functions having the same name are deleted, |
| 3812 | ** that collation is no longer usable. |
| 3813 | ** |
| 3814 | ** ^The collating function callback is invoked with a copy of the pArg |
| 3815 | ** application data pointer and with two strings in the encoding specified |
| 3816 | ** by the eTextRep argument. The collating function must return an |
| 3817 | ** integer that is negative, zero, or positive |
| 3818 | ** if the first string is less than, equal to, or greater than the second, |
| 3819 | ** respectively. A collating function must alway return the same answer |
| 3820 | ** given the same inputs. If two or more collating functions are registered |
| 3821 | ** to the same collation name (using different eTextRep values) then all |
| 3822 | ** must give an equivalent answer when invoked with equivalent strings. |
| 3823 | ** The collating function must obey the following properties for all |
| 3824 | ** strings A, B, and C: |
| 3825 | ** |
| 3826 | ** <ol> |
| 3827 | ** <li> If A==B then B==A. |
| 3828 | ** <li> If A==B and B==C then A==C. |
| 3829 | ** <li> If A<B THEN B>A. |
| 3830 | ** <li> If A<B and B<C then A<C. |
| 3831 | ** </ol> |
| 3832 | ** |
| 3833 | ** If a collating function fails any of the above constraints and that |
| 3834 | ** collating function is registered and used, then the behavior of SQLite |
| 3835 | ** is undefined. |
| 3836 | ** |
| 3837 | ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() |
| 3838 | ** with the addition that the xDestroy callback is invoked on pArg when |
| 3839 | ** the collating function is deleted. |
| 3840 | ** ^Collating functions are deleted when they are overridden by later |
| 3841 | ** calls to the collation creation functions or when the |
| 3842 | ** [database connection] is closed using [sqlite3_close()]. |
| 3843 | ** |
| 3844 | ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. |
| 3845 | */ |
| 3846 | SQLITE_API int sqlite3_create_collation( |
| 3847 | sqlite3*, |
| 3848 | const char *zName, |
| 3849 | int eTextRep, |
| 3850 | void *pArg, |
| 3851 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3852 | ); |
| 3853 | SQLITE_API int sqlite3_create_collation_v2( |
| 3854 | sqlite3*, |
| 3855 | const char *zName, |
| 3856 | int eTextRep, |
| 3857 | void *pArg, |
| 3858 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 3859 | void(*xDestroy)(void*) |
| 3860 | ); |
| 3861 | SQLITE_API int sqlite3_create_collation16( |
| 3862 | sqlite3*, |
| 3863 | const void *zName, |
| 3864 | int eTextRep, |
| 3865 | void *pArg, |
| 3866 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 3867 | ); |
| 3868 | |
| 3869 | /* |
| 3870 | ** CAPI3REF: Collation Needed Callbacks |
| @@ -3852,20 +3949,23 @@ | |
| 3949 | #endif |
| 3950 | |
| 3951 | /* |
| 3952 | ** CAPI3REF: Suspend Execution For A Short Time |
| 3953 | ** |
| 3954 | ** The sqlite3_sleep() function causes the current thread to suspend execution |
| 3955 | ** for at least a number of milliseconds specified in its parameter. |
| 3956 | ** |
| 3957 | ** If the operating system does not support sleep requests with |
| 3958 | ** millisecond time resolution, then the time will be rounded up to |
| 3959 | ** the nearest second. The number of milliseconds of sleep actually |
| 3960 | ** requested from the operating system is returned. |
| 3961 | ** |
| 3962 | ** ^SQLite implements this interface by calling the xSleep() |
| 3963 | ** method of the default [sqlite3_vfs] object. If the xSleep() method |
| 3964 | ** of the default VFS is not implemented correctly, or not implemented at |
| 3965 | ** all, then the behavior of sqlite3_sleep() may deviate from the description |
| 3966 | ** in the previous paragraphs. |
| 3967 | */ |
| 3968 | SQLITE_API int sqlite3_sleep(int); |
| 3969 | |
| 3970 | /* |
| 3971 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| @@ -4083,44 +4183,77 @@ | |
| 4183 | ** of heap memory by deallocating non-essential memory allocations |
| 4184 | ** held by the database library. Memory used to cache database |
| 4185 | ** pages to improve performance is an example of non-essential memory. |
| 4186 | ** ^sqlite3_release_memory() returns the number of bytes actually freed, |
| 4187 | ** which might be more or less than the amount requested. |
| 4188 | ** ^The sqlite3_release_memory() routine is a no-op returning zero |
| 4189 | ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. |
| 4190 | */ |
| 4191 | SQLITE_API int sqlite3_release_memory(int); |
| 4192 | |
| 4193 | /* |
| 4194 | ** CAPI3REF: Impose A Limit On Heap Size |
| 4195 | ** |
| 4196 | ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the |
| 4197 | ** soft limit on the amount of heap memory that may be allocated by SQLite. |
| 4198 | ** ^SQLite strives to keep heap memory utilization below the soft heap |
| 4199 | ** limit by reducing the number of pages held in the page cache |
| 4200 | ** as heap memory usages approaches the limit. |
| 4201 | ** ^The soft heap limit is "soft" because even though SQLite strives to stay |
| 4202 | ** below the limit, it will exceed the limit rather than generate |
| 4203 | ** an [SQLITE_NOMEM] error. In other words, the soft heap limit |
| 4204 | ** is advisory only. |
| 4205 | ** |
| 4206 | ** ^The return value from sqlite3_soft_heap_limit64() is the size of |
| 4207 | ** the soft heap limit prior to the call. ^If the argument N is negative |
| 4208 | ** then no change is made to the soft heap limit. Hence, the current |
| 4209 | ** size of the soft heap limit can be determined by invoking |
| 4210 | ** sqlite3_soft_heap_limit64() with a negative argument. |
| 4211 | ** |
| 4212 | ** ^If the argument N is zero then the soft heap limit is disabled. |
| 4213 | ** |
| 4214 | ** ^(The soft heap limit is not enforced in the current implementation |
| 4215 | ** if one or more of following conditions are true: |
| 4216 | ** |
| 4217 | ** <ul> |
| 4218 | ** <li> The soft heap limit is set to zero. |
| 4219 | ** <li> Memory accounting is disabled using a combination of the |
| 4220 | ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and |
| 4221 | ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. |
| 4222 | ** <li> An alternative page cache implementation is specifed using |
| 4223 | ** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...). |
| 4224 | ** <li> The page cache allocates from its own memory pool supplied |
| 4225 | ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than |
| 4226 | ** from the heap. |
| 4227 | ** </ul>)^ |
| 4228 | ** |
| 4229 | ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced |
| 4230 | ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] |
| 4231 | ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], |
| 4232 | ** the soft heap limit is enforced on every memory allocation. Without |
| 4233 | ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced |
| 4234 | ** when memory is allocated by the page cache. Testing suggests that because |
| 4235 | ** the page cache is the predominate memory user in SQLite, most |
| 4236 | ** applications will achieve adequate soft heap limit enforcement without |
| 4237 | ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. |
| 4238 | ** |
| 4239 | ** The circumstances under which SQLite will enforce the soft heap limit may |
| 4240 | ** changes in future releases of SQLite. |
| 4241 | */ |
| 4242 | SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); |
| 4243 | |
| 4244 | /* |
| 4245 | ** CAPI3REF: Deprecated Soft Heap Limit Interface |
| 4246 | ** DEPRECATED |
| 4247 | ** |
| 4248 | ** This is a deprecated version of the [sqlite3_soft_heap_limit64()] |
| 4249 | ** interface. This routine is provided for historical compatibility |
| 4250 | ** only. All new applications should use the |
| 4251 | ** [sqlite3_soft_heap_limit64()] interface rather than this one. |
| 4252 | */ |
| 4253 | SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); |
| 4254 | |
| 4255 | |
| 4256 | /* |
| 4257 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 4258 | ** |
| 4259 | ** ^This routine returns metadata about a specific column of a specific |
| @@ -4240,38 +4373,51 @@ | |
| 4373 | ** it back off again. |
| 4374 | */ |
| 4375 | SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); |
| 4376 | |
| 4377 | /* |
| 4378 | ** CAPI3REF: Automatically Load Statically Linked Extensions |
| 4379 | ** |
| 4380 | ** ^This interface causes the xEntryPoint() function to be invoked for |
| 4381 | ** each new [database connection] that is created. The idea here is that |
| 4382 | ** xEntryPoint() is the entry point for a statically linked SQLite extension |
| 4383 | ** that is to be automatically loaded into all new database connections. |
| 4384 | ** |
| 4385 | ** ^(Even though the function prototype shows that xEntryPoint() takes |
| 4386 | ** no arguments and returns void, SQLite invokes xEntryPoint() with three |
| 4387 | ** arguments and expects and integer result as if the signature of the |
| 4388 | ** entry point where as follows: |
| 4389 | ** |
| 4390 | ** <blockquote><pre> |
| 4391 | ** int xEntryPoint( |
| 4392 | ** sqlite3 *db, |
| 4393 | ** const char **pzErrMsg, |
| 4394 | ** const struct sqlite3_api_routines *pThunk |
| 4395 | ** ); |
| 4396 | ** </pre></blockquote>)^ |
| 4397 | ** |
| 4398 | ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg |
| 4399 | ** point to an appropriate error message (obtained from [sqlite3_mprintf()]) |
| 4400 | ** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg |
| 4401 | ** is NULL before calling the xEntryPoint(). ^SQLite will invoke |
| 4402 | ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any |
| 4403 | ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], |
| 4404 | ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. |
| 4405 | ** |
| 4406 | ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already |
| 4407 | ** on the list of automatic extensions is a harmless no-op. ^No entry point |
| 4408 | ** will be called more than once for each database connection that is opened. |
| 4409 | ** |
| 4410 | ** See also: [sqlite3_reset_auto_extension()]. |
| 4411 | */ |
| 4412 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 4413 | |
| 4414 | /* |
| 4415 | ** CAPI3REF: Reset Automatic Extension Loading |
| 4416 | ** |
| 4417 | ** ^This interface disables all automatic extensions previously |
| 4418 | ** registered using [sqlite3_auto_extension()]. |
| 4419 | */ |
| 4420 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4421 | |
| 4422 | /* |
| 4423 | ** The interface to the virtual-table mechanism is currently considered |
| @@ -4906,11 +5052,11 @@ | |
| 5052 | ** output variable when querying the system for the current mutex |
| 5053 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| 5054 | ** |
| 5055 | ** ^The xMutexInit method defined by this structure is invoked as |
| 5056 | ** part of system initialization by the sqlite3_initialize() function. |
| 5057 | ** ^The xMutexInit routine is called by SQLite exactly once for each |
| 5058 | ** effective call to [sqlite3_initialize()]. |
| 5059 | ** |
| 5060 | ** ^The xMutexEnd method defined by this structure is invoked as |
| 5061 | ** part of system shutdown by the sqlite3_shutdown() function. The |
| 5062 | ** implementation of this method is expected to release all outstanding |
| @@ -5103,11 +5249,12 @@ | |
| 5249 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 5250 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 5251 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 5252 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5253 | #define SQLITE_TESTCTRL_PGHDRSZ 17 |
| 5254 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 18 |
| 5255 | #define SQLITE_TESTCTRL_LAST 18 |
| 5256 | |
| 5257 | /* |
| 5258 | ** CAPI3REF: SQLite Runtime Status |
| 5259 | ** |
| 5260 | ** ^This interface is used to retrieve runtime status information |
| @@ -5122,11 +5269,11 @@ | |
| 5269 | ** value. For those parameters |
| 5270 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 5271 | ** ^(Other parameters record only the highwater mark and not the current |
| 5272 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 5273 | ** |
| 5274 | ** ^The sqlite3_status() routine returns SQLITE_OK on success and a |
| 5275 | ** non-zero [error code] on failure. |
| 5276 | ** |
| 5277 | ** This routine is threadsafe but is not atomic. This routine can be |
| 5278 | ** called while other threads are running the same or different SQLite |
| 5279 | ** interfaces. However the values returned in *pCurrent and |
| @@ -5172,11 +5319,11 @@ | |
| 5319 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5320 | ** value returned is in pages, not in bytes.</dd>)^ |
| 5321 | ** |
| 5322 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> |
| 5323 | ** <dd>This parameter returns the number of bytes of page cache |
| 5324 | ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] |
| 5325 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The |
| 5326 | ** returned value includes allocations that overflowed because they |
| 5327 | ** where too large (they were larger than the "sz" parameter to |
| 5328 | ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because |
| 5329 | ** no space was left in the page cache.</dd>)^ |
| @@ -5195,11 +5342,11 @@ | |
| 5342 | ** outstanding at time, this parameter also reports the number of threads |
| 5343 | ** using scratch memory at the same time.</dd>)^ |
| 5344 | ** |
| 5345 | ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> |
| 5346 | ** <dd>This parameter returns the number of bytes of scratch memory |
| 5347 | ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] |
| 5348 | ** buffer and where forced to overflow to [sqlite3_malloc()]. The values |
| 5349 | ** returned include overflows because the requested allocation was too |
| 5350 | ** larger (that is, because the requested allocation was larger than the |
| 5351 | ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer |
| 5352 | ** slots were available. |
| @@ -5243,10 +5390,13 @@ | |
| 5390 | ** |
| 5391 | ** ^The current value of the requested parameter is written into *pCur |
| 5392 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5393 | ** the resetFlg is true, then the highest instantaneous value is |
| 5394 | ** reset back down to the current value. |
| 5395 | ** |
| 5396 | ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a |
| 5397 | ** non-zero [error code] on failure. |
| 5398 | ** |
| 5399 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5400 | */ |
| 5401 | SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5402 | |
| @@ -5370,122 +5520,134 @@ | |
| 5520 | ** CAPI3REF: Application Defined Page Cache. |
| 5521 | ** KEYWORDS: {page cache} |
| 5522 | ** |
| 5523 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5524 | ** register an alternative page cache implementation by passing in an |
| 5525 | ** instance of the sqlite3_pcache_methods structure.)^ |
| 5526 | ** In many applications, most of the heap memory allocated by |
| 5527 | ** SQLite is used for the page cache. |
| 5528 | ** By implementing a |
| 5529 | ** custom page cache using this API, an application can better control |
| 5530 | ** the amount of memory consumed by SQLite, the way in which |
| 5531 | ** that memory is allocated and released, and the policies used to |
| 5532 | ** determine exactly which parts of a database file are cached and for |
| 5533 | ** how long. |
| 5534 | ** |
| 5535 | ** The alternative page cache mechanism is an |
| 5536 | ** extreme measure that is only needed by the most demanding applications. |
| 5537 | ** The built-in page cache is recommended for most uses. |
| 5538 | ** |
| 5539 | ** ^(The contents of the sqlite3_pcache_methods structure are copied to an |
| 5540 | ** internal buffer by SQLite within the call to [sqlite3_config]. Hence |
| 5541 | ** the application may discard the parameter after the call to |
| 5542 | ** [sqlite3_config()] returns.)^ |
| 5543 | ** |
| 5544 | ** ^(The xInit() method is called once for each effective |
| 5545 | ** call to [sqlite3_initialize()])^ |
| 5546 | ** (usually only once during the lifetime of the process). ^(The xInit() |
| 5547 | ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ |
| 5548 | ** The intent of the xInit() method is to set up global data structures |
| 5549 | ** required by the custom page cache implementation. |
| 5550 | ** ^(If the xInit() method is NULL, then the |
| 5551 | ** built-in default page cache is used instead of the application defined |
| 5552 | ** page cache.)^ |
| 5553 | ** |
| 5554 | ** ^The xShutdown() method is called by [sqlite3_shutdown()]. |
| 5555 | ** It can be used to clean up |
| 5556 | ** any outstanding resources before process shutdown, if required. |
| 5557 | ** ^The xShutdown() method may be NULL. |
| 5558 | ** |
| 5559 | ** ^SQLite automatically serializes calls to the xInit method, |
| 5560 | ** so the xInit method need not be threadsafe. ^The |
| 5561 | ** xShutdown method is only called from [sqlite3_shutdown()] so it does |
| 5562 | ** not need to be threadsafe either. All other methods must be threadsafe |
| 5563 | ** in multithreaded applications. |
| 5564 | ** |
| 5565 | ** ^SQLite will never invoke xInit() more than once without an intervening |
| 5566 | ** call to xShutdown(). |
| 5567 | ** |
| 5568 | ** ^SQLite invokes the xCreate() method to construct a new cache instance. |
| 5569 | ** SQLite will typically create one cache instance for each open database file, |
| 5570 | ** though this is not guaranteed. ^The |
| 5571 | ** first parameter, szPage, is the size in bytes of the pages that must |
| 5572 | ** be allocated by the cache. ^szPage will not be a power of two. ^szPage |
| 5573 | ** will the page size of the database file that is to be cached plus an |
| 5574 | ** increment (here called "R") of about 100 or 200. SQLite will use the |
| 5575 | ** extra R bytes on each page to store metadata about the underlying |
| 5576 | ** database page on disk. The value of R depends |
| 5577 | ** on the SQLite version, the target platform, and how SQLite was compiled. |
| 5578 | ** ^R is constant for a particular build of SQLite. ^The second argument to |
| 5579 | ** xCreate(), bPurgeable, is true if the cache being created will |
| 5580 | ** be used to cache database pages of a file stored on disk, or |
| 5581 | ** false if it is used for an in-memory database. The cache implementation |
| 5582 | ** does not have to do anything special based with the value of bPurgeable; |
| 5583 | ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will |
| 5584 | ** never invoke xUnpin() except to deliberately delete a page. |
| 5585 | ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to |
| 5586 | ** false will always have the "discard" flag set to true. |
| 5587 | ** ^Hence, a cache created with bPurgeable false will |
| 5588 | ** never contain any unpinned pages. |
| 5589 | ** |
| 5590 | ** ^(The xCachesize() method may be called at any time by SQLite to set the |
| 5591 | ** suggested maximum cache-size (number of pages stored by) the cache |
| 5592 | ** instance passed as the first argument. This is the value configured using |
| 5593 | ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable |
| 5594 | ** parameter, the implementation is not required to do anything with this |
| 5595 | ** value; it is advisory only. |
| 5596 | ** |
| 5597 | ** The xPagecount() method must return the number of pages currently |
| 5598 | ** stored in the cache, both pinned and unpinned. |
| 5599 | ** |
| 5600 | ** The xFetch() method locates a page in the cache and returns a pointer to |
| 5601 | ** the page, or a NULL pointer. |
| 5602 | ** A "page", in this context, means a buffer of szPage bytes aligned at an |
| 5603 | ** 8-byte boundary. The page to be fetched is determined by the key. ^The |
| 5604 | ** mimimum key value is 1. After it has been retrieved using xFetch, the page |
| 5605 | ** is considered to be "pinned". |
| 5606 | ** |
| 5607 | ** If the requested page is already in the page cache, then the page cache |
| 5608 | ** implementation must return a pointer to the page buffer with its content |
| 5609 | ** intact. If the requested page is not already in the cache, then the |
| 5610 | ** behavior of the cache implementation should use the value of the createFlag |
| 5611 | ** parameter to help it determined what action to take: |
| 5612 | ** |
| 5613 | ** <table border=1 width=85% align=center> |
| 5614 | ** <tr><th> createFlag <th> Behaviour when page is not already in cache |
| 5615 | ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. |
| 5616 | ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. |
| 5617 | ** Otherwise return NULL. |
| 5618 | ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return |
| 5619 | ** NULL if allocating a new page is effectively impossible. |
| 5620 | ** </table> |
| 5621 | ** |
| 5622 | ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite |
| 5623 | ** will only use a createFlag of 2 after a prior call with a createFlag of 1 |
| 5624 | ** failed.)^ In between the to xFetch() calls, SQLite may |
| 5625 | ** attempt to unpin one or more cache pages by spilling the content of |
| 5626 | ** pinned pages to disk and synching the operating system disk cache. |
| 5627 | ** |
| 5628 | ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page |
| 5629 | ** as its second argument. If the third parameter, discard, is non-zero, |
| 5630 | ** then the page must be evicted from the cache. |
| 5631 | ** ^If the discard parameter is |
| 5632 | ** zero, then the page may be discarded or retained at the discretion of |
| 5633 | ** page cache implementation. ^The page cache implementation |
| 5634 | ** may choose to evict unpinned pages at any time. |
| 5635 | ** |
| 5636 | ** The cache must not perform any reference counting. A single |
| 5637 | ** call to xUnpin() unpins the page regardless of the number of prior calls |
| 5638 | ** to xFetch(). |
| 5639 | ** |
| 5640 | ** The xRekey() method is used to change the key value associated with the |
| 5641 | ** page passed as the second argument. If the cache |
| 5642 | ** previously contains an entry associated with newKey, it must be |
| 5643 | ** discarded. ^Any prior cache entry associated with newKey is guaranteed not |
| 5644 | ** to be pinned. |
| 5645 | ** |
| 5646 | ** When SQLite calls the xTruncate() method, the cache must discard all |
| 5647 | ** existing cache entries with page numbers (keys) greater than or equal |
| 5648 | ** to the value of the iLimit parameter passed to xTruncate(). If any |
| 5649 | ** of these pages are pinned, they are implicitly unpinned, meaning that |
| 5650 | ** they can be safely discarded. |
| 5651 | ** |
| 5652 | ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). |
| 5653 | ** All resources associated with the specified cache should be freed. ^After |
| @@ -5959,5 +6121,61 @@ | |
| 6121 | #ifdef __cplusplus |
| 6122 | } /* End of the 'extern "C"' block */ |
| 6123 | #endif |
| 6124 | #endif |
| 6125 | |
| 6126 | /* |
| 6127 | ** 2010 August 30 |
| 6128 | ** |
| 6129 | ** The author disclaims copyright to this source code. In place of |
| 6130 | ** a legal notice, here is a blessing: |
| 6131 | ** |
| 6132 | ** May you do good and not evil. |
| 6133 | ** May you find forgiveness for yourself and forgive others. |
| 6134 | ** May you share freely, never taking more than you give. |
| 6135 | ** |
| 6136 | ************************************************************************* |
| 6137 | */ |
| 6138 | |
| 6139 | #ifndef _SQLITE3RTREE_H_ |
| 6140 | #define _SQLITE3RTREE_H_ |
| 6141 | |
| 6142 | |
| 6143 | #ifdef __cplusplus |
| 6144 | extern "C" { |
| 6145 | #endif |
| 6146 | |
| 6147 | typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; |
| 6148 | |
| 6149 | /* |
| 6150 | ** Register a geometry callback named zGeom that can be used as part of an |
| 6151 | ** R-Tree geometry query as follows: |
| 6152 | ** |
| 6153 | ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...) |
| 6154 | */ |
| 6155 | SQLITE_API int sqlite3_rtree_geometry_callback( |
| 6156 | sqlite3 *db, |
| 6157 | const char *zGeom, |
| 6158 | int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), |
| 6159 | void *pContext |
| 6160 | ); |
| 6161 | |
| 6162 | |
| 6163 | /* |
| 6164 | ** A pointer to a structure of the following type is passed as the first |
| 6165 | ** argument to callbacks registered using rtree_geometry_callback(). |
| 6166 | */ |
| 6167 | struct sqlite3_rtree_geometry { |
| 6168 | void *pContext; /* Copy of pContext passed to s_r_g_c() */ |
| 6169 | int nParam; /* Size of array aParam[] */ |
| 6170 | double *aParam; /* Parameters passed to SQL geom function */ |
| 6171 | void *pUser; /* Callback implementation user data */ |
| 6172 | void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ |
| 6173 | }; |
| 6174 | |
| 6175 | |
| 6176 | #ifdef __cplusplus |
| 6177 | } /* end of the 'extern "C"' block */ |
| 6178 | #endif |
| 6179 | |
| 6180 | #endif /* ifndef _SQLITE3RTREE_H_ */ |
| 6181 | |
| 6182 |