monotone

monotone Mtn Source Tree

Root/sqlite/vdbemem.c

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 "os.h"
20#include <math.h>
21#include <ctype.h>
22#include "vdbeInt.h"
23
24/*
25** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
26** P if required.
27*/
28#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
29
30/*
31** If pMem is an object with a valid string representation, this routine
32** ensures the internal encoding for the string representation is
33** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
34**
35** If pMem is not a string object, or the encoding of the string
36** representation is already stored using the requested encoding, then this
37** routine is a no-op.
38**
39** SQLITE_OK is returned if the conversion is successful (or not required).
40** SQLITE_NOMEM may be returned if a malloc() fails during conversion
41** between formats.
42*/
43int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
44 int rc;
45 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
46 return SQLITE_OK;
47 }
48#ifdef SQLITE_OMIT_UTF16
49 return SQLITE_ERROR;
50#else
51
52
53 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
54 ** then the encoding of the value may not have changed.
55 */
56 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
57 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
58 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
59 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
60 return rc;
61#endif
62}
63
64/*
65** Make the given Mem object MEM_Dyn.
66**
67** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
68*/
69int sqlite3VdbeMemDynamicify(Mem *pMem){
70 int n;
71 u8 *z;
72 expandBlob(pMem);
73 if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
74 return SQLITE_OK;
75 }
76 assert( (pMem->flags & MEM_Dyn)==0 );
77 n = pMem->n;
78 assert( pMem->flags & (MEM_Str|MEM_Blob) );
79 z = sqliteMallocRaw( n+2 );
80 if( z==0 ){
81 return SQLITE_NOMEM;
82 }
83 pMem->flags |= MEM_Dyn|MEM_Term;
84 pMem->xDel = 0;
85 memcpy(z, pMem->z, n );
86 z[n] = 0;
87 z[n+1] = 0;
88 pMem->z = (char*)z;
89 pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
90 return SQLITE_OK;
91}
92
93/*
94** If the given Mem* has a zero-filled tail, turn it into an ordinary
95** blob stored in dynamically allocated space.
96*/
97#ifndef SQLITE_OMIT_INCRBLOB
98int sqlite3VdbeMemExpandBlob(Mem *pMem){
99 if( pMem->flags & MEM_Zero ){
100 char *pNew;
101 int nByte;
102 assert( (pMem->flags & MEM_Blob)!=0 );
103 nByte = pMem->n + pMem->u.i;
104 if( nByte<=0 ) nByte = 1;
105 pNew = sqliteMalloc(nByte);
106 if( pNew==0 ){
107 return SQLITE_NOMEM;
108 }
109 memcpy(pNew, pMem->z, pMem->n);
110 memset(&pNew[pMem->n], 0, pMem->u.i);
111 sqlite3VdbeMemRelease(pMem);
112 pMem->z = pNew;
113 pMem->n += pMem->u.i;
114 pMem->u.i = 0;
115 pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);
116 pMem->flags |= MEM_Dyn;
117 }
118 return SQLITE_OK;
119}
120#endif
121
122
123/*
124** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
125** of the Mem.z[] array can be modified.
126**
127** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
128*/
129int sqlite3VdbeMemMakeWriteable(Mem *pMem){
130 int n;
131 u8 *z;
132 expandBlob(pMem);
133 if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
134 return SQLITE_OK;
135 }
136 assert( (pMem->flags & MEM_Dyn)==0 );
137 assert( pMem->flags & (MEM_Str|MEM_Blob) );
138 if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
139 z = (u8*)pMem->zShort;
140 pMem->flags |= MEM_Short|MEM_Term;
141 }else{
142 z = sqliteMallocRaw( n+2 );
143 if( z==0 ){
144 return SQLITE_NOMEM;
145 }
146 pMem->flags |= MEM_Dyn|MEM_Term;
147 pMem->xDel = 0;
148 }
149 memcpy(z, pMem->z, n );
150 z[n] = 0;
151 z[n+1] = 0;
152 pMem->z = (char*)z;
153 pMem->flags &= ~(MEM_Ephem|MEM_Static);
154 assert(0==(1&(int)pMem->z));
155 return SQLITE_OK;
156}
157
158/*
159** Make sure the given Mem is \u0000 terminated.
160*/
161int sqlite3VdbeMemNulTerminate(Mem *pMem){
162 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
163 return SQLITE_OK; /* Nothing to do */
164 }
165 if( pMem->flags & (MEM_Static|MEM_Ephem) ){
166 return sqlite3VdbeMemMakeWriteable(pMem);
167 }else{
168 char *z;
169 sqlite3VdbeMemExpandBlob(pMem);
170 z = sqliteMalloc(pMem->n+2);
171
172 if( !z ) return SQLITE_NOMEM;
173 memcpy(z, pMem->z, pMem->n);
174 z[pMem->n] = 0;
175 z[pMem->n+1] = 0;
176 if( pMem->xDel ){
177 pMem->xDel(pMem->z);
178 }else{
179 sqliteFree(pMem->z);
180 }
181 pMem->xDel = 0;
182 pMem->z = z;
183 pMem->flags |= MEM_Term;
184 }
185 return SQLITE_OK;
186}
187
188/*
189** Add MEM_Str to the set of representations for the given Mem. Numbers
190** are converted using sqlite3_snprintf(). Converting a BLOB to a string
191** is a no-op.
192**
193** Existing representations MEM_Int and MEM_Real are *not* invalidated.
194**
195** A MEM_Null value will never be passed to this function. This function is
196** used for converting values to text for returning to the user (i.e. via
197** sqlite3_value_text()), or for ensuring that values to be used as btree
198** keys are strings. In the former case a NULL pointer is returned the
199** user and the later is an internal programming error.
200*/
201int sqlite3VdbeMemStringify(Mem *pMem, int enc){
202 int rc = SQLITE_OK;
203 int fg = pMem->flags;
204 char *z = pMem->zShort;
205
206 assert( !(fg&MEM_Zero) );
207 assert( !(fg&(MEM_Str|MEM_Blob)) );
208 assert( fg&(MEM_Int|MEM_Real) );
209
210 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
211 ** string representation of the value. Then, if the required encoding
212 ** is UTF-16le or UTF-16be do a translation.
213 **
214 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
215 */
216 if( fg & MEM_Int ){
217 sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i);
218 }else{
219 assert( fg & MEM_Real );
220 sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
221 }
222 pMem->n = strlen(z);
223 pMem->z = z;
224 pMem->enc = SQLITE_UTF8;
225 pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
226 sqlite3VdbeChangeEncoding(pMem, enc);
227 return rc;
228}
229
230/*
231** Memory cell pMem contains the context of an aggregate function.
232** This routine calls the finalize method for that function. The
233** result of the aggregate is stored back into pMem.
234**
235** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
236** otherwise.
237*/
238int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
239 int rc = SQLITE_OK;
240 if( pFunc && pFunc->xFinalize ){
241 sqlite3_context ctx;
242 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
243 ctx.s.flags = MEM_Null;
244 ctx.s.z = pMem->zShort;
245 ctx.pMem = pMem;
246 ctx.pFunc = pFunc;
247 ctx.isError = 0;
248 pFunc->xFinalize(&ctx);
249 if( pMem->z && pMem->z!=pMem->zShort ){
250 sqliteFree( pMem->z );
251 }
252 *pMem = ctx.s;
253 if( pMem->flags & MEM_Short ){
254 pMem->z = pMem->zShort;
255 }
256 if( ctx.isError ){
257 rc = SQLITE_ERROR;
258 }
259 }
260 return rc;
261}
262
263/*
264** Release any memory held by the Mem. This may leave the Mem in an
265** inconsistent state, for example with (Mem.z==0) and
266** (Mem.type==SQLITE_TEXT).
267*/
268void sqlite3VdbeMemRelease(Mem *p){
269 if( p->flags & (MEM_Dyn|MEM_Agg) ){
270 if( p->xDel ){
271 if( p->flags & MEM_Agg ){
272 sqlite3VdbeMemFinalize(p, p->u.pDef);
273 assert( (p->flags & MEM_Agg)==0 );
274 sqlite3VdbeMemRelease(p);
275 }else{
276 p->xDel((void *)p->z);
277 }
278 }else{
279 sqliteFree(p->z);
280 }
281 p->z = 0;
282 p->xDel = 0;
283 }
284}
285
286/*
287** Return some kind of integer value which is the best we can do
288** at representing the value that *pMem describes as an integer.
289** If pMem is an integer, then the value is exact. If pMem is
290** a floating-point then the value returned is the integer part.
291** If pMem is a string or blob, then we make an attempt to convert
292** it into a integer and return that. If pMem is NULL, return 0.
293**
294** If pMem is a string, its encoding might be changed.
295*/
296i64 sqlite3VdbeIntValue(Mem *pMem){
297 int flags = pMem->flags;
298 if( flags & MEM_Int ){
299 return pMem->u.i;
300 }else if( flags & MEM_Real ){
301 return (i64)pMem->r;
302 }else if( flags & (MEM_Str|MEM_Blob) ){
303 i64 value;
304 pMem->flags |= MEM_Str;
305 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
306 || sqlite3VdbeMemNulTerminate(pMem) ){
307 return 0;
308 }
309 assert( pMem->z );
310 sqlite3Atoi64(pMem->z, &value);
311 return value;
312 }else{
313 return 0;
314 }
315}
316
317/*
318** Return the best representation of pMem that we can get into a
319** double. If pMem is already a double or an integer, return its
320** value. If it is a string or blob, try to convert it to a double.
321** If it is a NULL, return 0.0.
322*/
323double sqlite3VdbeRealValue(Mem *pMem){
324 if( pMem->flags & MEM_Real ){
325 return pMem->r;
326 }else if( pMem->flags & MEM_Int ){
327 return (double)pMem->u.i;
328 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
329 double val = 0.0;
330 pMem->flags |= MEM_Str;
331 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
332 || sqlite3VdbeMemNulTerminate(pMem) ){
333 return 0.0;
334 }
335 assert( pMem->z );
336 sqlite3AtoF(pMem->z, &val);
337 return val;
338 }else{
339 return 0.0;
340 }
341}
342
343/*
344** The MEM structure is already a MEM_Real. Try to also make it a
345** MEM_Int if we can.
346*/
347void sqlite3VdbeIntegerAffinity(Mem *pMem){
348 assert( pMem->flags & MEM_Real );
349 pMem->u.i = pMem->r;
350 if( ((double)pMem->u.i)==pMem->r ){
351 pMem->flags |= MEM_Int;
352 }
353}
354
355/*
356** Convert pMem to type integer. Invalidate any prior representations.
357*/
358int sqlite3VdbeMemIntegerify(Mem *pMem){
359 pMem->u.i = sqlite3VdbeIntValue(pMem);
360 sqlite3VdbeMemRelease(pMem);
361 pMem->flags = MEM_Int;
362 return SQLITE_OK;
363}
364
365/*
366** Convert pMem so that it is of type MEM_Real.
367** Invalidate any prior representations.
368*/
369int sqlite3VdbeMemRealify(Mem *pMem){
370 pMem->r = sqlite3VdbeRealValue(pMem);
371 sqlite3VdbeMemRelease(pMem);
372 pMem->flags = MEM_Real;
373 return SQLITE_OK;
374}
375
376/*
377** Convert pMem so that it has types MEM_Real or MEM_Int or both.
378** Invalidate any prior representations.
379*/
380int sqlite3VdbeMemNumerify(Mem *pMem){
381 double r1, r2;
382 i64 i;
383 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
384 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
385 r1 = sqlite3VdbeRealValue(pMem);
386 i = (i64)r1;
387 r2 = (double)i;
388 if( r1==r2 ){
389 sqlite3VdbeMemIntegerify(pMem);
390 }else{
391 pMem->r = r1;
392 pMem->flags = MEM_Real;
393 sqlite3VdbeMemRelease(pMem);
394 }
395 return SQLITE_OK;
396}
397
398/*
399** Delete any previous value and set the value stored in *pMem to NULL.
400*/
401void sqlite3VdbeMemSetNull(Mem *pMem){
402 sqlite3VdbeMemRelease(pMem);
403 pMem->flags = MEM_Null;
404 pMem->type = SQLITE_NULL;
405 pMem->n = 0;
406}
407
408/*
409** Delete any previous value and set the value to be a BLOB of length
410** n containing all zeros.
411*/
412void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
413 sqlite3VdbeMemRelease(pMem);
414 pMem->flags = MEM_Blob|MEM_Zero|MEM_Short;
415 pMem->type = SQLITE_BLOB;
416 pMem->n = 0;
417 if( n<0 ) n = 0;
418 pMem->u.i = n;
419 pMem->z = pMem->zShort;
420 pMem->enc = SQLITE_UTF8;
421}
422
423/*
424** Delete any previous value and set the value stored in *pMem to val,
425** manifest type INTEGER.
426*/
427void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
428 sqlite3VdbeMemRelease(pMem);
429 pMem->u.i = val;
430 pMem->flags = MEM_Int;
431 pMem->type = SQLITE_INTEGER;
432}
433
434/*
435** Delete any previous value and set the value stored in *pMem to val,
436** manifest type REAL.
437*/
438void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
439 if( isnan(val) ){
440 sqlite3VdbeMemSetNull(pMem);
441 }else{
442 sqlite3VdbeMemRelease(pMem);
443 pMem->r = val;
444 pMem->flags = MEM_Real;
445 pMem->type = SQLITE_FLOAT;
446 }
447}
448
449/*
450** Return true if the Mem object contains a TEXT or BLOB that is
451** too large - whose size exceeds SQLITE_MAX_LENGTH.
452*/
453int sqlite3VdbeMemTooBig(Mem *p){
454 if( p->flags & (MEM_Str|MEM_Blob) ){
455 int n = p->n;
456 if( p->flags & MEM_Zero ){
457 n += p->u.i;
458 }
459 return n>SQLITE_MAX_LENGTH;
460 }
461 return 0;
462}
463
464/*
465** Make an shallow copy of pFrom into pTo. Prior contents of
466** pTo are overwritten. The pFrom->z field is not duplicated. If
467** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
468** and flags gets srcType (either MEM_Ephem or MEM_Static).
469*/
470void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
471 memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
472 pTo->xDel = 0;
473 if( pTo->flags & (MEM_Str|MEM_Blob) ){
474 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
475 assert( srcType==MEM_Ephem || srcType==MEM_Static );
476 pTo->flags |= srcType;
477 }
478}
479
480/*
481** Make a full copy of pFrom into pTo. Prior contents of pTo are
482** freed before the copy is made.
483*/
484int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
485 int rc;
486 if( pTo->flags & MEM_Dyn ){
487 sqlite3VdbeMemRelease(pTo);
488 }
489 sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
490 if( pTo->flags & MEM_Ephem ){
491 rc = sqlite3VdbeMemMakeWriteable(pTo);
492 }else{
493 rc = SQLITE_OK;
494 }
495 return rc;
496}
497
498/*
499** Transfer the contents of pFrom to pTo. Any existing value in pTo is
500** freed. If pFrom contains ephemeral data, a copy is made.
501**
502** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM
503** might be returned if pFrom held ephemeral data and we were unable
504** to allocate enough space to make a copy.
505*/
506int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
507 int rc;
508 if( pTo->flags & MEM_Dyn ){
509 sqlite3VdbeMemRelease(pTo);
510 }
511 memcpy(pTo, pFrom, sizeof(Mem));
512 if( pFrom->flags & MEM_Short ){
513 pTo->z = pTo->zShort;
514 }
515 pFrom->flags = MEM_Null;
516 pFrom->xDel = 0;
517 if( pTo->flags & MEM_Ephem ){
518 rc = sqlite3VdbeMemMakeWriteable(pTo);
519 }else{
520 rc = SQLITE_OK;
521 }
522 return rc;
523}
524
525/*
526** Change the value of a Mem to be a string or a BLOB.
527*/
528int sqlite3VdbeMemSetStr(
529 Mem *pMem, /* Memory cell to set to string value */
530 const char *z, /* String pointer */
531 int n, /* Bytes in string, or negative */
532 u8 enc, /* Encoding of z. 0 for BLOBs */
533 void (*xDel)(void*) /* Destructor function */
534){
535 sqlite3VdbeMemRelease(pMem);
536 if( !z ){
537 pMem->flags = MEM_Null;
538 pMem->type = SQLITE_NULL;
539 return SQLITE_OK;
540 }
541
542 pMem->z = (char *)z;
543 if( xDel==SQLITE_STATIC ){
544 pMem->flags = MEM_Static;
545 }else if( xDel==SQLITE_TRANSIENT ){
546 pMem->flags = MEM_Ephem;
547 }else{
548 pMem->flags = MEM_Dyn;
549 pMem->xDel = xDel;
550 }
551
552 pMem->enc = enc;
553 pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
554 pMem->n = n;
555
556 assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
557 || enc==SQLITE_UTF16BE );
558 switch( enc ){
559 case 0:
560 pMem->flags |= MEM_Blob;
561 pMem->enc = SQLITE_UTF8;
562 break;
563
564 case SQLITE_UTF8:
565 pMem->flags |= MEM_Str;
566 if( n<0 ){
567 pMem->n = strlen(z);
568 pMem->flags |= MEM_Term;
569 }
570 break;
571
572#ifndef SQLITE_OMIT_UTF16
573 case SQLITE_UTF16LE:
574 case SQLITE_UTF16BE:
575 pMem->flags |= MEM_Str;
576 if( pMem->n<0 ){
577 pMem->n = sqlite3Utf16ByteLen(pMem->z,-1);
578 pMem->flags |= MEM_Term;
579 }
580 if( sqlite3VdbeMemHandleBom(pMem) ){
581 return SQLITE_NOMEM;
582 }
583#endif /* SQLITE_OMIT_UTF16 */
584 }
585 if( pMem->flags&MEM_Ephem ){
586 return sqlite3VdbeMemMakeWriteable(pMem);
587 }
588 return SQLITE_OK;
589}
590
591/*
592** Compare the values contained by the two memory cells, returning
593** negative, zero or positive if pMem1 is less than, equal to, or greater
594** than pMem2. Sorting order is NULL's first, followed by numbers (integers
595** and reals) sorted numerically, followed by text ordered by the collating
596** sequence pColl and finally blob's ordered by memcmp().
597**
598** Two NULL values are considered equal by this function.
599*/
600int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
601 int rc;
602 int f1, f2;
603 int combined_flags;
604
605 /* Interchange pMem1 and pMem2 if the collating sequence specifies
606 ** DESC order.
607 */
608 f1 = pMem1->flags;
609 f2 = pMem2->flags;
610 combined_flags = f1|f2;
611
612 /* If one value is NULL, it is less than the other. If both values
613 ** are NULL, return 0.
614 */
615 if( combined_flags&MEM_Null ){
616 return (f2&MEM_Null) - (f1&MEM_Null);
617 }
618
619 /* If one value is a number and the other is not, the number is less.
620 ** If both are numbers, compare as reals if one is a real, or as integers
621 ** if both values are integers.
622 */
623 if( combined_flags&(MEM_Int|MEM_Real) ){
624 if( !(f1&(MEM_Int|MEM_Real)) ){
625 return 1;
626 }
627 if( !(f2&(MEM_Int|MEM_Real)) ){
628 return -1;
629 }
630 if( (f1 & f2 & MEM_Int)==0 ){
631 double r1, r2;
632 if( (f1&MEM_Real)==0 ){
633 r1 = pMem1->u.i;
634 }else{
635 r1 = pMem1->r;
636 }
637 if( (f2&MEM_Real)==0 ){
638 r2 = pMem2->u.i;
639 }else{
640 r2 = pMem2->r;
641 }
642 if( r1<r2 ) return -1;
643 if( r1>r2 ) return 1;
644 return 0;
645 }else{
646 assert( f1&MEM_Int );
647 assert( f2&MEM_Int );
648 if( pMem1->u.i < pMem2->u.i ) return -1;
649 if( pMem1->u.i > pMem2->u.i ) return 1;
650 return 0;
651 }
652 }
653
654 /* If one value is a string and the other is a blob, the string is less.
655 ** If both are strings, compare using the collating functions.
656 */
657 if( combined_flags&MEM_Str ){
658 if( (f1 & MEM_Str)==0 ){
659 return 1;
660 }
661 if( (f2 & MEM_Str)==0 ){
662 return -1;
663 }
664
665 assert( pMem1->enc==pMem2->enc );
666 assert( pMem1->enc==SQLITE_UTF8 ||
667 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
668
669 /* The collation sequence must be defined at this point, even if
670 ** the user deletes the collation sequence after the vdbe program is
671 ** compiled (this was not always the case).
672 */
673 assert( !pColl || pColl->xCmp );
674
675 if( pColl ){
676 if( pMem1->enc==pColl->enc ){
677 /* The strings are already in the correct encoding. Call the
678 ** comparison function directly */
679 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
680 }else{
681 u8 origEnc = pMem1->enc;
682 const void *v1, *v2;
683 int n1, n2;
684 /* Convert the strings into the encoding that the comparison
685 ** function expects */
686 v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
687 n1 = v1==0 ? 0 : pMem1->n;
688 assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
689 v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
690 n2 = v2==0 ? 0 : pMem2->n;
691 assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
692 /* Do the comparison */
693 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
694 /* Convert the strings back into the database encoding */
695 sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
696 sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
697 return rc;
698 }
699 }
700 /* If a NULL pointer was passed as the collate function, fall through
701 ** to the blob case and use memcmp(). */
702 }
703
704 /* Both values must be blobs. Compare using memcmp(). */
705 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
706 if( rc==0 ){
707 rc = pMem1->n - pMem2->n;
708 }
709 return rc;
710}
711
712/*
713** Move data out of a btree key or data field and into a Mem structure.
714** The data or key is taken from the entry that pCur is currently pointing
715** to. offset and amt determine what portion of the data or key to retrieve.
716** key is true to get the key or false to get data. The result is written
717** into the pMem element.
718**
719** The pMem structure is assumed to be uninitialized. Any prior content
720** is overwritten without being freed.
721**
722** If this routine fails for any reason (malloc returns NULL or unable
723** to read from the disk) then the pMem is left in an inconsistent state.
724*/
725int sqlite3VdbeMemFromBtree(
726 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
727 int offset, /* Offset from the start of data to return bytes from. */
728 int amt, /* Number of bytes to return. */
729 int key, /* If true, retrieve from the btree key, not data. */
730 Mem *pMem /* OUT: Return data in this Mem structure. */
731){
732 char *zData; /* Data from the btree layer */
733 int available = 0; /* Number of bytes available on the local btree page */
734
735 if( key ){
736 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
737 }else{
738 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
739 }
740 assert( zData!=0 );
741
742 pMem->n = amt;
743 if( offset+amt<=available ){
744 pMem->z = &zData[offset];
745 pMem->flags = MEM_Blob|MEM_Ephem;
746 }else{
747 int rc;
748 if( amt>NBFS-2 ){
749 zData = (char *)sqliteMallocRaw(amt+2);
750 if( !zData ){
751 return SQLITE_NOMEM;
752 }
753 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
754 pMem->xDel = 0;
755 }else{
756 zData = &(pMem->zShort[0]);
757 pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
758 }
759 pMem->z = zData;
760 pMem->enc = 0;
761 pMem->type = SQLITE_BLOB;
762
763 if( key ){
764 rc = sqlite3BtreeKey(pCur, offset, amt, zData);
765 }else{
766 rc = sqlite3BtreeData(pCur, offset, amt, zData);
767 }
768 zData[amt] = 0;
769 zData[amt+1] = 0;
770 if( rc!=SQLITE_OK ){
771 if( amt>NBFS-2 ){
772 assert( zData!=pMem->zShort );
773 assert( pMem->flags & MEM_Dyn );
774 sqliteFree(zData);
775 } else {
776 assert( zData==pMem->zShort );
777 assert( pMem->flags & MEM_Short );
778 }
779 return rc;
780 }
781 }
782
783 return SQLITE_OK;
784}
785
786#ifndef NDEBUG
787/*
788** Perform various checks on the memory cell pMem. An assert() will
789** fail if pMem is internally inconsistent.
790*/
791void sqlite3VdbeMemSanity(Mem *pMem){
792 int flags = pMem->flags;
793 assert( flags!=0 ); /* Must define some type */
794 if( flags & (MEM_Str|MEM_Blob) ){
795 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
796 assert( x!=0 ); /* Strings must define a string subtype */
797 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
798 assert( pMem->z!=0 ); /* Strings must have a value */
799 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
800 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
801 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
802 /* No destructor unless there is MEM_Dyn */
803 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
804
805 if( (flags & MEM_Str) ){
806 assert( pMem->enc==SQLITE_UTF8 ||
807 pMem->enc==SQLITE_UTF16BE ||
808 pMem->enc==SQLITE_UTF16LE
809 );
810 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
811 ** must be the length of the string. (Later:) If the database file
812 ** has been corrupted, '\000' characters might have been inserted
813 ** into the middle of the string. In that case, the strlen() might
814 ** be less.
815 */
816 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
817 assert( strlen(pMem->z)<=pMem->n );
818 assert( pMem->z[pMem->n]==0 );
819 }
820 }
821 }else{
822 /* Cannot define a string subtype for non-string objects */
823 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
824 assert( pMem->xDel==0 );
825 }
826 /* MEM_Null excludes all other types */
827 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
828 || (pMem->flags&MEM_Null)==0 );
829 /* If the MEM is both real and integer, the values are equal */
830 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
831 || pMem->r==pMem->u.i );
832}
833#endif
834
835/* This function is only available internally, it is not part of the
836** external API. It works in a similar way to sqlite3_value_text(),
837** except the data returned is in the encoding specified by the second
838** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
839** SQLITE_UTF8.
840**
841** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
842** If that is the case, then the result must be aligned on an even byte
843** boundary.
844*/
845const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
846 if( !pVal ) return 0;
847 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
848
849 if( pVal->flags&MEM_Null ){
850 return 0;
851 }
852 assert( (MEM_Blob>>3) == MEM_Str );
853 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
854 expandBlob(pVal);
855 if( pVal->flags&MEM_Str ){
856 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
857 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
858 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
859 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
860 return 0;
861 }
862 }
863 sqlite3VdbeMemNulTerminate(pVal);
864 }else{
865 assert( (pVal->flags&MEM_Blob)==0 );
866 sqlite3VdbeMemStringify(pVal, enc);
867 assert( 0==(1&(int)pVal->z) );
868 }
869 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || sqlite3MallocFailed() );
870 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
871 return pVal->z;
872 }else{
873 return 0;
874 }
875}
876
877/*
878** Create a new sqlite3_value object.
879*/
880sqlite3_value *sqlite3ValueNew(void){
881 Mem *p = sqliteMalloc(sizeof(*p));
882 if( p ){
883 p->flags = MEM_Null;
884 p->type = SQLITE_NULL;
885 }
886 return p;
887}
888
889/*
890** Create a new sqlite3_value object, containing the value of pExpr.
891**
892** This only works for very simple expressions that consist of one constant
893** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
894** be converted directly into a value, then the value is allocated and
895** a pointer written to *ppVal. The caller is responsible for deallocating
896** the value by passing it to sqlite3ValueFree() later on. If the expression
897** cannot be converted to a value, then *ppVal is set to NULL.
898*/
899int sqlite3ValueFromExpr(
900 Expr *pExpr,
901 u8 enc,
902 u8 affinity,
903 sqlite3_value **ppVal
904){
905 int op;
906 char *zVal = 0;
907 sqlite3_value *pVal = 0;
908
909 if( !pExpr ){
910 *ppVal = 0;
911 return SQLITE_OK;
912 }
913 op = pExpr->op;
914
915 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
916 zVal = sqliteStrNDup((char*)pExpr->token.z, pExpr->token.n);
917 pVal = sqlite3ValueNew();
918 if( !zVal || !pVal ) goto no_mem;
919 sqlite3Dequote(zVal);
920 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
921 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
922 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
923 }else{
924 sqlite3ValueApplyAffinity(pVal, affinity, enc);
925 }
926 }else if( op==TK_UMINUS ) {
927 if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
928 pVal->u.i = -1 * pVal->u.i;
929 pVal->r = -1.0 * pVal->r;
930 }
931 }
932#ifndef SQLITE_OMIT_BLOB_LITERAL
933 else if( op==TK_BLOB ){
934 int nVal;
935 pVal = sqlite3ValueNew();
936 zVal = sqliteStrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
937 if( !zVal || !pVal ) goto no_mem;
938 sqlite3Dequote(zVal);
939 nVal = strlen(zVal)/2;
940 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
941 sqliteFree(zVal);
942 }
943#endif
944
945 *ppVal = pVal;
946 return SQLITE_OK;
947
948no_mem:
949 sqliteFree(zVal);
950 sqlite3ValueFree(pVal);
951 *ppVal = 0;
952 return SQLITE_NOMEM;
953}
954
955/*
956** Change the string value of an sqlite3_value object
957*/
958void sqlite3ValueSetStr(
959 sqlite3_value *v,
960 int n,
961 const void *z,
962 u8 enc,
963 void (*xDel)(void*)
964){
965 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
966}
967
968/*
969** Free an sqlite3_value object
970*/
971void sqlite3ValueFree(sqlite3_value *v){
972 if( !v ) return;
973 sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
974 sqliteFree(v);
975}
976
977/*
978** Return the number of bytes in the sqlite3_value object assuming
979** that it uses the encoding "enc"
980*/
981int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
982 Mem *p = (Mem*)pVal;
983 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
984 if( p->flags & MEM_Zero ){
985 return p->n+p->u.i;
986 }else{
987 return p->n;
988 }
989 }
990 return 0;
991}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status