System.Data.SQLite
Check-in [46a5ce61ca]
Not logged in

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

Overview
Comment:no message
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1: 46a5ce61ca85ddcee54776948af6d33d98eb168a
User & Date: rmsimpson 2006-01-23 19:41:55
Context
2006-01-23
23:20
no message check-in: 7aa1c190a9 user: rmsimpson tags: sourceforge
19:41
no message check-in: 46a5ce61ca user: rmsimpson tags: sourceforge
05:16
no message check-in: 29ec851a72 user: rmsimpson tags: sourceforge
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/interop.c.

    53     53     /* If this is an EXPLAIN, skip this step */
    54     54     if( pParse->explain ){
    55     55       return;
    56     56     }
    57     57   #endif
    58     58   
    59     59     assert( v!=0 );
    60         -  if( pParse->colNamesSet || v==0 || sqlite3ThreadData()->mallocFailed ) return;
           60  +  if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return;
    61     61     pParse->colNamesSet = 1;
    62     62     fullNames = (db->flags & SQLITE_FullColNames)!=0;
    63     63     shortNames = (db->flags & SQLITE_ShortColNames)!=0;
    64     64     if (realNames) fullNames = 1;                      /*** ADDED - SQLite.Interop ***/
    65     65   
    66     66     sqlite3VdbeSetNumCols(v, pEList->nExpr);
    67     67     for(i=0; i<pEList->nExpr; i++){

Changes to SQLite.Interop/merge.h.

     1      1   // This code was automatically generated from assembly
     2         -// D:\src\SQLite.NET.Test\System.Data.SQLite\bin\CompactFramework\System.Data.SQLite.dll
            2  +// C:\src\SQLite.NET\System.Data.SQLite\bin\CompactFramework\System.Data.SQLite.dll
     3      3   
     4      4   #include <windef.h>
     5      5   
     6      6   #pragma data_seg(".clr")
     7      7   #pragma comment(linker, "/SECTION:.clr,ER")
     8         -  char __ph[85156] = {0}; // The number of bytes to reserve
            8  +  char __ph[85184] = {0}; // The number of bytes to reserve
     9      9   #pragma data_seg()
    10     10   
    11     11   typedef BOOL (WINAPI *DLLMAIN)(HANDLE, DWORD, LPVOID);
    12     12   extern BOOL WINAPI _DllMainCRTStartup(HANDLE, DWORD, LPVOID);
    13     13   
    14     14   __declspec(dllexport) BOOL WINAPI _CorDllMainStub(HANDLE hModule, DWORD dwReason, LPVOID pvReserved)
    15     15   {

Changes to SQLite.Interop/merge_full.h.

     1      1   // This code was automatically generated from assembly
     2         -// D:\src\SQLite.NET.Test\System.Data.SQLite\bin\System.Data.SQLite.dll
            2  +// C:\src\SQLite.NET\System.Data.SQLite\bin\System.Data.SQLite.dll
     3      3   
     4      4   #include <windef.h>
     5      5   
     6      6   #pragma data_seg(".clr")
     7      7   #pragma comment(linker, "/SECTION:.clr,ER")
     8         -  char __ph[96452] = {0}; // The number of bytes to reserve
            8  +  char __ph[96380] = {0}; // The number of bytes to reserve
     9      9   #pragma data_seg()
    10     10   
    11     11   typedef BOOL (WINAPI *DLLMAIN)(HANDLE, DWORD, LPVOID);
    12     12   extern BOOL WINAPI _DllMainCRTStartup(HANDLE, DWORD, LPVOID);
    13     13   
    14     14   __declspec(dllexport) BOOL WINAPI _CorDllMainStub(HANDLE hModule, DWORD dwReason, LPVOID pvReserved)
    15     15   {

Changes to SQLite.Interop/src/alter.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14     14   **
    15         -** $Id: alter.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: alter.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** The code in this file only exists if we are not omitting the
    22     22   ** ALTER TABLE logic from the build.
................................................................................
   158    158   #ifndef SQLITE_OMIT_TRIGGER
   159    159       { "sqlite_rename_trigger",  2, renameTriggerFunc},
   160    160   #endif
   161    161     };
   162    162     int i;
   163    163   
   164    164     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   165         -    sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
          165  +    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
   166    166           SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
   167    167     }
   168    168   }
   169    169   
   170    170   /*
   171    171   ** Generate the text of a WHERE expression which can be used to select all
   172    172   ** temporary triggers on table pTab from the sqlite_temp_master table. If
................................................................................
   263    263     char *zName = 0;          /* NULL-terminated version of pName */ 
   264    264     sqlite3 *db = pParse->db; /* Database connection */
   265    265     Vdbe *v;
   266    266   #ifndef SQLITE_OMIT_TRIGGER
   267    267     char *zWhere = 0;         /* Where clause to locate temp triggers */
   268    268   #endif
   269    269     
   270         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_rename_table;
          270  +  if( sqlite3MallocFailed() ) goto exit_rename_table;
   271    271     assert( pSrc->nSrc==1 );
   272    272   
   273    273     pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   274    274     if( !pTab ) goto exit_rename_table;
   275    275     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   276    276     zDb = db->aDb[iDb].zName;
   277    277   
................................................................................
   497    497     Vdbe *v;
   498    498     int iDb;
   499    499     int i;
   500    500     int nAlloc;
   501    501   
   502    502     /* Look up the table being altered. */
   503    503     assert( pParse->pNewTable==0 );
   504         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_begin_add_column;
          504  +  if( sqlite3MallocFailed() ) goto exit_begin_add_column;
   505    505     pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   506    506     if( !pTab ) goto exit_begin_add_column;
   507    507   
   508    508     /* Make sure this is not an attempt to ALTER a view. */
   509    509     if( pTab->pSelect ){
   510    510       sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
   511    511       goto exit_begin_add_column;

Changes to SQLite.Interop/src/analyze.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
    13     13   **
    14         -** @(#) $Id: analyze.c,v 1.9 2006/01/16 15:51:47 rmsimpson Exp $
           14  +** @(#) $Id: analyze.c,v 1.10 2006/01/23 19:45:55 rmsimpson Exp $
    15     15   */
    16     16   #ifndef SQLITE_OMIT_ANALYZE
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** This routine generates code that opens the sqlite_stat1 table on cursor
    21     21   ** iStatCur.

Changes to SQLite.Interop/src/attach.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the ATTACH and DETACH commands.
    13     13   **
    14         -** $Id: attach.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           14  +** $Id: attach.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /*
    19     19   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    20     20   ** is slightly different from resolving a normal SQL expression, because simple
    21     21   ** identifiers are treated as strings, not possible column names or aliases.
................................................................................
   268    268     int rc;
   269    269     NameContext sName;
   270    270     Vdbe *v;
   271    271     FuncDef *pFunc;
   272    272     sqlite3* db = pParse->db;
   273    273   
   274    274   #ifndef SQLITE_OMIT_AUTHORIZATION
   275         -  assert( sqlite3ThreadDataReadOnly()->mallocFailed || pAuthArg );
          275  +  assert( sqlite3MallocFailed() || pAuthArg );
   276    276     if( pAuthArg ){
   277    277       char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span);
   278    278       if( !zAuthArg ){
   279    279         goto attach_end;
   280    280       }
   281    281       rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   282    282       sqliteFree(zAuthArg);
................................................................................
   299    299     }
   300    300   
   301    301     v = sqlite3GetVdbe(pParse);
   302    302     sqlite3ExprCode(pParse, pFilename);
   303    303     sqlite3ExprCode(pParse, pDbname);
   304    304     sqlite3ExprCode(pParse, pKey);
   305    305   
   306         -  assert(v || sqlite3ThreadDataReadOnly()->mallocFailed);
          306  +  assert( v || sqlite3MallocFailed() );
   307    307     if( v ){
   308    308       sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
   309    309       pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
   310    310       sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
   311    311   
   312    312       /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
   313    313       ** statement only). For DETACH, set it to false (expire all existing
................................................................................
   341    341   }
   342    342   
   343    343   /*
   344    344   ** Register the functions sqlite_attach and sqlite_detach.
   345    345   */
   346    346   void sqlite3AttachFunctions(sqlite3 *db){
   347    347     static const int enc = SQLITE_UTF8;
   348         -  sqlite3_create_function(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0);
   349         -  sqlite3_create_function(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0);
          348  +  sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0);
          349  +  sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0);
   350    350   }
   351    351   
   352    352   /*
   353    353   ** Initialize a DbFixer structure.  This routine must be called prior
   354    354   ** to passing the structure to one of the sqliteFixAAAA() routines below.
   355    355   **
   356    356   ** The return value indicates whether or not fixation is required.  TRUE

Changes to SQLite.Interop/src/auth.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the sqlite3_set_authorizer()
    13     13   ** API.  This facility is an optional feature of the library.  Embedded
    14     14   ** systems that do not need this facility may omit it by recompiling
    15     15   ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
    16     16   **
    17         -** $Id: auth.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           17  +** $Id: auth.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** All of the code in this file may be omitted by defining a single
    23     23   ** macro.
    24     24   */

Changes to SQLite.Interop/src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
           12  +** $Id: btree.c,v 1.18 2006/01/23 19:45:55 rmsimpson Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
   458    458   
   459    459   /*
   460    460   ** Routines to read and write variable-length integers.  These used to
   461    461   ** be defined locally, but now we use the varint routines in the util.c
   462    462   ** file.
   463    463   */
   464    464   #define getVarint    sqlite3GetVarint
   465         -#define getVarint32  sqlite3GetVarint32
          465  +/* #define getVarint32  sqlite3GetVarint32 */
          466  +#define getVarint32(A,B)  ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
   466    467   #define putVarint    sqlite3PutVarint
   467    468   
   468    469   /* The database page the PENDING_BYTE occupies. This page is never used.
   469    470   ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
   470    471   ** should possibly be consolidated (presumably in pager.h).
   471    472   */
   472    473   #define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
................................................................................
   502    503     #define lockTable(a,b,c) SQLITE_OK
   503    504     #define unlockAllTables(a)
   504    505     #define restoreOrClearCursorPosition(a,b) SQLITE_OK
   505    506     #define saveAllCursors(a,b,c) SQLITE_OK
   506    507   
   507    508   #else
   508    509   
          510  +static void releasePage(MemPage *pPage);
          511  +
   509    512   /*
   510    513   ** Save the current cursor position in the variables BtCursor.nKey 
   511    514   ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
   512    515   */
   513    516   static int saveCursorPosition(BtCursor *pCur){
   514         -  int rc = SQLITE_OK;
          517  +  int rc;
   515    518   
   516         -  assert( CURSOR_VALID==pCur->eState|| CURSOR_INVALID==pCur->eState );
          519  +  assert( CURSOR_VALID==pCur->eState );
   517    520     assert( 0==pCur->pKey );
   518    521   
   519         -  if( pCur->eState==CURSOR_VALID ){
   520         -    rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
   521         -
   522         -    /* If this is an intKey table, then the above call to BtreeKeySize()
   523         -    ** stores the integer key in pCur->nKey. In this case this value is
   524         -    ** all that is required. Otherwise, if pCur is not open on an intKey
   525         -    ** table, then malloc space for and store the pCur->nKey bytes of key 
   526         -    ** data.
   527         -    */
   528         -    if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
   529         -      void *pKey = sqliteMalloc(pCur->nKey);
   530         -      if( pKey ){
   531         -        rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
   532         -        if( rc==SQLITE_OK ){
   533         -          pCur->pKey = pKey;
   534         -        }else{
   535         -          sqliteFree(pKey);
   536         -        }
   537         -      }else{
   538         -        rc = SQLITE_NOMEM;
   539         -      }
   540         -    }
   541         -    assert( !pCur->pPage->intKey || !pCur->pKey );
   542         -
   543         -    /* Todo: Should we drop the reference to pCur->pPage here? */
   544         -
   545         -    if( rc==SQLITE_OK ){
   546         -      pCur->eState = CURSOR_REQUIRESEEK;
   547         -    }
          522  +  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
          523  +
          524  +  /* If this is an intKey table, then the above call to BtreeKeySize()
          525  +  ** stores the integer key in pCur->nKey. In this case this value is
          526  +  ** all that is required. Otherwise, if pCur is not open on an intKey
          527  +  ** table, then malloc space for and store the pCur->nKey bytes of key 
          528  +  ** data.
          529  +  */
          530  +  if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
          531  +    void *pKey = sqliteMalloc(pCur->nKey);
          532  +    if( pKey ){
          533  +      rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
          534  +      if( rc==SQLITE_OK ){
          535  +        pCur->pKey = pKey;
          536  +      }else{
          537  +        sqliteFree(pKey);
          538  +      }
          539  +    }else{
          540  +      rc = SQLITE_NOMEM;
          541  +    }
          542  +  }
          543  +  assert( !pCur->pPage->intKey || !pCur->pKey );
          544  +
          545  +  if( rc==SQLITE_OK ){
          546  +    releasePage(pCur->pPage);
          547  +    pCur->pPage = 0;
          548  +    pCur->eState = CURSOR_REQUIRESEEK;
   548    549     }
   549    550   
   550    551     return rc;
   551    552   }
   552    553   
   553    554   /*
   554    555   ** Save the positions of all cursors except pExcept open on the table 
................................................................................
   581    582   ** returning the cursor to it's saved position, any saved position is deleted
   582    583   ** and the cursor state set to CURSOR_INVALID.
   583    584   */
   584    585   static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){
   585    586     int rc = SQLITE_OK;
   586    587     assert( sqlite3ThreadDataReadOnly()->useSharedData );
   587    588     assert( pCur->eState==CURSOR_REQUIRESEEK );
          589  +  pCur->eState = CURSOR_INVALID;
   588    590     if( doSeek ){
   589    591       rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
   590         -  }else{
   591         -    pCur->eState = CURSOR_INVALID;
   592    592     }
   593    593     if( rc==SQLITE_OK ){
   594    594       sqliteFree(pCur->pKey);
   595    595       pCur->pKey = 0;
   596    596       assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
   597    597     }
   598    598     return rc;
................................................................................
   917    917     n = pPage->childPtrSize;
   918    918     assert( n==4-4*pPage->leaf );
   919    919     if( pPage->hasData ){
   920    920       n += getVarint32(&pCell[n], &nPayload);
   921    921     }else{
   922    922       nPayload = 0;
   923    923     }
   924         -  n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
   925         -  pInfo->nHeader = n;
   926    924     pInfo->nData = nPayload;
   927         -  if( !pPage->intKey ){
   928         -    nPayload += pInfo->nKey;
          925  +  if( pPage->intKey ){
          926  +    n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
          927  +  }else{
          928  +    u32 x;
          929  +    n += getVarint32(&pCell[n], &x);
          930  +    pInfo->nKey = x;
          931  +    nPayload += x;
   929    932     }
          933  +  pInfo->nHeader = n;
   930    934     if( nPayload<=pPage->maxLocal ){
   931    935       /* This is the (easy) common case where the entire payload fits
   932    936       ** on the local page.  No overflow is required.
   933    937       */
   934    938       int nSize;          /* Total size of cell content in bytes */
   935    939       pInfo->nLocal = nPayload;
   936    940       pInfo->iOverflow = 0;
................................................................................
  1658   1662   #endif
  1659   1663     }
  1660   1664     pBt->usableSize = pBt->pageSize - nReserve;
  1661   1665     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
  1662   1666     sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
  1663   1667   
  1664   1668   #ifndef SQLITE_OMIT_SHARED_CACHE
  1665         -  /* Add the new btree to the linked list starting at ThreadData.pBtree */
         1669  +  /* Add the new btree to the linked list starting at ThreadData.pBtree.
         1670  +  ** There is no chance that a malloc() may fail inside of the 
         1671  +  ** sqlite3ThreadData() call, as the ThreadData structure must have already
         1672  +  ** been allocated for pTsdro->useSharedData to be non-zero.
         1673  +  */
  1666   1674     if( pTsdro->useSharedData && zFilename && !isMemdb ){
  1667   1675       pBt->pNext = pTsdro->pBtree;
  1668   1676       sqlite3ThreadData()->pBtree = pBt;
  1669   1677     }
  1670   1678   #endif
  1671   1679     pBt->nRef = 1;
  1672   1680     *ppBtree = p;
................................................................................
  1708   1716     */
  1709   1717     assert( pBt->nRef>0 );
  1710   1718     pBt->nRef--;
  1711   1719     if( pBt->nRef ){
  1712   1720       return SQLITE_OK;
  1713   1721     }
  1714   1722   
  1715         -  /* Remove the shared-btree from the thread wide list */
  1716         -  pTsd = sqlite3ThreadData();
         1723  +  /* Remove the shared-btree from the thread wide list. Call 
         1724  +  ** ThreadDataReadOnly() and then cast away the const property of the 
         1725  +  ** pointer to avoid allocating thread data if it is not really required.
         1726  +  */
         1727  +  pTsd = (ThreadData *)sqlite3ThreadDataReadOnly();
  1717   1728     if( pTsd->pBtree==pBt ){
         1729  +    assert( pTsd==sqlite3ThreadData() );
  1718   1730       pTsd->pBtree = pBt->pNext;
  1719   1731     }else{
  1720   1732       BtShared *pPrev;
  1721   1733       for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext);
  1722   1734       if( pPrev ){
         1735  +      assert( pTsd==sqlite3ThreadData() );
  1723   1736         pPrev->pNext = pBt->pNext;
  1724   1737       }
  1725   1738     }
  1726   1739   #endif
  1727   1740   
  1728   1741     /* Close the pager and free the shared-btree structure */
  1729   1742     assert( !pBt->pCursor );
................................................................................
  2433   2446   /*
  2434   2447   ** Commit the transaction currently in progress.
  2435   2448   **
  2436   2449   ** This will release the write lock on the database file.  If there
  2437   2450   ** are no active cursors, it also releases the read lock.
  2438   2451   */
  2439   2452   int sqlite3BtreeCommit(Btree *p){
  2440         -  int rc = SQLITE_OK;
  2441   2453     BtShared *pBt = p->pBt;
  2442   2454   
  2443   2455     btreeIntegrity(p);
  2444         -  unlockAllTables(p);
  2445   2456   
  2446   2457     /* If the handle has a write-transaction open, commit the shared-btrees 
  2447   2458     ** transaction and set the shared state to TRANS_READ.
  2448   2459     */
  2449   2460     if( p->inTrans==TRANS_WRITE ){
         2461  +    int rc;
  2450   2462       assert( pBt->inTransaction==TRANS_WRITE );
  2451   2463       assert( pBt->nTransaction>0 );
  2452   2464       rc = sqlite3pager_commit(pBt->pPager);
         2465  +    if( rc!=SQLITE_OK ){
         2466  +      return rc;
         2467  +    }
  2453   2468       pBt->inTransaction = TRANS_READ;
  2454   2469       pBt->inStmt = 0;
  2455   2470     }
         2471  +  unlockAllTables(p);
  2456   2472   
  2457   2473     /* If the handle has any kind of transaction open, decrement the transaction
  2458   2474     ** count of the shared btree. If the transaction count reaches 0, set
  2459   2475     ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
  2460   2476     ** will unlock the pager.
  2461   2477     */
  2462   2478     if( p->inTrans!=TRANS_NONE ){
................................................................................
  2469   2485     /* Set the handles current transaction state to TRANS_NONE and unlock
  2470   2486     ** the pager if this call closed the only read or write transaction.
  2471   2487     */
  2472   2488     p->inTrans = TRANS_NONE;
  2473   2489     unlockBtreeIfUnused(pBt);
  2474   2490   
  2475   2491     btreeIntegrity(p);
  2476         -  return rc;
         2492  +  return SQLITE_OK;
  2477   2493   }
  2478   2494   
  2479   2495   #ifndef NDEBUG
  2480   2496   /*
  2481   2497   ** Return the number of write-cursors open on this handle. This is for use
  2482   2498   ** in assert() expressions, so it is only compiled if NDEBUG is not
  2483   2499   ** defined.
................................................................................
  2488   2504     for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
  2489   2505       if( pCur->wrFlag ) r++; 
  2490   2506     }
  2491   2507     return r;
  2492   2508   }
  2493   2509   #endif
  2494   2510   
  2495         -#ifdef SQLITE_TEST
         2511  +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  2496   2512   /*
  2497   2513   ** Print debugging information about all cursors to standard output.
  2498   2514   */
  2499   2515   void sqlite3BtreeCursorList(Btree *p){
  2500   2516     BtCursor *pCur;
  2501   2517     BtShared *pBt = p->pBt;
  2502   2518     for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
................................................................................
  2607   2623   ** is active this routine is a no-op.
  2608   2624   **
  2609   2625   ** All cursors will be invalidated by this operation.  Any attempt
  2610   2626   ** to use a cursor that was open at the beginning of this operation
  2611   2627   ** will result in an error.
  2612   2628   */
  2613   2629   int sqlite3BtreeRollbackStmt(Btree *p){
  2614         -  int rc;
         2630  +  int rc = SQLITE_OK;
  2615   2631     BtShared *pBt = p->pBt;
  2616         -  if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK;
  2617         -  rc = sqlite3pager_stmt_rollback(pBt->pPager);
  2618         -  assert( countWriteCursors(pBt)==0 );
  2619         -  pBt->inStmt = 0;
         2632  +  sqlite3MallocDisallow();
         2633  +  if( pBt->inStmt && !pBt->readOnly ){
         2634  +    rc = sqlite3pager_stmt_rollback(pBt->pPager);
         2635  +    assert( countWriteCursors(pBt)==0 );
         2636  +    pBt->inStmt = 0;
         2637  +  }
         2638  +  sqlite3MallocAllow();
  2620   2639     return rc;
  2621   2640   }
  2622   2641   
  2623   2642   /*
  2624   2643   ** Default key comparison function to be used if no comparison function
  2625   2644   ** is specified on the sqlite3BtreeCursor() call.
  2626   2645   */
................................................................................
  3164   3183   */
  3165   3184   static int moveToRoot(BtCursor *pCur){
  3166   3185     MemPage *pRoot;
  3167   3186     int rc = SQLITE_OK;
  3168   3187     BtShared *pBt = pCur->pBtree->pBt;
  3169   3188   
  3170   3189     restoreOrClearCursorPosition(pCur, 0);
  3171         -  assert( pCur->pPage );
  3172   3190     pRoot = pCur->pPage;
  3173         -  if( pRoot->pgno==pCur->pgnoRoot ){
         3191  +  if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
  3174   3192       assert( pRoot->isInit );
  3175   3193     }else{
  3176   3194       if( 
  3177   3195         SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
  3178   3196       ){
  3179   3197         pCur->eState = CURSOR_INVALID;
  3180   3198         return rc;
................................................................................
  3343   3361         if( pPage->intKey ){
  3344   3362           u8 *pCell;
  3345   3363           if( tryRightmost ){
  3346   3364             pCur->idx = upr;
  3347   3365           }
  3348   3366           pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
  3349   3367           if( pPage->hasData ){
  3350         -          int dummy;
         3368  +          u32 dummy;
  3351   3369             pCell += getVarint32(pCell, &dummy);
  3352   3370           }
  3353         -        getVarint(pCell, &nCellKey);
         3371  +        getVarint(pCell, (u64 *)&nCellKey);
  3354   3372           if( nCellKey<nKey ){
  3355   3373             c = -1;
  3356   3374           }else if( nCellKey>nKey ){
  3357   3375             c = +1;
  3358   3376             tryRightmost = 0;
  3359   3377           }else{
  3360   3378             c = 0;
................................................................................
  3433   3451   ** Advance the cursor to the next entry in the database.  If
  3434   3452   ** successful then set *pRes=0.  If the cursor
  3435   3453   ** was already pointing to the last entry in the database before
  3436   3454   ** this routine was called, then set *pRes=1.
  3437   3455   */
  3438   3456   int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  3439   3457     int rc;
  3440         -  MemPage *pPage = pCur->pPage;
         3458  +  MemPage *pPage;
  3441   3459   
  3442   3460   #ifndef SQLITE_OMIT_SHARED_CACHE
  3443   3461     rc = restoreOrClearCursorPosition(pCur, 1);
  3444   3462     if( rc!=SQLITE_OK ){
  3445   3463       return rc;
  3446   3464     }
  3447   3465     if( pCur->skip>0 ){
  3448   3466       pCur->skip = 0;
  3449   3467       *pRes = 0;
  3450   3468       return SQLITE_OK;
  3451   3469     }
  3452   3470     pCur->skip = 0;
  3453         -#endif
         3471  +#endif 
  3454   3472   
  3455   3473     assert( pRes!=0 );
         3474  +  pPage = pCur->pPage;
  3456   3475     if( CURSOR_INVALID==pCur->eState ){
  3457   3476       *pRes = 1;
  3458   3477       return SQLITE_OK;
  3459   3478     }
  3460   3479     assert( pPage->isInit );
  3461   3480     assert( pCur->idx<pPage->nCell );
  3462   3481   
................................................................................
  4423   4442     /* 
  4424   4443     ** Find the parent page.
  4425   4444     */
  4426   4445     assert( pPage->isInit );
  4427   4446     assert( sqlite3pager_iswriteable(pPage->aData) );
  4428   4447     pBt = pPage->pBt;
  4429   4448     pParent = pPage->pParent;
  4430         -  sqlite3pager_write(pParent->aData);
  4431   4449     assert( pParent );
         4450  +  if( SQLITE_OK!=(rc = sqlite3pager_write(pParent->aData)) ){
         4451  +    return rc;
         4452  +  }
  4432   4453     TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
  4433   4454   
  4434   4455   #ifndef SQLITE_OMIT_QUICKBALANCE
  4435   4456     /*
  4436   4457     ** A special case:  If a new entry has just been inserted into a
  4437   4458     ** table (that is, a btree with integer keys and all data at the leaves)
  4438   4459     ** and the new entry is the right-most entry in the tree (it has the
................................................................................
  5260   5281   
  5261   5282     /* Restore the current cursor position (a no-op if the cursor is not in 
  5262   5283     ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors 
  5263   5284     ** open on the same table. Then call sqlite3pager_write() on the page
  5264   5285     ** that the entry will be deleted from.
  5265   5286     */
  5266   5287     if( 
  5267         -    (rc = restoreOrClearCursorPosition(pCur, 1)) ||
  5268         -    (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
  5269         -    (rc = sqlite3pager_write(pPage->aData))
         5288  +    (rc = restoreOrClearCursorPosition(pCur, 1))!=0 ||
         5289  +    (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
         5290  +    (rc = sqlite3pager_write(pPage->aData))!=0
  5270   5291     ){
  5271   5292       return rc;
  5272   5293     }
  5273   5294   
  5274   5295     /* Locate the cell within it's page and leave pCell pointing to the
  5275   5296     ** data. The clearCell() call frees any overflow pages associated with the
  5276   5297     ** cell. The cell itself is still intact.
................................................................................
  5288   5309       ** do something we will leave a hole on an internal page.
  5289   5310       ** We have to fill the hole by moving in a cell from a leaf.  The
  5290   5311       ** next Cell after the one to be deleted is guaranteed to exist and
  5291   5312       ** to be a leaf so we can use it.
  5292   5313       */
  5293   5314       BtCursor leafCur;
  5294   5315       unsigned char *pNext;
  5295         -    int szNext;
         5316  +    int szNext;  /* The compiler warning is wrong: szNext is always 
         5317  +                 ** initialized before use.  Adding an extra initialization
         5318  +                 ** to silence the compiler slows down the code. */
  5296   5319       int notUsed;
  5297   5320       unsigned char *tempCell = 0;
  5298   5321       assert( !pPage->leafData );
  5299   5322       getTempCursor(pCur, &leafCur);
  5300   5323       rc = sqlite3BtreeNext(&leafCur, &notUsed);
  5301   5324       if( rc!=SQLITE_OK ){
  5302   5325         if( rc!=SQLITE_NOMEM ){
................................................................................
  5527   5550   ** read cursors on the table.  Open write cursors are moved to the
  5528   5551   ** root of the table.
  5529   5552   */
  5530   5553   int sqlite3BtreeClearTable(Btree *p, int iTable){
  5531   5554     int rc;
  5532   5555     BtCursor *pCur;
  5533   5556     BtShared *pBt = p->pBt;
         5557  +  sqlite3 *db = p->pSqlite;
  5534   5558     if( p->inTrans!=TRANS_WRITE ){
  5535   5559       return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  5536   5560     }
  5537         -  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
  5538         -    if( pCur->pgnoRoot==(Pgno)iTable ){
  5539         -      if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
  5540         -      moveToRoot(pCur);
         5561  +
         5562  +  /* If this connection is not in read-uncommitted mode and currently has
         5563  +  ** a read-cursor open on the table being cleared, return SQLITE_LOCKED.
         5564  +  */
         5565  +  if( 0==db || 0==(db->flags&SQLITE_ReadUncommitted) ){
         5566  +    for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
         5567  +      if( pCur->pBtree==p && pCur->pgnoRoot==(Pgno)iTable ){
         5568  +        if( 0==pCur->wrFlag ){
         5569  +          return SQLITE_LOCKED;
         5570  +        }
         5571  +        moveToRoot(pCur);
         5572  +      }
  5541   5573       }
  5542   5574     }
  5543         -  rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
  5544         -#if 0
  5545         -  if( rc ){
  5546         -    sqlite3BtreeRollback(pBt);
         5575  +
         5576  +  /* Save the position of all cursors open on this table */
         5577  +  if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
         5578  +    return rc;
  5547   5579     }
  5548         -#endif
  5549         -  return rc;
         5580  +
         5581  +  return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
  5550   5582   }
  5551   5583   
  5552   5584   /*
  5553   5585   ** Erase all information in a table and add the root of the table to
  5554   5586   ** the freelist.  Except, the root of the principle table (the one on
  5555   5587   ** page 1) is never added to the freelist.
  5556   5588   **
................................................................................
  5854   5886     return SQLITE_OK;
  5855   5887   }
  5856   5888   int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){
  5857   5889     return btreePageDump(p->pBt, pgno, recursive, 0);
  5858   5890   }
  5859   5891   #endif
  5860   5892   
  5861         -#ifdef SQLITE_TEST
         5893  +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  5862   5894   /*
  5863   5895   ** Fill aResult[] with information about the entry and page that the
  5864   5896   ** cursor is pointing to.
  5865   5897   ** 
  5866   5898   **   aResult[0] =  The page number
  5867   5899   **   aResult[1] =  The entry number
  5868   5900   **   aResult[2] =  Total number of entries on this page
................................................................................
  6527   6559   }
  6528   6560   
  6529   6561   /*
  6530   6562   ** The following debugging interface has to be in this file (rather
  6531   6563   ** than in, for example, test1.c) so that it can get access to
  6532   6564   ** the definition of BtShared.
  6533   6565   */
  6534         -#if defined(SQLITE_TEST) && defined(TCLSH)
         6566  +#if defined(SQLITE_DEBUG) && defined(TCLSH)
  6535   6567   #include <tcl.h>
  6536   6568   int sqlite3_shared_cache_report(
  6537   6569     void * clientData,
  6538   6570     Tcl_Interp *interp,
  6539   6571     int objc,
  6540   6572     Tcl_Obj *CONST objv[]
  6541   6573   ){

Changes to SQLite.Interop/src/btree.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite B-Tree file
    13     13   ** subsystem.  See comments in the source code for a detailed description
    14     14   ** of what each interface routine does.
    15     15   **
    16         -** @(#) $Id: btree.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           16  +** @(#) $Id: btree.h,v 1.17 2006/01/23 19:45:55 rmsimpson Exp $
    17     17   */
    18     18   #ifndef _BTREE_H_
    19     19   #define _BTREE_H_
    20     20   
    21     21   /* TODO: This definition is just included so other modules compile. It
    22     22   ** needs to be revisited.
    23     23   */

Changes to SQLite.Interop/src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           25  +** $Id: build.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   #include <ctype.h>
    29     29   
    30     30   /*
    31     31   ** This routine is called when a new SQL statement is beginning to
    32     32   ** be parsed.  Initialize the pParse structure as needed.
................................................................................
   126    126   ** Note that if an error occurred, it might be the case that
   127    127   ** no VDBE code was generated.
   128    128   */
   129    129   void sqlite3FinishCoding(Parse *pParse){
   130    130     sqlite3 *db;
   131    131     Vdbe *v;
   132    132   
   133         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return;
          133  +  if( sqlite3MallocFailed() ) return;
   134    134     if( pParse->nested ) return;
   135    135     if( !pParse->pVdbe ){
   136    136       if( pParse->rc==SQLITE_OK && pParse->nErr ){
   137    137         pParse->rc = SQLITE_ERROR;
   138    138       }
   139    139       return;
   140    140     }
................................................................................
  1360   1360     Token *pEnd,            /* The final ')' token in the CREATE TABLE */
  1361   1361     Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
  1362   1362   ){
  1363   1363     Table *p;
  1364   1364     sqlite3 *db = pParse->db;
  1365   1365     int iDb;
  1366   1366   
  1367         -  if( (pEnd==0 && pSelect==0) || 
  1368         -      pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) {
         1367  +  if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3MallocFailed() ) {
  1369   1368       return;
  1370   1369     }
  1371   1370     p = pParse->pNewTable;
  1372   1371     if( p==0 ) return;
  1373   1372   
  1374   1373     assert( !db->init.busy || !pSelect );
  1375   1374   
................................................................................
  1543   1542   #endif
  1544   1543       pParse->pNewTable = 0;
  1545   1544       db->nTable++;
  1546   1545       db->flags |= SQLITE_InternChanges;
  1547   1546   
  1548   1547   #ifndef SQLITE_OMIT_ALTERTABLE
  1549   1548       if( !p->pSelect ){
  1550         -      const unsigned char *zName = pParse->sNameToken.z;
         1549  +      const char *zName = (const char *)pParse->sNameToken.z;
         1550  +      int nName;
  1551   1551         assert( !pSelect && pCons && pEnd );
  1552         -      if( pCons->z==0 ) pCons = pEnd;
  1553         -      p->addColOffset = 13 + sqlite3utf8CharLen(zName, pCons->z - zName);
         1552  +      if( pCons->z==0 ){
         1553  +        pCons = pEnd;
         1554  +      }
         1555  +      nName = (const char *)pCons->z - zName;
         1556  +      p->addColOffset = 13 + sqlite3utf8CharLen(zName, nName);
  1554   1557       }
  1555   1558   #endif
  1556   1559     }
  1557   1560   }
  1558   1561   
  1559   1562   #ifndef SQLITE_OMIT_VIEW
  1560   1563   /*
................................................................................
  1599   1602     /* Make a copy of the entire SELECT statement that defines the view.
  1600   1603     ** This will force all the Expr.token.z values to be dynamically
  1601   1604     ** allocated rather than point to the input string - which means that
  1602   1605     ** they will persist after the current sqlite3_exec() call returns.
  1603   1606     */
  1604   1607     p->pSelect = sqlite3SelectDup(pSelect);
  1605   1608     sqlite3SelectDelete(pSelect);
  1606         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1609  +  if( sqlite3MallocFailed() ){
  1607   1610       return;
  1608   1611     }
  1609   1612     if( !pParse->db->init.busy ){
  1610   1613       sqlite3ViewGetColumnNames(pParse, p);
  1611   1614     }
  1612   1615   
  1613   1616     /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
................................................................................
  1841   1844   */
  1842   1845   void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
  1843   1846     Table *pTab;
  1844   1847     Vdbe *v;
  1845   1848     sqlite3 *db = pParse->db;
  1846   1849     int iDb;
  1847   1850   
  1848         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
         1851  +  if( pParse->nErr || sqlite3MallocFailed() ){
  1849   1852       goto exit_drop_table;
  1850   1853     }
  1851   1854     assert( pName->nSrc==1 );
  1852   1855     pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
  1853   1856   
  1854   1857     if( pTab==0 ){
  1855   1858       if( noErr ){
................................................................................
  2202   2205     int iDb;             /* Index of the database that is being written */
  2203   2206     Token *pName = 0;    /* Unqualified name of the index to create */
  2204   2207     struct ExprList_item *pListItem; /* For looping over pList */
  2205   2208     int nCol;
  2206   2209     int nExtra = 0;
  2207   2210     char *zExtra;
  2208   2211   
  2209         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
         2212  +  if( pParse->nErr || sqlite3MallocFailed() ){
  2210   2213       goto exit_create_index;
  2211   2214     }
  2212   2215   
  2213   2216     /*
  2214   2217     ** Find the table that is to be indexed.  Return early if not found.
  2215   2218     */
  2216   2219     if( pTblName!=0 ){
................................................................................
  2356   2359         sizeof(int)*nCol +           /* Index.aiColumn   */
  2357   2360         sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
  2358   2361         sizeof(char *)*nCol +        /* Index.azColl     */
  2359   2362         sizeof(u8)*nCol +            /* Index.aSortOrder */
  2360   2363         nName + 1 +                  /* Index.zName      */
  2361   2364         nExtra                       /* Collation sequence names */
  2362   2365     );
  2363         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_create_index;
         2366  +  if( sqlite3MallocFailed() ) goto exit_create_index;
  2364   2367     pIndex->aiColumn = (int *)(&pIndex[1]);
  2365         -  pIndex->aiRowEst = (int *)(&pIndex->aiColumn[nCol]);
         2368  +  pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
  2366   2369     pIndex->azColl = (char **)(&pIndex->aiRowEst[nCol+1]);
  2367   2370     pIndex->aSortOrder = (u8 *)(&pIndex->azColl[nCol]);
  2368   2371     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
  2369   2372     zExtra = (char *)(&pIndex->zName[nName+1]);
  2370   2373     strcpy(pIndex->zName, zName);
  2371   2374     pIndex->pTable = pTab;
  2372   2375     pIndex->nColumn = pList->nExpr;
................................................................................
  2643   2646   */
  2644   2647   void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
  2645   2648     Index *pIndex;
  2646   2649     Vdbe *v;
  2647   2650     sqlite3 *db = pParse->db;
  2648   2651     int iDb;
  2649   2652   
  2650         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
         2653  +  if( pParse->nErr || sqlite3MallocFailed() ){
  2651   2654       goto exit_drop_index;
  2652   2655     }
  2653   2656     assert( pName->nSrc==1 );
  2654   2657     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  2655   2658       goto exit_drop_index;
  2656   2659     }
  2657   2660     pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
................................................................................
  2852   2855   
  2853   2856   /*
  2854   2857   ** Assign cursors to all tables in a SrcList
  2855   2858   */
  2856   2859   void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
  2857   2860     int i;
  2858   2861     struct SrcList_item *pItem;
  2859         -  assert(pList || sqlite3ThreadDataReadOnly()->mallocFailed);
         2862  +  assert(pList || sqlite3MallocFailed() );
  2860   2863     if( pList ){
  2861   2864       for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
  2862   2865         if( pItem->iCursor>=0 ) break;
  2863   2866         pItem->iCursor = pParse->nTab++;
  2864   2867         if( pItem->pSelect ){
  2865   2868           sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
  2866   2869         }
................................................................................
  2901   2904   */
  2902   2905   void sqlite3BeginTransaction(Parse *pParse, int type){
  2903   2906     sqlite3 *db;
  2904   2907     Vdbe *v;
  2905   2908     int i;
  2906   2909   
  2907   2910     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2908         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
         2911  +  if( pParse->nErr || sqlite3MallocFailed() ) return;
  2909   2912     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
  2910   2913   
  2911   2914     v = sqlite3GetVdbe(pParse);
  2912   2915     if( !v ) return;
  2913   2916     if( type!=TK_DEFERRED ){
  2914   2917       for(i=0; i<db->nDb; i++){
  2915   2918         sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
................................................................................
  2922   2925   ** Commit a transaction
  2923   2926   */
  2924   2927   void sqlite3CommitTransaction(Parse *pParse){
  2925   2928     sqlite3 *db;
  2926   2929     Vdbe *v;
  2927   2930   
  2928   2931     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2929         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
         2932  +  if( pParse->nErr || sqlite3MallocFailed() ) return;
  2930   2933     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
  2931   2934   
  2932   2935     v = sqlite3GetVdbe(pParse);
  2933   2936     if( v ){
  2934   2937       sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
  2935   2938     }
  2936   2939   }
................................................................................
  2939   2942   ** Rollback a transaction
  2940   2943   */
  2941   2944   void sqlite3RollbackTransaction(Parse *pParse){
  2942   2945     sqlite3 *db;
  2943   2946     Vdbe *v;
  2944   2947   
  2945   2948     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2946         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
         2949  +  if( pParse->nErr || sqlite3MallocFailed() ) return;
  2947   2950     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
  2948   2951   
  2949   2952     v = sqlite3GetVdbe(pParse);
  2950   2953     if( v ){
  2951   2954       sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
  2952   2955     }
  2953   2956   }
................................................................................
  3150   3153     if( pName1==0 || pName1->z==0 ){
  3151   3154       reindexDatabases(pParse, 0);
  3152   3155       return;
  3153   3156     }else if( pName2==0 || pName2->z==0 ){
  3154   3157       assert( pName1->z );
  3155   3158       pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
  3156   3159       if( pColl ){
  3157         -      char *z = sqliteStrNDup(pName1->z, pName1->n);
         3160  +      char *z = sqliteStrNDup((const char *)pName1->z, pName1->n);
  3158   3161         if( z ){
  3159   3162           reindexDatabases(pParse, z);
  3160   3163           sqliteFree(z);
  3161   3164         }
  3162   3165         return;
  3163   3166       }
  3164   3167     }

Changes to SQLite.Interop/src/callback.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains functions used to access the internal hash tables
    14     14   ** of user defined functions and collation sequences.
    15     15   **
    16         -** $Id: callback.c,v 1.11 2006/01/16 15:51:47 rmsimpson Exp $
           16  +** $Id: callback.c,v 1.12 2006/01/23 19:45:55 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** Invoke the 'collation needed' callback to request a collation sequence
    23     23   ** in the database text encoding of name zName, length nName.
................................................................................
   175    175         pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
   176    176   
   177    177         /* If a malloc() failure occured in sqlite3HashInsert(), it will 
   178    178         ** return the pColl pointer to be deleted (because it wasn't added
   179    179         ** to the hash table).
   180    180         */
   181    181         assert( !pDel ||
   182         -              (sqlite3ThreadDataReadOnly()->mallocFailed && pDel==pColl) );
          182  +              (sqlite3MallocFailed() && pDel==pColl) );
   183    183         sqliteFree(pDel);
   184    184       }
   185    185     }
   186    186     return pColl;
   187    187   }
   188    188   
   189    189   /*

Changes to SQLite.Interop/src/complete.c.

    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that implements the sqlite3_complete() API.
    15     15   ** This code used to be part of the tokenizer.c source file.  But by
    16     16   ** separating it out, the code will be automatically omitted from
    17     17   ** static links that do not use it.
    18     18   **
    19         -** $Id: complete.c,v 1.9 2006/01/16 15:51:47 rmsimpson Exp $
           19  +** $Id: complete.c,v 1.10 2006/01/23 19:45:55 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #ifndef SQLITE_OMIT_COMPLETE
    23     23   
    24     24   /*
    25     25   ** This is defined in tokenize.c.  We just have to import the definition.
    26     26   */
................................................................................
   251    251     int rc = 0;
   252    252   
   253    253     pVal = sqlite3ValueNew();
   254    254     sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   255    255     zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
   256    256     if( zSql8 ){
   257    257       rc = sqlite3_complete(zSql8);
   258         -  }else if( zSql ){
   259         -    rc = SQLITE_NOMEM;
   260         -    sqlite3MallocClearFailed();
   261    258     }
   262    259     sqlite3ValueFree(pVal);
   263         -  return rc;
          260  +  return sqlite3ApiExit(0, rc);
   264    261   }
   265    262   #endif /* SQLITE_OMIT_UTF16 */
   266    263   #endif /* SQLITE_OMIT_COMPLETE */

Changes to SQLite.Interop/src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           19  +** $Id: date.c,v 1.17 2006/01/23 19:45:55 rmsimpson Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system.
................................................................................
   972    972       { "current_time",       0, ctimeFunc      },
   973    973       { "current_timestamp",  0, ctimestampFunc },
   974    974       { "current_date",       0, cdateFunc      },
   975    975     };
   976    976     int i;
   977    977   
   978    978     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   979         -    sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
          979  +    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
   980    980           SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
   981    981     }
   982    982   #else
   983    983     static const struct {
   984    984        char *zName;
   985    985        char *zFormat;
   986    986     } aFuncs[] = {
................................................................................
   987    987       { "current_time", "%H:%M:%S" },
   988    988       { "current_date", "%Y-%m-%d" },
   989    989       { "current_timestamp", "%Y-%m-%d %H:%M:%S" }
   990    990     };
   991    991     int i;
   992    992   
   993    993     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   994         -    sqlite3_create_function(db, aFuncs[i].zName, 0, SQLITE_UTF8, 
          994  +    sqlite3CreateFunc(db, aFuncs[i].zName, 0, SQLITE_UTF8, 
   995    995           aFuncs[i].zFormat, currentTimeFunc, 0, 0);
   996    996     }
   997    997   #endif
   998    998   }

Changes to SQLite.Interop/src/delete.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: delete.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.
................................................................................
   104    104   
   105    105   #ifndef SQLITE_OMIT_TRIGGER
   106    106     int isView;                  /* True if attempting to delete from a view */
   107    107     int triggers_exist = 0;      /* True if any triggers exist */
   108    108   #endif
   109    109   
   110    110     sContext.pParse = 0;
   111         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          111  +  if( pParse->nErr || sqlite3MallocFailed() ){
   112    112       goto delete_from_cleanup;
   113    113     }
   114    114     db = pParse->db;
   115    115     assert( pTabList->nSrc==1 );
   116    116   
   117    117     /* Locate the table which we want to delete.  This table has to be
   118    118     ** put in an SrcList structure because some of the subroutines we

Changes to SQLite.Interop/src/experimental.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are not a part of the official
    13     13   ** SQLite API.  These routines are unsupported.
    14     14   **
    15         -** $Id: experimental.c,v 1.5 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: experimental.c,v 1.6 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set all the parameters in the compiled SQL statement to NULL.
    21     21   */
    22     22   int sqlite3_clear_bindings(sqlite3_stmt *pStmt){

Changes to SQLite.Interop/src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.22 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: expr.c,v 1.23 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
   259    259   /*
   260    260   ** Set the Expr.span field of the given expression to span all
   261    261   ** text between the two given tokens.
   262    262   */
   263    263   void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
   264    264     assert( pRight!=0 );
   265    265     assert( pLeft!=0 );
   266         -  if( !sqlite3ThreadDataReadOnly()->mallocFailed && pRight->z && pLeft->z ){
          266  +  if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
   267    267       assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
   268    268       if( pLeft->dyn==0 && pRight->dyn==0 ){
   269    269         pExpr->span.z = pLeft->z;
   270    270         pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
   271    271       }else{
   272    272         pExpr->span.z = 0;
   273    273       }
................................................................................
   276    276   
   277    277   /*
   278    278   ** Construct a new expression node for a function with multiple
   279    279   ** arguments.
   280    280   */
   281    281   Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
   282    282     Expr *pNew;
          283  +  assert( pToken );
   283    284     pNew = sqliteMalloc( sizeof(Expr) );
   284    285     if( pNew==0 ){
   285    286       sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
   286    287       return 0;
   287    288     }
   288    289     pNew->op = TK_FUNCTION;
   289    290     pNew->pList = pList;
   290         -  if( pToken ){
   291         -    assert( pToken->dyn==0 );
   292         -    pNew->token = *pToken;
   293         -  }else{
   294         -    pNew->token.z = 0;
   295         -  }
          291  +  assert( pToken->dyn==0 );
          292  +  pNew->token = *pToken;
   296    293     pNew->span = pNew->token;
   297    294     return pNew;
   298    295   }
   299    296   
   300    297   /*
   301    298   ** Assign a variable number to an expression that encodes a wildcard
   302    299   ** in the original SQL statement.  
................................................................................
   354    351       if( i>=pParse->nVarExpr ){
   355    352         pExpr->iTable = ++pParse->nVar;
   356    353         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   357    354           pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   358    355           sqliteReallocOrFree((void**)&pParse->apVarExpr,
   359    356                          pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
   360    357         }
   361         -      if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
          358  +      if( !sqlite3MallocFailed() ){
   362    359           assert( pParse->apVarExpr!=0 );
   363    360           pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
   364    361         }
   365    362       }
   366    363     } 
   367    364   }
   368    365   
................................................................................
   459    456         /* Always make a copy of the span for top-level expressions in the
   460    457         ** expression list.  The logic in SELECT processing that determines
   461    458         ** the names of columns in the result set needs this information */
   462    459         sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
   463    460       }
   464    461       assert( pNewExpr==0 || pNewExpr->span.z!=0 
   465    462               || pOldExpr->span.z==0
   466         -            || sqlite3ThreadDataReadOnly()->mallocFailed );
          463  +            || sqlite3MallocFailed() );
   467    464       pItem->zName = sqliteStrDup(pOldItem->zName);
   468    465       pItem->sortOrder = pOldItem->sortOrder;
   469    466       pItem->isAgg = pOldItem->isAgg;
   470    467       pItem->done = 0;
   471    468     }
   472    469     return pNew;
   473    470   }
................................................................................
   828    825     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   829    826     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   830    827   
   831    828     assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
   832    829     zDb = sqlite3NameFromToken(pDbToken);
   833    830     zTab = sqlite3NameFromToken(pTableToken);
   834    831     zCol = sqlite3NameFromToken(pColumnToken);
   835         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
          832  +  if( sqlite3MallocFailed() ){
   836    833       goto lookupname_end;
   837    834     }
   838    835   
   839    836     pExpr->iTable = -1;
   840    837     while( pNC && cnt==0 ){
   841    838       ExprList *pEList;
   842    839       SrcList *pSrcList = pNC->pSrcList;
................................................................................
  1305   1302     ** If all of the above are false, then we can run this code just once
  1306   1303     ** save the results, and reuse the same result on subsequent invocations.
  1307   1304     */
  1308   1305     if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
  1309   1306       int mem = pParse->nMem++;
  1310   1307       sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
  1311   1308       testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
  1312         -    assert( testAddr>0 || sqlite3ThreadDataReadOnly()->mallocFailed );
         1309  +    assert( testAddr>0 || sqlite3MallocFailed() );
  1313   1310       sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
  1314   1311     }
  1315   1312   
  1316   1313     switch( pExpr->op ){
  1317   1314       case TK_IN: {
  1318   1315         char affinity;
  1319   1316         KeyInfo keyInfo;
................................................................................
  2081   2078   
  2082   2079   /*
  2083   2080   ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
  2084   2081   ** if they are identical and return FALSE if they differ in any way.
  2085   2082   */
  2086   2083   int sqlite3ExprCompare(Expr *pA, Expr *pB){
  2087   2084     int i;
  2088         -  if( pA==0 ){
  2089         -    return pB==0;
  2090         -  }else if( pB==0 ){
  2091         -    return 0;
         2085  +  if( pA==0||pB==0 ){
         2086  +    return pB==pA;
  2092   2087     }
  2093   2088     if( pA->op!=pB->op ) return 0;
  2094   2089     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
  2095   2090     if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
  2096   2091     if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
  2097   2092     if( pA->pList ){
  2098   2093       if( pB->pList==0 ) return 0;

Changes to SQLite.Interop/src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
           19  +** $Id: func.c,v 1.18 2006/01/23 19:45:55 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   /* #include <math.h> */
    24     24   #include <stdlib.h>
    25     25   #include <assert.h>
    26     26   #include "vdbeInt.h"
................................................................................
  1020   1020   
  1021   1021     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
  1022   1022       void *pArg = 0;
  1023   1023       switch( aFuncs[i].argType ){
  1024   1024         case 1: pArg = db; break;
  1025   1025         case 2: pArg = (void *)(-1); break;
  1026   1026       }
  1027         -    sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
         1027  +    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
  1028   1028           aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
  1029   1029       if( aFuncs[i].needCollSeq ){
  1030   1030         FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 
  1031   1031             strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
  1032   1032         if( pFunc && aFuncs[i].needCollSeq ){
  1033   1033           pFunc->needCollSeq = 1;
  1034   1034         }
................................................................................
  1042   1042   #endif
  1043   1043     for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
  1044   1044       void *pArg = 0;
  1045   1045       switch( aAggs[i].argType ){
  1046   1046         case 1: pArg = db; break;
  1047   1047         case 2: pArg = (void *)(-1); break;
  1048   1048       }
  1049         -    sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 
         1049  +    sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 
  1050   1050           pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
  1051   1051       if( aAggs[i].needCollSeq ){
  1052   1052         FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
  1053   1053             strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
  1054   1054         if( pFunc && aAggs[i].needCollSeq ){
  1055   1055           pFunc->needCollSeq = 1;
  1056   1056         }
................................................................................
  1086   1086   void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  1087   1087     struct compareInfo *pInfo;
  1088   1088     if( caseSensitive ){
  1089   1089       pInfo = (struct compareInfo*)&likeInfoAlt;
  1090   1090     }else{
  1091   1091       pInfo = (struct compareInfo*)&likeInfoNorm;
  1092   1092     }
  1093         -  sqlite3_create_function(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
  1094         -  sqlite3_create_function(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
  1095         -  sqlite3_create_function(db, "glob", 2, SQLITE_UTF8, 
         1093  +  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
         1094  +  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
         1095  +  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
  1096   1096         (struct compareInfo*)&globInfo, likeFunc, 0,0);
  1097   1097     setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  1098   1098     setLikeOptFlag(db, "like", 
  1099   1099         caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
  1100   1100   }
  1101   1101   
  1102   1102   /*

Changes to SQLite.Interop/src/hash.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the implementation of generic hash-tables
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: hash.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <assert.h>
    19     19   
    20     20   /* Turn bulk memory into a hash table object by initializing the
    21     21   ** fields of the Hash structure.
    22     22   **

Changes to SQLite.Interop/src/hash.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the header file for the generic hash-table implemenation
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: hash.h,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE_HASH_H_
    18     18   #define _SQLITE_HASH_H_
    19     19   
    20     20   /* Forward declarations of structures. */
    21     21   typedef struct Hash Hash;
    22     22   typedef struct HashElem HashElem;

Changes to SQLite.Interop/src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: insert.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P3 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:
................................................................................
   218    218   
   219    219   #ifndef SQLITE_OMIT_TRIGGER
   220    220     int isView;                 /* True if attempting to insert into a view */
   221    221     int triggers_exist = 0;     /* True if there are FOR EACH ROW triggers */
   222    222   #endif
   223    223   
   224    224   #ifndef SQLITE_OMIT_AUTOINCREMENT
   225         -  int counterRowid;     /* Memory cell holding rowid of autoinc counter */
          225  +  int counterRowid = 0;  /* Memory cell holding rowid of autoinc counter */
   226    226   #endif
   227    227   
   228         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          228  +  if( pParse->nErr || sqlite3MallocFailed() ){
   229    229       goto insert_cleanup;
   230    230     }
   231    231     db = pParse->db;
   232    232   
   233    233     /* Locate the table into which we will be inserting new information.
   234    234     */
   235    235     assert( pTabList->nSrc==1 );
................................................................................
   329    329       int rc, iInitCode;
   330    330       iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
   331    331       iSelectLoop = sqlite3VdbeCurrentAddr(v);
   332    332       iInsertBlock = sqlite3VdbeMakeLabel(v);
   333    333   
   334    334       /* Resolve the expressions in the SELECT statement and execute it. */
   335    335       rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
   336         -    if( rc || pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          336  +    if( rc || pParse->nErr || sqlite3MallocFailed() ){
   337    337         goto insert_cleanup;
   338    338       }
   339    339   
   340    340       iCleanup = sqlite3VdbeMakeLabel(v);
   341    341       sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
   342    342       assert( pSelect->pEList );
   343    343       nColumn = pSelect->pEList->nExpr;

Changes to SQLite.Interop/src/legacy.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: legacy.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           17  +** $Id: legacy.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    18     18   */
    19     19   
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
    64     64         continue;
    65     65       }
    66     66   
    67     67       db->nChange += nChange;
    68     68       nCallback = 0;
    69     69   
    70     70       nCol = sqlite3_column_count(pStmt);
    71         -    azCols = sqliteMalloc(2*nCol*sizeof(const char *));
    72         -    if( nCol && !azCols ){
           71  +    azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
           72  +    if( azCols==0 ){
    73     73         goto exec_out;
    74     74       }
    75     75   
    76     76       while( 1 ){
    77     77         int i;
    78     78         rc = sqlite3_step(pStmt);
    79     79   
................................................................................
   117    117       azCols = 0;
   118    118     }
   119    119   
   120    120   exec_out:
   121    121     if( pStmt ) sqlite3_finalize(pStmt);
   122    122     if( azCols ) sqliteFree(azCols);
   123    123   
   124         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   125         -    rc = SQLITE_NOMEM;
   126         -    sqlite3MallocClearFailed();
   127         -  }
   128         -
          124  +  rc = sqlite3ApiExit(0, rc);
   129    125     if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
   130    126       *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
   131    127       if( *pzErrMsg ){
   132    128         strcpy(*pzErrMsg, sqlite3_errmsg(db));
   133    129       }
   134    130     }else if( pzErrMsg ){
   135    131       *pzErrMsg = 0;
   136    132     }
   137    133   
   138    134     return rc;
   139    135   }

Changes to SQLite.Interop/src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           17  +** $Id: main.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** The following constant value is used by the SQLITE_BIGENDIAN and
................................................................................
   382    382   ** directly.
   383    383   **
   384    384   ** Note that we need to call free() not sqliteFree() here.
   385    385   */
   386    386   void sqlite3_free(char *p){ free(p); }
   387    387   
   388    388   /*
   389         -** Create new user functions.
          389  +** This function is exactly the same as sqlite3_create_function(), except
          390  +** that it is designed to be called by internal code. The difference is
          391  +** that if a malloc() fails in sqlite3_create_function(), an error code
          392  +** is returned and the mallocFailed flag cleared. 
   390    393   */
   391         -int sqlite3_create_function(
          394  +int sqlite3CreateFunc(
   392    395     sqlite3 *db,
   393    396     const char *zFunctionName,
   394    397     int nArg,
   395    398     int enc,
   396    399     void *pUserData,
   397    400     void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
   398    401     void (*xStep)(sqlite3_context*,int,sqlite3_value **),
................................................................................
   421    424     ** If SQLITE_ANY is specified, add three versions of the function
   422    425     ** to the hash table.
   423    426     */
   424    427     if( enc==SQLITE_UTF16 ){
   425    428       enc = SQLITE_UTF16NATIVE;
   426    429     }else if( enc==SQLITE_ANY ){
   427    430       int rc;
   428         -    rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
          431  +    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
   429    432            pUserData, xFunc, xStep, xFinal);
   430    433       if( rc!=SQLITE_OK ) return rc;
   431         -    rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
          434  +    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
   432    435           pUserData, xFunc, xStep, xFinal);
   433    436       if( rc!=SQLITE_OK ) return rc;
   434    437       enc = SQLITE_UTF16BE;
   435    438     }
   436    439   #else
   437    440     enc = SQLITE_UTF8;
   438    441   #endif
................................................................................
   443    446     ** operation to continue but invalidate all precompiled statements.
   444    447     */
   445    448     p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
   446    449     if( p && p->iPrefEnc==enc && p->nArg==nArg ){
   447    450       if( db->activeVdbeCnt ){
   448    451         sqlite3Error(db, SQLITE_BUSY, 
   449    452           "Unable to delete/modify user-function due to active statements");
          453  +      assert( !sqlite3MallocFailed() );
   450    454         return SQLITE_BUSY;
   451    455       }else{
   452    456         sqlite3ExpirePreparedStatements(db);
   453    457       }
   454    458     }
   455    459   
   456    460     p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
   457         -  if( p==0 ) return SQLITE_NOMEM;
   458         -  p->flags = 0;
   459         -  p->xFunc = xFunc;
   460         -  p->xStep = xStep;
   461         -  p->xFinalize = xFinal;
   462         -  p->pUserData = pUserData;
          461  +  if( p ){
          462  +    p->flags = 0;
          463  +    p->xFunc = xFunc;
          464  +    p->xStep = xStep;
          465  +    p->xFinalize = xFinal;
          466  +    p->pUserData = pUserData;
          467  +  }
   463    468     return SQLITE_OK;
   464    469   }
          470  +
          471  +/*
          472  +** Create new user functions.
          473  +*/
          474  +int sqlite3_create_function(
          475  +  sqlite3 *db,
          476  +  const char *zFunctionName,
          477  +  int nArg,
          478  +  int enc,
          479  +  void *p,
          480  +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
          481  +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
          482  +  void (*xFinal)(sqlite3_context*)
          483  +){
          484  +  int rc;
          485  +  assert( !sqlite3MallocFailed() );
          486  +  rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
          487  +
          488  +  return sqlite3ApiExit(db, rc);
          489  +}
          490  +
   465    491   #ifndef SQLITE_OMIT_UTF16
   466    492   int sqlite3_create_function16(
   467    493     sqlite3 *db,
   468    494     const void *zFunctionName,
   469    495     int nArg,
   470    496     int eTextRep,
   471         -  void *pUserData,
          497  +  void *p,
   472    498     void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   473    499     void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   474    500     void (*xFinal)(sqlite3_context*)
   475    501   ){
   476    502     int rc;
   477    503     char *zFunc8;
          504  +  assert( !sqlite3MallocFailed() );
   478    505   
   479         -  if( sqlite3SafetyCheck(db) ){
   480         -    return SQLITE_MISUSE;
   481         -  }
   482    506     zFunc8 = sqlite3utf16to8(zFunctionName, -1);
   483         -  if( !zFunc8 ){
   484         -    return SQLITE_NOMEM;
   485         -  }
   486         -  rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep, 
   487         -      pUserData, xFunc, xStep, xFinal);
          507  +  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
   488    508     sqliteFree(zFunc8);
   489         -  return rc;
          509  +
          510  +  return sqlite3ApiExit(db, rc);
   490    511   }
   491    512   #endif
   492    513   
   493    514   #ifndef SQLITE_OMIT_TRACE
   494    515   /*
   495    516   ** Register a trace function.  The pArg from the previously registered trace
   496    517   ** is returned.  
................................................................................
   641    662   
   642    663   /*
   643    664   ** Return UTF-8 encoded English language explanation of the most recent
   644    665   ** error.
   645    666   */
   646    667   const char *sqlite3_errmsg(sqlite3 *db){
   647    668     const char *z;
   648         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
          669  +  if( !db || sqlite3MallocFailed() ){
   649    670       return sqlite3ErrStr(SQLITE_NOMEM);
   650    671     }
   651    672     if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   652    673       return sqlite3ErrStr(SQLITE_MISUSE);
   653    674     }
   654    675     z = (char*)sqlite3_value_text(db->pErr);
   655    676     if( z==0 ){
................................................................................
   680    701       0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', 
   681    702       0, 'o', 0, 'u', 0, 't', 0, ' ', 
   682    703       0, 'o', 0, 'f', 0, ' ', 
   683    704       0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
   684    705     };
   685    706   
   686    707     const void *z;
   687         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
          708  +  if( sqlite3MallocFailed() ){
   688    709       return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   689    710     }
   690    711     if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   691    712       return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   692    713     }
   693    714     z = sqlite3_value_text16(db->pErr);
   694    715     if( z==0 ){
   695    716       sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
   696    717            SQLITE_UTF8, SQLITE_STATIC);
   697    718       z = sqlite3_value_text16(db->pErr);
   698    719     }
          720  +  sqlite3ApiExit(0, 0);
   699    721     return z;
   700    722   }
   701    723   #endif /* SQLITE_OMIT_UTF16 */
   702    724   
   703    725   /*
   704    726   ** Return the most recent error code generated by an SQLite routine. If NULL is
   705    727   ** passed to this function, we assume a malloc() failed during sqlite3_open().
   706    728   */
   707    729   int sqlite3_errcode(sqlite3 *db){
   708         -  if( !db || sqlite3ThreadDataReadOnly()->mallocFailed ){
          730  +  if( !db || sqlite3MallocFailed() ){
   709    731       return SQLITE_NOMEM;
   710    732     }
   711    733     if( sqlite3SafetyCheck(db) ){
   712    734       return SQLITE_MISUSE;
   713    735     }
   714    736     return db->errCode;
   715    737   }
          738  +
          739  +static int createCollation(
          740  +  sqlite3* db, 
          741  +  const char *zName, 
          742  +  int enc, 
          743  +  void* pCtx,
          744  +  int(*xCompare)(void*,int,const void*,int,const void*)
          745  +){
          746  +  CollSeq *pColl;
          747  +  
          748  +  if( sqlite3SafetyCheck(db) ){
          749  +    return SQLITE_MISUSE;
          750  +  }
          751  +
          752  +  /* If SQLITE_UTF16 is specified as the encoding type, transform this
          753  +  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
          754  +  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
          755  +  */
          756  +  if( enc==SQLITE_UTF16 ){
          757  +    enc = SQLITE_UTF16NATIVE;
          758  +  }
          759  +
          760  +  if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
          761  +    sqlite3Error(db, SQLITE_ERROR, 
          762  +        "Param 3 to sqlite3_create_collation() must be one of "
          763  +        "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
          764  +    );
          765  +    return SQLITE_ERROR;
          766  +  }
          767  +
          768  +  /* Check if this call is removing or replacing an existing collation 
          769  +  ** sequence. If so, and there are active VMs, return busy. If there
          770  +  ** are no active VMs, invalidate any pre-compiled statements.
          771  +  */
          772  +  pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
          773  +  if( pColl && pColl->xCmp ){
          774  +    if( db->activeVdbeCnt ){
          775  +      sqlite3Error(db, SQLITE_BUSY, 
          776  +        "Unable to delete/modify collation sequence due to active statements");
          777  +      return SQLITE_BUSY;
          778  +    }
          779  +    sqlite3ExpirePreparedStatements(db);
          780  +  }
          781  +
          782  +  pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
          783  +  if( pColl ){
          784  +    pColl->xCmp = xCompare;
          785  +    pColl->pUser = pCtx;
          786  +    pColl->enc = enc;
          787  +  }
          788  +  sqlite3Error(db, SQLITE_OK, 0);
          789  +  return SQLITE_OK;
          790  +}
          791  +
   716    792   
   717    793   /*
   718    794   ** This routine does the work of opening a database on behalf of
   719    795   ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
   720    796   ** is UTF-8 encoded.
   721    797   */
   722    798   static int openDatabase(
................................................................................
   723    799     const char *zFilename, /* Database filename UTF-8 encoded */
   724    800     sqlite3 **ppDb         /* OUT: Returned database handle */
   725    801   ){
   726    802     sqlite3 *db;
   727    803     int rc;
   728    804     CollSeq *pColl;
   729    805   
   730         -  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
          806  +  assert( !sqlite3MallocFailed() );
   731    807   
   732    808     /* Allocate the sqlite data structure */
   733    809     db = sqliteMalloc( sizeof(sqlite3) );
   734    810     if( db==0 ) goto opendb_out;
   735    811     db->priorNewRowid = 0;
   736    812     db->magic = SQLITE_MAGIC_BUSY;
   737    813     db->nDb = 2;
   738    814     db->aDb = db->aDbStatic;
   739    815     db->autoCommit = 1;
   740    816     db->flags |= SQLITE_ShortColNames;
   741    817     sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
   742    818     sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
   743    819   
   744         -#if 0
   745         -  for(i=0; i<db->nDb; i++){
   746         -    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
   747         -    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
   748         -    sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
   749         -    sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
   750         -  }
   751         -#endif
   752         - 
   753    820     /* Add the default collation sequence BINARY. BINARY works for both UTF-8
   754    821     ** and UTF-16, so add a version for each to avoid any unnecessary
   755    822     ** conversions. The only error that can occur here is a malloc() failure.
   756    823     */
   757         -  if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
   758         -      sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
          824  +  if( createCollation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
          825  +      createCollation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
   759    826         (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
   760    827     ){
   761         -    /* sqlite3_create_collation() is an external API. So the mallocFailed flag
   762         -    ** will have been cleared before returning. So set it explicitly here.
   763         -    */
   764         -    sqlite3ThreadData()->mallocFailed = 1;
          828  +    assert( sqlite3MallocFailed() );
   765    829       db->magic = SQLITE_MAGIC_CLOSED;
   766    830       goto opendb_out;
   767    831     }
   768    832   
   769    833     /* Also add a UTF-8 case-insensitive collation sequence. */
   770         -  sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
          834  +  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
   771    835   
   772    836     /* Set flags on the built-in collating sequences */
   773    837     db->pDfltColl->type = SQLITE_COLL_BINARY;
   774    838     pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
   775    839     if( pColl ){
   776    840       pColl->type = SQLITE_COLL_NOCASE;
   777    841     }
................................................................................
   802    866     db->aDb[1].safety_level = 1;
   803    867   #endif
   804    868   
   805    869     /* Register all built-in functions, but do not attempt to read the
   806    870     ** database schema yet. This is delayed until the first time the database
   807    871     ** is accessed.
   808    872     */
   809         -  sqlite3RegisterBuiltinFunctions(db);
   810         -  sqlite3Error(db, SQLITE_OK, 0);
          873  +  if( !sqlite3MallocFailed() ){
          874  +    sqlite3RegisterBuiltinFunctions(db);
          875  +    sqlite3Error(db, SQLITE_OK, 0);
          876  +  }
   811    877     db->magic = SQLITE_MAGIC_OPEN;
   812    878   
   813    879   opendb_out:
   814    880     if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
   815    881       sqlite3_close(db);
   816    882       db = 0;
   817    883     }
   818    884     *ppDb = db;
   819         -  sqlite3MallocClearFailed();
   820         -  return rc;
          885  +  return sqlite3ApiExit(0, rc);
   821    886   }
   822    887   
   823    888   /*
   824    889   ** Open a new database handle.
   825    890   */
   826    891   int sqlite3_open(
   827    892     const char *zFilename, 
................................................................................
   835    900   ** Open a new database handle.
   836    901   */
   837    902   int sqlite3_open16(
   838    903     const void *zFilename, 
   839    904     sqlite3 **ppDb
   840    905   ){
   841    906     char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
   842         -  int rc = SQLITE_NOMEM;
          907  +  int rc = SQLITE_OK;
   843    908     sqlite3_value *pVal;
   844    909   
   845    910     assert( zFilename );
   846    911     assert( ppDb );
   847    912     *ppDb = 0;
   848    913     pVal = sqlite3ValueNew();
   849    914     sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   850    915     zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
   851    916     if( zFilename8 ){
   852    917       rc = openDatabase(zFilename8, ppDb);
   853    918       if( rc==SQLITE_OK && *ppDb ){
   854    919         rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
          920  +      if( rc!=SQLITE_OK ){
          921  +        sqlite3_close(*ppDb);
          922  +        *ppDb = 0;
          923  +      }
   855    924       }
   856         -  }else{
   857         -    assert( sqlite3ThreadDataReadOnly()->mallocFailed );
   858         -    sqlite3MallocClearFailed();
   859    925     }
   860    926     sqlite3ValueFree(pVal);
   861    927   
   862         -  return rc;
          928  +  return sqlite3ApiExit(0, rc);
   863    929   }
   864    930   #endif /* SQLITE_OMIT_UTF16 */
   865    931   
   866    932   /*
   867    933   ** The following routine destroys a virtual machine that is created by
   868    934   ** the sqlite3_compile() routine. The integer returned is an SQLITE_
   869    935   ** success/failure code that describes the result of executing the virtual
................................................................................
   907    973   int sqlite3_create_collation(
   908    974     sqlite3* db, 
   909    975     const char *zName, 
   910    976     int enc, 
   911    977     void* pCtx,
   912    978     int(*xCompare)(void*,int,const void*,int,const void*)
   913    979   ){
   914         -  CollSeq *pColl;
   915         -  int rc = SQLITE_OK;
   916         -  
   917         -  if( sqlite3SafetyCheck(db) ){
   918         -    return SQLITE_MISUSE;
   919         -  }
   920         -
   921         -  /* If SQLITE_UTF16 is specified as the encoding type, transform this
   922         -  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   923         -  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   924         -  */
   925         -  if( enc==SQLITE_UTF16 ){
   926         -    enc = SQLITE_UTF16NATIVE;
   927         -  }
   928         -
   929         -  if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
   930         -    sqlite3Error(db, SQLITE_ERROR, 
   931         -        "Param 3 to sqlite3_create_collation() must be one of "
   932         -        "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
   933         -    );
   934         -    return SQLITE_ERROR;
   935         -  }
   936         -
   937         -  /* Check if this call is removing or replacing an existing collation 
   938         -  ** sequence. If so, and there are active VMs, return busy. If there
   939         -  ** are no active VMs, invalidate any pre-compiled statements.
   940         -  */
   941         -  pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
   942         -  if( pColl && pColl->xCmp ){
   943         -    if( db->activeVdbeCnt ){
   944         -      sqlite3Error(db, SQLITE_BUSY, 
   945         -        "Unable to delete/modify collation sequence due to active statements");
   946         -      return SQLITE_BUSY;
   947         -    }
   948         -    sqlite3ExpirePreparedStatements(db);
   949         -  }
   950         -
   951         -  pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
   952         -  if( 0==pColl ){
   953         -    rc = SQLITE_NOMEM;
   954         -  }else{
   955         -    pColl->xCmp = xCompare;
   956         -    pColl->pUser = pCtx;
   957         -    pColl->enc = enc;
   958         -  }
   959         -  sqlite3Error(db, rc, 0);
   960         -  return rc;
          980  +  int rc;
          981  +  assert( !sqlite3MallocFailed() );
          982  +  rc = createCollation(db, zName, enc, pCtx, xCompare);
          983  +  return sqlite3ApiExit(db, rc);
   961    984   }
   962    985   
   963    986   #ifndef SQLITE_OMIT_UTF16
   964    987   /*
   965    988   ** Register a new collation sequence with the database handle db.
   966    989   */
   967    990   int sqlite3_create_collation16(
   968    991     sqlite3* db, 
   969    992     const char *zName, 
   970    993     int enc, 
   971    994     void* pCtx,
   972    995     int(*xCompare)(void*,int,const void*,int,const void*)
   973    996   ){
   974         -  char *zName8;
   975         -  int rc;
   976         -  if( sqlite3SafetyCheck(db) ){
   977         -    return SQLITE_MISUSE;
          997  +  int rc = SQLITE_OK;
          998  +  char *zName8; 
          999  +  assert( !sqlite3MallocFailed() );
         1000  +  zName8 = sqlite3utf16to8(zName, -1);
         1001  +  if( zName8 ){
         1002  +    rc = createCollation(db, zName8, enc, pCtx, xCompare);
         1003  +    sqliteFree(zName8);
   978   1004     }
   979         -  zName8 = sqlite3utf16to8(zName, -1);
   980         -  rc = sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
   981         -  sqliteFree(zName8);
   982         -  return rc;
         1005  +  return sqlite3ApiExit(db, rc);
   983   1006   }
   984   1007   #endif /* SQLITE_OMIT_UTF16 */
   985   1008   
   986   1009   /*
   987   1010   ** Register a collation sequence factory callback with the database handle
   988   1011   ** db. Replace any previously installed collation sequence factory.
   989   1012   */
................................................................................
  1061   1084   ** current thread.
  1062   1085   **
  1063   1086   ** This routine should only be called when there are no open
  1064   1087   ** database connections.
  1065   1088   */
  1066   1089   int sqlite3_enable_shared_cache(int enable){
  1067   1090     ThreadData *pTd = sqlite3ThreadData();
         1091  +  if( !pTd ){
         1092  +    return SQLITE_NOMEM;
         1093  +  }
  1068   1094     
  1069   1095     /* It is only legal to call sqlite3_enable_shared_cache() when there
  1070   1096     ** are no currently open b-trees that were opened by the calling thread.
  1071   1097     ** This condition is only easy to detect if the shared-cache were 
  1072   1098     ** previously enabled (and is being disabled). 
  1073   1099     */
  1074   1100     if( pTd->pBtree && !enable ){
................................................................................
  1083   1109   #endif
  1084   1110   
  1085   1111   /*
  1086   1112   ** This is a convenience routine that makes sure that all thread-specific
  1087   1113   ** data for this thread has been deallocated.
  1088   1114   */
  1089   1115   void sqlite3_thread_cleanup(void){
  1090         -  ThreadData *pTd = sqlite3ThreadData();
  1091         -  memset(pTd, 0, sizeof(*pTd));
  1092         -  sqlite3ReleaseThreadData();
         1116  +  ThreadData *pTd = sqlite3OsThreadSpecificData(0);
         1117  +  if( pTd ){
         1118  +    memset(pTd, 0, sizeof(*pTd));
         1119  +    sqlite3OsThreadSpecificData(-1);
         1120  +  }
  1093   1121   }

Changes to SQLite.Interop/src/os.h.

   287    287   int sqlite3OsSyncDirectory(const char*);
   288    288   int sqlite3OsTempFileName(char*);
   289    289   int sqlite3OsRandomSeed(char*);
   290    290   int sqlite3OsSleep(int ms);
   291    291   int sqlite3OsCurrentTime(double*);
   292    292   void sqlite3OsEnterMutex(void);
   293    293   void sqlite3OsLeaveMutex(void);
   294         -int sqlite3OsInMutex(void);
          294  +int sqlite3OsInMutex(int);
   295    295   ThreadData *sqlite3OsThreadSpecificData(int);
   296    296   void *sqlite3OsMalloc(int);
   297    297   void *sqlite3OsRealloc(void *, int);
   298    298   void sqlite3OsFree(void *);
   299    299   int sqlite3OsAllocationSize(void *);
   300    300   
   301    301   /*
................................................................................
   330    330   
   331    331     int (*xRandomSeed)(char*);
   332    332     int (*xSleep)(int ms);
   333    333     int (*xCurrentTime)(double*);
   334    334   
   335    335     void (*xEnterMutex)(void);
   336    336     void (*xLeaveMutex)(void);
   337         -  int (*xInMutex)(void);
          337  +  int (*xInMutex)(int);
   338    338     ThreadData *(*xThreadSpecificData)(int);
   339    339   
   340    340     void *(*xMalloc)(int);
   341    341     void *(*xRealloc)(void *, int);
   342    342     void (*xFree)(void *);
   343    343     int (*xAllocationSize)(void *);
   344    344   };

Changes to SQLite.Interop/src/os_common.h.

   179    179     p = realloc(p, n);
   180    180     return p;
   181    181   }
   182    182   void sqlite3GenericFree(void *p){
   183    183     assert(p);
   184    184     free(p);
   185    185   }
   186         -#if 0   /* Never actually invoked */
   187         -int sqlite3GenericAllocationSize(void *p){
   188         -  assert(0);
   189         -}
   190         -#endif
          186  +/* Never actually used, but needed for the linker */
          187  +int sqlite3GenericAllocationSize(void *p){ return 0; }
   191    188   #endif

Changes to SQLite.Interop/src/os_unix.c.

   474    474   }
   475    475   #endif /* SQLITE_UNIX_THREADS */
   476    476   
   477    477   /*
   478    478   ** Release a lockInfo structure previously allocated by findLockInfo().
   479    479   */
   480    480   static void releaseLockInfo(struct lockInfo *pLock){
   481         -  assert( sqlite3OsInMutex() );
          481  +  assert( sqlite3OsInMutex(1) );
   482    482     pLock->nRef--;
   483    483     if( pLock->nRef==0 ){
   484    484       sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
   485    485       sqliteFree(pLock);
   486    486     }
   487    487   }
   488    488   
   489    489   /*
   490    490   ** Release a openCnt structure previously allocated by findLockInfo().
   491    491   */
   492    492   static void releaseOpenCnt(struct openCnt *pOpen){
   493         -  assert( sqlite3OsInMutex() );
          493  +  assert( sqlite3OsInMutex(1) );
   494    494     pOpen->nRef--;
   495    495     if( pOpen->nRef==0 ){
   496    496       sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
   497    497       free(pOpen->aPending);
   498    498       sqliteFree(pOpen);
   499    499     }
   500    500   }
................................................................................
   516    516     struct openKey key2;
   517    517     struct stat statbuf;
   518    518     struct lockInfo *pLock;
   519    519     struct openCnt *pOpen;
   520    520     rc = fstat(fd, &statbuf);
   521    521     if( rc!=0 ) return 1;
   522    522   
   523         -  assert( sqlite3OsInMutex() );
          523  +  assert( sqlite3OsInMutex(1) );
   524    524     memset(&key1, 0, sizeof(key1));
   525    525     key1.dev = statbuf.st_dev;
   526    526     key1.ino = statbuf.st_ino;
   527    527   #ifdef SQLITE_UNIX_THREADS
   528    528     if( threadsOverrideEachOthersLocks<0 ){
   529    529       testThreadLockingBehavior(fd);
   530    530     }
................................................................................
   689    689     int *pReadonly
   690    690   ){
   691    691     int rc;
   692    692     unixFile f;
   693    693   
   694    694     CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly);
   695    695     assert( 0==*pId );
   696         -  f.dirfd = -1;
   697         -  SET_THREADID(&f);
   698    696     f.h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
   699    697                             SQLITE_DEFAULT_FILE_PERMISSIONS);
   700    698     if( f.h<0 ){
   701    699   #ifdef EISDIR
   702    700       if( errno==EISDIR ){
   703    701         return SQLITE_CANTOPEN;
   704    702       }
................................................................................
   714    712     sqlite3OsEnterMutex();
   715    713     rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
   716    714     sqlite3OsLeaveMutex();
   717    715     if( rc ){
   718    716       close(f.h);
   719    717       return SQLITE_NOMEM;
   720    718     }
   721         -  f.locktype = 0;
   722    719     TRACE3("OPEN    %-3d %s\n", f.h, zFilename);
   723    720     return allocateUnixFile(&f, pId);
   724    721   }
   725    722   
   726    723   
   727    724   /*
   728    725   ** Attempt to open a new file for exclusive access by this process.
................................................................................
   743    740     unixFile f;
   744    741   
   745    742     CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag);
   746    743     assert( 0==*pId );
   747    744     if( access(zFilename, 0)==0 ){
   748    745       return SQLITE_CANTOPEN;
   749    746     }
   750         -  SET_THREADID(&f);
   751         -  f.dirfd = -1;
   752    747     f.h = open(zFilename,
   753    748                   O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
   754    749                   SQLITE_DEFAULT_FILE_PERMISSIONS);
   755    750     if( f.h<0 ){
   756    751       return SQLITE_CANTOPEN;
   757    752     }
   758    753     sqlite3OsEnterMutex();
................................................................................
   759    754     rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
   760    755     sqlite3OsLeaveMutex();
   761    756     if( rc ){
   762    757       close(f.h);
   763    758       unlink(zFilename);
   764    759       return SQLITE_NOMEM;
   765    760     }
   766         -  f.locktype = 0;
   767    761     if( delFlag ){
   768    762       unlink(zFilename);
   769    763     }
   770    764     TRACE3("OPEN-EX %-3d %s\n", f.h, zFilename);
   771    765     return allocateUnixFile(&f, pId);
   772    766   }
   773    767   
................................................................................
   780    774   */
   781    775   int sqlite3UnixOpenReadOnly(const char *zFilename, OsFile **pId){
   782    776     int rc;
   783    777     unixFile f;
   784    778   
   785    779     CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0);
   786    780     assert( 0==*pId );
   787         -  SET_THREADID(&f);
   788         -  f.dirfd = -1;
   789    781     f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
   790    782     if( f.h<0 ){
   791    783       return SQLITE_CANTOPEN;
   792    784     }
   793    785     sqlite3OsEnterMutex();
   794    786     rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
   795    787     sqlite3OsLeaveMutex();
   796    788     if( rc ){
   797    789       close(f.h);
   798    790       return SQLITE_NOMEM;
   799    791     }
   800         -  f.locktype = 0;
   801    792     TRACE3("OPEN-RO %-3d %s\n", f.h, zFilename);
   802         -
   803    793     return allocateUnixFile(&f, pId);
   804    794   }
   805    795   
   806    796   /*
   807    797   ** Attempt to open a file descriptor for the directory that contains a
   808    798   ** file.  This file descriptor can be used to fsync() the directory
   809    799   ** in order to make sure the creation of a new file is actually written
................................................................................
  1467   1457   }
  1468   1458   
  1469   1459   /*
  1470   1460   ** Close a file.
  1471   1461   */
  1472   1462   static int unixClose(OsFile **pId){
  1473   1463     unixFile *id = (unixFile*)*pId;
  1474         -  int rc;
  1475   1464   
  1476   1465     if( !id ) return SQLITE_OK;
  1477   1466     unixUnlock(*pId, NO_LOCK);
  1478   1467     if( id->dirfd>=0 ) close(id->dirfd);
  1479   1468     id->dirfd = -1;
  1480   1469     sqlite3OsEnterMutex();
  1481   1470   
................................................................................
  1504   1493   
  1505   1494     sqlite3OsLeaveMutex();
  1506   1495     id->isOpen = 0;
  1507   1496     TRACE2("CLOSE   %-3d\n", id->h);
  1508   1497     OpenCounter(-1);
  1509   1498     sqliteFree(id);
  1510   1499     *pId = 0;
  1511         -  return rc;
         1500  +  return SQLITE_OK;
  1512   1501   }
  1513   1502   
  1514   1503   /*
  1515   1504   ** Turn a relative pathname into a full pathname.  Return a pointer
  1516   1505   ** to the full pathname stored in space obtained from sqliteMalloc().
  1517   1506   ** The calling function is responsible for freeing this space once it
  1518   1507   ** is no longer needed.
................................................................................
  1580   1569   /*
  1581   1570   ** Allocate memory for a unixFile.  Initialize the new unixFile
  1582   1571   ** to the value given in pInit and return a pointer to the new
  1583   1572   ** OsFile.  If we run out of memory, close the file and return NULL.
  1584   1573   */
  1585   1574   static int allocateUnixFile(unixFile *pInit, OsFile **pId){
  1586   1575     unixFile *pNew;
         1576  +  pInit->dirfd = -1;
         1577  +  pInit->fullSync = 0;
         1578  +  pInit->locktype = 0;
         1579  +  SET_THREADID(pInit);
  1587   1580     pNew = sqliteMalloc( sizeof(unixFile) );
  1588   1581     if( pNew==0 ){
  1589   1582       close(pInit->h);
  1590   1583       sqlite3OsEnterMutex();
  1591   1584       releaseLockInfo(pInit->pLock);
  1592   1585       releaseOpenCnt(pInit->pOpen);
  1593   1586       sqlite3OsLeaveMutex();
................................................................................
  1663   1656   }
  1664   1657   
  1665   1658   /*
  1666   1659   ** Static variables used for thread synchronization
  1667   1660   */
  1668   1661   static int inMutex = 0;
  1669   1662   #ifdef SQLITE_UNIX_THREADS
  1670         -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
         1663  +static pthread_t mutexOwner;
         1664  +static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
         1665  +static pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
  1671   1666   #endif
  1672   1667   
  1673   1668   /*
  1674   1669   ** The following pair of routine implement mutual exclusion for
  1675   1670   ** multi-threaded processes.  Only a single thread is allowed to
  1676   1671   ** executed code that is surrounded by EnterMutex() and LeaveMutex().
  1677   1672   **
  1678   1673   ** SQLite uses only a single Mutex.  There is not much critical
  1679   1674   ** code and what little there is executes quickly and without blocking.
  1680   1675   **
  1681         -** This mutex is not recursive.
         1676  +** As of version 3.3.2, this mutex must be recursive.
  1682   1677   */
  1683   1678   void sqlite3UnixEnterMutex(){
  1684   1679   #ifdef SQLITE_UNIX_THREADS
  1685         -  pthread_mutex_lock(&mutex);
         1680  +  pthread_mutex_lock(&mutex1);
         1681  +  if( inMutex==0 ){
         1682  +    pthread_mutex_lock(&mutex2);
         1683  +    mutexOwner = pthread_self();
         1684  +  }
         1685  +  pthread_mutex_unlock(&mutex1);
  1686   1686   #endif
  1687         -  assert( !inMutex );
  1688         -  inMutex = 1;
         1687  +  inMutex++;
  1689   1688   }
  1690   1689   void sqlite3UnixLeaveMutex(){
  1691         -  assert( inMutex );
  1692         -  inMutex = 0;
         1690  +  assert( inMutex>0 );
  1693   1691   #ifdef SQLITE_UNIX_THREADS
  1694         -  pthread_mutex_unlock(&mutex);
         1692  +  assert( pthread_equal(mutexOwner, pthread_self()) );
         1693  +  pthread_mutex_lock(&mutex1);
         1694  +  inMutex--;
         1695  +  if( inMutex==0 ){
         1696  +    pthread_mutex_unlock(&mutex2);
         1697  +  }
         1698  +  pthread_mutex_unlock(&mutex1);
         1699  +#else
         1700  +  inMutex--;
  1695   1701   #endif
  1696   1702   }
  1697   1703   
  1698   1704   /*
  1699         -** Return TRUE if we are currently within the mutex and FALSE if not.
         1705  +** Return TRUE if the mutex is currently held.
         1706  +**
         1707  +** If the thisThreadOnly parameter is true, return true only if the
         1708  +** calling thread holds the mutex.  If the parameter is false, return
         1709  +** true if any thread holds the mutex.
  1700   1710   */
  1701         -int sqlite3UnixInMutex(){
  1702         -  return inMutex;
         1711  +int sqlite3UnixInMutex(int thisThreadOnly){
         1712  +#ifdef SQLITE_UNIX_THREADS
         1713  +  return inMutex>0 && 
         1714  +           (thisThreadOnly==0 || pthread_equal(mutexOwner, pthread_self()));
         1715  +#else
         1716  +  return inMutex>0;
         1717  +#endif
  1703   1718   }
  1704   1719   
  1705   1720   /*
  1706   1721   ** Remember the number of thread-specific-data blocks allocated.
  1707   1722   ** Use this to verify that we are not leaking thread-specific-data.
  1708   1723   ** Ticket #1601
  1709   1724   */
................................................................................
  1717   1732                pthread_mutex_unlock(&tsd_counter_mutex);
  1718   1733   # else
  1719   1734   #   define TSD_COUNTER(N)  sqlite3_tsd_count += N
  1720   1735   # endif
  1721   1736   #else
  1722   1737   # define TSD_COUNTER(N)  /* no-op */
  1723   1738   #endif
  1724         -
  1725   1739   
  1726   1740   /*
  1727   1741   ** If called with allocateFlag>0, then return a pointer to thread
  1728   1742   ** specific data for the current thread.  Allocate and zero the
  1729   1743   ** thread-specific data if it does not already exist.
  1730   1744   **
  1731   1745   ** If called with allocateFlag==0, then check the current thread
................................................................................
  1757   1771       }
  1758   1772       sqlite3OsLeaveMutex();
  1759   1773     }
  1760   1774   
  1761   1775     pTsd = pthread_getspecific(key);
  1762   1776     if( allocateFlag>0 ){
  1763   1777       if( pTsd==0 ){
  1764         -      pTsd = sqlite3OsMalloc(sizeof(zeroData));
         1778  +      if( !sqlite3TestMallocFail() ){
         1779  +        pTsd = sqlite3OsMalloc(sizeof(zeroData));
         1780  +      }
         1781  +#ifdef SQLITE_MEMDEBUG
         1782  +      sqlite3_isFail = 0;
         1783  +#endif
  1765   1784         if( pTsd ){
  1766   1785           *pTsd = zeroData;
  1767   1786           pthread_setspecific(key, pTsd);
  1768   1787           TSD_COUNTER(+1);
  1769   1788         }
  1770   1789       }
  1771   1790     }else if( pTsd!=0 && allocateFlag<0 
  1772         -            && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1791  +            && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
  1773   1792       sqlite3OsFree(pTsd);
  1774   1793       pthread_setspecific(key, 0);
  1775   1794       TSD_COUNTER(-1);
  1776   1795       pTsd = 0;
  1777   1796     }
  1778   1797     return pTsd;
  1779   1798   #else
  1780   1799     static ThreadData *pTsd = 0;
  1781   1800     if( allocateFlag>0 ){
  1782   1801       if( pTsd==0 ){
  1783         -      pTsd = sqlite3OsMalloc( sizeof(zeroData) );
         1802  +      if( !sqlite3TestMallocFail() ){
         1803  +        pTsd = sqlite3OsMalloc( sizeof(zeroData) );
         1804  +      }
         1805  +#ifdef SQLITE_MEMDEBUG
         1806  +      sqlite3_isFail = 0;
         1807  +#endif
  1784   1808         if( pTsd ){
  1785   1809           *pTsd = zeroData;
  1786   1810           TSD_COUNTER(+1);
  1787   1811         }
  1788   1812       }
  1789   1813     }else if( pTsd!=0 && allocateFlag<0
  1790         -            && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1814  +            && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
  1791   1815       sqlite3OsFree(pTsd);
  1792   1816       TSD_COUNTER(-1);
  1793   1817       pTsd = 0;
  1794   1818     }
  1795   1819     return pTsd;
  1796   1820   #endif
  1797   1821   }

Changes to SQLite.Interop/src/os_win.c.

   599    599         *pReadonly = 1;
   600    600       }else{
   601    601         *pReadonly = 0;
   602    602       }
   603    603   #endif /* OS_WINCE */
   604    604     }
   605    605     f.h = h;
   606         -  f.locktype = NO_LOCK;
   607         -  f.sharedLockByte = 0;
   608    606   #if OS_WINCE
   609    607     f.zDeleteOnClose = 0;
   610    608   #endif
   611    609     TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
   612    610     return allocateWinFile(&f, pId);
   613    611   }
   614    612   
................................................................................
   663    661       );
   664    662   #endif /* OS_WINCE */
   665    663     }
   666    664     if( h==INVALID_HANDLE_VALUE ){
   667    665       return SQLITE_CANTOPEN;
   668    666     }
   669    667     f.h = h;
   670         -  f.locktype = NO_LOCK;
   671         -  f.sharedLockByte = 0;
   672    668   #if OS_WINCE
   673    669     f.zDeleteOnClose = delFlag ? utf8ToUnicode(zFilename) : 0;
   674         -  f.hMutex = NULL;
   675    670   #endif
   676    671     TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
   677    672     return allocateWinFile(&f, pId);
   678    673   }
   679    674   
   680    675   /*
   681    676   ** Attempt to open a new file for read-only access.
................................................................................
   713    708       );
   714    709   #endif
   715    710     }
   716    711     if( h==INVALID_HANDLE_VALUE ){
   717    712       return SQLITE_CANTOPEN;
   718    713     }
   719    714     f.h = h;
   720         -  f.locktype = NO_LOCK;
   721         -  f.sharedLockByte = 0;
   722    715   #if OS_WINCE
   723    716     f.zDeleteOnClose = 0;
   724         -  f.hMutex = NULL;
   725    717   #endif
   726    718     TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
   727    719     return allocateWinFile(&f, pId);
   728    720   }
   729    721   
   730    722   /*
   731    723   ** Attempt to open a file descriptor for the directory that contains a
................................................................................
  1304   1296       sqliteFree(pInit->zDeleteOnClose);
  1305   1297   #endif
  1306   1298       *pId = 0;
  1307   1299       return SQLITE_NOMEM;
  1308   1300     }else{
  1309   1301       *pNew = *pInit;
  1310   1302       pNew->pMethod = &sqlite3WinIoMethod;
         1303  +    pNew->locktype = NO_LOCK;
         1304  +    pNew->sharedLockByte = 0;
         1305  +#if OS_WINCE
         1306  +    pNew->hMutex = NULL;
         1307  +#endif
  1311   1308       *pId = (OsFile*)pNew;
         1309  +    OpenCounter(+1);
  1312   1310       return SQLITE_OK;
  1313   1311     }
  1314   1312   }
  1315   1313   
  1316   1314   
  1317   1315   #endif /* SQLITE_OMIT_DISKIO */
  1318   1316   /***************************************************************************
................................................................................
  1352   1350   }
  1353   1351   
  1354   1352   /*
  1355   1353   ** Static variables used for thread synchronization
  1356   1354   */
  1357   1355   static int inMutex = 0;
  1358   1356   #ifdef SQLITE_W32_THREADS
         1357  +  static DWORD mutexOwner;
  1359   1358     static CRITICAL_SECTION cs;
  1360   1359   #endif
  1361   1360   
  1362   1361   /*
  1363         -** The following pair of routine implement mutual exclusion for
         1362  +** The following pair of routines implement mutual exclusion for
  1364   1363   ** multi-threaded processes.  Only a single thread is allowed to
  1365   1364   ** executed code that is surrounded by EnterMutex() and LeaveMutex().
  1366   1365   **
  1367   1366   ** SQLite uses only a single Mutex.  There is not much critical
  1368   1367   ** code and what little there is executes quickly and without blocking.
         1368  +**
         1369  +** Version 3.3.1 and earlier used a simple mutex.  Beginning with
         1370  +** version 3.3.2, a recursive mutex is required.
  1369   1371   */
  1370   1372   void sqlite3WinEnterMutex(){
  1371   1373   #ifdef SQLITE_W32_THREADS
  1372   1374     static int isInit = 0;
  1373   1375     while( !isInit ){
  1374   1376       static long lock = 0;
  1375   1377       if( InterlockedIncrement(&lock)==1 ){
................................................................................
  1376   1378         InitializeCriticalSection(&cs);
  1377   1379         isInit = 1;
  1378   1380       }else{
  1379   1381         Sleep(1);
  1380   1382       }
  1381   1383     }
  1382   1384     EnterCriticalSection(&cs);
         1385  +  mutexOwner = GetCurrentThreadId();
  1383   1386   #endif
  1384         -  assert( !inMutex );
  1385         -  inMutex = 1;
         1387  +  inMutex++;
  1386   1388   }
  1387   1389   void sqlite3WinLeaveMutex(){
  1388   1390     assert( inMutex );
  1389         -  inMutex = 0;
         1391  +  inMutex--;
  1390   1392   #ifdef SQLITE_W32_THREADS
         1393  +  assert( mutexOwner==GetCurrentThreadId() );
  1391   1394     LeaveCriticalSection(&cs);
  1392   1395   #endif
  1393   1396   }
  1394   1397   
  1395   1398   /*
  1396         -** Return TRUE if we are currently within the mutex and FALSE if not.
         1399  +** Return TRUE if the mutex is currently held.
         1400  +**
         1401  +** If the thisThreadOnly parameter is true, return true if and only if the
         1402  +** calling thread holds the mutex.  If the parameter is false, return
         1403  +** true if any thread holds the mutex.
  1397   1404   */
  1398         -int sqlite3WinInMutex(){
  1399         -  return inMutex;
         1405  +int sqlite3WinInMutex(int thisThreadOnly){
         1406  +#ifdef SQLITE_W32_THREADS
         1407  +  return inMutex>0 && (thisThreadOnly==0 || mutexOwner==GetCurrentThreadId());
         1408  +#else
         1409  +  return inMutex>0;
         1410  +#endif
  1400   1411   }
  1401   1412   
  1402   1413   
  1403   1414   /*
  1404   1415   ** The following variable, if set to a non-zero value, becomes the result
  1405   1416   ** returned from sqlite3OsCurrentTime().  This is used for testing.
  1406   1417   */
................................................................................
  1491   1502         if( pTsd ){
  1492   1503           *pTsd = zeroData;
  1493   1504           TlsSetValue(key, pTsd);
  1494   1505           TSD_COUNTER_INCR;
  1495   1506         }
  1496   1507       }
  1497   1508     }else if( pTsd!=0 && allocateFlag<0 
  1498         -              && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1509  +              && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
  1499   1510       sqlite3OsFree(pTsd);
  1500   1511       TlsSetValue(key, 0);
  1501   1512       TSD_COUNTER_DECR;
  1502   1513       pTsd = 0;
  1503   1514     }
  1504   1515     return pTsd;
  1505   1516   }
  1506   1517   #endif /* OS_WIN */

Changes to SQLite.Interop/src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
           21  +** @(#) $Id: pager.c,v 1.18 2006/01/23 19:45:55 rmsimpson Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
   425    425   }
   426    426   
   427    427   /*
   428    428   ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
   429    429   ** on success or an error code is something goes wrong.
   430    430   */
   431    431   static int write32bits(OsFile *fd, u32 val){
   432         -  unsigned char ac[4];
          432  +  char ac[4];
   433    433     put32bits(ac, val);
   434    434     return sqlite3OsWrite(fd, ac, 4);
   435    435   }
   436    436   
   437    437   /*
   438    438   ** Write the 32-bit integer 'val' into the page identified by page header
   439    439   ** 'p' at offset 'offset'.
   440    440   */
   441    441   static void store32bits(u32 val, PgHdr *p, int offset){
   442         -  unsigned char *ac;
   443         -  ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
          442  +  char *ac;
          443  +  ac = &((char*)PGHDR_TO_DATA(p))[offset];
   444    444     put32bits(ac, val);
   445    445   }
   446    446   
   447    447   /*
   448    448   ** Read a 32-bit integer at offset 'offset' from the page identified by
   449    449   ** page header 'p'.
   450    450   */
................................................................................
   464    464   ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_PROTOCOL,
   465    465   ** the error becomes persistent. All subsequent API calls on this Pager
   466    466   ** will immediately return the same error code.
   467    467   */
   468    468   static int pager_error(Pager *pPager, int rc){
   469    469     assert( pPager->errCode==SQLITE_FULL || pPager->errCode==SQLITE_OK );
   470    470     if( 
   471         -    rc==SQLITE_FULL || 
   472         -    rc==SQLITE_IOERR || 
          471  +    rc==SQLITE_FULL ||
          472  +    rc==SQLITE_IOERR ||
   473    473       rc==SQLITE_CORRUPT ||
   474    474       rc==SQLITE_PROTOCOL
   475    475     ){
   476    476       pPager->errCode = rc;
   477    477     }
   478    478     return rc;
   479    479   }
................................................................................
   905    905       assert( pPager->aInJournal==0 );
   906    906       assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
   907    907     }
   908    908     rc = sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
   909    909     pPager->state = PAGER_SHARED;
   910    910     pPager->origDbSize = 0;
   911    911     pPager->setMaster = 0;
          912  +  pPager->needSync = 0;
          913  +  pPager->pFirstSynced = pPager->pFirst;
   912    914     return rc;
   913    915   }
   914    916   
   915    917   /*
   916    918   ** Compute and return a checksum for the page of data.
   917    919   **
   918    920   ** This is not a real checksum.  It is really just the sum of the 
................................................................................
  1458   1460         if( rc!=SQLITE_OK ) goto end_stmt_playback;
  1459   1461       }
  1460   1462     }
  1461   1463   
  1462   1464     pPager->journalOff = szJ;
  1463   1465     
  1464   1466   end_stmt_playback:
  1465         -  if( rc!=SQLITE_OK ){
  1466         -    rc = pager_error(pPager, SQLITE_CORRUPT);
  1467         -  }else{
         1467  +  if( rc==SQLITE_OK) {
  1468   1468       pPager->journalOff = szJ;
  1469   1469       /* pager_reload_cache(pPager); */
  1470   1470     }
  1471   1471     return rc;
  1472   1472   }
  1473   1473   
  1474   1474   /*
................................................................................
  1562   1562     Pager **ppPager,         /* Return the Pager structure here */
  1563   1563     const char *zFilename,   /* Name of the database file to open */
  1564   1564     int nExtra,              /* Extra bytes append to each in-memory page */
  1565   1565     int flags                /* flags controlling this file */
  1566   1566   ){
  1567   1567     Pager *pPager = 0;
  1568   1568     char *zFullPathname = 0;
  1569         -  int nameLen;
         1569  +  int nameLen;  /* Compiler is wrong. This is always initialized before use */
  1570   1570     OsFile *fd;
  1571   1571     int rc = SQLITE_OK;
  1572   1572     int i;
  1573   1573     int tempFile = 0;
  1574   1574     int memDb = 0;
  1575   1575     int readOnly = 0;
  1576   1576     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
  1577   1577     int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
  1578   1578     char zTemp[SQLITE_TEMPNAME_SIZE];
  1579   1579   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1580         -  const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
         1580  +  /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to 
         1581  +  ** malloc() must have already been made by this thread before it gets
         1582  +  ** to this point. This means the ThreadData must have been allocated already
         1583  +  ** so that ThreadData.nAlloc can be set. It would be nice to assert
         1584  +  ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases 
         1585  +  ** written to invoke the pager directly.
         1586  +  */
         1587  +  ThreadData *pTsd = sqlite3ThreadData();
         1588  +  assert( pTsd );
  1581   1589   #endif
  1582   1590   
  1583   1591     /* If malloc() has already failed return SQLITE_NOMEM. Before even
  1584   1592     ** testing for this, set *ppPager to NULL so the caller knows the pager
  1585   1593     ** structure was never allocated. 
  1586   1594     */
  1587   1595     *ppPager = 0;
  1588         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1596  +  if( sqlite3MallocFailed() ){
  1589   1597       return SQLITE_NOMEM;
  1590   1598     }
  1591   1599     memset(&fd, 0, sizeof(fd));
  1592   1600   
  1593   1601     /* Open the pager file and set zFullPathname to point at malloc()ed 
  1594   1602     ** memory containing the complete filename (i.e. including the directory).
  1595   1603     */
................................................................................
  1676   1684     /* pPager->pLast = 0; */
  1677   1685     pPager->nExtra = FORCE_ALIGNMENT(nExtra);
  1678   1686     pPager->sectorSize = PAGER_SECTOR_SIZE;
  1679   1687     /* pPager->pBusyHandler = 0; */
  1680   1688     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  1681   1689     *ppPager = pPager;
  1682   1690   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1683         -  if( pTsdro->useMemoryManagement ){
  1684         -    ThreadData *pTsd = sqlite3ThreadData();
  1685         -    pPager->pNext = pTsd->pPager;
  1686         -    pTsd->pPager = pPager;
  1687         -  }
         1691  +  pPager->pNext = pTsd->pPager;
         1692  +  pTsd->pPager = pPager;
  1688   1693   #endif
  1689   1694     return SQLITE_OK;
  1690   1695   }
  1691   1696   
  1692   1697   /*
  1693   1698   ** Set the busy handler function.
  1694   1699   */
................................................................................
  1984   1989   ** is made to roll it back. If an error occurs during the rollback 
  1985   1990   ** a hot journal may be left in the filesystem but no error is returned
  1986   1991   ** to the caller.
  1987   1992   */
  1988   1993   int sqlite3pager_close(Pager *pPager){
  1989   1994     PgHdr *pPg, *pNext;
  1990   1995   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1991         -  const ThreadData *pTsd = sqlite3ThreadDataReadOnly();
         1996  +  /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to 
         1997  +  ** malloc() must have already been made by this thread before it gets
         1998  +  ** to this point. This means the ThreadData must have been allocated already
         1999  +  ** so that ThreadData.nAlloc can be set.
         2000  +  */
         2001  +  ThreadData *pTsd = sqlite3ThreadData();
         2002  +  assert( pPager );
         2003  +  assert( pTsd && pTsd->nAlloc );
  1992   2004   #endif
  1993   2005   
  1994   2006     switch( pPager->state ){
  1995   2007       case PAGER_RESERVED:
  1996   2008       case PAGER_SYNCED: 
  1997   2009       case PAGER_EXCLUSIVE: {
  1998   2010         /* We ignore any IO errors that occur during the rollback
................................................................................
  2047   2059     ** }
  2048   2060     */
  2049   2061   
  2050   2062   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  2051   2063     /* Remove the pager from the linked list of pagers starting at 
  2052   2064     ** ThreadData.pPager if memory-management is enabled.
  2053   2065     */
  2054         -  if( pTsd->useMemoryManagement ){
  2055         -    if( pPager==pTsd->pPager ){
  2056         -      pTsd->pPager = pPager->pNext;
  2057         -    }else{
  2058         -      Pager *pTmp;
  2059         -      for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext);
  2060         -      pTmp->pNext = pPager->pNext;
  2061         -    }
         2066  +  if( pPager==pTsd->pPager ){
         2067  +    pTsd->pPager = pPager->pNext;
         2068  +  }else{
         2069  +    Pager *pTmp;
         2070  +    for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext);
         2071  +    pTmp->pNext = pPager->pNext;
  2062   2072     }
  2063   2073   #endif
  2064   2074   
  2065   2075   #ifdef SQLITE_HAS_CODEC
  2066   2076     sqlite3pager_free_codecarg(pPager->pCodecArg);
  2067   2077   #endif
  2068   2078     sqliteFree(pPager);
................................................................................
  2427   2437     int i;
  2428   2438   
  2429   2439     /* If the the global mutex is held, this subroutine becomes a
  2430   2440     ** o-op; zero bytes of memory are freed.  This is because
  2431   2441     ** some of the code invoked by this function may also
  2432   2442     ** try to obtain the mutex, resulting in a deadlock.
  2433   2443     */
  2434         -  if( sqlite3OsInMutex() ){
         2444  +  if( sqlite3OsInMutex(0) ){
  2435   2445       return 0;
  2436   2446     }
  2437   2447   
  2438   2448     /* Outermost loop runs for at most two iterations. First iteration we
  2439   2449     ** try to find memory that can be released without calling fsync(). Second
  2440   2450     ** iteration (which only runs if the first failed to free nReq bytes of
  2441   2451     ** memory) is permitted to call fsync(). This is of course much more 
................................................................................
  2629   2639         if( pPager->nPage>pPager->nMaxPage ){
  2630   2640           assert( pPager->nMaxPage==(pPager->nPage-1) );
  2631   2641           pPager->nMaxPage++;
  2632   2642         }
  2633   2643       }else{
  2634   2644         rc = pager_recycle(pPager, 1, &pPg);
  2635   2645         if( rc!=SQLITE_OK ){
  2636         -        return pager_error(pPager, rc);
         2646  +        return rc;
  2637   2647         }
  2638   2648         assert(pPg) ;
  2639   2649       }
  2640   2650       pPg->pgno = pgno;
  2641   2651       if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
  2642   2652         sqlite3CheckMemory(pPager->aInJournal, pgno/8);
  2643   2653         assert( pPager->journalOpen );
................................................................................
  2652   2662         page_add_to_stmt_list(pPg);
  2653   2663       }else{
  2654   2664         page_remove_from_stmt_list(pPg);
  2655   2665       }
  2656   2666       pPg->dirty = 0;
  2657   2667       pPg->nRef = 1;
  2658   2668       REFINFO(pPg);
         2669  +
  2659   2670       pPager->nRef++;
  2660         -    h = pager_hash(pgno);
  2661         -    pPg->pNextHash = pPager->aHash[h];
  2662         -    pPager->aHash[h] = pPg;
  2663         -    if( pPg->pNextHash ){
  2664         -      assert( pPg->pNextHash->pPrevHash==0 );
  2665         -      pPg->pNextHash->pPrevHash = pPg;
  2666         -    }
  2667   2671       if( pPager->nExtra>0 ){
  2668   2672         memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
  2669   2673       }
  2670   2674       if( pPager->errCode ){
  2671   2675         sqlite3pager_unref(PGHDR_TO_DATA(pPg));
  2672   2676         rc = pPager->errCode;
  2673   2677         return rc;
  2674   2678       }
         2679  +
         2680  +    /* Populate the page with data, either by reading from the database
         2681  +    ** file, or by setting the entire page to zero.
         2682  +    */
  2675   2683       if( sqlite3pager_pagecount(pPager)<(int)pgno ){
  2676   2684         memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
  2677   2685       }else{
  2678   2686         int rc;
  2679   2687         assert( MEMDB==0 );
  2680   2688         rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
  2681   2689         if( rc==SQLITE_OK ){
................................................................................
  2682   2690           rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg),
  2683   2691                                 pPager->pageSize);
  2684   2692         }
  2685   2693         TRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
  2686   2694         CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
  2687   2695         if( rc!=SQLITE_OK ){
  2688   2696           i64 fileSize;
  2689         -        if( sqlite3OsFileSize(pPager->fd,&fileSize)!=SQLITE_OK
  2690         -               || fileSize>=pgno*pPager->pageSize ){
         2697  +        int rc2 = sqlite3OsFileSize(pPager->fd, &fileSize);
         2698  +        if( rc2!=SQLITE_OK || fileSize>=pgno*pPager->pageSize ){
         2699  +	  /* An IO error occured in one of the the sqlite3OsSeek() or
         2700  +          ** sqlite3OsRead() calls above. */
         2701  +          pPg->pgno = 0;
  2691   2702             sqlite3pager_unref(PGHDR_TO_DATA(pPg));
  2692         -          return pager_error(pPager, rc);
         2703  +          return rc;
  2693   2704           }else{
  2694   2705             clear_simulated_io_error();
  2695   2706             memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
  2696   2707           }
  2697   2708         }else{
  2698   2709           TEST_INCR(pPager->nRead);
  2699   2710         }
  2700   2711       }
         2712  +
         2713  +    /* Link the page into the page hash table */
         2714  +    h = pager_hash(pgno);
         2715  +    pPg->pNextHash = pPager->aHash[h];
         2716  +    pPager->aHash[h] = pPg;
         2717  +    if( pPg->pNextHash ){
         2718  +      assert( pPg->pNextHash->pPrevHash==0 );
         2719  +      pPg->pNextHash->pPrevHash = pPg;
         2720  +    }
         2721  +
  2701   2722   #ifdef SQLITE_CHECK_PAGES
  2702   2723       pPg->pageHash = pager_pagehash(pPg);
  2703   2724   #endif
  2704   2725     }else{
  2705   2726       /* The requested page is in the page cache. */
  2706   2727       TEST_INCR(pPager->nHit);
  2707   2728       page_ref(pPg);
................................................................................
  3006   3027             assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  3007   3028             CODEC(pPager, pData, pPg->pgno, 7);
  3008   3029             cksum = pager_cksum(pPager, pPg->pgno, pData);
  3009   3030             saved = *(u32*)PGHDR_TO_EXTRA(pPg, pPager);
  3010   3031             store32bits(cksum, pPg, pPager->pageSize);
  3011   3032             szPg = pPager->pageSize+8;
  3012   3033             store32bits(pPg->pgno, pPg, -4);
         3034  +
  3013   3035             rc = sqlite3OsWrite(pPager->jfd, &((char*)pData)[-4], szPg);
  3014   3036             pPager->journalOff += szPg;
  3015   3037             TRACE4("JOURNAL %d page %d needSync=%d\n",
  3016   3038                     PAGERID(pPager), pPg->pgno, pPg->needSync);
  3017   3039             CODEC(pPager, pData, pPg->pgno, 0);
  3018   3040             *(u32*)PGHDR_TO_EXTRA(pPg, pPager) = saved;
         3041  +
         3042  +	  /* An error has occured writing to the journal file. The 
         3043  +          ** transaction will be rolled back by the layer above.
         3044  +          */
  3019   3045             if( rc!=SQLITE_OK ){
  3020         -            sqlite3pager_rollback(pPager);
  3021         -            if( !pPager->errCode ){
  3022         -              pager_error(pPager, SQLITE_FULL);
  3023         -            }
  3024   3046               return rc;
  3025   3047             }
         3048  +
  3026   3049             pPager->nRec++;
  3027   3050             assert( pPager->aInJournal!=0 );
  3028   3051             pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3029   3052             pPg->needSync = !pPager->noSync;
  3030   3053             if( pPager->stmtInUse ){
  3031   3054               pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3032   3055               page_add_to_stmt_list(pPg);
................................................................................
  3062   3085           store32bits(pPg->pgno, pPg, -4);
  3063   3086           CODEC(pPager, pData, pPg->pgno, 7);
  3064   3087           rc = sqlite3OsWrite(pPager->stfd,((char*)pData)-4,
  3065   3088                                  pPager->pageSize+4);
  3066   3089           TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  3067   3090           CODEC(pPager, pData, pPg->pgno, 0);
  3068   3091           if( rc!=SQLITE_OK ){
  3069         -          sqlite3pager_rollback(pPager);
  3070         -          if( !pPager->errCode ){
  3071         -            pager_error(pPager, SQLITE_FULL);
  3072         -          }
  3073   3092             return rc;
  3074   3093           }
  3075   3094           pPager->stmtNRec++;
  3076   3095           assert( pPager->aInStmt!=0 );
  3077   3096           pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3078   3097         }
  3079   3098         page_add_to_stmt_list(pPg);
................................................................................
  3092   3111   }
  3093   3112   
  3094   3113   /*
  3095   3114   ** Return TRUE if the page given in the argument was previously passed
  3096   3115   ** to sqlite3pager_write().  In other words, return TRUE if it is ok
  3097   3116   ** to change the content of the page.
  3098   3117   */
         3118  +#ifndef NDEBUG
  3099   3119   int sqlite3pager_iswriteable(void *pData){
  3100   3120     PgHdr *pPg = DATA_TO_PGHDR(pData);
  3101   3121     return pPg->dirty;
  3102   3122   }
         3123  +#endif
  3103   3124   
  3104   3125   #ifndef SQLITE_OMIT_VACUUM
  3105   3126   /*
  3106   3127   ** Replace the content of a single page with the information in the third
  3107   3128   ** argument.
  3108   3129   */
  3109   3130   int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){
................................................................................
  3225   3246   ** and an error code is returned.  If the commit worked, SQLITE_OK
  3226   3247   ** is returned.
  3227   3248   */
  3228   3249   int sqlite3pager_commit(Pager *pPager){
  3229   3250     int rc;
  3230   3251     PgHdr *pPg;
  3231   3252   
  3232         -  if( pPager->errCode==SQLITE_FULL ){
  3233         -    rc = sqlite3pager_rollback(pPager);
  3234         -    if( rc==SQLITE_OK ){
  3235         -      rc = SQLITE_FULL;
  3236         -    }
  3237         -    return rc;
  3238         -  }
  3239   3253     if( pPager->errCode ){
  3240         -    rc = pPager->errCode;
  3241         -    return rc;
         3254  +    return pPager->errCode;
  3242   3255     }
  3243   3256     if( pPager->state<PAGER_RESERVED ){
  3244   3257       return SQLITE_ERROR;
  3245   3258     }
  3246   3259     TRACE2("COMMIT %d\n", PAGERID(pPager));
  3247   3260     if( MEMDB ){
  3248   3261       pPg = pager_get_all_dirty_pages(pPager);
................................................................................
  3272   3285       assert( pPager->needSync==0 );
  3273   3286       rc = pager_unwritelock(pPager);
  3274   3287       pPager->dbSize = -1;
  3275   3288       return rc;
  3276   3289     }
  3277   3290     assert( pPager->journalOpen );
  3278   3291     rc = sqlite3pager_sync(pPager, 0, 0);
  3279         -  if( rc!=SQLITE_OK ){
  3280         -    goto commit_abort;
         3292  +  if( rc==SQLITE_OK ){
         3293  +    rc = pager_unwritelock(pPager);
         3294  +    pPager->dbSize = -1;
  3281   3295     }
  3282         -  rc = pager_unwritelock(pPager);
  3283         -  pPager->dbSize = -1;
  3284         -  return rc;
  3285         -
  3286         -  /* Jump here if anything goes wrong during the commit process.
  3287         -  */
  3288         -commit_abort:
  3289         -  sqlite3pager_rollback(pPager);
  3290   3296     return rc;
  3291   3297   }
  3292   3298   
  3293   3299   /*
  3294   3300   ** Rollback all changes.  The database falls back to PAGER_SHARED mode.
  3295   3301   ** All in-memory cache pages revert to their original data contents.
  3296   3302   ** The journal is deleted.
................................................................................
  3360   3366       rc2 = pager_unwritelock(pPager);
  3361   3367       if( rc==SQLITE_OK ){
  3362   3368         rc = rc2;
  3363   3369       }
  3364   3370     }else{
  3365   3371       rc = pager_playback(pPager);
  3366   3372     }
  3367         -  if( rc!=SQLITE_OK ){
  3368         -    rc = SQLITE_CORRUPT_BKPT;
  3369         -    pager_error(pPager, SQLITE_CORRUPT);
  3370         -  }
  3371   3373     pPager->dbSize = -1;
  3372         -  return rc;
         3374  +
         3375  +  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
         3376  +  ** cache. So call pager_error() on the way out to make any error 
         3377  +  ** persistent.
         3378  +  */
         3379  +  return pager_error(pPager, rc);
  3373   3380   }
  3374   3381   
  3375   3382   /*
  3376   3383   ** Return TRUE if the database file is opened read-only.  Return FALSE
  3377   3384   ** if the database is (in theory) writable.
  3378   3385   */
  3379   3386   int sqlite3pager_isreadonly(Pager *pPager){

Changes to SQLite.Interop/src/pager.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite page cache
    13     13   ** subsystem.  The page cache subsystem reads and writes a file a page
    14     14   ** at a time and provides a journal for rollback.
    15     15   **
    16         -** @(#) $Id: pager.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           16  +** @(#) $Id: pager.h,v 1.17 2006/01/23 19:45:55 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #ifndef _PAGER_H_
    20     20   #define _PAGER_H_
    21     21   
    22     22   /*
    23     23   ** The default size of a database page.

Changes to SQLite.Interop/src/parse.c.

    19     19   };
    20     20   
    21     21   /*
    22     22   ** An instance of this structure is used to store the LIKE,
    23     23   ** GLOB, NOT LIKE, and NOT GLOB operators.
    24     24   */
    25     25   struct LikeOp {
    26         -  Token operator;  /* "like" or "glob" or "regexp" */
           26  +  Token eOperator;  /* "like" or "glob" or "regexp" */
    27     27     int not;         /* True if the NOT keyword is present */
    28     28   };
    29     29   
    30     30   /*
    31     31   ** An instance of the following structure describes the event of a
    32     32   ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
    33     33   ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
................................................................................
  2583   2583         case 197:
  2584   2584   #line 675 "parse.y"
  2585   2585   {yygotominor.yy178 = sqlite3Expr(yymsp[-1].major, yymsp[-2].minor.yy178, yymsp[0].minor.yy178, 0);}
  2586   2586   #line 2588 "parse.c"
  2587   2587           break;
  2588   2588         case 198:
  2589   2589   #line 685 "parse.y"
  2590         -{yygotominor.yy440.operator = yymsp[0].minor.yy0; yygotominor.yy440.not = 0;}
         2590  +{yygotominor.yy440.eOperator = yymsp[0].minor.yy0; yygotominor.yy440.not = 0;}
  2591   2591   #line 2593 "parse.c"
  2592   2592           break;
  2593   2593         case 199:
  2594   2594   #line 686 "parse.y"
  2595         -{yygotominor.yy440.operator = yymsp[0].minor.yy0; yygotominor.yy440.not = 1;}
         2595  +{yygotominor.yy440.eOperator = yymsp[0].minor.yy0; yygotominor.yy440.not = 1;}
  2596   2596   #line 2598 "parse.c"
  2597   2597           break;
  2598   2598         case 202:
  2599   2599   #line 691 "parse.y"
  2600   2600   {
  2601   2601     ExprList *pList = sqlite3ExprListAppend(0, yymsp[-1].minor.yy178, 0);
  2602   2602     pList = sqlite3ExprListAppend(pList, yymsp[-3].minor.yy178, 0);
  2603   2603     if( yymsp[0].minor.yy178 ){
  2604   2604       pList = sqlite3ExprListAppend(pList, yymsp[0].minor.yy178, 0);
  2605   2605     }
  2606         -  yygotominor.yy178 = sqlite3ExprFunction(pList, &yymsp[-2].minor.yy440.operator);
         2606  +  yygotominor.yy178 = sqlite3ExprFunction(pList, &yymsp[-2].minor.yy440.eOperator);
  2607   2607     if( yymsp[-2].minor.yy440.not ) yygotominor.yy178 = sqlite3Expr(TK_NOT, yygotominor.yy178, 0, 0);
  2608   2608     sqlite3ExprSpan(yygotominor.yy178, &yymsp[-3].minor.yy178->span, &yymsp[-1].minor.yy178->span);
  2609   2609   }
  2610   2610   #line 2612 "parse.c"
  2611   2611           break;
  2612   2612         case 203:
  2613   2613   #line 702 "parse.y"

Changes to SQLite.Interop/src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           14  +** $Id: pragma.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include "os.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /* Ignore this whole file if pragmas are disabled
    21     21   */

Changes to SQLite.Interop/src/prepare.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the implementation of the sqlite3_prepare()
    13     13   ** interface, and routines that contribute to loading the database schema
    14     14   ** from disk.
    15     15   **
    16         -** $Id: prepare.c,v 1.11 2006/01/16 15:51:47 rmsimpson Exp $
           16  +** $Id: prepare.c,v 1.12 2006/01/23 19:45:55 rmsimpson Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "os.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   /*
    23     23   ** Fill the InitData structure with an error message that indicates
    24     24   ** that the database is corrupt.
    25     25   */
    26     26   static void corruptSchema(InitData *pData, const char *zExtra){
    27         -  if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
           27  +  if( !sqlite3MallocFailed() ){
    28     28       sqlite3SetString(pData->pzErrMsg, "malformed database schema",
    29     29          zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
    30     30     }
    31     31   }
    32     32   
    33     33   /*
    34     34   ** This is the callback routine for the code that initializes the
................................................................................
    45     45   **
    46     46   */
    47     47   int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
    48     48     InitData *pData = (InitData*)pInit;
    49     49     sqlite3 *db = pData->db;
    50     50     int iDb;
    51     51   
    52         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
           52  +  if( sqlite3MallocFailed() ){
    53     53       return SQLITE_NOMEM;
    54     54     }
    55     55   
    56     56     assert( argc==4 );
    57     57     if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
    58     58     if( argv[1]==0 || argv[3]==0 ){
    59     59       corruptSchema(pData, 0);
................................................................................
    72     72       assert( db->init.busy );
    73     73       db->init.iDb = iDb;
    74     74       db->init.newTnum = atoi(argv[1]);
    75     75       rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
    76     76       db->init.iDb = 0;
    77     77       if( SQLITE_OK!=rc ){
    78     78         if( rc==SQLITE_NOMEM ){
    79         -          sqlite3ThreadData()->mallocFailed = 1;
           79  +        sqlite3FailedMalloc();
    80     80         }else{
    81         -          corruptSchema(pData, zErr);
           81  +        corruptSchema(pData, zErr);
    82     82         }
    83     83         sqlite3_free(zErr);
    84     84         return rc;
    85     85       }
    86     86     }else{
    87     87       /* If the SQL column is blank it means this is an index that
    88     88       ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
................................................................................
   299    299   #ifndef SQLITE_OMIT_ANALYZE
   300    300       if( rc==SQLITE_OK ){
   301    301         sqlite3AnalysisLoad(db, iDb);
   302    302       }
   303    303   #endif
   304    304       sqlite3BtreeCloseCursor(curMain);
   305    305     }
   306         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   307         -    sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
          306  +  if( sqlite3MallocFailed() ){
          307  +    /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
   308    308       rc = SQLITE_NOMEM;
   309    309       sqlite3ResetInternalSchema(db, 0);
   310    310     }
   311    311     if( rc==SQLITE_OK ){
   312    312       DbSetProperty(db, iDb, DB_SchemaLoaded);
   313    313     }else{
   314    314       sqlite3ResetInternalSchema(db, iDb);
................................................................................
   492    492   ){
   493    493     Parse sParse;
   494    494     char *zErrMsg = 0;
   495    495     int rc = SQLITE_OK;
   496    496     int i;
   497    497   
   498    498     /* Assert that malloc() has not failed */
   499         -  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
          499  +  assert( !sqlite3MallocFailed() );
   500    500   
   501    501     assert( ppStmt );
   502    502     *ppStmt = 0;
   503    503     if( sqlite3SafetyOn(db) ){
   504    504       return SQLITE_MISUSE;
   505    505     }
   506    506   
................................................................................
   515    515         sqlite3SafetyOff(db);
   516    516         return SQLITE_LOCKED;
   517    517       }
   518    518     }
   519    519     
   520    520     memset(&sParse, 0, sizeof(sParse));
   521    521     sParse.db = db;
   522         -  sParse.pTsd = sqlite3ThreadData();
   523         -  sParse.pTsd->nRef++;
   524    522     sqlite3RunParser(&sParse, zSql, &zErrMsg);
   525    523   
   526         -  if( sParse.pTsd->mallocFailed ){
          524  +  if( sqlite3MallocFailed() ){
   527    525       sParse.rc = SQLITE_NOMEM;
   528    526     }
   529    527     if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
   530    528     if( sParse.checkSchema && !schemaIsValid(db) ){
   531    529       sParse.rc = SQLITE_SCHEMA;
   532    530     }
   533    531     if( sParse.rc==SQLITE_SCHEMA ){
................................................................................
   566    564     if( zErrMsg ){
   567    565       sqlite3Error(db, rc, "%s", zErrMsg);
   568    566       sqliteFree(zErrMsg);
   569    567     }else{
   570    568       sqlite3Error(db, rc, 0);
   571    569     }
   572    570   
   573         -  /* We must check for malloc failure last of all, in case malloc() failed
   574         -  ** inside of the sqlite3Error() call above or something.
   575         -  */
   576         -  if( sParse.pTsd->mallocFailed ){
   577         -    rc = SQLITE_NOMEM;
   578         -    sqlite3Error(db, rc, 0);
   579         -  }
   580         -
   581         -  sParse.pTsd->nRef--;
   582         -  sqlite3MallocClearFailed();
          571  +  rc = sqlite3ApiExit(db, rc);
   583    572     sqlite3ReleaseThreadData();
   584    573     return rc;
   585    574   }
   586    575   
   587    576   #ifndef SQLITE_OMIT_UTF16
   588    577   /*
   589    578   ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
................................................................................
   595    584     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   596    585     const void **pzTail       /* OUT: End of parsed string */
   597    586   ){
   598    587     /* This function currently works by first transforming the UTF-16
   599    588     ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
   600    589     ** tricky bit is figuring out the pointer to return in *pzTail.
   601    590     */
   602         -  char *zSql8 = 0;
          591  +  char *zSql8;
   603    592     const char *zTail8 = 0;
   604         -  int rc;
          593  +  int rc = SQLITE_OK;
   605    594   
   606    595     if( sqlite3SafetyCheck(db) ){
   607    596       return SQLITE_MISUSE;
   608    597     }
   609    598     zSql8 = sqlite3utf16to8(zSql, nBytes);
   610         -  if( !zSql8 ){
   611         -    sqlite3Error(db, SQLITE_NOMEM, 0);
   612         -    return SQLITE_NOMEM;
          599  +  if( zSql8 ){
          600  +    rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
   613    601     }
   614         -  rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
   615    602   
   616    603     if( zTail8 && pzTail ){
   617    604       /* If sqlite3_prepare returns a tail pointer, we calculate the
   618    605       ** equivalent pointer into the UTF-16 string by counting the unicode
   619    606       ** characters between zSql8 and zTail8, and then returning a pointer
   620    607       ** the same number of characters into the UTF-16 string.
   621    608       */
   622    609       int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
   623    610       *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
   624    611     }
   625    612     sqliteFree(zSql8); 
   626         -  return rc;
          613  +  return sqlite3ApiExit(db, rc);
   627    614   }
   628    615   #endif /* SQLITE_OMIT_UTF16 */

Changes to SQLite.Interop/src/random.c.

    11     11   *************************************************************************
    12     12   ** This file contains code to implement a pseudo-random number
    13     13   ** generator (PRNG) for SQLite.
    14     14   **
    15     15   ** Random numbers are used by some of the database backends in order
    16     16   ** to generate random integer keys for tables or random filenames.
    17     17   **
    18         -** $Id: random.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           18  +** $Id: random.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   
    24     24   /*
    25     25   ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex

Changes to SQLite.Interop/src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: select.c,v 1.17 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
           19  +
           20  +/*
           21  +** Delete all the content of a Select structure but do not deallocate
           22  +** the select structure itself.
           23  +*/
           24  +void clearSelect(Select *p){
           25  +  sqlite3ExprListDelete(p->pEList);
           26  +  sqlite3SrcListDelete(p->pSrc);
           27  +  sqlite3ExprDelete(p->pWhere);
           28  +  sqlite3ExprListDelete(p->pGroupBy);
           29  +  sqlite3ExprDelete(p->pHaving);
           30  +  sqlite3ExprListDelete(p->pOrderBy);
           31  +  sqlite3SelectDelete(p->pPrior);
           32  +  sqlite3ExprDelete(p->pLimit);
           33  +  sqlite3ExprDelete(p->pOffset);
           34  +}
           35  +
    19     36   
    20     37   /*
    21     38   ** Allocate a new Select structure and return a pointer to that
    22     39   ** structure.
    23     40   */
    24     41   Select *sqlite3SelectNew(
    25     42     ExprList *pEList,     /* which columns to include in the result */
................................................................................
    29     46     Expr *pHaving,        /* the HAVING clause */
    30     47     ExprList *pOrderBy,   /* the ORDER BY clause */
    31     48     int isDistinct,       /* true if the DISTINCT keyword is present */
    32     49     Expr *pLimit,         /* LIMIT value.  NULL means not used */
    33     50     Expr *pOffset         /* OFFSET value.  NULL means no offset */
    34     51   ){
    35     52     Select *pNew;
           53  +  Select standin;
    36     54     pNew = sqliteMalloc( sizeof(*pNew) );
    37     55     assert( !pOffset || pLimit );   /* Can't have OFFSET without LIMIT. */
    38     56     if( pNew==0 ){
    39         -    sqlite3ExprListDelete(pEList);
    40         -    sqlite3SrcListDelete(pSrc);
    41         -    sqlite3ExprDelete(pWhere);
    42         -    sqlite3ExprListDelete(pGroupBy);
    43         -    sqlite3ExprDelete(pHaving);
    44         -    sqlite3ExprListDelete(pOrderBy);
    45         -    sqlite3ExprDelete(pLimit);
    46         -    sqlite3ExprDelete(pOffset);
    47         -  }else{
    48         -    if( pEList==0 ){
    49         -      pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0);
    50         -    }
    51         -    pNew->pEList = pEList;
    52         -    pNew->pSrc = pSrc;
    53         -    pNew->pWhere = pWhere;
    54         -    pNew->pGroupBy = pGroupBy;
    55         -    pNew->pHaving = pHaving;
    56         -    pNew->pOrderBy = pOrderBy;
    57         -    pNew->isDistinct = isDistinct;
    58         -    pNew->op = TK_SELECT;
    59         -    pNew->pLimit = pLimit;
    60         -    pNew->pOffset = pOffset;
    61         -    pNew->iLimit = -1;
    62         -    pNew->iOffset = -1;
    63         -    pNew->addrOpenVirt[0] = -1;
    64         -    pNew->addrOpenVirt[1] = -1;
    65         -    pNew->addrOpenVirt[2] = -1;
           57  +    pNew = &standin;
           58  +    memset(pNew, 0, sizeof(*pNew));
           59  +  }
           60  +  if( pEList==0 ){
           61  +    pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0);
           62  +  }
           63  +  pNew->pEList = pEList;
           64  +  pNew->pSrc = pSrc;
           65  +  pNew->pWhere = pWhere;
           66  +  pNew->pGroupBy = pGroupBy;
           67  +  pNew->pHaving = pHaving;
           68  +  pNew->pOrderBy = pOrderBy;
           69  +  pNew->isDistinct = isDistinct;
           70  +  pNew->op = TK_SELECT;
           71  +  pNew->pLimit = pLimit;
           72  +  pNew->pOffset = pOffset;
           73  +  pNew->iLimit = -1;
           74  +  pNew->iOffset = -1;
           75  +  pNew->addrOpenVirt[0] = -1;
           76  +  pNew->addrOpenVirt[1] = -1;
           77  +  pNew->addrOpenVirt[2] = -1;
           78  +  if( pNew==&standin) {
           79  +    clearSelect(pNew);
           80  +    pNew = 0;
    66     81     }
    67     82     return pNew;
    68     83   }
           84  +
           85  +/*
           86  +** Delete the given Select structure and all of its substructures.
           87  +*/
           88  +void sqlite3SelectDelete(Select *p){
           89  +  if( p ){
           90  +    clearSelect(p);
           91  +    sqliteFree(p);
           92  +  }
           93  +}
    69     94   
    70     95   /*
    71     96   ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
    72     97   ** type of join.  Return an integer constant that expresses that type
    73     98   ** in terms of the following bit values:
    74     99   **
    75    100   **     JT_INNER
................................................................................
   326    351                               pRight->iCursor, &p->pWhere);
   327    352         }
   328    353       }
   329    354     }
   330    355     return 0;
   331    356   }
   332    357   
   333         -/*
   334         -** Delete the given Select structure and all of its substructures.
   335         -*/
   336         -void sqlite3SelectDelete(Select *p){
   337         -  if( p==0 ) return;
   338         -  sqlite3ExprListDelete(p->pEList);
   339         -  sqlite3SrcListDelete(p->pSrc);
   340         -  sqlite3ExprDelete(p->pWhere);
   341         -  sqlite3ExprListDelete(p->pGroupBy);
   342         -  sqlite3ExprDelete(p->pHaving);
   343         -  sqlite3ExprListDelete(p->pOrderBy);
   344         -  sqlite3SelectDelete(p->pPrior);
   345         -  sqlite3ExprDelete(p->pLimit);
   346         -  sqlite3ExprDelete(p->pOffset);
   347         -  sqliteFree(p);
   348         -}
   349         -
   350    358   /*
   351    359   ** Insert code into "v" that will push the record on the top of the
   352    360   ** stack into the sorter.
   353    361   */
   354    362   static void pushOntoSorter(
   355    363     Parse *pParse,         /* Parser context */
   356    364     ExprList *pOrderBy,    /* The ORDER BY clause */
................................................................................
   454    462   
   455    463     if( v==0 ) return 0;
   456    464     assert( pEList!=0 );
   457    465   
   458    466     /* If there was a LIMIT clause on the SELECT statement, then do the check
   459    467     ** to see if this row should be output.
   460    468     */
   461         -  hasDistinct = distinct>=0 && pEList && pEList->nExpr>0;
          469  +  hasDistinct = distinct>=0 && pEList->nExpr>0;
   462    470     if( pOrderBy==0 && !hasDistinct ){
   463    471       codeOffset(v, p, iContinue, 0);
   464    472     }
   465    473   
   466    474     /* Pull the requested columns.
   467    475     */
   468    476     if( nColumn>0 ){
................................................................................
   554    562           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &aff, 1);
   555    563           sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
   556    564         }
   557    565         sqlite3VdbeJumpHere(v, addr2);
   558    566         break;
   559    567       }
   560    568   
   561         -    /* If any row exists in the result set, record that fact and abort.
          569  +    /* If any row exist in the result set, record that fact and abort.
   562    570       */
   563    571       case SRT_Exists: {
   564    572         sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm);
   565    573         sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
   566    574         /* The LIMIT clause will terminate the loop for us */
   567    575         break;
   568    576       }
................................................................................
   868    876     /* If this is an EXPLAIN, skip this step */
   869    877     if( pParse->explain ){
   870    878       return;
   871    879     }
   872    880   #endif
   873    881   
   874    882     assert( v!=0 );
   875         -  if( pParse->colNamesSet || v==0
   876         -     || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
          883  +  if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return;
   877    884     pParse->colNamesSet = 1;
   878    885     fullNames = (db->flags & SQLITE_FullColNames)!=0;
   879    886     shortNames = (db->flags & SQLITE_ShortColNames)!=0;
   880    887     sqlite3VdbeSetNumCols(v, pEList->nExpr);
   881    888     for(i=0; i<pEList->nExpr; i++){
   882    889       Expr *p;
   883    890       p = pEList->a[i].pExpr;
................................................................................
   998   1005         /* Use the original text of the column expression as its name */
   999   1006         zName = sqlite3MPrintf("%T", &p->span);
  1000   1007       }else{
  1001   1008         /* If all else fails, make up a name */
  1002   1009         zName = sqlite3MPrintf("column%d", i+1);
  1003   1010       }
  1004   1011       sqlite3Dequote(zName);
  1005         -    if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1012  +    if( sqlite3MallocFailed() ){
  1006   1013         sqliteFree(zName);
  1007   1014         sqlite3DeleteTable(0, pTab);
  1008   1015         return 0;
  1009   1016       }
  1010   1017   
  1011   1018       /* Make sure the column name is unique.  If the name is not unique,
  1012   1019       ** append a integer to the name so that it becomes unique.
................................................................................
  1070   1077   static int prepSelectStmt(Parse *pParse, Select *p){
  1071   1078     int i, j, k, rc;
  1072   1079     SrcList *pTabList;
  1073   1080     ExprList *pEList;
  1074   1081     Table *pTab;
  1075   1082     struct SrcList_item *pFrom;
  1076   1083   
  1077         -  if( p==0 || p->pSrc==0 || sqlite3ThreadDataReadOnly()->mallocFailed ){
         1084  +  if( p==0 || p->pSrc==0 || sqlite3MallocFailed() ){
  1078   1085       return 1;
  1079   1086     }
  1080   1087     pTabList = p->pSrc;
  1081   1088     pEList = p->pEList;
  1082   1089   
  1083   1090     /* Make sure cursor numbers have been assigned to all entries in
  1084   1091     ** the FROM clause of the SELECT statement.
................................................................................
  1385   1392   ** (usually but not always -1) prior to calling this routine.
  1386   1393   ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
  1387   1394   ** redefined.  The UNION ALL operator uses this property to force
  1388   1395   ** the reuse of the same limit and offset registers across multiple
  1389   1396   ** SELECT statements.
  1390   1397   */
  1391   1398   static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
  1392         -  Vdbe *v;
  1393         -  int iLimit;
         1399  +  Vdbe *v = 0;
         1400  +  int iLimit = 0;
  1394   1401     int iOffset;
  1395   1402     int addr1, addr2;
  1396   1403   
  1397   1404     /* 
  1398   1405     ** "LIMIT -1" always shows all rows.  There is some
  1399   1406     ** contraversy about what the correct behavior should be.
  1400   1407     ** The current implementation interprets "LIMIT 0" to mean
................................................................................
  1858   1865   
  1859   1866       if( pOrderBy ){
  1860   1867         struct ExprList_item *pOTerm = pOrderBy->a;
  1861   1868         int nOrderByExpr = pOrderBy->nExpr;
  1862   1869         int addr;
  1863   1870         u8 *pSortOrder;
  1864   1871   
  1865         -      aCopy = (CollSeq**)&pKeyInfo[1];
         1872  +      aCopy = &pKeyInfo->aColl[nCol];
  1866   1873         pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol];
  1867   1874         memcpy(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*));
  1868   1875         apColl = pKeyInfo->aColl;
  1869   1876         for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){
  1870   1877           Expr *pExpr = pOTerm->pExpr;
  1871   1878           char *zName = pOTerm->zName;
  1872   1879           assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol );
................................................................................
  2016   2023   **  (10)  The subquery does not use aggregates or the outer query does not
  2017   2024   **        use LIMIT.
  2018   2025   **
  2019   2026   **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
  2020   2027   **
  2021   2028   **  (12)  The subquery is not the right term of a LEFT OUTER JOIN or the
  2022   2029   **        subquery has no WHERE clause.  (added by ticket #350)
         2030  +**
         2031  +**  (13)  The subquery and outer query do not both use LIMIT
         2032  +**
         2033  +**  (14)  The subquery does not use OFFSET
  2023   2034   **
  2024   2035   ** In this routine, the "p" parameter is a pointer to the outer query.
  2025   2036   ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
  2026   2037   ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
  2027   2038   **
  2028   2039   ** If flattening is not attempted, this routine is a no-op and returns 0.
  2029   2040   ** If flattening is attempted this routine returns 1.
................................................................................
  2051   2062     */
  2052   2063     if( p==0 ) return 0;
  2053   2064     pSrc = p->pSrc;
  2054   2065     assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
  2055   2066     pSubitem = &pSrc->a[iFrom];
  2056   2067     pSub = pSubitem->pSelect;
  2057   2068     assert( pSub!=0 );
  2058         -  if( isAgg && subqueryIsAgg ) return 0;
  2059         -  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
         2069  +  if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
         2070  +  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
  2060   2071     pSubSrc = pSub->pSrc;
  2061   2072     assert( pSubSrc );
  2062         -  if( (pSub->pLimit && p->pLimit) || pSub->pOffset || 
  2063         -      (pSub->pLimit && isAgg) ) return 0;
  2064         -  if( pSubSrc->nSrc==0 ) return 0;
  2065         -  if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){
  2066         -     return 0;
         2073  +  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
         2074  +  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
         2075  +  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
         2076  +  ** became arbitrary expressions, we were forced to add restrictions (13)
         2077  +  ** and (14). */
         2078  +  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
         2079  +  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
         2080  +  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
         2081  +  if( (pSub->isDistinct || pSub->pLimit) 
         2082  +         && (pSrc->nSrc>1 || isAgg) ){          /* Restrictions (4)(5)(8)(9) */
         2083  +     return 0;       
  2067   2084     }
  2068         -  if( p->isDistinct && subqueryIsAgg ) return 0;
  2069         -  if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ) return 0;
         2085  +  if( p->isDistinct && subqueryIsAgg ) return 0;         /* Restriction (6)  */
         2086  +  if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){
         2087  +     return 0;                                           /* Restriction (11) */
         2088  +  }
  2070   2089   
  2071   2090     /* Restriction 3:  If the subquery is a join, make sure the subquery is 
  2072   2091     ** not used as the right operand of an outer join.  Examples of why this
  2073   2092     ** is not allowed:
  2074   2093     **
  2075   2094     **         t1 LEFT OUTER JOIN (t2 JOIN t3)
  2076   2095     **
................................................................................
  2192   2211     /* The flattened query is distinct if either the inner or the
  2193   2212     ** outer query is distinct. 
  2194   2213     */
  2195   2214     p->isDistinct = p->isDistinct || pSub->isDistinct;
  2196   2215   
  2197   2216     /*
  2198   2217     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
         2218  +  **
         2219  +  ** One is tempted to try to add a and b to combine the limits.  But this
         2220  +  ** does not work if either limit is negative.
  2199   2221     */
  2200   2222     if( pSub->pLimit ){
  2201   2223       p->pLimit = pSub->pLimit;
  2202   2224       pSub->pLimit = 0;
  2203   2225     }
  2204   2226   
  2205   2227     /* Finially, delete what is left of the subquery and return
................................................................................
  2689   2711     int isDistinct;        /* True if the DISTINCT keyword is present */
  2690   2712     int distinct;          /* Table to use for the distinct set */
  2691   2713     int rc = 1;            /* Value to return from this function */
  2692   2714     int addrSortIndex;     /* Address of an OP_OpenVirtual instruction */
  2693   2715     AggInfo sAggInfo;      /* Information used by aggregate queries */
  2694   2716     int iEnd;              /* Address of the end of the query */
  2695   2717   
  2696         -  if( p==0 || sqlite3ThreadDataReadOnly()->mallocFailed || pParse->nErr ){
         2718  +  if( p==0 || sqlite3MallocFailed() || pParse->nErr ){
  2697   2719       return 1;
  2698   2720     }
  2699   2721     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  2700   2722     memset(&sAggInfo, 0, sizeof(sAggInfo));
  2701   2723   
  2702   2724   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  2703   2725     /* If there is are a sequence of queries, do the earlier ones first.
................................................................................
  2945   2967       }
  2946   2968       sAggInfo.nAccumulator = sAggInfo.nColumn;
  2947   2969       for(i=0; i<sAggInfo.nFunc; i++){
  2948   2970         if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
  2949   2971           goto select_end;
  2950   2972         }
  2951   2973       }
  2952         -    if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto select_end;
         2974  +    if( sqlite3MallocFailed() ) goto select_end;
  2953   2975   
  2954   2976       /* Processing for aggregates with GROUP BY is very different and
  2955   2977       ** much more complex tha aggregates without a GROUP BY.
  2956   2978       */
  2957   2979       if( pGroupBy ){
  2958   2980         KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
  2959   2981   

Changes to SQLite.Interop/src/shell.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code to implement the "sqlite" command line
    13     13   ** utility for accessing SQLite databases.
    14     14   **
    15         -** $Id: shell.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: shell.c,v 1.16 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #include <stdlib.h>
    18     18   #include <string.h>
    19     19   #include <stdio.h>
    20     20   #include <assert.h>
    21     21   #include "sqlite3.h"
    22     22   #include <ctype.h>

Changes to SQLite.Interop/src/sqlite3.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite3.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** @(#) $Id: sqlite3.h,v 1.17 2006/01/23 19:45:55 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE3_H_
    18     18   #define _SQLITE3_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
    27     27   
    28     28   /*
    29     29   ** The version of the SQLite library.
    30     30   */
    31     31   #ifdef SQLITE_VERSION
    32     32   # undef SQLITE_VERSION
    33     33   #endif
    34         -#define SQLITE_VERSION         "3.3.1"
           34  +#define SQLITE_VERSION         "3.3.2"
    35     35   
    36     36   /*
    37     37   ** The format of the version string is "X.Y.Z<trailing string>", where
    38     38   ** X is the major version number, Y is the minor version number and Z
    39     39   ** is the release number. The trailing string is often "alpha" or "beta".
    40     40   ** For example "3.1.1beta".
    41     41   **
................................................................................
    44     44   ** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using 
    45     45   ** version 3.1.1 or greater at compile time, programs may use the test 
    46     46   ** (SQLITE_VERSION_NUMBER>=3001001).
    47     47   */
    48     48   #ifdef SQLITE_VERSION_NUMBER
    49     49   # undef SQLITE_VERSION_NUMBER
    50     50   #endif
    51         -#define SQLITE_VERSION_NUMBER 3003001
           51  +#define SQLITE_VERSION_NUMBER 3003002
    52     52   
    53     53   /*
    54     54   ** The version string is also compiled into the library so that a program
    55     55   ** can check to make sure that the lib*.a file and the *.h file are from
    56     56   ** the same version.  The sqlite3_libversion() function returns a pointer
    57     57   ** to the sqlite3_version variable - useful in DLLs which cannot access
    58     58   ** global variables.

Changes to SQLite.Interop/src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.16 2006/01/23 19:45:56 rmsimpson Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Extra interface definitions for those who need them
    21     21   */
................................................................................
    26     26   /*
    27     27   ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
    28     28   ** Setting NDEBUG makes the code smaller and run faster.  So the following
    29     29   ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
    30     30   ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
    31     31   ** feature.
    32     32   */
    33         -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
           33  +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
    34     34   # define NDEBUG 1
    35     35   #endif
    36     36   
    37     37   /*
    38     38   ** These #defines should enable >2GB file support on Posix if the
    39     39   ** underlying operating system supports it.  If the OS lacks
    40     40   ** large file support, or if the OS is windows, these should be no-ops.
................................................................................
    69     69   /*
    70     70   ** If compiling for a processor that lacks floating point support,
    71     71   ** substitute integer for floating-point
    72     72   */
    73     73   #ifdef SQLITE_OMIT_FLOATING_POINT
    74     74   # define double sqlite_int64
    75     75   # define LONGDOUBLE_TYPE sqlite_int64
    76         -# define SQLITE_BIG_DBL (0x7fffffffffffffff)
           76  +# ifndef SQLITE_BIG_DBL
           77  +#   define SQLITE_BIG_DBL (0x7fffffffffffffff)
           78  +# endif
    77     79   # define SQLITE_OMIT_DATETIME_FUNCS 1
    78     80   # define SQLITE_OMIT_TRACE 1
    79     81   #endif
           82  +#ifndef SQLITE_BIG_DBL
           83  +# define SQLITE_BIG_DBL (1e99)
           84  +#endif
    80     85   
    81     86   /*
    82     87   ** The maximum number of in-memory pages to use for the main database
    83     88   ** table and for temporary tables. Internally, the MAX_PAGES and 
    84     89   ** TEMP_PAGES macros are used. To override the default values at
    85     90   ** compilation time, the SQLITE_DEFAULT_CACHE_SIZE and 
    86     91   ** SQLITE_DEFAULT_TEMP_CACHE_SIZE macros should be set.
................................................................................
   257    262   ** The following global variables are used for testing and debugging
   258    263   ** only.  They only work if SQLITE_MEMDEBUG is defined.
   259    264   */
   260    265   extern int sqlite3_nMalloc;      /* Number of sqliteMalloc() calls */
   261    266   extern int sqlite3_nFree;        /* Number of sqliteFree() calls */
   262    267   extern int sqlite3_iMallocFail;  /* Fail sqliteMalloc() after this many calls */
   263    268   extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */
   264         -#define ENTER_MALLOC (\
   265         -  sqlite3ThreadData()->zFile = __FILE__, sqlite3ThreadData()->iLine = __LINE__ \
   266         -)
          269  +
          270  +
          271  +extern void *sqlite3_pFirst;         /* Pointer to linked list of allocations */
          272  +extern int sqlite3_nMaxAlloc;        /* High water mark of ThreadData.nAlloc */
          273  +extern int sqlite3_mallocDisallowed; /* assert() in sqlite3Malloc() if set */
          274  +extern int sqlite3_isFail;           /* True if all malloc calls should fail */
          275  +extern const char *sqlite3_zFile;    /* Filename to associate debug info with */
          276  +extern int sqlite3_iLine;            /* Line number for debug info */
          277  +
          278  +#define ENTER_MALLOC (sqlite3_zFile = __FILE__, sqlite3_iLine = __LINE__)
   267    279   #define sqliteMalloc(x)          (ENTER_MALLOC, sqlite3Malloc(x))
   268    280   #define sqliteMallocRaw(x)       (ENTER_MALLOC, sqlite3MallocRaw(x))
   269    281   #define sqliteRealloc(x,y)       (ENTER_MALLOC, sqlite3Realloc(x,y))
   270    282   #define sqliteStrDup(x)          (ENTER_MALLOC, sqlite3StrDup(x))
   271    283   #define sqliteStrNDup(x,y)       (ENTER_MALLOC, sqlite3StrNDup(x,y))
   272    284   #define sqliteReallocOrFree(x,y) (ENTER_MALLOC, sqlite3ReallocOrFree(x,y))
   273    285   
................................................................................
   285    297   #define sqliteFree(x)          sqlite3FreeX(x)
   286    298   #define sqliteAllocSize(x)     sqlite3AllocSize(x)
   287    299   
   288    300   
   289    301   /*
   290    302   ** An instance of this structure might be allocated to store information
   291    303   ** specific to a single thread.
   292         -**
   293         -** To avoid a memory leak on windows, the content of this structure is
   294         -** checked at the conclusion of each API call.  If it is all zero, it
   295         -** is deallocated.
   296    304   */
   297    305   struct ThreadData {
   298         -  int mallocFailed;        /* True after a malloc() has failed */
   299         -  int nRef;                /* Number of users */
          306  +  int dummy;               /* So that this structure is never empty */
   300    307   
   301    308   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   302    309     int nSoftHeapLimit;      /* Suggested max mem allocation.  No limit if <0 */
   303    310     int nAlloc;              /* Number of bytes currently allocated */
   304    311     Pager *pPager;           /* Linked list of all pagers in this thread */
   305    312   #endif
   306    313   
   307    314   #ifndef SQLITE_OMIT_SHARED_CACHE
   308    315     u8 useSharedData;        /* True if shared pagers and schemas are enabled */
   309    316     BtShared *pBtree;        /* Linked list of all currently open BTrees */
   310    317   #endif
   311         -
   312         -#ifdef SQLITE_MEMDEBUG
   313         -  int nMaxAlloc;           /* High water mark of ThreadData.nAlloc */
   314         -  int mallocDisallowed;    /* assert() in sqlite3Malloc() if set */
   315         -  int isFail;              /* True if all malloc() calls should fail */
   316         -  const char *zFile;       /* Filename to associate debugging info with */
   317         -  int iLine;               /* Line number to associate debugging info with */
   318         -  void *pFirst;            /* Pointer to linked list of allocations */
   319         -#endif
   320    318   };
   321    319   
   322    320   /*
   323    321   ** Name of the master database table.  The master database table
   324    322   ** is a special table that holds the names and attributes of all
   325    323   ** user tables and indices.
   326    324   */
................................................................................
   696    694     int nCol;        /* Number of columns in this table */
   697    695     Column *aCol;    /* Information about each column */
   698    696     int iPKey;       /* If not less then 0, use aCol[iPKey] as the primary key */
   699    697     Index *pIndex;   /* List of SQL indexes on this table. */
   700    698     int tnum;        /* Root BTree node for this table (see note above) */
   701    699     Select *pSelect; /* NULL for tables.  Points to definition if a view. */
   702    700     u8 readOnly;     /* True if this table should not be written by the user */
   703         -// u8 iDb;          /* Index into sqlite.aDb[] of the backend for this table */
   704    701     u8 isTransient;  /* True if automatically deleted when VDBE finishes */
   705    702     u8 hasPrimKey;   /* True if there exists a primary key */
   706    703     u8 keyConf;      /* What to do in case of uniqueness conflict on iPKey */
   707    704     u8 autoInc;      /* True if the integer primary key is autoincrement */
   708    705     int nRef;          /* Number of pointers to this Table */
   709    706     Trigger *pTrigger; /* List of SQL triggers on this table */
   710    707     FKey *pFKey;       /* Linked list of all foreign keys in this table */
................................................................................
   960    957   ** If the Expr is of type OP_Column, and the table it is selecting from
   961    958   ** is a disk table or the "old.*" pseudo-table, then pTab points to the
   962    959   ** corresponding table definition.
   963    960   */
   964    961   struct Expr {
   965    962     u8 op;                 /* Operation performed by this node */
   966    963     char affinity;         /* The affinity of the column or 0 if not a column */
   967         -//u8 iDb;                /* Database referenced by this expression */
   968    964     u8 flags;              /* Various flags.  See below */
   969    965     CollSeq *pColl;        /* The collation type of the column or 0 */
   970    966     Expr *pLeft, *pRight;  /* Left and right subnodes */
   971    967     ExprList *pList;       /* A list of expressions used as function arguments
   972    968                            ** or in "<expr> IN (<expr-list)" */
   973    969     Token token;           /* An operand token */
   974    970     Token span;            /* Complete text of the expression */
................................................................................
  1254   1250     int nErr;            /* Number of errors seen */
  1255   1251     int nTab;            /* Number of previously allocated VDBE cursors */
  1256   1252     int nMem;            /* Number of memory cells used so far */
  1257   1253     int nSet;            /* Number of sets used so far */
  1258   1254     int ckOffset;        /* Stack offset to data used by CHECK constraints */
  1259   1255     u32 writeMask;       /* Start a write transaction on these databases */
  1260   1256     u32 cookieMask;      /* Bitmask of schema verified databases */
  1261         -  ThreadData *pTsd;    /* Thread specific data for this thread */
  1262   1257     int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  1263   1258     int cookieValue[MAX_ATTACHED+2];  /* Values of cookies to verify */
  1264   1259   #ifndef SQLITE_OMIT_SHARED_CACHE
  1265   1260     int nTableLock;        /* Number of locks in aTableLock */
  1266   1261     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  1267   1262   #endif
  1268   1263   
................................................................................
  1742   1737   void sqlite3ReleaseThreadData(void);
  1743   1738   void sqlite3AttachFunctions(sqlite3 *);
  1744   1739   void sqlite3MinimumFileFormat(Parse*, int, int);
  1745   1740   void sqlite3SchemaFree(void *);
  1746   1741   Schema *sqlite3SchemaGet(Btree *);
  1747   1742   int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  1748   1743   KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
         1744  +int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
         1745  +  void (*)(sqlite3_context*,int,sqlite3_value **),
         1746  +  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*));
         1747  +int sqlite3ApiExit(sqlite3 *db, int);
         1748  +int sqlite3MallocFailed(void);
         1749  +void sqlite3FailedMalloc(void);
  1749   1750   
  1750   1751   #ifndef SQLITE_OMIT_SHARED_CACHE
  1751   1752     void sqlite3TableLock(Parse *, int, int, u8, const char *);
  1752   1753   #else
  1753   1754     #define sqlite3TableLock(v,w,x,y,z)
  1754   1755   #endif
  1755   1756   
  1756         -void sqlite3MallocClearFailed();
  1757         -#ifndef SQLITE_MEMDEBUG
         1757  +#ifdef SQLITE_MEMDEBUG
         1758  +  void sqlite3MallocDisallow(void);
         1759  +  void sqlite3MallocAllow(void);
         1760  +  int sqlite3TestMallocFail(void);
         1761  +#else
         1762  +  #define sqlite3TestMallocFail() 0
  1758   1763     #define sqlite3MallocDisallow()
  1759   1764     #define sqlite3MallocAllow()
  1760         -#else
  1761         -  void sqlite3MallocDisallow();
  1762         -  void sqlite3MallocAllow();
  1763   1765   #endif
  1764   1766   
  1765   1767   #ifdef SQLITE_SSE
  1766   1768   #include "sseInt.h"
  1767   1769   #endif
  1768   1770   
  1769   1771   #endif

Changes to SQLite.Interop/src/tclsqlite.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** A TCL Interface to SQLite
    13     13   **
    14         -** $Id: tclsqlite.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           14  +** $Id: tclsqlite.c,v 1.16 2006/01/23 19:45:56 rmsimpson Exp $
    15     15   */
    16     16   #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "hash.h"
    20     20   #include "tcl.h"
    21     21   #include <stdlib.h>
................................................................................
   252    252     rc = Tcl_Eval(pDb->interp, pDb->zProgress);
   253    253     if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
   254    254       return 1;
   255    255     }
   256    256     return 0;
   257    257   }
   258    258   
          259  +#ifndef SQLITE_OMIT_TRACE
   259    260   /*
   260    261   ** This routine is called by the SQLite trace handler whenever a new
   261    262   ** block of SQL is executed.  The TCL script in pDb->zTrace is executed.
   262    263   */
   263    264   static void DbTraceHandler(void *cd, const char *zSql){
   264    265     SqliteDb *pDb = (SqliteDb*)cd;
   265    266     Tcl_DString str;
................................................................................
   267    268     Tcl_DStringInit(&str);
   268    269     Tcl_DStringAppend(&str, pDb->zTrace, -1);
   269    270     Tcl_DStringAppendElement(&str, zSql);
   270    271     Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
   271    272     Tcl_DStringFree(&str);
   272    273     Tcl_ResetResult(pDb->interp);
   273    274   }
          275  +#endif
   274    276   
          277  +#ifndef SQLITE_OMIT_TRACE
   275    278   /*
   276    279   ** This routine is called by the SQLite profile handler after a statement
   277    280   ** SQL has executed.  The TCL script in pDb->zProfile is evaluated.
   278    281   */
   279    282   static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
   280    283     SqliteDb *pDb = (SqliteDb*)cd;
   281    284     Tcl_DString str;
................................................................................
   286    289     Tcl_DStringAppend(&str, pDb->zProfile, -1);
   287    290     Tcl_DStringAppendElement(&str, zSql);
   288    291     Tcl_DStringAppendElement(&str, zTm);
   289    292     Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
   290    293     Tcl_DStringFree(&str);
   291    294     Tcl_ResetResult(pDb->interp);
   292    295   }
          296  +#endif
   293    297   
   294    298   /*
   295    299   ** This routine is called when a transaction is committed.  The
   296    300   ** TCL script in pDb->zCommit is executed.  If it returns non-zero or
   297    301   ** if it throws an exception, the transaction is rolled back instead
   298    302   ** of being committed.
   299    303   */

Changes to SQLite.Interop/src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.19 2006/01/16 15:51:47 rmsimpson Exp $
           18  +** $Id: tokenize.c,v 1.20 2006/01/23 19:45:56 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   #include <stdlib.h>
    24     24   
    25     25   /*
................................................................................
   336    336   int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
   337    337     int nErr = 0;
   338    338     int i;
   339    339     void *pEngine;
   340    340     int tokenType;
   341    341     int lastTokenParsed = -1;
   342    342     sqlite3 *db = pParse->db;
   343         -  ThreadData *pTsd = pParse->pTsd;
   344    343     extern void *sqlite3ParserAlloc(void*(*)(int));
   345    344     extern void sqlite3ParserFree(void*, void(*)(void*));
   346    345     extern int sqlite3Parser(void*, int, Token, Parse*);
   347    346   
   348    347     db->flags &= ~SQLITE_Interrupt;
   349    348     pParse->rc = SQLITE_OK;
   350    349     i = 0;
................................................................................
   356    355     assert( pParse->pNewTable==0 );
   357    356     assert( pParse->pNewTrigger==0 );
   358    357     assert( pParse->nVar==0 );
   359    358     assert( pParse->nVarExpr==0 );
   360    359     assert( pParse->nVarExprAlloc==0 );
   361    360     assert( pParse->apVarExpr==0 );
   362    361     pParse->zTail = pParse->zSql = zSql;
   363         -  while( pTsd->mallocFailed==0 && zSql[i]!=0 ){
          362  +  while( !sqlite3MallocFailed() && zSql[i]!=0 ){
   364    363       assert( i>=0 );
   365    364       pParse->sLastToken.z = (u8*)&zSql[i];
   366    365       assert( pParse->sLastToken.dyn==0 );
   367    366       pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
   368    367       i += pParse->sLastToken.n;
   369    368       switch( tokenType ){
   370    369         case TK_SPACE:
................................................................................
   404    403       if( lastTokenParsed!=TK_SEMI ){
   405    404         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
   406    405         pParse->zTail = &zSql[i];
   407    406       }
   408    407       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   409    408     }
   410    409     sqlite3ParserFree(pEngine, sqlite3FreeX);
   411         -  if( pTsd->mallocFailed ){
          410  +  if( sqlite3MallocFailed() ){
   412    411       pParse->rc = SQLITE_NOMEM;
   413    412     }
   414    413     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
   415    414       sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
   416    415     }
   417    416     if( pParse->zErrMsg ){
   418    417       if( pzErrMsg && *pzErrMsg==0 ){

Changes to SQLite.Interop/src/trigger.c.

    77     77     }
    78     78   
    79     79     /* If the trigger name was unqualified, and the table is a temp table,
    80     80     ** then set iDb to 1 to create the trigger in the temporary database.
    81     81     ** If sqlite3SrcListLookup() returns 0, indicating the table does not
    82     82     ** exist, the error is caught by the block below.
    83     83     */
    84         -  if( !pTableName || sqlite3ThreadDataReadOnly()->mallocFailed ){
           84  +  if( !pTableName || sqlite3MallocFailed() ){
    85     85       goto trigger_cleanup;
    86     86     }
    87     87     pTab = sqlite3SrcListLookup(pParse, pTableName);
    88     88     if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
    89     89       iDb = 1;
    90     90     }
    91     91   
    92     92     /* Ensure the table name matches database name and that the table exists */
    93         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto trigger_cleanup;
           93  +  if( sqlite3MallocFailed() ) goto trigger_cleanup;
    94     94     assert( pTableName->nSrc==1 );
    95     95     if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
    96     96         sqlite3FixSrcList(&sFix, pTableName) ){
    97     97       goto trigger_cleanup;
    98     98     }
    99     99     pTab = sqlite3SrcListLookup(pParse, pTableName);
   100    100     if( !pTab ){
................................................................................
   253    253     if( db->init.busy ){
   254    254       int n;
   255    255       Table *pTab;
   256    256       Trigger *pDel;
   257    257       pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash, 
   258    258                        pTrig->name, strlen(pTrig->name)+1, pTrig);
   259    259       if( pDel ){
   260         -      assert( sqlite3ThreadDataReadOnly()->mallocFailed && pDel==pTrig );
          260  +      assert( sqlite3MallocFailed() && pDel==pTrig );
   261    261         goto triggerfinish_cleanup;
   262    262       }
   263    263       n = strlen(pTrig->table) + 1;
   264    264       pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
   265    265       assert( pTab!=0 );
   266    266       pTrig->pNext = pTab->pTrigger;
   267    267       pTab->pTrigger = pTrig;
................................................................................
   437    437     Trigger *pTrigger = 0;
   438    438     int i;
   439    439     const char *zDb;
   440    440     const char *zName;
   441    441     int nName;
   442    442     sqlite3 *db = pParse->db;
   443    443   
   444         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto drop_trigger_cleanup;
          444  +  if( sqlite3MallocFailed() ) goto drop_trigger_cleanup;
   445    445     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   446    446       goto drop_trigger_cleanup;
   447    447     }
   448    448   
   449    449     assert( pName->nSrc==1 );
   450    450     zDb = pName->a[0].zDatabase;
   451    451     zName = pName->a[0].zName;

Changes to SQLite.Interop/src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: update.c,v 1.16 2006/01/23 19:45:56 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The most recently coded instruction was an OP_Column to retrieve the
    21     21   ** i-th column of table pTab. This routine sets the P3 parameter of the 
    22     22   ** OP_Column to the default value, if any.
................................................................................
    96     96     int triggers_exist = 0;      /* True if any row triggers exist */
    97     97   #endif
    98     98   
    99     99     int newIdx      = -1;  /* index of trigger "new" temp table       */
   100    100     int oldIdx      = -1;  /* index of trigger "old" temp table       */
   101    101   
   102    102     sContext.pParse = 0;
   103         -  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          103  +  if( pParse->nErr || sqlite3MallocFailed() ){
   104    104       goto update_cleanup;
   105    105     }
   106    106     db = pParse->db;
   107    107     assert( pTabList->nSrc==1 );
   108    108   
   109    109     /* Locate the table which we want to update. 
   110    110     */

Changes to SQLite.Interop/src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           15  +** $Id: utf.c,v 1.16 2006/01/23 19:45:56 rmsimpson Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx

Changes to SQLite.Interop/src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           17  +** $Id: util.c,v 1.17 2006/01/23 19:45:56 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <stdarg.h>
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
    25     25   ** MALLOC WRAPPER ARCHITECTURE
    26     26   **
    27     27   ** The sqlite code accesses dynamic memory allocation/deallocation by invoking
    28         -** the following four APIs (which may be implemented as macros).
           28  +** the following six APIs (which may be implemented as macros).
    29     29   **
    30     30   **     sqlite3Malloc()
    31     31   **     sqlite3MallocRaw()
    32     32   **     sqlite3Realloc()
    33     33   **     sqlite3ReallocOrFree()
    34     34   **     sqlite3Free()
    35     35   **     sqlite3AllocSize()
    36     36   **
    37     37   ** The function sqlite3FreeX performs the same task as sqlite3Free and is
    38     38   ** guaranteed to be a real function. The same holds for sqlite3MallocX
    39     39   **
    40         -** The above APIs are implemented in terms of the functions provided at the Os
    41         -** level (not in this file). The Os level interface is never accessed directly
           40  +** The above APIs are implemented in terms of the functions provided in the
           41  +** operating-system interface. The OS interface is never accessed directly
    42     42   ** by code outside of this file.
    43     43   **
    44     44   **     sqlite3OsMalloc()
    45     45   **     sqlite3OsRealloc()
    46     46   **     sqlite3OsFree()
    47     47   **     sqlite3OsAllocationSize()
    48     48   **
................................................................................
    51     51   ** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
    52     52   ** exceeded). Function sqlite3Malloc() usually invokes
    53     53   ** sqlite3MallocRaw().
    54     54   **
    55     55   ** MALLOC TEST WRAPPER ARCHITECTURE
    56     56   **
    57     57   ** The test wrapper provides extra test facilities to ensure the library 
    58         -** does not leak memory and handles the failure of the underlying (Os level)
           58  +** does not leak memory and handles the failure of the underlying OS level
    59     59   ** allocation system correctly. It is only present if the library is 
    60     60   ** compiled with the SQLITE_MEMDEBUG macro set.
    61     61   **
    62     62   **     * Guardposts to detect overwrites.
    63     63   **     * Ability to cause a specific Malloc() or Realloc() to fail.
    64     64   **     * Audit outstanding memory allocations (i.e check for leaks).
    65     65   */
................................................................................
    68     68   
    69     69   #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
    70     70   /*
    71     71   ** Set the soft heap-size limit for the current thread. Passing a negative
    72     72   ** value indicates no limit.
    73     73   */
    74     74   void sqlite3_soft_heap_limit(int n){
    75         -  sqlite3ThreadData()->nSoftHeapLimit = n;
           75  +  ThreadData *pTd = sqlite3ThreadData();
           76  +  if( pTd ){
           77  +    pTd->nSoftHeapLimit = n;
           78  +  }
    76     79     sqlite3ReleaseThreadData();
    77     80   }
    78     81   
    79     82   /*
    80     83   ** Release memory held by SQLite instances created by the current thread.
    81     84   */
    82     85   int sqlite3_release_memory(int n){
................................................................................
   192    195   int sqlite3_nMalloc;         /* Number of sqliteMalloc() calls */
   193    196   int sqlite3_nFree;           /* Number of sqliteFree() calls */
   194    197   int sqlite3_memUsed;         /* TODO Total memory obtained from malloc */
   195    198   int sqlite3_memMax;          /* TODO Mem usage high-water mark */
   196    199   int sqlite3_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
   197    200   int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
   198    201   
          202  +void *sqlite3_pFirst = 0;         /* Pointer to linked list of allocations */
          203  +int sqlite3_nMaxAlloc = 0;        /* High water mark of ThreadData.nAlloc */
          204  +int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
          205  +int sqlite3_isFail = 0;           /* True if all malloc calls should fail */
          206  +const char *sqlite3_zFile = 0;    /* Filename to associate debug info with */
          207  +int sqlite3_iLine = 0;            /* Line number for debug info */
          208  +
   199    209   /*
   200    210   ** Check for a simulated memory allocation failure.  Return true if
   201    211   ** the failure should be simulated.  Return false to proceed as normal.
   202    212   */
   203         -static int failMalloc(){
   204         -  ThreadData *pTsd = sqlite3ThreadData();
   205         -  if( pTsd->isFail ){
          213  +int sqlite3TestMallocFail(){
          214  +  if( sqlite3_isFail ){
   206    215       return 1;
   207    216     }
   208    217     if( sqlite3_iMallocFail>=0 ){
   209    218       sqlite3_iMallocFail--;
   210    219       if( sqlite3_iMallocFail==0 ){
   211    220         sqlite3_iMallocFail = sqlite3_iMallocReset;
   212         -      pTsd->isFail = 1;
          221  +      sqlite3_isFail = 1;
   213    222         return 1;
   214    223       }
   215    224     }
   216    225     return 0;
   217    226   }
   218    227   
   219    228   /*
................................................................................
   265    274       static const int guard = 0xdead3344;
   266    275       memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
   267    276     }
   268    277   
   269    278     /* Line number */
   270    279     z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)];             /* Guard words */
   271    280     z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
   272         -  memcpy(z, &sqlite3ThreadData()->iLine, sizeof(u32));
          281  +  memcpy(z, &sqlite3_iLine, sizeof(u32));
   273    282   
   274    283     /* File name */
   275    284     z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
   276         -  strncpy(z, sqlite3ThreadData()->zFile, TESTALLOC_FILESIZE);
          285  +  strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
   277    286     z[TESTALLOC_FILESIZE - 1] = '\0';
   278    287   
   279    288     /* User string */
   280    289     z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
   281    290     z[0] = 0;
   282    291     if( sqlite3_malloc_id ){
   283    292       strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
................................................................................
   305    314   
   306    315   #if SQLITE_MEMDEBUG>1
   307    316   /*
   308    317   ** The argument points to an Os level allocation. Link it into the threads list
   309    318   ** of allocations.
   310    319   */
   311    320   static void linkAlloc(void *p){
   312         -  ThreadData *pTsd = sqlite3ThreadData();
   313    321     void **pp = (void **)p;
   314    322     pp[0] = 0;
   315         -  pp[1] = pTsd->pFirst;
   316         -  if( pTsd->pFirst ){
   317         -    ((void **)pTsd->pFirst)[0] = p;
          323  +  pp[1] = sqlite3_pFirst;
          324  +  if( sqlite3_pFirst ){
          325  +    ((void **)sqlite3_pFirst)[0] = p;
   318    326     }
   319         -  pTsd->pFirst = p;
          327  +  sqlite3_pFirst = p;
   320    328   }
   321    329   
   322    330   /*
   323    331   ** The argument points to an Os level allocation. Unlinke it from the threads
   324    332   ** list of allocations.
   325    333   */
   326    334   static void unlinkAlloc(void *p)
   327    335   {
   328         -  ThreadData *pTsd = sqlite3ThreadData();
   329    336     void **pp = (void **)p;
   330         -  if( p==pTsd->pFirst ){
          337  +  if( p==sqlite3_pFirst ){
   331    338       assert(!pp[0]);
   332    339       assert(!pp[1] || ((void **)(pp[1]))[0]==p);
   333         -    pTsd->pFirst = pp[1];
   334         -    if( pTsd->pFirst ){
   335         -      ((void **)pTsd->pFirst)[0] = 0;
          340  +    sqlite3_pFirst = pp[1];
          341  +    if( sqlite3_pFirst ){
          342  +      ((void **)sqlite3_pFirst)[0] = 0;
   336    343       }
   337    344     }else{
   338    345       void **pprev = pp[0];
   339    346       void **pnext = pp[1];
   340    347       assert(pprev);
   341    348       assert(pprev[1]==p);
   342    349       pprev[1] = (void *)pnext;
................................................................................
   354    361   */
   355    362   static void relinkAlloc(void *p)
   356    363   {
   357    364     void **pp = (void **)p;
   358    365     if( pp[0] ){
   359    366       ((void **)(pp[0]))[1] = p;
   360    367     }else{
   361         -    ThreadData *pTsd = sqlite3ThreadData();
   362         -    pTsd->pFirst = p;
          368  +    sqlite3_pFirst = p;
   363    369     }
   364    370     if( pp[1] ){
   365    371       ((void **)(pp[1]))[0] = p;
   366    372     }
   367    373   }
   368    374   #else
   369    375   #define linkAlloc(x)
................................................................................
   382    388   **     * The __LINE__ macro ...
   383    389   **     * The value of the sqlite3_malloc_id variable ...
   384    390   **     * The output of backtrace() (if available) ...
   385    391   **
   386    392   ** Todo: We could have a version of this function that outputs to stdout, 
   387    393   ** to debug memory leaks when Tcl is not available.
   388    394   */
   389         -#ifdef TCLSH
          395  +#if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
   390    396   #include <tcl.h>
   391    397   int sqlite3OutstandingMallocs(Tcl_Interp *interp){
   392    398     void *p;
   393         -  ThreadData *pTsd = sqlite3ThreadData();
   394    399     Tcl_Obj *pRes = Tcl_NewObj();
   395    400     Tcl_IncrRefCount(pRes);
   396    401   
   397         -  for(p=pTsd->pFirst; p; p=((void **)p)[1]){
          402  +
          403  +  for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
   398    404       Tcl_Obj *pEntry = Tcl_NewObj();
   399    405       Tcl_Obj *pStack = Tcl_NewObj();
   400    406       char *z;
   401    407       u32 iLine;
   402    408       int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
   403    409       char *zAlloc = (char *)p;
   404    410       int i;
................................................................................
   434    440   #endif
   435    441   
   436    442   /*
   437    443   ** This is the test layer's wrapper around sqlite3OsMalloc().
   438    444   */
   439    445   static void * OSMALLOC(int n){
   440    446   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   441         -  ThreadData *pTsd = sqlite3ThreadData();
   442         -  pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
          447  +  sqlite3_nMaxAlloc = 
          448  +      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
   443    449   #endif
   444         -  assert( !sqlite3ThreadData()->mallocDisallowed );
   445         -  if( !failMalloc() ){
          450  +  assert( !sqlite3_mallocDisallowed );
          451  +  if( !sqlite3TestMallocFail() ){
   446    452       u32 *p;
   447    453       p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
   448    454       assert(p);
   449    455       sqlite3_nMalloc++;
   450    456       applyGuards(p);
   451    457       linkAlloc(p);
   452    458       return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
................................................................................
   476    482   }
   477    483   
   478    484   /*
   479    485   ** This is the test layer's wrapper around sqlite3OsRealloc().
   480    486   */
   481    487   static void * OSREALLOC(void *pRealloc, int n){
   482    488   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   483         -  ThreadData *pTsd = sqlite3ThreadData();
   484         -  pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
          489  +  sqlite3_nMaxAlloc = 
          490  +      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
   485    491   #endif
   486         -  assert( !sqlite3ThreadData()->mallocDisallowed );
   487         -  if( !failMalloc() ){
          492  +  assert( !sqlite3_mallocDisallowed );
          493  +  if( !sqlite3TestMallocFail() ){
   488    494       u32 *p = (u32 *)getOsPointer(pRealloc);
   489    495       checkGuards(p);
   490    496       p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
   491    497       applyGuards(p);
   492    498       relinkAlloc(p);
   493    499       return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
   494    500     }
   495    501     return 0;
   496    502   }
   497    503   
   498    504   static void OSMALLOC_FAILED(){
   499         -  sqlite3ThreadData()->isFail = 0;
          505  +  sqlite3_isFail = 0;
   500    506   }
   501    507   
   502    508   #else
   503    509   /* Define macros to call the sqlite3OsXXX interface directly if 
   504    510   ** the SQLITE_MEMDEBUG macro is not defined.
   505    511   */
   506    512   #define OSMALLOC(x)        sqlite3OsMalloc(x)
................................................................................
   524    530   ** called to try to avoid this. No indication of whether or not this is
   525    531   ** successful is returned to the caller.
   526    532   **
   527    533   ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
   528    534   ** a no-op
   529    535   */
   530    536   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   531         -static void handleSoftLimit(int n){
          537  +static int handleSoftLimit(int n){
   532    538     ThreadData *pTsd = sqlite3ThreadData();
   533         -  pTsd->nAlloc += n;
   534         -  assert( pTsd->nAlloc>=0 );
   535         -  if( n>0 && pTsd->nSoftHeapLimit>0 ){
   536         -    while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) );
   537         -  }else if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
   538         -    sqlite3ReleaseThreadData();
          539  +  if( pTsd ){
          540  +    pTsd->nAlloc += n;
          541  +    assert( pTsd->nAlloc>=0 );
          542  +    if( n>0 && pTsd->nSoftHeapLimit>0 ){
          543  +      while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) );
          544  +    }else if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
          545  +      sqlite3ReleaseThreadData();
          546  +    }
   539    547     }
          548  +  return (pTsd ? 0 : 1);
   540    549   }
   541    550   #else
   542         -#define handleSoftLimit(x)
          551  +#define handleSoftLimit(x) 0
   543    552   #endif
   544    553   
   545    554   /*
   546    555   ** Allocate and return N bytes of uninitialised memory by calling
   547    556   ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory 
   548    557   ** by calling sqlite3_release_memory().
   549    558   */
   550    559   void *sqlite3MallocRaw(int n){
   551    560     void *p = 0;
   552         -  if( n>0 && !sqlite3ThreadDataReadOnly()->mallocFailed ){
   553         -    handleSoftLimit(n);
   554         -    while( !(p = OSMALLOC(n)) && sqlite3_release_memory(n) );
          561  +  if( n>0 && !sqlite3MallocFailed() && !handleSoftLimit(n) ){
          562  +    while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) );
   555    563       if( !p ){
   556    564         /* If the allocation failed, call handleSoftLimit() again, this time
   557    565         ** with the additive inverse of the argument passed to 
   558    566         ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is
   559    567         ** still correct after a malloc() failure. 
   560    568         */
   561         -      handleSoftLimit(n * -1);
   562         -      sqlite3ThreadData()->mallocFailed = 1;
          569  +      (void)handleSoftLimit(n * -1);
          570  +      sqlite3FailedMalloc();
   563    571         OSMALLOC_FAILED();
   564    572       }
   565    573     }
   566    574     return p;
   567    575   }
   568    576   
   569    577   /*
   570    578   ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
   571    579   ** pointer to the new allocation is returned.  If the Realloc() call fails,
   572    580   ** attempt to free memory by calling sqlite3_release_memory().
   573    581   */
   574    582   void *sqlite3Realloc(void *p, int n){
   575         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
          583  +  if( sqlite3MallocFailed() ){
   576    584       return 0;
   577    585     }
   578    586   
   579    587     if( !p ){
   580    588       return sqlite3Malloc(n);
   581    589     }else{
   582    590       void *np = 0;
   583         -    handleSoftLimit(n - OSSIZEOF(p));
   584         -    while( !(np = OSREALLOC(p, n)) && sqlite3_release_memory(n) );
   585         -    if( !np ){
   586         -      /* If the allocation failed, call handleSoftLimit() again, this time
   587         -      ** with the additive inverse of the argument passed to 
   588         -      ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is
   589         -      ** still correct after a malloc() failure. 
   590         -      */
   591         -      handleSoftLimit(OSSIZEOF(p) - n);
   592         -      sqlite3ThreadData()->mallocFailed = 1;
   593         -      OSMALLOC_FAILED();
          591  +    if( !handleSoftLimit(n - OSSIZEOF(p)) ){
          592  +      while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) );
          593  +      if( !np ){
          594  +        /* If the allocation failed, call handleSoftLimit() again, this time
          595  +        ** with the additive inverse of the argument passed to 
          596  +        ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is
          597  +        ** still correct after a malloc() failure. 
          598  +        */
          599  +        (void)handleSoftLimit(OSSIZEOF(p) - n);
          600  +        sqlite3FailedMalloc();
          601  +        OSMALLOC_FAILED();
          602  +      }
   594    603       }
   595    604       return np;
   596    605     }
   597    606   }
   598    607   
   599    608   /*
   600    609   ** Free the memory pointed to by p. p must be either a NULL pointer or a 
   601    610   ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
   602    611   */
   603    612   void sqlite3FreeX(void *p){
   604         -  handleSoftLimit(0 - OSSIZEOF(p));
          613  +  (void)handleSoftLimit(0 - OSSIZEOF(p));
   605    614     if( p ){
   606    615       OSFREE(p);
   607    616     }
   608    617   }
   609    618   
   610    619   /*
   611    620   ** A version of sqliteMalloc() that is always a function, not a macro.
................................................................................
   643    652   ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
   644    653   **
   645    654   ** The number of bytes allocated does not include any overhead inserted by 
   646    655   ** any malloc() wrapper functions that may be called. So the value returned
   647    656   ** is the number of bytes that were available to SQLite using pointer p, 
   648    657   ** regardless of how much memory was actually allocated.
   649    658   */
   650         -#if 0          /* This is never actually used */
          659  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   651    660   int sqlite3AllocSize(void *p){
   652    661     return OSSIZEOF(p);
   653    662   }
   654    663   #endif
   655    664   
   656    665   /*
   657    666   ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
................................................................................
   730    739   ** encoded in UTF-8.
   731    740   **
   732    741   ** To clear the most recent error for sqlite handle "db", sqlite3Error
   733    742   ** should be called with err_code set to SQLITE_OK and zFormat set
   734    743   ** to NULL.
   735    744   */
   736    745   void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
   737         -  if( db && (db->pErr || (db->pErr = sqlite3ValueNew()))!=0 ){
          746  +  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
   738    747       db->errCode = err_code;
   739    748       if( zFormat ){
   740    749         char *z;
   741    750         va_list ap;
   742    751         va_start(ap, zFormat);
   743    752         z = sqlite3VMPrintf(zFormat, ap);
   744    753         va_end(ap);
................................................................................
  1311   1320   }
  1312   1321   #endif
  1313   1322   
  1314   1323   /*
  1315   1324   ** Return a pointer to the ThreadData associated with the calling thread.
  1316   1325   */
  1317   1326   ThreadData *sqlite3ThreadData(){
  1318         -  return (ThreadData*)sqlite3OsThreadSpecificData(1);
         1327  +  ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
         1328  +  if( !p ){
         1329  +    sqlite3FailedMalloc();
         1330  +  }
         1331  +  return p;
  1319   1332   }
  1320   1333   
  1321   1334   /*
  1322   1335   ** Return a pointer to the ThreadData associated with the calling thread.
  1323   1336   ** If no ThreadData has been allocated to this thread yet, return a pointer
  1324   1337   ** to a substitute ThreadData structure that is all zeros. 
  1325   1338   */
  1326   1339   const ThreadData *sqlite3ThreadDataReadOnly(){
  1327         -  static const ThreadData zeroData;
         1340  +  static const ThreadData zeroData = {0};  /* Initializer to silence warnings
         1341  +                                           ** from broken compilers */
  1328   1342     const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
  1329   1343     return pTd ? pTd : &zeroData;
  1330   1344   }
  1331   1345   
  1332   1346   /*
  1333   1347   ** Check to see if the ThreadData for this thread is all zero.  If it
  1334   1348   ** is, then deallocate it. 
  1335   1349   */
  1336   1350   void sqlite3ReleaseThreadData(){
  1337   1351     sqlite3OsThreadSpecificData(-1);
  1338   1352   }
  1339   1353   
  1340   1354   /*
  1341         -** Clear the "mallocFailed" flag. This should be invoked before exiting any
  1342         -** entry points that may have called sqliteMalloc().
         1355  +** This function must be called before exiting any API function (i.e. 
         1356  +** returning control to the user) that has called sqlite3Malloc or
         1357  +** sqlite3Realloc.
         1358  +**
         1359  +** The returned value is normally a copy of the second argument to this
         1360  +** function. However, if a malloc() failure has occured since the previous
         1361  +** invocation SQLITE_NOMEM is returned instead. 
         1362  +**
         1363  +** If the first argument, db, is not NULL and a malloc() error has occured,
         1364  +** then the connection error-code (the value returned by sqlite3_errcode())
         1365  +** is set to SQLITE_NOMEM.
  1343   1366   */
  1344         -void sqlite3MallocClearFailed(){
  1345         -  ThreadData *pTd = sqlite3OsThreadSpecificData(0);
  1346         -  if( pTd && pTd->mallocFailed ){
  1347         -    pTd->mallocFailed = 0;
  1348         -    sqlite3OsThreadSpecificData(0);
         1367  +static int mallocHasFailed = 0;
         1368  +int sqlite3ApiExit(sqlite3* db, int rc){
         1369  +  if( sqlite3MallocFailed() ){
         1370  +    mallocHasFailed = 0;
         1371  +    sqlite3OsLeaveMutex();
         1372  +    sqlite3Error(db, SQLITE_NOMEM, 0);
         1373  +    rc = SQLITE_NOMEM;
  1349   1374     }
         1375  +  return rc;
         1376  +}
         1377  +
         1378  +/* 
         1379  +** Return true is a malloc has failed in this thread since the last call
         1380  +** to sqlite3ApiExit(), or false otherwise.
         1381  +*/
         1382  +int sqlite3MallocFailed(){
         1383  +  return (mallocHasFailed && sqlite3OsInMutex(1));
         1384  +}
         1385  +
         1386  +/* 
         1387  +** Set the "malloc has failed" condition to true for this thread.
         1388  +*/
         1389  +void sqlite3FailedMalloc(){
         1390  +  sqlite3OsEnterMutex();
         1391  +  assert( mallocHasFailed==0 );
         1392  +  mallocHasFailed = 1;
  1350   1393   }
  1351   1394   
  1352         -#ifndef NDEBUG
         1395  +#ifdef SQLITE_MEMDEBUG
  1353   1396   /*
  1354   1397   ** This function sets a flag in the thread-specific-data structure that will
  1355   1398   ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
  1356   1399   */
  1357   1400   void sqlite3MallocDisallow(){
  1358         -  assert( sqlite3ThreadData()->mallocDisallowed>=0 );
  1359         -  sqlite3ThreadData()->mallocDisallowed++;
         1401  +  assert( sqlite3_mallocDisallowed>=0 );
         1402  +  sqlite3_mallocDisallowed++;
  1360   1403   }
  1361   1404   
  1362   1405   /*
  1363   1406   ** This function clears the flag set in the thread-specific-data structure set
  1364   1407   ** by sqlite3MallocDisallow().
  1365   1408   */
  1366   1409   void sqlite3MallocAllow(){
  1367         -  assert( sqlite3ThreadData()->mallocDisallowed>0 );
  1368         -  sqlite3ThreadData()->mallocDisallowed--;
         1410  +  assert( sqlite3_mallocDisallowed>0 );
         1411  +  sqlite3_mallocDisallowed--;
  1369   1412   }
  1370   1413   #endif

Changes to SQLite.Interop/src/vacuum.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the VACUUM command.
    13     13   **
    14     14   ** Most of the code in this file may be omitted by defining the
    15     15   ** SQLITE_OMIT_VACUUM macro.
    16     16   **
    17         -** $Id: vacuum.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           17  +** $Id: vacuum.c,v 1.17 2006/01/23 19:45:56 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "vdbeInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   #ifndef SQLITE_OMIT_VACUUM
    24     24   /*
................................................................................
   310    310     }
   311    311   
   312    312     /* If one of the execSql() calls above returned SQLITE_NOMEM, then the
   313    313     ** mallocFailed flag will be clear (because execSql() calls sqlite3_exec()).
   314    314     ** Fix this so the flag and return code match.
   315    315     */
   316    316     if( rc==SQLITE_NOMEM ){
   317         -    sqlite3ThreadData()->mallocFailed = 1;
          317  +    sqlite3MallocFailed();
   318    318     }
   319    319   
   320    320     if( zTemp ){
   321    321       sqlite3OsDelete(zTemp);
   322    322       sqliteFree(zTemp);
   323    323     }
   324    324     sqliteFree( zSql );
   325    325     sqlite3ResetInternalSchema(db, 0);
   326    326   #endif
   327    327   
   328    328     return rc;
   329    329   }

Changes to SQLite.Interop/src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
           46  +** $Id: vdbe.c,v 1.17 2006/01/23 19:45:56 rmsimpson Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   403    403   #endif
   404    404   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   405    405     int nProgressOps = 0;      /* Opcodes executed since progress callback. */
   406    406   #endif
   407    407   #ifndef NDEBUG
   408    408     Mem *pStackLimit;
   409    409   #endif
   410         -  ThreadData *pTsd = sqlite3ThreadData();
   411    410   
   412    411     if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
   413         -  pTsd->nRef++;
   414    412     assert( db->magic==SQLITE_MAGIC_BUSY );
   415    413     pTos = p->pTos;
   416    414     if( p->rc==SQLITE_NOMEM ){
   417    415       /* This happens if a malloc() inside a call to sqlite3_column_text() or
   418    416       ** sqlite3_column_text16() failed.  */
   419    417       goto no_mem;
   420    418     }
................................................................................
   427    425     }
   428    426     p->resOnStack = 0;
   429    427     db->busyHandler.nBusy = 0;
   430    428     CHECK_FOR_INTERRUPT;
   431    429     for(pc=p->pc; rc==SQLITE_OK; pc++){
   432    430       assert( pc>=0 && pc<p->nOp );
   433    431       assert( pTos<=&p->aStack[pc] );
   434         -    if( pTsd->mallocFailed ) goto no_mem;
          432  +    if( sqlite3MallocFailed() ) goto no_mem;
   435    433   #ifdef VDBE_PROFILE
   436    434       origPc = pc;
   437    435       start = hwtime();
   438    436   #endif
   439    437       pOp = &p->aOp[pc];
   440    438   
   441    439       /* Only allow tracing if SQLITE_DEBUG is defined.
................................................................................
   607    605     p->pc = pc;
   608    606     p->errorAction = pOp->p2;
   609    607     if( pOp->p3 ){
   610    608       sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
   611    609     }
   612    610     rc = sqlite3VdbeHalt(p);
   613    611     assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
   614         -  pTsd->nRef--;
   615    612     if( rc==SQLITE_BUSY ){
   616    613       p->rc = SQLITE_BUSY;
   617    614       return SQLITE_BUSY;
   618    615     }
   619    616     return p->rc ? SQLITE_ERROR : SQLITE_DONE;
   620    617   }
   621    618   
................................................................................
   914    911     ** results from the stack when the statement returns.
   915    912     */
   916    913     p->resOnStack = 1;
   917    914     p->nCallback++;
   918    915     p->popStack = pOp->p1;
   919    916     p->pc = pc + 1;
   920    917     p->pTos = pTos;
   921         -  pTsd->nRef--;
   922    918     return SQLITE_ROW;
   923    919   }
   924    920   
   925    921   /* Opcode: Concat P1 P2 *
   926    922   **
   927    923   ** Look at the first P1+2 elements of the stack.  Append them all 
   928    924   ** together with the lowest element first.  The original P1+2 elements
................................................................................
  1183   1179       assert( pOp[-1].p3type==P3_COLLSEQ );
  1184   1180       assert( pOp[-1].opcode==OP_CollSeq );
  1185   1181       ctx.pColl = (CollSeq *)pOp[-1].p3;
  1186   1182     }
  1187   1183     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  1188   1184     (*ctx.pFunc->xFunc)(&ctx, n, apVal);
  1189   1185     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  1190         -  if( pTsd->mallocFailed ) goto no_mem;
         1186  +  if( sqlite3MallocFailed() ) goto no_mem;
  1191   1187     popStack(&pTos, n);
  1192   1188   
  1193   1189     /* If any auxilary data functions have been called by this user function,
  1194   1190     ** immediately call the destructor for any non-static values.
  1195   1191     */
  1196   1192     if( ctx.pVdbeFunc ){
  1197   1193       sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
  1198   1194       pOp->p3 = (char *)ctx.pVdbeFunc;
  1199   1195       pOp->p3type = P3_VDBEFUNC;
  1200   1196     }
         1197  +
         1198  +  /* If the function returned an error, throw an exception */
         1199  +  if( ctx.isError ){
         1200  +    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
         1201  +    rc = SQLITE_ERROR;
         1202  +  }
  1201   1203   
  1202   1204     /* Copy the result of the function to the top of the stack */
  1203   1205     sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1204   1206     pTos++;
  1205   1207     pTos->flags = 0;
  1206   1208     sqlite3VdbeMemMove(pTos, &ctx.s);
  1207         -
  1208         -  /* If the function returned an error, throw an exception */
  1209         -  if( ctx.isError ){
  1210         -    if( !(pTos->flags&MEM_Str) ){
  1211         -      sqlite3SetString(&p->zErrMsg, "user function error", (char*)0);
  1212         -    }else{
  1213         -      sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pTos), (char*)0);
  1214         -      sqlite3VdbeChangeEncoding(pTos, encoding);
  1215         -    }
  1216         -    rc = SQLITE_ERROR;
  1217         -  }
  1218   1209     break;
  1219   1210   }
  1220   1211   
  1221   1212   /* Opcode: BitAnd * * *
  1222   1213   **
  1223   1214   ** Pop the top two elements from the stack.  Convert both elements
  1224   1215   ** to integers.  Push back onto the stack the bit-wise AND of the
................................................................................
  2018   2009         rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
  2019   2010         if( rc!=SQLITE_OK ){
  2020   2011           goto op_column_out;
  2021   2012         }
  2022   2013         zData = sMem.z;
  2023   2014       }
  2024   2015       zEndHdr = (u8 *)zData + offset;
  2025         -    zIdx = zData + (int)zIdx;
         2016  +    zIdx = (u8 *)zData + (int)zIdx;
  2026   2017   
  2027   2018       /* Scan the header and use it to fill in the aType[] and aOffset[]
  2028   2019       ** arrays.  aType[i] will contain the type integer for the i-th
  2029   2020       ** column and aOffset[i] will contain the offset from the beginning
  2030   2021       ** of the record to the start of the data for the i-th column
  2031   2022       */
  2032   2023       for(i=0; i<nField; i++){
................................................................................
  2339   2330       ** still running, and a transaction is active, return an error indicating
  2340   2331       ** that the other VMs must complete first. 
  2341   2332       */
  2342   2333       sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", 
  2343   2334           " transaction - SQL statements in progress", (char*)0);
  2344   2335       rc = SQLITE_ERROR;
  2345   2336     }else if( i!=db->autoCommit ){
  2346         -    pTsd->nRef--;
  2347   2337       if( pOp->p2 ){
  2348   2338         assert( i==1 );
  2349   2339         sqlite3RollbackAll(db);
  2350   2340         db->autoCommit = 1;
  2351   2341       }else{
  2352   2342         db->autoCommit = i;
  2353   2343         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
................................................................................
  2399   2389   
  2400   2390     if( pBt ){
  2401   2391       rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
  2402   2392       if( rc==SQLITE_BUSY ){
  2403   2393         p->pc = pc;
  2404   2394         p->rc = SQLITE_BUSY;
  2405   2395         p->pTos = pTos;
  2406         -      pTsd->nRef--;
  2407   2396         return SQLITE_BUSY;
  2408   2397       }
  2409   2398       if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
  2410   2399         goto abort_due_to_error;
  2411   2400       }
  2412   2401     }
  2413   2402     break;
................................................................................
  2607   2596       pCur->pIncrKey = &pCur->bogusIncrKey;
  2608   2597     }
  2609   2598     switch( rc ){
  2610   2599       case SQLITE_BUSY: {
  2611   2600         p->pc = pc;
  2612   2601         p->rc = SQLITE_BUSY;
  2613   2602         p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
  2614         -      pTsd->nRef--;
  2615   2603         return SQLITE_BUSY;
  2616   2604       }
  2617   2605       case SQLITE_OK: {
  2618   2606         int flags = sqlite3BtreeFlags(pCur->pCursor);
  2619   2607         /* Sanity checking.  Only the lower four bits of the flags byte should
  2620   2608         ** be used.  Bit 3 (mask 0x08) is unpreditable.  The lower 3 bits
  2621   2609         ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
................................................................................
  3288   3276   
  3289   3277       assert( pNos->flags & MEM_Int );
  3290   3278       assert( pC->isTable );
  3291   3279       iKey = intToKey(pNos->i);
  3292   3280   
  3293   3281       if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
  3294   3282       if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
  3295         -    if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
         3283  +    if( pC->nextRowidValid && pNos->i>=pC->nextRowid ){
  3296   3284         pC->nextRowidValid = 0;
  3297   3285       }
  3298   3286       if( pTos->flags & MEM_Null ){
  3299   3287         pTos->z = 0;
  3300   3288         pTos->n = 0;
  3301   3289       }else{
  3302   3290         assert( pTos->flags & (MEM_Blob|MEM_Str) );
................................................................................
  3713   3701     Cursor *pC;
  3714   3702     BtCursor *pCrsr;
  3715   3703     assert( pTos>=p->aStack );
  3716   3704     assert( pTos->flags & MEM_Blob );
  3717   3705     assert( i>=0 && i<p->nCursor );
  3718   3706     assert( p->apCsr[i]!=0 );
  3719   3707     if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
  3720         -    int rx, res;
  3721         -    rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
  3722         -    if( rx==SQLITE_OK && res==0 ){
         3708  +    int res;
         3709  +    rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
         3710  +    if( rc==SQLITE_OK && res==0 ){
  3723   3711         rc = sqlite3BtreeDelete(pCrsr);
  3724   3712       }
  3725   3713       assert( pC->deferredMoveto==0 );
  3726   3714       pC->cacheStatus = CACHE_STALE;
  3727   3715     }
  3728   3716     Release(pTos);
  3729   3717     pTos--;
................................................................................
  4036   4024     zSql = sqlite3MPrintf(
  4037   4025        "SELECT name, rootpage, sql, %d FROM '%q'.%s WHERE %s",
  4038   4026        pOp->p1, db->aDb[iDb].zName, zMaster, pOp->p3);
  4039   4027     if( zSql==0 ) goto no_mem;
  4040   4028     sqlite3SafetyOff(db);
  4041   4029     assert( db->init.busy==0 );
  4042   4030     db->init.busy = 1;
  4043         -  assert(0==pTsd->mallocFailed);
         4031  +  assert( !sqlite3MallocFailed() );
  4044   4032     rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  4045   4033     sqliteFree(zSql);
  4046   4034     db->init.busy = 0;
  4047   4035     sqlite3SafetyOn(db);
  4048   4036     if( rc==SQLITE_NOMEM ){
  4049         -    pTsd->mallocFailed = 1;
         4037  +    sqlite3FailedMalloc();
  4050   4038       goto no_mem;
  4051   4039     }
  4052   4040     break;  
  4053   4041   }
  4054   4042   
  4055   4043   #ifndef SQLITE_OMIT_ANALYZE
  4056   4044   /* Opcode: LoadAnalysis P1 * *
................................................................................
  4430   4418       apVal[i] = pRec;
  4431   4419       storeTypeInfo(pRec, encoding);
  4432   4420     }
  4433   4421     ctx.pFunc = (FuncDef*)pOp->p3;
  4434   4422     assert( pOp->p1>=0 && pOp->p1<p->nMem );
  4435   4423     ctx.pMem = pMem = &p->aMem[pOp->p1];
  4436   4424     pMem->n++;
         4425  +  ctx.s.flags = MEM_Null;
         4426  +  ctx.s.z = 0;
         4427  +  ctx.s.xDel = 0;
  4437   4428     ctx.isError = 0;
  4438   4429     ctx.pColl = 0;
  4439   4430     if( ctx.pFunc->needCollSeq ){
  4440   4431       assert( pOp>p->aOp );
  4441   4432       assert( pOp[-1].p3type==P3_COLLSEQ );
  4442   4433       assert( pOp[-1].opcode==OP_CollSeq );
  4443   4434       ctx.pColl = (CollSeq *)pOp[-1].p3;
  4444   4435     }
  4445   4436     (ctx.pFunc->xStep)(&ctx, n, apVal);
  4446   4437     popStack(&pTos, n);
  4447   4438     if( ctx.isError ){
         4439  +    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
  4448   4440       rc = SQLITE_ERROR;
  4449   4441     }
         4442  +  sqlite3VdbeMemRelease(&ctx.s);
  4450   4443     break;
  4451   4444   }
  4452   4445   
  4453   4446   /* Opcode: AggFinal P1 P2 P3
  4454   4447   **
  4455   4448   ** Execute the finalizer function for an aggregate.  P1 is
  4456   4449   ** the memory location that is the accumulator for the aggregate.
................................................................................
  4463   4456   ** the step function was not previously called.
  4464   4457   */
  4465   4458   case OP_AggFinal: {        /* no-push */
  4466   4459     Mem *pMem;
  4467   4460     assert( pOp->p1>=0 && pOp->p1<p->nMem );
  4468   4461     pMem = &p->aMem[pOp->p1];
  4469   4462     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  4470         -  sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
         4463  +  rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
         4464  +  if( rc==SQLITE_ERROR ){
         4465  +    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
         4466  +  }
  4471   4467     break;
  4472   4468   }
  4473   4469   
  4474   4470   
  4475   4471   /* Opcode: Vacuum * * *
  4476   4472   **
  4477   4473   ** Vacuum the entire database.  This opcode will cause other virtual
................................................................................
  4611   4607       p->rc = rc;
  4612   4608       rc = SQLITE_ERROR;
  4613   4609     }else{
  4614   4610       rc = SQLITE_DONE;
  4615   4611     }
  4616   4612     sqlite3VdbeHalt(p);
  4617   4613     p->pTos = pTos;
  4618         -  pTsd->nRef--;
  4619   4614     return rc;
  4620   4615   
  4621   4616     /* Jump to here if a malloc() fails.  It's hard to get a malloc()
  4622   4617     ** to fail on a modern VM computer, so this code is untested.
  4623   4618     */
  4624   4619   no_mem:
  4625   4620     sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
................................................................................
  4633   4628     /* Fall thru into abort_due_to_error */
  4634   4629   
  4635   4630     /* Jump to here for any other kind of fatal error.  The "rc" variable
  4636   4631     ** should hold the error number.
  4637   4632     */
  4638   4633   abort_due_to_error:
  4639   4634     if( p->zErrMsg==0 ){
  4640         -    if( pTsd->mallocFailed ) rc = SQLITE_NOMEM;
         4635  +    if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM;
  4641   4636       sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
  4642   4637     }
  4643   4638     goto vdbe_halt;
  4644   4639   
  4645   4640     /* Jump to here if the sqlite3_interrupt() API sets the interrupt
  4646   4641     ** flag.
  4647   4642     */

Changes to SQLite.Interop/src/vdbe.h.

    11     11   *************************************************************************
    12     12   ** Header file for the Virtual DataBase Engine (VDBE)
    13     13   **
    14     14   ** This header defines the interface to the virtual database engine
    15     15   ** or VDBE.  The VDBE implements an abstract machine that runs a
    16     16   ** simple program to access and modify the underlying database.
    17     17   **
    18         -** $Id: vdbe.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
           18  +** $Id: vdbe.h,v 1.16 2006/01/23 19:45:56 rmsimpson Exp $
    19     19   */
    20     20   #ifndef _SQLITE_VDBE_H_
    21     21   #define _SQLITE_VDBE_H_
    22     22   #include <stdio.h>
    23     23   
    24     24   /*
    25     25   ** A single VDBE is an opaque structure named "Vdbe".  Only routines

Changes to SQLite.Interop/src/vdbeInt.h.

   370    370   int sqlite3VdbeMemIntegerify(Mem*);
   371    371   double sqlite3VdbeRealValue(Mem*);
   372    372   void sqlite3VdbeIntegerAffinity(Mem*);
   373    373   int sqlite3VdbeMemRealify(Mem*);
   374    374   int sqlite3VdbeMemNumerify(Mem*);
   375    375   int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   376    376   void sqlite3VdbeMemRelease(Mem *p);
   377         -void sqlite3VdbeMemFinalize(Mem*, FuncDef*);
          377  +int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   378    378   #ifndef NDEBUG
   379    379   void sqlite3VdbeMemSanity(Mem*, u8);
   380    380   int sqlite3VdbeOpcodeNoPush(u8);
   381    381   #endif
   382    382   int sqlite3VdbeMemTranslate(Mem*, u8);
   383    383   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf, int nBuf);
   384    384   int sqlite3VdbeMemHandleBom(Mem *pMem);
   385    385   void sqlite3VdbeFifoInit(Fifo*);
   386    386   int sqlite3VdbeFifoPush(Fifo*, i64);
   387    387   int sqlite3VdbeFifoPop(Fifo*, i64*);
   388    388   void sqlite3VdbeFifoClear(Fifo*);

Changes to SQLite.Interop/src/vdbeapi.c.

    91     91   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
    92     92     sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
    93     93   }
    94     94   void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
    95     95     pCtx->isError = 1;
    96     96     sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
    97     97   }
           98  +#ifndef SQLITE_OMIT_UTF16
    98     99   void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
    99    100     pCtx->isError = 1;
   100    101     sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   101    102   }
          103  +#endif
   102    104   void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   103    105     sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
   104    106   }
   105    107   void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   106    108     sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
   107    109   }
   108    110   void sqlite3_result_null(sqlite3_context *pCtx){
................................................................................
   153    155   */
   154    156   int sqlite3_step(sqlite3_stmt *pStmt){
   155    157     Vdbe *p = (Vdbe*)pStmt;
   156    158     sqlite3 *db;
   157    159     int rc;
   158    160   
   159    161     /* Assert that malloc() has not failed */
   160         -  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
          162  +  assert( !sqlite3MallocFailed() );
   161    163   
   162    164     if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
   163    165       return SQLITE_MISUSE;
   164    166     }
   165    167     if( p->aborted ){
   166    168       return SQLITE_ABORT;
   167    169     }
................................................................................
   237    239       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
   238    240       assert( p->aOp[p->nOp-1].p3!=0 );
   239    241       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
   240    242       db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
   241    243     }
   242    244   #endif
   243    245   
   244         -  sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
   245         -  sqlite3MallocClearFailed();
          246  +  sqlite3Error(p->db, rc, 0);
          247  +  p->rc = sqlite3ApiExit(p->db, p->rc);
   246    248     return rc;
   247    249   }
   248    250   
   249    251   /*
   250    252   ** Extract the user data from a sqlite3_context structure and return a
   251    253   ** pointer to it.
   252    254   */
................................................................................
   401    403   static void columnMallocFailure(sqlite3_stmt *pStmt)
   402    404   {
   403    405     /* If malloc() failed during an encoding conversion within an
   404    406     ** sqlite3_column_XXX API, then set the return code of the statement to
   405    407     ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
   406    408     ** and _finalize() will return NOMEM.
   407    409     */
   408         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   409         -    ((Vdbe *)pStmt)->rc = SQLITE_NOMEM;
   410         -    sqlite3MallocClearFailed();
   411         -  }
          410  +  Vdbe *p = (Vdbe *)pStmt;
          411  +  p->rc = sqlite3ApiExit(0, p->rc);
   412    412   }
   413    413   
   414    414   /**************************** sqlite3_column_  *******************************
   415    415   ** The following routines are used to access elements of the current row
   416    416   ** in the result set.
   417    417   */
   418    418   const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
................................................................................
   499    499     }
   500    500     N += useType*n;
   501    501     ret = xFunc(&p->aColName[N]);
   502    502   
   503    503     /* A malloc may have failed inside of the xFunc() call. If this is the case,
   504    504     ** clear the mallocFailed flag and return NULL.
   505    505     */
   506         -  sqlite3MallocClearFailed();
          506  +  sqlite3ApiExit(0, 0);
   507    507     return ret;
   508    508   }
   509    509   
   510    510   /*
   511    511   ** Return the name of the Nth column of the result set returned by SQL
   512    512   ** statement pStmt.
   513    513   */
................................................................................
   627    627   
   628    628     rc = vdbeUnbind(p, i);
   629    629     if( rc || zData==0 ){
   630    630       return rc;
   631    631     }
   632    632     pVar = &p->aVar[i-1];
   633    633     rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
   634         -  if( rc ){
   635         -    return rc;
   636         -  }
   637    634     if( rc==SQLITE_OK && encoding!=0 ){
   638    635       rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
   639    636     }
   640         -  return rc;
          637  +
          638  +  sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
          639  +  return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
   641    640   }
   642    641   
   643    642   
   644    643   /*
   645    644   ** Bind a blob value to an SQL statement variable.
   646    645   */
   647    646   int sqlite3_bind_blob(
................................................................................
   777    776       || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
   778    777       return SQLITE_MISUSE;
   779    778     }
   780    779     if( pFrom->nVar!=pTo->nVar ){
   781    780       return SQLITE_ERROR;
   782    781     }
   783    782     for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
          783  +    sqlite3MallocDisallow();
   784    784       rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
          785  +    sqlite3MallocAllow();
   785    786     }
   786    787     return rc;
   787    788   }
   788    789   
   789    790   /*
   790    791   ** Return the sqlite3* database handle to which the prepared statement given
   791    792   ** in the argument belongs.  This is the same database handle that was
   792    793   ** the first argument to the sqlite3_prepare() that was used to create
   793    794   ** the statement in the first place.
   794    795   */
   795    796   sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
   796    797     return pStmt ? ((Vdbe*)pStmt)->db : 0;
   797    798   }

Changes to SQLite.Interop/src/vdbeaux.c.

   297    297   ** Add a whole list of operations to the operation stack.  Return the
   298    298   ** address of the first operation added.
   299    299   */
   300    300   int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
   301    301     int addr;
   302    302     assert( p->magic==VDBE_MAGIC_INIT );
   303    303     resizeOpArray(p, p->nOp + nOp);
   304         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
          304  +  if( sqlite3MallocFailed() ){
   305    305       return 0;
   306    306     }
   307    307     addr = p->nOp;
   308    308     if( nOp>0 ){
   309    309       int i;
   310    310       VdbeOpList const *pIn = aOp;
   311    311       for(i=0; i<nOp; i++, pIn++){
................................................................................
   411    411   ** the Vdbe. In these cases we can just copy the pointer.
   412    412   **
   413    413   ** If addr<0 then change P3 on the most recently inserted instruction.
   414    414   */
   415    415   void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
   416    416     Op *pOp;
   417    417     assert( p->magic==VDBE_MAGIC_INIT );
   418         -  if( p==0 || p->aOp==0 || sqlite3ThreadDataReadOnly()->mallocFailed ){
          418  +  if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
   419    419       if (n != P3_KEYINFO) {
   420    420         freeP3(n, (void*)*(char**)&zP3);
   421    421       }
   422    422       return;
   423    423     }
   424    424     if( addr<0 || addr>=p->nOp ){
   425    425       addr = p->nOp - 1;
................................................................................
   436    436       int nField, nByte;
   437    437   
   438    438       nField = ((KeyInfo*)zP3)->nField;
   439    439       nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
   440    440       pKeyInfo = sqliteMallocRaw( nByte );
   441    441       pOp->p3 = (char*)pKeyInfo;
   442    442       if( pKeyInfo ){
   443         -      char *aSortOrder;
          443  +      unsigned char *aSortOrder;
   444    444         memcpy(pKeyInfo, zP3, nByte);
   445    445         aSortOrder = pKeyInfo->aSortOrder;
   446    446         if( aSortOrder ){
   447         -        pKeyInfo->aSortOrder = (char*)&pKeyInfo->aColl[nField];
          447  +        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
   448    448           memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
   449    449         }
   450    450         pOp->p3type = P3_KEYINFO;
   451    451       }else{
   452    452         pOp->p3type = P3_NOTUSED;
   453    453       }
   454    454     }else if( n==P3_KEYINFO_HANDOFF ){
................................................................................
   469    469   ** Replace the P3 field of the most recently coded instruction with
   470    470   ** comment text.
   471    471   */
   472    472   void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
   473    473     va_list ap;
   474    474     assert( p->nOp>0 );
   475    475     assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 
   476         -          || sqlite3ThreadDataReadOnly()->mallocFailed );
          476  +          || sqlite3MallocFailed() );
   477    477     va_start(ap, zFormat);
   478    478     sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
   479    479     va_end(ap);
   480    480   }
   481    481   #endif
   482    482   
   483    483   /*
................................................................................
   735    735           nStack*sizeof(p->aStack[0])    /* aStack */
   736    736         + nArg*sizeof(Mem*)              /* apArg */
   737    737         + nVar*sizeof(Mem)               /* aVar */
   738    738         + nVar*sizeof(char*)             /* azVar */
   739    739         + nMem*sizeof(Mem)               /* aMem */
   740    740         + nCursor*sizeof(Cursor*)        /* apCsr */
   741    741       );
   742         -    if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
          742  +    if( !sqlite3MallocFailed() ){
   743    743         p->aMem = &p->aStack[nStack];
   744    744         p->nMem = nMem;
   745    745         p->aVar = &p->aMem[nMem];
   746    746         p->nVar = nVar;
   747    747         p->okVar = 0;
   748    748         p->apArg = (Mem**)&p->aVar[nVar];
   749    749         p->azVar = (char**)&p->apArg[nArg];
................................................................................
   887    887   ** the string is freed using sqliteFree() when the vdbe is finished with
   888    888   ** it. Otherwise, N bytes of zName are copied.
   889    889   */
   890    890   int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
   891    891     int rc;
   892    892     Mem *pColName;
   893    893     assert( idx<(2*p->nResColumn) );
   894         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return SQLITE_NOMEM;
          894  +  if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
   895    895     assert( p->aColName!=0 );
   896    896     pColName = &(p->aColName[idx]);
   897    897     if( N==P3_DYNAMIC || N==P3_STATIC ){
   898    898       rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
   899    899     }else{
   900    900       rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
   901    901     }
................................................................................
  1149   1149   ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
  1150   1150   ** means the close did not happen and needs to be repeated.
  1151   1151   */
  1152   1152   int sqlite3VdbeHalt(Vdbe *p){
  1153   1153     sqlite3 *db = p->db;
  1154   1154     int i;
  1155   1155     int (*xFunc)(Btree *pBt) = 0;  /* Function to call on each btree backend */
         1156  +  int isSpecialError;            /* Set to true if SQLITE_NOMEM or IOERR */
  1156   1157   
  1157         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1158  +  /* This function contains the logic that determines if a statement or
         1159  +  ** transaction will be committed or rolled back as a result of the
         1160  +  ** execution of this virtual machine. 
         1161  +  **
         1162  +  ** Special errors:
         1163  +  **
         1164  +  **     If an SQLITE_NOMEM error has occured in a statement that writes to
         1165  +  **     the database, then either a statement or transaction must be rolled
         1166  +  **     back to ensure the tree-structures are in a consistent state. A
         1167  +  **     statement transaction is rolled back if one is open, otherwise the
         1168  +  **     entire transaction must be rolled back.
         1169  +  **
         1170  +  **     If an SQLITE_IOERR error has occured in a statement that writes to
         1171  +  **     the database, then the entire transaction must be rolled back. The
         1172  +  **     I/O error may have caused garbage to be written to the journal 
         1173  +  **     file. Were the transaction to continue and eventually be rolled 
         1174  +  **     back that garbage might end up in the database file.
         1175  +  **     
         1176  +  **     In both of the above cases, the Vdbe.errorAction variable is 
         1177  +  **     ignored. If the sqlite3.autoCommit flag is false and a transaction
         1178  +  **     is rolled back, it will be set to true.
         1179  +  **
         1180  +  ** Other errors:
         1181  +  **
         1182  +  ** No error:
         1183  +  **
         1184  +  */
         1185  +
         1186  +  if( sqlite3MallocFailed() ){
  1158   1187       p->rc = SQLITE_NOMEM;
  1159   1188     }
  1160         -
  1161   1189     if( p->magic!=VDBE_MAGIC_RUN ){
  1162   1190       /* Already halted.  Nothing to do. */
  1163   1191       assert( p->magic==VDBE_MAGIC_HALT );
  1164   1192       return SQLITE_OK;
  1165   1193     }
  1166   1194     closeAllCursors(p);
  1167   1195     checkActiveVdbeCnt(db);
  1168         -  if( p->pc<0 ){
  1169         -    /* No commit or rollback needed if the program never started */
  1170         -  }else if( db->autoCommit && db->activeVdbeCnt==1 ){
  1171   1196   
  1172         -    if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && p->rc!=SQLITE_NOMEM)){
  1173         -      /* The auto-commit flag is true, there are no other active queries
  1174         -      ** using this handle and the vdbe program was successful or hit an
  1175         -      ** 'OR FAIL' constraint. This means a commit is required.
  1176         -      */
  1177         -      int rc = vdbeCommit(db);
  1178         -      if( rc==SQLITE_BUSY ){
  1179         -        return SQLITE_BUSY;
  1180         -      }else if( rc!=SQLITE_OK ){
  1181         -        p->rc = rc;
  1182         -        sqlite3RollbackAll(db);
  1183         -      }else{
  1184         -        sqlite3CommitInternalChanges(db);
  1185         -      }
  1186         -    }else{
  1187         -      sqlite3RollbackAll(db);
  1188         -    }
  1189         -  }else{
         1197  +  /* No commit or rollback needed if the program never started */
         1198  +  if( p->pc>=0 ){
  1190   1199   
  1191         -    if( p->rc==SQLITE_NOMEM ){
         1200  +    /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
         1201  +    isSpecialError = ((p->rc==SQLITE_NOMEM || p->rc==SQLITE_IOERR)?1:0);
         1202  +    if( isSpecialError ){
  1192   1203         /* This loop does static analysis of the query to see which of the
  1193   1204         ** following three categories it falls into:
  1194   1205         **
  1195   1206         **     Read-only
  1196         -      **     Query with statement journal          -> rollback statement
  1197         -      **     Query without statement journal       -> rollback transaction
         1207  +      **     Query with statement journal
         1208  +      **     Query without statement journal
  1198   1209         **
  1199   1210         ** We could do something more elegant than this static analysis (i.e.
  1200   1211         ** store the type of query as part of the compliation phase), but 
  1201         -      ** handling malloc() failure is a fairly obscure edge case so this is
  1202         -      ** probably easier.
  1203         -      **
  1204         -      ** Todo: This means we always override the p->errorAction value for a
  1205         -      ** malloc() failure. Is there any other choice here though?
         1212  +      ** handling malloc() or IO failure is a fairly obscure edge case so 
         1213  +      ** this is probably easier. Todo: Might be an opportunity to reduce 
         1214  +      ** code size a very small amount though...
  1206   1215         */
  1207   1216         int isReadOnly = 1;
  1208   1217         int isStatement = 0;
  1209   1218         assert(p->aOp || p->nOp==0);
  1210   1219         for(i=0; i<p->nOp; i++){ 
  1211   1220           switch( p->aOp[i].opcode ){
  1212   1221             case OP_Transaction:
................................................................................
  1213   1222               isReadOnly = 0;
  1214   1223               break;
  1215   1224             case OP_Statement:
  1216   1225               isStatement = 1;
  1217   1226               break;
  1218   1227           }
  1219   1228         }
  1220         -      if( (isReadOnly||isStatement) && p->errorAction!=OE_Rollback ){
  1221         -        p->errorAction = OE_Abort;
  1222         -      }else{ 
  1223         -        p->errorAction = OE_Rollback;
         1229  +  
         1230  +      /* If the query was read-only, we need do no rollback at all. Otherwise,
         1231  +      ** proceed with the special handling.
         1232  +      */
         1233  +      if( !isReadOnly ){
         1234  +        if( p->rc==SQLITE_NOMEM && isStatement ){
         1235  +          xFunc = sqlite3BtreeRollbackStmt;
         1236  +        }else{
         1237  +          /* We are forced to roll back the active transaction. Before doing
         1238  +          ** so, abort any other statements this handle currently has active.
         1239  +          */
         1240  +          abortOtherActiveVdbes(p);
         1241  +          sqlite3RollbackAll(db);
         1242  +          db->autoCommit = 1;
         1243  +        }
         1244  +      }
         1245  +    }
         1246  +  
         1247  +    /* If the auto-commit flag is set and this is the only active vdbe, then
         1248  +    ** we do either a commit or rollback of the current transaction. 
         1249  +    **
         1250  +    ** Note: This block also runs if one of the special errors handled 
         1251  +    ** above has occured. 
         1252  +    */
         1253  +    if( db->autoCommit && db->activeVdbeCnt==1 ){
         1254  +      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
         1255  +	/* The auto-commit flag is true, and the vdbe program was 
         1256  +        ** successful or hit an 'OR FAIL' constraint. This means a commit 
         1257  +        ** is required.
         1258  +        */
         1259  +        int rc = vdbeCommit(db);
         1260  +        if( rc==SQLITE_BUSY ){
         1261  +          return SQLITE_BUSY;
         1262  +        }else if( rc!=SQLITE_OK ){
         1263  +          p->rc = rc;
         1264  +          sqlite3RollbackAll(db);
         1265  +        }else{
         1266  +          sqlite3CommitInternalChanges(db);
         1267  +        }
         1268  +      }else{
         1269  +        sqlite3RollbackAll(db);
         1270  +      }
         1271  +    }else if( !xFunc ){
         1272  +      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
         1273  +        xFunc = sqlite3BtreeCommitStmt;
         1274  +      }else if( p->errorAction==OE_Abort ){
         1275  +        xFunc = sqlite3BtreeRollbackStmt;
         1276  +      }else{
         1277  +        abortOtherActiveVdbes(p);
         1278  +        sqlite3RollbackAll(db);
         1279  +        db->autoCommit = 1;
         1280  +      }
         1281  +    }
         1282  +  
         1283  +    /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
         1284  +    ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
         1285  +    ** and the return code is still SQLITE_OK, set the return code to the new
         1286  +    ** error value.
         1287  +    */
         1288  +    assert(!xFunc ||
         1289  +      xFunc==sqlite3BtreeCommitStmt ||
         1290  +      xFunc==sqlite3BtreeRollbackStmt
         1291  +    );
         1292  +    for(i=0; xFunc && i<db->nDb; i++){ 
         1293  +      int rc;
         1294  +      Btree *pBt = db->aDb[i].pBt;
         1295  +      if( pBt ){
         1296  +        rc = xFunc(pBt);
         1297  +        if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
         1298  +          p->rc = rc;
         1299  +          sqlite3SetString(&p->zErrMsg, 0);
         1300  +        }
         1301  +      }
         1302  +    }
         1303  +  
         1304  +    /* If this was an INSERT, UPDATE or DELETE and the statement was committed, 
         1305  +    ** set the change counter. 
         1306  +    */
         1307  +    if( p->changeCntOn && p->pc>=0 ){
         1308  +      if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
         1309  +        sqlite3VdbeSetChanges(db, p->nChange);
         1310  +      }else{
         1311  +        sqlite3VdbeSetChanges(db, 0);
  1224   1312         }
         1313  +      p->nChange = 0;
  1225   1314       }
  1226         -
  1227         -    if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
  1228         -      xFunc = sqlite3BtreeCommitStmt;
  1229         -    }else if( p->errorAction==OE_Abort ){
  1230         -      xFunc = sqlite3BtreeRollbackStmt;
  1231         -    }else{
  1232         -      abortOtherActiveVdbes(p);
  1233         -      sqlite3RollbackAll(db);
  1234         -      db->autoCommit = 1;
         1315  +  
         1316  +    /* Rollback or commit any schema changes that occurred. */
         1317  +    if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
         1318  +      sqlite3ResetInternalSchema(db, 0);
         1319  +      db->flags = (db->flags | SQLITE_InternChanges);
  1235   1320       }
  1236   1321     }
  1237   1322   
  1238         -  /* If xFunc is not NULL, then it is one of 
  1239         -  ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
  1240         -  ** each backend. If an error occurs and the return code is still
  1241         -  ** SQLITE_OK, set the return code to the new error value.
  1242         -  */
  1243         -  assert(!xFunc ||
  1244         -    xFunc==sqlite3BtreeCommitStmt ||
  1245         -    xFunc==sqlite3BtreeRollbackStmt
  1246         -  );
  1247         -  for(i=0; xFunc && i<db->nDb; i++){ 
  1248         -    int rc;
  1249         -    Btree *pBt = db->aDb[i].pBt;
  1250         -    if( pBt ){
  1251         -      rc = xFunc(pBt);
  1252         -      if( p->rc==SQLITE_OK ) p->rc = rc;
  1253         -    }
  1254         -  }
  1255         -
  1256         -  /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
  1257         -  if( p->changeCntOn && p->pc>=0 ){
  1258         -    if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
  1259         -      sqlite3VdbeSetChanges(db, p->nChange);
  1260         -    }else{
  1261         -      sqlite3VdbeSetChanges(db, 0);
  1262         -    }
  1263         -    p->nChange = 0;
  1264         -  }
  1265         -
  1266         -  /* Rollback or commit any schema changes that occurred. */
  1267         -  if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
  1268         -    sqlite3ResetInternalSchema(db, 0);
  1269         -    db->flags = (db->flags | SQLITE_InternChanges);
  1270         -  }
  1271         -
  1272   1323     /* We have successfully halted and closed the VM.  Record this fact. */
  1273   1324     if( p->pc>=0 ){
  1274   1325       db->activeVdbeCnt--;
  1275   1326     }
  1276   1327     p->magic = VDBE_MAGIC_HALT;
  1277   1328     checkActiveVdbeCnt(db);
  1278   1329   
................................................................................
  1305   1356     /* If the VDBE has be run even partially, then transfer the error code
  1306   1357     ** and error message from the VDBE into the main database structure.  But
  1307   1358     ** if the VDBE has just been set to run but has not actually executed any
  1308   1359     ** instructions yet, leave the main database error information unchanged.
  1309   1360     */
  1310   1361     if( p->pc>=0 ){
  1311   1362       if( p->zErrMsg ){
  1312         -      sqlite3Error(p->db, p->rc, "%s", p->zErrMsg);
  1313         -      sqliteFree(p->zErrMsg);
         1363  +      sqlite3* db = p->db;
         1364  +      sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
         1365  +      db->errCode = p->rc;
  1314   1366         p->zErrMsg = 0;
  1315   1367       }else if( p->rc ){
  1316   1368         sqlite3Error(p->db, p->rc, 0);
  1317   1369       }else{
  1318   1370         sqlite3Error(p->db, SQLITE_OK, 0);
  1319   1371       }
  1320   1372     }else if( p->rc && p->expired ){

Changes to SQLite.Interop/src/vdbemem.c.

   197    197     return rc;
   198    198   }
   199    199   
   200    200   /*
   201    201   ** Memory cell pMem contains the context of an aggregate function.
   202    202   ** This routine calls the finalize method for that function.  The
   203    203   ** result of the aggregate is stored back into pMem.
          204  +**
          205  +** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
          206  +** otherwise.
   204    207   */
   205         -void sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
          208  +int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
          209  +  int rc = SQLITE_OK;
   206    210     if( pFunc && pFunc->xFinalize ){
   207    211       sqlite3_context ctx;
   208    212       assert( (pMem->flags & MEM_Null)!=0 || pFunc==*(FuncDef**)&pMem->i );
   209    213       ctx.s.flags = MEM_Null;
   210    214       ctx.s.z = pMem->zShort;
   211    215       ctx.pMem = pMem;
   212    216       ctx.pFunc = pFunc;
          217  +    ctx.isError = 0;
   213    218       pFunc->xFinalize(&ctx);
   214    219       if( pMem->z && pMem->z!=pMem->zShort ){
   215    220         sqliteFree( pMem->z );
   216    221       }
   217    222       *pMem = ctx.s;
   218    223       if( pMem->flags & MEM_Short ){
   219    224         pMem->z = pMem->zShort;
   220    225       }
          226  +    if( ctx.isError ){
          227  +      rc = SQLITE_ERROR;
          228  +    }
   221    229     }
          230  +  return rc;
   222    231   }
   223    232   
   224    233   /*
   225    234   ** Release any memory held by the Mem. This may leave the Mem in an
   226    235   ** inconsistent state, for example with (Mem.z==0) and
   227    236   ** (Mem.type==SQLITE_TEXT).
   228    237   */
................................................................................
   754    763     assert( (MEM_Blob>>3) == MEM_Str );
   755    764     pVal->flags |= (pVal->flags & MEM_Blob)>>3;
   756    765     if( pVal->flags&MEM_Str ){
   757    766       sqlite3VdbeChangeEncoding(pVal, enc);
   758    767     }else if( !(pVal->flags&MEM_Blob) ){
   759    768       sqlite3VdbeMemStringify(pVal, enc);
   760    769     }
   761         -  assert(pVal->enc==enc || sqlite3ThreadDataReadOnly()->mallocFailed);
          770  +  assert(pVal->enc==enc || sqlite3MallocFailed() );
   762    771     return (const void *)(pVal->enc==enc ? (pVal->z) : 0);
   763    772   }
   764    773   
   765    774   /*
   766    775   ** Create a new sqlite3_value object.
   767    776   */
   768    777   sqlite3_value* sqlite3ValueNew(void){

Changes to SQLite.Interop/src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is reponsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
           19  +** $Id: where.c,v 1.18 2006/01/23 19:45:56 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
    25     25   */
    26     26   #define BMS  (sizeof(Bitmask)*8)
................................................................................
    36     36   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    37     37   int sqlite3_where_trace = 0;
    38     38   # define TRACE(X)  if(sqlite3_where_trace) sqlite3DebugPrintf X
    39     39   #else
    40     40   # define TRACE(X)
    41     41   #endif
    42     42   
    43         -/*
    44         -** A large value which is the maximum cost of using an index.
    45         -** By default this is a large floating point value.  When compiling
    46         -** SQLite for a processor that lacks floating point support, simply
    47         -** redefine this constant to a large integer.
    48         -*/
    49         -#ifndef SQLITE_BIG_DBL
    50         -# define SQLITE_BIG_DBL (1.0e+99)
    51         -#endif
    52         -
    53     43   /* Forward reference
    54     44   */
    55     45   typedef struct WhereClause WhereClause;
    56     46   
    57     47   /*
    58     48   ** The query generator uses an array of instances of this structure to
    59     49   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
................................................................................
    89     79   */
    90     80   typedef struct WhereTerm WhereTerm;
    91     81   struct WhereTerm {
    92     82     Expr *pExpr;            /* Pointer to the subexpression */
    93     83     i16 iParent;            /* Disable pWC->a[iParent] when this term disabled */
    94     84     i16 leftCursor;         /* Cursor number of X in "X <op> <expr>" */
    95     85     i16 leftColumn;         /* Column number of X in "X <op> <expr>" */
    96         -  u16 operator;           /* A WO_xx value describing <op> */
           86  +  u16 eOperator;          /* A WO_xx value describing <op> */
    97     87     u8 flags;               /* Bit flags.  See below */
    98     88     u8 nChild;              /* Number of children that must disable us */
    99     89     WhereClause *pWC;       /* The clause this term is part of */
   100     90     Bitmask prereqRight;    /* Bitmask of tables used by pRight */
   101     91     Bitmask prereqAll;      /* Bitmask of tables referenced by p */
   102     92   };
   103     93   
................................................................................
   424    414   ){
   425    415     WhereTerm *pTerm;
   426    416     int k;
   427    417     for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
   428    418       if( pTerm->leftCursor==iCur
   429    419          && (pTerm->prereqRight & notReady)==0
   430    420          && pTerm->leftColumn==iColumn
   431         -       && (pTerm->operator & op)!=0
          421  +       && (pTerm->eOperator & op)!=0
   432    422       ){
   433    423         if( iCur>=0 && pIdx ){
   434    424           Expr *pX = pTerm->pExpr;
   435    425           CollSeq *pColl;
   436    426           char idxaff;
   437    427           int k;
   438    428           Parse *pParse = pWC->pParse;
................................................................................
   563    553     WhereTerm *pTerm = &pWC->a[idxTerm];
   564    554     Expr *pExpr = pTerm->pExpr;
   565    555     Bitmask prereqLeft;
   566    556     Bitmask prereqAll;
   567    557     int nPattern;
   568    558     int isComplete;
   569    559   
   570         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return;
          560  +  if( sqlite3MallocFailed() ) return;
   571    561     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
   572    562     if( pExpr->op==TK_IN ){
   573    563       assert( pExpr->pRight==0 );
   574    564       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->pList)
   575    565                             | exprSelectTableUsage(pMaskSet, pExpr->pSelect);
   576    566     }else{
   577    567       pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
................................................................................
   579    569     prereqAll = exprTableUsage(pMaskSet, pExpr);
   580    570     if( ExprHasProperty(pExpr, EP_FromJoin) ){
   581    571       prereqAll |= getMask(pMaskSet, pExpr->iRightJoinTable);
   582    572     }
   583    573     pTerm->prereqAll = prereqAll;
   584    574     pTerm->leftCursor = -1;
   585    575     pTerm->iParent = -1;
   586         -  pTerm->operator = 0;
          576  +  pTerm->eOperator = 0;
   587    577     if( allowedOp(pExpr->op) && (pTerm->prereqRight & prereqLeft)==0 ){
   588    578       Expr *pLeft = pExpr->pLeft;
   589    579       Expr *pRight = pExpr->pRight;
   590    580       if( pLeft->op==TK_COLUMN ){
   591    581         pTerm->leftCursor = pLeft->iTable;
   592    582         pTerm->leftColumn = pLeft->iColumn;
   593         -      pTerm->operator = operatorMask(pExpr->op);
          583  +      pTerm->eOperator = operatorMask(pExpr->op);
   594    584       }
   595    585       if( pRight && pRight->op==TK_COLUMN ){
   596    586         WhereTerm *pNew;
   597    587         Expr *pDup;
   598    588         if( pTerm->leftCursor>=0 ){
   599    589           int idxNew;
   600    590           pDup = sqlite3ExprDup(pExpr);
................................................................................
   611    601         }
   612    602         exprCommute(pDup);
   613    603         pLeft = pDup->pLeft;
   614    604         pNew->leftCursor = pLeft->iTable;
   615    605         pNew->leftColumn = pLeft->iColumn;
   616    606         pNew->prereqRight = prereqLeft;
   617    607         pNew->prereqAll = prereqAll;
   618         -      pNew->operator = operatorMask(pDup->op);
          608  +      pNew->eOperator = operatorMask(pDup->op);
   619    609       }
   620    610     }
   621    611   
   622    612   #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
   623    613     /* If a term is the BETWEEN operator, create two new virtual terms
   624    614     ** that define the range that the BETWEEN implements.
   625    615     */
................................................................................
   670    660       assert( sOr.nTerm>0 );
   671    661       j = 0;
   672    662       do{
   673    663         iColumn = sOr.a[j].leftColumn;
   674    664         iCursor = sOr.a[j].leftCursor;
   675    665         ok = iCursor>=0;
   676    666         for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
   677         -        if( pOrTerm->operator!=WO_EQ ){
          667  +        if( pOrTerm->eOperator!=WO_EQ ){
   678    668             goto or_not_possible;
   679    669           }
   680    670           if( pOrTerm->leftCursor==iCursor && pOrTerm->leftColumn==iColumn ){
   681    671             pOrTerm->flags |= TERM_OR_OK;
   682    672           }else if( (pOrTerm->flags & TERM_COPIED)!=0 ||
   683    673                       ((pOrTerm->flags & TERM_VIRTUAL)!=0 &&
   684    674                        (sOr.a[pOrTerm->iParent].flags & TERM_OR_OK)!=0) ){
................................................................................
   941    931     /* Check for a rowid=EXPR or rowid IN (...) constraints
   942    932     */
   943    933     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
   944    934     if( pTerm ){
   945    935       Expr *pExpr;
   946    936       *ppIndex = 0;
   947    937       bestFlags = WHERE_ROWID_EQ;
   948         -    if( pTerm->operator & WO_EQ ){
          938  +    if( pTerm->eOperator & WO_EQ ){
   949    939         /* Rowid== is always the best pick.  Look no further.  Because only
   950    940         ** a single row is generated, output is always in sorted order */
   951    941         *pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
   952    942         *pnEq = 1;
   953    943         TRACE(("... best is rowid\n"));
   954    944         return 0.0;
   955    945       }else if( (pExpr = pTerm->pExpr)->pList!=0 ){
................................................................................
  1022   1012       */
  1023   1013       flags = 0;
  1024   1014       for(i=0; i<pProbe->nColumn; i++){
  1025   1015         int j = pProbe->aiColumn[i];
  1026   1016         pTerm = findTerm(pWC, iCur, j, notReady, WO_EQ|WO_IN, pProbe);
  1027   1017         if( pTerm==0 ) break;
  1028   1018         flags |= WHERE_COLUMN_EQ;
  1029         -      if( pTerm->operator & WO_IN ){
         1019  +      if( pTerm->eOperator & WO_IN ){
  1030   1020           Expr *pExpr = pTerm->pExpr;
  1031   1021           flags |= WHERE_COLUMN_IN;
  1032   1022           if( pExpr->pSelect!=0 ){
  1033   1023             inMultiplier *= 100;
  1034   1024           }else if( pExpr->pList!=0 ){
  1035   1025             inMultiplier *= pExpr->pList->nExpr + 1;
  1036   1026           }
................................................................................
  1447   1437     whereClauseInit(&wc, pParse);
  1448   1438     whereSplit(&wc, pWhere, TK_AND);
  1449   1439       
  1450   1440     /* Allocate and initialize the WhereInfo structure that will become the
  1451   1441     ** return value.
  1452   1442     */
  1453   1443     pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
  1454         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1444  +  if( sqlite3MallocFailed() ){
  1455   1445       goto whereBeginNoMem;
  1456   1446     }
  1457   1447     pWInfo->pParse = pParse;
  1458   1448     pWInfo->pTabList = pTabList;
  1459   1449     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  1460   1450   
  1461   1451     /* Special case: a WHERE clause that is constant.  Evaluate the
................................................................................
  1471   1461     ** want to analyze these virtual terms, so start analyzing at the end
  1472   1462     ** and work forward so that the added virtual terms are never processed.
  1473   1463     */
  1474   1464     for(i=0; i<pTabList->nSrc; i++){
  1475   1465       createMask(&maskSet, pTabList->a[i].iCursor);
  1476   1466     }
  1477   1467     exprAnalyzeAll(pTabList, &maskSet, &wc);
  1478         -  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
         1468  +  if( sqlite3MallocFailed() ){
  1479   1469       goto whereBeginNoMem;
  1480   1470     }
  1481   1471   
  1482   1472     /* Chose the best index to use for each table in the FROM clause.
  1483   1473     **
  1484   1474     ** This loop fills in the following fields:
  1485   1475     **
................................................................................
  1504   1494       int nEq;                    /* Number of == or IN constraints */
  1505   1495       double cost;                /* The cost for pIdx */
  1506   1496       int j;                      /* For looping over FROM tables */
  1507   1497       Index *pBest = 0;           /* The best index seen so far */
  1508   1498       int bestFlags = 0;          /* Flags associated with pBest */
  1509   1499       int bestNEq = 0;            /* nEq associated with pBest */
  1510   1500       double lowestCost;          /* Cost of the pBest */
  1511         -    int bestJ;                  /* The value of j */
         1501  +    int bestJ = 0;              /* The value of j */
  1512   1502       Bitmask m;                  /* Bitmask value for j or bestJ */
  1513   1503   
  1514   1504       lowestCost = SQLITE_BIG_DBL;
  1515   1505       for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
  1516   1506         m = getMask(&maskSet, pTabItem->iCursor);
  1517   1507         if( (m & notReady)==0 ){
  1518   1508           if( j==iFrom ) iFrom++;
................................................................................
  1789   1779           Expr *pX;
  1790   1780           int k = pIdx->aiColumn[j];
  1791   1781           pTerm = findTerm(&wc, iCur, k, notReady, topOp, pIdx);
  1792   1782           assert( pTerm!=0 );
  1793   1783           pX = pTerm->pExpr;
  1794   1784           assert( (pTerm->flags & TERM_CODED)==0 );
  1795   1785           sqlite3ExprCode(pParse, pX->pRight);
  1796         -        topEq = pTerm->operator & (WO_LE|WO_GE);
         1786  +        topEq = pTerm->eOperator & (WO_LE|WO_GE);
  1797   1787           disableTerm(pLevel, pTerm);
  1798   1788           testOp = OP_IdxGE;
  1799   1789         }else{
  1800   1790           testOp = nEq>0 ? OP_IdxGE : OP_Noop;
  1801   1791           topEq = 1;
  1802   1792         }
  1803   1793         if( testOp!=OP_Noop ){
................................................................................
  1827   1817           Expr *pX;
  1828   1818           int k = pIdx->aiColumn[j];
  1829   1819           pTerm = findTerm(&wc, iCur, k, notReady, btmOp, pIdx);
  1830   1820           assert( pTerm!=0 );
  1831   1821           pX = pTerm->pExpr;
  1832   1822           assert( (pTerm->flags & TERM_CODED)==0 );
  1833   1823           sqlite3ExprCode(pParse, pX->pRight);
  1834         -        btmEq = pTerm->operator & (WO_LE|WO_GE);
         1824  +        btmEq = pTerm->eOperator & (WO_LE|WO_GE);
  1835   1825           disableTerm(pLevel, pTerm);
  1836   1826         }else{
  1837   1827           btmEq = 1;
  1838   1828         }
  1839   1829         if( nEq>0 || btmLimit ){
  1840   1830           int nCol = nEq + btmLimit;
  1841   1831           buildIndexProbe(v, nCol, 0, brk, pIdx);

Changes to SQLite.NET.sln.

     1      1   
     2      2   Microsoft Visual Studio Solution File, Format Version 9.00
     3      3   # Visual Studio 2005
     4      4   Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Data.SQLite - Compact", "System.Data.SQLite\System.Data.SQLite - Compact.csproj", "{AC139951-261A-4463-B6FA-AEBC25283A66}"
     5      5   EndProject
     6      6   Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{E27B1B1E-19C0-45E8-AA74-B6E1C041A130}"
     7      7   	ProjectSection(ProjectDependencies) = postProject
     8         -		{10B51CE8-A838-44DE-BD82-B658F0296F80} = {10B51CE8-A838-44DE-BD82-B658F0296F80}
            8  +		{AC139951-261A-4463-B6FA-AEBC25283A66} = {AC139951-261A-4463-B6FA-AEBC25283A66}
     9      9   		{AC139952-261A-4463-B6FA-AEBC25283A66} = {AC139952-261A-4463-B6FA-AEBC25283A66}
    10         -		{AC139951-261A-4463-B6FA-AEBC25283A66} = {AC139951-261A-4463-B6FA-AEBC25283A66}
           10  +		{10B51CE8-A838-44DE-BD82-B658F0296F80} = {10B51CE8-A838-44DE-BD82-B658F0296F80}
    11     11   	EndProjectSection
    12     12   EndProject
    13     13   Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{39A3B743-1EBD-4CC0-8E37-ACE3DD38B1C0}"
    14     14   	ProjectSection(SolutionItems) = preProject
    15     15   		readme.htm = readme.htm
    16     16   	EndProjectSection
    17     17   EndProject
................................................................................
   150    150   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
   151    151   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
   152    152   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|Win32.ActiveCfg = Debug|Any CPU
   153    153   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|Win32.Build.0 = Debug|Any CPU
   154    154   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|x64.ActiveCfg = Debug|Any CPU
   155    155   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Debug|x64.Build.0 = Debug|Any CPU
   156    156   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Compact Framework.ActiveCfg = Debug|Any CPU
   157         -		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Itanium.ActiveCfg = Debug|Any CPU
          157  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Itanium.ActiveCfg = Release|Any CPU
          158  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Itanium.Build.0 = Release|Any CPU
   158    159   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Mixed Platforms.ActiveCfg = Debug|Any CPU
   159    160   		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Mixed Platforms.Build.0 = Debug|Any CPU
   160         -		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Win32.ActiveCfg = Debug|Any CPU
   161         -		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|x64.ActiveCfg = Debug|Any CPU
          161  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Win32.ActiveCfg = Release|Any CPU
          162  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|Win32.Build.0 = Release|Any CPU
          163  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|x64.ActiveCfg = Release|Any CPU
          164  +		{AC139952-261A-4463-B6FA-AEBC25283A66}.Release|x64.Build.0 = Release|Any CPU
   162    165   	EndGlobalSection
   163    166   	GlobalSection(SolutionProperties) = preSolution
   164    167   		HideSolutionNode = FALSE
   165    168   	EndGlobalSection
   166    169   EndGlobal

Changes to SQLite.NET.suo.

cannot compute difference between binary files

Changes to System.Data.SQLite/System.Data.SQLite.

    24     24       <WarningLevel>4</WarningLevel>
    25     25       <DocumentationFile>
    26     26       </DocumentationFile>
    27     27       <GenerateSerializationAssemblies>off</GenerateSerializationAssemblies>
    28     28       <FileAlignment>512</FileAlignment>
    29     29     </PropertyGroup>
    30     30     <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    31         -    <DebugSymbols>true</DebugSymbols>
           31  +    <DebugSymbols>false</DebugSymbols>
    32     32       <OutputPath>bin\</OutputPath>
    33     33       <DefineConstants>
    34     34       </DefineConstants>
    35         -    <DebugType>full</DebugType>
           35  +    <DebugType>none</DebugType>
    36     36       <PlatformTarget>AnyCPU</PlatformTarget>
    37     37       <CodeAnalysisRuleAssemblies>C:\Program Files\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
    38     38       <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
    39     39       <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
    40     40       <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
    41     41       <ErrorReport>prompt</ErrorReport>
    42     42       <FileAlignment>512</FileAlignment>

Changes to bin/tools/mergebin.exe.

cannot compute difference between binary files