blob: 32ca9996749b517beaa89c961b8cbc1793b7a6b2 [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
2** 2004 May 26
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains code use to implement APIs that are part of the
14** VDBE.
15*/
16#include "sqliteInt.h"
17#include "vdbeInt.h"
18
danielk1977dfb316d2008-03-26 18:34:43 +000019#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
20/*
21** The following structure contains pointers to the end points of a
22** doubly-linked list of all compiled SQL statements that may be holding
23** buffers eligible for release when the sqlite3_release_memory() interface is
24** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
25** mutex.
26**
27** Statements are added to the end of this list when sqlite3_reset() is
28** called. They are removed either when sqlite3_step() or sqlite3_finalize()
29** is called. When statements are added to this list, the associated
30** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
31** can be freed using sqlite3VdbeReleaseMemory().
32**
33** When statements are added or removed from this list, the mutex
34** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
35** already held. The LRU2 mutex is then obtained, blocking if necessary,
36** the linked-list pointers manipulated and the LRU2 mutex relinquished.
37*/
38struct StatementLruList {
39 Vdbe *pFirst;
40 Vdbe *pLast;
41};
42static struct StatementLruList sqlite3LruStatements;
43
44/*
45** Check that the list looks to be internally consistent. This is used
46** as part of an assert() statement as follows:
47**
48** assert( stmtLruCheck() );
49*/
50static int stmtLruCheck(){
51 Vdbe *p;
52 for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
53 assert(p->pLruNext || p==sqlite3LruStatements.pLast);
54 assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
55 assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
56 assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
57 }
58 return 1;
59}
60
61/*
62** Add vdbe p to the end of the statement lru list. It is assumed that
63** p is not already part of the list when this is called. The lru list
64** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
65*/
66static void stmtLruAdd(Vdbe *p){
67 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
68
69 if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
70 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
71 return;
72 }
73
74 assert( stmtLruCheck() );
75
76 if( !sqlite3LruStatements.pFirst ){
77 assert( !sqlite3LruStatements.pLast );
78 sqlite3LruStatements.pFirst = p;
79 sqlite3LruStatements.pLast = p;
80 }else{
81 assert( !sqlite3LruStatements.pLast->pLruNext );
82 p->pLruPrev = sqlite3LruStatements.pLast;
83 sqlite3LruStatements.pLast->pLruNext = p;
84 sqlite3LruStatements.pLast = p;
85 }
86
87 assert( stmtLruCheck() );
88
89 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
90}
91
92/*
93** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
94** statement p from the least-recently-used statement list. If the
95** statement is not currently part of the list, this call is a no-op.
96*/
97static void stmtLruRemoveNomutex(Vdbe *p){
98 if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
99 assert( stmtLruCheck() );
100 if( p->pLruNext ){
101 p->pLruNext->pLruPrev = p->pLruPrev;
102 }else{
103 sqlite3LruStatements.pLast = p->pLruPrev;
104 }
105 if( p->pLruPrev ){
106 p->pLruPrev->pLruNext = p->pLruNext;
107 }else{
108 sqlite3LruStatements.pFirst = p->pLruNext;
109 }
110 p->pLruNext = 0;
111 p->pLruPrev = 0;
112 assert( stmtLruCheck() );
113 }
114}
115
116/*
117** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
118** statement p from the least-recently-used statement list. If the
119** statement is not currently part of the list, this call is a no-op.
120*/
121static void stmtLruRemove(Vdbe *p){
122 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
123 stmtLruRemoveNomutex(p);
124 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
125}
126
127/*
128** Try to release n bytes of memory by freeing buffers associated
129** with the memory registers of currently unused vdbes.
130*/
131int sqlite3VdbeReleaseMemory(int n){
132 Vdbe *p;
133 Vdbe *pNext;
134 int nFree = 0;
135
136 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
137 for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
138 pNext = p->pLruNext;
139
140 /* For each statement handle in the lru list, attempt to obtain the
141 ** associated database mutex. If it cannot be obtained, continue
142 ** to the next statement handle. It is not possible to block on
143 ** the database mutex - that could cause deadlock.
144 */
145 if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
146 nFree += sqlite3VdbeReleaseBuffers(p);
147 stmtLruRemoveNomutex(p);
148 sqlite3_mutex_leave(p->db->mutex);
149 }
150 }
151 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
152
153 return nFree;
154}
155
156/*
157** Call sqlite3Reprepare() on the statement. Remove it from the
158** lru list before doing so, as Reprepare() will free all the
159** memory register buffers anyway.
160*/
161int vdbeReprepare(Vdbe *p){
162 stmtLruRemove(p);
163 return sqlite3Reprepare(p);
164}
165
166#else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
167 #define stmtLruRemove(x)
168 #define stmtLruAdd(x)
169 #define vdbeReprepare(x) sqlite3Reprepare(x)
170#endif
171
172
drhd89bd002005-01-22 03:03:54 +0000173/*
174** Return TRUE (non-zero) of the statement supplied as an argument needs
175** to be recompiled. A statement needs to be recompiled whenever the
176** execution environment changes in a way that would alter the program
177** that sqlite3_prepare() generates. For example, if new functions or
178** collating sequences are registered or if an authorizer function is
179** added or changed.
drhd89bd002005-01-22 03:03:54 +0000180*/
181int sqlite3_expired(sqlite3_stmt *pStmt){
182 Vdbe *p = (Vdbe*)pStmt;
183 return p==0 || p->expired;
184}
185
drhe30f4422007-08-21 16:15:55 +0000186/*
187** The following routine destroys a virtual machine that is created by
188** the sqlite3_compile() routine. The integer returned is an SQLITE_
189** success/failure code that describes the result of executing the virtual
190** machine.
191**
192** This routine sets the error code and string returned by
193** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
194*/
195int sqlite3_finalize(sqlite3_stmt *pStmt){
196 int rc;
197 if( pStmt==0 ){
198 rc = SQLITE_OK;
199 }else{
200 Vdbe *v = (Vdbe*)pStmt;
drh7e8b8482008-01-23 03:03:05 +0000201#ifndef SQLITE_MUTEX_NOOP
drh86f8c192007-08-22 00:39:19 +0000202 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +0000203#endif
drh86f8c192007-08-22 00:39:19 +0000204 sqlite3_mutex_enter(mutex);
danielk1977dfb316d2008-03-26 18:34:43 +0000205 stmtLruRemove(v);
drhe30f4422007-08-21 16:15:55 +0000206 rc = sqlite3VdbeFinalize(v);
drh86f8c192007-08-22 00:39:19 +0000207 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000208 }
209 return rc;
210}
211
212/*
213** Terminate the current execution of an SQL statement and reset it
214** back to its starting state so that it can be reused. A success code from
215** the prior execution is returned.
216**
217** This routine sets the error code and string returned by
218** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
219*/
220int sqlite3_reset(sqlite3_stmt *pStmt){
221 int rc;
222 if( pStmt==0 ){
223 rc = SQLITE_OK;
224 }else{
225 Vdbe *v = (Vdbe*)pStmt;
226 sqlite3_mutex_enter(v->db->mutex);
danielk1977dfb316d2008-03-26 18:34:43 +0000227 rc = sqlite3VdbeReset(v, 0);
228 stmtLruAdd(v);
drhe30f4422007-08-21 16:15:55 +0000229 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
230 assert( (rc & (v->db->errMask))==rc );
231 sqlite3_mutex_leave(v->db->mutex);
232 }
233 return rc;
234}
235
236/*
237** Set all the parameters in the compiled SQL statement to NULL.
238*/
239int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
240 int i;
241 int rc = SQLITE_OK;
drh7e8b8482008-01-23 03:03:05 +0000242#ifndef SQLITE_MUTEX_NOOP
243 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
244#endif
245 sqlite3_mutex_enter(mutex);
drhe30f4422007-08-21 16:15:55 +0000246 for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
247 rc = sqlite3_bind_null(pStmt, i);
248 }
drh7e8b8482008-01-23 03:03:05 +0000249 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000250 return rc;
251}
252
253
drh4f26d6c2004-05-26 23:25:30 +0000254/**************************** sqlite3_value_ *******************************
255** The following routines extract information from a Mem or sqlite3_value
256** structure.
257*/
258const void *sqlite3_value_blob(sqlite3_value *pVal){
259 Mem *p = (Mem*)pVal;
260 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000261 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000262 p->flags &= ~MEM_Str;
263 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000264 return p->z;
265 }else{
266 return sqlite3_value_text(pVal);
267 }
268}
269int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000270 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000271}
272int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000273 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000274}
275double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000276 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000277}
278int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000279 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000280}
drhefad9992004-06-22 12:13:55 +0000281sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000282 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000283}
284const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000285 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000286}
drh6c626082004-11-14 21:56:29 +0000287#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000288const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000289 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000290}
danielk1977d8123362004-06-12 09:25:12 +0000291const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000292 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000293}
294const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000295 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000296}
drh6c626082004-11-14 21:56:29 +0000297#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000298int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000299 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000300}
301
302/**************************** sqlite3_result_ *******************************
303** The following routines are used by user-defined functions to specify
304** the function result.
305*/
306void sqlite3_result_blob(
307 sqlite3_context *pCtx,
308 const void *z,
309 int n,
danielk1977d8123362004-06-12 09:25:12 +0000310 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000311){
drh5708d2d2005-06-22 10:53:59 +0000312 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000313 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000314 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000315}
316void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000317 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000318 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
319}
320void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000321 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000322 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000323 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000324}
danielk1977a1686c92006-01-23 07:52:37 +0000325#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000326void sqlite3_result_error16(sqlite3_context *pCtx, const void *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_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000330}
danielk1977a1686c92006-01-23 07:52:37 +0000331#endif
drhf4479502004-05-27 03:12:53 +0000332void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000333 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000334 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
335}
336void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000337 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000338 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
339}
340void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000341 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000342 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000343}
344void sqlite3_result_text(
345 sqlite3_context *pCtx,
346 const char *z,
347 int n,
danielk1977d8123362004-06-12 09:25:12 +0000348 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000349){
drh32bc3f62007-08-21 20:25:39 +0000350 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000351 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000352}
drh6c626082004-11-14 21:56:29 +0000353#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000354void sqlite3_result_text16(
355 sqlite3_context *pCtx,
356 const void *z,
357 int n,
danielk1977d8123362004-06-12 09:25:12 +0000358 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000359){
drh32bc3f62007-08-21 20:25:39 +0000360 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000361 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000362}
363void sqlite3_result_text16be(
364 sqlite3_context *pCtx,
365 const void *z,
366 int n,
367 void (*xDel)(void *)
368){
drh32bc3f62007-08-21 20:25:39 +0000369 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000370 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000371}
372void sqlite3_result_text16le(
373 sqlite3_context *pCtx,
374 const void *z,
375 int n,
376 void (*xDel)(void *)
377){
drh32bc3f62007-08-21 20:25:39 +0000378 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000379 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000380}
drh6c626082004-11-14 21:56:29 +0000381#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000382void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000383 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000384 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000385}
drhb026e052007-05-02 01:34:31 +0000386void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000387 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000388 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
389}
drh69544ec2008-02-06 14:11:34 +0000390void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
391 pCtx->isError = errCode;
392}
drh4f26d6c2004-05-26 23:25:30 +0000393
drha0206bc2007-05-08 15:15:02 +0000394/* Force an SQLITE_TOOBIG error. */
395void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000396 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000397 pCtx->isError = SQLITE_TOOBIG;
398 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
399 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000400}
401
danielk1977a1644fd2007-08-29 12:31:25 +0000402/* An SQLITE_NOMEM error. */
403void sqlite3_result_error_nomem(sqlite3_context *pCtx){
404 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
405 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000406 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000407 pCtx->s.db->mallocFailed = 1;
408}
drh4f26d6c2004-05-26 23:25:30 +0000409
410/*
411** Execute the statement pStmt, either until a row of data is ready, the
412** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000413**
414** This routine implements the bulk of the logic behind the sqlite_step()
415** API. The only thing omitted is the automatic recompile if a
416** schema change has occurred. That detail is handled by the
417** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000418*/
drhb900aaf2006-11-09 00:24:53 +0000419static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000420 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000421 int rc;
422
danielk1977a185ddb2007-11-15 16:04:15 +0000423 assert(p);
424 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000425 return SQLITE_MISUSE;
426 }
427
danielk1977efaaf572006-01-16 11:29:19 +0000428 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000429 db = p->db;
430 assert( !db->mallocFailed );
danielk1977261919c2005-12-06 12:52:59 +0000431
drh91b48aa2004-06-30 11:14:18 +0000432 if( p->aborted ){
433 return SQLITE_ABORT;
434 }
danielk1977a21c6b62005-01-24 10:25:59 +0000435 if( p->pc<=0 && p->expired ){
436 if( p->rc==SQLITE_OK ){
437 p->rc = SQLITE_SCHEMA;
438 }
drhb900aaf2006-11-09 00:24:53 +0000439 rc = SQLITE_ERROR;
440 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000441 }
drh4f26d6c2004-05-26 23:25:30 +0000442 if( sqlite3SafetyOn(db) ){
443 p->rc = SQLITE_MISUSE;
444 return SQLITE_MISUSE;
445 }
danielk19771d850a72004-05-31 08:26:49 +0000446 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000447 /* If there are no other statements currently running, then
448 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
449 ** from interrupting a statement that has not yet started.
450 */
451 if( db->activeVdbeCnt==0 ){
452 db->u1.isInterrupted = 0;
453 }
454
drh19e2d372005-08-29 23:00:03 +0000455#ifndef SQLITE_OMIT_TRACE
drh19e2d372005-08-29 23:00:03 +0000456 if( db->xProfile && !db->init.busy ){
457 double rNow;
danielk1977b4b47412007-08-17 15:53:36 +0000458 sqlite3OsCurrentTime(db->pVfs, &rNow);
drh19e2d372005-08-29 23:00:03 +0000459 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
460 }
461#endif
drhc16a03b2004-09-15 13:38:10 +0000462
danielk19771d850a72004-05-31 08:26:49 +0000463 db->activeVdbeCnt++;
464 p->pc = 0;
danielk1977dfb316d2008-03-26 18:34:43 +0000465 stmtLruRemove(p);
danielk19771d850a72004-05-31 08:26:49 +0000466 }
drhb7f91642004-10-31 02:22:47 +0000467#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000468 if( p->explain ){
469 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000470 }else
471#endif /* SQLITE_OMIT_EXPLAIN */
472 {
drh4f26d6c2004-05-26 23:25:30 +0000473 rc = sqlite3VdbeExec(p);
474 }
475
476 if( sqlite3SafetyOff(db) ){
477 rc = SQLITE_MISUSE;
478 }
479
drh19e2d372005-08-29 23:00:03 +0000480#ifndef SQLITE_OMIT_TRACE
481 /* Invoke the profile callback if there is one
482 */
drh949f9cd2008-01-12 21:35:57 +0000483 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
484 && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
drh19e2d372005-08-29 23:00:03 +0000485 double rNow;
486 u64 elapseTime;
487
danielk1977b4b47412007-08-17 15:53:36 +0000488 sqlite3OsCurrentTime(db->pVfs, &rNow);
drh19e2d372005-08-29 23:00:03 +0000489 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
drh949f9cd2008-01-12 21:35:57 +0000490 db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
drh19e2d372005-08-29 23:00:03 +0000491 }
492#endif
493
danielk197797a227c2006-01-20 16:32:04 +0000494 sqlite3Error(p->db, rc, 0);
danielk197776e8d1a2006-01-18 18:22:43 +0000495 p->rc = sqlite3ApiExit(p->db, p->rc);
drhb900aaf2006-11-09 00:24:53 +0000496end_of_step:
drh4ac285a2006-09-15 07:28:50 +0000497 assert( (rc&0xff)==rc );
drhb900aaf2006-11-09 00:24:53 +0000498 if( p->zSql && (rc&0xff)<SQLITE_ROW ){
499 /* This behavior occurs if sqlite3_prepare_v2() was used to build
500 ** the prepared statement. Return error codes directly */
danielk1977612642d2007-07-12 13:18:05 +0000501 sqlite3Error(p->db, p->rc, 0);
drhb900aaf2006-11-09 00:24:53 +0000502 return p->rc;
503 }else{
504 /* This is for legacy sqlite3_prepare() builds and when the code
505 ** is SQLITE_ROW or SQLITE_DONE */
506 return rc;
507 }
508}
509
510/*
511** This is the top-level implementation of sqlite3_step(). Call
512** sqlite3Step() to do most of the work. If a schema error occurs,
513** call sqlite3Reprepare() and try again.
514*/
515#ifdef SQLITE_OMIT_PARSER
516int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000517 int rc = SQLITE_MISUSE;
518 if( pStmt ){
519 Vdbe *v;
520 v = (Vdbe*)pStmt;
521 sqlite3_mutex_enter(v->db->mutex);
522 rc = sqlite3Step(v);
523 sqlite3_mutex_leave(v->db->mutex);
524 }
drhb21c8cd2007-08-21 19:33:56 +0000525 return rc;
drhb900aaf2006-11-09 00:24:53 +0000526}
527#else
528int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000529 int rc = SQLITE_MISUSE;
530 if( pStmt ){
531 int cnt = 0;
532 Vdbe *v = (Vdbe*)pStmt;
533 sqlite3 *db = v->db;
534 sqlite3_mutex_enter(db->mutex);
535 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
536 && cnt++ < 5
danielk1977dfb316d2008-03-26 18:34:43 +0000537 && vdbeReprepare(v) ){
danielk1977a185ddb2007-11-15 16:04:15 +0000538 sqlite3_reset(pStmt);
539 v->expired = 0;
danielk19778e556522007-11-13 10:30:24 +0000540 }
danielk1977a185ddb2007-11-15 16:04:15 +0000541 if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
542 /* This case occurs after failing to recompile an sql statement.
543 ** The error message from the SQL compiler has already been loaded
544 ** into the database handle. This block copies the error message
545 ** from the database handle into the statement and sets the statement
546 ** program counter to 0 to ensure that when the statement is
547 ** finalized or reset the parser error message is available via
548 ** sqlite3_errmsg() and sqlite3_errcode().
549 */
550 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
551 sqlite3_free(v->zErrMsg);
552 if( !db->mallocFailed ){
553 v->zErrMsg = sqlite3DbStrDup(db, zErr);
554 } else {
555 v->zErrMsg = 0;
556 v->rc = SQLITE_NOMEM;
557 }
558 }
559 rc = sqlite3ApiExit(db, rc);
560 sqlite3_mutex_leave(db->mutex);
danielk19778e556522007-11-13 10:30:24 +0000561 }
danielk197776e8d1a2006-01-18 18:22:43 +0000562 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000563}
drhb900aaf2006-11-09 00:24:53 +0000564#endif
drh4f26d6c2004-05-26 23:25:30 +0000565
566/*
drheb2e1762004-05-27 01:53:56 +0000567** Extract the user data from a sqlite3_context structure and return a
568** pointer to it.
569*/
570void *sqlite3_user_data(sqlite3_context *p){
571 assert( p && p->pFunc );
572 return p->pFunc->pUserData;
573}
574
575/*
drhfa4a4b92008-03-19 21:45:51 +0000576** Extract the user data from a sqlite3_context structure and return a
577** pointer to it.
578*/
579sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
580 assert( p && p->pFunc );
581 return p->s.db;
582}
583
584/*
drhb7481e72006-09-16 21:45:14 +0000585** The following is the implementation of an SQL function that always
586** fails with an error message stating that the function is used in the
587** wrong context. The sqlite3_overload_function() API might construct
588** SQL function that use this routine so that the functions will exist
589** for name resolution but are actually overloaded by the xFindFunction
590** method of virtual tables.
591*/
592void sqlite3InvalidFunction(
593 sqlite3_context *context, /* The function calling context */
594 int argc, /* Number of arguments to the function */
595 sqlite3_value **argv /* Value of each argument */
596){
597 const char *zName = context->pFunc->zName;
598 char *zErr;
danielk19771e536952007-08-16 10:09:01 +0000599 zErr = sqlite3MPrintf(0,
drhb7481e72006-09-16 21:45:14 +0000600 "unable to use function %s in the requested context", zName);
601 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000602 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000603}
604
605/*
drheb2e1762004-05-27 01:53:56 +0000606** Allocate or return the aggregate context for a user function. A new
607** context is allocated on the first call. Subsequent calls return the
608** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000609*/
610void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhb21c8cd2007-08-21 19:33:56 +0000611 Mem *pMem;
drh825c6622005-09-08 19:01:05 +0000612 assert( p && p->pFunc && p->pFunc->xStep );
drhb21c8cd2007-08-21 19:33:56 +0000613 assert( sqlite3_mutex_held(p->s.db->mutex) );
614 pMem = p->pMem;
drha10a34b2005-09-07 22:09:48 +0000615 if( (pMem->flags & MEM_Agg)==0 ){
616 if( nByte==0 ){
danielk19775f096132008-03-28 15:44:09 +0000617 sqlite3VdbeMemReleaseExternal(pMem);
618 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000619 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000620 }else{
danielk19775f096132008-03-28 15:44:09 +0000621 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000622 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000623 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000624 if( pMem->z ){
625 memset(pMem->z, 0, nByte);
626 }
drheb2e1762004-05-27 01:53:56 +0000627 }
628 }
drhabfcea22005-09-06 20:36:48 +0000629 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000630}
631
632/*
danielk1977682f68b2004-06-05 10:22:17 +0000633** Return the auxilary data pointer, if any, for the iArg'th argument to
634** the user-function defined by pCtx.
635*/
636void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000637 VdbeFunc *pVdbeFunc;
638
639 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
640 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000641 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
642 return 0;
643 }
drhf92c7ff2004-06-19 15:40:23 +0000644 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000645}
646
647/*
648** Set the auxilary data pointer and delete function, for the iArg'th
649** argument to the user-function defined by pCtx. Any previous value is
650** deleted by calling the delete function specified when it was set.
651*/
652void sqlite3_set_auxdata(
653 sqlite3_context *pCtx,
654 int iArg,
655 void *pAux,
656 void (*xDelete)(void*)
657){
658 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000659 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000660 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000661
drhb21c8cd2007-08-21 19:33:56 +0000662 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000663 pVdbeFunc = pCtx->pVdbeFunc;
664 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000665 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000666 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000667 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000668 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000669 goto failed;
670 }
drh53f733c2005-09-16 02:38:09 +0000671 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000672 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000673 pVdbeFunc->nAux = iArg+1;
674 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000675 }
676
drhf92c7ff2004-06-19 15:40:23 +0000677 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000678 if( pAuxData->pAux && pAuxData->xDelete ){
679 pAuxData->xDelete(pAuxData->pAux);
680 }
681 pAuxData->pAux = pAux;
682 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000683 return;
684
685failed:
686 if( xDelete ){
687 xDelete(pAux);
688 }
danielk1977682f68b2004-06-05 10:22:17 +0000689}
690
691/*
drheb2e1762004-05-27 01:53:56 +0000692** Return the number of times the Step function of a aggregate has been
693** called.
694**
drhcf85a512006-02-09 18:35:29 +0000695** This function is deprecated. Do not use it for new code. It is
696** provide only to avoid breaking legacy code. New aggregate function
697** implementations should keep their own counts within their aggregate
698** context.
drheb2e1762004-05-27 01:53:56 +0000699*/
700int sqlite3_aggregate_count(sqlite3_context *p){
701 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000702 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000703}
704
705/*
drh4f26d6c2004-05-26 23:25:30 +0000706** Return the number of columns in the result set for the statement pStmt.
707*/
708int sqlite3_column_count(sqlite3_stmt *pStmt){
709 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000710 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000711}
712
713/*
714** Return the number of values available from the current row of the
715** currently executing statement pStmt.
716*/
717int sqlite3_data_count(sqlite3_stmt *pStmt){
718 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000719 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000720 return pVm->nResColumn;
721}
722
723
724/*
725** Check to see if column iCol of the given statement is valid. If
726** it is, return a pointer to the Mem for the value of that column.
727** If iCol is not valid, return a pointer to a Mem which has a value
728** of NULL.
729*/
730static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000731 Vdbe *pVm;
732 int vals;
733 Mem *pOut;
734
735 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000736 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000737 sqlite3_mutex_enter(pVm->db->mutex);
738 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000739 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000740 }else{
drhb21c8cd2007-08-21 19:33:56 +0000741 static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
drh27641702007-08-22 02:56:42 +0000742 if( pVm->db ){
743 sqlite3_mutex_enter(pVm->db->mutex);
744 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
745 }
drh32bc3f62007-08-21 20:25:39 +0000746 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000747 }
drh32bc3f62007-08-21 20:25:39 +0000748 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000749}
750
danielk19772e588c72005-12-09 14:25:08 +0000751/*
752** This function is called after invoking an sqlite3_value_XXX function on a
753** column value (i.e. a value returned by evaluating an SQL expression in the
754** select list of a SELECT statement) that may cause a malloc() failure. If
755** malloc() has failed, the threads mallocFailed flag is cleared and the result
756** code of statement pStmt set to SQLITE_NOMEM.
757**
drh32bc3f62007-08-21 20:25:39 +0000758** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000759**
760** sqlite3_column_int()
761** sqlite3_column_int64()
762** sqlite3_column_text()
763** sqlite3_column_text16()
764** sqlite3_column_real()
765** sqlite3_column_bytes()
766** sqlite3_column_bytes16()
767**
768** But not for sqlite3_column_blob(), which never calls malloc().
769*/
770static void columnMallocFailure(sqlite3_stmt *pStmt)
771{
772 /* If malloc() failed during an encoding conversion within an
773 ** sqlite3_column_XXX API, then set the return code of the statement to
774 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
775 ** and _finalize() will return NOMEM.
776 */
danielk197754f01982006-01-18 15:25:17 +0000777 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000778 if( p ){
779 p->rc = sqlite3ApiExit(p->db, p->rc);
780 sqlite3_mutex_leave(p->db->mutex);
781 }
danielk19772e588c72005-12-09 14:25:08 +0000782}
783
drh4f26d6c2004-05-26 23:25:30 +0000784/**************************** sqlite3_column_ *******************************
785** The following routines are used to access elements of the current row
786** in the result set.
787*/
danielk1977c572ef72004-05-27 09:28:41 +0000788const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000789 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000790 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000791 /* Even though there is no encoding conversion, value_blob() might
792 ** need to call malloc() to expand the result of a zeroblob()
793 ** expression.
794 */
795 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000796 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000797}
drh4f26d6c2004-05-26 23:25:30 +0000798int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000799 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
800 columnMallocFailure(pStmt);
801 return val;
drh4f26d6c2004-05-26 23:25:30 +0000802}
803int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000804 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
805 columnMallocFailure(pStmt);
806 return val;
drh4f26d6c2004-05-26 23:25:30 +0000807}
808double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000809 double val = sqlite3_value_double( columnMem(pStmt,i) );
810 columnMallocFailure(pStmt);
811 return val;
drh4f26d6c2004-05-26 23:25:30 +0000812}
813int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000814 int val = sqlite3_value_int( columnMem(pStmt,i) );
815 columnMallocFailure(pStmt);
816 return val;
drh4f26d6c2004-05-26 23:25:30 +0000817}
drhefad9992004-06-22 12:13:55 +0000818sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000819 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
820 columnMallocFailure(pStmt);
821 return val;
drh4f26d6c2004-05-26 23:25:30 +0000822}
823const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000824 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
825 columnMallocFailure(pStmt);
826 return val;
drh4f26d6c2004-05-26 23:25:30 +0000827}
drhd1e47332005-06-26 17:55:33 +0000828sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000829 sqlite3_value *pOut = columnMem(pStmt, i);
830 columnMallocFailure(pStmt);
831 return pOut;
drhd1e47332005-06-26 17:55:33 +0000832}
drh6c626082004-11-14 21:56:29 +0000833#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000834const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000835 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
836 columnMallocFailure(pStmt);
837 return val;
drh4f26d6c2004-05-26 23:25:30 +0000838}
drh6c626082004-11-14 21:56:29 +0000839#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000840int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000841 int iType = sqlite3_value_type( columnMem(pStmt,i) );
842 columnMallocFailure(pStmt);
843 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000844}
845
drh29d72102006-02-09 22:13:41 +0000846/* The following function is experimental and subject to change or
847** removal */
848/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
849** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
850**}
851*/
852
drh5e2517e2004-09-24 23:59:12 +0000853/*
854** Convert the N-th element of pStmt->pColName[] into a string using
855** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000856**
857** There are up to 5 names for each column. useType determines which
858** name is returned. Here are the names:
859**
860** 0 The column name as it should be displayed for output
861** 1 The datatype name for the column
862** 2 The name of the database that the column derives from
863** 3 The name of the table that the column derives from
864** 4 The name of the table column that the result column derives from
865**
866** If the result is not a simple column reference (if it is an expression
867** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000868*/
869static const void *columnName(
870 sqlite3_stmt *pStmt,
871 int N,
872 const void *(*xFunc)(Mem*),
873 int useType
874){
drh32bc3f62007-08-21 20:25:39 +0000875 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000876 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000877 int n;
878
drh5e2517e2004-09-24 23:59:12 +0000879
drh32bc3f62007-08-21 20:25:39 +0000880 if( p!=0 ){
881 n = sqlite3_column_count(pStmt);
882 if( N<n && N>=0 ){
883 N += useType*n;
drh153c62c2007-08-24 03:51:33 +0000884 sqlite3_mutex_enter(p->db->mutex);
drh32bc3f62007-08-21 20:25:39 +0000885 ret = xFunc(&p->aColName[N]);
886
drh32bc3f62007-08-21 20:25:39 +0000887 /* A malloc may have failed inside of the xFunc() call. If this
888 ** is the case, clear the mallocFailed flag and return NULL.
889 */
890 if( p->db && p->db->mallocFailed ){
891 p->db->mallocFailed = 0;
892 ret = 0;
893 }
drh153c62c2007-08-24 03:51:33 +0000894 sqlite3_mutex_leave(p->db->mutex);
drh32bc3f62007-08-21 20:25:39 +0000895 }
drh5e2517e2004-09-24 23:59:12 +0000896 }
danielk197700fd9572005-12-07 06:27:43 +0000897 return ret;
drh5e2517e2004-09-24 23:59:12 +0000898}
899
drh4f26d6c2004-05-26 23:25:30 +0000900/*
901** Return the name of the Nth column of the result set returned by SQL
902** statement pStmt.
903*/
904const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000905 return columnName(
906 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000907}
drhe590fbd2005-05-20 19:36:01 +0000908#ifndef SQLITE_OMIT_UTF16
909const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000910 return columnName(
911 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000912}
913#endif
drh4f26d6c2004-05-26 23:25:30 +0000914
915/*
drh3f913572008-03-22 01:07:17 +0000916** Constraint: If you have ENABLE_COLUMN_METADATA then you must
917** not define OMIT_DECLTYPE.
918*/
919#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
920# error "Must not define both SQLITE_OMIT_DECLTYPE \
921 and SQLITE_ENABLE_COLUMN_METADATA"
922#endif
923
924#ifndef SQLITE_OMIT_DECLTYPE
925/*
drh6c626082004-11-14 21:56:29 +0000926** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000927** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000928*/
929const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000930 return columnName(
931 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000932}
drh6c626082004-11-14 21:56:29 +0000933#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000934const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000935 return columnName(
936 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000937}
drh6c626082004-11-14 21:56:29 +0000938#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000939#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000940
danielk19774b1ae992006-02-10 03:06:10 +0000941#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000942/*
943** Return the name of the database from which a result column derives.
944** NULL is returned if the result column is an expression or constant or
945** anything else which is not an unabiguous reference to a database column.
946*/
947const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000948 return columnName(
949 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000950}
951#ifndef SQLITE_OMIT_UTF16
952const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000953 return columnName(
954 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000955}
956#endif /* SQLITE_OMIT_UTF16 */
957
958/*
959** Return the name of the table from which a result column derives.
960** NULL is returned if the result column is an expression or constant or
961** anything else which is not an unabiguous reference to a database column.
962*/
963const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000964 return columnName(
965 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000966}
967#ifndef SQLITE_OMIT_UTF16
968const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000969 return columnName(
970 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000971}
972#endif /* SQLITE_OMIT_UTF16 */
973
974/*
975** Return the name of the table column from which a result column derives.
976** NULL is returned if the result column is an expression or constant or
977** anything else which is not an unabiguous reference to a database column.
978*/
979const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000980 return columnName(
981 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000982}
983#ifndef SQLITE_OMIT_UTF16
984const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000985 return columnName(
986 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000987}
988#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000989#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000990
991
drh4f26d6c2004-05-26 23:25:30 +0000992/******************************* sqlite3_bind_ ***************************
993**
994** Routines used to attach values to wildcards in a compiled SQL statement.
995*/
996/*
997** Unbind the value bound to variable i in virtual machine p. This is the
998** the same as binding a NULL value to the column. If the "i" parameter is
999** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1000**
1001** The error code stored in database p->db is overwritten with the return
1002** value in any case.
1003*/
1004static int vdbeUnbind(Vdbe *p, int i){
1005 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +00001006 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +00001007 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +00001008 return SQLITE_MISUSE;
1009 }
1010 if( i<1 || i>p->nVar ){
1011 sqlite3Error(p->db, SQLITE_RANGE, 0);
1012 return SQLITE_RANGE;
1013 }
1014 i--;
drh290c1942004-08-21 17:54:45 +00001015 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +00001016 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +00001017 pVar->flags = MEM_Null;
1018 sqlite3Error(p->db, SQLITE_OK, 0);
1019 return SQLITE_OK;
1020}
1021
1022/*
drh5e2517e2004-09-24 23:59:12 +00001023** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +00001024*/
drh5e2517e2004-09-24 23:59:12 +00001025static int bindText(
drh605264d2007-08-21 15:13:19 +00001026 sqlite3_stmt *pStmt, /* The statement to bind against */
1027 int i, /* Index of the parameter to bind */
1028 const void *zData, /* Pointer to the data to be bound */
1029 int nData, /* Number of bytes of data to be bound */
1030 void (*xDel)(void*), /* Destructor for the data */
1031 int encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +00001032){
1033 Vdbe *p = (Vdbe *)pStmt;
1034 Mem *pVar;
1035 int rc;
1036
drh605264d2007-08-21 15:13:19 +00001037 if( p==0 ){
1038 return SQLITE_MISUSE;
1039 }
1040 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001041 rc = vdbeUnbind(p, i);
drh27641702007-08-22 02:56:42 +00001042 if( rc==SQLITE_OK && zData!=0 ){
1043 pVar = &p->aVar[i-1];
1044 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1045 if( rc==SQLITE_OK && encoding!=0 ){
1046 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1047 }
1048 sqlite3Error(p->db, rc, 0);
1049 rc = sqlite3ApiExit(p->db, rc);
drh4f26d6c2004-05-26 23:25:30 +00001050 }
drh605264d2007-08-21 15:13:19 +00001051 sqlite3_mutex_leave(p->db->mutex);
1052 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001053}
drh5e2517e2004-09-24 23:59:12 +00001054
1055
1056/*
1057** Bind a blob value to an SQL statement variable.
1058*/
1059int sqlite3_bind_blob(
1060 sqlite3_stmt *pStmt,
1061 int i,
1062 const void *zData,
1063 int nData,
1064 void (*xDel)(void*)
1065){
1066 return bindText(pStmt, i, zData, nData, xDel, 0);
1067}
drh4f26d6c2004-05-26 23:25:30 +00001068int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1069 int rc;
1070 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001071 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001072 rc = vdbeUnbind(p, i);
1073 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001074 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +00001075 }
drh605264d2007-08-21 15:13:19 +00001076 sqlite3_mutex_leave(p->db->mutex);
danielk1977f4618892004-06-28 13:09:11 +00001077 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001078}
1079int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +00001080 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +00001081}
drhefad9992004-06-22 12:13:55 +00001082int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +00001083 int rc;
1084 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001085 sqlite3_mutex_enter(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001086 rc = vdbeUnbind(p, i);
1087 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +00001088 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +00001089 }
drh605264d2007-08-21 15:13:19 +00001090 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +00001091 return rc;
1092}
drh605264d2007-08-21 15:13:19 +00001093int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1094 int rc;
1095 Vdbe *p = (Vdbe*)pStmt;
1096 sqlite3_mutex_enter(p->db->mutex);
1097 rc = vdbeUnbind(p, i);
1098 sqlite3_mutex_leave(p->db->mutex);
1099 return rc;
drh4f26d6c2004-05-26 23:25:30 +00001100}
1101int sqlite3_bind_text(
1102 sqlite3_stmt *pStmt,
1103 int i,
1104 const char *zData,
1105 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001106 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001107){
drh5e2517e2004-09-24 23:59:12 +00001108 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001109}
drh6c626082004-11-14 21:56:29 +00001110#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001111int sqlite3_bind_text16(
1112 sqlite3_stmt *pStmt,
1113 int i,
1114 const void *zData,
1115 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001116 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001117){
drh5e2517e2004-09-24 23:59:12 +00001118 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001119}
drh6c626082004-11-14 21:56:29 +00001120#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001121int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1122 int rc;
1123 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001124 sqlite3_mutex_enter(p->db->mutex);
danielk197726e41442006-06-14 15:16:35 +00001125 rc = vdbeUnbind(p, i);
1126 if( rc==SQLITE_OK ){
drhb21c8cd2007-08-21 19:33:56 +00001127 rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
danielk197726e41442006-06-14 15:16:35 +00001128 }
danielk1977a7a8e142008-02-13 18:25:27 +00001129 rc = sqlite3ApiExit(p->db, rc);
drh605264d2007-08-21 15:13:19 +00001130 sqlite3_mutex_leave(p->db->mutex);
danielk197726e41442006-06-14 15:16:35 +00001131 return rc;
1132}
drhb026e052007-05-02 01:34:31 +00001133int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1134 int rc;
1135 Vdbe *p = (Vdbe *)pStmt;
drh605264d2007-08-21 15:13:19 +00001136 sqlite3_mutex_enter(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001137 rc = vdbeUnbind(p, i);
1138 if( rc==SQLITE_OK ){
1139 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1140 }
drh605264d2007-08-21 15:13:19 +00001141 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001142 return rc;
1143}
drh75f6a032004-07-15 14:15:00 +00001144
1145/*
1146** Return the number of wildcards that can be potentially bound to.
1147** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001148*/
1149int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001150 Vdbe *p = (Vdbe*)pStmt;
1151 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001152}
drh895d7472004-08-20 16:02:39 +00001153
1154/*
drhfa6bc002004-09-07 16:19:52 +00001155** Create a mapping from variable numbers to variable names
1156** in the Vdbe.azVar[] array, if such a mapping does not already
1157** exist.
drh895d7472004-08-20 16:02:39 +00001158*/
drhfa6bc002004-09-07 16:19:52 +00001159static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001160 if( !p->okVar ){
drh605264d2007-08-21 15:13:19 +00001161 sqlite3_mutex_enter(p->db->mutex);
1162 if( !p->okVar ){
1163 int j;
1164 Op *pOp;
1165 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1166 if( pOp->opcode==OP_Variable ){
1167 assert( pOp->p1>0 && pOp->p1<=p->nVar );
danielk19772dca4ac2008-01-03 11:50:29 +00001168 p->azVar[pOp->p1-1] = pOp->p4.z;
drh605264d2007-08-21 15:13:19 +00001169 }
drh895d7472004-08-20 16:02:39 +00001170 }
drh605264d2007-08-21 15:13:19 +00001171 p->okVar = 1;
drh895d7472004-08-20 16:02:39 +00001172 }
drh605264d2007-08-21 15:13:19 +00001173 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001174 }
drhfa6bc002004-09-07 16:19:52 +00001175}
1176
1177/*
1178** Return the name of a wildcard parameter. Return NULL if the index
1179** is out of range or if the wildcard is unnamed.
1180**
1181** The result is always UTF-8.
1182*/
1183const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1184 Vdbe *p = (Vdbe*)pStmt;
1185 if( p==0 || i<1 || i>p->nVar ){
1186 return 0;
1187 }
1188 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001189 return p->azVar[i-1];
1190}
drhfa6bc002004-09-07 16:19:52 +00001191
1192/*
1193** Given a wildcard parameter name, return the index of the variable
1194** with that name. If there is no variable with the given name,
1195** return 0.
1196*/
1197int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1198 Vdbe *p = (Vdbe*)pStmt;
1199 int i;
1200 if( p==0 ){
1201 return 0;
1202 }
1203 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +00001204 if( zName ){
1205 for(i=0; i<p->nVar; i++){
1206 const char *z = p->azVar[i];
1207 if( z && strcmp(z,zName)==0 ){
1208 return i+1;
1209 }
drhfa6bc002004-09-07 16:19:52 +00001210 }
1211 }
1212 return 0;
1213}
drhf8db1bc2005-04-22 02:38:37 +00001214
drh51942bc2005-06-12 22:01:42 +00001215/*
drhf8db1bc2005-04-22 02:38:37 +00001216** Transfer all bindings from the first statement over to the second.
1217** If the two statements contain a different number of bindings, then
1218** an SQLITE_ERROR is returned.
1219*/
1220int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1221 Vdbe *pFrom = (Vdbe*)pFromStmt;
1222 Vdbe *pTo = (Vdbe*)pToStmt;
1223 int i, rc = SQLITE_OK;
1224 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
drh27641702007-08-22 02:56:42 +00001225 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1226 || pTo->db!=pFrom->db ){
drhf8db1bc2005-04-22 02:38:37 +00001227 return SQLITE_MISUSE;
1228 }
1229 if( pFrom->nVar!=pTo->nVar ){
1230 return SQLITE_ERROR;
1231 }
drh27641702007-08-22 02:56:42 +00001232 sqlite3_mutex_enter(pTo->db->mutex);
drhf8db1bc2005-04-22 02:38:37 +00001233 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001234 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001235 }
drh27641702007-08-22 02:56:42 +00001236 sqlite3_mutex_leave(pTo->db->mutex);
drh4ac285a2006-09-15 07:28:50 +00001237 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
drhf8db1bc2005-04-22 02:38:37 +00001238 return rc;
1239}
drh51942bc2005-06-12 22:01:42 +00001240
1241/*
1242** Return the sqlite3* database handle to which the prepared statement given
1243** in the argument belongs. This is the same database handle that was
1244** the first argument to the sqlite3_prepare() that was used to create
1245** the statement in the first place.
1246*/
1247sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1248 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1249}