| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.39.2. By combining all the individual C code files into this |
| 3 | +** version 3.40.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. |
| | @@ -450,13 +450,13 @@ |
| 450 | 450 | ** |
| 451 | 451 | ** See also: [sqlite3_libversion()], |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | | -#define SQLITE_VERSION "3.39.2" |
| 456 | | -#define SQLITE_VERSION_NUMBER 3039002 |
| 457 | | -#define SQLITE_SOURCE_ID "2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21" |
| 455 | +#define SQLITE_VERSION "3.40.0" |
| 456 | +#define SQLITE_VERSION_NUMBER 3040000 |
| 457 | +#define SQLITE_SOURCE_ID "2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -3728,10 +3728,13 @@ |
| 3728 | 3728 | ** |
| 3729 | 3729 | ** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt> |
| 3730 | 3730 | ** <dd>The database is opened [shared cache] enabled, overriding |
| 3731 | 3731 | ** the default shared cache setting provided by |
| 3732 | 3732 | ** [sqlite3_enable_shared_cache()].)^ |
| 3733 | +** The [use of shared cache mode is discouraged] and hence shared cache |
| 3734 | +** capabilities may be omitted from many builds of SQLite. In such cases, |
| 3735 | +** this option is a no-op. |
| 3733 | 3736 | ** |
| 3734 | 3737 | ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt> |
| 3735 | 3738 | ** <dd>The database is opened [shared cache] disabled, overriding |
| 3736 | 3739 | ** the default shared cache setting provided by |
| 3737 | 3740 | ** [sqlite3_enable_shared_cache()].)^ |
| | @@ -3743,11 +3746,11 @@ |
| 3743 | 3746 | ** connection as soon as the connection is created. In addition to setting |
| 3744 | 3747 | ** the extended result code mode, this flag also causes [sqlite3_open_v2()] |
| 3745 | 3748 | ** to return an extended result code.</dd> |
| 3746 | 3749 | ** |
| 3747 | 3750 | ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt> |
| 3748 | | -** <dd>The database filename is not allowed to be a symbolic link</dd> |
| 3751 | +** <dd>The database filename is not allowed to contain a symbolic link</dd> |
| 3749 | 3752 | ** </dl>)^ |
| 3750 | 3753 | ** |
| 3751 | 3754 | ** If the 3rd parameter to sqlite3_open_v2() is not one of the |
| 3752 | 3755 | ** required combinations shown above optionally combined with other |
| 3753 | 3756 | ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits] |
| | @@ -6769,11 +6772,11 @@ |
| 6769 | 6772 | ** |
| 6770 | 6773 | ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback |
| 6771 | 6774 | ** function C that is invoked prior to each autovacuum of the database |
| 6772 | 6775 | ** file. ^The callback is passed a copy of the generic data pointer (P), |
| 6773 | 6776 | ** the schema-name of the attached database that is being autovacuumed, |
| 6774 | | -** the the size of the database file in pages, the number of free pages, |
| 6777 | +** the size of the database file in pages, the number of free pages, |
| 6775 | 6778 | ** and the number of bytes per page, respectively. The callback should |
| 6776 | 6779 | ** return the number of free pages that should be removed by the |
| 6777 | 6780 | ** autovacuum. ^If the callback returns zero, then no autovacuum happens. |
| 6778 | 6781 | ** ^If the value returned is greater than or equal to the number of |
| 6779 | 6782 | ** free pages, then a complete autovacuum happens. |
| | @@ -6889,10 +6892,15 @@ |
| 6889 | 6892 | ** |
| 6890 | 6893 | ** ^(This routine enables or disables the sharing of the database cache |
| 6891 | 6894 | ** and schema data structures between [database connection | connections] |
| 6892 | 6895 | ** to the same database. Sharing is enabled if the argument is true |
| 6893 | 6896 | ** and disabled if the argument is false.)^ |
| 6897 | +** |
| 6898 | +** This interface is omitted if SQLite is compiled with |
| 6899 | +** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE] |
| 6900 | +** compile-time option is recommended because the |
| 6901 | +** [use of shared cache mode is discouraged]. |
| 6894 | 6902 | ** |
| 6895 | 6903 | ** ^Cache sharing is enabled and disabled for an entire process. |
| 6896 | 6904 | ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). |
| 6897 | 6905 | ** In prior versions of SQLite, |
| 6898 | 6906 | ** sharing was enabled or disabled for each thread separately. |
| | @@ -6988,11 +6996,11 @@ |
| 6988 | 6996 | ** ^Setting the heap limits to zero disables the heap limiter mechanism. |
| 6989 | 6997 | ** |
| 6990 | 6998 | ** ^The soft heap limit may not be greater than the hard heap limit. |
| 6991 | 6999 | ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N) |
| 6992 | 7000 | ** is invoked with a value of N that is greater than the hard heap limit, |
| 6993 | | -** the the soft heap limit is set to the value of the hard heap limit. |
| 7001 | +** the soft heap limit is set to the value of the hard heap limit. |
| 6994 | 7002 | ** ^The soft heap limit is automatically enabled whenever the hard heap |
| 6995 | 7003 | ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and |
| 6996 | 7004 | ** the soft heap limit is outside the range of 1..N, then the soft heap |
| 6997 | 7005 | ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the |
| 6998 | 7006 | ** hard heap limit is enabled makes the soft heap limit equal to the |
| | @@ -9283,11 +9291,11 @@ |
| 9283 | 9291 | ** sqlite3_backup_init() is called and before the corresponding call to |
| 9284 | 9292 | ** sqlite3_backup_finish(). SQLite does not currently check to see |
| 9285 | 9293 | ** if the application incorrectly accesses the destination [database connection] |
| 9286 | 9294 | ** and so no error code is reported, but the operations may malfunction |
| 9287 | 9295 | ** nevertheless. Use of the destination database connection while a |
| 9288 | | -** backup is in progress might also also cause a mutex deadlock. |
| 9296 | +** backup is in progress might also cause a mutex deadlock. |
| 9289 | 9297 | ** |
| 9290 | 9298 | ** If running in [shared cache mode], the application must |
| 9291 | 9299 | ** guarantee that the shared cache used by the destination database |
| 9292 | 9300 | ** is not accessed while the backup is running. In practice this means |
| 9293 | 9301 | ** that the application must guarantee that the disk file being |
| | @@ -9711,11 +9719,11 @@ |
| 9711 | 9719 | ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the |
| 9712 | 9720 | ** meaning of each of these checkpoint modes. |
| 9713 | 9721 | */ |
| 9714 | 9722 | #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ |
| 9715 | 9723 | #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ |
| 9716 | | -#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ |
| 9724 | +#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */ |
| 9717 | 9725 | #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ |
| 9718 | 9726 | |
| 9719 | 9727 | /* |
| 9720 | 9728 | ** CAPI3REF: Virtual Table Interface Configuration |
| 9721 | 9729 | ** |
| | @@ -13142,10 +13150,15 @@ |
| 13142 | 13150 | /******** End of fts5.h *********/ |
| 13143 | 13151 | |
| 13144 | 13152 | /************** End of sqlite3.h *********************************************/ |
| 13145 | 13153 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 13146 | 13154 | |
| 13155 | +/* |
| 13156 | +** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory. |
| 13157 | +*/ |
| 13158 | +#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1 |
| 13159 | + |
| 13147 | 13160 | /* |
| 13148 | 13161 | ** Include the configuration header output by 'configure' if we're using the |
| 13149 | 13162 | ** autoconf-based build |
| 13150 | 13163 | */ |
| 13151 | 13164 | #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) |
| | @@ -15553,67 +15566,67 @@ |
| 15553 | 15566 | #define OP_Checkpoint 3 |
| 15554 | 15567 | #define OP_JournalMode 4 |
| 15555 | 15568 | #define OP_Vacuum 5 |
| 15556 | 15569 | #define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */ |
| 15557 | 15570 | #define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */ |
| 15558 | | -#define OP_Goto 8 /* jump */ |
| 15559 | | -#define OP_Gosub 9 /* jump */ |
| 15560 | | -#define OP_InitCoroutine 10 /* jump */ |
| 15561 | | -#define OP_Yield 11 /* jump */ |
| 15562 | | -#define OP_MustBeInt 12 /* jump */ |
| 15563 | | -#define OP_Jump 13 /* jump */ |
| 15564 | | -#define OP_Once 14 /* jump */ |
| 15565 | | -#define OP_If 15 /* jump */ |
| 15566 | | -#define OP_IfNot 16 /* jump */ |
| 15567 | | -#define OP_IsNullOrType 17 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */ |
| 15568 | | -#define OP_IfNullRow 18 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ |
| 15571 | +#define OP_Init 8 /* jump, synopsis: Start at P2 */ |
| 15572 | +#define OP_Goto 9 /* jump */ |
| 15573 | +#define OP_Gosub 10 /* jump */ |
| 15574 | +#define OP_InitCoroutine 11 /* jump */ |
| 15575 | +#define OP_Yield 12 /* jump */ |
| 15576 | +#define OP_MustBeInt 13 /* jump */ |
| 15577 | +#define OP_Jump 14 /* jump */ |
| 15578 | +#define OP_Once 15 /* jump */ |
| 15579 | +#define OP_If 16 /* jump */ |
| 15580 | +#define OP_IfNot 17 /* jump */ |
| 15581 | +#define OP_IsNullOrType 18 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */ |
| 15569 | 15582 | #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ |
| 15570 | | -#define OP_SeekLT 20 /* jump, synopsis: key=r[P3@P4] */ |
| 15571 | | -#define OP_SeekLE 21 /* jump, synopsis: key=r[P3@P4] */ |
| 15572 | | -#define OP_SeekGE 22 /* jump, synopsis: key=r[P3@P4] */ |
| 15573 | | -#define OP_SeekGT 23 /* jump, synopsis: key=r[P3@P4] */ |
| 15574 | | -#define OP_IfNotOpen 24 /* jump, synopsis: if( !csr[P1] ) goto P2 */ |
| 15575 | | -#define OP_IfNoHope 25 /* jump, synopsis: key=r[P3@P4] */ |
| 15576 | | -#define OP_NoConflict 26 /* jump, synopsis: key=r[P3@P4] */ |
| 15577 | | -#define OP_NotFound 27 /* jump, synopsis: key=r[P3@P4] */ |
| 15578 | | -#define OP_Found 28 /* jump, synopsis: key=r[P3@P4] */ |
| 15579 | | -#define OP_SeekRowid 29 /* jump, synopsis: intkey=r[P3] */ |
| 15580 | | -#define OP_NotExists 30 /* jump, synopsis: intkey=r[P3] */ |
| 15581 | | -#define OP_Last 31 /* jump */ |
| 15582 | | -#define OP_IfSmaller 32 /* jump */ |
| 15583 | | -#define OP_SorterSort 33 /* jump */ |
| 15584 | | -#define OP_Sort 34 /* jump */ |
| 15585 | | -#define OP_Rewind 35 /* jump */ |
| 15586 | | -#define OP_SorterNext 36 /* jump */ |
| 15587 | | -#define OP_Prev 37 /* jump */ |
| 15588 | | -#define OP_Next 38 /* jump */ |
| 15589 | | -#define OP_IdxLE 39 /* jump, synopsis: key=r[P3@P4] */ |
| 15590 | | -#define OP_IdxGT 40 /* jump, synopsis: key=r[P3@P4] */ |
| 15591 | | -#define OP_IdxLT 41 /* jump, synopsis: key=r[P3@P4] */ |
| 15592 | | -#define OP_IdxGE 42 /* jump, synopsis: key=r[P3@P4] */ |
| 15583 | +#define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ |
| 15584 | +#define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */ |
| 15585 | +#define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */ |
| 15586 | +#define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */ |
| 15587 | +#define OP_SeekGT 24 /* jump, synopsis: key=r[P3@P4] */ |
| 15588 | +#define OP_IfNotOpen 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */ |
| 15589 | +#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */ |
| 15590 | +#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */ |
| 15591 | +#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */ |
| 15592 | +#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */ |
| 15593 | +#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */ |
| 15594 | +#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */ |
| 15595 | +#define OP_Last 32 /* jump */ |
| 15596 | +#define OP_IfSmaller 33 /* jump */ |
| 15597 | +#define OP_SorterSort 34 /* jump */ |
| 15598 | +#define OP_Sort 35 /* jump */ |
| 15599 | +#define OP_Rewind 36 /* jump */ |
| 15600 | +#define OP_SorterNext 37 /* jump */ |
| 15601 | +#define OP_Prev 38 /* jump */ |
| 15602 | +#define OP_Next 39 /* jump */ |
| 15603 | +#define OP_IdxLE 40 /* jump, synopsis: key=r[P3@P4] */ |
| 15604 | +#define OP_IdxGT 41 /* jump, synopsis: key=r[P3@P4] */ |
| 15605 | +#define OP_IdxLT 42 /* jump, synopsis: key=r[P3@P4] */ |
| 15593 | 15606 | #define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ |
| 15594 | 15607 | #define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */ |
| 15595 | | -#define OP_RowSetRead 45 /* jump, synopsis: r[P3]=rowset(P1) */ |
| 15596 | | -#define OP_RowSetTest 46 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */ |
| 15597 | | -#define OP_Program 47 /* jump */ |
| 15598 | | -#define OP_FkIfZero 48 /* jump, synopsis: if fkctr[P1]==0 goto P2 */ |
| 15599 | | -#define OP_IfPos 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */ |
| 15608 | +#define OP_IdxGE 45 /* jump, synopsis: key=r[P3@P4] */ |
| 15609 | +#define OP_RowSetRead 46 /* jump, synopsis: r[P3]=rowset(P1) */ |
| 15610 | +#define OP_RowSetTest 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */ |
| 15611 | +#define OP_Program 48 /* jump */ |
| 15612 | +#define OP_FkIfZero 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */ |
| 15600 | 15613 | #define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */ |
| 15601 | 15614 | #define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ |
| 15602 | 15615 | #define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */ |
| 15603 | 15616 | #define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */ |
| 15604 | 15617 | #define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */ |
| 15605 | 15618 | #define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */ |
| 15606 | 15619 | #define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */ |
| 15607 | 15620 | #define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */ |
| 15608 | 15621 | #define OP_ElseEq 58 /* jump, same as TK_ESCAPE */ |
| 15609 | | -#define OP_IfNotZero 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */ |
| 15610 | | -#define OP_DecrJumpZero 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */ |
| 15611 | | -#define OP_IncrVacuum 61 /* jump */ |
| 15612 | | -#define OP_VNext 62 /* jump */ |
| 15613 | | -#define OP_Filter 63 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */ |
| 15614 | | -#define OP_Init 64 /* jump, synopsis: Start at P2 */ |
| 15622 | +#define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */ |
| 15623 | +#define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */ |
| 15624 | +#define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */ |
| 15625 | +#define OP_IncrVacuum 62 /* jump */ |
| 15626 | +#define OP_VNext 63 /* jump */ |
| 15627 | +#define OP_Filter 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */ |
| 15615 | 15628 | #define OP_PureFunc 65 /* synopsis: r[P3]=func(r[P2@NP]) */ |
| 15616 | 15629 | #define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */ |
| 15617 | 15630 | #define OP_Return 67 |
| 15618 | 15631 | #define OP_EndCoroutine 68 |
| 15619 | 15632 | #define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */ |
| | @@ -15745,17 +15758,17 @@ |
| 15745 | 15758 | #define OPFLG_IN3 0x08 /* in3: P3 is an input */ |
| 15746 | 15759 | #define OPFLG_OUT2 0x10 /* out2: P2 is an output */ |
| 15747 | 15760 | #define OPFLG_OUT3 0x20 /* out3: P3 is an output */ |
| 15748 | 15761 | #define OPFLG_INITIALIZER {\ |
| 15749 | 15762 | /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ |
| 15750 | | -/* 8 */ 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03,\ |
| 15751 | | -/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x09, 0x09, 0x09, 0x09,\ |
| 15752 | | -/* 24 */ 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,\ |
| 15763 | +/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\ |
| 15764 | +/* 16 */ 0x03, 0x03, 0x03, 0x12, 0x01, 0x09, 0x09, 0x09,\ |
| 15765 | +/* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\ |
| 15753 | 15766 | /* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\ |
| 15754 | | -/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\ |
| 15755 | | -/* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ |
| 15756 | | -/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\ |
| 15767 | +/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\ |
| 15768 | +/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ |
| 15769 | +/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\ |
| 15757 | 15770 | /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\ |
| 15758 | 15771 | /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\ |
| 15759 | 15772 | /* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\ |
| 15760 | 15773 | /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\ |
| 15761 | 15774 | /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\ |
| | @@ -15857,10 +15870,11 @@ |
| 15857 | 15870 | SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); |
| 15858 | 15871 | SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type); |
| 15859 | 15872 | SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*); |
| 15860 | 15873 | SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); |
| 15861 | 15874 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); |
| 15875 | +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetLastOp(Vdbe*); |
| 15862 | 15876 | SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*); |
| 15863 | 15877 | SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*); |
| 15864 | 15878 | SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*); |
| 15865 | 15879 | SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*); |
| 15866 | 15880 | SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*); |
| | @@ -16741,10 +16755,11 @@ |
| 16741 | 16755 | void *pMiddle; /* First byte past end of full-size buffers and |
| 16742 | 16756 | ** the first byte of LOOKASIDE_SMALL buffers */ |
| 16743 | 16757 | #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 16744 | 16758 | void *pStart; /* First byte of available memory space */ |
| 16745 | 16759 | void *pEnd; /* First byte past end of available space */ |
| 16760 | + void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */ |
| 16746 | 16761 | }; |
| 16747 | 16762 | struct LookasideSlot { |
| 16748 | 16763 | LookasideSlot *pNext; /* Next buffer in the list of free buffers */ |
| 16749 | 16764 | }; |
| 16750 | 16765 | |
| | @@ -18189,11 +18204,11 @@ |
| 18189 | 18204 | #define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */ |
| 18190 | 18205 | #define EP_Skip 0x002000 /* Operator does not contribute to affinity */ |
| 18191 | 18206 | #define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ |
| 18192 | 18207 | #define EP_Win 0x008000 /* Contains window functions */ |
| 18193 | 18208 | #define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ |
| 18194 | | -#define EP_MemToken 0x020000 /* Need to sqlite3DbFree() Expr.zToken */ |
| 18209 | + /* 0x020000 // Available for reuse */ |
| 18195 | 18210 | #define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */ |
| 18196 | 18211 | #define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */ |
| 18197 | 18212 | #define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ |
| 18198 | 18213 | #define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */ |
| 18199 | 18214 | #define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */ |
| | @@ -19667,10 +19682,11 @@ |
| 19667 | 19682 | SQLITE_PRIVATE void *sqlite3Realloc(void*, u64); |
| 19668 | 19683 | SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64); |
| 19669 | 19684 | SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64); |
| 19670 | 19685 | SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*); |
| 19671 | 19686 | SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*); |
| 19687 | +SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3*, void*); |
| 19672 | 19688 | SQLITE_PRIVATE int sqlite3MallocSize(const void*); |
| 19673 | 19689 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*); |
| 19674 | 19690 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 19675 | 19691 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 19676 | 19692 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| | @@ -20191,10 +20207,11 @@ |
| 20191 | 20207 | SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); |
| 20192 | 20208 | SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); |
| 20193 | 20209 | SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); |
| 20194 | 20210 | SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); |
| 20195 | 20211 | SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64); |
| 20212 | +SQLITE_PRIVATE i64 sqlite3RealToI64(double); |
| 20196 | 20213 | SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*); |
| 20197 | 20214 | SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8); |
| 20198 | 20215 | SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*); |
| 20199 | 20216 | SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*); |
| 20200 | 20217 | SQLITE_PRIVATE int sqlite3Atoi(const char*); |
| | @@ -21637,13 +21654,10 @@ |
| 21637 | 21654 | "OMIT_WSD", |
| 21638 | 21655 | #endif |
| 21639 | 21656 | #ifdef SQLITE_OMIT_XFER_OPT |
| 21640 | 21657 | "OMIT_XFER_OPT", |
| 21641 | 21658 | #endif |
| 21642 | | -#ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 21643 | | - "PCACHE_SEPARATE_HEADER", |
| 21644 | | -#endif |
| 21645 | 21659 | #ifdef SQLITE_PERFORMANCE_TRACE |
| 21646 | 21660 | "PERFORMANCE_TRACE", |
| 21647 | 21661 | #endif |
| 21648 | 21662 | #ifdef SQLITE_POWERSAFE_OVERWRITE |
| 21649 | 21663 | # if SQLITE_POWERSAFE_OVERWRITE != 1 |
| | @@ -22592,11 +22606,11 @@ |
| 22592 | 22606 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| 22593 | 22607 | ** is really a pointer to an instance of this structure. |
| 22594 | 22608 | */ |
| 22595 | 22609 | struct Vdbe { |
| 22596 | 22610 | sqlite3 *db; /* The database connection that owns this statement */ |
| 22597 | | - Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 22611 | + Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 22598 | 22612 | Parse *pParse; /* Parsing context used to create this Vdbe */ |
| 22599 | 22613 | ynVar nVar; /* Number of entries in aVar[] */ |
| 22600 | 22614 | int nMem; /* Number of memory locations currently allocated */ |
| 22601 | 22615 | int nCursor; /* Number of slots in apCsr[] */ |
| 22602 | 22616 | u32 cacheCtr; /* VdbeCursor row cache generation counter */ |
| | @@ -23150,10 +23164,12 @@ |
| 23150 | 23164 | int i; /* Used to iterate through schemas */ |
| 23151 | 23165 | int nByte = 0; /* Used to accumulate return value */ |
| 23152 | 23166 | |
| 23153 | 23167 | sqlite3BtreeEnterAll(db); |
| 23154 | 23168 | db->pnBytesFreed = &nByte; |
| 23169 | + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 23170 | + db->lookaside.pEnd = db->lookaside.pStart; |
| 23155 | 23171 | for(i=0; i<db->nDb; i++){ |
| 23156 | 23172 | Schema *pSchema = db->aDb[i].pSchema; |
| 23157 | 23173 | if( ALWAYS(pSchema!=0) ){ |
| 23158 | 23174 | HashElem *p; |
| 23159 | 23175 | |
| | @@ -23175,10 +23191,11 @@ |
| 23175 | 23191 | sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); |
| 23176 | 23192 | } |
| 23177 | 23193 | } |
| 23178 | 23194 | } |
| 23179 | 23195 | db->pnBytesFreed = 0; |
| 23196 | + db->lookaside.pEnd = db->lookaside.pTrueEnd; |
| 23180 | 23197 | sqlite3BtreeLeaveAll(db); |
| 23181 | 23198 | |
| 23182 | 23199 | *pHighwater = 0; |
| 23183 | 23200 | *pCurrent = nByte; |
| 23184 | 23201 | break; |
| | @@ -23192,13 +23209,16 @@ |
| 23192 | 23209 | case SQLITE_DBSTATUS_STMT_USED: { |
| 23193 | 23210 | struct Vdbe *pVdbe; /* Used to iterate through VMs */ |
| 23194 | 23211 | int nByte = 0; /* Used to accumulate return value */ |
| 23195 | 23212 | |
| 23196 | 23213 | db->pnBytesFreed = &nByte; |
| 23197 | | - for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ |
| 23214 | + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 23215 | + db->lookaside.pEnd = db->lookaside.pStart; |
| 23216 | + for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ |
| 23198 | 23217 | sqlite3VdbeDelete(pVdbe); |
| 23199 | 23218 | } |
| 23219 | + db->lookaside.pEnd = db->lookaside.pTrueEnd; |
| 23200 | 23220 | db->pnBytesFreed = 0; |
| 23201 | 23221 | |
| 23202 | 23222 | *pHighwater = 0; /* IMP: R-64479-57858 */ |
| 23203 | 23223 | *pCurrent = nByte; |
| 23204 | 23224 | |
| | @@ -23530,11 +23550,11 @@ |
| 23530 | 23550 | X1 = 36525*(Y+4716)/100; |
| 23531 | 23551 | X2 = 306001*(M+1)/10000; |
| 23532 | 23552 | p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); |
| 23533 | 23553 | p->validJD = 1; |
| 23534 | 23554 | if( p->validHMS ){ |
| 23535 | | - p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); |
| 23555 | + p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); |
| 23536 | 23556 | if( p->validTZ ){ |
| 23537 | 23557 | p->iJD -= p->tz*60000; |
| 23538 | 23558 | p->validYMD = 0; |
| 23539 | 23559 | p->validHMS = 0; |
| 23540 | 23560 | p->validTZ = 0; |
| | @@ -24039,11 +24059,11 @@ |
| 24039 | 24059 | ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
| 24040 | 24060 | ** date is already on the appropriate weekday, this is a no-op. |
| 24041 | 24061 | */ |
| 24042 | 24062 | if( sqlite3_strnicmp(z, "weekday ", 8)==0 |
| 24043 | 24063 | && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0 |
| 24044 | | - && (n=(int)r)==r && n>=0 && r<7 ){ |
| 24064 | + && r>=0.0 && r<7.0 && (n=(int)r)==r ){ |
| 24045 | 24065 | sqlite3_int64 Z; |
| 24046 | 24066 | computeYMD_HMS(p); |
| 24047 | 24067 | p->validTZ = 0; |
| 24048 | 24068 | p->validJD = 0; |
| 24049 | 24069 | computeJD(p); |
| | @@ -24837,10 +24857,11 @@ |
| 24837 | 24857 | DO_OS_MALLOC_TEST(0); |
| 24838 | 24858 | /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed |
| 24839 | 24859 | ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, |
| 24840 | 24860 | ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before |
| 24841 | 24861 | ** reaching the VFS. */ |
| 24862 | + assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) ); |
| 24842 | 24863 | rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); |
| 24843 | 24864 | assert( rc==SQLITE_OK || pFile->pMethods==0 ); |
| 24844 | 24865 | return rc; |
| 24845 | 24866 | } |
| 24846 | 24867 | SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| | @@ -29102,11 +29123,11 @@ |
| 29102 | 29123 | /* |
| 29103 | 29124 | ** TRUE if p is a lookaside memory allocation from db |
| 29104 | 29125 | */ |
| 29105 | 29126 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 29106 | 29127 | static int isLookaside(sqlite3 *db, const void *p){ |
| 29107 | | - return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); |
| 29128 | + return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); |
| 29108 | 29129 | } |
| 29109 | 29130 | #else |
| 29110 | 29131 | #define isLookaside(A,B) 0 |
| 29111 | 29132 | #endif |
| 29112 | 29133 | |
| | @@ -29126,22 +29147,20 @@ |
| 29126 | 29147 | #endif |
| 29127 | 29148 | } |
| 29128 | 29149 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){ |
| 29129 | 29150 | assert( p!=0 ); |
| 29130 | 29151 | #ifdef SQLITE_DEBUG |
| 29131 | | - if( db==0 || !isLookaside(db,p) ){ |
| 29132 | | - if( db==0 ){ |
| 29133 | | - assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 29134 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 29135 | | - }else{ |
| 29136 | | - assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29137 | | - assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29138 | | - } |
| 29152 | + if( db==0 ){ |
| 29153 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 29154 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 29155 | + }else if( !isLookaside(db,p) ){ |
| 29156 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29157 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29139 | 29158 | } |
| 29140 | 29159 | #endif |
| 29141 | 29160 | if( db ){ |
| 29142 | | - if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ |
| 29161 | + if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ |
| 29143 | 29162 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 29144 | 29163 | if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ |
| 29145 | 29164 | assert( sqlite3_mutex_held(db->mutex) ); |
| 29146 | 29165 | return LOOKASIDE_SMALL; |
| 29147 | 29166 | } |
| | @@ -29193,18 +29212,15 @@ |
| 29193 | 29212 | */ |
| 29194 | 29213 | SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){ |
| 29195 | 29214 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 29196 | 29215 | assert( p!=0 ); |
| 29197 | 29216 | if( db ){ |
| 29198 | | - if( db->pnBytesFreed ){ |
| 29199 | | - measureAllocationSize(db, p); |
| 29200 | | - return; |
| 29201 | | - } |
| 29202 | 29217 | if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ |
| 29203 | 29218 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 29204 | 29219 | if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ |
| 29205 | 29220 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 29221 | + assert( db->pnBytesFreed==0 ); |
| 29206 | 29222 | #ifdef SQLITE_DEBUG |
| 29207 | 29223 | memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ |
| 29208 | 29224 | #endif |
| 29209 | 29225 | pBuf->pNext = db->lookaside.pSmallFree; |
| 29210 | 29226 | db->lookaside.pSmallFree = pBuf; |
| | @@ -29211,24 +29227,66 @@ |
| 29211 | 29227 | return; |
| 29212 | 29228 | } |
| 29213 | 29229 | #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 29214 | 29230 | if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ |
| 29215 | 29231 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 29232 | + assert( db->pnBytesFreed==0 ); |
| 29216 | 29233 | #ifdef SQLITE_DEBUG |
| 29217 | 29234 | memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ |
| 29218 | 29235 | #endif |
| 29219 | 29236 | pBuf->pNext = db->lookaside.pFree; |
| 29220 | 29237 | db->lookaside.pFree = pBuf; |
| 29221 | 29238 | return; |
| 29222 | 29239 | } |
| 29223 | 29240 | } |
| 29241 | + if( db->pnBytesFreed ){ |
| 29242 | + measureAllocationSize(db, p); |
| 29243 | + return; |
| 29244 | + } |
| 29224 | 29245 | } |
| 29225 | 29246 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29226 | 29247 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29227 | 29248 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 29228 | 29249 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 29229 | 29250 | sqlite3_free(p); |
| 29251 | +} |
| 29252 | +SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3 *db, void *p){ |
| 29253 | + assert( db!=0 ); |
| 29254 | + assert( sqlite3_mutex_held(db->mutex) ); |
| 29255 | + assert( p!=0 ); |
| 29256 | + if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ |
| 29257 | +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 29258 | + if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ |
| 29259 | + LookasideSlot *pBuf = (LookasideSlot*)p; |
| 29260 | + assert( db->pnBytesFreed==0 ); |
| 29261 | +#ifdef SQLITE_DEBUG |
| 29262 | + memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ |
| 29263 | +#endif |
| 29264 | + pBuf->pNext = db->lookaside.pSmallFree; |
| 29265 | + db->lookaside.pSmallFree = pBuf; |
| 29266 | + return; |
| 29267 | + } |
| 29268 | +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 29269 | + if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ |
| 29270 | + LookasideSlot *pBuf = (LookasideSlot*)p; |
| 29271 | + assert( db->pnBytesFreed==0 ); |
| 29272 | +#ifdef SQLITE_DEBUG |
| 29273 | + memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ |
| 29274 | +#endif |
| 29275 | + pBuf->pNext = db->lookaside.pFree; |
| 29276 | + db->lookaside.pFree = pBuf; |
| 29277 | + return; |
| 29278 | + } |
| 29279 | + } |
| 29280 | + if( db->pnBytesFreed ){ |
| 29281 | + measureAllocationSize(db, p); |
| 29282 | + return; |
| 29283 | + } |
| 29284 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29285 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 29286 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 29287 | + sqlite3_free(p); |
| 29230 | 29288 | } |
| 29231 | 29289 | SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ |
| 29232 | 29290 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 29233 | 29291 | if( p ) sqlite3DbFreeNN(db, p); |
| 29234 | 29292 | } |
| | @@ -29561,12 +29619,17 @@ |
| 29561 | 29619 | if( db->nVdbeExec>0 ){ |
| 29562 | 29620 | AtomicStore(&db->u1.isInterrupted, 1); |
| 29563 | 29621 | } |
| 29564 | 29622 | DisableLookaside; |
| 29565 | 29623 | if( db->pParse ){ |
| 29624 | + Parse *pParse; |
| 29566 | 29625 | sqlite3ErrorMsg(db->pParse, "out of memory"); |
| 29567 | 29626 | db->pParse->rc = SQLITE_NOMEM_BKPT; |
| 29627 | + for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ |
| 29628 | + pParse->nErr++; |
| 29629 | + pParse->rc = SQLITE_NOMEM; |
| 29630 | + } |
| 29568 | 29631 | } |
| 29569 | 29632 | } |
| 29570 | 29633 | return 0; |
| 29571 | 29634 | } |
| 29572 | 29635 | |
| | @@ -32332,20 +32395,45 @@ |
| 32332 | 32395 | |
| 32333 | 32396 | /* All threads share a single random number generator. |
| 32334 | 32397 | ** This structure is the current state of the generator. |
| 32335 | 32398 | */ |
| 32336 | 32399 | static SQLITE_WSD struct sqlite3PrngType { |
| 32337 | | - unsigned char isInit; /* True if initialized */ |
| 32338 | | - unsigned char i, j; /* State variables */ |
| 32339 | | - unsigned char s[256]; /* State variables */ |
| 32400 | + u32 s[16]; /* 64 bytes of chacha20 state */ |
| 32401 | + u8 out[64]; /* Output bytes */ |
| 32402 | + u8 n; /* Output bytes remaining */ |
| 32340 | 32403 | } sqlite3Prng; |
| 32341 | 32404 | |
| 32405 | + |
| 32406 | +/* The RFC-7539 ChaCha20 block function |
| 32407 | +*/ |
| 32408 | +#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) |
| 32409 | +#define QR(a, b, c, d) ( \ |
| 32410 | + a += b, d ^= a, d = ROTL(d,16), \ |
| 32411 | + c += d, b ^= c, b = ROTL(b,12), \ |
| 32412 | + a += b, d ^= a, d = ROTL(d, 8), \ |
| 32413 | + c += d, b ^= c, b = ROTL(b, 7)) |
| 32414 | +static void chacha_block(u32 *out, const u32 *in){ |
| 32415 | + int i; |
| 32416 | + u32 x[16]; |
| 32417 | + memcpy(x, in, 64); |
| 32418 | + for(i=0; i<10; i++){ |
| 32419 | + QR(x[0], x[4], x[ 8], x[12]); |
| 32420 | + QR(x[1], x[5], x[ 9], x[13]); |
| 32421 | + QR(x[2], x[6], x[10], x[14]); |
| 32422 | + QR(x[3], x[7], x[11], x[15]); |
| 32423 | + QR(x[0], x[5], x[10], x[15]); |
| 32424 | + QR(x[1], x[6], x[11], x[12]); |
| 32425 | + QR(x[2], x[7], x[ 8], x[13]); |
| 32426 | + QR(x[3], x[4], x[ 9], x[14]); |
| 32427 | + } |
| 32428 | + for(i=0; i<16; i++) out[i] = x[i]+in[i]; |
| 32429 | +} |
| 32430 | + |
| 32342 | 32431 | /* |
| 32343 | 32432 | ** Return N random bytes. |
| 32344 | 32433 | */ |
| 32345 | 32434 | SQLITE_API void sqlite3_randomness(int N, void *pBuf){ |
| 32346 | | - unsigned char t; |
| 32347 | 32435 | unsigned char *zBuf = pBuf; |
| 32348 | 32436 | |
| 32349 | 32437 | /* The "wsdPrng" macro will resolve to the pseudo-random number generator |
| 32350 | 32438 | ** state vector. If writable static data is unsupported on the target, |
| 32351 | 32439 | ** we have to locate the state vector at run-time. In the more common |
| | @@ -32371,57 +32459,50 @@ |
| 32371 | 32459 | mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 32372 | 32460 | #endif |
| 32373 | 32461 | |
| 32374 | 32462 | sqlite3_mutex_enter(mutex); |
| 32375 | 32463 | if( N<=0 || pBuf==0 ){ |
| 32376 | | - wsdPrng.isInit = 0; |
| 32464 | + wsdPrng.s[0] = 0; |
| 32377 | 32465 | sqlite3_mutex_leave(mutex); |
| 32378 | 32466 | return; |
| 32379 | 32467 | } |
| 32380 | 32468 | |
| 32381 | 32469 | /* Initialize the state of the random number generator once, |
| 32382 | | - ** the first time this routine is called. The seed value does |
| 32383 | | - ** not need to contain a lot of randomness since we are not |
| 32384 | | - ** trying to do secure encryption or anything like that... |
| 32385 | | - ** |
| 32386 | | - ** Nothing in this file or anywhere else in SQLite does any kind of |
| 32387 | | - ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 32388 | | - ** number generator) not as an encryption device. |
| 32470 | + ** the first time this routine is called. |
| 32389 | 32471 | */ |
| 32390 | | - if( !wsdPrng.isInit ){ |
| 32472 | + if( wsdPrng.s[0]==0 ){ |
| 32391 | 32473 | sqlite3_vfs *pVfs = sqlite3_vfs_find(0); |
| 32392 | | - int i; |
| 32393 | | - char k[256]; |
| 32394 | | - wsdPrng.j = 0; |
| 32395 | | - wsdPrng.i = 0; |
| 32474 | + static const u32 chacha20_init[] = { |
| 32475 | + 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 |
| 32476 | + }; |
| 32477 | + memcpy(&wsdPrng.s[0], chacha20_init, 16); |
| 32396 | 32478 | if( NEVER(pVfs==0) ){ |
| 32397 | | - memset(k, 0, sizeof(k)); |
| 32479 | + memset(&wsdPrng.s[4], 0, 44); |
| 32398 | 32480 | }else{ |
| 32399 | | - sqlite3OsRandomness(pVfs, 256, k); |
| 32400 | | - } |
| 32401 | | - for(i=0; i<256; i++){ |
| 32402 | | - wsdPrng.s[i] = (u8)i; |
| 32403 | | - } |
| 32404 | | - for(i=0; i<256; i++){ |
| 32405 | | - wsdPrng.j += wsdPrng.s[i] + k[i]; |
| 32406 | | - t = wsdPrng.s[wsdPrng.j]; |
| 32407 | | - wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; |
| 32408 | | - wsdPrng.s[i] = t; |
| 32409 | | - } |
| 32410 | | - wsdPrng.isInit = 1; |
| 32481 | + sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrng.s[4]); |
| 32482 | + } |
| 32483 | + wsdPrng.s[15] = wsdPrng.s[12]; |
| 32484 | + wsdPrng.s[12] = 0; |
| 32485 | + wsdPrng.n = 0; |
| 32411 | 32486 | } |
| 32412 | 32487 | |
| 32413 | 32488 | assert( N>0 ); |
| 32414 | | - do{ |
| 32415 | | - wsdPrng.i++; |
| 32416 | | - t = wsdPrng.s[wsdPrng.i]; |
| 32417 | | - wsdPrng.j += t; |
| 32418 | | - wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; |
| 32419 | | - wsdPrng.s[wsdPrng.j] = t; |
| 32420 | | - t += wsdPrng.s[wsdPrng.i]; |
| 32421 | | - *(zBuf++) = wsdPrng.s[t]; |
| 32422 | | - }while( --N ); |
| 32489 | + while( 1 /* exit by break */ ){ |
| 32490 | + if( N<=wsdPrng.n ){ |
| 32491 | + memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); |
| 32492 | + wsdPrng.n -= N; |
| 32493 | + break; |
| 32494 | + } |
| 32495 | + if( wsdPrng.n>0 ){ |
| 32496 | + memcpy(zBuf, wsdPrng.out, wsdPrng.n); |
| 32497 | + N -= wsdPrng.n; |
| 32498 | + zBuf += wsdPrng.n; |
| 32499 | + } |
| 32500 | + wsdPrng.s[12]++; |
| 32501 | + chacha_block((u32*)wsdPrng.out, wsdPrng.s); |
| 32502 | + wsdPrng.n = 64; |
| 32503 | + } |
| 32423 | 32504 | sqlite3_mutex_leave(mutex); |
| 32424 | 32505 | } |
| 32425 | 32506 | |
| 32426 | 32507 | #ifndef SQLITE_UNTESTABLE |
| 32427 | 32508 | /* |
| | @@ -33457,11 +33538,11 @@ |
| 33457 | 33538 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 33458 | 33539 | char *zMsg; |
| 33459 | 33540 | va_list ap; |
| 33460 | 33541 | sqlite3 *db = pParse->db; |
| 33461 | 33542 | assert( db!=0 ); |
| 33462 | | - assert( db->pParse==pParse ); |
| 33543 | + assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); |
| 33463 | 33544 | db->errByteOffset = -2; |
| 33464 | 33545 | va_start(ap, zFormat); |
| 33465 | 33546 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
| 33466 | 33547 | va_end(ap); |
| 33467 | 33548 | if( db->errByteOffset<-1 ) db->errByteOffset = -1; |
| | @@ -35275,67 +35356,67 @@ |
| 35275 | 35356 | /* 3 */ "Checkpoint" OpHelp(""), |
| 35276 | 35357 | /* 4 */ "JournalMode" OpHelp(""), |
| 35277 | 35358 | /* 5 */ "Vacuum" OpHelp(""), |
| 35278 | 35359 | /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 35279 | 35360 | /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 35280 | | - /* 8 */ "Goto" OpHelp(""), |
| 35281 | | - /* 9 */ "Gosub" OpHelp(""), |
| 35282 | | - /* 10 */ "InitCoroutine" OpHelp(""), |
| 35283 | | - /* 11 */ "Yield" OpHelp(""), |
| 35284 | | - /* 12 */ "MustBeInt" OpHelp(""), |
| 35285 | | - /* 13 */ "Jump" OpHelp(""), |
| 35286 | | - /* 14 */ "Once" OpHelp(""), |
| 35287 | | - /* 15 */ "If" OpHelp(""), |
| 35288 | | - /* 16 */ "IfNot" OpHelp(""), |
| 35289 | | - /* 17 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"), |
| 35290 | | - /* 18 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), |
| 35361 | + /* 8 */ "Init" OpHelp("Start at P2"), |
| 35362 | + /* 9 */ "Goto" OpHelp(""), |
| 35363 | + /* 10 */ "Gosub" OpHelp(""), |
| 35364 | + /* 11 */ "InitCoroutine" OpHelp(""), |
| 35365 | + /* 12 */ "Yield" OpHelp(""), |
| 35366 | + /* 13 */ "MustBeInt" OpHelp(""), |
| 35367 | + /* 14 */ "Jump" OpHelp(""), |
| 35368 | + /* 15 */ "Once" OpHelp(""), |
| 35369 | + /* 16 */ "If" OpHelp(""), |
| 35370 | + /* 17 */ "IfNot" OpHelp(""), |
| 35371 | + /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"), |
| 35291 | 35372 | /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), |
| 35292 | | - /* 20 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 35293 | | - /* 21 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 35294 | | - /* 22 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 35295 | | - /* 23 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 35296 | | - /* 24 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"), |
| 35297 | | - /* 25 */ "IfNoHope" OpHelp("key=r[P3@P4]"), |
| 35298 | | - /* 26 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 35299 | | - /* 27 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 35300 | | - /* 28 */ "Found" OpHelp("key=r[P3@P4]"), |
| 35301 | | - /* 29 */ "SeekRowid" OpHelp("intkey=r[P3]"), |
| 35302 | | - /* 30 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 35303 | | - /* 31 */ "Last" OpHelp(""), |
| 35304 | | - /* 32 */ "IfSmaller" OpHelp(""), |
| 35305 | | - /* 33 */ "SorterSort" OpHelp(""), |
| 35306 | | - /* 34 */ "Sort" OpHelp(""), |
| 35307 | | - /* 35 */ "Rewind" OpHelp(""), |
| 35308 | | - /* 36 */ "SorterNext" OpHelp(""), |
| 35309 | | - /* 37 */ "Prev" OpHelp(""), |
| 35310 | | - /* 38 */ "Next" OpHelp(""), |
| 35311 | | - /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 35312 | | - /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 35313 | | - /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 35314 | | - /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 35373 | + /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), |
| 35374 | + /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 35375 | + /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 35376 | + /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 35377 | + /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 35378 | + /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"), |
| 35379 | + /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"), |
| 35380 | + /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 35381 | + /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 35382 | + /* 29 */ "Found" OpHelp("key=r[P3@P4]"), |
| 35383 | + /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"), |
| 35384 | + /* 31 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 35385 | + /* 32 */ "Last" OpHelp(""), |
| 35386 | + /* 33 */ "IfSmaller" OpHelp(""), |
| 35387 | + /* 34 */ "SorterSort" OpHelp(""), |
| 35388 | + /* 35 */ "Sort" OpHelp(""), |
| 35389 | + /* 36 */ "Rewind" OpHelp(""), |
| 35390 | + /* 37 */ "SorterNext" OpHelp(""), |
| 35391 | + /* 38 */ "Prev" OpHelp(""), |
| 35392 | + /* 39 */ "Next" OpHelp(""), |
| 35393 | + /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 35394 | + /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 35395 | + /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 35315 | 35396 | /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), |
| 35316 | 35397 | /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), |
| 35317 | | - /* 45 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), |
| 35318 | | - /* 46 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), |
| 35319 | | - /* 47 */ "Program" OpHelp(""), |
| 35320 | | - /* 48 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 35321 | | - /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), |
| 35398 | + /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 35399 | + /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), |
| 35400 | + /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), |
| 35401 | + /* 48 */ "Program" OpHelp(""), |
| 35402 | + /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 35322 | 35403 | /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), |
| 35323 | 35404 | /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), |
| 35324 | 35405 | /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"), |
| 35325 | 35406 | /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"), |
| 35326 | 35407 | /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"), |
| 35327 | 35408 | /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"), |
| 35328 | 35409 | /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"), |
| 35329 | 35410 | /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"), |
| 35330 | 35411 | /* 58 */ "ElseEq" OpHelp(""), |
| 35331 | | - /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"), |
| 35332 | | - /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 35333 | | - /* 61 */ "IncrVacuum" OpHelp(""), |
| 35334 | | - /* 62 */ "VNext" OpHelp(""), |
| 35335 | | - /* 63 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"), |
| 35336 | | - /* 64 */ "Init" OpHelp("Start at P2"), |
| 35412 | + /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), |
| 35413 | + /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"), |
| 35414 | + /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 35415 | + /* 62 */ "IncrVacuum" OpHelp(""), |
| 35416 | + /* 63 */ "VNext" OpHelp(""), |
| 35417 | + /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"), |
| 35337 | 35418 | /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"), |
| 35338 | 35419 | /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"), |
| 35339 | 35420 | /* 67 */ "Return" OpHelp(""), |
| 35340 | 35421 | /* 68 */ "EndCoroutine" OpHelp(""), |
| 35341 | 35422 | /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), |
| | @@ -41318,30 +41399,39 @@ |
| 41318 | 41399 | ** pVfs->mxPathname bytes. |
| 41319 | 41400 | */ |
| 41320 | 41401 | static int unixGetTempname(int nBuf, char *zBuf){ |
| 41321 | 41402 | const char *zDir; |
| 41322 | 41403 | int iLimit = 0; |
| 41404 | + int rc = SQLITE_OK; |
| 41323 | 41405 | |
| 41324 | 41406 | /* It's odd to simulate an io-error here, but really this is just |
| 41325 | 41407 | ** using the io-error infrastructure to test that SQLite handles this |
| 41326 | 41408 | ** function failing. |
| 41327 | 41409 | */ |
| 41328 | 41410 | zBuf[0] = 0; |
| 41329 | 41411 | SimulateIOError( return SQLITE_IOERR ); |
| 41330 | 41412 | |
| 41413 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 41331 | 41414 | zDir = unixTempFileDir(); |
| 41332 | | - if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
| 41333 | | - do{ |
| 41334 | | - u64 r; |
| 41335 | | - sqlite3_randomness(sizeof(r), &r); |
| 41336 | | - assert( nBuf>2 ); |
| 41337 | | - zBuf[nBuf-2] = 0; |
| 41338 | | - sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 41339 | | - zDir, r, 0); |
| 41340 | | - if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
| 41341 | | - }while( osAccess(zBuf,0)==0 ); |
| 41342 | | - return SQLITE_OK; |
| 41415 | + if( zDir==0 ){ |
| 41416 | + rc = SQLITE_IOERR_GETTEMPPATH; |
| 41417 | + }else{ |
| 41418 | + do{ |
| 41419 | + u64 r; |
| 41420 | + sqlite3_randomness(sizeof(r), &r); |
| 41421 | + assert( nBuf>2 ); |
| 41422 | + zBuf[nBuf-2] = 0; |
| 41423 | + sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 41424 | + zDir, r, 0); |
| 41425 | + if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){ |
| 41426 | + rc = SQLITE_ERROR; |
| 41427 | + break; |
| 41428 | + } |
| 41429 | + }while( osAccess(zBuf,0)==0 ); |
| 41430 | + } |
| 41431 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 41432 | + return rc; |
| 41343 | 41433 | } |
| 41344 | 41434 | |
| 41345 | 41435 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 41346 | 41436 | /* |
| 41347 | 41437 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| | @@ -43512,11 +43602,16 @@ |
| 43512 | 43602 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
| 43513 | 43603 | assert( ArraySize(aSyscall)==29 ); |
| 43514 | 43604 | |
| 43515 | 43605 | /* Register all VFSes defined in the aVfs[] array */ |
| 43516 | 43606 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
| 43607 | +#ifdef SQLITE_DEFAULT_UNIX_VFS |
| 43608 | + sqlite3_vfs_register(&aVfs[i], |
| 43609 | + 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS)); |
| 43610 | +#else |
| 43517 | 43611 | sqlite3_vfs_register(&aVfs[i], i==0); |
| 43612 | +#endif |
| 43518 | 43613 | } |
| 43519 | 43614 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
| 43520 | 43615 | |
| 43521 | 43616 | #ifndef SQLITE_OMIT_WAL |
| 43522 | 43617 | /* Validate lock assumptions */ |
| | @@ -45480,10 +45575,11 @@ |
| 45480 | 45575 | char **ppDirectory = 0; |
| 45481 | 45576 | #ifndef SQLITE_OMIT_AUTOINIT |
| 45482 | 45577 | int rc = sqlite3_initialize(); |
| 45483 | 45578 | if( rc ) return rc; |
| 45484 | 45579 | #endif |
| 45580 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 45485 | 45581 | if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){ |
| 45486 | 45582 | ppDirectory = &sqlite3_data_directory; |
| 45487 | 45583 | }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){ |
| 45488 | 45584 | ppDirectory = &sqlite3_temp_directory; |
| 45489 | 45585 | } |
| | @@ -45494,18 +45590,23 @@ |
| 45494 | 45590 | if( ppDirectory ){ |
| 45495 | 45591 | char *zCopy = 0; |
| 45496 | 45592 | if( zValue && zValue[0] ){ |
| 45497 | 45593 | zCopy = sqlite3_mprintf("%s", zValue); |
| 45498 | 45594 | if ( zCopy==0 ){ |
| 45499 | | - return SQLITE_NOMEM_BKPT; |
| 45595 | + rc = SQLITE_NOMEM_BKPT; |
| 45596 | + goto set_directory8_done; |
| 45500 | 45597 | } |
| 45501 | 45598 | } |
| 45502 | 45599 | sqlite3_free(*ppDirectory); |
| 45503 | 45600 | *ppDirectory = zCopy; |
| 45504 | | - return SQLITE_OK; |
| 45601 | + rc = SQLITE_OK; |
| 45602 | + }else{ |
| 45603 | + rc = SQLITE_ERROR; |
| 45505 | 45604 | } |
| 45506 | | - return SQLITE_ERROR; |
| 45605 | +set_directory8_done: |
| 45606 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 45607 | + return rc; |
| 45507 | 45608 | } |
| 45508 | 45609 | |
| 45509 | 45610 | /* |
| 45510 | 45611 | ** This function is the same as sqlite3_win32_set_directory (below); however, |
| 45511 | 45612 | ** it accepts a UTF-16 string. |
| | @@ -48274,10 +48375,22 @@ |
| 48274 | 48375 | } |
| 48275 | 48376 | } |
| 48276 | 48377 | } |
| 48277 | 48378 | return 0; |
| 48278 | 48379 | } |
| 48380 | + |
| 48381 | +/* |
| 48382 | +** If sqlite3_temp_directory is not, take the mutex and return true. |
| 48383 | +** |
| 48384 | +** If sqlite3_temp_directory is NULL, omit the mutex and return false. |
| 48385 | +*/ |
| 48386 | +static int winTempDirDefined(void){ |
| 48387 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 48388 | + if( sqlite3_temp_directory!=0 ) return 1; |
| 48389 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 48390 | + return 0; |
| 48391 | +} |
| 48279 | 48392 | |
| 48280 | 48393 | /* |
| 48281 | 48394 | ** Create a temporary file name and store the resulting pointer into pzBuf. |
| 48282 | 48395 | ** The pointer returned in pzBuf must be freed via sqlite3_free(). |
| 48283 | 48396 | */ |
| | @@ -48311,24 +48424,27 @@ |
| 48311 | 48424 | ** has been explicitly set by the application; otherwise, use the one |
| 48312 | 48425 | ** configured by the operating system. |
| 48313 | 48426 | */ |
| 48314 | 48427 | nDir = nMax - (nPre + 15); |
| 48315 | 48428 | assert( nDir>0 ); |
| 48316 | | - if( sqlite3_temp_directory ){ |
| 48429 | + if( winTempDirDefined() ){ |
| 48317 | 48430 | int nDirLen = sqlite3Strlen30(sqlite3_temp_directory); |
| 48318 | 48431 | if( nDirLen>0 ){ |
| 48319 | 48432 | if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){ |
| 48320 | 48433 | nDirLen++; |
| 48321 | 48434 | } |
| 48322 | 48435 | if( nDirLen>nDir ){ |
| 48436 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 48323 | 48437 | sqlite3_free(zBuf); |
| 48324 | 48438 | OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 48325 | 48439 | return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0); |
| 48326 | 48440 | } |
| 48327 | 48441 | sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory); |
| 48328 | 48442 | } |
| 48443 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 48329 | 48444 | } |
| 48445 | + |
| 48330 | 48446 | #if defined(__CYGWIN__) |
| 48331 | 48447 | else{ |
| 48332 | 48448 | static const char *azDirs[] = { |
| 48333 | 48449 | 0, /* getenv("SQLITE_TMPDIR") */ |
| 48334 | 48450 | 0, /* getenv("TMPDIR") */ |
| | @@ -49113,11 +49229,11 @@ |
| 49113 | 49229 | /* |
| 49114 | 49230 | ** Turn a relative pathname into a full pathname. Write the full |
| 49115 | 49231 | ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname |
| 49116 | 49232 | ** bytes in size. |
| 49117 | 49233 | */ |
| 49118 | | -static int winFullPathname( |
| 49234 | +static int winFullPathnameNoMutex( |
| 49119 | 49235 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 49120 | 49236 | const char *zRelative, /* Possibly relative input path */ |
| 49121 | 49237 | int nFull, /* Size of output buffer in bytes */ |
| 49122 | 49238 | char *zFull /* Output buffer */ |
| 49123 | 49239 | ){ |
| | @@ -49291,10 +49407,23 @@ |
| 49291 | 49407 | return SQLITE_OK; |
| 49292 | 49408 | }else{ |
| 49293 | 49409 | return SQLITE_IOERR_NOMEM_BKPT; |
| 49294 | 49410 | } |
| 49295 | 49411 | #endif |
| 49412 | +} |
| 49413 | +static int winFullPathname( |
| 49414 | + sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 49415 | + const char *zRelative, /* Possibly relative input path */ |
| 49416 | + int nFull, /* Size of output buffer in bytes */ |
| 49417 | + char *zFull /* Output buffer */ |
| 49418 | +){ |
| 49419 | + int rc; |
| 49420 | + sqlite3_mutex *pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); |
| 49421 | + sqlite3_mutex_enter(pMutex); |
| 49422 | + rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull); |
| 49423 | + sqlite3_mutex_leave(pMutex); |
| 49424 | + return rc; |
| 49296 | 49425 | } |
| 49297 | 49426 | |
| 49298 | 49427 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 49299 | 49428 | /* |
| 49300 | 49429 | ** Interfaces for opening a shared library, finding entry points |
| | @@ -51080,39 +51209,58 @@ |
| 51080 | 51209 | */ |
| 51081 | 51210 | #if defined(SQLITE_DEBUG) && 0 |
| 51082 | 51211 | int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ |
| 51083 | 51212 | int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ |
| 51084 | 51213 | # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} |
| 51085 | | - void pcacheDump(PCache *pCache){ |
| 51086 | | - int N; |
| 51087 | | - int i, j; |
| 51088 | | - sqlite3_pcache_page *pLower; |
| 51214 | + static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){ |
| 51089 | 51215 | PgHdr *pPg; |
| 51090 | 51216 | unsigned char *a; |
| 51217 | + int j; |
| 51218 | + pPg = (PgHdr*)pLower->pExtra; |
| 51219 | + printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); |
| 51220 | + a = (unsigned char *)pLower->pBuf; |
| 51221 | + for(j=0; j<12; j++) printf("%02x", a[j]); |
| 51222 | + printf(" ptr %p\n", pPg); |
| 51223 | + } |
| 51224 | + static void pcacheDump(PCache *pCache){ |
| 51225 | + int N; |
| 51226 | + int i; |
| 51227 | + sqlite3_pcache_page *pLower; |
| 51091 | 51228 | |
| 51092 | 51229 | if( sqlite3PcacheTrace<2 ) return; |
| 51093 | 51230 | if( pCache->pCache==0 ) return; |
| 51094 | 51231 | N = sqlite3PcachePagecount(pCache); |
| 51095 | 51232 | if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; |
| 51096 | 51233 | for(i=1; i<=N; i++){ |
| 51097 | 51234 | pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); |
| 51098 | 51235 | if( pLower==0 ) continue; |
| 51099 | | - pPg = (PgHdr*)pLower->pExtra; |
| 51100 | | - printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); |
| 51101 | | - a = (unsigned char *)pLower->pBuf; |
| 51102 | | - for(j=0; j<12; j++) printf("%02x", a[j]); |
| 51103 | | - printf("\n"); |
| 51104 | | - if( pPg->pPage==0 ){ |
| 51236 | + pcachePageTrace(i, pLower); |
| 51237 | + if( ((PgHdr*)pLower)->pPage==0 ){ |
| 51105 | 51238 | sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); |
| 51106 | 51239 | } |
| 51107 | 51240 | } |
| 51108 | 51241 | } |
| 51109 | | - #else |
| 51242 | +#else |
| 51110 | 51243 | # define pcacheTrace(X) |
| 51244 | +# define pcachePageTrace(PGNO, X) |
| 51111 | 51245 | # define pcacheDump(X) |
| 51112 | 51246 | #endif |
| 51113 | 51247 | |
| 51248 | +/* |
| 51249 | +** Return 1 if pPg is on the dirty list for pCache. Return 0 if not. |
| 51250 | +** This routine runs inside of assert() statements only. |
| 51251 | +*/ |
| 51252 | +#ifdef SQLITE_DEBUG |
| 51253 | +static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){ |
| 51254 | + PgHdr *p; |
| 51255 | + for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 51256 | + if( p==pPg ) return 1; |
| 51257 | + } |
| 51258 | + return 0; |
| 51259 | +} |
| 51260 | +#endif |
| 51261 | + |
| 51114 | 51262 | /* |
| 51115 | 51263 | ** Check invariants on a PgHdr entry. Return true if everything is OK. |
| 51116 | 51264 | ** Return false if any invariant is violated. |
| 51117 | 51265 | ** |
| 51118 | 51266 | ** This routine is for use inside of assert() statements only. For |
| | @@ -51127,12 +51275,17 @@ |
| 51127 | 51275 | assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */ |
| 51128 | 51276 | pCache = pPg->pCache; |
| 51129 | 51277 | assert( pCache!=0 ); /* Every page has an associated PCache */ |
| 51130 | 51278 | if( pPg->flags & PGHDR_CLEAN ){ |
| 51131 | 51279 | assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ |
| 51132 | | - assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */ |
| 51133 | | - assert( pCache->pDirtyTail!=pPg ); |
| 51280 | + assert( !pageOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirty list */ |
| 51281 | + }else{ |
| 51282 | + assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */ |
| 51283 | + assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg ); |
| 51284 | + assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg ); |
| 51285 | + assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg ); |
| 51286 | + assert( pageOnDirtyList(pCache, pPg) ); |
| 51134 | 51287 | } |
| 51135 | 51288 | /* WRITEABLE pages must also be DIRTY */ |
| 51136 | 51289 | if( pPg->flags & PGHDR_WRITEABLE ){ |
| 51137 | 51290 | assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ |
| 51138 | 51291 | } |
| | @@ -51402,12 +51555,13 @@ |
| 51402 | 51555 | eCreate = createFlag & pCache->eCreate; |
| 51403 | 51556 | assert( eCreate==0 || eCreate==1 || eCreate==2 ); |
| 51404 | 51557 | assert( createFlag==0 || pCache->eCreate==eCreate ); |
| 51405 | 51558 | assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); |
| 51406 | 51559 | pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); |
| 51407 | | - pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno, |
| 51560 | + pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno, |
| 51408 | 51561 | createFlag?" create":"",pRes)); |
| 51562 | + pcachePageTrace(pgno, pRes); |
| 51409 | 51563 | return pRes; |
| 51410 | 51564 | } |
| 51411 | 51565 | |
| 51412 | 51566 | /* |
| 51413 | 51567 | ** If the sqlite3PcacheFetch() routine is unable to allocate a new |
| | @@ -51531,10 +51685,11 @@ |
| 51531 | 51685 | if( (--p->nRef)==0 ){ |
| 51532 | 51686 | if( p->flags&PGHDR_CLEAN ){ |
| 51533 | 51687 | pcacheUnpin(p); |
| 51534 | 51688 | }else{ |
| 51535 | 51689 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
| 51690 | + assert( sqlite3PcachePageSanity(p) ); |
| 51536 | 51691 | } |
| 51537 | 51692 | } |
| 51538 | 51693 | } |
| 51539 | 51694 | |
| 51540 | 51695 | /* |
| | @@ -51574,10 +51729,11 @@ |
| 51574 | 51729 | if( p->flags & PGHDR_CLEAN ){ |
| 51575 | 51730 | p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); |
| 51576 | 51731 | pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); |
| 51577 | 51732 | assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); |
| 51578 | 51733 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); |
| 51734 | + assert( sqlite3PcachePageSanity(p) ); |
| 51579 | 51735 | } |
| 51580 | 51736 | assert( sqlite3PcachePageSanity(p) ); |
| 51581 | 51737 | } |
| 51582 | 51738 | } |
| 51583 | 51739 | |
| | @@ -51636,18 +51792,28 @@ |
| 51636 | 51792 | /* |
| 51637 | 51793 | ** Change the page number of page p to newPgno. |
| 51638 | 51794 | */ |
| 51639 | 51795 | SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ |
| 51640 | 51796 | PCache *pCache = p->pCache; |
| 51797 | + sqlite3_pcache_page *pOther; |
| 51641 | 51798 | assert( p->nRef>0 ); |
| 51642 | 51799 | assert( newPgno>0 ); |
| 51643 | 51800 | assert( sqlite3PcachePageSanity(p) ); |
| 51644 | 51801 | pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); |
| 51802 | + pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0); |
| 51645 | 51803 | sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); |
| 51804 | + if( pOther ){ |
| 51805 | + PgHdr *pPg = (PgHdr*)pOther->pExtra; |
| 51806 | + pPg->pgno = p->pgno; |
| 51807 | + if( pPg->pPage==0 ){ |
| 51808 | + sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pOther, 0); |
| 51809 | + } |
| 51810 | + } |
| 51646 | 51811 | p->pgno = newPgno; |
| 51647 | 51812 | if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ |
| 51648 | 51813 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
| 51814 | + assert( sqlite3PcachePageSanity(p) ); |
| 51649 | 51815 | } |
| 51650 | 51816 | } |
| 51651 | 51817 | |
| 51652 | 51818 | /* |
| 51653 | 51819 | ** Drop every cache entry whose page number is greater than "pgno". The |
| | @@ -51941,16 +52107,17 @@ |
| 51941 | 52107 | ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The |
| 51942 | 52108 | ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this |
| 51943 | 52109 | ** size can vary according to architecture, compile-time options, and |
| 51944 | 52110 | ** SQLite library version number. |
| 51945 | 52111 | ** |
| 51946 | | -** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained |
| 51947 | | -** using a separate memory allocation from the database page content. This |
| 51948 | | -** seeks to overcome the "clownshoe" problem (also called "internal |
| 51949 | | -** fragmentation" in academic literature) of allocating a few bytes more |
| 51950 | | -** than a power of two with the memory allocator rounding up to the next |
| 51951 | | -** power of two, and leaving the rounded-up space unused. |
| 52112 | +** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER |
| 52113 | +** was defined, then the page content would be held in a separate memory |
| 52114 | +** allocation from the PgHdr1. This was intended to avoid clownshoe memory |
| 52115 | +** allocations. However, the btree layer needs a small (16-byte) overrun |
| 52116 | +** area after the page content buffer. The header serves as that overrun |
| 52117 | +** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid |
| 52118 | +** any possibility of a memory error. |
| 51952 | 52119 | ** |
| 51953 | 52120 | ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates |
| 51954 | 52121 | ** with this module. Information is passed back and forth as PgHdr1 pointers. |
| 51955 | 52122 | ** |
| 51956 | 52123 | ** The pcache.c and pager.c modules deal pointers to PgHdr objects. |
| | @@ -51991,34 +52158,44 @@ |
| 51991 | 52158 | typedef struct PgFreeslot PgFreeslot; |
| 51992 | 52159 | typedef struct PGroup PGroup; |
| 51993 | 52160 | |
| 51994 | 52161 | /* |
| 51995 | 52162 | ** Each cache entry is represented by an instance of the following |
| 51996 | | -** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of |
| 51997 | | -** PgHdr1.pCache->szPage bytes is allocated directly before this structure |
| 51998 | | -** in memory. |
| 52163 | +** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated |
| 52164 | +** directly before this structure and is used to cache the page content. |
| 51999 | 52165 | ** |
| 52000 | | -** Note: Variables isBulkLocal and isAnchor were once type "u8". That works, |
| 52166 | +** When reading a corrupt database file, it is possible that SQLite might |
| 52167 | +** read a few bytes (no more than 16 bytes) past the end of the page buffer. |
| 52168 | +** It will only read past the end of the page buffer, never write. This |
| 52169 | +** object is positioned immediately after the page buffer to serve as an |
| 52170 | +** overrun area, so that overreads are harmless. |
| 52171 | +** |
| 52172 | +** Variables isBulkLocal and isAnchor were once type "u8". That works, |
| 52001 | 52173 | ** but causes a 2-byte gap in the structure for most architectures (since |
| 52002 | 52174 | ** pointers must be either 4 or 8-byte aligned). As this structure is located |
| 52003 | 52175 | ** in memory directly after the associated page data, if the database is |
| 52004 | 52176 | ** corrupt, code at the b-tree layer may overread the page buffer and |
| 52005 | 52177 | ** read part of this structure before the corruption is detected. This |
| 52006 | 52178 | ** can cause a valgrind error if the unitialized gap is accessed. Using u16 |
| 52007 | | -** ensures there is no such gap, and therefore no bytes of unitialized memory |
| 52008 | | -** in the structure. |
| 52179 | +** ensures there is no such gap, and therefore no bytes of uninitialized |
| 52180 | +** memory in the structure. |
| 52181 | +** |
| 52182 | +** The pLruNext and pLruPrev pointers form a double-linked circular list |
| 52183 | +** of all pages that are unpinned. The PGroup.lru element (which should be |
| 52184 | +** the only element on the list with PgHdr1.isAnchor set to 1) forms the |
| 52185 | +** beginning and the end of the list. |
| 52009 | 52186 | */ |
| 52010 | 52187 | struct PgHdr1 { |
| 52011 | | - sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ |
| 52012 | | - unsigned int iKey; /* Key value (page number) */ |
| 52013 | | - u16 isBulkLocal; /* This page from bulk local storage */ |
| 52014 | | - u16 isAnchor; /* This is the PGroup.lru element */ |
| 52015 | | - PgHdr1 *pNext; /* Next in hash table chain */ |
| 52016 | | - PCache1 *pCache; /* Cache that currently owns this page */ |
| 52017 | | - PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ |
| 52018 | | - PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 52019 | | - /* NB: pLruPrev is only valid if pLruNext!=0 */ |
| 52188 | + sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ |
| 52189 | + unsigned int iKey; /* Key value (page number) */ |
| 52190 | + u16 isBulkLocal; /* This page from bulk local storage */ |
| 52191 | + u16 isAnchor; /* This is the PGroup.lru element */ |
| 52192 | + PgHdr1 *pNext; /* Next in hash table chain */ |
| 52193 | + PCache1 *pCache; /* Cache that currently owns this page */ |
| 52194 | + PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */ |
| 52195 | + PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 52196 | + /* NB: pLruPrev is only valid if pLruNext!=0 */ |
| 52020 | 52197 | }; |
| 52021 | 52198 | |
| 52022 | 52199 | /* |
| 52023 | 52200 | ** A page is pinned if it is not on the LRU list. To be "pinned" means |
| 52024 | 52201 | ** that the page is in active use and must not be deallocated. |
| | @@ -52340,29 +52517,17 @@ |
| 52340 | 52517 | assert( pcache1.separateCache==0 ); |
| 52341 | 52518 | assert( pCache->pGroup==&pcache1.grp ); |
| 52342 | 52519 | pcache1LeaveMutex(pCache->pGroup); |
| 52343 | 52520 | #endif |
| 52344 | 52521 | if( benignMalloc ){ sqlite3BeginBenignMalloc(); } |
| 52345 | | -#ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 52346 | | - pPg = pcache1Alloc(pCache->szPage); |
| 52347 | | - p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); |
| 52348 | | - if( !pPg || !p ){ |
| 52349 | | - pcache1Free(pPg); |
| 52350 | | - sqlite3_free(p); |
| 52351 | | - pPg = 0; |
| 52352 | | - } |
| 52353 | | -#else |
| 52354 | 52522 | pPg = pcache1Alloc(pCache->szAlloc); |
| 52355 | | -#endif |
| 52356 | 52523 | if( benignMalloc ){ sqlite3EndBenignMalloc(); } |
| 52357 | 52524 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 52358 | 52525 | pcache1EnterMutex(pCache->pGroup); |
| 52359 | 52526 | #endif |
| 52360 | 52527 | if( pPg==0 ) return 0; |
| 52361 | | -#ifndef SQLITE_PCACHE_SEPARATE_HEADER |
| 52362 | 52528 | p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; |
| 52363 | | -#endif |
| 52364 | 52529 | p->page.pBuf = pPg; |
| 52365 | 52530 | p->page.pExtra = &p[1]; |
| 52366 | 52531 | p->isBulkLocal = 0; |
| 52367 | 52532 | p->isAnchor = 0; |
| 52368 | 52533 | p->pLruPrev = 0; /* Initializing this saves a valgrind error */ |
| | @@ -52382,13 +52547,10 @@ |
| 52382 | 52547 | if( p->isBulkLocal ){ |
| 52383 | 52548 | p->pNext = pCache->pFree; |
| 52384 | 52549 | pCache->pFree = p; |
| 52385 | 52550 | }else{ |
| 52386 | 52551 | pcache1Free(p->page.pBuf); |
| 52387 | | -#ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 52388 | | - sqlite3_free(p); |
| 52389 | | -#endif |
| 52390 | 52552 | } |
| 52391 | 52553 | (*pCache->pnPurgeable)--; |
| 52392 | 52554 | } |
| 52393 | 52555 | |
| 52394 | 52556 | /* |
| | @@ -53025,27 +53187,45 @@ |
| 53025 | 53187 | unsigned int iNew |
| 53026 | 53188 | ){ |
| 53027 | 53189 | PCache1 *pCache = (PCache1 *)p; |
| 53028 | 53190 | PgHdr1 *pPage = (PgHdr1 *)pPg; |
| 53029 | 53191 | PgHdr1 **pp; |
| 53030 | | - unsigned int h; |
| 53192 | + unsigned int hOld, hNew; |
| 53031 | 53193 | assert( pPage->iKey==iOld ); |
| 53032 | 53194 | assert( pPage->pCache==pCache ); |
| 53195 | + assert( iOld!=iNew ); /* The page number really is changing */ |
| 53033 | 53196 | |
| 53034 | 53197 | pcache1EnterMutex(pCache->pGroup); |
| 53035 | 53198 | |
| 53036 | | - h = iOld%pCache->nHash; |
| 53037 | | - pp = &pCache->apHash[h]; |
| 53199 | + assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */ |
| 53200 | + hOld = iOld%pCache->nHash; |
| 53201 | + pp = &pCache->apHash[hOld]; |
| 53038 | 53202 | while( (*pp)!=pPage ){ |
| 53039 | 53203 | pp = &(*pp)->pNext; |
| 53040 | 53204 | } |
| 53041 | 53205 | *pp = pPage->pNext; |
| 53042 | 53206 | |
| 53043 | | - h = iNew%pCache->nHash; |
| 53207 | + hNew = iNew%pCache->nHash; |
| 53208 | + pp = &pCache->apHash[hNew]; |
| 53209 | + while( *pp ){ |
| 53210 | + if( (*pp)->iKey==iNew ){ |
| 53211 | + /* If there is already another pcache entry at iNew, change it to iOld, |
| 53212 | + ** thus swapping the positions of iNew and iOld */ |
| 53213 | + PgHdr1 *pOld = *pp; |
| 53214 | + *pp = pOld->pNext; |
| 53215 | + pOld->pNext = pCache->apHash[hOld]; |
| 53216 | + pCache->apHash[hOld] = pOld; |
| 53217 | + pOld->iKey = iOld; |
| 53218 | + break; |
| 53219 | + }else{ |
| 53220 | + pp = &(*pp)->pNext; |
| 53221 | + } |
| 53222 | + } |
| 53223 | + |
| 53044 | 53224 | pPage->iKey = iNew; |
| 53045 | | - pPage->pNext = pCache->apHash[h]; |
| 53046 | | - pCache->apHash[h] = pPage; |
| 53225 | + pPage->pNext = pCache->apHash[hNew]; |
| 53226 | + pCache->apHash[hNew] = pPage; |
| 53047 | 53227 | if( iNew>pCache->iMaxKey ){ |
| 53048 | 53228 | pCache->iMaxKey = iNew; |
| 53049 | 53229 | } |
| 53050 | 53230 | |
| 53051 | 53231 | pcache1LeaveMutex(pCache->pGroup); |
| | @@ -53148,13 +53328,10 @@ |
| 53148 | 53328 | while( (nReq<0 || nFree<nReq) |
| 53149 | 53329 | && (p=pcache1.grp.lru.pLruPrev)!=0 |
| 53150 | 53330 | && p->isAnchor==0 |
| 53151 | 53331 | ){ |
| 53152 | 53332 | nFree += pcache1MemSize(p->page.pBuf); |
| 53153 | | -#ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 53154 | | - nFree += sqlite3MemSize(p); |
| 53155 | | -#endif |
| 53156 | 53333 | assert( PAGE_IS_UNPINNED(p) ); |
| 53157 | 53334 | pcache1PinPage(p); |
| 53158 | 53335 | pcache1RemoveFromHash(p, 1); |
| 53159 | 53336 | } |
| 53160 | 53337 | pcache1LeaveMutex(&pcache1.grp); |
| | @@ -59639,10 +59816,11 @@ |
| 59639 | 59816 | int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 59640 | 59817 | int nSpill; |
| 59641 | 59818 | |
| 59642 | 59819 | if( pPager->tempFile ){ |
| 59643 | 59820 | flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 59821 | + flags |= SQLITE_OPEN_EXCLUSIVE; |
| 59644 | 59822 | nSpill = sqlite3Config.nStmtSpill; |
| 59645 | 59823 | }else{ |
| 59646 | 59824 | flags |= SQLITE_OPEN_MAIN_JOURNAL; |
| 59647 | 59825 | nSpill = jrnlBufferSize(pPager); |
| 59648 | 59826 | } |
| | @@ -59674,10 +59852,11 @@ |
| 59674 | 59852 | } |
| 59675 | 59853 | |
| 59676 | 59854 | if( rc!=SQLITE_OK ){ |
| 59677 | 59855 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 59678 | 59856 | pPager->pInJournal = 0; |
| 59857 | + pPager->journalOff = 0; |
| 59679 | 59858 | }else{ |
| 59680 | 59859 | assert( pPager->eState==PAGER_WRITER_LOCKED ); |
| 59681 | 59860 | pPager->eState = PAGER_WRITER_CACHEMOD; |
| 59682 | 59861 | } |
| 59683 | 59862 | |
| | @@ -66737,10 +66916,11 @@ |
| 66737 | 66916 | ** db using sqlite3SchemaToIndex(). |
| 66738 | 66917 | */ |
| 66739 | 66918 | SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){ |
| 66740 | 66919 | Btree *p; |
| 66741 | 66920 | assert( db!=0 ); |
| 66921 | + if( db->pVfs==0 && db->nDb==0 ) return 1; |
| 66742 | 66922 | if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema); |
| 66743 | 66923 | assert( iDb>=0 && iDb<db->nDb ); |
| 66744 | 66924 | if( !sqlite3_mutex_held(db->mutex) ) return 0; |
| 66745 | 66925 | if( iDb==1 ) return 1; |
| 66746 | 66926 | p = db->aDb[iDb].pBt; |
| | @@ -68309,12 +68489,11 @@ |
| 68309 | 68489 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 68310 | 68490 | assert( pPage->pBt!=0 ); |
| 68311 | 68491 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 68312 | 68492 | assert( pPage->nOverflow==0 ); |
| 68313 | 68493 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 68314 | | - temp = 0; |
| 68315 | | - src = data = pPage->aData; |
| 68494 | + data = pPage->aData; |
| 68316 | 68495 | hdr = pPage->hdrOffset; |
| 68317 | 68496 | cellOffset = pPage->cellOffset; |
| 68318 | 68497 | nCell = pPage->nCell; |
| 68319 | 68498 | assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB ); |
| 68320 | 68499 | iCellFirst = cellOffset + 2*nCell; |
| | @@ -68364,43 +68543,42 @@ |
| 68364 | 68543 | } |
| 68365 | 68544 | |
| 68366 | 68545 | cbrk = usableSize; |
| 68367 | 68546 | iCellLast = usableSize - 4; |
| 68368 | 68547 | iCellStart = get2byte(&data[hdr+5]); |
| 68369 | | - for(i=0; i<nCell; i++){ |
| 68370 | | - u8 *pAddr; /* The i-th cell pointer */ |
| 68371 | | - pAddr = &data[cellOffset + i*2]; |
| 68372 | | - pc = get2byte(pAddr); |
| 68373 | | - testcase( pc==iCellFirst ); |
| 68374 | | - testcase( pc==iCellLast ); |
| 68375 | | - /* These conditions have already been verified in btreeInitPage() |
| 68376 | | - ** if PRAGMA cell_size_check=ON. |
| 68377 | | - */ |
| 68378 | | - if( pc<iCellStart || pc>iCellLast ){ |
| 68379 | | - return SQLITE_CORRUPT_PAGE(pPage); |
| 68380 | | - } |
| 68381 | | - assert( pc>=iCellStart && pc<=iCellLast ); |
| 68382 | | - size = pPage->xCellSize(pPage, &src[pc]); |
| 68383 | | - cbrk -= size; |
| 68384 | | - if( cbrk<iCellStart || pc+size>usableSize ){ |
| 68385 | | - return SQLITE_CORRUPT_PAGE(pPage); |
| 68386 | | - } |
| 68387 | | - assert( cbrk+size<=usableSize && cbrk>=iCellStart ); |
| 68388 | | - testcase( cbrk+size==usableSize ); |
| 68389 | | - testcase( pc+size==usableSize ); |
| 68390 | | - put2byte(pAddr, cbrk); |
| 68391 | | - if( temp==0 ){ |
| 68392 | | - if( cbrk==pc ) continue; |
| 68393 | | - temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 68394 | | - memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart); |
| 68395 | | - src = temp; |
| 68396 | | - } |
| 68397 | | - memcpy(&data[cbrk], &src[pc], size); |
| 68548 | + if( nCell>0 ){ |
| 68549 | + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 68550 | + memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart); |
| 68551 | + src = temp; |
| 68552 | + for(i=0; i<nCell; i++){ |
| 68553 | + u8 *pAddr; /* The i-th cell pointer */ |
| 68554 | + pAddr = &data[cellOffset + i*2]; |
| 68555 | + pc = get2byte(pAddr); |
| 68556 | + testcase( pc==iCellFirst ); |
| 68557 | + testcase( pc==iCellLast ); |
| 68558 | + /* These conditions have already been verified in btreeInitPage() |
| 68559 | + ** if PRAGMA cell_size_check=ON. |
| 68560 | + */ |
| 68561 | + if( pc<iCellStart || pc>iCellLast ){ |
| 68562 | + return SQLITE_CORRUPT_PAGE(pPage); |
| 68563 | + } |
| 68564 | + assert( pc>=iCellStart && pc<=iCellLast ); |
| 68565 | + size = pPage->xCellSize(pPage, &src[pc]); |
| 68566 | + cbrk -= size; |
| 68567 | + if( cbrk<iCellStart || pc+size>usableSize ){ |
| 68568 | + return SQLITE_CORRUPT_PAGE(pPage); |
| 68569 | + } |
| 68570 | + assert( cbrk+size<=usableSize && cbrk>=iCellStart ); |
| 68571 | + testcase( cbrk+size==usableSize ); |
| 68572 | + testcase( pc+size==usableSize ); |
| 68573 | + put2byte(pAddr, cbrk); |
| 68574 | + memcpy(&data[cbrk], &src[pc], size); |
| 68575 | + } |
| 68398 | 68576 | } |
| 68399 | 68577 | data[hdr+7] = 0; |
| 68400 | 68578 | |
| 68401 | | - defragment_out: |
| 68579 | +defragment_out: |
| 68402 | 68580 | assert( pPage->nFree>=0 ); |
| 68403 | 68581 | if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){ |
| 68404 | 68582 | return SQLITE_CORRUPT_PAGE(pPage); |
| 68405 | 68583 | } |
| 68406 | 68584 | assert( cbrk>=iCellFirst ); |
| | @@ -68469,13 +68647,13 @@ |
| 68469 | 68647 | return &aData[pc + x]; |
| 68470 | 68648 | } |
| 68471 | 68649 | iAddr = pc; |
| 68472 | 68650 | pTmp = &aData[pc]; |
| 68473 | 68651 | pc = get2byte(pTmp); |
| 68474 | | - if( pc<=iAddr+size ){ |
| 68652 | + if( pc<=iAddr ){ |
| 68475 | 68653 | if( pc ){ |
| 68476 | | - /* The next slot in the chain is not past the end of the current slot */ |
| 68654 | + /* The next slot in the chain comes before the current slot */ |
| 68477 | 68655 | *pRc = SQLITE_CORRUPT_PAGE(pPg); |
| 68478 | 68656 | } |
| 68479 | 68657 | return 0; |
| 68480 | 68658 | } |
| 68481 | 68659 | } |
| | @@ -68623,11 +68801,11 @@ |
| 68623 | 68801 | iPtr = hdr + 1; |
| 68624 | 68802 | if( data[iPtr+1]==0 && data[iPtr]==0 ){ |
| 68625 | 68803 | iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ |
| 68626 | 68804 | }else{ |
| 68627 | 68805 | while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){ |
| 68628 | | - if( iFreeBlk<iPtr+4 ){ |
| 68806 | + if( iFreeBlk<=iPtr ){ |
| 68629 | 68807 | if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */ |
| 68630 | 68808 | return SQLITE_CORRUPT_PAGE(pPage); |
| 68631 | 68809 | } |
| 68632 | 68810 | iPtr = iFreeBlk; |
| 68633 | 68811 | } |
| | @@ -69105,13 +69283,11 @@ |
| 69105 | 69283 | if( pCur ){ |
| 69106 | 69284 | pCur->iPage--; |
| 69107 | 69285 | pCur->pPage = pCur->apPage[pCur->iPage]; |
| 69108 | 69286 | } |
| 69109 | 69287 | testcase( pgno==0 ); |
| 69110 | | - assert( pgno!=0 || rc==SQLITE_CORRUPT |
| 69111 | | - || rc==SQLITE_IOERR_NOMEM |
| 69112 | | - || rc==SQLITE_NOMEM ); |
| 69288 | + assert( pgno!=0 || rc!=SQLITE_OK ); |
| 69113 | 69289 | return rc; |
| 69114 | 69290 | } |
| 69115 | 69291 | |
| 69116 | 69292 | /* |
| 69117 | 69293 | ** Release a MemPage. This should be called once for each prior |
| | @@ -72049,12 +72225,10 @@ |
| 72049 | 72225 | ** the new child page does not match the flags field of the parent (i.e. |
| 72050 | 72226 | ** if an intkey page appears to be the parent of a non-intkey page, or |
| 72051 | 72227 | ** vice-versa). |
| 72052 | 72228 | */ |
| 72053 | 72229 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
| 72054 | | - BtShared *pBt = pCur->pBt; |
| 72055 | | - |
| 72056 | 72230 | assert( cursorOwnsBtShared(pCur) ); |
| 72057 | 72231 | assert( pCur->eState==CURSOR_VALID ); |
| 72058 | 72232 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 72059 | 72233 | assert( pCur->iPage>=0 ); |
| 72060 | 72234 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| | @@ -72064,11 +72238,12 @@ |
| 72064 | 72238 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 72065 | 72239 | pCur->aiIdx[pCur->iPage] = pCur->ix; |
| 72066 | 72240 | pCur->apPage[pCur->iPage] = pCur->pPage; |
| 72067 | 72241 | pCur->ix = 0; |
| 72068 | 72242 | pCur->iPage++; |
| 72069 | | - return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags); |
| 72243 | + return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur, |
| 72244 | + pCur->curPagerFlags); |
| 72070 | 72245 | } |
| 72071 | 72246 | |
| 72072 | 72247 | #ifdef SQLITE_DEBUG |
| 72073 | 72248 | /* |
| 72074 | 72249 | ** Page pParent is an internal (non-leaf) tree page. This function |
| | @@ -72170,11 +72345,11 @@ |
| 72170 | 72345 | assert( pCur->skipNext!=SQLITE_OK ); |
| 72171 | 72346 | return pCur->skipNext; |
| 72172 | 72347 | } |
| 72173 | 72348 | sqlite3BtreeClearCursor(pCur); |
| 72174 | 72349 | } |
| 72175 | | - rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage, |
| 72350 | + rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage, |
| 72176 | 72351 | 0, pCur->curPagerFlags); |
| 72177 | 72352 | if( rc!=SQLITE_OK ){ |
| 72178 | 72353 | pCur->eState = CURSOR_INVALID; |
| 72179 | 72354 | return rc; |
| 72180 | 72355 | } |
| | @@ -73811,16 +73986,10 @@ |
| 73811 | 73986 | data = pPage->aData; |
| 73812 | 73987 | ptr = &pPage->aCellIdx[2*idx]; |
| 73813 | 73988 | assert( pPage->pBt->usableSize > (u32)(ptr-data) ); |
| 73814 | 73989 | pc = get2byte(ptr); |
| 73815 | 73990 | hdr = pPage->hdrOffset; |
| 73816 | | -#if 0 /* Not required. Omit for efficiency */ |
| 73817 | | - if( pc<hdr+pPage->nCell*2 ){ |
| 73818 | | - *pRC = SQLITE_CORRUPT_BKPT; |
| 73819 | | - return; |
| 73820 | | - } |
| 73821 | | -#endif |
| 73822 | 73991 | testcase( pc==(u32)get2byte(&data[hdr+5]) ); |
| 73823 | 73992 | testcase( pc+sz==pPage->pBt->usableSize ); |
| 73824 | 73993 | if( pc+sz > pPage->pBt->usableSize ){ |
| 73825 | 73994 | *pRC = SQLITE_CORRUPT_BKPT; |
| 73826 | 73995 | return; |
| | @@ -74700,12 +74869,10 @@ |
| 74700 | 74869 | int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
| 74701 | 74870 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 74702 | 74871 | Pgno pgno; /* Temp var to store a page number in */ |
| 74703 | 74872 | u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 74704 | 74873 | Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
| 74705 | | - Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
| 74706 | | - u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
| 74707 | 74874 | CellArray b; /* Parsed information on cells being balanced */ |
| 74708 | 74875 | |
| 74709 | 74876 | memset(abDone, 0, sizeof(abDone)); |
| 74710 | 74877 | memset(&b, 0, sizeof(b)); |
| 74711 | 74878 | pBt = pParent->pBt; |
| | @@ -75125,46 +75292,43 @@ |
| 75125 | 75292 | ** Reassign page numbers so that the new pages are in ascending order. |
| 75126 | 75293 | ** This helps to keep entries in the disk file in order so that a scan |
| 75127 | 75294 | ** of the table is closer to a linear scan through the file. That in turn |
| 75128 | 75295 | ** helps the operating system to deliver pages from the disk more rapidly. |
| 75129 | 75296 | ** |
| 75130 | | - ** An O(n^2) insertion sort algorithm is used, but since n is never more |
| 75131 | | - ** than (NB+2) (a small constant), that should not be a problem. |
| 75297 | + ** An O(N*N) sort algorithm is used, but since N is never more than NB+2 |
| 75298 | + ** (5), that is not a performance concern. |
| 75132 | 75299 | ** |
| 75133 | 75300 | ** When NB==3, this one optimization makes the database about 25% faster |
| 75134 | 75301 | ** for large insertions and deletions. |
| 75135 | 75302 | */ |
| 75136 | 75303 | for(i=0; i<nNew; i++){ |
| 75137 | | - aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
| 75138 | | - aPgFlags[i] = apNew[i]->pDbPage->flags; |
| 75139 | | - for(j=0; j<i; j++){ |
| 75140 | | - if( NEVER(aPgno[j]==aPgno[i]) ){ |
| 75141 | | - /* This branch is taken if the set of sibling pages somehow contains |
| 75142 | | - ** duplicate entries. This can happen if the database is corrupt. |
| 75143 | | - ** It would be simpler to detect this as part of the loop below, but |
| 75144 | | - ** we do the detection here in order to avoid populating the pager |
| 75145 | | - ** cache with two separate objects associated with the same |
| 75146 | | - ** page number. */ |
| 75147 | | - assert( CORRUPT_DB ); |
| 75148 | | - rc = SQLITE_CORRUPT_BKPT; |
| 75149 | | - goto balance_cleanup; |
| 75150 | | - } |
| 75151 | | - } |
| 75152 | | - } |
| 75153 | | - for(i=0; i<nNew; i++){ |
| 75154 | | - int iBest = 0; /* aPgno[] index of page number to use */ |
| 75155 | | - for(j=1; j<nNew; j++){ |
| 75156 | | - if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
| 75157 | | - } |
| 75158 | | - pgno = aPgOrder[iBest]; |
| 75159 | | - aPgOrder[iBest] = 0xffffffff; |
| 75160 | | - if( iBest!=i ){ |
| 75161 | | - if( iBest>i ){ |
| 75162 | | - sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
| 75163 | | - } |
| 75164 | | - sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
| 75165 | | - apNew[i]->pgno = pgno; |
| 75304 | + aPgno[i] = apNew[i]->pgno; |
| 75305 | + assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE ); |
| 75306 | + assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY ); |
| 75307 | + } |
| 75308 | + for(i=0; i<nNew-1; i++){ |
| 75309 | + int iB = i; |
| 75310 | + for(j=i+1; j<nNew; j++){ |
| 75311 | + if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j; |
| 75312 | + } |
| 75313 | + |
| 75314 | + /* If apNew[i] has a page number that is bigger than any of the |
| 75315 | + ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent |
| 75316 | + ** entry that has the smallest page number (which we know to be |
| 75317 | + ** entry apNew[iB]). |
| 75318 | + */ |
| 75319 | + if( iB!=i ){ |
| 75320 | + Pgno pgnoA = apNew[i]->pgno; |
| 75321 | + Pgno pgnoB = apNew[iB]->pgno; |
| 75322 | + Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1; |
| 75323 | + u16 fgA = apNew[i]->pDbPage->flags; |
| 75324 | + u16 fgB = apNew[iB]->pDbPage->flags; |
| 75325 | + sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB); |
| 75326 | + sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA); |
| 75327 | + sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB); |
| 75328 | + apNew[i]->pgno = pgnoB; |
| 75329 | + apNew[iB]->pgno = pgnoA; |
| 75166 | 75330 | } |
| 75167 | 75331 | } |
| 75168 | 75332 | |
| 75169 | 75333 | TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 75170 | 75334 | "%d(%d nc=%d) %d(%d nc=%d)\n", |
| | @@ -79428,10 +79592,20 @@ |
| 79428 | 79592 | double r2 = (double)i; |
| 79429 | 79593 | return r1==0.0 |
| 79430 | 79594 | || (memcmp(&r1, &r2, sizeof(r1))==0 |
| 79431 | 79595 | && i >= -2251799813685248LL && i < 2251799813685248LL); |
| 79432 | 79596 | } |
| 79597 | + |
| 79598 | +/* Convert a floating point value to its closest integer. Do so in |
| 79599 | +** a way that avoids 'outside the range of representable values' warnings |
| 79600 | +** from UBSAN. |
| 79601 | +*/ |
| 79602 | +SQLITE_PRIVATE i64 sqlite3RealToI64(double r){ |
| 79603 | + if( r<=(double)SMALLEST_INT64 ) return SMALLEST_INT64; |
| 79604 | + if( r>=(double)LARGEST_INT64) return LARGEST_INT64; |
| 79605 | + return (i64)r; |
| 79606 | +} |
| 79433 | 79607 | |
| 79434 | 79608 | /* |
| 79435 | 79609 | ** Convert pMem so that it has type MEM_Real or MEM_Int. |
| 79436 | 79610 | ** Invalidate any prior representations. |
| 79437 | 79611 | ** |
| | @@ -79450,11 +79624,11 @@ |
| 79450 | 79624 | sqlite3_int64 ix; |
| 79451 | 79625 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); |
| 79452 | 79626 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 79453 | 79627 | rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 79454 | 79628 | if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1) |
| 79455 | | - || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r)) |
| 79629 | + || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r))) |
| 79456 | 79630 | ){ |
| 79457 | 79631 | pMem->u.i = ix; |
| 79458 | 79632 | MemSetTypeFlag(pMem, MEM_Int); |
| 79459 | 79633 | }else{ |
| 79460 | 79634 | MemSetTypeFlag(pMem, MEM_Real); |
| | @@ -80682,14 +80856,14 @@ |
| 80682 | 80856 | p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) ); |
| 80683 | 80857 | if( p==0 ) return 0; |
| 80684 | 80858 | memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)); |
| 80685 | 80859 | p->db = db; |
| 80686 | 80860 | if( db->pVdbe ){ |
| 80687 | | - db->pVdbe->pPrev = p; |
| 80861 | + db->pVdbe->ppVPrev = &p->pVNext; |
| 80688 | 80862 | } |
| 80689 | | - p->pNext = db->pVdbe; |
| 80690 | | - p->pPrev = 0; |
| 80863 | + p->pVNext = db->pVdbe; |
| 80864 | + p->ppVPrev = &db->pVdbe; |
| 80691 | 80865 | db->pVdbe = p; |
| 80692 | 80866 | assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 80693 | 80867 | p->pParse = pParse; |
| 80694 | 80868 | pParse->pVdbe = p; |
| 80695 | 80869 | assert( pParse->aLabel==0 ); |
| | @@ -80767,25 +80941,32 @@ |
| 80767 | 80941 | return 0; |
| 80768 | 80942 | } |
| 80769 | 80943 | #endif |
| 80770 | 80944 | |
| 80771 | 80945 | /* |
| 80772 | | -** Swap all content between two VDBE structures. |
| 80946 | +** Swap byte-code between two VDBE structures. |
| 80947 | +** |
| 80948 | +** This happens after pB was previously run and returned |
| 80949 | +** SQLITE_SCHEMA. The statement was then reprepared in pA. |
| 80950 | +** This routine transfers the new bytecode in pA over to pB |
| 80951 | +** so that pB can be run again. The old pB byte code is |
| 80952 | +** moved back to pA so that it will be cleaned up when pA is |
| 80953 | +** finalized. |
| 80773 | 80954 | */ |
| 80774 | 80955 | SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
| 80775 | | - Vdbe tmp, *pTmp; |
| 80956 | + Vdbe tmp, *pTmp, **ppTmp; |
| 80776 | 80957 | char *zTmp; |
| 80777 | 80958 | assert( pA->db==pB->db ); |
| 80778 | 80959 | tmp = *pA; |
| 80779 | 80960 | *pA = *pB; |
| 80780 | 80961 | *pB = tmp; |
| 80781 | | - pTmp = pA->pNext; |
| 80782 | | - pA->pNext = pB->pNext; |
| 80783 | | - pB->pNext = pTmp; |
| 80784 | | - pTmp = pA->pPrev; |
| 80785 | | - pA->pPrev = pB->pPrev; |
| 80786 | | - pB->pPrev = pTmp; |
| 80962 | + pTmp = pA->pVNext; |
| 80963 | + pA->pVNext = pB->pVNext; |
| 80964 | + pB->pVNext = pTmp; |
| 80965 | + ppTmp = pA->ppVPrev; |
| 80966 | + pA->ppVPrev = pB->ppVPrev; |
| 80967 | + pB->ppVPrev = ppTmp; |
| 80787 | 80968 | zTmp = pA->zSql; |
| 80788 | 80969 | pA->zSql = pB->zSql; |
| 80789 | 80970 | pB->zSql = zTmp; |
| 80790 | 80971 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 80791 | 80972 | zTmp = pA->zNormSql; |
| | @@ -81033,10 +81214,11 @@ |
| 81033 | 81214 | pCtx->argc = nArg; |
| 81034 | 81215 | pCtx->iOp = sqlite3VdbeCurrentAddr(v); |
| 81035 | 81216 | addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function, |
| 81036 | 81217 | p1, p2, p3, (char*)pCtx, P4_FUNCCTX); |
| 81037 | 81218 | sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef); |
| 81219 | + sqlite3MayAbort(pParse); |
| 81038 | 81220 | return addr; |
| 81039 | 81221 | } |
| 81040 | 81222 | |
| 81041 | 81223 | /* |
| 81042 | 81224 | ** Add an opcode that includes the p4 value with a P4_INT64 or |
| | @@ -81101,11 +81283,11 @@ |
| 81101 | 81283 | va_end(ap); |
| 81102 | 81284 | v = pParse->pVdbe; |
| 81103 | 81285 | iThis = v->nOp; |
| 81104 | 81286 | sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0, |
| 81105 | 81287 | zMsg, P4_DYNAMIC); |
| 81106 | | - sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z); |
| 81288 | + sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z); |
| 81107 | 81289 | if( bPush){ |
| 81108 | 81290 | pParse->addrExplain = iThis; |
| 81109 | 81291 | } |
| 81110 | 81292 | } |
| 81111 | 81293 | } |
| | @@ -81368,10 +81550,11 @@ |
| 81368 | 81550 | int opcode = pOp->opcode; |
| 81369 | 81551 | if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
| 81370 | 81552 | || opcode==OP_VDestroy |
| 81371 | 81553 | || opcode==OP_VCreate |
| 81372 | 81554 | || opcode==OP_ParseSchema |
| 81555 | + || opcode==OP_Function || opcode==OP_PureFunc |
| 81373 | 81556 | || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
| 81374 | 81557 | && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) |
| 81375 | 81558 | ){ |
| 81376 | 81559 | hasAbort = 1; |
| 81377 | 81560 | break; |
| | @@ -81458,12 +81641,12 @@ |
| 81458 | 81641 | Parse *pParse = p->pParse; |
| 81459 | 81642 | int *aLabel = pParse->aLabel; |
| 81460 | 81643 | p->readOnly = 1; |
| 81461 | 81644 | p->bIsReader = 0; |
| 81462 | 81645 | pOp = &p->aOp[p->nOp-1]; |
| 81463 | | - while(1){ |
| 81464 | | - |
| 81646 | + assert( p->aOp[0].opcode==OP_Init ); |
| 81647 | + while( 1 /* Loop termates when it reaches the OP_Init opcode */ ){ |
| 81465 | 81648 | /* Only JUMP opcodes and the short list of special opcodes in the switch |
| 81466 | 81649 | ** below need to be considered. The mkopcodeh.tcl generator script groups |
| 81467 | 81650 | ** all these opcodes together near the front of the opcode list. Skip |
| 81468 | 81651 | ** any opcode that does not need processing by virtual of the fact that |
| 81469 | 81652 | ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. |
| | @@ -81488,10 +81671,14 @@ |
| 81488 | 81671 | case OP_JournalMode: { |
| 81489 | 81672 | p->readOnly = 0; |
| 81490 | 81673 | p->bIsReader = 1; |
| 81491 | 81674 | break; |
| 81492 | 81675 | } |
| 81676 | + case OP_Init: { |
| 81677 | + assert( pOp->p2>=0 ); |
| 81678 | + goto resolve_p2_values_loop_exit; |
| 81679 | + } |
| 81493 | 81680 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81494 | 81681 | case OP_VUpdate: { |
| 81495 | 81682 | if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
| 81496 | 81683 | break; |
| 81497 | 81684 | } |
| | @@ -81520,15 +81707,16 @@ |
| 81520 | 81707 | /* The mkopcodeh.tcl script has so arranged things that the only |
| 81521 | 81708 | ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to |
| 81522 | 81709 | ** have non-negative values for P2. */ |
| 81523 | 81710 | assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0); |
| 81524 | 81711 | } |
| 81525 | | - if( pOp==p->aOp ) break; |
| 81712 | + assert( pOp>p->aOp ); |
| 81526 | 81713 | pOp--; |
| 81527 | 81714 | } |
| 81715 | +resolve_p2_values_loop_exit: |
| 81528 | 81716 | if( aLabel ){ |
| 81529 | | - sqlite3DbFreeNN(p->db, pParse->aLabel); |
| 81717 | + sqlite3DbNNFreeNN(p->db, pParse->aLabel); |
| 81530 | 81718 | pParse->aLabel = 0; |
| 81531 | 81719 | } |
| 81532 | 81720 | pParse->nLabel = 0; |
| 81533 | 81721 | *pMaxFuncArgs = nMaxArgs; |
| 81534 | 81722 | assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
| | @@ -81773,19 +81961,23 @@ |
| 81773 | 81961 | /* |
| 81774 | 81962 | ** Change the value of the opcode, or P1, P2, P3, or P5 operands |
| 81775 | 81963 | ** for a specific instruction. |
| 81776 | 81964 | */ |
| 81777 | 81965 | SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){ |
| 81966 | + assert( addr>=0 ); |
| 81778 | 81967 | sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode; |
| 81779 | 81968 | } |
| 81780 | 81969 | SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
| 81970 | + assert( addr>=0 ); |
| 81781 | 81971 | sqlite3VdbeGetOp(p,addr)->p1 = val; |
| 81782 | 81972 | } |
| 81783 | 81973 | SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
| 81974 | + assert( addr>=0 || p->db->mallocFailed ); |
| 81784 | 81975 | sqlite3VdbeGetOp(p,addr)->p2 = val; |
| 81785 | 81976 | } |
| 81786 | 81977 | SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
| 81978 | + assert( addr>=0 ); |
| 81787 | 81979 | sqlite3VdbeGetOp(p,addr)->p3 = val; |
| 81788 | 81980 | } |
| 81789 | 81981 | SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ |
| 81790 | 81982 | assert( p->nOp>0 || p->db->mallocFailed ); |
| 81791 | 81983 | if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; |
| | @@ -81817,11 +82009,11 @@ |
| 81817 | 82009 | assert( p->aOp[addr].opcode==OP_Once |
| 81818 | 82010 | || p->aOp[addr].opcode==OP_If |
| 81819 | 82011 | || p->aOp[addr].opcode==OP_FkIfZero ); |
| 81820 | 82012 | assert( p->aOp[addr].p4type==0 ); |
| 81821 | 82013 | #ifdef SQLITE_VDBE_COVERAGE |
| 81822 | | - sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */ |
| 82014 | + sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */ |
| 81823 | 82015 | #endif |
| 81824 | 82016 | p->nOp--; |
| 81825 | 82017 | }else{ |
| 81826 | 82018 | sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 81827 | 82019 | } |
| | @@ -81831,25 +82023,27 @@ |
| 81831 | 82023 | /* |
| 81832 | 82024 | ** If the input FuncDef structure is ephemeral, then free it. If |
| 81833 | 82025 | ** the FuncDef is not ephermal, then do nothing. |
| 81834 | 82026 | */ |
| 81835 | 82027 | static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
| 82028 | + assert( db!=0 ); |
| 81836 | 82029 | if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ |
| 81837 | | - sqlite3DbFreeNN(db, pDef); |
| 82030 | + sqlite3DbNNFreeNN(db, pDef); |
| 81838 | 82031 | } |
| 81839 | 82032 | } |
| 81840 | 82033 | |
| 81841 | 82034 | /* |
| 81842 | 82035 | ** Delete a P4 value if necessary. |
| 81843 | 82036 | */ |
| 81844 | 82037 | static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){ |
| 81845 | 82038 | if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
| 81846 | | - sqlite3DbFreeNN(db, p); |
| 82039 | + sqlite3DbNNFreeNN(db, p); |
| 81847 | 82040 | } |
| 81848 | 82041 | static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){ |
| 82042 | + assert( db!=0 ); |
| 81849 | 82043 | freeEphemeralFunction(db, p->pFunc); |
| 81850 | | - sqlite3DbFreeNN(db, p); |
| 82044 | + sqlite3DbNNFreeNN(db, p); |
| 81851 | 82045 | } |
| 81852 | 82046 | static void freeP4(sqlite3 *db, int p4type, void *p4){ |
| 81853 | 82047 | assert( db ); |
| 81854 | 82048 | switch( p4type ){ |
| 81855 | 82049 | case P4_FUNCCTX: { |
| | @@ -81858,11 +82052,11 @@ |
| 81858 | 82052 | } |
| 81859 | 82053 | case P4_REAL: |
| 81860 | 82054 | case P4_INT64: |
| 81861 | 82055 | case P4_DYNAMIC: |
| 81862 | 82056 | case P4_INTARRAY: { |
| 81863 | | - sqlite3DbFree(db, p4); |
| 82057 | + if( p4 ) sqlite3DbNNFreeNN(db, p4); |
| 81864 | 82058 | break; |
| 81865 | 82059 | } |
| 81866 | 82060 | case P4_KEYINFO: { |
| 81867 | 82061 | if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); |
| 81868 | 82062 | break; |
| | @@ -81897,10 +82091,11 @@ |
| 81897 | 82091 | ** opcodes contained within. If aOp is not NULL it is assumed to contain |
| 81898 | 82092 | ** nOp entries. |
| 81899 | 82093 | */ |
| 81900 | 82094 | static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
| 81901 | 82095 | assert( nOp>=0 ); |
| 82096 | + assert( db!=0 ); |
| 81902 | 82097 | if( aOp ){ |
| 81903 | 82098 | Op *pOp = &aOp[nOp-1]; |
| 81904 | 82099 | while(1){ /* Exit via break */ |
| 81905 | 82100 | if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p); |
| 81906 | 82101 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| | @@ -81907,11 +82102,11 @@ |
| 81907 | 82102 | sqlite3DbFree(db, pOp->zComment); |
| 81908 | 82103 | #endif |
| 81909 | 82104 | if( pOp==aOp ) break; |
| 81910 | 82105 | pOp--; |
| 81911 | 82106 | } |
| 81912 | | - sqlite3DbFreeNN(db, aOp); |
| 82107 | + sqlite3DbNNFreeNN(db, aOp); |
| 81913 | 82108 | } |
| 81914 | 82109 | } |
| 81915 | 82110 | |
| 81916 | 82111 | /* |
| 81917 | 82112 | ** Link the SubProgram object passed as the second argument into the linked |
| | @@ -82138,17 +82333,17 @@ |
| 82138 | 82333 | #ifdef SQLITE_VDBE_COVERAGE |
| 82139 | 82334 | /* |
| 82140 | 82335 | ** Set the value if the iSrcLine field for the previously coded instruction. |
| 82141 | 82336 | */ |
| 82142 | 82337 | SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ |
| 82143 | | - sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; |
| 82338 | + sqlite3VdbeGetLastOp(v)->iSrcLine = iLine; |
| 82144 | 82339 | } |
| 82145 | 82340 | #endif /* SQLITE_VDBE_COVERAGE */ |
| 82146 | 82341 | |
| 82147 | 82342 | /* |
| 82148 | | -** Return the opcode for a given address. If the address is -1, then |
| 82149 | | -** return the most recently inserted opcode. |
| 82343 | +** Return the opcode for a given address. The address must be non-negative. |
| 82344 | +** See sqlite3VdbeGetLastOp() to get the most recently added opcode. |
| 82150 | 82345 | ** |
| 82151 | 82346 | ** If a memory allocation error has occurred prior to the calling of this |
| 82152 | 82347 | ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode |
| 82153 | 82348 | ** is readable but not writable, though it is cast to a writable value. |
| 82154 | 82349 | ** The return of a dummy opcode allows the call to continue functioning |
| | @@ -82160,20 +82355,23 @@ |
| 82160 | 82355 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
| 82161 | 82356 | /* C89 specifies that the constant "dummy" will be initialized to all |
| 82162 | 82357 | ** zeros, which is correct. MSVC generates a warning, nevertheless. */ |
| 82163 | 82358 | static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
| 82164 | 82359 | assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 82165 | | - if( addr<0 ){ |
| 82166 | | - addr = p->nOp - 1; |
| 82167 | | - } |
| 82168 | 82360 | assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
| 82169 | 82361 | if( p->db->mallocFailed ){ |
| 82170 | 82362 | return (VdbeOp*)&dummy; |
| 82171 | 82363 | }else{ |
| 82172 | 82364 | return &p->aOp[addr]; |
| 82173 | 82365 | } |
| 82174 | 82366 | } |
| 82367 | + |
| 82368 | +/* Return the most recently added opcode |
| 82369 | +*/ |
| 82370 | +VdbeOp * sqlite3VdbeGetLastOp(Vdbe *p){ |
| 82371 | + return sqlite3VdbeGetOp(p, p->nOp - 1); |
| 82372 | +} |
| 82175 | 82373 | |
| 82176 | 82374 | #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) |
| 82177 | 82375 | /* |
| 82178 | 82376 | ** Return an integer value for one of the parameters to the opcode pOp |
| 82179 | 82377 | ** determined by character c. |
| | @@ -82658,11 +82856,11 @@ |
| 82658 | 82856 | if( p->flags&(MEM_Agg|MEM_Dyn) ){ |
| 82659 | 82857 | testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel ); |
| 82660 | 82858 | sqlite3VdbeMemRelease(p); |
| 82661 | 82859 | p->flags = MEM_Undefined; |
| 82662 | 82860 | }else if( p->szMalloc ){ |
| 82663 | | - sqlite3DbFreeNN(db, p->zMalloc); |
| 82861 | + sqlite3DbNNFreeNN(db, p->zMalloc); |
| 82664 | 82862 | p->szMalloc = 0; |
| 82665 | 82863 | p->flags = MEM_Undefined; |
| 82666 | 82864 | } |
| 82667 | 82865 | #ifdef SQLITE_DEBUG |
| 82668 | 82866 | else{ |
| | @@ -83650,11 +83848,11 @@ |
| 83650 | 83848 | if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ |
| 83651 | 83849 | cnt++; |
| 83652 | 83850 | if( p->readOnly==0 ) nWrite++; |
| 83653 | 83851 | if( p->bIsReader ) nRead++; |
| 83654 | 83852 | } |
| 83655 | | - p = p->pNext; |
| 83853 | + p = p->pVNext; |
| 83656 | 83854 | } |
| 83657 | 83855 | assert( cnt==db->nVdbeActive ); |
| 83658 | 83856 | assert( nWrite==db->nVdbeWrite ); |
| 83659 | 83857 | assert( nRead==db->nVdbeRead ); |
| 83660 | 83858 | } |
| | @@ -84179,27 +84377,28 @@ |
| 84179 | 84377 | ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
| 84180 | 84378 | ** the database connection and frees the object itself. |
| 84181 | 84379 | */ |
| 84182 | 84380 | static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ |
| 84183 | 84381 | SubProgram *pSub, *pNext; |
| 84382 | + assert( db!=0 ); |
| 84184 | 84383 | assert( p->db==0 || p->db==db ); |
| 84185 | 84384 | if( p->aColName ){ |
| 84186 | 84385 | releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 84187 | | - sqlite3DbFreeNN(db, p->aColName); |
| 84386 | + sqlite3DbNNFreeNN(db, p->aColName); |
| 84188 | 84387 | } |
| 84189 | 84388 | for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 84190 | 84389 | pNext = pSub->pNext; |
| 84191 | 84390 | vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 84192 | 84391 | sqlite3DbFree(db, pSub); |
| 84193 | 84392 | } |
| 84194 | 84393 | if( p->eVdbeState!=VDBE_INIT_STATE ){ |
| 84195 | 84394 | releaseMemArray(p->aVar, p->nVar); |
| 84196 | | - if( p->pVList ) sqlite3DbFreeNN(db, p->pVList); |
| 84197 | | - if( p->pFree ) sqlite3DbFreeNN(db, p->pFree); |
| 84395 | + if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList); |
| 84396 | + if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree); |
| 84198 | 84397 | } |
| 84199 | 84398 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 84200 | | - sqlite3DbFree(db, p->zSql); |
| 84399 | + if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql); |
| 84201 | 84400 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 84202 | 84401 | sqlite3DbFree(db, p->zNormSql); |
| 84203 | 84402 | { |
| 84204 | 84403 | DblquoteStr *pThis, *pNext; |
| 84205 | 84404 | for(pThis=p->pDblStr; pThis; pThis=pNext){ |
| | @@ -84225,24 +84424,21 @@ |
| 84225 | 84424 | SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){ |
| 84226 | 84425 | sqlite3 *db; |
| 84227 | 84426 | |
| 84228 | 84427 | assert( p!=0 ); |
| 84229 | 84428 | db = p->db; |
| 84429 | + assert( db!=0 ); |
| 84230 | 84430 | assert( sqlite3_mutex_held(db->mutex) ); |
| 84231 | 84431 | sqlite3VdbeClearObject(db, p); |
| 84232 | 84432 | if( db->pnBytesFreed==0 ){ |
| 84233 | | - if( p->pPrev ){ |
| 84234 | | - p->pPrev->pNext = p->pNext; |
| 84235 | | - }else{ |
| 84236 | | - assert( db->pVdbe==p ); |
| 84237 | | - db->pVdbe = p->pNext; |
| 84238 | | - } |
| 84239 | | - if( p->pNext ){ |
| 84240 | | - p->pNext->pPrev = p->pPrev; |
| 84433 | + assert( p->ppVPrev!=0 ); |
| 84434 | + *p->ppVPrev = p->pVNext; |
| 84435 | + if( p->pVNext ){ |
| 84436 | + p->pVNext->ppVPrev = p->ppVPrev; |
| 84241 | 84437 | } |
| 84242 | 84438 | } |
| 84243 | | - sqlite3DbFreeNN(db, p); |
| 84439 | + sqlite3DbNNFreeNN(db, p); |
| 84244 | 84440 | } |
| 84245 | 84441 | |
| 84246 | 84442 | /* |
| 84247 | 84443 | ** The cursor "p" has a pending seek operation that has not yet been |
| 84248 | 84444 | ** carried out. Seek the cursor now. If an error occurs, return |
| | @@ -85733,11 +85929,11 @@ |
| 85733 | 85929 | ** prepared statements. The flag is set to 1 for an immediate expiration |
| 85734 | 85930 | ** and set to 2 for an advisory expiration. |
| 85735 | 85931 | */ |
| 85736 | 85932 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){ |
| 85737 | 85933 | Vdbe *p; |
| 85738 | | - for(p = db->pVdbe; p; p=p->pNext){ |
| 85934 | + for(p = db->pVdbe; p; p=p->pVNext){ |
| 85739 | 85935 | p->expired = iCode+1; |
| 85740 | 85936 | } |
| 85741 | 85937 | } |
| 85742 | 85938 | |
| 85743 | 85939 | /* |
| | @@ -85854,17 +86050,18 @@ |
| 85854 | 86050 | ** |
| 85855 | 86051 | ** This function is used to free UnpackedRecord structures allocated by |
| 85856 | 86052 | ** the vdbeUnpackRecord() function found in vdbeapi.c. |
| 85857 | 86053 | */ |
| 85858 | 86054 | static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ |
| 86055 | + assert( db!=0 ); |
| 85859 | 86056 | if( p ){ |
| 85860 | 86057 | int i; |
| 85861 | 86058 | for(i=0; i<nField; i++){ |
| 85862 | 86059 | Mem *pMem = &p->aMem[i]; |
| 85863 | 86060 | if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem); |
| 85864 | 86061 | } |
| 85865 | | - sqlite3DbFreeNN(db, p); |
| 86062 | + sqlite3DbNNFreeNN(db, p); |
| 85866 | 86063 | } |
| 85867 | 86064 | } |
| 85868 | 86065 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 85869 | 86066 | |
| 85870 | 86067 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| | @@ -85931,11 +86128,11 @@ |
| 85931 | 86128 | if( preupdate.aNew ){ |
| 85932 | 86129 | int i; |
| 85933 | 86130 | for(i=0; i<pCsr->nField; i++){ |
| 85934 | 86131 | sqlite3VdbeMemRelease(&preupdate.aNew[i]); |
| 85935 | 86132 | } |
| 85936 | | - sqlite3DbFreeNN(db, preupdate.aNew); |
| 86133 | + sqlite3DbNNFreeNN(db, preupdate.aNew); |
| 85937 | 86134 | } |
| 85938 | 86135 | } |
| 85939 | 86136 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 85940 | 86137 | |
| 85941 | 86138 | /************** End of vdbeaux.c *********************************************/ |
| | @@ -86048,11 +86245,13 @@ |
| 86048 | 86245 | Vdbe *v = (Vdbe*)pStmt; |
| 86049 | 86246 | sqlite3 *db = v->db; |
| 86050 | 86247 | if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; |
| 86051 | 86248 | sqlite3_mutex_enter(db->mutex); |
| 86052 | 86249 | checkProfileCallback(db, v); |
| 86053 | | - rc = sqlite3VdbeFinalize(v); |
| 86250 | + assert( v->eVdbeState>=VDBE_READY_STATE ); |
| 86251 | + rc = sqlite3VdbeReset(v); |
| 86252 | + sqlite3VdbeDelete(v); |
| 86054 | 86253 | rc = sqlite3ApiExit(db, rc); |
| 86055 | 86254 | sqlite3LeaveMutexAndCloseZombie(db); |
| 86056 | 86255 | } |
| 86057 | 86256 | return rc; |
| 86058 | 86257 | } |
| | @@ -87370,11 +87569,11 @@ |
| 87370 | 87569 | ** the mutex is released if any kind of error occurs. |
| 87371 | 87570 | ** |
| 87372 | 87571 | ** The error code stored in database p->db is overwritten with the return |
| 87373 | 87572 | ** value in any case. |
| 87374 | 87573 | */ |
| 87375 | | -static int vdbeUnbind(Vdbe *p, int i){ |
| 87574 | +static int vdbeUnbind(Vdbe *p, unsigned int i){ |
| 87376 | 87575 | Mem *pVar; |
| 87377 | 87576 | if( vdbeSafetyNotNull(p) ){ |
| 87378 | 87577 | return SQLITE_MISUSE_BKPT; |
| 87379 | 87578 | } |
| 87380 | 87579 | sqlite3_mutex_enter(p->db->mutex); |
| | @@ -87383,16 +87582,15 @@ |
| 87383 | 87582 | sqlite3_mutex_leave(p->db->mutex); |
| 87384 | 87583 | sqlite3_log(SQLITE_MISUSE, |
| 87385 | 87584 | "bind on a busy prepared statement: [%s]", p->zSql); |
| 87386 | 87585 | return SQLITE_MISUSE_BKPT; |
| 87387 | 87586 | } |
| 87388 | | - if( i<1 || i>p->nVar ){ |
| 87587 | + if( i>=(unsigned int)p->nVar ){ |
| 87389 | 87588 | sqlite3Error(p->db, SQLITE_RANGE); |
| 87390 | 87589 | sqlite3_mutex_leave(p->db->mutex); |
| 87391 | 87590 | return SQLITE_RANGE; |
| 87392 | 87591 | } |
| 87393 | | - i--; |
| 87394 | 87592 | pVar = &p->aVar[i]; |
| 87395 | 87593 | sqlite3VdbeMemRelease(pVar); |
| 87396 | 87594 | pVar->flags = MEM_Null; |
| 87397 | 87595 | p->db->errCode = SQLITE_OK; |
| 87398 | 87596 | |
| | @@ -87425,11 +87623,11 @@ |
| 87425 | 87623 | ){ |
| 87426 | 87624 | Vdbe *p = (Vdbe *)pStmt; |
| 87427 | 87625 | Mem *pVar; |
| 87428 | 87626 | int rc; |
| 87429 | 87627 | |
| 87430 | | - rc = vdbeUnbind(p, i); |
| 87628 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87431 | 87629 | if( rc==SQLITE_OK ){ |
| 87432 | 87630 | if( zData!=0 ){ |
| 87433 | 87631 | pVar = &p->aVar[i-1]; |
| 87434 | 87632 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 87435 | 87633 | if( rc==SQLITE_OK && encoding!=0 ){ |
| | @@ -87474,11 +87672,11 @@ |
| 87474 | 87672 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 87475 | 87673 | } |
| 87476 | 87674 | SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 87477 | 87675 | int rc; |
| 87478 | 87676 | Vdbe *p = (Vdbe *)pStmt; |
| 87479 | | - rc = vdbeUnbind(p, i); |
| 87677 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87480 | 87678 | if( rc==SQLITE_OK ){ |
| 87481 | 87679 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
| 87482 | 87680 | sqlite3_mutex_leave(p->db->mutex); |
| 87483 | 87681 | } |
| 87484 | 87682 | return rc; |
| | @@ -87487,21 +87685,21 @@ |
| 87487 | 87685 | return sqlite3_bind_int64(p, i, (i64)iValue); |
| 87488 | 87686 | } |
| 87489 | 87687 | SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
| 87490 | 87688 | int rc; |
| 87491 | 87689 | Vdbe *p = (Vdbe *)pStmt; |
| 87492 | | - rc = vdbeUnbind(p, i); |
| 87690 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87493 | 87691 | if( rc==SQLITE_OK ){ |
| 87494 | 87692 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
| 87495 | 87693 | sqlite3_mutex_leave(p->db->mutex); |
| 87496 | 87694 | } |
| 87497 | 87695 | return rc; |
| 87498 | 87696 | } |
| 87499 | 87697 | SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 87500 | 87698 | int rc; |
| 87501 | 87699 | Vdbe *p = (Vdbe*)pStmt; |
| 87502 | | - rc = vdbeUnbind(p, i); |
| 87700 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87503 | 87701 | if( rc==SQLITE_OK ){ |
| 87504 | 87702 | sqlite3_mutex_leave(p->db->mutex); |
| 87505 | 87703 | } |
| 87506 | 87704 | return rc; |
| 87507 | 87705 | } |
| | @@ -87512,11 +87710,11 @@ |
| 87512 | 87710 | const char *zPTtype, |
| 87513 | 87711 | void (*xDestructor)(void*) |
| 87514 | 87712 | ){ |
| 87515 | 87713 | int rc; |
| 87516 | 87714 | Vdbe *p = (Vdbe*)pStmt; |
| 87517 | | - rc = vdbeUnbind(p, i); |
| 87715 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87518 | 87716 | if( rc==SQLITE_OK ){ |
| 87519 | 87717 | sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); |
| 87520 | 87718 | sqlite3_mutex_leave(p->db->mutex); |
| 87521 | 87719 | }else if( xDestructor ){ |
| 87522 | 87720 | xDestructor(pPtr); |
| | @@ -87590,11 +87788,11 @@ |
| 87590 | 87788 | return rc; |
| 87591 | 87789 | } |
| 87592 | 87790 | SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 87593 | 87791 | int rc; |
| 87594 | 87792 | Vdbe *p = (Vdbe *)pStmt; |
| 87595 | | - rc = vdbeUnbind(p, i); |
| 87793 | + rc = vdbeUnbind(p, (u32)(i-1)); |
| 87596 | 87794 | if( rc==SQLITE_OK ){ |
| 87597 | 87795 | #ifndef SQLITE_OMIT_INCRBLOB |
| 87598 | 87796 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 87599 | 87797 | #else |
| 87600 | 87798 | rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| | @@ -87750,11 +87948,11 @@ |
| 87750 | 87948 | #endif |
| 87751 | 87949 | sqlite3_mutex_enter(pDb->mutex); |
| 87752 | 87950 | if( pStmt==0 ){ |
| 87753 | 87951 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 87754 | 87952 | }else{ |
| 87755 | | - pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| 87953 | + pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; |
| 87756 | 87954 | } |
| 87757 | 87955 | sqlite3_mutex_leave(pDb->mutex); |
| 87758 | 87956 | return pNext; |
| 87759 | 87957 | } |
| 87760 | 87958 | |
| | @@ -87775,12 +87973,15 @@ |
| 87775 | 87973 | if( op==SQLITE_STMTSTATUS_MEMUSED ){ |
| 87776 | 87974 | sqlite3 *db = pVdbe->db; |
| 87777 | 87975 | sqlite3_mutex_enter(db->mutex); |
| 87778 | 87976 | v = 0; |
| 87779 | 87977 | db->pnBytesFreed = (int*)&v; |
| 87978 | + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 87979 | + db->lookaside.pEnd = db->lookaside.pStart; |
| 87780 | 87980 | sqlite3VdbeDelete(pVdbe); |
| 87781 | 87981 | db->pnBytesFreed = 0; |
| 87982 | + db->lookaside.pEnd = db->lookaside.pTrueEnd; |
| 87782 | 87983 | sqlite3_mutex_leave(db->mutex); |
| 87783 | 87984 | }else{ |
| 87784 | 87985 | v = pVdbe->aCounter[op]; |
| 87785 | 87986 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 87786 | 87987 | } |
| | @@ -88616,11 +88817,12 @@ |
| 88616 | 88817 | ** floating point value of rValue. Return true and set *piValue to the |
| 88617 | 88818 | ** integer value if the string is in range to be an integer. Otherwise, |
| 88618 | 88819 | ** return false. |
| 88619 | 88820 | */ |
| 88620 | 88821 | static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ |
| 88621 | | - i64 iValue = (double)rValue; |
| 88822 | + i64 iValue; |
| 88823 | + iValue = sqlite3RealToI64(rValue); |
| 88622 | 88824 | if( sqlite3RealSameAsInt(rValue,iValue) ){ |
| 88623 | 88825 | *piValue = iValue; |
| 88624 | 88826 | return 1; |
| 88625 | 88827 | } |
| 88626 | 88828 | return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); |
| | @@ -88778,21 +88980,22 @@ |
| 88778 | 88980 | ** |
| 88779 | 88981 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
| 88780 | 88982 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
| 88781 | 88983 | */ |
| 88782 | 88984 | static u16 numericType(Mem *pMem){ |
| 88783 | | - if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){ |
| 88985 | + assert( (pMem->flags & MEM_Null)==0 |
| 88986 | + || pMem->db==0 || pMem->db->mallocFailed ); |
| 88987 | + if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){ |
| 88784 | 88988 | testcase( pMem->flags & MEM_Int ); |
| 88785 | 88989 | testcase( pMem->flags & MEM_Real ); |
| 88786 | 88990 | testcase( pMem->flags & MEM_IntReal ); |
| 88787 | | - return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal); |
| 88991 | + return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null); |
| 88788 | 88992 | } |
| 88789 | | - if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
| 88790 | | - testcase( pMem->flags & MEM_Str ); |
| 88791 | | - testcase( pMem->flags & MEM_Blob ); |
| 88792 | | - return computeNumericType(pMem); |
| 88793 | | - } |
| 88993 | + assert( pMem->flags & (MEM_Str|MEM_Blob) ); |
| 88994 | + testcase( pMem->flags & MEM_Str ); |
| 88995 | + testcase( pMem->flags & MEM_Blob ); |
| 88996 | + return computeNumericType(pMem); |
| 88794 | 88997 | return 0; |
| 88795 | 88998 | } |
| 88796 | 88999 | |
| 88797 | 89000 | #ifdef SQLITE_DEBUG |
| 88798 | 89001 | /* |
| | @@ -90033,25 +90236,24 @@ |
| 90033 | 90236 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 90034 | 90237 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 90035 | 90238 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 90036 | 90239 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 90037 | 90240 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
| 90038 | | - u16 flags; /* Combined MEM_* flags from both inputs */ |
| 90039 | 90241 | u16 type1; /* Numeric type of left operand */ |
| 90040 | 90242 | u16 type2; /* Numeric type of right operand */ |
| 90041 | 90243 | i64 iA; /* Integer value of left operand */ |
| 90042 | 90244 | i64 iB; /* Integer value of right operand */ |
| 90043 | 90245 | double rA; /* Real value of left operand */ |
| 90044 | 90246 | double rB; /* Real value of right operand */ |
| 90045 | 90247 | |
| 90046 | 90248 | pIn1 = &aMem[pOp->p1]; |
| 90047 | | - type1 = numericType(pIn1); |
| 90249 | + type1 = pIn1->flags; |
| 90048 | 90250 | pIn2 = &aMem[pOp->p2]; |
| 90049 | | - type2 = numericType(pIn2); |
| 90251 | + type2 = pIn2->flags; |
| 90050 | 90252 | pOut = &aMem[pOp->p3]; |
| 90051 | | - flags = pIn1->flags | pIn2->flags; |
| 90052 | 90253 | if( (type1 & type2 & MEM_Int)!=0 ){ |
| 90254 | +int_math: |
| 90053 | 90255 | iA = pIn1->u.i; |
| 90054 | 90256 | iB = pIn2->u.i; |
| 90055 | 90257 | switch( pOp->opcode ){ |
| 90056 | 90258 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 90057 | 90259 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| | @@ -90069,13 +90271,16 @@ |
| 90069 | 90271 | break; |
| 90070 | 90272 | } |
| 90071 | 90273 | } |
| 90072 | 90274 | pOut->u.i = iB; |
| 90073 | 90275 | MemSetTypeFlag(pOut, MEM_Int); |
| 90074 | | - }else if( (flags & MEM_Null)!=0 ){ |
| 90276 | + }else if( ((type1 | type2) & MEM_Null)!=0 ){ |
| 90075 | 90277 | goto arithmetic_result_is_null; |
| 90076 | 90278 | }else{ |
| 90279 | + type1 = numericType(pIn1); |
| 90280 | + type2 = numericType(pIn2); |
| 90281 | + if( (type1 & type2 & MEM_Int)!=0 ) goto int_math; |
| 90077 | 90282 | fp_math: |
| 90078 | 90283 | rA = sqlite3VdbeRealValue(pIn1); |
| 90079 | 90284 | rB = sqlite3VdbeRealValue(pIn2); |
| 90080 | 90285 | switch( pOp->opcode ){ |
| 90081 | 90286 | case OP_Add: rB += rA; break; |
| | @@ -90941,15 +91146,18 @@ |
| 90941 | 91146 | ** |
| 90942 | 91147 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 90943 | 91148 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 90944 | 91149 | ** If P1 is not on a NULL row, then fall through without making any |
| 90945 | 91150 | ** changes. |
| 91151 | +** |
| 91152 | +** If P1 is not an open cursor, then this opcode is a no-op. |
| 90946 | 91153 | */ |
| 90947 | 91154 | case OP_IfNullRow: { /* jump */ |
| 91155 | + VdbeCursor *pC; |
| 90948 | 91156 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 90949 | | - assert( p->apCsr[pOp->p1]!=0 ); |
| 90950 | | - if( p->apCsr[pOp->p1]->nullRow ){ |
| 91157 | + pC = p->apCsr[pOp->p1]; |
| 91158 | + if( ALWAYS(pC) && pC->nullRow ){ |
| 90951 | 91159 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 90952 | 91160 | goto jump_to_p2; |
| 90953 | 91161 | } |
| 90954 | 91162 | break; |
| 90955 | 91163 | } |
| | @@ -93052,11 +93260,11 @@ |
| 93052 | 93260 | ** |
| 93053 | 93261 | ** <li> If the cursor is successfully moved to the target row by 0 or more |
| 93054 | 93262 | ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just |
| 93055 | 93263 | ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE. |
| 93056 | 93264 | ** |
| 93057 | | -** <li> If the cursor ends up past the target row (indicating the the target |
| 93265 | +** <li> If the cursor ends up past the target row (indicating that the target |
| 93058 | 93266 | ** row does not exist in the btree) then jump to SeekOP.P2. |
| 93059 | 93267 | ** </ol> |
| 93060 | 93268 | */ |
| 93061 | 93269 | case OP_SeekScan: { |
| 93062 | 93270 | VdbeCursor *pC; |
| | @@ -94388,11 +94596,13 @@ |
| 94388 | 94596 | rc = sqlite3VdbeSorterNext(db, pC); |
| 94389 | 94597 | goto next_tail; |
| 94390 | 94598 | |
| 94391 | 94599 | case OP_Prev: /* jump */ |
| 94392 | 94600 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 94393 | | - assert( pOp->p5<ArraySize(p->aCounter) ); |
| 94601 | + assert( pOp->p5==0 |
| 94602 | + || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP |
| 94603 | + || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); |
| 94394 | 94604 | pC = p->apCsr[pOp->p1]; |
| 94395 | 94605 | assert( pC!=0 ); |
| 94396 | 94606 | assert( pC->deferredMoveto==0 ); |
| 94397 | 94607 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 94398 | 94608 | assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| | @@ -94401,11 +94611,13 @@ |
| 94401 | 94611 | rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); |
| 94402 | 94612 | goto next_tail; |
| 94403 | 94613 | |
| 94404 | 94614 | case OP_Next: /* jump */ |
| 94405 | 94615 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 94406 | | - assert( pOp->p5<ArraySize(p->aCounter) ); |
| 94616 | + assert( pOp->p5==0 |
| 94617 | + || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP |
| 94618 | + || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); |
| 94407 | 94619 | pC = p->apCsr[pOp->p1]; |
| 94408 | 94620 | assert( pC!=0 ); |
| 94409 | 94621 | assert( pC->deferredMoveto==0 ); |
| 94410 | 94622 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 94411 | 94623 | assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
| | @@ -94608,14 +94820,14 @@ |
| 94608 | 94820 | |
| 94609 | 94821 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 94610 | 94822 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 94611 | 94823 | rc = sqlite3VdbeCursorRestore(pC); |
| 94612 | 94824 | |
| 94613 | | - /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
| 94614 | | - ** out from under the cursor. That will never happens for an IdxRowid |
| 94615 | | - ** or Seek opcode */ |
| 94616 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 94825 | + /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed |
| 94826 | + ** since it was last positioned and an error (e.g. OOM or an IO error) |
| 94827 | + ** occurs while trying to reposition it. */ |
| 94828 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 94617 | 94829 | |
| 94618 | 94830 | if( !pC->nullRow ){ |
| 94619 | 94831 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
| 94620 | 94832 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
| 94621 | 94833 | if( rc!=SQLITE_OK ){ |
| | @@ -95513,11 +95725,11 @@ |
| 95513 | 95725 | |
| 95514 | 95726 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 95515 | 95727 | ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) |
| 95516 | 95728 | ** |
| 95517 | 95729 | ** This opcode performs a commonly used computation associated with |
| 95518 | | -** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 95730 | +** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] |
| 95519 | 95731 | ** holds the offset counter. The opcode computes the combined value |
| 95520 | 95732 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 95521 | 95733 | ** value computed is the total number of rows that will need to be |
| 95522 | 95734 | ** visited in order to complete the query. |
| 95523 | 95735 | ** |
| | @@ -101110,10 +101322,12 @@ |
| 101110 | 101322 | sqlite3_file *pJfd, /* Preallocated, blank file handle */ |
| 101111 | 101323 | int flags, /* Opening flags */ |
| 101112 | 101324 | int nSpill /* Bytes buffered before opening the file */ |
| 101113 | 101325 | ){ |
| 101114 | 101326 | MemJournal *p = (MemJournal*)pJfd; |
| 101327 | + |
| 101328 | + assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) ); |
| 101115 | 101329 | |
| 101116 | 101330 | /* Zero the file-handle object. If nSpill was passed zero, initialize |
| 101117 | 101331 | ** it using the sqlite3OsOpen() function of the underlying VFS. In this |
| 101118 | 101332 | ** case none of the code in this module is executed as a result of calls |
| 101119 | 101333 | ** made on the journal file-handle. */ |
| | @@ -101552,13 +101766,11 @@ |
| 101552 | 101766 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 101553 | 101767 | if( ALWAYS(pExpr->y.pWin!=0) ){ |
| 101554 | 101768 | pExpr->y.pWin->pOwner = pExpr; |
| 101555 | 101769 | } |
| 101556 | 101770 | } |
| 101557 | | - sqlite3ParserAddCleanup(pParse, |
| 101558 | | - (void(*)(sqlite3*,void*))sqlite3ExprDelete, |
| 101559 | | - pDup); |
| 101771 | + sqlite3ExprDeferredDelete(pParse, pDup); |
| 101560 | 101772 | } |
| 101561 | 101773 | } |
| 101562 | 101774 | |
| 101563 | 101775 | /* |
| 101564 | 101776 | ** Subqueries stores the original database, table and column names for their |
| | @@ -104363,11 +104575,13 @@ |
| 104363 | 104575 | ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, |
| 104364 | 104576 | ** if appropriate. |
| 104365 | 104577 | */ |
| 104366 | 104578 | static void exprSetHeight(Expr *p){ |
| 104367 | 104579 | int nHeight = p->pLeft ? p->pLeft->nHeight : 0; |
| 104368 | | - if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight; |
| 104580 | + if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){ |
| 104581 | + nHeight = p->pRight->nHeight; |
| 104582 | + } |
| 104369 | 104583 | if( ExprUseXSelect(p) ){ |
| 104370 | 104584 | heightOfSelect(p->x.pSelect, &nHeight); |
| 104371 | 104585 | }else if( p->x.pList ){ |
| 104372 | 104586 | heightOfExprList(p->x.pList, &nHeight); |
| 104373 | 104587 | p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
| | @@ -104506,19 +104720,30 @@ |
| 104506 | 104720 | if( pRoot==0 ){ |
| 104507 | 104721 | assert( db->mallocFailed ); |
| 104508 | 104722 | sqlite3ExprDelete(db, pLeft); |
| 104509 | 104723 | sqlite3ExprDelete(db, pRight); |
| 104510 | 104724 | }else{ |
| 104725 | + assert( ExprUseXList(pRoot) ); |
| 104726 | + assert( pRoot->x.pSelect==0 ); |
| 104511 | 104727 | if( pRight ){ |
| 104512 | 104728 | pRoot->pRight = pRight; |
| 104513 | 104729 | pRoot->flags |= EP_Propagate & pRight->flags; |
| 104730 | +#if SQLITE_MAX_EXPR_DEPTH>0 |
| 104731 | + pRoot->nHeight = pRight->nHeight+1; |
| 104732 | + }else{ |
| 104733 | + pRoot->nHeight = 1; |
| 104734 | +#endif |
| 104514 | 104735 | } |
| 104515 | 104736 | if( pLeft ){ |
| 104516 | 104737 | pRoot->pLeft = pLeft; |
| 104517 | 104738 | pRoot->flags |= EP_Propagate & pLeft->flags; |
| 104739 | +#if SQLITE_MAX_EXPR_DEPTH>0 |
| 104740 | + if( pLeft->nHeight>=pRoot->nHeight ){ |
| 104741 | + pRoot->nHeight = pLeft->nHeight+1; |
| 104742 | + } |
| 104743 | +#endif |
| 104518 | 104744 | } |
| 104519 | | - exprSetHeight(pRoot); |
| 104520 | 104745 | } |
| 104521 | 104746 | } |
| 104522 | 104747 | |
| 104523 | 104748 | /* |
| 104524 | 104749 | ** Allocate an Expr node which joins as many as two subtrees. |
| | @@ -104800,10 +105025,11 @@ |
| 104800 | 105025 | /* |
| 104801 | 105026 | ** Recursively delete an expression tree. |
| 104802 | 105027 | */ |
| 104803 | 105028 | static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ |
| 104804 | 105029 | assert( p!=0 ); |
| 105030 | + assert( db!=0 ); |
| 104805 | 105031 | assert( !ExprUseUValue(p) || p->u.iValue>=0 ); |
| 104806 | 105032 | assert( !ExprUseYWin(p) || !ExprUseYSub(p) ); |
| 104807 | 105033 | assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed ); |
| 104808 | 105034 | assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) ); |
| 104809 | 105035 | #ifdef SQLITE_DEBUG |
| | @@ -104831,16 +105057,12 @@ |
| 104831 | 105057 | sqlite3WindowDelete(db, p->y.pWin); |
| 104832 | 105058 | } |
| 104833 | 105059 | #endif |
| 104834 | 105060 | } |
| 104835 | 105061 | } |
| 104836 | | - if( ExprHasProperty(p, EP_MemToken) ){ |
| 104837 | | - assert( !ExprHasProperty(p, EP_IntValue) ); |
| 104838 | | - sqlite3DbFree(db, p->u.zToken); |
| 104839 | | - } |
| 104840 | 105062 | if( !ExprHasProperty(p, EP_Static) ){ |
| 104841 | | - sqlite3DbFreeNN(db, p); |
| 105063 | + sqlite3DbNNFreeNN(db, p); |
| 104842 | 105064 | } |
| 104843 | 105065 | } |
| 104844 | 105066 | SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
| 104845 | 105067 | if( p ) sqlite3ExprDeleteNN(db, p); |
| 104846 | 105068 | } |
| | @@ -104867,12 +105089,13 @@ |
| 104867 | 105089 | ** |
| 104868 | 105090 | ** The deferred delete is (currently) implemented by adding the |
| 104869 | 105091 | ** pExpr to the pParse->pConstExpr list with a register number of 0. |
| 104870 | 105092 | */ |
| 104871 | 105093 | SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){ |
| 104872 | | - pParse->pConstExpr = |
| 104873 | | - sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr); |
| 105094 | + sqlite3ParserAddCleanup(pParse, |
| 105095 | + (void(*)(sqlite3*,void*))sqlite3ExprDelete, |
| 105096 | + pExpr); |
| 104874 | 105097 | } |
| 104875 | 105098 | |
| 104876 | 105099 | /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the |
| 104877 | 105100 | ** expression. |
| 104878 | 105101 | */ |
| | @@ -104942,11 +105165,10 @@ |
| 104942 | 105165 | ){ |
| 104943 | 105166 | nSize = EXPR_FULLSIZE; |
| 104944 | 105167 | }else{ |
| 104945 | 105168 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| 104946 | 105169 | assert( !ExprHasProperty(p, EP_OuterON) ); |
| 104947 | | - assert( !ExprHasProperty(p, EP_MemToken) ); |
| 104948 | 105170 | assert( !ExprHasVVAProperty(p, EP_NoReduce) ); |
| 104949 | 105171 | if( p->pLeft || p->x.pList ){ |
| 104950 | 105172 | nSize = EXPR_REDUCEDSIZE | EP_Reduced; |
| 104951 | 105173 | }else{ |
| 104952 | 105174 | assert( p->pRight==0 ); |
| | @@ -105046,11 +105268,11 @@ |
| 105046 | 105268 | memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
| 105047 | 105269 | } |
| 105048 | 105270 | } |
| 105049 | 105271 | |
| 105050 | 105272 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 105051 | | - pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 105273 | + pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static); |
| 105052 | 105274 | pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| 105053 | 105275 | pNew->flags |= staticFlag; |
| 105054 | 105276 | ExprClearVVAProperties(pNew); |
| 105055 | 105277 | if( dupFlags ){ |
| 105056 | 105278 | ExprSetVVAProperty(pNew, EP_Immutable); |
| | @@ -105622,16 +105844,17 @@ |
| 105622 | 105844 | */ |
| 105623 | 105845 | static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ |
| 105624 | 105846 | int i = pList->nExpr; |
| 105625 | 105847 | struct ExprList_item *pItem = pList->a; |
| 105626 | 105848 | assert( pList->nExpr>0 ); |
| 105849 | + assert( db!=0 ); |
| 105627 | 105850 | do{ |
| 105628 | 105851 | sqlite3ExprDelete(db, pItem->pExpr); |
| 105629 | | - sqlite3DbFree(db, pItem->zEName); |
| 105852 | + if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName); |
| 105630 | 105853 | pItem++; |
| 105631 | 105854 | }while( --i>0 ); |
| 105632 | | - sqlite3DbFreeNN(db, pList); |
| 105855 | + sqlite3DbNNFreeNN(db, pList); |
| 105633 | 105856 | } |
| 105634 | 105857 | SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
| 105635 | 105858 | if( pList ) exprListDeleteNN(db, pList); |
| 105636 | 105859 | } |
| 105637 | 105860 | |
| | @@ -106918,11 +107141,11 @@ |
| 106918 | 107141 | if( pLimit ){ |
| 106919 | 107142 | pLimit->affExpr = SQLITE_AFF_NUMERIC; |
| 106920 | 107143 | pLimit = sqlite3PExpr(pParse, TK_NE, |
| 106921 | 107144 | sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit); |
| 106922 | 107145 | } |
| 106923 | | - sqlite3ExprDelete(db, pSel->pLimit->pLeft); |
| 107146 | + sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft); |
| 106924 | 107147 | pSel->pLimit->pLeft = pLimit; |
| 106925 | 107148 | }else{ |
| 106926 | 107149 | /* If there is no pre-existing limit add a limit of 1 */ |
| 106927 | 107150 | pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1"); |
| 106928 | 107151 | pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); |
| | @@ -107432,11 +107655,11 @@ |
| 107432 | 107655 | u8 p5 /* P5 value for OP_Column + FLAGS */ |
| 107433 | 107656 | ){ |
| 107434 | 107657 | assert( pParse->pVdbe!=0 ); |
| 107435 | 107658 | sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg); |
| 107436 | 107659 | if( p5 ){ |
| 107437 | | - VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1); |
| 107660 | + VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); |
| 107438 | 107661 | if( pOp->opcode==OP_Column ) pOp->p5 = p5; |
| 107439 | 107662 | } |
| 107440 | 107663 | return iReg; |
| 107441 | 107664 | } |
| 107442 | 107665 | |
| | @@ -107501,11 +107724,11 @@ |
| 107501 | 107724 | /* |
| 107502 | 107725 | ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5) |
| 107503 | 107726 | ** so that a subsequent copy will not be merged into this one. |
| 107504 | 107727 | */ |
| 107505 | 107728 | static void setDoNotMergeFlagOnCopy(Vdbe *v){ |
| 107506 | | - if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){ |
| 107729 | + if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){ |
| 107507 | 107730 | sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */ |
| 107508 | 107731 | } |
| 107509 | 107732 | } |
| 107510 | 107733 | |
| 107511 | 107734 | /* |
| | @@ -107672,11 +107895,11 @@ |
| 107672 | 107895 | Table *pTab = pCol->pTab; |
| 107673 | 107896 | sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
| 107674 | 107897 | pCol->iSorterColumn, target); |
| 107675 | 107898 | if( pCol->iColumn<0 ){ |
| 107676 | 107899 | VdbeComment((v,"%s.rowid",pTab->zName)); |
| 107677 | | - }else{ |
| 107900 | + }else if( ALWAYS(pTab!=0) ){ |
| 107678 | 107901 | VdbeComment((v,"%s.%s", |
| 107679 | 107902 | pTab->zName, pTab->aCol[pCol->iColumn].zCnName)); |
| 107680 | 107903 | if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){ |
| 107681 | 107904 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 107682 | 107905 | } |
| | @@ -108267,10 +108490,25 @@ |
| 108267 | 108490 | ** on a LEFT JOIN NULL row. |
| 108268 | 108491 | */ |
| 108269 | 108492 | case TK_IF_NULL_ROW: { |
| 108270 | 108493 | int addrINR; |
| 108271 | 108494 | u8 okConstFactor = pParse->okConstFactor; |
| 108495 | + AggInfo *pAggInfo = pExpr->pAggInfo; |
| 108496 | + if( pAggInfo ){ |
| 108497 | + assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 108498 | + if( !pAggInfo->directMode ){ |
| 108499 | + inReg = pAggInfo->aCol[pExpr->iAgg].iMem; |
| 108500 | + break; |
| 108501 | + } |
| 108502 | + if( pExpr->pAggInfo->useSortingIdx ){ |
| 108503 | + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
| 108504 | + pAggInfo->aCol[pExpr->iAgg].iSorterColumn, |
| 108505 | + target); |
| 108506 | + inReg = target; |
| 108507 | + break; |
| 108508 | + } |
| 108509 | + } |
| 108272 | 108510 | addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable); |
| 108273 | 108511 | /* Temporarily disable factoring of constant expressions, since |
| 108274 | 108512 | ** even though expressions may appear to be constant, they are not |
| 108275 | 108513 | ** really constant because they originate from the right-hand side |
| 108276 | 108514 | ** of a LEFT JOIN. */ |
| | @@ -108608,11 +108846,11 @@ |
| 108608 | 108846 | }else{ |
| 108609 | 108847 | int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 108610 | 108848 | if( inReg!=target+i ){ |
| 108611 | 108849 | VdbeOp *pOp; |
| 108612 | 108850 | if( copyOp==OP_Copy |
| 108613 | | - && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy |
| 108851 | + && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy |
| 108614 | 108852 | && pOp->p1+pOp->p3+1==inReg |
| 108615 | 108853 | && pOp->p2+pOp->p3+1==target+i |
| 108616 | 108854 | && pOp->p5==0 /* The do-not-merge flag must be clear */ |
| 108617 | 108855 | ){ |
| 108618 | 108856 | pOp->p3++; |
| | @@ -109644,10 +109882,11 @@ |
| 109644 | 109882 | ** fact is exploited for efficiency. |
| 109645 | 109883 | */ |
| 109646 | 109884 | SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){ |
| 109647 | 109885 | Walker w; |
| 109648 | 109886 | struct RefSrcList x; |
| 109887 | + assert( pParse->db!=0 ); |
| 109649 | 109888 | memset(&w, 0, sizeof(w)); |
| 109650 | 109889 | memset(&x, 0, sizeof(x)); |
| 109651 | 109890 | w.xExprCallback = exprRefToSrcList; |
| 109652 | 109891 | w.xSelectCallback = selectRefEnter; |
| 109653 | 109892 | w.xSelectCallback2 = selectRefLeave; |
| | @@ -109660,11 +109899,11 @@ |
| 109660 | 109899 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 109661 | 109900 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 109662 | 109901 | sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter); |
| 109663 | 109902 | } |
| 109664 | 109903 | #endif |
| 109665 | | - sqlite3DbFree(pParse->db, x.aiExclude); |
| 109904 | + if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude); |
| 109666 | 109905 | if( w.eCode & 0x01 ){ |
| 109667 | 109906 | return 1; |
| 109668 | 109907 | }else if( w.eCode ){ |
| 109669 | 109908 | return 0; |
| 109670 | 109909 | }else{ |
| | @@ -109691,21 +109930,22 @@ |
| 109691 | 109930 | ){ |
| 109692 | 109931 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 109693 | 109932 | int iAgg = pExpr->iAgg; |
| 109694 | 109933 | Parse *pParse = pWalker->pParse; |
| 109695 | 109934 | sqlite3 *db = pParse->db; |
| 109696 | | - assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION ); |
| 109697 | | - if( pExpr->op==TK_AGG_COLUMN ){ |
| 109935 | + if( pExpr->op!=TK_AGG_FUNCTION ){ |
| 109936 | + assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_IF_NULL_ROW ); |
| 109698 | 109937 | assert( iAgg>=0 && iAgg<pAggInfo->nColumn ); |
| 109699 | 109938 | if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){ |
| 109700 | 109939 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 109701 | 109940 | if( pExpr ){ |
| 109702 | 109941 | pAggInfo->aCol[iAgg].pCExpr = pExpr; |
| 109703 | 109942 | sqlite3ExprDeferredDelete(pParse, pExpr); |
| 109704 | 109943 | } |
| 109705 | 109944 | } |
| 109706 | 109945 | }else{ |
| 109946 | + assert( pExpr->op==TK_AGG_FUNCTION ); |
| 109707 | 109947 | assert( iAgg>=0 && iAgg<pAggInfo->nFunc ); |
| 109708 | 109948 | if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){ |
| 109709 | 109949 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 109710 | 109950 | if( pExpr ){ |
| 109711 | 109951 | pAggInfo->aFunc[iAgg].pFExpr = pExpr; |
| | @@ -109772,14 +110012,16 @@ |
| 109772 | 110012 | SrcList *pSrcList = pNC->pSrcList; |
| 109773 | 110013 | AggInfo *pAggInfo = pNC->uNC.pAggInfo; |
| 109774 | 110014 | |
| 109775 | 110015 | assert( pNC->ncFlags & NC_UAggInfo ); |
| 109776 | 110016 | switch( pExpr->op ){ |
| 110017 | + case TK_IF_NULL_ROW: |
| 109777 | 110018 | case TK_AGG_COLUMN: |
| 109778 | 110019 | case TK_COLUMN: { |
| 109779 | 110020 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 109780 | 110021 | testcase( pExpr->op==TK_COLUMN ); |
| 110022 | + testcase( pExpr->op==TK_IF_NULL_ROW ); |
| 109781 | 110023 | /* Check to see if the column is in one of the tables in the FROM |
| 109782 | 110024 | ** clause of the aggregate query */ |
| 109783 | 110025 | if( ALWAYS(pSrcList!=0) ){ |
| 109784 | 110026 | SrcItem *pItem = pSrcList->a; |
| 109785 | 110027 | for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
| | @@ -109793,12 +110035,14 @@ |
| 109793 | 110035 | ** is not an entry there already. |
| 109794 | 110036 | */ |
| 109795 | 110037 | int k; |
| 109796 | 110038 | pCol = pAggInfo->aCol; |
| 109797 | 110039 | for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
| 109798 | | - if( pCol->iTable==pExpr->iTable && |
| 109799 | | - pCol->iColumn==pExpr->iColumn ){ |
| 110040 | + if( pCol->iTable==pExpr->iTable |
| 110041 | + && pCol->iColumn==pExpr->iColumn |
| 110042 | + && pExpr->op!=TK_IF_NULL_ROW |
| 110043 | + ){ |
| 109800 | 110044 | break; |
| 109801 | 110045 | } |
| 109802 | 110046 | } |
| 109803 | 110047 | if( (k>=pAggInfo->nColumn) |
| 109804 | 110048 | && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
| | @@ -109809,19 +110053,21 @@ |
| 109809 | 110053 | pCol->iTable = pExpr->iTable; |
| 109810 | 110054 | pCol->iColumn = pExpr->iColumn; |
| 109811 | 110055 | pCol->iMem = ++pParse->nMem; |
| 109812 | 110056 | pCol->iSorterColumn = -1; |
| 109813 | 110057 | pCol->pCExpr = pExpr; |
| 109814 | | - if( pAggInfo->pGroupBy ){ |
| 110058 | + if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){ |
| 109815 | 110059 | int j, n; |
| 109816 | 110060 | ExprList *pGB = pAggInfo->pGroupBy; |
| 109817 | 110061 | struct ExprList_item *pTerm = pGB->a; |
| 109818 | 110062 | n = pGB->nExpr; |
| 109819 | 110063 | for(j=0; j<n; j++, pTerm++){ |
| 109820 | 110064 | Expr *pE = pTerm->pExpr; |
| 109821 | | - if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
| 109822 | | - pE->iColumn==pExpr->iColumn ){ |
| 110065 | + if( pE->op==TK_COLUMN |
| 110066 | + && pE->iTable==pExpr->iTable |
| 110067 | + && pE->iColumn==pExpr->iColumn |
| 110068 | + ){ |
| 109823 | 110069 | pCol->iSorterColumn = j; |
| 109824 | 110070 | break; |
| 109825 | 110071 | } |
| 109826 | 110072 | } |
| 109827 | 110073 | } |
| | @@ -109834,11 +110080,13 @@ |
| 109834 | 110080 | ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
| 109835 | 110081 | ** pAggInfo->aCol[] entry. |
| 109836 | 110082 | */ |
| 109837 | 110083 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 109838 | 110084 | pExpr->pAggInfo = pAggInfo; |
| 109839 | | - pExpr->op = TK_AGG_COLUMN; |
| 110085 | + if( pExpr->op==TK_COLUMN ){ |
| 110086 | + pExpr->op = TK_AGG_COLUMN; |
| 110087 | + } |
| 109840 | 110088 | pExpr->iAgg = (i16)k; |
| 109841 | 110089 | break; |
| 109842 | 110090 | } /* endif pExpr->iTable==pItem->iCursor */ |
| 109843 | 110091 | } /* end loop over pSrcList */ |
| 109844 | 110092 | } |
| | @@ -115256,10 +115504,11 @@ |
| 115256 | 115504 | ** no VDBE code was generated. |
| 115257 | 115505 | */ |
| 115258 | 115506 | SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ |
| 115259 | 115507 | sqlite3 *db; |
| 115260 | 115508 | Vdbe *v; |
| 115509 | + int iDb, i; |
| 115261 | 115510 | |
| 115262 | 115511 | assert( pParse->pToplevel==0 ); |
| 115263 | 115512 | db = pParse->db; |
| 115264 | 115513 | assert( db->pParse==pParse ); |
| 115265 | 115514 | if( pParse->nested ) return; |
| | @@ -115285,11 +115534,10 @@ |
| 115285 | 115534 | || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
| 115286 | 115535 | if( v ){ |
| 115287 | 115536 | if( pParse->bReturning ){ |
| 115288 | 115537 | Returning *pReturning = pParse->u1.pReturning; |
| 115289 | 115538 | int addrRewind; |
| 115290 | | - int i; |
| 115291 | 115539 | int reg; |
| 115292 | 115540 | |
| 115293 | 115541 | if( pReturning->nRetCol ){ |
| 115294 | 115542 | sqlite3VdbeAddOp0(v, OP_FkCheck); |
| 115295 | 115543 | addrRewind = |
| | @@ -115322,80 +115570,73 @@ |
| 115322 | 115570 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 115323 | 115571 | ** set for each database that is used. Generate code to start a |
| 115324 | 115572 | ** transaction on each used database and to verify the schema cookie |
| 115325 | 115573 | ** on each used database. |
| 115326 | 115574 | */ |
| 115327 | | - if( db->mallocFailed==0 |
| 115328 | | - && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) |
| 115329 | | - ){ |
| 115330 | | - int iDb, i; |
| 115331 | | - assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 115332 | | - sqlite3VdbeJumpHere(v, 0); |
| 115333 | | - assert( db->nDb>0 ); |
| 115334 | | - iDb = 0; |
| 115335 | | - do{ |
| 115336 | | - Schema *pSchema; |
| 115337 | | - if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 115338 | | - sqlite3VdbeUsesBtree(v, iDb); |
| 115339 | | - pSchema = db->aDb[iDb].pSchema; |
| 115340 | | - sqlite3VdbeAddOp4Int(v, |
| 115341 | | - OP_Transaction, /* Opcode */ |
| 115342 | | - iDb, /* P1 */ |
| 115343 | | - DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
| 115344 | | - pSchema->schema_cookie, /* P3 */ |
| 115345 | | - pSchema->iGeneration /* P4 */ |
| 115346 | | - ); |
| 115347 | | - if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 115348 | | - VdbeComment((v, |
| 115349 | | - "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 115350 | | - }while( ++iDb<db->nDb ); |
| 115575 | + assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 115576 | + sqlite3VdbeJumpHere(v, 0); |
| 115577 | + assert( db->nDb>0 ); |
| 115578 | + iDb = 0; |
| 115579 | + do{ |
| 115580 | + Schema *pSchema; |
| 115581 | + if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 115582 | + sqlite3VdbeUsesBtree(v, iDb); |
| 115583 | + pSchema = db->aDb[iDb].pSchema; |
| 115584 | + sqlite3VdbeAddOp4Int(v, |
| 115585 | + OP_Transaction, /* Opcode */ |
| 115586 | + iDb, /* P1 */ |
| 115587 | + DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
| 115588 | + pSchema->schema_cookie, /* P3 */ |
| 115589 | + pSchema->iGeneration /* P4 */ |
| 115590 | + ); |
| 115591 | + if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 115592 | + VdbeComment((v, |
| 115593 | + "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 115594 | + }while( ++iDb<db->nDb ); |
| 115351 | 115595 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115352 | | - for(i=0; i<pParse->nVtabLock; i++){ |
| 115353 | | - char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 115354 | | - sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 115355 | | - } |
| 115356 | | - pParse->nVtabLock = 0; |
| 115596 | + for(i=0; i<pParse->nVtabLock; i++){ |
| 115597 | + char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 115598 | + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 115599 | + } |
| 115600 | + pParse->nVtabLock = 0; |
| 115357 | 115601 | #endif |
| 115358 | 115602 | |
| 115359 | | - /* Once all the cookies have been verified and transactions opened, |
| 115360 | | - ** obtain the required table-locks. This is a no-op unless the |
| 115361 | | - ** shared-cache feature is enabled. |
| 115362 | | - */ |
| 115363 | | - codeTableLocks(pParse); |
| 115364 | | - |
| 115365 | | - /* Initialize any AUTOINCREMENT data structures required. |
| 115366 | | - */ |
| 115367 | | - sqlite3AutoincrementBegin(pParse); |
| 115368 | | - |
| 115369 | | - /* Code constant expressions that where factored out of inner loops. |
| 115370 | | - ** |
| 115371 | | - ** The pConstExpr list might also contain expressions that we simply |
| 115372 | | - ** want to keep around until the Parse object is deleted. Such |
| 115373 | | - ** expressions have iConstExprReg==0. Do not generate code for |
| 115374 | | - ** those expressions, of course. |
| 115375 | | - */ |
| 115376 | | - if( pParse->pConstExpr ){ |
| 115377 | | - ExprList *pEL = pParse->pConstExpr; |
| 115378 | | - pParse->okConstFactor = 0; |
| 115379 | | - for(i=0; i<pEL->nExpr; i++){ |
| 115380 | | - int iReg = pEL->a[i].u.iConstExprReg; |
| 115381 | | - if( iReg>0 ){ |
| 115382 | | - sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg); |
| 115383 | | - } |
| 115384 | | - } |
| 115385 | | - } |
| 115386 | | - |
| 115387 | | - if( pParse->bReturning ){ |
| 115388 | | - Returning *pRet = pParse->u1.pReturning; |
| 115389 | | - if( pRet->nRetCol ){ |
| 115390 | | - sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); |
| 115391 | | - } |
| 115392 | | - } |
| 115393 | | - |
| 115394 | | - /* Finally, jump back to the beginning of the executable code. */ |
| 115395 | | - sqlite3VdbeGoto(v, 1); |
| 115396 | | - } |
| 115603 | + /* Once all the cookies have been verified and transactions opened, |
| 115604 | + ** obtain the required table-locks. This is a no-op unless the |
| 115605 | + ** shared-cache feature is enabled. |
| 115606 | + */ |
| 115607 | + codeTableLocks(pParse); |
| 115608 | + |
| 115609 | + /* Initialize any AUTOINCREMENT data structures required. |
| 115610 | + */ |
| 115611 | + sqlite3AutoincrementBegin(pParse); |
| 115612 | + |
| 115613 | + /* Code constant expressions that where factored out of inner loops. |
| 115614 | + ** |
| 115615 | + ** The pConstExpr list might also contain expressions that we simply |
| 115616 | + ** want to keep around until the Parse object is deleted. Such |
| 115617 | + ** expressions have iConstExprReg==0. Do not generate code for |
| 115618 | + ** those expressions, of course. |
| 115619 | + */ |
| 115620 | + if( pParse->pConstExpr ){ |
| 115621 | + ExprList *pEL = pParse->pConstExpr; |
| 115622 | + pParse->okConstFactor = 0; |
| 115623 | + for(i=0; i<pEL->nExpr; i++){ |
| 115624 | + int iReg = pEL->a[i].u.iConstExprReg; |
| 115625 | + sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg); |
| 115626 | + } |
| 115627 | + } |
| 115628 | + |
| 115629 | + if( pParse->bReturning ){ |
| 115630 | + Returning *pRet = pParse->u1.pReturning; |
| 115631 | + if( pRet->nRetCol ){ |
| 115632 | + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); |
| 115633 | + } |
| 115634 | + } |
| 115635 | + |
| 115636 | + /* Finally, jump back to the beginning of the executable code. */ |
| 115637 | + sqlite3VdbeGoto(v, 1); |
| 115397 | 115638 | } |
| 115398 | 115639 | |
| 115399 | 115640 | /* Get the VDBE program ready for execution |
| 115400 | 115641 | */ |
| 115401 | 115642 | assert( v!=0 || pParse->nErr ); |
| | @@ -115898,20 +116139,21 @@ |
| 115898 | 116139 | */ |
| 115899 | 116140 | SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
| 115900 | 116141 | int i; |
| 115901 | 116142 | Column *pCol; |
| 115902 | 116143 | assert( pTable!=0 ); |
| 116144 | + assert( db!=0 ); |
| 115903 | 116145 | if( (pCol = pTable->aCol)!=0 ){ |
| 115904 | 116146 | for(i=0; i<pTable->nCol; i++, pCol++){ |
| 115905 | 116147 | assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) ); |
| 115906 | 116148 | sqlite3DbFree(db, pCol->zCnName); |
| 115907 | 116149 | } |
| 115908 | | - sqlite3DbFree(db, pTable->aCol); |
| 116150 | + sqlite3DbNNFreeNN(db, pTable->aCol); |
| 115909 | 116151 | if( IsOrdinaryTable(pTable) ){ |
| 115910 | 116152 | sqlite3ExprListDelete(db, pTable->u.tab.pDfltList); |
| 115911 | 116153 | } |
| 115912 | | - if( db==0 || db->pnBytesFreed==0 ){ |
| 116154 | + if( db->pnBytesFreed==0 ){ |
| 115913 | 116155 | pTable->aCol = 0; |
| 115914 | 116156 | pTable->nCol = 0; |
| 115915 | 116157 | if( IsOrdinaryTable(pTable) ){ |
| 115916 | 116158 | pTable->u.tab.pDfltList = 0; |
| 115917 | 116159 | } |
| | @@ -115944,21 +116186,22 @@ |
| 115944 | 116186 | ** |
| 115945 | 116187 | ** If malloc has already failed, it may be that it failed while allocating |
| 115946 | 116188 | ** a Table object that was going to be marked ephemeral. So do not check |
| 115947 | 116189 | ** that no lookaside memory is used in this case either. */ |
| 115948 | 116190 | int nLookaside = 0; |
| 115949 | | - if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
| 116191 | + assert( db!=0 ); |
| 116192 | + if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
| 115950 | 116193 | nLookaside = sqlite3LookasideUsed(db, 0); |
| 115951 | 116194 | } |
| 115952 | 116195 | #endif |
| 115953 | 116196 | |
| 115954 | 116197 | /* Delete all indices associated with this table. */ |
| 115955 | 116198 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 115956 | 116199 | pNext = pIndex->pNext; |
| 115957 | 116200 | assert( pIndex->pSchema==pTable->pSchema |
| 115958 | 116201 | || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
| 115959 | | - if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ |
| 116202 | + if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){ |
| 115960 | 116203 | char *zName = pIndex->zName; |
| 115961 | 116204 | TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
| 115962 | 116205 | &pIndex->pSchema->idxHash, zName, 0 |
| 115963 | 116206 | ); |
| 115964 | 116207 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
| | @@ -115991,12 +116234,13 @@ |
| 115991 | 116234 | /* Verify that no lookaside memory was used by schema tables */ |
| 115992 | 116235 | assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); |
| 115993 | 116236 | } |
| 115994 | 116237 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 115995 | 116238 | /* Do not delete the table until the reference count reaches zero. */ |
| 116239 | + assert( db!=0 ); |
| 115996 | 116240 | if( !pTable ) return; |
| 115997 | | - if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; |
| 116241 | + if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return; |
| 115998 | 116242 | deleteTable(db, pTable); |
| 115999 | 116243 | } |
| 116000 | 116244 | |
| 116001 | 116245 | |
| 116002 | 116246 | /* |
| | @@ -118165,11 +118409,11 @@ |
| 118165 | 118409 | /* |
| 118166 | 118410 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 118167 | 118411 | ** the columns of the view in the pTable structure. Return the number |
| 118168 | 118412 | ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
| 118169 | 118413 | */ |
| 118170 | | -SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 118414 | +static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){ |
| 118171 | 118415 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 118172 | 118416 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 118173 | 118417 | int nErr = 0; /* Number of errors encountered */ |
| 118174 | 118418 | sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
| 118175 | 118419 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -118190,13 +118434,14 @@ |
| 118190 | 118434 | } |
| 118191 | 118435 | #endif |
| 118192 | 118436 | |
| 118193 | 118437 | #ifndef SQLITE_OMIT_VIEW |
| 118194 | 118438 | /* A positive nCol means the columns names for this view are |
| 118195 | | - ** already known. |
| 118439 | + ** already known. This routine is not called unless either the |
| 118440 | + ** table is virtual or nCol is zero. |
| 118196 | 118441 | */ |
| 118197 | | - if( pTable->nCol>0 ) return 0; |
| 118442 | + assert( pTable->nCol<=0 ); |
| 118198 | 118443 | |
| 118199 | 118444 | /* A negative nCol is a special marker meaning that we are currently |
| 118200 | 118445 | ** trying to compute the column names. If we enter this routine with |
| 118201 | 118446 | ** a negative nCol, it means two or more views form a loop, like this: |
| 118202 | 118447 | ** |
| | @@ -118287,10 +118532,15 @@ |
| 118287 | 118532 | if( db->mallocFailed ){ |
| 118288 | 118533 | sqlite3DeleteColumnNames(db, pTable); |
| 118289 | 118534 | } |
| 118290 | 118535 | #endif /* SQLITE_OMIT_VIEW */ |
| 118291 | 118536 | return nErr; |
| 118537 | +} |
| 118538 | +SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 118539 | + assert( pTable!=0 ); |
| 118540 | + if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0; |
| 118541 | + return viewGetColumnNames(pParse, pTable); |
| 118292 | 118542 | } |
| 118293 | 118543 | #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 118294 | 118544 | |
| 118295 | 118545 | #ifndef SQLITE_OMIT_VIEW |
| 118296 | 118546 | /* |
| | @@ -119153,11 +119403,11 @@ |
| 119153 | 119403 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
| 119154 | 119404 | goto exit_create_index; |
| 119155 | 119405 | } |
| 119156 | 119406 | if( !IN_RENAME_OBJECT ){ |
| 119157 | 119407 | if( !db->init.busy ){ |
| 119158 | | - if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| 119408 | + if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ |
| 119159 | 119409 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 119160 | 119410 | goto exit_create_index; |
| 119161 | 119411 | } |
| 119162 | 119412 | } |
| 119163 | 119413 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| | @@ -119806,16 +120056,17 @@ |
| 119806 | 120056 | /* |
| 119807 | 120057 | ** Delete an IdList. |
| 119808 | 120058 | */ |
| 119809 | 120059 | SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
| 119810 | 120060 | int i; |
| 120061 | + assert( db!=0 ); |
| 119811 | 120062 | if( pList==0 ) return; |
| 119812 | 120063 | assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */ |
| 119813 | 120064 | for(i=0; i<pList->nId; i++){ |
| 119814 | 120065 | sqlite3DbFree(db, pList->a[i].zName); |
| 119815 | 120066 | } |
| 119816 | | - sqlite3DbFreeNN(db, pList); |
| 120067 | + sqlite3DbNNFreeNN(db, pList); |
| 119817 | 120068 | } |
| 119818 | 120069 | |
| 119819 | 120070 | /* |
| 119820 | 120071 | ** Return the index in pList of the identifier named zId. Return -1 |
| 119821 | 120072 | ** if not found. |
| | @@ -120014,15 +120265,16 @@ |
| 120014 | 120265 | ** Delete an entire SrcList including all its substructure. |
| 120015 | 120266 | */ |
| 120016 | 120267 | SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
| 120017 | 120268 | int i; |
| 120018 | 120269 | SrcItem *pItem; |
| 120270 | + assert( db!=0 ); |
| 120019 | 120271 | if( pList==0 ) return; |
| 120020 | 120272 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
| 120021 | | - if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase); |
| 120022 | | - sqlite3DbFree(db, pItem->zName); |
| 120023 | | - if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias); |
| 120273 | + if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase); |
| 120274 | + if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName); |
| 120275 | + if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias); |
| 120024 | 120276 | if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 120025 | 120277 | if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
| 120026 | 120278 | sqlite3DeleteTable(db, pItem->pTab); |
| 120027 | 120279 | if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect); |
| 120028 | 120280 | if( pItem->fg.isUsing ){ |
| | @@ -120029,11 +120281,11 @@ |
| 120029 | 120281 | sqlite3IdListDelete(db, pItem->u3.pUsing); |
| 120030 | 120282 | }else if( pItem->u3.pOn ){ |
| 120031 | 120283 | sqlite3ExprDelete(db, pItem->u3.pOn); |
| 120032 | 120284 | } |
| 120033 | 120285 | } |
| 120034 | | - sqlite3DbFreeNN(db, pList); |
| 120286 | + sqlite3DbNNFreeNN(db, pList); |
| 120035 | 120287 | } |
| 120036 | 120288 | |
| 120037 | 120289 | /* |
| 120038 | 120290 | ** This routine is called by the parser to add a new term to the |
| 120039 | 120291 | ** end of a growing FROM clause. The "p" parameter is the part of |
| | @@ -121281,23 +121533,25 @@ |
| 121281 | 121533 | SQLITE_PRIVATE void sqlite3SchemaClear(void *p){ |
| 121282 | 121534 | Hash temp1; |
| 121283 | 121535 | Hash temp2; |
| 121284 | 121536 | HashElem *pElem; |
| 121285 | 121537 | Schema *pSchema = (Schema *)p; |
| 121538 | + sqlite3 xdb; |
| 121286 | 121539 | |
| 121540 | + memset(&xdb, 0, sizeof(xdb)); |
| 121287 | 121541 | temp1 = pSchema->tblHash; |
| 121288 | 121542 | temp2 = pSchema->trigHash; |
| 121289 | 121543 | sqlite3HashInit(&pSchema->trigHash); |
| 121290 | 121544 | sqlite3HashClear(&pSchema->idxHash); |
| 121291 | 121545 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
| 121292 | | - sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); |
| 121546 | + sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem)); |
| 121293 | 121547 | } |
| 121294 | 121548 | sqlite3HashClear(&temp2); |
| 121295 | 121549 | sqlite3HashInit(&pSchema->tblHash); |
| 121296 | 121550 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 121297 | 121551 | Table *pTab = sqliteHashData(pElem); |
| 121298 | | - sqlite3DeleteTable(0, pTab); |
| 121552 | + sqlite3DeleteTable(&xdb, pTab); |
| 121299 | 121553 | } |
| 121300 | 121554 | sqlite3HashClear(&temp1); |
| 121301 | 121555 | sqlite3HashClear(&pSchema->fkeyHash); |
| 121302 | 121556 | pSchema->pSeqTab = 0; |
| 121303 | 121557 | if( pSchema->schemaFlags & DB_SchemaLoaded ){ |
| | @@ -121392,22 +121646,46 @@ |
| 121392 | 121646 | ** A table is read-only if any of the following are true: |
| 121393 | 121647 | ** |
| 121394 | 121648 | ** 1) It is a virtual table and no implementation of the xUpdate method |
| 121395 | 121649 | ** has been provided |
| 121396 | 121650 | ** |
| 121397 | | -** 2) It is a system table (i.e. sqlite_schema), this call is not |
| 121651 | +** 2) A trigger is currently being coded and the table is a virtual table |
| 121652 | +** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and |
| 121653 | +** the table is not SQLITE_VTAB_INNOCUOUS. |
| 121654 | +** |
| 121655 | +** 3) It is a system table (i.e. sqlite_schema), this call is not |
| 121398 | 121656 | ** part of a nested parse and writable_schema pragma has not |
| 121399 | 121657 | ** been specified |
| 121400 | 121658 | ** |
| 121401 | | -** 3) The table is a shadow table, the database connection is in |
| 121659 | +** 4) The table is a shadow table, the database connection is in |
| 121402 | 121660 | ** defensive mode, and the current sqlite3_prepare() |
| 121403 | 121661 | ** is for a top-level SQL statement. |
| 121404 | 121662 | */ |
| 121663 | +static int vtabIsReadOnly(Parse *pParse, Table *pTab){ |
| 121664 | + if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){ |
| 121665 | + return 1; |
| 121666 | + } |
| 121667 | + |
| 121668 | + /* Within triggers: |
| 121669 | + ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY |
| 121670 | + ** virtual tables |
| 121671 | + ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS |
| 121672 | + ** virtual tables if PRAGMA trusted_schema=ON. |
| 121673 | + */ |
| 121674 | + if( pParse->pToplevel!=0 |
| 121675 | + && pTab->u.vtab.p->eVtabRisk > |
| 121676 | + ((pParse->db->flags & SQLITE_TrustedSchema)!=0) |
| 121677 | + ){ |
| 121678 | + sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"", |
| 121679 | + pTab->zName); |
| 121680 | + } |
| 121681 | + return 0; |
| 121682 | +} |
| 121405 | 121683 | static int tabIsReadOnly(Parse *pParse, Table *pTab){ |
| 121406 | 121684 | sqlite3 *db; |
| 121407 | 121685 | if( IsVirtual(pTab) ){ |
| 121408 | | - return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0; |
| 121686 | + return vtabIsReadOnly(pParse, pTab); |
| 121409 | 121687 | } |
| 121410 | 121688 | if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0; |
| 121411 | 121689 | db = pParse->db; |
| 121412 | 121690 | if( (pTab->tabFlags & TF_Readonly)!=0 ){ |
| 121413 | 121691 | return sqlite3WritableSchema(db)==0 && pParse->nested==0; |
| | @@ -121415,13 +121693,15 @@ |
| 121415 | 121693 | assert( pTab->tabFlags & TF_Shadow ); |
| 121416 | 121694 | return sqlite3ReadOnlyShadowTables(db); |
| 121417 | 121695 | } |
| 121418 | 121696 | |
| 121419 | 121697 | /* |
| 121420 | | -** Check to make sure the given table is writable. If it is not |
| 121421 | | -** writable, generate an error message and return 1. If it is |
| 121422 | | -** writable return 0; |
| 121698 | +** Check to make sure the given table is writable. |
| 121699 | +** |
| 121700 | +** If pTab is not writable -> generate an error message and return 1. |
| 121701 | +** If pTab is writable but other errors have occurred -> return 1. |
| 121702 | +** If pTab is writable and no prior errors -> return 0; |
| 121423 | 121703 | */ |
| 121424 | 121704 | SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
| 121425 | 121705 | if( tabIsReadOnly(pParse, pTab) ){ |
| 121426 | 121706 | sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
| 121427 | 121707 | return 1; |
| | @@ -121778,13 +122058,14 @@ |
| 121778 | 122058 | sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1, |
| 121779 | 122059 | pTab->zName, P4_STATIC); |
| 121780 | 122060 | } |
| 121781 | 122061 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 121782 | 122062 | assert( pIdx->pSchema==pTab->pSchema ); |
| 121783 | | - sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
| 121784 | 122063 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
| 121785 | | - sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1); |
| 122064 | + sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1); |
| 122065 | + }else{ |
| 122066 | + sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
| 121786 | 122067 | } |
| 121787 | 122068 | } |
| 121788 | 122069 | }else |
| 121789 | 122070 | #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ |
| 121790 | 122071 | { |
| | @@ -121980,11 +122261,11 @@ |
| 121980 | 122261 | sqlite3ExprDelete(db, pWhere); |
| 121981 | 122262 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) |
| 121982 | 122263 | sqlite3ExprListDelete(db, pOrderBy); |
| 121983 | 122264 | sqlite3ExprDelete(db, pLimit); |
| 121984 | 122265 | #endif |
| 121985 | | - sqlite3DbFree(db, aToOpen); |
| 122266 | + if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen); |
| 121986 | 122267 | return; |
| 121987 | 122268 | } |
| 121988 | 122269 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 121989 | 122270 | ** they may interfere with compilation of other functions in this file |
| 121990 | 122271 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| | @@ -126148,15 +126429,16 @@ |
| 126148 | 126429 | SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){ |
| 126149 | 126430 | FKey *pFKey; /* Iterator variable */ |
| 126150 | 126431 | FKey *pNext; /* Copy of pFKey->pNextFrom */ |
| 126151 | 126432 | |
| 126152 | 126433 | assert( IsOrdinaryTable(pTab) ); |
| 126434 | + assert( db!=0 ); |
| 126153 | 126435 | for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){ |
| 126154 | 126436 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) ); |
| 126155 | 126437 | |
| 126156 | 126438 | /* Remove the FK from the fkeyHash hash table. */ |
| 126157 | | - if( !db || db->pnBytesFreed==0 ){ |
| 126439 | + if( db->pnBytesFreed==0 ){ |
| 126158 | 126440 | if( pFKey->pPrevTo ){ |
| 126159 | 126441 | pFKey->pPrevTo->pNextTo = pFKey->pNextTo; |
| 126160 | 126442 | }else{ |
| 126161 | 126443 | void *p = (void *)pFKey->pNextTo; |
| 126162 | 126444 | const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo); |
| | @@ -126345,11 +126627,11 @@ |
| 126345 | 126627 | /* Move the previous opcode (which should be OP_MakeRecord) forward |
| 126346 | 126628 | ** by one slot and insert a new OP_TypeCheck where the current |
| 126347 | 126629 | ** OP_MakeRecord is found */ |
| 126348 | 126630 | VdbeOp *pPrev; |
| 126349 | 126631 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 126350 | | - pPrev = sqlite3VdbeGetOp(v, -1); |
| 126632 | + pPrev = sqlite3VdbeGetLastOp(v); |
| 126351 | 126633 | assert( pPrev!=0 ); |
| 126352 | 126634 | assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed ); |
| 126353 | 126635 | pPrev->opcode = OP_TypeCheck; |
| 126354 | 126636 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3); |
| 126355 | 126637 | }else{ |
| | @@ -126383,11 +126665,11 @@ |
| 126383 | 126665 | i = sqlite3Strlen30NN(zColAff); |
| 126384 | 126666 | if( i ){ |
| 126385 | 126667 | if( iReg ){ |
| 126386 | 126668 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
| 126387 | 126669 | }else{ |
| 126388 | | - assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord |
| 126670 | + assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord |
| 126389 | 126671 | || sqlite3VdbeDb(v)->mallocFailed ); |
| 126390 | 126672 | sqlite3VdbeChangeP4(v, -1, zColAff, i); |
| 126391 | 126673 | } |
| 126392 | 126674 | } |
| 126393 | 126675 | } |
| | @@ -126469,11 +126751,11 @@ |
| 126469 | 126751 | /* Before computing generated columns, first go through and make sure |
| 126470 | 126752 | ** that appropriate affinity has been applied to the regular columns |
| 126471 | 126753 | */ |
| 126472 | 126754 | sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); |
| 126473 | 126755 | if( (pTab->tabFlags & TF_HasStored)!=0 ){ |
| 126474 | | - pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1); |
| 126756 | + pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); |
| 126475 | 126757 | if( pOp->opcode==OP_Affinity ){ |
| 126476 | 126758 | /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 126477 | 126759 | ** columns. '@' is the no-op affinity and those columns have not |
| 126478 | 126760 | ** yet been computed. */ |
| 126479 | 126761 | int ii, jj; |
| | @@ -127375,11 +127657,16 @@ |
| 127375 | 127657 | }else if( pSelect ){ |
| 127376 | 127658 | if( regFromSelect!=regData ){ |
| 127377 | 127659 | sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore); |
| 127378 | 127660 | } |
| 127379 | 127661 | }else{ |
| 127380 | | - sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore); |
| 127662 | + Expr *pX = pList->a[k].pExpr; |
| 127663 | + int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore); |
| 127664 | + if( y!=iRegStore ){ |
| 127665 | + sqlite3VdbeAddOp2(v, |
| 127666 | + ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore); |
| 127667 | + } |
| 127381 | 127668 | } |
| 127382 | 127669 | } |
| 127383 | 127670 | |
| 127384 | 127671 | |
| 127385 | 127672 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
| | @@ -127512,11 +127799,13 @@ |
| 127512 | 127799 | int isReplace = 0;/* Set to true if constraints may cause a replace */ |
| 127513 | 127800 | int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ |
| 127514 | 127801 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
| 127515 | 127802 | regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert |
| 127516 | 127803 | ); |
| 127517 | | - sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
| 127804 | + if( db->flags & SQLITE_ForeignKeys ){ |
| 127805 | + sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
| 127806 | + } |
| 127518 | 127807 | |
| 127519 | 127808 | /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE |
| 127520 | 127809 | ** constraints or (b) there are no triggers and this table is not a |
| 127521 | 127810 | ** parent table in a foreign key constraint. It is safe to set the |
| 127522 | 127811 | ** flag in the second case as if any REPLACE constraint is hit, an |
| | @@ -127596,11 +127885,11 @@ |
| 127596 | 127885 | sqlite3SrcListDelete(db, pTabList); |
| 127597 | 127886 | sqlite3ExprListDelete(db, pList); |
| 127598 | 127887 | sqlite3UpsertDelete(db, pUpsert); |
| 127599 | 127888 | sqlite3SelectDelete(db, pSelect); |
| 127600 | 127889 | sqlite3IdListDelete(db, pColumn); |
| 127601 | | - sqlite3DbFree(db, aRegIdx); |
| 127890 | + if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx); |
| 127602 | 127891 | } |
| 127603 | 127892 | |
| 127604 | 127893 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 127605 | 127894 | ** they may interfere with compilation of other functions in this file |
| 127606 | 127895 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| | @@ -132702,19 +132991,21 @@ |
| 132702 | 132991 | ** Setting to a null string reverts to the default temporary directory search. |
| 132703 | 132992 | ** If temporary directory is changed, then invalidateTempStorage. |
| 132704 | 132993 | ** |
| 132705 | 132994 | */ |
| 132706 | 132995 | case PragTyp_TEMP_STORE_DIRECTORY: { |
| 132996 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132707 | 132997 | if( !zRight ){ |
| 132708 | 132998 | returnSingleText(v, sqlite3_temp_directory); |
| 132709 | 132999 | }else{ |
| 132710 | 133000 | #ifndef SQLITE_OMIT_WSD |
| 132711 | 133001 | if( zRight[0] ){ |
| 132712 | 133002 | int res; |
| 132713 | 133003 | rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 132714 | 133004 | if( rc!=SQLITE_OK || res==0 ){ |
| 132715 | 133005 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 133006 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132716 | 133007 | goto pragma_out; |
| 132717 | 133008 | } |
| 132718 | 133009 | } |
| 132719 | 133010 | if( SQLITE_TEMP_STORE==0 |
| 132720 | 133011 | || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) |
| | @@ -132728,10 +133019,11 @@ |
| 132728 | 133019 | }else{ |
| 132729 | 133020 | sqlite3_temp_directory = 0; |
| 132730 | 133021 | } |
| 132731 | 133022 | #endif /* SQLITE_OMIT_WSD */ |
| 132732 | 133023 | } |
| 133024 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132733 | 133025 | break; |
| 132734 | 133026 | } |
| 132735 | 133027 | |
| 132736 | 133028 | #if SQLITE_OS_WIN |
| 132737 | 133029 | /* |
| | @@ -132746,19 +133038,21 @@ |
| 132746 | 133038 | ** process. Database file specified with an absolute path are not impacted |
| 132747 | 133039 | ** by this setting, regardless of its value. |
| 132748 | 133040 | ** |
| 132749 | 133041 | */ |
| 132750 | 133042 | case PragTyp_DATA_STORE_DIRECTORY: { |
| 133043 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132751 | 133044 | if( !zRight ){ |
| 132752 | 133045 | returnSingleText(v, sqlite3_data_directory); |
| 132753 | 133046 | }else{ |
| 132754 | 133047 | #ifndef SQLITE_OMIT_WSD |
| 132755 | 133048 | if( zRight[0] ){ |
| 132756 | 133049 | int res; |
| 132757 | 133050 | rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 132758 | 133051 | if( rc!=SQLITE_OK || res==0 ){ |
| 132759 | 133052 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 133053 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132760 | 133054 | goto pragma_out; |
| 132761 | 133055 | } |
| 132762 | 133056 | } |
| 132763 | 133057 | sqlite3_free(sqlite3_data_directory); |
| 132764 | 133058 | if( zRight[0] ){ |
| | @@ -132766,10 +133060,11 @@ |
| 132766 | 133060 | }else{ |
| 132767 | 133061 | sqlite3_data_directory = 0; |
| 132768 | 133062 | } |
| 132769 | 133063 | #endif /* SQLITE_OMIT_WSD */ |
| 132770 | 133064 | } |
| 133065 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 132771 | 133066 | break; |
| 132772 | 133067 | } |
| 132773 | 133068 | #endif |
| 132774 | 133069 | |
| 132775 | 133070 | #if SQLITE_ENABLE_LOCKING_STYLE |
| | @@ -133479,19 +133774,27 @@ |
| 133479 | 133774 | /* Make sure all the indices are constructed correctly. |
| 133480 | 133775 | */ |
| 133481 | 133776 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
| 133482 | 133777 | Table *pTab = sqliteHashData(x); |
| 133483 | 133778 | Index *pIdx, *pPk; |
| 133484 | | - Index *pPrior = 0; |
| 133779 | + Index *pPrior = 0; /* Previous index */ |
| 133485 | 133780 | int loopTop; |
| 133486 | 133781 | int iDataCur, iIdxCur; |
| 133487 | 133782 | int r1 = -1; |
| 133488 | 133783 | int bStrict; |
| 133784 | + int r2; /* Previous key for WITHOUT ROWID tables */ |
| 133489 | 133785 | |
| 133490 | 133786 | if( !IsOrdinaryTable(pTab) ) continue; |
| 133491 | 133787 | if( pObjTab && pObjTab!=pTab ) continue; |
| 133492 | | - pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
| 133788 | + if( isQuick || HasRowid(pTab) ){ |
| 133789 | + pPk = 0; |
| 133790 | + r2 = 0; |
| 133791 | + }else{ |
| 133792 | + pPk = sqlite3PrimaryKeyIndex(pTab); |
| 133793 | + r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 133794 | + sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1); |
| 133795 | + } |
| 133493 | 133796 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, |
| 133494 | 133797 | 1, 0, &iDataCur, &iIdxCur); |
| 133495 | 133798 | /* reg[7] counts the number of entries in the table. |
| 133496 | 133799 | ** reg[8+i] counts the number of entries in the i-th index |
| 133497 | 133800 | */ |
| | @@ -133506,10 +133809,28 @@ |
| 133506 | 133809 | if( !isQuick ){ |
| 133507 | 133810 | /* Sanity check on record header decoding */ |
| 133508 | 133811 | sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3); |
| 133509 | 133812 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 133510 | 133813 | VdbeComment((v, "(right-most column)")); |
| 133814 | + if( pPk ){ |
| 133815 | + /* Verify WITHOUT ROWID keys are in ascending order */ |
| 133816 | + int a1; |
| 133817 | + char *zErr; |
| 133818 | + a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol); |
| 133819 | + VdbeCoverage(v); |
| 133820 | + sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v); |
| 133821 | + zErr = sqlite3MPrintf(db, |
| 133822 | + "row not in PRIMARY KEY order for %s", |
| 133823 | + pTab->zName); |
| 133824 | + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 133825 | + integrityCheckResultRow(v); |
| 133826 | + sqlite3VdbeJumpHere(v, a1); |
| 133827 | + sqlite3VdbeJumpHere(v, a1+1); |
| 133828 | + for(j=0; j<pPk->nKeyCol; j++){ |
| 133829 | + sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j); |
| 133830 | + } |
| 133831 | + } |
| 133511 | 133832 | } |
| 133512 | 133833 | /* Verify that all NOT NULL columns really are NOT NULL. At the |
| 133513 | 133834 | ** same time verify the type of the content of STRICT tables */ |
| 133514 | 133835 | bStrict = (pTab->tabFlags & TF_Strict)!=0; |
| 133515 | 133836 | for(j=0; j<pTab->nCol; j++){ |
| | @@ -133518,11 +133839,11 @@ |
| 133518 | 133839 | int doError, jmp2; |
| 133519 | 133840 | if( j==pTab->iPKey ) continue; |
| 133520 | 133841 | if( pCol->notNull==0 && !bStrict ) continue; |
| 133521 | 133842 | doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0; |
| 133522 | 133843 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
| 133523 | | - if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){ |
| 133844 | + if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){ |
| 133524 | 133845 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 133525 | 133846 | } |
| 133526 | 133847 | if( pCol->notNull ){ |
| 133527 | 133848 | jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
| 133528 | 133849 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| | @@ -133533,13 +133854,11 @@ |
| 133533 | 133854 | }else{ |
| 133534 | 133855 | integrityCheckResultRow(v); |
| 133535 | 133856 | } |
| 133536 | 133857 | sqlite3VdbeJumpHere(v, jmp2); |
| 133537 | 133858 | } |
| 133538 | | - if( (pTab->tabFlags & TF_Strict)!=0 |
| 133539 | | - && pCol->eCType!=COLTYPE_ANY |
| 133540 | | - ){ |
| 133859 | + if( bStrict && pCol->eCType!=COLTYPE_ANY ){ |
| 133541 | 133860 | jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0, |
| 133542 | 133861 | sqlite3StdTypeMap[pCol->eCType-1]); |
| 133543 | 133862 | VdbeCoverage(v); |
| 133544 | 133863 | zErr = sqlite3MPrintf(db, "non-%s value in %s.%s", |
| 133545 | 133864 | sqlite3StdType[pCol->eCType-1], |
| | @@ -133634,10 +133953,13 @@ |
| 133634 | 133953 | sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 133635 | 133954 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); |
| 133636 | 133955 | integrityCheckResultRow(v); |
| 133637 | 133956 | sqlite3VdbeJumpHere(v, addr); |
| 133638 | 133957 | } |
| 133958 | + if( pPk ){ |
| 133959 | + sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol); |
| 133960 | + } |
| 133639 | 133961 | } |
| 133640 | 133962 | } |
| 133641 | 133963 | } |
| 133642 | 133964 | { |
| 133643 | 133965 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| | @@ -135032,19 +135354,19 @@ |
| 135032 | 135354 | sqlite3 *db = pParse->db; |
| 135033 | 135355 | assert( db!=0 ); |
| 135034 | 135356 | assert( db->pParse==pParse ); |
| 135035 | 135357 | assert( pParse->nested==0 ); |
| 135036 | 135358 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 135037 | | - sqlite3DbFree(db, pParse->aTableLock); |
| 135359 | + if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock); |
| 135038 | 135360 | #endif |
| 135039 | 135361 | while( pParse->pCleanup ){ |
| 135040 | 135362 | ParseCleanup *pCleanup = pParse->pCleanup; |
| 135041 | 135363 | pParse->pCleanup = pCleanup->pNext; |
| 135042 | 135364 | pCleanup->xCleanup(db, pCleanup->pPtr); |
| 135043 | | - sqlite3DbFreeNN(db, pCleanup); |
| 135365 | + sqlite3DbNNFreeNN(db, pCleanup); |
| 135044 | 135366 | } |
| 135045 | | - sqlite3DbFree(db, pParse->aLabel); |
| 135367 | + if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel); |
| 135046 | 135368 | if( pParse->pConstExpr ){ |
| 135047 | 135369 | sqlite3ExprListDelete(db, pParse->pConstExpr); |
| 135048 | 135370 | } |
| 135049 | 135371 | assert( db->lookaside.bDisable >= pParse->disableLookaside ); |
| 135050 | 135372 | db->lookaside.bDisable -= pParse->disableLookaside; |
| | @@ -135599,10 +135921,11 @@ |
| 135599 | 135921 | ** |
| 135600 | 135922 | ** If bFree==1, call sqlite3DbFree() on the p object. |
| 135601 | 135923 | ** If bFree==0, Leave the first Select object unfreed |
| 135602 | 135924 | */ |
| 135603 | 135925 | static void clearSelect(sqlite3 *db, Select *p, int bFree){ |
| 135926 | + assert( db!=0 ); |
| 135604 | 135927 | while( p ){ |
| 135605 | 135928 | Select *pPrior = p->pPrior; |
| 135606 | 135929 | sqlite3ExprListDelete(db, p->pEList); |
| 135607 | 135930 | sqlite3SrcListDelete(db, p->pSrc); |
| 135608 | 135931 | sqlite3ExprDelete(db, p->pWhere); |
| | @@ -135618,11 +135941,11 @@ |
| 135618 | 135941 | while( p->pWin ){ |
| 135619 | 135942 | assert( p->pWin->ppThis==&p->pWin ); |
| 135620 | 135943 | sqlite3WindowUnlinkFromSelect(p->pWin); |
| 135621 | 135944 | } |
| 135622 | 135945 | #endif |
| 135623 | | - if( bFree ) sqlite3DbFreeNN(db, p); |
| 135946 | + if( bFree ) sqlite3DbNNFreeNN(db, p); |
| 135624 | 135947 | p = pPrior; |
| 135625 | 135948 | bFree = 1; |
| 135626 | 135949 | } |
| 135627 | 135950 | } |
| 135628 | 135951 | |
| | @@ -137024,13 +137347,14 @@ |
| 137024 | 137347 | /* |
| 137025 | 137348 | ** Deallocate a KeyInfo object |
| 137026 | 137349 | */ |
| 137027 | 137350 | SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){ |
| 137028 | 137351 | if( p ){ |
| 137352 | + assert( p->db!=0 ); |
| 137029 | 137353 | assert( p->nRef>0 ); |
| 137030 | 137354 | p->nRef--; |
| 137031 | | - if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p); |
| 137355 | + if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p); |
| 137032 | 137356 | } |
| 137033 | 137357 | } |
| 137034 | 137358 | |
| 137035 | 137359 | /* |
| 137036 | 137360 | ** Make a new pointer to a KeyInfo object |
| | @@ -137211,18 +137535,21 @@ |
| 137211 | 137535 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, |
| 137212 | 137536 | nKey+1+nColumn+nRefKey); |
| 137213 | 137537 | if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
| 137214 | 137538 | addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
| 137215 | 137539 | VdbeCoverage(v); |
| 137216 | | - codeOffset(v, p->iOffset, addrContinue); |
| 137540 | + assert( p->iLimit==0 && p->iOffset==0 ); |
| 137217 | 137541 | sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); |
| 137218 | 137542 | bSeq = 0; |
| 137219 | 137543 | }else{ |
| 137220 | 137544 | addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
| 137221 | 137545 | codeOffset(v, p->iOffset, addrContinue); |
| 137222 | 137546 | iSortTab = iTab; |
| 137223 | 137547 | bSeq = 1; |
| 137548 | + if( p->iOffset>0 ){ |
| 137549 | + sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1); |
| 137550 | + } |
| 137224 | 137551 | } |
| 137225 | 137552 | for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){ |
| 137226 | 137553 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 137227 | 137554 | if( aOutEx[i].fg.bSorterRef ) continue; |
| 137228 | 137555 | #endif |
| | @@ -137343,13 +137670,10 @@ |
| 137343 | 137670 | |
| 137344 | 137671 | /* |
| 137345 | 137672 | ** Return a pointer to a string containing the 'declaration type' of the |
| 137346 | 137673 | ** expression pExpr. The string may be treated as static by the caller. |
| 137347 | 137674 | ** |
| 137348 | | -** Also try to estimate the size of the returned value and return that |
| 137349 | | -** result in *pEstWidth. |
| 137350 | | -** |
| 137351 | 137675 | ** The declaration type is the exact datatype definition extracted from the |
| 137352 | 137676 | ** original CREATE TABLE statement if the expression is a column. The |
| 137353 | 137677 | ** declaration type for a ROWID field is INTEGER. Exactly when an expression |
| 137354 | 137678 | ** is considered a column can be complex in the presence of subqueries. The |
| 137355 | 137679 | ** result-set expression in all of the following SELECT statements is |
| | @@ -139211,14 +139535,15 @@ |
| 139211 | 139535 | |
| 139212 | 139536 | /* Jump to the this point in order to terminate the query. |
| 139213 | 139537 | */ |
| 139214 | 139538 | sqlite3VdbeResolveLabel(v, labelEnd); |
| 139215 | 139539 | |
| 139216 | | - /* Reassembly the compound query so that it will be freed correctly |
| 139540 | + /* Reassemble the compound query so that it will be freed correctly |
| 139217 | 139541 | ** by the calling function */ |
| 139218 | 139542 | if( pSplit->pPrior ){ |
| 139219 | | - sqlite3SelectDelete(db, pSplit->pPrior); |
| 139543 | + sqlite3ParserAddCleanup(pParse, |
| 139544 | + (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior); |
| 139220 | 139545 | } |
| 139221 | 139546 | pSplit->pPrior = pPrior; |
| 139222 | 139547 | pPrior->pNext = pSplit; |
| 139223 | 139548 | sqlite3ExprListDelete(db, pPrior->pOrderBy); |
| 139224 | 139549 | pPrior->pOrderBy = 0; |
| | @@ -139324,10 +139649,11 @@ |
| 139324 | 139649 | if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){ |
| 139325 | 139650 | memset(&ifNullRow, 0, sizeof(ifNullRow)); |
| 139326 | 139651 | ifNullRow.op = TK_IF_NULL_ROW; |
| 139327 | 139652 | ifNullRow.pLeft = pCopy; |
| 139328 | 139653 | ifNullRow.iTable = pSubst->iNewTable; |
| 139654 | + ifNullRow.iColumn = -99; |
| 139329 | 139655 | ifNullRow.flags = EP_IfNullRow; |
| 139330 | 139656 | pCopy = &ifNullRow; |
| 139331 | 139657 | } |
| 139332 | 139658 | testcase( ExprHasProperty(pCopy, EP_Subquery) ); |
| 139333 | 139659 | pNew = sqlite3ExprDup(db, pCopy, 0); |
| | @@ -139591,11 +139917,12 @@ |
| 139591 | 139917 | ** |
| 139592 | 139918 | ** (3) If the subquery is the right operand of a LEFT JOIN then |
| 139593 | 139919 | ** (3a) the subquery may not be a join and |
| 139594 | 139920 | ** (3b) the FROM clause of the subquery may not contain a virtual |
| 139595 | 139921 | ** table and |
| 139596 | | -** (3c) the outer query may not be an aggregate. |
| 139922 | +** (**) Was: "The outer query may not have a GROUP BY." This case |
| 139923 | +** is now managed correctly |
| 139597 | 139924 | ** (3d) the outer query may not be DISTINCT. |
| 139598 | 139925 | ** See also (26) for restrictions on RIGHT JOIN. |
| 139599 | 139926 | ** |
| 139600 | 139927 | ** (4) The subquery can not be DISTINCT. |
| 139601 | 139928 | ** |
| | @@ -139803,20 +140130,14 @@ |
| 139803 | 140130 | ** |
| 139804 | 140131 | ** (t1 LEFT OUTER JOIN t2) JOIN t3 |
| 139805 | 140132 | ** |
| 139806 | 140133 | ** which is not at all the same thing. |
| 139807 | 140134 | ** |
| 139808 | | - ** If the subquery is the right operand of a LEFT JOIN, then the outer |
| 139809 | | - ** query cannot be an aggregate. (3c) This is an artifact of the way |
| 139810 | | - ** aggregates are processed - there is no mechanism to determine if |
| 139811 | | - ** the LEFT JOIN table should be all-NULL. |
| 139812 | | - ** |
| 139813 | 140135 | ** See also tickets #306, #350, and #3300. |
| 139814 | 140136 | */ |
| 139815 | 140137 | if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){ |
| 139816 | 140138 | if( pSubSrc->nSrc>1 /* (3a) */ |
| 139817 | | - || isAgg /* (3c) */ |
| 139818 | 140139 | || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */ |
| 139819 | 140140 | || (p->selFlags & SF_Distinct)!=0 /* (3d) */ |
| 139820 | 140141 | || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */ |
| 139821 | 140142 | ){ |
| 139822 | 140143 | return 0; |
| | @@ -140733,10 +141054,11 @@ |
| 140733 | 141054 | if( p->pWhere |
| 140734 | 141055 | || p->pEList->nExpr!=1 |
| 140735 | 141056 | || p->pSrc->nSrc!=1 |
| 140736 | 141057 | || p->pSrc->a[0].pSelect |
| 140737 | 141058 | || pAggInfo->nFunc!=1 |
| 141059 | + || p->pHaving |
| 140738 | 141060 | ){ |
| 140739 | 141061 | return 0; |
| 140740 | 141062 | } |
| 140741 | 141063 | pTab = p->pSrc->a[0].pTab; |
| 140742 | 141064 | assert( pTab!=0 ); |
| | @@ -142726,11 +143048,11 @@ |
| 142726 | 143048 | */ |
| 142727 | 143049 | iEnd = sqlite3VdbeMakeLabel(pParse); |
| 142728 | 143050 | if( (p->selFlags & SF_FixedLimit)==0 ){ |
| 142729 | 143051 | p->nSelectRow = 320; /* 4 billion rows */ |
| 142730 | 143052 | } |
| 142731 | | - computeLimitRegisters(pParse, p, iEnd); |
| 143053 | + if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd); |
| 142732 | 143054 | if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ |
| 142733 | 143055 | sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); |
| 142734 | 143056 | sSort.sortFlags |= SORTFLAG_UseSorter; |
| 142735 | 143057 | } |
| 142736 | 143058 | |
| | @@ -142948,12 +143270,17 @@ |
| 142948 | 143270 | if( minMaxFlag ){ |
| 142949 | 143271 | sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag); |
| 142950 | 143272 | sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY"); |
| 142951 | 143273 | } |
| 142952 | 143274 | for(ii=0; ii<pAggInfo->nColumn; ii++){ |
| 142953 | | - sqlite3DebugPrintf("agg-column[%d] iMem=%d\n", |
| 142954 | | - ii, pAggInfo->aCol[ii].iMem); |
| 143275 | + struct AggInfo_col *pCol = &pAggInfo->aCol[ii]; |
| 143276 | + sqlite3DebugPrintf( |
| 143277 | + "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d" |
| 143278 | + " iSorterColumn=%d\n", |
| 143279 | + ii, pCol->pTab ? pCol->pTab->zName : "NULL", |
| 143280 | + pCol->iTable, pCol->iColumn, pCol->iMem, |
| 143281 | + pCol->iSorterColumn); |
| 142955 | 143282 | sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0); |
| 142956 | 143283 | } |
| 142957 | 143284 | for(ii=0; ii<pAggInfo->nFunc; ii++){ |
| 142958 | 143285 | sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n", |
| 142959 | 143286 | ii, pAggInfo->aFunc[ii].iMem); |
| | @@ -143070,19 +143397,19 @@ |
| 143070 | 143397 | } |
| 143071 | 143398 | } |
| 143072 | 143399 | regBase = sqlite3GetTempRange(pParse, nCol); |
| 143073 | 143400 | sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0); |
| 143074 | 143401 | j = nGroupBy; |
| 143402 | + pAggInfo->directMode = 1; |
| 143075 | 143403 | for(i=0; i<pAggInfo->nColumn; i++){ |
| 143076 | 143404 | struct AggInfo_col *pCol = &pAggInfo->aCol[i]; |
| 143077 | 143405 | if( pCol->iSorterColumn>=j ){ |
| 143078 | | - int r1 = j + regBase; |
| 143079 | | - sqlite3ExprCodeGetColumnOfTable(v, |
| 143080 | | - pCol->pTab, pCol->iTable, pCol->iColumn, r1); |
| 143406 | + sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase); |
| 143081 | 143407 | j++; |
| 143082 | 143408 | } |
| 143083 | 143409 | } |
| 143410 | + pAggInfo->directMode = 0; |
| 143084 | 143411 | regRecord = sqlite3GetTempReg(pParse); |
| 143085 | 143412 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); |
| 143086 | 143413 | sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord); |
| 143087 | 143414 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 143088 | 143415 | sqlite3ReleaseTempRange(pParse, regBase, nCol); |
| | @@ -147496,11 +147823,12 @@ |
| 147496 | 147823 | ** in the list are moved to the sqlite3.pDisconnect list of the associated |
| 147497 | 147824 | ** database connection. |
| 147498 | 147825 | */ |
| 147499 | 147826 | SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){ |
| 147500 | 147827 | assert( IsVirtual(p) ); |
| 147501 | | - if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); |
| 147828 | + assert( db!=0 ); |
| 147829 | + if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); |
| 147502 | 147830 | if( p->u.vtab.azArg ){ |
| 147503 | 147831 | int i; |
| 147504 | 147832 | for(i=0; i<p->u.vtab.nArg; i++){ |
| 147505 | 147833 | if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]); |
| 147506 | 147834 | } |
| | @@ -149173,10 +149501,11 @@ |
| 149173 | 149501 | #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ |
| 149174 | 149502 | #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ |
| 149175 | 149503 | #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ |
| 149176 | 149504 | #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ |
| 149177 | 149505 | #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ |
| 149506 | +#define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */ |
| 149178 | 149507 | |
| 149179 | 149508 | #endif /* !defined(SQLITE_WHEREINT_H) */ |
| 149180 | 149509 | |
| 149181 | 149510 | /************** End of whereInt.h ********************************************/ |
| 149182 | 149511 | /************** Continuing where we left off in wherecode.c ******************/ |
| | @@ -149781,11 +150110,12 @@ |
| 149781 | 150110 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap,&iTab); |
| 149782 | 150111 | pExpr->iTable = iTab; |
| 149783 | 150112 | } |
| 149784 | 150113 | sqlite3ExprDelete(db, pX); |
| 149785 | 150114 | }else{ |
| 149786 | | - aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq); |
| 150115 | + int n = sqlite3ExprVectorSize(pX->pLeft); |
| 150116 | + aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n)); |
| 149787 | 150117 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab); |
| 149788 | 150118 | } |
| 149789 | 150119 | pX = pExpr; |
| 149790 | 150120 | } |
| 149791 | 150121 | |
| | @@ -150051,11 +150381,11 @@ |
| 150051 | 150381 | WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 150052 | 150382 | ){ |
| 150053 | 150383 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 150054 | 150384 | VdbeOp *pOp; |
| 150055 | 150385 | assert( pLevel->iLikeRepCntr>0 ); |
| 150056 | | - pOp = sqlite3VdbeGetOp(v, -1); |
| 150386 | + pOp = sqlite3VdbeGetLastOp(v); |
| 150057 | 150387 | assert( pOp!=0 ); |
| 150058 | 150388 | assert( pOp->opcode==OP_String8 |
| 150059 | 150389 | || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
| 150060 | 150390 | pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */ |
| 150061 | 150391 | pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */ |
| | @@ -151267,12 +151597,12 @@ |
| 151267 | 151597 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 151268 | 151598 | endEq = 0; |
| 151269 | 151599 | } |
| 151270 | 151600 | nConstraint++; |
| 151271 | 151601 | } |
| 151272 | | - sqlite3DbFree(db, zStartAff); |
| 151273 | | - sqlite3DbFree(db, zEndAff); |
| 151602 | + if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff); |
| 151603 | + if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff); |
| 151274 | 151604 | |
| 151275 | 151605 | /* Top of the loop body */ |
| 151276 | 151606 | if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 151277 | 151607 | |
| 151278 | 151608 | /* Check if the index cursor is past the end of the range. */ |
| | @@ -155499,11 +155829,11 @@ |
| 155499 | 155829 | /* At this point, the (iCol+1) field prefix of aSample[i] is the first |
| 155500 | 155830 | ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec |
| 155501 | 155831 | ** is larger than all samples in the array. */ |
| 155502 | 155832 | tRowcnt iUpper, iGap; |
| 155503 | 155833 | if( i>=pIdx->nSample ){ |
| 155504 | | - iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 155834 | + iUpper = pIdx->nRowEst0; |
| 155505 | 155835 | }else{ |
| 155506 | 155836 | iUpper = aSample[i].anLt[iCol]; |
| 155507 | 155837 | } |
| 155508 | 155838 | |
| 155509 | 155839 | if( iLower>=iUpper ){ |
| | @@ -156128,16 +156458,22 @@ |
| 156128 | 156458 | } |
| 156129 | 156459 | } |
| 156130 | 156460 | } |
| 156131 | 156461 | |
| 156132 | 156462 | /* |
| 156133 | | -** Deallocate internal memory used by a WhereLoop object |
| 156463 | +** Deallocate internal memory used by a WhereLoop object. Leave the |
| 156464 | +** object in an initialized state, as if it had been newly allocated. |
| 156134 | 156465 | */ |
| 156135 | 156466 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 156136 | | - if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm); |
| 156467 | + if( p->aLTerm!=p->aLTermSpace ){ |
| 156468 | + sqlite3DbFreeNN(db, p->aLTerm); |
| 156469 | + p->aLTerm = p->aLTermSpace; |
| 156470 | + p->nLSlot = ArraySize(p->aLTermSpace); |
| 156471 | + } |
| 156137 | 156472 | whereLoopClearUnion(db, p); |
| 156138 | | - whereLoopInit(p); |
| 156473 | + p->nLTerm = 0; |
| 156474 | + p->wsFlags = 0; |
| 156139 | 156475 | } |
| 156140 | 156476 | |
| 156141 | 156477 | /* |
| 156142 | 156478 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 156143 | 156479 | */ |
| | @@ -156157,11 +156493,13 @@ |
| 156157 | 156493 | /* |
| 156158 | 156494 | ** Transfer content from the second pLoop into the first. |
| 156159 | 156495 | */ |
| 156160 | 156496 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 156161 | 156497 | whereLoopClearUnion(db, pTo); |
| 156162 | | - if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 156498 | + if( pFrom->nLTerm > pTo->nLSlot |
| 156499 | + && whereLoopResize(db, pTo, pFrom->nLTerm) |
| 156500 | + ){ |
| 156163 | 156501 | memset(pTo, 0, WHERE_LOOP_XFER_SZ); |
| 156164 | 156502 | return SQLITE_NOMEM_BKPT; |
| 156165 | 156503 | } |
| 156166 | 156504 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 156167 | 156505 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
| | @@ -156175,32 +156513,34 @@ |
| 156175 | 156513 | |
| 156176 | 156514 | /* |
| 156177 | 156515 | ** Delete a WhereLoop object |
| 156178 | 156516 | */ |
| 156179 | 156517 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
| 156518 | + assert( db!=0 ); |
| 156180 | 156519 | whereLoopClear(db, p); |
| 156181 | | - sqlite3DbFreeNN(db, p); |
| 156520 | + sqlite3DbNNFreeNN(db, p); |
| 156182 | 156521 | } |
| 156183 | 156522 | |
| 156184 | 156523 | /* |
| 156185 | 156524 | ** Free a WhereInfo structure |
| 156186 | 156525 | */ |
| 156187 | 156526 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 156188 | 156527 | assert( pWInfo!=0 ); |
| 156528 | + assert( db!=0 ); |
| 156189 | 156529 | sqlite3WhereClauseClear(&pWInfo->sWC); |
| 156190 | 156530 | while( pWInfo->pLoops ){ |
| 156191 | 156531 | WhereLoop *p = pWInfo->pLoops; |
| 156192 | 156532 | pWInfo->pLoops = p->pNextLoop; |
| 156193 | 156533 | whereLoopDelete(db, p); |
| 156194 | 156534 | } |
| 156195 | 156535 | assert( pWInfo->pExprMods==0 ); |
| 156196 | 156536 | while( pWInfo->pMemToFree ){ |
| 156197 | 156537 | WhereMemBlock *pNext = pWInfo->pMemToFree->pNext; |
| 156198 | | - sqlite3DbFreeNN(db, pWInfo->pMemToFree); |
| 156538 | + sqlite3DbNNFreeNN(db, pWInfo->pMemToFree); |
| 156199 | 156539 | pWInfo->pMemToFree = pNext; |
| 156200 | 156540 | } |
| 156201 | | - sqlite3DbFreeNN(db, pWInfo); |
| 156541 | + sqlite3DbNNFreeNN(db, pWInfo); |
| 156202 | 156542 | } |
| 156203 | 156543 | |
| 156204 | 156544 | /* Undo all Expr node modifications |
| 156205 | 156545 | */ |
| 156206 | 156546 | static void whereUndoExprMods(WhereInfo *pWInfo){ |
| | @@ -156810,11 +157150,15 @@ |
| 156810 | 157150 | pNew->wsFlags = saved_wsFlags; |
| 156811 | 157151 | pNew->u.btree.nEq = saved_nEq; |
| 156812 | 157152 | pNew->u.btree.nBtm = saved_nBtm; |
| 156813 | 157153 | pNew->u.btree.nTop = saved_nTop; |
| 156814 | 157154 | pNew->nLTerm = saved_nLTerm; |
| 156815 | | - if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 157155 | + if( pNew->nLTerm>=pNew->nLSlot |
| 157156 | + && whereLoopResize(db, pNew, pNew->nLTerm+1) |
| 157157 | + ){ |
| 157158 | + break; /* OOM while trying to enlarge the pNew->aLTerm array */ |
| 157159 | + } |
| 156816 | 157160 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 156817 | 157161 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
| 156818 | 157162 | |
| 156819 | 157163 | assert( nInMul==0 |
| 156820 | 157164 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| | @@ -156903,42 +157247,43 @@ |
| 156903 | 157247 | } |
| 156904 | 157248 | } |
| 156905 | 157249 | if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS; |
| 156906 | 157250 | }else if( eOp & WO_ISNULL ){ |
| 156907 | 157251 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 156908 | | - }else if( eOp & (WO_GT|WO_GE) ){ |
| 156909 | | - testcase( eOp & WO_GT ); |
| 156910 | | - testcase( eOp & WO_GE ); |
| 156911 | | - pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 156912 | | - pNew->u.btree.nBtm = whereRangeVectorLen( |
| 156913 | | - pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 156914 | | - ); |
| 156915 | | - pBtm = pTerm; |
| 156916 | | - pTop = 0; |
| 156917 | | - if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 156918 | | - /* Range constraints that come from the LIKE optimization are |
| 156919 | | - ** always used in pairs. */ |
| 156920 | | - pTop = &pTerm[1]; |
| 156921 | | - assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 156922 | | - assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 156923 | | - assert( pTop->eOperator==WO_LT ); |
| 156924 | | - if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 156925 | | - pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 156926 | | - pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 156927 | | - pNew->u.btree.nTop = 1; |
| 156928 | | - } |
| 156929 | | - }else{ |
| 156930 | | - assert( eOp & (WO_LT|WO_LE) ); |
| 156931 | | - testcase( eOp & WO_LT ); |
| 156932 | | - testcase( eOp & WO_LE ); |
| 156933 | | - pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 156934 | | - pNew->u.btree.nTop = whereRangeVectorLen( |
| 156935 | | - pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 156936 | | - ); |
| 156937 | | - pTop = pTerm; |
| 156938 | | - pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 156939 | | - pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 157252 | + }else{ |
| 157253 | + int nVecLen = whereRangeVectorLen( |
| 157254 | + pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 157255 | + ); |
| 157256 | + if( eOp & (WO_GT|WO_GE) ){ |
| 157257 | + testcase( eOp & WO_GT ); |
| 157258 | + testcase( eOp & WO_GE ); |
| 157259 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 157260 | + pNew->u.btree.nBtm = nVecLen; |
| 157261 | + pBtm = pTerm; |
| 157262 | + pTop = 0; |
| 157263 | + if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 157264 | + /* Range constraints that come from the LIKE optimization are |
| 157265 | + ** always used in pairs. */ |
| 157266 | + pTop = &pTerm[1]; |
| 157267 | + assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 157268 | + assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 157269 | + assert( pTop->eOperator==WO_LT ); |
| 157270 | + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 157271 | + pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 157272 | + pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 157273 | + pNew->u.btree.nTop = 1; |
| 157274 | + } |
| 157275 | + }else{ |
| 157276 | + assert( eOp & (WO_LT|WO_LE) ); |
| 157277 | + testcase( eOp & WO_LT ); |
| 157278 | + testcase( eOp & WO_LE ); |
| 157279 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 157280 | + pNew->u.btree.nTop = nVecLen; |
| 157281 | + pTop = pTerm; |
| 157282 | + pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 157283 | + pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 157284 | + } |
| 156940 | 157285 | } |
| 156941 | 157286 | |
| 156942 | 157287 | /* At this point pNew->nOut is set to the number of rows expected to |
| 156943 | 157288 | ** be visited by the index scan before considering term pTerm, or the |
| 156944 | 157289 | ** values of nIn and nInMul. In other words, assuming that all |
| | @@ -157380,10 +157725,13 @@ |
| 157380 | 157725 | #ifdef SQLITE_ENABLE_STAT4 |
| 157381 | 157726 | pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0); |
| 157382 | 157727 | #else |
| 157383 | 157728 | pNew->rRun = rSize + 16; |
| 157384 | 157729 | #endif |
| 157730 | + if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){ |
| 157731 | + pNew->wsFlags |= WHERE_VIEWSCAN; |
| 157732 | + } |
| 157385 | 157733 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
| 157386 | 157734 | whereLoopOutputAdjust(pWC, pNew, rSize); |
| 157387 | 157735 | rc = whereLoopInsert(pBuilder, pNew); |
| 157388 | 157736 | pNew->nOut = rSize; |
| 157389 | 157737 | if( rc ) break; |
| | @@ -158106,11 +158454,17 @@ |
| 158106 | 158454 | WhereLoop *pNew; |
| 158107 | 158455 | |
| 158108 | 158456 | |
| 158109 | 158457 | /* Loop over the tables in the join, from left to right */ |
| 158110 | 158458 | pNew = pBuilder->pNew; |
| 158111 | | - whereLoopInit(pNew); |
| 158459 | + |
| 158460 | + /* Verify that pNew has already been initialized */ |
| 158461 | + assert( pNew->nLTerm==0 ); |
| 158462 | + assert( pNew->wsFlags==0 ); |
| 158463 | + assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) ); |
| 158464 | + assert( pNew->aLTerm!=0 ); |
| 158465 | + |
| 158112 | 158466 | pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT; |
| 158113 | 158467 | for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ |
| 158114 | 158468 | Bitmask mUnusable = 0; |
| 158115 | 158469 | pNew->iTab = iTab; |
| 158116 | 158470 | pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR; |
| | @@ -158703,13 +159057,13 @@ |
| 158703 | 159057 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 158704 | 159058 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 158705 | 159059 | LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 158706 | 159060 | LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 158707 | 159061 | LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 158708 | | - i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 159062 | + i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 158709 | 159063 | Bitmask maskNew; /* Mask of src visited by (..) */ |
| 158710 | | - Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 159064 | + Bitmask revMask; /* Mask of rev-order loops for (..) */ |
| 158711 | 159065 | |
| 158712 | 159066 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 158713 | 159067 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 158714 | 159068 | if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){ |
| 158715 | 159069 | /* Do not use an automatic index if the this loop is expected |
| | @@ -158724,11 +159078,13 @@ |
| 158724 | 159078 | ** Compute its cost */ |
| 158725 | 159079 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 158726 | 159080 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
| 158727 | 159081 | nOut = pFrom->nRow + pWLoop->nOut; |
| 158728 | 159082 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
| 159083 | + isOrdered = pFrom->isOrdered; |
| 158729 | 159084 | if( isOrdered<0 ){ |
| 159085 | + revMask = 0; |
| 158730 | 159086 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
| 158731 | 159087 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 158732 | 159088 | iLoop, pWLoop, &revMask); |
| 158733 | 159089 | }else{ |
| 158734 | 159090 | revMask = pFrom->revLoop; |
| | @@ -158751,10 +159107,17 @@ |
| 158751 | 159107 | rUnsorted, rCost)); |
| 158752 | 159108 | }else{ |
| 158753 | 159109 | rCost = rUnsorted; |
| 158754 | 159110 | rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */ |
| 158755 | 159111 | } |
| 159112 | + |
| 159113 | + /* TUNING: A full-scan of a VIEW or subquery in the outer loop |
| 159114 | + ** is not so bad. */ |
| 159115 | + if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){ |
| 159116 | + rCost += -10; |
| 159117 | + nOut += -30; |
| 159118 | + } |
| 158756 | 159119 | |
| 158757 | 159120 | /* Check to see if pWLoop should be added to the set of |
| 158758 | 159121 | ** mxChoice best-so-far paths. |
| 158759 | 159122 | ** |
| 158760 | 159123 | ** First look for an existing path among best-so-far paths |
| | @@ -158984,11 +159347,12 @@ |
| 158984 | 159347 | |
| 158985 | 159348 | |
| 158986 | 159349 | pWInfo->nRowOut = pFrom->nRow; |
| 158987 | 159350 | |
| 158988 | 159351 | /* Free temporary memory and return success */ |
| 158989 | | - sqlite3DbFreeNN(db, pSpace); |
| 159352 | + assert( db!=0 ); |
| 159353 | + sqlite3DbNNFreeNN(db, pSpace); |
| 158990 | 159354 | return SQLITE_OK; |
| 158991 | 159355 | } |
| 158992 | 159356 | |
| 158993 | 159357 | /* |
| 158994 | 159358 | ** Most queries use only a single table (they are not joins) and have |
| | @@ -161217,11 +161581,10 @@ |
| 161217 | 161581 | int i; |
| 161218 | 161582 | int nInit = pList ? pList->nExpr : 0; |
| 161219 | 161583 | for(i=0; i<pAppend->nExpr; i++){ |
| 161220 | 161584 | sqlite3 *db = pParse->db; |
| 161221 | 161585 | Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0); |
| 161222 | | - assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) ); |
| 161223 | 161586 | if( db->mallocFailed ){ |
| 161224 | 161587 | sqlite3ExprDelete(db, pDup); |
| 161225 | 161588 | break; |
| 161226 | 161589 | } |
| 161227 | 161590 | if( bIntToNull ){ |
| | @@ -162488,14 +162851,13 @@ |
| 162488 | 162851 | } |
| 162489 | 162852 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone); |
| 162490 | 162853 | |
| 162491 | 162854 | /* This block runs if reg1 is not NULL, but reg2 is. */ |
| 162492 | 162855 | sqlite3VdbeJumpHere(v, addr); |
| 162493 | | - sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); VdbeCoverage(v); |
| 162494 | | - if( op==OP_Gt || op==OP_Ge ){ |
| 162495 | | - sqlite3VdbeChangeP2(v, -1, addrDone); |
| 162496 | | - } |
| 162856 | + sqlite3VdbeAddOp2(v, OP_IsNull, reg2, |
| 162857 | + (op==OP_Gt || op==OP_Ge) ? addrDone : lbl); |
| 162858 | + VdbeCoverage(v); |
| 162497 | 162859 | } |
| 162498 | 162860 | |
| 162499 | 162861 | /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1). |
| 162500 | 162862 | ** This block adds (or subtracts for DESC) the numeric value in regVal |
| 162501 | 162863 | ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob), |
| | @@ -170068,11 +170430,11 @@ |
| 170068 | 170430 | sqlite3DeleteTable(db, pParse->pNewTable); |
| 170069 | 170431 | } |
| 170070 | 170432 | if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){ |
| 170071 | 170433 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 170072 | 170434 | } |
| 170073 | | - if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList); |
| 170435 | + if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList); |
| 170074 | 170436 | db->pParse = pParentParse; |
| 170075 | 170437 | assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| 170076 | 170438 | return nErr; |
| 170077 | 170439 | } |
| 170078 | 170440 | |
| | @@ -171424,22 +171786,23 @@ |
| 171424 | 171786 | db->lookaside.pEnd = p; |
| 171425 | 171787 | db->lookaside.bDisable = 0; |
| 171426 | 171788 | db->lookaside.bMalloced = pBuf==0 ?1:0; |
| 171427 | 171789 | db->lookaside.nSlot = nBig+nSm; |
| 171428 | 171790 | }else{ |
| 171429 | | - db->lookaside.pStart = db; |
| 171791 | + db->lookaside.pStart = 0; |
| 171430 | 171792 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 171431 | 171793 | db->lookaside.pSmallInit = 0; |
| 171432 | 171794 | db->lookaside.pSmallFree = 0; |
| 171433 | | - db->lookaside.pMiddle = db; |
| 171795 | + db->lookaside.pMiddle = 0; |
| 171434 | 171796 | #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 171435 | | - db->lookaside.pEnd = db; |
| 171797 | + db->lookaside.pEnd = 0; |
| 171436 | 171798 | db->lookaside.bDisable = 1; |
| 171437 | 171799 | db->lookaside.sz = 0; |
| 171438 | 171800 | db->lookaside.bMalloced = 0; |
| 171439 | 171801 | db->lookaside.nSlot = 0; |
| 171440 | 171802 | } |
| 171803 | + db->lookaside.pTrueEnd = db->lookaside.pEnd; |
| 171441 | 171804 | assert( sqlite3LookasideUsed(db,0)==0 ); |
| 171442 | 171805 | #endif /* SQLITE_OMIT_LOOKASIDE */ |
| 171443 | 171806 | return SQLITE_OK; |
| 171444 | 171807 | } |
| 171445 | 171808 | |
| | @@ -171514,10 +171877,11 @@ |
| 171514 | 171877 | ** Configuration settings for an individual database connection |
| 171515 | 171878 | */ |
| 171516 | 171879 | SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){ |
| 171517 | 171880 | va_list ap; |
| 171518 | 171881 | int rc; |
| 171882 | + sqlite3_mutex_enter(db->mutex); |
| 171519 | 171883 | va_start(ap, op); |
| 171520 | 171884 | switch( op ){ |
| 171521 | 171885 | case SQLITE_DBCONFIG_MAINDBNAME: { |
| 171522 | 171886 | /* IMP: R-06824-28531 */ |
| 171523 | 171887 | /* IMP: R-36257-52125 */ |
| | @@ -171579,10 +171943,11 @@ |
| 171579 | 171943 | } |
| 171580 | 171944 | break; |
| 171581 | 171945 | } |
| 171582 | 171946 | } |
| 171583 | 171947 | va_end(ap); |
| 171948 | + sqlite3_mutex_leave(db->mutex); |
| 171584 | 171949 | return rc; |
| 171585 | 171950 | } |
| 171586 | 171951 | |
| 171587 | 171952 | /* |
| 171588 | 171953 | ** This is the default collating function named "BINARY" which is always |
| | @@ -181118,11 +181483,11 @@ |
| 181118 | 181483 | p1 = pPhrase->doclist.pList; |
| 181119 | 181484 | p2 = aPoslist; |
| 181120 | 181485 | nDistance = iPrev - nMaxUndeferred; |
| 181121 | 181486 | } |
| 181122 | 181487 | |
| 181123 | | - aOut = (char *)sqlite3_malloc(nPoslist+8); |
| 181488 | + aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING); |
| 181124 | 181489 | if( !aOut ){ |
| 181125 | 181490 | sqlite3_free(aPoslist); |
| 181126 | 181491 | return SQLITE_NOMEM; |
| 181127 | 181492 | } |
| 181128 | 181493 | |
| | @@ -204144,11 +204509,11 @@ |
| 204144 | 204509 | sqlite3_bind_value(pUp, 2, aData[2]); |
| 204145 | 204510 | } |
| 204146 | 204511 | sqlite3_free(p); |
| 204147 | 204512 | nChange = 1; |
| 204148 | 204513 | } |
| 204149 | | - for(jj=1; jj<pRtree->nAux; jj++){ |
| 204514 | + for(jj=1; jj<nData-2; jj++){ |
| 204150 | 204515 | nChange++; |
| 204151 | 204516 | sqlite3_bind_value(pUp, jj+2, aData[jj+2]); |
| 204152 | 204517 | } |
| 204153 | 204518 | if( nChange ){ |
| 204154 | 204519 | sqlite3_step(pUp); |
| | @@ -212503,15 +212868,16 @@ |
| 212503 | 212868 | */ |
| 212504 | 212869 | static int dbpageBegin(sqlite3_vtab *pVtab){ |
| 212505 | 212870 | DbpageTable *pTab = (DbpageTable *)pVtab; |
| 212506 | 212871 | sqlite3 *db = pTab->db; |
| 212507 | 212872 | int i; |
| 212508 | | - for(i=0; i<db->nDb; i++){ |
| 212873 | + int rc = SQLITE_OK; |
| 212874 | + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 212509 | 212875 | Btree *pBt = db->aDb[i].pBt; |
| 212510 | | - if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0); |
| 212876 | + if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0); |
| 212511 | 212877 | } |
| 212512 | | - return SQLITE_OK; |
| 212878 | + return rc; |
| 212513 | 212879 | } |
| 212514 | 212880 | |
| 212515 | 212881 | |
| 212516 | 212882 | /* |
| 212517 | 212883 | ** Invoke this routine to register the "dbpage" virtual table module |
| | @@ -219231,11 +219597,11 @@ |
| 219231 | 219597 | static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...); |
| 219232 | 219598 | |
| 219233 | 219599 | static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...); |
| 219234 | 219600 | |
| 219235 | 219601 | #define fts5BufferZero(x) sqlite3Fts5BufferZero(x) |
| 219236 | | -#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c) |
| 219602 | +#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c) |
| 219237 | 219603 | #define fts5BufferFree(a) sqlite3Fts5BufferFree(a) |
| 219238 | 219604 | #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d) |
| 219239 | 219605 | #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d) |
| 219240 | 219606 | |
| 219241 | 219607 | #define fts5BufferGrow(pRc,pBuf,nn) ( \ |
| | @@ -231108,11 +231474,13 @@ |
| 231108 | 231474 | /* Write the rowid. */ |
| 231109 | 231475 | if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){ |
| 231110 | 231476 | fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid); |
| 231111 | 231477 | }else{ |
| 231112 | 231478 | assert_nc( p->rc || iRowid>pWriter->iPrevRowid ); |
| 231113 | | - fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid); |
| 231479 | + fts5BufferAppendVarint(&p->rc, &pPage->buf, |
| 231480 | + (u64)iRowid - (u64)pWriter->iPrevRowid |
| 231481 | + ); |
| 231114 | 231482 | } |
| 231115 | 231483 | pWriter->iPrevRowid = iRowid; |
| 231116 | 231484 | pWriter->bFirstRowidInDoclist = 0; |
| 231117 | 231485 | pWriter->bFirstRowidInPage = 0; |
| 231118 | 231486 | } |
| | @@ -231872,21 +232240,21 @@ |
| 231872 | 232240 | return fts5IndexReturn(p); |
| 231873 | 232241 | } |
| 231874 | 232242 | |
| 231875 | 232243 | static void fts5AppendRowid( |
| 231876 | 232244 | Fts5Index *p, |
| 231877 | | - i64 iDelta, |
| 232245 | + u64 iDelta, |
| 231878 | 232246 | Fts5Iter *pUnused, |
| 231879 | 232247 | Fts5Buffer *pBuf |
| 231880 | 232248 | ){ |
| 231881 | 232249 | UNUSED_PARAM(pUnused); |
| 231882 | 232250 | fts5BufferAppendVarint(&p->rc, pBuf, iDelta); |
| 231883 | 232251 | } |
| 231884 | 232252 | |
| 231885 | 232253 | static void fts5AppendPoslist( |
| 231886 | 232254 | Fts5Index *p, |
| 231887 | | - i64 iDelta, |
| 232255 | + u64 iDelta, |
| 231888 | 232256 | Fts5Iter *pMulti, |
| 231889 | 232257 | Fts5Buffer *pBuf |
| 231890 | 232258 | ){ |
| 231891 | 232259 | int nData = pMulti->base.nData; |
| 231892 | 232260 | int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING; |
| | @@ -231957,14 +232325,14 @@ |
| 231957 | 232325 | fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid); |
| 231958 | 232326 | *piLastRowid = iRowid; |
| 231959 | 232327 | } |
| 231960 | 232328 | #endif |
| 231961 | 232329 | |
| 231962 | | -#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \ |
| 231963 | | - assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \ |
| 231964 | | - fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \ |
| 231965 | | - (iLastRowid) = (iRowid); \ |
| 232330 | +#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \ |
| 232331 | + assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \ |
| 232332 | + fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \ |
| 232333 | + (iLastRowid) = (iRowid); \ |
| 231966 | 232334 | } |
| 231967 | 232335 | |
| 231968 | 232336 | /* |
| 231969 | 232337 | ** Swap the contents of buffer *p1 with that of *p2. |
| 231970 | 232338 | */ |
| | @@ -232231,11 +232599,11 @@ |
| 232231 | 232599 | Fts5Buffer *aBuf; |
| 232232 | 232600 | int nBuf = 32; |
| 232233 | 232601 | int nMerge = 1; |
| 232234 | 232602 | |
| 232235 | 232603 | void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*); |
| 232236 | | - void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*); |
| 232604 | + void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*); |
| 232237 | 232605 | if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){ |
| 232238 | 232606 | xMerge = fts5MergeRowidLists; |
| 232239 | 232607 | xAppend = fts5AppendRowid; |
| 232240 | 232608 | }else{ |
| 232241 | 232609 | nMerge = FTS5_MERGE_NLIST-1; |
| | @@ -232270,11 +232638,11 @@ |
| 232270 | 232638 | fts5MultiIterNext2(p, p1, &dummy) |
| 232271 | 232639 | ){ |
| 232272 | 232640 | Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; |
| 232273 | 232641 | p1->xSetOutputs(p1, pSeg); |
| 232274 | 232642 | if( p1->base.nData ){ |
| 232275 | | - xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist); |
| 232643 | + xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist); |
| 232276 | 232644 | iLastRowid = p1->base.iRowid; |
| 232277 | 232645 | } |
| 232278 | 232646 | } |
| 232279 | 232647 | fts5MultiIterFree(p1); |
| 232280 | 232648 | } |
| | @@ -232318,11 +232686,11 @@ |
| 232318 | 232686 | } |
| 232319 | 232687 | } |
| 232320 | 232688 | iLastRowid = 0; |
| 232321 | 232689 | } |
| 232322 | 232690 | |
| 232323 | | - xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist); |
| 232691 | + xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist); |
| 232324 | 232692 | iLastRowid = p1->base.iRowid; |
| 232325 | 232693 | } |
| 232326 | 232694 | |
| 232327 | 232695 | assert( (nBuf%nMerge)==0 ); |
| 232328 | 232696 | for(i=0; i<nBuf; i+=nMerge){ |
| | @@ -236634,11 +237002,11 @@ |
| 236634 | 237002 | int nArg, /* Number of args */ |
| 236635 | 237003 | sqlite3_value **apUnused /* Function arguments */ |
| 236636 | 237004 | ){ |
| 236637 | 237005 | assert( nArg==0 ); |
| 236638 | 237006 | UNUSED_PARAM2(nArg, apUnused); |
| 236639 | | - sqlite3_result_text(pCtx, "fts5: 2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21", -1, SQLITE_TRANSIENT); |
| 237007 | + sqlite3_result_text(pCtx, "fts5: 2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c", -1, SQLITE_TRANSIENT); |
| 236640 | 237008 | } |
| 236641 | 237009 | |
| 236642 | 237010 | /* |
| 236643 | 237011 | ** Return true if zName is the extension on one of the shadow tables used |
| 236644 | 237012 | ** by this module. |
| 236645 | 237013 | |