blob: 5826c5b1a85640916196762668830149ca591078 [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"
18
shaneeec556d2008-10-12 00:27:53 +000019#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +000020/*
21** Return TRUE (non-zero) of the statement supplied as an argument needs
22** to be recompiled. A statement needs to be recompiled whenever the
23** execution environment changes in a way that would alter the program
24** that sqlite3_prepare() generates. For example, if new functions or
25** collating sequences are registered or if an authorizer function is
26** added or changed.
drhd89bd002005-01-22 03:03:54 +000027*/
28int sqlite3_expired(sqlite3_stmt *pStmt){
29 Vdbe *p = (Vdbe*)pStmt;
30 return p==0 || p->expired;
31}
shaneeec556d2008-10-12 00:27:53 +000032#endif
drhd89bd002005-01-22 03:03:54 +000033
drhe30f4422007-08-21 16:15:55 +000034/*
drh413c3d32010-02-23 20:11:56 +000035** Check on a Vdbe to make sure it has not been finalized. Log
36** an error and return true if it has been finalized (or is otherwise
37** invalid). Return false if it is ok.
38*/
39static int vdbeSafety(Vdbe *p){
40 if( p->db==0 ){
41 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
42 return 1;
43 }else{
44 return 0;
45 }
46}
47static int vdbeSafetyNotNull(Vdbe *p){
48 if( p==0 ){
49 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
50 return 1;
51 }else{
52 return vdbeSafety(p);
53 }
54}
55
drh201e0c62015-07-14 14:48:50 +000056#ifndef SQLITE_OMIT_TRACE
57/*
58** Invoke the profile callback. This routine is only called if we already
59** know that the profile callback is defined and needs to be invoked.
60*/
61static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
62 sqlite3_int64 iNow;
drh3d2a5292016-07-13 22:55:01 +000063 sqlite3_int64 iElapse;
drh201e0c62015-07-14 14:48:50 +000064 assert( p->startTime>0 );
drh3d2a5292016-07-13 22:55:01 +000065 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
drh201e0c62015-07-14 14:48:50 +000066 assert( db->init.busy==0 );
67 assert( p->zSql!=0 );
68 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
drh3d2a5292016-07-13 22:55:01 +000069 iElapse = (iNow - p->startTime)*1000000;
70 if( db->xProfile ){
71 db->xProfile(db->pProfileArg, p->zSql, iElapse);
72 }
73 if( db->mTrace & SQLITE_TRACE_PROFILE ){
drhfca760c2016-07-14 01:09:08 +000074 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
drh3d2a5292016-07-13 22:55:01 +000075 }
drh201e0c62015-07-14 14:48:50 +000076 p->startTime = 0;
77}
78/*
79** The checkProfileCallback(DB,P) macro checks to see if a profile callback
80** is needed, and it invokes the callback if it is needed.
81*/
82# define checkProfileCallback(DB,P) \
83 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
84#else
85# define checkProfileCallback(DB,P) /*no-op*/
86#endif
87
drh413c3d32010-02-23 20:11:56 +000088/*
drhe30f4422007-08-21 16:15:55 +000089** The following routine destroys a virtual machine that is created by
90** the sqlite3_compile() routine. The integer returned is an SQLITE_
91** success/failure code that describes the result of executing the virtual
92** machine.
93**
94** This routine sets the error code and string returned by
95** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
96*/
97int sqlite3_finalize(sqlite3_stmt *pStmt){
98 int rc;
99 if( pStmt==0 ){
drh65bafa62010-09-29 01:54:00 +0000100 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
101 ** pointer is a harmless no-op. */
drhe30f4422007-08-21 16:15:55 +0000102 rc = SQLITE_OK;
103 }else{
104 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06 +0000105 sqlite3 *db = v->db;
drh413c3d32010-02-23 20:11:56 +0000106 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
drh4245c402012-06-02 14:32:21 +0000107 sqlite3_mutex_enter(db->mutex);
drh201e0c62015-07-14 14:48:50 +0000108 checkProfileCallback(db, v);
drhe30f4422007-08-21 16:15:55 +0000109 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +0000110 rc = sqlite3ApiExit(db, rc);
drh4245c402012-06-02 14:32:21 +0000111 sqlite3LeaveMutexAndCloseZombie(db);
drhe30f4422007-08-21 16:15:55 +0000112 }
113 return rc;
114}
115
116/*
117** Terminate the current execution of an SQL statement and reset it
118** back to its starting state so that it can be reused. A success code from
119** the prior execution is returned.
120**
121** This routine sets the error code and string returned by
122** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
123*/
124int sqlite3_reset(sqlite3_stmt *pStmt){
125 int rc;
126 if( pStmt==0 ){
127 rc = SQLITE_OK;
128 }else{
129 Vdbe *v = (Vdbe*)pStmt;
drh201e0c62015-07-14 14:48:50 +0000130 sqlite3 *db = v->db;
131 sqlite3_mutex_enter(db->mutex);
132 checkProfileCallback(db, v);
drhc890fec2008-08-01 20:10:08 +0000133 rc = sqlite3VdbeReset(v);
drh124c0b42011-06-01 18:15:55 +0000134 sqlite3VdbeRewind(v);
drh201e0c62015-07-14 14:48:50 +0000135 assert( (rc & (db->errMask))==rc );
136 rc = sqlite3ApiExit(db, rc);
137 sqlite3_mutex_leave(db->mutex);
drhe30f4422007-08-21 16:15:55 +0000138 }
139 return rc;
140}
141
142/*
143** Set all the parameters in the compiled SQL statement to NULL.
144*/
145int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
146 int i;
147 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +0000148 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +0000149#if SQLITE_THREADSAFE
drh7e8b8482008-01-23 03:03:05 +0000150 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
151#endif
152 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +0000153 for(i=0; i<p->nVar; i++){
154 sqlite3VdbeMemRelease(&p->aVar[i]);
155 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000156 }
drh2c2f3922017-06-01 00:54:35 +0000157 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
danbda4cb82017-02-23 16:30:16 +0000158 if( p->expmask ){
danc94b8592009-10-20 07:01:24 +0000159 p->expired = 1;
160 }
drh7e8b8482008-01-23 03:03:05 +0000161 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000162 return rc;
163}
164
165
drh4f26d6c2004-05-26 23:25:30 +0000166/**************************** sqlite3_value_ *******************************
167** The following routines extract information from a Mem or sqlite3_value
168** structure.
169*/
170const void *sqlite3_value_blob(sqlite3_value *pVal){
171 Mem *p = (Mem*)pVal;
172 if( p->flags & (MEM_Blob|MEM_Str) ){
drhff535a22016-09-20 01:46:15 +0000173 if( ExpandBlob(p)!=SQLITE_OK ){
dana4d5ae82015-07-24 16:24:37 +0000174 assert( p->flags==MEM_Null && p->z==0 );
175 return 0;
176 }
drh1f0feef2007-05-15 13:27:07 +0000177 p->flags |= MEM_Blob;
drhf43514f2017-07-27 14:04:57 +0000178 return p->z; // p->n ? p->z : 0;
drh4f26d6c2004-05-26 23:25:30 +0000179 }else{
180 return sqlite3_value_text(pVal);
181 }
182}
183int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000184 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000185}
186int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000187 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000188}
189double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000190 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000191}
192int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000193 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000194}
drhefad9992004-06-22 12:13:55 +0000195sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000196 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000197}
drhbcdf78a2015-09-10 20:34:56 +0000198unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
dan5b6c8e42016-01-30 15:46:03 +0000199 Mem *pMem = (Mem*)pVal;
200 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
drhbcdf78a2015-09-10 20:34:56 +0000201}
drhae3ec3f2017-07-17 00:40:19 +0000202void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
drh3a96a5d2017-06-30 23:09:03 +0000203 Mem *p = (Mem*)pVal;
drh9a541c02017-07-17 11:39:46 +0000204 if( p->flags==(MEM_Null|MEM_Subtype|MEM_Term|MEM_Static)
drhae3ec3f2017-07-17 00:40:19 +0000205 && zPType!=0
drh06d49402017-07-17 17:46:29 +0000206 && p->eSubtype=='p'
drhae3ec3f2017-07-17 00:40:19 +0000207 && strcmp(p->z, zPType)==0
208 ){
drh3a96a5d2017-06-30 23:09:03 +0000209 return p->u.pPtr;
210 }else{
211 return 0;
212 }
213}
drh4f26d6c2004-05-26 23:25:30 +0000214const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000215 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000216}
drh6c626082004-11-14 21:56:29 +0000217#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000218const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000219 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000220}
danielk1977d8123362004-06-12 09:25:12 +0000221const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000222 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000223}
224const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000225 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000226}
drh6c626082004-11-14 21:56:29 +0000227#endif /* SQLITE_OMIT_UTF16 */
drh22ec1342015-02-27 00:33:15 +0000228/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
229** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
230** point number string BLOB NULL
231*/
drh4f26d6c2004-05-26 23:25:30 +0000232int sqlite3_value_type(sqlite3_value* pVal){
drh1b27b8c2014-02-10 03:21:57 +0000233 static const u8 aType[] = {
234 SQLITE_BLOB, /* 0x00 */
235 SQLITE_NULL, /* 0x01 */
236 SQLITE_TEXT, /* 0x02 */
237 SQLITE_NULL, /* 0x03 */
238 SQLITE_INTEGER, /* 0x04 */
239 SQLITE_NULL, /* 0x05 */
240 SQLITE_INTEGER, /* 0x06 */
241 SQLITE_NULL, /* 0x07 */
242 SQLITE_FLOAT, /* 0x08 */
243 SQLITE_NULL, /* 0x09 */
244 SQLITE_FLOAT, /* 0x0a */
245 SQLITE_NULL, /* 0x0b */
246 SQLITE_INTEGER, /* 0x0c */
247 SQLITE_NULL, /* 0x0d */
248 SQLITE_INTEGER, /* 0x0e */
249 SQLITE_NULL, /* 0x0f */
250 SQLITE_BLOB, /* 0x10 */
251 SQLITE_NULL, /* 0x11 */
252 SQLITE_TEXT, /* 0x12 */
253 SQLITE_NULL, /* 0x13 */
254 SQLITE_INTEGER, /* 0x14 */
255 SQLITE_NULL, /* 0x15 */
256 SQLITE_INTEGER, /* 0x16 */
257 SQLITE_NULL, /* 0x17 */
258 SQLITE_FLOAT, /* 0x18 */
259 SQLITE_NULL, /* 0x19 */
260 SQLITE_FLOAT, /* 0x1a */
261 SQLITE_NULL, /* 0x1b */
262 SQLITE_INTEGER, /* 0x1c */
263 SQLITE_NULL, /* 0x1d */
264 SQLITE_INTEGER, /* 0x1e */
265 SQLITE_NULL, /* 0x1f */
266 };
mistachkinafc14f72014-03-05 01:29:18 +0000267 return aType[pVal->flags&MEM_AffMask];
drh4f26d6c2004-05-26 23:25:30 +0000268}
269
drh4f03f412015-05-20 21:28:32 +0000270/* Make a copy of an sqlite3_value object
271*/
272sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
273 sqlite3_value *pNew;
274 if( pOrig==0 ) return 0;
275 pNew = sqlite3_malloc( sizeof(*pNew) );
276 if( pNew==0 ) return 0;
277 memset(pNew, 0, sizeof(*pNew));
278 memcpy(pNew, pOrig, MEMCELLSIZE);
279 pNew->flags &= ~MEM_Dyn;
280 pNew->db = 0;
281 if( pNew->flags&(MEM_Str|MEM_Blob) ){
drh10ca5b42015-05-22 21:04:54 +0000282 pNew->flags &= ~(MEM_Static|MEM_Dyn);
283 pNew->flags |= MEM_Ephem;
284 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
285 sqlite3ValueFree(pNew);
286 pNew = 0;
drh4f03f412015-05-20 21:28:32 +0000287 }
288 }
289 return pNew;
290}
291
292/* Destroy an sqlite3_value object previously obtained from
293** sqlite3_value_dup().
294*/
295void sqlite3_value_free(sqlite3_value *pOld){
296 sqlite3ValueFree(pOld);
297}
298
299
drh4f26d6c2004-05-26 23:25:30 +0000300/**************************** sqlite3_result_ *******************************
301** The following routines are used by user-defined functions to specify
302** the function result.
drh4c8555f2009-06-25 01:47:11 +0000303**
peter.d.reid60ec9142014-09-06 16:39:46 +0000304** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
drh4c8555f2009-06-25 01:47:11 +0000305** result as a string or blob but if the string or blob is too large, it
306** then sets the error code to SQLITE_TOOBIG
drhda4ca9d2014-09-09 17:27:35 +0000307**
308** The invokeValueDestructor(P,X) routine invokes destructor function X()
309** on value P is not going to be used and need to be destroyed.
drh4f26d6c2004-05-26 23:25:30 +0000310*/
drh4c8555f2009-06-25 01:47:11 +0000311static void setResultStrOrError(
312 sqlite3_context *pCtx, /* Function context */
313 const char *z, /* String pointer */
314 int n, /* Bytes in string, or negative */
315 u8 enc, /* Encoding of z. 0 for BLOBs */
316 void (*xDel)(void*) /* Destructor function */
317){
drh9bd038f2014-08-27 14:14:06 +0000318 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
drh4c8555f2009-06-25 01:47:11 +0000319 sqlite3_result_error_toobig(pCtx);
320 }
321}
drhda4ca9d2014-09-09 17:27:35 +0000322static int invokeValueDestructor(
323 const void *p, /* Value to destroy */
324 void (*xDel)(void*), /* The destructor */
325 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
326){
drhfc59a952014-09-11 23:34:55 +0000327 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000328 if( xDel==0 ){
329 /* noop */
330 }else if( xDel==SQLITE_TRANSIENT ){
331 /* noop */
drhda4ca9d2014-09-09 17:27:35 +0000332 }else{
333 xDel((void*)p);
334 }
335 if( pCtx ) sqlite3_result_error_toobig(pCtx);
336 return SQLITE_TOOBIG;
337}
drh4f26d6c2004-05-26 23:25:30 +0000338void sqlite3_result_blob(
339 sqlite3_context *pCtx,
340 const void *z,
341 int n,
danielk1977d8123362004-06-12 09:25:12 +0000342 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000343){
drh5708d2d2005-06-22 10:53:59 +0000344 assert( n>=0 );
drh9bd038f2014-08-27 14:14:06 +0000345 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000346 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000347}
drhda4ca9d2014-09-09 17:27:35 +0000348void sqlite3_result_blob64(
349 sqlite3_context *pCtx,
350 const void *z,
351 sqlite3_uint64 n,
352 void (*xDel)(void *)
353){
354 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000355 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000356 if( n>0x7fffffff ){
357 (void)invokeValueDestructor(z, xDel, pCtx);
358 }else{
359 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
360 }
361}
drh4f26d6c2004-05-26 23:25:30 +0000362void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh9bd038f2014-08-27 14:14:06 +0000363 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
364 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
drh4f26d6c2004-05-26 23:25:30 +0000365}
366void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000367 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000368 pCtx->isError = SQLITE_ERROR;
drh9b47ee32013-08-20 03:13:51 +0000369 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000370 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000371}
danielk1977a1686c92006-01-23 07:52:37 +0000372#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000373void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000374 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000375 pCtx->isError = SQLITE_ERROR;
drh9b47ee32013-08-20 03:13:51 +0000376 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000377 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000378}
danielk1977a1686c92006-01-23 07:52:37 +0000379#endif
drhf4479502004-05-27 03:12:53 +0000380void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh9bd038f2014-08-27 14:14:06 +0000381 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
382 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
drh4f26d6c2004-05-26 23:25:30 +0000383}
384void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh9bd038f2014-08-27 14:14:06 +0000385 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
386 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
drh4f26d6c2004-05-26 23:25:30 +0000387}
388void sqlite3_result_null(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000389 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
390 sqlite3VdbeMemSetNull(pCtx->pOut);
drh4f26d6c2004-05-26 23:25:30 +0000391}
drhae3ec3f2017-07-17 00:40:19 +0000392void sqlite3_result_pointer(sqlite3_context *pCtx, void *pPtr, const char *zPT){
drh3a96a5d2017-06-30 23:09:03 +0000393 Mem *pOut = pCtx->pOut;
394 assert( sqlite3_mutex_held(pOut->db->mutex) );
395 sqlite3VdbeMemSetNull(pOut);
drhae3ec3f2017-07-17 00:40:19 +0000396 sqlite3VdbeMemSetPointer(pOut, pPtr, zPT);
drh3a96a5d2017-06-30 23:09:03 +0000397}
drhbcdf78a2015-09-10 20:34:56 +0000398void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
dan5b6c8e42016-01-30 15:46:03 +0000399 Mem *pOut = pCtx->pOut;
400 assert( sqlite3_mutex_held(pOut->db->mutex) );
401 pOut->eSubtype = eSubtype & 0xff;
402 pOut->flags |= MEM_Subtype;
drhbcdf78a2015-09-10 20:34:56 +0000403}
drh4f26d6c2004-05-26 23:25:30 +0000404void sqlite3_result_text(
405 sqlite3_context *pCtx,
406 const char *z,
407 int n,
danielk1977d8123362004-06-12 09:25:12 +0000408 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000409){
drh9bd038f2014-08-27 14:14:06 +0000410 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000411 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000412}
drhbbf483f2014-09-09 20:30:24 +0000413void sqlite3_result_text64(
drhda4ca9d2014-09-09 17:27:35 +0000414 sqlite3_context *pCtx,
415 const char *z,
416 sqlite3_uint64 n,
417 void (*xDel)(void *),
418 unsigned char enc
419){
420 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000421 assert( xDel!=SQLITE_DYNAMIC );
drh79f7af92014-10-03 16:00:51 +0000422 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drhda4ca9d2014-09-09 17:27:35 +0000423 if( n>0x7fffffff ){
424 (void)invokeValueDestructor(z, xDel, pCtx);
425 }else{
426 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
427 }
428}
drh6c626082004-11-14 21:56:29 +0000429#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000430void sqlite3_result_text16(
431 sqlite3_context *pCtx,
432 const void *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_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000438}
439void sqlite3_result_text16be(
440 sqlite3_context *pCtx,
441 const void *z,
442 int n,
443 void (*xDel)(void *)
444){
drh9bd038f2014-08-27 14:14:06 +0000445 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000446 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000447}
448void sqlite3_result_text16le(
449 sqlite3_context *pCtx,
450 const void *z,
451 int n,
452 void (*xDel)(void *)
453){
drh9bd038f2014-08-27 14:14:06 +0000454 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000455 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000456}
drh6c626082004-11-14 21:56:29 +0000457#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000458void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh9bd038f2014-08-27 14:14:06 +0000459 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
460 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000461}
drhb026e052007-05-02 01:34:31 +0000462void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh9bd038f2014-08-27 14:14:06 +0000463 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
464 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
drhb026e052007-05-02 01:34:31 +0000465}
dana4d5ae82015-07-24 16:24:37 +0000466int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
467 Mem *pOut = pCtx->pOut;
468 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh6bacdc22015-07-24 17:14:03 +0000469 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
dana4d5ae82015-07-24 16:24:37 +0000470 return SQLITE_TOOBIG;
471 }
472 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
473 return SQLITE_OK;
474}
drh69544ec2008-02-06 14:11:34 +0000475void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
476 pCtx->isError = errCode;
drh9b47ee32013-08-20 03:13:51 +0000477 pCtx->fErrorOrAux = 1;
drh983b5ee2015-02-13 12:05:56 +0000478#ifdef SQLITE_DEBUG
drh96f4ad22015-03-12 21:02:36 +0000479 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
drh983b5ee2015-02-13 12:05:56 +0000480#endif
drh9bd038f2014-08-27 14:14:06 +0000481 if( pCtx->pOut->flags & MEM_Null ){
482 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
drh51c7d862009-04-27 18:46:06 +0000483 SQLITE_UTF8, SQLITE_STATIC);
484 }
drh69544ec2008-02-06 14:11:34 +0000485}
drh4f26d6c2004-05-26 23:25:30 +0000486
drha0206bc2007-05-08 15:15:02 +0000487/* Force an SQLITE_TOOBIG error. */
488void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000489 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000490 pCtx->isError = SQLITE_TOOBIG;
drh9b47ee32013-08-20 03:13:51 +0000491 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000492 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
drhbb4957f2008-03-20 14:03:29 +0000493 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000494}
495
danielk1977a1644fd2007-08-29 12:31:25 +0000496/* An SQLITE_NOMEM error. */
497void sqlite3_result_error_nomem(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000498 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
499 sqlite3VdbeMemSetNull(pCtx->pOut);
mistachkinfad30392016-02-13 23:43:46 +0000500 pCtx->isError = SQLITE_NOMEM_BKPT;
drh9b47ee32013-08-20 03:13:51 +0000501 pCtx->fErrorOrAux = 1;
drh4a642b62016-02-05 01:55:27 +0000502 sqlite3OomFault(pCtx->pOut->db);
danielk1977a1644fd2007-08-29 12:31:25 +0000503}
drh4f26d6c2004-05-26 23:25:30 +0000504
dan5cf53532010-05-01 16:40:20 +0000505/*
506** This function is called after a transaction has been committed. It
507** invokes callbacks registered with sqlite3_wal_hook() as required.
508*/
drh7ed91f22010-04-29 22:34:07 +0000509static int doWalCallbacks(sqlite3 *db){
dan8d22a172010-04-19 18:03:51 +0000510 int rc = SQLITE_OK;
dan5cf53532010-05-01 16:40:20 +0000511#ifndef SQLITE_OMIT_WAL
512 int i;
dan8d22a172010-04-19 18:03:51 +0000513 for(i=0; i<db->nDb; i++){
514 Btree *pBt = db->aDb[i].pBt;
515 if( pBt ){
drhef15c6e2014-12-12 01:27:17 +0000516 int nEntry;
517 sqlite3BtreeEnter(pBt);
518 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
519 sqlite3BtreeLeave(pBt);
drh5def0842010-05-05 20:00:25 +0000520 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
drh69c33822016-08-18 14:33:11 +0000521 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
dan8d22a172010-04-19 18:03:51 +0000522 }
523 }
524 }
dan5cf53532010-05-01 16:40:20 +0000525#endif
dan8d22a172010-04-19 18:03:51 +0000526 return rc;
527}
528
drh201e0c62015-07-14 14:48:50 +0000529
drh4f26d6c2004-05-26 23:25:30 +0000530/*
531** Execute the statement pStmt, either until a row of data is ready, the
532** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000533**
534** This routine implements the bulk of the logic behind the sqlite_step()
535** API. The only thing omitted is the automatic recompile if a
536** schema change has occurred. That detail is handled by the
537** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000538*/
drhb900aaf2006-11-09 00:24:53 +0000539static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000540 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000541 int rc;
542
danielk1977a185ddb2007-11-15 16:04:15 +0000543 assert(p);
544 if( p->magic!=VDBE_MAGIC_RUN ){
drh3674bfd2010-04-17 12:53:19 +0000545 /* We used to require that sqlite3_reset() be called before retrying
drh602acb42011-01-17 17:42:37 +0000546 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
547 ** with version 3.7.0, we changed this so that sqlite3_reset() would
548 ** be called automatically instead of throwing the SQLITE_MISUSE error.
549 ** This "automatic-reset" change is not technically an incompatibility,
550 ** since any application that receives an SQLITE_MISUSE is broken by
551 ** definition.
552 **
553 ** Nevertheless, some published applications that were originally written
554 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
drhb8a45bb2011-12-31 21:51:55 +0000555 ** returns, and those were broken by the automatic-reset change. As a
drh602acb42011-01-17 17:42:37 +0000556 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
557 ** legacy behavior of returning SQLITE_MISUSE for cases where the
558 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
559 ** or SQLITE_BUSY error.
drh3674bfd2010-04-17 12:53:19 +0000560 */
drh602acb42011-01-17 17:42:37 +0000561#ifdef SQLITE_OMIT_AUTORESET
drh983b5ee2015-02-13 12:05:56 +0000562 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
drh602acb42011-01-17 17:42:37 +0000563 sqlite3_reset((sqlite3_stmt*)p);
564 }else{
565 return SQLITE_MISUSE_BKPT;
566 }
567#else
drh3674bfd2010-04-17 12:53:19 +0000568 sqlite3_reset((sqlite3_stmt*)p);
drh602acb42011-01-17 17:42:37 +0000569#endif
drhf1bfe9a2007-10-17 01:44:20 +0000570 }
571
dan14d4cc42010-01-20 14:25:37 +0000572 /* Check that malloc() has not failed. If it has, return early. */
drh17435752007-08-16 04:30:38 +0000573 db = p->db;
drhc456e572008-08-11 18:44:58 +0000574 if( db->mallocFailed ){
dan14d4cc42010-01-20 14:25:37 +0000575 p->rc = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46 +0000576 return SQLITE_NOMEM_BKPT;
drhc456e572008-08-11 18:44:58 +0000577 }
danielk1977261919c2005-12-06 12:52:59 +0000578
danielk1977a21c6b62005-01-24 10:25:59 +0000579 if( p->pc<=0 && p->expired ){
drhbee80652010-02-25 21:27:58 +0000580 p->rc = SQLITE_SCHEMA;
drhb900aaf2006-11-09 00:24:53 +0000581 rc = SQLITE_ERROR;
582 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000583 }
danielk19771d850a72004-05-31 08:26:49 +0000584 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000585 /* If there are no other statements currently running, then
586 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
587 ** from interrupting a statement that has not yet started.
588 */
drh4f7d3a52013-06-27 23:54:02 +0000589 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000590 db->u1.isInterrupted = 0;
591 }
592
drh888e16e2013-07-09 13:05:49 +0000593 assert( db->nVdbeWrite>0 || db->autoCommit==0
dancb3e4b72013-07-03 19:53:05 +0000594 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
595 );
dan1da40a32009-09-19 17:00:31 +0000596
drh19e2d372005-08-29 23:00:03 +0000597#ifndef SQLITE_OMIT_TRACE
drh3d2a5292016-07-13 22:55:01 +0000598 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
599 && !db->init.busy && p->zSql ){
drhb7e8ea22010-05-03 14:32:30 +0000600 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
drh201e0c62015-07-14 14:48:50 +0000601 }else{
602 assert( p->startTime==0 );
drh19e2d372005-08-29 23:00:03 +0000603 }
604#endif
drhc16a03b2004-09-15 13:38:10 +0000605
drh4f7d3a52013-06-27 23:54:02 +0000606 db->nVdbeActive++;
607 if( p->readOnly==0 ) db->nVdbeWrite++;
drh1713afb2013-06-28 01:24:57 +0000608 if( p->bIsReader ) db->nVdbeRead++;
danielk19771d850a72004-05-31 08:26:49 +0000609 p->pc = 0;
610 }
drh983b5ee2015-02-13 12:05:56 +0000611#ifdef SQLITE_DEBUG
612 p->rcApp = SQLITE_OK;
613#endif
drhb7f91642004-10-31 02:22:47 +0000614#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000615 if( p->explain ){
616 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000617 }else
618#endif /* SQLITE_OMIT_EXPLAIN */
619 {
drh4f7d3a52013-06-27 23:54:02 +0000620 db->nVdbeExec++;
drh4f26d6c2004-05-26 23:25:30 +0000621 rc = sqlite3VdbeExec(p);
drh4f7d3a52013-06-27 23:54:02 +0000622 db->nVdbeExec--;
drh4f26d6c2004-05-26 23:25:30 +0000623 }
624
drh19e2d372005-08-29 23:00:03 +0000625#ifndef SQLITE_OMIT_TRACE
drh201e0c62015-07-14 14:48:50 +0000626 /* If the statement completed successfully, invoke the profile callback */
627 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
drh19e2d372005-08-29 23:00:03 +0000628#endif
629
dan8d22a172010-04-19 18:03:51 +0000630 if( rc==SQLITE_DONE ){
631 assert( p->rc==SQLITE_OK );
drh7ed91f22010-04-29 22:34:07 +0000632 p->rc = doWalCallbacks(db);
dan8d22a172010-04-19 18:03:51 +0000633 if( p->rc!=SQLITE_OK ){
634 rc = SQLITE_ERROR;
635 }
636 }
637
drh80cc85b2008-07-23 21:07:25 +0000638 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000639 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
mistachkinfad30392016-02-13 23:43:46 +0000640 p->rc = SQLITE_NOMEM_BKPT;
drhb900aaf2006-11-09 00:24:53 +0000641 }
danielk1977357864e2009-03-25 15:43:08 +0000642end_of_step:
643 /* At this point local variable rc holds the value that should be
644 ** returned if this statement was compiled using the legacy
645 ** sqlite3_prepare() interface. According to the docs, this can only
646 ** be one of the values in the first assert() below. Variable p->rc
647 ** contains the value that would be returned if sqlite3_finalize()
648 ** were called on statement p.
649 */
650 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
drhcbd8db32015-08-20 17:18:32 +0000651 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
danielk1977357864e2009-03-25 15:43:08 +0000652 );
drh983b5ee2015-02-13 12:05:56 +0000653 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
drh2c2f3922017-06-01 00:54:35 +0000654 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
655 && rc!=SQLITE_ROW
656 && rc!=SQLITE_DONE
657 ){
658 /* If this statement was prepared using saved SQL and an
mistachkin48864df2013-03-21 21:20:32 +0000659 ** error has occurred, then return the error code in p->rc to the
danielk1977357864e2009-03-25 15:43:08 +0000660 ** caller. Set the error code in the database handle to the same value.
661 */
dan029ead62011-10-27 15:19:58 +0000662 rc = sqlite3VdbeTransferError(p);
danielk1977357864e2009-03-25 15:43:08 +0000663 }
664 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000665}
666
667/*
668** This is the top-level implementation of sqlite3_step(). Call
669** sqlite3Step() to do most of the work. If a schema error occurs,
670** call sqlite3Reprepare() and try again.
671*/
drhb900aaf2006-11-09 00:24:53 +0000672int sqlite3_step(sqlite3_stmt *pStmt){
drha6129fa2010-02-24 17:15:19 +0000673 int rc = SQLITE_OK; /* Result from sqlite3Step() */
674 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
675 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
676 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
677 sqlite3 *db; /* The database connection */
678
drh413c3d32010-02-23 20:11:56 +0000679 if( vdbeSafetyNotNull(v) ){
680 return SQLITE_MISUSE_BKPT;
danielk19778e556522007-11-13 10:30:24 +0000681 }
drh413c3d32010-02-23 20:11:56 +0000682 db = v->db;
683 sqlite3_mutex_enter(db->mutex);
drh37f58e92012-09-04 21:34:26 +0000684 v->doingRerun = 0;
drh413c3d32010-02-23 20:11:56 +0000685 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
drh2c7946a2014-08-19 20:27:40 +0000686 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
687 int savedPc = v->pc;
688 rc2 = rc = sqlite3Reprepare(v);
689 if( rc!=SQLITE_OK) break;
drh413c3d32010-02-23 20:11:56 +0000690 sqlite3_reset(pStmt);
drh5f58ae72014-08-19 20:41:36 +0000691 if( savedPc>=0 ) v->doingRerun = 1;
dan08f5f4c2011-06-27 19:37:23 +0000692 assert( v->expired==0 );
drh413c3d32010-02-23 20:11:56 +0000693 }
drha3cc0072013-12-13 16:23:55 +0000694 if( rc2!=SQLITE_OK ){
drh413c3d32010-02-23 20:11:56 +0000695 /* This case occurs after failing to recompile an sql statement.
696 ** The error message from the SQL compiler has already been loaded
697 ** into the database handle. This block copies the error message
698 ** from the database handle into the statement and sets the statement
699 ** program counter to 0 to ensure that when the statement is
700 ** finalized or reset the parser error message is available via
701 ** sqlite3_errmsg() and sqlite3_errcode().
702 */
703 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
704 sqlite3DbFree(db, v->zErrMsg);
705 if( !db->mallocFailed ){
706 v->zErrMsg = sqlite3DbStrDup(db, zErr);
drha6129fa2010-02-24 17:15:19 +0000707 v->rc = rc2;
drh413c3d32010-02-23 20:11:56 +0000708 } else {
709 v->zErrMsg = 0;
mistachkinfad30392016-02-13 23:43:46 +0000710 v->rc = rc = SQLITE_NOMEM_BKPT;
drh413c3d32010-02-23 20:11:56 +0000711 }
712 }
713 rc = sqlite3ApiExit(db, rc);
714 sqlite3_mutex_leave(db->mutex);
danielk197776e8d1a2006-01-18 18:22:43 +0000715 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000716}
717
drh95a7b3e2013-09-16 12:57:19 +0000718
drh4f26d6c2004-05-26 23:25:30 +0000719/*
drheb2e1762004-05-27 01:53:56 +0000720** Extract the user data from a sqlite3_context structure and return a
721** pointer to it.
722*/
723void *sqlite3_user_data(sqlite3_context *p){
724 assert( p && p->pFunc );
725 return p->pFunc->pUserData;
726}
727
728/*
drhfa4a4b92008-03-19 21:45:51 +0000729** Extract the user data from a sqlite3_context structure and return a
730** pointer to it.
drh9f129f42010-08-31 15:27:32 +0000731**
732** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
733** returns a copy of the pointer to the database connection (the 1st
734** parameter) of the sqlite3_create_function() and
735** sqlite3_create_function16() routines that originally registered the
736** application defined function.
drhfa4a4b92008-03-19 21:45:51 +0000737*/
738sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
drha46a4a62015-09-08 21:12:53 +0000739 assert( p && p->pOut );
drh9bd038f2014-08-27 14:14:06 +0000740 return p->pOut->db;
drhfa4a4b92008-03-19 21:45:51 +0000741}
742
743/*
drha9e03b12015-03-12 06:46:52 +0000744** Return the current time for a statement. If the current time
745** is requested more than once within the same run of a single prepared
746** statement, the exact same time is returned for each invocation regardless
747** of the amount of time that elapses between invocations. In other words,
748** the time returned is always the time of the first call.
drh95a7b3e2013-09-16 12:57:19 +0000749*/
750sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
drh95a7b3e2013-09-16 12:57:19 +0000751 int rc;
drha9e03b12015-03-12 06:46:52 +0000752#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
drh3df79a92015-03-12 23:48:27 +0000753 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
drha9e03b12015-03-12 06:46:52 +0000754 assert( p->pVdbe!=0 );
755#else
drh3df79a92015-03-12 23:48:27 +0000756 sqlite3_int64 iTime = 0;
drha9e03b12015-03-12 06:46:52 +0000757 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
drha9e03b12015-03-12 06:46:52 +0000758#endif
drh3df79a92015-03-12 23:48:27 +0000759 if( *piTime==0 ){
drha9e03b12015-03-12 06:46:52 +0000760 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
761 if( rc ) *piTime = 0;
drh95a7b3e2013-09-16 12:57:19 +0000762 }
drha9e03b12015-03-12 06:46:52 +0000763 return *piTime;
drh95a7b3e2013-09-16 12:57:19 +0000764}
765
766/*
drhb7481e72006-09-16 21:45:14 +0000767** The following is the implementation of an SQL function that always
768** fails with an error message stating that the function is used in the
769** wrong context. The sqlite3_overload_function() API might construct
770** SQL function that use this routine so that the functions will exist
771** for name resolution but are actually overloaded by the xFindFunction
772** method of virtual tables.
773*/
774void sqlite3InvalidFunction(
775 sqlite3_context *context, /* The function calling context */
danielk197762c14b32008-11-19 09:05:26 +0000776 int NotUsed, /* Number of arguments to the function */
777 sqlite3_value **NotUsed2 /* Value of each argument */
drhb7481e72006-09-16 21:45:14 +0000778){
779 const char *zName = context->pFunc->zName;
780 char *zErr;
danielk197762c14b32008-11-19 09:05:26 +0000781 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbc6160b2009-04-08 15:45:31 +0000782 zErr = sqlite3_mprintf(
drhb7481e72006-09-16 21:45:14 +0000783 "unable to use function %s in the requested context", zName);
784 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000785 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000786}
787
788/*
drh9de4a172014-08-23 18:17:19 +0000789** Create a new aggregate context for p and return a pointer to
790** its pMem->z element.
791*/
792static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
793 Mem *pMem = p->pMem;
794 assert( (pMem->flags & MEM_Agg)==0 );
795 if( nByte<=0 ){
drh0725cab2014-09-17 14:52:46 +0000796 sqlite3VdbeMemSetNull(pMem);
drh9de4a172014-08-23 18:17:19 +0000797 pMem->z = 0;
798 }else{
drh322f2852014-09-19 00:43:39 +0000799 sqlite3VdbeMemClearAndResize(pMem, nByte);
drh9de4a172014-08-23 18:17:19 +0000800 pMem->flags = MEM_Agg;
801 pMem->u.pDef = p->pFunc;
802 if( pMem->z ){
803 memset(pMem->z, 0, nByte);
804 }
805 }
806 return (void*)pMem->z;
807}
808
809/*
drheb2e1762004-05-27 01:53:56 +0000810** Allocate or return the aggregate context for a user function. A new
811** context is allocated on the first call. Subsequent calls return the
812** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000813*/
814void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drh2d801512016-01-14 22:19:58 +0000815 assert( p && p->pFunc && p->pFunc->xFinalize );
drh9bd038f2014-08-27 14:14:06 +0000816 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
drhd68eee02009-12-11 03:44:18 +0000817 testcase( nByte<0 );
drh9de4a172014-08-23 18:17:19 +0000818 if( (p->pMem->flags & MEM_Agg)==0 ){
819 return createAggContext(p, nByte);
820 }else{
821 return (void*)p->pMem->z;
drheb2e1762004-05-27 01:53:56 +0000822 }
drheb2e1762004-05-27 01:53:56 +0000823}
824
825/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000826** Return the auxiliary data pointer, if any, for the iArg'th argument to
danielk1977682f68b2004-06-05 10:22:17 +0000827** the user-function defined by pCtx.
drhf7fa4e72017-05-11 15:20:18 +0000828**
829** The left-most argument is 0.
830**
831** Undocumented behavior: If iArg is negative then access a cache of
832** auxiliary data pointers that is available to all functions within a
833** single prepared statement. The iArg values must match.
danielk1977682f68b2004-06-05 10:22:17 +0000834*/
835void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
dan0c547792013-07-18 17:12:08 +0000836 AuxData *pAuxData;
drhb21c8cd2007-08-21 19:33:56 +0000837
drh9bd038f2014-08-27 14:14:06 +0000838 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000839#if SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000840 if( pCtx->pVdbe==0 ) return 0;
drha9e03b12015-03-12 06:46:52 +0000841#else
842 assert( pCtx->pVdbe!=0 );
843#endif
drhe6941392017-05-10 19:42:52 +0000844 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000845 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
drhe6941392017-05-10 19:42:52 +0000846 return pAuxData->pAux;
847 }
danielk1977682f68b2004-06-05 10:22:17 +0000848 }
drhe6941392017-05-10 19:42:52 +0000849 return 0;
danielk1977682f68b2004-06-05 10:22:17 +0000850}
851
852/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000853** Set the auxiliary data pointer and delete function, for the iArg'th
danielk1977682f68b2004-06-05 10:22:17 +0000854** argument to the user-function defined by pCtx. Any previous value is
855** deleted by calling the delete function specified when it was set.
drhf7fa4e72017-05-11 15:20:18 +0000856**
857** The left-most argument is 0.
858**
859** Undocumented behavior: If iArg is negative then make the data available
860** to all functions within the current prepared statement using iArg as an
861** access code.
danielk1977682f68b2004-06-05 10:22:17 +0000862*/
863void sqlite3_set_auxdata(
864 sqlite3_context *pCtx,
865 int iArg,
866 void *pAux,
867 void (*xDelete)(void*)
868){
dan0c547792013-07-18 17:12:08 +0000869 AuxData *pAuxData;
870 Vdbe *pVdbe = pCtx->pVdbe;
danielk1977682f68b2004-06-05 10:22:17 +0000871
drh9bd038f2014-08-27 14:14:06 +0000872 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000873#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000874 if( pVdbe==0 ) goto failed;
drha9e03b12015-03-12 06:46:52 +0000875#else
876 assert( pVdbe!=0 );
877#endif
danielk1977682f68b2004-06-05 10:22:17 +0000878
drhe6941392017-05-10 19:42:52 +0000879 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000880 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
881 break;
882 }
dan0c547792013-07-18 17:12:08 +0000883 }
884 if( pAuxData==0 ){
885 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
886 if( !pAuxData ) goto failed;
drhe6941392017-05-10 19:42:52 +0000887 pAuxData->iAuxOp = pCtx->iOp;
888 pAuxData->iAuxArg = iArg;
889 pAuxData->pNextAux = pVdbe->pAuxData;
dan0c547792013-07-18 17:12:08 +0000890 pVdbe->pAuxData = pAuxData;
drh9b47ee32013-08-20 03:13:51 +0000891 if( pCtx->fErrorOrAux==0 ){
892 pCtx->isError = 0;
893 pCtx->fErrorOrAux = 1;
894 }
drhe6941392017-05-10 19:42:52 +0000895 }else if( pAuxData->xDeleteAux ){
896 pAuxData->xDeleteAux(pAuxData->pAux);
danielk1977682f68b2004-06-05 10:22:17 +0000897 }
dan0c547792013-07-18 17:12:08 +0000898
danielk1977682f68b2004-06-05 10:22:17 +0000899 pAuxData->pAux = pAux;
drhe6941392017-05-10 19:42:52 +0000900 pAuxData->xDeleteAux = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000901 return;
902
903failed:
904 if( xDelete ){
905 xDelete(pAux);
906 }
danielk1977682f68b2004-06-05 10:22:17 +0000907}
908
shaneeec556d2008-10-12 00:27:53 +0000909#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000910/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000911** Return the number of times the Step function of an aggregate has been
drheb2e1762004-05-27 01:53:56 +0000912** called.
913**
drhcf85a512006-02-09 18:35:29 +0000914** This function is deprecated. Do not use it for new code. It is
915** provide only to avoid breaking legacy code. New aggregate function
916** implementations should keep their own counts within their aggregate
917** context.
drheb2e1762004-05-27 01:53:56 +0000918*/
919int sqlite3_aggregate_count(sqlite3_context *p){
drh2d801512016-01-14 22:19:58 +0000920 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
drhabfcea22005-09-06 20:36:48 +0000921 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000922}
shaneeec556d2008-10-12 00:27:53 +0000923#endif
drheb2e1762004-05-27 01:53:56 +0000924
925/*
drh4f26d6c2004-05-26 23:25:30 +0000926** Return the number of columns in the result set for the statement pStmt.
927*/
928int sqlite3_column_count(sqlite3_stmt *pStmt){
929 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000930 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000931}
932
933/*
934** Return the number of values available from the current row of the
935** currently executing statement pStmt.
936*/
937int sqlite3_data_count(sqlite3_stmt *pStmt){
938 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000939 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000940 return pVm->nResColumn;
941}
942
dan46c47d42011-03-01 18:42:07 +0000943/*
944** Return a pointer to static memory containing an SQL NULL value.
945*/
946static const Mem *columnNullValue(void){
947 /* Even though the Mem structure contains an element
drhb1a1c292014-03-05 12:47:55 +0000948 ** of type i64, on certain architectures (x86) with certain compiler
dan46c47d42011-03-01 18:42:07 +0000949 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
950 ** instead of an 8-byte one. This all works fine, except that when
951 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
952 ** that a Mem structure is located on an 8-byte boundary. To prevent
drhb1a1c292014-03-05 12:47:55 +0000953 ** these assert()s from failing, when building with SQLITE_DEBUG defined
954 ** using gcc, we force nullMem to be 8-byte aligned using the magical
dan46c47d42011-03-01 18:42:07 +0000955 ** __attribute__((aligned(8))) macro. */
956 static const Mem nullMem
957#if defined(SQLITE_DEBUG) && defined(__GNUC__)
drhb1a1c292014-03-05 12:47:55 +0000958 __attribute__((aligned(8)))
dan46c47d42011-03-01 18:42:07 +0000959#endif
drh035e5632014-09-16 14:16:31 +0000960 = {
drh3329a632014-09-18 01:21:43 +0000961 /* .u = */ {0},
drh8faee872015-09-19 18:08:13 +0000962 /* .flags = */ (u16)MEM_Null,
963 /* .enc = */ (u8)0,
964 /* .eSubtype = */ (u8)0,
965 /* .n = */ (int)0,
966 /* .z = */ (char*)0,
967 /* .zMalloc = */ (char*)0,
968 /* .szMalloc = */ (int)0,
969 /* .uTemp = */ (u32)0,
970 /* .db = */ (sqlite3*)0,
971 /* .xDel = */ (void(*)(void*))0,
drhb1a1c292014-03-05 12:47:55 +0000972#ifdef SQLITE_DEBUG
drh8faee872015-09-19 18:08:13 +0000973 /* .pScopyFrom = */ (Mem*)0,
974 /* .pFiller = */ (void*)0,
drhb1a1c292014-03-05 12:47:55 +0000975#endif
drh035e5632014-09-16 14:16:31 +0000976 };
dan46c47d42011-03-01 18:42:07 +0000977 return &nullMem;
978}
drh4f26d6c2004-05-26 23:25:30 +0000979
980/*
981** Check to see if column iCol of the given statement is valid. If
982** it is, return a pointer to the Mem for the value of that column.
983** If iCol is not valid, return a pointer to a Mem which has a value
984** of NULL.
985*/
986static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000987 Vdbe *pVm;
drh32bc3f62007-08-21 20:25:39 +0000988 Mem *pOut;
989
990 pVm = (Vdbe *)pStmt;
drh28f17012016-09-22 21:37:18 +0000991 if( pVm==0 ) return (Mem*)columnNullValue();
992 assert( pVm->db );
993 sqlite3_mutex_enter(pVm->db->mutex);
994 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drhd4e70eb2008-01-02 00:34:36 +0000995 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000996 }else{
drh28f17012016-09-22 21:37:18 +0000997 sqlite3Error(pVm->db, SQLITE_RANGE);
dan46c47d42011-03-01 18:42:07 +0000998 pOut = (Mem*)columnNullValue();
drh4f26d6c2004-05-26 23:25:30 +0000999 }
drh32bc3f62007-08-21 20:25:39 +00001000 return pOut;
drh4f26d6c2004-05-26 23:25:30 +00001001}
1002
danielk19772e588c72005-12-09 14:25:08 +00001003/*
1004** This function is called after invoking an sqlite3_value_XXX function on a
1005** column value (i.e. a value returned by evaluating an SQL expression in the
1006** select list of a SELECT statement) that may cause a malloc() failure. If
1007** malloc() has failed, the threads mallocFailed flag is cleared and the result
1008** code of statement pStmt set to SQLITE_NOMEM.
1009**
drh32bc3f62007-08-21 20:25:39 +00001010** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +00001011**
1012** sqlite3_column_int()
1013** sqlite3_column_int64()
1014** sqlite3_column_text()
1015** sqlite3_column_text16()
1016** sqlite3_column_real()
1017** sqlite3_column_bytes()
1018** sqlite3_column_bytes16()
drh42262532010-09-08 16:30:36 +00001019** sqiite3_column_blob()
danielk19772e588c72005-12-09 14:25:08 +00001020*/
1021static void columnMallocFailure(sqlite3_stmt *pStmt)
1022{
1023 /* If malloc() failed during an encoding conversion within an
1024 ** sqlite3_column_XXX API, then set the return code of the statement to
1025 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
1026 ** and _finalize() will return NOMEM.
1027 */
danielk197754f01982006-01-18 15:25:17 +00001028 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +00001029 if( p ){
drh28f17012016-09-22 21:37:18 +00001030 assert( p->db!=0 );
1031 assert( sqlite3_mutex_held(p->db->mutex) );
drh32bc3f62007-08-21 20:25:39 +00001032 p->rc = sqlite3ApiExit(p->db, p->rc);
1033 sqlite3_mutex_leave(p->db->mutex);
1034 }
danielk19772e588c72005-12-09 14:25:08 +00001035}
1036
drh4f26d6c2004-05-26 23:25:30 +00001037/**************************** sqlite3_column_ *******************************
1038** The following routines are used to access elements of the current row
1039** in the result set.
1040*/
danielk1977c572ef72004-05-27 09:28:41 +00001041const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001042 const void *val;
danielk19772e588c72005-12-09 14:25:08 +00001043 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +00001044 /* Even though there is no encoding conversion, value_blob() might
1045 ** need to call malloc() to expand the result of a zeroblob()
1046 ** expression.
1047 */
1048 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +00001049 return val;
danielk1977c572ef72004-05-27 09:28:41 +00001050}
drh4f26d6c2004-05-26 23:25:30 +00001051int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001052 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
1053 columnMallocFailure(pStmt);
1054 return val;
drh4f26d6c2004-05-26 23:25:30 +00001055}
1056int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001057 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
1058 columnMallocFailure(pStmt);
1059 return val;
drh4f26d6c2004-05-26 23:25:30 +00001060}
1061double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001062 double val = sqlite3_value_double( columnMem(pStmt,i) );
1063 columnMallocFailure(pStmt);
1064 return val;
drh4f26d6c2004-05-26 23:25:30 +00001065}
1066int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001067 int val = sqlite3_value_int( columnMem(pStmt,i) );
1068 columnMallocFailure(pStmt);
1069 return val;
drh4f26d6c2004-05-26 23:25:30 +00001070}
drhefad9992004-06-22 12:13:55 +00001071sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001072 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
1073 columnMallocFailure(pStmt);
1074 return val;
drh4f26d6c2004-05-26 23:25:30 +00001075}
1076const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001077 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
1078 columnMallocFailure(pStmt);
1079 return val;
drh4f26d6c2004-05-26 23:25:30 +00001080}
drhd1e47332005-06-26 17:55:33 +00001081sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +00001082 Mem *pOut = columnMem(pStmt, i);
1083 if( pOut->flags&MEM_Static ){
1084 pOut->flags &= ~MEM_Static;
1085 pOut->flags |= MEM_Ephem;
1086 }
drh32bc3f62007-08-21 20:25:39 +00001087 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +00001088 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +00001089}
drh6c626082004-11-14 21:56:29 +00001090#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001091const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001092 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
1093 columnMallocFailure(pStmt);
1094 return val;
drh4f26d6c2004-05-26 23:25:30 +00001095}
drh6c626082004-11-14 21:56:29 +00001096#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +00001097int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +00001098 int iType = sqlite3_value_type( columnMem(pStmt,i) );
1099 columnMallocFailure(pStmt);
1100 return iType;
drh4f26d6c2004-05-26 23:25:30 +00001101}
1102
drh5e2517e2004-09-24 23:59:12 +00001103/*
1104** Convert the N-th element of pStmt->pColName[] into a string using
1105** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +00001106**
1107** There are up to 5 names for each column. useType determines which
1108** name is returned. Here are the names:
1109**
1110** 0 The column name as it should be displayed for output
1111** 1 The datatype name for the column
1112** 2 The name of the database that the column derives from
1113** 3 The name of the table that the column derives from
1114** 4 The name of the table column that the result column derives from
1115**
1116** If the result is not a simple column reference (if it is an expression
1117** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +00001118*/
1119static const void *columnName(
1120 sqlite3_stmt *pStmt,
1121 int N,
1122 const void *(*xFunc)(Mem*),
1123 int useType
1124){
drh9ca95732014-10-24 00:35:58 +00001125 const void *ret;
1126 Vdbe *p;
drh32bc3f62007-08-21 20:25:39 +00001127 int n;
drh9ca95732014-10-24 00:35:58 +00001128 sqlite3 *db;
1129#ifdef SQLITE_ENABLE_API_ARMOR
1130 if( pStmt==0 ){
1131 (void)SQLITE_MISUSE_BKPT;
1132 return 0;
1133 }
1134#endif
1135 ret = 0;
1136 p = (Vdbe *)pStmt;
1137 db = p->db;
drh9b4ea4a2009-04-10 20:32:00 +00001138 assert( db!=0 );
1139 n = sqlite3_column_count(pStmt);
1140 if( N<n && N>=0 ){
1141 N += useType*n;
1142 sqlite3_mutex_enter(db->mutex);
1143 assert( db->mallocFailed==0 );
1144 ret = xFunc(&p->aColName[N]);
1145 /* A malloc may have failed inside of the xFunc() call. If this
1146 ** is the case, clear the mallocFailed flag and return NULL.
1147 */
1148 if( db->mallocFailed ){
drh4a642b62016-02-05 01:55:27 +00001149 sqlite3OomClear(db);
drh9b4ea4a2009-04-10 20:32:00 +00001150 ret = 0;
drh32bc3f62007-08-21 20:25:39 +00001151 }
drh9b4ea4a2009-04-10 20:32:00 +00001152 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +00001153 }
danielk197700fd9572005-12-07 06:27:43 +00001154 return ret;
drh5e2517e2004-09-24 23:59:12 +00001155}
1156
drh4f26d6c2004-05-26 23:25:30 +00001157/*
1158** Return the name of the Nth column of the result set returned by SQL
1159** statement pStmt.
1160*/
1161const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001162 return columnName(
1163 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +00001164}
drhe590fbd2005-05-20 19:36:01 +00001165#ifndef SQLITE_OMIT_UTF16
1166const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001167 return columnName(
1168 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +00001169}
1170#endif
drh4f26d6c2004-05-26 23:25:30 +00001171
1172/*
drh3f913572008-03-22 01:07:17 +00001173** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1174** not define OMIT_DECLTYPE.
1175*/
1176#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
1177# error "Must not define both SQLITE_OMIT_DECLTYPE \
1178 and SQLITE_ENABLE_COLUMN_METADATA"
1179#endif
1180
1181#ifndef SQLITE_OMIT_DECLTYPE
1182/*
drh6c626082004-11-14 21:56:29 +00001183** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +00001184** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +00001185*/
1186const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001187 return columnName(
1188 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +00001189}
drh6c626082004-11-14 21:56:29 +00001190#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +00001191const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001192 return columnName(
1193 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +00001194}
drh6c626082004-11-14 21:56:29 +00001195#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +00001196#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +00001197
danielk19774b1ae992006-02-10 03:06:10 +00001198#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +00001199/*
1200** Return the name of the database from which a result column derives.
1201** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001202** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001203*/
1204const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001205 return columnName(
1206 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001207}
1208#ifndef SQLITE_OMIT_UTF16
1209const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001210 return columnName(
1211 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001212}
1213#endif /* SQLITE_OMIT_UTF16 */
1214
1215/*
1216** Return the name of the table from which a result column derives.
1217** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001218** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001219*/
1220const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001221 return columnName(
1222 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001223}
1224#ifndef SQLITE_OMIT_UTF16
1225const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001226 return columnName(
1227 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001228}
1229#endif /* SQLITE_OMIT_UTF16 */
1230
1231/*
1232** Return the name of the table column from which a result column derives.
1233** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001234** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001235*/
1236const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001237 return columnName(
1238 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001239}
1240#ifndef SQLITE_OMIT_UTF16
1241const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001242 return columnName(
1243 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001244}
1245#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +00001246#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +00001247
1248
drh4f26d6c2004-05-26 23:25:30 +00001249/******************************* sqlite3_bind_ ***************************
1250**
1251** Routines used to attach values to wildcards in a compiled SQL statement.
1252*/
1253/*
1254** Unbind the value bound to variable i in virtual machine p. This is the
1255** the same as binding a NULL value to the column. If the "i" parameter is
1256** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1257**
drh488e7b62008-10-06 12:46:43 +00001258** A successful evaluation of this routine acquires the mutex on p.
1259** the mutex is released if any kind of error occurs.
1260**
drh4f26d6c2004-05-26 23:25:30 +00001261** The error code stored in database p->db is overwritten with the return
1262** value in any case.
1263*/
1264static int vdbeUnbind(Vdbe *p, int i){
1265 Mem *pVar;
drh413c3d32010-02-23 20:11:56 +00001266 if( vdbeSafetyNotNull(p) ){
1267 return SQLITE_MISUSE_BKPT;
1268 }
drh488e7b62008-10-06 12:46:43 +00001269 sqlite3_mutex_enter(p->db->mutex);
1270 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh13f40da2014-08-22 18:00:11 +00001271 sqlite3Error(p->db, SQLITE_MISUSE);
drh488e7b62008-10-06 12:46:43 +00001272 sqlite3_mutex_leave(p->db->mutex);
drh413c3d32010-02-23 20:11:56 +00001273 sqlite3_log(SQLITE_MISUSE,
1274 "bind on a busy prepared statement: [%s]", p->zSql);
1275 return SQLITE_MISUSE_BKPT;
drh4f26d6c2004-05-26 23:25:30 +00001276 }
1277 if( i<1 || i>p->nVar ){
drh13f40da2014-08-22 18:00:11 +00001278 sqlite3Error(p->db, SQLITE_RANGE);
drh488e7b62008-10-06 12:46:43 +00001279 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001280 return SQLITE_RANGE;
1281 }
1282 i--;
drh290c1942004-08-21 17:54:45 +00001283 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001284 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001285 pVar->flags = MEM_Null;
drh13f40da2014-08-22 18:00:11 +00001286 sqlite3Error(p->db, SQLITE_OK);
dan937d0de2009-10-15 18:35:38 +00001287
dan1d2ce4f2009-10-19 18:11:09 +00001288 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1289 ** binding a new value to this variable invalidates the current query plan.
drha7044002010-09-14 18:22:59 +00001290 **
1291 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1292 ** parameter in the WHERE clause might influence the choice of query plan
1293 ** for a statement, then the statement will be automatically recompiled,
1294 ** as if there had been a schema change, on the first sqlite3_step() call
1295 ** following any change to the bindings of that parameter.
dan1d2ce4f2009-10-19 18:11:09 +00001296 */
drh2c2f3922017-06-01 00:54:35 +00001297 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
drh29967962017-03-03 21:51:40 +00001298 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
dan937d0de2009-10-15 18:35:38 +00001299 p->expired = 1;
1300 }
drh4f26d6c2004-05-26 23:25:30 +00001301 return SQLITE_OK;
1302}
1303
1304/*
drh5e2517e2004-09-24 23:59:12 +00001305** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001306*/
drh5e2517e2004-09-24 23:59:12 +00001307static int bindText(
drh605264d2007-08-21 15:13:19 +00001308 sqlite3_stmt *pStmt, /* The statement to bind against */
1309 int i, /* Index of the parameter to bind */
1310 const void *zData, /* Pointer to the data to be bound */
1311 int nData, /* Number of bytes of data to be bound */
1312 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +00001313 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001314){
1315 Vdbe *p = (Vdbe *)pStmt;
1316 Mem *pVar;
1317 int rc;
1318
1319 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001320 if( rc==SQLITE_OK ){
1321 if( zData!=0 ){
1322 pVar = &p->aVar[i-1];
1323 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1324 if( rc==SQLITE_OK && encoding!=0 ){
1325 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1326 }
drhe70d01f2017-05-29 22:44:18 +00001327 if( rc ){
1328 sqlite3Error(p->db, rc);
1329 rc = sqlite3ApiExit(p->db, rc);
1330 }
drh27641702007-08-22 02:56:42 +00001331 }
drh488e7b62008-10-06 12:46:43 +00001332 sqlite3_mutex_leave(p->db->mutex);
drh94bb2ba2010-10-14 01:16:32 +00001333 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
drh6fec9ee2010-10-12 02:13:32 +00001334 xDel((void*)zData);
drh4f26d6c2004-05-26 23:25:30 +00001335 }
drh605264d2007-08-21 15:13:19 +00001336 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001337}
drh5e2517e2004-09-24 23:59:12 +00001338
1339
1340/*
1341** Bind a blob value to an SQL statement variable.
1342*/
1343int sqlite3_bind_blob(
1344 sqlite3_stmt *pStmt,
1345 int i,
1346 const void *zData,
1347 int nData,
1348 void (*xDel)(void*)
1349){
drh5b081d82016-02-18 01:29:12 +00001350#ifdef SQLITE_ENABLE_API_ARMOR
1351 if( nData<0 ) return SQLITE_MISUSE_BKPT;
1352#endif
drh5e2517e2004-09-24 23:59:12 +00001353 return bindText(pStmt, i, zData, nData, xDel, 0);
1354}
drhda4ca9d2014-09-09 17:27:35 +00001355int sqlite3_bind_blob64(
1356 sqlite3_stmt *pStmt,
1357 int i,
1358 const void *zData,
1359 sqlite3_uint64 nData,
1360 void (*xDel)(void*)
1361){
drhfc59a952014-09-11 23:34:55 +00001362 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001363 if( nData>0x7fffffff ){
1364 return invokeValueDestructor(zData, xDel, 0);
1365 }else{
drh3329a632014-09-18 01:21:43 +00001366 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
drhda4ca9d2014-09-09 17:27:35 +00001367 }
1368}
drh4f26d6c2004-05-26 23:25:30 +00001369int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1370 int rc;
1371 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +00001372 rc = vdbeUnbind(p, i);
1373 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001374 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +00001375 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001376 }
danielk1977f4618892004-06-28 13:09:11 +00001377 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001378}
1379int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +00001380 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001381}
drhefad9992004-06-22 12:13:55 +00001382int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001383 int rc;
1384 Vdbe *p = (Vdbe *)pStmt;
1385 rc = vdbeUnbind(p, i);
1386 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001387 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +00001388 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001389 }
1390 return rc;
1391}
drh605264d2007-08-21 15:13:19 +00001392int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1393 int rc;
1394 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +00001395 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001396 if( rc==SQLITE_OK ){
1397 sqlite3_mutex_leave(p->db->mutex);
1398 }
drh605264d2007-08-21 15:13:19 +00001399 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001400}
drhae3ec3f2017-07-17 00:40:19 +00001401int sqlite3_bind_pointer(sqlite3_stmt *pStmt, int i, void *pPtr,const char *zT){
drh3a96a5d2017-06-30 23:09:03 +00001402 int rc;
1403 Vdbe *p = (Vdbe*)pStmt;
1404 rc = vdbeUnbind(p, i);
1405 if( rc==SQLITE_OK ){
drhae3ec3f2017-07-17 00:40:19 +00001406 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zT);
drh3a96a5d2017-06-30 23:09:03 +00001407 sqlite3_mutex_leave(p->db->mutex);
1408 }
1409 return rc;
1410}
drh4f26d6c2004-05-26 23:25:30 +00001411int sqlite3_bind_text(
1412 sqlite3_stmt *pStmt,
1413 int i,
1414 const char *zData,
1415 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001416 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001417){
drh5e2517e2004-09-24 23:59:12 +00001418 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001419}
drhbbf483f2014-09-09 20:30:24 +00001420int sqlite3_bind_text64(
drhda4ca9d2014-09-09 17:27:35 +00001421 sqlite3_stmt *pStmt,
1422 int i,
1423 const char *zData,
1424 sqlite3_uint64 nData,
1425 void (*xDel)(void*),
1426 unsigned char enc
1427){
drhfc59a952014-09-11 23:34:55 +00001428 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001429 if( nData>0x7fffffff ){
1430 return invokeValueDestructor(zData, xDel, 0);
1431 }else{
drhfc59a952014-09-11 23:34:55 +00001432 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drh3329a632014-09-18 01:21:43 +00001433 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
drhda4ca9d2014-09-09 17:27:35 +00001434 }
1435}
drh6c626082004-11-14 21:56:29 +00001436#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001437int sqlite3_bind_text16(
1438 sqlite3_stmt *pStmt,
1439 int i,
1440 const void *zData,
1441 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001442 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001443){
drh5e2517e2004-09-24 23:59:12 +00001444 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001445}
drh6c626082004-11-14 21:56:29 +00001446#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001447int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1448 int rc;
drh1b27b8c2014-02-10 03:21:57 +00001449 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
drh29def562009-04-14 12:43:33 +00001450 case SQLITE_INTEGER: {
1451 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1452 break;
1453 }
1454 case SQLITE_FLOAT: {
drh74eaba42014-09-18 17:52:15 +00001455 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
drh29def562009-04-14 12:43:33 +00001456 break;
1457 }
1458 case SQLITE_BLOB: {
1459 if( pValue->flags & MEM_Zero ){
1460 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1461 }else{
1462 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1463 }
1464 break;
1465 }
1466 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001467 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1468 pValue->enc);
1469 break;
1470 }
1471 default: {
1472 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001473 break;
1474 }
danielk197726e41442006-06-14 15:16:35 +00001475 }
1476 return rc;
1477}
drhb026e052007-05-02 01:34:31 +00001478int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1479 int rc;
1480 Vdbe *p = (Vdbe *)pStmt;
1481 rc = vdbeUnbind(p, i);
1482 if( rc==SQLITE_OK ){
1483 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001484 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001485 }
1486 return rc;
1487}
dan80c03022015-07-24 17:36:34 +00001488int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
1489 int rc;
1490 Vdbe *p = (Vdbe *)pStmt;
1491 sqlite3_mutex_enter(p->db->mutex);
1492 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
1493 rc = SQLITE_TOOBIG;
1494 }else{
1495 assert( (n & 0x7FFFFFFF)==n );
1496 rc = sqlite3_bind_zeroblob(pStmt, i, n);
1497 }
1498 rc = sqlite3ApiExit(p->db, rc);
1499 sqlite3_mutex_leave(p->db->mutex);
1500 return rc;
1501}
drh75f6a032004-07-15 14:15:00 +00001502
1503/*
1504** Return the number of wildcards that can be potentially bound to.
1505** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001506*/
1507int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001508 Vdbe *p = (Vdbe*)pStmt;
1509 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001510}
drh895d7472004-08-20 16:02:39 +00001511
1512/*
drhfa6bc002004-09-07 16:19:52 +00001513** Return the name of a wildcard parameter. Return NULL if the index
1514** is out of range or if the wildcard is unnamed.
1515**
1516** The result is always UTF-8.
1517*/
1518const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1519 Vdbe *p = (Vdbe*)pStmt;
drh9bf755c2016-12-23 03:59:31 +00001520 if( p==0 ) return 0;
1521 return sqlite3VListNumToName(p->pVList, i);
drh895d7472004-08-20 16:02:39 +00001522}
drhfa6bc002004-09-07 16:19:52 +00001523
1524/*
1525** Given a wildcard parameter name, return the index of the variable
1526** with that name. If there is no variable with the given name,
1527** return 0.
1528*/
drh5f18a222009-11-26 14:01:53 +00001529int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
drh9bf755c2016-12-23 03:59:31 +00001530 if( p==0 || zName==0 ) return 0;
1531 return sqlite3VListNameToNum(p->pVList, zName, nName);
drhfa6bc002004-09-07 16:19:52 +00001532}
drh5f18a222009-11-26 14:01:53 +00001533int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1534 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1535}
drhf8db1bc2005-04-22 02:38:37 +00001536
drh51942bc2005-06-12 22:01:42 +00001537/*
drhf8db1bc2005-04-22 02:38:37 +00001538** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001539*/
shane145834a2008-09-04 12:03:42 +00001540int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001541 Vdbe *pFrom = (Vdbe*)pFromStmt;
1542 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001543 int i;
1544 assert( pTo->db==pFrom->db );
1545 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001546 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001547 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001548 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001549 }
drh27641702007-08-22 02:56:42 +00001550 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001551 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001552}
drh51942bc2005-06-12 22:01:42 +00001553
shaneeec556d2008-10-12 00:27:53 +00001554#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001555/*
shane145834a2008-09-04 12:03:42 +00001556** Deprecated external interface. Internal/core SQLite code
1557** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001558**
peter.d.reid60ec9142014-09-06 16:39:46 +00001559** It is misuse to call this routine with statements from different
drh0f5ea0b2009-04-09 14:02:44 +00001560** database connections. But as this is a deprecated interface, we
1561** will not bother to check for that condition.
1562**
1563** If the two statements contain a different number of bindings, then
1564** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1565** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001566*/
1567int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001568 Vdbe *pFrom = (Vdbe*)pFromStmt;
1569 Vdbe *pTo = (Vdbe*)pToStmt;
1570 if( pFrom->nVar!=pTo->nVar ){
1571 return SQLITE_ERROR;
1572 }
drh2c2f3922017-06-01 00:54:35 +00001573 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001574 if( pTo->expmask ){
danc94b8592009-10-20 07:01:24 +00001575 pTo->expired = 1;
1576 }
drh2c2f3922017-06-01 00:54:35 +00001577 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001578 if( pFrom->expmask ){
danc94b8592009-10-20 07:01:24 +00001579 pFrom->expired = 1;
1580 }
shane145834a2008-09-04 12:03:42 +00001581 return sqlite3TransferBindings(pFromStmt, pToStmt);
1582}
shaneeec556d2008-10-12 00:27:53 +00001583#endif
shane145834a2008-09-04 12:03:42 +00001584
1585/*
drh51942bc2005-06-12 22:01:42 +00001586** Return the sqlite3* database handle to which the prepared statement given
1587** in the argument belongs. This is the same database handle that was
1588** the first argument to the sqlite3_prepare() that was used to create
1589** the statement in the first place.
1590*/
1591sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1592 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1593}
drhbb5a9c32008-06-19 02:52:25 +00001594
1595/*
drhf03d9cc2010-11-16 23:10:25 +00001596** Return true if the prepared statement is guaranteed to not modify the
1597** database.
1598*/
1599int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1600 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1601}
1602
1603/*
drh2fb66932011-11-25 17:21:47 +00001604** Return true if the prepared statement is in need of being reset.
1605*/
1606int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
1607 Vdbe *v = (Vdbe*)pStmt;
drh76336d52016-10-01 11:39:53 +00001608 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
drh2fb66932011-11-25 17:21:47 +00001609}
1610
1611/*
drhbb5a9c32008-06-19 02:52:25 +00001612** Return a pointer to the next prepared statement after pStmt associated
1613** with database connection pDb. If pStmt is NULL, return the first
1614** prepared statement for the database connection. Return NULL if there
1615** are no more.
1616*/
1617sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1618 sqlite3_stmt *pNext;
drh9ca95732014-10-24 00:35:58 +00001619#ifdef SQLITE_ENABLE_API_ARMOR
1620 if( !sqlite3SafetyCheckOk(pDb) ){
1621 (void)SQLITE_MISUSE_BKPT;
1622 return 0;
1623 }
1624#endif
drhbb5a9c32008-06-19 02:52:25 +00001625 sqlite3_mutex_enter(pDb->mutex);
1626 if( pStmt==0 ){
1627 pNext = (sqlite3_stmt*)pDb->pVdbe;
1628 }else{
1629 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1630 }
1631 sqlite3_mutex_leave(pDb->mutex);
1632 return pNext;
1633}
drhd1d38482008-10-07 23:46:38 +00001634
1635/*
1636** Return the value of a status counter for a prepared statement
1637*/
1638int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1639 Vdbe *pVdbe = (Vdbe*)pStmt;
drh9ca95732014-10-24 00:35:58 +00001640 u32 v;
1641#ifdef SQLITE_ENABLE_API_ARMOR
1642 if( !pStmt ){
1643 (void)SQLITE_MISUSE_BKPT;
1644 return 0;
1645 }
1646#endif
drh3528f6b2017-05-31 16:21:54 +00001647 if( op==SQLITE_STMTSTATUS_MEMUSED ){
1648 sqlite3 *db = pVdbe->db;
1649 sqlite3_mutex_enter(db->mutex);
1650 v = 0;
1651 db->pnBytesFreed = (int*)&v;
1652 sqlite3VdbeClearObject(db, pVdbe);
1653 sqlite3DbFree(db, pVdbe);
1654 db->pnBytesFreed = 0;
1655 sqlite3_mutex_leave(db->mutex);
1656 }else{
1657 v = pVdbe->aCounter[op];
1658 if( resetFlag ) pVdbe->aCounter[op] = 0;
1659 }
drh9b47ee32013-08-20 03:13:51 +00001660 return (int)v;
drhd1d38482008-10-07 23:46:38 +00001661}
dan46c47d42011-03-01 18:42:07 +00001662
drh557341e2016-07-23 02:07:26 +00001663/*
1664** Return the SQL associated with a prepared statement
1665*/
1666const char *sqlite3_sql(sqlite3_stmt *pStmt){
1667 Vdbe *p = (Vdbe *)pStmt;
1668 return p ? p->zSql : 0;
1669}
1670
1671/*
1672** Return the SQL associated with a prepared statement with
1673** bound parameters expanded. Space to hold the returned string is
1674** obtained from sqlite3_malloc(). The caller is responsible for
1675** freeing the returned string by passing it to sqlite3_free().
1676**
1677** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1678** expanded bound parameters.
1679*/
1680char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1681#ifdef SQLITE_OMIT_TRACE
1682 return 0;
1683#else
1684 char *z = 0;
1685 const char *zSql = sqlite3_sql(pStmt);
1686 if( zSql ){
1687 Vdbe *p = (Vdbe *)pStmt;
1688 sqlite3_mutex_enter(p->db->mutex);
1689 z = sqlite3VdbeExpandSql(p, zSql);
1690 sqlite3_mutex_leave(p->db->mutex);
1691 }
1692 return z;
1693#endif
1694}
1695
drh9b1c62d2011-03-30 21:04:43 +00001696#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001697/*
dan93bca692011-09-14 19:41:44 +00001698** Allocate and populate an UnpackedRecord structure based on the serialized
1699** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
1700** if successful, or a NULL pointer if an OOM error is encountered.
1701*/
1702static UnpackedRecord *vdbeUnpackRecord(
1703 KeyInfo *pKeyInfo,
1704 int nKey,
1705 const void *pKey
1706){
dan93bca692011-09-14 19:41:44 +00001707 UnpackedRecord *pRet; /* Return value */
1708
drha582b012016-12-21 19:45:54 +00001709 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
dan93bca692011-09-14 19:41:44 +00001710 if( pRet ){
drhc8d985e2013-12-14 18:24:46 +00001711 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
dan93bca692011-09-14 19:41:44 +00001712 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
1713 }
1714 return pRet;
1715}
1716
1717/*
dan37db03b2011-03-16 19:59:18 +00001718** This function is called from within a pre-update callback to retrieve
1719** a field of the row currently being updated or deleted.
1720*/
dan46c47d42011-03-01 18:42:07 +00001721int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
1722 PreUpdate *p = db->pPreUpdate;
dan7271d7a2017-01-25 18:53:27 +00001723 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001724 int rc = SQLITE_OK;
1725
dan37db03b2011-03-16 19:59:18 +00001726 /* Test that this call is being made from within an SQLITE_DELETE or
1727 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
dan46c47d42011-03-01 18:42:07 +00001728 if( !p || p->op==SQLITE_INSERT ){
1729 rc = SQLITE_MISUSE_BKPT;
1730 goto preupdate_old_out;
1731 }
dancb9a3642017-01-30 19:44:53 +00001732 if( p->pPk ){
1733 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1734 }
dan46c47d42011-03-01 18:42:07 +00001735 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1736 rc = SQLITE_RANGE;
1737 goto preupdate_old_out;
1738 }
1739
dan37db03b2011-03-16 19:59:18 +00001740 /* If the old.* record has not yet been loaded into memory, do so now. */
dan46c47d42011-03-01 18:42:07 +00001741 if( p->pUnpacked==0 ){
dan12ca0b52011-03-21 16:17:42 +00001742 u32 nRec;
1743 u8 *aRec;
dan46c47d42011-03-01 18:42:07 +00001744
drha7c90c42016-06-04 20:37:10 +00001745 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
dan12ca0b52011-03-21 16:17:42 +00001746 aRec = sqlite3DbMallocRaw(db, nRec);
1747 if( !aRec ) goto preupdate_old_out;
drhcb3cabd2016-11-25 19:18:28 +00001748 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001749 if( rc==SQLITE_OK ){
dan93bca692011-09-14 19:41:44 +00001750 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001751 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
1752 }
dan46c47d42011-03-01 18:42:07 +00001753 if( rc!=SQLITE_OK ){
dan12ca0b52011-03-21 16:17:42 +00001754 sqlite3DbFree(db, aRec);
dan46c47d42011-03-01 18:42:07 +00001755 goto preupdate_old_out;
1756 }
dan12ca0b52011-03-21 16:17:42 +00001757 p->aRecord = aRec;
dan46c47d42011-03-01 18:42:07 +00001758 }
1759
dan7271d7a2017-01-25 18:53:27 +00001760 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1761 if( iIdx==p->pTab->iPKey ){
1762 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
1763 }else if( iIdx>=p->pUnpacked->nField ){
dan46c47d42011-03-01 18:42:07 +00001764 *ppValue = (sqlite3_value *)columnNullValue();
dan7271d7a2017-01-25 18:53:27 +00001765 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1766 if( pMem->flags & MEM_Int ){
1767 sqlite3VdbeMemRealify(pMem);
dan319eeb72011-03-19 08:38:50 +00001768 }
dan46c47d42011-03-01 18:42:07 +00001769 }
1770
1771 preupdate_old_out:
drhe1ed0b02014-08-26 02:15:07 +00001772 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001773 return sqlite3ApiExit(db, rc);
1774}
drh9b1c62d2011-03-30 21:04:43 +00001775#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001776
drh9b1c62d2011-03-30 21:04:43 +00001777#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001778/*
1779** This function is called from within a pre-update callback to retrieve
1780** the number of columns in the row being updated, deleted or inserted.
1781*/
dan46c47d42011-03-01 18:42:07 +00001782int sqlite3_preupdate_count(sqlite3 *db){
1783 PreUpdate *p = db->pPreUpdate;
dane437ca52011-07-11 19:45:38 +00001784 return (p ? p->keyinfo.nField : 0);
dan46c47d42011-03-01 18:42:07 +00001785}
drh9b1c62d2011-03-30 21:04:43 +00001786#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001787
drh9b1c62d2011-03-30 21:04:43 +00001788#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001789/*
dan1e7a2d42011-03-22 18:45:29 +00001790** This function is designed to be called from within a pre-update callback
1791** only. It returns zero if the change that caused the callback was made
1792** immediately by a user SQL statement. Or, if the change was made by a
1793** trigger program, it returns the number of trigger programs currently
1794** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
1795** top-level trigger etc.).
1796**
1797** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
1798** or SET DEFAULT action is considered a trigger.
1799*/
1800int sqlite3_preupdate_depth(sqlite3 *db){
1801 PreUpdate *p = db->pPreUpdate;
1802 return (p ? p->v->nFrame : 0);
1803}
drh9b1c62d2011-03-30 21:04:43 +00001804#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan1e7a2d42011-03-22 18:45:29 +00001805
drh9b1c62d2011-03-30 21:04:43 +00001806#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan1e7a2d42011-03-22 18:45:29 +00001807/*
dan37db03b2011-03-16 19:59:18 +00001808** This function is called from within a pre-update callback to retrieve
1809** a field of the row currently being updated or inserted.
1810*/
1811int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
dan46c47d42011-03-01 18:42:07 +00001812 PreUpdate *p = db->pPreUpdate;
1813 int rc = SQLITE_OK;
dan37db03b2011-03-16 19:59:18 +00001814 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001815
dan37db03b2011-03-16 19:59:18 +00001816 if( !p || p->op==SQLITE_DELETE ){
dan46c47d42011-03-01 18:42:07 +00001817 rc = SQLITE_MISUSE_BKPT;
dan37db03b2011-03-16 19:59:18 +00001818 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001819 }
dancb9a3642017-01-30 19:44:53 +00001820 if( p->pPk && p->op!=SQLITE_UPDATE ){
1821 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1822 }
dan46c47d42011-03-01 18:42:07 +00001823 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1824 rc = SQLITE_RANGE;
dan37db03b2011-03-16 19:59:18 +00001825 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001826 }
dan46c47d42011-03-01 18:42:07 +00001827
dan37db03b2011-03-16 19:59:18 +00001828 if( p->op==SQLITE_INSERT ){
1829 /* For an INSERT, memory cell p->iNewReg contains the serialized record
1830 ** that is being inserted. Deserialize it. */
1831 UnpackedRecord *pUnpack = p->pNewUnpacked;
1832 if( !pUnpack ){
1833 Mem *pData = &p->v->aMem[p->iNewReg];
drhff535a22016-09-20 01:46:15 +00001834 rc = ExpandBlob(pData);
dan37db03b2011-03-16 19:59:18 +00001835 if( rc!=SQLITE_OK ) goto preupdate_new_out;
dan93bca692011-09-14 19:41:44 +00001836 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
dan37db03b2011-03-16 19:59:18 +00001837 if( !pUnpack ){
1838 rc = SQLITE_NOMEM;
1839 goto preupdate_new_out;
1840 }
1841 p->pNewUnpacked = pUnpack;
1842 }
dan2a86c192017-01-25 17:44:13 +00001843 pMem = &pUnpack->aMem[iIdx];
1844 if( iIdx==p->pTab->iPKey ){
1845 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1846 }else if( iIdx>=pUnpack->nField ){
dan37db03b2011-03-16 19:59:18 +00001847 pMem = (sqlite3_value *)columnNullValue();
dan37db03b2011-03-16 19:59:18 +00001848 }
1849 }else{
1850 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
1851 ** value. Make a copy of the cell contents and return a pointer to it.
1852 ** It is not safe to return a pointer to the memory cell itself as the
1853 ** caller may modify the value text encoding.
1854 */
1855 assert( p->op==SQLITE_UPDATE );
1856 if( !p->aNew ){
1857 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
1858 if( !p->aNew ){
1859 rc = SQLITE_NOMEM;
1860 goto preupdate_new_out;
1861 }
1862 }
drh304637c2011-03-18 16:47:27 +00001863 assert( iIdx>=0 && iIdx<p->pCsr->nField );
dan37db03b2011-03-16 19:59:18 +00001864 pMem = &p->aNew[iIdx];
1865 if( pMem->flags==0 ){
dane43635a2016-10-21 21:21:45 +00001866 if( iIdx==p->pTab->iPKey ){
dan319eeb72011-03-19 08:38:50 +00001867 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1868 }else{
1869 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
1870 if( rc!=SQLITE_OK ) goto preupdate_new_out;
1871 }
dan37db03b2011-03-16 19:59:18 +00001872 }
1873 }
1874 *ppValue = pMem;
1875
1876 preupdate_new_out:
drhe1ed0b02014-08-26 02:15:07 +00001877 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001878 return sqlite3ApiExit(db, rc);
1879}
drh9b1c62d2011-03-30 21:04:43 +00001880#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
drh04e8a582014-11-18 21:20:57 +00001881
dan6f9702e2014-11-01 20:38:06 +00001882#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan04489b62014-10-31 20:11:32 +00001883/*
1884** Return status data for a single loop within query pStmt.
1885*/
1886int sqlite3_stmt_scanstatus(
drhd1a1c232014-11-03 16:35:55 +00001887 sqlite3_stmt *pStmt, /* Prepared statement being queried */
dan04489b62014-10-31 20:11:32 +00001888 int idx, /* Index of loop to report on */
drhd1a1c232014-11-03 16:35:55 +00001889 int iScanStatusOp, /* Which metric to return */
1890 void *pOut /* OUT: Write the answer here */
dan04489b62014-10-31 20:11:32 +00001891){
1892 Vdbe *p = (Vdbe*)pStmt;
dan037b5322014-11-03 11:25:32 +00001893 ScanStatus *pScan;
dan6f9702e2014-11-01 20:38:06 +00001894 if( idx<0 || idx>=p->nScan ) return 1;
1895 pScan = &p->aScan[idx];
drhd1a1c232014-11-03 16:35:55 +00001896 switch( iScanStatusOp ){
1897 case SQLITE_SCANSTAT_NLOOP: {
1898 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
1899 break;
1900 }
1901 case SQLITE_SCANSTAT_NVISIT: {
1902 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
1903 break;
1904 }
1905 case SQLITE_SCANSTAT_EST: {
drh518140e2014-11-06 03:55:10 +00001906 double r = 1.0;
1907 LogEst x = pScan->nEst;
1908 while( x<100 ){
1909 x += 10;
1910 r *= 0.5;
1911 }
1912 *(double*)pOut = r*sqlite3LogEstToInt(x);
drhd1a1c232014-11-03 16:35:55 +00001913 break;
1914 }
1915 case SQLITE_SCANSTAT_NAME: {
dand72219d2014-11-03 16:39:37 +00001916 *(const char**)pOut = pScan->zName;
drhd1a1c232014-11-03 16:35:55 +00001917 break;
1918 }
1919 case SQLITE_SCANSTAT_EXPLAIN: {
1920 if( pScan->addrExplain ){
1921 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
1922 }else{
1923 *(const char**)pOut = 0;
1924 }
1925 break;
1926 }
drhc6652b12014-11-06 04:42:20 +00001927 case SQLITE_SCANSTAT_SELECTID: {
1928 if( pScan->addrExplain ){
1929 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
1930 }else{
1931 *(int*)pOut = -1;
1932 }
1933 break;
1934 }
drhd1a1c232014-11-03 16:35:55 +00001935 default: {
1936 return 1;
dan6f9702e2014-11-01 20:38:06 +00001937 }
1938 }
dan04489b62014-10-31 20:11:32 +00001939 return 0;
1940}
1941
1942/*
1943** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
1944*/
1945void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
1946 Vdbe *p = (Vdbe*)pStmt;
dan6f9702e2014-11-01 20:38:06 +00001947 memset(p->anExec, 0, p->nOp * sizeof(i64));
dan04489b62014-10-31 20:11:32 +00001948}
dan6f9702e2014-11-01 20:38:06 +00001949#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */