blob: 6cbc95459559999e2dea7a380316e86afcdd62f9 [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
2** 2004 May 26
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains code use to implement APIs that are part of the
14** VDBE.
15*/
16#include "sqliteInt.h"
17#include "vdbeInt.h"
18
shaneeec556d2008-10-12 00:27:53 +000019#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +000020/*
21** Return TRUE (non-zero) of the statement supplied as an argument needs
22** to be recompiled. A statement needs to be recompiled whenever the
23** execution environment changes in a way that would alter the program
24** that sqlite3_prepare() generates. For example, if new functions or
25** collating sequences are registered or if an authorizer function is
26** added or changed.
drhd89bd002005-01-22 03:03:54 +000027*/
28int sqlite3_expired(sqlite3_stmt *pStmt){
29 Vdbe *p = (Vdbe*)pStmt;
30 return p==0 || p->expired;
31}
shaneeec556d2008-10-12 00:27:53 +000032#endif
drhd89bd002005-01-22 03:03:54 +000033
drhe30f4422007-08-21 16:15:55 +000034/*
35** The following routine destroys a virtual machine that is created by
36** the sqlite3_compile() routine. The integer returned is an SQLITE_
37** success/failure code that describes the result of executing the virtual
38** machine.
39**
40** This routine sets the error code and string returned by
41** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
42*/
43int sqlite3_finalize(sqlite3_stmt *pStmt){
44 int rc;
45 if( pStmt==0 ){
46 rc = SQLITE_OK;
47 }else{
48 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06 +000049 sqlite3 *db = v->db;
drh87f5c5f2010-01-20 01:20:56 +000050 if( db==0 ) return SQLITE_MISUSE;
drh18472fa2008-10-07 15:25:48 +000051#if SQLITE_THREADSAFE
drh86f8c192007-08-22 00:39:19 +000052 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +000053#endif
drh86f8c192007-08-22 00:39:19 +000054 sqlite3_mutex_enter(mutex);
drhe30f4422007-08-21 16:15:55 +000055 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +000056 rc = sqlite3ApiExit(db, rc);
drh86f8c192007-08-22 00:39:19 +000057 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +000058 }
59 return rc;
60}
61
62/*
63** Terminate the current execution of an SQL statement and reset it
64** back to its starting state so that it can be reused. A success code from
65** the prior execution is returned.
66**
67** This routine sets the error code and string returned by
68** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
69*/
70int sqlite3_reset(sqlite3_stmt *pStmt){
71 int rc;
72 if( pStmt==0 ){
73 rc = SQLITE_OK;
74 }else{
75 Vdbe *v = (Vdbe*)pStmt;
76 sqlite3_mutex_enter(v->db->mutex);
drhc890fec2008-08-01 20:10:08 +000077 rc = sqlite3VdbeReset(v);
dane0af83a2009-09-08 19:15:01 +000078 sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
drhe30f4422007-08-21 16:15:55 +000079 assert( (rc & (v->db->errMask))==rc );
danielk1977238746a2009-03-19 18:51:06 +000080 rc = sqlite3ApiExit(v->db, rc);
drhe30f4422007-08-21 16:15:55 +000081 sqlite3_mutex_leave(v->db->mutex);
82 }
83 return rc;
84}
85
86/*
87** Set all the parameters in the compiled SQL statement to NULL.
88*/
89int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
90 int i;
91 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +000092 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +000093#if SQLITE_THREADSAFE
drh7e8b8482008-01-23 03:03:05 +000094 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
95#endif
96 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +000097 for(i=0; i<p->nVar; i++){
98 sqlite3VdbeMemRelease(&p->aVar[i]);
99 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000100 }
danc94b8592009-10-20 07:01:24 +0000101 if( p->isPrepareV2 && p->expmask ){
102 p->expired = 1;
103 }
drh7e8b8482008-01-23 03:03:05 +0000104 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000105 return rc;
106}
107
108
drh4f26d6c2004-05-26 23:25:30 +0000109/**************************** sqlite3_value_ *******************************
110** The following routines extract information from a Mem or sqlite3_value
111** structure.
112*/
113const void *sqlite3_value_blob(sqlite3_value *pVal){
114 Mem *p = (Mem*)pVal;
115 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000116 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000117 p->flags &= ~MEM_Str;
118 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000119 return p->z;
120 }else{
121 return sqlite3_value_text(pVal);
122 }
123}
124int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000125 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000126}
127int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000128 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000129}
130double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000131 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000132}
133int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000134 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000135}
drhefad9992004-06-22 12:13:55 +0000136sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000137 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000138}
139const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000140 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000141}
drh6c626082004-11-14 21:56:29 +0000142#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000143const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000144 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000145}
danielk1977d8123362004-06-12 09:25:12 +0000146const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000147 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000148}
149const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000150 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000151}
drh6c626082004-11-14 21:56:29 +0000152#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000153int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000154 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000155}
156
157/**************************** sqlite3_result_ *******************************
158** The following routines are used by user-defined functions to specify
159** the function result.
drh4c8555f2009-06-25 01:47:11 +0000160**
161** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
162** result as a string or blob but if the string or blob is too large, it
163** then sets the error code to SQLITE_TOOBIG
drh4f26d6c2004-05-26 23:25:30 +0000164*/
drh4c8555f2009-06-25 01:47:11 +0000165static void setResultStrOrError(
166 sqlite3_context *pCtx, /* Function context */
167 const char *z, /* String pointer */
168 int n, /* Bytes in string, or negative */
169 u8 enc, /* Encoding of z. 0 for BLOBs */
170 void (*xDel)(void*) /* Destructor function */
171){
172 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
173 sqlite3_result_error_toobig(pCtx);
174 }
175}
drh4f26d6c2004-05-26 23:25:30 +0000176void sqlite3_result_blob(
177 sqlite3_context *pCtx,
178 const void *z,
179 int n,
danielk1977d8123362004-06-12 09:25:12 +0000180 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000181){
drh5708d2d2005-06-22 10:53:59 +0000182 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000183 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000184 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000185}
186void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000187 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000188 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
189}
190void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000191 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000192 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000193 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000194}
danielk1977a1686c92006-01-23 07:52:37 +0000195#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000196void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000197 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000198 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000199 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000200}
danielk1977a1686c92006-01-23 07:52:37 +0000201#endif
drhf4479502004-05-27 03:12:53 +0000202void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000203 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000204 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
205}
206void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000207 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000208 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
209}
210void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000211 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000212 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000213}
214void sqlite3_result_text(
215 sqlite3_context *pCtx,
216 const char *z,
217 int n,
danielk1977d8123362004-06-12 09:25:12 +0000218 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000219){
drh32bc3f62007-08-21 20:25:39 +0000220 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000221 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000222}
drh6c626082004-11-14 21:56:29 +0000223#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000224void sqlite3_result_text16(
225 sqlite3_context *pCtx,
226 const void *z,
227 int n,
danielk1977d8123362004-06-12 09:25:12 +0000228 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000229){
drh32bc3f62007-08-21 20:25:39 +0000230 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000231 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000232}
233void sqlite3_result_text16be(
234 sqlite3_context *pCtx,
235 const void *z,
236 int n,
237 void (*xDel)(void *)
238){
drh32bc3f62007-08-21 20:25:39 +0000239 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000240 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000241}
242void sqlite3_result_text16le(
243 sqlite3_context *pCtx,
244 const void *z,
245 int n,
246 void (*xDel)(void *)
247){
drh32bc3f62007-08-21 20:25:39 +0000248 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000249 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000250}
drh6c626082004-11-14 21:56:29 +0000251#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000252void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000253 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000254 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000255}
drhb026e052007-05-02 01:34:31 +0000256void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000257 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000258 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
259}
drh69544ec2008-02-06 14:11:34 +0000260void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
261 pCtx->isError = errCode;
drh51c7d862009-04-27 18:46:06 +0000262 if( pCtx->s.flags & MEM_Null ){
263 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
264 SQLITE_UTF8, SQLITE_STATIC);
265 }
drh69544ec2008-02-06 14:11:34 +0000266}
drh4f26d6c2004-05-26 23:25:30 +0000267
drha0206bc2007-05-08 15:15:02 +0000268/* Force an SQLITE_TOOBIG error. */
269void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000270 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000271 pCtx->isError = SQLITE_TOOBIG;
272 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
273 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000274}
275
danielk1977a1644fd2007-08-29 12:31:25 +0000276/* An SQLITE_NOMEM error. */
277void sqlite3_result_error_nomem(sqlite3_context *pCtx){
278 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
279 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000280 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000281 pCtx->s.db->mallocFailed = 1;
282}
drh4f26d6c2004-05-26 23:25:30 +0000283
284/*
285** Execute the statement pStmt, either until a row of data is ready, the
286** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000287**
288** This routine implements the bulk of the logic behind the sqlite_step()
289** API. The only thing omitted is the automatic recompile if a
290** schema change has occurred. That detail is handled by the
291** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000292*/
drhb900aaf2006-11-09 00:24:53 +0000293static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000294 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000295 int rc;
296
danielk1977a185ddb2007-11-15 16:04:15 +0000297 assert(p);
298 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000299 return SQLITE_MISUSE;
300 }
301
dan14d4cc42010-01-20 14:25:37 +0000302 /* Check that malloc() has not failed. If it has, return early. */
drh17435752007-08-16 04:30:38 +0000303 db = p->db;
drhc456e572008-08-11 18:44:58 +0000304 if( db->mallocFailed ){
dan14d4cc42010-01-20 14:25:37 +0000305 p->rc = SQLITE_NOMEM;
drhc456e572008-08-11 18:44:58 +0000306 return SQLITE_NOMEM;
307 }
danielk1977261919c2005-12-06 12:52:59 +0000308
danielk1977a21c6b62005-01-24 10:25:59 +0000309 if( p->pc<=0 && p->expired ){
danef516222009-10-19 18:30:34 +0000310 if( ALWAYS(p->rc==SQLITE_OK || p->rc==SQLITE_SCHEMA) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000311 p->rc = SQLITE_SCHEMA;
312 }
drhb900aaf2006-11-09 00:24:53 +0000313 rc = SQLITE_ERROR;
314 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000315 }
drh4f26d6c2004-05-26 23:25:30 +0000316 if( sqlite3SafetyOn(db) ){
317 p->rc = SQLITE_MISUSE;
318 return SQLITE_MISUSE;
319 }
danielk19771d850a72004-05-31 08:26:49 +0000320 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000321 /* If there are no other statements currently running, then
322 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
323 ** from interrupting a statement that has not yet started.
324 */
325 if( db->activeVdbeCnt==0 ){
326 db->u1.isInterrupted = 0;
327 }
328
dan1da40a32009-09-19 17:00:31 +0000329 assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
330
drh19e2d372005-08-29 23:00:03 +0000331#ifndef SQLITE_OMIT_TRACE
drh19e2d372005-08-29 23:00:03 +0000332 if( db->xProfile && !db->init.busy ){
333 double rNow;
danielk1977b4b47412007-08-17 15:53:36 +0000334 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000335 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
drh19e2d372005-08-29 23:00:03 +0000336 }
337#endif
drhc16a03b2004-09-15 13:38:10 +0000338
danielk19771d850a72004-05-31 08:26:49 +0000339 db->activeVdbeCnt++;
drhad4a4b82008-11-05 16:37:34 +0000340 if( p->readOnly==0 ) db->writeVdbeCnt++;
danielk19771d850a72004-05-31 08:26:49 +0000341 p->pc = 0;
342 }
drhb7f91642004-10-31 02:22:47 +0000343#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000344 if( p->explain ){
345 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000346 }else
347#endif /* SQLITE_OMIT_EXPLAIN */
348 {
drh4f26d6c2004-05-26 23:25:30 +0000349 rc = sqlite3VdbeExec(p);
350 }
351
352 if( sqlite3SafetyOff(db) ){
353 rc = SQLITE_MISUSE;
354 }
355
drh19e2d372005-08-29 23:00:03 +0000356#ifndef SQLITE_OMIT_TRACE
357 /* Invoke the profile callback if there is one
358 */
danielk19776ab3a2e2009-02-19 14:39:25 +0000359 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
drh19e2d372005-08-29 23:00:03 +0000360 double rNow;
361 u64 elapseTime;
362
danielk1977b4b47412007-08-17 15:53:36 +0000363 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000364 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
365 elapseTime -= p->startTime;
danielk19776ab3a2e2009-02-19 14:39:25 +0000366 db->xProfile(db->pProfileArg, p->zSql, elapseTime);
drh19e2d372005-08-29 23:00:03 +0000367 }
368#endif
369
drh80cc85b2008-07-23 21:07:25 +0000370 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000371 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
372 p->rc = SQLITE_NOMEM;
drhb900aaf2006-11-09 00:24:53 +0000373 }
danielk1977357864e2009-03-25 15:43:08 +0000374end_of_step:
375 /* At this point local variable rc holds the value that should be
376 ** returned if this statement was compiled using the legacy
377 ** sqlite3_prepare() interface. According to the docs, this can only
378 ** be one of the values in the first assert() below. Variable p->rc
379 ** contains the value that would be returned if sqlite3_finalize()
380 ** were called on statement p.
381 */
382 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
383 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
384 );
385 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
386 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
387 /* If this statement was prepared using sqlite3_prepare_v2(), and an
388 ** error has occured, then return the error code in p->rc to the
389 ** caller. Set the error code in the database handle to the same value.
390 */
391 rc = db->errCode = p->rc;
392 }
393 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000394}
395
396/*
397** This is the top-level implementation of sqlite3_step(). Call
398** sqlite3Step() to do most of the work. If a schema error occurs,
399** call sqlite3Reprepare() and try again.
400*/
drhb900aaf2006-11-09 00:24:53 +0000401int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000402 int rc = SQLITE_MISUSE;
drh87f5c5f2010-01-20 01:20:56 +0000403 Vdbe *v = (Vdbe*)pStmt;
404 if( v && (v->db)!=0 ){
danielk1977a185ddb2007-11-15 16:04:15 +0000405 int cnt = 0;
danielk1977a185ddb2007-11-15 16:04:15 +0000406 sqlite3 *db = v->db;
407 sqlite3_mutex_enter(db->mutex);
408 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
409 && cnt++ < 5
drh8bfdf722009-06-19 14:06:03 +0000410 && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
danielk1977a185ddb2007-11-15 16:04:15 +0000411 sqlite3_reset(pStmt);
412 v->expired = 0;
danielk19778e556522007-11-13 10:30:24 +0000413 }
drh7950acd2009-04-10 23:11:31 +0000414 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
danielk1977a185ddb2007-11-15 16:04:15 +0000415 /* This case occurs after failing to recompile an sql statement.
416 ** The error message from the SQL compiler has already been loaded
417 ** into the database handle. This block copies the error message
418 ** from the database handle into the statement and sets the statement
419 ** program counter to 0 to ensure that when the statement is
420 ** finalized or reset the parser error message is available via
421 ** sqlite3_errmsg() and sqlite3_errcode().
422 */
423 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
drh633e6d52008-07-28 19:34:53 +0000424 sqlite3DbFree(db, v->zErrMsg);
danielk1977a185ddb2007-11-15 16:04:15 +0000425 if( !db->mallocFailed ){
426 v->zErrMsg = sqlite3DbStrDup(db, zErr);
427 } else {
428 v->zErrMsg = 0;
429 v->rc = SQLITE_NOMEM;
430 }
431 }
432 rc = sqlite3ApiExit(db, rc);
433 sqlite3_mutex_leave(db->mutex);
danielk19778e556522007-11-13 10:30:24 +0000434 }
danielk197776e8d1a2006-01-18 18:22:43 +0000435 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000436}
437
438/*
drheb2e1762004-05-27 01:53:56 +0000439** Extract the user data from a sqlite3_context structure and return a
440** pointer to it.
441*/
442void *sqlite3_user_data(sqlite3_context *p){
443 assert( p && p->pFunc );
444 return p->pFunc->pUserData;
445}
446
447/*
drhfa4a4b92008-03-19 21:45:51 +0000448** Extract the user data from a sqlite3_context structure and return a
449** pointer to it.
450*/
451sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
452 assert( p && p->pFunc );
453 return p->s.db;
454}
455
456/*
drhb7481e72006-09-16 21:45:14 +0000457** The following is the implementation of an SQL function that always
458** fails with an error message stating that the function is used in the
459** wrong context. The sqlite3_overload_function() API might construct
460** SQL function that use this routine so that the functions will exist
461** for name resolution but are actually overloaded by the xFindFunction
462** method of virtual tables.
463*/
464void sqlite3InvalidFunction(
465 sqlite3_context *context, /* The function calling context */
danielk197762c14b32008-11-19 09:05:26 +0000466 int NotUsed, /* Number of arguments to the function */
467 sqlite3_value **NotUsed2 /* Value of each argument */
drhb7481e72006-09-16 21:45:14 +0000468){
469 const char *zName = context->pFunc->zName;
470 char *zErr;
danielk197762c14b32008-11-19 09:05:26 +0000471 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbc6160b2009-04-08 15:45:31 +0000472 zErr = sqlite3_mprintf(
drhb7481e72006-09-16 21:45:14 +0000473 "unable to use function %s in the requested context", zName);
474 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000475 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000476}
477
478/*
drheb2e1762004-05-27 01:53:56 +0000479** Allocate or return the aggregate context for a user function. A new
480** context is allocated on the first call. Subsequent calls return the
481** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000482*/
483void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhb21c8cd2007-08-21 19:33:56 +0000484 Mem *pMem;
drh825c6622005-09-08 19:01:05 +0000485 assert( p && p->pFunc && p->pFunc->xStep );
drhb21c8cd2007-08-21 19:33:56 +0000486 assert( sqlite3_mutex_held(p->s.db->mutex) );
487 pMem = p->pMem;
drhd68eee02009-12-11 03:44:18 +0000488 testcase( nByte<0 );
drha10a34b2005-09-07 22:09:48 +0000489 if( (pMem->flags & MEM_Agg)==0 ){
drhd68eee02009-12-11 03:44:18 +0000490 if( nByte<=0 ){
danielk19775f096132008-03-28 15:44:09 +0000491 sqlite3VdbeMemReleaseExternal(pMem);
492 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000493 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000494 }else{
danielk19775f096132008-03-28 15:44:09 +0000495 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000496 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000497 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000498 if( pMem->z ){
499 memset(pMem->z, 0, nByte);
500 }
drheb2e1762004-05-27 01:53:56 +0000501 }
502 }
drhabfcea22005-09-06 20:36:48 +0000503 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000504}
505
506/*
danielk1977682f68b2004-06-05 10:22:17 +0000507** Return the auxilary data pointer, if any, for the iArg'th argument to
508** the user-function defined by pCtx.
509*/
510void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000511 VdbeFunc *pVdbeFunc;
512
513 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
514 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000515 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
516 return 0;
517 }
drhf92c7ff2004-06-19 15:40:23 +0000518 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000519}
520
521/*
522** Set the auxilary data pointer and delete function, for the iArg'th
523** argument to the user-function defined by pCtx. Any previous value is
524** deleted by calling the delete function specified when it was set.
525*/
526void sqlite3_set_auxdata(
527 sqlite3_context *pCtx,
528 int iArg,
529 void *pAux,
530 void (*xDelete)(void*)
531){
532 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000533 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000534 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000535
drhb21c8cd2007-08-21 19:33:56 +0000536 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000537 pVdbeFunc = pCtx->pVdbeFunc;
538 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000539 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000540 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000541 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000542 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000543 goto failed;
544 }
drh53f733c2005-09-16 02:38:09 +0000545 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000546 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000547 pVdbeFunc->nAux = iArg+1;
548 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000549 }
550
drhf92c7ff2004-06-19 15:40:23 +0000551 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000552 if( pAuxData->pAux && pAuxData->xDelete ){
553 pAuxData->xDelete(pAuxData->pAux);
554 }
555 pAuxData->pAux = pAux;
556 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000557 return;
558
559failed:
560 if( xDelete ){
561 xDelete(pAux);
562 }
danielk1977682f68b2004-06-05 10:22:17 +0000563}
564
shaneeec556d2008-10-12 00:27:53 +0000565#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000566/*
drheb2e1762004-05-27 01:53:56 +0000567** Return the number of times the Step function of a aggregate has been
568** called.
569**
drhcf85a512006-02-09 18:35:29 +0000570** This function is deprecated. Do not use it for new code. It is
571** provide only to avoid breaking legacy code. New aggregate function
572** implementations should keep their own counts within their aggregate
573** context.
drheb2e1762004-05-27 01:53:56 +0000574*/
575int sqlite3_aggregate_count(sqlite3_context *p){
shanee34c6472009-03-05 04:23:47 +0000576 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000577 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000578}
shaneeec556d2008-10-12 00:27:53 +0000579#endif
drheb2e1762004-05-27 01:53:56 +0000580
581/*
drh4f26d6c2004-05-26 23:25:30 +0000582** Return the number of columns in the result set for the statement pStmt.
583*/
584int sqlite3_column_count(sqlite3_stmt *pStmt){
585 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000586 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000587}
588
589/*
590** Return the number of values available from the current row of the
591** currently executing statement pStmt.
592*/
593int sqlite3_data_count(sqlite3_stmt *pStmt){
594 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000595 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000596 return pVm->nResColumn;
597}
598
599
600/*
601** Check to see if column iCol of the given statement is valid. If
602** it is, return a pointer to the Mem for the value of that column.
603** If iCol is not valid, return a pointer to a Mem which has a value
604** of NULL.
605*/
606static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000607 Vdbe *pVm;
608 int vals;
609 Mem *pOut;
610
611 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000612 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000613 sqlite3_mutex_enter(pVm->db->mutex);
614 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000615 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000616 }else{
danielk197741f5b042009-06-06 14:13:26 +0000617 /* If the value passed as the second argument is out of range, return
618 ** a pointer to the following static Mem object which contains the
619 ** value SQL NULL. Even though the Mem structure contains an element
620 ** of type i64, on certain architecture (x86) with certain compiler
621 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
622 ** instead of an 8-byte one. This all works fine, except that when
623 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
624 ** that a Mem structure is located on an 8-byte boundary. To prevent
625 ** this assert() from failing, when building with SQLITE_DEBUG defined
626 ** using gcc, force nullMem to be 8-byte aligned using the magical
627 ** __attribute__((aligned(8))) macro. */
628 static const Mem nullMem
629#if defined(SQLITE_DEBUG) && defined(__GNUC__)
630 __attribute__((aligned(8)))
631#endif
632 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
633
drh9b4ea4a2009-04-10 20:32:00 +0000634 if( pVm && ALWAYS(pVm->db) ){
drh27641702007-08-22 02:56:42 +0000635 sqlite3_mutex_enter(pVm->db->mutex);
636 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
637 }
drh32bc3f62007-08-21 20:25:39 +0000638 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000639 }
drh32bc3f62007-08-21 20:25:39 +0000640 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000641}
642
danielk19772e588c72005-12-09 14:25:08 +0000643/*
644** This function is called after invoking an sqlite3_value_XXX function on a
645** column value (i.e. a value returned by evaluating an SQL expression in the
646** select list of a SELECT statement) that may cause a malloc() failure. If
647** malloc() has failed, the threads mallocFailed flag is cleared and the result
648** code of statement pStmt set to SQLITE_NOMEM.
649**
drh32bc3f62007-08-21 20:25:39 +0000650** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000651**
652** sqlite3_column_int()
653** sqlite3_column_int64()
654** sqlite3_column_text()
655** sqlite3_column_text16()
656** sqlite3_column_real()
657** sqlite3_column_bytes()
658** sqlite3_column_bytes16()
659**
660** But not for sqlite3_column_blob(), which never calls malloc().
661*/
662static void columnMallocFailure(sqlite3_stmt *pStmt)
663{
664 /* If malloc() failed during an encoding conversion within an
665 ** sqlite3_column_XXX API, then set the return code of the statement to
666 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
667 ** and _finalize() will return NOMEM.
668 */
danielk197754f01982006-01-18 15:25:17 +0000669 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000670 if( p ){
671 p->rc = sqlite3ApiExit(p->db, p->rc);
672 sqlite3_mutex_leave(p->db->mutex);
673 }
danielk19772e588c72005-12-09 14:25:08 +0000674}
675
drh4f26d6c2004-05-26 23:25:30 +0000676/**************************** sqlite3_column_ *******************************
677** The following routines are used to access elements of the current row
678** in the result set.
679*/
danielk1977c572ef72004-05-27 09:28:41 +0000680const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000681 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000682 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000683 /* Even though there is no encoding conversion, value_blob() might
684 ** need to call malloc() to expand the result of a zeroblob()
685 ** expression.
686 */
687 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000688 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000689}
drh4f26d6c2004-05-26 23:25:30 +0000690int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000691 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
692 columnMallocFailure(pStmt);
693 return val;
drh4f26d6c2004-05-26 23:25:30 +0000694}
695int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000696 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
697 columnMallocFailure(pStmt);
698 return val;
drh4f26d6c2004-05-26 23:25:30 +0000699}
700double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000701 double val = sqlite3_value_double( columnMem(pStmt,i) );
702 columnMallocFailure(pStmt);
703 return val;
drh4f26d6c2004-05-26 23:25:30 +0000704}
705int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000706 int val = sqlite3_value_int( columnMem(pStmt,i) );
707 columnMallocFailure(pStmt);
708 return val;
drh4f26d6c2004-05-26 23:25:30 +0000709}
drhefad9992004-06-22 12:13:55 +0000710sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000711 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
712 columnMallocFailure(pStmt);
713 return val;
drh4f26d6c2004-05-26 23:25:30 +0000714}
715const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000716 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
717 columnMallocFailure(pStmt);
718 return val;
drh4f26d6c2004-05-26 23:25:30 +0000719}
drhd1e47332005-06-26 17:55:33 +0000720sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +0000721 Mem *pOut = columnMem(pStmt, i);
722 if( pOut->flags&MEM_Static ){
723 pOut->flags &= ~MEM_Static;
724 pOut->flags |= MEM_Ephem;
725 }
drh32bc3f62007-08-21 20:25:39 +0000726 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +0000727 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +0000728}
drh6c626082004-11-14 21:56:29 +0000729#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000730const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000731 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
732 columnMallocFailure(pStmt);
733 return val;
drh4f26d6c2004-05-26 23:25:30 +0000734}
drh6c626082004-11-14 21:56:29 +0000735#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000736int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000737 int iType = sqlite3_value_type( columnMem(pStmt,i) );
738 columnMallocFailure(pStmt);
739 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000740}
741
drh29d72102006-02-09 22:13:41 +0000742/* The following function is experimental and subject to change or
743** removal */
744/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
745** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
746**}
747*/
748
drh5e2517e2004-09-24 23:59:12 +0000749/*
750** Convert the N-th element of pStmt->pColName[] into a string using
751** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000752**
753** There are up to 5 names for each column. useType determines which
754** name is returned. Here are the names:
755**
756** 0 The column name as it should be displayed for output
757** 1 The datatype name for the column
758** 2 The name of the database that the column derives from
759** 3 The name of the table that the column derives from
760** 4 The name of the table column that the result column derives from
761**
762** If the result is not a simple column reference (if it is an expression
763** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000764*/
765static const void *columnName(
766 sqlite3_stmt *pStmt,
767 int N,
768 const void *(*xFunc)(Mem*),
769 int useType
770){
drh32bc3f62007-08-21 20:25:39 +0000771 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000772 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000773 int n;
drhc6c7fd52009-04-08 23:05:28 +0000774 sqlite3 *db = p->db;
drh32bc3f62007-08-21 20:25:39 +0000775
drh9b4ea4a2009-04-10 20:32:00 +0000776 assert( db!=0 );
777 n = sqlite3_column_count(pStmt);
778 if( N<n && N>=0 ){
779 N += useType*n;
780 sqlite3_mutex_enter(db->mutex);
781 assert( db->mallocFailed==0 );
782 ret = xFunc(&p->aColName[N]);
783 /* A malloc may have failed inside of the xFunc() call. If this
784 ** is the case, clear the mallocFailed flag and return NULL.
785 */
786 if( db->mallocFailed ){
787 db->mallocFailed = 0;
788 ret = 0;
drh32bc3f62007-08-21 20:25:39 +0000789 }
drh9b4ea4a2009-04-10 20:32:00 +0000790 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +0000791 }
danielk197700fd9572005-12-07 06:27:43 +0000792 return ret;
drh5e2517e2004-09-24 23:59:12 +0000793}
794
drh4f26d6c2004-05-26 23:25:30 +0000795/*
796** Return the name of the Nth column of the result set returned by SQL
797** statement pStmt.
798*/
799const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000800 return columnName(
801 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000802}
drhe590fbd2005-05-20 19:36:01 +0000803#ifndef SQLITE_OMIT_UTF16
804const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000805 return columnName(
806 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000807}
808#endif
drh4f26d6c2004-05-26 23:25:30 +0000809
810/*
drh3f913572008-03-22 01:07:17 +0000811** Constraint: If you have ENABLE_COLUMN_METADATA then you must
812** not define OMIT_DECLTYPE.
813*/
814#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
815# error "Must not define both SQLITE_OMIT_DECLTYPE \
816 and SQLITE_ENABLE_COLUMN_METADATA"
817#endif
818
819#ifndef SQLITE_OMIT_DECLTYPE
820/*
drh6c626082004-11-14 21:56:29 +0000821** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000822** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000823*/
824const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000825 return columnName(
826 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000827}
drh6c626082004-11-14 21:56:29 +0000828#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000829const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000830 return columnName(
831 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000832}
drh6c626082004-11-14 21:56:29 +0000833#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000834#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000835
danielk19774b1ae992006-02-10 03:06:10 +0000836#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000837/*
838** Return the name of the database from which a result column derives.
839** NULL is returned if the result column is an expression or constant or
840** anything else which is not an unabiguous reference to a database column.
841*/
842const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000843 return columnName(
844 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000845}
846#ifndef SQLITE_OMIT_UTF16
847const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000848 return columnName(
849 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000850}
851#endif /* SQLITE_OMIT_UTF16 */
852
853/*
854** Return the name of the table from which a result column derives.
855** NULL is returned if the result column is an expression or constant or
856** anything else which is not an unabiguous reference to a database column.
857*/
858const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000859 return columnName(
860 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000861}
862#ifndef SQLITE_OMIT_UTF16
863const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000864 return columnName(
865 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000866}
867#endif /* SQLITE_OMIT_UTF16 */
868
869/*
870** Return the name of the table column from which a result column derives.
871** NULL is returned if the result column is an expression or constant or
872** anything else which is not an unabiguous reference to a database column.
873*/
874const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000875 return columnName(
876 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000877}
878#ifndef SQLITE_OMIT_UTF16
879const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000880 return columnName(
881 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000882}
883#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000884#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000885
886
drh4f26d6c2004-05-26 23:25:30 +0000887/******************************* sqlite3_bind_ ***************************
888**
889** Routines used to attach values to wildcards in a compiled SQL statement.
890*/
891/*
892** Unbind the value bound to variable i in virtual machine p. This is the
893** the same as binding a NULL value to the column. If the "i" parameter is
894** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
895**
drh488e7b62008-10-06 12:46:43 +0000896** A successful evaluation of this routine acquires the mutex on p.
897** the mutex is released if any kind of error occurs.
898**
drh4f26d6c2004-05-26 23:25:30 +0000899** The error code stored in database p->db is overwritten with the return
900** value in any case.
901*/
902static int vdbeUnbind(Vdbe *p, int i){
903 Mem *pVar;
drh488e7b62008-10-06 12:46:43 +0000904 if( p==0 ) return SQLITE_MISUSE;
905 sqlite3_mutex_enter(p->db->mutex);
906 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
907 sqlite3Error(p->db, SQLITE_MISUSE, 0);
908 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000909 return SQLITE_MISUSE;
910 }
911 if( i<1 || i>p->nVar ){
912 sqlite3Error(p->db, SQLITE_RANGE, 0);
drh488e7b62008-10-06 12:46:43 +0000913 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000914 return SQLITE_RANGE;
915 }
916 i--;
drh290c1942004-08-21 17:54:45 +0000917 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000918 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000919 pVar->flags = MEM_Null;
920 sqlite3Error(p->db, SQLITE_OK, 0);
dan937d0de2009-10-15 18:35:38 +0000921
dan1d2ce4f2009-10-19 18:11:09 +0000922 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
923 ** binding a new value to this variable invalidates the current query plan.
924 */
drh823e09a2009-10-19 20:15:38 +0000925 if( p->isPrepareV2 &&
926 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
927 ){
dan937d0de2009-10-15 18:35:38 +0000928 p->expired = 1;
929 }
drh4f26d6c2004-05-26 23:25:30 +0000930 return SQLITE_OK;
931}
932
933/*
drh5e2517e2004-09-24 23:59:12 +0000934** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000935*/
drh5e2517e2004-09-24 23:59:12 +0000936static int bindText(
drh605264d2007-08-21 15:13:19 +0000937 sqlite3_stmt *pStmt, /* The statement to bind against */
938 int i, /* Index of the parameter to bind */
939 const void *zData, /* Pointer to the data to be bound */
940 int nData, /* Number of bytes of data to be bound */
941 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +0000942 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +0000943){
944 Vdbe *p = (Vdbe *)pStmt;
945 Mem *pVar;
946 int rc;
947
948 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +0000949 if( rc==SQLITE_OK ){
950 if( zData!=0 ){
951 pVar = &p->aVar[i-1];
952 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
953 if( rc==SQLITE_OK && encoding!=0 ){
954 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
955 }
956 sqlite3Error(p->db, rc, 0);
957 rc = sqlite3ApiExit(p->db, rc);
drh27641702007-08-22 02:56:42 +0000958 }
drh488e7b62008-10-06 12:46:43 +0000959 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000960 }
drh605264d2007-08-21 15:13:19 +0000961 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000962}
drh5e2517e2004-09-24 23:59:12 +0000963
964
965/*
966** Bind a blob value to an SQL statement variable.
967*/
968int sqlite3_bind_blob(
969 sqlite3_stmt *pStmt,
970 int i,
971 const void *zData,
972 int nData,
973 void (*xDel)(void*)
974){
975 return bindText(pStmt, i, zData, nData, xDel, 0);
976}
drh4f26d6c2004-05-26 23:25:30 +0000977int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
978 int rc;
979 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000980 rc = vdbeUnbind(p, i);
981 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000982 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +0000983 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000984 }
danielk1977f4618892004-06-28 13:09:11 +0000985 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000986}
987int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000988 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000989}
drhefad9992004-06-22 12:13:55 +0000990int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000991 int rc;
992 Vdbe *p = (Vdbe *)pStmt;
993 rc = vdbeUnbind(p, i);
994 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000995 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +0000996 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000997 }
998 return rc;
999}
drh605264d2007-08-21 15:13:19 +00001000int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1001 int rc;
1002 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +00001003 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001004 if( rc==SQLITE_OK ){
1005 sqlite3_mutex_leave(p->db->mutex);
1006 }
drh605264d2007-08-21 15:13:19 +00001007 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001008}
1009int sqlite3_bind_text(
1010 sqlite3_stmt *pStmt,
1011 int i,
1012 const char *zData,
1013 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001014 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001015){
drh5e2517e2004-09-24 23:59:12 +00001016 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001017}
drh6c626082004-11-14 21:56:29 +00001018#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001019int sqlite3_bind_text16(
1020 sqlite3_stmt *pStmt,
1021 int i,
1022 const void *zData,
1023 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001024 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001025){
drh5e2517e2004-09-24 23:59:12 +00001026 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001027}
drh6c626082004-11-14 21:56:29 +00001028#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001029int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1030 int rc;
drh29def562009-04-14 12:43:33 +00001031 switch( pValue->type ){
drh29def562009-04-14 12:43:33 +00001032 case SQLITE_INTEGER: {
1033 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1034 break;
1035 }
1036 case SQLITE_FLOAT: {
1037 rc = sqlite3_bind_double(pStmt, i, pValue->r);
1038 break;
1039 }
1040 case SQLITE_BLOB: {
1041 if( pValue->flags & MEM_Zero ){
1042 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1043 }else{
1044 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1045 }
1046 break;
1047 }
1048 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001049 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1050 pValue->enc);
1051 break;
1052 }
1053 default: {
1054 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001055 break;
1056 }
danielk197726e41442006-06-14 15:16:35 +00001057 }
1058 return rc;
1059}
drhb026e052007-05-02 01:34:31 +00001060int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1061 int rc;
1062 Vdbe *p = (Vdbe *)pStmt;
1063 rc = vdbeUnbind(p, i);
1064 if( rc==SQLITE_OK ){
1065 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001066 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001067 }
1068 return rc;
1069}
drh75f6a032004-07-15 14:15:00 +00001070
1071/*
1072** Return the number of wildcards that can be potentially bound to.
1073** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001074*/
1075int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001076 Vdbe *p = (Vdbe*)pStmt;
1077 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001078}
drh895d7472004-08-20 16:02:39 +00001079
1080/*
drhfa6bc002004-09-07 16:19:52 +00001081** Create a mapping from variable numbers to variable names
1082** in the Vdbe.azVar[] array, if such a mapping does not already
1083** exist.
drh895d7472004-08-20 16:02:39 +00001084*/
drhfa6bc002004-09-07 16:19:52 +00001085static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001086 if( !p->okVar ){
drh0f5ea0b2009-04-09 14:02:44 +00001087 int j;
1088 Op *pOp;
drh605264d2007-08-21 15:13:19 +00001089 sqlite3_mutex_enter(p->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001090 /* The race condition here is harmless. If two threads call this
1091 ** routine on the same Vdbe at the same time, they both might end
1092 ** up initializing the Vdbe.azVar[] array. That is a little extra
1093 ** work but it results in the same answer.
1094 */
1095 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1096 if( pOp->opcode==OP_Variable ){
1097 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1098 p->azVar[pOp->p1-1] = pOp->p4.z;
drh895d7472004-08-20 16:02:39 +00001099 }
1100 }
drh0f5ea0b2009-04-09 14:02:44 +00001101 p->okVar = 1;
drh605264d2007-08-21 15:13:19 +00001102 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001103 }
drhfa6bc002004-09-07 16:19:52 +00001104}
1105
1106/*
1107** Return the name of a wildcard parameter. Return NULL if the index
1108** is out of range or if the wildcard is unnamed.
1109**
1110** The result is always UTF-8.
1111*/
1112const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1113 Vdbe *p = (Vdbe*)pStmt;
1114 if( p==0 || i<1 || i>p->nVar ){
1115 return 0;
1116 }
1117 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001118 return p->azVar[i-1];
1119}
drhfa6bc002004-09-07 16:19:52 +00001120
1121/*
1122** Given a wildcard parameter name, return the index of the variable
1123** with that name. If there is no variable with the given name,
1124** return 0.
1125*/
drh5f18a222009-11-26 14:01:53 +00001126int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
drhfa6bc002004-09-07 16:19:52 +00001127 int i;
1128 if( p==0 ){
1129 return 0;
1130 }
1131 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +00001132 if( zName ){
1133 for(i=0; i<p->nVar; i++){
1134 const char *z = p->azVar[i];
drh5f18a222009-11-26 14:01:53 +00001135 if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
drh6a8903c2004-12-10 18:00:04 +00001136 return i+1;
1137 }
drhfa6bc002004-09-07 16:19:52 +00001138 }
1139 }
1140 return 0;
1141}
drh5f18a222009-11-26 14:01:53 +00001142int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1143 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1144}
drhf8db1bc2005-04-22 02:38:37 +00001145
drh51942bc2005-06-12 22:01:42 +00001146/*
drhf8db1bc2005-04-22 02:38:37 +00001147** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001148*/
shane145834a2008-09-04 12:03:42 +00001149int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001150 Vdbe *pFrom = (Vdbe*)pFromStmt;
1151 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001152 int i;
1153 assert( pTo->db==pFrom->db );
1154 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001155 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001156 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001157 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001158 }
drh27641702007-08-22 02:56:42 +00001159 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001160 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001161}
drh51942bc2005-06-12 22:01:42 +00001162
shaneeec556d2008-10-12 00:27:53 +00001163#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001164/*
shane145834a2008-09-04 12:03:42 +00001165** Deprecated external interface. Internal/core SQLite code
1166** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001167**
1168** Is is misuse to call this routine with statements from different
1169** database connections. But as this is a deprecated interface, we
1170** will not bother to check for that condition.
1171**
1172** If the two statements contain a different number of bindings, then
1173** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1174** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001175*/
1176int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001177 Vdbe *pFrom = (Vdbe*)pFromStmt;
1178 Vdbe *pTo = (Vdbe*)pToStmt;
1179 if( pFrom->nVar!=pTo->nVar ){
1180 return SQLITE_ERROR;
1181 }
danc94b8592009-10-20 07:01:24 +00001182 if( pTo->isPrepareV2 && pTo->expmask ){
1183 pTo->expired = 1;
1184 }
1185 if( pFrom->isPrepareV2 && pFrom->expmask ){
1186 pFrom->expired = 1;
1187 }
shane145834a2008-09-04 12:03:42 +00001188 return sqlite3TransferBindings(pFromStmt, pToStmt);
1189}
shaneeec556d2008-10-12 00:27:53 +00001190#endif
shane145834a2008-09-04 12:03:42 +00001191
1192/*
drh51942bc2005-06-12 22:01:42 +00001193** Return the sqlite3* database handle to which the prepared statement given
1194** in the argument belongs. This is the same database handle that was
1195** the first argument to the sqlite3_prepare() that was used to create
1196** the statement in the first place.
1197*/
1198sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1199 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1200}
drhbb5a9c32008-06-19 02:52:25 +00001201
1202/*
1203** Return a pointer to the next prepared statement after pStmt associated
1204** with database connection pDb. If pStmt is NULL, return the first
1205** prepared statement for the database connection. Return NULL if there
1206** are no more.
1207*/
1208sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1209 sqlite3_stmt *pNext;
1210 sqlite3_mutex_enter(pDb->mutex);
1211 if( pStmt==0 ){
1212 pNext = (sqlite3_stmt*)pDb->pVdbe;
1213 }else{
1214 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1215 }
1216 sqlite3_mutex_leave(pDb->mutex);
1217 return pNext;
1218}
drhd1d38482008-10-07 23:46:38 +00001219
1220/*
1221** Return the value of a status counter for a prepared statement
1222*/
1223int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1224 Vdbe *pVdbe = (Vdbe*)pStmt;
1225 int v = pVdbe->aCounter[op-1];
1226 if( resetFlag ) pVdbe->aCounter[op-1] = 0;
1227 return v;
1228}