xref: /sqlite-3.40.0/src/vdbeapi.c (revision 27a770e0)
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 /*
33 ** The following routine destroys a virtual machine that is created by
34 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
35 ** success/failure code that describes the result of executing the virtual
36 ** machine.
37 **
38 ** This routine sets the error code and string returned by
39 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
40 */
41 int sqlite3_finalize(sqlite3_stmt *pStmt){
42   int rc;
43   if( pStmt==0 ){
44     rc = SQLITE_OK;
45   }else{
46     Vdbe *v = (Vdbe*)pStmt;
47     sqlite3_mutex *mutex = v->db->mutex;
48     sqlite3_mutex_enter(mutex);
49     rc = sqlite3VdbeFinalize(v);
50     sqlite3_mutex_leave(mutex);
51   }
52   return rc;
53 }
54 
55 /*
56 ** Terminate the current execution of an SQL statement and reset it
57 ** back to its starting state so that it can be reused. A success code from
58 ** the prior execution is returned.
59 **
60 ** This routine sets the error code and string returned by
61 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
62 */
63 int sqlite3_reset(sqlite3_stmt *pStmt){
64   int rc;
65   if( pStmt==0 ){
66     rc = SQLITE_OK;
67   }else{
68     Vdbe *v = (Vdbe*)pStmt;
69     sqlite3_mutex_enter(v->db->mutex);
70     rc = sqlite3VdbeReset(v);
71     sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
72     assert( (rc & (v->db->errMask))==rc );
73     sqlite3_mutex_leave(v->db->mutex);
74   }
75   return rc;
76 }
77 
78 /*
79 ** Set all the parameters in the compiled SQL statement to NULL.
80 */
81 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
82   int i;
83   int rc = SQLITE_OK;
84   Vdbe *v = (Vdbe*)pStmt;
85   sqlite3_mutex_enter(v->db->mutex);
86   for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
87     rc = sqlite3_bind_null(pStmt, i);
88   }
89   sqlite3_mutex_leave(v->db->mutex);
90   return rc;
91 }
92 
93 
94 /**************************** sqlite3_value_  *******************************
95 ** The following routines extract information from a Mem or sqlite3_value
96 ** structure.
97 */
98 const void *sqlite3_value_blob(sqlite3_value *pVal){
99   Mem *p = (Mem*)pVal;
100   if( p->flags & (MEM_Blob|MEM_Str) ){
101     sqlite3VdbeMemExpandBlob(p);
102     p->flags &= ~MEM_Str;
103     p->flags |= MEM_Blob;
104     return p->z;
105   }else{
106     return sqlite3_value_text(pVal);
107   }
108 }
109 int sqlite3_value_bytes(sqlite3_value *pVal){
110   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
111 }
112 int sqlite3_value_bytes16(sqlite3_value *pVal){
113   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
114 }
115 double sqlite3_value_double(sqlite3_value *pVal){
116   return sqlite3VdbeRealValue((Mem*)pVal);
117 }
118 int sqlite3_value_int(sqlite3_value *pVal){
119   return sqlite3VdbeIntValue((Mem*)pVal);
120 }
121 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
122   return sqlite3VdbeIntValue((Mem*)pVal);
123 }
124 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
125   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
126 }
127 #ifndef SQLITE_OMIT_UTF16
128 const void *sqlite3_value_text16(sqlite3_value* pVal){
129   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
130 }
131 const void *sqlite3_value_text16be(sqlite3_value *pVal){
132   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
133 }
134 const void *sqlite3_value_text16le(sqlite3_value *pVal){
135   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
136 }
137 #endif /* SQLITE_OMIT_UTF16 */
138 int sqlite3_value_type(sqlite3_value* pVal){
139   return pVal->type;
140 }
141 
142 /**************************** sqlite3_result_  *******************************
143 ** The following routines are used by user-defined functions to specify
144 ** the function result.
145 */
146 void sqlite3_result_blob(
147   sqlite3_context *pCtx,
148   const void *z,
149   int n,
150   void (*xDel)(void *)
151 ){
152   assert( n>=0 );
153   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
154   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
155 }
156 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
157   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
158   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
159 }
160 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
161   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
162   pCtx->isError = 1;
163   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
164 }
165 #ifndef SQLITE_OMIT_UTF16
166 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
167   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
168   pCtx->isError = 1;
169   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
170 }
171 #endif
172 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
173   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
174   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
175 }
176 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
177   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
178   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
179 }
180 void sqlite3_result_null(sqlite3_context *pCtx){
181   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
182   sqlite3VdbeMemSetNull(&pCtx->s);
183 }
184 void sqlite3_result_text(
185   sqlite3_context *pCtx,
186   const char *z,
187   int n,
188   void (*xDel)(void *)
189 ){
190   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
191   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
192 }
193 #ifndef SQLITE_OMIT_UTF16
194 void sqlite3_result_text16(
195   sqlite3_context *pCtx,
196   const void *z,
197   int n,
198   void (*xDel)(void *)
199 ){
200   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
201   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
202 }
203 void sqlite3_result_text16be(
204   sqlite3_context *pCtx,
205   const void *z,
206   int n,
207   void (*xDel)(void *)
208 ){
209   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
210   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
211 }
212 void sqlite3_result_text16le(
213   sqlite3_context *pCtx,
214   const void *z,
215   int n,
216   void (*xDel)(void *)
217 ){
218   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
219   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
220 }
221 #endif /* SQLITE_OMIT_UTF16 */
222 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
223   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
224   sqlite3VdbeMemCopy(&pCtx->s, pValue);
225 }
226 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
227   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
228   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
229 }
230 
231 /* Force an SQLITE_TOOBIG error. */
232 void sqlite3_result_error_toobig(sqlite3_context *pCtx){
233   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
234   sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
235 }
236 
237 
238 /*
239 ** Execute the statement pStmt, either until a row of data is ready, the
240 ** statement is completely executed or an error occurs.
241 **
242 ** This routine implements the bulk of the logic behind the sqlite_step()
243 ** API.  The only thing omitted is the automatic recompile if a
244 ** schema change has occurred.  That detail is handled by the
245 ** outer sqlite3_step() wrapper procedure.
246 */
247 static int sqlite3Step(Vdbe *p){
248   sqlite3 *db;
249   int rc;
250 
251   /* Assert that malloc() has not failed */
252   db = p->db;
253   assert( !db->mallocFailed );
254 
255   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
256     return SQLITE_MISUSE;
257   }
258   if( p->aborted ){
259     return SQLITE_ABORT;
260   }
261   if( p->pc<=0 && p->expired ){
262     if( p->rc==SQLITE_OK ){
263       p->rc = SQLITE_SCHEMA;
264     }
265     rc = SQLITE_ERROR;
266     goto end_of_step;
267   }
268   if( sqlite3SafetyOn(db) ){
269     p->rc = SQLITE_MISUSE;
270     return SQLITE_MISUSE;
271   }
272   if( p->pc<0 ){
273     /* If there are no other statements currently running, then
274     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
275     ** from interrupting a statement that has not yet started.
276     */
277     if( db->activeVdbeCnt==0 ){
278       db->u1.isInterrupted = 0;
279     }
280 
281 #ifndef SQLITE_OMIT_TRACE
282     /* Invoke the trace callback if there is one
283     */
284     if( db->xTrace && !db->init.busy ){
285       assert( p->nOp>0 );
286       assert( p->aOp[p->nOp-1].opcode==OP_Noop );
287       assert( p->aOp[p->nOp-1].p3!=0 );
288       assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
289       sqlite3SafetyOff(db);
290       db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
291       if( sqlite3SafetyOn(db) ){
292         p->rc = SQLITE_MISUSE;
293         return SQLITE_MISUSE;
294       }
295     }
296     if( db->xProfile && !db->init.busy ){
297       double rNow;
298       sqlite3OsCurrentTime(db->pVfs, &rNow);
299       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
300     }
301 #endif
302 
303     /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
304     ** on in debugging mode.
305     */
306 #ifdef SQLITE_DEBUG
307     if( (db->flags & SQLITE_SqlTrace)!=0 ){
308       sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
309     }
310 #endif /* SQLITE_DEBUG */
311 
312     db->activeVdbeCnt++;
313     p->pc = 0;
314   }
315 #ifndef SQLITE_OMIT_EXPLAIN
316   if( p->explain ){
317     rc = sqlite3VdbeList(p);
318   }else
319 #endif /* SQLITE_OMIT_EXPLAIN */
320   {
321     rc = sqlite3VdbeExec(p);
322   }
323 
324   if( sqlite3SafetyOff(db) ){
325     rc = SQLITE_MISUSE;
326   }
327 
328 #ifndef SQLITE_OMIT_TRACE
329   /* Invoke the profile callback if there is one
330   */
331   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
332     double rNow;
333     u64 elapseTime;
334 
335     sqlite3OsCurrentTime(db->pVfs, &rNow);
336     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
337     assert( p->nOp>0 );
338     assert( p->aOp[p->nOp-1].opcode==OP_Noop );
339     assert( p->aOp[p->nOp-1].p3!=0 );
340     assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
341     db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
342   }
343 #endif
344 
345   sqlite3Error(p->db, rc, 0);
346   p->rc = sqlite3ApiExit(p->db, p->rc);
347 end_of_step:
348   assert( (rc&0xff)==rc );
349   if( p->zSql && (rc&0xff)<SQLITE_ROW ){
350     /* This behavior occurs if sqlite3_prepare_v2() was used to build
351     ** the prepared statement.  Return error codes directly */
352     sqlite3Error(p->db, p->rc, 0);
353     return p->rc;
354   }else{
355     /* This is for legacy sqlite3_prepare() builds and when the code
356     ** is SQLITE_ROW or SQLITE_DONE */
357     return rc;
358   }
359 }
360 
361 /*
362 ** This is the top-level implementation of sqlite3_step().  Call
363 ** sqlite3Step() to do most of the work.  If a schema error occurs,
364 ** call sqlite3Reprepare() and try again.
365 */
366 #ifdef SQLITE_OMIT_PARSER
367 int sqlite3_step(sqlite3_stmt *pStmt){
368   int rc;
369   Vdbe *v;
370   v = (Vdbe*)pStmt;
371   sqlite3_mutex_enter(v->db->mutex);
372   rc = sqlite3Step(v);
373   sqlite3_mutex_leave(v->db->mutex);
374   return rc;
375 }
376 #else
377 int sqlite3_step(sqlite3_stmt *pStmt){
378   int cnt = 0;
379   int rc;
380   Vdbe *v = (Vdbe*)pStmt;
381   sqlite3_mutex_enter(v->db->mutex);
382   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
383          && cnt++ < 5
384          && sqlite3Reprepare(v) ){
385     sqlite3_reset(pStmt);
386     v->expired = 0;
387   }
388   sqlite3_mutex_leave(v->db->mutex);
389   return rc;
390 }
391 #endif
392 
393 /*
394 ** Extract the user data from a sqlite3_context structure and return a
395 ** pointer to it.
396 */
397 void *sqlite3_user_data(sqlite3_context *p){
398   assert( p && p->pFunc );
399   return p->pFunc->pUserData;
400 }
401 
402 /*
403 ** The following is the implementation of an SQL function that always
404 ** fails with an error message stating that the function is used in the
405 ** wrong context.  The sqlite3_overload_function() API might construct
406 ** SQL function that use this routine so that the functions will exist
407 ** for name resolution but are actually overloaded by the xFindFunction
408 ** method of virtual tables.
409 */
410 void sqlite3InvalidFunction(
411   sqlite3_context *context,  /* The function calling context */
412   int argc,                  /* Number of arguments to the function */
413   sqlite3_value **argv       /* Value of each argument */
414 ){
415   const char *zName = context->pFunc->zName;
416   char *zErr;
417   zErr = sqlite3MPrintf(0,
418       "unable to use function %s in the requested context", zName);
419   sqlite3_result_error(context, zErr, -1);
420   sqlite3_free(zErr);
421 }
422 
423 /*
424 ** Allocate or return the aggregate context for a user function.  A new
425 ** context is allocated on the first call.  Subsequent calls return the
426 ** same context that was returned on prior calls.
427 */
428 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
429   Mem *pMem;
430   assert( p && p->pFunc && p->pFunc->xStep );
431   assert( sqlite3_mutex_held(p->s.db->mutex) );
432   pMem = p->pMem;
433   if( (pMem->flags & MEM_Agg)==0 ){
434     if( nByte==0 ){
435       assert( pMem->flags==MEM_Null );
436       pMem->z = 0;
437     }else{
438       pMem->flags = MEM_Agg;
439       pMem->xDel = sqlite3_free;
440       pMem->u.pDef = p->pFunc;
441       if( nByte<=NBFS ){
442         pMem->z = pMem->zShort;
443         memset(pMem->z, 0, nByte);
444       }else{
445         pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
446       }
447     }
448   }
449   return (void*)pMem->z;
450 }
451 
452 /*
453 ** Return the auxilary data pointer, if any, for the iArg'th argument to
454 ** the user-function defined by pCtx.
455 */
456 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
457   VdbeFunc *pVdbeFunc;
458 
459   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
460   pVdbeFunc = pCtx->pVdbeFunc;
461   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
462     return 0;
463   }
464   return pVdbeFunc->apAux[iArg].pAux;
465 }
466 
467 /*
468 ** Set the auxilary data pointer and delete function, for the iArg'th
469 ** argument to the user-function defined by pCtx. Any previous value is
470 ** deleted by calling the delete function specified when it was set.
471 */
472 void sqlite3_set_auxdata(
473   sqlite3_context *pCtx,
474   int iArg,
475   void *pAux,
476   void (*xDelete)(void*)
477 ){
478   struct AuxData *pAuxData;
479   VdbeFunc *pVdbeFunc;
480   if( iArg<0 ) goto failed;
481 
482   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
483   pVdbeFunc = pCtx->pVdbeFunc;
484   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
485     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
486     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
487     pVdbeFunc = sqlite3_realloc(pVdbeFunc, nMalloc);
488     if( !pVdbeFunc ){
489       pCtx->s.db->mallocFailed = 1;
490       goto failed;
491     }
492     pCtx->pVdbeFunc = pVdbeFunc;
493     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
494     pVdbeFunc->nAux = iArg+1;
495     pVdbeFunc->pFunc = pCtx->pFunc;
496   }
497 
498   pAuxData = &pVdbeFunc->apAux[iArg];
499   if( pAuxData->pAux && pAuxData->xDelete ){
500     pAuxData->xDelete(pAuxData->pAux);
501   }
502   pAuxData->pAux = pAux;
503   pAuxData->xDelete = xDelete;
504   return;
505 
506 failed:
507   if( xDelete ){
508     xDelete(pAux);
509   }
510 }
511 
512 /*
513 ** Return the number of times the Step function of a aggregate has been
514 ** called.
515 **
516 ** This function is deprecated.  Do not use it for new code.  It is
517 ** provide only to avoid breaking legacy code.  New aggregate function
518 ** implementations should keep their own counts within their aggregate
519 ** context.
520 */
521 int sqlite3_aggregate_count(sqlite3_context *p){
522   assert( p && p->pFunc && p->pFunc->xStep );
523   return p->pMem->n;
524 }
525 
526 /*
527 ** Return the number of columns in the result set for the statement pStmt.
528 */
529 int sqlite3_column_count(sqlite3_stmt *pStmt){
530   Vdbe *pVm = (Vdbe *)pStmt;
531   return pVm ? pVm->nResColumn : 0;
532 }
533 
534 /*
535 ** Return the number of values available from the current row of the
536 ** currently executing statement pStmt.
537 */
538 int sqlite3_data_count(sqlite3_stmt *pStmt){
539   Vdbe *pVm = (Vdbe *)pStmt;
540   if( pVm==0 || !pVm->resOnStack ) return 0;
541   return pVm->nResColumn;
542 }
543 
544 
545 /*
546 ** Check to see if column iCol of the given statement is valid.  If
547 ** it is, return a pointer to the Mem for the value of that column.
548 ** If iCol is not valid, return a pointer to a Mem which has a value
549 ** of NULL.
550 */
551 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
552   Vdbe *pVm;
553   int vals;
554   Mem *pOut;
555 
556   pVm = (Vdbe *)pStmt;
557   if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
558     sqlite3_mutex_enter(pVm->db->mutex);
559     vals = sqlite3_data_count(pStmt);
560     pOut = &pVm->pTos[(1-vals)+i];
561   }else{
562     static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
563     if( pVm->db ){
564       sqlite3_mutex_enter(pVm->db->mutex);
565       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
566     }
567     pOut = (Mem*)&nullMem;
568   }
569   return pOut;
570 }
571 
572 /*
573 ** This function is called after invoking an sqlite3_value_XXX function on a
574 ** column value (i.e. a value returned by evaluating an SQL expression in the
575 ** select list of a SELECT statement) that may cause a malloc() failure. If
576 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
577 ** code of statement pStmt set to SQLITE_NOMEM.
578 **
579 ** Specifically, this is called from within:
580 **
581 **     sqlite3_column_int()
582 **     sqlite3_column_int64()
583 **     sqlite3_column_text()
584 **     sqlite3_column_text16()
585 **     sqlite3_column_real()
586 **     sqlite3_column_bytes()
587 **     sqlite3_column_bytes16()
588 **
589 ** But not for sqlite3_column_blob(), which never calls malloc().
590 */
591 static void columnMallocFailure(sqlite3_stmt *pStmt)
592 {
593   /* If malloc() failed during an encoding conversion within an
594   ** sqlite3_column_XXX API, then set the return code of the statement to
595   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
596   ** and _finalize() will return NOMEM.
597   */
598   Vdbe *p = (Vdbe *)pStmt;
599   if( p ){
600     p->rc = sqlite3ApiExit(p->db, p->rc);
601     sqlite3_mutex_leave(p->db->mutex);
602   }
603 }
604 
605 /**************************** sqlite3_column_  *******************************
606 ** The following routines are used to access elements of the current row
607 ** in the result set.
608 */
609 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
610   const void *val;
611   val = sqlite3_value_blob( columnMem(pStmt,i) );
612   /* Even though there is no encoding conversion, value_blob() might
613   ** need to call malloc() to expand the result of a zeroblob()
614   ** expression.
615   */
616   columnMallocFailure(pStmt);
617   return val;
618 }
619 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
620   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
621   columnMallocFailure(pStmt);
622   return val;
623 }
624 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
625   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
626   columnMallocFailure(pStmt);
627   return val;
628 }
629 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
630   double val = sqlite3_value_double( columnMem(pStmt,i) );
631   columnMallocFailure(pStmt);
632   return val;
633 }
634 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
635   int val = sqlite3_value_int( columnMem(pStmt,i) );
636   columnMallocFailure(pStmt);
637   return val;
638 }
639 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
640   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
641   columnMallocFailure(pStmt);
642   return val;
643 }
644 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
645   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
646   columnMallocFailure(pStmt);
647   return val;
648 }
649 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
650   sqlite3_value *pOut = columnMem(pStmt, i);
651   columnMallocFailure(pStmt);
652   return pOut;
653 }
654 #ifndef SQLITE_OMIT_UTF16
655 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
656   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
657   columnMallocFailure(pStmt);
658   return val;
659 }
660 #endif /* SQLITE_OMIT_UTF16 */
661 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
662   int iType = sqlite3_value_type( columnMem(pStmt,i) );
663   columnMallocFailure(pStmt);
664   return iType;
665 }
666 
667 /* The following function is experimental and subject to change or
668 ** removal */
669 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
670 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
671 **}
672 */
673 
674 /*
675 ** Convert the N-th element of pStmt->pColName[] into a string using
676 ** xFunc() then return that string.  If N is out of range, return 0.
677 **
678 ** There are up to 5 names for each column.  useType determines which
679 ** name is returned.  Here are the names:
680 **
681 **    0      The column name as it should be displayed for output
682 **    1      The datatype name for the column
683 **    2      The name of the database that the column derives from
684 **    3      The name of the table that the column derives from
685 **    4      The name of the table column that the result column derives from
686 **
687 ** If the result is not a simple column reference (if it is an expression
688 ** or a constant) then useTypes 2, 3, and 4 return NULL.
689 */
690 static const void *columnName(
691   sqlite3_stmt *pStmt,
692   int N,
693   const void *(*xFunc)(Mem*),
694   int useType
695 ){
696   const void *ret = 0;
697   Vdbe *p = (Vdbe *)pStmt;
698   int n;
699 
700 
701   if( p!=0 ){
702     n = sqlite3_column_count(pStmt);
703     if( N<n && N>=0 ){
704       N += useType*n;
705       sqlite3_mutex_enter(p->db->mutex);
706       ret = xFunc(&p->aColName[N]);
707 
708       /* A malloc may have failed inside of the xFunc() call. If this
709       ** is the case, clear the mallocFailed flag and return NULL.
710       */
711       if( p->db && p->db->mallocFailed ){
712         p->db->mallocFailed = 0;
713         ret = 0;
714       }
715       sqlite3_mutex_leave(p->db->mutex);
716     }
717   }
718   return ret;
719 }
720 
721 /*
722 ** Return the name of the Nth column of the result set returned by SQL
723 ** statement pStmt.
724 */
725 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
726   return columnName(
727       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
728 }
729 #ifndef SQLITE_OMIT_UTF16
730 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
731   return columnName(
732       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
733 }
734 #endif
735 
736 /*
737 ** Return the column declaration type (if applicable) of the 'i'th column
738 ** of the result set of SQL statement pStmt.
739 */
740 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
741   return columnName(
742       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
743 }
744 #ifndef SQLITE_OMIT_UTF16
745 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
746   return columnName(
747       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
748 }
749 #endif /* SQLITE_OMIT_UTF16 */
750 
751 #ifdef SQLITE_ENABLE_COLUMN_METADATA
752 /*
753 ** Return the name of the database from which a result column derives.
754 ** NULL is returned if the result column is an expression or constant or
755 ** anything else which is not an unabiguous reference to a database column.
756 */
757 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
758   return columnName(
759       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
760 }
761 #ifndef SQLITE_OMIT_UTF16
762 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
763   return columnName(
764       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
765 }
766 #endif /* SQLITE_OMIT_UTF16 */
767 
768 /*
769 ** Return the name of the table from which a result column derives.
770 ** NULL is returned if the result column is an expression or constant or
771 ** anything else which is not an unabiguous reference to a database column.
772 */
773 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
774   return columnName(
775       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
776 }
777 #ifndef SQLITE_OMIT_UTF16
778 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
779   return columnName(
780       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
781 }
782 #endif /* SQLITE_OMIT_UTF16 */
783 
784 /*
785 ** Return the name of the table column from which a result column derives.
786 ** NULL is returned if the result column is an expression or constant or
787 ** anything else which is not an unabiguous reference to a database column.
788 */
789 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
790   return columnName(
791       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
792 }
793 #ifndef SQLITE_OMIT_UTF16
794 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
795   return columnName(
796       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
797 }
798 #endif /* SQLITE_OMIT_UTF16 */
799 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
800 
801 
802 /******************************* sqlite3_bind_  ***************************
803 **
804 ** Routines used to attach values to wildcards in a compiled SQL statement.
805 */
806 /*
807 ** Unbind the value bound to variable i in virtual machine p. This is the
808 ** the same as binding a NULL value to the column. If the "i" parameter is
809 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
810 **
811 ** The error code stored in database p->db is overwritten with the return
812 ** value in any case.
813 */
814 static int vdbeUnbind(Vdbe *p, int i){
815   Mem *pVar;
816   if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
817     if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
818     return SQLITE_MISUSE;
819   }
820   if( i<1 || i>p->nVar ){
821     sqlite3Error(p->db, SQLITE_RANGE, 0);
822     return SQLITE_RANGE;
823   }
824   i--;
825   pVar = &p->aVar[i];
826   sqlite3VdbeMemRelease(pVar);
827   pVar->flags = MEM_Null;
828   sqlite3Error(p->db, SQLITE_OK, 0);
829   return SQLITE_OK;
830 }
831 
832 /*
833 ** Bind a text or BLOB value.
834 */
835 static int bindText(
836   sqlite3_stmt *pStmt,   /* The statement to bind against */
837   int i,                 /* Index of the parameter to bind */
838   const void *zData,     /* Pointer to the data to be bound */
839   int nData,             /* Number of bytes of data to be bound */
840   void (*xDel)(void*),   /* Destructor for the data */
841   int encoding           /* Encoding for the data */
842 ){
843   Vdbe *p = (Vdbe *)pStmt;
844   Mem *pVar;
845   int rc;
846 
847   if( p==0 ){
848     return SQLITE_MISUSE;
849   }
850   sqlite3_mutex_enter(p->db->mutex);
851   rc = vdbeUnbind(p, i);
852   if( rc==SQLITE_OK && zData!=0 ){
853     pVar = &p->aVar[i-1];
854     rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
855     if( rc==SQLITE_OK && encoding!=0 ){
856       rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
857     }
858     sqlite3Error(p->db, rc, 0);
859     rc = sqlite3ApiExit(p->db, rc);
860   }
861   sqlite3_mutex_leave(p->db->mutex);
862   return rc;
863 }
864 
865 
866 /*
867 ** Bind a blob value to an SQL statement variable.
868 */
869 int sqlite3_bind_blob(
870   sqlite3_stmt *pStmt,
871   int i,
872   const void *zData,
873   int nData,
874   void (*xDel)(void*)
875 ){
876   return bindText(pStmt, i, zData, nData, xDel, 0);
877 }
878 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
879   int rc;
880   Vdbe *p = (Vdbe *)pStmt;
881   sqlite3_mutex_enter(p->db->mutex);
882   rc = vdbeUnbind(p, i);
883   if( rc==SQLITE_OK ){
884     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
885   }
886   sqlite3_mutex_leave(p->db->mutex);
887   return rc;
888 }
889 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
890   return sqlite3_bind_int64(p, i, (i64)iValue);
891 }
892 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
893   int rc;
894   Vdbe *p = (Vdbe *)pStmt;
895   sqlite3_mutex_enter(p->db->mutex);
896   rc = vdbeUnbind(p, i);
897   if( rc==SQLITE_OK ){
898     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
899   }
900   sqlite3_mutex_leave(p->db->mutex);
901   return rc;
902 }
903 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
904   int rc;
905   Vdbe *p = (Vdbe*)pStmt;
906   sqlite3_mutex_enter(p->db->mutex);
907   rc = vdbeUnbind(p, i);
908   sqlite3_mutex_leave(p->db->mutex);
909   return rc;
910 }
911 int sqlite3_bind_text(
912   sqlite3_stmt *pStmt,
913   int i,
914   const char *zData,
915   int nData,
916   void (*xDel)(void*)
917 ){
918   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
919 }
920 #ifndef SQLITE_OMIT_UTF16
921 int sqlite3_bind_text16(
922   sqlite3_stmt *pStmt,
923   int i,
924   const void *zData,
925   int nData,
926   void (*xDel)(void*)
927 ){
928   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
929 }
930 #endif /* SQLITE_OMIT_UTF16 */
931 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
932   int rc;
933   Vdbe *p = (Vdbe *)pStmt;
934   sqlite3_mutex_enter(p->db->mutex);
935   rc = vdbeUnbind(p, i);
936   if( rc==SQLITE_OK ){
937     rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
938   }
939   sqlite3_mutex_leave(p->db->mutex);
940   return rc;
941 }
942 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
943   int rc;
944   Vdbe *p = (Vdbe *)pStmt;
945   sqlite3_mutex_enter(p->db->mutex);
946   rc = vdbeUnbind(p, i);
947   if( rc==SQLITE_OK ){
948     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
949   }
950   sqlite3_mutex_leave(p->db->mutex);
951   return rc;
952 }
953 
954 /*
955 ** Return the number of wildcards that can be potentially bound to.
956 ** This routine is added to support DBD::SQLite.
957 */
958 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
959   Vdbe *p = (Vdbe*)pStmt;
960   return p ? p->nVar : 0;
961 }
962 
963 /*
964 ** Create a mapping from variable numbers to variable names
965 ** in the Vdbe.azVar[] array, if such a mapping does not already
966 ** exist.
967 */
968 static void createVarMap(Vdbe *p){
969   if( !p->okVar ){
970     sqlite3_mutex_enter(p->db->mutex);
971     if( !p->okVar ){
972       int j;
973       Op *pOp;
974       for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
975         if( pOp->opcode==OP_Variable ){
976           assert( pOp->p1>0 && pOp->p1<=p->nVar );
977           p->azVar[pOp->p1-1] = pOp->p3;
978         }
979       }
980       p->okVar = 1;
981     }
982     sqlite3_mutex_leave(p->db->mutex);
983   }
984 }
985 
986 /*
987 ** Return the name of a wildcard parameter.  Return NULL if the index
988 ** is out of range or if the wildcard is unnamed.
989 **
990 ** The result is always UTF-8.
991 */
992 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
993   Vdbe *p = (Vdbe*)pStmt;
994   if( p==0 || i<1 || i>p->nVar ){
995     return 0;
996   }
997   createVarMap(p);
998   return p->azVar[i-1];
999 }
1000 
1001 /*
1002 ** Given a wildcard parameter name, return the index of the variable
1003 ** with that name.  If there is no variable with the given name,
1004 ** return 0.
1005 */
1006 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1007   Vdbe *p = (Vdbe*)pStmt;
1008   int i;
1009   if( p==0 ){
1010     return 0;
1011   }
1012   createVarMap(p);
1013   if( zName ){
1014     for(i=0; i<p->nVar; i++){
1015       const char *z = p->azVar[i];
1016       if( z && strcmp(z,zName)==0 ){
1017         return i+1;
1018       }
1019     }
1020   }
1021   return 0;
1022 }
1023 
1024 /*
1025 ** Transfer all bindings from the first statement over to the second.
1026 ** If the two statements contain a different number of bindings, then
1027 ** an SQLITE_ERROR is returned.
1028 */
1029 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1030   Vdbe *pFrom = (Vdbe*)pFromStmt;
1031   Vdbe *pTo = (Vdbe*)pToStmt;
1032   int i, rc = SQLITE_OK;
1033   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
1034     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1035     || pTo->db!=pFrom->db ){
1036     return SQLITE_MISUSE;
1037   }
1038   if( pFrom->nVar!=pTo->nVar ){
1039     return SQLITE_ERROR;
1040   }
1041   sqlite3_mutex_enter(pTo->db->mutex);
1042   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
1043     sqlite3MallocDisallow();
1044     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1045     sqlite3MallocAllow();
1046   }
1047   sqlite3_mutex_leave(pTo->db->mutex);
1048   assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
1049   return rc;
1050 }
1051 
1052 /*
1053 ** Return the sqlite3* database handle to which the prepared statement given
1054 ** in the argument belongs.  This is the same database handle that was
1055 ** the first argument to the sqlite3_prepare() that was used to create
1056 ** the statement in the first place.
1057 */
1058 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1059   return pStmt ? ((Vdbe*)pStmt)->db : 0;
1060 }
1061