vdbeapi.c 31 KB

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