xref: /sqlite-3.40.0/src/vdbeapi.c (revision 950f054c)
14f26d6c4Sdrh /*
24f26d6c4Sdrh ** 2004 May 26
34f26d6c4Sdrh **
44f26d6c4Sdrh ** The author disclaims copyright to this source code.  In place of
54f26d6c4Sdrh ** a legal notice, here is a blessing:
64f26d6c4Sdrh **
74f26d6c4Sdrh **    May you do good and not evil.
84f26d6c4Sdrh **    May you find forgiveness for yourself and forgive others.
94f26d6c4Sdrh **    May you share freely, never taking more than you give.
104f26d6c4Sdrh **
114f26d6c4Sdrh *************************************************************************
124f26d6c4Sdrh **
134f26d6c4Sdrh ** This file contains code use to implement APIs that are part of the
144f26d6c4Sdrh ** VDBE.
154f26d6c4Sdrh */
164f26d6c4Sdrh #include "sqliteInt.h"
174f26d6c4Sdrh #include "vdbeInt.h"
1819e2d37fSdrh #include "os.h"
194f26d6c4Sdrh 
20d89bd007Sdrh /*
21d89bd007Sdrh ** Return TRUE (non-zero) of the statement supplied as an argument needs
22d89bd007Sdrh ** to be recompiled.  A statement needs to be recompiled whenever the
23d89bd007Sdrh ** execution environment changes in a way that would alter the program
24d89bd007Sdrh ** that sqlite3_prepare() generates.  For example, if new functions or
25d89bd007Sdrh ** collating sequences are registered or if an authorizer function is
26d89bd007Sdrh ** added or changed.
27d89bd007Sdrh */
28d89bd007Sdrh int sqlite3_expired(sqlite3_stmt *pStmt){
29d89bd007Sdrh   Vdbe *p = (Vdbe*)pStmt;
30d89bd007Sdrh   return p==0 || p->expired;
31d89bd007Sdrh }
32d89bd007Sdrh 
334f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
344f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
354f26d6c4Sdrh ** structure.
364f26d6c4Sdrh */
374f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
384f26d6c4Sdrh   Mem *p = (Mem*)pVal;
394f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
404f26d6c4Sdrh     return p->z;
414f26d6c4Sdrh   }else{
424f26d6c4Sdrh     return sqlite3_value_text(pVal);
434f26d6c4Sdrh   }
444f26d6c4Sdrh }
454f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
4613073931Sdanielk1977   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
474f26d6c4Sdrh }
484f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
4913073931Sdanielk1977   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
504f26d6c4Sdrh }
514f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
526a6124e2Sdrh   return sqlite3VdbeRealValue((Mem*)pVal);
534f26d6c4Sdrh }
544f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
556a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
564f26d6c4Sdrh }
57efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
586a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
594f26d6c4Sdrh }
604f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
6100fd957bSdanielk1977   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
624f26d6c4Sdrh }
636c62608fSdrh #ifndef SQLITE_OMIT_UTF16
644f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
65dc8453fdSdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
664f26d6c4Sdrh }
67d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){
68d8123366Sdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
69d8123366Sdanielk1977 }
70d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){
71d8123366Sdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
72d8123366Sdanielk1977 }
736c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
744f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
75f4479501Sdrh   return pVal->type;
764f26d6c4Sdrh }
774f26d6c4Sdrh 
784f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
794f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
804f26d6c4Sdrh ** the function result.
814f26d6c4Sdrh */
824f26d6c4Sdrh void sqlite3_result_blob(
834f26d6c4Sdrh   sqlite3_context *pCtx,
844f26d6c4Sdrh   const void *z,
854f26d6c4Sdrh   int n,
86d8123366Sdanielk1977   void (*xDel)(void *)
874f26d6c4Sdrh ){
885708d2deSdrh   assert( n>=0 );
89d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
904f26d6c4Sdrh }
914f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
924f26d6c4Sdrh   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
934f26d6c4Sdrh }
944f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
954f26d6c4Sdrh   pCtx->isError = 1;
96d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
974f26d6c4Sdrh }
984f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
994f26d6c4Sdrh   pCtx->isError = 1;
100d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
1014f26d6c4Sdrh }
102f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
1034f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
1044f26d6c4Sdrh }
1054f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
1064f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
1074f26d6c4Sdrh }
1084f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
109f4479501Sdrh   sqlite3VdbeMemSetNull(&pCtx->s);
1104f26d6c4Sdrh }
1114f26d6c4Sdrh void sqlite3_result_text(
1124f26d6c4Sdrh   sqlite3_context *pCtx,
1134f26d6c4Sdrh   const char *z,
1144f26d6c4Sdrh   int n,
115d8123366Sdanielk1977   void (*xDel)(void *)
1164f26d6c4Sdrh ){
117d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
1184f26d6c4Sdrh }
1196c62608fSdrh #ifndef SQLITE_OMIT_UTF16
1204f26d6c4Sdrh void sqlite3_result_text16(
1214f26d6c4Sdrh   sqlite3_context *pCtx,
1224f26d6c4Sdrh   const void *z,
1234f26d6c4Sdrh   int n,
124d8123366Sdanielk1977   void (*xDel)(void *)
1254f26d6c4Sdrh ){
126d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
127d8123366Sdanielk1977 }
128d8123366Sdanielk1977 void sqlite3_result_text16be(
129d8123366Sdanielk1977   sqlite3_context *pCtx,
130d8123366Sdanielk1977   const void *z,
131d8123366Sdanielk1977   int n,
132d8123366Sdanielk1977   void (*xDel)(void *)
133d8123366Sdanielk1977 ){
134d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
135d8123366Sdanielk1977 }
136d8123366Sdanielk1977 void sqlite3_result_text16le(
137d8123366Sdanielk1977   sqlite3_context *pCtx,
138d8123366Sdanielk1977   const void *z,
139d8123366Sdanielk1977   int n,
140d8123366Sdanielk1977   void (*xDel)(void *)
141d8123366Sdanielk1977 ){
142d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
1434f26d6c4Sdrh }
1446c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
1454f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
1464f26d6c4Sdrh   sqlite3VdbeMemCopy(&pCtx->s, pValue);
1474f26d6c4Sdrh }
1484f26d6c4Sdrh 
1494f26d6c4Sdrh 
1504f26d6c4Sdrh /*
1514f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
1524f26d6c4Sdrh ** statement is completely executed or an error occurs.
1534f26d6c4Sdrh */
1544f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
1554f26d6c4Sdrh   Vdbe *p = (Vdbe*)pStmt;
1569bb575fdSdrh   sqlite3 *db;
1574f26d6c4Sdrh   int rc;
1584f26d6c4Sdrh 
159efaaf579Sdanielk1977   /* Assert that malloc() has not failed */
1606f7adc8aSdrh   assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
161261919ccSdanielk1977 
1621bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
1634f26d6c4Sdrh     return SQLITE_MISUSE;
1644f26d6c4Sdrh   }
16591b48aa1Sdrh   if( p->aborted ){
16691b48aa1Sdrh     return SQLITE_ABORT;
16791b48aa1Sdrh   }
168a21c6b6fSdanielk1977   if( p->pc<=0 && p->expired ){
169a21c6b6fSdanielk1977     if( p->rc==SQLITE_OK ){
170a21c6b6fSdanielk1977       p->rc = SQLITE_SCHEMA;
171a21c6b6fSdanielk1977     }
172a21c6b6fSdanielk1977     return SQLITE_ERROR;
173a21c6b6fSdanielk1977   }
1744f26d6c4Sdrh   db = p->db;
1754f26d6c4Sdrh   if( sqlite3SafetyOn(db) ){
1764f26d6c4Sdrh     p->rc = SQLITE_MISUSE;
1774f26d6c4Sdrh     return SQLITE_MISUSE;
1784f26d6c4Sdrh   }
1791d850a72Sdanielk1977   if( p->pc<0 ){
18019e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
181c16a03b5Sdrh     /* Invoke the trace callback if there is one
182c16a03b5Sdrh     */
18319e2d37fSdrh     if( db->xTrace && !db->init.busy ){
184c16a03b5Sdrh       assert( p->nOp>0 );
185c16a03b5Sdrh       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
186c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3!=0 );
187c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
188c16a03b5Sdrh       sqlite3SafetyOff(db);
189c16a03b5Sdrh       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
190c16a03b5Sdrh       if( sqlite3SafetyOn(db) ){
191c16a03b5Sdrh         p->rc = SQLITE_MISUSE;
192c16a03b5Sdrh         return SQLITE_MISUSE;
193c16a03b5Sdrh       }
194c16a03b5Sdrh     }
19519e2d37fSdrh     if( db->xProfile && !db->init.busy ){
19619e2d37fSdrh       double rNow;
19766560adaSdrh       sqlite3OsCurrentTime(&rNow);
19819e2d37fSdrh       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
19919e2d37fSdrh     }
20019e2d37fSdrh #endif
201c16a03b5Sdrh 
202c16a03b5Sdrh     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
203c16a03b5Sdrh     ** on in debugging mode.
204c16a03b5Sdrh     */
205c16a03b5Sdrh #ifdef SQLITE_DEBUG
206c16a03b5Sdrh     if( (db->flags & SQLITE_SqlTrace)!=0 ){
207c16a03b5Sdrh       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
208c16a03b5Sdrh     }
209c16a03b5Sdrh #endif /* SQLITE_DEBUG */
210c16a03b5Sdrh 
2111d850a72Sdanielk1977     db->activeVdbeCnt++;
2121d850a72Sdanielk1977     p->pc = 0;
2131d850a72Sdanielk1977   }
214b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
2154f26d6c4Sdrh   if( p->explain ){
2164f26d6c4Sdrh     rc = sqlite3VdbeList(p);
217b7f9164eSdrh   }else
218b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
219b7f9164eSdrh   {
2204f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
2214f26d6c4Sdrh   }
2224f26d6c4Sdrh 
2234f26d6c4Sdrh   if( sqlite3SafetyOff(db) ){
2244f26d6c4Sdrh     rc = SQLITE_MISUSE;
2254f26d6c4Sdrh   }
2264f26d6c4Sdrh 
22719e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
22819e2d37fSdrh   /* Invoke the profile callback if there is one
22919e2d37fSdrh   */
23019e2d37fSdrh   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
23119e2d37fSdrh     double rNow;
23219e2d37fSdrh     u64 elapseTime;
23319e2d37fSdrh 
23466560adaSdrh     sqlite3OsCurrentTime(&rNow);
23519e2d37fSdrh     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
23619e2d37fSdrh     assert( p->nOp>0 );
23719e2d37fSdrh     assert( p->aOp[p->nOp-1].opcode==OP_Noop );
23819e2d37fSdrh     assert( p->aOp[p->nOp-1].p3!=0 );
23919e2d37fSdrh     assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
24019e2d37fSdrh     db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
24119e2d37fSdrh   }
24219e2d37fSdrh #endif
24319e2d37fSdrh 
244130b9f40Sdrh   sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
2452e588c75Sdanielk1977   sqlite3MallocClearFailed();
2464f26d6c4Sdrh   return rc;
2474f26d6c4Sdrh }
2484f26d6c4Sdrh 
2494f26d6c4Sdrh /*
250eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
251eb2e176aSdrh ** pointer to it.
252eb2e176aSdrh */
253eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
254eb2e176aSdrh   assert( p && p->pFunc );
255eb2e176aSdrh   return p->pFunc->pUserData;
256eb2e176aSdrh }
257eb2e176aSdrh 
258eb2e176aSdrh /*
259eb2e176aSdrh ** Allocate or return the aggregate context for a user function.  A new
260eb2e176aSdrh ** context is allocated on the first call.  Subsequent calls return the
261eb2e176aSdrh ** same context that was returned on prior calls.
262eb2e176aSdrh */
263eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
264abfcea25Sdrh   Mem *pMem = p->pMem;
265825c662eSdrh   assert( p && p->pFunc && p->pFunc->xStep );
266a10a34b8Sdrh   if( (pMem->flags & MEM_Agg)==0 ){
267a10a34b8Sdrh     if( nByte==0 ){
268a10a34b8Sdrh       assert( pMem->flags==MEM_Null );
269a10a34b8Sdrh       pMem->z = 0;
270a10a34b8Sdrh     }else{
271abfcea25Sdrh       pMem->flags = MEM_Agg;
2725360ad34Sdrh       pMem->xDel = sqlite3FreeX;
273abfcea25Sdrh       *(FuncDef**)&pMem->i = p->pFunc;
274eb2e176aSdrh       if( nByte<=NBFS ){
275abfcea25Sdrh         pMem->z = pMem->zShort;
276abfcea25Sdrh         memset(pMem->z, 0, nByte);
277eb2e176aSdrh       }else{
278abfcea25Sdrh         pMem->z = sqliteMalloc( nByte );
279eb2e176aSdrh       }
280eb2e176aSdrh     }
281a10a34b8Sdrh   }
282abfcea25Sdrh   return (void*)pMem->z;
283eb2e176aSdrh }
284eb2e176aSdrh 
285eb2e176aSdrh /*
286682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to
287682f68b0Sdanielk1977 ** the user-function defined by pCtx.
288682f68b0Sdanielk1977 */
289682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
290682f68b0Sdanielk1977   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
291682f68b0Sdanielk1977   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
292682f68b0Sdanielk1977     return 0;
293682f68b0Sdanielk1977   }
294f92c7ff7Sdrh   return pVdbeFunc->apAux[iArg].pAux;
295682f68b0Sdanielk1977 }
296682f68b0Sdanielk1977 
297682f68b0Sdanielk1977 /*
298682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th
299682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
300682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
301682f68b0Sdanielk1977 */
302682f68b0Sdanielk1977 void sqlite3_set_auxdata(
303682f68b0Sdanielk1977   sqlite3_context *pCtx,
304682f68b0Sdanielk1977   int iArg,
305682f68b0Sdanielk1977   void *pAux,
306682f68b0Sdanielk1977   void (*xDelete)(void*)
307682f68b0Sdanielk1977 ){
308682f68b0Sdanielk1977   struct AuxData *pAuxData;
309f92c7ff7Sdrh   VdbeFunc *pVdbeFunc;
310682f68b0Sdanielk1977   if( iArg<0 ) return;
311682f68b0Sdanielk1977 
312f92c7ff7Sdrh   pVdbeFunc = pCtx->pVdbeFunc;
313f92c7ff7Sdrh   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
314998da3a2Sdrh     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
31553f733c7Sdrh     pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
316998da3a2Sdrh     if( !pVdbeFunc ) return;
31753f733c7Sdrh     pCtx->pVdbeFunc = pVdbeFunc;
3180e3d7476Sdrh     memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
3190e3d7476Sdrh              sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
320998da3a2Sdrh     pVdbeFunc->nAux = iArg+1;
321998da3a2Sdrh     pVdbeFunc->pFunc = pCtx->pFunc;
322682f68b0Sdanielk1977   }
323682f68b0Sdanielk1977 
324f92c7ff7Sdrh   pAuxData = &pVdbeFunc->apAux[iArg];
325682f68b0Sdanielk1977   if( pAuxData->pAux && pAuxData->xDelete ){
326682f68b0Sdanielk1977     pAuxData->xDelete(pAuxData->pAux);
327682f68b0Sdanielk1977   }
328682f68b0Sdanielk1977   pAuxData->pAux = pAux;
329682f68b0Sdanielk1977   pAuxData->xDelete = xDelete;
330682f68b0Sdanielk1977 }
331682f68b0Sdanielk1977 
332682f68b0Sdanielk1977 /*
333eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
334eb2e176aSdrh ** called.
335eb2e176aSdrh **
336eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
337eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
338eb2e176aSdrh ** this source file.
339eb2e176aSdrh */
340eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
341eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
342abfcea25Sdrh   return p->pMem->n;
343eb2e176aSdrh }
344eb2e176aSdrh 
345eb2e176aSdrh /*
3464f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
3474f26d6c4Sdrh */
3484f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
3494f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3501bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
3514f26d6c4Sdrh }
3524f26d6c4Sdrh 
3534f26d6c4Sdrh /*
3544f26d6c4Sdrh ** Return the number of values available from the current row of the
3554f26d6c4Sdrh ** currently executing statement pStmt.
3564f26d6c4Sdrh */
3574f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
3584f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3591bcdb0c0Sdrh   if( pVm==0 || !pVm->resOnStack ) return 0;
3604f26d6c4Sdrh   return pVm->nResColumn;
3614f26d6c4Sdrh }
3624f26d6c4Sdrh 
3634f26d6c4Sdrh 
3644f26d6c4Sdrh /*
3654f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
3664f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
3674f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
3684f26d6c4Sdrh ** of NULL.
3694f26d6c4Sdrh */
3704f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
3714f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3724f26d6c4Sdrh   int vals = sqlite3_data_count(pStmt);
3734f26d6c4Sdrh   if( i>=vals || i<0 ){
3744f26d6c4Sdrh     static Mem nullMem;
3754f26d6c4Sdrh     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
3764f26d6c4Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
3774f26d6c4Sdrh     return &nullMem;
3784f26d6c4Sdrh   }
3794f26d6c4Sdrh   return &pVm->pTos[(1-vals)+i];
3804f26d6c4Sdrh }
3814f26d6c4Sdrh 
3822e588c75Sdanielk1977 /*
3832e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a
3842e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the
3852e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If
3862e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
3872e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM.
3882e588c75Sdanielk1977 **
3892e588c75Sdanielk1977 ** Specificly, this is called from within:
3902e588c75Sdanielk1977 **
3912e588c75Sdanielk1977 **     sqlite3_column_int()
3922e588c75Sdanielk1977 **     sqlite3_column_int64()
3932e588c75Sdanielk1977 **     sqlite3_column_text()
3942e588c75Sdanielk1977 **     sqlite3_column_text16()
3952e588c75Sdanielk1977 **     sqlite3_column_real()
3962e588c75Sdanielk1977 **     sqlite3_column_bytes()
3972e588c75Sdanielk1977 **     sqlite3_column_bytes16()
3982e588c75Sdanielk1977 **
3992e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc().
4002e588c75Sdanielk1977 */
4012e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt)
4022e588c75Sdanielk1977 {
4032e588c75Sdanielk1977   /* If malloc() failed during an encoding conversion within an
4042e588c75Sdanielk1977   ** sqlite3_column_XXX API, then set the return code of the statement to
4052e588c75Sdanielk1977   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
4062e588c75Sdanielk1977   ** and _finalize() will return NOMEM.
4072e588c75Sdanielk1977   */
4086f7adc8aSdrh   if( sqlite3ThreadDataReadOnly()->mallocFailed ){
4092e588c75Sdanielk1977     ((Vdbe *)pStmt)->rc = SQLITE_NOMEM;
4102e588c75Sdanielk1977     sqlite3MallocClearFailed();
4112e588c75Sdanielk1977   }
4122e588c75Sdanielk1977 }
4132e588c75Sdanielk1977 
4144f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
4154f26d6c4Sdrh ** The following routines are used to access elements of the current row
4164f26d6c4Sdrh ** in the result set.
4174f26d6c4Sdrh */
418c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
4192e588c75Sdanielk1977   const void *val;
4202e588c75Sdanielk1977   sqlite3MallocDisallow();
4212e588c75Sdanielk1977   val = sqlite3_value_blob( columnMem(pStmt,i) );
4222e588c75Sdanielk1977   sqlite3MallocAllow();
4232e588c75Sdanielk1977   return val;
424c572ef7fSdanielk1977 }
4254f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
4262e588c75Sdanielk1977   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
4272e588c75Sdanielk1977   columnMallocFailure(pStmt);
4282e588c75Sdanielk1977   return val;
4294f26d6c4Sdrh }
4304f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
4312e588c75Sdanielk1977   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
4322e588c75Sdanielk1977   columnMallocFailure(pStmt);
4332e588c75Sdanielk1977   return val;
4344f26d6c4Sdrh }
4354f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
4362e588c75Sdanielk1977   double val = sqlite3_value_double( columnMem(pStmt,i) );
4372e588c75Sdanielk1977   columnMallocFailure(pStmt);
4382e588c75Sdanielk1977   return val;
4394f26d6c4Sdrh }
4404f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
4412e588c75Sdanielk1977   int val = sqlite3_value_int( columnMem(pStmt,i) );
4422e588c75Sdanielk1977   columnMallocFailure(pStmt);
4432e588c75Sdanielk1977   return val;
4444f26d6c4Sdrh }
445efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
4462e588c75Sdanielk1977   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
4472e588c75Sdanielk1977   columnMallocFailure(pStmt);
4482e588c75Sdanielk1977   return val;
4494f26d6c4Sdrh }
4504f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
4512e588c75Sdanielk1977   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
4522e588c75Sdanielk1977   columnMallocFailure(pStmt);
4532e588c75Sdanielk1977   return val;
4544f26d6c4Sdrh }
455d1e4733dSdrh #if 0
456d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
457d1e4733dSdrh   return columnMem(pStmt, i);
458d1e4733dSdrh }
459d1e4733dSdrh #endif
4606c62608fSdrh #ifndef SQLITE_OMIT_UTF16
4614f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
4622e588c75Sdanielk1977   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
4632e588c75Sdanielk1977   columnMallocFailure(pStmt);
4642e588c75Sdanielk1977   return val;
4654f26d6c4Sdrh }
4666c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
4674f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
4684f26d6c4Sdrh   return sqlite3_value_type( columnMem(pStmt,i) );
4694f26d6c4Sdrh }
4704f26d6c4Sdrh 
4715e2517e1Sdrh /*
4725e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
4735e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
474e590fbdeSdrh **
475e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
476e590fbdeSdrh ** name is returned.  Here are the names:
477e590fbdeSdrh **
478e590fbdeSdrh **    0      The column name as it should be displayed for output
479e590fbdeSdrh **    1      The datatype name for the column
480e590fbdeSdrh **    2      The name of the database that the column derives from
481e590fbdeSdrh **    3      The name of the table that the column derives from
482e590fbdeSdrh **    4      The name of the table column that the result column derives from
483e590fbdeSdrh **
484e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
485e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
4865e2517e1Sdrh */
4875e2517e1Sdrh static const void *columnName(
4885e2517e1Sdrh   sqlite3_stmt *pStmt,
4895e2517e1Sdrh   int N,
4905e2517e1Sdrh   const void *(*xFunc)(Mem*),
4915e2517e1Sdrh   int useType
4925e2517e1Sdrh ){
49300fd957bSdanielk1977   const void *ret;
4945e2517e1Sdrh   Vdbe *p = (Vdbe *)pStmt;
4955e2517e1Sdrh   int n = sqlite3_column_count(pStmt);
4965e2517e1Sdrh 
4975e2517e1Sdrh   if( p==0 || N>=n || N<0 ){
4985e2517e1Sdrh     return 0;
4995e2517e1Sdrh   }
500e590fbdeSdrh   N += useType*n;
50100fd957bSdanielk1977   ret = xFunc(&p->aColName[N]);
50200fd957bSdanielk1977 
50300fd957bSdanielk1977   /* A malloc may have failed inside of the xFunc() call. If this is the case,
50400fd957bSdanielk1977   ** clear the mallocFailed flag and return NULL.
50500fd957bSdanielk1977   */
5062e588c75Sdanielk1977   sqlite3MallocClearFailed();
50700fd957bSdanielk1977   return ret;
5085e2517e1Sdrh }
5095e2517e1Sdrh 
5104f26d6c4Sdrh /*
5114f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
5124f26d6c4Sdrh ** statement pStmt.
5134f26d6c4Sdrh */
5144f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
5155e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
5164f26d6c4Sdrh }
517e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
518e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
519e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
520e590fbdeSdrh }
521e590fbdeSdrh #endif
5224f26d6c4Sdrh 
5234f26d6c4Sdrh /*
5246c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
525e590fbdeSdrh ** of the result set of SQL statement pStmt.
5266c62608fSdrh */
5276c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
5286c62608fSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
5296c62608fSdrh }
5306c62608fSdrh #ifndef SQLITE_OMIT_UTF16
53176d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
5325e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
5334f26d6c4Sdrh }
5346c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
5354f26d6c4Sdrh 
536e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
537e590fbdeSdrh /*
538e590fbdeSdrh ** Return the name of the database from which a result column derives.
539e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
540e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
541e590fbdeSdrh */
542e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
543e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
544e590fbdeSdrh }
545e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
546e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
547e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
548e590fbdeSdrh }
549e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
550e590fbdeSdrh 
551e590fbdeSdrh /*
552e590fbdeSdrh ** Return the name of the table from which a result column derives.
553e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
554e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
555e590fbdeSdrh */
556e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
557e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
558e590fbdeSdrh }
559e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
560e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
561e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
562e590fbdeSdrh }
563e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
564e590fbdeSdrh 
565e590fbdeSdrh /*
566e590fbdeSdrh ** Return the name of the table column from which a result column derives.
567e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
568e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
569e590fbdeSdrh */
570e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
571e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
572e590fbdeSdrh }
573e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
574e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
575e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
576e590fbdeSdrh }
577e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
578e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */
579e590fbdeSdrh 
580e590fbdeSdrh 
581e590fbdeSdrh 
582e590fbdeSdrh 
5834f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
5844f26d6c4Sdrh **
5854f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
5864f26d6c4Sdrh */
5874f26d6c4Sdrh /*
5884f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
5894f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
5904f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
5914f26d6c4Sdrh **
5924f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
5934f26d6c4Sdrh ** value in any case.
5944f26d6c4Sdrh */
5954f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
5964f26d6c4Sdrh   Mem *pVar;
5971bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
598473d179dSdrh     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
5994f26d6c4Sdrh     return SQLITE_MISUSE;
6004f26d6c4Sdrh   }
6014f26d6c4Sdrh   if( i<1 || i>p->nVar ){
6024f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
6034f26d6c4Sdrh     return SQLITE_RANGE;
6044f26d6c4Sdrh   }
6054f26d6c4Sdrh   i--;
606290c1948Sdrh   pVar = &p->aVar[i];
607d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
6084f26d6c4Sdrh   pVar->flags = MEM_Null;
6094f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
6104f26d6c4Sdrh   return SQLITE_OK;
6114f26d6c4Sdrh }
6124f26d6c4Sdrh 
6134f26d6c4Sdrh /*
6145e2517e1Sdrh ** Bind a text or BLOB value.
6154f26d6c4Sdrh */
6165e2517e1Sdrh static int bindText(
617f4479501Sdrh   sqlite3_stmt *pStmt,
6184f26d6c4Sdrh   int i,
6194f26d6c4Sdrh   const void *zData,
6204f26d6c4Sdrh   int nData,
6215e2517e1Sdrh   void (*xDel)(void*),
6225e2517e1Sdrh   int encoding
6234f26d6c4Sdrh ){
6244f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
6254f26d6c4Sdrh   Mem *pVar;
6264f26d6c4Sdrh   int rc;
6274f26d6c4Sdrh 
6284f26d6c4Sdrh   rc = vdbeUnbind(p, i);
629feac5f8dSdrh   if( rc || zData==0 ){
6304f26d6c4Sdrh     return rc;
6314f26d6c4Sdrh   }
632290c1948Sdrh   pVar = &p->aVar[i-1];
6335e2517e1Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
6345e2517e1Sdrh   if( rc==SQLITE_OK && encoding!=0 ){
63514db2665Sdanielk1977     rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
6365e2517e1Sdrh   }
637771151b6Sdanielk1977   sqlite3MallocClearFailed();
638771151b6Sdanielk1977   sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
6395e2517e1Sdrh   return rc;
6405e2517e1Sdrh }
6415e2517e1Sdrh 
6425e2517e1Sdrh 
6435e2517e1Sdrh /*
6445e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
6455e2517e1Sdrh */
6465e2517e1Sdrh int sqlite3_bind_blob(
6475e2517e1Sdrh   sqlite3_stmt *pStmt,
6485e2517e1Sdrh   int i,
6495e2517e1Sdrh   const void *zData,
6505e2517e1Sdrh   int nData,
6515e2517e1Sdrh   void (*xDel)(void*)
6525e2517e1Sdrh ){
6535e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
6545e2517e1Sdrh }
6554f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
6564f26d6c4Sdrh   int rc;
6574f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
6584f26d6c4Sdrh   rc = vdbeUnbind(p, i);
6594f26d6c4Sdrh   if( rc==SQLITE_OK ){
660290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
6614f26d6c4Sdrh   }
662f4618891Sdanielk1977   return rc;
6634f26d6c4Sdrh }
6644f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
665efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
6664f26d6c4Sdrh }
667efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
6684f26d6c4Sdrh   int rc;
6694f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
6704f26d6c4Sdrh   rc = vdbeUnbind(p, i);
6714f26d6c4Sdrh   if( rc==SQLITE_OK ){
672290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
6734f26d6c4Sdrh   }
6744f26d6c4Sdrh   return rc;
6754f26d6c4Sdrh }
6764f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){
6774f26d6c4Sdrh   return vdbeUnbind((Vdbe *)p, i);
6784f26d6c4Sdrh }
6794f26d6c4Sdrh int sqlite3_bind_text(
6804f26d6c4Sdrh   sqlite3_stmt *pStmt,
6814f26d6c4Sdrh   int i,
6824f26d6c4Sdrh   const char *zData,
6834f26d6c4Sdrh   int nData,
684d8123366Sdanielk1977   void (*xDel)(void*)
6854f26d6c4Sdrh ){
6865e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
6874f26d6c4Sdrh }
6886c62608fSdrh #ifndef SQLITE_OMIT_UTF16
6894f26d6c4Sdrh int sqlite3_bind_text16(
6904f26d6c4Sdrh   sqlite3_stmt *pStmt,
6914f26d6c4Sdrh   int i,
6924f26d6c4Sdrh   const void *zData,
6934f26d6c4Sdrh   int nData,
694d8123366Sdanielk1977   void (*xDel)(void*)
6954f26d6c4Sdrh ){
6965e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
6974f26d6c4Sdrh }
6986c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
69975f6a032Sdrh 
70075f6a032Sdrh /*
70175f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
70275f6a032Sdrh ** This routine is added to support DBD::SQLite.
70375f6a032Sdrh */
70475f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
70592febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
70692febd92Sdrh   return p ? p->nVar : 0;
70775f6a032Sdrh }
708895d7472Sdrh 
709895d7472Sdrh /*
710fa6bc000Sdrh ** Create a mapping from variable numbers to variable names
711fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already
712fa6bc000Sdrh ** exist.
713895d7472Sdrh */
714fa6bc000Sdrh static void createVarMap(Vdbe *p){
715895d7472Sdrh   if( !p->okVar ){
716895d7472Sdrh     int j;
717895d7472Sdrh     Op *pOp;
718895d7472Sdrh     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
719895d7472Sdrh       if( pOp->opcode==OP_Variable ){
720895d7472Sdrh         assert( pOp->p1>0 && pOp->p1<=p->nVar );
721895d7472Sdrh         p->azVar[pOp->p1-1] = pOp->p3;
722895d7472Sdrh       }
723895d7472Sdrh     }
724895d7472Sdrh     p->okVar = 1;
725895d7472Sdrh   }
726fa6bc000Sdrh }
727fa6bc000Sdrh 
728fa6bc000Sdrh /*
729fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
730fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
731fa6bc000Sdrh **
732fa6bc000Sdrh ** The result is always UTF-8.
733fa6bc000Sdrh */
734fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
735fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
736fa6bc000Sdrh   if( p==0 || i<1 || i>p->nVar ){
737fa6bc000Sdrh     return 0;
738fa6bc000Sdrh   }
739fa6bc000Sdrh   createVarMap(p);
740895d7472Sdrh   return p->azVar[i-1];
741895d7472Sdrh }
742fa6bc000Sdrh 
743fa6bc000Sdrh /*
744fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
745fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
746fa6bc000Sdrh ** return 0.
747fa6bc000Sdrh */
748fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
749fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
750fa6bc000Sdrh   int i;
751fa6bc000Sdrh   if( p==0 ){
752fa6bc000Sdrh     return 0;
753fa6bc000Sdrh   }
754fa6bc000Sdrh   createVarMap(p);
7556a8903c3Sdrh   if( zName ){
756fa6bc000Sdrh     for(i=0; i<p->nVar; i++){
757971a7c87Sdrh       const char *z = p->azVar[i];
7586a8903c3Sdrh       if( z && strcmp(z,zName)==0 ){
759fa6bc000Sdrh         return i+1;
760fa6bc000Sdrh       }
761fa6bc000Sdrh     }
7626a8903c3Sdrh   }
763fa6bc000Sdrh   return 0;
764fa6bc000Sdrh }
765f8db1bc0Sdrh 
76651942bc3Sdrh /*
767f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
768f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then
769f8db1bc0Sdrh ** an SQLITE_ERROR is returned.
770f8db1bc0Sdrh */
771f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
772f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
773f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
774f8db1bc0Sdrh   int i, rc = SQLITE_OK;
775f8db1bc0Sdrh   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
776f8db1bc0Sdrh     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
777f8db1bc0Sdrh     return SQLITE_MISUSE;
778f8db1bc0Sdrh   }
779f8db1bc0Sdrh   if( pFrom->nVar!=pTo->nVar ){
780f8db1bc0Sdrh     return SQLITE_ERROR;
781f8db1bc0Sdrh   }
782f8db1bc0Sdrh   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
783*950f054cSdanielk1977     sqlite3MallocDisallow();
784f8db1bc0Sdrh     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
785*950f054cSdanielk1977     sqlite3MallocAllow();
786f8db1bc0Sdrh   }
787f8db1bc0Sdrh   return rc;
788f8db1bc0Sdrh }
78951942bc3Sdrh 
79051942bc3Sdrh /*
79151942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
79251942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
79351942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
79451942bc3Sdrh ** the statement in the first place.
79551942bc3Sdrh */
79651942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
79751942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
79851942bc3Sdrh }
799