blob: f6047f6fbee2251938c9e426005f2344954b615e [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 }
drh4f26d6c2004-05-26 23:25:30 +0000180 if( p->explain ){
181 rc = sqlite3VdbeList(p);
182 }else{
183 rc = sqlite3VdbeExec(p);
184 }
185
186 if( sqlite3SafetyOff(db) ){
187 rc = SQLITE_MISUSE;
188 }
189
190 sqlite3Error(p->db, rc, p->zErrMsg);
191 return rc;
192}
193
194/*
drheb2e1762004-05-27 01:53:56 +0000195** Extract the user data from a sqlite3_context structure and return a
196** pointer to it.
197*/
198void *sqlite3_user_data(sqlite3_context *p){
199 assert( p && p->pFunc );
200 return p->pFunc->pUserData;
201}
202
203/*
204** Allocate or return the aggregate context for a user function. A new
205** context is allocated on the first call. Subsequent calls return the
206** same context that was returned on prior calls.
207**
208** This routine is defined here in vdbe.c because it depends on knowing
209** the internals of the sqlite3_context structure which is only defined in
210** this source file.
211*/
212void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
213 assert( p && p->pFunc && p->pFunc->xStep );
214 if( p->pAgg==0 ){
215 if( nByte<=NBFS ){
216 p->pAgg = (void*)p->s.z;
217 memset(p->pAgg, 0, nByte);
218 }else{
219 p->pAgg = sqliteMalloc( nByte );
220 }
221 }
222 return p->pAgg;
223}
224
225/*
danielk1977682f68b2004-06-05 10:22:17 +0000226** Return the auxilary data pointer, if any, for the iArg'th argument to
227** the user-function defined by pCtx.
228*/
229void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
230 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
231 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
232 return 0;
233 }
drhf92c7ff2004-06-19 15:40:23 +0000234 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000235}
236
237/*
238** Set the auxilary data pointer and delete function, for the iArg'th
239** argument to the user-function defined by pCtx. Any previous value is
240** deleted by calling the delete function specified when it was set.
241*/
242void sqlite3_set_auxdata(
243 sqlite3_context *pCtx,
244 int iArg,
245 void *pAux,
246 void (*xDelete)(void*)
247){
248 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000249 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000250 if( iArg<0 ) return;
251
drhf92c7ff2004-06-19 15:40:23 +0000252 pVdbeFunc = pCtx->pVdbeFunc;
253 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
254 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
255 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000256 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000257 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
258 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000259 pVdbeFunc->nAux = iArg+1;
260 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000261 }
262
drhf92c7ff2004-06-19 15:40:23 +0000263 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000264 if( pAuxData->pAux && pAuxData->xDelete ){
265 pAuxData->xDelete(pAuxData->pAux);
266 }
267 pAuxData->pAux = pAux;
268 pAuxData->xDelete = xDelete;
269}
270
271/*
drheb2e1762004-05-27 01:53:56 +0000272** Return the number of times the Step function of a aggregate has been
273** called.
274**
275** This routine is defined here in vdbe.c because it depends on knowing
276** the internals of the sqlite3_context structure which is only defined in
277** this source file.
278*/
279int sqlite3_aggregate_count(sqlite3_context *p){
280 assert( p && p->pFunc && p->pFunc->xStep );
281 return p->cnt;
282}
283
284/*
drh4f26d6c2004-05-26 23:25:30 +0000285** Return the number of columns in the result set for the statement pStmt.
286*/
287int sqlite3_column_count(sqlite3_stmt *pStmt){
288 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000289 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000290}
291
292/*
293** Return the number of values available from the current row of the
294** currently executing statement pStmt.
295*/
296int sqlite3_data_count(sqlite3_stmt *pStmt){
297 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000298 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000299 return pVm->nResColumn;
300}
301
302
303/*
304** Check to see if column iCol of the given statement is valid. If
305** it is, return a pointer to the Mem for the value of that column.
306** If iCol is not valid, return a pointer to a Mem which has a value
307** of NULL.
308*/
309static Mem *columnMem(sqlite3_stmt *pStmt, int i){
310 Vdbe *pVm = (Vdbe *)pStmt;
311 int vals = sqlite3_data_count(pStmt);
312 if( i>=vals || i<0 ){
313 static Mem nullMem;
314 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
315 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
316 return &nullMem;
317 }
318 return &pVm->pTos[(1-vals)+i];
319}
320
321/**************************** sqlite3_column_ *******************************
322** The following routines are used to access elements of the current row
323** in the result set.
324*/
danielk1977c572ef72004-05-27 09:28:41 +0000325const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
326 return sqlite3_value_blob( columnMem(pStmt,i) );
327}
drh4f26d6c2004-05-26 23:25:30 +0000328int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
329 return sqlite3_value_bytes( columnMem(pStmt,i) );
330}
331int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
332 return sqlite3_value_bytes16( columnMem(pStmt,i) );
333}
334double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
335 return sqlite3_value_double( columnMem(pStmt,i) );
336}
337int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
338 return sqlite3_value_int( columnMem(pStmt,i) );
339}
drhefad9992004-06-22 12:13:55 +0000340sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000341 return sqlite3_value_int64( columnMem(pStmt,i) );
342}
343const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
344 return sqlite3_value_text( columnMem(pStmt,i) );
345}
346const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
347 return sqlite3_value_text16( columnMem(pStmt,i) );
348}
349int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
350 return sqlite3_value_type( columnMem(pStmt,i) );
351}
352
drh5e2517e2004-09-24 23:59:12 +0000353/*
354** Convert the N-th element of pStmt->pColName[] into a string using
355** xFunc() then return that string. If N is out of range, return 0.
356** If useType is 1, then use the second set of N elements (the datatype
357** names) instead of the first set.
358*/
359static const void *columnName(
360 sqlite3_stmt *pStmt,
361 int N,
362 const void *(*xFunc)(Mem*),
363 int useType
364){
365 Vdbe *p = (Vdbe *)pStmt;
366 int n = sqlite3_column_count(pStmt);
367
368 if( p==0 || N>=n || N<0 ){
369 return 0;
370 }
371 if( useType ){
372 N += n;
373 }
374 return xFunc(&p->aColName[N]);
375}
376
drh4f26d6c2004-05-26 23:25:30 +0000377
378/*
379** Return the name of the Nth column of the result set returned by SQL
380** statement pStmt.
381*/
382const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000383 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000384}
385
386/*
387** Return the name of the 'i'th column of the result set of SQL statement
388** pStmt, encoded as UTF-16.
389*/
390const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000391 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
drh4f26d6c2004-05-26 23:25:30 +0000392}
393
drh4f26d6c2004-05-26 23:25:30 +0000394/*
395** Return the column declaration type (if applicable) of the 'i'th column
396** of the result set of SQL statement pStmt, encoded as UTF-8.
397*/
danielk197776d505b2004-05-28 13:13:02 +0000398const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000399 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
drh4f26d6c2004-05-26 23:25:30 +0000400}
401
402/*
403** Return the column declaration type (if applicable) of the 'i'th column
404** of the result set of SQL statement pStmt, encoded as UTF-16.
405*/
danielk197776d505b2004-05-28 13:13:02 +0000406const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000407 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000408}
409
410/******************************* sqlite3_bind_ ***************************
411**
412** Routines used to attach values to wildcards in a compiled SQL statement.
413*/
414/*
415** Unbind the value bound to variable i in virtual machine p. This is the
416** the same as binding a NULL value to the column. If the "i" parameter is
417** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
418**
419** The error code stored in database p->db is overwritten with the return
420** value in any case.
421*/
422static int vdbeUnbind(Vdbe *p, int i){
423 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000424 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000425 sqlite3Error(p->db, SQLITE_MISUSE, 0);
426 return SQLITE_MISUSE;
427 }
428 if( i<1 || i>p->nVar ){
429 sqlite3Error(p->db, SQLITE_RANGE, 0);
430 return SQLITE_RANGE;
431 }
432 i--;
drh290c1942004-08-21 17:54:45 +0000433 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000434 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000435 pVar->flags = MEM_Null;
436 sqlite3Error(p->db, SQLITE_OK, 0);
437 return SQLITE_OK;
438}
439
440/*
drh5e2517e2004-09-24 23:59:12 +0000441** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000442*/
drh5e2517e2004-09-24 23:59:12 +0000443static int bindText(
drhf4479502004-05-27 03:12:53 +0000444 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000445 int i,
446 const void *zData,
447 int nData,
drh5e2517e2004-09-24 23:59:12 +0000448 void (*xDel)(void*),
449 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000450){
451 Vdbe *p = (Vdbe *)pStmt;
452 Mem *pVar;
453 int rc;
454
455 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000456 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000457 return rc;
458 }
drh290c1942004-08-21 17:54:45 +0000459 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000460 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
461 if( rc ){
462 return rc;
463 }
464 if( rc==SQLITE_OK && encoding!=0 ){
465 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
466 }
drh4f26d6c2004-05-26 23:25:30 +0000467 return rc;
468}
drh5e2517e2004-09-24 23:59:12 +0000469
470
471/*
472** Bind a blob value to an SQL statement variable.
473*/
474int sqlite3_bind_blob(
475 sqlite3_stmt *pStmt,
476 int i,
477 const void *zData,
478 int nData,
479 void (*xDel)(void*)
480){
481 return bindText(pStmt, i, zData, nData, xDel, 0);
482}
drh4f26d6c2004-05-26 23:25:30 +0000483int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
484 int rc;
485 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000486 rc = vdbeUnbind(p, i);
487 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000488 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000489 }
danielk1977f4618892004-06-28 13:09:11 +0000490 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000491}
492int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000493 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000494}
drhefad9992004-06-22 12:13:55 +0000495int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000496 int rc;
497 Vdbe *p = (Vdbe *)pStmt;
498 rc = vdbeUnbind(p, i);
499 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000500 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000501 }
502 return rc;
503}
504int sqlite3_bind_null(sqlite3_stmt* p, int i){
505 return vdbeUnbind((Vdbe *)p, i);
506}
507int sqlite3_bind_text(
508 sqlite3_stmt *pStmt,
509 int i,
510 const char *zData,
511 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000512 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000513){
drh5e2517e2004-09-24 23:59:12 +0000514 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000515}
516int sqlite3_bind_text16(
517 sqlite3_stmt *pStmt,
518 int i,
519 const void *zData,
520 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000521 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000522){
drh5e2517e2004-09-24 23:59:12 +0000523 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000524}
drh75f6a032004-07-15 14:15:00 +0000525
526/*
527** Return the number of wildcards that can be potentially bound to.
528** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000529*/
530int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000531 Vdbe *p = (Vdbe*)pStmt;
532 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000533}
drh895d7472004-08-20 16:02:39 +0000534
535/*
drhfa6bc002004-09-07 16:19:52 +0000536** Create a mapping from variable numbers to variable names
537** in the Vdbe.azVar[] array, if such a mapping does not already
538** exist.
drh895d7472004-08-20 16:02:39 +0000539*/
drhfa6bc002004-09-07 16:19:52 +0000540static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000541 if( !p->okVar ){
542 int j;
543 Op *pOp;
544 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
545 if( pOp->opcode==OP_Variable ){
546 assert( pOp->p1>0 && pOp->p1<=p->nVar );
547 p->azVar[pOp->p1-1] = pOp->p3;
548 }
549 }
550 p->okVar = 1;
551 }
drhfa6bc002004-09-07 16:19:52 +0000552}
553
554/*
555** Return the name of a wildcard parameter. Return NULL if the index
556** is out of range or if the wildcard is unnamed.
557**
558** The result is always UTF-8.
559*/
560const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
561 Vdbe *p = (Vdbe*)pStmt;
562 if( p==0 || i<1 || i>p->nVar ){
563 return 0;
564 }
565 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000566 return p->azVar[i-1];
567}
drhfa6bc002004-09-07 16:19:52 +0000568
569/*
570** Given a wildcard parameter name, return the index of the variable
571** with that name. If there is no variable with the given name,
572** return 0.
573*/
574int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
575 Vdbe *p = (Vdbe*)pStmt;
576 int i;
577 if( p==0 ){
578 return 0;
579 }
580 createVarMap(p);
581 for(i=0; i<p->nVar; i++){
drh971a7c82004-09-24 12:48:12 +0000582 const char *z = p->azVar[i];
583 if( z && strcmp(z,zName)==0 ){
drhfa6bc002004-09-07 16:19:52 +0000584 return i+1;
585 }
586 }
587 return 0;
588}