xref: /sqlite-3.40.0/src/vdbeapi.c (revision c8d985e0)
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 
19eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
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 }
32eec556d3Sshane #endif
33d89bd007Sdrh 
34e30f4426Sdrh /*
35413c3d36Sdrh ** Check on a Vdbe to make sure it has not been finalized.  Log
36413c3d36Sdrh ** an error and return true if it has been finalized (or is otherwise
37413c3d36Sdrh ** invalid).  Return false if it is ok.
38413c3d36Sdrh */
39413c3d36Sdrh static int vdbeSafety(Vdbe *p){
40413c3d36Sdrh   if( p->db==0 ){
41413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
42413c3d36Sdrh     return 1;
43413c3d36Sdrh   }else{
44413c3d36Sdrh     return 0;
45413c3d36Sdrh   }
46413c3d36Sdrh }
47413c3d36Sdrh static int vdbeSafetyNotNull(Vdbe *p){
48413c3d36Sdrh   if( p==0 ){
49413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
50413c3d36Sdrh     return 1;
51413c3d36Sdrh   }else{
52413c3d36Sdrh     return vdbeSafety(p);
53413c3d36Sdrh   }
54413c3d36Sdrh }
55413c3d36Sdrh 
56413c3d36Sdrh /*
57e30f4426Sdrh ** The following routine destroys a virtual machine that is created by
58e30f4426Sdrh ** the sqlite3_compile() routine. The integer returned is an SQLITE_
59e30f4426Sdrh ** success/failure code that describes the result of executing the virtual
60e30f4426Sdrh ** machine.
61e30f4426Sdrh **
62e30f4426Sdrh ** This routine sets the error code and string returned by
63e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
64e30f4426Sdrh */
65e30f4426Sdrh int sqlite3_finalize(sqlite3_stmt *pStmt){
66e30f4426Sdrh   int rc;
67e30f4426Sdrh   if( pStmt==0 ){
6865bafa65Sdrh     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
6965bafa65Sdrh     ** pointer is a harmless no-op. */
70e30f4426Sdrh     rc = SQLITE_OK;
71e30f4426Sdrh   }else{
72e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
73238746a6Sdanielk1977     sqlite3 *db = v->db;
74413c3d36Sdrh     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
754245c405Sdrh     sqlite3_mutex_enter(db->mutex);
76e30f4426Sdrh     rc = sqlite3VdbeFinalize(v);
77238746a6Sdanielk1977     rc = sqlite3ApiExit(db, rc);
784245c405Sdrh     sqlite3LeaveMutexAndCloseZombie(db);
79e30f4426Sdrh   }
80e30f4426Sdrh   return rc;
81e30f4426Sdrh }
82e30f4426Sdrh 
83e30f4426Sdrh /*
84e30f4426Sdrh ** Terminate the current execution of an SQL statement and reset it
85e30f4426Sdrh ** back to its starting state so that it can be reused. A success code from
86e30f4426Sdrh ** the prior execution is returned.
87e30f4426Sdrh **
88e30f4426Sdrh ** This routine sets the error code and string returned by
89e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
90e30f4426Sdrh */
91e30f4426Sdrh int sqlite3_reset(sqlite3_stmt *pStmt){
92e30f4426Sdrh   int rc;
93e30f4426Sdrh   if( pStmt==0 ){
94e30f4426Sdrh     rc = SQLITE_OK;
95e30f4426Sdrh   }else{
96e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
97e30f4426Sdrh     sqlite3_mutex_enter(v->db->mutex);
98c890fec3Sdrh     rc = sqlite3VdbeReset(v);
99124c0b49Sdrh     sqlite3VdbeRewind(v);
100e30f4426Sdrh     assert( (rc & (v->db->errMask))==rc );
101238746a6Sdanielk1977     rc = sqlite3ApiExit(v->db, rc);
102e30f4426Sdrh     sqlite3_mutex_leave(v->db->mutex);
103e30f4426Sdrh   }
104e30f4426Sdrh   return rc;
105e30f4426Sdrh }
106e30f4426Sdrh 
107e30f4426Sdrh /*
108e30f4426Sdrh ** Set all the parameters in the compiled SQL statement to NULL.
109e30f4426Sdrh */
110e30f4426Sdrh int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
111e30f4426Sdrh   int i;
112e30f4426Sdrh   int rc = SQLITE_OK;
1137297d1f0Sdrh   Vdbe *p = (Vdbe*)pStmt;
11418472fa7Sdrh #if SQLITE_THREADSAFE
1157e8b848aSdrh   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
1167e8b848aSdrh #endif
1177e8b848aSdrh   sqlite3_mutex_enter(mutex);
1187297d1f0Sdrh   for(i=0; i<p->nVar; i++){
1197297d1f0Sdrh     sqlite3VdbeMemRelease(&p->aVar[i]);
1207297d1f0Sdrh     p->aVar[i].flags = MEM_Null;
121e30f4426Sdrh   }
122c94b8595Sdan   if( p->isPrepareV2 && p->expmask ){
123c94b8595Sdan     p->expired = 1;
124c94b8595Sdan   }
1257e8b848aSdrh   sqlite3_mutex_leave(mutex);
126e30f4426Sdrh   return rc;
127e30f4426Sdrh }
128e30f4426Sdrh 
129e30f4426Sdrh 
1304f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
1314f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
1324f26d6c4Sdrh ** structure.
1334f26d6c4Sdrh */
1344f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
1354f26d6c4Sdrh   Mem *p = (Mem*)pVal;
1364f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
137b21c8cd4Sdrh     sqlite3VdbeMemExpandBlob(p);
1389310ef23Sdrh     p->flags &= ~MEM_Str;
1391f0feef8Sdrh     p->flags |= MEM_Blob;
14042262536Sdrh     return p->n ? p->z : 0;
1414f26d6c4Sdrh   }else{
1424f26d6c4Sdrh     return sqlite3_value_text(pVal);
1434f26d6c4Sdrh   }
1444f26d6c4Sdrh }
1454f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
146b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
1474f26d6c4Sdrh }
1484f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
149b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
1504f26d6c4Sdrh }
1514f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
1526a6124e2Sdrh   return sqlite3VdbeRealValue((Mem*)pVal);
1534f26d6c4Sdrh }
1544f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
155b27b7f5dSdrh   return (int)sqlite3VdbeIntValue((Mem*)pVal);
1564f26d6c4Sdrh }
157efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
1586a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
1594f26d6c4Sdrh }
1604f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
161b21c8cd4Sdrh   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
1624f26d6c4Sdrh }
1636c62608fSdrh #ifndef SQLITE_OMIT_UTF16
1644f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
165b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1664f26d6c4Sdrh }
167d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){
168b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
169d8123366Sdanielk1977 }
170d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){
171b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
172d8123366Sdanielk1977 }
1736c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
1744f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
175f4479501Sdrh   return pVal->type;
1764f26d6c4Sdrh }
1774f26d6c4Sdrh 
1784f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
1794f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
1804f26d6c4Sdrh ** the function result.
1814c8555fdSdrh **
1824c8555fdSdrh ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
1834c8555fdSdrh ** result as a string or blob but if the string or blob is too large, it
1844c8555fdSdrh ** then sets the error code to SQLITE_TOOBIG
1854f26d6c4Sdrh */
1864c8555fdSdrh static void setResultStrOrError(
1874c8555fdSdrh   sqlite3_context *pCtx,  /* Function context */
1884c8555fdSdrh   const char *z,          /* String pointer */
1894c8555fdSdrh   int n,                  /* Bytes in string, or negative */
1904c8555fdSdrh   u8 enc,                 /* Encoding of z.  0 for BLOBs */
1914c8555fdSdrh   void (*xDel)(void*)     /* Destructor function */
1924c8555fdSdrh ){
1934c8555fdSdrh   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
1944c8555fdSdrh     sqlite3_result_error_toobig(pCtx);
1954c8555fdSdrh   }
1964c8555fdSdrh }
1974f26d6c4Sdrh void sqlite3_result_blob(
1984f26d6c4Sdrh   sqlite3_context *pCtx,
1994f26d6c4Sdrh   const void *z,
2004f26d6c4Sdrh   int n,
201d8123366Sdanielk1977   void (*xDel)(void *)
2024f26d6c4Sdrh ){
2035708d2deSdrh   assert( n>=0 );
20432bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2054c8555fdSdrh   setResultStrOrError(pCtx, z, n, 0, xDel);
2064f26d6c4Sdrh }
2074f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
20832bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2094f26d6c4Sdrh   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
2104f26d6c4Sdrh }
2114f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
21232bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
21369544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
2149b47ee3fSdrh   pCtx->fErrorOrAux = 1;
215b21c8cd4Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
2164f26d6c4Sdrh }
217a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16
2184f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
21932bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
22069544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
2219b47ee3fSdrh   pCtx->fErrorOrAux = 1;
222b21c8cd4Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
2234f26d6c4Sdrh }
224a1686c9aSdanielk1977 #endif
225f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
22632bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2274f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
2284f26d6c4Sdrh }
2294f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
23032bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2314f26d6c4Sdrh   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
2324f26d6c4Sdrh }
2334f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
23432bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
235f4479501Sdrh   sqlite3VdbeMemSetNull(&pCtx->s);
2364f26d6c4Sdrh }
2374f26d6c4Sdrh void sqlite3_result_text(
2384f26d6c4Sdrh   sqlite3_context *pCtx,
2394f26d6c4Sdrh   const char *z,
2404f26d6c4Sdrh   int n,
241d8123366Sdanielk1977   void (*xDel)(void *)
2424f26d6c4Sdrh ){
24332bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2444c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
2454f26d6c4Sdrh }
2466c62608fSdrh #ifndef SQLITE_OMIT_UTF16
2474f26d6c4Sdrh void sqlite3_result_text16(
2484f26d6c4Sdrh   sqlite3_context *pCtx,
2494f26d6c4Sdrh   const void *z,
2504f26d6c4Sdrh   int n,
251d8123366Sdanielk1977   void (*xDel)(void *)
2524f26d6c4Sdrh ){
25332bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2544c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
255d8123366Sdanielk1977 }
256d8123366Sdanielk1977 void sqlite3_result_text16be(
257d8123366Sdanielk1977   sqlite3_context *pCtx,
258d8123366Sdanielk1977   const void *z,
259d8123366Sdanielk1977   int n,
260d8123366Sdanielk1977   void (*xDel)(void *)
261d8123366Sdanielk1977 ){
26232bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2634c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
264d8123366Sdanielk1977 }
265d8123366Sdanielk1977 void sqlite3_result_text16le(
266d8123366Sdanielk1977   sqlite3_context *pCtx,
267d8123366Sdanielk1977   const void *z,
268d8123366Sdanielk1977   int n,
269d8123366Sdanielk1977   void (*xDel)(void *)
270d8123366Sdanielk1977 ){
27132bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
2724c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
2734f26d6c4Sdrh }
2746c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
2754f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
27632bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
277b21c8cd4Sdrh   sqlite3VdbeMemCopy(&pCtx->s, pValue);
2784f26d6c4Sdrh }
279b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
28032bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
281b026e05eSdrh   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
282b026e05eSdrh }
28369544ec9Sdrh void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
28469544ec9Sdrh   pCtx->isError = errCode;
2859b47ee3fSdrh   pCtx->fErrorOrAux = 1;
28651c7d864Sdrh   if( pCtx->s.flags & MEM_Null ){
28751c7d864Sdrh     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
28851c7d864Sdrh                          SQLITE_UTF8, SQLITE_STATIC);
28951c7d864Sdrh   }
29069544ec9Sdrh }
2914f26d6c4Sdrh 
292a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */
293a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){
29432bc3f6eSdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
295bb4957f8Sdrh   pCtx->isError = SQLITE_TOOBIG;
2969b47ee3fSdrh   pCtx->fErrorOrAux = 1;
297bb4957f8Sdrh   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
298bb4957f8Sdrh                        SQLITE_UTF8, SQLITE_STATIC);
299a0206bc8Sdrh }
300a0206bc8Sdrh 
301a1644fd8Sdanielk1977 /* An SQLITE_NOMEM error. */
302a1644fd8Sdanielk1977 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
303a1644fd8Sdanielk1977   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
304a1644fd8Sdanielk1977   sqlite3VdbeMemSetNull(&pCtx->s);
30569544ec9Sdrh   pCtx->isError = SQLITE_NOMEM;
3069b47ee3fSdrh   pCtx->fErrorOrAux = 1;
307a1644fd8Sdanielk1977   pCtx->s.db->mallocFailed = 1;
308a1644fd8Sdanielk1977 }
3094f26d6c4Sdrh 
3105cf53537Sdan /*
3115cf53537Sdan ** This function is called after a transaction has been committed. It
3125cf53537Sdan ** invokes callbacks registered with sqlite3_wal_hook() as required.
3135cf53537Sdan */
3147ed91f23Sdrh static int doWalCallbacks(sqlite3 *db){
3158d22a174Sdan   int rc = SQLITE_OK;
3165cf53537Sdan #ifndef SQLITE_OMIT_WAL
3175cf53537Sdan   int i;
3188d22a174Sdan   for(i=0; i<db->nDb; i++){
3198d22a174Sdan     Btree *pBt = db->aDb[i].pBt;
3208d22a174Sdan     if( pBt ){
3217ed91f23Sdrh       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
3225def0843Sdrh       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
3235def0843Sdrh         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
3248d22a174Sdan       }
3258d22a174Sdan     }
3268d22a174Sdan   }
3275cf53537Sdan #endif
3288d22a174Sdan   return rc;
3298d22a174Sdan }
3308d22a174Sdan 
3314f26d6c4Sdrh /*
3324f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
3334f26d6c4Sdrh ** statement is completely executed or an error occurs.
334b900aaf3Sdrh **
335b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step()
336b900aaf3Sdrh ** API.  The only thing omitted is the automatic recompile if a
337b900aaf3Sdrh ** schema change has occurred.  That detail is handled by the
338b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure.
3394f26d6c4Sdrh */
340b900aaf3Sdrh static int sqlite3Step(Vdbe *p){
3419bb575fdSdrh   sqlite3 *db;
3424f26d6c4Sdrh   int rc;
3434f26d6c4Sdrh 
344a185ddbfSdanielk1977   assert(p);
345a185ddbfSdanielk1977   if( p->magic!=VDBE_MAGIC_RUN ){
3463674bfd1Sdrh     /* We used to require that sqlite3_reset() be called before retrying
347602acb48Sdrh     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
348602acb48Sdrh     ** with version 3.7.0, we changed this so that sqlite3_reset() would
349602acb48Sdrh     ** be called automatically instead of throwing the SQLITE_MISUSE error.
350602acb48Sdrh     ** This "automatic-reset" change is not technically an incompatibility,
351602acb48Sdrh     ** since any application that receives an SQLITE_MISUSE is broken by
352602acb48Sdrh     ** definition.
353602acb48Sdrh     **
354602acb48Sdrh     ** Nevertheless, some published applications that were originally written
355602acb48Sdrh     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
356b8a45bbdSdrh     ** returns, and those were broken by the automatic-reset change.  As a
357602acb48Sdrh     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
358602acb48Sdrh     ** legacy behavior of returning SQLITE_MISUSE for cases where the
359602acb48Sdrh     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
360602acb48Sdrh     ** or SQLITE_BUSY error.
3613674bfd1Sdrh     */
362602acb48Sdrh #ifdef SQLITE_OMIT_AUTORESET
363602acb48Sdrh     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
3643674bfd1Sdrh       sqlite3_reset((sqlite3_stmt*)p);
365602acb48Sdrh     }else{
366602acb48Sdrh       return SQLITE_MISUSE_BKPT;
367602acb48Sdrh     }
368602acb48Sdrh #else
369602acb48Sdrh     sqlite3_reset((sqlite3_stmt*)p);
370602acb48Sdrh #endif
371f1bfe9a8Sdrh   }
372f1bfe9a8Sdrh 
37314d4cc43Sdan   /* Check that malloc() has not failed. If it has, return early. */
37417435752Sdrh   db = p->db;
375c456e57aSdrh   if( db->mallocFailed ){
37614d4cc43Sdan     p->rc = SQLITE_NOMEM;
377c456e57aSdrh     return SQLITE_NOMEM;
378c456e57aSdrh   }
379261919ccSdanielk1977 
380a21c6b6fSdanielk1977   if( p->pc<=0 && p->expired ){
381a21c6b6fSdanielk1977     p->rc = SQLITE_SCHEMA;
382b900aaf3Sdrh     rc = SQLITE_ERROR;
383b900aaf3Sdrh     goto end_of_step;
384a21c6b6fSdanielk1977   }
3851d850a72Sdanielk1977   if( p->pc<0 ){
38615ca1df1Sdrh     /* If there are no other statements currently running, then
38715ca1df1Sdrh     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
38815ca1df1Sdrh     ** from interrupting a statement that has not yet started.
38915ca1df1Sdrh     */
3904f7d3a5fSdrh     if( db->nVdbeActive==0 ){
39115ca1df1Sdrh       db->u1.isInterrupted = 0;
39215ca1df1Sdrh     }
39315ca1df1Sdrh 
394888e16e7Sdrh     assert( db->nVdbeWrite>0 || db->autoCommit==0
395cb3e4b79Sdan         || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
396cb3e4b79Sdan     );
3971da40a38Sdan 
39819e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
39919e2d37fSdrh     if( db->xProfile && !db->init.busy ){
400b7e8ea20Sdrh       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
40119e2d37fSdrh     }
40219e2d37fSdrh #endif
403c16a03b5Sdrh 
4044f7d3a5fSdrh     db->nVdbeActive++;
4054f7d3a5fSdrh     if( p->readOnly==0 ) db->nVdbeWrite++;
4061713afb0Sdrh     if( p->bIsReader ) db->nVdbeRead++;
4071d850a72Sdanielk1977     p->pc = 0;
4081d850a72Sdanielk1977   }
409b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
4104f26d6c4Sdrh   if( p->explain ){
4114f26d6c4Sdrh     rc = sqlite3VdbeList(p);
412b7f9164eSdrh   }else
413b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
414b7f9164eSdrh   {
4154f7d3a5fSdrh     db->nVdbeExec++;
4164f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
4174f7d3a5fSdrh     db->nVdbeExec--;
4184f26d6c4Sdrh   }
4194f26d6c4Sdrh 
42019e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
42119e2d37fSdrh   /* Invoke the profile callback if there is one
42219e2d37fSdrh   */
4236ab3a2ecSdanielk1977   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
424b7e8ea20Sdrh     sqlite3_int64 iNow;
425b7e8ea20Sdrh     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
426df0db0feSdrh     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
42719e2d37fSdrh   }
42819e2d37fSdrh #endif
42919e2d37fSdrh 
4308d22a174Sdan   if( rc==SQLITE_DONE ){
4318d22a174Sdan     assert( p->rc==SQLITE_OK );
4327ed91f23Sdrh     p->rc = doWalCallbacks(db);
4338d22a174Sdan     if( p->rc!=SQLITE_OK ){
4348d22a174Sdan       rc = SQLITE_ERROR;
4358d22a174Sdan     }
4368d22a174Sdan   }
4378d22a174Sdan 
43880cc85b3Sdrh   db->errCode = rc;
439357864ecSdanielk1977   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
440357864ecSdanielk1977     p->rc = SQLITE_NOMEM;
4414f26d6c4Sdrh   }
442357864ecSdanielk1977 end_of_step:
443357864ecSdanielk1977   /* At this point local variable rc holds the value that should be
444357864ecSdanielk1977   ** returned if this statement was compiled using the legacy
445357864ecSdanielk1977   ** sqlite3_prepare() interface. According to the docs, this can only
446357864ecSdanielk1977   ** be one of the values in the first assert() below. Variable p->rc
447357864ecSdanielk1977   ** contains the value that would be returned if sqlite3_finalize()
448357864ecSdanielk1977   ** were called on statement p.
449357864ecSdanielk1977   */
450357864ecSdanielk1977   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
451357864ecSdanielk1977        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
452357864ecSdanielk1977   );
453357864ecSdanielk1977   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
454357864ecSdanielk1977   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
455357864ecSdanielk1977     /* If this statement was prepared using sqlite3_prepare_v2(), and an
45648864df9Smistachkin     ** error has occurred, then return the error code in p->rc to the
457357864ecSdanielk1977     ** caller. Set the error code in the database handle to the same value.
458357864ecSdanielk1977     */
459029ead64Sdan     rc = sqlite3VdbeTransferError(p);
460357864ecSdanielk1977   }
461357864ecSdanielk1977   return (rc&db->errMask);
462b900aaf3Sdrh }
463b900aaf3Sdrh 
464b900aaf3Sdrh /*
465b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step().  Call
466b900aaf3Sdrh ** sqlite3Step() to do most of the work.  If a schema error occurs,
467b900aaf3Sdrh ** call sqlite3Reprepare() and try again.
468b900aaf3Sdrh */
469b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
470a6129fa7Sdrh   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
471a6129fa7Sdrh   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
472a6129fa7Sdrh   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
473a6129fa7Sdrh   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
474a6129fa7Sdrh   sqlite3 *db;             /* The database connection */
475a6129fa7Sdrh 
476413c3d36Sdrh   if( vdbeSafetyNotNull(v) ){
477413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
478413c3d36Sdrh   }
479413c3d36Sdrh   db = v->db;
4808e556520Sdanielk1977   sqlite3_mutex_enter(db->mutex);
48137f58e99Sdrh   v->doingRerun = 0;
482b900aaf3Sdrh   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
48324e713d9Sdrh          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
484a6129fa7Sdrh          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
485b900aaf3Sdrh     sqlite3_reset(pStmt);
48637f58e99Sdrh     v->doingRerun = 1;
48708f5f4c5Sdan     assert( v->expired==0 );
488b900aaf3Sdrh   }
489*a3cc007dSdrh   if( rc2!=SQLITE_OK ){
4908e556520Sdanielk1977     /* This case occurs after failing to recompile an sql statement.
4918e556520Sdanielk1977     ** The error message from the SQL compiler has already been loaded
4928e556520Sdanielk1977     ** into the database handle. This block copies the error message
4938e556520Sdanielk1977     ** from the database handle into the statement and sets the statement
4948e556520Sdanielk1977     ** program counter to 0 to ensure that when the statement is
4958e556520Sdanielk1977     ** finalized or reset the parser error message is available via
4968e556520Sdanielk1977     ** sqlite3_errmsg() and sqlite3_errcode().
4978e556520Sdanielk1977     */
4988e556520Sdanielk1977     const char *zErr = (const char *)sqlite3_value_text(db->pErr);
499*a3cc007dSdrh     assert( zErr!=0 || db->mallocFailed );
500633e6d57Sdrh     sqlite3DbFree(db, v->zErrMsg);
5018e556520Sdanielk1977     if( !db->mallocFailed ){
5028e556520Sdanielk1977       v->zErrMsg = sqlite3DbStrDup(db, zErr);
503a6129fa7Sdrh       v->rc = rc2;
5048e556520Sdanielk1977     } else {
5058e556520Sdanielk1977       v->zErrMsg = 0;
506a6129fa7Sdrh       v->rc = rc = SQLITE_NOMEM;
5078e556520Sdanielk1977     }
5088e556520Sdanielk1977   }
5098e556520Sdanielk1977   rc = sqlite3ApiExit(db, rc);
5108e556520Sdanielk1977   sqlite3_mutex_leave(db->mutex);
511b900aaf3Sdrh   return rc;
512b900aaf3Sdrh }
5134f26d6c4Sdrh 
51495a7b3e3Sdrh 
5154f26d6c4Sdrh /*
516eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
517eb2e176aSdrh ** pointer to it.
518eb2e176aSdrh */
519eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
520eb2e176aSdrh   assert( p && p->pFunc );
521eb2e176aSdrh   return p->pFunc->pUserData;
522eb2e176aSdrh }
523eb2e176aSdrh 
524eb2e176aSdrh /*
525fa4a4b91Sdrh ** Extract the user data from a sqlite3_context structure and return a
526fa4a4b91Sdrh ** pointer to it.
5279f129f46Sdrh **
5289f129f46Sdrh ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
5299f129f46Sdrh ** returns a copy of the pointer to the database connection (the 1st
5309f129f46Sdrh ** parameter) of the sqlite3_create_function() and
5319f129f46Sdrh ** sqlite3_create_function16() routines that originally registered the
5329f129f46Sdrh ** application defined function.
533fa4a4b91Sdrh */
534fa4a4b91Sdrh sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
535fa4a4b91Sdrh   assert( p && p->pFunc );
536fa4a4b91Sdrh   return p->s.db;
537fa4a4b91Sdrh }
538fa4a4b91Sdrh 
539fa4a4b91Sdrh /*
54095a7b3e3Sdrh ** Return the current time for a statement
54195a7b3e3Sdrh */
54295a7b3e3Sdrh sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
54395a7b3e3Sdrh   Vdbe *v = p->pVdbe;
54495a7b3e3Sdrh   int rc;
54595a7b3e3Sdrh   if( v->iCurrentTime==0 ){
54695a7b3e3Sdrh     rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
54795a7b3e3Sdrh     if( rc ) v->iCurrentTime = 0;
54895a7b3e3Sdrh   }
54995a7b3e3Sdrh   return v->iCurrentTime;
55095a7b3e3Sdrh }
55195a7b3e3Sdrh 
55295a7b3e3Sdrh /*
553b7481e70Sdrh ** The following is the implementation of an SQL function that always
554b7481e70Sdrh ** fails with an error message stating that the function is used in the
555b7481e70Sdrh ** wrong context.  The sqlite3_overload_function() API might construct
556b7481e70Sdrh ** SQL function that use this routine so that the functions will exist
557b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction
558b7481e70Sdrh ** method of virtual tables.
559b7481e70Sdrh */
560b7481e70Sdrh void sqlite3InvalidFunction(
561b7481e70Sdrh   sqlite3_context *context,  /* The function calling context */
56262c14b34Sdanielk1977   int NotUsed,               /* Number of arguments to the function */
56362c14b34Sdanielk1977   sqlite3_value **NotUsed2   /* Value of each argument */
564b7481e70Sdrh ){
565b7481e70Sdrh   const char *zName = context->pFunc->zName;
566b7481e70Sdrh   char *zErr;
56762c14b34Sdanielk1977   UNUSED_PARAMETER2(NotUsed, NotUsed2);
568bc6160b0Sdrh   zErr = sqlite3_mprintf(
569b7481e70Sdrh       "unable to use function %s in the requested context", zName);
570b7481e70Sdrh   sqlite3_result_error(context, zErr, -1);
5711e536953Sdanielk1977   sqlite3_free(zErr);
572b7481e70Sdrh }
573b7481e70Sdrh 
574b7481e70Sdrh /*
575eb2e176aSdrh ** Allocate or return the aggregate context for a user function.  A new
576eb2e176aSdrh ** context is allocated on the first call.  Subsequent calls return the
577eb2e176aSdrh ** same context that was returned on prior calls.
578eb2e176aSdrh */
579eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
580b21c8cd4Sdrh   Mem *pMem;
581825c662eSdrh   assert( p && p->pFunc && p->pFunc->xStep );
582b21c8cd4Sdrh   assert( sqlite3_mutex_held(p->s.db->mutex) );
583b21c8cd4Sdrh   pMem = p->pMem;
584d68eee04Sdrh   testcase( nByte<0 );
585a10a34b8Sdrh   if( (pMem->flags & MEM_Agg)==0 ){
586d68eee04Sdrh     if( nByte<=0 ){
5875f096135Sdanielk1977       sqlite3VdbeMemReleaseExternal(pMem);
5885f096135Sdanielk1977       pMem->flags = MEM_Null;
589a10a34b8Sdrh       pMem->z = 0;
590a10a34b8Sdrh     }else{
5915f096135Sdanielk1977       sqlite3VdbeMemGrow(pMem, nByte, 0);
592abfcea25Sdrh       pMem->flags = MEM_Agg;
5933c024d69Sdrh       pMem->u.pDef = p->pFunc;
5945f096135Sdanielk1977       if( pMem->z ){
5955f096135Sdanielk1977         memset(pMem->z, 0, nByte);
5965f096135Sdanielk1977       }
597eb2e176aSdrh     }
598eb2e176aSdrh   }
599abfcea25Sdrh   return (void*)pMem->z;
600eb2e176aSdrh }
601eb2e176aSdrh 
602eb2e176aSdrh /*
603682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to
604682f68b0Sdanielk1977 ** the user-function defined by pCtx.
605682f68b0Sdanielk1977 */
606682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
6070c547799Sdan   AuxData *pAuxData;
608b21c8cd4Sdrh 
609b21c8cd4Sdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6100c547799Sdan   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
6110c547799Sdan     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
612682f68b0Sdanielk1977   }
6130c547799Sdan 
6140c547799Sdan   return (pAuxData ? pAuxData->pAux : 0);
615682f68b0Sdanielk1977 }
616682f68b0Sdanielk1977 
617682f68b0Sdanielk1977 /*
618682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th
619682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
620682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
621682f68b0Sdanielk1977 */
622682f68b0Sdanielk1977 void sqlite3_set_auxdata(
623682f68b0Sdanielk1977   sqlite3_context *pCtx,
624682f68b0Sdanielk1977   int iArg,
625682f68b0Sdanielk1977   void *pAux,
626682f68b0Sdanielk1977   void (*xDelete)(void*)
627682f68b0Sdanielk1977 ){
6280c547799Sdan   AuxData *pAuxData;
6290c547799Sdan   Vdbe *pVdbe = pCtx->pVdbe;
630682f68b0Sdanielk1977 
631b21c8cd4Sdrh   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6320c547799Sdan   if( iArg<0 ) goto failed;
633682f68b0Sdanielk1977 
6340c547799Sdan   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
6350c547799Sdan     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
6360c547799Sdan   }
6370c547799Sdan   if( pAuxData==0 ){
6380c547799Sdan     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
6390c547799Sdan     if( !pAuxData ) goto failed;
6400c547799Sdan     pAuxData->iOp = pCtx->iOp;
6410c547799Sdan     pAuxData->iArg = iArg;
6420c547799Sdan     pAuxData->pNext = pVdbe->pAuxData;
6430c547799Sdan     pVdbe->pAuxData = pAuxData;
6449b47ee3fSdrh     if( pCtx->fErrorOrAux==0 ){
6459b47ee3fSdrh       pCtx->isError = 0;
6469b47ee3fSdrh       pCtx->fErrorOrAux = 1;
6479b47ee3fSdrh     }
6480c547799Sdan   }else if( pAuxData->xDelete ){
649682f68b0Sdanielk1977     pAuxData->xDelete(pAuxData->pAux);
650682f68b0Sdanielk1977   }
6510c547799Sdan 
652682f68b0Sdanielk1977   pAuxData->pAux = pAux;
653682f68b0Sdanielk1977   pAuxData->xDelete = xDelete;
654e0fc5261Sdanielk1977   return;
655e0fc5261Sdanielk1977 
656e0fc5261Sdanielk1977 failed:
657e0fc5261Sdanielk1977   if( xDelete ){
658e0fc5261Sdanielk1977     xDelete(pAux);
659e0fc5261Sdanielk1977   }
660682f68b0Sdanielk1977 }
661682f68b0Sdanielk1977 
662eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
663682f68b0Sdanielk1977 /*
664eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been
665eb2e176aSdrh ** called.
666eb2e176aSdrh **
667cf85a51cSdrh ** This function is deprecated.  Do not use it for new code.  It is
668cf85a51cSdrh ** provide only to avoid breaking legacy code.  New aggregate function
669cf85a51cSdrh ** implementations should keep their own counts within their aggregate
670cf85a51cSdrh ** context.
671eb2e176aSdrh */
672eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
673e34c647eSshane   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
674abfcea25Sdrh   return p->pMem->n;
675eb2e176aSdrh }
676eec556d3Sshane #endif
677eb2e176aSdrh 
678eb2e176aSdrh /*
6794f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
6804f26d6c4Sdrh */
6814f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
6824f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
6831bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
6844f26d6c4Sdrh }
6854f26d6c4Sdrh 
6864f26d6c4Sdrh /*
6874f26d6c4Sdrh ** Return the number of values available from the current row of the
6884f26d6c4Sdrh ** currently executing statement pStmt.
6894f26d6c4Sdrh */
6904f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
6914f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
692d4e70ebdSdrh   if( pVm==0 || pVm->pResultSet==0 ) return 0;
6934f26d6c4Sdrh   return pVm->nResColumn;
6944f26d6c4Sdrh }
6954f26d6c4Sdrh 
69646c47d46Sdan /*
69746c47d46Sdan ** Return a pointer to static memory containing an SQL NULL value.
69846c47d46Sdan */
69946c47d46Sdan static const Mem *columnNullValue(void){
70046c47d46Sdan   /* Even though the Mem structure contains an element
70146c47d46Sdan   ** of type i64, on certain architecture (x86) with certain compiler
70246c47d46Sdan   ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
70346c47d46Sdan   ** instead of an 8-byte one. This all works fine, except that when
70446c47d46Sdan   ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
70546c47d46Sdan   ** that a Mem structure is located on an 8-byte boundary. To prevent
70646c47d46Sdan   ** this assert() from failing, when building with SQLITE_DEBUG defined
70746c47d46Sdan   ** using gcc, force nullMem to be 8-byte aligned using the magical
70846c47d46Sdan   ** __attribute__((aligned(8))) macro.  */
70946c47d46Sdan   static const Mem nullMem
71046c47d46Sdan #if defined(SQLITE_DEBUG) && defined(__GNUC__)
71146c47d46Sdan   __attribute__((aligned(8)))
71246c47d46Sdan #endif
71346c47d46Sdan     = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
71446c47d46Sdan   return &nullMem;
71546c47d46Sdan }
7164f26d6c4Sdrh 
7174f26d6c4Sdrh /*
7184f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
7194f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
7204f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
7214f26d6c4Sdrh ** of NULL.
7224f26d6c4Sdrh */
7234f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
72432bc3f6eSdrh   Vdbe *pVm;
72532bc3f6eSdrh   Mem *pOut;
72632bc3f6eSdrh 
72732bc3f6eSdrh   pVm = (Vdbe *)pStmt;
728d4e70ebdSdrh   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
72932bc3f6eSdrh     sqlite3_mutex_enter(pVm->db->mutex);
730d4e70ebdSdrh     pOut = &pVm->pResultSet[i];
73132bc3f6eSdrh   }else{
7329b4ea4a5Sdrh     if( pVm && ALWAYS(pVm->db) ){
73327641703Sdrh       sqlite3_mutex_enter(pVm->db->mutex);
7344f26d6c4Sdrh       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
73527641703Sdrh     }
73646c47d46Sdan     pOut = (Mem*)columnNullValue();
7374f26d6c4Sdrh   }
73832bc3f6eSdrh   return pOut;
7394f26d6c4Sdrh }
7404f26d6c4Sdrh 
7412e588c75Sdanielk1977 /*
7422e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a
7432e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the
7442e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If
7452e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
7462e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM.
7472e588c75Sdanielk1977 **
74832bc3f6eSdrh ** Specifically, this is called from within:
7492e588c75Sdanielk1977 **
7502e588c75Sdanielk1977 **     sqlite3_column_int()
7512e588c75Sdanielk1977 **     sqlite3_column_int64()
7522e588c75Sdanielk1977 **     sqlite3_column_text()
7532e588c75Sdanielk1977 **     sqlite3_column_text16()
7542e588c75Sdanielk1977 **     sqlite3_column_real()
7552e588c75Sdanielk1977 **     sqlite3_column_bytes()
7562e588c75Sdanielk1977 **     sqlite3_column_bytes16()
75742262536Sdrh **     sqiite3_column_blob()
7582e588c75Sdanielk1977 */
7592e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt)
7602e588c75Sdanielk1977 {
7612e588c75Sdanielk1977   /* If malloc() failed during an encoding conversion within an
7622e588c75Sdanielk1977   ** sqlite3_column_XXX API, then set the return code of the statement to
7632e588c75Sdanielk1977   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
7642e588c75Sdanielk1977   ** and _finalize() will return NOMEM.
7652e588c75Sdanielk1977   */
76654f0198eSdanielk1977   Vdbe *p = (Vdbe *)pStmt;
76732bc3f6eSdrh   if( p ){
76832bc3f6eSdrh     p->rc = sqlite3ApiExit(p->db, p->rc);
76932bc3f6eSdrh     sqlite3_mutex_leave(p->db->mutex);
77032bc3f6eSdrh   }
7712e588c75Sdanielk1977 }
7722e588c75Sdanielk1977 
7734f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
7744f26d6c4Sdrh ** The following routines are used to access elements of the current row
7754f26d6c4Sdrh ** in the result set.
7764f26d6c4Sdrh */
777c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
7782e588c75Sdanielk1977   const void *val;
7792e588c75Sdanielk1977   val = sqlite3_value_blob( columnMem(pStmt,i) );
780c9cf901dSdanielk1977   /* Even though there is no encoding conversion, value_blob() might
781c9cf901dSdanielk1977   ** need to call malloc() to expand the result of a zeroblob()
782c9cf901dSdanielk1977   ** expression.
783c9cf901dSdanielk1977   */
784c9cf901dSdanielk1977   columnMallocFailure(pStmt);
7852e588c75Sdanielk1977   return val;
786c572ef7fSdanielk1977 }
7874f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
7882e588c75Sdanielk1977   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
7892e588c75Sdanielk1977   columnMallocFailure(pStmt);
7902e588c75Sdanielk1977   return val;
7914f26d6c4Sdrh }
7924f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
7932e588c75Sdanielk1977   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
7942e588c75Sdanielk1977   columnMallocFailure(pStmt);
7952e588c75Sdanielk1977   return val;
7964f26d6c4Sdrh }
7974f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
7982e588c75Sdanielk1977   double val = sqlite3_value_double( columnMem(pStmt,i) );
7992e588c75Sdanielk1977   columnMallocFailure(pStmt);
8002e588c75Sdanielk1977   return val;
8014f26d6c4Sdrh }
8024f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
8032e588c75Sdanielk1977   int val = sqlite3_value_int( columnMem(pStmt,i) );
8042e588c75Sdanielk1977   columnMallocFailure(pStmt);
8052e588c75Sdanielk1977   return val;
8064f26d6c4Sdrh }
807efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
8082e588c75Sdanielk1977   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
8092e588c75Sdanielk1977   columnMallocFailure(pStmt);
8102e588c75Sdanielk1977   return val;
8114f26d6c4Sdrh }
8124f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
8132e588c75Sdanielk1977   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
8142e588c75Sdanielk1977   columnMallocFailure(pStmt);
8152e588c75Sdanielk1977   return val;
8164f26d6c4Sdrh }
817d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
818d0ffa1e8Sdanielk1977   Mem *pOut = columnMem(pStmt, i);
819d0ffa1e8Sdanielk1977   if( pOut->flags&MEM_Static ){
820d0ffa1e8Sdanielk1977     pOut->flags &= ~MEM_Static;
821d0ffa1e8Sdanielk1977     pOut->flags |= MEM_Ephem;
822d0ffa1e8Sdanielk1977   }
82332bc3f6eSdrh   columnMallocFailure(pStmt);
824d0ffa1e8Sdanielk1977   return (sqlite3_value *)pOut;
825d1e4733dSdrh }
8266c62608fSdrh #ifndef SQLITE_OMIT_UTF16
8274f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
8282e588c75Sdanielk1977   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
8292e588c75Sdanielk1977   columnMallocFailure(pStmt);
8302e588c75Sdanielk1977   return val;
8314f26d6c4Sdrh }
8326c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
8334f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
83432bc3f6eSdrh   int iType = sqlite3_value_type( columnMem(pStmt,i) );
83532bc3f6eSdrh   columnMallocFailure(pStmt);
83632bc3f6eSdrh   return iType;
8374f26d6c4Sdrh }
8384f26d6c4Sdrh 
8395e2517e1Sdrh /*
8405e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
8415e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
842e590fbdeSdrh **
843e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
844e590fbdeSdrh ** name is returned.  Here are the names:
845e590fbdeSdrh **
846e590fbdeSdrh **    0      The column name as it should be displayed for output
847e590fbdeSdrh **    1      The datatype name for the column
848e590fbdeSdrh **    2      The name of the database that the column derives from
849e590fbdeSdrh **    3      The name of the table that the column derives from
850e590fbdeSdrh **    4      The name of the table column that the result column derives from
851e590fbdeSdrh **
852e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
853e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
8545e2517e1Sdrh */
8555e2517e1Sdrh static const void *columnName(
8565e2517e1Sdrh   sqlite3_stmt *pStmt,
8575e2517e1Sdrh   int N,
8585e2517e1Sdrh   const void *(*xFunc)(Mem*),
8595e2517e1Sdrh   int useType
8605e2517e1Sdrh ){
86132bc3f6eSdrh   const void *ret = 0;
8625e2517e1Sdrh   Vdbe *p = (Vdbe *)pStmt;
86332bc3f6eSdrh   int n;
864c6c7fd51Sdrh   sqlite3 *db = p->db;
8655e2517e1Sdrh 
866c6c7fd51Sdrh   assert( db!=0 );
86732bc3f6eSdrh   n = sqlite3_column_count(pStmt);
86832bc3f6eSdrh   if( N<n && N>=0 ){
869e590fbdeSdrh     N += useType*n;
870c6c7fd51Sdrh     sqlite3_mutex_enter(db->mutex);
871c6c7fd51Sdrh     assert( db->mallocFailed==0 );
87200fd957bSdanielk1977     ret = xFunc(&p->aColName[N]);
87332bc3f6eSdrh      /* A malloc may have failed inside of the xFunc() call. If this
87432bc3f6eSdrh     ** is the case, clear the mallocFailed flag and return NULL.
87500fd957bSdanielk1977     */
876c6c7fd51Sdrh     if( db->mallocFailed ){
877c6c7fd51Sdrh       db->mallocFailed = 0;
87832bc3f6eSdrh       ret = 0;
87932bc3f6eSdrh     }
880c6c7fd51Sdrh     sqlite3_mutex_leave(db->mutex);
88132bc3f6eSdrh   }
88200fd957bSdanielk1977   return ret;
8835e2517e1Sdrh }
8845e2517e1Sdrh 
8854f26d6c4Sdrh /*
8864f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
8874f26d6c4Sdrh ** statement pStmt.
8884f26d6c4Sdrh */
8894f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
890955de52cSdanielk1977   return columnName(
891955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
8924f26d6c4Sdrh }
893e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
894e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
895955de52cSdanielk1977   return columnName(
896955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
897e590fbdeSdrh }
898e590fbdeSdrh #endif
8994f26d6c4Sdrh 
9004f26d6c4Sdrh /*
9013f913576Sdrh ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
9023f913576Sdrh ** not define OMIT_DECLTYPE.
9033f913576Sdrh */
9043f913576Sdrh #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
9053f913576Sdrh # error "Must not define both SQLITE_OMIT_DECLTYPE \
9063f913576Sdrh          and SQLITE_ENABLE_COLUMN_METADATA"
9073f913576Sdrh #endif
9083f913576Sdrh 
9093f913576Sdrh #ifndef SQLITE_OMIT_DECLTYPE
9103f913576Sdrh /*
9116c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
912e590fbdeSdrh ** of the result set of SQL statement pStmt.
9136c62608fSdrh */
9146c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
915955de52cSdanielk1977   return columnName(
916955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
9176c62608fSdrh }
9186c62608fSdrh #ifndef SQLITE_OMIT_UTF16
91976d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
920955de52cSdanielk1977   return columnName(
921955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
9224f26d6c4Sdrh }
9236c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
9243f913576Sdrh #endif /* SQLITE_OMIT_DECLTYPE */
9254f26d6c4Sdrh 
9264b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA
927e590fbdeSdrh /*
928e590fbdeSdrh ** Return the name of the database from which a result column derives.
929e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
930e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
931e590fbdeSdrh */
932e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
933955de52cSdanielk1977   return columnName(
934955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
935e590fbdeSdrh }
936e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
937e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
938955de52cSdanielk1977   return columnName(
939955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
940e590fbdeSdrh }
941e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
942e590fbdeSdrh 
943e590fbdeSdrh /*
944e590fbdeSdrh ** Return the name of the table from which a result column derives.
945e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
946e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
947e590fbdeSdrh */
948e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
949955de52cSdanielk1977   return columnName(
950955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
951e590fbdeSdrh }
952e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
953e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
954955de52cSdanielk1977   return columnName(
955955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
956e590fbdeSdrh }
957e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
958e590fbdeSdrh 
959e590fbdeSdrh /*
960e590fbdeSdrh ** Return the name of the table column from which a result column derives.
961e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
962e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column.
963e590fbdeSdrh */
964e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
965955de52cSdanielk1977   return columnName(
966955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
967e590fbdeSdrh }
968e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
969e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
970955de52cSdanielk1977   return columnName(
971955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
972e590fbdeSdrh }
973e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
9744b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
975e590fbdeSdrh 
976e590fbdeSdrh 
9774f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
9784f26d6c4Sdrh **
9794f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
9804f26d6c4Sdrh */
9814f26d6c4Sdrh /*
9824f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
9834f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
9844f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
9854f26d6c4Sdrh **
986488e7b63Sdrh ** A successful evaluation of this routine acquires the mutex on p.
987488e7b63Sdrh ** the mutex is released if any kind of error occurs.
988488e7b63Sdrh **
9894f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
9904f26d6c4Sdrh ** value in any case.
9914f26d6c4Sdrh */
9924f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
9934f26d6c4Sdrh   Mem *pVar;
994413c3d36Sdrh   if( vdbeSafetyNotNull(p) ){
995413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
996413c3d36Sdrh   }
997488e7b63Sdrh   sqlite3_mutex_enter(p->db->mutex);
998488e7b63Sdrh   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
999488e7b63Sdrh     sqlite3Error(p->db, SQLITE_MISUSE, 0);
1000488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
1001413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE,
1002413c3d36Sdrh         "bind on a busy prepared statement: [%s]", p->zSql);
1003413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
10044f26d6c4Sdrh   }
10054f26d6c4Sdrh   if( i<1 || i>p->nVar ){
10064f26d6c4Sdrh     sqlite3Error(p->db, SQLITE_RANGE, 0);
1007488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
10084f26d6c4Sdrh     return SQLITE_RANGE;
10094f26d6c4Sdrh   }
10104f26d6c4Sdrh   i--;
1011290c1948Sdrh   pVar = &p->aVar[i];
1012d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
10134f26d6c4Sdrh   pVar->flags = MEM_Null;
10144f26d6c4Sdrh   sqlite3Error(p->db, SQLITE_OK, 0);
1015937d0deaSdan 
10161d2ce4f8Sdan   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
10171d2ce4f8Sdan   ** binding a new value to this variable invalidates the current query plan.
1018a7044007Sdrh   **
1019a7044007Sdrh   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1020a7044007Sdrh   ** parameter in the WHERE clause might influence the choice of query plan
1021a7044007Sdrh   ** for a statement, then the statement will be automatically recompiled,
1022a7044007Sdrh   ** as if there had been a schema change, on the first sqlite3_step() call
1023a7044007Sdrh   ** following any change to the bindings of that parameter.
10241d2ce4f8Sdan   */
1025823e09abSdrh   if( p->isPrepareV2 &&
1026823e09abSdrh      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
1027823e09abSdrh   ){
1028937d0deaSdan     p->expired = 1;
1029937d0deaSdan   }
10304f26d6c4Sdrh   return SQLITE_OK;
10314f26d6c4Sdrh }
10324f26d6c4Sdrh 
10334f26d6c4Sdrh /*
10345e2517e1Sdrh ** Bind a text or BLOB value.
10354f26d6c4Sdrh */
10365e2517e1Sdrh static int bindText(
1037605264d2Sdrh   sqlite3_stmt *pStmt,   /* The statement to bind against */
1038605264d2Sdrh   int i,                 /* Index of the parameter to bind */
1039605264d2Sdrh   const void *zData,     /* Pointer to the data to be bound */
1040605264d2Sdrh   int nData,             /* Number of bytes of data to be bound */
1041605264d2Sdrh   void (*xDel)(void*),   /* Destructor for the data */
1042b27b7f5dSdrh   u8 encoding            /* Encoding for the data */
10434f26d6c4Sdrh ){
10444f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
10454f26d6c4Sdrh   Mem *pVar;
10464f26d6c4Sdrh   int rc;
10474f26d6c4Sdrh 
10484f26d6c4Sdrh   rc = vdbeUnbind(p, i);
1049488e7b63Sdrh   if( rc==SQLITE_OK ){
1050488e7b63Sdrh     if( zData!=0 ){
1051290c1948Sdrh       pVar = &p->aVar[i-1];
1052b21c8cd4Sdrh       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
10535e2517e1Sdrh       if( rc==SQLITE_OK && encoding!=0 ){
1054b21c8cd4Sdrh         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
10555e2517e1Sdrh       }
10561e536953Sdanielk1977       sqlite3Error(p->db, rc, 0);
1057605264d2Sdrh       rc = sqlite3ApiExit(p->db, rc);
105827641703Sdrh     }
1059605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
106094bb2ba6Sdrh   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
10616fec9ee3Sdrh     xDel((void*)zData);
1062488e7b63Sdrh   }
1063605264d2Sdrh   return rc;
10645e2517e1Sdrh }
10655e2517e1Sdrh 
10665e2517e1Sdrh 
10675e2517e1Sdrh /*
10685e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
10695e2517e1Sdrh */
10705e2517e1Sdrh int sqlite3_bind_blob(
10715e2517e1Sdrh   sqlite3_stmt *pStmt,
10725e2517e1Sdrh   int i,
10735e2517e1Sdrh   const void *zData,
10745e2517e1Sdrh   int nData,
10755e2517e1Sdrh   void (*xDel)(void*)
10765e2517e1Sdrh ){
10775e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
10785e2517e1Sdrh }
10794f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
10804f26d6c4Sdrh   int rc;
10814f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
10824f26d6c4Sdrh   rc = vdbeUnbind(p, i);
10834f26d6c4Sdrh   if( rc==SQLITE_OK ){
1084290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1085605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1086488e7b63Sdrh   }
1087f4618891Sdanielk1977   return rc;
10884f26d6c4Sdrh }
10894f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
1090efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
10914f26d6c4Sdrh }
1092efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
10934f26d6c4Sdrh   int rc;
10944f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
10954f26d6c4Sdrh   rc = vdbeUnbind(p, i);
10964f26d6c4Sdrh   if( rc==SQLITE_OK ){
1097290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
1098605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1099488e7b63Sdrh   }
11004f26d6c4Sdrh   return rc;
11014f26d6c4Sdrh }
1102605264d2Sdrh int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1103605264d2Sdrh   int rc;
1104605264d2Sdrh   Vdbe *p = (Vdbe*)pStmt;
1105605264d2Sdrh   rc = vdbeUnbind(p, i);
1106488e7b63Sdrh   if( rc==SQLITE_OK ){
1107605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1108488e7b63Sdrh   }
1109605264d2Sdrh   return rc;
11104f26d6c4Sdrh }
11114f26d6c4Sdrh int sqlite3_bind_text(
11124f26d6c4Sdrh   sqlite3_stmt *pStmt,
11134f26d6c4Sdrh   int i,
11144f26d6c4Sdrh   const char *zData,
11154f26d6c4Sdrh   int nData,
1116d8123366Sdanielk1977   void (*xDel)(void*)
11174f26d6c4Sdrh ){
11185e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
11194f26d6c4Sdrh }
11206c62608fSdrh #ifndef SQLITE_OMIT_UTF16
11214f26d6c4Sdrh int sqlite3_bind_text16(
11224f26d6c4Sdrh   sqlite3_stmt *pStmt,
11234f26d6c4Sdrh   int i,
11244f26d6c4Sdrh   const void *zData,
11254f26d6c4Sdrh   int nData,
1126d8123366Sdanielk1977   void (*xDel)(void*)
11274f26d6c4Sdrh ){
11285e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
11294f26d6c4Sdrh }
11306c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
113126e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
113226e4144dSdanielk1977   int rc;
113329def560Sdrh   switch( pValue->type ){
113429def560Sdrh     case SQLITE_INTEGER: {
113529def560Sdrh       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
113629def560Sdrh       break;
113729def560Sdrh     }
113829def560Sdrh     case SQLITE_FLOAT: {
113929def560Sdrh       rc = sqlite3_bind_double(pStmt, i, pValue->r);
114029def560Sdrh       break;
114129def560Sdrh     }
114229def560Sdrh     case SQLITE_BLOB: {
114329def560Sdrh       if( pValue->flags & MEM_Zero ){
114429def560Sdrh         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
114529def560Sdrh       }else{
114629def560Sdrh         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
114729def560Sdrh       }
114829def560Sdrh       break;
114929def560Sdrh     }
115029def560Sdrh     case SQLITE_TEXT: {
1151ac80db78Sdrh       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
1152ac80db78Sdrh                               pValue->enc);
1153ac80db78Sdrh       break;
1154ac80db78Sdrh     }
1155ac80db78Sdrh     default: {
1156ac80db78Sdrh       rc = sqlite3_bind_null(pStmt, i);
115729def560Sdrh       break;
115829def560Sdrh     }
11590f5ea0b3Sdrh   }
116026e4144dSdanielk1977   return rc;
116126e4144dSdanielk1977 }
1162b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1163b026e05eSdrh   int rc;
1164b026e05eSdrh   Vdbe *p = (Vdbe *)pStmt;
1165b026e05eSdrh   rc = vdbeUnbind(p, i);
1166b026e05eSdrh   if( rc==SQLITE_OK ){
1167b026e05eSdrh     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1168605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1169488e7b63Sdrh   }
1170b026e05eSdrh   return rc;
1171b026e05eSdrh }
117275f6a032Sdrh 
117375f6a032Sdrh /*
117475f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
117575f6a032Sdrh ** This routine is added to support DBD::SQLite.
117675f6a032Sdrh */
117775f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
117892febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
117992febd92Sdrh   return p ? p->nVar : 0;
118075f6a032Sdrh }
1181895d7472Sdrh 
1182895d7472Sdrh /*
1183fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
1184fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
1185fa6bc000Sdrh **
1186fa6bc000Sdrh ** The result is always UTF-8.
1187fa6bc000Sdrh */
1188fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1189fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
1190124c0b49Sdrh   if( p==0 || i<1 || i>p->nzVar ){
1191fa6bc000Sdrh     return 0;
1192fa6bc000Sdrh   }
1193895d7472Sdrh   return p->azVar[i-1];
1194895d7472Sdrh }
1195fa6bc000Sdrh 
1196fa6bc000Sdrh /*
1197fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
1198fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
1199fa6bc000Sdrh ** return 0.
1200fa6bc000Sdrh */
12015f18a221Sdrh int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
1202fa6bc000Sdrh   int i;
1203fa6bc000Sdrh   if( p==0 ){
1204fa6bc000Sdrh     return 0;
1205fa6bc000Sdrh   }
12066a8903c3Sdrh   if( zName ){
1207124c0b49Sdrh     for(i=0; i<p->nzVar; i++){
1208971a7c87Sdrh       const char *z = p->azVar[i];
1209503a686eSdrh       if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
1210fa6bc000Sdrh         return i+1;
1211fa6bc000Sdrh       }
1212fa6bc000Sdrh     }
12136a8903c3Sdrh   }
1214fa6bc000Sdrh   return 0;
1215fa6bc000Sdrh }
12165f18a221Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
12175f18a221Sdrh   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
12185f18a221Sdrh }
1219f8db1bc0Sdrh 
122051942bc3Sdrh /*
1221f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
1222f8db1bc0Sdrh */
1223145834a4Sshane int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1224f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
1225f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
12260f5ea0b3Sdrh   int i;
12270f5ea0b3Sdrh   assert( pTo->db==pFrom->db );
12280f5ea0b3Sdrh   assert( pTo->nVar==pFrom->nVar );
122927641703Sdrh   sqlite3_mutex_enter(pTo->db->mutex);
12300f5ea0b3Sdrh   for(i=0; i<pFrom->nVar; i++){
1231643167ffSdrh     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1232f8db1bc0Sdrh   }
123327641703Sdrh   sqlite3_mutex_leave(pTo->db->mutex);
12340f5ea0b3Sdrh   return SQLITE_OK;
1235f8db1bc0Sdrh }
123651942bc3Sdrh 
1237eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
123851942bc3Sdrh /*
1239145834a4Sshane ** Deprecated external interface.  Internal/core SQLite code
1240145834a4Sshane ** should call sqlite3TransferBindings.
12410f5ea0b3Sdrh **
12420f5ea0b3Sdrh ** Is is misuse to call this routine with statements from different
12430f5ea0b3Sdrh ** database connections.  But as this is a deprecated interface, we
12440f5ea0b3Sdrh ** will not bother to check for that condition.
12450f5ea0b3Sdrh **
12460f5ea0b3Sdrh ** If the two statements contain a different number of bindings, then
12470f5ea0b3Sdrh ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
12480f5ea0b3Sdrh ** SQLITE_OK is returned.
1249145834a4Sshane */
1250145834a4Sshane int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
12510f5ea0b3Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
12520f5ea0b3Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
12530f5ea0b3Sdrh   if( pFrom->nVar!=pTo->nVar ){
12540f5ea0b3Sdrh     return SQLITE_ERROR;
12550f5ea0b3Sdrh   }
1256c94b8595Sdan   if( pTo->isPrepareV2 && pTo->expmask ){
1257c94b8595Sdan     pTo->expired = 1;
1258c94b8595Sdan   }
1259c94b8595Sdan   if( pFrom->isPrepareV2 && pFrom->expmask ){
1260c94b8595Sdan     pFrom->expired = 1;
1261c94b8595Sdan   }
1262145834a4Sshane   return sqlite3TransferBindings(pFromStmt, pToStmt);
1263145834a4Sshane }
1264eec556d3Sshane #endif
1265145834a4Sshane 
1266145834a4Sshane /*
126751942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
126851942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
126951942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
127051942bc3Sdrh ** the statement in the first place.
127151942bc3Sdrh */
127251942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
127351942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
127451942bc3Sdrh }
1275bb5a9c3eSdrh 
1276bb5a9c3eSdrh /*
1277f03d9cccSdrh ** Return true if the prepared statement is guaranteed to not modify the
1278f03d9cccSdrh ** database.
1279f03d9cccSdrh */
1280f03d9cccSdrh int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1281f03d9cccSdrh   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1282f03d9cccSdrh }
1283f03d9cccSdrh 
1284f03d9cccSdrh /*
12852fb6693eSdrh ** Return true if the prepared statement is in need of being reset.
12862fb6693eSdrh */
12872fb6693eSdrh int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
12882fb6693eSdrh   Vdbe *v = (Vdbe*)pStmt;
12892fb6693eSdrh   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
12902fb6693eSdrh }
12912fb6693eSdrh 
12922fb6693eSdrh /*
1293bb5a9c3eSdrh ** Return a pointer to the next prepared statement after pStmt associated
1294bb5a9c3eSdrh ** with database connection pDb.  If pStmt is NULL, return the first
1295bb5a9c3eSdrh ** prepared statement for the database connection.  Return NULL if there
1296bb5a9c3eSdrh ** are no more.
1297bb5a9c3eSdrh */
1298bb5a9c3eSdrh sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1299bb5a9c3eSdrh   sqlite3_stmt *pNext;
1300bb5a9c3eSdrh   sqlite3_mutex_enter(pDb->mutex);
1301bb5a9c3eSdrh   if( pStmt==0 ){
1302bb5a9c3eSdrh     pNext = (sqlite3_stmt*)pDb->pVdbe;
1303bb5a9c3eSdrh   }else{
1304bb5a9c3eSdrh     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1305bb5a9c3eSdrh   }
1306bb5a9c3eSdrh   sqlite3_mutex_leave(pDb->mutex);
1307bb5a9c3eSdrh   return pNext;
1308bb5a9c3eSdrh }
1309d1d38488Sdrh 
1310d1d38488Sdrh /*
1311d1d38488Sdrh ** Return the value of a status counter for a prepared statement
1312d1d38488Sdrh */
1313d1d38488Sdrh int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1314d1d38488Sdrh   Vdbe *pVdbe = (Vdbe*)pStmt;
13159b47ee3fSdrh   u32 v = pVdbe->aCounter[op];
13169b47ee3fSdrh   if( resetFlag ) pVdbe->aCounter[op] = 0;
13179b47ee3fSdrh   return (int)v;
1318d1d38488Sdrh }
131946c47d46Sdan 
13209b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
132137db03bfSdan /*
132293bca695Sdan ** Allocate and populate an UnpackedRecord structure based on the serialized
132393bca695Sdan ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
132493bca695Sdan ** if successful, or a NULL pointer if an OOM error is encountered.
132593bca695Sdan */
132693bca695Sdan static UnpackedRecord *vdbeUnpackRecord(
132793bca695Sdan   KeyInfo *pKeyInfo,
132893bca695Sdan   int nKey,
132993bca695Sdan   const void *pKey
133093bca695Sdan ){
133193bca695Sdan   char *dummy;                    /* Dummy argument for AllocUnpackedRecord() */
133293bca695Sdan   UnpackedRecord *pRet;           /* Return value */
133393bca695Sdan 
133493bca695Sdan   pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo, 0, 0, &dummy);
133593bca695Sdan   if( pRet ){
1336c8d985e0Sdrh     memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
133793bca695Sdan     sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
133893bca695Sdan   }
133993bca695Sdan   return pRet;
134093bca695Sdan }
134193bca695Sdan 
134293bca695Sdan /*
134337db03bfSdan ** This function is called from within a pre-update callback to retrieve
134437db03bfSdan ** a field of the row currently being updated or deleted.
134537db03bfSdan */
134646c47d46Sdan int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
134746c47d46Sdan   PreUpdate *p = db->pPreUpdate;
134846c47d46Sdan   int rc = SQLITE_OK;
134946c47d46Sdan 
135037db03bfSdan   /* Test that this call is being made from within an SQLITE_DELETE or
135137db03bfSdan   ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
135246c47d46Sdan   if( !p || p->op==SQLITE_INSERT ){
135346c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
135446c47d46Sdan     goto preupdate_old_out;
135546c47d46Sdan   }
135646c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
135746c47d46Sdan     rc = SQLITE_RANGE;
135846c47d46Sdan     goto preupdate_old_out;
135946c47d46Sdan   }
136046c47d46Sdan 
136137db03bfSdan   /* If the old.* record has not yet been loaded into memory, do so now. */
136246c47d46Sdan   if( p->pUnpacked==0 ){
136312ca0b56Sdan     u32 nRec;
136412ca0b56Sdan     u8 *aRec;
136546c47d46Sdan 
136612ca0b56Sdan     rc = sqlite3BtreeDataSize(p->pCsr->pCursor, &nRec);
136746c47d46Sdan     if( rc!=SQLITE_OK ) goto preupdate_old_out;
136812ca0b56Sdan     aRec = sqlite3DbMallocRaw(db, nRec);
136912ca0b56Sdan     if( !aRec ) goto preupdate_old_out;
137012ca0b56Sdan     rc = sqlite3BtreeData(p->pCsr->pCursor, 0, nRec, aRec);
137112ca0b56Sdan     if( rc==SQLITE_OK ){
137293bca695Sdan       p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
137312ca0b56Sdan       if( !p->pUnpacked ) rc = SQLITE_NOMEM;
137412ca0b56Sdan     }
137546c47d46Sdan     if( rc!=SQLITE_OK ){
137612ca0b56Sdan       sqlite3DbFree(db, aRec);
137746c47d46Sdan       goto preupdate_old_out;
137846c47d46Sdan     }
137912ca0b56Sdan     p->aRecord = aRec;
138046c47d46Sdan   }
138146c47d46Sdan 
138246c47d46Sdan   if( iIdx>=p->pUnpacked->nField ){
138346c47d46Sdan     *ppValue = (sqlite3_value *)columnNullValue();
138446c47d46Sdan   }else{
138546c47d46Sdan     *ppValue = &p->pUnpacked->aMem[iIdx];
1386319eeb7bSdan     if( iIdx==p->iPKey ){
1387319eeb7bSdan       sqlite3VdbeMemSetInt64(*ppValue, p->iKey1);
1388319eeb7bSdan     }
138946c47d46Sdan     sqlite3VdbeMemStoreType(*ppValue);
139046c47d46Sdan   }
139146c47d46Sdan 
139246c47d46Sdan  preupdate_old_out:
139346c47d46Sdan   sqlite3Error(db, rc, 0);
139446c47d46Sdan   return sqlite3ApiExit(db, rc);
139546c47d46Sdan }
13969b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
139746c47d46Sdan 
13989b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
139937db03bfSdan /*
140037db03bfSdan ** This function is called from within a pre-update callback to retrieve
140137db03bfSdan ** the number of columns in the row being updated, deleted or inserted.
140237db03bfSdan */
140346c47d46Sdan int sqlite3_preupdate_count(sqlite3 *db){
140446c47d46Sdan   PreUpdate *p = db->pPreUpdate;
1405e437ca5eSdan   return (p ? p->keyinfo.nField : 0);
140646c47d46Sdan }
14079b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
140846c47d46Sdan 
14099b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
141037db03bfSdan /*
14111e7a2d43Sdan ** This function is designed to be called from within a pre-update callback
14121e7a2d43Sdan ** only. It returns zero if the change that caused the callback was made
14131e7a2d43Sdan ** immediately by a user SQL statement. Or, if the change was made by a
14141e7a2d43Sdan ** trigger program, it returns the number of trigger programs currently
14151e7a2d43Sdan ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
14161e7a2d43Sdan ** top-level trigger etc.).
14171e7a2d43Sdan **
14181e7a2d43Sdan ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
14191e7a2d43Sdan ** or SET DEFAULT action is considered a trigger.
14201e7a2d43Sdan */
14211e7a2d43Sdan int sqlite3_preupdate_depth(sqlite3 *db){
14221e7a2d43Sdan   PreUpdate *p = db->pPreUpdate;
14231e7a2d43Sdan   return (p ? p->v->nFrame : 0);
14241e7a2d43Sdan }
14259b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
14261e7a2d43Sdan 
14279b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
14281e7a2d43Sdan /*
142937db03bfSdan ** This function is called from within a pre-update callback to retrieve
143037db03bfSdan ** a field of the row currently being updated or inserted.
143137db03bfSdan */
143237db03bfSdan int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
143346c47d46Sdan   PreUpdate *p = db->pPreUpdate;
143446c47d46Sdan   int rc = SQLITE_OK;
143537db03bfSdan   Mem *pMem;
143646c47d46Sdan 
143737db03bfSdan   if( !p || p->op==SQLITE_DELETE ){
143846c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
143937db03bfSdan     goto preupdate_new_out;
144046c47d46Sdan   }
144146c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
144246c47d46Sdan     rc = SQLITE_RANGE;
144337db03bfSdan     goto preupdate_new_out;
144446c47d46Sdan   }
144546c47d46Sdan 
144637db03bfSdan   if( p->op==SQLITE_INSERT ){
144737db03bfSdan     /* For an INSERT, memory cell p->iNewReg contains the serialized record
144837db03bfSdan     ** that is being inserted. Deserialize it. */
144937db03bfSdan     UnpackedRecord *pUnpack = p->pNewUnpacked;
145037db03bfSdan     if( !pUnpack ){
145137db03bfSdan       Mem *pData = &p->v->aMem[p->iNewReg];
145237db03bfSdan       rc = sqlite3VdbeMemExpandBlob(pData);
145337db03bfSdan       if( rc!=SQLITE_OK ) goto preupdate_new_out;
145493bca695Sdan       pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
145537db03bfSdan       if( !pUnpack ){
145637db03bfSdan         rc = SQLITE_NOMEM;
145737db03bfSdan         goto preupdate_new_out;
145837db03bfSdan       }
145937db03bfSdan       p->pNewUnpacked = pUnpack;
146037db03bfSdan     }
146137db03bfSdan     if( iIdx>=pUnpack->nField ){
146237db03bfSdan       pMem = (sqlite3_value *)columnNullValue();
146337db03bfSdan     }else{
146437db03bfSdan       pMem = &pUnpack->aMem[iIdx];
1465319eeb7bSdan       if( iIdx==p->iPKey ){
1466319eeb7bSdan         sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1467319eeb7bSdan       }
146837db03bfSdan       sqlite3VdbeMemStoreType(pMem);
146937db03bfSdan     }
147037db03bfSdan   }else{
147137db03bfSdan     /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
147237db03bfSdan     ** value. Make a copy of the cell contents and return a pointer to it.
147337db03bfSdan     ** It is not safe to return a pointer to the memory cell itself as the
147437db03bfSdan     ** caller may modify the value text encoding.
147537db03bfSdan     */
147637db03bfSdan     assert( p->op==SQLITE_UPDATE );
147737db03bfSdan     if( !p->aNew ){
147837db03bfSdan       p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
147937db03bfSdan       if( !p->aNew ){
148037db03bfSdan         rc = SQLITE_NOMEM;
148137db03bfSdan         goto preupdate_new_out;
148237db03bfSdan       }
148337db03bfSdan     }
1484304637c0Sdrh     assert( iIdx>=0 && iIdx<p->pCsr->nField );
148537db03bfSdan     pMem = &p->aNew[iIdx];
148637db03bfSdan     if( pMem->flags==0 ){
1487319eeb7bSdan       if( iIdx==p->iPKey ){
1488319eeb7bSdan         sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1489319eeb7bSdan       }else{
149037db03bfSdan         rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
149137db03bfSdan         if( rc!=SQLITE_OK ) goto preupdate_new_out;
1492319eeb7bSdan       }
149337db03bfSdan       sqlite3VdbeMemStoreType(pMem);
149437db03bfSdan     }
149537db03bfSdan   }
149637db03bfSdan   *ppValue = pMem;
149737db03bfSdan 
149837db03bfSdan  preupdate_new_out:
149946c47d46Sdan   sqlite3Error(db, rc, 0);
150046c47d46Sdan   return sqlite3ApiExit(db, rc);
150146c47d46Sdan }
15029b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1503