Back to index

lightning-sunbird  0.9+nobinonly
vdbeapi.c
Go to the documentation of this file.
00001 /*
00002 ** 2004 May 26
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 **
00013 ** This file contains code use to implement APIs that are part of the
00014 ** VDBE.
00015 */
00016 #include "sqliteInt.h"
00017 #include "vdbeInt.h"
00018 #include "os.h"
00019 
00020 /*
00021 ** Return TRUE (non-zero) of the statement supplied as an argument needs
00022 ** to be recompiled.  A statement needs to be recompiled whenever the
00023 ** execution environment changes in a way that would alter the program
00024 ** that sqlite3_prepare() generates.  For example, if new functions or
00025 ** collating sequences are registered or if an authorizer function is
00026 ** added or changed.
00027 */
00028 int sqlite3_expired(sqlite3_stmt *pStmt){
00029   Vdbe *p = (Vdbe*)pStmt;
00030   return p==0 || p->expired;
00031 }
00032 
00033 /**************************** sqlite3_value_  *******************************
00034 ** The following routines extract information from a Mem or sqlite3_value
00035 ** structure.
00036 */
00037 const void *sqlite3_value_blob(sqlite3_value *pVal){
00038   Mem *p = (Mem*)pVal;
00039   if( p->flags & (MEM_Blob|MEM_Str) ){
00040     return p->z;
00041   }else{
00042     return sqlite3_value_text(pVal);
00043   }
00044 }
00045 int sqlite3_value_bytes(sqlite3_value *pVal){
00046   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
00047 }
00048 int sqlite3_value_bytes16(sqlite3_value *pVal){
00049   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
00050 }
00051 double sqlite3_value_double(sqlite3_value *pVal){
00052   return sqlite3VdbeRealValue((Mem*)pVal);
00053 }
00054 int sqlite3_value_int(sqlite3_value *pVal){
00055   return sqlite3VdbeIntValue((Mem*)pVal);
00056 }
00057 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
00058   return sqlite3VdbeIntValue((Mem*)pVal);
00059 }
00060 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
00061   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
00062 }
00063 #ifndef SQLITE_OMIT_UTF16
00064 const void *sqlite3_value_text16(sqlite3_value* pVal){
00065   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
00066 }
00067 const void *sqlite3_value_text16be(sqlite3_value *pVal){
00068   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
00069 }
00070 const void *sqlite3_value_text16le(sqlite3_value *pVal){
00071   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
00072 }
00073 #endif /* SQLITE_OMIT_UTF16 */
00074 int sqlite3_value_type(sqlite3_value* pVal){
00075   return pVal->type;
00076 }
00077 /* sqlite3_value_numeric_type() defined in vdbe.c */
00078 
00079 /**************************** sqlite3_result_  *******************************
00080 ** The following routines are used by user-defined functions to specify
00081 ** the function result.
00082 */
00083 void sqlite3_result_blob(
00084   sqlite3_context *pCtx, 
00085   const void *z, 
00086   int n, 
00087   void (*xDel)(void *)
00088 ){
00089   assert( n>=0 );
00090   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
00091 }
00092 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
00093   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
00094 }
00095 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
00096   pCtx->isError = 1;
00097   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
00098 }
00099 #ifndef SQLITE_OMIT_UTF16
00100 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
00101   pCtx->isError = 1;
00102   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
00103 }
00104 #endif
00105 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
00106   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
00107 }
00108 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
00109   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
00110 }
00111 void sqlite3_result_null(sqlite3_context *pCtx){
00112   sqlite3VdbeMemSetNull(&pCtx->s);
00113 }
00114 void sqlite3_result_text(
00115   sqlite3_context *pCtx, 
00116   const char *z, 
00117   int n,
00118   void (*xDel)(void *)
00119 ){
00120   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
00121 }
00122 #ifndef SQLITE_OMIT_UTF16
00123 void sqlite3_result_text16(
00124   sqlite3_context *pCtx, 
00125   const void *z, 
00126   int n, 
00127   void (*xDel)(void *)
00128 ){
00129   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
00130 }
00131 void sqlite3_result_text16be(
00132   sqlite3_context *pCtx, 
00133   const void *z, 
00134   int n, 
00135   void (*xDel)(void *)
00136 ){
00137   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
00138 }
00139 void sqlite3_result_text16le(
00140   sqlite3_context *pCtx, 
00141   const void *z, 
00142   int n, 
00143   void (*xDel)(void *)
00144 ){
00145   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
00146 }
00147 #endif /* SQLITE_OMIT_UTF16 */
00148 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
00149   sqlite3VdbeMemCopy(&pCtx->s, pValue);
00150 }
00151 
00152 
00153 /*
00154 ** Execute the statement pStmt, either until a row of data is ready, the
00155 ** statement is completely executed or an error occurs.
00156 */
00157 int sqlite3_step(sqlite3_stmt *pStmt){
00158   Vdbe *p = (Vdbe*)pStmt;
00159   sqlite3 *db;
00160   int rc;
00161 
00162   /* Assert that malloc() has not failed */
00163   assert( !sqlite3MallocFailed() );
00164 
00165   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
00166     return SQLITE_MISUSE;
00167   }
00168   if( p->aborted ){
00169     return SQLITE_ABORT;
00170   }
00171   if( p->pc<=0 && p->expired ){
00172     if( p->rc==SQLITE_OK ){
00173       p->rc = SQLITE_SCHEMA;
00174     }
00175     return SQLITE_ERROR;
00176   }
00177   db = p->db;
00178   if( sqlite3SafetyOn(db) ){
00179     p->rc = SQLITE_MISUSE;
00180     return SQLITE_MISUSE;
00181   }
00182   if( p->pc<0 ){
00183 #ifndef SQLITE_OMIT_TRACE
00184     /* Invoke the trace callback if there is one
00185     */
00186     if( db->xTrace && !db->init.busy ){
00187       assert( p->nOp>0 );
00188       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
00189       assert( p->aOp[p->nOp-1].p3!=0 );
00190       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
00191       sqlite3SafetyOff(db);
00192       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
00193       if( sqlite3SafetyOn(db) ){
00194         p->rc = SQLITE_MISUSE;
00195         return SQLITE_MISUSE;
00196       }
00197     }
00198     if( db->xProfile && !db->init.busy ){
00199       double rNow;
00200       sqlite3OsCurrentTime(&rNow);
00201       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
00202     }
00203 #endif
00204 
00205     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
00206     ** on in debugging mode.
00207     */
00208 #ifdef SQLITE_DEBUG
00209     if( (db->flags & SQLITE_SqlTrace)!=0 ){
00210       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
00211     }
00212 #endif /* SQLITE_DEBUG */
00213 
00214     db->activeVdbeCnt++;
00215     p->pc = 0;
00216   }
00217 #ifndef SQLITE_OMIT_EXPLAIN
00218   if( p->explain ){
00219     rc = sqlite3VdbeList(p);
00220   }else
00221 #endif /* SQLITE_OMIT_EXPLAIN */
00222   {
00223     rc = sqlite3VdbeExec(p);
00224   }
00225 
00226   if( sqlite3SafetyOff(db) ){
00227     rc = SQLITE_MISUSE;
00228   }
00229 
00230 #ifndef SQLITE_OMIT_TRACE
00231   /* Invoke the profile callback if there is one
00232   */
00233   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
00234     double rNow;
00235     u64 elapseTime;
00236 
00237     sqlite3OsCurrentTime(&rNow);
00238     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
00239     assert( p->nOp>0 );
00240     assert( p->aOp[p->nOp-1].opcode==OP_Noop );
00241     assert( p->aOp[p->nOp-1].p3!=0 );
00242     assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
00243     db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
00244   }
00245 #endif
00246 
00247   sqlite3Error(p->db, rc, 0);
00248   p->rc = sqlite3ApiExit(p->db, p->rc);
00249   return rc;
00250 }
00251 
00252 /*
00253 ** Extract the user data from a sqlite3_context structure and return a
00254 ** pointer to it.
00255 */
00256 void *sqlite3_user_data(sqlite3_context *p){
00257   assert( p && p->pFunc );
00258   return p->pFunc->pUserData;
00259 }
00260 
00261 /*
00262 ** Allocate or return the aggregate context for a user function.  A new
00263 ** context is allocated on the first call.  Subsequent calls return the
00264 ** same context that was returned on prior calls.
00265 */
00266 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
00267   Mem *pMem = p->pMem;
00268   assert( p && p->pFunc && p->pFunc->xStep );
00269   if( (pMem->flags & MEM_Agg)==0 ){
00270     if( nByte==0 ){
00271       assert( pMem->flags==MEM_Null );
00272       pMem->z = 0;
00273     }else{
00274       pMem->flags = MEM_Agg;
00275       pMem->xDel = sqlite3FreeX;
00276       *(FuncDef**)&pMem->i = p->pFunc;
00277       if( nByte<=NBFS ){
00278         pMem->z = pMem->zShort;
00279         memset(pMem->z, 0, nByte);
00280       }else{
00281         pMem->z = sqliteMalloc( nByte );
00282       }
00283     }
00284   }
00285   return (void*)pMem->z;
00286 }
00287 
00288 /*
00289 ** Return the auxilary data pointer, if any, for the iArg'th argument to
00290 ** the user-function defined by pCtx.
00291 */
00292 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
00293   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
00294   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
00295     return 0;
00296   }
00297   return pVdbeFunc->apAux[iArg].pAux;
00298 }
00299 
00300 /*
00301 ** Set the auxilary data pointer and delete function, for the iArg'th
00302 ** argument to the user-function defined by pCtx. Any previous value is
00303 ** deleted by calling the delete function specified when it was set.
00304 */
00305 void sqlite3_set_auxdata(
00306   sqlite3_context *pCtx, 
00307   int iArg, 
00308   void *pAux, 
00309   void (*xDelete)(void*)
00310 ){
00311   struct AuxData *pAuxData;
00312   VdbeFunc *pVdbeFunc;
00313   if( iArg<0 ) return;
00314 
00315   pVdbeFunc = pCtx->pVdbeFunc;
00316   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
00317     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
00318     pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
00319     if( !pVdbeFunc ) return;
00320     pCtx->pVdbeFunc = pVdbeFunc;
00321     memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 
00322              sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
00323     pVdbeFunc->nAux = iArg+1;
00324     pVdbeFunc->pFunc = pCtx->pFunc;
00325   }
00326 
00327   pAuxData = &pVdbeFunc->apAux[iArg];
00328   if( pAuxData->pAux && pAuxData->xDelete ){
00329     pAuxData->xDelete(pAuxData->pAux);
00330   }
00331   pAuxData->pAux = pAux;
00332   pAuxData->xDelete = xDelete;
00333 }
00334 
00335 /*
00336 ** Return the number of times the Step function of a aggregate has been 
00337 ** called.
00338 **
00339 ** This function is deprecated.  Do not use it for new code.  It is
00340 ** provide only to avoid breaking legacy code.  New aggregate function
00341 ** implementations should keep their own counts within their aggregate
00342 ** context.
00343 */
00344 int sqlite3_aggregate_count(sqlite3_context *p){
00345   assert( p && p->pFunc && p->pFunc->xStep );
00346   return p->pMem->n;
00347 }
00348 
00349 /*
00350 ** Return the number of columns in the result set for the statement pStmt.
00351 */
00352 int sqlite3_column_count(sqlite3_stmt *pStmt){
00353   Vdbe *pVm = (Vdbe *)pStmt;
00354   return pVm ? pVm->nResColumn : 0;
00355 }
00356 
00357 /*
00358 ** Return the number of values available from the current row of the
00359 ** currently executing statement pStmt.
00360 */
00361 int sqlite3_data_count(sqlite3_stmt *pStmt){
00362   Vdbe *pVm = (Vdbe *)pStmt;
00363   if( pVm==0 || !pVm->resOnStack ) return 0;
00364   return pVm->nResColumn;
00365 }
00366 
00367 
00368 /*
00369 ** Check to see if column iCol of the given statement is valid.  If
00370 ** it is, return a pointer to the Mem for the value of that column.
00371 ** If iCol is not valid, return a pointer to a Mem which has a value
00372 ** of NULL.
00373 */
00374 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
00375   Vdbe *pVm = (Vdbe *)pStmt;
00376   int vals = sqlite3_data_count(pStmt);
00377   if( i>=vals || i<0 ){
00378     static Mem nullMem;
00379     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
00380     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
00381     return &nullMem;
00382   }
00383   return &pVm->pTos[(1-vals)+i];
00384 }
00385 
00386 /*
00387 ** This function is called after invoking an sqlite3_value_XXX function on a 
00388 ** column value (i.e. a value returned by evaluating an SQL expression in the
00389 ** select list of a SELECT statement) that may cause a malloc() failure. If 
00390 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
00391 ** code of statement pStmt set to SQLITE_NOMEM.
00392 **
00393 ** Specificly, this is called from within:
00394 **
00395 **     sqlite3_column_int()
00396 **     sqlite3_column_int64()
00397 **     sqlite3_column_text()
00398 **     sqlite3_column_text16()
00399 **     sqlite3_column_real()
00400 **     sqlite3_column_bytes()
00401 **     sqlite3_column_bytes16()
00402 **
00403 ** But not for sqlite3_column_blob(), which never calls malloc().
00404 */
00405 static void columnMallocFailure(sqlite3_stmt *pStmt)
00406 {
00407   /* If malloc() failed during an encoding conversion within an
00408   ** sqlite3_column_XXX API, then set the return code of the statement to
00409   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
00410   ** and _finalize() will return NOMEM.
00411   */
00412   Vdbe *p = (Vdbe *)pStmt;
00413   p->rc = sqlite3ApiExit(0, p->rc);
00414 }
00415 
00416 /**************************** sqlite3_column_  *******************************
00417 ** The following routines are used to access elements of the current row
00418 ** in the result set.
00419 */
00420 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
00421   const void *val;
00422   sqlite3MallocDisallow();
00423   val = sqlite3_value_blob( columnMem(pStmt,i) );
00424   sqlite3MallocAllow();
00425   return val;
00426 }
00427 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
00428   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
00429   columnMallocFailure(pStmt);
00430   return val;
00431 }
00432 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
00433   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
00434   columnMallocFailure(pStmt);
00435   return val;
00436 }
00437 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
00438   double val = sqlite3_value_double( columnMem(pStmt,i) );
00439   columnMallocFailure(pStmt);
00440   return val;
00441 }
00442 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
00443   int val = sqlite3_value_int( columnMem(pStmt,i) );
00444   columnMallocFailure(pStmt);
00445   return val;
00446 }
00447 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
00448   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
00449   columnMallocFailure(pStmt);
00450   return val;
00451 }
00452 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
00453   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
00454   columnMallocFailure(pStmt);
00455   return val;
00456 }
00457 #if 0
00458 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
00459   return columnMem(pStmt, i);
00460 }
00461 #endif
00462 #ifndef SQLITE_OMIT_UTF16
00463 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
00464   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
00465   columnMallocFailure(pStmt);
00466   return val;
00467 }
00468 #endif /* SQLITE_OMIT_UTF16 */
00469 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
00470   return sqlite3_value_type( columnMem(pStmt,i) );
00471 }
00472 
00473 /* The following function is experimental and subject to change or
00474 ** removal */
00475 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
00476 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
00477 **}
00478 */
00479 
00480 /*
00481 ** Convert the N-th element of pStmt->pColName[] into a string using
00482 ** xFunc() then return that string.  If N is out of range, return 0.
00483 **
00484 ** There are up to 5 names for each column.  useType determines which
00485 ** name is returned.  Here are the names:
00486 **
00487 **    0      The column name as it should be displayed for output
00488 **    1      The datatype name for the column
00489 **    2      The name of the database that the column derives from
00490 **    3      The name of the table that the column derives from
00491 **    4      The name of the table column that the result column derives from
00492 **
00493 ** If the result is not a simple column reference (if it is an expression
00494 ** or a constant) then useTypes 2, 3, and 4 return NULL.
00495 */
00496 static const void *columnName(
00497   sqlite3_stmt *pStmt,
00498   int N,
00499   const void *(*xFunc)(Mem*),
00500   int useType
00501 ){
00502   const void *ret;
00503   Vdbe *p = (Vdbe *)pStmt;
00504   int n = sqlite3_column_count(pStmt);
00505 
00506   if( p==0 || N>=n || N<0 ){
00507     return 0;
00508   }
00509   N += useType*n;
00510   ret = xFunc(&p->aColName[N]);
00511 
00512   /* A malloc may have failed inside of the xFunc() call. If this is the case,
00513   ** clear the mallocFailed flag and return NULL.
00514   */
00515   sqlite3ApiExit(0, 0);
00516   return ret;
00517 }
00518 
00519 /*
00520 ** Return the name of the Nth column of the result set returned by SQL
00521 ** statement pStmt.
00522 */
00523 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
00524   return columnName(
00525       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
00526 }
00527 #ifndef SQLITE_OMIT_UTF16
00528 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
00529   return columnName(
00530       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
00531 }
00532 #endif
00533 
00534 /*
00535 ** Return the column declaration type (if applicable) of the 'i'th column
00536 ** of the result set of SQL statement pStmt.
00537 */
00538 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
00539   return columnName(
00540       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
00541 }
00542 #ifndef SQLITE_OMIT_UTF16
00543 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
00544   return columnName(
00545       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
00546 }
00547 #endif /* SQLITE_OMIT_UTF16 */
00548 
00549 #ifdef SQLITE_ENABLE_COLUMN_METADATA
00550 /*
00551 ** Return the name of the database from which a result column derives.
00552 ** NULL is returned if the result column is an expression or constant or
00553 ** anything else which is not an unabiguous reference to a database column.
00554 */
00555 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
00556   return columnName(
00557       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
00558 }
00559 #ifndef SQLITE_OMIT_UTF16
00560 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
00561   return columnName(
00562       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
00563 }
00564 #endif /* SQLITE_OMIT_UTF16 */
00565 
00566 /*
00567 ** Return the name of the table from which a result column derives.
00568 ** NULL is returned if the result column is an expression or constant or
00569 ** anything else which is not an unabiguous reference to a database column.
00570 */
00571 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
00572   return columnName(
00573       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
00574 }
00575 #ifndef SQLITE_OMIT_UTF16
00576 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
00577   return columnName(
00578       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
00579 }
00580 #endif /* SQLITE_OMIT_UTF16 */
00581 
00582 /*
00583 ** Return the name of the table column from which a result column derives.
00584 ** NULL is returned if the result column is an expression or constant or
00585 ** anything else which is not an unabiguous reference to a database column.
00586 */
00587 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
00588   return columnName(
00589       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
00590 }
00591 #ifndef SQLITE_OMIT_UTF16
00592 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
00593   return columnName(
00594       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
00595 }
00596 #endif /* SQLITE_OMIT_UTF16 */
00597 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
00598 
00599 
00600 /******************************* sqlite3_bind_  ***************************
00601 ** 
00602 ** Routines used to attach values to wildcards in a compiled SQL statement.
00603 */
00604 /*
00605 ** Unbind the value bound to variable i in virtual machine p. This is the 
00606 ** the same as binding a NULL value to the column. If the "i" parameter is
00607 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
00608 **
00609 ** The error code stored in database p->db is overwritten with the return
00610 ** value in any case.
00611 */
00612 static int vdbeUnbind(Vdbe *p, int i){
00613   Mem *pVar;
00614   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
00615     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
00616     return SQLITE_MISUSE;
00617   }
00618   if( i<1 || i>p->nVar ){
00619     sqlite3Error(p->db, SQLITE_RANGE, 0);
00620     return SQLITE_RANGE;
00621   }
00622   i--;
00623   pVar = &p->aVar[i];
00624   sqlite3VdbeMemRelease(pVar);
00625   pVar->flags = MEM_Null;
00626   sqlite3Error(p->db, SQLITE_OK, 0);
00627   return SQLITE_OK;
00628 }
00629 
00630 /*
00631 ** Bind a text or BLOB value.
00632 */
00633 static int bindText(
00634   sqlite3_stmt *pStmt, 
00635   int i, 
00636   const void *zData, 
00637   int nData, 
00638   void (*xDel)(void*),
00639   int encoding
00640 ){
00641   Vdbe *p = (Vdbe *)pStmt;
00642   Mem *pVar;
00643   int rc;
00644 
00645   rc = vdbeUnbind(p, i);
00646   if( rc || zData==0 ){
00647     return rc;
00648   }
00649   pVar = &p->aVar[i-1];
00650   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
00651   if( rc==SQLITE_OK && encoding!=0 ){
00652     rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
00653   }
00654 
00655   sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
00656   return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
00657 }
00658 
00659 
00660 /*
00661 ** Bind a blob value to an SQL statement variable.
00662 */
00663 int sqlite3_bind_blob(
00664   sqlite3_stmt *pStmt, 
00665   int i, 
00666   const void *zData, 
00667   int nData, 
00668   void (*xDel)(void*)
00669 ){
00670   return bindText(pStmt, i, zData, nData, xDel, 0);
00671 }
00672 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
00673   int rc;
00674   Vdbe *p = (Vdbe *)pStmt;
00675   rc = vdbeUnbind(p, i);
00676   if( rc==SQLITE_OK ){
00677     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
00678   }
00679   return rc;
00680 }
00681 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
00682   return sqlite3_bind_int64(p, i, (i64)iValue);
00683 }
00684 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
00685   int rc;
00686   Vdbe *p = (Vdbe *)pStmt;
00687   rc = vdbeUnbind(p, i);
00688   if( rc==SQLITE_OK ){
00689     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
00690   }
00691   return rc;
00692 }
00693 int sqlite3_bind_null(sqlite3_stmt* p, int i){
00694   return vdbeUnbind((Vdbe *)p, i);
00695 }
00696 int sqlite3_bind_text( 
00697   sqlite3_stmt *pStmt, 
00698   int i, 
00699   const char *zData, 
00700   int nData, 
00701   void (*xDel)(void*)
00702 ){
00703   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
00704 }
00705 #ifndef SQLITE_OMIT_UTF16
00706 int sqlite3_bind_text16(
00707   sqlite3_stmt *pStmt, 
00708   int i, 
00709   const void *zData, 
00710   int nData, 
00711   void (*xDel)(void*)
00712 ){
00713   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
00714 }
00715 #endif /* SQLITE_OMIT_UTF16 */
00716 
00717 /*
00718 ** Return the number of wildcards that can be potentially bound to.
00719 ** This routine is added to support DBD::SQLite.  
00720 */
00721 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
00722   Vdbe *p = (Vdbe*)pStmt;
00723   return p ? p->nVar : 0;
00724 }
00725 
00726 /*
00727 ** Create a mapping from variable numbers to variable names
00728 ** in the Vdbe.azVar[] array, if such a mapping does not already
00729 ** exist.
00730 */
00731 static void createVarMap(Vdbe *p){
00732   if( !p->okVar ){
00733     int j;
00734     Op *pOp;
00735     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
00736       if( pOp->opcode==OP_Variable ){
00737         assert( pOp->p1>0 && pOp->p1<=p->nVar );
00738         p->azVar[pOp->p1-1] = pOp->p3;
00739       }
00740     }
00741     p->okVar = 1;
00742   }
00743 }
00744 
00745 /*
00746 ** Return the name of a wildcard parameter.  Return NULL if the index
00747 ** is out of range or if the wildcard is unnamed.
00748 **
00749 ** The result is always UTF-8.
00750 */
00751 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
00752   Vdbe *p = (Vdbe*)pStmt;
00753   if( p==0 || i<1 || i>p->nVar ){
00754     return 0;
00755   }
00756   createVarMap(p);
00757   return p->azVar[i-1];
00758 }
00759 
00760 /*
00761 ** Given a wildcard parameter name, return the index of the variable
00762 ** with that name.  If there is no variable with the given name,
00763 ** return 0.
00764 */
00765 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
00766   Vdbe *p = (Vdbe*)pStmt;
00767   int i;
00768   if( p==0 ){
00769     return 0;
00770   }
00771   createVarMap(p); 
00772   if( zName ){
00773     for(i=0; i<p->nVar; i++){
00774       const char *z = p->azVar[i];
00775       if( z && strcmp(z,zName)==0 ){
00776         return i+1;
00777       }
00778     }
00779   }
00780   return 0;
00781 }
00782 
00783 /*
00784 ** Given a wildcard parameter name, return the set of indexes of the
00785 ** variables with that name.  If there are no variables with the given
00786 ** name, return 0.  Otherwise, return the number of indexes returned
00787 ** in *pIndexes.  The array should be freed with
00788 ** sqlite3_free_parameter_indexes.
00789 */
00790 int sqlite3_bind_parameter_indexes(
00791     sqlite3_stmt *pStmt,
00792     const char *zName,
00793     int **pIndexes
00794 ){
00795   Vdbe *p = (Vdbe*)pStmt;
00796   int i, j, nVars, *indexes;
00797   if( p==0 ){
00798     return 0;
00799   }
00800   createVarMap(p);
00801   if( !zName )
00802     return 0;
00803   /* first count */
00804   nVars = 0;
00805   for(i=0; i<p->nVar; i++){
00806     const char *z = p->azVar[i];
00807     if( z && strcmp(z,zName)==0 ){
00808       nVars++;
00809     }
00810   }
00811   indexes = sqliteMalloc( sizeof(int) * nVars );
00812   j = 0;
00813   for(i=0; i<p->nVar; i++){
00814     const char *z = p->azVar[i];
00815     if( z && strcmp(z,zName)==0 )
00816       indexes[j++] = i+1;
00817   }
00818   *pIndexes = indexes;
00819   return nVars;
00820 }
00821 
00822 void sqlite3_free_parameter_indexes(int *pIndexes)
00823 {
00824   sqliteFree( pIndexes );
00825 }
00826 
00827 /*
00828 ** Transfer all bindings from the first statement over to the second.
00829 ** If the two statements contain a different number of bindings, then
00830 ** an SQLITE_ERROR is returned.
00831 */
00832 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
00833   Vdbe *pFrom = (Vdbe*)pFromStmt;
00834   Vdbe *pTo = (Vdbe*)pToStmt;
00835   int i, rc = SQLITE_OK;
00836   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
00837     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
00838     return SQLITE_MISUSE;
00839   }
00840   if( pFrom->nVar!=pTo->nVar ){
00841     return SQLITE_ERROR;
00842   }
00843   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
00844     sqlite3MallocDisallow();
00845     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
00846     sqlite3MallocAllow();
00847   }
00848   return rc;
00849 }
00850 
00851 /*
00852 ** Return the sqlite3* database handle to which the prepared statement given
00853 ** in the argument belongs.  This is the same database handle that was
00854 ** the first argument to the sqlite3_prepare() that was used to create
00855 ** the statement in the first place.
00856 */
00857 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
00858   return pStmt ? ((Vdbe*)pStmt)->db : 0;
00859 }