| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.48.0. By combining all the individual C code files into this |
| 3 | +** version 3.50.0. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** e6c30ee52c5cdc193804cec63374d558b45e with changes in files: |
| 21 | +** e6784af6d50f715338ae3218fc8ba1b89488 with changes in files: |
| 22 | 22 | ** |
| 23 | 23 | ** |
| 24 | 24 | */ |
| 25 | 25 | #ifndef SQLITE_AMALGAMATION |
| 26 | 26 | #define SQLITE_CORE 1 |
| | @@ -463,13 +463,13 @@ |
| 463 | 463 | ** |
| 464 | 464 | ** See also: [sqlite3_libversion()], |
| 465 | 465 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 466 | 466 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 467 | 467 | */ |
| 468 | | -#define SQLITE_VERSION "3.48.0" |
| 469 | | -#define SQLITE_VERSION_NUMBER 3048000 |
| 470 | | -#define SQLITE_SOURCE_ID "2024-12-19 19:02:09 e6c30ee52c5cdc193804cec63374d558b45e4d67fc6bde58771ca78485ca0acf" |
| 468 | +#define SQLITE_VERSION "3.50.0" |
| 469 | +#define SQLITE_VERSION_NUMBER 3050000 |
| 470 | +#define SQLITE_SOURCE_ID "2025-02-25 18:10:47 e6784af6d50f715338ae3218fc8ba1b894883c27d797f0b7fd2625cac17d9cd7" |
| 471 | 471 | |
| 472 | 472 | /* |
| 473 | 473 | ** CAPI3REF: Run-Time Library Version Numbers |
| 474 | 474 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 475 | 475 | ** |
| | @@ -1479,10 +1479,16 @@ |
| 1479 | 1479 | ** to block for up to M milliseconds before failing when attempting to |
| 1480 | 1480 | ** obtain a file lock using the xLock or xShmLock methods of the VFS. |
| 1481 | 1481 | ** The parameter is a pointer to a 32-bit signed integer that contains |
| 1482 | 1482 | ** the value that M is to be set to. Before returning, the 32-bit signed |
| 1483 | 1483 | ** integer is overwritten with the previous value of M. |
| 1484 | +** |
| 1485 | +** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]] |
| 1486 | +** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the |
| 1487 | +** VFS to block when taking a SHARED lock to connect to a wal mode database. |
| 1488 | +** This is used to implement the functionality associated with |
| 1489 | +** SQLITE_SETLK_BLOCK_ON_CONNECT. |
| 1484 | 1490 | ** |
| 1485 | 1491 | ** <li>[[SQLITE_FCNTL_DATA_VERSION]] |
| 1486 | 1492 | ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to |
| 1487 | 1493 | ** a database file. The argument is a pointer to a 32-bit unsigned integer. |
| 1488 | 1494 | ** The "data version" for the pager is written into the pointer. The |
| | @@ -1576,10 +1582,11 @@ |
| 1576 | 1582 | #define SQLITE_FCNTL_CKPT_START 39 |
| 1577 | 1583 | #define SQLITE_FCNTL_EXTERNAL_READER 40 |
| 1578 | 1584 | #define SQLITE_FCNTL_CKSM_FILE 41 |
| 1579 | 1585 | #define SQLITE_FCNTL_RESET_CACHE 42 |
| 1580 | 1586 | #define SQLITE_FCNTL_NULL_IO 43 |
| 1587 | +#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44 |
| 1581 | 1588 | |
| 1582 | 1589 | /* deprecated names */ |
| 1583 | 1590 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 1584 | 1591 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 1585 | 1592 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -2306,17 +2313,20 @@ |
| 2306 | 2313 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 2307 | 2314 | ** return [SQLITE_ERROR].</dd> |
| 2308 | 2315 | ** |
| 2309 | 2316 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 2310 | 2317 | ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine |
| 2311 | | -** the default size of lookaside memory on each [database connection]. |
| 2318 | +** the default size of [lookaside memory] on each [database connection]. |
| 2312 | 2319 | ** The first argument is the |
| 2313 | | -** size of each lookaside buffer slot and the second is the number of |
| 2314 | | -** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE |
| 2315 | | -** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 2316 | | -** option to [sqlite3_db_config()] can be used to change the lookaside |
| 2317 | | -** configuration on individual connections.)^ </dd> |
| 2320 | +** size of each lookaside buffer slot ("sz") and the second is the number of |
| 2321 | +** slots allocated to each database connection ("cnt").)^ |
| 2322 | +** ^(SQLITE_CONFIG_LOOKASIDE sets the <i>default</i> lookaside size. |
| 2323 | +** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can |
| 2324 | +** be used to change the lookaside configuration on individual connections.)^ |
| 2325 | +** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the |
| 2326 | +** default lookaside configuration at compile-time. |
| 2327 | +** </dd> |
| 2318 | 2328 | ** |
| 2319 | 2329 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 2320 | 2330 | ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is |
| 2321 | 2331 | ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies |
| 2322 | 2332 | ** the interface to a custom page cache implementation.)^ |
| | @@ -2528,11 +2538,19 @@ |
| 2528 | 2538 | |
| 2529 | 2539 | /* |
| 2530 | 2540 | ** CAPI3REF: Database Connection Configuration Options |
| 2531 | 2541 | ** |
| 2532 | 2542 | ** These constants are the available integer configuration options that |
| 2533 | | -** can be passed as the second argument to the [sqlite3_db_config()] interface. |
| 2543 | +** can be passed as the second parameter to the [sqlite3_db_config()] interface. |
| 2544 | +** |
| 2545 | +** The [sqlite3_db_config()] interface is a var-args functions. It takes a |
| 2546 | +** variable number of parameters, though always at least two. The number of |
| 2547 | +** parameters passed into sqlite3_db_config() depends on which of these |
| 2548 | +** constants is given as the second parameter. This documentation page |
| 2549 | +** refers to parameters beyond the second as "arguments". Thus, when this |
| 2550 | +** page says "the N-th argument" it means "the N-th parameter past the |
| 2551 | +** configuration option" or "the (N+2)-th parameter to sqlite3_db_config()". |
| 2534 | 2552 | ** |
| 2535 | 2553 | ** New configuration options may be added in future releases of SQLite. |
| 2536 | 2554 | ** Existing configuration options might be discontinued. Applications |
| 2537 | 2555 | ** should check the return code from [sqlite3_db_config()] to make sure that |
| 2538 | 2556 | ** the call worked. ^The [sqlite3_db_config()] interface will return a |
| | @@ -2540,35 +2558,61 @@ |
| 2540 | 2558 | ** is invoked. |
| 2541 | 2559 | ** |
| 2542 | 2560 | ** <dl> |
| 2543 | 2561 | ** [[SQLITE_DBCONFIG_LOOKASIDE]] |
| 2544 | 2562 | ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt> |
| 2545 | | -** <dd> ^This option takes three additional arguments that determine the |
| 2546 | | -** [lookaside memory allocator] configuration for the [database connection]. |
| 2547 | | -** ^The first argument (the third parameter to [sqlite3_db_config()] is a |
| 2563 | +** <dd> The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the |
| 2564 | +** configuration of the [lookaside memory allocator] within a database |
| 2565 | +** connection. |
| 2566 | +** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are <i>not</i> |
| 2567 | +** in the [DBCONFIG arguments|usual format]. |
| 2568 | +** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two, |
| 2569 | +** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE |
| 2570 | +** should have a total of five parameters. |
| 2571 | +** <ol> |
| 2572 | +** <li><p>The first argument ("buf") is a |
| 2548 | 2573 | ** pointer to a memory buffer to use for lookaside memory. |
| 2549 | | -** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb |
| 2550 | | -** may be NULL in which case SQLite will allocate the |
| 2551 | | -** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the |
| 2552 | | -** size of each lookaside buffer slot. ^The third argument is the number of |
| 2553 | | -** slots. The size of the buffer in the first argument must be greater than |
| 2554 | | -** or equal to the product of the second and third arguments. The buffer |
| 2555 | | -** must be aligned to an 8-byte boundary. ^If the second argument to |
| 2556 | | -** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally |
| 2557 | | -** rounded down to the next smaller multiple of 8. ^(The lookaside memory |
| 2574 | +** The first argument may be NULL in which case SQLite will allocate the |
| 2575 | +** lookaside buffer itself using [sqlite3_malloc()]. |
| 2576 | +** <li><P>The second argument ("sz") is the |
| 2577 | +** size of each lookaside buffer slot. Lookaside is disabled if "sz" |
| 2578 | +** is less than 8. The "sz" argument should be a multiple of 8 less than |
| 2579 | +** 65536. If "sz" does not meet this constraint, it is reduced in size until |
| 2580 | +** it does. |
| 2581 | +** <li><p>The third argument ("cnt") is the number of slots. Lookaside is disabled |
| 2582 | +** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so |
| 2583 | +** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt" |
| 2584 | +** parameter is usually chosen so that the product of "sz" and "cnt" is less |
| 2585 | +** than 1,000,000. |
| 2586 | +** </ol> |
| 2587 | +** <p>If the "buf" argument is not NULL, then it must |
| 2588 | +** point to a memory buffer with a size that is greater than |
| 2589 | +** or equal to the product of "sz" and "cnt". |
| 2590 | +** The buffer must be aligned to an 8-byte boundary. |
| 2591 | +** The lookaside memory |
| 2558 | 2592 | ** configuration for a database connection can only be changed when that |
| 2559 | 2593 | ** connection is not currently using lookaside memory, or in other words |
| 2560 | | -** when the "current value" returned by |
| 2561 | | -** [sqlite3_db_status](D,[SQLITE_DBSTATUS_LOOKASIDE_USED],...) is zero. |
| 2594 | +** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero. |
| 2562 | 2595 | ** Any attempt to change the lookaside memory configuration when lookaside |
| 2563 | 2596 | ** memory is in use leaves the configuration unchanged and returns |
| 2564 | | -** [SQLITE_BUSY].)^</dd> |
| 2597 | +** [SQLITE_BUSY]. |
| 2598 | +** If the "buf" argument is NULL and an attempt |
| 2599 | +** to allocate memory based on "sz" and "cnt" fails, then |
| 2600 | +** lookaside is silently disabled. |
| 2601 | +** <p> |
| 2602 | +** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the |
| 2603 | +** default lookaside configuration at initialization. The |
| 2604 | +** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside |
| 2605 | +** configuration at compile-time. Typical values for lookaside are 1200 for |
| 2606 | +** "sz" and 40 to 100 for "cnt". |
| 2607 | +** </dd> |
| 2565 | 2608 | ** |
| 2566 | 2609 | ** [[SQLITE_DBCONFIG_ENABLE_FKEY]] |
| 2567 | 2610 | ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt> |
| 2568 | 2611 | ** <dd> ^This option is used to enable or disable the enforcement of |
| 2569 | | -** [foreign key constraints]. There should be two additional arguments. |
| 2612 | +** [foreign key constraints]. This is the same setting that is |
| 2613 | +** enabled or disabled by the [PRAGMA foreign_keys] statement. |
| 2570 | 2614 | ** The first argument is an integer which is 0 to disable FK enforcement, |
| 2571 | 2615 | ** positive to enable FK enforcement or negative to leave FK enforcement |
| 2572 | 2616 | ** unchanged. The second parameter is a pointer to an integer into which |
| 2573 | 2617 | ** is written 0 or 1 to indicate whether FK enforcement is off or on |
| 2574 | 2618 | ** following this call. The second parameter may be a NULL pointer, in |
| | @@ -2586,17 +2630,17 @@ |
| 2586 | 2630 | ** which case the trigger setting is not reported back. |
| 2587 | 2631 | ** |
| 2588 | 2632 | ** <p>Originally this option disabled all triggers. ^(However, since |
| 2589 | 2633 | ** SQLite version 3.35.0, TEMP triggers are still allowed even if |
| 2590 | 2634 | ** this option is off. So, in other words, this option now only disables |
| 2591 | | -** triggers in the main database schema or in the schemas of ATTACH-ed |
| 2635 | +** triggers in the main database schema or in the schemas of [ATTACH]-ed |
| 2592 | 2636 | ** databases.)^ </dd> |
| 2593 | 2637 | ** |
| 2594 | 2638 | ** [[SQLITE_DBCONFIG_ENABLE_VIEW]] |
| 2595 | 2639 | ** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt> |
| 2596 | 2640 | ** <dd> ^This option is used to enable or disable [CREATE VIEW | views]. |
| 2597 | | -** There should be two additional arguments. |
| 2641 | +** There must be two additional arguments. |
| 2598 | 2642 | ** The first argument is an integer which is 0 to disable views, |
| 2599 | 2643 | ** positive to enable views or negative to leave the setting unchanged. |
| 2600 | 2644 | ** The second parameter is a pointer to an integer into which |
| 2601 | 2645 | ** is written 0 or 1 to indicate whether views are disabled or enabled |
| 2602 | 2646 | ** following this call. The second parameter may be a NULL pointer, in |
| | @@ -2611,11 +2655,11 @@ |
| 2611 | 2655 | ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]] |
| 2612 | 2656 | ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt> |
| 2613 | 2657 | ** <dd> ^This option is used to enable or disable the |
| 2614 | 2658 | ** [fts3_tokenizer()] function which is part of the |
| 2615 | 2659 | ** [FTS3] full-text search engine extension. |
| 2616 | | -** There should be two additional arguments. |
| 2660 | +** There must be two additional arguments. |
| 2617 | 2661 | ** The first argument is an integer which is 0 to disable fts3_tokenizer() or |
| 2618 | 2662 | ** positive to enable fts3_tokenizer() or negative to leave the setting |
| 2619 | 2663 | ** unchanged. |
| 2620 | 2664 | ** The second parameter is a pointer to an integer into which |
| 2621 | 2665 | ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled |
| | @@ -2626,11 +2670,11 @@ |
| 2626 | 2670 | ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt> |
| 2627 | 2671 | ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()] |
| 2628 | 2672 | ** interface independently of the [load_extension()] SQL function. |
| 2629 | 2673 | ** The [sqlite3_enable_load_extension()] API enables or disables both the |
| 2630 | 2674 | ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. |
| 2631 | | -** There should be two additional arguments. |
| 2675 | +** There must be two additional arguments. |
| 2632 | 2676 | ** When the first argument to this interface is 1, then only the C-API is |
| 2633 | 2677 | ** enabled and the SQL function remains disabled. If the first argument to |
| 2634 | 2678 | ** this interface is 0, then both the C-API and the SQL function are disabled. |
| 2635 | 2679 | ** If the first argument is -1, then no changes are made to state of either the |
| 2636 | 2680 | ** C-API or the SQL function. |
| | @@ -2640,27 +2684,34 @@ |
| 2640 | 2684 | ** be a NULL pointer, in which case the new setting is not reported back. |
| 2641 | 2685 | ** </dd> |
| 2642 | 2686 | ** |
| 2643 | 2687 | ** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt> |
| 2644 | 2688 | ** <dd> ^This option is used to change the name of the "main" database |
| 2645 | | -** schema. ^The sole argument is a pointer to a constant UTF8 string |
| 2646 | | -** which will become the new schema name in place of "main". ^SQLite |
| 2647 | | -** does not make a copy of the new main schema name string, so the application |
| 2648 | | -** must ensure that the argument passed into this DBCONFIG option is unchanged |
| 2649 | | -** until after the database connection closes. |
| 2689 | +** schema. This option does not follow the |
| 2690 | +** [DBCONFIG arguments|usual SQLITE_DBCONFIG argument format]. |
| 2691 | +** This option takes exactly one additional argument so that the |
| 2692 | +** [sqlite3_db_config()] call has a total of three parameters. The |
| 2693 | +** extra argument must be a pointer to a constant UTF8 string which |
| 2694 | +** will become the new schema name in place of "main". ^SQLite does |
| 2695 | +** not make a copy of the new main schema name string, so the application |
| 2696 | +** must ensure that the argument passed into SQLITE_DBCONFIG MAINDBNAME |
| 2697 | +** is unchanged until after the database connection closes. |
| 2650 | 2698 | ** </dd> |
| 2651 | 2699 | ** |
| 2652 | 2700 | ** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] |
| 2653 | 2701 | ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt> |
| 2654 | | -** <dd> Usually, when a database in wal mode is closed or detached from a |
| 2655 | | -** database handle, SQLite checks if this will mean that there are now no |
| 2656 | | -** connections at all to the database. If so, it performs a checkpoint |
| 2657 | | -** operation before closing the connection. This option may be used to |
| 2658 | | -** override this behavior. The first parameter passed to this operation |
| 2659 | | -** is an integer - positive to disable checkpoints-on-close, or zero (the |
| 2660 | | -** default) to enable them, and negative to leave the setting unchanged. |
| 2661 | | -** The second parameter is a pointer to an integer |
| 2702 | +** <dd> Usually, when a database in [WAL mode] is closed or detached from a |
| 2703 | +** database handle, SQLite checks if if there are other connections to the |
| 2704 | +** same database, and if there are no other database connection (if the |
| 2705 | +** connection being closed is the last open connection to the database), |
| 2706 | +** then SQLite performs a [checkpoint] before closing the connection and |
| 2707 | +** deletes the WAL file. The SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE option can |
| 2708 | +** be used to override that behavior. The first argument passed to this |
| 2709 | +** operation (the third parameter to [sqlite3_db_config()]) is an integer |
| 2710 | +** which is positive to disable checkpoints-on-close, or zero (the default) |
| 2711 | +** to enable them, and negative to leave the setting unchanged. |
| 2712 | +** The second argument (the fourth parameter) is a pointer to an integer |
| 2662 | 2713 | ** into which is written 0 or 1 to indicate whether checkpoints-on-close |
| 2663 | 2714 | ** have been disabled - 0 if they are not disabled, 1 if they are. |
| 2664 | 2715 | ** </dd> |
| 2665 | 2716 | ** |
| 2666 | 2717 | ** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt> |
| | @@ -2817,11 +2868,11 @@ |
| 2817 | 2868 | ** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears |
| 2818 | 2869 | ** a flag that enables collection of the sqlite3_stmt_scanstatus_v2() |
| 2819 | 2870 | ** statistics. For statistics to be collected, the flag must be set on |
| 2820 | 2871 | ** the database handle both when the SQL statement is prepared and when it |
| 2821 | 2872 | ** is stepped. The flag is set (collection of statistics is enabled) |
| 2822 | | -** by default. This option takes two arguments: an integer and a pointer to |
| 2873 | +** by default. <p>This option takes two arguments: an integer and a pointer to |
| 2823 | 2874 | ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or |
| 2824 | 2875 | ** leave unchanged the statement scanstatus option. If the second argument |
| 2825 | 2876 | ** is not NULL, then the value of the statement scanstatus setting after |
| 2826 | 2877 | ** processing the first argument is written into the integer that the second |
| 2827 | 2878 | ** argument points to. |
| | @@ -2831,20 +2882,89 @@ |
| 2831 | 2882 | ** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</dt> |
| 2832 | 2883 | ** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order |
| 2833 | 2884 | ** in which tables and indexes are scanned so that the scans start at the end |
| 2834 | 2885 | ** and work toward the beginning rather than starting at the beginning and |
| 2835 | 2886 | ** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the |
| 2836 | | -** same as setting [PRAGMA reverse_unordered_selects]. This option takes |
| 2887 | +** same as setting [PRAGMA reverse_unordered_selects]. <p>This option takes |
| 2837 | 2888 | ** two arguments which are an integer and a pointer to an integer. The first |
| 2838 | 2889 | ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the |
| 2839 | 2890 | ** reverse scan order flag, respectively. If the second argument is not NULL, |
| 2840 | 2891 | ** then 0 or 1 is written into the integer that the second argument points to |
| 2841 | 2892 | ** depending on if the reverse scan order flag is set after processing the |
| 2842 | 2893 | ** first argument. |
| 2843 | 2894 | ** </dd> |
| 2844 | 2895 | ** |
| 2896 | +** [[SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE]] |
| 2897 | +** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE</dt> |
| 2898 | +** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE option enables or disables |
| 2899 | +** the ability of the [ATTACH DATABASE] SQL command to create a new database |
| 2900 | +** file if the database filed named in the ATTACH command does not already |
| 2901 | +** exist. This ability of ATTACH to create a new database is enabled by |
| 2902 | +** default. Applications can disable or reenable the ability for ATTACH to |
| 2903 | +** create new database files using this DBCONFIG option.<p> |
| 2904 | +** This option takes two arguments which are an integer and a pointer |
| 2905 | +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or |
| 2906 | +** leave unchanged the attach-create flag, respectively. If the second |
| 2907 | +** argument is not NULL, then 0 or 1 is written into the integer that the |
| 2908 | +** second argument points to depending on if the attach-create flag is set |
| 2909 | +** after processing the first argument. |
| 2910 | +** </dd> |
| 2911 | +** |
| 2912 | +** [[SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE]] |
| 2913 | +** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE</dt> |
| 2914 | +** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE option enables or disables the |
| 2915 | +** ability of the [ATTACH DATABASE] SQL command to open a database for writing. |
| 2916 | +** This capability is enabled by default. Applications can disable or |
| 2917 | +** reenable this capability using the current DBCONFIG option. If the |
| 2918 | +** the this capability is disabled, the [ATTACH] command will still work, |
| 2919 | +** but the database will be opened read-only. If this option is disabled, |
| 2920 | +** then the ability to create a new database using [ATTACH] is also disabled, |
| 2921 | +** regardless of the value of the [SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE] |
| 2922 | +** option.<p> |
| 2923 | +** This option takes two arguments which are an integer and a pointer |
| 2924 | +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or |
| 2925 | +** leave unchanged the ability to ATTACH another database for writing, |
| 2926 | +** respectively. If the second argument is not NULL, then 0 or 1 is written |
| 2927 | +** into the integer to which the second argument points, depending on whether |
| 2928 | +** the ability to ATTACH a read/write database is enabled or disabled |
| 2929 | +** after processing the first argument. |
| 2930 | +** </dd> |
| 2931 | +** |
| 2932 | +** [[SQLITE_DBCONFIG_ENABLE_COMMENTS]] |
| 2933 | +** <dt>SQLITE_DBCONFIG_ENABLE_COMMENTS</dt> |
| 2934 | +** <dd>The SQLITE_DBCONFIG_ENABLE_COMMENTS option enables or disables the |
| 2935 | +** ability to include comments in SQL text. Comments are enabled by default. |
| 2936 | +** An application can disable or reenable comments in SQL text using this |
| 2937 | +** DBCONFIG option.<p> |
| 2938 | +** This option takes two arguments which are an integer and a pointer |
| 2939 | +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or |
| 2940 | +** leave unchanged the ability to use comments in SQL text, |
| 2941 | +** respectively. If the second argument is not NULL, then 0 or 1 is written |
| 2942 | +** into the integer that the second argument points to depending on if |
| 2943 | +** comments are allowed in SQL text after processing the first argument. |
| 2944 | +** </dd> |
| 2945 | +** |
| 2845 | 2946 | ** </dl> |
| 2947 | +** |
| 2948 | +** [[DBCONFIG arguments]] <h3>Arguments To SQLITE_DBCONFIG Options</h3> |
| 2949 | +** |
| 2950 | +** <p>Most of the SQLITE_DBCONFIG options take two arguments, so that the |
| 2951 | +** overall call to [sqlite3_db_config()] has a total of four parameters. |
| 2952 | +** The first argument (the third parameter to sqlite3_db_config()) is a integer. |
| 2953 | +** The second argument is a pointer to an integer. If the first argument is 1, |
| 2954 | +** then the option becomes enabled. If the first integer argument is 0, then the |
| 2955 | +** option is disabled. If the first argument is -1, then the option setting |
| 2956 | +** is unchanged. The second argument, the pointer to an integer, may be NULL. |
| 2957 | +** If the second argument is not NULL, then a value of 0 or 1 is written into |
| 2958 | +** the integer to which the second argument points, depending on whether the |
| 2959 | +** setting is disabled or enabled after applying any changes specified by |
| 2960 | +** the first argument. |
| 2961 | +** |
| 2962 | +** <p>While most SQLITE_DBCONFIG options use the argument format |
| 2963 | +** described in the previous paragraph, the [SQLITE_DBCONFIG_MAINDBNAME] |
| 2964 | +** and [SQLITE_DBCONFIG_LOOKASIDE] options are different. See the |
| 2965 | +** documentation of those exceptional options for details. |
| 2846 | 2966 | */ |
| 2847 | 2967 | #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ |
| 2848 | 2968 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 2849 | 2969 | #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ |
| 2850 | 2970 | #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ |
| | @@ -2862,11 +2982,14 @@ |
| 2862 | 2982 | #define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */ |
| 2863 | 2983 | #define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */ |
| 2864 | 2984 | #define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */ |
| 2865 | 2985 | #define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */ |
| 2866 | 2986 | #define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */ |
| 2867 | | -#define SQLITE_DBCONFIG_MAX 1019 /* Largest DBCONFIG */ |
| 2987 | +#define SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE 1020 /* int int* */ |
| 2988 | +#define SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE 1021 /* int int* */ |
| 2989 | +#define SQLITE_DBCONFIG_ENABLE_COMMENTS 1022 /* int int* */ |
| 2990 | +#define SQLITE_DBCONFIG_MAX 1022 /* Largest DBCONFIG */ |
| 2868 | 2991 | |
| 2869 | 2992 | /* |
| 2870 | 2993 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 2871 | 2994 | ** METHOD: sqlite3 |
| 2872 | 2995 | ** |
| | @@ -3216,10 +3339,48 @@ |
| 3216 | 3339 | ** |
| 3217 | 3340 | ** See also: [PRAGMA busy_timeout] |
| 3218 | 3341 | */ |
| 3219 | 3342 | SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); |
| 3220 | 3343 | |
| 3344 | +/* |
| 3345 | +** CAPI3REF: Set the Setlk Timeout |
| 3346 | +** METHOD: sqlite3 |
| 3347 | +** |
| 3348 | +** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If |
| 3349 | +** the VFS supports blocking locks, it sets the timeout in ms used by |
| 3350 | +** eligible locks taken on wal mode databases by the specified database |
| 3351 | +** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does |
| 3352 | +** not support blocking locks, this function is a no-op. |
| 3353 | +** |
| 3354 | +** Passing 0 to this function disables blocking locks altogether. Passing |
| 3355 | +** -1 to this function requests that the VFS blocks for a long time - |
| 3356 | +** indefinitely if possible. The results of passing any other negative value |
| 3357 | +** are undefined. |
| 3358 | +** |
| 3359 | +** Internally, each SQLite database handle store two timeout values - the |
| 3360 | +** busy-timeout (used for rollback mode databases, or if the VFS does not |
| 3361 | +** support blocking locks) and the setlk-timeout (used for blocking locks |
| 3362 | +** on wal-mode databases). The sqlite3_busy_timeout() method sets both |
| 3363 | +** values, this function sets only the setlk-timeout value. Therefore, |
| 3364 | +** to configure separate busy-timeout and setlk-timeout values for a single |
| 3365 | +** database handle, call sqlite3_busy_timeout() followed by this function. |
| 3366 | +** |
| 3367 | +** Whenever the number of connections to a wal mode database falls from |
| 3368 | +** 1 to 0, the last connection takes an exclusive lock on the database, |
| 3369 | +** then checkpoints and deletes the wal file. While it is doing this, any |
| 3370 | +** new connection that tries to read from the database fails with an |
| 3371 | +** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is |
| 3372 | +** passed to this API, the new connection blocks until the exclusive lock |
| 3373 | +** has been released. |
| 3374 | +*/ |
| 3375 | +SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags); |
| 3376 | + |
| 3377 | +/* |
| 3378 | +** CAPI3REF: Flags for sqlite3_setlk_timeout() |
| 3379 | +*/ |
| 3380 | +#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01 |
| 3381 | + |
| 3221 | 3382 | /* |
| 3222 | 3383 | ** CAPI3REF: Convenience Routines For Running Queries |
| 3223 | 3384 | ** METHOD: sqlite3 |
| 3224 | 3385 | ** |
| 3225 | 3386 | ** This is a legacy interface that is preserved for backwards compatibility. |
| | @@ -11065,12 +11226,13 @@ |
| 11065 | 11226 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb); |
| 11066 | 11227 | |
| 11067 | 11228 | /* |
| 11068 | 11229 | ** CAPI3REF: Serialize a database |
| 11069 | 11230 | ** |
| 11070 | | -** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory |
| 11071 | | -** that is a serialization of the S database on [database connection] D. |
| 11231 | +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to |
| 11232 | +** memory that is a serialization of the S database on |
| 11233 | +** [database connection] D. If S is a NULL pointer, the main database is used. |
| 11072 | 11234 | ** If P is not a NULL pointer, then the size of the database in bytes |
| 11073 | 11235 | ** is written into *P. |
| 11074 | 11236 | ** |
| 11075 | 11237 | ** For an ordinary on-disk database file, the serialization is just a |
| 11076 | 11238 | ** copy of the disk file. For an in-memory database or a "TEMP" database, |
| | @@ -12232,23 +12394,10 @@ |
| 12232 | 12394 | int nB, /* Number of bytes in buffer pB */ |
| 12233 | 12395 | void *pB, /* Pointer to buffer containing changeset B */ |
| 12234 | 12396 | int *pnOut, /* OUT: Number of bytes in output changeset */ |
| 12235 | 12397 | void **ppOut /* OUT: Buffer containing output changeset */ |
| 12236 | 12398 | ); |
| 12237 | | - |
| 12238 | | - |
| 12239 | | -/* |
| 12240 | | -** CAPI3REF: Upgrade the Schema of a Changeset/Patchset |
| 12241 | | -*/ |
| 12242 | | -SQLITE_API int sqlite3changeset_upgrade( |
| 12243 | | - sqlite3 *db, |
| 12244 | | - const char *zDb, |
| 12245 | | - int nIn, const void *pIn, /* Input changeset */ |
| 12246 | | - int *pnOut, void **ppOut /* OUT: Inverse of input */ |
| 12247 | | -); |
| 12248 | | - |
| 12249 | | - |
| 12250 | 12399 | |
| 12251 | 12400 | /* |
| 12252 | 12401 | ** CAPI3REF: Changegroup Handle |
| 12253 | 12402 | ** |
| 12254 | 12403 | ** A changegroup is an object used to combine two or more |
| | @@ -13993,18 +14142,26 @@ |
| 13993 | 14142 | ** * Terms in the SET clause of an UPDATE statement |
| 13994 | 14143 | ** * Terms in the result set of a SELECT statement |
| 13995 | 14144 | ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. |
| 13996 | 14145 | ** * Terms in the VALUES clause of an INSERT statement |
| 13997 | 14146 | ** |
| 13998 | | -** The hard upper limit here is 32676. Most database people will |
| 14147 | +** The hard upper limit here is 32767. Most database people will |
| 13999 | 14148 | ** tell you that in a well-normalized database, you usually should |
| 14000 | 14149 | ** not have more than a dozen or so columns in any table. And if |
| 14001 | 14150 | ** that is the case, there is no point in having more than a few |
| 14002 | 14151 | ** dozen values in any of the other situations described above. |
| 14152 | +** |
| 14153 | +** An index can only have SQLITE_MAX_COLUMN columns from the user |
| 14154 | +** point of view, but the underlying b-tree that implements the index |
| 14155 | +** might have up to twice as many columns in a WITHOUT ROWID table, |
| 14156 | +** since must also store the primary key at the end. Hence the |
| 14157 | +** column count for Index is u16 instead of i16. |
| 14003 | 14158 | */ |
| 14004 | | -#ifndef SQLITE_MAX_COLUMN |
| 14159 | +#if !defined(SQLITE_MAX_COLUMN) |
| 14005 | 14160 | # define SQLITE_MAX_COLUMN 2000 |
| 14161 | +#elif SQLITE_MAX_COLUMN>32767 |
| 14162 | +# error SQLITE_MAX_COLUMN may not exceed 32767 |
| 14006 | 14163 | #endif |
| 14007 | 14164 | |
| 14008 | 14165 | /* |
| 14009 | 14166 | ** The maximum length of a single SQL statement in bytes. |
| 14010 | 14167 | ** |
| | @@ -14652,10 +14809,11 @@ |
| 14652 | 14809 | */ |
| 14653 | 14810 | struct HashElem { |
| 14654 | 14811 | HashElem *next, *prev; /* Next and previous elements in the table */ |
| 14655 | 14812 | void *data; /* Data associated with this element */ |
| 14656 | 14813 | const char *pKey; /* Key associated with this element */ |
| 14814 | + unsigned int h; /* hash for pKey */ |
| 14657 | 14815 | }; |
| 14658 | 14816 | |
| 14659 | 14817 | /* |
| 14660 | 14818 | ** Access routines. To delete, insert a NULL pointer. |
| 14661 | 14819 | */ |
| | @@ -14875,11 +15033,12 @@ |
| 14875 | 15033 | #define TK_ASTERISK 180 |
| 14876 | 15034 | #define TK_SPAN 181 |
| 14877 | 15035 | #define TK_ERROR 182 |
| 14878 | 15036 | #define TK_QNUMBER 183 |
| 14879 | 15037 | #define TK_SPACE 184 |
| 14880 | | -#define TK_ILLEGAL 185 |
| 15038 | +#define TK_COMMENT 185 |
| 15039 | +#define TK_ILLEGAL 186 |
| 14881 | 15040 | |
| 14882 | 15041 | /************** End of parse.h ***********************************************/ |
| 14883 | 15042 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 14884 | 15043 | #include <stdio.h> |
| 14885 | 15044 | #include <stdlib.h> |
| | @@ -15089,10 +15248,15 @@ |
| 15089 | 15248 | typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ |
| 15090 | 15249 | typedef INT16_TYPE i16; /* 2-byte signed integer */ |
| 15091 | 15250 | typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ |
| 15092 | 15251 | typedef INT8_TYPE i8; /* 1-byte signed integer */ |
| 15093 | 15252 | |
| 15253 | +/* A bitfield type for use inside of structures. Always follow with :N where |
| 15254 | +** N is the number of bits. |
| 15255 | +*/ |
| 15256 | +typedef unsigned bft; /* Bit Field Type */ |
| 15257 | + |
| 15094 | 15258 | /* |
| 15095 | 15259 | ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value |
| 15096 | 15260 | ** that can be stored in a u32 without loss of data. The value |
| 15097 | 15261 | ** is 0x00000000ffffffff. But because of quirks of some compilers, we |
| 15098 | 15262 | ** have to specify the value in the less intuitive manner shown: |
| | @@ -15127,10 +15291,12 @@ |
| 15127 | 15291 | ** Examples: |
| 15128 | 15292 | ** |
| 15129 | 15293 | ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 |
| 15130 | 15294 | */ |
| 15131 | 15295 | typedef INT16_TYPE LogEst; |
| 15296 | +#define LOGEST_MIN (-32768) |
| 15297 | +#define LOGEST_MAX (32767) |
| 15132 | 15298 | |
| 15133 | 15299 | /* |
| 15134 | 15300 | ** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer |
| 15135 | 15301 | */ |
| 15136 | 15302 | #ifndef SQLITE_PTRSIZE |
| | @@ -15255,10 +15421,18 @@ |
| 15255 | 15421 | */ |
| 15256 | 15422 | #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) |
| 15257 | 15423 | #define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32)) |
| 15258 | 15424 | #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) |
| 15259 | 15425 | |
| 15426 | +/* |
| 15427 | +** Macro SMXV(n) return the maximum value that can be held in variable n, |
| 15428 | +** assuming n is a signed integer type. UMXV(n) is similar for unsigned |
| 15429 | +** integer types. |
| 15430 | +*/ |
| 15431 | +#define SMXV(n) ((((i64)1)<<(sizeof(n)-1))-1) |
| 15432 | +#define UMXV(n) ((((i64)1)<<(sizeof(n)))-1) |
| 15433 | + |
| 15260 | 15434 | /* |
| 15261 | 15435 | ** Round up a number to the next larger multiple of 8. This is used |
| 15262 | 15436 | ** to force 8-byte alignment on 64-bit architectures. |
| 15263 | 15437 | ** |
| 15264 | 15438 | ** ROUND8() always does the rounding, for any argument. |
| | @@ -15397,11 +15571,11 @@ |
| 15397 | 15571 | ** 0x-------F High-level debug messages |
| 15398 | 15572 | ** 0x----FFF- More detail |
| 15399 | 15573 | ** 0xFFFF---- Low-level debug messages |
| 15400 | 15574 | ** |
| 15401 | 15575 | ** 0x00000001 Code generation |
| 15402 | | -** 0x00000002 Solver |
| 15576 | +** 0x00000002 Solver (Use 0x40000 for less detail) |
| 15403 | 15577 | ** 0x00000004 Solver costs |
| 15404 | 15578 | ** 0x00000008 WhereLoop inserts |
| 15405 | 15579 | ** |
| 15406 | 15580 | ** 0x00000010 Display sqlite3_index_info xBestIndex calls |
| 15407 | 15581 | ** 0x00000020 Range an equality scan metrics |
| | @@ -15416,10 +15590,12 @@ |
| 15416 | 15590 | ** 0x00004000 Show all WHERE terms at key points |
| 15417 | 15591 | ** 0x00008000 Show the full SELECT statement at key places |
| 15418 | 15592 | ** |
| 15419 | 15593 | ** 0x00010000 Show more detail when printing WHERE terms |
| 15420 | 15594 | ** 0x00020000 Show WHERE terms returned from whereScanNext() |
| 15595 | +** 0x00040000 Solver overview messages |
| 15596 | +** 0x00080000 Star-query heuristic |
| 15421 | 15597 | */ |
| 15422 | 15598 | |
| 15423 | 15599 | |
| 15424 | 15600 | /* |
| 15425 | 15601 | ** An instance of the following structure is used to store the busy-handler |
| | @@ -16708,10 +16884,11 @@ |
| 16708 | 16884 | ** A signature for a reusable subroutine that materializes the RHS of |
| 16709 | 16885 | ** an IN operator. |
| 16710 | 16886 | */ |
| 16711 | 16887 | struct SubrtnSig { |
| 16712 | 16888 | int selId; /* SELECT-id for the SELECT statement on the RHS */ |
| 16889 | + u8 bComplete; /* True if fully coded and available for reusable */ |
| 16713 | 16890 | char *zAff; /* Affinity of the overall IN expression */ |
| 16714 | 16891 | int iTable; /* Ephemeral table generated by the subroutine */ |
| 16715 | 16892 | int iAddr; /* Subroutine entry address */ |
| 16716 | 16893 | int regReturn; /* Register used to hold return address */ |
| 16717 | 16894 | }; |
| | @@ -17952,10 +18129,14 @@ |
| 17952 | 18129 | BusyHandler busyHandler; /* Busy callback */ |
| 17953 | 18130 | Db aDbStatic[2]; /* Static space for the 2 default backends */ |
| 17954 | 18131 | Savepoint *pSavepoint; /* List of active savepoints */ |
| 17955 | 18132 | int nAnalysisLimit; /* Number of index rows to ANALYZE */ |
| 17956 | 18133 | int busyTimeout; /* Busy handler timeout, in msec */ |
| 18134 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 18135 | + int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */ |
| 18136 | + int setlkFlags; /* Flags passed to setlk_timeout() */ |
| 18137 | +#endif |
| 17957 | 18138 | int nSavepoint; /* Number of non-transaction savepoints */ |
| 17958 | 18139 | int nStatement; /* Number of nested statement-transactions */ |
| 17959 | 18140 | i64 nDeferredCons; /* Net deferred constraints this transaction. */ |
| 17960 | 18141 | i64 nDeferredImmCons; /* Net deferred immediate constraints */ |
| 17961 | 18142 | int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ |
| | @@ -18038,10 +18219,13 @@ |
| 18038 | 18219 | /* DELETE, or UPDATE and return */ |
| 18039 | 18220 | /* the count using a callback. */ |
| 18040 | 18221 | #define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */ |
| 18041 | 18222 | #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */ |
| 18042 | 18223 | #define SQLITE_FkNoAction HI(0x00008) /* Treat all FK as NO ACTION */ |
| 18224 | +#define SQLITE_AttachCreate HI(0x00010) /* ATTACH allowed to create new dbs */ |
| 18225 | +#define SQLITE_AttachWrite HI(0x00020) /* ATTACH allowed to open for write */ |
| 18226 | +#define SQLITE_Comments HI(0x00040) /* Enable SQL comments */ |
| 18043 | 18227 | |
| 18044 | 18228 | /* Flags used only if debugging */ |
| 18045 | 18229 | #ifdef SQLITE_DEBUG |
| 18046 | 18230 | #define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */ |
| 18047 | 18231 | #define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */ |
| | @@ -18097,10 +18281,11 @@ |
| 18097 | 18281 | #define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */ |
| 18098 | 18282 | #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */ |
| 18099 | 18283 | #define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */ |
| 18100 | 18284 | #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */ |
| 18101 | 18285 | #define SQLITE_OrderBySubq 0x10000000 /* ORDER BY in subquery helps outer */ |
| 18286 | +#define SQLITE_StarQuery 0x20000000 /* Heurists for star queries */ |
| 18102 | 18287 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 18103 | 18288 | |
| 18104 | 18289 | /* |
| 18105 | 18290 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 18106 | 18291 | */ |
| | @@ -18626,10 +18811,11 @@ |
| 18626 | 18811 | VTable *p; /* List of VTable objects. */ |
| 18627 | 18812 | } vtab; |
| 18628 | 18813 | } u; |
| 18629 | 18814 | Trigger *pTrigger; /* List of triggers on this object */ |
| 18630 | 18815 | Schema *pSchema; /* Schema that contains this table */ |
| 18816 | + u8 aHx[16]; /* Column aHt[K%sizeof(aHt)] might have hash K */ |
| 18631 | 18817 | }; |
| 18632 | 18818 | |
| 18633 | 18819 | /* |
| 18634 | 18820 | ** Allowed values for Table.tabFlags. |
| 18635 | 18821 | ** |
| | @@ -18945,11 +19131,11 @@ |
| 18945 | 19131 | Expr *pPartIdxWhere; /* WHERE clause for partial indices */ |
| 18946 | 19132 | ExprList *aColExpr; /* Column expressions */ |
| 18947 | 19133 | Pgno tnum; /* DB Page containing root of this index */ |
| 18948 | 19134 | LogEst szIdxRow; /* Estimated average row size in bytes */ |
| 18949 | 19135 | u16 nKeyCol; /* Number of columns forming the key */ |
| 18950 | | - u16 nColumn; /* Number of columns stored in the index */ |
| 19136 | + u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */ |
| 18951 | 19137 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 18952 | 19138 | unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */ |
| 18953 | 19139 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 18954 | 19140 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 18955 | 19141 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| | @@ -19283,14 +19469,14 @@ |
| 19283 | 19469 | #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc) |
| 19284 | 19470 | |
| 19285 | 19471 | /* Macros can be used to test, set, or clear bits in the |
| 19286 | 19472 | ** Expr.flags field. |
| 19287 | 19473 | */ |
| 19288 | | -#define ExprHasProperty(E,P) (((E)->flags&(P))!=0) |
| 19289 | | -#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P)) |
| 19290 | | -#define ExprSetProperty(E,P) (E)->flags|=(P) |
| 19291 | | -#define ExprClearProperty(E,P) (E)->flags&=~(P) |
| 19474 | +#define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0) |
| 19475 | +#define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P)) |
| 19476 | +#define ExprSetProperty(E,P) (E)->flags|=(u32)(P) |
| 19477 | +#define ExprClearProperty(E,P) (E)->flags&=~(u32)(P) |
| 19292 | 19478 | #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue) |
| 19293 | 19479 | #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse) |
| 19294 | 19480 | #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0) |
| 19295 | 19481 | |
| 19296 | 19482 | /* Macros used to ensure that the correct members of unions are accessed |
| | @@ -19426,17 +19612,12 @@ |
| 19426 | 19612 | ** |
| 19427 | 19613 | ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k. |
| 19428 | 19614 | */ |
| 19429 | 19615 | struct IdList { |
| 19430 | 19616 | int nId; /* Number of identifiers on the list */ |
| 19431 | | - u8 eU4; /* Which element of a.u4 is valid */ |
| 19432 | 19617 | struct IdList_item { |
| 19433 | 19618 | char *zName; /* Name of the identifier */ |
| 19434 | | - union { |
| 19435 | | - int idx; /* Index in some Table.aCol[] of a column named zName */ |
| 19436 | | - Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */ |
| 19437 | | - } u4; |
| 19438 | 19619 | } a[1]; |
| 19439 | 19620 | }; |
| 19440 | 19621 | |
| 19441 | 19622 | /* |
| 19442 | 19623 | ** Allowed values for IdList.eType, which determines which value of the a.u4 |
| | @@ -20028,29 +20209,36 @@ |
| 20028 | 20209 | struct Parse { |
| 20029 | 20210 | sqlite3 *db; /* The main database structure */ |
| 20030 | 20211 | char *zErrMsg; /* An error message */ |
| 20031 | 20212 | Vdbe *pVdbe; /* An engine for executing database bytecode */ |
| 20032 | 20213 | int rc; /* Return code from execution */ |
| 20033 | | - u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */ |
| 20034 | | - u8 checkSchema; /* Causes schema cookie check after an error */ |
| 20214 | + LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 20035 | 20215 | u8 nested; /* Number of nested calls to the parser/code generator */ |
| 20036 | 20216 | u8 nTempReg; /* Number of temporary registers in aTempReg[] */ |
| 20037 | 20217 | u8 isMultiWrite; /* True if statement may modify/insert multiple rows */ |
| 20038 | 20218 | u8 mayAbort; /* True if statement may throw an ABORT exception */ |
| 20039 | 20219 | u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ |
| 20040 | | - u8 okConstFactor; /* OK to factor out constants */ |
| 20041 | 20220 | u8 disableLookaside; /* Number of times lookaside has been disabled */ |
| 20042 | 20221 | u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| 20043 | 20222 | u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */ |
| 20044 | | - u8 bHasWith; /* True if statement contains WITH */ |
| 20045 | 20223 | u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */ |
| 20224 | + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 20225 | + u8 bReturning; /* Coding a RETURNING trigger */ |
| 20226 | + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 20227 | + u8 disableTriggers; /* True to disable triggers */ |
| 20046 | 20228 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 20047 | 20229 | u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */ |
| 20048 | 20230 | #endif |
| 20049 | 20231 | #ifdef SQLITE_DEBUG |
| 20050 | 20232 | u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */ |
| 20233 | + u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER) |
| 20234 | + ** and ALTER TABLE ADD COLUMN. */ |
| 20051 | 20235 | #endif |
| 20236 | + bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */ |
| 20237 | + bft bHasWith :1; /* True if statement contains WITH */ |
| 20238 | + bft okConstFactor :1; /* OK to factor out constants */ |
| 20239 | + bft checkSchema :1; /* Causes schema cookie check after an error */ |
| 20052 | 20240 | int nRangeReg; /* Size of the temporary register block */ |
| 20053 | 20241 | int iRangeReg; /* First register in temporary register block */ |
| 20054 | 20242 | int nErr; /* Number of errors seen */ |
| 20055 | 20243 | int nTab; /* Number of previously allocated VDBE cursors */ |
| 20056 | 20244 | int nMem; /* Number of memory cells used so far */ |
| | @@ -20061,16 +20249,13 @@ |
| 20061 | 20249 | int nLabelAlloc; /* Number of slots in aLabel */ |
| 20062 | 20250 | int *aLabel; /* Space to hold the labels */ |
| 20063 | 20251 | ExprList *pConstExpr;/* Constant expressions */ |
| 20064 | 20252 | IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */ |
| 20065 | 20253 | IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */ |
| 20066 | | - Token constraintName;/* Name of the constraint currently being parsed */ |
| 20067 | 20254 | yDbMask writeMask; /* Start a write transaction on these databases */ |
| 20068 | 20255 | yDbMask cookieMask; /* Bitmask of schema verified databases */ |
| 20069 | | - int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| 20070 | | - int regRoot; /* Register holding root page number for new objects */ |
| 20071 | | - int nMaxArg; /* Max args passed to user function by sub-program */ |
| 20256 | + int nMaxArg; /* Max args to xUpdate and xFilter vtab methods */ |
| 20072 | 20257 | int nSelect; /* Number of SELECT stmts. Counter for Select.selId */ |
| 20073 | 20258 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 20074 | 20259 | u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */ |
| 20075 | 20260 | #endif |
| 20076 | 20261 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -20080,21 +20265,10 @@ |
| 20080 | 20265 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| 20081 | 20266 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 20082 | 20267 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 20083 | 20268 | TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ |
| 20084 | 20269 | ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */ |
| 20085 | | - union { |
| 20086 | | - int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */ |
| 20087 | | - Returning *pReturning; /* The RETURNING clause */ |
| 20088 | | - } u1; |
| 20089 | | - u32 oldmask; /* Mask of old.* columns referenced */ |
| 20090 | | - u32 newmask; /* Mask of new.* columns referenced */ |
| 20091 | | - LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 20092 | | - u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 20093 | | - u8 bReturning; /* Coding a RETURNING trigger */ |
| 20094 | | - u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 20095 | | - u8 disableTriggers; /* True to disable triggers */ |
| 20096 | 20270 | |
| 20097 | 20271 | /************************************************************************** |
| 20098 | 20272 | ** Fields above must be initialized to zero. The fields that follow, |
| 20099 | 20273 | ** down to the beginning of the recursive section, do not need to be |
| 20100 | 20274 | ** initialized as they will be set before being used. The boundary is |
| | @@ -20102,10 +20276,23 @@ |
| 20102 | 20276 | **************************************************************************/ |
| 20103 | 20277 | |
| 20104 | 20278 | int aTempReg[8]; /* Holding area for temporary registers */ |
| 20105 | 20279 | Parse *pOuterParse; /* Outer Parse object when nested */ |
| 20106 | 20280 | Token sNameToken; /* Token with unqualified schema object name */ |
| 20281 | + u32 oldmask; /* Mask of old.* columns referenced */ |
| 20282 | + u32 newmask; /* Mask of new.* columns referenced */ |
| 20283 | + union { |
| 20284 | + struct { /* These fields available when isCreate is true */ |
| 20285 | + int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */ |
| 20286 | + int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| 20287 | + int regRoot; /* Register holding root page for new objects */ |
| 20288 | + Token constraintName; /* Name of the constraint currently being parsed */ |
| 20289 | + } cr; |
| 20290 | + struct { /* These fields available to all other statements */ |
| 20291 | + Returning *pReturning; /* The RETURNING clause */ |
| 20292 | + } d; |
| 20293 | + } u1; |
| 20107 | 20294 | |
| 20108 | 20295 | /************************************************************************ |
| 20109 | 20296 | ** Above is constant between recursions. Below is reset before and after |
| 20110 | 20297 | ** each recursion. The boundary between these two regions is determined |
| 20111 | 20298 | ** using offsetof(Parse,sLastToken) so the sLastToken field must be the |
| | @@ -20119,13 +20306,11 @@ |
| 20119 | 20306 | u8 eParseMode; /* PARSE_MODE_XXX constant */ |
| 20120 | 20307 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 20121 | 20308 | int nVtabLock; /* Number of virtual tables to lock */ |
| 20122 | 20309 | #endif |
| 20123 | 20310 | int nHeight; /* Expression tree height of current sub-select */ |
| 20124 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 20125 | 20311 | int addrExplain; /* Address of current OP_Explain opcode */ |
| 20126 | | -#endif |
| 20127 | 20312 | VList *pVList; /* Mapping between variable names and numbers */ |
| 20128 | 20313 | Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */ |
| 20129 | 20314 | const char *zTail; /* All SQL text past the last semicolon parsed */ |
| 20130 | 20315 | Table *pNewTable; /* A table being constructed by CREATE TABLE */ |
| 20131 | 20316 | Index *pNewIndex; /* An index being constructed by CREATE INDEX. |
| | @@ -21095,11 +21280,11 @@ |
| 21095 | 21280 | SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**); |
| 21096 | 21281 | SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char); |
| 21097 | 21282 | SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char); |
| 21098 | 21283 | SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int); |
| 21099 | 21284 | SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*); |
| 21100 | | -SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16); |
| 21285 | +SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index*, int); |
| 21101 | 21286 | #ifdef SQLITE_OMIT_GENERATED_COLUMNS |
| 21102 | 21287 | # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */ |
| 21103 | 21288 | # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */ |
| 21104 | 21289 | #else |
| 21105 | 21290 | SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16); |
| | @@ -21193,11 +21378,11 @@ |
| 21193 | 21378 | SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse*,SrcList*); |
| 21194 | 21379 | SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*); |
| 21195 | 21380 | SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*); |
| 21196 | 21381 | SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*); |
| 21197 | 21382 | SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*); |
| 21198 | | -SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**); |
| 21383 | +SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**); |
| 21199 | 21384 | SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, |
| 21200 | 21385 | Expr*, int, int, u8); |
| 21201 | 21386 | SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); |
| 21202 | 21387 | SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); |
| 21203 | 21388 | SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, |
| | @@ -21329,11 +21514,12 @@ |
| 21329 | 21514 | SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*); |
| 21330 | 21515 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int); |
| 21331 | 21516 | SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*); |
| 21332 | 21517 | SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int); |
| 21333 | 21518 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); |
| 21334 | | -SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*); |
| 21519 | +SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int); |
| 21520 | +SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char*, u32); |
| 21335 | 21521 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void); |
| 21336 | 21522 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 21337 | 21523 | SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void); |
| 21338 | 21524 | SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); |
| 21339 | 21525 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) |
| | @@ -22429,10 +22615,13 @@ |
| 22429 | 22615 | #ifdef SQLITE_ENABLE_RTREE |
| 22430 | 22616 | "ENABLE_RTREE", |
| 22431 | 22617 | #endif |
| 22432 | 22618 | #ifdef SQLITE_ENABLE_SESSION |
| 22433 | 22619 | "ENABLE_SESSION", |
| 22620 | +#endif |
| 22621 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 22622 | + "ENABLE_SETLK_TIMEOUT", |
| 22434 | 22623 | #endif |
| 22435 | 22624 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 22436 | 22625 | "ENABLE_SNAPSHOT", |
| 22437 | 22626 | #endif |
| 22438 | 22627 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| | @@ -23579,10 +23768,11 @@ |
| 23579 | 23768 | char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ |
| 23580 | 23769 | void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ |
| 23581 | 23770 | #ifdef SQLITE_DEBUG |
| 23582 | 23771 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 23583 | 23772 | u16 mScopyFlags; /* flags value immediately after the shallow copy */ |
| 23773 | + u8 bScopy; /* The pScopyFrom of some other Mem *might* point here */ |
| 23584 | 23774 | #endif |
| 23585 | 23775 | }; |
| 23586 | 23776 | |
| 23587 | 23777 | /* |
| 23588 | 23778 | ** Size of struct Mem not including the Mem.zMalloc member or anything that |
| | @@ -23732,14 +23922,10 @@ |
| 23732 | 23922 | u8 skipFlag; /* Skip accumulator loading if true */ |
| 23733 | 23923 | u16 argc; /* Number of arguments */ |
| 23734 | 23924 | sqlite3_value *argv[1]; /* Argument set */ |
| 23735 | 23925 | }; |
| 23736 | 23926 | |
| 23737 | | -/* A bitfield type for use inside of structures. Always follow with :N where |
| 23738 | | -** N is the number of bits. |
| 23739 | | -*/ |
| 23740 | | -typedef unsigned bft; /* Bit Field Type */ |
| 23741 | 23927 | |
| 23742 | 23928 | /* The ScanStatus object holds a single value for the |
| 23743 | 23929 | ** sqlite3_stmt_scanstatus() interface. |
| 23744 | 23930 | ** |
| 23745 | 23931 | ** aAddrRange[]: |
| | @@ -23796,11 +23982,11 @@ |
| 23796 | 23982 | i64 iCurrentTime; /* Value of julianday('now') for this statement */ |
| 23797 | 23983 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 23798 | 23984 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 23799 | 23985 | i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ |
| 23800 | 23986 | Mem *aMem; /* The memory locations */ |
| 23801 | | - Mem **apArg; /* Arguments to currently executing user function */ |
| 23987 | + Mem **apArg; /* Arguments xUpdate and xFilter vtab methods */ |
| 23802 | 23988 | VdbeCursor **apCsr; /* One element of this array for each open cursor */ |
| 23803 | 23989 | Mem *aVar; /* Values for the OP_Variable opcode. */ |
| 23804 | 23990 | |
| 23805 | 23991 | /* When allocating a new Vdbe object, all of the fields below should be |
| 23806 | 23992 | ** initialized to zero or NULL */ |
| | @@ -23816,10 +24002,11 @@ |
| 23816 | 24002 | i64 startTime; /* Time when query started - used for profiling */ |
| 23817 | 24003 | #endif |
| 23818 | 24004 | #ifdef SQLITE_DEBUG |
| 23819 | 24005 | int rcApp; /* errcode set by sqlite3_result_error_code() */ |
| 23820 | 24006 | u32 nWrite; /* Number of write operations that have occurred */ |
| 24007 | + int napArg; /* Size of the apArg[] array */ |
| 23821 | 24008 | #endif |
| 23822 | 24009 | u16 nResColumn; /* Number of columns in one row of the result set */ |
| 23823 | 24010 | u16 nResAlloc; /* Column slots allocated to aColName[] */ |
| 23824 | 24011 | u8 errorAction; /* Recovery action to do in case of an error */ |
| 23825 | 24012 | u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| | @@ -24246,12 +24433,13 @@ |
| 24246 | 24433 | u32 nFree = countLookasideSlots(db->lookaside.pFree); |
| 24247 | 24434 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 24248 | 24435 | nInit += countLookasideSlots(db->lookaside.pSmallInit); |
| 24249 | 24436 | nFree += countLookasideSlots(db->lookaside.pSmallFree); |
| 24250 | 24437 | #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 24251 | | - if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; |
| 24252 | | - return db->lookaside.nSlot - (nInit+nFree); |
| 24438 | + assert( db->lookaside.nSlot >= nInit+nFree ); |
| 24439 | + if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit); |
| 24440 | + return (int)(db->lookaside.nSlot - (nInit+nFree)); |
| 24253 | 24441 | } |
| 24254 | 24442 | |
| 24255 | 24443 | /* |
| 24256 | 24444 | ** Query status information for a single database connection |
| 24257 | 24445 | */ |
| | @@ -24300,11 +24488,11 @@ |
| 24300 | 24488 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); |
| 24301 | 24489 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); |
| 24302 | 24490 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); |
| 24303 | 24491 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); |
| 24304 | 24492 | *pCurrent = 0; |
| 24305 | | - *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; |
| 24493 | + *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT]; |
| 24306 | 24494 | if( resetFlag ){ |
| 24307 | 24495 | db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; |
| 24308 | 24496 | } |
| 24309 | 24497 | break; |
| 24310 | 24498 | } |
| | @@ -24677,10 +24865,13 @@ |
| 24677 | 24865 | ms = ms*10.0 + *zDate - '0'; |
| 24678 | 24866 | rScale *= 10.0; |
| 24679 | 24867 | zDate++; |
| 24680 | 24868 | } |
| 24681 | 24869 | ms /= rScale; |
| 24870 | + /* Truncate to avoid problems with sub-milliseconds |
| 24871 | + ** rounding. https://sqlite.org/forum/forumpost/766a2c9231 */ |
| 24872 | + if( ms>0.999 ) ms = 0.999; |
| 24682 | 24873 | } |
| 24683 | 24874 | }else{ |
| 24684 | 24875 | s = 0; |
| 24685 | 24876 | } |
| 24686 | 24877 | p->validJD = 0; |
| | @@ -25884,11 +26075,11 @@ |
| 25884 | 26075 | sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D); |
| 25885 | 26076 | break; |
| 25886 | 26077 | } |
| 25887 | 26078 | case 'f': { /* Fractional seconds. (Non-standard) */ |
| 25888 | 26079 | double s = x.s; |
| 25889 | | - if( s>59.999 ) s = 59.999; |
| 26080 | + if( NEVER(s>59.999) ) s = 59.999; |
| 25890 | 26081 | sqlite3_str_appendf(&sRes, "%06.3f", s); |
| 25891 | 26082 | break; |
| 25892 | 26083 | } |
| 25893 | 26084 | case 'F': { |
| 25894 | 26085 | sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); |
| | @@ -31412,21 +31603,21 @@ |
| 31412 | 31603 | #define etSTRING 5 /* Strings. %s */ |
| 31413 | 31604 | #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ |
| 31414 | 31605 | #define etPERCENT 7 /* Percent symbol. %% */ |
| 31415 | 31606 | #define etCHARX 8 /* Characters. %c */ |
| 31416 | 31607 | /* The rest are extensions, not normally found in printf() */ |
| 31417 | | -#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ |
| 31418 | | -#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', |
| 31419 | | - NULL pointers replaced by SQL NULL. %Q */ |
| 31420 | | -#define etTOKEN 11 /* a pointer to a Token structure */ |
| 31421 | | -#define etSRCITEM 12 /* a pointer to a SrcItem */ |
| 31422 | | -#define etPOINTER 13 /* The %p conversion */ |
| 31423 | | -#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ |
| 31424 | | -#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 31425 | | -#define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
| 31426 | | - |
| 31427 | | -#define etINVALID 17 /* Any unrecognized conversion type */ |
| 31608 | +#define etESCAPE_q 9 /* Strings with '\'' doubled. %q */ |
| 31609 | +#define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '', |
| 31610 | + NULL pointers replaced by SQL NULL. %Q */ |
| 31611 | +#define etTOKEN 11 /* a pointer to a Token structure */ |
| 31612 | +#define etSRCITEM 12 /* a pointer to a SrcItem */ |
| 31613 | +#define etPOINTER 13 /* The %p conversion */ |
| 31614 | +#define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */ |
| 31615 | +#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 31616 | +#define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
| 31617 | + |
| 31618 | +#define etINVALID 17 /* Any unrecognized conversion type */ |
| 31428 | 31619 | |
| 31429 | 31620 | |
| 31430 | 31621 | /* |
| 31431 | 31622 | ** An "etByte" is an 8-bit unsigned value. |
| 31432 | 31623 | */ |
| | @@ -31461,13 +31652,13 @@ |
| 31461 | 31652 | static const et_info fmtinfo[] = { |
| 31462 | 31653 | { 'd', 10, 1, etDECIMAL, 0, 0 }, |
| 31463 | 31654 | { 's', 0, 4, etSTRING, 0, 0 }, |
| 31464 | 31655 | { 'g', 0, 1, etGENERIC, 30, 0 }, |
| 31465 | 31656 | { 'z', 0, 4, etDYNSTRING, 0, 0 }, |
| 31466 | | - { 'q', 0, 4, etSQLESCAPE, 0, 0 }, |
| 31467 | | - { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, |
| 31468 | | - { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, |
| 31657 | + { 'q', 0, 4, etESCAPE_q, 0, 0 }, |
| 31658 | + { 'Q', 0, 4, etESCAPE_Q, 0, 0 }, |
| 31659 | + { 'w', 0, 4, etESCAPE_w, 0, 0 }, |
| 31469 | 31660 | { 'c', 0, 0, etCHARX, 0, 0 }, |
| 31470 | 31661 | { 'o', 8, 0, etRADIX, 0, 2 }, |
| 31471 | 31662 | { 'u', 10, 0, etDECIMAL, 0, 0 }, |
| 31472 | 31663 | { 'x', 16, 0, etRADIX, 16, 1 }, |
| 31473 | 31664 | { 'X', 16, 0, etRADIX, 0, 4 }, |
| | @@ -32060,29 +32251,11 @@ |
| 32060 | 32251 | }else{ |
| 32061 | 32252 | buf[0] = 0; |
| 32062 | 32253 | } |
| 32063 | 32254 | }else{ |
| 32064 | 32255 | unsigned int ch = va_arg(ap,unsigned int); |
| 32065 | | - if( ch<0x00080 ){ |
| 32066 | | - buf[0] = ch & 0xff; |
| 32067 | | - length = 1; |
| 32068 | | - }else if( ch<0x00800 ){ |
| 32069 | | - buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); |
| 32070 | | - buf[1] = 0x80 + (u8)(ch & 0x3f); |
| 32071 | | - length = 2; |
| 32072 | | - }else if( ch<0x10000 ){ |
| 32073 | | - buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); |
| 32074 | | - buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); |
| 32075 | | - buf[2] = 0x80 + (u8)(ch & 0x3f); |
| 32076 | | - length = 3; |
| 32077 | | - }else{ |
| 32078 | | - buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); |
| 32079 | | - buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); |
| 32080 | | - buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); |
| 32081 | | - buf[3] = 0x80 + (u8)(ch & 0x3f); |
| 32082 | | - length = 4; |
| 32083 | | - } |
| 32256 | + length = sqlite3AppendOneUtf8Character(buf, ch); |
| 32084 | 32257 | } |
| 32085 | 32258 | if( precision>1 ){ |
| 32086 | 32259 | i64 nPrior = 1; |
| 32087 | 32260 | width -= precision-1; |
| 32088 | 32261 | if( width>1 && !flag_leftjustify ){ |
| | @@ -32158,26 +32331,35 @@ |
| 32158 | 32331 | /* Adjust width to account for extra bytes in UTF-8 characters */ |
| 32159 | 32332 | int ii = length - 1; |
| 32160 | 32333 | while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; |
| 32161 | 32334 | } |
| 32162 | 32335 | break; |
| 32163 | | - case etSQLESCAPE: /* %q: Escape ' characters */ |
| 32164 | | - case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ |
| 32165 | | - case etSQLESCAPE3: { /* %w: Escape " characters */ |
| 32336 | + case etESCAPE_q: /* %q: Escape ' characters */ |
| 32337 | + case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */ |
| 32338 | + case etESCAPE_w: { /* %w: Escape " characters */ |
| 32166 | 32339 | i64 i, j, k, n; |
| 32167 | | - int needQuote, isnull; |
| 32340 | + int needQuote = 0; |
| 32168 | 32341 | char ch; |
| 32169 | | - char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ |
| 32170 | 32342 | char *escarg; |
| 32343 | + char q; |
| 32171 | 32344 | |
| 32172 | 32345 | if( bArgList ){ |
| 32173 | 32346 | escarg = getTextArg(pArgList); |
| 32174 | 32347 | }else{ |
| 32175 | 32348 | escarg = va_arg(ap,char*); |
| 32176 | 32349 | } |
| 32177 | | - isnull = escarg==0; |
| 32178 | | - if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 32350 | + if( escarg==0 ){ |
| 32351 | + escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)"); |
| 32352 | + }else if( xtype==etESCAPE_Q ){ |
| 32353 | + needQuote = 1; |
| 32354 | + } |
| 32355 | + if( xtype==etESCAPE_w ){ |
| 32356 | + q = '"'; |
| 32357 | + flag_alternateform = 0; |
| 32358 | + }else{ |
| 32359 | + q = '\''; |
| 32360 | + } |
| 32179 | 32361 | /* For %q, %Q, and %w, the precision is the number of bytes (or |
| 32180 | 32362 | ** characters if the ! flags is present) to use from the input. |
| 32181 | 32363 | ** Because of the extra quoting characters inserted, the number |
| 32182 | 32364 | ** of output characters may be larger than the precision. |
| 32183 | 32365 | */ |
| | @@ -32186,26 +32368,77 @@ |
| 32186 | 32368 | if( ch==q ) n++; |
| 32187 | 32369 | if( flag_altform2 && (ch&0xc0)==0xc0 ){ |
| 32188 | 32370 | while( (escarg[i+1]&0xc0)==0x80 ){ i++; } |
| 32189 | 32371 | } |
| 32190 | 32372 | } |
| 32191 | | - needQuote = !isnull && xtype==etSQLESCAPE2; |
| 32373 | + if( flag_alternateform ){ |
| 32374 | + /* For %#q, do unistr()-style backslash escapes for |
| 32375 | + ** all control characters, and for backslash itself. |
| 32376 | + ** For %#Q, do the same but only if there is at least |
| 32377 | + ** one control character. */ |
| 32378 | + u32 nBack = 0; |
| 32379 | + u32 nCtrl = 0; |
| 32380 | + for(k=0; k<i; k++){ |
| 32381 | + if( escarg[k]=='\\' ){ |
| 32382 | + nBack++; |
| 32383 | + }else if( escarg[k]<=0x1f ){ |
| 32384 | + nCtrl++; |
| 32385 | + } |
| 32386 | + } |
| 32387 | + if( nCtrl || xtype==etESCAPE_q ){ |
| 32388 | + n += nBack + 5*nCtrl; |
| 32389 | + if( xtype==etESCAPE_Q ){ |
| 32390 | + n += 10; |
| 32391 | + needQuote = 2; |
| 32392 | + } |
| 32393 | + }else{ |
| 32394 | + flag_alternateform = 0; |
| 32395 | + } |
| 32396 | + } |
| 32192 | 32397 | n += i + 3; |
| 32193 | 32398 | if( n>etBUFSIZE ){ |
| 32194 | 32399 | bufpt = zExtra = printfTempBuf(pAccum, n); |
| 32195 | 32400 | if( bufpt==0 ) return; |
| 32196 | 32401 | }else{ |
| 32197 | 32402 | bufpt = buf; |
| 32198 | 32403 | } |
| 32199 | 32404 | j = 0; |
| 32200 | | - if( needQuote ) bufpt[j++] = q; |
| 32405 | + if( needQuote ){ |
| 32406 | + if( needQuote==2 ){ |
| 32407 | + memcpy(&bufpt[j], "unistr('", 8); |
| 32408 | + j += 8; |
| 32409 | + }else{ |
| 32410 | + bufpt[j++] = '\''; |
| 32411 | + } |
| 32412 | + } |
| 32201 | 32413 | k = i; |
| 32202 | | - for(i=0; i<k; i++){ |
| 32203 | | - bufpt[j++] = ch = escarg[i]; |
| 32204 | | - if( ch==q ) bufpt[j++] = ch; |
| 32414 | + if( flag_alternateform ){ |
| 32415 | + for(i=0; i<k; i++){ |
| 32416 | + bufpt[j++] = ch = escarg[i]; |
| 32417 | + if( ch==q ){ |
| 32418 | + bufpt[j++] = ch; |
| 32419 | + }else if( ch=='\\' ){ |
| 32420 | + bufpt[j++] = '\\'; |
| 32421 | + }else if( ch<=0x1f ){ |
| 32422 | + bufpt[j-1] = '\\'; |
| 32423 | + bufpt[j++] = 'u'; |
| 32424 | + bufpt[j++] = '0'; |
| 32425 | + bufpt[j++] = '0'; |
| 32426 | + bufpt[j++] = ch>=0x10 ? '1' : '0'; |
| 32427 | + bufpt[j++] = "0123456789abcdef"[ch&0xf]; |
| 32428 | + } |
| 32429 | + } |
| 32430 | + }else{ |
| 32431 | + for(i=0; i<k; i++){ |
| 32432 | + bufpt[j++] = ch = escarg[i]; |
| 32433 | + if( ch==q ) bufpt[j++] = ch; |
| 32434 | + } |
| 32205 | 32435 | } |
| 32206 | | - if( needQuote ) bufpt[j++] = q; |
| 32436 | + if( needQuote ){ |
| 32437 | + bufpt[j++] = '\''; |
| 32438 | + if( needQuote==2 ) bufpt[j++] = ')'; |
| 32439 | + } |
| 32207 | 32440 | bufpt[j] = 0; |
| 32208 | 32441 | length = j; |
| 32209 | 32442 | goto adjust_width_for_utf8; |
| 32210 | 32443 | } |
| 32211 | 32444 | case etTOKEN: { |
| | @@ -32444,11 +32677,11 @@ |
| 32444 | 32677 | ** pointer if any kind of error was encountered. |
| 32445 | 32678 | */ |
| 32446 | 32679 | static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ |
| 32447 | 32680 | char *zText; |
| 32448 | 32681 | assert( p->mxAlloc>0 && !isMalloced(p) ); |
| 32449 | | - zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 32682 | + zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar ); |
| 32450 | 32683 | if( zText ){ |
| 32451 | 32684 | memcpy(zText, p->zText, p->nChar+1); |
| 32452 | 32685 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 32453 | 32686 | }else{ |
| 32454 | 32687 | sqlite3StrAccumSetError(p, SQLITE_NOMEM); |
| | @@ -33052,11 +33285,14 @@ |
| 33052 | 33285 | } |
| 33053 | 33286 | if( pItem->fg.fromDDL ){ |
| 33054 | 33287 | sqlite3_str_appendf(&x, " DDL"); |
| 33055 | 33288 | } |
| 33056 | 33289 | if( pItem->fg.isCte ){ |
| 33057 | | - sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); |
| 33290 | + static const char *aMat[] = {",MAT", "", ",NO-MAT"}; |
| 33291 | + sqlite3_str_appendf(&x, " CteUse=%d%s", |
| 33292 | + pItem->u2.pCteUse->nUse, |
| 33293 | + aMat[pItem->u2.pCteUse->eM10d]); |
| 33058 | 33294 | } |
| 33059 | 33295 | if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ |
| 33060 | 33296 | sqlite3_str_appendf(&x, " isOn"); |
| 33061 | 33297 | } |
| 33062 | 33298 | if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); |
| | @@ -33083,13 +33319,10 @@ |
| 33083 | 33319 | if( pItem->pSTab ){ |
| 33084 | 33320 | Table *pTab = pItem->pSTab; |
| 33085 | 33321 | sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); |
| 33086 | 33322 | } |
| 33087 | 33323 | assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) ); |
| 33088 | | - sqlite3TreeViewPush(&pView, 0); |
| 33089 | | - sqlite3TreeViewLine(pView, "SUBQUERY"); |
| 33090 | | - sqlite3TreeViewPop(&pView); |
| 33091 | 33324 | sqlite3TreeViewSelect(pView, pItem->u4.pSubq->pSelect, 0); |
| 33092 | 33325 | } |
| 33093 | 33326 | if( pItem->fg.isTabFunc ){ |
| 33094 | 33327 | sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 33095 | 33328 | } |
| | @@ -33815,25 +34048,11 @@ |
| 33815 | 34048 | char *zName = pList->a[i].zName; |
| 33816 | 34049 | int moreToFollow = i<pList->nId - 1; |
| 33817 | 34050 | if( zName==0 ) zName = "(null)"; |
| 33818 | 34051 | sqlite3TreeViewPush(&pView, moreToFollow); |
| 33819 | 34052 | sqlite3TreeViewLine(pView, 0); |
| 33820 | | - if( pList->eU4==EU4_NONE ){ |
| 33821 | | - fprintf(stdout, "%s\n", zName); |
| 33822 | | - }else if( pList->eU4==EU4_IDX ){ |
| 33823 | | - fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx); |
| 33824 | | - }else{ |
| 33825 | | - assert( pList->eU4==EU4_EXPR ); |
| 33826 | | - if( pList->a[i].u4.pExpr==0 ){ |
| 33827 | | - fprintf(stdout, "%s (pExpr=NULL)\n", zName); |
| 33828 | | - }else{ |
| 33829 | | - fprintf(stdout, "%s\n", zName); |
| 33830 | | - sqlite3TreeViewPush(&pView, i<pList->nId-1); |
| 33831 | | - sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0); |
| 33832 | | - sqlite3TreeViewPop(&pView); |
| 33833 | | - } |
| 33834 | | - } |
| 34053 | + fprintf(stdout, "%s\n", zName); |
| 33835 | 34054 | sqlite3TreeViewPop(&pView); |
| 33836 | 34055 | } |
| 33837 | 34056 | } |
| 33838 | 34057 | } |
| 33839 | 34058 | SQLITE_PRIVATE void sqlite3TreeViewIdList( |
| | @@ -34713,10 +34932,39 @@ |
| 34713 | 34932 | *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ |
| 34714 | 34933 | *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ |
| 34715 | 34934 | *zOut++ = (u8)(c&0x00FF); \ |
| 34716 | 34935 | } \ |
| 34717 | 34936 | } |
| 34937 | + |
| 34938 | +/* |
| 34939 | +** Write a single UTF8 character whose value is v into the |
| 34940 | +** buffer starting at zOut. zOut must be sized to hold at |
| 34941 | +** least for bytes. Return the number of bytes needed |
| 34942 | +** to encode the new character. |
| 34943 | +*/ |
| 34944 | +SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char *zOut, u32 v){ |
| 34945 | + if( v<0x00080 ){ |
| 34946 | + zOut[0] = (u8)(v & 0xff); |
| 34947 | + return 1; |
| 34948 | + } |
| 34949 | + if( v<0x00800 ){ |
| 34950 | + zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f); |
| 34951 | + zOut[1] = 0x80 + (u8)(v & 0x3f); |
| 34952 | + return 2; |
| 34953 | + } |
| 34954 | + if( v<0x10000 ){ |
| 34955 | + zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f); |
| 34956 | + zOut[1] = 0x80 + (u8)((v>>6) & 0x3f); |
| 34957 | + zOut[2] = 0x80 + (u8)(v & 0x3f); |
| 34958 | + return 3; |
| 34959 | + } |
| 34960 | + zOut[0] = 0xf0 + (u8)((v>>18) & 0x07); |
| 34961 | + zOut[1] = 0x80 + (u8)((v>>12) & 0x3f); |
| 34962 | + zOut[2] = 0x80 + (u8)((v>>6) & 0x3f); |
| 34963 | + zOut[3] = 0x80 + (u8)(v & 0x3f); |
| 34964 | + return 4; |
| 34965 | +} |
| 34718 | 34966 | |
| 34719 | 34967 | /* |
| 34720 | 34968 | ** Translate a single UTF-8 character. Return the unicode value. |
| 34721 | 34969 | ** |
| 34722 | 34970 | ** During translation, assume that the byte that zTerm points |
| | @@ -36310,11 +36558,15 @@ |
| 36310 | 36558 | } |
| 36311 | 36559 | } |
| 36312 | 36560 | } |
| 36313 | 36561 | p->z = &p->zBuf[i+1]; |
| 36314 | 36562 | assert( i+p->n < sizeof(p->zBuf) ); |
| 36315 | | - while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; } |
| 36563 | + assert( p->n>0 ); |
| 36564 | + while( p->z[p->n-1]=='0' ){ |
| 36565 | + p->n--; |
| 36566 | + assert( p->n>0 ); |
| 36567 | + } |
| 36316 | 36568 | } |
| 36317 | 36569 | |
| 36318 | 36570 | /* |
| 36319 | 36571 | ** Try to convert z into an unsigned 32-bit integer. Return true on |
| 36320 | 36572 | ** success and false if there is an error. |
| | @@ -37096,16 +37348,23 @@ |
| 37096 | 37348 | /* |
| 37097 | 37349 | ** The hashing function. |
| 37098 | 37350 | */ |
| 37099 | 37351 | static unsigned int strHash(const char *z){ |
| 37100 | 37352 | unsigned int h = 0; |
| 37101 | | - unsigned char c; |
| 37102 | | - while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 37353 | + while( z[0] ){ /*OPTIMIZATION-IF-TRUE*/ |
| 37103 | 37354 | /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). |
| 37104 | 37355 | ** 0x9e3779b1 is 2654435761 which is the closest prime number to |
| 37105 | | - ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ |
| 37106 | | - h += sqlite3UpperToLower[c]; |
| 37356 | + ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. |
| 37357 | + ** |
| 37358 | + ** Only bits 0xdf for ASCII and bits 0xbf for EBCDIC each octet are |
| 37359 | + ** hashed since the omitted bits determine the upper/lower case difference. |
| 37360 | + */ |
| 37361 | +#ifdef SQLITE_EBCDIC |
| 37362 | + h += 0xbf & (unsigned char)*(z++); |
| 37363 | +#else |
| 37364 | + h += 0xdf & (unsigned char)*(z++); |
| 37365 | +#endif |
| 37107 | 37366 | h *= 0x9e3779b1; |
| 37108 | 37367 | } |
| 37109 | 37368 | return h; |
| 37110 | 37369 | } |
| 37111 | 37370 | |
| | @@ -37174,13 +37433,12 @@ |
| 37174 | 37433 | sqlite3_free(pH->ht); |
| 37175 | 37434 | pH->ht = new_ht; |
| 37176 | 37435 | pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); |
| 37177 | 37436 | memset(new_ht, 0, new_size*sizeof(struct _ht)); |
| 37178 | 37437 | for(elem=pH->first, pH->first=0; elem; elem = next_elem){ |
| 37179 | | - unsigned int h = strHash(elem->pKey) % new_size; |
| 37180 | 37438 | next_elem = elem->next; |
| 37181 | | - insertElement(pH, &new_ht[h], elem); |
| 37439 | + insertElement(pH, &new_ht[elem->h % new_size], elem); |
| 37182 | 37440 | } |
| 37183 | 37441 | return 1; |
| 37184 | 37442 | } |
| 37185 | 37443 | |
| 37186 | 37444 | /* This function (for internal use only) locates an element in an |
| | @@ -37194,27 +37452,26 @@ |
| 37194 | 37452 | unsigned int *pHash /* Write the hash value here */ |
| 37195 | 37453 | ){ |
| 37196 | 37454 | HashElem *elem; /* Used to loop thru the element list */ |
| 37197 | 37455 | unsigned int count; /* Number of elements left to test */ |
| 37198 | 37456 | unsigned int h; /* The computed hash */ |
| 37199 | | - static HashElem nullElement = { 0, 0, 0, 0 }; |
| 37457 | + static HashElem nullElement = { 0, 0, 0, 0, 0 }; |
| 37200 | 37458 | |
| 37459 | + h = strHash(pKey); |
| 37201 | 37460 | if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ |
| 37202 | 37461 | struct _ht *pEntry; |
| 37203 | | - h = strHash(pKey) % pH->htsize; |
| 37204 | | - pEntry = &pH->ht[h]; |
| 37462 | + pEntry = &pH->ht[h % pH->htsize]; |
| 37205 | 37463 | elem = pEntry->chain; |
| 37206 | 37464 | count = pEntry->count; |
| 37207 | 37465 | }else{ |
| 37208 | | - h = 0; |
| 37209 | 37466 | elem = pH->first; |
| 37210 | 37467 | count = pH->count; |
| 37211 | 37468 | } |
| 37212 | 37469 | if( pHash ) *pHash = h; |
| 37213 | 37470 | while( count ){ |
| 37214 | 37471 | assert( elem!=0 ); |
| 37215 | | - if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ |
| 37472 | + if( h==elem->h && sqlite3StrICmp(elem->pKey,pKey)==0 ){ |
| 37216 | 37473 | return elem; |
| 37217 | 37474 | } |
| 37218 | 37475 | elem = elem->next; |
| 37219 | 37476 | count--; |
| 37220 | 37477 | } |
| | @@ -37222,14 +37479,13 @@ |
| 37222 | 37479 | } |
| 37223 | 37480 | |
| 37224 | 37481 | /* Remove a single entry from the hash table given a pointer to that |
| 37225 | 37482 | ** element and a hash on the element's key. |
| 37226 | 37483 | */ |
| 37227 | | -static void removeElementGivenHash( |
| 37484 | +static void removeElement( |
| 37228 | 37485 | Hash *pH, /* The pH containing "elem" */ |
| 37229 | | - HashElem* elem, /* The element to be removed from the pH */ |
| 37230 | | - unsigned int h /* Hash value for the element */ |
| 37486 | + HashElem *elem /* The element to be removed from the pH */ |
| 37231 | 37487 | ){ |
| 37232 | 37488 | struct _ht *pEntry; |
| 37233 | 37489 | if( elem->prev ){ |
| 37234 | 37490 | elem->prev->next = elem->next; |
| 37235 | 37491 | }else{ |
| | @@ -37237,11 +37493,11 @@ |
| 37237 | 37493 | } |
| 37238 | 37494 | if( elem->next ){ |
| 37239 | 37495 | elem->next->prev = elem->prev; |
| 37240 | 37496 | } |
| 37241 | 37497 | if( pH->ht ){ |
| 37242 | | - pEntry = &pH->ht[h]; |
| 37498 | + pEntry = &pH->ht[elem->h % pH->htsize]; |
| 37243 | 37499 | if( pEntry->chain==elem ){ |
| 37244 | 37500 | pEntry->chain = elem->next; |
| 37245 | 37501 | } |
| 37246 | 37502 | assert( pEntry->count>0 ); |
| 37247 | 37503 | pEntry->count--; |
| | @@ -37288,11 +37544,11 @@ |
| 37288 | 37544 | assert( pKey!=0 ); |
| 37289 | 37545 | elem = findElementWithHash(pH,pKey,&h); |
| 37290 | 37546 | if( elem->data ){ |
| 37291 | 37547 | void *old_data = elem->data; |
| 37292 | 37548 | if( data==0 ){ |
| 37293 | | - removeElementGivenHash(pH,elem,h); |
| 37549 | + removeElement(pH,elem); |
| 37294 | 37550 | }else{ |
| 37295 | 37551 | elem->data = data; |
| 37296 | 37552 | elem->pKey = pKey; |
| 37297 | 37553 | } |
| 37298 | 37554 | return old_data; |
| | @@ -37299,19 +37555,17 @@ |
| 37299 | 37555 | } |
| 37300 | 37556 | if( data==0 ) return 0; |
| 37301 | 37557 | new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); |
| 37302 | 37558 | if( new_elem==0 ) return data; |
| 37303 | 37559 | new_elem->pKey = pKey; |
| 37560 | + new_elem->h = h; |
| 37304 | 37561 | new_elem->data = data; |
| 37305 | 37562 | pH->count++; |
| 37306 | | - if( pH->count>=10 && pH->count > 2*pH->htsize ){ |
| 37307 | | - if( rehash(pH, pH->count*2) ){ |
| 37308 | | - assert( pH->htsize>0 ); |
| 37309 | | - h = strHash(pKey) % pH->htsize; |
| 37310 | | - } |
| 37563 | + if( pH->count>=5 && pH->count > 2*pH->htsize ){ |
| 37564 | + rehash(pH, pH->count*3); |
| 37311 | 37565 | } |
| 37312 | | - insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); |
| 37566 | + insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem); |
| 37313 | 37567 | return 0; |
| 37314 | 37568 | } |
| 37315 | 37569 | |
| 37316 | 37570 | /************** End of hash.c ************************************************/ |
| 37317 | 37571 | /************** Begin file opcodes.c *****************************************/ |
| | @@ -38790,10 +39044,11 @@ |
| 38790 | 39044 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
| 38791 | 39045 | unsigned fsFlags; /* cached details from statfs() */ |
| 38792 | 39046 | #endif |
| 38793 | 39047 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 38794 | 39048 | unsigned iBusyTimeout; /* Wait this many millisec on locks */ |
| 39049 | + int bBlockOnConnect; /* True to block for SHARED locks */ |
| 38795 | 39050 | #endif |
| 38796 | 39051 | #if OS_VXWORKS |
| 38797 | 39052 | struct vxworksFileId *pId; /* Unique file ID */ |
| 38798 | 39053 | #endif |
| 38799 | 39054 | #ifdef SQLITE_DEBUG |
| | @@ -40170,11 +40425,11 @@ |
| 40170 | 40425 | assert( pInode!=0 ); |
| 40171 | 40426 | assert( sqlite3_mutex_held(pInode->pLockMutex) ); |
| 40172 | 40427 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
| 40173 | 40428 | if( pInode->bProcessLock==0 ){ |
| 40174 | 40429 | struct flock lock; |
| 40175 | | - assert( pInode->nLock==0 ); |
| 40430 | + /* assert( pInode->nLock==0 ); <-- Not true if unix-excl READONLY used */ |
| 40176 | 40431 | lock.l_whence = SEEK_SET; |
| 40177 | 40432 | lock.l_start = SHARED_FIRST; |
| 40178 | 40433 | lock.l_len = SHARED_SIZE; |
| 40179 | 40434 | lock.l_type = F_WRLCK; |
| 40180 | 40435 | rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); |
| | @@ -40183,10 +40438,17 @@ |
| 40183 | 40438 | pInode->nLock++; |
| 40184 | 40439 | }else{ |
| 40185 | 40440 | rc = 0; |
| 40186 | 40441 | } |
| 40187 | 40442 | }else{ |
| 40443 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 40444 | + if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK |
| 40445 | + && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE |
| 40446 | + ){ |
| 40447 | + rc = osFcntl(pFile->h, F_SETLKW, pLock); |
| 40448 | + }else |
| 40449 | +#endif |
| 40188 | 40450 | rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); |
| 40189 | 40451 | } |
| 40190 | 40452 | return rc; |
| 40191 | 40453 | } |
| 40192 | 40454 | |
| | @@ -42544,21 +42806,27 @@ |
| 42544 | 42806 | return SQLITE_OK; |
| 42545 | 42807 | } |
| 42546 | 42808 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42547 | 42809 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 42548 | 42810 | int iOld = pFile->iBusyTimeout; |
| 42811 | + int iNew = *(int*)pArg; |
| 42549 | 42812 | #if SQLITE_ENABLE_SETLK_TIMEOUT==1 |
| 42550 | | - pFile->iBusyTimeout = *(int*)pArg; |
| 42813 | + pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew; |
| 42551 | 42814 | #elif SQLITE_ENABLE_SETLK_TIMEOUT==2 |
| 42552 | 42815 | pFile->iBusyTimeout = !!(*(int*)pArg); |
| 42553 | 42816 | #else |
| 42554 | 42817 | # error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" |
| 42555 | 42818 | #endif |
| 42556 | 42819 | *(int*)pArg = iOld; |
| 42557 | 42820 | return SQLITE_OK; |
| 42558 | 42821 | } |
| 42559 | | -#endif |
| 42822 | + case SQLITE_FCNTL_BLOCK_ON_CONNECT: { |
| 42823 | + int iNew = *(int*)pArg; |
| 42824 | + pFile->bBlockOnConnect = iNew; |
| 42825 | + return SQLITE_OK; |
| 42826 | + } |
| 42827 | +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 42560 | 42828 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 42561 | 42829 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 42562 | 42830 | i64 newLimit = *(i64*)pArg; |
| 42563 | 42831 | int rc = SQLITE_OK; |
| 42564 | 42832 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| | @@ -43537,11 +43805,11 @@ |
| 43537 | 43805 | ** occur later in the above list than the lock being obtained may be |
| 43538 | 43806 | ** held. |
| 43539 | 43807 | ** |
| 43540 | 43808 | ** It is not permitted to block on the RECOVER lock. |
| 43541 | 43809 | */ |
| 43542 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 43810 | +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG) |
| 43543 | 43811 | { |
| 43544 | 43812 | u16 lockMask = (p->exclMask|p->sharedMask); |
| 43545 | 43813 | assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 43546 | 43814 | (ofst!=2) /* not RECOVER */ |
| 43547 | 43815 | && (ofst!=1 || lockMask==0 || lockMask==2) |
| | @@ -47067,11 +47335,21 @@ |
| 47067 | 47335 | HANDLE hMap; /* Handle for accessing memory mapping */ |
| 47068 | 47336 | void *pMapRegion; /* Area memory mapped */ |
| 47069 | 47337 | sqlite3_int64 mmapSize; /* Size of mapped region */ |
| 47070 | 47338 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
| 47071 | 47339 | #endif |
| 47340 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 47341 | + DWORD iBusyTimeout; /* Wait this many millisec on locks */ |
| 47342 | + int bBlockOnConnect; |
| 47343 | +#endif |
| 47072 | 47344 | }; |
| 47345 | + |
| 47346 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 47347 | +# define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout |
| 47348 | +#else |
| 47349 | +# define winFileBusyTimeout(pDbFd) 0 |
| 47350 | +#endif |
| 47073 | 47351 | |
| 47074 | 47352 | /* |
| 47075 | 47353 | ** The winVfsAppData structure is used for the pAppData member for all of the |
| 47076 | 47354 | ** Win32 VFS variants. |
| 47077 | 47355 | */ |
| | @@ -47502,10 +47780,16 @@ |
| 47502 | 47780 | #endif |
| 47503 | 47781 | |
| 47504 | 47782 | #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \ |
| 47505 | 47783 | LPWSTR*))aSyscall[25].pCurrent) |
| 47506 | 47784 | |
| 47785 | +/* |
| 47786 | +** For GetLastError(), MSDN says: |
| 47787 | +** |
| 47788 | +** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 47789 | +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 47790 | +*/ |
| 47507 | 47791 | { "GetLastError", (SYSCALL)GetLastError, 0 }, |
| 47508 | 47792 | |
| 47509 | 47793 | #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent) |
| 47510 | 47794 | |
| 47511 | 47795 | #if !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| | @@ -47784,15 +48068,17 @@ |
| 47784 | 48068 | #endif |
| 47785 | 48069 | |
| 47786 | 48070 | #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \ |
| 47787 | 48071 | DWORD,DWORD))aSyscall[62].pCurrent) |
| 47788 | 48072 | |
| 47789 | | -#if !SQLITE_OS_WINRT |
| 48073 | +/* |
| 48074 | +** For WaitForSingleObject(), MSDN says: |
| 48075 | +** |
| 48076 | +** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 48077 | +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 48078 | +*/ |
| 47790 | 48079 | { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 }, |
| 47791 | | -#else |
| 47792 | | - { "WaitForSingleObject", (SYSCALL)0, 0 }, |
| 47793 | | -#endif |
| 47794 | 48080 | |
| 47795 | 48081 | #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ |
| 47796 | 48082 | DWORD))aSyscall[63].pCurrent) |
| 47797 | 48083 | |
| 47798 | 48084 | #if !SQLITE_OS_WINCE |
| | @@ -47935,10 +48221,44 @@ |
| 47935 | 48221 | #endif |
| 47936 | 48222 | |
| 47937 | 48223 | #define osFlushViewOfFile \ |
| 47938 | 48224 | ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent) |
| 47939 | 48225 | |
| 48226 | +/* |
| 48227 | +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent() |
| 48228 | +** to implement blocking locks with timeouts. MSDN says: |
| 48229 | +** |
| 48230 | +** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 48231 | +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 48232 | +*/ |
| 48233 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 48234 | + { "CreateEvent", (SYSCALL)CreateEvent, 0 }, |
| 48235 | +#else |
| 48236 | + { "CreateEvent", (SYSCALL)0, 0 }, |
| 48237 | +#endif |
| 48238 | + |
| 48239 | +#define osCreateEvent ( \ |
| 48240 | + (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \ |
| 48241 | + aSyscall[80].pCurrent \ |
| 48242 | +) |
| 48243 | + |
| 48244 | +/* |
| 48245 | +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo() |
| 48246 | +** for the case where a timeout expires and a lock request must be |
| 48247 | +** cancelled. |
| 48248 | +** |
| 48249 | +** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 48250 | +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 48251 | +*/ |
| 48252 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 48253 | + { "CancelIo", (SYSCALL)CancelIo, 0 }, |
| 48254 | +#else |
| 48255 | + { "CancelIo", (SYSCALL)0, 0 }, |
| 48256 | +#endif |
| 48257 | + |
| 48258 | +#define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent) |
| 48259 | + |
| 47940 | 48260 | }; /* End of the overrideable system calls */ |
| 47941 | 48261 | |
| 47942 | 48262 | /* |
| 47943 | 48263 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
| 47944 | 48264 | ** "win32" VFSes. Return SQLITE_OK upon successfully updating the |
| | @@ -48233,11 +48553,13 @@ |
| 48233 | 48553 | (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); |
| 48234 | 48554 | #endif |
| 48235 | 48555 | } |
| 48236 | 48556 | return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; |
| 48237 | 48557 | #elif SQLITE_TEST |
| 48238 | | - return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; |
| 48558 | + return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2 |
| 48559 | + || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 |
| 48560 | + ; |
| 48239 | 48561 | #else |
| 48240 | 48562 | /* |
| 48241 | 48563 | ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are |
| 48242 | 48564 | ** deprecated are always assumed to be based on the NT kernel. |
| 48243 | 48565 | */ |
| | @@ -49319,10 +49641,89 @@ |
| 49319 | 49641 | return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 49320 | 49642 | numBytesHigh); |
| 49321 | 49643 | } |
| 49322 | 49644 | #endif |
| 49323 | 49645 | } |
| 49646 | + |
| 49647 | +/* |
| 49648 | +** Lock a region of nByte bytes starting at offset offset of file hFile. |
| 49649 | +** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock |
| 49650 | +** otherwise. If nMs is greater than zero and the lock cannot be obtained |
| 49651 | +** immediately, block for that many ms before giving up. |
| 49652 | +** |
| 49653 | +** This function returns SQLITE_OK if the lock is obtained successfully. If |
| 49654 | +** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or |
| 49655 | +** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR. |
| 49656 | +*/ |
| 49657 | +static int winHandleLockTimeout( |
| 49658 | + HANDLE hFile, |
| 49659 | + DWORD offset, |
| 49660 | + DWORD nByte, |
| 49661 | + int bExcl, |
| 49662 | + DWORD nMs |
| 49663 | +){ |
| 49664 | + DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0); |
| 49665 | + int rc = SQLITE_OK; |
| 49666 | + BOOL ret; |
| 49667 | + |
| 49668 | + if( !osIsNT() ){ |
| 49669 | + ret = winLockFile(&hFile, flags, offset, 0, nByte, 0); |
| 49670 | + }else{ |
| 49671 | + OVERLAPPED ovlp; |
| 49672 | + memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 49673 | + ovlp.Offset = offset; |
| 49674 | + |
| 49675 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 49676 | + if( nMs!=0 ){ |
| 49677 | + flags &= ~LOCKFILE_FAIL_IMMEDIATELY; |
| 49678 | + } |
| 49679 | + ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL); |
| 49680 | + if( ovlp.hEvent==NULL ){ |
| 49681 | + return SQLITE_IOERR_LOCK; |
| 49682 | + } |
| 49683 | +#endif |
| 49684 | + |
| 49685 | + ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp); |
| 49686 | + |
| 49687 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 49688 | + /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was |
| 49689 | + ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to |
| 49690 | + ** LockFileEx() may fail because the request is still pending. This can |
| 49691 | + ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified. |
| 49692 | + ** |
| 49693 | + ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags |
| 49694 | + ** passed to LockFileEx(). In this case, if the operation is pending, |
| 49695 | + ** block indefinitely until it is finished. |
| 49696 | + ** |
| 49697 | + ** Otherwise, wait for up to nMs ms for the operation to finish. nMs |
| 49698 | + ** may be set to INFINITE. |
| 49699 | + */ |
| 49700 | + if( !ret && GetLastError()==ERROR_IO_PENDING ){ |
| 49701 | + DWORD nDelay = (nMs==0 ? INFINITE : nMs); |
| 49702 | + DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay); |
| 49703 | + if( res==WAIT_OBJECT_0 ){ |
| 49704 | + ret = TRUE; |
| 49705 | + }else if( res==WAIT_TIMEOUT ){ |
| 49706 | + rc = SQLITE_BUSY_TIMEOUT; |
| 49707 | + }else{ |
| 49708 | + /* Some other error has occurred */ |
| 49709 | + rc = SQLITE_IOERR_LOCK; |
| 49710 | + } |
| 49711 | + |
| 49712 | + /* If it is still pending, cancel the LockFileEx() call. */ |
| 49713 | + osCancelIo(hFile); |
| 49714 | + } |
| 49715 | + |
| 49716 | + osCloseHandle(ovlp.hEvent); |
| 49717 | +#endif |
| 49718 | + } |
| 49719 | + |
| 49720 | + if( rc==SQLITE_OK && !ret ){ |
| 49721 | + rc = SQLITE_BUSY; |
| 49722 | + } |
| 49723 | + return rc; |
| 49724 | +} |
| 49324 | 49725 | |
| 49325 | 49726 | /* |
| 49326 | 49727 | ** Unlock a file region. |
| 49327 | 49728 | */ |
| 49328 | 49729 | static BOOL winUnlockFile( |
| | @@ -49350,10 +49751,18 @@ |
| 49350 | 49751 | return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 49351 | 49752 | numBytesHigh); |
| 49352 | 49753 | } |
| 49353 | 49754 | #endif |
| 49354 | 49755 | } |
| 49756 | + |
| 49757 | +/* |
| 49758 | +** Remove an nByte lock starting at offset iOff from HANDLE h. |
| 49759 | +*/ |
| 49760 | +static int winHandleUnlock(HANDLE h, int iOff, int nByte){ |
| 49761 | + BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0); |
| 49762 | + return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK); |
| 49763 | +} |
| 49355 | 49764 | |
| 49356 | 49765 | /***************************************************************************** |
| 49357 | 49766 | ** The next group of routines implement the I/O methods specified |
| 49358 | 49767 | ** by the sqlite3_io_methods object. |
| 49359 | 49768 | ******************************************************************************/ |
| | @@ -49364,69 +49773,73 @@ |
| 49364 | 49773 | #ifndef INVALID_SET_FILE_POINTER |
| 49365 | 49774 | # define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 49366 | 49775 | #endif |
| 49367 | 49776 | |
| 49368 | 49777 | /* |
| 49369 | | -** Move the current position of the file handle passed as the first |
| 49370 | | -** argument to offset iOffset within the file. If successful, return 0. |
| 49371 | | -** Otherwise, set pFile->lastErrno and return non-zero. |
| 49778 | +** Seek the file handle h to offset nByte of the file. |
| 49779 | +** |
| 49780 | +** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite |
| 49781 | +** error code. |
| 49372 | 49782 | */ |
| 49373 | | -static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ |
| 49783 | +static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){ |
| 49784 | + int rc = SQLITE_OK; /* Return value */ |
| 49785 | + |
| 49374 | 49786 | #if !SQLITE_OS_WINRT |
| 49375 | 49787 | LONG upperBits; /* Most sig. 32 bits of new offset */ |
| 49376 | 49788 | LONG lowerBits; /* Least sig. 32 bits of new offset */ |
| 49377 | 49789 | DWORD dwRet; /* Value returned by SetFilePointer() */ |
| 49378 | | - DWORD lastErrno; /* Value returned by GetLastError() */ |
| 49379 | | - |
| 49380 | | - OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset)); |
| 49381 | 49790 | |
| 49382 | 49791 | upperBits = (LONG)((iOffset>>32) & 0x7fffffff); |
| 49383 | 49792 | lowerBits = (LONG)(iOffset & 0xffffffff); |
| 49793 | + |
| 49794 | + dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN); |
| 49384 | 49795 | |
| 49385 | 49796 | /* API oddity: If successful, SetFilePointer() returns a dword |
| 49386 | 49797 | ** containing the lower 32-bits of the new file-offset. Or, if it fails, |
| 49387 | 49798 | ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, |
| 49388 | 49799 | ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine |
| 49389 | 49800 | ** whether an error has actually occurred, it is also necessary to call |
| 49390 | | - ** GetLastError(). |
| 49391 | | - */ |
| 49392 | | - dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 49393 | | - |
| 49394 | | - if( (dwRet==INVALID_SET_FILE_POINTER |
| 49395 | | - && ((lastErrno = osGetLastError())!=NO_ERROR)) ){ |
| 49396 | | - pFile->lastErrno = lastErrno; |
| 49397 | | - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, |
| 49398 | | - "winSeekFile", pFile->zPath); |
| 49399 | | - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); |
| 49400 | | - return 1; |
| 49401 | | - } |
| 49402 | | - |
| 49403 | | - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 49404 | | - return 0; |
| 49405 | | -#else |
| 49406 | | - /* |
| 49407 | | - ** Same as above, except that this implementation works for WinRT. |
| 49408 | | - */ |
| 49409 | | - |
| 49801 | + ** GetLastError(). */ |
| 49802 | + if( dwRet==INVALID_SET_FILE_POINTER ){ |
| 49803 | + DWORD lastErrno = osGetLastError(); |
| 49804 | + if( lastErrno!=NO_ERROR ){ |
| 49805 | + rc = SQLITE_IOERR_SEEK; |
| 49806 | + } |
| 49807 | + } |
| 49808 | +#else |
| 49809 | + /* This implementation works for WinRT. */ |
| 49410 | 49810 | LARGE_INTEGER x; /* The new offset */ |
| 49411 | 49811 | BOOL bRet; /* Value returned by SetFilePointerEx() */ |
| 49412 | 49812 | |
| 49413 | 49813 | x.QuadPart = iOffset; |
| 49414 | | - bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN); |
| 49814 | + bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN); |
| 49415 | 49815 | |
| 49416 | 49816 | if(!bRet){ |
| 49817 | + rc = SQLITE_IOERR_SEEK; |
| 49818 | + } |
| 49819 | +#endif |
| 49820 | + |
| 49821 | + OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc))); |
| 49822 | + return rc; |
| 49823 | +} |
| 49824 | + |
| 49825 | +/* |
| 49826 | +** Move the current position of the file handle passed as the first |
| 49827 | +** argument to offset iOffset within the file. If successful, return 0. |
| 49828 | +** Otherwise, set pFile->lastErrno and return non-zero. |
| 49829 | +*/ |
| 49830 | +static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ |
| 49831 | + int rc; |
| 49832 | + |
| 49833 | + rc = winHandleSeek(pFile->h, iOffset); |
| 49834 | + if( rc!=SQLITE_OK ){ |
| 49417 | 49835 | pFile->lastErrno = osGetLastError(); |
| 49418 | | - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, |
| 49419 | | - "winSeekFile", pFile->zPath); |
| 49420 | | - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); |
| 49421 | | - return 1; |
| 49422 | | - } |
| 49423 | | - |
| 49424 | | - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 49425 | | - return 0; |
| 49426 | | -#endif |
| 49427 | | -} |
| 49836 | + winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath); |
| 49837 | + } |
| 49838 | + return rc; |
| 49839 | +} |
| 49840 | + |
| 49428 | 49841 | |
| 49429 | 49842 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 49430 | 49843 | /* Forward references to VFS helper methods used for memory mapped files */ |
| 49431 | 49844 | static int winMapfile(winFile*, sqlite3_int64); |
| 49432 | 49845 | static int winUnmapfile(winFile*); |
| | @@ -49682,10 +50095,64 @@ |
| 49682 | 50095 | } |
| 49683 | 50096 | OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 49684 | 50097 | osGetCurrentProcessId(), pFile, pFile->h)); |
| 49685 | 50098 | return SQLITE_OK; |
| 49686 | 50099 | } |
| 50100 | + |
| 50101 | +/* |
| 50102 | +** Truncate the file opened by handle h to nByte bytes in size. |
| 50103 | +*/ |
| 50104 | +static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){ |
| 50105 | + int rc = SQLITE_OK; /* Return code */ |
| 50106 | + rc = winHandleSeek(h, nByte); |
| 50107 | + if( rc==SQLITE_OK ){ |
| 50108 | + if( 0==osSetEndOfFile(h) ){ |
| 50109 | + rc = SQLITE_IOERR_TRUNCATE; |
| 50110 | + } |
| 50111 | + } |
| 50112 | + return rc; |
| 50113 | +} |
| 50114 | + |
| 50115 | +/* |
| 50116 | +** Determine the size in bytes of the file opened by the handle passed as |
| 50117 | +** the first argument. |
| 50118 | +*/ |
| 50119 | +static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){ |
| 50120 | + int rc = SQLITE_OK; |
| 50121 | + |
| 50122 | +#if SQLITE_OS_WINRT |
| 50123 | + FILE_STANDARD_INFO info; |
| 50124 | + BOOL b; |
| 50125 | + b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info)); |
| 50126 | + if( b ){ |
| 50127 | + *pnByte = info.EndOfFile.QuadPart; |
| 50128 | + }else{ |
| 50129 | + rc = SQLITE_IOERR_FSTAT; |
| 50130 | + } |
| 50131 | +#else |
| 50132 | + DWORD upperBits = 0; |
| 50133 | + DWORD lowerBits = 0; |
| 50134 | + |
| 50135 | + assert( pnByte ); |
| 50136 | + lowerBits = osGetFileSize(h, &upperBits); |
| 50137 | + *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits; |
| 50138 | + if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){ |
| 50139 | + rc = SQLITE_IOERR_FSTAT; |
| 50140 | + } |
| 50141 | +#endif |
| 50142 | + |
| 50143 | + return rc; |
| 50144 | +} |
| 50145 | + |
| 50146 | +/* |
| 50147 | +** Close the handle passed as the only argument. |
| 50148 | +*/ |
| 50149 | +static void winHandleClose(HANDLE h){ |
| 50150 | + if( h!=INVALID_HANDLE_VALUE ){ |
| 50151 | + osCloseHandle(h); |
| 50152 | + } |
| 50153 | +} |
| 49687 | 50154 | |
| 49688 | 50155 | /* |
| 49689 | 50156 | ** Truncate an open file to a specified size |
| 49690 | 50157 | */ |
| 49691 | 50158 | static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ |
| | @@ -49938,31 +50405,32 @@ |
| 49938 | 50405 | /* |
| 49939 | 50406 | ** Acquire a reader lock. |
| 49940 | 50407 | ** Different API routines are called depending on whether or not this |
| 49941 | 50408 | ** is Win9x or WinNT. |
| 49942 | 50409 | */ |
| 49943 | | -static int winGetReadLock(winFile *pFile){ |
| 50410 | +static int winGetReadLock(winFile *pFile, int bBlock){ |
| 49944 | 50411 | int res; |
| 50412 | + DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0); |
| 49945 | 50413 | OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); |
| 49946 | 50414 | if( osIsNT() ){ |
| 49947 | 50415 | #if SQLITE_OS_WINCE |
| 49948 | 50416 | /* |
| 49949 | 50417 | ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 49950 | 50418 | ** API LockFileEx. |
| 49951 | 50419 | */ |
| 49952 | 50420 | res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0); |
| 49953 | 50421 | #else |
| 49954 | | - res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0, |
| 50422 | + res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0, |
| 49955 | 50423 | SHARED_SIZE, 0); |
| 49956 | 50424 | #endif |
| 49957 | 50425 | } |
| 49958 | 50426 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 49959 | 50427 | else{ |
| 49960 | 50428 | int lk; |
| 49961 | 50429 | sqlite3_randomness(sizeof(lk), &lk); |
| 49962 | 50430 | pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1)); |
| 49963 | | - res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, |
| 50431 | + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask, |
| 49964 | 50432 | SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
| 49965 | 50433 | } |
| 49966 | 50434 | #endif |
| 49967 | 50435 | if( res == 0 ){ |
| 49968 | 50436 | pFile->lastErrno = osGetLastError(); |
| | @@ -50053,50 +50521,66 @@ |
| 50053 | 50521 | */ |
| 50054 | 50522 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 50055 | 50523 | assert( locktype!=PENDING_LOCK ); |
| 50056 | 50524 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 50057 | 50525 | |
| 50058 | | - /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or |
| 50526 | + /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or |
| 50059 | 50527 | ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 50060 | 50528 | ** the PENDING_LOCK byte is temporary. |
| 50061 | 50529 | */ |
| 50062 | 50530 | newLocktype = pFile->locktype; |
| 50063 | | - if( pFile->locktype==NO_LOCK |
| 50064 | | - || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK) |
| 50531 | + if( locktype==SHARED_LOCK |
| 50532 | + || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) |
| 50065 | 50533 | ){ |
| 50066 | 50534 | int cnt = 3; |
| 50067 | | - while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, |
| 50068 | | - PENDING_BYTE, 0, 1, 0))==0 ){ |
| 50535 | + |
| 50536 | + /* Flags for the LockFileEx() call. This should be an exclusive lock if |
| 50537 | + ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to |
| 50538 | + ** obtain SHARED. */ |
| 50539 | + int flags = LOCKFILE_FAIL_IMMEDIATELY; |
| 50540 | + if( locktype==EXCLUSIVE_LOCK ){ |
| 50541 | + flags |= LOCKFILE_EXCLUSIVE_LOCK; |
| 50542 | + } |
| 50543 | + while( cnt>0 ){ |
| 50069 | 50544 | /* Try 3 times to get the pending lock. This is needed to work |
| 50070 | 50545 | ** around problems caused by indexing and/or anti-virus software on |
| 50071 | 50546 | ** Windows systems. |
| 50547 | + ** |
| 50072 | 50548 | ** If you are using this code as a model for alternative VFSes, do not |
| 50073 | | - ** copy this retry logic. It is a hack intended for Windows only. |
| 50074 | | - */ |
| 50549 | + ** copy this retry logic. It is a hack intended for Windows only. */ |
| 50550 | + res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0); |
| 50551 | + if( res ) break; |
| 50552 | + |
| 50075 | 50553 | lastErrno = osGetLastError(); |
| 50076 | 50554 | OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", |
| 50077 | | - pFile->h, cnt, res)); |
| 50555 | + pFile->h, cnt, res |
| 50556 | + )); |
| 50557 | + |
| 50078 | 50558 | if( lastErrno==ERROR_INVALID_HANDLE ){ |
| 50079 | 50559 | pFile->lastErrno = lastErrno; |
| 50080 | 50560 | rc = SQLITE_IOERR_LOCK; |
| 50081 | 50561 | OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", |
| 50082 | | - pFile->h, cnt, sqlite3ErrName(rc))); |
| 50562 | + pFile->h, cnt, sqlite3ErrName(rc) |
| 50563 | + )); |
| 50083 | 50564 | return rc; |
| 50084 | 50565 | } |
| 50085 | | - if( cnt ) sqlite3_win32_sleep(1); |
| 50566 | + |
| 50567 | + cnt--; |
| 50568 | + if( cnt>0 ) sqlite3_win32_sleep(1); |
| 50086 | 50569 | } |
| 50087 | 50570 | gotPendingLock = res; |
| 50088 | | - if( !res ){ |
| 50089 | | - lastErrno = osGetLastError(); |
| 50090 | | - } |
| 50091 | 50571 | } |
| 50092 | 50572 | |
| 50093 | 50573 | /* Acquire a shared lock |
| 50094 | 50574 | */ |
| 50095 | 50575 | if( locktype==SHARED_LOCK && res ){ |
| 50096 | 50576 | assert( pFile->locktype==NO_LOCK ); |
| 50097 | | - res = winGetReadLock(pFile); |
| 50577 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 50578 | + res = winGetReadLock(pFile, pFile->bBlockOnConnect); |
| 50579 | +#else |
| 50580 | + res = winGetReadLock(pFile, 0); |
| 50581 | +#endif |
| 50098 | 50582 | if( res ){ |
| 50099 | 50583 | newLocktype = SHARED_LOCK; |
| 50100 | 50584 | }else{ |
| 50101 | 50585 | lastErrno = osGetLastError(); |
| 50102 | 50586 | } |
| | @@ -50130,11 +50614,11 @@ |
| 50130 | 50614 | SHARED_SIZE, 0); |
| 50131 | 50615 | if( res ){ |
| 50132 | 50616 | newLocktype = EXCLUSIVE_LOCK; |
| 50133 | 50617 | }else{ |
| 50134 | 50618 | lastErrno = osGetLastError(); |
| 50135 | | - winGetReadLock(pFile); |
| 50619 | + winGetReadLock(pFile, 0); |
| 50136 | 50620 | } |
| 50137 | 50621 | } |
| 50138 | 50622 | |
| 50139 | 50623 | /* If we are holding a PENDING lock that ought to be released, then |
| 50140 | 50624 | ** release it now. |
| | @@ -50210,11 +50694,11 @@ |
| 50210 | 50694 | OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n", |
| 50211 | 50695 | pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); |
| 50212 | 50696 | type = pFile->locktype; |
| 50213 | 50697 | if( type>=EXCLUSIVE_LOCK ){ |
| 50214 | 50698 | winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 50215 | | - if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){ |
| 50699 | + if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){ |
| 50216 | 50700 | /* This should never happen. We should always be able to |
| 50217 | 50701 | ** reacquire the read lock */ |
| 50218 | 50702 | rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(), |
| 50219 | 50703 | "winUnlock", pFile->zPath); |
| 50220 | 50704 | } |
| | @@ -50420,10 +50904,32 @@ |
| 50420 | 50904 | } |
| 50421 | 50905 | OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
| 50422 | 50906 | return rc; |
| 50423 | 50907 | } |
| 50424 | 50908 | #endif |
| 50909 | + |
| 50910 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 50911 | + case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 50912 | + int iOld = pFile->iBusyTimeout; |
| 50913 | + int iNew = *(int*)pArg; |
| 50914 | +#if SQLITE_ENABLE_SETLK_TIMEOUT==1 |
| 50915 | + pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew; |
| 50916 | +#elif SQLITE_ENABLE_SETLK_TIMEOUT==2 |
| 50917 | + pFile->iBusyTimeout = (DWORD)(!!iNew); |
| 50918 | +#else |
| 50919 | +# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" |
| 50920 | +#endif |
| 50921 | + *(int*)pArg = iOld; |
| 50922 | + return SQLITE_OK; |
| 50923 | + } |
| 50924 | + case SQLITE_FCNTL_BLOCK_ON_CONNECT: { |
| 50925 | + int iNew = *(int*)pArg; |
| 50926 | + pFile->bBlockOnConnect = iNew; |
| 50927 | + return SQLITE_OK; |
| 50928 | + } |
| 50929 | +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 50930 | + |
| 50425 | 50931 | } |
| 50426 | 50932 | OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
| 50427 | 50933 | return SQLITE_NOTFOUND; |
| 50428 | 50934 | } |
| 50429 | 50935 | |
| | @@ -50500,36 +51006,39 @@ |
| 50500 | 51006 | ** nRef |
| 50501 | 51007 | ** pNext |
| 50502 | 51008 | ** |
| 50503 | 51009 | ** The following fields are read-only after the object is created: |
| 50504 | 51010 | ** |
| 50505 | | -** fid |
| 50506 | 51011 | ** zFilename |
| 50507 | 51012 | ** |
| 50508 | 51013 | ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and |
| 50509 | 51014 | ** winShmMutexHeld() is true when reading or writing any other field |
| 50510 | 51015 | ** in this structure. |
| 50511 | 51016 | ** |
| 51017 | +** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate |
| 51018 | +** the *-shm file if the DMS-locking protocol demands it, and (c) map |
| 51019 | +** regions of the *-shm file into memory using MapViewOfFile() or |
| 51020 | +** similar. Other locks are taken by individual clients using the |
| 51021 | +** winShm.hShm handles. |
| 50512 | 51022 | */ |
| 50513 | 51023 | struct winShmNode { |
| 50514 | 51024 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 50515 | 51025 | char *zFilename; /* Name of the file */ |
| 50516 | | - winFile hFile; /* File handle from winOpen */ |
| 51026 | + HANDLE hSharedShm; /* File handle open on zFilename */ |
| 50517 | 51027 | |
| 51028 | + int isUnlocked; /* DMS lock has not yet been obtained */ |
| 51029 | + int isReadonly; /* True if read-only */ |
| 50518 | 51030 | int szRegion; /* Size of shared-memory regions */ |
| 50519 | 51031 | int nRegion; /* Size of array apRegion */ |
| 50520 | | - u8 isReadonly; /* True if read-only */ |
| 50521 | | - u8 isUnlocked; /* True if no DMS lock held */ |
| 50522 | 51032 | |
| 50523 | 51033 | struct ShmRegion { |
| 50524 | 51034 | HANDLE hMap; /* File handle from CreateFileMapping */ |
| 50525 | 51035 | void *pMap; |
| 50526 | 51036 | } *aRegion; |
| 50527 | 51037 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 50528 | 51038 | |
| 50529 | 51039 | int nRef; /* Number of winShm objects pointing to this */ |
| 50530 | | - winShm *pFirst; /* All winShm objects pointing to this */ |
| 50531 | 51040 | winShmNode *pNext; /* Next in list of all winShmNode objects */ |
| 50532 | 51041 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 50533 | 51042 | u8 nextShmId; /* Next available winShm.id value */ |
| 50534 | 51043 | #endif |
| 50535 | 51044 | }; |
| | @@ -50541,27 +51050,19 @@ |
| 50541 | 51050 | */ |
| 50542 | 51051 | static winShmNode *winShmNodeList = 0; |
| 50543 | 51052 | |
| 50544 | 51053 | /* |
| 50545 | 51054 | ** Structure used internally by this VFS to record the state of an |
| 50546 | | -** open shared memory connection. |
| 50547 | | -** |
| 50548 | | -** The following fields are initialized when this object is created and |
| 50549 | | -** are read-only thereafter: |
| 50550 | | -** |
| 50551 | | -** winShm.pShmNode |
| 50552 | | -** winShm.id |
| 50553 | | -** |
| 50554 | | -** All other fields are read/write. The winShm.pShmNode->mutex must be held |
| 50555 | | -** while accessing any read/write fields. |
| 51055 | +** open shared memory connection. There is one such structure for each |
| 51056 | +** winFile open on a wal mode database. |
| 50556 | 51057 | */ |
| 50557 | 51058 | struct winShm { |
| 50558 | 51059 | winShmNode *pShmNode; /* The underlying winShmNode object */ |
| 50559 | | - winShm *pNext; /* Next winShm with the same winShmNode */ |
| 50560 | | - u8 hasMutex; /* True if holding the winShmNode mutex */ |
| 50561 | 51060 | u16 sharedMask; /* Mask of shared locks held */ |
| 50562 | 51061 | u16 exclMask; /* Mask of exclusive locks held */ |
| 51062 | + HANDLE hShm; /* File-handle on *-shm file. For locking. */ |
| 51063 | + int bReadonly; /* True if hShm is opened read-only */ |
| 50563 | 51064 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 50564 | 51065 | u8 id; /* Id of this connection with its winShmNode */ |
| 50565 | 51066 | #endif |
| 50566 | 51067 | }; |
| 50567 | 51068 | |
| | @@ -50569,54 +51070,10 @@ |
| 50569 | 51070 | ** Constants used for locking |
| 50570 | 51071 | */ |
| 50571 | 51072 | #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 50572 | 51073 | #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
| 50573 | 51074 | |
| 50574 | | -/* |
| 50575 | | -** Apply advisory locks for all n bytes beginning at ofst. |
| 50576 | | -*/ |
| 50577 | | -#define WINSHM_UNLCK 1 |
| 50578 | | -#define WINSHM_RDLCK 2 |
| 50579 | | -#define WINSHM_WRLCK 3 |
| 50580 | | -static int winShmSystemLock( |
| 50581 | | - winShmNode *pFile, /* Apply locks to this open shared-memory segment */ |
| 50582 | | - int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */ |
| 50583 | | - int ofst, /* Offset to first byte to be locked/unlocked */ |
| 50584 | | - int nByte /* Number of bytes to lock or unlock */ |
| 50585 | | -){ |
| 50586 | | - int rc = 0; /* Result code form Lock/UnlockFileEx() */ |
| 50587 | | - |
| 50588 | | - /* Access to the winShmNode object is serialized by the caller */ |
| 50589 | | - assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) ); |
| 50590 | | - |
| 50591 | | - OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n", |
| 50592 | | - pFile->hFile.h, lockType, ofst, nByte)); |
| 50593 | | - |
| 50594 | | - /* Release/Acquire the system-level lock */ |
| 50595 | | - if( lockType==WINSHM_UNLCK ){ |
| 50596 | | - rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0); |
| 50597 | | - }else{ |
| 50598 | | - /* Initialize the locking parameters */ |
| 50599 | | - DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; |
| 50600 | | - if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; |
| 50601 | | - rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0); |
| 50602 | | - } |
| 50603 | | - |
| 50604 | | - if( rc!= 0 ){ |
| 50605 | | - rc = SQLITE_OK; |
| 50606 | | - }else{ |
| 50607 | | - pFile->lastErrno = osGetLastError(); |
| 50608 | | - rc = SQLITE_BUSY; |
| 50609 | | - } |
| 50610 | | - |
| 50611 | | - OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n", |
| 50612 | | - pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" : |
| 50613 | | - "winLockFile", pFile->lastErrno, sqlite3ErrName(rc))); |
| 50614 | | - |
| 50615 | | - return rc; |
| 50616 | | -} |
| 50617 | | - |
| 50618 | 51075 | /* Forward references to VFS methods */ |
| 50619 | 51076 | static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*); |
| 50620 | 51077 | static int winDelete(sqlite3_vfs *,const char*,int); |
| 50621 | 51078 | |
| 50622 | 51079 | /* |
| | @@ -50644,15 +51101,11 @@ |
| 50644 | 51101 | bRc = osCloseHandle(p->aRegion[i].hMap); |
| 50645 | 51102 | OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n", |
| 50646 | 51103 | osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); |
| 50647 | 51104 | UNUSED_VARIABLE_VALUE(bRc); |
| 50648 | 51105 | } |
| 50649 | | - if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){ |
| 50650 | | - SimulateIOErrorBenign(1); |
| 50651 | | - winClose((sqlite3_file *)&p->hFile); |
| 50652 | | - SimulateIOErrorBenign(0); |
| 50653 | | - } |
| 51106 | + winHandleClose(p->hSharedShm); |
| 50654 | 51107 | if( deleteFlag ){ |
| 50655 | 51108 | SimulateIOErrorBenign(1); |
| 50656 | 51109 | sqlite3BeginBenignMalloc(); |
| 50657 | 51110 | winDelete(pVfs, p->zFilename, 0); |
| 50658 | 51111 | sqlite3EndBenignMalloc(); |
| | @@ -50666,46 +51119,166 @@ |
| 50666 | 51119 | } |
| 50667 | 51120 | } |
| 50668 | 51121 | } |
| 50669 | 51122 | |
| 50670 | 51123 | /* |
| 50671 | | -** The DMS lock has not yet been taken on shm file pShmNode. Attempt to |
| 50672 | | -** take it now. Return SQLITE_OK if successful, or an SQLite error |
| 50673 | | -** code otherwise. |
| 50674 | | -** |
| 50675 | | -** If the DMS cannot be locked because this is a readonly_shm=1 |
| 50676 | | -** connection and no other process already holds a lock, return |
| 50677 | | -** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. |
| 51124 | +** The DMS lock has not yet been taken on the shm file associated with |
| 51125 | +** pShmNode. Take the lock. Truncate the *-shm file if required. |
| 51126 | +** Return SQLITE_OK if successful, or an SQLite error code otherwise. |
| 50678 | 51127 | */ |
| 50679 | | -static int winLockSharedMemory(winShmNode *pShmNode){ |
| 50680 | | - int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1); |
| 51128 | +static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){ |
| 51129 | + HANDLE h = pShmNode->hSharedShm; |
| 51130 | + int rc = SQLITE_OK; |
| 50681 | 51131 | |
| 51132 | + assert( sqlite3_mutex_held(pShmNode->mutex) ); |
| 51133 | + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0); |
| 50682 | 51134 | if( rc==SQLITE_OK ){ |
| 51135 | + /* We have an EXCLUSIVE lock on the DMS byte. This means that this |
| 51136 | + ** is the first process to open the file. Truncate it to zero bytes |
| 51137 | + ** in this case. */ |
| 50683 | 51138 | if( pShmNode->isReadonly ){ |
| 50684 | | - pShmNode->isUnlocked = 1; |
| 50685 | | - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); |
| 50686 | | - return SQLITE_READONLY_CANTINIT; |
| 50687 | | - }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){ |
| 50688 | | - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); |
| 50689 | | - return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), |
| 50690 | | - "winLockSharedMemory", pShmNode->zFilename); |
| 50691 | | - } |
| 50692 | | - } |
| 50693 | | - |
| 50694 | | - if( rc==SQLITE_OK ){ |
| 50695 | | - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); |
| 50696 | | - } |
| 50697 | | - |
| 50698 | | - return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1); |
| 50699 | | -} |
| 50700 | | - |
| 50701 | | -/* |
| 50702 | | -** Open the shared-memory area associated with database file pDbFd. |
| 50703 | | -** |
| 50704 | | -** When opening a new shared-memory file, if no other instances of that |
| 50705 | | -** file are currently open, in this process or in other processes, then |
| 50706 | | -** the file must be truncated to zero length or have its header cleared. |
| 51139 | + rc = SQLITE_READONLY_CANTINIT; |
| 51140 | + }else{ |
| 51141 | + rc = winHandleTruncate(h, 0); |
| 51142 | + } |
| 51143 | + |
| 51144 | + /* Release the EXCLUSIVE lock acquired above. */ |
| 51145 | + winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0); |
| 51146 | + }else if( (rc & 0xFF)==SQLITE_BUSY ){ |
| 51147 | + rc = SQLITE_OK; |
| 51148 | + } |
| 51149 | + |
| 51150 | + if( rc==SQLITE_OK ){ |
| 51151 | + /* Take a SHARED lock on the DMS byte. */ |
| 51152 | + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs); |
| 51153 | + if( rc==SQLITE_OK ){ |
| 51154 | + pShmNode->isUnlocked = 0; |
| 51155 | + } |
| 51156 | + } |
| 51157 | + |
| 51158 | + return rc; |
| 51159 | +} |
| 51160 | + |
| 51161 | + |
| 51162 | +/* |
| 51163 | +** Convert a UTF-8 filename into whatever form the underlying |
| 51164 | +** operating system wants filenames in. Space to hold the result |
| 51165 | +** is obtained from malloc and must be freed by the calling |
| 51166 | +** function. |
| 51167 | +*/ |
| 51168 | +static void *winConvertFromUtf8Filename(const char *zFilename){ |
| 51169 | + void *zConverted = 0; |
| 51170 | + if( osIsNT() ){ |
| 51171 | + zConverted = winUtf8ToUnicode(zFilename); |
| 51172 | + } |
| 51173 | +#ifdef SQLITE_WIN32_HAS_ANSI |
| 51174 | + else{ |
| 51175 | + zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI()); |
| 51176 | + } |
| 51177 | +#endif |
| 51178 | + /* caller will handle out of memory */ |
| 51179 | + return zConverted; |
| 51180 | +} |
| 51181 | + |
| 51182 | +/* |
| 51183 | +** This function is used to open a handle on a *-shm file. |
| 51184 | +** |
| 51185 | +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file |
| 51186 | +** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not. |
| 51187 | +*/ |
| 51188 | +static int winHandleOpen( |
| 51189 | + const char *zUtf8, /* File to open */ |
| 51190 | + int *pbReadonly, /* IN/OUT: True for readonly handle */ |
| 51191 | + HANDLE *ph /* OUT: New HANDLE for file */ |
| 51192 | +){ |
| 51193 | + int rc = SQLITE_OK; |
| 51194 | + void *zConverted = 0; |
| 51195 | + int bReadonly = *pbReadonly; |
| 51196 | + HANDLE h = INVALID_HANDLE_VALUE; |
| 51197 | + |
| 51198 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 51199 | + const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED; |
| 51200 | +#else |
| 51201 | + const DWORD flag_overlapped = 0; |
| 51202 | +#endif |
| 51203 | + |
| 51204 | + /* Convert the filename to the system encoding. */ |
| 51205 | + zConverted = winConvertFromUtf8Filename(zUtf8); |
| 51206 | + if( zConverted==0 ){ |
| 51207 | + OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8)); |
| 51208 | + rc = SQLITE_IOERR_NOMEM_BKPT; |
| 51209 | + goto winopenfile_out; |
| 51210 | + } |
| 51211 | + |
| 51212 | + /* Ensure the file we are trying to open is not actually a directory. */ |
| 51213 | + if( winIsDir(zConverted) ){ |
| 51214 | + OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8)); |
| 51215 | + rc = SQLITE_CANTOPEN_ISDIR; |
| 51216 | + goto winopenfile_out; |
| 51217 | + } |
| 51218 |