blob: a57cff3f8b7ad23bd63ef61a88beab97f19ee5ab [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);
257 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000258}
259
260/*
drheb2e1762004-05-27 01:53:56 +0000261** Extract the user data from a sqlite3_context structure and return a
262** pointer to it.
263*/
264void *sqlite3_user_data(sqlite3_context *p){
265 assert( p && p->pFunc );
266 return p->pFunc->pUserData;
267}
268
269/*
270** Allocate or return the aggregate context for a user function. A new
271** context is allocated on the first call. Subsequent calls return the
272** same context that was returned on prior calls.
drheb2e1762004-05-27 01:53:56 +0000273*/
274void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
drhabfcea22005-09-06 20:36:48 +0000275 Mem *pMem = p->pMem;
drh825c6622005-09-08 19:01:05 +0000276 assert( p && p->pFunc && p->pFunc->xStep );
drha10a34b2005-09-07 22:09:48 +0000277 if( (pMem->flags & MEM_Agg)==0 ){
278 if( nByte==0 ){
279 assert( pMem->flags==MEM_Null );
280 pMem->z = 0;
drheb2e1762004-05-27 01:53:56 +0000281 }else{
drha10a34b2005-09-07 22:09:48 +0000282 pMem->flags = MEM_Agg;
drh5360ad32005-09-08 00:13:27 +0000283 pMem->xDel = sqlite3FreeX;
drha10a34b2005-09-07 22:09:48 +0000284 *(FuncDef**)&pMem->i = p->pFunc;
285 if( nByte<=NBFS ){
286 pMem->z = pMem->zShort;
287 memset(pMem->z, 0, nByte);
288 }else{
289 pMem->z = sqliteMalloc( nByte );
290 }
drheb2e1762004-05-27 01:53:56 +0000291 }
292 }
drhabfcea22005-09-06 20:36:48 +0000293 return (void*)pMem->z;
drheb2e1762004-05-27 01:53:56 +0000294}
295
296/*
danielk1977682f68b2004-06-05 10:22:17 +0000297** Return the auxilary data pointer, if any, for the iArg'th argument to
298** the user-function defined by pCtx.
299*/
300void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
301 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
302 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
303 return 0;
304 }
drhf92c7ff2004-06-19 15:40:23 +0000305 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000306}
307
308/*
309** Set the auxilary data pointer and delete function, for the iArg'th
310** argument to the user-function defined by pCtx. Any previous value is
311** deleted by calling the delete function specified when it was set.
312*/
313void sqlite3_set_auxdata(
314 sqlite3_context *pCtx,
315 int iArg,
316 void *pAux,
317 void (*xDelete)(void*)
318){
319 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000320 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000321 if( iArg<0 ) return;
322
drhf92c7ff2004-06-19 15:40:23 +0000323 pVdbeFunc = pCtx->pVdbeFunc;
324 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
325 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
drh53f733c2005-09-16 02:38:09 +0000326 pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000327 if( !pVdbeFunc ) return;
drh53f733c2005-09-16 02:38:09 +0000328 pCtx->pVdbeFunc = pVdbeFunc;
drh0e3d7472004-06-19 17:33:07 +0000329 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
330 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000331 pVdbeFunc->nAux = iArg+1;
332 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000333 }
334
drhf92c7ff2004-06-19 15:40:23 +0000335 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000336 if( pAuxData->pAux && pAuxData->xDelete ){
337 pAuxData->xDelete(pAuxData->pAux);
338 }
339 pAuxData->pAux = pAux;
340 pAuxData->xDelete = xDelete;
341}
342
343/*
drheb2e1762004-05-27 01:53:56 +0000344** Return the number of times the Step function of a aggregate has been
345** called.
346**
drhcf85a512006-02-09 18:35:29 +0000347** This function is deprecated. Do not use it for new code. It is
348** provide only to avoid breaking legacy code. New aggregate function
349** implementations should keep their own counts within their aggregate
350** context.
drheb2e1762004-05-27 01:53:56 +0000351*/
352int sqlite3_aggregate_count(sqlite3_context *p){
353 assert( p && p->pFunc && p->pFunc->xStep );
drhabfcea22005-09-06 20:36:48 +0000354 return p->pMem->n;
drheb2e1762004-05-27 01:53:56 +0000355}
356
357/*
drh4f26d6c2004-05-26 23:25:30 +0000358** Return the number of columns in the result set for the statement pStmt.
359*/
360int sqlite3_column_count(sqlite3_stmt *pStmt){
361 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000362 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000363}
364
365/*
366** Return the number of values available from the current row of the
367** currently executing statement pStmt.
368*/
369int sqlite3_data_count(sqlite3_stmt *pStmt){
370 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000371 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000372 return pVm->nResColumn;
373}
374
375
376/*
377** Check to see if column iCol of the given statement is valid. If
378** it is, return a pointer to the Mem for the value of that column.
379** If iCol is not valid, return a pointer to a Mem which has a value
380** of NULL.
381*/
382static Mem *columnMem(sqlite3_stmt *pStmt, int i){
383 Vdbe *pVm = (Vdbe *)pStmt;
384 int vals = sqlite3_data_count(pStmt);
385 if( i>=vals || i<0 ){
386 static Mem nullMem;
387 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
388 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
389 return &nullMem;
390 }
391 return &pVm->pTos[(1-vals)+i];
392}
393
danielk19772e588c72005-12-09 14:25:08 +0000394/*
395** This function is called after invoking an sqlite3_value_XXX function on a
396** column value (i.e. a value returned by evaluating an SQL expression in the
397** select list of a SELECT statement) that may cause a malloc() failure. If
398** malloc() has failed, the threads mallocFailed flag is cleared and the result
399** code of statement pStmt set to SQLITE_NOMEM.
400**
401** Specificly, this is called from within:
402**
403** sqlite3_column_int()
404** sqlite3_column_int64()
405** sqlite3_column_text()
406** sqlite3_column_text16()
407** sqlite3_column_real()
408** sqlite3_column_bytes()
409** sqlite3_column_bytes16()
410**
411** But not for sqlite3_column_blob(), which never calls malloc().
412*/
413static void columnMallocFailure(sqlite3_stmt *pStmt)
414{
415 /* If malloc() failed during an encoding conversion within an
416 ** sqlite3_column_XXX API, then set the return code of the statement to
417 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
418 ** and _finalize() will return NOMEM.
419 */
danielk197754f01982006-01-18 15:25:17 +0000420 Vdbe *p = (Vdbe *)pStmt;
421 p->rc = sqlite3ApiExit(0, p->rc);
danielk19772e588c72005-12-09 14:25:08 +0000422}
423
drh4f26d6c2004-05-26 23:25:30 +0000424/**************************** sqlite3_column_ *******************************
425** The following routines are used to access elements of the current row
426** in the result set.
427*/
danielk1977c572ef72004-05-27 09:28:41 +0000428const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000429 const void *val;
430 sqlite3MallocDisallow();
431 val = sqlite3_value_blob( columnMem(pStmt,i) );
432 sqlite3MallocAllow();
433 return val;
danielk1977c572ef72004-05-27 09:28:41 +0000434}
drh4f26d6c2004-05-26 23:25:30 +0000435int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000436 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
437 columnMallocFailure(pStmt);
438 return val;
drh4f26d6c2004-05-26 23:25:30 +0000439}
440int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000441 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
442 columnMallocFailure(pStmt);
443 return val;
drh4f26d6c2004-05-26 23:25:30 +0000444}
445double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000446 double val = sqlite3_value_double( columnMem(pStmt,i) );
447 columnMallocFailure(pStmt);
448 return val;
drh4f26d6c2004-05-26 23:25:30 +0000449}
450int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000451 int val = sqlite3_value_int( columnMem(pStmt,i) );
452 columnMallocFailure(pStmt);
453 return val;
drh4f26d6c2004-05-26 23:25:30 +0000454}
drhefad9992004-06-22 12:13:55 +0000455sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000456 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
457 columnMallocFailure(pStmt);
458 return val;
drh4f26d6c2004-05-26 23:25:30 +0000459}
460const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000461 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
462 columnMallocFailure(pStmt);
463 return val;
drh4f26d6c2004-05-26 23:25:30 +0000464}
drhd1e47332005-06-26 17:55:33 +0000465sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
466 return columnMem(pStmt, i);
467}
drh6c626082004-11-14 21:56:29 +0000468#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000469const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
danielk19772e588c72005-12-09 14:25:08 +0000470 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
471 columnMallocFailure(pStmt);
472 return val;
drh4f26d6c2004-05-26 23:25:30 +0000473}
drh6c626082004-11-14 21:56:29 +0000474#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000475int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
476 return sqlite3_value_type( columnMem(pStmt,i) );
477}
478
drh29d72102006-02-09 22:13:41 +0000479/* The following function is experimental and subject to change or
480** removal */
481/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
482** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
483**}
484*/
485
drh5e2517e2004-09-24 23:59:12 +0000486/*
487** Convert the N-th element of pStmt->pColName[] into a string using
488** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000489**
490** There are up to 5 names for each column. useType determines which
491** name is returned. Here are the names:
492**
493** 0 The column name as it should be displayed for output
494** 1 The datatype name for the column
495** 2 The name of the database that the column derives from
496** 3 The name of the table that the column derives from
497** 4 The name of the table column that the result column derives from
498**
499** If the result is not a simple column reference (if it is an expression
500** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000501*/
502static const void *columnName(
503 sqlite3_stmt *pStmt,
504 int N,
505 const void *(*xFunc)(Mem*),
506 int useType
507){
danielk197700fd9572005-12-07 06:27:43 +0000508 const void *ret;
drh5e2517e2004-09-24 23:59:12 +0000509 Vdbe *p = (Vdbe *)pStmt;
510 int n = sqlite3_column_count(pStmt);
511
512 if( p==0 || N>=n || N<0 ){
513 return 0;
514 }
drhe590fbd2005-05-20 19:36:01 +0000515 N += useType*n;
danielk197700fd9572005-12-07 06:27:43 +0000516 ret = xFunc(&p->aColName[N]);
517
518 /* A malloc may have failed inside of the xFunc() call. If this is the case,
519 ** clear the mallocFailed flag and return NULL.
520 */
danielk197754f01982006-01-18 15:25:17 +0000521 sqlite3ApiExit(0, 0);
danielk197700fd9572005-12-07 06:27:43 +0000522 return ret;
drh5e2517e2004-09-24 23:59:12 +0000523}
524
drh4f26d6c2004-05-26 23:25:30 +0000525/*
526** Return the name of the Nth column of the result set returned by SQL
527** statement pStmt.
528*/
529const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000530 return columnName(
531 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
drh4f26d6c2004-05-26 23:25:30 +0000532}
drhe590fbd2005-05-20 19:36:01 +0000533#ifndef SQLITE_OMIT_UTF16
534const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000535 return columnName(
536 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
drhe590fbd2005-05-20 19:36:01 +0000537}
538#endif
drh4f26d6c2004-05-26 23:25:30 +0000539
540/*
drh6c626082004-11-14 21:56:29 +0000541** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000542** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000543*/
544const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000545 return columnName(
546 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
drh6c626082004-11-14 21:56:29 +0000547}
drh6c626082004-11-14 21:56:29 +0000548#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000549const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000550 return columnName(
551 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
drh4f26d6c2004-05-26 23:25:30 +0000552}
drh6c626082004-11-14 21:56:29 +0000553#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000554
danielk19774b1ae992006-02-10 03:06:10 +0000555#ifdef SQLITE_ENABLE_COLUMN_METADATA
drhe590fbd2005-05-20 19:36:01 +0000556/*
557** Return the name of the database from which a result column derives.
558** NULL is returned if the result column is an expression or constant or
559** anything else which is not an unabiguous reference to a database column.
560*/
561const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000562 return columnName(
563 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000564}
565#ifndef SQLITE_OMIT_UTF16
566const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000567 return columnName(
568 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
drhe590fbd2005-05-20 19:36:01 +0000569}
570#endif /* SQLITE_OMIT_UTF16 */
571
572/*
573** Return the name of the table from which a result column derives.
574** NULL is returned if the result column is an expression or constant or
575** anything else which is not an unabiguous reference to a database column.
576*/
577const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000578 return columnName(
579 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000580}
581#ifndef SQLITE_OMIT_UTF16
582const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000583 return columnName(
584 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
drhe590fbd2005-05-20 19:36:01 +0000585}
586#endif /* SQLITE_OMIT_UTF16 */
587
588/*
589** Return the name of the table column from which a result column derives.
590** NULL is returned if the result column is an expression or constant or
591** anything else which is not an unabiguous reference to a database column.
592*/
593const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000594 return columnName(
595 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000596}
597#ifndef SQLITE_OMIT_UTF16
598const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
danielk1977955de522006-02-10 02:27:42 +0000599 return columnName(
600 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
drhe590fbd2005-05-20 19:36:01 +0000601}
602#endif /* SQLITE_OMIT_UTF16 */
danielk19774b1ae992006-02-10 03:06:10 +0000603#endif /* SQLITE_ENABLE_COLUMN_METADATA */
drhe590fbd2005-05-20 19:36:01 +0000604
605
drh4f26d6c2004-05-26 23:25:30 +0000606/******************************* sqlite3_bind_ ***************************
607**
608** Routines used to attach values to wildcards in a compiled SQL statement.
609*/
610/*
611** Unbind the value bound to variable i in virtual machine p. This is the
612** the same as binding a NULL value to the column. If the "i" parameter is
613** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
614**
615** The error code stored in database p->db is overwritten with the return
616** value in any case.
617*/
618static int vdbeUnbind(Vdbe *p, int i){
619 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000620 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +0000621 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +0000622 return SQLITE_MISUSE;
623 }
624 if( i<1 || i>p->nVar ){
625 sqlite3Error(p->db, SQLITE_RANGE, 0);
626 return SQLITE_RANGE;
627 }
628 i--;
drh290c1942004-08-21 17:54:45 +0000629 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000630 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000631 pVar->flags = MEM_Null;
632 sqlite3Error(p->db, SQLITE_OK, 0);
633 return SQLITE_OK;
634}
635
636/*
drh5e2517e2004-09-24 23:59:12 +0000637** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000638*/
drh5e2517e2004-09-24 23:59:12 +0000639static int bindText(
drhf4479502004-05-27 03:12:53 +0000640 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000641 int i,
642 const void *zData,
643 int nData,
drh5e2517e2004-09-24 23:59:12 +0000644 void (*xDel)(void*),
645 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000646){
647 Vdbe *p = (Vdbe *)pStmt;
648 Mem *pVar;
649 int rc;
650
651 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000652 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000653 return rc;
654 }
drh290c1942004-08-21 17:54:45 +0000655 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000656 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
drh5e2517e2004-09-24 23:59:12 +0000657 if( rc==SQLITE_OK && encoding!=0 ){
danielk197714db2662006-01-09 16:12:04 +0000658 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
drh5e2517e2004-09-24 23:59:12 +0000659 }
danielk197754f01982006-01-18 15:25:17 +0000660
danielk1977771151b2006-01-17 13:21:40 +0000661 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
danielk197754f01982006-01-18 15:25:17 +0000662 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
drh4f26d6c2004-05-26 23:25:30 +0000663}
drh5e2517e2004-09-24 23:59:12 +0000664
665
666/*
667** Bind a blob value to an SQL statement variable.
668*/
669int sqlite3_bind_blob(
670 sqlite3_stmt *pStmt,
671 int i,
672 const void *zData,
673 int nData,
674 void (*xDel)(void*)
675){
676 return bindText(pStmt, i, zData, nData, xDel, 0);
677}
drh4f26d6c2004-05-26 23:25:30 +0000678int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
679 int rc;
680 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000681 rc = vdbeUnbind(p, i);
682 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000683 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000684 }
danielk1977f4618892004-06-28 13:09:11 +0000685 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000686}
687int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000688 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000689}
drhefad9992004-06-22 12:13:55 +0000690int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000691 int rc;
692 Vdbe *p = (Vdbe *)pStmt;
693 rc = vdbeUnbind(p, i);
694 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000695 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000696 }
697 return rc;
698}
699int sqlite3_bind_null(sqlite3_stmt* p, int i){
700 return vdbeUnbind((Vdbe *)p, i);
701}
702int sqlite3_bind_text(
703 sqlite3_stmt *pStmt,
704 int i,
705 const char *zData,
706 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000707 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000708){
drh5e2517e2004-09-24 23:59:12 +0000709 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000710}
drh6c626082004-11-14 21:56:29 +0000711#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000712int sqlite3_bind_text16(
713 sqlite3_stmt *pStmt,
714 int i,
715 const void *zData,
716 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000717 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000718){
drh5e2517e2004-09-24 23:59:12 +0000719 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000720}
drh6c626082004-11-14 21:56:29 +0000721#endif /* SQLITE_OMIT_UTF16 */
danielk197726e41442006-06-14 15:16:35 +0000722int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
723 int rc;
724 Vdbe *p = (Vdbe *)pStmt;
725 rc = vdbeUnbind(p, i);
726 if( rc==SQLITE_OK ){
727 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
728 }
729 return rc;
730}
drh75f6a032004-07-15 14:15:00 +0000731
732/*
733** Return the number of wildcards that can be potentially bound to.
734** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000735*/
736int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000737 Vdbe *p = (Vdbe*)pStmt;
738 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000739}
drh895d7472004-08-20 16:02:39 +0000740
741/*
drhfa6bc002004-09-07 16:19:52 +0000742** Create a mapping from variable numbers to variable names
743** in the Vdbe.azVar[] array, if such a mapping does not already
744** exist.
drh895d7472004-08-20 16:02:39 +0000745*/
drhfa6bc002004-09-07 16:19:52 +0000746static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000747 if( !p->okVar ){
748 int j;
749 Op *pOp;
750 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
751 if( pOp->opcode==OP_Variable ){
752 assert( pOp->p1>0 && pOp->p1<=p->nVar );
753 p->azVar[pOp->p1-1] = pOp->p3;
754 }
755 }
756 p->okVar = 1;
757 }
drhfa6bc002004-09-07 16:19:52 +0000758}
759
760/*
761** Return the name of a wildcard parameter. Return NULL if the index
762** is out of range or if the wildcard is unnamed.
763**
764** The result is always UTF-8.
765*/
766const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
767 Vdbe *p = (Vdbe*)pStmt;
768 if( p==0 || i<1 || i>p->nVar ){
769 return 0;
770 }
771 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000772 return p->azVar[i-1];
773}
drhfa6bc002004-09-07 16:19:52 +0000774
775/*
776** Given a wildcard parameter name, return the index of the variable
777** with that name. If there is no variable with the given name,
778** return 0.
779*/
780int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
781 Vdbe *p = (Vdbe*)pStmt;
782 int i;
783 if( p==0 ){
784 return 0;
785 }
786 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000787 if( zName ){
788 for(i=0; i<p->nVar; i++){
789 const char *z = p->azVar[i];
790 if( z && strcmp(z,zName)==0 ){
791 return i+1;
792 }
drhfa6bc002004-09-07 16:19:52 +0000793 }
794 }
795 return 0;
796}
drhf8db1bc2005-04-22 02:38:37 +0000797
drh51942bc2005-06-12 22:01:42 +0000798/*
drhf8db1bc2005-04-22 02:38:37 +0000799** Transfer all bindings from the first statement over to the second.
800** If the two statements contain a different number of bindings, then
801** an SQLITE_ERROR is returned.
802*/
803int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
804 Vdbe *pFrom = (Vdbe*)pFromStmt;
805 Vdbe *pTo = (Vdbe*)pToStmt;
806 int i, rc = SQLITE_OK;
807 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
808 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
809 return SQLITE_MISUSE;
810 }
811 if( pFrom->nVar!=pTo->nVar ){
812 return SQLITE_ERROR;
813 }
814 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
danielk1977950f0542006-01-18 05:51:57 +0000815 sqlite3MallocDisallow();
drhf8db1bc2005-04-22 02:38:37 +0000816 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
danielk1977950f0542006-01-18 05:51:57 +0000817 sqlite3MallocAllow();
drhf8db1bc2005-04-22 02:38:37 +0000818 }
819 return rc;
820}
drh51942bc2005-06-12 22:01:42 +0000821
822/*
823** Return the sqlite3* database handle to which the prepared statement given
824** in the argument belongs. This is the same database handle that was
825** the first argument to the sqlite3_prepare() that was used to create
826** the statement in the first place.
827*/
828sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
829 return pStmt ? ((Vdbe*)pStmt)->db : 0;
830}