persistentstorage/sql/SQLite/vdbeapi.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 ** 2004 May 26
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 **
       
    13 ** This file contains code use to implement APIs that are part of the
       
    14 ** VDBE.
       
    15 **
       
    16 ** $Id: vdbeapi.c,v 1.138 2008/08/02 03:50:39 drh Exp $
       
    17 */
       
    18 #include "sqliteInt.h"
       
    19 #include "vdbeInt.h"
       
    20 
       
    21 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
    22 /*
       
    23 ** The following structure contains pointers to the end points of a
       
    24 ** doubly-linked list of all compiled SQL statements that may be holding
       
    25 ** buffers eligible for release when the sqlite3_release_memory() interface is
       
    26 ** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
       
    27 ** mutex.
       
    28 **
       
    29 ** Statements are added to the end of this list when sqlite3_reset() is
       
    30 ** called. They are removed either when sqlite3_step() or sqlite3_finalize()
       
    31 ** is called. When statements are added to this list, the associated 
       
    32 ** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
       
    33 ** can be freed using sqlite3VdbeReleaseMemory().
       
    34 **
       
    35 ** When statements are added or removed from this list, the mutex
       
    36 ** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
       
    37 ** already held. The LRU2 mutex is then obtained, blocking if necessary,
       
    38 ** the linked-list pointers manipulated and the LRU2 mutex relinquished.
       
    39 */
       
    40 struct StatementLruList {
       
    41   Vdbe *pFirst;
       
    42   Vdbe *pLast;
       
    43 };
       
    44 static struct StatementLruList sqlite3LruStatements;
       
    45 
       
    46 /*
       
    47 ** Check that the list looks to be internally consistent. This is used
       
    48 ** as part of an assert() statement as follows:
       
    49 **
       
    50 **   assert( stmtLruCheck() );
       
    51 */
       
    52 #ifndef NDEBUG
       
    53 static int stmtLruCheck(){
       
    54   Vdbe *p;
       
    55   for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
       
    56     assert(p->pLruNext || p==sqlite3LruStatements.pLast);
       
    57     assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
       
    58     assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
       
    59     assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
       
    60   }
       
    61   return 1;
       
    62 }
       
    63 #endif
       
    64 
       
    65 /*
       
    66 ** Add vdbe p to the end of the statement lru list. It is assumed that
       
    67 ** p is not already part of the list when this is called. The lru list
       
    68 ** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
       
    69 */
       
    70 static void stmtLruAdd(Vdbe *p){
       
    71   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
    72 
       
    73   if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
       
    74     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
    75     return;
       
    76   }
       
    77 
       
    78   assert( stmtLruCheck() );
       
    79 
       
    80   if( !sqlite3LruStatements.pFirst ){
       
    81     assert( !sqlite3LruStatements.pLast );
       
    82     sqlite3LruStatements.pFirst = p;
       
    83     sqlite3LruStatements.pLast = p;
       
    84   }else{
       
    85     assert( !sqlite3LruStatements.pLast->pLruNext );
       
    86     p->pLruPrev = sqlite3LruStatements.pLast;
       
    87     sqlite3LruStatements.pLast->pLruNext = p;
       
    88     sqlite3LruStatements.pLast = p;
       
    89   }
       
    90 
       
    91   assert( stmtLruCheck() );
       
    92 
       
    93   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
    94 }
       
    95 
       
    96 /*
       
    97 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
       
    98 ** statement p from the least-recently-used statement list. If the 
       
    99 ** statement is not currently part of the list, this call is a no-op.
       
   100 */
       
   101 static void stmtLruRemoveNomutex(Vdbe *p){
       
   102   if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
       
   103     assert( stmtLruCheck() );
       
   104     if( p->pLruNext ){
       
   105       p->pLruNext->pLruPrev = p->pLruPrev;
       
   106     }else{
       
   107       sqlite3LruStatements.pLast = p->pLruPrev;
       
   108     }
       
   109     if( p->pLruPrev ){
       
   110       p->pLruPrev->pLruNext = p->pLruNext;
       
   111     }else{
       
   112       sqlite3LruStatements.pFirst = p->pLruNext;
       
   113     }
       
   114     p->pLruNext = 0;
       
   115     p->pLruPrev = 0;
       
   116     assert( stmtLruCheck() );
       
   117   }
       
   118 }
       
   119 
       
   120 /*
       
   121 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
       
   122 ** statement p from the least-recently-used statement list. If the 
       
   123 ** statement is not currently part of the list, this call is a no-op.
       
   124 */
       
   125 static void stmtLruRemove(Vdbe *p){
       
   126   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
   127   stmtLruRemoveNomutex(p);
       
   128   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
   129 }
       
   130 
       
   131 /*
       
   132 ** Try to release n bytes of memory by freeing buffers associated 
       
   133 ** with the memory registers of currently unused vdbes.
       
   134 */
       
   135 int sqlite3VdbeReleaseMemory(int n){
       
   136   Vdbe *p;
       
   137   Vdbe *pNext;
       
   138   int nFree = 0;
       
   139 
       
   140   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
   141   for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
       
   142     pNext = p->pLruNext;
       
   143 
       
   144     /* For each statement handle in the lru list, attempt to obtain the
       
   145     ** associated database mutex. If it cannot be obtained, continue
       
   146     ** to the next statement handle. It is not possible to block on
       
   147     ** the database mutex - that could cause deadlock.
       
   148     */
       
   149     if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
       
   150       nFree += sqlite3VdbeReleaseBuffers(p);
       
   151       stmtLruRemoveNomutex(p);
       
   152       sqlite3_mutex_leave(p->db->mutex);
       
   153     }
       
   154   }
       
   155   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
       
   156 
       
   157   return nFree;
       
   158 }
       
   159 
       
   160 /*
       
   161 ** Call sqlite3Reprepare() on the statement. Remove it from the
       
   162 ** lru list before doing so, as Reprepare() will free all the
       
   163 ** memory register buffers anyway.
       
   164 */
       
   165 int vdbeReprepare(Vdbe *p){
       
   166   stmtLruRemove(p);
       
   167   return sqlite3Reprepare(p);
       
   168 }
       
   169 
       
   170 #else       /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
       
   171   #define stmtLruRemove(x)
       
   172   #define stmtLruAdd(x)
       
   173   #define vdbeReprepare(x) sqlite3Reprepare(x)
       
   174 #endif
       
   175 
       
   176 
       
   177 /*
       
   178 ** Return TRUE (non-zero) of the statement supplied as an argument needs
       
   179 ** to be recompiled.  A statement needs to be recompiled whenever the
       
   180 ** execution environment changes in a way that would alter the program
       
   181 ** that sqlite3_prepare() generates.  For example, if new functions or
       
   182 ** collating sequences are registered or if an authorizer function is
       
   183 ** added or changed.
       
   184 */
       
   185 int sqlite3_expired(sqlite3_stmt *pStmt){
       
   186   Vdbe *p = (Vdbe*)pStmt;
       
   187   return p==0 || p->expired;
       
   188 }
       
   189 
       
   190 /*
       
   191 ** The following routine destroys a virtual machine that is created by
       
   192 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
       
   193 ** success/failure code that describes the result of executing the virtual
       
   194 ** machine.
       
   195 **
       
   196 ** This routine sets the error code and string returned by
       
   197 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
       
   198 */
       
   199 int sqlite3_finalize(sqlite3_stmt *pStmt){
       
   200   int rc;
       
   201   if( pStmt==0 ){
       
   202     rc = SQLITE_OK;
       
   203   }else{
       
   204     Vdbe *v = (Vdbe*)pStmt;
       
   205 #ifndef SQLITE_MUTEX_NOOP
       
   206     sqlite3_mutex *mutex = v->db->mutex;
       
   207 #endif
       
   208     sqlite3_mutex_enter(mutex);
       
   209     stmtLruRemove(v);
       
   210     rc = sqlite3VdbeFinalize(v);
       
   211     sqlite3_mutex_leave(mutex);
       
   212   }
       
   213   return rc;
       
   214 }
       
   215 
       
   216 /*
       
   217 ** Terminate the current execution of an SQL statement and reset it
       
   218 ** back to its starting state so that it can be reused. A success code from
       
   219 ** the prior execution is returned.
       
   220 **
       
   221 ** This routine sets the error code and string returned by
       
   222 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
       
   223 */
       
   224 int sqlite3_reset(sqlite3_stmt *pStmt){
       
   225   int rc;
       
   226   if( pStmt==0 ){
       
   227     rc = SQLITE_OK;
       
   228   }else{
       
   229     Vdbe *v = (Vdbe*)pStmt;
       
   230     sqlite3_mutex_enter(v->db->mutex);
       
   231     rc = sqlite3VdbeReset(v);
       
   232     stmtLruAdd(v);
       
   233     sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
       
   234     assert( (rc & (v->db->errMask))==rc );
       
   235     sqlite3_mutex_leave(v->db->mutex);
       
   236   }
       
   237   return rc;
       
   238 }
       
   239 
       
   240 /*
       
   241 ** Set all the parameters in the compiled SQL statement to NULL.
       
   242 */
       
   243 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
       
   244   int i;
       
   245   int rc = SQLITE_OK;
       
   246   Vdbe *p = (Vdbe*)pStmt;
       
   247 #ifndef SQLITE_MUTEX_NOOP
       
   248   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
       
   249 #endif
       
   250   sqlite3_mutex_enter(mutex);
       
   251   for(i=0; i<p->nVar; i++){
       
   252     sqlite3VdbeMemRelease(&p->aVar[i]);
       
   253     p->aVar[i].flags = MEM_Null;
       
   254   }
       
   255   sqlite3_mutex_leave(mutex);
       
   256   return rc;
       
   257 }
       
   258 
       
   259 
       
   260 /**************************** sqlite3_value_  *******************************
       
   261 ** The following routines extract information from a Mem or sqlite3_value
       
   262 ** structure.
       
   263 */
       
   264 const void *sqlite3_value_blob(sqlite3_value *pVal){
       
   265   Mem *p = (Mem*)pVal;
       
   266   if( p->flags & (MEM_Blob|MEM_Str) ){
       
   267     sqlite3VdbeMemExpandBlob(p);
       
   268     p->flags &= ~MEM_Str;
       
   269     p->flags |= MEM_Blob;
       
   270     return p->z;
       
   271   }else{
       
   272     return sqlite3_value_text(pVal);
       
   273   }
       
   274 }
       
   275 int sqlite3_value_bytes(sqlite3_value *pVal){
       
   276   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
       
   277 }
       
   278 int sqlite3_value_bytes16(sqlite3_value *pVal){
       
   279   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
       
   280 }
       
   281 double sqlite3_value_double(sqlite3_value *pVal){
       
   282   return sqlite3VdbeRealValue((Mem*)pVal);
       
   283 }
       
   284 int sqlite3_value_int(sqlite3_value *pVal){
       
   285   return sqlite3VdbeIntValue((Mem*)pVal);
       
   286 }
       
   287 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
       
   288   return sqlite3VdbeIntValue((Mem*)pVal);
       
   289 }
       
   290 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
       
   291   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
       
   292 }
       
   293 #ifndef SQLITE_OMIT_UTF16
       
   294 const void *sqlite3_value_text16(sqlite3_value* pVal){
       
   295   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
       
   296 }
       
   297 const void *sqlite3_value_text16be(sqlite3_value *pVal){
       
   298   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
       
   299 }
       
   300 const void *sqlite3_value_text16le(sqlite3_value *pVal){
       
   301   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
       
   302 }
       
   303 #endif /* SQLITE_OMIT_UTF16 */
       
   304 int sqlite3_value_type(sqlite3_value* pVal){
       
   305   return pVal->type;
       
   306 }
       
   307 
       
   308 /**************************** sqlite3_result_  *******************************
       
   309 ** The following routines are used by user-defined functions to specify
       
   310 ** the function result.
       
   311 */
       
   312 void sqlite3_result_blob(
       
   313   sqlite3_context *pCtx, 
       
   314   const void *z, 
       
   315   int n, 
       
   316   void (*xDel)(void *)
       
   317 ){
       
   318   assert( n>=0 );
       
   319   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   320   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
       
   321 }
       
   322 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
       
   323   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   324   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
       
   325 }
       
   326 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
       
   327   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   328   pCtx->isError = SQLITE_ERROR;
       
   329   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
       
   330 }
       
   331 #ifndef SQLITE_OMIT_UTF16
       
   332 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
       
   333   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   334   pCtx->isError = SQLITE_ERROR;
       
   335   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
       
   336 }
       
   337 #endif
       
   338 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
       
   339   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   340   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
       
   341 }
       
   342 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
       
   343   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   344   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
       
   345 }
       
   346 void sqlite3_result_null(sqlite3_context *pCtx){
       
   347   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   348   sqlite3VdbeMemSetNull(&pCtx->s);
       
   349 }
       
   350 void sqlite3_result_text(
       
   351   sqlite3_context *pCtx, 
       
   352   const char *z, 
       
   353   int n,
       
   354   void (*xDel)(void *)
       
   355 ){
       
   356   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   357   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
       
   358 }
       
   359 #ifndef SQLITE_OMIT_UTF16
       
   360 void sqlite3_result_text16(
       
   361   sqlite3_context *pCtx, 
       
   362   const void *z, 
       
   363   int n, 
       
   364   void (*xDel)(void *)
       
   365 ){
       
   366   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   367   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
       
   368 }
       
   369 void sqlite3_result_text16be(
       
   370   sqlite3_context *pCtx, 
       
   371   const void *z, 
       
   372   int n, 
       
   373   void (*xDel)(void *)
       
   374 ){
       
   375   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   376   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
       
   377 }
       
   378 void sqlite3_result_text16le(
       
   379   sqlite3_context *pCtx, 
       
   380   const void *z, 
       
   381   int n, 
       
   382   void (*xDel)(void *)
       
   383 ){
       
   384   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   385   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
       
   386 }
       
   387 #endif /* SQLITE_OMIT_UTF16 */
       
   388 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
       
   389   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   390   sqlite3VdbeMemCopy(&pCtx->s, pValue);
       
   391 }
       
   392 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
       
   393   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   394   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
       
   395 }
       
   396 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
       
   397   pCtx->isError = errCode;
       
   398 }
       
   399 
       
   400 /* Force an SQLITE_TOOBIG error. */
       
   401 void sqlite3_result_error_toobig(sqlite3_context *pCtx){
       
   402   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   403   pCtx->isError = SQLITE_TOOBIG;
       
   404   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
       
   405                        SQLITE_UTF8, SQLITE_STATIC);
       
   406 }
       
   407 
       
   408 /* An SQLITE_NOMEM error. */
       
   409 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
       
   410   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   411   sqlite3VdbeMemSetNull(&pCtx->s);
       
   412   pCtx->isError = SQLITE_NOMEM;
       
   413   pCtx->s.db->mallocFailed = 1;
       
   414 }
       
   415 
       
   416 /*
       
   417 ** Execute the statement pStmt, either until a row of data is ready, the
       
   418 ** statement is completely executed or an error occurs.
       
   419 **
       
   420 ** This routine implements the bulk of the logic behind the sqlite_step()
       
   421 ** API.  The only thing omitted is the automatic recompile if a 
       
   422 ** schema change has occurred.  That detail is handled by the
       
   423 ** outer sqlite3_step() wrapper procedure.
       
   424 */
       
   425 static int sqlite3Step(Vdbe *p){
       
   426   sqlite3 *db;
       
   427   int rc;
       
   428 
       
   429   assert(p);
       
   430   if( p->magic!=VDBE_MAGIC_RUN ){
       
   431     return SQLITE_MISUSE;
       
   432   }
       
   433 
       
   434   /* Assert that malloc() has not failed */
       
   435   db = p->db;
       
   436   assert( !db->mallocFailed );
       
   437 
       
   438   if( p->pc<=0 && p->expired ){
       
   439     if( p->rc==SQLITE_OK ){
       
   440       p->rc = SQLITE_SCHEMA;
       
   441     }
       
   442     rc = SQLITE_ERROR;
       
   443     goto end_of_step;
       
   444   }
       
   445   if( sqlite3SafetyOn(db) ){
       
   446     p->rc = SQLITE_MISUSE;
       
   447     return SQLITE_MISUSE;
       
   448   }
       
   449   if( p->pc<0 ){
       
   450     /* If there are no other statements currently running, then
       
   451     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
       
   452     ** from interrupting a statement that has not yet started.
       
   453     */
       
   454     if( db->activeVdbeCnt==0 ){
       
   455       db->u1.isInterrupted = 0;
       
   456     }
       
   457 
       
   458 #ifndef SQLITE_OMIT_TRACE
       
   459     if( db->xProfile && !db->init.busy ){
       
   460       double rNow;
       
   461       sqlite3OsCurrentTime(db->pVfs, &rNow);
       
   462       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
       
   463     }
       
   464 #endif
       
   465 
       
   466     db->activeVdbeCnt++;
       
   467     p->pc = 0;
       
   468     stmtLruRemove(p);
       
   469   }
       
   470 #ifndef SQLITE_OMIT_EXPLAIN
       
   471   if( p->explain ){
       
   472     rc = sqlite3VdbeList(p);
       
   473   }else
       
   474 #endif /* SQLITE_OMIT_EXPLAIN */
       
   475   {
       
   476     rc = sqlite3VdbeExec(p);
       
   477   }
       
   478 
       
   479   if( sqlite3SafetyOff(db) ){
       
   480     rc = SQLITE_MISUSE;
       
   481   }
       
   482 
       
   483 #ifndef SQLITE_OMIT_TRACE
       
   484   /* Invoke the profile callback if there is one
       
   485   */
       
   486   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
       
   487            && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
       
   488     double rNow;
       
   489     u64 elapseTime;
       
   490 
       
   491     sqlite3OsCurrentTime(db->pVfs, &rNow);
       
   492     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
       
   493     db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
       
   494   }
       
   495 #endif
       
   496 
       
   497   db->errCode = rc;
       
   498   /*sqlite3Error(p->db, rc, 0);*/
       
   499   p->rc = sqlite3ApiExit(p->db, p->rc);
       
   500 end_of_step:
       
   501   assert( (rc&0xff)==rc );
       
   502   if( p->zSql && (rc&0xff)<SQLITE_ROW ){
       
   503     /* This behavior occurs if sqlite3_prepare_v2() was used to build
       
   504     ** the prepared statement.  Return error codes directly */
       
   505     p->db->errCode = p->rc;
       
   506     /* sqlite3Error(p->db, p->rc, 0); */
       
   507     return p->rc;
       
   508   }else{
       
   509     /* This is for legacy sqlite3_prepare() builds and when the code
       
   510     ** is SQLITE_ROW or SQLITE_DONE */
       
   511     return rc;
       
   512   }
       
   513 }
       
   514 
       
   515 /*
       
   516 ** This is the top-level implementation of sqlite3_step().  Call
       
   517 ** sqlite3Step() to do most of the work.  If a schema error occurs,
       
   518 ** call sqlite3Reprepare() and try again.
       
   519 */
       
   520 #ifdef SQLITE_OMIT_PARSER
       
   521 int sqlite3_step(sqlite3_stmt *pStmt){
       
   522   int rc = SQLITE_MISUSE;
       
   523   if( pStmt ){
       
   524     Vdbe *v;
       
   525     v = (Vdbe*)pStmt;
       
   526     sqlite3_mutex_enter(v->db->mutex);
       
   527     rc = sqlite3Step(v);
       
   528     sqlite3_mutex_leave(v->db->mutex);
       
   529   }
       
   530   return rc;
       
   531 }
       
   532 #else
       
   533 int sqlite3_step(sqlite3_stmt *pStmt){
       
   534   int rc = SQLITE_MISUSE;
       
   535   if( pStmt ){
       
   536     int cnt = 0;
       
   537     Vdbe *v = (Vdbe*)pStmt;
       
   538     sqlite3 *db = v->db;
       
   539     sqlite3_mutex_enter(db->mutex);
       
   540     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
       
   541            && cnt++ < 5
       
   542            && vdbeReprepare(v) ){
       
   543       sqlite3_reset(pStmt);
       
   544       v->expired = 0;
       
   545     }
       
   546     if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
       
   547       /* This case occurs after failing to recompile an sql statement. 
       
   548       ** The error message from the SQL compiler has already been loaded 
       
   549       ** into the database handle. This block copies the error message 
       
   550       ** from the database handle into the statement and sets the statement
       
   551       ** program counter to 0 to ensure that when the statement is 
       
   552       ** finalized or reset the parser error message is available via
       
   553       ** sqlite3_errmsg() and sqlite3_errcode().
       
   554       */
       
   555       const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
       
   556       sqlite3DbFree(db, v->zErrMsg);
       
   557       if( !db->mallocFailed ){
       
   558         v->zErrMsg = sqlite3DbStrDup(db, zErr);
       
   559       } else {
       
   560         v->zErrMsg = 0;
       
   561         v->rc = SQLITE_NOMEM;
       
   562       }
       
   563     }
       
   564     rc = sqlite3ApiExit(db, rc);
       
   565     sqlite3_mutex_leave(db->mutex);
       
   566   }
       
   567   return rc;
       
   568 }
       
   569 #endif
       
   570 
       
   571 /*
       
   572 ** Extract the user data from a sqlite3_context structure and return a
       
   573 ** pointer to it.
       
   574 */
       
   575 void *sqlite3_user_data(sqlite3_context *p){
       
   576   assert( p && p->pFunc );
       
   577   return p->pFunc->pUserData;
       
   578 }
       
   579 
       
   580 /*
       
   581 ** Extract the user data from a sqlite3_context structure and return a
       
   582 ** pointer to it.
       
   583 */
       
   584 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
       
   585   assert( p && p->pFunc );
       
   586   return p->s.db;
       
   587 }
       
   588 
       
   589 /*
       
   590 ** The following is the implementation of an SQL function that always
       
   591 ** fails with an error message stating that the function is used in the
       
   592 ** wrong context.  The sqlite3_overload_function() API might construct
       
   593 ** SQL function that use this routine so that the functions will exist
       
   594 ** for name resolution but are actually overloaded by the xFindFunction
       
   595 ** method of virtual tables.
       
   596 */
       
   597 void sqlite3InvalidFunction(
       
   598   sqlite3_context *context,  /* The function calling context */
       
   599   int argc,                  /* Number of arguments to the function */
       
   600   sqlite3_value **argv       /* Value of each argument */
       
   601 ){
       
   602   const char *zName = context->pFunc->zName;
       
   603   char *zErr;
       
   604   zErr = sqlite3MPrintf(0,
       
   605       "unable to use function %s in the requested context", zName);
       
   606   sqlite3_result_error(context, zErr, -1);
       
   607   sqlite3_free(zErr);
       
   608 }
       
   609 
       
   610 /*
       
   611 ** Allocate or return the aggregate context for a user function.  A new
       
   612 ** context is allocated on the first call.  Subsequent calls return the
       
   613 ** same context that was returned on prior calls.
       
   614 */
       
   615 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
       
   616   Mem *pMem;
       
   617   assert( p && p->pFunc && p->pFunc->xStep );
       
   618   assert( sqlite3_mutex_held(p->s.db->mutex) );
       
   619   pMem = p->pMem;
       
   620   if( (pMem->flags & MEM_Agg)==0 ){
       
   621     if( nByte==0 ){
       
   622       sqlite3VdbeMemReleaseExternal(pMem);
       
   623       pMem->flags = MEM_Null;
       
   624       pMem->z = 0;
       
   625     }else{
       
   626       sqlite3VdbeMemGrow(pMem, nByte, 0);
       
   627       pMem->flags = MEM_Agg;
       
   628       pMem->u.pDef = p->pFunc;
       
   629       if( pMem->z ){
       
   630         memset(pMem->z, 0, nByte);
       
   631       }
       
   632     }
       
   633   }
       
   634   return (void*)pMem->z;
       
   635 }
       
   636 
       
   637 /*
       
   638 ** Return the auxilary data pointer, if any, for the iArg'th argument to
       
   639 ** the user-function defined by pCtx.
       
   640 */
       
   641 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
       
   642   VdbeFunc *pVdbeFunc;
       
   643 
       
   644   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   645   pVdbeFunc = pCtx->pVdbeFunc;
       
   646   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
       
   647     return 0;
       
   648   }
       
   649   return pVdbeFunc->apAux[iArg].pAux;
       
   650 }
       
   651 
       
   652 /*
       
   653 ** Set the auxilary data pointer and delete function, for the iArg'th
       
   654 ** argument to the user-function defined by pCtx. Any previous value is
       
   655 ** deleted by calling the delete function specified when it was set.
       
   656 */
       
   657 void sqlite3_set_auxdata(
       
   658   sqlite3_context *pCtx, 
       
   659   int iArg, 
       
   660   void *pAux, 
       
   661   void (*xDelete)(void*)
       
   662 ){
       
   663   struct AuxData *pAuxData;
       
   664   VdbeFunc *pVdbeFunc;
       
   665   if( iArg<0 ) goto failed;
       
   666 
       
   667   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
   668   pVdbeFunc = pCtx->pVdbeFunc;
       
   669   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
       
   670     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
       
   671     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
       
   672     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
       
   673     if( !pVdbeFunc ){
       
   674       goto failed;
       
   675     }
       
   676     pCtx->pVdbeFunc = pVdbeFunc;
       
   677     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
       
   678     pVdbeFunc->nAux = iArg+1;
       
   679     pVdbeFunc->pFunc = pCtx->pFunc;
       
   680   }
       
   681 
       
   682   pAuxData = &pVdbeFunc->apAux[iArg];
       
   683   if( pAuxData->pAux && pAuxData->xDelete ){
       
   684     pAuxData->xDelete(pAuxData->pAux);
       
   685   }
       
   686   pAuxData->pAux = pAux;
       
   687   pAuxData->xDelete = xDelete;
       
   688   return;
       
   689 
       
   690 failed:
       
   691   if( xDelete ){
       
   692     xDelete(pAux);
       
   693   }
       
   694 }
       
   695 
       
   696 /*
       
   697 ** Return the number of times the Step function of a aggregate has been 
       
   698 ** called.
       
   699 **
       
   700 ** This function is deprecated.  Do not use it for new code.  It is
       
   701 ** provide only to avoid breaking legacy code.  New aggregate function
       
   702 ** implementations should keep their own counts within their aggregate
       
   703 ** context.
       
   704 */
       
   705 int sqlite3_aggregate_count(sqlite3_context *p){
       
   706   assert( p && p->pFunc && p->pFunc->xStep );
       
   707   return p->pMem->n;
       
   708 }
       
   709 
       
   710 /*
       
   711 ** Return the number of columns in the result set for the statement pStmt.
       
   712 */
       
   713 int sqlite3_column_count(sqlite3_stmt *pStmt){
       
   714   Vdbe *pVm = (Vdbe *)pStmt;
       
   715   return pVm ? pVm->nResColumn : 0;
       
   716 }
       
   717 
       
   718 /*
       
   719 ** Return the number of values available from the current row of the
       
   720 ** currently executing statement pStmt.
       
   721 */
       
   722 int sqlite3_data_count(sqlite3_stmt *pStmt){
       
   723   Vdbe *pVm = (Vdbe *)pStmt;
       
   724   if( pVm==0 || pVm->pResultSet==0 ) return 0;
       
   725   return pVm->nResColumn;
       
   726 }
       
   727 
       
   728 
       
   729 /*
       
   730 ** Check to see if column iCol of the given statement is valid.  If
       
   731 ** it is, return a pointer to the Mem for the value of that column.
       
   732 ** If iCol is not valid, return a pointer to a Mem which has a value
       
   733 ** of NULL.
       
   734 */
       
   735 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
       
   736   Vdbe *pVm;
       
   737   int vals;
       
   738   Mem *pOut;
       
   739 
       
   740   pVm = (Vdbe *)pStmt;
       
   741   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
       
   742     sqlite3_mutex_enter(pVm->db->mutex);
       
   743     vals = sqlite3_data_count(pStmt);
       
   744     pOut = &pVm->pResultSet[i];
       
   745   }else{
       
   746     static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
       
   747     if( pVm->db ){
       
   748       sqlite3_mutex_enter(pVm->db->mutex);
       
   749       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
       
   750     }
       
   751     pOut = (Mem*)&nullMem;
       
   752   }
       
   753   return pOut;
       
   754 }
       
   755 
       
   756 /*
       
   757 ** This function is called after invoking an sqlite3_value_XXX function on a 
       
   758 ** column value (i.e. a value returned by evaluating an SQL expression in the
       
   759 ** select list of a SELECT statement) that may cause a malloc() failure. If 
       
   760 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
       
   761 ** code of statement pStmt set to SQLITE_NOMEM.
       
   762 **
       
   763 ** Specifically, this is called from within:
       
   764 **
       
   765 **     sqlite3_column_int()
       
   766 **     sqlite3_column_int64()
       
   767 **     sqlite3_column_text()
       
   768 **     sqlite3_column_text16()
       
   769 **     sqlite3_column_real()
       
   770 **     sqlite3_column_bytes()
       
   771 **     sqlite3_column_bytes16()
       
   772 **
       
   773 ** But not for sqlite3_column_blob(), which never calls malloc().
       
   774 */
       
   775 static void columnMallocFailure(sqlite3_stmt *pStmt)
       
   776 {
       
   777   /* If malloc() failed during an encoding conversion within an
       
   778   ** sqlite3_column_XXX API, then set the return code of the statement to
       
   779   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
       
   780   ** and _finalize() will return NOMEM.
       
   781   */
       
   782   Vdbe *p = (Vdbe *)pStmt;
       
   783   if( p ){
       
   784     p->rc = sqlite3ApiExit(p->db, p->rc);
       
   785     sqlite3_mutex_leave(p->db->mutex);
       
   786   }
       
   787 }
       
   788 
       
   789 /**************************** sqlite3_column_  *******************************
       
   790 ** The following routines are used to access elements of the current row
       
   791 ** in the result set.
       
   792 */
       
   793 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
       
   794   const void *val;
       
   795   val = sqlite3_value_blob( columnMem(pStmt,i) );
       
   796   /* Even though there is no encoding conversion, value_blob() might
       
   797   ** need to call malloc() to expand the result of a zeroblob() 
       
   798   ** expression. 
       
   799   */
       
   800   columnMallocFailure(pStmt);
       
   801   return val;
       
   802 }
       
   803 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
       
   804   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
       
   805   columnMallocFailure(pStmt);
       
   806   return val;
       
   807 }
       
   808 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
       
   809   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
       
   810   columnMallocFailure(pStmt);
       
   811   return val;
       
   812 }
       
   813 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
       
   814   double val = sqlite3_value_double( columnMem(pStmt,i) );
       
   815   columnMallocFailure(pStmt);
       
   816   return val;
       
   817 }
       
   818 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
       
   819   int val = sqlite3_value_int( columnMem(pStmt,i) );
       
   820   columnMallocFailure(pStmt);
       
   821   return val;
       
   822 }
       
   823 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
       
   824   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
       
   825   columnMallocFailure(pStmt);
       
   826   return val;
       
   827 }
       
   828 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
       
   829   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
       
   830   columnMallocFailure(pStmt);
       
   831   return val;
       
   832 }
       
   833 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
       
   834   sqlite3_value *pOut = columnMem(pStmt, i);
       
   835   columnMallocFailure(pStmt);
       
   836   return pOut;
       
   837 }
       
   838 #ifndef SQLITE_OMIT_UTF16
       
   839 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
       
   840   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
       
   841   columnMallocFailure(pStmt);
       
   842   return val;
       
   843 }
       
   844 #endif /* SQLITE_OMIT_UTF16 */
       
   845 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
       
   846   int iType = sqlite3_value_type( columnMem(pStmt,i) );
       
   847   columnMallocFailure(pStmt);
       
   848   return iType;
       
   849 }
       
   850 
       
   851 /* The following function is experimental and subject to change or
       
   852 ** removal */
       
   853 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
       
   854 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
       
   855 **}
       
   856 */
       
   857 
       
   858 /*
       
   859 ** Convert the N-th element of pStmt->pColName[] into a string using
       
   860 ** xFunc() then return that string.  If N is out of range, return 0.
       
   861 **
       
   862 ** There are up to 5 names for each column.  useType determines which
       
   863 ** name is returned.  Here are the names:
       
   864 **
       
   865 **    0      The column name as it should be displayed for output
       
   866 **    1      The datatype name for the column
       
   867 **    2      The name of the database that the column derives from
       
   868 **    3      The name of the table that the column derives from
       
   869 **    4      The name of the table column that the result column derives from
       
   870 **
       
   871 ** If the result is not a simple column reference (if it is an expression
       
   872 ** or a constant) then useTypes 2, 3, and 4 return NULL.
       
   873 */
       
   874 static const void *columnName(
       
   875   sqlite3_stmt *pStmt,
       
   876   int N,
       
   877   const void *(*xFunc)(Mem*),
       
   878   int useType
       
   879 ){
       
   880   const void *ret = 0;
       
   881   Vdbe *p = (Vdbe *)pStmt;
       
   882   int n;
       
   883   
       
   884 
       
   885   if( p!=0 ){
       
   886     n = sqlite3_column_count(pStmt);
       
   887     if( N<n && N>=0 ){
       
   888       N += useType*n;
       
   889       sqlite3_mutex_enter(p->db->mutex);
       
   890       ret = xFunc(&p->aColName[N]);
       
   891 
       
   892       /* A malloc may have failed inside of the xFunc() call. If this
       
   893       ** is the case, clear the mallocFailed flag and return NULL.
       
   894       */
       
   895       if( p->db && p->db->mallocFailed ){
       
   896         p->db->mallocFailed = 0;
       
   897         ret = 0;
       
   898       }
       
   899       sqlite3_mutex_leave(p->db->mutex);
       
   900     }
       
   901   }
       
   902   return ret;
       
   903 }
       
   904 
       
   905 /*
       
   906 ** Return the name of the Nth column of the result set returned by SQL
       
   907 ** statement pStmt.
       
   908 */
       
   909 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
       
   910   return columnName(
       
   911       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
       
   912 }
       
   913 #ifndef SQLITE_OMIT_UTF16
       
   914 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
       
   915   return columnName(
       
   916       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
       
   917 }
       
   918 #endif
       
   919 
       
   920 /*
       
   921 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
       
   922 ** not define OMIT_DECLTYPE.
       
   923 */
       
   924 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
       
   925 # error "Must not define both SQLITE_OMIT_DECLTYPE \
       
   926          and SQLITE_ENABLE_COLUMN_METADATA"
       
   927 #endif
       
   928 
       
   929 #ifndef SQLITE_OMIT_DECLTYPE
       
   930 /*
       
   931 ** Return the column declaration type (if applicable) of the 'i'th column
       
   932 ** of the result set of SQL statement pStmt.
       
   933 */
       
   934 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
       
   935   return columnName(
       
   936       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
       
   937 }
       
   938 #ifndef SQLITE_OMIT_UTF16
       
   939 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
       
   940   return columnName(
       
   941       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
       
   942 }
       
   943 #endif /* SQLITE_OMIT_UTF16 */
       
   944 #endif /* SQLITE_OMIT_DECLTYPE */
       
   945 
       
   946 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
   947 /*
       
   948 ** Return the name of the database from which a result column derives.
       
   949 ** NULL is returned if the result column is an expression or constant or
       
   950 ** anything else which is not an unabiguous reference to a database column.
       
   951 */
       
   952 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
       
   953   return columnName(
       
   954       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
       
   955 }
       
   956 #ifndef SQLITE_OMIT_UTF16
       
   957 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
       
   958   return columnName(
       
   959       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
       
   960 }
       
   961 #endif /* SQLITE_OMIT_UTF16 */
       
   962 
       
   963 /*
       
   964 ** Return the name of the table from which a result column derives.
       
   965 ** NULL is returned if the result column is an expression or constant or
       
   966 ** anything else which is not an unabiguous reference to a database column.
       
   967 */
       
   968 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
       
   969   return columnName(
       
   970       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
       
   971 }
       
   972 #ifndef SQLITE_OMIT_UTF16
       
   973 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
       
   974   return columnName(
       
   975       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
       
   976 }
       
   977 #endif /* SQLITE_OMIT_UTF16 */
       
   978 
       
   979 /*
       
   980 ** Return the name of the table column from which a result column derives.
       
   981 ** NULL is returned if the result column is an expression or constant or
       
   982 ** anything else which is not an unabiguous reference to a database column.
       
   983 */
       
   984 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
       
   985   return columnName(
       
   986       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
       
   987 }
       
   988 #ifndef SQLITE_OMIT_UTF16
       
   989 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
       
   990   return columnName(
       
   991       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
       
   992 }
       
   993 #endif /* SQLITE_OMIT_UTF16 */
       
   994 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
       
   995 
       
   996 
       
   997 /******************************* sqlite3_bind_  ***************************
       
   998 ** 
       
   999 ** Routines used to attach values to wildcards in a compiled SQL statement.
       
  1000 */
       
  1001 /*
       
  1002 ** Unbind the value bound to variable i in virtual machine p. This is the 
       
  1003 ** the same as binding a NULL value to the column. If the "i" parameter is
       
  1004 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
       
  1005 **
       
  1006 ** The error code stored in database p->db is overwritten with the return
       
  1007 ** value in any case.
       
  1008 */
       
  1009 static int vdbeUnbind(Vdbe *p, int i){
       
  1010   Mem *pVar;
       
  1011   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
       
  1012     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
       
  1013     return SQLITE_MISUSE;
       
  1014   }
       
  1015   if( i<1 || i>p->nVar ){
       
  1016     sqlite3Error(p->db, SQLITE_RANGE, 0);
       
  1017     return SQLITE_RANGE;
       
  1018   }
       
  1019   i--;
       
  1020   pVar = &p->aVar[i];
       
  1021   sqlite3VdbeMemRelease(pVar);
       
  1022   pVar->flags = MEM_Null;
       
  1023   sqlite3Error(p->db, SQLITE_OK, 0);
       
  1024   return SQLITE_OK;
       
  1025 }
       
  1026 
       
  1027 /*
       
  1028 ** Bind a text or BLOB value.
       
  1029 */
       
  1030 static int bindText(
       
  1031   sqlite3_stmt *pStmt,   /* The statement to bind against */
       
  1032   int i,                 /* Index of the parameter to bind */
       
  1033   const void *zData,     /* Pointer to the data to be bound */
       
  1034   int nData,             /* Number of bytes of data to be bound */
       
  1035   void (*xDel)(void*),   /* Destructor for the data */
       
  1036   int encoding           /* Encoding for the data */
       
  1037 ){
       
  1038   Vdbe *p = (Vdbe *)pStmt;
       
  1039   Mem *pVar;
       
  1040   int rc;
       
  1041 
       
  1042   if( p==0 ){
       
  1043     return SQLITE_MISUSE;
       
  1044   }
       
  1045   sqlite3_mutex_enter(p->db->mutex);
       
  1046   rc = vdbeUnbind(p, i);
       
  1047   if( rc==SQLITE_OK && zData!=0 ){
       
  1048     pVar = &p->aVar[i-1];
       
  1049     rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
       
  1050     if( rc==SQLITE_OK && encoding!=0 ){
       
  1051       rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
       
  1052     }
       
  1053     sqlite3Error(p->db, rc, 0);
       
  1054     rc = sqlite3ApiExit(p->db, rc);
       
  1055   }
       
  1056   sqlite3_mutex_leave(p->db->mutex);
       
  1057   return rc;
       
  1058 }
       
  1059 
       
  1060 
       
  1061 /*
       
  1062 ** Bind a blob value to an SQL statement variable.
       
  1063 */
       
  1064 int sqlite3_bind_blob(
       
  1065   sqlite3_stmt *pStmt, 
       
  1066   int i, 
       
  1067   const void *zData, 
       
  1068   int nData, 
       
  1069   void (*xDel)(void*)
       
  1070 ){
       
  1071   return bindText(pStmt, i, zData, nData, xDel, 0);
       
  1072 }
       
  1073 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
       
  1074   int rc;
       
  1075   Vdbe *p = (Vdbe *)pStmt;
       
  1076   sqlite3_mutex_enter(p->db->mutex);
       
  1077   rc = vdbeUnbind(p, i);
       
  1078   if( rc==SQLITE_OK ){
       
  1079     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
       
  1080   }
       
  1081   sqlite3_mutex_leave(p->db->mutex);
       
  1082   return rc;
       
  1083 }
       
  1084 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
       
  1085   return sqlite3_bind_int64(p, i, (i64)iValue);
       
  1086 }
       
  1087 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
       
  1088   int rc;
       
  1089   Vdbe *p = (Vdbe *)pStmt;
       
  1090   sqlite3_mutex_enter(p->db->mutex);
       
  1091   rc = vdbeUnbind(p, i);
       
  1092   if( rc==SQLITE_OK ){
       
  1093     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
       
  1094   }
       
  1095   sqlite3_mutex_leave(p->db->mutex);
       
  1096   return rc;
       
  1097 }
       
  1098 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
       
  1099   int rc;
       
  1100   Vdbe *p = (Vdbe*)pStmt;
       
  1101   sqlite3_mutex_enter(p->db->mutex);
       
  1102   rc = vdbeUnbind(p, i);
       
  1103   sqlite3_mutex_leave(p->db->mutex);
       
  1104   return rc;
       
  1105 }
       
  1106 int sqlite3_bind_text( 
       
  1107   sqlite3_stmt *pStmt, 
       
  1108   int i, 
       
  1109   const char *zData, 
       
  1110   int nData, 
       
  1111   void (*xDel)(void*)
       
  1112 ){
       
  1113   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
       
  1114 }
       
  1115 #ifndef SQLITE_OMIT_UTF16
       
  1116 int sqlite3_bind_text16(
       
  1117   sqlite3_stmt *pStmt, 
       
  1118   int i, 
       
  1119   const void *zData, 
       
  1120   int nData, 
       
  1121   void (*xDel)(void*)
       
  1122 ){
       
  1123   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
       
  1124 }
       
  1125 #endif /* SQLITE_OMIT_UTF16 */
       
  1126 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
       
  1127   int rc;
       
  1128   Vdbe *p = (Vdbe *)pStmt;
       
  1129   sqlite3_mutex_enter(p->db->mutex);
       
  1130   rc = vdbeUnbind(p, i);
       
  1131   if( rc==SQLITE_OK ){
       
  1132     rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
       
  1133     if( rc==SQLITE_OK ){
       
  1134       rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db));
       
  1135     }
       
  1136   }
       
  1137   rc = sqlite3ApiExit(p->db, rc);
       
  1138   sqlite3_mutex_leave(p->db->mutex);
       
  1139   return rc;
       
  1140 }
       
  1141 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
       
  1142   int rc;
       
  1143   Vdbe *p = (Vdbe *)pStmt;
       
  1144   sqlite3_mutex_enter(p->db->mutex);
       
  1145   rc = vdbeUnbind(p, i);
       
  1146   if( rc==SQLITE_OK ){
       
  1147     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
       
  1148   }
       
  1149   sqlite3_mutex_leave(p->db->mutex);
       
  1150   return rc;
       
  1151 }
       
  1152 
       
  1153 /*
       
  1154 ** Return the number of wildcards that can be potentially bound to.
       
  1155 ** This routine is added to support DBD::SQLite.  
       
  1156 */
       
  1157 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
       
  1158   Vdbe *p = (Vdbe*)pStmt;
       
  1159   return p ? p->nVar : 0;
       
  1160 }
       
  1161 
       
  1162 /*
       
  1163 ** Create a mapping from variable numbers to variable names
       
  1164 ** in the Vdbe.azVar[] array, if such a mapping does not already
       
  1165 ** exist.
       
  1166 */
       
  1167 static void createVarMap(Vdbe *p){
       
  1168   if( !p->okVar ){
       
  1169     sqlite3_mutex_enter(p->db->mutex);
       
  1170     if( !p->okVar ){
       
  1171       int j;
       
  1172       Op *pOp;
       
  1173       for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
       
  1174         if( pOp->opcode==OP_Variable ){
       
  1175           assert( pOp->p1>0 && pOp->p1<=p->nVar );
       
  1176           p->azVar[pOp->p1-1] = pOp->p4.z;
       
  1177         }
       
  1178       }
       
  1179       p->okVar = 1;
       
  1180     }
       
  1181     sqlite3_mutex_leave(p->db->mutex);
       
  1182   }
       
  1183 }
       
  1184 
       
  1185 /*
       
  1186 ** Return the name of a wildcard parameter.  Return NULL if the index
       
  1187 ** is out of range or if the wildcard is unnamed.
       
  1188 **
       
  1189 ** The result is always UTF-8.
       
  1190 */
       
  1191 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
       
  1192   Vdbe *p = (Vdbe*)pStmt;
       
  1193   if( p==0 || i<1 || i>p->nVar ){
       
  1194     return 0;
       
  1195   }
       
  1196   createVarMap(p);
       
  1197   return p->azVar[i-1];
       
  1198 }
       
  1199 
       
  1200 /*
       
  1201 ** Given a wildcard parameter name, return the index of the variable
       
  1202 ** with that name.  If there is no variable with the given name,
       
  1203 ** return 0.
       
  1204 */
       
  1205 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
       
  1206   Vdbe *p = (Vdbe*)pStmt;
       
  1207   int i;
       
  1208   if( p==0 ){
       
  1209     return 0;
       
  1210   }
       
  1211   createVarMap(p); 
       
  1212   if( zName ){
       
  1213     for(i=0; i<p->nVar; i++){
       
  1214       const char *z = p->azVar[i];
       
  1215       if( z && strcmp(z,zName)==0 ){
       
  1216         return i+1;
       
  1217       }
       
  1218     }
       
  1219   }
       
  1220   return 0;
       
  1221 }
       
  1222 
       
  1223 /*
       
  1224 ** Transfer all bindings from the first statement over to the second.
       
  1225 ** If the two statements contain a different number of bindings, then
       
  1226 ** an SQLITE_ERROR is returned.
       
  1227 */
       
  1228 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
       
  1229   Vdbe *pFrom = (Vdbe*)pFromStmt;
       
  1230   Vdbe *pTo = (Vdbe*)pToStmt;
       
  1231   int i, rc = SQLITE_OK;
       
  1232   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
       
  1233     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
       
  1234     || pTo->db!=pFrom->db ){
       
  1235     return SQLITE_MISUSE;
       
  1236   }
       
  1237   if( pFrom->nVar!=pTo->nVar ){
       
  1238     return SQLITE_ERROR;
       
  1239   }
       
  1240   sqlite3_mutex_enter(pTo->db->mutex);
       
  1241   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
       
  1242     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
       
  1243   }
       
  1244   sqlite3_mutex_leave(pTo->db->mutex);
       
  1245   assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
       
  1246   return rc;
       
  1247 }
       
  1248 
       
  1249 /*
       
  1250 ** Return the sqlite3* database handle to which the prepared statement given
       
  1251 ** in the argument belongs.  This is the same database handle that was
       
  1252 ** the first argument to the sqlite3_prepare() that was used to create
       
  1253 ** the statement in the first place.
       
  1254 */
       
  1255 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
       
  1256   return pStmt ? ((Vdbe*)pStmt)->db : 0;
       
  1257 }
       
  1258 
       
  1259 /*
       
  1260 ** Return a pointer to the next prepared statement after pStmt associated
       
  1261 ** with database connection pDb.  If pStmt is NULL, return the first
       
  1262 ** prepared statement for the database connection.  Return NULL if there
       
  1263 ** are no more.
       
  1264 */
       
  1265 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
       
  1266   sqlite3_stmt *pNext;
       
  1267   sqlite3_mutex_enter(pDb->mutex);
       
  1268   if( pStmt==0 ){
       
  1269     pNext = (sqlite3_stmt*)pDb->pVdbe;
       
  1270   }else{
       
  1271     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
       
  1272   }
       
  1273   sqlite3_mutex_leave(pDb->mutex);
       
  1274   return pNext;
       
  1275 }