blob: caaebd8160d223b658e5268e542ca81c2379816b [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
2** 2004 May 26
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains code use to implement APIs that are part of the
14** VDBE.
danielk1977822a5162008-05-16 04:51:54 +000015**
drh4c8555f2009-06-25 01:47:11 +000016** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
drh4f26d6c2004-05-26 23:25:30 +000017*/
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
shaneeec556d2008-10-12 00:27:53 +000021#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +000022/*
23** Return TRUE (non-zero) of the statement supplied as an argument needs
24** to be recompiled. A statement needs to be recompiled whenever the
25** execution environment changes in a way that would alter the program
26** that sqlite3_prepare() generates. For example, if new functions or
27** collating sequences are registered or if an authorizer function is
28** added or changed.
drhd89bd002005-01-22 03:03:54 +000029*/
30int sqlite3_expired(sqlite3_stmt *pStmt){
31 Vdbe *p = (Vdbe*)pStmt;
32 return p==0 || p->expired;
33}
shaneeec556d2008-10-12 00:27:53 +000034#endif
drhd89bd002005-01-22 03:03:54 +000035
drhe30f4422007-08-21 16:15:55 +000036/*
37** The following routine destroys a virtual machine that is created by
38** the sqlite3_compile() routine. The integer returned is an SQLITE_
39** success/failure code that describes the result of executing the virtual
40** machine.
41**
42** This routine sets the error code and string returned by
43** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
44*/
45int sqlite3_finalize(sqlite3_stmt *pStmt){
46 int rc;
47 if( pStmt==0 ){
48 rc = SQLITE_OK;
49 }else{
50 Vdbe *v = (Vdbe*)pStmt;
danielk1977238746a2009-03-19 18:51:06 +000051 sqlite3 *db = v->db;
drh18472fa2008-10-07 15:25:48 +000052#if SQLITE_THREADSAFE
drh86f8c192007-08-22 00:39:19 +000053 sqlite3_mutex *mutex = v->db->mutex;
drh7e8b8482008-01-23 03:03:05 +000054#endif
drh86f8c192007-08-22 00:39:19 +000055 sqlite3_mutex_enter(mutex);
drhe30f4422007-08-21 16:15:55 +000056 rc = sqlite3VdbeFinalize(v);
danielk1977238746a2009-03-19 18:51:06 +000057 rc = sqlite3ApiExit(db, rc);
drh86f8c192007-08-22 00:39:19 +000058 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +000059 }
60 return rc;
61}
62
63/*
64** Terminate the current execution of an SQL statement and reset it
65** back to its starting state so that it can be reused. A success code from
66** the prior execution is returned.
67**
68** This routine sets the error code and string returned by
69** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
70*/
71int sqlite3_reset(sqlite3_stmt *pStmt){
72 int rc;
73 if( pStmt==0 ){
74 rc = SQLITE_OK;
75 }else{
76 Vdbe *v = (Vdbe*)pStmt;
77 sqlite3_mutex_enter(v->db->mutex);
drhc890fec2008-08-01 20:10:08 +000078 rc = sqlite3VdbeReset(v);
drhe30f4422007-08-21 16:15:55 +000079 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
80 assert( (rc & (v->db->errMask))==rc );
danielk1977238746a2009-03-19 18:51:06 +000081 rc = sqlite3ApiExit(v->db, rc);
drhe30f4422007-08-21 16:15:55 +000082 sqlite3_mutex_leave(v->db->mutex);
83 }
84 return rc;
85}
86
87/*
88** Set all the parameters in the compiled SQL statement to NULL.
89*/
90int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
91 int i;
92 int rc = SQLITE_OK;
drh7297d1f2008-05-09 14:39:44 +000093 Vdbe *p = (Vdbe*)pStmt;
drh18472fa2008-10-07 15:25:48 +000094#if SQLITE_THREADSAFE
drh7e8b8482008-01-23 03:03:05 +000095 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
96#endif
97 sqlite3_mutex_enter(mutex);
drh7297d1f2008-05-09 14:39:44 +000098 for(i=0; i<p->nVar; i++){
99 sqlite3VdbeMemRelease(&p->aVar[i]);
100 p->aVar[i].flags = MEM_Null;
drhe30f4422007-08-21 16:15:55 +0000101 }
drh7e8b8482008-01-23 03:03:05 +0000102 sqlite3_mutex_leave(mutex);
drhe30f4422007-08-21 16:15:55 +0000103 return rc;
104}
105
106
drh4f26d6c2004-05-26 23:25:30 +0000107/**************************** sqlite3_value_ *******************************
108** The following routines extract information from a Mem or sqlite3_value
109** structure.
110*/
111const void *sqlite3_value_blob(sqlite3_value *pVal){
112 Mem *p = (Mem*)pVal;
113 if( p->flags & (MEM_Blob|MEM_Str) ){
drhb21c8cd2007-08-21 19:33:56 +0000114 sqlite3VdbeMemExpandBlob(p);
drh1f0feef2007-05-15 13:27:07 +0000115 p->flags &= ~MEM_Str;
116 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +0000117 return p->z;
118 }else{
119 return sqlite3_value_text(pVal);
120 }
121}
122int sqlite3_value_bytes(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000123 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000124}
125int sqlite3_value_bytes16(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000126 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000127}
128double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000129 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000130}
131int sqlite3_value_int(sqlite3_value *pVal){
drhb27b7f52008-12-10 18:03:45 +0000132 return (int)sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000133}
drhefad9992004-06-22 12:13:55 +0000134sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +0000135 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +0000136}
137const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000138 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000139}
drh6c626082004-11-14 21:56:29 +0000140#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000141const void *sqlite3_value_text16(sqlite3_value* pVal){
drhb21c8cd2007-08-21 19:33:56 +0000142 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000143}
danielk1977d8123362004-06-12 09:25:12 +0000144const void *sqlite3_value_text16be(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000145 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +0000146}
147const void *sqlite3_value_text16le(sqlite3_value *pVal){
drhb21c8cd2007-08-21 19:33:56 +0000148 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +0000149}
drh6c626082004-11-14 21:56:29 +0000150#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000151int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +0000152 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +0000153}
154
155/**************************** sqlite3_result_ *******************************
156** The following routines are used by user-defined functions to specify
157** the function result.
drh4c8555f2009-06-25 01:47:11 +0000158**
159** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
160** result as a string or blob but if the string or blob is too large, it
161** then sets the error code to SQLITE_TOOBIG
drh4f26d6c2004-05-26 23:25:30 +0000162*/
drh4c8555f2009-06-25 01:47:11 +0000163static void setResultStrOrError(
164 sqlite3_context *pCtx, /* Function context */
165 const char *z, /* String pointer */
166 int n, /* Bytes in string, or negative */
167 u8 enc, /* Encoding of z. 0 for BLOBs */
168 void (*xDel)(void*) /* Destructor function */
169){
170 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
171 sqlite3_result_error_toobig(pCtx);
172 }
173}
drh4f26d6c2004-05-26 23:25:30 +0000174void sqlite3_result_blob(
175 sqlite3_context *pCtx,
176 const void *z,
177 int n,
danielk1977d8123362004-06-12 09:25:12 +0000178 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000179){
drh5708d2d2005-06-22 10:53:59 +0000180 assert( n>=0 );
drh32bc3f62007-08-21 20:25:39 +0000181 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000182 setResultStrOrError(pCtx, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000183}
184void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
drh32bc3f62007-08-21 20:25:39 +0000185 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000186 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
187}
188void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000189 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000190 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000191 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000192}
danielk1977a1686c92006-01-23 07:52:37 +0000193#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000194void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
drh32bc3f62007-08-21 20:25:39 +0000195 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh69544ec2008-02-06 14:11:34 +0000196 pCtx->isError = SQLITE_ERROR;
drhb21c8cd2007-08-21 19:33:56 +0000197 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000198}
danielk1977a1686c92006-01-23 07:52:37 +0000199#endif
drhf4479502004-05-27 03:12:53 +0000200void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh32bc3f62007-08-21 20:25:39 +0000201 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000202 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
203}
204void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
drh32bc3f62007-08-21 20:25:39 +0000205 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4f26d6c2004-05-26 23:25:30 +0000206 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
207}
208void sqlite3_result_null(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000209 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf4479502004-05-27 03:12:53 +0000210 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000211}
212void sqlite3_result_text(
213 sqlite3_context *pCtx,
214 const char *z,
215 int n,
danielk1977d8123362004-06-12 09:25:12 +0000216 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000217){
drh32bc3f62007-08-21 20:25:39 +0000218 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000219 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000220}
drh6c626082004-11-14 21:56:29 +0000221#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000222void sqlite3_result_text16(
223 sqlite3_context *pCtx,
224 const void *z,
225 int n,
danielk1977d8123362004-06-12 09:25:12 +0000226 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000227){
drh32bc3f62007-08-21 20:25:39 +0000228 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000229 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000230}
231void sqlite3_result_text16be(
232 sqlite3_context *pCtx,
233 const void *z,
234 int n,
235 void (*xDel)(void *)
236){
drh32bc3f62007-08-21 20:25:39 +0000237 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000238 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000239}
240void sqlite3_result_text16le(
241 sqlite3_context *pCtx,
242 const void *z,
243 int n,
244 void (*xDel)(void *)
245){
drh32bc3f62007-08-21 20:25:39 +0000246 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drh4c8555f2009-06-25 01:47:11 +0000247 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000248}
drh6c626082004-11-14 21:56:29 +0000249#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000250void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
drh32bc3f62007-08-21 20:25:39 +0000251 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb21c8cd2007-08-21 19:33:56 +0000252 sqlite3VdbeMemCopy(&pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000253}
drhb026e052007-05-02 01:34:31 +0000254void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
drh32bc3f62007-08-21 20:25:39 +0000255 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhb026e052007-05-02 01:34:31 +0000256 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
257}
drh69544ec2008-02-06 14:11:34 +0000258void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
259 pCtx->isError = errCode;
drh51c7d862009-04-27 18:46:06 +0000260 if( pCtx->s.flags & MEM_Null ){
261 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
262 SQLITE_UTF8, SQLITE_STATIC);
263 }
drh69544ec2008-02-06 14:11:34 +0000264}
drh4f26d6c2004-05-26 23:25:30 +0000265
drha0206bc2007-05-08 15:15:02 +0000266/* Force an SQLITE_TOOBIG error. */
267void sqlite3_result_error_toobig(sqlite3_context *pCtx){
drh32bc3f62007-08-21 20:25:39 +0000268 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhbb4957f2008-03-20 14:03:29 +0000269 pCtx->isError = SQLITE_TOOBIG;
270 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
271 SQLITE_UTF8, SQLITE_STATIC);
drha0206bc2007-05-08 15:15:02 +0000272}
273
danielk1977a1644fd2007-08-29 12:31:25 +0000274/* An SQLITE_NOMEM error. */
275void sqlite3_result_error_nomem(sqlite3_context *pCtx){
276 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
277 sqlite3VdbeMemSetNull(&pCtx->s);
drh69544ec2008-02-06 14:11:34 +0000278 pCtx->isError = SQLITE_NOMEM;
danielk1977a1644fd2007-08-29 12:31:25 +0000279 pCtx->s.db->mallocFailed = 1;
280}
drh4f26d6c2004-05-26 23:25:30 +0000281
282/*
283** Execute the statement pStmt, either until a row of data is ready, the
284** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000285**
286** This routine implements the bulk of the logic behind the sqlite_step()
287** API. The only thing omitted is the automatic recompile if a
288** schema change has occurred. That detail is handled by the
289** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000290*/
drhb900aaf2006-11-09 00:24:53 +0000291static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000292 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000293 int rc;
294
danielk1977a185ddb2007-11-15 16:04:15 +0000295 assert(p);
296 if( p->magic!=VDBE_MAGIC_RUN ){
drhf1bfe9a2007-10-17 01:44:20 +0000297 return SQLITE_MISUSE;
298 }
299
danielk1977efaaf572006-01-16 11:29:19 +0000300 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000301 db = p->db;
drhc456e572008-08-11 18:44:58 +0000302 if( db->mallocFailed ){
303 return SQLITE_NOMEM;
304 }
danielk1977261919c2005-12-06 12:52:59 +0000305
danielk1977a21c6b62005-01-24 10:25:59 +0000306 if( p->pc<=0 && p->expired ){
drh7950acd2009-04-10 23:11:31 +0000307 if( ALWAYS(p->rc==SQLITE_OK) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000308 p->rc = SQLITE_SCHEMA;
309 }
drhb900aaf2006-11-09 00:24:53 +0000310 rc = SQLITE_ERROR;
311 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000312 }
drh4f26d6c2004-05-26 23:25:30 +0000313 if( sqlite3SafetyOn(db) ){
314 p->rc = SQLITE_MISUSE;
315 return SQLITE_MISUSE;
316 }
danielk19771d850a72004-05-31 08:26:49 +0000317 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000318 /* If there are no other statements currently running, then
319 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
320 ** from interrupting a statement that has not yet started.
321 */
322 if( db->activeVdbeCnt==0 ){
323 db->u1.isInterrupted = 0;
324 }
325
drh19e2d372005-08-29 23:00:03 +0000326#ifndef SQLITE_OMIT_TRACE
drh19e2d372005-08-29 23:00:03 +0000327 if( db->xProfile && !db->init.busy ){
328 double rNow;
danielk1977b4b47412007-08-17 15:53:36 +0000329 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000330 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
drh19e2d372005-08-29 23:00:03 +0000331 }
332#endif
drhc16a03b2004-09-15 13:38:10 +0000333
danielk19771d850a72004-05-31 08:26:49 +0000334 db->activeVdbeCnt++;
drhad4a4b82008-11-05 16:37:34 +0000335 if( p->readOnly==0 ) db->writeVdbeCnt++;
danielk19771d850a72004-05-31 08:26:49 +0000336 p->pc = 0;
337 }
drhb7f91642004-10-31 02:22:47 +0000338#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000339 if( p->explain ){
340 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000341 }else
342#endif /* SQLITE_OMIT_EXPLAIN */
343 {
drh4f26d6c2004-05-26 23:25:30 +0000344 rc = sqlite3VdbeExec(p);
345 }
346
347 if( sqlite3SafetyOff(db) ){
348 rc = SQLITE_MISUSE;
349 }
350
drh19e2d372005-08-29 23:00:03 +0000351#ifndef SQLITE_OMIT_TRACE
352 /* Invoke the profile callback if there is one
353 */
danielk19776ab3a2e2009-02-19 14:39:25 +0000354 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
drh19e2d372005-08-29 23:00:03 +0000355 double rNow;
356 u64 elapseTime;
357
danielk1977b4b47412007-08-17 15:53:36 +0000358 sqlite3OsCurrentTime(db->pVfs, &rNow);
drhb27b7f52008-12-10 18:03:45 +0000359 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
360 elapseTime -= p->startTime;
danielk19776ab3a2e2009-02-19 14:39:25 +0000361 db->xProfile(db->pProfileArg, p->zSql, elapseTime);
drh19e2d372005-08-29 23:00:03 +0000362 }
363#endif
364
drh80cc85b2008-07-23 21:07:25 +0000365 db->errCode = rc;
danielk1977357864e2009-03-25 15:43:08 +0000366 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
367 p->rc = SQLITE_NOMEM;
drhb900aaf2006-11-09 00:24:53 +0000368 }
danielk1977357864e2009-03-25 15:43:08 +0000369end_of_step:
370 /* At this point local variable rc holds the value that should be
371 ** returned if this statement was compiled using the legacy
372 ** sqlite3_prepare() interface. According to the docs, this can only
373 ** be one of the values in the first assert() below. Variable p->rc
374 ** contains the value that would be returned if sqlite3_finalize()
375 ** were called on statement p.
376 */
377 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
378 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
379 );
380 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
381 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
382 /* If this statement was prepared using sqlite3_prepare_v2(), and an
383 ** error has occured, then return the error code in p->rc to the
384 ** caller. Set the error code in the database handle to the same value.
385 */
386 rc = db->errCode = p->rc;
387 }
388 return (rc&db->errMask);
drhb900aaf2006-11-09 00:24:53 +0000389}
390
391/*
392** This is the top-level implementation of sqlite3_step(). Call
393** sqlite3Step() to do most of the work. If a schema error occurs,
394** call sqlite3Reprepare() and try again.
395*/
drhb900aaf2006-11-09 00:24:53 +0000396int sqlite3_step(sqlite3_stmt *pStmt){
danielk1977a185ddb2007-11-15 16:04:15 +0000397 int rc = SQLITE_MISUSE;
398 if( pStmt ){
399 int cnt = 0;
400 Vdbe *v = (Vdbe*)pStmt;
401 sqlite3 *db = v->db;
402 sqlite3_mutex_enter(db->mutex);
403 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
404 && cnt++ < 5
drh8bfdf722009-06-19 14:06:03 +0000405 && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
danielk1977a185ddb2007-11-15 16:04:15 +0000406 sqlite3_reset(pStmt);
407 v->expired = 0;
danielk19778e556522007-11-13 10:30:24 +0000408 }
drh7950acd2009-04-10 23:11:31 +0000409 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
danielk1977a185ddb2007-11-15 16:04:15 +0000410 /* This case occurs after failing to recompile an sql statement.
411 ** The error message from the SQL compiler has already been loaded
412 ** into the database handle. This block copies the error message
413 ** from the database handle into the statement and sets the statement
414 ** program counter to 0 to ensure that when the statement is
415 ** finalized or reset the parser error message is available via
416 ** sqlite3_errmsg() and sqlite3_errcode().
417 */
418 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
drh633e6d52008-07-28 19:34:53 +0000419 sqlite3DbFree(db, v->zErrMsg);
danielk1977a185ddb2007-11-15 16:04:15 +0000420 if( !db->mallocFailed ){
421 v->zErrMsg = sqlite3DbStrDup(db, zErr);
422 } else {
423 v->zErrMsg = 0;
424 v->rc = SQLITE_NOMEM;
425 }
426 }
427 rc = sqlite3ApiExit(db, rc);
428 sqlite3_mutex_leave(db->mutex);
danielk19778e556522007-11-13 10:30:24 +0000429 }
danielk197776e8d1a2006-01-18 18:22:43 +0000430 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000431}
432
433/*
drheb2e1762004-05-27 01:53:56 +0000434** Extract the user data from a sqlite3_context structure and return a
435** pointer to it.
436*/
437void *sqlite3_user_data(sqlite3_context *p){
438 assert( p && p->pFunc );
439 return p->pFunc->pUserData;
440}
441
442/*
drhfa4a4b92008-03-19 21:45:51 +0000443** Extract the user data from a sqlite3_context structure and return a
444** pointer to it.
445*/
446sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
447 assert( p && p->pFunc );
448 return p->s.db;
449}
450
451/*
drhb7481e72006-09-16 21:45:14 +0000452** The following is the implementation of an SQL function that always
453** fails with an error message stating that the function is used in the
454** wrong context. The sqlite3_overload_function() API might construct
455** SQL function that use this routine so that the functions will exist
456** for name resolution but are actually overloaded by the xFindFunction
457** method of virtual tables.
458*/
459void sqlite3InvalidFunction(
460 sqlite3_context *context, /* The function calling context */
danielk197762c14b32008-11-19 09:05:26 +0000461 int NotUsed, /* Number of arguments to the function */
462 sqlite3_value **NotUsed2 /* Value of each argument */
drhb7481e72006-09-16 21:45:14 +0000463){
464 const char *zName = context->pFunc->zName;
465 char *zErr;
danielk197762c14b32008-11-19 09:05:26 +0000466 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbc6160b2009-04-08 15:45:31 +0000467 zErr = sqlite3_mprintf(
drhb7481e72006-09-16 21:45:14 +0000468 "unable to use function %s in the requested context", zName);
469 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000470 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000471}
472
473/*
drheb2e1762004-05-27 01:53:56 +0000474** Allocate or return the aggregate context for a user function. A new
475** context is allocated on the first call. Subsequent calls return the
476** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000477*/
478void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhb21c8cd2007-08-21 19:33:56 +0000479 Mem *pMem;
drh825c6622005-09-08 19:01:05 +0000480 assert( p && p->pFunc && p->pFunc->xStep );
drhb21c8cd2007-08-21 19:33:56 +0000481 assert( sqlite3_mutex_held(p->s.db->mutex) );
482 pMem = p->pMem;
drha10a34b2005-09-07 22:09:48 +0000483 if( (pMem->flags & MEM_Agg)==0 ){
484 if( nByte==0 ){
danielk19775f096132008-03-28 15:44:09 +0000485 sqlite3VdbeMemReleaseExternal(pMem);
486 pMem->flags = MEM_Null;
drha10a34b2005-09-07 22:09:48 +0000487 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000488 }else{
danielk19775f096132008-03-28 15:44:09 +0000489 sqlite3VdbeMemGrow(pMem, nByte, 0);
drha10a34b2005-09-07 22:09:48 +0000490 pMem->flags = MEM_Agg;
drh3c024d62007-03-30 11:23:45 +0000491 pMem->u.pDef = p->pFunc;
danielk19775f096132008-03-28 15:44:09 +0000492 if( pMem->z ){
493 memset(pMem->z, 0, nByte);
494 }
drheb2e1762004-05-27 01:53:56 +0000495 }
496 }
drhabfcea22005-09-06 20:36:48 +0000497 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000498}
499
500/*
danielk1977682f68b2004-06-05 10:22:17 +0000501** Return the auxilary data pointer, if any, for the iArg'th argument to
502** the user-function defined by pCtx.
503*/
504void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
drhb21c8cd2007-08-21 19:33:56 +0000505 VdbeFunc *pVdbeFunc;
506
507 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
508 pVdbeFunc = pCtx->pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000509 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
510 return 0;
511 }
drhf92c7ff2004-06-19 15:40:23 +0000512 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000513}
514
515/*
516** Set the auxilary data pointer and delete function, for the iArg'th
517** argument to the user-function defined by pCtx. Any previous value is
518** deleted by calling the delete function specified when it was set.
519*/
520void sqlite3_set_auxdata(
521 sqlite3_context *pCtx,
522 int iArg,
523 void *pAux,
524 void (*xDelete)(void*)
525){
526 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000527 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000528 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000529
drhb21c8cd2007-08-21 19:33:56 +0000530 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
drhf92c7ff2004-06-19 15:40:23 +0000531 pVdbeFunc = pCtx->pVdbeFunc;
532 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
danielk197731dad9d2007-08-16 11:36:15 +0000533 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
drhf92c7ff2004-06-19 15:40:23 +0000534 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
danielk197726783a52007-08-29 14:06:22 +0000535 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
drh17435752007-08-16 04:30:38 +0000536 if( !pVdbeFunc ){
drh17435752007-08-16 04:30:38 +0000537 goto failed;
538 }
drh53f733c2005-09-16 02:38:09 +0000539 pCtx->pVdbeFunc = pVdbeFunc;
danielk197731dad9d2007-08-16 11:36:15 +0000540 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
drh998da3a2004-06-19 15:22:56 +0000541 pVdbeFunc->nAux = iArg+1;
542 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000543 }
544
drhf92c7ff2004-06-19 15:40:23 +0000545 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000546 if( pAuxData->pAux && pAuxData->xDelete ){
547 pAuxData->xDelete(pAuxData->pAux);
548 }
549 pAuxData->pAux = pAux;
550 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000551 return;
552
553failed:
554 if( xDelete ){
555 xDelete(pAux);
556 }
danielk1977682f68b2004-06-05 10:22:17 +0000557}
558
shaneeec556d2008-10-12 00:27:53 +0000559#ifndef SQLITE_OMIT_DEPRECATED
danielk1977682f68b2004-06-05 10:22:17 +0000560/*
drheb2e1762004-05-27 01:53:56 +0000561** Return the number of times the Step function of a aggregate has been
562** called.
563**
drhcf85a512006-02-09 18:35:29 +0000564** This function is deprecated. Do not use it for new code. It is
565** provide only to avoid breaking legacy code. New aggregate function
566** implementations should keep their own counts within their aggregate
567** context.
drheb2e1762004-05-27 01:53:56 +0000568*/
569int sqlite3_aggregate_count(sqlite3_context *p){
shanee34c6472009-03-05 04:23:47 +0000570 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000571 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000572}
shaneeec556d2008-10-12 00:27:53 +0000573#endif
drheb2e1762004-05-27 01:53:56 +0000574
575/*
drh4f26d6c2004-05-26 23:25:30 +0000576** Return the number of columns in the result set for the statement pStmt.
577*/
578int sqlite3_column_count(sqlite3_stmt *pStmt){
579 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000580 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000581}
582
583/*
584** Return the number of values available from the current row of the
585** currently executing statement pStmt.
586*/
587int sqlite3_data_count(sqlite3_stmt *pStmt){
588 Vdbe *pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000589 if( pVm==0 || pVm->pResultSet==0 ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000590 return pVm->nResColumn;
591}
592
593
594/*
595** Check to see if column iCol of the given statement is valid. If
596** it is, return a pointer to the Mem for the value of that column.
597** If iCol is not valid, return a pointer to a Mem which has a value
598** of NULL.
599*/
600static Mem *columnMem(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000601 Vdbe *pVm;
602 int vals;
603 Mem *pOut;
604
605 pVm = (Vdbe *)pStmt;
drhd4e70eb2008-01-02 00:34:36 +0000606 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
drh32bc3f62007-08-21 20:25:39 +0000607 sqlite3_mutex_enter(pVm->db->mutex);
608 vals = sqlite3_data_count(pStmt);
drhd4e70eb2008-01-02 00:34:36 +0000609 pOut = &pVm->pResultSet[i];
drh32bc3f62007-08-21 20:25:39 +0000610 }else{
danielk197741f5b042009-06-06 14:13:26 +0000611 /* If the value passed as the second argument is out of range, return
612 ** a pointer to the following static Mem object which contains the
613 ** value SQL NULL. Even though the Mem structure contains an element
614 ** of type i64, on certain architecture (x86) with certain compiler
615 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
616 ** instead of an 8-byte one. This all works fine, except that when
617 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
618 ** that a Mem structure is located on an 8-byte boundary. To prevent
619 ** this assert() from failing, when building with SQLITE_DEBUG defined
620 ** using gcc, force nullMem to be 8-byte aligned using the magical
621 ** __attribute__((aligned(8))) macro. */
622 static const Mem nullMem
623#if defined(SQLITE_DEBUG) && defined(__GNUC__)
624 __attribute__((aligned(8)))
625#endif
626 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
627
drh9b4ea4a2009-04-10 20:32:00 +0000628 if( pVm && ALWAYS(pVm->db) ){
drh27641702007-08-22 02:56:42 +0000629 sqlite3_mutex_enter(pVm->db->mutex);
630 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
631 }
drh32bc3f62007-08-21 20:25:39 +0000632 pOut = (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000633 }
drh32bc3f62007-08-21 20:25:39 +0000634 return pOut;
drh4f26d6c2004-05-26 23:25:30 +0000635}
636
danielk19772e588c72005-12-09 14:25:08 +0000637/*
638** This function is called after invoking an sqlite3_value_XXX function on a
639** column value (i.e. a value returned by evaluating an SQL expression in the
640** select list of a SELECT statement) that may cause a malloc() failure. If
641** malloc() has failed, the threads mallocFailed flag is cleared and the result
642** code of statement pStmt set to SQLITE_NOMEM.
643**
drh32bc3f62007-08-21 20:25:39 +0000644** Specifically, this is called from within:
danielk19772e588c72005-12-09 14:25:08 +0000645**
646** sqlite3_column_int()
647** sqlite3_column_int64()
648** sqlite3_column_text()
649** sqlite3_column_text16()
650** sqlite3_column_real()
651** sqlite3_column_bytes()
652** sqlite3_column_bytes16()
653**
654** But not for sqlite3_column_blob(), which never calls malloc().
655*/
656static void columnMallocFailure(sqlite3_stmt *pStmt)
657{
658 /* If malloc() failed during an encoding conversion within an
659 ** sqlite3_column_XXX API, then set the return code of the statement to
660 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
661 ** and _finalize() will return NOMEM.
662 */
danielk197754f01982006-01-18 15:25:17 +0000663 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000664 if( p ){
665 p->rc = sqlite3ApiExit(p->db, p->rc);
666 sqlite3_mutex_leave(p->db->mutex);
667 }
danielk19772e588c72005-12-09 14:25:08 +0000668}
669
drh4f26d6c2004-05-26 23:25:30 +0000670/**************************** sqlite3_column_ *******************************
671** The following routines are used to access elements of the current row
672** in the result set.
673*/
danielk1977c572ef72004-05-27 09:28:41 +0000674const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000675 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000676 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000677 /* Even though there is no encoding conversion, value_blob() might
678 ** need to call malloc() to expand the result of a zeroblob()
679 ** expression.
680 */
681 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000682 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000683}
drh4f26d6c2004-05-26 23:25:30 +0000684int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000685 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
686 columnMallocFailure(pStmt);
687 return val;
drh4f26d6c2004-05-26 23:25:30 +0000688}
689int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000690 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
691 columnMallocFailure(pStmt);
692 return val;
drh4f26d6c2004-05-26 23:25:30 +0000693}
694double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000695 double val = sqlite3_value_double( columnMem(pStmt,i) );
696 columnMallocFailure(pStmt);
697 return val;
drh4f26d6c2004-05-26 23:25:30 +0000698}
699int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000700 int val = sqlite3_value_int( columnMem(pStmt,i) );
701 columnMallocFailure(pStmt);
702 return val;
drh4f26d6c2004-05-26 23:25:30 +0000703}
drhefad9992004-06-22 12:13:55 +0000704sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000705 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
706 columnMallocFailure(pStmt);
707 return val;
drh4f26d6c2004-05-26 23:25:30 +0000708}
709const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000710 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
711 columnMallocFailure(pStmt);
712 return val;
drh4f26d6c2004-05-26 23:25:30 +0000713}
drhd1e47332005-06-26 17:55:33 +0000714sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
danielk1977d0ffa1e2008-10-13 10:37:49 +0000715 Mem *pOut = columnMem(pStmt, i);
716 if( pOut->flags&MEM_Static ){
717 pOut->flags &= ~MEM_Static;
718 pOut->flags |= MEM_Ephem;
719 }
drh32bc3f62007-08-21 20:25:39 +0000720 columnMallocFailure(pStmt);
danielk1977d0ffa1e2008-10-13 10:37:49 +0000721 return (sqlite3_value *)pOut;
drhd1e47332005-06-26 17:55:33 +0000722}
drh6c626082004-11-14 21:56:29 +0000723#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000724const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000725 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
726 columnMallocFailure(pStmt);
727 return val;
drh4f26d6c2004-05-26 23:25:30 +0000728}
drh6c626082004-11-14 21:56:29 +0000729#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000730int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
drh32bc3f62007-08-21 20:25:39 +0000731 int iType = sqlite3_value_type( columnMem(pStmt,i) );
732 columnMallocFailure(pStmt);
733 return iType;
drh4f26d6c2004-05-26 23:25:30 +0000734}
735
drh29d72102006-02-09 22:13:41 +0000736/* The following function is experimental and subject to change or
737** removal */
738/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
739** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
740**}
741*/
742
drh5e2517e2004-09-24 23:59:12 +0000743/*
744** Convert the N-th element of pStmt->pColName[] into a string using
745** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000746**
747** There are up to 5 names for each column. useType determines which
748** name is returned. Here are the names:
749**
750** 0 The column name as it should be displayed for output
751** 1 The datatype name for the column
752** 2 The name of the database that the column derives from
753** 3 The name of the table that the column derives from
754** 4 The name of the table column that the result column derives from
755**
756** If the result is not a simple column reference (if it is an expression
757** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000758*/
759static const void *columnName(
760 sqlite3_stmt *pStmt,
761 int N,
762 const void *(*xFunc)(Mem*),
763 int useType
764){
drh32bc3f62007-08-21 20:25:39 +0000765 const void *ret = 0;
drh5e2517e2004-09-24 23:59:12 +0000766 Vdbe *p = (Vdbe *)pStmt;
drh32bc3f62007-08-21 20:25:39 +0000767 int n;
drhc6c7fd52009-04-08 23:05:28 +0000768 sqlite3 *db = p->db;
drh32bc3f62007-08-21 20:25:39 +0000769
drh9b4ea4a2009-04-10 20:32:00 +0000770 assert( db!=0 );
771 n = sqlite3_column_count(pStmt);
772 if( N<n && N>=0 ){
773 N += useType*n;
774 sqlite3_mutex_enter(db->mutex);
775 assert( db->mallocFailed==0 );
776 ret = xFunc(&p->aColName[N]);
777 /* A malloc may have failed inside of the xFunc() call. If this
778 ** is the case, clear the mallocFailed flag and return NULL.
779 */
780 if( db->mallocFailed ){
781 db->mallocFailed = 0;
782 ret = 0;
drh32bc3f62007-08-21 20:25:39 +0000783 }
drh9b4ea4a2009-04-10 20:32:00 +0000784 sqlite3_mutex_leave(db->mutex);
drh5e2517e2004-09-24 23:59:12 +0000785 }
danielk197700fd9572005-12-07 06:27:43 +0000786 return ret;
drh5e2517e2004-09-24 23:59:12 +0000787}
788
drh4f26d6c2004-05-26 23:25:30 +0000789/*
790** Return the name of the Nth column of the result set returned by SQL
791** statement pStmt.
792*/
793const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000794 return columnName(
795 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000796}
drhe590fbd2005-05-20 19:36:01 +0000797#ifndef SQLITE_OMIT_UTF16
798const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000799 return columnName(
800 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000801}
802#endif
drh4f26d6c2004-05-26 23:25:30 +0000803
804/*
drh3f913572008-03-22 01:07:17 +0000805** Constraint: If you have ENABLE_COLUMN_METADATA then you must
806** not define OMIT_DECLTYPE.
807*/
808#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
809# error "Must not define both SQLITE_OMIT_DECLTYPE \
810 and SQLITE_ENABLE_COLUMN_METADATA"
811#endif
812
813#ifndef SQLITE_OMIT_DECLTYPE
814/*
drh6c626082004-11-14 21:56:29 +0000815** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000816** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000817*/
818const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000819 return columnName(
820 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000821}
drh6c626082004-11-14 21:56:29 +0000822#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000823const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000824 return columnName(
825 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000826}
drh6c626082004-11-14 21:56:29 +0000827#endif /* SQLITE_OMIT_UTF16 */
drh3f913572008-03-22 01:07:17 +0000828#endif /* SQLITE_OMIT_DECLTYPE */
drh4f26d6c2004-05-26 23:25:30 +0000829
danielk19774b1ae992006-02-10 03:06:10 +0000830#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000831/*
832** Return the name of the database from which a result column derives.
833** NULL is returned if the result column is an expression or constant or
834** anything else which is not an unabiguous reference to a database column.
835*/
836const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000837 return columnName(
838 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000839}
840#ifndef SQLITE_OMIT_UTF16
841const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000842 return columnName(
843 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000844}
845#endif /* SQLITE_OMIT_UTF16 */
846
847/*
848** Return the name of the table from which a result column derives.
849** NULL is returned if the result column is an expression or constant or
850** anything else which is not an unabiguous reference to a database column.
851*/
852const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000853 return columnName(
854 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000855}
856#ifndef SQLITE_OMIT_UTF16
857const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000858 return columnName(
859 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000860}
861#endif /* SQLITE_OMIT_UTF16 */
862
863/*
864** Return the name of the table column from which a result column derives.
865** NULL is returned if the result column is an expression or constant or
866** anything else which is not an unabiguous reference to a database column.
867*/
868const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000869 return columnName(
870 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000871}
872#ifndef SQLITE_OMIT_UTF16
873const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000874 return columnName(
875 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000876}
877#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000878#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000879
880
drh4f26d6c2004-05-26 23:25:30 +0000881/******************************* sqlite3_bind_ ***************************
882**
883** Routines used to attach values to wildcards in a compiled SQL statement.
884*/
885/*
886** Unbind the value bound to variable i in virtual machine p. This is the
887** the same as binding a NULL value to the column. If the "i" parameter is
888** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
889**
drh488e7b62008-10-06 12:46:43 +0000890** A successful evaluation of this routine acquires the mutex on p.
891** the mutex is released if any kind of error occurs.
892**
drh4f26d6c2004-05-26 23:25:30 +0000893** The error code stored in database p->db is overwritten with the return
894** value in any case.
895*/
896static int vdbeUnbind(Vdbe *p, int i){
897 Mem *pVar;
drh488e7b62008-10-06 12:46:43 +0000898 if( p==0 ) return SQLITE_MISUSE;
899 sqlite3_mutex_enter(p->db->mutex);
900 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
901 sqlite3Error(p->db, SQLITE_MISUSE, 0);
902 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000903 return SQLITE_MISUSE;
904 }
905 if( i<1 || i>p->nVar ){
906 sqlite3Error(p->db, SQLITE_RANGE, 0);
drh488e7b62008-10-06 12:46:43 +0000907 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000908 return SQLITE_RANGE;
909 }
910 i--;
drh290c1942004-08-21 17:54:45 +0000911 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000912 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000913 pVar->flags = MEM_Null;
914 sqlite3Error(p->db, SQLITE_OK, 0);
915 return SQLITE_OK;
916}
917
918/*
drh5e2517e2004-09-24 23:59:12 +0000919** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000920*/
drh5e2517e2004-09-24 23:59:12 +0000921static int bindText(
drh605264d2007-08-21 15:13:19 +0000922 sqlite3_stmt *pStmt, /* The statement to bind against */
923 int i, /* Index of the parameter to bind */
924 const void *zData, /* Pointer to the data to be bound */
925 int nData, /* Number of bytes of data to be bound */
926 void (*xDel)(void*), /* Destructor for the data */
drhb27b7f52008-12-10 18:03:45 +0000927 u8 encoding /* Encoding for the data */
drh4f26d6c2004-05-26 23:25:30 +0000928){
929 Vdbe *p = (Vdbe *)pStmt;
930 Mem *pVar;
931 int rc;
932
933 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +0000934 if( rc==SQLITE_OK ){
935 if( zData!=0 ){
936 pVar = &p->aVar[i-1];
937 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
938 if( rc==SQLITE_OK && encoding!=0 ){
939 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
940 }
941 sqlite3Error(p->db, rc, 0);
942 rc = sqlite3ApiExit(p->db, rc);
drh27641702007-08-22 02:56:42 +0000943 }
drh488e7b62008-10-06 12:46:43 +0000944 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000945 }
drh605264d2007-08-21 15:13:19 +0000946 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000947}
drh5e2517e2004-09-24 23:59:12 +0000948
949
950/*
951** Bind a blob value to an SQL statement variable.
952*/
953int sqlite3_bind_blob(
954 sqlite3_stmt *pStmt,
955 int i,
956 const void *zData,
957 int nData,
958 void (*xDel)(void*)
959){
960 return bindText(pStmt, i, zData, nData, xDel, 0);
961}
drh4f26d6c2004-05-26 23:25:30 +0000962int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
963 int rc;
964 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000965 rc = vdbeUnbind(p, i);
966 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000967 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh488e7b62008-10-06 12:46:43 +0000968 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000969 }
danielk1977f4618892004-06-28 13:09:11 +0000970 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000971}
972int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000973 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000974}
drhefad9992004-06-22 12:13:55 +0000975int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000976 int rc;
977 Vdbe *p = (Vdbe *)pStmt;
978 rc = vdbeUnbind(p, i);
979 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000980 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh488e7b62008-10-06 12:46:43 +0000981 sqlite3_mutex_leave(p->db->mutex);
drh4f26d6c2004-05-26 23:25:30 +0000982 }
983 return rc;
984}
drh605264d2007-08-21 15:13:19 +0000985int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
986 int rc;
987 Vdbe *p = (Vdbe*)pStmt;
drh605264d2007-08-21 15:13:19 +0000988 rc = vdbeUnbind(p, i);
drh488e7b62008-10-06 12:46:43 +0000989 if( rc==SQLITE_OK ){
990 sqlite3_mutex_leave(p->db->mutex);
991 }
drh605264d2007-08-21 15:13:19 +0000992 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000993}
994int sqlite3_bind_text(
995 sqlite3_stmt *pStmt,
996 int i,
997 const char *zData,
998 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000999 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001000){
drh5e2517e2004-09-24 23:59:12 +00001001 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +00001002}
drh6c626082004-11-14 21:56:29 +00001003#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +00001004int sqlite3_bind_text16(
1005 sqlite3_stmt *pStmt,
1006 int i,
1007 const void *zData,
1008 int nData,
danielk1977d8123362004-06-12 09:25:12 +00001009 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +00001010){
drh5e2517e2004-09-24 23:59:12 +00001011 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +00001012}
drh6c626082004-11-14 21:56:29 +00001013#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +00001014int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1015 int rc;
drh29def562009-04-14 12:43:33 +00001016 switch( pValue->type ){
drh29def562009-04-14 12:43:33 +00001017 case SQLITE_INTEGER: {
1018 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1019 break;
1020 }
1021 case SQLITE_FLOAT: {
1022 rc = sqlite3_bind_double(pStmt, i, pValue->r);
1023 break;
1024 }
1025 case SQLITE_BLOB: {
1026 if( pValue->flags & MEM_Zero ){
1027 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1028 }else{
1029 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1030 }
1031 break;
1032 }
1033 case SQLITE_TEXT: {
drhac80db72009-04-14 12:58:20 +00001034 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
1035 pValue->enc);
1036 break;
1037 }
1038 default: {
1039 rc = sqlite3_bind_null(pStmt, i);
drh29def562009-04-14 12:43:33 +00001040 break;
1041 }
danielk197726e41442006-06-14 15:16:35 +00001042 }
1043 return rc;
1044}
drhb026e052007-05-02 01:34:31 +00001045int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1046 int rc;
1047 Vdbe *p = (Vdbe *)pStmt;
1048 rc = vdbeUnbind(p, i);
1049 if( rc==SQLITE_OK ){
1050 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
drh488e7b62008-10-06 12:46:43 +00001051 sqlite3_mutex_leave(p->db->mutex);
drhb026e052007-05-02 01:34:31 +00001052 }
1053 return rc;
1054}
drh75f6a032004-07-15 14:15:00 +00001055
1056/*
1057** Return the number of wildcards that can be potentially bound to.
1058** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +00001059*/
1060int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +00001061 Vdbe *p = (Vdbe*)pStmt;
1062 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +00001063}
drh895d7472004-08-20 16:02:39 +00001064
1065/*
drhfa6bc002004-09-07 16:19:52 +00001066** Create a mapping from variable numbers to variable names
1067** in the Vdbe.azVar[] array, if such a mapping does not already
1068** exist.
drh895d7472004-08-20 16:02:39 +00001069*/
drhfa6bc002004-09-07 16:19:52 +00001070static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +00001071 if( !p->okVar ){
drh0f5ea0b2009-04-09 14:02:44 +00001072 int j;
1073 Op *pOp;
drh605264d2007-08-21 15:13:19 +00001074 sqlite3_mutex_enter(p->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001075 /* The race condition here is harmless. If two threads call this
1076 ** routine on the same Vdbe at the same time, they both might end
1077 ** up initializing the Vdbe.azVar[] array. That is a little extra
1078 ** work but it results in the same answer.
1079 */
1080 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1081 if( pOp->opcode==OP_Variable ){
1082 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1083 p->azVar[pOp->p1-1] = pOp->p4.z;
drh895d7472004-08-20 16:02:39 +00001084 }
1085 }
drh0f5ea0b2009-04-09 14:02:44 +00001086 p->okVar = 1;
drh605264d2007-08-21 15:13:19 +00001087 sqlite3_mutex_leave(p->db->mutex);
drh895d7472004-08-20 16:02:39 +00001088 }
drhfa6bc002004-09-07 16:19:52 +00001089}
1090
1091/*
1092** Return the name of a wildcard parameter. Return NULL if the index
1093** is out of range or if the wildcard is unnamed.
1094**
1095** The result is always UTF-8.
1096*/
1097const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1098 Vdbe *p = (Vdbe*)pStmt;
1099 if( p==0 || i<1 || i>p->nVar ){
1100 return 0;
1101 }
1102 createVarMap(p);
drh895d7472004-08-20 16:02:39 +00001103 return p->azVar[i-1];
1104}
drhfa6bc002004-09-07 16:19:52 +00001105
1106/*
1107** Given a wildcard parameter name, return the index of the variable
1108** with that name. If there is no variable with the given name,
1109** return 0.
1110*/
1111int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1112 Vdbe *p = (Vdbe*)pStmt;
1113 int i;
1114 if( p==0 ){
1115 return 0;
1116 }
1117 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +00001118 if( zName ){
1119 for(i=0; i<p->nVar; i++){
1120 const char *z = p->azVar[i];
1121 if( z && strcmp(z,zName)==0 ){
1122 return i+1;
1123 }
drhfa6bc002004-09-07 16:19:52 +00001124 }
1125 }
1126 return 0;
1127}
drhf8db1bc2005-04-22 02:38:37 +00001128
drh51942bc2005-06-12 22:01:42 +00001129/*
drhf8db1bc2005-04-22 02:38:37 +00001130** Transfer all bindings from the first statement over to the second.
drhf8db1bc2005-04-22 02:38:37 +00001131*/
shane145834a2008-09-04 12:03:42 +00001132int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drhf8db1bc2005-04-22 02:38:37 +00001133 Vdbe *pFrom = (Vdbe*)pFromStmt;
1134 Vdbe *pTo = (Vdbe*)pToStmt;
drh0f5ea0b2009-04-09 14:02:44 +00001135 int i;
1136 assert( pTo->db==pFrom->db );
1137 assert( pTo->nVar==pFrom->nVar );
drh27641702007-08-22 02:56:42 +00001138 sqlite3_mutex_enter(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001139 for(i=0; i<pFrom->nVar; i++){
drh643167f2008-01-22 21:30:53 +00001140 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
drhf8db1bc2005-04-22 02:38:37 +00001141 }
drh27641702007-08-22 02:56:42 +00001142 sqlite3_mutex_leave(pTo->db->mutex);
drh0f5ea0b2009-04-09 14:02:44 +00001143 return SQLITE_OK;
drhf8db1bc2005-04-22 02:38:37 +00001144}
drh51942bc2005-06-12 22:01:42 +00001145
shaneeec556d2008-10-12 00:27:53 +00001146#ifndef SQLITE_OMIT_DEPRECATED
drh51942bc2005-06-12 22:01:42 +00001147/*
shane145834a2008-09-04 12:03:42 +00001148** Deprecated external interface. Internal/core SQLite code
1149** should call sqlite3TransferBindings.
drh0f5ea0b2009-04-09 14:02:44 +00001150**
1151** Is is misuse to call this routine with statements from different
1152** database connections. But as this is a deprecated interface, we
1153** will not bother to check for that condition.
1154**
1155** If the two statements contain a different number of bindings, then
1156** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1157** SQLITE_OK is returned.
shane145834a2008-09-04 12:03:42 +00001158*/
1159int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
drh0f5ea0b2009-04-09 14:02:44 +00001160 Vdbe *pFrom = (Vdbe*)pFromStmt;
1161 Vdbe *pTo = (Vdbe*)pToStmt;
1162 if( pFrom->nVar!=pTo->nVar ){
1163 return SQLITE_ERROR;
1164 }
shane145834a2008-09-04 12:03:42 +00001165 return sqlite3TransferBindings(pFromStmt, pToStmt);
1166}
shaneeec556d2008-10-12 00:27:53 +00001167#endif
shane145834a2008-09-04 12:03:42 +00001168
1169/*
drh51942bc2005-06-12 22:01:42 +00001170** Return the sqlite3* database handle to which the prepared statement given
1171** in the argument belongs. This is the same database handle that was
1172** the first argument to the sqlite3_prepare() that was used to create
1173** the statement in the first place.
1174*/
1175sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1176 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1177}
drhbb5a9c32008-06-19 02:52:25 +00001178
1179/*
1180** Return a pointer to the next prepared statement after pStmt associated
1181** with database connection pDb. If pStmt is NULL, return the first
1182** prepared statement for the database connection. Return NULL if there
1183** are no more.
1184*/
1185sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1186 sqlite3_stmt *pNext;
1187 sqlite3_mutex_enter(pDb->mutex);
1188 if( pStmt==0 ){
1189 pNext = (sqlite3_stmt*)pDb->pVdbe;
1190 }else{
1191 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1192 }
1193 sqlite3_mutex_leave(pDb->mutex);
1194 return pNext;
1195}
drhd1d38482008-10-07 23:46:38 +00001196
1197/*
1198** Return the value of a status counter for a prepared statement
1199*/
1200int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1201 Vdbe *pVdbe = (Vdbe*)pStmt;
1202 int v = pVdbe->aCounter[op-1];
1203 if( resetFlag ) pVdbe->aCounter[op-1] = 0;
1204 return v;
1205}