blob: bd39a539f40c8ddd30b3c2f0be20d38285ba2fc6 [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"
18
19/**************************** sqlite3_value_ *******************************
20** The following routines extract information from a Mem or sqlite3_value
21** structure.
22*/
23const void *sqlite3_value_blob(sqlite3_value *pVal){
24 Mem *p = (Mem*)pVal;
25 if( p->flags & (MEM_Blob|MEM_Str) ){
26 return p->z;
27 }else{
28 return sqlite3_value_text(pVal);
29 }
30}
31int sqlite3_value_bytes(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000032 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000033}
34int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000035 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000036}
37double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000038 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000039}
40int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000041 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000042}
drhefad9992004-06-22 12:13:55 +000043sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000044 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000045}
46const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000047 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000048}
49const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000050 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000051}
danielk1977d8123362004-06-12 09:25:12 +000052const void *sqlite3_value_text16be(sqlite3_value *pVal){
53 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
54}
55const void *sqlite3_value_text16le(sqlite3_value *pVal){
56 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
57}
drh4f26d6c2004-05-26 23:25:30 +000058int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000059 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000060}
61
62/**************************** sqlite3_result_ *******************************
63** The following routines are used by user-defined functions to specify
64** the function result.
65*/
66void sqlite3_result_blob(
67 sqlite3_context *pCtx,
68 const void *z,
69 int n,
danielk1977d8123362004-06-12 09:25:12 +000070 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000071){
72 assert( n>0 );
danielk1977d8123362004-06-12 09:25:12 +000073 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000074}
75void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
76 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
77}
78void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
79 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000080 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000081}
82void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
83 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000084 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000085}
drhf4479502004-05-27 03:12:53 +000086void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +000087 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
88}
89void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
90 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
91}
92void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +000093 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +000094}
95void sqlite3_result_text(
96 sqlite3_context *pCtx,
97 const char *z,
98 int n,
danielk1977d8123362004-06-12 09:25:12 +000099 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000100){
danielk1977d8123362004-06-12 09:25:12 +0000101 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000102}
103void sqlite3_result_text16(
104 sqlite3_context *pCtx,
105 const void *z,
106 int n,
danielk1977d8123362004-06-12 09:25:12 +0000107 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000108){
danielk1977d8123362004-06-12 09:25:12 +0000109 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
110}
111void sqlite3_result_text16be(
112 sqlite3_context *pCtx,
113 const void *z,
114 int n,
115 void (*xDel)(void *)
116){
117 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
118}
119void sqlite3_result_text16le(
120 sqlite3_context *pCtx,
121 const void *z,
122 int n,
123 void (*xDel)(void *)
124){
125 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000126}
127void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
128 sqlite3VdbeMemCopy(&pCtx->s, pValue);
129}
130
131
132/*
133** Execute the statement pStmt, either until a row of data is ready, the
134** statement is completely executed or an error occurs.
135*/
136int sqlite3_step(sqlite3_stmt *pStmt){
137 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000138 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000139 int rc;
140
drh1bcdb0c2004-08-28 14:49:46 +0000141 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000142 return SQLITE_MISUSE;
143 }
drh91b48aa2004-06-30 11:14:18 +0000144 if( p->aborted ){
145 return SQLITE_ABORT;
146 }
drh4f26d6c2004-05-26 23:25:30 +0000147 db = p->db;
148 if( sqlite3SafetyOn(db) ){
149 p->rc = SQLITE_MISUSE;
150 return SQLITE_MISUSE;
151 }
danielk19771d850a72004-05-31 08:26:49 +0000152 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000153 /* Invoke the trace callback if there is one
154 */
155 if( (db = p->db)->xTrace && !db->init.busy ){
156 assert( p->nOp>0 );
157 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
158 assert( p->aOp[p->nOp-1].p3!=0 );
159 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
160 sqlite3SafetyOff(db);
161 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
162 if( sqlite3SafetyOn(db) ){
163 p->rc = SQLITE_MISUSE;
164 return SQLITE_MISUSE;
165 }
166 }
167
168 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
169 ** on in debugging mode.
170 */
171#ifdef SQLITE_DEBUG
172 if( (db->flags & SQLITE_SqlTrace)!=0 ){
173 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
174 }
175#endif /* SQLITE_DEBUG */
176
danielk19771d850a72004-05-31 08:26:49 +0000177 db->activeVdbeCnt++;
178 p->pc = 0;
179 }
drhb7f91642004-10-31 02:22:47 +0000180#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000181 if( p->explain ){
182 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000183 }else
184#endif /* SQLITE_OMIT_EXPLAIN */
185 {
drh4f26d6c2004-05-26 23:25:30 +0000186 rc = sqlite3VdbeExec(p);
187 }
188
189 if( sqlite3SafetyOff(db) ){
190 rc = SQLITE_MISUSE;
191 }
192
193 sqlite3Error(p->db, rc, p->zErrMsg);
194 return rc;
195}
196
197/*
drheb2e1762004-05-27 01:53:56 +0000198** Extract the user data from a sqlite3_context structure and return a
199** pointer to it.
200*/
201void *sqlite3_user_data(sqlite3_context *p){
202 assert( p && p->pFunc );
203 return p->pFunc->pUserData;
204}
205
206/*
207** Allocate or return the aggregate context for a user function. A new
208** context is allocated on the first call. Subsequent calls return the
209** same context that was returned on prior calls.
210**
211** This routine is defined here in vdbe.c because it depends on knowing
212** the internals of the sqlite3_context structure which is only defined in
213** this source file.
214*/
215void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
216 assert( p && p->pFunc && p->pFunc->xStep );
217 if( p->pAgg==0 ){
218 if( nByte<=NBFS ){
219 p->pAgg = (void*)p->s.z;
220 memset(p->pAgg, 0, nByte);
221 }else{
222 p->pAgg = sqliteMalloc( nByte );
223 }
224 }
225 return p->pAgg;
226}
227
228/*
danielk1977682f68b2004-06-05 10:22:17 +0000229** Return the auxilary data pointer, if any, for the iArg'th argument to
230** the user-function defined by pCtx.
231*/
232void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
233 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
234 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
235 return 0;
236 }
drhf92c7ff2004-06-19 15:40:23 +0000237 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000238}
239
240/*
241** Set the auxilary data pointer and delete function, for the iArg'th
242** argument to the user-function defined by pCtx. Any previous value is
243** deleted by calling the delete function specified when it was set.
244*/
245void sqlite3_set_auxdata(
246 sqlite3_context *pCtx,
247 int iArg,
248 void *pAux,
249 void (*xDelete)(void*)
250){
251 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000252 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000253 if( iArg<0 ) return;
254
drhf92c7ff2004-06-19 15:40:23 +0000255 pVdbeFunc = pCtx->pVdbeFunc;
256 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
257 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
258 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000259 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000260 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
261 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000262 pVdbeFunc->nAux = iArg+1;
263 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000264 }
265
drhf92c7ff2004-06-19 15:40:23 +0000266 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000267 if( pAuxData->pAux && pAuxData->xDelete ){
268 pAuxData->xDelete(pAuxData->pAux);
269 }
270 pAuxData->pAux = pAux;
271 pAuxData->xDelete = xDelete;
272}
273
274/*
drheb2e1762004-05-27 01:53:56 +0000275** Return the number of times the Step function of a aggregate has been
276** called.
277**
278** This routine is defined here in vdbe.c because it depends on knowing
279** the internals of the sqlite3_context structure which is only defined in
280** this source file.
281*/
282int sqlite3_aggregate_count(sqlite3_context *p){
283 assert( p && p->pFunc && p->pFunc->xStep );
284 return p->cnt;
285}
286
287/*
drh4f26d6c2004-05-26 23:25:30 +0000288** Return the number of columns in the result set for the statement pStmt.
289*/
290int sqlite3_column_count(sqlite3_stmt *pStmt){
291 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000292 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000293}
294
295/*
296** Return the number of values available from the current row of the
297** currently executing statement pStmt.
298*/
299int sqlite3_data_count(sqlite3_stmt *pStmt){
300 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000301 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000302 return pVm->nResColumn;
303}
304
305
306/*
307** Check to see if column iCol of the given statement is valid. If
308** it is, return a pointer to the Mem for the value of that column.
309** If iCol is not valid, return a pointer to a Mem which has a value
310** of NULL.
311*/
312static Mem *columnMem(sqlite3_stmt *pStmt, int i){
313 Vdbe *pVm = (Vdbe *)pStmt;
314 int vals = sqlite3_data_count(pStmt);
315 if( i>=vals || i<0 ){
316 static Mem nullMem;
317 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
318 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
319 return &nullMem;
320 }
321 return &pVm->pTos[(1-vals)+i];
322}
323
324/**************************** sqlite3_column_ *******************************
325** The following routines are used to access elements of the current row
326** in the result set.
327*/
danielk1977c572ef72004-05-27 09:28:41 +0000328const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
329 return sqlite3_value_blob( columnMem(pStmt,i) );
330}
drh4f26d6c2004-05-26 23:25:30 +0000331int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
332 return sqlite3_value_bytes( columnMem(pStmt,i) );
333}
334int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
335 return sqlite3_value_bytes16( columnMem(pStmt,i) );
336}
337double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
338 return sqlite3_value_double( columnMem(pStmt,i) );
339}
340int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
341 return sqlite3_value_int( columnMem(pStmt,i) );
342}
drhefad9992004-06-22 12:13:55 +0000343sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000344 return sqlite3_value_int64( columnMem(pStmt,i) );
345}
346const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
347 return sqlite3_value_text( columnMem(pStmt,i) );
348}
349const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
350 return sqlite3_value_text16( columnMem(pStmt,i) );
351}
352int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
353 return sqlite3_value_type( columnMem(pStmt,i) );
354}
355
drh5e2517e2004-09-24 23:59:12 +0000356/*
357** Convert the N-th element of pStmt->pColName[] into a string using
358** xFunc() then return that string. If N is out of range, return 0.
359** If useType is 1, then use the second set of N elements (the datatype
360** names) instead of the first set.
361*/
362static const void *columnName(
363 sqlite3_stmt *pStmt,
364 int N,
365 const void *(*xFunc)(Mem*),
366 int useType
367){
368 Vdbe *p = (Vdbe *)pStmt;
369 int n = sqlite3_column_count(pStmt);
370
371 if( p==0 || N>=n || N<0 ){
372 return 0;
373 }
374 if( useType ){
375 N += n;
376 }
377 return xFunc(&p->aColName[N]);
378}
379
drh4f26d6c2004-05-26 23:25:30 +0000380
381/*
382** Return the name of the Nth column of the result set returned by SQL
383** statement pStmt.
384*/
385const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000386 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000387}
388
389/*
390** Return the name of the 'i'th column of the result set of SQL statement
391** pStmt, encoded as UTF-16.
392*/
393const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000394 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
drh4f26d6c2004-05-26 23:25:30 +0000395}
396
drh4f26d6c2004-05-26 23:25:30 +0000397/*
398** Return the column declaration type (if applicable) of the 'i'th column
399** of the result set of SQL statement pStmt, encoded as UTF-8.
400*/
danielk197776d505b2004-05-28 13:13:02 +0000401const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000402 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
drh4f26d6c2004-05-26 23:25:30 +0000403}
404
405/*
406** Return the column declaration type (if applicable) of the 'i'th column
407** of the result set of SQL statement pStmt, encoded as UTF-16.
408*/
danielk197776d505b2004-05-28 13:13:02 +0000409const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000410 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000411}
412
413/******************************* sqlite3_bind_ ***************************
414**
415** Routines used to attach values to wildcards in a compiled SQL statement.
416*/
417/*
418** Unbind the value bound to variable i in virtual machine p. This is the
419** the same as binding a NULL value to the column. If the "i" parameter is
420** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
421**
422** The error code stored in database p->db is overwritten with the return
423** value in any case.
424*/
425static int vdbeUnbind(Vdbe *p, int i){
426 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000427 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000428 sqlite3Error(p->db, SQLITE_MISUSE, 0);
429 return SQLITE_MISUSE;
430 }
431 if( i<1 || i>p->nVar ){
432 sqlite3Error(p->db, SQLITE_RANGE, 0);
433 return SQLITE_RANGE;
434 }
435 i--;
drh290c1942004-08-21 17:54:45 +0000436 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000437 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000438 pVar->flags = MEM_Null;
439 sqlite3Error(p->db, SQLITE_OK, 0);
440 return SQLITE_OK;
441}
442
443/*
drh5e2517e2004-09-24 23:59:12 +0000444** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000445*/
drh5e2517e2004-09-24 23:59:12 +0000446static int bindText(
drhf4479502004-05-27 03:12:53 +0000447 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000448 int i,
449 const void *zData,
450 int nData,
drh5e2517e2004-09-24 23:59:12 +0000451 void (*xDel)(void*),
452 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000453){
454 Vdbe *p = (Vdbe *)pStmt;
455 Mem *pVar;
456 int rc;
457
458 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000459 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000460 return rc;
461 }
drh290c1942004-08-21 17:54:45 +0000462 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000463 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
464 if( rc ){
465 return rc;
466 }
467 if( rc==SQLITE_OK && encoding!=0 ){
468 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
469 }
drh4f26d6c2004-05-26 23:25:30 +0000470 return rc;
471}
drh5e2517e2004-09-24 23:59:12 +0000472
473
474/*
475** Bind a blob value to an SQL statement variable.
476*/
477int sqlite3_bind_blob(
478 sqlite3_stmt *pStmt,
479 int i,
480 const void *zData,
481 int nData,
482 void (*xDel)(void*)
483){
484 return bindText(pStmt, i, zData, nData, xDel, 0);
485}
drh4f26d6c2004-05-26 23:25:30 +0000486int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
487 int rc;
488 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000489 rc = vdbeUnbind(p, i);
490 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000491 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000492 }
danielk1977f4618892004-06-28 13:09:11 +0000493 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000494}
495int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000496 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000497}
drhefad9992004-06-22 12:13:55 +0000498int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000499 int rc;
500 Vdbe *p = (Vdbe *)pStmt;
501 rc = vdbeUnbind(p, i);
502 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000503 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000504 }
505 return rc;
506}
507int sqlite3_bind_null(sqlite3_stmt* p, int i){
508 return vdbeUnbind((Vdbe *)p, i);
509}
510int sqlite3_bind_text(
511 sqlite3_stmt *pStmt,
512 int i,
513 const char *zData,
514 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000515 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000516){
drh5e2517e2004-09-24 23:59:12 +0000517 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000518}
519int sqlite3_bind_text16(
520 sqlite3_stmt *pStmt,
521 int i,
522 const void *zData,
523 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000524 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000525){
drh5e2517e2004-09-24 23:59:12 +0000526 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000527}
drh75f6a032004-07-15 14:15:00 +0000528
529/*
530** Return the number of wildcards that can be potentially bound to.
531** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000532*/
533int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000534 Vdbe *p = (Vdbe*)pStmt;
535 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000536}
drh895d7472004-08-20 16:02:39 +0000537
538/*
drhfa6bc002004-09-07 16:19:52 +0000539** Create a mapping from variable numbers to variable names
540** in the Vdbe.azVar[] array, if such a mapping does not already
541** exist.
drh895d7472004-08-20 16:02:39 +0000542*/
drhfa6bc002004-09-07 16:19:52 +0000543static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000544 if( !p->okVar ){
545 int j;
546 Op *pOp;
547 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
548 if( pOp->opcode==OP_Variable ){
549 assert( pOp->p1>0 && pOp->p1<=p->nVar );
550 p->azVar[pOp->p1-1] = pOp->p3;
551 }
552 }
553 p->okVar = 1;
554 }
drhfa6bc002004-09-07 16:19:52 +0000555}
556
557/*
558** Return the name of a wildcard parameter. Return NULL if the index
559** is out of range or if the wildcard is unnamed.
560**
561** The result is always UTF-8.
562*/
563const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
564 Vdbe *p = (Vdbe*)pStmt;
565 if( p==0 || i<1 || i>p->nVar ){
566 return 0;
567 }
568 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000569 return p->azVar[i-1];
570}
drhfa6bc002004-09-07 16:19:52 +0000571
572/*
573** Given a wildcard parameter name, return the index of the variable
574** with that name. If there is no variable with the given name,
575** return 0.
576*/
577int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
578 Vdbe *p = (Vdbe*)pStmt;
579 int i;
580 if( p==0 ){
581 return 0;
582 }
583 createVarMap(p);
584 for(i=0; i<p->nVar; i++){
drh971a7c82004-09-24 12:48:12 +0000585 const char *z = p->azVar[i];
586 if( z && strcmp(z,zName)==0 ){
drhfa6bc002004-09-07 16:19:52 +0000587 return i+1;
588 }
589 }
590 return 0;
591}