/ Check-in [ecbe0832]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge 1958db4493 and 26cd015c0e.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ecbe0832be77599c7cc66fb9968d0f419000d231
User & Date: dan 2009-09-07 17:41:48
Context
2009-09-07
18:14
Add the SQLITE_LIMIT_TRIGGER_DEPTH option to sqlite3_limit(). check-in: d7dc8b43 user: drh tags: trunk
17:41
Merge 1958db4493 and 26cd015c0e. check-in: ecbe0832 user: dan tags: trunk
17:32
Fix a bug where a condition was incorrectly being compiled out when SQLITE_OMIT_VIRTUALTABLE was defined. check-in: 1958db44 user: dan tags: trunk
04:38
Warning cleanup from the MSVC compile. check-in: 26cd015c user: shane tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

    45     45       { "sqlite_stat1", "tbl,idx,stat" },
    46     46   #ifdef SQLITE_ENABLE_STAT2
    47     47       { "sqlite_stat2", "tbl,idx,sampleno,sample" },
    48     48   #endif
    49     49     };
    50     50   
    51     51     int aRoot[] = {0, 0};
    52         -  int aCreateTbl[] = {0, 0};
           52  +  u8 aCreateTbl[] = {0, 0};
    53     53   
    54     54     int i;
    55     55     sqlite3 *db = pParse->db;
    56     56     Db *pDb;
    57     57     Vdbe *v = sqlite3GetVdbe(pParse);
    58     58     if( v==0 ) return;
    59     59     assert( sqlite3BtreeHoldsAllMutexes(db) );
................................................................................
   502    502         if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
   503    503           sqlite3DbFree(pIdx->pTable->dbMem, p->u.z);
   504    504         }
   505    505       }
   506    506       sqlite3DbFree(dbMem, pIdx->aSample);
   507    507       pIdx->aSample = 0;
   508    508     }
          509  +#else
          510  +  UNUSED_PARAMETER(pIdx);
   509    511   #endif
   510    512   }
   511    513   
   512    514   /*
   513    515   ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
   514    516   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
   515    517   ** arrays. The contents of sqlite_stat2 are used to populate the
................................................................................
   605    607                   break;
   606    608                 }
   607    609               }
   608    610   
   609    611               assert( pIdx->aSample );
   610    612               {
   611    613                 IndexSample *pSample = &pIdx->aSample[iSample];
   612         -              pSample->eType = eType;
          614  +              pSample->eType = (u8)eType;
   613    615                 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
   614    616                   pSample->u.r = sqlite3_column_double(pStmt, 2);
   615    617                 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
   616    618                   const char *z = (const char *)(
   617    619                       (eType==SQLITE_BLOB) ?
   618    620                       sqlite3_column_blob(pStmt, 2):
   619    621                       sqlite3_column_text(pStmt, 2)
   620    622                   );
   621    623                   int n = sqlite3_column_bytes(pStmt, 2);
   622    624                   if( n>24 ){
   623    625                     n = 24;
   624    626                   }
   625         -                pSample->nByte = n;
          627  +                pSample->nByte = (u8)n;
   626    628                   pSample->u.z = sqlite3DbMallocRaw(dbMem, n);
   627    629                   if( pSample->u.z ){
   628    630                     memcpy(pSample->u.z, z, n);
   629    631                   }else{
   630    632                     db->mallocFailed = 1;
   631    633                     break;
   632    634                   }

Changes to src/callback.c.

    79     79   ** db for collation type name zName, length nName, or NULL, if no collation
    80     80   ** sequence can be found.
    81     81   **
    82     82   ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
    83     83   */
    84     84   CollSeq *sqlite3GetCollSeq(
    85     85     sqlite3* db,          /* The database connection */
    86         -  int enc,              /* The desired encoding for the collating sequence */
           86  +  u8 enc,               /* The desired encoding for the collating sequence */
    87     87     CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
    88     88     const char *zName     /* Collating sequence name */
    89     89   ){
    90     90     CollSeq *p;
    91     91   
    92     92     p = pColl;
    93     93     if( !p ){

Changes to src/main.c.

  1355   1355   /*
  1356   1356   ** Create a new collating function for database "db".  The name is zName
  1357   1357   ** and the encoding is enc.
  1358   1358   */
  1359   1359   static int createCollation(
  1360   1360     sqlite3* db,
  1361   1361     const char *zName, 
  1362         -  int enc,
  1363         -  int collType,
         1362  +  u8 enc,
         1363  +  u8 collType,
  1364   1364     void* pCtx,
  1365   1365     int(*xCompare)(void*,int,const void*,int,const void*),
  1366   1366     void(*xDel)(void*)
  1367   1367   ){
  1368   1368     CollSeq *pColl;
  1369   1369     int enc2;
  1370   1370     int nName = sqlite3Strlen30(zName);
................................................................................
  1804   1804     int enc, 
  1805   1805     void* pCtx,
  1806   1806     int(*xCompare)(void*,int,const void*,int,const void*)
  1807   1807   ){
  1808   1808     int rc;
  1809   1809     sqlite3_mutex_enter(db->mutex);
  1810   1810     assert( !db->mallocFailed );
  1811         -  rc = createCollation(db, zName, enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
         1811  +  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
  1812   1812     rc = sqlite3ApiExit(db, rc);
  1813   1813     sqlite3_mutex_leave(db->mutex);
  1814   1814     return rc;
  1815   1815   }
  1816   1816   
  1817   1817   /*
  1818   1818   ** Register a new collation sequence with the database handle db.
................................................................................
  1824   1824     void* pCtx,
  1825   1825     int(*xCompare)(void*,int,const void*,int,const void*),
  1826   1826     void(*xDel)(void*)
  1827   1827   ){
  1828   1828     int rc;
  1829   1829     sqlite3_mutex_enter(db->mutex);
  1830   1830     assert( !db->mallocFailed );
  1831         -  rc = createCollation(db, zName, enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
         1831  +  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
  1832   1832     rc = sqlite3ApiExit(db, rc);
  1833   1833     sqlite3_mutex_leave(db->mutex);
  1834   1834     return rc;
  1835   1835   }
  1836   1836   
  1837   1837   #ifndef SQLITE_OMIT_UTF16
  1838   1838   /*
................................................................................
  1847   1847   ){
  1848   1848     int rc = SQLITE_OK;
  1849   1849     char *zName8;
  1850   1850     sqlite3_mutex_enter(db->mutex);
  1851   1851     assert( !db->mallocFailed );
  1852   1852     zName8 = sqlite3Utf16to8(db, zName, -1);
  1853   1853     if( zName8 ){
  1854         -    rc = createCollation(db, zName8, enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
         1854  +    rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
  1855   1855       sqlite3DbFree(db, zName8);
  1856   1856     }
  1857   1857     rc = sqlite3ApiExit(db, rc);
  1858   1858     sqlite3_mutex_leave(db->mutex);
  1859   1859     return rc;
  1860   1860   }
  1861   1861   #endif /* SQLITE_OMIT_UTF16 */

Changes to src/os_unix.c.

   191    191     struct unixLockInfo *pLock;      /* Info about locks on this inode */
   192    192     int h;                           /* The file descriptor */
   193    193     int dirfd;                       /* File descriptor for the directory */
   194    194     unsigned char locktype;          /* The type of lock held on this fd */
   195    195     int lastErrno;                   /* The unix errno from the last I/O error */
   196    196     void *lockingContext;            /* Locking style specific state */
   197    197     UnixUnusedFd *pUnused;           /* Pre-allocated UnixUnusedFd */
          198  +  int fileFlags;                   /* Miscellanous flags */
   198    199   #if SQLITE_ENABLE_LOCKING_STYLE
   199    200     int openFlags;                   /* The flags specified at open() */
   200    201   #endif
   201    202   #if SQLITE_THREADSAFE && defined(__linux__)
   202    203     pthread_t tid;                   /* The thread that "owns" this unixFile */
   203    204   #endif
   204    205   #if OS_VXWORKS
................................................................................
   221    222     /* In test mode, increase the size of this structure a bit so that 
   222    223     ** it is larger than the struct CrashFile defined in test6.c.
   223    224     */
   224    225     char aPadding[32];
   225    226   #endif
   226    227   };
   227    228   
          229  +/*
          230  +** The following macros define bits in unixFile.fileFlags
          231  +*/
          232  +#define SQLITE_WHOLE_FILE_LOCKING  0x0001   /* Use whole-file locking */
          233  +
   228    234   /*
   229    235   ** Include code that is common to all os_*.c files
   230    236   */
   231    237   #include "os_common.h"
   232    238   
   233    239   /*
   234    240   ** Define various macros that are missing from some systems.
................................................................................
  1144   1150     
  1145   1151     unixLeaveMutex();
  1146   1152     OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1147   1153   
  1148   1154     *pResOut = reserved;
  1149   1155     return rc;
  1150   1156   }
         1157  +
         1158  +/*
         1159  +** Perform a file locking operation on a range of bytes in a file.
         1160  +** The "op" parameter should be one of F_RDLCK, F_WRLCK, or F_UNLCK.
         1161  +** Return 0 on success or -1 for failure.  On failure, write the error
         1162  +** code into *pErrcode.
         1163  +**
         1164  +** If the SQLITE_WHOLE_FILE_LOCKING bit is clear, then only lock
         1165  +** the range of bytes on the locking page between SHARED_FIRST and
         1166  +** SHARED_SIZE.  If SQLITE_WHOLE_FILE_LOCKING is set, then lock all
         1167  +** bytes from 0 up to but not including PENDING_BYTE, and all bytes
         1168  +** that follow SHARED_FIRST.
         1169  +**
         1170  +** In other words, of SQLITE_WHOLE_FILE_LOCKING if false (the historical
         1171  +** default case) then only lock a small range of bytes from SHARED_FIRST
         1172  +** through SHARED_FIRST+SHARED_SIZE-1.  But if SQLITE_WHOLE_FILE_LOCKING is
         1173  +** true then lock every byte in the file except for PENDING_BYTE and
         1174  +** RESERVED_BYTE.
         1175  +**
         1176  +** SQLITE_WHOLE_FILE_LOCKING=true overlaps SQLITE_WHOLE_FILE_LOCKING=false
         1177  +** and so the locking schemes are compatible.  One type of lock will
         1178  +** effectively exclude the other type.  The reason for using the
         1179  +** SQLITE_WHOLE_FILE_LOCKING=true is that by indicating the full range
         1180  +** of bytes to be read or written, we give hints to NFS to help it
         1181  +** maintain cache coherency.  On the other hand, whole file locking
         1182  +** is slower, so we don't want to use it except for NFS.
         1183  +*/
         1184  +static int rangeLock(unixFile *pFile, int op, int *pErrcode){
         1185  +  struct flock lock;
         1186  +  int rc;
         1187  +  lock.l_type = op;
         1188  +  lock.l_start = SHARED_FIRST;
         1189  +  lock.l_whence = SEEK_SET;
         1190  +  if( (pFile->fileFlags & SQLITE_WHOLE_FILE_LOCKING)==0 ){
         1191  +    lock.l_len = SHARED_SIZE;
         1192  +    rc = fcntl(pFile->h, F_SETLK, &lock);
         1193  +    *pErrcode = errno;
         1194  +  }else{
         1195  +    lock.l_len = 0;
         1196  +    rc = fcntl(pFile->h, F_SETLK, &lock);
         1197  +    *pErrcode = errno;
         1198  +    if( NEVER(op==F_UNLCK) || rc!=(-1) ){
         1199  +      lock.l_start = 0;
         1200  +      lock.l_len = PENDING_BYTE;
         1201  +      rc = fcntl(pFile->h, F_SETLK, &lock);
         1202  +      if( ALWAYS(op!=F_UNLCK) && rc==(-1) ){
         1203  +        *pErrcode = errno;
         1204  +        lock.l_type = F_UNLCK;
         1205  +        lock.l_start = SHARED_FIRST;
         1206  +        lock.l_len = 0;
         1207  +        fcntl(pFile->h, F_SETLK, &lock);
         1208  +      }
         1209  +    }
         1210  +  }
         1211  +  return rc;
         1212  +}
  1151   1213   
  1152   1214   /*
  1153   1215   ** Lock the file with the lock specified by parameter locktype - one
  1154   1216   ** of the following:
  1155   1217   **
  1156   1218   **     (1) SHARED_LOCK
  1157   1219   **     (2) RESERVED_LOCK
................................................................................
  1213   1275     ** even if the locking primitive used is always a write-lock.
  1214   1276     */
  1215   1277     int rc = SQLITE_OK;
  1216   1278     unixFile *pFile = (unixFile*)id;
  1217   1279     struct unixLockInfo *pLock = pFile->pLock;
  1218   1280     struct flock lock;
  1219   1281     int s;
         1282  +  int tErrno;
  1220   1283   
  1221   1284     assert( pFile );
  1222   1285     OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
  1223   1286         locktypeName(locktype), locktypeName(pFile->locktype),
  1224   1287         locktypeName(pLock->locktype), pLock->cnt , getpid());
  1225   1288   
  1226   1289     /* If there is already a lock of this type or more restrictive on the
................................................................................
  1229   1292     */
  1230   1293     if( pFile->locktype>=locktype ){
  1231   1294       OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
  1232   1295               locktypeName(locktype));
  1233   1296       return SQLITE_OK;
  1234   1297     }
  1235   1298   
  1236         -  /* Make sure the locking sequence is correct
         1299  +  /* Make sure the locking sequence is correct.
         1300  +  **  (1) We never move from unlocked to anything higher than shared lock.
         1301  +  **  (2) SQLite never explicitly requests a pendig lock.
         1302  +  **  (3) A shared lock is always held when a reserve lock is requested.
  1237   1303     */
  1238   1304     assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  1239   1305     assert( locktype!=PENDING_LOCK );
  1240   1306     assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  1241   1307   
  1242   1308     /* This mutex is needed because pFile->pLock is shared across threads
  1243   1309     */
................................................................................
  1273   1339       assert( pLock->cnt>0 );
  1274   1340       pFile->locktype = SHARED_LOCK;
  1275   1341       pLock->cnt++;
  1276   1342       pFile->pOpen->nLock++;
  1277   1343       goto end_lock;
  1278   1344     }
  1279   1345   
  1280         -  lock.l_len = 1L;
  1281         -
  1282         -  lock.l_whence = SEEK_SET;
  1283   1346   
  1284   1347     /* A PENDING lock is needed before acquiring a SHARED lock and before
  1285   1348     ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
  1286   1349     ** be released.
  1287   1350     */
         1351  +  lock.l_len = 1L;
         1352  +  lock.l_whence = SEEK_SET;
  1288   1353     if( locktype==SHARED_LOCK 
  1289   1354         || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
  1290   1355     ){
  1291   1356       lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
  1292   1357       lock.l_start = PENDING_BYTE;
  1293   1358       s = fcntl(pFile->h, F_SETLK, &lock);
  1294   1359       if( s==(-1) ){
  1295         -      int tErrno = errno;
         1360  +      tErrno = errno;
  1296   1361         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1297   1362         if( IS_LOCK_ERROR(rc) ){
  1298   1363           pFile->lastErrno = tErrno;
  1299   1364         }
  1300   1365         goto end_lock;
  1301   1366       }
  1302   1367     }
  1303   1368   
  1304   1369   
  1305   1370     /* If control gets to this point, then actually go ahead and make
  1306   1371     ** operating system calls for the specified lock.
  1307   1372     */
  1308   1373     if( locktype==SHARED_LOCK ){
  1309         -    int tErrno = 0;
  1310   1374       assert( pLock->cnt==0 );
  1311   1375       assert( pLock->locktype==0 );
  1312   1376   
  1313   1377       /* Now get the read-lock */
  1314         -    lock.l_start = SHARED_FIRST;
  1315         -    lock.l_len = SHARED_SIZE;
  1316         -    if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
  1317         -      tErrno = errno;
  1318         -    }
         1378  +    s = rangeLock(pFile, F_RDLCK, &tErrno);
         1379  +
  1319   1380       /* Drop the temporary PENDING lock */
  1320   1381       lock.l_start = PENDING_BYTE;
  1321   1382       lock.l_len = 1L;
  1322   1383       lock.l_type = F_UNLCK;
  1323   1384       if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
  1324   1385         if( s != -1 ){
  1325   1386           /* This could happen with a network mount */
................................................................................
  1351   1412       ** already.
  1352   1413       */
  1353   1414       assert( 0!=pFile->locktype );
  1354   1415       lock.l_type = F_WRLCK;
  1355   1416       switch( locktype ){
  1356   1417         case RESERVED_LOCK:
  1357   1418           lock.l_start = RESERVED_BYTE;
         1419  +        s = fcntl(pFile->h, F_SETLK, &lock);
         1420  +        tErrno = errno;
  1358   1421           break;
  1359   1422         case EXCLUSIVE_LOCK:
  1360         -        lock.l_start = SHARED_FIRST;
  1361         -        lock.l_len = SHARED_SIZE;
         1423  +        s = rangeLock(pFile, F_WRLCK, &tErrno);
  1362   1424           break;
  1363   1425         default:
  1364   1426           assert(0);
  1365   1427       }
  1366         -    s = fcntl(pFile->h, F_SETLK, &lock);
  1367   1428       if( s==(-1) ){
  1368         -      int tErrno = errno;
  1369   1429         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1370   1430         if( IS_LOCK_ERROR(rc) ){
  1371   1431           pFile->lastErrno = tErrno;
  1372   1432         }
  1373   1433       }
  1374   1434     }
  1375   1435     
................................................................................
  1453   1513   ** Lower the locking level on file descriptor pFile to locktype.  locktype
  1454   1514   ** must be either NO_LOCK or SHARED_LOCK.
  1455   1515   **
  1456   1516   ** If the locking level of the file descriptor is already at or below
  1457   1517   ** the requested locking level, this routine is a no-op.
  1458   1518   */
  1459   1519   static int unixUnlock(sqlite3_file *id, int locktype){
  1460         -  struct unixLockInfo *pLock;
  1461         -  struct flock lock;
  1462         -  int rc = SQLITE_OK;
  1463         -  unixFile *pFile = (unixFile*)id;
  1464         -  int h;
         1520  +  unixFile *pFile = (unixFile*)id; /* The open file */
         1521  +  struct unixLockInfo *pLock;      /* Structure describing current lock state */
         1522  +  struct flock lock;               /* Information passed into fcntl() */
         1523  +  int rc = SQLITE_OK;              /* Return code from this interface */
         1524  +  int h;                           /* The underlying file descriptor */
         1525  +  int tErrno;                      /* Error code from system call errors */
  1465   1526   
  1466   1527     assert( pFile );
  1467   1528     OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
  1468   1529         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1469   1530   
  1470   1531     assert( locktype<=SHARED_LOCK );
  1471   1532     if( pFile->locktype<=locktype ){
................................................................................
  1497   1558            || pFile->dbUpdate==0
  1498   1559            || pFile->transCntrChng==1 );
  1499   1560       pFile->inNormalWrite = 0;
  1500   1561   #endif
  1501   1562   
  1502   1563   
  1503   1564       if( locktype==SHARED_LOCK ){
  1504         -      lock.l_type = F_RDLCK;
  1505         -      lock.l_whence = SEEK_SET;
  1506         -      lock.l_start = SHARED_FIRST;
  1507         -      lock.l_len = SHARED_SIZE;
  1508         -      if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1509         -        int tErrno = errno;
         1565  +      if( rangeLock(pFile, F_RDLCK, &tErrno)==(-1) ){
  1510   1566           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
  1511   1567           if( IS_LOCK_ERROR(rc) ){
  1512   1568             pFile->lastErrno = tErrno;
  1513   1569           }
  1514   1570           goto end_unlock;
  1515   1571         }
  1516   1572       }
................................................................................
  1517   1573       lock.l_type = F_UNLCK;
  1518   1574       lock.l_whence = SEEK_SET;
  1519   1575       lock.l_start = PENDING_BYTE;
  1520   1576       lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
  1521   1577       if( fcntl(h, F_SETLK, &lock)!=(-1) ){
  1522   1578         pLock->locktype = SHARED_LOCK;
  1523   1579       }else{
  1524         -      int tErrno = errno;
         1580  +      tErrno = errno;
  1525   1581         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1526   1582         if( IS_LOCK_ERROR(rc) ){
  1527   1583           pFile->lastErrno = tErrno;
  1528   1584         }
  1529   1585         goto end_unlock;
  1530   1586       }
  1531   1587     }
................................................................................
  1543   1599         lock.l_start = lock.l_len = 0L;
  1544   1600         SimulateIOErrorBenign(1);
  1545   1601         SimulateIOError( h=(-1) )
  1546   1602         SimulateIOErrorBenign(0);
  1547   1603         if( fcntl(h, F_SETLK, &lock)!=(-1) ){
  1548   1604           pLock->locktype = NO_LOCK;
  1549   1605         }else{
  1550         -        int tErrno = errno;
         1606  +        tErrno = errno;
  1551   1607           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1552   1608           if( IS_LOCK_ERROR(rc) ){
  1553   1609             pFile->lastErrno = tErrno;
  1554   1610           }
  1555   1611           pLock->locktype = NO_LOCK;
  1556   1612           pFile->locktype = NO_LOCK;
  1557   1613         }
................................................................................
  1692   1748   
  1693   1749   /******************* End of the no-op lock implementation *********************
  1694   1750   ******************************************************************************/
  1695   1751   
  1696   1752   /******************************************************************************
  1697   1753   ************************* Begin dot-file Locking ******************************
  1698   1754   **
  1699         -** The dotfile locking implementation uses the existing of separate lock
         1755  +** The dotfile locking implementation uses the existance of separate lock
  1700   1756   ** files in order to control access to the database.  This works on just
  1701   1757   ** about every filesystem imaginable.  But there are serious downsides:
  1702   1758   **
  1703   1759   **    (1)  There is zero concurrency.  A single reader blocks all other
  1704   1760   **         connections from reading or writing the database.
  1705   1761   **
  1706   1762   **    (2)  An application crash or power loss can leave stale lock files
................................................................................
  3195   3251      LOCK,                       /* xLock */                                   \
  3196   3252      UNLOCK,                     /* xUnlock */                                 \
  3197   3253      CKLOCK,                     /* xCheckReservedLock */                      \
  3198   3254      unixFileControl,            /* xFileControl */                            \
  3199   3255      unixSectorSize,             /* xSectorSize */                             \
  3200   3256      unixDeviceCharacteristics   /* xDeviceCapabilities */                     \
  3201   3257   };                                                                           \
  3202         -static const sqlite3_io_methods *FINDER##Impl(const char *z, int h){         \
  3203         -  UNUSED_PARAMETER(z); UNUSED_PARAMETER(h);                                  \
         3258  +static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
         3259  +  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
  3204   3260     return &METHOD;                                                            \
  3205   3261   }                                                                            \
  3206         -static const sqlite3_io_methods *(*const FINDER)(const char*,int)            \
         3262  +static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
  3207   3263       = FINDER##Impl;
  3208   3264   
  3209   3265   /*
  3210   3266   ** Here are all of the sqlite3_io_methods objects for each of the
  3211   3267   ** locking strategies.  Functions that return pointers to these methods
  3212   3268   ** are also created.
  3213   3269   */
................................................................................
  3265   3321     afpClose,                 /* xClose method */
  3266   3322     afpLock,                  /* xLock method */
  3267   3323     afpUnlock,                /* xUnlock method */
  3268   3324     afpCheckReservedLock      /* xCheckReservedLock method */
  3269   3325   )
  3270   3326   #endif
  3271   3327   
         3328  +/*
         3329  +** The "Whole File Locking" finder returns the same set of methods as
         3330  +** the posix locking finder.  But it also sets the SQLITE_WHOLE_FILE_LOCKING
         3331  +** flag to force the posix advisory locks to cover the whole file instead
         3332  +** of just a small span of bytes near the 1GiB boundary.  Whole File Locking
         3333  +** is useful on NFS-mounted files since it helps NFS to maintain cache
         3334  +** coherency.  But it is a detriment to other filesystems since it runs
         3335  +** slower.
         3336  +*/
         3337  +static const sqlite3_io_methods *posixWflIoFinderImpl(const char*z, unixFile*p){
         3338  +  UNUSED_PARAMETER(z);
         3339  +  p->fileFlags = SQLITE_WHOLE_FILE_LOCKING;
         3340  +  return &posixIoMethods;
         3341  +}
         3342  +static const sqlite3_io_methods 
         3343  +  *(*const posixWflIoFinder)(const char*,unixFile *p) = posixWflIoFinderImpl;
         3344  +
  3272   3345   /*
  3273   3346   ** The proxy locking method is a "super-method" in the sense that it
  3274   3347   ** opens secondary file descriptors for the conch and lock files and
  3275   3348   ** it uses proxy, dot-file, AFP, and flock() locking methods on those
  3276   3349   ** secondary files.  For this reason, the division that implements
  3277   3350   ** proxy locking is located much further down in the file.  But we need
  3278   3351   ** to go ahead and define the sqlite3_io_methods and finder function
................................................................................
  3300   3373   ** for the database file "filePath".  It then returns the sqlite3_io_methods
  3301   3374   ** object that implements that strategy.
  3302   3375   **
  3303   3376   ** This is for MacOSX only.
  3304   3377   */
  3305   3378   static const sqlite3_io_methods *autolockIoFinderImpl(
  3306   3379     const char *filePath,    /* name of the database file */
  3307         -  int fd                   /* file descriptor open on the database file */
         3380  +  unixFile *pNew           /* open file object for the database file */
  3308   3381   ){
  3309   3382     static const struct Mapping {
  3310   3383       const char *zFilesystem;              /* Filesystem type name */
  3311   3384       const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
  3312   3385     } aMap[] = {
  3313   3386       { "hfs",    &posixIoMethods },
  3314   3387       { "ufs",    &posixIoMethods },
................................................................................
  3345   3418     ** Test byte-range lock using fcntl(). If the call succeeds, 
  3346   3419     ** assume that the file-system supports POSIX style locks. 
  3347   3420     */
  3348   3421     lockInfo.l_len = 1;
  3349   3422     lockInfo.l_start = 0;
  3350   3423     lockInfo.l_whence = SEEK_SET;
  3351   3424     lockInfo.l_type = F_RDLCK;
  3352         -  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
         3425  +  if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
         3426  +    pNew->fileFlags = SQLITE_WHOLE_FILE_LOCKING;
  3353   3427       return &posixIoMethods;
  3354   3428     }else{
  3355   3429       return &dotlockIoMethods;
  3356   3430     }
  3357   3431   }
  3358         -static const sqlite3_io_methods *(*const autolockIoFinder)(const char*,int)
  3359         -        = autolockIoFinderImpl;
         3432  +static const sqlite3_io_methods 
         3433  +  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
  3360   3434   
  3361   3435   #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
  3362   3436   
  3363   3437   #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
  3364   3438   /* 
  3365   3439   ** This "finder" function attempts to determine the best locking strategy 
  3366   3440   ** for the database file "filePath".  It then returns the sqlite3_io_methods
  3367   3441   ** object that implements that strategy.
  3368   3442   **
  3369   3443   ** This is for VXWorks only.
  3370   3444   */
  3371   3445   static const sqlite3_io_methods *autolockIoFinderImpl(
  3372   3446     const char *filePath,    /* name of the database file */
  3373         -  int fd                   /* file descriptor open on the database file */
         3447  +  unixFile *pNew           /* the open file object */
  3374   3448   ){
  3375   3449     struct flock lockInfo;
  3376   3450   
  3377   3451     if( !filePath ){
  3378   3452       /* If filePath==NULL that means we are dealing with a transient file
  3379   3453       ** that does not need to be locked. */
  3380   3454       return &nolockIoMethods;
................................................................................
  3383   3457     /* Test if fcntl() is supported and use POSIX style locks.
  3384   3458     ** Otherwise fall back to the named semaphore method.
  3385   3459     */
  3386   3460     lockInfo.l_len = 1;
  3387   3461     lockInfo.l_start = 0;
  3388   3462     lockInfo.l_whence = SEEK_SET;
  3389   3463     lockInfo.l_type = F_RDLCK;
  3390         -  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
         3464  +  if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
  3391   3465       return &posixIoMethods;
  3392   3466     }else{
  3393   3467       return &semIoMethods;
  3394   3468     }
  3395   3469   }
  3396         -static const sqlite3_io_methods *(*const autolockIoFinder)(const char*,int)
  3397         -        = autolockIoFinderImpl;
         3470  +static const sqlite3_io_methods 
         3471  +  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
  3398   3472   
  3399   3473   #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
  3400   3474   
  3401   3475   /*
  3402   3476   ** An abstract type for a pointer to a IO method finder function:
  3403   3477   */
  3404         -typedef const sqlite3_io_methods *(*finder_type)(const char*,int);
         3478  +typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
  3405   3479   
  3406   3480   
  3407   3481   /****************************************************************************
  3408   3482   **************************** sqlite3_vfs methods ****************************
  3409   3483   **
  3410   3484   ** This division contains the implementation of methods on the
  3411   3485   ** sqlite3_vfs object.
................................................................................
  3435   3509     */
  3436   3510     UNUSED_PARAMETER(isDelete);
  3437   3511   
  3438   3512     OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
  3439   3513     pNew->h = h;
  3440   3514     pNew->dirfd = dirfd;
  3441   3515     SET_THREADID(pNew);
         3516  +  pNew->fileFlags = 0;
  3442   3517   
  3443   3518   #if OS_VXWORKS
  3444   3519     pNew->pId = vxworksFindFileId(zFilename);
  3445   3520     if( pNew->pId==0 ){
  3446   3521       noLock = 1;
  3447   3522       rc = SQLITE_NOMEM;
  3448   3523     }
  3449   3524   #endif
  3450   3525   
  3451   3526     if( noLock ){
  3452   3527       pLockingStyle = &nolockIoMethods;
  3453   3528     }else{
  3454         -    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, h);
         3529  +    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
  3455   3530   #if SQLITE_ENABLE_LOCKING_STYLE
  3456   3531       /* Cache zFilename in the locking context (AFP and dotlock override) for
  3457   3532       ** proxyLock activation is possible (remote proxy is based on db name)
  3458   3533       ** zFilename remains valid until file is closed, to support */
  3459   3534       pNew->lockingContext = (void*)zFilename;
  3460   3535   #endif
  3461   3536     }
................................................................................
  5262   5337   #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
  5263   5338       UNIXVFS("unix",          autolockIoFinder ),
  5264   5339   #else
  5265   5340       UNIXVFS("unix",          posixIoFinder ),
  5266   5341   #endif
  5267   5342       UNIXVFS("unix-none",     nolockIoFinder ),
  5268   5343       UNIXVFS("unix-dotfile",  dotlockIoFinder ),
         5344  +    UNIXVFS("unix-wfl",      posixWflIoFinder ),
  5269   5345   #if OS_VXWORKS
  5270   5346       UNIXVFS("unix-namedsem", semIoFinder ),
  5271   5347   #endif
  5272   5348   #if SQLITE_ENABLE_LOCKING_STYLE
  5273   5349       UNIXVFS("unix-posix",    posixIoFinder ),
  5274   5350   #if !OS_VXWORKS
  5275   5351       UNIXVFS("unix-flock",    flockIoFinder ),

Changes to src/resolve.c.

   257    257             if( iCol<0 ){
   258    258               pExpr->affinity = SQLITE_AFF_INTEGER;
   259    259             }else if( pExpr->iTable==0 ){
   260    260               testcase( iCol==31 );
   261    261               testcase( iCol==32 );
   262    262               pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   263    263             }
   264         -          pExpr->iColumn = iCol;
          264  +          pExpr->iColumn = (i16)iCol;
   265    265             pExpr->pTab = pTab;
   266    266             isTrigger = 1;
   267    267           }
   268    268         }
   269    269       }
   270    270   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
   271    271   

Changes to src/sqliteInt.h.

  2801   2801   int sqlite3ValueBytes(sqlite3_value*, u8);
  2802   2802   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  2803   2803                           void(*)(void*));
  2804   2804   void sqlite3ValueFree(sqlite3_value*);
  2805   2805   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  2806   2806   char *sqlite3Utf16to8(sqlite3 *, const void*, int);
  2807   2807   #ifdef SQLITE_ENABLE_STAT2
  2808         -char *sqlite3Utf8to16(sqlite3 *, int, char *, int, int *);
         2808  +char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
  2809   2809   #endif
  2810   2810   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2811   2811   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2812   2812   #ifndef SQLITE_AMALGAMATION
  2813   2813   extern const unsigned char sqlite3UpperToLower[];
  2814   2814   extern const unsigned char sqlite3CtypeMap[];
  2815   2815   extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
................................................................................
  2827   2827   void sqlite3SelectPrep(Parse*, Select*, NameContext*);
  2828   2828   int sqlite3ResolveExprNames(NameContext*, Expr*);
  2829   2829   void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
  2830   2830   int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  2831   2831   void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  2832   2832   void sqlite3AlterFinishAddColumn(Parse *, Token *);
  2833   2833   void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  2834         -CollSeq *sqlite3GetCollSeq(sqlite3*, int, CollSeq *, const char*);
         2834  +CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
  2835   2835   char sqlite3AffinityType(const char*);
  2836   2836   void sqlite3Analyze(Parse*, Token*, Token*);
  2837   2837   int sqlite3InvokeBusyHandler(BusyHandler*);
  2838   2838   int sqlite3FindDb(sqlite3*, Token*);
  2839   2839   int sqlite3FindDbName(sqlite3 *, const char *);
  2840   2840   int sqlite3AnalysisLoad(sqlite3*,int iDB);
  2841   2841   void sqlite3DeleteIndexSamples(Index*);

Changes to src/trigger.c.

   691    691       **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
   692    692       **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
   693    693       **   END;
   694    694       **
   695    695       **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
   696    696       **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
   697    697       */
   698         -    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:orconf;
          698  +    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
   699    699   
   700    700       switch( pStep->op ){
   701    701         case TK_UPDATE: {
   702    702           sqlite3Update(pParse, 
   703    703             targetSrcList(pParse, pStep),
   704    704             sqlite3ExprListDup(db, pStep->pExprList, 0), 
   705    705             sqlite3ExprDup(db, pStep->pWhere, 0), 
................................................................................
   950    950     Table *pTab,         /* The table to code triggers from */
   951    951     int newIdx,          /* The indice of the "new" row to access */
   952    952     int oldIdx,          /* The indice of the "old" row to access */
   953    953     int orconf,          /* ON CONFLICT policy */
   954    954     int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
   955    955   ){
   956    956     Trigger *p;
          957  +
          958  +  UNUSED_PARAMETER(newIdx);
   957    959   
   958    960     assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
   959    961     assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
   960    962   
   961    963     for(p=pTrigger; p; p=p->pNext){
   962    964   
   963    965       /* Sanity checking:  The schema for the trigger and for the table are

Changes to src/update.c.

   123    123     u32 oldmask = 0;        /* Mask of OLD.* columns in use */
   124    124   
   125    125     /* Register Allocations */
   126    126     int regRowCount = 0;   /* A count of rows changed */
   127    127     int regOldRowid;       /* The old rowid */
   128    128     int regNewRowid;       /* The new rowid */
   129    129     int regNew;
   130         -  int regOld;
          130  +  int regOld = 0;
   131    131     int regRowSet = 0;     /* Rowset of rows to be updated */
   132    132     int regRec;            /* Register used for new table record to insert */
   133    133   
   134    134     memset(&sContext, 0, sizeof(sContext));
   135    135     db = pParse->db;
   136    136     if( pParse->nErr || db->mallocFailed ){
   137    137       goto update_cleanup;

Changes to src/utf.c.

   461    461   ** arrange to call sqlite3DbFree() on the returned pointer when it is
   462    462   ** no longer required.
   463    463   ** 
   464    464   ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
   465    465   ** flag set.
   466    466   */
   467    467   #ifdef SQLITE_ENABLE_STAT2
   468         -char *sqlite3Utf8to16(sqlite3 *db, int enc, char *z, int n, int *pnOut){
          468  +char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
   469    469     Mem m;
   470    470     memset(&m, 0, sizeof(m));
   471    471     m.db = db;
   472    472     sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
   473    473     if( sqlite3VdbeMemTranslate(&m, enc) ){
   474    474       assert( db->mallocFailed );
   475    475       return 0;

Changes to src/util.c.

   360    360       /* In the IEEE 754 standard, zero is signed.
   361    361       ** Add the sign if we've seen at least one digit */
   362    362       result = (sign<0 && nDigits) ? -(double)0 : (double)0;
   363    363     } else {
   364    364       /* attempt to reduce exponent */
   365    365       if( esign>0 ){
   366    366         while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
          367  +    }else{
          368  +      while( !(s%10) && e>0 ) e--,s/=10;
   367    369       }
   368    370   
   369    371       /* adjust the sign of significand */
   370    372       s = sign<0 ? -s : s;
   371    373   
   372    374       /* if exponent, scale significand as appropriate
   373    375       ** and store in result. */
   374    376       if( e ){
   375    377         double scale = 1.0;
   376         -      while( e>=16 ){ scale *= 1.0e+16; e -= 16; }
   377         -      while( e>=4 ){ scale *= 1.0e+4; e -= 4; }
   378         -      while( e>=1 ){ scale *= 1.0e+1; e -= 1; }
   379         -      if( esign<0 ){
   380         -        result = s / scale;
          378  +      /* attempt to handle extremely small/large numbers better */
          379  +      if( e>307 && e<342 ){
          380  +        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
          381  +        if( esign<0 ){
          382  +          result = s / scale;
          383  +          result /= 1.0e+308;
          384  +        }else{
          385  +          result = s * scale;
          386  +          result *= 1.0e+308;
          387  +        }
   381    388         }else{
   382         -        result = s * scale;
          389  +        /* 1.0e+22 is the largest power of 10 than can be 
          390  +        ** represented exactly. */
          391  +        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
          392  +        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
          393  +        if( esign<0 ){
          394  +          result = s / scale;
          395  +        }else{
          396  +          result = s * scale;
          397  +        }
   383    398         }
   384    399       } else {
   385    400         result = (double)s;
   386    401       }
   387    402     }
   388    403   
   389    404     /* store the result */

Changes to src/vdbe.c.

  4859   4859     pFrame->pParent = p->pFrame;
  4860   4860     pFrame->lastRowid = db->lastRowid;
  4861   4861     pFrame->nChange = p->nChange;
  4862   4862     p->nChange = 0;
  4863   4863     p->pFrame = pFrame;
  4864   4864     p->aMem = &VdbeFrameMem(pFrame)[-1];
  4865   4865     p->nMem = pFrame->nChildMem;
  4866         -  p->nCursor = pFrame->nChildCsr;
         4866  +  p->nCursor = (u16)pFrame->nChildCsr;
  4867   4867     p->apCsr = (VdbeCursor **)&p->aMem[p->nMem+1];
  4868   4868     p->aOp = pProgram->aOp;
  4869   4869     p->nOp = pProgram->nOp;
  4870   4870     pc = -1;
  4871   4871   
  4872   4872     break;
  4873   4873   }

Changes to src/vdbeaux.c.

  2756   2756   int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
  2757   2757     i64 nCellKey = 0;
  2758   2758     int rc;
  2759   2759     u32 szHdr;        /* Size of the header */
  2760   2760     u32 typeRowid;    /* Serial type of the rowid */
  2761   2761     u32 lenRowid;     /* Size of the rowid */
  2762   2762     Mem m, v;
         2763  +
         2764  +  UNUSED_PARAMETER(db);
  2763   2765   
  2764   2766     /* Get the size of the index entry.  Only indices entries of less
  2765   2767     ** than 2GiB are support - anything large must be database corruption.
  2766   2768     ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
  2767   2769     ** this code can safely assume that nCellKey is 32-bits  
  2768   2770     */
  2769   2771     assert( sqlite3BtreeCursorIsValid(pCur) );

Changes to src/where.c.

  2353   2353       **
  2354   2354       ** Estimate the number of rows of output.  For an IN operator,
  2355   2355       ** do not let the estimate exceed half the rows in the table.
  2356   2356       */
  2357   2357       nRow = (double)(aiRowEst[nEq] * nInMul);
  2358   2358       if( bInEst && nRow*2>aiRowEst[0] ){
  2359   2359         nRow = aiRowEst[0]/2;
  2360         -      nInMul = nRow / aiRowEst[nEq];
         2360  +      nInMul = (int)(nRow / aiRowEst[nEq]);
  2361   2361       }
  2362   2362   
  2363   2363       /* Assume constant cost to access a row and logarithmic cost to
  2364   2364       ** do a binary search.  Hence, the initial cost is the number of output
  2365   2365       ** rows plus log2(table-size) times the number of binary searches.
  2366   2366       */
  2367   2367       cost = nRow + nInMul*estLog(aiRowEst[0]);