| | @@ -438,11 +438,12 @@ |
| 438 | 438 | ** if it is already defined or if it is unneeded because we are |
| 439 | 439 | ** not doing a threadsafe build. Ticket #2681. |
| 440 | 440 | ** |
| 441 | 441 | ** See also ticket #2741. |
| 442 | 442 | */ |
| 443 | | -#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE |
| 443 | +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) \ |
| 444 | + && !defined(__APPLE__) && SQLITE_THREADSAFE |
| 444 | 445 | # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */ |
| 445 | 446 | #endif |
| 446 | 447 | |
| 447 | 448 | /* |
| 448 | 449 | ** The TCL headers are only needed when compiling the TCL bindings. |
| | @@ -673,11 +674,11 @@ |
| 673 | 674 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 674 | 675 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 675 | 676 | */ |
| 676 | 677 | #define SQLITE_VERSION "3.7.16" |
| 677 | 678 | #define SQLITE_VERSION_NUMBER 3007016 |
| 678 | | -#define SQLITE_SOURCE_ID "2013-02-13 14:04:28 7e10a62d0eb1cb2bdafb6752b78a9d368e9f21f5" |
| 679 | +#define SQLITE_SOURCE_ID "2013-03-01 23:40:26 780d06c5e54590f677f993fa9c313989c2eab8c7" |
| 679 | 680 | |
| 680 | 681 | /* |
| 681 | 682 | ** CAPI3REF: Run-Time Library Version Numbers |
| 682 | 683 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 683 | 684 | ** |
| | @@ -11915,11 +11916,11 @@ |
| 11915 | 11916 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); |
| 11916 | 11917 | SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); |
| 11917 | 11918 | SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int); |
| 11918 | 11919 | SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); |
| 11919 | 11920 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
| 11920 | | -SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *); |
| 11921 | +SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*); |
| 11921 | 11922 | #endif |
| 11922 | 11923 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); |
| 11923 | 11924 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); |
| 11924 | 11925 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 11925 | 11926 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| | @@ -12096,12 +12097,15 @@ |
| 12096 | 12097 | ** |
| 12097 | 12098 | ** x = getVarint32( A, B ); |
| 12098 | 12099 | ** x = putVarint32( A, B ); |
| 12099 | 12100 | ** |
| 12100 | 12101 | */ |
| 12101 | | -#define getVarint32(A,B) (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B))) |
| 12102 | | -#define putVarint32(A,B) (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B))) |
| 12102 | +#define getVarint32(A,B) \ |
| 12103 | + (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B))) |
| 12104 | +#define putVarint32(A,B) \ |
| 12105 | + (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\ |
| 12106 | + sqlite3PutVarint32((A),(B))) |
| 12103 | 12107 | #define getVarint sqlite3GetVarint |
| 12104 | 12108 | #define putVarint sqlite3PutVarint |
| 12105 | 12109 | |
| 12106 | 12110 | |
| 12107 | 12111 | SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *); |
| | @@ -27655,11 +27659,11 @@ |
| 27655 | 27659 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 27656 | 27660 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 27657 | 27661 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 27658 | 27662 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
| 27659 | 27663 | } |
| 27660 | | - if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
| 27664 | + if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| 27661 | 27665 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
| 27662 | 27666 | } |
| 27663 | 27667 | |
| 27664 | 27668 | #if OS_VXWORKS |
| 27665 | 27669 | pNew->pId = vxworksFindFileId(zFilename); |
| | @@ -31959,21 +31963,23 @@ |
| 31959 | 31963 | bReturn = TRUE; |
| 31960 | 31964 | } |
| 31961 | 31965 | } |
| 31962 | 31966 | |
| 31963 | 31967 | /* Want a pending lock? */ |
| 31964 | | - else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){ |
| 31968 | + else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 31969 | + && nNumberOfBytesToLockLow == 1){ |
| 31965 | 31970 | /* If no pending lock has been acquired, then acquire it */ |
| 31966 | 31971 | if (pFile->shared->bPending == 0) { |
| 31967 | 31972 | pFile->shared->bPending = TRUE; |
| 31968 | 31973 | pFile->local.bPending = TRUE; |
| 31969 | 31974 | bReturn = TRUE; |
| 31970 | 31975 | } |
| 31971 | 31976 | } |
| 31972 | 31977 | |
| 31973 | 31978 | /* Want a reserved lock? */ |
| 31974 | | - else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){ |
| 31979 | + else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 31980 | + && nNumberOfBytesToLockLow == 1){ |
| 31975 | 31981 | if (pFile->shared->bReserved == 0) { |
| 31976 | 31982 | pFile->shared->bReserved = TRUE; |
| 31977 | 31983 | pFile->local.bReserved = TRUE; |
| 31978 | 31984 | bReturn = TRUE; |
| 31979 | 31985 | } |
| | @@ -32012,11 +32018,12 @@ |
| 32012 | 32018 | bReturn = TRUE; |
| 32013 | 32019 | } |
| 32014 | 32020 | |
| 32015 | 32021 | /* Did we just have a reader lock? */ |
| 32016 | 32022 | else if (pFile->local.nReaders){ |
| 32017 | | - assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1); |
| 32023 | + assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE |
| 32024 | + || nNumberOfBytesToUnlockLow == 1); |
| 32018 | 32025 | pFile->local.nReaders --; |
| 32019 | 32026 | if (pFile->local.nReaders == 0) |
| 32020 | 32027 | { |
| 32021 | 32028 | pFile->shared->nReaders --; |
| 32022 | 32029 | } |
| | @@ -32023,19 +32030,21 @@ |
| 32023 | 32030 | bReturn = TRUE; |
| 32024 | 32031 | } |
| 32025 | 32032 | } |
| 32026 | 32033 | |
| 32027 | 32034 | /* Releasing a pending lock */ |
| 32028 | | - else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){ |
| 32035 | + else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 32036 | + && nNumberOfBytesToUnlockLow == 1){ |
| 32029 | 32037 | if (pFile->local.bPending){ |
| 32030 | 32038 | pFile->local.bPending = FALSE; |
| 32031 | 32039 | pFile->shared->bPending = FALSE; |
| 32032 | 32040 | bReturn = TRUE; |
| 32033 | 32041 | } |
| 32034 | 32042 | } |
| 32035 | 32043 | /* Releasing a reserved lock */ |
| 32036 | | - else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){ |
| 32044 | + else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 32045 | + && nNumberOfBytesToUnlockLow == 1){ |
| 32037 | 32046 | if (pFile->local.bReserved) { |
| 32038 | 32047 | pFile->local.bReserved = FALSE; |
| 32039 | 32048 | pFile->shared->bReserved = FALSE; |
| 32040 | 32049 | bReturn = TRUE; |
| 32041 | 32050 | } |
| | @@ -32197,10 +32206,11 @@ |
| 32197 | 32206 | assert( id!=0 ); |
| 32198 | 32207 | #ifndef SQLITE_OMIT_WAL |
| 32199 | 32208 | assert( pFile->pShm==0 ); |
| 32200 | 32209 | #endif |
| 32201 | 32210 | OSTRACE(("CLOSE %d\n", pFile->h)); |
| 32211 | + assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE ); |
| 32202 | 32212 | do{ |
| 32203 | 32213 | rc = osCloseHandle(pFile->h); |
| 32204 | 32214 | /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */ |
| 32205 | 32215 | }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); |
| 32206 | 32216 | #if SQLITE_OS_WINCE |
| | @@ -33113,11 +33123,11 @@ |
| 33113 | 33123 | bRc = osCloseHandle(p->aRegion[i].hMap); |
| 33114 | 33124 | OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n", |
| 33115 | 33125 | (int)osGetCurrentProcessId(), i, |
| 33116 | 33126 | bRc ? "ok" : "failed")); |
| 33117 | 33127 | } |
| 33118 | | - if( p->hFile.h != INVALID_HANDLE_VALUE ){ |
| 33128 | + if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){ |
| 33119 | 33129 | SimulateIOErrorBenign(1); |
| 33120 | 33130 | winClose((sqlite3_file *)&p->hFile); |
| 33121 | 33131 | SimulateIOErrorBenign(0); |
| 33122 | 33132 | } |
| 33123 | 33133 | if( deleteFlag ){ |
| | @@ -33193,11 +33203,11 @@ |
| 33193 | 33203 | } |
| 33194 | 33204 | |
| 33195 | 33205 | rc = winOpen(pDbFd->pVfs, |
| 33196 | 33206 | pShmNode->zFilename, /* Name of the file (UTF-8) */ |
| 33197 | 33207 | (sqlite3_file*)&pShmNode->hFile, /* File handle here */ |
| 33198 | | - SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */ |
| 33208 | + SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 33199 | 33209 | 0); |
| 33200 | 33210 | if( SQLITE_OK!=rc ){ |
| 33201 | 33211 | goto shm_open_err; |
| 33202 | 33212 | } |
| 33203 | 33213 | |
| | @@ -33808,22 +33818,21 @@ |
| 33808 | 33818 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 33809 | 33819 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
| 33810 | 33820 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
| 33811 | 33821 | ); |
| 33812 | 33822 | |
| 33813 | | - assert( id!=0 ); |
| 33814 | | - UNUSED_PARAMETER(pVfs); |
| 33823 | + assert( pFile!=0 ); |
| 33824 | + memset(pFile, 0, sizeof(winFile)); |
| 33825 | + pFile->h = INVALID_HANDLE_VALUE; |
| 33815 | 33826 | |
| 33816 | 33827 | #if SQLITE_OS_WINRT |
| 33817 | 33828 | if( !sqlite3_temp_directory ){ |
| 33818 | 33829 | sqlite3_log(SQLITE_ERROR, |
| 33819 | 33830 | "sqlite3_temp_directory variable should be set for WinRT"); |
| 33820 | 33831 | } |
| 33821 | 33832 | #endif |
| 33822 | 33833 | |
| 33823 | | - pFile->h = INVALID_HANDLE_VALUE; |
| 33824 | | - |
| 33825 | 33834 | /* If the second argument to this function is NULL, generate a |
| 33826 | 33835 | ** temporary file name to use |
| 33827 | 33836 | */ |
| 33828 | 33837 | if( !zUtf8Name ){ |
| 33829 | 33838 | assert(isDelete && !isOpenJournal); |
| | @@ -33948,11 +33957,13 @@ |
| 33948 | 33957 | pFile->lastErrno = lastErrno; |
| 33949 | 33958 | winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); |
| 33950 | 33959 | sqlite3_free(zConverted); |
| 33951 | 33960 | if( isReadWrite && !isExclusive ){ |
| 33952 | 33961 | return winOpen(pVfs, zName, id, |
| 33953 | | - ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); |
| 33962 | + ((flags|SQLITE_OPEN_READONLY) & |
| 33963 | + ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), |
| 33964 | + pOutFlags); |
| 33954 | 33965 | }else{ |
| 33955 | 33966 | return SQLITE_CANTOPEN_BKPT; |
| 33956 | 33967 | } |
| 33957 | 33968 | } |
| 33958 | 33969 | |
| | @@ -33962,23 +33973,10 @@ |
| 33962 | 33973 | }else{ |
| 33963 | 33974 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 33964 | 33975 | } |
| 33965 | 33976 | } |
| 33966 | 33977 | |
| 33967 | | - memset(pFile, 0, sizeof(*pFile)); |
| 33968 | | - pFile->pMethod = &winIoMethod; |
| 33969 | | - pFile->h = h; |
| 33970 | | - pFile->lastErrno = NO_ERROR; |
| 33971 | | - pFile->pVfs = pVfs; |
| 33972 | | -#ifndef SQLITE_OMIT_WAL |
| 33973 | | - pFile->pShm = 0; |
| 33974 | | -#endif |
| 33975 | | - pFile->zPath = zName; |
| 33976 | | - if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 33977 | | - pFile->ctrlFlags |= WINFILE_PSOW; |
| 33978 | | - } |
| 33979 | | - |
| 33980 | 33978 | #if SQLITE_OS_WINCE |
| 33981 | 33979 | if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB |
| 33982 | 33980 | && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK |
| 33983 | 33981 | ){ |
| 33984 | 33982 | osCloseHandle(h); |
| | @@ -33990,10 +33988,19 @@ |
| 33990 | 33988 | }else |
| 33991 | 33989 | #endif |
| 33992 | 33990 | { |
| 33993 | 33991 | sqlite3_free(zConverted); |
| 33994 | 33992 | } |
| 33993 | + |
| 33994 | + pFile->pMethod = &winIoMethod; |
| 33995 | + pFile->pVfs = pVfs; |
| 33996 | + pFile->h = h; |
| 33997 | + if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 33998 | + pFile->ctrlFlags |= WINFILE_PSOW; |
| 33999 | + } |
| 34000 | + pFile->lastErrno = NO_ERROR; |
| 34001 | + pFile->zPath = zName; |
| 33995 | 34002 | |
| 33996 | 34003 | OpenCounter(+1); |
| 33997 | 34004 | return rc; |
| 33998 | 34005 | } |
| 33999 | 34006 | |
| | @@ -34035,11 +34042,12 @@ |
| 34035 | 34042 | if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard, |
| 34036 | 34043 | &sAttrData) ){ |
| 34037 | 34044 | attr = sAttrData.dwFileAttributes; |
| 34038 | 34045 | }else{ |
| 34039 | 34046 | lastErrno = osGetLastError(); |
| 34040 | | - if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34047 | + if( lastErrno==ERROR_FILE_NOT_FOUND |
| 34048 | + || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34041 | 34049 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 34042 | 34050 | }else{ |
| 34043 | 34051 | rc = SQLITE_ERROR; |
| 34044 | 34052 | } |
| 34045 | 34053 | break; |
| | @@ -34047,11 +34055,12 @@ |
| 34047 | 34055 | #else |
| 34048 | 34056 | attr = osGetFileAttributesW(zConverted); |
| 34049 | 34057 | #endif |
| 34050 | 34058 | if ( attr==INVALID_FILE_ATTRIBUTES ){ |
| 34051 | 34059 | lastErrno = osGetLastError(); |
| 34052 | | - if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34060 | + if( lastErrno==ERROR_FILE_NOT_FOUND |
| 34061 | + || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34053 | 34062 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 34054 | 34063 | }else{ |
| 34055 | 34064 | rc = SQLITE_ERROR; |
| 34056 | 34065 | } |
| 34057 | 34066 | break; |
| | @@ -34074,11 +34083,12 @@ |
| 34074 | 34083 | else{ |
| 34075 | 34084 | do { |
| 34076 | 34085 | attr = osGetFileAttributesA(zConverted); |
| 34077 | 34086 | if ( attr==INVALID_FILE_ATTRIBUTES ){ |
| 34078 | 34087 | lastErrno = osGetLastError(); |
| 34079 | | - if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34088 | + if( lastErrno==ERROR_FILE_NOT_FOUND |
| 34089 | + || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 34080 | 34090 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 34081 | 34091 | }else{ |
| 34082 | 34092 | rc = SQLITE_ERROR; |
| 34083 | 34093 | } |
| 34084 | 34094 | break; |
| | @@ -34242,20 +34252,16 @@ |
| 34242 | 34252 | ** for converting the relative path name to an absolute |
| 34243 | 34253 | ** one by prepending the data directory and a slash. |
| 34244 | 34254 | */ |
| 34245 | 34255 | char zOut[MAX_PATH+1]; |
| 34246 | 34256 | memset(zOut, 0, MAX_PATH+1); |
| 34247 | | - cygwin_conv_to_win32_path(zRelative, zOut); /* POSIX to Win32 */ |
| 34257 | + cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut, |
| 34258 | + MAX_PATH+1); |
| 34248 | 34259 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s", |
| 34249 | 34260 | sqlite3_data_directory, zOut); |
| 34250 | 34261 | }else{ |
| 34251 | | - /* |
| 34252 | | - ** NOTE: The Cygwin docs state that the maximum length needed |
| 34253 | | - ** for the buffer passed to cygwin_conv_to_full_win32_path |
| 34254 | | - ** is MAX_PATH. |
| 34255 | | - */ |
| 34256 | | - cygwin_conv_to_full_win32_path(zRelative, zFull); |
| 34262 | + cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull); |
| 34257 | 34263 | } |
| 34258 | 34264 | return SQLITE_OK; |
| 34259 | 34265 | #endif |
| 34260 | 34266 | |
| 34261 | 34267 | #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__) |
| | @@ -34409,13 +34415,13 @@ |
| 34409 | 34415 | } |
| 34410 | 34416 | static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
| 34411 | 34417 | UNUSED_PARAMETER(pVfs); |
| 34412 | 34418 | getLastErrorMsg(osGetLastError(), nBuf, zBufOut); |
| 34413 | 34419 | } |
| 34414 | | -static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
| 34420 | +static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){ |
| 34415 | 34421 | UNUSED_PARAMETER(pVfs); |
| 34416 | | - return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol); |
| 34422 | + return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym); |
| 34417 | 34423 | } |
| 34418 | 34424 | static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 34419 | 34425 | UNUSED_PARAMETER(pVfs); |
| 34420 | 34426 | osFreeLibrary((HANDLE)pHandle); |
| 34421 | 34427 | } |
| | @@ -34509,11 +34515,12 @@ |
| 34509 | 34515 | #ifdef SQLITE_TEST |
| 34510 | 34516 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 34511 | 34517 | #endif |
| 34512 | 34518 | /* 2^32 - to avoid use of LL and warnings in gcc */ |
| 34513 | 34519 | static const sqlite3_int64 max32BitValue = |
| 34514 | | - (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296; |
| 34520 | + (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + |
| 34521 | + (sqlite3_int64)294967296; |
| 34515 | 34522 | |
| 34516 | 34523 | #if SQLITE_OS_WINCE |
| 34517 | 34524 | SYSTEMTIME time; |
| 34518 | 34525 | osGetSystemTime(&time); |
| 34519 | 34526 | /* if SystemTimeToFileTime() fails, it returns zero. */ |
| | @@ -39187,10 +39194,12 @@ |
| 39187 | 39194 | pPager->eState = PAGER_ERROR; |
| 39188 | 39195 | } |
| 39189 | 39196 | return rc; |
| 39190 | 39197 | } |
| 39191 | 39198 | |
| 39199 | +static int pager_truncate(Pager *pPager, Pgno nPage); |
| 39200 | + |
| 39192 | 39201 | /* |
| 39193 | 39202 | ** This routine ends a transaction. A transaction is usually ended by |
| 39194 | 39203 | ** either a COMMIT or a ROLLBACK operation. This routine may be called |
| 39195 | 39204 | ** after rollback of a hot-journal, or if an error occurs while opening |
| 39196 | 39205 | ** the journal file or writing the very first journal-header of a |
| | @@ -39240,11 +39249,11 @@ |
| 39240 | 39249 | ** tries to unlock the database file if not in exclusive mode. If the |
| 39241 | 39250 | ** unlock operation fails as well, then the first error code related |
| 39242 | 39251 | ** to the first error encountered (the journal finalization one) is |
| 39243 | 39252 | ** returned. |
| 39244 | 39253 | */ |
| 39245 | | -static int pager_end_transaction(Pager *pPager, int hasMaster){ |
| 39254 | +static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ |
| 39246 | 39255 | int rc = SQLITE_OK; /* Error code from journal finalization operation */ |
| 39247 | 39256 | int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ |
| 39248 | 39257 | |
| 39249 | 39258 | /* Do nothing if the pager does not have an open write transaction |
| 39250 | 39259 | ** or at least a RESERVED lock. This function may be called when there |
| | @@ -39326,11 +39335,21 @@ |
| 39326 | 39335 | ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE |
| 39327 | 39336 | ** lock held on the database file. |
| 39328 | 39337 | */ |
| 39329 | 39338 | rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); |
| 39330 | 39339 | assert( rc2==SQLITE_OK ); |
| 39340 | + }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ |
| 39341 | + /* This branch is taken when committing a transaction in rollback-journal |
| 39342 | + ** mode if the database file on disk is larger than the database image. |
| 39343 | + ** At this point the journal has been finalized and the transaction |
| 39344 | + ** successfully committed, but the EXCLUSIVE lock is still held on the |
| 39345 | + ** file. So it is safe to truncate the database file to its minimum |
| 39346 | + ** required size. */ |
| 39347 | + assert( pPager->eLock==EXCLUSIVE_LOCK ); |
| 39348 | + rc = pager_truncate(pPager, pPager->dbSize); |
| 39331 | 39349 | } |
| 39350 | + |
| 39332 | 39351 | if( !pPager->exclusiveMode |
| 39333 | 39352 | && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0)) |
| 39334 | 39353 | ){ |
| 39335 | 39354 | rc2 = pagerUnlockDb(pPager, SHARED_LOCK); |
| 39336 | 39355 | pPager->changeCountDone = 0; |
| | @@ -39365,11 +39384,11 @@ |
| 39365 | 39384 | sqlite3BeginBenignMalloc(); |
| 39366 | 39385 | sqlite3PagerRollback(pPager); |
| 39367 | 39386 | sqlite3EndBenignMalloc(); |
| 39368 | 39387 | }else if( !pPager->exclusiveMode ){ |
| 39369 | 39388 | assert( pPager->eState==PAGER_READER ); |
| 39370 | | - pager_end_transaction(pPager, 0); |
| 39389 | + pager_end_transaction(pPager, 0, 0); |
| 39371 | 39390 | } |
| 39372 | 39391 | } |
| 39373 | 39392 | pager_unlock(pPager); |
| 39374 | 39393 | } |
| 39375 | 39394 | |
| | @@ -40140,11 +40159,11 @@ |
| 40140 | 40159 | && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 40141 | 40160 | ){ |
| 40142 | 40161 | rc = sqlite3PagerSync(pPager); |
| 40143 | 40162 | } |
| 40144 | 40163 | if( rc==SQLITE_OK ){ |
| 40145 | | - rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
| 40164 | + rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0); |
| 40146 | 40165 | testcase( rc!=SQLITE_OK ); |
| 40147 | 40166 | } |
| 40148 | 40167 | if( rc==SQLITE_OK && zMaster[0] && res ){ |
| 40149 | 40168 | /* If there was a master journal and this routine will return success, |
| 40150 | 40169 | ** see if it is possible to delete the master journal. |
| | @@ -43234,40 +43253,10 @@ |
| 43234 | 43253 | #else |
| 43235 | 43254 | rc = pager_incr_changecounter(pPager, 0); |
| 43236 | 43255 | #endif |
| 43237 | 43256 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 43238 | 43257 | |
| 43239 | | - /* If this transaction has made the database smaller, then all pages |
| 43240 | | - ** being discarded by the truncation must be written to the journal |
| 43241 | | - ** file. |
| 43242 | | - ** |
| 43243 | | - ** Before reading the pages with page numbers larger than the |
| 43244 | | - ** current value of Pager.dbSize, set dbSize back to the value |
| 43245 | | - ** that it took at the start of the transaction. Otherwise, the |
| 43246 | | - ** calls to sqlite3PagerGet() return zeroed pages instead of |
| 43247 | | - ** reading data from the database file. |
| 43248 | | - */ |
| 43249 | | - if( pPager->dbSize<pPager->dbOrigSize |
| 43250 | | - && pPager->journalMode!=PAGER_JOURNALMODE_OFF |
| 43251 | | - ){ |
| 43252 | | - Pgno i; /* Iterator variable */ |
| 43253 | | - const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */ |
| 43254 | | - const Pgno dbSize = pPager->dbSize; /* Database image size */ |
| 43255 | | - pPager->dbSize = pPager->dbOrigSize; |
| 43256 | | - for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){ |
| 43257 | | - if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
| 43258 | | - PgHdr *pPage; /* Page to journal */ |
| 43259 | | - rc = sqlite3PagerGet(pPager, i, &pPage); |
| 43260 | | - if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 43261 | | - rc = sqlite3PagerWrite(pPage); |
| 43262 | | - sqlite3PagerUnref(pPage); |
| 43263 | | - if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 43264 | | - } |
| 43265 | | - } |
| 43266 | | - pPager->dbSize = dbSize; |
| 43267 | | - } |
| 43268 | | - |
| 43269 | 43258 | /* Write the master journal name into the journal file. If a master |
| 43270 | 43259 | ** journal file name has already been written to the journal file, |
| 43271 | 43260 | ** or if zMaster is NULL (no master journal), then this call is a no-op. |
| 43272 | 43261 | */ |
| 43273 | 43262 | rc = writeMasterJournal(pPager, zMaster); |
| | @@ -43291,15 +43280,18 @@ |
| 43291 | 43280 | if( rc!=SQLITE_OK ){ |
| 43292 | 43281 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 43293 | 43282 | goto commit_phase_one_exit; |
| 43294 | 43283 | } |
| 43295 | 43284 | sqlite3PcacheCleanAll(pPager->pPCache); |
| 43296 | | - |
| 43297 | | - /* If the file on disk is not the same size as the database image, |
| 43298 | | - ** then use pager_truncate to grow or shrink the file here. |
| 43299 | | - */ |
| 43300 | | - if( pPager->dbSize!=pPager->dbFileSize ){ |
| 43285 | + |
| 43286 | + /* If the file on disk is smaller than the database image, use |
| 43287 | + ** pager_truncate to grow the file here. This can happen if the database |
| 43288 | + ** image was extended as part of the current transaction and then the |
| 43289 | + ** last page in the db image moved to the free-list. In this case the |
| 43290 | + ** last page is never written out to disk, leaving the database file |
| 43291 | + ** undersized. Fix this now if it is the case. */ |
| 43292 | + if( pPager->dbSize>pPager->dbFileSize ){ |
| 43301 | 43293 | Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); |
| 43302 | 43294 | assert( pPager->eState==PAGER_WRITER_DBMOD ); |
| 43303 | 43295 | rc = pager_truncate(pPager, nNew); |
| 43304 | 43296 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 43305 | 43297 | } |
| | @@ -43368,11 +43360,11 @@ |
| 43368 | 43360 | pPager->eState = PAGER_READER; |
| 43369 | 43361 | return SQLITE_OK; |
| 43370 | 43362 | } |
| 43371 | 43363 | |
| 43372 | 43364 | PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); |
| 43373 | | - rc = pager_end_transaction(pPager, pPager->setMaster); |
| 43365 | + rc = pager_end_transaction(pPager, pPager->setMaster, 1); |
| 43374 | 43366 | return pager_error(pPager, rc); |
| 43375 | 43367 | } |
| 43376 | 43368 | |
| 43377 | 43369 | /* |
| 43378 | 43370 | ** If a write transaction is open, then all changes made within the |
| | @@ -43413,15 +43405,15 @@ |
| 43413 | 43405 | if( pPager->eState<=PAGER_READER ) return SQLITE_OK; |
| 43414 | 43406 | |
| 43415 | 43407 | if( pagerUseWal(pPager) ){ |
| 43416 | 43408 | int rc2; |
| 43417 | 43409 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); |
| 43418 | | - rc2 = pager_end_transaction(pPager, pPager->setMaster); |
| 43410 | + rc2 = pager_end_transaction(pPager, pPager->setMaster, 0); |
| 43419 | 43411 | if( rc==SQLITE_OK ) rc = rc2; |
| 43420 | 43412 | }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ |
| 43421 | 43413 | int eState = pPager->eState; |
| 43422 | | - rc = pager_end_transaction(pPager, 0); |
| 43414 | + rc = pager_end_transaction(pPager, 0, 0); |
| 43423 | 43415 | if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ |
| 43424 | 43416 | /* This can happen using journal_mode=off. Move the pager to the error |
| 43425 | 43417 | ** state to indicate that the contents of the cache may not be trusted. |
| 43426 | 43418 | ** Any active readers will get SQLITE_ABORT. |
| 43427 | 43419 | */ |
| | @@ -43815,11 +43807,12 @@ |
| 43815 | 43807 | ** the journal needs to be sync()ed before database page pPg->pgno |
| 43816 | 43808 | ** can be written to. The caller has already promised not to write to it. |
| 43817 | 43809 | */ |
| 43818 | 43810 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
| 43819 | 43811 | needSyncPgno = pPg->pgno; |
| 43820 | | - assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
| 43812 | + assert( pPager->journalMode==PAGER_JOURNALMODE_OFF || |
| 43813 | + pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
| 43821 | 43814 | assert( pPg->flags&PGHDR_DIRTY ); |
| 43822 | 43815 | } |
| 43823 | 43816 | |
| 43824 | 43817 | /* If the cache contains a page with page-number pgno, remove it |
| 43825 | 43818 | ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for |
| | @@ -47819,10 +47812,11 @@ |
| 47819 | 47812 | MemPage *pPage1; /* First page of the database */ |
| 47820 | 47813 | u8 openFlags; /* Flags to sqlite3BtreeOpen() */ |
| 47821 | 47814 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 47822 | 47815 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 47823 | 47816 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 47817 | + u8 bDoTruncate; /* True to truncate db on commit */ |
| 47824 | 47818 | #endif |
| 47825 | 47819 | u8 inTransaction; /* Transaction state */ |
| 47826 | 47820 | u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ |
| 47827 | 47821 | u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
| 47828 | 47822 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| | @@ -50937,10 +50931,11 @@ |
| 50937 | 50931 | ** is requested, this is a no-op. |
| 50938 | 50932 | */ |
| 50939 | 50933 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
| 50940 | 50934 | goto trans_begun; |
| 50941 | 50935 | } |
| 50936 | + assert( pBt->bDoTruncate==0 ); |
| 50942 | 50937 | |
| 50943 | 50938 | /* Write transactions are not possible on a read-only database */ |
| 50944 | 50939 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ |
| 50945 | 50940 | rc = SQLITE_READONLY; |
| 50946 | 50941 | goto trans_begun; |
| | @@ -51251,30 +51246,32 @@ |
| 51251 | 51246 | return rc; |
| 51252 | 51247 | } |
| 51253 | 51248 | |
| 51254 | 51249 | /* Forward declaration required by incrVacuumStep(). */ |
| 51255 | 51250 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
| 51251 | +#define BTALLOC_ANY 0 /* Allocate any page */ |
| 51252 | +#define BTALLOC_EXACT 1 /* Allocate exact page if possible */ |
| 51253 | +#define BTALLOC_LE 2 /* Allocate any page <= the parameter */ |
| 51256 | 51254 | |
| 51257 | 51255 | /* |
| 51258 | | -** Perform a single step of an incremental-vacuum. If successful, |
| 51259 | | -** return SQLITE_OK. If there is no work to do (and therefore no |
| 51260 | | -** point in calling this function again), return SQLITE_DONE. |
| 51261 | | -** |
| 51262 | | -** More specificly, this function attempts to re-organize the |
| 51263 | | -** database so that the last page of the file currently in use |
| 51264 | | -** is no longer in use. |
| 51265 | | -** |
| 51266 | | -** If the nFin parameter is non-zero, this function assumes |
| 51267 | | -** that the caller will keep calling incrVacuumStep() until |
| 51268 | | -** it returns SQLITE_DONE or an error, and that nFin is the |
| 51269 | | -** number of pages the database file will contain after this |
| 51270 | | -** process is complete. If nFin is zero, it is assumed that |
| 51271 | | -** incrVacuumStep() will be called a finite amount of times |
| 51272 | | -** which may or may not empty the freelist. A full autovacuum |
| 51273 | | -** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0. |
| 51274 | | -*/ |
| 51275 | | -static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
| 51256 | +** Perform a single step of an incremental-vacuum. If successful, return |
| 51257 | +** SQLITE_OK. If there is no work to do (and therefore no point in |
| 51258 | +** calling this function again), return SQLITE_DONE. Or, if an error |
| 51259 | +** occurs, return some other error code. |
| 51260 | +** |
| 51261 | +** More specificly, this function attempts to re-organize the database so |
| 51262 | +** that the last page of the file currently in use is no longer in use. |
| 51263 | +** |
| 51264 | +** Parameter nFin is the number of pages that this database would contain |
| 51265 | +** were this function called until it returns SQLITE_DONE. |
| 51266 | +** |
| 51267 | +** If the bCommit parameter is non-zero, this function assumes that the |
| 51268 | +** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE |
| 51269 | +** or an error. bCommit is passed true for an auto-vacuum-on-commmit |
| 51270 | +** operation, or false for an incremental vacuum. |
| 51271 | +*/ |
| 51272 | +static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ |
| 51276 | 51273 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 51277 | 51274 | int rc; |
| 51278 | 51275 | |
| 51279 | 51276 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 51280 | 51277 | assert( iLastPg>nFin ); |
| | @@ -51295,85 +51292,98 @@ |
| 51295 | 51292 | if( eType==PTRMAP_ROOTPAGE ){ |
| 51296 | 51293 | return SQLITE_CORRUPT_BKPT; |
| 51297 | 51294 | } |
| 51298 | 51295 | |
| 51299 | 51296 | if( eType==PTRMAP_FREEPAGE ){ |
| 51300 | | - if( nFin==0 ){ |
| 51297 | + if( bCommit==0 ){ |
| 51301 | 51298 | /* Remove the page from the files free-list. This is not required |
| 51302 | | - ** if nFin is non-zero. In that case, the free-list will be |
| 51299 | + ** if bCommit is non-zero. In that case, the free-list will be |
| 51303 | 51300 | ** truncated to zero after this function returns, so it doesn't |
| 51304 | 51301 | ** matter if it still contains some garbage entries. |
| 51305 | 51302 | */ |
| 51306 | 51303 | Pgno iFreePg; |
| 51307 | 51304 | MemPage *pFreePg; |
| 51308 | | - rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 51305 | + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); |
| 51309 | 51306 | if( rc!=SQLITE_OK ){ |
| 51310 | 51307 | return rc; |
| 51311 | 51308 | } |
| 51312 | 51309 | assert( iFreePg==iLastPg ); |
| 51313 | 51310 | releasePage(pFreePg); |
| 51314 | 51311 | } |
| 51315 | 51312 | } else { |
| 51316 | 51313 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 51317 | 51314 | MemPage *pLastPg; |
| 51315 | + u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ |
| 51316 | + Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ |
| 51318 | 51317 | |
| 51319 | 51318 | rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); |
| 51320 | 51319 | if( rc!=SQLITE_OK ){ |
| 51321 | 51320 | return rc; |
| 51322 | 51321 | } |
| 51323 | 51322 | |
| 51324 | | - /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 51323 | + /* If bCommit is zero, this loop runs exactly once and page pLastPg |
| 51325 | 51324 | ** is swapped with the first free page pulled off the free list. |
| 51326 | 51325 | ** |
| 51327 | | - ** On the other hand, if nFin is greater than zero, then keep |
| 51326 | + ** On the other hand, if bCommit is greater than zero, then keep |
| 51328 | 51327 | ** looping until a free-page located within the first nFin pages |
| 51329 | 51328 | ** of the file is found. |
| 51330 | 51329 | */ |
| 51330 | + if( bCommit==0 ){ |
| 51331 | + eMode = BTALLOC_LE; |
| 51332 | + iNear = nFin; |
| 51333 | + } |
| 51331 | 51334 | do { |
| 51332 | 51335 | MemPage *pFreePg; |
| 51333 | | - rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 51336 | + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); |
| 51334 | 51337 | if( rc!=SQLITE_OK ){ |
| 51335 | 51338 | releasePage(pLastPg); |
| 51336 | 51339 | return rc; |
| 51337 | 51340 | } |
| 51338 | 51341 | releasePage(pFreePg); |
| 51339 | | - }while( nFin!=0 && iFreePg>nFin ); |
| 51342 | + }while( bCommit && iFreePg>nFin ); |
| 51340 | 51343 | assert( iFreePg<iLastPg ); |
| 51341 | 51344 | |
| 51342 | | - rc = sqlite3PagerWrite(pLastPg->pDbPage); |
| 51343 | | - if( rc==SQLITE_OK ){ |
| 51344 | | - rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
| 51345 | | - } |
| 51345 | + rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
| 51346 | 51346 | releasePage(pLastPg); |
| 51347 | 51347 | if( rc!=SQLITE_OK ){ |
| 51348 | 51348 | return rc; |
| 51349 | 51349 | } |
| 51350 | 51350 | } |
| 51351 | 51351 | } |
| 51352 | 51352 | |
| 51353 | | - if( nFin==0 ){ |
| 51354 | | - iLastPg--; |
| 51355 | | - while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 51356 | | - if( PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 51357 | | - MemPage *pPg; |
| 51358 | | - rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
| 51359 | | - if( rc!=SQLITE_OK ){ |
| 51360 | | - return rc; |
| 51361 | | - } |
| 51362 | | - rc = sqlite3PagerWrite(pPg->pDbPage); |
| 51363 | | - releasePage(pPg); |
| 51364 | | - if( rc!=SQLITE_OK ){ |
| 51365 | | - return rc; |
| 51366 | | - } |
| 51367 | | - } |
| 51368 | | - iLastPg--; |
| 51369 | | - } |
| 51370 | | - sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
| 51353 | + if( bCommit==0 ){ |
| 51354 | + do { |
| 51355 | + iLastPg--; |
| 51356 | + }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) ); |
| 51357 | + pBt->bDoTruncate = 1; |
| 51371 | 51358 | pBt->nPage = iLastPg; |
| 51372 | 51359 | } |
| 51373 | 51360 | return SQLITE_OK; |
| 51374 | 51361 | } |
| 51362 | + |
| 51363 | +/* |
| 51364 | +** The database opened by the first argument is an auto-vacuum database |
| 51365 | +** nOrig pages in size containing nFree free pages. Return the expected |
| 51366 | +** size of the database in pages following an auto-vacuum operation. |
| 51367 | +*/ |
| 51368 | +static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ |
| 51369 | + int nEntry; /* Number of entries on one ptrmap page */ |
| 51370 | + Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 51371 | + Pgno nFin; /* Return value */ |
| 51372 | + |
| 51373 | + nEntry = pBt->usableSize/5; |
| 51374 | + nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; |
| 51375 | + nFin = nOrig - nFree - nPtrmap; |
| 51376 | + if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ |
| 51377 | + nFin--; |
| 51378 | + } |
| 51379 | + while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 51380 | + nFin--; |
| 51381 | + } |
| 51382 | + |
| 51383 | + return nFin; |
| 51384 | +} |
| 51375 | 51385 | |
| 51376 | 51386 | /* |
| 51377 | 51387 | ** A write-transaction must be opened before calling this function. |
| 51378 | 51388 | ** It performs a single unit of work towards an incremental vacuum. |
| 51379 | 51389 | ** |
| | @@ -51388,15 +51398,25 @@ |
| 51388 | 51398 | sqlite3BtreeEnter(p); |
| 51389 | 51399 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 51390 | 51400 | if( !pBt->autoVacuum ){ |
| 51391 | 51401 | rc = SQLITE_DONE; |
| 51392 | 51402 | }else{ |
| 51393 | | - invalidateAllOverflowCache(pBt); |
| 51394 | | - rc = incrVacuumStep(pBt, 0, btreePagecount(pBt)); |
| 51395 | | - if( rc==SQLITE_OK ){ |
| 51396 | | - rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 51397 | | - put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 51403 | + Pgno nOrig = btreePagecount(pBt); |
| 51404 | + Pgno nFree = get4byte(&pBt->pPage1->aData[36]); |
| 51405 | + Pgno nFin = finalDbSize(pBt, nOrig, nFree); |
| 51406 | + |
| 51407 | + if( nOrig<nFin ){ |
| 51408 | + rc = SQLITE_CORRUPT_BKPT; |
| 51409 | + }else if( nFree>0 ){ |
| 51410 | + invalidateAllOverflowCache(pBt); |
| 51411 | + rc = incrVacuumStep(pBt, nFin, nOrig, 0); |
| 51412 | + if( rc==SQLITE_OK ){ |
| 51413 | + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 51414 | + put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 51415 | + } |
| 51416 | + }else{ |
| 51417 | + rc = SQLITE_DONE; |
| 51398 | 51418 | } |
| 51399 | 51419 | } |
| 51400 | 51420 | sqlite3BtreeLeave(p); |
| 51401 | 51421 | return rc; |
| 51402 | 51422 | } |
| | @@ -51419,13 +51439,11 @@ |
| 51419 | 51439 | invalidateAllOverflowCache(pBt); |
| 51420 | 51440 | assert(pBt->autoVacuum); |
| 51421 | 51441 | if( !pBt->incrVacuum ){ |
| 51422 | 51442 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 51423 | 51443 | Pgno nFree; /* Number of pages on the freelist initially */ |
| 51424 | | - Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 51425 | 51444 | Pgno iFree; /* The next page to be freed */ |
| 51426 | | - int nEntry; /* Number of entries on one ptrmap page */ |
| 51427 | 51445 | Pgno nOrig; /* Database size before freeing */ |
| 51428 | 51446 | |
| 51429 | 51447 | nOrig = btreePagecount(pBt); |
| 51430 | 51448 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 51431 | 51449 | /* It is not possible to create a database for which the final page |
| | @@ -51434,30 +51452,22 @@ |
| 51434 | 51452 | */ |
| 51435 | 51453 | return SQLITE_CORRUPT_BKPT; |
| 51436 | 51454 | } |
| 51437 | 51455 | |
| 51438 | 51456 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 51439 | | - nEntry = pBt->usableSize/5; |
| 51440 | | - nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; |
| 51441 | | - nFin = nOrig - nFree - nPtrmap; |
| 51442 | | - if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ |
| 51443 | | - nFin--; |
| 51444 | | - } |
| 51445 | | - while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 51446 | | - nFin--; |
| 51447 | | - } |
| 51457 | + nFin = finalDbSize(pBt, nOrig, nFree); |
| 51448 | 51458 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
| 51449 | 51459 | |
| 51450 | 51460 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 51451 | | - rc = incrVacuumStep(pBt, nFin, iFree); |
| 51461 | + rc = incrVacuumStep(pBt, nFin, iFree, 1); |
| 51452 | 51462 | } |
| 51453 | 51463 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
| 51454 | 51464 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 51455 | 51465 | put4byte(&pBt->pPage1->aData[32], 0); |
| 51456 | 51466 | put4byte(&pBt->pPage1->aData[36], 0); |
| 51457 | 51467 | put4byte(&pBt->pPage1->aData[28], nFin); |
| 51458 | | - sqlite3PagerTruncateImage(pBt->pPager, nFin); |
| 51468 | + pBt->bDoTruncate = 1; |
| 51459 | 51469 | pBt->nPage = nFin; |
| 51460 | 51470 | } |
| 51461 | 51471 | if( rc!=SQLITE_OK ){ |
| 51462 | 51472 | sqlite3PagerRollback(pPager); |
| 51463 | 51473 | } |
| | @@ -51508,10 +51518,13 @@ |
| 51508 | 51518 | if( rc!=SQLITE_OK ){ |
| 51509 | 51519 | sqlite3BtreeLeave(p); |
| 51510 | 51520 | return rc; |
| 51511 | 51521 | } |
| 51512 | 51522 | } |
| 51523 | + if( pBt->bDoTruncate ){ |
| 51524 | + sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); |
| 51525 | + } |
| 51513 | 51526 | #endif |
| 51514 | 51527 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
| 51515 | 51528 | sqlite3BtreeLeave(p); |
| 51516 | 51529 | } |
| 51517 | 51530 | return rc; |
| | @@ -51523,10 +51536,13 @@ |
| 51523 | 51536 | */ |
| 51524 | 51537 | static void btreeEndTransaction(Btree *p){ |
| 51525 | 51538 | BtShared *pBt = p->pBt; |
| 51526 | 51539 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 51527 | 51540 | |
| 51541 | +#ifndef SQLITE_OMIT_AUTOVACUUM |
| 51542 | + pBt->bDoTruncate = 0; |
| 51543 | +#endif |
| 51528 | 51544 | btreeClearHasContent(pBt); |
| 51529 | 51545 | if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){ |
| 51530 | 51546 | /* If there are other active statements that belong to this database |
| 51531 | 51547 | ** handle, downgrade to a read-only transaction. The other statements |
| 51532 | 51548 | ** may still be reading from the database. */ |
| | @@ -53209,11 +53225,11 @@ |
| 53209 | 53225 | static int allocateBtreePage( |
| 53210 | 53226 | BtShared *pBt, |
| 53211 | 53227 | MemPage **ppPage, |
| 53212 | 53228 | Pgno *pPgno, |
| 53213 | 53229 | Pgno nearby, |
| 53214 | | - u8 exact |
| 53230 | + u8 eMode |
| 53215 | 53231 | ){ |
| 53216 | 53232 | MemPage *pPage1; |
| 53217 | 53233 | int rc; |
| 53218 | 53234 | u32 n; /* Number of pages on the freelist */ |
| 53219 | 53235 | u32 k; /* Number of leaves on the trunk of the freelist */ |
| | @@ -53237,20 +53253,23 @@ |
| 53237 | 53253 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 53238 | 53254 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 53239 | 53255 | ** the entire-list will be searched for that page. |
| 53240 | 53256 | */ |
| 53241 | 53257 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 53242 | | - if( exact && nearby<=mxPage ){ |
| 53243 | | - u8 eType; |
| 53244 | | - assert( nearby>0 ); |
| 53245 | | - assert( pBt->autoVacuum ); |
| 53246 | | - rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 53247 | | - if( rc ) return rc; |
| 53248 | | - if( eType==PTRMAP_FREEPAGE ){ |
| 53249 | | - searchList = 1; |
| 53250 | | - } |
| 53251 | | - *pPgno = nearby; |
| 53258 | + if( eMode==BTALLOC_EXACT ){ |
| 53259 | + if( nearby<=mxPage ){ |
| 53260 | + u8 eType; |
| 53261 | + assert( nearby>0 ); |
| 53262 | + assert( pBt->autoVacuum ); |
| 53263 | + rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 53264 | + if( rc ) return rc; |
| 53265 | + if( eType==PTRMAP_FREEPAGE ){ |
| 53266 | + searchList = 1; |
| 53267 | + } |
| 53268 | + } |
| 53269 | + }else if( eMode==BTALLOC_LE ){ |
| 53270 | + searchList = 1; |
| 53252 | 53271 | } |
| 53253 | 53272 | #endif |
| 53254 | 53273 | |
| 53255 | 53274 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 53256 | 53275 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| | @@ -53301,15 +53320,17 @@ |
| 53301 | 53320 | }else if( k>(u32)(pBt->usableSize/4 - 2) ){ |
| 53302 | 53321 | /* Value of k is out of range. Database corruption */ |
| 53303 | 53322 | rc = SQLITE_CORRUPT_BKPT; |
| 53304 | 53323 | goto end_allocate_page; |
| 53305 | 53324 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 53306 | | - }else if( searchList && nearby==iTrunk ){ |
| 53325 | + }else if( searchList |
| 53326 | + && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) |
| 53327 | + ){ |
| 53307 | 53328 | /* The list is being searched and this trunk page is the page |
| 53308 | 53329 | ** to allocate, regardless of whether it has leaves. |
| 53309 | 53330 | */ |
| 53310 | | - assert( *pPgno==iTrunk ); |
| 53331 | + *pPgno = iTrunk; |
| 53311 | 53332 | *ppPage = pTrunk; |
| 53312 | 53333 | searchList = 0; |
| 53313 | 53334 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 53314 | 53335 | if( rc ){ |
| 53315 | 53336 | goto end_allocate_page; |
| | @@ -53368,18 +53389,28 @@ |
| 53368 | 53389 | u32 closest; |
| 53369 | 53390 | Pgno iPage; |
| 53370 | 53391 | unsigned char *aData = pTrunk->aData; |
| 53371 | 53392 | if( nearby>0 ){ |
| 53372 | 53393 | u32 i; |
| 53373 | | - int dist; |
| 53374 | 53394 | closest = 0; |
| 53375 | | - dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); |
| 53376 | | - for(i=1; i<k; i++){ |
| 53377 | | - int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); |
| 53378 | | - if( d2<dist ){ |
| 53379 | | - closest = i; |
| 53380 | | - dist = d2; |
| 53395 | + if( eMode==BTALLOC_LE ){ |
| 53396 | + for(i=0; i<k; i++){ |
| 53397 | + iPage = get4byte(&aData[8+i*4]); |
| 53398 | + if( iPage<=nearby ){ |
| 53399 | + closest = i; |
| 53400 | + break; |
| 53401 | + } |
| 53402 | + } |
| 53403 | + }else{ |
| 53404 | + int dist; |
| 53405 | + dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); |
| 53406 | + for(i=1; i<k; i++){ |
| 53407 | + int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); |
| 53408 | + if( d2<dist ){ |
| 53409 | + closest = i; |
| 53410 | + dist = d2; |
| 53411 | + } |
| 53381 | 53412 | } |
| 53382 | 53413 | } |
| 53383 | 53414 | }else{ |
| 53384 | 53415 | closest = 0; |
| 53385 | 53416 | } |
| | @@ -53389,11 +53420,13 @@ |
| 53389 | 53420 | if( iPage>mxPage ){ |
| 53390 | 53421 | rc = SQLITE_CORRUPT_BKPT; |
| 53391 | 53422 | goto end_allocate_page; |
| 53392 | 53423 | } |
| 53393 | 53424 | testcase( iPage==mxPage ); |
| 53394 | | - if( !searchList || iPage==nearby ){ |
| 53425 | + if( !searchList |
| 53426 | + || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) |
| 53427 | + ){ |
| 53395 | 53428 | int noContent; |
| 53396 | 53429 | *pPgno = iPage; |
| 53397 | 53430 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 53398 | 53431 | ": %d more free pages\n", |
| 53399 | 53432 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| | @@ -53416,12 +53449,30 @@ |
| 53416 | 53449 | } |
| 53417 | 53450 | releasePage(pPrevTrunk); |
| 53418 | 53451 | pPrevTrunk = 0; |
| 53419 | 53452 | }while( searchList ); |
| 53420 | 53453 | }else{ |
| 53421 | | - /* There are no pages on the freelist, so create a new page at the |
| 53422 | | - ** end of the file */ |
| 53454 | + /* There are no pages on the freelist, so append a new page to the |
| 53455 | + ** database image. |
| 53456 | + ** |
| 53457 | + ** Normally, new pages allocated by this block can be requested from the |
| 53458 | + ** pager layer with the 'no-content' flag set. This prevents the pager |
| 53459 | + ** from trying to read the pages content from disk. However, if the |
| 53460 | + ** current transaction has already run one or more incremental-vacuum |
| 53461 | + ** steps, then the page we are about to allocate may contain content |
| 53462 | + ** that is required in the event of a rollback. In this case, do |
| 53463 | + ** not set the no-content flag. This causes the pager to load and journal |
| 53464 | + ** the current page content before overwriting it. |
| 53465 | + ** |
| 53466 | + ** Note that the pager will not actually attempt to load or journal |
| 53467 | + ** content for any page that really does lie past the end of the database |
| 53468 | + ** file on disk. So the effects of disabling the no-content optimization |
| 53469 | + ** here are confined to those pages that lie between the end of the |
| 53470 | + ** database image and the end of the database file. |
| 53471 | + */ |
| 53472 | + int bNoContent = (0==pBt->bDoTruncate); |
| 53473 | + |
| 53423 | 53474 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 53424 | 53475 | if( rc ) return rc; |
| 53425 | 53476 | pBt->nPage++; |
| 53426 | 53477 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
| 53427 | 53478 | |
| | @@ -53432,11 +53483,11 @@ |
| 53432 | 53483 | ** becomes a new pointer-map page, the second is used by the caller. |
| 53433 | 53484 | */ |
| 53434 | 53485 | MemPage *pPg = 0; |
| 53435 | 53486 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 53436 | 53487 | assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
| 53437 | | - rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1); |
| 53488 | + rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); |
| 53438 | 53489 | if( rc==SQLITE_OK ){ |
| 53439 | 53490 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 53440 | 53491 | releasePage(pPg); |
| 53441 | 53492 | } |
| 53442 | 53493 | if( rc ) return rc; |
| | @@ -53446,11 +53497,11 @@ |
| 53446 | 53497 | #endif |
| 53447 | 53498 | put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 53448 | 53499 | *pPgno = pBt->nPage; |
| 53449 | 53500 | |
| 53450 | 53501 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 53451 | | - rc = btreeGetPage(pBt, *pPgno, ppPage, 1); |
| 53502 | + rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); |
| 53452 | 53503 | if( rc ) return rc; |
| 53453 | 53504 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 53454 | 53505 | if( rc!=SQLITE_OK ){ |
| 53455 | 53506 | releasePage(*ppPage); |
| 53456 | 53507 | } |
| | @@ -55461,11 +55512,11 @@ |
| 55461 | 55512 | |
| 55462 | 55513 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 55463 | 55514 | ** be moved to the allocated page (unless the allocated page happens |
| 55464 | 55515 | ** to reside at pgnoRoot). |
| 55465 | 55516 | */ |
| 55466 | | - rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
| 55517 | + rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); |
| 55467 | 55518 | if( rc!=SQLITE_OK ){ |
| 55468 | 55519 | return rc; |
| 55469 | 55520 | } |
| 55470 | 55521 | |
| 55471 | 55522 | if( pgnoMove!=pgnoRoot ){ |
| | @@ -57153,11 +57204,10 @@ |
| 57153 | 57204 | } |
| 57154 | 57205 | }else{ |
| 57155 | 57206 | nDestTruncate = nSrcPage * (pgszSrc/pgszDest); |
| 57156 | 57207 | } |
| 57157 | 57208 | assert( nDestTruncate>0 ); |
| 57158 | | - sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 57159 | 57209 | |
| 57160 | 57210 | if( pgszSrc<pgszDest ){ |
| 57161 | 57211 | /* If the source page-size is smaller than the destination page-size, |
| 57162 | 57212 | ** two extra things may need to happen: |
| 57163 | 57213 | ** |
| | @@ -57167,10 +57217,12 @@ |
| 57167 | 57217 | ** pending-byte page in the source database may need to be |
| 57168 | 57218 | ** copied into the destination database. |
| 57169 | 57219 | */ |
| 57170 | 57220 | const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; |
| 57171 | 57221 | sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); |
| 57222 | + Pgno iPg; |
| 57223 | + int nDstPage; |
| 57172 | 57224 | i64 iOff; |
| 57173 | 57225 | i64 iEnd; |
| 57174 | 57226 | |
| 57175 | 57227 | assert( pFile ); |
| 57176 | 57228 | assert( nDestTruncate==0 |
| | @@ -57177,17 +57229,30 @@ |
| 57177 | 57229 | || (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 57178 | 57230 | nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 57179 | 57231 | && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 57180 | 57232 | )); |
| 57181 | 57233 | |
| 57182 | | - /* This call ensures that all data required to recreate the original |
| 57234 | + /* This block ensures that all data required to recreate the original |
| 57183 | 57235 | ** database has been stored in the journal for pDestPager and the |
| 57184 | 57236 | ** journal synced to disk. So at this point we may safely modify |
| 57185 | 57237 | ** the database file in any way, knowing that if a power failure |
| 57186 | 57238 | ** occurs, the original database will be reconstructed from the |
| 57187 | 57239 | ** journal file. */ |
| 57188 | | - rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 57240 | + sqlite3PagerPagecount(pDestPager, &nDstPage); |
| 57241 | + for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){ |
| 57242 | + if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 57243 | + DbPage *pPg; |
| 57244 | + rc = sqlite3PagerGet(pDestPager, iPg, &pPg); |
| 57245 | + if( rc==SQLITE_OK ){ |
| 57246 | + rc = sqlite3PagerWrite(pPg); |
| 57247 | + sqlite3PagerUnref(pPg); |
| 57248 | + } |
| 57249 | + } |
| 57250 | + } |
| 57251 | + if( rc==SQLITE_OK ){ |
| 57252 | + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 57253 | + } |
| 57189 | 57254 | |
| 57190 | 57255 | /* Write the extra pages and truncate the database file as required */ |
| 57191 | 57256 | iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 57192 | 57257 | for( |
| 57193 | 57258 | iOff=PENDING_BYTE+pgszSrc; |
| | @@ -57210,10 +57275,11 @@ |
| 57210 | 57275 | /* Sync the database file to disk. */ |
| 57211 | 57276 | if( rc==SQLITE_OK ){ |
| 57212 | 57277 | rc = sqlite3PagerSync(pDestPager); |
| 57213 | 57278 | } |
| 57214 | 57279 | }else{ |
| 57280 | + sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 57215 | 57281 | rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
| 57216 | 57282 | } |
| 57217 | 57283 | |
| 57218 | 57284 | /* Finish committing the transaction to the destination database. */ |
| 57219 | 57285 | if( SQLITE_OK==rc |
| | @@ -63039,11 +63105,11 @@ |
| 63039 | 63105 | return 0; |
| 63040 | 63106 | } |
| 63041 | 63107 | if( zName ){ |
| 63042 | 63108 | for(i=0; i<p->nzVar; i++){ |
| 63043 | 63109 | const char *z = p->azVar[i]; |
| 63044 | | - if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){ |
| 63110 | + if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ |
| 63045 | 63111 | return i+1; |
| 63046 | 63112 | } |
| 63047 | 63113 | } |
| 63048 | 63114 | } |
| 63049 | 63115 | return 0; |
| | @@ -72691,16 +72757,16 @@ |
| 72691 | 72757 | const char *zTab, |
| 72692 | 72758 | const char *zDb |
| 72693 | 72759 | ){ |
| 72694 | 72760 | int n; |
| 72695 | 72761 | for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
| 72696 | | - if( zDb && sqlite3StrNICmp(zSpan, zDb, n)!=0 ){ |
| 72762 | + if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ |
| 72697 | 72763 | return 0; |
| 72698 | 72764 | } |
| 72699 | 72765 | zSpan += n+1; |
| 72700 | 72766 | for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
| 72701 | | - if( zTab && sqlite3StrNICmp(zSpan, zTab, n)!=0 ){ |
| 72767 | + if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){ |
| 72702 | 72768 | return 0; |
| 72703 | 72769 | } |
| 72704 | 72770 | zSpan += n+1; |
| 72705 | 72771 | if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ |
| 72706 | 72772 | return 0; |
| | @@ -74493,11 +74559,11 @@ |
| 74493 | 74559 | ** number as the prior appearance of the same name, or if the name |
| 74494 | 74560 | ** has never appeared before, reuse the same variable number |
| 74495 | 74561 | */ |
| 74496 | 74562 | ynVar i; |
| 74497 | 74563 | for(i=0; i<pParse->nzVar; i++){ |
| 74498 | | - if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){ |
| 74564 | + if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ |
| 74499 | 74565 | pExpr->iColumn = x = (ynVar)i+1; |
| 74500 | 74566 | break; |
| 74501 | 74567 | } |
| 74502 | 74568 | } |
| 74503 | 74569 | if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); |
| | @@ -79350,11 +79416,11 @@ |
| 79350 | 79416 | } |
| 79351 | 79417 | if( pTab->tnum==0 ){ |
| 79352 | 79418 | /* Do not gather statistics on views or virtual tables */ |
| 79353 | 79419 | return; |
| 79354 | 79420 | } |
| 79355 | | - if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 79421 | + if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 79356 | 79422 | /* Do not gather statistics on system tables */ |
| 79357 | 79423 | return; |
| 79358 | 79424 | } |
| 79359 | 79425 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 79360 | 79426 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| | @@ -79760,11 +79826,11 @@ |
| 79760 | 79826 | } |
| 79761 | 79827 | if( i==0 ) pTable->nRowEst = v; |
| 79762 | 79828 | if( pIndex==0 ) break; |
| 79763 | 79829 | pIndex->aiRowEst[i] = v; |
| 79764 | 79830 | if( *z==' ' ) z++; |
| 79765 | | - if( memcmp(z, "unordered", 10)==0 ){ |
| 79831 | + if( strcmp(z, "unordered")==0 ){ |
| 79766 | 79832 | pIndex->bUnordered = 1; |
| 79767 | 79833 | break; |
| 79768 | 79834 | } |
| 79769 | 79835 | } |
| 79770 | 79836 | return 0; |
| | @@ -83409,11 +83475,11 @@ |
| 83409 | 83475 | pDb = &db->aDb[iDb]; |
| 83410 | 83476 | |
| 83411 | 83477 | assert( pTab!=0 ); |
| 83412 | 83478 | assert( pParse->nErr==0 ); |
| 83413 | 83479 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 83414 | | - && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){ |
| 83480 | + && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){ |
| 83415 | 83481 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
| 83416 | 83482 | goto exit_create_index; |
| 83417 | 83483 | } |
| 83418 | 83484 | #ifndef SQLITE_OMIT_VIEW |
| 83419 | 83485 | if( pTab->pSelect ){ |
| | @@ -86787,10 +86853,61 @@ |
| 86787 | 86853 | sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
| 86788 | 86854 | break; |
| 86789 | 86855 | } |
| 86790 | 86856 | } |
| 86791 | 86857 | } |
| 86858 | + |
| 86859 | +/* |
| 86860 | +** The unicode() function. Return the integer unicode code-point value |
| 86861 | +** for the first character of the input string. |
| 86862 | +*/ |
| 86863 | +static void unicodeFunc( |
| 86864 | + sqlite3_context *context, |
| 86865 | + int argc, |
| 86866 | + sqlite3_value **argv |
| 86867 | +){ |
| 86868 | + const unsigned char *z = sqlite3_value_text(argv[0]); |
| 86869 | + (void)argc; |
| 86870 | + if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); |
| 86871 | +} |
| 86872 | + |
| 86873 | +/* |
| 86874 | +** The char() function takes zero or more arguments, each of which is |
| 86875 | +** an integer. It constructs a string where each character of the string |
| 86876 | +** is the unicode character for the corresponding integer argument. |
| 86877 | +*/ |
| 86878 | +static void charFunc( |
| 86879 | + sqlite3_context *context, |
| 86880 | + int argc, |
| 86881 | + sqlite3_value **argv |
| 86882 | +){ |
| 86883 | + unsigned char *z, *zOut; |
| 86884 | + int i; |
| 86885 | + zOut = z = sqlite3_malloc( argc*4 ); |
| 86886 | + if( z==0 ){ |
| 86887 | + sqlite3_result_error_nomem(context); |
| 86888 | + return; |
| 86889 | + } |
| 86890 | + for(i=0; i<argc; i++){ |
| 86891 | + sqlite3_int64 x; |
| 86892 | + unsigned c; |
| 86893 | + x = sqlite3_value_int64(argv[i]); |
| 86894 | + if( x<0 || x>0x10ffff ) x = 0xfffd; |
| 86895 | + c = (unsigned)(x & 0x1fffff); |
| 86896 | + if( c<=0xFFFF ){ |
| 86897 | + if( c>=0xd800 && c<=0xdfff ) c = 0xfffd; |
| 86898 | + *zOut++ = (u8)(c&0x00FF); |
| 86899 | + *zOut++ = (u8)((c>>8)&0x00FF); |
| 86900 | + }else{ |
| 86901 | + *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); |
| 86902 | + *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); |
| 86903 | + *zOut++ = (u8)(c&0x00FF); |
| 86904 | + *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); |
| 86905 | + } |
| 86906 | + } |
| 86907 | + sqlite3_result_text16le(context, (char*)z, (int)(zOut-z), sqlite3_free); |
| 86908 | +} |
| 86792 | 86909 | |
| 86793 | 86910 | /* |
| 86794 | 86911 | ** The hex() function. Interpret the argument as a blob. Return |
| 86795 | 86912 | ** a hexadecimal rendering as text. |
| 86796 | 86913 | */ |
| | @@ -87415,10 +87532,12 @@ |
| 87415 | 87532 | FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), |
| 87416 | 87533 | FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), |
| 87417 | 87534 | FUNCTION(instr, 2, 0, 0, instrFunc ), |
| 87418 | 87535 | FUNCTION(substr, 2, 0, 0, substrFunc ), |
| 87419 | 87536 | FUNCTION(substr, 3, 0, 0, substrFunc ), |
| 87537 | + FUNCTION(unicode, 1, 0, 0, unicodeFunc ), |
| 87538 | + FUNCTION(char, -1, 0, 0, charFunc ), |
| 87420 | 87539 | FUNCTION(abs, 1, 0, 0, absFunc ), |
| 87421 | 87540 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 87422 | 87541 | FUNCTION(round, 1, 0, 0, roundFunc ), |
| 87423 | 87542 | FUNCTION(round, 2, 0, 0, roundFunc ), |
| 87424 | 87543 | #endif |
| | @@ -91626,10 +91745,23 @@ |
| 91626 | 91745 | 0, |
| 91627 | 91746 | #endif |
| 91628 | 91747 | sqlite3_blob_reopen, |
| 91629 | 91748 | sqlite3_vtab_config, |
| 91630 | 91749 | sqlite3_vtab_on_conflict, |
| 91750 | + sqlite3_close_v2, |
| 91751 | + sqlite3_db_filename, |
| 91752 | + sqlite3_db_readonly, |
| 91753 | + sqlite3_db_release_memory, |
| 91754 | + sqlite3_errstr, |
| 91755 | + sqlite3_stmt_busy, |
| 91756 | + sqlite3_stmt_readonly, |
| 91757 | + sqlite3_stricmp, |
| 91758 | + sqlite3_uri_boolean, |
| 91759 | + sqlite3_uri_int64, |
| 91760 | + sqlite3_uri_parameter, |
| 91761 | + sqlite3_vsnprintf, |
| 91762 | + sqlite3_wal_checkpoint_v2 |
| 91631 | 91763 | }; |
| 91632 | 91764 | |
| 91633 | 91765 | /* |
| 91634 | 91766 | ** Attempt to load an SQLite extension library contained in the file |
| 91635 | 91767 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| | @@ -92866,10 +92998,11 @@ |
| 92866 | 92998 | Column *pCol; |
| 92867 | 92999 | Index *pPk; |
| 92868 | 93000 | for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){} |
| 92869 | 93001 | sqlite3VdbeSetNumCols(v, 6); |
| 92870 | 93002 | pParse->nMem = 6; |
| 93003 | + sqlite3CodeVerifySchema(pParse, iDb); |
| 92871 | 93004 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 92872 | 93005 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 92873 | 93006 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC); |
| 92874 | 93007 | sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC); |
| 92875 | 93008 | sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC); |
| | @@ -92911,10 +93044,11 @@ |
| 92911 | 93044 | if( pIdx ){ |
| 92912 | 93045 | int i; |
| 92913 | 93046 | pTab = pIdx->pTable; |
| 92914 | 93047 | sqlite3VdbeSetNumCols(v, 3); |
| 92915 | 93048 | pParse->nMem = 3; |
| 93049 | + sqlite3CodeVerifySchema(pParse, iDb); |
| 92916 | 93050 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC); |
| 92917 | 93051 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 92918 | 93052 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC); |
| 92919 | 93053 | for(i=0; i<pIdx->nColumn; i++){ |
| 92920 | 93054 | int cnum = pIdx->aiColumn[i]; |
| | @@ -92937,10 +93071,11 @@ |
| 92937 | 93071 | pIdx = pTab->pIndex; |
| 92938 | 93072 | if( pIdx ){ |
| 92939 | 93073 | int i = 0; |
| 92940 | 93074 | sqlite3VdbeSetNumCols(v, 3); |
| 92941 | 93075 | pParse->nMem = 3; |
| 93076 | + sqlite3CodeVerifySchema(pParse, iDb); |
| 92942 | 93077 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 92943 | 93078 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 92944 | 93079 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC); |
| 92945 | 93080 | while(pIdx){ |
| 92946 | 93081 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| | @@ -93000,10 +93135,11 @@ |
| 93000 | 93135 | pFK = pTab->pFKey; |
| 93001 | 93136 | if( pFK ){ |
| 93002 | 93137 | int i = 0; |
| 93003 | 93138 | sqlite3VdbeSetNumCols(v, 8); |
| 93004 | 93139 | pParse->nMem = 8; |
| 93140 | + sqlite3CodeVerifySchema(pParse, iDb); |
| 93005 | 93141 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC); |
| 93006 | 93142 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 93007 | 93143 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC); |
| 93008 | 93144 | sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC); |
| 93009 | 93145 | sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC); |
| | @@ -96914,11 +97050,12 @@ |
| 96914 | 97050 | if( op==TK_ALL ){ |
| 96915 | 97051 | regPrev = 0; |
| 96916 | 97052 | }else{ |
| 96917 | 97053 | int nExpr = p->pEList->nExpr; |
| 96918 | 97054 | assert( nOrderBy>=nExpr || db->mallocFailed ); |
| 96919 | | - regPrev = sqlite3GetTempRange(pParse, nExpr+1); |
| 97055 | + regPrev = pParse->nMem+1; |
| 97056 | + pParse->nMem += nExpr+1; |
| 96920 | 97057 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev); |
| 96921 | 97058 | pKeyDup = sqlite3DbMallocZero(db, |
| 96922 | 97059 | sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) ); |
| 96923 | 97060 | if( pKeyDup ){ |
| 96924 | 97061 | pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr]; |
| | @@ -97096,16 +97233,10 @@ |
| 97096 | 97233 | sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy, |
| 97097 | 97234 | (char*)pKeyMerge, P4_KEYINFO_HANDOFF); |
| 97098 | 97235 | sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE); |
| 97099 | 97236 | sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); |
| 97100 | 97237 | |
| 97101 | | - /* Release temporary registers |
| 97102 | | - */ |
| 97103 | | - if( regPrev ){ |
| 97104 | | - sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1); |
| 97105 | | - } |
| 97106 | | - |
| 97107 | 97238 | /* Jump to the this point in order to terminate the query. |
| 97108 | 97239 | */ |
| 97109 | 97240 | sqlite3VdbeResolveLabel(v, labelEnd); |
| 97110 | 97241 | |
| 97111 | 97242 | /* Set the number of output columns |
| | @@ -114105,11 +114236,11 @@ |
| 114105 | 114236 | } |
| 114106 | 114237 | } |
| 114107 | 114238 | sqlite3VtabRollback(db); |
| 114108 | 114239 | sqlite3EndBenignMalloc(); |
| 114109 | 114240 | |
| 114110 | | - if( db->flags&SQLITE_InternChanges ){ |
| 114241 | + if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){ |
| 114111 | 114242 | sqlite3ExpirePreparedStatements(db); |
| 114112 | 114243 | sqlite3ResetAllSchemasOfConnection(db); |
| 114113 | 114244 | } |
| 114114 | 114245 | |
| 114115 | 114246 | /* Any deferred constraint violations have now been resolved. */ |
| 114116 | 114247 | |