blob: d40578fb3b36eddcd78909aba4edec2f144a18aa [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**
danielk197741f5b042009-06-06 14:13:26 +000016** $Id: vdbeapi.c,v 1.165 2009/06/06 14:13:27 danielk1977 Exp $
drh4f26d6c2004-05-26 23:25:30 +000017*/
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
danielk197767e3da72008-08-21 12:19:44 +000021#if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
danielk1977dfb316d2008-03-26 18:34:43 +000022/*
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
shaneeec556d2008-10-12 00:27:53 +0000177#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +0000178/*
179** Return TRUE (non-zero) of the statement supplied as an argument needs
180** to be recompiled. A statement needs to be recompiled whenever the
181** execution environment changes in a way that would alter the program
182** that sqlite3_prepare() generates. For example, if new functions or
183** collating sequences are registered or if an authorizer function is
184** added or changed.
drhd89bd002005-01-22 03:03:54 +0000185*/
186int sqlite3_expired(sqlite3_stmt *pStmt){
187 Vdbe *p = (Vdbe*)pStmt;
188 return p==0 || p->expired;
189}
shaneeec556d2008-10-12 00:27:53 +0000190#endif
drhd89bd002005-01-22 03:03:54 +0000191
drhe30f4422007-08-21 16:15:55 +0000192/*
193** The following routine destroys a virtual machine that is created by
194** the sqlite3_compile() routine. The integer returned is an SQLITE_
195** success/failure code that describes the result of executing the virtual
196** machine.
197**
198** This routine sets the error code and string returned by
199** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
200*/
201int sqlite3_finalize(sqlite3_stmt *pStmt){
202 int rc;
203 if( pStmt==0 ){
204 rc = SQLITE_OK;
205 }else{
206 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06 +0000207 sqlite3 *db = v->db;
drh18472fa2008-10-07 15:25:48 +0000208#if SQLITE_THREADSAFE
drh86f8c192007-08-22 00:39:19 +0000209 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +0000210#endif
drh86f8c192007-08-22 00:39:19 +0000211 sqlite3_mutex_enter(mutex);
danielk1977dfb316d2008-03-26 18:34:43 +0000212 stmtLruRemove(v);
drhe30f4422007-08-21 16:15:55 +0000213 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +0000214 rc = sqlite3ApiExit(db, rc);
drh86f8c192007-08-22 00:39:19 +0000215 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000216 }
217 return rc;
218}
219
220/*
221** Terminate the current execution of an SQL statement and reset it
222** back to its starting state so that it can be reused. A success code from
223** the prior execution is returned.
224**
225** This routine sets the error code and string returned by
226** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
227*/
228int sqlite3_reset(sqlite3_stmt *pStmt){
229 int rc;
230 if( pStmt==0 ){
231 rc = SQLITE_OK;
232 }else{
233 Vdbe *v = (Vdbe*)pStmt;
234 sqlite3_mutex_enter(v->db->mutex);
drhc890fec2008-08-01 20:10:08 +0000235 rc = sqlite3VdbeReset(v);
danielk1977dfb316d2008-03-26 18:34:43 +0000236 stmtLruAdd(v);
drhe30f4422007-08-21 16:15:55 +0000237 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
238 assert( (rc & (v->db->errMask))==rc );
danielk1977238746a2009-03-19 18:51:06 +0000239 rc = sqlite3ApiExit(v->db, rc);
drhe30f4422007-08-21 16:15:55 +0000240 sqlite3_mutex_leave(v->db->mutex);
241 }
242 return rc;
243}
244
245/*
246** Set all the parameters in the compiled SQL statement to NULL.
247*/
248int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
249 int i;
250 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +0000251 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +0000252#if SQLITE_THREADSAFE
drh7e8b8482008-01-23 03:03:05 +0000253 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
254#endif
255 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +0000256 for(i=0; i<p->nVar; i++){
257 sqlite3VdbeMemRelease(&p->aVar[i]);
258 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000259 }
drh7e8b8482008-01-23 03:03:05 +0000260 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000261 return rc;
262}
263
264
drh4f26d6c2004-05-26 23:25:30 +0000265/**************************** sqlite3_value_ *******************************
266** The following routines extract information from a Mem or sqlite3_value
267** structure.
268*/
269const void *sqlite3_value_blob(sqlite3_value *pVal){
270 Mem *p = (Mem*)pVal;
271 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000272 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000273 p->flags &= ~MEM_Str;
274 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000275 return p->z;
276 }else{
277 return sqlite3_value_text(pVal);
278 }
279}
280int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000281 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000282}
283int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000284 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000285}
286double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000287 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000288}
289int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000290 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000291}
drhefad9992004-06-22 12:13:55 +0000292sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000293 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000294}
295const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000296 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000297}
drh6c626082004-11-14 21:56:29 +0000298#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000299const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000300 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000301}
danielk1977d8123362004-06-12 09:25:12 +0000302const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000303 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000304}
305const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000306 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000307}
drh6c626082004-11-14 21:56:29 +0000308#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000309int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000310 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000311}
312
313/**************************** sqlite3_result_ *******************************
314** The following routines are used by user-defined functions to specify
315** the function result.
316*/
317void sqlite3_result_blob(
318 sqlite3_context *pCtx,
319 const void *z,
320 int n,
danielk1977d8123362004-06-12 09:25:12 +0000321 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000322){
drh5708d2d2005-06-22 10:53:59 +0000323 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000324 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000325 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000326}
327void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000328 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000329 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
330}
331void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000332 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000333 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000334 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000335}
danielk1977a1686c92006-01-23 07:52:37 +0000336#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000337void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000338 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000339 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000340 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000341}
danielk1977a1686c92006-01-23 07:52:37 +0000342#endif
drhf4479502004-05-27 03:12:53 +0000343void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000344 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000345 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
346}
347void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000348 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000349 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
350}
351void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000352 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000353 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000354}
355void sqlite3_result_text(
356 sqlite3_context *pCtx,
357 const char *z,
358 int n,
danielk1977d8123362004-06-12 09:25:12 +0000359 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000360){
drh32bc3f62007-08-21 20:25:39 +0000361 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000362 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000363}
drh6c626082004-11-14 21:56:29 +0000364#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000365void sqlite3_result_text16(
366 sqlite3_context *pCtx,
367 const void *z,
368 int n,
danielk1977d8123362004-06-12 09:25:12 +0000369 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000370){
drh32bc3f62007-08-21 20:25:39 +0000371 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000372 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000373}
374void sqlite3_result_text16be(
375 sqlite3_context *pCtx,
376 const void *z,
377 int n,
378 void (*xDel)(void *)
379){
drh32bc3f62007-08-21 20:25:39 +0000380 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000381 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000382}
383void sqlite3_result_text16le(
384 sqlite3_context *pCtx,
385 const void *z,
386 int n,
387 void (*xDel)(void *)
388){
drh32bc3f62007-08-21 20:25:39 +0000389 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000390 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000391}
drh6c626082004-11-14 21:56:29 +0000392#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000393void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000394 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000395 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000396}
drhb026e052007-05-02 01:34:31 +0000397void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000398 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000399 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
400}
drh69544ec2008-02-06 14:11:34 +0000401void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
402 pCtx->isError = errCode;
drh51c7d862009-04-27 18:46:06 +0000403 if( pCtx->s.flags & MEM_Null ){
404 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
405 SQLITE_UTF8, SQLITE_STATIC);
406 }
drh69544ec2008-02-06 14:11:34 +0000407}
drh4f26d6c2004-05-26 23:25:30 +0000408
drha0206bc2007-05-08 15:15:02 +0000409/* Force an SQLITE_TOOBIG error. */
410void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000411 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000412 pCtx->isError = SQLITE_TOOBIG;
413 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
414 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000415}
416
danielk1977a1644fd2007-08-29 12:31:25 +0000417/* An SQLITE_NOMEM error. */
418void sqlite3_result_error_nomem(sqlite3_context *pCtx){
419 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
420 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000421 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000422 pCtx->s.db->mallocFailed = 1;
423}
drh4f26d6c2004-05-26 23:25:30 +0000424
425/*
426** Execute the statement pStmt, either until a row of data is ready, the
427** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000428**
429** This routine implements the bulk of the logic behind the sqlite_step()
430** API. The only thing omitted is the automatic recompile if a
431** schema change has occurred. That detail is handled by the
432** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000433*/
drhb900aaf2006-11-09 00:24:53 +0000434static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000435 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000436 int rc;
437
danielk1977a185ddb2007-11-15 16:04:15 +0000438 assert(p);
439 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000440 return SQLITE_MISUSE;
441 }
442
danielk1977efaaf572006-01-16 11:29:19 +0000443 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000444 db = p->db;
drhc456e572008-08-11 18:44:58 +0000445 if( db->mallocFailed ){
446 return SQLITE_NOMEM;
447 }
danielk1977261919c2005-12-06 12:52:59 +0000448
danielk1977a21c6b62005-01-24 10:25:59 +0000449 if( p->pc<=0 && p->expired ){
drh7950acd2009-04-10 23:11:31 +0000450 if( ALWAYS(p->rc==SQLITE_OK) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000451 p->rc = SQLITE_SCHEMA;
452 }
drhb900aaf2006-11-09 00:24:53 +0000453 rc = SQLITE_ERROR;
454 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000455 }
drh4f26d6c2004-05-26 23:25:30 +0000456 if( sqlite3SafetyOn(db) ){
457 p->rc = SQLITE_MISUSE;
458 return SQLITE_MISUSE;
459 }
danielk19771d850a72004-05-31 08:26:49 +0000460 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000461 /* If there are no other statements currently running, then
462 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
463 ** from interrupting a statement that has not yet started.
464 */
465 if( db->activeVdbeCnt==0 ){
466 db->u1.isInterrupted = 0;
467 }
468
drh19e2d372005-08-29 23:00:03 +0000469#ifndef SQLITE_OMIT_TRACE
drh19e2d372005-08-29 23:00:03 +0000470 if( db->xProfile && !db->init.busy ){
471 double rNow;
danielk1977b4b47412007-08-17 15:53:36 +0000472 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000473 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
drh19e2d372005-08-29 23:00:03 +0000474 }
475#endif
drhc16a03b2004-09-15 13:38:10 +0000476
danielk19771d850a72004-05-31 08:26:49 +0000477 db->activeVdbeCnt++;
drhad4a4b82008-11-05 16:37:34 +0000478 if( p->readOnly==0 ) db->writeVdbeCnt++;
danielk19771d850a72004-05-31 08:26:49 +0000479 p->pc = 0;
danielk1977dfb316d2008-03-26 18:34:43 +0000480 stmtLruRemove(p);
danielk19771d850a72004-05-31 08:26:49 +0000481 }
drhb7f91642004-10-31 02:22:47 +0000482#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000483 if( p->explain ){
484 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000485 }else
486#endif /* SQLITE_OMIT_EXPLAIN */
487 {
drh4f26d6c2004-05-26 23:25:30 +0000488 rc = sqlite3VdbeExec(p);
489 }
490
491 if( sqlite3SafetyOff(db) ){
492 rc = SQLITE_MISUSE;
493 }
494
drh19e2d372005-08-29 23:00:03 +0000495#ifndef SQLITE_OMIT_TRACE
496 /* Invoke the profile callback if there is one
497 */
danielk19776ab3a2e2009-02-19 14:39:25 +0000498 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
drh19e2d372005-08-29 23:00:03 +0000499 double rNow;
500 u64 elapseTime;
501
danielk1977b4b47412007-08-17 15:53:36 +0000502 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000503 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
504 elapseTime -= p->startTime;
danielk19776ab3a2e2009-02-19 14:39:25 +0000505 db->xProfile(db->pProfileArg, p->zSql, elapseTime);
drh19e2d372005-08-29 23:00:03 +0000506 }
507#endif
508
drh80cc85b2008-07-23 21:07:25 +0000509 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000510 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
511 p->rc = SQLITE_NOMEM;
drhb900aaf2006-11-09 00:24:53 +0000512 }
danielk1977357864e2009-03-25 15:43:08 +0000513end_of_step:
514 /* At this point local variable rc holds the value that should be
515 ** returned if this statement was compiled using the legacy
516 ** sqlite3_prepare() interface. According to the docs, this can only
517 ** be one of the values in the first assert() below. Variable p->rc
518 ** contains the value that would be returned if sqlite3_finalize()
519 ** were called on statement p.
520 */
521 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
522 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
523 );
524 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
525 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
526 /* If this statement was prepared using sqlite3_prepare_v2(), and an
527 ** error has occured, then return the error code in p->rc to the
528 ** caller. Set the error code in the database handle to the same value.
529 */
530 rc = db->errCode = p->rc;
531 }
532 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000533}
534
535/*
536** This is the top-level implementation of sqlite3_step(). Call
537** sqlite3Step() to do most of the work. If a schema error occurs,
538** call sqlite3Reprepare() and try again.
539*/
540#ifdef SQLITE_OMIT_PARSER
541int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000542 int rc = SQLITE_MISUSE;
543 if( pStmt ){
544 Vdbe *v;
545 v = (Vdbe*)pStmt;
546 sqlite3_mutex_enter(v->db->mutex);
547 rc = sqlite3Step(v);
548 sqlite3_mutex_leave(v->db->mutex);
549 }
drhb21c8cd2007-08-21 19:33:56 +0000550 return rc;
drhb900aaf2006-11-09 00:24:53 +0000551}
552#else
553int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000554 int rc = SQLITE_MISUSE;
555 if( pStmt ){
556 int cnt = 0;
557 Vdbe *v = (Vdbe*)pStmt;
558 sqlite3 *db = v->db;
559 sqlite3_mutex_enter(db->mutex);
560 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
561 && cnt++ < 5
danielk197765a2ea12009-03-19 07:58:31 +0000562 && (rc = vdbeReprepare(v))==SQLITE_OK ){
danielk1977a185ddb2007-11-15 16:04:15 +0000563 sqlite3_reset(pStmt);
564 v->expired = 0;
danielk19778e556522007-11-13 10:30:24 +0000565 }
drh7950acd2009-04-10 23:11:31 +0000566 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
danielk1977a185ddb2007-11-15 16:04:15 +0000567 /* This case occurs after failing to recompile an sql statement.
568 ** The error message from the SQL compiler has already been loaded
569 ** into the database handle. This block copies the error message
570 ** from the database handle into the statement and sets the statement
571 ** program counter to 0 to ensure that when the statement is
572 ** finalized or reset the parser error message is available via
573 ** sqlite3_errmsg() and sqlite3_errcode().
574 */
575 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
drh633e6d52008-07-28 19:34:53 +0000576 sqlite3DbFree(db, v->zErrMsg);
danielk1977a185ddb2007-11-15 16:04:15 +0000577 if( !db->mallocFailed ){
578 v->zErrMsg = sqlite3DbStrDup(db, zErr);
579 } else {
580 v->zErrMsg = 0;
581 v->rc = SQLITE_NOMEM;
582 }
583 }
584 rc = sqlite3ApiExit(db, rc);
585 sqlite3_mutex_leave(db->mutex);
danielk19778e556522007-11-13 10:30:24 +0000586 }
danielk197776e8d1a2006-01-18 18:22:43 +0000587 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000588}
drhb900aaf2006-11-09 00:24:53 +0000589#endif
drh4f26d6c2004-05-26 23:25:30 +0000590
591/*
drheb2e1762004-05-27 01:53:56 +0000592** Extract the user data from a sqlite3_context structure and return a
593** pointer to it.
594*/
595void *sqlite3_user_data(sqlite3_context *p){
596 assert( p && p->pFunc );
597 return p->pFunc->pUserData;
598}
599
600/*
drhfa4a4b92008-03-19 21:45:51 +0000601** Extract the user data from a sqlite3_context structure and return a
602** pointer to it.
603*/
604sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
605 assert( p && p->pFunc );
606 return p->s.db;
607}
608
609/*
drhb7481e72006-09-16 21:45:14 +0000610** The following is the implementation of an SQL function that always
611** fails with an error message stating that the function is used in the
612** wrong context. The sqlite3_overload_function() API might construct
613** SQL function that use this routine so that the functions will exist
614** for name resolution but are actually overloaded by the xFindFunction
615** method of virtual tables.
616*/
617void sqlite3InvalidFunction(
618 sqlite3_context *context, /* The function calling context */
danielk197762c14b32008-11-19 09:05:26 +0000619 int NotUsed, /* Number of arguments to the function */
620 sqlite3_value **NotUsed2 /* Value of each argument */
drhb7481e72006-09-16 21:45:14 +0000621){
622 const char *zName = context->pFunc->zName;
623 char *zErr;
danielk197762c14b32008-11-19 09:05:26 +0000624 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbc6160b2009-04-08 15:45:31 +0000625 zErr = sqlite3_mprintf(
drhb7481e72006-09-16 21:45:14 +0000626 "unable to use function %s in the requested context", zName);
627 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000628 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000629}
630
631/*
drheb2e1762004-05-27 01:53:56 +0000632** Allocate or return the aggregate context for a user function. A new
633** context is allocated on the first call. Subsequent calls return the
634** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000635*/
636void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhb21c8cd2007-08-21 19:33:56 +0000637 Mem *pMem;
drh825c6622005-09-08 19:01:05 +0000638 assert( p && p->pFunc && p->pFunc->xStep );
drhb21c8cd2007-08-21 19:33:56 +0000639 assert( sqlite3_mutex_held(p->s.db->mutex) );
640 pMem = p->pMem;
drha10a34b2005-09-07 22:09:48 +0000641 if( (pMem->flags & MEM_Agg)==0 ){
642 if( nByte==0 ){
danielk19775f096132008-03-28 15:44:09 +0000643 sqlite3VdbeMemReleaseExternal(pMem);
644 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000645 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000646 }else{
danielk19775f096132008-03-28 15:44:09 +0000647 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000648 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000649 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000650 if( pMem->z ){
651 memset(pMem->z, 0, nByte);
652 }
drheb2e1762004-05-27 01:53:56 +0000653 }
654 }
drhabfcea22005-09-06 20:36:48 +0000655 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000656}
657
658/*
danielk1977682f68b2004-06-05 10:22:17 +0000659** Return the auxilary data pointer, if any, for the iArg'th argument to
660** the user-function defined by pCtx.
661*/
662void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000663 VdbeFunc *pVdbeFunc;
664
665 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
666 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000667 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
668 return 0;
669 }
drhf92c7ff2004-06-19 15:40:23 +0000670 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000671}
672
673/*
674** Set the auxilary data pointer and delete function, for the iArg'th
675** argument to the user-function defined by pCtx. Any previous value is
676** deleted by calling the delete function specified when it was set.
677*/
678void sqlite3_set_auxdata(
679 sqlite3_context *pCtx,
680 int iArg,
681 void *pAux,
682 void (*xDelete)(void*)
683){
684 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000685 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000686 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000687
drhb21c8cd2007-08-21 19:33:56 +0000688 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000689 pVdbeFunc = pCtx->pVdbeFunc;
690 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000691 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000692 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000693 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000694 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000695 goto failed;
696 }
drh53f733c2005-09-16 02:38:09 +0000697 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000698 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000699 pVdbeFunc->nAux = iArg+1;
700 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000701 }
702
drhf92c7ff2004-06-19 15:40:23 +0000703 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000704 if( pAuxData->pAux && pAuxData->xDelete ){
705 pAuxData->xDelete(pAuxData->pAux);
706 }
707 pAuxData->pAux = pAux;
708 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000709 return;
710
711failed:
712 if( xDelete ){
713 xDelete(pAux);
714 }
danielk1977682f68b2004-06-05 10:22:17 +0000715}
716
shaneeec556d2008-10-12 00:27:53 +0000717#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000718/*
drheb2e1762004-05-27 01:53:56 +0000719** Return the number of times the Step function of a aggregate has been
720** called.
721**
drhcf85a512006-02-09 18:35:29 +0000722** This function is deprecated. Do not use it for new code. It is
723** provide only to avoid breaking legacy code. New aggregate function
724** implementations should keep their own counts within their aggregate
725** context.
drheb2e1762004-05-27 01:53:56 +0000726*/
727int sqlite3_aggregate_count(sqlite3_context *p){
shanee34c6472009-03-05 04:23:47 +0000728 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000729 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000730}
shaneeec556d2008-10-12 00:27:53 +0000731#endif
drheb2e1762004-05-27 01:53:56 +0000732
733/*
drh4f26d6c2004-05-26 23:25:30 +0000734** Return the number of columns in the result set for the statement pStmt.
735*/
736int sqlite3_column_count(sqlite3_stmt *pStmt){
737 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000738 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000739}
740
741/*
742** Return the number of values available from the current row of the
743** currently executing statement pStmt.
744*/
745int sqlite3_data_count(sqlite3_stmt *pStmt){
746 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000747 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000748 return pVm->nResColumn;
749}
750
751
752/*
753** Check to see if column iCol of the given statement is valid. If
754** it is, return a pointer to the Mem for the value of that column.
755** If iCol is not valid, return a pointer to a Mem which has a value
756** of NULL.
757*/
758static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000759 Vdbe *pVm;
760 int vals;
761 Mem *pOut;
762
763 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000764 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000765 sqlite3_mutex_enter(pVm->db->mutex);
766 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000767 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000768 }else{
danielk197741f5b042009-06-06 14:13:26 +0000769 /* If the value passed as the second argument is out of range, return
770 ** a pointer to the following static Mem object which contains the
771 ** value SQL NULL. Even though the Mem structure contains an element
772 ** of type i64, on certain architecture (x86) with certain compiler
773 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
774 ** instead of an 8-byte one. This all works fine, except that when
775 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
776 ** that a Mem structure is located on an 8-byte boundary. To prevent
777 ** this assert() from failing, when building with SQLITE_DEBUG defined
778 ** using gcc, force nullMem to be 8-byte aligned using the magical
779 ** __attribute__((aligned(8))) macro. */
780 static const Mem nullMem
781#if defined(SQLITE_DEBUG) && defined(__GNUC__)
782 __attribute__((aligned(8)))
783#endif
784 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
785
drh9b4ea4a2009-04-10 20:32:00 +0000786 if( pVm && ALWAYS(pVm->db) ){
drh27641702007-08-22 02:56:42 +0000787 sqlite3_mutex_enter(pVm->db->mutex);
788 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
789 }
drh32bc3f62007-08-21 20:25:39 +0000790 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000791 }
drh32bc3f62007-08-21 20:25:39 +0000792 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000793}
794
danielk19772e588c72005-12-09 14:25:08 +0000795/*
796** This function is called after invoking an sqlite3_value_XXX function on a
797** column value (i.e. a value returned by evaluating an SQL expression in the
798** select list of a SELECT statement) that may cause a malloc() failure. If
799** malloc() has failed, the threads mallocFailed flag is cleared and the result
800** code of statement pStmt set to SQLITE_NOMEM.
801**
drh32bc3f62007-08-21 20:25:39 +0000802** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000803**
804** sqlite3_column_int()
805** sqlite3_column_int64()
806** sqlite3_column_text()
807** sqlite3_column_text16()
808** sqlite3_column_real()
809** sqlite3_column_bytes()
810** sqlite3_column_bytes16()
811**
812** But not for sqlite3_column_blob(), which never calls malloc().
813*/
814static void columnMallocFailure(sqlite3_stmt *pStmt)
815{
816 /* If malloc() failed during an encoding conversion within an
817 ** sqlite3_column_XXX API, then set the return code of the statement to
818 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
819 ** and _finalize() will return NOMEM.
820 */
danielk197754f01982006-01-18 15:25:17 +0000821 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000822 if( p ){
823 p->rc = sqlite3ApiExit(p->db, p->rc);
824 sqlite3_mutex_leave(p->db->mutex);
825 }
danielk19772e588c72005-12-09 14:25:08 +0000826}
827
drh4f26d6c2004-05-26 23:25:30 +0000828/**************************** sqlite3_column_ *******************************
829** The following routines are used to access elements of the current row
830** in the result set.
831*/
danielk1977c572ef72004-05-27 09:28:41 +0000832const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000833 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000834 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000835 /* Even though there is no encoding conversion, value_blob() might
836 ** need to call malloc() to expand the result of a zeroblob()
837 ** expression.
838 */
839 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000840 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000841}
drh4f26d6c2004-05-26 23:25:30 +0000842int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000843 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
844 columnMallocFailure(pStmt);
845 return val;
drh4f26d6c2004-05-26 23:25:30 +0000846}
847int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000848 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
849 columnMallocFailure(pStmt);
850 return val;
drh4f26d6c2004-05-26 23:25:30 +0000851}
852double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000853 double val = sqlite3_value_double( columnMem(pStmt,i) );
854 columnMallocFailure(pStmt);
855 return val;
drh4f26d6c2004-05-26 23:25:30 +0000856}
857int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000858 int val = sqlite3_value_int( columnMem(pStmt,i) );
859 columnMallocFailure(pStmt);
860 return val;
drh4f26d6c2004-05-26 23:25:30 +0000861}
drhefad9992004-06-22 12:13:55 +0000862sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000863 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
864 columnMallocFailure(pStmt);
865 return val;
drh4f26d6c2004-05-26 23:25:30 +0000866}
867const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000868 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
869 columnMallocFailure(pStmt);
870 return val;
drh4f26d6c2004-05-26 23:25:30 +0000871}
drhd1e47332005-06-26 17:55:33 +0000872sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +0000873 Mem *pOut = columnMem(pStmt, i);
874 if( pOut->flags&MEM_Static ){
875 pOut->flags &= ~MEM_Static;
876 pOut->flags |= MEM_Ephem;
877 }
drh32bc3f62007-08-21 20:25:39 +0000878 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +0000879 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +0000880}
drh6c626082004-11-14 21:56:29 +0000881#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000882const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000883 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
884 columnMallocFailure(pStmt);
885 return val;
drh4f26d6c2004-05-26 23:25:30 +0000886}
drh6c626082004-11-14 21:56:29 +0000887#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000888int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000889 int iType = sqlite3_value_type( columnMem(pStmt,i) );
890 columnMallocFailure(pStmt);
891 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000892}
893
drh29d72102006-02-09 22:13:41 +0000894/* The following function is experimental and subject to change or
895** removal */
896/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
897** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
898**}
899*/
900
drh5e2517e2004-09-24 23:59:12 +0000901/*
902** Convert the N-th element of pStmt->pColName[] into a string using
903** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000904**
905** There are up to 5 names for each column. useType determines which
906** name is returned. Here are the names:
907**
908** 0 The column name as it should be displayed for output
909** 1 The datatype name for the column
910** 2 The name of the database that the column derives from
911** 3 The name of the table that the column derives from
912** 4 The name of the table column that the result column derives from
913**
914** If the result is not a simple column reference (if it is an expression
915** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000916*/
917static const void *columnName(
918 sqlite3_stmt *pStmt,
919 int N,
920 const void *(*xFunc)(Mem*),
921 int useType
922){
drh32bc3f62007-08-21 20:25:39 +0000923 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000924 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000925 int n;
drhc6c7fd52009-04-08 23:05:28 +0000926 sqlite3 *db = p->db;
drh32bc3f62007-08-21 20:25:39 +0000927
drh9b4ea4a2009-04-10 20:32:00 +0000928 assert( db!=0 );
929 n = sqlite3_column_count(pStmt);
930 if( N<n && N>=0 ){
931 N += useType*n;
932 sqlite3_mutex_enter(db->mutex);
933 assert( db->mallocFailed==0 );
934 ret = xFunc(&p->aColName[N]);
935 /* A malloc may have failed inside of the xFunc() call. If this
936 ** is the case, clear the mallocFailed flag and return NULL.
937 */
938 if( db->mallocFailed ){
939 db->mallocFailed = 0;
940 ret = 0;
drh32bc3f62007-08-21 20:25:39 +0000941 }
drh9b4ea4a2009-04-10 20:32:00 +0000942 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +0000943 }
danielk197700fd9572005-12-07 06:27:43 +0000944 return ret;
drh5e2517e2004-09-24 23:59:12 +0000945}
946
drh4f26d6c2004-05-26 23:25:30 +0000947/*
948** Return the name of the Nth column of the result set returned by SQL
949** statement pStmt.
950*/
951const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000952 return columnName(
953 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000954}
drhe590fbd2005-05-20 19:36:01 +0000955#ifndef SQLITE_OMIT_UTF16
956const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000957 return columnName(
958 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000959}
960#endif
drh4f26d6c2004-05-26 23:25:30 +0000961
962/*
drh3f913572008-03-22 01:07:17 +0000963** Constraint: If you have ENABLE_COLUMN_METADATA then you must
964** not define OMIT_DECLTYPE.
965*/
966#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
967# error "Must not define both SQLITE_OMIT_DECLTYPE \
968 and SQLITE_ENABLE_COLUMN_METADATA"
969#endif
970
971#ifndef SQLITE_OMIT_DECLTYPE
972/*
drh6c626082004-11-14 21:56:29 +0000973** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000974** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000975*/
976const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000977 return columnName(
978 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000979}
drh6c626082004-11-14 21:56:29 +0000980#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000981const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000982 return columnName(
983 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000984}
drh6c626082004-11-14 21:56:29 +0000985#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000986#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000987
danielk19774b1ae992006-02-10 03:06:10 +0000988#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000989/*
990** Return the name of the database from which a result column derives.
991** NULL is returned if the result column is an expression or constant or
992** anything else which is not an unabiguous reference to a database column.
993*/
994const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000995 return columnName(
996 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000997}
998#ifndef SQLITE_OMIT_UTF16
999const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001000 return columnName(
1001 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +00001002}
1003#endif /* SQLITE_OMIT_UTF16 */
1004
1005/*
1006** Return the name of the table from which a result column derives.
1007** NULL is returned if the result column is an expression or constant or
1008** anything else which is not an unabiguous reference to a database column.
1009*/
1010const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001011 return columnName(
1012 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001013}
1014#ifndef SQLITE_OMIT_UTF16
1015const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001016 return columnName(
1017 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +00001018}
1019#endif /* SQLITE_OMIT_UTF16 */
1020
1021/*
1022** Return the name of the table column from which a result column derives.
1023** NULL is returned if the result column is an expression or constant or
1024** anything else which is not an unabiguous reference to a database column.
1025*/
1026const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001027 return columnName(
1028 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001029}
1030#ifndef SQLITE_OMIT_UTF16
1031const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +00001032 return columnName(
1033 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +00001034}
1035#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +00001036#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +00001037
1038
drh4f26d6c2004-05-26 23:25:30 +00001039/******************************* sqlite3_bind_ ***************************
1040**
1041** Routines used to attach values to wildcards in a compiled SQL statement.
1042*/
1043/*
1044** Unbind the value bound to variable i in virtual machine p. This is the
1045** the same as binding a NULL value to the column. If the "i" parameter is
1046** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1047**
drh488e7b62008-10-06 12:46:43 +00001048** A successful evaluation of this routine acquires the mutex on p.
1049** the mutex is released if any kind of error occurs.
1050**
drh4f26d6c2004-05-26 23:25:30 +00001051** The error code stored in database p->db is overwritten with the return
1052** value in any case.
1053*/
1054static int vdbeUnbind(Vdbe *p, int i){
1055 Mem *pVar;
drh488e7b62008-10-06 12:46:43 +00001056 if( p==0 ) return SQLITE_MISUSE;
1057 sqlite3_mutex_enter(p->db->mutex);
1058 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
1059 sqlite3Error(p->db, SQLITE_MISUSE, 0);
1060 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001061 return SQLITE_MISUSE;
1062 }
1063 if( i<1 || i>p->nVar ){
1064 sqlite3Error(p->db, SQLITE_RANGE, 0);
drh488e7b62008-10-06 12:46:43 +00001065 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001066 return SQLITE_RANGE;
1067 }
1068 i--;
drh290c1942004-08-21 17:54:45 +00001069 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001070 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001071 pVar->flags = MEM_Null;
1072 sqlite3Error(p->db, SQLITE_OK, 0);
1073 return SQLITE_OK;
1074}
1075
1076/*
drh5e2517e2004-09-24 23:59:12 +00001077** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001078*/
drh5e2517e2004-09-24 23:59:12 +00001079static int bindText(
drh605264d2007-08-21 15:13:19 +00001080 sqlite3_stmt *pStmt, /* The statement to bind against */
1081 int i, /* Index of the parameter to bind */
1082 const void *zData, /* Pointer to the data to be bound */
1083 int nData, /* Number of bytes of data to be bound */
1084 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +00001085 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001086){
1087 Vdbe *p = (Vdbe *)pStmt;
1088 Mem *pVar;
1089 int rc;
1090
1091 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001092 if( rc==SQLITE_OK ){
1093 if( zData!=0 ){
1094 pVar = &p->aVar[i-1];
1095 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1096 if( rc==SQLITE_OK && encoding!=0 ){
1097 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1098 }
1099 sqlite3Error(p->db, rc, 0);
1100 rc = sqlite3ApiExit(p->db, rc);
drh27641702007-08-22 02:56:42 +00001101 }
drh488e7b62008-10-06 12:46:43 +00001102 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001103 }
drh605264d2007-08-21 15:13:19 +00001104 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001105}
drh5e2517e2004-09-24 23:59:12 +00001106
1107
1108/*
1109** Bind a blob value to an SQL statement variable.
1110*/
1111int sqlite3_bind_blob(
1112 sqlite3_stmt *pStmt,
1113 int i,
1114 const void *zData,
1115 int nData,
1116 void (*xDel)(void*)
1117){
1118 return bindText(pStmt, i, zData, nData, xDel, 0);
1119}
drh4f26d6c2004-05-26 23:25:30 +00001120int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1121 int rc;
1122 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +00001123 rc = vdbeUnbind(p, i);
1124 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001125 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +00001126 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001127 }
danielk1977f4618892004-06-28 13:09:11 +00001128 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001129}
1130int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +00001131 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001132}
drhefad9992004-06-22 12:13:55 +00001133int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001134 int rc;
1135 Vdbe *p = (Vdbe *)pStmt;
1136 rc = vdbeUnbind(p, i);
1137 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001138 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +00001139 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001140 }
1141 return rc;
1142}
drh605264d2007-08-21 15:13:19 +00001143int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1144 int rc;
1145 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +00001146 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +00001147 if( rc==SQLITE_OK ){
1148 sqlite3_mutex_leave(p->db->mutex);
1149 }
drh605264d2007-08-21 15:13:19 +00001150 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001151}
1152int sqlite3_bind_text(
1153 sqlite3_stmt *pStmt,
1154 int i,
1155 const char *zData,
1156 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001157 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001158){
drh5e2517e2004-09-24 23:59:12 +00001159 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001160}
drh6c626082004-11-14 21:56:29 +00001161#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001162int sqlite3_bind_text16(
1163 sqlite3_stmt *pStmt,
1164 int i,
1165 const void *zData,
1166 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001167 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001168){
drh5e2517e2004-09-24 23:59:12 +00001169 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001170}
drh6c626082004-11-14 21:56:29 +00001171#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001172int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1173 int rc;
drh29def562009-04-14 12:43:33 +00001174 switch( pValue->type ){
drh29def562009-04-14 12:43:33 +00001175 case SQLITE_INTEGER: {
1176 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1177 break;
1178 }
1179 case SQLITE_FLOAT: {
1180 rc = sqlite3_bind_double(pStmt, i, pValue->r);
1181 break;
1182 }
1183 case SQLITE_BLOB: {
1184 if( pValue->flags & MEM_Zero ){
1185 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1186 }else{
1187 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1188 }
1189 break;
1190 }
1191 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001192 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1193 pValue->enc);
1194 break;
1195 }
1196 default: {
1197 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001198 break;
1199 }
danielk197726e41442006-06-14 15:16:35 +00001200 }
1201 return rc;
1202}
drhb026e052007-05-02 01:34:31 +00001203int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1204 int rc;
1205 Vdbe *p = (Vdbe *)pStmt;
1206 rc = vdbeUnbind(p, i);
1207 if( rc==SQLITE_OK ){
1208 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001209 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001210 }
1211 return rc;
1212}
drh75f6a032004-07-15 14:15:00 +00001213
1214/*
1215** Return the number of wildcards that can be potentially bound to.
1216** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001217*/
1218int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001219 Vdbe *p = (Vdbe*)pStmt;
1220 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001221}
drh895d7472004-08-20 16:02:39 +00001222
1223/*
drhfa6bc002004-09-07 16:19:52 +00001224** Create a mapping from variable numbers to variable names
1225** in the Vdbe.azVar[] array, if such a mapping does not already
1226** exist.
drh895d7472004-08-20 16:02:39 +00001227*/
drhfa6bc002004-09-07 16:19:52 +00001228static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001229 if( !p->okVar ){
drh0f5ea0b2009-04-09 14:02:44 +00001230 int j;
1231 Op *pOp;
drh605264d2007-08-21 15:13:19 +00001232 sqlite3_mutex_enter(p->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001233 /* The race condition here is harmless. If two threads call this
1234 ** routine on the same Vdbe at the same time, they both might end
1235 ** up initializing the Vdbe.azVar[] array. That is a little extra
1236 ** work but it results in the same answer.
1237 */
1238 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1239 if( pOp->opcode==OP_Variable ){
1240 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1241 p->azVar[pOp->p1-1] = pOp->p4.z;
drh895d7472004-08-20 16:02:39 +00001242 }
1243 }
drh0f5ea0b2009-04-09 14:02:44 +00001244 p->okVar = 1;
drh605264d2007-08-21 15:13:19 +00001245 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001246 }
drhfa6bc002004-09-07 16:19:52 +00001247}
1248
1249/*
1250** Return the name of a wildcard parameter. Return NULL if the index
1251** is out of range or if the wildcard is unnamed.
1252**
1253** The result is always UTF-8.
1254*/
1255const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1256 Vdbe *p = (Vdbe*)pStmt;
1257 if( p==0 || i<1 || i>p->nVar ){
1258 return 0;
1259 }
1260 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001261 return p->azVar[i-1];
1262}
drhfa6bc002004-09-07 16:19:52 +00001263
1264/*
1265** Given a wildcard parameter name, return the index of the variable
1266** with that name. If there is no variable with the given name,
1267** return 0.
1268*/
1269int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1270 Vdbe *p = (Vdbe*)pStmt;
1271 int i;
1272 if( p==0 ){
1273 return 0;
1274 }
1275 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +00001276 if( zName ){
1277 for(i=0; i<p->nVar; i++){
1278 const char *z = p->azVar[i];
1279 if( z && strcmp(z,zName)==0 ){
1280 return i+1;
1281 }
drhfa6bc002004-09-07 16:19:52 +00001282 }
1283 }
1284 return 0;
1285}
drhf8db1bc2005-04-22 02:38:37 +00001286
drh51942bc2005-06-12 22:01:42 +00001287/*
drhf8db1bc2005-04-22 02:38:37 +00001288** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001289*/
shane145834a2008-09-04 12:03:42 +00001290int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001291 Vdbe *pFrom = (Vdbe*)pFromStmt;
1292 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001293 int i;
1294 assert( pTo->db==pFrom->db );
1295 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001296 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001297 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001298 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001299 }
drh27641702007-08-22 02:56:42 +00001300 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001301 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001302}
drh51942bc2005-06-12 22:01:42 +00001303
shaneeec556d2008-10-12 00:27:53 +00001304#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001305/*
shane145834a2008-09-04 12:03:42 +00001306** Deprecated external interface. Internal/core SQLite code
1307** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001308**
1309** Is is misuse to call this routine with statements from different
1310** database connections. But as this is a deprecated interface, we
1311** will not bother to check for that condition.
1312**
1313** If the two statements contain a different number of bindings, then
1314** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1315** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001316*/
1317int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001318 Vdbe *pFrom = (Vdbe*)pFromStmt;
1319 Vdbe *pTo = (Vdbe*)pToStmt;
1320 if( pFrom->nVar!=pTo->nVar ){
1321 return SQLITE_ERROR;
1322 }
shane145834a2008-09-04 12:03:42 +00001323 return sqlite3TransferBindings(pFromStmt, pToStmt);
1324}
shaneeec556d2008-10-12 00:27:53 +00001325#endif
shane145834a2008-09-04 12:03:42 +00001326
1327/*
drh51942bc2005-06-12 22:01:42 +00001328** Return the sqlite3* database handle to which the prepared statement given
1329** in the argument belongs. This is the same database handle that was
1330** the first argument to the sqlite3_prepare() that was used to create
1331** the statement in the first place.
1332*/
1333sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1334 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1335}
drhbb5a9c32008-06-19 02:52:25 +00001336
1337/*
1338** Return a pointer to the next prepared statement after pStmt associated
1339** with database connection pDb. If pStmt is NULL, return the first
1340** prepared statement for the database connection. Return NULL if there
1341** are no more.
1342*/
1343sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1344 sqlite3_stmt *pNext;
1345 sqlite3_mutex_enter(pDb->mutex);
1346 if( pStmt==0 ){
1347 pNext = (sqlite3_stmt*)pDb->pVdbe;
1348 }else{
1349 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1350 }
1351 sqlite3_mutex_leave(pDb->mutex);
1352 return pNext;
1353}
drhd1d38482008-10-07 23:46:38 +00001354
1355/*
1356** Return the value of a status counter for a prepared statement
1357*/
1358int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1359 Vdbe *pVdbe = (Vdbe*)pStmt;
1360 int v = pVdbe->aCounter[op-1];
1361 if( resetFlag ) pVdbe->aCounter[op-1] = 0;
1362 return v;
1363}