summaryrefslogtreecommitdiffstats
path: root/third_party/sqlite/src/vdbeapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/sqlite/src/vdbeapi.c')
-rw-r--r--third_party/sqlite/src/vdbeapi.c498
1 files changed, 214 insertions, 284 deletions
diff --git a/third_party/sqlite/src/vdbeapi.c b/third_party/sqlite/src/vdbeapi.c
index 880c4ae..634c29c4 100644
--- a/third_party/sqlite/src/vdbeapi.c
+++ b/third_party/sqlite/src/vdbeapi.c
@@ -13,167 +13,12 @@
** This file contains code use to implement APIs that are part of the
** VDBE.
**
-** $Id: vdbeapi.c,v 1.138 2008/08/02 03:50:39 drh Exp $
+** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
*/
#include "sqliteInt.h"
#include "vdbeInt.h"
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-/*
-** The following structure contains pointers to the end points of a
-** doubly-linked list of all compiled SQL statements that may be holding
-** buffers eligible for release when the sqlite3_release_memory() interface is
-** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
-** mutex.
-**
-** Statements are added to the end of this list when sqlite3_reset() is
-** called. They are removed either when sqlite3_step() or sqlite3_finalize()
-** is called. When statements are added to this list, the associated
-** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
-** can be freed using sqlite3VdbeReleaseMemory().
-**
-** When statements are added or removed from this list, the mutex
-** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
-** already held. The LRU2 mutex is then obtained, blocking if necessary,
-** the linked-list pointers manipulated and the LRU2 mutex relinquished.
-*/
-struct StatementLruList {
- Vdbe *pFirst;
- Vdbe *pLast;
-};
-static struct StatementLruList sqlite3LruStatements;
-
-/*
-** Check that the list looks to be internally consistent. This is used
-** as part of an assert() statement as follows:
-**
-** assert( stmtLruCheck() );
-*/
-#ifndef NDEBUG
-static int stmtLruCheck(){
- Vdbe *p;
- for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
- assert(p->pLruNext || p==sqlite3LruStatements.pLast);
- assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
- assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
- assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
- }
- return 1;
-}
-#endif
-
-/*
-** Add vdbe p to the end of the statement lru list. It is assumed that
-** p is not already part of the list when this is called. The lru list
-** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
-*/
-static void stmtLruAdd(Vdbe *p){
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
-
- if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
- return;
- }
-
- assert( stmtLruCheck() );
-
- if( !sqlite3LruStatements.pFirst ){
- assert( !sqlite3LruStatements.pLast );
- sqlite3LruStatements.pFirst = p;
- sqlite3LruStatements.pLast = p;
- }else{
- assert( !sqlite3LruStatements.pLast->pLruNext );
- p->pLruPrev = sqlite3LruStatements.pLast;
- sqlite3LruStatements.pLast->pLruNext = p;
- sqlite3LruStatements.pLast = p;
- }
-
- assert( stmtLruCheck() );
-
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
-}
-
-/*
-** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
-** statement p from the least-recently-used statement list. If the
-** statement is not currently part of the list, this call is a no-op.
-*/
-static void stmtLruRemoveNomutex(Vdbe *p){
- if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
- assert( stmtLruCheck() );
- if( p->pLruNext ){
- p->pLruNext->pLruPrev = p->pLruPrev;
- }else{
- sqlite3LruStatements.pLast = p->pLruPrev;
- }
- if( p->pLruPrev ){
- p->pLruPrev->pLruNext = p->pLruNext;
- }else{
- sqlite3LruStatements.pFirst = p->pLruNext;
- }
- p->pLruNext = 0;
- p->pLruPrev = 0;
- assert( stmtLruCheck() );
- }
-}
-
-/*
-** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
-** statement p from the least-recently-used statement list. If the
-** statement is not currently part of the list, this call is a no-op.
-*/
-static void stmtLruRemove(Vdbe *p){
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
- stmtLruRemoveNomutex(p);
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
-}
-
-/*
-** Try to release n bytes of memory by freeing buffers associated
-** with the memory registers of currently unused vdbes.
-*/
-int sqlite3VdbeReleaseMemory(int n){
- Vdbe *p;
- Vdbe *pNext;
- int nFree = 0;
-
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
- for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
- pNext = p->pLruNext;
-
- /* For each statement handle in the lru list, attempt to obtain the
- ** associated database mutex. If it cannot be obtained, continue
- ** to the next statement handle. It is not possible to block on
- ** the database mutex - that could cause deadlock.
- */
- if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
- nFree += sqlite3VdbeReleaseBuffers(p);
- stmtLruRemoveNomutex(p);
- sqlite3_mutex_leave(p->db->mutex);
- }
- }
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
-
- return nFree;
-}
-
-/*
-** Call sqlite3Reprepare() on the statement. Remove it from the
-** lru list before doing so, as Reprepare() will free all the
-** memory register buffers anyway.
-*/
-int vdbeReprepare(Vdbe *p){
- stmtLruRemove(p);
- return sqlite3Reprepare(p);
-}
-
-#else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
- #define stmtLruRemove(x)
- #define stmtLruAdd(x)
- #define vdbeReprepare(x) sqlite3Reprepare(x)
-#endif
-
-
+#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
@@ -186,6 +31,7 @@ int sqlite3_expired(sqlite3_stmt *pStmt){
Vdbe *p = (Vdbe*)pStmt;
return p==0 || p->expired;
}
+#endif
/*
** The following routine destroys a virtual machine that is created by
@@ -202,12 +48,13 @@ int sqlite3_finalize(sqlite3_stmt *pStmt){
rc = SQLITE_OK;
}else{
Vdbe *v = (Vdbe*)pStmt;
-#ifndef SQLITE_MUTEX_NOOP
+ sqlite3 *db = v->db;
+#if SQLITE_THREADSAFE
sqlite3_mutex *mutex = v->db->mutex;
#endif
sqlite3_mutex_enter(mutex);
- stmtLruRemove(v);
rc = sqlite3VdbeFinalize(v);
+ rc = sqlite3ApiExit(db, rc);
sqlite3_mutex_leave(mutex);
}
return rc;
@@ -229,9 +76,9 @@ int sqlite3_reset(sqlite3_stmt *pStmt){
Vdbe *v = (Vdbe*)pStmt;
sqlite3_mutex_enter(v->db->mutex);
rc = sqlite3VdbeReset(v);
- stmtLruAdd(v);
- sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
+ sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
assert( (rc & (v->db->errMask))==rc );
+ rc = sqlite3ApiExit(v->db, rc);
sqlite3_mutex_leave(v->db->mutex);
}
return rc;
@@ -244,7 +91,7 @@ int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
int i;
int rc = SQLITE_OK;
Vdbe *p = (Vdbe*)pStmt;
-#ifndef SQLITE_MUTEX_NOOP
+#if SQLITE_THREADSAFE
sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
#endif
sqlite3_mutex_enter(mutex);
@@ -282,7 +129,7 @@ double sqlite3_value_double(sqlite3_value *pVal){
return sqlite3VdbeRealValue((Mem*)pVal);
}
int sqlite3_value_int(sqlite3_value *pVal){
- return sqlite3VdbeIntValue((Mem*)pVal);
+ return (int)sqlite3VdbeIntValue((Mem*)pVal);
}
sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
return sqlite3VdbeIntValue((Mem*)pVal);
@@ -308,7 +155,22 @@ int sqlite3_value_type(sqlite3_value* pVal){
/**************************** 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,
@@ -317,7 +179,7 @@ void sqlite3_result_blob(
){
assert( n>=0 );
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
+ setResultStrOrError(pCtx, z, n, 0, xDel);
}
void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
@@ -354,7 +216,7 @@ void sqlite3_result_text(
void (*xDel)(void *)
){
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
}
#ifndef SQLITE_OMIT_UTF16
void sqlite3_result_text16(
@@ -364,7 +226,7 @@ void sqlite3_result_text16(
void (*xDel)(void *)
){
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
}
void sqlite3_result_text16be(
sqlite3_context *pCtx,
@@ -373,7 +235,7 @@ void sqlite3_result_text16be(
void (*xDel)(void *)
){
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
}
void sqlite3_result_text16le(
sqlite3_context *pCtx,
@@ -382,7 +244,7 @@ void sqlite3_result_text16le(
void (*xDel)(void *)
){
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
}
#endif /* SQLITE_OMIT_UTF16 */
void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
@@ -395,6 +257,10 @@ void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
}
void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
pCtx->isError = errCode;
+ if( pCtx->s.flags & MEM_Null ){
+ sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
+ SQLITE_UTF8, SQLITE_STATIC);
+ }
}
/* Force an SQLITE_TOOBIG error. */
@@ -433,10 +299,12 @@ static int sqlite3Step(Vdbe *p){
/* Assert that malloc() has not failed */
db = p->db;
- assert( !db->mallocFailed );
+ if( db->mallocFailed ){
+ return SQLITE_NOMEM;
+ }
if( p->pc<=0 && p->expired ){
- if( p->rc==SQLITE_OK ){
+ if( ALWAYS(p->rc==SQLITE_OK) ){
p->rc = SQLITE_SCHEMA;
}
rc = SQLITE_ERROR;
@@ -459,13 +327,13 @@ static int sqlite3Step(Vdbe *p){
if( db->xProfile && !db->init.busy ){
double rNow;
sqlite3OsCurrentTime(db->pVfs, &rNow);
- p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
+ p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
}
#endif
db->activeVdbeCnt++;
+ if( p->readOnly==0 ) db->writeVdbeCnt++;
p->pc = 0;
- stmtLruRemove(p);
}
#ifndef SQLITE_OMIT_EXPLAIN
if( p->explain ){
@@ -483,33 +351,41 @@ static int sqlite3Step(Vdbe *p){
#ifndef SQLITE_OMIT_TRACE
/* Invoke the profile callback if there is one
*/
- if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
- && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
+ if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
double rNow;
u64 elapseTime;
sqlite3OsCurrentTime(db->pVfs, &rNow);
- elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
- db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
+ elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
+ elapseTime -= p->startTime;
+ db->xProfile(db->pProfileArg, p->zSql, elapseTime);
}
#endif
db->errCode = rc;
- /*sqlite3Error(p->db, rc, 0);*/
- p->rc = sqlite3ApiExit(p->db, p->rc);
+ if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
+ p->rc = SQLITE_NOMEM;
+ }
end_of_step:
- assert( (rc&0xff)==rc );
- if( p->zSql && (rc&0xff)<SQLITE_ROW ){
- /* This behavior occurs if sqlite3_prepare_v2() was used to build
- ** the prepared statement. Return error codes directly */
- p->db->errCode = p->rc;
- /* sqlite3Error(p->db, p->rc, 0); */
- return p->rc;
- }else{
- /* This is for legacy sqlite3_prepare() builds and when the code
- ** is SQLITE_ROW or SQLITE_DONE */
- return rc;
+ /* 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 occured, then return the error code in p->rc to the
+ ** caller. Set the error code in the database handle to the same value.
+ */
+ rc = db->errCode = p->rc;
}
+ return (rc&db->errMask);
}
/*
@@ -517,19 +393,6 @@ end_of_step:
** sqlite3Step() to do most of the work. If a schema error occurs,
** call sqlite3Reprepare() and try again.
*/
-#ifdef SQLITE_OMIT_PARSER
-int sqlite3_step(sqlite3_stmt *pStmt){
- int rc = SQLITE_MISUSE;
- if( pStmt ){
- Vdbe *v;
- v = (Vdbe*)pStmt;
- sqlite3_mutex_enter(v->db->mutex);
- rc = sqlite3Step(v);
- sqlite3_mutex_leave(v->db->mutex);
- }
- return rc;
-}
-#else
int sqlite3_step(sqlite3_stmt *pStmt){
int rc = SQLITE_MISUSE;
if( pStmt ){
@@ -539,11 +402,11 @@ int sqlite3_step(sqlite3_stmt *pStmt){
sqlite3_mutex_enter(db->mutex);
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
&& cnt++ < 5
- && vdbeReprepare(v) ){
+ && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
sqlite3_reset(pStmt);
v->expired = 0;
}
- if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
+ if( rc==SQLITE_SCHEMA && 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
@@ -566,7 +429,6 @@ int sqlite3_step(sqlite3_stmt *pStmt){
}
return rc;
}
-#endif
/*
** Extract the user data from a sqlite3_context structure and return a
@@ -596,12 +458,13 @@ sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
*/
void sqlite3InvalidFunction(
sqlite3_context *context, /* The function calling context */
- int argc, /* Number of arguments to the function */
- sqlite3_value **argv /* Value of each argument */
+ int NotUsed, /* Number of arguments to the function */
+ sqlite3_value **NotUsed2 /* Value of each argument */
){
const char *zName = context->pFunc->zName;
char *zErr;
- zErr = sqlite3MPrintf(0,
+ 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);
@@ -693,6 +556,7 @@ failed:
}
}
+#ifndef SQLITE_OMIT_DEPRECATED
/*
** Return the number of times the Step function of a aggregate has been
** called.
@@ -703,9 +567,10 @@ failed:
** context.
*/
int sqlite3_aggregate_count(sqlite3_context *p){
- assert( p && p->pFunc && p->pFunc->xStep );
+ 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.
@@ -743,8 +608,24 @@ static Mem *columnMem(sqlite3_stmt *pStmt, int i){
vals = sqlite3_data_count(pStmt);
pOut = &pVm->pResultSet[i];
}else{
- static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
- if( pVm->db ){
+ /* 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 architecture (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
+ ** this assert() from failing, when building with SQLITE_DEBUG defined
+ ** using gcc, 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, 0, 0 };
+
+ if( pVm && ALWAYS(pVm->db) ){
sqlite3_mutex_enter(pVm->db->mutex);
sqlite3Error(pVm->db, SQLITE_RANGE, 0);
}
@@ -831,9 +712,13 @@ const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
return val;
}
sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
- sqlite3_value *pOut = columnMem(pStmt, i);
+ Mem *pOut = columnMem(pStmt, i);
+ if( pOut->flags&MEM_Static ){
+ pOut->flags &= ~MEM_Static;
+ pOut->flags |= MEM_Ephem;
+ }
columnMallocFailure(pStmt);
- return pOut;
+ return (sqlite3_value *)pOut;
}
#ifndef SQLITE_OMIT_UTF16
const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
@@ -880,24 +765,23 @@ static const void *columnName(
const void *ret = 0;
Vdbe *p = (Vdbe *)pStmt;
int n;
+ sqlite3 *db = p->db;
-
- if( p!=0 ){
- n = sqlite3_column_count(pStmt);
- if( N<n && N>=0 ){
- N += useType*n;
- sqlite3_mutex_enter(p->db->mutex);
- 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( p->db && p->db->mallocFailed ){
- p->db->mallocFailed = 0;
- ret = 0;
- }
- sqlite3_mutex_leave(p->db->mutex);
+ 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;
}
@@ -1003,17 +887,24 @@ const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
** 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( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
- if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
+ if( p==0 ) return SQLITE_MISUSE;
+ 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);
return SQLITE_MISUSE;
}
if( i<1 || i>p->nVar ){
sqlite3Error(p->db, SQLITE_RANGE, 0);
+ sqlite3_mutex_leave(p->db->mutex);
return SQLITE_RANGE;
}
i--;
@@ -1033,27 +924,25 @@ static int bindText(
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 */
- int encoding /* Encoding for the data */
+ u8 encoding /* Encoding for the data */
){
Vdbe *p = (Vdbe *)pStmt;
Mem *pVar;
int rc;
- if( p==0 ){
- return SQLITE_MISUSE;
- }
- sqlite3_mutex_enter(p->db->mutex);
rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK && 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));
+ 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);
}
- sqlite3Error(p->db, rc, 0);
- rc = sqlite3ApiExit(p->db, rc);
+ sqlite3_mutex_leave(p->db->mutex);
}
- sqlite3_mutex_leave(p->db->mutex);
return rc;
}
@@ -1073,12 +962,11 @@ int sqlite3_bind_blob(
int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
int rc;
Vdbe *p = (Vdbe *)pStmt;
- sqlite3_mutex_enter(p->db->mutex);
rc = vdbeUnbind(p, i);
if( rc==SQLITE_OK ){
sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
+ sqlite3_mutex_leave(p->db->mutex);
}
- sqlite3_mutex_leave(p->db->mutex);
return rc;
}
int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
@@ -1087,20 +975,20 @@ int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
int rc;
Vdbe *p = (Vdbe *)pStmt;
- sqlite3_mutex_enter(p->db->mutex);
rc = vdbeUnbind(p, i);
if( rc==SQLITE_OK ){
sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
+ sqlite3_mutex_leave(p->db->mutex);
}
- sqlite3_mutex_leave(p->db->mutex);
return rc;
}
int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
int rc;
Vdbe *p = (Vdbe*)pStmt;
- sqlite3_mutex_enter(p->db->mutex);
rc = vdbeUnbind(p, i);
- sqlite3_mutex_leave(p->db->mutex);
+ if( rc==SQLITE_OK ){
+ sqlite3_mutex_leave(p->db->mutex);
+ }
return rc;
}
int sqlite3_bind_text(
@@ -1125,28 +1013,43 @@ int sqlite3_bind_text16(
#endif /* SQLITE_OMIT_UTF16 */
int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
int rc;
- Vdbe *p = (Vdbe *)pStmt;
- sqlite3_mutex_enter(p->db->mutex);
- rc = vdbeUnbind(p, i);
- if( rc==SQLITE_OK ){
- rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
- if( rc==SQLITE_OK ){
- rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db));
+ 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;
}
}
- rc = sqlite3ApiExit(p->db, rc);
- sqlite3_mutex_leave(p->db->mutex);
return rc;
}
int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
int rc;
Vdbe *p = (Vdbe *)pStmt;
- sqlite3_mutex_enter(p->db->mutex);
rc = vdbeUnbind(p, i);
if( rc==SQLITE_OK ){
sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
+ sqlite3_mutex_leave(p->db->mutex);
}
- sqlite3_mutex_leave(p->db->mutex);
return rc;
}
@@ -1166,18 +1069,21 @@ int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
*/
static void createVarMap(Vdbe *p){
if( !p->okVar ){
+ int j;
+ Op *pOp;
sqlite3_mutex_enter(p->db->mutex);
- if( !p->okVar ){
- int j;
- Op *pOp;
- for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
- if( pOp->opcode==OP_Variable ){
- assert( pOp->p1>0 && pOp->p1<=p->nVar );
- p->azVar[pOp->p1-1] = pOp->p4.z;
- }
+ /* The race condition here is harmless. If two threads call this
+ ** routine on the same Vdbe at the same time, they both might end
+ ** up initializing the Vdbe.azVar[] array. That is a little extra
+ ** work but it results in the same answer.
+ */
+ for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
+ if( pOp->opcode==OP_Variable ){
+ assert( pOp->p1>0 && pOp->p1<=p->nVar );
+ p->azVar[pOp->p1-1] = pOp->p4.z;
}
- p->okVar = 1;
}
+ p->okVar = 1;
sqlite3_mutex_leave(p->db->mutex);
}
}
@@ -1222,29 +1128,43 @@ int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *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.
+** 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;
- int i, rc = SQLITE_OK;
- if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
- || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
- || pTo->db!=pFrom->db ){
- return SQLITE_MISUSE;
- }
if( pFrom->nVar!=pTo->nVar ){
return SQLITE_ERROR;
}
- sqlite3_mutex_enter(pTo->db->mutex);
- for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
- sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
- }
- sqlite3_mutex_leave(pTo->db->mutex);
- assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
- return rc;
+ return sqlite3TransferBindings(pFromStmt, pToStmt);
}
+#endif
/*
** Return the sqlite3* database handle to which the prepared statement given
@@ -1273,3 +1193,13 @@ sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
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;
+ int v = pVdbe->aCounter[op-1];
+ if( resetFlag ) pVdbe->aCounter[op-1] = 0;
+ return v;
+}