4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
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
18 #include "sqliteInt.h"
24 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
27 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
30 ** If pMem is an object with a valid string representation, this routine
31 ** ensures the internal encoding for the string representation is
32 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
34 ** If pMem is not a string object, or the encoding of the string
35 ** representation is already stored using the requested encoding, then this
36 ** routine is a no-op.
38 ** SQLITE_OK is returned if the conversion is successful (or not required).
39 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
42 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
44 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
47 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
48 #ifdef SQLITE_OMIT_UTF16
52 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
53 ** then the encoding of the value may not have changed.
55 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
56 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
57 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
58 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
64 ** Make the given Mem object MEM_Dyn.
66 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
68 int sqlite3VdbeMemDynamicify(Mem *pMem){
71 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
73 if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
76 assert( (pMem->flags & MEM_Dyn)==0 );
78 assert( pMem->flags & (MEM_Str|MEM_Blob) );
79 z = sqlite3DbMallocRaw(pMem->db, n+2 );
83 pMem->flags |= MEM_Dyn|MEM_Term;
85 memcpy(z, pMem->z, n );
89 pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
94 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
95 ** blob stored in dynamically allocated space.
97 #ifndef SQLITE_OMIT_INCRBLOB
98 int sqlite3VdbeMemExpandBlob(Mem *pMem){
99 if( pMem->flags & MEM_Zero ){
102 assert( (pMem->flags & MEM_Blob)!=0 );
103 nByte = pMem->n + pMem->u.i;
104 if( nByte<=0 ) nByte = 1;
105 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
106 pNew = sqlite3DbMallocRaw(pMem->db, nByte);
110 memcpy(pNew, pMem->z, pMem->n);
111 memset(&pNew[pMem->n], 0, pMem->u.i);
112 sqlite3VdbeMemRelease(pMem);
114 pMem->n += pMem->u.i;
116 pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);
117 pMem->flags |= MEM_Dyn;
125 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
126 ** of the Mem.z[] array can be modified.
128 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
130 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
133 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
135 if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
138 assert( (pMem->flags & MEM_Dyn)==0 );
139 assert( pMem->flags & (MEM_Str|MEM_Blob) );
140 if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
141 z = (u8*)pMem->zShort;
142 pMem->flags |= MEM_Short|MEM_Term;
144 z = sqlite3DbMallocRaw(pMem->db, n+2 );
148 pMem->flags |= MEM_Dyn|MEM_Term;
151 memcpy(z, pMem->z, n );
155 pMem->flags &= ~(MEM_Ephem|MEM_Static);
156 assert(0==(1&(int)pMem->z));
161 ** Make sure the given Mem is \u0000 terminated.
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 */
168 if( pMem->flags & (MEM_Static|MEM_Ephem) ){
169 return sqlite3VdbeMemMakeWriteable(pMem);
172 sqlite3VdbeMemExpandBlob(pMem);
173 z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);
177 memcpy(z, pMem->z, pMem->n);
183 sqlite3_free(pMem->z);
187 pMem->flags |= MEM_Term;
193 ** Add MEM_Str to the set of representations for the given Mem. Numbers
194 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
197 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
199 ** A MEM_Null value will never be passed to this function. This function is
200 ** used for converting values to text for returning to the user (i.e. via
201 ** sqlite3_value_text()), or for ensuring that values to be used as btree
202 ** keys are strings. In the former case a NULL pointer is returned the
203 ** user and the later is an internal programming error.
205 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
207 int fg = pMem->flags;
208 char *z = pMem->zShort;
210 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
211 assert( !(fg&MEM_Zero) );
212 assert( !(fg&(MEM_Str|MEM_Blob)) );
213 assert( fg&(MEM_Int|MEM_Real) );
215 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
216 ** string representation of the value. Then, if the required encoding
217 ** is UTF-16le or UTF-16be do a translation.
219 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
222 sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i);
224 assert( fg & MEM_Real );
225 sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
229 pMem->enc = SQLITE_UTF8;
230 pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
231 sqlite3VdbeChangeEncoding(pMem, enc);
236 ** Memory cell pMem contains the context of an aggregate function.
237 ** This routine calls the finalize method for that function. The
238 ** result of the aggregate is stored back into pMem.
240 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
243 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
245 if( pFunc && pFunc->xFinalize ){
247 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
248 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
249 ctx.s.flags = MEM_Null;
250 ctx.s.z = pMem->zShort;
255 pFunc->xFinalize(&ctx);
256 if( pMem->z && pMem->z!=pMem->zShort ){
257 sqlite3_free( pMem->z );
260 if( pMem->flags & MEM_Short ){
261 pMem->z = pMem->zShort;
263 rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
269 ** Release any memory held by the Mem. This may leave the Mem in an
270 ** inconsistent state, for example with (Mem.z==0) and
271 ** (Mem.type==SQLITE_TEXT).
273 void sqlite3VdbeMemRelease(Mem *p){
274 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
275 if( p->flags & (MEM_Dyn|MEM_Agg) ){
277 if( p->flags & MEM_Agg ){
278 sqlite3VdbeMemFinalize(p, p->u.pDef);
279 assert( (p->flags & MEM_Agg)==0 );
280 sqlite3VdbeMemRelease(p);
282 p->xDel((void *)p->z);
293 ** Return some kind of integer value which is the best we can do
294 ** at representing the value that *pMem describes as an integer.
295 ** If pMem is an integer, then the value is exact. If pMem is
296 ** a floating-point then the value returned is the integer part.
297 ** If pMem is a string or blob, then we make an attempt to convert
298 ** it into a integer and return that. If pMem is NULL, return 0.
300 ** If pMem is a string, its encoding might be changed.
302 i64 sqlite3VdbeIntValue(Mem *pMem){
304 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
306 if( flags & MEM_Int ){
308 }else if( flags & MEM_Real ){
310 }else if( flags & (MEM_Str|MEM_Blob) ){
312 pMem->flags |= MEM_Str;
313 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
314 || sqlite3VdbeMemNulTerminate(pMem) ){
318 sqlite3Atoi64(pMem->z, &value);
326 ** Return the best representation of pMem that we can get into a
327 ** double. If pMem is already a double or an integer, return its
328 ** value. If it is a string or blob, try to convert it to a double.
329 ** If it is a NULL, return 0.0.
331 double sqlite3VdbeRealValue(Mem *pMem){
332 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
333 if( pMem->flags & MEM_Real ){
335 }else if( pMem->flags & MEM_Int ){
336 return (double)pMem->u.i;
337 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
339 pMem->flags |= MEM_Str;
340 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
341 || sqlite3VdbeMemNulTerminate(pMem) ){
345 sqlite3AtoF(pMem->z, &val);
353 ** The MEM structure is already a MEM_Real. Try to also make it a
354 ** MEM_Int if we can.
356 void sqlite3VdbeIntegerAffinity(Mem *pMem){
357 assert( pMem->flags & MEM_Real );
358 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
360 if( ((double)pMem->u.i)==pMem->r ){
361 pMem->flags |= MEM_Int;
366 ** Convert pMem to type integer. Invalidate any prior representations.
368 int sqlite3VdbeMemIntegerify(Mem *pMem){
369 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
370 pMem->u.i = sqlite3VdbeIntValue(pMem);
371 sqlite3VdbeMemRelease(pMem);
372 pMem->flags = MEM_Int;
377 ** Convert pMem so that it is of type MEM_Real.
378 ** Invalidate any prior representations.
380 int sqlite3VdbeMemRealify(Mem *pMem){
381 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
382 pMem->r = sqlite3VdbeRealValue(pMem);
383 sqlite3VdbeMemRelease(pMem);
384 pMem->flags = MEM_Real;
389 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
390 ** Invalidate any prior representations.
392 int sqlite3VdbeMemNumerify(Mem *pMem){
395 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
396 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
397 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
398 r1 = sqlite3VdbeRealValue(pMem);
402 sqlite3VdbeMemIntegerify(pMem);
405 pMem->flags = MEM_Real;
406 sqlite3VdbeMemRelease(pMem);
412 ** Delete any previous value and set the value stored in *pMem to NULL.
414 void sqlite3VdbeMemSetNull(Mem *pMem){
415 sqlite3VdbeMemRelease(pMem);
416 pMem->flags = MEM_Null;
417 pMem->type = SQLITE_NULL;
422 ** Delete any previous value and set the value to be a BLOB of length
423 ** n containing all zeros.
425 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
426 sqlite3VdbeMemRelease(pMem);
427 pMem->flags = MEM_Blob|MEM_Zero|MEM_Short;
428 pMem->type = SQLITE_BLOB;
432 pMem->z = pMem->zShort;
433 pMem->enc = SQLITE_UTF8;
437 ** Delete any previous value and set the value stored in *pMem to val,
438 ** manifest type INTEGER.
440 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
441 sqlite3VdbeMemRelease(pMem);
443 pMem->flags = MEM_Int;
444 pMem->type = SQLITE_INTEGER;
448 ** Delete any previous value and set the value stored in *pMem to val,
449 ** manifest type REAL.
451 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
452 if( sqlite3_isnan(val) ){
453 sqlite3VdbeMemSetNull(pMem);
455 sqlite3VdbeMemRelease(pMem);
457 pMem->flags = MEM_Real;
458 pMem->type = SQLITE_FLOAT;
463 ** Return true if the Mem object contains a TEXT or BLOB that is
464 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
466 int sqlite3VdbeMemTooBig(Mem *p){
467 if( p->flags & (MEM_Str|MEM_Blob) ){
469 if( p->flags & MEM_Zero ){
472 return n>SQLITE_MAX_LENGTH;
478 ** Make an shallow copy of pFrom into pTo. Prior contents of
479 ** pTo are overwritten. The pFrom->z field is not duplicated. If
480 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
481 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
483 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
484 memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
486 if( pTo->flags & (MEM_Str|MEM_Blob) ){
487 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
488 assert( srcType==MEM_Ephem || srcType==MEM_Static );
489 pTo->flags |= srcType;
494 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
495 ** freed before the copy is made.
497 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
499 if( pTo->flags & MEM_Dyn ){
500 sqlite3VdbeMemRelease(pTo);
502 sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
503 if( pTo->flags & MEM_Ephem ){
504 rc = sqlite3VdbeMemMakeWriteable(pTo);
512 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
513 ** freed. If pFrom contains ephemeral data, a copy is made.
515 ** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM
516 ** might be returned if pFrom held ephemeral data and we were unable
517 ** to allocate enough space to make a copy.
519 int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
521 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
522 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
523 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
524 if( pTo->flags & MEM_Dyn ){
525 sqlite3VdbeMemRelease(pTo);
527 memcpy(pTo, pFrom, sizeof(Mem));
528 if( pFrom->flags & MEM_Short ){
529 pTo->z = pTo->zShort;
531 pFrom->flags = MEM_Null;
533 if( pTo->flags & MEM_Ephem ){
534 rc = sqlite3VdbeMemMakeWriteable(pTo);
542 ** Change the value of a Mem to be a string or a BLOB.
544 int sqlite3VdbeMemSetStr(
545 Mem *pMem, /* Memory cell to set to string value */
546 const char *z, /* String pointer */
547 int n, /* Bytes in string, or negative */
548 u8 enc, /* Encoding of z. 0 for BLOBs */
549 void (*xDel)(void*) /* Destructor function */
551 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
552 sqlite3VdbeMemRelease(pMem);
554 pMem->flags = MEM_Null;
555 pMem->type = SQLITE_NULL;
559 if( xDel==SQLITE_STATIC ){
560 pMem->flags = MEM_Static;
561 }else if( xDel==SQLITE_TRANSIENT ){
562 pMem->flags = MEM_Ephem;
564 pMem->flags = MEM_Dyn;
569 pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
572 assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
573 || enc==SQLITE_UTF16BE );
576 pMem->flags |= MEM_Blob;
577 pMem->enc = SQLITE_UTF8;
581 pMem->flags |= MEM_Str;
584 pMem->flags |= MEM_Term;
588 #ifndef SQLITE_OMIT_UTF16
591 pMem->flags |= MEM_Str;
593 pMem->n = sqlite3Utf16ByteLen(pMem->z,-1);
594 pMem->flags |= MEM_Term;
596 if( sqlite3VdbeMemHandleBom(pMem) ){
599 #endif /* SQLITE_OMIT_UTF16 */
601 if( pMem->flags&MEM_Ephem ){
602 return sqlite3VdbeMemMakeWriteable(pMem);
608 ** Compare the values contained by the two memory cells, returning
609 ** negative, zero or positive if pMem1 is less than, equal to, or greater
610 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
611 ** and reals) sorted numerically, followed by text ordered by the collating
612 ** sequence pColl and finally blob's ordered by memcmp().
614 ** Two NULL values are considered equal by this function.
616 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
621 /* Interchange pMem1 and pMem2 if the collating sequence specifies
626 combined_flags = f1|f2;
628 /* If one value is NULL, it is less than the other. If both values
629 ** are NULL, return 0.
631 if( combined_flags&MEM_Null ){
632 return (f2&MEM_Null) - (f1&MEM_Null);
635 /* If one value is a number and the other is not, the number is less.
636 ** If both are numbers, compare as reals if one is a real, or as integers
637 ** if both values are integers.
639 if( combined_flags&(MEM_Int|MEM_Real) ){
640 if( !(f1&(MEM_Int|MEM_Real)) ){
643 if( !(f2&(MEM_Int|MEM_Real)) ){
646 if( (f1 & f2 & MEM_Int)==0 ){
648 if( (f1&MEM_Real)==0 ){
653 if( (f2&MEM_Real)==0 ){
658 if( r1<r2 ) return -1;
659 if( r1>r2 ) return 1;
662 assert( f1&MEM_Int );
663 assert( f2&MEM_Int );
664 if( pMem1->u.i < pMem2->u.i ) return -1;
665 if( pMem1->u.i > pMem2->u.i ) return 1;
670 /* If one value is a string and the other is a blob, the string is less.
671 ** If both are strings, compare using the collating functions.
673 if( combined_flags&MEM_Str ){
674 if( (f1 & MEM_Str)==0 ){
677 if( (f2 & MEM_Str)==0 ){
681 assert( pMem1->enc==pMem2->enc );
682 assert( pMem1->enc==SQLITE_UTF8 ||
683 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
685 /* The collation sequence must be defined at this point, even if
686 ** the user deletes the collation sequence after the vdbe program is
687 ** compiled (this was not always the case).
689 assert( !pColl || pColl->xCmp );
692 if( pMem1->enc==pColl->enc ){
693 /* The strings are already in the correct encoding. Call the
694 ** comparison function directly */
695 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
697 u8 origEnc = pMem1->enc;
700 /* Convert the strings into the encoding that the comparison
701 ** function expects */
702 v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
703 n1 = v1==0 ? 0 : pMem1->n;
704 assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
705 v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
706 n2 = v2==0 ? 0 : pMem2->n;
707 assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
708 /* Do the comparison */
709 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
710 /* Convert the strings back into the database encoding */
711 sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
712 sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
716 /* If a NULL pointer was passed as the collate function, fall through
717 ** to the blob case and use memcmp(). */
720 /* Both values must be blobs. Compare using memcmp(). */
721 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
723 rc = pMem1->n - pMem2->n;
729 ** Move data out of a btree key or data field and into a Mem structure.
730 ** The data or key is taken from the entry that pCur is currently pointing
731 ** to. offset and amt determine what portion of the data or key to retrieve.
732 ** key is true to get the key or false to get data. The result is written
733 ** into the pMem element.
735 ** The pMem structure is assumed to be uninitialized. Any prior content
736 ** is overwritten without being freed.
738 ** If this routine fails for any reason (malloc returns NULL or unable
739 ** to read from the disk) then the pMem is left in an inconsistent state.
741 int sqlite3VdbeMemFromBtree(
742 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
743 int offset, /* Offset from the start of data to return bytes from. */
744 int amt, /* Number of bytes to return. */
745 int key, /* If true, retrieve from the btree key, not data. */
746 Mem *pMem /* OUT: Return data in this Mem structure. */
748 char *zData; /* Data from the btree layer */
749 int available = 0; /* Number of bytes available on the local btree page */
750 sqlite3 *db; /* Database connection */
752 db = sqlite3BtreeCursorDb(pCur);
753 assert( sqlite3_mutex_held(db->mutex) );
755 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
757 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
763 if( offset+amt<=available ){
764 pMem->z = &zData[offset];
765 pMem->flags = MEM_Blob|MEM_Ephem;
769 zData = (char *)sqlite3DbMallocRaw(db, amt+2);
773 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
776 zData = &(pMem->zShort[0]);
777 pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
781 pMem->type = SQLITE_BLOB;
784 rc = sqlite3BtreeKey(pCur, offset, amt, zData);
786 rc = sqlite3BtreeData(pCur, offset, amt, zData);
792 assert( zData!=pMem->zShort );
793 assert( pMem->flags & MEM_Dyn );
796 assert( zData==pMem->zShort );
797 assert( pMem->flags & MEM_Short );
808 ** Perform various checks on the memory cell pMem. An assert() will
809 ** fail if pMem is internally inconsistent.
811 void sqlite3VdbeMemSanity(Mem *pMem){
812 int flags = pMem->flags;
813 assert( flags!=0 ); /* Must define some type */
814 if( flags & (MEM_Str|MEM_Blob) ){
815 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
816 assert( x!=0 ); /* Strings must define a string subtype */
817 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
818 assert( pMem->z!=0 ); /* Strings must have a value */
819 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
820 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
821 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
822 /* No destructor unless there is MEM_Dyn */
823 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
825 if( (flags & MEM_Str) ){
826 assert( pMem->enc==SQLITE_UTF8 ||
827 pMem->enc==SQLITE_UTF16BE ||
828 pMem->enc==SQLITE_UTF16LE
830 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
831 ** must be the length of the string. (Later:) If the database file
832 ** has been corrupted, '\000' characters might have been inserted
833 ** into the middle of the string. In that case, the strlen() might
836 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
837 assert( strlen(pMem->z)<=pMem->n );
838 assert( pMem->z[pMem->n]==0 );
842 /* Cannot define a string subtype for non-string objects */
843 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
844 assert( pMem->xDel==0 );
846 /* MEM_Null excludes all other types */
847 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
848 || (pMem->flags&MEM_Null)==0 );
849 /* If the MEM is both real and integer, the values are equal */
850 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
851 || pMem->r==pMem->u.i );
855 /* This function is only available internally, it is not part of the
856 ** external API. It works in a similar way to sqlite3_value_text(),
857 ** except the data returned is in the encoding specified by the second
858 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
861 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
862 ** If that is the case, then the result must be aligned on an even byte
865 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
866 if( !pVal ) return 0;
868 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
869 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
871 if( pVal->flags&MEM_Null ){
874 assert( (MEM_Blob>>3) == MEM_Str );
875 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
877 if( pVal->flags&MEM_Str ){
878 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
879 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
880 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
881 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
885 sqlite3VdbeMemNulTerminate(pVal);
887 assert( (pVal->flags&MEM_Blob)==0 );
888 sqlite3VdbeMemStringify(pVal, enc);
889 assert( 0==(1&(int)pVal->z) );
891 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
892 || pVal->db->mallocFailed );
893 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
901 ** Create a new sqlite3_value object.
903 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
904 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
907 p->type = SQLITE_NULL;
914 ** Create a new sqlite3_value object, containing the value of pExpr.
916 ** This only works for very simple expressions that consist of one constant
917 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
918 ** be converted directly into a value, then the value is allocated and
919 ** a pointer written to *ppVal. The caller is responsible for deallocating
920 ** the value by passing it to sqlite3ValueFree() later on. If the expression
921 ** cannot be converted to a value, then *ppVal is set to NULL.
923 int sqlite3ValueFromExpr(
924 sqlite3 *db, /* The database connection */
925 Expr *pExpr, /* The expression to evaluate */
926 u8 enc, /* Encoding to use */
927 u8 affinity, /* Affinity to use */
928 sqlite3_value **ppVal /* Write the new value here */
932 sqlite3_value *pVal = 0;
940 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
941 zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
942 pVal = sqlite3ValueNew(db);
943 if( !zVal || !pVal ) goto no_mem;
944 sqlite3Dequote(zVal);
945 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
946 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
947 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
949 sqlite3ValueApplyAffinity(pVal, affinity, enc);
951 }else if( op==TK_UMINUS ) {
952 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
953 pVal->u.i = -1 * pVal->u.i;
954 pVal->r = -1.0 * pVal->r;
957 #ifndef SQLITE_OMIT_BLOB_LITERAL
958 else if( op==TK_BLOB ){
960 pVal = sqlite3ValueNew(db);
961 zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
962 if( !zVal || !pVal ) goto no_mem;
963 sqlite3Dequote(zVal);
964 nVal = strlen(zVal)/2;
965 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free);
974 db->mallocFailed = 1;
976 sqlite3ValueFree(pVal);
982 ** Change the string value of an sqlite3_value object
984 void sqlite3ValueSetStr(
985 sqlite3_value *v, /* Value to be set */
986 int n, /* Length of string z */
987 const void *z, /* Text of the new string */
988 u8 enc, /* Encoding to use */
989 void (*xDel)(void*) /* Destructor for the string */
991 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
995 ** Free an sqlite3_value object
997 void sqlite3ValueFree(sqlite3_value *v){
999 sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
1004 ** Return the number of bytes in the sqlite3_value object assuming
1005 ** that it uses the encoding "enc"
1007 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1008 Mem *p = (Mem*)pVal;
1009 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1010 if( p->flags & MEM_Zero ){