xref: /sqlite-3.40.0/src/vdbeapi.c (revision 4dcbdbff)
1 /*
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 /*
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 int sqlite3_expired(sqlite3_stmt *pStmt){
28   Vdbe *p = (Vdbe*)pStmt;
29   return p==0 || p->expired;
30 }
31 
32 /**************************** sqlite3_value_  *******************************
33 ** The following routines extract information from a Mem or sqlite3_value
34 ** structure.
35 */
36 const void *sqlite3_value_blob(sqlite3_value *pVal){
37   Mem *p = (Mem*)pVal;
38   if( p->flags & (MEM_Blob|MEM_Str) ){
39     return p->z;
40   }else{
41     return sqlite3_value_text(pVal);
42   }
43 }
44 int sqlite3_value_bytes(sqlite3_value *pVal){
45   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
46 }
47 int sqlite3_value_bytes16(sqlite3_value *pVal){
48   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
49 }
50 double sqlite3_value_double(sqlite3_value *pVal){
51   return sqlite3VdbeRealValue((Mem*)pVal);
52 }
53 int sqlite3_value_int(sqlite3_value *pVal){
54   return sqlite3VdbeIntValue((Mem*)pVal);
55 }
56 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
57   return sqlite3VdbeIntValue((Mem*)pVal);
58 }
59 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
60   return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
61 }
62 #ifndef SQLITE_OMIT_UTF16
63 const void *sqlite3_value_text16(sqlite3_value* pVal){
64   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
65 }
66 const void *sqlite3_value_text16be(sqlite3_value *pVal){
67   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
68 }
69 const void *sqlite3_value_text16le(sqlite3_value *pVal){
70   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
71 }
72 #endif /* SQLITE_OMIT_UTF16 */
73 int sqlite3_value_type(sqlite3_value* pVal){
74   return pVal->type;
75 }
76 
77 /**************************** sqlite3_result_  *******************************
78 ** The following routines are used by user-defined functions to specify
79 ** the function result.
80 */
81 void sqlite3_result_blob(
82   sqlite3_context *pCtx,
83   const void *z,
84   int n,
85   void (*xDel)(void *)
86 ){
87   assert( n>=0 );
88   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
89 }
90 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
91   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
92 }
93 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
94   pCtx->isError = 1;
95   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
96 }
97 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
98   pCtx->isError = 1;
99   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
100 }
101 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
102   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
103 }
104 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
105   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
106 }
107 void sqlite3_result_null(sqlite3_context *pCtx){
108   sqlite3VdbeMemSetNull(&pCtx->s);
109 }
110 void sqlite3_result_text(
111   sqlite3_context *pCtx,
112   const char *z,
113   int n,
114   void (*xDel)(void *)
115 ){
116   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
117 }
118 #ifndef SQLITE_OMIT_UTF16
119 void sqlite3_result_text16(
120   sqlite3_context *pCtx,
121   const void *z,
122   int n,
123   void (*xDel)(void *)
124 ){
125   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
126 }
127 void sqlite3_result_text16be(
128   sqlite3_context *pCtx,
129   const void *z,
130   int n,
131   void (*xDel)(void *)
132 ){
133   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
134 }
135 void sqlite3_result_text16le(
136   sqlite3_context *pCtx,
137   const void *z,
138   int n,
139   void (*xDel)(void *)
140 ){
141   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
142 }
143 #endif /* SQLITE_OMIT_UTF16 */
144 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
145   sqlite3VdbeMemCopy(&pCtx->s, pValue);
146 }
147 
148 
149 /*
150 ** Execute the statement pStmt, either until a row of data is ready, the
151 ** statement is completely executed or an error occurs.
152 */
153 int sqlite3_step(sqlite3_stmt *pStmt){
154   Vdbe *p = (Vdbe*)pStmt;
155   sqlite3 *db;
156   int rc;
157 
158   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
159     return SQLITE_MISUSE;
160   }
161   if( p->aborted ){
162     return SQLITE_ABORT;
163   }
164   if( p->pc<=0 && p->expired ){
165     if( p->rc==SQLITE_OK ){
166       p->rc = SQLITE_SCHEMA;
167     }
168     return SQLITE_ERROR;
169   }
170   db = p->db;
171   if( sqlite3SafetyOn(db) ){
172     p->rc = SQLITE_MISUSE;
173     return SQLITE_MISUSE;
174   }
175   if( p->pc<0 ){
176     /* Invoke the trace callback if there is one
177     */
178     if( (db = p->db)->xTrace && !db->init.busy ){
179       assert( p->nOp>0 );
180       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
181       assert( p->aOp[p->nOp-1].p3!=0 );
182       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
183       sqlite3SafetyOff(db);
184       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
185       if( sqlite3SafetyOn(db) ){
186         p->rc = SQLITE_MISUSE;
187         return SQLITE_MISUSE;
188       }
189     }
190 
191     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
192     ** on in debugging mode.
193     */
194 #ifdef SQLITE_DEBUG
195     if( (db->flags & SQLITE_SqlTrace)!=0 ){
196       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
197     }
198 #endif /* SQLITE_DEBUG */
199 
200     db->activeVdbeCnt++;
201     p->pc = 0;
202   }
203 #ifndef SQLITE_OMIT_EXPLAIN
204   if( p->explain ){
205     rc = sqlite3VdbeList(p);
206   }else
207 #endif /* SQLITE_OMIT_EXPLAIN */
208   {
209     rc = sqlite3VdbeExec(p);
210   }
211 
212   if( sqlite3SafetyOff(db) ){
213     rc = SQLITE_MISUSE;
214   }
215 
216   sqlite3Error(p->db, rc, p->zErrMsg);
217   return rc;
218 }
219 
220 /*
221 ** Extract the user data from a sqlite3_context structure and return a
222 ** pointer to it.
223 */
224 void *sqlite3_user_data(sqlite3_context *p){
225   assert( p && p->pFunc );
226   return p->pFunc->pUserData;
227 }
228 
229 /*
230 ** Allocate or return the aggregate context for a user function.  A new
231 ** context is allocated on the first call.  Subsequent calls return the
232 ** same context that was returned on prior calls.
233 **
234 ** This routine is defined here in vdbe.c because it depends on knowing
235 ** the internals of the sqlite3_context structure which is only defined in
236 ** this source file.
237 */
238 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
239   assert( p && p->pFunc && p->pFunc->xStep );
240   if( p->pAgg==0 ){
241     if( nByte<=NBFS ){
242       p->pAgg = (void*)p->s.z;
243       memset(p->pAgg, 0, nByte);
244     }else{
245       p->pAgg = sqliteMalloc( nByte );
246     }
247   }
248   return p->pAgg;
249 }
250 
251 /*
252 ** Return the auxilary data pointer, if any, for the iArg'th argument to
253 ** the user-function defined by pCtx.
254 */
255 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
256   VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
257   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
258     return 0;
259   }
260   return pVdbeFunc->apAux[iArg].pAux;
261 }
262 
263 /*
264 ** Set the auxilary data pointer and delete function, for the iArg'th
265 ** argument to the user-function defined by pCtx. Any previous value is
266 ** deleted by calling the delete function specified when it was set.
267 */
268 void sqlite3_set_auxdata(
269   sqlite3_context *pCtx,
270   int iArg,
271   void *pAux,
272   void (*xDelete)(void*)
273 ){
274   struct AuxData *pAuxData;
275   VdbeFunc *pVdbeFunc;
276   if( iArg<0 ) return;
277 
278   pVdbeFunc = pCtx->pVdbeFunc;
279   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
280     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
281     pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
282     if( !pVdbeFunc ) return;
283     memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
284              sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
285     pVdbeFunc->nAux = iArg+1;
286     pVdbeFunc->pFunc = pCtx->pFunc;
287   }
288 
289   pAuxData = &pVdbeFunc->apAux[iArg];
290   if( pAuxData->pAux && pAuxData->xDelete ){
291     pAuxData->xDelete(pAuxData->pAux);
292   }
293   pAuxData->pAux = pAux;
294   pAuxData->xDelete = xDelete;
295 }
296 
297 /*
298 ** Return the number of times the Step function of a aggregate has been
299 ** called.
300 **
301 ** This routine is defined here in vdbe.c because it depends on knowing
302 ** the internals of the sqlite3_context structure which is only defined in
303 ** this source file.
304 */
305 int sqlite3_aggregate_count(sqlite3_context *p){
306   assert( p && p->pFunc && p->pFunc->xStep );
307   return p->cnt;
308 }
309 
310 /*
311 ** Return the number of columns in the result set for the statement pStmt.
312 */
313 int sqlite3_column_count(sqlite3_stmt *pStmt){
314   Vdbe *pVm = (Vdbe *)pStmt;
315   return pVm ? pVm->nResColumn : 0;
316 }
317 
318 /*
319 ** Return the number of values available from the current row of the
320 ** currently executing statement pStmt.
321 */
322 int sqlite3_data_count(sqlite3_stmt *pStmt){
323   Vdbe *pVm = (Vdbe *)pStmt;
324   if( pVm==0 || !pVm->resOnStack ) return 0;
325   return pVm->nResColumn;
326 }
327 
328 
329 /*
330 ** Check to see if column iCol of the given statement is valid.  If
331 ** it is, return a pointer to the Mem for the value of that column.
332 ** If iCol is not valid, return a pointer to a Mem which has a value
333 ** of NULL.
334 */
335 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
336   Vdbe *pVm = (Vdbe *)pStmt;
337   int vals = sqlite3_data_count(pStmt);
338   if( i>=vals || i<0 ){
339     static Mem nullMem;
340     if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
341     sqlite3Error(pVm->db, SQLITE_RANGE, 0);
342     return &nullMem;
343   }
344   return &pVm->pTos[(1-vals)+i];
345 }
346 
347 /**************************** sqlite3_column_  *******************************
348 ** The following routines are used to access elements of the current row
349 ** in the result set.
350 */
351 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
352   return sqlite3_value_blob( columnMem(pStmt,i) );
353 }
354 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
355   return sqlite3_value_bytes( columnMem(pStmt,i) );
356 }
357 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
358   return sqlite3_value_bytes16( columnMem(pStmt,i) );
359 }
360 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
361   return sqlite3_value_double( columnMem(pStmt,i) );
362 }
363 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
364   return sqlite3_value_int( columnMem(pStmt,i) );
365 }
366 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
367   return sqlite3_value_int64( columnMem(pStmt,i) );
368 }
369 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
370   return sqlite3_value_text( columnMem(pStmt,i) );
371 }
372 #if 0
373 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
374   return columnMem(pStmt, i);
375 }
376 #endif
377 #ifndef SQLITE_OMIT_UTF16
378 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
379   return sqlite3_value_text16( columnMem(pStmt,i) );
380 }
381 #endif /* SQLITE_OMIT_UTF16 */
382 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
383   return sqlite3_value_type( columnMem(pStmt,i) );
384 }
385 
386 /*
387 ** Convert the N-th element of pStmt->pColName[] into a string using
388 ** xFunc() then return that string.  If N is out of range, return 0.
389 **
390 ** There are up to 5 names for each column.  useType determines which
391 ** name is returned.  Here are the names:
392 **
393 **    0      The column name as it should be displayed for output
394 **    1      The datatype name for the column
395 **    2      The name of the database that the column derives from
396 **    3      The name of the table that the column derives from
397 **    4      The name of the table column that the result column derives from
398 **
399 ** If the result is not a simple column reference (if it is an expression
400 ** or a constant) then useTypes 2, 3, and 4 return NULL.
401 */
402 static const void *columnName(
403   sqlite3_stmt *pStmt,
404   int N,
405   const void *(*xFunc)(Mem*),
406   int useType
407 ){
408   Vdbe *p = (Vdbe *)pStmt;
409   int n = sqlite3_column_count(pStmt);
410 
411   if( p==0 || N>=n || N<0 ){
412     return 0;
413   }
414   N += useType*n;
415   return xFunc(&p->aColName[N]);
416 }
417 
418 
419 /*
420 ** Return the name of the Nth column of the result set returned by SQL
421 ** statement pStmt.
422 */
423 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
424   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
425 }
426 #ifndef SQLITE_OMIT_UTF16
427 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
428   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
429 }
430 #endif
431 
432 /*
433 ** Return the column declaration type (if applicable) of the 'i'th column
434 ** of the result set of SQL statement pStmt.
435 */
436 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
437   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
438 }
439 #ifndef SQLITE_OMIT_UTF16
440 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
441   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
442 }
443 #endif /* SQLITE_OMIT_UTF16 */
444 
445 #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
446 /*
447 ** Return the name of the database from which a result column derives.
448 ** NULL is returned if the result column is an expression or constant or
449 ** anything else which is not an unabiguous reference to a database column.
450 */
451 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
452   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
453 }
454 #ifndef SQLITE_OMIT_UTF16
455 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
456   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
457 }
458 #endif /* SQLITE_OMIT_UTF16 */
459 
460 /*
461 ** Return the name of the table from which a result column derives.
462 ** NULL is returned if the result column is an expression or constant or
463 ** anything else which is not an unabiguous reference to a database column.
464 */
465 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
466   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
467 }
468 #ifndef SQLITE_OMIT_UTF16
469 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
470   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
471 }
472 #endif /* SQLITE_OMIT_UTF16 */
473 
474 /*
475 ** Return the name of the table column from which a result column derives.
476 ** NULL is returned if the result column is an expression or constant or
477 ** anything else which is not an unabiguous reference to a database column.
478 */
479 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
480   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
481 }
482 #ifndef SQLITE_OMIT_UTF16
483 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
484   return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
485 }
486 #endif /* SQLITE_OMIT_UTF16 */
487 #endif /* SQLITE_OMIT_ORIGIN_NAMES */
488 
489 
490 
491 
492 /******************************* sqlite3_bind_  ***************************
493 **
494 ** Routines used to attach values to wildcards in a compiled SQL statement.
495 */
496 /*
497 ** Unbind the value bound to variable i in virtual machine p. This is the
498 ** the same as binding a NULL value to the column. If the "i" parameter is
499 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
500 **
501 ** The error code stored in database p->db is overwritten with the return
502 ** value in any case.
503 */
504 static int vdbeUnbind(Vdbe *p, int i){
505   Mem *pVar;
506   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
507     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
508     return SQLITE_MISUSE;
509   }
510   if( i<1 || i>p->nVar ){
511     sqlite3Error(p->db, SQLITE_RANGE, 0);
512     return SQLITE_RANGE;
513   }
514   i--;
515   pVar = &p->aVar[i];
516   sqlite3VdbeMemRelease(pVar);
517   pVar->flags = MEM_Null;
518   sqlite3Error(p->db, SQLITE_OK, 0);
519   return SQLITE_OK;
520 }
521 
522 /*
523 ** Bind a text or BLOB value.
524 */
525 static int bindText(
526   sqlite3_stmt *pStmt,
527   int i,
528   const void *zData,
529   int nData,
530   void (*xDel)(void*),
531   int encoding
532 ){
533   Vdbe *p = (Vdbe *)pStmt;
534   Mem *pVar;
535   int rc;
536 
537   rc = vdbeUnbind(p, i);
538   if( rc || zData==0 ){
539     return rc;
540   }
541   pVar = &p->aVar[i-1];
542   rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
543   if( rc ){
544     return rc;
545   }
546   if( rc==SQLITE_OK && encoding!=0 ){
547     rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
548   }
549   return rc;
550 }
551 
552 
553 /*
554 ** Bind a blob value to an SQL statement variable.
555 */
556 int sqlite3_bind_blob(
557   sqlite3_stmt *pStmt,
558   int i,
559   const void *zData,
560   int nData,
561   void (*xDel)(void*)
562 ){
563   return bindText(pStmt, i, zData, nData, xDel, 0);
564 }
565 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
566   int rc;
567   Vdbe *p = (Vdbe *)pStmt;
568   rc = vdbeUnbind(p, i);
569   if( rc==SQLITE_OK ){
570     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
571   }
572   return rc;
573 }
574 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
575   return sqlite3_bind_int64(p, i, (i64)iValue);
576 }
577 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
578   int rc;
579   Vdbe *p = (Vdbe *)pStmt;
580   rc = vdbeUnbind(p, i);
581   if( rc==SQLITE_OK ){
582     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
583   }
584   return rc;
585 }
586 int sqlite3_bind_null(sqlite3_stmt* p, int i){
587   return vdbeUnbind((Vdbe *)p, i);
588 }
589 int sqlite3_bind_text(
590   sqlite3_stmt *pStmt,
591   int i,
592   const char *zData,
593   int nData,
594   void (*xDel)(void*)
595 ){
596   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
597 }
598 #ifndef SQLITE_OMIT_UTF16
599 int sqlite3_bind_text16(
600   sqlite3_stmt *pStmt,
601   int i,
602   const void *zData,
603   int nData,
604   void (*xDel)(void*)
605 ){
606   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
607 }
608 #endif /* SQLITE_OMIT_UTF16 */
609 
610 /*
611 ** Return the number of wildcards that can be potentially bound to.
612 ** This routine is added to support DBD::SQLite.
613 */
614 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
615   Vdbe *p = (Vdbe*)pStmt;
616   return p ? p->nVar : 0;
617 }
618 
619 /*
620 ** Create a mapping from variable numbers to variable names
621 ** in the Vdbe.azVar[] array, if such a mapping does not already
622 ** exist.
623 */
624 static void createVarMap(Vdbe *p){
625   if( !p->okVar ){
626     int j;
627     Op *pOp;
628     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
629       if( pOp->opcode==OP_Variable ){
630         assert( pOp->p1>0 && pOp->p1<=p->nVar );
631         p->azVar[pOp->p1-1] = pOp->p3;
632       }
633     }
634     p->okVar = 1;
635   }
636 }
637 
638 /*
639 ** Return the name of a wildcard parameter.  Return NULL if the index
640 ** is out of range or if the wildcard is unnamed.
641 **
642 ** The result is always UTF-8.
643 */
644 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
645   Vdbe *p = (Vdbe*)pStmt;
646   if( p==0 || i<1 || i>p->nVar ){
647     return 0;
648   }
649   createVarMap(p);
650   return p->azVar[i-1];
651 }
652 
653 /*
654 ** Given a wildcard parameter name, return the index of the variable
655 ** with that name.  If there is no variable with the given name,
656 ** return 0.
657 */
658 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
659   Vdbe *p = (Vdbe*)pStmt;
660   int i;
661   if( p==0 ){
662     return 0;
663   }
664   createVarMap(p);
665   if( zName ){
666     for(i=0; i<p->nVar; i++){
667       const char *z = p->azVar[i];
668       if( z && strcmp(z,zName)==0 ){
669         return i+1;
670       }
671     }
672   }
673   return 0;
674 }
675 
676 /*
677 ** Transfer all bindings from the first statement over to the second.
678 ** If the two statements contain a different number of bindings, then
679 ** an SQLITE_ERROR is returned.
680 */
681 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
682   Vdbe *pFrom = (Vdbe*)pFromStmt;
683   Vdbe *pTo = (Vdbe*)pToStmt;
684   int i, rc = SQLITE_OK;
685   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
686     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
687     return SQLITE_MISUSE;
688   }
689   if( pFrom->nVar!=pTo->nVar ){
690     return SQLITE_ERROR;
691   }
692   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
693     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
694   }
695   return rc;
696 }
697 
698 /*
699 ** Return the sqlite3* database handle to which the prepared statement given
700 ** in the argument belongs.  This is the same database handle that was
701 ** the first argument to the sqlite3_prepare() that was used to create
702 ** the statement in the first place.
703 */
704 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
705   return pStmt ? ((Vdbe*)pStmt)->db : 0;
706 }
707