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 ** If pMem is an object with a valid string representation, this routine
25 ** ensures the internal encoding for the string representation is
26 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
28 ** If pMem is not a string object, or the encoding of the string
29 ** representation is already stored using the requested encoding, then this
30 ** routine is a no-op.
32 ** SQLITE_OK is returned if the conversion is successful (or not required).
33 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
36 int sqlite3VdbeChangeEncoding(Mem
*pMem
, int desiredEnc
){
38 if( !(pMem
->flags
&MEM_Str
) || pMem
->enc
==desiredEnc
){
41 #ifdef SQLITE_OMIT_UTF16
44 rc
= sqlite3VdbeMemTranslate(pMem
, desiredEnc
);
45 if( rc
==SQLITE_NOMEM
){
46 sqlite3VdbeMemRelease(pMem
);
47 pMem
->flags
= MEM_Null
;
55 ** Make the given Mem object MEM_Dyn.
57 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
59 int sqlite3VdbeMemDynamicify(Mem
*pMem
){
62 if( (pMem
->flags
& (MEM_Ephem
|MEM_Static
|MEM_Short
))==0 ){
65 assert( (pMem
->flags
& MEM_Dyn
)==0 );
66 assert( pMem
->flags
& (MEM_Str
|MEM_Blob
) );
67 z
= sqliteMallocRaw( n
+2 );
71 pMem
->flags
|= MEM_Dyn
|MEM_Term
;
73 memcpy(z
, pMem
->z
, n
);
77 pMem
->flags
&= ~(MEM_Ephem
|MEM_Static
|MEM_Short
);
82 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
83 ** of the Mem.z[] array can be modified.
85 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
87 int sqlite3VdbeMemMakeWriteable(Mem
*pMem
){
90 if( (pMem
->flags
& (MEM_Ephem
|MEM_Static
))==0 ){
93 assert( (pMem
->flags
& MEM_Dyn
)==0 );
94 assert( pMem
->flags
& (MEM_Str
|MEM_Blob
) );
95 if( (n
= pMem
->n
)+2<sizeof(pMem
->zShort
) ){
97 pMem
->flags
|= MEM_Short
|MEM_Term
;
99 z
= sqliteMallocRaw( n
+2 );
103 pMem
->flags
|= MEM_Dyn
|MEM_Term
;
106 memcpy(z
, pMem
->z
, n
);
110 pMem
->flags
&= ~(MEM_Ephem
|MEM_Static
);
115 ** Make sure the given Mem is \u0000 terminated.
117 int sqlite3VdbeMemNulTerminate(Mem
*pMem
){
118 /* In SQLite, a string without a nul terminator occurs when a string
119 ** is loaded from disk (in this case the memory management is ephemeral),
120 ** or when it is supplied by the user as a bound variable or function
121 ** return value. Therefore, the memory management of the string must be
122 ** either ephemeral, static or controlled by a user-supplied destructor.
125 !(pMem
->flags
&MEM_Str
) || /* it's not a string, or */
126 (pMem
->flags
&MEM_Term
) || /* it's nul term. already, or */
127 (pMem
->flags
&(MEM_Ephem
|MEM_Static
)) || /* it's static or ephem, or */
128 (pMem
->flags
&MEM_Dyn
&& pMem
->xDel
) /* external management */
130 if( (pMem
->flags
& MEM_Term
)!=0 || (pMem
->flags
& MEM_Str
)==0 ){
131 return SQLITE_OK
; /* Nothing to do */
134 if( pMem
->flags
& (MEM_Static
|MEM_Ephem
) ){
135 return sqlite3VdbeMemMakeWriteable(pMem
);
137 char *z
= sqliteMalloc(pMem
->n
+2);
138 if( !z
) return SQLITE_NOMEM
;
139 memcpy(z
, pMem
->z
, pMem
->n
);
150 ** Add MEM_Str to the set of representations for the given Mem. Numbers
151 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
154 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
156 ** A MEM_Null value will never be passed to this function. This function is
157 ** used for converting values to text for returning to the user (i.e. via
158 ** sqlite3_value_text()), or for ensuring that values to be used as btree
159 ** keys are strings. In the former case a NULL pointer is returned the
160 ** user and the later is an internal programming error.
162 int sqlite3VdbeMemStringify(Mem
*pMem
, int enc
){
164 int fg
= pMem
->flags
;
165 u8
*z
= pMem
->zShort
;
167 assert( !(fg
&(MEM_Str
|MEM_Blob
)) );
168 assert( fg
&(MEM_Int
|MEM_Real
) );
170 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
171 ** string representation of the value. Then, if the required encoding
172 ** is UTF-16le or UTF-16be do a translation.
174 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
177 sqlite3_snprintf(NBFS
, z
, "%.15g", pMem
->r
);
179 assert( fg
& MEM_Int
);
180 sqlite3_snprintf(NBFS
, z
, "%lld", pMem
->i
);
184 pMem
->enc
= SQLITE_UTF8
;
185 pMem
->flags
|= MEM_Str
| MEM_Short
| MEM_Term
;
186 sqlite3VdbeChangeEncoding(pMem
, enc
);
191 ** Release any memory held by the Mem. This may leave the Mem in an
192 ** inconsistent state, for example with (Mem.z==0) and
193 ** (Mem.type==SQLITE_TEXT).
195 void sqlite3VdbeMemRelease(Mem
*p
){
196 if( p
->flags
& MEM_Dyn
){
198 p
->xDel((void *)p
->z
);
208 ** Return some kind of integer value which is the best we can do
209 ** at representing the value that *pMem describes as an integer.
210 ** If pMem is an integer, then the value is exact. If pMem is
211 ** a floating-point then the value returned is the integer part.
212 ** If pMem is a string or blob, then we make an attempt to convert
213 ** it into a integer and return that. If pMem is NULL, return 0.
215 ** If pMem is a string, its encoding might be changed.
217 i64
sqlite3VdbeIntValue(Mem
*pMem
){
218 int flags
= pMem
->flags
;
219 if( flags
& MEM_Int
){
221 }else if( flags
& MEM_Real
){
223 }else if( flags
& (MEM_Str
|MEM_Blob
) ){
225 if( sqlite3VdbeChangeEncoding(pMem
, SQLITE_UTF8
)
226 || sqlite3VdbeMemNulTerminate(pMem
) ){
230 sqlite3atoi64(pMem
->z
, &value
);
238 ** Convert pMem to type integer. Invalidate any prior representations.
240 int sqlite3VdbeMemIntegerify(Mem
*pMem
){
241 pMem
->i
= sqlite3VdbeIntValue(pMem
);
242 sqlite3VdbeMemRelease(pMem
);
243 pMem
->flags
= MEM_Int
;
248 ** Return the best representation of pMem that we can get into a
249 ** double. If pMem is already a double or an integer, return its
250 ** value. If it is a string or blob, try to convert it to a double.
251 ** If it is a NULL, return 0.0.
253 double sqlite3VdbeRealValue(Mem
*pMem
){
254 if( pMem
->flags
& MEM_Real
){
256 }else if( pMem
->flags
& MEM_Int
){
257 return (double)pMem
->i
;
258 }else if( pMem
->flags
& (MEM_Str
|MEM_Blob
) ){
259 if( sqlite3VdbeChangeEncoding(pMem
, SQLITE_UTF8
)
260 || sqlite3VdbeMemNulTerminate(pMem
) ){
264 return sqlite3AtoF(pMem
->z
, 0);
271 ** Convert pMem so that it is of type MEM_Real. Invalidate any
272 ** prior representations.
274 int sqlite3VdbeMemRealify(Mem
*pMem
){
275 pMem
->r
= sqlite3VdbeRealValue(pMem
);
276 sqlite3VdbeMemRelease(pMem
);
277 pMem
->flags
= MEM_Real
;
282 ** Delete any previous value and set the value stored in *pMem to NULL.
284 void sqlite3VdbeMemSetNull(Mem
*pMem
){
285 sqlite3VdbeMemRelease(pMem
);
286 pMem
->flags
= MEM_Null
;
287 pMem
->type
= SQLITE_NULL
;
291 ** Delete any previous value and set the value stored in *pMem to val,
292 ** manifest type INTEGER.
294 void sqlite3VdbeMemSetInt64(Mem
*pMem
, i64 val
){
295 sqlite3VdbeMemRelease(pMem
);
297 pMem
->flags
= MEM_Int
;
298 pMem
->type
= SQLITE_INTEGER
;
302 ** Delete any previous value and set the value stored in *pMem to val,
303 ** manifest type REAL.
305 void sqlite3VdbeMemSetDouble(Mem
*pMem
, double val
){
306 sqlite3VdbeMemRelease(pMem
);
308 pMem
->flags
= MEM_Real
;
309 pMem
->type
= SQLITE_FLOAT
;
313 ** Make an shallow copy of pFrom into pTo. Prior contents of
314 ** pTo are overwritten. The pFrom->z field is not duplicated. If
315 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
316 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
318 void sqlite3VdbeMemShallowCopy(Mem
*pTo
, const Mem
*pFrom
, int srcType
){
319 memcpy(pTo
, pFrom
, sizeof(*pFrom
)-sizeof(pFrom
->zShort
));
321 if( pTo
->flags
& (MEM_Str
|MEM_Blob
) ){
322 pTo
->flags
&= ~(MEM_Dyn
|MEM_Static
|MEM_Short
|MEM_Ephem
);
323 assert( srcType
==MEM_Ephem
|| srcType
==MEM_Static
);
324 pTo
->flags
|= srcType
;
329 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
330 ** freed before the copy is made.
332 int sqlite3VdbeMemCopy(Mem
*pTo
, const Mem
*pFrom
){
334 if( pTo
->flags
& MEM_Dyn
){
335 sqlite3VdbeMemRelease(pTo
);
337 sqlite3VdbeMemShallowCopy(pTo
, pFrom
, MEM_Ephem
);
338 if( pTo
->flags
& MEM_Ephem
){
339 rc
= sqlite3VdbeMemMakeWriteable(pTo
);
347 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
348 ** freed. If pFrom contains ephemeral data, a copy is made.
350 ** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM
351 ** might be returned if pFrom held ephemeral data and we were unable
352 ** to allocate enough space to make a copy.
354 int sqlite3VdbeMemMove(Mem
*pTo
, Mem
*pFrom
){
356 if( pTo
->flags
& MEM_Dyn
){
357 sqlite3VdbeMemRelease(pTo
);
359 memcpy(pTo
, pFrom
, sizeof(Mem
));
360 if( pFrom
->flags
& MEM_Short
){
361 pTo
->z
= pTo
->zShort
;
363 pFrom
->flags
= MEM_Null
;
365 if( pTo
->flags
& MEM_Ephem
){
366 rc
= sqlite3VdbeMemMakeWriteable(pTo
);
374 ** Change the value of a Mem to be a string or a BLOB.
376 int sqlite3VdbeMemSetStr(
377 Mem
*pMem
, /* Memory cell to set to string value */
378 const char *z
, /* String pointer */
379 int n
, /* Bytes in string, or negative */
380 u8 enc
, /* Encoding of z. 0 for BLOBs */
381 void (*xDel
)(void*) /* Destructor function */
383 sqlite3VdbeMemRelease(pMem
);
385 pMem
->flags
= MEM_Null
;
386 pMem
->type
= SQLITE_NULL
;
391 if( xDel
==SQLITE_STATIC
){
392 pMem
->flags
= MEM_Static
;
393 }else if( xDel
==SQLITE_TRANSIENT
){
394 pMem
->flags
= MEM_Ephem
;
396 pMem
->flags
= MEM_Dyn
;
401 pMem
->type
= enc
==0 ? SQLITE_BLOB
: SQLITE_TEXT
;
404 assert( enc
==0 || enc
==SQLITE_UTF8
|| enc
==SQLITE_UTF16LE
405 || enc
==SQLITE_UTF16BE
);
408 pMem
->flags
|= MEM_Blob
;
412 pMem
->flags
|= MEM_Str
;
415 pMem
->flags
|= MEM_Term
;
419 #ifndef SQLITE_OMIT_UTF16
422 pMem
->flags
|= MEM_Str
;
424 pMem
->n
= sqlite3utf16ByteLen(pMem
->z
,-1);
425 pMem
->flags
|= MEM_Term
;
427 if( sqlite3VdbeMemHandleBom(pMem
) ){
430 #endif /* SQLITE_OMIT_UTF16 */
432 if( pMem
->flags
&MEM_Ephem
){
433 return sqlite3VdbeMemMakeWriteable(pMem
);
439 ** Compare the values contained by the two memory cells, returning
440 ** negative, zero or positive if pMem1 is less than, equal to, or greater
441 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
442 ** and reals) sorted numerically, followed by text ordered by the collating
443 ** sequence pColl and finally blob's ordered by memcmp().
445 ** Two NULL values are considered equal by this function.
447 int sqlite3MemCompare(const Mem
*pMem1
, const Mem
*pMem2
, const CollSeq
*pColl
){
452 /* Interchange pMem1 and pMem2 if the collating sequence specifies
457 combined_flags
= f1
|f2
;
459 /* If one value is NULL, it is less than the other. If both values
460 ** are NULL, return 0.
462 if( combined_flags
&MEM_Null
){
463 return (f2
&MEM_Null
) - (f1
&MEM_Null
);
466 /* If one value is a number and the other is not, the number is less.
467 ** If both are numbers, compare as reals if one is a real, or as integers
468 ** if both values are integers.
470 if( combined_flags
&(MEM_Int
|MEM_Real
) ){
471 if( !(f1
&(MEM_Int
|MEM_Real
)) ){
474 if( !(f2
&(MEM_Int
|MEM_Real
)) ){
477 if( (f1
& f2
& MEM_Int
)==0 ){
479 if( (f1
&MEM_Real
)==0 ){
484 if( (f2
&MEM_Real
)==0 ){
489 if( r1
<r2
) return -1;
490 if( r1
>r2
) return 1;
493 assert( f1
&MEM_Int
);
494 assert( f2
&MEM_Int
);
495 if( pMem1
->i
< pMem2
->i
) return -1;
496 if( pMem1
->i
> pMem2
->i
) return 1;
501 /* If one value is a string and the other is a blob, the string is less.
502 ** If both are strings, compare using the collating functions.
504 if( combined_flags
&MEM_Str
){
505 if( (f1
& MEM_Str
)==0 ){
508 if( (f2
& MEM_Str
)==0 ){
512 assert( pMem1
->enc
==pMem2
->enc
);
513 assert( pMem1
->enc
==SQLITE_UTF8
||
514 pMem1
->enc
==SQLITE_UTF16LE
|| pMem1
->enc
==SQLITE_UTF16BE
);
516 /* This assert may fail if the collation sequence is deleted after this
517 ** vdbe program is compiled. The documentation defines this as an
518 ** undefined condition. A crash is usual result.
520 assert( !pColl
|| pColl
->xCmp
);
523 if( pMem1
->enc
==pColl
->enc
){
524 return pColl
->xCmp(pColl
->pUser
,pMem1
->n
,pMem1
->z
,pMem2
->n
,pMem2
->z
);
526 u8 origEnc
= pMem1
->enc
;
529 sqlite3ValueBytes((sqlite3_value
*)pMem1
, pColl
->enc
),
530 sqlite3ValueText((sqlite3_value
*)pMem1
, pColl
->enc
),
531 sqlite3ValueBytes((sqlite3_value
*)pMem2
, pColl
->enc
),
532 sqlite3ValueText((sqlite3_value
*)pMem2
, pColl
->enc
)
534 sqlite3ValueBytes((sqlite3_value
*)pMem1
, origEnc
);
535 sqlite3ValueText((sqlite3_value
*)pMem1
, origEnc
);
536 sqlite3ValueBytes((sqlite3_value
*)pMem2
, origEnc
);
537 sqlite3ValueText((sqlite3_value
*)pMem2
, origEnc
);
541 /* If a NULL pointer was passed as the collate function, fall through
542 ** to the blob case and use memcmp(). */
545 /* Both values must be blobs. Compare using memcmp(). */
546 rc
= memcmp(pMem1
->z
, pMem2
->z
, (pMem1
->n
>pMem2
->n
)?pMem2
->n
:pMem1
->n
);
548 rc
= pMem1
->n
- pMem2
->n
;
554 ** Move data out of a btree key or data field and into a Mem structure.
555 ** The data or key is taken from the entry that pCur is currently pointing
556 ** to. offset and amt determine what portion of the data or key to retrieve.
557 ** key is true to get the key or false to get data. The result is written
558 ** into the pMem element.
560 ** The pMem structure is assumed to be uninitialized. Any prior content
561 ** is overwritten without being freed.
563 ** If this routine fails for any reason (malloc returns NULL or unable
564 ** to read from the disk) then the pMem is left in an inconsistent state.
566 int sqlite3VdbeMemFromBtree(
567 BtCursor
*pCur
, /* Cursor pointing at record to retrieve. */
568 int offset
, /* Offset from the start of data to return bytes from. */
569 int amt
, /* Number of bytes to return. */
570 int key
, /* If true, retrieve from the btree key, not data. */
571 Mem
*pMem
/* OUT: Return data in this Mem structure. */
573 char *zData
; /* Data from the btree layer */
574 int available
; /* Number of bytes available on the local btree page */
577 zData
= (char *)sqlite3BtreeKeyFetch(pCur
, &available
);
579 zData
= (char *)sqlite3BtreeDataFetch(pCur
, &available
);
583 if( offset
+amt
<=available
){
584 pMem
->z
= &zData
[offset
];
585 pMem
->flags
= MEM_Blob
|MEM_Ephem
;
589 zData
= (char *)sqliteMallocRaw(amt
+2);
593 pMem
->flags
= MEM_Blob
|MEM_Dyn
|MEM_Term
;
596 zData
= &(pMem
->zShort
[0]);
597 pMem
->flags
= MEM_Blob
|MEM_Short
|MEM_Term
;
601 pMem
->type
= SQLITE_BLOB
;
604 rc
= sqlite3BtreeKey(pCur
, offset
, amt
, zData
);
606 rc
= sqlite3BtreeData(pCur
, offset
, amt
, zData
);
612 assert( zData
!=pMem
->zShort
);
613 assert( pMem
->flags
& MEM_Dyn
);
616 assert( zData
==pMem
->zShort
);
617 assert( pMem
->flags
& MEM_Short
);
628 ** Perform various checks on the memory cell pMem. An assert() will
629 ** fail if pMem is internally inconsistent.
631 void sqlite3VdbeMemSanity(Mem
*pMem
, u8 db_enc
){
632 int flags
= pMem
->flags
;
633 assert( flags
!=0 ); /* Must define some type */
634 if( pMem
->flags
& (MEM_Str
|MEM_Blob
) ){
635 int x
= pMem
->flags
& (MEM_Static
|MEM_Dyn
|MEM_Ephem
|MEM_Short
);
636 assert( x
!=0 ); /* Strings must define a string subtype */
637 assert( (x
& (x
-1))==0 ); /* Only one string subtype can be defined */
638 assert( pMem
->z
!=0 ); /* Strings must have a value */
639 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
640 assert( (pMem
->flags
& MEM_Short
)==0 || pMem
->z
==pMem
->zShort
);
641 assert( (pMem
->flags
& MEM_Short
)!=0 || pMem
->z
!=pMem
->zShort
);
642 /* No destructor unless there is MEM_Dyn */
643 assert( pMem
->xDel
==0 || (pMem
->flags
& MEM_Dyn
)!=0 );
645 if( (flags
& MEM_Str
) ){
646 assert( pMem
->enc
==SQLITE_UTF8
||
647 pMem
->enc
==SQLITE_UTF16BE
||
648 pMem
->enc
==SQLITE_UTF16LE
650 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
651 ** must be the length of the string. (Later:) If the database file
652 ** has been corrupted, '\000' characters might have been inserted
653 ** into the middle of the string. In that case, the strlen() might
656 if( pMem
->enc
==SQLITE_UTF8
&& (flags
& MEM_Term
) ){
657 assert( strlen(pMem
->z
)<=pMem
->n
);
658 assert( pMem
->z
[pMem
->n
]==0 );
662 /* Cannot define a string subtype for non-string objects */
663 assert( (pMem
->flags
& (MEM_Static
|MEM_Dyn
|MEM_Ephem
|MEM_Short
))==0 );
664 assert( pMem
->xDel
==0 );
666 /* MEM_Null excludes all other types */
667 assert( (pMem
->flags
&(MEM_Str
|MEM_Int
|MEM_Real
|MEM_Blob
))==0
668 || (pMem
->flags
&MEM_Null
)==0 );
669 if( (pMem
->flags
& (MEM_Int
|MEM_Real
))==(MEM_Int
|MEM_Real
) ){
670 assert( pMem
->r
==pMem
->i
);
675 /* This function is only available internally, it is not part of the
676 ** external API. It works in a similar way to sqlite3_value_text(),
677 ** except the data returned is in the encoding specified by the second
678 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
681 const void *sqlite3ValueText(sqlite3_value
* pVal
, u8 enc
){
682 if( !pVal
) return 0;
683 assert( enc
==SQLITE_UTF16LE
|| enc
==SQLITE_UTF16BE
|| enc
==SQLITE_UTF8
);
685 if( pVal
->flags
&MEM_Null
){
688 if( pVal
->flags
&MEM_Str
){
689 sqlite3VdbeChangeEncoding(pVal
, enc
);
690 }else if( !(pVal
->flags
&MEM_Blob
) ){
691 sqlite3VdbeMemStringify(pVal
, enc
);
693 return (const void *)(pVal
->z
);
697 ** Create a new sqlite3_value object.
699 sqlite3_value
* sqlite3ValueNew(){
700 Mem
*p
= sqliteMalloc(sizeof(*p
));
703 p
->type
= SQLITE_NULL
;
709 ** Create a new sqlite3_value object, containing the value of pExpr.
711 ** This only works for very simple expressions that consist of one constant
712 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
713 ** be converted directly into a value, then the value is allocated and
714 ** a pointer written to *ppVal. The caller is responsible for deallocating
715 ** the value by passing it to sqlite3ValueFree() later on. If the expression
716 ** cannot be converted to a value, then *ppVal is set to NULL.
718 int sqlite3ValueFromExpr(
722 sqlite3_value
**ppVal
726 sqlite3_value
*pVal
= 0;
734 if( op
==TK_STRING
|| op
==TK_FLOAT
|| op
==TK_INTEGER
){
735 zVal
= sqliteStrNDup(pExpr
->token
.z
, pExpr
->token
.n
);
736 pVal
= sqlite3ValueNew();
737 if( !zVal
|| !pVal
) goto no_mem
;
738 sqlite3Dequote(zVal
);
739 sqlite3ValueSetStr(pVal
, -1, zVal
, SQLITE_UTF8
, sqlite3FreeX
);
740 if( (op
==TK_INTEGER
|| op
==TK_FLOAT
) && affinity
==SQLITE_AFF_NONE
){
741 sqlite3ValueApplyAffinity(pVal
, SQLITE_AFF_NUMERIC
, enc
);
743 sqlite3ValueApplyAffinity(pVal
, affinity
, enc
);
745 }else if( op
==TK_UMINUS
) {
746 if( SQLITE_OK
==sqlite3ValueFromExpr(pExpr
->pLeft
, enc
, affinity
, &pVal
) ){
747 pVal
->i
= -1 * pVal
->i
;
748 pVal
->r
= -1.0 * pVal
->r
;
751 #ifndef SQLITE_OMIT_BLOB_LITERAL
752 else if( op
==TK_BLOB
){
754 pVal
= sqlite3ValueNew();
755 zVal
= sqliteStrNDup(pExpr
->token
.z
+1, pExpr
->token
.n
-1);
756 if( !zVal
|| !pVal
) goto no_mem
;
757 sqlite3Dequote(zVal
);
758 nVal
= strlen(zVal
)/2;
759 sqlite3VdbeMemSetStr(pVal
, sqlite3HexToBlob(zVal
), nVal
, 0, sqlite3FreeX
);
769 sqlite3ValueFree(pVal
);
775 ** Change the string value of an sqlite3_value object
777 void sqlite3ValueSetStr(
784 if( v
) sqlite3VdbeMemSetStr((Mem
*)v
, z
, n
, enc
, xDel
);
788 ** Free an sqlite3_value object
790 void sqlite3ValueFree(sqlite3_value
*v
){
792 sqlite3ValueSetStr(v
, 0, 0, SQLITE_UTF8
, SQLITE_STATIC
);
797 ** Return the number of bytes in the sqlite3_value object assuming
798 ** that it uses the encoding "enc"
800 int sqlite3ValueBytes(sqlite3_value
*pVal
, u8 enc
){
802 if( (p
->flags
& MEM_Blob
)!=0 || sqlite3ValueText(pVal
, enc
) ){