| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318 |
- /*
- ** 2004 May 26
- **
- ** The author disclaims copyright to this source code. In place of
- ** a legal notice, here is a blessing:
- **
- ** May you do good and not evil.
- ** May you find forgiveness for yourself and forgive others.
- ** May you share freely, never taking more than you give.
- **
- *************************************************************************
- **
- ** This file contains code use to implement APIs that are part of the
- ** VDBE.
- */
- #include "sqliteInt.h"
- #include "vdbeInt.h"
- #ifndef SQLITE_OMIT_DEPRECATED
- /*
- ** Return TRUE (non-zero) of the statement supplied as an argument needs
- ** to be recompiled. A statement needs to be recompiled whenever the
- ** execution environment changes in a way that would alter the program
- ** that sqlite3_prepare() generates. For example, if new functions or
- ** collating sequences are registered or if an authorizer function is
- ** added or changed.
- */
- int sqlite3_expired(sqlite3_stmt *pStmt){
- Vdbe *p = (Vdbe*)pStmt;
- return p==0 || p->expired;
- }
- #endif
- /*
- ** Check on a Vdbe to make sure it has not been finalized. Log
- ** an error and return true if it has been finalized (or is otherwise
- ** invalid). Return false if it is ok.
- */
- static int vdbeSafety(Vdbe *p){
- if( p->db==0 ){
- sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
- return 1;
- }else{
- return 0;
- }
- }
- static int vdbeSafetyNotNull(Vdbe *p){
- if( p==0 ){
- sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
- return 1;
- }else{
- return vdbeSafety(p);
- }
- }
- /*
- ** The following routine destroys a virtual machine that is created by
- ** the sqlite3_compile() routine. The integer returned is an SQLITE_
- ** success/failure code that describes the result of executing the virtual
- ** machine.
- **
- ** This routine sets the error code and string returned by
- ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
- */
- int sqlite3_finalize(sqlite3_stmt *pStmt){
- int rc;
- if( pStmt==0 ){
- /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
- ** pointer is a harmless no-op. */
- rc = SQLITE_OK;
- }else{
- Vdbe *v = (Vdbe*)pStmt;
- sqlite3 *db = v->db;
- if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
- sqlite3_mutex_enter(db->mutex);
- rc = sqlite3VdbeFinalize(v);
- rc = sqlite3ApiExit(db, rc);
- sqlite3LeaveMutexAndCloseZombie(db);
- }
- return rc;
- }
- /*
- ** Terminate the current execution of an SQL statement and reset it
- ** back to its starting state so that it can be reused. A success code from
- ** the prior execution is returned.
- **
- ** This routine sets the error code and string returned by
- ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
- */
- int sqlite3_reset(sqlite3_stmt *pStmt){
- int rc;
- if( pStmt==0 ){
- rc = SQLITE_OK;
- }else{
- Vdbe *v = (Vdbe*)pStmt;
- sqlite3_mutex_enter(v->db->mutex);
- rc = sqlite3VdbeReset(v);
- sqlite3VdbeRewind(v);
- assert( (rc & (v->db->errMask))==rc );
- rc = sqlite3ApiExit(v->db, rc);
- sqlite3_mutex_leave(v->db->mutex);
- }
- return rc;
- }
- /*
- ** Set all the parameters in the compiled SQL statement to NULL.
- */
- int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
- int i;
- int rc = SQLITE_OK;
- Vdbe *p = (Vdbe*)pStmt;
- #if SQLITE_THREADSAFE
- sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
- #endif
- sqlite3_mutex_enter(mutex);
- for(i=0; i<p->nVar; i++){
- sqlite3VdbeMemRelease(&p->aVar[i]);
- p->aVar[i].flags = MEM_Null;
- }
- if( p->isPrepareV2 && p->expmask ){
- p->expired = 1;
- }
- sqlite3_mutex_leave(mutex);
- return rc;
- }
- /**************************** sqlite3_value_ *******************************
- ** The following routines extract information from a Mem or sqlite3_value
- ** structure.
- */
- const void *sqlite3_value_blob(sqlite3_value *pVal){
- Mem *p = (Mem*)pVal;
- if( p->flags & (MEM_Blob|MEM_Str) ){
- sqlite3VdbeMemExpandBlob(p);
- p->flags &= ~MEM_Str;
- p->flags |= MEM_Blob;
- return p->n ? p->z : 0;
- }else{
- return sqlite3_value_text(pVal);
- }
- }
- int sqlite3_value_bytes(sqlite3_value *pVal){
- return sqlite3ValueBytes(pVal, SQLITE_UTF8);
- }
- int sqlite3_value_bytes16(sqlite3_value *pVal){
- return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
- }
- double sqlite3_value_double(sqlite3_value *pVal){
- return sqlite3VdbeRealValue((Mem*)pVal);
- }
- int sqlite3_value_int(sqlite3_value *pVal){
- return (int)sqlite3VdbeIntValue((Mem*)pVal);
- }
- sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
- return sqlite3VdbeIntValue((Mem*)pVal);
- }
- const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
- return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_value_text16(sqlite3_value* pVal){
- return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
- }
- const void *sqlite3_value_text16be(sqlite3_value *pVal){
- return sqlite3ValueText(pVal, SQLITE_UTF16BE);
- }
- const void *sqlite3_value_text16le(sqlite3_value *pVal){
- return sqlite3ValueText(pVal, SQLITE_UTF16LE);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- int sqlite3_value_type(sqlite3_value* pVal){
- return pVal->type;
- }
- /**************************** sqlite3_result_ *******************************
- ** The following routines are used by user-defined functions to specify
- ** the function result.
- **
- ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
- ** result as a string or blob but if the string or blob is too large, it
- ** then sets the error code to SQLITE_TOOBIG
- */
- static void setResultStrOrError(
- sqlite3_context *pCtx, /* Function context */
- const char *z, /* String pointer */
- int n, /* Bytes in string, or negative */
- u8 enc, /* Encoding of z. 0 for BLOBs */
- void (*xDel)(void*) /* Destructor function */
- ){
- if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
- sqlite3_result_error_toobig(pCtx);
- }
- }
- void sqlite3_result_blob(
- sqlite3_context *pCtx,
- const void *z,
- int n,
- void (*xDel)(void *)
- ){
- assert( n>=0 );
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- setResultStrOrError(pCtx, z, n, 0, xDel);
- }
- void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
- }
- void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- pCtx->isError = SQLITE_ERROR;
- pCtx->fErrorOrAux = 1;
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
- }
- #ifndef SQLITE_OMIT_UTF16
- void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- pCtx->isError = SQLITE_ERROR;
- pCtx->fErrorOrAux = 1;
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
- }
- #endif
- void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
- }
- void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
- }
- void sqlite3_result_null(sqlite3_context *pCtx){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetNull(&pCtx->s);
- }
- void sqlite3_result_text(
- sqlite3_context *pCtx,
- const char *z,
- int n,
- void (*xDel)(void *)
- ){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
- }
- #ifndef SQLITE_OMIT_UTF16
- void sqlite3_result_text16(
- sqlite3_context *pCtx,
- const void *z,
- int n,
- void (*xDel)(void *)
- ){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
- }
- void sqlite3_result_text16be(
- sqlite3_context *pCtx,
- const void *z,
- int n,
- void (*xDel)(void *)
- ){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
- }
- void sqlite3_result_text16le(
- sqlite3_context *pCtx,
- const void *z,
- int n,
- void (*xDel)(void *)
- ){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemCopy(&pCtx->s, pValue);
- }
- void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
- }
- void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
- pCtx->isError = errCode;
- pCtx->fErrorOrAux = 1;
- if( pCtx->s.flags & MEM_Null ){
- sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
- SQLITE_UTF8, SQLITE_STATIC);
- }
- }
- /* Force an SQLITE_TOOBIG error. */
- void sqlite3_result_error_toobig(sqlite3_context *pCtx){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- pCtx->isError = SQLITE_TOOBIG;
- pCtx->fErrorOrAux = 1;
- sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
- SQLITE_UTF8, SQLITE_STATIC);
- }
- /* An SQLITE_NOMEM error. */
- void sqlite3_result_error_nomem(sqlite3_context *pCtx){
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetNull(&pCtx->s);
- pCtx->isError = SQLITE_NOMEM;
- pCtx->fErrorOrAux = 1;
- pCtx->s.db->mallocFailed = 1;
- }
- /*
- ** This function is called after a transaction has been committed. It
- ** invokes callbacks registered with sqlite3_wal_hook() as required.
- */
- static int doWalCallbacks(sqlite3 *db){
- int rc = SQLITE_OK;
- #ifndef SQLITE_OMIT_WAL
- int i;
- for(i=0; i<db->nDb; i++){
- Btree *pBt = db->aDb[i].pBt;
- if( pBt ){
- int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
- if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
- rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
- }
- }
- }
- #endif
- return rc;
- }
- /*
- ** Execute the statement pStmt, either until a row of data is ready, the
- ** statement is completely executed or an error occurs.
- **
- ** This routine implements the bulk of the logic behind the sqlite_step()
- ** API. The only thing omitted is the automatic recompile if a
- ** schema change has occurred. That detail is handled by the
- ** outer sqlite3_step() wrapper procedure.
- */
- static int sqlite3Step(Vdbe *p){
- sqlite3 *db;
- int rc;
- assert(p);
- if( p->magic!=VDBE_MAGIC_RUN ){
- /* We used to require that sqlite3_reset() be called before retrying
- ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
- ** with version 3.7.0, we changed this so that sqlite3_reset() would
- ** be called automatically instead of throwing the SQLITE_MISUSE error.
- ** This "automatic-reset" change is not technically an incompatibility,
- ** since any application that receives an SQLITE_MISUSE is broken by
- ** definition.
- **
- ** Nevertheless, some published applications that were originally written
- ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
- ** returns, and those were broken by the automatic-reset change. As a
- ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
- ** legacy behavior of returning SQLITE_MISUSE for cases where the
- ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
- ** or SQLITE_BUSY error.
- */
- #ifdef SQLITE_OMIT_AUTORESET
- if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
- sqlite3_reset((sqlite3_stmt*)p);
- }else{
- return SQLITE_MISUSE_BKPT;
- }
- #else
- sqlite3_reset((sqlite3_stmt*)p);
- #endif
- }
- /* Check that malloc() has not failed. If it has, return early. */
- db = p->db;
- if( db->mallocFailed ){
- p->rc = SQLITE_NOMEM;
- return SQLITE_NOMEM;
- }
- if( p->pc<=0 && p->expired ){
- p->rc = SQLITE_SCHEMA;
- rc = SQLITE_ERROR;
- goto end_of_step;
- }
- if( p->pc<0 ){
- /* If there are no other statements currently running, then
- ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
- ** from interrupting a statement that has not yet started.
- */
- if( db->nVdbeActive==0 ){
- db->u1.isInterrupted = 0;
- }
- assert( db->nVdbeWrite>0 || db->autoCommit==0
- || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
- );
- #ifndef SQLITE_OMIT_TRACE
- if( db->xProfile && !db->init.busy ){
- sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
- }
- #endif
- db->nVdbeActive++;
- if( p->readOnly==0 ) db->nVdbeWrite++;
- if( p->bIsReader ) db->nVdbeRead++;
- p->pc = 0;
- }
- #ifndef SQLITE_OMIT_EXPLAIN
- if( p->explain ){
- rc = sqlite3VdbeList(p);
- }else
- #endif /* SQLITE_OMIT_EXPLAIN */
- {
- db->nVdbeExec++;
- rc = sqlite3VdbeExec(p);
- db->nVdbeExec--;
- }
- #ifndef SQLITE_OMIT_TRACE
- /* Invoke the profile callback if there is one
- */
- if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
- sqlite3_int64 iNow;
- sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
- db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
- }
- #endif
- if( rc==SQLITE_DONE ){
- assert( p->rc==SQLITE_OK );
- p->rc = doWalCallbacks(db);
- if( p->rc!=SQLITE_OK ){
- rc = SQLITE_ERROR;
- }
- }
- db->errCode = rc;
- if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
- p->rc = SQLITE_NOMEM;
- }
- end_of_step:
- /* At this point local variable rc holds the value that should be
- ** returned if this statement was compiled using the legacy
- ** sqlite3_prepare() interface. According to the docs, this can only
- ** be one of the values in the first assert() below. Variable p->rc
- ** contains the value that would be returned if sqlite3_finalize()
- ** were called on statement p.
- */
- assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
- || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
- );
- assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
- if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
- /* If this statement was prepared using sqlite3_prepare_v2(), and an
- ** error has occurred, then return the error code in p->rc to the
- ** caller. Set the error code in the database handle to the same value.
- */
- rc = sqlite3VdbeTransferError(p);
- }
- return (rc&db->errMask);
- }
- /*
- ** This is the top-level implementation of sqlite3_step(). Call
- ** sqlite3Step() to do most of the work. If a schema error occurs,
- ** call sqlite3Reprepare() and try again.
- */
- int sqlite3_step(sqlite3_stmt *pStmt){
- int rc = SQLITE_OK; /* Result from sqlite3Step() */
- int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
- Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
- int cnt = 0; /* Counter to prevent infinite loop of reprepares */
- sqlite3 *db; /* The database connection */
- if( vdbeSafetyNotNull(v) ){
- return SQLITE_MISUSE_BKPT;
- }
- db = v->db;
- sqlite3_mutex_enter(db->mutex);
- v->doingRerun = 0;
- while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
- && cnt++ < SQLITE_MAX_SCHEMA_RETRY
- && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
- sqlite3_reset(pStmt);
- v->doingRerun = 1;
- assert( v->expired==0 );
- }
- if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
- /* This case occurs after failing to recompile an sql statement.
- ** The error message from the SQL compiler has already been loaded
- ** into the database handle. This block copies the error message
- ** from the database handle into the statement and sets the statement
- ** program counter to 0 to ensure that when the statement is
- ** finalized or reset the parser error message is available via
- ** sqlite3_errmsg() and sqlite3_errcode().
- */
- const char *zErr = (const char *)sqlite3_value_text(db->pErr);
- sqlite3DbFree(db, v->zErrMsg);
- if( !db->mallocFailed ){
- v->zErrMsg = sqlite3DbStrDup(db, zErr);
- v->rc = rc2;
- } else {
- v->zErrMsg = 0;
- v->rc = rc = SQLITE_NOMEM;
- }
- }
- rc = sqlite3ApiExit(db, rc);
- sqlite3_mutex_leave(db->mutex);
- return rc;
- }
- /*
- ** Extract the user data from a sqlite3_context structure and return a
- ** pointer to it.
- */
- void *sqlite3_user_data(sqlite3_context *p){
- assert( p && p->pFunc );
- return p->pFunc->pUserData;
- }
- /*
- ** Extract the user data from a sqlite3_context structure and return a
- ** pointer to it.
- **
- ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
- ** returns a copy of the pointer to the database connection (the 1st
- ** parameter) of the sqlite3_create_function() and
- ** sqlite3_create_function16() routines that originally registered the
- ** application defined function.
- */
- sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
- assert( p && p->pFunc );
- return p->s.db;
- }
- /*
- ** Return the current time for a statement
- */
- sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
- Vdbe *v = p->pVdbe;
- int rc;
- if( v->iCurrentTime==0 ){
- rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
- if( rc ) v->iCurrentTime = 0;
- }
- return v->iCurrentTime;
- }
- /*
- ** The following is the implementation of an SQL function that always
- ** fails with an error message stating that the function is used in the
- ** wrong context. The sqlite3_overload_function() API might construct
- ** SQL function that use this routine so that the functions will exist
- ** for name resolution but are actually overloaded by the xFindFunction
- ** method of virtual tables.
- */
- void sqlite3InvalidFunction(
- sqlite3_context *context, /* The function calling context */
- int NotUsed, /* Number of arguments to the function */
- sqlite3_value **NotUsed2 /* Value of each argument */
- ){
- const char *zName = context->pFunc->zName;
- char *zErr;
- UNUSED_PARAMETER2(NotUsed, NotUsed2);
- zErr = sqlite3_mprintf(
- "unable to use function %s in the requested context", zName);
- sqlite3_result_error(context, zErr, -1);
- sqlite3_free(zErr);
- }
- /*
- ** Allocate or return the aggregate context for a user function. A new
- ** context is allocated on the first call. Subsequent calls return the
- ** same context that was returned on prior calls.
- */
- void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
- Mem *pMem;
- assert( p && p->pFunc && p->pFunc->xStep );
- assert( sqlite3_mutex_held(p->s.db->mutex) );
- pMem = p->pMem;
- testcase( nByte<0 );
- if( (pMem->flags & MEM_Agg)==0 ){
- if( nByte<=0 ){
- sqlite3VdbeMemReleaseExternal(pMem);
- pMem->flags = MEM_Null;
- pMem->z = 0;
- }else{
- sqlite3VdbeMemGrow(pMem, nByte, 0);
- pMem->flags = MEM_Agg;
- pMem->u.pDef = p->pFunc;
- if( pMem->z ){
- memset(pMem->z, 0, nByte);
- }
- }
- }
- return (void*)pMem->z;
- }
- /*
- ** Return the auxilary data pointer, if any, for the iArg'th argument to
- ** the user-function defined by pCtx.
- */
- void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
- AuxData *pAuxData;
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
- if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
- }
- return (pAuxData ? pAuxData->pAux : 0);
- }
- /*
- ** Set the auxilary data pointer and delete function, for the iArg'th
- ** argument to the user-function defined by pCtx. Any previous value is
- ** deleted by calling the delete function specified when it was set.
- */
- void sqlite3_set_auxdata(
- sqlite3_context *pCtx,
- int iArg,
- void *pAux,
- void (*xDelete)(void*)
- ){
- AuxData *pAuxData;
- Vdbe *pVdbe = pCtx->pVdbe;
- assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- if( iArg<0 ) goto failed;
- for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
- if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
- }
- if( pAuxData==0 ){
- pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
- if( !pAuxData ) goto failed;
- pAuxData->iOp = pCtx->iOp;
- pAuxData->iArg = iArg;
- pAuxData->pNext = pVdbe->pAuxData;
- pVdbe->pAuxData = pAuxData;
- if( pCtx->fErrorOrAux==0 ){
- pCtx->isError = 0;
- pCtx->fErrorOrAux = 1;
- }
- }else if( pAuxData->xDelete ){
- pAuxData->xDelete(pAuxData->pAux);
- }
- pAuxData->pAux = pAux;
- pAuxData->xDelete = xDelete;
- return;
- failed:
- if( xDelete ){
- xDelete(pAux);
- }
- }
- #ifndef SQLITE_OMIT_DEPRECATED
- /*
- ** Return the number of times the Step function of a aggregate has been
- ** called.
- **
- ** This function is deprecated. Do not use it for new code. It is
- ** provide only to avoid breaking legacy code. New aggregate function
- ** implementations should keep their own counts within their aggregate
- ** context.
- */
- int sqlite3_aggregate_count(sqlite3_context *p){
- assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
- return p->pMem->n;
- }
- #endif
- /*
- ** Return the number of columns in the result set for the statement pStmt.
- */
- int sqlite3_column_count(sqlite3_stmt *pStmt){
- Vdbe *pVm = (Vdbe *)pStmt;
- return pVm ? pVm->nResColumn : 0;
- }
- /*
- ** Return the number of values available from the current row of the
- ** currently executing statement pStmt.
- */
- int sqlite3_data_count(sqlite3_stmt *pStmt){
- Vdbe *pVm = (Vdbe *)pStmt;
- if( pVm==0 || pVm->pResultSet==0 ) return 0;
- return pVm->nResColumn;
- }
- /*
- ** Check to see if column iCol of the given statement is valid. If
- ** it is, return a pointer to the Mem for the value of that column.
- ** If iCol is not valid, return a pointer to a Mem which has a value
- ** of NULL.
- */
- static Mem *columnMem(sqlite3_stmt *pStmt, int i){
- Vdbe *pVm;
- Mem *pOut;
- pVm = (Vdbe *)pStmt;
- if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
- sqlite3_mutex_enter(pVm->db->mutex);
- pOut = &pVm->pResultSet[i];
- }else{
- /* If the value passed as the second argument is out of range, return
- ** a pointer to the following static Mem object which contains the
- ** value SQL NULL. Even though the Mem structure contains an element
- ** of type i64, on certain architectures (x86) with certain compiler
- ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
- ** instead of an 8-byte one. This all works fine, except that when
- ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
- ** that a Mem structure is located on an 8-byte boundary. To prevent
- ** these assert()s from failing, when building with SQLITE_DEBUG defined
- ** using gcc, we force nullMem to be 8-byte aligned using the magical
- ** __attribute__((aligned(8))) macro. */
- static const Mem nullMem
- #if defined(SQLITE_DEBUG) && defined(__GNUC__)
- __attribute__((aligned(8)))
- #endif
- = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
- #ifdef SQLITE_DEBUG
- 0, 0, /* pScopyFrom, pFiller */
- #endif
- 0, 0 };
- if( pVm && ALWAYS(pVm->db) ){
- sqlite3_mutex_enter(pVm->db->mutex);
- sqlite3Error(pVm->db, SQLITE_RANGE, 0);
- }
- pOut = (Mem*)&nullMem;
- }
- return pOut;
- }
- /*
- ** This function is called after invoking an sqlite3_value_XXX function on a
- ** column value (i.e. a value returned by evaluating an SQL expression in the
- ** select list of a SELECT statement) that may cause a malloc() failure. If
- ** malloc() has failed, the threads mallocFailed flag is cleared and the result
- ** code of statement pStmt set to SQLITE_NOMEM.
- **
- ** Specifically, this is called from within:
- **
- ** sqlite3_column_int()
- ** sqlite3_column_int64()
- ** sqlite3_column_text()
- ** sqlite3_column_text16()
- ** sqlite3_column_real()
- ** sqlite3_column_bytes()
- ** sqlite3_column_bytes16()
- ** sqiite3_column_blob()
- */
- static void columnMallocFailure(sqlite3_stmt *pStmt)
- {
- /* If malloc() failed during an encoding conversion within an
- ** sqlite3_column_XXX API, then set the return code of the statement to
- ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
- ** and _finalize() will return NOMEM.
- */
- Vdbe *p = (Vdbe *)pStmt;
- if( p ){
- p->rc = sqlite3ApiExit(p->db, p->rc);
- sqlite3_mutex_leave(p->db->mutex);
- }
- }
- /**************************** sqlite3_column_ *******************************
- ** The following routines are used to access elements of the current row
- ** in the result set.
- */
- const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
- const void *val;
- val = sqlite3_value_blob( columnMem(pStmt,i) );
- /* Even though there is no encoding conversion, value_blob() might
- ** need to call malloc() to expand the result of a zeroblob()
- ** expression.
- */
- columnMallocFailure(pStmt);
- return val;
- }
- int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
- int val = sqlite3_value_bytes( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
- int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
- double val = sqlite3_value_double( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
- int val = sqlite3_value_int( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
- sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
- const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
- Mem *pOut = columnMem(pStmt, i);
- if( pOut->flags&MEM_Static ){
- pOut->flags &= ~MEM_Static;
- pOut->flags |= MEM_Ephem;
- }
- columnMallocFailure(pStmt);
- return (sqlite3_value *)pOut;
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
- const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return val;
- }
- #endif /* SQLITE_OMIT_UTF16 */
- int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
- int iType = sqlite3_value_type( columnMem(pStmt,i) );
- columnMallocFailure(pStmt);
- return iType;
- }
- /*
- ** Convert the N-th element of pStmt->pColName[] into a string using
- ** xFunc() then return that string. If N is out of range, return 0.
- **
- ** There are up to 5 names for each column. useType determines which
- ** name is returned. Here are the names:
- **
- ** 0 The column name as it should be displayed for output
- ** 1 The datatype name for the column
- ** 2 The name of the database that the column derives from
- ** 3 The name of the table that the column derives from
- ** 4 The name of the table column that the result column derives from
- **
- ** If the result is not a simple column reference (if it is an expression
- ** or a constant) then useTypes 2, 3, and 4 return NULL.
- */
- static const void *columnName(
- sqlite3_stmt *pStmt,
- int N,
- const void *(*xFunc)(Mem*),
- int useType
- ){
- const void *ret = 0;
- Vdbe *p = (Vdbe *)pStmt;
- int n;
- sqlite3 *db = p->db;
-
- assert( db!=0 );
- n = sqlite3_column_count(pStmt);
- if( N<n && N>=0 ){
- N += useType*n;
- sqlite3_mutex_enter(db->mutex);
- assert( db->mallocFailed==0 );
- ret = xFunc(&p->aColName[N]);
- /* A malloc may have failed inside of the xFunc() call. If this
- ** is the case, clear the mallocFailed flag and return NULL.
- */
- if( db->mallocFailed ){
- db->mallocFailed = 0;
- ret = 0;
- }
- sqlite3_mutex_leave(db->mutex);
- }
- return ret;
- }
- /*
- ** Return the name of the Nth column of the result set returned by SQL
- ** statement pStmt.
- */
- const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
- }
- #endif
- /*
- ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
- ** not define OMIT_DECLTYPE.
- */
- #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
- # error "Must not define both SQLITE_OMIT_DECLTYPE \
- and SQLITE_ENABLE_COLUMN_METADATA"
- #endif
- #ifndef SQLITE_OMIT_DECLTYPE
- /*
- ** Return the column declaration type (if applicable) of the 'i'th column
- ** of the result set of SQL statement pStmt.
- */
- const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- #endif /* SQLITE_OMIT_DECLTYPE */
- #ifdef SQLITE_ENABLE_COLUMN_METADATA
- /*
- ** Return the name of the database from which a result column derives.
- ** NULL is returned if the result column is an expression or constant or
- ** anything else which is not an unabiguous reference to a database column.
- */
- const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- /*
- ** Return the name of the table from which a result column derives.
- ** NULL is returned if the result column is an expression or constant or
- ** anything else which is not an unabiguous reference to a database column.
- */
- const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- /*
- ** Return the name of the table column from which a result column derives.
- ** NULL is returned if the result column is an expression or constant or
- ** anything else which is not an unabiguous reference to a database column.
- */
- const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
- }
- #ifndef SQLITE_OMIT_UTF16
- const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
- return columnName(
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- #endif /* SQLITE_ENABLE_COLUMN_METADATA */
- /******************************* sqlite3_bind_ ***************************
- **
- ** Routines used to attach values to wildcards in a compiled SQL statement.
- */
- /*
- ** Unbind the value bound to variable i in virtual machine p. This is the
- ** the same as binding a NULL value to the column. If the "i" parameter is
- ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
- **
- ** A successful evaluation of this routine acquires the mutex on p.
- ** the mutex is released if any kind of error occurs.
- **
- ** The error code stored in database p->db is overwritten with the return
- ** value in any case.
- */
- static int vdbeUnbind(Vdbe *p, int i){
- Mem *pVar;
- if( vdbeSafetyNotNull(p) ){
- return SQLITE_MISUSE_BKPT;
- }
- sqlite3_mutex_enter(p->db->mutex);
- if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
- sqlite3Error(p->db, SQLITE_MISUSE, 0);
- sqlite3_mutex_leave(p->db->mutex);
- sqlite3_log(SQLITE_MISUSE,
- "bind on a busy prepared statement: [%s]", p->zSql);
- return SQLITE_MISUSE_BKPT;
- }
- if( i<1 || i>p->nVar ){
- sqlite3Error(p->db, SQLITE_RANGE, 0);
- sqlite3_mutex_leave(p->db->mutex);
- return SQLITE_RANGE;
- }
- i--;
- pVar = &p->aVar[i];
- sqlite3VdbeMemRelease(pVar);
- pVar->flags = MEM_Null;
- sqlite3Error(p->db, SQLITE_OK, 0);
- /* If the bit corresponding to this variable in Vdbe.expmask is set, then
- ** binding a new value to this variable invalidates the current query plan.
- **
- ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
- ** parameter in the WHERE clause might influence the choice of query plan
- ** for a statement, then the statement will be automatically recompiled,
- ** as if there had been a schema change, on the first sqlite3_step() call
- ** following any change to the bindings of that parameter.
- */
- if( p->isPrepareV2 &&
- ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
- ){
- p->expired = 1;
- }
- return SQLITE_OK;
- }
- /*
- ** Bind a text or BLOB value.
- */
- static int bindText(
- sqlite3_stmt *pStmt, /* The statement to bind against */
- int i, /* Index of the parameter to bind */
- const void *zData, /* Pointer to the data to be bound */
- int nData, /* Number of bytes of data to be bound */
- void (*xDel)(void*), /* Destructor for the data */
- u8 encoding /* Encoding for the data */
- ){
- Vdbe *p = (Vdbe *)pStmt;
- Mem *pVar;
- int rc;
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- if( zData!=0 ){
- pVar = &p->aVar[i-1];
- rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
- if( rc==SQLITE_OK && encoding!=0 ){
- rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
- }
- sqlite3Error(p->db, rc, 0);
- rc = sqlite3ApiExit(p->db, rc);
- }
- sqlite3_mutex_leave(p->db->mutex);
- }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
- xDel((void*)zData);
- }
- return rc;
- }
- /*
- ** Bind a blob value to an SQL statement variable.
- */
- int sqlite3_bind_blob(
- sqlite3_stmt *pStmt,
- int i,
- const void *zData,
- int nData,
- void (*xDel)(void*)
- ){
- return bindText(pStmt, i, zData, nData, xDel, 0);
- }
- int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
- int rc;
- Vdbe *p = (Vdbe *)pStmt;
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
- sqlite3_mutex_leave(p->db->mutex);
- }
- return rc;
- }
- int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
- return sqlite3_bind_int64(p, i, (i64)iValue);
- }
- int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
- int rc;
- Vdbe *p = (Vdbe *)pStmt;
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
- sqlite3_mutex_leave(p->db->mutex);
- }
- return rc;
- }
- int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
- int rc;
- Vdbe *p = (Vdbe*)pStmt;
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- sqlite3_mutex_leave(p->db->mutex);
- }
- return rc;
- }
- int sqlite3_bind_text(
- sqlite3_stmt *pStmt,
- int i,
- const char *zData,
- int nData,
- void (*xDel)(void*)
- ){
- return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
- }
- #ifndef SQLITE_OMIT_UTF16
- int sqlite3_bind_text16(
- sqlite3_stmt *pStmt,
- int i,
- const void *zData,
- int nData,
- void (*xDel)(void*)
- ){
- return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
- }
- #endif /* SQLITE_OMIT_UTF16 */
- int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
- int rc;
- switch( pValue->type ){
- case SQLITE_INTEGER: {
- rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
- break;
- }
- case SQLITE_FLOAT: {
- rc = sqlite3_bind_double(pStmt, i, pValue->r);
- break;
- }
- case SQLITE_BLOB: {
- if( pValue->flags & MEM_Zero ){
- rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
- }else{
- rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
- }
- break;
- }
- case SQLITE_TEXT: {
- rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
- pValue->enc);
- break;
- }
- default: {
- rc = sqlite3_bind_null(pStmt, i);
- break;
- }
- }
- return rc;
- }
- int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
- int rc;
- Vdbe *p = (Vdbe *)pStmt;
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
- sqlite3_mutex_leave(p->db->mutex);
- }
- return rc;
- }
- /*
- ** Return the number of wildcards that can be potentially bound to.
- ** This routine is added to support DBD::SQLite.
- */
- int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
- Vdbe *p = (Vdbe*)pStmt;
- return p ? p->nVar : 0;
- }
- /*
- ** Return the name of a wildcard parameter. Return NULL if the index
- ** is out of range or if the wildcard is unnamed.
- **
- ** The result is always UTF-8.
- */
- const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
- Vdbe *p = (Vdbe*)pStmt;
- if( p==0 || i<1 || i>p->nzVar ){
- return 0;
- }
- return p->azVar[i-1];
- }
- /*
- ** Given a wildcard parameter name, return the index of the variable
- ** with that name. If there is no variable with the given name,
- ** return 0.
- */
- int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
- int i;
- if( p==0 ){
- return 0;
- }
- if( zName ){
- for(i=0; i<p->nzVar; i++){
- const char *z = p->azVar[i];
- if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
- return i+1;
- }
- }
- }
- return 0;
- }
- int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
- return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
- }
- /*
- ** Transfer all bindings from the first statement over to the second.
- */
- int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
- Vdbe *pFrom = (Vdbe*)pFromStmt;
- Vdbe *pTo = (Vdbe*)pToStmt;
- int i;
- assert( pTo->db==pFrom->db );
- assert( pTo->nVar==pFrom->nVar );
- sqlite3_mutex_enter(pTo->db->mutex);
- for(i=0; i<pFrom->nVar; i++){
- sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
- }
- sqlite3_mutex_leave(pTo->db->mutex);
- return SQLITE_OK;
- }
- #ifndef SQLITE_OMIT_DEPRECATED
- /*
- ** Deprecated external interface. Internal/core SQLite code
- ** should call sqlite3TransferBindings.
- **
- ** Is is misuse to call this routine with statements from different
- ** database connections. But as this is a deprecated interface, we
- ** will not bother to check for that condition.
- **
- ** If the two statements contain a different number of bindings, then
- ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
- ** SQLITE_OK is returned.
- */
- int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
- Vdbe *pFrom = (Vdbe*)pFromStmt;
- Vdbe *pTo = (Vdbe*)pToStmt;
- if( pFrom->nVar!=pTo->nVar ){
- return SQLITE_ERROR;
- }
- if( pTo->isPrepareV2 && pTo->expmask ){
- pTo->expired = 1;
- }
- if( pFrom->isPrepareV2 && pFrom->expmask ){
- pFrom->expired = 1;
- }
- return sqlite3TransferBindings(pFromStmt, pToStmt);
- }
- #endif
- /*
- ** Return the sqlite3* database handle to which the prepared statement given
- ** in the argument belongs. This is the same database handle that was
- ** the first argument to the sqlite3_prepare() that was used to create
- ** the statement in the first place.
- */
- sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
- return pStmt ? ((Vdbe*)pStmt)->db : 0;
- }
- /*
- ** Return true if the prepared statement is guaranteed to not modify the
- ** database.
- */
- int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
- return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
- }
- /*
- ** Return true if the prepared statement is in need of being reset.
- */
- int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
- Vdbe *v = (Vdbe*)pStmt;
- return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
- }
- /*
- ** Return a pointer to the next prepared statement after pStmt associated
- ** with database connection pDb. If pStmt is NULL, return the first
- ** prepared statement for the database connection. Return NULL if there
- ** are no more.
- */
- sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
- sqlite3_stmt *pNext;
- sqlite3_mutex_enter(pDb->mutex);
- if( pStmt==0 ){
- pNext = (sqlite3_stmt*)pDb->pVdbe;
- }else{
- pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
- }
- sqlite3_mutex_leave(pDb->mutex);
- return pNext;
- }
- /*
- ** Return the value of a status counter for a prepared statement
- */
- int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
- Vdbe *pVdbe = (Vdbe*)pStmt;
- u32 v = pVdbe->aCounter[op];
- if( resetFlag ) pVdbe->aCounter[op] = 0;
- return (int)v;
- }
|