Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: af90d4497a941eb5207ee1e80130f17ba8d6ea5a [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:301/*
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"
dan2adb3092022-12-06 18:48:0618#include "opcodes.h"
drh4f26d6c2004-05-26 23:25:3019
shaneeec556d2008-10-12 00:27:5320#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:5421/*
22** Return TRUE (non-zero) of the statement supplied as an argument needs
23** to be recompiled. A statement needs to be recompiled whenever the
24** execution environment changes in a way that would alter the program
25** that sqlite3_prepare() generates. For example, if new functions or
26** collating sequences are registered or if an authorizer function is
27** added or changed.
drhd89bd002005-01-22 03:03:5428*/
29int sqlite3_expired(sqlite3_stmt *pStmt){
30 Vdbe *p = (Vdbe*)pStmt;
31 return p==0 || p->expired;
32}
shaneeec556d2008-10-12 00:27:5333#endif
drhd89bd002005-01-22 03:03:5434
drhe30f4422007-08-21 16:15:5535/*
drh413c3d32010-02-23 20:11:5636** Check on a Vdbe to make sure it has not been finalized. Log
37** an error and return true if it has been finalized (or is otherwise
38** invalid). Return false if it is ok.
39*/
40static int vdbeSafety(Vdbe *p){
41 if( p->db==0 ){
42 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
43 return 1;
44 }else{
45 return 0;
46 }
47}
48static int vdbeSafetyNotNull(Vdbe *p){
49 if( p==0 ){
50 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
51 return 1;
52 }else{
53 return vdbeSafety(p);
54 }
55}
56
drh201e0c62015-07-14 14:48:5057#ifndef SQLITE_OMIT_TRACE
58/*
59** Invoke the profile callback. This routine is only called if we already
60** know that the profile callback is defined and needs to be invoked.
61*/
62static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
63 sqlite3_int64 iNow;
drh3d2a5292016-07-13 22:55:0164 sqlite3_int64 iElapse;
drh201e0c62015-07-14 14:48:5065 assert( p->startTime>0 );
drh201e0c62015-07-14 14:48:5066 assert( db->init.busy==0 );
67 assert( p->zSql!=0 );
68 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
drh3d2a5292016-07-13 22:55:0169 iElapse = (iNow - p->startTime)*1000000;
drh07891f02019-04-14 00:40:2970#ifndef SQLITE_OMIT_DEPRECATED
drh3d2a5292016-07-13 22:55:0171 if( db->xProfile ){
72 db->xProfile(db->pProfileArg, p->zSql, iElapse);
73 }
drh04c67472018-12-04 14:33:0274#endif
drh3d2a5292016-07-13 22:55:0175 if( db->mTrace & SQLITE_TRACE_PROFILE ){
drh08b92082020-08-10 14:18:0076 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
drh3d2a5292016-07-13 22:55:0177 }
drh201e0c62015-07-14 14:48:5078 p->startTime = 0;
79}
80/*
81** The checkProfileCallback(DB,P) macro checks to see if a profile callback
82** is needed, and it invokes the callback if it is needed.
83*/
84# define checkProfileCallback(DB,P) \
85 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
86#else
87# define checkProfileCallback(DB,P) /*no-op*/
88#endif
89
drh413c3d32010-02-23 20:11:5690/*
drhe30f4422007-08-21 16:15:5591** The following routine destroys a virtual machine that is created by
92** the sqlite3_compile() routine. The integer returned is an SQLITE_
93** success/failure code that describes the result of executing the virtual
94** machine.
95**
96** This routine sets the error code and string returned by
97** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
98*/
99int sqlite3_finalize(sqlite3_stmt *pStmt){
100 int rc;
101 if( pStmt==0 ){
drh65bafa62010-09-29 01:54:00102 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
103 ** pointer is a harmless no-op. */
drhe30f4422007-08-21 16:15:55104 rc = SQLITE_OK;
105 }else{
106 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06107 sqlite3 *db = v->db;
drh413c3d32010-02-23 20:11:56108 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
drh4245c402012-06-02 14:32:21109 sqlite3_mutex_enter(db->mutex);
drh201e0c62015-07-14 14:48:50110 checkProfileCallback(db, v);
drh2b294b52022-06-30 22:46:28111 assert( v->eVdbeState>=VDBE_READY_STATE );
112 rc = sqlite3VdbeReset(v);
113 sqlite3VdbeDelete(v);
danielk1977238746a2009-03-19 18:51:06114 rc = sqlite3ApiExit(db, rc);
drh4245c402012-06-02 14:32:21115 sqlite3LeaveMutexAndCloseZombie(db);
drhe30f4422007-08-21 16:15:55116 }
117 return rc;
118}
119
120/*
121** Terminate the current execution of an SQL statement and reset it
122** back to its starting state so that it can be reused. A success code from
123** the prior execution is returned.
124**
125** This routine sets the error code and string returned by
126** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
127*/
128int sqlite3_reset(sqlite3_stmt *pStmt){
129 int rc;
130 if( pStmt==0 ){
131 rc = SQLITE_OK;
132 }else{
133 Vdbe *v = (Vdbe*)pStmt;
drh201e0c62015-07-14 14:48:50134 sqlite3 *db = v->db;
135 sqlite3_mutex_enter(db->mutex);
136 checkProfileCallback(db, v);
drhc890fec2008-08-01 20:10:08137 rc = sqlite3VdbeReset(v);
drh124c0b42011-06-01 18:15:55138 sqlite3VdbeRewind(v);
drh201e0c62015-07-14 14:48:50139 assert( (rc & (db->errMask))==rc );
140 rc = sqlite3ApiExit(db, rc);
141 sqlite3_mutex_leave(db->mutex);
drhe30f4422007-08-21 16:15:55142 }
143 return rc;
144}
145
146/*
147** Set all the parameters in the compiled SQL statement to NULL.
148*/
149int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
150 int i;
151 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44152 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48153#if SQLITE_THREADSAFE
stephan4598b6e2023-10-22 13:09:37154 sqlite3_mutex *mutex;
155#endif
156#ifdef SQLITE_ENABLE_API_ARMOR
157 if( pStmt==0 ){
158 return SQLITE_MISUSE_BKPT;
159 }
160#endif
161#if SQLITE_THREADSAFE
162 mutex = p->db->mutex;
drh7e8b8482008-01-23 03:03:05163#endif
164 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44165 for(i=0; i<p->nVar; i++){
166 sqlite3VdbeMemRelease(&p->aVar[i]);
167 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55168 }
drh2c2f3922017-06-01 00:54:35169 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
danbda4cb82017-02-23 16:30:16170 if( p->expmask ){
danc94b8592009-10-20 07:01:24171 p->expired = 1;
172 }
drh7e8b8482008-01-23 03:03:05173 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55174 return rc;
175}
176
177
drh4f26d6c2004-05-26 23:25:30178/**************************** sqlite3_value_ *******************************
179** The following routines extract information from a Mem or sqlite3_value
180** structure.
181*/
182const void *sqlite3_value_blob(sqlite3_value *pVal){
183 Mem *p = (Mem*)pVal;
184 if( p->flags & (MEM_Blob|MEM_Str) ){
drhff535a22016-09-20 01:46:15185 if( ExpandBlob(p)!=SQLITE_OK ){
dana4d5ae82015-07-24 16:24:37186 assert( p->flags==MEM_Null && p->z==0 );
187 return 0;
188 }
drh1f0feef2007-05-15 13:27:07189 p->flags |= MEM_Blob;
drh42262532010-09-08 16:30:36190 return p->n ? p->z : 0;
drh4f26d6c2004-05-26 23:25:30191 }else{
192 return sqlite3_value_text(pVal);
193 }
194}
195int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56196 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30197}
198int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56199 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30200}
201double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33202 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30203}
204int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45205 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30206}
drhefad9992004-06-22 12:13:55207sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33208 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30209}
drhbcdf78a2015-09-10 20:34:56210unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
dan5b6c8e42016-01-30 15:46:03211 Mem *pMem = (Mem*)pVal;
212 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
drhbcdf78a2015-09-10 20:34:56213}
drhae3ec3f2017-07-17 00:40:19214void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
drh3a96a5d2017-06-30 23:09:03215 Mem *p = (Mem*)pVal;
drha0024e62017-07-27 15:53:24216 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
217 (MEM_Null|MEM_Term|MEM_Subtype)
drhae3ec3f2017-07-17 00:40:19218 && zPType!=0
drha0024e62017-07-27 15:53:24219 && p->eSubtype=='p'
drh22930062017-07-27 03:48:02220 && strcmp(p->u.zPType, zPType)==0
drhae3ec3f2017-07-17 00:40:19221 ){
drh22930062017-07-27 03:48:02222 return (void*)p->z;
drh3a96a5d2017-06-30 23:09:03223 }else{
224 return 0;
225 }
226}
drh4f26d6c2004-05-26 23:25:30227const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56228 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30229}
drh6c626082004-11-14 21:56:29230#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30231const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56232 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30233}
danielk1977d8123362004-06-12 09:25:12234const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56235 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12236}
237const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56238 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12239}
drh6c626082004-11-14 21:56:29240#endif /* SQLITE_OMIT_UTF16 */
drh22ec1342015-02-27 00:33:15241/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
242** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
243** point number string BLOB NULL
244*/
drh4f26d6c2004-05-26 23:25:30245int sqlite3_value_type(sqlite3_value* pVal){
drh1b27b8c2014-02-10 03:21:57246 static const u8 aType[] = {
drhf2566c42019-05-03 17:08:16247 SQLITE_BLOB, /* 0x00 (not possible) */
248 SQLITE_NULL, /* 0x01 NULL */
249 SQLITE_TEXT, /* 0x02 TEXT */
250 SQLITE_NULL, /* 0x03 (not possible) */
251 SQLITE_INTEGER, /* 0x04 INTEGER */
252 SQLITE_NULL, /* 0x05 (not possible) */
253 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */
254 SQLITE_NULL, /* 0x07 (not possible) */
255 SQLITE_FLOAT, /* 0x08 FLOAT */
256 SQLITE_NULL, /* 0x09 (not possible) */
257 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */
258 SQLITE_NULL, /* 0x0b (not possible) */
259 SQLITE_INTEGER, /* 0x0c (not possible) */
260 SQLITE_NULL, /* 0x0d (not possible) */
261 SQLITE_INTEGER, /* 0x0e (not possible) */
262 SQLITE_NULL, /* 0x0f (not possible) */
263 SQLITE_BLOB, /* 0x10 BLOB */
264 SQLITE_NULL, /* 0x11 (not possible) */
265 SQLITE_TEXT, /* 0x12 (not possible) */
266 SQLITE_NULL, /* 0x13 (not possible) */
267 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */
268 SQLITE_NULL, /* 0x15 (not possible) */
269 SQLITE_INTEGER, /* 0x16 (not possible) */
270 SQLITE_NULL, /* 0x17 (not possible) */
271 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */
272 SQLITE_NULL, /* 0x19 (not possible) */
273 SQLITE_FLOAT, /* 0x1a (not possible) */
274 SQLITE_NULL, /* 0x1b (not possible) */
275 SQLITE_INTEGER, /* 0x1c (not possible) */
276 SQLITE_NULL, /* 0x1d (not possible) */
277 SQLITE_INTEGER, /* 0x1e (not possible) */
278 SQLITE_NULL, /* 0x1f (not possible) */
279 SQLITE_FLOAT, /* 0x20 INTREAL */
280 SQLITE_NULL, /* 0x21 (not possible) */
drhcd0f5402023-04-04 18:55:31281 SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */
drhf2566c42019-05-03 17:08:16282 SQLITE_NULL, /* 0x23 (not possible) */
283 SQLITE_FLOAT, /* 0x24 (not possible) */
284 SQLITE_NULL, /* 0x25 (not possible) */
285 SQLITE_FLOAT, /* 0x26 (not possible) */
286 SQLITE_NULL, /* 0x27 (not possible) */
287 SQLITE_FLOAT, /* 0x28 (not possible) */
288 SQLITE_NULL, /* 0x29 (not possible) */
289 SQLITE_FLOAT, /* 0x2a (not possible) */
290 SQLITE_NULL, /* 0x2b (not possible) */
291 SQLITE_FLOAT, /* 0x2c (not possible) */
292 SQLITE_NULL, /* 0x2d (not possible) */
293 SQLITE_FLOAT, /* 0x2e (not possible) */
294 SQLITE_NULL, /* 0x2f (not possible) */
295 SQLITE_BLOB, /* 0x30 (not possible) */
296 SQLITE_NULL, /* 0x31 (not possible) */
297 SQLITE_TEXT, /* 0x32 (not possible) */
298 SQLITE_NULL, /* 0x33 (not possible) */
299 SQLITE_FLOAT, /* 0x34 (not possible) */
300 SQLITE_NULL, /* 0x35 (not possible) */
301 SQLITE_FLOAT, /* 0x36 (not possible) */
302 SQLITE_NULL, /* 0x37 (not possible) */
303 SQLITE_FLOAT, /* 0x38 (not possible) */
304 SQLITE_NULL, /* 0x39 (not possible) */
305 SQLITE_FLOAT, /* 0x3a (not possible) */
306 SQLITE_NULL, /* 0x3b (not possible) */
307 SQLITE_FLOAT, /* 0x3c (not possible) */
308 SQLITE_NULL, /* 0x3d (not possible) */
309 SQLITE_FLOAT, /* 0x3e (not possible) */
310 SQLITE_NULL, /* 0x3f (not possible) */
drh1b27b8c2014-02-10 03:21:57311 };
drhde7109e2019-05-02 17:45:52312#ifdef SQLITE_DEBUG
313 {
314 int eType = SQLITE_BLOB;
315 if( pVal->flags & MEM_Null ){
316 eType = SQLITE_NULL;
drh169f0772019-05-02 21:36:26317 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
drhde7109e2019-05-02 17:45:52318 eType = SQLITE_FLOAT;
drh169f0772019-05-02 21:36:26319 }else if( pVal->flags & MEM_Int ){
320 eType = SQLITE_INTEGER;
drhde7109e2019-05-02 17:45:52321 }else if( pVal->flags & MEM_Str ){
322 eType = SQLITE_TEXT;
323 }
324 assert( eType == aType[pVal->flags&MEM_AffMask] );
325 }
326#endif
mistachkinafc14f72014-03-05 01:29:18327 return aType[pVal->flags&MEM_AffMask];
drh4f26d6c2004-05-26 23:25:30328}
drh47996ea2022-10-12 12:49:29329int sqlite3_value_encoding(sqlite3_value *pVal){
330 return pVal->enc;
331}
drh4f26d6c2004-05-26 23:25:30332
drhce2fbd12018-01-12 21:00:14333/* Return true if a parameter to xUpdate represents an unchanged column */
334int sqlite3_value_nochange(sqlite3_value *pVal){
335 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
336}
337
drh57b1a3e2019-03-29 11:13:37338/* Return true if a parameter value originated from an sqlite3_bind() */
339int sqlite3_value_frombind(sqlite3_value *pVal){
340 return (pVal->flags&MEM_FromBind)!=0;
341}
342
drh4f03f412015-05-20 21:28:32343/* Make a copy of an sqlite3_value object
344*/
345sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
346 sqlite3_value *pNew;
347 if( pOrig==0 ) return 0;
348 pNew = sqlite3_malloc( sizeof(*pNew) );
349 if( pNew==0 ) return 0;
350 memset(pNew, 0, sizeof(*pNew));
351 memcpy(pNew, pOrig, MEMCELLSIZE);
352 pNew->flags &= ~MEM_Dyn;
353 pNew->db = 0;
354 if( pNew->flags&(MEM_Str|MEM_Blob) ){
drh10ca5b42015-05-22 21:04:54355 pNew->flags &= ~(MEM_Static|MEM_Dyn);
356 pNew->flags |= MEM_Ephem;
357 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
358 sqlite3ValueFree(pNew);
359 pNew = 0;
drh4f03f412015-05-20 21:28:32360 }
drh83665292022-03-14 23:50:38361 }else if( pNew->flags & MEM_Null ){
362 /* Do not duplicate pointer values */
363 pNew->flags &= ~(MEM_Term|MEM_Subtype);
drh4f03f412015-05-20 21:28:32364 }
365 return pNew;
366}
367
368/* Destroy an sqlite3_value object previously obtained from
369** sqlite3_value_dup().
370*/
371void sqlite3_value_free(sqlite3_value *pOld){
372 sqlite3ValueFree(pOld);
373}
larrybrbc917382023-06-07 08:40:31374
drh4f03f412015-05-20 21:28:32375
drh4f26d6c2004-05-26 23:25:30376/**************************** sqlite3_result_ *******************************
377** The following routines are used by user-defined functions to specify
378** the function result.
drh4c8555f2009-06-25 01:47:11379**
peter.d.reid60ec9142014-09-06 16:39:46380** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
drhdbe349d2021-10-13 13:00:34381** result as a string or blob. Appropriate errors are set if the string/blob
382** is too big or if an OOM occurs.
drhda4ca9d2014-09-09 17:27:35383**
384** The invokeValueDestructor(P,X) routine invokes destructor function X()
drh42b49a32023-10-17 18:28:27385** on value P if P is not going to be used and need to be destroyed.
drh4f26d6c2004-05-26 23:25:30386*/
drh4c8555f2009-06-25 01:47:11387static void setResultStrOrError(
388 sqlite3_context *pCtx, /* Function context */
389 const char *z, /* String pointer */
390 int n, /* Bytes in string, or negative */
391 u8 enc, /* Encoding of z. 0 for BLOBs */
392 void (*xDel)(void*) /* Destructor function */
393){
drhfb92e072022-03-29 01:43:09394 Mem *pOut = pCtx->pOut;
395 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
drhdbe349d2021-10-13 13:00:34396 if( rc ){
397 if( rc==SQLITE_TOOBIG ){
398 sqlite3_result_error_toobig(pCtx);
399 }else{
400 /* The only errors possible from sqlite3VdbeMemSetStr are
401 ** SQLITE_TOOBIG and SQLITE_NOMEM */
402 assert( rc==SQLITE_NOMEM );
403 sqlite3_result_error_nomem(pCtx);
404 }
drhfb92e072022-03-29 01:43:09405 return;
406 }
drh659fdb42022-04-01 15:31:58407 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
drhc55b62d2022-03-29 22:57:00408 if( sqlite3VdbeMemTooBig(pOut) ){
409 sqlite3_result_error_toobig(pCtx);
drh4c8555f2009-06-25 01:47:11410 }
411}
drhda4ca9d2014-09-09 17:27:35412static int invokeValueDestructor(
413 const void *p, /* Value to destroy */
414 void (*xDel)(void*), /* The destructor */
stephanc76e4502023-10-14 16:29:36415 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */
drhda4ca9d2014-09-09 17:27:35416){
drhfc59a952014-09-11 23:34:55417 assert( xDel!=SQLITE_DYNAMIC );
drhda4ca9d2014-09-09 17:27:35418 if( xDel==0 ){
419 /* noop */
420 }else if( xDel==SQLITE_TRANSIENT ){
421 /* noop */
drhda4ca9d2014-09-09 17:27:35422 }else{
423 xDel((void*)p);
424 }
drh42b49a32023-10-17 18:28:27425#ifdef SQLITE_ENABLE_API_ARMOR
stephanc76e4502023-10-14 16:29:36426 if( pCtx!=0 ){
427 sqlite3_result_error_toobig(pCtx);
428 }
drh42b49a32023-10-17 18:28:27429#else
430 assert( pCtx!=0 );
drhd6228552021-06-09 14:45:02431 sqlite3_result_error_toobig(pCtx);
drh42b49a32023-10-17 18:28:27432#endif
drhda4ca9d2014-09-09 17:27:35433 return SQLITE_TOOBIG;
434}
drh4f26d6c2004-05-26 23:25:30435void sqlite3_result_blob(
larrybrbc917382023-06-07 08:40:31436 sqlite3_context *pCtx,
437 const void *z,
438 int n,
danielk1977d8123362004-06-12 09:25:12439 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30440){
stephanc76e4502023-10-14 16:29:36441#ifdef SQLITE_ENABLE_API_ARMOR
442 if( pCtx==0 || n<0 ){
443 invokeValueDestructor(z, xDel, pCtx);
444 return;
445 }
446#endif
drh5708d2d2005-06-22 10:53:59447 assert( n>=0 );
drh9bd038f2014-08-27 14:14:06448 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11449 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30450}
drhda4ca9d2014-09-09 17:27:35451void sqlite3_result_blob64(
larrybrbc917382023-06-07 08:40:31452 sqlite3_context *pCtx,
453 const void *z,
drhda4ca9d2014-09-09 17:27:35454 sqlite3_uint64 n,
455 void (*xDel)(void *)
456){
drhfc59a952014-09-11 23:34:55457 assert( xDel!=SQLITE_DYNAMIC );
stephanc76e4502023-10-14 16:29:36458#ifdef SQLITE_ENABLE_API_ARMOR
459 if( pCtx==0 ){
460 invokeValueDestructor(z, xDel, 0);
461 return;
462 }
463#endif
464 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhda4ca9d2014-09-09 17:27:35465 if( n>0x7fffffff ){
466 (void)invokeValueDestructor(z, xDel, pCtx);
467 }else{
468 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
469 }
470}
drh4f26d6c2004-05-26 23:25:30471void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
stephanc76e4502023-10-14 16:29:36472#ifdef SQLITE_ENABLE_API_ARMOR
473 if( pCtx==0 ) return;
474#endif
drh9bd038f2014-08-27 14:14:06475 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
476 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
drh4f26d6c2004-05-26 23:25:30477}
478void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
stephanc76e4502023-10-14 16:29:36479#ifdef SQLITE_ENABLE_API_ARMOR
480 if( pCtx==0 ) return;
481#endif
drh9bd038f2014-08-27 14:14:06482 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34483 pCtx->isError = SQLITE_ERROR;
drh9bd038f2014-08-27 14:14:06484 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30485}
danielk1977a1686c92006-01-23 07:52:37486#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30487void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
stephanc76e4502023-10-14 16:29:36488#ifdef SQLITE_ENABLE_API_ARMOR
489 if( pCtx==0 ) return;
490#endif
drh9bd038f2014-08-27 14:14:06491 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh69544ec2008-02-06 14:11:34492 pCtx->isError = SQLITE_ERROR;
drh9bd038f2014-08-27 14:14:06493 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30494}
danielk1977a1686c92006-01-23 07:52:37495#endif
drhf4479502004-05-27 03:12:53496void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
stephanc76e4502023-10-14 16:29:36497#ifdef SQLITE_ENABLE_API_ARMOR
498 if( pCtx==0 ) return;
499#endif
drh9bd038f2014-08-27 14:14:06500 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
501 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
drh4f26d6c2004-05-26 23:25:30502}
503void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
stephanc76e4502023-10-14 16:29:36504#ifdef SQLITE_ENABLE_API_ARMOR
505 if( pCtx==0 ) return;
506#endif
drh9bd038f2014-08-27 14:14:06507 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
508 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
drh4f26d6c2004-05-26 23:25:30509}
510void sqlite3_result_null(sqlite3_context *pCtx){
stephanc76e4502023-10-14 16:29:36511#ifdef SQLITE_ENABLE_API_ARMOR
512 if( pCtx==0 ) return;
513#endif
drh9bd038f2014-08-27 14:14:06514 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
515 sqlite3VdbeMemSetNull(pCtx->pOut);
drh4f26d6c2004-05-26 23:25:30516}
drh22930062017-07-27 03:48:02517void sqlite3_result_pointer(
518 sqlite3_context *pCtx,
519 void *pPtr,
520 const char *zPType,
521 void (*xDestructor)(void*)
522){
stephanc76e4502023-10-14 16:29:36523 Mem *pOut;
524#ifdef SQLITE_ENABLE_API_ARMOR
525 if( pCtx==0 ){
526 invokeValueDestructor(pPtr, xDestructor, 0);
527 return;
528 }
529#endif
530 pOut = pCtx->pOut;
drh3a96a5d2017-06-30 23:09:03531 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh526740b2017-08-24 13:55:46532 sqlite3VdbeMemRelease(pOut);
533 pOut->flags = MEM_Null;
drh22930062017-07-27 03:48:02534 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
drh3a96a5d2017-06-30 23:09:03535}
drhbcdf78a2015-09-10 20:34:56536void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
stephanc76e4502023-10-14 16:29:36537 Mem *pOut;
538#ifdef SQLITE_ENABLE_API_ARMOR
539 if( pCtx==0 ) return;
540#endif
drh6eb381f2023-11-09 12:58:03541#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
drhb10c3d32023-11-09 15:01:56542 if( pCtx->pFunc!=0
543 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
544 ){
drh6eb381f2023-11-09 12:58:03545 char zErr[200];
546 sqlite3_snprintf(sizeof(zErr), zErr,
547 "misuse of sqlite3_result_subtype() by %s()",
548 pCtx->pFunc->zName);
549 sqlite3_result_error(pCtx, zErr, -1);
550 return;
551 }
552#endif /* SQLITE_STRICT_SUBTYPE */
stephanc76e4502023-10-14 16:29:36553 pOut = pCtx->pOut;
dan5b6c8e42016-01-30 15:46:03554 assert( sqlite3_mutex_held(pOut->db->mutex) );
555 pOut->eSubtype = eSubtype & 0xff;
556 pOut->flags |= MEM_Subtype;
drhbcdf78a2015-09-10 20:34:56557}
drh4f26d6c2004-05-26 23:25:30558void sqlite3_result_text(
larrybrbc917382023-06-07 08:40:31559 sqlite3_context *pCtx,
560 const char *z,
drh4f26d6c2004-05-26 23:25:30561 int n,
danielk1977d8123362004-06-12 09:25:12562 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30563){
stephanc76e4502023-10-14 16:29:36564#ifdef SQLITE_ENABLE_API_ARMOR
565 if( pCtx==0 ){
566 invokeValueDestructor(z, xDel, 0);
567 return;
568 }
569#endif
drh9bd038f2014-08-27 14:14:06570 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh4c8555f2009-06-25 01:47:11571 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30572}
drhbbf483f2014-09-09 20:30:24573void sqlite3_result_text64(
larrybrbc917382023-06-07 08:40:31574 sqlite3_context *pCtx,
575 const char *z,
drhda4ca9d2014-09-09 17:27:35576 sqlite3_uint64 n,
577 void (*xDel)(void *),
578 unsigned char enc
579){
stephanc76e4502023-10-14 16:29:36580#ifdef SQLITE_ENABLE_API_ARMOR
581 if( pCtx==0 ){
582 invokeValueDestructor(z, xDel, 0);
583 return;
584 }
585#endif
drhda4ca9d2014-09-09 17:27:35586 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfc59a952014-09-11 23:34:55587 assert( xDel!=SQLITE_DYNAMIC );
drhd2603ad2022-12-02 17:52:52588 if( enc!=SQLITE_UTF8 ){
589 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
590 n &= ~(u64)1;
591 }
drhda4ca9d2014-09-09 17:27:35592 if( n>0x7fffffff ){
593 (void)invokeValueDestructor(z, xDel, pCtx);
594 }else{
595 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
drh569700a2023-07-21 18:09:07596 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
drhda4ca9d2014-09-09 17:27:35597 }
598}
drh6c626082004-11-14 21:56:29599#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30600void sqlite3_result_text16(
larrybrbc917382023-06-07 08:40:31601 sqlite3_context *pCtx,
602 const void *z,
603 int n,
danielk1977d8123362004-06-12 09:25:12604 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30605){
drh9bd038f2014-08-27 14:14:06606 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhd2603ad2022-12-02 17:52:52607 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12608}
609void sqlite3_result_text16be(
larrybrbc917382023-06-07 08:40:31610 sqlite3_context *pCtx,
611 const void *z,
612 int n,
danielk1977d8123362004-06-12 09:25:12613 void (*xDel)(void *)
614){
drh9bd038f2014-08-27 14:14:06615 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhd2603ad2022-12-02 17:52:52616 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12617}
618void sqlite3_result_text16le(
larrybrbc917382023-06-07 08:40:31619 sqlite3_context *pCtx,
620 const void *z,
621 int n,
danielk1977d8123362004-06-12 09:25:12622 void (*xDel)(void *)
623){
drh9bd038f2014-08-27 14:14:06624 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhd2603ad2022-12-02 17:52:52625 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30626}
drh6c626082004-11-14 21:56:29627#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30628void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
stephan7a54b122023-10-14 14:53:18629 Mem *pOut;
630
631#ifdef SQLITE_ENABLE_API_ARMOR
632 if( pCtx==0 ) return;
633 if( pValue==0 ){
634 sqlite3_result_null(pCtx);
635 return;
636 }
637#endif
638 pOut = pCtx->pOut;
drh9bd038f2014-08-27 14:14:06639 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhfb92e072022-03-29 01:43:09640 sqlite3VdbeMemCopy(pOut, pValue);
drh659fdb42022-04-01 15:31:58641 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
drhfb92e072022-03-29 01:43:09642 if( sqlite3VdbeMemTooBig(pOut) ){
643 sqlite3_result_error_toobig(pCtx);
644 }
drh4f26d6c2004-05-26 23:25:30645}
drhb026e052007-05-02 01:34:31646void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drhfb92e072022-03-29 01:43:09647 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);
drhb026e052007-05-02 01:34:31648}
dana4d5ae82015-07-24 16:24:37649int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
stephanc76e4502023-10-14 16:29:36650 Mem *pOut;
651
652#ifdef SQLITE_ENABLE_API_ARMOR
653 if( pCtx==0 ) return SQLITE_MISUSE_BKPT;
654#endif
655 pOut = pCtx->pOut;
dana4d5ae82015-07-24 16:24:37656 assert( sqlite3_mutex_held(pOut->db->mutex) );
drh6bacdc22015-07-24 17:14:03657 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhfb92e072022-03-29 01:43:09658 sqlite3_result_error_toobig(pCtx);
dana4d5ae82015-07-24 16:24:37659 return SQLITE_TOOBIG;
660 }
dana32536b2021-11-08 19:35:26661#ifndef SQLITE_OMIT_INCRBLOB
dana4d5ae82015-07-24 16:24:37662 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
663 return SQLITE_OK;
dana32536b2021-11-08 19:35:26664#else
665 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
666#endif
dana4d5ae82015-07-24 16:24:37667}
drh69544ec2008-02-06 14:11:34668void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
stephanc76e4502023-10-14 16:29:36669#ifdef SQLITE_ENABLE_API_ARMOR
670 if( pCtx==0 ) return;
671#endif
drhf09ac0b2018-01-23 03:44:06672 pCtx->isError = errCode ? errCode : -1;
drh983b5ee2015-02-13 12:05:56673#ifdef SQLITE_DEBUG
drh96f4ad22015-03-12 21:02:36674 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
drh983b5ee2015-02-13 12:05:56675#endif
drh9bd038f2014-08-27 14:14:06676 if( pCtx->pOut->flags & MEM_Null ){
drhfb92e072022-03-29 01:43:09677 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
678 SQLITE_STATIC);
drh51c7d862009-04-27 18:46:06679 }
drh69544ec2008-02-06 14:11:34680}
drh4f26d6c2004-05-26 23:25:30681
drha0206bc2007-05-08 15:15:02682/* Force an SQLITE_TOOBIG error. */
683void sqlite3_result_error_toobig(sqlite3_context *pCtx){
stephanc76e4502023-10-14 16:29:36684#ifdef SQLITE_ENABLE_API_ARMOR
685 if( pCtx==0 ) return;
686#endif
drh9bd038f2014-08-27 14:14:06687 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drhbb4957f2008-03-20 14:03:29688 pCtx->isError = SQLITE_TOOBIG;
larrybrbc917382023-06-07 08:40:31689 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
drhbb4957f2008-03-20 14:03:29690 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02691}
692
danielk1977a1644fd2007-08-29 12:31:25693/* An SQLITE_NOMEM error. */
694void sqlite3_result_error_nomem(sqlite3_context *pCtx){
stephanc76e4502023-10-14 16:29:36695#ifdef SQLITE_ENABLE_API_ARMOR
696 if( pCtx==0 ) return;
697#endif
drh9bd038f2014-08-27 14:14:06698 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
699 sqlite3VdbeMemSetNull(pCtx->pOut);
mistachkinfad30392016-02-13 23:43:46700 pCtx->isError = SQLITE_NOMEM_BKPT;
drh4a642b62016-02-05 01:55:27701 sqlite3OomFault(pCtx->pOut->db);
danielk1977a1644fd2007-08-29 12:31:25702}
drh4f26d6c2004-05-26 23:25:30703
drh0c8f4032019-05-03 21:17:28704#ifndef SQLITE_UNTESTABLE
705/* Force the INT64 value currently stored as the result to be
706** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
707** test-control.
708*/
larrybrbc917382023-06-07 08:40:31709void sqlite3ResultIntReal(sqlite3_context *pCtx){
drh0c8f4032019-05-03 21:17:28710 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
711 if( pCtx->pOut->flags & MEM_Int ){
712 pCtx->pOut->flags &= ~MEM_Int;
713 pCtx->pOut->flags |= MEM_IntReal;
714 }
715}
716#endif
717
718
dan5cf53532010-05-01 16:40:20719/*
larrybrbc917382023-06-07 08:40:31720** This function is called after a transaction has been committed. It
dan5cf53532010-05-01 16:40:20721** invokes callbacks registered with sqlite3_wal_hook() as required.
722*/
drh7ed91f22010-04-29 22:34:07723static int doWalCallbacks(sqlite3 *db){
dan8d22a172010-04-19 18:03:51724 int rc = SQLITE_OK;
dan5cf53532010-05-01 16:40:20725#ifndef SQLITE_OMIT_WAL
726 int i;
dan8d22a172010-04-19 18:03:51727 for(i=0; i<db->nDb; i++){
728 Btree *pBt = db->aDb[i].pBt;
729 if( pBt ){
drhef15c6e2014-12-12 01:27:17730 int nEntry;
731 sqlite3BtreeEnter(pBt);
732 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
733 sqlite3BtreeLeave(pBt);
drh59533aa2017-08-02 18:28:26734 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
drh69c33822016-08-18 14:33:11735 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
dan8d22a172010-04-19 18:03:51736 }
737 }
738 }
dan5cf53532010-05-01 16:40:20739#endif
dan8d22a172010-04-19 18:03:51740 return rc;
741}
742
drh201e0c62015-07-14 14:48:50743
drh4f26d6c2004-05-26 23:25:30744/*
745** Execute the statement pStmt, either until a row of data is ready, the
746** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53747**
748** This routine implements the bulk of the logic behind the sqlite_step()
larrybrbc917382023-06-07 08:40:31749** API. The only thing omitted is the automatic recompile if a
drhb900aaf2006-11-09 00:24:53750** schema change has occurred. That detail is handled by the
751** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30752*/
drhb900aaf2006-11-09 00:24:53753static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11754 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30755 int rc;
756
drh99a21822022-03-31 21:15:09757 assert(p);
drh35e9e352022-04-01 19:13:39758 db = p->db;
drh99a21822022-03-31 21:15:09759 if( p->eVdbeState!=VDBE_RUN_STATE ){
760 restart_step:
761 if( p->eVdbeState==VDBE_READY_STATE ){
762 if( p->expired ){
763 p->rc = SQLITE_SCHEMA;
764 rc = SQLITE_ERROR;
765 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
larrybrbc917382023-06-07 08:40:31766 /* If this statement was prepared using saved SQL and an
drh99a21822022-03-31 21:15:09767 ** error has occurred, then return the error code in p->rc to the
768 ** caller. Set the error code in the database handle to the same
769 ** value.
larrybrbc917382023-06-07 08:40:31770 */
drh99a21822022-03-31 21:15:09771 rc = sqlite3VdbeTransferError(p);
772 }
773 goto end_of_step;
774 }
drh15ca1df2006-07-26 13:43:30775
drh99a21822022-03-31 21:15:09776 /* If there are no other statements currently running, then
777 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
778 ** from interrupting a statement that has not yet started.
779 */
780 if( db->nVdbeActive==0 ){
781 AtomicStore(&db->u1.isInterrupted, 0);
782 }
783
larrybrbc917382023-06-07 08:40:31784 assert( db->nVdbeWrite>0 || db->autoCommit==0
dan467e20a2025-01-20 18:26:58785 || ((db->nDeferredCons + db->nDeferredImmCons)==0)
drh99a21822022-03-31 21:15:09786 );
dan1da40a32009-09-19 17:00:31787
drh19e2d372005-08-29 23:00:03788#ifndef SQLITE_OMIT_TRACE
drh99a21822022-03-31 21:15:09789 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
790 && !db->init.busy && p->zSql ){
791 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
792 }else{
793 assert( p->startTime==0 );
794 }
drh19e2d372005-08-29 23:00:03795#endif
drhc16a03b2004-09-15 13:38:10796
drh99a21822022-03-31 21:15:09797 db->nVdbeActive++;
798 if( p->readOnly==0 ) db->nVdbeWrite++;
799 if( p->bIsReader ) db->nVdbeRead++;
800 p->pc = 0;
801 p->eVdbeState = VDBE_RUN_STATE;
802 }else
803
drh17c48652022-04-01 17:01:57804 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
drh99a21822022-03-31 21:15:09805 /* We used to require that sqlite3_reset() be called before retrying
806 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
807 ** with version 3.7.0, we changed this so that sqlite3_reset() would
808 ** be called automatically instead of throwing the SQLITE_MISUSE error.
larrybrbc917382023-06-07 08:40:31809 ** This "automatic-reset" change is not technically an incompatibility,
drh99a21822022-03-31 21:15:09810 ** since any application that receives an SQLITE_MISUSE is broken by
811 ** definition.
812 **
813 ** Nevertheless, some published applications that were originally written
larrybrbc917382023-06-07 08:40:31814 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
drh99a21822022-03-31 21:15:09815 ** returns, and those were broken by the automatic-reset change. As a
816 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
larrybrbc917382023-06-07 08:40:31817 ** legacy behavior of returning SQLITE_MISUSE for cases where the
drh99a21822022-03-31 21:15:09818 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
819 ** or SQLITE_BUSY error.
820 */
821#ifdef SQLITE_OMIT_AUTORESET
822 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
823 sqlite3_reset((sqlite3_stmt*)p);
824 }else{
825 return SQLITE_MISUSE_BKPT;
826 }
827#else
828 sqlite3_reset((sqlite3_stmt*)p);
829#endif
830 assert( p->eVdbeState==VDBE_READY_STATE );
831 goto restart_step;
832 }
danielk19771d850a72004-05-31 08:26:49833 }
drh99a21822022-03-31 21:15:09834
drh983b5ee2015-02-13 12:05:56835#ifdef SQLITE_DEBUG
836 p->rcApp = SQLITE_OK;
837#endif
drhb7f91642004-10-31 02:22:47838#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30839 if( p->explain ){
840 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47841 }else
842#endif /* SQLITE_OMIT_EXPLAIN */
843 {
drh4f7d3a52013-06-27 23:54:02844 db->nVdbeExec++;
drh4f26d6c2004-05-26 23:25:30845 rc = sqlite3VdbeExec(p);
drh4f7d3a52013-06-27 23:54:02846 db->nVdbeExec--;
drh4f26d6c2004-05-26 23:25:30847 }
848
drh7e621462022-03-30 17:56:27849 if( rc==SQLITE_ROW ){
850 assert( p->rc==SQLITE_OK );
851 assert( db->mallocFailed==0 );
852 db->errCode = SQLITE_ROW;
853 return SQLITE_ROW;
854 }else{
drh19e2d372005-08-29 23:00:03855#ifndef SQLITE_OMIT_TRACE
drhb7de8272018-12-04 13:51:26856 /* If the statement completed successfully, invoke the profile callback */
857 checkProfileCallback(db, p);
drh19e2d372005-08-29 23:00:03858#endif
drhcce70d52022-12-22 19:12:48859 p->pResultRow = 0;
drhb7de8272018-12-04 13:51:26860 if( rc==SQLITE_DONE && db->autoCommit ){
861 assert( p->rc==SQLITE_OK );
862 p->rc = doWalCallbacks(db);
863 if( p->rc!=SQLITE_OK ){
864 rc = SQLITE_ERROR;
865 }
drh5cbb4422020-06-29 13:12:42866 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
larrybrbc917382023-06-07 08:40:31867 /* If this statement was prepared using saved SQL and an
drh5cbb4422020-06-29 13:12:42868 ** error has occurred, then return the error code in p->rc to the
869 ** caller. Set the error code in the database handle to the same value.
larrybrbc917382023-06-07 08:40:31870 */
drh5cbb4422020-06-29 13:12:42871 rc = sqlite3VdbeTransferError(p);
dan8d22a172010-04-19 18:03:51872 }
873 }
874
drh80cc85b2008-07-23 21:07:25875 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08876 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
mistachkinfad30392016-02-13 23:43:46877 p->rc = SQLITE_NOMEM_BKPT;
drh5cbb4422020-06-29 13:12:42878 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
drhb900aaf2006-11-09 00:24:53879 }
danielk1977357864e2009-03-25 15:43:08880end_of_step:
drh5cbb4422020-06-29 13:12:42881 /* There are only a limited number of result codes allowed from the
882 ** statements prepared using the legacy sqlite3_prepare() interface */
883 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
larrybrbc917382023-06-07 08:40:31884 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
drhcbd8db32015-08-20 17:18:32885 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
danielk1977357864e2009-03-25 15:43:08886 );
danielk1977357864e2009-03-25 15:43:08887 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53888}
889
890/*
891** This is the top-level implementation of sqlite3_step(). Call
892** sqlite3Step() to do most of the work. If a schema error occurs,
893** call sqlite3Reprepare() and try again.
894*/
drhb900aaf2006-11-09 00:24:53895int sqlite3_step(sqlite3_stmt *pStmt){
drha6129fa2010-02-24 17:15:19896 int rc = SQLITE_OK; /* Result from sqlite3Step() */
drha6129fa2010-02-24 17:15:19897 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
898 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
899 sqlite3 *db; /* The database connection */
900
drh413c3d32010-02-23 20:11:56901 if( vdbeSafetyNotNull(v) ){
902 return SQLITE_MISUSE_BKPT;
danielk19778e556522007-11-13 10:30:24903 }
drh413c3d32010-02-23 20:11:56904 db = v->db;
905 sqlite3_mutex_enter(db->mutex);
drh413c3d32010-02-23 20:11:56906 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
drh2c7946a2014-08-19 20:27:40907 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
908 int savedPc = v->pc;
drh754ee282017-08-02 19:04:37909 rc = sqlite3Reprepare(v);
910 if( rc!=SQLITE_OK ){
larrybrbc917382023-06-07 08:40:31911 /* This case occurs after failing to recompile an sql statement.
912 ** The error message from the SQL compiler has already been loaded
913 ** into the database handle. This block copies the error message
drh754ee282017-08-02 19:04:37914 ** from the database handle into the statement and sets the statement
larrybrbc917382023-06-07 08:40:31915 ** program counter to 0 to ensure that when the statement is
drh754ee282017-08-02 19:04:37916 ** finalized or reset the parser error message is available via
917 ** sqlite3_errmsg() and sqlite3_errcode().
918 */
larrybrbc917382023-06-07 08:40:31919 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
drh754ee282017-08-02 19:04:37920 sqlite3DbFree(db, v->zErrMsg);
921 if( !db->mallocFailed ){
922 v->zErrMsg = sqlite3DbStrDup(db, zErr);
923 v->rc = rc = sqlite3ApiExit(db, rc);
924 } else {
925 v->zErrMsg = 0;
926 v->rc = rc = SQLITE_NOMEM_BKPT;
927 }
928 break;
929 }
drh413c3d32010-02-23 20:11:56930 sqlite3_reset(pStmt);
drha24832b2022-04-01 19:04:13931 if( savedPc>=0 ){
932 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
drh2591cfb2022-06-22 14:25:12933 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
934 ** already been done once on a prior invocation that failed due to
935 ** SQLITE_SCHEMA. tag-20220401a */
drha24832b2022-04-01 19:04:13936 v->minWriteFileFormat = 254;
937 }
dan08f5f4c2011-06-27 19:37:23938 assert( v->expired==0 );
drh413c3d32010-02-23 20:11:56939 }
drh413c3d32010-02-23 20:11:56940 sqlite3_mutex_leave(db->mutex);
danielk197776e8d1a2006-01-18 18:22:43941 return rc;
drh4f26d6c2004-05-26 23:25:30942}
943
drh95a7b3e2013-09-16 12:57:19944
drh4f26d6c2004-05-26 23:25:30945/*
drheb2e1762004-05-27 01:53:56946** Extract the user data from a sqlite3_context structure and return a
947** pointer to it.
948*/
949void *sqlite3_user_data(sqlite3_context *p){
stephan7a54b122023-10-14 14:53:18950#ifdef SQLITE_ENABLE_API_ARMOR
951 if( p==0 ) return 0;
stephan7a54b122023-10-14 14:53:18952#endif
drhf5e487d2023-10-19 20:46:22953 assert( p && p->pFunc );
drhd8f26352023-09-30 16:50:17954 return p->pFunc->pUserData;
drheb2e1762004-05-27 01:53:56955}
956
957/*
drhfa4a4b92008-03-19 21:45:51958** Extract the user data from a sqlite3_context structure and return a
959** pointer to it.
drh9f129f42010-08-31 15:27:32960**
961** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
962** returns a copy of the pointer to the database connection (the 1st
963** parameter) of the sqlite3_create_function() and
964** sqlite3_create_function16() routines that originally registered the
965** application defined function.
drhfa4a4b92008-03-19 21:45:51966*/
967sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
stephan7a54b122023-10-14 14:53:18968#ifdef SQLITE_ENABLE_API_ARMOR
969 if( p==0 ) return 0;
970#else
drha46a4a62015-09-08 21:12:53971 assert( p && p->pOut );
stephan7a54b122023-10-14 14:53:18972#endif
drh9bd038f2014-08-27 14:14:06973 return p->pOut->db;
drhfa4a4b92008-03-19 21:45:51974}
975
976/*
drh6f390be2018-01-11 17:04:26977** If this routine is invoked from within an xColumn method of a virtual
978** table, then it returns true if and only if the the call is during an
979** UPDATE operation and the value of the column will not be modified
980** by the UPDATE.
981**
982** If this routine is called from any context other than within the
983** xColumn method of a virtual table, then the return value is meaningless
984** and arbitrary.
985**
986** Virtual table implements might use this routine to optimize their
987** performance by substituting a NULL result, or some other light-weight
988** value, as a signal to the xUpdate routine that the column is unchanged.
989*/
990int sqlite3_vtab_nochange(sqlite3_context *p){
stephan7a54b122023-10-14 14:53:18991#ifdef SQLITE_ENABLE_API_ARMOR
992 if( p==0 ) return 0;
993#else
drh6f390be2018-01-11 17:04:26994 assert( p );
stephan7a54b122023-10-14 14:53:18995#endif
drhce2fbd12018-01-12 21:00:14996 return sqlite3_value_nochange(p->pOut);
drh6f390be2018-01-11 17:04:26997}
998
999/*
drhb604f142023-01-25 16:56:241000** The destructor function for a ValueList object. This needs to be
1001** a separate function, unknowable to the application, to ensure that
1002** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not
larrybrbc917382023-06-07 08:40:311003** preceded by activation of IN processing via sqlite3_vtab_int() do not
drhb604f142023-01-25 16:56:241004** try to access a fake ValueList object inserted by a hostile extension.
1005*/
1006void sqlite3VdbeValueListFree(void *pToDelete){
1007 sqlite3_free(pToDelete);
1008}
1009
1010/*
drh0fe7e7d2022-02-01 14:58:291011** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
1012** sqlite3_vtab_in_next() (if bNext!=0).
1013*/
drh30e314e2022-02-02 14:36:581014static int valueFromValueList(
1015 sqlite3_value *pVal, /* Pointer to the ValueList object */
1016 sqlite3_value **ppOut, /* Store the next value from the list here */
1017 int bNext /* 1 for _next(). 0 for _first() */
1018){
drhb30298d2022-02-01 21:59:431019 int rc;
drh30e314e2022-02-02 14:36:581020 ValueList *pRhs;
1021
drh0fe7e7d2022-02-01 14:58:291022 *ppOut = 0;
drh5a05a682023-09-05 15:03:231023 if( pVal==0 ) return SQLITE_MISUSE_BKPT;
drh3c12ebd2023-01-26 02:18:531024 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
drhb604f142023-01-25 16:56:241025 return SQLITE_ERROR;
1026 }else{
1027 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
1028 (MEM_Null|MEM_Term|MEM_Subtype) );
1029 assert( pVal->eSubtype=='p' );
1030 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
1031 pRhs = (ValueList*)pVal->z;
1032 }
drhb30298d2022-02-01 21:59:431033 if( bNext ){
drh30e314e2022-02-02 14:36:581034 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
drhb30298d2022-02-01 21:59:431035 }else{
1036 int dummy = 0;
drh30e314e2022-02-02 14:36:581037 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
drh3d7a69e2022-02-02 16:24:011038 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
1039 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
drhb30298d2022-02-01 21:59:431040 }
1041 if( rc==SQLITE_OK ){
drh30e314e2022-02-02 14:36:581042 u32 sz; /* Size of current row in bytes */
1043 Mem sMem; /* Raw content of current row */
1044 memset(&sMem, 0, sizeof(sMem));
1045 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
1046 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
1047 if( rc==SQLITE_OK ){
1048 u8 *zBuf = (u8*)sMem.z;
1049 u32 iSerial;
1050 sqlite3_value *pOut = pRhs->pOut;
1051 int iOff = 1 + getVarint32(&zBuf[1], iSerial);
1052 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);
drh38d1e442022-02-02 15:10:451053 pOut->enc = ENC(pOut->db);
drh30e314e2022-02-02 14:36:581054 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
1055 rc = SQLITE_NOMEM;
1056 }else{
1057 *ppOut = pOut;
1058 }
1059 }
1060 sqlite3VdbeMemRelease(&sMem);
drh0fe7e7d2022-02-01 14:58:291061 }
1062 return rc;
1063}
1064
1065/*
1066** Set the iterator value pVal to point to the first value in the set.
1067** Set (*ppOut) to point to this value before returning.
1068*/
1069int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
drh30e314e2022-02-02 14:36:581070 return valueFromValueList(pVal, ppOut, 0);
drh0fe7e7d2022-02-01 14:58:291071}
1072
1073/*
1074** Set the iterator value pVal to point to the next value in the set.
1075** Set (*ppOut) to point to this value before returning.
1076*/
1077int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
drh30e314e2022-02-02 14:36:581078 return valueFromValueList(pVal, ppOut, 1);
drh0fe7e7d2022-02-01 14:58:291079}
1080
1081/*
drha9e03b12015-03-12 06:46:521082** Return the current time for a statement. If the current time
1083** is requested more than once within the same run of a single prepared
1084** statement, the exact same time is returned for each invocation regardless
1085** of the amount of time that elapses between invocations. In other words,
1086** the time returned is always the time of the first call.
drh95a7b3e2013-09-16 12:57:191087*/
drh601e4d42023-02-08 20:29:481088sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
drh95a7b3e2013-09-16 12:57:191089 int rc;
drh175b8f02019-08-08 15:24:171090#ifndef SQLITE_ENABLE_STAT4
drh601e4d42023-02-08 20:29:481091 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
drha9e03b12015-03-12 06:46:521092 assert( p->pVdbe!=0 );
1093#else
drh3df79a92015-03-12 23:48:271094 sqlite3_int64 iTime = 0;
drh601e4d42023-02-08 20:29:481095 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
drha9e03b12015-03-12 06:46:521096#endif
drh3df79a92015-03-12 23:48:271097 if( *piTime==0 ){
drh601e4d42023-02-08 20:29:481098 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
drhd4af8822023-02-07 21:55:141099 if( rc ) *piTime = 0;
1100 }
1101 return *piTime;
1102}
1103
1104/*
drh9de4a172014-08-23 18:17:191105** Create a new aggregate context for p and return a pointer to
1106** its pMem->z element.
1107*/
1108static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
1109 Mem *pMem = p->pMem;
1110 assert( (pMem->flags & MEM_Agg)==0 );
1111 if( nByte<=0 ){
drh0725cab2014-09-17 14:52:461112 sqlite3VdbeMemSetNull(pMem);
drh9de4a172014-08-23 18:17:191113 pMem->z = 0;
1114 }else{
drh322f2852014-09-19 00:43:391115 sqlite3VdbeMemClearAndResize(pMem, nByte);
drh9de4a172014-08-23 18:17:191116 pMem->flags = MEM_Agg;
1117 pMem->u.pDef = p->pFunc;
1118 if( pMem->z ){
1119 memset(pMem->z, 0, nByte);
1120 }
1121 }
1122 return (void*)pMem->z;
1123}
1124
1125/*
drheb2e1762004-05-27 01:53:561126** Allocate or return the aggregate context for a user function. A new
1127** context is allocated on the first call. Subsequent calls return the
1128** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:561129*/
1130void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drh2d801512016-01-14 22:19:581131 assert( p && p->pFunc && p->pFunc->xFinalize );
drh9bd038f2014-08-27 14:14:061132 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
drhd68eee02009-12-11 03:44:181133 testcase( nByte<0 );
drh9de4a172014-08-23 18:17:191134 if( (p->pMem->flags & MEM_Agg)==0 ){
1135 return createAggContext(p, nByte);
1136 }else{
1137 return (void*)p->pMem->z;
drheb2e1762004-05-27 01:53:561138 }
drheb2e1762004-05-27 01:53:561139}
1140
1141/*
peter.d.reid60ec9142014-09-06 16:39:461142** Return the auxiliary data pointer, if any, for the iArg'th argument to
danielk1977682f68b2004-06-05 10:22:171143** the user-function defined by pCtx.
drhf7fa4e72017-05-11 15:20:181144**
1145** The left-most argument is 0.
1146**
1147** Undocumented behavior: If iArg is negative then access a cache of
1148** auxiliary data pointers that is available to all functions within a
1149** single prepared statement. The iArg values must match.
danielk1977682f68b2004-06-05 10:22:171150*/
1151void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
dan0c547792013-07-18 17:12:081152 AuxData *pAuxData;
drhb21c8cd2007-08-21 19:33:561153
stephan7a54b122023-10-14 14:53:181154#ifdef SQLITE_ENABLE_API_ARMOR
1155 if( pCtx==0 ) return 0;
1156#endif
drh9bd038f2014-08-27 14:14:061157 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh175b8f02019-08-08 15:24:171158#if SQLITE_ENABLE_STAT4
dan18bf8072015-03-11 20:06:401159 if( pCtx->pVdbe==0 ) return 0;
drha9e03b12015-03-12 06:46:521160#else
1161 assert( pCtx->pVdbe!=0 );
1162#endif
drhe6941392017-05-10 19:42:521163 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:181164 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
drhe6941392017-05-10 19:42:521165 return pAuxData->pAux;
1166 }
danielk1977682f68b2004-06-05 10:22:171167 }
drhe6941392017-05-10 19:42:521168 return 0;
danielk1977682f68b2004-06-05 10:22:171169}
1170
1171/*
peter.d.reid60ec9142014-09-06 16:39:461172** Set the auxiliary data pointer and delete function, for the iArg'th
danielk1977682f68b2004-06-05 10:22:171173** argument to the user-function defined by pCtx. Any previous value is
1174** deleted by calling the delete function specified when it was set.
drhf7fa4e72017-05-11 15:20:181175**
1176** The left-most argument is 0.
1177**
1178** Undocumented behavior: If iArg is negative then make the data available
1179** to all functions within the current prepared statement using iArg as an
1180** access code.
danielk1977682f68b2004-06-05 10:22:171181*/
1182void sqlite3_set_auxdata(
larrybrbc917382023-06-07 08:40:311183 sqlite3_context *pCtx,
1184 int iArg,
1185 void *pAux,
danielk1977682f68b2004-06-05 10:22:171186 void (*xDelete)(void*)
1187){
dan0c547792013-07-18 17:12:081188 AuxData *pAuxData;
stephan7a54b122023-10-14 14:53:181189 Vdbe *pVdbe;
danielk1977682f68b2004-06-05 10:22:171190
stephan7a54b122023-10-14 14:53:181191#ifdef SQLITE_ENABLE_API_ARMOR
1192 if( pCtx==0 ) return;
1193#endif
1194 pVdbe= pCtx->pVdbe;
drh9bd038f2014-08-27 14:14:061195 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
drh175b8f02019-08-08 15:24:171196#ifdef SQLITE_ENABLE_STAT4
dan18bf8072015-03-11 20:06:401197 if( pVdbe==0 ) goto failed;
drha9e03b12015-03-12 06:46:521198#else
1199 assert( pVdbe!=0 );
1200#endif
danielk1977682f68b2004-06-05 10:22:171201
drhe6941392017-05-10 19:42:521202 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
drhf7fa4e72017-05-11 15:20:181203 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
1204 break;
1205 }
dan0c547792013-07-18 17:12:081206 }
1207 if( pAuxData==0 ){
1208 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
1209 if( !pAuxData ) goto failed;
drhe6941392017-05-10 19:42:521210 pAuxData->iAuxOp = pCtx->iOp;
1211 pAuxData->iAuxArg = iArg;
1212 pAuxData->pNextAux = pVdbe->pAuxData;
dan0c547792013-07-18 17:12:081213 pVdbe->pAuxData = pAuxData;
drhf09ac0b2018-01-23 03:44:061214 if( pCtx->isError==0 ) pCtx->isError = -1;
drhe6941392017-05-10 19:42:521215 }else if( pAuxData->xDeleteAux ){
1216 pAuxData->xDeleteAux(pAuxData->pAux);
danielk1977682f68b2004-06-05 10:22:171217 }
dan0c547792013-07-18 17:12:081218
danielk1977682f68b2004-06-05 10:22:171219 pAuxData->pAux = pAux;
drhe6941392017-05-10 19:42:521220 pAuxData->xDeleteAux = xDelete;
danielk1977e0fc5262007-07-26 06:50:051221 return;
1222
1223failed:
1224 if( xDelete ){
1225 xDelete(pAux);
1226 }
danielk1977682f68b2004-06-05 10:22:171227}
1228
shaneeec556d2008-10-12 00:27:531229#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:171230/*
larrybrbc917382023-06-07 08:40:311231** Return the number of times the Step function of an aggregate has been
drheb2e1762004-05-27 01:53:561232** called.
1233**
drhcf85a512006-02-09 18:35:291234** This function is deprecated. Do not use it for new code. It is
1235** provide only to avoid breaking legacy code. New aggregate function
1236** implementations should keep their own counts within their aggregate
1237** context.
drheb2e1762004-05-27 01:53:561238*/
1239int sqlite3_aggregate_count(sqlite3_context *p){
drh2d801512016-01-14 22:19:581240 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
drhabfcea22005-09-06 20:36:481241 return p->pMem->n;
drheb2e1762004-05-27 01:53:561242}
shaneeec556d2008-10-12 00:27:531243#endif
drheb2e1762004-05-27 01:53:561244
1245/*
drh4f26d6c2004-05-26 23:25:301246** Return the number of columns in the result set for the statement pStmt.
1247*/
1248int sqlite3_column_count(sqlite3_stmt *pStmt){
1249 Vdbe *pVm = (Vdbe *)pStmt;
drhd1db37a2023-07-18 17:29:051250 if( pVm==0 ) return 0;
1251 return pVm->nResColumn;
drh4f26d6c2004-05-26 23:25:301252}
1253
1254/*
1255** Return the number of values available from the current row of the
1256** currently executing statement pStmt.
1257*/
1258int sqlite3_data_count(sqlite3_stmt *pStmt){
1259 Vdbe *pVm = (Vdbe *)pStmt;
drhedc27132022-12-22 18:44:391260 if( pVm==0 || pVm->pResultRow==0 ) return 0;
drh4f26d6c2004-05-26 23:25:301261 return pVm->nResColumn;
1262}
1263
dan46c47d42011-03-01 18:42:071264/*
1265** Return a pointer to static memory containing an SQL NULL value.
1266*/
1267static const Mem *columnNullValue(void){
1268 /* Even though the Mem structure contains an element
drhb1a1c292014-03-05 12:47:551269 ** of type i64, on certain architectures (x86) with certain compiler
dan46c47d42011-03-01 18:42:071270 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
1271 ** instead of an 8-byte one. This all works fine, except that when
1272 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
1273 ** that a Mem structure is located on an 8-byte boundary. To prevent
drhb1a1c292014-03-05 12:47:551274 ** these assert()s from failing, when building with SQLITE_DEBUG defined
1275 ** using gcc, we force nullMem to be 8-byte aligned using the magical
dan46c47d42011-03-01 18:42:071276 ** __attribute__((aligned(8))) macro. */
larrybrbc917382023-06-07 08:40:311277 static const Mem nullMem
dan46c47d42011-03-01 18:42:071278#if defined(SQLITE_DEBUG) && defined(__GNUC__)
larrybrbc917382023-06-07 08:40:311279 __attribute__((aligned(8)))
dan46c47d42011-03-01 18:42:071280#endif
drh035e5632014-09-16 14:16:311281 = {
drh3329a632014-09-18 01:21:431282 /* .u = */ {0},
drhc9373e82022-02-28 03:25:131283 /* .z = */ (char*)0,
1284 /* .n = */ (int)0,
drh8faee872015-09-19 18:08:131285 /* .flags = */ (u16)MEM_Null,
1286 /* .enc = */ (u8)0,
1287 /* .eSubtype = */ (u8)0,
drhc9373e82022-02-28 03:25:131288 /* .db = */ (sqlite3*)0,
drh8faee872015-09-19 18:08:131289 /* .szMalloc = */ (int)0,
1290 /* .uTemp = */ (u32)0,
drhc9373e82022-02-28 03:25:131291 /* .zMalloc = */ (char*)0,
drh8faee872015-09-19 18:08:131292 /* .xDel = */ (void(*)(void*))0,
drhb1a1c292014-03-05 12:47:551293#ifdef SQLITE_DEBUG
drh8faee872015-09-19 18:08:131294 /* .pScopyFrom = */ (Mem*)0,
drh58773a52018-06-12 13:52:231295 /* .mScopyFlags= */ 0,
drh8e7a1682025-01-21 16:30:551296 /* .bScopy = */ 0,
drh58773a52018-06-12 13:52:231297#endif
drh035e5632014-09-16 14:16:311298 };
dan46c47d42011-03-01 18:42:071299 return &nullMem;
1300}
drh4f26d6c2004-05-26 23:25:301301
1302/*
1303** Check to see if column iCol of the given statement is valid. If
1304** it is, return a pointer to the Mem for the value of that column.
1305** If iCol is not valid, return a pointer to a Mem which has a value
1306** of NULL.
1307*/
1308static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:391309 Vdbe *pVm;
drh32bc3f62007-08-21 20:25:391310 Mem *pOut;
1311
1312 pVm = (Vdbe *)pStmt;
drh28f17012016-09-22 21:37:181313 if( pVm==0 ) return (Mem*)columnNullValue();
1314 assert( pVm->db );
1315 sqlite3_mutex_enter(pVm->db->mutex);
drhedc27132022-12-22 18:44:391316 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
1317 pOut = &pVm->pResultRow[i];
drh32bc3f62007-08-21 20:25:391318 }else{
drh28f17012016-09-22 21:37:181319 sqlite3Error(pVm->db, SQLITE_RANGE);
dan46c47d42011-03-01 18:42:071320 pOut = (Mem*)columnNullValue();
drh4f26d6c2004-05-26 23:25:301321 }
drh32bc3f62007-08-21 20:25:391322 return pOut;
drh4f26d6c2004-05-26 23:25:301323}
1324
danielk19772e588c72005-12-09 14:25:081325/*
larrybrbc917382023-06-07 08:40:311326** This function is called after invoking an sqlite3_value_XXX function on a
danielk19772e588c72005-12-09 14:25:081327** column value (i.e. a value returned by evaluating an SQL expression in the
larrybrbc917382023-06-07 08:40:311328** select list of a SELECT statement) that may cause a malloc() failure. If
danielk19772e588c72005-12-09 14:25:081329** malloc() has failed, the threads mallocFailed flag is cleared and the result
1330** code of statement pStmt set to SQLITE_NOMEM.
1331**
drh32bc3f62007-08-21 20:25:391332** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:081333**
1334** sqlite3_column_int()
1335** sqlite3_column_int64()
1336** sqlite3_column_text()
1337** sqlite3_column_text16()
drh6e53f672024-12-10 12:32:341338** sqlite3_column_double()
danielk19772e588c72005-12-09 14:25:081339** sqlite3_column_bytes()
1340** sqlite3_column_bytes16()
larrybrbc917382023-06-07 08:40:311341** sqlite3_column_blob()
danielk19772e588c72005-12-09 14:25:081342*/
1343static void columnMallocFailure(sqlite3_stmt *pStmt)
1344{
1345 /* If malloc() failed during an encoding conversion within an
1346 ** sqlite3_column_XXX API, then set the return code of the statement to
1347 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
1348 ** and _finalize() will return NOMEM.
1349 */
danielk197754f01982006-01-18 15:25:171350 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:391351 if( p ){
drh28f17012016-09-22 21:37:181352 assert( p->db!=0 );
1353 assert( sqlite3_mutex_held(p->db->mutex) );
drh32bc3f62007-08-21 20:25:391354 p->rc = sqlite3ApiExit(p->db, p->rc);
1355 sqlite3_mutex_leave(p->db->mutex);
1356 }
danielk19772e588c72005-12-09 14:25:081357}
1358
drh4f26d6c2004-05-26 23:25:301359/**************************** sqlite3_column_ *******************************
1360** The following routines are used to access elements of the current row
1361** in the result set.
1362*/
danielk1977c572ef72004-05-27 09:28:411363const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081364 const void *val;
danielk19772e588c72005-12-09 14:25:081365 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:471366 /* Even though there is no encoding conversion, value_blob() might
larrybrbc917382023-06-07 08:40:311367 ** need to call malloc() to expand the result of a zeroblob()
1368 ** expression.
danielk1977c9cf9012007-05-30 10:36:471369 */
1370 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:081371 return val;
danielk1977c572ef72004-05-27 09:28:411372}
drh4f26d6c2004-05-26 23:25:301373int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081374 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
1375 columnMallocFailure(pStmt);
1376 return val;
drh4f26d6c2004-05-26 23:25:301377}
1378int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081379 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
1380 columnMallocFailure(pStmt);
1381 return val;
drh4f26d6c2004-05-26 23:25:301382}
1383double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081384 double val = sqlite3_value_double( columnMem(pStmt,i) );
1385 columnMallocFailure(pStmt);
1386 return val;
drh4f26d6c2004-05-26 23:25:301387}
1388int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081389 int val = sqlite3_value_int( columnMem(pStmt,i) );
1390 columnMallocFailure(pStmt);
1391 return val;
drh4f26d6c2004-05-26 23:25:301392}
drhefad9992004-06-22 12:13:551393sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081394 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
1395 columnMallocFailure(pStmt);
1396 return val;
drh4f26d6c2004-05-26 23:25:301397}
1398const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081399 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
1400 columnMallocFailure(pStmt);
1401 return val;
drh4f26d6c2004-05-26 23:25:301402}
drhd1e47332005-06-26 17:55:331403sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:491404 Mem *pOut = columnMem(pStmt, i);
1405 if( pOut->flags&MEM_Static ){
1406 pOut->flags &= ~MEM_Static;
1407 pOut->flags |= MEM_Ephem;
1408 }
drh32bc3f62007-08-21 20:25:391409 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:491410 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:331411}
drh6c626082004-11-14 21:56:291412#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:301413const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:081414 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
1415 columnMallocFailure(pStmt);
1416 return val;
drh4f26d6c2004-05-26 23:25:301417}
drh6c626082004-11-14 21:56:291418#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:301419int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:391420 int iType = sqlite3_value_type( columnMem(pStmt,i) );
1421 columnMallocFailure(pStmt);
1422 return iType;
drh4f26d6c2004-05-26 23:25:301423}
1424
drh5e2517e2004-09-24 23:59:121425/*
drhd1db37a2023-07-18 17:29:051426** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN.
1427*/
1428static const char * const azExplainColNames8[] = {
1429 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */
1430 "id", "parent", "notused", "detail" /* EQP */
1431};
1432static const u16 azExplainColNames16data[] = {
1433 /* 0 */ 'a', 'd', 'd', 'r', 0,
1434 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0,
1435 /* 12 */ 'p', '1', 0,
1436 /* 15 */ 'p', '2', 0,
1437 /* 18 */ 'p', '3', 0,
1438 /* 21 */ 'p', '4', 0,
1439 /* 24 */ 'p', '5', 0,
1440 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0,
1441 /* 35 */ 'i', 'd', 0,
1442 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0,
1443 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0,
1444 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0
1445};
1446static const u8 iExplainColNames16[] = {
1447 0, 5, 12, 15, 18, 21, 24, 27,
1448 35, 38, 45, 53
1449};
1450
1451/*
drh5e2517e2004-09-24 23:59:121452** Convert the N-th element of pStmt->pColName[] into a string using
1453** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:011454**
1455** There are up to 5 names for each column. useType determines which
1456** name is returned. Here are the names:
1457**
1458** 0 The column name as it should be displayed for output
1459** 1 The datatype name for the column
1460** 2 The name of the database that the column derives from
1461** 3 The name of the table that the column derives from
1462** 4 The name of the table column that the result column derives from
1463**
1464** If the result is not a simple column reference (if it is an expression
1465** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:121466*/
1467static const void *columnName(
drh8cf92892019-02-20 18:13:571468 sqlite3_stmt *pStmt, /* The statement */
1469 int N, /* Which column to get the name for */
1470 int useUtf16, /* True to return the name as UTF16 */
1471 int useType /* What type of name */
drh5e2517e2004-09-24 23:59:121472){
drh9ca95732014-10-24 00:35:581473 const void *ret;
1474 Vdbe *p;
drh32bc3f62007-08-21 20:25:391475 int n;
drh9ca95732014-10-24 00:35:581476 sqlite3 *db;
1477#ifdef SQLITE_ENABLE_API_ARMOR
1478 if( pStmt==0 ){
1479 (void)SQLITE_MISUSE_BKPT;
1480 return 0;
1481 }
1482#endif
drhd1db37a2023-07-18 17:29:051483 if( N<0 ) return 0;
drh9ca95732014-10-24 00:35:581484 ret = 0;
1485 p = (Vdbe *)pStmt;
1486 db = p->db;
drh9b4ea4a2009-04-10 20:32:001487 assert( db!=0 );
drhd1db37a2023-07-18 17:29:051488 sqlite3_mutex_enter(db->mutex);
1489
1490 if( p->explain ){
1491 if( useType>0 ) goto columnName_end;
1492 n = p->explain==1 ? 8 : 4;
1493 if( N>=n ) goto columnName_end;
1494 if( useUtf16 ){
1495 int i = iExplainColNames16[N + 8*p->explain - 8];
1496 ret = (void*)&azExplainColNames16data[i];
1497 }else{
1498 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
1499 }
1500 goto columnName_end;
1501 }
1502 n = p->nResColumn;
1503 if( N<n ){
drh93b4c3b2023-04-27 23:59:511504 u8 prior_mallocFailed = db->mallocFailed;
drh9b4ea4a2009-04-10 20:32:001505 N += useType*n;
dan4474e862019-03-04 07:15:571506#ifndef SQLITE_OMIT_UTF16
drh8cf92892019-02-20 18:13:571507 if( useUtf16 ){
1508 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
dan4474e862019-03-04 07:15:571509 }else
1510#endif
1511 {
drh8cf92892019-02-20 18:13:571512 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
1513 }
1514 /* A malloc may have failed inside of the _text() call. If this
drh9b4ea4a2009-04-10 20:32:001515 ** is the case, clear the mallocFailed flag and return NULL.
1516 */
drh93b4c3b2023-04-27 23:59:511517 assert( db->mallocFailed==0 || db->mallocFailed==1 );
1518 if( db->mallocFailed > prior_mallocFailed ){
drh4a642b62016-02-05 01:55:271519 sqlite3OomClear(db);
drh9b4ea4a2009-04-10 20:32:001520 ret = 0;
drh32bc3f62007-08-21 20:25:391521 }
drh5e2517e2004-09-24 23:59:121522 }
drhd1db37a2023-07-18 17:29:051523columnName_end:
1524 sqlite3_mutex_leave(db->mutex);
danielk197700fd9572005-12-07 06:27:431525 return ret;
drh5e2517e2004-09-24 23:59:121526}
1527
drh4f26d6c2004-05-26 23:25:301528/*
1529** Return the name of the Nth column of the result set returned by SQL
1530** statement pStmt.
1531*/
1532const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571533 return columnName(pStmt, N, 0, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:301534}
drhe590fbd2005-05-20 19:36:011535#ifndef SQLITE_OMIT_UTF16
1536const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571537 return columnName(pStmt, N, 1, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:011538}
1539#endif
drh4f26d6c2004-05-26 23:25:301540
1541/*
drh3f913572008-03-22 01:07:171542** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1543** not define OMIT_DECLTYPE.
1544*/
1545#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
1546# error "Must not define both SQLITE_OMIT_DECLTYPE \
1547 and SQLITE_ENABLE_COLUMN_METADATA"
1548#endif
1549
1550#ifndef SQLITE_OMIT_DECLTYPE
1551/*
drh6c626082004-11-14 21:56:291552** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:011553** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:291554*/
1555const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571556 return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:291557}
drh6c626082004-11-14 21:56:291558#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:021559const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571560 return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:301561}
drh6c626082004-11-14 21:56:291562#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:171563#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:301564
danielk19774b1ae992006-02-10 03:06:101565#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:011566/*
1567** Return the name of the database from which a result column derives.
1568** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:461569** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:011570*/
1571const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571572 return columnName(pStmt, N, 0, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:011573}
1574#ifndef SQLITE_OMIT_UTF16
1575const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571576 return columnName(pStmt, N, 1, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:011577}
1578#endif /* SQLITE_OMIT_UTF16 */
1579
1580/*
1581** Return the name of the table from which a result column derives.
1582** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:461583** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:011584*/
1585const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571586 return columnName(pStmt, N, 0, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:011587}
1588#ifndef SQLITE_OMIT_UTF16
1589const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571590 return columnName(pStmt, N, 1, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:011591}
1592#endif /* SQLITE_OMIT_UTF16 */
1593
1594/*
1595** Return the name of the table column from which a result column derives.
1596** NULL is returned if the result column is an expression or constant or
peter.d.reid60ec9142014-09-06 16:39:461597** anything else which is not an unambiguous reference to a database column.
drhe590fbd2005-05-20 19:36:011598*/
1599const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571600 return columnName(pStmt, N, 0, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:011601}
1602#ifndef SQLITE_OMIT_UTF16
1603const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
drh8cf92892019-02-20 18:13:571604 return columnName(pStmt, N, 1, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:011605}
1606#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:101607#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:011608
1609
drh4f26d6c2004-05-26 23:25:301610/******************************* sqlite3_bind_ ***************************
larrybrbc917382023-06-07 08:40:311611**
drh4f26d6c2004-05-26 23:25:301612** Routines used to attach values to wildcards in a compiled SQL statement.
1613*/
1614/*
larrybrbc917382023-06-07 08:40:311615** Unbind the value bound to variable i in virtual machine p. This is the
drh4f26d6c2004-05-26 23:25:301616** the same as binding a NULL value to the column. If the "i" parameter is
larrybrbc917382023-06-07 08:40:311617** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
drh4f26d6c2004-05-26 23:25:301618**
drh488e7b62008-10-06 12:46:431619** A successful evaluation of this routine acquires the mutex on p.
1620** the mutex is released if any kind of error occurs.
1621**
drh4f26d6c2004-05-26 23:25:301622** The error code stored in database p->db is overwritten with the return
1623** value in any case.
drh87dce452024-09-17 21:42:041624**
1625** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK,
1626** that means all of the the following will be true:
1627**
1628** p!=0
1629** p->pVar!=0
1630** i>0
1631** i<=p->nVar
1632**
1633** An assert() is normally added after vdbeUnbind() to help static analyzers
1634** realize this.
drh4f26d6c2004-05-26 23:25:301635*/
drh403f0022022-08-03 19:37:251636static int vdbeUnbind(Vdbe *p, unsigned int i){
drh4f26d6c2004-05-26 23:25:301637 Mem *pVar;
drh413c3d32010-02-23 20:11:561638 if( vdbeSafetyNotNull(p) ){
1639 return SQLITE_MISUSE_BKPT;
1640 }
drh488e7b62008-10-06 12:46:431641 sqlite3_mutex_enter(p->db->mutex);
drh99a21822022-03-31 21:15:091642 if( p->eVdbeState!=VDBE_READY_STATE ){
drh5a05a682023-09-05 15:03:231643 sqlite3Error(p->db, SQLITE_MISUSE_BKPT);
drh488e7b62008-10-06 12:46:431644 sqlite3_mutex_leave(p->db->mutex);
larrybrbc917382023-06-07 08:40:311645 sqlite3_log(SQLITE_MISUSE,
drh413c3d32010-02-23 20:11:561646 "bind on a busy prepared statement: [%s]", p->zSql);
1647 return SQLITE_MISUSE_BKPT;
drh4f26d6c2004-05-26 23:25:301648 }
mistachkine2848932022-08-05 05:30:071649 if( i>=(unsigned int)p->nVar ){
drh13f40da2014-08-22 18:00:111650 sqlite3Error(p->db, SQLITE_RANGE);
drh488e7b62008-10-06 12:46:431651 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:301652 return SQLITE_RANGE;
1653 }
drh290c1942004-08-21 17:54:451654 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:121655 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:301656 pVar->flags = MEM_Null;
drh368bfe82018-12-11 12:20:411657 p->db->errCode = SQLITE_OK;
dan937d0de2009-10-15 18:35:381658
larrybrbc917382023-06-07 08:40:311659 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
dan1d2ce4f2009-10-19 18:11:091660 ** binding a new value to this variable invalidates the current query plan.
drha7044002010-09-14 18:22:591661 **
drhbbf6d432020-05-15 15:03:511662 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
drha7044002010-09-14 18:22:591663 ** parameter in the WHERE clause might influence the choice of query plan
1664 ** for a statement, then the statement will be automatically recompiled,
1665 ** as if there had been a schema change, on the first sqlite3_step() call
1666 ** following any change to the bindings of that parameter.
dan1d2ce4f2009-10-19 18:11:091667 */
drh2c2f3922017-06-01 00:54:351668 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
drh29967962017-03-03 21:51:401669 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
dan937d0de2009-10-15 18:35:381670 p->expired = 1;
1671 }
drh4f26d6c2004-05-26 23:25:301672 return SQLITE_OK;
1673}
1674
1675/*
drh5e2517e2004-09-24 23:59:121676** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:301677*/
drh5e2517e2004-09-24 23:59:121678static int bindText(
drh605264d2007-08-21 15:13:191679 sqlite3_stmt *pStmt, /* The statement to bind against */
1680 int i, /* Index of the parameter to bind */
1681 const void *zData, /* Pointer to the data to be bound */
drhd6228552021-06-09 14:45:021682 i64 nData, /* Number of bytes of data to be bound */
drh605264d2007-08-21 15:13:191683 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:451684 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:301685){
1686 Vdbe *p = (Vdbe *)pStmt;
1687 Mem *pVar;
1688 int rc;
1689
drh403f0022022-08-03 19:37:251690 rc = vdbeUnbind(p, (u32)(i-1));
drh488e7b62008-10-06 12:46:431691 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041692 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
drh488e7b62008-10-06 12:46:431693 if( zData!=0 ){
1694 pVar = &p->aVar[i-1];
1695 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1696 if( rc==SQLITE_OK && encoding!=0 ){
1697 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1698 }
drhe70d01f2017-05-29 22:44:181699 if( rc ){
1700 sqlite3Error(p->db, rc);
1701 rc = sqlite3ApiExit(p->db, rc);
1702 }
drh27641702007-08-22 02:56:421703 }
drh488e7b62008-10-06 12:46:431704 sqlite3_mutex_leave(p->db->mutex);
drh94bb2ba2010-10-14 01:16:321705 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
drh6fec9ee2010-10-12 02:13:321706 xDel((void*)zData);
drh4f26d6c2004-05-26 23:25:301707 }
drh605264d2007-08-21 15:13:191708 return rc;
drh4f26d6c2004-05-26 23:25:301709}
drh5e2517e2004-09-24 23:59:121710
1711
1712/*
1713** Bind a blob value to an SQL statement variable.
1714*/
1715int sqlite3_bind_blob(
larrybrbc917382023-06-07 08:40:311716 sqlite3_stmt *pStmt,
1717 int i,
1718 const void *zData,
1719 int nData,
drh5e2517e2004-09-24 23:59:121720 void (*xDel)(void*)
1721){
drh5b081d82016-02-18 01:29:121722#ifdef SQLITE_ENABLE_API_ARMOR
1723 if( nData<0 ) return SQLITE_MISUSE_BKPT;
1724#endif
drh5e2517e2004-09-24 23:59:121725 return bindText(pStmt, i, zData, nData, xDel, 0);
1726}
drhda4ca9d2014-09-09 17:27:351727int sqlite3_bind_blob64(
larrybrbc917382023-06-07 08:40:311728 sqlite3_stmt *pStmt,
1729 int i,
1730 const void *zData,
1731 sqlite3_uint64 nData,
drhda4ca9d2014-09-09 17:27:351732 void (*xDel)(void*)
1733){
drhfc59a952014-09-11 23:34:551734 assert( xDel!=SQLITE_DYNAMIC );
drhd6228552021-06-09 14:45:021735 return bindText(pStmt, i, zData, nData, xDel, 0);
drhda4ca9d2014-09-09 17:27:351736}
drh4f26d6c2004-05-26 23:25:301737int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1738 int rc;
1739 Vdbe *p = (Vdbe *)pStmt;
drh403f0022022-08-03 19:37:251740 rc = vdbeUnbind(p, (u32)(i-1));
drh4f26d6c2004-05-26 23:25:301741 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041742 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
drh290c1942004-08-21 17:54:451743 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:431744 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:301745 }
danielk1977f4618892004-06-28 13:09:111746 return rc;
drh4f26d6c2004-05-26 23:25:301747}
1748int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:551749 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:301750}
drhefad9992004-06-22 12:13:551751int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:301752 int rc;
1753 Vdbe *p = (Vdbe *)pStmt;
drh403f0022022-08-03 19:37:251754 rc = vdbeUnbind(p, (u32)(i-1));
drh4f26d6c2004-05-26 23:25:301755 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041756 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
drh290c1942004-08-21 17:54:451757 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:431758 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:301759 }
1760 return rc;
1761}
drh605264d2007-08-21 15:13:191762int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1763 int rc;
1764 Vdbe *p = (Vdbe*)pStmt;
drh403f0022022-08-03 19:37:251765 rc = vdbeUnbind(p, (u32)(i-1));
drh488e7b62008-10-06 12:46:431766 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041767 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
drh488e7b62008-10-06 12:46:431768 sqlite3_mutex_leave(p->db->mutex);
1769 }
drh605264d2007-08-21 15:13:191770 return rc;
drh4f26d6c2004-05-26 23:25:301771}
drh22930062017-07-27 03:48:021772int sqlite3_bind_pointer(
1773 sqlite3_stmt *pStmt,
1774 int i,
1775 void *pPtr,
1776 const char *zPTtype,
1777 void (*xDestructor)(void*)
1778){
drh3a96a5d2017-06-30 23:09:031779 int rc;
1780 Vdbe *p = (Vdbe*)pStmt;
drh403f0022022-08-03 19:37:251781 rc = vdbeUnbind(p, (u32)(i-1));
drh3a96a5d2017-06-30 23:09:031782 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041783 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
drh22930062017-07-27 03:48:021784 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
drh3a96a5d2017-06-30 23:09:031785 sqlite3_mutex_leave(p->db->mutex);
drh34fcf362017-07-27 16:42:361786 }else if( xDestructor ){
1787 xDestructor(pPtr);
drh3a96a5d2017-06-30 23:09:031788 }
1789 return rc;
1790}
larrybrbc917382023-06-07 08:40:311791int sqlite3_bind_text(
1792 sqlite3_stmt *pStmt,
1793 int i,
1794 const char *zData,
1795 int nData,
danielk1977d8123362004-06-12 09:25:121796 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:301797){
drh5e2517e2004-09-24 23:59:121798 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:301799}
larrybrbc917382023-06-07 08:40:311800int sqlite3_bind_text64(
1801 sqlite3_stmt *pStmt,
1802 int i,
1803 const char *zData,
1804 sqlite3_uint64 nData,
drhda4ca9d2014-09-09 17:27:351805 void (*xDel)(void*),
1806 unsigned char enc
1807){
drhfc59a952014-09-11 23:34:551808 assert( xDel!=SQLITE_DYNAMIC );
drhd2603ad2022-12-02 17:52:521809 if( enc!=SQLITE_UTF8 ){
1810 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
drh29c7c8b2025-04-06 10:22:261811 nData &= ~(u64)1;
drhd2603ad2022-12-02 17:52:521812 }
drhd6228552021-06-09 14:45:021813 return bindText(pStmt, i, zData, nData, xDel, enc);
drhda4ca9d2014-09-09 17:27:351814}
drh6c626082004-11-14 21:56:291815#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:301816int sqlite3_bind_text16(
larrybrbc917382023-06-07 08:40:311817 sqlite3_stmt *pStmt,
1818 int i,
1819 const void *zData,
1820 int n,
danielk1977d8123362004-06-12 09:25:121821 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:301822){
drhd2603ad2022-12-02 17:52:521823 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:301824}
drh6c626082004-11-14 21:56:291825#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:351826int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1827 int rc;
drh1b27b8c2014-02-10 03:21:571828 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
drh29def562009-04-14 12:43:331829 case SQLITE_INTEGER: {
1830 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1831 break;
1832 }
1833 case SQLITE_FLOAT: {
dan63a47332022-02-11 16:10:181834 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
larrybrbc917382023-06-07 08:40:311835 rc = sqlite3_bind_double(pStmt, i,
dan63a47332022-02-11 16:10:181836 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
1837 );
drh29def562009-04-14 12:43:331838 break;
1839 }
1840 case SQLITE_BLOB: {
1841 if( pValue->flags & MEM_Zero ){
1842 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1843 }else{
1844 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1845 }
1846 break;
1847 }
1848 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:201849 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1850 pValue->enc);
1851 break;
1852 }
1853 default: {
1854 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:331855 break;
1856 }
danielk197726e41442006-06-14 15:16:351857 }
1858 return rc;
1859}
drhb026e052007-05-02 01:34:311860int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1861 int rc;
1862 Vdbe *p = (Vdbe *)pStmt;
drh403f0022022-08-03 19:37:251863 rc = vdbeUnbind(p, (u32)(i-1));
drhb026e052007-05-02 01:34:311864 if( rc==SQLITE_OK ){
drh87dce452024-09-17 21:42:041865 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
dana32536b2021-11-08 19:35:261866#ifndef SQLITE_OMIT_INCRBLOB
drhb026e052007-05-02 01:34:311867 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
dana32536b2021-11-08 19:35:261868#else
1869 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1870#endif
drh488e7b62008-10-06 12:46:431871 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:311872 }
1873 return rc;
1874}
dan80c03022015-07-24 17:36:341875int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
1876 int rc;
1877 Vdbe *p = (Vdbe *)pStmt;
stephanc76e4502023-10-14 16:29:361878#ifdef SQLITE_ENABLE_API_ARMOR
1879 if( p==0 ) return SQLITE_MISUSE_BKPT;
1880#endif
dan80c03022015-07-24 17:36:341881 sqlite3_mutex_enter(p->db->mutex);
1882 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
1883 rc = SQLITE_TOOBIG;
1884 }else{
1885 assert( (n & 0x7FFFFFFF)==n );
1886 rc = sqlite3_bind_zeroblob(pStmt, i, n);
1887 }
1888 rc = sqlite3ApiExit(p->db, rc);
1889 sqlite3_mutex_leave(p->db->mutex);
1890 return rc;
1891}
drh75f6a032004-07-15 14:15:001892
1893/*
1894** Return the number of wildcards that can be potentially bound to.
larrybrbc917382023-06-07 08:40:311895** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:001896*/
1897int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:201898 Vdbe *p = (Vdbe*)pStmt;
1899 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:001900}
drh895d7472004-08-20 16:02:391901
1902/*
drhfa6bc002004-09-07 16:19:521903** Return the name of a wildcard parameter. Return NULL if the index
1904** is out of range or if the wildcard is unnamed.
1905**
1906** The result is always UTF-8.
1907*/
1908const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1909 Vdbe *p = (Vdbe*)pStmt;
drh9bf755c2016-12-23 03:59:311910 if( p==0 ) return 0;
1911 return sqlite3VListNumToName(p->pVList, i);
drh895d7472004-08-20 16:02:391912}
drhfa6bc002004-09-07 16:19:521913
1914/*
1915** Given a wildcard parameter name, return the index of the variable
1916** with that name. If there is no variable with the given name,
1917** return 0.
1918*/
drh5f18a222009-11-26 14:01:531919int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
drh9bf755c2016-12-23 03:59:311920 if( p==0 || zName==0 ) return 0;
1921 return sqlite3VListNameToNum(p->pVList, zName, nName);
drhfa6bc002004-09-07 16:19:521922}
drh5f18a222009-11-26 14:01:531923int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1924 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1925}
drhf8db1bc2005-04-22 02:38:371926
drh51942bc2005-06-12 22:01:421927/*
drhf8db1bc2005-04-22 02:38:371928** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:371929*/
shane145834a2008-09-04 12:03:421930int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:371931 Vdbe *pFrom = (Vdbe*)pFromStmt;
1932 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:441933 int i;
1934 assert( pTo->db==pFrom->db );
1935 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:421936 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:441937 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:531938 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:371939 }
drh27641702007-08-22 02:56:421940 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:441941 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:371942}
drh51942bc2005-06-12 22:01:421943
shaneeec556d2008-10-12 00:27:531944#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:421945/*
shane145834a2008-09-04 12:03:421946** Deprecated external interface. Internal/core SQLite code
1947** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:441948**
peter.d.reid60ec9142014-09-06 16:39:461949** It is misuse to call this routine with statements from different
drh0f5ea0b2009-04-09 14:02:441950** database connections. But as this is a deprecated interface, we
1951** will not bother to check for that condition.
1952**
1953** If the two statements contain a different number of bindings, then
1954** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1955** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:421956*/
1957int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:441958 Vdbe *pFrom = (Vdbe*)pFromStmt;
1959 Vdbe *pTo = (Vdbe*)pToStmt;
1960 if( pFrom->nVar!=pTo->nVar ){
1961 return SQLITE_ERROR;
1962 }
drh2c2f3922017-06-01 00:54:351963 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
danbda4cb82017-02-23 16:30:161964 if( pTo->expmask ){
danc94b8592009-10-20 07:01:241965 pTo->expired = 1;
1966 }
drh2c2f3922017-06-01 00:54:351967 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
danbda4cb82017-02-23 16:30:161968 if( pFrom->expmask ){
danc94b8592009-10-20 07:01:241969 pFrom->expired = 1;
1970 }
shane145834a2008-09-04 12:03:421971 return sqlite3TransferBindings(pFromStmt, pToStmt);
1972}
shaneeec556d2008-10-12 00:27:531973#endif
shane145834a2008-09-04 12:03:421974
1975/*
drh51942bc2005-06-12 22:01:421976** Return the sqlite3* database handle to which the prepared statement given
1977** in the argument belongs. This is the same database handle that was
1978** the first argument to the sqlite3_prepare() that was used to create
1979** the statement in the first place.
1980*/
1981sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1982 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1983}
drhbb5a9c32008-06-19 02:52:251984
1985/*
drhf03d9cc2010-11-16 23:10:251986** Return true if the prepared statement is guaranteed to not modify the
1987** database.
1988*/
1989int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1990 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1991}
1992
1993/*
drh39c5c4a2019-03-06 14:53:271994** Return 1 if the statement is an EXPLAIN and return 2 if the
1995** statement is an EXPLAIN QUERY PLAN
1996*/
1997int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
1998 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
1999}
2000
2001/*
drhe393f6e2023-07-15 16:48:142002** Set the explain mode for a statement.
2003*/
2004int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){
2005 Vdbe *v = (Vdbe*)pStmt;
2006 int rc;
stephaneb62ccd2023-10-14 20:01:552007#ifdef SQLITE_ENABLE_API_ARMOR
2008 if( pStmt==0 ) return SQLITE_MISUSE_BKPT;
2009#endif
drhe393f6e2023-07-15 16:48:142010 sqlite3_mutex_enter(v->db->mutex);
drh592ae282023-08-14 12:20:442011 if( ((int)v->explain)==eMode ){
drh0c6b8692023-07-29 15:31:482012 rc = SQLITE_OK;
2013 }else if( eMode<0 || eMode>2 ){
2014 rc = SQLITE_ERROR;
2015 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
2016 rc = SQLITE_ERROR;
2017 }else if( v->eVdbeState!=VDBE_READY_STATE ){
2018 rc = SQLITE_BUSY;
2019 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
drhd1db37a2023-07-18 17:29:052020 /* No reprepare necessary */
2021 v->explain = eMode;
2022 rc = SQLITE_OK;
2023 }else{
drhd1db37a2023-07-18 17:29:052024 v->explain = eMode;
2025 rc = sqlite3Reprepare(v);
drhfb546c02023-07-29 17:05:352026 v->haveEqpOps = eMode==2;
drhd1db37a2023-07-18 17:29:052027 }
2028 if( v->explain ){
2029 v->nResColumn = 12 - 4*v->explain;
2030 }else{
2031 v->nResColumn = v->nResAlloc;
2032 }
drhe393f6e2023-07-15 16:48:142033 sqlite3_mutex_leave(v->db->mutex);
2034 return rc;
2035}
2036
2037/*
drh2fb66932011-11-25 17:21:472038** Return true if the prepared statement is in need of being reset.
2039*/
2040int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
2041 Vdbe *v = (Vdbe*)pStmt;
drh99a21822022-03-31 21:15:092042 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
drh2fb66932011-11-25 17:21:472043}
2044
2045/*
drhbb5a9c32008-06-19 02:52:252046** Return a pointer to the next prepared statement after pStmt associated
2047** with database connection pDb. If pStmt is NULL, return the first
2048** prepared statement for the database connection. Return NULL if there
2049** are no more.
2050*/
2051sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
2052 sqlite3_stmt *pNext;
drh9ca95732014-10-24 00:35:582053#ifdef SQLITE_ENABLE_API_ARMOR
2054 if( !sqlite3SafetyCheckOk(pDb) ){
2055 (void)SQLITE_MISUSE_BKPT;
2056 return 0;
2057 }
2058#endif
drhbb5a9c32008-06-19 02:52:252059 sqlite3_mutex_enter(pDb->mutex);
2060 if( pStmt==0 ){
2061 pNext = (sqlite3_stmt*)pDb->pVdbe;
2062 }else{
drhe5928b12022-08-23 20:11:012063 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
drhbb5a9c32008-06-19 02:52:252064 }
2065 sqlite3_mutex_leave(pDb->mutex);
2066 return pNext;
2067}
drhd1d38482008-10-07 23:46:382068
2069/*
2070** Return the value of a status counter for a prepared statement
2071*/
2072int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
2073 Vdbe *pVdbe = (Vdbe*)pStmt;
drh9ca95732014-10-24 00:35:582074 u32 v;
2075#ifdef SQLITE_ENABLE_API_ARMOR
larrybrbc917382023-06-07 08:40:312076 if( !pStmt
dan68cf69e2018-03-14 08:27:392077 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
2078 ){
drh9ca95732014-10-24 00:35:582079 (void)SQLITE_MISUSE_BKPT;
2080 return 0;
2081 }
2082#endif
drh3528f6b2017-05-31 16:21:542083 if( op==SQLITE_STMTSTATUS_MEMUSED ){
2084 sqlite3 *db = pVdbe->db;
2085 sqlite3_mutex_enter(db->mutex);
2086 v = 0;
2087 db->pnBytesFreed = (int*)&v;
drh376860b2022-08-22 15:18:372088 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
2089 db->lookaside.pEnd = db->lookaside.pStart;
drh1c848632022-04-04 01:12:112090 sqlite3VdbeDelete(pVdbe);
drh3528f6b2017-05-31 16:21:542091 db->pnBytesFreed = 0;
drh376860b2022-08-22 15:18:372092 db->lookaside.pEnd = db->lookaside.pTrueEnd;
drh3528f6b2017-05-31 16:21:542093 sqlite3_mutex_leave(db->mutex);
2094 }else{
2095 v = pVdbe->aCounter[op];
2096 if( resetFlag ) pVdbe->aCounter[op] = 0;
2097 }
drh9b47ee32013-08-20 03:13:512098 return (int)v;
drhd1d38482008-10-07 23:46:382099}
dan46c47d42011-03-01 18:42:072100
drh557341e2016-07-23 02:07:262101/*
2102** Return the SQL associated with a prepared statement
2103*/
2104const char *sqlite3_sql(sqlite3_stmt *pStmt){
2105 Vdbe *p = (Vdbe *)pStmt;
2106 return p ? p->zSql : 0;
2107}
2108
2109/*
2110** Return the SQL associated with a prepared statement with
2111** bound parameters expanded. Space to hold the returned string is
2112** obtained from sqlite3_malloc(). The caller is responsible for
2113** freeing the returned string by passing it to sqlite3_free().
2114**
2115** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
2116** expanded bound parameters.
2117*/
2118char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
2119#ifdef SQLITE_OMIT_TRACE
2120 return 0;
2121#else
2122 char *z = 0;
2123 const char *zSql = sqlite3_sql(pStmt);
2124 if( zSql ){
2125 Vdbe *p = (Vdbe *)pStmt;
2126 sqlite3_mutex_enter(p->db->mutex);
2127 z = sqlite3VdbeExpandSql(p, zSql);
2128 sqlite3_mutex_leave(p->db->mutex);
2129 }
2130 return z;
2131#endif
2132}
2133
mistachkin8bee11a2018-10-29 17:53:232134#ifdef SQLITE_ENABLE_NORMALIZE
2135/*
2136** Return the normalized SQL associated with a prepared statement.
2137*/
2138const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
2139 Vdbe *p = (Vdbe *)pStmt;
drh707821f2018-12-05 13:39:062140 if( p==0 ) return 0;
drh1a6c2b12018-12-10 20:01:402141 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
drh1f169fe2018-12-06 01:08:582142 sqlite3_mutex_enter(p->db->mutex);
drh1a6c2b12018-12-10 20:01:402143 p->zNormSql = sqlite3Normalize(p, p->zSql);
drh1f169fe2018-12-06 01:08:582144 sqlite3_mutex_leave(p->db->mutex);
drh707821f2018-12-05 13:39:062145 }
2146 return p->zNormSql;
mistachkin8bee11a2018-10-29 17:53:232147}
2148#endif /* SQLITE_ENABLE_NORMALIZE */
2149
drh9b1c62d2011-03-30 21:04:432150#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:182151/*
dan93bca692011-09-14 19:41:442152** Allocate and populate an UnpackedRecord structure based on the serialized
2153** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
2154** if successful, or a NULL pointer if an OOM error is encountered.
2155*/
2156static UnpackedRecord *vdbeUnpackRecord(
larrybrbc917382023-06-07 08:40:312157 KeyInfo *pKeyInfo,
2158 int nKey,
dan93bca692011-09-14 19:41:442159 const void *pKey
2160){
dan93bca692011-09-14 19:41:442161 UnpackedRecord *pRet; /* Return value */
2162
drha582b012016-12-21 19:45:542163 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
dan93bca692011-09-14 19:41:442164 if( pRet ){
drha485ad12017-08-02 22:43:142165 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
drh8658a8d2025-06-02 13:54:332166 sqlite3VdbeRecordUnpack(nKey, pKey, pRet);
dan93bca692011-09-14 19:41:442167 }
2168 return pRet;
2169}
2170
2171/*
dan37db03b2011-03-16 19:59:182172** This function is called from within a pre-update callback to retrieve
2173** a field of the row currently being updated or deleted.
2174*/
dan46c47d42011-03-01 18:42:072175int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
stephan7dc0cc42023-10-13 12:48:352176 PreUpdate *p;
dan7271d7a2017-01-25 18:53:272177 Mem *pMem;
dan46c47d42011-03-01 18:42:072178 int rc = SQLITE_OK;
dan9dcf3d02025-01-28 19:03:372179 int iStore = 0;
dan46c47d42011-03-01 18:42:072180
stephan7dc0cc42023-10-13 12:48:352181#ifdef SQLITE_ENABLE_API_ARMOR
2182 if( db==0 || ppValue==0 ){
2183 return SQLITE_MISUSE_BKPT;
2184 }
2185#endif
2186 p = db->pPreUpdate;
dan37db03b2011-03-16 19:59:182187 /* Test that this call is being made from within an SQLITE_DELETE or
2188 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
dan46c47d42011-03-01 18:42:072189 if( !p || p->op==SQLITE_INSERT ){
2190 rc = SQLITE_MISUSE_BKPT;
2191 goto preupdate_old_out;
2192 }
dancb9a3642017-01-30 19:44:532193 if( p->pPk ){
dan9dcf3d02025-01-28 19:03:372194 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
drh66cd2002025-06-24 15:58:322195 }else if( iIdx >= p->pTab->nCol ){
2196 rc = SQLITE_MISUSE_BKPT;
2197 goto preupdate_old_out;
dan9dcf3d02025-01-28 19:03:372198 }else{
2199 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
dancb9a3642017-01-30 19:44:532200 }
dan9dcf3d02025-01-28 19:03:372201 if( iStore>=p->pCsr->nField || iStore<0 ){
dan46c47d42011-03-01 18:42:072202 rc = SQLITE_RANGE;
2203 goto preupdate_old_out;
2204 }
2205
dan7271d7a2017-01-25 18:53:272206 if( iIdx==p->pTab->iPKey ){
dan4bf24c82024-11-04 16:59:022207 *ppValue = pMem = &p->oldipk;
dan7271d7a2017-01-25 18:53:272208 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
dan4bf24c82024-11-04 16:59:022209 }else{
2210
2211 /* If the old.* record has not yet been loaded into memory, do so now. */
2212 if( p->pUnpacked==0 ){
2213 u32 nRec;
2214 u8 *aRec;
2215
2216 assert( p->pCsr->eCurType==CURTYPE_BTREE );
2217 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
2218 aRec = sqlite3DbMallocRaw(db, nRec);
2219 if( !aRec ) goto preupdate_old_out;
2220 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
2221 if( rc==SQLITE_OK ){
drhb6e8f652025-03-14 19:07:112222 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
dan4bf24c82024-11-04 16:59:022223 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
dan38b31a92024-09-18 15:02:272224 }
dan4bf24c82024-11-04 16:59:022225 if( rc!=SQLITE_OK ){
2226 sqlite3DbFree(db, aRec);
2227 goto preupdate_old_out;
dan38b31a92024-09-18 15:02:272228 }
dan4bf24c82024-11-04 16:59:022229 p->aRecord = aRec;
dan38b31a92024-09-18 15:02:272230 }
dan4bf24c82024-11-04 16:59:022231
dan9dcf3d02025-01-28 19:03:372232 pMem = *ppValue = &p->pUnpacked->aMem[iStore];
2233 if( iStore>=p->pUnpacked->nField ){
dan4bf24c82024-11-04 16:59:022234 /* This occurs when the table has been extended using ALTER TABLE
2235 ** ADD COLUMN. The value to return is the default value of the column. */
2236 Column *pCol = &p->pTab->aCol[iIdx];
2237 if( pCol->iDflt>0 ){
2238 if( p->apDflt==0 ){
drhef86b942025-02-17 17:33:142239 int nByte;
2240 assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff );
2241 nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
dan4bf24c82024-11-04 16:59:022242 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
2243 if( p->apDflt==0 ) goto preupdate_old_out;
2244 }
2245 if( p->apDflt[iIdx]==0 ){
2246 sqlite3_value *pVal = 0;
2247 Expr *pDflt;
2248 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) );
2249 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
2250 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal);
2251 if( rc==SQLITE_OK && pVal==0 ){
2252 rc = SQLITE_CORRUPT_BKPT;
2253 }
2254 p->apDflt[iIdx] = pVal;
2255 }
2256 *ppValue = p->apDflt[iIdx];
2257 }else{
2258 *ppValue = (sqlite3_value *)columnNullValue();
2259 }
2260 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
2261 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
2262 testcase( pMem->flags & MEM_Int );
2263 testcase( pMem->flags & MEM_IntReal );
2264 sqlite3VdbeMemRealify(pMem);
2265 }
dan319eeb72011-03-19 08:38:502266 }
dan46c47d42011-03-01 18:42:072267 }
2268
2269 preupdate_old_out:
drhe1ed0b02014-08-26 02:15:072270 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:072271 return sqlite3ApiExit(db, rc);
2272}
drh9b1c62d2011-03-30 21:04:432273#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:072274
drh9b1c62d2011-03-30 21:04:432275#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:182276/*
2277** This function is called from within a pre-update callback to retrieve
2278** the number of columns in the row being updated, deleted or inserted.
2279*/
dan46c47d42011-03-01 18:42:072280int sqlite3_preupdate_count(sqlite3 *db){
stephan7dc0cc42023-10-13 12:48:352281 PreUpdate *p;
2282#ifdef SQLITE_ENABLE_API_ARMOR
2283 p = db!=0 ? db->pPreUpdate : 0;
2284#else
2285 p = db->pPreUpdate;
2286#endif
drhb6e8f652025-03-14 19:07:112287 return (p ? p->pKeyinfo->nKeyField : 0);
dan46c47d42011-03-01 18:42:072288}
drh9b1c62d2011-03-30 21:04:432289#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:072290
drh9b1c62d2011-03-30 21:04:432291#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan37db03b2011-03-16 19:59:182292/*
dan1e7a2d42011-03-22 18:45:292293** This function is designed to be called from within a pre-update callback
2294** only. It returns zero if the change that caused the callback was made
2295** immediately by a user SQL statement. Or, if the change was made by a
2296** trigger program, it returns the number of trigger programs currently
larrybrbc917382023-06-07 08:40:312297** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
dan1e7a2d42011-03-22 18:45:292298** top-level trigger etc.).
2299**
2300** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
2301** or SET DEFAULT action is considered a trigger.
2302*/
2303int sqlite3_preupdate_depth(sqlite3 *db){
stephan7dc0cc42023-10-13 12:48:352304 PreUpdate *p;
2305#ifdef SQLITE_ENABLE_API_ARMOR
2306 p = db!=0 ? db->pPreUpdate : 0;
2307#else
2308 p = db->pPreUpdate;
2309#endif
dan1e7a2d42011-03-22 18:45:292310 return (p ? p->v->nFrame : 0);
2311}
drh9b1c62d2011-03-30 21:04:432312#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan1e7a2d42011-03-22 18:45:292313
drh9b1c62d2011-03-30 21:04:432314#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan1e7a2d42011-03-22 18:45:292315/*
dana23a8732021-04-21 20:52:172316** This function is designed to be called from within a pre-update callback
larrybrbc917382023-06-07 08:40:312317** only.
dana23a8732021-04-21 20:52:172318*/
2319int sqlite3_preupdate_blobwrite(sqlite3 *db){
stephan7dc0cc42023-10-13 12:48:352320 PreUpdate *p;
2321#ifdef SQLITE_ENABLE_API_ARMOR
2322 p = db!=0 ? db->pPreUpdate : 0;
2323#else
2324 p = db->pPreUpdate;
2325#endif
dana23a8732021-04-21 20:52:172326 return (p ? p->iBlobWrite : -1);
2327}
2328#endif
2329
2330#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2331/*
dan37db03b2011-03-16 19:59:182332** This function is called from within a pre-update callback to retrieve
2333** a field of the row currently being updated or inserted.
2334*/
2335int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
stephan7dc0cc42023-10-13 12:48:352336 PreUpdate *p;
dan46c47d42011-03-01 18:42:072337 int rc = SQLITE_OK;
dan37db03b2011-03-16 19:59:182338 Mem *pMem;
dan9dcf3d02025-01-28 19:03:372339 int iStore = 0;
dan46c47d42011-03-01 18:42:072340
stephan7dc0cc42023-10-13 12:48:352341#ifdef SQLITE_ENABLE_API_ARMOR
2342 if( db==0 || ppValue==0 ){
2343 return SQLITE_MISUSE_BKPT;
2344 }
2345#endif
2346 p = db->pPreUpdate;
dan37db03b2011-03-16 19:59:182347 if( !p || p->op==SQLITE_DELETE ){
dan46c47d42011-03-01 18:42:072348 rc = SQLITE_MISUSE_BKPT;
dan37db03b2011-03-16 19:59:182349 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:072350 }
dancb9a3642017-01-30 19:44:532351 if( p->pPk && p->op!=SQLITE_UPDATE ){
dan9dcf3d02025-01-28 19:03:372352 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
drha67c7122025-06-24 18:27:592353 }else if( iIdx >= p->pTab->nCol ){
2354 return SQLITE_MISUSE_BKPT;
dan9dcf3d02025-01-28 19:03:372355 }else{
2356 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
dancb9a3642017-01-30 19:44:532357 }
dan9dcf3d02025-01-28 19:03:372358
2359 if( iStore>=p->pCsr->nField || iStore<0 ){
dan46c47d42011-03-01 18:42:072360 rc = SQLITE_RANGE;
dan37db03b2011-03-16 19:59:182361 goto preupdate_new_out;
dan46c47d42011-03-01 18:42:072362 }
dan46c47d42011-03-01 18:42:072363
dan37db03b2011-03-16 19:59:182364 if( p->op==SQLITE_INSERT ){
2365 /* For an INSERT, memory cell p->iNewReg contains the serialized record
2366 ** that is being inserted. Deserialize it. */
2367 UnpackedRecord *pUnpack = p->pNewUnpacked;
2368 if( !pUnpack ){
2369 Mem *pData = &p->v->aMem[p->iNewReg];
drhff535a22016-09-20 01:46:152370 rc = ExpandBlob(pData);
dan37db03b2011-03-16 19:59:182371 if( rc!=SQLITE_OK ) goto preupdate_new_out;
drhb6e8f652025-03-14 19:07:112372 pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z);
dan37db03b2011-03-16 19:59:182373 if( !pUnpack ){
2374 rc = SQLITE_NOMEM;
2375 goto preupdate_new_out;
2376 }
2377 p->pNewUnpacked = pUnpack;
2378 }
dan9dcf3d02025-01-28 19:03:372379 pMem = &pUnpack->aMem[iStore];
dan2a86c192017-01-25 17:44:132380 if( iIdx==p->pTab->iPKey ){
2381 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
dan9dcf3d02025-01-28 19:03:372382 }else if( iStore>=pUnpack->nField ){
dan37db03b2011-03-16 19:59:182383 pMem = (sqlite3_value *)columnNullValue();
dan37db03b2011-03-16 19:59:182384 }
2385 }else{
dan9dcf3d02025-01-28 19:03:372386 /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required
dan37db03b2011-03-16 19:59:182387 ** value. Make a copy of the cell contents and return a pointer to it.
2388 ** It is not safe to return a pointer to the memory cell itself as the
2389 ** caller may modify the value text encoding.
2390 */
2391 assert( p->op==SQLITE_UPDATE );
2392 if( !p->aNew ){
drhef86b942025-02-17 17:33:142393 assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff );
2394 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField);
dan37db03b2011-03-16 19:59:182395 if( !p->aNew ){
2396 rc = SQLITE_NOMEM;
2397 goto preupdate_new_out;
2398 }
2399 }
dan9dcf3d02025-01-28 19:03:372400 assert( iStore>=0 && iStore<p->pCsr->nField );
2401 pMem = &p->aNew[iStore];
dan37db03b2011-03-16 19:59:182402 if( pMem->flags==0 ){
dane43635a2016-10-21 21:21:452403 if( iIdx==p->pTab->iPKey ){
dan319eeb72011-03-19 08:38:502404 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
2405 }else{
dan9dcf3d02025-01-28 19:03:372406 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]);
dan319eeb72011-03-19 08:38:502407 if( rc!=SQLITE_OK ) goto preupdate_new_out;
2408 }
dan37db03b2011-03-16 19:59:182409 }
2410 }
2411 *ppValue = pMem;
2412
2413 preupdate_new_out:
drhe1ed0b02014-08-26 02:15:072414 sqlite3Error(db, rc);
dan46c47d42011-03-01 18:42:072415 return sqlite3ApiExit(db, rc);
2416}
drh9b1c62d2011-03-30 21:04:432417#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
drh04e8a582014-11-18 21:20:572418
dan6f9702e2014-11-01 20:38:062419#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan04489b62014-10-31 20:11:322420/*
2421** Return status data for a single loop within query pStmt.
2422*/
dan231ff4b2022-12-02 20:32:222423int sqlite3_stmt_scanstatus_v2(
drhd1a1c232014-11-03 16:35:552424 sqlite3_stmt *pStmt, /* Prepared statement being queried */
dan231ff4b2022-12-02 20:32:222425 int iScan, /* Index of loop to report on */
drhd1a1c232014-11-03 16:35:552426 int iScanStatusOp, /* Which metric to return */
dan231ff4b2022-12-02 20:32:222427 int flags,
drhd1a1c232014-11-03 16:35:552428 void *pOut /* OUT: Write the answer here */
dan04489b62014-10-31 20:11:322429){
2430 Vdbe *p = (Vdbe*)pStmt;
stephaneb62ccd2023-10-14 20:01:552431 VdbeOp *aOp;
2432 int nOp;
drh54b81e32023-04-01 15:51:212433 ScanStatus *pScan = 0;
dan231ff4b2022-12-02 20:32:222434 int idx;
2435
stephaneb62ccd2023-10-14 20:01:552436#ifdef SQLITE_ENABLE_API_ARMOR
stephanb866f982023-10-17 02:15:492437 if( p==0 || pOut==0
2438 || iScanStatusOp<SQLITE_SCANSTAT_NLOOP
2439 || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE ){
2440 return 1;
2441 }
stephaneb62ccd2023-10-14 20:01:552442#endif
2443 aOp = p->aOp;
2444 nOp = p->nOp;
dan7b56e972023-03-29 18:54:012445 if( p->pFrame ){
2446 VdbeFrame *pFrame;
2447 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
2448 aOp = pFrame->aOp;
2449 nOp = pFrame->nOp;
2450 }
2451
dan231ff4b2022-12-02 20:32:222452 if( iScan<0 ){
2453 int ii;
2454 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){
2455 i64 res = 0;
dan7b56e972023-03-29 18:54:012456 for(ii=0; ii<nOp; ii++){
2457 res += aOp[ii].nCycle;
dan231ff4b2022-12-02 20:32:222458 }
2459 *(i64*)pOut = res;
2460 return 0;
2461 }
2462 return 1;
2463 }
2464 if( flags & SQLITE_SCANSTAT_COMPLEX ){
2465 idx = iScan;
dan231ff4b2022-12-02 20:32:222466 }else{
larrybrbc917382023-06-07 08:40:312467 /* If the COMPLEX flag is clear, then this function must ignore any
dan7f4b0662022-12-07 20:09:542468 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
dan231ff4b2022-12-02 20:32:222469 for(idx=0; idx<p->nScan; idx++){
2470 pScan = &p->aScan[idx];
dana3d0c152022-12-05 18:19:562471 if( pScan->zName ){
dan231ff4b2022-12-02 20:32:222472 iScan--;
2473 if( iScan<0 ) break;
2474 }
2475 }
2476 }
2477 if( idx>=p->nScan ) return 1;
daneb5bd4d2024-03-25 18:30:152478 assert( pScan==0 || pScan==&p->aScan[idx] );
2479 pScan = &p->aScan[idx];
dan231ff4b2022-12-02 20:32:222480
drhd1a1c232014-11-03 16:35:552481 switch( iScanStatusOp ){
2482 case SQLITE_SCANSTAT_NLOOP: {
dan231ff4b2022-12-02 20:32:222483 if( pScan->addrLoop>0 ){
dan7b56e972023-03-29 18:54:012484 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
dan231ff4b2022-12-02 20:32:222485 }else{
2486 *(sqlite3_int64*)pOut = -1;
2487 }
drhd1a1c232014-11-03 16:35:552488 break;
2489 }
2490 case SQLITE_SCANSTAT_NVISIT: {
dan231ff4b2022-12-02 20:32:222491 if( pScan->addrVisit>0 ){
dan7b56e972023-03-29 18:54:012492 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
dan231ff4b2022-12-02 20:32:222493 }else{
2494 *(sqlite3_int64*)pOut = -1;
2495 }
drhd1a1c232014-11-03 16:35:552496 break;
2497 }
2498 case SQLITE_SCANSTAT_EST: {
drh518140e2014-11-06 03:55:102499 double r = 1.0;
2500 LogEst x = pScan->nEst;
2501 while( x<100 ){
2502 x += 10;
2503 r *= 0.5;
2504 }
2505 *(double*)pOut = r*sqlite3LogEstToInt(x);
drhd1a1c232014-11-03 16:35:552506 break;
2507 }
2508 case SQLITE_SCANSTAT_NAME: {
dand72219d2014-11-03 16:39:372509 *(const char**)pOut = pScan->zName;
drhd1a1c232014-11-03 16:35:552510 break;
2511 }
2512 case SQLITE_SCANSTAT_EXPLAIN: {
2513 if( pScan->addrExplain ){
dan7b56e972023-03-29 18:54:012514 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
drhd1a1c232014-11-03 16:35:552515 }else{
2516 *(const char**)pOut = 0;
2517 }
2518 break;
2519 }
drhc6652b12014-11-06 04:42:202520 case SQLITE_SCANSTAT_SELECTID: {
2521 if( pScan->addrExplain ){
dan7b56e972023-03-29 18:54:012522 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
drhc6652b12014-11-06 04:42:202523 }else{
2524 *(int*)pOut = -1;
2525 }
2526 break;
2527 }
dan231ff4b2022-12-02 20:32:222528 case SQLITE_SCANSTAT_PARENTID: {
2529 if( pScan->addrExplain ){
dan7b56e972023-03-29 18:54:012530 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
dan231ff4b2022-12-02 20:32:222531 }else{
2532 *(int*)pOut = -1;
2533 }
2534 break;
2535 }
2536 case SQLITE_SCANSTAT_NCYCLE: {
danad23a472022-12-03 21:24:262537 i64 res = 0;
2538 if( pScan->aAddrRange[0]==0 ){
2539 res = -1;
2540 }else{
dan231ff4b2022-12-02 20:32:222541 int ii;
danf6f01f12022-12-03 18:16:252542 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
2543 int iIns = pScan->aAddrRange[ii];
2544 int iEnd = pScan->aAddrRange[ii+1];
2545 if( iIns==0 ) break;
danad23a472022-12-03 21:24:262546 if( iIns>0 ){
2547 while( iIns<=iEnd ){
dan7b56e972023-03-29 18:54:012548 res += aOp[iIns].nCycle;
danad23a472022-12-03 21:24:262549 iIns++;
2550 }
2551 }else{
2552 int iOp;
dan7b56e972023-03-29 18:54:012553 for(iOp=0; iOp<nOp; iOp++){
2554 Op *pOp = &aOp[iOp];
danad23a472022-12-03 21:24:262555 if( pOp->p1!=iEnd ) continue;
dan2adb3092022-12-06 18:48:062556 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
danad23a472022-12-03 21:24:262557 continue;
2558 }
dan7b56e972023-03-29 18:54:012559 res += aOp[iOp].nCycle;
danad23a472022-12-03 21:24:262560 }
danf6f01f12022-12-03 18:16:252561 }
dan231ff4b2022-12-02 20:32:222562 }
2563 }
2564 *(i64*)pOut = res;
2565 break;
2566 }
drhd1a1c232014-11-03 16:35:552567 default: {
2568 return 1;
dan6f9702e2014-11-01 20:38:062569 }
2570 }
dan04489b62014-10-31 20:11:322571 return 0;
2572}
2573
2574/*
dan231ff4b2022-12-02 20:32:222575** Return status data for a single loop within query pStmt.
2576*/
2577int sqlite3_stmt_scanstatus(
2578 sqlite3_stmt *pStmt, /* Prepared statement being queried */
2579 int iScan, /* Index of loop to report on */
2580 int iScanStatusOp, /* Which metric to return */
2581 void *pOut /* OUT: Write the answer here */
2582){
2583 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut);
2584}
2585
dan231ff4b2022-12-02 20:32:222586/*
dan04489b62014-10-31 20:11:322587** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
2588*/
2589void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
2590 Vdbe *p = (Vdbe*)pStmt;
dan7f4b0662022-12-07 20:09:542591 int ii;
stephaneb62ccd2023-10-14 20:01:552592 for(ii=0; p!=0 && ii<p->nOp; ii++){
dan7f4b0662022-12-07 20:09:542593 Op *pOp = &p->aOp[ii];
2594 pOp->nExec = 0;
2595 pOp->nCycle = 0;
2596 }
dan04489b62014-10-31 20:11:322597}
dan6f9702e2014-11-01 20:38:062598#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */