os/persistentdata/persistentstorage/sqlite3api/SQLite/vdbeapi.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/SQLite/vdbeapi.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1285 @@
     1.4 +/*
     1.5 +** 2004 May 26
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +**
    1.16 +** This file contains code use to implement APIs that are part of the
    1.17 +** VDBE.
    1.18 +**
    1.19 +** $Id: vdbeapi.c,v 1.141 2008/09/04 12:03:43 shane Exp $
    1.20 +*/
    1.21 +#include "sqliteInt.h"
    1.22 +#include "vdbeInt.h"
    1.23 +
    1.24 +#if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
    1.25 +/*
    1.26 +** The following structure contains pointers to the end points of a
    1.27 +** doubly-linked list of all compiled SQL statements that may be holding
    1.28 +** buffers eligible for release when the sqlite3_release_memory() interface is
    1.29 +** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
    1.30 +** mutex.
    1.31 +**
    1.32 +** Statements are added to the end of this list when sqlite3_reset() is
    1.33 +** called. They are removed either when sqlite3_step() or sqlite3_finalize()
    1.34 +** is called. When statements are added to this list, the associated 
    1.35 +** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
    1.36 +** can be freed using sqlite3VdbeReleaseMemory().
    1.37 +**
    1.38 +** When statements are added or removed from this list, the mutex
    1.39 +** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
    1.40 +** already held. The LRU2 mutex is then obtained, blocking if necessary,
    1.41 +** the linked-list pointers manipulated and the LRU2 mutex relinquished.
    1.42 +*/
    1.43 +struct StatementLruList {
    1.44 +  Vdbe *pFirst;
    1.45 +  Vdbe *pLast;
    1.46 +};
    1.47 +static struct StatementLruList sqlite3LruStatements;
    1.48 +
    1.49 +/*
    1.50 +** Check that the list looks to be internally consistent. This is used
    1.51 +** as part of an assert() statement as follows:
    1.52 +**
    1.53 +**   assert( stmtLruCheck() );
    1.54 +*/
    1.55 +#ifndef NDEBUG
    1.56 +static int stmtLruCheck(){
    1.57 +  Vdbe *p;
    1.58 +  for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
    1.59 +    assert(p->pLruNext || p==sqlite3LruStatements.pLast);
    1.60 +    assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
    1.61 +    assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
    1.62 +    assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
    1.63 +  }
    1.64 +  return 1;
    1.65 +}
    1.66 +#endif
    1.67 +
    1.68 +/*
    1.69 +** Add vdbe p to the end of the statement lru list. It is assumed that
    1.70 +** p is not already part of the list when this is called. The lru list
    1.71 +** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
    1.72 +*/
    1.73 +static void stmtLruAdd(Vdbe *p){
    1.74 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
    1.75 +
    1.76 +  if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
    1.77 +    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
    1.78 +    return;
    1.79 +  }
    1.80 +
    1.81 +  assert( stmtLruCheck() );
    1.82 +
    1.83 +  if( !sqlite3LruStatements.pFirst ){
    1.84 +    assert( !sqlite3LruStatements.pLast );
    1.85 +    sqlite3LruStatements.pFirst = p;
    1.86 +    sqlite3LruStatements.pLast = p;
    1.87 +  }else{
    1.88 +    assert( !sqlite3LruStatements.pLast->pLruNext );
    1.89 +    p->pLruPrev = sqlite3LruStatements.pLast;
    1.90 +    sqlite3LruStatements.pLast->pLruNext = p;
    1.91 +    sqlite3LruStatements.pLast = p;
    1.92 +  }
    1.93 +
    1.94 +  assert( stmtLruCheck() );
    1.95 +
    1.96 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
    1.97 +}
    1.98 +
    1.99 +/*
   1.100 +** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
   1.101 +** statement p from the least-recently-used statement list. If the 
   1.102 +** statement is not currently part of the list, this call is a no-op.
   1.103 +*/
   1.104 +static void stmtLruRemoveNomutex(Vdbe *p){
   1.105 +  if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
   1.106 +    assert( stmtLruCheck() );
   1.107 +    if( p->pLruNext ){
   1.108 +      p->pLruNext->pLruPrev = p->pLruPrev;
   1.109 +    }else{
   1.110 +      sqlite3LruStatements.pLast = p->pLruPrev;
   1.111 +    }
   1.112 +    if( p->pLruPrev ){
   1.113 +      p->pLruPrev->pLruNext = p->pLruNext;
   1.114 +    }else{
   1.115 +      sqlite3LruStatements.pFirst = p->pLruNext;
   1.116 +    }
   1.117 +    p->pLruNext = 0;
   1.118 +    p->pLruPrev = 0;
   1.119 +    assert( stmtLruCheck() );
   1.120 +  }
   1.121 +}
   1.122 +
   1.123 +/*
   1.124 +** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
   1.125 +** statement p from the least-recently-used statement list. If the 
   1.126 +** statement is not currently part of the list, this call is a no-op.
   1.127 +*/
   1.128 +static void stmtLruRemove(Vdbe *p){
   1.129 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
   1.130 +  stmtLruRemoveNomutex(p);
   1.131 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
   1.132 +}
   1.133 +
   1.134 +/*
   1.135 +** Try to release n bytes of memory by freeing buffers associated 
   1.136 +** with the memory registers of currently unused vdbes.
   1.137 +*/
   1.138 +int sqlite3VdbeReleaseMemory(int n){
   1.139 +  Vdbe *p;
   1.140 +  Vdbe *pNext;
   1.141 +  int nFree = 0;
   1.142 +
   1.143 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
   1.144 +  for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
   1.145 +    pNext = p->pLruNext;
   1.146 +
   1.147 +    /* For each statement handle in the lru list, attempt to obtain the
   1.148 +    ** associated database mutex. If it cannot be obtained, continue
   1.149 +    ** to the next statement handle. It is not possible to block on
   1.150 +    ** the database mutex - that could cause deadlock.
   1.151 +    */
   1.152 +    if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
   1.153 +      nFree += sqlite3VdbeReleaseBuffers(p);
   1.154 +      stmtLruRemoveNomutex(p);
   1.155 +      sqlite3_mutex_leave(p->db->mutex);
   1.156 +    }
   1.157 +  }
   1.158 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
   1.159 +
   1.160 +  return nFree;
   1.161 +}
   1.162 +
   1.163 +/*
   1.164 +** Call sqlite3Reprepare() on the statement. Remove it from the
   1.165 +** lru list before doing so, as Reprepare() will free all the
   1.166 +** memory register buffers anyway.
   1.167 +*/
   1.168 +int vdbeReprepare(Vdbe *p){
   1.169 +  stmtLruRemove(p);
   1.170 +  return sqlite3Reprepare(p);
   1.171 +}
   1.172 +
   1.173 +#else       /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
   1.174 +  #define stmtLruRemove(x)
   1.175 +  #define stmtLruAdd(x)
   1.176 +  #define vdbeReprepare(x) sqlite3Reprepare(x)
   1.177 +#endif
   1.178 +
   1.179 +
   1.180 +/*
   1.181 +** Return TRUE (non-zero) of the statement supplied as an argument needs
   1.182 +** to be recompiled.  A statement needs to be recompiled whenever the
   1.183 +** execution environment changes in a way that would alter the program
   1.184 +** that sqlite3_prepare() generates.  For example, if new functions or
   1.185 +** collating sequences are registered or if an authorizer function is
   1.186 +** added or changed.
   1.187 +*/
   1.188 +SQLITE_EXPORT int sqlite3_expired(sqlite3_stmt *pStmt){
   1.189 +  Vdbe *p = (Vdbe*)pStmt;
   1.190 +  return p==0 || p->expired;
   1.191 +}
   1.192 +
   1.193 +/*
   1.194 +** The following routine destroys a virtual machine that is created by
   1.195 +** the sqlite3_compile() routine. The integer returned is an SQLITE_
   1.196 +** success/failure code that describes the result of executing the virtual
   1.197 +** machine.
   1.198 +**
   1.199 +** This routine sets the error code and string returned by
   1.200 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   1.201 +*/
   1.202 +SQLITE_EXPORT int sqlite3_finalize(sqlite3_stmt *pStmt){
   1.203 +  int rc;
   1.204 +  if( pStmt==0 ){
   1.205 +    rc = SQLITE_OK;
   1.206 +  }else{
   1.207 +    Vdbe *v = (Vdbe*)pStmt;
   1.208 +#ifndef SQLITE_MUTEX_NOOP
   1.209 +    sqlite3_mutex *mutex = v->db->mutex;
   1.210 +#endif
   1.211 +    sqlite3_mutex_enter(mutex);
   1.212 +    stmtLruRemove(v);
   1.213 +    rc = sqlite3VdbeFinalize(v);
   1.214 +    sqlite3_mutex_leave(mutex);
   1.215 +  }
   1.216 +  return rc;
   1.217 +}
   1.218 +
   1.219 +/*
   1.220 +** Terminate the current execution of an SQL statement and reset it
   1.221 +** back to its starting state so that it can be reused. A success code from
   1.222 +** the prior execution is returned.
   1.223 +**
   1.224 +** This routine sets the error code and string returned by
   1.225 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   1.226 +*/
   1.227 +SQLITE_EXPORT int sqlite3_reset(sqlite3_stmt *pStmt){
   1.228 +  int rc;
   1.229 +  if( pStmt==0 ){
   1.230 +    rc = SQLITE_OK;
   1.231 +  }else{
   1.232 +    Vdbe *v = (Vdbe*)pStmt;
   1.233 +    sqlite3_mutex_enter(v->db->mutex);
   1.234 +    rc = sqlite3VdbeReset(v);
   1.235 +    stmtLruAdd(v);
   1.236 +    sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
   1.237 +    assert( (rc & (v->db->errMask))==rc );
   1.238 +    sqlite3_mutex_leave(v->db->mutex);
   1.239 +  }
   1.240 +  return rc;
   1.241 +}
   1.242 +
   1.243 +/*
   1.244 +** Set all the parameters in the compiled SQL statement to NULL.
   1.245 +*/
   1.246 +SQLITE_EXPORT int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
   1.247 +  int i;
   1.248 +  int rc = SQLITE_OK;
   1.249 +  Vdbe *p = (Vdbe*)pStmt;
   1.250 +#ifndef SQLITE_MUTEX_NOOP
   1.251 +  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
   1.252 +#endif
   1.253 +  sqlite3_mutex_enter(mutex);
   1.254 +  for(i=0; i<p->nVar; i++){
   1.255 +    sqlite3VdbeMemRelease(&p->aVar[i]);
   1.256 +    p->aVar[i].flags = MEM_Null;
   1.257 +  }
   1.258 +  sqlite3_mutex_leave(mutex);
   1.259 +  return rc;
   1.260 +}
   1.261 +
   1.262 +
   1.263 +/**************************** sqlite3_value_  *******************************
   1.264 +** The following routines extract information from a Mem or sqlite3_value
   1.265 +** structure.
   1.266 +*/
   1.267 +SQLITE_EXPORT const void *sqlite3_value_blob(sqlite3_value *pVal){
   1.268 +  Mem *p = (Mem*)pVal;
   1.269 +  if( p->flags & (MEM_Blob|MEM_Str) ){
   1.270 +    sqlite3VdbeMemExpandBlob(p);
   1.271 +    p->flags &= ~MEM_Str;
   1.272 +    p->flags |= MEM_Blob;
   1.273 +    return p->z;
   1.274 +  }else{
   1.275 +    return sqlite3_value_text(pVal);
   1.276 +  }
   1.277 +}
   1.278 +SQLITE_EXPORT int sqlite3_value_bytes(sqlite3_value *pVal){
   1.279 +  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
   1.280 +}
   1.281 +SQLITE_EXPORT int sqlite3_value_bytes16(sqlite3_value *pVal){
   1.282 +  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
   1.283 +}
   1.284 +SQLITE_EXPORT double sqlite3_value_double(sqlite3_value *pVal){
   1.285 +  return sqlite3VdbeRealValue((Mem*)pVal);
   1.286 +}
   1.287 +SQLITE_EXPORT int sqlite3_value_int(sqlite3_value *pVal){
   1.288 +  return sqlite3VdbeIntValue((Mem*)pVal);
   1.289 +}
   1.290 +SQLITE_EXPORT sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
   1.291 +  return sqlite3VdbeIntValue((Mem*)pVal);
   1.292 +}
   1.293 +SQLITE_EXPORT const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
   1.294 +  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
   1.295 +}
   1.296 +#ifndef SQLITE_OMIT_UTF16
   1.297 +SQLITE_EXPORT const void *sqlite3_value_text16(sqlite3_value* pVal){
   1.298 +  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
   1.299 +}
   1.300 +SQLITE_EXPORT const void *sqlite3_value_text16be(sqlite3_value *pVal){
   1.301 +  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
   1.302 +}
   1.303 +SQLITE_EXPORT const void *sqlite3_value_text16le(sqlite3_value *pVal){
   1.304 +  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
   1.305 +}
   1.306 +#endif /* SQLITE_OMIT_UTF16 */
   1.307 +SQLITE_EXPORT int sqlite3_value_type(sqlite3_value* pVal){
   1.308 +  return pVal->type;
   1.309 +}
   1.310 +
   1.311 +/**************************** sqlite3_result_  *******************************
   1.312 +** The following routines are used by user-defined functions to specify
   1.313 +** the function result.
   1.314 +*/
   1.315 +SQLITE_EXPORT void sqlite3_result_blob(
   1.316 +  sqlite3_context *pCtx, 
   1.317 +  const void *z, 
   1.318 +  int n, 
   1.319 +  void (*xDel)(void *)
   1.320 +){
   1.321 +  assert( n>=0 );
   1.322 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.323 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
   1.324 +}
   1.325 +SQLITE_EXPORT void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   1.326 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.327 +  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
   1.328 +}
   1.329 +SQLITE_EXPORT void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   1.330 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.331 +  pCtx->isError = SQLITE_ERROR;
   1.332 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
   1.333 +}
   1.334 +#ifndef SQLITE_OMIT_UTF16
   1.335 +SQLITE_EXPORT void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
   1.336 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.337 +  pCtx->isError = SQLITE_ERROR;
   1.338 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   1.339 +}
   1.340 +#endif
   1.341 +SQLITE_EXPORT void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   1.342 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.343 +  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
   1.344 +}
   1.345 +SQLITE_EXPORT void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   1.346 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.347 +  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
   1.348 +}
   1.349 +SQLITE_EXPORT void sqlite3_result_null(sqlite3_context *pCtx){
   1.350 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.351 +  sqlite3VdbeMemSetNull(&pCtx->s);
   1.352 +}
   1.353 +SQLITE_EXPORT void sqlite3_result_text(
   1.354 +  sqlite3_context *pCtx, 
   1.355 +  const char *z, 
   1.356 +  int n,
   1.357 +  void (*xDel)(void *)
   1.358 +){
   1.359 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.360 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
   1.361 +}
   1.362 +#ifndef SQLITE_OMIT_UTF16
   1.363 +SQLITE_EXPORT void sqlite3_result_text16(
   1.364 +  sqlite3_context *pCtx, 
   1.365 +  const void *z, 
   1.366 +  int n, 
   1.367 +  void (*xDel)(void *)
   1.368 +){
   1.369 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.370 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
   1.371 +}
   1.372 +SQLITE_EXPORT void sqlite3_result_text16be(
   1.373 +  sqlite3_context *pCtx, 
   1.374 +  const void *z, 
   1.375 +  int n, 
   1.376 +  void (*xDel)(void *)
   1.377 +){
   1.378 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.379 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
   1.380 +}
   1.381 +SQLITE_EXPORT void sqlite3_result_text16le(
   1.382 +  sqlite3_context *pCtx, 
   1.383 +  const void *z, 
   1.384 +  int n, 
   1.385 +  void (*xDel)(void *)
   1.386 +){
   1.387 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.388 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
   1.389 +}
   1.390 +#endif /* SQLITE_OMIT_UTF16 */
   1.391 +SQLITE_EXPORT void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   1.392 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.393 +  sqlite3VdbeMemCopy(&pCtx->s, pValue);
   1.394 +}
   1.395 +SQLITE_EXPORT void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   1.396 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.397 +  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
   1.398 +}
   1.399 +SQLITE_EXPORT void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
   1.400 +  pCtx->isError = errCode;
   1.401 +}
   1.402 +
   1.403 +/* Force an SQLITE_TOOBIG error. */
   1.404 +SQLITE_EXPORT void sqlite3_result_error_toobig(sqlite3_context *pCtx){
   1.405 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.406 +  pCtx->isError = SQLITE_TOOBIG;
   1.407 +  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
   1.408 +                       SQLITE_UTF8, SQLITE_STATIC);
   1.409 +}
   1.410 +
   1.411 +/* An SQLITE_NOMEM error. */
   1.412 +SQLITE_EXPORT void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   1.413 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.414 +  sqlite3VdbeMemSetNull(&pCtx->s);
   1.415 +  pCtx->isError = SQLITE_NOMEM;
   1.416 +  pCtx->s.db->mallocFailed = 1;
   1.417 +}
   1.418 +
   1.419 +/*
   1.420 +** Execute the statement pStmt, either until a row of data is ready, the
   1.421 +** statement is completely executed or an error occurs.
   1.422 +**
   1.423 +** This routine implements the bulk of the logic behind the sqlite_step()
   1.424 +** API.  The only thing omitted is the automatic recompile if a 
   1.425 +** schema change has occurred.  That detail is handled by the
   1.426 +** outer sqlite3_step() wrapper procedure.
   1.427 +*/
   1.428 +static int sqlite3Step(Vdbe *p){
   1.429 +  sqlite3 *db;
   1.430 +  int rc;
   1.431 +
   1.432 +  assert(p);
   1.433 +  if( p->magic!=VDBE_MAGIC_RUN ){
   1.434 +    return SQLITE_MISUSE;
   1.435 +  }
   1.436 +
   1.437 +  /* Assert that malloc() has not failed */
   1.438 +  db = p->db;
   1.439 +  if( db->mallocFailed ){
   1.440 +    return SQLITE_NOMEM;
   1.441 +  }
   1.442 +
   1.443 +  if( p->pc<=0 && p->expired ){
   1.444 +    if( p->rc==SQLITE_OK ){
   1.445 +      p->rc = SQLITE_SCHEMA;
   1.446 +    }
   1.447 +    rc = SQLITE_ERROR;
   1.448 +    goto end_of_step;
   1.449 +  }
   1.450 +  if( sqlite3SafetyOn(db) ){
   1.451 +    p->rc = SQLITE_MISUSE;
   1.452 +    return SQLITE_MISUSE;
   1.453 +  }
   1.454 +  if( p->pc<0 ){
   1.455 +    /* If there are no other statements currently running, then
   1.456 +    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
   1.457 +    ** from interrupting a statement that has not yet started.
   1.458 +    */
   1.459 +    if( db->activeVdbeCnt==0 ){
   1.460 +      db->u1.isInterrupted = 0;
   1.461 +    }
   1.462 +
   1.463 +#ifndef SQLITE_OMIT_TRACE
   1.464 +    if( db->xProfile && !db->init.busy ){
   1.465 +      double rNow;
   1.466 +      sqlite3OsCurrentTime(db->pVfs, &rNow);
   1.467 +      p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
   1.468 +    }
   1.469 +#endif
   1.470 +
   1.471 +    db->activeVdbeCnt++;
   1.472 +    p->pc = 0;
   1.473 +    stmtLruRemove(p);
   1.474 +  }
   1.475 +#ifndef SQLITE_OMIT_EXPLAIN
   1.476 +  if( p->explain ){
   1.477 +    rc = sqlite3VdbeList(p);
   1.478 +  }else
   1.479 +#endif /* SQLITE_OMIT_EXPLAIN */
   1.480 +  {
   1.481 +    rc = sqlite3VdbeExec(p);
   1.482 +  }
   1.483 +
   1.484 +  if( sqlite3SafetyOff(db) ){
   1.485 +    rc = SQLITE_MISUSE;
   1.486 +  }
   1.487 +
   1.488 +#ifndef SQLITE_OMIT_TRACE
   1.489 +  /* Invoke the profile callback if there is one
   1.490 +  */
   1.491 +  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
   1.492 +           && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
   1.493 +    double rNow;
   1.494 +    u64 elapseTime;
   1.495 +
   1.496 +    sqlite3OsCurrentTime(db->pVfs, &rNow);
   1.497 +    elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
   1.498 +    db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
   1.499 +  }
   1.500 +#endif
   1.501 +
   1.502 +  db->errCode = rc;
   1.503 +  /*sqlite3Error(p->db, rc, 0);*/
   1.504 +  p->rc = sqlite3ApiExit(p->db, p->rc);
   1.505 +end_of_step:
   1.506 +  assert( (rc&0xff)==rc );
   1.507 +  if( p->zSql && (rc&0xff)<SQLITE_ROW ){
   1.508 +    /* This behavior occurs if sqlite3_prepare_v2() was used to build
   1.509 +    ** the prepared statement.  Return error codes directly */
   1.510 +    p->db->errCode = p->rc;
   1.511 +    /* sqlite3Error(p->db, p->rc, 0); */
   1.512 +    return p->rc;
   1.513 +  }else{
   1.514 +    /* This is for legacy sqlite3_prepare() builds and when the code
   1.515 +    ** is SQLITE_ROW or SQLITE_DONE */
   1.516 +    return rc;
   1.517 +  }
   1.518 +}
   1.519 +
   1.520 +/*
   1.521 +** This is the top-level implementation of sqlite3_step().  Call
   1.522 +** sqlite3Step() to do most of the work.  If a schema error occurs,
   1.523 +** call sqlite3Reprepare() and try again.
   1.524 +*/
   1.525 +#ifdef SQLITE_OMIT_PARSER
   1.526 +int sqlite3_step(sqlite3_stmt *pStmt){
   1.527 +  int rc = SQLITE_MISUSE;
   1.528 +  if( pStmt ){
   1.529 +    Vdbe *v;
   1.530 +    v = (Vdbe*)pStmt;
   1.531 +    sqlite3_mutex_enter(v->db->mutex);
   1.532 +    rc = sqlite3Step(v);
   1.533 +    sqlite3_mutex_leave(v->db->mutex);
   1.534 +  }
   1.535 +  return rc;
   1.536 +}
   1.537 +#else
   1.538 +SQLITE_EXPORT int sqlite3_step(sqlite3_stmt *pStmt){
   1.539 +  int rc = SQLITE_MISUSE;
   1.540 +  if( pStmt ){
   1.541 +    int cnt = 0;
   1.542 +    Vdbe *v = (Vdbe*)pStmt;
   1.543 +    sqlite3 *db = v->db;
   1.544 +    sqlite3_mutex_enter(db->mutex);
   1.545 +    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   1.546 +           && cnt++ < 5
   1.547 +           && vdbeReprepare(v) ){
   1.548 +      sqlite3_reset(pStmt);
   1.549 +      v->expired = 0;
   1.550 +    }
   1.551 +    if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
   1.552 +      /* This case occurs after failing to recompile an sql statement. 
   1.553 +      ** The error message from the SQL compiler has already been loaded 
   1.554 +      ** into the database handle. This block copies the error message 
   1.555 +      ** from the database handle into the statement and sets the statement
   1.556 +      ** program counter to 0 to ensure that when the statement is 
   1.557 +      ** finalized or reset the parser error message is available via
   1.558 +      ** sqlite3_errmsg() and sqlite3_errcode().
   1.559 +      */
   1.560 +      const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
   1.561 +      sqlite3DbFree(db, v->zErrMsg);
   1.562 +      if( !db->mallocFailed ){
   1.563 +        v->zErrMsg = sqlite3DbStrDup(db, zErr);
   1.564 +      } else {
   1.565 +        v->zErrMsg = 0;
   1.566 +        v->rc = SQLITE_NOMEM;
   1.567 +      }
   1.568 +    }
   1.569 +    rc = sqlite3ApiExit(db, rc);
   1.570 +    sqlite3_mutex_leave(db->mutex);
   1.571 +  }
   1.572 +  return rc;
   1.573 +}
   1.574 +#endif
   1.575 +
   1.576 +/*
   1.577 +** Extract the user data from a sqlite3_context structure and return a
   1.578 +** pointer to it.
   1.579 +*/
   1.580 +SQLITE_EXPORT void *sqlite3_user_data(sqlite3_context *p){
   1.581 +  assert( p && p->pFunc );
   1.582 +  return p->pFunc->pUserData;
   1.583 +}
   1.584 +
   1.585 +/*
   1.586 +** Extract the user data from a sqlite3_context structure and return a
   1.587 +** pointer to it.
   1.588 +*/
   1.589 +SQLITE_EXPORT sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
   1.590 +  assert( p && p->pFunc );
   1.591 +  return p->s.db;
   1.592 +}
   1.593 +
   1.594 +/*
   1.595 +** The following is the implementation of an SQL function that always
   1.596 +** fails with an error message stating that the function is used in the
   1.597 +** wrong context.  The sqlite3_overload_function() API might construct
   1.598 +** SQL function that use this routine so that the functions will exist
   1.599 +** for name resolution but are actually overloaded by the xFindFunction
   1.600 +** method of virtual tables.
   1.601 +*/
   1.602 +void sqlite3InvalidFunction(
   1.603 +  sqlite3_context *context,  /* The function calling context */
   1.604 +  int argc,                  /* Number of arguments to the function */
   1.605 +  sqlite3_value **argv       /* Value of each argument */
   1.606 +){
   1.607 +  const char *zName = context->pFunc->zName;
   1.608 +  char *zErr;
   1.609 +  zErr = sqlite3MPrintf(0,
   1.610 +      "unable to use function %s in the requested context", zName);
   1.611 +  sqlite3_result_error(context, zErr, -1);
   1.612 +  sqlite3_free(zErr);
   1.613 +}
   1.614 +
   1.615 +/*
   1.616 +** Allocate or return the aggregate context for a user function.  A new
   1.617 +** context is allocated on the first call.  Subsequent calls return the
   1.618 +** same context that was returned on prior calls.
   1.619 +*/
   1.620 +SQLITE_EXPORT void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   1.621 +  Mem *pMem;
   1.622 +  assert( p && p->pFunc && p->pFunc->xStep );
   1.623 +  assert( sqlite3_mutex_held(p->s.db->mutex) );
   1.624 +  pMem = p->pMem;
   1.625 +  if( (pMem->flags & MEM_Agg)==0 ){
   1.626 +    if( nByte==0 ){
   1.627 +      sqlite3VdbeMemReleaseExternal(pMem);
   1.628 +      pMem->flags = MEM_Null;
   1.629 +      pMem->z = 0;
   1.630 +    }else{
   1.631 +      sqlite3VdbeMemGrow(pMem, nByte, 0);
   1.632 +      pMem->flags = MEM_Agg;
   1.633 +      pMem->u.pDef = p->pFunc;
   1.634 +      if( pMem->z ){
   1.635 +        memset(pMem->z, 0, nByte);
   1.636 +      }
   1.637 +    }
   1.638 +  }
   1.639 +  return (void*)pMem->z;
   1.640 +}
   1.641 +
   1.642 +/*
   1.643 +** Return the auxilary data pointer, if any, for the iArg'th argument to
   1.644 +** the user-function defined by pCtx.
   1.645 +*/
   1.646 +SQLITE_EXPORT void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   1.647 +  VdbeFunc *pVdbeFunc;
   1.648 +
   1.649 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.650 +  pVdbeFunc = pCtx->pVdbeFunc;
   1.651 +  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
   1.652 +    return 0;
   1.653 +  }
   1.654 +  return pVdbeFunc->apAux[iArg].pAux;
   1.655 +}
   1.656 +
   1.657 +/*
   1.658 +** Set the auxilary data pointer and delete function, for the iArg'th
   1.659 +** argument to the user-function defined by pCtx. Any previous value is
   1.660 +** deleted by calling the delete function specified when it was set.
   1.661 +*/
   1.662 +SQLITE_EXPORT void sqlite3_set_auxdata(
   1.663 +  sqlite3_context *pCtx, 
   1.664 +  int iArg, 
   1.665 +  void *pAux, 
   1.666 +  void (*xDelete)(void*)
   1.667 +){
   1.668 +  struct AuxData *pAuxData;
   1.669 +  VdbeFunc *pVdbeFunc;
   1.670 +  if( iArg<0 ) goto failed;
   1.671 +
   1.672 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   1.673 +  pVdbeFunc = pCtx->pVdbeFunc;
   1.674 +  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
   1.675 +    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
   1.676 +    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
   1.677 +    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
   1.678 +    if( !pVdbeFunc ){
   1.679 +      goto failed;
   1.680 +    }
   1.681 +    pCtx->pVdbeFunc = pVdbeFunc;
   1.682 +    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
   1.683 +    pVdbeFunc->nAux = iArg+1;
   1.684 +    pVdbeFunc->pFunc = pCtx->pFunc;
   1.685 +  }
   1.686 +
   1.687 +  pAuxData = &pVdbeFunc->apAux[iArg];
   1.688 +  if( pAuxData->pAux && pAuxData->xDelete ){
   1.689 +    pAuxData->xDelete(pAuxData->pAux);
   1.690 +  }
   1.691 +  pAuxData->pAux = pAux;
   1.692 +  pAuxData->xDelete = xDelete;
   1.693 +  return;
   1.694 +
   1.695 +failed:
   1.696 +  if( xDelete ){
   1.697 +    xDelete(pAux);
   1.698 +  }
   1.699 +}
   1.700 +
   1.701 +/*
   1.702 +** Return the number of times the Step function of a aggregate has been 
   1.703 +** called.
   1.704 +**
   1.705 +** This function is deprecated.  Do not use it for new code.  It is
   1.706 +** provide only to avoid breaking legacy code.  New aggregate function
   1.707 +** implementations should keep their own counts within their aggregate
   1.708 +** context.
   1.709 +*/
   1.710 +SQLITE_EXPORT int sqlite3_aggregate_count(sqlite3_context *p){
   1.711 +  assert( p && p->pFunc && p->pFunc->xStep );
   1.712 +  return p->pMem->n;
   1.713 +}
   1.714 +
   1.715 +/*
   1.716 +** Return the number of columns in the result set for the statement pStmt.
   1.717 +*/
   1.718 +SQLITE_EXPORT int sqlite3_column_count(sqlite3_stmt *pStmt){
   1.719 +  Vdbe *pVm = (Vdbe *)pStmt;
   1.720 +  return pVm ? pVm->nResColumn : 0;
   1.721 +}
   1.722 +
   1.723 +/*
   1.724 +** Return the number of values available from the current row of the
   1.725 +** currently executing statement pStmt.
   1.726 +*/
   1.727 +SQLITE_EXPORT int sqlite3_data_count(sqlite3_stmt *pStmt){
   1.728 +  Vdbe *pVm = (Vdbe *)pStmt;
   1.729 +  if( pVm==0 || pVm->pResultSet==0 ) return 0;
   1.730 +  return pVm->nResColumn;
   1.731 +}
   1.732 +
   1.733 +
   1.734 +/*
   1.735 +** Check to see if column iCol of the given statement is valid.  If
   1.736 +** it is, return a pointer to the Mem for the value of that column.
   1.737 +** If iCol is not valid, return a pointer to a Mem which has a value
   1.738 +** of NULL.
   1.739 +*/
   1.740 +static Mem *columnMem(sqlite3_stmt *pStmt, int i){
   1.741 +  Vdbe *pVm;
   1.742 +  int vals;
   1.743 +  Mem *pOut;
   1.744 +
   1.745 +  pVm = (Vdbe *)pStmt;
   1.746 +  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
   1.747 +    sqlite3_mutex_enter(pVm->db->mutex);
   1.748 +    vals = sqlite3_data_count(pStmt);
   1.749 +    pOut = &pVm->pResultSet[i];
   1.750 +  }else{
   1.751 +    static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
   1.752 +    if( pVm->db ){
   1.753 +      sqlite3_mutex_enter(pVm->db->mutex);
   1.754 +      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   1.755 +    }
   1.756 +    pOut = (Mem*)&nullMem;
   1.757 +  }
   1.758 +  return pOut;
   1.759 +}
   1.760 +
   1.761 +/*
   1.762 +** This function is called after invoking an sqlite3_value_XXX function on a 
   1.763 +** column value (i.e. a value returned by evaluating an SQL expression in the
   1.764 +** select list of a SELECT statement) that may cause a malloc() failure. If 
   1.765 +** malloc() has failed, the threads mallocFailed flag is cleared and the result
   1.766 +** code of statement pStmt set to SQLITE_NOMEM.
   1.767 +**
   1.768 +** Specifically, this is called from within:
   1.769 +**
   1.770 +**     sqlite3_column_int()
   1.771 +**     sqlite3_column_int64()
   1.772 +**     sqlite3_column_text()
   1.773 +**     sqlite3_column_text16()
   1.774 +**     sqlite3_column_real()
   1.775 +**     sqlite3_column_bytes()
   1.776 +**     sqlite3_column_bytes16()
   1.777 +**
   1.778 +** But not for sqlite3_column_blob(), which never calls malloc().
   1.779 +*/
   1.780 +static void columnMallocFailure(sqlite3_stmt *pStmt)
   1.781 +{
   1.782 +  /* If malloc() failed during an encoding conversion within an
   1.783 +  ** sqlite3_column_XXX API, then set the return code of the statement to
   1.784 +  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
   1.785 +  ** and _finalize() will return NOMEM.
   1.786 +  */
   1.787 +  Vdbe *p = (Vdbe *)pStmt;
   1.788 +  if( p ){
   1.789 +    p->rc = sqlite3ApiExit(p->db, p->rc);
   1.790 +    sqlite3_mutex_leave(p->db->mutex);
   1.791 +  }
   1.792 +}
   1.793 +
   1.794 +/**************************** sqlite3_column_  *******************************
   1.795 +** The following routines are used to access elements of the current row
   1.796 +** in the result set.
   1.797 +*/
   1.798 +SQLITE_EXPORT const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
   1.799 +  const void *val;
   1.800 +  val = sqlite3_value_blob( columnMem(pStmt,i) );
   1.801 +  /* Even though there is no encoding conversion, value_blob() might
   1.802 +  ** need to call malloc() to expand the result of a zeroblob() 
   1.803 +  ** expression. 
   1.804 +  */
   1.805 +  columnMallocFailure(pStmt);
   1.806 +  return val;
   1.807 +}
   1.808 +SQLITE_EXPORT int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
   1.809 +  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
   1.810 +  columnMallocFailure(pStmt);
   1.811 +  return val;
   1.812 +}
   1.813 +SQLITE_EXPORT int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
   1.814 +  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
   1.815 +  columnMallocFailure(pStmt);
   1.816 +  return val;
   1.817 +}
   1.818 +SQLITE_EXPORT double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
   1.819 +  double val = sqlite3_value_double( columnMem(pStmt,i) );
   1.820 +  columnMallocFailure(pStmt);
   1.821 +  return val;
   1.822 +}
   1.823 +SQLITE_EXPORT int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
   1.824 +  int val = sqlite3_value_int( columnMem(pStmt,i) );
   1.825 +  columnMallocFailure(pStmt);
   1.826 +  return val;
   1.827 +}
   1.828 +SQLITE_EXPORT sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
   1.829 +  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
   1.830 +  columnMallocFailure(pStmt);
   1.831 +  return val;
   1.832 +}
   1.833 +SQLITE_EXPORT const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
   1.834 +  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
   1.835 +  columnMallocFailure(pStmt);
   1.836 +  return val;
   1.837 +}
   1.838 +SQLITE_EXPORT sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
   1.839 +  sqlite3_value *pOut = columnMem(pStmt, i);
   1.840 +  columnMallocFailure(pStmt);
   1.841 +  return pOut;
   1.842 +}
   1.843 +#ifndef SQLITE_OMIT_UTF16
   1.844 +SQLITE_EXPORT const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
   1.845 +  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
   1.846 +  columnMallocFailure(pStmt);
   1.847 +  return val;
   1.848 +}
   1.849 +#endif /* SQLITE_OMIT_UTF16 */
   1.850 +SQLITE_EXPORT int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
   1.851 +  int iType = sqlite3_value_type( columnMem(pStmt,i) );
   1.852 +  columnMallocFailure(pStmt);
   1.853 +  return iType;
   1.854 +}
   1.855 +
   1.856 +/* The following function is experimental and subject to change or
   1.857 +** removal */
   1.858 +/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
   1.859 +**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
   1.860 +**}
   1.861 +*/
   1.862 +
   1.863 +/*
   1.864 +** Convert the N-th element of pStmt->pColName[] into a string using
   1.865 +** xFunc() then return that string.  If N is out of range, return 0.
   1.866 +**
   1.867 +** There are up to 5 names for each column.  useType determines which
   1.868 +** name is returned.  Here are the names:
   1.869 +**
   1.870 +**    0      The column name as it should be displayed for output
   1.871 +**    1      The datatype name for the column
   1.872 +**    2      The name of the database that the column derives from
   1.873 +**    3      The name of the table that the column derives from
   1.874 +**    4      The name of the table column that the result column derives from
   1.875 +**
   1.876 +** If the result is not a simple column reference (if it is an expression
   1.877 +** or a constant) then useTypes 2, 3, and 4 return NULL.
   1.878 +*/
   1.879 +static const void *columnName(
   1.880 +  sqlite3_stmt *pStmt,
   1.881 +  int N,
   1.882 +  const void *(*xFunc)(Mem*),
   1.883 +  int useType
   1.884 +){
   1.885 +  const void *ret = 0;
   1.886 +  Vdbe *p = (Vdbe *)pStmt;
   1.887 +  int n;
   1.888 +  
   1.889 +
   1.890 +  if( p!=0 ){
   1.891 +    n = sqlite3_column_count(pStmt);
   1.892 +    if( N<n && N>=0 ){
   1.893 +      N += useType*n;
   1.894 +      sqlite3_mutex_enter(p->db->mutex);
   1.895 +      ret = xFunc(&p->aColName[N]);
   1.896 +
   1.897 +      /* A malloc may have failed inside of the xFunc() call. If this
   1.898 +      ** is the case, clear the mallocFailed flag and return NULL.
   1.899 +      */
   1.900 +      if( p->db && p->db->mallocFailed ){
   1.901 +        p->db->mallocFailed = 0;
   1.902 +        ret = 0;
   1.903 +      }
   1.904 +      sqlite3_mutex_leave(p->db->mutex);
   1.905 +    }
   1.906 +  }
   1.907 +  return ret;
   1.908 +}
   1.909 +
   1.910 +/*
   1.911 +** Return the name of the Nth column of the result set returned by SQL
   1.912 +** statement pStmt.
   1.913 +*/
   1.914 +SQLITE_EXPORT const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
   1.915 +  return columnName(
   1.916 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
   1.917 +}
   1.918 +#ifndef SQLITE_OMIT_UTF16
   1.919 +SQLITE_EXPORT const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
   1.920 +  return columnName(
   1.921 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
   1.922 +}
   1.923 +#endif
   1.924 +
   1.925 +/*
   1.926 +** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
   1.927 +** not define OMIT_DECLTYPE.
   1.928 +*/
   1.929 +#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
   1.930 +# error "Must not define both SQLITE_OMIT_DECLTYPE \
   1.931 +         and SQLITE_ENABLE_COLUMN_METADATA"
   1.932 +#endif
   1.933 +
   1.934 +#ifndef SQLITE_OMIT_DECLTYPE
   1.935 +/*
   1.936 +** Return the column declaration type (if applicable) of the 'i'th column
   1.937 +** of the result set of SQL statement pStmt.
   1.938 +*/
   1.939 +SQLITE_EXPORT const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
   1.940 +  return columnName(
   1.941 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
   1.942 +}
   1.943 +#ifndef SQLITE_OMIT_UTF16
   1.944 +SQLITE_EXPORT const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
   1.945 +  return columnName(
   1.946 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
   1.947 +}
   1.948 +#endif /* SQLITE_OMIT_UTF16 */
   1.949 +#endif /* SQLITE_OMIT_DECLTYPE */
   1.950 +
   1.951 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
   1.952 +/*
   1.953 +** Return the name of the database from which a result column derives.
   1.954 +** NULL is returned if the result column is an expression or constant or
   1.955 +** anything else which is not an unabiguous reference to a database column.
   1.956 +*/
   1.957 +const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
   1.958 +  return columnName(
   1.959 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
   1.960 +}
   1.961 +#ifndef SQLITE_OMIT_UTF16
   1.962 +const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
   1.963 +  return columnName(
   1.964 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
   1.965 +}
   1.966 +#endif /* SQLITE_OMIT_UTF16 */
   1.967 +
   1.968 +/*
   1.969 +** Return the name of the table from which a result column derives.
   1.970 +** NULL is returned if the result column is an expression or constant or
   1.971 +** anything else which is not an unabiguous reference to a database column.
   1.972 +*/
   1.973 +const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
   1.974 +  return columnName(
   1.975 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
   1.976 +}
   1.977 +#ifndef SQLITE_OMIT_UTF16
   1.978 +const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
   1.979 +  return columnName(
   1.980 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
   1.981 +}
   1.982 +#endif /* SQLITE_OMIT_UTF16 */
   1.983 +
   1.984 +/*
   1.985 +** Return the name of the table column from which a result column derives.
   1.986 +** NULL is returned if the result column is an expression or constant or
   1.987 +** anything else which is not an unabiguous reference to a database column.
   1.988 +*/
   1.989 +const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
   1.990 +  return columnName(
   1.991 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
   1.992 +}
   1.993 +#ifndef SQLITE_OMIT_UTF16
   1.994 +const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
   1.995 +  return columnName(
   1.996 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
   1.997 +}
   1.998 +#endif /* SQLITE_OMIT_UTF16 */
   1.999 +#endif /* SQLITE_ENABLE_COLUMN_METADATA */
  1.1000 +
  1.1001 +
  1.1002 +/******************************* sqlite3_bind_  ***************************
  1.1003 +** 
  1.1004 +** Routines used to attach values to wildcards in a compiled SQL statement.
  1.1005 +*/
  1.1006 +/*
  1.1007 +** Unbind the value bound to variable i in virtual machine p. This is the 
  1.1008 +** the same as binding a NULL value to the column. If the "i" parameter is
  1.1009 +** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
  1.1010 +**
  1.1011 +** The error code stored in database p->db is overwritten with the return
  1.1012 +** value in any case.
  1.1013 +*/
  1.1014 +static int vdbeUnbind(Vdbe *p, int i){
  1.1015 +  Mem *pVar;
  1.1016 +  if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
  1.1017 +    if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
  1.1018 +    return SQLITE_MISUSE;
  1.1019 +  }
  1.1020 +  if( i<1 || i>p->nVar ){
  1.1021 +    sqlite3Error(p->db, SQLITE_RANGE, 0);
  1.1022 +    return SQLITE_RANGE;
  1.1023 +  }
  1.1024 +  i--;
  1.1025 +  pVar = &p->aVar[i];
  1.1026 +  sqlite3VdbeMemRelease(pVar);
  1.1027 +  pVar->flags = MEM_Null;
  1.1028 +  sqlite3Error(p->db, SQLITE_OK, 0);
  1.1029 +  return SQLITE_OK;
  1.1030 +}
  1.1031 +
  1.1032 +/*
  1.1033 +** Bind a text or BLOB value.
  1.1034 +*/
  1.1035 +static int bindText(
  1.1036 +  sqlite3_stmt *pStmt,   /* The statement to bind against */
  1.1037 +  int i,                 /* Index of the parameter to bind */
  1.1038 +  const void *zData,     /* Pointer to the data to be bound */
  1.1039 +  int nData,             /* Number of bytes of data to be bound */
  1.1040 +  void (*xDel)(void*),   /* Destructor for the data */
  1.1041 +  int encoding           /* Encoding for the data */
  1.1042 +){
  1.1043 +  Vdbe *p = (Vdbe *)pStmt;
  1.1044 +  Mem *pVar;
  1.1045 +  int rc;
  1.1046 +
  1.1047 +  if( p==0 ){
  1.1048 +    return SQLITE_MISUSE;
  1.1049 +  }
  1.1050 +  sqlite3_mutex_enter(p->db->mutex);
  1.1051 +  rc = vdbeUnbind(p, i);
  1.1052 +  if( rc==SQLITE_OK && zData!=0 ){
  1.1053 +    pVar = &p->aVar[i-1];
  1.1054 +    rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
  1.1055 +    if( rc==SQLITE_OK && encoding!=0 ){
  1.1056 +      rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
  1.1057 +    }
  1.1058 +    sqlite3Error(p->db, rc, 0);
  1.1059 +    rc = sqlite3ApiExit(p->db, rc);
  1.1060 +  }
  1.1061 +  sqlite3_mutex_leave(p->db->mutex);
  1.1062 +  return rc;
  1.1063 +}
  1.1064 +
  1.1065 +
  1.1066 +/*
  1.1067 +** Bind a blob value to an SQL statement variable.
  1.1068 +*/
  1.1069 +SQLITE_EXPORT int sqlite3_bind_blob(
  1.1070 +  sqlite3_stmt *pStmt, 
  1.1071 +  int i, 
  1.1072 +  const void *zData, 
  1.1073 +  int nData, 
  1.1074 +  void (*xDel)(void*)
  1.1075 +){
  1.1076 +  return bindText(pStmt, i, zData, nData, xDel, 0);
  1.1077 +}
  1.1078 +SQLITE_EXPORT int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
  1.1079 +  int rc;
  1.1080 +  Vdbe *p = (Vdbe *)pStmt;
  1.1081 +  sqlite3_mutex_enter(p->db->mutex);
  1.1082 +  rc = vdbeUnbind(p, i);
  1.1083 +  if( rc==SQLITE_OK ){
  1.1084 +    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
  1.1085 +  }
  1.1086 +  sqlite3_mutex_leave(p->db->mutex);
  1.1087 +  return rc;
  1.1088 +}
  1.1089 +SQLITE_EXPORT int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
  1.1090 +  return sqlite3_bind_int64(p, i, (i64)iValue);
  1.1091 +}
  1.1092 +SQLITE_EXPORT int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
  1.1093 +  int rc;
  1.1094 +  Vdbe *p = (Vdbe *)pStmt;
  1.1095 +  sqlite3_mutex_enter(p->db->mutex);
  1.1096 +  rc = vdbeUnbind(p, i);
  1.1097 +  if( rc==SQLITE_OK ){
  1.1098 +    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
  1.1099 +  }
  1.1100 +  sqlite3_mutex_leave(p->db->mutex);
  1.1101 +  return rc;
  1.1102 +}
  1.1103 +SQLITE_EXPORT int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
  1.1104 +  int rc;
  1.1105 +  Vdbe *p = (Vdbe*)pStmt;
  1.1106 +  sqlite3_mutex_enter(p->db->mutex);
  1.1107 +  rc = vdbeUnbind(p, i);
  1.1108 +  sqlite3_mutex_leave(p->db->mutex);
  1.1109 +  return rc;
  1.1110 +}
  1.1111 +SQLITE_EXPORT int sqlite3_bind_text( 
  1.1112 +  sqlite3_stmt *pStmt, 
  1.1113 +  int i, 
  1.1114 +  const char *zData, 
  1.1115 +  int nData, 
  1.1116 +  void (*xDel)(void*)
  1.1117 +){
  1.1118 +  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
  1.1119 +}
  1.1120 +#ifndef SQLITE_OMIT_UTF16
  1.1121 +SQLITE_EXPORT int sqlite3_bind_text16(
  1.1122 +  sqlite3_stmt *pStmt, 
  1.1123 +  int i, 
  1.1124 +  const void *zData, 
  1.1125 +  int nData, 
  1.1126 +  void (*xDel)(void*)
  1.1127 +){
  1.1128 +  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
  1.1129 +}
  1.1130 +#endif /* SQLITE_OMIT_UTF16 */
  1.1131 +SQLITE_EXPORT int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
  1.1132 +  int rc;
  1.1133 +  Vdbe *p = (Vdbe *)pStmt;
  1.1134 +  sqlite3_mutex_enter(p->db->mutex);
  1.1135 +  rc = vdbeUnbind(p, i);
  1.1136 +  if( rc==SQLITE_OK ){
  1.1137 +    rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
  1.1138 +    if( rc==SQLITE_OK ){
  1.1139 +      rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db));
  1.1140 +    }
  1.1141 +  }
  1.1142 +  rc = sqlite3ApiExit(p->db, rc);
  1.1143 +  sqlite3_mutex_leave(p->db->mutex);
  1.1144 +  return rc;
  1.1145 +}
  1.1146 +SQLITE_EXPORT int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
  1.1147 +  int rc;
  1.1148 +  Vdbe *p = (Vdbe *)pStmt;
  1.1149 +  sqlite3_mutex_enter(p->db->mutex);
  1.1150 +  rc = vdbeUnbind(p, i);
  1.1151 +  if( rc==SQLITE_OK ){
  1.1152 +    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
  1.1153 +  }
  1.1154 +  sqlite3_mutex_leave(p->db->mutex);
  1.1155 +  return rc;
  1.1156 +}
  1.1157 +
  1.1158 +/*
  1.1159 +** Return the number of wildcards that can be potentially bound to.
  1.1160 +** This routine is added to support DBD::SQLite.  
  1.1161 +*/
  1.1162 +SQLITE_EXPORT int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
  1.1163 +  Vdbe *p = (Vdbe*)pStmt;
  1.1164 +  return p ? p->nVar : 0;
  1.1165 +}
  1.1166 +
  1.1167 +/*
  1.1168 +** Create a mapping from variable numbers to variable names
  1.1169 +** in the Vdbe.azVar[] array, if such a mapping does not already
  1.1170 +** exist.
  1.1171 +*/
  1.1172 +static void createVarMap(Vdbe *p){
  1.1173 +  if( !p->okVar ){
  1.1174 +    sqlite3_mutex_enter(p->db->mutex);
  1.1175 +    if( !p->okVar ){
  1.1176 +      int j;
  1.1177 +      Op *pOp;
  1.1178 +      for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
  1.1179 +        if( pOp->opcode==OP_Variable ){
  1.1180 +          assert( pOp->p1>0 && pOp->p1<=p->nVar );
  1.1181 +          p->azVar[pOp->p1-1] = pOp->p4.z;
  1.1182 +        }
  1.1183 +      }
  1.1184 +      p->okVar = 1;
  1.1185 +    }
  1.1186 +    sqlite3_mutex_leave(p->db->mutex);
  1.1187 +  }
  1.1188 +}
  1.1189 +
  1.1190 +/*
  1.1191 +** Return the name of a wildcard parameter.  Return NULL if the index
  1.1192 +** is out of range or if the wildcard is unnamed.
  1.1193 +**
  1.1194 +** The result is always UTF-8.
  1.1195 +*/
  1.1196 +SQLITE_EXPORT const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
  1.1197 +  Vdbe *p = (Vdbe*)pStmt;
  1.1198 +  if( p==0 || i<1 || i>p->nVar ){
  1.1199 +    return 0;
  1.1200 +  }
  1.1201 +  createVarMap(p);
  1.1202 +  return p->azVar[i-1];
  1.1203 +}
  1.1204 +
  1.1205 +/*
  1.1206 +** Given a wildcard parameter name, return the index of the variable
  1.1207 +** with that name.  If there is no variable with the given name,
  1.1208 +** return 0.
  1.1209 +*/
  1.1210 +SQLITE_EXPORT int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
  1.1211 +  Vdbe *p = (Vdbe*)pStmt;
  1.1212 +  int i;
  1.1213 +  if( p==0 ){
  1.1214 +    return 0;
  1.1215 +  }
  1.1216 +  createVarMap(p); 
  1.1217 +  if( zName ){
  1.1218 +    for(i=0; i<p->nVar; i++){
  1.1219 +      const char *z = p->azVar[i];
  1.1220 +      if( z && strcmp(z,zName)==0 ){
  1.1221 +        return i+1;
  1.1222 +      }
  1.1223 +    }
  1.1224 +  }
  1.1225 +  return 0;
  1.1226 +}
  1.1227 +
  1.1228 +/*
  1.1229 +** Transfer all bindings from the first statement over to the second.
  1.1230 +** If the two statements contain a different number of bindings, then
  1.1231 +** an SQLITE_ERROR is returned.
  1.1232 +*/
  1.1233 +int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
  1.1234 +  Vdbe *pFrom = (Vdbe*)pFromStmt;
  1.1235 +  Vdbe *pTo = (Vdbe*)pToStmt;
  1.1236 +  int i, rc = SQLITE_OK;
  1.1237 +  if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
  1.1238 +    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
  1.1239 +    || pTo->db!=pFrom->db ){
  1.1240 +    return SQLITE_MISUSE;
  1.1241 +  }
  1.1242 +  if( pFrom->nVar!=pTo->nVar ){
  1.1243 +    return SQLITE_ERROR;
  1.1244 +  }
  1.1245 +  sqlite3_mutex_enter(pTo->db->mutex);
  1.1246 +  for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
  1.1247 +    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
  1.1248 +  }
  1.1249 +  sqlite3_mutex_leave(pTo->db->mutex);
  1.1250 +  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
  1.1251 +  return rc;
  1.1252 +}
  1.1253 +
  1.1254 +/*
  1.1255 +** Deprecated external interface.  Internal/core SQLite code
  1.1256 +** should call sqlite3TransferBindings.
  1.1257 +*/
  1.1258 +SQLITE_EXPORT int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
  1.1259 +  return sqlite3TransferBindings(pFromStmt, pToStmt);
  1.1260 +}
  1.1261 +
  1.1262 +/*
  1.1263 +** Return the sqlite3* database handle to which the prepared statement given
  1.1264 +** in the argument belongs.  This is the same database handle that was
  1.1265 +** the first argument to the sqlite3_prepare() that was used to create
  1.1266 +** the statement in the first place.
  1.1267 +*/
  1.1268 +SQLITE_EXPORT sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
  1.1269 +  return pStmt ? ((Vdbe*)pStmt)->db : 0;
  1.1270 +}
  1.1271 +
  1.1272 +/*
  1.1273 +** Return a pointer to the next prepared statement after pStmt associated
  1.1274 +** with database connection pDb.  If pStmt is NULL, return the first
  1.1275 +** prepared statement for the database connection.  Return NULL if there
  1.1276 +** are no more.
  1.1277 +*/
  1.1278 +SQLITE_EXPORT sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
  1.1279 +  sqlite3_stmt *pNext;
  1.1280 +  sqlite3_mutex_enter(pDb->mutex);
  1.1281 +  if( pStmt==0 ){
  1.1282 +    pNext = (sqlite3_stmt*)pDb->pVdbe;
  1.1283 +  }else{
  1.1284 +    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
  1.1285 +  }
  1.1286 +  sqlite3_mutex_leave(pDb->mutex);
  1.1287 +  return pNext;
  1.1288 +}