blob: fea540f4327a77adf63475c11f582e1027bdb92f [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**
drhbb5a9c32008-06-19 02:52:25 +000016** $Id: vdbeapi.c,v 1.134 2008/06/19 02:52:25 drh Exp $
drh4f26d6c2004-05-26 23:25:30 +000017*/
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
danielk1977dfb316d2008-03-26 18:34:43 +000021#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
22/*
23** The following structure contains pointers to the end points of a
24** doubly-linked list of all compiled SQL statements that may be holding
25** buffers eligible for release when the sqlite3_release_memory() interface is
26** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
27** mutex.
28**
29** Statements are added to the end of this list when sqlite3_reset() is
30** called. They are removed either when sqlite3_step() or sqlite3_finalize()
31** is called. When statements are added to this list, the associated
32** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
33** can be freed using sqlite3VdbeReleaseMemory().
34**
35** When statements are added or removed from this list, the mutex
36** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
37** already held. The LRU2 mutex is then obtained, blocking if necessary,
38** the linked-list pointers manipulated and the LRU2 mutex relinquished.
39*/
40struct StatementLruList {
41 Vdbe *pFirst;
42 Vdbe *pLast;
43};
44static struct StatementLruList sqlite3LruStatements;
45
46/*
47** Check that the list looks to be internally consistent. This is used
48** as part of an assert() statement as follows:
49**
50** assert( stmtLruCheck() );
51*/
drh26e4a8b2008-05-01 17:16:52 +000052#ifndef NDEBUG
danielk1977dfb316d2008-03-26 18:34:43 +000053static int stmtLruCheck(){
54 Vdbe *p;
55 for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
56 assert(p->pLruNext || p==sqlite3LruStatements.pLast);
57 assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
58 assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
59 assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
60 }
61 return 1;
62}
drh26e4a8b2008-05-01 17:16:52 +000063#endif
danielk1977dfb316d2008-03-26 18:34:43 +000064
65/*
66** Add vdbe p to the end of the statement lru list. It is assumed that
67** p is not already part of the list when this is called. The lru list
68** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
69*/
70static void stmtLruAdd(Vdbe *p){
danielk197759f8c082008-06-18 17:09:10 +000071 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +000072
73 if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
danielk197759f8c082008-06-18 17:09:10 +000074 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +000075 return;
76 }
77
78 assert( stmtLruCheck() );
79
80 if( !sqlite3LruStatements.pFirst ){
81 assert( !sqlite3LruStatements.pLast );
82 sqlite3LruStatements.pFirst = p;
83 sqlite3LruStatements.pLast = p;
84 }else{
85 assert( !sqlite3LruStatements.pLast->pLruNext );
86 p->pLruPrev = sqlite3LruStatements.pLast;
87 sqlite3LruStatements.pLast->pLruNext = p;
88 sqlite3LruStatements.pLast = p;
89 }
90
91 assert( stmtLruCheck() );
92
danielk197759f8c082008-06-18 17:09:10 +000093 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +000094}
95
96/*
97** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
98** statement p from the least-recently-used statement list. If the
99** statement is not currently part of the list, this call is a no-op.
100*/
101static void stmtLruRemoveNomutex(Vdbe *p){
102 if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
103 assert( stmtLruCheck() );
104 if( p->pLruNext ){
105 p->pLruNext->pLruPrev = p->pLruPrev;
106 }else{
107 sqlite3LruStatements.pLast = p->pLruPrev;
108 }
109 if( p->pLruPrev ){
110 p->pLruPrev->pLruNext = p->pLruNext;
111 }else{
112 sqlite3LruStatements.pFirst = p->pLruNext;
113 }
114 p->pLruNext = 0;
115 p->pLruPrev = 0;
116 assert( stmtLruCheck() );
117 }
118}
119
120/*
121** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
122** statement p from the least-recently-used statement list. If the
123** statement is not currently part of the list, this call is a no-op.
124*/
125static void stmtLruRemove(Vdbe *p){
danielk197759f8c082008-06-18 17:09:10 +0000126 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +0000127 stmtLruRemoveNomutex(p);
danielk197759f8c082008-06-18 17:09:10 +0000128 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +0000129}
130
131/*
132** Try to release n bytes of memory by freeing buffers associated
133** with the memory registers of currently unused vdbes.
134*/
135int sqlite3VdbeReleaseMemory(int n){
136 Vdbe *p;
137 Vdbe *pNext;
138 int nFree = 0;
139
danielk197759f8c082008-06-18 17:09:10 +0000140 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +0000141 for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
142 pNext = p->pLruNext;
143
144 /* For each statement handle in the lru list, attempt to obtain the
145 ** associated database mutex. If it cannot be obtained, continue
146 ** to the next statement handle. It is not possible to block on
147 ** the database mutex - that could cause deadlock.
148 */
149 if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
150 nFree += sqlite3VdbeReleaseBuffers(p);
151 stmtLruRemoveNomutex(p);
152 sqlite3_mutex_leave(p->db->mutex);
153 }
154 }
danielk197759f8c082008-06-18 17:09:10 +0000155 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
danielk1977dfb316d2008-03-26 18:34:43 +0000156
157 return nFree;
158}
159
160/*
161** Call sqlite3Reprepare() on the statement. Remove it from the
162** lru list before doing so, as Reprepare() will free all the
163** memory register buffers anyway.
164*/
165int vdbeReprepare(Vdbe *p){
166 stmtLruRemove(p);
167 return sqlite3Reprepare(p);
168}
169
170#else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
171 #define stmtLruRemove(x)
172 #define stmtLruAdd(x)
173 #define vdbeReprepare(x) sqlite3Reprepare(x)
174#endif
175
176
drhd89bd002005-01-22 03:03:54 +0000177/*
178** Return TRUE (non-zero) of the statement supplied as an argument needs
179** to be recompiled. A statement needs to be recompiled whenever the
180** execution environment changes in a way that would alter the program
181** that sqlite3_prepare() generates. For example, if new functions or
182** collating sequences are registered or if an authorizer function is
183** added or changed.
drhd89bd002005-01-22 03:03:54 +0000184*/
185int sqlite3_expired(sqlite3_stmt *pStmt){
186 Vdbe *p = (Vdbe*)pStmt;
187 return p==0 || p->expired;
188}
189
drhe30f4422007-08-21 16:15:55 +0000190/*
191** The following routine destroys a virtual machine that is created by
192** the sqlite3_compile() routine. The integer returned is an SQLITE_
193** success/failure code that describes the result of executing the virtual
194** machine.
195**
196** This routine sets the error code and string returned by
197** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
198*/
199int sqlite3_finalize(sqlite3_stmt *pStmt){
200 int rc;
201 if( pStmt==0 ){
202 rc = SQLITE_OK;
203 }else{
204 Vdbe *v = (Vdbe*)pStmt;
drh7e8b8482008-01-23 03:03:05 +0000205#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +0000206 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +0000207#endif
drh86f8c192007-08-22 00:39:19 +0000208 sqlite3_mutex_enter(mutex);
danielk1977dfb316d2008-03-26 18:34:43 +0000209 stmtLruRemove(v);
drhe30f4422007-08-21 16:15:55 +0000210 rc = sqlite3VdbeFinalize(v);
drh86f8c192007-08-22 00:39:19 +0000211 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000212 }
213 return rc;
214}
215
216/*
217** Terminate the current execution of an SQL statement and reset it
218** back to its starting state so that it can be reused. A success code from
219** the prior execution is returned.
220**
221** This routine sets the error code and string returned by
222** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
223*/
224int sqlite3_reset(sqlite3_stmt *pStmt){
225 int rc;
226 if( pStmt==0 ){
227 rc = SQLITE_OK;
228 }else{
229 Vdbe *v = (Vdbe*)pStmt;
230 sqlite3_mutex_enter(v->db->mutex);
danielk197765710b12008-04-14 15:15:22 +0000231 rc = sqlite3VdbeReset(v, 1);
danielk1977dfb316d2008-03-26 18:34:43 +0000232 stmtLruAdd(v);
drhe30f4422007-08-21 16:15:55 +0000233 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
234 assert( (rc & (v->db->errMask))==rc );
235 sqlite3_mutex_leave(v->db->mutex);
236 }
237 return rc;
238}
239
240/*
241** Set all the parameters in the compiled SQL statement to NULL.
242*/
243int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
244 int i;
245 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +0000246 Vdbe *p = (Vdbe*)pStmt;
drh7e8b8482008-01-23 03:03:05 +0000247#ifndef SQLITE_MUTEX_NOOP
248 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
249#endif
250 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +0000251 for(i=0; i<p->nVar; i++){
252 sqlite3VdbeMemRelease(&p->aVar[i]);
253 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000254 }
drh7e8b8482008-01-23 03:03:05 +0000255 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000256 return rc;
257}
258
259
drh4f26d6c2004-05-26 23:25:30 +0000260/**************************** sqlite3_value_ *******************************
261** The following routines extract information from a Mem or sqlite3_value
262** structure.
263*/
264const void *sqlite3_value_blob(sqlite3_value *pVal){
265 Mem *p = (Mem*)pVal;
266 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000267 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000268 p->flags &= ~MEM_Str;
269 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000270 return p->z;
271 }else{
272 return sqlite3_value_text(pVal);
273 }
274}
275int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000276 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000277}
278int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000279 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000280}
281double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000282 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000283}
284int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000285 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000286}
drhefad9992004-06-22 12:13:55 +0000287sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000288 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000289}
290const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000291 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000292}
drh6c626082004-11-14 21:56:29 +0000293#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000294const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000295 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000296}
danielk1977d8123362004-06-12 09:25:12 +0000297const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000298 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000299}
300const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000301 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000302}
drh6c626082004-11-14 21:56:29 +0000303#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000304int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000305 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000306}
307
308/**************************** sqlite3_result_ *******************************
309** The following routines are used by user-defined functions to specify
310** the function result.
311*/
312void sqlite3_result_blob(
313 sqlite3_context *pCtx,
314 const void *z,
315 int n,
danielk1977d8123362004-06-12 09:25:12 +0000316 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000317){
drh5708d2d2005-06-22 10:53:59 +0000318 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000319 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000320 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000321}
322void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000323 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000324 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
325}
326void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000327 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000328 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000329 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000330}
danielk1977a1686c92006-01-23 07:52:37 +0000331#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000332void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000333 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000334 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000335 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000336}
danielk1977a1686c92006-01-23 07:52:37 +0000337#endif
drhf4479502004-05-27 03:12:53 +0000338void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000339 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000340 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
341}
342void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000343 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000344 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
345}
346void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000347 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000348 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000349}
350void sqlite3_result_text(
351 sqlite3_context *pCtx,
352 const char *z,
353 int n,
danielk1977d8123362004-06-12 09:25:12 +0000354 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000355){
drh32bc3f62007-08-21 20:25:39 +0000356 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000357 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000358}
drh6c626082004-11-14 21:56:29 +0000359#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000360void sqlite3_result_text16(
361 sqlite3_context *pCtx,
362 const void *z,
363 int n,
danielk1977d8123362004-06-12 09:25:12 +0000364 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000365){
drh32bc3f62007-08-21 20:25:39 +0000366 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000367 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000368}
369void sqlite3_result_text16be(
370 sqlite3_context *pCtx,
371 const void *z,
372 int n,
373 void (*xDel)(void *)
374){
drh32bc3f62007-08-21 20:25:39 +0000375 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000376 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000377}
378void sqlite3_result_text16le(
379 sqlite3_context *pCtx,
380 const void *z,
381 int n,
382 void (*xDel)(void *)
383){
drh32bc3f62007-08-21 20:25:39 +0000384 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000385 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000386}
drh6c626082004-11-14 21:56:29 +0000387#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000388void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000389 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000390 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000391}
drhb026e052007-05-02 01:34:31 +0000392void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000393 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000394 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
395}
drh69544ec2008-02-06 14:11:34 +0000396void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
397 pCtx->isError = errCode;
398}
drh4f26d6c2004-05-26 23:25:30 +0000399
drha0206bc2007-05-08 15:15:02 +0000400/* Force an SQLITE_TOOBIG error. */
401void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000402 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000403 pCtx->isError = SQLITE_TOOBIG;
404 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
405 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000406}
407
danielk1977a1644fd2007-08-29 12:31:25 +0000408/* An SQLITE_NOMEM error. */
409void sqlite3_result_error_nomem(sqlite3_context *pCtx){
410 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
411 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000412 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000413 pCtx->s.db->mallocFailed = 1;
414}
drh4f26d6c2004-05-26 23:25:30 +0000415
416/*
417** Execute the statement pStmt, either until a row of data is ready, the
418** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000419**
420** This routine implements the bulk of the logic behind the sqlite_step()
421** API. The only thing omitted is the automatic recompile if a
422** schema change has occurred. That detail is handled by the
423** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000424*/
drhb900aaf2006-11-09 00:24:53 +0000425static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000426 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000427 int rc;
428
danielk1977a185ddb2007-11-15 16:04:15 +0000429 assert(p);
430 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000431 return SQLITE_MISUSE;
432 }
433
danielk1977efaaf572006-01-16 11:29:19 +0000434 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000435 db = p->db;
436 assert( !db->mallocFailed );
danielk1977261919c2005-12-06 12:52:59 +0000437
drh91b48aa2004-06-30 11:14:18 +0000438 if( p->aborted ){
439 return SQLITE_ABORT;
440 }
danielk1977a21c6b62005-01-24 10:25:59 +0000441 if( p->pc<=0 && p->expired ){
442 if( p->rc==SQLITE_OK ){
443 p->rc = SQLITE_SCHEMA;
444 }
drhb900aaf2006-11-09 00:24:53 +0000445 rc = SQLITE_ERROR;
446 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000447 }
drh4f26d6c2004-05-26 23:25:30 +0000448 if( sqlite3SafetyOn(db) ){
449 p->rc = SQLITE_MISUSE;
450 return SQLITE_MISUSE;
451 }
danielk19771d850a72004-05-31 08:26:49 +0000452 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000453 /* If there are no other statements currently running, then
454 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
455 ** from interrupting a statement that has not yet started.
456 */
457 if( db->activeVdbeCnt==0 ){
458 db->u1.isInterrupted = 0;
459 }
460
drh19e2d372005-08-29 23:00:03 +0000461#ifndef SQLITE_OMIT_TRACE
drh19e2d372005-08-29 23:00:03 +0000462 if( db->xProfile && !db->init.busy ){
463 double rNow;
danielk1977b4b47412007-08-17 15:53:36 +0000464 sqlite3OsCurrentTime(db->pVfs, &rNow);
drh19e2d372005-08-29 23:00:03 +0000465 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
466 }
467#endif
drhc16a03b2004-09-15 13:38:10 +0000468
danielk19771d850a72004-05-31 08:26:49 +0000469 db->activeVdbeCnt++;
470 p->pc = 0;
danielk1977dfb316d2008-03-26 18:34:43 +0000471 stmtLruRemove(p);
danielk19771d850a72004-05-31 08:26:49 +0000472 }
drhb7f91642004-10-31 02:22:47 +0000473#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000474 if( p->explain ){
475 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000476 }else
477#endif /* SQLITE_OMIT_EXPLAIN */
478 {
drh4f26d6c2004-05-26 23:25:30 +0000479 rc = sqlite3VdbeExec(p);
480 }
481
482 if( sqlite3SafetyOff(db) ){
483 rc = SQLITE_MISUSE;
484 }
485
drh19e2d372005-08-29 23:00:03 +0000486#ifndef SQLITE_OMIT_TRACE
487 /* Invoke the profile callback if there is one
488 */
drh949f9cd2008-01-12 21:35:57 +0000489 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
490 && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
drh19e2d372005-08-29 23:00:03 +0000491 double rNow;
492 u64 elapseTime;
493
danielk1977b4b47412007-08-17 15:53:36 +0000494 sqlite3OsCurrentTime(db->pVfs, &rNow);
drh19e2d372005-08-29 23:00:03 +0000495 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
drh949f9cd2008-01-12 21:35:57 +0000496 db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
drh19e2d372005-08-29 23:00:03 +0000497 }
498#endif
499
danielk197797a227c2006-01-20 16:32:04 +0000500 sqlite3Error(p->db, rc, 0);
danielk197776e8d1a2006-01-18 18:22:43 +0000501 p->rc = sqlite3ApiExit(p->db, p->rc);
drhb900aaf2006-11-09 00:24:53 +0000502end_of_step:
drh4ac285a2006-09-15 07:28:50 +0000503 assert( (rc&0xff)==rc );
drhb900aaf2006-11-09 00:24:53 +0000504 if( p->zSql && (rc&0xff)<SQLITE_ROW ){
505 /* This behavior occurs if sqlite3_prepare_v2() was used to build
506 ** the prepared statement. Return error codes directly */
danielk1977612642d2007-07-12 13:18:05 +0000507 sqlite3Error(p->db, p->rc, 0);
drhb900aaf2006-11-09 00:24:53 +0000508 return p->rc;
509 }else{
510 /* This is for legacy sqlite3_prepare() builds and when the code
511 ** is SQLITE_ROW or SQLITE_DONE */
512 return rc;
513 }
514}
515
516/*
517** This is the top-level implementation of sqlite3_step(). Call
518** sqlite3Step() to do most of the work. If a schema error occurs,
519** call sqlite3Reprepare() and try again.
520*/
521#ifdef SQLITE_OMIT_PARSER
522int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000523 int rc = SQLITE_MISUSE;
524 if( pStmt ){
525 Vdbe *v;
526 v = (Vdbe*)pStmt;
527 sqlite3_mutex_enter(v->db->mutex);
528 rc = sqlite3Step(v);
529 sqlite3_mutex_leave(v->db->mutex);
530 }
drhb21c8cd2007-08-21 19:33:56 +0000531 return rc;
drhb900aaf2006-11-09 00:24:53 +0000532}
533#else
534int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000535 int rc = SQLITE_MISUSE;
536 if( pStmt ){
537 int cnt = 0;
538 Vdbe *v = (Vdbe*)pStmt;
539 sqlite3 *db = v->db;
540 sqlite3_mutex_enter(db->mutex);
541 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
542 && cnt++ < 5
danielk1977dfb316d2008-03-26 18:34:43 +0000543 && vdbeReprepare(v) ){
danielk1977a185ddb2007-11-15 16:04:15 +0000544 sqlite3_reset(pStmt);
545 v->expired = 0;
danielk19778e556522007-11-13 10:30:24 +0000546 }
danielk1977a185ddb2007-11-15 16:04:15 +0000547 if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
548 /* This case occurs after failing to recompile an sql statement.
549 ** The error message from the SQL compiler has already been loaded
550 ** into the database handle. This block copies the error message
551 ** from the database handle into the statement and sets the statement
552 ** program counter to 0 to ensure that when the statement is
553 ** finalized or reset the parser error message is available via
554 ** sqlite3_errmsg() and sqlite3_errcode().
555 */
556 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
557 sqlite3_free(v->zErrMsg);
558 if( !db->mallocFailed ){
559 v->zErrMsg = sqlite3DbStrDup(db, zErr);
560 } else {
561 v->zErrMsg = 0;
562 v->rc = SQLITE_NOMEM;
563 }
564 }
565 rc = sqlite3ApiExit(db, rc);
566 sqlite3_mutex_leave(db->mutex);
danielk19778e556522007-11-13 10:30:24 +0000567 }
danielk197776e8d1a2006-01-18 18:22:43 +0000568 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000569}
drhb900aaf2006-11-09 00:24:53 +0000570#endif
drh4f26d6c2004-05-26 23:25:30 +0000571
572/*
drheb2e1762004-05-27 01:53:56 +0000573** Extract the user data from a sqlite3_context structure and return a
574** pointer to it.
575*/
576void *sqlite3_user_data(sqlite3_context *p){
577 assert( p && p->pFunc );
578 return p->pFunc->pUserData;
579}
580
581/*
drhfa4a4b92008-03-19 21:45:51 +0000582** Extract the user data from a sqlite3_context structure and return a
583** pointer to it.
584*/
585sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
586 assert( p && p->pFunc );
587 return p->s.db;
588}
589
590/*
drhb7481e72006-09-16 21:45:14 +0000591** The following is the implementation of an SQL function that always
592** fails with an error message stating that the function is used in the
593** wrong context. The sqlite3_overload_function() API might construct
594** SQL function that use this routine so that the functions will exist
595** for name resolution but are actually overloaded by the xFindFunction
596** method of virtual tables.
597*/
598void sqlite3InvalidFunction(
599 sqlite3_context *context, /* The function calling context */
600 int argc, /* Number of arguments to the function */
601 sqlite3_value **argv /* Value of each argument */
602){
603 const char *zName = context->pFunc->zName;
604 char *zErr;
danielk19771e536952007-08-16 10:09:01 +0000605 zErr = sqlite3MPrintf(0,
drhb7481e72006-09-16 21:45:14 +0000606 "unable to use function %s in the requested context", zName);
607 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000608 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000609}
610
611/*
drheb2e1762004-05-27 01:53:56 +0000612** Allocate or return the aggregate context for a user function. A new
613** context is allocated on the first call. Subsequent calls return the
614** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000615*/
616void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhb21c8cd2007-08-21 19:33:56 +0000617 Mem *pMem;
drh825c6622005-09-08 19:01:05 +0000618 assert( p && p->pFunc && p->pFunc->xStep );
drhb21c8cd2007-08-21 19:33:56 +0000619 assert( sqlite3_mutex_held(p->s.db->mutex) );
620 pMem = p->pMem;
drha10a34b2005-09-07 22:09:48 +0000621 if( (pMem->flags & MEM_Agg)==0 ){
622 if( nByte==0 ){
danielk19775f096132008-03-28 15:44:09 +0000623 sqlite3VdbeMemReleaseExternal(pMem);
624 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000625 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000626 }else{
danielk19775f096132008-03-28 15:44:09 +0000627 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000628 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000629 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000630 if( pMem->z ){
631 memset(pMem->z, 0, nByte);
632 }
drheb2e1762004-05-27 01:53:56 +0000633 }
634 }
drhabfcea22005-09-06 20:36:48 +0000635 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000636}
637
638/*
danielk1977682f68b2004-06-05 10:22:17 +0000639** Return the auxilary data pointer, if any, for the iArg'th argument to
640** the user-function defined by pCtx.
641*/
642void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000643 VdbeFunc *pVdbeFunc;
644
645 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
646 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000647 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
648 return 0;
649 }
drhf92c7ff2004-06-19 15:40:23 +0000650 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000651}
652
653/*
654** Set the auxilary data pointer and delete function, for the iArg'th
655** argument to the user-function defined by pCtx. Any previous value is
656** deleted by calling the delete function specified when it was set.
657*/
658void sqlite3_set_auxdata(
659 sqlite3_context *pCtx,
660 int iArg,
661 void *pAux,
662 void (*xDelete)(void*)
663){
664 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000665 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000666 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000667
drhb21c8cd2007-08-21 19:33:56 +0000668 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000669 pVdbeFunc = pCtx->pVdbeFunc;
670 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000671 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000672 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000673 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000674 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000675 goto failed;
676 }
drh53f733c2005-09-16 02:38:09 +0000677 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000678 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000679 pVdbeFunc->nAux = iArg+1;
680 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000681 }
682
drhf92c7ff2004-06-19 15:40:23 +0000683 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000684 if( pAuxData->pAux && pAuxData->xDelete ){
685 pAuxData->xDelete(pAuxData->pAux);
686 }
687 pAuxData->pAux = pAux;
688 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000689 return;
690
691failed:
692 if( xDelete ){
693 xDelete(pAux);
694 }
danielk1977682f68b2004-06-05 10:22:17 +0000695}
696
697/*
drheb2e1762004-05-27 01:53:56 +0000698** Return the number of times the Step function of a aggregate has been
699** called.
700**
drhcf85a512006-02-09 18:35:29 +0000701** This function is deprecated. Do not use it for new code. It is
702** provide only to avoid breaking legacy code. New aggregate function
703** implementations should keep their own counts within their aggregate
704** context.
drheb2e1762004-05-27 01:53:56 +0000705*/
706int sqlite3_aggregate_count(sqlite3_context *p){
707 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000708 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000709}
710
711/*
drh4f26d6c2004-05-26 23:25:30 +0000712** Return the number of columns in the result set for the statement pStmt.
713*/
714int sqlite3_column_count(sqlite3_stmt *pStmt){
715 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000716 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000717}
718
719/*
720** Return the number of values available from the current row of the
721** currently executing statement pStmt.
722*/
723int sqlite3_data_count(sqlite3_stmt *pStmt){
724 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000725 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000726 return pVm->nResColumn;
727}
728
729
730/*
731** Check to see if column iCol of the given statement is valid. If
732** it is, return a pointer to the Mem for the value of that column.
733** If iCol is not valid, return a pointer to a Mem which has a value
734** of NULL.
735*/
736static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000737 Vdbe *pVm;
738 int vals;
739 Mem *pOut;
740
741 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000742 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000743 sqlite3_mutex_enter(pVm->db->mutex);
744 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000745 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000746 }else{
rse867780a2008-03-29 12:39:39 +0000747 static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
drh27641702007-08-22 02:56:42 +0000748 if( pVm->db ){
749 sqlite3_mutex_enter(pVm->db->mutex);
750 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
751 }
drh32bc3f62007-08-21 20:25:39 +0000752 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000753 }
drh32bc3f62007-08-21 20:25:39 +0000754 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000755}
756
danielk19772e588c72005-12-09 14:25:08 +0000757/*
758** This function is called after invoking an sqlite3_value_XXX function on a
759** column value (i.e. a value returned by evaluating an SQL expression in the
760** select list of a SELECT statement) that may cause a malloc() failure. If
761** malloc() has failed, the threads mallocFailed flag is cleared and the result
762** code of statement pStmt set to SQLITE_NOMEM.
763**
drh32bc3f62007-08-21 20:25:39 +0000764** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000765**
766** sqlite3_column_int()
767** sqlite3_column_int64()
768** sqlite3_column_text()
769** sqlite3_column_text16()
770** sqlite3_column_real()
771** sqlite3_column_bytes()
772** sqlite3_column_bytes16()
773**
774** But not for sqlite3_column_blob(), which never calls malloc().
775*/
776static void columnMallocFailure(sqlite3_stmt *pStmt)
777{
778 /* If malloc() failed during an encoding conversion within an
779 ** sqlite3_column_XXX API, then set the return code of the statement to
780 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
781 ** and _finalize() will return NOMEM.
782 */
danielk197754f01982006-01-18 15:25:17 +0000783 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000784 if( p ){
785 p->rc = sqlite3ApiExit(p->db, p->rc);
786 sqlite3_mutex_leave(p->db->mutex);
787 }
danielk19772e588c72005-12-09 14:25:08 +0000788}
789
drh4f26d6c2004-05-26 23:25:30 +0000790/**************************** sqlite3_column_ *******************************
791** The following routines are used to access elements of the current row
792** in the result set.
793*/
danielk1977c572ef72004-05-27 09:28:41 +0000794const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000795 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000796 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000797 /* Even though there is no encoding conversion, value_blob() might
798 ** need to call malloc() to expand the result of a zeroblob()
799 ** expression.
800 */
801 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000802 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000803}
drh4f26d6c2004-05-26 23:25:30 +0000804int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000805 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
806 columnMallocFailure(pStmt);
807 return val;
drh4f26d6c2004-05-26 23:25:30 +0000808}
809int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000810 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
811 columnMallocFailure(pStmt);
812 return val;
drh4f26d6c2004-05-26 23:25:30 +0000813}
814double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000815 double val = sqlite3_value_double( columnMem(pStmt,i) );
816 columnMallocFailure(pStmt);
817 return val;
drh4f26d6c2004-05-26 23:25:30 +0000818}
819int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000820 int val = sqlite3_value_int( columnMem(pStmt,i) );
821 columnMallocFailure(pStmt);
822 return val;
drh4f26d6c2004-05-26 23:25:30 +0000823}
drhefad9992004-06-22 12:13:55 +0000824sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000825 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
826 columnMallocFailure(pStmt);
827 return val;
drh4f26d6c2004-05-26 23:25:30 +0000828}
829const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000830 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
831 columnMallocFailure(pStmt);
832 return val;
drh4f26d6c2004-05-26 23:25:30 +0000833}
drhd1e47332005-06-26 17:55:33 +0000834sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000835 sqlite3_value *pOut = columnMem(pStmt, i);
836 columnMallocFailure(pStmt);
837 return pOut;
drhd1e47332005-06-26 17:55:33 +0000838}
drh6c626082004-11-14 21:56:29 +0000839#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000840const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000841 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
842 columnMallocFailure(pStmt);
843 return val;
drh4f26d6c2004-05-26 23:25:30 +0000844}
drh6c626082004-11-14 21:56:29 +0000845#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000846int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000847 int iType = sqlite3_value_type( columnMem(pStmt,i) );
848 columnMallocFailure(pStmt);
849 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000850}
851
drh29d72102006-02-09 22:13:41 +0000852/* The following function is experimental and subject to change or
853** removal */
854/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
855** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
856**}
857*/
858
drh5e2517e2004-09-24 23:59:12 +0000859/*
860** Convert the N-th element of pStmt->pColName[] into a string using
861** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000862**
863** There are up to 5 names for each column. useType determines which
864** name is returned. Here are the names:
865**
866** 0 The column name as it should be displayed for output
867** 1 The datatype name for the column
868** 2 The name of the database that the column derives from
869** 3 The name of the table that the column derives from
870** 4 The name of the table column that the result column derives from
871**
872** If the result is not a simple column reference (if it is an expression
873** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000874*/
875static const void *columnName(
876 sqlite3_stmt *pStmt,
877 int N,
878 const void *(*xFunc)(Mem*),
879 int useType
880){
drh32bc3f62007-08-21 20:25:39 +0000881 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000882 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000883 int n;
884
drh5e2517e2004-09-24 23:59:12 +0000885
drh32bc3f62007-08-21 20:25:39 +0000886 if( p!=0 ){
887 n = sqlite3_column_count(pStmt);
888 if( N<n && N>=0 ){
889 N += useType*n;
drh153c62c2007-08-24 03:51:33 +0000890 sqlite3_mutex_enter(p->db->mutex);
drh32bc3f62007-08-21 20:25:39 +0000891 ret = xFunc(&p->aColName[N]);
892
drh32bc3f62007-08-21 20:25:39 +0000893 /* A malloc may have failed inside of the xFunc() call. If this
894 ** is the case, clear the mallocFailed flag and return NULL.
895 */
896 if( p->db && p->db->mallocFailed ){
897 p->db->mallocFailed = 0;
898 ret = 0;
899 }
drh153c62c2007-08-24 03:51:33 +0000900 sqlite3_mutex_leave(p->db->mutex);
drh32bc3f62007-08-21 20:25:39 +0000901 }
drh5e2517e2004-09-24 23:59:12 +0000902 }
danielk197700fd9572005-12-07 06:27:43 +0000903 return ret;
drh5e2517e2004-09-24 23:59:12 +0000904}
905
drh4f26d6c2004-05-26 23:25:30 +0000906/*
907** Return the name of the Nth column of the result set returned by SQL
908** statement pStmt.
909*/
910const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000911 return columnName(
912 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000913}
drhe590fbd2005-05-20 19:36:01 +0000914#ifndef SQLITE_OMIT_UTF16
915const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000916 return columnName(
917 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000918}
919#endif
drh4f26d6c2004-05-26 23:25:30 +0000920
921/*
drh3f913572008-03-22 01:07:17 +0000922** Constraint: If you have ENABLE_COLUMN_METADATA then you must
923** not define OMIT_DECLTYPE.
924*/
925#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
926# error "Must not define both SQLITE_OMIT_DECLTYPE \
927 and SQLITE_ENABLE_COLUMN_METADATA"
928#endif
929
930#ifndef SQLITE_OMIT_DECLTYPE
931/*
drh6c626082004-11-14 21:56:29 +0000932** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000933** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000934*/
935const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000936 return columnName(
937 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000938}
drh6c626082004-11-14 21:56:29 +0000939#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000940const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000941 return columnName(
942 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000943}
drh6c626082004-11-14 21:56:29 +0000944#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000945#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000946
danielk19774b1ae992006-02-10 03:06:10 +0000947#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000948/*
949** Return the name of the database from which a result column derives.
950** NULL is returned if the result column is an expression or constant or
951** anything else which is not an unabiguous reference to a database column.
952*/
953const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000954 return columnName(
955 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000956}
957#ifndef SQLITE_OMIT_UTF16
958const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000959 return columnName(
960 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000961}
962#endif /* SQLITE_OMIT_UTF16 */
963
964/*
965** Return the name of the table from which a result column derives.
966** NULL is returned if the result column is an expression or constant or
967** anything else which is not an unabiguous reference to a database column.
968*/
969const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000970 return columnName(
971 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000972}
973#ifndef SQLITE_OMIT_UTF16
974const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000975 return columnName(
976 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000977}
978#endif /* SQLITE_OMIT_UTF16 */
979
980/*
981** Return the name of the table column from which a result column derives.
982** NULL is returned if the result column is an expression or constant or
983** anything else which is not an unabiguous reference to a database column.
984*/
985const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000986 return columnName(
987 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000988}
989#ifndef SQLITE_OMIT_UTF16
990const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000991 return columnName(
992 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000993}
994#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000995#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000996
997
drh4f26d6c2004-05-26 23:25:30 +0000998/******************************* sqlite3_bind_ ***************************
999**
1000** Routines used to attach values to wildcards in a compiled SQL statement.
1001*/
1002/*
1003** Unbind the value bound to variable i in virtual machine p. This is the
1004** the same as binding a NULL value to the column. If the "i" parameter is
1005** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1006**
1007** The error code stored in database p->db is overwritten with the return
1008** value in any case.
1009*/
1010static int vdbeUnbind(Vdbe *p, int i){
1011 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +00001012 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +00001013 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +00001014 return SQLITE_MISUSE;
1015 }
1016 if( i<1 || i>p->nVar ){
1017 sqlite3Error(p->db, SQLITE_RANGE, 0);
1018 return SQLITE_RANGE;
1019 }
1020 i--;
drh290c1942004-08-21 17:54:45 +00001021 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001022 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001023 pVar->flags = MEM_Null;
1024 sqlite3Error(p->db, SQLITE_OK, 0);
1025 return SQLITE_OK;
1026}
1027
1028/*
drh5e2517e2004-09-24 23:59:12 +00001029** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001030*/
drh5e2517e2004-09-24 23:59:12 +00001031static int bindText(
drh605264d2007-08-21 15:13:19 +00001032 sqlite3_stmt *pStmt, /* The statement to bind against */
1033 int i, /* Index of the parameter to bind */
1034 const void *zData, /* Pointer to the data to be bound */
1035 int nData, /* Number of bytes of data to be bound */
1036 void (*xDel)(void*), /* Destructor for the data */
1037 int encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001038){
1039 Vdbe *p = (Vdbe *)pStmt;
1040 Mem *pVar;
1041 int rc;
1042
drh605264d2007-08-21 15:13:19 +00001043 if( p==0 ){
1044 return SQLITE_MISUSE;
1045 }
1046 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001047 rc = vdbeUnbind(p, i);
drh27641702007-08-22 02:56:42 +00001048 if( rc==SQLITE_OK && zData!=0 ){
1049 pVar = &p->aVar[i-1];
1050 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1051 if( rc==SQLITE_OK && encoding!=0 ){
1052 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1053 }
1054 sqlite3Error(p->db, rc, 0);
1055 rc = sqlite3ApiExit(p->db, rc);
drh4f26d6c2004-05-26 23:25:30 +00001056 }
drh605264d2007-08-21 15:13:19 +00001057 sqlite3_mutex_leave(p->db->mutex);
1058 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001059}
drh5e2517e2004-09-24 23:59:12 +00001060
1061
1062/*
1063** Bind a blob value to an SQL statement variable.
1064*/
1065int sqlite3_bind_blob(
1066 sqlite3_stmt *pStmt,
1067 int i,
1068 const void *zData,
1069 int nData,
1070 void (*xDel)(void*)
1071){
1072 return bindText(pStmt, i, zData, nData, xDel, 0);
1073}
drh4f26d6c2004-05-26 23:25:30 +00001074int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1075 int rc;
1076 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001077 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001078 rc = vdbeUnbind(p, i);
1079 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001080 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +00001081 }
drh605264d2007-08-21 15:13:19 +00001082 sqlite3_mutex_leave(p->db->mutex);
danielk1977f4618892004-06-28 13:09:11 +00001083 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001084}
1085int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +00001086 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001087}
drhefad9992004-06-22 12:13:55 +00001088int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001089 int rc;
1090 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001091 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001092 rc = vdbeUnbind(p, i);
1093 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001094 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +00001095 }
drh605264d2007-08-21 15:13:19 +00001096 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001097 return rc;
1098}
drh605264d2007-08-21 15:13:19 +00001099int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1100 int rc;
1101 Vdbe *p = (Vdbe*)pStmt;
1102 sqlite3_mutex_enter(p->db->mutex);
1103 rc = vdbeUnbind(p, i);
1104 sqlite3_mutex_leave(p->db->mutex);
1105 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001106}
1107int sqlite3_bind_text(
1108 sqlite3_stmt *pStmt,
1109 int i,
1110 const char *zData,
1111 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001112 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001113){
drh5e2517e2004-09-24 23:59:12 +00001114 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001115}
drh6c626082004-11-14 21:56:29 +00001116#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001117int sqlite3_bind_text16(
1118 sqlite3_stmt *pStmt,
1119 int i,
1120 const void *zData,
1121 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001122 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001123){
drh5e2517e2004-09-24 23:59:12 +00001124 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001125}
drh6c626082004-11-14 21:56:29 +00001126#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001127int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1128 int rc;
1129 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001130 sqlite3_mutex_enter(p->db->mutex);
danielk197726e41442006-06-14 15:16:35 +00001131 rc = vdbeUnbind(p, i);
1132 if( rc==SQLITE_OK ){
drhb21c8cd2007-08-21 19:33:56 +00001133 rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
danielk19771e740c62008-05-16 15:24:58 +00001134 if( rc==SQLITE_OK ){
1135 rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db));
1136 }
danielk197726e41442006-06-14 15:16:35 +00001137 }
danielk1977a7a8e142008-02-13 18:25:27 +00001138 rc = sqlite3ApiExit(p->db, rc);
drh605264d2007-08-21 15:13:19 +00001139 sqlite3_mutex_leave(p->db->mutex);
danielk197726e41442006-06-14 15:16:35 +00001140 return rc;
1141}
drhb026e052007-05-02 01:34:31 +00001142int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1143 int rc;
1144 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001145 sqlite3_mutex_enter(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001146 rc = vdbeUnbind(p, i);
1147 if( rc==SQLITE_OK ){
1148 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1149 }
drh605264d2007-08-21 15:13:19 +00001150 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001151 return rc;
1152}
drh75f6a032004-07-15 14:15:00 +00001153
1154/*
1155** Return the number of wildcards that can be potentially bound to.
1156** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001157*/
1158int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001159 Vdbe *p = (Vdbe*)pStmt;
1160 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001161}
drh895d7472004-08-20 16:02:39 +00001162
1163/*
drhfa6bc002004-09-07 16:19:52 +00001164** Create a mapping from variable numbers to variable names
1165** in the Vdbe.azVar[] array, if such a mapping does not already
1166** exist.
drh895d7472004-08-20 16:02:39 +00001167*/
drhfa6bc002004-09-07 16:19:52 +00001168static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001169 if( !p->okVar ){
drh605264d2007-08-21 15:13:19 +00001170 sqlite3_mutex_enter(p->db->mutex);
1171 if( !p->okVar ){
1172 int j;
1173 Op *pOp;
1174 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1175 if( pOp->opcode==OP_Variable ){
1176 assert( pOp->p1>0 && pOp->p1<=p->nVar );
danielk19772dca4ac2008-01-03 11:50:29 +00001177 p->azVar[pOp->p1-1] = pOp->p4.z;
drh605264d2007-08-21 15:13:19 +00001178 }
drh895d7472004-08-20 16:02:39 +00001179 }
drh605264d2007-08-21 15:13:19 +00001180 p->okVar = 1;
drh895d7472004-08-20 16:02:39 +00001181 }
drh605264d2007-08-21 15:13:19 +00001182 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001183 }
drhfa6bc002004-09-07 16:19:52 +00001184}
1185
1186/*
1187** Return the name of a wildcard parameter. Return NULL if the index
1188** is out of range or if the wildcard is unnamed.
1189**
1190** The result is always UTF-8.
1191*/
1192const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1193 Vdbe *p = (Vdbe*)pStmt;
1194 if( p==0 || i<1 || i>p->nVar ){
1195 return 0;
1196 }
1197 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001198 return p->azVar[i-1];
1199}
drhfa6bc002004-09-07 16:19:52 +00001200
1201/*
1202** Given a wildcard parameter name, return the index of the variable
1203** with that name. If there is no variable with the given name,
1204** return 0.
1205*/
1206int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1207 Vdbe *p = (Vdbe*)pStmt;
1208 int i;
1209 if( p==0 ){
1210 return 0;
1211 }
1212 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +00001213 if( zName ){
1214 for(i=0; i<p->nVar; i++){
1215 const char *z = p->azVar[i];
1216 if( z && strcmp(z,zName)==0 ){
1217 return i+1;
1218 }
drhfa6bc002004-09-07 16:19:52 +00001219 }
1220 }
1221 return 0;
1222}
drhf8db1bc2005-04-22 02:38:37 +00001223
drh51942bc2005-06-12 22:01:42 +00001224/*
drhf8db1bc2005-04-22 02:38:37 +00001225** Transfer all bindings from the first statement over to the second.
1226** If the two statements contain a different number of bindings, then
1227** an SQLITE_ERROR is returned.
1228*/
1229int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1230 Vdbe *pFrom = (Vdbe*)pFromStmt;
1231 Vdbe *pTo = (Vdbe*)pToStmt;
1232 int i, rc = SQLITE_OK;
1233 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
drh27641702007-08-22 02:56:42 +00001234 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1235 || pTo->db!=pFrom->db ){
drhf8db1bc2005-04-22 02:38:37 +00001236 return SQLITE_MISUSE;
1237 }
1238 if( pFrom->nVar!=pTo->nVar ){
1239 return SQLITE_ERROR;
1240 }
drh27641702007-08-22 02:56:42 +00001241 sqlite3_mutex_enter(pTo->db->mutex);
drhf8db1bc2005-04-22 02:38:37 +00001242 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001243 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001244 }
drh27641702007-08-22 02:56:42 +00001245 sqlite3_mutex_leave(pTo->db->mutex);
drh4ac285a2006-09-15 07:28:50 +00001246 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
drhf8db1bc2005-04-22 02:38:37 +00001247 return rc;
1248}
drh51942bc2005-06-12 22:01:42 +00001249
1250/*
1251** Return the sqlite3* database handle to which the prepared statement given
1252** in the argument belongs. This is the same database handle that was
1253** the first argument to the sqlite3_prepare() that was used to create
1254** the statement in the first place.
1255*/
1256sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1257 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1258}
drhbb5a9c32008-06-19 02:52:25 +00001259
1260/*
1261** Return a pointer to the next prepared statement after pStmt associated
1262** with database connection pDb. If pStmt is NULL, return the first
1263** prepared statement for the database connection. Return NULL if there
1264** are no more.
1265*/
1266sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1267 sqlite3_stmt *pNext;
1268 sqlite3_mutex_enter(pDb->mutex);
1269 if( pStmt==0 ){
1270 pNext = (sqlite3_stmt*)pDb->pVdbe;
1271 }else{
1272 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1273 }
1274 sqlite3_mutex_leave(pDb->mutex);
1275 return pNext;
1276}