blob: b9ee52b6ce2c75f246b25b6735ef94790845af00 [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.
danielk1977822a5162008-05-16 04:51:54 +000015**
drh4c8555f2009-06-25 01:47:11 +000016** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
drh4f26d6c2004-05-26 23:25:30 +000017*/
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
shaneeec556d2008-10-12 00:27:53 +000021#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +000022/*
23** Return TRUE (non-zero) of the statement supplied as an argument needs
24** to be recompiled. A statement needs to be recompiled whenever the
25** execution environment changes in a way that would alter the program
26** that sqlite3_prepare() generates. For example, if new functions or
27** collating sequences are registered or if an authorizer function is
28** added or changed.
drhd89bd002005-01-22 03:03:54 +000029*/
30int sqlite3_expired(sqlite3_stmt *pStmt){
31 Vdbe *p = (Vdbe*)pStmt;
32 return p==0 || p->expired;
33}
shaneeec556d2008-10-12 00:27:53 +000034#endif
drhd89bd002005-01-22 03:03:54 +000035
drhe30f4422007-08-21 16:15:55 +000036/*
37** The following routine destroys a virtual machine that is created by
38** the sqlite3_compile() routine. The integer returned is an SQLITE_
39** success/failure code that describes the result of executing the virtual
40** machine.
41**
42** This routine sets the error code and string returned by
43** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
44*/
45int sqlite3_finalize(sqlite3_stmt *pStmt){
46 int rc;
47 if( pStmt==0 ){
48 rc = SQLITE_OK;
49 }else{
50 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06 +000051 sqlite3 *db = v->db;
drh18472fa2008-10-07 15:25:48 +000052#if SQLITE_THREADSAFE
drh86f8c192007-08-22 00:39:19 +000053 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +000054#endif
drh86f8c192007-08-22 00:39:19 +000055 sqlite3_mutex_enter(mutex);
drhe30f4422007-08-21 16:15:55 +000056 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +000057 rc = sqlite3ApiExit(db, rc);
drh86f8c192007-08-22 00:39:19 +000058 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +000059 }
60 return rc;
61}
62
63/*
64** Terminate the current execution of an SQL statement and reset it
65** back to its starting state so that it can be reused. A success code from
66** the prior execution is returned.
67**
68** This routine sets the error code and string returned by
69** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
70*/
71int sqlite3_reset(sqlite3_stmt *pStmt){
72 int rc;
73 if( pStmt==0 ){
74 rc = SQLITE_OK;
75 }else{
76 Vdbe *v = (Vdbe*)pStmt;
77 sqlite3_mutex_enter(v->db->mutex);
drhc890fec2008-08-01 20:10:08 +000078 rc = sqlite3VdbeReset(v);
dane0af83a2009-09-08 19:15:01 +000079 sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
drhe30f4422007-08-21 16:15:55 +000080 assert( (rc & (v->db->errMask))==rc );
danielk1977238746a2009-03-19 18:51:06 +000081 rc = sqlite3ApiExit(v->db, rc);
drhe30f4422007-08-21 16:15:55 +000082 sqlite3_mutex_leave(v->db->mutex);
83 }
84 return rc;
85}
86
87/*
88** Set all the parameters in the compiled SQL statement to NULL.
89*/
90int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
91 int i;
92 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +000093 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +000094#if SQLITE_THREADSAFE
drh7e8b8482008-01-23 03:03:05 +000095 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
96#endif
97 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +000098 for(i=0; i<p->nVar; i++){
99 sqlite3VdbeMemRelease(&p->aVar[i]);
100 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000101 }
danc94b8592009-10-20 07:01:24 +0000102 if( p->isPrepareV2 && p->expmask ){
103 p->expired = 1;
104 }
drh7e8b8482008-01-23 03:03:05 +0000105 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000106 return rc;
107}
108
109
drh4f26d6c2004-05-26 23:25:30 +0000110/**************************** sqlite3_value_ *******************************
111** The following routines extract information from a Mem or sqlite3_value
112** structure.
113*/
114const void *sqlite3_value_blob(sqlite3_value *pVal){
115 Mem *p = (Mem*)pVal;
116 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000117 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000118 p->flags &= ~MEM_Str;
119 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000120 return p->z;
121 }else{
122 return sqlite3_value_text(pVal);
123 }
124}
125int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000126 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000127}
128int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000129 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000130}
131double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000132 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000133}
134int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000135 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000136}
drhefad9992004-06-22 12:13:55 +0000137sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000138 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000139}
140const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000141 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000142}
drh6c626082004-11-14 21:56:29 +0000143#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000144const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000145 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000146}
danielk1977d8123362004-06-12 09:25:12 +0000147const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000148 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000149}
150const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000151 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000152}
drh6c626082004-11-14 21:56:29 +0000153#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000154int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000155 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000156}
157
158/**************************** sqlite3_result_ *******************************
159** The following routines are used by user-defined functions to specify
160** the function result.
drh4c8555f2009-06-25 01:47:11 +0000161**
162** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
163** result as a string or blob but if the string or blob is too large, it
164** then sets the error code to SQLITE_TOOBIG
drh4f26d6c2004-05-26 23:25:30 +0000165*/
drh4c8555f2009-06-25 01:47:11 +0000166static void setResultStrOrError(
167 sqlite3_context *pCtx, /* Function context */
168 const char *z, /* String pointer */
169 int n, /* Bytes in string, or negative */
170 u8 enc, /* Encoding of z. 0 for BLOBs */
171 void (*xDel)(void*) /* Destructor function */
172){
173 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
174 sqlite3_result_error_toobig(pCtx);
175 }
176}
drh4f26d6c2004-05-26 23:25:30 +0000177void sqlite3_result_blob(
178 sqlite3_context *pCtx,
179 const void *z,
180 int n,
danielk1977d8123362004-06-12 09:25:12 +0000181 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000182){
drh5708d2d2005-06-22 10:53:59 +0000183 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000184 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000185 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000186}
187void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000188 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000189 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
190}
191void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000192 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000193 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000194 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000195}
danielk1977a1686c92006-01-23 07:52:37 +0000196#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000197void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000198 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000199 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000200 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000201}
danielk1977a1686c92006-01-23 07:52:37 +0000202#endif
drhf4479502004-05-27 03:12:53 +0000203void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000204 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000205 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
206}
207void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000208 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000209 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
210}
211void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000212 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000213 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000214}
215void sqlite3_result_text(
216 sqlite3_context *pCtx,
217 const char *z,
218 int n,
danielk1977d8123362004-06-12 09:25:12 +0000219 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000220){
drh32bc3f62007-08-21 20:25:39 +0000221 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000222 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000223}
drh6c626082004-11-14 21:56:29 +0000224#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000225void sqlite3_result_text16(
226 sqlite3_context *pCtx,
227 const void *z,
228 int n,
danielk1977d8123362004-06-12 09:25:12 +0000229 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000230){
drh32bc3f62007-08-21 20:25:39 +0000231 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000232 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000233}
234void sqlite3_result_text16be(
235 sqlite3_context *pCtx,
236 const void *z,
237 int n,
238 void (*xDel)(void *)
239){
drh32bc3f62007-08-21 20:25:39 +0000240 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000241 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000242}
243void sqlite3_result_text16le(
244 sqlite3_context *pCtx,
245 const void *z,
246 int n,
247 void (*xDel)(void *)
248){
drh32bc3f62007-08-21 20:25:39 +0000249 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000250 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000251}
drh6c626082004-11-14 21:56:29 +0000252#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000253void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000254 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000255 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000256}
drhb026e052007-05-02 01:34:31 +0000257void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000258 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000259 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
260}
drh69544ec2008-02-06 14:11:34 +0000261void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
262 pCtx->isError = errCode;
drh51c7d862009-04-27 18:46:06 +0000263 if( pCtx->s.flags & MEM_Null ){
264 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
265 SQLITE_UTF8, SQLITE_STATIC);
266 }
drh69544ec2008-02-06 14:11:34 +0000267}
drh4f26d6c2004-05-26 23:25:30 +0000268
drha0206bc2007-05-08 15:15:02 +0000269/* Force an SQLITE_TOOBIG error. */
270void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000271 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000272 pCtx->isError = SQLITE_TOOBIG;
273 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
274 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000275}
276
danielk1977a1644fd2007-08-29 12:31:25 +0000277/* An SQLITE_NOMEM error. */
278void sqlite3_result_error_nomem(sqlite3_context *pCtx){
279 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
280 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000281 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000282 pCtx->s.db->mallocFailed = 1;
283}
drh4f26d6c2004-05-26 23:25:30 +0000284
285/*
286** Execute the statement pStmt, either until a row of data is ready, the
287** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000288**
289** This routine implements the bulk of the logic behind the sqlite_step()
290** API. The only thing omitted is the automatic recompile if a
291** schema change has occurred. That detail is handled by the
292** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000293*/
drhb900aaf2006-11-09 00:24:53 +0000294static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000295 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000296 int rc;
297
danielk1977a185ddb2007-11-15 16:04:15 +0000298 assert(p);
299 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000300 return SQLITE_MISUSE;
301 }
302
danielk1977efaaf572006-01-16 11:29:19 +0000303 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000304 db = p->db;
drhc456e572008-08-11 18:44:58 +0000305 if( db->mallocFailed ){
306 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;
403 if( pStmt ){
404 int cnt = 0;
405 Vdbe *v = (Vdbe*)pStmt;
406 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;
drha10a34b2005-09-07 22:09:48 +0000488 if( (pMem->flags & MEM_Agg)==0 ){
489 if( nByte==0 ){
danielk19775f096132008-03-28 15:44:09 +0000490 sqlite3VdbeMemReleaseExternal(pMem);
491 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000492 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000493 }else{
danielk19775f096132008-03-28 15:44:09 +0000494 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000495 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000496 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000497 if( pMem->z ){
498 memset(pMem->z, 0, nByte);
499 }
drheb2e1762004-05-27 01:53:56 +0000500 }
501 }
drhabfcea22005-09-06 20:36:48 +0000502 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000503}
504
505/*
danielk1977682f68b2004-06-05 10:22:17 +0000506** Return the auxilary data pointer, if any, for the iArg'th argument to
507** the user-function defined by pCtx.
508*/
509void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000510 VdbeFunc *pVdbeFunc;
511
512 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
513 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000514 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
515 return 0;
516 }
drhf92c7ff2004-06-19 15:40:23 +0000517 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000518}
519
520/*
521** Set the auxilary data pointer and delete function, for the iArg'th
522** argument to the user-function defined by pCtx. Any previous value is
523** deleted by calling the delete function specified when it was set.
524*/
525void sqlite3_set_auxdata(
526 sqlite3_context *pCtx,
527 int iArg,
528 void *pAux,
529 void (*xDelete)(void*)
530){
531 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000532 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000533 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000534
drhb21c8cd2007-08-21 19:33:56 +0000535 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000536 pVdbeFunc = pCtx->pVdbeFunc;
537 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000538 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000539 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000540 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000541 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000542 goto failed;
543 }
drh53f733c2005-09-16 02:38:09 +0000544 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000545 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000546 pVdbeFunc->nAux = iArg+1;
547 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000548 }
549
drhf92c7ff2004-06-19 15:40:23 +0000550 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000551 if( pAuxData->pAux && pAuxData->xDelete ){
552 pAuxData->xDelete(pAuxData->pAux);
553 }
554 pAuxData->pAux = pAux;
555 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000556 return;
557
558failed:
559 if( xDelete ){
560 xDelete(pAux);
561 }
danielk1977682f68b2004-06-05 10:22:17 +0000562}
563
shaneeec556d2008-10-12 00:27:53 +0000564#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000565/*
drheb2e1762004-05-27 01:53:56 +0000566** Return the number of times the Step function of a aggregate has been
567** called.
568**
drhcf85a512006-02-09 18:35:29 +0000569** This function is deprecated. Do not use it for new code. It is
570** provide only to avoid breaking legacy code. New aggregate function
571** implementations should keep their own counts within their aggregate
572** context.
drheb2e1762004-05-27 01:53:56 +0000573*/
574int sqlite3_aggregate_count(sqlite3_context *p){
shanee34c6472009-03-05 04:23:47 +0000575 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000576 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000577}
shaneeec556d2008-10-12 00:27:53 +0000578#endif
drheb2e1762004-05-27 01:53:56 +0000579
580/*
drh4f26d6c2004-05-26 23:25:30 +0000581** Return the number of columns in the result set for the statement pStmt.
582*/
583int sqlite3_column_count(sqlite3_stmt *pStmt){
584 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000585 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000586}
587
588/*
589** Return the number of values available from the current row of the
590** currently executing statement pStmt.
591*/
592int sqlite3_data_count(sqlite3_stmt *pStmt){
593 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000594 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000595 return pVm->nResColumn;
596}
597
598
599/*
600** Check to see if column iCol of the given statement is valid. If
601** it is, return a pointer to the Mem for the value of that column.
602** If iCol is not valid, return a pointer to a Mem which has a value
603** of NULL.
604*/
605static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000606 Vdbe *pVm;
607 int vals;
608 Mem *pOut;
609
610 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000611 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000612 sqlite3_mutex_enter(pVm->db->mutex);
613 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000614 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000615 }else{
danielk197741f5b042009-06-06 14:13:26 +0000616 /* If the value passed as the second argument is out of range, return
617 ** a pointer to the following static Mem object which contains the
618 ** value SQL NULL. Even though the Mem structure contains an element
619 ** of type i64, on certain architecture (x86) with certain compiler
620 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
621 ** instead of an 8-byte one. This all works fine, except that when
622 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
623 ** that a Mem structure is located on an 8-byte boundary. To prevent
624 ** this assert() from failing, when building with SQLITE_DEBUG defined
625 ** using gcc, force nullMem to be 8-byte aligned using the magical
626 ** __attribute__((aligned(8))) macro. */
627 static const Mem nullMem
628#if defined(SQLITE_DEBUG) && defined(__GNUC__)
629 __attribute__((aligned(8)))
630#endif
631 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
632
drh9b4ea4a2009-04-10 20:32:00 +0000633 if( pVm && ALWAYS(pVm->db) ){
drh27641702007-08-22 02:56:42 +0000634 sqlite3_mutex_enter(pVm->db->mutex);
635 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
636 }
drh32bc3f62007-08-21 20:25:39 +0000637 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000638 }
drh32bc3f62007-08-21 20:25:39 +0000639 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000640}
641
danielk19772e588c72005-12-09 14:25:08 +0000642/*
643** This function is called after invoking an sqlite3_value_XXX function on a
644** column value (i.e. a value returned by evaluating an SQL expression in the
645** select list of a SELECT statement) that may cause a malloc() failure. If
646** malloc() has failed, the threads mallocFailed flag is cleared and the result
647** code of statement pStmt set to SQLITE_NOMEM.
648**
drh32bc3f62007-08-21 20:25:39 +0000649** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000650**
651** sqlite3_column_int()
652** sqlite3_column_int64()
653** sqlite3_column_text()
654** sqlite3_column_text16()
655** sqlite3_column_real()
656** sqlite3_column_bytes()
657** sqlite3_column_bytes16()
658**
659** But not for sqlite3_column_blob(), which never calls malloc().
660*/
661static void columnMallocFailure(sqlite3_stmt *pStmt)
662{
663 /* If malloc() failed during an encoding conversion within an
664 ** sqlite3_column_XXX API, then set the return code of the statement to
665 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
666 ** and _finalize() will return NOMEM.
667 */
danielk197754f01982006-01-18 15:25:17 +0000668 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000669 if( p ){
670 p->rc = sqlite3ApiExit(p->db, p->rc);
671 sqlite3_mutex_leave(p->db->mutex);
672 }
danielk19772e588c72005-12-09 14:25:08 +0000673}
674
drh4f26d6c2004-05-26 23:25:30 +0000675/**************************** sqlite3_column_ *******************************
676** The following routines are used to access elements of the current row
677** in the result set.
678*/
danielk1977c572ef72004-05-27 09:28:41 +0000679const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000680 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000681 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000682 /* Even though there is no encoding conversion, value_blob() might
683 ** need to call malloc() to expand the result of a zeroblob()
684 ** expression.
685 */
686 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000687 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000688}
drh4f26d6c2004-05-26 23:25:30 +0000689int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000690 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
691 columnMallocFailure(pStmt);
692 return val;
drh4f26d6c2004-05-26 23:25:30 +0000693}
694int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000695 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
696 columnMallocFailure(pStmt);
697 return val;
drh4f26d6c2004-05-26 23:25:30 +0000698}
699double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000700 double val = sqlite3_value_double( columnMem(pStmt,i) );
701 columnMallocFailure(pStmt);
702 return val;
drh4f26d6c2004-05-26 23:25:30 +0000703}
704int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000705 int val = sqlite3_value_int( columnMem(pStmt,i) );
706 columnMallocFailure(pStmt);
707 return val;
drh4f26d6c2004-05-26 23:25:30 +0000708}
drhefad9992004-06-22 12:13:55 +0000709sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000710 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
711 columnMallocFailure(pStmt);
712 return val;
drh4f26d6c2004-05-26 23:25:30 +0000713}
714const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000715 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
716 columnMallocFailure(pStmt);
717 return val;
drh4f26d6c2004-05-26 23:25:30 +0000718}
drhd1e47332005-06-26 17:55:33 +0000719sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +0000720 Mem *pOut = columnMem(pStmt, i);
721 if( pOut->flags&MEM_Static ){
722 pOut->flags &= ~MEM_Static;
723 pOut->flags |= MEM_Ephem;
724 }
drh32bc3f62007-08-21 20:25:39 +0000725 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +0000726 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +0000727}
drh6c626082004-11-14 21:56:29 +0000728#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000729const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000730 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
731 columnMallocFailure(pStmt);
732 return val;
drh4f26d6c2004-05-26 23:25:30 +0000733}
drh6c626082004-11-14 21:56:29 +0000734#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000735int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000736 int iType = sqlite3_value_type( columnMem(pStmt,i) );
737 columnMallocFailure(pStmt);
738 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000739}
740
drh29d72102006-02-09 22:13:41 +0000741/* The following function is experimental and subject to change or
742** removal */
743/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
744** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
745**}
746*/
747
drh5e2517e2004-09-24 23:59:12 +0000748/*
749** Convert the N-th element of pStmt->pColName[] into a string using
750** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000751**
752** There are up to 5 names for each column. useType determines which
753** name is returned. Here are the names:
754**
755** 0 The column name as it should be displayed for output
756** 1 The datatype name for the column
757** 2 The name of the database that the column derives from
758** 3 The name of the table that the column derives from
759** 4 The name of the table column that the result column derives from
760**
761** If the result is not a simple column reference (if it is an expression
762** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000763*/
764static const void *columnName(
765 sqlite3_stmt *pStmt,
766 int N,
767 const void *(*xFunc)(Mem*),
768 int useType
769){
drh32bc3f62007-08-21 20:25:39 +0000770 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000771 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000772 int n;
drhc6c7fd52009-04-08 23:05:28 +0000773 sqlite3 *db = p->db;
drh32bc3f62007-08-21 20:25:39 +0000774
drh9b4ea4a2009-04-10 20:32:00 +0000775 assert( db!=0 );
776 n = sqlite3_column_count(pStmt);
777 if( N<n && N>=0 ){
778 N += useType*n;
779 sqlite3_mutex_enter(db->mutex);
780 assert( db->mallocFailed==0 );
781 ret = xFunc(&p->aColName[N]);
782 /* A malloc may have failed inside of the xFunc() call. If this
783 ** is the case, clear the mallocFailed flag and return NULL.
784 */
785 if( db->mallocFailed ){
786 db->mallocFailed = 0;
787 ret = 0;
drh32bc3f62007-08-21 20:25:39 +0000788 }
drh9b4ea4a2009-04-10 20:32:00 +0000789 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +0000790 }
danielk197700fd9572005-12-07 06:27:43 +0000791 return ret;
drh5e2517e2004-09-24 23:59:12 +0000792}
793
drh4f26d6c2004-05-26 23:25:30 +0000794/*
795** Return the name of the Nth column of the result set returned by SQL
796** statement pStmt.
797*/
798const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000799 return columnName(
800 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000801}
drhe590fbd2005-05-20 19:36:01 +0000802#ifndef SQLITE_OMIT_UTF16
803const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000804 return columnName(
805 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000806}
807#endif
drh4f26d6c2004-05-26 23:25:30 +0000808
809/*
drh3f913572008-03-22 01:07:17 +0000810** Constraint: If you have ENABLE_COLUMN_METADATA then you must
811** not define OMIT_DECLTYPE.
812*/
813#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
814# error "Must not define both SQLITE_OMIT_DECLTYPE \
815 and SQLITE_ENABLE_COLUMN_METADATA"
816#endif
817
818#ifndef SQLITE_OMIT_DECLTYPE
819/*
drh6c626082004-11-14 21:56:29 +0000820** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000821** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000822*/
823const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000824 return columnName(
825 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000826}
drh6c626082004-11-14 21:56:29 +0000827#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000828const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000829 return columnName(
830 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000831}
drh6c626082004-11-14 21:56:29 +0000832#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000833#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000834
danielk19774b1ae992006-02-10 03:06:10 +0000835#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000836/*
837** Return the name of the database from which a result column derives.
838** NULL is returned if the result column is an expression or constant or
839** anything else which is not an unabiguous reference to a database column.
840*/
841const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000842 return columnName(
843 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000844}
845#ifndef SQLITE_OMIT_UTF16
846const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000847 return columnName(
848 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000849}
850#endif /* SQLITE_OMIT_UTF16 */
851
852/*
853** Return the name of the table from which a result column derives.
854** NULL is returned if the result column is an expression or constant or
855** anything else which is not an unabiguous reference to a database column.
856*/
857const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000858 return columnName(
859 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000860}
861#ifndef SQLITE_OMIT_UTF16
862const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000863 return columnName(
864 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000865}
866#endif /* SQLITE_OMIT_UTF16 */
867
868/*
869** Return the name of the table column from which a result column derives.
870** NULL is returned if the result column is an expression or constant or
871** anything else which is not an unabiguous reference to a database column.
872*/
873const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000874 return columnName(
875 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000876}
877#ifndef SQLITE_OMIT_UTF16
878const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000879 return columnName(
880 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000881}
882#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000883#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000884
885
drh4f26d6c2004-05-26 23:25:30 +0000886/******************************* sqlite3_bind_ ***************************
887**
888** Routines used to attach values to wildcards in a compiled SQL statement.
889*/
890/*
891** Unbind the value bound to variable i in virtual machine p. This is the
892** the same as binding a NULL value to the column. If the "i" parameter is
893** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
894**
drh488e7b62008-10-06 12:46:43 +0000895** A successful evaluation of this routine acquires the mutex on p.
896** the mutex is released if any kind of error occurs.
897**
drh4f26d6c2004-05-26 23:25:30 +0000898** The error code stored in database p->db is overwritten with the return
899** value in any case.
900*/
901static int vdbeUnbind(Vdbe *p, int i){
902 Mem *pVar;
drh488e7b62008-10-06 12:46:43 +0000903 if( p==0 ) return SQLITE_MISUSE;
904 sqlite3_mutex_enter(p->db->mutex);
905 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
906 sqlite3Error(p->db, SQLITE_MISUSE, 0);
907 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000908 return SQLITE_MISUSE;
909 }
910 if( i<1 || i>p->nVar ){
911 sqlite3Error(p->db, SQLITE_RANGE, 0);
drh488e7b62008-10-06 12:46:43 +0000912 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000913 return SQLITE_RANGE;
914 }
915 i--;
drh290c1942004-08-21 17:54:45 +0000916 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000917 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000918 pVar->flags = MEM_Null;
919 sqlite3Error(p->db, SQLITE_OK, 0);
dan937d0de2009-10-15 18:35:38 +0000920
dan1d2ce4f2009-10-19 18:11:09 +0000921 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
922 ** binding a new value to this variable invalidates the current query plan.
923 */
drh823e09a2009-10-19 20:15:38 +0000924 if( p->isPrepareV2 &&
925 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
926 ){
dan937d0de2009-10-15 18:35:38 +0000927 p->expired = 1;
928 }
drh4f26d6c2004-05-26 23:25:30 +0000929 return SQLITE_OK;
930}
931
932/*
drh5e2517e2004-09-24 23:59:12 +0000933** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000934*/
drh5e2517e2004-09-24 23:59:12 +0000935static int bindText(
drh605264d2007-08-21 15:13:19 +0000936 sqlite3_stmt *pStmt, /* The statement to bind against */
937 int i, /* Index of the parameter to bind */
938 const void *zData, /* Pointer to the data to be bound */
939 int nData, /* Number of bytes of data to be bound */
940 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +0000941 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +0000942){
943 Vdbe *p = (Vdbe *)pStmt;
944 Mem *pVar;
945 int rc;
946
947 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +0000948 if( rc==SQLITE_OK ){
949 if( zData!=0 ){
950 pVar = &p->aVar[i-1];
951 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
952 if( rc==SQLITE_OK && encoding!=0 ){
953 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
954 }
955 sqlite3Error(p->db, rc, 0);
956 rc = sqlite3ApiExit(p->db, rc);
drh27641702007-08-22 02:56:42 +0000957 }
drh488e7b62008-10-06 12:46:43 +0000958 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000959 }
drh605264d2007-08-21 15:13:19 +0000960 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000961}
drh5e2517e2004-09-24 23:59:12 +0000962
963
964/*
965** Bind a blob value to an SQL statement variable.
966*/
967int sqlite3_bind_blob(
968 sqlite3_stmt *pStmt,
969 int i,
970 const void *zData,
971 int nData,
972 void (*xDel)(void*)
973){
974 return bindText(pStmt, i, zData, nData, xDel, 0);
975}
drh4f26d6c2004-05-26 23:25:30 +0000976int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
977 int rc;
978 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000979 rc = vdbeUnbind(p, i);
980 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000981 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +0000982 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000983 }
danielk1977f4618892004-06-28 13:09:11 +0000984 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000985}
986int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000987 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000988}
drhefad9992004-06-22 12:13:55 +0000989int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000990 int rc;
991 Vdbe *p = (Vdbe *)pStmt;
992 rc = vdbeUnbind(p, i);
993 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000994 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +0000995 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000996 }
997 return rc;
998}
drh605264d2007-08-21 15:13:19 +0000999int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1000 int rc;
1001 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +00001002 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001003 if( rc==SQLITE_OK ){
1004 sqlite3_mutex_leave(p->db->mutex);
1005 }
drh605264d2007-08-21 15:13:19 +00001006 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001007}
1008int sqlite3_bind_text(
1009 sqlite3_stmt *pStmt,
1010 int i,
1011 const char *zData,
1012 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001013 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001014){
drh5e2517e2004-09-24 23:59:12 +00001015 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001016}
drh6c626082004-11-14 21:56:29 +00001017#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001018int sqlite3_bind_text16(
1019 sqlite3_stmt *pStmt,
1020 int i,
1021 const void *zData,
1022 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001023 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001024){
drh5e2517e2004-09-24 23:59:12 +00001025 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001026}
drh6c626082004-11-14 21:56:29 +00001027#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001028int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1029 int rc;
drh29def562009-04-14 12:43:33 +00001030 switch( pValue->type ){
drh29def562009-04-14 12:43:33 +00001031 case SQLITE_INTEGER: {
1032 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1033 break;
1034 }
1035 case SQLITE_FLOAT: {
1036 rc = sqlite3_bind_double(pStmt, i, pValue->r);
1037 break;
1038 }
1039 case SQLITE_BLOB: {
1040 if( pValue->flags & MEM_Zero ){
1041 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1042 }else{
1043 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1044 }
1045 break;
1046 }
1047 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001048 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1049 pValue->enc);
1050 break;
1051 }
1052 default: {
1053 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001054 break;
1055 }
danielk197726e41442006-06-14 15:16:35 +00001056 }
1057 return rc;
1058}
drhb026e052007-05-02 01:34:31 +00001059int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1060 int rc;
1061 Vdbe *p = (Vdbe *)pStmt;
1062 rc = vdbeUnbind(p, i);
1063 if( rc==SQLITE_OK ){
1064 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001065 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001066 }
1067 return rc;
1068}
drh75f6a032004-07-15 14:15:00 +00001069
1070/*
1071** Return the number of wildcards that can be potentially bound to.
1072** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001073*/
1074int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001075 Vdbe *p = (Vdbe*)pStmt;
1076 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001077}
drh895d7472004-08-20 16:02:39 +00001078
1079/*
drhfa6bc002004-09-07 16:19:52 +00001080** Create a mapping from variable numbers to variable names
1081** in the Vdbe.azVar[] array, if such a mapping does not already
1082** exist.
drh895d7472004-08-20 16:02:39 +00001083*/
drhfa6bc002004-09-07 16:19:52 +00001084static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001085 if( !p->okVar ){
drh0f5ea0b2009-04-09 14:02:44 +00001086 int j;
1087 Op *pOp;
drh605264d2007-08-21 15:13:19 +00001088 sqlite3_mutex_enter(p->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001089 /* The race condition here is harmless. If two threads call this
1090 ** routine on the same Vdbe at the same time, they both might end
1091 ** up initializing the Vdbe.azVar[] array. That is a little extra
1092 ** work but it results in the same answer.
1093 */
1094 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1095 if( pOp->opcode==OP_Variable ){
1096 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1097 p->azVar[pOp->p1-1] = pOp->p4.z;
drh895d7472004-08-20 16:02:39 +00001098 }
1099 }
drh0f5ea0b2009-04-09 14:02:44 +00001100 p->okVar = 1;
drh605264d2007-08-21 15:13:19 +00001101 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001102 }
drhfa6bc002004-09-07 16:19:52 +00001103}
1104
1105/*
1106** Return the name of a wildcard parameter. Return NULL if the index
1107** is out of range or if the wildcard is unnamed.
1108**
1109** The result is always UTF-8.
1110*/
1111const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1112 Vdbe *p = (Vdbe*)pStmt;
1113 if( p==0 || i<1 || i>p->nVar ){
1114 return 0;
1115 }
1116 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001117 return p->azVar[i-1];
1118}
drhfa6bc002004-09-07 16:19:52 +00001119
1120/*
1121** Given a wildcard parameter name, return the index of the variable
1122** with that name. If there is no variable with the given name,
1123** return 0.
1124*/
1125int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1126 Vdbe *p = (Vdbe*)pStmt;
1127 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];
1135 if( z && strcmp(z,zName)==0 ){
1136 return i+1;
1137 }
drhfa6bc002004-09-07 16:19:52 +00001138 }
1139 }
1140 return 0;
1141}
drhf8db1bc2005-04-22 02:38:37 +00001142
drh51942bc2005-06-12 22:01:42 +00001143/*
drhf8db1bc2005-04-22 02:38:37 +00001144** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001145*/
shane145834a2008-09-04 12:03:42 +00001146int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001147 Vdbe *pFrom = (Vdbe*)pFromStmt;
1148 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001149 int i;
1150 assert( pTo->db==pFrom->db );
1151 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001152 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001153 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001154 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001155 }
drh27641702007-08-22 02:56:42 +00001156 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001157 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001158}
drh51942bc2005-06-12 22:01:42 +00001159
shaneeec556d2008-10-12 00:27:53 +00001160#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001161/*
shane145834a2008-09-04 12:03:42 +00001162** Deprecated external interface. Internal/core SQLite code
1163** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001164**
1165** Is is misuse to call this routine with statements from different
1166** database connections. But as this is a deprecated interface, we
1167** will not bother to check for that condition.
1168**
1169** If the two statements contain a different number of bindings, then
1170** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1171** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001172*/
1173int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001174 Vdbe *pFrom = (Vdbe*)pFromStmt;
1175 Vdbe *pTo = (Vdbe*)pToStmt;
1176 if( pFrom->nVar!=pTo->nVar ){
1177 return SQLITE_ERROR;
1178 }
danc94b8592009-10-20 07:01:24 +00001179 if( pTo->isPrepareV2 && pTo->expmask ){
1180 pTo->expired = 1;
1181 }
1182 if( pFrom->isPrepareV2 && pFrom->expmask ){
1183 pFrom->expired = 1;
1184 }
shane145834a2008-09-04 12:03:42 +00001185 return sqlite3TransferBindings(pFromStmt, pToStmt);
1186}
shaneeec556d2008-10-12 00:27:53 +00001187#endif
shane145834a2008-09-04 12:03:42 +00001188
1189/*
drh51942bc2005-06-12 22:01:42 +00001190** Return the sqlite3* database handle to which the prepared statement given
1191** in the argument belongs. This is the same database handle that was
1192** the first argument to the sqlite3_prepare() that was used to create
1193** the statement in the first place.
1194*/
1195sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1196 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1197}
drhbb5a9c32008-06-19 02:52:25 +00001198
1199/*
1200** Return a pointer to the next prepared statement after pStmt associated
1201** with database connection pDb. If pStmt is NULL, return the first
1202** prepared statement for the database connection. Return NULL if there
1203** are no more.
1204*/
1205sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1206 sqlite3_stmt *pNext;
1207 sqlite3_mutex_enter(pDb->mutex);
1208 if( pStmt==0 ){
1209 pNext = (sqlite3_stmt*)pDb->pVdbe;
1210 }else{
1211 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1212 }
1213 sqlite3_mutex_leave(pDb->mutex);
1214 return pNext;
1215}
drhd1d38482008-10-07 23:46:38 +00001216
1217/*
1218** Return the value of a status counter for a prepared statement
1219*/
1220int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1221 Vdbe *pVdbe = (Vdbe*)pStmt;
1222 int v = pVdbe->aCounter[op-1];
1223 if( resetFlag ) pVdbe->aCounter[op-1] = 0;
1224 return v;
1225}