xref: /sqlite-3.40.0/src/vdbeapi.c (revision 130b9f40)
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"
184f26d6c4Sdrh 
19d89bd007Sdrh /*
20d89bd007Sdrh ** Return TRUE (non-zero) of the statement supplied as an argument needs
21d89bd007Sdrh ** to be recompiled.  A statement needs to be recompiled whenever the
22d89bd007Sdrh ** execution environment changes in a way that would alter the program
23d89bd007Sdrh ** that sqlite3_prepare() generates.  For example, if new functions or
24d89bd007Sdrh ** collating sequences are registered or if an authorizer function is
25d89bd007Sdrh ** added or changed.
26d89bd007Sdrh */
27d89bd007Sdrh int sqlite3_expired(sqlite3_stmt *pStmt){
28d89bd007Sdrh   Vdbe *p = (Vdbe*)pStmt;
29d89bd007Sdrh   return p==0 || p->expired;
30d89bd007Sdrh }
31d89bd007Sdrh 
324f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
334f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
344f26d6c4Sdrh ** structure.
354f26d6c4Sdrh */
364f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
374f26d6c4Sdrh   Mem *p = (Mem*)pVal;
384f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
394f26d6c4Sdrh     return p->z;
404f26d6c4Sdrh   }else{
414f26d6c4Sdrh     return sqlite3_value_text(pVal);
424f26d6c4Sdrh   }
434f26d6c4Sdrh }
444f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
4513073931Sdanielk1977   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
464f26d6c4Sdrh }
474f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
4813073931Sdanielk1977   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
494f26d6c4Sdrh }
504f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
516a6124e2Sdrh   return sqlite3VdbeRealValue((Mem*)pVal);
524f26d6c4Sdrh }
534f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
546a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
554f26d6c4Sdrh }
56efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
576a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
584f26d6c4Sdrh }
594f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
60dc8453fdSdanielk1977   return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
614f26d6c4Sdrh }
626c62608fSdrh #ifndef SQLITE_OMIT_UTF16
634f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
64dc8453fdSdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
654f26d6c4Sdrh }
66d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){
67d8123366Sdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
68d8123366Sdanielk1977 }
69d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){
70d8123366Sdanielk1977   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
71d8123366Sdanielk1977 }
726c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
734f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
74f4479501Sdrh   return pVal->type;
754f26d6c4Sdrh }
764f26d6c4Sdrh 
774f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
784f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
794f26d6c4Sdrh ** the function result.
804f26d6c4Sdrh */
814f26d6c4Sdrh void sqlite3_result_blob(
824f26d6c4Sdrh   sqlite3_context *pCtx,
834f26d6c4Sdrh   const void *z,
844f26d6c4Sdrh   int n,
85d8123366Sdanielk1977   void (*xDel)(void *)
864f26d6c4Sdrh ){
875708d2deSdrh   assert( n>=0 );
88d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
894f26d6c4Sdrh }
904f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
914f26d6c4Sdrh   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
924f26d6c4Sdrh }
934f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
944f26d6c4Sdrh   pCtx->isError = 1;
95d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
964f26d6c4Sdrh }
974f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
984f26d6c4Sdrh   pCtx->isError = 1;
99d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
1004f26d6c4Sdrh }
101f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
1024f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
1034f26d6c4Sdrh }
1044f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
1054f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
1064f26d6c4Sdrh }
1074f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
108f4479501Sdrh   sqlite3VdbeMemSetNull(&pCtx->s);
1094f26d6c4Sdrh }
1104f26d6c4Sdrh void sqlite3_result_text(
1114f26d6c4Sdrh   sqlite3_context *pCtx,
1124f26d6c4Sdrh   const char *z,
1134f26d6c4Sdrh   int n,
114d8123366Sdanielk1977   void (*xDel)(void *)
1154f26d6c4Sdrh ){
116d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
1174f26d6c4Sdrh }
1186c62608fSdrh #ifndef SQLITE_OMIT_UTF16
1194f26d6c4Sdrh void sqlite3_result_text16(
1204f26d6c4Sdrh   sqlite3_context *pCtx,
1214f26d6c4Sdrh   const void *z,
1224f26d6c4Sdrh   int n,
123d8123366Sdanielk1977   void (*xDel)(void *)
1244f26d6c4Sdrh ){
125d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
126d8123366Sdanielk1977 }
127d8123366Sdanielk1977 void sqlite3_result_text16be(
128d8123366Sdanielk1977   sqlite3_context *pCtx,
129d8123366Sdanielk1977   const void *z,
130d8123366Sdanielk1977   int n,
131d8123366Sdanielk1977   void (*xDel)(void *)
132d8123366Sdanielk1977 ){
133d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
134d8123366Sdanielk1977 }
135d8123366Sdanielk1977 void sqlite3_result_text16le(
136d8123366Sdanielk1977   sqlite3_context *pCtx,
137d8123366Sdanielk1977   const void *z,
138d8123366Sdanielk1977   int n,
139d8123366Sdanielk1977   void (*xDel)(void *)
140d8123366Sdanielk1977 ){
141d8123366Sdanielk1977   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
1424f26d6c4Sdrh }
1436c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
1444f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
1454f26d6c4Sdrh   sqlite3VdbeMemCopy(&pCtx->s, pValue);
1464f26d6c4Sdrh }
1474f26d6c4Sdrh 
1484f26d6c4Sdrh 
1494f26d6c4Sdrh /*
1504f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
1514f26d6c4Sdrh ** statement is completely executed or an error occurs.
1524f26d6c4Sdrh */
1534f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
1544f26d6c4Sdrh   Vdbe *p = (Vdbe*)pStmt;
1559bb575fdSdrh   sqlite3 *db;
1564f26d6c4Sdrh   int rc;
1574f26d6c4Sdrh 
1581bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
1594f26d6c4Sdrh     return SQLITE_MISUSE;
1604f26d6c4Sdrh   }
16191b48aa1Sdrh   if( p->aborted ){
16291b48aa1Sdrh     return SQLITE_ABORT;
16391b48aa1Sdrh   }
164a21c6b6fSdanielk1977   if( p->pc<=0 && p->expired ){
165a21c6b6fSdanielk1977     if( p->rc==SQLITE_OK ){
166a21c6b6fSdanielk1977       p->rc = SQLITE_SCHEMA;
167a21c6b6fSdanielk1977     }
168a21c6b6fSdanielk1977     return SQLITE_ERROR;
169a21c6b6fSdanielk1977   }
1704f26d6c4Sdrh   db = p->db;
1714f26d6c4Sdrh   if( sqlite3SafetyOn(db) ){
1724f26d6c4Sdrh     p->rc = SQLITE_MISUSE;
1734f26d6c4Sdrh     return SQLITE_MISUSE;
1744f26d6c4Sdrh   }
1751d850a72Sdanielk1977   if( p->pc<0 ){
176c16a03b5Sdrh     /* Invoke the trace callback if there is one
177c16a03b5Sdrh     */
178c16a03b5Sdrh     if( (db = p->db)->xTrace && !db->init.busy ){
179c16a03b5Sdrh       assert( p->nOp>0 );
180c16a03b5Sdrh       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
181c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3!=0 );
182c16a03b5Sdrh       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
183c16a03b5Sdrh       sqlite3SafetyOff(db);
184c16a03b5Sdrh       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
185c16a03b5Sdrh       if( sqlite3SafetyOn(db) ){
186c16a03b5Sdrh         p->rc = SQLITE_MISUSE;
187c16a03b5Sdrh         return SQLITE_MISUSE;
188c16a03b5Sdrh       }
189c16a03b5Sdrh     }
190c16a03b5Sdrh 
191c16a03b5Sdrh     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
192c16a03b5Sdrh     ** on in debugging mode.
193c16a03b5Sdrh     */
194c16a03b5Sdrh #ifdef SQLITE_DEBUG
195c16a03b5Sdrh     if( (db->flags & SQLITE_SqlTrace)!=0 ){
196c16a03b5Sdrh       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
197c16a03b5Sdrh     }
198c16a03b5Sdrh #endif /* SQLITE_DEBUG */
199c16a03b5Sdrh 
2001d850a72Sdanielk1977     db->activeVdbeCnt++;
2011d850a72Sdanielk1977     p->pc = 0;
2021d850a72Sdanielk1977   }
203b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
2044f26d6c4Sdrh   if( p->explain ){
2054f26d6c4Sdrh     rc = sqlite3VdbeList(p);
206b7f9164eSdrh   }else
207b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
208b7f9164eSdrh   {
2094f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
2104f26d6c4Sdrh   }
2114f26d6c4Sdrh 
2124f26d6c4Sdrh   if( sqlite3SafetyOff(db) ){
2134f26d6c4Sdrh     rc = SQLITE_MISUSE;
2144f26d6c4Sdrh   }
2154f26d6c4Sdrh 
216*130b9f40Sdrh   sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
2174f26d6c4Sdrh   return rc;
2184f26d6c4Sdrh }
2194f26d6c4Sdrh 
2204f26d6c4Sdrh /*
221eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
222eb2e176aSdrh ** pointer to it.
223eb2e176aSdrh */
224eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
225eb2e176aSdrh   assert( p && p->pFunc );
226eb2e176aSdrh   return p->pFunc->pUserData;
227eb2e176aSdrh }
228eb2e176aSdrh 
229eb2e176aSdrh /*
230eb2e176aSdrh ** Allocate or return the aggregate context for a user function.  A new
231eb2e176aSdrh ** context is allocated on the first call.  Subsequent calls return the
232eb2e176aSdrh ** same context that was returned on prior calls.
233eb2e176aSdrh **
234eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
235eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
236eb2e176aSdrh ** this source file.
237eb2e176aSdrh */
238eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
239eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
240eb2e176aSdrh   if( p->pAgg==0 ){
241eb2e176aSdrh     if( nByte<=NBFS ){
242eb2e176aSdrh       p->pAgg = (void*)p->s.z;
243eb2e176aSdrh       memset(p->pAgg, 0, nByte);
244eb2e176aSdrh     }else{
245eb2e176aSdrh       p->pAgg = sqliteMalloc( nByte );
246eb2e176aSdrh     }
247eb2e176aSdrh   }
248eb2e176aSdrh   return p->pAgg;
249eb2e176aSdrh }
250eb2e176aSdrh 
251eb2e176aSdrh /*
252682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to
253682f68b0Sdanielk1977 ** the user-function defined by pCtx.
254682f68b0Sdanielk1977 */
255682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
256682f68b0Sdanielk1977   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
257682f68b0Sdanielk1977   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
258682f68b0Sdanielk1977     return 0;
259682f68b0Sdanielk1977   }
260f92c7ff7Sdrh   return pVdbeFunc->apAux[iArg].pAux;
261682f68b0Sdanielk1977 }
262682f68b0Sdanielk1977 
263682f68b0Sdanielk1977 /*
264682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th
265682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
266682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
267682f68b0Sdanielk1977 */
268682f68b0Sdanielk1977 void sqlite3_set_auxdata(
269682f68b0Sdanielk1977   sqlite3_context *pCtx,
270682f68b0Sdanielk1977   int iArg,
271682f68b0Sdanielk1977   void *pAux,
272682f68b0Sdanielk1977   void (*xDelete)(void*)
273682f68b0Sdanielk1977 ){
274682f68b0Sdanielk1977   struct AuxData *pAuxData;
275f92c7ff7Sdrh   VdbeFunc *pVdbeFunc;
276682f68b0Sdanielk1977   if( iArg<0 ) return;
277682f68b0Sdanielk1977 
278f92c7ff7Sdrh   pVdbeFunc = pCtx->pVdbeFunc;
279f92c7ff7Sdrh   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
280998da3a2Sdrh     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
281f92c7ff7Sdrh     pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
282998da3a2Sdrh     if( !pVdbeFunc ) return;
2830e3d7476Sdrh     memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
2840e3d7476Sdrh              sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
285998da3a2Sdrh     pVdbeFunc->nAux = iArg+1;
286998da3a2Sdrh     pVdbeFunc->pFunc = pCtx->pFunc;
287682f68b0Sdanielk1977   }
288682f68b0Sdanielk1977 
289f92c7ff7Sdrh   pAuxData = &pVdbeFunc->apAux[iArg];
290682f68b0Sdanielk1977   if( pAuxData->pAux && pAuxData->xDelete ){
291682f68b0Sdanielk1977     pAuxData->xDelete(pAuxData->pAux);
292682f68b0Sdanielk1977   }
293682f68b0Sdanielk1977   pAuxData->pAux = pAux;
294682f68b0Sdanielk1977   pAuxData->xDelete = xDelete;
295682f68b0Sdanielk1977 }
296682f68b0Sdanielk1977 
297682f68b0Sdanielk1977 /*
298eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
299eb2e176aSdrh ** called.
300eb2e176aSdrh **
301eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
302eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
303eb2e176aSdrh ** this source file.
304eb2e176aSdrh */
305eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
306eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
307eb2e176aSdrh   return p->cnt;
308eb2e176aSdrh }
309eb2e176aSdrh 
310eb2e176aSdrh /*
3114f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
3124f26d6c4Sdrh */
3134f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
3144f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3151bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
3164f26d6c4Sdrh }
3174f26d6c4Sdrh 
3184f26d6c4Sdrh /*
3194f26d6c4Sdrh ** Return the number of values available from the current row of the
3204f26d6c4Sdrh ** currently executing statement pStmt.
3214f26d6c4Sdrh */
3224f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
3234f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3241bcdb0c0Sdrh   if( pVm==0 || !pVm->resOnStack ) return 0;
3254f26d6c4Sdrh   return pVm->nResColumn;
3264f26d6c4Sdrh }
3274f26d6c4Sdrh 
3284f26d6c4Sdrh 
3294f26d6c4Sdrh /*
3304f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
3314f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
3324f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
3334f26d6c4Sdrh ** of NULL.
3344f26d6c4Sdrh */
3354f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
3364f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3374f26d6c4Sdrh   int vals = sqlite3_data_count(pStmt);
3384f26d6c4Sdrh   if( i>=vals || i<0 ){
3394f26d6c4Sdrh     static Mem nullMem;
3404f26d6c4Sdrh     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
3414f26d6c4Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
3424f26d6c4Sdrh     return &nullMem;
3434f26d6c4Sdrh   }
3444f26d6c4Sdrh   return &pVm->pTos[(1-vals)+i];
3454f26d6c4Sdrh }
3464f26d6c4Sdrh 
3474f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
3484f26d6c4Sdrh ** The following routines are used to access elements of the current row
3494f26d6c4Sdrh ** in the result set.
3504f26d6c4Sdrh */
351c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
352c572ef7fSdanielk1977   return sqlite3_value_blob( columnMem(pStmt,i) );
353c572ef7fSdanielk1977 }
3544f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
3554f26d6c4Sdrh   return sqlite3_value_bytes( columnMem(pStmt,i) );
3564f26d6c4Sdrh }
3574f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
3584f26d6c4Sdrh   return sqlite3_value_bytes16( columnMem(pStmt,i) );
3594f26d6c4Sdrh }
3604f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
3614f26d6c4Sdrh   return sqlite3_value_double( columnMem(pStmt,i) );
3624f26d6c4Sdrh }
3634f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
3644f26d6c4Sdrh   return sqlite3_value_int( columnMem(pStmt,i) );
3654f26d6c4Sdrh }
366efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
3674f26d6c4Sdrh   return sqlite3_value_int64( columnMem(pStmt,i) );
3684f26d6c4Sdrh }
3694f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
3704f26d6c4Sdrh   return sqlite3_value_text( columnMem(pStmt,i) );
3714f26d6c4Sdrh }
372d1e4733dSdrh #if 0
373d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
374d1e4733dSdrh   return columnMem(pStmt, i);
375d1e4733dSdrh }
376d1e4733dSdrh #endif
3776c62608fSdrh #ifndef SQLITE_OMIT_UTF16
3784f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
3794f26d6c4Sdrh   return sqlite3_value_text16( columnMem(pStmt,i) );
3804f26d6c4Sdrh }
3816c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
3824f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
3834f26d6c4Sdrh   return sqlite3_value_type( columnMem(pStmt,i) );
3844f26d6c4Sdrh }
3854f26d6c4Sdrh 
3865e2517e1Sdrh /*
3875e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
3885e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
389e590fbdeSdrh **
390e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
391e590fbdeSdrh ** name is returned.  Here are the names:
392e590fbdeSdrh **
393e590fbdeSdrh **    0      The column name as it should be displayed for output
394e590fbdeSdrh **    1      The datatype name for the column
395e590fbdeSdrh **    2      The name of the database that the column derives from
396e590fbdeSdrh **    3      The name of the table that the column derives from
397e590fbdeSdrh **    4      The name of the table column that the result column derives from
398e590fbdeSdrh **
399e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
400e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
4015e2517e1Sdrh */
4025e2517e1Sdrh static const void *columnName(
4035e2517e1Sdrh   sqlite3_stmt *pStmt,
4045e2517e1Sdrh   int N,
4055e2517e1Sdrh   const void *(*xFunc)(Mem*),
4065e2517e1Sdrh   int useType
4075e2517e1Sdrh ){
4085e2517e1Sdrh   Vdbe *p = (Vdbe *)pStmt;
4095e2517e1Sdrh   int n = sqlite3_column_count(pStmt);
4105e2517e1Sdrh 
4115e2517e1Sdrh   if( p==0 || N>=n || N<0 ){
4125e2517e1Sdrh     return 0;
4135e2517e1Sdrh   }
414e590fbdeSdrh   N += useType*n;
4155e2517e1Sdrh   return xFunc(&p->aColName[N]);
4165e2517e1Sdrh }
4175e2517e1Sdrh 
4184f26d6c4Sdrh 
4194f26d6c4Sdrh /*
4204f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
4214f26d6c4Sdrh ** statement pStmt.
4224f26d6c4Sdrh */
4234f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
4245e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
4254f26d6c4Sdrh }
426e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
427e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
428e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
429e590fbdeSdrh }
430e590fbdeSdrh #endif
4314f26d6c4Sdrh 
4324f26d6c4Sdrh /*
4336c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
434e590fbdeSdrh ** of the result set of SQL statement pStmt.
4356c62608fSdrh */
4366c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
4376c62608fSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
4386c62608fSdrh }
4396c62608fSdrh #ifndef SQLITE_OMIT_UTF16
44076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
4415e2517e1Sdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
4424f26d6c4Sdrh }
4436c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
4444f26d6c4Sdrh 
445e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
446e590fbdeSdrh /*
447e590fbdeSdrh ** Return the name of the database from which a result column derives.
448e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
449e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
450e590fbdeSdrh */
451e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
452e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
453e590fbdeSdrh }
454e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
455e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
456e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
457e590fbdeSdrh }
458e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
459e590fbdeSdrh 
460e590fbdeSdrh /*
461e590fbdeSdrh ** Return the name of the table from which a result column derives.
462e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
463e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
464e590fbdeSdrh */
465e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
466e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
467e590fbdeSdrh }
468e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
469e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
470e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
471e590fbdeSdrh }
472e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
473e590fbdeSdrh 
474e590fbdeSdrh /*
475e590fbdeSdrh ** Return the name of the table column from which a result column derives.
476e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
477e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
478e590fbdeSdrh */
479e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
480e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
481e590fbdeSdrh }
482e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
483e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
484e590fbdeSdrh   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
485e590fbdeSdrh }
486e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
487e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */
488e590fbdeSdrh 
489e590fbdeSdrh 
490e590fbdeSdrh 
491e590fbdeSdrh 
4924f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
4934f26d6c4Sdrh **
4944f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
4954f26d6c4Sdrh */
4964f26d6c4Sdrh /*
4974f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
4984f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
4994f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
5004f26d6c4Sdrh **
5014f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
5024f26d6c4Sdrh ** value in any case.
5034f26d6c4Sdrh */
5044f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
5054f26d6c4Sdrh   Mem *pVar;
5061bcdb0c0Sdrh   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
507473d179dSdrh     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
5084f26d6c4Sdrh     return SQLITE_MISUSE;
5094f26d6c4Sdrh   }
5104f26d6c4Sdrh   if( i<1 || i>p->nVar ){
5114f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
5124f26d6c4Sdrh     return SQLITE_RANGE;
5134f26d6c4Sdrh   }
5144f26d6c4Sdrh   i--;
515290c1948Sdrh   pVar = &p->aVar[i];
516d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
5174f26d6c4Sdrh   pVar->flags = MEM_Null;
5184f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
5194f26d6c4Sdrh   return SQLITE_OK;
5204f26d6c4Sdrh }
5214f26d6c4Sdrh 
5224f26d6c4Sdrh /*
5235e2517e1Sdrh ** Bind a text or BLOB value.
5244f26d6c4Sdrh */
5255e2517e1Sdrh static int bindText(
526f4479501Sdrh   sqlite3_stmt *pStmt,
5274f26d6c4Sdrh   int i,
5284f26d6c4Sdrh   const void *zData,
5294f26d6c4Sdrh   int nData,
5305e2517e1Sdrh   void (*xDel)(void*),
5315e2517e1Sdrh   int encoding
5324f26d6c4Sdrh ){
5334f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5344f26d6c4Sdrh   Mem *pVar;
5354f26d6c4Sdrh   int rc;
5364f26d6c4Sdrh 
5374f26d6c4Sdrh   rc = vdbeUnbind(p, i);
538feac5f8dSdrh   if( rc || zData==0 ){
5394f26d6c4Sdrh     return rc;
5404f26d6c4Sdrh   }
541290c1948Sdrh   pVar = &p->aVar[i-1];
5425e2517e1Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
5435e2517e1Sdrh   if( rc ){
5444f26d6c4Sdrh     return rc;
5454f26d6c4Sdrh   }
5465e2517e1Sdrh   if( rc==SQLITE_OK && encoding!=0 ){
5475e2517e1Sdrh     rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
5485e2517e1Sdrh   }
5495e2517e1Sdrh   return rc;
5505e2517e1Sdrh }
5515e2517e1Sdrh 
5525e2517e1Sdrh 
5535e2517e1Sdrh /*
5545e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
5555e2517e1Sdrh */
5565e2517e1Sdrh int sqlite3_bind_blob(
5575e2517e1Sdrh   sqlite3_stmt *pStmt,
5585e2517e1Sdrh   int i,
5595e2517e1Sdrh   const void *zData,
5605e2517e1Sdrh   int nData,
5615e2517e1Sdrh   void (*xDel)(void*)
5625e2517e1Sdrh ){
5635e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
5645e2517e1Sdrh }
5654f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
5664f26d6c4Sdrh   int rc;
5674f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5684f26d6c4Sdrh   rc = vdbeUnbind(p, i);
5694f26d6c4Sdrh   if( rc==SQLITE_OK ){
570290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
5714f26d6c4Sdrh   }
572f4618891Sdanielk1977   return rc;
5734f26d6c4Sdrh }
5744f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
575efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
5764f26d6c4Sdrh }
577efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
5784f26d6c4Sdrh   int rc;
5794f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5804f26d6c4Sdrh   rc = vdbeUnbind(p, i);
5814f26d6c4Sdrh   if( rc==SQLITE_OK ){
582290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
5834f26d6c4Sdrh   }
5844f26d6c4Sdrh   return rc;
5854f26d6c4Sdrh }
5864f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){
5874f26d6c4Sdrh   return vdbeUnbind((Vdbe *)p, i);
5884f26d6c4Sdrh }
5894f26d6c4Sdrh int sqlite3_bind_text(
5904f26d6c4Sdrh   sqlite3_stmt *pStmt,
5914f26d6c4Sdrh   int i,
5924f26d6c4Sdrh   const char *zData,
5934f26d6c4Sdrh   int nData,
594d8123366Sdanielk1977   void (*xDel)(void*)
5954f26d6c4Sdrh ){
5965e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
5974f26d6c4Sdrh }
5986c62608fSdrh #ifndef SQLITE_OMIT_UTF16
5994f26d6c4Sdrh int sqlite3_bind_text16(
6004f26d6c4Sdrh   sqlite3_stmt *pStmt,
6014f26d6c4Sdrh   int i,
6024f26d6c4Sdrh   const void *zData,
6034f26d6c4Sdrh   int nData,
604d8123366Sdanielk1977   void (*xDel)(void*)
6054f26d6c4Sdrh ){
6065e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
6074f26d6c4Sdrh }
6086c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
60975f6a032Sdrh 
61075f6a032Sdrh /*
61175f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
61275f6a032Sdrh ** This routine is added to support DBD::SQLite.
61375f6a032Sdrh */
61475f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
61592febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
61692febd92Sdrh   return p ? p->nVar : 0;
61775f6a032Sdrh }
618895d7472Sdrh 
619895d7472Sdrh /*
620fa6bc000Sdrh ** Create a mapping from variable numbers to variable names
621fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already
622fa6bc000Sdrh ** exist.
623895d7472Sdrh */
624fa6bc000Sdrh static void createVarMap(Vdbe *p){
625895d7472Sdrh   if( !p->okVar ){
626895d7472Sdrh     int j;
627895d7472Sdrh     Op *pOp;
628895d7472Sdrh     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
629895d7472Sdrh       if( pOp->opcode==OP_Variable ){
630895d7472Sdrh         assert( pOp->p1>0 && pOp->p1<=p->nVar );
631895d7472Sdrh         p->azVar[pOp->p1-1] = pOp->p3;
632895d7472Sdrh       }
633895d7472Sdrh     }
634895d7472Sdrh     p->okVar = 1;
635895d7472Sdrh   }
636fa6bc000Sdrh }
637fa6bc000Sdrh 
638fa6bc000Sdrh /*
639fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
640fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
641fa6bc000Sdrh **
642fa6bc000Sdrh ** The result is always UTF-8.
643fa6bc000Sdrh */
644fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
645fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
646fa6bc000Sdrh   if( p==0 || i<1 || i>p->nVar ){
647fa6bc000Sdrh     return 0;
648fa6bc000Sdrh   }
649fa6bc000Sdrh   createVarMap(p);
650895d7472Sdrh   return p->azVar[i-1];
651895d7472Sdrh }
652fa6bc000Sdrh 
653fa6bc000Sdrh /*
654fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
655fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
656fa6bc000Sdrh ** return 0.
657fa6bc000Sdrh */
658fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
659fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
660fa6bc000Sdrh   int i;
661fa6bc000Sdrh   if( p==0 ){
662fa6bc000Sdrh     return 0;
663fa6bc000Sdrh   }
664fa6bc000Sdrh   createVarMap(p);
6656a8903c3Sdrh   if( zName ){
666fa6bc000Sdrh     for(i=0; i<p->nVar; i++){
667971a7c87Sdrh       const char *z = p->azVar[i];
6686a8903c3Sdrh       if( z && strcmp(z,zName)==0 ){
669fa6bc000Sdrh         return i+1;
670fa6bc000Sdrh       }
671fa6bc000Sdrh     }
6726a8903c3Sdrh   }
673fa6bc000Sdrh   return 0;
674fa6bc000Sdrh }
675f8db1bc0Sdrh 
67651942bc3Sdrh /*
677f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
678f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then
679f8db1bc0Sdrh ** an SQLITE_ERROR is returned.
680f8db1bc0Sdrh */
681f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
682f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
683f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
684f8db1bc0Sdrh   int i, rc = SQLITE_OK;
685f8db1bc0Sdrh   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
686f8db1bc0Sdrh     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
687f8db1bc0Sdrh     return SQLITE_MISUSE;
688f8db1bc0Sdrh   }
689f8db1bc0Sdrh   if( pFrom->nVar!=pTo->nVar ){
690f8db1bc0Sdrh     return SQLITE_ERROR;
691f8db1bc0Sdrh   }
692f8db1bc0Sdrh   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
693f8db1bc0Sdrh     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
694f8db1bc0Sdrh   }
695f8db1bc0Sdrh   return rc;
696f8db1bc0Sdrh }
69751942bc3Sdrh 
69851942bc3Sdrh /*
69951942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
70051942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
70151942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
70251942bc3Sdrh ** the statement in the first place.
70351942bc3Sdrh */
70451942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
70551942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
70651942bc3Sdrh }
707