00001 /* 00002 ** 2004 May 26 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** 00013 ** This file contains code use to implement APIs that are part of the 00014 ** VDBE. 00015 ** 00016 ** $Id: vdbeapi.c,v 1.148 2008/11/05 16:37:35 drh Exp $ 00017 */ 00018 #include "sqliteInt.h" 00019 #include "vdbeInt.h" 00020 00021 #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) 00022 /* 00023 ** The following structure contains pointers to the end points of a 00024 ** doubly-linked list of all compiled SQL statements that may be holding 00025 ** buffers eligible for release when the sqlite3_release_memory() interface is 00026 ** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2 00027 ** mutex. 00028 ** 00029 ** Statements are added to the end of this list when sqlite3_reset() is 00030 ** called. They are removed either when sqlite3_step() or sqlite3_finalize() 00031 ** is called. When statements are added to this list, the associated 00032 ** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that 00033 ** can be freed using sqlite3VdbeReleaseMemory(). 00034 ** 00035 ** When statements are added or removed from this list, the mutex 00036 ** associated with the Vdbe being added or removed (Vdbe.db->mutex) is 00037 ** already held. The LRU2 mutex is then obtained, blocking if necessary, 00038 ** the linked-list pointers manipulated and the LRU2 mutex relinquished. 00039 */ 00040 struct StatementLruList { 00041 Vdbe *pFirst; 00042 Vdbe *pLast; 00043 }; 00044 static struct StatementLruList sqlite3LruStatements; 00045 00046 /* 00047 ** Check that the list looks to be internally consistent. This is used 00048 ** as part of an assert() statement as follows: 00049 ** 00050 ** assert( stmtLruCheck() ); 00051 */ 00052 #ifndef NDEBUG 00053 static int stmtLruCheck(){ 00054 Vdbe *p; 00055 for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){ 00056 assert(p->pLruNext || p==sqlite3LruStatements.pLast); 00057 assert(!p->pLruNext || p->pLruNext->pLruPrev==p); 00058 assert(p->pLruPrev || p==sqlite3LruStatements.pFirst); 00059 assert(!p->pLruPrev || p->pLruPrev->pLruNext==p); 00060 } 00061 return 1; 00062 } 00063 #endif 00064 00065 /* 00066 ** Add vdbe p to the end of the statement lru list. It is assumed that 00067 ** p is not already part of the list when this is called. The lru list 00068 ** is protected by the SQLITE_MUTEX_STATIC_LRU mutex. 00069 */ 00070 static void stmtLruAdd(Vdbe *p){ 00071 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00072 00073 if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){ 00074 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00075 return; 00076 } 00077 00078 assert( stmtLruCheck() ); 00079 00080 if( !sqlite3LruStatements.pFirst ){ 00081 assert( !sqlite3LruStatements.pLast ); 00082 sqlite3LruStatements.pFirst = p; 00083 sqlite3LruStatements.pLast = p; 00084 }else{ 00085 assert( !sqlite3LruStatements.pLast->pLruNext ); 00086 p->pLruPrev = sqlite3LruStatements.pLast; 00087 sqlite3LruStatements.pLast->pLruNext = p; 00088 sqlite3LruStatements.pLast = p; 00089 } 00090 00091 assert( stmtLruCheck() ); 00092 00093 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00094 } 00095 00096 /* 00097 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove 00098 ** statement p from the least-recently-used statement list. If the 00099 ** statement is not currently part of the list, this call is a no-op. 00100 */ 00101 static void stmtLruRemoveNomutex(Vdbe *p){ 00102 if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){ 00103 assert( stmtLruCheck() ); 00104 if( p->pLruNext ){ 00105 p->pLruNext->pLruPrev = p->pLruPrev; 00106 }else{ 00107 sqlite3LruStatements.pLast = p->pLruPrev; 00108 } 00109 if( p->pLruPrev ){ 00110 p->pLruPrev->pLruNext = p->pLruNext; 00111 }else{ 00112 sqlite3LruStatements.pFirst = p->pLruNext; 00113 } 00114 p->pLruNext = 0; 00115 p->pLruPrev = 0; 00116 assert( stmtLruCheck() ); 00117 } 00118 } 00119 00120 /* 00121 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove 00122 ** statement p from the least-recently-used statement list. If the 00123 ** statement is not currently part of the list, this call is a no-op. 00124 */ 00125 static void stmtLruRemove(Vdbe *p){ 00126 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00127 stmtLruRemoveNomutex(p); 00128 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00129 } 00130 00131 /* 00132 ** Try to release n bytes of memory by freeing buffers associated 00133 ** with the memory registers of currently unused vdbes. 00134 */ 00135 int sqlite3VdbeReleaseMemory(int n){ 00136 Vdbe *p; 00137 Vdbe *pNext; 00138 int nFree = 0; 00139 00140 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00141 for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){ 00142 pNext = p->pLruNext; 00143 00144 /* For each statement handle in the lru list, attempt to obtain the 00145 ** associated database mutex. If it cannot be obtained, continue 00146 ** to the next statement handle. It is not possible to block on 00147 ** the database mutex - that could cause deadlock. 00148 */ 00149 if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){ 00150 nFree += sqlite3VdbeReleaseBuffers(p); 00151 stmtLruRemoveNomutex(p); 00152 sqlite3_mutex_leave(p->db->mutex); 00153 } 00154 } 00155 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); 00156 00157 return nFree; 00158 } 00159 00160 /* 00161 ** Call sqlite3Reprepare() on the statement. Remove it from the 00162 ** lru list before doing so, as Reprepare() will free all the 00163 ** memory register buffers anyway. 00164 */ 00165 int vdbeReprepare(Vdbe *p){ 00166 stmtLruRemove(p); 00167 return sqlite3Reprepare(p); 00168 } 00169 00170 #else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */ 00171 #define stmtLruRemove(x) 00172 #define stmtLruAdd(x) 00173 #define vdbeReprepare(x) sqlite3Reprepare(x) 00174 #endif 00175 00176 00177 #ifndef SQLITE_OMIT_DEPRECATED 00178 /* 00179 ** Return TRUE (non-zero) of the statement supplied as an argument needs 00180 ** to be recompiled. A statement needs to be recompiled whenever the 00181 ** execution environment changes in a way that would alter the program 00182 ** that sqlite3_prepare() generates. For example, if new functions or 00183 ** collating sequences are registered or if an authorizer function is 00184 ** added or changed. 00185 */ 00186 int sqlite3_expired(sqlite3_stmt *pStmt){ 00187 Vdbe *p = (Vdbe*)pStmt; 00188 return p==0 || p->expired; 00189 } 00190 #endif 00191 00192 /* 00193 ** The following routine destroys a virtual machine that is created by 00194 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 00195 ** success/failure code that describes the result of executing the virtual 00196 ** machine. 00197 ** 00198 ** This routine sets the error code and string returned by 00199 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 00200 */ 00201 int sqlite3_finalize(sqlite3_stmt *pStmt){ 00202 int rc; 00203 if( pStmt==0 ){ 00204 rc = SQLITE_OK; 00205 }else{ 00206 Vdbe *v = (Vdbe*)pStmt; 00207 #if SQLITE_THREADSAFE 00208 sqlite3_mutex *mutex = v->db->mutex; 00209 #endif 00210 sqlite3_mutex_enter(mutex); 00211 stmtLruRemove(v); 00212 rc = sqlite3VdbeFinalize(v); 00213 sqlite3_mutex_leave(mutex); 00214 } 00215 return rc; 00216 } 00217 00218 /* 00219 ** Terminate the current execution of an SQL statement and reset it 00220 ** back to its starting state so that it can be reused. A success code from 00221 ** the prior execution is returned. 00222 ** 00223 ** This routine sets the error code and string returned by 00224 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 00225 */ 00226 int sqlite3_reset(sqlite3_stmt *pStmt){ 00227 int rc; 00228 if( pStmt==0 ){ 00229 rc = SQLITE_OK; 00230 }else{ 00231 Vdbe *v = (Vdbe*)pStmt; 00232 sqlite3_mutex_enter(v->db->mutex); 00233 rc = sqlite3VdbeReset(v); 00234 stmtLruAdd(v); 00235 sqlite3VdbeMakeReady(v, -1, 0, 0, 0); 00236 assert( (rc & (v->db->errMask))==rc ); 00237 sqlite3_mutex_leave(v->db->mutex); 00238 } 00239 return rc; 00240 } 00241 00242 /* 00243 ** Set all the parameters in the compiled SQL statement to NULL. 00244 */ 00245 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 00246 int i; 00247 int rc = SQLITE_OK; 00248 Vdbe *p = (Vdbe*)pStmt; 00249 #if SQLITE_THREADSAFE 00250 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 00251 #endif 00252 sqlite3_mutex_enter(mutex); 00253 for(i=0; i<p->nVar; i++){ 00254 sqlite3VdbeMemRelease(&p->aVar[i]); 00255 p->aVar[i].flags = MEM_Null; 00256 } 00257 sqlite3_mutex_leave(mutex); 00258 return rc; 00259 } 00260 00261 00262 /**************************** sqlite3_value_ ******************************* 00263 ** The following routines extract information from a Mem or sqlite3_value 00264 ** structure. 00265 */ 00266 const void *sqlite3_value_blob(sqlite3_value *pVal){ 00267 Mem *p = (Mem*)pVal; 00268 if( p->flags & (MEM_Blob|MEM_Str) ){ 00269 sqlite3VdbeMemExpandBlob(p); 00270 p->flags &= ~MEM_Str; 00271 p->flags |= MEM_Blob; 00272 return p->z; 00273 }else{ 00274 return sqlite3_value_text(pVal); 00275 } 00276 } 00277 int sqlite3_value_bytes(sqlite3_value *pVal){ 00278 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 00279 } 00280 int sqlite3_value_bytes16(sqlite3_value *pVal){ 00281 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 00282 } 00283 double sqlite3_value_double(sqlite3_value *pVal){ 00284 return sqlite3VdbeRealValue((Mem*)pVal); 00285 } 00286 int sqlite3_value_int(sqlite3_value *pVal){ 00287 return sqlite3VdbeIntValue((Mem*)pVal); 00288 } 00289 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 00290 return sqlite3VdbeIntValue((Mem*)pVal); 00291 } 00292 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 00293 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 00294 } 00295 #ifndef SQLITE_OMIT_UTF16 00296 const void *sqlite3_value_text16(sqlite3_value* pVal){ 00297 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 00298 } 00299 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 00300 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 00301 } 00302 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 00303 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 00304 } 00305 #endif /* SQLITE_OMIT_UTF16 */ 00306 int sqlite3_value_type(sqlite3_value* pVal){ 00307 return pVal->type; 00308 } 00309 00310 /**************************** sqlite3_result_ ******************************* 00311 ** The following routines are used by user-defined functions to specify 00312 ** the function result. 00313 */ 00314 void sqlite3_result_blob( 00315 sqlite3_context *pCtx, 00316 const void *z, 00317 int n, 00318 void (*xDel)(void *) 00319 ){ 00320 assert( n>=0 ); 00321 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00322 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); 00323 } 00324 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 00325 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00326 sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 00327 } 00328 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 00329 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00330 pCtx->isError = SQLITE_ERROR; 00331 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 00332 } 00333 #ifndef SQLITE_OMIT_UTF16 00334 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 00335 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00336 pCtx->isError = SQLITE_ERROR; 00337 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 00338 } 00339 #endif 00340 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 00341 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00342 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 00343 } 00344 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 00345 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00346 sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 00347 } 00348 void sqlite3_result_null(sqlite3_context *pCtx){ 00349 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00350 sqlite3VdbeMemSetNull(&pCtx->s); 00351 } 00352 void sqlite3_result_text( 00353 sqlite3_context *pCtx, 00354 const char *z, 00355 int n, 00356 void (*xDel)(void *) 00357 ){ 00358 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00359 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); 00360 } 00361 #ifndef SQLITE_OMIT_UTF16 00362 void sqlite3_result_text16( 00363 sqlite3_context *pCtx, 00364 const void *z, 00365 int n, 00366 void (*xDel)(void *) 00367 ){ 00368 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00369 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); 00370 } 00371 void sqlite3_result_text16be( 00372 sqlite3_context *pCtx, 00373 const void *z, 00374 int n, 00375 void (*xDel)(void *) 00376 ){ 00377 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00378 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); 00379 } 00380 void sqlite3_result_text16le( 00381 sqlite3_context *pCtx, 00382 const void *z, 00383 int n, 00384 void (*xDel)(void *) 00385 ){ 00386 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00387 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); 00388 } 00389 #endif /* SQLITE_OMIT_UTF16 */ 00390 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 00391 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00392 sqlite3VdbeMemCopy(&pCtx->s, pValue); 00393 } 00394 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 00395 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00396 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); 00397 } 00398 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 00399 pCtx->isError = errCode; 00400 } 00401 00402 /* Force an SQLITE_TOOBIG error. */ 00403 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 00404 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00405 pCtx->isError = SQLITE_TOOBIG; 00406 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 00407 SQLITE_UTF8, SQLITE_STATIC); 00408 } 00409 00410 /* An SQLITE_NOMEM error. */ 00411 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 00412 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00413 sqlite3VdbeMemSetNull(&pCtx->s); 00414 pCtx->isError = SQLITE_NOMEM; 00415 pCtx->s.db->mallocFailed = 1; 00416 } 00417 00418 /* 00419 ** Execute the statement pStmt, either until a row of data is ready, the 00420 ** statement is completely executed or an error occurs. 00421 ** 00422 ** This routine implements the bulk of the logic behind the sqlite_step() 00423 ** API. The only thing omitted is the automatic recompile if a 00424 ** schema change has occurred. That detail is handled by the 00425 ** outer sqlite3_step() wrapper procedure. 00426 */ 00427 static int sqlite3Step(Vdbe *p){ 00428 sqlite3 *db; 00429 int rc; 00430 00431 assert(p); 00432 if( p->magic!=VDBE_MAGIC_RUN ){ 00433 return SQLITE_MISUSE; 00434 } 00435 00436 /* Assert that malloc() has not failed */ 00437 db = p->db; 00438 if( db->mallocFailed ){ 00439 return SQLITE_NOMEM; 00440 } 00441 00442 if( p->pc<=0 && p->expired ){ 00443 if( p->rc==SQLITE_OK ){ 00444 p->rc = SQLITE_SCHEMA; 00445 } 00446 rc = SQLITE_ERROR; 00447 goto end_of_step; 00448 } 00449 if( sqlite3SafetyOn(db) ){ 00450 p->rc = SQLITE_MISUSE; 00451 return SQLITE_MISUSE; 00452 } 00453 if( p->pc<0 ){ 00454 /* If there are no other statements currently running, then 00455 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 00456 ** from interrupting a statement that has not yet started. 00457 */ 00458 if( db->activeVdbeCnt==0 ){ 00459 db->u1.isInterrupted = 0; 00460 } 00461 00462 #ifndef SQLITE_OMIT_TRACE 00463 if( db->xProfile && !db->init.busy ){ 00464 double rNow; 00465 sqlite3OsCurrentTime(db->pVfs, &rNow); 00466 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; 00467 } 00468 #endif 00469 00470 db->activeVdbeCnt++; 00471 if( p->readOnly==0 ) db->writeVdbeCnt++; 00472 p->pc = 0; 00473 stmtLruRemove(p); 00474 } 00475 #ifndef SQLITE_OMIT_EXPLAIN 00476 if( p->explain ){ 00477 rc = sqlite3VdbeList(p); 00478 }else 00479 #endif /* SQLITE_OMIT_EXPLAIN */ 00480 { 00481 rc = sqlite3VdbeExec(p); 00482 } 00483 00484 if( sqlite3SafetyOff(db) ){ 00485 rc = SQLITE_MISUSE; 00486 } 00487 00488 #ifndef SQLITE_OMIT_TRACE 00489 /* Invoke the profile callback if there is one 00490 */ 00491 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0 00492 && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){ 00493 double rNow; 00494 u64 elapseTime; 00495 00496 sqlite3OsCurrentTime(db->pVfs, &rNow); 00497 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; 00498 db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime); 00499 } 00500 #endif 00501 00502 db->errCode = rc; 00503 /*sqlite3Error(p->db, rc, 0);*/ 00504 p->rc = sqlite3ApiExit(p->db, p->rc); 00505 end_of_step: 00506 assert( (rc&0xff)==rc ); 00507 if( p->zSql && (rc&0xff)<SQLITE_ROW ){ 00508 /* This behavior occurs if sqlite3_prepare_v2() was used to build 00509 ** the prepared statement. Return error codes directly */ 00510 p->db->errCode = p->rc; 00511 /* sqlite3Error(p->db, p->rc, 0); */ 00512 return p->rc; 00513 }else{ 00514 /* This is for legacy sqlite3_prepare() builds and when the code 00515 ** is SQLITE_ROW or SQLITE_DONE */ 00516 return rc; 00517 } 00518 } 00519 00520 /* 00521 ** This is the top-level implementation of sqlite3_step(). Call 00522 ** sqlite3Step() to do most of the work. If a schema error occurs, 00523 ** call sqlite3Reprepare() and try again. 00524 */ 00525 #ifdef SQLITE_OMIT_PARSER 00526 int sqlite3_step(sqlite3_stmt *pStmt){ 00527 int rc = SQLITE_MISUSE; 00528 if( pStmt ){ 00529 Vdbe *v; 00530 v = (Vdbe*)pStmt; 00531 sqlite3_mutex_enter(v->db->mutex); 00532 rc = sqlite3Step(v); 00533 sqlite3_mutex_leave(v->db->mutex); 00534 } 00535 return rc; 00536 } 00537 #else 00538 int sqlite3_step(sqlite3_stmt *pStmt){ 00539 int rc = SQLITE_MISUSE; 00540 if( pStmt ){ 00541 int cnt = 0; 00542 Vdbe *v = (Vdbe*)pStmt; 00543 sqlite3 *db = v->db; 00544 sqlite3_mutex_enter(db->mutex); 00545 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 00546 && cnt++ < 5 00547 && vdbeReprepare(v) ){ 00548 sqlite3_reset(pStmt); 00549 v->expired = 0; 00550 } 00551 if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){ 00552 /* This case occurs after failing to recompile an sql statement. 00553 ** The error message from the SQL compiler has already been loaded 00554 ** into the database handle. This block copies the error message 00555 ** from the database handle into the statement and sets the statement 00556 ** program counter to 0 to ensure that when the statement is 00557 ** finalized or reset the parser error message is available via 00558 ** sqlite3_errmsg() and sqlite3_errcode(). 00559 */ 00560 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 00561 sqlite3DbFree(db, v->zErrMsg); 00562 if( !db->mallocFailed ){ 00563 v->zErrMsg = sqlite3DbStrDup(db, zErr); 00564 } else { 00565 v->zErrMsg = 0; 00566 v->rc = SQLITE_NOMEM; 00567 } 00568 } 00569 rc = sqlite3ApiExit(db, rc); 00570 sqlite3_mutex_leave(db->mutex); 00571 } 00572 return rc; 00573 } 00574 #endif 00575 00576 /* 00577 ** Extract the user data from a sqlite3_context structure and return a 00578 ** pointer to it. 00579 */ 00580 void *sqlite3_user_data(sqlite3_context *p){ 00581 assert( p && p->pFunc ); 00582 return p->pFunc->pUserData; 00583 } 00584 00585 /* 00586 ** Extract the user data from a sqlite3_context structure and return a 00587 ** pointer to it. 00588 */ 00589 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 00590 assert( p && p->pFunc ); 00591 return p->s.db; 00592 } 00593 00594 /* 00595 ** The following is the implementation of an SQL function that always 00596 ** fails with an error message stating that the function is used in the 00597 ** wrong context. The sqlite3_overload_function() API might construct 00598 ** SQL function that use this routine so that the functions will exist 00599 ** for name resolution but are actually overloaded by the xFindFunction 00600 ** method of virtual tables. 00601 */ 00602 void sqlite3InvalidFunction( 00603 sqlite3_context *context, /* The function calling context */ 00604 int argc, /* Number of arguments to the function */ 00605 sqlite3_value **argv /* Value of each argument */ 00606 ){ 00607 const char *zName = context->pFunc->zName; 00608 char *zErr; 00609 zErr = sqlite3MPrintf(0, 00610 "unable to use function %s in the requested context", zName); 00611 sqlite3_result_error(context, zErr, -1); 00612 sqlite3_free(zErr); 00613 } 00614 00615 /* 00616 ** Allocate or return the aggregate context for a user function. A new 00617 ** context is allocated on the first call. Subsequent calls return the 00618 ** same context that was returned on prior calls. 00619 */ 00620 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 00621 Mem *pMem; 00622 assert( p && p->pFunc && p->pFunc->xStep ); 00623 assert( sqlite3_mutex_held(p->s.db->mutex) ); 00624 pMem = p->pMem; 00625 if( (pMem->flags & MEM_Agg)==0 ){ 00626 if( nByte==0 ){ 00627 sqlite3VdbeMemReleaseExternal(pMem); 00628 pMem->flags = MEM_Null; 00629 pMem->z = 0; 00630 }else{ 00631 sqlite3VdbeMemGrow(pMem, nByte, 0); 00632 pMem->flags = MEM_Agg; 00633 pMem->u.pDef = p->pFunc; 00634 if( pMem->z ){ 00635 memset(pMem->z, 0, nByte); 00636 } 00637 } 00638 } 00639 return (void*)pMem->z; 00640 } 00641 00642 /* 00643 ** Return the auxilary data pointer, if any, for the iArg'th argument to 00644 ** the user-function defined by pCtx. 00645 */ 00646 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 00647 VdbeFunc *pVdbeFunc; 00648 00649 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00650 pVdbeFunc = pCtx->pVdbeFunc; 00651 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 00652 return 0; 00653 } 00654 return pVdbeFunc->apAux[iArg].pAux; 00655 } 00656 00657 /* 00658 ** Set the auxilary data pointer and delete function, for the iArg'th 00659 ** argument to the user-function defined by pCtx. Any previous value is 00660 ** deleted by calling the delete function specified when it was set. 00661 */ 00662 void sqlite3_set_auxdata( 00663 sqlite3_context *pCtx, 00664 int iArg, 00665 void *pAux, 00666 void (*xDelete)(void*) 00667 ){ 00668 struct AuxData *pAuxData; 00669 VdbeFunc *pVdbeFunc; 00670 if( iArg<0 ) goto failed; 00671 00672 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 00673 pVdbeFunc = pCtx->pVdbeFunc; 00674 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 00675 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); 00676 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 00677 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); 00678 if( !pVdbeFunc ){ 00679 goto failed; 00680 } 00681 pCtx->pVdbeFunc = pVdbeFunc; 00682 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); 00683 pVdbeFunc->nAux = iArg+1; 00684 pVdbeFunc->pFunc = pCtx->pFunc; 00685 } 00686 00687 pAuxData = &pVdbeFunc->apAux[iArg]; 00688 if( pAuxData->pAux && pAuxData->xDelete ){ 00689 pAuxData->xDelete(pAuxData->pAux); 00690 } 00691 pAuxData->pAux = pAux; 00692 pAuxData->xDelete = xDelete; 00693 return; 00694 00695 failed: 00696 if( xDelete ){ 00697 xDelete(pAux); 00698 } 00699 } 00700 00701 #ifndef SQLITE_OMIT_DEPRECATED 00702 /* 00703 ** Return the number of times the Step function of a aggregate has been 00704 ** called. 00705 ** 00706 ** This function is deprecated. Do not use it for new code. It is 00707 ** provide only to avoid breaking legacy code. New aggregate function 00708 ** implementations should keep their own counts within their aggregate 00709 ** context. 00710 */ 00711 int sqlite3_aggregate_count(sqlite3_context *p){ 00712 assert( p && p->pFunc && p->pFunc->xStep ); 00713 return p->pMem->n; 00714 } 00715 #endif 00716 00717 /* 00718 ** Return the number of columns in the result set for the statement pStmt. 00719 */ 00720 int sqlite3_column_count(sqlite3_stmt *pStmt){ 00721 Vdbe *pVm = (Vdbe *)pStmt; 00722 return pVm ? pVm->nResColumn : 0; 00723 } 00724 00725 /* 00726 ** Return the number of values available from the current row of the 00727 ** currently executing statement pStmt. 00728 */ 00729 int sqlite3_data_count(sqlite3_stmt *pStmt){ 00730 Vdbe *pVm = (Vdbe *)pStmt; 00731 if( pVm==0 || pVm->pResultSet==0 ) return 0; 00732 return pVm->nResColumn; 00733 } 00734 00735 00736 /* 00737 ** Check to see if column iCol of the given statement is valid. If 00738 ** it is, return a pointer to the Mem for the value of that column. 00739 ** If iCol is not valid, return a pointer to a Mem which has a value 00740 ** of NULL. 00741 */ 00742 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 00743 Vdbe *pVm; 00744 int vals; 00745 Mem *pOut; 00746 00747 pVm = (Vdbe *)pStmt; 00748 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 00749 sqlite3_mutex_enter(pVm->db->mutex); 00750 vals = sqlite3_data_count(pStmt); 00751 pOut = &pVm->pResultSet[i]; 00752 }else{ 00753 static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 }; 00754 if( pVm->db ){ 00755 sqlite3_mutex_enter(pVm->db->mutex); 00756 sqlite3Error(pVm->db, SQLITE_RANGE, 0); 00757 } 00758 pOut = (Mem*)&nullMem; 00759 } 00760 return pOut; 00761 } 00762 00763 /* 00764 ** This function is called after invoking an sqlite3_value_XXX function on a 00765 ** column value (i.e. a value returned by evaluating an SQL expression in the 00766 ** select list of a SELECT statement) that may cause a malloc() failure. If 00767 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 00768 ** code of statement pStmt set to SQLITE_NOMEM. 00769 ** 00770 ** Specifically, this is called from within: 00771 ** 00772 ** sqlite3_column_int() 00773 ** sqlite3_column_int64() 00774 ** sqlite3_column_text() 00775 ** sqlite3_column_text16() 00776 ** sqlite3_column_real() 00777 ** sqlite3_column_bytes() 00778 ** sqlite3_column_bytes16() 00779 ** 00780 ** But not for sqlite3_column_blob(), which never calls malloc(). 00781 */ 00782 static void columnMallocFailure(sqlite3_stmt *pStmt) 00783 { 00784 /* If malloc() failed during an encoding conversion within an 00785 ** sqlite3_column_XXX API, then set the return code of the statement to 00786 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 00787 ** and _finalize() will return NOMEM. 00788 */ 00789 Vdbe *p = (Vdbe *)pStmt; 00790 if( p ){ 00791 p->rc = sqlite3ApiExit(p->db, p->rc); 00792 sqlite3_mutex_leave(p->db->mutex); 00793 } 00794 } 00795 00796 /**************************** sqlite3_column_ ******************************* 00797 ** The following routines are used to access elements of the current row 00798 ** in the result set. 00799 */ 00800 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 00801 const void *val; 00802 val = sqlite3_value_blob( columnMem(pStmt,i) ); 00803 /* Even though there is no encoding conversion, value_blob() might 00804 ** need to call malloc() to expand the result of a zeroblob() 00805 ** expression. 00806 */ 00807 columnMallocFailure(pStmt); 00808 return val; 00809 } 00810 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 00811 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 00812 columnMallocFailure(pStmt); 00813 return val; 00814 } 00815 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 00816 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 00817 columnMallocFailure(pStmt); 00818 return val; 00819 } 00820 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 00821 double val = sqlite3_value_double( columnMem(pStmt,i) ); 00822 columnMallocFailure(pStmt); 00823 return val; 00824 } 00825 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 00826 int val = sqlite3_value_int( columnMem(pStmt,i) ); 00827 columnMallocFailure(pStmt); 00828 return val; 00829 } 00830 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 00831 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 00832 columnMallocFailure(pStmt); 00833 return val; 00834 } 00835 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 00836 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 00837 columnMallocFailure(pStmt); 00838 return val; 00839 } 00840 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 00841 Mem *pOut = columnMem(pStmt, i); 00842 if( pOut->flags&MEM_Static ){ 00843 pOut->flags &= ~MEM_Static; 00844 pOut->flags |= MEM_Ephem; 00845 } 00846 columnMallocFailure(pStmt); 00847 return (sqlite3_value *)pOut; 00848 } 00849 #ifndef SQLITE_OMIT_UTF16 00850 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 00851 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 00852 columnMallocFailure(pStmt); 00853 return val; 00854 } 00855 #endif /* SQLITE_OMIT_UTF16 */ 00856 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 00857 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 00858 columnMallocFailure(pStmt); 00859 return iType; 00860 } 00861 00862 /* The following function is experimental and subject to change or 00863 ** removal */ 00864 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 00865 ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 00866 **} 00867 */ 00868 00869 /* 00870 ** Convert the N-th element of pStmt->pColName[] into a string using 00871 ** xFunc() then return that string. If N is out of range, return 0. 00872 ** 00873 ** There are up to 5 names for each column. useType determines which 00874 ** name is returned. Here are the names: 00875 ** 00876 ** 0 The column name as it should be displayed for output 00877 ** 1 The datatype name for the column 00878 ** 2 The name of the database that the column derives from 00879 ** 3 The name of the table that the column derives from 00880 ** 4 The name of the table column that the result column derives from 00881 ** 00882 ** If the result is not a simple column reference (if it is an expression 00883 ** or a constant) then useTypes 2, 3, and 4 return NULL. 00884 */ 00885 static const void *columnName( 00886 sqlite3_stmt *pStmt, 00887 int N, 00888 const void *(*xFunc)(Mem*), 00889 int useType 00890 ){ 00891 const void *ret = 0; 00892 Vdbe *p = (Vdbe *)pStmt; 00893 int n; 00894 00895 00896 if( p!=0 ){ 00897 n = sqlite3_column_count(pStmt); 00898 if( N<n && N>=0 ){ 00899 N += useType*n; 00900 sqlite3_mutex_enter(p->db->mutex); 00901 ret = xFunc(&p->aColName[N]); 00902 00903 /* A malloc may have failed inside of the xFunc() call. If this 00904 ** is the case, clear the mallocFailed flag and return NULL. 00905 */ 00906 if( p->db && p->db->mallocFailed ){ 00907 p->db->mallocFailed = 0; 00908 ret = 0; 00909 } 00910 sqlite3_mutex_leave(p->db->mutex); 00911 } 00912 } 00913 return ret; 00914 } 00915 00916 /* 00917 ** Return the name of the Nth column of the result set returned by SQL 00918 ** statement pStmt. 00919 */ 00920 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 00921 return columnName( 00922 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 00923 } 00924 #ifndef SQLITE_OMIT_UTF16 00925 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 00926 return columnName( 00927 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 00928 } 00929 #endif 00930 00931 /* 00932 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 00933 ** not define OMIT_DECLTYPE. 00934 */ 00935 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 00936 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 00937 and SQLITE_ENABLE_COLUMN_METADATA" 00938 #endif 00939 00940 #ifndef SQLITE_OMIT_DECLTYPE 00941 /* 00942 ** Return the column declaration type (if applicable) of the 'i'th column 00943 ** of the result set of SQL statement pStmt. 00944 */ 00945 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 00946 return columnName( 00947 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 00948 } 00949 #ifndef SQLITE_OMIT_UTF16 00950 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 00951 return columnName( 00952 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 00953 } 00954 #endif /* SQLITE_OMIT_UTF16 */ 00955 #endif /* SQLITE_OMIT_DECLTYPE */ 00956 00957 #ifdef SQLITE_ENABLE_COLUMN_METADATA 00958 /* 00959 ** Return the name of the database from which a result column derives. 00960 ** NULL is returned if the result column is an expression or constant or 00961 ** anything else which is not an unabiguous reference to a database column. 00962 */ 00963 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 00964 return columnName( 00965 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 00966 } 00967 #ifndef SQLITE_OMIT_UTF16 00968 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 00969 return columnName( 00970 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 00971 } 00972 #endif /* SQLITE_OMIT_UTF16 */ 00973 00974 /* 00975 ** Return the name of the table from which a result column derives. 00976 ** NULL is returned if the result column is an expression or constant or 00977 ** anything else which is not an unabiguous reference to a database column. 00978 */ 00979 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 00980 return columnName( 00981 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 00982 } 00983 #ifndef SQLITE_OMIT_UTF16 00984 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 00985 return columnName( 00986 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 00987 } 00988 #endif /* SQLITE_OMIT_UTF16 */ 00989 00990 /* 00991 ** Return the name of the table column from which a result column derives. 00992 ** NULL is returned if the result column is an expression or constant or 00993 ** anything else which is not an unabiguous reference to a database column. 00994 */ 00995 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 00996 return columnName( 00997 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 00998 } 00999 #ifndef SQLITE_OMIT_UTF16 01000 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 01001 return columnName( 01002 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 01003 } 01004 #endif /* SQLITE_OMIT_UTF16 */ 01005 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 01006 01007 01008 /******************************* sqlite3_bind_ *************************** 01009 ** 01010 ** Routines used to attach values to wildcards in a compiled SQL statement. 01011 */ 01012 /* 01013 ** Unbind the value bound to variable i in virtual machine p. This is the 01014 ** the same as binding a NULL value to the column. If the "i" parameter is 01015 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 01016 ** 01017 ** A successful evaluation of this routine acquires the mutex on p. 01018 ** the mutex is released if any kind of error occurs. 01019 ** 01020 ** The error code stored in database p->db is overwritten with the return 01021 ** value in any case. 01022 */ 01023 static int vdbeUnbind(Vdbe *p, int i){ 01024 Mem *pVar; 01025 if( p==0 ) return SQLITE_MISUSE; 01026 sqlite3_mutex_enter(p->db->mutex); 01027 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 01028 sqlite3Error(p->db, SQLITE_MISUSE, 0); 01029 sqlite3_mutex_leave(p->db->mutex); 01030 return SQLITE_MISUSE; 01031 } 01032 if( i<1 || i>p->nVar ){ 01033 sqlite3Error(p->db, SQLITE_RANGE, 0); 01034 sqlite3_mutex_leave(p->db->mutex); 01035 return SQLITE_RANGE; 01036 } 01037 i--; 01038 pVar = &p->aVar[i]; 01039 sqlite3VdbeMemRelease(pVar); 01040 pVar->flags = MEM_Null; 01041 sqlite3Error(p->db, SQLITE_OK, 0); 01042 return SQLITE_OK; 01043 } 01044 01045 /* 01046 ** Bind a text or BLOB value. 01047 */ 01048 static int bindText( 01049 sqlite3_stmt *pStmt, /* The statement to bind against */ 01050 int i, /* Index of the parameter to bind */ 01051 const void *zData, /* Pointer to the data to be bound */ 01052 int nData, /* Number of bytes of data to be bound */ 01053 void (*xDel)(void*), /* Destructor for the data */ 01054 int encoding /* Encoding for the data */ 01055 ){ 01056 Vdbe *p = (Vdbe *)pStmt; 01057 Mem *pVar; 01058 int rc; 01059 01060 rc = vdbeUnbind(p, i); 01061 if( rc==SQLITE_OK ){ 01062 if( zData!=0 ){ 01063 pVar = &p->aVar[i-1]; 01064 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 01065 if( rc==SQLITE_OK && encoding!=0 ){ 01066 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 01067 } 01068 sqlite3Error(p->db, rc, 0); 01069 rc = sqlite3ApiExit(p->db, rc); 01070 } 01071 sqlite3_mutex_leave(p->db->mutex); 01072 } 01073 return rc; 01074 } 01075 01076 01077 /* 01078 ** Bind a blob value to an SQL statement variable. 01079 */ 01080 int sqlite3_bind_blob( 01081 sqlite3_stmt *pStmt, 01082 int i, 01083 const void *zData, 01084 int nData, 01085 void (*xDel)(void*) 01086 ){ 01087 return bindText(pStmt, i, zData, nData, xDel, 0); 01088 } 01089 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 01090 int rc; 01091 Vdbe *p = (Vdbe *)pStmt; 01092 rc = vdbeUnbind(p, i); 01093 if( rc==SQLITE_OK ){ 01094 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 01095 sqlite3_mutex_leave(p->db->mutex); 01096 } 01097 return rc; 01098 } 01099 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 01100 return sqlite3_bind_int64(p, i, (i64)iValue); 01101 } 01102 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 01103 int rc; 01104 Vdbe *p = (Vdbe *)pStmt; 01105 rc = vdbeUnbind(p, i); 01106 if( rc==SQLITE_OK ){ 01107 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 01108 sqlite3_mutex_leave(p->db->mutex); 01109 } 01110 return rc; 01111 } 01112 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 01113 int rc; 01114 Vdbe *p = (Vdbe*)pStmt; 01115 rc = vdbeUnbind(p, i); 01116 if( rc==SQLITE_OK ){ 01117 sqlite3_mutex_leave(p->db->mutex); 01118 } 01119 return rc; 01120 } 01121 int sqlite3_bind_text( 01122 sqlite3_stmt *pStmt, 01123 int i, 01124 const char *zData, 01125 int nData, 01126 void (*xDel)(void*) 01127 ){ 01128 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 01129 } 01130 #ifndef SQLITE_OMIT_UTF16 01131 int sqlite3_bind_text16( 01132 sqlite3_stmt *pStmt, 01133 int i, 01134 const void *zData, 01135 int nData, 01136 void (*xDel)(void*) 01137 ){ 01138 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 01139 } 01140 #endif /* SQLITE_OMIT_UTF16 */ 01141 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 01142 int rc; 01143 Vdbe *p = (Vdbe *)pStmt; 01144 rc = vdbeUnbind(p, i); 01145 if( rc==SQLITE_OK ){ 01146 rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); 01147 if( rc==SQLITE_OK ){ 01148 rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db)); 01149 } 01150 sqlite3_mutex_leave(p->db->mutex); 01151 } 01152 rc = sqlite3ApiExit(p->db, rc); 01153 return rc; 01154 } 01155 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 01156 int rc; 01157 Vdbe *p = (Vdbe *)pStmt; 01158 rc = vdbeUnbind(p, i); 01159 if( rc==SQLITE_OK ){ 01160 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 01161 sqlite3_mutex_leave(p->db->mutex); 01162 } 01163 return rc; 01164 } 01165 01166 /* 01167 ** Return the number of wildcards that can be potentially bound to. 01168 ** This routine is added to support DBD::SQLite. 01169 */ 01170 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 01171 Vdbe *p = (Vdbe*)pStmt; 01172 return p ? p->nVar : 0; 01173 } 01174 01175 /* 01176 ** Create a mapping from variable numbers to variable names 01177 ** in the Vdbe.azVar[] array, if such a mapping does not already 01178 ** exist. 01179 */ 01180 static void createVarMap(Vdbe *p){ 01181 if( !p->okVar ){ 01182 sqlite3_mutex_enter(p->db->mutex); 01183 if( !p->okVar ){ 01184 int j; 01185 Op *pOp; 01186 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 01187 if( pOp->opcode==OP_Variable ){ 01188 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 01189 p->azVar[pOp->p1-1] = pOp->p4.z; 01190 } 01191 } 01192 p->okVar = 1; 01193 } 01194 sqlite3_mutex_leave(p->db->mutex); 01195 } 01196 } 01197 01198 /* 01199 ** Return the name of a wildcard parameter. Return NULL if the index 01200 ** is out of range or if the wildcard is unnamed. 01201 ** 01202 ** The result is always UTF-8. 01203 */ 01204 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 01205 Vdbe *p = (Vdbe*)pStmt; 01206 if( p==0 || i<1 || i>p->nVar ){ 01207 return 0; 01208 } 01209 createVarMap(p); 01210 return p->azVar[i-1]; 01211 } 01212 01213 /* 01214 ** Given a wildcard parameter name, return the index of the variable 01215 ** with that name. If there is no variable with the given name, 01216 ** return 0. 01217 */ 01218 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 01219 Vdbe *p = (Vdbe*)pStmt; 01220 int i; 01221 if( p==0 ){ 01222 return 0; 01223 } 01224 createVarMap(p); 01225 if( zName ){ 01226 for(i=0; i<p->nVar; i++){ 01227 const char *z = p->azVar[i]; 01228 if( z && strcmp(z,zName)==0 ){ 01229 return i+1; 01230 } 01231 } 01232 } 01233 return 0; 01234 } 01235 01236 /* 01237 ** Transfer all bindings from the first statement over to the second. 01238 ** If the two statements contain a different number of bindings, then 01239 ** an SQLITE_ERROR is returned. 01240 */ 01241 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 01242 Vdbe *pFrom = (Vdbe*)pFromStmt; 01243 Vdbe *pTo = (Vdbe*)pToStmt; 01244 int i, rc = SQLITE_OK; 01245 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 01246 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) 01247 || pTo->db!=pFrom->db ){ 01248 return SQLITE_MISUSE; 01249 } 01250 if( pFrom->nVar!=pTo->nVar ){ 01251 return SQLITE_ERROR; 01252 } 01253 sqlite3_mutex_enter(pTo->db->mutex); 01254 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 01255 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 01256 } 01257 sqlite3_mutex_leave(pTo->db->mutex); 01258 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 01259 return rc; 01260 } 01261 01262 #ifndef SQLITE_OMIT_DEPRECATED 01263 /* 01264 ** Deprecated external interface. Internal/core SQLite code 01265 ** should call sqlite3TransferBindings. 01266 */ 01267 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 01268 return sqlite3TransferBindings(pFromStmt, pToStmt); 01269 } 01270 #endif 01271 01272 /* 01273 ** Return the sqlite3* database handle to which the prepared statement given 01274 ** in the argument belongs. This is the same database handle that was 01275 ** the first argument to the sqlite3_prepare() that was used to create 01276 ** the statement in the first place. 01277 */ 01278 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 01279 return pStmt ? ((Vdbe*)pStmt)->db : 0; 01280 } 01281 01282 /* 01283 ** Return a pointer to the next prepared statement after pStmt associated 01284 ** with database connection pDb. If pStmt is NULL, return the first 01285 ** prepared statement for the database connection. Return NULL if there 01286 ** are no more. 01287 */ 01288 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 01289 sqlite3_stmt *pNext; 01290 sqlite3_mutex_enter(pDb->mutex); 01291 if( pStmt==0 ){ 01292 pNext = (sqlite3_stmt*)pDb->pVdbe; 01293 }else{ 01294 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 01295 } 01296 sqlite3_mutex_leave(pDb->mutex); 01297 return pNext; 01298 } 01299 01300 /* 01301 ** Return the value of a status counter for a prepared statement 01302 */ 01303 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 01304 Vdbe *pVdbe = (Vdbe*)pStmt; 01305 int v = pVdbe->aCounter[op-1]; 01306 if( resetFlag ) pVdbe->aCounter[op-1] = 0; 01307 return v; 01308 }
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1