xref: /sqlite-3.40.0/src/vdbeapi.c (revision 5360ad34)
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){
61dc8453fdSdanielk1977   return (const 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 
1591bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
1604f26d6c4Sdrh     return SQLITE_MISUSE;
1614f26d6c4Sdrh   }
16291b48aa1Sdrh   if( p->aborted ){
16391b48aa1Sdrh     return SQLITE_ABORT;
16491b48aa1Sdrh   }
165a21c6b6fSdanielk1977   if( p->pc<=0 && p->expired ){
166a21c6b6fSdanielk1977     if( p->rc==SQLITE_OK ){
167a21c6b6fSdanielk1977       p->rc = SQLITE_SCHEMA;
168a21c6b6fSdanielk1977     }
169a21c6b6fSdanielk1977     return SQLITE_ERROR;
170a21c6b6fSdanielk1977   }
1714f26d6c4Sdrh   db = p->db;
1724f26d6c4Sdrh   if( sqlite3SafetyOn(db) ){
1734f26d6c4Sdrh     p->rc = SQLITE_MISUSE;
1744f26d6c4Sdrh     return SQLITE_MISUSE;
1754f26d6c4Sdrh   }
1761d850a72Sdanielk1977   if( p->pc<0 ){
17719e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
178c16a03b5Sdrh     /* Invoke the trace callback if there is one
179c16a03b5Sdrh     */
18019e2d37fSdrh     if( db->xTrace && !db->init.busy ){
181c16a03b5Sdrh       assert( p->nOp>0 );
182c16a03b5Sdrh       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
183c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3!=0 );
184c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
185c16a03b5Sdrh       sqlite3SafetyOff(db);
186c16a03b5Sdrh       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
187c16a03b5Sdrh       if( sqlite3SafetyOn(db) ){
188c16a03b5Sdrh         p->rc = SQLITE_MISUSE;
189c16a03b5Sdrh         return SQLITE_MISUSE;
190c16a03b5Sdrh       }
191c16a03b5Sdrh     }
19219e2d37fSdrh     if( db->xProfile && !db->init.busy ){
19319e2d37fSdrh       double rNow;
19419e2d37fSdrh       sqlite3OsCurrentTime(&rNow);
19519e2d37fSdrh       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
19619e2d37fSdrh     }
19719e2d37fSdrh #endif
198c16a03b5Sdrh 
199c16a03b5Sdrh     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
200c16a03b5Sdrh     ** on in debugging mode.
201c16a03b5Sdrh     */
202c16a03b5Sdrh #ifdef SQLITE_DEBUG
203c16a03b5Sdrh     if( (db->flags & SQLITE_SqlTrace)!=0 ){
204c16a03b5Sdrh       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
205c16a03b5Sdrh     }
206c16a03b5Sdrh #endif /* SQLITE_DEBUG */
207c16a03b5Sdrh 
2081d850a72Sdanielk1977     db->activeVdbeCnt++;
2091d850a72Sdanielk1977     p->pc = 0;
2101d850a72Sdanielk1977   }
211b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
2124f26d6c4Sdrh   if( p->explain ){
2134f26d6c4Sdrh     rc = sqlite3VdbeList(p);
214b7f9164eSdrh   }else
215b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
216b7f9164eSdrh   {
2174f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
2184f26d6c4Sdrh   }
2194f26d6c4Sdrh 
2204f26d6c4Sdrh   if( sqlite3SafetyOff(db) ){
2214f26d6c4Sdrh     rc = SQLITE_MISUSE;
2224f26d6c4Sdrh   }
2234f26d6c4Sdrh 
22419e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
22519e2d37fSdrh   /* Invoke the profile callback if there is one
22619e2d37fSdrh   */
22719e2d37fSdrh   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
22819e2d37fSdrh     double rNow;
22919e2d37fSdrh     u64 elapseTime;
23019e2d37fSdrh 
23119e2d37fSdrh     sqlite3OsCurrentTime(&rNow);
23219e2d37fSdrh     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
23319e2d37fSdrh     assert( p->nOp>0 );
23419e2d37fSdrh     assert( p->aOp[p->nOp-1].opcode==OP_Noop );
23519e2d37fSdrh     assert( p->aOp[p->nOp-1].p3!=0 );
23619e2d37fSdrh     assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
23719e2d37fSdrh     db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
23819e2d37fSdrh   }
23919e2d37fSdrh #endif
24019e2d37fSdrh 
241130b9f40Sdrh   sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
2424f26d6c4Sdrh   return rc;
2434f26d6c4Sdrh }
2444f26d6c4Sdrh 
2454f26d6c4Sdrh /*
246eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
247eb2e176aSdrh ** pointer to it.
248eb2e176aSdrh */
249eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
250eb2e176aSdrh   assert( p && p->pFunc );
251eb2e176aSdrh   return p->pFunc->pUserData;
252eb2e176aSdrh }
253eb2e176aSdrh 
254eb2e176aSdrh /*
255eb2e176aSdrh ** Allocate or return the aggregate context for a user function.  A new
256eb2e176aSdrh ** context is allocated on the first call.  Subsequent calls return the
257eb2e176aSdrh ** same context that was returned on prior calls.
258eb2e176aSdrh */
259eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
260eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
261abfcea25Sdrh   Mem *pMem = p->pMem;
262a10a34b8Sdrh   if( (pMem->flags & MEM_Agg)==0 ){
263a10a34b8Sdrh     if( nByte==0 ){
264a10a34b8Sdrh       assert( pMem->flags==MEM_Null );
265a10a34b8Sdrh       pMem->z = 0;
266a10a34b8Sdrh     }else{
267abfcea25Sdrh       pMem->flags = MEM_Agg;
268*5360ad34Sdrh       pMem->xDel = sqlite3FreeX;
269abfcea25Sdrh       *(FuncDef**)&pMem->i = p->pFunc;
270eb2e176aSdrh       if( nByte<=NBFS ){
271abfcea25Sdrh         pMem->z = pMem->zShort;
272abfcea25Sdrh         memset(pMem->z, 0, nByte);
273eb2e176aSdrh       }else{
274abfcea25Sdrh         pMem->z = sqliteMalloc( nByte );
275eb2e176aSdrh       }
276eb2e176aSdrh     }
277a10a34b8Sdrh   }
278abfcea25Sdrh   return (void*)pMem->z;
279eb2e176aSdrh }
280eb2e176aSdrh 
281eb2e176aSdrh /*
282682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to
283682f68b0Sdanielk1977 ** the user-function defined by pCtx.
284682f68b0Sdanielk1977 */
285682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
286682f68b0Sdanielk1977   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
287682f68b0Sdanielk1977   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
288682f68b0Sdanielk1977     return 0;
289682f68b0Sdanielk1977   }
290f92c7ff7Sdrh   return pVdbeFunc->apAux[iArg].pAux;
291682f68b0Sdanielk1977 }
292682f68b0Sdanielk1977 
293682f68b0Sdanielk1977 /*
294682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th
295682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
296682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
297682f68b0Sdanielk1977 */
298682f68b0Sdanielk1977 void sqlite3_set_auxdata(
299682f68b0Sdanielk1977   sqlite3_context *pCtx,
300682f68b0Sdanielk1977   int iArg,
301682f68b0Sdanielk1977   void *pAux,
302682f68b0Sdanielk1977   void (*xDelete)(void*)
303682f68b0Sdanielk1977 ){
304682f68b0Sdanielk1977   struct AuxData *pAuxData;
305f92c7ff7Sdrh   VdbeFunc *pVdbeFunc;
306682f68b0Sdanielk1977   if( iArg<0 ) return;
307682f68b0Sdanielk1977 
308f92c7ff7Sdrh   pVdbeFunc = pCtx->pVdbeFunc;
309f92c7ff7Sdrh   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
310998da3a2Sdrh     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
311f92c7ff7Sdrh     pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
312998da3a2Sdrh     if( !pVdbeFunc ) return;
3130e3d7476Sdrh     memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
3140e3d7476Sdrh              sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
315998da3a2Sdrh     pVdbeFunc->nAux = iArg+1;
316998da3a2Sdrh     pVdbeFunc->pFunc = pCtx->pFunc;
317682f68b0Sdanielk1977   }
318682f68b0Sdanielk1977 
319f92c7ff7Sdrh   pAuxData = &pVdbeFunc->apAux[iArg];
320682f68b0Sdanielk1977   if( pAuxData->pAux && pAuxData->xDelete ){
321682f68b0Sdanielk1977     pAuxData->xDelete(pAuxData->pAux);
322682f68b0Sdanielk1977   }
323682f68b0Sdanielk1977   pAuxData->pAux = pAux;
324682f68b0Sdanielk1977   pAuxData->xDelete = xDelete;
325682f68b0Sdanielk1977 }
326682f68b0Sdanielk1977 
327682f68b0Sdanielk1977 /*
328eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
329eb2e176aSdrh ** called.
330eb2e176aSdrh **
331eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
332eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
333eb2e176aSdrh ** this source file.
334eb2e176aSdrh */
335eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
336eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
337abfcea25Sdrh   return p->pMem->n;
338eb2e176aSdrh }
339eb2e176aSdrh 
340eb2e176aSdrh /*
3414f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
3424f26d6c4Sdrh */
3434f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
3444f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3451bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
3464f26d6c4Sdrh }
3474f26d6c4Sdrh 
3484f26d6c4Sdrh /*
3494f26d6c4Sdrh ** Return the number of values available from the current row of the
3504f26d6c4Sdrh ** currently executing statement pStmt.
3514f26d6c4Sdrh */
3524f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
3534f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3541bcdb0c0Sdrh   if( pVm==0 || !pVm->resOnStack ) return 0;
3554f26d6c4Sdrh   return pVm->nResColumn;
3564f26d6c4Sdrh }
3574f26d6c4Sdrh 
3584f26d6c4Sdrh 
3594f26d6c4Sdrh /*
3604f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
3614f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
3624f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
3634f26d6c4Sdrh ** of NULL.
3644f26d6c4Sdrh */
3654f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
3664f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3674f26d6c4Sdrh   int vals = sqlite3_data_count(pStmt);
3684f26d6c4Sdrh   if( i>=vals || i<0 ){
3694f26d6c4Sdrh     static Mem nullMem;
3704f26d6c4Sdrh     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
3714f26d6c4Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
3724f26d6c4Sdrh     return &nullMem;
3734f26d6c4Sdrh   }
3744f26d6c4Sdrh   return &pVm->pTos[(1-vals)+i];
3754f26d6c4Sdrh }
3764f26d6c4Sdrh 
3774f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
3784f26d6c4Sdrh ** The following routines are used to access elements of the current row
3794f26d6c4Sdrh ** in the result set.
3804f26d6c4Sdrh */
381c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
382c572ef7fSdanielk1977   return sqlite3_value_blob( columnMem(pStmt,i) );
383c572ef7fSdanielk1977 }
3844f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
3854f26d6c4Sdrh   return sqlite3_value_bytes( columnMem(pStmt,i) );
3864f26d6c4Sdrh }
3874f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
3884f26d6c4Sdrh   return sqlite3_value_bytes16( columnMem(pStmt,i) );
3894f26d6c4Sdrh }
3904f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
3914f26d6c4Sdrh   return sqlite3_value_double( columnMem(pStmt,i) );
3924f26d6c4Sdrh }
3934f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
3944f26d6c4Sdrh   return sqlite3_value_int( columnMem(pStmt,i) );
3954f26d6c4Sdrh }
396efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
3974f26d6c4Sdrh   return sqlite3_value_int64( columnMem(pStmt,i) );
3984f26d6c4Sdrh }
3994f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
4004f26d6c4Sdrh   return sqlite3_value_text( columnMem(pStmt,i) );
4014f26d6c4Sdrh }
402d1e4733dSdrh #if 0
403d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
404d1e4733dSdrh   return columnMem(pStmt, i);
405d1e4733dSdrh }
406d1e4733dSdrh #endif
4076c62608fSdrh #ifndef SQLITE_OMIT_UTF16
4084f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
4094f26d6c4Sdrh   return sqlite3_value_text16( columnMem(pStmt,i) );
4104f26d6c4Sdrh }
4116c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
4124f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
4134f26d6c4Sdrh   return sqlite3_value_type( columnMem(pStmt,i) );
4144f26d6c4Sdrh }
4154f26d6c4Sdrh 
4165e2517e1Sdrh /*
4175e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
4185e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
419e590fbdeSdrh **
420e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
421e590fbdeSdrh ** name is returned.  Here are the names:
422e590fbdeSdrh **
423e590fbdeSdrh **    0      The column name as it should be displayed for output
424e590fbdeSdrh **    1      The datatype name for the column
425e590fbdeSdrh **    2      The name of the database that the column derives from
426e590fbdeSdrh **    3      The name of the table that the column derives from
427e590fbdeSdrh **    4      The name of the table column that the result column derives from
428e590fbdeSdrh **
429e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
430e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
4315e2517e1Sdrh */
4325e2517e1Sdrh static const void *columnName(
4335e2517e1Sdrh   sqlite3_stmt *pStmt,
4345e2517e1Sdrh   int N,
4355e2517e1Sdrh   const void *(*xFunc)(Mem*),
4365e2517e1Sdrh   int useType
4375e2517e1Sdrh ){
4385e2517e1Sdrh   Vdbe *p = (Vdbe *)pStmt;
4395e2517e1Sdrh   int n = sqlite3_column_count(pStmt);
4405e2517e1Sdrh 
4415e2517e1Sdrh   if( p==0 || N>=n || N<0 ){
4425e2517e1Sdrh     return 0;
4435e2517e1Sdrh   }
444e590fbdeSdrh   N += useType*n;
4455e2517e1Sdrh   return xFunc(&p->aColName[N]);
4465e2517e1Sdrh }
4475e2517e1Sdrh 
4484f26d6c4Sdrh 
4494f26d6c4Sdrh /*
4504f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
4514f26d6c4Sdrh ** statement pStmt.
4524f26d6c4Sdrh */
4534f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
4545e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
4554f26d6c4Sdrh }
456e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
457e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
458e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
459e590fbdeSdrh }
460e590fbdeSdrh #endif
4614f26d6c4Sdrh 
4624f26d6c4Sdrh /*
4636c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
464e590fbdeSdrh ** of the result set of SQL statement pStmt.
4656c62608fSdrh */
4666c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
4676c62608fSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
4686c62608fSdrh }
4696c62608fSdrh #ifndef SQLITE_OMIT_UTF16
47076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
4715e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
4724f26d6c4Sdrh }
4736c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
4744f26d6c4Sdrh 
475e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
476e590fbdeSdrh /*
477e590fbdeSdrh ** Return the name of the database from which a result column derives.
478e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
479e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
480e590fbdeSdrh */
481e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
482e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
483e590fbdeSdrh }
484e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
485e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
486e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
487e590fbdeSdrh }
488e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
489e590fbdeSdrh 
490e590fbdeSdrh /*
491e590fbdeSdrh ** Return the name of the table from which a result column derives.
492e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
493e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
494e590fbdeSdrh */
495e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
496e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
497e590fbdeSdrh }
498e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
499e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
500e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
501e590fbdeSdrh }
502e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
503e590fbdeSdrh 
504e590fbdeSdrh /*
505e590fbdeSdrh ** Return the name of the table column from which a result column derives.
506e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
507e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
508e590fbdeSdrh */
509e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
510e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
511e590fbdeSdrh }
512e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
513e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
514e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
515e590fbdeSdrh }
516e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
517e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */
518e590fbdeSdrh 
519e590fbdeSdrh 
520e590fbdeSdrh 
521e590fbdeSdrh 
5224f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
5234f26d6c4Sdrh **
5244f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
5254f26d6c4Sdrh */
5264f26d6c4Sdrh /*
5274f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
5284f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
5294f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
5304f26d6c4Sdrh **
5314f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
5324f26d6c4Sdrh ** value in any case.
5334f26d6c4Sdrh */
5344f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
5354f26d6c4Sdrh   Mem *pVar;
5361bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
537473d179dSdrh     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
5384f26d6c4Sdrh     return SQLITE_MISUSE;
5394f26d6c4Sdrh   }
5404f26d6c4Sdrh   if( i<1 || i>p->nVar ){
5414f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
5424f26d6c4Sdrh     return SQLITE_RANGE;
5434f26d6c4Sdrh   }
5444f26d6c4Sdrh   i--;
545290c1948Sdrh   pVar = &p->aVar[i];
546d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
5474f26d6c4Sdrh   pVar->flags = MEM_Null;
5484f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
5494f26d6c4Sdrh   return SQLITE_OK;
5504f26d6c4Sdrh }
5514f26d6c4Sdrh 
5524f26d6c4Sdrh /*
5535e2517e1Sdrh ** Bind a text or BLOB value.
5544f26d6c4Sdrh */
5555e2517e1Sdrh static int bindText(
556f4479501Sdrh   sqlite3_stmt *pStmt,
5574f26d6c4Sdrh   int i,
5584f26d6c4Sdrh   const void *zData,
5594f26d6c4Sdrh   int nData,
5605e2517e1Sdrh   void (*xDel)(void*),
5615e2517e1Sdrh   int encoding
5624f26d6c4Sdrh ){
5634f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5644f26d6c4Sdrh   Mem *pVar;
5654f26d6c4Sdrh   int rc;
5664f26d6c4Sdrh 
5674f26d6c4Sdrh   rc = vdbeUnbind(p, i);
568feac5f8dSdrh   if( rc || zData==0 ){
5694f26d6c4Sdrh     return rc;
5704f26d6c4Sdrh   }
571290c1948Sdrh   pVar = &p->aVar[i-1];
5725e2517e1Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
5735e2517e1Sdrh   if( rc ){
5744f26d6c4Sdrh     return rc;
5754f26d6c4Sdrh   }
5765e2517e1Sdrh   if( rc==SQLITE_OK && encoding!=0 ){
5775e2517e1Sdrh     rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
5785e2517e1Sdrh   }
5795e2517e1Sdrh   return rc;
5805e2517e1Sdrh }
5815e2517e1Sdrh 
5825e2517e1Sdrh 
5835e2517e1Sdrh /*
5845e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
5855e2517e1Sdrh */
5865e2517e1Sdrh int sqlite3_bind_blob(
5875e2517e1Sdrh   sqlite3_stmt *pStmt,
5885e2517e1Sdrh   int i,
5895e2517e1Sdrh   const void *zData,
5905e2517e1Sdrh   int nData,
5915e2517e1Sdrh   void (*xDel)(void*)
5925e2517e1Sdrh ){
5935e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
5945e2517e1Sdrh }
5954f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
5964f26d6c4Sdrh   int rc;
5974f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5984f26d6c4Sdrh   rc = vdbeUnbind(p, i);
5994f26d6c4Sdrh   if( rc==SQLITE_OK ){
600290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
6014f26d6c4Sdrh   }
602f4618891Sdanielk1977   return rc;
6034f26d6c4Sdrh }
6044f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
605efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
6064f26d6c4Sdrh }
607efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
6084f26d6c4Sdrh   int rc;
6094f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
6104f26d6c4Sdrh   rc = vdbeUnbind(p, i);
6114f26d6c4Sdrh   if( rc==SQLITE_OK ){
612290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
6134f26d6c4Sdrh   }
6144f26d6c4Sdrh   return rc;
6154f26d6c4Sdrh }
6164f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){
6174f26d6c4Sdrh   return vdbeUnbind((Vdbe *)p, i);
6184f26d6c4Sdrh }
6194f26d6c4Sdrh int sqlite3_bind_text(
6204f26d6c4Sdrh   sqlite3_stmt *pStmt,
6214f26d6c4Sdrh   int i,
6224f26d6c4Sdrh   const char *zData,
6234f26d6c4Sdrh   int nData,
624d8123366Sdanielk1977   void (*xDel)(void*)
6254f26d6c4Sdrh ){
6265e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
6274f26d6c4Sdrh }
6286c62608fSdrh #ifndef SQLITE_OMIT_UTF16
6294f26d6c4Sdrh int sqlite3_bind_text16(
6304f26d6c4Sdrh   sqlite3_stmt *pStmt,
6314f26d6c4Sdrh   int i,
6324f26d6c4Sdrh   const void *zData,
6334f26d6c4Sdrh   int nData,
634d8123366Sdanielk1977   void (*xDel)(void*)
6354f26d6c4Sdrh ){
6365e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
6374f26d6c4Sdrh }
6386c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
63975f6a032Sdrh 
64075f6a032Sdrh /*
64175f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
64275f6a032Sdrh ** This routine is added to support DBD::SQLite.
64375f6a032Sdrh */
64475f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
64592febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
64692febd92Sdrh   return p ? p->nVar : 0;
64775f6a032Sdrh }
648895d7472Sdrh 
649895d7472Sdrh /*
650fa6bc000Sdrh ** Create a mapping from variable numbers to variable names
651fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already
652fa6bc000Sdrh ** exist.
653895d7472Sdrh */
654fa6bc000Sdrh static void createVarMap(Vdbe *p){
655895d7472Sdrh   if( !p->okVar ){
656895d7472Sdrh     int j;
657895d7472Sdrh     Op *pOp;
658895d7472Sdrh     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
659895d7472Sdrh       if( pOp->opcode==OP_Variable ){
660895d7472Sdrh         assert( pOp->p1>0 && pOp->p1<=p->nVar );
661895d7472Sdrh         p->azVar[pOp->p1-1] = pOp->p3;
662895d7472Sdrh       }
663895d7472Sdrh     }
664895d7472Sdrh     p->okVar = 1;
665895d7472Sdrh   }
666fa6bc000Sdrh }
667fa6bc000Sdrh 
668fa6bc000Sdrh /*
669fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
670fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
671fa6bc000Sdrh **
672fa6bc000Sdrh ** The result is always UTF-8.
673fa6bc000Sdrh */
674fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
675fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
676fa6bc000Sdrh   if( p==0 || i<1 || i>p->nVar ){
677fa6bc000Sdrh     return 0;
678fa6bc000Sdrh   }
679fa6bc000Sdrh   createVarMap(p);
680895d7472Sdrh   return p->azVar[i-1];
681895d7472Sdrh }
682fa6bc000Sdrh 
683fa6bc000Sdrh /*
684fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
685fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
686fa6bc000Sdrh ** return 0.
687fa6bc000Sdrh */
688fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
689fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
690fa6bc000Sdrh   int i;
691fa6bc000Sdrh   if( p==0 ){
692fa6bc000Sdrh     return 0;
693fa6bc000Sdrh   }
694fa6bc000Sdrh   createVarMap(p);
6956a8903c3Sdrh   if( zName ){
696fa6bc000Sdrh     for(i=0; i<p->nVar; i++){
697971a7c87Sdrh       const char *z = p->azVar[i];
6986a8903c3Sdrh       if( z && strcmp(z,zName)==0 ){
699fa6bc000Sdrh         return i+1;
700fa6bc000Sdrh       }
701fa6bc000Sdrh     }
7026a8903c3Sdrh   }
703fa6bc000Sdrh   return 0;
704fa6bc000Sdrh }
705f8db1bc0Sdrh 
70651942bc3Sdrh /*
707f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
708f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then
709f8db1bc0Sdrh ** an SQLITE_ERROR is returned.
710f8db1bc0Sdrh */
711f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
712f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
713f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
714f8db1bc0Sdrh   int i, rc = SQLITE_OK;
715f8db1bc0Sdrh   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
716f8db1bc0Sdrh     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
717f8db1bc0Sdrh     return SQLITE_MISUSE;
718f8db1bc0Sdrh   }
719f8db1bc0Sdrh   if( pFrom->nVar!=pTo->nVar ){
720f8db1bc0Sdrh     return SQLITE_ERROR;
721f8db1bc0Sdrh   }
722f8db1bc0Sdrh   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
723f8db1bc0Sdrh     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
724f8db1bc0Sdrh   }
725f8db1bc0Sdrh   return rc;
726f8db1bc0Sdrh }
72751942bc3Sdrh 
72851942bc3Sdrh /*
72951942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
73051942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
73151942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
73251942bc3Sdrh ** the statement in the first place.
73351942bc3Sdrh */
73451942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
73551942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
73651942bc3Sdrh }
737