1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/vdbeapi.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,1066 @@
1.4 +/*
1.5 +** 2004 May 26
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +**
1.16 +** This file contains code use to implement APIs that are part of the
1.17 +** VDBE.
1.18 +*/
1.19 +#include "sqliteInt.h"
1.20 +#include "vdbeInt.h"
1.21 +
1.22 +/*
1.23 +** Return TRUE (non-zero) of the statement supplied as an argument needs
1.24 +** to be recompiled. A statement needs to be recompiled whenever the
1.25 +** execution environment changes in a way that would alter the program
1.26 +** that sqlite3_prepare() generates. For example, if new functions or
1.27 +** collating sequences are registered or if an authorizer function is
1.28 +** added or changed.
1.29 +*/
1.30 +int sqlite3_expired(sqlite3_stmt *pStmt){
1.31 + Vdbe *p = (Vdbe*)pStmt;
1.32 + return p==0 || p->expired;
1.33 +}
1.34 +
1.35 +/*
1.36 +** The following routine destroys a virtual machine that is created by
1.37 +** the sqlite3_compile() routine. The integer returned is an SQLITE_
1.38 +** success/failure code that describes the result of executing the virtual
1.39 +** machine.
1.40 +**
1.41 +** This routine sets the error code and string returned by
1.42 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1.43 +*/
1.44 +int sqlite3_finalize(sqlite3_stmt *pStmt){
1.45 + int rc;
1.46 + if( pStmt==0 ){
1.47 + rc = SQLITE_OK;
1.48 + }else{
1.49 + Vdbe *v = (Vdbe*)pStmt;
1.50 + sqlite3_mutex *mutex = v->db->mutex;
1.51 + sqlite3_mutex_enter(mutex);
1.52 + rc = sqlite3VdbeFinalize(v);
1.53 + sqlite3_mutex_leave(mutex);
1.54 + }
1.55 + return rc;
1.56 +}
1.57 +
1.58 +/*
1.59 +** Terminate the current execution of an SQL statement and reset it
1.60 +** back to its starting state so that it can be reused. A success code from
1.61 +** the prior execution is returned.
1.62 +**
1.63 +** This routine sets the error code and string returned by
1.64 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1.65 +*/
1.66 +int sqlite3_reset(sqlite3_stmt *pStmt){
1.67 + int rc;
1.68 + if( pStmt==0 ){
1.69 + rc = SQLITE_OK;
1.70 + }else{
1.71 + Vdbe *v = (Vdbe*)pStmt;
1.72 + sqlite3_mutex_enter(v->db->mutex);
1.73 + rc = sqlite3VdbeReset(v);
1.74 + sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
1.75 + assert( (rc & (v->db->errMask))==rc );
1.76 + sqlite3_mutex_leave(v->db->mutex);
1.77 + }
1.78 + return rc;
1.79 +}
1.80 +
1.81 +/*
1.82 +** Set all the parameters in the compiled SQL statement to NULL.
1.83 +*/
1.84 +int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
1.85 + int i;
1.86 + int rc = SQLITE_OK;
1.87 + Vdbe *v = (Vdbe*)pStmt;
1.88 + sqlite3_mutex_enter(v->db->mutex);
1.89 + for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
1.90 + rc = sqlite3_bind_null(pStmt, i);
1.91 + }
1.92 + sqlite3_mutex_leave(v->db->mutex);
1.93 + return rc;
1.94 +}
1.95 +
1.96 +
1.97 +/**************************** sqlite3_value_ *******************************
1.98 +** The following routines extract information from a Mem or sqlite3_value
1.99 +** structure.
1.100 +*/
1.101 +const void *sqlite3_value_blob(sqlite3_value *pVal){
1.102 + Mem *p = (Mem*)pVal;
1.103 + if( p->flags & (MEM_Blob|MEM_Str) ){
1.104 + sqlite3VdbeMemExpandBlob(p);
1.105 + p->flags &= ~MEM_Str;
1.106 + p->flags |= MEM_Blob;
1.107 + return p->z;
1.108 + }else{
1.109 + return sqlite3_value_text(pVal);
1.110 + }
1.111 +}
1.112 +int sqlite3_value_bytes(sqlite3_value *pVal){
1.113 + return sqlite3ValueBytes(pVal, SQLITE_UTF8);
1.114 +}
1.115 +int sqlite3_value_bytes16(sqlite3_value *pVal){
1.116 + return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
1.117 +}
1.118 +double sqlite3_value_double(sqlite3_value *pVal){
1.119 + return sqlite3VdbeRealValue((Mem*)pVal);
1.120 +}
1.121 +int sqlite3_value_int(sqlite3_value *pVal){
1.122 + return sqlite3VdbeIntValue((Mem*)pVal);
1.123 +}
1.124 +sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
1.125 + return sqlite3VdbeIntValue((Mem*)pVal);
1.126 +}
1.127 +const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
1.128 + return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
1.129 +}
1.130 +#ifndef SQLITE_OMIT_UTF16
1.131 +const void *sqlite3_value_text16(sqlite3_value* pVal){
1.132 + return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1.133 +}
1.134 +const void *sqlite3_value_text16be(sqlite3_value *pVal){
1.135 + return sqlite3ValueText(pVal, SQLITE_UTF16BE);
1.136 +}
1.137 +const void *sqlite3_value_text16le(sqlite3_value *pVal){
1.138 + return sqlite3ValueText(pVal, SQLITE_UTF16LE);
1.139 +}
1.140 +#endif /* SQLITE_OMIT_UTF16 */
1.141 +int sqlite3_value_type(sqlite3_value* pVal){
1.142 + return pVal->type;
1.143 +}
1.144 +
1.145 +/**************************** sqlite3_result_ *******************************
1.146 +** The following routines are used by user-defined functions to specify
1.147 +** the function result.
1.148 +*/
1.149 +void sqlite3_result_blob(
1.150 + sqlite3_context *pCtx,
1.151 + const void *z,
1.152 + int n,
1.153 + void (*xDel)(void *)
1.154 +){
1.155 + assert( n>=0 );
1.156 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.157 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
1.158 +}
1.159 +void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
1.160 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.161 + sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
1.162 +}
1.163 +void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
1.164 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.165 + pCtx->isError = 1;
1.166 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
1.167 +}
1.168 +#ifndef SQLITE_OMIT_UTF16
1.169 +void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
1.170 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.171 + pCtx->isError = 1;
1.172 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
1.173 +}
1.174 +#endif
1.175 +void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
1.176 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.177 + sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
1.178 +}
1.179 +void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
1.180 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.181 + sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
1.182 +}
1.183 +void sqlite3_result_null(sqlite3_context *pCtx){
1.184 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.185 + sqlite3VdbeMemSetNull(&pCtx->s);
1.186 +}
1.187 +void sqlite3_result_text(
1.188 + sqlite3_context *pCtx,
1.189 + const char *z,
1.190 + int n,
1.191 + void (*xDel)(void *)
1.192 +){
1.193 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.194 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
1.195 +}
1.196 +#ifndef SQLITE_OMIT_UTF16
1.197 +void sqlite3_result_text16(
1.198 + sqlite3_context *pCtx,
1.199 + const void *z,
1.200 + int n,
1.201 + void (*xDel)(void *)
1.202 +){
1.203 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.204 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
1.205 +}
1.206 +void sqlite3_result_text16be(
1.207 + sqlite3_context *pCtx,
1.208 + const void *z,
1.209 + int n,
1.210 + void (*xDel)(void *)
1.211 +){
1.212 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.213 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
1.214 +}
1.215 +void sqlite3_result_text16le(
1.216 + sqlite3_context *pCtx,
1.217 + const void *z,
1.218 + int n,
1.219 + void (*xDel)(void *)
1.220 +){
1.221 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.222 + sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
1.223 +}
1.224 +#endif /* SQLITE_OMIT_UTF16 */
1.225 +void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
1.226 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.227 + sqlite3VdbeMemCopy(&pCtx->s, pValue);
1.228 +}
1.229 +void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
1.230 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.231 + sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
1.232 +}
1.233 +
1.234 +/* Force an SQLITE_TOOBIG error. */
1.235 +void sqlite3_result_error_toobig(sqlite3_context *pCtx){
1.236 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.237 + sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
1.238 +}
1.239 +
1.240 +/* An SQLITE_NOMEM error. */
1.241 +void sqlite3_result_error_nomem(sqlite3_context *pCtx){
1.242 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.243 + sqlite3VdbeMemSetNull(&pCtx->s);
1.244 + pCtx->isError = 1;
1.245 + pCtx->s.db->mallocFailed = 1;
1.246 +}
1.247 +
1.248 +/*
1.249 +** Execute the statement pStmt, either until a row of data is ready, the
1.250 +** statement is completely executed or an error occurs.
1.251 +**
1.252 +** This routine implements the bulk of the logic behind the sqlite_step()
1.253 +** API. The only thing omitted is the automatic recompile if a
1.254 +** schema change has occurred. That detail is handled by the
1.255 +** outer sqlite3_step() wrapper procedure.
1.256 +*/
1.257 +static int sqlite3Step(Vdbe *p){
1.258 + sqlite3 *db;
1.259 + int rc;
1.260 +
1.261 + /* Assert that malloc() has not failed */
1.262 + db = p->db;
1.263 + assert( !db->mallocFailed );
1.264 +
1.265 + if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
1.266 + return SQLITE_MISUSE;
1.267 + }
1.268 + if( p->aborted ){
1.269 + return SQLITE_ABORT;
1.270 + }
1.271 + if( p->pc<=0 && p->expired ){
1.272 + if( p->rc==SQLITE_OK ){
1.273 + p->rc = SQLITE_SCHEMA;
1.274 + }
1.275 + rc = SQLITE_ERROR;
1.276 + goto end_of_step;
1.277 + }
1.278 + if( sqlite3SafetyOn(db) ){
1.279 + p->rc = SQLITE_MISUSE;
1.280 + return SQLITE_MISUSE;
1.281 + }
1.282 + if( p->pc<0 ){
1.283 + /* If there are no other statements currently running, then
1.284 + ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
1.285 + ** from interrupting a statement that has not yet started.
1.286 + */
1.287 + if( db->activeVdbeCnt==0 ){
1.288 + db->u1.isInterrupted = 0;
1.289 + }
1.290 +
1.291 +#ifndef SQLITE_OMIT_TRACE
1.292 + /* Invoke the trace callback if there is one
1.293 + */
1.294 + if( db->xTrace && !db->init.busy ){
1.295 + assert( p->nOp>0 );
1.296 + assert( p->aOp[p->nOp-1].opcode==OP_Noop );
1.297 + assert( p->aOp[p->nOp-1].p3!=0 );
1.298 + assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
1.299 + sqlite3SafetyOff(db);
1.300 + db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
1.301 + if( sqlite3SafetyOn(db) ){
1.302 + p->rc = SQLITE_MISUSE;
1.303 + return SQLITE_MISUSE;
1.304 + }
1.305 + }
1.306 + if( db->xProfile && !db->init.busy ){
1.307 + double rNow;
1.308 + sqlite3OsCurrentTime(db->pVfs, &rNow);
1.309 + p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
1.310 + }
1.311 +#endif
1.312 +
1.313 + /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
1.314 + ** on in debugging mode.
1.315 + */
1.316 +#ifdef SQLITE_DEBUG
1.317 + if( (db->flags & SQLITE_SqlTrace)!=0 ){
1.318 + sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
1.319 + }
1.320 +#endif /* SQLITE_DEBUG */
1.321 +
1.322 + db->activeVdbeCnt++;
1.323 + p->pc = 0;
1.324 + }
1.325 +#ifndef SQLITE_OMIT_EXPLAIN
1.326 + if( p->explain ){
1.327 + rc = sqlite3VdbeList(p);
1.328 + }else
1.329 +#endif /* SQLITE_OMIT_EXPLAIN */
1.330 + {
1.331 + rc = sqlite3VdbeExec(p);
1.332 + }
1.333 +
1.334 + if( sqlite3SafetyOff(db) ){
1.335 + rc = SQLITE_MISUSE;
1.336 + }
1.337 +
1.338 +#ifndef SQLITE_OMIT_TRACE
1.339 + /* Invoke the profile callback if there is one
1.340 + */
1.341 + if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
1.342 + double rNow;
1.343 + u64 elapseTime;
1.344 +
1.345 + sqlite3OsCurrentTime(db->pVfs, &rNow);
1.346 + elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
1.347 + assert( p->nOp>0 );
1.348 + assert( p->aOp[p->nOp-1].opcode==OP_Noop );
1.349 + assert( p->aOp[p->nOp-1].p3!=0 );
1.350 + assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
1.351 + db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
1.352 + }
1.353 +#endif
1.354 +
1.355 + sqlite3Error(p->db, rc, 0);
1.356 + p->rc = sqlite3ApiExit(p->db, p->rc);
1.357 +end_of_step:
1.358 + assert( (rc&0xff)==rc );
1.359 + if( p->zSql && (rc&0xff)<SQLITE_ROW ){
1.360 + /* This behavior occurs if sqlite3_prepare_v2() was used to build
1.361 + ** the prepared statement. Return error codes directly */
1.362 + sqlite3Error(p->db, p->rc, 0);
1.363 + return p->rc;
1.364 + }else{
1.365 + /* This is for legacy sqlite3_prepare() builds and when the code
1.366 + ** is SQLITE_ROW or SQLITE_DONE */
1.367 + return rc;
1.368 + }
1.369 +}
1.370 +
1.371 +/*
1.372 +** This is the top-level implementation of sqlite3_step(). Call
1.373 +** sqlite3Step() to do most of the work. If a schema error occurs,
1.374 +** call sqlite3Reprepare() and try again.
1.375 +*/
1.376 +#ifdef SQLITE_OMIT_PARSER
1.377 +int sqlite3_step(sqlite3_stmt *pStmt){
1.378 + int rc;
1.379 + Vdbe *v;
1.380 + v = (Vdbe*)pStmt;
1.381 + sqlite3_mutex_enter(v->db->mutex);
1.382 + rc = sqlite3Step(v);
1.383 + sqlite3_mutex_leave(v->db->mutex);
1.384 + return rc;
1.385 +}
1.386 +#else
1.387 +int sqlite3_step(sqlite3_stmt *pStmt){
1.388 + int cnt = 0;
1.389 + int rc;
1.390 + Vdbe *v = (Vdbe*)pStmt;
1.391 + sqlite3_mutex_enter(v->db->mutex);
1.392 + while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
1.393 + && cnt++ < 5
1.394 + && sqlite3Reprepare(v) ){
1.395 + sqlite3_reset(pStmt);
1.396 + v->expired = 0;
1.397 + }
1.398 + sqlite3_mutex_leave(v->db->mutex);
1.399 + return rc;
1.400 +}
1.401 +#endif
1.402 +
1.403 +/*
1.404 +** Extract the user data from a sqlite3_context structure and return a
1.405 +** pointer to it.
1.406 +*/
1.407 +void *sqlite3_user_data(sqlite3_context *p){
1.408 + assert( p && p->pFunc );
1.409 + return p->pFunc->pUserData;
1.410 +}
1.411 +
1.412 +/*
1.413 +** The following is the implementation of an SQL function that always
1.414 +** fails with an error message stating that the function is used in the
1.415 +** wrong context. The sqlite3_overload_function() API might construct
1.416 +** SQL function that use this routine so that the functions will exist
1.417 +** for name resolution but are actually overloaded by the xFindFunction
1.418 +** method of virtual tables.
1.419 +*/
1.420 +void sqlite3InvalidFunction(
1.421 + sqlite3_context *context, /* The function calling context */
1.422 + int argc, /* Number of arguments to the function */
1.423 + sqlite3_value **argv /* Value of each argument */
1.424 +){
1.425 + const char *zName = context->pFunc->zName;
1.426 + char *zErr;
1.427 + zErr = sqlite3MPrintf(0,
1.428 + "unable to use function %s in the requested context", zName);
1.429 + sqlite3_result_error(context, zErr, -1);
1.430 + sqlite3_free(zErr);
1.431 +}
1.432 +
1.433 +/*
1.434 +** Allocate or return the aggregate context for a user function. A new
1.435 +** context is allocated on the first call. Subsequent calls return the
1.436 +** same context that was returned on prior calls.
1.437 +*/
1.438 +void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
1.439 + Mem *pMem;
1.440 + assert( p && p->pFunc && p->pFunc->xStep );
1.441 + assert( sqlite3_mutex_held(p->s.db->mutex) );
1.442 + pMem = p->pMem;
1.443 + if( (pMem->flags & MEM_Agg)==0 ){
1.444 + if( nByte==0 ){
1.445 + assert( pMem->flags==MEM_Null );
1.446 + pMem->z = 0;
1.447 + }else{
1.448 + pMem->flags = MEM_Agg;
1.449 + pMem->xDel = sqlite3_free;
1.450 + pMem->u.pDef = p->pFunc;
1.451 + if( nByte<=NBFS ){
1.452 + pMem->z = pMem->zShort;
1.453 + memset(pMem->z, 0, nByte);
1.454 + }else{
1.455 + pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
1.456 + }
1.457 + }
1.458 + }
1.459 + return (void*)pMem->z;
1.460 +}
1.461 +
1.462 +/*
1.463 +** Return the auxilary data pointer, if any, for the iArg'th argument to
1.464 +** the user-function defined by pCtx.
1.465 +*/
1.466 +void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
1.467 + VdbeFunc *pVdbeFunc;
1.468 +
1.469 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.470 + pVdbeFunc = pCtx->pVdbeFunc;
1.471 + if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
1.472 + return 0;
1.473 + }
1.474 + return pVdbeFunc->apAux[iArg].pAux;
1.475 +}
1.476 +
1.477 +/*
1.478 +** Set the auxilary data pointer and delete function, for the iArg'th
1.479 +** argument to the user-function defined by pCtx. Any previous value is
1.480 +** deleted by calling the delete function specified when it was set.
1.481 +*/
1.482 +void sqlite3_set_auxdata(
1.483 + sqlite3_context *pCtx,
1.484 + int iArg,
1.485 + void *pAux,
1.486 + void (*xDelete)(void*)
1.487 +){
1.488 + struct AuxData *pAuxData;
1.489 + VdbeFunc *pVdbeFunc;
1.490 + if( iArg<0 ) goto failed;
1.491 +
1.492 + assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
1.493 + pVdbeFunc = pCtx->pVdbeFunc;
1.494 + if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
1.495 + int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
1.496 + int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
1.497 + pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
1.498 + if( !pVdbeFunc ){
1.499 + goto failed;
1.500 + }
1.501 + pCtx->pVdbeFunc = pVdbeFunc;
1.502 + memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
1.503 + pVdbeFunc->nAux = iArg+1;
1.504 + pVdbeFunc->pFunc = pCtx->pFunc;
1.505 + }
1.506 +
1.507 + pAuxData = &pVdbeFunc->apAux[iArg];
1.508 + if( pAuxData->pAux && pAuxData->xDelete ){
1.509 + pAuxData->xDelete(pAuxData->pAux);
1.510 + }
1.511 + pAuxData->pAux = pAux;
1.512 + pAuxData->xDelete = xDelete;
1.513 + return;
1.514 +
1.515 +failed:
1.516 + if( xDelete ){
1.517 + xDelete(pAux);
1.518 + }
1.519 +}
1.520 +
1.521 +/*
1.522 +** Return the number of times the Step function of a aggregate has been
1.523 +** called.
1.524 +**
1.525 +** This function is deprecated. Do not use it for new code. It is
1.526 +** provide only to avoid breaking legacy code. New aggregate function
1.527 +** implementations should keep their own counts within their aggregate
1.528 +** context.
1.529 +*/
1.530 +int sqlite3_aggregate_count(sqlite3_context *p){
1.531 + assert( p && p->pFunc && p->pFunc->xStep );
1.532 + return p->pMem->n;
1.533 +}
1.534 +
1.535 +/*
1.536 +** Return the number of columns in the result set for the statement pStmt.
1.537 +*/
1.538 +int sqlite3_column_count(sqlite3_stmt *pStmt){
1.539 + Vdbe *pVm = (Vdbe *)pStmt;
1.540 + return pVm ? pVm->nResColumn : 0;
1.541 +}
1.542 +
1.543 +/*
1.544 +** Return the number of values available from the current row of the
1.545 +** currently executing statement pStmt.
1.546 +*/
1.547 +int sqlite3_data_count(sqlite3_stmt *pStmt){
1.548 + Vdbe *pVm = (Vdbe *)pStmt;
1.549 + if( pVm==0 || !pVm->resOnStack ) return 0;
1.550 + return pVm->nResColumn;
1.551 +}
1.552 +
1.553 +
1.554 +/*
1.555 +** Check to see if column iCol of the given statement is valid. If
1.556 +** it is, return a pointer to the Mem for the value of that column.
1.557 +** If iCol is not valid, return a pointer to a Mem which has a value
1.558 +** of NULL.
1.559 +*/
1.560 +static Mem *columnMem(sqlite3_stmt *pStmt, int i){
1.561 + Vdbe *pVm;
1.562 + int vals;
1.563 + Mem *pOut;
1.564 +
1.565 + pVm = (Vdbe *)pStmt;
1.566 + if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
1.567 + sqlite3_mutex_enter(pVm->db->mutex);
1.568 + vals = sqlite3_data_count(pStmt);
1.569 + pOut = &pVm->pTos[(1-vals)+i];
1.570 + }else{
1.571 + static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
1.572 + if( pVm->db ){
1.573 + sqlite3_mutex_enter(pVm->db->mutex);
1.574 + sqlite3Error(pVm->db, SQLITE_RANGE, 0);
1.575 + }
1.576 + pOut = (Mem*)&nullMem;
1.577 + }
1.578 + return pOut;
1.579 +}
1.580 +
1.581 +/*
1.582 +** This function is called after invoking an sqlite3_value_XXX function on a
1.583 +** column value (i.e. a value returned by evaluating an SQL expression in the
1.584 +** select list of a SELECT statement) that may cause a malloc() failure. If
1.585 +** malloc() has failed, the threads mallocFailed flag is cleared and the result
1.586 +** code of statement pStmt set to SQLITE_NOMEM.
1.587 +**
1.588 +** Specifically, this is called from within:
1.589 +**
1.590 +** sqlite3_column_int()
1.591 +** sqlite3_column_int64()
1.592 +** sqlite3_column_text()
1.593 +** sqlite3_column_text16()
1.594 +** sqlite3_column_real()
1.595 +** sqlite3_column_bytes()
1.596 +** sqlite3_column_bytes16()
1.597 +**
1.598 +** But not for sqlite3_column_blob(), which never calls malloc().
1.599 +*/
1.600 +static void columnMallocFailure(sqlite3_stmt *pStmt)
1.601 +{
1.602 + /* If malloc() failed during an encoding conversion within an
1.603 + ** sqlite3_column_XXX API, then set the return code of the statement to
1.604 + ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
1.605 + ** and _finalize() will return NOMEM.
1.606 + */
1.607 + Vdbe *p = (Vdbe *)pStmt;
1.608 + if( p ){
1.609 + p->rc = sqlite3ApiExit(p->db, p->rc);
1.610 + sqlite3_mutex_leave(p->db->mutex);
1.611 + }
1.612 +}
1.613 +
1.614 +/**************************** sqlite3_column_ *******************************
1.615 +** The following routines are used to access elements of the current row
1.616 +** in the result set.
1.617 +*/
1.618 +const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
1.619 + const void *val;
1.620 + val = sqlite3_value_blob( columnMem(pStmt,i) );
1.621 + /* Even though there is no encoding conversion, value_blob() might
1.622 + ** need to call malloc() to expand the result of a zeroblob()
1.623 + ** expression.
1.624 + */
1.625 + columnMallocFailure(pStmt);
1.626 + return val;
1.627 +}
1.628 +int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
1.629 + int val = sqlite3_value_bytes( columnMem(pStmt,i) );
1.630 + columnMallocFailure(pStmt);
1.631 + return val;
1.632 +}
1.633 +int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
1.634 + int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
1.635 + columnMallocFailure(pStmt);
1.636 + return val;
1.637 +}
1.638 +double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
1.639 + double val = sqlite3_value_double( columnMem(pStmt,i) );
1.640 + columnMallocFailure(pStmt);
1.641 + return val;
1.642 +}
1.643 +int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
1.644 + int val = sqlite3_value_int( columnMem(pStmt,i) );
1.645 + columnMallocFailure(pStmt);
1.646 + return val;
1.647 +}
1.648 +sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
1.649 + sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
1.650 + columnMallocFailure(pStmt);
1.651 + return val;
1.652 +}
1.653 +const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
1.654 + const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
1.655 + columnMallocFailure(pStmt);
1.656 + return val;
1.657 +}
1.658 +sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
1.659 + sqlite3_value *pOut = columnMem(pStmt, i);
1.660 + columnMallocFailure(pStmt);
1.661 + return pOut;
1.662 +}
1.663 +#ifndef SQLITE_OMIT_UTF16
1.664 +const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
1.665 + const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
1.666 + columnMallocFailure(pStmt);
1.667 + return val;
1.668 +}
1.669 +#endif /* SQLITE_OMIT_UTF16 */
1.670 +int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
1.671 + int iType = sqlite3_value_type( columnMem(pStmt,i) );
1.672 + columnMallocFailure(pStmt);
1.673 + return iType;
1.674 +}
1.675 +
1.676 +/* The following function is experimental and subject to change or
1.677 +** removal */
1.678 +/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
1.679 +** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
1.680 +**}
1.681 +*/
1.682 +
1.683 +/*
1.684 +** Convert the N-th element of pStmt->pColName[] into a string using
1.685 +** xFunc() then return that string. If N is out of range, return 0.
1.686 +**
1.687 +** There are up to 5 names for each column. useType determines which
1.688 +** name is returned. Here are the names:
1.689 +**
1.690 +** 0 The column name as it should be displayed for output
1.691 +** 1 The datatype name for the column
1.692 +** 2 The name of the database that the column derives from
1.693 +** 3 The name of the table that the column derives from
1.694 +** 4 The name of the table column that the result column derives from
1.695 +**
1.696 +** If the result is not a simple column reference (if it is an expression
1.697 +** or a constant) then useTypes 2, 3, and 4 return NULL.
1.698 +*/
1.699 +static const void *columnName(
1.700 + sqlite3_stmt *pStmt,
1.701 + int N,
1.702 + const void *(*xFunc)(Mem*),
1.703 + int useType
1.704 +){
1.705 + const void *ret = 0;
1.706 + Vdbe *p = (Vdbe *)pStmt;
1.707 + int n;
1.708 +
1.709 +
1.710 + if( p!=0 ){
1.711 + n = sqlite3_column_count(pStmt);
1.712 + if( N<n && N>=0 ){
1.713 + N += useType*n;
1.714 + sqlite3_mutex_enter(p->db->mutex);
1.715 + ret = xFunc(&p->aColName[N]);
1.716 +
1.717 + /* A malloc may have failed inside of the xFunc() call. If this
1.718 + ** is the case, clear the mallocFailed flag and return NULL.
1.719 + */
1.720 + if( p->db && p->db->mallocFailed ){
1.721 + p->db->mallocFailed = 0;
1.722 + ret = 0;
1.723 + }
1.724 + sqlite3_mutex_leave(p->db->mutex);
1.725 + }
1.726 + }
1.727 + return ret;
1.728 +}
1.729 +
1.730 +/*
1.731 +** Return the name of the Nth column of the result set returned by SQL
1.732 +** statement pStmt.
1.733 +*/
1.734 +const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
1.735 + return columnName(
1.736 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
1.737 +}
1.738 +#ifndef SQLITE_OMIT_UTF16
1.739 +const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
1.740 + return columnName(
1.741 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
1.742 +}
1.743 +#endif
1.744 +
1.745 +/*
1.746 +** Return the column declaration type (if applicable) of the 'i'th column
1.747 +** of the result set of SQL statement pStmt.
1.748 +*/
1.749 +const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
1.750 + return columnName(
1.751 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
1.752 +}
1.753 +#ifndef SQLITE_OMIT_UTF16
1.754 +const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
1.755 + return columnName(
1.756 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
1.757 +}
1.758 +#endif /* SQLITE_OMIT_UTF16 */
1.759 +
1.760 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
1.761 +/*
1.762 +** Return the name of the database from which a result column derives.
1.763 +** NULL is returned if the result column is an expression or constant or
1.764 +** anything else which is not an unabiguous reference to a database column.
1.765 +*/
1.766 +const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
1.767 + return columnName(
1.768 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
1.769 +}
1.770 +#ifndef SQLITE_OMIT_UTF16
1.771 +const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
1.772 + return columnName(
1.773 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
1.774 +}
1.775 +#endif /* SQLITE_OMIT_UTF16 */
1.776 +
1.777 +/*
1.778 +** Return the name of the table from which a result column derives.
1.779 +** NULL is returned if the result column is an expression or constant or
1.780 +** anything else which is not an unabiguous reference to a database column.
1.781 +*/
1.782 +const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
1.783 + return columnName(
1.784 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
1.785 +}
1.786 +#ifndef SQLITE_OMIT_UTF16
1.787 +const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
1.788 + return columnName(
1.789 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
1.790 +}
1.791 +#endif /* SQLITE_OMIT_UTF16 */
1.792 +
1.793 +/*
1.794 +** Return the name of the table column from which a result column derives.
1.795 +** NULL is returned if the result column is an expression or constant or
1.796 +** anything else which is not an unabiguous reference to a database column.
1.797 +*/
1.798 +const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
1.799 + return columnName(
1.800 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
1.801 +}
1.802 +#ifndef SQLITE_OMIT_UTF16
1.803 +const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
1.804 + return columnName(
1.805 + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
1.806 +}
1.807 +#endif /* SQLITE_OMIT_UTF16 */
1.808 +#endif /* SQLITE_ENABLE_COLUMN_METADATA */
1.809 +
1.810 +
1.811 +/******************************* sqlite3_bind_ ***************************
1.812 +**
1.813 +** Routines used to attach values to wildcards in a compiled SQL statement.
1.814 +*/
1.815 +/*
1.816 +** Unbind the value bound to variable i in virtual machine p. This is the
1.817 +** the same as binding a NULL value to the column. If the "i" parameter is
1.818 +** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1.819 +**
1.820 +** The error code stored in database p->db is overwritten with the return
1.821 +** value in any case.
1.822 +*/
1.823 +static int vdbeUnbind(Vdbe *p, int i){
1.824 + Mem *pVar;
1.825 + if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
1.826 + if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
1.827 + return SQLITE_MISUSE;
1.828 + }
1.829 + if( i<1 || i>p->nVar ){
1.830 + sqlite3Error(p->db, SQLITE_RANGE, 0);
1.831 + return SQLITE_RANGE;
1.832 + }
1.833 + i--;
1.834 + pVar = &p->aVar[i];
1.835 + sqlite3VdbeMemRelease(pVar);
1.836 + pVar->flags = MEM_Null;
1.837 + sqlite3Error(p->db, SQLITE_OK, 0);
1.838 + return SQLITE_OK;
1.839 +}
1.840 +
1.841 +/*
1.842 +** Bind a text or BLOB value.
1.843 +*/
1.844 +static int bindText(
1.845 + sqlite3_stmt *pStmt, /* The statement to bind against */
1.846 + int i, /* Index of the parameter to bind */
1.847 + const void *zData, /* Pointer to the data to be bound */
1.848 + int nData, /* Number of bytes of data to be bound */
1.849 + void (*xDel)(void*), /* Destructor for the data */
1.850 + int encoding /* Encoding for the data */
1.851 +){
1.852 + Vdbe *p = (Vdbe *)pStmt;
1.853 + Mem *pVar;
1.854 + int rc;
1.855 +
1.856 + if( p==0 ){
1.857 + return SQLITE_MISUSE;
1.858 + }
1.859 + sqlite3_mutex_enter(p->db->mutex);
1.860 + rc = vdbeUnbind(p, i);
1.861 + if( rc==SQLITE_OK && zData!=0 ){
1.862 + pVar = &p->aVar[i-1];
1.863 + rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1.864 + if( rc==SQLITE_OK && encoding!=0 ){
1.865 + rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1.866 + }
1.867 + sqlite3Error(p->db, rc, 0);
1.868 + rc = sqlite3ApiExit(p->db, rc);
1.869 + }
1.870 + sqlite3_mutex_leave(p->db->mutex);
1.871 + return rc;
1.872 +}
1.873 +
1.874 +
1.875 +/*
1.876 +** Bind a blob value to an SQL statement variable.
1.877 +*/
1.878 +int sqlite3_bind_blob(
1.879 + sqlite3_stmt *pStmt,
1.880 + int i,
1.881 + const void *zData,
1.882 + int nData,
1.883 + void (*xDel)(void*)
1.884 +){
1.885 + return bindText(pStmt, i, zData, nData, xDel, 0);
1.886 +}
1.887 +int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1.888 + int rc;
1.889 + Vdbe *p = (Vdbe *)pStmt;
1.890 + sqlite3_mutex_enter(p->db->mutex);
1.891 + rc = vdbeUnbind(p, i);
1.892 + if( rc==SQLITE_OK ){
1.893 + sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1.894 + }
1.895 + sqlite3_mutex_leave(p->db->mutex);
1.896 + return rc;
1.897 +}
1.898 +int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
1.899 + return sqlite3_bind_int64(p, i, (i64)iValue);
1.900 +}
1.901 +int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
1.902 + int rc;
1.903 + Vdbe *p = (Vdbe *)pStmt;
1.904 + sqlite3_mutex_enter(p->db->mutex);
1.905 + rc = vdbeUnbind(p, i);
1.906 + if( rc==SQLITE_OK ){
1.907 + sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
1.908 + }
1.909 + sqlite3_mutex_leave(p->db->mutex);
1.910 + return rc;
1.911 +}
1.912 +int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1.913 + int rc;
1.914 + Vdbe *p = (Vdbe*)pStmt;
1.915 + sqlite3_mutex_enter(p->db->mutex);
1.916 + rc = vdbeUnbind(p, i);
1.917 + sqlite3_mutex_leave(p->db->mutex);
1.918 + return rc;
1.919 +}
1.920 +int sqlite3_bind_text(
1.921 + sqlite3_stmt *pStmt,
1.922 + int i,
1.923 + const char *zData,
1.924 + int nData,
1.925 + void (*xDel)(void*)
1.926 +){
1.927 + return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
1.928 +}
1.929 +#ifndef SQLITE_OMIT_UTF16
1.930 +int sqlite3_bind_text16(
1.931 + sqlite3_stmt *pStmt,
1.932 + int i,
1.933 + const void *zData,
1.934 + int nData,
1.935 + void (*xDel)(void*)
1.936 +){
1.937 + return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
1.938 +}
1.939 +#endif /* SQLITE_OMIT_UTF16 */
1.940 +int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1.941 + int rc;
1.942 + Vdbe *p = (Vdbe *)pStmt;
1.943 + sqlite3_mutex_enter(p->db->mutex);
1.944 + rc = vdbeUnbind(p, i);
1.945 + if( rc==SQLITE_OK ){
1.946 + rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
1.947 + }
1.948 + sqlite3_mutex_leave(p->db->mutex);
1.949 + return rc;
1.950 +}
1.951 +int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1.952 + int rc;
1.953 + Vdbe *p = (Vdbe *)pStmt;
1.954 + sqlite3_mutex_enter(p->db->mutex);
1.955 + rc = vdbeUnbind(p, i);
1.956 + if( rc==SQLITE_OK ){
1.957 + sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1.958 + }
1.959 + sqlite3_mutex_leave(p->db->mutex);
1.960 + return rc;
1.961 +}
1.962 +
1.963 +/*
1.964 +** Return the number of wildcards that can be potentially bound to.
1.965 +** This routine is added to support DBD::SQLite.
1.966 +*/
1.967 +int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
1.968 + Vdbe *p = (Vdbe*)pStmt;
1.969 + return p ? p->nVar : 0;
1.970 +}
1.971 +
1.972 +/*
1.973 +** Create a mapping from variable numbers to variable names
1.974 +** in the Vdbe.azVar[] array, if such a mapping does not already
1.975 +** exist.
1.976 +*/
1.977 +static void createVarMap(Vdbe *p){
1.978 + if( !p->okVar ){
1.979 + sqlite3_mutex_enter(p->db->mutex);
1.980 + if( !p->okVar ){
1.981 + int j;
1.982 + Op *pOp;
1.983 + for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1.984 + if( pOp->opcode==OP_Variable ){
1.985 + assert( pOp->p1>0 && pOp->p1<=p->nVar );
1.986 + p->azVar[pOp->p1-1] = pOp->p3;
1.987 + }
1.988 + }
1.989 + p->okVar = 1;
1.990 + }
1.991 + sqlite3_mutex_leave(p->db->mutex);
1.992 + }
1.993 +}
1.994 +
1.995 +/*
1.996 +** Return the name of a wildcard parameter. Return NULL if the index
1.997 +** is out of range or if the wildcard is unnamed.
1.998 +**
1.999 +** The result is always UTF-8.
1.1000 +*/
1.1001 +const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1.1002 + Vdbe *p = (Vdbe*)pStmt;
1.1003 + if( p==0 || i<1 || i>p->nVar ){
1.1004 + return 0;
1.1005 + }
1.1006 + createVarMap(p);
1.1007 + return p->azVar[i-1];
1.1008 +}
1.1009 +
1.1010 +/*
1.1011 +** Given a wildcard parameter name, return the index of the variable
1.1012 +** with that name. If there is no variable with the given name,
1.1013 +** return 0.
1.1014 +*/
1.1015 +int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1.1016 + Vdbe *p = (Vdbe*)pStmt;
1.1017 + int i;
1.1018 + if( p==0 ){
1.1019 + return 0;
1.1020 + }
1.1021 + createVarMap(p);
1.1022 + if( zName ){
1.1023 + for(i=0; i<p->nVar; i++){
1.1024 + const char *z = p->azVar[i];
1.1025 + if( z && strcmp(z,zName)==0 ){
1.1026 + return i+1;
1.1027 + }
1.1028 + }
1.1029 + }
1.1030 + return 0;
1.1031 +}
1.1032 +
1.1033 +/*
1.1034 +** Transfer all bindings from the first statement over to the second.
1.1035 +** If the two statements contain a different number of bindings, then
1.1036 +** an SQLITE_ERROR is returned.
1.1037 +*/
1.1038 +int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1.1039 + Vdbe *pFrom = (Vdbe*)pFromStmt;
1.1040 + Vdbe *pTo = (Vdbe*)pToStmt;
1.1041 + int i, rc = SQLITE_OK;
1.1042 + if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
1.1043 + || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1.1044 + || pTo->db!=pFrom->db ){
1.1045 + return SQLITE_MISUSE;
1.1046 + }
1.1047 + if( pFrom->nVar!=pTo->nVar ){
1.1048 + return SQLITE_ERROR;
1.1049 + }
1.1050 + sqlite3_mutex_enter(pTo->db->mutex);
1.1051 + for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
1.1052 + sqlite3MallocDisallow();
1.1053 + rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1.1054 + sqlite3MallocAllow();
1.1055 + }
1.1056 + sqlite3_mutex_leave(pTo->db->mutex);
1.1057 + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
1.1058 + return rc;
1.1059 +}
1.1060 +
1.1061 +/*
1.1062 +** Return the sqlite3* database handle to which the prepared statement given
1.1063 +** in the argument belongs. This is the same database handle that was
1.1064 +** the first argument to the sqlite3_prepare() that was used to create
1.1065 +** the statement in the first place.
1.1066 +*/
1.1067 +sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1.1068 + return pStmt ? ((Vdbe*)pStmt)->db : 0;
1.1069 +}