extensions/sqlite/sqlite-source/vdbeapi.c
changeset 1645 27307df4d711
parent 1203 633c25ce6405
     1.1 --- a/extensions/sqlite/sqlite-source/vdbeapi.c	Sun Oct 28 05:06:12 2007 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/vdbeapi.c	Sun Oct 28 05:48:06 2007 +0000
     1.3 @@ -15,7 +15,6 @@
     1.4  */
     1.5  #include "sqliteInt.h"
     1.6  #include "vdbeInt.h"
     1.7 -#include "os.h"
     1.8  
     1.9  /*
    1.10  ** Return TRUE (non-zero) of the statement supplied as an argument needs
    1.11 @@ -30,6 +29,68 @@
    1.12    return p==0 || p->expired;
    1.13  }
    1.14  
    1.15 +/*
    1.16 +** The following routine destroys a virtual machine that is created by
    1.17 +** the sqlite3_compile() routine. The integer returned is an SQLITE_
    1.18 +** success/failure code that describes the result of executing the virtual
    1.19 +** machine.
    1.20 +**
    1.21 +** This routine sets the error code and string returned by
    1.22 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
    1.23 +*/
    1.24 +int sqlite3_finalize(sqlite3_stmt *pStmt){
    1.25 +  int rc;
    1.26 +  if( pStmt==0 ){
    1.27 +    rc = SQLITE_OK;
    1.28 +  }else{
    1.29 +    Vdbe *v = (Vdbe*)pStmt;
    1.30 +    sqlite3_mutex *mutex = v->db->mutex;
    1.31 +    sqlite3_mutex_enter(mutex);
    1.32 +    rc = sqlite3VdbeFinalize(v);
    1.33 +    sqlite3_mutex_leave(mutex);
    1.34 +  }
    1.35 +  return rc;
    1.36 +}
    1.37 +
    1.38 +/*
    1.39 +** Terminate the current execution of an SQL statement and reset it
    1.40 +** back to its starting state so that it can be reused. A success code from
    1.41 +** the prior execution is returned.
    1.42 +**
    1.43 +** This routine sets the error code and string returned by
    1.44 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
    1.45 +*/
    1.46 +int sqlite3_reset(sqlite3_stmt *pStmt){
    1.47 +  int rc;
    1.48 +  if( pStmt==0 ){
    1.49 +    rc = SQLITE_OK;
    1.50 +  }else{
    1.51 +    Vdbe *v = (Vdbe*)pStmt;
    1.52 +    sqlite3_mutex_enter(v->db->mutex);
    1.53 +    rc = sqlite3VdbeReset(v);
    1.54 +    sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
    1.55 +    assert( (rc & (v->db->errMask))==rc );
    1.56 +    sqlite3_mutex_leave(v->db->mutex);
    1.57 +  }
    1.58 +  return rc;
    1.59 +}
    1.60 +
    1.61 +/*
    1.62 +** Set all the parameters in the compiled SQL statement to NULL.
    1.63 +*/
    1.64 +int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
    1.65 +  int i;
    1.66 +  int rc = SQLITE_OK;
    1.67 +  Vdbe *v = (Vdbe*)pStmt;
    1.68 +  sqlite3_mutex_enter(v->db->mutex);
    1.69 +  for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
    1.70 +    rc = sqlite3_bind_null(pStmt, i);
    1.71 +  }
    1.72 +  sqlite3_mutex_leave(v->db->mutex);
    1.73 +  return rc;
    1.74 +}
    1.75 +
    1.76 +
    1.77  /**************************** sqlite3_value_  *******************************
    1.78  ** The following routines extract information from a Mem or sqlite3_value
    1.79  ** structure.
    1.80 @@ -77,7 +138,6 @@
    1.81  int sqlite3_value_type(sqlite3_value* pVal){
    1.82    return pVal->type;
    1.83  }
    1.84 -/* sqlite3_value_numeric_type() defined in vdbe.c */
    1.85  
    1.86  /**************************** sqlite3_result_  *******************************
    1.87  ** The following routines are used by user-defined functions to specify
    1.88 @@ -90,28 +150,35 @@
    1.89    void (*xDel)(void *)
    1.90  ){
    1.91    assert( n>=0 );
    1.92 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
    1.93    sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
    1.94  }
    1.95  void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
    1.96 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
    1.97    sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
    1.98  }
    1.99  void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   1.100 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.101    pCtx->isError = 1;
   1.102    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
   1.103  }
   1.104  #ifndef SQLITE_OMIT_UTF16
   1.105  void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
   1.106 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.107    pCtx->isError = 1;
   1.108    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   1.109  }
   1.110  #endif
   1.111  void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   1.112 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.113    sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
   1.114  }
   1.115  void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   1.116 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.117    sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
   1.118  }
   1.119  void sqlite3_result_null(sqlite3_context *pCtx){
   1.120 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.121    sqlite3VdbeMemSetNull(&pCtx->s);
   1.122  }
   1.123  void sqlite3_result_text(
   1.124 @@ -120,6 +187,7 @@
   1.125    int n,
   1.126    void (*xDel)(void *)
   1.127  ){
   1.128 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.129    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
   1.130  }
   1.131  #ifndef SQLITE_OMIT_UTF16
   1.132 @@ -129,6 +197,7 @@
   1.133    int n, 
   1.134    void (*xDel)(void *)
   1.135  ){
   1.136 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.137    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
   1.138  }
   1.139  void sqlite3_result_text16be(
   1.140 @@ -137,6 +206,7 @@
   1.141    int n, 
   1.142    void (*xDel)(void *)
   1.143  ){
   1.144 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.145    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
   1.146  }
   1.147  void sqlite3_result_text16le(
   1.148 @@ -145,21 +215,32 @@
   1.149    int n, 
   1.150    void (*xDel)(void *)
   1.151  ){
   1.152 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.153    sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
   1.154  }
   1.155  #endif /* SQLITE_OMIT_UTF16 */
   1.156  void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   1.157 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.158    sqlite3VdbeMemCopy(&pCtx->s, pValue);
   1.159  }
   1.160  void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   1.161 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.162    sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
   1.163  }
   1.164  
   1.165  /* Force an SQLITE_TOOBIG error. */
   1.166  void sqlite3_result_error_toobig(sqlite3_context *pCtx){
   1.167 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.168    sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
   1.169  }
   1.170  
   1.171 +/* An SQLITE_NOMEM error. */
   1.172 +void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   1.173 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.174 +  sqlite3VdbeMemSetNull(&pCtx->s);
   1.175 +  pCtx->isError = 1;
   1.176 +  pCtx->s.db->mallocFailed = 1;
   1.177 +}
   1.178  
   1.179  /*
   1.180  ** Execute the statement pStmt, either until a row of data is ready, the
   1.181 @@ -175,7 +256,8 @@
   1.182    int rc;
   1.183  
   1.184    /* Assert that malloc() has not failed */
   1.185 -  assert( !sqlite3MallocFailed() );
   1.186 +  db = p->db;
   1.187 +  assert( !db->mallocFailed );
   1.188  
   1.189    if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
   1.190      return SQLITE_MISUSE;
   1.191 @@ -190,7 +272,6 @@
   1.192      rc = SQLITE_ERROR;
   1.193      goto end_of_step;
   1.194    }
   1.195 -  db = p->db;
   1.196    if( sqlite3SafetyOn(db) ){
   1.197      p->rc = SQLITE_MISUSE;
   1.198      return SQLITE_MISUSE;
   1.199 @@ -221,7 +302,7 @@
   1.200      }
   1.201      if( db->xProfile && !db->init.busy ){
   1.202        double rNow;
   1.203 -      sqlite3OsCurrentTime(&rNow);
   1.204 +      sqlite3OsCurrentTime(db->pVfs, &rNow);
   1.205        p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
   1.206      }
   1.207  #endif
   1.208 @@ -258,7 +339,7 @@
   1.209      double rNow;
   1.210      u64 elapseTime;
   1.211  
   1.212 -    sqlite3OsCurrentTime(&rNow);
   1.213 +    sqlite3OsCurrentTime(db->pVfs, &rNow);
   1.214      elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
   1.215      assert( p->nOp>0 );
   1.216      assert( p->aOp[p->nOp-1].opcode==OP_Noop );
   1.217 @@ -291,19 +372,27 @@
   1.218  */
   1.219  #ifdef SQLITE_OMIT_PARSER
   1.220  int sqlite3_step(sqlite3_stmt *pStmt){
   1.221 -  return sqlite3Step((Vdbe*)pStmt);
   1.222 +  int rc;
   1.223 +  Vdbe *v;
   1.224 +  v = (Vdbe*)pStmt;
   1.225 +  sqlite3_mutex_enter(v->db->mutex);
   1.226 +  rc = sqlite3Step(v);
   1.227 +  sqlite3_mutex_leave(v->db->mutex);
   1.228 +  return rc;
   1.229  }
   1.230  #else
   1.231  int sqlite3_step(sqlite3_stmt *pStmt){
   1.232    int cnt = 0;
   1.233    int rc;
   1.234    Vdbe *v = (Vdbe*)pStmt;
   1.235 +  sqlite3_mutex_enter(v->db->mutex);
   1.236    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   1.237           && cnt++ < 5
   1.238           && sqlite3Reprepare(v) ){
   1.239      sqlite3_reset(pStmt);
   1.240      v->expired = 0;
   1.241    }
   1.242 +  sqlite3_mutex_leave(v->db->mutex);
   1.243    return rc;
   1.244  }
   1.245  #endif
   1.246 @@ -332,10 +421,10 @@
   1.247  ){
   1.248    const char *zName = context->pFunc->zName;
   1.249    char *zErr;
   1.250 -  zErr = sqlite3MPrintf(
   1.251 +  zErr = sqlite3MPrintf(0,
   1.252        "unable to use function %s in the requested context", zName);
   1.253    sqlite3_result_error(context, zErr, -1);
   1.254 -  sqliteFree(zErr);
   1.255 +  sqlite3_free(zErr);
   1.256  }
   1.257  
   1.258  /*
   1.259 @@ -344,21 +433,23 @@
   1.260  ** same context that was returned on prior calls.
   1.261  */
   1.262  void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   1.263 -  Mem *pMem = p->pMem;
   1.264 +  Mem *pMem;
   1.265    assert( p && p->pFunc && p->pFunc->xStep );
   1.266 +  assert( sqlite3_mutex_held(p->s.db->mutex) );
   1.267 +  pMem = p->pMem;
   1.268    if( (pMem->flags & MEM_Agg)==0 ){
   1.269      if( nByte==0 ){
   1.270        assert( pMem->flags==MEM_Null );
   1.271        pMem->z = 0;
   1.272      }else{
   1.273        pMem->flags = MEM_Agg;
   1.274 -      pMem->xDel = sqlite3FreeX;
   1.275 +      pMem->xDel = sqlite3_free;
   1.276        pMem->u.pDef = p->pFunc;
   1.277        if( nByte<=NBFS ){
   1.278          pMem->z = pMem->zShort;
   1.279          memset(pMem->z, 0, nByte);
   1.280        }else{
   1.281 -        pMem->z = sqliteMalloc( nByte );
   1.282 +        pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
   1.283        }
   1.284      }
   1.285    }
   1.286 @@ -370,7 +461,10 @@
   1.287  ** the user-function defined by pCtx.
   1.288  */
   1.289  void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   1.290 -  VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
   1.291 +  VdbeFunc *pVdbeFunc;
   1.292 +
   1.293 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.294 +  pVdbeFunc = pCtx->pVdbeFunc;
   1.295    if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
   1.296      return 0;
   1.297    }
   1.298 @@ -390,16 +484,19 @@
   1.299  ){
   1.300    struct AuxData *pAuxData;
   1.301    VdbeFunc *pVdbeFunc;
   1.302 -  if( iArg<0 ) return;
   1.303 +  if( iArg<0 ) goto failed;
   1.304  
   1.305 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.306    pVdbeFunc = pCtx->pVdbeFunc;
   1.307    if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
   1.308 +    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
   1.309      int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
   1.310 -    pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
   1.311 -    if( !pVdbeFunc ) return;
   1.312 +    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
   1.313 +    if( !pVdbeFunc ){
   1.314 +      goto failed;
   1.315 +    }
   1.316      pCtx->pVdbeFunc = pVdbeFunc;
   1.317 -    memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 
   1.318 -             sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
   1.319 +    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
   1.320      pVdbeFunc->nAux = iArg+1;
   1.321      pVdbeFunc->pFunc = pCtx->pFunc;
   1.322    }
   1.323 @@ -410,6 +507,12 @@
   1.324    }
   1.325    pAuxData->pAux = pAux;
   1.326    pAuxData->xDelete = xDelete;
   1.327 +  return;
   1.328 +
   1.329 +failed:
   1.330 +  if( xDelete ){
   1.331 +    xDelete(pAux);
   1.332 +  }
   1.333  }
   1.334  
   1.335  /*
   1.336 @@ -452,14 +555,24 @@
   1.337  ** of NULL.
   1.338  */
   1.339  static Mem *columnMem(sqlite3_stmt *pStmt, int i){
   1.340 -  Vdbe *pVm = (Vdbe *)pStmt;
   1.341 -  int vals = sqlite3_data_count(pStmt);
   1.342 -  if( pVm==0 || pVm->resOnStack==0 || i>=pVm->nResColumn || i<0 ){
   1.343 -    static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL };
   1.344 -    sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   1.345 -    return (Mem*)&nullMem;
   1.346 +  Vdbe *pVm;
   1.347 +  int vals;
   1.348 +  Mem *pOut;
   1.349 +
   1.350 +  pVm = (Vdbe *)pStmt;
   1.351 +  if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
   1.352 +    sqlite3_mutex_enter(pVm->db->mutex);
   1.353 +    vals = sqlite3_data_count(pStmt);
   1.354 +    pOut = &pVm->pTos[(1-vals)+i];
   1.355 +  }else{
   1.356 +    static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
   1.357 +    if( pVm->db ){
   1.358 +      sqlite3_mutex_enter(pVm->db->mutex);
   1.359 +      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   1.360 +    }
   1.361 +    pOut = (Mem*)&nullMem;
   1.362    }
   1.363 -  return &pVm->pTos[(1-vals)+i];
   1.364 +  return pOut;
   1.365  }
   1.366  
   1.367  /*
   1.368 @@ -469,7 +582,7 @@
   1.369  ** malloc() has failed, the threads mallocFailed flag is cleared and the result
   1.370  ** code of statement pStmt set to SQLITE_NOMEM.
   1.371  **
   1.372 -** Specificly, this is called from within:
   1.373 +** Specifically, this is called from within:
   1.374  **
   1.375  **     sqlite3_column_int()
   1.376  **     sqlite3_column_int64()
   1.377 @@ -489,7 +602,10 @@
   1.378    ** and _finalize() will return NOMEM.
   1.379    */
   1.380    Vdbe *p = (Vdbe *)pStmt;
   1.381 -  p->rc = sqlite3ApiExit(0, p->rc);
   1.382 +  if( p ){
   1.383 +    p->rc = sqlite3ApiExit(p->db, p->rc);
   1.384 +    sqlite3_mutex_leave(p->db->mutex);
   1.385 +  }
   1.386  }
   1.387  
   1.388  /**************************** sqlite3_column_  *******************************
   1.389 @@ -537,7 +653,9 @@
   1.390    return val;
   1.391  }
   1.392  sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
   1.393 -  return columnMem(pStmt, i);
   1.394 +  sqlite3_value *pOut = columnMem(pStmt, i);
   1.395 +  columnMallocFailure(pStmt);
   1.396 +  return pOut;
   1.397  }
   1.398  #ifndef SQLITE_OMIT_UTF16
   1.399  const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
   1.400 @@ -547,7 +665,9 @@
   1.401  }
   1.402  #endif /* SQLITE_OMIT_UTF16 */
   1.403  int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
   1.404 -  return sqlite3_value_type( columnMem(pStmt,i) );
   1.405 +  int iType = sqlite3_value_type( columnMem(pStmt,i) );
   1.406 +  columnMallocFailure(pStmt);
   1.407 +  return iType;
   1.408  }
   1.409  
   1.410  /* The following function is experimental and subject to change or
   1.411 @@ -579,20 +699,28 @@
   1.412    const void *(*xFunc)(Mem*),
   1.413    int useType
   1.414  ){
   1.415 -  const void *ret;
   1.416 +  const void *ret = 0;
   1.417    Vdbe *p = (Vdbe *)pStmt;
   1.418 -  int n = sqlite3_column_count(pStmt);
   1.419 +  int n;
   1.420 +  
   1.421 +
   1.422 +  if( p!=0 ){
   1.423 +    n = sqlite3_column_count(pStmt);
   1.424 +    if( N<n && N>=0 ){
   1.425 +      N += useType*n;
   1.426 +      sqlite3_mutex_enter(p->db->mutex);
   1.427 +      ret = xFunc(&p->aColName[N]);
   1.428  
   1.429 -  if( p==0 || N>=n || N<0 ){
   1.430 -    return 0;
   1.431 +      /* A malloc may have failed inside of the xFunc() call. If this
   1.432 +      ** is the case, clear the mallocFailed flag and return NULL.
   1.433 +      */
   1.434 +      if( p->db && p->db->mallocFailed ){
   1.435 +        p->db->mallocFailed = 0;
   1.436 +        ret = 0;
   1.437 +      }
   1.438 +      sqlite3_mutex_leave(p->db->mutex);
   1.439 +    }
   1.440    }
   1.441 -  N += useType*n;
   1.442 -  ret = xFunc(&p->aColName[N]);
   1.443 -
   1.444 -  /* A malloc may have failed inside of the xFunc() call. If this is the case,
   1.445 -  ** clear the mallocFailed flag and return NULL.
   1.446 -  */
   1.447 -  sqlite3ApiExit(0, 0);
   1.448    return ret;
   1.449  }
   1.450  
   1.451 @@ -711,29 +839,33 @@
   1.452  ** Bind a text or BLOB value.
   1.453  */
   1.454  static int bindText(
   1.455 -  sqlite3_stmt *pStmt, 
   1.456 -  int i, 
   1.457 -  const void *zData, 
   1.458 -  int nData, 
   1.459 -  void (*xDel)(void*),
   1.460 -  int encoding
   1.461 +  sqlite3_stmt *pStmt,   /* The statement to bind against */
   1.462 +  int i,                 /* Index of the parameter to bind */
   1.463 +  const void *zData,     /* Pointer to the data to be bound */
   1.464 +  int nData,             /* Number of bytes of data to be bound */
   1.465 +  void (*xDel)(void*),   /* Destructor for the data */
   1.466 +  int encoding           /* Encoding for the data */
   1.467  ){
   1.468    Vdbe *p = (Vdbe *)pStmt;
   1.469    Mem *pVar;
   1.470    int rc;
   1.471  
   1.472 -  rc = vdbeUnbind(p, i);
   1.473 -  if( rc || zData==0 ){
   1.474 -    return rc;
   1.475 +  if( p==0 ){
   1.476 +    return SQLITE_MISUSE;
   1.477    }
   1.478 -  pVar = &p->aVar[i-1];
   1.479 -  rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
   1.480 -  if( rc==SQLITE_OK && encoding!=0 ){
   1.481 -    rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
   1.482 +  sqlite3_mutex_enter(p->db->mutex);
   1.483 +  rc = vdbeUnbind(p, i);
   1.484 +  if( rc==SQLITE_OK && zData!=0 ){
   1.485 +    pVar = &p->aVar[i-1];
   1.486 +    rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
   1.487 +    if( rc==SQLITE_OK && encoding!=0 ){
   1.488 +      rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
   1.489 +    }
   1.490 +    sqlite3Error(p->db, rc, 0);
   1.491 +    rc = sqlite3ApiExit(p->db, rc);
   1.492    }
   1.493 -
   1.494 -  sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
   1.495 -  return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
   1.496 +  sqlite3_mutex_leave(p->db->mutex);
   1.497 +  return rc;
   1.498  }
   1.499  
   1.500  
   1.501 @@ -752,10 +884,12 @@
   1.502  int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
   1.503    int rc;
   1.504    Vdbe *p = (Vdbe *)pStmt;
   1.505 +  sqlite3_mutex_enter(p->db->mutex);
   1.506    rc = vdbeUnbind(p, i);
   1.507    if( rc==SQLITE_OK ){
   1.508      sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
   1.509    }
   1.510 +  sqlite3_mutex_leave(p->db->mutex);
   1.511    return rc;
   1.512  }
   1.513  int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
   1.514 @@ -764,14 +898,21 @@
   1.515  int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
   1.516    int rc;
   1.517    Vdbe *p = (Vdbe *)pStmt;
   1.518 +  sqlite3_mutex_enter(p->db->mutex);
   1.519    rc = vdbeUnbind(p, i);
   1.520    if( rc==SQLITE_OK ){
   1.521      sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
   1.522    }
   1.523 +  sqlite3_mutex_leave(p->db->mutex);
   1.524    return rc;
   1.525  }
   1.526 -int sqlite3_bind_null(sqlite3_stmt* p, int i){
   1.527 -  return vdbeUnbind((Vdbe *)p, i);
   1.528 +int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
   1.529 +  int rc;
   1.530 +  Vdbe *p = (Vdbe*)pStmt;
   1.531 +  sqlite3_mutex_enter(p->db->mutex);
   1.532 +  rc = vdbeUnbind(p, i);
   1.533 +  sqlite3_mutex_leave(p->db->mutex);
   1.534 +  return rc;
   1.535  }
   1.536  int sqlite3_bind_text( 
   1.537    sqlite3_stmt *pStmt, 
   1.538 @@ -796,19 +937,23 @@
   1.539  int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
   1.540    int rc;
   1.541    Vdbe *p = (Vdbe *)pStmt;
   1.542 +  sqlite3_mutex_enter(p->db->mutex);
   1.543    rc = vdbeUnbind(p, i);
   1.544    if( rc==SQLITE_OK ){
   1.545 -    sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
   1.546 +    rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
   1.547    }
   1.548 +  sqlite3_mutex_leave(p->db->mutex);
   1.549    return rc;
   1.550  }
   1.551  int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
   1.552    int rc;
   1.553    Vdbe *p = (Vdbe *)pStmt;
   1.554 +  sqlite3_mutex_enter(p->db->mutex);
   1.555    rc = vdbeUnbind(p, i);
   1.556    if( rc==SQLITE_OK ){
   1.557      sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
   1.558    }
   1.559 +  sqlite3_mutex_leave(p->db->mutex);
   1.560    return rc;
   1.561  }
   1.562  
   1.563 @@ -828,15 +973,19 @@
   1.564  */
   1.565  static void createVarMap(Vdbe *p){
   1.566    if( !p->okVar ){
   1.567 -    int j;
   1.568 -    Op *pOp;
   1.569 -    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
   1.570 -      if( pOp->opcode==OP_Variable ){
   1.571 -        assert( pOp->p1>0 && pOp->p1<=p->nVar );
   1.572 -        p->azVar[pOp->p1-1] = pOp->p3;
   1.573 +    sqlite3_mutex_enter(p->db->mutex);
   1.574 +    if( !p->okVar ){
   1.575 +      int j;
   1.576 +      Op *pOp;
   1.577 +      for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
   1.578 +        if( pOp->opcode==OP_Variable ){
   1.579 +          assert( pOp->p1>0 && pOp->p1<=p->nVar );
   1.580 +          p->azVar[pOp->p1-1] = pOp->p3;
   1.581 +        }
   1.582        }
   1.583 +      p->okVar = 1;
   1.584      }
   1.585 -    p->okVar = 1;
   1.586 +    sqlite3_mutex_leave(p->db->mutex);
   1.587    }
   1.588  }
   1.589  
   1.590 @@ -888,17 +1037,20 @@
   1.591    Vdbe *pTo = (Vdbe*)pToStmt;
   1.592    int i, rc = SQLITE_OK;
   1.593    if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
   1.594 -    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
   1.595 +    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
   1.596 +    || pTo->db!=pFrom->db ){
   1.597      return SQLITE_MISUSE;
   1.598    }
   1.599    if( pFrom->nVar!=pTo->nVar ){
   1.600      return SQLITE_ERROR;
   1.601    }
   1.602 +  sqlite3_mutex_enter(pTo->db->mutex);
   1.603    for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
   1.604      sqlite3MallocDisallow();
   1.605      rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
   1.606      sqlite3MallocAllow();
   1.607    }
   1.608 +  sqlite3_mutex_leave(pTo->db->mutex);
   1.609    assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
   1.610    return rc;
   1.611  }