blob: b94faceadd208123d3f46aeabda8632ab7618bca [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;
drh42262532010-09-08 16:30:36 +0000178 return 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}
drh3a96a5d2017-06-30 23:09:03 +0000202void *sqlite3_value_pointer(sqlite3_value *pVal){
203 Mem *p = (Mem*)pVal;
204 if( (p->flags & MEM_TypeMask)==(MEM_Null|MEM_Subtype) && p->eSubtype=='p' ){
205 return p->u.pPtr;
206 }else{
207 return 0;
208 }
209}
drh4f26d6c2004-05-26 23:25:30 +0000210const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000211 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000212}
drh6c626082004-11-14 21:56:29 +0000213#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000214const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000215 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000216}
danielk1977d8123362004-06-12 09:25:12 +0000217const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000218 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000219}
220const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000221 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000222}
drh6c626082004-11-14 21:56:29 +0000223#endif /* SQLITE_OMIT_UTF16 */
drh22ec1342015-02-27 00:33:15 +0000224/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
225** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
226** point number string BLOB NULL
227*/
drh4f26d6c2004-05-26 23:25:30 +0000228int sqlite3_value_type(sqlite3_value* pVal){
drh1b27b8c2014-02-10 03:21:57 +0000229 static const u8 aType[] = {
230 SQLITE_BLOB, /* 0x00 */
231 SQLITE_NULL, /* 0x01 */
232 SQLITE_TEXT, /* 0x02 */
233 SQLITE_NULL, /* 0x03 */
234 SQLITE_INTEGER, /* 0x04 */
235 SQLITE_NULL, /* 0x05 */
236 SQLITE_INTEGER, /* 0x06 */
237 SQLITE_NULL, /* 0x07 */
238 SQLITE_FLOAT, /* 0x08 */
239 SQLITE_NULL, /* 0x09 */
240 SQLITE_FLOAT, /* 0x0a */
241 SQLITE_NULL, /* 0x0b */
242 SQLITE_INTEGER, /* 0x0c */
243 SQLITE_NULL, /* 0x0d */
244 SQLITE_INTEGER, /* 0x0e */
245 SQLITE_NULL, /* 0x0f */
246 SQLITE_BLOB, /* 0x10 */
247 SQLITE_NULL, /* 0x11 */
248 SQLITE_TEXT, /* 0x12 */
249 SQLITE_NULL, /* 0x13 */
250 SQLITE_INTEGER, /* 0x14 */
251 SQLITE_NULL, /* 0x15 */
252 SQLITE_INTEGER, /* 0x16 */
253 SQLITE_NULL, /* 0x17 */
254 SQLITE_FLOAT, /* 0x18 */
255 SQLITE_NULL, /* 0x19 */
256 SQLITE_FLOAT, /* 0x1a */
257 SQLITE_NULL, /* 0x1b */
258 SQLITE_INTEGER, /* 0x1c */
259 SQLITE_NULL, /* 0x1d */
260 SQLITE_INTEGER, /* 0x1e */
261 SQLITE_NULL, /* 0x1f */
262 };
mistachkinafc14f72014-03-05 01:29:18 +0000263 return aType[pVal->flags&MEM_AffMask];
drh4f26d6c2004-05-26 23:25:30 +0000264}
265
drh4f03f412015-05-20 21:28:32 +0000266/* Make a copy of an sqlite3_value object
267*/
268sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
269 sqlite3_value *pNew;
270 if( pOrig==0 ) return 0;
271 pNew = sqlite3_malloc( sizeof(*pNew) );
272 if( pNew==0 ) return 0;
273 memset(pNew, 0, sizeof(*pNew));
274 memcpy(pNew, pOrig, MEMCELLSIZE);
275 pNew->flags &= ~MEM_Dyn;
276 pNew->db = 0;
277 if( pNew->flags&(MEM_Str|MEM_Blob) ){
drh10ca5b42015-05-22 21:04:54 +0000278 pNew->flags &= ~(MEM_Static|MEM_Dyn);
279 pNew->flags |= MEM_Ephem;
280 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
281 sqlite3ValueFree(pNew);
282 pNew = 0;
drh4f03f412015-05-20 21:28:32 +0000283 }
284 }
285 return pNew;
286}
287
288/* Destroy an sqlite3_value object previously obtained from
289** sqlite3_value_dup().
290*/
291void sqlite3_value_free(sqlite3_value *pOld){
292 sqlite3ValueFree(pOld);
293}
294
295
drh4f26d6c2004-05-26 23:25:30 +0000296/**************************** sqlite3_result_ *******************************
297** The following routines are used by user-defined functions to specify
298** the function result.
drh4c8555f2009-06-25 01:47:11 +0000299**
peter.d.reid60ec9142014-09-06 16:39:46 +0000300** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
drh4c8555f2009-06-25 01:47:11 +0000301** result as a string or blob but if the string or blob is too large, it
302** then sets the error code to SQLITE_TOOBIG
drhda4ca9d2014-09-09 17:27:35 +0000303**
304** The invokeValueDestructor(P,X) routine invokes destructor function X()
305** on value P is not going to be used and need to be destroyed.
drh4f26d6c2004-05-26 23:25:30 +0000306*/
drh4c8555f2009-06-25 01:47:11 +0000307static void setResultStrOrError(
308 sqlite3_context *pCtx, /* Function context */
309 const char *z, /* String pointer */
310 int n, /* Bytes in string, or negative */
311 u8 enc, /* Encoding of z. 0 for BLOBs */
312 void (*xDel)(void*) /* Destructor function */
313){
drh9bd038f2014-08-27 14:14:06 +0000314 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
drh4c8555f2009-06-25 01:47:11 +0000315 sqlite3_result_error_toobig(pCtx);
316 }
317}
drhda4ca9d2014-09-09 17:27:35 +0000318static int invokeValueDestructor(
319 const void *p, /* Value to destroy */
320 void (*xDel)(void*), /* The destructor */
321 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
322){
drhfc59a952014-09-11 23:34:55 +0000323 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000324 if( xDel==0 ){
325 /* noop */
326 }else if( xDel==SQLITE_TRANSIENT ){
327 /* noop */
drhda4ca9d2014-09-09 17:27:35 +0000328 }else{
329 xDel((void*)p);
330 }
331 if( pCtx ) sqlite3_result_error_toobig(pCtx);
332 return SQLITE_TOOBIG;
333}
drh4f26d6c2004-05-26 23:25:30 +0000334void sqlite3_result_blob(
335 sqlite3_context *pCtx,
336 const void *z,
337 int n,
danielk1977d8123362004-06-12 09:25:12 +0000338 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000339){
drh5708d2d2005-06-22 10:53:59 +0000340 assert( n>=0 );
drh9bd038f2014-08-27 14:14:06 +0000341 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000342 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000343}
drhda4ca9d2014-09-09 17:27:35 +0000344void sqlite3_result_blob64(
345 sqlite3_context *pCtx,
346 const void *z,
347 sqlite3_uint64 n,
348 void (*xDel)(void *)
349){
350 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000351 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +0000352 if( n>0x7fffffff ){
353 (void)invokeValueDestructor(z, xDel, pCtx);
354 }else{
355 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
356 }
357}
drh4f26d6c2004-05-26 23:25:30 +0000358void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh9bd038f2014-08-27 14:14:06 +0000359 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
360 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
drh4f26d6c2004-05-26 23:25:30 +0000361}
362void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000363 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000364 pCtx->isError = SQLITE_ERROR;
drh9b47ee32013-08-20 03:13:51 +0000365 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000366 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000367}
danielk1977a1686c92006-01-23 07:52:37 +0000368#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000369void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh9bd038f2014-08-27 14:14:06 +0000370 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000371 pCtx->isError = SQLITE_ERROR;
drh9b47ee32013-08-20 03:13:51 +0000372 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000373 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000374}
danielk1977a1686c92006-01-23 07:52:37 +0000375#endif
drhf4479502004-05-27 03:12:53 +0000376void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh9bd038f2014-08-27 14:14:06 +0000377 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
378 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
drh4f26d6c2004-05-26 23:25:30 +0000379}
380void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh9bd038f2014-08-27 14:14:06 +0000381 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
382 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
drh4f26d6c2004-05-26 23:25:30 +0000383}
384void sqlite3_result_null(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000385 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
386 sqlite3VdbeMemSetNull(pCtx->pOut);
drh4f26d6c2004-05-26 23:25:30 +0000387}
drh3a96a5d2017-06-30 23:09:03 +0000388void sqlite3_result_pointer(sqlite3_context *pCtx, void *pPtr){
389 Mem *pOut = pCtx->pOut;
390 assert( sqlite3_mutex_held(pOut->db->mutex) );
391 sqlite3VdbeMemSetNull(pOut);
392 sqlite3VdbeMemSetPointer(pOut, pPtr);
393}
drhbcdf78a2015-09-10 20:34:56 +0000394void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
dan5b6c8e42016-01-30 15:46:03 +0000395 Mem *pOut = pCtx->pOut;
396 assert( sqlite3_mutex_held(pOut->db->mutex) );
397 pOut->eSubtype = eSubtype & 0xff;
398 pOut->flags |= MEM_Subtype;
drhbcdf78a2015-09-10 20:34:56 +0000399}
drh4f26d6c2004-05-26 23:25:30 +0000400void sqlite3_result_text(
401 sqlite3_context *pCtx,
402 const char *z,
403 int n,
danielk1977d8123362004-06-12 09:25:12 +0000404 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000405){
drh9bd038f2014-08-27 14:14:06 +0000406 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000407 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000408}
drhbbf483f2014-09-09 20:30:24 +0000409void sqlite3_result_text64(
drhda4ca9d2014-09-09 17:27:35 +0000410 sqlite3_context *pCtx,
411 const char *z,
412 sqlite3_uint64 n,
413 void (*xDel)(void *),
414 unsigned char enc
415){
416 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55 +0000417 assert( xDel!=SQLITE_DYNAMIC );
drh79f7af92014-10-03 16:00:51 +0000418 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drhda4ca9d2014-09-09 17:27:35 +0000419 if( n>0x7fffffff ){
420 (void)invokeValueDestructor(z, xDel, pCtx);
421 }else{
422 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
423 }
424}
drh6c626082004-11-14 21:56:29 +0000425#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000426void sqlite3_result_text16(
427 sqlite3_context *pCtx,
428 const void *z,
429 int n,
danielk1977d8123362004-06-12 09:25:12 +0000430 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000431){
drh9bd038f2014-08-27 14:14:06 +0000432 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000433 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000434}
435void sqlite3_result_text16be(
436 sqlite3_context *pCtx,
437 const void *z,
438 int n,
439 void (*xDel)(void *)
440){
drh9bd038f2014-08-27 14:14:06 +0000441 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000442 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000443}
444void sqlite3_result_text16le(
445 sqlite3_context *pCtx,
446 const void *z,
447 int n,
448 void (*xDel)(void *)
449){
drh9bd038f2014-08-27 14:14:06 +0000450 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000451 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000452}
drh6c626082004-11-14 21:56:29 +0000453#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000454void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh9bd038f2014-08-27 14:14:06 +0000455 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
456 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000457}
drhb026e052007-05-02 01:34:31 +0000458void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh9bd038f2014-08-27 14:14:06 +0000459 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
460 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
drhb026e052007-05-02 01:34:31 +0000461}
dana4d5ae82015-07-24 16:24:37 +0000462int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
463 Mem *pOut = pCtx->pOut;
464 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh6bacdc22015-07-24 17:14:03 +0000465 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
dana4d5ae82015-07-24 16:24:37 +0000466 return SQLITE_TOOBIG;
467 }
468 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
469 return SQLITE_OK;
470}
drh69544ec2008-02-06 14:11:34 +0000471void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
472 pCtx->isError = errCode;
drh9b47ee32013-08-20 03:13:51 +0000473 pCtx->fErrorOrAux = 1;
drh983b5ee2015-02-13 12:05:56 +0000474#ifdef SQLITE_DEBUG
drh96f4ad22015-03-12 21:02:36 +0000475 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
drh983b5ee2015-02-13 12:05:56 +0000476#endif
drh9bd038f2014-08-27 14:14:06 +0000477 if( pCtx->pOut->flags & MEM_Null ){
478 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
drh51c7d862009-04-27 18:46:06 +0000479 SQLITE_UTF8, SQLITE_STATIC);
480 }
drh69544ec2008-02-06 14:11:34 +0000481}
drh4f26d6c2004-05-26 23:25:30 +0000482
drha0206bc2007-05-08 15:15:02 +0000483/* Force an SQLITE_TOOBIG error. */
484void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000485 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000486 pCtx->isError = SQLITE_TOOBIG;
drh9b47ee32013-08-20 03:13:51 +0000487 pCtx->fErrorOrAux = 1;
drh9bd038f2014-08-27 14:14:06 +0000488 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
drhbb4957f2008-03-20 14:03:29 +0000489 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000490}
491
danielk1977a1644fd2007-08-29 12:31:25 +0000492/* An SQLITE_NOMEM error. */
493void sqlite3_result_error_nomem(sqlite3_context *pCtx){
drh9bd038f2014-08-27 14:14:06 +0000494 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
495 sqlite3VdbeMemSetNull(pCtx->pOut);
mistachkinfad30392016-02-13 23:43:46 +0000496 pCtx->isError = SQLITE_NOMEM_BKPT;
drh9b47ee32013-08-20 03:13:51 +0000497 pCtx->fErrorOrAux = 1;
drh4a642b62016-02-05 01:55:27 +0000498 sqlite3OomFault(pCtx->pOut->db);
danielk1977a1644fd2007-08-29 12:31:25 +0000499}
drh4f26d6c2004-05-26 23:25:30 +0000500
dan5cf53532010-05-01 16:40:20 +0000501/*
502** This function is called after a transaction has been committed. It
503** invokes callbacks registered with sqlite3_wal_hook() as required.
504*/
drh7ed91f22010-04-29 22:34:07 +0000505static int doWalCallbacks(sqlite3 *db){
dan8d22a172010-04-19 18:03:51 +0000506 int rc = SQLITE_OK;
dan5cf53532010-05-01 16:40:20 +0000507#ifndef SQLITE_OMIT_WAL
508 int i;
dan8d22a172010-04-19 18:03:51 +0000509 for(i=0; i<db->nDb; i++){
510 Btree *pBt = db->aDb[i].pBt;
511 if( pBt ){
drhef15c6e2014-12-12 01:27:17 +0000512 int nEntry;
513 sqlite3BtreeEnter(pBt);
514 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
515 sqlite3BtreeLeave(pBt);
drh5def0842010-05-05 20:00:25 +0000516 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
drh69c33822016-08-18 14:33:11 +0000517 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
dan8d22a172010-04-19 18:03:51 +0000518 }
519 }
520 }
dan5cf53532010-05-01 16:40:20 +0000521#endif
dan8d22a172010-04-19 18:03:51 +0000522 return rc;
523}
524
drh201e0c62015-07-14 14:48:50 +0000525
drh4f26d6c2004-05-26 23:25:30 +0000526/*
527** Execute the statement pStmt, either until a row of data is ready, the
528** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000529**
530** This routine implements the bulk of the logic behind the sqlite_step()
531** API. The only thing omitted is the automatic recompile if a
532** schema change has occurred. That detail is handled by the
533** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000534*/
drhb900aaf2006-11-09 00:24:53 +0000535static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000536 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000537 int rc;
538
danielk1977a185ddb2007-11-15 16:04:15 +0000539 assert(p);
540 if( p->magic!=VDBE_MAGIC_RUN ){
drh3674bfd2010-04-17 12:53:19 +0000541 /* We used to require that sqlite3_reset() be called before retrying
drh602acb42011-01-17 17:42:37 +0000542 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
543 ** with version 3.7.0, we changed this so that sqlite3_reset() would
544 ** be called automatically instead of throwing the SQLITE_MISUSE error.
545 ** This "automatic-reset" change is not technically an incompatibility,
546 ** since any application that receives an SQLITE_MISUSE is broken by
547 ** definition.
548 **
549 ** Nevertheless, some published applications that were originally written
550 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
drhb8a45bb2011-12-31 21:51:55 +0000551 ** returns, and those were broken by the automatic-reset change. As a
drh602acb42011-01-17 17:42:37 +0000552 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
553 ** legacy behavior of returning SQLITE_MISUSE for cases where the
554 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
555 ** or SQLITE_BUSY error.
drh3674bfd2010-04-17 12:53:19 +0000556 */
drh602acb42011-01-17 17:42:37 +0000557#ifdef SQLITE_OMIT_AUTORESET
drh983b5ee2015-02-13 12:05:56 +0000558 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
drh602acb42011-01-17 17:42:37 +0000559 sqlite3_reset((sqlite3_stmt*)p);
560 }else{
561 return SQLITE_MISUSE_BKPT;
562 }
563#else
drh3674bfd2010-04-17 12:53:19 +0000564 sqlite3_reset((sqlite3_stmt*)p);
drh602acb42011-01-17 17:42:37 +0000565#endif
drhf1bfe9a2007-10-17 01:44:20 +0000566 }
567
dan14d4cc42010-01-20 14:25:37 +0000568 /* Check that malloc() has not failed. If it has, return early. */
drh17435752007-08-16 04:30:38 +0000569 db = p->db;
drhc456e572008-08-11 18:44:58 +0000570 if( db->mallocFailed ){
dan14d4cc42010-01-20 14:25:37 +0000571 p->rc = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46 +0000572 return SQLITE_NOMEM_BKPT;
drhc456e572008-08-11 18:44:58 +0000573 }
danielk1977261919c2005-12-06 12:52:59 +0000574
danielk1977a21c6b62005-01-24 10:25:59 +0000575 if( p->pc<=0 && p->expired ){
drhbee80652010-02-25 21:27:58 +0000576 p->rc = SQLITE_SCHEMA;
drhb900aaf2006-11-09 00:24:53 +0000577 rc = SQLITE_ERROR;
578 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000579 }
danielk19771d850a72004-05-31 08:26:49 +0000580 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000581 /* If there are no other statements currently running, then
582 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
583 ** from interrupting a statement that has not yet started.
584 */
drh4f7d3a52013-06-27 23:54:02 +0000585 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000586 db->u1.isInterrupted = 0;
587 }
588
drh888e16e2013-07-09 13:05:49 +0000589 assert( db->nVdbeWrite>0 || db->autoCommit==0
dancb3e4b72013-07-03 19:53:05 +0000590 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
591 );
dan1da40a32009-09-19 17:00:31 +0000592
drh19e2d372005-08-29 23:00:03 +0000593#ifndef SQLITE_OMIT_TRACE
drh3d2a5292016-07-13 22:55:01 +0000594 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
595 && !db->init.busy && p->zSql ){
drhb7e8ea22010-05-03 14:32:30 +0000596 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
drh201e0c62015-07-14 14:48:50 +0000597 }else{
598 assert( p->startTime==0 );
drh19e2d372005-08-29 23:00:03 +0000599 }
600#endif
drhc16a03b2004-09-15 13:38:10 +0000601
drh4f7d3a52013-06-27 23:54:02 +0000602 db->nVdbeActive++;
603 if( p->readOnly==0 ) db->nVdbeWrite++;
drh1713afb2013-06-28 01:24:57 +0000604 if( p->bIsReader ) db->nVdbeRead++;
danielk19771d850a72004-05-31 08:26:49 +0000605 p->pc = 0;
606 }
drh983b5ee2015-02-13 12:05:56 +0000607#ifdef SQLITE_DEBUG
608 p->rcApp = SQLITE_OK;
609#endif
drhb7f91642004-10-31 02:22:47 +0000610#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000611 if( p->explain ){
612 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000613 }else
614#endif /* SQLITE_OMIT_EXPLAIN */
615 {
drh4f7d3a52013-06-27 23:54:02 +0000616 db->nVdbeExec++;
drh4f26d6c2004-05-26 23:25:30 +0000617 rc = sqlite3VdbeExec(p);
drh4f7d3a52013-06-27 23:54:02 +0000618 db->nVdbeExec--;
drh4f26d6c2004-05-26 23:25:30 +0000619 }
620
drh19e2d372005-08-29 23:00:03 +0000621#ifndef SQLITE_OMIT_TRACE
drh201e0c62015-07-14 14:48:50 +0000622 /* If the statement completed successfully, invoke the profile callback */
623 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
drh19e2d372005-08-29 23:00:03 +0000624#endif
625
dan8d22a172010-04-19 18:03:51 +0000626 if( rc==SQLITE_DONE ){
627 assert( p->rc==SQLITE_OK );
drh7ed91f22010-04-29 22:34:07 +0000628 p->rc = doWalCallbacks(db);
dan8d22a172010-04-19 18:03:51 +0000629 if( p->rc!=SQLITE_OK ){
630 rc = SQLITE_ERROR;
631 }
632 }
633
drh80cc85b2008-07-23 21:07:25 +0000634 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000635 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
mistachkinfad30392016-02-13 23:43:46 +0000636 p->rc = SQLITE_NOMEM_BKPT;
drhb900aaf2006-11-09 00:24:53 +0000637 }
danielk1977357864e2009-03-25 15:43:08 +0000638end_of_step:
639 /* At this point local variable rc holds the value that should be
640 ** returned if this statement was compiled using the legacy
641 ** sqlite3_prepare() interface. According to the docs, this can only
642 ** be one of the values in the first assert() below. Variable p->rc
643 ** contains the value that would be returned if sqlite3_finalize()
644 ** were called on statement p.
645 */
646 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
drhcbd8db32015-08-20 17:18:32 +0000647 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
danielk1977357864e2009-03-25 15:43:08 +0000648 );
drh983b5ee2015-02-13 12:05:56 +0000649 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
drh2c2f3922017-06-01 00:54:35 +0000650 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
651 && rc!=SQLITE_ROW
652 && rc!=SQLITE_DONE
653 ){
654 /* If this statement was prepared using saved SQL and an
mistachkin48864df2013-03-21 21:20:32 +0000655 ** error has occurred, then return the error code in p->rc to the
danielk1977357864e2009-03-25 15:43:08 +0000656 ** caller. Set the error code in the database handle to the same value.
657 */
dan029ead62011-10-27 15:19:58 +0000658 rc = sqlite3VdbeTransferError(p);
danielk1977357864e2009-03-25 15:43:08 +0000659 }
660 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000661}
662
663/*
664** This is the top-level implementation of sqlite3_step(). Call
665** sqlite3Step() to do most of the work. If a schema error occurs,
666** call sqlite3Reprepare() and try again.
667*/
drhb900aaf2006-11-09 00:24:53 +0000668int sqlite3_step(sqlite3_stmt *pStmt){
drha6129fa2010-02-24 17:15:19 +0000669 int rc = SQLITE_OK; /* Result from sqlite3Step() */
670 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
671 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
672 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
673 sqlite3 *db; /* The database connection */
674
drh413c3d32010-02-23 20:11:56 +0000675 if( vdbeSafetyNotNull(v) ){
676 return SQLITE_MISUSE_BKPT;
danielk19778e556522007-11-13 10:30:24 +0000677 }
drh413c3d32010-02-23 20:11:56 +0000678 db = v->db;
679 sqlite3_mutex_enter(db->mutex);
drh37f58e92012-09-04 21:34:26 +0000680 v->doingRerun = 0;
drh413c3d32010-02-23 20:11:56 +0000681 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
drh2c7946a2014-08-19 20:27:40 +0000682 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
683 int savedPc = v->pc;
684 rc2 = rc = sqlite3Reprepare(v);
685 if( rc!=SQLITE_OK) break;
drh413c3d32010-02-23 20:11:56 +0000686 sqlite3_reset(pStmt);
drh5f58ae72014-08-19 20:41:36 +0000687 if( savedPc>=0 ) v->doingRerun = 1;
dan08f5f4c2011-06-27 19:37:23 +0000688 assert( v->expired==0 );
drh413c3d32010-02-23 20:11:56 +0000689 }
drha3cc0072013-12-13 16:23:55 +0000690 if( rc2!=SQLITE_OK ){
drh413c3d32010-02-23 20:11:56 +0000691 /* This case occurs after failing to recompile an sql statement.
692 ** The error message from the SQL compiler has already been loaded
693 ** into the database handle. This block copies the error message
694 ** from the database handle into the statement and sets the statement
695 ** program counter to 0 to ensure that when the statement is
696 ** finalized or reset the parser error message is available via
697 ** sqlite3_errmsg() and sqlite3_errcode().
698 */
699 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
700 sqlite3DbFree(db, v->zErrMsg);
701 if( !db->mallocFailed ){
702 v->zErrMsg = sqlite3DbStrDup(db, zErr);
drha6129fa2010-02-24 17:15:19 +0000703 v->rc = rc2;
drh413c3d32010-02-23 20:11:56 +0000704 } else {
705 v->zErrMsg = 0;
mistachkinfad30392016-02-13 23:43:46 +0000706 v->rc = rc = SQLITE_NOMEM_BKPT;
drh413c3d32010-02-23 20:11:56 +0000707 }
708 }
709 rc = sqlite3ApiExit(db, rc);
710 sqlite3_mutex_leave(db->mutex);
danielk197776e8d1a2006-01-18 18:22:43 +0000711 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000712}
713
drh95a7b3e2013-09-16 12:57:19 +0000714
drh4f26d6c2004-05-26 23:25:30 +0000715/*
drheb2e1762004-05-27 01:53:56 +0000716** Extract the user data from a sqlite3_context structure and return a
717** pointer to it.
718*/
719void *sqlite3_user_data(sqlite3_context *p){
720 assert( p && p->pFunc );
721 return p->pFunc->pUserData;
722}
723
724/*
drhfa4a4b92008-03-19 21:45:51 +0000725** Extract the user data from a sqlite3_context structure and return a
726** pointer to it.
drh9f129f42010-08-31 15:27:32 +0000727**
728** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
729** returns a copy of the pointer to the database connection (the 1st
730** parameter) of the sqlite3_create_function() and
731** sqlite3_create_function16() routines that originally registered the
732** application defined function.
drhfa4a4b92008-03-19 21:45:51 +0000733*/
734sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
drha46a4a62015-09-08 21:12:53 +0000735 assert( p && p->pOut );
drh9bd038f2014-08-27 14:14:06 +0000736 return p->pOut->db;
drhfa4a4b92008-03-19 21:45:51 +0000737}
738
739/*
drha9e03b12015-03-12 06:46:52 +0000740** Return the current time for a statement. If the current time
741** is requested more than once within the same run of a single prepared
742** statement, the exact same time is returned for each invocation regardless
743** of the amount of time that elapses between invocations. In other words,
744** the time returned is always the time of the first call.
drh95a7b3e2013-09-16 12:57:19 +0000745*/
746sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
drh95a7b3e2013-09-16 12:57:19 +0000747 int rc;
drha9e03b12015-03-12 06:46:52 +0000748#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
drh3df79a92015-03-12 23:48:27 +0000749 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
drha9e03b12015-03-12 06:46:52 +0000750 assert( p->pVdbe!=0 );
751#else
drh3df79a92015-03-12 23:48:27 +0000752 sqlite3_int64 iTime = 0;
drha9e03b12015-03-12 06:46:52 +0000753 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
drha9e03b12015-03-12 06:46:52 +0000754#endif
drh3df79a92015-03-12 23:48:27 +0000755 if( *piTime==0 ){
drha9e03b12015-03-12 06:46:52 +0000756 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
757 if( rc ) *piTime = 0;
drh95a7b3e2013-09-16 12:57:19 +0000758 }
drha9e03b12015-03-12 06:46:52 +0000759 return *piTime;
drh95a7b3e2013-09-16 12:57:19 +0000760}
761
762/*
drhb7481e72006-09-16 21:45:14 +0000763** The following is the implementation of an SQL function that always
764** fails with an error message stating that the function is used in the
765** wrong context. The sqlite3_overload_function() API might construct
766** SQL function that use this routine so that the functions will exist
767** for name resolution but are actually overloaded by the xFindFunction
768** method of virtual tables.
769*/
770void sqlite3InvalidFunction(
771 sqlite3_context *context, /* The function calling context */
danielk197762c14b32008-11-19 09:05:26 +0000772 int NotUsed, /* Number of arguments to the function */
773 sqlite3_value **NotUsed2 /* Value of each argument */
drhb7481e72006-09-16 21:45:14 +0000774){
775 const char *zName = context->pFunc->zName;
776 char *zErr;
danielk197762c14b32008-11-19 09:05:26 +0000777 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbc6160b2009-04-08 15:45:31 +0000778 zErr = sqlite3_mprintf(
drhb7481e72006-09-16 21:45:14 +0000779 "unable to use function %s in the requested context", zName);
780 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000781 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000782}
783
784/*
drh9de4a172014-08-23 18:17:19 +0000785** Create a new aggregate context for p and return a pointer to
786** its pMem->z element.
787*/
788static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
789 Mem *pMem = p->pMem;
790 assert( (pMem->flags & MEM_Agg)==0 );
791 if( nByte<=0 ){
drh0725cab2014-09-17 14:52:46 +0000792 sqlite3VdbeMemSetNull(pMem);
drh9de4a172014-08-23 18:17:19 +0000793 pMem->z = 0;
794 }else{
drh322f2852014-09-19 00:43:39 +0000795 sqlite3VdbeMemClearAndResize(pMem, nByte);
drh9de4a172014-08-23 18:17:19 +0000796 pMem->flags = MEM_Agg;
797 pMem->u.pDef = p->pFunc;
798 if( pMem->z ){
799 memset(pMem->z, 0, nByte);
800 }
801 }
802 return (void*)pMem->z;
803}
804
805/*
drheb2e1762004-05-27 01:53:56 +0000806** Allocate or return the aggregate context for a user function. A new
807** context is allocated on the first call. Subsequent calls return the
808** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000809*/
810void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drh2d801512016-01-14 22:19:58 +0000811 assert( p && p->pFunc && p->pFunc->xFinalize );
drh9bd038f2014-08-27 14:14:06 +0000812 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
drhd68eee02009-12-11 03:44:18 +0000813 testcase( nByte<0 );
drh9de4a172014-08-23 18:17:19 +0000814 if( (p->pMem->flags & MEM_Agg)==0 ){
815 return createAggContext(p, nByte);
816 }else{
817 return (void*)p->pMem->z;
drheb2e1762004-05-27 01:53:56 +0000818 }
drheb2e1762004-05-27 01:53:56 +0000819}
820
821/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000822** Return the auxiliary data pointer, if any, for the iArg'th argument to
danielk1977682f68b2004-06-05 10:22:17 +0000823** the user-function defined by pCtx.
drhf7fa4e72017-05-11 15:20:18 +0000824**
825** The left-most argument is 0.
826**
827** Undocumented behavior: If iArg is negative then access a cache of
828** auxiliary data pointers that is available to all functions within a
829** single prepared statement. The iArg values must match.
danielk1977682f68b2004-06-05 10:22:17 +0000830*/
831void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
dan0c547792013-07-18 17:12:08 +0000832 AuxData *pAuxData;
drhb21c8cd2007-08-21 19:33:56 +0000833
drh9bd038f2014-08-27 14:14:06 +0000834 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000835#if SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000836 if( pCtx->pVdbe==0 ) return 0;
drha9e03b12015-03-12 06:46:52 +0000837#else
838 assert( pCtx->pVdbe!=0 );
839#endif
drhe6941392017-05-10 19:42:52 +0000840 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000841 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
drhe6941392017-05-10 19:42:52 +0000842 return pAuxData->pAux;
843 }
danielk1977682f68b2004-06-05 10:22:17 +0000844 }
drhe6941392017-05-10 19:42:52 +0000845 return 0;
danielk1977682f68b2004-06-05 10:22:17 +0000846}
847
848/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000849** Set the auxiliary data pointer and delete function, for the iArg'th
danielk1977682f68b2004-06-05 10:22:17 +0000850** argument to the user-function defined by pCtx. Any previous value is
851** deleted by calling the delete function specified when it was set.
drhf7fa4e72017-05-11 15:20:18 +0000852**
853** The left-most argument is 0.
854**
855** Undocumented behavior: If iArg is negative then make the data available
856** to all functions within the current prepared statement using iArg as an
857** access code.
danielk1977682f68b2004-06-05 10:22:17 +0000858*/
859void sqlite3_set_auxdata(
860 sqlite3_context *pCtx,
861 int iArg,
862 void *pAux,
863 void (*xDelete)(void*)
864){
dan0c547792013-07-18 17:12:08 +0000865 AuxData *pAuxData;
866 Vdbe *pVdbe = pCtx->pVdbe;
danielk1977682f68b2004-06-05 10:22:17 +0000867
drh9bd038f2014-08-27 14:14:06 +0000868 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drha9e03b12015-03-12 06:46:52 +0000869#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
dan18bf8072015-03-11 20:06:40 +0000870 if( pVdbe==0 ) goto failed;
drha9e03b12015-03-12 06:46:52 +0000871#else
872 assert( pVdbe!=0 );
873#endif
danielk1977682f68b2004-06-05 10:22:17 +0000874
drhe6941392017-05-10 19:42:52 +0000875 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:18 +0000876 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
877 break;
878 }
dan0c547792013-07-18 17:12:08 +0000879 }
880 if( pAuxData==0 ){
881 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
882 if( !pAuxData ) goto failed;
drhe6941392017-05-10 19:42:52 +0000883 pAuxData->iAuxOp = pCtx->iOp;
884 pAuxData->iAuxArg = iArg;
885 pAuxData->pNextAux = pVdbe->pAuxData;
dan0c547792013-07-18 17:12:08 +0000886 pVdbe->pAuxData = pAuxData;
drh9b47ee32013-08-20 03:13:51 +0000887 if( pCtx->fErrorOrAux==0 ){
888 pCtx->isError = 0;
889 pCtx->fErrorOrAux = 1;
890 }
drhe6941392017-05-10 19:42:52 +0000891 }else if( pAuxData->xDeleteAux ){
892 pAuxData->xDeleteAux(pAuxData->pAux);
danielk1977682f68b2004-06-05 10:22:17 +0000893 }
dan0c547792013-07-18 17:12:08 +0000894
danielk1977682f68b2004-06-05 10:22:17 +0000895 pAuxData->pAux = pAux;
drhe6941392017-05-10 19:42:52 +0000896 pAuxData->xDeleteAux = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000897 return;
898
899failed:
900 if( xDelete ){
901 xDelete(pAux);
902 }
danielk1977682f68b2004-06-05 10:22:17 +0000903}
904
shaneeec556d2008-10-12 00:27:53 +0000905#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000906/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000907** Return the number of times the Step function of an aggregate has been
drheb2e1762004-05-27 01:53:56 +0000908** called.
909**
drhcf85a512006-02-09 18:35:29 +0000910** This function is deprecated. Do not use it for new code. It is
911** provide only to avoid breaking legacy code. New aggregate function
912** implementations should keep their own counts within their aggregate
913** context.
drheb2e1762004-05-27 01:53:56 +0000914*/
915int sqlite3_aggregate_count(sqlite3_context *p){
drh2d801512016-01-14 22:19:58 +0000916 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
drhabfcea22005-09-06 20:36:48 +0000917 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000918}
shaneeec556d2008-10-12 00:27:53 +0000919#endif
drheb2e1762004-05-27 01:53:56 +0000920
921/*
drh4f26d6c2004-05-26 23:25:30 +0000922** Return the number of columns in the result set for the statement pStmt.
923*/
924int sqlite3_column_count(sqlite3_stmt *pStmt){
925 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000926 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000927}
928
929/*
930** Return the number of values available from the current row of the
931** currently executing statement pStmt.
932*/
933int sqlite3_data_count(sqlite3_stmt *pStmt){
934 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000935 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000936 return pVm->nResColumn;
937}
938
dan46c47d42011-03-01 18:42:07 +0000939/*
940** Return a pointer to static memory containing an SQL NULL value.
941*/
942static const Mem *columnNullValue(void){
943 /* Even though the Mem structure contains an element
drhb1a1c292014-03-05 12:47:55 +0000944 ** of type i64, on certain architectures (x86) with certain compiler
dan46c47d42011-03-01 18:42:07 +0000945 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
946 ** instead of an 8-byte one. This all works fine, except that when
947 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
948 ** that a Mem structure is located on an 8-byte boundary. To prevent
drhb1a1c292014-03-05 12:47:55 +0000949 ** these assert()s from failing, when building with SQLITE_DEBUG defined
950 ** using gcc, we force nullMem to be 8-byte aligned using the magical
dan46c47d42011-03-01 18:42:07 +0000951 ** __attribute__((aligned(8))) macro. */
952 static const Mem nullMem
953#if defined(SQLITE_DEBUG) && defined(__GNUC__)
drhb1a1c292014-03-05 12:47:55 +0000954 __attribute__((aligned(8)))
dan46c47d42011-03-01 18:42:07 +0000955#endif
drh035e5632014-09-16 14:16:31 +0000956 = {
drh3329a632014-09-18 01:21:43 +0000957 /* .u = */ {0},
drh8faee872015-09-19 18:08:13 +0000958 /* .flags = */ (u16)MEM_Null,
959 /* .enc = */ (u8)0,
960 /* .eSubtype = */ (u8)0,
961 /* .n = */ (int)0,
962 /* .z = */ (char*)0,
963 /* .zMalloc = */ (char*)0,
964 /* .szMalloc = */ (int)0,
965 /* .uTemp = */ (u32)0,
966 /* .db = */ (sqlite3*)0,
967 /* .xDel = */ (void(*)(void*))0,
drhb1a1c292014-03-05 12:47:55 +0000968#ifdef SQLITE_DEBUG
drh8faee872015-09-19 18:08:13 +0000969 /* .pScopyFrom = */ (Mem*)0,
970 /* .pFiller = */ (void*)0,
drhb1a1c292014-03-05 12:47:55 +0000971#endif
drh035e5632014-09-16 14:16:31 +0000972 };
dan46c47d42011-03-01 18:42:07 +0000973 return &nullMem;
974}
drh4f26d6c2004-05-26 23:25:30 +0000975
976/*
977** Check to see if column iCol of the given statement is valid. If
978** it is, return a pointer to the Mem for the value of that column.
979** If iCol is not valid, return a pointer to a Mem which has a value
980** of NULL.
981*/
982static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000983 Vdbe *pVm;
drh32bc3f62007-08-21 20:25:39 +0000984 Mem *pOut;
985
986 pVm = (Vdbe *)pStmt;
drh28f17012016-09-22 21:37:18 +0000987 if( pVm==0 ) return (Mem*)columnNullValue();
988 assert( pVm->db );
989 sqlite3_mutex_enter(pVm->db->mutex);
990 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drhd4e70eb2008-01-02 00:34:36 +0000991 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000992 }else{
drh28f17012016-09-22 21:37:18 +0000993 sqlite3Error(pVm->db, SQLITE_RANGE);
dan46c47d42011-03-01 18:42:07 +0000994 pOut = (Mem*)columnNullValue();
drh4f26d6c2004-05-26 23:25:30 +0000995 }
drh32bc3f62007-08-21 20:25:39 +0000996 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000997}
998
danielk19772e588c72005-12-09 14:25:08 +0000999/*
1000** This function is called after invoking an sqlite3_value_XXX function on a
1001** column value (i.e. a value returned by evaluating an SQL expression in the
1002** select list of a SELECT statement) that may cause a malloc() failure. If
1003** malloc() has failed, the threads mallocFailed flag is cleared and the result
1004** code of statement pStmt set to SQLITE_NOMEM.
1005**
drh32bc3f62007-08-21 20:25:39 +00001006** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +00001007**
1008** sqlite3_column_int()
1009** sqlite3_column_int64()
1010** sqlite3_column_text()
1011** sqlite3_column_text16()
1012** sqlite3_column_real()
1013** sqlite3_column_bytes()
1014** sqlite3_column_bytes16()
drh42262532010-09-08 16:30:36 +00001015** sqiite3_column_blob()
danielk19772e588c72005-12-09 14:25:08 +00001016*/
1017static void columnMallocFailure(sqlite3_stmt *pStmt)
1018{
1019 /* If malloc() failed during an encoding conversion within an
1020 ** sqlite3_column_XXX API, then set the return code of the statement to
1021 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
1022 ** and _finalize() will return NOMEM.
1023 */
danielk197754f01982006-01-18 15:25:17 +00001024 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +00001025 if( p ){
drh28f17012016-09-22 21:37:18 +00001026 assert( p->db!=0 );
1027 assert( sqlite3_mutex_held(p->db->mutex) );
drh32bc3f62007-08-21 20:25:39 +00001028 p->rc = sqlite3ApiExit(p->db, p->rc);
1029 sqlite3_mutex_leave(p->db->mutex);
1030 }
danielk19772e588c72005-12-09 14:25:08 +00001031}
1032
drh4f26d6c2004-05-26 23:25:30 +00001033/**************************** sqlite3_column_ *******************************
1034** The following routines are used to access elements of the current row
1035** in the result set.
1036*/
danielk1977c572ef72004-05-27 09:28:41 +00001037const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001038 const void *val;
danielk19772e588c72005-12-09 14:25:08 +00001039 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +00001040 /* Even though there is no encoding conversion, value_blob() might
1041 ** need to call malloc() to expand the result of a zeroblob()
1042 ** expression.
1043 */
1044 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +00001045 return val;
danielk1977c572ef72004-05-27 09:28:41 +00001046}
drh4f26d6c2004-05-26 23:25:30 +00001047int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001048 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
1049 columnMallocFailure(pStmt);
1050 return val;
drh4f26d6c2004-05-26 23:25:30 +00001051}
1052int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001053 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
1054 columnMallocFailure(pStmt);
1055 return val;
drh4f26d6c2004-05-26 23:25:30 +00001056}
1057double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001058 double val = sqlite3_value_double( columnMem(pStmt,i) );
1059 columnMallocFailure(pStmt);
1060 return val;
drh4f26d6c2004-05-26 23:25:30 +00001061}
1062int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001063 int val = sqlite3_value_int( columnMem(pStmt,i) );
1064 columnMallocFailure(pStmt);
1065 return val;
drh4f26d6c2004-05-26 23:25:30 +00001066}
drhefad9992004-06-22 12:13:55 +00001067sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001068 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
1069 columnMallocFailure(pStmt);
1070 return val;
drh4f26d6c2004-05-26 23:25:30 +00001071}
1072const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001073 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
1074 columnMallocFailure(pStmt);
1075 return val;
drh4f26d6c2004-05-26 23:25:30 +00001076}
drhd1e47332005-06-26 17:55:33 +00001077sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +00001078 Mem *pOut = columnMem(pStmt, i);
1079 if( pOut->flags&MEM_Static ){
1080 pOut->flags &= ~MEM_Static;
1081 pOut->flags |= MEM_Ephem;
1082 }
drh32bc3f62007-08-21 20:25:39 +00001083 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +00001084 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +00001085}
drh6c626082004-11-14 21:56:29 +00001086#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001087const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +00001088 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
1089 columnMallocFailure(pStmt);
1090 return val;
drh4f26d6c2004-05-26 23:25:30 +00001091}
drh6c626082004-11-14 21:56:29 +00001092#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +00001093int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +00001094 int iType = sqlite3_value_type( columnMem(pStmt,i) );
1095 columnMallocFailure(pStmt);
1096 return iType;
drh4f26d6c2004-05-26 23:25:30 +00001097}
1098
drh5e2517e2004-09-24 23:59:12 +00001099/*
1100** Convert the N-th element of pStmt->pColName[] into a string using
1101** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +00001102**
1103** There are up to 5 names for each column. useType determines which
1104** name is returned. Here are the names:
1105**
1106** 0 The column name as it should be displayed for output
1107** 1 The datatype name for the column
1108** 2 The name of the database that the column derives from
1109** 3 The name of the table that the column derives from
1110** 4 The name of the table column that the result column derives from
1111**
1112** If the result is not a simple column reference (if it is an expression
1113** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +00001114*/
1115static const void *columnName(
1116 sqlite3_stmt *pStmt,
1117 int N,
1118 const void *(*xFunc)(Mem*),
1119 int useType
1120){
drh9ca95732014-10-24 00:35:58 +00001121 const void *ret;
1122 Vdbe *p;
drh32bc3f62007-08-21 20:25:39 +00001123 int n;
drh9ca95732014-10-24 00:35:58 +00001124 sqlite3 *db;
1125#ifdef SQLITE_ENABLE_API_ARMOR
1126 if( pStmt==0 ){
1127 (void)SQLITE_MISUSE_BKPT;
1128 return 0;
1129 }
1130#endif
1131 ret = 0;
1132 p = (Vdbe *)pStmt;
1133 db = p->db;
drh9b4ea4a2009-04-10 20:32:00 +00001134 assert( db!=0 );
1135 n = sqlite3_column_count(pStmt);
1136 if( N<n && N>=0 ){
1137 N += useType*n;
1138 sqlite3_mutex_enter(db->mutex);
1139 assert( db->mallocFailed==0 );
1140 ret = xFunc(&p->aColName[N]);
1141 /* A malloc may have failed inside of the xFunc() call. If this
1142 ** is the case, clear the mallocFailed flag and return NULL.
1143 */
1144 if( db->mallocFailed ){
drh4a642b62016-02-05 01:55:27 +00001145 sqlite3OomClear(db);
drh9b4ea4a2009-04-10 20:32:00 +00001146 ret = 0;
drh32bc3f62007-08-21 20:25:39 +00001147 }
drh9b4ea4a2009-04-10 20:32:00 +00001148 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +00001149 }
danielk197700fd9572005-12-07 06:27:43 +00001150 return ret;
drh5e2517e2004-09-24 23:59:12 +00001151}
1152
drh4f26d6c2004-05-26 23:25:30 +00001153/*
1154** Return the name of the Nth column of the result set returned by SQL
1155** statement pStmt.
1156*/
1157const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001158 return columnName(
1159 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +00001160}
drhe590fbd2005-05-20 19:36:01 +00001161#ifndef SQLITE_OMIT_UTF16
1162const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001163 return columnName(
1164 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +00001165}
1166#endif
drh4f26d6c2004-05-26 23:25:30 +00001167
1168/*
drh3f913572008-03-22 01:07:17 +00001169** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1170** not define OMIT_DECLTYPE.
1171*/
1172#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
1173# error "Must not define both SQLITE_OMIT_DECLTYPE \
1174 and SQLITE_ENABLE_COLUMN_METADATA"
1175#endif
1176
1177#ifndef SQLITE_OMIT_DECLTYPE
1178/*
drh6c626082004-11-14 21:56:29 +00001179** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +00001180** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +00001181*/
1182const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001183 return columnName(
1184 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +00001185}
drh6c626082004-11-14 21:56:29 +00001186#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +00001187const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001188 return columnName(
1189 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +00001190}
drh6c626082004-11-14 21:56:29 +00001191#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +00001192#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +00001193
danielk19774b1ae992006-02-10 03:06:10 +00001194#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +00001195/*
1196** Return the name of the database from which a result column derives.
1197** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001198** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001199*/
1200const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001201 return columnName(
1202 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001203}
1204#ifndef SQLITE_OMIT_UTF16
1205const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001206 return columnName(
1207 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001208}
1209#endif /* SQLITE_OMIT_UTF16 */
1210
1211/*
1212** Return the name of the table from which a result column derives.
1213** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001214** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001215*/
1216const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001217 return columnName(
1218 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001219}
1220#ifndef SQLITE_OMIT_UTF16
1221const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001222 return columnName(
1223 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001224}
1225#endif /* SQLITE_OMIT_UTF16 */
1226
1227/*
1228** Return the name of the table column from which a result column derives.
1229** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:46 +00001230** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:01 +00001231*/
1232const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001233 return columnName(
1234 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001235}
1236#ifndef SQLITE_OMIT_UTF16
1237const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001238 return columnName(
1239 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001240}
1241#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +00001242#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +00001243
1244
drh4f26d6c2004-05-26 23:25:30 +00001245/******************************* sqlite3_bind_ ***************************
1246**
1247** Routines used to attach values to wildcards in a compiled SQL statement.
1248*/
1249/*
1250** Unbind the value bound to variable i in virtual machine p. This is the
1251** the same as binding a NULL value to the column. If the "i" parameter is
1252** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1253**
drh488e7b62008-10-06 12:46:43 +00001254** A successful evaluation of this routine acquires the mutex on p.
1255** the mutex is released if any kind of error occurs.
1256**
drh4f26d6c2004-05-26 23:25:30 +00001257** The error code stored in database p->db is overwritten with the return
1258** value in any case.
1259*/
1260static int vdbeUnbind(Vdbe *p, int i){
1261 Mem *pVar;
drh413c3d32010-02-23 20:11:56 +00001262 if( vdbeSafetyNotNull(p) ){
1263 return SQLITE_MISUSE_BKPT;
1264 }
drh488e7b62008-10-06 12:46:43 +00001265 sqlite3_mutex_enter(p->db->mutex);
1266 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh13f40da2014-08-22 18:00:11 +00001267 sqlite3Error(p->db, SQLITE_MISUSE);
drh488e7b62008-10-06 12:46:43 +00001268 sqlite3_mutex_leave(p->db->mutex);
drh413c3d32010-02-23 20:11:56 +00001269 sqlite3_log(SQLITE_MISUSE,
1270 "bind on a busy prepared statement: [%s]", p->zSql);
1271 return SQLITE_MISUSE_BKPT;
drh4f26d6c2004-05-26 23:25:30 +00001272 }
1273 if( i<1 || i>p->nVar ){
drh13f40da2014-08-22 18:00:11 +00001274 sqlite3Error(p->db, SQLITE_RANGE);
drh488e7b62008-10-06 12:46:43 +00001275 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001276 return SQLITE_RANGE;
1277 }
1278 i--;
drh290c1942004-08-21 17:54:45 +00001279 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001280 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001281 pVar->flags = MEM_Null;
drh13f40da2014-08-22 18:00:11 +00001282 sqlite3Error(p->db, SQLITE_OK);
dan937d0de2009-10-15 18:35:38 +00001283
dan1d2ce4f2009-10-19 18:11:09 +00001284 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1285 ** binding a new value to this variable invalidates the current query plan.
drha7044002010-09-14 18:22:59 +00001286 **
1287 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1288 ** parameter in the WHERE clause might influence the choice of query plan
1289 ** for a statement, then the statement will be automatically recompiled,
1290 ** as if there had been a schema change, on the first sqlite3_step() call
1291 ** following any change to the bindings of that parameter.
dan1d2ce4f2009-10-19 18:11:09 +00001292 */
drh2c2f3922017-06-01 00:54:35 +00001293 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
drh29967962017-03-03 21:51:40 +00001294 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
dan937d0de2009-10-15 18:35:38 +00001295 p->expired = 1;
1296 }
drh4f26d6c2004-05-26 23:25:30 +00001297 return SQLITE_OK;
1298}
1299
1300/*
drh5e2517e2004-09-24 23:59:12 +00001301** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001302*/
drh5e2517e2004-09-24 23:59:12 +00001303static int bindText(
drh605264d2007-08-21 15:13:19 +00001304 sqlite3_stmt *pStmt, /* The statement to bind against */
1305 int i, /* Index of the parameter to bind */
1306 const void *zData, /* Pointer to the data to be bound */
1307 int nData, /* Number of bytes of data to be bound */
1308 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +00001309 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001310){
1311 Vdbe *p = (Vdbe *)pStmt;
1312 Mem *pVar;
1313 int rc;
1314
1315 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001316 if( rc==SQLITE_OK ){
1317 if( zData!=0 ){
1318 pVar = &p->aVar[i-1];
1319 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1320 if( rc==SQLITE_OK && encoding!=0 ){
1321 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1322 }
drhe70d01f2017-05-29 22:44:18 +00001323 if( rc ){
1324 sqlite3Error(p->db, rc);
1325 rc = sqlite3ApiExit(p->db, rc);
1326 }
drh27641702007-08-22 02:56:42 +00001327 }
drh488e7b62008-10-06 12:46:43 +00001328 sqlite3_mutex_leave(p->db->mutex);
drh94bb2ba2010-10-14 01:16:32 +00001329 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
drh6fec9ee2010-10-12 02:13:32 +00001330 xDel((void*)zData);
drh4f26d6c2004-05-26 23:25:30 +00001331 }
drh605264d2007-08-21 15:13:19 +00001332 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001333}
drh5e2517e2004-09-24 23:59:12 +00001334
1335
1336/*
1337** Bind a blob value to an SQL statement variable.
1338*/
1339int sqlite3_bind_blob(
1340 sqlite3_stmt *pStmt,
1341 int i,
1342 const void *zData,
1343 int nData,
1344 void (*xDel)(void*)
1345){
drh5b081d82016-02-18 01:29:12 +00001346#ifdef SQLITE_ENABLE_API_ARMOR
1347 if( nData<0 ) return SQLITE_MISUSE_BKPT;
1348#endif
drh5e2517e2004-09-24 23:59:12 +00001349 return bindText(pStmt, i, zData, nData, xDel, 0);
1350}
drhda4ca9d2014-09-09 17:27:35 +00001351int sqlite3_bind_blob64(
1352 sqlite3_stmt *pStmt,
1353 int i,
1354 const void *zData,
1355 sqlite3_uint64 nData,
1356 void (*xDel)(void*)
1357){
drhfc59a952014-09-11 23:34:55 +00001358 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001359 if( nData>0x7fffffff ){
1360 return invokeValueDestructor(zData, xDel, 0);
1361 }else{
drh3329a632014-09-18 01:21:43 +00001362 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
drhda4ca9d2014-09-09 17:27:35 +00001363 }
1364}
drh4f26d6c2004-05-26 23:25:30 +00001365int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1366 int rc;
1367 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +00001368 rc = vdbeUnbind(p, i);
1369 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001370 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +00001371 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001372 }
danielk1977f4618892004-06-28 13:09:11 +00001373 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001374}
1375int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +00001376 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001377}
drhefad9992004-06-22 12:13:55 +00001378int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001379 int rc;
1380 Vdbe *p = (Vdbe *)pStmt;
1381 rc = vdbeUnbind(p, i);
1382 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001383 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +00001384 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001385 }
1386 return rc;
1387}
drh605264d2007-08-21 15:13:19 +00001388int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1389 int rc;
1390 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +00001391 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001392 if( rc==SQLITE_OK ){
1393 sqlite3_mutex_leave(p->db->mutex);
1394 }
drh605264d2007-08-21 15:13:19 +00001395 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001396}
drh3a96a5d2017-06-30 23:09:03 +00001397int sqlite3_bind_pointer(sqlite3_stmt *pStmt, int i, void *pPtr){
1398 int rc;
1399 Vdbe *p = (Vdbe*)pStmt;
1400 rc = vdbeUnbind(p, i);
1401 if( rc==SQLITE_OK ){
1402 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr);
1403 sqlite3_mutex_leave(p->db->mutex);
1404 }
1405 return rc;
1406}
drh4f26d6c2004-05-26 23:25:30 +00001407int sqlite3_bind_text(
1408 sqlite3_stmt *pStmt,
1409 int i,
1410 const char *zData,
1411 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001412 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001413){
drh5e2517e2004-09-24 23:59:12 +00001414 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001415}
drhbbf483f2014-09-09 20:30:24 +00001416int sqlite3_bind_text64(
drhda4ca9d2014-09-09 17:27:35 +00001417 sqlite3_stmt *pStmt,
1418 int i,
1419 const char *zData,
1420 sqlite3_uint64 nData,
1421 void (*xDel)(void*),
1422 unsigned char enc
1423){
drhfc59a952014-09-11 23:34:55 +00001424 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35 +00001425 if( nData>0x7fffffff ){
1426 return invokeValueDestructor(zData, xDel, 0);
1427 }else{
drhfc59a952014-09-11 23:34:55 +00001428 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drh3329a632014-09-18 01:21:43 +00001429 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
drhda4ca9d2014-09-09 17:27:35 +00001430 }
1431}
drh6c626082004-11-14 21:56:29 +00001432#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001433int sqlite3_bind_text16(
1434 sqlite3_stmt *pStmt,
1435 int i,
1436 const void *zData,
1437 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001438 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001439){
drh5e2517e2004-09-24 23:59:12 +00001440 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001441}
drh6c626082004-11-14 21:56:29 +00001442#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001443int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1444 int rc;
drh1b27b8c2014-02-10 03:21:57 +00001445 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
drh29def562009-04-14 12:43:33 +00001446 case SQLITE_INTEGER: {
1447 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1448 break;
1449 }
1450 case SQLITE_FLOAT: {
drh74eaba42014-09-18 17:52:15 +00001451 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
drh29def562009-04-14 12:43:33 +00001452 break;
1453 }
1454 case SQLITE_BLOB: {
1455 if( pValue->flags & MEM_Zero ){
1456 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1457 }else{
1458 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1459 }
1460 break;
1461 }
1462 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001463 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1464 pValue->enc);
1465 break;
1466 }
1467 default: {
1468 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001469 break;
1470 }
danielk197726e41442006-06-14 15:16:35 +00001471 }
1472 return rc;
1473}
drhb026e052007-05-02 01:34:31 +00001474int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1475 int rc;
1476 Vdbe *p = (Vdbe *)pStmt;
1477 rc = vdbeUnbind(p, i);
1478 if( rc==SQLITE_OK ){
1479 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001480 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001481 }
1482 return rc;
1483}
dan80c03022015-07-24 17:36:34 +00001484int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
1485 int rc;
1486 Vdbe *p = (Vdbe *)pStmt;
1487 sqlite3_mutex_enter(p->db->mutex);
1488 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
1489 rc = SQLITE_TOOBIG;
1490 }else{
1491 assert( (n & 0x7FFFFFFF)==n );
1492 rc = sqlite3_bind_zeroblob(pStmt, i, n);
1493 }
1494 rc = sqlite3ApiExit(p->db, rc);
1495 sqlite3_mutex_leave(p->db->mutex);
1496 return rc;
1497}
drh75f6a032004-07-15 14:15:00 +00001498
1499/*
1500** Return the number of wildcards that can be potentially bound to.
1501** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001502*/
1503int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001504 Vdbe *p = (Vdbe*)pStmt;
1505 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001506}
drh895d7472004-08-20 16:02:39 +00001507
1508/*
drhfa6bc002004-09-07 16:19:52 +00001509** Return the name of a wildcard parameter. Return NULL if the index
1510** is out of range or if the wildcard is unnamed.
1511**
1512** The result is always UTF-8.
1513*/
1514const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1515 Vdbe *p = (Vdbe*)pStmt;
drh9bf755c2016-12-23 03:59:31 +00001516 if( p==0 ) return 0;
1517 return sqlite3VListNumToName(p->pVList, i);
drh895d7472004-08-20 16:02:39 +00001518}
drhfa6bc002004-09-07 16:19:52 +00001519
1520/*
1521** Given a wildcard parameter name, return the index of the variable
1522** with that name. If there is no variable with the given name,
1523** return 0.
1524*/
drh5f18a222009-11-26 14:01:53 +00001525int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
drh9bf755c2016-12-23 03:59:31 +00001526 if( p==0 || zName==0 ) return 0;
1527 return sqlite3VListNameToNum(p->pVList, zName, nName);
drhfa6bc002004-09-07 16:19:52 +00001528}
drh5f18a222009-11-26 14:01:53 +00001529int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1530 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1531}
drhf8db1bc2005-04-22 02:38:37 +00001532
drh51942bc2005-06-12 22:01:42 +00001533/*
drhf8db1bc2005-04-22 02:38:37 +00001534** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001535*/
shane145834a2008-09-04 12:03:42 +00001536int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001537 Vdbe *pFrom = (Vdbe*)pFromStmt;
1538 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001539 int i;
1540 assert( pTo->db==pFrom->db );
1541 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001542 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001543 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001544 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001545 }
drh27641702007-08-22 02:56:42 +00001546 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001547 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001548}
drh51942bc2005-06-12 22:01:42 +00001549
shaneeec556d2008-10-12 00:27:53 +00001550#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001551/*
shane145834a2008-09-04 12:03:42 +00001552** Deprecated external interface. Internal/core SQLite code
1553** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001554**
peter.d.reid60ec9142014-09-06 16:39:46 +00001555** It is misuse to call this routine with statements from different
drh0f5ea0b2009-04-09 14:02:44 +00001556** database connections. But as this is a deprecated interface, we
1557** will not bother to check for that condition.
1558**
1559** If the two statements contain a different number of bindings, then
1560** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1561** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001562*/
1563int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001564 Vdbe *pFrom = (Vdbe*)pFromStmt;
1565 Vdbe *pTo = (Vdbe*)pToStmt;
1566 if( pFrom->nVar!=pTo->nVar ){
1567 return SQLITE_ERROR;
1568 }
drh2c2f3922017-06-01 00:54:35 +00001569 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001570 if( pTo->expmask ){
danc94b8592009-10-20 07:01:24 +00001571 pTo->expired = 1;
1572 }
drh2c2f3922017-06-01 00:54:35 +00001573 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
danbda4cb82017-02-23 16:30:16 +00001574 if( pFrom->expmask ){
danc94b8592009-10-20 07:01:24 +00001575 pFrom->expired = 1;
1576 }
shane145834a2008-09-04 12:03:42 +00001577 return sqlite3TransferBindings(pFromStmt, pToStmt);
1578}
shaneeec556d2008-10-12 00:27:53 +00001579#endif
shane145834a2008-09-04 12:03:42 +00001580
1581/*
drh51942bc2005-06-12 22:01:42 +00001582** Return the sqlite3* database handle to which the prepared statement given
1583** in the argument belongs. This is the same database handle that was
1584** the first argument to the sqlite3_prepare() that was used to create
1585** the statement in the first place.
1586*/
1587sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1588 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1589}
drhbb5a9c32008-06-19 02:52:25 +00001590
1591/*
drhf03d9cc2010-11-16 23:10:25 +00001592** Return true if the prepared statement is guaranteed to not modify the
1593** database.
1594*/
1595int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1596 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1597}
1598
1599/*
drh2fb66932011-11-25 17:21:47 +00001600** Return true if the prepared statement is in need of being reset.
1601*/
1602int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
1603 Vdbe *v = (Vdbe*)pStmt;
drh76336d52016-10-01 11:39:53 +00001604 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
drh2fb66932011-11-25 17:21:47 +00001605}
1606
1607/*
drhbb5a9c32008-06-19 02:52:25 +00001608** Return a pointer to the next prepared statement after pStmt associated
1609** with database connection pDb. If pStmt is NULL, return the first
1610** prepared statement for the database connection. Return NULL if there
1611** are no more.
1612*/
1613sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1614 sqlite3_stmt *pNext;
drh9ca95732014-10-24 00:35:58 +00001615#ifdef SQLITE_ENABLE_API_ARMOR
1616 if( !sqlite3SafetyCheckOk(pDb) ){
1617 (void)SQLITE_MISUSE_BKPT;
1618 return 0;
1619 }
1620#endif
drhbb5a9c32008-06-19 02:52:25 +00001621 sqlite3_mutex_enter(pDb->mutex);
1622 if( pStmt==0 ){
1623 pNext = (sqlite3_stmt*)pDb->pVdbe;
1624 }else{
1625 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1626 }
1627 sqlite3_mutex_leave(pDb->mutex);
1628 return pNext;
1629}
drhd1d38482008-10-07 23:46:38 +00001630
1631/*
1632** Return the value of a status counter for a prepared statement
1633*/
1634int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1635 Vdbe *pVdbe = (Vdbe*)pStmt;
drh9ca95732014-10-24 00:35:58 +00001636 u32 v;
1637#ifdef SQLITE_ENABLE_API_ARMOR
1638 if( !pStmt ){
1639 (void)SQLITE_MISUSE_BKPT;
1640 return 0;
1641 }
1642#endif
drh3528f6b2017-05-31 16:21:54 +00001643 if( op==SQLITE_STMTSTATUS_MEMUSED ){
1644 sqlite3 *db = pVdbe->db;
1645 sqlite3_mutex_enter(db->mutex);
1646 v = 0;
1647 db->pnBytesFreed = (int*)&v;
1648 sqlite3VdbeClearObject(db, pVdbe);
1649 sqlite3DbFree(db, pVdbe);
1650 db->pnBytesFreed = 0;
1651 sqlite3_mutex_leave(db->mutex);
1652 }else{
1653 v = pVdbe->aCounter[op];
1654 if( resetFlag ) pVdbe->aCounter[op] = 0;
1655 }
drh9b47ee32013-08-20 03:13:51 +00001656 return (int)v;
drhd1d38482008-10-07 23:46:38 +00001657}
dan46c47d42011-03-01 18:42:07 +00001658
drh557341e2016-07-23 02:07:26 +00001659/*
1660** Return the SQL associated with a prepared statement
1661*/
1662const char *sqlite3_sql(sqlite3_stmt *pStmt){
1663 Vdbe *p = (Vdbe *)pStmt;
1664 return p ? p->zSql : 0;
1665}
1666
1667/*
1668** Return the SQL associated with a prepared statement with
1669** bound parameters expanded. Space to hold the returned string is
1670** obtained from sqlite3_malloc(). The caller is responsible for
1671** freeing the returned string by passing it to sqlite3_free().
1672**
1673** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1674** expanded bound parameters.
1675*/
1676char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1677#ifdef SQLITE_OMIT_TRACE
1678 return 0;
1679#else
1680 char *z = 0;
1681 const char *zSql = sqlite3_sql(pStmt);
1682 if( zSql ){
1683 Vdbe *p = (Vdbe *)pStmt;
1684 sqlite3_mutex_enter(p->db->mutex);
1685 z = sqlite3VdbeExpandSql(p, zSql);
1686 sqlite3_mutex_leave(p->db->mutex);
1687 }
1688 return z;
1689#endif
1690}
1691
drh9b1c62d2011-03-30 21:04:43 +00001692#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001693/*
dan93bca692011-09-14 19:41:44 +00001694** Allocate and populate an UnpackedRecord structure based on the serialized
1695** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
1696** if successful, or a NULL pointer if an OOM error is encountered.
1697*/
1698static UnpackedRecord *vdbeUnpackRecord(
1699 KeyInfo *pKeyInfo,
1700 int nKey,
1701 const void *pKey
1702){
dan93bca692011-09-14 19:41:44 +00001703 UnpackedRecord *pRet; /* Return value */
1704
drha582b012016-12-21 19:45:54 +00001705 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
dan93bca692011-09-14 19:41:44 +00001706 if( pRet ){
drhc8d985e2013-12-14 18:24:46 +00001707 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
dan93bca692011-09-14 19:41:44 +00001708 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
1709 }
1710 return pRet;
1711}
1712
1713/*
dan37db03b2011-03-16 19:59:18 +00001714** This function is called from within a pre-update callback to retrieve
1715** a field of the row currently being updated or deleted.
1716*/
dan46c47d42011-03-01 18:42:07 +00001717int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
1718 PreUpdate *p = db->pPreUpdate;
dan7271d7a2017-01-25 18:53:27 +00001719 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001720 int rc = SQLITE_OK;
1721
dan37db03b2011-03-16 19:59:18 +00001722 /* Test that this call is being made from within an SQLITE_DELETE or
1723 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
dan46c47d42011-03-01 18:42:07 +00001724 if( !p || p->op==SQLITE_INSERT ){
1725 rc = SQLITE_MISUSE_BKPT;
1726 goto preupdate_old_out;
1727 }
dancb9a3642017-01-30 19:44:53 +00001728 if( p->pPk ){
1729 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1730 }
dan46c47d42011-03-01 18:42:07 +00001731 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1732 rc = SQLITE_RANGE;
1733 goto preupdate_old_out;
1734 }
1735
dan37db03b2011-03-16 19:59:18 +00001736 /* If the old.* record has not yet been loaded into memory, do so now. */
dan46c47d42011-03-01 18:42:07 +00001737 if( p->pUnpacked==0 ){
dan12ca0b52011-03-21 16:17:42 +00001738 u32 nRec;
1739 u8 *aRec;
dan46c47d42011-03-01 18:42:07 +00001740
drha7c90c42016-06-04 20:37:10 +00001741 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
dan12ca0b52011-03-21 16:17:42 +00001742 aRec = sqlite3DbMallocRaw(db, nRec);
1743 if( !aRec ) goto preupdate_old_out;
drhcb3cabd2016-11-25 19:18:28 +00001744 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001745 if( rc==SQLITE_OK ){
dan93bca692011-09-14 19:41:44 +00001746 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
dan12ca0b52011-03-21 16:17:42 +00001747 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
1748 }
dan46c47d42011-03-01 18:42:07 +00001749 if( rc!=SQLITE_OK ){
dan12ca0b52011-03-21 16:17:42 +00001750 sqlite3DbFree(db, aRec);
dan46c47d42011-03-01 18:42:07 +00001751 goto preupdate_old_out;
1752 }
dan12ca0b52011-03-21 16:17:42 +00001753 p->aRecord = aRec;
dan46c47d42011-03-01 18:42:07 +00001754 }
1755
dan7271d7a2017-01-25 18:53:27 +00001756 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1757 if( iIdx==p->pTab->iPKey ){
1758 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
1759 }else if( iIdx>=p->pUnpacked->nField ){
dan46c47d42011-03-01 18:42:07 +00001760 *ppValue = (sqlite3_value *)columnNullValue();
dan7271d7a2017-01-25 18:53:27 +00001761 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1762 if( pMem->flags & MEM_Int ){
1763 sqlite3VdbeMemRealify(pMem);
dan319eeb72011-03-19 08:38:50 +00001764 }
dan46c47d42011-03-01 18:42:07 +00001765 }
1766
1767 preupdate_old_out:
drhe1ed0b02014-08-26 02:15:07 +00001768 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001769 return sqlite3ApiExit(db, rc);
1770}
drh9b1c62d2011-03-30 21:04:43 +00001771#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001772
drh9b1c62d2011-03-30 21:04:43 +00001773#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001774/*
1775** This function is called from within a pre-update callback to retrieve
1776** the number of columns in the row being updated, deleted or inserted.
1777*/
dan46c47d42011-03-01 18:42:07 +00001778int sqlite3_preupdate_count(sqlite3 *db){
1779 PreUpdate *p = db->pPreUpdate;
dane437ca52011-07-11 19:45:38 +00001780 return (p ? p->keyinfo.nField : 0);
dan46c47d42011-03-01 18:42:07 +00001781}
drh9b1c62d2011-03-30 21:04:43 +00001782#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00001783
drh9b1c62d2011-03-30 21:04:43 +00001784#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:18 +00001785/*
dan1e7a2d42011-03-22 18:45:29 +00001786** This function is designed to be called from within a pre-update callback
1787** only. It returns zero if the change that caused the callback was made
1788** immediately by a user SQL statement. Or, if the change was made by a
1789** trigger program, it returns the number of trigger programs currently
1790** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
1791** top-level trigger etc.).
1792**
1793** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
1794** or SET DEFAULT action is considered a trigger.
1795*/
1796int sqlite3_preupdate_depth(sqlite3 *db){
1797 PreUpdate *p = db->pPreUpdate;
1798 return (p ? p->v->nFrame : 0);
1799}
drh9b1c62d2011-03-30 21:04:43 +00001800#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan1e7a2d42011-03-22 18:45:29 +00001801
drh9b1c62d2011-03-30 21:04:43 +00001802#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan1e7a2d42011-03-22 18:45:29 +00001803/*
dan37db03b2011-03-16 19:59:18 +00001804** This function is called from within a pre-update callback to retrieve
1805** a field of the row currently being updated or inserted.
1806*/
1807int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
dan46c47d42011-03-01 18:42:07 +00001808 PreUpdate *p = db->pPreUpdate;
1809 int rc = SQLITE_OK;
dan37db03b2011-03-16 19:59:18 +00001810 Mem *pMem;
dan46c47d42011-03-01 18:42:07 +00001811
dan37db03b2011-03-16 19:59:18 +00001812 if( !p || p->op==SQLITE_DELETE ){
dan46c47d42011-03-01 18:42:07 +00001813 rc = SQLITE_MISUSE_BKPT;
dan37db03b2011-03-16 19:59:18 +00001814 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001815 }
dancb9a3642017-01-30 19:44:53 +00001816 if( p->pPk && p->op!=SQLITE_UPDATE ){
1817 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1818 }
dan46c47d42011-03-01 18:42:07 +00001819 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1820 rc = SQLITE_RANGE;
dan37db03b2011-03-16 19:59:18 +00001821 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:07 +00001822 }
dan46c47d42011-03-01 18:42:07 +00001823
dan37db03b2011-03-16 19:59:18 +00001824 if( p->op==SQLITE_INSERT ){
1825 /* For an INSERT, memory cell p->iNewReg contains the serialized record
1826 ** that is being inserted. Deserialize it. */
1827 UnpackedRecord *pUnpack = p->pNewUnpacked;
1828 if( !pUnpack ){
1829 Mem *pData = &p->v->aMem[p->iNewReg];
drhff535a22016-09-20 01:46:15 +00001830 rc = ExpandBlob(pData);
dan37db03b2011-03-16 19:59:18 +00001831 if( rc!=SQLITE_OK ) goto preupdate_new_out;
dan93bca692011-09-14 19:41:44 +00001832 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
dan37db03b2011-03-16 19:59:18 +00001833 if( !pUnpack ){
1834 rc = SQLITE_NOMEM;
1835 goto preupdate_new_out;
1836 }
1837 p->pNewUnpacked = pUnpack;
1838 }
dan2a86c192017-01-25 17:44:13 +00001839 pMem = &pUnpack->aMem[iIdx];
1840 if( iIdx==p->pTab->iPKey ){
1841 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1842 }else if( iIdx>=pUnpack->nField ){
dan37db03b2011-03-16 19:59:18 +00001843 pMem = (sqlite3_value *)columnNullValue();
dan37db03b2011-03-16 19:59:18 +00001844 }
1845 }else{
1846 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
1847 ** value. Make a copy of the cell contents and return a pointer to it.
1848 ** It is not safe to return a pointer to the memory cell itself as the
1849 ** caller may modify the value text encoding.
1850 */
1851 assert( p->op==SQLITE_UPDATE );
1852 if( !p->aNew ){
1853 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
1854 if( !p->aNew ){
1855 rc = SQLITE_NOMEM;
1856 goto preupdate_new_out;
1857 }
1858 }
drh304637c2011-03-18 16:47:27 +00001859 assert( iIdx>=0 && iIdx<p->pCsr->nField );
dan37db03b2011-03-16 19:59:18 +00001860 pMem = &p->aNew[iIdx];
1861 if( pMem->flags==0 ){
dane43635a2016-10-21 21:21:45 +00001862 if( iIdx==p->pTab->iPKey ){
dan319eeb72011-03-19 08:38:50 +00001863 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1864 }else{
1865 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
1866 if( rc!=SQLITE_OK ) goto preupdate_new_out;
1867 }
dan37db03b2011-03-16 19:59:18 +00001868 }
1869 }
1870 *ppValue = pMem;
1871
1872 preupdate_new_out:
drhe1ed0b02014-08-26 02:15:07 +00001873 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:07 +00001874 return sqlite3ApiExit(db, rc);
1875}
drh9b1c62d2011-03-30 21:04:43 +00001876#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
drh04e8a582014-11-18 21:20:57 +00001877
dan6f9702e2014-11-01 20:38:06 +00001878#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan04489b62014-10-31 20:11:32 +00001879/*
1880** Return status data for a single loop within query pStmt.
1881*/
1882int sqlite3_stmt_scanstatus(
drhd1a1c232014-11-03 16:35:55 +00001883 sqlite3_stmt *pStmt, /* Prepared statement being queried */
dan04489b62014-10-31 20:11:32 +00001884 int idx, /* Index of loop to report on */
drhd1a1c232014-11-03 16:35:55 +00001885 int iScanStatusOp, /* Which metric to return */
1886 void *pOut /* OUT: Write the answer here */
dan04489b62014-10-31 20:11:32 +00001887){
1888 Vdbe *p = (Vdbe*)pStmt;
dan037b5322014-11-03 11:25:32 +00001889 ScanStatus *pScan;
dan6f9702e2014-11-01 20:38:06 +00001890 if( idx<0 || idx>=p->nScan ) return 1;
1891 pScan = &p->aScan[idx];
drhd1a1c232014-11-03 16:35:55 +00001892 switch( iScanStatusOp ){
1893 case SQLITE_SCANSTAT_NLOOP: {
1894 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
1895 break;
1896 }
1897 case SQLITE_SCANSTAT_NVISIT: {
1898 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
1899 break;
1900 }
1901 case SQLITE_SCANSTAT_EST: {
drh518140e2014-11-06 03:55:10 +00001902 double r = 1.0;
1903 LogEst x = pScan->nEst;
1904 while( x<100 ){
1905 x += 10;
1906 r *= 0.5;
1907 }
1908 *(double*)pOut = r*sqlite3LogEstToInt(x);
drhd1a1c232014-11-03 16:35:55 +00001909 break;
1910 }
1911 case SQLITE_SCANSTAT_NAME: {
dand72219d2014-11-03 16:39:37 +00001912 *(const char**)pOut = pScan->zName;
drhd1a1c232014-11-03 16:35:55 +00001913 break;
1914 }
1915 case SQLITE_SCANSTAT_EXPLAIN: {
1916 if( pScan->addrExplain ){
1917 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
1918 }else{
1919 *(const char**)pOut = 0;
1920 }
1921 break;
1922 }
drhc6652b12014-11-06 04:42:20 +00001923 case SQLITE_SCANSTAT_SELECTID: {
1924 if( pScan->addrExplain ){
1925 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
1926 }else{
1927 *(int*)pOut = -1;
1928 }
1929 break;
1930 }
drhd1a1c232014-11-03 16:35:55 +00001931 default: {
1932 return 1;
dan6f9702e2014-11-01 20:38:06 +00001933 }
1934 }
dan04489b62014-10-31 20:11:32 +00001935 return 0;
1936}
1937
1938/*
1939** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
1940*/
1941void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
1942 Vdbe *p = (Vdbe*)pStmt;
dan6f9702e2014-11-01 20:38:06 +00001943 memset(p->anExec, 0, p->nOp * sizeof(i64));
dan04489b62014-10-31 20:11:32 +00001944}
dan6f9702e2014-11-01 20:38:06 +00001945#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */