vdbeapi.c 39 KB

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