blob: 8fc222e2de20501450ecea9d4ebfaac9814aae59 [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
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"
drh4f26d6c2004-05-26 23:25:30 +000019#include "vdbeInt.h"
20
21/*
danielk1977bfd6cce2004-06-18 04:24:54 +000022** If pMem is an object with a valid string representation, this routine
23** ensures the internal encoding for the string representation is
24** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
drh4f26d6c2004-05-26 23:25:30 +000025**
danielk1977bfd6cce2004-06-18 04:24:54 +000026** If pMem is not a string object, or the encoding of the string
27** representation is already stored using the requested encoding, then this
28** routine is a no-op.
drh4f26d6c2004-05-26 23:25:30 +000029**
30** SQLITE_OK is returned if the conversion is successful (or not required).
31** SQLITE_NOMEM may be returned if a malloc() fails during conversion
32** between formats.
33*/
drhb21c8cd2007-08-21 19:33:56 +000034int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
mistachkinef593f22013-03-07 06:42:53 +000035#ifndef SQLITE_OMIT_UTF16
danielk19772c336542005-01-13 02:14:23 +000036 int rc;
mistachkinef593f22013-03-07 06:42:53 +000037#endif
drh3d4501e2008-12-04 20:40:10 +000038 assert( (pMem->flags&MEM_RowSet)==0 );
drhb27b7f52008-12-10 18:03:45 +000039 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
40 || desiredEnc==SQLITE_UTF16BE );
drheb2e1762004-05-27 01:53:56 +000041 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
drh4f26d6c2004-05-26 23:25:30 +000042 return SQLITE_OK;
43 }
drhb21c8cd2007-08-21 19:33:56 +000044 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh6c626082004-11-14 21:56:29 +000045#ifdef SQLITE_OMIT_UTF16
46 return SQLITE_ERROR;
47#else
danielk197700fd9572005-12-07 06:27:43 +000048
49 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
50 ** then the encoding of the value may not have changed.
51 */
drhb27b7f52008-12-10 18:03:45 +000052 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
danielk197700fd9572005-12-07 06:27:43 +000053 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
54 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
55 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
danielk19772c336542005-01-13 02:14:23 +000056 return rc;
drh6c626082004-11-14 21:56:29 +000057#endif
drh4f26d6c2004-05-26 23:25:30 +000058}
59
drheb2e1762004-05-27 01:53:56 +000060/*
danielk1977a7a8e142008-02-13 18:25:27 +000061** Make sure pMem->z points to a writable allocation of at least
62** n bytes.
63**
dan2b9ee772012-03-31 09:59:44 +000064** If the third argument passed to this function is true, then memory
65** cell pMem must contain a string or blob. In this case the content is
66** preserved. Otherwise, if the third parameter to this function is false,
67** any current string or blob value may be discarded.
danielk1977a7a8e142008-02-13 18:25:27 +000068**
69** This function sets the MEM_Dyn flag and clears any xDel callback.
70** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
71** not set, Mem.n is zeroed.
72*/
73int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
danielk19775f096132008-03-28 15:44:09 +000074 assert( 1 >=
75 ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
76 (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
77 ((pMem->flags&MEM_Ephem) ? 1 : 0) +
78 ((pMem->flags&MEM_Static) ? 1 : 0)
danielk1977a7a8e142008-02-13 18:25:27 +000079 );
drh3d4501e2008-12-04 20:40:10 +000080 assert( (pMem->flags&MEM_RowSet)==0 );
danielk1977a7a8e142008-02-13 18:25:27 +000081
dan2b9ee772012-03-31 09:59:44 +000082 /* If the preserve flag is set to true, then the memory cell must already
83 ** contain a valid string or blob value. */
84 assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
85
drhaf005fb2008-07-09 16:51:51 +000086 if( n<32 ) n = 32;
drh633e6d52008-07-28 19:34:53 +000087 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
danielk19775f096132008-03-28 15:44:09 +000088 if( preserve && pMem->z==pMem->zMalloc ){
89 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
90 preserve = 0;
91 }else{
drh633e6d52008-07-28 19:34:53 +000092 sqlite3DbFree(pMem->db, pMem->zMalloc);
danielk19775f096132008-03-28 15:44:09 +000093 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
danielk1977a7a8e142008-02-13 18:25:27 +000094 }
danielk1977a7a8e142008-02-13 18:25:27 +000095 }
danielk19775f096132008-03-28 15:44:09 +000096
drh4c8555f2009-06-25 01:47:11 +000097 if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
danielk19775f096132008-03-28 15:44:09 +000098 memcpy(pMem->zMalloc, pMem->z, pMem->n);
99 }
drhb08c2a72008-04-16 00:28:13 +0000100 if( pMem->flags&MEM_Dyn && pMem->xDel ){
drhaa538a52012-01-19 16:57:16 +0000101 assert( pMem->xDel!=SQLITE_DYNAMIC );
danielk19775f096132008-03-28 15:44:09 +0000102 pMem->xDel((void *)(pMem->z));
103 }
104
105 pMem->z = pMem->zMalloc;
drh753cc102008-11-11 00:21:30 +0000106 if( pMem->z==0 ){
107 pMem->flags = MEM_Null;
108 }else{
109 pMem->flags &= ~(MEM_Ephem|MEM_Static);
110 }
danielk19775f096132008-03-28 15:44:09 +0000111 pMem->xDel = 0;
112 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
danielk1977a7a8e142008-02-13 18:25:27 +0000113}
114
115/*
drhdab898f2008-07-30 13:14:55 +0000116** Make the given Mem object MEM_Dyn. In other words, make it so
117** that any TEXT or BLOB content is stored in memory obtained from
118** malloc(). In this way, we know that the memory is safe to be
119** overwritten or altered.
drheb2e1762004-05-27 01:53:56 +0000120**
121** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
122*/
drhdab898f2008-07-30 13:14:55 +0000123int sqlite3VdbeMemMakeWriteable(Mem *pMem){
danielk1977a7a8e142008-02-13 18:25:27 +0000124 int f;
drhb21c8cd2007-08-21 19:33:56 +0000125 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh3d4501e2008-12-04 20:40:10 +0000126 assert( (pMem->flags&MEM_RowSet)==0 );
drh45d29302012-01-08 22:18:33 +0000127 ExpandBlob(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000128 f = pMem->flags;
danielk19775f096132008-03-28 15:44:09 +0000129 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
danielk1977a7a8e142008-02-13 18:25:27 +0000130 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
131 return SQLITE_NOMEM;
132 }
133 pMem->z[pMem->n] = 0;
134 pMem->z[pMem->n+1] = 0;
135 pMem->flags |= MEM_Term;
drhebc16712010-09-28 00:25:58 +0000136#ifdef SQLITE_DEBUG
137 pMem->pScopyFrom = 0;
138#endif
drheb2e1762004-05-27 01:53:56 +0000139 }
danielk1977a7a8e142008-02-13 18:25:27 +0000140
drhf4479502004-05-27 03:12:53 +0000141 return SQLITE_OK;
drheb2e1762004-05-27 01:53:56 +0000142}
143
144/*
drhfdf972a2007-05-02 13:30:27 +0000145** If the given Mem* has a zero-filled tail, turn it into an ordinary
drhb026e052007-05-02 01:34:31 +0000146** blob stored in dynamically allocated space.
147*/
danielk1977246ad312007-05-16 14:23:00 +0000148#ifndef SQLITE_OMIT_INCRBLOB
drhb21c8cd2007-08-21 19:33:56 +0000149int sqlite3VdbeMemExpandBlob(Mem *pMem){
drhb026e052007-05-02 01:34:31 +0000150 if( pMem->flags & MEM_Zero ){
drh98640a32007-06-07 19:08:32 +0000151 int nByte;
danielk1977a7a8e142008-02-13 18:25:27 +0000152 assert( pMem->flags&MEM_Blob );
drh3d4501e2008-12-04 20:40:10 +0000153 assert( (pMem->flags&MEM_RowSet)==0 );
drhb21c8cd2007-08-21 19:33:56 +0000154 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977a7a8e142008-02-13 18:25:27 +0000155
156 /* Set nByte to the number of bytes required to store the expanded blob. */
drh8df32842008-12-09 02:51:23 +0000157 nByte = pMem->n + pMem->u.nZero;
danielk1977a7a8e142008-02-13 18:25:27 +0000158 if( nByte<=0 ){
159 nByte = 1;
160 }
161 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
drhb026e052007-05-02 01:34:31 +0000162 return SQLITE_NOMEM;
163 }
danielk1977a7a8e142008-02-13 18:25:27 +0000164
drh8df32842008-12-09 02:51:23 +0000165 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
166 pMem->n += pMem->u.nZero;
danielk1977a7a8e142008-02-13 18:25:27 +0000167 pMem->flags &= ~(MEM_Zero|MEM_Term);
drhb026e052007-05-02 01:34:31 +0000168 }
169 return SQLITE_OK;
170}
danielk1977246ad312007-05-16 14:23:00 +0000171#endif
drhb026e052007-05-02 01:34:31 +0000172
173
174/*
drheb2e1762004-05-27 01:53:56 +0000175** Make sure the given Mem is \u0000 terminated.
176*/
drhb21c8cd2007-08-21 19:33:56 +0000177int sqlite3VdbeMemNulTerminate(Mem *pMem){
178 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk197713073932004-06-30 11:54:06 +0000179 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
drheb2e1762004-05-27 01:53:56 +0000180 return SQLITE_OK; /* Nothing to do */
181 }
danielk1977a7a8e142008-02-13 18:25:27 +0000182 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
183 return SQLITE_NOMEM;
danielk19773f6b0872004-06-17 05:36:44 +0000184 }
danielk1977a7a8e142008-02-13 18:25:27 +0000185 pMem->z[pMem->n] = 0;
186 pMem->z[pMem->n+1] = 0;
187 pMem->flags |= MEM_Term;
danielk19773f6b0872004-06-17 05:36:44 +0000188 return SQLITE_OK;
drheb2e1762004-05-27 01:53:56 +0000189}
190
191/*
danielk197713073932004-06-30 11:54:06 +0000192** Add MEM_Str to the set of representations for the given Mem. Numbers
193** are converted using sqlite3_snprintf(). Converting a BLOB to a string
194** is a no-op.
drheb2e1762004-05-27 01:53:56 +0000195**
196** Existing representations MEM_Int and MEM_Real are *not* invalidated.
danielk197713073932004-06-30 11:54:06 +0000197**
198** A MEM_Null value will never be passed to this function. This function is
199** used for converting values to text for returning to the user (i.e. via
200** sqlite3_value_text()), or for ensuring that values to be used as btree
201** keys are strings. In the former case a NULL pointer is returned the
202** user and the later is an internal programming error.
drheb2e1762004-05-27 01:53:56 +0000203*/
drhb21c8cd2007-08-21 19:33:56 +0000204int sqlite3VdbeMemStringify(Mem *pMem, int enc){
drheb2e1762004-05-27 01:53:56 +0000205 int rc = SQLITE_OK;
206 int fg = pMem->flags;
danielk1977a7a8e142008-02-13 18:25:27 +0000207 const int nByte = 32;
drheb2e1762004-05-27 01:53:56 +0000208
drhb21c8cd2007-08-21 19:33:56 +0000209 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977def0fec2007-05-10 15:37:52 +0000210 assert( !(fg&MEM_Zero) );
drheb2e1762004-05-27 01:53:56 +0000211 assert( !(fg&(MEM_Str|MEM_Blob)) );
danielk197713073932004-06-30 11:54:06 +0000212 assert( fg&(MEM_Int|MEM_Real) );
drh3d4501e2008-12-04 20:40:10 +0000213 assert( (pMem->flags&MEM_RowSet)==0 );
drhea598cb2009-04-05 12:22:08 +0000214 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
drh3d4501e2008-12-04 20:40:10 +0000215
drheb2e1762004-05-27 01:53:56 +0000216
danielk1977a7a8e142008-02-13 18:25:27 +0000217 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
218 return SQLITE_NOMEM;
219 }
220
221 /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
danielk197713073932004-06-30 11:54:06 +0000222 ** string representation of the value. Then, if the required encoding
223 ** is UTF-16le or UTF-16be do a translation.
224 **
225 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
226 */
drh8df447f2005-11-01 15:48:24 +0000227 if( fg & MEM_Int ){
danielk1977a7a8e142008-02-13 18:25:27 +0000228 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
drh8df447f2005-11-01 15:48:24 +0000229 }else{
230 assert( fg & MEM_Real );
danielk1977a7a8e142008-02-13 18:25:27 +0000231 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
drheb2e1762004-05-27 01:53:56 +0000232 }
drhea678832008-12-10 19:26:22 +0000233 pMem->n = sqlite3Strlen30(pMem->z);
danielk197713073932004-06-30 11:54:06 +0000234 pMem->enc = SQLITE_UTF8;
danielk1977a7a8e142008-02-13 18:25:27 +0000235 pMem->flags |= MEM_Str|MEM_Term;
drhb21c8cd2007-08-21 19:33:56 +0000236 sqlite3VdbeChangeEncoding(pMem, enc);
drheb2e1762004-05-27 01:53:56 +0000237 return rc;
238}
239
240/*
drhabfcea22005-09-06 20:36:48 +0000241** Memory cell pMem contains the context of an aggregate function.
242** This routine calls the finalize method for that function. The
243** result of the aggregate is stored back into pMem.
drh90669c12006-01-20 15:45:36 +0000244**
245** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
246** otherwise.
drhabfcea22005-09-06 20:36:48 +0000247*/
drh90669c12006-01-20 15:45:36 +0000248int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
249 int rc = SQLITE_OK;
drh4c8555f2009-06-25 01:47:11 +0000250 if( ALWAYS(pFunc && pFunc->xFinalize) ){
drha10a34b2005-09-07 22:09:48 +0000251 sqlite3_context ctx;
drh3c024d62007-03-30 11:23:45 +0000252 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
drhb21c8cd2007-08-21 19:33:56 +0000253 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh709b8cb2008-08-22 14:41:00 +0000254 memset(&ctx, 0, sizeof(ctx));
drha10a34b2005-09-07 22:09:48 +0000255 ctx.s.flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000256 ctx.s.db = pMem->db;
drha10a34b2005-09-07 22:09:48 +0000257 ctx.pMem = pMem;
258 ctx.pFunc = pFunc;
drhee9ff672010-09-03 18:50:48 +0000259 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
drhb08c2a72008-04-16 00:28:13 +0000260 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
drh633e6d52008-07-28 19:34:53 +0000261 sqlite3DbFree(pMem->db, pMem->zMalloc);
drh092d5ef2008-12-10 11:49:06 +0000262 memcpy(pMem, &ctx.s, sizeof(ctx.s));
drh4c8555f2009-06-25 01:47:11 +0000263 rc = ctx.isError;
drhabfcea22005-09-06 20:36:48 +0000264 }
drh90669c12006-01-20 15:45:36 +0000265 return rc;
drhabfcea22005-09-06 20:36:48 +0000266}
267
268/*
danielk19775f096132008-03-28 15:44:09 +0000269** If the memory cell contains a string value that must be freed by
270** invoking an external callback, free it now. Calling this function
271** does not free any Mem.zMalloc buffer.
272*/
273void sqlite3VdbeMemReleaseExternal(Mem *p){
274 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
drh2d36eb42011-08-29 02:49:41 +0000275 if( p->flags&MEM_Agg ){
276 sqlite3VdbeMemFinalize(p, p->u.pDef);
277 assert( (p->flags & MEM_Agg)==0 );
278 sqlite3VdbeMemRelease(p);
279 }else if( p->flags&MEM_Dyn && p->xDel ){
280 assert( (p->flags&MEM_RowSet)==0 );
drhaa538a52012-01-19 16:57:16 +0000281 assert( p->xDel!=SQLITE_DYNAMIC );
drh2d36eb42011-08-29 02:49:41 +0000282 p->xDel((void *)p->z);
283 p->xDel = 0;
284 }else if( p->flags&MEM_RowSet ){
285 sqlite3RowSetClear(p->u.pRowSet);
286 }else if( p->flags&MEM_Frame ){
287 sqlite3VdbeMemSetNull(p);
danielk19775f096132008-03-28 15:44:09 +0000288 }
289}
290
291/*
danielk1977d8123362004-06-12 09:25:12 +0000292** Release any memory held by the Mem. This may leave the Mem in an
293** inconsistent state, for example with (Mem.z==0) and
294** (Mem.type==SQLITE_TEXT).
drhf4479502004-05-27 03:12:53 +0000295*/
danielk1977d8123362004-06-12 09:25:12 +0000296void sqlite3VdbeMemRelease(Mem *p){
drhe4c88c02012-01-04 12:57:45 +0000297 VdbeMemRelease(p);
drh633e6d52008-07-28 19:34:53 +0000298 sqlite3DbFree(p->db, p->zMalloc);
danielk19775f096132008-03-28 15:44:09 +0000299 p->z = 0;
300 p->zMalloc = 0;
301 p->xDel = 0;
drhf4479502004-05-27 03:12:53 +0000302}
303
304/*
drhd8c303f2008-01-11 15:27:03 +0000305** Convert a 64-bit IEEE double into a 64-bit signed integer.
306** If the double is too large, return 0x8000000000000000.
307**
308** Most systems appear to do this simply by assigning
309** variables and without the extra range tests. But
310** there are reports that windows throws an expection
311** if the floating point value is out of range. (See ticket #2880.)
312** Because we do not completely understand the problem, we will
313** take the conservative approach and always do range tests
314** before attempting the conversion.
315*/
316static i64 doubleToInt64(double r){
drh52d14522010-01-13 15:15:40 +0000317#ifdef SQLITE_OMIT_FLOATING_POINT
318 /* When floating-point is omitted, double and int64 are the same thing */
319 return r;
320#else
drhd8c303f2008-01-11 15:27:03 +0000321 /*
322 ** Many compilers we encounter do not define constants for the
323 ** minimum and maximum 64-bit integers, or they define them
324 ** inconsistently. And many do not understand the "LL" notation.
325 ** So we define our own static constants here using nothing
326 ** larger than a 32-bit integer constant.
327 */
drh0f050352008-05-09 18:03:13 +0000328 static const i64 maxInt = LARGEST_INT64;
329 static const i64 minInt = SMALLEST_INT64;
drhd8c303f2008-01-11 15:27:03 +0000330
331 if( r<(double)minInt ){
332 return minInt;
333 }else if( r>(double)maxInt ){
drhf9e749c2009-03-29 15:12:09 +0000334 /* minInt is correct here - not maxInt. It turns out that assigning
335 ** a very large positive number to an integer results in a very large
336 ** negative integer. This makes no sense, but it is what x86 hardware
337 ** does so for compatibility we will do the same in software. */
drhd8c303f2008-01-11 15:27:03 +0000338 return minInt;
339 }else{
340 return (i64)r;
341 }
drh52d14522010-01-13 15:15:40 +0000342#endif
drhd8c303f2008-01-11 15:27:03 +0000343}
344
345/*
drh6a6124e2004-06-27 01:56:33 +0000346** Return some kind of integer value which is the best we can do
347** at representing the value that *pMem describes as an integer.
348** If pMem is an integer, then the value is exact. If pMem is
349** a floating-point then the value returned is the integer part.
350** If pMem is a string or blob, then we make an attempt to convert
drh347a7cb2009-03-23 21:37:04 +0000351** it into a integer and return that. If pMem represents an
352** an SQL-NULL value, return 0.
drh6a6124e2004-06-27 01:56:33 +0000353**
drh347a7cb2009-03-23 21:37:04 +0000354** If pMem represents a string value, its encoding might be changed.
drheb2e1762004-05-27 01:53:56 +0000355*/
drh6a6124e2004-06-27 01:56:33 +0000356i64 sqlite3VdbeIntValue(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000357 int flags;
358 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhea598cb2009-04-05 12:22:08 +0000359 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
drhb21c8cd2007-08-21 19:33:56 +0000360 flags = pMem->flags;
drh6fec0762004-05-30 01:38:43 +0000361 if( flags & MEM_Int ){
drh3c024d62007-03-30 11:23:45 +0000362 return pMem->u.i;
drh6fec0762004-05-30 01:38:43 +0000363 }else if( flags & MEM_Real ){
drhd8c303f2008-01-11 15:27:03 +0000364 return doubleToInt64(pMem->r);
drh6fec0762004-05-30 01:38:43 +0000365 }else if( flags & (MEM_Str|MEM_Blob) ){
drh158b9cb2011-03-05 20:59:46 +0000366 i64 value = 0;
drh9339da12010-09-30 00:50:49 +0000367 assert( pMem->z || pMem->n==0 );
368 testcase( pMem->z==0 );
369 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
drh6a6124e2004-06-27 01:56:33 +0000370 return value;
drheb2e1762004-05-27 01:53:56 +0000371 }else{
drh6a6124e2004-06-27 01:56:33 +0000372 return 0;
drheb2e1762004-05-27 01:53:56 +0000373 }
drh6a6124e2004-06-27 01:56:33 +0000374}
375
376/*
drh6a6124e2004-06-27 01:56:33 +0000377** Return the best representation of pMem that we can get into a
378** double. If pMem is already a double or an integer, return its
379** value. If it is a string or blob, try to convert it to a double.
380** If it is a NULL, return 0.0.
drheb2e1762004-05-27 01:53:56 +0000381*/
drh6a6124e2004-06-27 01:56:33 +0000382double sqlite3VdbeRealValue(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000383 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhea598cb2009-04-05 12:22:08 +0000384 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
danielk1977f93bbbe2004-05-27 10:30:52 +0000385 if( pMem->flags & MEM_Real ){
drh6a6124e2004-06-27 01:56:33 +0000386 return pMem->r;
387 }else if( pMem->flags & MEM_Int ){
drh3c024d62007-03-30 11:23:45 +0000388 return (double)pMem->u.i;
drheb2e1762004-05-27 01:53:56 +0000389 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
shanefbd60f82009-02-04 03:59:25 +0000390 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
391 double val = (double)0;
drhe062d7b2010-10-05 12:05:32 +0000392 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
drh487e2622005-06-25 18:42:14 +0000393 return val;
drheb2e1762004-05-27 01:53:56 +0000394 }else{
shanefbd60f82009-02-04 03:59:25 +0000395 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
396 return (double)0;
drheb2e1762004-05-27 01:53:56 +0000397 }
drh6a6124e2004-06-27 01:56:33 +0000398}
399
400/*
drh8df447f2005-11-01 15:48:24 +0000401** The MEM structure is already a MEM_Real. Try to also make it a
402** MEM_Int if we can.
403*/
404void sqlite3VdbeIntegerAffinity(Mem *pMem){
405 assert( pMem->flags & MEM_Real );
drh3d4501e2008-12-04 20:40:10 +0000406 assert( (pMem->flags & MEM_RowSet)==0 );
drhb21c8cd2007-08-21 19:33:56 +0000407 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhea598cb2009-04-05 12:22:08 +0000408 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
drhefe3d652008-01-11 00:06:10 +0000409
drhd8c303f2008-01-11 15:27:03 +0000410 pMem->u.i = doubleToInt64(pMem->r);
drh94c3a2b2009-06-17 16:20:04 +0000411
412 /* Only mark the value as an integer if
413 **
414 ** (1) the round-trip conversion real->int->real is a no-op, and
415 ** (2) The integer is neither the largest nor the smallest
416 ** possible integer (ticket #3922)
417 **
drhe74871a2009-08-14 17:53:39 +0000418 ** The second and third terms in the following conditional enforces
419 ** the second condition under the assumption that addition overflow causes
420 ** values to wrap around. On x86 hardware, the third term is always
421 ** true and could be omitted. But we leave it in because other
422 ** architectures might behave differently.
drh94c3a2b2009-06-17 16:20:04 +0000423 */
drhbb8c1b52012-02-10 01:25:13 +0000424 if( pMem->r==(double)pMem->u.i
425 && pMem->u.i>SMALLEST_INT64
426#if defined(__i486__) || defined(__x86_64__)
427 && ALWAYS(pMem->u.i<LARGEST_INT64)
428#else
429 && pMem->u.i<LARGEST_INT64
430#endif
431 ){
drh8df447f2005-11-01 15:48:24 +0000432 pMem->flags |= MEM_Int;
433 }
434}
435
drh8a512562005-11-14 22:29:05 +0000436/*
437** Convert pMem to type integer. Invalidate any prior representations.
438*/
439int sqlite3VdbeMemIntegerify(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000440 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh3d4501e2008-12-04 20:40:10 +0000441 assert( (pMem->flags & MEM_RowSet)==0 );
drhea598cb2009-04-05 12:22:08 +0000442 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
443
drh3c024d62007-03-30 11:23:45 +0000444 pMem->u.i = sqlite3VdbeIntValue(pMem);
drh3d4501e2008-12-04 20:40:10 +0000445 MemSetTypeFlag(pMem, MEM_Int);
drh8a512562005-11-14 22:29:05 +0000446 return SQLITE_OK;
447}
drh8df447f2005-11-01 15:48:24 +0000448
449/*
drh8a512562005-11-14 22:29:05 +0000450** Convert pMem so that it is of type MEM_Real.
451** Invalidate any prior representations.
drh6a6124e2004-06-27 01:56:33 +0000452*/
453int sqlite3VdbeMemRealify(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000454 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhea598cb2009-04-05 12:22:08 +0000455 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
456
drh6a6124e2004-06-27 01:56:33 +0000457 pMem->r = sqlite3VdbeRealValue(pMem);
drh3d4501e2008-12-04 20:40:10 +0000458 MemSetTypeFlag(pMem, MEM_Real);
drh8a512562005-11-14 22:29:05 +0000459 return SQLITE_OK;
460}
461
462/*
463** Convert pMem so that it has types MEM_Real or MEM_Int or both.
464** Invalidate any prior representations.
drh4b5db5a2010-01-21 01:53:07 +0000465**
466** Every effort is made to force the conversion, even if the input
467** is a string that does not look completely like a number. Convert
468** as much of the string as we can and ignore the rest.
drh8a512562005-11-14 22:29:05 +0000469*/
470int sqlite3VdbeMemNumerify(Mem *pMem){
drh93518622010-09-30 14:48:06 +0000471 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
472 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
473 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
shaneh5f1d6b62010-09-30 16:51:25 +0000474 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
drh93518622010-09-30 14:48:06 +0000475 MemSetTypeFlag(pMem, MEM_Int);
476 }else{
477 pMem->r = sqlite3VdbeRealValue(pMem);
478 MemSetTypeFlag(pMem, MEM_Real);
479 sqlite3VdbeIntegerAffinity(pMem);
480 }
drhcd7b46d2007-05-16 11:55:56 +0000481 }
drh93518622010-09-30 14:48:06 +0000482 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
483 pMem->flags &= ~(MEM_Str|MEM_Blob);
drhf4479502004-05-27 03:12:53 +0000484 return SQLITE_OK;
drh4f26d6c2004-05-26 23:25:30 +0000485}
486
487/*
488** Delete any previous value and set the value stored in *pMem to NULL.
489*/
490void sqlite3VdbeMemSetNull(Mem *pMem){
dan165921a2009-08-28 18:53:45 +0000491 if( pMem->flags & MEM_Frame ){
dan27106572010-12-01 08:04:47 +0000492 VdbeFrame *pFrame = pMem->u.pFrame;
493 pFrame->pParent = pFrame->v->pDelFrame;
494 pFrame->v->pDelFrame = pFrame;
dan165921a2009-08-28 18:53:45 +0000495 }
drh3d4501e2008-12-04 20:40:10 +0000496 if( pMem->flags & MEM_RowSet ){
497 sqlite3RowSetClear(pMem->u.pRowSet);
498 }
499 MemSetTypeFlag(pMem, MEM_Null);
drh9c054832004-05-31 18:51:57 +0000500 pMem->type = SQLITE_NULL;
drh4f26d6c2004-05-26 23:25:30 +0000501}
502
503/*
drhb026e052007-05-02 01:34:31 +0000504** Delete any previous value and set the value to be a BLOB of length
505** n containing all zeros.
506*/
507void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
508 sqlite3VdbeMemRelease(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000509 pMem->flags = MEM_Blob|MEM_Zero;
drhb026e052007-05-02 01:34:31 +0000510 pMem->type = SQLITE_BLOB;
511 pMem->n = 0;
drh98640a32007-06-07 19:08:32 +0000512 if( n<0 ) n = 0;
drh8df32842008-12-09 02:51:23 +0000513 pMem->u.nZero = n;
danielk1977def0fec2007-05-10 15:37:52 +0000514 pMem->enc = SQLITE_UTF8;
danielk1977f16c6242009-07-18 14:36:23 +0000515
516#ifdef SQLITE_OMIT_INCRBLOB
517 sqlite3VdbeMemGrow(pMem, n, 0);
518 if( pMem->z ){
519 pMem->n = n;
520 memset(pMem->z, 0, n);
521 }
522#endif
drhb026e052007-05-02 01:34:31 +0000523}
524
525/*
drh4f26d6c2004-05-26 23:25:30 +0000526** Delete any previous value and set the value stored in *pMem to val,
527** manifest type INTEGER.
528*/
drheb2e1762004-05-27 01:53:56 +0000529void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
danielk1977d8123362004-06-12 09:25:12 +0000530 sqlite3VdbeMemRelease(pMem);
drh3c024d62007-03-30 11:23:45 +0000531 pMem->u.i = val;
drh4f26d6c2004-05-26 23:25:30 +0000532 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000533 pMem->type = SQLITE_INTEGER;
drh4f26d6c2004-05-26 23:25:30 +0000534}
535
drh7ec5ea92010-01-13 00:04:13 +0000536#ifndef SQLITE_OMIT_FLOATING_POINT
drh4f26d6c2004-05-26 23:25:30 +0000537/*
538** Delete any previous value and set the value stored in *pMem to val,
539** manifest type REAL.
540*/
drheb2e1762004-05-27 01:53:56 +0000541void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
drh0de3ae92008-04-28 16:55:26 +0000542 if( sqlite3IsNaN(val) ){
drh53c14022007-05-10 17:23:11 +0000543 sqlite3VdbeMemSetNull(pMem);
544 }else{
545 sqlite3VdbeMemRelease(pMem);
546 pMem->r = val;
547 pMem->flags = MEM_Real;
548 pMem->type = SQLITE_FLOAT;
549 }
drh4f26d6c2004-05-26 23:25:30 +0000550}
drh7ec5ea92010-01-13 00:04:13 +0000551#endif
drh4f26d6c2004-05-26 23:25:30 +0000552
553/*
drh3d4501e2008-12-04 20:40:10 +0000554** Delete any previous value and set the value of pMem to be an
555** empty boolean index.
556*/
557void sqlite3VdbeMemSetRowSet(Mem *pMem){
558 sqlite3 *db = pMem->db;
559 assert( db!=0 );
drh4c8555f2009-06-25 01:47:11 +0000560 assert( (pMem->flags & MEM_RowSet)==0 );
561 sqlite3VdbeMemRelease(pMem);
562 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
drh8d993632008-12-04 22:17:55 +0000563 if( db->mallocFailed ){
564 pMem->flags = MEM_Null;
565 }else{
drh3d4501e2008-12-04 20:40:10 +0000566 assert( pMem->zMalloc );
567 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
568 sqlite3DbMallocSize(db, pMem->zMalloc));
569 assert( pMem->u.pRowSet!=0 );
drh8d993632008-12-04 22:17:55 +0000570 pMem->flags = MEM_RowSet;
drh3d4501e2008-12-04 20:40:10 +0000571 }
572}
573
574/*
drh023ae032007-05-08 12:12:16 +0000575** Return true if the Mem object contains a TEXT or BLOB that is
576** too large - whose size exceeds SQLITE_MAX_LENGTH.
577*/
578int sqlite3VdbeMemTooBig(Mem *p){
drhfa4a4b92008-03-19 21:45:51 +0000579 assert( p->db!=0 );
drh023ae032007-05-08 12:12:16 +0000580 if( p->flags & (MEM_Str|MEM_Blob) ){
581 int n = p->n;
582 if( p->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000583 n += p->u.nZero;
drh023ae032007-05-08 12:12:16 +0000584 }
drhbb4957f2008-03-20 14:03:29 +0000585 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
drh023ae032007-05-08 12:12:16 +0000586 }
587 return 0;
588}
589
drh2b4ded92010-09-27 21:09:31 +0000590#ifdef SQLITE_DEBUG
591/*
592** This routine prepares a memory cell for modication by breaking
593** its link to a shallow copy and by marking any current shallow
594** copies of this cell as invalid.
595**
596** This is used for testing and debugging only - to make sure shallow
597** copies are not misused.
598*/
drhe4c88c02012-01-04 12:57:45 +0000599void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
drh2b4ded92010-09-27 21:09:31 +0000600 int i;
601 Mem *pX;
602 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
603 if( pX->pScopyFrom==pMem ){
604 pX->flags |= MEM_Invalid;
605 pX->pScopyFrom = 0;
606 }
607 }
608 pMem->pScopyFrom = 0;
609}
610#endif /* SQLITE_DEBUG */
611
danielk1977e5f5b8f2008-03-28 18:11:16 +0000612/*
613** Size of struct Mem not including the Mem.zMalloc member.
614*/
mlcreechfe3f4e82008-03-29 23:25:27 +0000615#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
danielk19775f096132008-03-28 15:44:09 +0000616
drh023ae032007-05-08 12:12:16 +0000617/*
drhfebe1062004-08-28 18:17:48 +0000618** Make an shallow copy of pFrom into pTo. Prior contents of
drha05a7222008-01-19 03:35:58 +0000619** pTo are freed. The pFrom->z field is not duplicated. If
drhfebe1062004-08-28 18:17:48 +0000620** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
621** and flags gets srcType (either MEM_Ephem or MEM_Static).
drh4f26d6c2004-05-26 23:25:30 +0000622*/
drhfebe1062004-08-28 18:17:48 +0000623void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
drh3d4501e2008-12-04 20:40:10 +0000624 assert( (pFrom->flags & MEM_RowSet)==0 );
drhe4c88c02012-01-04 12:57:45 +0000625 VdbeMemRelease(pTo);
danielk19775f096132008-03-28 15:44:09 +0000626 memcpy(pTo, pFrom, MEMCELLSIZE);
danielk1977d8123362004-06-12 09:25:12 +0000627 pTo->xDel = 0;
dan5fea9072010-03-05 18:46:12 +0000628 if( (pFrom->flags&MEM_Static)==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +0000629 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
drhfebe1062004-08-28 18:17:48 +0000630 assert( srcType==MEM_Ephem || srcType==MEM_Static );
631 pTo->flags |= srcType;
632 }
633}
634
635/*
636** Make a full copy of pFrom into pTo. Prior contents of pTo are
637** freed before the copy is made.
638*/
drhb21c8cd2007-08-21 19:33:56 +0000639int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
danielk1977a7a8e142008-02-13 18:25:27 +0000640 int rc = SQLITE_OK;
danielk1977a7a8e142008-02-13 18:25:27 +0000641
drh3d4501e2008-12-04 20:40:10 +0000642 assert( (pFrom->flags & MEM_RowSet)==0 );
drhe4c88c02012-01-04 12:57:45 +0000643 VdbeMemRelease(pTo);
danielk19775f096132008-03-28 15:44:09 +0000644 memcpy(pTo, pFrom, MEMCELLSIZE);
645 pTo->flags &= ~MEM_Dyn;
646
647 if( pTo->flags&(MEM_Str|MEM_Blob) ){
648 if( 0==(pFrom->flags&MEM_Static) ){
649 pTo->flags |= MEM_Ephem;
650 rc = sqlite3VdbeMemMakeWriteable(pTo);
danielk19779172fd82008-02-14 15:31:52 +0000651 }
danielk1977a7a8e142008-02-13 18:25:27 +0000652 }
653
drh71c697e2004-08-08 23:39:19 +0000654 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000655}
656
drheb2e1762004-05-27 01:53:56 +0000657/*
danielk1977369f27e2004-06-15 11:40:04 +0000658** Transfer the contents of pFrom to pTo. Any existing value in pTo is
drhfebe1062004-08-28 18:17:48 +0000659** freed. If pFrom contains ephemeral data, a copy is made.
660**
drh643167f2008-01-22 21:30:53 +0000661** pFrom contains an SQL NULL when this routine returns.
danielk1977369f27e2004-06-15 11:40:04 +0000662*/
drh643167f2008-01-22 21:30:53 +0000663void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
drhb21c8cd2007-08-21 19:33:56 +0000664 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
665 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
666 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
danielk19775f096132008-03-28 15:44:09 +0000667
668 sqlite3VdbeMemRelease(pTo);
danielk197713073932004-06-30 11:54:06 +0000669 memcpy(pTo, pFrom, sizeof(Mem));
danielk197713073932004-06-30 11:54:06 +0000670 pFrom->flags = MEM_Null;
671 pFrom->xDel = 0;
danielk19775f096132008-03-28 15:44:09 +0000672 pFrom->zMalloc = 0;
danielk1977369f27e2004-06-15 11:40:04 +0000673}
674
675/*
drheb2e1762004-05-27 01:53:56 +0000676** Change the value of a Mem to be a string or a BLOB.
danielk1977a7a8e142008-02-13 18:25:27 +0000677**
678** The memory management strategy depends on the value of the xDel
679** parameter. If the value passed is SQLITE_TRANSIENT, then the
680** string is copied into a (possibly existing) buffer managed by the
681** Mem structure. Otherwise, any existing buffer is freed and the
682** pointer copied.
drh9a65f2c2009-06-22 19:05:40 +0000683**
684** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
685** size limit) then no memory allocation occurs. If the string can be
686** stored without allocating memory, then it is. If a memory allocation
687** is required to store the string, then value of pMem is unchanged. In
688** either case, SQLITE_TOOBIG is returned.
drheb2e1762004-05-27 01:53:56 +0000689*/
drh4f26d6c2004-05-26 23:25:30 +0000690int sqlite3VdbeMemSetStr(
691 Mem *pMem, /* Memory cell to set to string value */
692 const char *z, /* String pointer */
693 int n, /* Bytes in string, or negative */
drheb2e1762004-05-27 01:53:56 +0000694 u8 enc, /* Encoding of z. 0 for BLOBs */
danielk1977d8123362004-06-12 09:25:12 +0000695 void (*xDel)(void*) /* Destructor function */
drh4f26d6c2004-05-26 23:25:30 +0000696){
danielk1977a7a8e142008-02-13 18:25:27 +0000697 int nByte = n; /* New value for pMem->n */
drh0a687d12008-07-08 14:52:07 +0000698 int iLimit; /* Maximum allowed string or blob size */
drh8df32842008-12-09 02:51:23 +0000699 u16 flags = 0; /* New value for pMem->flags */
danielk1977a7a8e142008-02-13 18:25:27 +0000700
drhb21c8cd2007-08-21 19:33:56 +0000701 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh3d4501e2008-12-04 20:40:10 +0000702 assert( (pMem->flags & MEM_RowSet)==0 );
danielk1977a7a8e142008-02-13 18:25:27 +0000703
704 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
drh4f26d6c2004-05-26 23:25:30 +0000705 if( !z ){
danielk1977a7a8e142008-02-13 18:25:27 +0000706 sqlite3VdbeMemSetNull(pMem);
drh4f26d6c2004-05-26 23:25:30 +0000707 return SQLITE_OK;
708 }
danielk1977a7a8e142008-02-13 18:25:27 +0000709
drh0a687d12008-07-08 14:52:07 +0000710 if( pMem->db ){
711 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
712 }else{
713 iLimit = SQLITE_MAX_LENGTH;
714 }
danielk1977a7a8e142008-02-13 18:25:27 +0000715 flags = (enc==0?MEM_Blob:MEM_Str);
716 if( nByte<0 ){
717 assert( enc!=0 );
drh8fd38972008-02-19 15:44:09 +0000718 if( enc==SQLITE_UTF8 ){
drh0a687d12008-07-08 14:52:07 +0000719 for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
drh8fd38972008-02-19 15:44:09 +0000720 }else{
drh0a687d12008-07-08 14:52:07 +0000721 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
drh8fd38972008-02-19 15:44:09 +0000722 }
danielk1977a7a8e142008-02-13 18:25:27 +0000723 flags |= MEM_Term;
drh4f26d6c2004-05-26 23:25:30 +0000724 }
danielk1977d8123362004-06-12 09:25:12 +0000725
danielk1977a7a8e142008-02-13 18:25:27 +0000726 /* The following block sets the new values of Mem.z and Mem.xDel. It
727 ** also sets a flag in local variable "flags" to indicate the memory
728 ** management (one of MEM_Dyn or MEM_Static).
729 */
730 if( xDel==SQLITE_TRANSIENT ){
731 int nAlloc = nByte;
732 if( flags&MEM_Term ){
733 nAlloc += (enc==SQLITE_UTF8?1:2);
734 }
drh0793f1b2008-11-05 17:41:19 +0000735 if( nByte>iLimit ){
736 return SQLITE_TOOBIG;
737 }
danielk1977a7a8e142008-02-13 18:25:27 +0000738 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
739 return SQLITE_NOMEM;
740 }
741 memcpy(pMem->z, z, nAlloc);
drh633e6d52008-07-28 19:34:53 +0000742 }else if( xDel==SQLITE_DYNAMIC ){
743 sqlite3VdbeMemRelease(pMem);
744 pMem->zMalloc = pMem->z = (char *)z;
745 pMem->xDel = 0;
danielk1977a7a8e142008-02-13 18:25:27 +0000746 }else{
747 sqlite3VdbeMemRelease(pMem);
748 pMem->z = (char *)z;
drhc890fec2008-08-01 20:10:08 +0000749 pMem->xDel = xDel;
750 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
danielk1977a7a8e142008-02-13 18:25:27 +0000751 }
danielk1977d8123362004-06-12 09:25:12 +0000752
danielk1977a7a8e142008-02-13 18:25:27 +0000753 pMem->n = nByte;
754 pMem->flags = flags;
755 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
756 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
drh4f26d6c2004-05-26 23:25:30 +0000757
drh6c626082004-11-14 21:56:29 +0000758#ifndef SQLITE_OMIT_UTF16
danielk1977a7a8e142008-02-13 18:25:27 +0000759 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
760 return SQLITE_NOMEM;
drh4f26d6c2004-05-26 23:25:30 +0000761 }
danielk1977a7a8e142008-02-13 18:25:27 +0000762#endif
763
drh9a65f2c2009-06-22 19:05:40 +0000764 if( nByte>iLimit ){
765 return SQLITE_TOOBIG;
766 }
767
drhf4479502004-05-27 03:12:53 +0000768 return SQLITE_OK;
drh4f26d6c2004-05-26 23:25:30 +0000769}
770
771/*
772** Compare the values contained by the two memory cells, returning
773** negative, zero or positive if pMem1 is less than, equal to, or greater
774** than pMem2. Sorting order is NULL's first, followed by numbers (integers
775** and reals) sorted numerically, followed by text ordered by the collating
776** sequence pColl and finally blob's ordered by memcmp().
777**
778** Two NULL values are considered equal by this function.
779*/
780int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
781 int rc;
782 int f1, f2;
783 int combined_flags;
784
drh4f26d6c2004-05-26 23:25:30 +0000785 f1 = pMem1->flags;
786 f2 = pMem2->flags;
787 combined_flags = f1|f2;
drh3d4501e2008-12-04 20:40:10 +0000788 assert( (combined_flags & MEM_RowSet)==0 );
drh4f26d6c2004-05-26 23:25:30 +0000789
790 /* If one value is NULL, it is less than the other. If both values
791 ** are NULL, return 0.
792 */
793 if( combined_flags&MEM_Null ){
794 return (f2&MEM_Null) - (f1&MEM_Null);
795 }
796
797 /* If one value is a number and the other is not, the number is less.
798 ** If both are numbers, compare as reals if one is a real, or as integers
799 ** if both values are integers.
800 */
801 if( combined_flags&(MEM_Int|MEM_Real) ){
802 if( !(f1&(MEM_Int|MEM_Real)) ){
803 return 1;
804 }
805 if( !(f2&(MEM_Int|MEM_Real)) ){
806 return -1;
807 }
808 if( (f1 & f2 & MEM_Int)==0 ){
809 double r1, r2;
810 if( (f1&MEM_Real)==0 ){
drh8df32842008-12-09 02:51:23 +0000811 r1 = (double)pMem1->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000812 }else{
813 r1 = pMem1->r;
814 }
815 if( (f2&MEM_Real)==0 ){
drh8df32842008-12-09 02:51:23 +0000816 r2 = (double)pMem2->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000817 }else{
818 r2 = pMem2->r;
819 }
820 if( r1<r2 ) return -1;
821 if( r1>r2 ) return 1;
822 return 0;
823 }else{
824 assert( f1&MEM_Int );
825 assert( f2&MEM_Int );
drh3c024d62007-03-30 11:23:45 +0000826 if( pMem1->u.i < pMem2->u.i ) return -1;
827 if( pMem1->u.i > pMem2->u.i ) return 1;
drh4f26d6c2004-05-26 23:25:30 +0000828 return 0;
829 }
830 }
831
832 /* If one value is a string and the other is a blob, the string is less.
833 ** If both are strings, compare using the collating functions.
834 */
835 if( combined_flags&MEM_Str ){
836 if( (f1 & MEM_Str)==0 ){
837 return 1;
838 }
839 if( (f2 & MEM_Str)==0 ){
840 return -1;
841 }
danielk19770202b292004-06-09 09:55:16 +0000842
843 assert( pMem1->enc==pMem2->enc );
danielk1977dc8453f2004-06-12 00:42:34 +0000844 assert( pMem1->enc==SQLITE_UTF8 ||
845 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
danielk19770202b292004-06-09 09:55:16 +0000846
danielk1977b3bf5562006-01-10 17:58:23 +0000847 /* The collation sequence must be defined at this point, even if
848 ** the user deletes the collation sequence after the vdbe program is
849 ** compiled (this was not always the case).
danielk19770202b292004-06-09 09:55:16 +0000850 */
danielk1977466be562004-06-10 02:16:01 +0000851 assert( !pColl || pColl->xCmp );
danielk19770202b292004-06-09 09:55:16 +0000852
853 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000854 if( pMem1->enc==pColl->enc ){
drh7d9bd4e2006-02-16 18:16:36 +0000855 /* The strings are already in the correct encoding. Call the
856 ** comparison function directly */
danielk1977466be562004-06-10 02:16:01 +0000857 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
danielk19770202b292004-06-09 09:55:16 +0000858 }else{
drh7d9bd4e2006-02-16 18:16:36 +0000859 const void *v1, *v2;
860 int n1, n2;
danielk19777eae4f52008-09-16 12:06:08 +0000861 Mem c1;
862 Mem c2;
863 memset(&c1, 0, sizeof(c1));
864 memset(&c2, 0, sizeof(c2));
865 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
866 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
867 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
868 n1 = v1==0 ? 0 : c1.n;
869 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
870 n2 = v2==0 ? 0 : c2.n;
drh7d9bd4e2006-02-16 18:16:36 +0000871 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
danielk19777eae4f52008-09-16 12:06:08 +0000872 sqlite3VdbeMemRelease(&c1);
873 sqlite3VdbeMemRelease(&c2);
danielk1977f4618892004-06-28 13:09:11 +0000874 return rc;
danielk19770202b292004-06-09 09:55:16 +0000875 }
drh4f26d6c2004-05-26 23:25:30 +0000876 }
danielk19770202b292004-06-09 09:55:16 +0000877 /* If a NULL pointer was passed as the collate function, fall through
danielk19774e6af132004-06-10 14:01:08 +0000878 ** to the blob case and use memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000879 }
880
danielk19774e6af132004-06-10 14:01:08 +0000881 /* Both values must be blobs. Compare using memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000882 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
883 if( rc==0 ){
884 rc = pMem1->n - pMem2->n;
885 }
886 return rc;
887}
danielk1977c572ef72004-05-27 09:28:41 +0000888
drhd5788202004-05-28 08:21:05 +0000889/*
890** Move data out of a btree key or data field and into a Mem structure.
891** The data or key is taken from the entry that pCur is currently pointing
892** to. offset and amt determine what portion of the data or key to retrieve.
893** key is true to get the key or false to get data. The result is written
894** into the pMem element.
895**
896** The pMem structure is assumed to be uninitialized. Any prior content
897** is overwritten without being freed.
898**
899** If this routine fails for any reason (malloc returns NULL or unable
900** to read from the disk) then the pMem is left in an inconsistent state.
901*/
902int sqlite3VdbeMemFromBtree(
903 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
904 int offset, /* Offset from the start of data to return bytes from. */
905 int amt, /* Number of bytes to return. */
906 int key, /* If true, retrieve from the btree key, not data. */
907 Mem *pMem /* OUT: Return data in this Mem structure. */
908){
danielk19774b0aa4c2009-05-28 11:05:57 +0000909 char *zData; /* Data from the btree layer */
910 int available = 0; /* Number of bytes available on the local btree page */
911 int rc = SQLITE_OK; /* Return code */
drhd5788202004-05-28 08:21:05 +0000912
drh5d1a8722009-07-22 18:07:40 +0000913 assert( sqlite3BtreeCursorIsValid(pCur) );
914
danielk19774b0aa4c2009-05-28 11:05:57 +0000915 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
916 ** that both the BtShared and database handle mutexes are held. */
drh3d4501e2008-12-04 20:40:10 +0000917 assert( (pMem->flags & MEM_RowSet)==0 );
drhd5788202004-05-28 08:21:05 +0000918 if( key ){
drhe51c44f2004-05-30 20:46:09 +0000919 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000920 }else{
drhe51c44f2004-05-30 20:46:09 +0000921 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000922 }
drh61fc5952007-04-01 23:49:51 +0000923 assert( zData!=0 );
drhd5788202004-05-28 08:21:05 +0000924
drh4c8555f2009-06-25 01:47:11 +0000925 if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +0000926 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000927 pMem->z = &zData[offset];
928 pMem->flags = MEM_Blob|MEM_Ephem;
danielk1977a7a8e142008-02-13 18:25:27 +0000929 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
930 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
drhd5788202004-05-28 08:21:05 +0000931 pMem->enc = 0;
drh9c054832004-05-31 18:51:57 +0000932 pMem->type = SQLITE_BLOB;
drhd5788202004-05-28 08:21:05 +0000933 if( key ){
danielk1977a7a8e142008-02-13 18:25:27 +0000934 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000935 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000936 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000937 }
danielk1977a7a8e142008-02-13 18:25:27 +0000938 pMem->z[amt] = 0;
939 pMem->z[amt+1] = 0;
drhd5788202004-05-28 08:21:05 +0000940 if( rc!=SQLITE_OK ){
danielk1977a7a8e142008-02-13 18:25:27 +0000941 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000942 }
943 }
danielk1977a7a8e142008-02-13 18:25:27 +0000944 pMem->n = amt;
drhd5788202004-05-28 08:21:05 +0000945
danielk1977a7a8e142008-02-13 18:25:27 +0000946 return rc;
drhd5788202004-05-28 08:21:05 +0000947}
948
danielk19774e6af132004-06-10 14:01:08 +0000949/* This function is only available internally, it is not part of the
950** external API. It works in a similar way to sqlite3_value_text(),
951** except the data returned is in the encoding specified by the second
952** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
953** SQLITE_UTF8.
drh7d9bd4e2006-02-16 18:16:36 +0000954**
955** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
956** If that is the case, then the result must be aligned on an even byte
957** boundary.
danielk19774e6af132004-06-10 14:01:08 +0000958*/
drhb21c8cd2007-08-21 19:33:56 +0000959const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
danielk1977bfd6cce2004-06-18 04:24:54 +0000960 if( !pVal ) return 0;
drhb21c8cd2007-08-21 19:33:56 +0000961
962 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
drh7d9bd4e2006-02-16 18:16:36 +0000963 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
drh3d4501e2008-12-04 20:40:10 +0000964 assert( (pVal->flags & MEM_RowSet)==0 );
danielk1977bfd6cce2004-06-18 04:24:54 +0000965
danielk19774e6af132004-06-10 14:01:08 +0000966 if( pVal->flags&MEM_Null ){
danielk19774e6af132004-06-10 14:01:08 +0000967 return 0;
968 }
drhf1f6c582006-01-12 19:42:41 +0000969 assert( (MEM_Blob>>3) == MEM_Str );
970 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
drh45d29302012-01-08 22:18:33 +0000971 ExpandBlob(pVal);
danielk19774e6af132004-06-10 14:01:08 +0000972 if( pVal->flags&MEM_Str ){
drhb21c8cd2007-08-21 19:33:56 +0000973 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
shane1fc41292008-07-08 22:28:48 +0000974 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
drh7d9bd4e2006-02-16 18:16:36 +0000975 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
drhb21c8cd2007-08-21 19:33:56 +0000976 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
drh7d9bd4e2006-02-16 18:16:36 +0000977 return 0;
978 }
979 }
drh2faf5f52011-12-30 15:17:47 +0000980 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
drhf0313812006-09-04 15:53:53 +0000981 }else{
982 assert( (pVal->flags&MEM_Blob)==0 );
drhb21c8cd2007-08-21 19:33:56 +0000983 sqlite3VdbeMemStringify(pVal, enc);
drh8df32842008-12-09 02:51:23 +0000984 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
danielk19774e6af132004-06-10 14:01:08 +0000985 }
drhb21c8cd2007-08-21 19:33:56 +0000986 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
987 || pVal->db->mallocFailed );
drh7d9bd4e2006-02-16 18:16:36 +0000988 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
989 return pVal->z;
990 }else{
991 return 0;
992 }
danielk19774e6af132004-06-10 14:01:08 +0000993}
994
drh6a6124e2004-06-27 01:56:33 +0000995/*
996** Create a new sqlite3_value object.
997*/
drh17435752007-08-16 04:30:38 +0000998sqlite3_value *sqlite3ValueNew(sqlite3 *db){
danielk197726783a52007-08-29 14:06:22 +0000999 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
danielk19774e6af132004-06-10 14:01:08 +00001000 if( p ){
1001 p->flags = MEM_Null;
1002 p->type = SQLITE_NULL;
drhb21c8cd2007-08-21 19:33:56 +00001003 p->db = db;
danielk19774e6af132004-06-10 14:01:08 +00001004 }
1005 return p;
1006}
1007
drh6a6124e2004-06-27 01:56:33 +00001008/*
danielk1977aee18ef2005-03-09 12:26:50 +00001009** Create a new sqlite3_value object, containing the value of pExpr.
1010**
1011** This only works for very simple expressions that consist of one constant
drhc4dd3fd2008-01-22 01:48:05 +00001012** token (i.e. "5", "5.1", "'a string'"). If the expression can
danielk1977aee18ef2005-03-09 12:26:50 +00001013** be converted directly into a value, then the value is allocated and
1014** a pointer written to *ppVal. The caller is responsible for deallocating
1015** the value by passing it to sqlite3ValueFree() later on. If the expression
1016** cannot be converted to a value, then *ppVal is set to NULL.
1017*/
1018int sqlite3ValueFromExpr(
drhb21c8cd2007-08-21 19:33:56 +00001019 sqlite3 *db, /* The database connection */
drh17435752007-08-16 04:30:38 +00001020 Expr *pExpr, /* The expression to evaluate */
1021 u8 enc, /* Encoding to use */
1022 u8 affinity, /* Affinity to use */
1023 sqlite3_value **ppVal /* Write the new value here */
danielk1977aee18ef2005-03-09 12:26:50 +00001024){
1025 int op;
1026 char *zVal = 0;
1027 sqlite3_value *pVal = 0;
drh93518622010-09-30 14:48:06 +00001028 int negInt = 1;
1029 const char *zNeg = "";
danielk1977aee18ef2005-03-09 12:26:50 +00001030
1031 if( !pExpr ){
1032 *ppVal = 0;
1033 return SQLITE_OK;
1034 }
1035 op = pExpr->op;
drh4a466d32010-06-25 14:17:58 +00001036
drhfaacf172011-08-12 01:51:45 +00001037 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
drh4a466d32010-06-25 14:17:58 +00001038 ** The ifdef here is to enable us to achieve 100% branch test coverage even
drhfaacf172011-08-12 01:51:45 +00001039 ** when SQLITE_ENABLE_STAT3 is omitted.
drh4a466d32010-06-25 14:17:58 +00001040 */
drhfaacf172011-08-12 01:51:45 +00001041#ifdef SQLITE_ENABLE_STAT3
drh4a466d32010-06-25 14:17:58 +00001042 if( op==TK_REGISTER ) op = pExpr->op2;
1043#else
1044 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
1045#endif
danielk1977aee18ef2005-03-09 12:26:50 +00001046
drh93518622010-09-30 14:48:06 +00001047 /* Handle negative integers in a single step. This is needed in the
1048 ** case when the value is -9223372036854775808.
1049 */
1050 if( op==TK_UMINUS
1051 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
1052 pExpr = pExpr->pLeft;
1053 op = pExpr->op;
1054 negInt = -1;
1055 zNeg = "-";
1056 }
1057
danielk1977aee18ef2005-03-09 12:26:50 +00001058 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
drh17435752007-08-16 04:30:38 +00001059 pVal = sqlite3ValueNew(db);
drh33e619f2009-05-28 01:00:55 +00001060 if( pVal==0 ) goto no_mem;
1061 if( ExprHasProperty(pExpr, EP_IntValue) ){
drh93518622010-09-30 14:48:06 +00001062 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
drh33e619f2009-05-28 01:00:55 +00001063 }else{
drh93518622010-09-30 14:48:06 +00001064 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
drh33e619f2009-05-28 01:00:55 +00001065 if( zVal==0 ) goto no_mem;
1066 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
drh3995c262009-08-19 22:14:17 +00001067 if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
drh33e619f2009-05-28 01:00:55 +00001068 }
danielk1977aee18ef2005-03-09 12:26:50 +00001069 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
drhe3b9bfe2009-05-05 12:54:50 +00001070 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
danielk1977aee18ef2005-03-09 12:26:50 +00001071 }else{
drhe3b9bfe2009-05-05 12:54:50 +00001072 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
1073 }
drh93518622010-09-30 14:48:06 +00001074 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
drhe3b9bfe2009-05-05 12:54:50 +00001075 if( enc!=SQLITE_UTF8 ){
1076 sqlite3VdbeChangeEncoding(pVal, enc);
danielk1977aee18ef2005-03-09 12:26:50 +00001077 }
1078 }else if( op==TK_UMINUS ) {
drh93518622010-09-30 14:48:06 +00001079 /* This branch happens for multiple negative signs. Ex: -(-5) */
drhb21c8cd2007-08-21 19:33:56 +00001080 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
drh93518622010-09-30 14:48:06 +00001081 sqlite3VdbeMemNumerify(pVal);
drhd50ffc42011-03-08 02:38:28 +00001082 if( pVal->u.i==SMALLEST_INT64 ){
1083 pVal->flags &= MEM_Int;
1084 pVal->flags |= MEM_Real;
1085 pVal->r = (double)LARGEST_INT64;
1086 }else{
1087 pVal->u.i = -pVal->u.i;
1088 }
1089 pVal->r = -pVal->r;
drh93518622010-09-30 14:48:06 +00001090 sqlite3ValueApplyAffinity(pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +00001091 }
drh9b3eb0a2011-01-21 14:37:04 +00001092 }else if( op==TK_NULL ){
1093 pVal = sqlite3ValueNew(db);
drhb1aa0ab2011-02-18 17:23:23 +00001094 if( pVal==0 ) goto no_mem;
danielk1977aee18ef2005-03-09 12:26:50 +00001095 }
1096#ifndef SQLITE_OMIT_BLOB_LITERAL
1097 else if( op==TK_BLOB ){
1098 int nVal;
drh33e619f2009-05-28 01:00:55 +00001099 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
1100 assert( pExpr->u.zToken[1]=='\'' );
danielk19771e536952007-08-16 10:09:01 +00001101 pVal = sqlite3ValueNew(db);
danielk1977f150c9d2008-10-30 17:21:12 +00001102 if( !pVal ) goto no_mem;
drh33e619f2009-05-28 01:00:55 +00001103 zVal = &pExpr->u.zToken[2];
drhb7916a72009-05-27 10:31:29 +00001104 nVal = sqlite3Strlen30(zVal)-1;
1105 assert( zVal[nVal]=='\'' );
drhca48c902008-01-18 14:08:24 +00001106 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
drh633e6d52008-07-28 19:34:53 +00001107 0, SQLITE_DYNAMIC);
danielk1977aee18ef2005-03-09 12:26:50 +00001108 }
1109#endif
1110
dan937d0de2009-10-15 18:35:38 +00001111 if( pVal ){
1112 sqlite3VdbeMemStoreType(pVal);
1113 }
danielk1977aee18ef2005-03-09 12:26:50 +00001114 *ppVal = pVal;
1115 return SQLITE_OK;
1116
1117no_mem:
drh17435752007-08-16 04:30:38 +00001118 db->mallocFailed = 1;
drh633e6d52008-07-28 19:34:53 +00001119 sqlite3DbFree(db, zVal);
danielk1977aee18ef2005-03-09 12:26:50 +00001120 sqlite3ValueFree(pVal);
1121 *ppVal = 0;
1122 return SQLITE_NOMEM;
1123}
1124
1125/*
drh6a6124e2004-06-27 01:56:33 +00001126** Change the string value of an sqlite3_value object
1127*/
danielk1977bfd6cce2004-06-18 04:24:54 +00001128void sqlite3ValueSetStr(
drh17435752007-08-16 04:30:38 +00001129 sqlite3_value *v, /* Value to be set */
1130 int n, /* Length of string z */
1131 const void *z, /* Text of the new string */
1132 u8 enc, /* Encoding to use */
1133 void (*xDel)(void*) /* Destructor for the string */
danielk1977bfd6cce2004-06-18 04:24:54 +00001134){
drhb21c8cd2007-08-21 19:33:56 +00001135 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
danielk19774e6af132004-06-10 14:01:08 +00001136}
1137
drh6a6124e2004-06-27 01:56:33 +00001138/*
1139** Free an sqlite3_value object
1140*/
danielk19774e6af132004-06-10 14:01:08 +00001141void sqlite3ValueFree(sqlite3_value *v){
danielk1977bfd6cce2004-06-18 04:24:54 +00001142 if( !v ) return;
danielk1977a7a8e142008-02-13 18:25:27 +00001143 sqlite3VdbeMemRelease((Mem *)v);
drh633e6d52008-07-28 19:34:53 +00001144 sqlite3DbFree(((Mem*)v)->db, v);
danielk19774e6af132004-06-10 14:01:08 +00001145}
1146
drh6a6124e2004-06-27 01:56:33 +00001147/*
1148** Return the number of bytes in the sqlite3_value object assuming
1149** that it uses the encoding "enc"
1150*/
drhb21c8cd2007-08-21 19:33:56 +00001151int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
danielk19774e6af132004-06-10 14:01:08 +00001152 Mem *p = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +00001153 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
drhb026e052007-05-02 01:34:31 +00001154 if( p->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00001155 return p->n + p->u.nZero;
drhb026e052007-05-02 01:34:31 +00001156 }else{
1157 return p->n;
1158 }
danielk19774e6af132004-06-10 14:01:08 +00001159 }
1160 return 0;
1161}