xref: /sqlite-3.40.0/src/vdbeapi.c (revision d02eb1fd)
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 
194f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
204f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
214f26d6c4Sdrh ** structure.
224f26d6c4Sdrh */
234f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
244f26d6c4Sdrh   Mem *p = (Mem*)pVal;
254f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
264f26d6c4Sdrh     return p->z;
274f26d6c4Sdrh   }else{
284f26d6c4Sdrh     return sqlite3_value_text(pVal);
294f26d6c4Sdrh   }
304f26d6c4Sdrh }
314f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
324f26d6c4Sdrh   Mem *p = (Mem*)pVal;
334f26d6c4Sdrh   if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text(pVal) ){
344f26d6c4Sdrh     return p->n;
354f26d6c4Sdrh   }
364f26d6c4Sdrh   return 0;
374f26d6c4Sdrh }
384f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
394f26d6c4Sdrh   Mem *p = (Mem*)pVal;
404f26d6c4Sdrh   if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text16(pVal) ){
414f26d6c4Sdrh     return ((Mem *)pVal)->n;
424f26d6c4Sdrh   }
434f26d6c4Sdrh   return 0;
444f26d6c4Sdrh }
454f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
464f26d6c4Sdrh   Mem *pMem = (Mem *)pVal;
47f4479501Sdrh   sqlite3VdbeMemRealify(pMem);
484f26d6c4Sdrh   return pMem->r;
494f26d6c4Sdrh }
504f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
514f26d6c4Sdrh   Mem *pMem = (Mem *)pVal;
52f4479501Sdrh   sqlite3VdbeMemIntegerify(pMem);
534f26d6c4Sdrh   return (int)pVal->i;
544f26d6c4Sdrh }
554f26d6c4Sdrh long long int sqlite3_value_int64(sqlite3_value *pVal){
564f26d6c4Sdrh   Mem *pMem = (Mem *)pVal;
57f4479501Sdrh   sqlite3VdbeMemIntegerify(pMem);
584f26d6c4Sdrh   return pVal->i;
594f26d6c4Sdrh }
604f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
614f26d6c4Sdrh   if( pVal->flags&MEM_Null ){
624f26d6c4Sdrh     /* For a NULL return a NULL Pointer */
634f26d6c4Sdrh     return 0;
644f26d6c4Sdrh   }
654f26d6c4Sdrh 
664f26d6c4Sdrh   if( pVal->flags&MEM_Str ){
674f26d6c4Sdrh     /* If there is already a string representation, make sure it is in
684f26d6c4Sdrh     ** encoded in UTF-8.
694f26d6c4Sdrh     */
70f4479501Sdrh     sqlite3VdbeChangeEncoding(pVal, TEXT_Utf8);
714f26d6c4Sdrh   }else if( !(pVal->flags&MEM_Blob) ){
724f26d6c4Sdrh     /* Otherwise, unless this is a blob, convert it to a UTF-8 string */
73f4479501Sdrh     sqlite3VdbeMemStringify(pVal, TEXT_Utf8);
744f26d6c4Sdrh   }
754f26d6c4Sdrh 
764f26d6c4Sdrh   return pVal->z;
774f26d6c4Sdrh }
784f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
794f26d6c4Sdrh   if( pVal->flags&MEM_Null ){
804f26d6c4Sdrh     /* For a NULL return a NULL Pointer */
814f26d6c4Sdrh     return 0;
824f26d6c4Sdrh   }
834f26d6c4Sdrh 
844f26d6c4Sdrh   if( pVal->flags&MEM_Str ){
854f26d6c4Sdrh     /* If there is already a string representation, make sure it is in
864f26d6c4Sdrh     ** encoded in UTF-16 machine byte order.
874f26d6c4Sdrh     */
88f4479501Sdrh     sqlite3VdbeChangeEncoding(pVal, TEXT_Utf16);
894f26d6c4Sdrh   }else if( !(pVal->flags&MEM_Blob) ){
904f26d6c4Sdrh     /* Otherwise, unless this is a blob, convert it to a UTF-16 string */
91f4479501Sdrh     sqlite3VdbeMemStringify(pVal, TEXT_Utf16);
924f26d6c4Sdrh   }
934f26d6c4Sdrh 
944f26d6c4Sdrh   return (const void *)(pVal->z);
954f26d6c4Sdrh }
964f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
97f4479501Sdrh   return pVal->type;
984f26d6c4Sdrh }
994f26d6c4Sdrh 
1004f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
1014f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
1024f26d6c4Sdrh ** the function result.
1034f26d6c4Sdrh */
1044f26d6c4Sdrh void sqlite3_result_blob(
1054f26d6c4Sdrh   sqlite3_context *pCtx,
1064f26d6c4Sdrh   const void *z,
1074f26d6c4Sdrh   int n,
1084f26d6c4Sdrh   int eCopy
1094f26d6c4Sdrh ){
1104f26d6c4Sdrh   assert( n>0 );
111f4479501Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, eCopy);
1124f26d6c4Sdrh }
1134f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
1144f26d6c4Sdrh   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
1154f26d6c4Sdrh }
1164f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
1174f26d6c4Sdrh   pCtx->isError = 1;
1184f26d6c4Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1);
1194f26d6c4Sdrh }
1204f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
1214f26d6c4Sdrh   pCtx->isError = 1;
1224f26d6c4Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1);
1234f26d6c4Sdrh }
124f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
1254f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
1264f26d6c4Sdrh }
1274f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
1284f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
1294f26d6c4Sdrh }
1304f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
131f4479501Sdrh   sqlite3VdbeMemSetNull(&pCtx->s);
1324f26d6c4Sdrh }
1334f26d6c4Sdrh void sqlite3_result_text(
1344f26d6c4Sdrh   sqlite3_context *pCtx,
1354f26d6c4Sdrh   const char *z,
1364f26d6c4Sdrh   int n,
1374f26d6c4Sdrh   int eCopy
1384f26d6c4Sdrh ){
139f4479501Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy);
1404f26d6c4Sdrh }
1414f26d6c4Sdrh void sqlite3_result_text16(
1424f26d6c4Sdrh   sqlite3_context *pCtx,
1434f26d6c4Sdrh   const void *z,
1444f26d6c4Sdrh   int n,
1454f26d6c4Sdrh   int eCopy
1464f26d6c4Sdrh ){
147f4479501Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy);
1484f26d6c4Sdrh }
1494f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
1504f26d6c4Sdrh   sqlite3VdbeMemCopy(&pCtx->s, pValue);
1514f26d6c4Sdrh }
1524f26d6c4Sdrh 
1534f26d6c4Sdrh 
1544f26d6c4Sdrh /*
1554f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
1564f26d6c4Sdrh ** statement is completely executed or an error occurs.
1574f26d6c4Sdrh */
1584f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
1594f26d6c4Sdrh   Vdbe *p = (Vdbe*)pStmt;
1604f26d6c4Sdrh   sqlite *db;
1614f26d6c4Sdrh   int rc;
1624f26d6c4Sdrh 
1634f26d6c4Sdrh   if( p->magic!=VDBE_MAGIC_RUN ){
1644f26d6c4Sdrh     return SQLITE_MISUSE;
1654f26d6c4Sdrh   }
1664f26d6c4Sdrh   db = p->db;
1674f26d6c4Sdrh   if( sqlite3SafetyOn(db) ){
1684f26d6c4Sdrh     p->rc = SQLITE_MISUSE;
1694f26d6c4Sdrh     return SQLITE_MISUSE;
1704f26d6c4Sdrh   }
1711d850a72Sdanielk1977   if( p->pc<0 ){
1721d850a72Sdanielk1977     db->activeVdbeCnt++;
1731d850a72Sdanielk1977     p->pc = 0;
1741d850a72Sdanielk1977   }
1754f26d6c4Sdrh   if( p->explain ){
1764f26d6c4Sdrh     rc = sqlite3VdbeList(p);
1774f26d6c4Sdrh   }else{
1784f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
1794f26d6c4Sdrh   }
1804f26d6c4Sdrh 
1814f26d6c4Sdrh   if( sqlite3SafetyOff(db) ){
1824f26d6c4Sdrh     rc = SQLITE_MISUSE;
1834f26d6c4Sdrh   }
1844f26d6c4Sdrh 
1854f26d6c4Sdrh   sqlite3Error(p->db, rc, p->zErrMsg);
1864f26d6c4Sdrh   return rc;
1874f26d6c4Sdrh }
1884f26d6c4Sdrh 
1894f26d6c4Sdrh /*
190eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
191eb2e176aSdrh ** pointer to it.
192eb2e176aSdrh */
193eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
194eb2e176aSdrh   assert( p && p->pFunc );
195eb2e176aSdrh   return p->pFunc->pUserData;
196eb2e176aSdrh }
197eb2e176aSdrh 
198eb2e176aSdrh /*
199eb2e176aSdrh ** Allocate or return the aggregate context for a user function.  A new
200eb2e176aSdrh ** context is allocated on the first call.  Subsequent calls return the
201eb2e176aSdrh ** same context that was returned on prior calls.
202eb2e176aSdrh **
203eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
204eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
205eb2e176aSdrh ** this source file.
206eb2e176aSdrh */
207eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
208eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
209eb2e176aSdrh   if( p->pAgg==0 ){
210eb2e176aSdrh     if( nByte<=NBFS ){
211eb2e176aSdrh       p->pAgg = (void*)p->s.z;
212eb2e176aSdrh       memset(p->pAgg, 0, nByte);
213eb2e176aSdrh     }else{
214eb2e176aSdrh       p->pAgg = sqliteMalloc( nByte );
215eb2e176aSdrh     }
216eb2e176aSdrh   }
217eb2e176aSdrh   return p->pAgg;
218eb2e176aSdrh }
219eb2e176aSdrh 
220eb2e176aSdrh /*
221682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to
222682f68b0Sdanielk1977 ** the user-function defined by pCtx.
223682f68b0Sdanielk1977 */
224682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
225682f68b0Sdanielk1977   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
226682f68b0Sdanielk1977   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
227682f68b0Sdanielk1977     return 0;
228682f68b0Sdanielk1977   }
229682f68b0Sdanielk1977   return pCtx->pVdbeFunc->apAux[iArg].pAux;
230682f68b0Sdanielk1977 }
231682f68b0Sdanielk1977 
232682f68b0Sdanielk1977 /*
233682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th
234682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
235682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
236682f68b0Sdanielk1977 */
237682f68b0Sdanielk1977 void sqlite3_set_auxdata(
238682f68b0Sdanielk1977   sqlite3_context *pCtx,
239682f68b0Sdanielk1977   int iArg,
240682f68b0Sdanielk1977   void *pAux,
241682f68b0Sdanielk1977   void (*xDelete)(void*)
242682f68b0Sdanielk1977 ){
243682f68b0Sdanielk1977   struct AuxData *pAuxData;
244682f68b0Sdanielk1977   if( iArg<0 ) return;
245682f68b0Sdanielk1977 
246682f68b0Sdanielk1977   if( !pCtx->pVdbeFunc || pCtx->pVdbeFunc->nAux<=iArg ){
247682f68b0Sdanielk1977     int nMalloc = sizeof(VdbeFunc)+sizeof(struct AuxData)*(iArg+1);
248682f68b0Sdanielk1977     pCtx->pVdbeFunc = sqliteRealloc(pCtx->pVdbeFunc, nMalloc);
249682f68b0Sdanielk1977     if( !pCtx->pVdbeFunc ) return;
250682f68b0Sdanielk1977     pCtx->pVdbeFunc->nAux = iArg+1;
251*d02eb1fdSdanielk1977     pCtx->pVdbeFunc->pFunc = pCtx->pFunc;
252682f68b0Sdanielk1977   }
253682f68b0Sdanielk1977 
254682f68b0Sdanielk1977   pAuxData = &pCtx->pVdbeFunc->apAux[iArg];
255682f68b0Sdanielk1977   if( pAuxData->pAux && pAuxData->xDelete ){
256682f68b0Sdanielk1977     pAuxData->xDelete(pAuxData->pAux);
257682f68b0Sdanielk1977   }
258682f68b0Sdanielk1977   pAuxData->pAux = pAux;
259682f68b0Sdanielk1977   pAuxData->xDelete = xDelete;
260682f68b0Sdanielk1977 }
261682f68b0Sdanielk1977 
262682f68b0Sdanielk1977 /*
263eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
264eb2e176aSdrh ** called.
265eb2e176aSdrh **
266eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
267eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
268eb2e176aSdrh ** this source file.
269eb2e176aSdrh */
270eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
271eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
272eb2e176aSdrh   return p->cnt;
273eb2e176aSdrh }
274eb2e176aSdrh 
275eb2e176aSdrh /*
2764f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
2774f26d6c4Sdrh */
2784f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
2794f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
2804f26d6c4Sdrh   return pVm->nResColumn;
2814f26d6c4Sdrh }
2824f26d6c4Sdrh 
2834f26d6c4Sdrh /*
2844f26d6c4Sdrh ** Return the number of values available from the current row of the
2854f26d6c4Sdrh ** currently executing statement pStmt.
2864f26d6c4Sdrh */
2874f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
2884f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
2894f26d6c4Sdrh   if( !pVm->resOnStack ) return 0;
2904f26d6c4Sdrh   return pVm->nResColumn;
2914f26d6c4Sdrh }
2924f26d6c4Sdrh 
2934f26d6c4Sdrh 
2944f26d6c4Sdrh /*
2954f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
2964f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
2974f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
2984f26d6c4Sdrh ** of NULL.
2994f26d6c4Sdrh */
3004f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
3014f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
3024f26d6c4Sdrh   int vals = sqlite3_data_count(pStmt);
3034f26d6c4Sdrh   if( i>=vals || i<0 ){
3044f26d6c4Sdrh     static Mem nullMem;
3054f26d6c4Sdrh     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
3064f26d6c4Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
3074f26d6c4Sdrh     return &nullMem;
3084f26d6c4Sdrh   }
3094f26d6c4Sdrh   return &pVm->pTos[(1-vals)+i];
3104f26d6c4Sdrh }
3114f26d6c4Sdrh 
3124f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
3134f26d6c4Sdrh ** The following routines are used to access elements of the current row
3144f26d6c4Sdrh ** in the result set.
3154f26d6c4Sdrh */
316c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
317c572ef7fSdanielk1977   return sqlite3_value_blob( columnMem(pStmt,i) );
318c572ef7fSdanielk1977 }
3194f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
3204f26d6c4Sdrh   return sqlite3_value_bytes( columnMem(pStmt,i) );
3214f26d6c4Sdrh }
3224f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
3234f26d6c4Sdrh   return sqlite3_value_bytes16( columnMem(pStmt,i) );
3244f26d6c4Sdrh }
3254f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
3264f26d6c4Sdrh   return sqlite3_value_double( columnMem(pStmt,i) );
3274f26d6c4Sdrh }
3284f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
3294f26d6c4Sdrh   return sqlite3_value_int( columnMem(pStmt,i) );
3304f26d6c4Sdrh }
3314f26d6c4Sdrh long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
3324f26d6c4Sdrh   return sqlite3_value_int64( columnMem(pStmt,i) );
3334f26d6c4Sdrh }
3344f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
3354f26d6c4Sdrh   return sqlite3_value_text( columnMem(pStmt,i) );
3364f26d6c4Sdrh }
3374f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
3384f26d6c4Sdrh   return sqlite3_value_text16( columnMem(pStmt,i) );
3394f26d6c4Sdrh }
3404f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
3414f26d6c4Sdrh   return sqlite3_value_type( columnMem(pStmt,i) );
3424f26d6c4Sdrh }
3434f26d6c4Sdrh 
3444f26d6c4Sdrh 
3454f26d6c4Sdrh /*
3464f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
3474f26d6c4Sdrh ** statement pStmt.
3484f26d6c4Sdrh */
3494f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
3504f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
3514f26d6c4Sdrh   Mem *pColName;
3524f26d6c4Sdrh 
3534f26d6c4Sdrh   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3544f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3554f26d6c4Sdrh     return 0;
3564f26d6c4Sdrh   }
3574f26d6c4Sdrh 
3584f26d6c4Sdrh   pColName = &(p->aColName[N]);
3594f26d6c4Sdrh   return sqlite3_value_text(pColName);
3604f26d6c4Sdrh }
3614f26d6c4Sdrh 
3624f26d6c4Sdrh /*
3634f26d6c4Sdrh ** Return the name of the 'i'th column of the result set of SQL statement
3644f26d6c4Sdrh ** pStmt, encoded as UTF-16.
3654f26d6c4Sdrh */
3664f26d6c4Sdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
3674f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
3684f26d6c4Sdrh   Mem *pColName;
3694f26d6c4Sdrh 
3704f26d6c4Sdrh   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3714f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3724f26d6c4Sdrh     return 0;
3734f26d6c4Sdrh   }
3744f26d6c4Sdrh 
3754f26d6c4Sdrh   pColName = &(p->aColName[N]);
3764f26d6c4Sdrh   return sqlite3_value_text16(pColName);
3774f26d6c4Sdrh }
3784f26d6c4Sdrh 
3794f26d6c4Sdrh /*
3804f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column
3814f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-8.
3824f26d6c4Sdrh */
38376d505baSdanielk1977 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
3844f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
38576d505baSdanielk1977   Mem *pColName;
3864f26d6c4Sdrh 
38776d505baSdanielk1977   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3884f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3894f26d6c4Sdrh     return 0;
3904f26d6c4Sdrh   }
3914f26d6c4Sdrh 
39276d505baSdanielk1977   pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
39376d505baSdanielk1977   return sqlite3_value_text(pColName);
3944f26d6c4Sdrh }
3954f26d6c4Sdrh 
3964f26d6c4Sdrh /*
3974f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column
3984f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-16.
3994f26d6c4Sdrh */
40076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
40176d505baSdanielk1977   Vdbe *p = (Vdbe *)pStmt;
40276d505baSdanielk1977   Mem *pColName;
40376d505baSdanielk1977 
40476d505baSdanielk1977   if( N>=sqlite3_column_count(pStmt) || N<0 ){
40576d505baSdanielk1977     sqlite3Error(p->db, SQLITE_RANGE, 0);
40676d505baSdanielk1977     return 0;
40776d505baSdanielk1977   }
40876d505baSdanielk1977 
40976d505baSdanielk1977   pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
41076d505baSdanielk1977   return sqlite3_value_text16(pColName);
4114f26d6c4Sdrh }
4124f26d6c4Sdrh 
4134f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
4144f26d6c4Sdrh **
4154f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
4164f26d6c4Sdrh */
4174f26d6c4Sdrh /*
4184f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
4194f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
4204f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
4214f26d6c4Sdrh **
4224f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
4234f26d6c4Sdrh ** value in any case.
4244f26d6c4Sdrh */
4254f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
4264f26d6c4Sdrh   Mem *pVar;
4271d850a72Sdanielk1977   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
4284f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_MISUSE, 0);
4294f26d6c4Sdrh     return SQLITE_MISUSE;
4304f26d6c4Sdrh   }
4314f26d6c4Sdrh   if( i<1 || i>p->nVar ){
4324f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
4334f26d6c4Sdrh     return SQLITE_RANGE;
4344f26d6c4Sdrh   }
4354f26d6c4Sdrh   i--;
4364f26d6c4Sdrh   pVar = &p->apVar[i];
4374f26d6c4Sdrh   if( pVar->flags&MEM_Dyn ){
4384f26d6c4Sdrh     sqliteFree(pVar->z);
4394f26d6c4Sdrh   }
4404f26d6c4Sdrh   pVar->flags = MEM_Null;
4414f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
4424f26d6c4Sdrh   return SQLITE_OK;
4434f26d6c4Sdrh }
4444f26d6c4Sdrh 
4454f26d6c4Sdrh /*
4464f26d6c4Sdrh ** Bind a blob value to an SQL statement variable.
4474f26d6c4Sdrh */
4484f26d6c4Sdrh int sqlite3_bind_blob(
449f4479501Sdrh   sqlite3_stmt *pStmt,
4504f26d6c4Sdrh   int i,
4514f26d6c4Sdrh   const void *zData,
4524f26d6c4Sdrh   int nData,
4534f26d6c4Sdrh   int eCopy
4544f26d6c4Sdrh ){
4554f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4564f26d6c4Sdrh   Mem *pVar;
4574f26d6c4Sdrh   int rc;
4584f26d6c4Sdrh 
4594f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4604f26d6c4Sdrh   if( rc ){
4614f26d6c4Sdrh     return rc;
4624f26d6c4Sdrh   }
4634f26d6c4Sdrh   pVar = &p->apVar[i-1];
4644f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy);
4654f26d6c4Sdrh   return rc;
4664f26d6c4Sdrh }
4674f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
4684f26d6c4Sdrh   int rc;
4694f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4704f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4714f26d6c4Sdrh   if( rc==SQLITE_OK ){
472f4479501Sdrh     sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
4734f26d6c4Sdrh   }
4744f26d6c4Sdrh   return SQLITE_OK;
4754f26d6c4Sdrh }
4764f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
4774f26d6c4Sdrh   return sqlite3_bind_int64(p, i, (long long int)iValue);
4784f26d6c4Sdrh }
4794f26d6c4Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){
4804f26d6c4Sdrh   int rc;
4814f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4824f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4834f26d6c4Sdrh   if( rc==SQLITE_OK ){
484f4479501Sdrh     sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
4854f26d6c4Sdrh   }
4864f26d6c4Sdrh   return rc;
4874f26d6c4Sdrh }
4884f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){
4894f26d6c4Sdrh   return vdbeUnbind((Vdbe *)p, i);
4904f26d6c4Sdrh }
4914f26d6c4Sdrh int sqlite3_bind_text(
4924f26d6c4Sdrh   sqlite3_stmt *pStmt,
4934f26d6c4Sdrh   int i,
4944f26d6c4Sdrh   const char *zData,
4954f26d6c4Sdrh   int nData,
4964f26d6c4Sdrh   int eCopy
4974f26d6c4Sdrh ){
4984f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4994f26d6c4Sdrh   Mem *pVar;
5004f26d6c4Sdrh   int rc;
5014f26d6c4Sdrh 
5024f26d6c4Sdrh   rc = vdbeUnbind(p, i);
5034f26d6c4Sdrh   if( rc ){
5044f26d6c4Sdrh     return rc;
5054f26d6c4Sdrh   }
5064f26d6c4Sdrh   pVar = &p->apVar[i-1];
5074f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy);
5084f26d6c4Sdrh   if( rc ){
5094f26d6c4Sdrh     return rc;
5104f26d6c4Sdrh   }
511f4479501Sdrh   rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
5124f26d6c4Sdrh   return rc;
5134f26d6c4Sdrh }
5144f26d6c4Sdrh int sqlite3_bind_text16(
5154f26d6c4Sdrh   sqlite3_stmt *pStmt,
5164f26d6c4Sdrh   int i,
5174f26d6c4Sdrh   const void *zData,
5184f26d6c4Sdrh   int nData,
5194f26d6c4Sdrh   int eCopy
5204f26d6c4Sdrh ){
5214f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
5224f26d6c4Sdrh   Mem *pVar;
523f4479501Sdrh   int rc, txt_enc;
5244f26d6c4Sdrh 
5254f26d6c4Sdrh   rc = vdbeUnbind(p, i);
5264f26d6c4Sdrh   if( rc ){
5274f26d6c4Sdrh     return rc;
5284f26d6c4Sdrh   }
529f4479501Sdrh   pVar = &p->apVar[i-1];
5304f26d6c4Sdrh 
5314f26d6c4Sdrh   /* There may or may not be a byte order mark at the start of the UTF-16.
5324f26d6c4Sdrh   ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the
5334f26d6c4Sdrh   ** actual byte order used by this string. If the string does happen
5344f26d6c4Sdrh   ** to contain a BOM, then move zData so that it points to the first
5354f26d6c4Sdrh   ** byte after the BOM.
5364f26d6c4Sdrh   */
5374f26d6c4Sdrh   txt_enc = sqlite3UtfReadBom(zData, nData);
5384f26d6c4Sdrh   if( txt_enc ){
5394f26d6c4Sdrh     zData = (void *)(((u8 *)zData) + 2);
5404f26d6c4Sdrh     nData -= 2;
5414f26d6c4Sdrh   }else{
5429c054830Sdrh     txt_enc = SQLITE_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
5434f26d6c4Sdrh   }
5444f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
5454f26d6c4Sdrh   if( rc ){
5464f26d6c4Sdrh     return rc;
5474f26d6c4Sdrh   }
548f4479501Sdrh   rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
5494f26d6c4Sdrh   return rc;
5504f26d6c4Sdrh }
551