4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This file contains code use to implement APIs that are part of the
16 #include "sqliteInt.h"
20 #ifndef SQLITE_OMIT_DEPRECATED
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
29 int sqlite3_expired(sqlite3_stmt
*pStmt
){
30 Vdbe
*p
= (Vdbe
*)pStmt
;
31 return p
==0 || p
->expired
;
36 ** 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.
40 static int vdbeSafety(Vdbe
*p
){
42 sqlite3_log(SQLITE_MISUSE
, "API called with finalized prepared statement");
48 static int vdbeSafetyNotNull(Vdbe
*p
){
50 sqlite3_log(SQLITE_MISUSE
, "API called with NULL prepared statement");
57 #ifndef SQLITE_OMIT_TRACE
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.
62 static SQLITE_NOINLINE
void invokeProfileCallback(sqlite3
*db
, Vdbe
*p
){
64 sqlite3_int64 iElapse
;
65 assert( p
->startTime
>0 );
66 assert( (db
->mTrace
& (SQLITE_TRACE_PROFILE
|SQLITE_TRACE_XPROFILE
))!=0 );
67 assert( db
->init
.busy
==0 );
69 sqlite3OsCurrentTimeInt64(db
->pVfs
, &iNow
);
70 iElapse
= (iNow
- p
->startTime
)*1000000;
71 #ifndef SQLITE_OMIT_DEPRECATED
73 db
->xProfile(db
->pProfileArg
, p
->zSql
, iElapse
);
76 if( db
->mTrace
& SQLITE_TRACE_PROFILE
){
77 db
->trace
.xV2(SQLITE_TRACE_PROFILE
, db
->pTraceArg
, p
, (void*)&iElapse
);
82 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
83 ** is needed, and it invokes the callback if it is needed.
85 # define checkProfileCallback(DB,P) \
86 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
88 # define checkProfileCallback(DB,P) /*no-op*/
92 ** The following routine destroys a virtual machine that is created by
93 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
94 ** success/failure code that describes the result of executing the virtual
97 ** This routine sets the error code and string returned by
98 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
100 int sqlite3_finalize(sqlite3_stmt
*pStmt
){
103 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
104 ** pointer is a harmless no-op. */
107 Vdbe
*v
= (Vdbe
*)pStmt
;
109 if( vdbeSafety(v
) ) return SQLITE_MISUSE_BKPT
;
110 sqlite3_mutex_enter(db
->mutex
);
111 checkProfileCallback(db
, v
);
112 assert( v
->eVdbeState
>=VDBE_READY_STATE
);
113 rc
= sqlite3VdbeReset(v
);
114 sqlite3VdbeDelete(v
);
115 rc
= sqlite3ApiExit(db
, rc
);
116 sqlite3LeaveMutexAndCloseZombie(db
);
122 ** Terminate the current execution of an SQL statement and reset it
123 ** back to its starting state so that it can be reused. A success code from
124 ** the prior execution is returned.
126 ** This routine sets the error code and string returned by
127 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
129 int sqlite3_reset(sqlite3_stmt
*pStmt
){
134 Vdbe
*v
= (Vdbe
*)pStmt
;
136 sqlite3_mutex_enter(db
->mutex
);
137 checkProfileCallback(db
, v
);
138 rc
= sqlite3VdbeReset(v
);
139 sqlite3VdbeRewind(v
);
140 assert( (rc
& (db
->errMask
))==rc
);
141 rc
= sqlite3ApiExit(db
, rc
);
142 sqlite3_mutex_leave(db
->mutex
);
148 ** Set all the parameters in the compiled SQL statement to NULL.
150 int sqlite3_clear_bindings(sqlite3_stmt
*pStmt
){
153 Vdbe
*p
= (Vdbe
*)pStmt
;
154 #if SQLITE_THREADSAFE
155 sqlite3_mutex
*mutex
;
157 #ifdef SQLITE_ENABLE_API_ARMOR
159 return SQLITE_MISUSE_BKPT
;
162 #if SQLITE_THREADSAFE
163 mutex
= p
->db
->mutex
;
165 sqlite3_mutex_enter(mutex
);
166 for(i
=0; i
<p
->nVar
; i
++){
167 sqlite3VdbeMemRelease(&p
->aVar
[i
]);
168 p
->aVar
[i
].flags
= MEM_Null
;
170 assert( (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 || p
->expmask
==0 );
174 sqlite3_mutex_leave(mutex
);
179 /**************************** sqlite3_value_ *******************************
180 ** The following routines extract information from a Mem or sqlite3_value
183 const void *sqlite3_value_blob(sqlite3_value
*pVal
){
185 if( p
->flags
& (MEM_Blob
|MEM_Str
) ){
186 if( ExpandBlob(p
)!=SQLITE_OK
){
187 assert( p
->flags
==MEM_Null
&& p
->z
==0 );
190 p
->flags
|= MEM_Blob
;
191 return p
->n
? p
->z
: 0;
193 return sqlite3_value_text(pVal
);
196 int sqlite3_value_bytes(sqlite3_value
*pVal
){
197 return sqlite3ValueBytes(pVal
, SQLITE_UTF8
);
199 int sqlite3_value_bytes16(sqlite3_value
*pVal
){
200 return sqlite3ValueBytes(pVal
, SQLITE_UTF16NATIVE
);
202 double sqlite3_value_double(sqlite3_value
*pVal
){
203 return sqlite3VdbeRealValue((Mem
*)pVal
);
205 int sqlite3_value_int(sqlite3_value
*pVal
){
206 return (int)sqlite3VdbeIntValue((Mem
*)pVal
);
208 sqlite_int64
sqlite3_value_int64(sqlite3_value
*pVal
){
209 return sqlite3VdbeIntValue((Mem
*)pVal
);
211 unsigned int sqlite3_value_subtype(sqlite3_value
*pVal
){
212 Mem
*pMem
= (Mem
*)pVal
;
213 return ((pMem
->flags
& MEM_Subtype
) ? pMem
->eSubtype
: 0);
215 void *sqlite3_value_pointer(sqlite3_value
*pVal
, const char *zPType
){
217 if( (p
->flags
&(MEM_TypeMask
|MEM_Term
|MEM_Subtype
)) ==
218 (MEM_Null
|MEM_Term
|MEM_Subtype
)
221 && strcmp(p
->u
.zPType
, zPType
)==0
228 const unsigned char *sqlite3_value_text(sqlite3_value
*pVal
){
229 return (const unsigned char *)sqlite3ValueText(pVal
, SQLITE_UTF8
);
231 #ifndef SQLITE_OMIT_UTF16
232 const void *sqlite3_value_text16(sqlite3_value
* pVal
){
233 return sqlite3ValueText(pVal
, SQLITE_UTF16NATIVE
);
235 const void *sqlite3_value_text16be(sqlite3_value
*pVal
){
236 return sqlite3ValueText(pVal
, SQLITE_UTF16BE
);
238 const void *sqlite3_value_text16le(sqlite3_value
*pVal
){
239 return sqlite3ValueText(pVal
, SQLITE_UTF16LE
);
241 #endif /* SQLITE_OMIT_UTF16 */
242 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
243 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
244 ** point number string BLOB NULL
246 int sqlite3_value_type(sqlite3_value
* pVal
){
247 static const u8 aType
[] = {
248 SQLITE_BLOB
, /* 0x00 (not possible) */
249 SQLITE_NULL
, /* 0x01 NULL */
250 SQLITE_TEXT
, /* 0x02 TEXT */
251 SQLITE_NULL
, /* 0x03 (not possible) */
252 SQLITE_INTEGER
, /* 0x04 INTEGER */
253 SQLITE_NULL
, /* 0x05 (not possible) */
254 SQLITE_INTEGER
, /* 0x06 INTEGER + TEXT */
255 SQLITE_NULL
, /* 0x07 (not possible) */
256 SQLITE_FLOAT
, /* 0x08 FLOAT */
257 SQLITE_NULL
, /* 0x09 (not possible) */
258 SQLITE_FLOAT
, /* 0x0a FLOAT + TEXT */
259 SQLITE_NULL
, /* 0x0b (not possible) */
260 SQLITE_INTEGER
, /* 0x0c (not possible) */
261 SQLITE_NULL
, /* 0x0d (not possible) */
262 SQLITE_INTEGER
, /* 0x0e (not possible) */
263 SQLITE_NULL
, /* 0x0f (not possible) */
264 SQLITE_BLOB
, /* 0x10 BLOB */
265 SQLITE_NULL
, /* 0x11 (not possible) */
266 SQLITE_TEXT
, /* 0x12 (not possible) */
267 SQLITE_NULL
, /* 0x13 (not possible) */
268 SQLITE_INTEGER
, /* 0x14 INTEGER + BLOB */
269 SQLITE_NULL
, /* 0x15 (not possible) */
270 SQLITE_INTEGER
, /* 0x16 (not possible) */
271 SQLITE_NULL
, /* 0x17 (not possible) */
272 SQLITE_FLOAT
, /* 0x18 FLOAT + BLOB */
273 SQLITE_NULL
, /* 0x19 (not possible) */
274 SQLITE_FLOAT
, /* 0x1a (not possible) */
275 SQLITE_NULL
, /* 0x1b (not possible) */
276 SQLITE_INTEGER
, /* 0x1c (not possible) */
277 SQLITE_NULL
, /* 0x1d (not possible) */
278 SQLITE_INTEGER
, /* 0x1e (not possible) */
279 SQLITE_NULL
, /* 0x1f (not possible) */
280 SQLITE_FLOAT
, /* 0x20 INTREAL */
281 SQLITE_NULL
, /* 0x21 (not possible) */
282 SQLITE_FLOAT
, /* 0x22 INTREAL + TEXT */
283 SQLITE_NULL
, /* 0x23 (not possible) */
284 SQLITE_FLOAT
, /* 0x24 (not possible) */
285 SQLITE_NULL
, /* 0x25 (not possible) */
286 SQLITE_FLOAT
, /* 0x26 (not possible) */
287 SQLITE_NULL
, /* 0x27 (not possible) */
288 SQLITE_FLOAT
, /* 0x28 (not possible) */
289 SQLITE_NULL
, /* 0x29 (not possible) */
290 SQLITE_FLOAT
, /* 0x2a (not possible) */
291 SQLITE_NULL
, /* 0x2b (not possible) */
292 SQLITE_FLOAT
, /* 0x2c (not possible) */
293 SQLITE_NULL
, /* 0x2d (not possible) */
294 SQLITE_FLOAT
, /* 0x2e (not possible) */
295 SQLITE_NULL
, /* 0x2f (not possible) */
296 SQLITE_BLOB
, /* 0x30 (not possible) */
297 SQLITE_NULL
, /* 0x31 (not possible) */
298 SQLITE_TEXT
, /* 0x32 (not possible) */
299 SQLITE_NULL
, /* 0x33 (not possible) */
300 SQLITE_FLOAT
, /* 0x34 (not possible) */
301 SQLITE_NULL
, /* 0x35 (not possible) */
302 SQLITE_FLOAT
, /* 0x36 (not possible) */
303 SQLITE_NULL
, /* 0x37 (not possible) */
304 SQLITE_FLOAT
, /* 0x38 (not possible) */
305 SQLITE_NULL
, /* 0x39 (not possible) */
306 SQLITE_FLOAT
, /* 0x3a (not possible) */
307 SQLITE_NULL
, /* 0x3b (not possible) */
308 SQLITE_FLOAT
, /* 0x3c (not possible) */
309 SQLITE_NULL
, /* 0x3d (not possible) */
310 SQLITE_FLOAT
, /* 0x3e (not possible) */
311 SQLITE_NULL
, /* 0x3f (not possible) */
315 int eType
= SQLITE_BLOB
;
316 if( pVal
->flags
& MEM_Null
){
318 }else if( pVal
->flags
& (MEM_Real
|MEM_IntReal
) ){
319 eType
= SQLITE_FLOAT
;
320 }else if( pVal
->flags
& MEM_Int
){
321 eType
= SQLITE_INTEGER
;
322 }else if( pVal
->flags
& MEM_Str
){
325 assert( eType
== aType
[pVal
->flags
&MEM_AffMask
] );
328 return aType
[pVal
->flags
&MEM_AffMask
];
330 int sqlite3_value_encoding(sqlite3_value
*pVal
){
334 /* Return true if a parameter to xUpdate represents an unchanged column */
335 int sqlite3_value_nochange(sqlite3_value
*pVal
){
336 return (pVal
->flags
&(MEM_Null
|MEM_Zero
))==(MEM_Null
|MEM_Zero
);
339 /* Return true if a parameter value originated from an sqlite3_bind() */
340 int sqlite3_value_frombind(sqlite3_value
*pVal
){
341 return (pVal
->flags
&MEM_FromBind
)!=0;
344 /* Make a copy of an sqlite3_value object
346 sqlite3_value
*sqlite3_value_dup(const sqlite3_value
*pOrig
){
348 if( pOrig
==0 ) return 0;
349 pNew
= sqlite3_malloc( sizeof(*pNew
) );
350 if( pNew
==0 ) return 0;
351 memset(pNew
, 0, sizeof(*pNew
));
352 memcpy(pNew
, pOrig
, MEMCELLSIZE
);
353 pNew
->flags
&= ~MEM_Dyn
;
355 if( pNew
->flags
&(MEM_Str
|MEM_Blob
) ){
356 pNew
->flags
&= ~(MEM_Static
|MEM_Dyn
);
357 pNew
->flags
|= MEM_Ephem
;
358 if( sqlite3VdbeMemMakeWriteable(pNew
)!=SQLITE_OK
){
359 sqlite3ValueFree(pNew
);
362 }else if( pNew
->flags
& MEM_Null
){
363 /* Do not duplicate pointer values */
364 pNew
->flags
&= ~(MEM_Term
|MEM_Subtype
);
369 /* Destroy an sqlite3_value object previously obtained from
370 ** sqlite3_value_dup().
372 void sqlite3_value_free(sqlite3_value
*pOld
){
373 sqlite3ValueFree(pOld
);
377 /**************************** sqlite3_result_ *******************************
378 ** The following routines are used by user-defined functions to specify
379 ** the function result.
381 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
382 ** result as a string or blob. Appropriate errors are set if the string/blob
383 ** is too big or if an OOM occurs.
385 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
386 ** on value P if P is not going to be used and need to be destroyed.
388 static void setResultStrOrError(
389 sqlite3_context
*pCtx
, /* Function context */
390 const char *z
, /* String pointer */
391 int n
, /* Bytes in string, or negative */
392 u8 enc
, /* Encoding of z. 0 for BLOBs */
393 void (*xDel
)(void*) /* Destructor function */
395 Mem
*pOut
= pCtx
->pOut
;
396 int rc
= sqlite3VdbeMemSetStr(pOut
, z
, n
, enc
, xDel
);
398 if( rc
==SQLITE_TOOBIG
){
399 sqlite3_result_error_toobig(pCtx
);
401 /* The only errors possible from sqlite3VdbeMemSetStr are
402 ** SQLITE_TOOBIG and SQLITE_NOMEM */
403 assert( rc
==SQLITE_NOMEM
);
404 sqlite3_result_error_nomem(pCtx
);
408 sqlite3VdbeChangeEncoding(pOut
, pCtx
->enc
);
409 if( sqlite3VdbeMemTooBig(pOut
) ){
410 sqlite3_result_error_toobig(pCtx
);
413 static int invokeValueDestructor(
414 const void *p
, /* Value to destroy */
415 void (*xDel
)(void*), /* The destructor */
416 sqlite3_context
*pCtx
/* Set a SQLITE_TOOBIG error if not NULL */
418 assert( xDel
!=SQLITE_DYNAMIC
);
421 }else if( xDel
==SQLITE_TRANSIENT
){
426 #ifdef SQLITE_ENABLE_API_ARMOR
428 sqlite3_result_error_toobig(pCtx
);
432 sqlite3_result_error_toobig(pCtx
);
434 return SQLITE_TOOBIG
;
436 void sqlite3_result_blob(
437 sqlite3_context
*pCtx
,
442 #ifdef SQLITE_ENABLE_API_ARMOR
443 if( pCtx
==0 || n
<0 ){
444 invokeValueDestructor(z
, xDel
, pCtx
);
449 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
450 setResultStrOrError(pCtx
, z
, n
, 0, xDel
);
452 void sqlite3_result_blob64(
453 sqlite3_context
*pCtx
,
458 assert( xDel
!=SQLITE_DYNAMIC
);
459 #ifdef SQLITE_ENABLE_API_ARMOR
461 invokeValueDestructor(z
, xDel
, 0);
465 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
467 (void)invokeValueDestructor(z
, xDel
, pCtx
);
469 setResultStrOrError(pCtx
, z
, (int)n
, 0, xDel
);
472 void sqlite3_result_double(sqlite3_context
*pCtx
, double rVal
){
473 #ifdef SQLITE_ENABLE_API_ARMOR
474 if( pCtx
==0 ) return;
476 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
477 sqlite3VdbeMemSetDouble(pCtx
->pOut
, rVal
);
479 void sqlite3_result_error(sqlite3_context
*pCtx
, const char *z
, int n
){
480 #ifdef SQLITE_ENABLE_API_ARMOR
481 if( pCtx
==0 ) return;
483 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
484 pCtx
->isError
= SQLITE_ERROR
;
485 sqlite3VdbeMemSetStr(pCtx
->pOut
, z
, n
, SQLITE_UTF8
, SQLITE_TRANSIENT
);
487 #ifndef SQLITE_OMIT_UTF16
488 void sqlite3_result_error16(sqlite3_context
*pCtx
, const void *z
, int n
){
489 #ifdef SQLITE_ENABLE_API_ARMOR
490 if( pCtx
==0 ) return;
492 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
493 pCtx
->isError
= SQLITE_ERROR
;
494 sqlite3VdbeMemSetStr(pCtx
->pOut
, z
, n
, SQLITE_UTF16NATIVE
, SQLITE_TRANSIENT
);
497 void sqlite3_result_int(sqlite3_context
*pCtx
, int iVal
){
498 #ifdef SQLITE_ENABLE_API_ARMOR
499 if( pCtx
==0 ) return;
501 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
502 sqlite3VdbeMemSetInt64(pCtx
->pOut
, (i64
)iVal
);
504 void sqlite3_result_int64(sqlite3_context
*pCtx
, i64 iVal
){
505 #ifdef SQLITE_ENABLE_API_ARMOR
506 if( pCtx
==0 ) return;
508 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
509 sqlite3VdbeMemSetInt64(pCtx
->pOut
, iVal
);
511 void sqlite3_result_null(sqlite3_context
*pCtx
){
512 #ifdef SQLITE_ENABLE_API_ARMOR
513 if( pCtx
==0 ) return;
515 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
516 sqlite3VdbeMemSetNull(pCtx
->pOut
);
518 void sqlite3_result_pointer(
519 sqlite3_context
*pCtx
,
522 void (*xDestructor
)(void*)
525 #ifdef SQLITE_ENABLE_API_ARMOR
527 invokeValueDestructor(pPtr
, xDestructor
, 0);
532 assert( sqlite3_mutex_held(pOut
->db
->mutex
) );
533 sqlite3VdbeMemRelease(pOut
);
534 pOut
->flags
= MEM_Null
;
535 sqlite3VdbeMemSetPointer(pOut
, pPtr
, zPType
, xDestructor
);
537 void sqlite3_result_subtype(sqlite3_context
*pCtx
, unsigned int eSubtype
){
539 #ifdef SQLITE_ENABLE_API_ARMOR
540 if( pCtx
==0 ) return;
542 #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
544 && (pCtx
->pFunc
->funcFlags
& SQLITE_RESULT_SUBTYPE
)==0
547 sqlite3_snprintf(sizeof(zErr
), zErr
,
548 "misuse of sqlite3_result_subtype() by %s()",
550 sqlite3_result_error(pCtx
, zErr
, -1);
553 #endif /* SQLITE_STRICT_SUBTYPE */
555 assert( sqlite3_mutex_held(pOut
->db
->mutex
) );
556 pOut
->eSubtype
= eSubtype
& 0xff;
557 pOut
->flags
|= MEM_Subtype
;
559 void sqlite3_result_text(
560 sqlite3_context
*pCtx
,
565 #ifdef SQLITE_ENABLE_API_ARMOR
567 invokeValueDestructor(z
, xDel
, 0);
571 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
572 setResultStrOrError(pCtx
, z
, n
, SQLITE_UTF8
, xDel
);
574 void sqlite3_result_text64(
575 sqlite3_context
*pCtx
,
578 void (*xDel
)(void *),
581 #ifdef SQLITE_ENABLE_API_ARMOR
583 invokeValueDestructor(z
, xDel
, 0);
587 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
588 assert( xDel
!=SQLITE_DYNAMIC
);
589 if( enc
!=SQLITE_UTF8
){
590 if( enc
==SQLITE_UTF16
) enc
= SQLITE_UTF16NATIVE
;
594 (void)invokeValueDestructor(z
, xDel
, pCtx
);
596 setResultStrOrError(pCtx
, z
, (int)n
, enc
, xDel
);
597 sqlite3VdbeMemZeroTerminateIfAble(pCtx
->pOut
);
600 #ifndef SQLITE_OMIT_UTF16
601 void sqlite3_result_text16(
602 sqlite3_context
*pCtx
,
607 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
608 setResultStrOrError(pCtx
, z
, n
& ~(u64
)1, SQLITE_UTF16NATIVE
, xDel
);
610 void sqlite3_result_text16be(
611 sqlite3_context
*pCtx
,
616 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
617 setResultStrOrError(pCtx
, z
, n
& ~(u64
)1, SQLITE_UTF16BE
, xDel
);
619 void sqlite3_result_text16le(
620 sqlite3_context
*pCtx
,
625 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
626 setResultStrOrError(pCtx
, z
, n
& ~(u64
)1, SQLITE_UTF16LE
, xDel
);
628 #endif /* SQLITE_OMIT_UTF16 */
629 void sqlite3_result_value(sqlite3_context
*pCtx
, sqlite3_value
*pValue
){
632 #ifdef SQLITE_ENABLE_API_ARMOR
633 if( pCtx
==0 ) return;
635 sqlite3_result_null(pCtx
);
640 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
641 sqlite3VdbeMemCopy(pOut
, pValue
);
642 sqlite3VdbeChangeEncoding(pOut
, pCtx
->enc
);
643 if( sqlite3VdbeMemTooBig(pOut
) ){
644 sqlite3_result_error_toobig(pCtx
);
647 void sqlite3_result_zeroblob(sqlite3_context
*pCtx
, int n
){
648 sqlite3_result_zeroblob64(pCtx
, n
>0 ? n
: 0);
650 int sqlite3_result_zeroblob64(sqlite3_context
*pCtx
, u64 n
){
653 #ifdef SQLITE_ENABLE_API_ARMOR
654 if( pCtx
==0 ) return SQLITE_MISUSE_BKPT
;
657 assert( sqlite3_mutex_held(pOut
->db
->mutex
) );
658 if( n
>(u64
)pOut
->db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
659 sqlite3_result_error_toobig(pCtx
);
660 return SQLITE_TOOBIG
;
662 #ifndef SQLITE_OMIT_INCRBLOB
663 sqlite3VdbeMemSetZeroBlob(pCtx
->pOut
, (int)n
);
666 return sqlite3VdbeMemSetZeroBlob(pCtx
->pOut
, (int)n
);
669 void sqlite3_result_error_code(sqlite3_context
*pCtx
, int errCode
){
670 #ifdef SQLITE_ENABLE_API_ARMOR
671 if( pCtx
==0 ) return;
673 pCtx
->isError
= errCode
? errCode
: -1;
675 if( pCtx
->pVdbe
) pCtx
->pVdbe
->rcApp
= errCode
;
677 if( pCtx
->pOut
->flags
& MEM_Null
){
678 setResultStrOrError(pCtx
, sqlite3ErrStr(errCode
), -1, SQLITE_UTF8
,
683 /* Force an SQLITE_TOOBIG error. */
684 void sqlite3_result_error_toobig(sqlite3_context
*pCtx
){
685 #ifdef SQLITE_ENABLE_API_ARMOR
686 if( pCtx
==0 ) return;
688 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
689 pCtx
->isError
= SQLITE_TOOBIG
;
690 sqlite3VdbeMemSetStr(pCtx
->pOut
, "string or blob too big", -1,
691 SQLITE_UTF8
, SQLITE_STATIC
);
694 /* An SQLITE_NOMEM error. */
695 void sqlite3_result_error_nomem(sqlite3_context
*pCtx
){
696 #ifdef SQLITE_ENABLE_API_ARMOR
697 if( pCtx
==0 ) return;
699 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
700 sqlite3VdbeMemSetNull(pCtx
->pOut
);
701 pCtx
->isError
= SQLITE_NOMEM_BKPT
;
702 sqlite3OomFault(pCtx
->pOut
->db
);
705 #ifndef SQLITE_UNTESTABLE
706 /* Force the INT64 value currently stored as the result to be
707 ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
710 void sqlite3ResultIntReal(sqlite3_context
*pCtx
){
711 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
712 if( pCtx
->pOut
->flags
& MEM_Int
){
713 pCtx
->pOut
->flags
&= ~MEM_Int
;
714 pCtx
->pOut
->flags
|= MEM_IntReal
;
721 ** This function is called after a transaction has been committed. It
722 ** invokes callbacks registered with sqlite3_wal_hook() as required.
724 static int doWalCallbacks(sqlite3
*db
){
726 #ifndef SQLITE_OMIT_WAL
728 for(i
=0; i
<db
->nDb
; i
++){
729 Btree
*pBt
= db
->aDb
[i
].pBt
;
732 sqlite3BtreeEnter(pBt
);
733 nEntry
= sqlite3PagerWalCallback(sqlite3BtreePager(pBt
));
734 sqlite3BtreeLeave(pBt
);
735 if( nEntry
>0 && db
->xWalCallback
&& rc
==SQLITE_OK
){
736 rc
= db
->xWalCallback(db
->pWalArg
, db
, db
->aDb
[i
].zDbSName
, nEntry
);
746 ** Execute the statement pStmt, either until a row of data is ready, the
747 ** statement is completely executed or an error occurs.
749 ** This routine implements the bulk of the logic behind the sqlite_step()
750 ** API. The only thing omitted is the automatic recompile if a
751 ** schema change has occurred. That detail is handled by the
752 ** outer sqlite3_step() wrapper procedure.
754 static int sqlite3Step(Vdbe
*p
){
760 if( p
->eVdbeState
!=VDBE_RUN_STATE
){
762 if( p
->eVdbeState
==VDBE_READY_STATE
){
764 p
->rc
= SQLITE_SCHEMA
;
766 if( (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 ){
767 /* If this statement was prepared using saved SQL and an
768 ** error has occurred, then return the error code in p->rc to the
769 ** caller. Set the error code in the database handle to the same
772 rc
= sqlite3VdbeTransferError(p
);
777 /* If there are no other statements currently running, then
778 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
779 ** from interrupting a statement that has not yet started.
781 if( db
->nVdbeActive
==0 ){
782 AtomicStore(&db
->u1
.isInterrupted
, 0);
785 assert( db
->nVdbeWrite
>0 || db
->autoCommit
==0
786 || (db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0)
789 #ifndef SQLITE_OMIT_TRACE
790 if( (db
->mTrace
& (SQLITE_TRACE_PROFILE
|SQLITE_TRACE_XPROFILE
))!=0
791 && !db
->init
.busy
&& p
->zSql
){
792 sqlite3OsCurrentTimeInt64(db
->pVfs
, &p
->startTime
);
794 assert( p
->startTime
==0 );
799 if( p
->readOnly
==0 ) db
->nVdbeWrite
++;
800 if( p
->bIsReader
) db
->nVdbeRead
++;
802 p
->eVdbeState
= VDBE_RUN_STATE
;
805 if( ALWAYS(p
->eVdbeState
==VDBE_HALT_STATE
) ){
806 /* We used to require that sqlite3_reset() be called before retrying
807 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
808 ** with version 3.7.0, we changed this so that sqlite3_reset() would
809 ** be called automatically instead of throwing the SQLITE_MISUSE error.
810 ** This "automatic-reset" change is not technically an incompatibility,
811 ** since any application that receives an SQLITE_MISUSE is broken by
814 ** Nevertheless, some published applications that were originally written
815 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
816 ** returns, and those were broken by the automatic-reset change. As a
817 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
818 ** legacy behavior of returning SQLITE_MISUSE for cases where the
819 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
820 ** or SQLITE_BUSY error.
822 #ifdef SQLITE_OMIT_AUTORESET
823 if( (rc
= p
->rc
&0xff)==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
824 sqlite3_reset((sqlite3_stmt
*)p
);
826 return SQLITE_MISUSE_BKPT
;
829 sqlite3_reset((sqlite3_stmt
*)p
);
831 assert( p
->eVdbeState
==VDBE_READY_STATE
);
837 p
->rcApp
= SQLITE_OK
;
839 #ifndef SQLITE_OMIT_EXPLAIN
841 rc
= sqlite3VdbeList(p
);
843 #endif /* SQLITE_OMIT_EXPLAIN */
846 rc
= sqlite3VdbeExec(p
);
850 if( rc
==SQLITE_ROW
){
851 assert( p
->rc
==SQLITE_OK
);
852 assert( db
->mallocFailed
==0 );
853 db
->errCode
= SQLITE_ROW
;
856 #ifndef SQLITE_OMIT_TRACE
857 /* If the statement completed successfully, invoke the profile callback */
858 checkProfileCallback(db
, p
);
861 if( rc
==SQLITE_DONE
&& db
->autoCommit
){
862 assert( p
->rc
==SQLITE_OK
);
863 p
->rc
= doWalCallbacks(db
);
864 if( p
->rc
!=SQLITE_OK
){
867 }else if( rc
!=SQLITE_DONE
&& (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 ){
868 /* If this statement was prepared using saved SQL and an
869 ** error has occurred, then return the error code in p->rc to the
870 ** caller. Set the error code in the database handle to the same value.
872 rc
= sqlite3VdbeTransferError(p
);
877 if( SQLITE_NOMEM
==sqlite3ApiExit(p
->db
, p
->rc
) ){
878 p
->rc
= SQLITE_NOMEM_BKPT
;
879 if( (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 ) rc
= p
->rc
;
882 /* There are only a limited number of result codes allowed from the
883 ** statements prepared using the legacy sqlite3_prepare() interface */
884 assert( (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0
885 || rc
==SQLITE_ROW
|| rc
==SQLITE_DONE
|| rc
==SQLITE_ERROR
886 || (rc
&0xff)==SQLITE_BUSY
|| rc
==SQLITE_MISUSE
888 return (rc
&db
->errMask
);
892 ** This is the top-level implementation of sqlite3_step(). Call
893 ** sqlite3Step() to do most of the work. If a schema error occurs,
894 ** call sqlite3Reprepare() and try again.
896 int sqlite3_step(sqlite3_stmt
*pStmt
){
897 int rc
= SQLITE_OK
; /* Result from sqlite3Step() */
898 Vdbe
*v
= (Vdbe
*)pStmt
; /* the prepared statement */
899 int cnt
= 0; /* Counter to prevent infinite loop of reprepares */
900 sqlite3
*db
; /* The database connection */
902 if( vdbeSafetyNotNull(v
) ){
903 return SQLITE_MISUSE_BKPT
;
906 sqlite3_mutex_enter(db
->mutex
);
907 while( (rc
= sqlite3Step(v
))==SQLITE_SCHEMA
908 && cnt
++ < SQLITE_MAX_SCHEMA_RETRY
){
910 rc
= sqlite3Reprepare(v
);
912 /* This case occurs after failing to recompile an sql statement.
913 ** The error message from the SQL compiler has already been loaded
914 ** into the database handle. This block copies the error message
915 ** from the database handle into the statement and sets the statement
916 ** program counter to 0 to ensure that when the statement is
917 ** finalized or reset the parser error message is available via
918 ** sqlite3_errmsg() and sqlite3_errcode().
920 const char *zErr
= (const char *)sqlite3_value_text(db
->pErr
);
921 sqlite3DbFree(db
, v
->zErrMsg
);
922 if( !db
->mallocFailed
){
923 v
->zErrMsg
= sqlite3DbStrDup(db
, zErr
);
924 v
->rc
= rc
= sqlite3ApiExit(db
, rc
);
927 v
->rc
= rc
= SQLITE_NOMEM_BKPT
;
931 sqlite3_reset(pStmt
);
933 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
934 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
935 ** already been done once on a prior invocation that failed due to
936 ** SQLITE_SCHEMA. tag-20220401a */
937 v
->minWriteFileFormat
= 254;
939 assert( v
->expired
==0 );
941 sqlite3_mutex_leave(db
->mutex
);
947 ** Extract the user data from a sqlite3_context structure and return a
950 void *sqlite3_user_data(sqlite3_context
*p
){
951 #ifdef SQLITE_ENABLE_API_ARMOR
954 assert( p
&& p
->pFunc
);
955 return p
->pFunc
->pUserData
;
959 ** Extract the user data from a sqlite3_context structure and return a
962 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
963 ** returns a copy of the pointer to the database connection (the 1st
964 ** parameter) of the sqlite3_create_function() and
965 ** sqlite3_create_function16() routines that originally registered the
966 ** application defined function.
968 sqlite3
*sqlite3_context_db_handle(sqlite3_context
*p
){
969 #ifdef SQLITE_ENABLE_API_ARMOR
972 assert( p
&& p
->pOut
);
978 ** If this routine is invoked from within an xColumn method of a virtual
979 ** table, then it returns true if and only if the the call is during an
980 ** UPDATE operation and the value of the column will not be modified
983 ** If this routine is called from any context other than within the
984 ** xColumn method of a virtual table, then the return value is meaningless
987 ** Virtual table implements might use this routine to optimize their
988 ** performance by substituting a NULL result, or some other light-weight
989 ** value, as a signal to the xUpdate routine that the column is unchanged.
991 int sqlite3_vtab_nochange(sqlite3_context
*p
){
992 #ifdef SQLITE_ENABLE_API_ARMOR
997 return sqlite3_value_nochange(p
->pOut
);
1001 ** The destructor function for a ValueList object. This needs to be
1002 ** a separate function, unknowable to the application, to ensure that
1003 ** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not
1004 ** preceded by activation of IN processing via sqlite3_vtab_int() do not
1005 ** try to access a fake ValueList object inserted by a hostile extension.
1007 void sqlite3VdbeValueListFree(void *pToDelete
){
1008 sqlite3_free(pToDelete
);
1012 ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
1013 ** sqlite3_vtab_in_next() (if bNext!=0).
1015 static int valueFromValueList(
1016 sqlite3_value
*pVal
, /* Pointer to the ValueList object */
1017 sqlite3_value
**ppOut
, /* Store the next value from the list here */
1018 int bNext
/* 1 for _next(). 0 for _first() */
1024 if( pVal
==0 ) return SQLITE_MISUSE_BKPT
;
1025 if( (pVal
->flags
& MEM_Dyn
)==0 || pVal
->xDel
!=sqlite3VdbeValueListFree
){
1026 return SQLITE_ERROR
;
1028 assert( (pVal
->flags
&(MEM_TypeMask
|MEM_Term
|MEM_Subtype
)) ==
1029 (MEM_Null
|MEM_Term
|MEM_Subtype
) );
1030 assert( pVal
->eSubtype
=='p' );
1031 assert( pVal
->u
.zPType
!=0 && strcmp(pVal
->u
.zPType
,"ValueList")==0 );
1032 pRhs
= (ValueList
*)pVal
->z
;
1035 rc
= sqlite3BtreeNext(pRhs
->pCsr
, 0);
1038 rc
= sqlite3BtreeFirst(pRhs
->pCsr
, &dummy
);
1039 assert( rc
==SQLITE_OK
|| sqlite3BtreeEof(pRhs
->pCsr
) );
1040 if( sqlite3BtreeEof(pRhs
->pCsr
) ) rc
= SQLITE_DONE
;
1042 if( rc
==SQLITE_OK
){
1043 u32 sz
; /* Size of current row in bytes */
1044 Mem sMem
; /* Raw content of current row */
1045 memset(&sMem
, 0, sizeof(sMem
));
1046 sz
= sqlite3BtreePayloadSize(pRhs
->pCsr
);
1047 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pRhs
->pCsr
,(int)sz
,&sMem
);
1048 if( rc
==SQLITE_OK
){
1049 u8
*zBuf
= (u8
*)sMem
.z
;
1051 sqlite3_value
*pOut
= pRhs
->pOut
;
1052 int iOff
= 1 + getVarint32(&zBuf
[1], iSerial
);
1053 sqlite3VdbeSerialGet(&zBuf
[iOff
], iSerial
, pOut
);
1054 pOut
->enc
= ENC(pOut
->db
);
1055 if( (pOut
->flags
& MEM_Ephem
)!=0 && sqlite3VdbeMemMakeWriteable(pOut
) ){
1061 sqlite3VdbeMemRelease(&sMem
);
1067 ** Set the iterator value pVal to point to the first value in the set.
1068 ** Set (*ppOut) to point to this value before returning.
1070 int sqlite3_vtab_in_first(sqlite3_value
*pVal
, sqlite3_value
**ppOut
){
1071 return valueFromValueList(pVal
, ppOut
, 0);
1075 ** Set the iterator value pVal to point to the next value in the set.
1076 ** Set (*ppOut) to point to this value before returning.
1078 int sqlite3_vtab_in_next(sqlite3_value
*pVal
, sqlite3_value
**ppOut
){
1079 return valueFromValueList(pVal
, ppOut
, 1);
1083 ** Return the current time for a statement. If the current time
1084 ** is requested more than once within the same run of a single prepared
1085 ** statement, the exact same time is returned for each invocation regardless
1086 ** of the amount of time that elapses between invocations. In other words,
1087 ** the time returned is always the time of the first call.
1089 sqlite3_int64
sqlite3StmtCurrentTime(sqlite3_context
*p
){
1091 #ifndef SQLITE_ENABLE_STAT4
1092 sqlite3_int64
*piTime
= &p
->pVdbe
->iCurrentTime
;
1093 assert( p
->pVdbe
!=0 );
1095 sqlite3_int64 iTime
= 0;
1096 sqlite3_int64
*piTime
= p
->pVdbe
!=0 ? &p
->pVdbe
->iCurrentTime
: &iTime
;
1099 rc
= sqlite3OsCurrentTimeInt64(p
->pOut
->db
->pVfs
, piTime
);
1100 if( rc
) *piTime
= 0;
1106 ** Create a new aggregate context for p and return a pointer to
1107 ** its pMem->z element.
1109 static SQLITE_NOINLINE
void *createAggContext(sqlite3_context
*p
, int nByte
){
1110 Mem
*pMem
= p
->pMem
;
1111 assert( (pMem
->flags
& MEM_Agg
)==0 );
1113 sqlite3VdbeMemSetNull(pMem
);
1116 sqlite3VdbeMemClearAndResize(pMem
, nByte
);
1117 pMem
->flags
= MEM_Agg
;
1118 pMem
->u
.pDef
= p
->pFunc
;
1120 memset(pMem
->z
, 0, nByte
);
1123 return (void*)pMem
->z
;
1127 ** Allocate or return the aggregate context for a user function. A new
1128 ** context is allocated on the first call. Subsequent calls return the
1129 ** same context that was returned on prior calls.
1131 void *sqlite3_aggregate_context(sqlite3_context
*p
, int nByte
){
1132 assert( p
&& p
->pFunc
&& p
->pFunc
->xFinalize
);
1133 assert( sqlite3_mutex_held(p
->pOut
->db
->mutex
) );
1134 testcase( nByte
<0 );
1135 if( (p
->pMem
->flags
& MEM_Agg
)==0 ){
1136 return createAggContext(p
, nByte
);
1138 return (void*)p
->pMem
->z
;
1143 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
1144 ** the user-function defined by pCtx.
1146 ** The left-most argument is 0.
1148 ** Undocumented behavior: If iArg is negative then access a cache of
1149 ** auxiliary data pointers that is available to all functions within a
1150 ** single prepared statement. The iArg values must match.
1152 void *sqlite3_get_auxdata(sqlite3_context
*pCtx
, int iArg
){
1155 #ifdef SQLITE_ENABLE_API_ARMOR
1156 if( pCtx
==0 ) return 0;
1158 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
1159 #if SQLITE_ENABLE_STAT4
1160 if( pCtx
->pVdbe
==0 ) return 0;
1162 assert( pCtx
->pVdbe
!=0 );
1164 for(pAuxData
=pCtx
->pVdbe
->pAuxData
; pAuxData
; pAuxData
=pAuxData
->pNextAux
){
1165 if( pAuxData
->iAuxArg
==iArg
&& (pAuxData
->iAuxOp
==pCtx
->iOp
|| iArg
<0) ){
1166 return pAuxData
->pAux
;
1173 ** Set the auxiliary data pointer and delete function, for the iArg'th
1174 ** argument to the user-function defined by pCtx. Any previous value is
1175 ** deleted by calling the delete function specified when it was set.
1177 ** The left-most argument is 0.
1179 ** Undocumented behavior: If iArg is negative then make the data available
1180 ** to all functions within the current prepared statement using iArg as an
1183 void sqlite3_set_auxdata(
1184 sqlite3_context
*pCtx
,
1187 void (*xDelete
)(void*)
1192 #ifdef SQLITE_ENABLE_API_ARMOR
1193 if( pCtx
==0 ) return;
1196 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
1197 #ifdef SQLITE_ENABLE_STAT4
1198 if( pVdbe
==0 ) goto failed
;
1203 for(pAuxData
=pVdbe
->pAuxData
; pAuxData
; pAuxData
=pAuxData
->pNextAux
){
1204 if( pAuxData
->iAuxArg
==iArg
&& (pAuxData
->iAuxOp
==pCtx
->iOp
|| iArg
<0) ){
1209 pAuxData
= sqlite3DbMallocZero(pVdbe
->db
, sizeof(AuxData
));
1210 if( !pAuxData
) goto failed
;
1211 pAuxData
->iAuxOp
= pCtx
->iOp
;
1212 pAuxData
->iAuxArg
= iArg
;
1213 pAuxData
->pNextAux
= pVdbe
->pAuxData
;
1214 pVdbe
->pAuxData
= pAuxData
;
1215 if( pCtx
->isError
==0 ) pCtx
->isError
= -1;
1216 }else if( pAuxData
->xDeleteAux
){
1217 pAuxData
->xDeleteAux(pAuxData
->pAux
);
1220 pAuxData
->pAux
= pAux
;
1221 pAuxData
->xDeleteAux
= xDelete
;
1230 #ifndef SQLITE_OMIT_DEPRECATED
1232 ** Return the number of times the Step function of an aggregate has been
1235 ** This function is deprecated. Do not use it for new code. It is
1236 ** provide only to avoid breaking legacy code. New aggregate function
1237 ** implementations should keep their own counts within their aggregate
1240 int sqlite3_aggregate_count(sqlite3_context
*p
){
1241 assert( p
&& p
->pMem
&& p
->pFunc
&& p
->pFunc
->xFinalize
);
1247 ** Return the number of columns in the result set for the statement pStmt.
1249 int sqlite3_column_count(sqlite3_stmt
*pStmt
){
1250 Vdbe
*pVm
= (Vdbe
*)pStmt
;
1251 if( pVm
==0 ) return 0;
1252 return pVm
->nResColumn
;
1256 ** Return the number of values available from the current row of the
1257 ** currently executing statement pStmt.
1259 int sqlite3_data_count(sqlite3_stmt
*pStmt
){
1260 Vdbe
*pVm
= (Vdbe
*)pStmt
;
1261 if( pVm
==0 || pVm
->pResultRow
==0 ) return 0;
1262 return pVm
->nResColumn
;
1266 ** Return a pointer to static memory containing an SQL NULL value.
1268 static const Mem
*columnNullValue(void){
1269 /* Even though the Mem structure contains an element
1270 ** of type i64, on certain architectures (x86) with certain compiler
1271 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
1272 ** instead of an 8-byte one. This all works fine, except that when
1273 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
1274 ** that a Mem structure is located on an 8-byte boundary. To prevent
1275 ** these assert()s from failing, when building with SQLITE_DEBUG defined
1276 ** using gcc, we force nullMem to be 8-byte aligned using the magical
1277 ** __attribute__((aligned(8))) macro. */
1278 static const Mem nullMem
1279 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
1280 __attribute__((aligned(8)))
1284 /* .z = */ (char*)0,
1286 /* .flags = */ (u16
)MEM_Null
,
1288 /* .eSubtype = */ (u8
)0,
1289 /* .db = */ (sqlite3
*)0,
1290 /* .szMalloc = */ (int)0,
1291 /* .uTemp = */ (u32
)0,
1292 /* .zMalloc = */ (char*)0,
1293 /* .xDel = */ (void(*)(void*))0,
1295 /* .pScopyFrom = */ (Mem
*)0,
1296 /* .mScopyFlags= */ 0,
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
1308 static Mem
*columnMem(sqlite3_stmt
*pStmt
, int i
){
1312 pVm
= (Vdbe
*)pStmt
;
1313 if( pVm
==0 ) return (Mem
*)columnNullValue();
1315 sqlite3_mutex_enter(pVm
->db
->mutex
);
1316 if( pVm
->pResultRow
!=0 && i
<pVm
->nResColumn
&& i
>=0 ){
1317 pOut
= &pVm
->pResultRow
[i
];
1319 sqlite3Error(pVm
->db
, SQLITE_RANGE
);
1320 pOut
= (Mem
*)columnNullValue();
1326 ** This function is called after invoking an sqlite3_value_XXX function on a
1327 ** column value (i.e. a value returned by evaluating an SQL expression in the
1328 ** select list of a SELECT statement) that may cause a malloc() failure. If
1329 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
1330 ** code of statement pStmt set to SQLITE_NOMEM.
1332 ** Specifically, this is called from within:
1334 ** sqlite3_column_int()
1335 ** sqlite3_column_int64()
1336 ** sqlite3_column_text()
1337 ** sqlite3_column_text16()
1338 ** sqlite3_column_real()
1339 ** sqlite3_column_bytes()
1340 ** sqlite3_column_bytes16()
1341 ** sqlite3_column_blob()
1343 static void columnMallocFailure(sqlite3_stmt
*pStmt
)
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.
1350 Vdbe
*p
= (Vdbe
*)pStmt
;
1353 assert( sqlite3_mutex_held(p
->db
->mutex
) );
1354 p
->rc
= sqlite3ApiExit(p
->db
, p
->rc
);
1355 sqlite3_mutex_leave(p
->db
->mutex
);
1359 /**************************** sqlite3_column_ *******************************
1360 ** The following routines are used to access elements of the current row
1361 ** in the result set.
1363 const void *sqlite3_column_blob(sqlite3_stmt
*pStmt
, int i
){
1365 val
= sqlite3_value_blob( columnMem(pStmt
,i
) );
1366 /* Even though there is no encoding conversion, value_blob() might
1367 ** need to call malloc() to expand the result of a zeroblob()
1370 columnMallocFailure(pStmt
);
1373 int sqlite3_column_bytes(sqlite3_stmt
*pStmt
, int i
){
1374 int val
= sqlite3_value_bytes( columnMem(pStmt
,i
) );
1375 columnMallocFailure(pStmt
);
1378 int sqlite3_column_bytes16(sqlite3_stmt
*pStmt
, int i
){
1379 int val
= sqlite3_value_bytes16( columnMem(pStmt
,i
) );
1380 columnMallocFailure(pStmt
);
1383 double sqlite3_column_double(sqlite3_stmt
*pStmt
, int i
){
1384 double val
= sqlite3_value_double( columnMem(pStmt
,i
) );
1385 columnMallocFailure(pStmt
);
1388 int sqlite3_column_int(sqlite3_stmt
*pStmt
, int i
){
1389 int val
= sqlite3_value_int( columnMem(pStmt
,i
) );
1390 columnMallocFailure(pStmt
);
1393 sqlite_int64
sqlite3_column_int64(sqlite3_stmt
*pStmt
, int i
){
1394 sqlite_int64 val
= sqlite3_value_int64( columnMem(pStmt
,i
) );
1395 columnMallocFailure(pStmt
);
1398 const unsigned char *sqlite3_column_text(sqlite3_stmt
*pStmt
, int i
){
1399 const unsigned char *val
= sqlite3_value_text( columnMem(pStmt
,i
) );
1400 columnMallocFailure(pStmt
);
1403 sqlite3_value
*sqlite3_column_value(sqlite3_stmt
*pStmt
, int i
){
1404 Mem
*pOut
= columnMem(pStmt
, i
);
1405 if( pOut
->flags
&MEM_Static
){
1406 pOut
->flags
&= ~MEM_Static
;
1407 pOut
->flags
|= MEM_Ephem
;
1409 columnMallocFailure(pStmt
);
1410 return (sqlite3_value
*)pOut
;
1412 #ifndef SQLITE_OMIT_UTF16
1413 const void *sqlite3_column_text16(sqlite3_stmt
*pStmt
, int i
){
1414 const void *val
= sqlite3_value_text16( columnMem(pStmt
,i
) );
1415 columnMallocFailure(pStmt
);
1418 #endif /* SQLITE_OMIT_UTF16 */
1419 int sqlite3_column_type(sqlite3_stmt
*pStmt
, int i
){
1420 int iType
= sqlite3_value_type( columnMem(pStmt
,i
) );
1421 columnMallocFailure(pStmt
);
1426 ** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN.
1428 static const char * const azExplainColNames8
[] = {
1429 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */
1430 "id", "parent", "notused", "detail" /* EQP */
1432 static 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
1446 static const u8 iExplainColNames16
[] = {
1447 0, 5, 12, 15, 18, 21, 24, 27,
1452 ** 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.
1455 ** There are up to 5 names for each column. useType determines which
1456 ** name is returned. Here are the names:
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
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.
1467 static const void *columnName(
1468 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 */
1477 #ifdef SQLITE_ENABLE_API_ARMOR
1479 (void)SQLITE_MISUSE_BKPT
;
1488 sqlite3_mutex_enter(db
->mutex
);
1491 if( useType
>0 ) goto columnName_end
;
1492 n
= p
->explain
==1 ? 8 : 4;
1493 if( N
>=n
) goto columnName_end
;
1495 int i
= iExplainColNames16
[N
+ 8*p
->explain
- 8];
1496 ret
= (void*)&azExplainColNames16data
[i
];
1498 ret
= (void*)azExplainColNames8
[N
+ 8*p
->explain
- 8];
1500 goto columnName_end
;
1504 u8 prior_mallocFailed
= db
->mallocFailed
;
1506 #ifndef SQLITE_OMIT_UTF16
1508 ret
= sqlite3_value_text16((sqlite3_value
*)&p
->aColName
[N
]);
1512 ret
= sqlite3_value_text((sqlite3_value
*)&p
->aColName
[N
]);
1514 /* A malloc may have failed inside of the _text() call. If this
1515 ** is the case, clear the mallocFailed flag and return NULL.
1517 assert( db
->mallocFailed
==0 || db
->mallocFailed
==1 );
1518 if( db
->mallocFailed
> prior_mallocFailed
){
1519 sqlite3OomClear(db
);
1524 sqlite3_mutex_leave(db
->mutex
);
1529 ** Return the name of the Nth column of the result set returned by SQL
1532 const char *sqlite3_column_name(sqlite3_stmt
*pStmt
, int N
){
1533 return columnName(pStmt
, N
, 0, COLNAME_NAME
);
1535 #ifndef SQLITE_OMIT_UTF16
1536 const void *sqlite3_column_name16(sqlite3_stmt
*pStmt
, int N
){
1537 return columnName(pStmt
, N
, 1, COLNAME_NAME
);
1542 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1543 ** not define OMIT_DECLTYPE.
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"
1550 #ifndef SQLITE_OMIT_DECLTYPE
1552 ** Return the column declaration type (if applicable) of the 'i'th column
1553 ** of the result set of SQL statement pStmt.
1555 const char *sqlite3_column_decltype(sqlite3_stmt
*pStmt
, int N
){
1556 return columnName(pStmt
, N
, 0, COLNAME_DECLTYPE
);
1558 #ifndef SQLITE_OMIT_UTF16
1559 const void *sqlite3_column_decltype16(sqlite3_stmt
*pStmt
, int N
){
1560 return columnName(pStmt
, N
, 1, COLNAME_DECLTYPE
);
1562 #endif /* SQLITE_OMIT_UTF16 */
1563 #endif /* SQLITE_OMIT_DECLTYPE */
1565 #ifdef SQLITE_ENABLE_COLUMN_METADATA
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
1569 ** anything else which is not an unambiguous reference to a database column.
1571 const char *sqlite3_column_database_name(sqlite3_stmt
*pStmt
, int N
){
1572 return columnName(pStmt
, N
, 0, COLNAME_DATABASE
);
1574 #ifndef SQLITE_OMIT_UTF16
1575 const void *sqlite3_column_database_name16(sqlite3_stmt
*pStmt
, int N
){
1576 return columnName(pStmt
, N
, 1, COLNAME_DATABASE
);
1578 #endif /* SQLITE_OMIT_UTF16 */
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
1583 ** anything else which is not an unambiguous reference to a database column.
1585 const char *sqlite3_column_table_name(sqlite3_stmt
*pStmt
, int N
){
1586 return columnName(pStmt
, N
, 0, COLNAME_TABLE
);
1588 #ifndef SQLITE_OMIT_UTF16
1589 const void *sqlite3_column_table_name16(sqlite3_stmt
*pStmt
, int N
){
1590 return columnName(pStmt
, N
, 1, COLNAME_TABLE
);
1592 #endif /* SQLITE_OMIT_UTF16 */
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
1597 ** anything else which is not an unambiguous reference to a database column.
1599 const char *sqlite3_column_origin_name(sqlite3_stmt
*pStmt
, int N
){
1600 return columnName(pStmt
, N
, 0, COLNAME_COLUMN
);
1602 #ifndef SQLITE_OMIT_UTF16
1603 const void *sqlite3_column_origin_name16(sqlite3_stmt
*pStmt
, int N
){
1604 return columnName(pStmt
, N
, 1, COLNAME_COLUMN
);
1606 #endif /* SQLITE_OMIT_UTF16 */
1607 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
1610 /******************************* sqlite3_bind_ ***************************
1612 ** Routines used to attach values to wildcards in a compiled SQL statement.
1615 ** Unbind the value bound to variable i in virtual machine p. This is the
1616 ** the same as binding a NULL value to the column. If the "i" parameter is
1617 ** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
1619 ** A successful evaluation of this routine acquires the mutex on p.
1620 ** the mutex is released if any kind of error occurs.
1622 ** The error code stored in database p->db is overwritten with the return
1623 ** value in any case.
1625 ** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK,
1626 ** that means all of the the following will be true:
1633 ** An assert() is normally added after vdbeUnbind() to help static analyzers
1636 static int vdbeUnbind(Vdbe
*p
, unsigned int i
){
1638 if( vdbeSafetyNotNull(p
) ){
1639 return SQLITE_MISUSE_BKPT
;
1641 sqlite3_mutex_enter(p
->db
->mutex
);
1642 if( p
->eVdbeState
!=VDBE_READY_STATE
){
1643 sqlite3Error(p
->db
, SQLITE_MISUSE_BKPT
);
1644 sqlite3_mutex_leave(p
->db
->mutex
);
1645 sqlite3_log(SQLITE_MISUSE
,
1646 "bind on a busy prepared statement: [%s]", p
->zSql
);
1647 return SQLITE_MISUSE_BKPT
;
1649 if( i
>=(unsigned int)p
->nVar
){
1650 sqlite3Error(p
->db
, SQLITE_RANGE
);
1651 sqlite3_mutex_leave(p
->db
->mutex
);
1652 return SQLITE_RANGE
;
1655 sqlite3VdbeMemRelease(pVar
);
1656 pVar
->flags
= MEM_Null
;
1657 p
->db
->errCode
= SQLITE_OK
;
1659 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1660 ** binding a new value to this variable invalidates the current query plan.
1662 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
1663 ** 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.
1668 assert( (p
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 || p
->expmask
==0 );
1669 if( p
->expmask
!=0 && (p
->expmask
& (i
>=31 ? 0x80000000 : (u32
)1<<i
))!=0 ){
1676 ** Bind a text or BLOB value.
1678 static int bindText(
1679 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 */
1682 i64 nData
, /* Number of bytes of data to be bound */
1683 void (*xDel
)(void*), /* Destructor for the data */
1684 u8 encoding
/* Encoding for the data */
1686 Vdbe
*p
= (Vdbe
*)pStmt
;
1690 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1691 if( rc
==SQLITE_OK
){
1692 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
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
));
1700 sqlite3Error(p
->db
, rc
);
1701 rc
= sqlite3ApiExit(p
->db
, rc
);
1704 sqlite3_mutex_leave(p
->db
->mutex
);
1705 }else if( xDel
!=SQLITE_STATIC
&& xDel
!=SQLITE_TRANSIENT
){
1713 ** Bind a blob value to an SQL statement variable.
1715 int sqlite3_bind_blob(
1716 sqlite3_stmt
*pStmt
,
1722 #ifdef SQLITE_ENABLE_API_ARMOR
1723 if( nData
<0 ) return SQLITE_MISUSE_BKPT
;
1725 return bindText(pStmt
, i
, zData
, nData
, xDel
, 0);
1727 int sqlite3_bind_blob64(
1728 sqlite3_stmt
*pStmt
,
1731 sqlite3_uint64 nData
,
1734 assert( xDel
!=SQLITE_DYNAMIC
);
1735 return bindText(pStmt
, i
, zData
, nData
, xDel
, 0);
1737 int sqlite3_bind_double(sqlite3_stmt
*pStmt
, int i
, double rValue
){
1739 Vdbe
*p
= (Vdbe
*)pStmt
;
1740 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1741 if( rc
==SQLITE_OK
){
1742 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
1743 sqlite3VdbeMemSetDouble(&p
->aVar
[i
-1], rValue
);
1744 sqlite3_mutex_leave(p
->db
->mutex
);
1748 int sqlite3_bind_int(sqlite3_stmt
*p
, int i
, int iValue
){
1749 return sqlite3_bind_int64(p
, i
, (i64
)iValue
);
1751 int sqlite3_bind_int64(sqlite3_stmt
*pStmt
, int i
, sqlite_int64 iValue
){
1753 Vdbe
*p
= (Vdbe
*)pStmt
;
1754 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1755 if( rc
==SQLITE_OK
){
1756 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
1757 sqlite3VdbeMemSetInt64(&p
->aVar
[i
-1], iValue
);
1758 sqlite3_mutex_leave(p
->db
->mutex
);
1762 int sqlite3_bind_null(sqlite3_stmt
*pStmt
, int i
){
1764 Vdbe
*p
= (Vdbe
*)pStmt
;
1765 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1766 if( rc
==SQLITE_OK
){
1767 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
1768 sqlite3_mutex_leave(p
->db
->mutex
);
1772 int sqlite3_bind_pointer(
1773 sqlite3_stmt
*pStmt
,
1776 const char *zPTtype
,
1777 void (*xDestructor
)(void*)
1780 Vdbe
*p
= (Vdbe
*)pStmt
;
1781 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1782 if( rc
==SQLITE_OK
){
1783 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
1784 sqlite3VdbeMemSetPointer(&p
->aVar
[i
-1], pPtr
, zPTtype
, xDestructor
);
1785 sqlite3_mutex_leave(p
->db
->mutex
);
1786 }else if( xDestructor
){
1791 int sqlite3_bind_text(
1792 sqlite3_stmt
*pStmt
,
1798 return bindText(pStmt
, i
, zData
, nData
, xDel
, SQLITE_UTF8
);
1800 int sqlite3_bind_text64(
1801 sqlite3_stmt
*pStmt
,
1804 sqlite3_uint64 nData
,
1805 void (*xDel
)(void*),
1808 assert( xDel
!=SQLITE_DYNAMIC
);
1809 if( enc
!=SQLITE_UTF8
){
1810 if( enc
==SQLITE_UTF16
) enc
= SQLITE_UTF16NATIVE
;
1813 return bindText(pStmt
, i
, zData
, nData
, xDel
, enc
);
1815 #ifndef SQLITE_OMIT_UTF16
1816 int sqlite3_bind_text16(
1817 sqlite3_stmt
*pStmt
,
1823 return bindText(pStmt
, i
, zData
, n
& ~(u64
)1, xDel
, SQLITE_UTF16NATIVE
);
1825 #endif /* SQLITE_OMIT_UTF16 */
1826 int sqlite3_bind_value(sqlite3_stmt
*pStmt
, int i
, const sqlite3_value
*pValue
){
1828 switch( sqlite3_value_type((sqlite3_value
*)pValue
) ){
1829 case SQLITE_INTEGER
: {
1830 rc
= sqlite3_bind_int64(pStmt
, i
, pValue
->u
.i
);
1833 case SQLITE_FLOAT
: {
1834 assert( pValue
->flags
& (MEM_Real
|MEM_IntReal
) );
1835 rc
= sqlite3_bind_double(pStmt
, i
,
1836 (pValue
->flags
& MEM_Real
) ? pValue
->u
.r
: (double)pValue
->u
.i
1841 if( pValue
->flags
& MEM_Zero
){
1842 rc
= sqlite3_bind_zeroblob(pStmt
, i
, pValue
->u
.nZero
);
1844 rc
= sqlite3_bind_blob(pStmt
, i
, pValue
->z
, pValue
->n
,SQLITE_TRANSIENT
);
1849 rc
= bindText(pStmt
,i
, pValue
->z
, pValue
->n
, SQLITE_TRANSIENT
,
1854 rc
= sqlite3_bind_null(pStmt
, i
);
1860 int sqlite3_bind_zeroblob(sqlite3_stmt
*pStmt
, int i
, int n
){
1862 Vdbe
*p
= (Vdbe
*)pStmt
;
1863 rc
= vdbeUnbind(p
, (u32
)(i
-1));
1864 if( rc
==SQLITE_OK
){
1865 assert( p
!=0 && p
->aVar
!=0 && i
>0 && i
<=p
->nVar
); /* tag-20240917-01 */
1866 #ifndef SQLITE_OMIT_INCRBLOB
1867 sqlite3VdbeMemSetZeroBlob(&p
->aVar
[i
-1], n
);
1869 rc
= sqlite3VdbeMemSetZeroBlob(&p
->aVar
[i
-1], n
);
1871 sqlite3_mutex_leave(p
->db
->mutex
);
1875 int sqlite3_bind_zeroblob64(sqlite3_stmt
*pStmt
, int i
, sqlite3_uint64 n
){
1877 Vdbe
*p
= (Vdbe
*)pStmt
;
1878 #ifdef SQLITE_ENABLE_API_ARMOR
1879 if( p
==0 ) return SQLITE_MISUSE_BKPT
;
1881 sqlite3_mutex_enter(p
->db
->mutex
);
1882 if( n
>(u64
)p
->db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1885 assert( (n
& 0x7FFFFFFF)==n
);
1886 rc
= sqlite3_bind_zeroblob(pStmt
, i
, n
);
1888 rc
= sqlite3ApiExit(p
->db
, rc
);
1889 sqlite3_mutex_leave(p
->db
->mutex
);
1894 ** Return the number of wildcards that can be potentially bound to.
1895 ** This routine is added to support DBD::SQLite.
1897 int sqlite3_bind_parameter_count(sqlite3_stmt
*pStmt
){
1898 Vdbe
*p
= (Vdbe
*)pStmt
;
1899 return p
? p
->nVar
: 0;
1903 ** Return the name of a wildcard parameter. Return NULL if the index
1904 ** is out of range or if the wildcard is unnamed.
1906 ** The result is always UTF-8.
1908 const char *sqlite3_bind_parameter_name(sqlite3_stmt
*pStmt
, int i
){
1909 Vdbe
*p
= (Vdbe
*)pStmt
;
1910 if( p
==0 ) return 0;
1911 return sqlite3VListNumToName(p
->pVList
, i
);
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,
1919 int sqlite3VdbeParameterIndex(Vdbe
*p
, const char *zName
, int nName
){
1920 if( p
==0 || zName
==0 ) return 0;
1921 return sqlite3VListNameToNum(p
->pVList
, zName
, nName
);
1923 int sqlite3_bind_parameter_index(sqlite3_stmt
*pStmt
, const char *zName
){
1924 return sqlite3VdbeParameterIndex((Vdbe
*)pStmt
, zName
, sqlite3Strlen30(zName
));
1928 ** Transfer all bindings from the first statement over to the second.
1930 int sqlite3TransferBindings(sqlite3_stmt
*pFromStmt
, sqlite3_stmt
*pToStmt
){
1931 Vdbe
*pFrom
= (Vdbe
*)pFromStmt
;
1932 Vdbe
*pTo
= (Vdbe
*)pToStmt
;
1934 assert( pTo
->db
==pFrom
->db
);
1935 assert( pTo
->nVar
==pFrom
->nVar
);
1936 sqlite3_mutex_enter(pTo
->db
->mutex
);
1937 for(i
=0; i
<pFrom
->nVar
; i
++){
1938 sqlite3VdbeMemMove(&pTo
->aVar
[i
], &pFrom
->aVar
[i
]);
1940 sqlite3_mutex_leave(pTo
->db
->mutex
);
1944 #ifndef SQLITE_OMIT_DEPRECATED
1946 ** Deprecated external interface. Internal/core SQLite code
1947 ** should call sqlite3TransferBindings.
1949 ** It is misuse to call this routine with statements from different
1950 ** database connections. But as this is a deprecated interface, we
1951 ** will not bother to check for that condition.
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.
1957 int sqlite3_transfer_bindings(sqlite3_stmt
*pFromStmt
, sqlite3_stmt
*pToStmt
){
1958 Vdbe
*pFrom
= (Vdbe
*)pFromStmt
;
1959 Vdbe
*pTo
= (Vdbe
*)pToStmt
;
1960 if( pFrom
->nVar
!=pTo
->nVar
){
1961 return SQLITE_ERROR
;
1963 assert( (pTo
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 || pTo
->expmask
==0 );
1967 assert( (pFrom
->prepFlags
& SQLITE_PREPARE_SAVESQL
)!=0 || pFrom
->expmask
==0 );
1968 if( pFrom
->expmask
){
1971 return sqlite3TransferBindings(pFromStmt
, pToStmt
);
1976 ** 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.
1981 sqlite3
*sqlite3_db_handle(sqlite3_stmt
*pStmt
){
1982 return pStmt
? ((Vdbe
*)pStmt
)->db
: 0;
1986 ** Return true if the prepared statement is guaranteed to not modify the
1989 int sqlite3_stmt_readonly(sqlite3_stmt
*pStmt
){
1990 return pStmt
? ((Vdbe
*)pStmt
)->readOnly
: 1;
1994 ** Return 1 if the statement is an EXPLAIN and return 2 if the
1995 ** statement is an EXPLAIN QUERY PLAN
1997 int sqlite3_stmt_isexplain(sqlite3_stmt
*pStmt
){
1998 return pStmt
? ((Vdbe
*)pStmt
)->explain
: 0;
2002 ** Set the explain mode for a statement.
2004 int sqlite3_stmt_explain(sqlite3_stmt
*pStmt
, int eMode
){
2005 Vdbe
*v
= (Vdbe
*)pStmt
;
2007 #ifdef SQLITE_ENABLE_API_ARMOR
2008 if( pStmt
==0 ) return SQLITE_MISUSE_BKPT
;
2010 sqlite3_mutex_enter(v
->db
->mutex
);
2011 if( ((int)v
->explain
)==eMode
){
2013 }else if( eMode
<0 || eMode
>2 ){
2015 }else if( (v
->prepFlags
& SQLITE_PREPARE_SAVESQL
)==0 ){
2017 }else if( v
->eVdbeState
!=VDBE_READY_STATE
){
2019 }else if( v
->nMem
>=10 && (eMode
!=2 || v
->haveEqpOps
) ){
2020 /* No reprepare necessary */
2025 rc
= sqlite3Reprepare(v
);
2026 v
->haveEqpOps
= eMode
==2;
2029 v
->nResColumn
= 12 - 4*v
->explain
;
2031 v
->nResColumn
= v
->nResAlloc
;
2033 sqlite3_mutex_leave(v
->db
->mutex
);
2038 ** Return true if the prepared statement is in need of being reset.
2040 int sqlite3_stmt_busy(sqlite3_stmt
*pStmt
){
2041 Vdbe
*v
= (Vdbe
*)pStmt
;
2042 return v
!=0 && v
->eVdbeState
==VDBE_RUN_STATE
;
2046 ** 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
2051 sqlite3_stmt
*sqlite3_next_stmt(sqlite3
*pDb
, sqlite3_stmt
*pStmt
){
2052 sqlite3_stmt
*pNext
;
2053 #ifdef SQLITE_ENABLE_API_ARMOR
2054 if( !sqlite3SafetyCheckOk(pDb
) ){
2055 (void)SQLITE_MISUSE_BKPT
;
2059 sqlite3_mutex_enter(pDb
->mutex
);
2061 pNext
= (sqlite3_stmt
*)pDb
->pVdbe
;
2063 pNext
= (sqlite3_stmt
*)((Vdbe
*)pStmt
)->pVNext
;
2065 sqlite3_mutex_leave(pDb
->mutex
);
2070 ** Return the value of a status counter for a prepared statement
2072 int sqlite3_stmt_status(sqlite3_stmt
*pStmt
, int op
, int resetFlag
){
2073 Vdbe
*pVdbe
= (Vdbe
*)pStmt
;
2075 #ifdef SQLITE_ENABLE_API_ARMOR
2077 || (op
!=SQLITE_STMTSTATUS_MEMUSED
&& (op
<0||op
>=ArraySize(pVdbe
->aCounter
)))
2079 (void)SQLITE_MISUSE_BKPT
;
2083 if( op
==SQLITE_STMTSTATUS_MEMUSED
){
2084 sqlite3
*db
= pVdbe
->db
;
2085 sqlite3_mutex_enter(db
->mutex
);
2087 db
->pnBytesFreed
= (int*)&v
;
2088 assert( db
->lookaside
.pEnd
==db
->lookaside
.pTrueEnd
);
2089 db
->lookaside
.pEnd
= db
->lookaside
.pStart
;
2090 sqlite3VdbeDelete(pVdbe
);
2091 db
->pnBytesFreed
= 0;
2092 db
->lookaside
.pEnd
= db
->lookaside
.pTrueEnd
;
2093 sqlite3_mutex_leave(db
->mutex
);
2095 v
= pVdbe
->aCounter
[op
];
2096 if( resetFlag
) pVdbe
->aCounter
[op
] = 0;
2102 ** Return the SQL associated with a prepared statement
2104 const char *sqlite3_sql(sqlite3_stmt
*pStmt
){
2105 Vdbe
*p
= (Vdbe
*)pStmt
;
2106 return p
? p
->zSql
: 0;
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().
2115 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
2116 ** expanded bound parameters.
2118 char *sqlite3_expanded_sql(sqlite3_stmt
*pStmt
){
2119 #ifdef SQLITE_OMIT_TRACE
2123 const char *zSql
= sqlite3_sql(pStmt
);
2125 Vdbe
*p
= (Vdbe
*)pStmt
;
2126 sqlite3_mutex_enter(p
->db
->mutex
);
2127 z
= sqlite3VdbeExpandSql(p
, zSql
);
2128 sqlite3_mutex_leave(p
->db
->mutex
);
2134 #ifdef SQLITE_ENABLE_NORMALIZE
2136 ** Return the normalized SQL associated with a prepared statement.
2138 const char *sqlite3_normalized_sql(sqlite3_stmt
*pStmt
){
2139 Vdbe
*p
= (Vdbe
*)pStmt
;
2140 if( p
==0 ) return 0;
2141 if( p
->zNormSql
==0 && ALWAYS(p
->zSql
!=0) ){
2142 sqlite3_mutex_enter(p
->db
->mutex
);
2143 p
->zNormSql
= sqlite3Normalize(p
, p
->zSql
);
2144 sqlite3_mutex_leave(p
->db
->mutex
);
2148 #endif /* SQLITE_ENABLE_NORMALIZE */
2150 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2152 ** 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.
2156 static UnpackedRecord
*vdbeUnpackRecord(
2161 UnpackedRecord
*pRet
; /* Return value */
2163 pRet
= sqlite3VdbeAllocUnpackedRecord(pKeyInfo
);
2165 memset(pRet
->aMem
, 0, sizeof(Mem
)*(pKeyInfo
->nKeyField
+1));
2166 sqlite3VdbeRecordUnpack(pKeyInfo
, nKey
, pKey
, pRet
);
2172 ** This function is called from within a pre-update callback to retrieve
2173 ** a field of the row currently being updated or deleted.
2175 int sqlite3_preupdate_old(sqlite3
*db
, int iIdx
, sqlite3_value
**ppValue
){
2180 #ifdef SQLITE_ENABLE_API_ARMOR
2181 if( db
==0 || ppValue
==0 ){
2182 return SQLITE_MISUSE_BKPT
;
2186 /* Test that this call is being made from within an SQLITE_DELETE or
2187 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
2188 if( !p
|| p
->op
==SQLITE_INSERT
){
2189 rc
= SQLITE_MISUSE_BKPT
;
2190 goto preupdate_old_out
;
2193 iIdx
= sqlite3TableColumnToIndex(p
->pPk
, iIdx
);
2195 if( iIdx
>=p
->pCsr
->nField
|| iIdx
<0 ){
2197 goto preupdate_old_out
;
2200 if( iIdx
==p
->pTab
->iPKey
){
2201 *ppValue
= pMem
= &p
->oldipk
;
2202 sqlite3VdbeMemSetInt64(pMem
, p
->iKey1
);
2205 /* If the old.* record has not yet been loaded into memory, do so now. */
2206 if( p
->pUnpacked
==0 ){
2210 assert( p
->pCsr
->eCurType
==CURTYPE_BTREE
);
2211 nRec
= sqlite3BtreePayloadSize(p
->pCsr
->uc
.pCursor
);
2212 aRec
= sqlite3DbMallocRaw(db
, nRec
);
2213 if( !aRec
) goto preupdate_old_out
;
2214 rc
= sqlite3BtreePayload(p
->pCsr
->uc
.pCursor
, 0, nRec
, aRec
);
2215 if( rc
==SQLITE_OK
){
2216 p
->pUnpacked
= vdbeUnpackRecord(&p
->keyinfo
, nRec
, aRec
);
2217 if( !p
->pUnpacked
) rc
= SQLITE_NOMEM
;
2219 if( rc
!=SQLITE_OK
){
2220 sqlite3DbFree(db
, aRec
);
2221 goto preupdate_old_out
;
2226 pMem
= *ppValue
= &p
->pUnpacked
->aMem
[iIdx
];
2227 if( iIdx
>=p
->pUnpacked
->nField
){
2228 /* This occurs when the table has been extended using ALTER TABLE
2229 ** ADD COLUMN. The value to return is the default value of the column. */
2230 Column
*pCol
= &p
->pTab
->aCol
[iIdx
];
2231 if( pCol
->iDflt
>0 ){
2233 int nByte
= sizeof(sqlite3_value
*)*p
->pTab
->nCol
;
2234 p
->apDflt
= (sqlite3_value
**)sqlite3DbMallocZero(db
, nByte
);
2235 if( p
->apDflt
==0 ) goto preupdate_old_out
;
2237 if( p
->apDflt
[iIdx
]==0 ){
2238 sqlite3_value
*pVal
= 0;
2240 assert( p
->pTab
!=0 && IsOrdinaryTable(p
->pTab
) );
2241 pDflt
= p
->pTab
->u
.tab
.pDfltList
->a
[pCol
->iDflt
-1].pExpr
;
2242 rc
= sqlite3ValueFromExpr(db
, pDflt
, ENC(db
), pCol
->affinity
, &pVal
);
2243 if( rc
==SQLITE_OK
&& pVal
==0 ){
2244 rc
= SQLITE_CORRUPT_BKPT
;
2246 p
->apDflt
[iIdx
] = pVal
;
2248 *ppValue
= p
->apDflt
[iIdx
];
2250 *ppValue
= (sqlite3_value
*)columnNullValue();
2252 }else if( p
->pTab
->aCol
[iIdx
].affinity
==SQLITE_AFF_REAL
){
2253 if( pMem
->flags
& (MEM_Int
|MEM_IntReal
) ){
2254 testcase( pMem
->flags
& MEM_Int
);
2255 testcase( pMem
->flags
& MEM_IntReal
);
2256 sqlite3VdbeMemRealify(pMem
);
2262 sqlite3Error(db
, rc
);
2263 return sqlite3ApiExit(db
, rc
);
2265 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2267 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2269 ** This function is called from within a pre-update callback to retrieve
2270 ** the number of columns in the row being updated, deleted or inserted.
2272 int sqlite3_preupdate_count(sqlite3
*db
){
2274 #ifdef SQLITE_ENABLE_API_ARMOR
2275 p
= db
!=0 ? db
->pPreUpdate
: 0;
2279 return (p
? p
->keyinfo
.nKeyField
: 0);
2281 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2283 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2285 ** This function is designed to be called from within a pre-update callback
2286 ** only. It returns zero if the change that caused the callback was made
2287 ** immediately by a user SQL statement. Or, if the change was made by a
2288 ** trigger program, it returns the number of trigger programs currently
2289 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
2290 ** top-level trigger etc.).
2292 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
2293 ** or SET DEFAULT action is considered a trigger.
2295 int sqlite3_preupdate_depth(sqlite3
*db
){
2297 #ifdef SQLITE_ENABLE_API_ARMOR
2298 p
= db
!=0 ? db
->pPreUpdate
: 0;
2302 return (p
? p
->v
->nFrame
: 0);
2304 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2306 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2308 ** This function is designed to be called from within a pre-update callback
2311 int sqlite3_preupdate_blobwrite(sqlite3
*db
){
2313 #ifdef SQLITE_ENABLE_API_ARMOR
2314 p
= db
!=0 ? db
->pPreUpdate
: 0;
2318 return (p
? p
->iBlobWrite
: -1);
2322 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2324 ** This function is called from within a pre-update callback to retrieve
2325 ** a field of the row currently being updated or inserted.
2327 int sqlite3_preupdate_new(sqlite3
*db
, int iIdx
, sqlite3_value
**ppValue
){
2332 #ifdef SQLITE_ENABLE_API_ARMOR
2333 if( db
==0 || ppValue
==0 ){
2334 return SQLITE_MISUSE_BKPT
;
2338 if( !p
|| p
->op
==SQLITE_DELETE
){
2339 rc
= SQLITE_MISUSE_BKPT
;
2340 goto preupdate_new_out
;
2342 if( p
->pPk
&& p
->op
!=SQLITE_UPDATE
){
2343 iIdx
= sqlite3TableColumnToIndex(p
->pPk
, iIdx
);
2345 if( iIdx
>=p
->pCsr
->nField
|| iIdx
<0 ){
2347 goto preupdate_new_out
;
2350 if( p
->op
==SQLITE_INSERT
){
2351 /* For an INSERT, memory cell p->iNewReg contains the serialized record
2352 ** that is being inserted. Deserialize it. */
2353 UnpackedRecord
*pUnpack
= p
->pNewUnpacked
;
2355 Mem
*pData
= &p
->v
->aMem
[p
->iNewReg
];
2356 rc
= ExpandBlob(pData
);
2357 if( rc
!=SQLITE_OK
) goto preupdate_new_out
;
2358 pUnpack
= vdbeUnpackRecord(&p
->keyinfo
, pData
->n
, pData
->z
);
2361 goto preupdate_new_out
;
2363 p
->pNewUnpacked
= pUnpack
;
2365 pMem
= &pUnpack
->aMem
[iIdx
];
2366 if( iIdx
==p
->pTab
->iPKey
){
2367 sqlite3VdbeMemSetInt64(pMem
, p
->iKey2
);
2368 }else if( iIdx
>=pUnpack
->nField
){
2369 pMem
= (sqlite3_value
*)columnNullValue();
2372 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
2373 ** value. Make a copy of the cell contents and return a pointer to it.
2374 ** It is not safe to return a pointer to the memory cell itself as the
2375 ** caller may modify the value text encoding.
2377 assert( p
->op
==SQLITE_UPDATE
);
2379 p
->aNew
= (Mem
*)sqlite3DbMallocZero(db
, sizeof(Mem
) * p
->pCsr
->nField
);
2382 goto preupdate_new_out
;
2385 assert( iIdx
>=0 && iIdx
<p
->pCsr
->nField
);
2386 pMem
= &p
->aNew
[iIdx
];
2387 if( pMem
->flags
==0 ){
2388 if( iIdx
==p
->pTab
->iPKey
){
2389 sqlite3VdbeMemSetInt64(pMem
, p
->iKey2
);
2391 rc
= sqlite3VdbeMemCopy(pMem
, &p
->v
->aMem
[p
->iNewReg
+1+iIdx
]);
2392 if( rc
!=SQLITE_OK
) goto preupdate_new_out
;
2399 sqlite3Error(db
, rc
);
2400 return sqlite3ApiExit(db
, rc
);
2402 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2404 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2406 ** Return status data for a single loop within query pStmt.
2408 int sqlite3_stmt_scanstatus_v2(
2409 sqlite3_stmt
*pStmt
, /* Prepared statement being queried */
2410 int iScan
, /* Index of loop to report on */
2411 int iScanStatusOp
, /* Which metric to return */
2413 void *pOut
/* OUT: Write the answer here */
2415 Vdbe
*p
= (Vdbe
*)pStmt
;
2418 ScanStatus
*pScan
= 0;
2421 #ifdef SQLITE_ENABLE_API_ARMOR
2423 || iScanStatusOp
<SQLITE_SCANSTAT_NLOOP
2424 || iScanStatusOp
>SQLITE_SCANSTAT_NCYCLE
){
2432 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
2439 if( iScanStatusOp
==SQLITE_SCANSTAT_NCYCLE
){
2441 for(ii
=0; ii
<nOp
; ii
++){
2442 res
+= aOp
[ii
].nCycle
;
2449 if( flags
& SQLITE_SCANSTAT_COMPLEX
){
2452 /* If the COMPLEX flag is clear, then this function must ignore any
2453 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
2454 for(idx
=0; idx
<p
->nScan
; idx
++){
2455 pScan
= &p
->aScan
[idx
];
2458 if( iScan
<0 ) break;
2462 if( idx
>=p
->nScan
) return 1;
2463 assert( pScan
==0 || pScan
==&p
->aScan
[idx
] );
2464 pScan
= &p
->aScan
[idx
];
2466 switch( iScanStatusOp
){
2467 case SQLITE_SCANSTAT_NLOOP
: {
2468 if( pScan
->addrLoop
>0 ){
2469 *(sqlite3_int64
*)pOut
= aOp
[pScan
->addrLoop
].nExec
;
2471 *(sqlite3_int64
*)pOut
= -1;
2475 case SQLITE_SCANSTAT_NVISIT
: {
2476 if( pScan
->addrVisit
>0 ){
2477 *(sqlite3_int64
*)pOut
= aOp
[pScan
->addrVisit
].nExec
;
2479 *(sqlite3_int64
*)pOut
= -1;
2483 case SQLITE_SCANSTAT_EST
: {
2485 LogEst x
= pScan
->nEst
;
2490 *(double*)pOut
= r
*sqlite3LogEstToInt(x
);
2493 case SQLITE_SCANSTAT_NAME
: {
2494 *(const char**)pOut
= pScan
->zName
;
2497 case SQLITE_SCANSTAT_EXPLAIN
: {
2498 if( pScan
->addrExplain
){
2499 *(const char**)pOut
= aOp
[ pScan
->addrExplain
].p4
.z
;
2501 *(const char**)pOut
= 0;
2505 case SQLITE_SCANSTAT_SELECTID
: {
2506 if( pScan
->addrExplain
){
2507 *(int*)pOut
= aOp
[ pScan
->addrExplain
].p1
;
2513 case SQLITE_SCANSTAT_PARENTID
: {
2514 if( pScan
->addrExplain
){
2515 *(int*)pOut
= aOp
[ pScan
->addrExplain
].p2
;
2521 case SQLITE_SCANSTAT_NCYCLE
: {
2523 if( pScan
->aAddrRange
[0]==0 ){
2527 for(ii
=0; ii
<ArraySize(pScan
->aAddrRange
); ii
+=2){
2528 int iIns
= pScan
->aAddrRange
[ii
];
2529 int iEnd
= pScan
->aAddrRange
[ii
+1];
2530 if( iIns
==0 ) break;
2532 while( iIns
<=iEnd
){
2533 res
+= aOp
[iIns
].nCycle
;
2538 for(iOp
=0; iOp
<nOp
; iOp
++){
2539 Op
*pOp
= &aOp
[iOp
];
2540 if( pOp
->p1
!=iEnd
) continue;
2541 if( (sqlite3OpcodeProperty
[pOp
->opcode
] & OPFLG_NCYCLE
)==0 ){
2544 res
+= aOp
[iOp
].nCycle
;
2560 ** Return status data for a single loop within query pStmt.
2562 int sqlite3_stmt_scanstatus(
2563 sqlite3_stmt
*pStmt
, /* Prepared statement being queried */
2564 int iScan
, /* Index of loop to report on */
2565 int iScanStatusOp
, /* Which metric to return */
2566 void *pOut
/* OUT: Write the answer here */
2568 return sqlite3_stmt_scanstatus_v2(pStmt
, iScan
, iScanStatusOp
, 0, pOut
);
2572 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
2574 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt
*pStmt
){
2575 Vdbe
*p
= (Vdbe
*)pStmt
;
2577 for(ii
=0; p
!=0 && ii
<p
->nOp
; ii
++){
2578 Op
*pOp
= &p
->aOp
[ii
];
2583 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */