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"
19 #ifndef SQLITE_OMIT_DEPRECATED
21 ** Return TRUE (non-zero) of the statement supplied as an argument needs
22 ** to be recompiled. A statement needs to be recompiled whenever the
23 ** execution environment changes in a way that would alter the program
24 ** that sqlite3_prepare() generates. For example, if new functions or
25 ** collating sequences are registered or if an authorizer function is
28 int sqlite3_expired(sqlite3_stmt
*pStmt
){
29 Vdbe
*p
= (Vdbe
*)pStmt
;
30 return p
==0 || p
->expired
;
35 ** Check on a Vdbe to make sure it has not been finalized. Log
36 ** an error and return true if it has been finalized (or is otherwise
37 ** invalid). Return false if it is ok.
39 static int vdbeSafety(Vdbe
*p
){
41 sqlite3_log(SQLITE_MISUSE
, "API called with finalized prepared statement");
47 static int vdbeSafetyNotNull(Vdbe
*p
){
49 sqlite3_log(SQLITE_MISUSE
, "API called with NULL prepared statement");
57 ** The following routine destroys a virtual machine that is created by
58 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
59 ** success/failure code that describes the result of executing the virtual
62 ** This routine sets the error code and string returned by
63 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
65 int sqlite3_finalize(sqlite3_stmt
*pStmt
){
68 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
69 ** pointer is a harmless no-op. */
72 Vdbe
*v
= (Vdbe
*)pStmt
;
74 if( vdbeSafety(v
) ) return SQLITE_MISUSE_BKPT
;
75 sqlite3_mutex_enter(db
->mutex
);
76 rc
= sqlite3VdbeFinalize(v
);
77 rc
= sqlite3ApiExit(db
, rc
);
78 sqlite3LeaveMutexAndCloseZombie(db
);
84 ** Terminate the current execution of an SQL statement and reset it
85 ** back to its starting state so that it can be reused. A success code from
86 ** the prior execution is returned.
88 ** This routine sets the error code and string returned by
89 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
91 int sqlite3_reset(sqlite3_stmt
*pStmt
){
96 Vdbe
*v
= (Vdbe
*)pStmt
;
97 sqlite3_mutex_enter(v
->db
->mutex
);
98 rc
= sqlite3VdbeReset(v
);
100 assert( (rc
& (v
->db
->errMask
))==rc
);
101 rc
= sqlite3ApiExit(v
->db
, rc
);
102 sqlite3_mutex_leave(v
->db
->mutex
);
108 ** Set all the parameters in the compiled SQL statement to NULL.
110 int sqlite3_clear_bindings(sqlite3_stmt
*pStmt
){
113 Vdbe
*p
= (Vdbe
*)pStmt
;
114 #if SQLITE_THREADSAFE
115 sqlite3_mutex
*mutex
= ((Vdbe
*)pStmt
)->db
->mutex
;
117 sqlite3_mutex_enter(mutex
);
118 for(i
=0; i
<p
->nVar
; i
++){
119 sqlite3VdbeMemRelease(&p
->aVar
[i
]);
120 p
->aVar
[i
].flags
= MEM_Null
;
122 if( p
->isPrepareV2
&& p
->expmask
){
125 sqlite3_mutex_leave(mutex
);
130 /**************************** sqlite3_value_ *******************************
131 ** The following routines extract information from a Mem or sqlite3_value
134 const void *sqlite3_value_blob(sqlite3_value
*pVal
){
136 if( p
->flags
& (MEM_Blob
|MEM_Str
) ){
137 sqlite3VdbeMemExpandBlob(p
);
138 p
->flags
|= MEM_Blob
;
139 return p
->n
? p
->z
: 0;
141 return sqlite3_value_text(pVal
);
144 int sqlite3_value_bytes(sqlite3_value
*pVal
){
145 return sqlite3ValueBytes(pVal
, SQLITE_UTF8
);
147 int sqlite3_value_bytes16(sqlite3_value
*pVal
){
148 return sqlite3ValueBytes(pVal
, SQLITE_UTF16NATIVE
);
150 double sqlite3_value_double(sqlite3_value
*pVal
){
151 return sqlite3VdbeRealValue((Mem
*)pVal
);
153 int sqlite3_value_int(sqlite3_value
*pVal
){
154 return (int)sqlite3VdbeIntValue((Mem
*)pVal
);
156 sqlite_int64
sqlite3_value_int64(sqlite3_value
*pVal
){
157 return sqlite3VdbeIntValue((Mem
*)pVal
);
159 const unsigned char *sqlite3_value_text(sqlite3_value
*pVal
){
160 return (const unsigned char *)sqlite3ValueText(pVal
, SQLITE_UTF8
);
162 #ifndef SQLITE_OMIT_UTF16
163 const void *sqlite3_value_text16(sqlite3_value
* pVal
){
164 return sqlite3ValueText(pVal
, SQLITE_UTF16NATIVE
);
166 const void *sqlite3_value_text16be(sqlite3_value
*pVal
){
167 return sqlite3ValueText(pVal
, SQLITE_UTF16BE
);
169 const void *sqlite3_value_text16le(sqlite3_value
*pVal
){
170 return sqlite3ValueText(pVal
, SQLITE_UTF16LE
);
172 #endif /* SQLITE_OMIT_UTF16 */
173 int sqlite3_value_type(sqlite3_value
* pVal
){
174 static const u8 aType
[] = {
175 SQLITE_BLOB
, /* 0x00 */
176 SQLITE_NULL
, /* 0x01 */
177 SQLITE_TEXT
, /* 0x02 */
178 SQLITE_NULL
, /* 0x03 */
179 SQLITE_INTEGER
, /* 0x04 */
180 SQLITE_NULL
, /* 0x05 */
181 SQLITE_INTEGER
, /* 0x06 */
182 SQLITE_NULL
, /* 0x07 */
183 SQLITE_FLOAT
, /* 0x08 */
184 SQLITE_NULL
, /* 0x09 */
185 SQLITE_FLOAT
, /* 0x0a */
186 SQLITE_NULL
, /* 0x0b */
187 SQLITE_INTEGER
, /* 0x0c */
188 SQLITE_NULL
, /* 0x0d */
189 SQLITE_INTEGER
, /* 0x0e */
190 SQLITE_NULL
, /* 0x0f */
191 SQLITE_BLOB
, /* 0x10 */
192 SQLITE_NULL
, /* 0x11 */
193 SQLITE_TEXT
, /* 0x12 */
194 SQLITE_NULL
, /* 0x13 */
195 SQLITE_INTEGER
, /* 0x14 */
196 SQLITE_NULL
, /* 0x15 */
197 SQLITE_INTEGER
, /* 0x16 */
198 SQLITE_NULL
, /* 0x17 */
199 SQLITE_FLOAT
, /* 0x18 */
200 SQLITE_NULL
, /* 0x19 */
201 SQLITE_FLOAT
, /* 0x1a */
202 SQLITE_NULL
, /* 0x1b */
203 SQLITE_INTEGER
, /* 0x1c */
204 SQLITE_NULL
, /* 0x1d */
205 SQLITE_INTEGER
, /* 0x1e */
206 SQLITE_NULL
, /* 0x1f */
208 return aType
[pVal
->flags
&MEM_AffMask
];
211 /**************************** sqlite3_result_ *******************************
212 ** The following routines are used by user-defined functions to specify
213 ** the function result.
215 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
216 ** result as a string or blob but if the string or blob is too large, it
217 ** then sets the error code to SQLITE_TOOBIG
219 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
220 ** on value P is not going to be used and need to be destroyed.
222 static void setResultStrOrError(
223 sqlite3_context
*pCtx
, /* Function context */
224 const char *z
, /* String pointer */
225 int n
, /* Bytes in string, or negative */
226 u8 enc
, /* Encoding of z. 0 for BLOBs */
227 void (*xDel
)(void*) /* Destructor function */
229 if( sqlite3VdbeMemSetStr(pCtx
->pOut
, z
, n
, enc
, xDel
)==SQLITE_TOOBIG
){
230 sqlite3_result_error_toobig(pCtx
);
233 static int invokeValueDestructor(
234 const void *p
, /* Value to destroy */
235 void (*xDel
)(void*), /* The destructor */
236 sqlite3_context
*pCtx
/* Set a SQLITE_TOOBIG error if no NULL */
238 assert( xDel
!=SQLITE_DYNAMIC
);
241 }else if( xDel
==SQLITE_TRANSIENT
){
246 if( pCtx
) sqlite3_result_error_toobig(pCtx
);
247 return SQLITE_TOOBIG
;
249 void sqlite3_result_blob(
250 sqlite3_context
*pCtx
,
256 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
257 setResultStrOrError(pCtx
, z
, n
, 0, xDel
);
259 void sqlite3_result_blob64(
260 sqlite3_context
*pCtx
,
265 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
266 assert( xDel
!=SQLITE_DYNAMIC
);
268 (void)invokeValueDestructor(z
, xDel
, pCtx
);
270 setResultStrOrError(pCtx
, z
, (int)n
, 0, xDel
);
273 void sqlite3_result_double(sqlite3_context
*pCtx
, double rVal
){
274 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
275 sqlite3VdbeMemSetDouble(pCtx
->pOut
, rVal
);
277 void sqlite3_result_error(sqlite3_context
*pCtx
, const char *z
, int n
){
278 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
279 pCtx
->isError
= SQLITE_ERROR
;
280 pCtx
->fErrorOrAux
= 1;
281 sqlite3VdbeMemSetStr(pCtx
->pOut
, z
, n
, SQLITE_UTF8
, SQLITE_TRANSIENT
);
283 #ifndef SQLITE_OMIT_UTF16
284 void sqlite3_result_error16(sqlite3_context
*pCtx
, const void *z
, int n
){
285 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
286 pCtx
->isError
= SQLITE_ERROR
;
287 pCtx
->fErrorOrAux
= 1;
288 sqlite3VdbeMemSetStr(pCtx
->pOut
, z
, n
, SQLITE_UTF16NATIVE
, SQLITE_TRANSIENT
);
291 void sqlite3_result_int(sqlite3_context
*pCtx
, int iVal
){
292 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
293 sqlite3VdbeMemSetInt64(pCtx
->pOut
, (i64
)iVal
);
295 void sqlite3_result_int64(sqlite3_context
*pCtx
, i64 iVal
){
296 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
297 sqlite3VdbeMemSetInt64(pCtx
->pOut
, iVal
);
299 void sqlite3_result_null(sqlite3_context
*pCtx
){
300 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
301 sqlite3VdbeMemSetNull(pCtx
->pOut
);
303 void sqlite3_result_text(
304 sqlite3_context
*pCtx
,
309 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
310 setResultStrOrError(pCtx
, z
, n
, SQLITE_UTF8
, xDel
);
312 void sqlite3_result_text64(
313 sqlite3_context
*pCtx
,
316 void (*xDel
)(void *),
319 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
320 assert( xDel
!=SQLITE_DYNAMIC
);
321 if( enc
==SQLITE_UTF16
) enc
= SQLITE_UTF16NATIVE
;
323 (void)invokeValueDestructor(z
, xDel
, pCtx
);
325 setResultStrOrError(pCtx
, z
, (int)n
, enc
, xDel
);
328 #ifndef SQLITE_OMIT_UTF16
329 void sqlite3_result_text16(
330 sqlite3_context
*pCtx
,
335 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
336 setResultStrOrError(pCtx
, z
, n
, SQLITE_UTF16NATIVE
, xDel
);
338 void sqlite3_result_text16be(
339 sqlite3_context
*pCtx
,
344 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
345 setResultStrOrError(pCtx
, z
, n
, SQLITE_UTF16BE
, xDel
);
347 void sqlite3_result_text16le(
348 sqlite3_context
*pCtx
,
353 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
354 setResultStrOrError(pCtx
, z
, n
, SQLITE_UTF16LE
, xDel
);
356 #endif /* SQLITE_OMIT_UTF16 */
357 void sqlite3_result_value(sqlite3_context
*pCtx
, sqlite3_value
*pValue
){
358 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
359 sqlite3VdbeMemCopy(pCtx
->pOut
, pValue
);
361 void sqlite3_result_zeroblob(sqlite3_context
*pCtx
, int n
){
362 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
363 sqlite3VdbeMemSetZeroBlob(pCtx
->pOut
, n
);
365 void sqlite3_result_error_code(sqlite3_context
*pCtx
, int errCode
){
366 pCtx
->isError
= errCode
;
367 pCtx
->fErrorOrAux
= 1;
368 if( pCtx
->pOut
->flags
& MEM_Null
){
369 sqlite3VdbeMemSetStr(pCtx
->pOut
, sqlite3ErrStr(errCode
), -1,
370 SQLITE_UTF8
, SQLITE_STATIC
);
374 /* Force an SQLITE_TOOBIG error. */
375 void sqlite3_result_error_toobig(sqlite3_context
*pCtx
){
376 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
377 pCtx
->isError
= SQLITE_TOOBIG
;
378 pCtx
->fErrorOrAux
= 1;
379 sqlite3VdbeMemSetStr(pCtx
->pOut
, "string or blob too big", -1,
380 SQLITE_UTF8
, SQLITE_STATIC
);
383 /* An SQLITE_NOMEM error. */
384 void sqlite3_result_error_nomem(sqlite3_context
*pCtx
){
385 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
386 sqlite3VdbeMemSetNull(pCtx
->pOut
);
387 pCtx
->isError
= SQLITE_NOMEM
;
388 pCtx
->fErrorOrAux
= 1;
389 pCtx
->pOut
->db
->mallocFailed
= 1;
393 ** This function is called after a transaction has been committed. It
394 ** invokes callbacks registered with sqlite3_wal_hook() as required.
396 static int doWalCallbacks(sqlite3
*db
){
398 #ifndef SQLITE_OMIT_WAL
400 for(i
=0; i
<db
->nDb
; i
++){
401 Btree
*pBt
= db
->aDb
[i
].pBt
;
403 int nEntry
= sqlite3PagerWalCallback(sqlite3BtreePager(pBt
));
404 if( db
->xWalCallback
&& nEntry
>0 && rc
==SQLITE_OK
){
405 rc
= db
->xWalCallback(db
->pWalArg
, db
, db
->aDb
[i
].zName
, nEntry
);
414 ** Execute the statement pStmt, either until a row of data is ready, the
415 ** statement is completely executed or an error occurs.
417 ** This routine implements the bulk of the logic behind the sqlite_step()
418 ** API. The only thing omitted is the automatic recompile if a
419 ** schema change has occurred. That detail is handled by the
420 ** outer sqlite3_step() wrapper procedure.
422 static int sqlite3Step(Vdbe
*p
){
427 if( p
->magic
!=VDBE_MAGIC_RUN
){
428 /* We used to require that sqlite3_reset() be called before retrying
429 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
430 ** with version 3.7.0, we changed this so that sqlite3_reset() would
431 ** be called automatically instead of throwing the SQLITE_MISUSE error.
432 ** This "automatic-reset" change is not technically an incompatibility,
433 ** since any application that receives an SQLITE_MISUSE is broken by
436 ** Nevertheless, some published applications that were originally written
437 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
438 ** returns, and those were broken by the automatic-reset change. As a
439 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
440 ** legacy behavior of returning SQLITE_MISUSE for cases where the
441 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
442 ** or SQLITE_BUSY error.
444 #ifdef SQLITE_OMIT_AUTORESET
445 if( p
->rc
==SQLITE_BUSY
|| p
->rc
==SQLITE_LOCKED
){
446 sqlite3_reset((sqlite3_stmt
*)p
);
448 return SQLITE_MISUSE_BKPT
;
451 sqlite3_reset((sqlite3_stmt
*)p
);
455 /* Check that malloc() has not failed. If it has, return early. */
457 if( db
->mallocFailed
){
458 p
->rc
= SQLITE_NOMEM
;
462 if( p
->pc
<=0 && p
->expired
){
463 p
->rc
= SQLITE_SCHEMA
;
468 /* If there are no other statements currently running, then
469 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
470 ** from interrupting a statement that has not yet started.
472 if( db
->nVdbeActive
==0 ){
473 db
->u1
.isInterrupted
= 0;
476 assert( db
->nVdbeWrite
>0 || db
->autoCommit
==0
477 || (db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0)
480 #ifndef SQLITE_OMIT_TRACE
481 if( db
->xProfile
&& !db
->init
.busy
){
482 sqlite3OsCurrentTimeInt64(db
->pVfs
, &p
->startTime
);
487 if( p
->readOnly
==0 ) db
->nVdbeWrite
++;
488 if( p
->bIsReader
) db
->nVdbeRead
++;
491 #ifndef SQLITE_OMIT_EXPLAIN
493 rc
= sqlite3VdbeList(p
);
495 #endif /* SQLITE_OMIT_EXPLAIN */
498 rc
= sqlite3VdbeExec(p
);
502 #ifndef SQLITE_OMIT_TRACE
503 /* Invoke the profile callback if there is one
505 if( rc
!=SQLITE_ROW
&& db
->xProfile
&& !db
->init
.busy
&& p
->zSql
){
507 sqlite3OsCurrentTimeInt64(db
->pVfs
, &iNow
);
508 db
->xProfile(db
->pProfileArg
, p
->zSql
, (iNow
- p
->startTime
)*1000000);
512 if( rc
==SQLITE_DONE
){
513 assert( p
->rc
==SQLITE_OK
);
514 p
->rc
= doWalCallbacks(db
);
515 if( p
->rc
!=SQLITE_OK
){
521 if( SQLITE_NOMEM
==sqlite3ApiExit(p
->db
, p
->rc
) ){
522 p
->rc
= SQLITE_NOMEM
;
525 /* At this point local variable rc holds the value that should be
526 ** returned if this statement was compiled using the legacy
527 ** sqlite3_prepare() interface. According to the docs, this can only
528 ** be one of the values in the first assert() below. Variable p->rc
529 ** contains the value that would be returned if sqlite3_finalize()
530 ** were called on statement p.
532 assert( rc
==SQLITE_ROW
|| rc
==SQLITE_DONE
|| rc
==SQLITE_ERROR
533 || rc
==SQLITE_BUSY
|| rc
==SQLITE_MISUSE
535 assert( p
->rc
!=SQLITE_ROW
&& p
->rc
!=SQLITE_DONE
);
536 if( p
->isPrepareV2
&& rc
!=SQLITE_ROW
&& rc
!=SQLITE_DONE
){
537 /* If this statement was prepared using sqlite3_prepare_v2(), and an
538 ** error has occurred, then return the error code in p->rc to the
539 ** caller. Set the error code in the database handle to the same value.
541 rc
= sqlite3VdbeTransferError(p
);
543 return (rc
&db
->errMask
);
547 ** This is the top-level implementation of sqlite3_step(). Call
548 ** sqlite3Step() to do most of the work. If a schema error occurs,
549 ** call sqlite3Reprepare() and try again.
551 int sqlite3_step(sqlite3_stmt
*pStmt
){
552 int rc
= SQLITE_OK
; /* Result from sqlite3Step() */
553 int rc2
= SQLITE_OK
; /* Result from sqlite3Reprepare() */
554 Vdbe
*v
= (Vdbe
*)pStmt
; /* the prepared statement */
555 int cnt
= 0; /* Counter to prevent infinite loop of reprepares */
556 sqlite3
*db
; /* The database connection */
558 if( vdbeSafetyNotNull(v
) ){
559 return SQLITE_MISUSE_BKPT
;
562 sqlite3_mutex_enter(db
->mutex
);
564 while( (rc
= sqlite3Step(v
))==SQLITE_SCHEMA
565 && cnt
++ < SQLITE_MAX_SCHEMA_RETRY
){
567 rc2
= rc
= sqlite3Reprepare(v
);
568 if( rc
!=SQLITE_OK
) break;
569 sqlite3_reset(pStmt
);
570 if( savedPc
>=0 ) v
->doingRerun
= 1;
571 assert( v
->expired
==0 );
573 if( rc2
!=SQLITE_OK
){
574 /* This case occurs after failing to recompile an sql statement.
575 ** The error message from the SQL compiler has already been loaded
576 ** into the database handle. This block copies the error message
577 ** from the database handle into the statement and sets the statement
578 ** program counter to 0 to ensure that when the statement is
579 ** finalized or reset the parser error message is available via
580 ** sqlite3_errmsg() and sqlite3_errcode().
582 const char *zErr
= (const char *)sqlite3_value_text(db
->pErr
);
583 assert( zErr
!=0 || db
->mallocFailed
);
584 sqlite3DbFree(db
, v
->zErrMsg
);
585 if( !db
->mallocFailed
){
586 v
->zErrMsg
= sqlite3DbStrDup(db
, zErr
);
590 v
->rc
= rc
= SQLITE_NOMEM
;
593 rc
= sqlite3ApiExit(db
, rc
);
594 sqlite3_mutex_leave(db
->mutex
);
600 ** Extract the user data from a sqlite3_context structure and return a
603 void *sqlite3_user_data(sqlite3_context
*p
){
604 assert( p
&& p
->pFunc
);
605 return p
->pFunc
->pUserData
;
609 ** Extract the user data from a sqlite3_context structure and return a
612 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
613 ** returns a copy of the pointer to the database connection (the 1st
614 ** parameter) of the sqlite3_create_function() and
615 ** sqlite3_create_function16() routines that originally registered the
616 ** application defined function.
618 sqlite3
*sqlite3_context_db_handle(sqlite3_context
*p
){
619 assert( p
&& p
->pFunc
);
624 ** Return the current time for a statement
626 sqlite3_int64
sqlite3StmtCurrentTime(sqlite3_context
*p
){
629 if( v
->iCurrentTime
==0 ){
630 rc
= sqlite3OsCurrentTimeInt64(p
->pOut
->db
->pVfs
, &v
->iCurrentTime
);
631 if( rc
) v
->iCurrentTime
= 0;
633 return v
->iCurrentTime
;
637 ** The following is the implementation of an SQL function that always
638 ** fails with an error message stating that the function is used in the
639 ** wrong context. The sqlite3_overload_function() API might construct
640 ** SQL function that use this routine so that the functions will exist
641 ** for name resolution but are actually overloaded by the xFindFunction
642 ** method of virtual tables.
644 void sqlite3InvalidFunction(
645 sqlite3_context
*context
, /* The function calling context */
646 int NotUsed
, /* Number of arguments to the function */
647 sqlite3_value
**NotUsed2
/* Value of each argument */
649 const char *zName
= context
->pFunc
->zName
;
651 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
652 zErr
= sqlite3_mprintf(
653 "unable to use function %s in the requested context", zName
);
654 sqlite3_result_error(context
, zErr
, -1);
659 ** Create a new aggregate context for p and return a pointer to
660 ** its pMem->z element.
662 static SQLITE_NOINLINE
void *createAggContext(sqlite3_context
*p
, int nByte
){
664 assert( (pMem
->flags
& MEM_Agg
)==0 );
666 sqlite3VdbeMemSetNull(pMem
);
669 sqlite3VdbeMemClearAndResize(pMem
, nByte
);
670 pMem
->flags
= MEM_Agg
;
671 pMem
->u
.pDef
= p
->pFunc
;
673 memset(pMem
->z
, 0, nByte
);
676 return (void*)pMem
->z
;
680 ** Allocate or return the aggregate context for a user function. A new
681 ** context is allocated on the first call. Subsequent calls return the
682 ** same context that was returned on prior calls.
684 void *sqlite3_aggregate_context(sqlite3_context
*p
, int nByte
){
685 assert( p
&& p
->pFunc
&& p
->pFunc
->xStep
);
686 assert( sqlite3_mutex_held(p
->pOut
->db
->mutex
) );
688 if( (p
->pMem
->flags
& MEM_Agg
)==0 ){
689 return createAggContext(p
, nByte
);
691 return (void*)p
->pMem
->z
;
696 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
697 ** the user-function defined by pCtx.
699 void *sqlite3_get_auxdata(sqlite3_context
*pCtx
, int iArg
){
702 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
703 for(pAuxData
=pCtx
->pVdbe
->pAuxData
; pAuxData
; pAuxData
=pAuxData
->pNext
){
704 if( pAuxData
->iOp
==pCtx
->iOp
&& pAuxData
->iArg
==iArg
) break;
707 return (pAuxData
? pAuxData
->pAux
: 0);
711 ** Set the auxiliary data pointer and delete function, for the iArg'th
712 ** argument to the user-function defined by pCtx. Any previous value is
713 ** deleted by calling the delete function specified when it was set.
715 void sqlite3_set_auxdata(
716 sqlite3_context
*pCtx
,
719 void (*xDelete
)(void*)
722 Vdbe
*pVdbe
= pCtx
->pVdbe
;
724 assert( sqlite3_mutex_held(pCtx
->pOut
->db
->mutex
) );
725 if( iArg
<0 ) goto failed
;
727 for(pAuxData
=pVdbe
->pAuxData
; pAuxData
; pAuxData
=pAuxData
->pNext
){
728 if( pAuxData
->iOp
==pCtx
->iOp
&& pAuxData
->iArg
==iArg
) break;
731 pAuxData
= sqlite3DbMallocZero(pVdbe
->db
, sizeof(AuxData
));
732 if( !pAuxData
) goto failed
;
733 pAuxData
->iOp
= pCtx
->iOp
;
734 pAuxData
->iArg
= iArg
;
735 pAuxData
->pNext
= pVdbe
->pAuxData
;
736 pVdbe
->pAuxData
= pAuxData
;
737 if( pCtx
->fErrorOrAux
==0 ){
739 pCtx
->fErrorOrAux
= 1;
741 }else if( pAuxData
->xDelete
){
742 pAuxData
->xDelete(pAuxData
->pAux
);
745 pAuxData
->pAux
= pAux
;
746 pAuxData
->xDelete
= xDelete
;
755 #ifndef SQLITE_OMIT_DEPRECATED
757 ** Return the number of times the Step function of an aggregate has been
760 ** This function is deprecated. Do not use it for new code. It is
761 ** provide only to avoid breaking legacy code. New aggregate function
762 ** implementations should keep their own counts within their aggregate
765 int sqlite3_aggregate_count(sqlite3_context
*p
){
766 assert( p
&& p
->pMem
&& p
->pFunc
&& p
->pFunc
->xStep
);
772 ** Return the number of columns in the result set for the statement pStmt.
774 int sqlite3_column_count(sqlite3_stmt
*pStmt
){
775 Vdbe
*pVm
= (Vdbe
*)pStmt
;
776 return pVm
? pVm
->nResColumn
: 0;
780 ** Return the number of values available from the current row of the
781 ** currently executing statement pStmt.
783 int sqlite3_data_count(sqlite3_stmt
*pStmt
){
784 Vdbe
*pVm
= (Vdbe
*)pStmt
;
785 if( pVm
==0 || pVm
->pResultSet
==0 ) return 0;
786 return pVm
->nResColumn
;
790 ** Return a pointer to static memory containing an SQL NULL value.
792 static const Mem
*columnNullValue(void){
793 /* Even though the Mem structure contains an element
794 ** of type i64, on certain architectures (x86) with certain compiler
795 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
796 ** instead of an 8-byte one. This all works fine, except that when
797 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
798 ** that a Mem structure is located on an 8-byte boundary. To prevent
799 ** these assert()s from failing, when building with SQLITE_DEBUG defined
800 ** using gcc, we force nullMem to be 8-byte aligned using the magical
801 ** __attribute__((aligned(8))) macro. */
802 static const Mem nullMem
803 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
804 __attribute__((aligned(8)))
808 /* .flags = */ MEM_Null
,
814 /* .iPadding1 = */ 0,
818 /* .pScopyFrom = */ 0,
826 ** Check to see if column iCol of the given statement is valid. If
827 ** it is, return a pointer to the Mem for the value of that column.
828 ** If iCol is not valid, return a pointer to a Mem which has a value
831 static Mem
*columnMem(sqlite3_stmt
*pStmt
, int i
){
836 if( pVm
&& pVm
->pResultSet
!=0 && i
<pVm
->nResColumn
&& i
>=0 ){
837 sqlite3_mutex_enter(pVm
->db
->mutex
);
838 pOut
= &pVm
->pResultSet
[i
];
840 if( pVm
&& ALWAYS(pVm
->db
) ){
841 sqlite3_mutex_enter(pVm
->db
->mutex
);
842 sqlite3Error(pVm
->db
, SQLITE_RANGE
);
844 pOut
= (Mem
*)columnNullValue();
850 ** This function is called after invoking an sqlite3_value_XXX function on a
851 ** column value (i.e. a value returned by evaluating an SQL expression in the
852 ** select list of a SELECT statement) that may cause a malloc() failure. If
853 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
854 ** code of statement pStmt set to SQLITE_NOMEM.
856 ** Specifically, this is called from within:
858 ** sqlite3_column_int()
859 ** sqlite3_column_int64()
860 ** sqlite3_column_text()
861 ** sqlite3_column_text16()
862 ** sqlite3_column_real()
863 ** sqlite3_column_bytes()
864 ** sqlite3_column_bytes16()
865 ** sqiite3_column_blob()
867 static void columnMallocFailure(sqlite3_stmt
*pStmt
)
869 /* If malloc() failed during an encoding conversion within an
870 ** sqlite3_column_XXX API, then set the return code of the statement to
871 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
872 ** and _finalize() will return NOMEM.
874 Vdbe
*p
= (Vdbe
*)pStmt
;
876 p
->rc
= sqlite3ApiExit(p
->db
, p
->rc
);
877 sqlite3_mutex_leave(p
->db
->mutex
);
881 /**************************** sqlite3_column_ *******************************
882 ** The following routines are used to access elements of the current row
883 ** in the result set.
885 const void *sqlite3_column_blob(sqlite3_stmt
*pStmt
, int i
){
887 val
= sqlite3_value_blob( columnMem(pStmt
,i
) );
888 /* Even though there is no encoding conversion, value_blob() might
889 ** need to call malloc() to expand the result of a zeroblob()
892 columnMallocFailure(pStmt
);
895 int sqlite3_column_bytes(sqlite3_stmt
*pStmt
, int i
){
896 int val
= sqlite3_value_bytes( columnMem(pStmt
,i
) );
897 columnMallocFailure(pStmt
);
900 int sqlite3_column_bytes16(sqlite3_stmt
*pStmt
, int i
){
901 int val
= sqlite3_value_bytes16( columnMem(pStmt
,i
) );
902 columnMallocFailure(pStmt
);
905 double sqlite3_column_double(sqlite3_stmt
*pStmt
, int i
){
906 double val
= sqlite3_value_double( columnMem(pStmt
,i
) );
907 columnMallocFailure(pStmt
);
910 int sqlite3_column_int(sqlite3_stmt
*pStmt
, int i
){
911 int val
= sqlite3_value_int( columnMem(pStmt
,i
) );
912 columnMallocFailure(pStmt
);
915 sqlite_int64
sqlite3_column_int64(sqlite3_stmt
*pStmt
, int i
){
916 sqlite_int64 val
= sqlite3_value_int64( columnMem(pStmt
,i
) );
917 columnMallocFailure(pStmt
);
920 const unsigned char *sqlite3_column_text(sqlite3_stmt
*pStmt
, int i
){
921 const unsigned char *val
= sqlite3_value_text( columnMem(pStmt
,i
) );
922 columnMallocFailure(pStmt
);
925 sqlite3_value
*sqlite3_column_value(sqlite3_stmt
*pStmt
, int i
){
926 Mem
*pOut
= columnMem(pStmt
, i
);
927 if( pOut
->flags
&MEM_Static
){
928 pOut
->flags
&= ~MEM_Static
;
929 pOut
->flags
|= MEM_Ephem
;
931 columnMallocFailure(pStmt
);
932 return (sqlite3_value
*)pOut
;
934 #ifndef SQLITE_OMIT_UTF16
935 const void *sqlite3_column_text16(sqlite3_stmt
*pStmt
, int i
){
936 const void *val
= sqlite3_value_text16( columnMem(pStmt
,i
) );
937 columnMallocFailure(pStmt
);
940 #endif /* SQLITE_OMIT_UTF16 */
941 int sqlite3_column_type(sqlite3_stmt
*pStmt
, int i
){
942 int iType
= sqlite3_value_type( columnMem(pStmt
,i
) );
943 columnMallocFailure(pStmt
);
948 ** Convert the N-th element of pStmt->pColName[] into a string using
949 ** xFunc() then return that string. If N is out of range, return 0.
951 ** There are up to 5 names for each column. useType determines which
952 ** name is returned. Here are the names:
954 ** 0 The column name as it should be displayed for output
955 ** 1 The datatype name for the column
956 ** 2 The name of the database that the column derives from
957 ** 3 The name of the table that the column derives from
958 ** 4 The name of the table column that the result column derives from
960 ** If the result is not a simple column reference (if it is an expression
961 ** or a constant) then useTypes 2, 3, and 4 return NULL.
963 static const void *columnName(
966 const void *(*xFunc
)(Mem
*),
970 Vdbe
*p
= (Vdbe
*)pStmt
;
975 n
= sqlite3_column_count(pStmt
);
978 sqlite3_mutex_enter(db
->mutex
);
979 assert( db
->mallocFailed
==0 );
980 ret
= xFunc(&p
->aColName
[N
]);
981 /* A malloc may have failed inside of the xFunc() call. If this
982 ** is the case, clear the mallocFailed flag and return NULL.
984 if( db
->mallocFailed
){
985 db
->mallocFailed
= 0;
988 sqlite3_mutex_leave(db
->mutex
);
994 ** Return the name of the Nth column of the result set returned by SQL
997 const char *sqlite3_column_name(sqlite3_stmt
*pStmt
, int N
){
999 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, COLNAME_NAME
);
1001 #ifndef SQLITE_OMIT_UTF16
1002 const void *sqlite3_column_name16(sqlite3_stmt
*pStmt
, int N
){
1004 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, COLNAME_NAME
);
1009 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
1010 ** not define OMIT_DECLTYPE.
1012 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
1013 # error "Must not define both SQLITE_OMIT_DECLTYPE \
1014 and SQLITE_ENABLE_COLUMN_METADATA"
1017 #ifndef SQLITE_OMIT_DECLTYPE
1019 ** Return the column declaration type (if applicable) of the 'i'th column
1020 ** of the result set of SQL statement pStmt.
1022 const char *sqlite3_column_decltype(sqlite3_stmt
*pStmt
, int N
){
1024 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, COLNAME_DECLTYPE
);
1026 #ifndef SQLITE_OMIT_UTF16
1027 const void *sqlite3_column_decltype16(sqlite3_stmt
*pStmt
, int N
){
1029 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, COLNAME_DECLTYPE
);
1031 #endif /* SQLITE_OMIT_UTF16 */
1032 #endif /* SQLITE_OMIT_DECLTYPE */
1034 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1036 ** Return the name of the database from which a result column derives.
1037 ** NULL is returned if the result column is an expression or constant or
1038 ** anything else which is not an unambiguous reference to a database column.
1040 const char *sqlite3_column_database_name(sqlite3_stmt
*pStmt
, int N
){
1042 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, COLNAME_DATABASE
);
1044 #ifndef SQLITE_OMIT_UTF16
1045 const void *sqlite3_column_database_name16(sqlite3_stmt
*pStmt
, int N
){
1047 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, COLNAME_DATABASE
);
1049 #endif /* SQLITE_OMIT_UTF16 */
1052 ** Return the name of the table from which a result column derives.
1053 ** NULL is returned if the result column is an expression or constant or
1054 ** anything else which is not an unambiguous reference to a database column.
1056 const char *sqlite3_column_table_name(sqlite3_stmt
*pStmt
, int N
){
1058 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, COLNAME_TABLE
);
1060 #ifndef SQLITE_OMIT_UTF16
1061 const void *sqlite3_column_table_name16(sqlite3_stmt
*pStmt
, int N
){
1063 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, COLNAME_TABLE
);
1065 #endif /* SQLITE_OMIT_UTF16 */
1068 ** Return the name of the table column from which a result column derives.
1069 ** NULL is returned if the result column is an expression or constant or
1070 ** anything else which is not an unambiguous reference to a database column.
1072 const char *sqlite3_column_origin_name(sqlite3_stmt
*pStmt
, int N
){
1074 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, COLNAME_COLUMN
);
1076 #ifndef SQLITE_OMIT_UTF16
1077 const void *sqlite3_column_origin_name16(sqlite3_stmt
*pStmt
, int N
){
1079 pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, COLNAME_COLUMN
);
1081 #endif /* SQLITE_OMIT_UTF16 */
1082 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
1085 /******************************* sqlite3_bind_ ***************************
1087 ** Routines used to attach values to wildcards in a compiled SQL statement.
1090 ** Unbind the value bound to variable i in virtual machine p. This is the
1091 ** the same as binding a NULL value to the column. If the "i" parameter is
1092 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1094 ** A successful evaluation of this routine acquires the mutex on p.
1095 ** the mutex is released if any kind of error occurs.
1097 ** The error code stored in database p->db is overwritten with the return
1098 ** value in any case.
1100 static int vdbeUnbind(Vdbe
*p
, int i
){
1102 if( vdbeSafetyNotNull(p
) ){
1103 return SQLITE_MISUSE_BKPT
;
1105 sqlite3_mutex_enter(p
->db
->mutex
);
1106 if( p
->magic
!=VDBE_MAGIC_RUN
|| p
->pc
>=0 ){
1107 sqlite3Error(p
->db
, SQLITE_MISUSE
);
1108 sqlite3_mutex_leave(p
->db
->mutex
);
1109 sqlite3_log(SQLITE_MISUSE
,
1110 "bind on a busy prepared statement: [%s]", p
->zSql
);
1111 return SQLITE_MISUSE_BKPT
;
1113 if( i
<1 || i
>p
->nVar
){
1114 sqlite3Error(p
->db
, SQLITE_RANGE
);
1115 sqlite3_mutex_leave(p
->db
->mutex
);
1116 return SQLITE_RANGE
;
1120 sqlite3VdbeMemRelease(pVar
);
1121 pVar
->flags
= MEM_Null
;
1122 sqlite3Error(p
->db
, SQLITE_OK
);
1124 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1125 ** binding a new value to this variable invalidates the current query plan.
1127 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1128 ** parameter in the WHERE clause might influence the choice of query plan
1129 ** for a statement, then the statement will be automatically recompiled,
1130 ** as if there had been a schema change, on the first sqlite3_step() call
1131 ** following any change to the bindings of that parameter.
1133 if( p
->isPrepareV2
&&
1134 ((i
<32 && p
->expmask
& ((u32
)1 << i
)) || p
->expmask
==0xffffffff)
1142 ** Bind a text or BLOB value.
1144 static int bindText(
1145 sqlite3_stmt
*pStmt
, /* The statement to bind against */
1146 int i
, /* Index of the parameter to bind */
1147 const void *zData
, /* Pointer to the data to be bound */
1148 int nData
, /* Number of bytes of data to be bound */
1149 void (*xDel
)(void*), /* Destructor for the data */
1150 u8 encoding
/* Encoding for the data */
1152 Vdbe
*p
= (Vdbe
*)pStmt
;
1156 rc
= vdbeUnbind(p
, i
);
1157 if( rc
==SQLITE_OK
){
1159 pVar
= &p
->aVar
[i
-1];
1160 rc
= sqlite3VdbeMemSetStr(pVar
, zData
, nData
, encoding
, xDel
);
1161 if( rc
==SQLITE_OK
&& encoding
!=0 ){
1162 rc
= sqlite3VdbeChangeEncoding(pVar
, ENC(p
->db
));
1164 sqlite3Error(p
->db
, rc
);
1165 rc
= sqlite3ApiExit(p
->db
, rc
);
1167 sqlite3_mutex_leave(p
->db
->mutex
);
1168 }else if( xDel
!=SQLITE_STATIC
&& xDel
!=SQLITE_TRANSIENT
){
1176 ** Bind a blob value to an SQL statement variable.
1178 int sqlite3_bind_blob(
1179 sqlite3_stmt
*pStmt
,
1185 return bindText(pStmt
, i
, zData
, nData
, xDel
, 0);
1187 int sqlite3_bind_blob64(
1188 sqlite3_stmt
*pStmt
,
1191 sqlite3_uint64 nData
,
1194 assert( xDel
!=SQLITE_DYNAMIC
);
1195 if( nData
>0x7fffffff ){
1196 return invokeValueDestructor(zData
, xDel
, 0);
1198 return bindText(pStmt
, i
, zData
, (int)nData
, xDel
, 0);
1201 int sqlite3_bind_double(sqlite3_stmt
*pStmt
, int i
, double rValue
){
1203 Vdbe
*p
= (Vdbe
*)pStmt
;
1204 rc
= vdbeUnbind(p
, i
);
1205 if( rc
==SQLITE_OK
){
1206 sqlite3VdbeMemSetDouble(&p
->aVar
[i
-1], rValue
);
1207 sqlite3_mutex_leave(p
->db
->mutex
);
1211 int sqlite3_bind_int(sqlite3_stmt
*p
, int i
, int iValue
){
1212 return sqlite3_bind_int64(p
, i
, (i64
)iValue
);
1214 int sqlite3_bind_int64(sqlite3_stmt
*pStmt
, int i
, sqlite_int64 iValue
){
1216 Vdbe
*p
= (Vdbe
*)pStmt
;
1217 rc
= vdbeUnbind(p
, i
);
1218 if( rc
==SQLITE_OK
){
1219 sqlite3VdbeMemSetInt64(&p
->aVar
[i
-1], iValue
);
1220 sqlite3_mutex_leave(p
->db
->mutex
);
1224 int sqlite3_bind_null(sqlite3_stmt
*pStmt
, int i
){
1226 Vdbe
*p
= (Vdbe
*)pStmt
;
1227 rc
= vdbeUnbind(p
, i
);
1228 if( rc
==SQLITE_OK
){
1229 sqlite3_mutex_leave(p
->db
->mutex
);
1233 int sqlite3_bind_text(
1234 sqlite3_stmt
*pStmt
,
1240 return bindText(pStmt
, i
, zData
, nData
, xDel
, SQLITE_UTF8
);
1242 int sqlite3_bind_text64(
1243 sqlite3_stmt
*pStmt
,
1246 sqlite3_uint64 nData
,
1247 void (*xDel
)(void*),
1250 assert( xDel
!=SQLITE_DYNAMIC
);
1251 if( nData
>0x7fffffff ){
1252 return invokeValueDestructor(zData
, xDel
, 0);
1254 if( enc
==SQLITE_UTF16
) enc
= SQLITE_UTF16NATIVE
;
1255 return bindText(pStmt
, i
, zData
, (int)nData
, xDel
, enc
);
1258 #ifndef SQLITE_OMIT_UTF16
1259 int sqlite3_bind_text16(
1260 sqlite3_stmt
*pStmt
,
1266 return bindText(pStmt
, i
, zData
, nData
, xDel
, SQLITE_UTF16NATIVE
);
1268 #endif /* SQLITE_OMIT_UTF16 */
1269 int sqlite3_bind_value(sqlite3_stmt
*pStmt
, int i
, const sqlite3_value
*pValue
){
1271 switch( sqlite3_value_type((sqlite3_value
*)pValue
) ){
1272 case SQLITE_INTEGER
: {
1273 rc
= sqlite3_bind_int64(pStmt
, i
, pValue
->u
.i
);
1276 case SQLITE_FLOAT
: {
1277 rc
= sqlite3_bind_double(pStmt
, i
, pValue
->u
.r
);
1281 if( pValue
->flags
& MEM_Zero
){
1282 rc
= sqlite3_bind_zeroblob(pStmt
, i
, pValue
->u
.nZero
);
1284 rc
= sqlite3_bind_blob(pStmt
, i
, pValue
->z
, pValue
->n
,SQLITE_TRANSIENT
);
1289 rc
= bindText(pStmt
,i
, pValue
->z
, pValue
->n
, SQLITE_TRANSIENT
,
1294 rc
= sqlite3_bind_null(pStmt
, i
);
1300 int sqlite3_bind_zeroblob(sqlite3_stmt
*pStmt
, int i
, int n
){
1302 Vdbe
*p
= (Vdbe
*)pStmt
;
1303 rc
= vdbeUnbind(p
, i
);
1304 if( rc
==SQLITE_OK
){
1305 sqlite3VdbeMemSetZeroBlob(&p
->aVar
[i
-1], n
);
1306 sqlite3_mutex_leave(p
->db
->mutex
);
1312 ** Return the number of wildcards that can be potentially bound to.
1313 ** This routine is added to support DBD::SQLite.
1315 int sqlite3_bind_parameter_count(sqlite3_stmt
*pStmt
){
1316 Vdbe
*p
= (Vdbe
*)pStmt
;
1317 return p
? p
->nVar
: 0;
1321 ** Return the name of a wildcard parameter. Return NULL if the index
1322 ** is out of range or if the wildcard is unnamed.
1324 ** The result is always UTF-8.
1326 const char *sqlite3_bind_parameter_name(sqlite3_stmt
*pStmt
, int i
){
1327 Vdbe
*p
= (Vdbe
*)pStmt
;
1328 if( p
==0 || i
<1 || i
>p
->nzVar
){
1331 return p
->azVar
[i
-1];
1335 ** Given a wildcard parameter name, return the index of the variable
1336 ** with that name. If there is no variable with the given name,
1339 int sqlite3VdbeParameterIndex(Vdbe
*p
, const char *zName
, int nName
){
1345 for(i
=0; i
<p
->nzVar
; i
++){
1346 const char *z
= p
->azVar
[i
];
1347 if( z
&& strncmp(z
,zName
,nName
)==0 && z
[nName
]==0 ){
1354 int sqlite3_bind_parameter_index(sqlite3_stmt
*pStmt
, const char *zName
){
1355 return sqlite3VdbeParameterIndex((Vdbe
*)pStmt
, zName
, sqlite3Strlen30(zName
));
1359 ** Transfer all bindings from the first statement over to the second.
1361 int sqlite3TransferBindings(sqlite3_stmt
*pFromStmt
, sqlite3_stmt
*pToStmt
){
1362 Vdbe
*pFrom
= (Vdbe
*)pFromStmt
;
1363 Vdbe
*pTo
= (Vdbe
*)pToStmt
;
1365 assert( pTo
->db
==pFrom
->db
);
1366 assert( pTo
->nVar
==pFrom
->nVar
);
1367 sqlite3_mutex_enter(pTo
->db
->mutex
);
1368 for(i
=0; i
<pFrom
->nVar
; i
++){
1369 sqlite3VdbeMemMove(&pTo
->aVar
[i
], &pFrom
->aVar
[i
]);
1371 sqlite3_mutex_leave(pTo
->db
->mutex
);
1375 #ifndef SQLITE_OMIT_DEPRECATED
1377 ** Deprecated external interface. Internal/core SQLite code
1378 ** should call sqlite3TransferBindings.
1380 ** It is misuse to call this routine with statements from different
1381 ** database connections. But as this is a deprecated interface, we
1382 ** will not bother to check for that condition.
1384 ** If the two statements contain a different number of bindings, then
1385 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1386 ** SQLITE_OK is returned.
1388 int sqlite3_transfer_bindings(sqlite3_stmt
*pFromStmt
, sqlite3_stmt
*pToStmt
){
1389 Vdbe
*pFrom
= (Vdbe
*)pFromStmt
;
1390 Vdbe
*pTo
= (Vdbe
*)pToStmt
;
1391 if( pFrom
->nVar
!=pTo
->nVar
){
1392 return SQLITE_ERROR
;
1394 if( pTo
->isPrepareV2
&& pTo
->expmask
){
1397 if( pFrom
->isPrepareV2
&& pFrom
->expmask
){
1400 return sqlite3TransferBindings(pFromStmt
, pToStmt
);
1405 ** Return the sqlite3* database handle to which the prepared statement given
1406 ** in the argument belongs. This is the same database handle that was
1407 ** the first argument to the sqlite3_prepare() that was used to create
1408 ** the statement in the first place.
1410 sqlite3
*sqlite3_db_handle(sqlite3_stmt
*pStmt
){
1411 return pStmt
? ((Vdbe
*)pStmt
)->db
: 0;
1415 ** Return true if the prepared statement is guaranteed to not modify the
1418 int sqlite3_stmt_readonly(sqlite3_stmt
*pStmt
){
1419 return pStmt
? ((Vdbe
*)pStmt
)->readOnly
: 1;
1423 ** Return true if the prepared statement is in need of being reset.
1425 int sqlite3_stmt_busy(sqlite3_stmt
*pStmt
){
1426 Vdbe
*v
= (Vdbe
*)pStmt
;
1427 return v
!=0 && v
->pc
>=0 && v
->magic
==VDBE_MAGIC_RUN
;
1431 ** Return a pointer to the next prepared statement after pStmt associated
1432 ** with database connection pDb. If pStmt is NULL, return the first
1433 ** prepared statement for the database connection. Return NULL if there
1436 sqlite3_stmt
*sqlite3_next_stmt(sqlite3
*pDb
, sqlite3_stmt
*pStmt
){
1437 sqlite3_stmt
*pNext
;
1438 sqlite3_mutex_enter(pDb
->mutex
);
1440 pNext
= (sqlite3_stmt
*)pDb
->pVdbe
;
1442 pNext
= (sqlite3_stmt
*)((Vdbe
*)pStmt
)->pNext
;
1444 sqlite3_mutex_leave(pDb
->mutex
);
1449 ** Return the value of a status counter for a prepared statement
1451 int sqlite3_stmt_status(sqlite3_stmt
*pStmt
, int op
, int resetFlag
){
1452 Vdbe
*pVdbe
= (Vdbe
*)pStmt
;
1453 u32 v
= pVdbe
->aCounter
[op
];
1454 if( resetFlag
) pVdbe
->aCounter
[op
] = 0;