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