xref: /sqlite-3.40.0/src/vdbeapi.c (revision 47996ea7)
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 */
sqlite3_expired(sqlite3_stmt * pStmt)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 */
vdbeSafety(Vdbe * p)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 }
vdbeSafetyNotNull(Vdbe * p)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 
56201e0c68Sdrh #ifndef SQLITE_OMIT_TRACE
57201e0c68Sdrh /*
58201e0c68Sdrh ** Invoke the profile callback.  This routine is only called if we already
59201e0c68Sdrh ** know that the profile callback is defined and needs to be invoked.
60201e0c68Sdrh */
invokeProfileCallback(sqlite3 * db,Vdbe * p)61201e0c68Sdrh static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
62201e0c68Sdrh   sqlite3_int64 iNow;
633d2a529dSdrh   sqlite3_int64 iElapse;
64201e0c68Sdrh   assert( p->startTime>0 );
6504c6747aSdrh   assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
66201e0c68Sdrh   assert( db->init.busy==0 );
67201e0c68Sdrh   assert( p->zSql!=0 );
68201e0c68Sdrh   sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
693d2a529dSdrh   iElapse = (iNow - p->startTime)*1000000;
7004c6747aSdrh #ifndef SQLITE_OMIT_DEPRECATED
713d2a529dSdrh   if( db->xProfile ){
723d2a529dSdrh     db->xProfile(db->pProfileArg, p->zSql, iElapse);
733d2a529dSdrh   }
7404c6747aSdrh #endif
753d2a529dSdrh   if( db->mTrace & SQLITE_TRACE_PROFILE ){
7608b92086Sdrh     db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
773d2a529dSdrh   }
78201e0c68Sdrh   p->startTime = 0;
79201e0c68Sdrh }
80201e0c68Sdrh /*
81201e0c68Sdrh ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
82201e0c68Sdrh ** is needed, and it invokes the callback if it is needed.
83201e0c68Sdrh */
84201e0c68Sdrh # define checkProfileCallback(DB,P) \
85201e0c68Sdrh    if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
86201e0c68Sdrh #else
87201e0c68Sdrh # define checkProfileCallback(DB,P)  /*no-op*/
88201e0c68Sdrh #endif
89201e0c68Sdrh 
90413c3d36Sdrh /*
91e30f4426Sdrh ** The following routine destroys a virtual machine that is created by
92e30f4426Sdrh ** the sqlite3_compile() routine. The integer returned is an SQLITE_
93e30f4426Sdrh ** success/failure code that describes the result of executing the virtual
94e30f4426Sdrh ** machine.
95e30f4426Sdrh **
96e30f4426Sdrh ** This routine sets the error code and string returned by
97e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
98e30f4426Sdrh */
sqlite3_finalize(sqlite3_stmt * pStmt)99e30f4426Sdrh int sqlite3_finalize(sqlite3_stmt *pStmt){
100e30f4426Sdrh   int rc;
101e30f4426Sdrh   if( pStmt==0 ){
10265bafa65Sdrh     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
10365bafa65Sdrh     ** pointer is a harmless no-op. */
104e30f4426Sdrh     rc = SQLITE_OK;
105e30f4426Sdrh   }else{
106e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
107238746a6Sdanielk1977     sqlite3 *db = v->db;
108413c3d36Sdrh     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
1094245c405Sdrh     sqlite3_mutex_enter(db->mutex);
110201e0c68Sdrh     checkProfileCallback(db, v);
1112b294b54Sdrh     assert( v->eVdbeState>=VDBE_READY_STATE );
1122b294b54Sdrh     rc = sqlite3VdbeReset(v);
1132b294b54Sdrh     sqlite3VdbeDelete(v);
114238746a6Sdanielk1977     rc = sqlite3ApiExit(db, rc);
1154245c405Sdrh     sqlite3LeaveMutexAndCloseZombie(db);
116e30f4426Sdrh   }
117e30f4426Sdrh   return rc;
118e30f4426Sdrh }
119e30f4426Sdrh 
120e30f4426Sdrh /*
121e30f4426Sdrh ** Terminate the current execution of an SQL statement and reset it
122e30f4426Sdrh ** back to its starting state so that it can be reused. A success code from
123e30f4426Sdrh ** the prior execution is returned.
124e30f4426Sdrh **
125e30f4426Sdrh ** This routine sets the error code and string returned by
126e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
127e30f4426Sdrh */
sqlite3_reset(sqlite3_stmt * pStmt)128e30f4426Sdrh int sqlite3_reset(sqlite3_stmt *pStmt){
129e30f4426Sdrh   int rc;
130e30f4426Sdrh   if( pStmt==0 ){
131e30f4426Sdrh     rc = SQLITE_OK;
132e30f4426Sdrh   }else{
133e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
134201e0c68Sdrh     sqlite3 *db = v->db;
135201e0c68Sdrh     sqlite3_mutex_enter(db->mutex);
136201e0c68Sdrh     checkProfileCallback(db, v);
137c890fec3Sdrh     rc = sqlite3VdbeReset(v);
138124c0b49Sdrh     sqlite3VdbeRewind(v);
139201e0c68Sdrh     assert( (rc & (db->errMask))==rc );
140201e0c68Sdrh     rc = sqlite3ApiExit(db, rc);
141201e0c68Sdrh     sqlite3_mutex_leave(db->mutex);
142e30f4426Sdrh   }
143e30f4426Sdrh   return rc;
144e30f4426Sdrh }
145e30f4426Sdrh 
146e30f4426Sdrh /*
147e30f4426Sdrh ** Set all the parameters in the compiled SQL statement to NULL.
148e30f4426Sdrh */
sqlite3_clear_bindings(sqlite3_stmt * pStmt)149e30f4426Sdrh int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
150e30f4426Sdrh   int i;
151e30f4426Sdrh   int rc = SQLITE_OK;
1527297d1f0Sdrh   Vdbe *p = (Vdbe*)pStmt;
15318472fa7Sdrh #if SQLITE_THREADSAFE
1547e8b848aSdrh   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
1557e8b848aSdrh #endif
1567e8b848aSdrh   sqlite3_mutex_enter(mutex);
1577297d1f0Sdrh   for(i=0; i<p->nVar; i++){
1587297d1f0Sdrh     sqlite3VdbeMemRelease(&p->aVar[i]);
1597297d1f0Sdrh     p->aVar[i].flags = MEM_Null;
160e30f4426Sdrh   }
1612c2f392dSdrh   assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
162bda4cb87Sdan   if( p->expmask ){
163c94b8595Sdan     p->expired = 1;
164c94b8595Sdan   }
1657e8b848aSdrh   sqlite3_mutex_leave(mutex);
166e30f4426Sdrh   return rc;
167e30f4426Sdrh }
168e30f4426Sdrh 
169e30f4426Sdrh 
1704f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
1714f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
1724f26d6c4Sdrh ** structure.
1734f26d6c4Sdrh */
sqlite3_value_blob(sqlite3_value * pVal)1744f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
1754f26d6c4Sdrh   Mem *p = (Mem*)pVal;
1764f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
177ff535a24Sdrh     if( ExpandBlob(p)!=SQLITE_OK ){
178a4d5ae8fSdan       assert( p->flags==MEM_Null && p->z==0 );
179a4d5ae8fSdan       return 0;
180a4d5ae8fSdan     }
1811f0feef8Sdrh     p->flags |= MEM_Blob;
18242262536Sdrh     return p->n ? p->z : 0;
1834f26d6c4Sdrh   }else{
1844f26d6c4Sdrh     return sqlite3_value_text(pVal);
1854f26d6c4Sdrh   }
1864f26d6c4Sdrh }
sqlite3_value_bytes(sqlite3_value * pVal)1874f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
188b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
1894f26d6c4Sdrh }
sqlite3_value_bytes16(sqlite3_value * pVal)1904f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
191b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
1924f26d6c4Sdrh }
sqlite3_value_double(sqlite3_value * pVal)1934f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
1946a6124e2Sdrh   return sqlite3VdbeRealValue((Mem*)pVal);
1954f26d6c4Sdrh }
sqlite3_value_int(sqlite3_value * pVal)1964f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
197b27b7f5dSdrh   return (int)sqlite3VdbeIntValue((Mem*)pVal);
1984f26d6c4Sdrh }
sqlite3_value_int64(sqlite3_value * pVal)199efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
2006a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
2014f26d6c4Sdrh }
sqlite3_value_subtype(sqlite3_value * pVal)202bcdf78a6Sdrh unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
2035b6c8e4eSdan   Mem *pMem = (Mem*)pVal;
2045b6c8e4eSdan   return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
205bcdf78a6Sdrh }
sqlite3_value_pointer(sqlite3_value * pVal,const char * zPType)206ae3ec3f9Sdrh void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
2073a96a5d9Sdrh   Mem *p = (Mem*)pVal;
208a0024e6cSdrh   if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
209a0024e6cSdrh                  (MEM_Null|MEM_Term|MEM_Subtype)
210ae3ec3f9Sdrh    && zPType!=0
211a0024e6cSdrh    && p->eSubtype=='p'
21222930062Sdrh    && strcmp(p->u.zPType, zPType)==0
213ae3ec3f9Sdrh   ){
21422930062Sdrh     return (void*)p->z;
2153a96a5d9Sdrh   }else{
2163a96a5d9Sdrh     return 0;
2173a96a5d9Sdrh   }
2183a96a5d9Sdrh }
sqlite3_value_text(sqlite3_value * pVal)2194f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
220b21c8cd4Sdrh   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
2214f26d6c4Sdrh }
2226c62608fSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_value_text16(sqlite3_value * pVal)2234f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
224b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2254f26d6c4Sdrh }
sqlite3_value_text16be(sqlite3_value * pVal)226d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){
227b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
228d8123366Sdanielk1977 }
sqlite3_value_text16le(sqlite3_value * pVal)229d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){
230b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
231d8123366Sdanielk1977 }
2326c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
23322ec1346Sdrh /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
23422ec1346Sdrh ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
23522ec1346Sdrh ** point number string BLOB NULL
23622ec1346Sdrh */
sqlite3_value_type(sqlite3_value * pVal)2374f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
2381b27b8c0Sdrh   static const u8 aType[] = {
239f2566c41Sdrh      SQLITE_BLOB,     /* 0x00 (not possible) */
240f2566c41Sdrh      SQLITE_NULL,     /* 0x01 NULL */
241f2566c41Sdrh      SQLITE_TEXT,     /* 0x02 TEXT */
242f2566c41Sdrh      SQLITE_NULL,     /* 0x03 (not possible) */
243f2566c41Sdrh      SQLITE_INTEGER,  /* 0x04 INTEGER */
244f2566c41Sdrh      SQLITE_NULL,     /* 0x05 (not possible) */
245f2566c41Sdrh      SQLITE_INTEGER,  /* 0x06 INTEGER + TEXT */
246f2566c41Sdrh      SQLITE_NULL,     /* 0x07 (not possible) */
247f2566c41Sdrh      SQLITE_FLOAT,    /* 0x08 FLOAT */
248f2566c41Sdrh      SQLITE_NULL,     /* 0x09 (not possible) */
249f2566c41Sdrh      SQLITE_FLOAT,    /* 0x0a FLOAT + TEXT */
250f2566c41Sdrh      SQLITE_NULL,     /* 0x0b (not possible) */
251f2566c41Sdrh      SQLITE_INTEGER,  /* 0x0c (not possible) */
252f2566c41Sdrh      SQLITE_NULL,     /* 0x0d (not possible) */
253f2566c41Sdrh      SQLITE_INTEGER,  /* 0x0e (not possible) */
254f2566c41Sdrh      SQLITE_NULL,     /* 0x0f (not possible) */
255f2566c41Sdrh      SQLITE_BLOB,     /* 0x10 BLOB */
256f2566c41Sdrh      SQLITE_NULL,     /* 0x11 (not possible) */
257f2566c41Sdrh      SQLITE_TEXT,     /* 0x12 (not possible) */
258f2566c41Sdrh      SQLITE_NULL,     /* 0x13 (not possible) */
259f2566c41Sdrh      SQLITE_INTEGER,  /* 0x14 INTEGER + BLOB */
260f2566c41Sdrh      SQLITE_NULL,     /* 0x15 (not possible) */
261f2566c41Sdrh      SQLITE_INTEGER,  /* 0x16 (not possible) */
262f2566c41Sdrh      SQLITE_NULL,     /* 0x17 (not possible) */
263f2566c41Sdrh      SQLITE_FLOAT,    /* 0x18 FLOAT + BLOB */
264f2566c41Sdrh      SQLITE_NULL,     /* 0x19 (not possible) */
265f2566c41Sdrh      SQLITE_FLOAT,    /* 0x1a (not possible) */
266f2566c41Sdrh      SQLITE_NULL,     /* 0x1b (not possible) */
267f2566c41Sdrh      SQLITE_INTEGER,  /* 0x1c (not possible) */
268f2566c41Sdrh      SQLITE_NULL,     /* 0x1d (not possible) */
269f2566c41Sdrh      SQLITE_INTEGER,  /* 0x1e (not possible) */
270f2566c41Sdrh      SQLITE_NULL,     /* 0x1f (not possible) */
271f2566c41Sdrh      SQLITE_FLOAT,    /* 0x20 INTREAL */
272f2566c41Sdrh      SQLITE_NULL,     /* 0x21 (not possible) */
273f2566c41Sdrh      SQLITE_TEXT,     /* 0x22 INTREAL + TEXT */
274f2566c41Sdrh      SQLITE_NULL,     /* 0x23 (not possible) */
275f2566c41Sdrh      SQLITE_FLOAT,    /* 0x24 (not possible) */
276f2566c41Sdrh      SQLITE_NULL,     /* 0x25 (not possible) */
277f2566c41Sdrh      SQLITE_FLOAT,    /* 0x26 (not possible) */
278f2566c41Sdrh      SQLITE_NULL,     /* 0x27 (not possible) */
279f2566c41Sdrh      SQLITE_FLOAT,    /* 0x28 (not possible) */
280f2566c41Sdrh      SQLITE_NULL,     /* 0x29 (not possible) */
281f2566c41Sdrh      SQLITE_FLOAT,    /* 0x2a (not possible) */
282f2566c41Sdrh      SQLITE_NULL,     /* 0x2b (not possible) */
283f2566c41Sdrh      SQLITE_FLOAT,    /* 0x2c (not possible) */
284f2566c41Sdrh      SQLITE_NULL,     /* 0x2d (not possible) */
285f2566c41Sdrh      SQLITE_FLOAT,    /* 0x2e (not possible) */
286f2566c41Sdrh      SQLITE_NULL,     /* 0x2f (not possible) */
287f2566c41Sdrh      SQLITE_BLOB,     /* 0x30 (not possible) */
288f2566c41Sdrh      SQLITE_NULL,     /* 0x31 (not possible) */
289f2566c41Sdrh      SQLITE_TEXT,     /* 0x32 (not possible) */
290f2566c41Sdrh      SQLITE_NULL,     /* 0x33 (not possible) */
291f2566c41Sdrh      SQLITE_FLOAT,    /* 0x34 (not possible) */
292f2566c41Sdrh      SQLITE_NULL,     /* 0x35 (not possible) */
293f2566c41Sdrh      SQLITE_FLOAT,    /* 0x36 (not possible) */
294f2566c41Sdrh      SQLITE_NULL,     /* 0x37 (not possible) */
295f2566c41Sdrh      SQLITE_FLOAT,    /* 0x38 (not possible) */
296f2566c41Sdrh      SQLITE_NULL,     /* 0x39 (not possible) */
297f2566c41Sdrh      SQLITE_FLOAT,    /* 0x3a (not possible) */
298f2566c41Sdrh      SQLITE_NULL,     /* 0x3b (not possible) */
299f2566c41Sdrh      SQLITE_FLOAT,    /* 0x3c (not possible) */
300f2566c41Sdrh      SQLITE_NULL,     /* 0x3d (not possible) */
301f2566c41Sdrh      SQLITE_FLOAT,    /* 0x3e (not possible) */
302f2566c41Sdrh      SQLITE_NULL,     /* 0x3f (not possible) */
3031b27b8c0Sdrh   };
304de7109e6Sdrh #ifdef SQLITE_DEBUG
305de7109e6Sdrh   {
306de7109e6Sdrh     int eType = SQLITE_BLOB;
307de7109e6Sdrh     if( pVal->flags & MEM_Null ){
308de7109e6Sdrh       eType = SQLITE_NULL;
309169f077eSdrh     }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
310de7109e6Sdrh       eType = SQLITE_FLOAT;
311169f077eSdrh     }else if( pVal->flags & MEM_Int ){
312169f077eSdrh       eType = SQLITE_INTEGER;
313de7109e6Sdrh     }else if( pVal->flags & MEM_Str ){
314de7109e6Sdrh       eType = SQLITE_TEXT;
315de7109e6Sdrh     }
316de7109e6Sdrh     assert( eType == aType[pVal->flags&MEM_AffMask] );
317de7109e6Sdrh   }
318de7109e6Sdrh #endif
319afc14f72Smistachkin   return aType[pVal->flags&MEM_AffMask];
3204f26d6c4Sdrh }
sqlite3_value_encoding(sqlite3_value * pVal)321*47996ea7Sdrh int sqlite3_value_encoding(sqlite3_value *pVal){
322*47996ea7Sdrh   return pVal->enc;
323*47996ea7Sdrh }
3244f26d6c4Sdrh 
325ce2fbd1bSdrh /* Return true if a parameter to xUpdate represents an unchanged column */
sqlite3_value_nochange(sqlite3_value * pVal)326ce2fbd1bSdrh int sqlite3_value_nochange(sqlite3_value *pVal){
327ce2fbd1bSdrh   return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
328ce2fbd1bSdrh }
329ce2fbd1bSdrh 
33057b1a3e3Sdrh /* Return true if a parameter value originated from an sqlite3_bind() */
sqlite3_value_frombind(sqlite3_value * pVal)33157b1a3e3Sdrh int sqlite3_value_frombind(sqlite3_value *pVal){
33257b1a3e3Sdrh   return (pVal->flags&MEM_FromBind)!=0;
33357b1a3e3Sdrh }
33457b1a3e3Sdrh 
3354f03f413Sdrh /* Make a copy of an sqlite3_value object
3364f03f413Sdrh */
sqlite3_value_dup(const sqlite3_value * pOrig)3374f03f413Sdrh sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
3384f03f413Sdrh   sqlite3_value *pNew;
3394f03f413Sdrh   if( pOrig==0 ) return 0;
3404f03f413Sdrh   pNew = sqlite3_malloc( sizeof(*pNew) );
3414f03f413Sdrh   if( pNew==0 ) return 0;
3424f03f413Sdrh   memset(pNew, 0, sizeof(*pNew));
3434f03f413Sdrh   memcpy(pNew, pOrig, MEMCELLSIZE);
3444f03f413Sdrh   pNew->flags &= ~MEM_Dyn;
3454f03f413Sdrh   pNew->db = 0;
3464f03f413Sdrh   if( pNew->flags&(MEM_Str|MEM_Blob) ){
34710ca5b48Sdrh     pNew->flags &= ~(MEM_Static|MEM_Dyn);
3484f03f413Sdrh     pNew->flags |= MEM_Ephem;
3499dfedc82Sdrh     if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
3509dfedc82Sdrh       sqlite3ValueFree(pNew);
3519dfedc82Sdrh       pNew = 0;
3529dfedc82Sdrh     }
35383665295Sdrh   }else if( pNew->flags & MEM_Null ){
35483665295Sdrh     /* Do not duplicate pointer values */
35583665295Sdrh     pNew->flags &= ~(MEM_Term|MEM_Subtype);
3564f03f413Sdrh   }
3574f03f413Sdrh   return pNew;
3584f03f413Sdrh }
3594f03f413Sdrh 
3604f03f413Sdrh /* Destroy an sqlite3_value object previously obtained from
3614f03f413Sdrh ** sqlite3_value_dup().
3624f03f413Sdrh */
sqlite3_value_free(sqlite3_value * pOld)3634f03f413Sdrh void sqlite3_value_free(sqlite3_value *pOld){
3644f03f413Sdrh   sqlite3ValueFree(pOld);
3654f03f413Sdrh }
3664f03f413Sdrh 
3674f03f413Sdrh 
3684f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
3694f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
3704f26d6c4Sdrh ** the function result.
3714c8555fdSdrh **
37260ec914cSpeter.d.reid ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
373dbe349dfSdrh ** result as a string or blob.  Appropriate errors are set if the string/blob
374dbe349dfSdrh ** is too big or if an OOM occurs.
375da4ca9d1Sdrh **
376da4ca9d1Sdrh ** The invokeValueDestructor(P,X) routine invokes destructor function X()
377da4ca9d1Sdrh ** on value P is not going to be used and need to be destroyed.
3784f26d6c4Sdrh */
setResultStrOrError(sqlite3_context * pCtx,const char * z,int n,u8 enc,void (* xDel)(void *))3794c8555fdSdrh static void setResultStrOrError(
3804c8555fdSdrh   sqlite3_context *pCtx,  /* Function context */
3814c8555fdSdrh   const char *z,          /* String pointer */
3824c8555fdSdrh   int n,                  /* Bytes in string, or negative */
3834c8555fdSdrh   u8 enc,                 /* Encoding of z.  0 for BLOBs */
3844c8555fdSdrh   void (*xDel)(void*)     /* Destructor function */
3854c8555fdSdrh ){
386fb92e071Sdrh   Mem *pOut = pCtx->pOut;
387fb92e071Sdrh   int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
388dbe349dfSdrh   if( rc ){
389dbe349dfSdrh     if( rc==SQLITE_TOOBIG ){
3904c8555fdSdrh       sqlite3_result_error_toobig(pCtx);
391dbe349dfSdrh     }else{
392dbe349dfSdrh       /* The only errors possible from sqlite3VdbeMemSetStr are
393dbe349dfSdrh       ** SQLITE_TOOBIG and SQLITE_NOMEM */
394dbe349dfSdrh       assert( rc==SQLITE_NOMEM );
395dbe349dfSdrh       sqlite3_result_error_nomem(pCtx);
396dbe349dfSdrh     }
397fb92e071Sdrh     return;
398fb92e071Sdrh   }
399659fdb4dSdrh   sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
400fb92e071Sdrh   if( sqlite3VdbeMemTooBig(pOut) ){
401fb92e071Sdrh     sqlite3_result_error_toobig(pCtx);
402fb92e071Sdrh   }
4034c8555fdSdrh }
invokeValueDestructor(const void * p,void (* xDel)(void *),sqlite3_context * pCtx)404da4ca9d1Sdrh static int invokeValueDestructor(
405da4ca9d1Sdrh   const void *p,             /* Value to destroy */
406da4ca9d1Sdrh   void (*xDel)(void*),       /* The destructor */
407da4ca9d1Sdrh   sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
408da4ca9d1Sdrh ){
409fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
410da4ca9d1Sdrh   if( xDel==0 ){
411da4ca9d1Sdrh     /* noop */
412da4ca9d1Sdrh   }else if( xDel==SQLITE_TRANSIENT ){
413da4ca9d1Sdrh     /* noop */
414da4ca9d1Sdrh   }else{
415da4ca9d1Sdrh     xDel((void*)p);
416da4ca9d1Sdrh   }
417d622855eSdrh   sqlite3_result_error_toobig(pCtx);
418da4ca9d1Sdrh   return SQLITE_TOOBIG;
419da4ca9d1Sdrh }
sqlite3_result_blob(sqlite3_context * pCtx,const void * z,int n,void (* xDel)(void *))4204f26d6c4Sdrh void sqlite3_result_blob(
4214f26d6c4Sdrh   sqlite3_context *pCtx,
4224f26d6c4Sdrh   const void *z,
4234f26d6c4Sdrh   int n,
424d8123366Sdanielk1977   void (*xDel)(void *)
4254f26d6c4Sdrh ){
4265708d2deSdrh   assert( n>=0 );
4279bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4284c8555fdSdrh   setResultStrOrError(pCtx, z, n, 0, xDel);
4294f26d6c4Sdrh }
sqlite3_result_blob64(sqlite3_context * pCtx,const void * z,sqlite3_uint64 n,void (* xDel)(void *))430da4ca9d1Sdrh void sqlite3_result_blob64(
431da4ca9d1Sdrh   sqlite3_context *pCtx,
432da4ca9d1Sdrh   const void *z,
433da4ca9d1Sdrh   sqlite3_uint64 n,
434da4ca9d1Sdrh   void (*xDel)(void *)
435da4ca9d1Sdrh ){
436da4ca9d1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
437fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
438da4ca9d1Sdrh   if( n>0x7fffffff ){
439da4ca9d1Sdrh     (void)invokeValueDestructor(z, xDel, pCtx);
440da4ca9d1Sdrh   }else{
441da4ca9d1Sdrh     setResultStrOrError(pCtx, z, (int)n, 0, xDel);
442da4ca9d1Sdrh   }
443da4ca9d1Sdrh }
sqlite3_result_double(sqlite3_context * pCtx,double rVal)4444f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
4459bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4469bd038f1Sdrh   sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
4474f26d6c4Sdrh }
sqlite3_result_error(sqlite3_context * pCtx,const char * z,int n)4484f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
4499bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
45069544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
4519bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
4524f26d6c4Sdrh }
453a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16
sqlite3_result_error16(sqlite3_context * pCtx,const void * z,int n)4544f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
4559bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
45669544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
4579bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
4584f26d6c4Sdrh }
459a1686c9aSdanielk1977 #endif
sqlite3_result_int(sqlite3_context * pCtx,int iVal)460f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
4619bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4629bd038f1Sdrh   sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
4634f26d6c4Sdrh }
sqlite3_result_int64(sqlite3_context * pCtx,i64 iVal)4644f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
4659bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4669bd038f1Sdrh   sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
4674f26d6c4Sdrh }
sqlite3_result_null(sqlite3_context * pCtx)4684f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
4699bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4709bd038f1Sdrh   sqlite3VdbeMemSetNull(pCtx->pOut);
4714f26d6c4Sdrh }
sqlite3_result_pointer(sqlite3_context * pCtx,void * pPtr,const char * zPType,void (* xDestructor)(void *))47222930062Sdrh void sqlite3_result_pointer(
47322930062Sdrh   sqlite3_context *pCtx,
47422930062Sdrh   void *pPtr,
47522930062Sdrh   const char *zPType,
47622930062Sdrh   void (*xDestructor)(void*)
47722930062Sdrh ){
4783a96a5d9Sdrh   Mem *pOut = pCtx->pOut;
4793a96a5d9Sdrh   assert( sqlite3_mutex_held(pOut->db->mutex) );
480526740b1Sdrh   sqlite3VdbeMemRelease(pOut);
481526740b1Sdrh   pOut->flags = MEM_Null;
48222930062Sdrh   sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
4833a96a5d9Sdrh }
sqlite3_result_subtype(sqlite3_context * pCtx,unsigned int eSubtype)484bcdf78a6Sdrh void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
4855b6c8e4eSdan   Mem *pOut = pCtx->pOut;
4865b6c8e4eSdan   assert( sqlite3_mutex_held(pOut->db->mutex) );
4875b6c8e4eSdan   pOut->eSubtype = eSubtype & 0xff;
4885b6c8e4eSdan   pOut->flags |= MEM_Subtype;
489bcdf78a6Sdrh }
sqlite3_result_text(sqlite3_context * pCtx,const char * z,int n,void (* xDel)(void *))4904f26d6c4Sdrh void sqlite3_result_text(
4914f26d6c4Sdrh   sqlite3_context *pCtx,
4924f26d6c4Sdrh   const char *z,
4934f26d6c4Sdrh   int n,
494d8123366Sdanielk1977   void (*xDel)(void *)
4954f26d6c4Sdrh ){
4969bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4974c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
4984f26d6c4Sdrh }
sqlite3_result_text64(sqlite3_context * pCtx,const char * z,sqlite3_uint64 n,void (* xDel)(void *),unsigned char enc)499bbf483f8Sdrh void sqlite3_result_text64(
500da4ca9d1Sdrh   sqlite3_context *pCtx,
501da4ca9d1Sdrh   const char *z,
502da4ca9d1Sdrh   sqlite3_uint64 n,
503da4ca9d1Sdrh   void (*xDel)(void *),
504da4ca9d1Sdrh   unsigned char enc
505da4ca9d1Sdrh ){
506da4ca9d1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
507fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
50879f7af9aSdrh   if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
509da4ca9d1Sdrh   if( n>0x7fffffff ){
510da4ca9d1Sdrh     (void)invokeValueDestructor(z, xDel, pCtx);
511da4ca9d1Sdrh   }else{
512da4ca9d1Sdrh     setResultStrOrError(pCtx, z, (int)n, enc, xDel);
513da4ca9d1Sdrh   }
514da4ca9d1Sdrh }
5156c62608fSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_result_text16(sqlite3_context * pCtx,const void * z,int n,void (* xDel)(void *))5164f26d6c4Sdrh void sqlite3_result_text16(
5174f26d6c4Sdrh   sqlite3_context *pCtx,
5184f26d6c4Sdrh   const void *z,
5194f26d6c4Sdrh   int n,
520d8123366Sdanielk1977   void (*xDel)(void *)
5214f26d6c4Sdrh ){
5229bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
5234c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
524d8123366Sdanielk1977 }
sqlite3_result_text16be(sqlite3_context * pCtx,const void * z,int n,void (* xDel)(void *))525d8123366Sdanielk1977 void sqlite3_result_text16be(
526d8123366Sdanielk1977   sqlite3_context *pCtx,
527d8123366Sdanielk1977   const void *z,
528d8123366Sdanielk1977   int n,
529d8123366Sdanielk1977   void (*xDel)(void *)
530d8123366Sdanielk1977 ){
5319bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
5324c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
533d8123366Sdanielk1977 }
sqlite3_result_text16le(sqlite3_context * pCtx,const void * z,int n,void (* xDel)(void *))534d8123366Sdanielk1977 void sqlite3_result_text16le(
535d8123366Sdanielk1977   sqlite3_context *pCtx,
536d8123366Sdanielk1977   const void *z,
537d8123366Sdanielk1977   int n,
538d8123366Sdanielk1977   void (*xDel)(void *)
539d8123366Sdanielk1977 ){
5409bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
5414c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
5424f26d6c4Sdrh }
5436c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
sqlite3_result_value(sqlite3_context * pCtx,sqlite3_value * pValue)5444f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
545fb92e071Sdrh   Mem *pOut = pCtx->pOut;
5469bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
547fb92e071Sdrh   sqlite3VdbeMemCopy(pOut, pValue);
548659fdb4dSdrh   sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
549fb92e071Sdrh   if( sqlite3VdbeMemTooBig(pOut) ){
550fb92e071Sdrh     sqlite3_result_error_toobig(pCtx);
551fb92e071Sdrh   }
5524f26d6c4Sdrh }
sqlite3_result_zeroblob(sqlite3_context * pCtx,int n)553b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
554fb92e071Sdrh   sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);
555b026e05eSdrh }
sqlite3_result_zeroblob64(sqlite3_context * pCtx,u64 n)556a4d5ae8fSdan int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
557a4d5ae8fSdan   Mem *pOut = pCtx->pOut;
558a4d5ae8fSdan   assert( sqlite3_mutex_held(pOut->db->mutex) );
5596bacdc21Sdrh   if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
560fb92e071Sdrh     sqlite3_result_error_toobig(pCtx);
561a4d5ae8fSdan     return SQLITE_TOOBIG;
562a4d5ae8fSdan   }
563a32536b4Sdan #ifndef SQLITE_OMIT_INCRBLOB
564a4d5ae8fSdan   sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
565a4d5ae8fSdan   return SQLITE_OK;
566a32536b4Sdan #else
567a32536b4Sdan   return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
568a32536b4Sdan #endif
569a4d5ae8fSdan }
sqlite3_result_error_code(sqlite3_context * pCtx,int errCode)57069544ec9Sdrh void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
571f09ac0b3Sdrh   pCtx->isError = errCode ? errCode : -1;
572983b5ee7Sdrh #ifdef SQLITE_DEBUG
57396f4ad20Sdrh   if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
574983b5ee7Sdrh #endif
5759bd038f1Sdrh   if( pCtx->pOut->flags & MEM_Null ){
576fb92e071Sdrh     setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
577fb92e071Sdrh                         SQLITE_STATIC);
57851c7d864Sdrh   }
57969544ec9Sdrh }
5804f26d6c4Sdrh 
581a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */
sqlite3_result_error_toobig(sqlite3_context * pCtx)582a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){
5839bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
584bb4957f8Sdrh   pCtx->isError = SQLITE_TOOBIG;
5859bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
586bb4957f8Sdrh                        SQLITE_UTF8, SQLITE_STATIC);
587a0206bc8Sdrh }
588a0206bc8Sdrh 
589a1644fd8Sdanielk1977 /* An SQLITE_NOMEM error. */
sqlite3_result_error_nomem(sqlite3_context * pCtx)590a1644fd8Sdanielk1977 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
5919bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
5929bd038f1Sdrh   sqlite3VdbeMemSetNull(pCtx->pOut);
593fad3039cSmistachkin   pCtx->isError = SQLITE_NOMEM_BKPT;
5944a642b60Sdrh   sqlite3OomFault(pCtx->pOut->db);
595a1644fd8Sdanielk1977 }
5964f26d6c4Sdrh 
5970c8f4038Sdrh #ifndef SQLITE_UNTESTABLE
5980c8f4038Sdrh /* Force the INT64 value currently stored as the result to be
5990c8f4038Sdrh ** a MEM_IntReal value.  See the SQLITE_TESTCTRL_RESULT_INTREAL
6000c8f4038Sdrh ** test-control.
6010c8f4038Sdrh */
sqlite3ResultIntReal(sqlite3_context * pCtx)6020c8f4038Sdrh void sqlite3ResultIntReal(sqlite3_context *pCtx){
6030c8f4038Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6040c8f4038Sdrh   if( pCtx->pOut->flags & MEM_Int ){
6050c8f4038Sdrh     pCtx->pOut->flags &= ~MEM_Int;
6060c8f4038Sdrh     pCtx->pOut->flags |= MEM_IntReal;
6070c8f4038Sdrh   }
6080c8f4038Sdrh }
6090c8f4038Sdrh #endif
6100c8f4038Sdrh 
6110c8f4038Sdrh 
6125cf53537Sdan /*
6135cf53537Sdan ** This function is called after a transaction has been committed. It
6145cf53537Sdan ** invokes callbacks registered with sqlite3_wal_hook() as required.
6155cf53537Sdan */
doWalCallbacks(sqlite3 * db)6167ed91f23Sdrh static int doWalCallbacks(sqlite3 *db){
6178d22a174Sdan   int rc = SQLITE_OK;
6185cf53537Sdan #ifndef SQLITE_OMIT_WAL
6195cf53537Sdan   int i;
6208d22a174Sdan   for(i=0; i<db->nDb; i++){
6218d22a174Sdan     Btree *pBt = db->aDb[i].pBt;
6228d22a174Sdan     if( pBt ){
623ef15c6e9Sdrh       int nEntry;
624ef15c6e9Sdrh       sqlite3BtreeEnter(pBt);
625ef15c6e9Sdrh       nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
626ef15c6e9Sdrh       sqlite3BtreeLeave(pBt);
62759533aa5Sdrh       if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
62869c33826Sdrh         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
6298d22a174Sdan       }
6308d22a174Sdan     }
6318d22a174Sdan   }
6325cf53537Sdan #endif
6338d22a174Sdan   return rc;
6348d22a174Sdan }
6358d22a174Sdan 
636201e0c68Sdrh 
6374f26d6c4Sdrh /*
6384f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
6394f26d6c4Sdrh ** statement is completely executed or an error occurs.
640b900aaf3Sdrh **
641b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step()
642b900aaf3Sdrh ** API.  The only thing omitted is the automatic recompile if a
643b900aaf3Sdrh ** schema change has occurred.  That detail is handled by the
644b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure.
6454f26d6c4Sdrh */
sqlite3Step(Vdbe * p)646b900aaf3Sdrh static int sqlite3Step(Vdbe *p){
6479bb575fdSdrh   sqlite3 *db;
6484f26d6c4Sdrh   int rc;
6494f26d6c4Sdrh 
65099a21828Sdrh   assert(p);
65135e9e350Sdrh   db = p->db;
65299a21828Sdrh   if( p->eVdbeState!=VDBE_RUN_STATE ){
65399a21828Sdrh     restart_step:
65499a21828Sdrh     if( p->eVdbeState==VDBE_READY_STATE ){
65599a21828Sdrh       if( p->expired ){
656a21c6b6fSdanielk1977         p->rc = SQLITE_SCHEMA;
657b900aaf3Sdrh         rc = SQLITE_ERROR;
6585cbb442eSdrh         if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
6595cbb442eSdrh           /* If this statement was prepared using saved SQL and an
6605cbb442eSdrh           ** error has occurred, then return the error code in p->rc to the
66199a21828Sdrh           ** caller. Set the error code in the database handle to the same
66299a21828Sdrh           ** value.
6635cbb442eSdrh           */
6645cbb442eSdrh           rc = sqlite3VdbeTransferError(p);
6655cbb442eSdrh         }
666b900aaf3Sdrh         goto end_of_step;
667a21c6b6fSdanielk1977       }
66899a21828Sdrh 
66915ca1df1Sdrh       /* If there are no other statements currently running, then
67015ca1df1Sdrh       ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
67115ca1df1Sdrh       ** from interrupting a statement that has not yet started.
67215ca1df1Sdrh       */
6734f7d3a5fSdrh       if( db->nVdbeActive==0 ){
674892edb69Sdan         AtomicStore(&db->u1.isInterrupted, 0);
67515ca1df1Sdrh       }
67615ca1df1Sdrh 
677888e16e7Sdrh       assert( db->nVdbeWrite>0 || db->autoCommit==0
678cb3e4b79Sdan           || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
679cb3e4b79Sdan       );
6801da40a38Sdan 
68119e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
68204c6747aSdrh       if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
6833d2a529dSdrh           && !db->init.busy && p->zSql ){
684b7e8ea20Sdrh         sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
685201e0c68Sdrh       }else{
686201e0c68Sdrh         assert( p->startTime==0 );
68719e2d37fSdrh       }
68819e2d37fSdrh #endif
689c16a03b5Sdrh 
6904f7d3a5fSdrh       db->nVdbeActive++;
6914f7d3a5fSdrh       if( p->readOnly==0 ) db->nVdbeWrite++;
6921713afb0Sdrh       if( p->bIsReader ) db->nVdbeRead++;
6931d850a72Sdanielk1977       p->pc = 0;
69499a21828Sdrh       p->eVdbeState = VDBE_RUN_STATE;
69599a21828Sdrh     }else
69699a21828Sdrh 
69717c4865bSdrh     if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
69899a21828Sdrh       /* We used to require that sqlite3_reset() be called before retrying
69999a21828Sdrh       ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
70099a21828Sdrh       ** with version 3.7.0, we changed this so that sqlite3_reset() would
70199a21828Sdrh       ** be called automatically instead of throwing the SQLITE_MISUSE error.
70299a21828Sdrh       ** This "automatic-reset" change is not technically an incompatibility,
70399a21828Sdrh       ** since any application that receives an SQLITE_MISUSE is broken by
70499a21828Sdrh       ** definition.
70599a21828Sdrh       **
70699a21828Sdrh       ** Nevertheless, some published applications that were originally written
70799a21828Sdrh       ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
70899a21828Sdrh       ** returns, and those were broken by the automatic-reset change.  As a
70999a21828Sdrh       ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
71099a21828Sdrh       ** legacy behavior of returning SQLITE_MISUSE for cases where the
71199a21828Sdrh       ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
71299a21828Sdrh       ** or SQLITE_BUSY error.
71399a21828Sdrh       */
71499a21828Sdrh #ifdef SQLITE_OMIT_AUTORESET
71599a21828Sdrh       if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
71699a21828Sdrh         sqlite3_reset((sqlite3_stmt*)p);
71799a21828Sdrh       }else{
71899a21828Sdrh         return SQLITE_MISUSE_BKPT;
7191d850a72Sdanielk1977       }
72099a21828Sdrh #else
72199a21828Sdrh       sqlite3_reset((sqlite3_stmt*)p);
72299a21828Sdrh #endif
72399a21828Sdrh       assert( p->eVdbeState==VDBE_READY_STATE );
72499a21828Sdrh       goto restart_step;
72599a21828Sdrh     }
72699a21828Sdrh   }
72799a21828Sdrh 
728983b5ee7Sdrh #ifdef SQLITE_DEBUG
729983b5ee7Sdrh   p->rcApp = SQLITE_OK;
730983b5ee7Sdrh #endif
731b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
7324f26d6c4Sdrh   if( p->explain ){
7334f26d6c4Sdrh     rc = sqlite3VdbeList(p);
734b7f9164eSdrh   }else
735b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
736b7f9164eSdrh   {
7374f7d3a5fSdrh     db->nVdbeExec++;
7384f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
7394f7d3a5fSdrh     db->nVdbeExec--;
7404f26d6c4Sdrh   }
7414f26d6c4Sdrh 
7427e62146bSdrh   if( rc==SQLITE_ROW ){
7437e62146bSdrh     assert( p->rc==SQLITE_OK );
7447e62146bSdrh     assert( db->mallocFailed==0 );
7457e62146bSdrh     db->errCode = SQLITE_ROW;
7467e62146bSdrh     return SQLITE_ROW;
7477e62146bSdrh   }else{
74819e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
749201e0c68Sdrh     /* If the statement completed successfully, invoke the profile callback */
750b7de8271Sdrh     checkProfileCallback(db, p);
75119e2d37fSdrh #endif
75219e2d37fSdrh 
75359533aa5Sdrh     if( rc==SQLITE_DONE && db->autoCommit ){
7548d22a174Sdan       assert( p->rc==SQLITE_OK );
7557ed91f23Sdrh       p->rc = doWalCallbacks(db);
7568d22a174Sdan       if( p->rc!=SQLITE_OK ){
7578d22a174Sdan         rc = SQLITE_ERROR;
7588d22a174Sdan       }
7595cbb442eSdrh     }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
7602c2f392dSdrh       /* If this statement was prepared using saved SQL and an
76148864df9Smistachkin       ** error has occurred, then return the error code in p->rc to the
762357864ecSdanielk1977       ** caller. Set the error code in the database handle to the same value.
763357864ecSdanielk1977       */
764029ead64Sdan       rc = sqlite3VdbeTransferError(p);
765357864ecSdanielk1977     }
7665cbb442eSdrh   }
7675cbb442eSdrh 
7685cbb442eSdrh   db->errCode = rc;
7695cbb442eSdrh   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
7705cbb442eSdrh     p->rc = SQLITE_NOMEM_BKPT;
7715cbb442eSdrh     if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
7725cbb442eSdrh   }
7735cbb442eSdrh end_of_step:
7745cbb442eSdrh   /* There are only a limited number of result codes allowed from the
7755cbb442eSdrh   ** statements prepared using the legacy sqlite3_prepare() interface */
7765cbb442eSdrh   assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
7775cbb442eSdrh        || rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
7785cbb442eSdrh        || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
7795cbb442eSdrh   );
780357864ecSdanielk1977   return (rc&db->errMask);
781b900aaf3Sdrh }
782b900aaf3Sdrh 
783b900aaf3Sdrh /*
784b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step().  Call
785b900aaf3Sdrh ** sqlite3Step() to do most of the work.  If a schema error occurs,
786b900aaf3Sdrh ** call sqlite3Reprepare() and try again.
787b900aaf3Sdrh */
sqlite3_step(sqlite3_stmt * pStmt)788b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
789a6129fa7Sdrh   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
790a6129fa7Sdrh   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
791a6129fa7Sdrh   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
792a6129fa7Sdrh   sqlite3 *db;             /* The database connection */
793a6129fa7Sdrh 
794413c3d36Sdrh   if( vdbeSafetyNotNull(v) ){
795413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
796413c3d36Sdrh   }
797413c3d36Sdrh   db = v->db;
7988e556520Sdanielk1977   sqlite3_mutex_enter(db->mutex);
799b900aaf3Sdrh   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
8002c7946a4Sdrh          && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
8012c7946a4Sdrh     int savedPc = v->pc;
802754ee285Sdrh     rc = sqlite3Reprepare(v);
803754ee285Sdrh     if( rc!=SQLITE_OK ){
8048e556520Sdanielk1977       /* This case occurs after failing to recompile an sql statement.
8058e556520Sdanielk1977       ** The error message from the SQL compiler has already been loaded
8068e556520Sdanielk1977       ** into the database handle. This block copies the error message
8078e556520Sdanielk1977       ** from the database handle into the statement and sets the statement
8088e556520Sdanielk1977       ** program counter to 0 to ensure that when the statement is
8098e556520Sdanielk1977       ** finalized or reset the parser error message is available via
8108e556520Sdanielk1977       ** sqlite3_errmsg() and sqlite3_errcode().
8118e556520Sdanielk1977       */
8128e556520Sdanielk1977       const char *zErr = (const char *)sqlite3_value_text(db->pErr);
813633e6d57Sdrh       sqlite3DbFree(db, v->zErrMsg);
8148e556520Sdanielk1977       if( !db->mallocFailed ){
8158e556520Sdanielk1977         v->zErrMsg = sqlite3DbStrDup(db, zErr);
816754ee285Sdrh         v->rc = rc = sqlite3ApiExit(db, rc);
8178e556520Sdanielk1977       } else {
8188e556520Sdanielk1977         v->zErrMsg = 0;
819fad3039cSmistachkin         v->rc = rc = SQLITE_NOMEM_BKPT;
8208e556520Sdanielk1977       }
821754ee285Sdrh       break;
8228e556520Sdanielk1977     }
823754ee285Sdrh     sqlite3_reset(pStmt);
824a24832b7Sdrh     if( savedPc>=0 ){
825a24832b7Sdrh       /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
8262591cfb6Sdrh       ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
8272591cfb6Sdrh       ** already been done once on a prior invocation that failed due to
8282591cfb6Sdrh       ** SQLITE_SCHEMA.   tag-20220401a  */
829a24832b7Sdrh       v->minWriteFileFormat = 254;
830a24832b7Sdrh     }
831754ee285Sdrh     assert( v->expired==0 );
832754ee285Sdrh   }
8338e556520Sdanielk1977   sqlite3_mutex_leave(db->mutex);
834b900aaf3Sdrh   return rc;
835b900aaf3Sdrh }
8364f26d6c4Sdrh 
83795a7b3e3Sdrh 
8384f26d6c4Sdrh /*
839eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
840eb2e176aSdrh ** pointer to it.
841eb2e176aSdrh */
sqlite3_user_data(sqlite3_context * p)842eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
843eb2e176aSdrh   assert( p && p->pFunc );
844eb2e176aSdrh   return p->pFunc->pUserData;
845eb2e176aSdrh }
846eb2e176aSdrh 
847eb2e176aSdrh /*
848fa4a4b91Sdrh ** Extract the user data from a sqlite3_context structure and return a
849fa4a4b91Sdrh ** pointer to it.
8509f129f46Sdrh **
8519f129f46Sdrh ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
8529f129f46Sdrh ** returns a copy of the pointer to the database connection (the 1st
8539f129f46Sdrh ** parameter) of the sqlite3_create_function() and
8549f129f46Sdrh ** sqlite3_create_function16() routines that originally registered the
8559f129f46Sdrh ** application defined function.
856fa4a4b91Sdrh */
sqlite3_context_db_handle(sqlite3_context * p)857fa4a4b91Sdrh sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
858a46a4a63Sdrh   assert( p && p->pOut );
8599bd038f1Sdrh   return p->pOut->db;
860fa4a4b91Sdrh }
861fa4a4b91Sdrh 
862fa4a4b91Sdrh /*
8636f390bebSdrh ** If this routine is invoked from within an xColumn method of a virtual
8646f390bebSdrh ** table, then it returns true if and only if the the call is during an
8656f390bebSdrh ** UPDATE operation and the value of the column will not be modified
8666f390bebSdrh ** by the UPDATE.
8676f390bebSdrh **
8686f390bebSdrh ** If this routine is called from any context other than within the
8696f390bebSdrh ** xColumn method of a virtual table, then the return value is meaningless
8706f390bebSdrh ** and arbitrary.
8716f390bebSdrh **
8726f390bebSdrh ** Virtual table implements might use this routine to optimize their
8736f390bebSdrh ** performance by substituting a NULL result, or some other light-weight
8746f390bebSdrh ** value, as a signal to the xUpdate routine that the column is unchanged.
8756f390bebSdrh */
sqlite3_vtab_nochange(sqlite3_context * p)8766f390bebSdrh int sqlite3_vtab_nochange(sqlite3_context *p){
8776f390bebSdrh   assert( p );
878ce2fbd1bSdrh   return sqlite3_value_nochange(p->pOut);
8796f390bebSdrh }
8806f390bebSdrh 
8816f390bebSdrh /*
8820fe7e7d9Sdrh ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
8830fe7e7d9Sdrh ** sqlite3_vtab_in_next() (if bNext!=0).
8840fe7e7d9Sdrh */
valueFromValueList(sqlite3_value * pVal,sqlite3_value ** ppOut,int bNext)88530e314e4Sdrh static int valueFromValueList(
88630e314e4Sdrh   sqlite3_value *pVal,        /* Pointer to the ValueList object */
88730e314e4Sdrh   sqlite3_value **ppOut,      /* Store the next value from the list here */
88830e314e4Sdrh   int bNext                   /* 1 for _next(). 0 for _first() */
88930e314e4Sdrh ){
890b30298d3Sdrh   int rc;
89130e314e4Sdrh   ValueList *pRhs;
89230e314e4Sdrh 
8930fe7e7d9Sdrh   *ppOut = 0;
894b30298d3Sdrh   if( pVal==0 ) return SQLITE_MISUSE;
89530e314e4Sdrh   pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList");
89630e314e4Sdrh   if( pRhs==0 ) return SQLITE_MISUSE;
8970fe7e7d9Sdrh   if( bNext ){
89830e314e4Sdrh     rc = sqlite3BtreeNext(pRhs->pCsr, 0);
8990fe7e7d9Sdrh   }else{
9000fe7e7d9Sdrh     int dummy = 0;
90130e314e4Sdrh     rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
9023d7a69e5Sdrh     assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
9033d7a69e5Sdrh     if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
9040fe7e7d9Sdrh   }
905b30298d3Sdrh   if( rc==SQLITE_OK ){
90630e314e4Sdrh     u32 sz;       /* Size of current row in bytes */
90730e314e4Sdrh     Mem sMem;     /* Raw content of current row */
90830e314e4Sdrh     memset(&sMem, 0, sizeof(sMem));
90930e314e4Sdrh     sz = sqlite3BtreePayloadSize(pRhs->pCsr);
91030e314e4Sdrh     rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
91130e314e4Sdrh     if( rc==SQLITE_OK ){
91230e314e4Sdrh       u8 *zBuf = (u8*)sMem.z;
91330e314e4Sdrh       u32 iSerial;
91430e314e4Sdrh       sqlite3_value *pOut = pRhs->pOut;
91530e314e4Sdrh       int iOff = 1 + getVarint32(&zBuf[1], iSerial);
91630e314e4Sdrh       sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);
91738d1e443Sdrh       pOut->enc = ENC(pOut->db);
91830e314e4Sdrh       if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
91930e314e4Sdrh         rc = SQLITE_NOMEM;
92030e314e4Sdrh       }else{
92130e314e4Sdrh         *ppOut = pOut;
92230e314e4Sdrh       }
92330e314e4Sdrh     }
92430e314e4Sdrh     sqlite3VdbeMemRelease(&sMem);
9250fe7e7d9Sdrh   }
9260fe7e7d9Sdrh   return rc;
9270fe7e7d9Sdrh }
9280fe7e7d9Sdrh 
9290fe7e7d9Sdrh /*
9300fe7e7d9Sdrh ** Set the iterator value pVal to point to the first value in the set.
9310fe7e7d9Sdrh ** Set (*ppOut) to point to this value before returning.
9320fe7e7d9Sdrh */
sqlite3_vtab_in_first(sqlite3_value * pVal,sqlite3_value ** ppOut)9330fe7e7d9Sdrh int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
93430e314e4Sdrh   return valueFromValueList(pVal, ppOut, 0);
9350fe7e7d9Sdrh }
9360fe7e7d9Sdrh 
9370fe7e7d9Sdrh /*
9380fe7e7d9Sdrh ** Set the iterator value pVal to point to the next value in the set.
9390fe7e7d9Sdrh ** Set (*ppOut) to point to this value before returning.
9400fe7e7d9Sdrh */
sqlite3_vtab_in_next(sqlite3_value * pVal,sqlite3_value ** ppOut)9410fe7e7d9Sdrh int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
94230e314e4Sdrh   return valueFromValueList(pVal, ppOut, 1);
9430fe7e7d9Sdrh }
9440fe7e7d9Sdrh 
9450fe7e7d9Sdrh /*
946a9e03b1bSdrh ** Return the current time for a statement.  If the current time
947a9e03b1bSdrh ** is requested more than once within the same run of a single prepared
948a9e03b1bSdrh ** statement, the exact same time is returned for each invocation regardless
949a9e03b1bSdrh ** of the amount of time that elapses between invocations.  In other words,
950a9e03b1bSdrh ** the time returned is always the time of the first call.
95195a7b3e3Sdrh */
sqlite3StmtCurrentTime(sqlite3_context * p)95295a7b3e3Sdrh sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
95395a7b3e3Sdrh   int rc;
954175b8f06Sdrh #ifndef SQLITE_ENABLE_STAT4
9553df79a9aSdrh   sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
956a9e03b1bSdrh   assert( p->pVdbe!=0 );
957a9e03b1bSdrh #else
9583df79a9aSdrh   sqlite3_int64 iTime = 0;
959a9e03b1bSdrh   sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
960a9e03b1bSdrh #endif
9613df79a9aSdrh   if( *piTime==0 ){
962a9e03b1bSdrh     rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
963a9e03b1bSdrh     if( rc ) *piTime = 0;
96495a7b3e3Sdrh   }
965a9e03b1bSdrh   return *piTime;
96695a7b3e3Sdrh }
96795a7b3e3Sdrh 
96895a7b3e3Sdrh /*
9699de4a171Sdrh ** Create a new aggregate context for p and return a pointer to
9709de4a171Sdrh ** its pMem->z element.
971eb2e176aSdrh */
createAggContext(sqlite3_context * p,int nByte)9729de4a171Sdrh static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
9739de4a171Sdrh   Mem *pMem = p->pMem;
9749de4a171Sdrh   assert( (pMem->flags & MEM_Agg)==0 );
975d68eee04Sdrh   if( nByte<=0 ){
9760725cabeSdrh     sqlite3VdbeMemSetNull(pMem);
977a10a34b8Sdrh     pMem->z = 0;
978a10a34b8Sdrh   }else{
979322f2852Sdrh     sqlite3VdbeMemClearAndResize(pMem, nByte);
980abfcea25Sdrh     pMem->flags = MEM_Agg;
9813c024d69Sdrh     pMem->u.pDef = p->pFunc;
9825f096135Sdanielk1977     if( pMem->z ){
9835f096135Sdanielk1977       memset(pMem->z, 0, nByte);
9845f096135Sdanielk1977     }
985eb2e176aSdrh   }
986abfcea25Sdrh   return (void*)pMem->z;
987eb2e176aSdrh }
988eb2e176aSdrh 
989eb2e176aSdrh /*
9909de4a171Sdrh ** Allocate or return the aggregate context for a user function.  A new
9919de4a171Sdrh ** context is allocated on the first call.  Subsequent calls return the
9929de4a171Sdrh ** same context that was returned on prior calls.
9939de4a171Sdrh */
sqlite3_aggregate_context(sqlite3_context * p,int nByte)9949de4a171Sdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
9952d80151fSdrh   assert( p && p->pFunc && p->pFunc->xFinalize );
9969bd038f1Sdrh   assert( sqlite3_mutex_held(p->pOut->db->mutex) );
9979de4a171Sdrh   testcase( nByte<0 );
9989de4a171Sdrh   if( (p->pMem->flags & MEM_Agg)==0 ){
9999de4a171Sdrh     return createAggContext(p, nByte);
10009de4a171Sdrh   }else{
10019de4a171Sdrh     return (void*)p->pMem->z;
10029de4a171Sdrh   }
10039de4a171Sdrh }
10049de4a171Sdrh 
10059de4a171Sdrh /*
100660ec914cSpeter.d.reid ** Return the auxiliary data pointer, if any, for the iArg'th argument to
1007682f68b0Sdanielk1977 ** the user-function defined by pCtx.
1008f7fa4e71Sdrh **
1009f7fa4e71Sdrh ** The left-most argument is 0.
1010f7fa4e71Sdrh **
1011f7fa4e71Sdrh ** Undocumented behavior:  If iArg is negative then access a cache of
1012f7fa4e71Sdrh ** auxiliary data pointers that is available to all functions within a
1013f7fa4e71Sdrh ** single prepared statement.  The iArg values must match.
1014682f68b0Sdanielk1977 */
sqlite3_get_auxdata(sqlite3_context * pCtx,int iArg)1015682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
10160c547799Sdan   AuxData *pAuxData;
1017b21c8cd4Sdrh 
10189bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
1019175b8f06Sdrh #if SQLITE_ENABLE_STAT4
102018bf8076Sdan   if( pCtx->pVdbe==0 ) return 0;
1021a9e03b1bSdrh #else
1022a9e03b1bSdrh   assert( pCtx->pVdbe!=0 );
1023a9e03b1bSdrh #endif
1024e6941397Sdrh   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
1025f7fa4e71Sdrh     if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
1026e6941397Sdrh       return pAuxData->pAux;
1027682f68b0Sdanielk1977     }
1028e6941397Sdrh   }
1029e6941397Sdrh   return 0;
1030682f68b0Sdanielk1977 }
1031682f68b0Sdanielk1977 
1032682f68b0Sdanielk1977 /*
103360ec914cSpeter.d.reid ** Set the auxiliary data pointer and delete function, for the iArg'th
1034682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
1035682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
1036f7fa4e71Sdrh **
1037f7fa4e71Sdrh ** The left-most argument is 0.
1038f7fa4e71Sdrh **
1039f7fa4e71Sdrh ** Undocumented behavior:  If iArg is negative then make the data available
1040f7fa4e71Sdrh ** to all functions within the current prepared statement using iArg as an
1041f7fa4e71Sdrh ** access code.
1042682f68b0Sdanielk1977 */
sqlite3_set_auxdata(sqlite3_context * pCtx,int iArg,void * pAux,void (* xDelete)(void *))1043682f68b0Sdanielk1977 void sqlite3_set_auxdata(
1044682f68b0Sdanielk1977   sqlite3_context *pCtx,
1045682f68b0Sdanielk1977   int iArg,
1046682f68b0Sdanielk1977   void *pAux,
1047682f68b0Sdanielk1977   void (*xDelete)(void*)
1048682f68b0Sdanielk1977 ){
10490c547799Sdan   AuxData *pAuxData;
10500c547799Sdan   Vdbe *pVdbe = pCtx->pVdbe;
1051682f68b0Sdanielk1977 
10529bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
1053175b8f06Sdrh #ifdef SQLITE_ENABLE_STAT4
105418bf8076Sdan   if( pVdbe==0 ) goto failed;
1055a9e03b1bSdrh #else
1056a9e03b1bSdrh   assert( pVdbe!=0 );
1057a9e03b1bSdrh #endif
1058682f68b0Sdanielk1977 
1059e6941397Sdrh   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
1060f7fa4e71Sdrh     if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
1061f7fa4e71Sdrh       break;
1062f7fa4e71Sdrh     }
10630c547799Sdan   }
10640c547799Sdan   if( pAuxData==0 ){
10650c547799Sdan     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
10660c547799Sdan     if( !pAuxData ) goto failed;
1067e6941397Sdrh     pAuxData->iAuxOp = pCtx->iOp;
1068e6941397Sdrh     pAuxData->iAuxArg = iArg;
1069e6941397Sdrh     pAuxData->pNextAux = pVdbe->pAuxData;
10700c547799Sdan     pVdbe->pAuxData = pAuxData;
1071f09ac0b3Sdrh     if( pCtx->isError==0 ) pCtx->isError = -1;
1072e6941397Sdrh   }else if( pAuxData->xDeleteAux ){
1073e6941397Sdrh     pAuxData->xDeleteAux(pAuxData->pAux);
1074682f68b0Sdanielk1977   }
10750c547799Sdan 
1076682f68b0Sdanielk1977   pAuxData->pAux = pAux;
1077e6941397Sdrh   pAuxData->xDeleteAux = xDelete;
1078e0fc5261Sdanielk1977   return;
1079e0fc5261Sdanielk1977 
1080e0fc5261Sdanielk1977 failed:
1081e0fc5261Sdanielk1977   if( xDelete ){
1082e0fc5261Sdanielk1977     xDelete(pAux);
1083e0fc5261Sdanielk1977   }
1084682f68b0Sdanielk1977 }
1085682f68b0Sdanielk1977 
1086eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
1087682f68b0Sdanielk1977 /*
108860ec914cSpeter.d.reid ** Return the number of times the Step function of an aggregate has been
1089eb2e176aSdrh ** called.
1090eb2e176aSdrh **
1091cf85a51cSdrh ** This function is deprecated.  Do not use it for new code.  It is
1092cf85a51cSdrh ** provide only to avoid breaking legacy code.  New aggregate function
1093cf85a51cSdrh ** implementations should keep their own counts within their aggregate
1094cf85a51cSdrh ** context.
1095eb2e176aSdrh */
sqlite3_aggregate_count(sqlite3_context * p)1096eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
10972d80151fSdrh   assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
1098abfcea25Sdrh   return p->pMem->n;
1099eb2e176aSdrh }
1100eec556d3Sshane #endif
1101eb2e176aSdrh 
1102eb2e176aSdrh /*
11034f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
11044f26d6c4Sdrh */
sqlite3_column_count(sqlite3_stmt * pStmt)11054f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
11064f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
11071bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
11084f26d6c4Sdrh }
11094f26d6c4Sdrh 
11104f26d6c4Sdrh /*
11114f26d6c4Sdrh ** Return the number of values available from the current row of the
11124f26d6c4Sdrh ** currently executing statement pStmt.
11134f26d6c4Sdrh */
sqlite3_data_count(sqlite3_stmt * pStmt)11144f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
11154f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
1116d4e70ebdSdrh   if( pVm==0 || pVm->pResultSet==0 ) return 0;
11174f26d6c4Sdrh   return pVm->nResColumn;
11184f26d6c4Sdrh }
11194f26d6c4Sdrh 
112046c47d46Sdan /*
112146c47d46Sdan ** Return a pointer to static memory containing an SQL NULL value.
112246c47d46Sdan */
columnNullValue(void)112346c47d46Sdan static const Mem *columnNullValue(void){
112446c47d46Sdan   /* Even though the Mem structure contains an element
1125b8a45bbdSdrh   ** of type i64, on certain architectures (x86) with certain compiler
112646c47d46Sdan   ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
112746c47d46Sdan   ** instead of an 8-byte one. This all works fine, except that when
112846c47d46Sdan   ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
112946c47d46Sdan   ** that a Mem structure is located on an 8-byte boundary. To prevent
1130b8a45bbdSdrh   ** these assert()s from failing, when building with SQLITE_DEBUG defined
1131b8a45bbdSdrh   ** using gcc, we force nullMem to be 8-byte aligned using the magical
113246c47d46Sdan   ** __attribute__((aligned(8))) macro.  */
113346c47d46Sdan   static const Mem nullMem
113446c47d46Sdan #if defined(SQLITE_DEBUG) && defined(__GNUC__)
113546c47d46Sdan     __attribute__((aligned(8)))
113646c47d46Sdan #endif
1137035e563bSdrh     = {
11383329a63aSdrh         /* .u          = */ {0},
1139c9373e86Sdrh         /* .z          = */ (char*)0,
1140c9373e86Sdrh         /* .n          = */ (int)0,
11418faee877Sdrh         /* .flags      = */ (u16)MEM_Null,
11428faee877Sdrh         /* .enc        = */ (u8)0,
11438faee877Sdrh         /* .eSubtype   = */ (u8)0,
1144c9373e86Sdrh         /* .db         = */ (sqlite3*)0,
11458faee877Sdrh         /* .szMalloc   = */ (int)0,
11468faee877Sdrh         /* .uTemp      = */ (u32)0,
1147c9373e86Sdrh         /* .zMalloc    = */ (char*)0,
11488faee877Sdrh         /* .xDel       = */ (void(*)(void*))0,
1149fcd71b60Sdrh #ifdef SQLITE_DEBUG
11508faee877Sdrh         /* .pScopyFrom = */ (Mem*)0,
115158773a53Sdrh         /* .mScopyFlags= */ 0,
115258773a53Sdrh #endif
1153035e563bSdrh       };
115446c47d46Sdan   return &nullMem;
115546c47d46Sdan }
11564f26d6c4Sdrh 
11574f26d6c4Sdrh /*
11584f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
11594f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
11604f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
11614f26d6c4Sdrh ** of NULL.
11624f26d6c4Sdrh */
columnMem(sqlite3_stmt * pStmt,int i)11634f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
116432bc3f6eSdrh   Vdbe *pVm;
116532bc3f6eSdrh   Mem *pOut;
116632bc3f6eSdrh 
116732bc3f6eSdrh   pVm = (Vdbe *)pStmt;
116828f17017Sdrh   if( pVm==0 ) return (Mem*)columnNullValue();
116928f17017Sdrh   assert( pVm->db );
117032bc3f6eSdrh   sqlite3_mutex_enter(pVm->db->mutex);
117128f17017Sdrh   if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
1172d4e70ebdSdrh     pOut = &pVm->pResultSet[i];
117332bc3f6eSdrh   }else{
117413f40da3Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE);
117546c47d46Sdan     pOut = (Mem*)columnNullValue();
11764f26d6c4Sdrh   }
117732bc3f6eSdrh   return pOut;
11784f26d6c4Sdrh }
11794f26d6c4Sdrh 
11802e588c75Sdanielk1977 /*
11812e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a
11822e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the
11832e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If
11842e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
11852e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM.
11862e588c75Sdanielk1977 **
118732bc3f6eSdrh ** Specifically, this is called from within:
11882e588c75Sdanielk1977 **
11892e588c75Sdanielk1977 **     sqlite3_column_int()
11902e588c75Sdanielk1977 **     sqlite3_column_int64()
11912e588c75Sdanielk1977 **     sqlite3_column_text()
11922e588c75Sdanielk1977 **     sqlite3_column_text16()
11932e588c75Sdanielk1977 **     sqlite3_column_real()
11942e588c75Sdanielk1977 **     sqlite3_column_bytes()
11952e588c75Sdanielk1977 **     sqlite3_column_bytes16()
119642262536Sdrh **     sqiite3_column_blob()
11972e588c75Sdanielk1977 */
columnMallocFailure(sqlite3_stmt * pStmt)11982e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt)
11992e588c75Sdanielk1977 {
12002e588c75Sdanielk1977   /* If malloc() failed during an encoding conversion within an
12012e588c75Sdanielk1977   ** sqlite3_column_XXX API, then set the return code of the statement to
12022e588c75Sdanielk1977   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
12032e588c75Sdanielk1977   ** and _finalize() will return NOMEM.
12042e588c75Sdanielk1977   */
120554f0198eSdanielk1977   Vdbe *p = (Vdbe *)pStmt;
120632bc3f6eSdrh   if( p ){
120728f17017Sdrh     assert( p->db!=0 );
120828f17017Sdrh     assert( sqlite3_mutex_held(p->db->mutex) );
120932bc3f6eSdrh     p->rc = sqlite3ApiExit(p->db, p->rc);
121032bc3f6eSdrh     sqlite3_mutex_leave(p->db->mutex);
121132bc3f6eSdrh   }
12122e588c75Sdanielk1977 }
12132e588c75Sdanielk1977 
12144f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
12154f26d6c4Sdrh ** The following routines are used to access elements of the current row
12164f26d6c4Sdrh ** in the result set.
12174f26d6c4Sdrh */
sqlite3_column_blob(sqlite3_stmt * pStmt,int i)1218c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
12192e588c75Sdanielk1977   const void *val;
12202e588c75Sdanielk1977   val = sqlite3_value_blob( columnMem(pStmt,i) );
1221c9cf901dSdanielk1977   /* Even though there is no encoding conversion, value_blob() might
1222c9cf901dSdanielk1977   ** need to call malloc() to expand the result of a zeroblob()
1223c9cf901dSdanielk1977   ** expression.
1224c9cf901dSdanielk1977   */
1225c9cf901dSdanielk1977   columnMallocFailure(pStmt);
12262e588c75Sdanielk1977   return val;
1227c572ef7fSdanielk1977 }
sqlite3_column_bytes(sqlite3_stmt * pStmt,int i)12284f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
12292e588c75Sdanielk1977   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
12302e588c75Sdanielk1977   columnMallocFailure(pStmt);
12312e588c75Sdanielk1977   return val;
12324f26d6c4Sdrh }
sqlite3_column_bytes16(sqlite3_stmt * pStmt,int i)12334f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
12342e588c75Sdanielk1977   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
12352e588c75Sdanielk1977   columnMallocFailure(pStmt);
12362e588c75Sdanielk1977   return val;
12374f26d6c4Sdrh }
sqlite3_column_double(sqlite3_stmt * pStmt,int i)12384f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
12392e588c75Sdanielk1977   double val = sqlite3_value_double( columnMem(pStmt,i) );
12402e588c75Sdanielk1977   columnMallocFailure(pStmt);
12412e588c75Sdanielk1977   return val;
12424f26d6c4Sdrh }
sqlite3_column_int(sqlite3_stmt * pStmt,int i)12434f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
12442e588c75Sdanielk1977   int val = sqlite3_value_int( columnMem(pStmt,i) );
12452e588c75Sdanielk1977   columnMallocFailure(pStmt);
12462e588c75Sdanielk1977   return val;
12474f26d6c4Sdrh }
sqlite3_column_int64(sqlite3_stmt * pStmt,int i)1248efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
12492e588c75Sdanielk1977   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
12502e588c75Sdanielk1977   columnMallocFailure(pStmt);
12512e588c75Sdanielk1977   return val;
12524f26d6c4Sdrh }
sqlite3_column_text(sqlite3_stmt * pStmt,int i)12534f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
12542e588c75Sdanielk1977   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
12552e588c75Sdanielk1977   columnMallocFailure(pStmt);
12562e588c75Sdanielk1977   return val;
12574f26d6c4Sdrh }
sqlite3_column_value(sqlite3_stmt * pStmt,int i)1258d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
1259d0ffa1e8Sdanielk1977   Mem *pOut = columnMem(pStmt, i);
1260d0ffa1e8Sdanielk1977   if( pOut->flags&MEM_Static ){
1261d0ffa1e8Sdanielk1977     pOut->flags &= ~MEM_Static;
1262d0ffa1e8Sdanielk1977     pOut->flags |= MEM_Ephem;
1263d0ffa1e8Sdanielk1977   }
126432bc3f6eSdrh   columnMallocFailure(pStmt);
1265d0ffa1e8Sdanielk1977   return (sqlite3_value *)pOut;
1266d1e4733dSdrh }
12676c62608fSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_text16(sqlite3_stmt * pStmt,int i)12684f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
12692e588c75Sdanielk1977   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
12702e588c75Sdanielk1977   columnMallocFailure(pStmt);
12712e588c75Sdanielk1977   return val;
12724f26d6c4Sdrh }
12736c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
sqlite3_column_type(sqlite3_stmt * pStmt,int i)12744f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
127532bc3f6eSdrh   int iType = sqlite3_value_type( columnMem(pStmt,i) );
127632bc3f6eSdrh   columnMallocFailure(pStmt);
127732bc3f6eSdrh   return iType;
12784f26d6c4Sdrh }
12794f26d6c4Sdrh 
12805e2517e1Sdrh /*
12815e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
12825e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
1283e590fbdeSdrh **
1284e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
1285e590fbdeSdrh ** name is returned.  Here are the names:
1286e590fbdeSdrh **
1287e590fbdeSdrh **    0      The column name as it should be displayed for output
1288e590fbdeSdrh **    1      The datatype name for the column
1289e590fbdeSdrh **    2      The name of the database that the column derives from
1290e590fbdeSdrh **    3      The name of the table that the column derives from
1291e590fbdeSdrh **    4      The name of the table column that the result column derives from
1292e590fbdeSdrh **
1293e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
1294e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
12955e2517e1Sdrh */
columnName(sqlite3_stmt * pStmt,int N,int useUtf16,int useType)12965e2517e1Sdrh static const void *columnName(
12978cf92890Sdrh   sqlite3_stmt *pStmt,     /* The statement */
12988cf92890Sdrh   int N,                   /* Which column to get the name for */
12998cf92890Sdrh   int useUtf16,            /* True to return the name as UTF16 */
13008cf92890Sdrh   int useType              /* What type of name */
13015e2517e1Sdrh ){
13029ca95730Sdrh   const void *ret;
13039ca95730Sdrh   Vdbe *p;
130432bc3f6eSdrh   int n;
13059ca95730Sdrh   sqlite3 *db;
13069ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
13079ca95730Sdrh   if( pStmt==0 ){
13089ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
13099ca95730Sdrh     return 0;
13109ca95730Sdrh   }
13119ca95730Sdrh #endif
13129ca95730Sdrh   ret = 0;
13139ca95730Sdrh   p = (Vdbe *)pStmt;
13149ca95730Sdrh   db = p->db;
1315c6c7fd51Sdrh   assert( db!=0 );
131632bc3f6eSdrh   n = sqlite3_column_count(pStmt);
131732bc3f6eSdrh   if( N<n && N>=0 ){
1318e590fbdeSdrh     N += useType*n;
1319c6c7fd51Sdrh     sqlite3_mutex_enter(db->mutex);
1320c6c7fd51Sdrh     assert( db->mallocFailed==0 );
13214474e869Sdan #ifndef SQLITE_OMIT_UTF16
13228cf92890Sdrh     if( useUtf16 ){
13238cf92890Sdrh       ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
13244474e869Sdan     }else
13254474e869Sdan #endif
13264474e869Sdan     {
13278cf92890Sdrh       ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
13288cf92890Sdrh     }
13298cf92890Sdrh     /* A malloc may have failed inside of the _text() call. If this
133032bc3f6eSdrh     ** is the case, clear the mallocFailed flag and return NULL.
133100fd957bSdanielk1977     */
1332c6c7fd51Sdrh     if( db->mallocFailed ){
13334a642b60Sdrh       sqlite3OomClear(db);
133432bc3f6eSdrh       ret = 0;
133532bc3f6eSdrh     }
1336c6c7fd51Sdrh     sqlite3_mutex_leave(db->mutex);
133732bc3f6eSdrh   }
133800fd957bSdanielk1977   return ret;
13395e2517e1Sdrh }
13405e2517e1Sdrh 
13414f26d6c4Sdrh /*
13424f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
13434f26d6c4Sdrh ** statement pStmt.
13444f26d6c4Sdrh */
sqlite3_column_name(sqlite3_stmt * pStmt,int N)13454f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
13468cf92890Sdrh   return columnName(pStmt, N, 0, COLNAME_NAME);
13474f26d6c4Sdrh }
1348e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_name16(sqlite3_stmt * pStmt,int N)1349e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
13508cf92890Sdrh   return columnName(pStmt, N, 1, COLNAME_NAME);
1351e590fbdeSdrh }
1352e590fbdeSdrh #endif
13534f26d6c4Sdrh 
13544f26d6c4Sdrh /*
13553f913576Sdrh ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
13563f913576Sdrh ** not define OMIT_DECLTYPE.
13573f913576Sdrh */
13583f913576Sdrh #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
13593f913576Sdrh # error "Must not define both SQLITE_OMIT_DECLTYPE \
13603f913576Sdrh          and SQLITE_ENABLE_COLUMN_METADATA"
13613f913576Sdrh #endif
13623f913576Sdrh 
13633f913576Sdrh #ifndef SQLITE_OMIT_DECLTYPE
13643f913576Sdrh /*
13656c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
1366e590fbdeSdrh ** of the result set of SQL statement pStmt.
13676c62608fSdrh */
sqlite3_column_decltype(sqlite3_stmt * pStmt,int N)13686c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
13698cf92890Sdrh   return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
13706c62608fSdrh }
13716c62608fSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_decltype16(sqlite3_stmt * pStmt,int N)137276d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
13738cf92890Sdrh   return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
13744f26d6c4Sdrh }
13756c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
13763f913576Sdrh #endif /* SQLITE_OMIT_DECLTYPE */
13774f26d6c4Sdrh 
13784b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1379e590fbdeSdrh /*
1380e590fbdeSdrh ** Return the name of the database from which a result column derives.
1381e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
138260ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1383e590fbdeSdrh */
sqlite3_column_database_name(sqlite3_stmt * pStmt,int N)1384e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
13858cf92890Sdrh   return columnName(pStmt, N, 0, COLNAME_DATABASE);
1386e590fbdeSdrh }
1387e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_database_name16(sqlite3_stmt * pStmt,int N)1388e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
13898cf92890Sdrh   return columnName(pStmt, N, 1, COLNAME_DATABASE);
1390e590fbdeSdrh }
1391e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
1392e590fbdeSdrh 
1393e590fbdeSdrh /*
1394e590fbdeSdrh ** Return the name of the table from which a result column derives.
1395e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
139660ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1397e590fbdeSdrh */
sqlite3_column_table_name(sqlite3_stmt * pStmt,int N)1398e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
13998cf92890Sdrh   return columnName(pStmt, N, 0, COLNAME_TABLE);
1400e590fbdeSdrh }
1401e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_table_name16(sqlite3_stmt * pStmt,int N)1402e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
14038cf92890Sdrh   return columnName(pStmt, N, 1, COLNAME_TABLE);
1404e590fbdeSdrh }
1405e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
1406e590fbdeSdrh 
1407e590fbdeSdrh /*
1408e590fbdeSdrh ** Return the name of the table column from which a result column derives.
1409e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
141060ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1411e590fbdeSdrh */
sqlite3_column_origin_name(sqlite3_stmt * pStmt,int N)1412e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
14138cf92890Sdrh   return columnName(pStmt, N, 0, COLNAME_COLUMN);
1414e590fbdeSdrh }
1415e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_column_origin_name16(sqlite3_stmt * pStmt,int N)1416e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
14178cf92890Sdrh   return columnName(pStmt, N, 1, COLNAME_COLUMN);
1418e590fbdeSdrh }
1419e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
14204b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
1421e590fbdeSdrh 
1422e590fbdeSdrh 
14234f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
14244f26d6c4Sdrh **
14254f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
14264f26d6c4Sdrh */
14274f26d6c4Sdrh /*
14284f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
14294f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
14304f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
14314f26d6c4Sdrh **
1432488e7b63Sdrh ** A successful evaluation of this routine acquires the mutex on p.
1433488e7b63Sdrh ** the mutex is released if any kind of error occurs.
1434488e7b63Sdrh **
14354f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
14364f26d6c4Sdrh ** value in any case.
14374f26d6c4Sdrh */
vdbeUnbind(Vdbe * p,unsigned int i)1438403f0021Sdrh static int vdbeUnbind(Vdbe *p, unsigned int i){
14394f26d6c4Sdrh   Mem *pVar;
1440413c3d36Sdrh   if( vdbeSafetyNotNull(p) ){
1441413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
1442413c3d36Sdrh   }
1443488e7b63Sdrh   sqlite3_mutex_enter(p->db->mutex);
144499a21828Sdrh   if( p->eVdbeState!=VDBE_READY_STATE ){
144513f40da3Sdrh     sqlite3Error(p->db, SQLITE_MISUSE);
1446488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
1447413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE,
1448413c3d36Sdrh         "bind on a busy prepared statement: [%s]", p->zSql);
1449413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
14504f26d6c4Sdrh   }
1451e2848932Smistachkin   if( i>=(unsigned int)p->nVar ){
145213f40da3Sdrh     sqlite3Error(p->db, SQLITE_RANGE);
1453488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
14544f26d6c4Sdrh     return SQLITE_RANGE;
14554f26d6c4Sdrh   }
1456290c1948Sdrh   pVar = &p->aVar[i];
1457d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
14584f26d6c4Sdrh   pVar->flags = MEM_Null;
1459368bfe8bSdrh   p->db->errCode = SQLITE_OK;
1460937d0deaSdan 
14611d2ce4f8Sdan   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
14621d2ce4f8Sdan   ** binding a new value to this variable invalidates the current query plan.
1463a7044007Sdrh   **
1464bbf6d432Sdrh   ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
1465a7044007Sdrh   ** parameter in the WHERE clause might influence the choice of query plan
1466a7044007Sdrh   ** for a statement, then the statement will be automatically recompiled,
1467a7044007Sdrh   ** as if there had been a schema change, on the first sqlite3_step() call
1468a7044007Sdrh   ** following any change to the bindings of that parameter.
14691d2ce4f8Sdan   */
14702c2f392dSdrh   assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
14712996796dSdrh   if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
1472937d0deaSdan     p->expired = 1;
1473937d0deaSdan   }
14744f26d6c4Sdrh   return SQLITE_OK;
14754f26d6c4Sdrh }
14764f26d6c4Sdrh 
14774f26d6c4Sdrh /*
14785e2517e1Sdrh ** Bind a text or BLOB value.
14794f26d6c4Sdrh */
bindText(sqlite3_stmt * pStmt,int i,const void * zData,i64 nData,void (* xDel)(void *),u8 encoding)14805e2517e1Sdrh static int bindText(
1481605264d2Sdrh   sqlite3_stmt *pStmt,   /* The statement to bind against */
1482605264d2Sdrh   int i,                 /* Index of the parameter to bind */
1483605264d2Sdrh   const void *zData,     /* Pointer to the data to be bound */
1484d622855eSdrh   i64 nData,             /* Number of bytes of data to be bound */
1485605264d2Sdrh   void (*xDel)(void*),   /* Destructor for the data */
1486b27b7f5dSdrh   u8 encoding            /* Encoding for the data */
14874f26d6c4Sdrh ){
14884f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
14894f26d6c4Sdrh   Mem *pVar;
14904f26d6c4Sdrh   int rc;
14914f26d6c4Sdrh 
1492403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
1493488e7b63Sdrh   if( rc==SQLITE_OK ){
1494488e7b63Sdrh     if( zData!=0 ){
1495290c1948Sdrh       pVar = &p->aVar[i-1];
1496b21c8cd4Sdrh       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
14975e2517e1Sdrh       if( rc==SQLITE_OK && encoding!=0 ){
1498b21c8cd4Sdrh         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
14995e2517e1Sdrh       }
1500e70d01f1Sdrh       if( rc ){
150113f40da3Sdrh         sqlite3Error(p->db, rc);
1502605264d2Sdrh         rc = sqlite3ApiExit(p->db, rc);
150327641703Sdrh       }
1504e70d01f1Sdrh     }
1505605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
150694bb2ba6Sdrh   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
15076fec9ee3Sdrh     xDel((void*)zData);
1508488e7b63Sdrh   }
1509605264d2Sdrh   return rc;
15105e2517e1Sdrh }
15115e2517e1Sdrh 
15125e2517e1Sdrh 
15135e2517e1Sdrh /*
15145e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
15155e2517e1Sdrh */
sqlite3_bind_blob(sqlite3_stmt * pStmt,int i,const void * zData,int nData,void (* xDel)(void *))15165e2517e1Sdrh int sqlite3_bind_blob(
15175e2517e1Sdrh   sqlite3_stmt *pStmt,
15185e2517e1Sdrh   int i,
15195e2517e1Sdrh   const void *zData,
15205e2517e1Sdrh   int nData,
15215e2517e1Sdrh   void (*xDel)(void*)
15225e2517e1Sdrh ){
15235b081d8aSdrh #ifdef SQLITE_ENABLE_API_ARMOR
15245b081d8aSdrh   if( nData<0 ) return SQLITE_MISUSE_BKPT;
15255b081d8aSdrh #endif
15265e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
15275e2517e1Sdrh }
sqlite3_bind_blob64(sqlite3_stmt * pStmt,int i,const void * zData,sqlite3_uint64 nData,void (* xDel)(void *))1528da4ca9d1Sdrh int sqlite3_bind_blob64(
1529da4ca9d1Sdrh   sqlite3_stmt *pStmt,
1530da4ca9d1Sdrh   int i,
1531da4ca9d1Sdrh   const void *zData,
1532da4ca9d1Sdrh   sqlite3_uint64 nData,
1533da4ca9d1Sdrh   void (*xDel)(void*)
1534da4ca9d1Sdrh ){
1535fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
1536d622855eSdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
1537da4ca9d1Sdrh }
sqlite3_bind_double(sqlite3_stmt * pStmt,int i,double rValue)15384f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
15394f26d6c4Sdrh   int rc;
15404f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
1541403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
15424f26d6c4Sdrh   if( rc==SQLITE_OK ){
1543290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1544605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1545488e7b63Sdrh   }
1546f4618891Sdanielk1977   return rc;
15474f26d6c4Sdrh }
sqlite3_bind_int(sqlite3_stmt * p,int i,int iValue)15484f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
1549efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
15504f26d6c4Sdrh }
sqlite3_bind_int64(sqlite3_stmt * pStmt,int i,sqlite_int64 iValue)1551efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
15524f26d6c4Sdrh   int rc;
15534f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
1554403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
15554f26d6c4Sdrh   if( rc==SQLITE_OK ){
1556290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
1557605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1558488e7b63Sdrh   }
15594f26d6c4Sdrh   return rc;
15604f26d6c4Sdrh }
sqlite3_bind_null(sqlite3_stmt * pStmt,int i)1561605264d2Sdrh int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1562605264d2Sdrh   int rc;
1563605264d2Sdrh   Vdbe *p = (Vdbe*)pStmt;
1564403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
1565488e7b63Sdrh   if( rc==SQLITE_OK ){
1566605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1567488e7b63Sdrh   }
1568605264d2Sdrh   return rc;
15694f26d6c4Sdrh }
sqlite3_bind_pointer(sqlite3_stmt * pStmt,int i,void * pPtr,const char * zPTtype,void (* xDestructor)(void *))157022930062Sdrh int sqlite3_bind_pointer(
157122930062Sdrh   sqlite3_stmt *pStmt,
157222930062Sdrh   int i,
157322930062Sdrh   void *pPtr,
157422930062Sdrh   const char *zPTtype,
157522930062Sdrh   void (*xDestructor)(void*)
157622930062Sdrh ){
15773a96a5d9Sdrh   int rc;
15783a96a5d9Sdrh   Vdbe *p = (Vdbe*)pStmt;
1579403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
15803a96a5d9Sdrh   if( rc==SQLITE_OK ){
158122930062Sdrh     sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
15823a96a5d9Sdrh     sqlite3_mutex_leave(p->db->mutex);
158334fcf36dSdrh   }else if( xDestructor ){
158434fcf36dSdrh     xDestructor(pPtr);
15853a96a5d9Sdrh   }
15863a96a5d9Sdrh   return rc;
15873a96a5d9Sdrh }
sqlite3_bind_text(sqlite3_stmt * pStmt,int i,const char * zData,int nData,void (* xDel)(void *))15884f26d6c4Sdrh int sqlite3_bind_text(
15894f26d6c4Sdrh   sqlite3_stmt *pStmt,
15904f26d6c4Sdrh   int i,
15914f26d6c4Sdrh   const char *zData,
15924f26d6c4Sdrh   int nData,
1593d8123366Sdanielk1977   void (*xDel)(void*)
15944f26d6c4Sdrh ){
15955e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
15964f26d6c4Sdrh }
sqlite3_bind_text64(sqlite3_stmt * pStmt,int i,const char * zData,sqlite3_uint64 nData,void (* xDel)(void *),unsigned char enc)1597bbf483f8Sdrh int sqlite3_bind_text64(
1598da4ca9d1Sdrh   sqlite3_stmt *pStmt,
1599da4ca9d1Sdrh   int i,
1600da4ca9d1Sdrh   const char *zData,
1601da4ca9d1Sdrh   sqlite3_uint64 nData,
1602da4ca9d1Sdrh   void (*xDel)(void*),
1603da4ca9d1Sdrh   unsigned char enc
1604da4ca9d1Sdrh ){
1605fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
1606fc59a954Sdrh   if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
1607d622855eSdrh   return bindText(pStmt, i, zData, nData, xDel, enc);
1608da4ca9d1Sdrh }
16096c62608fSdrh #ifndef SQLITE_OMIT_UTF16
sqlite3_bind_text16(sqlite3_stmt * pStmt,int i,const void * zData,int nData,void (* xDel)(void *))16104f26d6c4Sdrh int sqlite3_bind_text16(
16114f26d6c4Sdrh   sqlite3_stmt *pStmt,
16124f26d6c4Sdrh   int i,
16134f26d6c4Sdrh   const void *zData,
16144f26d6c4Sdrh   int nData,
1615d8123366Sdanielk1977   void (*xDel)(void*)
16164f26d6c4Sdrh ){
16175e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
16184f26d6c4Sdrh }
16196c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
sqlite3_bind_value(sqlite3_stmt * pStmt,int i,const sqlite3_value * pValue)162026e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
162126e4144dSdanielk1977   int rc;
16221b27b8c0Sdrh   switch( sqlite3_value_type((sqlite3_value*)pValue) ){
162329def560Sdrh     case SQLITE_INTEGER: {
162429def560Sdrh       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
162529def560Sdrh       break;
162629def560Sdrh     }
162729def560Sdrh     case SQLITE_FLOAT: {
162863a47336Sdan       assert( pValue->flags & (MEM_Real|MEM_IntReal) );
162963a47336Sdan       rc = sqlite3_bind_double(pStmt, i,
163063a47336Sdan           (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
163163a47336Sdan       );
163229def560Sdrh       break;
163329def560Sdrh     }
163429def560Sdrh     case SQLITE_BLOB: {
163529def560Sdrh       if( pValue->flags & MEM_Zero ){
163629def560Sdrh         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
163729def560Sdrh       }else{
163829def560Sdrh         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
163929def560Sdrh       }
164029def560Sdrh       break;
164129def560Sdrh     }
164229def560Sdrh     case SQLITE_TEXT: {
1643ac80db78Sdrh       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
1644ac80db78Sdrh                               pValue->enc);
1645ac80db78Sdrh       break;
1646ac80db78Sdrh     }
1647ac80db78Sdrh     default: {
1648ac80db78Sdrh       rc = sqlite3_bind_null(pStmt, i);
164929def560Sdrh       break;
165029def560Sdrh     }
16510f5ea0b3Sdrh   }
165226e4144dSdanielk1977   return rc;
165326e4144dSdanielk1977 }
sqlite3_bind_zeroblob(sqlite3_stmt * pStmt,int i,int n)1654b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1655b026e05eSdrh   int rc;
1656b026e05eSdrh   Vdbe *p = (Vdbe *)pStmt;
1657403f0021Sdrh   rc = vdbeUnbind(p, (u32)(i-1));
1658b026e05eSdrh   if( rc==SQLITE_OK ){
1659a32536b4Sdan #ifndef SQLITE_OMIT_INCRBLOB
1660b026e05eSdrh     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1661a32536b4Sdan #else
1662a32536b4Sdan     rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1663a32536b4Sdan #endif
1664605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1665488e7b63Sdrh   }
1666b026e05eSdrh   return rc;
1667b026e05eSdrh }
sqlite3_bind_zeroblob64(sqlite3_stmt * pStmt,int i,sqlite3_uint64 n)166880c03022Sdan int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
166980c03022Sdan   int rc;
167080c03022Sdan   Vdbe *p = (Vdbe *)pStmt;
167180c03022Sdan   sqlite3_mutex_enter(p->db->mutex);
167280c03022Sdan   if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
167380c03022Sdan     rc = SQLITE_TOOBIG;
167480c03022Sdan   }else{
167580c03022Sdan     assert( (n & 0x7FFFFFFF)==n );
167680c03022Sdan     rc = sqlite3_bind_zeroblob(pStmt, i, n);
167780c03022Sdan   }
167880c03022Sdan   rc = sqlite3ApiExit(p->db, rc);
167980c03022Sdan   sqlite3_mutex_leave(p->db->mutex);
168080c03022Sdan   return rc;
168180c03022Sdan }
168275f6a032Sdrh 
168375f6a032Sdrh /*
168475f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
168575f6a032Sdrh ** This routine is added to support DBD::SQLite.
168675f6a032Sdrh */
sqlite3_bind_parameter_count(sqlite3_stmt * pStmt)168775f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
168892febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
168992febd92Sdrh   return p ? p->nVar : 0;
169075f6a032Sdrh }
1691895d7472Sdrh 
1692895d7472Sdrh /*
1693fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
1694fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
1695fa6bc000Sdrh **
1696fa6bc000Sdrh ** The result is always UTF-8.
1697fa6bc000Sdrh */
sqlite3_bind_parameter_name(sqlite3_stmt * pStmt,int i)1698fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1699fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
17009bf755ccSdrh   if( p==0 ) return 0;
17019bf755ccSdrh   return sqlite3VListNumToName(p->pVList, i);
1702895d7472Sdrh }
1703fa6bc000Sdrh 
1704fa6bc000Sdrh /*
1705fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
1706fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
1707fa6bc000Sdrh ** return 0.
1708fa6bc000Sdrh */
sqlite3VdbeParameterIndex(Vdbe * p,const char * zName,int nName)17095f18a221Sdrh int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
17109bf755ccSdrh   if( p==0 || zName==0 ) return 0;
17119bf755ccSdrh   return sqlite3VListNameToNum(p->pVList, zName, nName);
1712fa6bc000Sdrh }
sqlite3_bind_parameter_index(sqlite3_stmt * pStmt,const char * zName)17135f18a221Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
17145f18a221Sdrh   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
17155f18a221Sdrh }
1716f8db1bc0Sdrh 
171751942bc3Sdrh /*
1718f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
1719f8db1bc0Sdrh */
sqlite3TransferBindings(sqlite3_stmt * pFromStmt,sqlite3_stmt * pToStmt)1720145834a4Sshane int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1721f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
1722f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
17230f5ea0b3Sdrh   int i;
17240f5ea0b3Sdrh   assert( pTo->db==pFrom->db );
17250f5ea0b3Sdrh   assert( pTo->nVar==pFrom->nVar );
172627641703Sdrh   sqlite3_mutex_enter(pTo->db->mutex);
17270f5ea0b3Sdrh   for(i=0; i<pFrom->nVar; i++){
1728643167ffSdrh     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1729f8db1bc0Sdrh   }
173027641703Sdrh   sqlite3_mutex_leave(pTo->db->mutex);
17310f5ea0b3Sdrh   return SQLITE_OK;
1732f8db1bc0Sdrh }
173351942bc3Sdrh 
1734eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
173551942bc3Sdrh /*
1736145834a4Sshane ** Deprecated external interface.  Internal/core SQLite code
1737145834a4Sshane ** should call sqlite3TransferBindings.
17380f5ea0b3Sdrh **
173960ec914cSpeter.d.reid ** It is misuse to call this routine with statements from different
17400f5ea0b3Sdrh ** database connections.  But as this is a deprecated interface, we
17410f5ea0b3Sdrh ** will not bother to check for that condition.
17420f5ea0b3Sdrh **
17430f5ea0b3Sdrh ** If the two statements contain a different number of bindings, then
17440f5ea0b3Sdrh ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
17450f5ea0b3Sdrh ** SQLITE_OK is returned.
1746145834a4Sshane */
sqlite3_transfer_bindings(sqlite3_stmt * pFromStmt,sqlite3_stmt * pToStmt)1747145834a4Sshane int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
17480f5ea0b3Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
17490f5ea0b3Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
17500f5ea0b3Sdrh   if( pFrom->nVar!=pTo->nVar ){
17510f5ea0b3Sdrh     return SQLITE_ERROR;
17520f5ea0b3Sdrh   }
17532c2f392dSdrh   assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
1754bda4cb87Sdan   if( pTo->expmask ){
1755c94b8595Sdan     pTo->expired = 1;
1756c94b8595Sdan   }
17572c2f392dSdrh   assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
1758bda4cb87Sdan   if( pFrom->expmask ){
1759c94b8595Sdan     pFrom->expired = 1;
1760c94b8595Sdan   }
1761145834a4Sshane   return sqlite3TransferBindings(pFromStmt, pToStmt);
1762145834a4Sshane }
1763eec556d3Sshane #endif
1764145834a4Sshane 
1765145834a4Sshane /*
176651942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
176751942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
176851942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
176951942bc3Sdrh ** the statement in the first place.
177051942bc3Sdrh */
sqlite3_db_handle(sqlite3_stmt * pStmt)177151942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
177251942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
177351942bc3Sdrh }
1774bb5a9c3eSdrh 
1775bb5a9c3eSdrh /*
1776f03d9cccSdrh ** Return true if the prepared statement is guaranteed to not modify the
1777f03d9cccSdrh ** database.
1778f03d9cccSdrh */
sqlite3_stmt_readonly(sqlite3_stmt * pStmt)1779f03d9cccSdrh int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1780f03d9cccSdrh   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1781f03d9cccSdrh }
1782f03d9cccSdrh 
1783f03d9cccSdrh /*
178439c5c4aeSdrh ** Return 1 if the statement is an EXPLAIN and return 2 if the
178539c5c4aeSdrh ** statement is an EXPLAIN QUERY PLAN
178639c5c4aeSdrh */
sqlite3_stmt_isexplain(sqlite3_stmt * pStmt)178739c5c4aeSdrh int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
178839c5c4aeSdrh   return pStmt ? ((Vdbe*)pStmt)->explain : 0;
178939c5c4aeSdrh }
179039c5c4aeSdrh 
179139c5c4aeSdrh /*
17922fb6693eSdrh ** Return true if the prepared statement is in need of being reset.
17932fb6693eSdrh */
sqlite3_stmt_busy(sqlite3_stmt * pStmt)17942fb6693eSdrh int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
17952fb6693eSdrh   Vdbe *v = (Vdbe*)pStmt;
179699a21828Sdrh   return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
17972fb6693eSdrh }
17982fb6693eSdrh 
17992fb6693eSdrh /*
1800bb5a9c3eSdrh ** Return a pointer to the next prepared statement after pStmt associated
1801bb5a9c3eSdrh ** with database connection pDb.  If pStmt is NULL, return the first
1802bb5a9c3eSdrh ** prepared statement for the database connection.  Return NULL if there
1803bb5a9c3eSdrh ** are no more.
1804bb5a9c3eSdrh */
sqlite3_next_stmt(sqlite3 * pDb,sqlite3_stmt * pStmt)1805bb5a9c3eSdrh sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1806bb5a9c3eSdrh   sqlite3_stmt *pNext;
18079ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
18089ca95730Sdrh   if( !sqlite3SafetyCheckOk(pDb) ){
18099ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
18109ca95730Sdrh     return 0;
18119ca95730Sdrh   }
18129ca95730Sdrh #endif
1813bb5a9c3eSdrh   sqlite3_mutex_enter(pDb->mutex);
1814bb5a9c3eSdrh   if( pStmt==0 ){
1815bb5a9c3eSdrh     pNext = (sqlite3_stmt*)pDb->pVdbe;
1816bb5a9c3eSdrh   }else{
1817e5928b17Sdrh     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
1818bb5a9c3eSdrh   }
1819bb5a9c3eSdrh   sqlite3_mutex_leave(pDb->mutex);
1820bb5a9c3eSdrh   return pNext;
1821bb5a9c3eSdrh }
1822d1d38488Sdrh 
1823d1d38488Sdrh /*
1824d1d38488Sdrh ** Return the value of a status counter for a prepared statement
1825d1d38488Sdrh */
sqlite3_stmt_status(sqlite3_stmt * pStmt,int op,int resetFlag)1826d1d38488Sdrh int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1827d1d38488Sdrh   Vdbe *pVdbe = (Vdbe*)pStmt;
18289ca95730Sdrh   u32 v;
18299ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
183068cf69edSdan   if( !pStmt
183168cf69edSdan    || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
183268cf69edSdan   ){
18339ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
18349ca95730Sdrh     return 0;
18359ca95730Sdrh   }
18369ca95730Sdrh #endif
18373528f6b6Sdrh   if( op==SQLITE_STMTSTATUS_MEMUSED ){
18383528f6b6Sdrh     sqlite3 *db = pVdbe->db;
18393528f6b6Sdrh     sqlite3_mutex_enter(db->mutex);
18403528f6b6Sdrh     v = 0;
18413528f6b6Sdrh     db->pnBytesFreed = (int*)&v;
1842376860baSdrh     assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
1843376860baSdrh     db->lookaside.pEnd = db->lookaside.pStart;
18441c848630Sdrh     sqlite3VdbeDelete(pVdbe);
18453528f6b6Sdrh     db->pnBytesFreed = 0;
1846376860baSdrh     db->lookaside.pEnd = db->lookaside.pTrueEnd;
18473528f6b6Sdrh     sqlite3_mutex_leave(db->mutex);
18483528f6b6Sdrh   }else{
18499ca95730Sdrh     v = pVdbe->aCounter[op];
18509b47ee3fSdrh     if( resetFlag ) pVdbe->aCounter[op] = 0;
18513528f6b6Sdrh   }
18529b47ee3fSdrh   return (int)v;
1853d1d38488Sdrh }
185446c47d46Sdan 
1855557341e8Sdrh /*
1856557341e8Sdrh ** Return the SQL associated with a prepared statement
1857557341e8Sdrh */
sqlite3_sql(sqlite3_stmt * pStmt)1858557341e8Sdrh const char *sqlite3_sql(sqlite3_stmt *pStmt){
1859557341e8Sdrh   Vdbe *p = (Vdbe *)pStmt;
1860557341e8Sdrh   return p ? p->zSql : 0;
1861557341e8Sdrh }
1862557341e8Sdrh 
1863557341e8Sdrh /*
1864557341e8Sdrh ** Return the SQL associated with a prepared statement with
1865557341e8Sdrh ** bound parameters expanded.  Space to hold the returned string is
1866557341e8Sdrh ** obtained from sqlite3_malloc().  The caller is responsible for
1867557341e8Sdrh ** freeing the returned string by passing it to sqlite3_free().
1868557341e8Sdrh **
1869557341e8Sdrh ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1870557341e8Sdrh ** expanded bound parameters.
1871557341e8Sdrh */
sqlite3_expanded_sql(sqlite3_stmt * pStmt)1872557341e8Sdrh char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1873557341e8Sdrh #ifdef SQLITE_OMIT_TRACE
1874557341e8Sdrh   return 0;
1875557341e8Sdrh #else
1876557341e8Sdrh   char *z = 0;
1877557341e8Sdrh   const char *zSql = sqlite3_sql(pStmt);
1878557341e8Sdrh   if( zSql ){
1879557341e8Sdrh     Vdbe *p = (Vdbe *)pStmt;
1880557341e8Sdrh     sqlite3_mutex_enter(p->db->mutex);
1881557341e8Sdrh     z = sqlite3VdbeExpandSql(p, zSql);
1882557341e8Sdrh     sqlite3_mutex_leave(p->db->mutex);
1883557341e8Sdrh   }
1884557341e8Sdrh   return z;
1885557341e8Sdrh #endif
1886557341e8Sdrh }
1887557341e8Sdrh 
18888bee11a4Smistachkin #ifdef SQLITE_ENABLE_NORMALIZE
18898bee11a4Smistachkin /*
18908bee11a4Smistachkin ** Return the normalized SQL associated with a prepared statement.
18918bee11a4Smistachkin */
sqlite3_normalized_sql(sqlite3_stmt * pStmt)18928bee11a4Smistachkin const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
18938bee11a4Smistachkin   Vdbe *p = (Vdbe *)pStmt;
1894707821ffSdrh   if( p==0 ) return 0;
18951a6c2b1dSdrh   if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
18961f169fefSdrh     sqlite3_mutex_enter(p->db->mutex);
18971a6c2b1dSdrh     p->zNormSql = sqlite3Normalize(p, p->zSql);
18981f169fefSdrh     sqlite3_mutex_leave(p->db->mutex);
1899707821ffSdrh   }
1900707821ffSdrh   return p->zNormSql;
19018bee11a4Smistachkin }
19028bee11a4Smistachkin #endif /* SQLITE_ENABLE_NORMALIZE */
19038bee11a4Smistachkin 
19049b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
190537db03bfSdan /*
190693bca695Sdan ** Allocate and populate an UnpackedRecord structure based on the serialized
190793bca695Sdan ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
190893bca695Sdan ** if successful, or a NULL pointer if an OOM error is encountered.
190993bca695Sdan */
vdbeUnpackRecord(KeyInfo * pKeyInfo,int nKey,const void * pKey)191093bca695Sdan static UnpackedRecord *vdbeUnpackRecord(
191193bca695Sdan   KeyInfo *pKeyInfo,
191293bca695Sdan   int nKey,
191393bca695Sdan   const void *pKey
191493bca695Sdan ){
191593bca695Sdan   UnpackedRecord *pRet;           /* Return value */
191693bca695Sdan 
1917a582b016Sdrh   pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
191893bca695Sdan   if( pRet ){
1919a485ad19Sdrh     memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
192093bca695Sdan     sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
192193bca695Sdan   }
192293bca695Sdan   return pRet;
192393bca695Sdan }
192493bca695Sdan 
192593bca695Sdan /*
192637db03bfSdan ** This function is called from within a pre-update callback to retrieve
192737db03bfSdan ** a field of the row currently being updated or deleted.
192837db03bfSdan */
sqlite3_preupdate_old(sqlite3 * db,int iIdx,sqlite3_value ** ppValue)192946c47d46Sdan int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
193046c47d46Sdan   PreUpdate *p = db->pPreUpdate;
19317271d7a1Sdan   Mem *pMem;
193246c47d46Sdan   int rc = SQLITE_OK;
193346c47d46Sdan 
193437db03bfSdan   /* Test that this call is being made from within an SQLITE_DELETE or
193537db03bfSdan   ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
193646c47d46Sdan   if( !p || p->op==SQLITE_INSERT ){
193746c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
193846c47d46Sdan     goto preupdate_old_out;
193946c47d46Sdan   }
1940cb9a3643Sdan   if( p->pPk ){
1941b9bcf7caSdrh     iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
1942cb9a3643Sdan   }
194346c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
194446c47d46Sdan     rc = SQLITE_RANGE;
194546c47d46Sdan     goto preupdate_old_out;
194646c47d46Sdan   }
194746c47d46Sdan 
194837db03bfSdan   /* If the old.* record has not yet been loaded into memory, do so now. */
194946c47d46Sdan   if( p->pUnpacked==0 ){
195012ca0b56Sdan     u32 nRec;
195112ca0b56Sdan     u8 *aRec;
195246c47d46Sdan 
19533ab4ffceSdrh     assert( p->pCsr->eCurType==CURTYPE_BTREE );
1954a7c90c42Sdrh     nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
195512ca0b56Sdan     aRec = sqlite3DbMallocRaw(db, nRec);
195612ca0b56Sdan     if( !aRec ) goto preupdate_old_out;
1957cb3cabd0Sdrh     rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
195812ca0b56Sdan     if( rc==SQLITE_OK ){
195993bca695Sdan       p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
196012ca0b56Sdan       if( !p->pUnpacked ) rc = SQLITE_NOMEM;
196112ca0b56Sdan     }
196246c47d46Sdan     if( rc!=SQLITE_OK ){
196312ca0b56Sdan       sqlite3DbFree(db, aRec);
196446c47d46Sdan       goto preupdate_old_out;
196546c47d46Sdan     }
196612ca0b56Sdan     p->aRecord = aRec;
196746c47d46Sdan   }
196846c47d46Sdan 
19697271d7a1Sdan   pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1970e43635aaSdan   if( iIdx==p->pTab->iPKey ){
1971e43635aaSdan     sqlite3VdbeMemSetInt64(pMem, p->iKey1);
19727271d7a1Sdan   }else if( iIdx>=p->pUnpacked->nField ){
19737271d7a1Sdan     *ppValue = (sqlite3_value *)columnNullValue();
1974e43635aaSdan   }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1975169f077eSdrh     if( pMem->flags & (MEM_Int|MEM_IntReal) ){
19763242c69cSdrh       testcase( pMem->flags & MEM_Int );
19773242c69cSdrh       testcase( pMem->flags & MEM_IntReal );
1978e43635aaSdan       sqlite3VdbeMemRealify(pMem);
1979e43635aaSdan     }
1980319eeb7bSdan   }
198146c47d46Sdan 
198246c47d46Sdan  preupdate_old_out:
1983e1ed0b0eSdrh   sqlite3Error(db, rc);
198446c47d46Sdan   return sqlite3ApiExit(db, rc);
198546c47d46Sdan }
19869b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
198746c47d46Sdan 
19889b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
198937db03bfSdan /*
199037db03bfSdan ** This function is called from within a pre-update callback to retrieve
199137db03bfSdan ** the number of columns in the row being updated, deleted or inserted.
199237db03bfSdan */
sqlite3_preupdate_count(sqlite3 * db)199346c47d46Sdan int sqlite3_preupdate_count(sqlite3 *db){
199446c47d46Sdan   PreUpdate *p = db->pPreUpdate;
1995a485ad19Sdrh   return (p ? p->keyinfo.nKeyField : 0);
199646c47d46Sdan }
19979b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
199846c47d46Sdan 
19999b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
200037db03bfSdan /*
20011e7a2d43Sdan ** This function is designed to be called from within a pre-update callback
20021e7a2d43Sdan ** only. It returns zero if the change that caused the callback was made
20031e7a2d43Sdan ** immediately by a user SQL statement. Or, if the change was made by a
20041e7a2d43Sdan ** trigger program, it returns the number of trigger programs currently
20051e7a2d43Sdan ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
20061e7a2d43Sdan ** top-level trigger etc.).
20071e7a2d43Sdan **
20081e7a2d43Sdan ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
20091e7a2d43Sdan ** or SET DEFAULT action is considered a trigger.
20101e7a2d43Sdan */
sqlite3_preupdate_depth(sqlite3 * db)20111e7a2d43Sdan int sqlite3_preupdate_depth(sqlite3 *db){
20121e7a2d43Sdan   PreUpdate *p = db->pPreUpdate;
20131e7a2d43Sdan   return (p ? p->v->nFrame : 0);
20141e7a2d43Sdan }
20159b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
20161e7a2d43Sdan 
20179b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
20181e7a2d43Sdan /*
2019a23a873fSdan ** This function is designed to be called from within a pre-update callback
2020a23a873fSdan ** only.
2021a23a873fSdan */
sqlite3_preupdate_blobwrite(sqlite3 * db)2022a23a873fSdan int sqlite3_preupdate_blobwrite(sqlite3 *db){
2023a23a873fSdan   PreUpdate *p = db->pPreUpdate;
2024a23a873fSdan   return (p ? p->iBlobWrite : -1);
2025a23a873fSdan }
2026a23a873fSdan #endif
2027a23a873fSdan 
2028a23a873fSdan #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2029a23a873fSdan /*
203037db03bfSdan ** This function is called from within a pre-update callback to retrieve
203137db03bfSdan ** a field of the row currently being updated or inserted.
203237db03bfSdan */
sqlite3_preupdate_new(sqlite3 * db,int iIdx,sqlite3_value ** ppValue)203337db03bfSdan int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
203446c47d46Sdan   PreUpdate *p = db->pPreUpdate;
203546c47d46Sdan   int rc = SQLITE_OK;
203637db03bfSdan   Mem *pMem;
203746c47d46Sdan 
203837db03bfSdan   if( !p || p->op==SQLITE_DELETE ){
203946c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
204037db03bfSdan     goto preupdate_new_out;
204146c47d46Sdan   }
2042cb9a3643Sdan   if( p->pPk && p->op!=SQLITE_UPDATE ){
2043b9bcf7caSdrh     iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
2044cb9a3643Sdan   }
204546c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
204646c47d46Sdan     rc = SQLITE_RANGE;
204737db03bfSdan     goto preupdate_new_out;
204846c47d46Sdan   }
204946c47d46Sdan 
205037db03bfSdan   if( p->op==SQLITE_INSERT ){
205137db03bfSdan     /* For an INSERT, memory cell p->iNewReg contains the serialized record
205237db03bfSdan     ** that is being inserted. Deserialize it. */
205337db03bfSdan     UnpackedRecord *pUnpack = p->pNewUnpacked;
205437db03bfSdan     if( !pUnpack ){
205537db03bfSdan       Mem *pData = &p->v->aMem[p->iNewReg];
2056ff535a24Sdrh       rc = ExpandBlob(pData);
205737db03bfSdan       if( rc!=SQLITE_OK ) goto preupdate_new_out;
205893bca695Sdan       pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
205937db03bfSdan       if( !pUnpack ){
206037db03bfSdan         rc = SQLITE_NOMEM;
206137db03bfSdan         goto preupdate_new_out;
206237db03bfSdan       }
206337db03bfSdan       p->pNewUnpacked = pUnpack;
206437db03bfSdan     }
206537db03bfSdan     pMem = &pUnpack->aMem[iIdx];
2066e43635aaSdan     if( iIdx==p->pTab->iPKey ){
2067319eeb7bSdan       sqlite3VdbeMemSetInt64(pMem, p->iKey2);
20682a86c196Sdan     }else if( iIdx>=pUnpack->nField ){
20692a86c196Sdan       pMem = (sqlite3_value *)columnNullValue();
207037db03bfSdan     }
207137db03bfSdan   }else{
207237db03bfSdan     /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
207337db03bfSdan     ** value. Make a copy of the cell contents and return a pointer to it.
207437db03bfSdan     ** It is not safe to return a pointer to the memory cell itself as the
207537db03bfSdan     ** caller may modify the value text encoding.
207637db03bfSdan     */
207737db03bfSdan     assert( p->op==SQLITE_UPDATE );
207837db03bfSdan     if( !p->aNew ){
207937db03bfSdan       p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
208037db03bfSdan       if( !p->aNew ){
208137db03bfSdan         rc = SQLITE_NOMEM;
208237db03bfSdan         goto preupdate_new_out;
208337db03bfSdan       }
208437db03bfSdan     }
2085304637c0Sdrh     assert( iIdx>=0 && iIdx<p->pCsr->nField );
208637db03bfSdan     pMem = &p->aNew[iIdx];
208737db03bfSdan     if( pMem->flags==0 ){
2088e43635aaSdan       if( iIdx==p->pTab->iPKey ){
2089319eeb7bSdan         sqlite3VdbeMemSetInt64(pMem, p->iKey2);
2090319eeb7bSdan       }else{
209137db03bfSdan         rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
209237db03bfSdan         if( rc!=SQLITE_OK ) goto preupdate_new_out;
2093319eeb7bSdan       }
209437db03bfSdan     }
209537db03bfSdan   }
209637db03bfSdan   *ppValue = pMem;
209737db03bfSdan 
209837db03bfSdan  preupdate_new_out:
2099e1ed0b0eSdrh   sqlite3Error(db, rc);
210046c47d46Sdan   return sqlite3ApiExit(db, rc);
210146c47d46Sdan }
21029b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
210304e8a586Sdrh 
21046f9702edSdan #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
210504489b6dSdan /*
210604489b6dSdan ** Return status data for a single loop within query pStmt.
210704489b6dSdan */
sqlite3_stmt_scanstatus(sqlite3_stmt * pStmt,int idx,int iScanStatusOp,void * pOut)210804489b6dSdan int sqlite3_stmt_scanstatus(
2109d1a1c234Sdrh   sqlite3_stmt *pStmt,            /* Prepared statement being queried */
211004489b6dSdan   int idx,                        /* Index of loop to report on */
2111d1a1c234Sdrh   int iScanStatusOp,              /* Which metric to return */
2112d1a1c234Sdrh   void *pOut                      /* OUT: Write the answer here */
211304489b6dSdan ){
211404489b6dSdan   Vdbe *p = (Vdbe*)pStmt;
2115037b5324Sdan   ScanStatus *pScan;
21166f9702edSdan   if( idx<0 || idx>=p->nScan ) return 1;
21176f9702edSdan   pScan = &p->aScan[idx];
2118d1a1c234Sdrh   switch( iScanStatusOp ){
2119d1a1c234Sdrh     case SQLITE_SCANSTAT_NLOOP: {
2120d1a1c234Sdrh       *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
2121d1a1c234Sdrh       break;
2122d1a1c234Sdrh     }
2123d1a1c234Sdrh     case SQLITE_SCANSTAT_NVISIT: {
2124d1a1c234Sdrh       *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
2125d1a1c234Sdrh       break;
2126d1a1c234Sdrh     }
2127d1a1c234Sdrh     case SQLITE_SCANSTAT_EST: {
2128518140edSdrh       double r = 1.0;
2129518140edSdrh       LogEst x = pScan->nEst;
2130518140edSdrh       while( x<100 ){
2131518140edSdrh         x += 10;
2132518140edSdrh         r *= 0.5;
2133518140edSdrh       }
2134518140edSdrh       *(double*)pOut = r*sqlite3LogEstToInt(x);
2135d1a1c234Sdrh       break;
2136d1a1c234Sdrh     }
2137d1a1c234Sdrh     case SQLITE_SCANSTAT_NAME: {
2138d72219daSdan       *(const char**)pOut = pScan->zName;
2139d1a1c234Sdrh       break;
2140d1a1c234Sdrh     }
2141d1a1c234Sdrh     case SQLITE_SCANSTAT_EXPLAIN: {
21426f9702edSdan       if( pScan->addrExplain ){
2143d1a1c234Sdrh         *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
21446f9702edSdan       }else{
2145d1a1c234Sdrh         *(const char**)pOut = 0;
2146d1a1c234Sdrh       }
2147d1a1c234Sdrh       break;
2148d1a1c234Sdrh     }
2149c6652b1eSdrh     case SQLITE_SCANSTAT_SELECTID: {
2150c6652b1eSdrh       if( pScan->addrExplain ){
2151c6652b1eSdrh         *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
2152c6652b1eSdrh       }else{
2153c6652b1eSdrh         *(int*)pOut = -1;
2154c6652b1eSdrh       }
2155c6652b1eSdrh       break;
2156c6652b1eSdrh     }
2157d1a1c234Sdrh     default: {
2158d1a1c234Sdrh       return 1;
21596f9702edSdan     }
21606f9702edSdan   }
216104489b6dSdan   return 0;
216204489b6dSdan }
216304489b6dSdan 
216404489b6dSdan /*
216504489b6dSdan ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
216604489b6dSdan */
sqlite3_stmt_scanstatus_reset(sqlite3_stmt * pStmt)216704489b6dSdan void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
216804489b6dSdan   Vdbe *p = (Vdbe*)pStmt;
21696f9702edSdan   memset(p->anExec, 0, p->nOp * sizeof(i64));
217004489b6dSdan }
21716f9702edSdan #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
2172