blob: 247c1e0d0f5823db06b77b4e46c7980443334f51 [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 }
danielk1977a21c6b62005-01-24 10:25:59 +0000166 if( p->pc<=0 && p->expired ){
167 if( p->rc==SQLITE_OK ){
168 p->rc = SQLITE_SCHEMA;
169 }
170 return SQLITE_ERROR;
171 }
drh4f26d6c2004-05-26 23:25:30 +0000172 db = p->db;
173 if( sqlite3SafetyOn(db) ){
174 p->rc = SQLITE_MISUSE;
175 return SQLITE_MISUSE;
176 }
danielk19771d850a72004-05-31 08:26:49 +0000177 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000178 /* Invoke the trace callback if there is one
179 */
180 if( (db = p->db)->xTrace && !db->init.busy ){
181 assert( p->nOp>0 );
182 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
183 assert( p->aOp[p->nOp-1].p3!=0 );
184 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
185 sqlite3SafetyOff(db);
186 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
187 if( sqlite3SafetyOn(db) ){
188 p->rc = SQLITE_MISUSE;
189 return SQLITE_MISUSE;
190 }
191 }
192
193 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
194 ** on in debugging mode.
195 */
196#ifdef SQLITE_DEBUG
197 if( (db->flags & SQLITE_SqlTrace)!=0 ){
198 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
199 }
200#endif /* SQLITE_DEBUG */
201
danielk19771d850a72004-05-31 08:26:49 +0000202 db->activeVdbeCnt++;
203 p->pc = 0;
204 }
drhb7f91642004-10-31 02:22:47 +0000205#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000206 if( p->explain ){
207 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000208 }else
209#endif /* SQLITE_OMIT_EXPLAIN */
210 {
drh4f26d6c2004-05-26 23:25:30 +0000211 rc = sqlite3VdbeExec(p);
212 }
213
214 if( sqlite3SafetyOff(db) ){
215 rc = SQLITE_MISUSE;
216 }
217
218 sqlite3Error(p->db, rc, p->zErrMsg);
219 return rc;
220}
221
222/*
drheb2e1762004-05-27 01:53:56 +0000223** Extract the user data from a sqlite3_context structure and return a
224** pointer to it.
225*/
226void *sqlite3_user_data(sqlite3_context *p){
227 assert( p && p->pFunc );
228 return p->pFunc->pUserData;
229}
230
231/*
232** Allocate or return the aggregate context for a user function. A new
233** context is allocated on the first call. Subsequent calls return the
234** same context that was returned on prior calls.
235**
236** This routine is defined here in vdbe.c because it depends on knowing
237** the internals of the sqlite3_context structure which is only defined in
238** this source file.
239*/
240void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
241 assert( p && p->pFunc && p->pFunc->xStep );
242 if( p->pAgg==0 ){
243 if( nByte<=NBFS ){
244 p->pAgg = (void*)p->s.z;
245 memset(p->pAgg, 0, nByte);
246 }else{
247 p->pAgg = sqliteMalloc( nByte );
248 }
249 }
250 return p->pAgg;
251}
252
253/*
danielk1977682f68b2004-06-05 10:22:17 +0000254** Return the auxilary data pointer, if any, for the iArg'th argument to
255** the user-function defined by pCtx.
256*/
257void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
258 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
259 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
260 return 0;
261 }
drhf92c7ff2004-06-19 15:40:23 +0000262 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000263}
264
265/*
266** Set the auxilary data pointer and delete function, for the iArg'th
267** argument to the user-function defined by pCtx. Any previous value is
268** deleted by calling the delete function specified when it was set.
269*/
270void sqlite3_set_auxdata(
271 sqlite3_context *pCtx,
272 int iArg,
273 void *pAux,
274 void (*xDelete)(void*)
275){
276 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000277 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000278 if( iArg<0 ) return;
279
drhf92c7ff2004-06-19 15:40:23 +0000280 pVdbeFunc = pCtx->pVdbeFunc;
281 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
282 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
283 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000284 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000285 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
286 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000287 pVdbeFunc->nAux = iArg+1;
288 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000289 }
290
drhf92c7ff2004-06-19 15:40:23 +0000291 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000292 if( pAuxData->pAux && pAuxData->xDelete ){
293 pAuxData->xDelete(pAuxData->pAux);
294 }
295 pAuxData->pAux = pAux;
296 pAuxData->xDelete = xDelete;
297}
298
299/*
drheb2e1762004-05-27 01:53:56 +0000300** Return the number of times the Step function of a aggregate has been
301** called.
302**
303** This routine is defined here in vdbe.c because it depends on knowing
304** the internals of the sqlite3_context structure which is only defined in
305** this source file.
306*/
307int sqlite3_aggregate_count(sqlite3_context *p){
308 assert( p && p->pFunc && p->pFunc->xStep );
309 return p->cnt;
310}
311
312/*
drh4f26d6c2004-05-26 23:25:30 +0000313** Return the number of columns in the result set for the statement pStmt.
314*/
315int sqlite3_column_count(sqlite3_stmt *pStmt){
316 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000317 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000318}
319
320/*
321** Return the number of values available from the current row of the
322** currently executing statement pStmt.
323*/
324int sqlite3_data_count(sqlite3_stmt *pStmt){
325 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000326 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000327 return pVm->nResColumn;
328}
329
330
331/*
332** Check to see if column iCol of the given statement is valid. If
333** it is, return a pointer to the Mem for the value of that column.
334** If iCol is not valid, return a pointer to a Mem which has a value
335** of NULL.
336*/
337static Mem *columnMem(sqlite3_stmt *pStmt, int i){
338 Vdbe *pVm = (Vdbe *)pStmt;
339 int vals = sqlite3_data_count(pStmt);
340 if( i>=vals || i<0 ){
341 static Mem nullMem;
342 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
343 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
344 return &nullMem;
345 }
346 return &pVm->pTos[(1-vals)+i];
347}
348
349/**************************** sqlite3_column_ *******************************
350** The following routines are used to access elements of the current row
351** in the result set.
352*/
danielk1977c572ef72004-05-27 09:28:41 +0000353const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
354 return sqlite3_value_blob( columnMem(pStmt,i) );
355}
drh4f26d6c2004-05-26 23:25:30 +0000356int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
357 return sqlite3_value_bytes( columnMem(pStmt,i) );
358}
359int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
360 return sqlite3_value_bytes16( columnMem(pStmt,i) );
361}
362double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
363 return sqlite3_value_double( columnMem(pStmt,i) );
364}
365int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
366 return sqlite3_value_int( columnMem(pStmt,i) );
367}
drhefad9992004-06-22 12:13:55 +0000368sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000369 return sqlite3_value_int64( columnMem(pStmt,i) );
370}
371const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
372 return sqlite3_value_text( columnMem(pStmt,i) );
373}
drh6c626082004-11-14 21:56:29 +0000374#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000375const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
376 return sqlite3_value_text16( columnMem(pStmt,i) );
377}
drh6c626082004-11-14 21:56:29 +0000378#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000379int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
380 return sqlite3_value_type( columnMem(pStmt,i) );
381}
382
drh5e2517e2004-09-24 23:59:12 +0000383/*
384** Convert the N-th element of pStmt->pColName[] into a string using
385** xFunc() then return that string. If N is out of range, return 0.
386** If useType is 1, then use the second set of N elements (the datatype
387** names) instead of the first set.
388*/
389static const void *columnName(
390 sqlite3_stmt *pStmt,
391 int N,
392 const void *(*xFunc)(Mem*),
393 int useType
394){
395 Vdbe *p = (Vdbe *)pStmt;
396 int n = sqlite3_column_count(pStmt);
397
398 if( p==0 || N>=n || N<0 ){
399 return 0;
400 }
401 if( useType ){
402 N += n;
403 }
404 return xFunc(&p->aColName[N]);
405}
406
drh4f26d6c2004-05-26 23:25:30 +0000407
408/*
409** Return the name of the Nth column of the result set returned by SQL
410** statement pStmt.
411*/
412const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000413 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000414}
415
416/*
drh6c626082004-11-14 21:56:29 +0000417** Return the column declaration type (if applicable) of the 'i'th column
418** of the result set of SQL statement pStmt, encoded as UTF-8.
419*/
420const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
421 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
422}
423
424#ifndef SQLITE_OMIT_UTF16
425/*
drh4f26d6c2004-05-26 23:25:30 +0000426** Return the name of the 'i'th column of the result set of SQL statement
427** pStmt, encoded as UTF-16.
428*/
429const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000430 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
drh4f26d6c2004-05-26 23:25:30 +0000431}
432
drh4f26d6c2004-05-26 23:25:30 +0000433/*
434** Return the column declaration type (if applicable) of the 'i'th column
drh4f26d6c2004-05-26 23:25:30 +0000435** of the result set of SQL statement pStmt, encoded as UTF-16.
436*/
danielk197776d505b2004-05-28 13:13:02 +0000437const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000438 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000439}
drh6c626082004-11-14 21:56:29 +0000440#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000441
442/******************************* sqlite3_bind_ ***************************
443**
444** Routines used to attach values to wildcards in a compiled SQL statement.
445*/
446/*
447** Unbind the value bound to variable i in virtual machine p. This is the
448** the same as binding a NULL value to the column. If the "i" parameter is
449** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
450**
451** The error code stored in database p->db is overwritten with the return
452** value in any case.
453*/
454static int vdbeUnbind(Vdbe *p, int i){
455 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000456 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000457 sqlite3Error(p->db, SQLITE_MISUSE, 0);
458 return SQLITE_MISUSE;
459 }
460 if( i<1 || i>p->nVar ){
461 sqlite3Error(p->db, SQLITE_RANGE, 0);
462 return SQLITE_RANGE;
463 }
464 i--;
drh290c1942004-08-21 17:54:45 +0000465 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000466 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000467 pVar->flags = MEM_Null;
468 sqlite3Error(p->db, SQLITE_OK, 0);
469 return SQLITE_OK;
470}
471
472/*
drh5e2517e2004-09-24 23:59:12 +0000473** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000474*/
drh5e2517e2004-09-24 23:59:12 +0000475static int bindText(
drhf4479502004-05-27 03:12:53 +0000476 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000477 int i,
478 const void *zData,
479 int nData,
drh5e2517e2004-09-24 23:59:12 +0000480 void (*xDel)(void*),
481 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000482){
483 Vdbe *p = (Vdbe *)pStmt;
484 Mem *pVar;
485 int rc;
486
487 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000488 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000489 return rc;
490 }
drh290c1942004-08-21 17:54:45 +0000491 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000492 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
493 if( rc ){
494 return rc;
495 }
496 if( rc==SQLITE_OK && encoding!=0 ){
497 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
498 }
drh4f26d6c2004-05-26 23:25:30 +0000499 return rc;
500}
drh5e2517e2004-09-24 23:59:12 +0000501
502
503/*
504** Bind a blob value to an SQL statement variable.
505*/
506int sqlite3_bind_blob(
507 sqlite3_stmt *pStmt,
508 int i,
509 const void *zData,
510 int nData,
511 void (*xDel)(void*)
512){
513 return bindText(pStmt, i, zData, nData, xDel, 0);
514}
drh4f26d6c2004-05-26 23:25:30 +0000515int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
516 int rc;
517 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000518 rc = vdbeUnbind(p, i);
519 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000520 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000521 }
danielk1977f4618892004-06-28 13:09:11 +0000522 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000523}
524int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000525 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000526}
drhefad9992004-06-22 12:13:55 +0000527int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000528 int rc;
529 Vdbe *p = (Vdbe *)pStmt;
530 rc = vdbeUnbind(p, i);
531 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000532 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000533 }
534 return rc;
535}
536int sqlite3_bind_null(sqlite3_stmt* p, int i){
537 return vdbeUnbind((Vdbe *)p, i);
538}
539int sqlite3_bind_text(
540 sqlite3_stmt *pStmt,
541 int i,
542 const char *zData,
543 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000544 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000545){
drh5e2517e2004-09-24 23:59:12 +0000546 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000547}
drh6c626082004-11-14 21:56:29 +0000548#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000549int sqlite3_bind_text16(
550 sqlite3_stmt *pStmt,
551 int i,
552 const void *zData,
553 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000554 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000555){
drh5e2517e2004-09-24 23:59:12 +0000556 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000557}
drh6c626082004-11-14 21:56:29 +0000558#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000559
560/*
561** Return the number of wildcards that can be potentially bound to.
562** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000563*/
564int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000565 Vdbe *p = (Vdbe*)pStmt;
566 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000567}
drh895d7472004-08-20 16:02:39 +0000568
569/*
drhfa6bc002004-09-07 16:19:52 +0000570** Create a mapping from variable numbers to variable names
571** in the Vdbe.azVar[] array, if such a mapping does not already
572** exist.
drh895d7472004-08-20 16:02:39 +0000573*/
drhfa6bc002004-09-07 16:19:52 +0000574static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000575 if( !p->okVar ){
576 int j;
577 Op *pOp;
578 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
579 if( pOp->opcode==OP_Variable ){
580 assert( pOp->p1>0 && pOp->p1<=p->nVar );
581 p->azVar[pOp->p1-1] = pOp->p3;
582 }
583 }
584 p->okVar = 1;
585 }
drhfa6bc002004-09-07 16:19:52 +0000586}
587
588/*
589** Return the name of a wildcard parameter. Return NULL if the index
590** is out of range or if the wildcard is unnamed.
591**
592** The result is always UTF-8.
593*/
594const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
595 Vdbe *p = (Vdbe*)pStmt;
596 if( p==0 || i<1 || i>p->nVar ){
597 return 0;
598 }
599 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000600 return p->azVar[i-1];
601}
drhfa6bc002004-09-07 16:19:52 +0000602
603/*
604** Given a wildcard parameter name, return the index of the variable
605** with that name. If there is no variable with the given name,
606** return 0.
607*/
608int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
609 Vdbe *p = (Vdbe*)pStmt;
610 int i;
611 if( p==0 ){
612 return 0;
613 }
614 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000615 if( zName ){
616 for(i=0; i<p->nVar; i++){
617 const char *z = p->azVar[i];
618 if( z && strcmp(z,zName)==0 ){
619 return i+1;
620 }
drhfa6bc002004-09-07 16:19:52 +0000621 }
622 }
623 return 0;
624}
drhf8db1bc2005-04-22 02:38:37 +0000625
626/* EXPERIMENTAL
627**
628** Transfer all bindings from the first statement over to the second.
629** If the two statements contain a different number of bindings, then
630** an SQLITE_ERROR is returned.
631*/
632int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
633 Vdbe *pFrom = (Vdbe*)pFromStmt;
634 Vdbe *pTo = (Vdbe*)pToStmt;
635 int i, rc = SQLITE_OK;
636 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
637 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
638 return SQLITE_MISUSE;
639 }
640 if( pFrom->nVar!=pTo->nVar ){
641 return SQLITE_ERROR;
642 }
643 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
644 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
645 }
646 return rc;
647}