xref: /sqlite-3.40.0/src/vdbeapi.c (revision 9c054830)
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 /*
221eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
222eb2e176aSdrh ** called.
223eb2e176aSdrh **
224eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing
225eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in
226eb2e176aSdrh ** this source file.
227eb2e176aSdrh */
228eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
229eb2e176aSdrh   assert( p && p->pFunc && p->pFunc->xStep );
230eb2e176aSdrh   return p->cnt;
231eb2e176aSdrh }
232eb2e176aSdrh 
233eb2e176aSdrh /*
2344f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
2354f26d6c4Sdrh */
2364f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
2374f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
2384f26d6c4Sdrh   return pVm->nResColumn;
2394f26d6c4Sdrh }
2404f26d6c4Sdrh 
2414f26d6c4Sdrh /*
2424f26d6c4Sdrh ** Return the number of values available from the current row of the
2434f26d6c4Sdrh ** currently executing statement pStmt.
2444f26d6c4Sdrh */
2454f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
2464f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
2474f26d6c4Sdrh   if( !pVm->resOnStack ) return 0;
2484f26d6c4Sdrh   return pVm->nResColumn;
2494f26d6c4Sdrh }
2504f26d6c4Sdrh 
2514f26d6c4Sdrh 
2524f26d6c4Sdrh /*
2534f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
2544f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
2554f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
2564f26d6c4Sdrh ** of NULL.
2574f26d6c4Sdrh */
2584f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
2594f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
2604f26d6c4Sdrh   int vals = sqlite3_data_count(pStmt);
2614f26d6c4Sdrh   if( i>=vals || i<0 ){
2624f26d6c4Sdrh     static Mem nullMem;
2634f26d6c4Sdrh     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
2644f26d6c4Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
2654f26d6c4Sdrh     return &nullMem;
2664f26d6c4Sdrh   }
2674f26d6c4Sdrh   return &pVm->pTos[(1-vals)+i];
2684f26d6c4Sdrh }
2694f26d6c4Sdrh 
2704f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
2714f26d6c4Sdrh ** The following routines are used to access elements of the current row
2724f26d6c4Sdrh ** in the result set.
2734f26d6c4Sdrh */
274c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
275c572ef7fSdanielk1977   return sqlite3_value_blob( columnMem(pStmt,i) );
276c572ef7fSdanielk1977 }
2774f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
2784f26d6c4Sdrh   return sqlite3_value_bytes( columnMem(pStmt,i) );
2794f26d6c4Sdrh }
2804f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
2814f26d6c4Sdrh   return sqlite3_value_bytes16( columnMem(pStmt,i) );
2824f26d6c4Sdrh }
2834f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
2844f26d6c4Sdrh   return sqlite3_value_double( columnMem(pStmt,i) );
2854f26d6c4Sdrh }
2864f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
2874f26d6c4Sdrh   return sqlite3_value_int( columnMem(pStmt,i) );
2884f26d6c4Sdrh }
2894f26d6c4Sdrh long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
2904f26d6c4Sdrh   return sqlite3_value_int64( columnMem(pStmt,i) );
2914f26d6c4Sdrh }
2924f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
2934f26d6c4Sdrh   return sqlite3_value_text( columnMem(pStmt,i) );
2944f26d6c4Sdrh }
2954f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
2964f26d6c4Sdrh   return sqlite3_value_text16( columnMem(pStmt,i) );
2974f26d6c4Sdrh }
2984f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
2994f26d6c4Sdrh   return sqlite3_value_type( columnMem(pStmt,i) );
3004f26d6c4Sdrh }
3014f26d6c4Sdrh 
3024f26d6c4Sdrh 
3034f26d6c4Sdrh /*
3044f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
3054f26d6c4Sdrh ** statement pStmt.
3064f26d6c4Sdrh */
3074f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
3084f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
3094f26d6c4Sdrh   Mem *pColName;
3104f26d6c4Sdrh 
3114f26d6c4Sdrh   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3124f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3134f26d6c4Sdrh     return 0;
3144f26d6c4Sdrh   }
3154f26d6c4Sdrh 
3164f26d6c4Sdrh   pColName = &(p->aColName[N]);
3174f26d6c4Sdrh   return sqlite3_value_text(pColName);
3184f26d6c4Sdrh }
3194f26d6c4Sdrh 
3204f26d6c4Sdrh /*
3214f26d6c4Sdrh ** Return the name of the 'i'th column of the result set of SQL statement
3224f26d6c4Sdrh ** pStmt, encoded as UTF-16.
3234f26d6c4Sdrh */
3244f26d6c4Sdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
3254f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
3264f26d6c4Sdrh   Mem *pColName;
3274f26d6c4Sdrh 
3284f26d6c4Sdrh   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3294f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3304f26d6c4Sdrh     return 0;
3314f26d6c4Sdrh   }
3324f26d6c4Sdrh 
3334f26d6c4Sdrh   pColName = &(p->aColName[N]);
3344f26d6c4Sdrh   return sqlite3_value_text16(pColName);
3354f26d6c4Sdrh }
3364f26d6c4Sdrh 
3374f26d6c4Sdrh /*
3384f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column
3394f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-8.
3404f26d6c4Sdrh */
34176d505baSdanielk1977 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
3424f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
34376d505baSdanielk1977   Mem *pColName;
3444f26d6c4Sdrh 
34576d505baSdanielk1977   if( N>=sqlite3_column_count(pStmt) || N<0 ){
3464f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3474f26d6c4Sdrh     return 0;
3484f26d6c4Sdrh   }
3494f26d6c4Sdrh 
35076d505baSdanielk1977   pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
35176d505baSdanielk1977   return sqlite3_value_text(pColName);
3524f26d6c4Sdrh }
3534f26d6c4Sdrh 
3544f26d6c4Sdrh /*
3554f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column
3564f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-16.
3574f26d6c4Sdrh */
35876d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
35976d505baSdanielk1977   Vdbe *p = (Vdbe *)pStmt;
36076d505baSdanielk1977   Mem *pColName;
36176d505baSdanielk1977 
36276d505baSdanielk1977   if( N>=sqlite3_column_count(pStmt) || N<0 ){
36376d505baSdanielk1977     sqlite3Error(p->db, SQLITE_RANGE, 0);
36476d505baSdanielk1977     return 0;
36576d505baSdanielk1977   }
36676d505baSdanielk1977 
36776d505baSdanielk1977   pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
36876d505baSdanielk1977   return sqlite3_value_text16(pColName);
3694f26d6c4Sdrh }
3704f26d6c4Sdrh 
3714f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
3724f26d6c4Sdrh **
3734f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
3744f26d6c4Sdrh */
3754f26d6c4Sdrh /*
3764f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
3774f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
3784f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
3794f26d6c4Sdrh **
3804f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
3814f26d6c4Sdrh ** value in any case.
3824f26d6c4Sdrh */
3834f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
3844f26d6c4Sdrh   Mem *pVar;
3851d850a72Sdanielk1977   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
3864f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_MISUSE, 0);
3874f26d6c4Sdrh     return SQLITE_MISUSE;
3884f26d6c4Sdrh   }
3894f26d6c4Sdrh   if( i<1 || i>p->nVar ){
3904f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
3914f26d6c4Sdrh     return SQLITE_RANGE;
3924f26d6c4Sdrh   }
3934f26d6c4Sdrh   i--;
3944f26d6c4Sdrh   pVar = &p->apVar[i];
3954f26d6c4Sdrh   if( pVar->flags&MEM_Dyn ){
3964f26d6c4Sdrh     sqliteFree(pVar->z);
3974f26d6c4Sdrh   }
3984f26d6c4Sdrh   pVar->flags = MEM_Null;
3994f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
4004f26d6c4Sdrh   return SQLITE_OK;
4014f26d6c4Sdrh }
4024f26d6c4Sdrh 
4034f26d6c4Sdrh /*
4044f26d6c4Sdrh ** Bind a blob value to an SQL statement variable.
4054f26d6c4Sdrh */
4064f26d6c4Sdrh int sqlite3_bind_blob(
407f4479501Sdrh   sqlite3_stmt *pStmt,
4084f26d6c4Sdrh   int i,
4094f26d6c4Sdrh   const void *zData,
4104f26d6c4Sdrh   int nData,
4114f26d6c4Sdrh   int eCopy
4124f26d6c4Sdrh ){
4134f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4144f26d6c4Sdrh   Mem *pVar;
4154f26d6c4Sdrh   int rc;
4164f26d6c4Sdrh 
4174f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4184f26d6c4Sdrh   if( rc ){
4194f26d6c4Sdrh     return rc;
4204f26d6c4Sdrh   }
4214f26d6c4Sdrh   pVar = &p->apVar[i-1];
4224f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy);
4234f26d6c4Sdrh   return rc;
4244f26d6c4Sdrh }
4254f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
4264f26d6c4Sdrh   int rc;
4274f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4284f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4294f26d6c4Sdrh   if( rc==SQLITE_OK ){
430f4479501Sdrh     sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
4314f26d6c4Sdrh   }
4324f26d6c4Sdrh   return SQLITE_OK;
4334f26d6c4Sdrh }
4344f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
4354f26d6c4Sdrh   return sqlite3_bind_int64(p, i, (long long int)iValue);
4364f26d6c4Sdrh }
4374f26d6c4Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){
4384f26d6c4Sdrh   int rc;
4394f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4404f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4414f26d6c4Sdrh   if( rc==SQLITE_OK ){
442f4479501Sdrh     sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
4434f26d6c4Sdrh   }
4444f26d6c4Sdrh   return rc;
4454f26d6c4Sdrh }
4464f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){
4474f26d6c4Sdrh   return vdbeUnbind((Vdbe *)p, i);
4484f26d6c4Sdrh }
4494f26d6c4Sdrh int sqlite3_bind_text(
4504f26d6c4Sdrh   sqlite3_stmt *pStmt,
4514f26d6c4Sdrh   int i,
4524f26d6c4Sdrh   const char *zData,
4534f26d6c4Sdrh   int nData,
4544f26d6c4Sdrh   int eCopy
4554f26d6c4Sdrh ){
4564f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4574f26d6c4Sdrh   Mem *pVar;
4584f26d6c4Sdrh   int rc;
4594f26d6c4Sdrh 
4604f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4614f26d6c4Sdrh   if( rc ){
4624f26d6c4Sdrh     return rc;
4634f26d6c4Sdrh   }
4644f26d6c4Sdrh   pVar = &p->apVar[i-1];
4654f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy);
4664f26d6c4Sdrh   if( rc ){
4674f26d6c4Sdrh     return rc;
4684f26d6c4Sdrh   }
469f4479501Sdrh   rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
4704f26d6c4Sdrh   return rc;
4714f26d6c4Sdrh }
4724f26d6c4Sdrh int sqlite3_bind_text16(
4734f26d6c4Sdrh   sqlite3_stmt *pStmt,
4744f26d6c4Sdrh   int i,
4754f26d6c4Sdrh   const void *zData,
4764f26d6c4Sdrh   int nData,
4774f26d6c4Sdrh   int eCopy
4784f26d6c4Sdrh ){
4794f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
4804f26d6c4Sdrh   Mem *pVar;
481f4479501Sdrh   int rc, txt_enc;
4824f26d6c4Sdrh 
4834f26d6c4Sdrh   rc = vdbeUnbind(p, i);
4844f26d6c4Sdrh   if( rc ){
4854f26d6c4Sdrh     return rc;
4864f26d6c4Sdrh   }
487f4479501Sdrh   pVar = &p->apVar[i-1];
4884f26d6c4Sdrh 
4894f26d6c4Sdrh   /* There may or may not be a byte order mark at the start of the UTF-16.
4904f26d6c4Sdrh   ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the
4914f26d6c4Sdrh   ** actual byte order used by this string. If the string does happen
4924f26d6c4Sdrh   ** to contain a BOM, then move zData so that it points to the first
4934f26d6c4Sdrh   ** byte after the BOM.
4944f26d6c4Sdrh   */
4954f26d6c4Sdrh   txt_enc = sqlite3UtfReadBom(zData, nData);
4964f26d6c4Sdrh   if( txt_enc ){
4974f26d6c4Sdrh     zData = (void *)(((u8 *)zData) + 2);
4984f26d6c4Sdrh     nData -= 2;
4994f26d6c4Sdrh   }else{
500*9c054830Sdrh     txt_enc = SQLITE_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
5014f26d6c4Sdrh   }
5024f26d6c4Sdrh   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
5034f26d6c4Sdrh   if( rc ){
5044f26d6c4Sdrh     return rc;
5054f26d6c4Sdrh   }
506f4479501Sdrh   rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
5074f26d6c4Sdrh   return rc;
5084f26d6c4Sdrh }
509