blob: 6be8d9909f9f96af464026dd4a99a20d965cd53c [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**
18** $Id: vdbemem.c,v 1.115 2008/05/16 04:51:55 danielk1977 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 */
591 int flags = 0; /* New value for pMem->flags */
592
drhb21c8cd2007-08-21 19:33:56 +0000593 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
danielk1977a7a8e142008-02-13 18:25:27 +0000594
595 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
drh4f26d6c2004-05-26 23:25:30 +0000596 if( !z ){
danielk1977a7a8e142008-02-13 18:25:27 +0000597 sqlite3VdbeMemSetNull(pMem);
drh4f26d6c2004-05-26 23:25:30 +0000598 return SQLITE_OK;
599 }
danielk1977a7a8e142008-02-13 18:25:27 +0000600
601 flags = (enc==0?MEM_Blob:MEM_Str);
602 if( nByte<0 ){
603 assert( enc!=0 );
drh8fd38972008-02-19 15:44:09 +0000604 if( enc==SQLITE_UTF8 ){
605 for(nByte=0; z[nByte]; nByte++){}
606 }else{
607 for(nByte=0; z[nByte] | z[nByte+1]; nByte+=2){}
608 }
danielk1977a7a8e142008-02-13 18:25:27 +0000609 flags |= MEM_Term;
drh4f26d6c2004-05-26 23:25:30 +0000610 }
danielk1977d8123362004-06-12 09:25:12 +0000611
danielk1977a7a8e142008-02-13 18:25:27 +0000612 /* The following block sets the new values of Mem.z and Mem.xDel. It
613 ** also sets a flag in local variable "flags" to indicate the memory
614 ** management (one of MEM_Dyn or MEM_Static).
615 */
616 if( xDel==SQLITE_TRANSIENT ){
617 int nAlloc = nByte;
618 if( flags&MEM_Term ){
619 nAlloc += (enc==SQLITE_UTF8?1:2);
620 }
621 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
622 return SQLITE_NOMEM;
623 }
624 memcpy(pMem->z, z, nAlloc);
danielk1977a7a8e142008-02-13 18:25:27 +0000625 }else{
626 sqlite3VdbeMemRelease(pMem);
627 pMem->z = (char *)z;
628 pMem->xDel = xDel;
629 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
630 }
danielk1977d8123362004-06-12 09:25:12 +0000631
danielk1977a7a8e142008-02-13 18:25:27 +0000632 pMem->n = nByte;
633 pMem->flags = flags;
634 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
635 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
drh4f26d6c2004-05-26 23:25:30 +0000636
drh6c626082004-11-14 21:56:29 +0000637#ifndef SQLITE_OMIT_UTF16
danielk1977a7a8e142008-02-13 18:25:27 +0000638 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
639 return SQLITE_NOMEM;
drh4f26d6c2004-05-26 23:25:30 +0000640 }
danielk1977a7a8e142008-02-13 18:25:27 +0000641#endif
642
drhf4479502004-05-27 03:12:53 +0000643 return SQLITE_OK;
drh4f26d6c2004-05-26 23:25:30 +0000644}
645
646/*
647** Compare the values contained by the two memory cells, returning
648** negative, zero or positive if pMem1 is less than, equal to, or greater
649** than pMem2. Sorting order is NULL's first, followed by numbers (integers
650** and reals) sorted numerically, followed by text ordered by the collating
651** sequence pColl and finally blob's ordered by memcmp().
652**
653** Two NULL values are considered equal by this function.
654*/
655int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
656 int rc;
657 int f1, f2;
658 int combined_flags;
659
660 /* Interchange pMem1 and pMem2 if the collating sequence specifies
661 ** DESC order.
662 */
663 f1 = pMem1->flags;
664 f2 = pMem2->flags;
665 combined_flags = f1|f2;
666
667 /* If one value is NULL, it is less than the other. If both values
668 ** are NULL, return 0.
669 */
670 if( combined_flags&MEM_Null ){
671 return (f2&MEM_Null) - (f1&MEM_Null);
672 }
673
674 /* If one value is a number and the other is not, the number is less.
675 ** If both are numbers, compare as reals if one is a real, or as integers
676 ** if both values are integers.
677 */
678 if( combined_flags&(MEM_Int|MEM_Real) ){
679 if( !(f1&(MEM_Int|MEM_Real)) ){
680 return 1;
681 }
682 if( !(f2&(MEM_Int|MEM_Real)) ){
683 return -1;
684 }
685 if( (f1 & f2 & MEM_Int)==0 ){
686 double r1, r2;
687 if( (f1&MEM_Real)==0 ){
drh3c024d62007-03-30 11:23:45 +0000688 r1 = pMem1->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000689 }else{
690 r1 = pMem1->r;
691 }
692 if( (f2&MEM_Real)==0 ){
drh3c024d62007-03-30 11:23:45 +0000693 r2 = pMem2->u.i;
drh4f26d6c2004-05-26 23:25:30 +0000694 }else{
695 r2 = pMem2->r;
696 }
697 if( r1<r2 ) return -1;
698 if( r1>r2 ) return 1;
699 return 0;
700 }else{
701 assert( f1&MEM_Int );
702 assert( f2&MEM_Int );
drh3c024d62007-03-30 11:23:45 +0000703 if( pMem1->u.i < pMem2->u.i ) return -1;
704 if( pMem1->u.i > pMem2->u.i ) return 1;
drh4f26d6c2004-05-26 23:25:30 +0000705 return 0;
706 }
707 }
708
709 /* If one value is a string and the other is a blob, the string is less.
710 ** If both are strings, compare using the collating functions.
711 */
712 if( combined_flags&MEM_Str ){
713 if( (f1 & MEM_Str)==0 ){
714 return 1;
715 }
716 if( (f2 & MEM_Str)==0 ){
717 return -1;
718 }
danielk19770202b292004-06-09 09:55:16 +0000719
720 assert( pMem1->enc==pMem2->enc );
danielk1977dc8453f2004-06-12 00:42:34 +0000721 assert( pMem1->enc==SQLITE_UTF8 ||
722 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
danielk19770202b292004-06-09 09:55:16 +0000723
danielk1977b3bf5562006-01-10 17:58:23 +0000724 /* The collation sequence must be defined at this point, even if
725 ** the user deletes the collation sequence after the vdbe program is
726 ** compiled (this was not always the case).
danielk19770202b292004-06-09 09:55:16 +0000727 */
danielk1977466be562004-06-10 02:16:01 +0000728 assert( !pColl || pColl->xCmp );
danielk19770202b292004-06-09 09:55:16 +0000729
730 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000731 if( pMem1->enc==pColl->enc ){
drh7d9bd4e2006-02-16 18:16:36 +0000732 /* The strings are already in the correct encoding. Call the
733 ** comparison function directly */
danielk1977466be562004-06-10 02:16:01 +0000734 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
danielk19770202b292004-06-09 09:55:16 +0000735 }else{
danielk1977f4618892004-06-28 13:09:11 +0000736 u8 origEnc = pMem1->enc;
drh7d9bd4e2006-02-16 18:16:36 +0000737 const void *v1, *v2;
738 int n1, n2;
739 /* Convert the strings into the encoding that the comparison
740 ** function expects */
drhb21c8cd2007-08-21 19:33:56 +0000741 v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
drh7d9bd4e2006-02-16 18:16:36 +0000742 n1 = v1==0 ? 0 : pMem1->n;
drhb21c8cd2007-08-21 19:33:56 +0000743 assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
744 v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
drh7d9bd4e2006-02-16 18:16:36 +0000745 n2 = v2==0 ? 0 : pMem2->n;
drhb21c8cd2007-08-21 19:33:56 +0000746 assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
drh7d9bd4e2006-02-16 18:16:36 +0000747 /* Do the comparison */
748 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
749 /* Convert the strings back into the database encoding */
drhb21c8cd2007-08-21 19:33:56 +0000750 sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
751 sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
danielk1977f4618892004-06-28 13:09:11 +0000752 return rc;
danielk19770202b292004-06-09 09:55:16 +0000753 }
drh4f26d6c2004-05-26 23:25:30 +0000754 }
danielk19770202b292004-06-09 09:55:16 +0000755 /* If a NULL pointer was passed as the collate function, fall through
danielk19774e6af132004-06-10 14:01:08 +0000756 ** to the blob case and use memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000757 }
758
danielk19774e6af132004-06-10 14:01:08 +0000759 /* Both values must be blobs. Compare using memcmp(). */
drh4f26d6c2004-05-26 23:25:30 +0000760 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
761 if( rc==0 ){
762 rc = pMem1->n - pMem2->n;
763 }
764 return rc;
765}
danielk1977c572ef72004-05-27 09:28:41 +0000766
drhd5788202004-05-28 08:21:05 +0000767/*
768** Move data out of a btree key or data field and into a Mem structure.
769** The data or key is taken from the entry that pCur is currently pointing
770** to. offset and amt determine what portion of the data or key to retrieve.
771** key is true to get the key or false to get data. The result is written
772** into the pMem element.
773**
774** The pMem structure is assumed to be uninitialized. Any prior content
775** is overwritten without being freed.
776**
777** If this routine fails for any reason (malloc returns NULL or unable
778** to read from the disk) then the pMem is left in an inconsistent state.
779*/
780int sqlite3VdbeMemFromBtree(
781 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
782 int offset, /* Offset from the start of data to return bytes from. */
783 int amt, /* Number of bytes to return. */
784 int key, /* If true, retrieve from the btree key, not data. */
785 Mem *pMem /* OUT: Return data in this Mem structure. */
786){
drh61fc5952007-04-01 23:49:51 +0000787 char *zData; /* Data from the btree layer */
788 int available = 0; /* Number of bytes available on the local btree page */
drhb21c8cd2007-08-21 19:33:56 +0000789 sqlite3 *db; /* Database connection */
danielk1977a7a8e142008-02-13 18:25:27 +0000790 int rc = SQLITE_OK;
drhd5788202004-05-28 08:21:05 +0000791
drhb21c8cd2007-08-21 19:33:56 +0000792 db = sqlite3BtreeCursorDb(pCur);
793 assert( sqlite3_mutex_held(db->mutex) );
drhd5788202004-05-28 08:21:05 +0000794 if( key ){
drhe51c44f2004-05-30 20:46:09 +0000795 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000796 }else{
drhe51c44f2004-05-30 20:46:09 +0000797 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
drhd5788202004-05-28 08:21:05 +0000798 }
drh61fc5952007-04-01 23:49:51 +0000799 assert( zData!=0 );
drhd5788202004-05-28 08:21:05 +0000800
danielk1977a7a8e142008-02-13 18:25:27 +0000801 if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
802 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000803 pMem->z = &zData[offset];
804 pMem->flags = MEM_Blob|MEM_Ephem;
danielk1977a7a8e142008-02-13 18:25:27 +0000805 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
806 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
drhd5788202004-05-28 08:21:05 +0000807 pMem->enc = 0;
drh9c054832004-05-31 18:51:57 +0000808 pMem->type = SQLITE_BLOB;
drhd5788202004-05-28 08:21:05 +0000809 if( key ){
danielk1977a7a8e142008-02-13 18:25:27 +0000810 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000811 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000812 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
drhd5788202004-05-28 08:21:05 +0000813 }
danielk1977a7a8e142008-02-13 18:25:27 +0000814 pMem->z[amt] = 0;
815 pMem->z[amt+1] = 0;
drhd5788202004-05-28 08:21:05 +0000816 if( rc!=SQLITE_OK ){
danielk1977a7a8e142008-02-13 18:25:27 +0000817 sqlite3VdbeMemRelease(pMem);
drhd5788202004-05-28 08:21:05 +0000818 }
819 }
danielk1977a7a8e142008-02-13 18:25:27 +0000820 pMem->n = amt;
drhd5788202004-05-28 08:21:05 +0000821
danielk1977a7a8e142008-02-13 18:25:27 +0000822 return rc;
drhd5788202004-05-28 08:21:05 +0000823}
824
drh820a9062008-01-31 13:35:48 +0000825#if 0
danielk1977c572ef72004-05-27 09:28:41 +0000826/*
827** Perform various checks on the memory cell pMem. An assert() will
828** fail if pMem is internally inconsistent.
829*/
drh74161702006-02-24 02:53:49 +0000830void sqlite3VdbeMemSanity(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000831 int flags = pMem->flags;
832 assert( flags!=0 ); /* Must define some type */
drhb026e052007-05-02 01:34:31 +0000833 if( flags & (MEM_Str|MEM_Blob) ){
834 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
danielk1977c572ef72004-05-27 09:28:41 +0000835 assert( x!=0 ); /* Strings must define a string subtype */
836 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
drhfdf972a2007-05-02 13:30:27 +0000837 assert( pMem->z!=0 ); /* Strings must have a value */
danielk1977c572ef72004-05-27 09:28:41 +0000838 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
drhb026e052007-05-02 01:34:31 +0000839 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
840 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
drh22276bd2004-06-22 22:54:22 +0000841 /* No destructor unless there is MEM_Dyn */
842 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
danielk1977c572ef72004-05-27 09:28:41 +0000843
844 if( (flags & MEM_Str) ){
danielk1977dc8453f2004-06-12 00:42:34 +0000845 assert( pMem->enc==SQLITE_UTF8 ||
846 pMem->enc==SQLITE_UTF16BE ||
847 pMem->enc==SQLITE_UTF16LE
danielk1977c572ef72004-05-27 09:28:41 +0000848 );
849 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
drhee696e22004-08-30 16:52:17 +0000850 ** must be the length of the string. (Later:) If the database file
851 ** has been corrupted, '\000' characters might have been inserted
852 ** into the middle of the string. In that case, the strlen() might
853 ** be less.
danielk1977c572ef72004-05-27 09:28:41 +0000854 */
danielk1977dc8453f2004-06-12 00:42:34 +0000855 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
drhee696e22004-08-30 16:52:17 +0000856 assert( strlen(pMem->z)<=pMem->n );
857 assert( pMem->z[pMem->n]==0 );
danielk1977c572ef72004-05-27 09:28:41 +0000858 }
859 }
860 }else{
861 /* Cannot define a string subtype for non-string objects */
862 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
drh22276bd2004-06-22 22:54:22 +0000863 assert( pMem->xDel==0 );
danielk1977c572ef72004-05-27 09:28:41 +0000864 }
865 /* MEM_Null excludes all other types */
866 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
867 || (pMem->flags&MEM_Null)==0 );
drhf0bce092005-08-20 13:47:41 +0000868 /* If the MEM is both real and integer, the values are equal */
869 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
drh3c024d62007-03-30 11:23:45 +0000870 || pMem->r==pMem->u.i );
danielk1977c572ef72004-05-27 09:28:41 +0000871}
872#endif
danielk19774e6af132004-06-10 14:01:08 +0000873
874/* This function is only available internally, it is not part of the
875** external API. It works in a similar way to sqlite3_value_text(),
876** except the data returned is in the encoding specified by the second
877** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
878** SQLITE_UTF8.
drh7d9bd4e2006-02-16 18:16:36 +0000879**
880** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
881** If that is the case, then the result must be aligned on an even byte
882** boundary.
danielk19774e6af132004-06-10 14:01:08 +0000883*/
drhb21c8cd2007-08-21 19:33:56 +0000884const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
danielk1977bfd6cce2004-06-18 04:24:54 +0000885 if( !pVal ) return 0;
drhb21c8cd2007-08-21 19:33:56 +0000886
887 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
drh7d9bd4e2006-02-16 18:16:36 +0000888 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
danielk1977bfd6cce2004-06-18 04:24:54 +0000889
danielk19774e6af132004-06-10 14:01:08 +0000890 if( pVal->flags&MEM_Null ){
danielk19774e6af132004-06-10 14:01:08 +0000891 return 0;
892 }
drhf1f6c582006-01-12 19:42:41 +0000893 assert( (MEM_Blob>>3) == MEM_Str );
894 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
drhb21c8cd2007-08-21 19:33:56 +0000895 expandBlob(pVal);
danielk19774e6af132004-06-10 14:01:08 +0000896 if( pVal->flags&MEM_Str ){
drhb21c8cd2007-08-21 19:33:56 +0000897 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
drh7209c692008-04-27 18:40:11 +0000898 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
drh7d9bd4e2006-02-16 18:16:36 +0000899 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
drhb21c8cd2007-08-21 19:33:56 +0000900 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
drh7d9bd4e2006-02-16 18:16:36 +0000901 return 0;
902 }
903 }
drhb21c8cd2007-08-21 19:33:56 +0000904 sqlite3VdbeMemNulTerminate(pVal);
drhf0313812006-09-04 15:53:53 +0000905 }else{
906 assert( (pVal->flags&MEM_Blob)==0 );
drhb21c8cd2007-08-21 19:33:56 +0000907 sqlite3VdbeMemStringify(pVal, enc);
drh7209c692008-04-27 18:40:11 +0000908 assert( 0==(1&(int)pVal->z) );
danielk19774e6af132004-06-10 14:01:08 +0000909 }
drhb21c8cd2007-08-21 19:33:56 +0000910 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
911 || pVal->db->mallocFailed );
drh7d9bd4e2006-02-16 18:16:36 +0000912 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
913 return pVal->z;
914 }else{
915 return 0;
916 }
danielk19774e6af132004-06-10 14:01:08 +0000917}
918
drh6a6124e2004-06-27 01:56:33 +0000919/*
920** Create a new sqlite3_value object.
921*/
drh17435752007-08-16 04:30:38 +0000922sqlite3_value *sqlite3ValueNew(sqlite3 *db){
danielk197726783a52007-08-29 14:06:22 +0000923 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
danielk19774e6af132004-06-10 14:01:08 +0000924 if( p ){
925 p->flags = MEM_Null;
926 p->type = SQLITE_NULL;
drhb21c8cd2007-08-21 19:33:56 +0000927 p->db = db;
danielk19774e6af132004-06-10 14:01:08 +0000928 }
929 return p;
930}
931
drh6a6124e2004-06-27 01:56:33 +0000932/*
danielk1977aee18ef2005-03-09 12:26:50 +0000933** Create a new sqlite3_value object, containing the value of pExpr.
934**
935** This only works for very simple expressions that consist of one constant
drhc4dd3fd2008-01-22 01:48:05 +0000936** token (i.e. "5", "5.1", "'a string'"). If the expression can
danielk1977aee18ef2005-03-09 12:26:50 +0000937** be converted directly into a value, then the value is allocated and
938** a pointer written to *ppVal. The caller is responsible for deallocating
939** the value by passing it to sqlite3ValueFree() later on. If the expression
940** cannot be converted to a value, then *ppVal is set to NULL.
941*/
942int sqlite3ValueFromExpr(
drhb21c8cd2007-08-21 19:33:56 +0000943 sqlite3 *db, /* The database connection */
drh17435752007-08-16 04:30:38 +0000944 Expr *pExpr, /* The expression to evaluate */
945 u8 enc, /* Encoding to use */
946 u8 affinity, /* Affinity to use */
947 sqlite3_value **ppVal /* Write the new value here */
danielk1977aee18ef2005-03-09 12:26:50 +0000948){
949 int op;
950 char *zVal = 0;
951 sqlite3_value *pVal = 0;
952
953 if( !pExpr ){
954 *ppVal = 0;
955 return SQLITE_OK;
956 }
957 op = pExpr->op;
958
959 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
drh17435752007-08-16 04:30:38 +0000960 zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
961 pVal = sqlite3ValueNew(db);
danielk1977aee18ef2005-03-09 12:26:50 +0000962 if( !zVal || !pVal ) goto no_mem;
963 sqlite3Dequote(zVal);
drhb21c8cd2007-08-21 19:33:56 +0000964 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
danielk1977aee18ef2005-03-09 12:26:50 +0000965 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
drhb21c8cd2007-08-21 19:33:56 +0000966 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000967 }else{
drhb21c8cd2007-08-21 19:33:56 +0000968 sqlite3ValueApplyAffinity(pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000969 }
970 }else if( op==TK_UMINUS ) {
drhb21c8cd2007-08-21 19:33:56 +0000971 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
drh3c024d62007-03-30 11:23:45 +0000972 pVal->u.i = -1 * pVal->u.i;
danielk1977aee18ef2005-03-09 12:26:50 +0000973 pVal->r = -1.0 * pVal->r;
974 }
975 }
976#ifndef SQLITE_OMIT_BLOB_LITERAL
977 else if( op==TK_BLOB ){
978 int nVal;
drhca48c902008-01-18 14:08:24 +0000979 assert( pExpr->token.n>=3 );
980 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
981 assert( pExpr->token.z[1]=='\'' );
982 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
danielk19771e536952007-08-16 10:09:01 +0000983 pVal = sqlite3ValueNew(db);
drhca48c902008-01-18 14:08:24 +0000984 nVal = pExpr->token.n - 3;
985 zVal = (char*)pExpr->token.z + 2;
986 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
987 0, sqlite3_free);
danielk1977aee18ef2005-03-09 12:26:50 +0000988 }
989#endif
990
991 *ppVal = pVal;
992 return SQLITE_OK;
993
994no_mem:
drh17435752007-08-16 04:30:38 +0000995 db->mallocFailed = 1;
996 sqlite3_free(zVal);
danielk1977aee18ef2005-03-09 12:26:50 +0000997 sqlite3ValueFree(pVal);
998 *ppVal = 0;
999 return SQLITE_NOMEM;
1000}
1001
1002/*
drh6a6124e2004-06-27 01:56:33 +00001003** Change the string value of an sqlite3_value object
1004*/
danielk1977bfd6cce2004-06-18 04:24:54 +00001005void sqlite3ValueSetStr(
drh17435752007-08-16 04:30:38 +00001006 sqlite3_value *v, /* Value to be set */
1007 int n, /* Length of string z */
1008 const void *z, /* Text of the new string */
1009 u8 enc, /* Encoding to use */
1010 void (*xDel)(void*) /* Destructor for the string */
danielk1977bfd6cce2004-06-18 04:24:54 +00001011){
drhb21c8cd2007-08-21 19:33:56 +00001012 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
danielk19774e6af132004-06-10 14:01:08 +00001013}
1014
drh6a6124e2004-06-27 01:56:33 +00001015/*
1016** Free an sqlite3_value object
1017*/
danielk19774e6af132004-06-10 14:01:08 +00001018void sqlite3ValueFree(sqlite3_value *v){
danielk1977bfd6cce2004-06-18 04:24:54 +00001019 if( !v ) return;
danielk1977a7a8e142008-02-13 18:25:27 +00001020 sqlite3VdbeMemRelease((Mem *)v);
drh17435752007-08-16 04:30:38 +00001021 sqlite3_free(v);
danielk19774e6af132004-06-10 14:01:08 +00001022}
1023
drh6a6124e2004-06-27 01:56:33 +00001024/*
1025** Return the number of bytes in the sqlite3_value object assuming
1026** that it uses the encoding "enc"
1027*/
drhb21c8cd2007-08-21 19:33:56 +00001028int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
danielk19774e6af132004-06-10 14:01:08 +00001029 Mem *p = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +00001030 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
drhb026e052007-05-02 01:34:31 +00001031 if( p->flags & MEM_Zero ){
1032 return p->n+p->u.i;
1033 }else{
1034 return p->n;
1035 }
danielk19774e6af132004-06-10 14:01:08 +00001036 }
1037 return 0;
1038}