xref: /sqlite-3.40.0/src/vdbeapi.c (revision f7fa4e71)
14f26d6c4Sdrh /*
24f26d6c4Sdrh ** 2004 May 26
34f26d6c4Sdrh **
44f26d6c4Sdrh ** The author disclaims copyright to this source code.  In place of
54f26d6c4Sdrh ** a legal notice, here is a blessing:
64f26d6c4Sdrh **
74f26d6c4Sdrh **    May you do good and not evil.
84f26d6c4Sdrh **    May you find forgiveness for yourself and forgive others.
94f26d6c4Sdrh **    May you share freely, never taking more than you give.
104f26d6c4Sdrh **
114f26d6c4Sdrh *************************************************************************
124f26d6c4Sdrh **
134f26d6c4Sdrh ** This file contains code use to implement APIs that are part of the
144f26d6c4Sdrh ** VDBE.
154f26d6c4Sdrh */
164f26d6c4Sdrh #include "sqliteInt.h"
174f26d6c4Sdrh #include "vdbeInt.h"
184f26d6c4Sdrh 
19eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
20d89bd007Sdrh /*
21d89bd007Sdrh ** Return TRUE (non-zero) of the statement supplied as an argument needs
22d89bd007Sdrh ** to be recompiled.  A statement needs to be recompiled whenever the
23d89bd007Sdrh ** execution environment changes in a way that would alter the program
24d89bd007Sdrh ** that sqlite3_prepare() generates.  For example, if new functions or
25d89bd007Sdrh ** collating sequences are registered or if an authorizer function is
26d89bd007Sdrh ** added or changed.
27d89bd007Sdrh */
28d89bd007Sdrh int sqlite3_expired(sqlite3_stmt *pStmt){
29d89bd007Sdrh   Vdbe *p = (Vdbe*)pStmt;
30d89bd007Sdrh   return p==0 || p->expired;
31d89bd007Sdrh }
32eec556d3Sshane #endif
33d89bd007Sdrh 
34e30f4426Sdrh /*
35413c3d36Sdrh ** Check on a Vdbe to make sure it has not been finalized.  Log
36413c3d36Sdrh ** an error and return true if it has been finalized (or is otherwise
37413c3d36Sdrh ** invalid).  Return false if it is ok.
38413c3d36Sdrh */
39413c3d36Sdrh static int vdbeSafety(Vdbe *p){
40413c3d36Sdrh   if( p->db==0 ){
41413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
42413c3d36Sdrh     return 1;
43413c3d36Sdrh   }else{
44413c3d36Sdrh     return 0;
45413c3d36Sdrh   }
46413c3d36Sdrh }
47413c3d36Sdrh static int vdbeSafetyNotNull(Vdbe *p){
48413c3d36Sdrh   if( p==0 ){
49413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
50413c3d36Sdrh     return 1;
51413c3d36Sdrh   }else{
52413c3d36Sdrh     return vdbeSafety(p);
53413c3d36Sdrh   }
54413c3d36Sdrh }
55413c3d36Sdrh 
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 */
61201e0c68Sdrh static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
62201e0c68Sdrh   sqlite3_int64 iNow;
633d2a529dSdrh   sqlite3_int64 iElapse;
64201e0c68Sdrh   assert( p->startTime>0 );
653d2a529dSdrh   assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
66201e0c68Sdrh   assert( db->init.busy==0 );
67201e0c68Sdrh   assert( p->zSql!=0 );
68201e0c68Sdrh   sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
693d2a529dSdrh   iElapse = (iNow - p->startTime)*1000000;
703d2a529dSdrh   if( db->xProfile ){
713d2a529dSdrh     db->xProfile(db->pProfileArg, p->zSql, iElapse);
723d2a529dSdrh   }
733d2a529dSdrh   if( db->mTrace & SQLITE_TRACE_PROFILE ){
74fca760c8Sdrh     db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
753d2a529dSdrh   }
76201e0c68Sdrh   p->startTime = 0;
77201e0c68Sdrh }
78201e0c68Sdrh /*
79201e0c68Sdrh ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
80201e0c68Sdrh ** is needed, and it invokes the callback if it is needed.
81201e0c68Sdrh */
82201e0c68Sdrh # define checkProfileCallback(DB,P) \
83201e0c68Sdrh    if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
84201e0c68Sdrh #else
85201e0c68Sdrh # define checkProfileCallback(DB,P)  /*no-op*/
86201e0c68Sdrh #endif
87201e0c68Sdrh 
88413c3d36Sdrh /*
89e30f4426Sdrh ** The following routine destroys a virtual machine that is created by
90e30f4426Sdrh ** the sqlite3_compile() routine. The integer returned is an SQLITE_
91e30f4426Sdrh ** success/failure code that describes the result of executing the virtual
92e30f4426Sdrh ** machine.
93e30f4426Sdrh **
94e30f4426Sdrh ** This routine sets the error code and string returned by
95e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
96e30f4426Sdrh */
97e30f4426Sdrh int sqlite3_finalize(sqlite3_stmt *pStmt){
98e30f4426Sdrh   int rc;
99e30f4426Sdrh   if( pStmt==0 ){
10065bafa65Sdrh     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
10165bafa65Sdrh     ** pointer is a harmless no-op. */
102e30f4426Sdrh     rc = SQLITE_OK;
103e30f4426Sdrh   }else{
104e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
105238746a6Sdanielk1977     sqlite3 *db = v->db;
106413c3d36Sdrh     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
1074245c405Sdrh     sqlite3_mutex_enter(db->mutex);
108201e0c68Sdrh     checkProfileCallback(db, v);
109e30f4426Sdrh     rc = sqlite3VdbeFinalize(v);
110238746a6Sdanielk1977     rc = sqlite3ApiExit(db, rc);
1114245c405Sdrh     sqlite3LeaveMutexAndCloseZombie(db);
112e30f4426Sdrh   }
113e30f4426Sdrh   return rc;
114e30f4426Sdrh }
115e30f4426Sdrh 
116e30f4426Sdrh /*
117e30f4426Sdrh ** Terminate the current execution of an SQL statement and reset it
118e30f4426Sdrh ** back to its starting state so that it can be reused. A success code from
119e30f4426Sdrh ** the prior execution is returned.
120e30f4426Sdrh **
121e30f4426Sdrh ** This routine sets the error code and string returned by
122e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
123e30f4426Sdrh */
124e30f4426Sdrh int sqlite3_reset(sqlite3_stmt *pStmt){
125e30f4426Sdrh   int rc;
126e30f4426Sdrh   if( pStmt==0 ){
127e30f4426Sdrh     rc = SQLITE_OK;
128e30f4426Sdrh   }else{
129e30f4426Sdrh     Vdbe *v = (Vdbe*)pStmt;
130201e0c68Sdrh     sqlite3 *db = v->db;
131201e0c68Sdrh     sqlite3_mutex_enter(db->mutex);
132201e0c68Sdrh     checkProfileCallback(db, v);
133c890fec3Sdrh     rc = sqlite3VdbeReset(v);
134124c0b49Sdrh     sqlite3VdbeRewind(v);
135201e0c68Sdrh     assert( (rc & (db->errMask))==rc );
136201e0c68Sdrh     rc = sqlite3ApiExit(db, rc);
137201e0c68Sdrh     sqlite3_mutex_leave(db->mutex);
138e30f4426Sdrh   }
139e30f4426Sdrh   return rc;
140e30f4426Sdrh }
141e30f4426Sdrh 
142e30f4426Sdrh /*
143e30f4426Sdrh ** Set all the parameters in the compiled SQL statement to NULL.
144e30f4426Sdrh */
145e30f4426Sdrh int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
146e30f4426Sdrh   int i;
147e30f4426Sdrh   int rc = SQLITE_OK;
1487297d1f0Sdrh   Vdbe *p = (Vdbe*)pStmt;
14918472fa7Sdrh #if SQLITE_THREADSAFE
1507e8b848aSdrh   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
1517e8b848aSdrh #endif
1527e8b848aSdrh   sqlite3_mutex_enter(mutex);
1537297d1f0Sdrh   for(i=0; i<p->nVar; i++){
1547297d1f0Sdrh     sqlite3VdbeMemRelease(&p->aVar[i]);
1557297d1f0Sdrh     p->aVar[i].flags = MEM_Null;
156e30f4426Sdrh   }
157bda4cb87Sdan   assert( p->isPrepareV2 || p->expmask==0 );
158bda4cb87Sdan   if( p->expmask ){
159c94b8595Sdan     p->expired = 1;
160c94b8595Sdan   }
1617e8b848aSdrh   sqlite3_mutex_leave(mutex);
162e30f4426Sdrh   return rc;
163e30f4426Sdrh }
164e30f4426Sdrh 
165e30f4426Sdrh 
1664f26d6c4Sdrh /**************************** sqlite3_value_  *******************************
1674f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value
1684f26d6c4Sdrh ** structure.
1694f26d6c4Sdrh */
1704f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){
1714f26d6c4Sdrh   Mem *p = (Mem*)pVal;
1724f26d6c4Sdrh   if( p->flags & (MEM_Blob|MEM_Str) ){
173ff535a24Sdrh     if( ExpandBlob(p)!=SQLITE_OK ){
174a4d5ae8fSdan       assert( p->flags==MEM_Null && p->z==0 );
175a4d5ae8fSdan       return 0;
176a4d5ae8fSdan     }
1771f0feef8Sdrh     p->flags |= MEM_Blob;
17842262536Sdrh     return p->n ? p->z : 0;
1794f26d6c4Sdrh   }else{
1804f26d6c4Sdrh     return sqlite3_value_text(pVal);
1814f26d6c4Sdrh   }
1824f26d6c4Sdrh }
1834f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){
184b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
1854f26d6c4Sdrh }
1864f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){
187b21c8cd4Sdrh   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
1884f26d6c4Sdrh }
1894f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){
1906a6124e2Sdrh   return sqlite3VdbeRealValue((Mem*)pVal);
1914f26d6c4Sdrh }
1924f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){
193b27b7f5dSdrh   return (int)sqlite3VdbeIntValue((Mem*)pVal);
1944f26d6c4Sdrh }
195efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
1966a6124e2Sdrh   return sqlite3VdbeIntValue((Mem*)pVal);
1974f26d6c4Sdrh }
198bcdf78a6Sdrh unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
1995b6c8e4eSdan   Mem *pMem = (Mem*)pVal;
2005b6c8e4eSdan   return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
201bcdf78a6Sdrh }
2024f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
203b21c8cd4Sdrh   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
2044f26d6c4Sdrh }
2056c62608fSdrh #ifndef SQLITE_OMIT_UTF16
2064f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){
207b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2084f26d6c4Sdrh }
209d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){
210b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
211d8123366Sdanielk1977 }
212d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){
213b21c8cd4Sdrh   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
214d8123366Sdanielk1977 }
2156c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
21622ec1346Sdrh /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
21722ec1346Sdrh ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
21822ec1346Sdrh ** point number string BLOB NULL
21922ec1346Sdrh */
2204f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){
2211b27b8c0Sdrh   static const u8 aType[] = {
2221b27b8c0Sdrh      SQLITE_BLOB,     /* 0x00 */
2231b27b8c0Sdrh      SQLITE_NULL,     /* 0x01 */
2241b27b8c0Sdrh      SQLITE_TEXT,     /* 0x02 */
2251b27b8c0Sdrh      SQLITE_NULL,     /* 0x03 */
2261b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x04 */
2271b27b8c0Sdrh      SQLITE_NULL,     /* 0x05 */
2281b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x06 */
2291b27b8c0Sdrh      SQLITE_NULL,     /* 0x07 */
2301b27b8c0Sdrh      SQLITE_FLOAT,    /* 0x08 */
2311b27b8c0Sdrh      SQLITE_NULL,     /* 0x09 */
2321b27b8c0Sdrh      SQLITE_FLOAT,    /* 0x0a */
2331b27b8c0Sdrh      SQLITE_NULL,     /* 0x0b */
2341b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x0c */
2351b27b8c0Sdrh      SQLITE_NULL,     /* 0x0d */
2361b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x0e */
2371b27b8c0Sdrh      SQLITE_NULL,     /* 0x0f */
2381b27b8c0Sdrh      SQLITE_BLOB,     /* 0x10 */
2391b27b8c0Sdrh      SQLITE_NULL,     /* 0x11 */
2401b27b8c0Sdrh      SQLITE_TEXT,     /* 0x12 */
2411b27b8c0Sdrh      SQLITE_NULL,     /* 0x13 */
2421b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x14 */
2431b27b8c0Sdrh      SQLITE_NULL,     /* 0x15 */
2441b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x16 */
2451b27b8c0Sdrh      SQLITE_NULL,     /* 0x17 */
2461b27b8c0Sdrh      SQLITE_FLOAT,    /* 0x18 */
2471b27b8c0Sdrh      SQLITE_NULL,     /* 0x19 */
2481b27b8c0Sdrh      SQLITE_FLOAT,    /* 0x1a */
2491b27b8c0Sdrh      SQLITE_NULL,     /* 0x1b */
2501b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x1c */
2511b27b8c0Sdrh      SQLITE_NULL,     /* 0x1d */
2521b27b8c0Sdrh      SQLITE_INTEGER,  /* 0x1e */
2531b27b8c0Sdrh      SQLITE_NULL,     /* 0x1f */
2541b27b8c0Sdrh   };
255afc14f72Smistachkin   return aType[pVal->flags&MEM_AffMask];
2564f26d6c4Sdrh }
2574f26d6c4Sdrh 
2584f03f413Sdrh /* Make a copy of an sqlite3_value object
2594f03f413Sdrh */
2604f03f413Sdrh sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
2614f03f413Sdrh   sqlite3_value *pNew;
2624f03f413Sdrh   if( pOrig==0 ) return 0;
2634f03f413Sdrh   pNew = sqlite3_malloc( sizeof(*pNew) );
2644f03f413Sdrh   if( pNew==0 ) return 0;
2654f03f413Sdrh   memset(pNew, 0, sizeof(*pNew));
2664f03f413Sdrh   memcpy(pNew, pOrig, MEMCELLSIZE);
2674f03f413Sdrh   pNew->flags &= ~MEM_Dyn;
2684f03f413Sdrh   pNew->db = 0;
2694f03f413Sdrh   if( pNew->flags&(MEM_Str|MEM_Blob) ){
27010ca5b48Sdrh     pNew->flags &= ~(MEM_Static|MEM_Dyn);
2714f03f413Sdrh     pNew->flags |= MEM_Ephem;
2729dfedc82Sdrh     if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
2739dfedc82Sdrh       sqlite3ValueFree(pNew);
2749dfedc82Sdrh       pNew = 0;
2759dfedc82Sdrh     }
2764f03f413Sdrh   }
2774f03f413Sdrh   return pNew;
2784f03f413Sdrh }
2794f03f413Sdrh 
2804f03f413Sdrh /* Destroy an sqlite3_value object previously obtained from
2814f03f413Sdrh ** sqlite3_value_dup().
2824f03f413Sdrh */
2834f03f413Sdrh void sqlite3_value_free(sqlite3_value *pOld){
2844f03f413Sdrh   sqlite3ValueFree(pOld);
2854f03f413Sdrh }
2864f03f413Sdrh 
2874f03f413Sdrh 
2884f26d6c4Sdrh /**************************** sqlite3_result_  *******************************
2894f26d6c4Sdrh ** The following routines are used by user-defined functions to specify
2904f26d6c4Sdrh ** the function result.
2914c8555fdSdrh **
29260ec914cSpeter.d.reid ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
2934c8555fdSdrh ** result as a string or blob but if the string or blob is too large, it
2944c8555fdSdrh ** then sets the error code to SQLITE_TOOBIG
295da4ca9d1Sdrh **
296da4ca9d1Sdrh ** The invokeValueDestructor(P,X) routine invokes destructor function X()
297da4ca9d1Sdrh ** on value P is not going to be used and need to be destroyed.
2984f26d6c4Sdrh */
2994c8555fdSdrh static void setResultStrOrError(
3004c8555fdSdrh   sqlite3_context *pCtx,  /* Function context */
3014c8555fdSdrh   const char *z,          /* String pointer */
3024c8555fdSdrh   int n,                  /* Bytes in string, or negative */
3034c8555fdSdrh   u8 enc,                 /* Encoding of z.  0 for BLOBs */
3044c8555fdSdrh   void (*xDel)(void*)     /* Destructor function */
3054c8555fdSdrh ){
3069bd038f1Sdrh   if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
3074c8555fdSdrh     sqlite3_result_error_toobig(pCtx);
3084c8555fdSdrh   }
3094c8555fdSdrh }
310da4ca9d1Sdrh static int invokeValueDestructor(
311da4ca9d1Sdrh   const void *p,             /* Value to destroy */
312da4ca9d1Sdrh   void (*xDel)(void*),       /* The destructor */
313da4ca9d1Sdrh   sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
314da4ca9d1Sdrh ){
315fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
316da4ca9d1Sdrh   if( xDel==0 ){
317da4ca9d1Sdrh     /* noop */
318da4ca9d1Sdrh   }else if( xDel==SQLITE_TRANSIENT ){
319da4ca9d1Sdrh     /* noop */
320da4ca9d1Sdrh   }else{
321da4ca9d1Sdrh     xDel((void*)p);
322da4ca9d1Sdrh   }
323da4ca9d1Sdrh   if( pCtx ) sqlite3_result_error_toobig(pCtx);
324da4ca9d1Sdrh   return SQLITE_TOOBIG;
325da4ca9d1Sdrh }
3264f26d6c4Sdrh void sqlite3_result_blob(
3274f26d6c4Sdrh   sqlite3_context *pCtx,
3284f26d6c4Sdrh   const void *z,
3294f26d6c4Sdrh   int n,
330d8123366Sdanielk1977   void (*xDel)(void *)
3314f26d6c4Sdrh ){
3325708d2deSdrh   assert( n>=0 );
3339bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3344c8555fdSdrh   setResultStrOrError(pCtx, z, n, 0, xDel);
3354f26d6c4Sdrh }
336da4ca9d1Sdrh void sqlite3_result_blob64(
337da4ca9d1Sdrh   sqlite3_context *pCtx,
338da4ca9d1Sdrh   const void *z,
339da4ca9d1Sdrh   sqlite3_uint64 n,
340da4ca9d1Sdrh   void (*xDel)(void *)
341da4ca9d1Sdrh ){
342da4ca9d1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
343fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
344da4ca9d1Sdrh   if( n>0x7fffffff ){
345da4ca9d1Sdrh     (void)invokeValueDestructor(z, xDel, pCtx);
346da4ca9d1Sdrh   }else{
347da4ca9d1Sdrh     setResultStrOrError(pCtx, z, (int)n, 0, xDel);
348da4ca9d1Sdrh   }
349da4ca9d1Sdrh }
3504f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
3519bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3529bd038f1Sdrh   sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
3534f26d6c4Sdrh }
3544f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
3559bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
35669544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
3579b47ee3fSdrh   pCtx->fErrorOrAux = 1;
3589bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
3594f26d6c4Sdrh }
360a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16
3614f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
3629bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
36369544ec9Sdrh   pCtx->isError = SQLITE_ERROR;
3649b47ee3fSdrh   pCtx->fErrorOrAux = 1;
3659bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
3664f26d6c4Sdrh }
367a1686c9aSdanielk1977 #endif
368f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
3699bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3709bd038f1Sdrh   sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
3714f26d6c4Sdrh }
3724f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
3739bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3749bd038f1Sdrh   sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
3754f26d6c4Sdrh }
3764f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){
3779bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3789bd038f1Sdrh   sqlite3VdbeMemSetNull(pCtx->pOut);
3794f26d6c4Sdrh }
380bcdf78a6Sdrh void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
3815b6c8e4eSdan   Mem *pOut = pCtx->pOut;
3825b6c8e4eSdan   assert( sqlite3_mutex_held(pOut->db->mutex) );
3835b6c8e4eSdan   pOut->eSubtype = eSubtype & 0xff;
3845b6c8e4eSdan   pOut->flags |= MEM_Subtype;
385bcdf78a6Sdrh }
3864f26d6c4Sdrh void sqlite3_result_text(
3874f26d6c4Sdrh   sqlite3_context *pCtx,
3884f26d6c4Sdrh   const char *z,
3894f26d6c4Sdrh   int n,
390d8123366Sdanielk1977   void (*xDel)(void *)
3914f26d6c4Sdrh ){
3929bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
3934c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
3944f26d6c4Sdrh }
395bbf483f8Sdrh void sqlite3_result_text64(
396da4ca9d1Sdrh   sqlite3_context *pCtx,
397da4ca9d1Sdrh   const char *z,
398da4ca9d1Sdrh   sqlite3_uint64 n,
399da4ca9d1Sdrh   void (*xDel)(void *),
400da4ca9d1Sdrh   unsigned char enc
401da4ca9d1Sdrh ){
402da4ca9d1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
403fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
40479f7af9aSdrh   if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
405da4ca9d1Sdrh   if( n>0x7fffffff ){
406da4ca9d1Sdrh     (void)invokeValueDestructor(z, xDel, pCtx);
407da4ca9d1Sdrh   }else{
408da4ca9d1Sdrh     setResultStrOrError(pCtx, z, (int)n, enc, xDel);
409da4ca9d1Sdrh   }
410da4ca9d1Sdrh }
4116c62608fSdrh #ifndef SQLITE_OMIT_UTF16
4124f26d6c4Sdrh void sqlite3_result_text16(
4134f26d6c4Sdrh   sqlite3_context *pCtx,
4144f26d6c4Sdrh   const void *z,
4154f26d6c4Sdrh   int n,
416d8123366Sdanielk1977   void (*xDel)(void *)
4174f26d6c4Sdrh ){
4189bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4194c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
420d8123366Sdanielk1977 }
421d8123366Sdanielk1977 void sqlite3_result_text16be(
422d8123366Sdanielk1977   sqlite3_context *pCtx,
423d8123366Sdanielk1977   const void *z,
424d8123366Sdanielk1977   int n,
425d8123366Sdanielk1977   void (*xDel)(void *)
426d8123366Sdanielk1977 ){
4279bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4284c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
429d8123366Sdanielk1977 }
430d8123366Sdanielk1977 void sqlite3_result_text16le(
431d8123366Sdanielk1977   sqlite3_context *pCtx,
432d8123366Sdanielk1977   const void *z,
433d8123366Sdanielk1977   int n,
434d8123366Sdanielk1977   void (*xDel)(void *)
435d8123366Sdanielk1977 ){
4369bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4374c8555fdSdrh   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
4384f26d6c4Sdrh }
4396c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
4404f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
4419bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4429bd038f1Sdrh   sqlite3VdbeMemCopy(pCtx->pOut, pValue);
4434f26d6c4Sdrh }
444b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
4459bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4469bd038f1Sdrh   sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
447b026e05eSdrh }
448a4d5ae8fSdan int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
449a4d5ae8fSdan   Mem *pOut = pCtx->pOut;
450a4d5ae8fSdan   assert( sqlite3_mutex_held(pOut->db->mutex) );
4516bacdc21Sdrh   if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
452a4d5ae8fSdan     return SQLITE_TOOBIG;
453a4d5ae8fSdan   }
454a4d5ae8fSdan   sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
455a4d5ae8fSdan   return SQLITE_OK;
456a4d5ae8fSdan }
45769544ec9Sdrh void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
45869544ec9Sdrh   pCtx->isError = errCode;
4599b47ee3fSdrh   pCtx->fErrorOrAux = 1;
460983b5ee7Sdrh #ifdef SQLITE_DEBUG
46196f4ad20Sdrh   if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
462983b5ee7Sdrh #endif
4639bd038f1Sdrh   if( pCtx->pOut->flags & MEM_Null ){
4649bd038f1Sdrh     sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
46551c7d864Sdrh                          SQLITE_UTF8, SQLITE_STATIC);
46651c7d864Sdrh   }
46769544ec9Sdrh }
4684f26d6c4Sdrh 
469a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */
470a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){
4719bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
472bb4957f8Sdrh   pCtx->isError = SQLITE_TOOBIG;
4739b47ee3fSdrh   pCtx->fErrorOrAux = 1;
4749bd038f1Sdrh   sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
475bb4957f8Sdrh                        SQLITE_UTF8, SQLITE_STATIC);
476a0206bc8Sdrh }
477a0206bc8Sdrh 
478a1644fd8Sdanielk1977 /* An SQLITE_NOMEM error. */
479a1644fd8Sdanielk1977 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
4809bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
4819bd038f1Sdrh   sqlite3VdbeMemSetNull(pCtx->pOut);
482fad3039cSmistachkin   pCtx->isError = SQLITE_NOMEM_BKPT;
4839b47ee3fSdrh   pCtx->fErrorOrAux = 1;
4844a642b60Sdrh   sqlite3OomFault(pCtx->pOut->db);
485a1644fd8Sdanielk1977 }
4864f26d6c4Sdrh 
4875cf53537Sdan /*
4885cf53537Sdan ** This function is called after a transaction has been committed. It
4895cf53537Sdan ** invokes callbacks registered with sqlite3_wal_hook() as required.
4905cf53537Sdan */
4917ed91f23Sdrh static int doWalCallbacks(sqlite3 *db){
4928d22a174Sdan   int rc = SQLITE_OK;
4935cf53537Sdan #ifndef SQLITE_OMIT_WAL
4945cf53537Sdan   int i;
4958d22a174Sdan   for(i=0; i<db->nDb; i++){
4968d22a174Sdan     Btree *pBt = db->aDb[i].pBt;
4978d22a174Sdan     if( pBt ){
498ef15c6e9Sdrh       int nEntry;
499ef15c6e9Sdrh       sqlite3BtreeEnter(pBt);
500ef15c6e9Sdrh       nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
501ef15c6e9Sdrh       sqlite3BtreeLeave(pBt);
5025def0843Sdrh       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
50369c33826Sdrh         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
5048d22a174Sdan       }
5058d22a174Sdan     }
5068d22a174Sdan   }
5075cf53537Sdan #endif
5088d22a174Sdan   return rc;
5098d22a174Sdan }
5108d22a174Sdan 
511201e0c68Sdrh 
5124f26d6c4Sdrh /*
5134f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the
5144f26d6c4Sdrh ** statement is completely executed or an error occurs.
515b900aaf3Sdrh **
516b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step()
517b900aaf3Sdrh ** API.  The only thing omitted is the automatic recompile if a
518b900aaf3Sdrh ** schema change has occurred.  That detail is handled by the
519b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure.
5204f26d6c4Sdrh */
521b900aaf3Sdrh static int sqlite3Step(Vdbe *p){
5229bb575fdSdrh   sqlite3 *db;
5234f26d6c4Sdrh   int rc;
5244f26d6c4Sdrh 
525a185ddbfSdanielk1977   assert(p);
526a185ddbfSdanielk1977   if( p->magic!=VDBE_MAGIC_RUN ){
5273674bfd1Sdrh     /* We used to require that sqlite3_reset() be called before retrying
528602acb48Sdrh     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
529602acb48Sdrh     ** with version 3.7.0, we changed this so that sqlite3_reset() would
530602acb48Sdrh     ** be called automatically instead of throwing the SQLITE_MISUSE error.
531602acb48Sdrh     ** This "automatic-reset" change is not technically an incompatibility,
532602acb48Sdrh     ** since any application that receives an SQLITE_MISUSE is broken by
533602acb48Sdrh     ** definition.
534602acb48Sdrh     **
535602acb48Sdrh     ** Nevertheless, some published applications that were originally written
536602acb48Sdrh     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
537b8a45bbdSdrh     ** returns, and those were broken by the automatic-reset change.  As a
538602acb48Sdrh     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
539602acb48Sdrh     ** legacy behavior of returning SQLITE_MISUSE for cases where the
540602acb48Sdrh     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
541602acb48Sdrh     ** or SQLITE_BUSY error.
5423674bfd1Sdrh     */
543602acb48Sdrh #ifdef SQLITE_OMIT_AUTORESET
544983b5ee7Sdrh     if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
5453674bfd1Sdrh       sqlite3_reset((sqlite3_stmt*)p);
546602acb48Sdrh     }else{
547602acb48Sdrh       return SQLITE_MISUSE_BKPT;
548602acb48Sdrh     }
549602acb48Sdrh #else
550602acb48Sdrh     sqlite3_reset((sqlite3_stmt*)p);
551602acb48Sdrh #endif
552f1bfe9a8Sdrh   }
553f1bfe9a8Sdrh 
55414d4cc43Sdan   /* Check that malloc() has not failed. If it has, return early. */
55517435752Sdrh   db = p->db;
556c456e57aSdrh   if( db->mallocFailed ){
55714d4cc43Sdan     p->rc = SQLITE_NOMEM;
558fad3039cSmistachkin     return SQLITE_NOMEM_BKPT;
559c456e57aSdrh   }
560261919ccSdanielk1977 
561a21c6b6fSdanielk1977   if( p->pc<=0 && p->expired ){
562a21c6b6fSdanielk1977     p->rc = SQLITE_SCHEMA;
563b900aaf3Sdrh     rc = SQLITE_ERROR;
564b900aaf3Sdrh     goto end_of_step;
565a21c6b6fSdanielk1977   }
5661d850a72Sdanielk1977   if( p->pc<0 ){
56715ca1df1Sdrh     /* If there are no other statements currently running, then
56815ca1df1Sdrh     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
56915ca1df1Sdrh     ** from interrupting a statement that has not yet started.
57015ca1df1Sdrh     */
5714f7d3a5fSdrh     if( db->nVdbeActive==0 ){
57215ca1df1Sdrh       db->u1.isInterrupted = 0;
57315ca1df1Sdrh     }
57415ca1df1Sdrh 
575888e16e7Sdrh     assert( db->nVdbeWrite>0 || db->autoCommit==0
576cb3e4b79Sdan         || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
577cb3e4b79Sdan     );
5781da40a38Sdan 
57919e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
5803d2a529dSdrh     if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
5813d2a529dSdrh         && !db->init.busy && p->zSql ){
582b7e8ea20Sdrh       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
583201e0c68Sdrh     }else{
584201e0c68Sdrh       assert( p->startTime==0 );
58519e2d37fSdrh     }
58619e2d37fSdrh #endif
587c16a03b5Sdrh 
5884f7d3a5fSdrh     db->nVdbeActive++;
5894f7d3a5fSdrh     if( p->readOnly==0 ) db->nVdbeWrite++;
5901713afb0Sdrh     if( p->bIsReader ) db->nVdbeRead++;
5911d850a72Sdanielk1977     p->pc = 0;
5921d850a72Sdanielk1977   }
593983b5ee7Sdrh #ifdef SQLITE_DEBUG
594983b5ee7Sdrh   p->rcApp = SQLITE_OK;
595983b5ee7Sdrh #endif
596b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN
5974f26d6c4Sdrh   if( p->explain ){
5984f26d6c4Sdrh     rc = sqlite3VdbeList(p);
599b7f9164eSdrh   }else
600b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */
601b7f9164eSdrh   {
6024f7d3a5fSdrh     db->nVdbeExec++;
6034f26d6c4Sdrh     rc = sqlite3VdbeExec(p);
6044f7d3a5fSdrh     db->nVdbeExec--;
6054f26d6c4Sdrh   }
6064f26d6c4Sdrh 
60719e2d37fSdrh #ifndef SQLITE_OMIT_TRACE
608201e0c68Sdrh   /* If the statement completed successfully, invoke the profile callback */
609201e0c68Sdrh   if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
61019e2d37fSdrh #endif
61119e2d37fSdrh 
6128d22a174Sdan   if( rc==SQLITE_DONE ){
6138d22a174Sdan     assert( p->rc==SQLITE_OK );
6147ed91f23Sdrh     p->rc = doWalCallbacks(db);
6158d22a174Sdan     if( p->rc!=SQLITE_OK ){
6168d22a174Sdan       rc = SQLITE_ERROR;
6178d22a174Sdan     }
6188d22a174Sdan   }
6198d22a174Sdan 
62080cc85b3Sdrh   db->errCode = rc;
621357864ecSdanielk1977   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
622fad3039cSmistachkin     p->rc = SQLITE_NOMEM_BKPT;
6234f26d6c4Sdrh   }
624357864ecSdanielk1977 end_of_step:
625357864ecSdanielk1977   /* At this point local variable rc holds the value that should be
626357864ecSdanielk1977   ** returned if this statement was compiled using the legacy
627357864ecSdanielk1977   ** sqlite3_prepare() interface. According to the docs, this can only
628357864ecSdanielk1977   ** be one of the values in the first assert() below. Variable p->rc
629357864ecSdanielk1977   ** contains the value that would be returned if sqlite3_finalize()
630357864ecSdanielk1977   ** were called on statement p.
631357864ecSdanielk1977   */
632357864ecSdanielk1977   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
633cbd8db35Sdrh        || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
634357864ecSdanielk1977   );
635983b5ee7Sdrh   assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
636357864ecSdanielk1977   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
637357864ecSdanielk1977     /* If this statement was prepared using sqlite3_prepare_v2(), and an
63848864df9Smistachkin     ** error has occurred, then return the error code in p->rc to the
639357864ecSdanielk1977     ** caller. Set the error code in the database handle to the same value.
640357864ecSdanielk1977     */
641029ead64Sdan     rc = sqlite3VdbeTransferError(p);
642357864ecSdanielk1977   }
643357864ecSdanielk1977   return (rc&db->errMask);
644b900aaf3Sdrh }
645b900aaf3Sdrh 
646b900aaf3Sdrh /*
647b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step().  Call
648b900aaf3Sdrh ** sqlite3Step() to do most of the work.  If a schema error occurs,
649b900aaf3Sdrh ** call sqlite3Reprepare() and try again.
650b900aaf3Sdrh */
651b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){
652a6129fa7Sdrh   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
653a6129fa7Sdrh   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
654a6129fa7Sdrh   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
655a6129fa7Sdrh   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
656a6129fa7Sdrh   sqlite3 *db;             /* The database connection */
657a6129fa7Sdrh 
658413c3d36Sdrh   if( vdbeSafetyNotNull(v) ){
659413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
660413c3d36Sdrh   }
661413c3d36Sdrh   db = v->db;
6628e556520Sdanielk1977   sqlite3_mutex_enter(db->mutex);
66337f58e99Sdrh   v->doingRerun = 0;
664b900aaf3Sdrh   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
6652c7946a4Sdrh          && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
6662c7946a4Sdrh     int savedPc = v->pc;
6672c7946a4Sdrh     rc2 = rc = sqlite3Reprepare(v);
6682c7946a4Sdrh     if( rc!=SQLITE_OK) break;
669b900aaf3Sdrh     sqlite3_reset(pStmt);
6705f58ae75Sdrh     if( savedPc>=0 ) v->doingRerun = 1;
67108f5f4c5Sdan     assert( v->expired==0 );
672b900aaf3Sdrh   }
673a3cc007dSdrh   if( rc2!=SQLITE_OK ){
6748e556520Sdanielk1977     /* This case occurs after failing to recompile an sql statement.
6758e556520Sdanielk1977     ** The error message from the SQL compiler has already been loaded
6768e556520Sdanielk1977     ** into the database handle. This block copies the error message
6778e556520Sdanielk1977     ** from the database handle into the statement and sets the statement
6788e556520Sdanielk1977     ** program counter to 0 to ensure that when the statement is
6798e556520Sdanielk1977     ** finalized or reset the parser error message is available via
6808e556520Sdanielk1977     ** sqlite3_errmsg() and sqlite3_errcode().
6818e556520Sdanielk1977     */
6828e556520Sdanielk1977     const char *zErr = (const char *)sqlite3_value_text(db->pErr);
683633e6d57Sdrh     sqlite3DbFree(db, v->zErrMsg);
6848e556520Sdanielk1977     if( !db->mallocFailed ){
6858e556520Sdanielk1977       v->zErrMsg = sqlite3DbStrDup(db, zErr);
686a6129fa7Sdrh       v->rc = rc2;
6878e556520Sdanielk1977     } else {
6888e556520Sdanielk1977       v->zErrMsg = 0;
689fad3039cSmistachkin       v->rc = rc = SQLITE_NOMEM_BKPT;
6908e556520Sdanielk1977     }
6918e556520Sdanielk1977   }
6928e556520Sdanielk1977   rc = sqlite3ApiExit(db, rc);
6938e556520Sdanielk1977   sqlite3_mutex_leave(db->mutex);
694b900aaf3Sdrh   return rc;
695b900aaf3Sdrh }
6964f26d6c4Sdrh 
69795a7b3e3Sdrh 
6984f26d6c4Sdrh /*
699eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a
700eb2e176aSdrh ** pointer to it.
701eb2e176aSdrh */
702eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){
703eb2e176aSdrh   assert( p && p->pFunc );
704eb2e176aSdrh   return p->pFunc->pUserData;
705eb2e176aSdrh }
706eb2e176aSdrh 
707eb2e176aSdrh /*
708fa4a4b91Sdrh ** Extract the user data from a sqlite3_context structure and return a
709fa4a4b91Sdrh ** pointer to it.
7109f129f46Sdrh **
7119f129f46Sdrh ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
7129f129f46Sdrh ** returns a copy of the pointer to the database connection (the 1st
7139f129f46Sdrh ** parameter) of the sqlite3_create_function() and
7149f129f46Sdrh ** sqlite3_create_function16() routines that originally registered the
7159f129f46Sdrh ** application defined function.
716fa4a4b91Sdrh */
717fa4a4b91Sdrh sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
718a46a4a63Sdrh   assert( p && p->pOut );
7199bd038f1Sdrh   return p->pOut->db;
720fa4a4b91Sdrh }
721fa4a4b91Sdrh 
722fa4a4b91Sdrh /*
723a9e03b1bSdrh ** Return the current time for a statement.  If the current time
724a9e03b1bSdrh ** is requested more than once within the same run of a single prepared
725a9e03b1bSdrh ** statement, the exact same time is returned for each invocation regardless
726a9e03b1bSdrh ** of the amount of time that elapses between invocations.  In other words,
727a9e03b1bSdrh ** the time returned is always the time of the first call.
72895a7b3e3Sdrh */
72995a7b3e3Sdrh sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
73095a7b3e3Sdrh   int rc;
731a9e03b1bSdrh #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
7323df79a9aSdrh   sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
733a9e03b1bSdrh   assert( p->pVdbe!=0 );
734a9e03b1bSdrh #else
7353df79a9aSdrh   sqlite3_int64 iTime = 0;
736a9e03b1bSdrh   sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
737a9e03b1bSdrh #endif
7383df79a9aSdrh   if( *piTime==0 ){
739a9e03b1bSdrh     rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
740a9e03b1bSdrh     if( rc ) *piTime = 0;
74195a7b3e3Sdrh   }
742a9e03b1bSdrh   return *piTime;
74395a7b3e3Sdrh }
74495a7b3e3Sdrh 
74595a7b3e3Sdrh /*
746b7481e70Sdrh ** The following is the implementation of an SQL function that always
747b7481e70Sdrh ** fails with an error message stating that the function is used in the
748b7481e70Sdrh ** wrong context.  The sqlite3_overload_function() API might construct
749b7481e70Sdrh ** SQL function that use this routine so that the functions will exist
750b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction
751b7481e70Sdrh ** method of virtual tables.
752b7481e70Sdrh */
753b7481e70Sdrh void sqlite3InvalidFunction(
754b7481e70Sdrh   sqlite3_context *context,  /* The function calling context */
75562c14b34Sdanielk1977   int NotUsed,               /* Number of arguments to the function */
75662c14b34Sdanielk1977   sqlite3_value **NotUsed2   /* Value of each argument */
757b7481e70Sdrh ){
758b7481e70Sdrh   const char *zName = context->pFunc->zName;
759b7481e70Sdrh   char *zErr;
76062c14b34Sdanielk1977   UNUSED_PARAMETER2(NotUsed, NotUsed2);
761bc6160b0Sdrh   zErr = sqlite3_mprintf(
762b7481e70Sdrh       "unable to use function %s in the requested context", zName);
763b7481e70Sdrh   sqlite3_result_error(context, zErr, -1);
7641e536953Sdanielk1977   sqlite3_free(zErr);
765b7481e70Sdrh }
766b7481e70Sdrh 
767b7481e70Sdrh /*
7689de4a171Sdrh ** Create a new aggregate context for p and return a pointer to
7699de4a171Sdrh ** its pMem->z element.
770eb2e176aSdrh */
7719de4a171Sdrh static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
7729de4a171Sdrh   Mem *pMem = p->pMem;
7739de4a171Sdrh   assert( (pMem->flags & MEM_Agg)==0 );
774d68eee04Sdrh   if( nByte<=0 ){
7750725cabeSdrh     sqlite3VdbeMemSetNull(pMem);
776a10a34b8Sdrh     pMem->z = 0;
777a10a34b8Sdrh   }else{
778322f2852Sdrh     sqlite3VdbeMemClearAndResize(pMem, nByte);
779abfcea25Sdrh     pMem->flags = MEM_Agg;
7803c024d69Sdrh     pMem->u.pDef = p->pFunc;
7815f096135Sdanielk1977     if( pMem->z ){
7825f096135Sdanielk1977       memset(pMem->z, 0, nByte);
7835f096135Sdanielk1977     }
784eb2e176aSdrh   }
785abfcea25Sdrh   return (void*)pMem->z;
786eb2e176aSdrh }
787eb2e176aSdrh 
788eb2e176aSdrh /*
7899de4a171Sdrh ** Allocate or return the aggregate context for a user function.  A new
7909de4a171Sdrh ** context is allocated on the first call.  Subsequent calls return the
7919de4a171Sdrh ** same context that was returned on prior calls.
7929de4a171Sdrh */
7939de4a171Sdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
7942d80151fSdrh   assert( p && p->pFunc && p->pFunc->xFinalize );
7959bd038f1Sdrh   assert( sqlite3_mutex_held(p->pOut->db->mutex) );
7969de4a171Sdrh   testcase( nByte<0 );
7979de4a171Sdrh   if( (p->pMem->flags & MEM_Agg)==0 ){
7989de4a171Sdrh     return createAggContext(p, nByte);
7999de4a171Sdrh   }else{
8009de4a171Sdrh     return (void*)p->pMem->z;
8019de4a171Sdrh   }
8029de4a171Sdrh }
8039de4a171Sdrh 
8049de4a171Sdrh /*
80560ec914cSpeter.d.reid ** Return the auxiliary data pointer, if any, for the iArg'th argument to
806682f68b0Sdanielk1977 ** the user-function defined by pCtx.
807*f7fa4e71Sdrh **
808*f7fa4e71Sdrh ** The left-most argument is 0.
809*f7fa4e71Sdrh **
810*f7fa4e71Sdrh ** Undocumented behavior:  If iArg is negative then access a cache of
811*f7fa4e71Sdrh ** auxiliary data pointers that is available to all functions within a
812*f7fa4e71Sdrh ** single prepared statement.  The iArg values must match.
813682f68b0Sdanielk1977 */
814682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
8150c547799Sdan   AuxData *pAuxData;
816b21c8cd4Sdrh 
8179bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
818a9e03b1bSdrh #if SQLITE_ENABLE_STAT3_OR_STAT4
81918bf8076Sdan   if( pCtx->pVdbe==0 ) return 0;
820a9e03b1bSdrh #else
821a9e03b1bSdrh   assert( pCtx->pVdbe!=0 );
822a9e03b1bSdrh #endif
823e6941397Sdrh   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
824*f7fa4e71Sdrh     if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
825e6941397Sdrh       return pAuxData->pAux;
826682f68b0Sdanielk1977     }
827e6941397Sdrh   }
828e6941397Sdrh   return 0;
829682f68b0Sdanielk1977 }
830682f68b0Sdanielk1977 
831682f68b0Sdanielk1977 /*
83260ec914cSpeter.d.reid ** Set the auxiliary data pointer and delete function, for the iArg'th
833682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is
834682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set.
835*f7fa4e71Sdrh **
836*f7fa4e71Sdrh ** The left-most argument is 0.
837*f7fa4e71Sdrh **
838*f7fa4e71Sdrh ** Undocumented behavior:  If iArg is negative then make the data available
839*f7fa4e71Sdrh ** to all functions within the current prepared statement using iArg as an
840*f7fa4e71Sdrh ** access code.
841682f68b0Sdanielk1977 */
842682f68b0Sdanielk1977 void sqlite3_set_auxdata(
843682f68b0Sdanielk1977   sqlite3_context *pCtx,
844682f68b0Sdanielk1977   int iArg,
845682f68b0Sdanielk1977   void *pAux,
846682f68b0Sdanielk1977   void (*xDelete)(void*)
847682f68b0Sdanielk1977 ){
8480c547799Sdan   AuxData *pAuxData;
8490c547799Sdan   Vdbe *pVdbe = pCtx->pVdbe;
850682f68b0Sdanielk1977 
8519bd038f1Sdrh   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
852a9e03b1bSdrh #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
85318bf8076Sdan   if( pVdbe==0 ) goto failed;
854a9e03b1bSdrh #else
855a9e03b1bSdrh   assert( pVdbe!=0 );
856a9e03b1bSdrh #endif
857682f68b0Sdanielk1977 
858e6941397Sdrh   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
859*f7fa4e71Sdrh     if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
860*f7fa4e71Sdrh       break;
861*f7fa4e71Sdrh     }
8620c547799Sdan   }
8630c547799Sdan   if( pAuxData==0 ){
8640c547799Sdan     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
8650c547799Sdan     if( !pAuxData ) goto failed;
866e6941397Sdrh     pAuxData->iAuxOp = pCtx->iOp;
867e6941397Sdrh     pAuxData->iAuxArg = iArg;
868e6941397Sdrh     pAuxData->pNextAux = pVdbe->pAuxData;
8690c547799Sdan     pVdbe->pAuxData = pAuxData;
8709b47ee3fSdrh     if( pCtx->fErrorOrAux==0 ){
8719b47ee3fSdrh       pCtx->isError = 0;
8729b47ee3fSdrh       pCtx->fErrorOrAux = 1;
8739b47ee3fSdrh     }
874e6941397Sdrh   }else if( pAuxData->xDeleteAux ){
875e6941397Sdrh     pAuxData->xDeleteAux(pAuxData->pAux);
876682f68b0Sdanielk1977   }
8770c547799Sdan 
878682f68b0Sdanielk1977   pAuxData->pAux = pAux;
879e6941397Sdrh   pAuxData->xDeleteAux = xDelete;
880e0fc5261Sdanielk1977   return;
881e0fc5261Sdanielk1977 
882e0fc5261Sdanielk1977 failed:
883e0fc5261Sdanielk1977   if( xDelete ){
884e0fc5261Sdanielk1977     xDelete(pAux);
885e0fc5261Sdanielk1977   }
886682f68b0Sdanielk1977 }
887682f68b0Sdanielk1977 
888eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
889682f68b0Sdanielk1977 /*
89060ec914cSpeter.d.reid ** Return the number of times the Step function of an aggregate has been
891eb2e176aSdrh ** called.
892eb2e176aSdrh **
893cf85a51cSdrh ** This function is deprecated.  Do not use it for new code.  It is
894cf85a51cSdrh ** provide only to avoid breaking legacy code.  New aggregate function
895cf85a51cSdrh ** implementations should keep their own counts within their aggregate
896cf85a51cSdrh ** context.
897eb2e176aSdrh */
898eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){
8992d80151fSdrh   assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
900abfcea25Sdrh   return p->pMem->n;
901eb2e176aSdrh }
902eec556d3Sshane #endif
903eb2e176aSdrh 
904eb2e176aSdrh /*
9054f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt.
9064f26d6c4Sdrh */
9074f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){
9084f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
9091bcdb0c0Sdrh   return pVm ? pVm->nResColumn : 0;
9104f26d6c4Sdrh }
9114f26d6c4Sdrh 
9124f26d6c4Sdrh /*
9134f26d6c4Sdrh ** Return the number of values available from the current row of the
9144f26d6c4Sdrh ** currently executing statement pStmt.
9154f26d6c4Sdrh */
9164f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){
9174f26d6c4Sdrh   Vdbe *pVm = (Vdbe *)pStmt;
918d4e70ebdSdrh   if( pVm==0 || pVm->pResultSet==0 ) return 0;
9194f26d6c4Sdrh   return pVm->nResColumn;
9204f26d6c4Sdrh }
9214f26d6c4Sdrh 
92246c47d46Sdan /*
92346c47d46Sdan ** Return a pointer to static memory containing an SQL NULL value.
92446c47d46Sdan */
92546c47d46Sdan static const Mem *columnNullValue(void){
92646c47d46Sdan   /* Even though the Mem structure contains an element
927b8a45bbdSdrh   ** of type i64, on certain architectures (x86) with certain compiler
92846c47d46Sdan   ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
92946c47d46Sdan   ** instead of an 8-byte one. This all works fine, except that when
93046c47d46Sdan   ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
93146c47d46Sdan   ** that a Mem structure is located on an 8-byte boundary. To prevent
932b8a45bbdSdrh   ** these assert()s from failing, when building with SQLITE_DEBUG defined
933b8a45bbdSdrh   ** using gcc, we force nullMem to be 8-byte aligned using the magical
93446c47d46Sdan   ** __attribute__((aligned(8))) macro.  */
93546c47d46Sdan   static const Mem nullMem
93646c47d46Sdan #if defined(SQLITE_DEBUG) && defined(__GNUC__)
93746c47d46Sdan     __attribute__((aligned(8)))
93846c47d46Sdan #endif
939035e563bSdrh     = {
9403329a63aSdrh         /* .u          = */ {0},
9418faee877Sdrh         /* .flags      = */ (u16)MEM_Null,
9428faee877Sdrh         /* .enc        = */ (u8)0,
9438faee877Sdrh         /* .eSubtype   = */ (u8)0,
9448faee877Sdrh         /* .n          = */ (int)0,
9458faee877Sdrh         /* .z          = */ (char*)0,
9468faee877Sdrh         /* .zMalloc    = */ (char*)0,
9478faee877Sdrh         /* .szMalloc   = */ (int)0,
9488faee877Sdrh         /* .uTemp      = */ (u32)0,
9498faee877Sdrh         /* .db         = */ (sqlite3*)0,
9508faee877Sdrh         /* .xDel       = */ (void(*)(void*))0,
951fcd71b60Sdrh #ifdef SQLITE_DEBUG
9528faee877Sdrh         /* .pScopyFrom = */ (Mem*)0,
9538faee877Sdrh         /* .pFiller    = */ (void*)0,
954fcd71b60Sdrh #endif
955035e563bSdrh       };
95646c47d46Sdan   return &nullMem;
95746c47d46Sdan }
9584f26d6c4Sdrh 
9594f26d6c4Sdrh /*
9604f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid.  If
9614f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column.
9624f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value
9634f26d6c4Sdrh ** of NULL.
9644f26d6c4Sdrh */
9654f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){
96632bc3f6eSdrh   Vdbe *pVm;
96732bc3f6eSdrh   Mem *pOut;
96832bc3f6eSdrh 
96932bc3f6eSdrh   pVm = (Vdbe *)pStmt;
97028f17017Sdrh   if( pVm==0 ) return (Mem*)columnNullValue();
97128f17017Sdrh   assert( pVm->db );
97232bc3f6eSdrh   sqlite3_mutex_enter(pVm->db->mutex);
97328f17017Sdrh   if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
974d4e70ebdSdrh     pOut = &pVm->pResultSet[i];
97532bc3f6eSdrh   }else{
97613f40da3Sdrh     sqlite3Error(pVm->db, SQLITE_RANGE);
97746c47d46Sdan     pOut = (Mem*)columnNullValue();
9784f26d6c4Sdrh   }
97932bc3f6eSdrh   return pOut;
9804f26d6c4Sdrh }
9814f26d6c4Sdrh 
9822e588c75Sdanielk1977 /*
9832e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a
9842e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the
9852e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If
9862e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
9872e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM.
9882e588c75Sdanielk1977 **
98932bc3f6eSdrh ** Specifically, this is called from within:
9902e588c75Sdanielk1977 **
9912e588c75Sdanielk1977 **     sqlite3_column_int()
9922e588c75Sdanielk1977 **     sqlite3_column_int64()
9932e588c75Sdanielk1977 **     sqlite3_column_text()
9942e588c75Sdanielk1977 **     sqlite3_column_text16()
9952e588c75Sdanielk1977 **     sqlite3_column_real()
9962e588c75Sdanielk1977 **     sqlite3_column_bytes()
9972e588c75Sdanielk1977 **     sqlite3_column_bytes16()
99842262536Sdrh **     sqiite3_column_blob()
9992e588c75Sdanielk1977 */
10002e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt)
10012e588c75Sdanielk1977 {
10022e588c75Sdanielk1977   /* If malloc() failed during an encoding conversion within an
10032e588c75Sdanielk1977   ** sqlite3_column_XXX API, then set the return code of the statement to
10042e588c75Sdanielk1977   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
10052e588c75Sdanielk1977   ** and _finalize() will return NOMEM.
10062e588c75Sdanielk1977   */
100754f0198eSdanielk1977   Vdbe *p = (Vdbe *)pStmt;
100832bc3f6eSdrh   if( p ){
100928f17017Sdrh     assert( p->db!=0 );
101028f17017Sdrh     assert( sqlite3_mutex_held(p->db->mutex) );
101132bc3f6eSdrh     p->rc = sqlite3ApiExit(p->db, p->rc);
101232bc3f6eSdrh     sqlite3_mutex_leave(p->db->mutex);
101332bc3f6eSdrh   }
10142e588c75Sdanielk1977 }
10152e588c75Sdanielk1977 
10164f26d6c4Sdrh /**************************** sqlite3_column_  *******************************
10174f26d6c4Sdrh ** The following routines are used to access elements of the current row
10184f26d6c4Sdrh ** in the result set.
10194f26d6c4Sdrh */
1020c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
10212e588c75Sdanielk1977   const void *val;
10222e588c75Sdanielk1977   val = sqlite3_value_blob( columnMem(pStmt,i) );
1023c9cf901dSdanielk1977   /* Even though there is no encoding conversion, value_blob() might
1024c9cf901dSdanielk1977   ** need to call malloc() to expand the result of a zeroblob()
1025c9cf901dSdanielk1977   ** expression.
1026c9cf901dSdanielk1977   */
1027c9cf901dSdanielk1977   columnMallocFailure(pStmt);
10282e588c75Sdanielk1977   return val;
1029c572ef7fSdanielk1977 }
10304f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
10312e588c75Sdanielk1977   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
10322e588c75Sdanielk1977   columnMallocFailure(pStmt);
10332e588c75Sdanielk1977   return val;
10344f26d6c4Sdrh }
10354f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
10362e588c75Sdanielk1977   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
10372e588c75Sdanielk1977   columnMallocFailure(pStmt);
10382e588c75Sdanielk1977   return val;
10394f26d6c4Sdrh }
10404f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
10412e588c75Sdanielk1977   double val = sqlite3_value_double( columnMem(pStmt,i) );
10422e588c75Sdanielk1977   columnMallocFailure(pStmt);
10432e588c75Sdanielk1977   return val;
10444f26d6c4Sdrh }
10454f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
10462e588c75Sdanielk1977   int val = sqlite3_value_int( columnMem(pStmt,i) );
10472e588c75Sdanielk1977   columnMallocFailure(pStmt);
10482e588c75Sdanielk1977   return val;
10494f26d6c4Sdrh }
1050efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
10512e588c75Sdanielk1977   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
10522e588c75Sdanielk1977   columnMallocFailure(pStmt);
10532e588c75Sdanielk1977   return val;
10544f26d6c4Sdrh }
10554f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
10562e588c75Sdanielk1977   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
10572e588c75Sdanielk1977   columnMallocFailure(pStmt);
10582e588c75Sdanielk1977   return val;
10594f26d6c4Sdrh }
1060d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
1061d0ffa1e8Sdanielk1977   Mem *pOut = columnMem(pStmt, i);
1062d0ffa1e8Sdanielk1977   if( pOut->flags&MEM_Static ){
1063d0ffa1e8Sdanielk1977     pOut->flags &= ~MEM_Static;
1064d0ffa1e8Sdanielk1977     pOut->flags |= MEM_Ephem;
1065d0ffa1e8Sdanielk1977   }
106632bc3f6eSdrh   columnMallocFailure(pStmt);
1067d0ffa1e8Sdanielk1977   return (sqlite3_value *)pOut;
1068d1e4733dSdrh }
10696c62608fSdrh #ifndef SQLITE_OMIT_UTF16
10704f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
10712e588c75Sdanielk1977   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
10722e588c75Sdanielk1977   columnMallocFailure(pStmt);
10732e588c75Sdanielk1977   return val;
10744f26d6c4Sdrh }
10756c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
10764f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
107732bc3f6eSdrh   int iType = sqlite3_value_type( columnMem(pStmt,i) );
107832bc3f6eSdrh   columnMallocFailure(pStmt);
107932bc3f6eSdrh   return iType;
10804f26d6c4Sdrh }
10814f26d6c4Sdrh 
10825e2517e1Sdrh /*
10835e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using
10845e2517e1Sdrh ** xFunc() then return that string.  If N is out of range, return 0.
1085e590fbdeSdrh **
1086e590fbdeSdrh ** There are up to 5 names for each column.  useType determines which
1087e590fbdeSdrh ** name is returned.  Here are the names:
1088e590fbdeSdrh **
1089e590fbdeSdrh **    0      The column name as it should be displayed for output
1090e590fbdeSdrh **    1      The datatype name for the column
1091e590fbdeSdrh **    2      The name of the database that the column derives from
1092e590fbdeSdrh **    3      The name of the table that the column derives from
1093e590fbdeSdrh **    4      The name of the table column that the result column derives from
1094e590fbdeSdrh **
1095e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression
1096e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL.
10975e2517e1Sdrh */
10985e2517e1Sdrh static const void *columnName(
10995e2517e1Sdrh   sqlite3_stmt *pStmt,
11005e2517e1Sdrh   int N,
11015e2517e1Sdrh   const void *(*xFunc)(Mem*),
11025e2517e1Sdrh   int useType
11035e2517e1Sdrh ){
11049ca95730Sdrh   const void *ret;
11059ca95730Sdrh   Vdbe *p;
110632bc3f6eSdrh   int n;
11079ca95730Sdrh   sqlite3 *db;
11089ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
11099ca95730Sdrh   if( pStmt==0 ){
11109ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
11119ca95730Sdrh     return 0;
11129ca95730Sdrh   }
11139ca95730Sdrh #endif
11149ca95730Sdrh   ret = 0;
11159ca95730Sdrh   p = (Vdbe *)pStmt;
11169ca95730Sdrh   db = p->db;
1117c6c7fd51Sdrh   assert( db!=0 );
111832bc3f6eSdrh   n = sqlite3_column_count(pStmt);
111932bc3f6eSdrh   if( N<n && N>=0 ){
1120e590fbdeSdrh     N += useType*n;
1121c6c7fd51Sdrh     sqlite3_mutex_enter(db->mutex);
1122c6c7fd51Sdrh     assert( db->mallocFailed==0 );
112300fd957bSdanielk1977     ret = xFunc(&p->aColName[N]);
112432bc3f6eSdrh      /* A malloc may have failed inside of the xFunc() call. If this
112532bc3f6eSdrh     ** is the case, clear the mallocFailed flag and return NULL.
112600fd957bSdanielk1977     */
1127c6c7fd51Sdrh     if( db->mallocFailed ){
11284a642b60Sdrh       sqlite3OomClear(db);
112932bc3f6eSdrh       ret = 0;
113032bc3f6eSdrh     }
1131c6c7fd51Sdrh     sqlite3_mutex_leave(db->mutex);
113232bc3f6eSdrh   }
113300fd957bSdanielk1977   return ret;
11345e2517e1Sdrh }
11355e2517e1Sdrh 
11364f26d6c4Sdrh /*
11374f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL
11384f26d6c4Sdrh ** statement pStmt.
11394f26d6c4Sdrh */
11404f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
1141955de52cSdanielk1977   return columnName(
1142955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
11434f26d6c4Sdrh }
1144e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
1145e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
1146955de52cSdanielk1977   return columnName(
1147955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
1148e590fbdeSdrh }
1149e590fbdeSdrh #endif
11504f26d6c4Sdrh 
11514f26d6c4Sdrh /*
11523f913576Sdrh ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
11533f913576Sdrh ** not define OMIT_DECLTYPE.
11543f913576Sdrh */
11553f913576Sdrh #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
11563f913576Sdrh # error "Must not define both SQLITE_OMIT_DECLTYPE \
11573f913576Sdrh          and SQLITE_ENABLE_COLUMN_METADATA"
11583f913576Sdrh #endif
11593f913576Sdrh 
11603f913576Sdrh #ifndef SQLITE_OMIT_DECLTYPE
11613f913576Sdrh /*
11626c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column
1163e590fbdeSdrh ** of the result set of SQL statement pStmt.
11646c62608fSdrh */
11656c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
1166955de52cSdanielk1977   return columnName(
1167955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
11686c62608fSdrh }
11696c62608fSdrh #ifndef SQLITE_OMIT_UTF16
117076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
1171955de52cSdanielk1977   return columnName(
1172955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
11734f26d6c4Sdrh }
11746c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
11753f913576Sdrh #endif /* SQLITE_OMIT_DECLTYPE */
11764f26d6c4Sdrh 
11774b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1178e590fbdeSdrh /*
1179e590fbdeSdrh ** Return the name of the database from which a result column derives.
1180e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
118160ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1182e590fbdeSdrh */
1183e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
1184955de52cSdanielk1977   return columnName(
1185955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
1186e590fbdeSdrh }
1187e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
1188e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
1189955de52cSdanielk1977   return columnName(
1190955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
1191e590fbdeSdrh }
1192e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
1193e590fbdeSdrh 
1194e590fbdeSdrh /*
1195e590fbdeSdrh ** Return the name of the table from which a result column derives.
1196e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
119760ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1198e590fbdeSdrh */
1199e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
1200955de52cSdanielk1977   return columnName(
1201955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
1202e590fbdeSdrh }
1203e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
1204e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
1205955de52cSdanielk1977   return columnName(
1206955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
1207e590fbdeSdrh }
1208e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
1209e590fbdeSdrh 
1210e590fbdeSdrh /*
1211e590fbdeSdrh ** Return the name of the table column from which a result column derives.
1212e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or
121360ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column.
1214e590fbdeSdrh */
1215e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
1216955de52cSdanielk1977   return columnName(
1217955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
1218e590fbdeSdrh }
1219e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16
1220e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
1221955de52cSdanielk1977   return columnName(
1222955de52cSdanielk1977       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
1223e590fbdeSdrh }
1224e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */
12254b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
1226e590fbdeSdrh 
1227e590fbdeSdrh 
12284f26d6c4Sdrh /******************************* sqlite3_bind_  ***************************
12294f26d6c4Sdrh **
12304f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement.
12314f26d6c4Sdrh */
12324f26d6c4Sdrh /*
12334f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the
12344f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is
12354f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
12364f26d6c4Sdrh **
1237488e7b63Sdrh ** A successful evaluation of this routine acquires the mutex on p.
1238488e7b63Sdrh ** the mutex is released if any kind of error occurs.
1239488e7b63Sdrh **
12404f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return
12414f26d6c4Sdrh ** value in any case.
12424f26d6c4Sdrh */
12434f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){
12444f26d6c4Sdrh   Mem *pVar;
1245413c3d36Sdrh   if( vdbeSafetyNotNull(p) ){
1246413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
1247413c3d36Sdrh   }
1248488e7b63Sdrh   sqlite3_mutex_enter(p->db->mutex);
1249488e7b63Sdrh   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
125013f40da3Sdrh     sqlite3Error(p->db, SQLITE_MISUSE);
1251488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
1252413c3d36Sdrh     sqlite3_log(SQLITE_MISUSE,
1253413c3d36Sdrh         "bind on a busy prepared statement: [%s]", p->zSql);
1254413c3d36Sdrh     return SQLITE_MISUSE_BKPT;
12554f26d6c4Sdrh   }
12564f26d6c4Sdrh   if( i<1 || i>p->nVar ){
125713f40da3Sdrh     sqlite3Error(p->db, SQLITE_RANGE);
1258488e7b63Sdrh     sqlite3_mutex_leave(p->db->mutex);
12594f26d6c4Sdrh     return SQLITE_RANGE;
12604f26d6c4Sdrh   }
12614f26d6c4Sdrh   i--;
1262290c1948Sdrh   pVar = &p->aVar[i];
1263d8123366Sdanielk1977   sqlite3VdbeMemRelease(pVar);
12644f26d6c4Sdrh   pVar->flags = MEM_Null;
126513f40da3Sdrh   sqlite3Error(p->db, SQLITE_OK);
1266937d0deaSdan 
12671d2ce4f8Sdan   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
12681d2ce4f8Sdan   ** binding a new value to this variable invalidates the current query plan.
1269a7044007Sdrh   **
1270a7044007Sdrh   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1271a7044007Sdrh   ** parameter in the WHERE clause might influence the choice of query plan
1272a7044007Sdrh   ** for a statement, then the statement will be automatically recompiled,
1273a7044007Sdrh   ** as if there had been a schema change, on the first sqlite3_step() call
1274a7044007Sdrh   ** following any change to the bindings of that parameter.
12751d2ce4f8Sdan   */
1276bda4cb87Sdan   assert( p->isPrepareV2 || p->expmask==0 );
12772996796dSdrh   if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
1278937d0deaSdan     p->expired = 1;
1279937d0deaSdan   }
12804f26d6c4Sdrh   return SQLITE_OK;
12814f26d6c4Sdrh }
12824f26d6c4Sdrh 
12834f26d6c4Sdrh /*
12845e2517e1Sdrh ** Bind a text or BLOB value.
12854f26d6c4Sdrh */
12865e2517e1Sdrh static int bindText(
1287605264d2Sdrh   sqlite3_stmt *pStmt,   /* The statement to bind against */
1288605264d2Sdrh   int i,                 /* Index of the parameter to bind */
1289605264d2Sdrh   const void *zData,     /* Pointer to the data to be bound */
1290605264d2Sdrh   int nData,             /* Number of bytes of data to be bound */
1291605264d2Sdrh   void (*xDel)(void*),   /* Destructor for the data */
1292b27b7f5dSdrh   u8 encoding            /* Encoding for the data */
12934f26d6c4Sdrh ){
12944f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
12954f26d6c4Sdrh   Mem *pVar;
12964f26d6c4Sdrh   int rc;
12974f26d6c4Sdrh 
12984f26d6c4Sdrh   rc = vdbeUnbind(p, i);
1299488e7b63Sdrh   if( rc==SQLITE_OK ){
1300488e7b63Sdrh     if( zData!=0 ){
1301290c1948Sdrh       pVar = &p->aVar[i-1];
1302b21c8cd4Sdrh       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
13035e2517e1Sdrh       if( rc==SQLITE_OK && encoding!=0 ){
1304b21c8cd4Sdrh         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
13055e2517e1Sdrh       }
130613f40da3Sdrh       sqlite3Error(p->db, rc);
1307605264d2Sdrh       rc = sqlite3ApiExit(p->db, rc);
130827641703Sdrh     }
1309605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
131094bb2ba6Sdrh   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
13116fec9ee3Sdrh     xDel((void*)zData);
1312488e7b63Sdrh   }
1313605264d2Sdrh   return rc;
13145e2517e1Sdrh }
13155e2517e1Sdrh 
13165e2517e1Sdrh 
13175e2517e1Sdrh /*
13185e2517e1Sdrh ** Bind a blob value to an SQL statement variable.
13195e2517e1Sdrh */
13205e2517e1Sdrh int sqlite3_bind_blob(
13215e2517e1Sdrh   sqlite3_stmt *pStmt,
13225e2517e1Sdrh   int i,
13235e2517e1Sdrh   const void *zData,
13245e2517e1Sdrh   int nData,
13255e2517e1Sdrh   void (*xDel)(void*)
13265e2517e1Sdrh ){
13275b081d8aSdrh #ifdef SQLITE_ENABLE_API_ARMOR
13285b081d8aSdrh   if( nData<0 ) return SQLITE_MISUSE_BKPT;
13295b081d8aSdrh #endif
13305e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, 0);
13315e2517e1Sdrh }
1332da4ca9d1Sdrh int sqlite3_bind_blob64(
1333da4ca9d1Sdrh   sqlite3_stmt *pStmt,
1334da4ca9d1Sdrh   int i,
1335da4ca9d1Sdrh   const void *zData,
1336da4ca9d1Sdrh   sqlite3_uint64 nData,
1337da4ca9d1Sdrh   void (*xDel)(void*)
1338da4ca9d1Sdrh ){
1339fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
1340da4ca9d1Sdrh   if( nData>0x7fffffff ){
1341da4ca9d1Sdrh     return invokeValueDestructor(zData, xDel, 0);
1342da4ca9d1Sdrh   }else{
13433329a63aSdrh     return bindText(pStmt, i, zData, (int)nData, xDel, 0);
1344da4ca9d1Sdrh   }
1345da4ca9d1Sdrh }
13464f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
13474f26d6c4Sdrh   int rc;
13484f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
13494f26d6c4Sdrh   rc = vdbeUnbind(p, i);
13504f26d6c4Sdrh   if( rc==SQLITE_OK ){
1351290c1948Sdrh     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1352605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1353488e7b63Sdrh   }
1354f4618891Sdanielk1977   return rc;
13554f26d6c4Sdrh }
13564f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
1357efad9995Sdrh   return sqlite3_bind_int64(p, i, (i64)iValue);
13584f26d6c4Sdrh }
1359efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
13604f26d6c4Sdrh   int rc;
13614f26d6c4Sdrh   Vdbe *p = (Vdbe *)pStmt;
13624f26d6c4Sdrh   rc = vdbeUnbind(p, i);
13634f26d6c4Sdrh   if( rc==SQLITE_OK ){
1364290c1948Sdrh     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
1365605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1366488e7b63Sdrh   }
13674f26d6c4Sdrh   return rc;
13684f26d6c4Sdrh }
1369605264d2Sdrh int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1370605264d2Sdrh   int rc;
1371605264d2Sdrh   Vdbe *p = (Vdbe*)pStmt;
1372605264d2Sdrh   rc = vdbeUnbind(p, i);
1373488e7b63Sdrh   if( rc==SQLITE_OK ){
1374605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1375488e7b63Sdrh   }
1376605264d2Sdrh   return rc;
13774f26d6c4Sdrh }
13784f26d6c4Sdrh int sqlite3_bind_text(
13794f26d6c4Sdrh   sqlite3_stmt *pStmt,
13804f26d6c4Sdrh   int i,
13814f26d6c4Sdrh   const char *zData,
13824f26d6c4Sdrh   int nData,
1383d8123366Sdanielk1977   void (*xDel)(void*)
13844f26d6c4Sdrh ){
13855e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
13864f26d6c4Sdrh }
1387bbf483f8Sdrh int sqlite3_bind_text64(
1388da4ca9d1Sdrh   sqlite3_stmt *pStmt,
1389da4ca9d1Sdrh   int i,
1390da4ca9d1Sdrh   const char *zData,
1391da4ca9d1Sdrh   sqlite3_uint64 nData,
1392da4ca9d1Sdrh   void (*xDel)(void*),
1393da4ca9d1Sdrh   unsigned char enc
1394da4ca9d1Sdrh ){
1395fc59a954Sdrh   assert( xDel!=SQLITE_DYNAMIC );
1396da4ca9d1Sdrh   if( nData>0x7fffffff ){
1397da4ca9d1Sdrh     return invokeValueDestructor(zData, xDel, 0);
1398da4ca9d1Sdrh   }else{
1399fc59a954Sdrh     if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
14003329a63aSdrh     return bindText(pStmt, i, zData, (int)nData, xDel, enc);
1401da4ca9d1Sdrh   }
1402da4ca9d1Sdrh }
14036c62608fSdrh #ifndef SQLITE_OMIT_UTF16
14044f26d6c4Sdrh int sqlite3_bind_text16(
14054f26d6c4Sdrh   sqlite3_stmt *pStmt,
14064f26d6c4Sdrh   int i,
14074f26d6c4Sdrh   const void *zData,
14084f26d6c4Sdrh   int nData,
1409d8123366Sdanielk1977   void (*xDel)(void*)
14104f26d6c4Sdrh ){
14115e2517e1Sdrh   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
14124f26d6c4Sdrh }
14136c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
141426e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
141526e4144dSdanielk1977   int rc;
14161b27b8c0Sdrh   switch( sqlite3_value_type((sqlite3_value*)pValue) ){
141729def560Sdrh     case SQLITE_INTEGER: {
141829def560Sdrh       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
141929def560Sdrh       break;
142029def560Sdrh     }
142129def560Sdrh     case SQLITE_FLOAT: {
142274eaba4dSdrh       rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
142329def560Sdrh       break;
142429def560Sdrh     }
142529def560Sdrh     case SQLITE_BLOB: {
142629def560Sdrh       if( pValue->flags & MEM_Zero ){
142729def560Sdrh         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
142829def560Sdrh       }else{
142929def560Sdrh         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
143029def560Sdrh       }
143129def560Sdrh       break;
143229def560Sdrh     }
143329def560Sdrh     case SQLITE_TEXT: {
1434ac80db78Sdrh       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
1435ac80db78Sdrh                               pValue->enc);
1436ac80db78Sdrh       break;
1437ac80db78Sdrh     }
1438ac80db78Sdrh     default: {
1439ac80db78Sdrh       rc = sqlite3_bind_null(pStmt, i);
144029def560Sdrh       break;
144129def560Sdrh     }
14420f5ea0b3Sdrh   }
144326e4144dSdanielk1977   return rc;
144426e4144dSdanielk1977 }
1445b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1446b026e05eSdrh   int rc;
1447b026e05eSdrh   Vdbe *p = (Vdbe *)pStmt;
1448b026e05eSdrh   rc = vdbeUnbind(p, i);
1449b026e05eSdrh   if( rc==SQLITE_OK ){
1450b026e05eSdrh     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1451605264d2Sdrh     sqlite3_mutex_leave(p->db->mutex);
1452488e7b63Sdrh   }
1453b026e05eSdrh   return rc;
1454b026e05eSdrh }
145580c03022Sdan int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
145680c03022Sdan   int rc;
145780c03022Sdan   Vdbe *p = (Vdbe *)pStmt;
145880c03022Sdan   sqlite3_mutex_enter(p->db->mutex);
145980c03022Sdan   if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
146080c03022Sdan     rc = SQLITE_TOOBIG;
146180c03022Sdan   }else{
146280c03022Sdan     assert( (n & 0x7FFFFFFF)==n );
146380c03022Sdan     rc = sqlite3_bind_zeroblob(pStmt, i, n);
146480c03022Sdan   }
146580c03022Sdan   rc = sqlite3ApiExit(p->db, rc);
146680c03022Sdan   sqlite3_mutex_leave(p->db->mutex);
146780c03022Sdan   return rc;
146880c03022Sdan }
146975f6a032Sdrh 
147075f6a032Sdrh /*
147175f6a032Sdrh ** Return the number of wildcards that can be potentially bound to.
147275f6a032Sdrh ** This routine is added to support DBD::SQLite.
147375f6a032Sdrh */
147475f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
147592febd92Sdrh   Vdbe *p = (Vdbe*)pStmt;
147692febd92Sdrh   return p ? p->nVar : 0;
147775f6a032Sdrh }
1478895d7472Sdrh 
1479895d7472Sdrh /*
1480fa6bc000Sdrh ** Return the name of a wildcard parameter.  Return NULL if the index
1481fa6bc000Sdrh ** is out of range or if the wildcard is unnamed.
1482fa6bc000Sdrh **
1483fa6bc000Sdrh ** The result is always UTF-8.
1484fa6bc000Sdrh */
1485fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1486fa6bc000Sdrh   Vdbe *p = (Vdbe*)pStmt;
14879bf755ccSdrh   if( p==0 ) return 0;
14889bf755ccSdrh   return sqlite3VListNumToName(p->pVList, i);
1489895d7472Sdrh }
1490fa6bc000Sdrh 
1491fa6bc000Sdrh /*
1492fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable
1493fa6bc000Sdrh ** with that name.  If there is no variable with the given name,
1494fa6bc000Sdrh ** return 0.
1495fa6bc000Sdrh */
14965f18a221Sdrh int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
14979bf755ccSdrh   if( p==0 || zName==0 ) return 0;
14989bf755ccSdrh   return sqlite3VListNameToNum(p->pVList, zName, nName);
1499fa6bc000Sdrh }
15005f18a221Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
15015f18a221Sdrh   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
15025f18a221Sdrh }
1503f8db1bc0Sdrh 
150451942bc3Sdrh /*
1505f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second.
1506f8db1bc0Sdrh */
1507145834a4Sshane int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1508f8db1bc0Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
1509f8db1bc0Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
15100f5ea0b3Sdrh   int i;
15110f5ea0b3Sdrh   assert( pTo->db==pFrom->db );
15120f5ea0b3Sdrh   assert( pTo->nVar==pFrom->nVar );
151327641703Sdrh   sqlite3_mutex_enter(pTo->db->mutex);
15140f5ea0b3Sdrh   for(i=0; i<pFrom->nVar; i++){
1515643167ffSdrh     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1516f8db1bc0Sdrh   }
151727641703Sdrh   sqlite3_mutex_leave(pTo->db->mutex);
15180f5ea0b3Sdrh   return SQLITE_OK;
1519f8db1bc0Sdrh }
152051942bc3Sdrh 
1521eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
152251942bc3Sdrh /*
1523145834a4Sshane ** Deprecated external interface.  Internal/core SQLite code
1524145834a4Sshane ** should call sqlite3TransferBindings.
15250f5ea0b3Sdrh **
152660ec914cSpeter.d.reid ** It is misuse to call this routine with statements from different
15270f5ea0b3Sdrh ** database connections.  But as this is a deprecated interface, we
15280f5ea0b3Sdrh ** will not bother to check for that condition.
15290f5ea0b3Sdrh **
15300f5ea0b3Sdrh ** If the two statements contain a different number of bindings, then
15310f5ea0b3Sdrh ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
15320f5ea0b3Sdrh ** SQLITE_OK is returned.
1533145834a4Sshane */
1534145834a4Sshane int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
15350f5ea0b3Sdrh   Vdbe *pFrom = (Vdbe*)pFromStmt;
15360f5ea0b3Sdrh   Vdbe *pTo = (Vdbe*)pToStmt;
15370f5ea0b3Sdrh   if( pFrom->nVar!=pTo->nVar ){
15380f5ea0b3Sdrh     return SQLITE_ERROR;
15390f5ea0b3Sdrh   }
1540bda4cb87Sdan   assert( pTo->isPrepareV2 || pTo->expmask==0 );
1541bda4cb87Sdan   if( pTo->expmask ){
1542c94b8595Sdan     pTo->expired = 1;
1543c94b8595Sdan   }
1544bda4cb87Sdan   assert( pFrom->isPrepareV2 || pFrom->expmask==0 );
1545bda4cb87Sdan   if( pFrom->expmask ){
1546c94b8595Sdan     pFrom->expired = 1;
1547c94b8595Sdan   }
1548145834a4Sshane   return sqlite3TransferBindings(pFromStmt, pToStmt);
1549145834a4Sshane }
1550eec556d3Sshane #endif
1551145834a4Sshane 
1552145834a4Sshane /*
155351942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given
155451942bc3Sdrh ** in the argument belongs.  This is the same database handle that was
155551942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create
155651942bc3Sdrh ** the statement in the first place.
155751942bc3Sdrh */
155851942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
155951942bc3Sdrh   return pStmt ? ((Vdbe*)pStmt)->db : 0;
156051942bc3Sdrh }
1561bb5a9c3eSdrh 
1562bb5a9c3eSdrh /*
1563f03d9cccSdrh ** Return true if the prepared statement is guaranteed to not modify the
1564f03d9cccSdrh ** database.
1565f03d9cccSdrh */
1566f03d9cccSdrh int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1567f03d9cccSdrh   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1568f03d9cccSdrh }
1569f03d9cccSdrh 
1570f03d9cccSdrh /*
15712fb6693eSdrh ** Return true if the prepared statement is in need of being reset.
15722fb6693eSdrh */
15732fb6693eSdrh int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
15742fb6693eSdrh   Vdbe *v = (Vdbe*)pStmt;
157576336d5bSdrh   return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
15762fb6693eSdrh }
15772fb6693eSdrh 
15782fb6693eSdrh /*
1579bb5a9c3eSdrh ** Return a pointer to the next prepared statement after pStmt associated
1580bb5a9c3eSdrh ** with database connection pDb.  If pStmt is NULL, return the first
1581bb5a9c3eSdrh ** prepared statement for the database connection.  Return NULL if there
1582bb5a9c3eSdrh ** are no more.
1583bb5a9c3eSdrh */
1584bb5a9c3eSdrh sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1585bb5a9c3eSdrh   sqlite3_stmt *pNext;
15869ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
15879ca95730Sdrh   if( !sqlite3SafetyCheckOk(pDb) ){
15889ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
15899ca95730Sdrh     return 0;
15909ca95730Sdrh   }
15919ca95730Sdrh #endif
1592bb5a9c3eSdrh   sqlite3_mutex_enter(pDb->mutex);
1593bb5a9c3eSdrh   if( pStmt==0 ){
1594bb5a9c3eSdrh     pNext = (sqlite3_stmt*)pDb->pVdbe;
1595bb5a9c3eSdrh   }else{
1596bb5a9c3eSdrh     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1597bb5a9c3eSdrh   }
1598bb5a9c3eSdrh   sqlite3_mutex_leave(pDb->mutex);
1599bb5a9c3eSdrh   return pNext;
1600bb5a9c3eSdrh }
1601d1d38488Sdrh 
1602d1d38488Sdrh /*
1603d1d38488Sdrh ** Return the value of a status counter for a prepared statement
1604d1d38488Sdrh */
1605d1d38488Sdrh int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1606d1d38488Sdrh   Vdbe *pVdbe = (Vdbe*)pStmt;
16079ca95730Sdrh   u32 v;
16089ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
16099ca95730Sdrh   if( !pStmt ){
16109ca95730Sdrh     (void)SQLITE_MISUSE_BKPT;
16119ca95730Sdrh     return 0;
16129ca95730Sdrh   }
16139ca95730Sdrh #endif
16149ca95730Sdrh   v = pVdbe->aCounter[op];
16159b47ee3fSdrh   if( resetFlag ) pVdbe->aCounter[op] = 0;
16169b47ee3fSdrh   return (int)v;
1617d1d38488Sdrh }
161846c47d46Sdan 
1619557341e8Sdrh /*
1620557341e8Sdrh ** Return the SQL associated with a prepared statement
1621557341e8Sdrh */
1622557341e8Sdrh const char *sqlite3_sql(sqlite3_stmt *pStmt){
1623557341e8Sdrh   Vdbe *p = (Vdbe *)pStmt;
1624557341e8Sdrh   return p ? p->zSql : 0;
1625557341e8Sdrh }
1626557341e8Sdrh 
1627557341e8Sdrh /*
1628557341e8Sdrh ** Return the SQL associated with a prepared statement with
1629557341e8Sdrh ** bound parameters expanded.  Space to hold the returned string is
1630557341e8Sdrh ** obtained from sqlite3_malloc().  The caller is responsible for
1631557341e8Sdrh ** freeing the returned string by passing it to sqlite3_free().
1632557341e8Sdrh **
1633557341e8Sdrh ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1634557341e8Sdrh ** expanded bound parameters.
1635557341e8Sdrh */
1636557341e8Sdrh char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1637557341e8Sdrh #ifdef SQLITE_OMIT_TRACE
1638557341e8Sdrh   return 0;
1639557341e8Sdrh #else
1640557341e8Sdrh   char *z = 0;
1641557341e8Sdrh   const char *zSql = sqlite3_sql(pStmt);
1642557341e8Sdrh   if( zSql ){
1643557341e8Sdrh     Vdbe *p = (Vdbe *)pStmt;
1644557341e8Sdrh     sqlite3_mutex_enter(p->db->mutex);
1645557341e8Sdrh     z = sqlite3VdbeExpandSql(p, zSql);
1646557341e8Sdrh     sqlite3_mutex_leave(p->db->mutex);
1647557341e8Sdrh   }
1648557341e8Sdrh   return z;
1649557341e8Sdrh #endif
1650557341e8Sdrh }
1651557341e8Sdrh 
16529b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
165337db03bfSdan /*
165493bca695Sdan ** Allocate and populate an UnpackedRecord structure based on the serialized
165593bca695Sdan ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
165693bca695Sdan ** if successful, or a NULL pointer if an OOM error is encountered.
165793bca695Sdan */
165893bca695Sdan static UnpackedRecord *vdbeUnpackRecord(
165993bca695Sdan   KeyInfo *pKeyInfo,
166093bca695Sdan   int nKey,
166193bca695Sdan   const void *pKey
166293bca695Sdan ){
166393bca695Sdan   UnpackedRecord *pRet;           /* Return value */
166493bca695Sdan 
1665a582b016Sdrh   pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
166693bca695Sdan   if( pRet ){
1667c8d985e0Sdrh     memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
166893bca695Sdan     sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
166993bca695Sdan   }
167093bca695Sdan   return pRet;
167193bca695Sdan }
167293bca695Sdan 
167393bca695Sdan /*
167437db03bfSdan ** This function is called from within a pre-update callback to retrieve
167537db03bfSdan ** a field of the row currently being updated or deleted.
167637db03bfSdan */
167746c47d46Sdan int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
167846c47d46Sdan   PreUpdate *p = db->pPreUpdate;
16797271d7a1Sdan   Mem *pMem;
168046c47d46Sdan   int rc = SQLITE_OK;
168146c47d46Sdan 
168237db03bfSdan   /* Test that this call is being made from within an SQLITE_DELETE or
168337db03bfSdan   ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
168446c47d46Sdan   if( !p || p->op==SQLITE_INSERT ){
168546c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
168646c47d46Sdan     goto preupdate_old_out;
168746c47d46Sdan   }
1688cb9a3643Sdan   if( p->pPk ){
1689cb9a3643Sdan     iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1690cb9a3643Sdan   }
169146c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
169246c47d46Sdan     rc = SQLITE_RANGE;
169346c47d46Sdan     goto preupdate_old_out;
169446c47d46Sdan   }
169546c47d46Sdan 
169637db03bfSdan   /* If the old.* record has not yet been loaded into memory, do so now. */
169746c47d46Sdan   if( p->pUnpacked==0 ){
169812ca0b56Sdan     u32 nRec;
169912ca0b56Sdan     u8 *aRec;
170046c47d46Sdan 
1701a7c90c42Sdrh     nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
170212ca0b56Sdan     aRec = sqlite3DbMallocRaw(db, nRec);
170312ca0b56Sdan     if( !aRec ) goto preupdate_old_out;
1704cb3cabd0Sdrh     rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
170512ca0b56Sdan     if( rc==SQLITE_OK ){
170693bca695Sdan       p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
170712ca0b56Sdan       if( !p->pUnpacked ) rc = SQLITE_NOMEM;
170812ca0b56Sdan     }
170946c47d46Sdan     if( rc!=SQLITE_OK ){
171012ca0b56Sdan       sqlite3DbFree(db, aRec);
171146c47d46Sdan       goto preupdate_old_out;
171246c47d46Sdan     }
171312ca0b56Sdan     p->aRecord = aRec;
171446c47d46Sdan   }
171546c47d46Sdan 
17167271d7a1Sdan   pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1717e43635aaSdan   if( iIdx==p->pTab->iPKey ){
1718e43635aaSdan     sqlite3VdbeMemSetInt64(pMem, p->iKey1);
17197271d7a1Sdan   }else if( iIdx>=p->pUnpacked->nField ){
17207271d7a1Sdan     *ppValue = (sqlite3_value *)columnNullValue();
1721e43635aaSdan   }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1722e43635aaSdan     if( pMem->flags & MEM_Int ){
1723e43635aaSdan       sqlite3VdbeMemRealify(pMem);
1724e43635aaSdan     }
1725319eeb7bSdan   }
172646c47d46Sdan 
172746c47d46Sdan  preupdate_old_out:
1728e1ed0b0eSdrh   sqlite3Error(db, rc);
172946c47d46Sdan   return sqlite3ApiExit(db, rc);
173046c47d46Sdan }
17319b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
173246c47d46Sdan 
17339b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
173437db03bfSdan /*
173537db03bfSdan ** This function is called from within a pre-update callback to retrieve
173637db03bfSdan ** the number of columns in the row being updated, deleted or inserted.
173737db03bfSdan */
173846c47d46Sdan int sqlite3_preupdate_count(sqlite3 *db){
173946c47d46Sdan   PreUpdate *p = db->pPreUpdate;
1740e437ca5eSdan   return (p ? p->keyinfo.nField : 0);
174146c47d46Sdan }
17429b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
174346c47d46Sdan 
17449b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
174537db03bfSdan /*
17461e7a2d43Sdan ** This function is designed to be called from within a pre-update callback
17471e7a2d43Sdan ** only. It returns zero if the change that caused the callback was made
17481e7a2d43Sdan ** immediately by a user SQL statement. Or, if the change was made by a
17491e7a2d43Sdan ** trigger program, it returns the number of trigger programs currently
17501e7a2d43Sdan ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
17511e7a2d43Sdan ** top-level trigger etc.).
17521e7a2d43Sdan **
17531e7a2d43Sdan ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
17541e7a2d43Sdan ** or SET DEFAULT action is considered a trigger.
17551e7a2d43Sdan */
17561e7a2d43Sdan int sqlite3_preupdate_depth(sqlite3 *db){
17571e7a2d43Sdan   PreUpdate *p = db->pPreUpdate;
17581e7a2d43Sdan   return (p ? p->v->nFrame : 0);
17591e7a2d43Sdan }
17609b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
17611e7a2d43Sdan 
17629b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
17631e7a2d43Sdan /*
176437db03bfSdan ** This function is called from within a pre-update callback to retrieve
176537db03bfSdan ** a field of the row currently being updated or inserted.
176637db03bfSdan */
176737db03bfSdan int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
176846c47d46Sdan   PreUpdate *p = db->pPreUpdate;
176946c47d46Sdan   int rc = SQLITE_OK;
177037db03bfSdan   Mem *pMem;
177146c47d46Sdan 
177237db03bfSdan   if( !p || p->op==SQLITE_DELETE ){
177346c47d46Sdan     rc = SQLITE_MISUSE_BKPT;
177437db03bfSdan     goto preupdate_new_out;
177546c47d46Sdan   }
1776cb9a3643Sdan   if( p->pPk && p->op!=SQLITE_UPDATE ){
1777cb9a3643Sdan     iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1778cb9a3643Sdan   }
177946c47d46Sdan   if( iIdx>=p->pCsr->nField || iIdx<0 ){
178046c47d46Sdan     rc = SQLITE_RANGE;
178137db03bfSdan     goto preupdate_new_out;
178246c47d46Sdan   }
178346c47d46Sdan 
178437db03bfSdan   if( p->op==SQLITE_INSERT ){
178537db03bfSdan     /* For an INSERT, memory cell p->iNewReg contains the serialized record
178637db03bfSdan     ** that is being inserted. Deserialize it. */
178737db03bfSdan     UnpackedRecord *pUnpack = p->pNewUnpacked;
178837db03bfSdan     if( !pUnpack ){
178937db03bfSdan       Mem *pData = &p->v->aMem[p->iNewReg];
1790ff535a24Sdrh       rc = ExpandBlob(pData);
179137db03bfSdan       if( rc!=SQLITE_OK ) goto preupdate_new_out;
179293bca695Sdan       pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
179337db03bfSdan       if( !pUnpack ){
179437db03bfSdan         rc = SQLITE_NOMEM;
179537db03bfSdan         goto preupdate_new_out;
179637db03bfSdan       }
179737db03bfSdan       p->pNewUnpacked = pUnpack;
179837db03bfSdan     }
179937db03bfSdan     pMem = &pUnpack->aMem[iIdx];
1800e43635aaSdan     if( iIdx==p->pTab->iPKey ){
1801319eeb7bSdan       sqlite3VdbeMemSetInt64(pMem, p->iKey2);
18022a86c196Sdan     }else if( iIdx>=pUnpack->nField ){
18032a86c196Sdan       pMem = (sqlite3_value *)columnNullValue();
180437db03bfSdan     }
180537db03bfSdan   }else{
180637db03bfSdan     /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
180737db03bfSdan     ** value. Make a copy of the cell contents and return a pointer to it.
180837db03bfSdan     ** It is not safe to return a pointer to the memory cell itself as the
180937db03bfSdan     ** caller may modify the value text encoding.
181037db03bfSdan     */
181137db03bfSdan     assert( p->op==SQLITE_UPDATE );
181237db03bfSdan     if( !p->aNew ){
181337db03bfSdan       p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
181437db03bfSdan       if( !p->aNew ){
181537db03bfSdan         rc = SQLITE_NOMEM;
181637db03bfSdan         goto preupdate_new_out;
181737db03bfSdan       }
181837db03bfSdan     }
1819304637c0Sdrh     assert( iIdx>=0 && iIdx<p->pCsr->nField );
182037db03bfSdan     pMem = &p->aNew[iIdx];
182137db03bfSdan     if( pMem->flags==0 ){
1822e43635aaSdan       if( iIdx==p->pTab->iPKey ){
1823319eeb7bSdan         sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1824319eeb7bSdan       }else{
182537db03bfSdan         rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
182637db03bfSdan         if( rc!=SQLITE_OK ) goto preupdate_new_out;
1827319eeb7bSdan       }
182837db03bfSdan     }
182937db03bfSdan   }
183037db03bfSdan   *ppValue = pMem;
183137db03bfSdan 
183237db03bfSdan  preupdate_new_out:
1833e1ed0b0eSdrh   sqlite3Error(db, rc);
183446c47d46Sdan   return sqlite3ApiExit(db, rc);
183546c47d46Sdan }
18369b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
183704e8a586Sdrh 
18386f9702edSdan #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
183904489b6dSdan /*
184004489b6dSdan ** Return status data for a single loop within query pStmt.
184104489b6dSdan */
184204489b6dSdan int sqlite3_stmt_scanstatus(
1843d1a1c234Sdrh   sqlite3_stmt *pStmt,            /* Prepared statement being queried */
184404489b6dSdan   int idx,                        /* Index of loop to report on */
1845d1a1c234Sdrh   int iScanStatusOp,              /* Which metric to return */
1846d1a1c234Sdrh   void *pOut                      /* OUT: Write the answer here */
184704489b6dSdan ){
184804489b6dSdan   Vdbe *p = (Vdbe*)pStmt;
1849037b5324Sdan   ScanStatus *pScan;
18506f9702edSdan   if( idx<0 || idx>=p->nScan ) return 1;
18516f9702edSdan   pScan = &p->aScan[idx];
1852d1a1c234Sdrh   switch( iScanStatusOp ){
1853d1a1c234Sdrh     case SQLITE_SCANSTAT_NLOOP: {
1854d1a1c234Sdrh       *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
1855d1a1c234Sdrh       break;
1856d1a1c234Sdrh     }
1857d1a1c234Sdrh     case SQLITE_SCANSTAT_NVISIT: {
1858d1a1c234Sdrh       *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
1859d1a1c234Sdrh       break;
1860d1a1c234Sdrh     }
1861d1a1c234Sdrh     case SQLITE_SCANSTAT_EST: {
1862518140edSdrh       double r = 1.0;
1863518140edSdrh       LogEst x = pScan->nEst;
1864518140edSdrh       while( x<100 ){
1865518140edSdrh         x += 10;
1866518140edSdrh         r *= 0.5;
1867518140edSdrh       }
1868518140edSdrh       *(double*)pOut = r*sqlite3LogEstToInt(x);
1869d1a1c234Sdrh       break;
1870d1a1c234Sdrh     }
1871d1a1c234Sdrh     case SQLITE_SCANSTAT_NAME: {
1872d72219daSdan       *(const char**)pOut = pScan->zName;
1873d1a1c234Sdrh       break;
1874d1a1c234Sdrh     }
1875d1a1c234Sdrh     case SQLITE_SCANSTAT_EXPLAIN: {
18766f9702edSdan       if( pScan->addrExplain ){
1877d1a1c234Sdrh         *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
18786f9702edSdan       }else{
1879d1a1c234Sdrh         *(const char**)pOut = 0;
1880d1a1c234Sdrh       }
1881d1a1c234Sdrh       break;
1882d1a1c234Sdrh     }
1883c6652b1eSdrh     case SQLITE_SCANSTAT_SELECTID: {
1884c6652b1eSdrh       if( pScan->addrExplain ){
1885c6652b1eSdrh         *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
1886c6652b1eSdrh       }else{
1887c6652b1eSdrh         *(int*)pOut = -1;
1888c6652b1eSdrh       }
1889c6652b1eSdrh       break;
1890c6652b1eSdrh     }
1891d1a1c234Sdrh     default: {
1892d1a1c234Sdrh       return 1;
18936f9702edSdan     }
18946f9702edSdan   }
189504489b6dSdan   return 0;
189604489b6dSdan }
189704489b6dSdan 
189804489b6dSdan /*
189904489b6dSdan ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
190004489b6dSdan */
190104489b6dSdan void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
190204489b6dSdan   Vdbe *p = (Vdbe*)pStmt;
19036f9702edSdan   memset(p->anExec, 0, p->nOp * sizeof(i64));
190404489b6dSdan }
19056f9702edSdan #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
1906