/ Check-in [d1b29156]
Login

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

Overview
Comment:Transactions commit on the last sqlite3_step(), not on sqlite3_finalize(). This allows the sqlite3_step() to return SQLITE_BUSY if the commit is blocked by a lock. Ticket #885. (CVS 1928)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d1b29156558f1c576002cdb2544dffaa693da39b
User & Date: drh 2004-09-02 14:57:08
Context
2004-09-02
15:14
Fix a problem with EXPLAIN on pragmas that return single integer values. Ticket #886 (CVS 1929) check-in: 8ce3d730 user: drh tags: trunk
14:57
Transactions commit on the last sqlite3_step(), not on sqlite3_finalize(). This allows the sqlite3_step() to return SQLITE_BUSY if the commit is blocked by a lock. Ticket #885. (CVS 1928) check-in: d1b29156 user: drh tags: trunk
2004-09-01
16:12
Work around a bug in the Borland C++ compiler. Ticket #881. (CVS 1927) check-in: 18af6ba5 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to 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.256 2004/08/29 20:08:59 drh Exp $
           17  +** $Id: main.c,v 1.257 2004/09/02 14:57:08 drh 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
................................................................................
   517    517     for(i=0; i<db->nDb; i++){
   518    518       if( db->aDb[i].pBt ){
   519    519         sqlite3BtreeRollback(db->aDb[i].pBt);
   520    520         db->aDb[i].inTrans = 0;
   521    521       }
   522    522     }
   523    523     sqlite3ResetInternalSchema(db, 0);
   524         -  /* sqlite3RollbackInternalChanges(db); */
   525    524   }
   526    525   
   527    526   /*
   528    527   ** Return a static string that describes the kind of error specified in the
   529    528   ** argument.
   530    529   */
   531    530   const char *sqlite3ErrStr(int rc){
................................................................................
   983    982     sqlite3RunParser(&sParse, zSql, &zErrMsg);
   984    983   
   985    984     if( db->xTrace && !db->init.busy ){
   986    985       /* Trace only the statment that was compiled.
   987    986       ** Make a copy of that part of the SQL string since zSQL is const
   988    987       ** and we must pass a zero terminated string to the trace function
   989    988       ** The copy is unnecessary if the tail pointer is pointing at the
   990         -    ** beginnig or end of the SQL string.
          989  +    ** beginning or end of the SQL string.
   991    990       */
   992    991       if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){
   993    992         char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql);
   994    993         if( tmpSql ){
   995    994           db->xTrace(db->pTraceArg, tmpSql);
   996    995           sqliteFree(tmpSql);
   997    996         }else{

Changes to 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.161 2004/08/30 16:52:18 drh Exp $
           21  +** @(#) $Id: pager.c,v 1.162 2004/09/02 14:57:08 drh Exp $
    22     22   */
    23     23   #include "os.h"         /* Must be first to enable large file support */
    24     24   #include "sqliteInt.h"
    25     25   #include "pager.h"
    26     26   #include <assert.h>
    27     27   #include <string.h>
    28     28   
................................................................................
  2794   2794     rc = pager_unwritelock(pPager);
  2795   2795     pPager->dbSize = -1;
  2796   2796     return rc;
  2797   2797   
  2798   2798     /* Jump here if anything goes wrong during the commit process.
  2799   2799     */
  2800   2800   commit_abort:
  2801         -  rc = sqlite3pager_rollback(pPager);
  2802         -  if( rc==SQLITE_OK ){
  2803         -    rc = SQLITE_FULL;
  2804         -  }
         2801  +  sqlite3pager_rollback(pPager);
  2805   2802     return rc;
  2806   2803   }
  2807   2804   
  2808   2805   /*
  2809   2806   ** Rollback all changes.  The database falls back to PAGER_SHARED mode.
  2810   2807   ** All in-memory cache pages revert to their original data contents.
  2811   2808   ** The journal is deleted.

Changes to 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.412 2004/08/31 13:45:12 drh Exp $
           46  +** $Id: vdbe.c,v 1.413 2004/09/02 14:57:09 drh 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   /*
................................................................................
   617    617   }
   618    618   
   619    619   /* Opcode:  Halt P1 P2 *
   620    620   **
   621    621   ** Exit immediately.  All open cursors, Lists, Sorts, etc are closed
   622    622   ** automatically.
   623    623   **
   624         -** P1 is the result code returned by sqlite3_exec().  For a normal
   625         -** halt, this should be SQLITE_OK (0).  For errors, it can be some
   626         -** other value.  If P1!=0 then P2 will determine whether or not to
   627         -** rollback the current transaction.  Do not rollback if P2==OE_Fail.
   628         -** Do the rollback if P2==OE_Rollback.  If P2==OE_Abort, then back
   629         -** out all changes that have occurred during this execution of the
          624  +** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
          625  +** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
          626  +** For errors, it can be some other value.  If P1!=0 then P2 will determine
          627  +** whether or not to rollback the current transaction.  Do not rollback
          628  +** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
          629  +** then back out all changes that have occurred during this execution of the
   630    630   ** VDBE, but do not rollback the transaction. 
   631    631   **
   632    632   ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
   633    633   ** every program.  So a jump past the last instruction of the program
   634    634   ** is the same as executing Halt.
   635    635   */
   636    636   case OP_Halt: {
   637         -  p->magic = VDBE_MAGIC_HALT;
   638    637     p->pTos = pTos;
   639         -  if( pOp->p1!=SQLITE_OK ){
   640         -    p->rc = pOp->p1;
   641         -    p->errorAction = pOp->p2;
          638  +  p->rc = pOp->p1;
          639  +  p->pc = pc;
          640  +  p->errorAction = pOp->p2;
          641  +  if( pOp->p3 ){
   642    642       sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
   643         -    return SQLITE_ERROR;
   644         -  }else{
   645         -    p->rc = SQLITE_OK;
   646         -    return SQLITE_DONE;
   647    643     }
          644  +  rc = sqlite3VdbeHalt(p);
          645  +  if( rc==SQLITE_BUSY ){
          646  +    p->rc = SQLITE_BUSY;
          647  +    return SQLITE_BUSY;
          648  +  }else if( rc!=SQLITE_OK ){
          649  +    p->rc = rc;
          650  +  }
          651  +  return p->rc ? SQLITE_ERROR : SQLITE_DONE;
   648    652   }
   649    653   
   650    654   /* Opcode: Integer P1 * P3
   651    655   **
   652    656   ** The integer value P1 is pushed onto the stack.  If P3 is not zero
   653    657   ** then it is assumed to be a string representation of the same integer.
   654    658   ** If P1 is zero and P3 is not zero, then the value is derived from P3.
................................................................................
  2114   2118   }
  2115   2119   
  2116   2120   /* Opcode: AutoCommit P1 P2 *
  2117   2121   **
  2118   2122   ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
  2119   2123   ** back any currently active btree transactions. If there are any active
  2120   2124   ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
         2125  +**
         2126  +** This instruction causes the VM to halt.
  2121   2127   */
  2122   2128   case OP_AutoCommit: {
  2123   2129     u8 i = pOp->p1;
  2124   2130     u8 rollback = pOp->p2;
  2125   2131   
  2126   2132     assert( i==1 || i==0 );
  2127   2133     assert( i==1 || rollback==0 );
  2128   2134   
  2129         -  assert( db->activeVdbeCnt>0 );
         2135  +  assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
  2130   2136   
  2131   2137     if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
  2132   2138       /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
  2133   2139       ** still running, and a transaction is active, return an error indicating
  2134   2140       ** that the other VMs must complete first. 
  2135   2141       */
  2136   2142       sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", 
  2137   2143           " transaction - SQL statements in progress", 0);
  2138   2144       rc = SQLITE_ERROR;
  2139   2145     }else if( i!=db->autoCommit ){
  2140   2146       db->autoCommit = i;
  2141         -    p->autoCommitOn |= i;
  2142   2147       if( pOp->p2 ){
         2148  +      assert( i==1 );
  2143   2149         sqlite3RollbackAll(db);
         2150  +    }else if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
         2151  +      p->pTos = pTos;
         2152  +      p->pc = pc;
         2153  +      db->autoCommit = 1-i;
         2154  +      p->rc = SQLITE_BUSY;
         2155  +      return SQLITE_BUSY;
  2144   2156       }
         2157  +    return SQLITE_DONE;
  2145   2158     }else{
  2146   2159       sqlite3SetString(&p->zErrMsg,
  2147   2160           (!i)?"cannot start a transaction within a transaction":(
  2148   2161           (rollback)?"cannot rollback - no transaction is active":
  2149   2162                      "cannot commit - no transaction is active"), 0);
  2150   2163            
  2151   2164       rc = SQLITE_ERROR;
................................................................................
  4447   4460   vdbe_halt:
  4448   4461     if( rc ){
  4449   4462       p->rc = rc;
  4450   4463       rc = SQLITE_ERROR;
  4451   4464     }else{
  4452   4465       rc = SQLITE_DONE;
  4453   4466     }
  4454         -  p->magic = VDBE_MAGIC_HALT;
         4467  +  sqlite3VdbeHalt(p);
  4455   4468     p->pTos = pTos;
  4456   4469     return rc;
  4457   4470   
  4458   4471     /* Jump to here if a malloc() fails.  It's hard to get a malloc()
  4459   4472     ** to fail on a modern VM computer, so this code is untested.
  4460   4473     */
  4461   4474   no_mem:

Changes to src/vdbeInt.h.

   334    334     int returnDepth;        /* Next unused element in returnStack[] */
   335    335     int nResColumn;         /* Number of columns in one row of the result set */
   336    336     char **azResColumn;     /* Values for one row of result */ 
   337    337     int popStack;           /* Pop the stack this much on entry to VdbeExec() */
   338    338     char *zErrMsg;          /* Error message written here */
   339    339     u8 resOnStack;          /* True if there are result values on the stack */
   340    340     u8 explain;             /* True if EXPLAIN present on SQL command */
   341         -  u8 autoCommitOn;        /* True if autocommit got turned on by this program */
   342    341     u8 changeCntOn;         /* True to update the change-counter */
   343    342     u8 aborted;             /* True if ROLLBACK in another VM causes an abort */
   344    343     int nChange;            /* Number of db changes made since last reset */
   345    344   };
   346    345   
   347    346   /*
   348    347   ** The following are allowed values for Vdbe.magic
................................................................................
   375    374   int sqlite3VdbeIdxKeyCompare(Cursor*, int , const unsigned char*, int*);
   376    375   int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
   377    376   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   378    377   int sqlite3VdbeRecordCompare(void*,int,const void*,int, const void*);
   379    378   int sqlite3VdbeIdxRowidLen(int,const u8*);
   380    379   int sqlite3VdbeExec(Vdbe*);
   381    380   int sqlite3VdbeList(Vdbe*);
          381  +int sqlite3VdbeHalt(Vdbe*);
   382    382   int sqlite3VdbeChangeEncoding(Mem *, int);
   383    383   int sqlite3VdbeMemCopy(Mem*, const Mem*);
   384    384   void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
   385    385   int sqlite3VdbeMemMove(Mem*, Mem*);
   386    386   int sqlite3VdbeMemNulTerminate(Mem*);
   387    387   int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   388    388   void sqlite3VdbeMemSetInt64(Mem*, i64);

Changes to src/vdbeaux.c.

   539    539   }
   540    540   
   541    541   /*
   542    542   ** Prepare a virtual machine for execution.  This involves things such
   543    543   ** as allocating stack space and initializing the program counter.
   544    544   ** After the VDBE has be prepped, it can be executed by one or more
   545    545   ** calls to sqlite3VdbeExec().  
          546  +**
          547  +** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
          548  +** VDBE_MAGIC_RUN.
   546    549   */
   547    550   void sqlite3VdbeMakeReady(
   548    551     Vdbe *p,                       /* The VDBE */
   549    552     int nVar,                      /* Number of '?' see in the SQL statement */
   550    553     int nMem,                      /* Number of memory cells to allocate */
   551    554     int nCursor,                   /* Number of cursors to allocate */
   552    555     int isExplain                  /* True if the EXPLAIN keywords is present */
................................................................................
   934    937         needXcommit = 1;
   935    938         if( i!=1 ) nTrans++;
   936    939       }
   937    940     }
   938    941   
   939    942     /* If there are any write-transactions at all, invoke the commit hook */
   940    943     if( needXcommit && db->xCommitCallback ){
   941         -    if( db->xCommitCallback(db->pCommitArg) ){
          944  +    int rc;
          945  +    sqlite3SafetyOff(db);
          946  +    rc = db->xCommitCallback(db->pCommitArg);
          947  +    sqlite3SafetyOn(db);
          948  +    if( rc ){
   942    949         return SQLITE_CONSTRAINT;
   943    950       }
   944    951     }
   945    952   
   946    953     /* The simple case - no more than one database file (not counting the
   947    954     ** TEMP database) has a transaction active.   There is no need for the
   948    955     ** master-journal.
................................................................................
  1120   1127     }
  1121   1128   }
  1122   1129   
  1123   1130   /* 
  1124   1131   ** This routine checks that the sqlite3.activeVdbeCnt count variable
  1125   1132   ** matches the number of vdbe's in the list sqlite3.pVdbe that are
  1126   1133   ** currently active. An assertion fails if the two counts do not match.
         1134  +** This is an internal self-check only - it is not an essential processing
         1135  +** step.
  1127   1136   **
  1128   1137   ** This is a no-op if NDEBUG is defined.
  1129   1138   */
  1130   1139   #ifndef NDEBUG
  1131   1140   static void checkActiveVdbeCnt(sqlite *db){
  1132   1141     Vdbe *p;
  1133   1142     int cnt = 0;
  1134         -
  1135   1143     p = db->pVdbe;
  1136   1144     while( p ){
  1137         -    if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
         1145  +    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
  1138   1146         cnt++;
  1139   1147       }
  1140   1148       p = p->pNext;
  1141   1149     }
  1142         -
  1143   1150     assert( cnt==db->activeVdbeCnt );
  1144   1151   }
  1145   1152   #else
  1146   1153   #define checkActiveVdbeCnt(x)
  1147   1154   #endif
  1148   1155   
  1149   1156   /*
  1150         -** Clean up a VDBE after execution but do not delete the VDBE just yet.
  1151         -** Write any error messages into *pzErrMsg.  Return the result code.
         1157  +** This routine is called the when a VDBE tries to halt.  If the VDBE
         1158  +** has made changes and is in autocommit mode, then commit those
         1159  +** changes.  If a rollback is needed, then do the rollback.
         1160  +**
         1161  +** This routine is the only way to move the state of a VM from
         1162  +** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
  1152   1163   **
  1153         -** After this routine is run, the VDBE should be ready to be executed
  1154         -** again.
         1164  +** Return an error code.  If the commit could not complete because of
         1165  +** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
         1166  +** means the close did not happen and needs to be repeated.
  1155   1167   */
  1156         -int sqlite3VdbeReset(Vdbe *p){
         1168  +int sqlite3VdbeHalt(Vdbe *p){
  1157   1169     sqlite *db = p->db;
  1158   1170     int i;
  1159   1171     int (*xFunc)(Btree *pBt) = 0;  /* Function to call on each btree backend */
  1160   1172   
  1161         -  if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
  1162         -    sqlite3Error(p->db, SQLITE_MISUSE, 0 ,0);
  1163         -    return SQLITE_MISUSE;
         1173  +  if( p->magic!=VDBE_MAGIC_RUN ){
         1174  +    /* Already halted.  Nothing to do. */
         1175  +    assert( p->magic==VDBE_MAGIC_HALT );
         1176  +    return SQLITE_OK;
  1164   1177     }
  1165         -  if( p->zErrMsg ){
  1166         -    sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
  1167         -    sqliteFree(p->zErrMsg);
  1168         -    p->zErrMsg = 0;
  1169         -  }else if( p->rc ){
  1170         -    sqlite3Error(p->db, p->rc, 0);
  1171         -  }else{
  1172         -    sqlite3Error(p->db, SQLITE_OK, 0);
  1173         -  }
  1174         -  Cleanup(p);
  1175         -
  1176         -  /* What is done now depends on the exit status of the vdbe, the value of
  1177         -  ** the sqlite.autoCommit flag and whether or not there are any other
  1178         -  ** queries in progress. A transaction or statement transaction may need
  1179         -  ** to be committed or rolled back on each open database file.
  1180         -  */
         1178  +  closeAllCursors(p);
  1181   1179     checkActiveVdbeCnt(db);
  1182   1180     if( db->autoCommit && db->activeVdbeCnt==1 ){
  1183   1181       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
  1184   1182         /* The auto-commit flag is true, there are no other active queries
  1185   1183         ** using this handle and the vdbe program was successful or hit an
  1186         -      ** 'OR FAIL' constraint. This means a commit is required, which is
  1187         -      ** handled a little differently from the other options.
         1184  +      ** 'OR FAIL' constraint. This means a commit is required.
  1188   1185         */
  1189         -      p->rc = vdbeCommit(db);
  1190         -      if( p->rc!=SQLITE_OK ){
  1191         -        sqlite3Error(p->db, p->rc, 0);
  1192         -        if( p->rc==SQLITE_BUSY && p->autoCommitOn ){
  1193         -          /* If we just now have turned autocommit on (meaning we just have
  1194         -          ** finished executing a COMMIT command) but the commit fails due
  1195         -          ** to lock contention, autocommit back off.  This gives the user
  1196         -          ** the opportunity to try again after the lock that was preventing
  1197         -          ** the commit has cleared. */
  1198         -          db->autoCommit = 0;
  1199         -        }else{
  1200         -          /* If the command just executed was not a COMMIT command, then
  1201         -          ** rollback whatever the results of that command were */
  1202         -          xFunc = sqlite3BtreeRollback;
  1203         -        }
         1186  +      int rc = vdbeCommit(db);
         1187  +      if( rc==SQLITE_BUSY ){
         1188  +        return SQLITE_BUSY;
         1189  +      }else if( rc!=SQLITE_OK ){
         1190  +        p->rc = rc;
         1191  +        xFunc = sqlite3BtreeRollback;
  1204   1192         }
  1205   1193       }else{
  1206   1194         xFunc = sqlite3BtreeRollback;
  1207   1195       }
  1208   1196     }else{
  1209   1197       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
  1210   1198         xFunc = sqlite3BtreeCommitStmt;
................................................................................
  1212   1200         xFunc = sqlite3BtreeRollbackStmt;
  1213   1201       }else{
  1214   1202         xFunc = sqlite3BtreeRollback;
  1215   1203         db->autoCommit = 1;
  1216   1204         abortOtherActiveVdbes(p);
  1217   1205       }
  1218   1206     }
  1219         -  p->autoCommitOn = 0;
  1220   1207   
  1221   1208     /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
  1222   1209     ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
  1223   1210     ** each backend. If an error occurs and the return code is still
  1224   1211     ** SQLITE_OK, set the return code to the new error value.
  1225   1212     */
  1226   1213     for(i=0; xFunc && i<db->nDb; i++){ 
................................................................................
  1238   1225         sqlite3VdbeSetChanges(db, p->nChange);
  1239   1226       }else{
  1240   1227         sqlite3VdbeSetChanges(db, 0);
  1241   1228       }
  1242   1229       p->nChange = 0;
  1243   1230     }
  1244   1231   
         1232  +  /* Rollback or commit any schema changes that occurred. */
  1245   1233     if( p->rc!=SQLITE_OK ){
  1246   1234       sqlite3RollbackInternalChanges(db);
  1247   1235     }else if( db->flags & SQLITE_InternChanges ){
  1248   1236       sqlite3CommitInternalChanges(db);
  1249   1237     }
  1250   1238   
  1251         -  if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
         1239  +  /* We have successfully halted and closed the VM.  Record this fact. */
         1240  +  if( p->pc>=0 ){
  1252   1241       db->activeVdbeCnt--;
  1253   1242     }
         1243  +  p->magic = VDBE_MAGIC_HALT;
         1244  +  checkActiveVdbeCnt(db);
  1254   1245   
         1246  +  return SQLITE_OK;
         1247  +}
         1248  +
         1249  +/*
         1250  +** Clean up a VDBE after execution but do not delete the VDBE just yet.
         1251  +** Write any error messages into *pzErrMsg.  Return the result code.
         1252  +**
         1253  +** After this routine is run, the VDBE should be ready to be executed
         1254  +** again.
         1255  +**
         1256  +** To look at it another way, this routine resets the state of the
         1257  +** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
         1258  +** VDBE_MAGIC_INIT.
         1259  +*/
         1260  +int sqlite3VdbeReset(Vdbe *p){
         1261  +  if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
         1262  +    sqlite3Error(p->db, SQLITE_MISUSE, 0 ,0);
         1263  +    return SQLITE_MISUSE;
         1264  +  }
         1265  +
         1266  +  /* If the VM did not run to completion or if it encountered an
         1267  +  ** error, then it might not have been halted properly.  So halt
         1268  +  ** it now.
         1269  +  */
         1270  +  sqlite3VdbeHalt(p);
         1271  +
         1272  +  /* Transfer the error code and error message from the VDBE into the
         1273  +  ** main database structure.
         1274  +  */
         1275  +  if( p->zErrMsg ){
         1276  +    sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
         1277  +    sqliteFree(p->zErrMsg);
         1278  +    p->zErrMsg = 0;
         1279  +  }else if( p->rc ){
         1280  +    sqlite3Error(p->db, p->rc, 0);
         1281  +  }else{
         1282  +    sqlite3Error(p->db, SQLITE_OK, 0);
         1283  +  }
         1284  +
         1285  +  /* Reclaim all memory used by the VDBE
         1286  +  */
         1287  +  Cleanup(p);
         1288  +
         1289  +  /* Save profiling information from this VDBE run.
         1290  +  */
  1255   1291     assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
  1256   1292   #ifdef VDBE_PROFILE
  1257   1293     {
  1258   1294       FILE *out = fopen("vdbe_profile.out", "a");
  1259   1295       if( out ){
  1260   1296         int i;
  1261   1297         fprintf(out, "---- ");
................................................................................
  1275   1311       }
  1276   1312     }
  1277   1313   #endif
  1278   1314     p->magic = VDBE_MAGIC_INIT;
  1279   1315     p->aborted = 0;
  1280   1316     return p->rc;
  1281   1317   }
  1282         -
         1318  + 
  1283   1319   /*
  1284   1320   ** Clean up and delete a VDBE after execution.  Return an integer which is
  1285   1321   ** the result code.  Write any error message text into *pzErrMsg.
  1286   1322   */
  1287   1323   int sqlite3VdbeFinalize(Vdbe *p){
  1288   1324     int rc = SQLITE_OK;
  1289   1325     sqlite *db = p->db;

Changes to test/attach2.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is testing the ATTACH and DETACH commands
    13     13   # and related functionality.
    14     14   #
    15         -# $Id: attach2.test,v 1.25 2004/08/18 16:05:20 drh Exp $
           15  +# $Id: attach2.test,v 1.26 2004/09/02 14:57:09 drh Exp $
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   
    21     21   
    22     22   # Ticket #354
................................................................................
   259    259       INSERT INTO t1 VALUES(1, 2)
   260    260     }
   261    261   } {1 {database is locked}}
   262    262   
   263    263   lock_status 4.9.1 db {main shared temp closed file2 shared}
   264    264   lock_status 4.9.2 db2 {main reserved temp closed file2 reserved}
   265    265   
   266         -btree_breakpoint
   267    266   do_test attach2-4.10 {
   268    267     # We cannot commit db2 while db is holding a read-lock
   269    268     catchsql {COMMIT} db2
   270    269   } {1 {database is locked}}
   271    270   
   272    271   lock_status 4.10.1 db {main shared temp closed file2 shared}
   273    272   lock_status 4.10.2 db2 {main pending temp closed file2 reserved}
   274    273   
   275    274   set sqlite_os_trace 0
   276         -btree_breakpoint
   277    275   do_test attach2-4.11 {
   278    276     # db is able to commit.
   279    277     catchsql {COMMIT}
   280    278   } {0 {}}
   281    279   
   282    280   lock_status 4.11.1 db {main unlocked temp closed file2 unlocked}
   283    281   lock_status 4.11.2 db2 {main pending temp closed file2 reserved}

Changes to test/capi2.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script testing the callback-free C/C++ API.
    13     13   #
    14         -# $Id: capi2.test,v 1.18 2004/06/30 06:30:26 danielk1977 Exp $
           14  +# $Id: capi2.test,v 1.19 2004/09/02 14:57:09 drh Exp $
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Return the text values from the current row pointed at by STMT as a list.
    21     21   proc get_row_values {STMT} {
................................................................................
   199    199          [sqlite3_column_count $VM] \
   200    200          [get_row_values $VM] \
   201    201          [get_column_names $VM]
   202    202   } {SQLITE_DONE 0 {} {}}
   203    203   
   204    204   # Update for v3 - the change has not actually happened until the query is
   205    205   # finalized. Is this going to cause trouble for anyone? Lee Nelson maybe?
   206         -do_test capi2-3.10b {db changes} {0}
          206  +# (Later:) The change now happens just before SQLITE_DONE is returned.
          207  +do_test capi2-3.10b {db changes} {1}
   207    208   do_test capi2-3.11 {
   208    209     sqlite3_finalize $VM
   209    210   } {SQLITE_OK}
   210    211   do_test capi2-3.11b {db changes} {1}
   211    212   do_test capi2-3.12 {
   212    213     sqlite3_finalize $VM
   213    214   } {SQLITE_MISUSE}
................................................................................
   216    217     list [sqlite3_step $VM] \
   217    218          [sqlite3_column_count $VM] \
   218    219          [get_row_values $VM] \
   219    220          [get_column_names $VM]
   220    221   } {SQLITE_ERROR 0 {} {}}
   221    222   
   222    223   # Update for v3: Preparing a statement does not affect the change counter.
   223         -# (Test result changes from 0 to 1).
   224         -do_test capi2-3.13b {db changes} {1}
          224  +# (Test result changes from 0 to 1).  (Later:) change counter updates occur
          225  +# when sqlite3_step returns, not at finalize time.
          226  +do_test capi2-3.13b {db changes} {0}
   225    227   
   226    228   do_test capi2-3.14 {
   227    229     list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
   228    230   } {SQLITE_CONSTRAINT {column a is not unique}}
   229    231   do_test capi2-3.15 {
   230    232     set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
   231    233     set TAIL

Changes to test/capi3.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script testing the callback-free C/C++ API.
    13     13   #
    14         -# $Id: capi3.test,v 1.20 2004/07/22 15:02:26 drh Exp $
           14  +# $Id: capi3.test,v 1.21 2004/09/02 14:57:09 drh Exp $
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Return the UTF-16 representation of the supplied UTF-8 string $str.
    21     21   # If $nt is true, append two 0x00 bytes as a nul terminator.
................................................................................
   532    532         set data [binary format c6a5 {6 23 0 0 0 0} table]
   533    533       }
   534    534       btree_insert $::bc 5 $data
   535    535     
   536    536       btree_close_cursor $::bc
   537    537       btree_commit $::bt
   538    538       btree_close $::bt
   539         -  } {}
          539  +  } {};
   540    540     do_test capi3-8.5 {
   541    541       db close 
   542    542       sqlite3 db test.db
   543    543       catchsql {
   544    544         SELECT * FROM sqlite_master;
   545    545       }
   546    546     } {1 {malformed database schema}}

Added test/capi3b.test.

            1  +# 2004 September 2
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this script testing the callback-free C/C++ API and in
           13  +# particular the behavior of sqlite3_step() when trying to commit
           14  +# with lock contention.
           15  +#
           16  +# $Id: capi3b.test,v 1.1 2004/09/02 14:57:09 drh Exp $
           17  +#
           18  +
           19  +set testdir [file dirname $argv0]
           20  +source $testdir/tester.tcl
           21  +
           22  +
           23  +db close
           24  +set DB [sqlite3 db test.db]
           25  +set DB2 [sqlite3 db2 test.db]
           26  +
           27  +# Create some data in the database
           28  +#
           29  +do_test capi3b-1.1 {
           30  +  execsql {
           31  +    CREATE TABLE t1(x);
           32  +    INSERT INTO t1 VALUES(1);
           33  +    INSERT INTO t1 VALUES(2);
           34  +    SELECT * FROM t1
           35  +  }
           36  +} {1 2}
           37  +
           38  +# Make sure the second database connection can see the data
           39  +#
           40  +do_test capi3b-1.2 {
           41  +  execsql {
           42  +    SELECT * FROM t1
           43  +  } db2
           44  +} {1 2}
           45  +
           46  +# First database connection acquires a shared lock
           47  +#
           48  +do_test capi3b-1.3 {
           49  +  execsql {
           50  +    BEGIN;
           51  +    SELECT * FROM t1;
           52  +  }
           53  +} {1 2}
           54  +
           55  +# Second database connection tries to write.  The sqlite3_step()
           56  +# function returns SQLITE_BUSY because it cannot commit.
           57  +#
           58  +do_test capi3b-1.4 {
           59  +  set VM [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(3)} -1 TAIL]
           60  +  sqlite3_step $VM
           61  +} SQLITE_BUSY
           62  +
           63  +# The sqlite3_step call can be repeated multiple times.
           64  +#
           65  +do_test capi3b-1.5.1 {
           66  +  sqlite3_step $VM
           67  +} SQLITE_BUSY
           68  +do_test capi3b-1.5.2 {
           69  +  sqlite3_step $VM
           70  +} SQLITE_BUSY
           71  +
           72  +# The first connection closes its transaction.  This allows the second
           73  +# connections sqlite3_step to succeed.
           74  +#
           75  +do_test capi3b-1.6 {
           76  +  execsql COMMIT
           77  +  sqlite3_step $VM
           78  +} SQLITE_DONE
           79  +do_test capi3b-1.7 {
           80  +  sqlite3_finalize $VM
           81  +} SQLITE_OK
           82  +do_test capi3b-1.8 {
           83  +  execsql {SELECT * FROM t1} db2
           84  +} {1 2 3}
           85  +do_test capi3b-1.9 {
           86  +  execsql {SELECT * FROM t1}
           87  +} {1 2 3}
           88  +
           89  +catch {db2 close}
           90  +finish_test

Changes to test/quick.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file runs all tests.
    12     12   #
    13         -# $Id: quick.test,v 1.29 2004/08/30 16:52:19 drh Exp $
           13  +# $Id: quick.test,v 1.30 2004/09/02 14:57:09 drh Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   rename finish_test really_finish_test
    18     18   proc finish_test {} {}
    19     19   set ISQUICK 1
    20     20   
    21     21   set EXCLUDE {
    22     22     all.test
    23     23     btree2.test
           24  +  btree3.test
           25  +  btree4.test
           26  +  btree5.test
           27  +  btree6.test
    24     28     corrupt.test
    25     29     crash.test
    26     30     malloc.test
    27     31     memleak.test
    28     32     misuse.test
    29     33     quick.test
    30     34     utf16.test