blob: 7abf08dc7d61e660b779ee470161fe09d50836b2 [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
danielk1977822a5162008-05-16 04:51:54 +000017**
drh0a687d12008-07-08 14:52:07 +000018** $Id: vdbemem.c,v 1.116 2008/07/08 14:52:10 drh Exp $
drh4f26d6c2004-05-26 23:25:30 +000019*/
20#include "sqliteInt.h"
drh4f26d6c2004-05-26 23:25:30 +000021#include <ctype.h>
22#include "vdbeInt.h"
23
24/*
danielk19771cc5ed82007-05-16 17:28:43 +000025** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
26** P if required.
27*/
drhb21c8cd2007-08-21 19:33:56 +000028#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +000029
30/*
danielk1977bfd6cce2004-06-18 04:24:54 +000031** 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.
drh4f26d6c2004-05-26 23:25:30 +000034**
danielk1977bfd6cce2004-06-18 04:24:54 +000035** 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.
drh4f26d6c2004-05-26 23:25:30 +000038**
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*/
drhb21c8cd2007-08-21 19:33:56 +000043int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
danielk19772c336542005-01-13 02:14:23 +000044 int rc;
drheb2e1762004-05-27 01:53:56 +000045 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
drh4f26d6c2004-05-26 23:25:30 +000046 return SQLITE_OK;
47 }
drhb21c8cd2007-08-21 19:33:56 +000048 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh6c626082004-11-14 21:56:29 +000049#ifdef SQLITE_OMIT_UTF16
50 return SQLITE_ERROR;
51#else
danielk197700fd9572005-12-07 06:27:43 +000052
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 */
drhb21c8cd2007-08-21 19:33:56 +000056 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
danielk197700fd9572005-12-07 06:27:43 +000057 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
58 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
59 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
danielk19772c336542005-01-13 02:14:23 +000060 return rc;
drh6c626082004-11-14 21:56:29 +000061#endif
drh4f26d6c2004-05-26 23:25:30 +000062}
63
drheb2e1762004-05-27 01:53:56 +000064/*
danielk1977a7a8e142008-02-13 18:25:27 +000065** Make sure pMem->z points to a writable allocation of at least
66** n bytes.
67**
68** If the memory cell currently contains string or blob data
69** and the third argument passed to this function is true, the
70** current content of the cell is preserved. Otherwise, it may
71** be discarded.
72**
73** This function sets the MEM_Dyn flag and clears any xDel callback.
74** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
75** not set, Mem.n is zeroed.
76*/
77int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
danielk19775f096132008-03-28 15:44:09 +000078 assert( 1 >=
79 ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
80 (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
81 ((pMem->flags&MEM_Ephem) ? 1 : 0) +
82 ((pMem->flags&MEM_Static) ? 1 : 0)
danielk1977a7a8e142008-02-13 18:25:27 +000083 );
84
danielk1977f934c5a2008-03-28 19:15:34 +000085 if( !pMem->zMalloc || sqlite3MallocSize(pMem->zMalloc)<n ){
danielk19775f096132008-03-28 15:44:09 +000086 n = (n>32?n:32);
87 if( preserve && pMem->z==pMem->zMalloc ){
88 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
danielk1977ce98bba2008-04-03 10:13:01 +000089 if( !pMem->z ){
mlcreech6cc9c282008-04-02 04:23:32 +000090 pMem->flags = MEM_Null;
danielk1977ce98bba2008-04-03 10:13:01 +000091 }
danielk19775f096132008-03-28 15:44:09 +000092 preserve = 0;
93 }else{
94 sqlite3_free(pMem->zMalloc);
95 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
danielk1977a7a8e142008-02-13 18:25:27 +000096 }
danielk1977a7a8e142008-02-13 18:25:27 +000097 }
danielk19775f096132008-03-28 15:44:09 +000098
99 if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
100 memcpy(pMem->zMalloc, pMem->z, pMem->n);
101 }
drhb08c2a72008-04-16 00:28:13 +0000102 if( pMem->flags&MEM_Dyn && pMem->xDel ){
danielk19775f096132008-03-28 15:44:09 +0000103 pMem->xDel((void *)(pMem->z));
104 }
105
106 pMem->z = pMem->zMalloc;
107 pMem->flags &= ~(MEM_Ephem|MEM_Static);
108 pMem->xDel = 0;
109 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
danielk1977a7a8e142008-02-13 18:25:27 +0000110}
111
112/*
drheb2e1762004-05-27 01:53:56 +0000113** Make the given Mem object MEM_Dyn.
114**
115** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
116*/
drhb21c8cd2007-08-21 19:33:56 +0000117int sqlite3VdbeMemDynamicify(Mem *pMem){
danielk1977a7a8e142008-02-13 18:25:27 +0000118 int f;
drhb21c8cd2007-08-21 19:33:56 +0000119 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
120 expandBlob(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000121 f = pMem->flags;
danielk19775f096132008-03-28 15:44:09 +0000122 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
danielk1977a7a8e142008-02-13 18:25:27 +0000123 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
124 return SQLITE_NOMEM;
125 }
126 pMem->z[pMem->n] = 0;
127 pMem->z[pMem->n+1] = 0;
128 pMem->flags |= MEM_Term;
drheb2e1762004-05-27 01:53:56 +0000129 }
danielk1977a7a8e142008-02-13 18:25:27 +0000130
drhf4479502004-05-27 03:12:53 +0000131 return SQLITE_OK;
drheb2e1762004-05-27 01:53:56 +0000132}
133
134/*
drhfdf972a2007-05-02 13:30:27 +0000135** If the given Mem* has a zero-filled tail, turn it into an ordinary
drhb026e052007-05-02 01:34:31 +0000136** blob stored in dynamically allocated space.
137*/
danielk1977246ad312007-05-16 14:23:00 +0000138#ifndef SQLITE_OMIT_INCRBLOB
drhb21c8cd2007-08-21 19:33:56 +0000139int sqlite3VdbeMemExpandBlob(Mem *pMem){
drhb026e052007-05-02 01:34:31 +0000140 if( pMem->flags & MEM_Zero ){
drh98640a32007-06-07 19:08:32 +0000141 int nByte;
danielk1977a7a8e142008-02-13 18:25:27 +0000142 assert( pMem->flags&MEM_Blob );
drhb21c8cd2007-08-21 19:33:56 +0000143 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977a7a8e142008-02-13 18:25:27 +0000144
145 /* Set nByte to the number of bytes required to store the expanded blob. */
146 nByte = pMem->n + pMem->u.i;
147 if( nByte<=0 ){
148 nByte = 1;
149 }
150 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
drhb026e052007-05-02 01:34:31 +0000151 return SQLITE_NOMEM;
152 }
danielk1977a7a8e142008-02-13 18:25:27 +0000153
154 memset(&pMem->z[pMem->n], 0, pMem->u.i);
drhfdf972a2007-05-02 13:30:27 +0000155 pMem->n += pMem->u.i;
danielk1977a7a8e142008-02-13 18:25:27 +0000156 pMem->flags &= ~(MEM_Zero|MEM_Term);
drhb026e052007-05-02 01:34:31 +0000157 }
158 return SQLITE_OK;
159}
danielk1977246ad312007-05-16 14:23:00 +0000160#endif
drhb026e052007-05-02 01:34:31 +0000161
162
163/*
drheb2e1762004-05-27 01:53:56 +0000164** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
165** of the Mem.z[] array can be modified.
166**
167** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
168*/
drhb21c8cd2007-08-21 19:33:56 +0000169int sqlite3VdbeMemMakeWriteable(Mem *pMem){
danielk1977a7a8e142008-02-13 18:25:27 +0000170 return sqlite3VdbeMemDynamicify(pMem);
drheb2e1762004-05-27 01:53:56 +0000171}
172
173/*
174** Make sure the given Mem is \u0000 terminated.
175*/
drhb21c8cd2007-08-21 19:33:56 +0000176int sqlite3VdbeMemNulTerminate(Mem *pMem){
177 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk197713073932004-06-30 11:54:06 +0000178 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
drheb2e1762004-05-27 01:53:56 +0000179 return SQLITE_OK; /* Nothing to do */
180 }
danielk1977a7a8e142008-02-13 18:25:27 +0000181 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
182 return SQLITE_NOMEM;
danielk19773f6b0872004-06-17 05:36:44 +0000183 }
danielk1977a7a8e142008-02-13 18:25:27 +0000184 pMem->z[pMem->n] = 0;
185 pMem->z[pMem->n+1] = 0;
186 pMem->flags |= MEM_Term;
danielk19773f6b0872004-06-17 05:36:44 +0000187 return SQLITE_OK;
drheb2e1762004-05-27 01:53:56 +0000188}
189
190/*
danielk197713073932004-06-30 11:54:06 +0000191** Add MEM_Str to the set of representations for the given Mem. Numbers
192** are converted using sqlite3_snprintf(). Converting a BLOB to a string
193** is a no-op.
drheb2e1762004-05-27 01:53:56 +0000194**
195** Existing representations MEM_Int and MEM_Real are *not* invalidated.
danielk197713073932004-06-30 11:54:06 +0000196**
197** A MEM_Null value will never be passed to this function. This function is
198** used for converting values to text for returning to the user (i.e. via
199** sqlite3_value_text()), or for ensuring that values to be used as btree
200** keys are strings. In the former case a NULL pointer is returned the
201** user and the later is an internal programming error.
drheb2e1762004-05-27 01:53:56 +0000202*/
drhb21c8cd2007-08-21 19:33:56 +0000203int sqlite3VdbeMemStringify(Mem *pMem, int enc){
drheb2e1762004-05-27 01:53:56 +0000204 int rc = SQLITE_OK;
205 int fg = pMem->flags;
danielk1977a7a8e142008-02-13 18:25:27 +0000206 const int nByte = 32;
drheb2e1762004-05-27 01:53:56 +0000207
drhb21c8cd2007-08-21 19:33:56 +0000208 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977def0fec2007-05-10 15:37:52 +0000209 assert( !(fg&MEM_Zero) );
drheb2e1762004-05-27 01:53:56 +0000210 assert( !(fg&(MEM_Str|MEM_Blob)) );
danielk197713073932004-06-30 11:54:06 +0000211 assert( fg&(MEM_Int|MEM_Real) );
drheb2e1762004-05-27 01:53:56 +0000212
danielk1977a7a8e142008-02-13 18:25:27 +0000213 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
214 return SQLITE_NOMEM;
215 }
216
217 /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
danielk197713073932004-06-30 11:54:06 +0000218 ** string representation of the value. Then, if the required encoding
219 ** is UTF-16le or UTF-16be do a translation.
220 **
221 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
222 */
drh8df447f2005-11-01 15:48:24 +0000223 if( fg & MEM_Int ){
danielk1977a7a8e142008-02-13 18:25:27 +0000224 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
drh8df447f2005-11-01 15:48:24 +0000225 }else{
226 assert( fg & MEM_Real );
danielk1977a7a8e142008-02-13 18:25:27 +0000227 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
drheb2e1762004-05-27 01:53:56 +0000228 }
danielk1977a7a8e142008-02-13 18:25:27 +0000229 pMem->n = strlen(pMem->z);
danielk197713073932004-06-30 11:54:06 +0000230 pMem->enc = SQLITE_UTF8;
danielk1977a7a8e142008-02-13 18:25:27 +0000231 pMem->flags |= MEM_Str|MEM_Term;
drhb21c8cd2007-08-21 19:33:56 +0000232 sqlite3VdbeChangeEncoding(pMem, enc);
drheb2e1762004-05-27 01:53:56 +0000233 return rc;
234}
235
236/*
drhabfcea22005-09-06 20:36:48 +0000237** Memory cell pMem contains the context of an aggregate function.
238** This routine calls the finalize method for that function. The
239** result of the aggregate is stored back into pMem.
drh90669c12006-01-20 15:45:36 +0000240**
241** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
242** otherwise.
drhabfcea22005-09-06 20:36:48 +0000243*/
drh90669c12006-01-20 15:45:36 +0000244int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
245 int rc = SQLITE_OK;
drha10a34b2005-09-07 22:09:48 +0000246 if( pFunc && pFunc->xFinalize ){
247 sqlite3_context ctx;
drh3c024d62007-03-30 11:23:45 +0000248 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
drhb21c8cd2007-08-21 19:33:56 +0000249 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drha10a34b2005-09-07 22:09:48 +0000250 ctx.s.flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000251 ctx.s.db = pMem->db;
danielk19775f096132008-03-28 15:44:09 +0000252 ctx.s.zMalloc = 0;
drha10a34b2005-09-07 22:09:48 +0000253 ctx.pMem = pMem;
254 ctx.pFunc = pFunc;
drh90669c12006-01-20 15:45:36 +0000255 ctx.isError = 0;
drha10a34b2005-09-07 22:09:48 +0000256 pFunc->xFinalize(&ctx);
drhb08c2a72008-04-16 00:28:13 +0000257 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
danielk19775f096132008-03-28 15:44:09 +0000258 sqlite3_free(pMem->zMalloc);
drha10a34b2005-09-07 22:09:48 +0000259 *pMem = ctx.s;
danielk1977a1644fd2007-08-29 12:31:25 +0000260 rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
drhabfcea22005-09-06 20:36:48 +0000261 }
drh90669c12006-01-20 15:45:36 +0000262 return rc;
drhabfcea22005-09-06 20:36:48 +0000263}
264
265/*
danielk19775f096132008-03-28 15:44:09 +0000266** If the memory cell contains a string value that must be freed by
267** invoking an external callback, free it now. Calling this function
268** does not free any Mem.zMalloc buffer.
269*/
270void sqlite3VdbeMemReleaseExternal(Mem *p){
271 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
272 if( p->flags&MEM_Agg ){
273 sqlite3VdbeMemFinalize(p, p->u.pDef);
274 assert( (p->flags & MEM_Agg)==0 );
275 sqlite3VdbeMemRelease(p);
drhb08c2a72008-04-16 00:28:13 +0000276 }else if( p->flags&MEM_Dyn && p->xDel ){
danielk19775f096132008-03-28 15:44:09 +0000277 p->xDel((void *)p->z);
278 p->xDel = 0;
279 }
280}
281
282/*
danielk1977d8123362004-06-12 09:25:12 +0000283** Release any memory held by the Mem. This may leave the Mem in an
284** inconsistent state, for example with (Mem.z==0) and
285** (Mem.type==SQLITE_TEXT).
drhf4479502004-05-27 03:12:53 +0000286*/
danielk1977d8123362004-06-12 09:25:12 +0000287void sqlite3VdbeMemRelease(Mem *p){
danielk19775f096132008-03-28 15:44:09 +0000288 sqlite3VdbeMemReleaseExternal(p);
289 sqlite3_free(p->zMalloc);
290 p->z = 0;
291 p->zMalloc = 0;
292 p->xDel = 0;
drhf4479502004-05-27 03:12:53 +0000293}
294
295/*
drhd8c303f2008-01-11 15:27:03 +0000296** Convert a 64-bit IEEE double into a 64-bit signed integer.
297** If the double is too large, return 0x8000000000000000.
298**
299** Most systems appear to do this simply by assigning
300** variables and without the extra range tests. But
301** there are reports that windows throws an expection
302** if the floating point value is out of range. (See ticket #2880.)
303** Because we do not completely understand the problem, we will
304** take the conservative approach and always do range tests
305** before attempting the conversion.
306*/
307static i64 doubleToInt64(double r){
308 /*
309 ** Many compilers we encounter do not define constants for the
310 ** minimum and maximum 64-bit integers, or they define them
311 ** inconsistently. And many do not understand the "LL" notation.
312 ** So we define our own static constants here using nothing
313 ** larger than a 32-bit integer constant.
314 */
drh0f050352008-05-09 18:03:13 +0000315 static const i64 maxInt = LARGEST_INT64;
316 static const i64 minInt = SMALLEST_INT64;
drhd8c303f2008-01-11 15:27:03 +0000317
318 if( r<(double)minInt ){
319 return minInt;
320 }else if( r>(double)maxInt ){
321 return minInt;
322 }else{
323 return (i64)r;
324 }
325}
326
327/*
drh6a6124e2004-06-27 01:56:33 +0000328** Return some kind of integer value which is the best we can do
329** at representing the value that *pMem describes as an integer.
330** If pMem is an integer, then the value is exact. If pMem is
331** a floating-point then the value returned is the integer part.
332** If pMem is a string or blob, then we make an attempt to convert
333** it into a integer and return that. If pMem is NULL, return 0.
334**
335** If pMem is a string, its encoding might be changed.
drheb2e1762004-05-27 01:53:56 +0000336*/
drh6a6124e2004-06-27 01:56:33 +0000337i64 sqlite3VdbeIntValue(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000338 int flags;
339 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
340 flags = pMem->flags;
drh6fec0762004-05-30 01:38:43 +0000341 if( flags & MEM_Int ){
drh3c024d62007-03-30 11:23:45 +0000342 return pMem->u.i;
drh6fec0762004-05-30 01:38:43 +0000343 }else if( flags & MEM_Real ){
drhd8c303f2008-01-11 15:27:03 +0000344 return doubleToInt64(pMem->r);
drh6fec0762004-05-30 01:38:43 +0000345 }else if( flags & (MEM_Str|MEM_Blob) ){
drh6a6124e2004-06-27 01:56:33 +0000346 i64 value;
danielk19775b159dc2007-05-17 16:34:43 +0000347 pMem->flags |= MEM_Str;
drhb21c8cd2007-08-21 19:33:56 +0000348 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
349 || sqlite3VdbeMemNulTerminate(pMem) ){
drhc01be742005-11-03 14:29:55 +0000350 return 0;
drheb2e1762004-05-27 01:53:56 +0000351 }
352 assert( pMem->z );
drhb6a9ece2007-06-26 00:37:27 +0000353 sqlite3Atoi64(pMem->z, &value);
drh6a6124e2004-06-27 01:56:33 +0000354 return value;
drheb2e1762004-05-27 01:53:56 +0000355 }else{
drh6a6124e2004-06-27 01:56:33 +0000356 return 0;
drheb2e1762004-05-27 01:53:56 +0000357 }
drh6a6124e2004-06-27 01:56:33 +0000358}
359
360/*
drh6a6124e2004-06-27 01:56:33 +0000361** Return the best representation of pMem that we can get into a
362** double. If pMem is already a double or an integer, return its
363** value. If it is a string or blob, try to convert it to a double.
364** If it is a NULL, return 0.0.
drheb2e1762004-05-27 01:53:56 +0000365*/
drh6a6124e2004-06-27 01:56:33 +0000366double sqlite3VdbeRealValue(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000367 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977f93bbbe2004-05-27 10:30:52 +0000368 if( pMem->flags & MEM_Real ){
drh6a6124e2004-06-27 01:56:33 +0000369 return pMem->r;
370 }else if( pMem->flags & MEM_Int ){
drh3c024d62007-03-30 11:23:45 +0000371 return (double)pMem->u.i;
drheb2e1762004-05-27 01:53:56 +0000372 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh487e2622005-06-25 18:42:14 +0000373 double val = 0.0;
danielk19775b159dc2007-05-17 16:34:43 +0000374 pMem->flags |= MEM_Str;
drhb21c8cd2007-08-21 19:33:56 +0000375 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
376 || sqlite3VdbeMemNulTerminate(pMem) ){
drhc01be742005-11-03 14:29:55 +0000377 return 0.0;
drheb2e1762004-05-27 01:53:56 +0000378 }
379 assert( pMem->z );
drh487e2622005-06-25 18:42:14 +0000380 sqlite3AtoF(pMem->z, &val);
381 return val;
drheb2e1762004-05-27 01:53:56 +0000382 }else{
drh6a6124e2004-06-27 01:56:33 +0000383 return 0.0;
drheb2e1762004-05-27 01:53:56 +0000384 }
drh6a6124e2004-06-27 01:56:33 +0000385}
386
387/*
drh8df447f2005-11-01 15:48:24 +0000388** The MEM structure is already a MEM_Real. Try to also make it a
389** MEM_Int if we can.
390*/
391void sqlite3VdbeIntegerAffinity(Mem *pMem){
392 assert( pMem->flags & MEM_Real );
drhb21c8cd2007-08-21 19:33:56 +0000393 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhefe3d652008-01-11 00:06:10 +0000394
drhd8c303f2008-01-11 15:27:03 +0000395 pMem->u.i = doubleToInt64(pMem->r);
396 if( pMem->r==(double)pMem->u.i ){
drh8df447f2005-11-01 15:48:24 +0000397 pMem->flags |= MEM_Int;
398 }
399}
400
danielk1977a7a8e142008-02-13 18:25:27 +0000401static void setTypeFlag(Mem *pMem, int f){
402 MemSetTypeFlag(pMem, f);
403}
404
drh8a512562005-11-14 22:29:05 +0000405/*
406** Convert pMem to type integer. Invalidate any prior representations.
407*/
408int sqlite3VdbeMemIntegerify(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000409 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh3c024d62007-03-30 11:23:45 +0000410 pMem->u.i = sqlite3VdbeIntValue(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000411 setTypeFlag(pMem, MEM_Int);
drh8a512562005-11-14 22:29:05 +0000412 return SQLITE_OK;
413}
drh8df447f2005-11-01 15:48:24 +0000414
415/*
drh8a512562005-11-14 22:29:05 +0000416** Convert pMem so that it is of type MEM_Real.
417** Invalidate any prior representations.
drh6a6124e2004-06-27 01:56:33 +0000418*/
419int sqlite3VdbeMemRealify(Mem *pMem){
drhb21c8cd2007-08-21 19:33:56 +0000420 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drh6a6124e2004-06-27 01:56:33 +0000421 pMem->r = sqlite3VdbeRealValue(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000422 setTypeFlag(pMem, MEM_Real);
drh8a512562005-11-14 22:29:05 +0000423 return SQLITE_OK;
424}
425
426/*
427** Convert pMem so that it has types MEM_Real or MEM_Int or both.
428** Invalidate any prior representations.
429*/
430int sqlite3VdbeMemNumerify(Mem *pMem){
drhcd7b46d2007-05-16 11:55:56 +0000431 double r1, r2;
432 i64 i;
433 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
434 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
drhb21c8cd2007-08-21 19:33:56 +0000435 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
drhcd7b46d2007-05-16 11:55:56 +0000436 r1 = sqlite3VdbeRealValue(pMem);
drhd8c303f2008-01-11 15:27:03 +0000437 i = doubleToInt64(r1);
drhcd7b46d2007-05-16 11:55:56 +0000438 r2 = (double)i;
439 if( r1==r2 ){
440 sqlite3VdbeMemIntegerify(pMem);
441 }else{
442 pMem->r = r1;
danielk1977a7a8e142008-02-13 18:25:27 +0000443 setTypeFlag(pMem, MEM_Real);
drhcd7b46d2007-05-16 11:55:56 +0000444 }
drhf4479502004-05-27 03:12:53 +0000445 return SQLITE_OK;
drh4f26d6c2004-05-26 23:25:30 +0000446}
447
448/*
449** Delete any previous value and set the value stored in *pMem to NULL.
450*/
451void sqlite3VdbeMemSetNull(Mem *pMem){
danielk1977a7a8e142008-02-13 18:25:27 +0000452 setTypeFlag(pMem, MEM_Null);
drh9c054832004-05-31 18:51:57 +0000453 pMem->type = SQLITE_NULL;
drh4f26d6c2004-05-26 23:25:30 +0000454}
455
456/*
drhb026e052007-05-02 01:34:31 +0000457** Delete any previous value and set the value to be a BLOB of length
458** n containing all zeros.
459*/
460void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
461 sqlite3VdbeMemRelease(pMem);
danielk1977a7a8e142008-02-13 18:25:27 +0000462 setTypeFlag(pMem, MEM_Blob);
463 pMem->flags = MEM_Blob|MEM_Zero;
drhb026e052007-05-02 01:34:31 +0000464 pMem->type = SQLITE_BLOB;
465 pMem->n = 0;
drh98640a32007-06-07 19:08:32 +0000466 if( n<0 ) n = 0;
drhb026e052007-05-02 01:34:31 +0000467 pMem->u.i = n;
danielk1977def0fec2007-05-10 15:37:52 +0000468 pMem->enc = SQLITE_UTF8;
drhb026e052007-05-02 01:34:31 +0000469}
470
471/*
drh4f26d6c2004-05-26 23:25:30 +0000472** Delete any previous value and set the value stored in *pMem to val,
473** manifest type INTEGER.
474*/
drheb2e1762004-05-27 01:53:56 +0000475void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
danielk1977d8123362004-06-12 09:25:12 +0000476 sqlite3VdbeMemRelease(pMem);
drh3c024d62007-03-30 11:23:45 +0000477 pMem->u.i = val;
drh4f26d6c2004-05-26 23:25:30 +0000478 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000479 pMem->type = SQLITE_INTEGER;
drh4f26d6c2004-05-26 23:25:30 +0000480}
481
482/*
483** Delete any previous value and set the value stored in *pMem to val,
484** manifest type REAL.
485*/
drheb2e1762004-05-27 01:53:56 +0000486void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
drh0de3ae92008-04-28 16:55:26 +0000487 if( sqlite3IsNaN(val) ){
drh53c14022007-05-10 17:23:11 +0000488 sqlite3VdbeMemSetNull(pMem);
489 }else{
490 sqlite3VdbeMemRelease(pMem);
491 pMem->r = val;
492 pMem->flags = MEM_Real;
493 pMem->type = SQLITE_FLOAT;
494 }
drh4f26d6c2004-05-26 23:25:30 +0000495}
496
497/*
drh023ae032007-05-08 12:12:16 +0000498** Return true if the Mem object contains a TEXT or BLOB that is
499** too large - whose size exceeds SQLITE_MAX_LENGTH.
500*/
501int sqlite3VdbeMemTooBig(Mem *p){
drhfa4a4b92008-03-19 21:45:51 +0000502 assert( p->db!=0 );
drh023ae032007-05-08 12:12:16 +0000503 if( p->flags & (MEM_Str|MEM_Blob) ){
504 int n = p->n;
505 if( p->flags & MEM_Zero ){
506 n += p->u.i;
507 }
drhbb4957f2008-03-20 14:03:29 +0000508 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
drh023ae032007-05-08 12:12:16 +0000509 }
510 return 0;
511}
512
danielk1977e5f5b8f2008-03-28 18:11:16 +0000513/*
514** Size of struct Mem not including the Mem.zMalloc member.
515*/
mlcreechfe3f4e82008-03-29 23:25:27 +0000516#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
danielk19775f096132008-03-28 15:44:09 +0000517
drh023ae032007-05-08 12:12:16 +0000518/*
drhfebe1062004-08-28 18:17:48 +0000519** Make an shallow copy of pFrom into pTo. Prior contents of
drha05a7222008-01-19 03:35:58 +0000520** pTo are freed. The pFrom->z field is not duplicated. If
drhfebe1062004-08-28 18:17:48 +0000521** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
522** and flags gets srcType (either MEM_Ephem or MEM_Static).
drh4f26d6c2004-05-26 23:25:30 +0000523*/
drhfebe1062004-08-28 18:17:48 +0000524void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
danielk19775f096132008-03-28 15:44:09 +0000525 sqlite3VdbeMemReleaseExternal(pTo);
526 memcpy(pTo, pFrom, MEMCELLSIZE);
danielk1977d8123362004-06-12 09:25:12 +0000527 pTo->xDel = 0;
drhb08c2a72008-04-16 00:28:13 +0000528 if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){
danielk1977a7a8e142008-02-13 18:25:27 +0000529 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
drhfebe1062004-08-28 18:17:48 +0000530 assert( srcType==MEM_Ephem || srcType==MEM_Static );
531 pTo->flags |= srcType;
532 }
533}
534
535/*
536** Make a full copy of pFrom into pTo. Prior contents of pTo are
537** freed before the copy is made.
538*/
drhb21c8cd2007-08-21 19:33:56 +0000539int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
danielk1977a7a8e142008-02-13 18:25:27 +0000540 int rc = SQLITE_OK;
danielk1977a7a8e142008-02-13 18:25:27 +0000541
danielk19775f096132008-03-28 15:44:09 +0000542 sqlite3VdbeMemReleaseExternal(pTo);
543 memcpy(pTo, pFrom, MEMCELLSIZE);
544 pTo->flags &= ~MEM_Dyn;
545
546 if( pTo->flags&(MEM_Str|MEM_Blob) ){
547 if( 0==(pFrom->flags&MEM_Static) ){
548 pTo->flags |= MEM_Ephem;
549 rc = sqlite3VdbeMemMakeWriteable(pTo);
danielk19779172fd82008-02-14 15:31:52 +0000550 }
danielk1977a7a8e142008-02-13 18:25:27 +0000551 }
552
drh71c697e2004-08-08 23:39:19 +0000553 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000554}
555
drheb2e1762004-05-27 01:53:56 +0000556/*
danielk1977369f27e2004-06-15 11:40:04 +0000557** Transfer the contents of pFrom to pTo. Any existing value in pTo is
drhfebe1062004-08-28 18:17:48 +0000558** freed. If pFrom contains ephemeral data, a copy is made.
559**
drh643167f2008-01-22 21:30:53 +0000560** pFrom contains an SQL NULL when this routine returns.
danielk1977369f27e2004-06-15 11:40:04 +0000561*/
drh643167f2008-01-22 21:30:53 +0000562void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
drhb21c8cd2007-08-21 19:33:56 +0000563 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
564 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
565 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
danielk19775f096132008-03-28 15:44:09 +0000566
567 sqlite3VdbeMemRelease(pTo);
danielk197713073932004-06-30 11:54:06 +0000568 memcpy(pTo, pFrom, sizeof(Mem));
danielk197713073932004-06-30 11:54:06 +0000569 pFrom->flags = MEM_Null;
570 pFrom->xDel = 0;
danielk19775f096132008-03-28 15:44:09 +0000571 pFrom->zMalloc = 0;
danielk1977369f27e2004-06-15 11:40:04 +0000572}
573
574/*
drheb2e1762004-05-27 01:53:56 +0000575** Change the value of a Mem to be a string or a BLOB.
danielk1977a7a8e142008-02-13 18:25:27 +0000576**
577** The memory management strategy depends on the value of the xDel
578** parameter. If the value passed is SQLITE_TRANSIENT, then the
579** string is copied into a (possibly existing) buffer managed by the
580** Mem structure. Otherwise, any existing buffer is freed and the
581** pointer copied.
drheb2e1762004-05-27 01:53:56 +0000582*/
drh4f26d6c2004-05-26 23:25:30 +0000583int sqlite3VdbeMemSetStr(
584 Mem *pMem, /* Memory cell to set to string value */
585 const char *z, /* String pointer */
586 int n, /* Bytes in string, or negative */
drheb2e1762004-05-27 01:53:56 +0000587 u8 enc, /* Encoding of z. 0 for BLOBs */
danielk1977d8123362004-06-12 09:25:12 +0000588 void (*xDel)(void*) /* Destructor function */
drh4f26d6c2004-05-26 23:25:30 +0000589){
danielk1977a7a8e142008-02-13 18:25:27 +0000590 int nByte = n; /* New value for pMem->n */
drh0a687d12008-07-08 14:52:07 +0000591 int iLimit; /* Maximum allowed string or blob size */
danielk1977a7a8e142008-02-13 18:25:27 +0000592 int flags = 0; /* New value for pMem->flags */
593
drhb21c8cd2007-08-21 19:33:56 +0000594 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977a7a8e142008-02-13 18:25:27 +0000595
596 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
drh4f26d6c2004-05-26 23:25:30 +0000597 if( !z ){
danielk1977a7a8e142008-02-13 18:25:27 +0000598 sqlite3VdbeMemSetNull(pMem);
drh4f26d6c2004-05-26 23:25:30 +0000599 return SQLITE_OK;
600 }
danielk1977a7a8e142008-02-13 18:25:27 +0000601
drh0a687d12008-07-08 14:52:07 +0000602 if( pMem->db ){
603 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
604 }else{
605 iLimit = SQLITE_MAX_LENGTH;
606 }
danielk1977a7a8e142008-02-13 18:25:27 +0000607 flags = (enc==0?MEM_Blob:MEM_Str);
608 if( nByte<0 ){
609 assert( enc!=0 );
drh8fd38972008-02-19 15:44:09 +0000610 if( enc==SQLITE_UTF8 ){
drh0a687d12008-07-08 14:52:07 +0000611 for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
drh8fd38972008-02-19 15:44:09 +0000612 }else{
drh0a687d12008-07-08 14:52:07 +0000613 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
drh8fd38972008-02-19 15:44:09 +0000614 }
danielk1977a7a8e142008-02-13 18:25:27 +0000615 flags |= MEM_Term;
drh4f26d6c2004-05-26 23:25:30 +0000616 }
drh0a687d12008-07-08 14:52:07 +0000617 if( nByte>iLimit ){
618 return SQLITE_TOOBIG;
619 }
danielk1977d8123362004-06-12 09:25:12 +0000620
danielk1977a7a8e142008-02-13 18:25:27 +0000621 /* The following block sets the new values of Mem.z and Mem.xDel. It
622 ** also sets a flag in local variable "flags" to indicate the memory
623 ** management (one of MEM_Dyn or MEM_Static).
624 */
625 if( xDel==SQLITE_TRANSIENT ){
626 int nAlloc = nByte;
627 if( flags&MEM_Term ){
628 nAlloc += (enc==SQLITE_UTF8?1:2);
629 }
630 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
631 return SQLITE_NOMEM;
632 }
633 memcpy(pMem->z, z, nAlloc);
danielk1977a7a8e142008-02-13 18:25:27 +0000634 }else{
635 sqlite3VdbeMemRelease(pMem);
636 pMem->z = (char *)z;
637 pMem->xDel = xDel;
638 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
639 }
danielk1977d8123362004-06-12 09:25:12 +0000640
danielk1977a7a8e142008-02-13 18:25:27 +0000641 pMem->n = nByte;
642 pMem->flags = flags;
643 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
644 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
drh4f26d6c2004-05-26 23:25:30 +0000645
drh6c626082004-11-14 21:56:29 +0000646#ifndef SQLITE_OMIT_UTF16
danielk1977a7a8e142008-02-13 18:25:27 +0000647 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
648 return SQLITE_NOMEM;
drh4f26d6c2004-05-26 23:25:30 +0000649 }
danielk1977a7a8e142008-02-13 18:25:27 +0000650#endif
651
drhf4479502004-05-27 03:12:53 +0000652 return SQLITE_OK;
drh4f26d6c2004-05-26 23:25:30 +0000653}
654
655/*
656** Compare the values contained by the two memory cells, returning
657** negative, zero or positive if pMem1 is less than, equal to, or greater
658** than pMem2. Sorting order is NULL's first, followed by numbers (integers
659** and reals) sorted numerically, followed by text ordered by the collating
660** sequence pColl and finally blob's ordered by memcmp().
661**
662** Two NULL values are considered equal by this function.
663*/
664int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
665 int rc;
666 int f1, f2;
667 int combined_flags;
668
669 /* Interchange pMem1 and pMem2 if the collating sequence specifies
670 ** DESC order.
671 */
672 f1 = pMem1->flags;
673 f2 = pMem2->flags;
674 combined_flags = f1|f2;
675
676 /* If one value is NULL, it is less than the other. If both values
677 ** are NULL, return 0.
678 */
679 if( combined_flags&MEM_Null ){
680 return (f2&MEM_Null) - (f1&MEM_Null);
681 }
682
683 /* If one value is a number and the other is not, the number is less.
684 ** If both are numbers, compare as reals if one is a real, or as integers
685 ** if both values are integers.
686 */
687 if( combined_flags&(MEM_Int|MEM_Real) ){
688 if( !(f1&(MEM_Int|MEM_Real)) ){
689 return 1;
690 }
691 if( !(f2&(MEM_Int|MEM_Real)) ){
692 return -1;
693 }
694 if( (f1 & f2 & MEM_Int)==0 ){
695 double r1, r2;
696 if( (f1&MEM_Real)==0 ){
drh3c024d62007-03-30 11:23:45 +0000697 r1 = pMem1->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000698 }else{
699 r1 = pMem1->r;
700 }
701 if( (f2&MEM_Real)==0 ){
drh3c024d62007-03-30 11:23:45 +0000702 r2 = pMem2->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000703 }else{
704 r2 = pMem2->r;
705 }
706 if( r1<r2 ) return -1;
707 if( r1>r2 ) return 1;
708 return 0;
709 }else{
710 assert( f1&MEM_Int );
711 assert( f2&MEM_Int );
drh3c024d62007-03-30 11:23:45 +0000712 if( pMem1->u.i < pMem2->u.i ) return -1;
713 if( pMem1->u.i > pMem2->u.i ) return 1;
drh4f26d6c2004-05-26 23:25:30 +0000714 return 0;
715 }
716 }
717
718 /* If one value is a string and the other is a blob, the string is less.
719 ** If both are strings, compare using the collating functions.
720 */
721 if( combined_flags&MEM_Str ){
722 if( (f1 & MEM_Str)==0 ){
723 return 1;
724 }
725 if( (f2 & MEM_Str)==0 ){
726 return -1;
727 }
danielk19770202b292004-06-09 09:55:16 +0000728
729 assert( pMem1->enc==pMem2->enc );
danielk1977dc8453f2004-06-12 00:42:34 +0000730 assert( pMem1->enc==SQLITE_UTF8 ||
731 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
danielk19770202b292004-06-09 09:55:16 +0000732
danielk1977b3bf5562006-01-10 17:58:23 +0000733 /* The collation sequence must be defined at this point, even if
734 ** the user deletes the collation sequence after the vdbe program is
735 ** compiled (this was not always the case).
danielk19770202b292004-06-09 09:55:16 +0000736 */
danielk1977466be562004-06-10 02:16:01 +0000737 assert( !pColl || pColl->xCmp );
danielk19770202b292004-06-09 09:55:16 +0000738
739 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000740 if( pMem1->enc==pColl->enc ){
drh7d9bd4e2006-02-16 18:16:36 +0000741 /* The strings are already in the correct encoding. Call the
742 ** comparison function directly */
danielk1977466be562004-06-10 02:16:01 +0000743 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
danielk19770202b292004-06-09 09:55:16 +0000744 }else{
danielk1977f4618892004-06-28 13:09:11 +0000745 u8 origEnc = pMem1->enc;
drh7d9bd4e2006-02-16 18:16:36 +0000746 const void *v1, *v2;
747 int n1, n2;
748 /* Convert the strings into the encoding that the comparison
749 ** function expects */
drhb21c8cd2007-08-21 19:33:56 +0000750 v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
drh7d9bd4e2006-02-16 18:16:36 +0000751 n1 = v1==0 ? 0 : pMem1->n;
drhb21c8cd2007-08-21 19:33:56 +0000752 assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
753 v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
drh7d9bd4e2006-02-16 18:16:36 +0000754 n2 = v2==0 ? 0 : pMem2->n;
drhb21c8cd2007-08-21 19:33:56 +0000755 assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
drh7d9bd4e2006-02-16 18:16:36 +0000756 /* Do the comparison */
757 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
758 /* Convert the strings back into the database encoding */
drhb21c8cd2007-08-21 19:33:56 +0000759 sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
760 sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
danielk1977f4618892004-06-28 13:09:11 +0000761 return rc;
danielk19770202b292004-06-09 09:55:16 +0000762 }
drh4f26d6c2004-05-26 23:25:30 +0000763 }
danielk19770202b292004-06-09 09:55:16 +0000764 /* If a NULL pointer was passed as the collate function, fall through
danielk19774e6af132004-06-10 14:01:08 +0000765 ** to the blob case and use memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000766 }
767
danielk19774e6af132004-06-10 14:01:08 +0000768 /* Both values must be blobs. Compare using memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000769 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
770 if( rc==0 ){
771 rc = pMem1->n - pMem2->n;
772 }
773 return rc;
774}
danielk1977c572ef72004-05-27 09:28:41 +0000775
drhd5788202004-05-28 08:21:05 +0000776/*
777** Move data out of a btree key or data field and into a Mem structure.
778** The data or key is taken from the entry that pCur is currently pointing
779** to. offset and amt determine what portion of the data or key to retrieve.
780** key is true to get the key or false to get data. The result is written
781** into the pMem element.
782**
783** The pMem structure is assumed to be uninitialized. Any prior content
784** is overwritten without being freed.
785**
786** If this routine fails for any reason (malloc returns NULL or unable
787** to read from the disk) then the pMem is left in an inconsistent state.
788*/
789int sqlite3VdbeMemFromBtree(
790 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
791 int offset, /* Offset from the start of data to return bytes from. */
792 int amt, /* Number of bytes to return. */
793 int key, /* If true, retrieve from the btree key, not data. */
794 Mem *pMem /* OUT: Return data in this Mem structure. */
795){
drh61fc5952007-04-01 23:49:51 +0000796 char *zData; /* Data from the btree layer */
797 int available = 0; /* Number of bytes available on the local btree page */
drhb21c8cd2007-08-21 19:33:56 +0000798 sqlite3 *db; /* Database connection */
danielk1977a7a8e142008-02-13 18:25:27 +0000799 int rc = SQLITE_OK;
drhd5788202004-05-28 08:21:05 +0000800
drhb21c8cd2007-08-21 19:33:56 +0000801 db = sqlite3BtreeCursorDb(pCur);
802 assert( sqlite3_mutex_held(db->mutex) );
drhd5788202004-05-28 08:21:05 +0000803 if( key ){
drhe51c44f2004-05-30 20:46:09 +0000804 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000805 }else{
drhe51c44f2004-05-30 20:46:09 +0000806 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000807 }
drh61fc5952007-04-01 23:49:51 +0000808 assert( zData!=0 );
drhd5788202004-05-28 08:21:05 +0000809
danielk1977a7a8e142008-02-13 18:25:27 +0000810 if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
811 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000812 pMem->z = &zData[offset];
813 pMem->flags = MEM_Blob|MEM_Ephem;
danielk1977a7a8e142008-02-13 18:25:27 +0000814 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
815 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
drhd5788202004-05-28 08:21:05 +0000816 pMem->enc = 0;
drh9c054832004-05-31 18:51:57 +0000817 pMem->type = SQLITE_BLOB;
drhd5788202004-05-28 08:21:05 +0000818 if( key ){
danielk1977a7a8e142008-02-13 18:25:27 +0000819 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000820 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000821 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000822 }
danielk1977a7a8e142008-02-13 18:25:27 +0000823 pMem->z[amt] = 0;
824 pMem->z[amt+1] = 0;
drhd5788202004-05-28 08:21:05 +0000825 if( rc!=SQLITE_OK ){
danielk1977a7a8e142008-02-13 18:25:27 +0000826 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000827 }
828 }
danielk1977a7a8e142008-02-13 18:25:27 +0000829 pMem->n = amt;
drhd5788202004-05-28 08:21:05 +0000830
danielk1977a7a8e142008-02-13 18:25:27 +0000831 return rc;
drhd5788202004-05-28 08:21:05 +0000832}
833
drh820a9062008-01-31 13:35:48 +0000834#if 0
danielk1977c572ef72004-05-27 09:28:41 +0000835/*
836** Perform various checks on the memory cell pMem. An assert() will
837** fail if pMem is internally inconsistent.
838*/
drh74161702006-02-24 02:53:49 +0000839void sqlite3VdbeMemSanity(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000840 int flags = pMem->flags;
841 assert( flags!=0 ); /* Must define some type */
drhb026e052007-05-02 01:34:31 +0000842 if( flags & (MEM_Str|MEM_Blob) ){
843 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
danielk1977c572ef72004-05-27 09:28:41 +0000844 assert( x!=0 ); /* Strings must define a string subtype */
845 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
drhfdf972a2007-05-02 13:30:27 +0000846 assert( pMem->z!=0 ); /* Strings must have a value */
danielk1977c572ef72004-05-27 09:28:41 +0000847 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
drhb026e052007-05-02 01:34:31 +0000848 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
849 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
drh22276bd2004-06-22 22:54:22 +0000850 /* No destructor unless there is MEM_Dyn */
851 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
danielk1977c572ef72004-05-27 09:28:41 +0000852
853 if( (flags & MEM_Str) ){
danielk1977dc8453f2004-06-12 00:42:34 +0000854 assert( pMem->enc==SQLITE_UTF8 ||
855 pMem->enc==SQLITE_UTF16BE ||
856 pMem->enc==SQLITE_UTF16LE
danielk1977c572ef72004-05-27 09:28:41 +0000857 );
858 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
drhee696e22004-08-30 16:52:17 +0000859 ** must be the length of the string. (Later:) If the database file
860 ** has been corrupted, '\000' characters might have been inserted
861 ** into the middle of the string. In that case, the strlen() might
862 ** be less.
danielk1977c572ef72004-05-27 09:28:41 +0000863 */
danielk1977dc8453f2004-06-12 00:42:34 +0000864 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
drhee696e22004-08-30 16:52:17 +0000865 assert( strlen(pMem->z)<=pMem->n );
866 assert( pMem->z[pMem->n]==0 );
danielk1977c572ef72004-05-27 09:28:41 +0000867 }
868 }
869 }else{
870 /* Cannot define a string subtype for non-string objects */
871 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
drh22276bd2004-06-22 22:54:22 +0000872 assert( pMem->xDel==0 );
danielk1977c572ef72004-05-27 09:28:41 +0000873 }
874 /* MEM_Null excludes all other types */
875 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
876 || (pMem->flags&MEM_Null)==0 );
drhf0bce092005-08-20 13:47:41 +0000877 /* If the MEM is both real and integer, the values are equal */
878 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
drh3c024d62007-03-30 11:23:45 +0000879 || pMem->r==pMem->u.i );
danielk1977c572ef72004-05-27 09:28:41 +0000880}
881#endif
danielk19774e6af132004-06-10 14:01:08 +0000882
883/* This function is only available internally, it is not part of the
884** external API. It works in a similar way to sqlite3_value_text(),
885** except the data returned is in the encoding specified by the second
886** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
887** SQLITE_UTF8.
drh7d9bd4e2006-02-16 18:16:36 +0000888**
889** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
890** If that is the case, then the result must be aligned on an even byte
891** boundary.
danielk19774e6af132004-06-10 14:01:08 +0000892*/
drhb21c8cd2007-08-21 19:33:56 +0000893const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
danielk1977bfd6cce2004-06-18 04:24:54 +0000894 if( !pVal ) return 0;
drhb21c8cd2007-08-21 19:33:56 +0000895
896 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
drh7d9bd4e2006-02-16 18:16:36 +0000897 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
danielk1977bfd6cce2004-06-18 04:24:54 +0000898
danielk19774e6af132004-06-10 14:01:08 +0000899 if( pVal->flags&MEM_Null ){
danielk19774e6af132004-06-10 14:01:08 +0000900 return 0;
901 }
drhf1f6c582006-01-12 19:42:41 +0000902 assert( (MEM_Blob>>3) == MEM_Str );
903 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
drhb21c8cd2007-08-21 19:33:56 +0000904 expandBlob(pVal);
danielk19774e6af132004-06-10 14:01:08 +0000905 if( pVal->flags&MEM_Str ){
drhb21c8cd2007-08-21 19:33:56 +0000906 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
drh7209c692008-04-27 18:40:11 +0000907 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
drh7d9bd4e2006-02-16 18:16:36 +0000908 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
drhb21c8cd2007-08-21 19:33:56 +0000909 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
drh7d9bd4e2006-02-16 18:16:36 +0000910 return 0;
911 }
912 }
drhb21c8cd2007-08-21 19:33:56 +0000913 sqlite3VdbeMemNulTerminate(pVal);
drhf0313812006-09-04 15:53:53 +0000914 }else{
915 assert( (pVal->flags&MEM_Blob)==0 );
drhb21c8cd2007-08-21 19:33:56 +0000916 sqlite3VdbeMemStringify(pVal, enc);
drh7209c692008-04-27 18:40:11 +0000917 assert( 0==(1&(int)pVal->z) );
danielk19774e6af132004-06-10 14:01:08 +0000918 }
drhb21c8cd2007-08-21 19:33:56 +0000919 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
920 || pVal->db->mallocFailed );
drh7d9bd4e2006-02-16 18:16:36 +0000921 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
922 return pVal->z;
923 }else{
924 return 0;
925 }
danielk19774e6af132004-06-10 14:01:08 +0000926}
927
drh6a6124e2004-06-27 01:56:33 +0000928/*
929** Create a new sqlite3_value object.
930*/
drh17435752007-08-16 04:30:38 +0000931sqlite3_value *sqlite3ValueNew(sqlite3 *db){
danielk197726783a52007-08-29 14:06:22 +0000932 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
danielk19774e6af132004-06-10 14:01:08 +0000933 if( p ){
934 p->flags = MEM_Null;
935 p->type = SQLITE_NULL;
drhb21c8cd2007-08-21 19:33:56 +0000936 p->db = db;
danielk19774e6af132004-06-10 14:01:08 +0000937 }
938 return p;
939}
940
drh6a6124e2004-06-27 01:56:33 +0000941/*
danielk1977aee18ef2005-03-09 12:26:50 +0000942** Create a new sqlite3_value object, containing the value of pExpr.
943**
944** This only works for very simple expressions that consist of one constant
drhc4dd3fd2008-01-22 01:48:05 +0000945** token (i.e. "5", "5.1", "'a string'"). If the expression can
danielk1977aee18ef2005-03-09 12:26:50 +0000946** be converted directly into a value, then the value is allocated and
947** a pointer written to *ppVal. The caller is responsible for deallocating
948** the value by passing it to sqlite3ValueFree() later on. If the expression
949** cannot be converted to a value, then *ppVal is set to NULL.
950*/
951int sqlite3ValueFromExpr(
drhb21c8cd2007-08-21 19:33:56 +0000952 sqlite3 *db, /* The database connection */
drh17435752007-08-16 04:30:38 +0000953 Expr *pExpr, /* The expression to evaluate */
954 u8 enc, /* Encoding to use */
955 u8 affinity, /* Affinity to use */
956 sqlite3_value **ppVal /* Write the new value here */
danielk1977aee18ef2005-03-09 12:26:50 +0000957){
958 int op;
959 char *zVal = 0;
960 sqlite3_value *pVal = 0;
961
962 if( !pExpr ){
963 *ppVal = 0;
964 return SQLITE_OK;
965 }
966 op = pExpr->op;
967
968 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
drh17435752007-08-16 04:30:38 +0000969 zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
970 pVal = sqlite3ValueNew(db);
danielk1977aee18ef2005-03-09 12:26:50 +0000971 if( !zVal || !pVal ) goto no_mem;
972 sqlite3Dequote(zVal);
drhb21c8cd2007-08-21 19:33:56 +0000973 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
danielk1977aee18ef2005-03-09 12:26:50 +0000974 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
drhb21c8cd2007-08-21 19:33:56 +0000975 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000976 }else{
drhb21c8cd2007-08-21 19:33:56 +0000977 sqlite3ValueApplyAffinity(pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000978 }
979 }else if( op==TK_UMINUS ) {
drhb21c8cd2007-08-21 19:33:56 +0000980 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
drh3c024d62007-03-30 11:23:45 +0000981 pVal->u.i = -1 * pVal->u.i;
danielk1977aee18ef2005-03-09 12:26:50 +0000982 pVal->r = -1.0 * pVal->r;
983 }
984 }
985#ifndef SQLITE_OMIT_BLOB_LITERAL
986 else if( op==TK_BLOB ){
987 int nVal;
drhca48c902008-01-18 14:08:24 +0000988 assert( pExpr->token.n>=3 );
989 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
990 assert( pExpr->token.z[1]=='\'' );
991 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
danielk19771e536952007-08-16 10:09:01 +0000992 pVal = sqlite3ValueNew(db);
drhca48c902008-01-18 14:08:24 +0000993 nVal = pExpr->token.n - 3;
994 zVal = (char*)pExpr->token.z + 2;
995 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
996 0, sqlite3_free);
danielk1977aee18ef2005-03-09 12:26:50 +0000997 }
998#endif
999
1000 *ppVal = pVal;
1001 return SQLITE_OK;
1002
1003no_mem:
drh17435752007-08-16 04:30:38 +00001004 db->mallocFailed = 1;
1005 sqlite3_free(zVal);
danielk1977aee18ef2005-03-09 12:26:50 +00001006 sqlite3ValueFree(pVal);
1007 *ppVal = 0;
1008 return SQLITE_NOMEM;
1009}
1010
1011/*
drh6a6124e2004-06-27 01:56:33 +00001012** Change the string value of an sqlite3_value object
1013*/
danielk1977bfd6cce2004-06-18 04:24:54 +00001014void sqlite3ValueSetStr(
drh17435752007-08-16 04:30:38 +00001015 sqlite3_value *v, /* Value to be set */
1016 int n, /* Length of string z */
1017 const void *z, /* Text of the new string */
1018 u8 enc, /* Encoding to use */
1019 void (*xDel)(void*) /* Destructor for the string */
danielk1977bfd6cce2004-06-18 04:24:54 +00001020){
drhb21c8cd2007-08-21 19:33:56 +00001021 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
danielk19774e6af132004-06-10 14:01:08 +00001022}
1023
drh6a6124e2004-06-27 01:56:33 +00001024/*
1025** Free an sqlite3_value object
1026*/
danielk19774e6af132004-06-10 14:01:08 +00001027void sqlite3ValueFree(sqlite3_value *v){
danielk1977bfd6cce2004-06-18 04:24:54 +00001028 if( !v ) return;
danielk1977a7a8e142008-02-13 18:25:27 +00001029 sqlite3VdbeMemRelease((Mem *)v);
drh17435752007-08-16 04:30:38 +00001030 sqlite3_free(v);
danielk19774e6af132004-06-10 14:01:08 +00001031}
1032
drh6a6124e2004-06-27 01:56:33 +00001033/*
1034** Return the number of bytes in the sqlite3_value object assuming
1035** that it uses the encoding "enc"
1036*/
drhb21c8cd2007-08-21 19:33:56 +00001037int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
danielk19774e6af132004-06-10 14:01:08 +00001038 Mem *p = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +00001039 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
drhb026e052007-05-02 01:34:31 +00001040 if( p->flags & MEM_Zero ){
1041 return p->n+p->u.i;
1042 }else{
1043 return p->n;
1044 }
danielk19774e6af132004-06-10 14:01:08 +00001045 }
1046 return 0;
1047}