blob: e2866975a7c016881d790cc42415a9e94086636e [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 ){
drh19e2d372005-08-29 23:00:03 +0000183#ifndef SQLITE_OMIT_TRACE
drhc16a03b2004-09-15 13:38:10 +0000184 /* Invoke the trace callback if there is one
185 */
drh19e2d372005-08-29 23:00:03 +0000186 if( db->xTrace && !db->init.busy ){
drhc16a03b2004-09-15 13:38:10 +0000187 assert( p->nOp>0 );
188 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
189 assert( p->aOp[p->nOp-1].p3!=0 );
190 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
191 sqlite3SafetyOff(db);
192 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
193 if( sqlite3SafetyOn(db) ){
194 p->rc = SQLITE_MISUSE;
195 return SQLITE_MISUSE;
196 }
197 }
drh19e2d372005-08-29 23:00:03 +0000198 if( db->xProfile && !db->init.busy ){
199 double rNow;
drh66560ad2006-01-06 14:32:19 +0000200 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000201 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
202 }
203#endif
drhc16a03b2004-09-15 13:38:10 +0000204
205 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
206 ** on in debugging mode.
207 */
208#ifdef SQLITE_DEBUG
209 if( (db->flags & SQLITE_SqlTrace)!=0 ){
210 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
211 }
212#endif /* SQLITE_DEBUG */
213
danielk19771d850a72004-05-31 08:26:49 +0000214 db->activeVdbeCnt++;
215 p->pc = 0;
216 }
drhb7f91642004-10-31 02:22:47 +0000217#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000218 if( p->explain ){
219 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000220 }else
221#endif /* SQLITE_OMIT_EXPLAIN */
222 {
drh4f26d6c2004-05-26 23:25:30 +0000223 rc = sqlite3VdbeExec(p);
224 }
225
226 if( sqlite3SafetyOff(db) ){
227 rc = SQLITE_MISUSE;
228 }
229
drh19e2d372005-08-29 23:00:03 +0000230#ifndef SQLITE_OMIT_TRACE
231 /* Invoke the profile callback if there is one
232 */
233 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
234 double rNow;
235 u64 elapseTime;
236
drh66560ad2006-01-06 14:32:19 +0000237 sqlite3OsCurrentTime(&rNow);
drh19e2d372005-08-29 23:00:03 +0000238 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
239 assert( p->nOp>0 );
240 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
241 assert( p->aOp[p->nOp-1].p3!=0 );
242 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
243 db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
244 }
245#endif
246
danielk197797a227c2006-01-20 16:32:04 +0000247 sqlite3Error(p->db, rc, 0);
danielk197776e8d1a2006-01-18 18:22:43 +0000248 p->rc = sqlite3ApiExit(p->db, p->rc);
249 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000250}
251
252/*
drheb2e1762004-05-27 01:53:56 +0000253** Extract the user data from a sqlite3_context structure and return a
254** pointer to it.
255*/
256void *sqlite3_user_data(sqlite3_context *p){
257 assert( p && p->pFunc );
258 return p->pFunc->pUserData;
259}
260
261/*
262** Allocate or return the aggregate context for a user function. A new
263** context is allocated on the first call. Subsequent calls return the
264** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000265*/
266void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhabfcea22005-09-06 20:36:48 +0000267 Mem *pMem = p->pMem;
drh825c6622005-09-08 19:01:05 +0000268 assert( p && p->pFunc && p->pFunc->xStep );
drha10a34b2005-09-07 22:09:48 +0000269 if( (pMem->flags & MEM_Agg)==0 ){
270 if( nByte==0 ){
271 assert( pMem->flags==MEM_Null );
272 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000273 }else{
drha10a34b2005-09-07 22:09:48 +0000274 pMem->flags = MEM_Agg;
drh5360ad32005-09-08 00:13:27 +0000275 pMem->xDel = sqlite3FreeX;
drha10a34b2005-09-07 22:09:48 +0000276 *(FuncDef**)&pMem->i = p->pFunc;
277 if( nByte<=NBFS ){
278 pMem->z = pMem->zShort;
279 memset(pMem->z, 0, nByte);
280 }else{
281 pMem->z = sqliteMalloc( nByte );
282 }
drheb2e1762004-05-27 01:53:56 +0000283 }
284 }
drhabfcea22005-09-06 20:36:48 +0000285 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000286}
287
288/*
danielk1977682f68b2004-06-05 10:22:17 +0000289** Return the auxilary data pointer, if any, for the iArg'th argument to
290** the user-function defined by pCtx.
291*/
292void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
293 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
294 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
295 return 0;
296 }
drhf92c7ff2004-06-19 15:40:23 +0000297 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000298}
299
300/*
301** Set the auxilary data pointer and delete function, for the iArg'th
302** argument to the user-function defined by pCtx. Any previous value is
303** deleted by calling the delete function specified when it was set.
304*/
305void sqlite3_set_auxdata(
306 sqlite3_context *pCtx,
307 int iArg,
308 void *pAux,
309 void (*xDelete)(void*)
310){
311 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000312 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000313 if( iArg<0 ) return;
314
drhf92c7ff2004-06-19 15:40:23 +0000315 pVdbeFunc = pCtx->pVdbeFunc;
316 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
317 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
drh53f733c2005-09-16 02:38:09 +0000318 pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000319 if( !pVdbeFunc ) return;
drh53f733c2005-09-16 02:38:09 +0000320 pCtx->pVdbeFunc = pVdbeFunc;
drh0e3d7472004-06-19 17:33:07 +0000321 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
322 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000323 pVdbeFunc->nAux = iArg+1;
324 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000325 }
326
drhf92c7ff2004-06-19 15:40:23 +0000327 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000328 if( pAuxData->pAux && pAuxData->xDelete ){
329 pAuxData->xDelete(pAuxData->pAux);
330 }
331 pAuxData->pAux = pAux;
332 pAuxData->xDelete = xDelete;
333}
334
335/*
drheb2e1762004-05-27 01:53:56 +0000336** Return the number of times the Step function of a aggregate has been
337** called.
338**
drhcf85a512006-02-09 18:35:29 +0000339** This function is deprecated. Do not use it for new code. It is
340** provide only to avoid breaking legacy code. New aggregate function
341** implementations should keep their own counts within their aggregate
342** context.
drheb2e1762004-05-27 01:53:56 +0000343*/
344int sqlite3_aggregate_count(sqlite3_context *p){
345 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000346 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000347}
348
349/*
drh4f26d6c2004-05-26 23:25:30 +0000350** Return the number of columns in the result set for the statement pStmt.
351*/
352int sqlite3_column_count(sqlite3_stmt *pStmt){
353 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000354 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000355}
356
357/*
358** Return the number of values available from the current row of the
359** currently executing statement pStmt.
360*/
361int sqlite3_data_count(sqlite3_stmt *pStmt){
362 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000363 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000364 return pVm->nResColumn;
365}
366
367
368/*
369** Check to see if column iCol of the given statement is valid. If
370** it is, return a pointer to the Mem for the value of that column.
371** If iCol is not valid, return a pointer to a Mem which has a value
372** of NULL.
373*/
374static Mem *columnMem(sqlite3_stmt *pStmt, int i){
375 Vdbe *pVm = (Vdbe *)pStmt;
376 int vals = sqlite3_data_count(pStmt);
377 if( i>=vals || i<0 ){
378 static Mem nullMem;
379 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
380 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
381 return &nullMem;
382 }
383 return &pVm->pTos[(1-vals)+i];
384}
385
danielk19772e588c72005-12-09 14:25:08 +0000386/*
387** This function is called after invoking an sqlite3_value_XXX function on a
388** column value (i.e. a value returned by evaluating an SQL expression in the
389** select list of a SELECT statement) that may cause a malloc() failure. If
390** malloc() has failed, the threads mallocFailed flag is cleared and the result
391** code of statement pStmt set to SQLITE_NOMEM.
392**
393** Specificly, this is called from within:
394**
395** sqlite3_column_int()
396** sqlite3_column_int64()
397** sqlite3_column_text()
398** sqlite3_column_text16()
399** sqlite3_column_real()
400** sqlite3_column_bytes()
401** sqlite3_column_bytes16()
402**
403** But not for sqlite3_column_blob(), which never calls malloc().
404*/
405static void columnMallocFailure(sqlite3_stmt *pStmt)
406{
407 /* If malloc() failed during an encoding conversion within an
408 ** sqlite3_column_XXX API, then set the return code of the statement to
409 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
410 ** and _finalize() will return NOMEM.
411 */
danielk197754f01982006-01-18 15:25:17 +0000412 Vdbe *p = (Vdbe *)pStmt;
413 p->rc = sqlite3ApiExit(0, p->rc);
danielk19772e588c72005-12-09 14:25:08 +0000414}
415
drh4f26d6c2004-05-26 23:25:30 +0000416/**************************** sqlite3_column_ *******************************
417** The following routines are used to access elements of the current row
418** in the result set.
419*/
danielk1977c572ef72004-05-27 09:28:41 +0000420const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000421 const void *val;
422 sqlite3MallocDisallow();
423 val = sqlite3_value_blob( columnMem(pStmt,i) );
424 sqlite3MallocAllow();
425 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000426}
drh4f26d6c2004-05-26 23:25:30 +0000427int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000428 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
429 columnMallocFailure(pStmt);
430 return val;
drh4f26d6c2004-05-26 23:25:30 +0000431}
432int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000433 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
434 columnMallocFailure(pStmt);
435 return val;
drh4f26d6c2004-05-26 23:25:30 +0000436}
437double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000438 double val = sqlite3_value_double( columnMem(pStmt,i) );
439 columnMallocFailure(pStmt);
440 return val;
drh4f26d6c2004-05-26 23:25:30 +0000441}
442int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000443 int val = sqlite3_value_int( columnMem(pStmt,i) );
444 columnMallocFailure(pStmt);
445 return val;
drh4f26d6c2004-05-26 23:25:30 +0000446}
drhefad9992004-06-22 12:13:55 +0000447sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000448 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
449 columnMallocFailure(pStmt);
450 return val;
drh4f26d6c2004-05-26 23:25:30 +0000451}
452const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000453 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
454 columnMallocFailure(pStmt);
455 return val;
drh4f26d6c2004-05-26 23:25:30 +0000456}
drhd1e47332005-06-26 17:55:33 +0000457#if 0
458sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
459 return columnMem(pStmt, i);
460}
461#endif
drh6c626082004-11-14 21:56:29 +0000462#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000463const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000464 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
465 columnMallocFailure(pStmt);
466 return val;
drh4f26d6c2004-05-26 23:25:30 +0000467}
drh6c626082004-11-14 21:56:29 +0000468#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000469int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
470 return sqlite3_value_type( columnMem(pStmt,i) );
471}
472
drh29d72102006-02-09 22:13:41 +0000473/* The following function is experimental and subject to change or
474** removal */
475/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
476** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
477**}
478*/
479
drh5e2517e2004-09-24 23:59:12 +0000480/*
481** Convert the N-th element of pStmt->pColName[] into a string using
482** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000483**
484** There are up to 5 names for each column. useType determines which
485** name is returned. Here are the names:
486**
487** 0 The column name as it should be displayed for output
488** 1 The datatype name for the column
489** 2 The name of the database that the column derives from
490** 3 The name of the table that the column derives from
491** 4 The name of the table column that the result column derives from
492**
493** If the result is not a simple column reference (if it is an expression
494** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000495*/
496static const void *columnName(
497 sqlite3_stmt *pStmt,
498 int N,
499 const void *(*xFunc)(Mem*),
500 int useType
501){
danielk197700fd9572005-12-07 06:27:43 +0000502 const void *ret;
drh5e2517e2004-09-24 23:59:12 +0000503 Vdbe *p = (Vdbe *)pStmt;
504 int n = sqlite3_column_count(pStmt);
505
506 if( p==0 || N>=n || N<0 ){
507 return 0;
508 }
drhe590fbd2005-05-20 19:36:01 +0000509 N += useType*n;
danielk197700fd9572005-12-07 06:27:43 +0000510 ret = xFunc(&p->aColName[N]);
511
512 /* A malloc may have failed inside of the xFunc() call. If this is the case,
513 ** clear the mallocFailed flag and return NULL.
514 */
danielk197754f01982006-01-18 15:25:17 +0000515 sqlite3ApiExit(0, 0);
danielk197700fd9572005-12-07 06:27:43 +0000516 return ret;
drh5e2517e2004-09-24 23:59:12 +0000517}
518
drh4f26d6c2004-05-26 23:25:30 +0000519/*
520** Return the name of the Nth column of the result set returned by SQL
521** statement pStmt.
522*/
523const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000524 return columnName(
525 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000526}
drhe590fbd2005-05-20 19:36:01 +0000527#ifndef SQLITE_OMIT_UTF16
528const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000529 return columnName(
530 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000531}
532#endif
drh4f26d6c2004-05-26 23:25:30 +0000533
534/*
drh6c626082004-11-14 21:56:29 +0000535** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000536** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000537*/
538const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000539 return columnName(
540 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000541}
drh6c626082004-11-14 21:56:29 +0000542#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000543const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000544 return columnName(
545 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000546}
drh6c626082004-11-14 21:56:29 +0000547#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000548
danielk1977955de522006-02-10 02:27:42 +0000549#if !defined(SQLITE_OMIT_ORIGIN_NAMES)
drhe590fbd2005-05-20 19:36:01 +0000550/*
551** Return the name of the database from which a result column derives.
552** NULL is returned if the result column is an expression or constant or
553** anything else which is not an unabiguous reference to a database column.
554*/
555const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000556 return columnName(
557 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000558}
559#ifndef SQLITE_OMIT_UTF16
560const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000561 return columnName(
562 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000563}
564#endif /* SQLITE_OMIT_UTF16 */
565
566/*
567** Return the name of the table from which a result column derives.
568** NULL is returned if the result column is an expression or constant or
569** anything else which is not an unabiguous reference to a database column.
570*/
571const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000572 return columnName(
573 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000574}
575#ifndef SQLITE_OMIT_UTF16
576const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000577 return columnName(
578 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000579}
580#endif /* SQLITE_OMIT_UTF16 */
581
582/*
583** Return the name of the table column from which a result column derives.
584** NULL is returned if the result column is an expression or constant or
585** anything else which is not an unabiguous reference to a database column.
586*/
587const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000588 return columnName(
589 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000590}
591#ifndef SQLITE_OMIT_UTF16
592const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000593 return columnName(
594 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000595}
596#endif /* SQLITE_OMIT_UTF16 */
597#endif /* SQLITE_OMIT_ORIGIN_NAMES */
598
599
600
601
drh4f26d6c2004-05-26 23:25:30 +0000602/******************************* sqlite3_bind_ ***************************
603**
604** Routines used to attach values to wildcards in a compiled SQL statement.
605*/
606/*
607** Unbind the value bound to variable i in virtual machine p. This is the
608** the same as binding a NULL value to the column. If the "i" parameter is
609** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
610**
611** The error code stored in database p->db is overwritten with the return
612** value in any case.
613*/
614static int vdbeUnbind(Vdbe *p, int i){
615 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000616 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +0000617 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +0000618 return SQLITE_MISUSE;
619 }
620 if( i<1 || i>p->nVar ){
621 sqlite3Error(p->db, SQLITE_RANGE, 0);
622 return SQLITE_RANGE;
623 }
624 i--;
drh290c1942004-08-21 17:54:45 +0000625 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000626 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000627 pVar->flags = MEM_Null;
628 sqlite3Error(p->db, SQLITE_OK, 0);
629 return SQLITE_OK;
630}
631
632/*
drh5e2517e2004-09-24 23:59:12 +0000633** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000634*/
drh5e2517e2004-09-24 23:59:12 +0000635static int bindText(
drhf4479502004-05-27 03:12:53 +0000636 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000637 int i,
638 const void *zData,
639 int nData,
drh5e2517e2004-09-24 23:59:12 +0000640 void (*xDel)(void*),
641 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000642){
643 Vdbe *p = (Vdbe *)pStmt;
644 Mem *pVar;
645 int rc;
646
647 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000648 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000649 return rc;
650 }
drh290c1942004-08-21 17:54:45 +0000651 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000652 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
drh5e2517e2004-09-24 23:59:12 +0000653 if( rc==SQLITE_OK && encoding!=0 ){
danielk197714db2662006-01-09 16:12:04 +0000654 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
drh5e2517e2004-09-24 23:59:12 +0000655 }
danielk197754f01982006-01-18 15:25:17 +0000656
danielk1977771151b2006-01-17 13:21:40 +0000657 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
danielk197754f01982006-01-18 15:25:17 +0000658 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
drh4f26d6c2004-05-26 23:25:30 +0000659}
drh5e2517e2004-09-24 23:59:12 +0000660
661
662/*
663** Bind a blob value to an SQL statement variable.
664*/
665int sqlite3_bind_blob(
666 sqlite3_stmt *pStmt,
667 int i,
668 const void *zData,
669 int nData,
670 void (*xDel)(void*)
671){
672 return bindText(pStmt, i, zData, nData, xDel, 0);
673}
drh4f26d6c2004-05-26 23:25:30 +0000674int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
675 int rc;
676 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000677 rc = vdbeUnbind(p, i);
678 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000679 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000680 }
danielk1977f4618892004-06-28 13:09:11 +0000681 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000682}
683int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000684 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000685}
drhefad9992004-06-22 12:13:55 +0000686int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000687 int rc;
688 Vdbe *p = (Vdbe *)pStmt;
689 rc = vdbeUnbind(p, i);
690 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000691 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000692 }
693 return rc;
694}
695int sqlite3_bind_null(sqlite3_stmt* p, int i){
696 return vdbeUnbind((Vdbe *)p, i);
697}
698int sqlite3_bind_text(
699 sqlite3_stmt *pStmt,
700 int i,
701 const char *zData,
702 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000703 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000704){
drh5e2517e2004-09-24 23:59:12 +0000705 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000706}
drh6c626082004-11-14 21:56:29 +0000707#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000708int sqlite3_bind_text16(
709 sqlite3_stmt *pStmt,
710 int i,
711 const void *zData,
712 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000713 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000714){
drh5e2517e2004-09-24 23:59:12 +0000715 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000716}
drh6c626082004-11-14 21:56:29 +0000717#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000718
719/*
720** Return the number of wildcards that can be potentially bound to.
721** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000722*/
723int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000724 Vdbe *p = (Vdbe*)pStmt;
725 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000726}
drh895d7472004-08-20 16:02:39 +0000727
728/*
drhfa6bc002004-09-07 16:19:52 +0000729** Create a mapping from variable numbers to variable names
730** in the Vdbe.azVar[] array, if such a mapping does not already
731** exist.
drh895d7472004-08-20 16:02:39 +0000732*/
drhfa6bc002004-09-07 16:19:52 +0000733static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000734 if( !p->okVar ){
735 int j;
736 Op *pOp;
737 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
738 if( pOp->opcode==OP_Variable ){
739 assert( pOp->p1>0 && pOp->p1<=p->nVar );
740 p->azVar[pOp->p1-1] = pOp->p3;
741 }
742 }
743 p->okVar = 1;
744 }
drhfa6bc002004-09-07 16:19:52 +0000745}
746
747/*
748** Return the name of a wildcard parameter. Return NULL if the index
749** is out of range or if the wildcard is unnamed.
750**
751** The result is always UTF-8.
752*/
753const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
754 Vdbe *p = (Vdbe*)pStmt;
755 if( p==0 || i<1 || i>p->nVar ){
756 return 0;
757 }
758 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000759 return p->azVar[i-1];
760}
drhfa6bc002004-09-07 16:19:52 +0000761
762/*
763** Given a wildcard parameter name, return the index of the variable
764** with that name. If there is no variable with the given name,
765** return 0.
766*/
767int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
768 Vdbe *p = (Vdbe*)pStmt;
769 int i;
770 if( p==0 ){
771 return 0;
772 }
773 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000774 if( zName ){
775 for(i=0; i<p->nVar; i++){
776 const char *z = p->azVar[i];
777 if( z && strcmp(z,zName)==0 ){
778 return i+1;
779 }
drhfa6bc002004-09-07 16:19:52 +0000780 }
781 }
782 return 0;
783}
drhf8db1bc2005-04-22 02:38:37 +0000784
drh51942bc2005-06-12 22:01:42 +0000785/*
drhf8db1bc2005-04-22 02:38:37 +0000786** Transfer all bindings from the first statement over to the second.
787** If the two statements contain a different number of bindings, then
788** an SQLITE_ERROR is returned.
789*/
790int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
791 Vdbe *pFrom = (Vdbe*)pFromStmt;
792 Vdbe *pTo = (Vdbe*)pToStmt;
793 int i, rc = SQLITE_OK;
794 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
795 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
796 return SQLITE_MISUSE;
797 }
798 if( pFrom->nVar!=pTo->nVar ){
799 return SQLITE_ERROR;
800 }
801 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
danielk1977950f0542006-01-18 05:51:57 +0000802 sqlite3MallocDisallow();
drhf8db1bc2005-04-22 02:38:37 +0000803 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
danielk1977950f0542006-01-18 05:51:57 +0000804 sqlite3MallocAllow();
drhf8db1bc2005-04-22 02:38:37 +0000805 }
806 return rc;
807}
drh51942bc2005-06-12 22:01:42 +0000808
809/*
810** Return the sqlite3* database handle to which the prepared statement given
811** in the argument belongs. This is the same database handle that was
812** the first argument to the sqlite3_prepare() that was used to create
813** the statement in the first place.
814*/
815sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
816 return pStmt ? ((Vdbe*)pStmt)->db : 0;
817}