blob: 65fb4f541c7b5391eec70c1caa22ad1ffe8581c2 [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
2** 2004 May 26
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains code use to implement APIs that are part of the
14** VDBE.
15*/
16#include "sqliteInt.h"
17#include "vdbeInt.h"
adame6c3c722009-11-03 22:34:35 +000018#ifdef SQLITE_ENABLE_SQLRR
19# include "sqlrr.h"
20#endif
drh4f26d6c2004-05-26 23:25:30 +000021
shaneeec556d2008-10-12 00:27:53 +000022#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +000023/*
24** Return TRUE (non-zero) of the statement supplied as an argument needs
25** to be recompiled. A statement needs to be recompiled whenever the
26** execution environment changes in a way that would alter the program
27** that sqlite3_prepare() generates. For example, if new functions or
28** collating sequences are registered or if an authorizer function is
29** added or changed.
drhd89bd002005-01-22 03:03:54 +000030*/
31int sqlite3_expired(sqlite3_stmt *pStmt){
32 Vdbe *p = (Vdbe*)pStmt;
33 return p==0 || p->expired;
34}
shaneeec556d2008-10-12 00:27:53 +000035#endif
drhd89bd002005-01-22 03:03:54 +000036
drhe30f4422007-08-21 16:15:55 +000037/*
drh413c3d32010-02-23 20:11:56 +000038** Check on a Vdbe to make sure it has not been finalized. Log
39** an error and return true if it has been finalized (or is otherwise
40** invalid). Return false if it is ok.
41*/
42static int vdbeSafety(Vdbe *p){
drhb33a4fd2014-05-09 12:18:06 +000043 if( p->db==0 ){
drh413c3d32010-02-23 20:11:56 +000044 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
45 return 1;
46 }else{
47 return 0;
48 }
49}
50static int vdbeSafetyNotNull(Vdbe *p){
51 if( p==0 ){
52 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
53 return 1;
54 }else{
55 return vdbeSafety(p);
56 }
57}
58
drh201e0c62015-07-14 14:48:50 +000059#ifndef SQLITE_OMIT_TRACE
60/*
61** Invoke the profile callback. This routine is only called if we already
62** know that the profile callback is defined and needs to be invoked.
63*/
64static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
65 sqlite3_int64 iNow;
drh3d2a5292016-07-13 22:55:01 +000066 sqlite3_int64 iElapse;
drh201e0c62015-07-14 14:48:50 +000067 assert( p->startTime>0 );
drh3d2a5292016-07-13 22:55:01 +000068 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
drh201e0c62015-07-14 14:48:50 +000069 assert( db->init.busy==0 );
70 assert( p->zSql!=0 );
71 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
drh3d2a5292016-07-13 22:55:01 +000072 iElapse = (iNow - p->startTime)*1000000;
73 if( db->xProfile ){
74 db->xProfile(db->pProfileArg, p->zSql, iElapse);
75 }
76 if( db->mTrace & SQLITE_TRACE_PROFILE ){
drhfca760c2016-07-14 01:09:08 +000077 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
drh3d2a5292016-07-13 22:55:01 +000078 }
drh201e0c62015-07-14 14:48:50 +000079 p->startTime = 0;
80}
81/*
82** The checkProfileCallback(DB,P) macro checks to see if a profile callback
83** is needed, and it invokes the callback if it is needed.
84*/
85# define checkProfileCallback(DB,P) \
86 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
87#else
88# define checkProfileCallback(DB,P) /*no-op*/
89#endif
90
drh413c3d32010-02-23 20:11:56 +000091/*
drhe30f4422007-08-21 16:15:55 +000092** The following routine destroys a virtual machine that is created by
93** the sqlite3_compile() routine. The integer returned is an SQLITE_
94** success/failure code that describes the result of executing the virtual
95** machine.
96**
97** This routine sets the error code and string returned by
98** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
99*/
100int sqlite3_finalize(sqlite3_stmt *pStmt){
101 int rc;
102 if( pStmt==0 ){
drh65bafa62010-09-29 01:54:00 +0000103 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
104 ** pointer is a harmless no-op. */
drhe30f4422007-08-21 16:15:55 +0000105 rc = SQLITE_OK;
106 }else{
107 Vdbe *v = (Vdbe*)pStmt;
adame6c3c722009-11-03 22:34:35 +0000108#ifdef SQLITE_ENABLE_SQLRR
109 SRRecFinalize(pStmt);
110#endif
danielk1977238746a2009-03-19 18:51:06 +0000111 sqlite3 *db = v->db;
drh413c3d32010-02-23 20:11:56 +0000112 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
drh4245c402012-06-02 14:32:21 +0000113 sqlite3_mutex_enter(db->mutex);
drh201e0c62015-07-14 14:48:50 +0000114 checkProfileCallback(db, v);
drhe30f4422007-08-21 16:15:55 +0000115 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +0000116 rc = sqlite3ApiExit(db, rc);
drh4245c402012-06-02 14:32:21 +0000117 sqlite3LeaveMutexAndCloseZombie(db);
drhe30f4422007-08-21 16:15:55 +0000118 }
119 return rc;
120}
121
122/*
123** Terminate the current execution of an SQL statement and reset it
124** back to its starting state so that it can be reused. A success code from
125** the prior execution is returned.
126**
127** This routine sets the error code and string returned by
128** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
129*/
130int sqlite3_reset(sqlite3_stmt *pStmt){
131 int rc;
132 if( pStmt==0 ){
133 rc = SQLITE_OK;
134 }else{
135 Vdbe *v = (Vdbe*)pStmt;
drh201e0c62015-07-14 14:48:50 +0000136 sqlite3 *db = v->db;
adame6c3c722009-11-03 22:34:35 +0000137#ifdef SQLITE_ENABLE_SQLRR
138 SRRecReset(pStmt);
139#endif
drh201e0c62015-07-14 14:48:50 +0000140 sqlite3_mutex_enter(db->mutex);
141 checkProfileCallback(db, v);
drhc890fec2008-08-01 20:10:08 +0000142 rc = sqlite3VdbeReset(v);
drh124c0b42011-06-01 18:15:55 +0000143 sqlite3VdbeRewind(v);
drh201e0c62015-07-14 14:48:50 +0000144 assert( (rc & (db->errMask))==rc );
145 rc = sqlite3ApiExit(db, rc);
146 sqlite3_mutex_leave(db->mutex);
drhe30f4422007-08-21 16:15:55 +0000147 }
148 return rc;
149}
150
151/*
152** Set all the parameters in the compiled SQL statement to NULL.
153*/
154int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
155 int i;
156 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +0000157 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +0000158#if SQLITE_THREADSAFE
adame6c3c722009-11-03 22:34:35 +0000159 sqlite3_mutex *mutex=NULL;
160#endif
161 if( NULL==pStmt ){ return SQLITE_OK; } /* <rdar://problem/6646331> */
162#ifdef SQLITE_ENABLE_SQLRR
163 SRRecClearBindings(pStmt);
164#endif
165#if SQLITE_THREADSAFE
166 mutex = ((Vdbe*)pStmt)->db->mutex;
drh7e8b8482008-01-23 03:03:05 +0000167#endif
168 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +0000169 for(i=0; i<p->nVar; i++){
170 sqlite3VdbeMemRelease(&p->aVar[i]);
171 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000172 }
drh2c2f3922017-06-01 00:54:35 +0000173 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
danbda4cb82017-02-23 16:30:16 +0000174 if( p->expmask ){
danc94b8592009-10-20 07:01:24 +0000175 p->expired = 1;
176 }
drh7e8b8482008-01-23 03:03:05 +0000177 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000178 return rc;
179}
180
181
drh4f26d6c2004-05-26 23:25:30 +0000182/**************************** sqlite3_value_ *******************************
183** The following routines extract information from a Mem or sqlite3_value
184** structure.
185*/
186const void *sqlite3_value_blob(sqlite3_value *pVal){
187 Mem *p = (Mem*)pVal;
188 if( p->flags & (MEM_Blob|MEM_Str) ){
drhff535a22016-09-20 01:46:15 +0000189 if( ExpandBlob(p)!=SQLITE_OK ){
dana4d5ae82015-07-24 16:24:37 +0000190 assert( p->flags==MEM_Null && p->z==0 );
191 return 0;
192 }
drh1f0feef2007-05-15 13:27:07 +0000193 p->flags |= MEM_Blob;
drh42262532010-09-08 16:30:36 +0000194 return p->n ? p->z : 0;
drh4f26d6c2004-05-26 23:25:30 +0000195 }else{
196 return sqlite3_value_text(pVal);
197 }
198}
199int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000200 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000201}
202int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000203 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000204}
205double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000206 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000207}
208int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000209 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000210}
drhefad9992004-06-22 12:13:55 +0000211sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000212 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000213}
drhbcdf78a2015-09-10 20:34:56 +0000214unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
dan5b6c8e42016-01-30 15:46:03 +0000215 Mem *pMem = (Mem*)pVal;
216 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
drhbcdf78a2015-09-10 20:34:56 +0000217}
drhae3ec3f2017-07-17 00:40:19 +0000218void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
drh3a96a5d2017-06-30 23:09:03 +0000219 Mem *p = (Mem*)pVal;
drha0024e62017-07-27 15:53:24 +0000220 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
221 (MEM_Null|MEM_Term|MEM_Subtype)
drhae3ec3f2017-07-17 00:40:19 +0000222 && zPType!=0
drh06d49402017-07-17 17:46:29 +0000223 && p->eSubtype=='p'
drh22930062017-07-27 03:48:02 +0000224 && strcmp(p->u.zPType, zPType)==0
drhae3ec3f2017-07-17 00:40:19 +0000225 ){
drh22930062017-07-27 03:48:02 +0000226 return (void*)p->z;
drh3a96a5d2017-06-30 23:09:03 +0000227 }else{
228 return 0;
229 }
230}
drh4f26d6c2004-05-26 23:25:30 +0000231const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000232 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000233}
drh6c626082004-11-14 21:56:29 +0000234#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000235const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000236 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000237}
danielk1977d8123362004-06-12 09:25:12 +0000238const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000239 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000240}
241const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000242 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000243}
drh6c626082004-11-14 21:56:29 +0000244#endif /* SQLITE_OMIT_UTF16 */
drh22ec1342015-02-27 00:33:15 +0000245/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
246** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
247** point number string BLOB NULL
248*/
drh4f26d6c2004-05-26 23:25:30 +0000249int sqlite3_value_type(sqlite3_value* pVal){
drh1b27b8c2014-02-10 03:21:57 +0000250 static const u8 aType[] = {
251 SQLITE_BLOB, /* 0x00 */
252 SQLITE_NULL, /* 0x01 */
253 SQLITE_TEXT, /* 0x02 */
254 SQLITE_NULL, /* 0x03 */
255 SQLITE_INTEGER, /* 0x04 */
256 SQLITE_NULL, /* 0x05 */
257 SQLITE_INTEGER, /* 0x06 */
258 SQLITE_NULL, /* 0x07 */
259 SQLITE_FLOAT, /* 0x08 */
260 SQLITE_NULL, /* 0x09 */
261 SQLITE_FLOAT, /* 0x0a */
262 SQLITE_NULL, /* 0x0b */
263 SQLITE_INTEGER, /* 0x0c */
264 SQLITE_NULL, /* 0x0d */
265 SQLITE_INTEGER, /* 0x0e */
266 SQLITE_NULL, /* 0x0f */
267 SQLITE_BLOB, /* 0x10 */
268 SQLITE_NULL, /* 0x11 */
269 SQLITE_TEXT, /* 0x12 */
270 SQLITE_NULL, /* 0x13 */
271 SQLITE_INTEGER, /* 0x14 */
272 SQLITE_NULL, /* 0x15 */
273 SQLITE_INTEGER, /* 0x16 */
274 SQLITE_NULL, /* 0x17 */
275 SQLITE_FLOAT, /* 0x18 */
276 SQLITE_NULL, /* 0x19 */
277 SQLITE_FLOAT, /* 0x1a */
278 SQLITE_NULL, /* 0x1b */
279 SQLITE_INTEGER, /* 0x1c */
280 SQLITE_NULL, /* 0x1d */
281 SQLITE_INTEGER, /* 0x1e */
282 SQLITE_NULL, /* 0x1f */
283 };
mistachkinafc14f72014-03-05 01:29:18 +0000284 return aType[pVal->flags&MEM_AffMask];
drh4f26d6c2004-05-26 23:25:30 +0000285}
286
drhce2fbd12018-01-12 21:00:14 +0000287/* Return true if a parameter to xUpdate represents an unchanged column */
288int sqlite3_value_nochange(sqlite3_value *pVal){
289 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
290}
291
drh4f03f412015-05-20 21:28:32 +0000292/* Make a copy of an sqlite3_value object
293*/
294sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
295 sqlite3_value *pNew;
296 if( pOrig==0 ) return 0;
297 pNew = sqlite3_malloc( sizeof(*pNew) );
298 if( pNew==0 ) return 0;
299 memset(pNew, 0, sizeof(*pNew));
300 memcpy(pNew, pOrig, MEMCELLSIZE);
301 pNew->flags &= ~MEM_Dyn;
302 pNew->db = 0;
303 if( pNew->flags&(MEM_Str|MEM_Blob) ){
drh10ca5b42015-05-22 21:04:54 +0000304 pNew->flags &= ~(MEM_Static|MEM_Dyn);
305 pNew->flags |= MEM_Ephem;
306 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
307 sqlite3ValueFree(pNew);
308 pNew = 0;
drh4f03f412015-05-20 21:28:32 +0000309 }
310 }
311 return pNew;
312}
313
314/* Destroy an sqlite3_value object previously obtained from
315** sqlite3_value_dup().
316*/
317void sqlite3_value_free(sqlite3_value *pOld){
318 sqlite3ValueFree(pOld);
319}
320
321
drh4f26d6c2004-05-26 23:25:30 +0000322/**************************** sqlite3_result_ *******************************
323** The following routines are used by user-defined functions to specify
324** the function result.
drh4c8555f2009-06-25 01:47:11 +0000325**
peter.d.reid60ec9142014-09-06 16:39:46 +0000326** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
drh4c8555f2009-06-25 01:47:11 +0000327** result as a string or blob but if the string or blob is too large, it
328** then sets the error code to SQLITE_TOOBIG
drhda4ca9d2014-09-09 17:27:35 +0000329**
330** The invokeValueDestructor(P,X) routine invokes destructor function X()
331** on value P is not going to be used and need to be destroyed.
drh4f26d6c2004-05-26 23:25:30 +0000332*/
drh4c8555f2009-06-25 01:47:11 +0000333static void setResultStrOrError(
334 sqlite3_context *pCtx, /* Function context */
335 const char *z, /* String pointer */
336 int n, /* Bytes in string, or negative */
337 u8 enc, /* Encoding of z. 0 for BLOBs */
338 void (*xDel)(void*) /* Destructor function */
339){
drh9bd038f2014-08-27 14:14:06 +0000340 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
drh4c8555f2009-06-25 01:47:11 +0000341 sqlite3_result_error_toobig(pCtx);
342 }
343}
drhda4ca9d2014-09-09 17:27:35 +0000344static int invokeValueDestructor(
345 const void *p, /* Value to destroy */
346 void (*xDel)(void*), /* The destructor */
347 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
348){
drhfc59a952014-09-11 23:34:55 +0000349 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000350 if( xDel==0 ){
351 /* noop */
352 }else if( xDel==SQLITE_TRANSIENT ){
353 /* noop */
drhda4ca9d2014-09-09 17:27:35 +0000354 }else{
355 xDel((void*)p);
356 }
357 if( pCtx ) sqlite3_result_error_toobig(pCtx);
358 return SQLITE_TOOBIG;
359}
drh4f26d6c2004-05-26 23:25:30 +0000360void sqlite3_result_blob(
361 sqlite3_context *pCtx,
362 const void *z,
363 int n,
danielk1977d8123362004-06-12 09:25:12 +0000364 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000365){
drh5708d2d2005-06-22 10:53:59 +0000366 assert( n>=0 );
drh9bd038f2014-08-27 14:14:06 +0000367 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000368 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000369}
drhda4ca9d2014-09-09 17:27:35 +0000370void sqlite3_result_blob64(
371 sqlite3_context *pCtx,
372 const void *z,
373 sqlite3_uint64 n,
374 void (*xDel)(void *)
375){
376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000377 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000378 if( n>0x7fffffff ){
379 (void)invokeValueDestructor(z, xDel, pCtx);
380 }else{
381 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
382 }
383}
drh4f26d6c2004-05-26 23:25:30 +0000384void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh9bd038f2014-08-27 14:14:06 +0000385 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
386 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
drh4f26d6c2004-05-26 23:25:30 +0000387}
388void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000389 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000390 pCtx->isError = SQLITE_ERROR;
drh9bd038f2014-08-27 14:14:06 +0000391 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000392}
danielk1977a1686c92006-01-23 07:52:37 +0000393#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000394void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000395 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000396 pCtx->isError = SQLITE_ERROR;
drh9bd038f2014-08-27 14:14:06 +0000397 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000398}
danielk1977a1686c92006-01-23 07:52:37 +0000399#endif
drhf4479502004-05-27 03:12:53 +0000400void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh9bd038f2014-08-27 14:14:06 +0000401 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
402 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
drh4f26d6c2004-05-26 23:25:30 +0000403}
404void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh9bd038f2014-08-27 14:14:06 +0000405 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
406 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
drh4f26d6c2004-05-26 23:25:30 +0000407}
408void sqlite3_result_null(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000409 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
410 sqlite3VdbeMemSetNull(pCtx->pOut);
drh4f26d6c2004-05-26 23:25:30 +0000411}
drh22930062017-07-27 03:48:02 +0000412void sqlite3_result_pointer(
413 sqlite3_context *pCtx,
414 void *pPtr,
415 const char *zPType,
416 void (*xDestructor)(void*)
417){
drh3a96a5d2017-06-30 23:09:03 +0000418 Mem *pOut = pCtx->pOut;
419 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh526740b2017-08-24 13:55:46 +0000420 sqlite3VdbeMemRelease(pOut);
421 pOut->flags = MEM_Null;
drh22930062017-07-27 03:48:02 +0000422 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
drh3a96a5d2017-06-30 23:09:03 +0000423}
drhbcdf78a2015-09-10 20:34:56 +0000424void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
dan5b6c8e42016-01-30 15:46:03 +0000425 Mem *pOut = pCtx->pOut;
426 assert( sqlite3_mutex_held(pOut->db->mutex) );
427 pOut->eSubtype = eSubtype & 0xff;
428 pOut->flags |= MEM_Subtype;
drhbcdf78a2015-09-10 20:34:56 +0000429}
drh4f26d6c2004-05-26 23:25:30 +0000430void sqlite3_result_text(
431 sqlite3_context *pCtx,
432 const char *z,
433 int n,
danielk1977d8123362004-06-12 09:25:12 +0000434 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000435){
drh9bd038f2014-08-27 14:14:06 +0000436 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000437 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000438}
drhbbf483f2014-09-09 20:30:24 +0000439void sqlite3_result_text64(
drhda4ca9d2014-09-09 17:27:35 +0000440 sqlite3_context *pCtx,
441 const char *z,
442 sqlite3_uint64 n,
443 void (*xDel)(void *),
444 unsigned char enc
445){
446 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000447 assert( xDel!=SQLITE_DYNAMIC );
drh79f7af92014-10-03 16:00:51 +0000448 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drhda4ca9d2014-09-09 17:27:35 +0000449 if( n>0x7fffffff ){
450 (void)invokeValueDestructor(z, xDel, pCtx);
451 }else{
452 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
453 }
454}
drh6c626082004-11-14 21:56:29 +0000455#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000456void sqlite3_result_text16(
457 sqlite3_context *pCtx,
458 const void *z,
459 int n,
danielk1977d8123362004-06-12 09:25:12 +0000460 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000461){
drh9bd038f2014-08-27 14:14:06 +0000462 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000463 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000464}
465void sqlite3_result_text16be(
466 sqlite3_context *pCtx,
467 const void *z,
468 int n,
469 void (*xDel)(void *)
470){
drh9bd038f2014-08-27 14:14:06 +0000471 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000472 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000473}
474void sqlite3_result_text16le(
475 sqlite3_context *pCtx,
476 const void *z,
477 int n,
478 void (*xDel)(void *)
479){
drh9bd038f2014-08-27 14:14:06 +0000480 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000481 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000482}
drh6c626082004-11-14 21:56:29 +0000483#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000484void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh9bd038f2014-08-27 14:14:06 +0000485 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
486 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000487}
drhb026e052007-05-02 01:34:31 +0000488void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh9bd038f2014-08-27 14:14:06 +0000489 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
490 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
drhb026e052007-05-02 01:34:31 +0000491}
dana4d5ae82015-07-24 16:24:37 +0000492int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
493 Mem *pOut = pCtx->pOut;
494 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh6bacdc22015-07-24 17:14:03 +0000495 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
dana4d5ae82015-07-24 16:24:37 +0000496 return SQLITE_TOOBIG;
497 }
498 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
499 return SQLITE_OK;
500}
drh69544ec2008-02-06 14:11:34 +0000501void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
drhf09ac0b2018-01-23 03:44:06 +0000502 pCtx->isError = errCode ? errCode : -1;
drh983b5ee2015-02-13 12:05:56 +0000503#ifdef SQLITE_DEBUG
drh96f4ad22015-03-12 21:02:36 +0000504 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
drh983b5ee2015-02-13 12:05:56 +0000505#endif
drh9bd038f2014-08-27 14:14:06 +0000506 if( pCtx->pOut->flags & MEM_Null ){
507 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
drh51c7d862009-04-27 18:46:06 +0000508 SQLITE_UTF8, SQLITE_STATIC);
509 }
drh69544ec2008-02-06 14:11:34 +0000510}
drh4f26d6c2004-05-26 23:25:30 +0000511
drha0206bc2007-05-08 15:15:02 +0000512/* Force an SQLITE_TOOBIG error. */
513void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000514 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000515 pCtx->isError = SQLITE_TOOBIG;
drh9bd038f2014-08-27 14:14:06 +0000516 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
drhbb4957f2008-03-20 14:03:29 +0000517 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000518}
519
danielk1977a1644fd2007-08-29 12:31:25 +0000520/* An SQLITE_NOMEM error. */
521void sqlite3_result_error_nomem(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000522 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
523 sqlite3VdbeMemSetNull(pCtx->pOut);
mistachkinfad30392016-02-13 23:43:46 +0000524 pCtx->isError = SQLITE_NOMEM_BKPT;
drh4a642b62016-02-05 01:55:27 +0000525 sqlite3OomFault(pCtx->pOut->db);
danielk1977a1644fd2007-08-29 12:31:25 +0000526}
drh4f26d6c2004-05-26 23:25:30 +0000527
528/*
dan5cf53532010-05-01 16:40:20 +0000529** This function is called after a transaction has been committed. It
530** invokes callbacks registered with sqlite3_wal_hook() as required.
531*/
drh7ed91f22010-04-29 22:34:07 +0000532static int doWalCallbacks(sqlite3 *db){
dan8d22a172010-04-19 18:03:51 +0000533 int rc = SQLITE_OK;
dan5cf53532010-05-01 16:40:20 +0000534#ifndef SQLITE_OMIT_WAL
535 int i;
dan8d22a172010-04-19 18:03:51 +0000536 for(i=0; i<db->nDb; i++){
537 Btree *pBt = db->aDb[i].pBt;
538 if( pBt ){
drhef15c6e2014-12-12 01:27:17 +0000539 int nEntry;
540 sqlite3BtreeEnter(pBt);
541 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
542 sqlite3BtreeLeave(pBt);
drh59533aa2017-08-02 18:28:26 +0000543 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
drh69c33822016-08-18 14:33:11 +0000544 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
dan8d22a172010-04-19 18:03:51 +0000545 }
546 }
547 }
dan5cf53532010-05-01 16:40:20 +0000548#endif
dan8d22a172010-04-19 18:03:51 +0000549 return rc;
550}
551
drh201e0c62015-07-14 14:48:50 +0000552
drh4f26d6c2004-05-26 23:25:30 +0000553/*
554** Execute the statement pStmt, either until a row of data is ready, the
555** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000556**
557** This routine implements the bulk of the logic behind the sqlite_step()
558** API. The only thing omitted is the automatic recompile if a
559** schema change has occurred. That detail is handled by the
560** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000561*/
drhb900aaf2006-11-09 00:24:53 +0000562static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000563 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000564 int rc;
565
danielk1977a185ddb2007-11-15 16:04:15 +0000566 assert(p);
567 if( p->magic!=VDBE_MAGIC_RUN ){
drh3674bfd2010-04-17 12:53:19 +0000568 /* We used to require that sqlite3_reset() be called before retrying
drh602acb42011-01-17 17:42:37 +0000569 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
570 ** with version 3.7.0, we changed this so that sqlite3_reset() would
571 ** be called automatically instead of throwing the SQLITE_MISUSE error.
572 ** This "automatic-reset" change is not technically an incompatibility,
573 ** since any application that receives an SQLITE_MISUSE is broken by
574 ** definition.
575 **
576 ** Nevertheless, some published applications that were originally written
577 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
drhb8a45bb2011-12-31 21:51:55 +0000578 ** returns, and those were broken by the automatic-reset change. As a
drh602acb42011-01-17 17:42:37 +0000579 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
580 ** legacy behavior of returning SQLITE_MISUSE for cases where the
581 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
582 ** or SQLITE_BUSY error.
drh3674bfd2010-04-17 12:53:19 +0000583 */
drh602acb42011-01-17 17:42:37 +0000584#ifdef SQLITE_OMIT_AUTORESET
drh983b5ee2015-02-13 12:05:56 +0000585 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
drh602acb42011-01-17 17:42:37 +0000586 sqlite3_reset((sqlite3_stmt*)p);
587 }else{
588 return SQLITE_MISUSE_BKPT;
589 }
590#else
drh3674bfd2010-04-17 12:53:19 +0000591 sqlite3_reset((sqlite3_stmt*)p);
drh602acb42011-01-17 17:42:37 +0000592#endif
drhf1bfe9a2007-10-17 01:44:20 +0000593 }
594
dan14d4cc42010-01-20 14:25:37 +0000595 /* Check that malloc() has not failed. If it has, return early. */
drh17435752007-08-16 04:30:38 +0000596 db = p->db;
drhc456e572008-08-11 18:44:58 +0000597 if( db->mallocFailed ){
dan14d4cc42010-01-20 14:25:37 +0000598 p->rc = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46 +0000599 return SQLITE_NOMEM_BKPT;
drhc456e572008-08-11 18:44:58 +0000600 }
danielk1977261919c2005-12-06 12:52:59 +0000601
danielk1977a21c6b62005-01-24 10:25:59 +0000602 if( p->pc<=0 && p->expired ){
drhbee80652010-02-25 21:27:58 +0000603 p->rc = SQLITE_SCHEMA;
drhb900aaf2006-11-09 00:24:53 +0000604 rc = SQLITE_ERROR;
605 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000606 }
danielk19771d850a72004-05-31 08:26:49 +0000607 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000608 /* If there are no other statements currently running, then
609 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
610 ** from interrupting a statement that has not yet started.
611 */
drh4f7d3a52013-06-27 23:54:02 +0000612 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000613 db->u1.isInterrupted = 0;
614 }
615
drh648e2642013-07-11 15:03:32 +0000616 assert( db->nVdbeWrite>0 || db->autoCommit==0
617 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
618 );
dan1da40a32009-09-19 17:00:31 +0000619
drh19e2d372005-08-29 23:00:03 +0000620#ifndef SQLITE_OMIT_TRACE
drh3d2a5292016-07-13 22:55:01 +0000621 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
622 && !db->init.busy && p->zSql ){
drhb7e8ea22010-05-03 14:32:30 +0000623 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
drh201e0c62015-07-14 14:48:50 +0000624 }else{
625 assert( p->startTime==0 );
drh19e2d372005-08-29 23:00:03 +0000626 }
627#endif
drhc16a03b2004-09-15 13:38:10 +0000628
drh4f7d3a52013-06-27 23:54:02 +0000629 db->nVdbeActive++;
630 if( p->readOnly==0 ) db->nVdbeWrite++;
drh1713afb2013-06-28 01:24:57 +0000631 if( p->bIsReader ) db->nVdbeRead++;
danielk19771d850a72004-05-31 08:26:49 +0000632 p->pc = 0;
633 }
drh983b5ee2015-02-13 12:05:56 +0000634#ifdef SQLITE_DEBUG
635 p->rcApp = SQLITE_OK;
636#endif
drhb7f91642004-10-31 02:22:47 +0000637#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000638 if( p->explain ){
639 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000640 }else
641#endif /* SQLITE_OMIT_EXPLAIN */
642 {
drh4f7d3a52013-06-27 23:54:02 +0000643 db->nVdbeExec++;
drh4f26d6c2004-05-26 23:25:30 +0000644 rc = sqlite3VdbeExec(p);
drh4f7d3a52013-06-27 23:54:02 +0000645 db->nVdbeExec--;
drh4f26d6c2004-05-26 23:25:30 +0000646 }
647
drh19e2d372005-08-29 23:00:03 +0000648#ifndef SQLITE_OMIT_TRACE
drh201e0c62015-07-14 14:48:50 +0000649 /* If the statement completed successfully, invoke the profile callback */
650 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
drh19e2d372005-08-29 23:00:03 +0000651#endif
652
drh59533aa2017-08-02 18:28:26 +0000653 if( rc==SQLITE_DONE && db->autoCommit ){
dan8d22a172010-04-19 18:03:51 +0000654 assert( p->rc==SQLITE_OK );
drh7ed91f22010-04-29 22:34:07 +0000655 p->rc = doWalCallbacks(db);
dan8d22a172010-04-19 18:03:51 +0000656 if( p->rc!=SQLITE_OK ){
657 rc = SQLITE_ERROR;
658 }
659 }
660
drh80cc85b2008-07-23 21:07:25 +0000661 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000662 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
mistachkinfad30392016-02-13 23:43:46 +0000663 p->rc = SQLITE_NOMEM_BKPT;
drhb900aaf2006-11-09 00:24:53 +0000664 }
danielk1977357864e2009-03-25 15:43:08 +0000665end_of_step:
666 /* At this point local variable rc holds the value that should be
667 ** returned if this statement was compiled using the legacy
668 ** sqlite3_prepare() interface. According to the docs, this can only
669 ** be one of the values in the first assert() below. Variable p->rc
670 ** contains the value that would be returned if sqlite3_finalize()
671 ** were called on statement p.
672 */
673 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
drhcbd8db32015-08-20 17:18:32 +0000674 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
danielk1977357864e2009-03-25 15:43:08 +0000675 );
drh983b5ee2015-02-13 12:05:56 +0000676 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
drh2c2f3922017-06-01 00:54:35 +0000677 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
678 && rc!=SQLITE_ROW
679 && rc!=SQLITE_DONE
680 ){
681 /* If this statement was prepared using saved SQL and an
mistachkin48864df2013-03-21 21:20:32 +0000682 ** error has occurred, then return the error code in p->rc to the
danielk1977357864e2009-03-25 15:43:08 +0000683 ** caller. Set the error code in the database handle to the same value.
684 */
dan029ead62011-10-27 15:19:58 +0000685 rc = sqlite3VdbeTransferError(p);
danielk1977357864e2009-03-25 15:43:08 +0000686 }
687 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000688}
689
690/*
691** This is the top-level implementation of sqlite3_step(). Call
692** sqlite3Step() to do most of the work. If a schema error occurs,
693** call sqlite3Reprepare() and try again.
694*/
drhb900aaf2006-11-09 00:24:53 +0000695int sqlite3_step(sqlite3_stmt *pStmt){
drha6129fa2010-02-24 17:15:19 +0000696 int rc = SQLITE_OK; /* Result from sqlite3Step() */
drha6129fa2010-02-24 17:15:19 +0000697 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
698 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
699 sqlite3 *db; /* The database connection */
700
drh413c3d32010-02-23 20:11:56 +0000701 if( vdbeSafetyNotNull(v) ){
702 return SQLITE_MISUSE_BKPT;
danielk19778e556522007-11-13 10:30:24 +0000703 }
drh413c3d32010-02-23 20:11:56 +0000704 db = v->db;
705 sqlite3_mutex_enter(db->mutex);
drh37f58e92012-09-04 21:34:26 +0000706 v->doingRerun = 0;
drh413c3d32010-02-23 20:11:56 +0000707 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
drh2c7946a2014-08-19 20:27:40 +0000708 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
709 int savedPc = v->pc;
drh754ee282017-08-02 19:04:37 +0000710 rc = sqlite3Reprepare(v);
711 if( rc!=SQLITE_OK ){
712 /* This case occurs after failing to recompile an sql statement.
713 ** The error message from the SQL compiler has already been loaded
714 ** into the database handle. This block copies the error message
715 ** from the database handle into the statement and sets the statement
716 ** program counter to 0 to ensure that when the statement is
717 ** finalized or reset the parser error message is available via
718 ** sqlite3_errmsg() and sqlite3_errcode().
719 */
720 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
721 sqlite3DbFree(db, v->zErrMsg);
722 if( !db->mallocFailed ){
723 v->zErrMsg = sqlite3DbStrDup(db, zErr);
724 v->rc = rc = sqlite3ApiExit(db, rc);
725 } else {
726 v->zErrMsg = 0;
727 v->rc = rc = SQLITE_NOMEM_BKPT;
728 }
729 break;
730 }
drh413c3d32010-02-23 20:11:56 +0000731 sqlite3_reset(pStmt);
drh5f58ae72014-08-19 20:41:36 +0000732 if( savedPc>=0 ) v->doingRerun = 1;
dan08f5f4c2011-06-27 19:37:23 +0000733 assert( v->expired==0 );
drh413c3d32010-02-23 20:11:56 +0000734 }
drh413c3d32010-02-23 20:11:56 +0000735 sqlite3_mutex_leave(db->mutex);
danielk197776e8d1a2006-01-18 18:22:43 +0000736 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000737}
738
drh95a7b3e2013-09-16 12:57:19 +0000739
drh4f26d6c2004-05-26 23:25:30 +0000740/*
drheb2e1762004-05-27 01:53:56 +0000741** Extract the user data from a sqlite3_context structure and return a
742** pointer to it.
743*/
744void *sqlite3_user_data(sqlite3_context *p){
745 assert( p && p->pFunc );
746 return p->pFunc->pUserData;
747}
748
749/*
drhfa4a4b92008-03-19 21:45:51 +0000750** Extract the user data from a sqlite3_context structure and return a
751** pointer to it.
drh9f129f42010-08-31 15:27:32 +0000752**
753** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
754** returns a copy of the pointer to the database connection (the 1st
755** parameter) of the sqlite3_create_function() and
756** sqlite3_create_function16() routines that originally registered the
757** application defined function.
drhfa4a4b92008-03-19 21:45:51 +0000758*/
759sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
drha46a4a62015-09-08 21:12:53 +0000760 assert( p && p->pOut );
drh9bd038f2014-08-27 14:14:06 +0000761 return p->pOut->db;
drhfa4a4b92008-03-19 21:45:51 +0000762}
763
764/*
drh6f390be2018-01-11 17:04:26 +0000765** If this routine is invoked from within an xColumn method of a virtual
766** table, then it returns true if and only if the the call is during an
767** UPDATE operation and the value of the column will not be modified
768** by the UPDATE.
769**
770** If this routine is called from any context other than within the
771** xColumn method of a virtual table, then the return value is meaningless
772** and arbitrary.
773**
774** Virtual table implements might use this routine to optimize their
775** performance by substituting a NULL result, or some other light-weight
776** value, as a signal to the xUpdate routine that the column is unchanged.
777*/
778int sqlite3_vtab_nochange(sqlite3_context *p){
779 assert( p );
drhce2fbd12018-01-12 21:00:14 +0000780 return sqlite3_value_nochange(p->pOut);
drh6f390be2018-01-11 17:04:26 +0000781}
782
783/*
drha9e03b12015-03-12 06:46:52 +0000784** Return the current time for a statement. If the current time
785** is requested more than once within the same run of a single prepared
786** statement, the exact same time is returned for each invocation regardless
787** of the amount of time that elapses between invocations. In other words,
788** the time returned is always the time of the first call.
drh95a7b3e2013-09-16 12:57:19 +0000789*/
790sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
drh95a7b3e2013-09-16 12:57:19 +0000791 int rc;
drha9e03b12015-03-12 06:46:52 +0000792#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
drh3df79a92015-03-12 23:48:27 +0000793 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
drha9e03b12015-03-12 06:46:52 +0000794 assert( p->pVdbe!=0 );
795#else
drh3df79a92015-03-12 23:48:27 +0000796 sqlite3_int64 iTime = 0;
drha9e03b12015-03-12 06:46:52 +0000797 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
drha9e03b12015-03-12 06:46:52 +0000798#endif
drh3df79a92015-03-12 23:48:27 +0000799 if( *piTime==0 ){
drha9e03b12015-03-12 06:46:52 +0000800 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
801 if( rc ) *piTime = 0;
drh95a7b3e2013-09-16 12:57:19 +0000802 }
drha9e03b12015-03-12 06:46:52 +0000803 return *piTime;
drh95a7b3e2013-09-16 12:57:19 +0000804}
805
806/*
drh9de4a172014-08-23 18:17:19 +0000807** Create a new aggregate context for p and return a pointer to
808** its pMem->z element.
809*/
810static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
811 Mem *pMem = p->pMem;
812 assert( (pMem->flags & MEM_Agg)==0 );
813 if( nByte<=0 ){
drh0725cab2014-09-17 14:52:46 +0000814 sqlite3VdbeMemSetNull(pMem);
drh9de4a172014-08-23 18:17:19 +0000815 pMem->z = 0;
816 }else{
drh322f2852014-09-19 00:43:39 +0000817 sqlite3VdbeMemClearAndResize(pMem, nByte);
drh9de4a172014-08-23 18:17:19 +0000818 pMem->flags = MEM_Agg;
819 pMem->u.pDef = p->pFunc;
820 if( pMem->z ){
821 memset(pMem->z, 0, nByte);
822 }
823 }
824 return (void*)pMem->z;
825}
826
827/*
drheb2e1762004-05-27 01:53:56 +0000828** Allocate or return the aggregate context for a user function. A new
829** context is allocated on the first call. Subsequent calls return the
830** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000831*/
832void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drh2d801512016-01-14 22:19:58 +0000833 assert( p && p->pFunc && p->pFunc->xFinalize );
drh9bd038f2014-08-27 14:14:06 +0000834 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
drhd68eee02009-12-11 03:44:18 +0000835 testcase( nByte<0 );
drh9de4a172014-08-23 18:17:19 +0000836 if( (p->pMem->flags & MEM_Agg)==0 ){
837 return createAggContext(p, nByte);
838 }else{
839 return (void*)p->pMem->z;
drheb2e1762004-05-27 01:53:56 +0000840 }
drheb2e1762004-05-27 01:53:56 +0000841}
842
843/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000844** Return the auxiliary data pointer, if any, for the iArg'th argument to
danielk1977682f68b2004-06-05 10:22:17 +0000845** the user-function defined by pCtx.
drhf7fa4e72017-05-11 15:20:18 +0000846**
847** The left-most argument is 0.
848**
849** Undocumented behavior: If iArg is negative then access a cache of
850** auxiliary data pointers that is available to all functions within a
851** single prepared statement. The iArg values must match.
danielk1977682f68b2004-06-05 10:22:17 +0000852*/
853void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
dan0c547792013-07-18 17:12:08 +0000854 AuxData *pAuxData;
drhb21c8cd2007-08-21 19:33:56 +0000855
drh9bd038f2014-08-27 14:14:06 +0000856 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000857#if SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000858 if( pCtx->pVdbe==0 ) return 0;
drha9e03b12015-03-12 06:46:52 +0000859#else
860 assert( pCtx->pVdbe!=0 );
861#endif
drhe6941392017-05-10 19:42:52 +0000862 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000863 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
drhe6941392017-05-10 19:42:52 +0000864 return pAuxData->pAux;
865 }
danielk1977682f68b2004-06-05 10:22:17 +0000866 }
drhe6941392017-05-10 19:42:52 +0000867 return 0;
danielk1977682f68b2004-06-05 10:22:17 +0000868}
869
870/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000871** Set the auxiliary data pointer and delete function, for the iArg'th
danielk1977682f68b2004-06-05 10:22:17 +0000872** argument to the user-function defined by pCtx. Any previous value is
873** deleted by calling the delete function specified when it was set.
drhf7fa4e72017-05-11 15:20:18 +0000874**
875** The left-most argument is 0.
876**
877** Undocumented behavior: If iArg is negative then make the data available
878** to all functions within the current prepared statement using iArg as an
879** access code.
danielk1977682f68b2004-06-05 10:22:17 +0000880*/
881void sqlite3_set_auxdata(
882 sqlite3_context *pCtx,
883 int iArg,
884 void *pAux,
885 void (*xDelete)(void*)
886){
dan0c547792013-07-18 17:12:08 +0000887 AuxData *pAuxData;
888 Vdbe *pVdbe = pCtx->pVdbe;
danielk1977682f68b2004-06-05 10:22:17 +0000889
drh9bd038f2014-08-27 14:14:06 +0000890 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000891#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000892 if( pVdbe==0 ) goto failed;
drha9e03b12015-03-12 06:46:52 +0000893#else
894 assert( pVdbe!=0 );
895#endif
danielk1977682f68b2004-06-05 10:22:17 +0000896
drhe6941392017-05-10 19:42:52 +0000897 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000898 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
899 break;
900 }
dan0c547792013-07-18 17:12:08 +0000901 }
902 if( pAuxData==0 ){
903 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
904 if( !pAuxData ) goto failed;
drhe6941392017-05-10 19:42:52 +0000905 pAuxData->iAuxOp = pCtx->iOp;
906 pAuxData->iAuxArg = iArg;
907 pAuxData->pNextAux = pVdbe->pAuxData;
dan0c547792013-07-18 17:12:08 +0000908 pVdbe->pAuxData = pAuxData;
drhf09ac0b2018-01-23 03:44:06 +0000909 if( pCtx->isError==0 ) pCtx->isError = -1;
drhe6941392017-05-10 19:42:52 +0000910 }else if( pAuxData->xDeleteAux ){
911 pAuxData->xDeleteAux(pAuxData->pAux);
danielk1977682f68b2004-06-05 10:22:17 +0000912 }
dan0c547792013-07-18 17:12:08 +0000913
danielk1977682f68b2004-06-05 10:22:17 +0000914 pAuxData->pAux = pAux;
drhe6941392017-05-10 19:42:52 +0000915 pAuxData->xDeleteAux = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000916 return;
917
918failed:
919 if( xDelete ){
920 xDelete(pAux);
921 }
danielk1977682f68b2004-06-05 10:22:17 +0000922}
923
shaneeec556d2008-10-12 00:27:53 +0000924#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000925/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000926** Return the number of times the Step function of an aggregate has been
drheb2e1762004-05-27 01:53:56 +0000927** called.
928**
drhcf85a512006-02-09 18:35:29 +0000929** This function is deprecated. Do not use it for new code. It is
930** provide only to avoid breaking legacy code. New aggregate function
931** implementations should keep their own counts within their aggregate
932** context.
drheb2e1762004-05-27 01:53:56 +0000933*/
934int sqlite3_aggregate_count(sqlite3_context *p){
drh2d801512016-01-14 22:19:58 +0000935 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
drhabfcea22005-09-06 20:36:48 +0000936 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000937}
shaneeec556d2008-10-12 00:27:53 +0000938#endif
drheb2e1762004-05-27 01:53:56 +0000939
940/*
drh4f26d6c2004-05-26 23:25:30 +0000941** Return the number of columns in the result set for the statement pStmt.
942*/
943int sqlite3_column_count(sqlite3_stmt *pStmt){
944 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000945 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000946}
947
948/*
949** Return the number of values available from the current row of the
950** currently executing statement pStmt.
951*/
952int sqlite3_data_count(sqlite3_stmt *pStmt){
953 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000954 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000955 return pVm->nResColumn;
956}
957
drhb1a1c292014-03-05 12:47:55 +0000958/*
959** Return a pointer to static memory containing an SQL NULL value.
960*/
961static const Mem *columnNullValue(void){
962 /* Even though the Mem structure contains an element
963 ** of type i64, on certain architectures (x86) with certain compiler
964 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
965 ** instead of an 8-byte one. This all works fine, except that when
966 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
967 ** that a Mem structure is located on an 8-byte boundary. To prevent
968 ** these assert()s from failing, when building with SQLITE_DEBUG defined
969 ** using gcc, we force nullMem to be 8-byte aligned using the magical
970 ** __attribute__((aligned(8))) macro. */
971 static const Mem nullMem
972#if defined(SQLITE_DEBUG) && defined(__GNUC__)
973 __attribute__((aligned(8)))
974#endif
drh035e5632014-09-16 14:16:31 +0000975 = {
drh3329a632014-09-18 01:21:43 +0000976 /* .u = */ {0},
drh8faee872015-09-19 18:08:13 +0000977 /* .flags = */ (u16)MEM_Null,
978 /* .enc = */ (u8)0,
979 /* .eSubtype = */ (u8)0,
980 /* .n = */ (int)0,
981 /* .z = */ (char*)0,
982 /* .zMalloc = */ (char*)0,
983 /* .szMalloc = */ (int)0,
984 /* .uTemp = */ (u32)0,
985 /* .db = */ (sqlite3*)0,
986 /* .xDel = */ (void(*)(void*))0,
drhb1a1c292014-03-05 12:47:55 +0000987#ifdef SQLITE_DEBUG
drh8faee872015-09-19 18:08:13 +0000988 /* .pScopyFrom = */ (Mem*)0,
989 /* .pFiller = */ (void*)0,
drhb1a1c292014-03-05 12:47:55 +0000990#endif
drh035e5632014-09-16 14:16:31 +0000991 };
drhb1a1c292014-03-05 12:47:55 +0000992 return &nullMem;
993}
drh4f26d6c2004-05-26 23:25:30 +0000994
995/*
996** Check to see if column iCol of the given statement is valid. If
997** it is, return a pointer to the Mem for the value of that column.
998** If iCol is not valid, return a pointer to a Mem which has a value
999** of NULL.
1000*/
1001static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +00001002 Vdbe *pVm;
drh32bc3f62007-08-21 20:25:39 +00001003 Mem *pOut;
1004
1005 pVm = (Vdbe *)pStmt;
drh28f17012016-09-22 21:37:18 +00001006 if( pVm==0 ) return (Mem*)columnNullValue();
1007 assert( pVm->db );
1008 sqlite3_mutex_enter(pVm->db->mutex);
1009 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drhd4e70eb2008-01-02 00:34:36 +00001010 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +00001011 }else{
drh28f17012016-09-22 21:37:18 +00001012 sqlite3Error(pVm->db, SQLITE_RANGE);
drhb1a1c292014-03-05 12:47:55 +00001013 pOut = (Mem*)columnNullValue();
drh4f26d6c2004-05-26 23:25:30 +00001014 }
drh32bc3f62007-08-21 20:25:39 +00001015 return pOut;
drh4f26d6c2004-05-26 23:25:30 +00001016}
1017
danielk19772e588c72005-12-09 14:25:08 +00001018/*
1019** This function is called after invoking an sqlite3_value_XXX function on a
1020** column value (i.e. a value returned by evaluating an SQL expression in the
1021** select list of a SELECT statement) that may cause a malloc() failure. If
1022** malloc() has failed, the threads mallocFailed flag is cleared and the result
1023** code of statement pStmt set to SQLITE_NOMEM.
1024**
drh32bc3f62007-08-21 20:25:39 +00001025** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +00001026**
1027** sqlite3_column_int()
1028** sqlite3_column_int64()
1029** sqlite3_column_text()
1030** sqlite3_column_text16()
1031** sqlite3_column_real()
1032** sqlite3_column_bytes()
1033** sqlite3_column_bytes16()
drh42262532010-09-08 16:30:36 +00001034** sqiite3_column_blob()
danielk19772e588c72005-12-09 14:25:08 +00001035*/
1036static void columnMallocFailure(sqlite3_stmt *pStmt)
1037{
1038 /* If malloc() failed during an encoding conversion within an
1039 ** sqlite3_column_XXX API, then set the return code of the statement to
1040 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
1041 ** and _finalize() will return NOMEM.
1042 */
danielk197754f01982006-01-18 15:25:17 +00001043 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +00001044 if( p ){
drh28f17012016-09-22 21:37:18 +00001045 assert( p->db!=0 );
1046 assert( sqlite3_mutex_held(p->db->mutex) );
drh32bc3f62007-08-21 20:25:39 +00001047 p->rc = sqlite3ApiExit(p->db, p->rc);
1048 sqlite3_mutex_leave(p->db->mutex);
1049 }
danielk19772e588c72005-12-09 14:25:08 +00001050}
1051
drh4f26d6c2004-05-26 23:25:30 +00001052/**************************** sqlite3_column_ *******************************
1053** The following routines are used to access elements of the current row
1054** in the result set.
1055*/
danielk1977c572ef72004-05-27 09:28:41 +00001056const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001057 const void *val;
danielk19772e588c72005-12-09 14:25:08 +00001058 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +00001059 /* Even though there is no encoding conversion, value_blob() might
1060 ** need to call malloc() to expand the result of a zeroblob()
1061 ** expression.
1062 */
1063 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +00001064 return val;
danielk1977c572ef72004-05-27 09:28:41 +00001065}
drh4f26d6c2004-05-26 23:25:30 +00001066int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001067 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
1068 columnMallocFailure(pStmt);
1069 return val;
drh4f26d6c2004-05-26 23:25:30 +00001070}
1071int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001072 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
1073 columnMallocFailure(pStmt);
1074 return val;
drh4f26d6c2004-05-26 23:25:30 +00001075}
1076double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001077 double val = sqlite3_value_double( columnMem(pStmt,i) );
1078 columnMallocFailure(pStmt);
1079 return val;
drh4f26d6c2004-05-26 23:25:30 +00001080}
1081int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001082 int val = sqlite3_value_int( columnMem(pStmt,i) );
1083 columnMallocFailure(pStmt);
1084 return val;
drh4f26d6c2004-05-26 23:25:30 +00001085}
drhefad9992004-06-22 12:13:55 +00001086sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001087 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
1088 columnMallocFailure(pStmt);
1089 return val;
drh4f26d6c2004-05-26 23:25:30 +00001090}
1091const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001092 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
1093 columnMallocFailure(pStmt);
1094 return val;
drh4f26d6c2004-05-26 23:25:30 +00001095}
drhd1e47332005-06-26 17:55:33 +00001096sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +00001097 Mem *pOut = columnMem(pStmt, i);
1098 if( pOut->flags&MEM_Static ){
1099 pOut->flags &= ~MEM_Static;
1100 pOut->flags |= MEM_Ephem;
1101 }
drh32bc3f62007-08-21 20:25:39 +00001102 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +00001103 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +00001104}
drh6c626082004-11-14 21:56:29 +00001105#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001106const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001107 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
1108 columnMallocFailure(pStmt);
1109 return val;
drh4f26d6c2004-05-26 23:25:30 +00001110}
drh6c626082004-11-14 21:56:29 +00001111#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +00001112int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +00001113 int iType = sqlite3_value_type( columnMem(pStmt,i) );
1114 columnMallocFailure(pStmt);
1115 return iType;
drh4f26d6c2004-05-26 23:25:30 +00001116}
1117
drh5e2517e2004-09-24 23:59:12 +00001118/*
1119** Convert the N-th element of pStmt->pColName[] into a string using
1120** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +00001121**
1122** There are up to 5 names for each column. useType determines which
1123** name is returned. Here are the names:
1124**
1125** 0 The column name as it should be displayed for output
1126** 1 The datatype name for the column
1127** 2 The name of the database that the column derives from
1128** 3 The name of the table that the column derives from
1129** 4 The name of the table column that the result column derives from
1130**
1131** If the result is not a simple column reference (if it is an expression
1132** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +00001133*/
1134static const void *columnName(
1135 sqlite3_stmt *pStmt,
1136 int N,
1137 const void *(*xFunc)(Mem*),
1138 int useType
1139){
drh9ca95732014-10-24 00:35:58 +00001140 const void *ret;
1141 Vdbe *p;
drh32bc3f62007-08-21 20:25:39 +00001142 int n;
drh9ca95732014-10-24 00:35:58 +00001143 sqlite3 *db;
1144#ifdef SQLITE_ENABLE_API_ARMOR
1145 if( pStmt==0 ){
1146 (void)SQLITE_MISUSE_BKPT;
1147 return 0;
1148 }
1149#endif
1150 ret = 0;
1151 p = (Vdbe *)pStmt;
1152 db = p->db;
drh9b4ea4a2009-04-10 20:32:00 +00001153 assert( db!=0 );
1154 n = sqlite3_column_count(pStmt);
1155 if( N<n && N>=0 ){
1156 N += useType*n;
1157 sqlite3_mutex_enter(db->mutex);
1158 assert( db->mallocFailed==0 );
1159 ret = xFunc(&p->aColName[N]);
1160 /* A malloc may have failed inside of the xFunc() call. If this
1161 ** is the case, clear the mallocFailed flag and return NULL.
1162 */
1163 if( db->mallocFailed ){
drh4a642b62016-02-05 01:55:27 +00001164 sqlite3OomClear(db);
drh9b4ea4a2009-04-10 20:32:00 +00001165 ret = 0;
drh32bc3f62007-08-21 20:25:39 +00001166 }
drh9b4ea4a2009-04-10 20:32:00 +00001167 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +00001168 }
danielk197700fd9572005-12-07 06:27:43 +00001169 return ret;
drh5e2517e2004-09-24 23:59:12 +00001170}
1171
drh4f26d6c2004-05-26 23:25:30 +00001172/*
1173** Return the name of the Nth column of the result set returned by SQL
1174** statement pStmt.
1175*/
1176const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001177 return columnName(
1178 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +00001179}
drhe590fbd2005-05-20 19:36:01 +00001180#ifndef SQLITE_OMIT_UTF16
1181const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001182 return columnName(
1183 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +00001184}
1185#endif
drh4f26d6c2004-05-26 23:25:30 +00001186
1187/*
drh3f913572008-03-22 01:07:17 +00001188** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1189** not define OMIT_DECLTYPE.
1190*/
1191#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
1192# error "Must not define both SQLITE_OMIT_DECLTYPE \
1193 and SQLITE_ENABLE_COLUMN_METADATA"
1194#endif
1195
1196#ifndef SQLITE_OMIT_DECLTYPE
1197/*
drh6c626082004-11-14 21:56:29 +00001198** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +00001199** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +00001200*/
1201const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001202 return columnName(
1203 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +00001204}
drh6c626082004-11-14 21:56:29 +00001205#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +00001206const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001207 return columnName(
1208 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +00001209}
drh6c626082004-11-14 21:56:29 +00001210#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +00001211#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +00001212
danielk19774b1ae992006-02-10 03:06:10 +00001213#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +00001214/*
1215** Return the name of the database from which a result column derives.
1216** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001217** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001218*/
1219const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001220 return columnName(
1221 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001222}
1223#ifndef SQLITE_OMIT_UTF16
1224const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001225 return columnName(
1226 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001227}
1228#endif /* SQLITE_OMIT_UTF16 */
1229
1230/*
1231** Return the name of the table from which a result column derives.
1232** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001233** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001234*/
1235const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001236 return columnName(
1237 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001238}
1239#ifndef SQLITE_OMIT_UTF16
1240const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001241 return columnName(
1242 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001243}
1244#endif /* SQLITE_OMIT_UTF16 */
1245
1246/*
1247** Return the name of the table column from which a result column derives.
1248** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001249** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001250*/
1251const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001252 return columnName(
1253 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001254}
1255#ifndef SQLITE_OMIT_UTF16
1256const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001257 return columnName(
1258 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001259}
1260#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +00001261#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +00001262
1263
drh4f26d6c2004-05-26 23:25:30 +00001264/******************************* sqlite3_bind_ ***************************
1265**
1266** Routines used to attach values to wildcards in a compiled SQL statement.
1267*/
1268/*
1269** Unbind the value bound to variable i in virtual machine p. This is the
1270** the same as binding a NULL value to the column. If the "i" parameter is
1271** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1272**
drh488e7b62008-10-06 12:46:43 +00001273** A successful evaluation of this routine acquires the mutex on p.
1274** the mutex is released if any kind of error occurs.
1275**
drh4f26d6c2004-05-26 23:25:30 +00001276** The error code stored in database p->db is overwritten with the return
1277** value in any case.
1278*/
1279static int vdbeUnbind(Vdbe *p, int i){
1280 Mem *pVar;
drh413c3d32010-02-23 20:11:56 +00001281 if( vdbeSafetyNotNull(p) ){
1282 return SQLITE_MISUSE_BKPT;
1283 }
drh488e7b62008-10-06 12:46:43 +00001284 sqlite3_mutex_enter(p->db->mutex);
1285 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh13f40da2014-08-22 18:00:11 +00001286 sqlite3Error(p->db, SQLITE_MISUSE);
drh488e7b62008-10-06 12:46:43 +00001287 sqlite3_mutex_leave(p->db->mutex);
drh413c3d32010-02-23 20:11:56 +00001288 sqlite3_log(SQLITE_MISUSE,
1289 "bind on a busy prepared statement: [%s]", p->zSql);
1290 return SQLITE_MISUSE_BKPT;
drh4f26d6c2004-05-26 23:25:30 +00001291 }
1292 if( i<1 || i>p->nVar ){
drh13f40da2014-08-22 18:00:11 +00001293 sqlite3Error(p->db, SQLITE_RANGE);
drh488e7b62008-10-06 12:46:43 +00001294 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001295 return SQLITE_RANGE;
1296 }
1297 i--;
drh290c1942004-08-21 17:54:45 +00001298 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001299 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001300 pVar->flags = MEM_Null;
drh13f40da2014-08-22 18:00:11 +00001301 sqlite3Error(p->db, SQLITE_OK);
dan937d0de2009-10-15 18:35:38 +00001302
dan1d2ce4f2009-10-19 18:11:09 +00001303 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1304 ** binding a new value to this variable invalidates the current query plan.
drha7044002010-09-14 18:22:59 +00001305 **
1306 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1307 ** parameter in the WHERE clause might influence the choice of query plan
1308 ** for a statement, then the statement will be automatically recompiled,
1309 ** as if there had been a schema change, on the first sqlite3_step() call
1310 ** following any change to the bindings of that parameter.
dan1d2ce4f2009-10-19 18:11:09 +00001311 */
drh2c2f3922017-06-01 00:54:35 +00001312 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
drh29967962017-03-03 21:51:40 +00001313 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
dan937d0de2009-10-15 18:35:38 +00001314 p->expired = 1;
1315 }
drh4f26d6c2004-05-26 23:25:30 +00001316 return SQLITE_OK;
1317}
1318
1319/*
drh5e2517e2004-09-24 23:59:12 +00001320** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001321*/
drh5e2517e2004-09-24 23:59:12 +00001322static int bindText(
drh605264d2007-08-21 15:13:19 +00001323 sqlite3_stmt *pStmt, /* The statement to bind against */
1324 int i, /* Index of the parameter to bind */
1325 const void *zData, /* Pointer to the data to be bound */
1326 int nData, /* Number of bytes of data to be bound */
1327 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +00001328 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001329){
1330 Vdbe *p = (Vdbe *)pStmt;
1331 Mem *pVar;
1332 int rc;
1333
1334 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001335 if( rc==SQLITE_OK ){
1336 if( zData!=0 ){
1337 pVar = &p->aVar[i-1];
1338 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1339 if( rc==SQLITE_OK && encoding!=0 ){
1340 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1341 }
drhe70d01f2017-05-29 22:44:18 +00001342 if( rc ){
1343 sqlite3Error(p->db, rc);
1344 rc = sqlite3ApiExit(p->db, rc);
1345 }
drh27641702007-08-22 02:56:42 +00001346 }
drh488e7b62008-10-06 12:46:43 +00001347 sqlite3_mutex_leave(p->db->mutex);
drh94bb2ba2010-10-14 01:16:32 +00001348 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
drh6fec9ee2010-10-12 02:13:32 +00001349 xDel((void*)zData);
drh4f26d6c2004-05-26 23:25:30 +00001350 }
drh605264d2007-08-21 15:13:19 +00001351 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001352}
drh5e2517e2004-09-24 23:59:12 +00001353
1354
1355/*
1356** Bind a blob value to an SQL statement variable.
1357*/
1358int sqlite3_bind_blob(
1359 sqlite3_stmt *pStmt,
1360 int i,
1361 const void *zData,
1362 int nData,
1363 void (*xDel)(void*)
1364){
adame6c3c722009-11-03 22:34:35 +00001365#ifdef SQLITE_ENABLE_SQLRR
1366 SRRecBindBlob(pStmt, i, zData, nData);
1367#endif
drh5b081d82016-02-18 01:29:12 +00001368#ifdef SQLITE_ENABLE_API_ARMOR
1369 if( nData<0 ) return SQLITE_MISUSE_BKPT;
1370#endif
drh5e2517e2004-09-24 23:59:12 +00001371 return bindText(pStmt, i, zData, nData, xDel, 0);
1372}
drhda4ca9d2014-09-09 17:27:35 +00001373int sqlite3_bind_blob64(
1374 sqlite3_stmt *pStmt,
1375 int i,
1376 const void *zData,
1377 sqlite3_uint64 nData,
1378 void (*xDel)(void*)
1379){
drhfc59a952014-09-11 23:34:55 +00001380 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001381 if( nData>0x7fffffff ){
1382 return invokeValueDestructor(zData, xDel, 0);
1383 }else{
drh3329a632014-09-18 01:21:43 +00001384 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
drhda4ca9d2014-09-09 17:27:35 +00001385 }
1386}
drh4f26d6c2004-05-26 23:25:30 +00001387int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1388 int rc;
1389 Vdbe *p = (Vdbe *)pStmt;
adame6c3c722009-11-03 22:34:35 +00001390#ifdef SQLITE_ENABLE_SQLRR
1391 SRRecBindDouble(pStmt, i, rValue);
1392#endif
drh4f26d6c2004-05-26 23:25:30 +00001393 rc = vdbeUnbind(p, i);
1394 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001395 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +00001396 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001397 }
danielk1977f4618892004-06-28 13:09:11 +00001398 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001399}
1400int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
adame6c3c722009-11-03 22:34:35 +00001401#ifdef SQLITE_ENABLE_SQLRR
1402 SRRecBindInt64(p, i, (i64)iValue);
1403#endif
drhefad9992004-06-22 12:13:55 +00001404 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001405}
drhefad9992004-06-22 12:13:55 +00001406int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001407 int rc;
1408 Vdbe *p = (Vdbe *)pStmt;
adame6c3c722009-11-03 22:34:35 +00001409#ifdef SQLITE_ENABLE_SQLRR
1410 SRRecBindInt64(pStmt, i, iValue);
1411#endif
drh4f26d6c2004-05-26 23:25:30 +00001412 rc = vdbeUnbind(p, i);
1413 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001414 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +00001415 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001416 }
1417 return rc;
1418}
drh605264d2007-08-21 15:13:19 +00001419int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1420 int rc;
1421 Vdbe *p = (Vdbe*)pStmt;
adame6c3c722009-11-03 22:34:35 +00001422#ifdef SQLITE_ENABLE_SQLRR
1423 SRRecBindNull(pStmt, i);
1424#endif
drh605264d2007-08-21 15:13:19 +00001425 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001426 if( rc==SQLITE_OK ){
1427 sqlite3_mutex_leave(p->db->mutex);
1428 }
drh605264d2007-08-21 15:13:19 +00001429 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001430}
drh22930062017-07-27 03:48:02 +00001431int sqlite3_bind_pointer(
1432 sqlite3_stmt *pStmt,
1433 int i,
1434 void *pPtr,
1435 const char *zPTtype,
1436 void (*xDestructor)(void*)
1437){
drh3a96a5d2017-06-30 23:09:03 +00001438 int rc;
1439 Vdbe *p = (Vdbe*)pStmt;
1440 rc = vdbeUnbind(p, i);
1441 if( rc==SQLITE_OK ){
drh22930062017-07-27 03:48:02 +00001442 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
drh3a96a5d2017-06-30 23:09:03 +00001443 sqlite3_mutex_leave(p->db->mutex);
drh34fcf362017-07-27 16:42:36 +00001444 }else if( xDestructor ){
1445 xDestructor(pPtr);
drh3a96a5d2017-06-30 23:09:03 +00001446 }
1447 return rc;
1448}
drh4f26d6c2004-05-26 23:25:30 +00001449int sqlite3_bind_text(
1450 sqlite3_stmt *pStmt,
1451 int i,
1452 const char *zData,
1453 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001454 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001455){
adame6c3c722009-11-03 22:34:35 +00001456#ifdef SQLITE_ENABLE_SQLRR
1457 SRRecBindText(pStmt, i, zData, nData);
1458#endif
drh5e2517e2004-09-24 23:59:12 +00001459 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001460}
drhbbf483f2014-09-09 20:30:24 +00001461int sqlite3_bind_text64(
drhda4ca9d2014-09-09 17:27:35 +00001462 sqlite3_stmt *pStmt,
1463 int i,
1464 const char *zData,
1465 sqlite3_uint64 nData,
1466 void (*xDel)(void*),
1467 unsigned char enc
1468){
drhfc59a952014-09-11 23:34:55 +00001469 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001470 if( nData>0x7fffffff ){
1471 return invokeValueDestructor(zData, xDel, 0);
1472 }else{
drhfc59a952014-09-11 23:34:55 +00001473 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drh3329a632014-09-18 01:21:43 +00001474 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
drhda4ca9d2014-09-09 17:27:35 +00001475 }
1476}
drh6c626082004-11-14 21:56:29 +00001477#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001478int sqlite3_bind_text16(
1479 sqlite3_stmt *pStmt,
1480 int i,
1481 const void *zData,
1482 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001483 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001484){
adame6c3c722009-11-03 22:34:35 +00001485#ifdef SQLITE_ENABLE_SQLRR
1486 SRRecBindText(pStmt, i, zData, nData);
1487#endif
drh5e2517e2004-09-24 23:59:12 +00001488 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001489}
drh6c626082004-11-14 21:56:29 +00001490#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001491int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1492 int rc;
drh1b27b8c2014-02-10 03:21:57 +00001493 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
drh29def562009-04-14 12:43:33 +00001494 case SQLITE_INTEGER: {
1495 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1496 break;
1497 }
1498 case SQLITE_FLOAT: {
drh74eaba42014-09-18 17:52:15 +00001499 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
drh29def562009-04-14 12:43:33 +00001500 break;
1501 }
1502 case SQLITE_BLOB: {
1503 if( pValue->flags & MEM_Zero ){
1504 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1505 }else{
1506 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1507 }
1508 break;
1509 }
1510 case SQLITE_TEXT: {
adame6c3c722009-11-03 22:34:35 +00001511#ifdef SQLITE_ENABLE_SQLRR
adamcc3ec7c2010-02-26 22:05:01 +00001512 SRRecBindText(pStmt, i, pValue->z, pValue->n);
adame6c3c722009-11-03 22:34:35 +00001513#endif
drhac80db72009-04-14 12:58:20 +00001514 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1515 pValue->enc);
1516 break;
1517 }
1518 default: {
1519 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001520 break;
1521 }
danielk197726e41442006-06-14 15:16:35 +00001522 }
1523 return rc;
1524}
drhb026e052007-05-02 01:34:31 +00001525int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1526 int rc;
1527 Vdbe *p = (Vdbe *)pStmt;
adame6c3c722009-11-03 22:34:35 +00001528#ifdef SQLITE_ENABLE_SQLRR
1529 SRRecBindBlob(pStmt, i, NULL, n);
1530#endif
drhb026e052007-05-02 01:34:31 +00001531 rc = vdbeUnbind(p, i);
1532 if( rc==SQLITE_OK ){
1533 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001534 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001535 }
1536 return rc;
1537}
dan80c03022015-07-24 17:36:34 +00001538int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
1539 int rc;
1540 Vdbe *p = (Vdbe *)pStmt;
1541 sqlite3_mutex_enter(p->db->mutex);
1542 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
1543 rc = SQLITE_TOOBIG;
1544 }else{
1545 assert( (n & 0x7FFFFFFF)==n );
1546 rc = sqlite3_bind_zeroblob(pStmt, i, n);
1547 }
1548 rc = sqlite3ApiExit(p->db, rc);
1549 sqlite3_mutex_leave(p->db->mutex);
1550 return rc;
1551}
drh75f6a032004-07-15 14:15:00 +00001552
1553/*
1554** Return the number of wildcards that can be potentially bound to.
1555** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001556*/
1557int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001558 Vdbe *p = (Vdbe*)pStmt;
1559 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001560}
drh895d7472004-08-20 16:02:39 +00001561
1562/*
drhfa6bc002004-09-07 16:19:52 +00001563** Return the name of a wildcard parameter. Return NULL if the index
1564** is out of range or if the wildcard is unnamed.
1565**
1566** The result is always UTF-8.
1567*/
1568const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1569 Vdbe *p = (Vdbe*)pStmt;
drh9bf755c2016-12-23 03:59:31 +00001570 if( p==0 ) return 0;
1571 return sqlite3VListNumToName(p->pVList, i);
drh895d7472004-08-20 16:02:39 +00001572}
drhfa6bc002004-09-07 16:19:52 +00001573
1574/*
1575** Given a wildcard parameter name, return the index of the variable
1576** with that name. If there is no variable with the given name,
1577** return 0.
1578*/
drh5f18a222009-11-26 14:01:53 +00001579int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
drh9bf755c2016-12-23 03:59:31 +00001580 if( p==0 || zName==0 ) return 0;
1581 return sqlite3VListNameToNum(p->pVList, zName, nName);
drhfa6bc002004-09-07 16:19:52 +00001582}
drh5f18a222009-11-26 14:01:53 +00001583int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1584 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1585}
drhf8db1bc2005-04-22 02:38:37 +00001586
drh51942bc2005-06-12 22:01:42 +00001587/*
drhf8db1bc2005-04-22 02:38:37 +00001588** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001589*/
shane145834a2008-09-04 12:03:42 +00001590int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001591 Vdbe *pFrom = (Vdbe*)pFromStmt;
1592 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001593 int i;
1594 assert( pTo->db==pFrom->db );
1595 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001596 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001597 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001598 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001599 }
drh27641702007-08-22 02:56:42 +00001600 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001601 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001602}
drh51942bc2005-06-12 22:01:42 +00001603
shaneeec556d2008-10-12 00:27:53 +00001604#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001605/*
shane145834a2008-09-04 12:03:42 +00001606** Deprecated external interface. Internal/core SQLite code
1607** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001608**
peter.d.reid60ec9142014-09-06 16:39:46 +00001609** It is misuse to call this routine with statements from different
drh0f5ea0b2009-04-09 14:02:44 +00001610** database connections. But as this is a deprecated interface, we
1611** will not bother to check for that condition.
1612**
1613** If the two statements contain a different number of bindings, then
1614** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1615** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001616*/
1617int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001618 Vdbe *pFrom = (Vdbe*)pFromStmt;
1619 Vdbe *pTo = (Vdbe*)pToStmt;
1620 if( pFrom->nVar!=pTo->nVar ){
1621 return SQLITE_ERROR;
1622 }
drh2c2f3922017-06-01 00:54:35 +00001623 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001624 if( pTo->expmask ){
danc94b8592009-10-20 07:01:24 +00001625 pTo->expired = 1;
1626 }
drh2c2f3922017-06-01 00:54:35 +00001627 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001628 if( pFrom->expmask ){
danc94b8592009-10-20 07:01:24 +00001629 pFrom->expired = 1;
1630 }
shane145834a2008-09-04 12:03:42 +00001631 return sqlite3TransferBindings(pFromStmt, pToStmt);
1632}
shaneeec556d2008-10-12 00:27:53 +00001633#endif
shane145834a2008-09-04 12:03:42 +00001634
1635/*
drh51942bc2005-06-12 22:01:42 +00001636** Return the sqlite3* database handle to which the prepared statement given
1637** in the argument belongs. This is the same database handle that was
1638** the first argument to the sqlite3_prepare() that was used to create
1639** the statement in the first place.
1640*/
1641sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1642 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1643}
drhbb5a9c32008-06-19 02:52:25 +00001644
1645/*
drhf03d9cc2010-11-16 23:10:25 +00001646** Return true if the prepared statement is guaranteed to not modify the
1647** database.
1648*/
1649int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1650 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1651}
1652
1653/*
drh2fb66932011-11-25 17:21:47 +00001654** Return true if the prepared statement is in need of being reset.
1655*/
1656int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
1657 Vdbe *v = (Vdbe*)pStmt;
drh76336d52016-10-01 11:39:53 +00001658 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
drh2fb66932011-11-25 17:21:47 +00001659}
1660
1661/*
drhbb5a9c32008-06-19 02:52:25 +00001662** Return a pointer to the next prepared statement after pStmt associated
1663** with database connection pDb. If pStmt is NULL, return the first
1664** prepared statement for the database connection. Return NULL if there
1665** are no more.
1666*/
1667sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1668 sqlite3_stmt *pNext;
drh9ca95732014-10-24 00:35:58 +00001669#ifdef SQLITE_ENABLE_API_ARMOR
1670 if( !sqlite3SafetyCheckOk(pDb) ){
1671 (void)SQLITE_MISUSE_BKPT;
1672 return 0;
1673 }
1674#endif
drhbb5a9c32008-06-19 02:52:25 +00001675 sqlite3_mutex_enter(pDb->mutex);
1676 if( pStmt==0 ){
1677 pNext = (sqlite3_stmt*)pDb->pVdbe;
1678 }else{
1679 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1680 }
1681 sqlite3_mutex_leave(pDb->mutex);
1682 return pNext;
1683}
drhd1d38482008-10-07 23:46:38 +00001684
1685/*
1686** Return the value of a status counter for a prepared statement
1687*/
1688int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1689 Vdbe *pVdbe = (Vdbe*)pStmt;
drh9ca95732014-10-24 00:35:58 +00001690 u32 v;
1691#ifdef SQLITE_ENABLE_API_ARMOR
dan68cf69e2018-03-14 08:27:39 +00001692 if( !pStmt
1693 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
1694 ){
drh9ca95732014-10-24 00:35:58 +00001695 (void)SQLITE_MISUSE_BKPT;
1696 return 0;
1697 }
1698#endif
drh3528f6b2017-05-31 16:21:54 +00001699 if( op==SQLITE_STMTSTATUS_MEMUSED ){
1700 sqlite3 *db = pVdbe->db;
1701 sqlite3_mutex_enter(db->mutex);
1702 v = 0;
1703 db->pnBytesFreed = (int*)&v;
1704 sqlite3VdbeClearObject(db, pVdbe);
1705 sqlite3DbFree(db, pVdbe);
1706 db->pnBytesFreed = 0;
1707 sqlite3_mutex_leave(db->mutex);
1708 }else{
1709 v = pVdbe->aCounter[op];
1710 if( resetFlag ) pVdbe->aCounter[op] = 0;
1711 }
drh9b47ee32013-08-20 03:13:51 +00001712 return (int)v;
drhd1d38482008-10-07 23:46:38 +00001713}
dan04489b62014-10-31 20:11:32 +00001714
drh557341e2016-07-23 02:07:26 +00001715/*
1716** Return the SQL associated with a prepared statement
1717*/
1718const char *sqlite3_sql(sqlite3_stmt *pStmt){
1719 Vdbe *p = (Vdbe *)pStmt;
1720 return p ? p->zSql : 0;
1721}
1722
1723/*
1724** Return the SQL associated with a prepared statement with
1725** bound parameters expanded. Space to hold the returned string is
1726** obtained from sqlite3_malloc(). The caller is responsible for
1727** freeing the returned string by passing it to sqlite3_free().
1728**
1729** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1730** expanded bound parameters.
1731*/
1732char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1733#ifdef SQLITE_OMIT_TRACE
1734 return 0;
1735#else
1736 char *z = 0;
1737 const char *zSql = sqlite3_sql(pStmt);
1738 if( zSql ){
1739 Vdbe *p = (Vdbe *)pStmt;
1740 sqlite3_mutex_enter(p->db->mutex);
1741 z = sqlite3VdbeExpandSql(p, zSql);
1742 sqlite3_mutex_leave(p->db->mutex);
1743 }
1744 return z;
1745#endif
1746}
1747
drh9b1c62d2011-03-30 21:04:43 +00001748#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001749/*
dan93bca692011-09-14 19:41:44 +00001750** Allocate and populate an UnpackedRecord structure based on the serialized
1751** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
1752** if successful, or a NULL pointer if an OOM error is encountered.
1753*/
1754static UnpackedRecord *vdbeUnpackRecord(
1755 KeyInfo *pKeyInfo,
1756 int nKey,
1757 const void *pKey
1758){
dan93bca692011-09-14 19:41:44 +00001759 UnpackedRecord *pRet; /* Return value */
1760
drha582b012016-12-21 19:45:54 +00001761 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
dan93bca692011-09-14 19:41:44 +00001762 if( pRet ){
drha485ad12017-08-02 22:43:14 +00001763 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
dan93bca692011-09-14 19:41:44 +00001764 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
1765 }
1766 return pRet;
1767}
1768
1769/*
dan37db03b2011-03-16 19:59:18 +00001770** This function is called from within a pre-update callback to retrieve
1771** a field of the row currently being updated or deleted.
1772*/
dan46c47d42011-03-01 18:42:07 +00001773int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
1774 PreUpdate *p = db->pPreUpdate;
dan7271d7a2017-01-25 18:53:27 +00001775 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001776 int rc = SQLITE_OK;
1777
dan37db03b2011-03-16 19:59:18 +00001778 /* Test that this call is being made from within an SQLITE_DELETE or
1779 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
dan46c47d42011-03-01 18:42:07 +00001780 if( !p || p->op==SQLITE_INSERT ){
1781 rc = SQLITE_MISUSE_BKPT;
1782 goto preupdate_old_out;
1783 }
dancb9a3642017-01-30 19:44:53 +00001784 if( p->pPk ){
1785 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1786 }
dan46c47d42011-03-01 18:42:07 +00001787 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1788 rc = SQLITE_RANGE;
1789 goto preupdate_old_out;
1790 }
1791
dan37db03b2011-03-16 19:59:18 +00001792 /* If the old.* record has not yet been loaded into memory, do so now. */
dan46c47d42011-03-01 18:42:07 +00001793 if( p->pUnpacked==0 ){
dan12ca0b52011-03-21 16:17:42 +00001794 u32 nRec;
1795 u8 *aRec;
dan46c47d42011-03-01 18:42:07 +00001796
drha7c90c42016-06-04 20:37:10 +00001797 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
dan12ca0b52011-03-21 16:17:42 +00001798 aRec = sqlite3DbMallocRaw(db, nRec);
1799 if( !aRec ) goto preupdate_old_out;
drhcb3cabd2016-11-25 19:18:28 +00001800 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001801 if( rc==SQLITE_OK ){
dan93bca692011-09-14 19:41:44 +00001802 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001803 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
1804 }
dan46c47d42011-03-01 18:42:07 +00001805 if( rc!=SQLITE_OK ){
dan12ca0b52011-03-21 16:17:42 +00001806 sqlite3DbFree(db, aRec);
dan46c47d42011-03-01 18:42:07 +00001807 goto preupdate_old_out;
1808 }
dan12ca0b52011-03-21 16:17:42 +00001809 p->aRecord = aRec;
dan46c47d42011-03-01 18:42:07 +00001810 }
1811
dan7271d7a2017-01-25 18:53:27 +00001812 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1813 if( iIdx==p->pTab->iPKey ){
1814 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
1815 }else if( iIdx>=p->pUnpacked->nField ){
dan46c47d42011-03-01 18:42:07 +00001816 *ppValue = (sqlite3_value *)columnNullValue();
dan7271d7a2017-01-25 18:53:27 +00001817 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1818 if( pMem->flags & MEM_Int ){
1819 sqlite3VdbeMemRealify(pMem);
dan319eeb72011-03-19 08:38:50 +00001820 }
dan46c47d42011-03-01 18:42:07 +00001821 }
1822
1823 preupdate_old_out:
drhe1ed0b02014-08-26 02:15:07 +00001824 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001825 return sqlite3ApiExit(db, rc);
1826}
drh9b1c62d2011-03-30 21:04:43 +00001827#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001828
drh9b1c62d2011-03-30 21:04:43 +00001829#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001830/*
1831** This function is called from within a pre-update callback to retrieve
1832** the number of columns in the row being updated, deleted or inserted.
1833*/
dan46c47d42011-03-01 18:42:07 +00001834int sqlite3_preupdate_count(sqlite3 *db){
1835 PreUpdate *p = db->pPreUpdate;
drha485ad12017-08-02 22:43:14 +00001836 return (p ? p->keyinfo.nKeyField : 0);
dan46c47d42011-03-01 18:42:07 +00001837}
drh9b1c62d2011-03-30 21:04:43 +00001838#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001839
drh9b1c62d2011-03-30 21:04:43 +00001840#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001841/*
dan1e7a2d42011-03-22 18:45:29 +00001842** This function is designed to be called from within a pre-update callback
1843** only. It returns zero if the change that caused the callback was made
1844** immediately by a user SQL statement. Or, if the change was made by a
1845** trigger program, it returns the number of trigger programs currently
1846** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
1847** top-level trigger etc.).
1848**
1849** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
1850** or SET DEFAULT action is considered a trigger.
1851*/
1852int sqlite3_preupdate_depth(sqlite3 *db){
1853 PreUpdate *p = db->pPreUpdate;
1854 return (p ? p->v->nFrame : 0);
1855}
drh9b1c62d2011-03-30 21:04:43 +00001856#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan1e7a2d42011-03-22 18:45:29 +00001857
drh9b1c62d2011-03-30 21:04:43 +00001858#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan1e7a2d42011-03-22 18:45:29 +00001859/*
dan37db03b2011-03-16 19:59:18 +00001860** This function is called from within a pre-update callback to retrieve
1861** a field of the row currently being updated or inserted.
1862*/
1863int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
dan46c47d42011-03-01 18:42:07 +00001864 PreUpdate *p = db->pPreUpdate;
1865 int rc = SQLITE_OK;
dan37db03b2011-03-16 19:59:18 +00001866 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001867
dan37db03b2011-03-16 19:59:18 +00001868 if( !p || p->op==SQLITE_DELETE ){
dan46c47d42011-03-01 18:42:07 +00001869 rc = SQLITE_MISUSE_BKPT;
dan37db03b2011-03-16 19:59:18 +00001870 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001871 }
dancb9a3642017-01-30 19:44:53 +00001872 if( p->pPk && p->op!=SQLITE_UPDATE ){
1873 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1874 }
dan46c47d42011-03-01 18:42:07 +00001875 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1876 rc = SQLITE_RANGE;
dan37db03b2011-03-16 19:59:18 +00001877 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001878 }
dan46c47d42011-03-01 18:42:07 +00001879
dan37db03b2011-03-16 19:59:18 +00001880 if( p->op==SQLITE_INSERT ){
1881 /* For an INSERT, memory cell p->iNewReg contains the serialized record
1882 ** that is being inserted. Deserialize it. */
1883 UnpackedRecord *pUnpack = p->pNewUnpacked;
1884 if( !pUnpack ){
1885 Mem *pData = &p->v->aMem[p->iNewReg];
drhff535a22016-09-20 01:46:15 +00001886 rc = ExpandBlob(pData);
dan37db03b2011-03-16 19:59:18 +00001887 if( rc!=SQLITE_OK ) goto preupdate_new_out;
dan93bca692011-09-14 19:41:44 +00001888 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
dan37db03b2011-03-16 19:59:18 +00001889 if( !pUnpack ){
1890 rc = SQLITE_NOMEM;
1891 goto preupdate_new_out;
1892 }
1893 p->pNewUnpacked = pUnpack;
1894 }
dan2a86c192017-01-25 17:44:13 +00001895 pMem = &pUnpack->aMem[iIdx];
1896 if( iIdx==p->pTab->iPKey ){
1897 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1898 }else if( iIdx>=pUnpack->nField ){
dan37db03b2011-03-16 19:59:18 +00001899 pMem = (sqlite3_value *)columnNullValue();
dan37db03b2011-03-16 19:59:18 +00001900 }
1901 }else{
1902 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
1903 ** value. Make a copy of the cell contents and return a pointer to it.
1904 ** It is not safe to return a pointer to the memory cell itself as the
1905 ** caller may modify the value text encoding.
1906 */
1907 assert( p->op==SQLITE_UPDATE );
1908 if( !p->aNew ){
1909 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
1910 if( !p->aNew ){
1911 rc = SQLITE_NOMEM;
1912 goto preupdate_new_out;
1913 }
1914 }
drh304637c2011-03-18 16:47:27 +00001915 assert( iIdx>=0 && iIdx<p->pCsr->nField );
dan37db03b2011-03-16 19:59:18 +00001916 pMem = &p->aNew[iIdx];
1917 if( pMem->flags==0 ){
dane43635a2016-10-21 21:21:45 +00001918 if( iIdx==p->pTab->iPKey ){
dan319eeb72011-03-19 08:38:50 +00001919 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1920 }else{
1921 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
1922 if( rc!=SQLITE_OK ) goto preupdate_new_out;
1923 }
dan37db03b2011-03-16 19:59:18 +00001924 }
1925 }
1926 *ppValue = pMem;
1927
1928 preupdate_new_out:
drhe1ed0b02014-08-26 02:15:07 +00001929 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001930 return sqlite3ApiExit(db, rc);
1931}
drh9b1c62d2011-03-30 21:04:43 +00001932#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
drh04e8a582014-11-18 21:20:57 +00001933
dan6f9702e2014-11-01 20:38:06 +00001934#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan04489b62014-10-31 20:11:32 +00001935/*
1936** Return status data for a single loop within query pStmt.
1937*/
1938int sqlite3_stmt_scanstatus(
drhd1a1c232014-11-03 16:35:55 +00001939 sqlite3_stmt *pStmt, /* Prepared statement being queried */
dan04489b62014-10-31 20:11:32 +00001940 int idx, /* Index of loop to report on */
drhd1a1c232014-11-03 16:35:55 +00001941 int iScanStatusOp, /* Which metric to return */
1942 void *pOut /* OUT: Write the answer here */
dan04489b62014-10-31 20:11:32 +00001943){
1944 Vdbe *p = (Vdbe*)pStmt;
dan037b5322014-11-03 11:25:32 +00001945 ScanStatus *pScan;
dan6f9702e2014-11-01 20:38:06 +00001946 if( idx<0 || idx>=p->nScan ) return 1;
1947 pScan = &p->aScan[idx];
drhd1a1c232014-11-03 16:35:55 +00001948 switch( iScanStatusOp ){
1949 case SQLITE_SCANSTAT_NLOOP: {
1950 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
1951 break;
1952 }
1953 case SQLITE_SCANSTAT_NVISIT: {
1954 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
1955 break;
1956 }
1957 case SQLITE_SCANSTAT_EST: {
drh518140e2014-11-06 03:55:10 +00001958 double r = 1.0;
1959 LogEst x = pScan->nEst;
1960 while( x<100 ){
1961 x += 10;
1962 r *= 0.5;
1963 }
1964 *(double*)pOut = r*sqlite3LogEstToInt(x);
drhd1a1c232014-11-03 16:35:55 +00001965 break;
1966 }
1967 case SQLITE_SCANSTAT_NAME: {
dand72219d2014-11-03 16:39:37 +00001968 *(const char**)pOut = pScan->zName;
drhd1a1c232014-11-03 16:35:55 +00001969 break;
1970 }
1971 case SQLITE_SCANSTAT_EXPLAIN: {
1972 if( pScan->addrExplain ){
1973 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
1974 }else{
1975 *(const char**)pOut = 0;
1976 }
1977 break;
1978 }
drhc6652b12014-11-06 04:42:20 +00001979 case SQLITE_SCANSTAT_SELECTID: {
1980 if( pScan->addrExplain ){
1981 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
1982 }else{
1983 *(int*)pOut = -1;
1984 }
1985 break;
1986 }
drhd1a1c232014-11-03 16:35:55 +00001987 default: {
1988 return 1;
dan6f9702e2014-11-01 20:38:06 +00001989 }
1990 }
dan04489b62014-10-31 20:11:32 +00001991 return 0;
1992}
1993
1994/*
1995** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
1996*/
1997void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
1998 Vdbe *p = (Vdbe*)pStmt;
dan6f9702e2014-11-01 20:38:06 +00001999 memset(p->anExec, 0, p->nOp * sizeof(i64));
dan04489b62014-10-31 20:11:32 +00002000}
dan6f9702e2014-11-01 20:38:06 +00002001#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */