ds@1952: /* ds@1952: ** 2004 May 26 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** ds@1952: ** This file contains code use to implement APIs that are part of the ds@1952: ** VDBE. ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include "vdbeInt.h" ds@1952: ds@1952: /* ds@1952: ** Return TRUE (non-zero) of the statement supplied as an argument needs ds@1952: ** to be recompiled. A statement needs to be recompiled whenever the ds@1952: ** execution environment changes in a way that would alter the program ds@1952: ** that sqlite3_prepare() generates. For example, if new functions or ds@1952: ** collating sequences are registered or if an authorizer function is ds@1952: ** added or changed. ds@1952: */ ds@1952: int sqlite3_expired(sqlite3_stmt *pStmt){ ds@1952: Vdbe *p = (Vdbe*)pStmt; ds@1952: return p==0 || p->expired; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following routine destroys a virtual machine that is created by ds@1952: ** the sqlite3_compile() routine. The integer returned is an SQLITE_ ds@1952: ** success/failure code that describes the result of executing the virtual ds@1952: ** machine. ds@1952: ** ds@1952: ** This routine sets the error code and string returned by ds@1952: ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). ds@1952: */ ds@1952: int sqlite3_finalize(sqlite3_stmt *pStmt){ ds@1952: int rc; ds@1952: if( pStmt==0 ){ ds@1952: rc = SQLITE_OK; ds@1952: }else{ ds@1952: Vdbe *v = (Vdbe*)pStmt; ds@1952: sqlite3_mutex *mutex = v->db->mutex; ds@1952: sqlite3_mutex_enter(mutex); ds@1952: rc = sqlite3VdbeFinalize(v); ds@1952: sqlite3_mutex_leave(mutex); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Terminate the current execution of an SQL statement and reset it ds@1952: ** back to its starting state so that it can be reused. A success code from ds@1952: ** the prior execution is returned. ds@1952: ** ds@1952: ** This routine sets the error code and string returned by ds@1952: ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). ds@1952: */ ds@1952: int sqlite3_reset(sqlite3_stmt *pStmt){ ds@1952: int rc; ds@1952: if( pStmt==0 ){ ds@1952: rc = SQLITE_OK; ds@1952: }else{ ds@1952: Vdbe *v = (Vdbe*)pStmt; ds@1952: sqlite3_mutex_enter(v->db->mutex); ds@1952: rc = sqlite3VdbeReset(v); ds@1952: sqlite3VdbeMakeReady(v, -1, 0, 0, 0); ds@1952: assert( (rc & (v->db->errMask))==rc ); ds@1952: sqlite3_mutex_leave(v->db->mutex); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set all the parameters in the compiled SQL statement to NULL. ds@1952: */ ds@1952: int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ ds@1952: int i; ds@1952: int rc = SQLITE_OK; ds@1952: Vdbe *v = (Vdbe*)pStmt; ds@1952: sqlite3_mutex_enter(v->db->mutex); ds@1952: for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){ ds@1952: rc = sqlite3_bind_null(pStmt, i); ds@1952: } ds@1952: sqlite3_mutex_leave(v->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: ds@1952: /**************************** sqlite3_value_ ******************************* ds@1952: ** The following routines extract information from a Mem or sqlite3_value ds@1952: ** structure. ds@1952: */ ds@1952: const void *sqlite3_value_blob(sqlite3_value *pVal){ ds@1952: Mem *p = (Mem*)pVal; ds@1952: if( p->flags & (MEM_Blob|MEM_Str) ){ ds@1952: sqlite3VdbeMemExpandBlob(p); ds@1952: p->flags &= ~MEM_Str; ds@1952: p->flags |= MEM_Blob; ds@1952: return p->z; ds@1952: }else{ ds@1952: return sqlite3_value_text(pVal); ds@1952: } ds@1952: } ds@1952: int sqlite3_value_bytes(sqlite3_value *pVal){ ds@1952: return sqlite3ValueBytes(pVal, SQLITE_UTF8); ds@1952: } ds@1952: int sqlite3_value_bytes16(sqlite3_value *pVal){ ds@1952: return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); ds@1952: } ds@1952: double sqlite3_value_double(sqlite3_value *pVal){ ds@1952: return sqlite3VdbeRealValue((Mem*)pVal); ds@1952: } ds@1952: int sqlite3_value_int(sqlite3_value *pVal){ ds@1952: return sqlite3VdbeIntValue((Mem*)pVal); ds@1952: } ds@1952: sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ ds@1952: return sqlite3VdbeIntValue((Mem*)pVal); ds@1952: } ds@1952: const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ ds@1952: return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_value_text16(sqlite3_value* pVal){ ds@1952: return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); ds@1952: } ds@1952: const void *sqlite3_value_text16be(sqlite3_value *pVal){ ds@1952: return sqlite3ValueText(pVal, SQLITE_UTF16BE); ds@1952: } ds@1952: const void *sqlite3_value_text16le(sqlite3_value *pVal){ ds@1952: return sqlite3ValueText(pVal, SQLITE_UTF16LE); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: int sqlite3_value_type(sqlite3_value* pVal){ ds@1952: return pVal->type; ds@1952: } ds@1952: ds@1952: /**************************** sqlite3_result_ ******************************* ds@1952: ** The following routines are used by user-defined functions to specify ds@1952: ** the function result. ds@1952: */ ds@1952: void sqlite3_result_blob( ds@1952: sqlite3_context *pCtx, ds@1952: const void *z, ds@1952: int n, ds@1952: void (*xDel)(void *) ds@1952: ){ ds@1952: assert( n>=0 ); ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); ds@1952: } ds@1952: void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetDouble(&pCtx->s, rVal); ds@1952: } ds@1952: void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: pCtx->isError = 1; ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: pCtx->isError = 1; ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); ds@1952: } ds@1952: #endif ds@1952: void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); ds@1952: } ds@1952: void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetInt64(&pCtx->s, iVal); ds@1952: } ds@1952: void sqlite3_result_null(sqlite3_context *pCtx){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetNull(&pCtx->s); ds@1952: } ds@1952: void sqlite3_result_text( ds@1952: sqlite3_context *pCtx, ds@1952: const char *z, ds@1952: int n, ds@1952: void (*xDel)(void *) ds@1952: ){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: void sqlite3_result_text16( ds@1952: sqlite3_context *pCtx, ds@1952: const void *z, ds@1952: int n, ds@1952: void (*xDel)(void *) ds@1952: ){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); ds@1952: } ds@1952: void sqlite3_result_text16be( ds@1952: sqlite3_context *pCtx, ds@1952: const void *z, ds@1952: int n, ds@1952: void (*xDel)(void *) ds@1952: ){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); ds@1952: } ds@1952: void sqlite3_result_text16le( ds@1952: sqlite3_context *pCtx, ds@1952: const void *z, ds@1952: int n, ds@1952: void (*xDel)(void *) ds@1952: ){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemCopy(&pCtx->s, pValue); ds@1952: } ds@1952: void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); ds@1952: } ds@1952: ds@1952: /* Force an SQLITE_TOOBIG error. */ ds@1952: void sqlite3_result_error_toobig(sqlite3_context *pCtx){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1); ds@1952: } ds@1952: ds@1952: /* An SQLITE_NOMEM error. */ ds@1952: void sqlite3_result_error_nomem(sqlite3_context *pCtx){ ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: sqlite3VdbeMemSetNull(&pCtx->s); ds@1952: pCtx->isError = 1; ds@1952: pCtx->s.db->mallocFailed = 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Execute the statement pStmt, either until a row of data is ready, the ds@1952: ** statement is completely executed or an error occurs. ds@1952: ** ds@1952: ** This routine implements the bulk of the logic behind the sqlite_step() ds@1952: ** API. The only thing omitted is the automatic recompile if a ds@1952: ** schema change has occurred. That detail is handled by the ds@1952: ** outer sqlite3_step() wrapper procedure. ds@1952: */ ds@1952: static int sqlite3Step(Vdbe *p){ ds@1952: sqlite3 *db; ds@1952: int rc; ds@1952: ds@1952: /* Assert that malloc() has not failed */ ds@1952: db = p->db; ds@1952: assert( !db->mallocFailed ); ds@1952: ds@1952: if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( p->aborted ){ ds@1952: return SQLITE_ABORT; ds@1952: } ds@1952: if( p->pc<=0 && p->expired ){ ds@1952: if( p->rc==SQLITE_OK ){ ds@1952: p->rc = SQLITE_SCHEMA; ds@1952: } ds@1952: rc = SQLITE_ERROR; ds@1952: goto end_of_step; ds@1952: } ds@1952: if( sqlite3SafetyOn(db) ){ ds@1952: p->rc = SQLITE_MISUSE; ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( p->pc<0 ){ ds@1952: /* If there are no other statements currently running, then ds@1952: ** reset the interrupt flag. This prevents a call to sqlite3_interrupt ds@1952: ** from interrupting a statement that has not yet started. ds@1952: */ ds@1952: if( db->activeVdbeCnt==0 ){ ds@1952: db->u1.isInterrupted = 0; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRACE ds@1952: /* Invoke the trace callback if there is one ds@1952: */ ds@1952: if( db->xTrace && !db->init.busy ){ ds@1952: assert( p->nOp>0 ); ds@1952: assert( p->aOp[p->nOp-1].opcode==OP_Noop ); ds@1952: assert( p->aOp[p->nOp-1].p3!=0 ); ds@1952: assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); ds@1952: sqlite3SafetyOff(db); ds@1952: db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); ds@1952: if( sqlite3SafetyOn(db) ){ ds@1952: p->rc = SQLITE_MISUSE; ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: } ds@1952: if( db->xProfile && !db->init.busy ){ ds@1952: double rNow; ds@1952: sqlite3OsCurrentTime(db->pVfs, &rNow); ds@1952: p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned ds@1952: ** on in debugging mode. ds@1952: */ ds@1952: #ifdef SQLITE_DEBUG ds@1952: if( (db->flags & SQLITE_SqlTrace)!=0 ){ ds@1952: sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); ds@1952: } ds@1952: #endif /* SQLITE_DEBUG */ ds@1952: ds@1952: db->activeVdbeCnt++; ds@1952: p->pc = 0; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_EXPLAIN ds@1952: if( p->explain ){ ds@1952: rc = sqlite3VdbeList(p); ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_EXPLAIN */ ds@1952: { ds@1952: rc = sqlite3VdbeExec(p); ds@1952: } ds@1952: ds@1952: if( sqlite3SafetyOff(db) ){ ds@1952: rc = SQLITE_MISUSE; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRACE ds@1952: /* Invoke the profile callback if there is one ds@1952: */ ds@1952: if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ ds@1952: double rNow; ds@1952: u64 elapseTime; ds@1952: ds@1952: sqlite3OsCurrentTime(db->pVfs, &rNow); ds@1952: elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; ds@1952: assert( p->nOp>0 ); ds@1952: assert( p->aOp[p->nOp-1].opcode==OP_Noop ); ds@1952: assert( p->aOp[p->nOp-1].p3!=0 ); ds@1952: assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); ds@1952: db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); ds@1952: } ds@1952: #endif ds@1952: ds@1952: sqlite3Error(p->db, rc, 0); ds@1952: p->rc = sqlite3ApiExit(p->db, p->rc); ds@1952: end_of_step: ds@1952: assert( (rc&0xff)==rc ); ds@1952: if( p->zSql && (rc&0xff)db, p->rc, 0); ds@1952: return p->rc; ds@1952: }else{ ds@1952: /* This is for legacy sqlite3_prepare() builds and when the code ds@1952: ** is SQLITE_ROW or SQLITE_DONE */ ds@1952: return rc; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This is the top-level implementation of sqlite3_step(). Call ds@1952: ** sqlite3Step() to do most of the work. If a schema error occurs, ds@1952: ** call sqlite3Reprepare() and try again. ds@1952: */ ds@1952: #ifdef SQLITE_OMIT_PARSER ds@1952: int sqlite3_step(sqlite3_stmt *pStmt){ ds@1952: int rc; ds@1952: Vdbe *v; ds@1952: v = (Vdbe*)pStmt; ds@1952: sqlite3_mutex_enter(v->db->mutex); ds@1952: rc = sqlite3Step(v); ds@1952: sqlite3_mutex_leave(v->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: #else ds@1952: int sqlite3_step(sqlite3_stmt *pStmt){ ds@1952: int cnt = 0; ds@1952: int rc; ds@1952: Vdbe *v = (Vdbe*)pStmt; ds@1952: sqlite3_mutex_enter(v->db->mutex); ds@1952: while( (rc = sqlite3Step(v))==SQLITE_SCHEMA ds@1952: && cnt++ < 5 ds@1952: && sqlite3Reprepare(v) ){ ds@1952: sqlite3_reset(pStmt); ds@1952: v->expired = 0; ds@1952: } ds@1952: sqlite3_mutex_leave(v->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Extract the user data from a sqlite3_context structure and return a ds@1952: ** pointer to it. ds@1952: */ ds@1952: void *sqlite3_user_data(sqlite3_context *p){ ds@1952: assert( p && p->pFunc ); ds@1952: return p->pFunc->pUserData; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following is the implementation of an SQL function that always ds@1952: ** fails with an error message stating that the function is used in the ds@1952: ** wrong context. The sqlite3_overload_function() API might construct ds@1952: ** SQL function that use this routine so that the functions will exist ds@1952: ** for name resolution but are actually overloaded by the xFindFunction ds@1952: ** method of virtual tables. ds@1952: */ ds@1952: void sqlite3InvalidFunction( ds@1952: sqlite3_context *context, /* The function calling context */ ds@1952: int argc, /* Number of arguments to the function */ ds@1952: sqlite3_value **argv /* Value of each argument */ ds@1952: ){ ds@1952: const char *zName = context->pFunc->zName; ds@1952: char *zErr; ds@1952: zErr = sqlite3MPrintf(0, ds@1952: "unable to use function %s in the requested context", zName); ds@1952: sqlite3_result_error(context, zErr, -1); ds@1952: sqlite3_free(zErr); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Allocate or return the aggregate context for a user function. A new ds@1952: ** context is allocated on the first call. Subsequent calls return the ds@1952: ** same context that was returned on prior calls. ds@1952: */ ds@1952: void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ ds@1952: Mem *pMem; ds@1952: assert( p && p->pFunc && p->pFunc->xStep ); ds@1952: assert( sqlite3_mutex_held(p->s.db->mutex) ); ds@1952: pMem = p->pMem; ds@1952: if( (pMem->flags & MEM_Agg)==0 ){ ds@1952: if( nByte==0 ){ ds@1952: assert( pMem->flags==MEM_Null ); ds@1952: pMem->z = 0; ds@1952: }else{ ds@1952: pMem->flags = MEM_Agg; ds@1952: pMem->xDel = sqlite3_free; ds@1952: pMem->u.pDef = p->pFunc; ds@1952: if( nByte<=NBFS ){ ds@1952: pMem->z = pMem->zShort; ds@1952: memset(pMem->z, 0, nByte); ds@1952: }else{ ds@1952: pMem->z = sqlite3DbMallocZero(p->s.db, nByte); ds@1952: } ds@1952: } ds@1952: } ds@1952: return (void*)pMem->z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the auxilary data pointer, if any, for the iArg'th argument to ds@1952: ** the user-function defined by pCtx. ds@1952: */ ds@1952: void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ ds@1952: VdbeFunc *pVdbeFunc; ds@1952: ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: pVdbeFunc = pCtx->pVdbeFunc; ds@1952: if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ ds@1952: return 0; ds@1952: } ds@1952: return pVdbeFunc->apAux[iArg].pAux; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the auxilary data pointer and delete function, for the iArg'th ds@1952: ** argument to the user-function defined by pCtx. Any previous value is ds@1952: ** deleted by calling the delete function specified when it was set. ds@1952: */ ds@1952: void sqlite3_set_auxdata( ds@1952: sqlite3_context *pCtx, ds@1952: int iArg, ds@1952: void *pAux, ds@1952: void (*xDelete)(void*) ds@1952: ){ ds@1952: struct AuxData *pAuxData; ds@1952: VdbeFunc *pVdbeFunc; ds@1952: if( iArg<0 ) goto failed; ds@1952: ds@1952: assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); ds@1952: pVdbeFunc = pCtx->pVdbeFunc; ds@1952: if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ ds@1952: int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); ds@1952: int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; ds@1952: pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); ds@1952: if( !pVdbeFunc ){ ds@1952: goto failed; ds@1952: } ds@1952: pCtx->pVdbeFunc = pVdbeFunc; ds@1952: memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); ds@1952: pVdbeFunc->nAux = iArg+1; ds@1952: pVdbeFunc->pFunc = pCtx->pFunc; ds@1952: } ds@1952: ds@1952: pAuxData = &pVdbeFunc->apAux[iArg]; ds@1952: if( pAuxData->pAux && pAuxData->xDelete ){ ds@1952: pAuxData->xDelete(pAuxData->pAux); ds@1952: } ds@1952: pAuxData->pAux = pAux; ds@1952: pAuxData->xDelete = xDelete; ds@1952: return; ds@1952: ds@1952: failed: ds@1952: if( xDelete ){ ds@1952: xDelete(pAux); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of times the Step function of a aggregate has been ds@1952: ** called. ds@1952: ** ds@1952: ** This function is deprecated. Do not use it for new code. It is ds@1952: ** provide only to avoid breaking legacy code. New aggregate function ds@1952: ** implementations should keep their own counts within their aggregate ds@1952: ** context. ds@1952: */ ds@1952: int sqlite3_aggregate_count(sqlite3_context *p){ ds@1952: assert( p && p->pFunc && p->pFunc->xStep ); ds@1952: return p->pMem->n; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of columns in the result set for the statement pStmt. ds@1952: */ ds@1952: int sqlite3_column_count(sqlite3_stmt *pStmt){ ds@1952: Vdbe *pVm = (Vdbe *)pStmt; ds@1952: return pVm ? pVm->nResColumn : 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of values available from the current row of the ds@1952: ** currently executing statement pStmt. ds@1952: */ ds@1952: int sqlite3_data_count(sqlite3_stmt *pStmt){ ds@1952: Vdbe *pVm = (Vdbe *)pStmt; ds@1952: if( pVm==0 || !pVm->resOnStack ) return 0; ds@1952: return pVm->nResColumn; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Check to see if column iCol of the given statement is valid. If ds@1952: ** it is, return a pointer to the Mem for the value of that column. ds@1952: ** If iCol is not valid, return a pointer to a Mem which has a value ds@1952: ** of NULL. ds@1952: */ ds@1952: static Mem *columnMem(sqlite3_stmt *pStmt, int i){ ds@1952: Vdbe *pVm; ds@1952: int vals; ds@1952: Mem *pOut; ds@1952: ds@1952: pVm = (Vdbe *)pStmt; ds@1952: if( pVm && pVm->resOnStack && inResColumn && i>=0 ){ ds@1952: sqlite3_mutex_enter(pVm->db->mutex); ds@1952: vals = sqlite3_data_count(pStmt); ds@1952: pOut = &pVm->pTos[(1-vals)+i]; ds@1952: }else{ ds@1952: static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL }; ds@1952: if( pVm->db ){ ds@1952: sqlite3_mutex_enter(pVm->db->mutex); ds@1952: sqlite3Error(pVm->db, SQLITE_RANGE, 0); ds@1952: } ds@1952: pOut = (Mem*)&nullMem; ds@1952: } ds@1952: return pOut; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This function is called after invoking an sqlite3_value_XXX function on a ds@1952: ** column value (i.e. a value returned by evaluating an SQL expression in the ds@1952: ** select list of a SELECT statement) that may cause a malloc() failure. If ds@1952: ** malloc() has failed, the threads mallocFailed flag is cleared and the result ds@1952: ** code of statement pStmt set to SQLITE_NOMEM. ds@1952: ** ds@1952: ** Specifically, this is called from within: ds@1952: ** ds@1952: ** sqlite3_column_int() ds@1952: ** sqlite3_column_int64() ds@1952: ** sqlite3_column_text() ds@1952: ** sqlite3_column_text16() ds@1952: ** sqlite3_column_real() ds@1952: ** sqlite3_column_bytes() ds@1952: ** sqlite3_column_bytes16() ds@1952: ** ds@1952: ** But not for sqlite3_column_blob(), which never calls malloc(). ds@1952: */ ds@1952: static void columnMallocFailure(sqlite3_stmt *pStmt) ds@1952: { ds@1952: /* If malloc() failed during an encoding conversion within an ds@1952: ** sqlite3_column_XXX API, then set the return code of the statement to ds@1952: ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR ds@1952: ** and _finalize() will return NOMEM. ds@1952: */ ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: if( p ){ ds@1952: p->rc = sqlite3ApiExit(p->db, p->rc); ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: } ds@1952: } ds@1952: ds@1952: /**************************** sqlite3_column_ ******************************* ds@1952: ** The following routines are used to access elements of the current row ds@1952: ** in the result set. ds@1952: */ ds@1952: const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ ds@1952: const void *val; ds@1952: val = sqlite3_value_blob( columnMem(pStmt,i) ); ds@1952: /* Even though there is no encoding conversion, value_blob() might ds@1952: ** need to call malloc() to expand the result of a zeroblob() ds@1952: ** expression. ds@1952: */ ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ ds@1952: int val = sqlite3_value_bytes( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ ds@1952: int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ ds@1952: double val = sqlite3_value_double( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ ds@1952: int val = sqlite3_value_int( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ ds@1952: sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ ds@1952: const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ ds@1952: sqlite3_value *pOut = columnMem(pStmt, i); ds@1952: columnMallocFailure(pStmt); ds@1952: return pOut; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ ds@1952: const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return val; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ ds@1952: int iType = sqlite3_value_type( columnMem(pStmt,i) ); ds@1952: columnMallocFailure(pStmt); ds@1952: return iType; ds@1952: } ds@1952: ds@1952: /* The following function is experimental and subject to change or ds@1952: ** removal */ ds@1952: /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ ds@1952: ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); ds@1952: **} ds@1952: */ ds@1952: ds@1952: /* ds@1952: ** Convert the N-th element of pStmt->pColName[] into a string using ds@1952: ** xFunc() then return that string. If N is out of range, return 0. ds@1952: ** ds@1952: ** There are up to 5 names for each column. useType determines which ds@1952: ** name is returned. Here are the names: ds@1952: ** ds@1952: ** 0 The column name as it should be displayed for output ds@1952: ** 1 The datatype name for the column ds@1952: ** 2 The name of the database that the column derives from ds@1952: ** 3 The name of the table that the column derives from ds@1952: ** 4 The name of the table column that the result column derives from ds@1952: ** ds@1952: ** If the result is not a simple column reference (if it is an expression ds@1952: ** or a constant) then useTypes 2, 3, and 4 return NULL. ds@1952: */ ds@1952: static const void *columnName( ds@1952: sqlite3_stmt *pStmt, ds@1952: int N, ds@1952: const void *(*xFunc)(Mem*), ds@1952: int useType ds@1952: ){ ds@1952: const void *ret = 0; ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: int n; ds@1952: ds@1952: ds@1952: if( p!=0 ){ ds@1952: n = sqlite3_column_count(pStmt); ds@1952: if( N=0 ){ ds@1952: N += useType*n; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: ret = xFunc(&p->aColName[N]); ds@1952: ds@1952: /* A malloc may have failed inside of the xFunc() call. If this ds@1952: ** is the case, clear the mallocFailed flag and return NULL. ds@1952: */ ds@1952: if( p->db && p->db->mallocFailed ){ ds@1952: p->db->mallocFailed = 0; ds@1952: ret = 0; ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: } ds@1952: } ds@1952: return ret; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the name of the Nth column of the result set returned by SQL ds@1952: ** statement pStmt. ds@1952: */ ds@1952: const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Return the column declaration type (if applicable) of the 'i'th column ds@1952: ** of the result set of SQL statement pStmt. ds@1952: */ ds@1952: const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: #ifdef SQLITE_ENABLE_COLUMN_METADATA ds@1952: /* ds@1952: ** Return the name of the database from which a result column derives. ds@1952: ** NULL is returned if the result column is an expression or constant or ds@1952: ** anything else which is not an unabiguous reference to a database column. ds@1952: */ ds@1952: const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: /* ds@1952: ** Return the name of the table from which a result column derives. ds@1952: ** NULL is returned if the result column is an expression or constant or ds@1952: ** anything else which is not an unabiguous reference to a database column. ds@1952: */ ds@1952: const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: /* ds@1952: ** Return the name of the table column from which a result column derives. ds@1952: ** NULL is returned if the result column is an expression or constant or ds@1952: ** anything else which is not an unabiguous reference to a database column. ds@1952: */ ds@1952: const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ ds@1952: return columnName( ds@1952: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: #endif /* SQLITE_ENABLE_COLUMN_METADATA */ ds@1952: ds@1952: ds@1952: /******************************* sqlite3_bind_ *************************** ds@1952: ** ds@1952: ** Routines used to attach values to wildcards in a compiled SQL statement. ds@1952: */ ds@1952: /* ds@1952: ** Unbind the value bound to variable i in virtual machine p. This is the ds@1952: ** the same as binding a NULL value to the column. If the "i" parameter is ds@1952: ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. ds@1952: ** ds@1952: ** The error code stored in database p->db is overwritten with the return ds@1952: ** value in any case. ds@1952: */ ds@1952: static int vdbeUnbind(Vdbe *p, int i){ ds@1952: Mem *pVar; ds@1952: if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ ds@1952: if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( i<1 || i>p->nVar ){ ds@1952: sqlite3Error(p->db, SQLITE_RANGE, 0); ds@1952: return SQLITE_RANGE; ds@1952: } ds@1952: i--; ds@1952: pVar = &p->aVar[i]; ds@1952: sqlite3VdbeMemRelease(pVar); ds@1952: pVar->flags = MEM_Null; ds@1952: sqlite3Error(p->db, SQLITE_OK, 0); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Bind a text or BLOB value. ds@1952: */ ds@1952: static int bindText( ds@1952: sqlite3_stmt *pStmt, /* The statement to bind against */ ds@1952: int i, /* Index of the parameter to bind */ ds@1952: const void *zData, /* Pointer to the data to be bound */ ds@1952: int nData, /* Number of bytes of data to be bound */ ds@1952: void (*xDel)(void*), /* Destructor for the data */ ds@1952: int encoding /* Encoding for the data */ ds@1952: ){ ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: Mem *pVar; ds@1952: int rc; ds@1952: ds@1952: if( p==0 ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: if( rc==SQLITE_OK && zData!=0 ){ ds@1952: pVar = &p->aVar[i-1]; ds@1952: rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); ds@1952: if( rc==SQLITE_OK && encoding!=0 ){ ds@1952: rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); ds@1952: } ds@1952: sqlite3Error(p->db, rc, 0); ds@1952: rc = sqlite3ApiExit(p->db, rc); ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Bind a blob value to an SQL statement variable. ds@1952: */ ds@1952: int sqlite3_bind_blob( ds@1952: sqlite3_stmt *pStmt, ds@1952: int i, ds@1952: const void *zData, ds@1952: int nData, ds@1952: void (*xDel)(void*) ds@1952: ){ ds@1952: return bindText(pStmt, i, zData, nData, xDel, 0); ds@1952: } ds@1952: int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ ds@1952: int rc; ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ ds@1952: return sqlite3_bind_int64(p, i, (i64)iValue); ds@1952: } ds@1952: int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ ds@1952: int rc; ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ ds@1952: int rc; ds@1952: Vdbe *p = (Vdbe*)pStmt; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: int sqlite3_bind_text( ds@1952: sqlite3_stmt *pStmt, ds@1952: int i, ds@1952: const char *zData, ds@1952: int nData, ds@1952: void (*xDel)(void*) ds@1952: ){ ds@1952: return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: int sqlite3_bind_text16( ds@1952: sqlite3_stmt *pStmt, ds@1952: int i, ds@1952: const void *zData, ds@1952: int nData, ds@1952: void (*xDel)(void*) ds@1952: ){ ds@1952: return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ ds@1952: int rc; ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: if( rc==SQLITE_OK ){ ds@1952: rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ ds@1952: int rc; ds@1952: Vdbe *p = (Vdbe *)pStmt; ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: rc = vdbeUnbind(p, i); ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of wildcards that can be potentially bound to. ds@1952: ** This routine is added to support DBD::SQLite. ds@1952: */ ds@1952: int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ ds@1952: Vdbe *p = (Vdbe*)pStmt; ds@1952: return p ? p->nVar : 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Create a mapping from variable numbers to variable names ds@1952: ** in the Vdbe.azVar[] array, if such a mapping does not already ds@1952: ** exist. ds@1952: */ ds@1952: static void createVarMap(Vdbe *p){ ds@1952: if( !p->okVar ){ ds@1952: sqlite3_mutex_enter(p->db->mutex); ds@1952: if( !p->okVar ){ ds@1952: int j; ds@1952: Op *pOp; ds@1952: for(j=0, pOp=p->aOp; jnOp; j++, pOp++){ ds@1952: if( pOp->opcode==OP_Variable ){ ds@1952: assert( pOp->p1>0 && pOp->p1<=p->nVar ); ds@1952: p->azVar[pOp->p1-1] = pOp->p3; ds@1952: } ds@1952: } ds@1952: p->okVar = 1; ds@1952: } ds@1952: sqlite3_mutex_leave(p->db->mutex); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the name of a wildcard parameter. Return NULL if the index ds@1952: ** is out of range or if the wildcard is unnamed. ds@1952: ** ds@1952: ** The result is always UTF-8. ds@1952: */ ds@1952: const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ ds@1952: Vdbe *p = (Vdbe*)pStmt; ds@1952: if( p==0 || i<1 || i>p->nVar ){ ds@1952: return 0; ds@1952: } ds@1952: createVarMap(p); ds@1952: return p->azVar[i-1]; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Given a wildcard parameter name, return the index of the variable ds@1952: ** with that name. If there is no variable with the given name, ds@1952: ** return 0. ds@1952: */ ds@1952: int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ ds@1952: Vdbe *p = (Vdbe*)pStmt; ds@1952: int i; ds@1952: if( p==0 ){ ds@1952: return 0; ds@1952: } ds@1952: createVarMap(p); ds@1952: if( zName ){ ds@1952: for(i=0; inVar; i++){ ds@1952: const char *z = p->azVar[i]; ds@1952: if( z && strcmp(z,zName)==0 ){ ds@1952: return i+1; ds@1952: } ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Transfer all bindings from the first statement over to the second. ds@1952: ** If the two statements contain a different number of bindings, then ds@1952: ** an SQLITE_ERROR is returned. ds@1952: */ ds@1952: int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ ds@1952: Vdbe *pFrom = (Vdbe*)pFromStmt; ds@1952: Vdbe *pTo = (Vdbe*)pToStmt; ds@1952: int i, rc = SQLITE_OK; ds@1952: if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) ds@1952: || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ds@1952: || pTo->db!=pFrom->db ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( pFrom->nVar!=pTo->nVar ){ ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: sqlite3_mutex_enter(pTo->db->mutex); ds@1952: for(i=0; rc==SQLITE_OK && inVar; i++){ ds@1952: sqlite3MallocDisallow(); ds@1952: rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); ds@1952: sqlite3MallocAllow(); ds@1952: } ds@1952: sqlite3_mutex_leave(pTo->db->mutex); ds@1952: assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the sqlite3* database handle to which the prepared statement given ds@1952: ** in the argument belongs. This is the same database handle that was ds@1952: ** the first argument to the sqlite3_prepare() that was used to create ds@1952: ** the statement in the first place. ds@1952: */ ds@1952: sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ ds@1952: return pStmt ? ((Vdbe*)pStmt)->db : 0; ds@1952: }