blob: 4758e967305b43b6cc3e81649aed324bf037becb [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) ){
40 return p->z;
41 }else{
42 return sqlite3_value_text(pVal);
43 }
44}
45int sqlite3_value_bytes(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000046 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000047}
48int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000049 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000050}
51double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000052 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000053}
54int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000055 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000056}
drhefad9992004-06-22 12:13:55 +000057sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000058 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000059}
60const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk197700fd9572005-12-07 06:27:43 +000061 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000062}
drh6c626082004-11-14 21:56:29 +000063#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000064const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000065 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000066}
danielk1977d8123362004-06-12 09:25:12 +000067const void *sqlite3_value_text16be(sqlite3_value *pVal){
68 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
69}
70const void *sqlite3_value_text16le(sqlite3_value *pVal){
71 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
72}
drh6c626082004-11-14 21:56:29 +000073#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000074int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000075 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000076}
drh29d72102006-02-09 22:13:41 +000077/* sqlite3_value_numeric_type() defined in vdbe.c */
drh4f26d6c2004-05-26 23:25:30 +000078
79/**************************** sqlite3_result_ *******************************
80** The following routines are used by user-defined functions to specify
81** the function result.
82*/
83void sqlite3_result_blob(
84 sqlite3_context *pCtx,
85 const void *z,
86 int n,
danielk1977d8123362004-06-12 09:25:12 +000087 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000088){
drh5708d2d2005-06-22 10:53:59 +000089 assert( n>=0 );
danielk1977d8123362004-06-12 09:25:12 +000090 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000091}
92void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
93 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
94}
95void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
96 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000097 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000098}
danielk1977a1686c92006-01-23 07:52:37 +000099#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000100void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
101 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +0000102 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000103}
danielk1977a1686c92006-01-23 07:52:37 +0000104#endif
drhf4479502004-05-27 03:12:53 +0000105void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000106 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
107}
108void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
109 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
110}
111void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000112 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000113}
114void sqlite3_result_text(
115 sqlite3_context *pCtx,
116 const char *z,
117 int n,
danielk1977d8123362004-06-12 09:25:12 +0000118 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000119){
danielk1977d8123362004-06-12 09:25:12 +0000120 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000121}
drh6c626082004-11-14 21:56:29 +0000122#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000123void sqlite3_result_text16(
124 sqlite3_context *pCtx,
125 const void *z,
126 int n,
danielk1977d8123362004-06-12 09:25:12 +0000127 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000128){
danielk1977d8123362004-06-12 09:25:12 +0000129 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
130}
131void sqlite3_result_text16be(
132 sqlite3_context *pCtx,
133 const void *z,
134 int n,
135 void (*xDel)(void *)
136){
137 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
138}
139void sqlite3_result_text16le(
140 sqlite3_context *pCtx,
141 const void *z,
142 int n,
143 void (*xDel)(void *)
144){
145 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000146}
drh6c626082004-11-14 21:56:29 +0000147#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000148void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
149 sqlite3VdbeMemCopy(&pCtx->s, pValue);
150}
151
152
153/*
154** Execute the statement pStmt, either until a row of data is ready, the
155** statement is completely executed or an error occurs.
156*/
157int sqlite3_step(sqlite3_stmt *pStmt){
158 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000159 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000160 int rc;
161
danielk1977efaaf572006-01-16 11:29:19 +0000162 /* Assert that malloc() has not failed */
danielk19779e128002006-01-18 16:51:35 +0000163 assert( !sqlite3MallocFailed() );
danielk1977261919c2005-12-06 12:52:59 +0000164
drh1bcdb0c2004-08-28 14:49:46 +0000165 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000166 return SQLITE_MISUSE;
167 }
drh91b48aa2004-06-30 11:14:18 +0000168 if( p->aborted ){
169 return SQLITE_ABORT;
170 }
danielk1977a21c6b62005-01-24 10:25:59 +0000171 if( p->pc<=0 && p->expired ){
172 if( p->rc==SQLITE_OK ){
173 p->rc = SQLITE_SCHEMA;
174 }
175 return SQLITE_ERROR;
176 }
drh4f26d6c2004-05-26 23:25:30 +0000177 db = p->db;
178 if( sqlite3SafetyOn(db) ){
179 p->rc = SQLITE_MISUSE;
180 return SQLITE_MISUSE;
181 }
danielk19771d850a72004-05-31 08:26:49 +0000182 if( p->pc<0 ){
drh15ca1df2006-07-26 13:43:30 +0000183 /* If there are no other statements currently running, then
184 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
185 ** from interrupting a statement that has not yet started.
186 */
187 if( db->activeVdbeCnt==0 ){
188 db->u1.isInterrupted = 0;
189 }
190
drh19e2d372005-08-29 23:00:03 +0000191#ifndef SQLITE_OMIT_TRACE
drhc16a03b2004-09-15 13:38:10 +0000192 /* Invoke the trace callback if there is one
193 */
drh19e2d372005-08-29 23:00:03 +0000194 if( db->xTrace && !db->init.busy ){
drhc16a03b2004-09-15 13:38:10 +0000195 assert( p->nOp>0 );
196 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
197 assert( p->aOp[p->nOp-1].p3!=0 );
198 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
199 sqlite3SafetyOff(db);
200 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
201 if( sqlite3SafetyOn(db) ){
202 p->rc = SQLITE_MISUSE;
203 return SQLITE_MISUSE;
204 }
205 }
drh19e2d372005-08-29 23:00:03 +0000206 if( db->xProfile && !db->init.busy ){
207 double rNow;
drh66560ad2006-01-06 14:32:19 +0000208 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000209 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
210 }
211#endif
drhc16a03b2004-09-15 13:38:10 +0000212
213 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
214 ** on in debugging mode.
215 */
216#ifdef SQLITE_DEBUG
217 if( (db->flags & SQLITE_SqlTrace)!=0 ){
218 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
219 }
220#endif /* SQLITE_DEBUG */
221
danielk19771d850a72004-05-31 08:26:49 +0000222 db->activeVdbeCnt++;
223 p->pc = 0;
224 }
drhb7f91642004-10-31 02:22:47 +0000225#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000226 if( p->explain ){
227 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000228 }else
229#endif /* SQLITE_OMIT_EXPLAIN */
230 {
drh4f26d6c2004-05-26 23:25:30 +0000231 rc = sqlite3VdbeExec(p);
232 }
233
234 if( sqlite3SafetyOff(db) ){
235 rc = SQLITE_MISUSE;
236 }
237
drh19e2d372005-08-29 23:00:03 +0000238#ifndef SQLITE_OMIT_TRACE
239 /* Invoke the profile callback if there is one
240 */
241 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
242 double rNow;
243 u64 elapseTime;
244
drh66560ad2006-01-06 14:32:19 +0000245 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000246 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
247 assert( p->nOp>0 );
248 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
249 assert( p->aOp[p->nOp-1].p3!=0 );
250 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
251 db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
252 }
253#endif
254
danielk197797a227c2006-01-20 16:32:04 +0000255 sqlite3Error(p->db, rc, 0);
danielk197776e8d1a2006-01-18 18:22:43 +0000256 p->rc = sqlite3ApiExit(p->db, p->rc);
drh4ac285a2006-09-15 07:28:50 +0000257 assert( (rc&0xff)==rc );
danielk197776e8d1a2006-01-18 18:22:43 +0000258 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000259}
260
261/*
drheb2e1762004-05-27 01:53:56 +0000262** Extract the user data from a sqlite3_context structure and return a
263** pointer to it.
264*/
265void *sqlite3_user_data(sqlite3_context *p){
266 assert( p && p->pFunc );
267 return p->pFunc->pUserData;
268}
269
270/*
drhb7481e72006-09-16 21:45:14 +0000271** The following is the implementation of an SQL function that always
272** fails with an error message stating that the function is used in the
273** wrong context. The sqlite3_overload_function() API might construct
274** SQL function that use this routine so that the functions will exist
275** for name resolution but are actually overloaded by the xFindFunction
276** method of virtual tables.
277*/
278void sqlite3InvalidFunction(
279 sqlite3_context *context, /* The function calling context */
280 int argc, /* Number of arguments to the function */
281 sqlite3_value **argv /* Value of each argument */
282){
283 const char *zName = context->pFunc->zName;
284 char *zErr;
285 zErr = sqlite3MPrintf(
286 "unable to use function %s in the requested context", zName);
287 sqlite3_result_error(context, zErr, -1);
288 sqliteFree(zErr);
289}
290
291/*
drheb2e1762004-05-27 01:53:56 +0000292** Allocate or return the aggregate context for a user function. A new
293** context is allocated on the first call. Subsequent calls return the
294** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000295*/
296void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhabfcea22005-09-06 20:36:48 +0000297 Mem *pMem = p->pMem;
drh825c6622005-09-08 19:01:05 +0000298 assert( p && p->pFunc && p->pFunc->xStep );
drha10a34b2005-09-07 22:09:48 +0000299 if( (pMem->flags & MEM_Agg)==0 ){
300 if( nByte==0 ){
301 assert( pMem->flags==MEM_Null );
302 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000303 }else{
drha10a34b2005-09-07 22:09:48 +0000304 pMem->flags = MEM_Agg;
drh5360ad32005-09-08 00:13:27 +0000305 pMem->xDel = sqlite3FreeX;
drha10a34b2005-09-07 22:09:48 +0000306 *(FuncDef**)&pMem->i = p->pFunc;
307 if( nByte<=NBFS ){
308 pMem->z = pMem->zShort;
309 memset(pMem->z, 0, nByte);
310 }else{
311 pMem->z = sqliteMalloc( nByte );
312 }
drheb2e1762004-05-27 01:53:56 +0000313 }
314 }
drhabfcea22005-09-06 20:36:48 +0000315 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000316}
317
318/*
danielk1977682f68b2004-06-05 10:22:17 +0000319** Return the auxilary data pointer, if any, for the iArg'th argument to
320** the user-function defined by pCtx.
321*/
322void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
323 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
324 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
325 return 0;
326 }
drhf92c7ff2004-06-19 15:40:23 +0000327 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000328}
329
330/*
331** Set the auxilary data pointer and delete function, for the iArg'th
332** argument to the user-function defined by pCtx. Any previous value is
333** deleted by calling the delete function specified when it was set.
334*/
335void sqlite3_set_auxdata(
336 sqlite3_context *pCtx,
337 int iArg,
338 void *pAux,
339 void (*xDelete)(void*)
340){
341 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000342 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000343 if( iArg<0 ) return;
344
drhf92c7ff2004-06-19 15:40:23 +0000345 pVdbeFunc = pCtx->pVdbeFunc;
346 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
347 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
drh53f733c2005-09-16 02:38:09 +0000348 pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000349 if( !pVdbeFunc ) return;
drh53f733c2005-09-16 02:38:09 +0000350 pCtx->pVdbeFunc = pVdbeFunc;
drh0e3d7472004-06-19 17:33:07 +0000351 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
352 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000353 pVdbeFunc->nAux = iArg+1;
354 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000355 }
356
drhf92c7ff2004-06-19 15:40:23 +0000357 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000358 if( pAuxData->pAux && pAuxData->xDelete ){
359 pAuxData->xDelete(pAuxData->pAux);
360 }
361 pAuxData->pAux = pAux;
362 pAuxData->xDelete = xDelete;
363}
364
365/*
drheb2e1762004-05-27 01:53:56 +0000366** Return the number of times the Step function of a aggregate has been
367** called.
368**
drhcf85a512006-02-09 18:35:29 +0000369** This function is deprecated. Do not use it for new code. It is
370** provide only to avoid breaking legacy code. New aggregate function
371** implementations should keep their own counts within their aggregate
372** context.
drheb2e1762004-05-27 01:53:56 +0000373*/
374int sqlite3_aggregate_count(sqlite3_context *p){
375 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000376 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000377}
378
379/*
drh4f26d6c2004-05-26 23:25:30 +0000380** Return the number of columns in the result set for the statement pStmt.
381*/
382int sqlite3_column_count(sqlite3_stmt *pStmt){
383 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000384 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000385}
386
387/*
388** Return the number of values available from the current row of the
389** currently executing statement pStmt.
390*/
391int sqlite3_data_count(sqlite3_stmt *pStmt){
392 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000393 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000394 return pVm->nResColumn;
395}
396
397
398/*
399** Check to see if column iCol of the given statement is valid. If
400** it is, return a pointer to the Mem for the value of that column.
401** If iCol is not valid, return a pointer to a Mem which has a value
402** of NULL.
403*/
404static Mem *columnMem(sqlite3_stmt *pStmt, int i){
405 Vdbe *pVm = (Vdbe *)pStmt;
406 int vals = sqlite3_data_count(pStmt);
407 if( i>=vals || i<0 ){
drh0f7eb612006-08-08 13:51:43 +0000408 static const Mem nullMem = {0, 0.0, "", 0, MEM_Null, MEM_Null };
drh4f26d6c2004-05-26 23:25:30 +0000409 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
drh0f7eb612006-08-08 13:51:43 +0000410 return (Mem*)&nullMem;
drh4f26d6c2004-05-26 23:25:30 +0000411 }
412 return &pVm->pTos[(1-vals)+i];
413}
414
danielk19772e588c72005-12-09 14:25:08 +0000415/*
416** This function is called after invoking an sqlite3_value_XXX function on a
417** column value (i.e. a value returned by evaluating an SQL expression in the
418** select list of a SELECT statement) that may cause a malloc() failure. If
419** malloc() has failed, the threads mallocFailed flag is cleared and the result
420** code of statement pStmt set to SQLITE_NOMEM.
421**
422** Specificly, this is called from within:
423**
424** sqlite3_column_int()
425** sqlite3_column_int64()
426** sqlite3_column_text()
427** sqlite3_column_text16()
428** sqlite3_column_real()
429** sqlite3_column_bytes()
430** sqlite3_column_bytes16()
431**
432** But not for sqlite3_column_blob(), which never calls malloc().
433*/
434static void columnMallocFailure(sqlite3_stmt *pStmt)
435{
436 /* If malloc() failed during an encoding conversion within an
437 ** sqlite3_column_XXX API, then set the return code of the statement to
438 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
439 ** and _finalize() will return NOMEM.
440 */
danielk197754f01982006-01-18 15:25:17 +0000441 Vdbe *p = (Vdbe *)pStmt;
442 p->rc = sqlite3ApiExit(0, p->rc);
danielk19772e588c72005-12-09 14:25:08 +0000443}
444
drh4f26d6c2004-05-26 23:25:30 +0000445/**************************** sqlite3_column_ *******************************
446** The following routines are used to access elements of the current row
447** in the result set.
448*/
danielk1977c572ef72004-05-27 09:28:41 +0000449const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000450 const void *val;
451 sqlite3MallocDisallow();
452 val = sqlite3_value_blob( columnMem(pStmt,i) );
453 sqlite3MallocAllow();
454 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000455}
drh4f26d6c2004-05-26 23:25:30 +0000456int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000457 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
458 columnMallocFailure(pStmt);
459 return val;
drh4f26d6c2004-05-26 23:25:30 +0000460}
461int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000462 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
463 columnMallocFailure(pStmt);
464 return val;
drh4f26d6c2004-05-26 23:25:30 +0000465}
466double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000467 double val = sqlite3_value_double( columnMem(pStmt,i) );
468 columnMallocFailure(pStmt);
469 return val;
drh4f26d6c2004-05-26 23:25:30 +0000470}
471int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000472 int val = sqlite3_value_int( columnMem(pStmt,i) );
473 columnMallocFailure(pStmt);
474 return val;
drh4f26d6c2004-05-26 23:25:30 +0000475}
drhefad9992004-06-22 12:13:55 +0000476sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000477 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
478 columnMallocFailure(pStmt);
479 return val;
drh4f26d6c2004-05-26 23:25:30 +0000480}
481const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000482 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
483 columnMallocFailure(pStmt);
484 return val;
drh4f26d6c2004-05-26 23:25:30 +0000485}
drhd1e47332005-06-26 17:55:33 +0000486sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
487 return columnMem(pStmt, i);
488}
drh6c626082004-11-14 21:56:29 +0000489#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000490const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000491 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
492 columnMallocFailure(pStmt);
493 return val;
drh4f26d6c2004-05-26 23:25:30 +0000494}
drh6c626082004-11-14 21:56:29 +0000495#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000496int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
497 return sqlite3_value_type( columnMem(pStmt,i) );
498}
499
drh29d72102006-02-09 22:13:41 +0000500/* The following function is experimental and subject to change or
501** removal */
502/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
503** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
504**}
505*/
506
drh5e2517e2004-09-24 23:59:12 +0000507/*
508** Convert the N-th element of pStmt->pColName[] into a string using
509** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000510**
511** There are up to 5 names for each column. useType determines which
512** name is returned. Here are the names:
513**
514** 0 The column name as it should be displayed for output
515** 1 The datatype name for the column
516** 2 The name of the database that the column derives from
517** 3 The name of the table that the column derives from
518** 4 The name of the table column that the result column derives from
519**
520** If the result is not a simple column reference (if it is an expression
521** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000522*/
523static const void *columnName(
524 sqlite3_stmt *pStmt,
525 int N,
526 const void *(*xFunc)(Mem*),
527 int useType
528){
danielk197700fd9572005-12-07 06:27:43 +0000529 const void *ret;
drh5e2517e2004-09-24 23:59:12 +0000530 Vdbe *p = (Vdbe *)pStmt;
531 int n = sqlite3_column_count(pStmt);
532
533 if( p==0 || N>=n || N<0 ){
534 return 0;
535 }
drhe590fbd2005-05-20 19:36:01 +0000536 N += useType*n;
danielk197700fd9572005-12-07 06:27:43 +0000537 ret = xFunc(&p->aColName[N]);
538
539 /* A malloc may have failed inside of the xFunc() call. If this is the case,
540 ** clear the mallocFailed flag and return NULL.
541 */
danielk197754f01982006-01-18 15:25:17 +0000542 sqlite3ApiExit(0, 0);
danielk197700fd9572005-12-07 06:27:43 +0000543 return ret;
drh5e2517e2004-09-24 23:59:12 +0000544}
545
drh4f26d6c2004-05-26 23:25:30 +0000546/*
547** Return the name of the Nth column of the result set returned by SQL
548** statement pStmt.
549*/
550const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000551 return columnName(
552 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000553}
drhe590fbd2005-05-20 19:36:01 +0000554#ifndef SQLITE_OMIT_UTF16
555const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000556 return columnName(
557 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000558}
559#endif
drh4f26d6c2004-05-26 23:25:30 +0000560
561/*
drh6c626082004-11-14 21:56:29 +0000562** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000563** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000564*/
565const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000566 return columnName(
567 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000568}
drh6c626082004-11-14 21:56:29 +0000569#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000570const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000571 return columnName(
572 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000573}
drh6c626082004-11-14 21:56:29 +0000574#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000575
danielk19774b1ae992006-02-10 03:06:10 +0000576#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000577/*
578** Return the name of the database from which a result column derives.
579** NULL is returned if the result column is an expression or constant or
580** anything else which is not an unabiguous reference to a database column.
581*/
582const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000583 return columnName(
584 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000585}
586#ifndef SQLITE_OMIT_UTF16
587const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000588 return columnName(
589 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000590}
591#endif /* SQLITE_OMIT_UTF16 */
592
593/*
594** Return the name of the table from which a result column derives.
595** NULL is returned if the result column is an expression or constant or
596** anything else which is not an unabiguous reference to a database column.
597*/
598const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000599 return columnName(
600 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000601}
602#ifndef SQLITE_OMIT_UTF16
603const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000604 return columnName(
605 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000606}
607#endif /* SQLITE_OMIT_UTF16 */
608
609/*
610** Return the name of the table column from which a result column derives.
611** NULL is returned if the result column is an expression or constant or
612** anything else which is not an unabiguous reference to a database column.
613*/
614const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000615 return columnName(
616 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000617}
618#ifndef SQLITE_OMIT_UTF16
619const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000620 return columnName(
621 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000622}
623#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000624#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000625
626
drh4f26d6c2004-05-26 23:25:30 +0000627/******************************* sqlite3_bind_ ***************************
628**
629** Routines used to attach values to wildcards in a compiled SQL statement.
630*/
631/*
632** Unbind the value bound to variable i in virtual machine p. This is the
633** the same as binding a NULL value to the column. If the "i" parameter is
634** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
635**
636** The error code stored in database p->db is overwritten with the return
637** value in any case.
638*/
639static int vdbeUnbind(Vdbe *p, int i){
640 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000641 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +0000642 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +0000643 return SQLITE_MISUSE;
644 }
645 if( i<1 || i>p->nVar ){
646 sqlite3Error(p->db, SQLITE_RANGE, 0);
647 return SQLITE_RANGE;
648 }
649 i--;
drh290c1942004-08-21 17:54:45 +0000650 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000651 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000652 pVar->flags = MEM_Null;
653 sqlite3Error(p->db, SQLITE_OK, 0);
654 return SQLITE_OK;
655}
656
657/*
drh5e2517e2004-09-24 23:59:12 +0000658** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000659*/
drh5e2517e2004-09-24 23:59:12 +0000660static int bindText(
drhf4479502004-05-27 03:12:53 +0000661 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000662 int i,
663 const void *zData,
664 int nData,
drh5e2517e2004-09-24 23:59:12 +0000665 void (*xDel)(void*),
666 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000667){
668 Vdbe *p = (Vdbe *)pStmt;
669 Mem *pVar;
670 int rc;
671
672 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000673 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000674 return rc;
675 }
drh290c1942004-08-21 17:54:45 +0000676 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000677 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
drh5e2517e2004-09-24 23:59:12 +0000678 if( rc==SQLITE_OK && encoding!=0 ){
danielk197714db2662006-01-09 16:12:04 +0000679 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
drh5e2517e2004-09-24 23:59:12 +0000680 }
danielk197754f01982006-01-18 15:25:17 +0000681
danielk1977771151b2006-01-17 13:21:40 +0000682 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
danielk197754f01982006-01-18 15:25:17 +0000683 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
drh4f26d6c2004-05-26 23:25:30 +0000684}
drh5e2517e2004-09-24 23:59:12 +0000685
686
687/*
688** Bind a blob value to an SQL statement variable.
689*/
690int sqlite3_bind_blob(
691 sqlite3_stmt *pStmt,
692 int i,
693 const void *zData,
694 int nData,
695 void (*xDel)(void*)
696){
697 return bindText(pStmt, i, zData, nData, xDel, 0);
698}
drh4f26d6c2004-05-26 23:25:30 +0000699int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
700 int rc;
701 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000702 rc = vdbeUnbind(p, i);
703 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000704 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000705 }
danielk1977f4618892004-06-28 13:09:11 +0000706 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000707}
708int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000709 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000710}
drhefad9992004-06-22 12:13:55 +0000711int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000712 int rc;
713 Vdbe *p = (Vdbe *)pStmt;
714 rc = vdbeUnbind(p, i);
715 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000716 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000717 }
718 return rc;
719}
720int sqlite3_bind_null(sqlite3_stmt* p, int i){
721 return vdbeUnbind((Vdbe *)p, i);
722}
723int sqlite3_bind_text(
724 sqlite3_stmt *pStmt,
725 int i,
726 const char *zData,
727 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000728 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000729){
drh5e2517e2004-09-24 23:59:12 +0000730 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000731}
drh6c626082004-11-14 21:56:29 +0000732#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000733int sqlite3_bind_text16(
734 sqlite3_stmt *pStmt,
735 int i,
736 const void *zData,
737 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000738 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000739){
drh5e2517e2004-09-24 23:59:12 +0000740 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000741}
drh6c626082004-11-14 21:56:29 +0000742#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +0000743int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
744 int rc;
745 Vdbe *p = (Vdbe *)pStmt;
746 rc = vdbeUnbind(p, i);
747 if( rc==SQLITE_OK ){
748 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
749 }
750 return rc;
751}
drh75f6a032004-07-15 14:15:00 +0000752
753/*
754** Return the number of wildcards that can be potentially bound to.
755** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000756*/
757int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000758 Vdbe *p = (Vdbe*)pStmt;
759 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000760}
drh895d7472004-08-20 16:02:39 +0000761
762/*
drhfa6bc002004-09-07 16:19:52 +0000763** Create a mapping from variable numbers to variable names
764** in the Vdbe.azVar[] array, if such a mapping does not already
765** exist.
drh895d7472004-08-20 16:02:39 +0000766*/
drhfa6bc002004-09-07 16:19:52 +0000767static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000768 if( !p->okVar ){
769 int j;
770 Op *pOp;
771 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
772 if( pOp->opcode==OP_Variable ){
773 assert( pOp->p1>0 && pOp->p1<=p->nVar );
774 p->azVar[pOp->p1-1] = pOp->p3;
775 }
776 }
777 p->okVar = 1;
778 }
drhfa6bc002004-09-07 16:19:52 +0000779}
780
781/*
782** Return the name of a wildcard parameter. Return NULL if the index
783** is out of range or if the wildcard is unnamed.
784**
785** The result is always UTF-8.
786*/
787const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
788 Vdbe *p = (Vdbe*)pStmt;
789 if( p==0 || i<1 || i>p->nVar ){
790 return 0;
791 }
792 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000793 return p->azVar[i-1];
794}
drhfa6bc002004-09-07 16:19:52 +0000795
796/*
797** Given a wildcard parameter name, return the index of the variable
798** with that name. If there is no variable with the given name,
799** return 0.
800*/
801int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
802 Vdbe *p = (Vdbe*)pStmt;
803 int i;
804 if( p==0 ){
805 return 0;
806 }
807 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000808 if( zName ){
809 for(i=0; i<p->nVar; i++){
810 const char *z = p->azVar[i];
811 if( z && strcmp(z,zName)==0 ){
812 return i+1;
813 }
drhfa6bc002004-09-07 16:19:52 +0000814 }
815 }
816 return 0;
817}
drhf8db1bc2005-04-22 02:38:37 +0000818
drh51942bc2005-06-12 22:01:42 +0000819/*
drhf8db1bc2005-04-22 02:38:37 +0000820** Transfer all bindings from the first statement over to the second.
821** If the two statements contain a different number of bindings, then
822** an SQLITE_ERROR is returned.
823*/
824int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
825 Vdbe *pFrom = (Vdbe*)pFromStmt;
826 Vdbe *pTo = (Vdbe*)pToStmt;
827 int i, rc = SQLITE_OK;
828 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
829 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
830 return SQLITE_MISUSE;
831 }
832 if( pFrom->nVar!=pTo->nVar ){
833 return SQLITE_ERROR;
834 }
835 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
danielk1977950f0542006-01-18 05:51:57 +0000836 sqlite3MallocDisallow();
drhf8db1bc2005-04-22 02:38:37 +0000837 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
danielk1977950f0542006-01-18 05:51:57 +0000838 sqlite3MallocAllow();
drhf8db1bc2005-04-22 02:38:37 +0000839 }
drh4ac285a2006-09-15 07:28:50 +0000840 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
drhf8db1bc2005-04-22 02:38:37 +0000841 return rc;
842}
drh51942bc2005-06-12 22:01:42 +0000843
844/*
845** Return the sqlite3* database handle to which the prepared statement given
846** in the argument belongs. This is the same database handle that was
847** the first argument to the sqlite3_prepare() that was used to create
848** the statement in the first place.
849*/
850sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
851 return pStmt ? ((Vdbe*)pStmt)->db : 0;
852}