blob: bc6a322d59d8211c9b94728ec6d92a1920d91b26 [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}
drh6c626082004-11-14 21:56:29 +000049#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000050const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000051 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000052}
danielk1977d8123362004-06-12 09:25:12 +000053const void *sqlite3_value_text16be(sqlite3_value *pVal){
54 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
55}
56const void *sqlite3_value_text16le(sqlite3_value *pVal){
57 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
58}
drh6c626082004-11-14 21:56:29 +000059#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000060int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000061 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000062}
63
64/**************************** sqlite3_result_ *******************************
65** The following routines are used by user-defined functions to specify
66** the function result.
67*/
68void sqlite3_result_blob(
69 sqlite3_context *pCtx,
70 const void *z,
71 int n,
danielk1977d8123362004-06-12 09:25:12 +000072 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000073){
74 assert( n>0 );
danielk1977d8123362004-06-12 09:25:12 +000075 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000076}
77void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
78 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
79}
80void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
81 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000082 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000083}
84void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
85 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000086 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000087}
drhf4479502004-05-27 03:12:53 +000088void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +000089 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
90}
91void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
92 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
93}
94void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +000095 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +000096}
97void sqlite3_result_text(
98 sqlite3_context *pCtx,
99 const char *z,
100 int n,
danielk1977d8123362004-06-12 09:25:12 +0000101 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000102){
danielk1977d8123362004-06-12 09:25:12 +0000103 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000104}
drh6c626082004-11-14 21:56:29 +0000105#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000106void sqlite3_result_text16(
107 sqlite3_context *pCtx,
108 const void *z,
109 int n,
danielk1977d8123362004-06-12 09:25:12 +0000110 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000111){
danielk1977d8123362004-06-12 09:25:12 +0000112 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
113}
114void sqlite3_result_text16be(
115 sqlite3_context *pCtx,
116 const void *z,
117 int n,
118 void (*xDel)(void *)
119){
120 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
121}
122void sqlite3_result_text16le(
123 sqlite3_context *pCtx,
124 const void *z,
125 int n,
126 void (*xDel)(void *)
127){
128 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000129}
drh6c626082004-11-14 21:56:29 +0000130#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000131void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
132 sqlite3VdbeMemCopy(&pCtx->s, pValue);
133}
134
135
136/*
137** Execute the statement pStmt, either until a row of data is ready, the
138** statement is completely executed or an error occurs.
139*/
140int sqlite3_step(sqlite3_stmt *pStmt){
141 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000142 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000143 int rc;
144
drh1bcdb0c2004-08-28 14:49:46 +0000145 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000146 return SQLITE_MISUSE;
147 }
drh91b48aa2004-06-30 11:14:18 +0000148 if( p->aborted ){
149 return SQLITE_ABORT;
150 }
drh4f26d6c2004-05-26 23:25:30 +0000151 db = p->db;
152 if( sqlite3SafetyOn(db) ){
153 p->rc = SQLITE_MISUSE;
154 return SQLITE_MISUSE;
155 }
danielk19771d850a72004-05-31 08:26:49 +0000156 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000157 /* Invoke the trace callback if there is one
158 */
159 if( (db = p->db)->xTrace && !db->init.busy ){
160 assert( p->nOp>0 );
161 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
162 assert( p->aOp[p->nOp-1].p3!=0 );
163 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
164 sqlite3SafetyOff(db);
165 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
166 if( sqlite3SafetyOn(db) ){
167 p->rc = SQLITE_MISUSE;
168 return SQLITE_MISUSE;
169 }
170 }
171
172 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
173 ** on in debugging mode.
174 */
175#ifdef SQLITE_DEBUG
176 if( (db->flags & SQLITE_SqlTrace)!=0 ){
177 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
178 }
179#endif /* SQLITE_DEBUG */
180
danielk19771d850a72004-05-31 08:26:49 +0000181 db->activeVdbeCnt++;
182 p->pc = 0;
183 }
drhb7f91642004-10-31 02:22:47 +0000184#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000185 if( p->explain ){
186 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000187 }else
188#endif /* SQLITE_OMIT_EXPLAIN */
189 {
drh4f26d6c2004-05-26 23:25:30 +0000190 rc = sqlite3VdbeExec(p);
191 }
192
193 if( sqlite3SafetyOff(db) ){
194 rc = SQLITE_MISUSE;
195 }
196
197 sqlite3Error(p->db, rc, p->zErrMsg);
198 return rc;
199}
200
201/*
drheb2e1762004-05-27 01:53:56 +0000202** Extract the user data from a sqlite3_context structure and return a
203** pointer to it.
204*/
205void *sqlite3_user_data(sqlite3_context *p){
206 assert( p && p->pFunc );
207 return p->pFunc->pUserData;
208}
209
210/*
211** Allocate or return the aggregate context for a user function. A new
212** context is allocated on the first call. Subsequent calls return the
213** same context that was returned on prior calls.
214**
215** This routine is defined here in vdbe.c because it depends on knowing
216** the internals of the sqlite3_context structure which is only defined in
217** this source file.
218*/
219void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
220 assert( p && p->pFunc && p->pFunc->xStep );
221 if( p->pAgg==0 ){
222 if( nByte<=NBFS ){
223 p->pAgg = (void*)p->s.z;
224 memset(p->pAgg, 0, nByte);
225 }else{
226 p->pAgg = sqliteMalloc( nByte );
227 }
228 }
229 return p->pAgg;
230}
231
232/*
danielk1977682f68b2004-06-05 10:22:17 +0000233** Return the auxilary data pointer, if any, for the iArg'th argument to
234** the user-function defined by pCtx.
235*/
236void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
237 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
238 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
239 return 0;
240 }
drhf92c7ff2004-06-19 15:40:23 +0000241 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000242}
243
244/*
245** Set the auxilary data pointer and delete function, for the iArg'th
246** argument to the user-function defined by pCtx. Any previous value is
247** deleted by calling the delete function specified when it was set.
248*/
249void sqlite3_set_auxdata(
250 sqlite3_context *pCtx,
251 int iArg,
252 void *pAux,
253 void (*xDelete)(void*)
254){
255 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000256 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000257 if( iArg<0 ) return;
258
drhf92c7ff2004-06-19 15:40:23 +0000259 pVdbeFunc = pCtx->pVdbeFunc;
260 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
261 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
262 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000263 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000264 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
265 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000266 pVdbeFunc->nAux = iArg+1;
267 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000268 }
269
drhf92c7ff2004-06-19 15:40:23 +0000270 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000271 if( pAuxData->pAux && pAuxData->xDelete ){
272 pAuxData->xDelete(pAuxData->pAux);
273 }
274 pAuxData->pAux = pAux;
275 pAuxData->xDelete = xDelete;
276}
277
278/*
drheb2e1762004-05-27 01:53:56 +0000279** Return the number of times the Step function of a aggregate has been
280** called.
281**
282** This routine is defined here in vdbe.c because it depends on knowing
283** the internals of the sqlite3_context structure which is only defined in
284** this source file.
285*/
286int sqlite3_aggregate_count(sqlite3_context *p){
287 assert( p && p->pFunc && p->pFunc->xStep );
288 return p->cnt;
289}
290
291/*
drh4f26d6c2004-05-26 23:25:30 +0000292** Return the number of columns in the result set for the statement pStmt.
293*/
294int sqlite3_column_count(sqlite3_stmt *pStmt){
295 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000296 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000297}
298
299/*
300** Return the number of values available from the current row of the
301** currently executing statement pStmt.
302*/
303int sqlite3_data_count(sqlite3_stmt *pStmt){
304 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000305 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000306 return pVm->nResColumn;
307}
308
309
310/*
311** Check to see if column iCol of the given statement is valid. If
312** it is, return a pointer to the Mem for the value of that column.
313** If iCol is not valid, return a pointer to a Mem which has a value
314** of NULL.
315*/
316static Mem *columnMem(sqlite3_stmt *pStmt, int i){
317 Vdbe *pVm = (Vdbe *)pStmt;
318 int vals = sqlite3_data_count(pStmt);
319 if( i>=vals || i<0 ){
320 static Mem nullMem;
321 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
322 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
323 return &nullMem;
324 }
325 return &pVm->pTos[(1-vals)+i];
326}
327
328/**************************** sqlite3_column_ *******************************
329** The following routines are used to access elements of the current row
330** in the result set.
331*/
danielk1977c572ef72004-05-27 09:28:41 +0000332const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
333 return sqlite3_value_blob( columnMem(pStmt,i) );
334}
drh4f26d6c2004-05-26 23:25:30 +0000335int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
336 return sqlite3_value_bytes( columnMem(pStmt,i) );
337}
338int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
339 return sqlite3_value_bytes16( columnMem(pStmt,i) );
340}
341double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
342 return sqlite3_value_double( columnMem(pStmt,i) );
343}
344int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
345 return sqlite3_value_int( columnMem(pStmt,i) );
346}
drhefad9992004-06-22 12:13:55 +0000347sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000348 return sqlite3_value_int64( columnMem(pStmt,i) );
349}
350const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
351 return sqlite3_value_text( columnMem(pStmt,i) );
352}
drh6c626082004-11-14 21:56:29 +0000353#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000354const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
355 return sqlite3_value_text16( columnMem(pStmt,i) );
356}
drh6c626082004-11-14 21:56:29 +0000357#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000358int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
359 return sqlite3_value_type( columnMem(pStmt,i) );
360}
361
drh5e2517e2004-09-24 23:59:12 +0000362/*
363** Convert the N-th element of pStmt->pColName[] into a string using
364** xFunc() then return that string. If N is out of range, return 0.
365** If useType is 1, then use the second set of N elements (the datatype
366** names) instead of the first set.
367*/
368static const void *columnName(
369 sqlite3_stmt *pStmt,
370 int N,
371 const void *(*xFunc)(Mem*),
372 int useType
373){
374 Vdbe *p = (Vdbe *)pStmt;
375 int n = sqlite3_column_count(pStmt);
376
377 if( p==0 || N>=n || N<0 ){
378 return 0;
379 }
380 if( useType ){
381 N += n;
382 }
383 return xFunc(&p->aColName[N]);
384}
385
drh4f26d6c2004-05-26 23:25:30 +0000386
387/*
388** Return the name of the Nth column of the result set returned by SQL
389** statement pStmt.
390*/
391const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000392 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000393}
394
395/*
drh6c626082004-11-14 21:56:29 +0000396** Return the column declaration type (if applicable) of the 'i'th column
397** of the result set of SQL statement pStmt, encoded as UTF-8.
398*/
399const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
400 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
401}
402
403#ifndef SQLITE_OMIT_UTF16
404/*
drh4f26d6c2004-05-26 23:25:30 +0000405** Return the name of the 'i'th column of the result set of SQL statement
406** pStmt, encoded as UTF-16.
407*/
408const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000409 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
drh4f26d6c2004-05-26 23:25:30 +0000410}
411
drh4f26d6c2004-05-26 23:25:30 +0000412/*
413** Return the column declaration type (if applicable) of the 'i'th column
drh4f26d6c2004-05-26 23:25:30 +0000414** of the result set of SQL statement pStmt, encoded as UTF-16.
415*/
danielk197776d505b2004-05-28 13:13:02 +0000416const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000417 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000418}
drh6c626082004-11-14 21:56:29 +0000419#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000420
421/******************************* sqlite3_bind_ ***************************
422**
423** Routines used to attach values to wildcards in a compiled SQL statement.
424*/
425/*
426** Unbind the value bound to variable i in virtual machine p. This is the
427** the same as binding a NULL value to the column. If the "i" parameter is
428** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
429**
430** The error code stored in database p->db is overwritten with the return
431** value in any case.
432*/
433static int vdbeUnbind(Vdbe *p, int i){
434 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000435 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000436 sqlite3Error(p->db, SQLITE_MISUSE, 0);
437 return SQLITE_MISUSE;
438 }
439 if( i<1 || i>p->nVar ){
440 sqlite3Error(p->db, SQLITE_RANGE, 0);
441 return SQLITE_RANGE;
442 }
443 i--;
drh290c1942004-08-21 17:54:45 +0000444 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000445 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000446 pVar->flags = MEM_Null;
447 sqlite3Error(p->db, SQLITE_OK, 0);
448 return SQLITE_OK;
449}
450
451/*
drh5e2517e2004-09-24 23:59:12 +0000452** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000453*/
drh5e2517e2004-09-24 23:59:12 +0000454static int bindText(
drhf4479502004-05-27 03:12:53 +0000455 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000456 int i,
457 const void *zData,
458 int nData,
drh5e2517e2004-09-24 23:59:12 +0000459 void (*xDel)(void*),
460 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000461){
462 Vdbe *p = (Vdbe *)pStmt;
463 Mem *pVar;
464 int rc;
465
466 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000467 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000468 return rc;
469 }
drh290c1942004-08-21 17:54:45 +0000470 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000471 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
472 if( rc ){
473 return rc;
474 }
475 if( rc==SQLITE_OK && encoding!=0 ){
476 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
477 }
drh4f26d6c2004-05-26 23:25:30 +0000478 return rc;
479}
drh5e2517e2004-09-24 23:59:12 +0000480
481
482/*
483** Bind a blob value to an SQL statement variable.
484*/
485int sqlite3_bind_blob(
486 sqlite3_stmt *pStmt,
487 int i,
488 const void *zData,
489 int nData,
490 void (*xDel)(void*)
491){
492 return bindText(pStmt, i, zData, nData, xDel, 0);
493}
drh4f26d6c2004-05-26 23:25:30 +0000494int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
495 int rc;
496 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000497 rc = vdbeUnbind(p, i);
498 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000499 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000500 }
danielk1977f4618892004-06-28 13:09:11 +0000501 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000502}
503int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000504 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000505}
drhefad9992004-06-22 12:13:55 +0000506int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000507 int rc;
508 Vdbe *p = (Vdbe *)pStmt;
509 rc = vdbeUnbind(p, i);
510 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000511 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000512 }
513 return rc;
514}
515int sqlite3_bind_null(sqlite3_stmt* p, int i){
516 return vdbeUnbind((Vdbe *)p, i);
517}
518int sqlite3_bind_text(
519 sqlite3_stmt *pStmt,
520 int i,
521 const char *zData,
522 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000523 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000524){
drh5e2517e2004-09-24 23:59:12 +0000525 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000526}
drh6c626082004-11-14 21:56:29 +0000527#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000528int sqlite3_bind_text16(
529 sqlite3_stmt *pStmt,
530 int i,
531 const void *zData,
532 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000533 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000534){
drh5e2517e2004-09-24 23:59:12 +0000535 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000536}
drh6c626082004-11-14 21:56:29 +0000537#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000538
539/*
540** Return the number of wildcards that can be potentially bound to.
541** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000542*/
543int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000544 Vdbe *p = (Vdbe*)pStmt;
545 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000546}
drh895d7472004-08-20 16:02:39 +0000547
548/*
drhfa6bc002004-09-07 16:19:52 +0000549** Create a mapping from variable numbers to variable names
550** in the Vdbe.azVar[] array, if such a mapping does not already
551** exist.
drh895d7472004-08-20 16:02:39 +0000552*/
drhfa6bc002004-09-07 16:19:52 +0000553static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000554 if( !p->okVar ){
555 int j;
556 Op *pOp;
557 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
558 if( pOp->opcode==OP_Variable ){
559 assert( pOp->p1>0 && pOp->p1<=p->nVar );
560 p->azVar[pOp->p1-1] = pOp->p3;
561 }
562 }
563 p->okVar = 1;
564 }
drhfa6bc002004-09-07 16:19:52 +0000565}
566
567/*
568** Return the name of a wildcard parameter. Return NULL if the index
569** is out of range or if the wildcard is unnamed.
570**
571** The result is always UTF-8.
572*/
573const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
574 Vdbe *p = (Vdbe*)pStmt;
575 if( p==0 || i<1 || i>p->nVar ){
576 return 0;
577 }
578 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000579 return p->azVar[i-1];
580}
drhfa6bc002004-09-07 16:19:52 +0000581
582/*
583** Given a wildcard parameter name, return the index of the variable
584** with that name. If there is no variable with the given name,
585** return 0.
586*/
587int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
588 Vdbe *p = (Vdbe*)pStmt;
589 int i;
590 if( p==0 ){
591 return 0;
592 }
593 createVarMap(p);
594 for(i=0; i<p->nVar; i++){
drh971a7c82004-09-24 12:48:12 +0000595 const char *z = p->azVar[i];
596 if( z && strcmp(z,zName)==0 ){
drhfa6bc002004-09-07 16:19:52 +0000597 return i+1;
598 }
599 }
600 return 0;
601}