Fossil SCM

Update the built-in SQLite to the latest beta for 3.7.6.

drh 2011-04-04 03:29 trunk
Commit a74cfe0a14a203e77bb10d21fbc759b23f2c68cc
2 files changed +502 -394 +7 -3
+502 -394
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,9 +1,9 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
33
** version 3.7.6. By combining all the individual C code files into this
4
-** single large file, the entire code can be compiled as a one translation
4
+** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
99
**
@@ -650,11 +650,11 @@
650650
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651651
** [sqlite_version()] and [sqlite_source_id()].
652652
*/
653653
#define SQLITE_VERSION "3.7.6"
654654
#define SQLITE_VERSION_NUMBER 3007006
655
-#define SQLITE_SOURCE_ID "2011-03-24 01:34:03 b6e268fce12829f058f1dfa223731ec8479493f8"
655
+#define SQLITE_SOURCE_ID "2011-04-04 03:27:16 f8e98ab3062a6e56924a86e8f3204c30d0f3d906"
656656
657657
/*
658658
** CAPI3REF: Run-Time Library Version Numbers
659659
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660660
**
@@ -3521,11 +3521,13 @@
35213521
** UTF-16 string. ^The first parameter is the [prepared statement]
35223522
** that implements the [SELECT] statement. ^The second parameter is the
35233523
** column number. ^The leftmost column is number 0.
35243524
**
35253525
** ^The returned string pointer is valid until either the [prepared statement]
3526
-** is destroyed by [sqlite3_finalize()] or until the next call to
3526
+** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3527
+** reprepared by the first call to [sqlite3_step()] for a particular run
3528
+** or until the next call to
35273529
** sqlite3_column_name() or sqlite3_column_name16() on the same column.
35283530
**
35293531
** ^If sqlite3_malloc() fails during the processing of either routine
35303532
** (for example during a conversion from UTF-8 to UTF-16) then a
35313533
** NULL pointer is returned.
@@ -3547,11 +3549,13 @@
35473549
** ^The name of the database or table or column can be returned as
35483550
** either a UTF-8 or UTF-16 string. ^The _database_ routines return
35493551
** the database name, the _table_ routines return the table name, and
35503552
** the origin_ routines return the column name.
35513553
** ^The returned string is valid until the [prepared statement] is destroyed
3552
-** using [sqlite3_finalize()] or until the same information is requested
3554
+** using [sqlite3_finalize()] or until the statement is automatically
3555
+** reprepared by the first call to [sqlite3_step()] for a particular run
3556
+** or until the same information is requested
35533557
** again in a different encoding.
35543558
**
35553559
** ^The names returned are the original un-aliased names of the
35563560
** database, table, and column.
35573561
**
@@ -7648,22 +7652,10 @@
76487652
** Forward declarations of structure
76497653
*/
76507654
typedef struct Btree Btree;
76517655
typedef struct BtCursor BtCursor;
76527656
typedef struct BtShared BtShared;
7653
-typedef struct BtreeMutexArray BtreeMutexArray;
7654
-
7655
-/*
7656
-** This structure records all of the Btrees that need to hold
7657
-** a mutex before we enter sqlite3VdbeExec(). The Btrees are
7658
-** are placed in aBtree[] in order of aBtree[]->pBt. That way,
7659
-** we can always lock and unlock them all quickly.
7660
-*/
7661
-struct BtreeMutexArray {
7662
- int nMutex;
7663
- Btree *aBtree[SQLITE_MAX_ATTACHED+1];
7664
-};
76657657
76667658
76677659
SQLITE_PRIVATE int sqlite3BtreeOpen(
76687660
const char *zFilename, /* Name of database file to open */
76697661
sqlite3 *db, /* Associated database connection */
@@ -7696,11 +7688,11 @@
76967688
SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
76977689
SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
76987690
SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
76997691
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
77007692
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7701
-SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
7693
+SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
77027694
SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
77037695
SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
77047696
SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
77057697
SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
77067698
SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
@@ -7837,27 +7829,23 @@
78377829
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
78387830
SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
78397831
SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
78407832
SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
78417833
SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
7842
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
7843
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
7844
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
78457834
#ifndef NDEBUG
78467835
/* These routines are used inside assert() statements only. */
78477836
SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
78487837
SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
7838
+SQLITE_PRIVATE u32 sqlite3BtreeMutexCounter(Btree*);
78497839
#endif
78507840
#else
78517841
78527842
# define sqlite3BtreeLeave(X)
7843
+# define sqlite3BtreeMutexCounter(X) 0
78537844
# define sqlite3BtreeEnterCursor(X)
78547845
# define sqlite3BtreeLeaveCursor(X)
78557846
# define sqlite3BtreeLeaveAll(X)
7856
-# define sqlite3BtreeMutexArrayEnter(X)
7857
-# define sqlite3BtreeMutexArrayLeave(X)
7858
-# define sqlite3BtreeMutexArrayInsert(X,Y)
78597847
78607848
# define sqlite3BtreeHoldsMutex(X) 1
78617849
# define sqlite3BtreeHoldsAllMutexes(X) 1
78627850
#endif
78637851
@@ -10467,15 +10455,17 @@
1046710455
SubProgram *pProgram; /* Program implementing pTrigger/orconf */
1046810456
u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
1046910457
TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
1047010458
};
1047110459
10472
-/* Datatype for the bitmask of all attached databases */
10460
+/*
10461
+** The yDbMask datatype for the bitmask of all attached databases.
10462
+*/
1047310463
#if SQLITE_MAX_ATTACHED>30
10474
- typedef sqlite3_uint64 tAttachMask;
10464
+ typedef sqlite3_uint64 yDbMask;
1047510465
#else
10476
- typedef unsigned int tAttachMask;
10466
+ typedef unsigned int yDbMask;
1047710467
#endif
1047810468
1047910469
/*
1048010470
** An SQL parser context. A copy of this structure is passed through
1048110471
** the parser and down into all the parser action routine in order to
@@ -10522,12 +10512,12 @@
1052210512
u8 tempReg; /* iReg is a temp register that needs to be freed */
1052310513
int iLevel; /* Nesting level */
1052410514
int iReg; /* Reg with value of this column. 0 means none. */
1052510515
int lru; /* Least recently used entry has the smallest value */
1052610516
} aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
10527
- tAttachMask writeMask; /* Start a write transaction on these databases */
10528
- tAttachMask cookieMask; /* Bitmask of schema verified databases */
10517
+ yDbMask writeMask; /* Start a write transaction on these databases */
10518
+ yDbMask cookieMask; /* Bitmask of schema verified databases */
1052910519
u8 isMultiWrite; /* True if statement may affect/insert multiple rows */
1053010520
u8 mayAbort; /* True if statement may throw an ABORT exception */
1053110521
int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
1053210522
int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
1053310523
#ifndef SQLITE_OMIT_SHARED_CACHE
@@ -12488,14 +12478,14 @@
1248812478
u8 inVtabMethod; /* See comments above */
1248912479
u8 usesStmtJournal; /* True if uses a statement journal */
1249012480
u8 readOnly; /* True for read-only statements */
1249112481
u8 isPrepareV2; /* True if prepared with prepare_v2() */
1249212482
int nChange; /* Number of db changes made since last reset */
12493
- tAttachMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
12483
+ yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
12484
+ u32 iMutexCounter; /* Mutex counter upon sqlite3VdbeEnter() */
1249412485
int iStatement; /* Statement number (or 0 if has not opened stmt) */
1249512486
int aCounter[3]; /* Counters used by sqlite3_stmt_status() */
12496
- BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
1249712487
#ifndef SQLITE_OMIT_TRACE
1249812488
i64 startTime; /* Time when query started - used for profiling */
1249912489
#endif
1250012490
i64 nFkConstraint; /* Number of imm. FK constraints this VM */
1250112491
i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -12573,10 +12563,13 @@
1257312563
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
1257412564
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
1257512565
SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
1257612566
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
1257712567
SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12568
+SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12569
+SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12570
+SQLITE_PRIVATE void sqlite3VdbeMutexResync(Vdbe*);
1257812571
1257912572
#ifdef SQLITE_DEBUG
1258012573
SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
1258112574
#endif
1258212575
@@ -12584,16 +12577,10 @@
1258412577
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
1258512578
#else
1258612579
# define sqlite3VdbeCheckFk(p,i) 0
1258712580
#endif
1258812581
12589
-#ifndef SQLITE_OMIT_SHARED_CACHE
12590
-SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p);
12591
-#else
12592
-# define sqlite3VdbeMutexArrayEnter(p)
12593
-#endif
12594
-
1259512582
SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
1259612583
#ifdef SQLITE_DEBUG
1259712584
SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
1259812585
SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
1259912586
#endif
@@ -19794,11 +19781,11 @@
1979419781
}
1979519782
SQLITE_PRIVATE int sqlite3Utf8Read(
1979619783
const unsigned char *zIn, /* First byte of UTF-8 character */
1979719784
const unsigned char **pzNext /* Write first byte past UTF-8 char here */
1979819785
){
19799
- int c;
19786
+ unsigned int c;
1980019787
1980119788
/* Same as READ_UTF8() above but without the zTerm parameter.
1980219789
** For this routine, we assume the UTF8 string is always zero-terminated.
1980319790
*/
1980419791
c = *(zIn++);
@@ -20037,19 +20024,19 @@
2003720024
** Translate UTF-8 to UTF-8.
2003820025
**
2003920026
** This has the effect of making sure that the string is well-formed
2004020027
** UTF-8. Miscoded characters are removed.
2004120028
**
20042
-** The translation is done in-place (since it is impossible for the
20043
-** correct UTF-8 encoding to be longer than a malformed encoding).
20029
+** The translation is done in-place and aborted if the output
20030
+** overruns the input.
2004420031
*/
2004520032
SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
2004620033
unsigned char *zOut = zIn;
2004720034
unsigned char *zStart = zIn;
2004820035
u32 c;
2004920036
20050
- while( zIn[0] ){
20037
+ while( zIn[0] && zOut<=zIn ){
2005120038
c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
2005220039
if( c!=0xfffd ){
2005320040
WRITE_UTF8(zOut, c);
2005420041
}
2005520042
}
@@ -23750,11 +23737,11 @@
2375023737
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
2375123738
{ "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
2375223739
#else
2375323740
{ "fallocate", (sqlite3_syscall_ptr)0, 0 },
2375423741
#endif
23755
-#define osFallocate ((int(*)(int,off_t,off_t)aSyscall[15].pCurrent)
23742
+#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
2375623743
2375723744
}; /* End of the overrideable system calls */
2375823745
2375923746
/*
2376023747
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23773,14 +23760,14 @@
2377323760
UNUSED_PARAMETER(pNotUsed);
2377423761
if( zName==0 ){
2377523762
/* If no zName is given, restore all system calls to their default
2377623763
** settings and return NULL
2377723764
*/
23765
+ rc = SQLITE_OK;
2377823766
for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
2377923767
if( aSyscall[i].pDefault ){
2378023768
aSyscall[i].pCurrent = aSyscall[i].pDefault;
23781
- rc = SQLITE_OK;
2378223769
}
2378323770
}
2378423771
}else{
2378523772
/* If zName is specified, operate on only the one system call
2378623773
** specified.
@@ -23823,22 +23810,20 @@
2382323810
** then return the name of the first system call. Return NULL if zName
2382423811
** is the last system call or if zName is not the name of a valid
2382523812
** system call.
2382623813
*/
2382723814
static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
23828
- unsigned int i;
23815
+ int i = -1;
2382923816
2383023817
UNUSED_PARAMETER(p);
23831
- if( zName==0 ){
23832
- i = -1;
23833
- }else{
23834
- for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0])-1; i++){
23835
- if( strcmp(zName, aSyscall[0].zName)==0 ) break;
23818
+ if( zName ){
23819
+ for(i=0; i<ArraySize(aSyscall)-1; i++){
23820
+ if( strcmp(zName, aSyscall[i].zName)==0 ) break;
2383623821
}
2383723822
}
23838
- for(i++; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
23839
- if( aSyscall[0].pCurrent!=0 ) return aSyscall[0].zName;
23823
+ for(i++; i<ArraySize(aSyscall); i++){
23824
+ if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
2384023825
}
2384123826
return 0;
2384223827
}
2384323828
2384423829
/*
@@ -23974,13 +23959,26 @@
2397423959
** Errors during initialization of locks, or file system support for locks,
2397523960
** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
2397623961
*/
2397723962
static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
2397823963
switch (posixError) {
23964
+#if 0
23965
+ /* At one point this code was not commented out. In theory, this branch
23966
+ ** should never be hit, as this function should only be called after
23967
+ ** a locking-related function (i.e. fcntl()) has returned non-zero with
23968
+ ** the value of errno as the first argument. Since a system call has failed,
23969
+ ** errno should be non-zero.
23970
+ **
23971
+ ** Despite this, if errno really is zero, we still don't want to return
23972
+ ** SQLITE_OK. The system call failed, and *some* SQLite error should be
23973
+ ** propagated back to the caller. Commenting this branch out means errno==0
23974
+ ** will be handled by the "default:" case below.
23975
+ */
2397923976
case 0:
2398023977
return SQLITE_OK;
23981
-
23978
+#endif
23979
+
2398223980
case EAGAIN:
2398323981
case ETIMEDOUT:
2398423982
case EBUSY:
2398523983
case EINTR:
2398623984
case ENOLCK:
@@ -23998,12 +23996,19 @@
2399823996
}
2399923997
/* else fall through */
2400023998
case EPERM:
2400123999
return SQLITE_PERM;
2400224000
24001
+ /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24002
+ ** this module never makes such a call. And the code in SQLite itself
24003
+ ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24004
+ ** this case is also commented out. If the system does set errno to EDEADLK,
24005
+ ** the default SQLITE_IOERR_XXX code will be returned. */
24006
+#if 0
2400324007
case EDEADLK:
2400424008
return SQLITE_IOERR_BLOCKED;
24009
+#endif
2400524010
2400624011
#if EOPNOTSUPP!=ENOTSUP
2400724012
case EOPNOTSUPP:
2400824013
/* something went terribly awry, unless during file system support
2400924014
* introspection, in which it actually means what it says */
@@ -24418,11 +24423,11 @@
2441824423
** when this function is called.
2441924424
*/
2442024425
static void releaseInodeInfo(unixFile *pFile){
2442124426
unixInodeInfo *pInode = pFile->pInode;
2442224427
assert( unixMutexHeld() );
24423
- if( pInode ){
24428
+ if( ALWAYS(pInode) ){
2442424429
pInode->nRef--;
2442524430
if( pInode->nRef==0 ){
2442624431
assert( pInode->pShmNode==0 );
2442724432
closePendingFds(pFile);
2442824433
if( pInode->pPrev ){
@@ -24560,14 +24565,13 @@
2456024565
struct flock lock;
2456124566
lock.l_whence = SEEK_SET;
2456224567
lock.l_start = RESERVED_BYTE;
2456324568
lock.l_len = 1;
2456424569
lock.l_type = F_WRLCK;
24565
- if (-1 == osFcntl(pFile->h, F_GETLK, &lock)) {
24566
- int tErrno = errno;
24567
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24568
- pFile->lastErrno = tErrno;
24570
+ if( osFcntl(pFile->h, F_GETLK, &lock) ){
24571
+ rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
24572
+ pFile->lastErrno = errno;
2456924573
} else if( lock.l_type!=F_UNLCK ){
2457024574
reserved = 1;
2457124575
}
2457224576
}
2457324577
#endif
@@ -24592,10 +24596,13 @@
2459224596
** operating system does not participate.
2459324597
**
2459424598
** This function is a pass-through to fcntl(F_SETLK) if pFile is using
2459524599
** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
2459624600
** and is read-only.
24601
+**
24602
+** Zero is returned if the call completes successfully, or -1 if a call
24603
+** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
2459724604
*/
2459824605
static int unixFileLock(unixFile *pFile, struct flock *pLock){
2459924606
int rc;
2460024607
unixInodeInfo *pInode = pFile->pInode;
2460124608
assert( unixMutexHeld() );
@@ -24688,11 +24695,10 @@
2468824695
*/
2468924696
int rc = SQLITE_OK;
2469024697
unixFile *pFile = (unixFile*)id;
2469124698
unixInodeInfo *pInode = pFile->pInode;
2469224699
struct flock lock;
24693
- int s = 0;
2469424700
int tErrno = 0;
2469524701
2469624702
assert( pFile );
2469724703
OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
2469824704
azFileLock(eFileLock), azFileLock(pFile->eFileLock),
@@ -24757,15 +24763,14 @@
2475724763
if( eFileLock==SHARED_LOCK
2475824764
|| (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
2475924765
){
2476024766
lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
2476124767
lock.l_start = PENDING_BYTE;
24762
- s = unixFileLock(pFile, &lock);
24763
- if( s==(-1) ){
24768
+ if( unixFileLock(pFile, &lock) ){
2476424769
tErrno = errno;
2476524770
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24766
- if( IS_LOCK_ERROR(rc) ){
24771
+ if( rc!=SQLITE_BUSY ){
2476724772
pFile->lastErrno = tErrno;
2476824773
}
2476924774
goto end_lock;
2477024775
}
2477124776
}
@@ -24775,37 +24780,35 @@
2477524780
** operating system calls for the specified lock.
2477624781
*/
2477724782
if( eFileLock==SHARED_LOCK ){
2477824783
assert( pInode->nShared==0 );
2477924784
assert( pInode->eFileLock==0 );
24785
+ assert( rc==SQLITE_OK );
2478024786
2478124787
/* Now get the read-lock */
2478224788
lock.l_start = SHARED_FIRST;
2478324789
lock.l_len = SHARED_SIZE;
24784
- if( (s = unixFileLock(pFile, &lock))==(-1) ){
24790
+ if( unixFileLock(pFile, &lock) ){
2478524791
tErrno = errno;
24792
+ rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2478624793
}
24794
+
2478724795
/* Drop the temporary PENDING lock */
2478824796
lock.l_start = PENDING_BYTE;
2478924797
lock.l_len = 1L;
2479024798
lock.l_type = F_UNLCK;
24791
- if( unixFileLock(pFile, &lock)!=0 ){
24792
- if( s != -1 ){
24793
- /* This could happen with a network mount */
24794
- tErrno = errno;
24795
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24796
- if( IS_LOCK_ERROR(rc) ){
24797
- pFile->lastErrno = tErrno;
24798
- }
24799
- goto end_lock;
24800
- }
24801
- }
24802
- if( s==(-1) ){
24803
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24804
- if( IS_LOCK_ERROR(rc) ){
24805
- pFile->lastErrno = tErrno;
24806
- }
24799
+ if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
24800
+ /* This could happen with a network mount */
24801
+ tErrno = errno;
24802
+ rc = SQLITE_IOERR_UNLOCK;
24803
+ }
24804
+
24805
+ if( rc ){
24806
+ if( rc!=SQLITE_BUSY ){
24807
+ pFile->lastErrno = tErrno;
24808
+ }
24809
+ goto end_lock;
2480724810
}else{
2480824811
pFile->eFileLock = SHARED_LOCK;
2480924812
pInode->nLock++;
2481024813
pInode->nShared = 1;
2481124814
}
@@ -24818,26 +24821,24 @@
2481824821
** assumed that there is a SHARED or greater lock on the file
2481924822
** already.
2482024823
*/
2482124824
assert( 0!=pFile->eFileLock );
2482224825
lock.l_type = F_WRLCK;
24823
- switch( eFileLock ){
24824
- case RESERVED_LOCK:
24825
- lock.l_start = RESERVED_BYTE;
24826
- break;
24827
- case EXCLUSIVE_LOCK:
24828
- lock.l_start = SHARED_FIRST;
24829
- lock.l_len = SHARED_SIZE;
24830
- break;
24831
- default:
24832
- assert(0);
24833
- }
24834
- s = unixFileLock(pFile, &lock);
24835
- if( s==(-1) ){
24826
+
24827
+ assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
24828
+ if( eFileLock==RESERVED_LOCK ){
24829
+ lock.l_start = RESERVED_BYTE;
24830
+ lock.l_len = 1L;
24831
+ }else{
24832
+ lock.l_start = SHARED_FIRST;
24833
+ lock.l_len = SHARED_SIZE;
24834
+ }
24835
+
24836
+ if( unixFileLock(pFile, &lock) ){
2483624837
tErrno = errno;
2483724838
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24838
- if( IS_LOCK_ERROR(rc) ){
24839
+ if( rc!=SQLITE_BUSY ){
2483924840
pFile->lastErrno = tErrno;
2484024841
}
2484124842
}
2484224843
}
2484324844
@@ -24904,11 +24905,10 @@
2490424905
unixFile *pFile = (unixFile*)id;
2490524906
unixInodeInfo *pInode;
2490624907
struct flock lock;
2490724908
int rc = SQLITE_OK;
2490824909
int h;
24909
- int tErrno; /* Error code from system call errors */
2491024910
2491124911
assert( pFile );
2491224912
OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
2491324913
pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
2491424914
getpid()));
@@ -24959,19 +24959,20 @@
2495924959
(void)handleNFSUnlock;
2496024960
assert( handleNFSUnlock==0 );
2496124961
#endif
2496224962
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2496324963
if( handleNFSUnlock ){
24964
+ int tErrno; /* Error code from system call errors */
2496424965
off_t divSize = SHARED_SIZE - 1;
2496524966
2496624967
lock.l_type = F_UNLCK;
2496724968
lock.l_whence = SEEK_SET;
2496824969
lock.l_start = SHARED_FIRST;
2496924970
lock.l_len = divSize;
24970
- if( unixFileLock(pFile,, &lock)==(-1) ){
24971
+ if( unixFileLock(pFile, &lock)==(-1) ){
2497124972
tErrno = errno;
24972
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24973
+ rc = SQLITE_IOERR_UNLOCK;
2497324974
if( IS_LOCK_ERROR(rc) ){
2497424975
pFile->lastErrno = tErrno;
2497524976
}
2497624977
goto end_unlock;
2497724978
}
@@ -24991,11 +24992,11 @@
2499124992
lock.l_whence = SEEK_SET;
2499224993
lock.l_start = SHARED_FIRST+divSize;
2499324994
lock.l_len = SHARED_SIZE-divSize;
2499424995
if( unixFileLock(pFile, &lock)==(-1) ){
2499524996
tErrno = errno;
24996
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24997
+ rc = SQLITE_IOERR_UNLOCK;
2499724998
if( IS_LOCK_ERROR(rc) ){
2499824999
pFile->lastErrno = tErrno;
2499925000
}
2500025001
goto end_unlock;
2500125002
}
@@ -25004,32 +25005,32 @@
2500425005
{
2500525006
lock.l_type = F_RDLCK;
2500625007
lock.l_whence = SEEK_SET;
2500725008
lock.l_start = SHARED_FIRST;
2500825009
lock.l_len = SHARED_SIZE;
25009
- if( unixFileLock(pFile, &lock)==(-1) ){
25010
- tErrno = errno;
25011
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
25012
- if( IS_LOCK_ERROR(rc) ){
25013
- pFile->lastErrno = tErrno;
25014
- }
25010
+ if( unixFileLock(pFile, &lock) ){
25011
+ /* In theory, the call to unixFileLock() cannot fail because another
25012
+ ** process is holding an incompatible lock. If it does, this
25013
+ ** indicates that the other process is not following the locking
25014
+ ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
25015
+ ** SQLITE_BUSY would confuse the upper layer (in practice it causes
25016
+ ** an assert to fail). */
25017
+ rc = SQLITE_IOERR_RDLOCK;
25018
+ pFile->lastErrno = errno;
2501525019
goto end_unlock;
2501625020
}
2501725021
}
2501825022
}
2501925023
lock.l_type = F_UNLCK;
2502025024
lock.l_whence = SEEK_SET;
2502125025
lock.l_start = PENDING_BYTE;
2502225026
lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
25023
- if( unixFileLock(pFile, &lock)!=(-1) ){
25027
+ if( unixFileLock(pFile, &lock)==0 ){
2502425028
pInode->eFileLock = SHARED_LOCK;
2502525029
}else{
25026
- tErrno = errno;
25027
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25028
- if( IS_LOCK_ERROR(rc) ){
25029
- pFile->lastErrno = tErrno;
25030
- }
25030
+ rc = SQLITE_IOERR_UNLOCK;
25031
+ pFile->lastErrno = errno;
2503125032
goto end_unlock;
2503225033
}
2503325034
}
2503425035
if( eFileLock==NO_LOCK ){
2503525036
/* Decrement the shared lock counter. Release the lock using an
@@ -25042,18 +25043,15 @@
2504225043
lock.l_whence = SEEK_SET;
2504325044
lock.l_start = lock.l_len = 0L;
2504425045
SimulateIOErrorBenign(1);
2504525046
SimulateIOError( h=(-1) )
2504625047
SimulateIOErrorBenign(0);
25047
- if( unixFileLock(pFile, &lock)!=(-1) ){
25048
+ if( unixFileLock(pFile, &lock)==0 ){
2504825049
pInode->eFileLock = NO_LOCK;
2504925050
}else{
25050
- tErrno = errno;
25051
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25052
- if( IS_LOCK_ERROR(rc) ){
25053
- pFile->lastErrno = tErrno;
25054
- }
25051
+ rc = SQLITE_IOERR_UNLOCK;
25052
+ pFile->lastErrno = errno;
2505525053
pInode->eFileLock = NO_LOCK;
2505625054
pFile->eFileLock = NO_LOCK;
2505725055
}
2505825056
}
2505925057
@@ -25095,59 +25093,58 @@
2509525093
** even on VxWorks. A mutex will be acquired on VxWorks by the
2509625094
** vxworksReleaseFileId() routine.
2509725095
*/
2509825096
static int closeUnixFile(sqlite3_file *id){
2509925097
unixFile *pFile = (unixFile*)id;
25100
- if( pFile ){
25101
- if( pFile->dirfd>=0 ){
25102
- robust_close(pFile, pFile->dirfd, __LINE__);
25103
- pFile->dirfd=-1;
25104
- }
25105
- if( pFile->h>=0 ){
25106
- robust_close(pFile, pFile->h, __LINE__);
25107
- pFile->h = -1;
25108
- }
25098
+ if( pFile->dirfd>=0 ){
25099
+ robust_close(pFile, pFile->dirfd, __LINE__);
25100
+ pFile->dirfd=-1;
25101
+ }
25102
+ if( pFile->h>=0 ){
25103
+ robust_close(pFile, pFile->h, __LINE__);
25104
+ pFile->h = -1;
25105
+ }
2510925106
#if OS_VXWORKS
25110
- if( pFile->pId ){
25111
- if( pFile->isDelete ){
25112
- unlink(pFile->pId->zCanonicalName);
25113
- }
25114
- vxworksReleaseFileId(pFile->pId);
25115
- pFile->pId = 0;
25116
- }
25117
-#endif
25118
- OSTRACE(("CLOSE %-3d\n", pFile->h));
25119
- OpenCounter(-1);
25120
- sqlite3_free(pFile->pUnused);
25121
- memset(pFile, 0, sizeof(unixFile));
25122
- }
25107
+ if( pFile->pId ){
25108
+ if( pFile->isDelete ){
25109
+ unlink(pFile->pId->zCanonicalName);
25110
+ }
25111
+ vxworksReleaseFileId(pFile->pId);
25112
+ pFile->pId = 0;
25113
+ }
25114
+#endif
25115
+ OSTRACE(("CLOSE %-3d\n", pFile->h));
25116
+ OpenCounter(-1);
25117
+ sqlite3_free(pFile->pUnused);
25118
+ memset(pFile, 0, sizeof(unixFile));
2512325119
return SQLITE_OK;
2512425120
}
2512525121
2512625122
/*
2512725123
** Close a file.
2512825124
*/
2512925125
static int unixClose(sqlite3_file *id){
2513025126
int rc = SQLITE_OK;
25131
- if( id ){
25132
- unixFile *pFile = (unixFile *)id;
25133
- unixUnlock(id, NO_LOCK);
25134
- unixEnterMutex();
25135
- assert( pFile->pInode==0 || pFile->pInode->nLock>0
25136
- || pFile->pInode->bProcessLock==0 );
25137
- if( pFile->pInode && pFile->pInode->nLock ){
25138
- /* If there are outstanding locks, do not actually close the file just
25139
- ** yet because that would clear those locks. Instead, add the file
25140
- ** descriptor to pInode->pUnused list. It will be automatically closed
25141
- ** when the last lock is cleared.
25142
- */
25143
- setPendingFd(pFile);
25144
- }
25145
- releaseInodeInfo(pFile);
25146
- rc = closeUnixFile(id);
25147
- unixLeaveMutex();
25148
- }
25127
+ unixFile *pFile = (unixFile *)id;
25128
+ unixUnlock(id, NO_LOCK);
25129
+ unixEnterMutex();
25130
+
25131
+ /* unixFile.pInode is always valid here. Otherwise, a different close
25132
+ ** routine (e.g. nolockClose()) would be called instead.
25133
+ */
25134
+ assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
25135
+ if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
25136
+ /* If there are outstanding locks, do not actually close the file just
25137
+ ** yet because that would clear those locks. Instead, add the file
25138
+ ** descriptor to pInode->pUnused list. It will be automatically closed
25139
+ ** when the last lock is cleared.
25140
+ */
25141
+ setPendingFd(pFile);
25142
+ }
25143
+ releaseInodeInfo(pFile);
25144
+ rc = closeUnixFile(id);
25145
+ unixLeaveMutex();
2514925146
return rc;
2515025147
}
2515125148
2515225149
/************** End of the posix advisory lock implementation *****************
2515325150
******************************************************************************/
@@ -25358,11 +25355,11 @@
2535825355
assert( eFileLock==NO_LOCK );
2535925356
if( unlink(zLockFile) ){
2536025357
int rc = 0;
2536125358
int tErrno = errno;
2536225359
if( ENOENT != tErrno ){
25363
- rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25360
+ rc = SQLITE_IOERR_UNLOCK;
2536425361
}
2536525362
if( IS_LOCK_ERROR(rc) ){
2536625363
pFile->lastErrno = tErrno;
2536725364
}
2536825365
return rc;
@@ -25446,11 +25443,11 @@
2544625443
/* got the lock, unlock it */
2544725444
lrc = robust_flock(pFile->h, LOCK_UN);
2544825445
if ( lrc ) {
2544925446
int tErrno = errno;
2545025447
/* unlock failed with an error */
25451
- lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25448
+ lrc = SQLITE_IOERR_UNLOCK;
2545225449
if( IS_LOCK_ERROR(lrc) ){
2545325450
pFile->lastErrno = tErrno;
2545425451
rc = lrc;
2545525452
}
2545625453
}
@@ -25568,25 +25565,16 @@
2556825565
pFile->eFileLock = eFileLock;
2556925566
return SQLITE_OK;
2557025567
}
2557125568
2557225569
/* no, really, unlock. */
25573
- int rc = robust_flock(pFile->h, LOCK_UN);
25574
- if (rc) {
25575
- int r, tErrno = errno;
25576
- r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25577
- if( IS_LOCK_ERROR(r) ){
25578
- pFile->lastErrno = tErrno;
25579
- }
25570
+ if( robust_flock(pFile->h, LOCK_UN) ){
2558025571
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25581
- if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
25582
- r = SQLITE_BUSY;
25583
- }
25572
+ return SQLITE_OK;
2558425573
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25585
-
25586
- return r;
25587
- } else {
25574
+ return SQLITE_IOERR_UNLOCK;
25575
+ }else{
2558825576
pFile->eFileLock = NO_LOCK;
2558925577
return SQLITE_OK;
2559025578
}
2559125579
}
2559225580
@@ -26388,10 +26376,11 @@
2638826376
do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
2638926377
#elif defined(USE_PREAD64)
2639026378
do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
2639126379
#else
2639226380
newOffset = lseek(id->h, offset, SEEK_SET);
26381
+ SimulateIOError( newOffset-- );
2639326382
if( newOffset!=offset ){
2639426383
if( newOffset == -1 ){
2639526384
((unixFile*)id)->lastErrno = errno;
2639626385
}else{
2639726386
((unixFile*)id)->lastErrno = 0;
@@ -26756,16 +26745,20 @@
2675626745
2675726746
if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
2675826747
2675926748
nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
2676026749
if( nSize>(i64)buf.st_size ){
26750
+
2676126751
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
26762
- int rc;
26752
+ /* The code below is handling the return value of osFallocate()
26753
+ ** correctly. posix_fallocate() is defined to "returns zero on success,
26754
+ ** or an error number on failure". See the manpage for details. */
26755
+ int err;
2676326756
do{
26764
- rc = osFallocate(pFile->.h, buf.st_size, nSize-buf.st_size;
26765
- }while( rc<0 && errno=EINTR );
26766
- if( rc ) return SQLITE_IOERR_WRITE;
26757
+ err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
26758
+ }while( err==EINTR );
26759
+ if( err ) return SQLITE_IOERR_WRITE;
2676726760
#else
2676826761
/* If the OS does not have posix_fallocate(), fake it. First use
2676926762
** ftruncate() to set the file size, then write a single byte to
2677026763
** the last byte in each block within the extended region. This
2677126764
** is the same technique used by glibc to implement posix_fallocate()
@@ -29114,11 +29107,13 @@
2911429107
rc = SQLITE_NOMEM;
2911529108
goto end_create_proxy;
2911629109
}
2911729110
memset(pNew, 0, sizeof(unixFile));
2911829111
pNew->openFlags = openFlags;
29112
+ memset(&dummyVfs, 0, sizeof(dummyVfs));
2911929113
dummyVfs.pAppData = (void*)&autolockIoFinder;
29114
+ dummyVfs.zName = "dummy";
2912029115
pUnused->fd = fd;
2912129116
pUnused->flags = openFlags;
2912229117
pNew->pUnused = pUnused;
2912329118
2912429119
rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
@@ -45633,11 +45628,11 @@
4563345628
** the BtShared object.
4563445629
**
4563545630
** All fields in this structure are accessed under sqlite3.mutex.
4563645631
** The pBt pointer itself may not be changed while there exists cursors
4563745632
** in the referenced BtShared that point back to this Btree since those
45638
-** cursors have to do go through this Btree to find their BtShared and
45633
+** cursors have to go through this Btree to find their BtShared and
4563945634
** they often do so without holding sqlite3.mutex.
4564045635
*/
4564145636
struct Btree {
4564245637
sqlite3 *db; /* The database connection holding this btree */
4564345638
BtShared *pBt; /* Sharable content of this btree */
@@ -45723,19 +45718,20 @@
4572345718
u32 usableSize; /* Number of usable bytes on each page */
4572445719
int nTransaction; /* Number of open transactions (read + write) */
4572545720
u32 nPage; /* Number of pages in the database */
4572645721
void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
4572745722
void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
45728
- sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
45723
+ sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
4572945724
Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
4573045725
#ifndef SQLITE_OMIT_SHARED_CACHE
4573145726
int nRef; /* Number of references to this structure */
4573245727
BtShared *pNext; /* Next on a list of sharable BtShared structs */
4573345728
BtLock *pLock; /* List of locks held on this shared-btree struct */
4573445729
Btree *pWriter; /* Btree with currently open write transaction */
4573545730
u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */
4573645731
u8 isPending; /* If waiting for read-locks to clear */
45732
+ u16 iMutexCounter; /* The number of mutex_leave(mutex) calls */
4573745733
#endif
4573845734
u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
4573945735
};
4574045736
4574145737
/*
@@ -45964,18 +45960,37 @@
4596445960
/*
4596545961
** Release the BtShared mutex associated with B-Tree handle p and
4596645962
** clear the p->locked boolean.
4596745963
*/
4596845964
static void unlockBtreeMutex(Btree *p){
45965
+ BtShared *pBt = p->pBt;
4596945966
assert( p->locked==1 );
45970
- assert( sqlite3_mutex_held(p->pBt->mutex) );
45967
+ assert( sqlite3_mutex_held(pBt->mutex) );
4597145968
assert( sqlite3_mutex_held(p->db->mutex) );
45972
- assert( p->db==p->pBt->db );
45969
+ assert( p->db==pBt->db );
4597345970
45974
- sqlite3_mutex_leave(p->pBt->mutex);
45971
+ pBt->iMutexCounter++;
45972
+ sqlite3_mutex_leave(pBt->mutex);
4597545973
p->locked = 0;
4597645974
}
45975
+
45976
+#ifdef SQLITE_DEBUG
45977
+/*
45978
+** Return the number of times that the mutex has been exited for
45979
+** the given btree.
45980
+**
45981
+** This is a small circular counter that wraps around to zero on
45982
+** overflow. It is used only for sanity checking - to verify that
45983
+** mutexes are held continously by asserting that the value of
45984
+** this counter at the beginning of a region is the same as at
45985
+** the end.
45986
+*/
45987
+SQLITE_PRIVATE u32 sqlite3BtreeMutexCounter(Btree *p){
45988
+ assert( p->locked==1 || p->sharable==0 );
45989
+ return p->pBt->iMutexCounter;
45990
+}
45991
+#endif
4597745992
4597845993
/*
4597945994
** Enter a mutex on the given BTree object.
4598045995
**
4598145996
** If the object is not sharable, then no mutex is ever required
@@ -46016,10 +46031,28 @@
4601646031
assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
4601746032
4601846033
if( !p->sharable ) return;
4601946034
p->wantToLock++;
4602046035
if( p->locked ) return;
46036
+
46037
+ /* Increment the mutex counter on all locked btrees in the same
46038
+ ** database connection. This simulates the unlocking that would
46039
+ ** occur on a worst-case mutex dead-lock avoidance scenario.
46040
+ */
46041
+#ifdef SQLITE_DEBUG
46042
+ {
46043
+ int ii;
46044
+ sqlite3 *db = p->db;
46045
+ Btree *pOther;
46046
+ for(ii=0; ii<db->nDb; ii++){
46047
+ if( ii==1 ) continue;
46048
+ pOther = db->aDb[ii].pBt;
46049
+ if( pOther==0 || pOther->sharable==0 || pOther->locked==0 ) continue;
46050
+ pOther->pBt->iMutexCounter++;
46051
+ }
46052
+ }
46053
+#endif
4602146054
4602246055
/* In most cases, we should be able to acquire the lock we
4602346056
** want without having to go throught the ascending lock
4602446057
** procedure that follows. Just be sure not to block.
4602546058
*/
@@ -46120,11 +46153,11 @@
4612046153
if( p && p->sharable ){
4612146154
p->wantToLock++;
4612246155
if( !p->locked ){
4612346156
assert( p->wantToLock==1 );
4612446157
while( p->pPrev ) p = p->pPrev;
46125
- /* Reason for ALWAYS: There must be at least on unlocked Btree in
46158
+ /* Reason for ALWAYS: There must be at least one unlocked Btree in
4612646159
** the chain. Otherwise the !p->locked test above would have failed */
4612746160
while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
4612846161
for(pLater = p->pNext; pLater; pLater=pLater->pNext){
4612946162
if( pLater->locked ){
4613046163
unlockBtreeMutex(pLater);
@@ -46176,101 +46209,21 @@
4617646209
}
4617746210
return 1;
4617846211
}
4617946212
#endif /* NDEBUG */
4618046213
46181
-/*
46182
-** Add a new Btree pointer to a BtreeMutexArray.
46183
-** if the pointer can possibly be shared with
46184
-** another database connection.
46185
-**
46186
-** The pointers are kept in sorted order by pBtree->pBt. That
46187
-** way when we go to enter all the mutexes, we can enter them
46188
-** in order without every having to backup and retry and without
46189
-** worrying about deadlock.
46190
-**
46191
-** The number of shared btrees will always be small (usually 0 or 1)
46192
-** so an insertion sort is an adequate algorithm here.
46193
-*/
46194
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
46195
- int i, j;
46196
- BtShared *pBt;
46197
- if( pBtree==0 || pBtree->sharable==0 ) return;
46198
-#ifndef NDEBUG
46199
- {
46200
- for(i=0; i<pArray->nMutex; i++){
46201
- assert( pArray->aBtree[i]!=pBtree );
46202
- }
46203
- }
46204
-#endif
46205
- assert( pArray->nMutex>=0 );
46206
- assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
46207
- pBt = pBtree->pBt;
46208
- for(i=0; i<pArray->nMutex; i++){
46209
- assert( pArray->aBtree[i]!=pBtree );
46210
- if( pArray->aBtree[i]->pBt>pBt ){
46211
- for(j=pArray->nMutex; j>i; j--){
46212
- pArray->aBtree[j] = pArray->aBtree[j-1];
46213
- }
46214
- pArray->aBtree[i] = pBtree;
46215
- pArray->nMutex++;
46216
- return;
46217
- }
46218
- }
46219
- pArray->aBtree[pArray->nMutex++] = pBtree;
46220
-}
46221
-
46222
-/*
46223
-** Enter the mutex of every btree in the array. This routine is
46224
-** called at the beginning of sqlite3VdbeExec(). The mutexes are
46225
-** exited at the end of the same function.
46226
-*/
46227
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
46228
- int i;
46229
- for(i=0; i<pArray->nMutex; i++){
46230
- Btree *p = pArray->aBtree[i];
46231
- /* Some basic sanity checking */
46232
- assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
46233
- assert( !p->locked || p->wantToLock>0 );
46234
-
46235
- /* We should already hold a lock on the database connection */
46236
- assert( sqlite3_mutex_held(p->db->mutex) );
46237
-
46238
- /* The Btree is sharable because only sharable Btrees are entered
46239
- ** into the array in the first place. */
46240
- assert( p->sharable );
46241
-
46242
- p->wantToLock++;
46243
- if( !p->locked ){
46244
- lockBtreeMutex(p);
46245
- }
46246
- }
46247
-}
46248
-
46249
-/*
46250
-** Leave the mutex of every btree in the group.
46251
-*/
46252
-SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
46253
- int i;
46254
- for(i=0; i<pArray->nMutex; i++){
46255
- Btree *p = pArray->aBtree[i];
46256
- /* Some basic sanity checking */
46257
- assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
46258
- assert( p->locked );
46259
- assert( p->wantToLock>0 );
46260
-
46261
- /* We should already hold a lock on the database connection */
46262
- assert( sqlite3_mutex_held(p->db->mutex) );
46263
-
46264
- p->wantToLock--;
46265
- if( p->wantToLock==0 ){
46266
- unlockBtreeMutex(p);
46267
- }
46268
- }
46269
-}
46270
-
46271
-#else
46214
+#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
46215
+/*
46216
+** The following are special cases for mutex enter routines for use
46217
+** in single threaded applications that use shared cache. Except for
46218
+** these two routines, all mutex operations are no-ops in that case and
46219
+** are null #defines in btree.h.
46220
+**
46221
+** If shared cache is disabled, then all btree mutex routines, including
46222
+** the ones below, are no-ops and are null #defines in btree.h.
46223
+*/
46224
+
4627246225
SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
4627346226
p->pBt->db = p->db;
4627446227
}
4627546228
SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
4627646229
int i;
@@ -49444,15 +49397,26 @@
4944449397
** routine did all the work of writing information out to disk and flushing the
4944549398
** contents so that they are written onto the disk platter. All this
4944649399
** routine has to do is delete or truncate or zero the header in the
4944749400
** the rollback journal (which causes the transaction to commit) and
4944849401
** drop locks.
49402
+**
49403
+** Normally, if an error occurs while the pager layer is attempting to
49404
+** finalize the underlying journal file, this function returns an error and
49405
+** the upper layer will attempt a rollback. However, if the second argument
49406
+** is non-zero then this b-tree transaction is part of a multi-file
49407
+** transaction. In this case, the transaction has already been committed
49408
+** (by deleting a master journal file) and the caller will ignore this
49409
+** functions return code. So, even if an error occurs in the pager layer,
49410
+** reset the b-tree objects internal state to indicate that the write
49411
+** transaction has been closed. This is quite safe, as the pager will have
49412
+** transitioned to the error state.
4944949413
**
4945049414
** This will release the write lock on the database file. If there
4945149415
** are no active cursors, it also releases the read lock.
4945249416
*/
49453
-SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){
49417
+SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
4945449418
4945549419
if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
4945649420
sqlite3BtreeEnter(p);
4945749421
btreeIntegrity(p);
4945849422
@@ -49463,11 +49427,11 @@
4946349427
int rc;
4946449428
BtShared *pBt = p->pBt;
4946549429
assert( pBt->inTransaction==TRANS_WRITE );
4946649430
assert( pBt->nTransaction>0 );
4946749431
rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
49468
- if( rc!=SQLITE_OK ){
49432
+ if( rc!=SQLITE_OK && bCleanup==0 ){
4946949433
sqlite3BtreeLeave(p);
4947049434
return rc;
4947149435
}
4947249436
pBt->inTransaction = TRANS_READ;
4947349437
}
@@ -49483,11 +49447,11 @@
4948349447
SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
4948449448
int rc;
4948549449
sqlite3BtreeEnter(p);
4948649450
rc = sqlite3BtreeCommitPhaseOne(p, 0);
4948749451
if( rc==SQLITE_OK ){
49488
- rc = sqlite3BtreeCommitPhaseTwo(p);
49452
+ rc = sqlite3BtreeCommitPhaseTwo(p, 0);
4948949453
}
4949049454
sqlite3BtreeLeave(p);
4949149455
return rc;
4949249456
}
4949349457
@@ -54264,11 +54228,11 @@
5426454228
** allocated, a null pointer is returned. If the blob has already been
5426554229
** allocated, it is returned as normal.
5426654230
**
5426754231
** Just before the shared-btree is closed, the function passed as the
5426854232
** xFree argument when the memory allocation was made is invoked on the
54269
-** blob of allocated memory. This function should not call sqlite3_free()
54233
+** blob of allocated memory. The xFree function should not call sqlite3_free()
5427054234
** on the memory, the btree layer does that.
5427154235
*/
5427254236
SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
5427354237
BtShared *pBt = p->pBt;
5427454238
sqlite3BtreeEnter(p);
@@ -54907,11 +54871,11 @@
5490754871
rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
5490854872
}
5490954873
5491054874
/* Finish committing the transaction to the destination database. */
5491154875
if( SQLITE_OK==rc
54912
- && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
54876
+ && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
5491354877
){
5491454878
rc = SQLITE_DONE;
5491554879
}
5491654880
}
5491754881
@@ -54921,11 +54885,11 @@
5492154885
** "committing" a read-only transaction cannot fail.
5492254886
*/
5492354887
if( bCloseTrans ){
5492454888
TESTONLY( int rc2 );
5492554889
TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
54926
- TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
54890
+ TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
5492754891
assert( rc2==SQLITE_OK );
5492854892
}
5492954893
5493054894
if( rc==SQLITE_IOERR_NOMEM ){
5493154895
rc = SQLITE_NOMEM;
@@ -56419,10 +56383,15 @@
5641956383
pOp->p2 = p2;
5642056384
pOp->p3 = p3;
5642156385
pOp->p4.p = 0;
5642256386
pOp->p4type = P4_NOTUSED;
5642356387
p->expired = 0;
56388
+ if( op==OP_ParseSchema ){
56389
+ /* Any program that uses the OP_ParseSchema opcode needs to lock
56390
+ ** all btrees. */
56391
+ p->btreeMask = ~(yDbMask)0;
56392
+ }
5642456393
#ifdef SQLITE_DEBUG
5642556394
pOp->zComment = 0;
5642656395
if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
5642756396
#endif
5642856397
#ifdef VDBE_PROFILE
@@ -56719,11 +56688,11 @@
5671956688
SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
5672056689
VdbeOp *aOp = p->aOp;
5672156690
assert( aOp && !p->db->mallocFailed );
5672256691
5672356692
/* Check that sqlite3VdbeUsesBtree() was not called on this VM */
56724
- assert( p->aMutex.nMutex==0 );
56693
+ assert( p->btreeMask==0 );
5672556694
5672656695
resolveP2Values(p, pnMaxArg);
5672756696
*pnOp = p->nOp;
5672856697
p->aOp = 0;
5672956698
return aOp;
@@ -56821,10 +56790,11 @@
5682156790
/*
5682256791
** Change the P2 operand of instruction addr so that it points to
5682356792
** the address of the next instruction to be coded.
5682456793
*/
5682556794
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
56795
+ assert( addr>=0 );
5682656796
sqlite3VdbeChangeP2(p, addr, p->nOp);
5682756797
}
5682856798
5682956799
5683056800
/*
@@ -57206,26 +57176,135 @@
5720657176
#endif
5720757177
5720857178
/*
5720957179
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
5721057180
**
57211
-** The prepared statement has to know in advance which Btree objects
57212
-** will be used so that it can acquire mutexes on them all in sorted
57213
-** order (via sqlite3VdbeMutexArrayEnter(). Mutexes are acquired
57214
-** in order (and released in reverse order) to avoid deadlocks.
57181
+** The prepared statements need to know in advance the complete set of
57182
+** attached databases that they will be using. A mask of these databases
57183
+** is maintained in p->btreeMask and is used for locking and other purposes.
5721557184
*/
5721657185
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
57217
- tAttachMask mask;
57218
- assert( i>=0 && i<p->db->nDb && i<sizeof(tAttachMask)*8 );
57186
+ assert( i>=0 && i<p->db->nDb && i<sizeof(yDbMask)*8 );
5721957187
assert( i<(int)sizeof(p->btreeMask)*8 );
57220
- mask = ((u32)1)<<i;
57221
- if( (p->btreeMask & mask)==0 ){
57222
- p->btreeMask |= mask;
57223
- sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
57224
- }
57188
+ p->btreeMask |= ((yDbMask)1)<<i;
5722557189
}
5722657190
57191
+/*
57192
+** Compute the sum of all mutex counters for all btrees in the
57193
+** given prepared statement.
57194
+*/
57195
+#ifndef SQLITE_OMIT_SHARED_CACHE
57196
+static u32 mutexCounterSum(Vdbe *p){
57197
+ u32 cntSum = 0;
57198
+#ifdef SQLITE_DEBUG
57199
+ int i;
57200
+ yDbMask mask;
57201
+ sqlite3 *db = p->db;
57202
+ Db *aDb = db->aDb;
57203
+ int nDb = db->nDb;
57204
+ for(i=0, mask=1; i<nDb; i++, mask += mask){
57205
+ if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57206
+ cntSum += sqlite3BtreeMutexCounter(aDb[i].pBt);
57207
+ }
57208
+ }
57209
+#else
57210
+ UNUSED_PARAMETER(p);
57211
+#endif
57212
+ return cntSum;
57213
+}
57214
+#endif
57215
+
57216
+/*
57217
+** If SQLite is compiled to support shared-cache mode and to be threadsafe,
57218
+** this routine obtains the mutex associated with each BtShared structure
57219
+** that may be accessed by the VM passed as an argument. In doing so it also
57220
+** sets the BtShared.db member of each of the BtShared structures, ensuring
57221
+** that the correct busy-handler callback is invoked if required.
57222
+**
57223
+** If SQLite is not threadsafe but does support shared-cache mode, then
57224
+** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
57225
+** of all of BtShared structures accessible via the database handle
57226
+** associated with the VM.
57227
+**
57228
+** If SQLite is not threadsafe and does not support shared-cache mode, this
57229
+** function is a no-op.
57230
+**
57231
+** The p->btreeMask field is a bitmask of all btrees that the prepared
57232
+** statement p will ever use. Let N be the number of bits in p->btreeMask
57233
+** corresponding to btrees that use shared cache. Then the runtime of
57234
+** this routine is N*N. But as N is rarely more than 1, this should not
57235
+** be a problem.
57236
+*/
57237
+SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
57238
+#ifndef SQLITE_OMIT_SHARED_CACHE
57239
+ int i;
57240
+ yDbMask mask;
57241
+ sqlite3 *db = p->db;
57242
+ Db *aDb = db->aDb;
57243
+ int nDb = db->nDb;
57244
+ for(i=0, mask=1; i<nDb; i++, mask += mask){
57245
+ if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57246
+ sqlite3BtreeEnter(aDb[i].pBt);
57247
+ }
57248
+ }
57249
+ p->iMutexCounter = mutexCounterSum(p);
57250
+#else
57251
+ UNUSED_PARAMETER(p);
57252
+#endif
57253
+}
57254
+
57255
+/*
57256
+** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
57257
+*/
57258
+SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
57259
+#ifndef SQLITE_OMIT_SHARED_CACHE
57260
+ int i;
57261
+ yDbMask mask;
57262
+ sqlite3 *db = p->db;
57263
+ Db *aDb = db->aDb;
57264
+ int nDb = db->nDb;
57265
+
57266
+ /* Assert that the all mutexes have been held continously since
57267
+ ** the most recent sqlite3VdbeEnter() or sqlite3VdbeMutexResync().
57268
+ */
57269
+ assert( mutexCounterSum(p) == p->iMutexCounter );
57270
+
57271
+ for(i=0, mask=1; i<nDb; i++, mask += mask){
57272
+ if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57273
+ sqlite3BtreeLeave(aDb[i].pBt);
57274
+ }
57275
+ }
57276
+#else
57277
+ UNUSED_PARAMETER(p);
57278
+#endif
57279
+}
57280
+
57281
+/*
57282
+** Recompute the sum of the mutex counters on all btrees used by the
57283
+** prepared statement p.
57284
+**
57285
+** Call this routine while holding a sqlite3VdbeEnter() after doing something
57286
+** that might cause one or more of the individual mutexes held by the
57287
+** prepared statement to be released. Calling sqlite3BtreeEnter() on
57288
+** any BtShared mutex which is not used by the prepared statement is one
57289
+** way to cause one or more of the mutexes in the prepared statement
57290
+** to be temporarily released. The anti-deadlocking logic in
57291
+** sqlite3BtreeEnter() can cause mutexes to be released temporarily then
57292
+** reacquired.
57293
+**
57294
+** Calling this routine is an acknowledgement that some of the individual
57295
+** mutexes in the prepared statement might have been released and reacquired.
57296
+** So checks to verify that mutex-protected content did not change
57297
+** unexpectedly should accompany any call to this routine.
57298
+*/
57299
+SQLITE_PRIVATE void sqlite3VdbeMutexResync(Vdbe *p){
57300
+#if !defined(SQLITE_OMIT_SHARED_CACHE) && defined(SQLITE_DEBUG)
57301
+ p->iMutexCounter = mutexCounterSum(p);
57302
+#else
57303
+ UNUSED_PARAMETER(p);
57304
+#endif
57305
+}
5722757306
5722857307
#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
5722957308
/*
5723057309
** Print a single opcode. This routine is used for debugging only.
5723157310
*/
@@ -57965,11 +58044,11 @@
5796558044
** but could happen. In this case abandon processing and return the error.
5796658045
*/
5796758046
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
5796858047
Btree *pBt = db->aDb[i].pBt;
5796958048
if( pBt ){
57970
- rc = sqlite3BtreeCommitPhaseTwo(pBt);
58049
+ rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
5797158050
}
5797258051
}
5797358052
if( rc==SQLITE_OK ){
5797458053
sqlite3VtabCommit(db);
5797558054
}
@@ -58097,11 +58176,11 @@
5809758176
disable_simulated_io_errors();
5809858177
sqlite3BeginBenignMalloc();
5809958178
for(i=0; i<db->nDb; i++){
5810058179
Btree *pBt = db->aDb[i].pBt;
5810158180
if( pBt ){
58102
- sqlite3BtreeCommitPhaseTwo(pBt);
58181
+ sqlite3BtreeCommitPhaseTwo(pBt, 1);
5810358182
}
5810458183
}
5810558184
sqlite3EndBenignMalloc();
5810658185
enable_simulated_io_errors();
5810758186
@@ -58220,37 +58299,10 @@
5822058299
}
5822158300
}
5822258301
return rc;
5822358302
}
5822458303
58225
-/*
58226
-** If SQLite is compiled to support shared-cache mode and to be threadsafe,
58227
-** this routine obtains the mutex associated with each BtShared structure
58228
-** that may be accessed by the VM passed as an argument. In doing so it
58229
-** sets the BtShared.db member of each of the BtShared structures, ensuring
58230
-** that the correct busy-handler callback is invoked if required.
58231
-**
58232
-** If SQLite is not threadsafe but does support shared-cache mode, then
58233
-** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
58234
-** of all of BtShared structures accessible via the database handle
58235
-** associated with the VM. Of course only a subset of these structures
58236
-** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
58237
-** that subset out, but there is no advantage to doing so.
58238
-**
58239
-** If SQLite is not threadsafe and does not support shared-cache mode, this
58240
-** function is a no-op.
58241
-*/
58242
-#ifndef SQLITE_OMIT_SHARED_CACHE
58243
-SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p){
58244
-#if SQLITE_THREADSAFE
58245
- sqlite3BtreeMutexArrayEnter(&p->aMutex);
58246
-#else
58247
- sqlite3BtreeEnterAll(p->db);
58248
-#endif
58249
-}
58250
-#endif
58251
-
5825258304
/*
5825358305
** This function is called when a transaction opened by the database
5825458306
** handle associated with the VM passed as an argument is about to be
5825558307
** committed. If there are outstanding deferred foreign key constraint
5825658308
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
@@ -58319,11 +58371,11 @@
5831958371
int mrc; /* Primary error code from p->rc */
5832058372
int eStatementOp = 0;
5832158373
int isSpecialError; /* Set to true if a 'special' error */
5832258374
5832358375
/* Lock all btrees used by the statement */
58324
- sqlite3VdbeMutexArrayEnter(p);
58376
+ sqlite3VdbeEnter(p);
5832558377
5832658378
/* Check for one of the special errors */
5832758379
mrc = p->rc & 0xff;
5832858380
assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
5832958381
isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
@@ -58373,11 +58425,11 @@
5837358425
){
5837458426
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
5837558427
rc = sqlite3VdbeCheckFk(p, 1);
5837658428
if( rc!=SQLITE_OK ){
5837758429
if( NEVER(p->readOnly) ){
58378
- sqlite3BtreeMutexArrayLeave(&p->aMutex);
58430
+ sqlite3VdbeLeave(p);
5837958431
return SQLITE_ERROR;
5838058432
}
5838158433
rc = SQLITE_CONSTRAINT;
5838258434
}else{
5838358435
/* The auto-commit flag is true, the vdbe program was successful
@@ -58385,11 +58437,11 @@
5838558437
** key constraints to hold up the transaction. This means a commit
5838658438
** is required. */
5838758439
rc = vdbeCommit(db, p);
5838858440
}
5838958441
if( rc==SQLITE_BUSY && p->readOnly ){
58390
- sqlite3BtreeMutexArrayLeave(&p->aMutex);
58442
+ sqlite3VdbeLeave(p);
5839158443
return SQLITE_BUSY;
5839258444
}else if( rc!=SQLITE_OK ){
5839358445
p->rc = rc;
5839458446
sqlite3RollbackAll(db);
5839558447
}else{
@@ -58457,11 +58509,12 @@
5845758509
sqlite3ResetInternalSchema(db, 0);
5845858510
db->flags = (db->flags | SQLITE_InternChanges);
5845958511
}
5846058512
5846158513
/* Release the locks */
58462
- sqlite3BtreeMutexArrayLeave(&p->aMutex);
58514
+ sqlite3VdbeMutexResync(p);
58515
+ sqlite3VdbeLeave(p);
5846358516
}
5846458517
5846558518
/* We have successfully halted and closed the VM. Record this fact. */
5846658519
if( p->pc>=0 ){
5846758520
db->activeVdbeCnt--;
@@ -61978,11 +62031,11 @@
6197862031
} u;
6197962032
/* End automatically generated code
6198062033
********************************************************************/
6198162034
6198262035
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
61983
- sqlite3VdbeMutexArrayEnter(p);
62036
+ sqlite3VdbeEnter(p);
6198462037
if( p->rc==SQLITE_NOMEM ){
6198562038
/* This happens if a malloc() inside a call to sqlite3_column_text() or
6198662039
** sqlite3_column_text16() failed. */
6198762040
goto no_mem;
6198862041
}
@@ -62815,19 +62868,31 @@
6281562868
assert( pOp[-1].p4type==P4_COLLSEQ );
6281662869
assert( pOp[-1].opcode==OP_CollSeq );
6281762870
u.ag.ctx.pColl = pOp[-1].p4.pColl;
6281862871
}
6281962872
(*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
62873
+ sqlite3VdbeMutexResync(p);
6282062874
if( db->mallocFailed ){
6282162875
/* Even though a malloc() has failed, the implementation of the
6282262876
** user function may have called an sqlite3_result_XXX() function
6282362877
** to return a value. The following call releases any resources
6282462878
** associated with such a value.
6282562879
*/
6282662880
sqlite3VdbeMemRelease(&u.ag.ctx.s);
6282762881
goto no_mem;
6282862882
}
62883
+
62884
+ /* The app-defined function has done something that as caused this
62885
+ ** statement to expire. (Perhaps the function called sqlite3_exec()
62886
+ ** with a CREATE TABLE statement.)
62887
+ */
62888
+#if 0
62889
+ if( p->expired ){
62890
+ rc = SQLITE_ABORT;
62891
+ break;
62892
+ }
62893
+#endif
6282962894
6283062895
/* If any auxiliary data functions have been called by this user function,
6283162896
** immediately call the destructor for any non-static values.
6283262897
*/
6283362898
if( u.ag.ctx.pVdbeFunc ){
@@ -64091,10 +64156,11 @@
6409164156
}
6409264157
}
6409364158
if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
6409464159
sqlite3ExpirePreparedStatements(db);
6409564160
sqlite3ResetInternalSchema(db, 0);
64161
+ sqlite3VdbeMutexResync(p);
6409664162
db->flags = (db->flags | SQLITE_InternChanges);
6409764163
}
6409864164
}
6409964165
6410064166
/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
@@ -64234,11 +64300,11 @@
6423464300
#if 0 /* local variables moved into u.as */
6423564301
Btree *pBt;
6423664302
#endif /* local variables moved into u.as */
6423764303
6423864304
assert( pOp->p1>=0 && pOp->p1<db->nDb );
64239
- assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64305
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6424064306
u.as.pBt = db->aDb[pOp->p1].pBt;
6424164307
6424264308
if( u.as.pBt ){
6424364309
rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
6424464310
if( rc==SQLITE_BUSY ){
@@ -64292,11 +64358,11 @@
6429264358
u.at.iDb = pOp->p1;
6429364359
u.at.iCookie = pOp->p3;
6429464360
assert( pOp->p3<SQLITE_N_BTREE_META );
6429564361
assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
6429664362
assert( db->aDb[u.at.iDb].pBt!=0 );
64297
- assert( (p->btreeMask & (1<<u.at.iDb))!=0 );
64363
+ assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
6429864364
6429964365
sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
6430064366
pOut->u.i = u.at.iMeta;
6430164367
break;
6430264368
}
@@ -64315,11 +64381,11 @@
6431564381
#if 0 /* local variables moved into u.au */
6431664382
Db *pDb;
6431764383
#endif /* local variables moved into u.au */
6431864384
assert( pOp->p2<SQLITE_N_BTREE_META );
6431964385
assert( pOp->p1>=0 && pOp->p1<db->nDb );
64320
- assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64386
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6432164387
u.au.pDb = &db->aDb[pOp->p1];
6432264388
assert( u.au.pDb->pBt!=0 );
6432364389
pIn3 = &aMem[pOp->p3];
6432464390
sqlite3VdbeMemIntegerify(pIn3);
6432564391
/* See note about index shifting on OP_ReadCookie */
@@ -64365,11 +64431,11 @@
6436564431
int iGen;
6436664432
Btree *pBt;
6436764433
#endif /* local variables moved into u.av */
6436864434
6436964435
assert( pOp->p1>=0 && pOp->p1<db->nDb );
64370
- assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64436
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6437164437
u.av.pBt = db->aDb[pOp->p1].pBt;
6437264438
if( u.av.pBt ){
6437364439
sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
6437464440
u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
6437564441
}else{
@@ -64391,10 +64457,11 @@
6439164457
** to be invalidated whenever sqlite3_step() is called from within
6439264458
** a v-table method.
6439364459
*/
6439464460
if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
6439564461
sqlite3ResetInternalSchema(db, pOp->p1);
64462
+ sqlite3VdbeMutexResync(p);
6439664463
}
6439764464
6439864465
p->expired = 1;
6439964466
rc = SQLITE_SCHEMA;
6440064467
}
@@ -64471,11 +64538,11 @@
6447164538
u.aw.nField = 0;
6447264539
u.aw.pKeyInfo = 0;
6447364540
u.aw.p2 = pOp->p2;
6447464541
u.aw.iDb = pOp->p3;
6447564542
assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
64476
- assert( (p->btreeMask & (1<<u.aw.iDb))!=0 );
64543
+ assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
6447764544
u.aw.pDb = &db->aDb[u.aw.iDb];
6447864545
u.aw.pX = u.aw.pDb->pBt;
6447964546
assert( u.aw.pX!=0 );
6448064547
if( pOp->opcode==OP_OpenWrite ){
6448164548
u.aw.wrFlag = 1;
@@ -66008,11 +66075,11 @@
6600866075
rc = SQLITE_LOCKED;
6600966076
p->errorAction = OE_Abort;
6601066077
}else{
6601166078
u.br.iDb = pOp->p3;
6601266079
assert( u.br.iCnt==1 );
66013
- assert( (p->btreeMask & (1<<u.br.iDb))!=0 );
66080
+ assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
6601466081
rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
6601566082
pOut->flags = MEM_Int;
6601666083
pOut->u.i = u.br.iMoved;
6601766084
#ifndef SQLITE_OMIT_AUTOVACUUM
6601866085
if( rc==SQLITE_OK && u.br.iMoved!=0 ){
@@ -66046,11 +66113,11 @@
6604666113
#if 0 /* local variables moved into u.bs */
6604766114
int nChange;
6604866115
#endif /* local variables moved into u.bs */
6604966116
6605066117
u.bs.nChange = 0;
66051
- assert( (p->btreeMask & (1<<pOp->p2))!=0 );
66118
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
6605266119
rc = sqlite3BtreeClearTable(
6605366120
db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
6605466121
);
6605566122
if( pOp->p3 ){
6605666123
p->nChange += u.bs.nChange;
@@ -66093,11 +66160,11 @@
6609366160
Db *pDb;
6609466161
#endif /* local variables moved into u.bt */
6609566162
6609666163
u.bt.pgno = 0;
6609766164
assert( pOp->p1>=0 && pOp->p1<db->nDb );
66098
- assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66165
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6609966166
u.bt.pDb = &db->aDb[pOp->p1];
6610066167
assert( u.bt.pDb->pBt!=0 );
6610166168
if( pOp->opcode==OP_CreateTable ){
6610266169
/* u.bt.flags = BTREE_INTKEY; */
6610366170
u.bt.flags = BTREE_INTKEY;
@@ -66122,31 +66189,27 @@
6612266189
int iDb;
6612366190
const char *zMaster;
6612466191
char *zSql;
6612566192
InitData initData;
6612666193
#endif /* local variables moved into u.bu */
66194
+
66195
+ /* Any prepared statement that invokes this opcode will hold mutexes
66196
+ ** on every btree. This is a prerequisite for invoking
66197
+ ** sqlite3InitCallback().
66198
+ */
66199
+#ifdef SQLITE_DEBUG
66200
+ for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
66201
+ assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
66202
+ }
66203
+#endif
66204
+ assert( p->btreeMask == ~(yDbMask)0 );
66205
+
6612766206
6612866207
u.bu.iDb = pOp->p1;
6612966208
assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
66130
-
66131
- /* Although the mutex on the BtShared object that corresponds to
66132
- ** database u.bu.iDb (the database containing the sqlite_master table
66133
- ** read by this instruction) is currently held, it is necessary to
66134
- ** obtain the mutexes on all attached databases before checking if
66135
- ** the schema of u.bu.iDb is loaded. This is because, at the start of
66136
- ** the sqlite3_exec() call below, SQLite will invoke
66137
- ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
66138
- ** u.bu.iDb mutex may be temporarily released to avoid deadlock. If
66139
- ** this happens, then some other thread may delete the in-memory
66140
- ** schema of database u.bu.iDb before the SQL statement runs. The schema
66141
- ** will not be reloaded becuase the db->init.busy flag is set. This
66142
- ** can result in a "no such table: sqlite_master" or "malformed
66143
- ** database schema" error being returned to the user.
66144
- */
66145
- assert( sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
66146
- sqlite3BtreeEnterAll(db);
66147
- if( ALWAYS(DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded)) ){
66209
+ assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
66210
+ /* Used to be a conditional */ {
6614866211
u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
6614966212
u.bu.initData.db = db;
6615066213
u.bu.initData.iDb = pOp->p1;
6615166214
u.bu.initData.pzErrMsg = &p->zErrMsg;
6615266215
u.bu.zSql = sqlite3MPrintf(db,
@@ -66163,11 +66226,10 @@
6616366226
if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
6616466227
sqlite3DbFree(db, u.bu.zSql);
6616566228
db->init.busy = 0;
6616666229
}
6616766230
}
66168
- sqlite3BtreeLeaveAll(db);
6616966231
if( rc==SQLITE_NOMEM ){
6617066232
goto no_mem;
6617166233
}
6617266234
break;
6617366235
}
@@ -66266,11 +66328,11 @@
6626666328
for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
6626766329
u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
6626866330
}
6626966331
u.bv.aRoot[u.bv.j] = 0;
6627066332
assert( pOp->p5<db->nDb );
66271
- assert( (p->btreeMask & (1<<pOp->p5))!=0 );
66333
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
6627266334
u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
6627366335
(int)u.bv.pnErr->u.i, &u.bv.nErr);
6627466336
sqlite3DbFree(db, u.bv.aRoot);
6627566337
u.bv.pnErr->u.i -= u.bv.nErr;
6627666338
sqlite3VdbeMemSetNull(pIn1);
@@ -66702,15 +66764,29 @@
6670266764
assert( pOp[-1].p4type==P4_COLLSEQ );
6670366765
assert( pOp[-1].opcode==OP_CollSeq );
6670466766
u.cb.ctx.pColl = pOp[-1].p4.pColl;
6670566767
}
6670666768
(u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
66769
+ sqlite3VdbeMutexResync(p);
6670766770
if( u.cb.ctx.isError ){
6670866771
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
6670966772
rc = u.cb.ctx.isError;
6671066773
}
66774
+
66775
+ /* The app-defined function has done something that as caused this
66776
+ ** statement to expire. (Perhaps the function called sqlite3_exec()
66777
+ ** with a CREATE TABLE statement.)
66778
+ */
66779
+#if 0
66780
+ if( p->expired ){
66781
+ rc = SQLITE_ABORT;
66782
+ break;
66783
+ }
66784
+#endif
66785
+
6671166786
sqlite3VdbeMemRelease(&u.cb.ctx.s);
66787
+
6671266788
break;
6671366789
}
6671466790
6671566791
/* Opcode: AggFinal P1 P2 * P4 *
6671666792
**
@@ -66730,12 +66806,15 @@
6673066806
#endif /* local variables moved into u.cc */
6673166807
assert( pOp->p1>0 && pOp->p1<=p->nMem );
6673266808
u.cc.pMem = &aMem[pOp->p1];
6673366809
assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
6673466810
rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
66811
+ sqlite3VdbeMutexResync(p);
6673566812
if( rc ){
6673666813
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
66814
+ }else if( p->expired ){
66815
+ rc = SQLITE_ABORT;
6673766816
}
6673866817
sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
6673966818
UPDATE_MAX_BLOBSIZE(u.cc.pMem);
6674066819
if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
6674166820
goto too_big;
@@ -66812,25 +66891,24 @@
6681266891
);
6681366892
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6681466893
6681566894
/* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
6681666895
** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
66817
- ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
66896
+ ** when the statement is prepared and so p->btreeMask!=0. All mutexes
6681866897
** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
6681966898
** is not called when the statement is prepared because it requires the
6682066899
** iDb index of the database as a parameter, and the database has not
6682166900
** yet been attached so that index is unavailable. We have to wait
6682266901
** until runtime (now) to get the mutex on the newly attached database.
6682366902
** No other mutexes are required by the ATTACH command so this is safe
6682466903
** to do.
6682566904
*/
66826
- assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
66827
- if( p->aMutex.nMutex==0 ){
66905
+ if( p->btreeMask==0 ){
6682866906
/* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
6682966907
** database. */
6683066908
sqlite3VdbeUsesBtree(p, pOp->p1);
66831
- sqlite3VdbeMutexArrayEnter(p);
66909
+ sqlite3VdbeEnter(p);
6683266910
}
6683366911
6683466912
u.ce.pBt = db->aDb[pOp->p1].pBt;
6683566913
u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
6683666914
u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
@@ -66928,11 +67006,11 @@
6692867006
#if 0 /* local variables moved into u.cf */
6692967007
Btree *pBt;
6693067008
#endif /* local variables moved into u.cf */
6693167009
6693267010
assert( pOp->p1>=0 && pOp->p1<db->nDb );
66933
- assert( (p->btreeMask & (1<<pOp->p1))!=0 );
67011
+ assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6693467012
u.cf.pBt = db->aDb[pOp->p1].pBt;
6693567013
rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
6693667014
if( rc==SQLITE_DONE ){
6693767015
pc = pOp->p2 - 1;
6693867016
rc = SQLITE_OK;
@@ -66977,11 +67055,11 @@
6697767055
case OP_TableLock: {
6697867056
u8 isWriteLock = (u8)pOp->p3;
6697967057
if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6698067058
int p1 = pOp->p1;
6698167059
assert( p1>=0 && p1<db->nDb );
66982
- assert( (p->btreeMask & (1<<p1))!=0 );
67060
+ assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
6698367061
assert( isWriteLock==0 || isWriteLock==1 );
6698467062
rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
6698567063
if( (rc&0xFF)==SQLITE_LOCKED ){
6698667064
const char *z = pOp->p4.z;
6698767065
sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -67482,17 +67560,20 @@
6748267560
sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6748367561
pc, p->zSql, p->zErrMsg);
6748467562
sqlite3VdbeHalt(p);
6748567563
if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6748667564
rc = SQLITE_ERROR;
67487
- if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
67565
+ if( resetSchemaOnFault ){
67566
+ sqlite3ResetInternalSchema(db, 0);
67567
+ sqlite3VdbeMutexResync(p);
67568
+ }
6748867569
6748967570
/* This is the only way out of this procedure. We have to
6749067571
** release the mutexes on btrees that were acquired at the
6749167572
** top. */
6749267573
vdbe_return:
67493
- sqlite3BtreeMutexArrayLeave(&p->aMutex);
67574
+ sqlite3VdbeLeave(p);
6749467575
return rc;
6749567576
6749667577
/* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6749767578
** is encountered.
6749867579
*/
@@ -73966,10 +74047,26 @@
7396674047
if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
7396774048
sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
7396874049
}
7396974050
#endif
7397074051
}
74052
+
74053
+/*
74054
+** Parameter zName is the name of a table that is about to be altered
74055
+** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
74056
+** If the table is a system table, this function leaves an error message
74057
+** in pParse->zErr (system tables may not be altered) and returns non-zero.
74058
+**
74059
+** Or, if zName is not a system table, zero is returned.
74060
+*/
74061
+static int isSystemTable(Parse *pParse, const char *zName){
74062
+ if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
74063
+ sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
74064
+ return 1;
74065
+ }
74066
+ return 0;
74067
+}
7397174068
7397274069
/*
7397374070
** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
7397474071
** command.
7397574072
*/
@@ -74017,18 +74114,15 @@
7401774114
}
7401874115
7401974116
/* Make sure it is not a system table being altered, or a reserved name
7402074117
** that the table is being renamed to.
7402174118
*/
74022
- if( sqlite3Strlen30(pTab->zName)>6
74023
- && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
74024
- ){
74025
- sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
74119
+ if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
7402674120
goto exit_rename_table;
7402774121
}
74028
- if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
74029
- goto exit_rename_table;
74122
+ if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
74123
+ exit_rename_table;
7403074124
}
7403174125
7403274126
#ifndef SQLITE_OMIT_VIEW
7403374127
if( pTab->pSelect ){
7403474128
sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
@@ -74356,10 +74450,13 @@
7435674450
/* Make sure this is not an attempt to ALTER a view. */
7435774451
if( pTab->pSelect ){
7435874452
sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
7435974453
goto exit_begin_add_column;
7436074454
}
74455
+ if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
74456
+ goto exit_begin_add_column;
74457
+ }
7436174458
7436274459
assert( pTab->addColOffset>0 );
7436374460
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
7436474461
7436574462
/* Put a copy of the Table struct in Parse.pNewTable for the
@@ -74443,11 +74540,12 @@
7444374540
*/
7444474541
static void openStatTable(
7444574542
Parse *pParse, /* Parsing context */
7444674543
int iDb, /* The database we are looking in */
7444774544
int iStatCur, /* Open the sqlite_stat1 table on this cursor */
74448
- const char *zWhere /* Delete entries associated with this table */
74545
+ const char *zWhere, /* Delete entries for this table or index */
74546
+ const char *zWhereType /* Either "tbl" or "idx" */
7444974547
){
7445074548
static const struct {
7445174549
const char *zName;
7445274550
const char *zCols;
7445374551
} aTable[] = {
@@ -74488,11 +74586,11 @@
7448874586
** entire contents of the table. */
7448974587
aRoot[i] = pStat->tnum;
7449074588
sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
7449174589
if( zWhere ){
7449274590
sqlite3NestedParse(pParse,
74493
- "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
74591
+ "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
7449474592
);
7449574593
}else{
7449674594
/* The sqlite_stat[12] table already exists. Delete all rows. */
7449774595
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
7449874596
}
@@ -74512,10 +74610,11 @@
7451274610
** a single table.
7451374611
*/
7451474612
static void analyzeOneTable(
7451574613
Parse *pParse, /* Parser context */
7451674614
Table *pTab, /* Table whose indices are to be analyzed */
74615
+ Index *pOnlyIdx, /* If not NULL, only analyze this one index */
7451774616
int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
7451874617
int iMem /* Available memory locations begin here */
7451974618
){
7452074619
sqlite3 *db = pParse->db; /* Database handle */
7452174620
Index *pIdx; /* An index to being analyzed */
@@ -74522,12 +74621,11 @@
7452274621
int iIdxCur; /* Cursor open on index being analyzed */
7452374622
Vdbe *v; /* The virtual machine being built up */
7452474623
int i; /* Loop counter */
7452574624
int topOfLoop; /* The top of the loop */
7452674625
int endOfLoop; /* The end of the loop */
74527
- int addr = 0; /* The address of an instruction */
74528
- int jZeroRows = 0; /* Jump from here if number of rows is zero */
74626
+ int jZeroRows = -1; /* Jump from here if number of rows is zero */
7452974627
int iDb; /* Index of database containing pTab */
7453074628
int regTabname = iMem++; /* Register containing table name */
7453174629
int regIdxname = iMem++; /* Register containing index name */
7453274630
int regSampleno = iMem++; /* Register containing next sample number */
7453374631
int regCol = iMem++; /* Content of a column analyzed table */
@@ -74534,10 +74632,11 @@
7453474632
int regRec = iMem++; /* Register holding completed record */
7453574633
int regTemp = iMem++; /* Temporary use register */
7453674634
int regRowid = iMem++; /* Rowid for the inserted record */
7453774635
7453874636
#ifdef SQLITE_ENABLE_STAT2
74637
+ int addr = 0; /* Instruction address */
7453974638
int regTemp2 = iMem++; /* Temporary use register */
7454074639
int regSamplerecno = iMem++; /* Index of next sample to record */
7454174640
int regRecno = iMem++; /* Current sample index */
7454274641
int regLast = iMem++; /* Index of last sample to record */
7454374642
int regFirst = iMem++; /* Index of first sample to record */
@@ -74569,13 +74668,16 @@
7456974668
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
7457074669
7457174670
iIdxCur = pParse->nTab++;
7457274671
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
7457374672
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
74574
- int nCol = pIdx->nColumn;
74575
- KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
74673
+ int nCol;
74674
+ KeyInfo *pKey;
7457674675
74676
+ if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
74677
+ nCol = pIdx->nColumn;
74678
+ pKey = sqlite3IndexKeyinfo(pParse, pIdx);
7457774679
if( iMem+1+(nCol*2)>pParse->nMem ){
7457874680
pParse->nMem = iMem+1+(nCol*2);
7457974681
}
7458074682
7458174683
/* Open a cursor to the index to be analyzed. */
@@ -74728,11 +74830,11 @@
7472874830
** If K==0 then no entry is made into the sqlite_stat1 table.
7472974831
** If K>0 then it is always the case the D>0 so division by zero
7473074832
** is never possible.
7473174833
*/
7473274834
sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
74733
- if( jZeroRows==0 ){
74835
+ if( jZeroRows<0 ){
7473474836
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
7473574837
}
7473674838
for(i=0; i<nCol; i++){
7473774839
sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
7473874840
sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
@@ -74754,24 +74856,22 @@
7475474856
if( pTab->pIndex==0 ){
7475574857
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
7475674858
VdbeComment((v, "%s", pTab->zName));
7475774859
sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
7475874860
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
74861
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
7475974862
}else{
74760
- assert( jZeroRows>0 );
74761
- addr = sqlite3VdbeAddOp0(v, OP_Goto);
7476274863
sqlite3VdbeJumpHere(v, jZeroRows);
74864
+ jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
7476374865
}
7476474866
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
7476574867
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
7476674868
sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
7476774869
sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
7476874870
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7476974871
if( pParse->nMem<regRec ) pParse->nMem = regRec;
74770
- if( jZeroRows ){
74771
- sqlite3VdbeJumpHere(v, addr);
74772
- }
74872
+ sqlite3VdbeJumpHere(v, jZeroRows);
7477374873
}
7477474874
7477574875
/*
7477674876
** Generate code that will cause the most recent index analysis to
7477774877
** be loaded into internal hash tables where is can be used.
@@ -74794,35 +74894,40 @@
7479474894
int iMem;
7479574895
7479674896
sqlite3BeginWriteOperation(pParse, 0, iDb);
7479774897
iStatCur = pParse->nTab;
7479874898
pParse->nTab += 2;
74799
- openStatTable(pParse, iDb, iStatCur, 0);
74899
+ openStatTable(pParse, iDb, iStatCur, 0, 0);
7480074900
iMem = pParse->nMem+1;
7480174901
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
7480274902
Table *pTab = (Table*)sqliteHashData(k);
74803
- analyzeOneTable(pParse, pTab, iStatCur, iMem);
74903
+ analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
7480474904
}
7480574905
loadAnalysis(pParse, iDb);
7480674906
}
7480774907
7480874908
/*
7480974909
** Generate code that will do an analysis of a single table in
74810
-** a database.
74910
+** a database. If pOnlyIdx is not NULL then it is a single index
74911
+** in pTab that should be analyzed.
7481174912
*/
74812
-static void analyzeTable(Parse *pParse, Table *pTab){
74913
+static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
7481374914
int iDb;
7481474915
int iStatCur;
7481574916
7481674917
assert( pTab!=0 );
7481774918
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
7481874919
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
7481974920
sqlite3BeginWriteOperation(pParse, 0, iDb);
7482074921
iStatCur = pParse->nTab;
7482174922
pParse->nTab += 2;
74822
- openStatTable(pParse, iDb, iStatCur, pTab->zName);
74823
- analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
74923
+ if( pOnlyIdx ){
74924
+ openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
74925
+ }else{
74926
+ openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
74927
+ }
74928
+ analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
7482474929
loadAnalysis(pParse, iDb);
7482574930
}
7482674931
7482774932
/*
7482874933
** Generate code for the ANALYZE command. The parser calls this routine
@@ -74840,10 +74945,11 @@
7484074945
sqlite3 *db = pParse->db;
7484174946
int iDb;
7484274947
int i;
7484374948
char *z, *zDb;
7484474949
Table *pTab;
74950
+ Index *pIdx;
7484574951
Token *pTableName;
7484674952
7484774953
/* Read the database schema. If an error occurs, leave an error message
7484874954
** and code in pParse and return NULL. */
7484974955
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
@@ -74864,29 +74970,31 @@
7486474970
if( iDb>=0 ){
7486574971
analyzeDatabase(pParse, iDb);
7486674972
}else{
7486774973
z = sqlite3NameFromToken(db, pName1);
7486874974
if( z ){
74869
- pTab = sqlite3LocateTable(pParse, 0, z, 0);
74975
+ if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
74976
+ analyzeTable(pParse, pIdx->pTable, pIdx);
74977
+ }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
74978
+ analyzeTable(pParse, pTab, 0);
74979
+ }
7487074980
sqlite3DbFree(db, z);
74871
- if( pTab ){
74872
- analyzeTable(pParse, pTab);
74873
- }
7487474981
}
7487574982
}
7487674983
}else{
7487774984
/* Form 3: Analyze the fully qualified table name */
7487874985
iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
7487974986
if( iDb>=0 ){
7488074987
zDb = db->aDb[iDb].zName;
7488174988
z = sqlite3NameFromToken(db, pTableName);
7488274989
if( z ){
74883
- pTab = sqlite3LocateTable(pParse, 0, z, zDb);
74990
+ if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
74991
+ analyzeTable(pParse, pIdx->pTable, pIdx);
74992
+ }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
74993
+ analyzeTable(pParse, pTab, 0);
74994
+ }
7488474995
sqlite3DbFree(db, z);
74885
- if( pTab ){
74886
- analyzeTable(pParse, pTab);
74887
- }
7488874996
}
7488974997
}
7489074998
}
7489174999
}
7489275000
@@ -76055,11 +76163,11 @@
7605576163
** set for each database that is used. Generate code to start a
7605676164
** transaction on each used database and to verify the schema cookie
7605776165
** on each used database.
7605876166
*/
7605976167
if( pParse->cookieGoto>0 ){
76060
- tAttachMask mask;
76168
+ yDbMask mask;
7606176169
int iDb;
7606276170
sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
7606376171
for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
7606476172
if( (mask & pParse->cookieMask)==0 ) continue;
7606576173
sqlite3VdbeUsesBtree(v, iDb);
@@ -79351,16 +79459,16 @@
7935179459
if( v==0 ) return; /* This only happens if there was a prior error */
7935279460
pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
7935379461
}
7935479462
if( iDb>=0 ){
7935579463
sqlite3 *db = pToplevel->db;
79356
- tAttachMask mask;
79464
+ yDbMask mask;
7935779465
7935879466
assert( iDb<db->nDb );
7935979467
assert( db->aDb[iDb].pBt!=0 || iDb==1 );
7936079468
assert( iDb<SQLITE_MAX_ATTACHED+2 );
79361
- mask = ((tAttachMask)1)<<iDb;
79469
+ mask = ((yDbMask)1)<<iDb;
7936279470
if( (pToplevel->cookieMask & mask)==0 ){
7936379471
pToplevel->cookieMask |= mask;
7936479472
pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
7936579473
if( !OMIT_TEMPDB && iDb==1 ){
7936679474
sqlite3OpenTempDatabase(pToplevel);
@@ -79383,11 +79491,11 @@
7938379491
** necessary to undo a write and the checkpoint should not be set.
7938479492
*/
7938579493
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
7938679494
Parse *pToplevel = sqlite3ParseToplevel(pParse);
7938779495
sqlite3CodeVerifySchema(pParse, iDb);
79388
- pToplevel->writeMask |= ((tAttachMask)1)<<iDb;
79496
+ pToplevel->writeMask |= ((yDbMask)1)<<iDb;
7938979497
pToplevel->isMultiWrite |= setStatement;
7939079498
}
7939179499
7939279500
/*
7939379501
** Indicate that the statement currently under construction might write
@@ -99567,11 +99675,11 @@
9956799675
** VALUE and how common that value is according to the histogram.
9956899676
*/
9956999677
if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
9957099678
if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
9957199679
testcase( pFirstTerm->eOperator==WO_EQ );
99572
- testcase( pFirstTerm->pOperator==WO_ISNULL );
99680
+ testcase( pFirstTerm->eOperator==WO_ISNULL );
9957399681
whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
9957499682
}else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
9957599683
whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
9957699684
}
9957799685
}
9957899686
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,9 +1,9 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.6. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
9 **
@@ -650,11 +650,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.6"
654 #define SQLITE_VERSION_NUMBER 3007006
655 #define SQLITE_SOURCE_ID "2011-03-24 01:34:03 b6e268fce12829f058f1dfa223731ec8479493f8"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -3521,11 +3521,13 @@
3521 ** UTF-16 string. ^The first parameter is the [prepared statement]
3522 ** that implements the [SELECT] statement. ^The second parameter is the
3523 ** column number. ^The leftmost column is number 0.
3524 **
3525 ** ^The returned string pointer is valid until either the [prepared statement]
3526 ** is destroyed by [sqlite3_finalize()] or until the next call to
 
 
3527 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3528 **
3529 ** ^If sqlite3_malloc() fails during the processing of either routine
3530 ** (for example during a conversion from UTF-8 to UTF-16) then a
3531 ** NULL pointer is returned.
@@ -3547,11 +3549,13 @@
3547 ** ^The name of the database or table or column can be returned as
3548 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
3549 ** the database name, the _table_ routines return the table name, and
3550 ** the origin_ routines return the column name.
3551 ** ^The returned string is valid until the [prepared statement] is destroyed
3552 ** using [sqlite3_finalize()] or until the same information is requested
 
 
3553 ** again in a different encoding.
3554 **
3555 ** ^The names returned are the original un-aliased names of the
3556 ** database, table, and column.
3557 **
@@ -7648,22 +7652,10 @@
7648 ** Forward declarations of structure
7649 */
7650 typedef struct Btree Btree;
7651 typedef struct BtCursor BtCursor;
7652 typedef struct BtShared BtShared;
7653 typedef struct BtreeMutexArray BtreeMutexArray;
7654
7655 /*
7656 ** This structure records all of the Btrees that need to hold
7657 ** a mutex before we enter sqlite3VdbeExec(). The Btrees are
7658 ** are placed in aBtree[] in order of aBtree[]->pBt. That way,
7659 ** we can always lock and unlock them all quickly.
7660 */
7661 struct BtreeMutexArray {
7662 int nMutex;
7663 Btree *aBtree[SQLITE_MAX_ATTACHED+1];
7664 };
7665
7666
7667 SQLITE_PRIVATE int sqlite3BtreeOpen(
7668 const char *zFilename, /* Name of database file to open */
7669 sqlite3 *db, /* Associated database connection */
@@ -7696,11 +7688,11 @@
7696 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
7697 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
7698 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
7699 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
7700 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7701 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
7702 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
7703 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
7704 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
7705 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
7706 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
@@ -7837,27 +7829,23 @@
7837 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
7838 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
7839 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
7840 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
7841 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
7842 SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
7843 SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
7844 SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
7845 #ifndef NDEBUG
7846 /* These routines are used inside assert() statements only. */
7847 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
7848 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
 
7849 #endif
7850 #else
7851
7852 # define sqlite3BtreeLeave(X)
 
7853 # define sqlite3BtreeEnterCursor(X)
7854 # define sqlite3BtreeLeaveCursor(X)
7855 # define sqlite3BtreeLeaveAll(X)
7856 # define sqlite3BtreeMutexArrayEnter(X)
7857 # define sqlite3BtreeMutexArrayLeave(X)
7858 # define sqlite3BtreeMutexArrayInsert(X,Y)
7859
7860 # define sqlite3BtreeHoldsMutex(X) 1
7861 # define sqlite3BtreeHoldsAllMutexes(X) 1
7862 #endif
7863
@@ -10467,15 +10455,17 @@
10467 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
10468 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
10469 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
10470 };
10471
10472 /* Datatype for the bitmask of all attached databases */
 
 
10473 #if SQLITE_MAX_ATTACHED>30
10474 typedef sqlite3_uint64 tAttachMask;
10475 #else
10476 typedef unsigned int tAttachMask;
10477 #endif
10478
10479 /*
10480 ** An SQL parser context. A copy of this structure is passed through
10481 ** the parser and down into all the parser action routine in order to
@@ -10522,12 +10512,12 @@
10522 u8 tempReg; /* iReg is a temp register that needs to be freed */
10523 int iLevel; /* Nesting level */
10524 int iReg; /* Reg with value of this column. 0 means none. */
10525 int lru; /* Least recently used entry has the smallest value */
10526 } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
10527 tAttachMask writeMask; /* Start a write transaction on these databases */
10528 tAttachMask cookieMask; /* Bitmask of schema verified databases */
10529 u8 isMultiWrite; /* True if statement may affect/insert multiple rows */
10530 u8 mayAbort; /* True if statement may throw an ABORT exception */
10531 int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
10532 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
10533 #ifndef SQLITE_OMIT_SHARED_CACHE
@@ -12488,14 +12478,14 @@
12488 u8 inVtabMethod; /* See comments above */
12489 u8 usesStmtJournal; /* True if uses a statement journal */
12490 u8 readOnly; /* True for read-only statements */
12491 u8 isPrepareV2; /* True if prepared with prepare_v2() */
12492 int nChange; /* Number of db changes made since last reset */
12493 tAttachMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
 
12494 int iStatement; /* Statement number (or 0 if has not opened stmt) */
12495 int aCounter[3]; /* Counters used by sqlite3_stmt_status() */
12496 BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
12497 #ifndef SQLITE_OMIT_TRACE
12498 i64 startTime; /* Time when query started - used for profiling */
12499 #endif
12500 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
12501 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -12573,10 +12563,13 @@
12573 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12574 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12575 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12576 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12577 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 
 
 
12578
12579 #ifdef SQLITE_DEBUG
12580 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
12581 #endif
12582
@@ -12584,16 +12577,10 @@
12584 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
12585 #else
12586 # define sqlite3VdbeCheckFk(p,i) 0
12587 #endif
12588
12589 #ifndef SQLITE_OMIT_SHARED_CACHE
12590 SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p);
12591 #else
12592 # define sqlite3VdbeMutexArrayEnter(p)
12593 #endif
12594
12595 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
12596 #ifdef SQLITE_DEBUG
12597 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
12598 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
12599 #endif
@@ -19794,11 +19781,11 @@
19794 }
19795 SQLITE_PRIVATE int sqlite3Utf8Read(
19796 const unsigned char *zIn, /* First byte of UTF-8 character */
19797 const unsigned char **pzNext /* Write first byte past UTF-8 char here */
19798 ){
19799 int c;
19800
19801 /* Same as READ_UTF8() above but without the zTerm parameter.
19802 ** For this routine, we assume the UTF8 string is always zero-terminated.
19803 */
19804 c = *(zIn++);
@@ -20037,19 +20024,19 @@
20037 ** Translate UTF-8 to UTF-8.
20038 **
20039 ** This has the effect of making sure that the string is well-formed
20040 ** UTF-8. Miscoded characters are removed.
20041 **
20042 ** The translation is done in-place (since it is impossible for the
20043 ** correct UTF-8 encoding to be longer than a malformed encoding).
20044 */
20045 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
20046 unsigned char *zOut = zIn;
20047 unsigned char *zStart = zIn;
20048 u32 c;
20049
20050 while( zIn[0] ){
20051 c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
20052 if( c!=0xfffd ){
20053 WRITE_UTF8(zOut, c);
20054 }
20055 }
@@ -23750,11 +23737,11 @@
23750 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
23751 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
23752 #else
23753 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
23754 #endif
23755 #define osFallocate ((int(*)(int,off_t,off_t)aSyscall[15].pCurrent)
23756
23757 }; /* End of the overrideable system calls */
23758
23759 /*
23760 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23773,14 +23760,14 @@
23773 UNUSED_PARAMETER(pNotUsed);
23774 if( zName==0 ){
23775 /* If no zName is given, restore all system calls to their default
23776 ** settings and return NULL
23777 */
 
23778 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
23779 if( aSyscall[i].pDefault ){
23780 aSyscall[i].pCurrent = aSyscall[i].pDefault;
23781 rc = SQLITE_OK;
23782 }
23783 }
23784 }else{
23785 /* If zName is specified, operate on only the one system call
23786 ** specified.
@@ -23823,22 +23810,20 @@
23823 ** then return the name of the first system call. Return NULL if zName
23824 ** is the last system call or if zName is not the name of a valid
23825 ** system call.
23826 */
23827 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
23828 unsigned int i;
23829
23830 UNUSED_PARAMETER(p);
23831 if( zName==0 ){
23832 i = -1;
23833 }else{
23834 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0])-1; i++){
23835 if( strcmp(zName, aSyscall[0].zName)==0 ) break;
23836 }
23837 }
23838 for(i++; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
23839 if( aSyscall[0].pCurrent!=0 ) return aSyscall[0].zName;
23840 }
23841 return 0;
23842 }
23843
23844 /*
@@ -23974,13 +23959,26 @@
23974 ** Errors during initialization of locks, or file system support for locks,
23975 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
23976 */
23977 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
23978 switch (posixError) {
 
 
 
 
 
 
 
 
 
 
 
 
23979 case 0:
23980 return SQLITE_OK;
23981
 
23982 case EAGAIN:
23983 case ETIMEDOUT:
23984 case EBUSY:
23985 case EINTR:
23986 case ENOLCK:
@@ -23998,12 +23996,19 @@
23998 }
23999 /* else fall through */
24000 case EPERM:
24001 return SQLITE_PERM;
24002
 
 
 
 
 
 
24003 case EDEADLK:
24004 return SQLITE_IOERR_BLOCKED;
 
24005
24006 #if EOPNOTSUPP!=ENOTSUP
24007 case EOPNOTSUPP:
24008 /* something went terribly awry, unless during file system support
24009 * introspection, in which it actually means what it says */
@@ -24418,11 +24423,11 @@
24418 ** when this function is called.
24419 */
24420 static void releaseInodeInfo(unixFile *pFile){
24421 unixInodeInfo *pInode = pFile->pInode;
24422 assert( unixMutexHeld() );
24423 if( pInode ){
24424 pInode->nRef--;
24425 if( pInode->nRef==0 ){
24426 assert( pInode->pShmNode==0 );
24427 closePendingFds(pFile);
24428 if( pInode->pPrev ){
@@ -24560,14 +24565,13 @@
24560 struct flock lock;
24561 lock.l_whence = SEEK_SET;
24562 lock.l_start = RESERVED_BYTE;
24563 lock.l_len = 1;
24564 lock.l_type = F_WRLCK;
24565 if (-1 == osFcntl(pFile->h, F_GETLK, &lock)) {
24566 int tErrno = errno;
24567 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24568 pFile->lastErrno = tErrno;
24569 } else if( lock.l_type!=F_UNLCK ){
24570 reserved = 1;
24571 }
24572 }
24573 #endif
@@ -24592,10 +24596,13 @@
24592 ** operating system does not participate.
24593 **
24594 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
24595 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
24596 ** and is read-only.
 
 
 
24597 */
24598 static int unixFileLock(unixFile *pFile, struct flock *pLock){
24599 int rc;
24600 unixInodeInfo *pInode = pFile->pInode;
24601 assert( unixMutexHeld() );
@@ -24688,11 +24695,10 @@
24688 */
24689 int rc = SQLITE_OK;
24690 unixFile *pFile = (unixFile*)id;
24691 unixInodeInfo *pInode = pFile->pInode;
24692 struct flock lock;
24693 int s = 0;
24694 int tErrno = 0;
24695
24696 assert( pFile );
24697 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
24698 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
@@ -24757,15 +24763,14 @@
24757 if( eFileLock==SHARED_LOCK
24758 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
24759 ){
24760 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
24761 lock.l_start = PENDING_BYTE;
24762 s = unixFileLock(pFile, &lock);
24763 if( s==(-1) ){
24764 tErrno = errno;
24765 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24766 if( IS_LOCK_ERROR(rc) ){
24767 pFile->lastErrno = tErrno;
24768 }
24769 goto end_lock;
24770 }
24771 }
@@ -24775,37 +24780,35 @@
24775 ** operating system calls for the specified lock.
24776 */
24777 if( eFileLock==SHARED_LOCK ){
24778 assert( pInode->nShared==0 );
24779 assert( pInode->eFileLock==0 );
 
24780
24781 /* Now get the read-lock */
24782 lock.l_start = SHARED_FIRST;
24783 lock.l_len = SHARED_SIZE;
24784 if( (s = unixFileLock(pFile, &lock))==(-1) ){
24785 tErrno = errno;
 
24786 }
 
24787 /* Drop the temporary PENDING lock */
24788 lock.l_start = PENDING_BYTE;
24789 lock.l_len = 1L;
24790 lock.l_type = F_UNLCK;
24791 if( unixFileLock(pFile, &lock)!=0 ){
24792 if( s != -1 ){
24793 /* This could happen with a network mount */
24794 tErrno = errno;
24795 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24796 if( IS_LOCK_ERROR(rc) ){
24797 pFile->lastErrno = tErrno;
24798 }
24799 goto end_lock;
24800 }
24801 }
24802 if( s==(-1) ){
24803 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24804 if( IS_LOCK_ERROR(rc) ){
24805 pFile->lastErrno = tErrno;
24806 }
24807 }else{
24808 pFile->eFileLock = SHARED_LOCK;
24809 pInode->nLock++;
24810 pInode->nShared = 1;
24811 }
@@ -24818,26 +24821,24 @@
24818 ** assumed that there is a SHARED or greater lock on the file
24819 ** already.
24820 */
24821 assert( 0!=pFile->eFileLock );
24822 lock.l_type = F_WRLCK;
24823 switch( eFileLock ){
24824 case RESERVED_LOCK:
24825 lock.l_start = RESERVED_BYTE;
24826 break;
24827 case EXCLUSIVE_LOCK:
24828 lock.l_start = SHARED_FIRST;
24829 lock.l_len = SHARED_SIZE;
24830 break;
24831 default:
24832 assert(0);
24833 }
24834 s = unixFileLock(pFile, &lock);
24835 if( s==(-1) ){
24836 tErrno = errno;
24837 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24838 if( IS_LOCK_ERROR(rc) ){
24839 pFile->lastErrno = tErrno;
24840 }
24841 }
24842 }
24843
@@ -24904,11 +24905,10 @@
24904 unixFile *pFile = (unixFile*)id;
24905 unixInodeInfo *pInode;
24906 struct flock lock;
24907 int rc = SQLITE_OK;
24908 int h;
24909 int tErrno; /* Error code from system call errors */
24910
24911 assert( pFile );
24912 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
24913 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
24914 getpid()));
@@ -24959,19 +24959,20 @@
24959 (void)handleNFSUnlock;
24960 assert( handleNFSUnlock==0 );
24961 #endif
24962 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
24963 if( handleNFSUnlock ){
 
24964 off_t divSize = SHARED_SIZE - 1;
24965
24966 lock.l_type = F_UNLCK;
24967 lock.l_whence = SEEK_SET;
24968 lock.l_start = SHARED_FIRST;
24969 lock.l_len = divSize;
24970 if( unixFileLock(pFile,, &lock)==(-1) ){
24971 tErrno = errno;
24972 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24973 if( IS_LOCK_ERROR(rc) ){
24974 pFile->lastErrno = tErrno;
24975 }
24976 goto end_unlock;
24977 }
@@ -24991,11 +24992,11 @@
24991 lock.l_whence = SEEK_SET;
24992 lock.l_start = SHARED_FIRST+divSize;
24993 lock.l_len = SHARED_SIZE-divSize;
24994 if( unixFileLock(pFile, &lock)==(-1) ){
24995 tErrno = errno;
24996 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24997 if( IS_LOCK_ERROR(rc) ){
24998 pFile->lastErrno = tErrno;
24999 }
25000 goto end_unlock;
25001 }
@@ -25004,32 +25005,32 @@
25004 {
25005 lock.l_type = F_RDLCK;
25006 lock.l_whence = SEEK_SET;
25007 lock.l_start = SHARED_FIRST;
25008 lock.l_len = SHARED_SIZE;
25009 if( unixFileLock(pFile, &lock)==(-1) ){
25010 tErrno = errno;
25011 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
25012 if( IS_LOCK_ERROR(rc) ){
25013 pFile->lastErrno = tErrno;
25014 }
 
 
 
25015 goto end_unlock;
25016 }
25017 }
25018 }
25019 lock.l_type = F_UNLCK;
25020 lock.l_whence = SEEK_SET;
25021 lock.l_start = PENDING_BYTE;
25022 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
25023 if( unixFileLock(pFile, &lock)!=(-1) ){
25024 pInode->eFileLock = SHARED_LOCK;
25025 }else{
25026 tErrno = errno;
25027 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25028 if( IS_LOCK_ERROR(rc) ){
25029 pFile->lastErrno = tErrno;
25030 }
25031 goto end_unlock;
25032 }
25033 }
25034 if( eFileLock==NO_LOCK ){
25035 /* Decrement the shared lock counter. Release the lock using an
@@ -25042,18 +25043,15 @@
25042 lock.l_whence = SEEK_SET;
25043 lock.l_start = lock.l_len = 0L;
25044 SimulateIOErrorBenign(1);
25045 SimulateIOError( h=(-1) )
25046 SimulateIOErrorBenign(0);
25047 if( unixFileLock(pFile, &lock)!=(-1) ){
25048 pInode->eFileLock = NO_LOCK;
25049 }else{
25050 tErrno = errno;
25051 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25052 if( IS_LOCK_ERROR(rc) ){
25053 pFile->lastErrno = tErrno;
25054 }
25055 pInode->eFileLock = NO_LOCK;
25056 pFile->eFileLock = NO_LOCK;
25057 }
25058 }
25059
@@ -25095,59 +25093,58 @@
25095 ** even on VxWorks. A mutex will be acquired on VxWorks by the
25096 ** vxworksReleaseFileId() routine.
25097 */
25098 static int closeUnixFile(sqlite3_file *id){
25099 unixFile *pFile = (unixFile*)id;
25100 if( pFile ){
25101 if( pFile->dirfd>=0 ){
25102 robust_close(pFile, pFile->dirfd, __LINE__);
25103 pFile->dirfd=-1;
25104 }
25105 if( pFile->h>=0 ){
25106 robust_close(pFile, pFile->h, __LINE__);
25107 pFile->h = -1;
25108 }
25109 #if OS_VXWORKS
25110 if( pFile->pId ){
25111 if( pFile->isDelete ){
25112 unlink(pFile->pId->zCanonicalName);
25113 }
25114 vxworksReleaseFileId(pFile->pId);
25115 pFile->pId = 0;
25116 }
25117 #endif
25118 OSTRACE(("CLOSE %-3d\n", pFile->h));
25119 OpenCounter(-1);
25120 sqlite3_free(pFile->pUnused);
25121 memset(pFile, 0, sizeof(unixFile));
25122 }
25123 return SQLITE_OK;
25124 }
25125
25126 /*
25127 ** Close a file.
25128 */
25129 static int unixClose(sqlite3_file *id){
25130 int rc = SQLITE_OK;
25131 if( id ){
25132 unixFile *pFile = (unixFile *)id;
25133 unixUnlock(id, NO_LOCK);
25134 unixEnterMutex();
25135 assert( pFile->pInode==0 || pFile->pInode->nLock>0
25136 || pFile->pInode->bProcessLock==0 );
25137 if( pFile->pInode && pFile->pInode->nLock ){
25138 /* If there are outstanding locks, do not actually close the file just
25139 ** yet because that would clear those locks. Instead, add the file
25140 ** descriptor to pInode->pUnused list. It will be automatically closed
25141 ** when the last lock is cleared.
25142 */
25143 setPendingFd(pFile);
25144 }
25145 releaseInodeInfo(pFile);
25146 rc = closeUnixFile(id);
25147 unixLeaveMutex();
25148 }
 
25149 return rc;
25150 }
25151
25152 /************** End of the posix advisory lock implementation *****************
25153 ******************************************************************************/
@@ -25358,11 +25355,11 @@
25358 assert( eFileLock==NO_LOCK );
25359 if( unlink(zLockFile) ){
25360 int rc = 0;
25361 int tErrno = errno;
25362 if( ENOENT != tErrno ){
25363 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25364 }
25365 if( IS_LOCK_ERROR(rc) ){
25366 pFile->lastErrno = tErrno;
25367 }
25368 return rc;
@@ -25446,11 +25443,11 @@
25446 /* got the lock, unlock it */
25447 lrc = robust_flock(pFile->h, LOCK_UN);
25448 if ( lrc ) {
25449 int tErrno = errno;
25450 /* unlock failed with an error */
25451 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25452 if( IS_LOCK_ERROR(lrc) ){
25453 pFile->lastErrno = tErrno;
25454 rc = lrc;
25455 }
25456 }
@@ -25568,25 +25565,16 @@
25568 pFile->eFileLock = eFileLock;
25569 return SQLITE_OK;
25570 }
25571
25572 /* no, really, unlock. */
25573 int rc = robust_flock(pFile->h, LOCK_UN);
25574 if (rc) {
25575 int r, tErrno = errno;
25576 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
25577 if( IS_LOCK_ERROR(r) ){
25578 pFile->lastErrno = tErrno;
25579 }
25580 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25581 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
25582 r = SQLITE_BUSY;
25583 }
25584 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25585
25586 return r;
25587 } else {
25588 pFile->eFileLock = NO_LOCK;
25589 return SQLITE_OK;
25590 }
25591 }
25592
@@ -26388,10 +26376,11 @@
26388 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
26389 #elif defined(USE_PREAD64)
26390 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
26391 #else
26392 newOffset = lseek(id->h, offset, SEEK_SET);
 
26393 if( newOffset!=offset ){
26394 if( newOffset == -1 ){
26395 ((unixFile*)id)->lastErrno = errno;
26396 }else{
26397 ((unixFile*)id)->lastErrno = 0;
@@ -26756,16 +26745,20 @@
26756
26757 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
26758
26759 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
26760 if( nSize>(i64)buf.st_size ){
 
26761 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
26762 int rc;
 
 
 
26763 do{
26764 rc = osFallocate(pFile->.h, buf.st_size, nSize-buf.st_size;
26765 }while( rc<0 && errno=EINTR );
26766 if( rc ) return SQLITE_IOERR_WRITE;
26767 #else
26768 /* If the OS does not have posix_fallocate(), fake it. First use
26769 ** ftruncate() to set the file size, then write a single byte to
26770 ** the last byte in each block within the extended region. This
26771 ** is the same technique used by glibc to implement posix_fallocate()
@@ -29114,11 +29107,13 @@
29114 rc = SQLITE_NOMEM;
29115 goto end_create_proxy;
29116 }
29117 memset(pNew, 0, sizeof(unixFile));
29118 pNew->openFlags = openFlags;
 
29119 dummyVfs.pAppData = (void*)&autolockIoFinder;
 
29120 pUnused->fd = fd;
29121 pUnused->flags = openFlags;
29122 pNew->pUnused = pUnused;
29123
29124 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
@@ -45633,11 +45628,11 @@
45633 ** the BtShared object.
45634 **
45635 ** All fields in this structure are accessed under sqlite3.mutex.
45636 ** The pBt pointer itself may not be changed while there exists cursors
45637 ** in the referenced BtShared that point back to this Btree since those
45638 ** cursors have to do go through this Btree to find their BtShared and
45639 ** they often do so without holding sqlite3.mutex.
45640 */
45641 struct Btree {
45642 sqlite3 *db; /* The database connection holding this btree */
45643 BtShared *pBt; /* Sharable content of this btree */
@@ -45723,19 +45718,20 @@
45723 u32 usableSize; /* Number of usable bytes on each page */
45724 int nTransaction; /* Number of open transactions (read + write) */
45725 u32 nPage; /* Number of pages in the database */
45726 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
45727 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
45728 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
45729 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
45730 #ifndef SQLITE_OMIT_SHARED_CACHE
45731 int nRef; /* Number of references to this structure */
45732 BtShared *pNext; /* Next on a list of sharable BtShared structs */
45733 BtLock *pLock; /* List of locks held on this shared-btree struct */
45734 Btree *pWriter; /* Btree with currently open write transaction */
45735 u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */
45736 u8 isPending; /* If waiting for read-locks to clear */
 
45737 #endif
45738 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
45739 };
45740
45741 /*
@@ -45964,18 +45960,37 @@
45964 /*
45965 ** Release the BtShared mutex associated with B-Tree handle p and
45966 ** clear the p->locked boolean.
45967 */
45968 static void unlockBtreeMutex(Btree *p){
 
45969 assert( p->locked==1 );
45970 assert( sqlite3_mutex_held(p->pBt->mutex) );
45971 assert( sqlite3_mutex_held(p->db->mutex) );
45972 assert( p->db==p->pBt->db );
45973
45974 sqlite3_mutex_leave(p->pBt->mutex);
 
45975 p->locked = 0;
45976 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45977
45978 /*
45979 ** Enter a mutex on the given BTree object.
45980 **
45981 ** If the object is not sharable, then no mutex is ever required
@@ -46016,10 +46031,28 @@
46016 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
46017
46018 if( !p->sharable ) return;
46019 p->wantToLock++;
46020 if( p->locked ) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46021
46022 /* In most cases, we should be able to acquire the lock we
46023 ** want without having to go throught the ascending lock
46024 ** procedure that follows. Just be sure not to block.
46025 */
@@ -46120,11 +46153,11 @@
46120 if( p && p->sharable ){
46121 p->wantToLock++;
46122 if( !p->locked ){
46123 assert( p->wantToLock==1 );
46124 while( p->pPrev ) p = p->pPrev;
46125 /* Reason for ALWAYS: There must be at least on unlocked Btree in
46126 ** the chain. Otherwise the !p->locked test above would have failed */
46127 while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
46128 for(pLater = p->pNext; pLater; pLater=pLater->pNext){
46129 if( pLater->locked ){
46130 unlockBtreeMutex(pLater);
@@ -46176,101 +46209,21 @@
46176 }
46177 return 1;
46178 }
46179 #endif /* NDEBUG */
46180
46181 /*
46182 ** Add a new Btree pointer to a BtreeMutexArray.
46183 ** if the pointer can possibly be shared with
46184 ** another database connection.
46185 **
46186 ** The pointers are kept in sorted order by pBtree->pBt. That
46187 ** way when we go to enter all the mutexes, we can enter them
46188 ** in order without every having to backup and retry and without
46189 ** worrying about deadlock.
46190 **
46191 ** The number of shared btrees will always be small (usually 0 or 1)
46192 ** so an insertion sort is an adequate algorithm here.
46193 */
46194 SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
46195 int i, j;
46196 BtShared *pBt;
46197 if( pBtree==0 || pBtree->sharable==0 ) return;
46198 #ifndef NDEBUG
46199 {
46200 for(i=0; i<pArray->nMutex; i++){
46201 assert( pArray->aBtree[i]!=pBtree );
46202 }
46203 }
46204 #endif
46205 assert( pArray->nMutex>=0 );
46206 assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
46207 pBt = pBtree->pBt;
46208 for(i=0; i<pArray->nMutex; i++){
46209 assert( pArray->aBtree[i]!=pBtree );
46210 if( pArray->aBtree[i]->pBt>pBt ){
46211 for(j=pArray->nMutex; j>i; j--){
46212 pArray->aBtree[j] = pArray->aBtree[j-1];
46213 }
46214 pArray->aBtree[i] = pBtree;
46215 pArray->nMutex++;
46216 return;
46217 }
46218 }
46219 pArray->aBtree[pArray->nMutex++] = pBtree;
46220 }
46221
46222 /*
46223 ** Enter the mutex of every btree in the array. This routine is
46224 ** called at the beginning of sqlite3VdbeExec(). The mutexes are
46225 ** exited at the end of the same function.
46226 */
46227 SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
46228 int i;
46229 for(i=0; i<pArray->nMutex; i++){
46230 Btree *p = pArray->aBtree[i];
46231 /* Some basic sanity checking */
46232 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
46233 assert( !p->locked || p->wantToLock>0 );
46234
46235 /* We should already hold a lock on the database connection */
46236 assert( sqlite3_mutex_held(p->db->mutex) );
46237
46238 /* The Btree is sharable because only sharable Btrees are entered
46239 ** into the array in the first place. */
46240 assert( p->sharable );
46241
46242 p->wantToLock++;
46243 if( !p->locked ){
46244 lockBtreeMutex(p);
46245 }
46246 }
46247 }
46248
46249 /*
46250 ** Leave the mutex of every btree in the group.
46251 */
46252 SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
46253 int i;
46254 for(i=0; i<pArray->nMutex; i++){
46255 Btree *p = pArray->aBtree[i];
46256 /* Some basic sanity checking */
46257 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
46258 assert( p->locked );
46259 assert( p->wantToLock>0 );
46260
46261 /* We should already hold a lock on the database connection */
46262 assert( sqlite3_mutex_held(p->db->mutex) );
46263
46264 p->wantToLock--;
46265 if( p->wantToLock==0 ){
46266 unlockBtreeMutex(p);
46267 }
46268 }
46269 }
46270
46271 #else
46272 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
46273 p->pBt->db = p->db;
46274 }
46275 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
46276 int i;
@@ -49444,15 +49397,26 @@
49444 ** routine did all the work of writing information out to disk and flushing the
49445 ** contents so that they are written onto the disk platter. All this
49446 ** routine has to do is delete or truncate or zero the header in the
49447 ** the rollback journal (which causes the transaction to commit) and
49448 ** drop locks.
 
 
 
 
 
 
 
 
 
 
 
49449 **
49450 ** This will release the write lock on the database file. If there
49451 ** are no active cursors, it also releases the read lock.
49452 */
49453 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){
49454
49455 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
49456 sqlite3BtreeEnter(p);
49457 btreeIntegrity(p);
49458
@@ -49463,11 +49427,11 @@
49463 int rc;
49464 BtShared *pBt = p->pBt;
49465 assert( pBt->inTransaction==TRANS_WRITE );
49466 assert( pBt->nTransaction>0 );
49467 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
49468 if( rc!=SQLITE_OK ){
49469 sqlite3BtreeLeave(p);
49470 return rc;
49471 }
49472 pBt->inTransaction = TRANS_READ;
49473 }
@@ -49483,11 +49447,11 @@
49483 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
49484 int rc;
49485 sqlite3BtreeEnter(p);
49486 rc = sqlite3BtreeCommitPhaseOne(p, 0);
49487 if( rc==SQLITE_OK ){
49488 rc = sqlite3BtreeCommitPhaseTwo(p);
49489 }
49490 sqlite3BtreeLeave(p);
49491 return rc;
49492 }
49493
@@ -54264,11 +54228,11 @@
54264 ** allocated, a null pointer is returned. If the blob has already been
54265 ** allocated, it is returned as normal.
54266 **
54267 ** Just before the shared-btree is closed, the function passed as the
54268 ** xFree argument when the memory allocation was made is invoked on the
54269 ** blob of allocated memory. This function should not call sqlite3_free()
54270 ** on the memory, the btree layer does that.
54271 */
54272 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
54273 BtShared *pBt = p->pBt;
54274 sqlite3BtreeEnter(p);
@@ -54907,11 +54871,11 @@
54907 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
54908 }
54909
54910 /* Finish committing the transaction to the destination database. */
54911 if( SQLITE_OK==rc
54912 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
54913 ){
54914 rc = SQLITE_DONE;
54915 }
54916 }
54917
@@ -54921,11 +54885,11 @@
54921 ** "committing" a read-only transaction cannot fail.
54922 */
54923 if( bCloseTrans ){
54924 TESTONLY( int rc2 );
54925 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
54926 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
54927 assert( rc2==SQLITE_OK );
54928 }
54929
54930 if( rc==SQLITE_IOERR_NOMEM ){
54931 rc = SQLITE_NOMEM;
@@ -56419,10 +56383,15 @@
56419 pOp->p2 = p2;
56420 pOp->p3 = p3;
56421 pOp->p4.p = 0;
56422 pOp->p4type = P4_NOTUSED;
56423 p->expired = 0;
 
 
 
 
 
56424 #ifdef SQLITE_DEBUG
56425 pOp->zComment = 0;
56426 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
56427 #endif
56428 #ifdef VDBE_PROFILE
@@ -56719,11 +56688,11 @@
56719 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
56720 VdbeOp *aOp = p->aOp;
56721 assert( aOp && !p->db->mallocFailed );
56722
56723 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
56724 assert( p->aMutex.nMutex==0 );
56725
56726 resolveP2Values(p, pnMaxArg);
56727 *pnOp = p->nOp;
56728 p->aOp = 0;
56729 return aOp;
@@ -56821,10 +56790,11 @@
56821 /*
56822 ** Change the P2 operand of instruction addr so that it points to
56823 ** the address of the next instruction to be coded.
56824 */
56825 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 
56826 sqlite3VdbeChangeP2(p, addr, p->nOp);
56827 }
56828
56829
56830 /*
@@ -57206,26 +57176,135 @@
57206 #endif
57207
57208 /*
57209 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
57210 **
57211 ** The prepared statement has to know in advance which Btree objects
57212 ** will be used so that it can acquire mutexes on them all in sorted
57213 ** order (via sqlite3VdbeMutexArrayEnter(). Mutexes are acquired
57214 ** in order (and released in reverse order) to avoid deadlocks.
57215 */
57216 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
57217 tAttachMask mask;
57218 assert( i>=0 && i<p->db->nDb && i<sizeof(tAttachMask)*8 );
57219 assert( i<(int)sizeof(p->btreeMask)*8 );
57220 mask = ((u32)1)<<i;
57221 if( (p->btreeMask & mask)==0 ){
57222 p->btreeMask |= mask;
57223 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
57224 }
57225 }
57226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57227
57228 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
57229 /*
57230 ** Print a single opcode. This routine is used for debugging only.
57231 */
@@ -57965,11 +58044,11 @@
57965 ** but could happen. In this case abandon processing and return the error.
57966 */
57967 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
57968 Btree *pBt = db->aDb[i].pBt;
57969 if( pBt ){
57970 rc = sqlite3BtreeCommitPhaseTwo(pBt);
57971 }
57972 }
57973 if( rc==SQLITE_OK ){
57974 sqlite3VtabCommit(db);
57975 }
@@ -58097,11 +58176,11 @@
58097 disable_simulated_io_errors();
58098 sqlite3BeginBenignMalloc();
58099 for(i=0; i<db->nDb; i++){
58100 Btree *pBt = db->aDb[i].pBt;
58101 if( pBt ){
58102 sqlite3BtreeCommitPhaseTwo(pBt);
58103 }
58104 }
58105 sqlite3EndBenignMalloc();
58106 enable_simulated_io_errors();
58107
@@ -58220,37 +58299,10 @@
58220 }
58221 }
58222 return rc;
58223 }
58224
58225 /*
58226 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
58227 ** this routine obtains the mutex associated with each BtShared structure
58228 ** that may be accessed by the VM passed as an argument. In doing so it
58229 ** sets the BtShared.db member of each of the BtShared structures, ensuring
58230 ** that the correct busy-handler callback is invoked if required.
58231 **
58232 ** If SQLite is not threadsafe but does support shared-cache mode, then
58233 ** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
58234 ** of all of BtShared structures accessible via the database handle
58235 ** associated with the VM. Of course only a subset of these structures
58236 ** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
58237 ** that subset out, but there is no advantage to doing so.
58238 **
58239 ** If SQLite is not threadsafe and does not support shared-cache mode, this
58240 ** function is a no-op.
58241 */
58242 #ifndef SQLITE_OMIT_SHARED_CACHE
58243 SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p){
58244 #if SQLITE_THREADSAFE
58245 sqlite3BtreeMutexArrayEnter(&p->aMutex);
58246 #else
58247 sqlite3BtreeEnterAll(p->db);
58248 #endif
58249 }
58250 #endif
58251
58252 /*
58253 ** This function is called when a transaction opened by the database
58254 ** handle associated with the VM passed as an argument is about to be
58255 ** committed. If there are outstanding deferred foreign key constraint
58256 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
@@ -58319,11 +58371,11 @@
58319 int mrc; /* Primary error code from p->rc */
58320 int eStatementOp = 0;
58321 int isSpecialError; /* Set to true if a 'special' error */
58322
58323 /* Lock all btrees used by the statement */
58324 sqlite3VdbeMutexArrayEnter(p);
58325
58326 /* Check for one of the special errors */
58327 mrc = p->rc & 0xff;
58328 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
58329 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
@@ -58373,11 +58425,11 @@
58373 ){
58374 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
58375 rc = sqlite3VdbeCheckFk(p, 1);
58376 if( rc!=SQLITE_OK ){
58377 if( NEVER(p->readOnly) ){
58378 sqlite3BtreeMutexArrayLeave(&p->aMutex);
58379 return SQLITE_ERROR;
58380 }
58381 rc = SQLITE_CONSTRAINT;
58382 }else{
58383 /* The auto-commit flag is true, the vdbe program was successful
@@ -58385,11 +58437,11 @@
58385 ** key constraints to hold up the transaction. This means a commit
58386 ** is required. */
58387 rc = vdbeCommit(db, p);
58388 }
58389 if( rc==SQLITE_BUSY && p->readOnly ){
58390 sqlite3BtreeMutexArrayLeave(&p->aMutex);
58391 return SQLITE_BUSY;
58392 }else if( rc!=SQLITE_OK ){
58393 p->rc = rc;
58394 sqlite3RollbackAll(db);
58395 }else{
@@ -58457,11 +58509,12 @@
58457 sqlite3ResetInternalSchema(db, 0);
58458 db->flags = (db->flags | SQLITE_InternChanges);
58459 }
58460
58461 /* Release the locks */
58462 sqlite3BtreeMutexArrayLeave(&p->aMutex);
 
58463 }
58464
58465 /* We have successfully halted and closed the VM. Record this fact. */
58466 if( p->pc>=0 ){
58467 db->activeVdbeCnt--;
@@ -61978,11 +62031,11 @@
61978 } u;
61979 /* End automatically generated code
61980 ********************************************************************/
61981
61982 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
61983 sqlite3VdbeMutexArrayEnter(p);
61984 if( p->rc==SQLITE_NOMEM ){
61985 /* This happens if a malloc() inside a call to sqlite3_column_text() or
61986 ** sqlite3_column_text16() failed. */
61987 goto no_mem;
61988 }
@@ -62815,19 +62868,31 @@
62815 assert( pOp[-1].p4type==P4_COLLSEQ );
62816 assert( pOp[-1].opcode==OP_CollSeq );
62817 u.ag.ctx.pColl = pOp[-1].p4.pColl;
62818 }
62819 (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
 
62820 if( db->mallocFailed ){
62821 /* Even though a malloc() has failed, the implementation of the
62822 ** user function may have called an sqlite3_result_XXX() function
62823 ** to return a value. The following call releases any resources
62824 ** associated with such a value.
62825 */
62826 sqlite3VdbeMemRelease(&u.ag.ctx.s);
62827 goto no_mem;
62828 }
 
 
 
 
 
 
 
 
 
 
 
62829
62830 /* If any auxiliary data functions have been called by this user function,
62831 ** immediately call the destructor for any non-static values.
62832 */
62833 if( u.ag.ctx.pVdbeFunc ){
@@ -64091,10 +64156,11 @@
64091 }
64092 }
64093 if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
64094 sqlite3ExpirePreparedStatements(db);
64095 sqlite3ResetInternalSchema(db, 0);
 
64096 db->flags = (db->flags | SQLITE_InternChanges);
64097 }
64098 }
64099
64100 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
@@ -64234,11 +64300,11 @@
64234 #if 0 /* local variables moved into u.as */
64235 Btree *pBt;
64236 #endif /* local variables moved into u.as */
64237
64238 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64239 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64240 u.as.pBt = db->aDb[pOp->p1].pBt;
64241
64242 if( u.as.pBt ){
64243 rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
64244 if( rc==SQLITE_BUSY ){
@@ -64292,11 +64358,11 @@
64292 u.at.iDb = pOp->p1;
64293 u.at.iCookie = pOp->p3;
64294 assert( pOp->p3<SQLITE_N_BTREE_META );
64295 assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
64296 assert( db->aDb[u.at.iDb].pBt!=0 );
64297 assert( (p->btreeMask & (1<<u.at.iDb))!=0 );
64298
64299 sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
64300 pOut->u.i = u.at.iMeta;
64301 break;
64302 }
@@ -64315,11 +64381,11 @@
64315 #if 0 /* local variables moved into u.au */
64316 Db *pDb;
64317 #endif /* local variables moved into u.au */
64318 assert( pOp->p2<SQLITE_N_BTREE_META );
64319 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64320 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64321 u.au.pDb = &db->aDb[pOp->p1];
64322 assert( u.au.pDb->pBt!=0 );
64323 pIn3 = &aMem[pOp->p3];
64324 sqlite3VdbeMemIntegerify(pIn3);
64325 /* See note about index shifting on OP_ReadCookie */
@@ -64365,11 +64431,11 @@
64365 int iGen;
64366 Btree *pBt;
64367 #endif /* local variables moved into u.av */
64368
64369 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64370 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
64371 u.av.pBt = db->aDb[pOp->p1].pBt;
64372 if( u.av.pBt ){
64373 sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
64374 u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
64375 }else{
@@ -64391,10 +64457,11 @@
64391 ** to be invalidated whenever sqlite3_step() is called from within
64392 ** a v-table method.
64393 */
64394 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
64395 sqlite3ResetInternalSchema(db, pOp->p1);
 
64396 }
64397
64398 p->expired = 1;
64399 rc = SQLITE_SCHEMA;
64400 }
@@ -64471,11 +64538,11 @@
64471 u.aw.nField = 0;
64472 u.aw.pKeyInfo = 0;
64473 u.aw.p2 = pOp->p2;
64474 u.aw.iDb = pOp->p3;
64475 assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
64476 assert( (p->btreeMask & (1<<u.aw.iDb))!=0 );
64477 u.aw.pDb = &db->aDb[u.aw.iDb];
64478 u.aw.pX = u.aw.pDb->pBt;
64479 assert( u.aw.pX!=0 );
64480 if( pOp->opcode==OP_OpenWrite ){
64481 u.aw.wrFlag = 1;
@@ -66008,11 +66075,11 @@
66008 rc = SQLITE_LOCKED;
66009 p->errorAction = OE_Abort;
66010 }else{
66011 u.br.iDb = pOp->p3;
66012 assert( u.br.iCnt==1 );
66013 assert( (p->btreeMask & (1<<u.br.iDb))!=0 );
66014 rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
66015 pOut->flags = MEM_Int;
66016 pOut->u.i = u.br.iMoved;
66017 #ifndef SQLITE_OMIT_AUTOVACUUM
66018 if( rc==SQLITE_OK && u.br.iMoved!=0 ){
@@ -66046,11 +66113,11 @@
66046 #if 0 /* local variables moved into u.bs */
66047 int nChange;
66048 #endif /* local variables moved into u.bs */
66049
66050 u.bs.nChange = 0;
66051 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
66052 rc = sqlite3BtreeClearTable(
66053 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
66054 );
66055 if( pOp->p3 ){
66056 p->nChange += u.bs.nChange;
@@ -66093,11 +66160,11 @@
66093 Db *pDb;
66094 #endif /* local variables moved into u.bt */
66095
66096 u.bt.pgno = 0;
66097 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66098 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66099 u.bt.pDb = &db->aDb[pOp->p1];
66100 assert( u.bt.pDb->pBt!=0 );
66101 if( pOp->opcode==OP_CreateTable ){
66102 /* u.bt.flags = BTREE_INTKEY; */
66103 u.bt.flags = BTREE_INTKEY;
@@ -66122,31 +66189,27 @@
66122 int iDb;
66123 const char *zMaster;
66124 char *zSql;
66125 InitData initData;
66126 #endif /* local variables moved into u.bu */
 
 
 
 
 
 
 
 
 
 
 
 
66127
66128 u.bu.iDb = pOp->p1;
66129 assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
66130
66131 /* Although the mutex on the BtShared object that corresponds to
66132 ** database u.bu.iDb (the database containing the sqlite_master table
66133 ** read by this instruction) is currently held, it is necessary to
66134 ** obtain the mutexes on all attached databases before checking if
66135 ** the schema of u.bu.iDb is loaded. This is because, at the start of
66136 ** the sqlite3_exec() call below, SQLite will invoke
66137 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
66138 ** u.bu.iDb mutex may be temporarily released to avoid deadlock. If
66139 ** this happens, then some other thread may delete the in-memory
66140 ** schema of database u.bu.iDb before the SQL statement runs. The schema
66141 ** will not be reloaded becuase the db->init.busy flag is set. This
66142 ** can result in a "no such table: sqlite_master" or "malformed
66143 ** database schema" error being returned to the user.
66144 */
66145 assert( sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
66146 sqlite3BtreeEnterAll(db);
66147 if( ALWAYS(DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded)) ){
66148 u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
66149 u.bu.initData.db = db;
66150 u.bu.initData.iDb = pOp->p1;
66151 u.bu.initData.pzErrMsg = &p->zErrMsg;
66152 u.bu.zSql = sqlite3MPrintf(db,
@@ -66163,11 +66226,10 @@
66163 if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
66164 sqlite3DbFree(db, u.bu.zSql);
66165 db->init.busy = 0;
66166 }
66167 }
66168 sqlite3BtreeLeaveAll(db);
66169 if( rc==SQLITE_NOMEM ){
66170 goto no_mem;
66171 }
66172 break;
66173 }
@@ -66266,11 +66328,11 @@
66266 for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
66267 u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
66268 }
66269 u.bv.aRoot[u.bv.j] = 0;
66270 assert( pOp->p5<db->nDb );
66271 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
66272 u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
66273 (int)u.bv.pnErr->u.i, &u.bv.nErr);
66274 sqlite3DbFree(db, u.bv.aRoot);
66275 u.bv.pnErr->u.i -= u.bv.nErr;
66276 sqlite3VdbeMemSetNull(pIn1);
@@ -66702,15 +66764,29 @@
66702 assert( pOp[-1].p4type==P4_COLLSEQ );
66703 assert( pOp[-1].opcode==OP_CollSeq );
66704 u.cb.ctx.pColl = pOp[-1].p4.pColl;
66705 }
66706 (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
 
66707 if( u.cb.ctx.isError ){
66708 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
66709 rc = u.cb.ctx.isError;
66710 }
 
 
 
 
 
 
 
 
 
 
 
 
66711 sqlite3VdbeMemRelease(&u.cb.ctx.s);
 
66712 break;
66713 }
66714
66715 /* Opcode: AggFinal P1 P2 * P4 *
66716 **
@@ -66730,12 +66806,15 @@
66730 #endif /* local variables moved into u.cc */
66731 assert( pOp->p1>0 && pOp->p1<=p->nMem );
66732 u.cc.pMem = &aMem[pOp->p1];
66733 assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
66734 rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
 
66735 if( rc ){
66736 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
 
 
66737 }
66738 sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
66739 UPDATE_MAX_BLOBSIZE(u.cc.pMem);
66740 if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
66741 goto too_big;
@@ -66812,25 +66891,24 @@
66812 );
66813 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66814
66815 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
66816 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
66817 ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
66818 ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
66819 ** is not called when the statement is prepared because it requires the
66820 ** iDb index of the database as a parameter, and the database has not
66821 ** yet been attached so that index is unavailable. We have to wait
66822 ** until runtime (now) to get the mutex on the newly attached database.
66823 ** No other mutexes are required by the ATTACH command so this is safe
66824 ** to do.
66825 */
66826 assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
66827 if( p->aMutex.nMutex==0 ){
66828 /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
66829 ** database. */
66830 sqlite3VdbeUsesBtree(p, pOp->p1);
66831 sqlite3VdbeMutexArrayEnter(p);
66832 }
66833
66834 u.ce.pBt = db->aDb[pOp->p1].pBt;
66835 u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
66836 u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
@@ -66928,11 +67006,11 @@
66928 #if 0 /* local variables moved into u.cf */
66929 Btree *pBt;
66930 #endif /* local variables moved into u.cf */
66931
66932 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66933 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66934 u.cf.pBt = db->aDb[pOp->p1].pBt;
66935 rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
66936 if( rc==SQLITE_DONE ){
66937 pc = pOp->p2 - 1;
66938 rc = SQLITE_OK;
@@ -66977,11 +67055,11 @@
66977 case OP_TableLock: {
66978 u8 isWriteLock = (u8)pOp->p3;
66979 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
66980 int p1 = pOp->p1;
66981 assert( p1>=0 && p1<db->nDb );
66982 assert( (p->btreeMask & (1<<p1))!=0 );
66983 assert( isWriteLock==0 || isWriteLock==1 );
66984 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
66985 if( (rc&0xFF)==SQLITE_LOCKED ){
66986 const char *z = pOp->p4.z;
66987 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -67482,17 +67560,20 @@
67482 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
67483 pc, p->zSql, p->zErrMsg);
67484 sqlite3VdbeHalt(p);
67485 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
67486 rc = SQLITE_ERROR;
67487 if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
 
 
 
67488
67489 /* This is the only way out of this procedure. We have to
67490 ** release the mutexes on btrees that were acquired at the
67491 ** top. */
67492 vdbe_return:
67493 sqlite3BtreeMutexArrayLeave(&p->aMutex);
67494 return rc;
67495
67496 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
67497 ** is encountered.
67498 */
@@ -73966,10 +74047,26 @@
73966 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
73967 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
73968 }
73969 #endif
73970 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73971
73972 /*
73973 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
73974 ** command.
73975 */
@@ -74017,18 +74114,15 @@
74017 }
74018
74019 /* Make sure it is not a system table being altered, or a reserved name
74020 ** that the table is being renamed to.
74021 */
74022 if( sqlite3Strlen30(pTab->zName)>6
74023 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
74024 ){
74025 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
74026 goto exit_rename_table;
74027 }
74028 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
74029 goto exit_rename_table;
74030 }
74031
74032 #ifndef SQLITE_OMIT_VIEW
74033 if( pTab->pSelect ){
74034 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
@@ -74356,10 +74450,13 @@
74356 /* Make sure this is not an attempt to ALTER a view. */
74357 if( pTab->pSelect ){
74358 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
74359 goto exit_begin_add_column;
74360 }
 
 
 
74361
74362 assert( pTab->addColOffset>0 );
74363 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
74364
74365 /* Put a copy of the Table struct in Parse.pNewTable for the
@@ -74443,11 +74540,12 @@
74443 */
74444 static void openStatTable(
74445 Parse *pParse, /* Parsing context */
74446 int iDb, /* The database we are looking in */
74447 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
74448 const char *zWhere /* Delete entries associated with this table */
 
74449 ){
74450 static const struct {
74451 const char *zName;
74452 const char *zCols;
74453 } aTable[] = {
@@ -74488,11 +74586,11 @@
74488 ** entire contents of the table. */
74489 aRoot[i] = pStat->tnum;
74490 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
74491 if( zWhere ){
74492 sqlite3NestedParse(pParse,
74493 "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
74494 );
74495 }else{
74496 /* The sqlite_stat[12] table already exists. Delete all rows. */
74497 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
74498 }
@@ -74512,10 +74610,11 @@
74512 ** a single table.
74513 */
74514 static void analyzeOneTable(
74515 Parse *pParse, /* Parser context */
74516 Table *pTab, /* Table whose indices are to be analyzed */
 
74517 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
74518 int iMem /* Available memory locations begin here */
74519 ){
74520 sqlite3 *db = pParse->db; /* Database handle */
74521 Index *pIdx; /* An index to being analyzed */
@@ -74522,12 +74621,11 @@
74522 int iIdxCur; /* Cursor open on index being analyzed */
74523 Vdbe *v; /* The virtual machine being built up */
74524 int i; /* Loop counter */
74525 int topOfLoop; /* The top of the loop */
74526 int endOfLoop; /* The end of the loop */
74527 int addr = 0; /* The address of an instruction */
74528 int jZeroRows = 0; /* Jump from here if number of rows is zero */
74529 int iDb; /* Index of database containing pTab */
74530 int regTabname = iMem++; /* Register containing table name */
74531 int regIdxname = iMem++; /* Register containing index name */
74532 int regSampleno = iMem++; /* Register containing next sample number */
74533 int regCol = iMem++; /* Content of a column analyzed table */
@@ -74534,10 +74632,11 @@
74534 int regRec = iMem++; /* Register holding completed record */
74535 int regTemp = iMem++; /* Temporary use register */
74536 int regRowid = iMem++; /* Rowid for the inserted record */
74537
74538 #ifdef SQLITE_ENABLE_STAT2
 
74539 int regTemp2 = iMem++; /* Temporary use register */
74540 int regSamplerecno = iMem++; /* Index of next sample to record */
74541 int regRecno = iMem++; /* Current sample index */
74542 int regLast = iMem++; /* Index of last sample to record */
74543 int regFirst = iMem++; /* Index of first sample to record */
@@ -74569,13 +74668,16 @@
74569 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
74570
74571 iIdxCur = pParse->nTab++;
74572 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
74573 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
74574 int nCol = pIdx->nColumn;
74575 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
74576
 
 
 
74577 if( iMem+1+(nCol*2)>pParse->nMem ){
74578 pParse->nMem = iMem+1+(nCol*2);
74579 }
74580
74581 /* Open a cursor to the index to be analyzed. */
@@ -74728,11 +74830,11 @@
74728 ** If K==0 then no entry is made into the sqlite_stat1 table.
74729 ** If K>0 then it is always the case the D>0 so division by zero
74730 ** is never possible.
74731 */
74732 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
74733 if( jZeroRows==0 ){
74734 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
74735 }
74736 for(i=0; i<nCol; i++){
74737 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
74738 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
@@ -74754,24 +74856,22 @@
74754 if( pTab->pIndex==0 ){
74755 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
74756 VdbeComment((v, "%s", pTab->zName));
74757 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
74758 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 
74759 }else{
74760 assert( jZeroRows>0 );
74761 addr = sqlite3VdbeAddOp0(v, OP_Goto);
74762 sqlite3VdbeJumpHere(v, jZeroRows);
 
74763 }
74764 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
74765 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
74766 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
74767 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
74768 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
74769 if( pParse->nMem<regRec ) pParse->nMem = regRec;
74770 if( jZeroRows ){
74771 sqlite3VdbeJumpHere(v, addr);
74772 }
74773 }
74774
74775 /*
74776 ** Generate code that will cause the most recent index analysis to
74777 ** be loaded into internal hash tables where is can be used.
@@ -74794,35 +74894,40 @@
74794 int iMem;
74795
74796 sqlite3BeginWriteOperation(pParse, 0, iDb);
74797 iStatCur = pParse->nTab;
74798 pParse->nTab += 2;
74799 openStatTable(pParse, iDb, iStatCur, 0);
74800 iMem = pParse->nMem+1;
74801 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
74802 Table *pTab = (Table*)sqliteHashData(k);
74803 analyzeOneTable(pParse, pTab, iStatCur, iMem);
74804 }
74805 loadAnalysis(pParse, iDb);
74806 }
74807
74808 /*
74809 ** Generate code that will do an analysis of a single table in
74810 ** a database.
 
74811 */
74812 static void analyzeTable(Parse *pParse, Table *pTab){
74813 int iDb;
74814 int iStatCur;
74815
74816 assert( pTab!=0 );
74817 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
74818 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
74819 sqlite3BeginWriteOperation(pParse, 0, iDb);
74820 iStatCur = pParse->nTab;
74821 pParse->nTab += 2;
74822 openStatTable(pParse, iDb, iStatCur, pTab->zName);
74823 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
 
 
 
 
74824 loadAnalysis(pParse, iDb);
74825 }
74826
74827 /*
74828 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -74840,10 +74945,11 @@
74840 sqlite3 *db = pParse->db;
74841 int iDb;
74842 int i;
74843 char *z, *zDb;
74844 Table *pTab;
 
74845 Token *pTableName;
74846
74847 /* Read the database schema. If an error occurs, leave an error message
74848 ** and code in pParse and return NULL. */
74849 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
@@ -74864,29 +74970,31 @@
74864 if( iDb>=0 ){
74865 analyzeDatabase(pParse, iDb);
74866 }else{
74867 z = sqlite3NameFromToken(db, pName1);
74868 if( z ){
74869 pTab = sqlite3LocateTable(pParse, 0, z, 0);
 
 
 
 
74870 sqlite3DbFree(db, z);
74871 if( pTab ){
74872 analyzeTable(pParse, pTab);
74873 }
74874 }
74875 }
74876 }else{
74877 /* Form 3: Analyze the fully qualified table name */
74878 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
74879 if( iDb>=0 ){
74880 zDb = db->aDb[iDb].zName;
74881 z = sqlite3NameFromToken(db, pTableName);
74882 if( z ){
74883 pTab = sqlite3LocateTable(pParse, 0, z, zDb);
 
 
 
 
74884 sqlite3DbFree(db, z);
74885 if( pTab ){
74886 analyzeTable(pParse, pTab);
74887 }
74888 }
74889 }
74890 }
74891 }
74892
@@ -76055,11 +76163,11 @@
76055 ** set for each database that is used. Generate code to start a
76056 ** transaction on each used database and to verify the schema cookie
76057 ** on each used database.
76058 */
76059 if( pParse->cookieGoto>0 ){
76060 tAttachMask mask;
76061 int iDb;
76062 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
76063 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
76064 if( (mask & pParse->cookieMask)==0 ) continue;
76065 sqlite3VdbeUsesBtree(v, iDb);
@@ -79351,16 +79459,16 @@
79351 if( v==0 ) return; /* This only happens if there was a prior error */
79352 pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
79353 }
79354 if( iDb>=0 ){
79355 sqlite3 *db = pToplevel->db;
79356 tAttachMask mask;
79357
79358 assert( iDb<db->nDb );
79359 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
79360 assert( iDb<SQLITE_MAX_ATTACHED+2 );
79361 mask = ((tAttachMask)1)<<iDb;
79362 if( (pToplevel->cookieMask & mask)==0 ){
79363 pToplevel->cookieMask |= mask;
79364 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
79365 if( !OMIT_TEMPDB && iDb==1 ){
79366 sqlite3OpenTempDatabase(pToplevel);
@@ -79383,11 +79491,11 @@
79383 ** necessary to undo a write and the checkpoint should not be set.
79384 */
79385 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
79386 Parse *pToplevel = sqlite3ParseToplevel(pParse);
79387 sqlite3CodeVerifySchema(pParse, iDb);
79388 pToplevel->writeMask |= ((tAttachMask)1)<<iDb;
79389 pToplevel->isMultiWrite |= setStatement;
79390 }
79391
79392 /*
79393 ** Indicate that the statement currently under construction might write
@@ -99567,11 +99675,11 @@
99567 ** VALUE and how common that value is according to the histogram.
99568 */
99569 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
99570 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
99571 testcase( pFirstTerm->eOperator==WO_EQ );
99572 testcase( pFirstTerm->pOperator==WO_ISNULL );
99573 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
99574 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
99575 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
99576 }
99577 }
99578
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,9 +1,9 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.6. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
9 **
@@ -650,11 +650,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.6"
654 #define SQLITE_VERSION_NUMBER 3007006
655 #define SQLITE_SOURCE_ID "2011-04-04 03:27:16 f8e98ab3062a6e56924a86e8f3204c30d0f3d906"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -3521,11 +3521,13 @@
3521 ** UTF-16 string. ^The first parameter is the [prepared statement]
3522 ** that implements the [SELECT] statement. ^The second parameter is the
3523 ** column number. ^The leftmost column is number 0.
3524 **
3525 ** ^The returned string pointer is valid until either the [prepared statement]
3526 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3527 ** reprepared by the first call to [sqlite3_step()] for a particular run
3528 ** or until the next call to
3529 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3530 **
3531 ** ^If sqlite3_malloc() fails during the processing of either routine
3532 ** (for example during a conversion from UTF-8 to UTF-16) then a
3533 ** NULL pointer is returned.
@@ -3547,11 +3549,13 @@
3549 ** ^The name of the database or table or column can be returned as
3550 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
3551 ** the database name, the _table_ routines return the table name, and
3552 ** the origin_ routines return the column name.
3553 ** ^The returned string is valid until the [prepared statement] is destroyed
3554 ** using [sqlite3_finalize()] or until the statement is automatically
3555 ** reprepared by the first call to [sqlite3_step()] for a particular run
3556 ** or until the same information is requested
3557 ** again in a different encoding.
3558 **
3559 ** ^The names returned are the original un-aliased names of the
3560 ** database, table, and column.
3561 **
@@ -7648,22 +7652,10 @@
7652 ** Forward declarations of structure
7653 */
7654 typedef struct Btree Btree;
7655 typedef struct BtCursor BtCursor;
7656 typedef struct BtShared BtShared;
 
 
 
 
 
 
 
 
 
 
 
 
7657
7658
7659 SQLITE_PRIVATE int sqlite3BtreeOpen(
7660 const char *zFilename, /* Name of database file to open */
7661 sqlite3 *db, /* Associated database connection */
@@ -7696,11 +7688,11 @@
7688 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
7689 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
7690 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
7691 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
7692 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7693 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
7694 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
7695 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
7696 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
7697 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
7698 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
@@ -7837,27 +7829,23 @@
7829 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
7830 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
7831 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
7832 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
7833 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
 
 
 
7834 #ifndef NDEBUG
7835 /* These routines are used inside assert() statements only. */
7836 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
7837 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
7838 SQLITE_PRIVATE u32 sqlite3BtreeMutexCounter(Btree*);
7839 #endif
7840 #else
7841
7842 # define sqlite3BtreeLeave(X)
7843 # define sqlite3BtreeMutexCounter(X) 0
7844 # define sqlite3BtreeEnterCursor(X)
7845 # define sqlite3BtreeLeaveCursor(X)
7846 # define sqlite3BtreeLeaveAll(X)
 
 
 
7847
7848 # define sqlite3BtreeHoldsMutex(X) 1
7849 # define sqlite3BtreeHoldsAllMutexes(X) 1
7850 #endif
7851
@@ -10467,15 +10455,17 @@
10455 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
10456 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
10457 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
10458 };
10459
10460 /*
10461 ** The yDbMask datatype for the bitmask of all attached databases.
10462 */
10463 #if SQLITE_MAX_ATTACHED>30
10464 typedef sqlite3_uint64 yDbMask;
10465 #else
10466 typedef unsigned int yDbMask;
10467 #endif
10468
10469 /*
10470 ** An SQL parser context. A copy of this structure is passed through
10471 ** the parser and down into all the parser action routine in order to
@@ -10522,12 +10512,12 @@
10512 u8 tempReg; /* iReg is a temp register that needs to be freed */
10513 int iLevel; /* Nesting level */
10514 int iReg; /* Reg with value of this column. 0 means none. */
10515 int lru; /* Least recently used entry has the smallest value */
10516 } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
10517 yDbMask writeMask; /* Start a write transaction on these databases */
10518 yDbMask cookieMask; /* Bitmask of schema verified databases */
10519 u8 isMultiWrite; /* True if statement may affect/insert multiple rows */
10520 u8 mayAbort; /* True if statement may throw an ABORT exception */
10521 int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
10522 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
10523 #ifndef SQLITE_OMIT_SHARED_CACHE
@@ -12488,14 +12478,14 @@
12478 u8 inVtabMethod; /* See comments above */
12479 u8 usesStmtJournal; /* True if uses a statement journal */
12480 u8 readOnly; /* True for read-only statements */
12481 u8 isPrepareV2; /* True if prepared with prepare_v2() */
12482 int nChange; /* Number of db changes made since last reset */
12483 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
12484 u32 iMutexCounter; /* Mutex counter upon sqlite3VdbeEnter() */
12485 int iStatement; /* Statement number (or 0 if has not opened stmt) */
12486 int aCounter[3]; /* Counters used by sqlite3_stmt_status() */
 
12487 #ifndef SQLITE_OMIT_TRACE
12488 i64 startTime; /* Time when query started - used for profiling */
12489 #endif
12490 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
12491 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -12573,10 +12563,13 @@
12563 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12564 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12565 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12566 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12567 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12568 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12569 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12570 SQLITE_PRIVATE void sqlite3VdbeMutexResync(Vdbe*);
12571
12572 #ifdef SQLITE_DEBUG
12573 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
12574 #endif
12575
@@ -12584,16 +12577,10 @@
12577 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
12578 #else
12579 # define sqlite3VdbeCheckFk(p,i) 0
12580 #endif
12581
 
 
 
 
 
 
12582 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
12583 #ifdef SQLITE_DEBUG
12584 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
12585 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
12586 #endif
@@ -19794,11 +19781,11 @@
19781 }
19782 SQLITE_PRIVATE int sqlite3Utf8Read(
19783 const unsigned char *zIn, /* First byte of UTF-8 character */
19784 const unsigned char **pzNext /* Write first byte past UTF-8 char here */
19785 ){
19786 unsigned int c;
19787
19788 /* Same as READ_UTF8() above but without the zTerm parameter.
19789 ** For this routine, we assume the UTF8 string is always zero-terminated.
19790 */
19791 c = *(zIn++);
@@ -20037,19 +20024,19 @@
20024 ** Translate UTF-8 to UTF-8.
20025 **
20026 ** This has the effect of making sure that the string is well-formed
20027 ** UTF-8. Miscoded characters are removed.
20028 **
20029 ** The translation is done in-place and aborted if the output
20030 ** overruns the input.
20031 */
20032 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
20033 unsigned char *zOut = zIn;
20034 unsigned char *zStart = zIn;
20035 u32 c;
20036
20037 while( zIn[0] && zOut<=zIn ){
20038 c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
20039 if( c!=0xfffd ){
20040 WRITE_UTF8(zOut, c);
20041 }
20042 }
@@ -23750,11 +23737,11 @@
23737 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
23738 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
23739 #else
23740 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
23741 #endif
23742 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
23743
23744 }; /* End of the overrideable system calls */
23745
23746 /*
23747 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23773,14 +23760,14 @@
23760 UNUSED_PARAMETER(pNotUsed);
23761 if( zName==0 ){
23762 /* If no zName is given, restore all system calls to their default
23763 ** settings and return NULL
23764 */
23765 rc = SQLITE_OK;
23766 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
23767 if( aSyscall[i].pDefault ){
23768 aSyscall[i].pCurrent = aSyscall[i].pDefault;
 
23769 }
23770 }
23771 }else{
23772 /* If zName is specified, operate on only the one system call
23773 ** specified.
@@ -23823,22 +23810,20 @@
23810 ** then return the name of the first system call. Return NULL if zName
23811 ** is the last system call or if zName is not the name of a valid
23812 ** system call.
23813 */
23814 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
23815 int i = -1;
23816
23817 UNUSED_PARAMETER(p);
23818 if( zName ){
23819 for(i=0; i<ArraySize(aSyscall)-1; i++){
23820 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 
 
23821 }
23822 }
23823 for(i++; i<ArraySize(aSyscall); i++){
23824 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
23825 }
23826 return 0;
23827 }
23828
23829 /*
@@ -23974,13 +23959,26 @@
23959 ** Errors during initialization of locks, or file system support for locks,
23960 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
23961 */
23962 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
23963 switch (posixError) {
23964 #if 0
23965 /* At one point this code was not commented out. In theory, this branch
23966 ** should never be hit, as this function should only be called after
23967 ** a locking-related function (i.e. fcntl()) has returned non-zero with
23968 ** the value of errno as the first argument. Since a system call has failed,
23969 ** errno should be non-zero.
23970 **
23971 ** Despite this, if errno really is zero, we still don't want to return
23972 ** SQLITE_OK. The system call failed, and *some* SQLite error should be
23973 ** propagated back to the caller. Commenting this branch out means errno==0
23974 ** will be handled by the "default:" case below.
23975 */
23976 case 0:
23977 return SQLITE_OK;
23978 #endif
23979
23980 case EAGAIN:
23981 case ETIMEDOUT:
23982 case EBUSY:
23983 case EINTR:
23984 case ENOLCK:
@@ -23998,12 +23996,19 @@
23996 }
23997 /* else fall through */
23998 case EPERM:
23999 return SQLITE_PERM;
24000
24001 /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24002 ** this module never makes such a call. And the code in SQLite itself
24003 ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24004 ** this case is also commented out. If the system does set errno to EDEADLK,
24005 ** the default SQLITE_IOERR_XXX code will be returned. */
24006 #if 0
24007 case EDEADLK:
24008 return SQLITE_IOERR_BLOCKED;
24009 #endif
24010
24011 #if EOPNOTSUPP!=ENOTSUP
24012 case EOPNOTSUPP:
24013 /* something went terribly awry, unless during file system support
24014 * introspection, in which it actually means what it says */
@@ -24418,11 +24423,11 @@
24423 ** when this function is called.
24424 */
24425 static void releaseInodeInfo(unixFile *pFile){
24426 unixInodeInfo *pInode = pFile->pInode;
24427 assert( unixMutexHeld() );
24428 if( ALWAYS(pInode) ){
24429 pInode->nRef--;
24430 if( pInode->nRef==0 ){
24431 assert( pInode->pShmNode==0 );
24432 closePendingFds(pFile);
24433 if( pInode->pPrev ){
@@ -24560,14 +24565,13 @@
24565 struct flock lock;
24566 lock.l_whence = SEEK_SET;
24567 lock.l_start = RESERVED_BYTE;
24568 lock.l_len = 1;
24569 lock.l_type = F_WRLCK;
24570 if( osFcntl(pFile->h, F_GETLK, &lock) ){
24571 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
24572 pFile->lastErrno = errno;
 
24573 } else if( lock.l_type!=F_UNLCK ){
24574 reserved = 1;
24575 }
24576 }
24577 #endif
@@ -24592,10 +24596,13 @@
24596 ** operating system does not participate.
24597 **
24598 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
24599 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
24600 ** and is read-only.
24601 **
24602 ** Zero is returned if the call completes successfully, or -1 if a call
24603 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
24604 */
24605 static int unixFileLock(unixFile *pFile, struct flock *pLock){
24606 int rc;
24607 unixInodeInfo *pInode = pFile->pInode;
24608 assert( unixMutexHeld() );
@@ -24688,11 +24695,10 @@
24695 */
24696 int rc = SQLITE_OK;
24697 unixFile *pFile = (unixFile*)id;
24698 unixInodeInfo *pInode = pFile->pInode;
24699 struct flock lock;
 
24700 int tErrno = 0;
24701
24702 assert( pFile );
24703 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
24704 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
@@ -24757,15 +24763,14 @@
24763 if( eFileLock==SHARED_LOCK
24764 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
24765 ){
24766 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
24767 lock.l_start = PENDING_BYTE;
24768 if( unixFileLock(pFile, &lock) ){
 
24769 tErrno = errno;
24770 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24771 if( rc!=SQLITE_BUSY ){
24772 pFile->lastErrno = tErrno;
24773 }
24774 goto end_lock;
24775 }
24776 }
@@ -24775,37 +24780,35 @@
24780 ** operating system calls for the specified lock.
24781 */
24782 if( eFileLock==SHARED_LOCK ){
24783 assert( pInode->nShared==0 );
24784 assert( pInode->eFileLock==0 );
24785 assert( rc==SQLITE_OK );
24786
24787 /* Now get the read-lock */
24788 lock.l_start = SHARED_FIRST;
24789 lock.l_len = SHARED_SIZE;
24790 if( unixFileLock(pFile, &lock) ){
24791 tErrno = errno;
24792 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24793 }
24794
24795 /* Drop the temporary PENDING lock */
24796 lock.l_start = PENDING_BYTE;
24797 lock.l_len = 1L;
24798 lock.l_type = F_UNLCK;
24799 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
24800 /* This could happen with a network mount */
24801 tErrno = errno;
24802 rc = SQLITE_IOERR_UNLOCK;
24803 }
24804
24805 if( rc ){
24806 if( rc!=SQLITE_BUSY ){
24807 pFile->lastErrno = tErrno;
24808 }
24809 goto end_lock;
 
 
 
 
 
24810 }else{
24811 pFile->eFileLock = SHARED_LOCK;
24812 pInode->nLock++;
24813 pInode->nShared = 1;
24814 }
@@ -24818,26 +24821,24 @@
24821 ** assumed that there is a SHARED or greater lock on the file
24822 ** already.
24823 */
24824 assert( 0!=pFile->eFileLock );
24825 lock.l_type = F_WRLCK;
24826
24827 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
24828 if( eFileLock==RESERVED_LOCK ){
24829 lock.l_start = RESERVED_BYTE;
24830 lock.l_len = 1L;
24831 }else{
24832 lock.l_start = SHARED_FIRST;
24833 lock.l_len = SHARED_SIZE;
24834 }
24835
24836 if( unixFileLock(pFile, &lock) ){
 
 
24837 tErrno = errno;
24838 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
24839 if( rc!=SQLITE_BUSY ){
24840 pFile->lastErrno = tErrno;
24841 }
24842 }
24843 }
24844
@@ -24904,11 +24905,10 @@
24905 unixFile *pFile = (unixFile*)id;
24906 unixInodeInfo *pInode;
24907 struct flock lock;
24908 int rc = SQLITE_OK;
24909 int h;
 
24910
24911 assert( pFile );
24912 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
24913 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
24914 getpid()));
@@ -24959,19 +24959,20 @@
24959 (void)handleNFSUnlock;
24960 assert( handleNFSUnlock==0 );
24961 #endif
24962 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
24963 if( handleNFSUnlock ){
24964 int tErrno; /* Error code from system call errors */
24965 off_t divSize = SHARED_SIZE - 1;
24966
24967 lock.l_type = F_UNLCK;
24968 lock.l_whence = SEEK_SET;
24969 lock.l_start = SHARED_FIRST;
24970 lock.l_len = divSize;
24971 if( unixFileLock(pFile, &lock)==(-1) ){
24972 tErrno = errno;
24973 rc = SQLITE_IOERR_UNLOCK;
24974 if( IS_LOCK_ERROR(rc) ){
24975 pFile->lastErrno = tErrno;
24976 }
24977 goto end_unlock;
24978 }
@@ -24991,11 +24992,11 @@
24992 lock.l_whence = SEEK_SET;
24993 lock.l_start = SHARED_FIRST+divSize;
24994 lock.l_len = SHARED_SIZE-divSize;
24995 if( unixFileLock(pFile, &lock)==(-1) ){
24996 tErrno = errno;
24997 rc = SQLITE_IOERR_UNLOCK;
24998 if( IS_LOCK_ERROR(rc) ){
24999 pFile->lastErrno = tErrno;
25000 }
25001 goto end_unlock;
25002 }
@@ -25004,32 +25005,32 @@
25005 {
25006 lock.l_type = F_RDLCK;
25007 lock.l_whence = SEEK_SET;
25008 lock.l_start = SHARED_FIRST;
25009 lock.l_len = SHARED_SIZE;
25010 if( unixFileLock(pFile, &lock) ){
25011 /* In theory, the call to unixFileLock() cannot fail because another
25012 ** process is holding an incompatible lock. If it does, this
25013 ** indicates that the other process is not following the locking
25014 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
25015 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
25016 ** an assert to fail). */
25017 rc = SQLITE_IOERR_RDLOCK;
25018 pFile->lastErrno = errno;
25019 goto end_unlock;
25020 }
25021 }
25022 }
25023 lock.l_type = F_UNLCK;
25024 lock.l_whence = SEEK_SET;
25025 lock.l_start = PENDING_BYTE;
25026 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
25027 if( unixFileLock(pFile, &lock)==0 ){
25028 pInode->eFileLock = SHARED_LOCK;
25029 }else{
25030 rc = SQLITE_IOERR_UNLOCK;
25031 pFile->lastErrno = errno;
 
 
 
25032 goto end_unlock;
25033 }
25034 }
25035 if( eFileLock==NO_LOCK ){
25036 /* Decrement the shared lock counter. Release the lock using an
@@ -25042,18 +25043,15 @@
25043 lock.l_whence = SEEK_SET;
25044 lock.l_start = lock.l_len = 0L;
25045 SimulateIOErrorBenign(1);
25046 SimulateIOError( h=(-1) )
25047 SimulateIOErrorBenign(0);
25048 if( unixFileLock(pFile, &lock)==0 ){
25049 pInode->eFileLock = NO_LOCK;
25050 }else{
25051 rc = SQLITE_IOERR_UNLOCK;
25052 pFile->lastErrno = errno;
 
 
 
25053 pInode->eFileLock = NO_LOCK;
25054 pFile->eFileLock = NO_LOCK;
25055 }
25056 }
25057
@@ -25095,59 +25093,58 @@
25093 ** even on VxWorks. A mutex will be acquired on VxWorks by the
25094 ** vxworksReleaseFileId() routine.
25095 */
25096 static int closeUnixFile(sqlite3_file *id){
25097 unixFile *pFile = (unixFile*)id;
25098 if( pFile->dirfd>=0 ){
25099 robust_close(pFile, pFile->dirfd, __LINE__);
25100 pFile->dirfd=-1;
25101 }
25102 if( pFile->h>=0 ){
25103 robust_close(pFile, pFile->h, __LINE__);
25104 pFile->h = -1;
25105 }
 
25106 #if OS_VXWORKS
25107 if( pFile->pId ){
25108 if( pFile->isDelete ){
25109 unlink(pFile->pId->zCanonicalName);
25110 }
25111 vxworksReleaseFileId(pFile->pId);
25112 pFile->pId = 0;
25113 }
25114 #endif
25115 OSTRACE(("CLOSE %-3d\n", pFile->h));
25116 OpenCounter(-1);
25117 sqlite3_free(pFile->pUnused);
25118 memset(pFile, 0, sizeof(unixFile));
 
25119 return SQLITE_OK;
25120 }
25121
25122 /*
25123 ** Close a file.
25124 */
25125 static int unixClose(sqlite3_file *id){
25126 int rc = SQLITE_OK;
25127 unixFile *pFile = (unixFile *)id;
25128 unixUnlock(id, NO_LOCK);
25129 unixEnterMutex();
25130
25131 /* unixFile.pInode is always valid here. Otherwise, a different close
25132 ** routine (e.g. nolockClose()) would be called instead.
25133 */
25134 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
25135 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
25136 /* If there are outstanding locks, do not actually close the file just
25137 ** yet because that would clear those locks. Instead, add the file
25138 ** descriptor to pInode->pUnused list. It will be automatically closed
25139 ** when the last lock is cleared.
25140 */
25141 setPendingFd(pFile);
25142 }
25143 releaseInodeInfo(pFile);
25144 rc = closeUnixFile(id);
25145 unixLeaveMutex();
25146 return rc;
25147 }
25148
25149 /************** End of the posix advisory lock implementation *****************
25150 ******************************************************************************/
@@ -25358,11 +25355,11 @@
25355 assert( eFileLock==NO_LOCK );
25356 if( unlink(zLockFile) ){
25357 int rc = 0;
25358 int tErrno = errno;
25359 if( ENOENT != tErrno ){
25360 rc = SQLITE_IOERR_UNLOCK;
25361 }
25362 if( IS_LOCK_ERROR(rc) ){
25363 pFile->lastErrno = tErrno;
25364 }
25365 return rc;
@@ -25446,11 +25443,11 @@
25443 /* got the lock, unlock it */
25444 lrc = robust_flock(pFile->h, LOCK_UN);
25445 if ( lrc ) {
25446 int tErrno = errno;
25447 /* unlock failed with an error */
25448 lrc = SQLITE_IOERR_UNLOCK;
25449 if( IS_LOCK_ERROR(lrc) ){
25450 pFile->lastErrno = tErrno;
25451 rc = lrc;
25452 }
25453 }
@@ -25568,25 +25565,16 @@
25565 pFile->eFileLock = eFileLock;
25566 return SQLITE_OK;
25567 }
25568
25569 /* no, really, unlock. */
25570 if( robust_flock(pFile->h, LOCK_UN) ){
 
 
 
 
 
 
25571 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25572 return SQLITE_OK;
 
 
25573 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25574 return SQLITE_IOERR_UNLOCK;
25575 }else{
 
25576 pFile->eFileLock = NO_LOCK;
25577 return SQLITE_OK;
25578 }
25579 }
25580
@@ -26388,10 +26376,11 @@
26376 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
26377 #elif defined(USE_PREAD64)
26378 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
26379 #else
26380 newOffset = lseek(id->h, offset, SEEK_SET);
26381 SimulateIOError( newOffset-- );
26382 if( newOffset!=offset ){
26383 if( newOffset == -1 ){
26384 ((unixFile*)id)->lastErrno = errno;
26385 }else{
26386 ((unixFile*)id)->lastErrno = 0;
@@ -26756,16 +26745,20 @@
26745
26746 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
26747
26748 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
26749 if( nSize>(i64)buf.st_size ){
26750
26751 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
26752 /* The code below is handling the return value of osFallocate()
26753 ** correctly. posix_fallocate() is defined to "returns zero on success,
26754 ** or an error number on failure". See the manpage for details. */
26755 int err;
26756 do{
26757 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
26758 }while( err==EINTR );
26759 if( err ) return SQLITE_IOERR_WRITE;
26760 #else
26761 /* If the OS does not have posix_fallocate(), fake it. First use
26762 ** ftruncate() to set the file size, then write a single byte to
26763 ** the last byte in each block within the extended region. This
26764 ** is the same technique used by glibc to implement posix_fallocate()
@@ -29114,11 +29107,13 @@
29107 rc = SQLITE_NOMEM;
29108 goto end_create_proxy;
29109 }
29110 memset(pNew, 0, sizeof(unixFile));
29111 pNew->openFlags = openFlags;
29112 memset(&dummyVfs, 0, sizeof(dummyVfs));
29113 dummyVfs.pAppData = (void*)&autolockIoFinder;
29114 dummyVfs.zName = "dummy";
29115 pUnused->fd = fd;
29116 pUnused->flags = openFlags;
29117 pNew->pUnused = pUnused;
29118
29119 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
@@ -45633,11 +45628,11 @@
45628 ** the BtShared object.
45629 **
45630 ** All fields in this structure are accessed under sqlite3.mutex.
45631 ** The pBt pointer itself may not be changed while there exists cursors
45632 ** in the referenced BtShared that point back to this Btree since those
45633 ** cursors have to go through this Btree to find their BtShared and
45634 ** they often do so without holding sqlite3.mutex.
45635 */
45636 struct Btree {
45637 sqlite3 *db; /* The database connection holding this btree */
45638 BtShared *pBt; /* Sharable content of this btree */
@@ -45723,19 +45718,20 @@
45718 u32 usableSize; /* Number of usable bytes on each page */
45719 int nTransaction; /* Number of open transactions (read + write) */
45720 u32 nPage; /* Number of pages in the database */
45721 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
45722 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
45723 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
45724 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
45725 #ifndef SQLITE_OMIT_SHARED_CACHE
45726 int nRef; /* Number of references to this structure */
45727 BtShared *pNext; /* Next on a list of sharable BtShared structs */
45728 BtLock *pLock; /* List of locks held on this shared-btree struct */
45729 Btree *pWriter; /* Btree with currently open write transaction */
45730 u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */
45731 u8 isPending; /* If waiting for read-locks to clear */
45732 u16 iMutexCounter; /* The number of mutex_leave(mutex) calls */
45733 #endif
45734 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
45735 };
45736
45737 /*
@@ -45964,18 +45960,37 @@
45960 /*
45961 ** Release the BtShared mutex associated with B-Tree handle p and
45962 ** clear the p->locked boolean.
45963 */
45964 static void unlockBtreeMutex(Btree *p){
45965 BtShared *pBt = p->pBt;
45966 assert( p->locked==1 );
45967 assert( sqlite3_mutex_held(pBt->mutex) );
45968 assert( sqlite3_mutex_held(p->db->mutex) );
45969 assert( p->db==pBt->db );
45970
45971 pBt->iMutexCounter++;
45972 sqlite3_mutex_leave(pBt->mutex);
45973 p->locked = 0;
45974 }
45975
45976 #ifdef SQLITE_DEBUG
45977 /*
45978 ** Return the number of times that the mutex has been exited for
45979 ** the given btree.
45980 **
45981 ** This is a small circular counter that wraps around to zero on
45982 ** overflow. It is used only for sanity checking - to verify that
45983 ** mutexes are held continously by asserting that the value of
45984 ** this counter at the beginning of a region is the same as at
45985 ** the end.
45986 */
45987 SQLITE_PRIVATE u32 sqlite3BtreeMutexCounter(Btree *p){
45988 assert( p->locked==1 || p->sharable==0 );
45989 return p->pBt->iMutexCounter;
45990 }
45991 #endif
45992
45993 /*
45994 ** Enter a mutex on the given BTree object.
45995 **
45996 ** If the object is not sharable, then no mutex is ever required
@@ -46016,10 +46031,28 @@
46031 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
46032
46033 if( !p->sharable ) return;
46034 p->wantToLock++;
46035 if( p->locked ) return;
46036
46037 /* Increment the mutex counter on all locked btrees in the same
46038 ** database connection. This simulates the unlocking that would
46039 ** occur on a worst-case mutex dead-lock avoidance scenario.
46040 */
46041 #ifdef SQLITE_DEBUG
46042 {
46043 int ii;
46044 sqlite3 *db = p->db;
46045 Btree *pOther;
46046 for(ii=0; ii<db->nDb; ii++){
46047 if( ii==1 ) continue;
46048 pOther = db->aDb[ii].pBt;
46049 if( pOther==0 || pOther->sharable==0 || pOther->locked==0 ) continue;
46050 pOther->pBt->iMutexCounter++;
46051 }
46052 }
46053 #endif
46054
46055 /* In most cases, we should be able to acquire the lock we
46056 ** want without having to go throught the ascending lock
46057 ** procedure that follows. Just be sure not to block.
46058 */
@@ -46120,11 +46153,11 @@
46153 if( p && p->sharable ){
46154 p->wantToLock++;
46155 if( !p->locked ){
46156 assert( p->wantToLock==1 );
46157 while( p->pPrev ) p = p->pPrev;
46158 /* Reason for ALWAYS: There must be at least one unlocked Btree in
46159 ** the chain. Otherwise the !p->locked test above would have failed */
46160 while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
46161 for(pLater = p->pNext; pLater; pLater=pLater->pNext){
46162 if( pLater->locked ){
46163 unlockBtreeMutex(pLater);
@@ -46176,101 +46209,21 @@
46209 }
46210 return 1;
46211 }
46212 #endif /* NDEBUG */
46213
46214 #else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
46215 /*
46216 ** The following are special cases for mutex enter routines for use
46217 ** in single threaded applications that use shared cache. Except for
46218 ** these two routines, all mutex operations are no-ops in that case and
46219 ** are null #defines in btree.h.
46220 **
46221 ** If shared cache is disabled, then all btree mutex routines, including
46222 ** the ones below, are no-ops and are null #defines in btree.h.
46223 */
46224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46225 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
46226 p->pBt->db = p->db;
46227 }
46228 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
46229 int i;
@@ -49444,15 +49397,26 @@
49397 ** routine did all the work of writing information out to disk and flushing the
49398 ** contents so that they are written onto the disk platter. All this
49399 ** routine has to do is delete or truncate or zero the header in the
49400 ** the rollback journal (which causes the transaction to commit) and
49401 ** drop locks.
49402 **
49403 ** Normally, if an error occurs while the pager layer is attempting to
49404 ** finalize the underlying journal file, this function returns an error and
49405 ** the upper layer will attempt a rollback. However, if the second argument
49406 ** is non-zero then this b-tree transaction is part of a multi-file
49407 ** transaction. In this case, the transaction has already been committed
49408 ** (by deleting a master journal file) and the caller will ignore this
49409 ** functions return code. So, even if an error occurs in the pager layer,
49410 ** reset the b-tree objects internal state to indicate that the write
49411 ** transaction has been closed. This is quite safe, as the pager will have
49412 ** transitioned to the error state.
49413 **
49414 ** This will release the write lock on the database file. If there
49415 ** are no active cursors, it also releases the read lock.
49416 */
49417 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
49418
49419 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
49420 sqlite3BtreeEnter(p);
49421 btreeIntegrity(p);
49422
@@ -49463,11 +49427,11 @@
49427 int rc;
49428 BtShared *pBt = p->pBt;
49429 assert( pBt->inTransaction==TRANS_WRITE );
49430 assert( pBt->nTransaction>0 );
49431 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
49432 if( rc!=SQLITE_OK && bCleanup==0 ){
49433 sqlite3BtreeLeave(p);
49434 return rc;
49435 }
49436 pBt->inTransaction = TRANS_READ;
49437 }
@@ -49483,11 +49447,11 @@
49447 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
49448 int rc;
49449 sqlite3BtreeEnter(p);
49450 rc = sqlite3BtreeCommitPhaseOne(p, 0);
49451 if( rc==SQLITE_OK ){
49452 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
49453 }
49454 sqlite3BtreeLeave(p);
49455 return rc;
49456 }
49457
@@ -54264,11 +54228,11 @@
54228 ** allocated, a null pointer is returned. If the blob has already been
54229 ** allocated, it is returned as normal.
54230 **
54231 ** Just before the shared-btree is closed, the function passed as the
54232 ** xFree argument when the memory allocation was made is invoked on the
54233 ** blob of allocated memory. The xFree function should not call sqlite3_free()
54234 ** on the memory, the btree layer does that.
54235 */
54236 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
54237 BtShared *pBt = p->pBt;
54238 sqlite3BtreeEnter(p);
@@ -54907,11 +54871,11 @@
54871 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
54872 }
54873
54874 /* Finish committing the transaction to the destination database. */
54875 if( SQLITE_OK==rc
54876 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
54877 ){
54878 rc = SQLITE_DONE;
54879 }
54880 }
54881
@@ -54921,11 +54885,11 @@
54885 ** "committing" a read-only transaction cannot fail.
54886 */
54887 if( bCloseTrans ){
54888 TESTONLY( int rc2 );
54889 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
54890 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
54891 assert( rc2==SQLITE_OK );
54892 }
54893
54894 if( rc==SQLITE_IOERR_NOMEM ){
54895 rc = SQLITE_NOMEM;
@@ -56419,10 +56383,15 @@
56383 pOp->p2 = p2;
56384 pOp->p3 = p3;
56385 pOp->p4.p = 0;
56386 pOp->p4type = P4_NOTUSED;
56387 p->expired = 0;
56388 if( op==OP_ParseSchema ){
56389 /* Any program that uses the OP_ParseSchema opcode needs to lock
56390 ** all btrees. */
56391 p->btreeMask = ~(yDbMask)0;
56392 }
56393 #ifdef SQLITE_DEBUG
56394 pOp->zComment = 0;
56395 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
56396 #endif
56397 #ifdef VDBE_PROFILE
@@ -56719,11 +56688,11 @@
56688 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
56689 VdbeOp *aOp = p->aOp;
56690 assert( aOp && !p->db->mallocFailed );
56691
56692 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
56693 assert( p->btreeMask==0 );
56694
56695 resolveP2Values(p, pnMaxArg);
56696 *pnOp = p->nOp;
56697 p->aOp = 0;
56698 return aOp;
@@ -56821,10 +56790,11 @@
56790 /*
56791 ** Change the P2 operand of instruction addr so that it points to
56792 ** the address of the next instruction to be coded.
56793 */
56794 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
56795 assert( addr>=0 );
56796 sqlite3VdbeChangeP2(p, addr, p->nOp);
56797 }
56798
56799
56800 /*
@@ -57206,26 +57176,135 @@
57176 #endif
57177
57178 /*
57179 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
57180 **
57181 ** The prepared statements need to know in advance the complete set of
57182 ** attached databases that they will be using. A mask of these databases
57183 ** is maintained in p->btreeMask and is used for locking and other purposes.
 
57184 */
57185 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
57186 assert( i>=0 && i<p->db->nDb && i<sizeof(yDbMask)*8 );
 
57187 assert( i<(int)sizeof(p->btreeMask)*8 );
57188 p->btreeMask |= ((yDbMask)1)<<i;
 
 
 
 
57189 }
57190
57191 /*
57192 ** Compute the sum of all mutex counters for all btrees in the
57193 ** given prepared statement.
57194 */
57195 #ifndef SQLITE_OMIT_SHARED_CACHE
57196 static u32 mutexCounterSum(Vdbe *p){
57197 u32 cntSum = 0;
57198 #ifdef SQLITE_DEBUG
57199 int i;
57200 yDbMask mask;
57201 sqlite3 *db = p->db;
57202 Db *aDb = db->aDb;
57203 int nDb = db->nDb;
57204 for(i=0, mask=1; i<nDb; i++, mask += mask){
57205 if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57206 cntSum += sqlite3BtreeMutexCounter(aDb[i].pBt);
57207 }
57208 }
57209 #else
57210 UNUSED_PARAMETER(p);
57211 #endif
57212 return cntSum;
57213 }
57214 #endif
57215
57216 /*
57217 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
57218 ** this routine obtains the mutex associated with each BtShared structure
57219 ** that may be accessed by the VM passed as an argument. In doing so it also
57220 ** sets the BtShared.db member of each of the BtShared structures, ensuring
57221 ** that the correct busy-handler callback is invoked if required.
57222 **
57223 ** If SQLite is not threadsafe but does support shared-cache mode, then
57224 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
57225 ** of all of BtShared structures accessible via the database handle
57226 ** associated with the VM.
57227 **
57228 ** If SQLite is not threadsafe and does not support shared-cache mode, this
57229 ** function is a no-op.
57230 **
57231 ** The p->btreeMask field is a bitmask of all btrees that the prepared
57232 ** statement p will ever use. Let N be the number of bits in p->btreeMask
57233 ** corresponding to btrees that use shared cache. Then the runtime of
57234 ** this routine is N*N. But as N is rarely more than 1, this should not
57235 ** be a problem.
57236 */
57237 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
57238 #ifndef SQLITE_OMIT_SHARED_CACHE
57239 int i;
57240 yDbMask mask;
57241 sqlite3 *db = p->db;
57242 Db *aDb = db->aDb;
57243 int nDb = db->nDb;
57244 for(i=0, mask=1; i<nDb; i++, mask += mask){
57245 if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57246 sqlite3BtreeEnter(aDb[i].pBt);
57247 }
57248 }
57249 p->iMutexCounter = mutexCounterSum(p);
57250 #else
57251 UNUSED_PARAMETER(p);
57252 #endif
57253 }
57254
57255 /*
57256 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
57257 */
57258 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
57259 #ifndef SQLITE_OMIT_SHARED_CACHE
57260 int i;
57261 yDbMask mask;
57262 sqlite3 *db = p->db;
57263 Db *aDb = db->aDb;
57264 int nDb = db->nDb;
57265
57266 /* Assert that the all mutexes have been held continously since
57267 ** the most recent sqlite3VdbeEnter() or sqlite3VdbeMutexResync().
57268 */
57269 assert( mutexCounterSum(p) == p->iMutexCounter );
57270
57271 for(i=0, mask=1; i<nDb; i++, mask += mask){
57272 if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57273 sqlite3BtreeLeave(aDb[i].pBt);
57274 }
57275 }
57276 #else
57277 UNUSED_PARAMETER(p);
57278 #endif
57279 }
57280
57281 /*
57282 ** Recompute the sum of the mutex counters on all btrees used by the
57283 ** prepared statement p.
57284 **
57285 ** Call this routine while holding a sqlite3VdbeEnter() after doing something
57286 ** that might cause one or more of the individual mutexes held by the
57287 ** prepared statement to be released. Calling sqlite3BtreeEnter() on
57288 ** any BtShared mutex which is not used by the prepared statement is one
57289 ** way to cause one or more of the mutexes in the prepared statement
57290 ** to be temporarily released. The anti-deadlocking logic in
57291 ** sqlite3BtreeEnter() can cause mutexes to be released temporarily then
57292 ** reacquired.
57293 **
57294 ** Calling this routine is an acknowledgement that some of the individual
57295 ** mutexes in the prepared statement might have been released and reacquired.
57296 ** So checks to verify that mutex-protected content did not change
57297 ** unexpectedly should accompany any call to this routine.
57298 */
57299 SQLITE_PRIVATE void sqlite3VdbeMutexResync(Vdbe *p){
57300 #if !defined(SQLITE_OMIT_SHARED_CACHE) && defined(SQLITE_DEBUG)
57301 p->iMutexCounter = mutexCounterSum(p);
57302 #else
57303 UNUSED_PARAMETER(p);
57304 #endif
57305 }
57306
57307 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
57308 /*
57309 ** Print a single opcode. This routine is used for debugging only.
57310 */
@@ -57965,11 +58044,11 @@
58044 ** but could happen. In this case abandon processing and return the error.
58045 */
58046 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
58047 Btree *pBt = db->aDb[i].pBt;
58048 if( pBt ){
58049 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
58050 }
58051 }
58052 if( rc==SQLITE_OK ){
58053 sqlite3VtabCommit(db);
58054 }
@@ -58097,11 +58176,11 @@
58176 disable_simulated_io_errors();
58177 sqlite3BeginBenignMalloc();
58178 for(i=0; i<db->nDb; i++){
58179 Btree *pBt = db->aDb[i].pBt;
58180 if( pBt ){
58181 sqlite3BtreeCommitPhaseTwo(pBt, 1);
58182 }
58183 }
58184 sqlite3EndBenignMalloc();
58185 enable_simulated_io_errors();
58186
@@ -58220,37 +58299,10 @@
58299 }
58300 }
58301 return rc;
58302 }
58303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58304 /*
58305 ** This function is called when a transaction opened by the database
58306 ** handle associated with the VM passed as an argument is about to be
58307 ** committed. If there are outstanding deferred foreign key constraint
58308 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
@@ -58319,11 +58371,11 @@
58371 int mrc; /* Primary error code from p->rc */
58372 int eStatementOp = 0;
58373 int isSpecialError; /* Set to true if a 'special' error */
58374
58375 /* Lock all btrees used by the statement */
58376 sqlite3VdbeEnter(p);
58377
58378 /* Check for one of the special errors */
58379 mrc = p->rc & 0xff;
58380 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
58381 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
@@ -58373,11 +58425,11 @@
58425 ){
58426 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
58427 rc = sqlite3VdbeCheckFk(p, 1);
58428 if( rc!=SQLITE_OK ){
58429 if( NEVER(p->readOnly) ){
58430 sqlite3VdbeLeave(p);
58431 return SQLITE_ERROR;
58432 }
58433 rc = SQLITE_CONSTRAINT;
58434 }else{
58435 /* The auto-commit flag is true, the vdbe program was successful
@@ -58385,11 +58437,11 @@
58437 ** key constraints to hold up the transaction. This means a commit
58438 ** is required. */
58439 rc = vdbeCommit(db, p);
58440 }
58441 if( rc==SQLITE_BUSY && p->readOnly ){
58442 sqlite3VdbeLeave(p);
58443 return SQLITE_BUSY;
58444 }else if( rc!=SQLITE_OK ){
58445 p->rc = rc;
58446 sqlite3RollbackAll(db);
58447 }else{
@@ -58457,11 +58509,12 @@
58509 sqlite3ResetInternalSchema(db, 0);
58510 db->flags = (db->flags | SQLITE_InternChanges);
58511 }
58512
58513 /* Release the locks */
58514 sqlite3VdbeMutexResync(p);
58515 sqlite3VdbeLeave(p);
58516 }
58517
58518 /* We have successfully halted and closed the VM. Record this fact. */
58519 if( p->pc>=0 ){
58520 db->activeVdbeCnt--;
@@ -61978,11 +62031,11 @@
62031 } u;
62032 /* End automatically generated code
62033 ********************************************************************/
62034
62035 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
62036 sqlite3VdbeEnter(p);
62037 if( p->rc==SQLITE_NOMEM ){
62038 /* This happens if a malloc() inside a call to sqlite3_column_text() or
62039 ** sqlite3_column_text16() failed. */
62040 goto no_mem;
62041 }
@@ -62815,19 +62868,31 @@
62868 assert( pOp[-1].p4type==P4_COLLSEQ );
62869 assert( pOp[-1].opcode==OP_CollSeq );
62870 u.ag.ctx.pColl = pOp[-1].p4.pColl;
62871 }
62872 (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
62873 sqlite3VdbeMutexResync(p);
62874 if( db->mallocFailed ){
62875 /* Even though a malloc() has failed, the implementation of the
62876 ** user function may have called an sqlite3_result_XXX() function
62877 ** to return a value. The following call releases any resources
62878 ** associated with such a value.
62879 */
62880 sqlite3VdbeMemRelease(&u.ag.ctx.s);
62881 goto no_mem;
62882 }
62883
62884 /* The app-defined function has done something that as caused this
62885 ** statement to expire. (Perhaps the function called sqlite3_exec()
62886 ** with a CREATE TABLE statement.)
62887 */
62888 #if 0
62889 if( p->expired ){
62890 rc = SQLITE_ABORT;
62891 break;
62892 }
62893 #endif
62894
62895 /* If any auxiliary data functions have been called by this user function,
62896 ** immediately call the destructor for any non-static values.
62897 */
62898 if( u.ag.ctx.pVdbeFunc ){
@@ -64091,10 +64156,11 @@
64156 }
64157 }
64158 if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
64159 sqlite3ExpirePreparedStatements(db);
64160 sqlite3ResetInternalSchema(db, 0);
64161 sqlite3VdbeMutexResync(p);
64162 db->flags = (db->flags | SQLITE_InternChanges);
64163 }
64164 }
64165
64166 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
@@ -64234,11 +64300,11 @@
64300 #if 0 /* local variables moved into u.as */
64301 Btree *pBt;
64302 #endif /* local variables moved into u.as */
64303
64304 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64305 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
64306 u.as.pBt = db->aDb[pOp->p1].pBt;
64307
64308 if( u.as.pBt ){
64309 rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
64310 if( rc==SQLITE_BUSY ){
@@ -64292,11 +64358,11 @@
64358 u.at.iDb = pOp->p1;
64359 u.at.iCookie = pOp->p3;
64360 assert( pOp->p3<SQLITE_N_BTREE_META );
64361 assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
64362 assert( db->aDb[u.at.iDb].pBt!=0 );
64363 assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
64364
64365 sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
64366 pOut->u.i = u.at.iMeta;
64367 break;
64368 }
@@ -64315,11 +64381,11 @@
64381 #if 0 /* local variables moved into u.au */
64382 Db *pDb;
64383 #endif /* local variables moved into u.au */
64384 assert( pOp->p2<SQLITE_N_BTREE_META );
64385 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64386 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
64387 u.au.pDb = &db->aDb[pOp->p1];
64388 assert( u.au.pDb->pBt!=0 );
64389 pIn3 = &aMem[pOp->p3];
64390 sqlite3VdbeMemIntegerify(pIn3);
64391 /* See note about index shifting on OP_ReadCookie */
@@ -64365,11 +64431,11 @@
64431 int iGen;
64432 Btree *pBt;
64433 #endif /* local variables moved into u.av */
64434
64435 assert( pOp->p1>=0 && pOp->p1<db->nDb );
64436 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
64437 u.av.pBt = db->aDb[pOp->p1].pBt;
64438 if( u.av.pBt ){
64439 sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
64440 u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
64441 }else{
@@ -64391,10 +64457,11 @@
64457 ** to be invalidated whenever sqlite3_step() is called from within
64458 ** a v-table method.
64459 */
64460 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
64461 sqlite3ResetInternalSchema(db, pOp->p1);
64462 sqlite3VdbeMutexResync(p);
64463 }
64464
64465 p->expired = 1;
64466 rc = SQLITE_SCHEMA;
64467 }
@@ -64471,11 +64538,11 @@
64538 u.aw.nField = 0;
64539 u.aw.pKeyInfo = 0;
64540 u.aw.p2 = pOp->p2;
64541 u.aw.iDb = pOp->p3;
64542 assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
64543 assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
64544 u.aw.pDb = &db->aDb[u.aw.iDb];
64545 u.aw.pX = u.aw.pDb->pBt;
64546 assert( u.aw.pX!=0 );
64547 if( pOp->opcode==OP_OpenWrite ){
64548 u.aw.wrFlag = 1;
@@ -66008,11 +66075,11 @@
66075 rc = SQLITE_LOCKED;
66076 p->errorAction = OE_Abort;
66077 }else{
66078 u.br.iDb = pOp->p3;
66079 assert( u.br.iCnt==1 );
66080 assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
66081 rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
66082 pOut->flags = MEM_Int;
66083 pOut->u.i = u.br.iMoved;
66084 #ifndef SQLITE_OMIT_AUTOVACUUM
66085 if( rc==SQLITE_OK && u.br.iMoved!=0 ){
@@ -66046,11 +66113,11 @@
66113 #if 0 /* local variables moved into u.bs */
66114 int nChange;
66115 #endif /* local variables moved into u.bs */
66116
66117 u.bs.nChange = 0;
66118 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
66119 rc = sqlite3BtreeClearTable(
66120 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
66121 );
66122 if( pOp->p3 ){
66123 p->nChange += u.bs.nChange;
@@ -66093,11 +66160,11 @@
66160 Db *pDb;
66161 #endif /* local variables moved into u.bt */
66162
66163 u.bt.pgno = 0;
66164 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66165 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66166 u.bt.pDb = &db->aDb[pOp->p1];
66167 assert( u.bt.pDb->pBt!=0 );
66168 if( pOp->opcode==OP_CreateTable ){
66169 /* u.bt.flags = BTREE_INTKEY; */
66170 u.bt.flags = BTREE_INTKEY;
@@ -66122,31 +66189,27 @@
66189 int iDb;
66190 const char *zMaster;
66191 char *zSql;
66192 InitData initData;
66193 #endif /* local variables moved into u.bu */
66194
66195 /* Any prepared statement that invokes this opcode will hold mutexes
66196 ** on every btree. This is a prerequisite for invoking
66197 ** sqlite3InitCallback().
66198 */
66199 #ifdef SQLITE_DEBUG
66200 for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
66201 assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
66202 }
66203 #endif
66204 assert( p->btreeMask == ~(yDbMask)0 );
66205
66206
66207 u.bu.iDb = pOp->p1;
66208 assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
66209 assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
66210 /* Used to be a conditional */ {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66211 u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
66212 u.bu.initData.db = db;
66213 u.bu.initData.iDb = pOp->p1;
66214 u.bu.initData.pzErrMsg = &p->zErrMsg;
66215 u.bu.zSql = sqlite3MPrintf(db,
@@ -66163,11 +66226,10 @@
66226 if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
66227 sqlite3DbFree(db, u.bu.zSql);
66228 db->init.busy = 0;
66229 }
66230 }
 
66231 if( rc==SQLITE_NOMEM ){
66232 goto no_mem;
66233 }
66234 break;
66235 }
@@ -66266,11 +66328,11 @@
66328 for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
66329 u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
66330 }
66331 u.bv.aRoot[u.bv.j] = 0;
66332 assert( pOp->p5<db->nDb );
66333 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
66334 u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
66335 (int)u.bv.pnErr->u.i, &u.bv.nErr);
66336 sqlite3DbFree(db, u.bv.aRoot);
66337 u.bv.pnErr->u.i -= u.bv.nErr;
66338 sqlite3VdbeMemSetNull(pIn1);
@@ -66702,15 +66764,29 @@
66764 assert( pOp[-1].p4type==P4_COLLSEQ );
66765 assert( pOp[-1].opcode==OP_CollSeq );
66766 u.cb.ctx.pColl = pOp[-1].p4.pColl;
66767 }
66768 (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
66769 sqlite3VdbeMutexResync(p);
66770 if( u.cb.ctx.isError ){
66771 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
66772 rc = u.cb.ctx.isError;
66773 }
66774
66775 /* The app-defined function has done something that as caused this
66776 ** statement to expire. (Perhaps the function called sqlite3_exec()
66777 ** with a CREATE TABLE statement.)
66778 */
66779 #if 0
66780 if( p->expired ){
66781 rc = SQLITE_ABORT;
66782 break;
66783 }
66784 #endif
66785
66786 sqlite3VdbeMemRelease(&u.cb.ctx.s);
66787
66788 break;
66789 }
66790
66791 /* Opcode: AggFinal P1 P2 * P4 *
66792 **
@@ -66730,12 +66806,15 @@
66806 #endif /* local variables moved into u.cc */
66807 assert( pOp->p1>0 && pOp->p1<=p->nMem );
66808 u.cc.pMem = &aMem[pOp->p1];
66809 assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
66810 rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
66811 sqlite3VdbeMutexResync(p);
66812 if( rc ){
66813 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
66814 }else if( p->expired ){
66815 rc = SQLITE_ABORT;
66816 }
66817 sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
66818 UPDATE_MAX_BLOBSIZE(u.cc.pMem);
66819 if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
66820 goto too_big;
@@ -66812,25 +66891,24 @@
66891 );
66892 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66893
66894 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
66895 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
66896 ** when the statement is prepared and so p->btreeMask!=0. All mutexes
66897 ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
66898 ** is not called when the statement is prepared because it requires the
66899 ** iDb index of the database as a parameter, and the database has not
66900 ** yet been attached so that index is unavailable. We have to wait
66901 ** until runtime (now) to get the mutex on the newly attached database.
66902 ** No other mutexes are required by the ATTACH command so this is safe
66903 ** to do.
66904 */
66905 if( p->btreeMask==0 ){
 
66906 /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
66907 ** database. */
66908 sqlite3VdbeUsesBtree(p, pOp->p1);
66909 sqlite3VdbeEnter(p);
66910 }
66911
66912 u.ce.pBt = db->aDb[pOp->p1].pBt;
66913 u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
66914 u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
@@ -66928,11 +67006,11 @@
67006 #if 0 /* local variables moved into u.cf */
67007 Btree *pBt;
67008 #endif /* local variables moved into u.cf */
67009
67010 assert( pOp->p1>=0 && pOp->p1<db->nDb );
67011 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67012 u.cf.pBt = db->aDb[pOp->p1].pBt;
67013 rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
67014 if( rc==SQLITE_DONE ){
67015 pc = pOp->p2 - 1;
67016 rc = SQLITE_OK;
@@ -66977,11 +67055,11 @@
67055 case OP_TableLock: {
67056 u8 isWriteLock = (u8)pOp->p3;
67057 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
67058 int p1 = pOp->p1;
67059 assert( p1>=0 && p1<db->nDb );
67060 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
67061 assert( isWriteLock==0 || isWriteLock==1 );
67062 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
67063 if( (rc&0xFF)==SQLITE_LOCKED ){
67064 const char *z = pOp->p4.z;
67065 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -67482,17 +67560,20 @@
67560 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
67561 pc, p->zSql, p->zErrMsg);
67562 sqlite3VdbeHalt(p);
67563 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
67564 rc = SQLITE_ERROR;
67565 if( resetSchemaOnFault ){
67566 sqlite3ResetInternalSchema(db, 0);
67567 sqlite3VdbeMutexResync(p);
67568 }
67569
67570 /* This is the only way out of this procedure. We have to
67571 ** release the mutexes on btrees that were acquired at the
67572 ** top. */
67573 vdbe_return:
67574 sqlite3VdbeLeave(p);
67575 return rc;
67576
67577 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
67578 ** is encountered.
67579 */
@@ -73966,10 +74047,26 @@
74047 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
74048 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
74049 }
74050 #endif
74051 }
74052
74053 /*
74054 ** Parameter zName is the name of a table that is about to be altered
74055 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
74056 ** If the table is a system table, this function leaves an error message
74057 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
74058 **
74059 ** Or, if zName is not a system table, zero is returned.
74060 */
74061 static int isSystemTable(Parse *pParse, const char *zName){
74062 if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
74063 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
74064 return 1;
74065 }
74066 return 0;
74067 }
74068
74069 /*
74070 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
74071 ** command.
74072 */
@@ -74017,18 +74114,15 @@
74114 }
74115
74116 /* Make sure it is not a system table being altered, or a reserved name
74117 ** that the table is being renamed to.
74118 */
74119 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 
 
 
74120 goto exit_rename_table;
74121 }
74122 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
74123 exit_rename_table;
74124 }
74125
74126 #ifndef SQLITE_OMIT_VIEW
74127 if( pTab->pSelect ){
74128 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
@@ -74356,10 +74450,13 @@
74450 /* Make sure this is not an attempt to ALTER a view. */
74451 if( pTab->pSelect ){
74452 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
74453 goto exit_begin_add_column;
74454 }
74455 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
74456 goto exit_begin_add_column;
74457 }
74458
74459 assert( pTab->addColOffset>0 );
74460 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
74461
74462 /* Put a copy of the Table struct in Parse.pNewTable for the
@@ -74443,11 +74540,12 @@
74540 */
74541 static void openStatTable(
74542 Parse *pParse, /* Parsing context */
74543 int iDb, /* The database we are looking in */
74544 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
74545 const char *zWhere, /* Delete entries for this table or index */
74546 const char *zWhereType /* Either "tbl" or "idx" */
74547 ){
74548 static const struct {
74549 const char *zName;
74550 const char *zCols;
74551 } aTable[] = {
@@ -74488,11 +74586,11 @@
74586 ** entire contents of the table. */
74587 aRoot[i] = pStat->tnum;
74588 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
74589 if( zWhere ){
74590 sqlite3NestedParse(pParse,
74591 "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
74592 );
74593 }else{
74594 /* The sqlite_stat[12] table already exists. Delete all rows. */
74595 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
74596 }
@@ -74512,10 +74610,11 @@
74610 ** a single table.
74611 */
74612 static void analyzeOneTable(
74613 Parse *pParse, /* Parser context */
74614 Table *pTab, /* Table whose indices are to be analyzed */
74615 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
74616 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
74617 int iMem /* Available memory locations begin here */
74618 ){
74619 sqlite3 *db = pParse->db; /* Database handle */
74620 Index *pIdx; /* An index to being analyzed */
@@ -74522,12 +74621,11 @@
74621 int iIdxCur; /* Cursor open on index being analyzed */
74622 Vdbe *v; /* The virtual machine being built up */
74623 int i; /* Loop counter */
74624 int topOfLoop; /* The top of the loop */
74625 int endOfLoop; /* The end of the loop */
74626 int jZeroRows = -1; /* Jump from here if number of rows is zero */
 
74627 int iDb; /* Index of database containing pTab */
74628 int regTabname = iMem++; /* Register containing table name */
74629 int regIdxname = iMem++; /* Register containing index name */
74630 int regSampleno = iMem++; /* Register containing next sample number */
74631 int regCol = iMem++; /* Content of a column analyzed table */
@@ -74534,10 +74632,11 @@
74632 int regRec = iMem++; /* Register holding completed record */
74633 int regTemp = iMem++; /* Temporary use register */
74634 int regRowid = iMem++; /* Rowid for the inserted record */
74635
74636 #ifdef SQLITE_ENABLE_STAT2
74637 int addr = 0; /* Instruction address */
74638 int regTemp2 = iMem++; /* Temporary use register */
74639 int regSamplerecno = iMem++; /* Index of next sample to record */
74640 int regRecno = iMem++; /* Current sample index */
74641 int regLast = iMem++; /* Index of last sample to record */
74642 int regFirst = iMem++; /* Index of first sample to record */
@@ -74569,13 +74668,16 @@
74668 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
74669
74670 iIdxCur = pParse->nTab++;
74671 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
74672 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
74673 int nCol;
74674 KeyInfo *pKey;
74675
74676 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
74677 nCol = pIdx->nColumn;
74678 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
74679 if( iMem+1+(nCol*2)>pParse->nMem ){
74680 pParse->nMem = iMem+1+(nCol*2);
74681 }
74682
74683 /* Open a cursor to the index to be analyzed. */
@@ -74728,11 +74830,11 @@
74830 ** If K==0 then no entry is made into the sqlite_stat1 table.
74831 ** If K>0 then it is always the case the D>0 so division by zero
74832 ** is never possible.
74833 */
74834 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
74835 if( jZeroRows<0 ){
74836 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
74837 }
74838 for(i=0; i<nCol; i++){
74839 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
74840 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
@@ -74754,24 +74856,22 @@
74856 if( pTab->pIndex==0 ){
74857 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
74858 VdbeComment((v, "%s", pTab->zName));
74859 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
74860 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
74861 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
74862 }else{
 
 
74863 sqlite3VdbeJumpHere(v, jZeroRows);
74864 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
74865 }
74866 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
74867 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
74868 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
74869 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
74870 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
74871 if( pParse->nMem<regRec ) pParse->nMem = regRec;
74872 sqlite3VdbeJumpHere(v, jZeroRows);
 
 
74873 }
74874
74875 /*
74876 ** Generate code that will cause the most recent index analysis to
74877 ** be loaded into internal hash tables where is can be used.
@@ -74794,35 +74894,40 @@
74894 int iMem;
74895
74896 sqlite3BeginWriteOperation(pParse, 0, iDb);
74897 iStatCur = pParse->nTab;
74898 pParse->nTab += 2;
74899 openStatTable(pParse, iDb, iStatCur, 0, 0);
74900 iMem = pParse->nMem+1;
74901 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
74902 Table *pTab = (Table*)sqliteHashData(k);
74903 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
74904 }
74905 loadAnalysis(pParse, iDb);
74906 }
74907
74908 /*
74909 ** Generate code that will do an analysis of a single table in
74910 ** a database. If pOnlyIdx is not NULL then it is a single index
74911 ** in pTab that should be analyzed.
74912 */
74913 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
74914 int iDb;
74915 int iStatCur;
74916
74917 assert( pTab!=0 );
74918 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
74919 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
74920 sqlite3BeginWriteOperation(pParse, 0, iDb);
74921 iStatCur = pParse->nTab;
74922 pParse->nTab += 2;
74923 if( pOnlyIdx ){
74924 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
74925 }else{
74926 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
74927 }
74928 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
74929 loadAnalysis(pParse, iDb);
74930 }
74931
74932 /*
74933 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -74840,10 +74945,11 @@
74945 sqlite3 *db = pParse->db;
74946 int iDb;
74947 int i;
74948 char *z, *zDb;
74949 Table *pTab;
74950 Index *pIdx;
74951 Token *pTableName;
74952
74953 /* Read the database schema. If an error occurs, leave an error message
74954 ** and code in pParse and return NULL. */
74955 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
@@ -74864,29 +74970,31 @@
74970 if( iDb>=0 ){
74971 analyzeDatabase(pParse, iDb);
74972 }else{
74973 z = sqlite3NameFromToken(db, pName1);
74974 if( z ){
74975 if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
74976 analyzeTable(pParse, pIdx->pTable, pIdx);
74977 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
74978 analyzeTable(pParse, pTab, 0);
74979 }
74980 sqlite3DbFree(db, z);
 
 
 
74981 }
74982 }
74983 }else{
74984 /* Form 3: Analyze the fully qualified table name */
74985 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
74986 if( iDb>=0 ){
74987 zDb = db->aDb[iDb].zName;
74988 z = sqlite3NameFromToken(db, pTableName);
74989 if( z ){
74990 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
74991 analyzeTable(pParse, pIdx->pTable, pIdx);
74992 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
74993 analyzeTable(pParse, pTab, 0);
74994 }
74995 sqlite3DbFree(db, z);
 
 
 
74996 }
74997 }
74998 }
74999 }
75000
@@ -76055,11 +76163,11 @@
76163 ** set for each database that is used. Generate code to start a
76164 ** transaction on each used database and to verify the schema cookie
76165 ** on each used database.
76166 */
76167 if( pParse->cookieGoto>0 ){
76168 yDbMask mask;
76169 int iDb;
76170 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
76171 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
76172 if( (mask & pParse->cookieMask)==0 ) continue;
76173 sqlite3VdbeUsesBtree(v, iDb);
@@ -79351,16 +79459,16 @@
79459 if( v==0 ) return; /* This only happens if there was a prior error */
79460 pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
79461 }
79462 if( iDb>=0 ){
79463 sqlite3 *db = pToplevel->db;
79464 yDbMask mask;
79465
79466 assert( iDb<db->nDb );
79467 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
79468 assert( iDb<SQLITE_MAX_ATTACHED+2 );
79469 mask = ((yDbMask)1)<<iDb;
79470 if( (pToplevel->cookieMask & mask)==0 ){
79471 pToplevel->cookieMask |= mask;
79472 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
79473 if( !OMIT_TEMPDB && iDb==1 ){
79474 sqlite3OpenTempDatabase(pToplevel);
@@ -79383,11 +79491,11 @@
79491 ** necessary to undo a write and the checkpoint should not be set.
79492 */
79493 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
79494 Parse *pToplevel = sqlite3ParseToplevel(pParse);
79495 sqlite3CodeVerifySchema(pParse, iDb);
79496 pToplevel->writeMask |= ((yDbMask)1)<<iDb;
79497 pToplevel->isMultiWrite |= setStatement;
79498 }
79499
79500 /*
79501 ** Indicate that the statement currently under construction might write
@@ -99567,11 +99675,11 @@
99675 ** VALUE and how common that value is according to the histogram.
99676 */
99677 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
99678 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
99679 testcase( pFirstTerm->eOperator==WO_EQ );
99680 testcase( pFirstTerm->eOperator==WO_ISNULL );
99681 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
99682 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
99683 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
99684 }
99685 }
99686
+7 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.6"
111111
#define SQLITE_VERSION_NUMBER 3007006
112
-#define SQLITE_SOURCE_ID "2011-03-24 01:34:03 b6e268fce12829f058f1dfa223731ec8479493f8"
112
+#define SQLITE_SOURCE_ID "2011-04-04 03:27:16 f8e98ab3062a6e56924a86e8f3204c30d0f3d906"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -2978,11 +2978,13 @@
29782978
** UTF-16 string. ^The first parameter is the [prepared statement]
29792979
** that implements the [SELECT] statement. ^The second parameter is the
29802980
** column number. ^The leftmost column is number 0.
29812981
**
29822982
** ^The returned string pointer is valid until either the [prepared statement]
2983
-** is destroyed by [sqlite3_finalize()] or until the next call to
2983
+** is destroyed by [sqlite3_finalize()] or until the statement is automatically
2984
+** reprepared by the first call to [sqlite3_step()] for a particular run
2985
+** or until the next call to
29842986
** sqlite3_column_name() or sqlite3_column_name16() on the same column.
29852987
**
29862988
** ^If sqlite3_malloc() fails during the processing of either routine
29872989
** (for example during a conversion from UTF-8 to UTF-16) then a
29882990
** NULL pointer is returned.
@@ -3004,11 +3006,13 @@
30043006
** ^The name of the database or table or column can be returned as
30053007
** either a UTF-8 or UTF-16 string. ^The _database_ routines return
30063008
** the database name, the _table_ routines return the table name, and
30073009
** the origin_ routines return the column name.
30083010
** ^The returned string is valid until the [prepared statement] is destroyed
3009
-** using [sqlite3_finalize()] or until the same information is requested
3011
+** using [sqlite3_finalize()] or until the statement is automatically
3012
+** reprepared by the first call to [sqlite3_step()] for a particular run
3013
+** or until the same information is requested
30103014
** again in a different encoding.
30113015
**
30123016
** ^The names returned are the original un-aliased names of the
30133017
** database, table, and column.
30143018
**
30153019
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.6"
111 #define SQLITE_VERSION_NUMBER 3007006
112 #define SQLITE_SOURCE_ID "2011-03-24 01:34:03 b6e268fce12829f058f1dfa223731ec8479493f8"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -2978,11 +2978,13 @@
2978 ** UTF-16 string. ^The first parameter is the [prepared statement]
2979 ** that implements the [SELECT] statement. ^The second parameter is the
2980 ** column number. ^The leftmost column is number 0.
2981 **
2982 ** ^The returned string pointer is valid until either the [prepared statement]
2983 ** is destroyed by [sqlite3_finalize()] or until the next call to
 
 
2984 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
2985 **
2986 ** ^If sqlite3_malloc() fails during the processing of either routine
2987 ** (for example during a conversion from UTF-8 to UTF-16) then a
2988 ** NULL pointer is returned.
@@ -3004,11 +3006,13 @@
3004 ** ^The name of the database or table or column can be returned as
3005 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
3006 ** the database name, the _table_ routines return the table name, and
3007 ** the origin_ routines return the column name.
3008 ** ^The returned string is valid until the [prepared statement] is destroyed
3009 ** using [sqlite3_finalize()] or until the same information is requested
 
 
3010 ** again in a different encoding.
3011 **
3012 ** ^The names returned are the original un-aliased names of the
3013 ** database, table, and column.
3014 **
3015
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.6"
111 #define SQLITE_VERSION_NUMBER 3007006
112 #define SQLITE_SOURCE_ID "2011-04-04 03:27:16 f8e98ab3062a6e56924a86e8f3204c30d0f3d906"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -2978,11 +2978,13 @@
2978 ** UTF-16 string. ^The first parameter is the [prepared statement]
2979 ** that implements the [SELECT] statement. ^The second parameter is the
2980 ** column number. ^The leftmost column is number 0.
2981 **
2982 ** ^The returned string pointer is valid until either the [prepared statement]
2983 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
2984 ** reprepared by the first call to [sqlite3_step()] for a particular run
2985 ** or until the next call to
2986 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
2987 **
2988 ** ^If sqlite3_malloc() fails during the processing of either routine
2989 ** (for example during a conversion from UTF-8 to UTF-16) then a
2990 ** NULL pointer is returned.
@@ -3004,11 +3006,13 @@
3006 ** ^The name of the database or table or column can be returned as
3007 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
3008 ** the database name, the _table_ routines return the table name, and
3009 ** the origin_ routines return the column name.
3010 ** ^The returned string is valid until the [prepared statement] is destroyed
3011 ** using [sqlite3_finalize()] or until the statement is automatically
3012 ** reprepared by the first call to [sqlite3_step()] for a particular run
3013 ** or until the same information is requested
3014 ** again in a different encoding.
3015 **
3016 ** ^The names returned are the original un-aliased names of the
3017 ** database, table, and column.
3018 **
3019

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button