blob: 348925a71e2b0f5c84b2df1aa4074aadfcb36f50 [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
drhd89bd002005-01-22 03:03:54 +000019/*
20** Return TRUE (non-zero) of the statement supplied as an argument needs
21** to be recompiled. A statement needs to be recompiled whenever the
22** execution environment changes in a way that would alter the program
23** that sqlite3_prepare() generates. For example, if new functions or
24** collating sequences are registered or if an authorizer function is
25** added or changed.
26**
27***** EXPERIMENTAL ******
28*/
29int sqlite3_expired(sqlite3_stmt *pStmt){
30 Vdbe *p = (Vdbe*)pStmt;
31 return p==0 || p->expired;
32}
33
drh4f26d6c2004-05-26 23:25:30 +000034/**************************** sqlite3_value_ *******************************
35** The following routines extract information from a Mem or sqlite3_value
36** structure.
37*/
38const void *sqlite3_value_blob(sqlite3_value *pVal){
39 Mem *p = (Mem*)pVal;
40 if( p->flags & (MEM_Blob|MEM_Str) ){
41 return p->z;
42 }else{
43 return sqlite3_value_text(pVal);
44 }
45}
46int sqlite3_value_bytes(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000047 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000048}
49int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000050 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000051}
52double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000053 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000054}
55int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000056 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000057}
drhefad9992004-06-22 12:13:55 +000058sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000059 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000060}
61const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000062 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000063}
drh6c626082004-11-14 21:56:29 +000064#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000065const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000066 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000067}
danielk1977d8123362004-06-12 09:25:12 +000068const void *sqlite3_value_text16be(sqlite3_value *pVal){
69 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
70}
71const void *sqlite3_value_text16le(sqlite3_value *pVal){
72 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
73}
drh6c626082004-11-14 21:56:29 +000074#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000075int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000076 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000077}
78
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){
89 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}
99void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
100 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +0000101 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000102}
drhf4479502004-05-27 03:12:53 +0000103void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000104 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
105}
106void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
107 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
108}
109void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000110 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000111}
112void sqlite3_result_text(
113 sqlite3_context *pCtx,
114 const char *z,
115 int n,
danielk1977d8123362004-06-12 09:25:12 +0000116 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000117){
danielk1977d8123362004-06-12 09:25:12 +0000118 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000119}
drh6c626082004-11-14 21:56:29 +0000120#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000121void sqlite3_result_text16(
122 sqlite3_context *pCtx,
123 const void *z,
124 int n,
danielk1977d8123362004-06-12 09:25:12 +0000125 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000126){
danielk1977d8123362004-06-12 09:25:12 +0000127 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
128}
129void sqlite3_result_text16be(
130 sqlite3_context *pCtx,
131 const void *z,
132 int n,
133 void (*xDel)(void *)
134){
135 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
136}
137void sqlite3_result_text16le(
138 sqlite3_context *pCtx,
139 const void *z,
140 int n,
141 void (*xDel)(void *)
142){
143 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000144}
drh6c626082004-11-14 21:56:29 +0000145#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000146void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
147 sqlite3VdbeMemCopy(&pCtx->s, pValue);
148}
149
150
151/*
152** Execute the statement pStmt, either until a row of data is ready, the
153** statement is completely executed or an error occurs.
154*/
155int sqlite3_step(sqlite3_stmt *pStmt){
156 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000157 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000158 int rc;
159
drh1bcdb0c2004-08-28 14:49:46 +0000160 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000161 return SQLITE_MISUSE;
162 }
drh91b48aa2004-06-30 11:14:18 +0000163 if( p->aborted ){
164 return SQLITE_ABORT;
165 }
drh4f26d6c2004-05-26 23:25:30 +0000166 db = p->db;
167 if( sqlite3SafetyOn(db) ){
168 p->rc = SQLITE_MISUSE;
169 return SQLITE_MISUSE;
170 }
danielk19771d850a72004-05-31 08:26:49 +0000171 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000172 /* Invoke the trace callback if there is one
173 */
174 if( (db = p->db)->xTrace && !db->init.busy ){
175 assert( p->nOp>0 );
176 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
177 assert( p->aOp[p->nOp-1].p3!=0 );
178 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
179 sqlite3SafetyOff(db);
180 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
181 if( sqlite3SafetyOn(db) ){
182 p->rc = SQLITE_MISUSE;
183 return SQLITE_MISUSE;
184 }
185 }
186
187 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
188 ** on in debugging mode.
189 */
190#ifdef SQLITE_DEBUG
191 if( (db->flags & SQLITE_SqlTrace)!=0 ){
192 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
193 }
194#endif /* SQLITE_DEBUG */
195
danielk19771d850a72004-05-31 08:26:49 +0000196 db->activeVdbeCnt++;
197 p->pc = 0;
198 }
drhb7f91642004-10-31 02:22:47 +0000199#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000200 if( p->explain ){
201 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000202 }else
203#endif /* SQLITE_OMIT_EXPLAIN */
204 {
drh4f26d6c2004-05-26 23:25:30 +0000205 rc = sqlite3VdbeExec(p);
206 }
207
208 if( sqlite3SafetyOff(db) ){
209 rc = SQLITE_MISUSE;
210 }
211
212 sqlite3Error(p->db, rc, p->zErrMsg);
213 return rc;
214}
215
216/*
drheb2e1762004-05-27 01:53:56 +0000217** Extract the user data from a sqlite3_context structure and return a
218** pointer to it.
219*/
220void *sqlite3_user_data(sqlite3_context *p){
221 assert( p && p->pFunc );
222 return p->pFunc->pUserData;
223}
224
225/*
226** Allocate or return the aggregate context for a user function. A new
227** context is allocated on the first call. Subsequent calls return the
228** same context that was returned on prior calls.
229**
230** This routine is defined here in vdbe.c because it depends on knowing
231** the internals of the sqlite3_context structure which is only defined in
232** this source file.
233*/
234void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
235 assert( p && p->pFunc && p->pFunc->xStep );
236 if( p->pAgg==0 ){
237 if( nByte<=NBFS ){
238 p->pAgg = (void*)p->s.z;
239 memset(p->pAgg, 0, nByte);
240 }else{
241 p->pAgg = sqliteMalloc( nByte );
242 }
243 }
244 return p->pAgg;
245}
246
247/*
danielk1977682f68b2004-06-05 10:22:17 +0000248** Return the auxilary data pointer, if any, for the iArg'th argument to
249** the user-function defined by pCtx.
250*/
251void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
252 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
253 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
254 return 0;
255 }
drhf92c7ff2004-06-19 15:40:23 +0000256 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000257}
258
259/*
260** Set the auxilary data pointer and delete function, for the iArg'th
261** argument to the user-function defined by pCtx. Any previous value is
262** deleted by calling the delete function specified when it was set.
263*/
264void sqlite3_set_auxdata(
265 sqlite3_context *pCtx,
266 int iArg,
267 void *pAux,
268 void (*xDelete)(void*)
269){
270 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000271 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000272 if( iArg<0 ) return;
273
drhf92c7ff2004-06-19 15:40:23 +0000274 pVdbeFunc = pCtx->pVdbeFunc;
275 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
276 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
277 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000278 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000279 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
280 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000281 pVdbeFunc->nAux = iArg+1;
282 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000283 }
284
drhf92c7ff2004-06-19 15:40:23 +0000285 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000286 if( pAuxData->pAux && pAuxData->xDelete ){
287 pAuxData->xDelete(pAuxData->pAux);
288 }
289 pAuxData->pAux = pAux;
290 pAuxData->xDelete = xDelete;
291}
292
293/*
drheb2e1762004-05-27 01:53:56 +0000294** Return the number of times the Step function of a aggregate has been
295** called.
296**
297** This routine is defined here in vdbe.c because it depends on knowing
298** the internals of the sqlite3_context structure which is only defined in
299** this source file.
300*/
301int sqlite3_aggregate_count(sqlite3_context *p){
302 assert( p && p->pFunc && p->pFunc->xStep );
303 return p->cnt;
304}
305
306/*
drh4f26d6c2004-05-26 23:25:30 +0000307** Return the number of columns in the result set for the statement pStmt.
308*/
309int sqlite3_column_count(sqlite3_stmt *pStmt){
310 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000311 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000312}
313
314/*
315** Return the number of values available from the current row of the
316** currently executing statement pStmt.
317*/
318int sqlite3_data_count(sqlite3_stmt *pStmt){
319 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000320 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000321 return pVm->nResColumn;
322}
323
324
325/*
326** Check to see if column iCol of the given statement is valid. If
327** it is, return a pointer to the Mem for the value of that column.
328** If iCol is not valid, return a pointer to a Mem which has a value
329** of NULL.
330*/
331static Mem *columnMem(sqlite3_stmt *pStmt, int i){
332 Vdbe *pVm = (Vdbe *)pStmt;
333 int vals = sqlite3_data_count(pStmt);
334 if( i>=vals || i<0 ){
335 static Mem nullMem;
336 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
337 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
338 return &nullMem;
339 }
340 return &pVm->pTos[(1-vals)+i];
341}
342
343/**************************** sqlite3_column_ *******************************
344** The following routines are used to access elements of the current row
345** in the result set.
346*/
danielk1977c572ef72004-05-27 09:28:41 +0000347const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
348 return sqlite3_value_blob( columnMem(pStmt,i) );
349}
drh4f26d6c2004-05-26 23:25:30 +0000350int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
351 return sqlite3_value_bytes( columnMem(pStmt,i) );
352}
353int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
354 return sqlite3_value_bytes16( columnMem(pStmt,i) );
355}
356double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
357 return sqlite3_value_double( columnMem(pStmt,i) );
358}
359int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
360 return sqlite3_value_int( columnMem(pStmt,i) );
361}
drhefad9992004-06-22 12:13:55 +0000362sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000363 return sqlite3_value_int64( columnMem(pStmt,i) );
364}
365const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
366 return sqlite3_value_text( columnMem(pStmt,i) );
367}
drh6c626082004-11-14 21:56:29 +0000368#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000369const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
370 return sqlite3_value_text16( columnMem(pStmt,i) );
371}
drh6c626082004-11-14 21:56:29 +0000372#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000373int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
374 return sqlite3_value_type( columnMem(pStmt,i) );
375}
376
drh5e2517e2004-09-24 23:59:12 +0000377/*
378** Convert the N-th element of pStmt->pColName[] into a string using
379** xFunc() then return that string. If N is out of range, return 0.
380** If useType is 1, then use the second set of N elements (the datatype
381** names) instead of the first set.
382*/
383static const void *columnName(
384 sqlite3_stmt *pStmt,
385 int N,
386 const void *(*xFunc)(Mem*),
387 int useType
388){
389 Vdbe *p = (Vdbe *)pStmt;
390 int n = sqlite3_column_count(pStmt);
391
392 if( p==0 || N>=n || N<0 ){
393 return 0;
394 }
395 if( useType ){
396 N += n;
397 }
398 return xFunc(&p->aColName[N]);
399}
400
drh4f26d6c2004-05-26 23:25:30 +0000401
402/*
403** Return the name of the Nth column of the result set returned by SQL
404** statement pStmt.
405*/
406const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000407 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000408}
409
410/*
drh6c626082004-11-14 21:56:29 +0000411** Return the column declaration type (if applicable) of the 'i'th column
412** of the result set of SQL statement pStmt, encoded as UTF-8.
413*/
414const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
415 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
416}
417
418#ifndef SQLITE_OMIT_UTF16
419/*
drh4f26d6c2004-05-26 23:25:30 +0000420** Return the name of the 'i'th column of the result set of SQL statement
421** pStmt, encoded as UTF-16.
422*/
423const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000424 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
drh4f26d6c2004-05-26 23:25:30 +0000425}
426
drh4f26d6c2004-05-26 23:25:30 +0000427/*
428** Return the column declaration type (if applicable) of the 'i'th column
drh4f26d6c2004-05-26 23:25:30 +0000429** of the result set of SQL statement pStmt, encoded as UTF-16.
430*/
danielk197776d505b2004-05-28 13:13:02 +0000431const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000432 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000433}
drh6c626082004-11-14 21:56:29 +0000434#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000435
436/******************************* sqlite3_bind_ ***************************
437**
438** Routines used to attach values to wildcards in a compiled SQL statement.
439*/
440/*
441** Unbind the value bound to variable i in virtual machine p. This is the
442** the same as binding a NULL value to the column. If the "i" parameter is
443** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
444**
445** The error code stored in database p->db is overwritten with the return
446** value in any case.
447*/
448static int vdbeUnbind(Vdbe *p, int i){
449 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000450 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000451 sqlite3Error(p->db, SQLITE_MISUSE, 0);
452 return SQLITE_MISUSE;
453 }
454 if( i<1 || i>p->nVar ){
455 sqlite3Error(p->db, SQLITE_RANGE, 0);
456 return SQLITE_RANGE;
457 }
458 i--;
drh290c1942004-08-21 17:54:45 +0000459 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000460 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000461 pVar->flags = MEM_Null;
462 sqlite3Error(p->db, SQLITE_OK, 0);
463 return SQLITE_OK;
464}
465
466/*
drh5e2517e2004-09-24 23:59:12 +0000467** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000468*/
drh5e2517e2004-09-24 23:59:12 +0000469static int bindText(
drhf4479502004-05-27 03:12:53 +0000470 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000471 int i,
472 const void *zData,
473 int nData,
drh5e2517e2004-09-24 23:59:12 +0000474 void (*xDel)(void*),
475 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000476){
477 Vdbe *p = (Vdbe *)pStmt;
478 Mem *pVar;
479 int rc;
480
481 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000482 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000483 return rc;
484 }
drh290c1942004-08-21 17:54:45 +0000485 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000486 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
487 if( rc ){
488 return rc;
489 }
490 if( rc==SQLITE_OK && encoding!=0 ){
491 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
492 }
drh4f26d6c2004-05-26 23:25:30 +0000493 return rc;
494}
drh5e2517e2004-09-24 23:59:12 +0000495
496
497/*
498** Bind a blob value to an SQL statement variable.
499*/
500int sqlite3_bind_blob(
501 sqlite3_stmt *pStmt,
502 int i,
503 const void *zData,
504 int nData,
505 void (*xDel)(void*)
506){
507 return bindText(pStmt, i, zData, nData, xDel, 0);
508}
drh4f26d6c2004-05-26 23:25:30 +0000509int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
510 int rc;
511 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000512 rc = vdbeUnbind(p, i);
513 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000514 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000515 }
danielk1977f4618892004-06-28 13:09:11 +0000516 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000517}
518int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000519 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000520}
drhefad9992004-06-22 12:13:55 +0000521int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000522 int rc;
523 Vdbe *p = (Vdbe *)pStmt;
524 rc = vdbeUnbind(p, i);
525 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000526 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000527 }
528 return rc;
529}
530int sqlite3_bind_null(sqlite3_stmt* p, int i){
531 return vdbeUnbind((Vdbe *)p, i);
532}
533int sqlite3_bind_text(
534 sqlite3_stmt *pStmt,
535 int i,
536 const char *zData,
537 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000538 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000539){
drh5e2517e2004-09-24 23:59:12 +0000540 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000541}
drh6c626082004-11-14 21:56:29 +0000542#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000543int sqlite3_bind_text16(
544 sqlite3_stmt *pStmt,
545 int i,
546 const void *zData,
547 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000548 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000549){
drh5e2517e2004-09-24 23:59:12 +0000550 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000551}
drh6c626082004-11-14 21:56:29 +0000552#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000553
554/*
555** Return the number of wildcards that can be potentially bound to.
556** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000557*/
558int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000559 Vdbe *p = (Vdbe*)pStmt;
560 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000561}
drh895d7472004-08-20 16:02:39 +0000562
563/*
drhfa6bc002004-09-07 16:19:52 +0000564** Create a mapping from variable numbers to variable names
565** in the Vdbe.azVar[] array, if such a mapping does not already
566** exist.
drh895d7472004-08-20 16:02:39 +0000567*/
drhfa6bc002004-09-07 16:19:52 +0000568static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000569 if( !p->okVar ){
570 int j;
571 Op *pOp;
572 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
573 if( pOp->opcode==OP_Variable ){
574 assert( pOp->p1>0 && pOp->p1<=p->nVar );
575 p->azVar[pOp->p1-1] = pOp->p3;
576 }
577 }
578 p->okVar = 1;
579 }
drhfa6bc002004-09-07 16:19:52 +0000580}
581
582/*
583** Return the name of a wildcard parameter. Return NULL if the index
584** is out of range or if the wildcard is unnamed.
585**
586** The result is always UTF-8.
587*/
588const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
589 Vdbe *p = (Vdbe*)pStmt;
590 if( p==0 || i<1 || i>p->nVar ){
591 return 0;
592 }
593 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000594 return p->azVar[i-1];
595}
drhfa6bc002004-09-07 16:19:52 +0000596
597/*
598** Given a wildcard parameter name, return the index of the variable
599** with that name. If there is no variable with the given name,
600** return 0.
601*/
602int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
603 Vdbe *p = (Vdbe*)pStmt;
604 int i;
605 if( p==0 ){
606 return 0;
607 }
608 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000609 if( zName ){
610 for(i=0; i<p->nVar; i++){
611 const char *z = p->azVar[i];
612 if( z && strcmp(z,zName)==0 ){
613 return i+1;
614 }
drhfa6bc002004-09-07 16:19:52 +0000615 }
616 }
617 return 0;
618}