blob: 6b80796a0ec692b8645e8d87a966fb4d9201a0cc [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"
drh19e2d372005-08-29 23:00:03 +000018#include "os.h"
drh4f26d6c2004-05-26 23:25:30 +000019
drhd89bd002005-01-22 03:03:54 +000020/*
21** Return TRUE (non-zero) of the statement supplied as an argument needs
22** to be recompiled. A statement needs to be recompiled whenever the
23** execution environment changes in a way that would alter the program
24** that sqlite3_prepare() generates. For example, if new functions or
25** collating sequences are registered or if an authorizer function is
26** added or changed.
drhd89bd002005-01-22 03:03:54 +000027*/
28int sqlite3_expired(sqlite3_stmt *pStmt){
29 Vdbe *p = (Vdbe*)pStmt;
30 return p==0 || p->expired;
31}
32
drh4f26d6c2004-05-26 23:25:30 +000033/**************************** sqlite3_value_ *******************************
34** The following routines extract information from a Mem or sqlite3_value
35** structure.
36*/
37const void *sqlite3_value_blob(sqlite3_value *pVal){
38 Mem *p = (Mem*)pVal;
39 if( p->flags & (MEM_Blob|MEM_Str) ){
danielk19771e536952007-08-16 10:09:01 +000040 sqlite3VdbeMemExpandBlob(0, p);
drh1f0feef2007-05-15 13:27:07 +000041 p->flags &= ~MEM_Str;
42 p->flags |= MEM_Blob;
drh4f26d6c2004-05-26 23:25:30 +000043 return p->z;
44 }else{
45 return sqlite3_value_text(pVal);
46 }
47}
48int sqlite3_value_bytes(sqlite3_value *pVal){
danielk19771e536952007-08-16 10:09:01 +000049 return sqlite3ValueBytes(0, pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000050}
51int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk19771e536952007-08-16 10:09:01 +000052 return sqlite3ValueBytes(0, pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000053}
54double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000055 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000056}
57int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000058 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000059}
drhefad9992004-06-22 12:13:55 +000060sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000061 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000062}
63const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk19771e536952007-08-16 10:09:01 +000064 return (const unsigned char *)sqlite3ValueText(0, pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000065}
drh6c626082004-11-14 21:56:29 +000066#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000067const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk19771e536952007-08-16 10:09:01 +000068 return sqlite3ValueText(0, pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000069}
danielk1977d8123362004-06-12 09:25:12 +000070const void *sqlite3_value_text16be(sqlite3_value *pVal){
danielk19771e536952007-08-16 10:09:01 +000071 return sqlite3ValueText(0, pVal, SQLITE_UTF16BE);
danielk1977d8123362004-06-12 09:25:12 +000072}
73const void *sqlite3_value_text16le(sqlite3_value *pVal){
danielk19771e536952007-08-16 10:09:01 +000074 return sqlite3ValueText(0, pVal, SQLITE_UTF16LE);
danielk1977d8123362004-06-12 09:25:12 +000075}
drh6c626082004-11-14 21:56:29 +000076#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000077int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000078 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000079}
drh29d72102006-02-09 22:13:41 +000080/* sqlite3_value_numeric_type() defined in vdbe.c */
drh4f26d6c2004-05-26 23:25:30 +000081
82/**************************** sqlite3_result_ *******************************
83** The following routines are used by user-defined functions to specify
84** the function result.
85*/
86void sqlite3_result_blob(
87 sqlite3_context *pCtx,
88 const void *z,
89 int n,
danielk1977d8123362004-06-12 09:25:12 +000090 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000091){
drh5708d2d2005-06-22 10:53:59 +000092 assert( n>=0 );
danielk19771e536952007-08-16 10:09:01 +000093 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000094}
95void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
96 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
97}
98void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
99 pCtx->isError = 1;
danielk19771e536952007-08-16 10:09:01 +0000100 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000101}
danielk1977a1686c92006-01-23 07:52:37 +0000102#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000103void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
104 pCtx->isError = 1;
danielk19771e536952007-08-16 10:09:01 +0000105 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000106}
danielk1977a1686c92006-01-23 07:52:37 +0000107#endif
drhf4479502004-05-27 03:12:53 +0000108void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000109 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
110}
111void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
112 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
113}
114void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000115 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000116}
117void sqlite3_result_text(
118 sqlite3_context *pCtx,
119 const char *z,
120 int n,
danielk1977d8123362004-06-12 09:25:12 +0000121 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000122){
danielk19771e536952007-08-16 10:09:01 +0000123 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000124}
drh6c626082004-11-14 21:56:29 +0000125#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000126void sqlite3_result_text16(
127 sqlite3_context *pCtx,
128 const void *z,
129 int n,
danielk1977d8123362004-06-12 09:25:12 +0000130 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000131){
danielk19771e536952007-08-16 10:09:01 +0000132 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000133}
134void sqlite3_result_text16be(
135 sqlite3_context *pCtx,
136 const void *z,
137 int n,
138 void (*xDel)(void *)
139){
danielk19771e536952007-08-16 10:09:01 +0000140 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF16BE, xDel);
danielk1977d8123362004-06-12 09:25:12 +0000141}
142void sqlite3_result_text16le(
143 sqlite3_context *pCtx,
144 const void *z,
145 int n,
146 void (*xDel)(void *)
147){
danielk19771e536952007-08-16 10:09:01 +0000148 sqlite3VdbeMemSetStr(0, &pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000149}
drh6c626082004-11-14 21:56:29 +0000150#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000151void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
danielk19771e536952007-08-16 10:09:01 +0000152 sqlite3VdbeMemCopy(0, &pCtx->s, pValue);
drh4f26d6c2004-05-26 23:25:30 +0000153}
drhb026e052007-05-02 01:34:31 +0000154void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
155 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
156}
drh4f26d6c2004-05-26 23:25:30 +0000157
drha0206bc2007-05-08 15:15:02 +0000158/* Force an SQLITE_TOOBIG error. */
159void sqlite3_result_error_toobig(sqlite3_context *pCtx){
160 sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
161}
162
drh4f26d6c2004-05-26 23:25:30 +0000163
164/*
165** Execute the statement pStmt, either until a row of data is ready, the
166** statement is completely executed or an error occurs.
drhb900aaf2006-11-09 00:24:53 +0000167**
168** This routine implements the bulk of the logic behind the sqlite_step()
169** API. The only thing omitted is the automatic recompile if a
170** schema change has occurred. That detail is handled by the
171** outer sqlite3_step() wrapper procedure.
drh4f26d6c2004-05-26 23:25:30 +0000172*/
drhb900aaf2006-11-09 00:24:53 +0000173static int sqlite3Step(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +0000174 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000175 int rc;
176
danielk1977efaaf572006-01-16 11:29:19 +0000177 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000178 db = p->db;
179 assert( !db->mallocFailed );
danielk1977261919c2005-12-06 12:52:59 +0000180
drh1bcdb0c2004-08-28 14:49:46 +0000181 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000182 return SQLITE_MISUSE;
183 }
drh91b48aa2004-06-30 11:14:18 +0000184 if( p->aborted ){
185 return SQLITE_ABORT;
186 }
danielk1977a21c6b62005-01-24 10:25:59 +0000187 if( p->pc<=0 && p->expired ){
188 if( p->rc==SQLITE_OK ){
189 p->rc = SQLITE_SCHEMA;
190 }
drhb900aaf2006-11-09 00:24:53 +0000191 rc = SQLITE_ERROR;
192 goto end_of_step;
danielk1977a21c6b62005-01-24 10:25:59 +0000193 }
drh4f26d6c2004-05-26 23:25:30 +0000194 if( sqlite3SafetyOn(db) ){
195 p->rc = SQLITE_MISUSE;
196 return SQLITE_MISUSE;
197 }
danielk19771d850a72004-05-31 08:26:49 +0000198 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000199 /* If there are no other statements currently running, then
200 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
201 ** from interrupting a statement that has not yet started.
202 */
203 if( db->activeVdbeCnt==0 ){
204 db->u1.isInterrupted = 0;
205 }
206
drh19e2d372005-08-29 23:00:03 +0000207#ifndef SQLITE_OMIT_TRACE
drhc16a03b2004-09-15 13:38:10 +0000208 /* Invoke the trace callback if there is one
209 */
drh19e2d372005-08-29 23:00:03 +0000210 if( db->xTrace && !db->init.busy ){
drhc16a03b2004-09-15 13:38:10 +0000211 assert( p->nOp>0 );
212 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
213 assert( p->aOp[p->nOp-1].p3!=0 );
214 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
215 sqlite3SafetyOff(db);
216 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
217 if( sqlite3SafetyOn(db) ){
218 p->rc = SQLITE_MISUSE;
219 return SQLITE_MISUSE;
220 }
221 }
drh19e2d372005-08-29 23:00:03 +0000222 if( db->xProfile && !db->init.busy ){
223 double rNow;
drh66560ad2006-01-06 14:32:19 +0000224 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000225 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
226 }
227#endif
drhc16a03b2004-09-15 13:38:10 +0000228
229 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
230 ** on in debugging mode.
231 */
232#ifdef SQLITE_DEBUG
233 if( (db->flags & SQLITE_SqlTrace)!=0 ){
234 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
235 }
236#endif /* SQLITE_DEBUG */
237
danielk19771d850a72004-05-31 08:26:49 +0000238 db->activeVdbeCnt++;
239 p->pc = 0;
240 }
drhb7f91642004-10-31 02:22:47 +0000241#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000242 if( p->explain ){
243 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000244 }else
245#endif /* SQLITE_OMIT_EXPLAIN */
246 {
drh4f26d6c2004-05-26 23:25:30 +0000247 rc = sqlite3VdbeExec(p);
248 }
249
250 if( sqlite3SafetyOff(db) ){
251 rc = SQLITE_MISUSE;
252 }
253
drh19e2d372005-08-29 23:00:03 +0000254#ifndef SQLITE_OMIT_TRACE
255 /* Invoke the profile callback if there is one
256 */
257 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
258 double rNow;
259 u64 elapseTime;
260
drh66560ad2006-01-06 14:32:19 +0000261 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000262 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
263 assert( p->nOp>0 );
264 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
265 assert( p->aOp[p->nOp-1].p3!=0 );
266 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
267 db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
268 }
269#endif
270
danielk197797a227c2006-01-20 16:32:04 +0000271 sqlite3Error(p->db, rc, 0);
danielk197776e8d1a2006-01-18 18:22:43 +0000272 p->rc = sqlite3ApiExit(p->db, p->rc);
drhb900aaf2006-11-09 00:24:53 +0000273end_of_step:
drh4ac285a2006-09-15 07:28:50 +0000274 assert( (rc&0xff)==rc );
drhb900aaf2006-11-09 00:24:53 +0000275 if( p->zSql && (rc&0xff)<SQLITE_ROW ){
276 /* This behavior occurs if sqlite3_prepare_v2() was used to build
277 ** the prepared statement. Return error codes directly */
danielk1977612642d2007-07-12 13:18:05 +0000278 sqlite3Error(p->db, p->rc, 0);
drhb900aaf2006-11-09 00:24:53 +0000279 return p->rc;
280 }else{
281 /* This is for legacy sqlite3_prepare() builds and when the code
282 ** is SQLITE_ROW or SQLITE_DONE */
283 return rc;
284 }
285}
286
287/*
288** This is the top-level implementation of sqlite3_step(). Call
289** sqlite3Step() to do most of the work. If a schema error occurs,
290** call sqlite3Reprepare() and try again.
291*/
292#ifdef SQLITE_OMIT_PARSER
293int sqlite3_step(sqlite3_stmt *pStmt){
294 return sqlite3Step((Vdbe*)pStmt);
295}
296#else
297int sqlite3_step(sqlite3_stmt *pStmt){
298 int cnt = 0;
299 int rc;
300 Vdbe *v = (Vdbe*)pStmt;
301 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
302 && cnt++ < 5
303 && sqlite3Reprepare(v) ){
304 sqlite3_reset(pStmt);
305 v->expired = 0;
306 }
danielk197776e8d1a2006-01-18 18:22:43 +0000307 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000308}
drhb900aaf2006-11-09 00:24:53 +0000309#endif
drh4f26d6c2004-05-26 23:25:30 +0000310
311/*
drheb2e1762004-05-27 01:53:56 +0000312** Extract the user data from a sqlite3_context structure and return a
313** pointer to it.
314*/
315void *sqlite3_user_data(sqlite3_context *p){
316 assert( p && p->pFunc );
317 return p->pFunc->pUserData;
318}
319
320/*
drhb7481e72006-09-16 21:45:14 +0000321** The following is the implementation of an SQL function that always
322** fails with an error message stating that the function is used in the
323** wrong context. The sqlite3_overload_function() API might construct
324** SQL function that use this routine so that the functions will exist
325** for name resolution but are actually overloaded by the xFindFunction
326** method of virtual tables.
327*/
328void sqlite3InvalidFunction(
329 sqlite3_context *context, /* The function calling context */
330 int argc, /* Number of arguments to the function */
331 sqlite3_value **argv /* Value of each argument */
332){
333 const char *zName = context->pFunc->zName;
334 char *zErr;
danielk19771e536952007-08-16 10:09:01 +0000335 zErr = sqlite3MPrintf(0,
drhb7481e72006-09-16 21:45:14 +0000336 "unable to use function %s in the requested context", zName);
337 sqlite3_result_error(context, zErr, -1);
danielk19771e536952007-08-16 10:09:01 +0000338 sqlite3_free(zErr);
drhb7481e72006-09-16 21:45:14 +0000339}
340
341/*
drheb2e1762004-05-27 01:53:56 +0000342** Allocate or return the aggregate context for a user function. A new
343** context is allocated on the first call. Subsequent calls return the
344** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000345*/
346void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhabfcea22005-09-06 20:36:48 +0000347 Mem *pMem = p->pMem;
drh825c6622005-09-08 19:01:05 +0000348 assert( p && p->pFunc && p->pFunc->xStep );
drha10a34b2005-09-07 22:09:48 +0000349 if( (pMem->flags & MEM_Agg)==0 ){
350 if( nByte==0 ){
351 assert( pMem->flags==MEM_Null );
352 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000353 }else{
drha10a34b2005-09-07 22:09:48 +0000354 pMem->flags = MEM_Agg;
danielk19771e536952007-08-16 10:09:01 +0000355 pMem->xDel = sqlite3_free;
drh3c024d62007-03-30 11:23:45 +0000356 pMem->u.pDef = p->pFunc;
drha10a34b2005-09-07 22:09:48 +0000357 if( nByte<=NBFS ){
358 pMem->z = pMem->zShort;
359 memset(pMem->z, 0, nByte);
360 }else{
drh17435752007-08-16 04:30:38 +0000361 pMem->z = sqlite3DbMallocZero(p->db, nByte);
drha10a34b2005-09-07 22:09:48 +0000362 }
drheb2e1762004-05-27 01:53:56 +0000363 }
364 }
drhabfcea22005-09-06 20:36:48 +0000365 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000366}
367
368/*
danielk1977682f68b2004-06-05 10:22:17 +0000369** Return the auxilary data pointer, if any, for the iArg'th argument to
370** the user-function defined by pCtx.
371*/
372void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
373 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
374 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
375 return 0;
376 }
drhf92c7ff2004-06-19 15:40:23 +0000377 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000378}
379
380/*
381** Set the auxilary data pointer and delete function, for the iArg'th
382** argument to the user-function defined by pCtx. Any previous value is
383** deleted by calling the delete function specified when it was set.
384*/
385void sqlite3_set_auxdata(
386 sqlite3_context *pCtx,
387 int iArg,
388 void *pAux,
389 void (*xDelete)(void*)
390){
391 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000392 VdbeFunc *pVdbeFunc;
danielk1977e0fc5262007-07-26 06:50:05 +0000393 if( iArg<0 ) goto failed;
danielk1977682f68b2004-06-05 10:22:17 +0000394
drhf92c7ff2004-06-19 15:40:23 +0000395 pVdbeFunc = pCtx->pVdbeFunc;
396 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
397 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
drh17435752007-08-16 04:30:38 +0000398 pVdbeFunc = sqlite3_realloc(pVdbeFunc, nMalloc);
399 if( !pVdbeFunc ){
400 pCtx->db->mallocFailed = 1;
401 goto failed;
402 }
drh53f733c2005-09-16 02:38:09 +0000403 pCtx->pVdbeFunc = pVdbeFunc;
drh0e3d7472004-06-19 17:33:07 +0000404 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
405 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000406 pVdbeFunc->nAux = iArg+1;
407 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000408 }
409
drhf92c7ff2004-06-19 15:40:23 +0000410 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000411 if( pAuxData->pAux && pAuxData->xDelete ){
412 pAuxData->xDelete(pAuxData->pAux);
413 }
414 pAuxData->pAux = pAux;
415 pAuxData->xDelete = xDelete;
danielk1977e0fc5262007-07-26 06:50:05 +0000416 return;
417
418failed:
419 if( xDelete ){
420 xDelete(pAux);
421 }
danielk1977682f68b2004-06-05 10:22:17 +0000422}
423
424/*
drheb2e1762004-05-27 01:53:56 +0000425** Return the number of times the Step function of a aggregate has been
426** called.
427**
drhcf85a512006-02-09 18:35:29 +0000428** This function is deprecated. Do not use it for new code. It is
429** provide only to avoid breaking legacy code. New aggregate function
430** implementations should keep their own counts within their aggregate
431** context.
drheb2e1762004-05-27 01:53:56 +0000432*/
433int sqlite3_aggregate_count(sqlite3_context *p){
434 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000435 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000436}
437
438/*
drh4f26d6c2004-05-26 23:25:30 +0000439** Return the number of columns in the result set for the statement pStmt.
440*/
441int sqlite3_column_count(sqlite3_stmt *pStmt){
442 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000443 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000444}
445
446/*
447** Return the number of values available from the current row of the
448** currently executing statement pStmt.
449*/
450int sqlite3_data_count(sqlite3_stmt *pStmt){
451 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000452 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000453 return pVm->nResColumn;
454}
455
456
457/*
458** Check to see if column iCol of the given statement is valid. If
459** it is, return a pointer to the Mem for the value of that column.
460** If iCol is not valid, return a pointer to a Mem which has a value
461** of NULL.
462*/
463static Mem *columnMem(sqlite3_stmt *pStmt, int i){
464 Vdbe *pVm = (Vdbe *)pStmt;
465 int vals = sqlite3_data_count(pStmt);
drhb21f87d2007-06-19 10:58:24 +0000466 if( pVm==0 || pVm->resOnStack==0 || i>=pVm->nResColumn || i<0 ){
drh7a521cf2007-04-25 18:23:52 +0000467 static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL };
drh4f26d6c2004-05-26 23:25:30 +0000468 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
drh0f7eb612006-08-08 13:51:43 +0000469 return (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000470 }
471 return &pVm->pTos[(1-vals)+i];
472}
473
danielk19772e588c72005-12-09 14:25:08 +0000474/*
475** This function is called after invoking an sqlite3_value_XXX function on a
476** column value (i.e. a value returned by evaluating an SQL expression in the
477** select list of a SELECT statement) that may cause a malloc() failure. If
478** malloc() has failed, the threads mallocFailed flag is cleared and the result
479** code of statement pStmt set to SQLITE_NOMEM.
480**
481** Specificly, this is called from within:
482**
483** sqlite3_column_int()
484** sqlite3_column_int64()
485** sqlite3_column_text()
486** sqlite3_column_text16()
487** sqlite3_column_real()
488** sqlite3_column_bytes()
489** sqlite3_column_bytes16()
490**
491** But not for sqlite3_column_blob(), which never calls malloc().
492*/
493static void columnMallocFailure(sqlite3_stmt *pStmt)
494{
495 /* If malloc() failed during an encoding conversion within an
496 ** sqlite3_column_XXX API, then set the return code of the statement to
497 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
498 ** and _finalize() will return NOMEM.
499 */
danielk197754f01982006-01-18 15:25:17 +0000500 Vdbe *p = (Vdbe *)pStmt;
501 p->rc = sqlite3ApiExit(0, p->rc);
danielk19772e588c72005-12-09 14:25:08 +0000502}
503
drh4f26d6c2004-05-26 23:25:30 +0000504/**************************** sqlite3_column_ *******************************
505** The following routines are used to access elements of the current row
506** in the result set.
507*/
danielk1977c572ef72004-05-27 09:28:41 +0000508const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000509 const void *val;
danielk19772e588c72005-12-09 14:25:08 +0000510 val = sqlite3_value_blob( columnMem(pStmt,i) );
danielk1977c9cf9012007-05-30 10:36:47 +0000511 /* Even though there is no encoding conversion, value_blob() might
512 ** need to call malloc() to expand the result of a zeroblob()
513 ** expression.
514 */
515 columnMallocFailure(pStmt);
danielk19772e588c72005-12-09 14:25:08 +0000516 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000517}
drh4f26d6c2004-05-26 23:25:30 +0000518int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000519 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
520 columnMallocFailure(pStmt);
521 return val;
drh4f26d6c2004-05-26 23:25:30 +0000522}
523int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000524 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
525 columnMallocFailure(pStmt);
526 return val;
drh4f26d6c2004-05-26 23:25:30 +0000527}
528double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000529 double val = sqlite3_value_double( columnMem(pStmt,i) );
530 columnMallocFailure(pStmt);
531 return val;
drh4f26d6c2004-05-26 23:25:30 +0000532}
533int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000534 int val = sqlite3_value_int( columnMem(pStmt,i) );
535 columnMallocFailure(pStmt);
536 return val;
drh4f26d6c2004-05-26 23:25:30 +0000537}
drhefad9992004-06-22 12:13:55 +0000538sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000539 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
540 columnMallocFailure(pStmt);
541 return val;
drh4f26d6c2004-05-26 23:25:30 +0000542}
543const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000544 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
545 columnMallocFailure(pStmt);
546 return val;
drh4f26d6c2004-05-26 23:25:30 +0000547}
drhd1e47332005-06-26 17:55:33 +0000548sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
549 return columnMem(pStmt, i);
550}
drh6c626082004-11-14 21:56:29 +0000551#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000552const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000553 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
554 columnMallocFailure(pStmt);
555 return val;
drh4f26d6c2004-05-26 23:25:30 +0000556}
drh6c626082004-11-14 21:56:29 +0000557#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000558int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
559 return sqlite3_value_type( columnMem(pStmt,i) );
560}
561
drh29d72102006-02-09 22:13:41 +0000562/* The following function is experimental and subject to change or
563** removal */
564/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
565** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
566**}
567*/
568
drh5e2517e2004-09-24 23:59:12 +0000569/*
570** Convert the N-th element of pStmt->pColName[] into a string using
571** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000572**
573** There are up to 5 names for each column. useType determines which
574** name is returned. Here are the names:
575**
576** 0 The column name as it should be displayed for output
577** 1 The datatype name for the column
578** 2 The name of the database that the column derives from
579** 3 The name of the table that the column derives from
580** 4 The name of the table column that the result column derives from
581**
582** If the result is not a simple column reference (if it is an expression
583** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000584*/
585static const void *columnName(
586 sqlite3_stmt *pStmt,
587 int N,
588 const void *(*xFunc)(Mem*),
589 int useType
590){
danielk197700fd9572005-12-07 06:27:43 +0000591 const void *ret;
drh5e2517e2004-09-24 23:59:12 +0000592 Vdbe *p = (Vdbe *)pStmt;
593 int n = sqlite3_column_count(pStmt);
594
595 if( p==0 || N>=n || N<0 ){
596 return 0;
597 }
drhe590fbd2005-05-20 19:36:01 +0000598 N += useType*n;
danielk197700fd9572005-12-07 06:27:43 +0000599 ret = xFunc(&p->aColName[N]);
600
601 /* A malloc may have failed inside of the xFunc() call. If this is the case,
602 ** clear the mallocFailed flag and return NULL.
603 */
danielk197754f01982006-01-18 15:25:17 +0000604 sqlite3ApiExit(0, 0);
danielk197700fd9572005-12-07 06:27:43 +0000605 return ret;
drh5e2517e2004-09-24 23:59:12 +0000606}
607
drh4f26d6c2004-05-26 23:25:30 +0000608/*
609** Return the name of the Nth column of the result set returned by SQL
610** statement pStmt.
611*/
612const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000613 return columnName(
614 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000615}
drhe590fbd2005-05-20 19:36:01 +0000616#ifndef SQLITE_OMIT_UTF16
617const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000618 return columnName(
619 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000620}
621#endif
drh4f26d6c2004-05-26 23:25:30 +0000622
623/*
drh6c626082004-11-14 21:56:29 +0000624** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000625** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000626*/
627const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000628 return columnName(
629 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000630}
drh6c626082004-11-14 21:56:29 +0000631#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000632const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000633 return columnName(
634 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000635}
drh6c626082004-11-14 21:56:29 +0000636#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000637
danielk19774b1ae992006-02-10 03:06:10 +0000638#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000639/*
640** Return the name of the database from which a result column derives.
641** NULL is returned if the result column is an expression or constant or
642** anything else which is not an unabiguous reference to a database column.
643*/
644const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000645 return columnName(
646 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000647}
648#ifndef SQLITE_OMIT_UTF16
649const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000650 return columnName(
651 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000652}
653#endif /* SQLITE_OMIT_UTF16 */
654
655/*
656** Return the name of the table from which a result column derives.
657** NULL is returned if the result column is an expression or constant or
658** anything else which is not an unabiguous reference to a database column.
659*/
660const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000661 return columnName(
662 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000663}
664#ifndef SQLITE_OMIT_UTF16
665const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000666 return columnName(
667 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000668}
669#endif /* SQLITE_OMIT_UTF16 */
670
671/*
672** Return the name of the table column from which a result column derives.
673** NULL is returned if the result column is an expression or constant or
674** anything else which is not an unabiguous reference to a database column.
675*/
676const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000677 return columnName(
678 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000679}
680#ifndef SQLITE_OMIT_UTF16
681const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000682 return columnName(
683 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000684}
685#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000686#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000687
688
drh4f26d6c2004-05-26 23:25:30 +0000689/******************************* sqlite3_bind_ ***************************
690**
691** Routines used to attach values to wildcards in a compiled SQL statement.
692*/
693/*
694** Unbind the value bound to variable i in virtual machine p. This is the
695** the same as binding a NULL value to the column. If the "i" parameter is
696** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
697**
698** The error code stored in database p->db is overwritten with the return
699** value in any case.
700*/
701static int vdbeUnbind(Vdbe *p, int i){
702 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000703 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +0000704 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +0000705 return SQLITE_MISUSE;
706 }
707 if( i<1 || i>p->nVar ){
708 sqlite3Error(p->db, SQLITE_RANGE, 0);
709 return SQLITE_RANGE;
710 }
711 i--;
drh290c1942004-08-21 17:54:45 +0000712 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000713 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000714 pVar->flags = MEM_Null;
715 sqlite3Error(p->db, SQLITE_OK, 0);
716 return SQLITE_OK;
717}
718
719/*
drh5e2517e2004-09-24 23:59:12 +0000720** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000721*/
drh5e2517e2004-09-24 23:59:12 +0000722static int bindText(
drhf4479502004-05-27 03:12:53 +0000723 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000724 int i,
725 const void *zData,
726 int nData,
drh5e2517e2004-09-24 23:59:12 +0000727 void (*xDel)(void*),
728 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000729){
730 Vdbe *p = (Vdbe *)pStmt;
731 Mem *pVar;
732 int rc;
733
734 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000735 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000736 return rc;
737 }
drh290c1942004-08-21 17:54:45 +0000738 pVar = &p->aVar[i-1];
danielk19771e536952007-08-16 10:09:01 +0000739 rc = sqlite3VdbeMemSetStr(0, pVar, zData, nData, encoding, xDel);
drh5e2517e2004-09-24 23:59:12 +0000740 if( rc==SQLITE_OK && encoding!=0 ){
danielk19771e536952007-08-16 10:09:01 +0000741 rc = sqlite3VdbeChangeEncoding(p->db, pVar, ENC(p->db));
drh5e2517e2004-09-24 23:59:12 +0000742 }
danielk197754f01982006-01-18 15:25:17 +0000743
danielk19771e536952007-08-16 10:09:01 +0000744 sqlite3Error(p->db, rc, 0);
745 return sqlite3ApiExit(p->db, rc);
drh4f26d6c2004-05-26 23:25:30 +0000746}
drh5e2517e2004-09-24 23:59:12 +0000747
748
749/*
750** Bind a blob value to an SQL statement variable.
751*/
752int sqlite3_bind_blob(
753 sqlite3_stmt *pStmt,
754 int i,
755 const void *zData,
756 int nData,
757 void (*xDel)(void*)
758){
759 return bindText(pStmt, i, zData, nData, xDel, 0);
760}
drh4f26d6c2004-05-26 23:25:30 +0000761int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
762 int rc;
763 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000764 rc = vdbeUnbind(p, i);
765 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000766 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000767 }
danielk1977f4618892004-06-28 13:09:11 +0000768 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000769}
770int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000771 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000772}
drhefad9992004-06-22 12:13:55 +0000773int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000774 int rc;
775 Vdbe *p = (Vdbe *)pStmt;
776 rc = vdbeUnbind(p, i);
777 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000778 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000779 }
780 return rc;
781}
782int sqlite3_bind_null(sqlite3_stmt* p, int i){
783 return vdbeUnbind((Vdbe *)p, i);
784}
785int sqlite3_bind_text(
786 sqlite3_stmt *pStmt,
787 int i,
788 const char *zData,
789 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000790 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000791){
drh5e2517e2004-09-24 23:59:12 +0000792 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000793}
drh6c626082004-11-14 21:56:29 +0000794#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000795int sqlite3_bind_text16(
796 sqlite3_stmt *pStmt,
797 int i,
798 const void *zData,
799 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000800 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000801){
drh5e2517e2004-09-24 23:59:12 +0000802 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000803}
drh6c626082004-11-14 21:56:29 +0000804#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +0000805int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
806 int rc;
807 Vdbe *p = (Vdbe *)pStmt;
808 rc = vdbeUnbind(p, i);
809 if( rc==SQLITE_OK ){
danielk19771e536952007-08-16 10:09:01 +0000810 rc = sqlite3VdbeMemCopy(0, &p->aVar[i-1], pValue);
danielk197726e41442006-06-14 15:16:35 +0000811 }
812 return rc;
813}
drhb026e052007-05-02 01:34:31 +0000814int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
815 int rc;
816 Vdbe *p = (Vdbe *)pStmt;
817 rc = vdbeUnbind(p, i);
818 if( rc==SQLITE_OK ){
819 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
820 }
821 return rc;
822}
drh75f6a032004-07-15 14:15:00 +0000823
824/*
825** Return the number of wildcards that can be potentially bound to.
826** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000827*/
828int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000829 Vdbe *p = (Vdbe*)pStmt;
830 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000831}
drh895d7472004-08-20 16:02:39 +0000832
833/*
drhfa6bc002004-09-07 16:19:52 +0000834** Create a mapping from variable numbers to variable names
835** in the Vdbe.azVar[] array, if such a mapping does not already
836** exist.
drh895d7472004-08-20 16:02:39 +0000837*/
drhfa6bc002004-09-07 16:19:52 +0000838static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000839 if( !p->okVar ){
840 int j;
841 Op *pOp;
842 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
843 if( pOp->opcode==OP_Variable ){
844 assert( pOp->p1>0 && pOp->p1<=p->nVar );
845 p->azVar[pOp->p1-1] = pOp->p3;
846 }
847 }
848 p->okVar = 1;
849 }
drhfa6bc002004-09-07 16:19:52 +0000850}
851
852/*
853** Return the name of a wildcard parameter. Return NULL if the index
854** is out of range or if the wildcard is unnamed.
855**
856** The result is always UTF-8.
857*/
858const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
859 Vdbe *p = (Vdbe*)pStmt;
860 if( p==0 || i<1 || i>p->nVar ){
861 return 0;
862 }
863 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000864 return p->azVar[i-1];
865}
drhfa6bc002004-09-07 16:19:52 +0000866
867/*
868** Given a wildcard parameter name, return the index of the variable
869** with that name. If there is no variable with the given name,
870** return 0.
871*/
872int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
873 Vdbe *p = (Vdbe*)pStmt;
874 int i;
875 if( p==0 ){
876 return 0;
877 }
878 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000879 if( zName ){
880 for(i=0; i<p->nVar; i++){
881 const char *z = p->azVar[i];
882 if( z && strcmp(z,zName)==0 ){
883 return i+1;
884 }
drhfa6bc002004-09-07 16:19:52 +0000885 }
886 }
887 return 0;
888}
drhf8db1bc2005-04-22 02:38:37 +0000889
drh51942bc2005-06-12 22:01:42 +0000890/*
drhf8db1bc2005-04-22 02:38:37 +0000891** Transfer all bindings from the first statement over to the second.
892** If the two statements contain a different number of bindings, then
893** an SQLITE_ERROR is returned.
894*/
895int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
896 Vdbe *pFrom = (Vdbe*)pFromStmt;
897 Vdbe *pTo = (Vdbe*)pToStmt;
898 int i, rc = SQLITE_OK;
899 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
900 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
901 return SQLITE_MISUSE;
902 }
903 if( pFrom->nVar!=pTo->nVar ){
904 return SQLITE_ERROR;
905 }
906 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
danielk1977950f0542006-01-18 05:51:57 +0000907 sqlite3MallocDisallow();
danielk19771e536952007-08-16 10:09:01 +0000908 rc = sqlite3VdbeMemMove(0, &pTo->aVar[i], &pFrom->aVar[i]);
danielk1977950f0542006-01-18 05:51:57 +0000909 sqlite3MallocAllow();
drhf8db1bc2005-04-22 02:38:37 +0000910 }
drh4ac285a2006-09-15 07:28:50 +0000911 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
drhf8db1bc2005-04-22 02:38:37 +0000912 return rc;
913}
drh51942bc2005-06-12 22:01:42 +0000914
915/*
916** Return the sqlite3* database handle to which the prepared statement given
917** in the argument belongs. This is the same database handle that was
918** the first argument to the sqlite3_prepare() that was used to create
919** the statement in the first place.
920*/
921sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
922 return pStmt ? ((Vdbe*)pStmt)->db : 0;
923}