Build Lunapaint from Contrib.
[AROS-Contrib.git] / sqlite3 / vdbemem.c
blobf08671f249c473e9b92f783347857018134578a9
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.
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
16 ** name sqlite_value
18 #include "sqliteInt.h"
19 #include "os.h"
20 #include <ctype.h>
21 #include "vdbeInt.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
34 ** between formats.
36 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
37 int rc;
38 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
39 return SQLITE_OK;
41 #ifdef SQLITE_OMIT_UTF16
42 return SQLITE_ERROR;
43 #else
44 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
45 if( rc==SQLITE_NOMEM ){
46 sqlite3VdbeMemRelease(pMem);
47 pMem->flags = MEM_Null;
48 pMem->z = 0;
50 return rc;
51 #endif
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){
60 int n = pMem->n;
61 u8 *z;
62 if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
63 return SQLITE_OK;
65 assert( (pMem->flags & MEM_Dyn)==0 );
66 assert( pMem->flags & (MEM_Str|MEM_Blob) );
67 z = sqliteMallocRaw( n+2 );
68 if( z==0 ){
69 return SQLITE_NOMEM;
71 pMem->flags |= MEM_Dyn|MEM_Term;
72 pMem->xDel = 0;
73 memcpy(z, pMem->z, n );
74 z[n] = 0;
75 z[n+1] = 0;
76 pMem->z = z;
77 pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
78 return SQLITE_OK;
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){
88 int n;
89 u8 *z;
90 if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
91 return SQLITE_OK;
93 assert( (pMem->flags & MEM_Dyn)==0 );
94 assert( pMem->flags & (MEM_Str|MEM_Blob) );
95 if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
96 z = pMem->zShort;
97 pMem->flags |= MEM_Short|MEM_Term;
98 }else{
99 z = sqliteMallocRaw( n+2 );
100 if( z==0 ){
101 return SQLITE_NOMEM;
103 pMem->flags |= MEM_Dyn|MEM_Term;
104 pMem->xDel = 0;
106 memcpy(z, pMem->z, n );
107 z[n] = 0;
108 z[n+1] = 0;
109 pMem->z = z;
110 pMem->flags &= ~(MEM_Ephem|MEM_Static);
111 return SQLITE_OK;
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.
124 assert(
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);
136 }else{
137 char *z = sqliteMalloc(pMem->n+2);
138 if( !z ) return SQLITE_NOMEM;
139 memcpy(z, pMem->z, pMem->n);
140 z[pMem->n] = 0;
141 z[pMem->n+1] = 0;
142 pMem->xDel(pMem->z);
143 pMem->xDel = 0;
144 pMem->z = z;
146 return SQLITE_OK;
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
152 ** is a no-op.
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){
163 int rc = SQLITE_OK;
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.
176 if( fg & MEM_Real ){
177 sqlite3_snprintf(NBFS, z, "%.15g", pMem->r);
178 }else{
179 assert( fg & MEM_Int );
180 sqlite3_snprintf(NBFS, z, "%lld", pMem->i);
182 pMem->n = strlen(z);
183 pMem->z = z;
184 pMem->enc = SQLITE_UTF8;
185 pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
186 sqlite3VdbeChangeEncoding(pMem, enc);
187 return rc;
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 ){
197 if( p->xDel ){
198 p->xDel((void *)p->z);
199 }else{
200 sqliteFree(p->z);
202 p->z = 0;
203 p->xDel = 0;
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 ){
220 return pMem->i;
221 }else if( flags & MEM_Real ){
222 return (i64)pMem->r;
223 }else if( flags & (MEM_Str|MEM_Blob) ){
224 i64 value;
225 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
226 || sqlite3VdbeMemNulTerminate(pMem) ){
227 return SQLITE_NOMEM;
229 assert( pMem->z );
230 sqlite3atoi64(pMem->z, &value);
231 return value;
232 }else{
233 return 0;
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;
244 return SQLITE_OK;
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 ){
255 return pMem->r;
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) ){
261 return SQLITE_NOMEM;
263 assert( pMem->z );
264 return sqlite3AtoF(pMem->z, 0);
265 }else{
266 return 0.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;
278 return SQLITE_OK;
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);
296 pMem->i = val;
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);
307 pMem->r = val;
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));
320 pTo->xDel = 0;
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){
333 int rc;
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);
340 }else{
341 rc = SQLITE_OK;
343 return rc;
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){
355 int rc;
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;
364 pFrom->xDel = 0;
365 if( pTo->flags & MEM_Ephem ){
366 rc = sqlite3VdbeMemMakeWriteable(pTo);
367 }else{
368 rc = SQLITE_OK;
370 return rc;
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);
384 if( !z ){
385 pMem->flags = MEM_Null;
386 pMem->type = SQLITE_NULL;
387 return SQLITE_OK;
390 pMem->z = (char *)z;
391 if( xDel==SQLITE_STATIC ){
392 pMem->flags = MEM_Static;
393 }else if( xDel==SQLITE_TRANSIENT ){
394 pMem->flags = MEM_Ephem;
395 }else{
396 pMem->flags = MEM_Dyn;
397 pMem->xDel = xDel;
400 pMem->enc = enc;
401 pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
402 pMem->n = n;
404 assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
405 || enc==SQLITE_UTF16BE );
406 switch( enc ){
407 case 0:
408 pMem->flags |= MEM_Blob;
409 break;
411 case SQLITE_UTF8:
412 pMem->flags |= MEM_Str;
413 if( n<0 ){
414 pMem->n = strlen(z);
415 pMem->flags |= MEM_Term;
417 break;
419 #ifndef SQLITE_OMIT_UTF16
420 case SQLITE_UTF16LE:
421 case SQLITE_UTF16BE:
422 pMem->flags |= MEM_Str;
423 if( pMem->n<0 ){
424 pMem->n = sqlite3utf16ByteLen(pMem->z,-1);
425 pMem->flags |= MEM_Term;
427 if( sqlite3VdbeMemHandleBom(pMem) ){
428 return SQLITE_NOMEM;
430 #endif /* SQLITE_OMIT_UTF16 */
432 if( pMem->flags&MEM_Ephem ){
433 return sqlite3VdbeMemMakeWriteable(pMem);
435 return SQLITE_OK;
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){
448 int rc;
449 int f1, f2;
450 int combined_flags;
452 /* Interchange pMem1 and pMem2 if the collating sequence specifies
453 ** DESC order.
455 f1 = pMem1->flags;
456 f2 = pMem2->flags;
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)) ){
472 return 1;
474 if( !(f2&(MEM_Int|MEM_Real)) ){
475 return -1;
477 if( (f1 & f2 & MEM_Int)==0 ){
478 double r1, r2;
479 if( (f1&MEM_Real)==0 ){
480 r1 = pMem1->i;
481 }else{
482 r1 = pMem1->r;
484 if( (f2&MEM_Real)==0 ){
485 r2 = pMem2->i;
486 }else{
487 r2 = pMem2->r;
489 if( r1<r2 ) return -1;
490 if( r1>r2 ) return 1;
491 return 0;
492 }else{
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;
497 return 0;
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 ){
506 return 1;
508 if( (f2 & MEM_Str)==0 ){
509 return -1;
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 );
522 if( pColl ){
523 if( pMem1->enc==pColl->enc ){
524 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
525 }else{
526 u8 origEnc = pMem1->enc;
527 rc = pColl->xCmp(
528 pColl->pUser,
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);
538 return rc;
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);
547 if( rc==0 ){
548 rc = pMem1->n - pMem2->n;
550 return rc;
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 */
576 if( key ){
577 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
578 }else{
579 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
582 pMem->n = amt;
583 if( offset+amt<=available ){
584 pMem->z = &zData[offset];
585 pMem->flags = MEM_Blob|MEM_Ephem;
586 }else{
587 int rc;
588 if( amt>NBFS-2 ){
589 zData = (char *)sqliteMallocRaw(amt+2);
590 if( !zData ){
591 return SQLITE_NOMEM;
593 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
594 pMem->xDel = 0;
595 }else{
596 zData = &(pMem->zShort[0]);
597 pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
599 pMem->z = zData;
600 pMem->enc = 0;
601 pMem->type = SQLITE_BLOB;
603 if( key ){
604 rc = sqlite3BtreeKey(pCur, offset, amt, zData);
605 }else{
606 rc = sqlite3BtreeData(pCur, offset, amt, zData);
608 zData[amt] = 0;
609 zData[amt+1] = 0;
610 if( rc!=SQLITE_OK ){
611 if( amt>NBFS-2 ){
612 assert( zData!=pMem->zShort );
613 assert( pMem->flags & MEM_Dyn );
614 sqliteFree(zData);
615 } else {
616 assert( zData==pMem->zShort );
617 assert( pMem->flags & MEM_Short );
619 return rc;
623 return SQLITE_OK;
626 #ifndef NDEBUG
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
654 ** be less.
656 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
657 assert( strlen(pMem->z)<=pMem->n );
658 assert( pMem->z[pMem->n]==0 );
661 }else{
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 );
673 #endif
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
679 ** SQLITE_UTF8.
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 ){
686 return 0;
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));
701 if( p ){
702 p->flags = MEM_Null;
703 p->type = SQLITE_NULL;
705 return p;
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(
719 Expr *pExpr,
720 u8 enc,
721 u8 affinity,
722 sqlite3_value **ppVal
724 int op;
725 char *zVal = 0;
726 sqlite3_value *pVal = 0;
728 if( !pExpr ){
729 *ppVal = 0;
730 return SQLITE_OK;
732 op = pExpr->op;
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);
742 }else{
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 ){
753 int nVal;
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);
760 sqliteFree(zVal);
762 #endif
764 *ppVal = pVal;
765 return SQLITE_OK;
767 no_mem:
768 sqliteFree(zVal);
769 sqlite3ValueFree(pVal);
770 *ppVal = 0;
771 return SQLITE_NOMEM;
775 ** Change the string value of an sqlite3_value object
777 void sqlite3ValueSetStr(
778 sqlite3_value *v,
779 int n,
780 const void *z,
781 u8 enc,
782 void (*xDel)(void*)
784 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
788 ** Free an sqlite3_value object
790 void sqlite3ValueFree(sqlite3_value *v){
791 if( !v ) return;
792 sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
793 sqliteFree(v);
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){
801 Mem *p = (Mem*)pVal;
802 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
803 return p->n;
805 return 0;