vdbemem.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 manipulate "Mem" structure. A "Mem"
  14. ** stores a single value in the VDBE. Mem is an opaque structure visible
  15. ** only within the VDBE. Interface routines refer to a Mem using the
  16. ** name sqlite_value
  17. */
  18. #include "sqliteInt.h"
  19. #include "vdbeInt.h"
  20. /*
  21. ** If pMem is an object with a valid string representation, this routine
  22. ** ensures the internal encoding for the string representation is
  23. ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
  24. **
  25. ** If pMem is not a string object, or the encoding of the string
  26. ** representation is already stored using the requested encoding, then this
  27. ** routine is a no-op.
  28. **
  29. ** SQLITE_OK is returned if the conversion is successful (or not required).
  30. ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
  31. ** between formats.
  32. */
  33. int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
  34. #ifndef SQLITE_OMIT_UTF16
  35. int rc;
  36. #endif
  37. assert( (pMem->flags&MEM_RowSet)==0 );
  38. assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
  39. || desiredEnc==SQLITE_UTF16BE );
  40. if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
  41. return SQLITE_OK;
  42. }
  43. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  44. #ifdef SQLITE_OMIT_UTF16
  45. return SQLITE_ERROR;
  46. #else
  47. /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
  48. ** then the encoding of the value may not have changed.
  49. */
  50. rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
  51. assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
  52. assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
  53. assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
  54. return rc;
  55. #endif
  56. }
  57. /*
  58. ** Make sure pMem->z points to a writable allocation of at least
  59. ** n bytes.
  60. **
  61. ** If the third argument passed to this function is true, then memory
  62. ** cell pMem must contain a string or blob. In this case the content is
  63. ** preserved. Otherwise, if the third parameter to this function is false,
  64. ** any current string or blob value may be discarded.
  65. **
  66. ** This function sets the MEM_Dyn flag and clears any xDel callback.
  67. ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
  68. ** not set, Mem.n is zeroed.
  69. */
  70. int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
  71. assert( 1 >=
  72. ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
  73. (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
  74. ((pMem->flags&MEM_Ephem) ? 1 : 0) +
  75. ((pMem->flags&MEM_Static) ? 1 : 0)
  76. );
  77. assert( (pMem->flags&MEM_RowSet)==0 );
  78. /* If the preserve flag is set to true, then the memory cell must already
  79. ** contain a valid string or blob value. */
  80. assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
  81. if( n<32 ) n = 32;
  82. if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
  83. if( preserve && pMem->z==pMem->zMalloc ){
  84. pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
  85. preserve = 0;
  86. }else{
  87. sqlite3DbFree(pMem->db, pMem->zMalloc);
  88. pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
  89. }
  90. }
  91. if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
  92. memcpy(pMem->zMalloc, pMem->z, pMem->n);
  93. }
  94. if( pMem->flags&MEM_Dyn && pMem->xDel ){
  95. assert( pMem->xDel!=SQLITE_DYNAMIC );
  96. pMem->xDel((void *)(pMem->z));
  97. }
  98. pMem->z = pMem->zMalloc;
  99. if( pMem->z==0 ){
  100. pMem->flags = MEM_Null;
  101. }else{
  102. pMem->flags &= ~(MEM_Ephem|MEM_Static);
  103. }
  104. pMem->xDel = 0;
  105. return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
  106. }
  107. /*
  108. ** Make the given Mem object MEM_Dyn. In other words, make it so
  109. ** that any TEXT or BLOB content is stored in memory obtained from
  110. ** malloc(). In this way, we know that the memory is safe to be
  111. ** overwritten or altered.
  112. **
  113. ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
  114. */
  115. int sqlite3VdbeMemMakeWriteable(Mem *pMem){
  116. int f;
  117. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  118. assert( (pMem->flags&MEM_RowSet)==0 );
  119. ExpandBlob(pMem);
  120. f = pMem->flags;
  121. if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
  122. if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
  123. return SQLITE_NOMEM;
  124. }
  125. pMem->z[pMem->n] = 0;
  126. pMem->z[pMem->n+1] = 0;
  127. pMem->flags |= MEM_Term;
  128. #ifdef SQLITE_DEBUG
  129. pMem->pScopyFrom = 0;
  130. #endif
  131. }
  132. return SQLITE_OK;
  133. }
  134. /*
  135. ** If the given Mem* has a zero-filled tail, turn it into an ordinary
  136. ** blob stored in dynamically allocated space.
  137. */
  138. #ifndef SQLITE_OMIT_INCRBLOB
  139. int sqlite3VdbeMemExpandBlob(Mem *pMem){
  140. if( pMem->flags & MEM_Zero ){
  141. int nByte;
  142. assert( pMem->flags&MEM_Blob );
  143. assert( (pMem->flags&MEM_RowSet)==0 );
  144. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  145. /* Set nByte to the number of bytes required to store the expanded blob. */
  146. nByte = pMem->n + pMem->u.nZero;
  147. if( nByte<=0 ){
  148. nByte = 1;
  149. }
  150. if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
  151. return SQLITE_NOMEM;
  152. }
  153. memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
  154. pMem->n += pMem->u.nZero;
  155. pMem->flags &= ~(MEM_Zero|MEM_Term);
  156. }
  157. return SQLITE_OK;
  158. }
  159. #endif
  160. /*
  161. ** Make sure the given Mem is \u0000 terminated.
  162. */
  163. int sqlite3VdbeMemNulTerminate(Mem *pMem){
  164. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  165. if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
  166. return SQLITE_OK; /* Nothing to do */
  167. }
  168. if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
  169. return SQLITE_NOMEM;
  170. }
  171. pMem->z[pMem->n] = 0;
  172. pMem->z[pMem->n+1] = 0;
  173. pMem->flags |= MEM_Term;
  174. return SQLITE_OK;
  175. }
  176. /*
  177. ** Add MEM_Str to the set of representations for the given Mem. Numbers
  178. ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
  179. ** is a no-op.
  180. **
  181. ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
  182. **
  183. ** A MEM_Null value will never be passed to this function. This function is
  184. ** used for converting values to text for returning to the user (i.e. via
  185. ** sqlite3_value_text()), or for ensuring that values to be used as btree
  186. ** keys are strings. In the former case a NULL pointer is returned the
  187. ** user and the later is an internal programming error.
  188. */
  189. int sqlite3VdbeMemStringify(Mem *pMem, int enc){
  190. int rc = SQLITE_OK;
  191. int fg = pMem->flags;
  192. const int nByte = 32;
  193. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  194. assert( !(fg&MEM_Zero) );
  195. assert( !(fg&(MEM_Str|MEM_Blob)) );
  196. assert( fg&(MEM_Int|MEM_Real) );
  197. assert( (pMem->flags&MEM_RowSet)==0 );
  198. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  199. if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
  200. return SQLITE_NOMEM;
  201. }
  202. /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
  203. ** string representation of the value. Then, if the required encoding
  204. ** is UTF-16le or UTF-16be do a translation.
  205. **
  206. ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
  207. */
  208. if( fg & MEM_Int ){
  209. sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
  210. }else{
  211. assert( fg & MEM_Real );
  212. sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
  213. }
  214. pMem->n = sqlite3Strlen30(pMem->z);
  215. pMem->enc = SQLITE_UTF8;
  216. pMem->flags |= MEM_Str|MEM_Term;
  217. sqlite3VdbeChangeEncoding(pMem, enc);
  218. return rc;
  219. }
  220. /*
  221. ** Memory cell pMem contains the context of an aggregate function.
  222. ** This routine calls the finalize method for that function. The
  223. ** result of the aggregate is stored back into pMem.
  224. **
  225. ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
  226. ** otherwise.
  227. */
  228. int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
  229. int rc = SQLITE_OK;
  230. if( ALWAYS(pFunc && pFunc->xFinalize) ){
  231. sqlite3_context ctx;
  232. assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
  233. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  234. memset(&ctx, 0, sizeof(ctx));
  235. ctx.s.flags = MEM_Null;
  236. ctx.s.db = pMem->db;
  237. ctx.pMem = pMem;
  238. ctx.pFunc = pFunc;
  239. pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
  240. assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
  241. sqlite3DbFree(pMem->db, pMem->zMalloc);
  242. memcpy(pMem, &ctx.s, sizeof(ctx.s));
  243. rc = ctx.isError;
  244. }
  245. return rc;
  246. }
  247. /*
  248. ** If the memory cell contains a string value that must be freed by
  249. ** invoking an external callback, free it now. Calling this function
  250. ** does not free any Mem.zMalloc buffer.
  251. */
  252. void sqlite3VdbeMemReleaseExternal(Mem *p){
  253. assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
  254. if( p->flags&MEM_Agg ){
  255. sqlite3VdbeMemFinalize(p, p->u.pDef);
  256. assert( (p->flags & MEM_Agg)==0 );
  257. sqlite3VdbeMemRelease(p);
  258. }else if( p->flags&MEM_Dyn && p->xDel ){
  259. assert( (p->flags&MEM_RowSet)==0 );
  260. assert( p->xDel!=SQLITE_DYNAMIC );
  261. p->xDel((void *)p->z);
  262. p->xDel = 0;
  263. }else if( p->flags&MEM_RowSet ){
  264. sqlite3RowSetClear(p->u.pRowSet);
  265. }else if( p->flags&MEM_Frame ){
  266. sqlite3VdbeMemSetNull(p);
  267. }
  268. }
  269. /*
  270. ** Release any memory held by the Mem. This may leave the Mem in an
  271. ** inconsistent state, for example with (Mem.z==0) and
  272. ** (Mem.type==SQLITE_TEXT).
  273. */
  274. void sqlite3VdbeMemRelease(Mem *p){
  275. VdbeMemRelease(p);
  276. sqlite3DbFree(p->db, p->zMalloc);
  277. p->z = 0;
  278. p->zMalloc = 0;
  279. p->xDel = 0;
  280. }
  281. /*
  282. ** Convert a 64-bit IEEE double into a 64-bit signed integer.
  283. ** If the double is too large, return 0x8000000000000000.
  284. **
  285. ** Most systems appear to do this simply by assigning
  286. ** variables and without the extra range tests. But
  287. ** there are reports that windows throws an expection
  288. ** if the floating point value is out of range. (See ticket #2880.)
  289. ** Because we do not completely understand the problem, we will
  290. ** take the conservative approach and always do range tests
  291. ** before attempting the conversion.
  292. */
  293. static i64 doubleToInt64(double r){
  294. #ifdef SQLITE_OMIT_FLOATING_POINT
  295. /* When floating-point is omitted, double and int64 are the same thing */
  296. return r;
  297. #else
  298. /*
  299. ** Many compilers we encounter do not define constants for the
  300. ** minimum and maximum 64-bit integers, or they define them
  301. ** inconsistently. And many do not understand the "LL" notation.
  302. ** So we define our own static constants here using nothing
  303. ** larger than a 32-bit integer constant.
  304. */
  305. static const i64 maxInt = LARGEST_INT64;
  306. static const i64 minInt = SMALLEST_INT64;
  307. if( r<(double)minInt ){
  308. return minInt;
  309. }else if( r>(double)maxInt ){
  310. /* minInt is correct here - not maxInt. It turns out that assigning
  311. ** a very large positive number to an integer results in a very large
  312. ** negative integer. This makes no sense, but it is what x86 hardware
  313. ** does so for compatibility we will do the same in software. */
  314. return minInt;
  315. }else{
  316. return (i64)r;
  317. }
  318. #endif
  319. }
  320. /*
  321. ** Return some kind of integer value which is the best we can do
  322. ** at representing the value that *pMem describes as an integer.
  323. ** If pMem is an integer, then the value is exact. If pMem is
  324. ** a floating-point then the value returned is the integer part.
  325. ** If pMem is a string or blob, then we make an attempt to convert
  326. ** it into a integer and return that. If pMem represents an
  327. ** an SQL-NULL value, return 0.
  328. **
  329. ** If pMem represents a string value, its encoding might be changed.
  330. */
  331. i64 sqlite3VdbeIntValue(Mem *pMem){
  332. int flags;
  333. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  334. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  335. flags = pMem->flags;
  336. if( flags & MEM_Int ){
  337. return pMem->u.i;
  338. }else if( flags & MEM_Real ){
  339. return doubleToInt64(pMem->r);
  340. }else if( flags & (MEM_Str|MEM_Blob) ){
  341. i64 value = 0;
  342. assert( pMem->z || pMem->n==0 );
  343. testcase( pMem->z==0 );
  344. sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
  345. return value;
  346. }else{
  347. return 0;
  348. }
  349. }
  350. /*
  351. ** Return the best representation of pMem that we can get into a
  352. ** double. If pMem is already a double or an integer, return its
  353. ** value. If it is a string or blob, try to convert it to a double.
  354. ** If it is a NULL, return 0.0.
  355. */
  356. double sqlite3VdbeRealValue(Mem *pMem){
  357. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  358. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  359. if( pMem->flags & MEM_Real ){
  360. return pMem->r;
  361. }else if( pMem->flags & MEM_Int ){
  362. return (double)pMem->u.i;
  363. }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
  364. /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  365. double val = (double)0;
  366. sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
  367. return val;
  368. }else{
  369. /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  370. return (double)0;
  371. }
  372. }
  373. /*
  374. ** The MEM structure is already a MEM_Real. Try to also make it a
  375. ** MEM_Int if we can.
  376. */
  377. void sqlite3VdbeIntegerAffinity(Mem *pMem){
  378. assert( pMem->flags & MEM_Real );
  379. assert( (pMem->flags & MEM_RowSet)==0 );
  380. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  381. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  382. pMem->u.i = doubleToInt64(pMem->r);
  383. /* Only mark the value as an integer if
  384. **
  385. ** (1) the round-trip conversion real->int->real is a no-op, and
  386. ** (2) The integer is neither the largest nor the smallest
  387. ** possible integer (ticket #3922)
  388. **
  389. ** The second and third terms in the following conditional enforces
  390. ** the second condition under the assumption that addition overflow causes
  391. ** values to wrap around. On x86 hardware, the third term is always
  392. ** true and could be omitted. But we leave it in because other
  393. ** architectures might behave differently.
  394. */
  395. if( pMem->r==(double)pMem->u.i
  396. && pMem->u.i>SMALLEST_INT64
  397. #if defined(__i486__) || defined(__x86_64__)
  398. && ALWAYS(pMem->u.i<LARGEST_INT64)
  399. #else
  400. && pMem->u.i<LARGEST_INT64
  401. #endif
  402. ){
  403. pMem->flags |= MEM_Int;
  404. }
  405. }
  406. /*
  407. ** Convert pMem to type integer. Invalidate any prior representations.
  408. */
  409. int sqlite3VdbeMemIntegerify(Mem *pMem){
  410. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  411. assert( (pMem->flags & MEM_RowSet)==0 );
  412. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  413. pMem->u.i = sqlite3VdbeIntValue(pMem);
  414. MemSetTypeFlag(pMem, MEM_Int);
  415. return SQLITE_OK;
  416. }
  417. /*
  418. ** Convert pMem so that it is of type MEM_Real.
  419. ** Invalidate any prior representations.
  420. */
  421. int sqlite3VdbeMemRealify(Mem *pMem){
  422. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  423. assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  424. pMem->r = sqlite3VdbeRealValue(pMem);
  425. MemSetTypeFlag(pMem, MEM_Real);
  426. return SQLITE_OK;
  427. }
  428. /*
  429. ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
  430. ** Invalidate any prior representations.
  431. **
  432. ** Every effort is made to force the conversion, even if the input
  433. ** is a string that does not look completely like a number. Convert
  434. ** as much of the string as we can and ignore the rest.
  435. */
  436. int sqlite3VdbeMemNumerify(Mem *pMem){
  437. if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
  438. assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
  439. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  440. if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
  441. MemSetTypeFlag(pMem, MEM_Int);
  442. }else{
  443. pMem->r = sqlite3VdbeRealValue(pMem);
  444. MemSetTypeFlag(pMem, MEM_Real);
  445. sqlite3VdbeIntegerAffinity(pMem);
  446. }
  447. }
  448. assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
  449. pMem->flags &= ~(MEM_Str|MEM_Blob);
  450. return SQLITE_OK;
  451. }
  452. /*
  453. ** Delete any previous value and set the value stored in *pMem to NULL.
  454. */
  455. void sqlite3VdbeMemSetNull(Mem *pMem){
  456. if( pMem->flags & MEM_Frame ){
  457. VdbeFrame *pFrame = pMem->u.pFrame;
  458. pFrame->pParent = pFrame->v->pDelFrame;
  459. pFrame->v->pDelFrame = pFrame;
  460. }
  461. if( pMem->flags & MEM_RowSet ){
  462. sqlite3RowSetClear(pMem->u.pRowSet);
  463. }
  464. MemSetTypeFlag(pMem, MEM_Null);
  465. pMem->type = SQLITE_NULL;
  466. }
  467. /*
  468. ** Delete any previous value and set the value to be a BLOB of length
  469. ** n containing all zeros.
  470. */
  471. void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
  472. sqlite3VdbeMemRelease(pMem);
  473. pMem->flags = MEM_Blob|MEM_Zero;
  474. pMem->type = SQLITE_BLOB;
  475. pMem->n = 0;
  476. if( n<0 ) n = 0;
  477. pMem->u.nZero = n;
  478. pMem->enc = SQLITE_UTF8;
  479. #ifdef SQLITE_OMIT_INCRBLOB
  480. sqlite3VdbeMemGrow(pMem, n, 0);
  481. if( pMem->z ){
  482. pMem->n = n;
  483. memset(pMem->z, 0, n);
  484. }
  485. #endif
  486. }
  487. /*
  488. ** Delete any previous value and set the value stored in *pMem to val,
  489. ** manifest type INTEGER.
  490. */
  491. void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
  492. sqlite3VdbeMemRelease(pMem);
  493. pMem->u.i = val;
  494. pMem->flags = MEM_Int;
  495. pMem->type = SQLITE_INTEGER;
  496. }
  497. #ifndef SQLITE_OMIT_FLOATING_POINT
  498. /*
  499. ** Delete any previous value and set the value stored in *pMem to val,
  500. ** manifest type REAL.
  501. */
  502. void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
  503. if( sqlite3IsNaN(val) ){
  504. sqlite3VdbeMemSetNull(pMem);
  505. }else{
  506. sqlite3VdbeMemRelease(pMem);
  507. pMem->r = val;
  508. pMem->flags = MEM_Real;
  509. pMem->type = SQLITE_FLOAT;
  510. }
  511. }
  512. #endif
  513. /*
  514. ** Delete any previous value and set the value of pMem to be an
  515. ** empty boolean index.
  516. */
  517. void sqlite3VdbeMemSetRowSet(Mem *pMem){
  518. sqlite3 *db = pMem->db;
  519. assert( db!=0 );
  520. assert( (pMem->flags & MEM_RowSet)==0 );
  521. sqlite3VdbeMemRelease(pMem);
  522. pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
  523. if( db->mallocFailed ){
  524. pMem->flags = MEM_Null;
  525. }else{
  526. assert( pMem->zMalloc );
  527. pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
  528. sqlite3DbMallocSize(db, pMem->zMalloc));
  529. assert( pMem->u.pRowSet!=0 );
  530. pMem->flags = MEM_RowSet;
  531. }
  532. }
  533. /*
  534. ** Return true if the Mem object contains a TEXT or BLOB that is
  535. ** too large - whose size exceeds SQLITE_MAX_LENGTH.
  536. */
  537. int sqlite3VdbeMemTooBig(Mem *p){
  538. assert( p->db!=0 );
  539. if( p->flags & (MEM_Str|MEM_Blob) ){
  540. int n = p->n;
  541. if( p->flags & MEM_Zero ){
  542. n += p->u.nZero;
  543. }
  544. return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
  545. }
  546. return 0;
  547. }
  548. #ifdef SQLITE_DEBUG
  549. /*
  550. ** This routine prepares a memory cell for modication by breaking
  551. ** its link to a shallow copy and by marking any current shallow
  552. ** copies of this cell as invalid.
  553. **
  554. ** This is used for testing and debugging only - to make sure shallow
  555. ** copies are not misused.
  556. */
  557. void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
  558. int i;
  559. Mem *pX;
  560. for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
  561. if( pX->pScopyFrom==pMem ){
  562. pX->flags |= MEM_Invalid;
  563. pX->pScopyFrom = 0;
  564. }
  565. }
  566. pMem->pScopyFrom = 0;
  567. }
  568. #endif /* SQLITE_DEBUG */
  569. /*
  570. ** Size of struct Mem not including the Mem.zMalloc member.
  571. */
  572. #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
  573. /*
  574. ** Make an shallow copy of pFrom into pTo. Prior contents of
  575. ** pTo are freed. The pFrom->z field is not duplicated. If
  576. ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
  577. ** and flags gets srcType (either MEM_Ephem or MEM_Static).
  578. */
  579. void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
  580. assert( (pFrom->flags & MEM_RowSet)==0 );
  581. VdbeMemRelease(pTo);
  582. memcpy(pTo, pFrom, MEMCELLSIZE);
  583. pTo->xDel = 0;
  584. if( (pFrom->flags&MEM_Static)==0 ){
  585. pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
  586. assert( srcType==MEM_Ephem || srcType==MEM_Static );
  587. pTo->flags |= srcType;
  588. }
  589. }
  590. /*
  591. ** Make a full copy of pFrom into pTo. Prior contents of pTo are
  592. ** freed before the copy is made.
  593. */
  594. int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
  595. int rc = SQLITE_OK;
  596. assert( (pFrom->flags & MEM_RowSet)==0 );
  597. VdbeMemRelease(pTo);
  598. memcpy(pTo, pFrom, MEMCELLSIZE);
  599. pTo->flags &= ~MEM_Dyn;
  600. if( pTo->flags&(MEM_Str|MEM_Blob) ){
  601. if( 0==(pFrom->flags&MEM_Static) ){
  602. pTo->flags |= MEM_Ephem;
  603. rc = sqlite3VdbeMemMakeWriteable(pTo);
  604. }
  605. }
  606. return rc;
  607. }
  608. /*
  609. ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
  610. ** freed. If pFrom contains ephemeral data, a copy is made.
  611. **
  612. ** pFrom contains an SQL NULL when this routine returns.
  613. */
  614. void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
  615. assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
  616. assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
  617. assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
  618. sqlite3VdbeMemRelease(pTo);
  619. memcpy(pTo, pFrom, sizeof(Mem));
  620. pFrom->flags = MEM_Null;
  621. pFrom->xDel = 0;
  622. pFrom->zMalloc = 0;
  623. }
  624. /*
  625. ** Change the value of a Mem to be a string or a BLOB.
  626. **
  627. ** The memory management strategy depends on the value of the xDel
  628. ** parameter. If the value passed is SQLITE_TRANSIENT, then the
  629. ** string is copied into a (possibly existing) buffer managed by the
  630. ** Mem structure. Otherwise, any existing buffer is freed and the
  631. ** pointer copied.
  632. **
  633. ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
  634. ** size limit) then no memory allocation occurs. If the string can be
  635. ** stored without allocating memory, then it is. If a memory allocation
  636. ** is required to store the string, then value of pMem is unchanged. In
  637. ** either case, SQLITE_TOOBIG is returned.
  638. */
  639. int sqlite3VdbeMemSetStr(
  640. Mem *pMem, /* Memory cell to set to string value */
  641. const char *z, /* String pointer */
  642. int n, /* Bytes in string, or negative */
  643. u8 enc, /* Encoding of z. 0 for BLOBs */
  644. void (*xDel)(void*) /* Destructor function */
  645. ){
  646. int nByte = n; /* New value for pMem->n */
  647. int iLimit; /* Maximum allowed string or blob size */
  648. u16 flags = 0; /* New value for pMem->flags */
  649. assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  650. assert( (pMem->flags & MEM_RowSet)==0 );
  651. /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
  652. if( !z ){
  653. sqlite3VdbeMemSetNull(pMem);
  654. return SQLITE_OK;
  655. }
  656. if( pMem->db ){
  657. iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
  658. }else{
  659. iLimit = SQLITE_MAX_LENGTH;
  660. }
  661. flags = (enc==0?MEM_Blob:MEM_Str);
  662. if( nByte<0 ){
  663. assert( enc!=0 );
  664. if( enc==SQLITE_UTF8 ){
  665. for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
  666. }else{
  667. for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
  668. }
  669. flags |= MEM_Term;
  670. }
  671. /* The following block sets the new values of Mem.z and Mem.xDel. It
  672. ** also sets a flag in local variable "flags" to indicate the memory
  673. ** management (one of MEM_Dyn or MEM_Static).
  674. */
  675. if( xDel==SQLITE_TRANSIENT ){
  676. int nAlloc = nByte;
  677. if( flags&MEM_Term ){
  678. nAlloc += (enc==SQLITE_UTF8?1:2);
  679. }
  680. if( nByte>iLimit ){
  681. return SQLITE_TOOBIG;
  682. }
  683. if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
  684. return SQLITE_NOMEM;
  685. }
  686. memcpy(pMem->z, z, nAlloc);
  687. }else if( xDel==SQLITE_DYNAMIC ){
  688. sqlite3VdbeMemRelease(pMem);
  689. pMem->zMalloc = pMem->z = (char *)z;
  690. pMem->xDel = 0;
  691. }else{
  692. sqlite3VdbeMemRelease(pMem);
  693. pMem->z = (char *)z;
  694. pMem->xDel = xDel;
  695. flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
  696. }
  697. pMem->n = nByte;
  698. pMem->flags = flags;
  699. pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
  700. pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
  701. #ifndef SQLITE_OMIT_UTF16
  702. if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
  703. return SQLITE_NOMEM;
  704. }
  705. #endif
  706. if( nByte>iLimit ){
  707. return SQLITE_TOOBIG;
  708. }
  709. return SQLITE_OK;
  710. }
  711. /*
  712. ** Compare the values contained by the two memory cells, returning
  713. ** negative, zero or positive if pMem1 is less than, equal to, or greater
  714. ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
  715. ** and reals) sorted numerically, followed by text ordered by the collating
  716. ** sequence pColl and finally blob's ordered by memcmp().
  717. **
  718. ** Two NULL values are considered equal by this function.
  719. */
  720. int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
  721. int rc;
  722. int f1, f2;
  723. int combined_flags;
  724. f1 = pMem1->flags;
  725. f2 = pMem2->flags;
  726. combined_flags = f1|f2;
  727. assert( (combined_flags & MEM_RowSet)==0 );
  728. /* If one value is NULL, it is less than the other. If both values
  729. ** are NULL, return 0.
  730. */
  731. if( combined_flags&MEM_Null ){
  732. return (f2&MEM_Null) - (f1&MEM_Null);
  733. }
  734. /* If one value is a number and the other is not, the number is less.
  735. ** If both are numbers, compare as reals if one is a real, or as integers
  736. ** if both values are integers.
  737. */
  738. if( combined_flags&(MEM_Int|MEM_Real) ){
  739. double r1, r2;
  740. if( (f1 & f2 & MEM_Int)!=0 ){
  741. if( pMem1->u.i < pMem2->u.i ) return -1;
  742. if( pMem1->u.i > pMem2->u.i ) return 1;
  743. return 0;
  744. }
  745. if( (f1&MEM_Real)!=0 ){
  746. r1 = pMem1->r;
  747. }else if( (f1&MEM_Int)!=0 ){
  748. r1 = (double)pMem1->u.i;
  749. }else{
  750. return 1;
  751. }
  752. if( (f2&MEM_Real)!=0 ){
  753. r2 = pMem2->r;
  754. }else if( (f2&MEM_Int)!=0 ){
  755. r2 = (double)pMem2->u.i;
  756. }else{
  757. return -1;
  758. }
  759. if( r1<r2 ) return -1;
  760. if( r1>r2 ) return 1;
  761. return 0;
  762. }
  763. /* If one value is a string and the other is a blob, the string is less.
  764. ** If both are strings, compare using the collating functions.
  765. */
  766. if( combined_flags&MEM_Str ){
  767. if( (f1 & MEM_Str)==0 ){
  768. return 1;
  769. }
  770. if( (f2 & MEM_Str)==0 ){
  771. return -1;
  772. }
  773. assert( pMem1->enc==pMem2->enc );
  774. assert( pMem1->enc==SQLITE_UTF8 ||
  775. pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
  776. /* The collation sequence must be defined at this point, even if
  777. ** the user deletes the collation sequence after the vdbe program is
  778. ** compiled (this was not always the case).
  779. */
  780. assert( !pColl || pColl->xCmp );
  781. if( pColl ){
  782. if( pMem1->enc==pColl->enc ){
  783. /* The strings are already in the correct encoding. Call the
  784. ** comparison function directly */
  785. return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
  786. }else{
  787. const void *v1, *v2;
  788. int n1, n2;
  789. Mem c1;
  790. Mem c2;
  791. memset(&c1, 0, sizeof(c1));
  792. memset(&c2, 0, sizeof(c2));
  793. sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
  794. sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
  795. v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
  796. n1 = v1==0 ? 0 : c1.n;
  797. v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
  798. n2 = v2==0 ? 0 : c2.n;
  799. rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
  800. sqlite3VdbeMemRelease(&c1);
  801. sqlite3VdbeMemRelease(&c2);
  802. return rc;
  803. }
  804. }
  805. /* If a NULL pointer was passed as the collate function, fall through
  806. ** to the blob case and use memcmp(). */
  807. }
  808. /* Both values must be blobs. Compare using memcmp(). */
  809. rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
  810. if( rc==0 ){
  811. rc = pMem1->n - pMem2->n;
  812. }
  813. return rc;
  814. }
  815. /*
  816. ** Move data out of a btree key or data field and into a Mem structure.
  817. ** The data or key is taken from the entry that pCur is currently pointing
  818. ** to. offset and amt determine what portion of the data or key to retrieve.
  819. ** key is true to get the key or false to get data. The result is written
  820. ** into the pMem element.
  821. **
  822. ** The pMem structure is assumed to be uninitialized. Any prior content
  823. ** is overwritten without being freed.
  824. **
  825. ** If this routine fails for any reason (malloc returns NULL or unable
  826. ** to read from the disk) then the pMem is left in an inconsistent state.
  827. */
  828. int sqlite3VdbeMemFromBtree(
  829. BtCursor *pCur, /* Cursor pointing at record to retrieve. */
  830. int offset, /* Offset from the start of data to return bytes from. */
  831. int amt, /* Number of bytes to return. */
  832. int key, /* If true, retrieve from the btree key, not data. */
  833. Mem *pMem /* OUT: Return data in this Mem structure. */
  834. ){
  835. char *zData; /* Data from the btree layer */
  836. int available = 0; /* Number of bytes available on the local btree page */
  837. int rc = SQLITE_OK; /* Return code */
  838. assert( sqlite3BtreeCursorIsValid(pCur) );
  839. /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
  840. ** that both the BtShared and database handle mutexes are held. */
  841. assert( (pMem->flags & MEM_RowSet)==0 );
  842. if( key ){
  843. zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
  844. }else{
  845. zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
  846. }
  847. assert( zData!=0 );
  848. if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
  849. sqlite3VdbeMemRelease(pMem);
  850. pMem->z = &zData[offset];
  851. pMem->flags = MEM_Blob|MEM_Ephem;
  852. }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
  853. pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
  854. pMem->enc = 0;
  855. pMem->type = SQLITE_BLOB;
  856. if( key ){
  857. rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
  858. }else{
  859. rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
  860. }
  861. pMem->z[amt] = 0;
  862. pMem->z[amt+1] = 0;
  863. if( rc!=SQLITE_OK ){
  864. sqlite3VdbeMemRelease(pMem);
  865. }
  866. }
  867. pMem->n = amt;
  868. return rc;
  869. }
  870. /* This function is only available internally, it is not part of the
  871. ** external API. It works in a similar way to sqlite3_value_text(),
  872. ** except the data returned is in the encoding specified by the second
  873. ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
  874. ** SQLITE_UTF8.
  875. **
  876. ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
  877. ** If that is the case, then the result must be aligned on an even byte
  878. ** boundary.
  879. */
  880. const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
  881. if( !pVal ) return 0;
  882. assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
  883. assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
  884. assert( (pVal->flags & MEM_RowSet)==0 );
  885. if( pVal->flags&MEM_Null ){
  886. return 0;
  887. }
  888. assert( (MEM_Blob>>3) == MEM_Str );
  889. pVal->flags |= (pVal->flags & MEM_Blob)>>3;
  890. ExpandBlob(pVal);
  891. if( pVal->flags&MEM_Str ){
  892. sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
  893. if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
  894. assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
  895. if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
  896. return 0;
  897. }
  898. }
  899. sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
  900. }else{
  901. assert( (pVal->flags&MEM_Blob)==0 );
  902. sqlite3VdbeMemStringify(pVal, enc);
  903. assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
  904. }
  905. assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
  906. || pVal->db->mallocFailed );
  907. if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
  908. return pVal->z;
  909. }else{
  910. return 0;
  911. }
  912. }
  913. /*
  914. ** Create a new sqlite3_value object.
  915. */
  916. sqlite3_value *sqlite3ValueNew(sqlite3 *db){
  917. Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
  918. if( p ){
  919. p->flags = MEM_Null;
  920. p->type = SQLITE_NULL;
  921. p->db = db;
  922. }
  923. return p;
  924. }
  925. /*
  926. ** Context object passed by sqlite3Stat4ProbeSetValue() through to
  927. ** valueNew(). See comments above valueNew() for details.
  928. */
  929. struct ValueNewStat4Ctx {
  930. Parse *pParse;
  931. Index *pIdx;
  932. UnpackedRecord **ppRec;
  933. int iVal;
  934. };
  935. /*
  936. ** Allocate and return a pointer to a new sqlite3_value object. If
  937. ** the second argument to this function is NULL, the object is allocated
  938. ** by calling sqlite3ValueNew().
  939. **
  940. ** Otherwise, if the second argument is non-zero, then this function is
  941. ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
  942. ** already been allocated, allocate the UnpackedRecord structure that
  943. ** that function will return to its caller here. Then return a pointer
  944. ** an sqlite3_value within the UnpackedRecord.a[] array.
  945. */
  946. static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
  947. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  948. if( p ){
  949. UnpackedRecord *pRec = p->ppRec[0];
  950. if( pRec==0 ){
  951. Index *pIdx = p->pIdx; /* Index being probed */
  952. int nByte; /* Bytes of space to allocate */
  953. int i; /* Counter variable */
  954. int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */
  955. nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord);
  956. pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
  957. if( pRec ){
  958. pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx);
  959. if( pRec->pKeyInfo ){
  960. assert( pRec->pKeyInfo->nField+1==nCol );
  961. pRec->pKeyInfo->enc = ENC(db);
  962. pRec->flags = UNPACKED_PREFIX_MATCH;
  963. pRec->aMem = (Mem *)&pRec[1];
  964. for(i=0; i<nCol; i++){
  965. pRec->aMem[i].flags = MEM_Null;
  966. pRec->aMem[i].type = SQLITE_NULL;
  967. pRec->aMem[i].db = db;
  968. }
  969. }else{
  970. sqlite3DbFree(db, pRec);
  971. pRec = 0;
  972. }
  973. }
  974. if( pRec==0 ) return 0;
  975. p->ppRec[0] = pRec;
  976. }
  977. pRec->nField = p->iVal+1;
  978. return &pRec->aMem[p->iVal];
  979. }
  980. #else
  981. UNUSED_PARAMETER(p);
  982. #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
  983. return sqlite3ValueNew(db);
  984. }
  985. /*
  986. ** Extract a value from the supplied expression in the manner described
  987. ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
  988. ** using valueNew().
  989. **
  990. ** If pCtx is NULL and an error occurs after the sqlite3_value object
  991. ** has been allocated, it is freed before returning. Or, if pCtx is not
  992. ** NULL, it is assumed that the caller will free any allocated object
  993. ** in all cases.
  994. */
  995. static int valueFromExpr(
  996. sqlite3 *db, /* The database connection */
  997. Expr *pExpr, /* The expression to evaluate */
  998. u8 enc, /* Encoding to use */
  999. u8 affinity, /* Affinity to use */
  1000. sqlite3_value **ppVal, /* Write the new value here */
  1001. struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
  1002. ){
  1003. int op;
  1004. char *zVal = 0;
  1005. sqlite3_value *pVal = 0;
  1006. int negInt = 1;
  1007. const char *zNeg = "";
  1008. int rc = SQLITE_OK;
  1009. if( !pExpr ){
  1010. *ppVal = 0;
  1011. return SQLITE_OK;
  1012. }
  1013. op = pExpr->op;
  1014. /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
  1015. ** The ifdef here is to enable us to achieve 100% branch test coverage even
  1016. ** when SQLITE_ENABLE_STAT4 is omitted.
  1017. */
  1018. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1019. if( op==TK_REGISTER ) op = pExpr->op2;
  1020. #else
  1021. if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
  1022. #endif
  1023. /* Handle negative integers in a single step. This is needed in the
  1024. ** case when the value is -9223372036854775808.
  1025. */
  1026. if( op==TK_UMINUS
  1027. && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
  1028. pExpr = pExpr->pLeft;
  1029. op = pExpr->op;
  1030. negInt = -1;
  1031. zNeg = "-";
  1032. }
  1033. if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
  1034. pVal = valueNew(db, pCtx);
  1035. if( pVal==0 ) goto no_mem;
  1036. if( ExprHasProperty(pExpr, EP_IntValue) ){
  1037. sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
  1038. }else{
  1039. zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
  1040. if( zVal==0 ) goto no_mem;
  1041. sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
  1042. if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
  1043. }
  1044. if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
  1045. sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
  1046. }else{
  1047. sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
  1048. }
  1049. if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
  1050. if( enc!=SQLITE_UTF8 ){
  1051. rc = sqlite3VdbeChangeEncoding(pVal, enc);
  1052. }
  1053. }else if( op==TK_UMINUS ) {
  1054. /* This branch happens for multiple negative signs. Ex: -(-5) */
  1055. if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
  1056. && pVal!=0
  1057. ){
  1058. sqlite3VdbeMemNumerify(pVal);
  1059. if( pVal->u.i==SMALLEST_INT64 ){
  1060. pVal->flags &= MEM_Int;
  1061. pVal->flags |= MEM_Real;
  1062. pVal->r = (double)LARGEST_INT64;
  1063. }else{
  1064. pVal->u.i = -pVal->u.i;
  1065. }
  1066. pVal->r = -pVal->r;
  1067. sqlite3ValueApplyAffinity(pVal, affinity, enc);
  1068. }
  1069. }else if( op==TK_NULL ){
  1070. pVal = valueNew(db, pCtx);
  1071. if( pVal==0 ) goto no_mem;
  1072. }
  1073. #ifndef SQLITE_OMIT_BLOB_LITERAL
  1074. else if( op==TK_BLOB ){
  1075. int nVal;
  1076. assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
  1077. assert( pExpr->u.zToken[1]=='\'' );
  1078. pVal = valueNew(db, pCtx);
  1079. if( !pVal ) goto no_mem;
  1080. zVal = &pExpr->u.zToken[2];
  1081. nVal = sqlite3Strlen30(zVal)-1;
  1082. assert( zVal[nVal]=='\'' );
  1083. sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
  1084. 0, SQLITE_DYNAMIC);
  1085. }
  1086. #endif
  1087. if( pVal ){
  1088. sqlite3VdbeMemStoreType(pVal);
  1089. }
  1090. *ppVal = pVal;
  1091. return rc;
  1092. no_mem:
  1093. db->mallocFailed = 1;
  1094. sqlite3DbFree(db, zVal);
  1095. assert( *ppVal==0 );
  1096. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1097. if( pCtx==0 ) sqlite3ValueFree(pVal);
  1098. #else
  1099. assert( pCtx==0 ); sqlite3ValueFree(pVal);
  1100. #endif
  1101. return SQLITE_NOMEM;
  1102. }
  1103. /*
  1104. ** Create a new sqlite3_value object, containing the value of pExpr.
  1105. **
  1106. ** This only works for very simple expressions that consist of one constant
  1107. ** token (i.e. "5", "5.1", "'a string'"). If the expression can
  1108. ** be converted directly into a value, then the value is allocated and
  1109. ** a pointer written to *ppVal. The caller is responsible for deallocating
  1110. ** the value by passing it to sqlite3ValueFree() later on. If the expression
  1111. ** cannot be converted to a value, then *ppVal is set to NULL.
  1112. */
  1113. int sqlite3ValueFromExpr(
  1114. sqlite3 *db, /* The database connection */
  1115. Expr *pExpr, /* The expression to evaluate */
  1116. u8 enc, /* Encoding to use */
  1117. u8 affinity, /* Affinity to use */
  1118. sqlite3_value **ppVal /* Write the new value here */
  1119. ){
  1120. return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
  1121. }
  1122. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1123. /*
  1124. ** The implementation of the sqlite_record() function. This function accepts
  1125. ** a single argument of any type. The return value is a formatted database
  1126. ** record (a blob) containing the argument value.
  1127. **
  1128. ** This is used to convert the value stored in the 'sample' column of the
  1129. ** sqlite_stat3 table to the record format SQLite uses internally.
  1130. */
  1131. static void recordFunc(
  1132. sqlite3_context *context,
  1133. int argc,
  1134. sqlite3_value **argv
  1135. ){
  1136. const int file_format = 1;
  1137. int iSerial; /* Serial type */
  1138. int nSerial; /* Bytes of space for iSerial as varint */
  1139. int nVal; /* Bytes of space required for argv[0] */
  1140. int nRet;
  1141. sqlite3 *db;
  1142. u8 *aRet;
  1143. UNUSED_PARAMETER( argc );
  1144. iSerial = sqlite3VdbeSerialType(argv[0], file_format);
  1145. nSerial = sqlite3VarintLen(iSerial);
  1146. nVal = sqlite3VdbeSerialTypeLen(iSerial);
  1147. db = sqlite3_context_db_handle(context);
  1148. nRet = 1 + nSerial + nVal;
  1149. aRet = sqlite3DbMallocRaw(db, nRet);
  1150. if( aRet==0 ){
  1151. sqlite3_result_error_nomem(context);
  1152. }else{
  1153. aRet[0] = nSerial+1;
  1154. sqlite3PutVarint(&aRet[1], iSerial);
  1155. sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
  1156. sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1157. sqlite3DbFree(db, aRet);
  1158. }
  1159. }
  1160. /*
  1161. ** Register built-in functions used to help read ANALYZE data.
  1162. */
  1163. void sqlite3AnalyzeFunctions(void){
  1164. static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
  1165. FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
  1166. };
  1167. int i;
  1168. FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
  1169. FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
  1170. for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
  1171. sqlite3FuncDefInsert(pHash, &aFunc[i]);
  1172. }
  1173. }
  1174. /*
  1175. ** This function is used to allocate and populate UnpackedRecord
  1176. ** structures intended to be compared against sample index keys stored
  1177. ** in the sqlite_stat4 table.
  1178. **
  1179. ** A single call to this function attempts to populates field iVal (leftmost
  1180. ** is 0 etc.) of the unpacked record with a value extracted from expression
  1181. ** pExpr. Extraction of values is possible if:
  1182. **
  1183. ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
  1184. **
  1185. ** * The expression is a bound variable, and this is a reprepare, or
  1186. **
  1187. ** * The sqlite3ValueFromExpr() function is able to extract a value
  1188. ** from the expression (i.e. the expression is a literal value).
  1189. **
  1190. ** If a value can be extracted, the affinity passed as the 5th argument
  1191. ** is applied to it before it is copied into the UnpackedRecord. Output
  1192. ** parameter *pbOk is set to true if a value is extracted, or false
  1193. ** otherwise.
  1194. **
  1195. ** When this function is called, *ppRec must either point to an object
  1196. ** allocated by an earlier call to this function, or must be NULL. If it
  1197. ** is NULL and a value can be successfully extracted, a new UnpackedRecord
  1198. ** is allocated (and *ppRec set to point to it) before returning.
  1199. **
  1200. ** Unless an error is encountered, SQLITE_OK is returned. It is not an
  1201. ** error if a value cannot be extracted from pExpr. If an error does
  1202. ** occur, an SQLite error code is returned.
  1203. */
  1204. int sqlite3Stat4ProbeSetValue(
  1205. Parse *pParse, /* Parse context */
  1206. Index *pIdx, /* Index being probed */
  1207. UnpackedRecord **ppRec, /* IN/OUT: Probe record */
  1208. Expr *pExpr, /* The expression to extract a value from */
  1209. u8 affinity, /* Affinity to use */
  1210. int iVal, /* Array element to populate */
  1211. int *pbOk /* OUT: True if value was extracted */
  1212. ){
  1213. int rc = SQLITE_OK;
  1214. sqlite3_value *pVal = 0;
  1215. sqlite3 *db = pParse->db;
  1216. struct ValueNewStat4Ctx alloc;
  1217. alloc.pParse = pParse;
  1218. alloc.pIdx = pIdx;
  1219. alloc.ppRec = ppRec;
  1220. alloc.iVal = iVal;
  1221. /* Skip over any TK_COLLATE nodes */
  1222. pExpr = sqlite3ExprSkipCollate(pExpr);
  1223. if( !pExpr ){
  1224. pVal = valueNew(db, &alloc);
  1225. if( pVal ){
  1226. sqlite3VdbeMemSetNull((Mem*)pVal);
  1227. *pbOk = 1;
  1228. }
  1229. }else if( pExpr->op==TK_VARIABLE
  1230. || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
  1231. ){
  1232. Vdbe *v;
  1233. int iBindVar = pExpr->iColumn;
  1234. sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
  1235. if( (v = pParse->pReprepare)!=0 ){
  1236. pVal = valueNew(db, &alloc);
  1237. if( pVal ){
  1238. rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
  1239. if( rc==SQLITE_OK ){
  1240. sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
  1241. }
  1242. pVal->db = pParse->db;
  1243. *pbOk = 1;
  1244. sqlite3VdbeMemStoreType((Mem*)pVal);
  1245. }
  1246. }else{
  1247. *pbOk = 0;
  1248. }
  1249. }else{
  1250. rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
  1251. *pbOk = (pVal!=0);
  1252. }
  1253. assert( pVal==0 || pVal->db==db );
  1254. return rc;
  1255. }
  1256. /*
  1257. ** Unless it is NULL, the argument must be an UnpackedRecord object returned
  1258. ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
  1259. ** the object.
  1260. */
  1261. void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
  1262. if( pRec ){
  1263. int i;
  1264. int nCol = pRec->pKeyInfo->nField+1;
  1265. Mem *aMem = pRec->aMem;
  1266. sqlite3 *db = aMem[0].db;
  1267. for(i=0; i<nCol; i++){
  1268. sqlite3DbFree(db, aMem[i].zMalloc);
  1269. }
  1270. sqlite3DbFree(db, pRec->pKeyInfo);
  1271. sqlite3DbFree(db, pRec);
  1272. }
  1273. }
  1274. #endif /* ifdef SQLITE_ENABLE_STAT4 */
  1275. /*
  1276. ** Change the string value of an sqlite3_value object
  1277. */
  1278. void sqlite3ValueSetStr(
  1279. sqlite3_value *v, /* Value to be set */
  1280. int n, /* Length of string z */
  1281. const void *z, /* Text of the new string */
  1282. u8 enc, /* Encoding to use */
  1283. void (*xDel)(void*) /* Destructor for the string */
  1284. ){
  1285. if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
  1286. }
  1287. /*
  1288. ** Free an sqlite3_value object
  1289. */
  1290. void sqlite3ValueFree(sqlite3_value *v){
  1291. if( !v ) return;
  1292. sqlite3VdbeMemRelease((Mem *)v);
  1293. sqlite3DbFree(((Mem*)v)->db, v);
  1294. }
  1295. /*
  1296. ** Return the number of bytes in the sqlite3_value object assuming
  1297. ** that it uses the encoding "enc"
  1298. */
  1299. int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
  1300. Mem *p = (Mem*)pVal;
  1301. if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
  1302. if( p->flags & MEM_Zero ){
  1303. return p->n + p->u.nZero;
  1304. }else{
  1305. return p->n;
  1306. }
  1307. }
  1308. return 0;
  1309. }