xref: /sqlite-3.40.0/src/vdbeapi.c (revision 78d41832)
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 ** $Id: vdbeapi.c,v 1.150 2008/12/10 18:03:47 drh Exp $
17 */
18 #include "sqliteInt.h"
19 #include "vdbeInt.h"
20 
21 #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
22 /*
23 ** The following structure contains pointers to the end points of a
24 ** doubly-linked list of all compiled SQL statements that may be holding
25 ** buffers eligible for release when the sqlite3_release_memory() interface is
26 ** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
27 ** mutex.
28 **
29 ** Statements are added to the end of this list when sqlite3_reset() is
30 ** called. They are removed either when sqlite3_step() or sqlite3_finalize()
31 ** is called. When statements are added to this list, the associated
32 ** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
33 ** can be freed using sqlite3VdbeReleaseMemory().
34 **
35 ** When statements are added or removed from this list, the mutex
36 ** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
37 ** already held. The LRU2 mutex is then obtained, blocking if necessary,
38 ** the linked-list pointers manipulated and the LRU2 mutex relinquished.
39 */
40 struct StatementLruList {
41   Vdbe *pFirst;
42   Vdbe *pLast;
43 };
44 static struct StatementLruList sqlite3LruStatements;
45 
46 /*
47 ** Check that the list looks to be internally consistent. This is used
48 ** as part of an assert() statement as follows:
49 **
50 **   assert( stmtLruCheck() );
51 */
52 #ifndef NDEBUG
53 static int stmtLruCheck(){
54   Vdbe *p;
55   for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
56     assert(p->pLruNext || p==sqlite3LruStatements.pLast);
57     assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
58     assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
59     assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
60   }
61   return 1;
62 }
63 #endif
64 
65 /*
66 ** Add vdbe p to the end of the statement lru list. It is assumed that
67 ** p is not already part of the list when this is called. The lru list
68 ** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
69 */
70 static void stmtLruAdd(Vdbe *p){
71   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
72 
73   if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
74     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
75     return;
76   }
77 
78   assert( stmtLruCheck() );
79 
80   if( !sqlite3LruStatements.pFirst ){
81     assert( !sqlite3LruStatements.pLast );
82     sqlite3LruStatements.pFirst = p;
83     sqlite3LruStatements.pLast = p;
84   }else{
85     assert( !sqlite3LruStatements.pLast->pLruNext );
86     p->pLruPrev = sqlite3LruStatements.pLast;
87     sqlite3LruStatements.pLast->pLruNext = p;
88     sqlite3LruStatements.pLast = p;
89   }
90 
91   assert( stmtLruCheck() );
92 
93   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
94 }
95 
96 /*
97 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
98 ** statement p from the least-recently-used statement list. If the
99 ** statement is not currently part of the list, this call is a no-op.
100 */
101 static void stmtLruRemoveNomutex(Vdbe *p){
102   if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
103     assert( stmtLruCheck() );
104     if( p->pLruNext ){
105       p->pLruNext->pLruPrev = p->pLruPrev;
106     }else{
107       sqlite3LruStatements.pLast = p->pLruPrev;
108     }
109     if( p->pLruPrev ){
110       p->pLruPrev->pLruNext = p->pLruNext;
111     }else{
112       sqlite3LruStatements.pFirst = p->pLruNext;
113     }
114     p->pLruNext = 0;
115     p->pLruPrev = 0;
116     assert( stmtLruCheck() );
117   }
118 }
119 
120 /*
121 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
122 ** statement p from the least-recently-used statement list. If the
123 ** statement is not currently part of the list, this call is a no-op.
124 */
125 static void stmtLruRemove(Vdbe *p){
126   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
127   stmtLruRemoveNomutex(p);
128   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
129 }
130 
131 /*
132 ** Try to release n bytes of memory by freeing buffers associated
133 ** with the memory registers of currently unused vdbes.
134 */
135 int sqlite3VdbeReleaseMemory(int n){
136   Vdbe *p;
137   Vdbe *pNext;
138   int nFree = 0;
139 
140   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
141   for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
142     pNext = p->pLruNext;
143 
144     /* For each statement handle in the lru list, attempt to obtain the
145     ** associated database mutex. If it cannot be obtained, continue
146     ** to the next statement handle. It is not possible to block on
147     ** the database mutex - that could cause deadlock.
148     */
149     if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
150       nFree += sqlite3VdbeReleaseBuffers(p);
151       stmtLruRemoveNomutex(p);
152       sqlite3_mutex_leave(p->db->mutex);
153     }
154   }
155   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
156 
157   return nFree;
158 }
159 
160 /*
161 ** Call sqlite3Reprepare() on the statement. Remove it from the
162 ** lru list before doing so, as Reprepare() will free all the
163 ** memory register buffers anyway.
164 */
165 int vdbeReprepare(Vdbe *p){
166   stmtLruRemove(p);
167   return sqlite3Reprepare(p);
168 }
169 
170 #else       /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
171   #define stmtLruRemove(x)
172   #define stmtLruAdd(x)
173   #define vdbeReprepare(x) sqlite3Reprepare(x)
174 #endif
175 
176 
177 #ifndef SQLITE_OMIT_DEPRECATED
178 /*
179 ** Return TRUE (non-zero) of the statement supplied as an argument needs
180 ** to be recompiled.  A statement needs to be recompiled whenever the
181 ** execution environment changes in a way that would alter the program
182 ** that sqlite3_prepare() generates.  For example, if new functions or
183 ** collating sequences are registered or if an authorizer function is
184 ** added or changed.
185 */
186 int sqlite3_expired(sqlite3_stmt *pStmt){
187   Vdbe *p = (Vdbe*)pStmt;
188   return p==0 || p->expired;
189 }
190 #endif
191 
192 /*
193 ** The following routine destroys a virtual machine that is created by
194 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
195 ** success/failure code that describes the result of executing the virtual
196 ** machine.
197 **
198 ** This routine sets the error code and string returned by
199 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
200 */
201 int sqlite3_finalize(sqlite3_stmt *pStmt){
202   int rc;
203   if( pStmt==0 ){
204     rc = SQLITE_OK;
205   }else{
206     Vdbe *v = (Vdbe*)pStmt;
207 #if SQLITE_THREADSAFE
208     sqlite3_mutex *mutex = v->db->mutex;
209 #endif
210     sqlite3_mutex_enter(mutex);
211     stmtLruRemove(v);
212     rc = sqlite3VdbeFinalize(v);
213     sqlite3_mutex_leave(mutex);
214   }
215   return rc;
216 }
217 
218 /*
219 ** Terminate the current execution of an SQL statement and reset it
220 ** back to its starting state so that it can be reused. A success code from
221 ** the prior execution is returned.
222 **
223 ** This routine sets the error code and string returned by
224 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
225 */
226 int sqlite3_reset(sqlite3_stmt *pStmt){
227   int rc;
228   if( pStmt==0 ){
229     rc = SQLITE_OK;
230   }else{
231     Vdbe *v = (Vdbe*)pStmt;
232     sqlite3_mutex_enter(v->db->mutex);
233     rc = sqlite3VdbeReset(v);
234     stmtLruAdd(v);
235     sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
236     assert( (rc & (v->db->errMask))==rc );
237     sqlite3_mutex_leave(v->db->mutex);
238   }
239   return rc;
240 }
241 
242 /*
243 ** Set all the parameters in the compiled SQL statement to NULL.
244 */
245 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
246   int i;
247   int rc = SQLITE_OK;
248   Vdbe *p = (Vdbe*)pStmt;
249 #if SQLITE_THREADSAFE
250   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
251 #endif
252   sqlite3_mutex_enter(mutex);
253   for(i=0; i<p->nVar; i++){
254     sqlite3VdbeMemRelease(&p->aVar[i]);
255     p->aVar[i].flags = MEM_Null;
256   }
257   sqlite3_mutex_leave(mutex);
258   return rc;
259 }
260 
261 
262 /**************************** sqlite3_value_  *******************************
263 ** The following routines extract information from a Mem or sqlite3_value
264 ** structure.
265 */
266 const void *sqlite3_value_blob(sqlite3_value *pVal){
267   Mem *p = (Mem*)pVal;
268   if( p->flags & (MEM_Blob|MEM_Str) ){
269     sqlite3VdbeMemExpandBlob(p);
270     p->flags &= ~MEM_Str;
271     p->flags |= MEM_Blob;
272     return p->z;
273   }else{
274     return sqlite3_value_text(pVal);
275   }
276 }
277 int sqlite3_value_bytes(sqlite3_value *pVal){
278   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
279 }
280 int sqlite3_value_bytes16(sqlite3_value *pVal){
281   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
282 }
283 double sqlite3_value_double(sqlite3_value *pVal){
284   return sqlite3VdbeRealValue((Mem*)pVal);
285 }
286 int sqlite3_value_int(sqlite3_value *pVal){
287   return (int)sqlite3VdbeIntValue((Mem*)pVal);
288 }
289 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
290   return sqlite3VdbeIntValue((Mem*)pVal);
291 }
292 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
293   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
294 }
295 #ifndef SQLITE_OMIT_UTF16
296 const void *sqlite3_value_text16(sqlite3_value* pVal){
297   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
298 }
299 const void *sqlite3_value_text16be(sqlite3_value *pVal){
300   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
301 }
302 const void *sqlite3_value_text16le(sqlite3_value *pVal){
303   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
304 }
305 #endif /* SQLITE_OMIT_UTF16 */
306 int sqlite3_value_type(sqlite3_value* pVal){
307   return pVal->type;
308 }
309 
310 /**************************** sqlite3_result_  *******************************
311 ** The following routines are used by user-defined functions to specify
312 ** the function result.
313 */
314 void sqlite3_result_blob(
315   sqlite3_context *pCtx,
316   const void *z,
317   int n,
318   void (*xDel)(void *)
319 ){
320   assert( n>=0 );
321   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
322   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
323 }
324 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
325   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
326   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
327 }
328 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
329   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
330   pCtx->isError = SQLITE_ERROR;
331   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
332 }
333 #ifndef SQLITE_OMIT_UTF16
334 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
335   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
336   pCtx->isError = SQLITE_ERROR;
337   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
338 }
339 #endif
340 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
341   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
342   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
343 }
344 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
345   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
346   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
347 }
348 void sqlite3_result_null(sqlite3_context *pCtx){
349   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
350   sqlite3VdbeMemSetNull(&pCtx->s);
351 }
352 void sqlite3_result_text(
353   sqlite3_context *pCtx,
354   const char *z,
355   int n,
356   void (*xDel)(void *)
357 ){
358   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
359   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
360 }
361 #ifndef SQLITE_OMIT_UTF16
362 void sqlite3_result_text16(
363   sqlite3_context *pCtx,
364   const void *z,
365   int n,
366   void (*xDel)(void *)
367 ){
368   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
369   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
370 }
371 void sqlite3_result_text16be(
372   sqlite3_context *pCtx,
373   const void *z,
374   int n,
375   void (*xDel)(void *)
376 ){
377   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
378   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
379 }
380 void sqlite3_result_text16le(
381   sqlite3_context *pCtx,
382   const void *z,
383   int n,
384   void (*xDel)(void *)
385 ){
386   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
387   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
388 }
389 #endif /* SQLITE_OMIT_UTF16 */
390 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
391   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
392   sqlite3VdbeMemCopy(&pCtx->s, pValue);
393 }
394 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
395   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
396   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
397 }
398 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
399   pCtx->isError = errCode;
400 }
401 
402 /* Force an SQLITE_TOOBIG error. */
403 void sqlite3_result_error_toobig(sqlite3_context *pCtx){
404   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
405   pCtx->isError = SQLITE_TOOBIG;
406   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
407                        SQLITE_UTF8, SQLITE_STATIC);
408 }
409 
410 /* An SQLITE_NOMEM error. */
411 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
412   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
413   sqlite3VdbeMemSetNull(&pCtx->s);
414   pCtx->isError = SQLITE_NOMEM;
415   pCtx->s.db->mallocFailed = 1;
416 }
417 
418 /*
419 ** Execute the statement pStmt, either until a row of data is ready, the
420 ** statement is completely executed or an error occurs.
421 **
422 ** This routine implements the bulk of the logic behind the sqlite_step()
423 ** API.  The only thing omitted is the automatic recompile if a
424 ** schema change has occurred.  That detail is handled by the
425 ** outer sqlite3_step() wrapper procedure.
426 */
427 static int sqlite3Step(Vdbe *p){
428   sqlite3 *db;
429   int rc;
430 
431   assert(p);
432   if( p->magic!=VDBE_MAGIC_RUN ){
433     return SQLITE_MISUSE;
434   }
435 
436   /* Assert that malloc() has not failed */
437   db = p->db;
438   if( db->mallocFailed ){
439     return SQLITE_NOMEM;
440   }
441 
442   if( p->pc<=0 && p->expired ){
443     if( p->rc==SQLITE_OK ){
444       p->rc = SQLITE_SCHEMA;
445     }
446     rc = SQLITE_ERROR;
447     goto end_of_step;
448   }
449   if( sqlite3SafetyOn(db) ){
450     p->rc = SQLITE_MISUSE;
451     return SQLITE_MISUSE;
452   }
453   if( p->pc<0 ){
454     /* If there are no other statements currently running, then
455     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
456     ** from interrupting a statement that has not yet started.
457     */
458     if( db->activeVdbeCnt==0 ){
459       db->u1.isInterrupted = 0;
460     }
461 
462 #ifndef SQLITE_OMIT_TRACE
463     if( db->xProfile && !db->init.busy ){
464       double rNow;
465       sqlite3OsCurrentTime(db->pVfs, &rNow);
466       p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
467     }
468 #endif
469 
470     db->activeVdbeCnt++;
471     if( p->readOnly==0 ) db->writeVdbeCnt++;
472     p->pc = 0;
473     stmtLruRemove(p);
474   }
475 #ifndef SQLITE_OMIT_EXPLAIN
476   if( p->explain ){
477     rc = sqlite3VdbeList(p);
478   }else
479 #endif /* SQLITE_OMIT_EXPLAIN */
480   {
481     rc = sqlite3VdbeExec(p);
482   }
483 
484   if( sqlite3SafetyOff(db) ){
485     rc = SQLITE_MISUSE;
486   }
487 
488 #ifndef SQLITE_OMIT_TRACE
489   /* Invoke the profile callback if there is one
490   */
491   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
492            && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
493     double rNow;
494     u64 elapseTime;
495 
496     sqlite3OsCurrentTime(db->pVfs, &rNow);
497     elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
498     elapseTime -= p->startTime;
499     db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
500   }
501 #endif
502 
503   db->errCode = rc;
504   /*sqlite3Error(p->db, rc, 0);*/
505   p->rc = sqlite3ApiExit(p->db, p->rc);
506 end_of_step:
507   assert( (rc&0xff)==rc );
508   if( p->zSql && (rc&0xff)<SQLITE_ROW ){
509     /* This behavior occurs if sqlite3_prepare_v2() was used to build
510     ** the prepared statement.  Return error codes directly */
511     p->db->errCode = p->rc;
512     /* sqlite3Error(p->db, p->rc, 0); */
513     return p->rc;
514   }else{
515     /* This is for legacy sqlite3_prepare() builds and when the code
516     ** is SQLITE_ROW or SQLITE_DONE */
517     return rc;
518   }
519 }
520 
521 /*
522 ** This is the top-level implementation of sqlite3_step().  Call
523 ** sqlite3Step() to do most of the work.  If a schema error occurs,
524 ** call sqlite3Reprepare() and try again.
525 */
526 #ifdef SQLITE_OMIT_PARSER
527 int sqlite3_step(sqlite3_stmt *pStmt){
528   int rc = SQLITE_MISUSE;
529   if( pStmt ){
530     Vdbe *v;
531     v = (Vdbe*)pStmt;
532     sqlite3_mutex_enter(v->db->mutex);
533     rc = sqlite3Step(v);
534     sqlite3_mutex_leave(v->db->mutex);
535   }
536   return rc;
537 }
538 #else
539 int sqlite3_step(sqlite3_stmt *pStmt){
540   int rc = SQLITE_MISUSE;
541   if( pStmt ){
542     int cnt = 0;
543     Vdbe *v = (Vdbe*)pStmt;
544     sqlite3 *db = v->db;
545     sqlite3_mutex_enter(db->mutex);
546     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
547            && cnt++ < 5
548            && vdbeReprepare(v) ){
549       sqlite3_reset(pStmt);
550       v->expired = 0;
551     }
552     if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
553       /* This case occurs after failing to recompile an sql statement.
554       ** The error message from the SQL compiler has already been loaded
555       ** into the database handle. This block copies the error message
556       ** from the database handle into the statement and sets the statement
557       ** program counter to 0 to ensure that when the statement is
558       ** finalized or reset the parser error message is available via
559       ** sqlite3_errmsg() and sqlite3_errcode().
560       */
561       const char *zErr = (const char *)sqlite3_value_text(db->pErr);
562       sqlite3DbFree(db, v->zErrMsg);
563       if( !db->mallocFailed ){
564         v->zErrMsg = sqlite3DbStrDup(db, zErr);
565       } else {
566         v->zErrMsg = 0;
567         v->rc = SQLITE_NOMEM;
568       }
569     }
570     rc = sqlite3ApiExit(db, rc);
571     sqlite3_mutex_leave(db->mutex);
572   }
573   return rc;
574 }
575 #endif
576 
577 /*
578 ** Extract the user data from a sqlite3_context structure and return a
579 ** pointer to it.
580 */
581 void *sqlite3_user_data(sqlite3_context *p){
582   assert( p && p->pFunc );
583   return p->pFunc->pUserData;
584 }
585 
586 /*
587 ** Extract the user data from a sqlite3_context structure and return a
588 ** pointer to it.
589 */
590 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
591   assert( p && p->pFunc );
592   return p->s.db;
593 }
594 
595 /*
596 ** The following is the implementation of an SQL function that always
597 ** fails with an error message stating that the function is used in the
598 ** wrong context.  The sqlite3_overload_function() API might construct
599 ** SQL function that use this routine so that the functions will exist
600 ** for name resolution but are actually overloaded by the xFindFunction
601 ** method of virtual tables.
602 */
603 void sqlite3InvalidFunction(
604   sqlite3_context *context,  /* The function calling context */
605   int NotUsed,               /* Number of arguments to the function */
606   sqlite3_value **NotUsed2   /* Value of each argument */
607 ){
608   const char *zName = context->pFunc->zName;
609   char *zErr;
610   UNUSED_PARAMETER2(NotUsed, NotUsed2);
611   zErr = sqlite3MPrintf(0,
612       "unable to use function %s in the requested context", zName);
613   sqlite3_result_error(context, zErr, -1);
614   sqlite3_free(zErr);
615 }
616 
617 /*
618 ** Allocate or return the aggregate context for a user function.  A new
619 ** context is allocated on the first call.  Subsequent calls return the
620 ** same context that was returned on prior calls.
621 */
622 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
623   Mem *pMem;
624   assert( p && p->pFunc && p->pFunc->xStep );
625   assert( sqlite3_mutex_held(p->s.db->mutex) );
626   pMem = p->pMem;
627   if( (pMem->flags & MEM_Agg)==0 ){
628     if( nByte==0 ){
629       sqlite3VdbeMemReleaseExternal(pMem);
630       pMem->flags = MEM_Null;
631       pMem->z = 0;
632     }else{
633       sqlite3VdbeMemGrow(pMem, nByte, 0);
634       pMem->flags = MEM_Agg;
635       pMem->u.pDef = p->pFunc;
636       if( pMem->z ){
637         memset(pMem->z, 0, nByte);
638       }
639     }
640   }
641   return (void*)pMem->z;
642 }
643 
644 /*
645 ** Return the auxilary data pointer, if any, for the iArg'th argument to
646 ** the user-function defined by pCtx.
647 */
648 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
649   VdbeFunc *pVdbeFunc;
650 
651   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
652   pVdbeFunc = pCtx->pVdbeFunc;
653   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
654     return 0;
655   }
656   return pVdbeFunc->apAux[iArg].pAux;
657 }
658 
659 /*
660 ** Set the auxilary data pointer and delete function, for the iArg'th
661 ** argument to the user-function defined by pCtx. Any previous value is
662 ** deleted by calling the delete function specified when it was set.
663 */
664 void sqlite3_set_auxdata(
665   sqlite3_context *pCtx,
666   int iArg,
667   void *pAux,
668   void (*xDelete)(void*)
669 ){
670   struct AuxData *pAuxData;
671   VdbeFunc *pVdbeFunc;
672   if( iArg<0 ) goto failed;
673 
674   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
675   pVdbeFunc = pCtx->pVdbeFunc;
676   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
677     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
678     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
679     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
680     if( !pVdbeFunc ){
681       goto failed;
682     }
683     pCtx->pVdbeFunc = pVdbeFunc;
684     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
685     pVdbeFunc->nAux = iArg+1;
686     pVdbeFunc->pFunc = pCtx->pFunc;
687   }
688 
689   pAuxData = &pVdbeFunc->apAux[iArg];
690   if( pAuxData->pAux && pAuxData->xDelete ){
691     pAuxData->xDelete(pAuxData->pAux);
692   }
693   pAuxData->pAux = pAux;
694   pAuxData->xDelete = xDelete;
695   return;
696 
697 failed:
698   if( xDelete ){
699     xDelete(pAux);
700   }
701 }
702 
703 #ifndef SQLITE_OMIT_DEPRECATED
704 /*
705 ** Return the number of times the Step function of a aggregate has been
706 ** called.
707 **
708 ** This function is deprecated.  Do not use it for new code.  It is
709 ** provide only to avoid breaking legacy code.  New aggregate function
710 ** implementations should keep their own counts within their aggregate
711 ** context.
712 */
713 int sqlite3_aggregate_count(sqlite3_context *p){
714   assert( p && p->pFunc && p->pFunc->xStep );
715   return p->pMem->n;
716 }
717 #endif
718 
719 /*
720 ** Return the number of columns in the result set for the statement pStmt.
721 */
722 int sqlite3_column_count(sqlite3_stmt *pStmt){
723   Vdbe *pVm = (Vdbe *)pStmt;
724   return pVm ? pVm->nResColumn : 0;
725 }
726 
727 /*
728 ** Return the number of values available from the current row of the
729 ** currently executing statement pStmt.
730 */
731 int sqlite3_data_count(sqlite3_stmt *pStmt){
732   Vdbe *pVm = (Vdbe *)pStmt;
733   if( pVm==0 || pVm->pResultSet==0 ) return 0;
734   return pVm->nResColumn;
735 }
736 
737 
738 /*
739 ** Check to see if column iCol of the given statement is valid.  If
740 ** it is, return a pointer to the Mem for the value of that column.
741 ** If iCol is not valid, return a pointer to a Mem which has a value
742 ** of NULL.
743 */
744 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
745   Vdbe *pVm;
746   int vals;
747   Mem *pOut;
748 
749   pVm = (Vdbe *)pStmt;
750   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
751     sqlite3_mutex_enter(pVm->db->mutex);
752     vals = sqlite3_data_count(pStmt);
753     pOut = &pVm->pResultSet[i];
754   }else{
755     static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
756     if( pVm->db ){
757       sqlite3_mutex_enter(pVm->db->mutex);
758       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
759     }
760     pOut = (Mem*)&nullMem;
761   }
762   return pOut;
763 }
764 
765 /*
766 ** This function is called after invoking an sqlite3_value_XXX function on a
767 ** column value (i.e. a value returned by evaluating an SQL expression in the
768 ** select list of a SELECT statement) that may cause a malloc() failure. If
769 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
770 ** code of statement pStmt set to SQLITE_NOMEM.
771 **
772 ** Specifically, this is called from within:
773 **
774 **     sqlite3_column_int()
775 **     sqlite3_column_int64()
776 **     sqlite3_column_text()
777 **     sqlite3_column_text16()
778 **     sqlite3_column_real()
779 **     sqlite3_column_bytes()
780 **     sqlite3_column_bytes16()
781 **
782 ** But not for sqlite3_column_blob(), which never calls malloc().
783 */
784 static void columnMallocFailure(sqlite3_stmt *pStmt)
785 {
786   /* If malloc() failed during an encoding conversion within an
787   ** sqlite3_column_XXX API, then set the return code of the statement to
788   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
789   ** and _finalize() will return NOMEM.
790   */
791   Vdbe *p = (Vdbe *)pStmt;
792   if( p ){
793     p->rc = sqlite3ApiExit(p->db, p->rc);
794     sqlite3_mutex_leave(p->db->mutex);
795   }
796 }
797 
798 /**************************** sqlite3_column_  *******************************
799 ** The following routines are used to access elements of the current row
800 ** in the result set.
801 */
802 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
803   const void *val;
804   val = sqlite3_value_blob( columnMem(pStmt,i) );
805   /* Even though there is no encoding conversion, value_blob() might
806   ** need to call malloc() to expand the result of a zeroblob()
807   ** expression.
808   */
809   columnMallocFailure(pStmt);
810   return val;
811 }
812 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
813   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
814   columnMallocFailure(pStmt);
815   return val;
816 }
817 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
818   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
819   columnMallocFailure(pStmt);
820   return val;
821 }
822 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
823   double val = sqlite3_value_double( columnMem(pStmt,i) );
824   columnMallocFailure(pStmt);
825   return val;
826 }
827 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
828   int val = sqlite3_value_int( columnMem(pStmt,i) );
829   columnMallocFailure(pStmt);
830   return val;
831 }
832 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
833   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
834   columnMallocFailure(pStmt);
835   return val;
836 }
837 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
838   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
839   columnMallocFailure(pStmt);
840   return val;
841 }
842 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
843   Mem *pOut = columnMem(pStmt, i);
844   if( pOut->flags&MEM_Static ){
845     pOut->flags &= ~MEM_Static;
846     pOut->flags |= MEM_Ephem;
847   }
848   columnMallocFailure(pStmt);
849   return (sqlite3_value *)pOut;
850 }
851 #ifndef SQLITE_OMIT_UTF16
852 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
853   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
854   columnMallocFailure(pStmt);
855   return val;
856 }
857 #endif /* SQLITE_OMIT_UTF16 */
858 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
859   int iType = sqlite3_value_type( columnMem(pStmt,i) );
860   columnMallocFailure(pStmt);
861   return iType;
862 }
863 
864 /* The following function is experimental and subject to change or
865 ** removal */
866 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
867 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
868 **}
869 */
870 
871 /*
872 ** Convert the N-th element of pStmt->pColName[] into a string using
873 ** xFunc() then return that string.  If N is out of range, return 0.
874 **
875 ** There are up to 5 names for each column.  useType determines which
876 ** name is returned.  Here are the names:
877 **
878 **    0      The column name as it should be displayed for output
879 **    1      The datatype name for the column
880 **    2      The name of the database that the column derives from
881 **    3      The name of the table that the column derives from
882 **    4      The name of the table column that the result column derives from
883 **
884 ** If the result is not a simple column reference (if it is an expression
885 ** or a constant) then useTypes 2, 3, and 4 return NULL.
886 */
887 static const void *columnName(
888   sqlite3_stmt *pStmt,
889   int N,
890   const void *(*xFunc)(Mem*),
891   int useType
892 ){
893   const void *ret = 0;
894   Vdbe *p = (Vdbe *)pStmt;
895   int n;
896 
897 
898   if( p!=0 ){
899     n = sqlite3_column_count(pStmt);
900     if( N<n && N>=0 ){
901       N += useType*n;
902       sqlite3_mutex_enter(p->db->mutex);
903       ret = xFunc(&p->aColName[N]);
904 
905       /* A malloc may have failed inside of the xFunc() call. If this
906       ** is the case, clear the mallocFailed flag and return NULL.
907       */
908       if( p->db && p->db->mallocFailed ){
909         p->db->mallocFailed = 0;
910         ret = 0;
911       }
912       sqlite3_mutex_leave(p->db->mutex);
913     }
914   }
915   return ret;
916 }
917 
918 /*
919 ** Return the name of the Nth column of the result set returned by SQL
920 ** statement pStmt.
921 */
922 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
923   return columnName(
924       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
925 }
926 #ifndef SQLITE_OMIT_UTF16
927 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
928   return columnName(
929       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
930 }
931 #endif
932 
933 /*
934 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
935 ** not define OMIT_DECLTYPE.
936 */
937 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
938 # error "Must not define both SQLITE_OMIT_DECLTYPE \
939          and SQLITE_ENABLE_COLUMN_METADATA"
940 #endif
941 
942 #ifndef SQLITE_OMIT_DECLTYPE
943 /*
944 ** Return the column declaration type (if applicable) of the 'i'th column
945 ** of the result set of SQL statement pStmt.
946 */
947 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
948   return columnName(
949       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
950 }
951 #ifndef SQLITE_OMIT_UTF16
952 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
953   return columnName(
954       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
955 }
956 #endif /* SQLITE_OMIT_UTF16 */
957 #endif /* SQLITE_OMIT_DECLTYPE */
958 
959 #ifdef SQLITE_ENABLE_COLUMN_METADATA
960 /*
961 ** Return the name of the database from which a result column derives.
962 ** NULL is returned if the result column is an expression or constant or
963 ** anything else which is not an unabiguous reference to a database column.
964 */
965 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
966   return columnName(
967       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
968 }
969 #ifndef SQLITE_OMIT_UTF16
970 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
971   return columnName(
972       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
973 }
974 #endif /* SQLITE_OMIT_UTF16 */
975 
976 /*
977 ** Return the name of the table from which a result column derives.
978 ** NULL is returned if the result column is an expression or constant or
979 ** anything else which is not an unabiguous reference to a database column.
980 */
981 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
982   return columnName(
983       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
984 }
985 #ifndef SQLITE_OMIT_UTF16
986 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
987   return columnName(
988       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
989 }
990 #endif /* SQLITE_OMIT_UTF16 */
991 
992 /*
993 ** Return the name of the table column from which a result column derives.
994 ** NULL is returned if the result column is an expression or constant or
995 ** anything else which is not an unabiguous reference to a database column.
996 */
997 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
998   return columnName(
999       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
1000 }
1001 #ifndef SQLITE_OMIT_UTF16
1002 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
1003   return columnName(
1004       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
1005 }
1006 #endif /* SQLITE_OMIT_UTF16 */
1007 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
1008 
1009 
1010 /******************************* sqlite3_bind_  ***************************
1011 **
1012 ** Routines used to attach values to wildcards in a compiled SQL statement.
1013 */
1014 /*
1015 ** Unbind the value bound to variable i in virtual machine p. This is the
1016 ** the same as binding a NULL value to the column. If the "i" parameter is
1017 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1018 **
1019 ** A successful evaluation of this routine acquires the mutex on p.
1020 ** the mutex is released if any kind of error occurs.
1021 **
1022 ** The error code stored in database p->db is overwritten with the return
1023 ** value in any case.
1024 */
1025 static int vdbeUnbind(Vdbe *p, int i){
1026   Mem *pVar;
1027   if( p==0 ) return SQLITE_MISUSE;
1028   sqlite3_mutex_enter(p->db->mutex);
1029   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
1030     sqlite3Error(p->db, SQLITE_MISUSE, 0);
1031     sqlite3_mutex_leave(p->db->mutex);
1032     return SQLITE_MISUSE;
1033   }
1034   if( i<1 || i>p->nVar ){
1035     sqlite3Error(p->db, SQLITE_RANGE, 0);
1036     sqlite3_mutex_leave(p->db->mutex);
1037     return SQLITE_RANGE;
1038   }
1039   i--;
1040   pVar = &p->aVar[i];
1041   sqlite3VdbeMemRelease(pVar);
1042   pVar->flags = MEM_Null;
1043   sqlite3Error(p->db, SQLITE_OK, 0);
1044   return SQLITE_OK;
1045 }
1046 
1047 /*
1048 ** Bind a text or BLOB value.
1049 */
1050 static int bindText(
1051   sqlite3_stmt *pStmt,   /* The statement to bind against */
1052   int i,                 /* Index of the parameter to bind */
1053   const void *zData,     /* Pointer to the data to be bound */
1054   int nData,             /* Number of bytes of data to be bound */
1055   void (*xDel)(void*),   /* Destructor for the data */
1056   u8 encoding            /* Encoding for the data */
1057 ){
1058   Vdbe *p = (Vdbe *)pStmt;
1059   Mem *pVar;
1060   int rc;
1061 
1062   rc = vdbeUnbind(p, i);
1063   if( rc==SQLITE_OK ){
1064     if( zData!=0 ){
1065       pVar = &p->aVar[i-1];
1066       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1067       if( rc==SQLITE_OK && encoding!=0 ){
1068         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1069       }
1070       sqlite3Error(p->db, rc, 0);
1071       rc = sqlite3ApiExit(p->db, rc);
1072     }
1073     sqlite3_mutex_leave(p->db->mutex);
1074   }
1075   return rc;
1076 }
1077 
1078 
1079 /*
1080 ** Bind a blob value to an SQL statement variable.
1081 */
1082 int sqlite3_bind_blob(
1083   sqlite3_stmt *pStmt,
1084   int i,
1085   const void *zData,
1086   int nData,
1087   void (*xDel)(void*)
1088 ){
1089   return bindText(pStmt, i, zData, nData, xDel, 0);
1090 }
1091 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1092   int rc;
1093   Vdbe *p = (Vdbe *)pStmt;
1094   rc = vdbeUnbind(p, i);
1095   if( rc==SQLITE_OK ){
1096     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1097     sqlite3_mutex_leave(p->db->mutex);
1098   }
1099   return rc;
1100 }
1101 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
1102   return sqlite3_bind_int64(p, i, (i64)iValue);
1103 }
1104 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
1105   int rc;
1106   Vdbe *p = (Vdbe *)pStmt;
1107   rc = vdbeUnbind(p, i);
1108   if( rc==SQLITE_OK ){
1109     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
1110     sqlite3_mutex_leave(p->db->mutex);
1111   }
1112   return rc;
1113 }
1114 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
1115   int rc;
1116   Vdbe *p = (Vdbe*)pStmt;
1117   rc = vdbeUnbind(p, i);
1118   if( rc==SQLITE_OK ){
1119     sqlite3_mutex_leave(p->db->mutex);
1120   }
1121   return rc;
1122 }
1123 int sqlite3_bind_text(
1124   sqlite3_stmt *pStmt,
1125   int i,
1126   const char *zData,
1127   int nData,
1128   void (*xDel)(void*)
1129 ){
1130   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
1131 }
1132 #ifndef SQLITE_OMIT_UTF16
1133 int sqlite3_bind_text16(
1134   sqlite3_stmt *pStmt,
1135   int i,
1136   const void *zData,
1137   int nData,
1138   void (*xDel)(void*)
1139 ){
1140   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
1141 }
1142 #endif /* SQLITE_OMIT_UTF16 */
1143 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1144   int rc;
1145   Vdbe *p = (Vdbe *)pStmt;
1146   rc = vdbeUnbind(p, i);
1147   if( rc==SQLITE_OK ){
1148     rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
1149     if( rc==SQLITE_OK ){
1150       rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db));
1151     }
1152     sqlite3_mutex_leave(p->db->mutex);
1153   }
1154   rc = sqlite3ApiExit(p->db, rc);
1155   return rc;
1156 }
1157 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1158   int rc;
1159   Vdbe *p = (Vdbe *)pStmt;
1160   rc = vdbeUnbind(p, i);
1161   if( rc==SQLITE_OK ){
1162     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1163     sqlite3_mutex_leave(p->db->mutex);
1164   }
1165   return rc;
1166 }
1167 
1168 /*
1169 ** Return the number of wildcards that can be potentially bound to.
1170 ** This routine is added to support DBD::SQLite.
1171 */
1172 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
1173   Vdbe *p = (Vdbe*)pStmt;
1174   return p ? p->nVar : 0;
1175 }
1176 
1177 /*
1178 ** Create a mapping from variable numbers to variable names
1179 ** in the Vdbe.azVar[] array, if such a mapping does not already
1180 ** exist.
1181 */
1182 static void createVarMap(Vdbe *p){
1183   if( !p->okVar ){
1184     sqlite3_mutex_enter(p->db->mutex);
1185     if( !p->okVar ){
1186       int j;
1187       Op *pOp;
1188       for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1189         if( pOp->opcode==OP_Variable ){
1190           assert( pOp->p1>0 && pOp->p1<=p->nVar );
1191           p->azVar[pOp->p1-1] = pOp->p4.z;
1192         }
1193       }
1194       p->okVar = 1;
1195     }
1196     sqlite3_mutex_leave(p->db->mutex);
1197   }
1198 }
1199 
1200 /*
1201 ** Return the name of a wildcard parameter.  Return NULL if the index
1202 ** is out of range or if the wildcard is unnamed.
1203 **
1204 ** The result is always UTF-8.
1205 */
1206 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1207   Vdbe *p = (Vdbe*)pStmt;
1208   if( p==0 || i<1 || i>p->nVar ){
1209     return 0;
1210   }
1211   createVarMap(p);
1212   return p->azVar[i-1];
1213 }
1214 
1215 /*
1216 ** Given a wildcard parameter name, return the index of the variable
1217 ** with that name.  If there is no variable with the given name,
1218 ** return 0.
1219 */
1220 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1221   Vdbe *p = (Vdbe*)pStmt;
1222   int i;
1223   if( p==0 ){
1224     return 0;
1225   }
1226   createVarMap(p);
1227   if( zName ){
1228     for(i=0; i<p->nVar; i++){
1229       const char *z = p->azVar[i];
1230       if( z && strcmp(z,zName)==0 ){
1231         return i+1;
1232       }
1233     }
1234   }
1235   return 0;
1236 }
1237 
1238 /*
1239 ** Transfer all bindings from the first statement over to the second.
1240 ** If the two statements contain a different number of bindings, then
1241 ** an SQLITE_ERROR is returned.
1242 */
1243 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1244   Vdbe *pFrom = (Vdbe*)pFromStmt;
1245   Vdbe *pTo = (Vdbe*)pToStmt;
1246   int i, rc = SQLITE_OK;
1247   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
1248     || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1249     || pTo->db!=pFrom->db ){
1250     return SQLITE_MISUSE;
1251   }
1252   if( pFrom->nVar!=pTo->nVar ){
1253     return SQLITE_ERROR;
1254   }
1255   sqlite3_mutex_enter(pTo->db->mutex);
1256   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
1257     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1258   }
1259   sqlite3_mutex_leave(pTo->db->mutex);
1260   assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
1261   return rc;
1262 }
1263 
1264 #ifndef SQLITE_OMIT_DEPRECATED
1265 /*
1266 ** Deprecated external interface.  Internal/core SQLite code
1267 ** should call sqlite3TransferBindings.
1268 */
1269 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1270   return sqlite3TransferBindings(pFromStmt, pToStmt);
1271 }
1272 #endif
1273 
1274 /*
1275 ** Return the sqlite3* database handle to which the prepared statement given
1276 ** in the argument belongs.  This is the same database handle that was
1277 ** the first argument to the sqlite3_prepare() that was used to create
1278 ** the statement in the first place.
1279 */
1280 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1281   return pStmt ? ((Vdbe*)pStmt)->db : 0;
1282 }
1283 
1284 /*
1285 ** Return a pointer to the next prepared statement after pStmt associated
1286 ** with database connection pDb.  If pStmt is NULL, return the first
1287 ** prepared statement for the database connection.  Return NULL if there
1288 ** are no more.
1289 */
1290 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1291   sqlite3_stmt *pNext;
1292   sqlite3_mutex_enter(pDb->mutex);
1293   if( pStmt==0 ){
1294     pNext = (sqlite3_stmt*)pDb->pVdbe;
1295   }else{
1296     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1297   }
1298   sqlite3_mutex_leave(pDb->mutex);
1299   return pNext;
1300 }
1301 
1302 /*
1303 ** Return the value of a status counter for a prepared statement
1304 */
1305 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1306   Vdbe *pVdbe = (Vdbe*)pStmt;
1307   int v = pVdbe->aCounter[op-1];
1308   if( resetFlag ) pVdbe->aCounter[op-1] = 0;
1309   return v;
1310 }
1311