Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/sqlite/src/src/vdbemem.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||||||||
2 | ** 2004 May 26 | - | ||||||||||||||||||
3 | ** | - | ||||||||||||||||||
4 | ** The author disclaims copyright to this source code. In place of | - | ||||||||||||||||||
5 | ** a legal notice, here is a blessing: | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** May you do good and not evil. | - | ||||||||||||||||||
8 | ** May you find forgiveness for yourself and forgive others. | - | ||||||||||||||||||
9 | ** May you share freely, never taking more than you give. | - | ||||||||||||||||||
10 | ** | - | ||||||||||||||||||
11 | ************************************************************************* | - | ||||||||||||||||||
12 | ** | - | ||||||||||||||||||
13 | ** This file contains code use to manipulate "Mem" structure. A "Mem" | - | ||||||||||||||||||
14 | ** stores a single value in the VDBE. Mem is an opaque structure visible | - | ||||||||||||||||||
15 | ** only within the VDBE. Interface routines refer to a Mem using the | - | ||||||||||||||||||
16 | ** name sqlite_value | - | ||||||||||||||||||
17 | */ | - | ||||||||||||||||||
18 | #include "sqliteInt.h" | - | ||||||||||||||||||
19 | #include "vdbeInt.h" | - | ||||||||||||||||||
20 | - | |||||||||||||||||||
21 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
22 | /* | - | ||||||||||||||||||
23 | ** Check invariants on a Mem object. | - | ||||||||||||||||||
24 | ** | - | ||||||||||||||||||
25 | ** This routine is intended for use inside of assert() statements, like | - | ||||||||||||||||||
26 | ** this: assert( sqlite3VdbeCheckMemInvariants(pMem) ); | - | ||||||||||||||||||
27 | */ | - | ||||||||||||||||||
28 | int sqlite3VdbeCheckMemInvariants(Mem *p){ | - | ||||||||||||||||||
29 | /* If MEM_Dyn is set then Mem.xDel!=0. | - | ||||||||||||||||||
30 | ** Mem.xDel might not be initialized if MEM_Dyn is clear. | - | ||||||||||||||||||
31 | */ | - | ||||||||||||||||||
32 | assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we | - | ||||||||||||||||||
35 | ** ensure that if Mem.szMalloc>0 then it is safe to do | - | ||||||||||||||||||
36 | ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. | - | ||||||||||||||||||
37 | ** That saves a few cycles in inner loops. */ | - | ||||||||||||||||||
38 | assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | /* Cannot be both MEM_Int and MEM_Real at the same time */ | - | ||||||||||||||||||
41 | assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); | - | ||||||||||||||||||
42 | - | |||||||||||||||||||
43 | if( p->flags & MEM_Null ){ | - | ||||||||||||||||||
44 | /* Cannot be both MEM_Null and some other type */ | - | ||||||||||||||||||
45 | assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 ); | - | ||||||||||||||||||
46 | - | |||||||||||||||||||
47 | /* If MEM_Null is set, then either the value is a pure NULL (the usual | - | ||||||||||||||||||
48 | ** case) or it is a pointer set using sqlite3_bind_pointer() or | - | ||||||||||||||||||
49 | ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be | - | ||||||||||||||||||
50 | ** set. | - | ||||||||||||||||||
51 | */ | - | ||||||||||||||||||
52 | if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){ | - | ||||||||||||||||||
53 | /* This is a pointer type. There may be a flag to indicate what to | - | ||||||||||||||||||
54 | ** do with the pointer. */ | - | ||||||||||||||||||
55 | assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + | - | ||||||||||||||||||
56 | ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + | - | ||||||||||||||||||
57 | ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 ); | - | ||||||||||||||||||
58 | - | |||||||||||||||||||
59 | /* No other bits set */ | - | ||||||||||||||||||
60 | assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype | - | ||||||||||||||||||
61 | |MEM_Dyn|MEM_Ephem|MEM_Static))==0 ); | - | ||||||||||||||||||
62 | }else{ | - | ||||||||||||||||||
63 | /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn, | - | ||||||||||||||||||
64 | ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */ | - | ||||||||||||||||||
65 | } | - | ||||||||||||||||||
66 | }else{ | - | ||||||||||||||||||
67 | /* The MEM_Cleared bit is only allowed on NULLs */ | - | ||||||||||||||||||
68 | assert( (p->flags & MEM_Cleared)==0 ); | - | ||||||||||||||||||
69 | } | - | ||||||||||||||||||
70 | - | |||||||||||||||||||
71 | /* The szMalloc field holds the correct memory allocation size */ | - | ||||||||||||||||||
72 | assert( p->szMalloc==0 | - | ||||||||||||||||||
73 | || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) ); | - | ||||||||||||||||||
74 | - | |||||||||||||||||||
75 | /* If p holds a string or blob, the Mem.z must point to exactly | - | ||||||||||||||||||
76 | ** one of the following: | - | ||||||||||||||||||
77 | ** | - | ||||||||||||||||||
78 | ** (1) Memory in Mem.zMalloc and managed by the Mem object | - | ||||||||||||||||||
79 | ** (2) Memory to be freed using Mem.xDel | - | ||||||||||||||||||
80 | ** (3) An ephemeral string or blob | - | ||||||||||||||||||
81 | ** (4) A static string or blob | - | ||||||||||||||||||
82 | */ | - | ||||||||||||||||||
83 | if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){ | - | ||||||||||||||||||
84 | assert( | - | ||||||||||||||||||
85 | ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) + | - | ||||||||||||||||||
86 | ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + | - | ||||||||||||||||||
87 | ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + | - | ||||||||||||||||||
88 | ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1 | - | ||||||||||||||||||
89 | ); | - | ||||||||||||||||||
90 | } | - | ||||||||||||||||||
91 | return 1; | - | ||||||||||||||||||
92 | } | - | ||||||||||||||||||
93 | #endif | - | ||||||||||||||||||
94 | - | |||||||||||||||||||
95 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
96 | /* | - | ||||||||||||||||||
97 | ** Check that string value of pMem agrees with its integer or real value. | - | ||||||||||||||||||
98 | ** | - | ||||||||||||||||||
99 | ** A single int or real value always converts to the same strings. But | - | ||||||||||||||||||
100 | ** many different strings can be converted into the same int or real. | - | ||||||||||||||||||
101 | ** If a table contains a numeric value and an index is based on the | - | ||||||||||||||||||
102 | ** corresponding string value, then it is important that the string be | - | ||||||||||||||||||
103 | ** derived from the numeric value, not the other way around, to ensure | - | ||||||||||||||||||
104 | ** that the index and table are consistent. See ticket | - | ||||||||||||||||||
105 | ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for | - | ||||||||||||||||||
106 | ** an example. | - | ||||||||||||||||||
107 | ** | - | ||||||||||||||||||
108 | ** This routine looks at pMem to verify that if it has both a numeric | - | ||||||||||||||||||
109 | ** representation and a string representation then the string rep has | - | ||||||||||||||||||
110 | ** been derived from the numeric and not the other way around. It returns | - | ||||||||||||||||||
111 | ** true if everything is ok and false if there is a problem. | - | ||||||||||||||||||
112 | ** | - | ||||||||||||||||||
113 | ** This routine is for use inside of assert() statements only. | - | ||||||||||||||||||
114 | */ | - | ||||||||||||||||||
115 | int sqlite3VdbeMemConsistentDualRep(Mem *p){ | - | ||||||||||||||||||
116 | char zBuf[100]; | - | ||||||||||||||||||
117 | char *z; | - | ||||||||||||||||||
118 | int i, j, incr; | - | ||||||||||||||||||
119 | if( (p->flags & MEM_Str)==0 ) return 1; | - | ||||||||||||||||||
120 | if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1; | - | ||||||||||||||||||
121 | if( p->flags & MEM_Int ){ | - | ||||||||||||||||||
122 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i); | - | ||||||||||||||||||
123 | }else{ | - | ||||||||||||||||||
124 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r); | - | ||||||||||||||||||
125 | } | - | ||||||||||||||||||
126 | z = p->z; | - | ||||||||||||||||||
127 | i = j = 0; | - | ||||||||||||||||||
128 | incr = 1; | - | ||||||||||||||||||
129 | if( p->enc!=SQLITE_UTF8 ){ | - | ||||||||||||||||||
130 | incr = 2; | - | ||||||||||||||||||
131 | if( p->enc==SQLITE_UTF16BE ) z++; | - | ||||||||||||||||||
132 | } | - | ||||||||||||||||||
133 | while( zBuf[j] ){ | - | ||||||||||||||||||
134 | if( zBuf[j++]!=z[i] ) return 0; | - | ||||||||||||||||||
135 | i += incr; | - | ||||||||||||||||||
136 | } | - | ||||||||||||||||||
137 | return 1; | - | ||||||||||||||||||
138 | } | - | ||||||||||||||||||
139 | #endif /* SQLITE_DEBUG */ | - | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | /* | - | ||||||||||||||||||
142 | ** If pMem is an object with a valid string representation, this routine | - | ||||||||||||||||||
143 | ** ensures the internal encoding for the string representation is | - | ||||||||||||||||||
144 | ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. | - | ||||||||||||||||||
145 | ** | - | ||||||||||||||||||
146 | ** If pMem is not a string object, or the encoding of the string | - | ||||||||||||||||||
147 | ** representation is already stored using the requested encoding, then this | - | ||||||||||||||||||
148 | ** routine is a no-op. | - | ||||||||||||||||||
149 | ** | - | ||||||||||||||||||
150 | ** SQLITE_OK is returned if the conversion is successful (or not required). | - | ||||||||||||||||||
151 | ** SQLITE_NOMEM may be returned if a malloc() fails during conversion | - | ||||||||||||||||||
152 | ** between formats. | - | ||||||||||||||||||
153 | */ | - | ||||||||||||||||||
154 | int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ | - | ||||||||||||||||||
155 | #ifndef SQLITE_OMIT_UTF16 | - | ||||||||||||||||||
156 | int rc; | - | ||||||||||||||||||
157 | #endif | - | ||||||||||||||||||
158 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
159 | assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE | - | ||||||||||||||||||
160 | || desiredEnc==SQLITE_UTF16BE ); | - | ||||||||||||||||||
161 | if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
| 4120-2876019 | ||||||||||||||||||
162 | return SQLITE_OK; executed 4310001 times by 434 tests: return 0; Executed by:
| 4310001 | ||||||||||||||||||
163 | } | - | ||||||||||||||||||
164 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
165 | #ifdef SQLITE_OMIT_UTF16 | - | ||||||||||||||||||
166 | return SQLITE_ERROR; | - | ||||||||||||||||||
167 | #else | - | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, | - | ||||||||||||||||||
170 | ** then the encoding of the value may not have changed. | - | ||||||||||||||||||
171 | */ | - | ||||||||||||||||||
172 | rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc); | - | ||||||||||||||||||
173 | assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); | - | ||||||||||||||||||
174 | assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); | - | ||||||||||||||||||
175 | assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); | - | ||||||||||||||||||
176 | return rc; executed 4120 times by 1 test: return rc; Executed by:
| 4120 | ||||||||||||||||||
177 | #endif | - | ||||||||||||||||||
178 | } | - | ||||||||||||||||||
179 | - | |||||||||||||||||||
180 | /* | - | ||||||||||||||||||
181 | ** Make sure pMem->z points to a writable allocation of at least | - | ||||||||||||||||||
182 | ** min(n,32) bytes. | - | ||||||||||||||||||
183 | ** | - | ||||||||||||||||||
184 | ** If the bPreserve argument is true, then copy of the content of | - | ||||||||||||||||||
185 | ** pMem->z into the new allocation. pMem must be either a string or | - | ||||||||||||||||||
186 | ** blob if bPreserve is true. If bPreserve is false, any prior content | - | ||||||||||||||||||
187 | ** in pMem->z is discarded. | - | ||||||||||||||||||
188 | */ | - | ||||||||||||||||||
189 | SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ | - | ||||||||||||||||||
190 | assert( sqlite3VdbeCheckMemInvariants(pMem) ); | - | ||||||||||||||||||
191 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
192 | testcase( pMem->db==0 ); | - | ||||||||||||||||||
193 | - | |||||||||||||||||||
194 | /* If the bPreserve flag is set to true, then the memory cell must already | - | ||||||||||||||||||
195 | ** contain a valid string or blob value. */ | - | ||||||||||||||||||
196 | assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) ); | - | ||||||||||||||||||
197 | testcase( bPreserve && pMem->z==0 ); | - | ||||||||||||||||||
198 | - | |||||||||||||||||||
199 | assert( pMem->szMalloc==0 | - | ||||||||||||||||||
200 | || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) ); | - | ||||||||||||||||||
201 | if( n<32 ) n = 32; executed 2436585 times by 434 tests: n = 32; Executed by:
| 2436585-3656289 | ||||||||||||||||||
202 | if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
| 9343-4080009 | ||||||||||||||||||
203 | pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); | - | ||||||||||||||||||
204 | bPreserve = 0; | - | ||||||||||||||||||
205 | }else{ executed 9343 times by 1 test: end of block Executed by:
| 9343 | ||||||||||||||||||
206 | if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); executed 2003522 times by 10 tests: sqlite3DbFreeNN(pMem->db, pMem->zMalloc); Executed by:
| 2003522-4080009 | ||||||||||||||||||
207 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); | - | ||||||||||||||||||
208 | } executed 6083531 times by 435 tests: end of block Executed by:
| 6083531 | ||||||||||||||||||
209 | if( pMem->zMalloc==0 ){
| 210-6092664 | ||||||||||||||||||
210 | sqlite3VdbeMemSetNull(pMem); | - | ||||||||||||||||||
211 | pMem->z = 0; | - | ||||||||||||||||||
212 | pMem->szMalloc = 0; | - | ||||||||||||||||||
213 | return SQLITE_NOMEM_BKPT; executed 210 times by 1 test: return 7; Executed by:
| 210 | ||||||||||||||||||
214 | }else{ | - | ||||||||||||||||||
215 | pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); | - | ||||||||||||||||||
216 | } executed 6092664 times by 435 tests: end of block Executed by:
| 6092664 | ||||||||||||||||||
217 | - | |||||||||||||||||||
218 | if( bPreserve && pMem->z ){
| 16533-4339314 | ||||||||||||||||||
219 | assert( pMem->z!=pMem->zMalloc ); | - | ||||||||||||||||||
220 | memcpy(pMem->zMalloc, pMem->z, pMem->n); | - | ||||||||||||||||||
221 | } executed 1736817 times by 1 test: end of block Executed by:
| 1736817 | ||||||||||||||||||
222 | if( (pMem->flags&MEM_Dyn)!=0 ){
| 14073-6078591 | ||||||||||||||||||
223 | assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); | - | ||||||||||||||||||
224 | pMem->xDel((void *)(pMem->z)); | - | ||||||||||||||||||
225 | } executed 14073 times by 1 test: end of block Executed by:
| 14073 | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | pMem->z = pMem->zMalloc; | - | ||||||||||||||||||
228 | pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static); | - | ||||||||||||||||||
229 | return SQLITE_OK; executed 6092664 times by 435 tests: return 0; Executed by:
| 6092664 | ||||||||||||||||||
230 | } | - | ||||||||||||||||||
231 | - | |||||||||||||||||||
232 | /* | - | ||||||||||||||||||
233 | ** Change the pMem->zMalloc allocation to be at least szNew bytes. | - | ||||||||||||||||||
234 | ** If pMem->zMalloc already meets or exceeds the requested size, this | - | ||||||||||||||||||
235 | ** routine is a no-op. | - | ||||||||||||||||||
236 | ** | - | ||||||||||||||||||
237 | ** Any prior string or blob content in the pMem object may be discarded. | - | ||||||||||||||||||
238 | ** The pMem->xDel destructor is called, if it exists. Though MEM_Str | - | ||||||||||||||||||
239 | ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null | - | ||||||||||||||||||
240 | ** values are preserved. | - | ||||||||||||||||||
241 | ** | - | ||||||||||||||||||
242 | ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) | - | ||||||||||||||||||
243 | ** if unable to complete the resizing. | - | ||||||||||||||||||
244 | */ | - | ||||||||||||||||||
245 | int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ | - | ||||||||||||||||||
246 | assert( szNew>0 ); | - | ||||||||||||||||||
247 | assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); | - | ||||||||||||||||||
248 | if( pMem->szMalloc<szNew ){
| 3789745-3951881 | ||||||||||||||||||
249 | return sqlite3VdbeMemGrow(pMem, szNew, 0); executed 3789745 times by 435 tests: return sqlite3VdbeMemGrow(pMem, szNew, 0); Executed by:
| 3789745 | ||||||||||||||||||
250 | } | - | ||||||||||||||||||
251 | assert( (pMem->flags & MEM_Dyn)==0 ); | - | ||||||||||||||||||
252 | pMem->z = pMem->zMalloc; | - | ||||||||||||||||||
253 | pMem->flags &= (MEM_Null|MEM_Int|MEM_Real); | - | ||||||||||||||||||
254 | return SQLITE_OK; executed 3951881 times by 402 tests: return 0; Executed by:
| 3951881 | ||||||||||||||||||
255 | } | - | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | /* | - | ||||||||||||||||||
258 | ** It is already known that pMem contains an unterminated string. | - | ||||||||||||||||||
259 | ** Add the zero terminator. | - | ||||||||||||||||||
260 | */ | - | ||||||||||||||||||
261 | static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ | - | ||||||||||||||||||
262 | if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
| 0-1745529 | ||||||||||||||||||
263 | return SQLITE_NOMEM_BKPT; never executed: return 7; | 0 | ||||||||||||||||||
264 | } | - | ||||||||||||||||||
265 | pMem->z[pMem->n] = 0; | - | ||||||||||||||||||
266 | pMem->z[pMem->n+1] = 0; | - | ||||||||||||||||||
267 | pMem->flags |= MEM_Term; | - | ||||||||||||||||||
268 | return SQLITE_OK; executed 1745529 times by 1 test: return 0; Executed by:
| 1745529 | ||||||||||||||||||
269 | } | - | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | /* | - | ||||||||||||||||||
272 | ** Change pMem so that its MEM_Str or MEM_Blob value is stored in | - | ||||||||||||||||||
273 | ** MEM.zMalloc, where it can be safely written. | - | ||||||||||||||||||
274 | ** | - | ||||||||||||||||||
275 | ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. | - | ||||||||||||||||||
276 | */ | - | ||||||||||||||||||
277 | int sqlite3VdbeMemMakeWriteable(Mem *pMem){ | - | ||||||||||||||||||
278 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
279 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
280 | if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
| 1738249-3368662 | ||||||||||||||||||
281 | if( ExpandBlob(pMem) ) return SQLITE_NOMEM; never executed: return 7;
| 0-1738249 | ||||||||||||||||||
282 | if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
| 16718-1690232 | ||||||||||||||||||
283 | int rc = vdbeMemAddTerminator(pMem); | - | ||||||||||||||||||
284 | if( rc ) return rc; never executed: return rc;
| 0-1721531 | ||||||||||||||||||
285 | } executed 1721531 times by 1 test: end of block Executed by:
| 1721531 | ||||||||||||||||||
286 | } executed 1738249 times by 1 test: end of block Executed by:
| 1738249 | ||||||||||||||||||
287 | pMem->flags &= ~MEM_Ephem; | - | ||||||||||||||||||
288 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
289 | pMem->pScopyFrom = 0; | - | ||||||||||||||||||
290 | #endif | - | ||||||||||||||||||
291 | - | |||||||||||||||||||
292 | return SQLITE_OK; executed 5106911 times by 32 tests: return 0; Executed by:
| 5106911 | ||||||||||||||||||
293 | } | - | ||||||||||||||||||
294 | - | |||||||||||||||||||
295 | /* | - | ||||||||||||||||||
296 | ** If the given Mem* has a zero-filled tail, turn it into an ordinary | - | ||||||||||||||||||
297 | ** blob stored in dynamically allocated space. | - | ||||||||||||||||||
298 | */ | - | ||||||||||||||||||
299 | #ifndef SQLITE_OMIT_INCRBLOB | - | ||||||||||||||||||
300 | int sqlite3VdbeMemExpandBlob(Mem *pMem){ | - | ||||||||||||||||||
301 | int nByte; | - | ||||||||||||||||||
302 | assert( pMem->flags & MEM_Zero ); | - | ||||||||||||||||||
303 | assert( pMem->flags&MEM_Blob ); | - | ||||||||||||||||||
304 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
305 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
306 | - | |||||||||||||||||||
307 | /* Set nByte to the number of bytes required to store the expanded blob. */ | - | ||||||||||||||||||
308 | nByte = pMem->n + pMem->u.nZero; | - | ||||||||||||||||||
309 | if( nByte<=0 ){
| 7-16534 | ||||||||||||||||||
310 | nByte = 1; | - | ||||||||||||||||||
311 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||
312 | if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
| 0-16541 | ||||||||||||||||||
313 | return SQLITE_NOMEM_BKPT; never executed: return 7; | 0 | ||||||||||||||||||
314 | } | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | memset(&pMem->z[pMem->n], 0, pMem->u.nZero); | - | ||||||||||||||||||
317 | pMem->n += pMem->u.nZero; | - | ||||||||||||||||||
318 | pMem->flags &= ~(MEM_Zero|MEM_Term); | - | ||||||||||||||||||
319 | return SQLITE_OK; executed 16541 times by 1 test: return 0; Executed by:
| 16541 | ||||||||||||||||||
320 | } | - | ||||||||||||||||||
321 | #endif | - | ||||||||||||||||||
322 | - | |||||||||||||||||||
323 | /* | - | ||||||||||||||||||
324 | ** Make sure the given Mem is \u0000 terminated. | - | ||||||||||||||||||
325 | */ | - | ||||||||||||||||||
326 | int sqlite3VdbeMemNulTerminate(Mem *pMem){ | - | ||||||||||||||||||
327 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
328 | testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) ); | - | ||||||||||||||||||
329 | testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 ); | - | ||||||||||||||||||
330 | if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
| 23998-6566308 | ||||||||||||||||||
331 | return SQLITE_OK; /* Nothing to do */ executed 6566308 times by 434 tests: return 0; Executed by:
| 6566308 | ||||||||||||||||||
332 | }else{ | - | ||||||||||||||||||
333 | return vdbeMemAddTerminator(pMem); executed 23998 times by 1 test: return vdbeMemAddTerminator(pMem); Executed by:
| 23998 | ||||||||||||||||||
334 | } | - | ||||||||||||||||||
335 | } | - | ||||||||||||||||||
336 | - | |||||||||||||||||||
337 | /* | - | ||||||||||||||||||
338 | ** Add MEM_Str to the set of representations for the given Mem. Numbers | - | ||||||||||||||||||
339 | ** are converted using sqlite3_snprintf(). Converting a BLOB to a string | - | ||||||||||||||||||
340 | ** is a no-op. | - | ||||||||||||||||||
341 | ** | - | ||||||||||||||||||
342 | ** Existing representations MEM_Int and MEM_Real are invalidated if | - | ||||||||||||||||||
343 | ** bForce is true but are retained if bForce is false. | - | ||||||||||||||||||
344 | ** | - | ||||||||||||||||||
345 | ** A MEM_Null value will never be passed to this function. This function is | - | ||||||||||||||||||
346 | ** used for converting values to text for returning to the user (i.e. via | - | ||||||||||||||||||
347 | ** sqlite3_value_text()), or for ensuring that values to be used as btree | - | ||||||||||||||||||
348 | ** keys are strings. In the former case a NULL pointer is returned the | - | ||||||||||||||||||
349 | ** user and the latter is an internal programming error. | - | ||||||||||||||||||
350 | */ | - | ||||||||||||||||||
351 | int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ | - | ||||||||||||||||||
352 | int fg = pMem->flags; | - | ||||||||||||||||||
353 | const int nByte = 32; | - | ||||||||||||||||||
354 | - | |||||||||||||||||||
355 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
356 | assert( !(fg&MEM_Zero) ); | - | ||||||||||||||||||
357 | assert( !(fg&(MEM_Str|MEM_Blob)) ); | - | ||||||||||||||||||
358 | assert( fg&(MEM_Int|MEM_Real) ); | - | ||||||||||||||||||
359 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
360 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | - | |||||||||||||||||||
363 | if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
| 17-385937 | ||||||||||||||||||
364 | pMem->enc = 0; | - | ||||||||||||||||||
365 | return SQLITE_NOMEM_BKPT; executed 17 times by 1 test: return 7; Executed by:
| 17 | ||||||||||||||||||
366 | } | - | ||||||||||||||||||
367 | - | |||||||||||||||||||
368 | /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 | - | ||||||||||||||||||
369 | ** string representation of the value. Then, if the required encoding | - | ||||||||||||||||||
370 | ** is UTF-16le or UTF-16be do a translation. | - | ||||||||||||||||||
371 | ** | - | ||||||||||||||||||
372 | ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. | - | ||||||||||||||||||
373 | */ | - | ||||||||||||||||||
374 | if( fg & MEM_Int ){
| 91980-293957 | ||||||||||||||||||
375 | sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); | - | ||||||||||||||||||
376 | }else{ executed 293957 times by 434 tests: end of block Executed by:
| 293957 | ||||||||||||||||||
377 | assert( fg & MEM_Real ); | - | ||||||||||||||||||
378 | sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); | - | ||||||||||||||||||
379 | } executed 91980 times by 1 test: end of block Executed by:
| 91980 | ||||||||||||||||||
380 | pMem->n = sqlite3Strlen30(pMem->z); | - | ||||||||||||||||||
381 | pMem->enc = SQLITE_UTF8; | - | ||||||||||||||||||
382 | pMem->flags |= MEM_Str|MEM_Term; | - | ||||||||||||||||||
383 | if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); executed 172768 times by 1 test: pMem->flags &= ~(0x0004|0x0008); Executed by:
| 172768-213169 | ||||||||||||||||||
384 | sqlite3VdbeChangeEncoding(pMem, enc); | - | ||||||||||||||||||
385 | return SQLITE_OK; executed 385937 times by 434 tests: return 0; Executed by:
| 385937 | ||||||||||||||||||
386 | } | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | /* | - | ||||||||||||||||||
389 | ** Memory cell pMem contains the context of an aggregate function. | - | ||||||||||||||||||
390 | ** This routine calls the finalize method for that function. The | - | ||||||||||||||||||
391 | ** result of the aggregate is stored back into pMem. | - | ||||||||||||||||||
392 | ** | - | ||||||||||||||||||
393 | ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK | - | ||||||||||||||||||
394 | ** otherwise. | - | ||||||||||||||||||
395 | */ | - | ||||||||||||||||||
396 | int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ | - | ||||||||||||||||||
397 | sqlite3_context ctx; | - | ||||||||||||||||||
398 | Mem t; | - | ||||||||||||||||||
399 | assert( pFunc!=0 ); | - | ||||||||||||||||||
400 | assert( pFunc->xFinalize!=0 ); | - | ||||||||||||||||||
401 | assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); | - | ||||||||||||||||||
402 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
403 | memset(&ctx, 0, sizeof(ctx)); | - | ||||||||||||||||||
404 | memset(&t, 0, sizeof(t)); | - | ||||||||||||||||||
405 | t.flags = MEM_Null; | - | ||||||||||||||||||
406 | t.db = pMem->db; | - | ||||||||||||||||||
407 | ctx.pOut = &t; | - | ||||||||||||||||||
408 | ctx.pMem = pMem; | - | ||||||||||||||||||
409 | ctx.pFunc = pFunc; | - | ||||||||||||||||||
410 | pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ | - | ||||||||||||||||||
411 | assert( (pMem->flags & MEM_Dyn)==0 ); | - | ||||||||||||||||||
412 | if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); executed 56198 times by 1 test: sqlite3DbFreeNN(pMem->db, pMem->zMalloc); Executed by:
| 2062-56198 | ||||||||||||||||||
413 | memcpy(pMem, &t, sizeof(t)); | - | ||||||||||||||||||
414 | return ctx.isError; executed 58260 times by 1 test: return ctx.isError; Executed by:
| 58260 | ||||||||||||||||||
415 | } | - | ||||||||||||||||||
416 | - | |||||||||||||||||||
417 | /* | - | ||||||||||||||||||
418 | ** Memory cell pAccum contains the context of an aggregate function. | - | ||||||||||||||||||
419 | ** This routine calls the xValue method for that function and stores | - | ||||||||||||||||||
420 | ** the results in memory cell pMem. | - | ||||||||||||||||||
421 | ** | - | ||||||||||||||||||
422 | ** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK | - | ||||||||||||||||||
423 | ** otherwise. | - | ||||||||||||||||||
424 | */ | - | ||||||||||||||||||
425 | #ifndef SQLITE_OMIT_WINDOWFUNC | - | ||||||||||||||||||
426 | int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){ | - | ||||||||||||||||||
427 | sqlite3_context ctx; | - | ||||||||||||||||||
428 | Mem t; | - | ||||||||||||||||||
429 | assert( pFunc!=0 ); | - | ||||||||||||||||||
430 | assert( pFunc->xValue!=0 ); | - | ||||||||||||||||||
431 | assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef ); | - | ||||||||||||||||||
432 | assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) ); | - | ||||||||||||||||||
433 | memset(&ctx, 0, sizeof(ctx)); | - | ||||||||||||||||||
434 | memset(&t, 0, sizeof(t)); | - | ||||||||||||||||||
435 | t.flags = MEM_Null; | - | ||||||||||||||||||
436 | t.db = pAccum->db; | - | ||||||||||||||||||
437 | sqlite3VdbeMemSetNull(pOut); | - | ||||||||||||||||||
438 | ctx.pOut = pOut; | - | ||||||||||||||||||
439 | ctx.pMem = pAccum; | - | ||||||||||||||||||
440 | ctx.pFunc = pFunc; | - | ||||||||||||||||||
441 | pFunc->xValue(&ctx); | - | ||||||||||||||||||
442 | return ctx.isError; executed 191503 times by 1 test: return ctx.isError; Executed by:
| 191503 | ||||||||||||||||||
443 | } | - | ||||||||||||||||||
444 | #endif /* SQLITE_OMIT_WINDOWFUNC */ | - | ||||||||||||||||||
445 | - | |||||||||||||||||||
446 | /* | - | ||||||||||||||||||
447 | ** If the memory cell contains a value that must be freed by | - | ||||||||||||||||||
448 | ** invoking the external callback in Mem.xDel, then this routine | - | ||||||||||||||||||
449 | ** will free that value. It also sets Mem.flags to MEM_Null. | - | ||||||||||||||||||
450 | ** | - | ||||||||||||||||||
451 | ** This is a helper routine for sqlite3VdbeMemSetNull() and | - | ||||||||||||||||||
452 | ** for sqlite3VdbeMemRelease(). Use those other routines as the | - | ||||||||||||||||||
453 | ** entry point for releasing Mem resources. | - | ||||||||||||||||||
454 | */ | - | ||||||||||||||||||
455 | static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){ | - | ||||||||||||||||||
456 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); | - | ||||||||||||||||||
457 | assert( VdbeMemDynamic(p) ); | - | ||||||||||||||||||
458 | if( p->flags&MEM_Agg ){
| 13268-1354588 | ||||||||||||||||||
459 | sqlite3VdbeMemFinalize(p, p->u.pDef); | - | ||||||||||||||||||
460 | assert( (p->flags & MEM_Agg)==0 ); | - | ||||||||||||||||||
461 | testcase( p->flags & MEM_Dyn ); | - | ||||||||||||||||||
462 | } executed 13268 times by 1 test: end of block Executed by:
| 13268 | ||||||||||||||||||
463 | if( p->flags&MEM_Dyn ){
| 10675-1357181 | ||||||||||||||||||
464 | assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); | - | ||||||||||||||||||
465 | p->xDel((void *)p->z); | - | ||||||||||||||||||
466 | } executed 1357181 times by 348 tests: end of block Executed by:
| 1357181 | ||||||||||||||||||
467 | p->flags = MEM_Null; | - | ||||||||||||||||||
468 | } executed 1367856 times by 348 tests: end of block Executed by:
| 1367856 | ||||||||||||||||||
469 | - | |||||||||||||||||||
470 | /* | - | ||||||||||||||||||
471 | ** Release memory held by the Mem p, both external memory cleared | - | ||||||||||||||||||
472 | ** by p->xDel and memory in p->zMalloc. | - | ||||||||||||||||||
473 | ** | - | ||||||||||||||||||
474 | ** This is a helper routine invoked by sqlite3VdbeMemRelease() in | - | ||||||||||||||||||
475 | ** the unusual case where there really is memory in p that needs | - | ||||||||||||||||||
476 | ** to be freed. | - | ||||||||||||||||||
477 | */ | - | ||||||||||||||||||
478 | static SQLITE_NOINLINE void vdbeMemClear(Mem *p){ | - | ||||||||||||||||||
479 | if( VdbeMemDynamic(p) ){
| 214556-1350541 | ||||||||||||||||||
480 | vdbeMemClearExternAndSetNull(p); | - | ||||||||||||||||||
481 | } executed 1350541 times by 348 tests: end of block Executed by:
| 1350541 | ||||||||||||||||||
482 | if( p->szMalloc ){
| 217070-1348027 | ||||||||||||||||||
483 | sqlite3DbFreeNN(p->db, p->zMalloc); | - | ||||||||||||||||||
484 | p->szMalloc = 0; | - | ||||||||||||||||||
485 | } executed 217070 times by 22 tests: end of block Executed by:
| 217070 | ||||||||||||||||||
486 | p->z = 0; | - | ||||||||||||||||||
487 | } executed 1565097 times by 368 tests: end of block Executed by:
| 1565097 | ||||||||||||||||||
488 | - | |||||||||||||||||||
489 | /* | - | ||||||||||||||||||
490 | ** Release any memory resources held by the Mem. Both the memory that is | - | ||||||||||||||||||
491 | ** free by Mem.xDel and the Mem.zMalloc allocation are freed. | - | ||||||||||||||||||
492 | ** | - | ||||||||||||||||||
493 | ** Use this routine prior to clean up prior to abandoning a Mem, or to | - | ||||||||||||||||||
494 | ** reset a Mem back to its minimum memory utilization. | - | ||||||||||||||||||
495 | ** | - | ||||||||||||||||||
496 | ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space | - | ||||||||||||||||||
497 | ** prior to inserting new content into the Mem. | - | ||||||||||||||||||
498 | */ | - | ||||||||||||||||||
499 | void sqlite3VdbeMemRelease(Mem *p){ | - | ||||||||||||||||||
500 | assert( sqlite3VdbeCheckMemInvariants(p) ); | - | ||||||||||||||||||
501 | if( VdbeMemDynamic(p) || p->szMalloc ){
| 214556-4660966 | ||||||||||||||||||
502 | vdbeMemClear(p); | - | ||||||||||||||||||
503 | } executed 1565097 times by 368 tests: end of block Executed by:
| 1565097 | ||||||||||||||||||
504 | } executed 6011507 times by 408 tests: end of block Executed by:
| 6011507 | ||||||||||||||||||
505 | - | |||||||||||||||||||
506 | /* | - | ||||||||||||||||||
507 | ** Convert a 64-bit IEEE double into a 64-bit signed integer. | - | ||||||||||||||||||
508 | ** If the double is out of range of a 64-bit signed integer then | - | ||||||||||||||||||
509 | ** return the closest available 64-bit signed integer. | - | ||||||||||||||||||
510 | */ | - | ||||||||||||||||||
511 | static SQLITE_NOINLINE i64 doubleToInt64(double r){ | - | ||||||||||||||||||
512 | #ifdef SQLITE_OMIT_FLOATING_POINT | - | ||||||||||||||||||
513 | /* When floating-point is omitted, double and int64 are the same thing */ | - | ||||||||||||||||||
514 | return r; | - | ||||||||||||||||||
515 | #else | - | ||||||||||||||||||
516 | /* | - | ||||||||||||||||||
517 | ** Many compilers we encounter do not define constants for the | - | ||||||||||||||||||
518 | ** minimum and maximum 64-bit integers, or they define them | - | ||||||||||||||||||
519 | ** inconsistently. And many do not understand the "LL" notation. | - | ||||||||||||||||||
520 | ** So we define our own static constants here using nothing | - | ||||||||||||||||||
521 | ** larger than a 32-bit integer constant. | - | ||||||||||||||||||
522 | */ | - | ||||||||||||||||||
523 | static const i64 maxInt = LARGEST_INT64; | - | ||||||||||||||||||
524 | static const i64 minInt = SMALLEST_INT64; | - | ||||||||||||||||||
525 | - | |||||||||||||||||||
526 | if( r<=(double)minInt ){
| 288-71853 | ||||||||||||||||||
527 | return minInt; executed 288 times by 1 test: return minInt; Executed by:
| 288 | ||||||||||||||||||
528 | }else if( r>=(double)maxInt ){
| 304-71549 | ||||||||||||||||||
529 | return maxInt; executed 304 times by 1 test: return maxInt; Executed by:
| 304 | ||||||||||||||||||
530 | }else{ | - | ||||||||||||||||||
531 | return (i64)r; executed 71549 times by 4 tests: return (i64)r; Executed by:
| 71549 | ||||||||||||||||||
532 | } | - | ||||||||||||||||||
533 | #endif | - | ||||||||||||||||||
534 | } | - | ||||||||||||||||||
535 | - | |||||||||||||||||||
536 | /* | - | ||||||||||||||||||
537 | ** Return some kind of integer value which is the best we can do | - | ||||||||||||||||||
538 | ** at representing the value that *pMem describes as an integer. | - | ||||||||||||||||||
539 | ** If pMem is an integer, then the value is exact. If pMem is | - | ||||||||||||||||||
540 | ** a floating-point then the value returned is the integer part. | - | ||||||||||||||||||
541 | ** If pMem is a string or blob, then we make an attempt to convert | - | ||||||||||||||||||
542 | ** it into an integer and return that. If pMem represents an | - | ||||||||||||||||||
543 | ** an SQL-NULL value, return 0. | - | ||||||||||||||||||
544 | ** | - | ||||||||||||||||||
545 | ** If pMem represents a string value, its encoding might be changed. | - | ||||||||||||||||||
546 | */ | - | ||||||||||||||||||
547 | static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){ | - | ||||||||||||||||||
548 | i64 value = 0; | - | ||||||||||||||||||
549 | sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); | - | ||||||||||||||||||
550 | return value; executed 13943 times by 1 test: return value; Executed by:
| 13943 | ||||||||||||||||||
551 | } | - | ||||||||||||||||||
552 | i64 sqlite3VdbeIntValue(Mem *pMem){ | - | ||||||||||||||||||
553 | int flags; | - | ||||||||||||||||||
554 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
555 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
556 | flags = pMem->flags; | - | ||||||||||||||||||
557 | if( flags & MEM_Int ){
| 22103-8704897 | ||||||||||||||||||
558 | return pMem->u.i; executed 8704897 times by 420 tests: return pMem->u.i; Executed by:
| 8704897 | ||||||||||||||||||
559 | }else if( flags & MEM_Real ){
| 6421-15682 | ||||||||||||||||||
560 | return doubleToInt64(pMem->u.r); executed 6421 times by 1 test: return doubleToInt64(pMem->u.r); Executed by:
| 6421 | ||||||||||||||||||
561 | }else if( flags & (MEM_Str|MEM_Blob) ){
| 1739-13943 | ||||||||||||||||||
562 | assert( pMem->z || pMem->n==0 ); | - | ||||||||||||||||||
563 | return memIntValue(pMem); executed 13943 times by 1 test: return memIntValue(pMem); Executed by:
| 13943 | ||||||||||||||||||
564 | }else{ | - | ||||||||||||||||||
565 | return 0; executed 1739 times by 1 test: return 0; Executed by:
| 1739 | ||||||||||||||||||
566 | } | - | ||||||||||||||||||
567 | } | - | ||||||||||||||||||
568 | - | |||||||||||||||||||
569 | /* | - | ||||||||||||||||||
570 | ** Return the best representation of pMem that we can get into a | - | ||||||||||||||||||
571 | ** double. If pMem is already a double or an integer, return its | - | ||||||||||||||||||
572 | ** value. If it is a string or blob, try to convert it to a double. | - | ||||||||||||||||||
573 | ** If it is a NULL, return 0.0. | - | ||||||||||||||||||
574 | */ | - | ||||||||||||||||||
575 | static SQLITE_NOINLINE double memRealValue(Mem *pMem){ | - | ||||||||||||||||||
576 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ | - | ||||||||||||||||||
577 | double val = (double)0; | - | ||||||||||||||||||
578 | sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); | - | ||||||||||||||||||
579 | return val; executed 22450 times by 1 test: return val; Executed by:
| 22450 | ||||||||||||||||||
580 | } | - | ||||||||||||||||||
581 | double sqlite3VdbeRealValue(Mem *pMem){ | - | ||||||||||||||||||
582 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
583 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
584 | if( pMem->flags & MEM_Real ){
| 800628-12303192 | ||||||||||||||||||
585 | return pMem->u.r; executed 800628 times by 1 test: return pMem->u.r; Executed by:
| 800628 | ||||||||||||||||||
586 | }else if( pMem->flags & MEM_Int ){
| 22455-12280737 | ||||||||||||||||||
587 | return (double)pMem->u.i; executed 12280737 times by 1 test: return (double)pMem->u.i; Executed by:
| 12280737 | ||||||||||||||||||
588 | }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
| 5-22450 | ||||||||||||||||||
589 | return memRealValue(pMem); executed 22450 times by 1 test: return memRealValue(pMem); Executed by:
| 22450 | ||||||||||||||||||
590 | }else{ | - | ||||||||||||||||||
591 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ | - | ||||||||||||||||||
592 | return (double)0; executed 5 times by 1 test: return (double)0; Executed by:
| 5 | ||||||||||||||||||
593 | } | - | ||||||||||||||||||
594 | } | - | ||||||||||||||||||
595 | - | |||||||||||||||||||
596 | /* | - | ||||||||||||||||||
597 | ** Return 1 if pMem represents true, and return 0 if pMem represents false. | - | ||||||||||||||||||
598 | ** Return the value ifNull if pMem is NULL. | - | ||||||||||||||||||
599 | */ | - | ||||||||||||||||||
600 | int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){ | - | ||||||||||||||||||
601 | if( pMem->flags & MEM_Int ) return pMem->u.i!=0; executed 785767 times by 29 tests: return pMem->u.i!=0; Executed by:
| 2431-785767 | ||||||||||||||||||
602 | if( pMem->flags & MEM_Null ) return ifNull; executed 1603 times by 1 test: return ifNull; Executed by:
| 828-1603 | ||||||||||||||||||
603 | return sqlite3VdbeRealValue(pMem)!=0.0; executed 828 times by 1 test: return sqlite3VdbeRealValue(pMem)!=0.0; Executed by:
| 828 | ||||||||||||||||||
604 | } | - | ||||||||||||||||||
605 | - | |||||||||||||||||||
606 | /* | - | ||||||||||||||||||
607 | ** The MEM structure is already a MEM_Real. Try to also make it a | - | ||||||||||||||||||
608 | ** MEM_Int if we can. | - | ||||||||||||||||||
609 | */ | - | ||||||||||||||||||
610 | void sqlite3VdbeIntegerAffinity(Mem *pMem){ | - | ||||||||||||||||||
611 | i64 ix; | - | ||||||||||||||||||
612 | assert( pMem->flags & MEM_Real ); | - | ||||||||||||||||||
613 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
614 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
615 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
616 | - | |||||||||||||||||||
617 | ix = doubleToInt64(pMem->u.r); | - | ||||||||||||||||||
618 | - | |||||||||||||||||||
619 | /* Only mark the value as an integer if | - | ||||||||||||||||||
620 | ** | - | ||||||||||||||||||
621 | ** (1) the round-trip conversion real->int->real is a no-op, and | - | ||||||||||||||||||
622 | ** (2) The integer is neither the largest nor the smallest | - | ||||||||||||||||||
623 | ** possible integer (ticket #3922) | - | ||||||||||||||||||
624 | ** | - | ||||||||||||||||||
625 | ** The second and third terms in the following conditional enforces | - | ||||||||||||||||||
626 | ** the second condition under the assumption that addition overflow causes | - | ||||||||||||||||||
627 | ** values to wrap around. | - | ||||||||||||||||||
628 | */ | - | ||||||||||||||||||
629 | if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
| 4-64947 | ||||||||||||||||||
630 | pMem->u.i = ix; | - | ||||||||||||||||||
631 | MemSetTypeFlag(pMem, MEM_Int); | - | ||||||||||||||||||
632 | } executed 762 times by 1 test: end of block Executed by:
| 762 | ||||||||||||||||||
633 | } executed 65720 times by 4 tests: end of block Executed by:
| 65720 | ||||||||||||||||||
634 | - | |||||||||||||||||||
635 | /* | - | ||||||||||||||||||
636 | ** Convert pMem to type integer. Invalidate any prior representations. | - | ||||||||||||||||||
637 | */ | - | ||||||||||||||||||
638 | int sqlite3VdbeMemIntegerify(Mem *pMem){ | - | ||||||||||||||||||
639 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
640 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
641 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
642 | - | |||||||||||||||||||
643 | pMem->u.i = sqlite3VdbeIntValue(pMem); | - | ||||||||||||||||||
644 | MemSetTypeFlag(pMem, MEM_Int); | - | ||||||||||||||||||
645 | return SQLITE_OK; executed 2727601 times by 24 tests: return 0; Executed by:
| 2727601 | ||||||||||||||||||
646 | } | - | ||||||||||||||||||
647 | - | |||||||||||||||||||
648 | /* | - | ||||||||||||||||||
649 | ** Convert pMem so that it is of type MEM_Real. | - | ||||||||||||||||||
650 | ** Invalidate any prior representations. | - | ||||||||||||||||||
651 | */ | - | ||||||||||||||||||
652 | int sqlite3VdbeMemRealify(Mem *pMem){ | - | ||||||||||||||||||
653 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
654 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | - | ||||||||||||||||||
655 | - | |||||||||||||||||||
656 | pMem->u.r = sqlite3VdbeRealValue(pMem); | - | ||||||||||||||||||
657 | MemSetTypeFlag(pMem, MEM_Real); | - | ||||||||||||||||||
658 | return SQLITE_OK; executed 284707 times by 1 test: return 0; Executed by:
| 284707 | ||||||||||||||||||
659 | } | - | ||||||||||||||||||
660 | - | |||||||||||||||||||
661 | /* Compare a floating point value to an integer. Return true if the two | - | ||||||||||||||||||
662 | ** values are the same within the precision of the floating point value. | - | ||||||||||||||||||
663 | ** | - | ||||||||||||||||||
664 | ** For some versions of GCC on 32-bit machines, if you do the more obvious | - | ||||||||||||||||||
665 | ** comparison of "r1==(double)i" you sometimes get an answer of false even | - | ||||||||||||||||||
666 | ** though the r1 and (double)i values are bit-for-bit the same. | - | ||||||||||||||||||
667 | */ | - | ||||||||||||||||||
668 | static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){ | - | ||||||||||||||||||
669 | double r2 = (double)i; | - | ||||||||||||||||||
670 | return memcmp(&r1, &r2, sizeof(r1))==0; executed 31 times by 1 test: return memcmp(&r1, &r2, sizeof(r1))==0; Executed by:
| 31 | ||||||||||||||||||
671 | } | - | ||||||||||||||||||
672 | - | |||||||||||||||||||
673 | /* | - | ||||||||||||||||||
674 | ** Convert pMem so that it has types MEM_Real or MEM_Int or both. | - | ||||||||||||||||||
675 | ** Invalidate any prior representations. | - | ||||||||||||||||||
676 | ** | - | ||||||||||||||||||
677 | ** Every effort is made to force the conversion, even if the input | - | ||||||||||||||||||
678 | ** is a string that does not look completely like a number. Convert | - | ||||||||||||||||||
679 | ** as much of the string as we can and ignore the rest. | - | ||||||||||||||||||
680 | */ | - | ||||||||||||||||||
681 | int sqlite3VdbeMemNumerify(Mem *pMem){ | - | ||||||||||||||||||
682 | if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
| 28-58 | ||||||||||||||||||
683 | int rc; | - | ||||||||||||||||||
684 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); | - | ||||||||||||||||||
685 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
686 | rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc); | - | ||||||||||||||||||
687 | if( rc==0 ){
| 23-35 | ||||||||||||||||||
688 | MemSetTypeFlag(pMem, MEM_Int); | - | ||||||||||||||||||
689 | }else{ executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||||||||
690 | i64 i = pMem->u.i; | - | ||||||||||||||||||
691 | sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); | - | ||||||||||||||||||
692 | if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
| 4-31 | ||||||||||||||||||
693 | pMem->u.i = i; | - | ||||||||||||||||||
694 | MemSetTypeFlag(pMem, MEM_Int); | - | ||||||||||||||||||
695 | }else{ executed 25 times by 1 test: end of block Executed by:
| 25 | ||||||||||||||||||
696 | MemSetTypeFlag(pMem, MEM_Real); | - | ||||||||||||||||||
697 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||
698 | } | - | ||||||||||||||||||
699 | } | - | ||||||||||||||||||
700 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); | - | ||||||||||||||||||
701 | pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero); | - | ||||||||||||||||||
702 | return SQLITE_OK; executed 86 times by 1 test: return 0; Executed by:
| 86 | ||||||||||||||||||
703 | } | - | ||||||||||||||||||
704 | - | |||||||||||||||||||
705 | /* | - | ||||||||||||||||||
706 | ** Cast the datatype of the value in pMem according to the affinity | - | ||||||||||||||||||
707 | ** "aff". Casting is different from applying affinity in that a cast | - | ||||||||||||||||||
708 | ** is forced. In other words, the value is converted into the desired | - | ||||||||||||||||||
709 | ** affinity even if that results in loss of data. This routine is | - | ||||||||||||||||||
710 | ** used (for example) to implement the SQL "cast()" operator. | - | ||||||||||||||||||
711 | */ | - | ||||||||||||||||||
712 | void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ | - | ||||||||||||||||||
713 | if( pMem->flags & MEM_Null ) return; executed 37 times by 1 test: return; Executed by:
| 37-196131 | ||||||||||||||||||
714 | switch( aff ){ | - | ||||||||||||||||||
715 | case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */ executed 57 times by 1 test: case 'A': Executed by:
| 57 | ||||||||||||||||||
716 | if( (pMem->flags & MEM_Blob)==0 ){
| 3-54 | ||||||||||||||||||
717 | sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); | - | ||||||||||||||||||
718 | assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); | - | ||||||||||||||||||
719 | if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob); executed 54 times by 1 test: ((pMem)->flags = ((pMem)->flags&~(0xc1ff|0x4000))|0x0010); Executed by:
| 0-54 | ||||||||||||||||||
720 | }else{ executed 54 times by 1 test: end of block Executed by:
| 54 | ||||||||||||||||||
721 | pMem->flags &= ~(MEM_TypeMask&~MEM_Blob); | - | ||||||||||||||||||
722 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
723 | break; executed 57 times by 1 test: break; Executed by:
| 57 | ||||||||||||||||||
724 | } | - | ||||||||||||||||||
725 | case SQLITE_AFF_NUMERIC: { executed 69 times by 1 test: case 'C': Executed by:
| 69 | ||||||||||||||||||
726 | sqlite3VdbeMemNumerify(pMem); | - | ||||||||||||||||||
727 | break; executed 69 times by 1 test: break; Executed by:
| 69 | ||||||||||||||||||
728 | } | - | ||||||||||||||||||
729 | case SQLITE_AFF_INTEGER: { executed 6177 times by 1 test: case 'D': Executed by:
| 6177 | ||||||||||||||||||
730 | sqlite3VdbeMemIntegerify(pMem); | - | ||||||||||||||||||
731 | break; executed 6177 times by 1 test: break; Executed by:
| 6177 | ||||||||||||||||||
732 | } | - | ||||||||||||||||||
733 | case SQLITE_AFF_REAL: { executed 44662 times by 1 test: case 'E': Executed by:
| 44662 | ||||||||||||||||||
734 | sqlite3VdbeMemRealify(pMem); | - | ||||||||||||||||||
735 | break; executed 44662 times by 1 test: break; Executed by:
| 44662 | ||||||||||||||||||
736 | } | - | ||||||||||||||||||
737 | default: { executed 145166 times by 1 test: default: Executed by:
| 145166 | ||||||||||||||||||
738 | assert( aff==SQLITE_AFF_TEXT ); | - | ||||||||||||||||||
739 | assert( MEM_Str==(MEM_Blob>>3) ); | - | ||||||||||||||||||
740 | pMem->flags |= (pMem->flags&MEM_Blob)>>3; | - | ||||||||||||||||||
741 | sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); | - | ||||||||||||||||||
742 | assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); | - | ||||||||||||||||||
743 | pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); | - | ||||||||||||||||||
744 | break; executed 145166 times by 1 test: break; Executed by:
| 145166 | ||||||||||||||||||
745 | } | - | ||||||||||||||||||
746 | } | - | ||||||||||||||||||
747 | } | - | ||||||||||||||||||
748 | - | |||||||||||||||||||
749 | /* | - | ||||||||||||||||||
750 | ** Initialize bulk memory to be a consistent Mem object. | - | ||||||||||||||||||
751 | ** | - | ||||||||||||||||||
752 | ** The minimum amount of initialization feasible is performed. | - | ||||||||||||||||||
753 | */ | - | ||||||||||||||||||
754 | void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){ | - | ||||||||||||||||||
755 | assert( (flags & ~MEM_TypeMask)==0 ); | - | ||||||||||||||||||
756 | pMem->flags = flags; | - | ||||||||||||||||||
757 | pMem->db = db; | - | ||||||||||||||||||
758 | pMem->szMalloc = 0; | - | ||||||||||||||||||
759 | } executed 3190808 times by 8 tests: end of block Executed by:
| 3190808 | ||||||||||||||||||
760 | - | |||||||||||||||||||
761 | - | |||||||||||||||||||
762 | /* | - | ||||||||||||||||||
763 | ** Delete any previous value and set the value stored in *pMem to NULL. | - | ||||||||||||||||||
764 | ** | - | ||||||||||||||||||
765 | ** This routine calls the Mem.xDel destructor to dispose of values that | - | ||||||||||||||||||
766 | ** require the destructor. But it preserves the Mem.zMalloc memory allocation. | - | ||||||||||||||||||
767 | ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this | - | ||||||||||||||||||
768 | ** routine to invoke the destructor and deallocates Mem.zMalloc. | - | ||||||||||||||||||
769 | ** | - | ||||||||||||||||||
770 | ** Use this routine to reset the Mem prior to insert a new value. | - | ||||||||||||||||||
771 | ** | - | ||||||||||||||||||
772 | ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it. | - | ||||||||||||||||||
773 | */ | - | ||||||||||||||||||
774 | void sqlite3VdbeMemSetNull(Mem *pMem){ | - | ||||||||||||||||||
775 | if( VdbeMemDynamic(pMem) ){
| 17279-848378 | ||||||||||||||||||
776 | vdbeMemClearExternAndSetNull(pMem); | - | ||||||||||||||||||
777 | }else{ executed 17279 times by 3 tests: end of block Executed by:
| 17279 | ||||||||||||||||||
778 | pMem->flags = MEM_Null; | - | ||||||||||||||||||
779 | } executed 848378 times by 414 tests: end of block Executed by:
| 848378 | ||||||||||||||||||
780 | } | - | ||||||||||||||||||
781 | void sqlite3ValueSetNull(sqlite3_value *p){ | - | ||||||||||||||||||
782 | sqlite3VdbeMemSetNull((Mem*)p); | - | ||||||||||||||||||
783 | } executed 283904 times by 19 tests: end of block Executed by:
| 283904 | ||||||||||||||||||
784 | - | |||||||||||||||||||
785 | /* | - | ||||||||||||||||||
786 | ** Delete any previous value and set the value to be a BLOB of length | - | ||||||||||||||||||
787 | ** n containing all zeros. | - | ||||||||||||||||||
788 | */ | - | ||||||||||||||||||
789 | void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ | - | ||||||||||||||||||
790 | sqlite3VdbeMemRelease(pMem); | - | ||||||||||||||||||
791 | pMem->flags = MEM_Blob|MEM_Zero; | - | ||||||||||||||||||
792 | pMem->n = 0; | - | ||||||||||||||||||
793 | if( n<0 ) n = 0; executed 1 time by 1 test: n = 0; Executed by:
| 1-877 | ||||||||||||||||||
794 | pMem->u.nZero = n; | - | ||||||||||||||||||
795 | pMem->enc = SQLITE_UTF8; | - | ||||||||||||||||||
796 | pMem->z = 0; | - | ||||||||||||||||||
797 | } executed 878 times by 1 test: end of block Executed by:
| 878 | ||||||||||||||||||
798 | - | |||||||||||||||||||
799 | /* | - | ||||||||||||||||||
800 | ** The pMem is known to contain content that needs to be destroyed prior | - | ||||||||||||||||||
801 | ** to a value change. So invoke the destructor, then set the value to | - | ||||||||||||||||||
802 | ** a 64-bit integer. | - | ||||||||||||||||||
803 | */ | - | ||||||||||||||||||
804 | static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){ | - | ||||||||||||||||||
805 | sqlite3VdbeMemSetNull(pMem); | - | ||||||||||||||||||
806 | pMem->u.i = val; | - | ||||||||||||||||||
807 | pMem->flags = MEM_Int; | - | ||||||||||||||||||
808 | } never executed: end of block | 0 | ||||||||||||||||||
809 | - | |||||||||||||||||||
810 | /* | - | ||||||||||||||||||
811 | ** Delete any previous value and set the value stored in *pMem to val, | - | ||||||||||||||||||
812 | ** manifest type INTEGER. | - | ||||||||||||||||||
813 | */ | - | ||||||||||||||||||
814 | void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ | - | ||||||||||||||||||
815 | if( VdbeMemDynamic(pMem) ){
| 0-4042714 | ||||||||||||||||||
816 | vdbeReleaseAndSetInt64(pMem, val); | - | ||||||||||||||||||
817 | }else{ never executed: end of block | 0 | ||||||||||||||||||
818 | pMem->u.i = val; | - | ||||||||||||||||||
819 | pMem->flags = MEM_Int; | - | ||||||||||||||||||
820 | } executed 4042714 times by 375 tests: end of block Executed by:
| 4042714 | ||||||||||||||||||
821 | } | - | ||||||||||||||||||
822 | - | |||||||||||||||||||
823 | /* A no-op destructor */ | - | ||||||||||||||||||
824 | void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); } executed 17 times by 1 test: end of block Executed by:
| 17 | ||||||||||||||||||
825 | - | |||||||||||||||||||
826 | /* | - | ||||||||||||||||||
827 | ** Set the value stored in *pMem should already be a NULL. | - | ||||||||||||||||||
828 | ** Also store a pointer to go with it. | - | ||||||||||||||||||
829 | */ | - | ||||||||||||||||||
830 | void sqlite3VdbeMemSetPointer( | - | ||||||||||||||||||
831 | Mem *pMem, | - | ||||||||||||||||||
832 | void *pPtr, | - | ||||||||||||||||||
833 | const char *zPType, | - | ||||||||||||||||||
834 | void (*xDestructor)(void*) | - | ||||||||||||||||||
835 | ){ | - | ||||||||||||||||||
836 | assert( pMem->flags==MEM_Null ); | - | ||||||||||||||||||
837 | pMem->u.zPType = zPType ? zPType : "";
| 0-17 | ||||||||||||||||||
838 | pMem->z = pPtr; | - | ||||||||||||||||||
839 | pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term; | - | ||||||||||||||||||
840 | pMem->eSubtype = 'p'; | - | ||||||||||||||||||
841 | pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
| 0-17 | ||||||||||||||||||
842 | } executed 17 times by 1 test: end of block Executed by:
| 17 | ||||||||||||||||||
843 | - | |||||||||||||||||||
844 | #ifndef SQLITE_OMIT_FLOATING_POINT | - | ||||||||||||||||||
845 | /* | - | ||||||||||||||||||
846 | ** Delete any previous value and set the value stored in *pMem to val, | - | ||||||||||||||||||
847 | ** manifest type REAL. | - | ||||||||||||||||||
848 | */ | - | ||||||||||||||||||
849 | void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ | - | ||||||||||||||||||
850 | sqlite3VdbeMemSetNull(pMem); | - | ||||||||||||||||||
851 | if( !sqlite3IsNaN(val) ){
| 28-72930 | ||||||||||||||||||
852 | pMem->u.r = val; | - | ||||||||||||||||||
853 | pMem->flags = MEM_Real; | - | ||||||||||||||||||
854 | } executed 72930 times by 1 test: end of block Executed by:
| 72930 | ||||||||||||||||||
855 | } executed 72958 times by 1 test: end of block Executed by:
| 72958 | ||||||||||||||||||
856 | #endif | - | ||||||||||||||||||
857 | - | |||||||||||||||||||
858 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
859 | /* | - | ||||||||||||||||||
860 | ** Return true if the Mem holds a RowSet object. This routine is intended | - | ||||||||||||||||||
861 | ** for use inside of assert() statements. | - | ||||||||||||||||||
862 | */ | - | ||||||||||||||||||
863 | int sqlite3VdbeMemIsRowSet(const Mem *pMem){ | - | ||||||||||||||||||
864 | return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn) | - | ||||||||||||||||||
865 | && pMem->xDel==sqlite3RowSetDelete; | - | ||||||||||||||||||
866 | } | - | ||||||||||||||||||
867 | #endif | - | ||||||||||||||||||
868 | - | |||||||||||||||||||
869 | /* | - | ||||||||||||||||||
870 | ** Delete any previous value and set the value of pMem to be an | - | ||||||||||||||||||
871 | ** empty boolean index. | - | ||||||||||||||||||
872 | ** | - | ||||||||||||||||||
873 | ** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation | - | ||||||||||||||||||
874 | ** error occurs. | - | ||||||||||||||||||
875 | */ | - | ||||||||||||||||||
876 | int sqlite3VdbeMemSetRowSet(Mem *pMem){ | - | ||||||||||||||||||
877 | sqlite3 *db = pMem->db; | - | ||||||||||||||||||
878 | RowSet *p; | - | ||||||||||||||||||
879 | assert( db!=0 ); | - | ||||||||||||||||||
880 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
881 | sqlite3VdbeMemRelease(pMem); | - | ||||||||||||||||||
882 | p = sqlite3RowSetInit(db); | - | ||||||||||||||||||
883 | if( p==0 ) return SQLITE_NOMEM; never executed: return 7;
| 0-6846 | ||||||||||||||||||
884 | pMem->z = (char*)p; | - | ||||||||||||||||||
885 | pMem->flags = MEM_Blob|MEM_Dyn; | - | ||||||||||||||||||
886 | pMem->xDel = sqlite3RowSetDelete; | - | ||||||||||||||||||
887 | return SQLITE_OK; executed 6846 times by 1 test: return 0; Executed by:
| 6846 | ||||||||||||||||||
888 | } | - | ||||||||||||||||||
889 | - | |||||||||||||||||||
890 | /* | - | ||||||||||||||||||
891 | ** Return true if the Mem object contains a TEXT or BLOB that is | - | ||||||||||||||||||
892 | ** too large - whose size exceeds SQLITE_MAX_LENGTH. | - | ||||||||||||||||||
893 | */ | - | ||||||||||||||||||
894 | int sqlite3VdbeMemTooBig(Mem *p){ | - | ||||||||||||||||||
895 | assert( p->db!=0 ); | - | ||||||||||||||||||
896 | if( p->flags & (MEM_Str|MEM_Blob) ){
| 2371552-6329429 | ||||||||||||||||||
897 | int n = p->n; | - | ||||||||||||||||||
898 | if( p->flags & MEM_Zero ){
| 879-6328550 | ||||||||||||||||||
899 | n += p->u.nZero; | - | ||||||||||||||||||
900 | } executed 879 times by 1 test: end of block Executed by:
| 879 | ||||||||||||||||||
901 | return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; executed 6329429 times by 368 tests: return n>p->db->aLimit[0]; Executed by:
| 6329429 | ||||||||||||||||||
902 | } | - | ||||||||||||||||||
903 | return 0; executed 2371552 times by 1 test: return 0; Executed by:
| 2371552 | ||||||||||||||||||
904 | } | - | ||||||||||||||||||
905 | - | |||||||||||||||||||
906 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
907 | /* | - | ||||||||||||||||||
908 | ** This routine prepares a memory cell for modification by breaking | - | ||||||||||||||||||
909 | ** its link to a shallow copy and by marking any current shallow | - | ||||||||||||||||||
910 | ** copies of this cell as invalid. | - | ||||||||||||||||||
911 | ** | - | ||||||||||||||||||
912 | ** This is used for testing and debugging only - to make sure shallow | - | ||||||||||||||||||
913 | ** copies are not misused. | - | ||||||||||||||||||
914 | */ | - | ||||||||||||||||||
915 | void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){ | - | ||||||||||||||||||
916 | int i; | - | ||||||||||||||||||
917 | Mem *pX; | - | ||||||||||||||||||
918 | for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){ | - | ||||||||||||||||||
919 | if( pX->pScopyFrom==pMem ){ | - | ||||||||||||||||||
920 | /* If pX is marked as a shallow copy of pMem, then verify that | - | ||||||||||||||||||
921 | ** no significant changes have been made to pX since the OP_SCopy. | - | ||||||||||||||||||
922 | ** A significant change would indicated a missed call to this | - | ||||||||||||||||||
923 | ** function for pX. Minor changes, such as adding or removing a | - | ||||||||||||||||||
924 | ** dual type, are allowed, as long as the underlying value is the | - | ||||||||||||||||||
925 | ** same. */ | - | ||||||||||||||||||
926 | u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags; | - | ||||||||||||||||||
927 | assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i ); | - | ||||||||||||||||||
928 | assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r ); | - | ||||||||||||||||||
929 | assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) ); | - | ||||||||||||||||||
930 | assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 ); | - | ||||||||||||||||||
931 | - | |||||||||||||||||||
932 | /* pMem is the register that is changing. But also mark pX as | - | ||||||||||||||||||
933 | ** undefined so that we can quickly detect the shallow-copy error */ | - | ||||||||||||||||||
934 | pX->flags = MEM_Undefined; | - | ||||||||||||||||||
935 | pX->pScopyFrom = 0; | - | ||||||||||||||||||
936 | } | - | ||||||||||||||||||
937 | } | - | ||||||||||||||||||
938 | pMem->pScopyFrom = 0; | - | ||||||||||||||||||
939 | } | - | ||||||||||||||||||
940 | #endif /* SQLITE_DEBUG */ | - | ||||||||||||||||||
941 | - | |||||||||||||||||||
942 | - | |||||||||||||||||||
943 | /* | - | ||||||||||||||||||
944 | ** Make an shallow copy of pFrom into pTo. Prior contents of | - | ||||||||||||||||||
945 | ** pTo are freed. The pFrom->z field is not duplicated. If | - | ||||||||||||||||||
946 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z | - | ||||||||||||||||||
947 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). | - | ||||||||||||||||||
948 | */ | - | ||||||||||||||||||
949 | static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){ | - | ||||||||||||||||||
950 | vdbeMemClearExternAndSetNull(pTo); | - | ||||||||||||||||||
951 | assert( !VdbeMemDynamic(pTo) ); | - | ||||||||||||||||||
952 | sqlite3VdbeMemShallowCopy(pTo, pFrom, eType); | - | ||||||||||||||||||
953 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
954 | void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ | - | ||||||||||||||||||
955 | assert( !sqlite3VdbeMemIsRowSet(pFrom) ); | - | ||||||||||||||||||
956 | assert( pTo->db==pFrom->db ); | - | ||||||||||||||||||
957 | if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; } executed 1 time by 1 test: return; Executed by:
| 1-10248827 | ||||||||||||||||||
958 | memcpy(pTo, pFrom, MEMCELLSIZE); | - | ||||||||||||||||||
959 | if( (pFrom->flags&MEM_Static)==0 ){
| 4510679-5738148 | ||||||||||||||||||
960 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); | - | ||||||||||||||||||
961 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); | - | ||||||||||||||||||
962 | pTo->flags |= srcType; | - | ||||||||||||||||||
963 | } executed 5738148 times by 386 tests: end of block Executed by:
| 5738148 | ||||||||||||||||||
964 | } executed 10248827 times by 387 tests: end of block Executed by:
| 10248827 | ||||||||||||||||||
965 | - | |||||||||||||||||||
966 | /* | - | ||||||||||||||||||
967 | ** Make a full copy of pFrom into pTo. Prior contents of pTo are | - | ||||||||||||||||||
968 | ** freed before the copy is made. | - | ||||||||||||||||||
969 | */ | - | ||||||||||||||||||
970 | int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ | - | ||||||||||||||||||
971 | int rc = SQLITE_OK; | - | ||||||||||||||||||
972 | - | |||||||||||||||||||
973 | assert( !sqlite3VdbeMemIsRowSet(pFrom) ); | - | ||||||||||||||||||
974 | if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); executed 35 times by 1 test: vdbeMemClearExternAndSetNull(pTo); Executed by:
| 35-682521 | ||||||||||||||||||
975 | memcpy(pTo, pFrom, MEMCELLSIZE); | - | ||||||||||||||||||
976 | pTo->flags &= ~MEM_Dyn; | - | ||||||||||||||||||
977 | if( pTo->flags&(MEM_Str|MEM_Blob) ){
| 162450-520106 | ||||||||||||||||||
978 | if( 0==(pFrom->flags&MEM_Static) ){
| 1073-161377 | ||||||||||||||||||
979 | pTo->flags |= MEM_Ephem; | - | ||||||||||||||||||
980 | rc = sqlite3VdbeMemMakeWriteable(pTo); | - | ||||||||||||||||||
981 | } executed 161377 times by 1 test: end of block Executed by:
| 161377 | ||||||||||||||||||
982 | } executed 162450 times by 1 test: end of block Executed by:
| 162450 | ||||||||||||||||||
983 | - | |||||||||||||||||||
984 | return rc; executed 682556 times by 1 test: return rc; Executed by:
| 682556 | ||||||||||||||||||
985 | } | - | ||||||||||||||||||
986 | - | |||||||||||||||||||
987 | /* | - | ||||||||||||||||||
988 | ** Transfer the contents of pFrom to pTo. Any existing value in pTo is | - | ||||||||||||||||||
989 | ** freed. If pFrom contains ephemeral data, a copy is made. | - | ||||||||||||||||||
990 | ** | - | ||||||||||||||||||
991 | ** pFrom contains an SQL NULL when this routine returns. | - | ||||||||||||||||||
992 | */ | - | ||||||||||||||||||
993 | void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ | - | ||||||||||||||||||
994 | assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); | - | ||||||||||||||||||
995 | assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); | - | ||||||||||||||||||
996 | assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); | - | ||||||||||||||||||
997 | - | |||||||||||||||||||
998 | sqlite3VdbeMemRelease(pTo); | - | ||||||||||||||||||
999 | memcpy(pTo, pFrom, sizeof(Mem)); | - | ||||||||||||||||||
1000 | pFrom->flags = MEM_Null; | - | ||||||||||||||||||
1001 | pFrom->szMalloc = 0; | - | ||||||||||||||||||
1002 | } executed 49642 times by 1 test: end of block Executed by:
| 49642 | ||||||||||||||||||
1003 | - | |||||||||||||||||||
1004 | /* | - | ||||||||||||||||||
1005 | ** Change the value of a Mem to be a string or a BLOB. | - | ||||||||||||||||||
1006 | ** | - | ||||||||||||||||||
1007 | ** The memory management strategy depends on the value of the xDel | - | ||||||||||||||||||
1008 | ** parameter. If the value passed is SQLITE_TRANSIENT, then the | - | ||||||||||||||||||
1009 | ** string is copied into a (possibly existing) buffer managed by the | - | ||||||||||||||||||
1010 | ** Mem structure. Otherwise, any existing buffer is freed and the | - | ||||||||||||||||||
1011 | ** pointer copied. | - | ||||||||||||||||||
1012 | ** | - | ||||||||||||||||||
1013 | ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH | - | ||||||||||||||||||
1014 | ** size limit) then no memory allocation occurs. If the string can be | - | ||||||||||||||||||
1015 | ** stored without allocating memory, then it is. If a memory allocation | - | ||||||||||||||||||
1016 | ** is required to store the string, then value of pMem is unchanged. In | - | ||||||||||||||||||
1017 | ** either case, SQLITE_TOOBIG is returned. | - | ||||||||||||||||||
1018 | */ | - | ||||||||||||||||||
1019 | int sqlite3VdbeMemSetStr( | - | ||||||||||||||||||
1020 | Mem *pMem, /* Memory cell to set to string value */ | - | ||||||||||||||||||
1021 | const char *z, /* String pointer */ | - | ||||||||||||||||||
1022 | int n, /* Bytes in string, or negative */ | - | ||||||||||||||||||
1023 | u8 enc, /* Encoding of z. 0 for BLOBs */ | - | ||||||||||||||||||
1024 | void (*xDel)(void*) /* Destructor function */ | - | ||||||||||||||||||
1025 | ){ | - | ||||||||||||||||||
1026 | int nByte = n; /* New value for pMem->n */ | - | ||||||||||||||||||
1027 | int iLimit; /* Maximum allowed string or blob size */ | - | ||||||||||||||||||
1028 | u16 flags = 0; /* New value for pMem->flags */ | - | ||||||||||||||||||
1029 | - | |||||||||||||||||||
1030 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); | - | ||||||||||||||||||
1031 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
1032 | - | |||||||||||||||||||
1033 | /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ | - | ||||||||||||||||||
1034 | if( !z ){
| 157859-2906338 | ||||||||||||||||||
1035 | sqlite3VdbeMemSetNull(pMem); | - | ||||||||||||||||||
1036 | return SQLITE_OK; executed 157859 times by 34 tests: return 0; Executed by:
| 157859 | ||||||||||||||||||
1037 | } | - | ||||||||||||||||||
1038 | - | |||||||||||||||||||
1039 | if( pMem->db ){
| 256-2906082 | ||||||||||||||||||
1040 | iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; | - | ||||||||||||||||||
1041 | }else{ executed 2906082 times by 436 tests: end of block Executed by:
| 2906082 | ||||||||||||||||||
1042 | iLimit = SQLITE_MAX_LENGTH; | - | ||||||||||||||||||
1043 | } executed 256 times by 1 test: end of block Executed by:
| 256 | ||||||||||||||||||
1044 | flags = (enc==0?MEM_Blob:MEM_Str);
| 1361751-1544587 | ||||||||||||||||||
1045 | if( nByte<0 ){
| 763106-2143232 | ||||||||||||||||||
1046 | assert( enc!=0 ); | - | ||||||||||||||||||
1047 | if( enc==SQLITE_UTF8 ){
| 1865-761241 | ||||||||||||||||||
1048 | nByte = 0x7fffffff & (int)strlen(z); | - | ||||||||||||||||||
1049 | if( nByte>iLimit ) nByte = iLimit+1; never executed: nByte = iLimit+1;
| 0-761241 | ||||||||||||||||||
1050 | }else{ executed 761241 times by 436 tests: end of block Executed by:
| 761241 | ||||||||||||||||||
1051 | for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} executed 102993 times by 1 test: end of block Executed by:
| 0-104858 | ||||||||||||||||||
1052 | } executed 1865 times by 1 test: end of block Executed by:
| 1865 | ||||||||||||||||||
1053 | flags |= MEM_Term; | - | ||||||||||||||||||
1054 | } executed 763106 times by 436 tests: end of block Executed by:
| 763106 | ||||||||||||||||||
1055 | - | |||||||||||||||||||
1056 | /* The following block sets the new values of Mem.z and Mem.xDel. It | - | ||||||||||||||||||
1057 | ** also sets a flag in local variable "flags" to indicate the memory | - | ||||||||||||||||||
1058 | ** management (one of MEM_Dyn or MEM_Static). | - | ||||||||||||||||||
1059 | */ | - | ||||||||||||||||||
1060 | if( xDel==SQLITE_TRANSIENT ){
| 1203433-1702905 | ||||||||||||||||||
1061 | int nAlloc = nByte; | - | ||||||||||||||||||
1062 | if( flags&MEM_Term ){
| 516027-687406 | ||||||||||||||||||
1063 | nAlloc += (enc==SQLITE_UTF8?1:2);
| 15-516012 | ||||||||||||||||||
1064 | } executed 516027 times by 435 tests: end of block Executed by:
| 516027 | ||||||||||||||||||
1065 | if( nByte>iLimit ){
| 0-1203433 | ||||||||||||||||||
1066 | return SQLITE_TOOBIG; never executed: return 18; | 0 | ||||||||||||||||||
1067 | } | - | ||||||||||||||||||
1068 | testcase( nAlloc==0 ); | - | ||||||||||||||||||
1069 | testcase( nAlloc==31 ); | - | ||||||||||||||||||
1070 | testcase( nAlloc==32 ); | - | ||||||||||||||||||
1071 | if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
| 124-1203309 | ||||||||||||||||||
1072 | return SQLITE_NOMEM_BKPT; executed 124 times by 1 test: return 7; Executed by:
| 124 | ||||||||||||||||||
1073 | } | - | ||||||||||||||||||
1074 | memcpy(pMem->z, z, nAlloc); | - | ||||||||||||||||||
1075 | }else if( xDel==SQLITE_DYNAMIC ){ executed 1203309 times by 435 tests: end of block Executed by:
| 128995-1573910 | ||||||||||||||||||
1076 | sqlite3VdbeMemRelease(pMem); | - | ||||||||||||||||||
1077 | pMem->zMalloc = pMem->z = (char *)z; | - | ||||||||||||||||||
1078 | pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); | - | ||||||||||||||||||
1079 | }else{ executed 128995 times by 16 tests: end of block Executed by:
| 128995 | ||||||||||||||||||
1080 | sqlite3VdbeMemRelease(pMem); | - | ||||||||||||||||||
1081 | pMem->z = (char *)z; | - | ||||||||||||||||||
1082 | pMem->xDel = xDel; | - | ||||||||||||||||||
1083 | flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
| 225409-1348501 | ||||||||||||||||||
1084 | } executed 1573910 times by 399 tests: end of block Executed by:
| 1573910 | ||||||||||||||||||
1085 | - | |||||||||||||||||||
1086 | pMem->n = nByte; | - | ||||||||||||||||||
1087 | pMem->flags = flags; | - | ||||||||||||||||||
1088 | pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
| 1361751-1544463 | ||||||||||||||||||
1089 | - | |||||||||||||||||||
1090 | #ifndef SQLITE_OMIT_UTF16 | - | ||||||||||||||||||
1091 | if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
| 0-2903703 | ||||||||||||||||||
1092 | return SQLITE_NOMEM_BKPT; never executed: return 7; | 0 | ||||||||||||||||||
1093 | } | - | ||||||||||||||||||
1094 | #endif | - | ||||||||||||||||||
1095 | - | |||||||||||||||||||
1096 | if( nByte>iLimit ){
| 0-2906214 | ||||||||||||||||||
1097 | return SQLITE_TOOBIG; never executed: return 18; | 0 | ||||||||||||||||||
1098 | } | - | ||||||||||||||||||
1099 | - | |||||||||||||||||||
1100 | return SQLITE_OK; executed 2906214 times by 436 tests: return 0; Executed by:
| 2906214 | ||||||||||||||||||
1101 | } | - | ||||||||||||||||||
1102 | - | |||||||||||||||||||
1103 | /* | - | ||||||||||||||||||
1104 | ** Move data out of a btree key or data field and into a Mem structure. | - | ||||||||||||||||||
1105 | ** The data is payload from the entry that pCur is currently pointing | - | ||||||||||||||||||
1106 | ** to. offset and amt determine what portion of the data or key to retrieve. | - | ||||||||||||||||||
1107 | ** The result is written into the pMem element. | - | ||||||||||||||||||
1108 | ** | - | ||||||||||||||||||
1109 | ** The pMem object must have been initialized. This routine will use | - | ||||||||||||||||||
1110 | ** pMem->zMalloc to hold the content from the btree, if possible. New | - | ||||||||||||||||||
1111 | ** pMem->zMalloc space will be allocated if necessary. The calling routine | - | ||||||||||||||||||
1112 | ** is responsible for making sure that the pMem object is eventually | - | ||||||||||||||||||
1113 | ** destroyed. | - | ||||||||||||||||||
1114 | ** | - | ||||||||||||||||||
1115 | ** If this routine fails for any reason (malloc returns NULL or unable | - | ||||||||||||||||||
1116 | ** to read from the disk) then the pMem is left in an inconsistent state. | - | ||||||||||||||||||
1117 | */ | - | ||||||||||||||||||
1118 | static SQLITE_NOINLINE int vdbeMemFromBtreeResize( | - | ||||||||||||||||||
1119 | BtCursor *pCur, /* Cursor pointing at record to retrieve. */ | - | ||||||||||||||||||
1120 | u32 offset, /* Offset from the start of data to return bytes from. */ | - | ||||||||||||||||||
1121 | u32 amt, /* Number of bytes to return. */ | - | ||||||||||||||||||
1122 | Mem *pMem /* OUT: Return data in this Mem structure. */ | - | ||||||||||||||||||
1123 | ){ | - | ||||||||||||||||||
1124 | int rc; | - | ||||||||||||||||||
1125 | pMem->flags = MEM_Null; | - | ||||||||||||||||||
1126 | if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
| 0-496729 | ||||||||||||||||||
1127 | rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z); | - | ||||||||||||||||||
1128 | if( rc==SQLITE_OK ){
| 5-496724 | ||||||||||||||||||
1129 | pMem->z[amt] = 0; /* Overrun area used when reading malformed records */ | - | ||||||||||||||||||
1130 | pMem->flags = MEM_Blob; | - | ||||||||||||||||||
1131 | pMem->n = (int)amt; | - | ||||||||||||||||||
1132 | }else{ executed 496724 times by 9 tests: end of block Executed by:
| 496724 | ||||||||||||||||||
1133 | sqlite3VdbeMemRelease(pMem); | - | ||||||||||||||||||
1134 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||
1135 | } | - | ||||||||||||||||||
1136 | return rc; executed 496729 times by 9 tests: return rc; Executed by:
| 496729 | ||||||||||||||||||
1137 | } | - | ||||||||||||||||||
1138 | int sqlite3VdbeMemFromBtree( | - | ||||||||||||||||||
1139 | BtCursor *pCur, /* Cursor pointing at record to retrieve. */ | - | ||||||||||||||||||
1140 | u32 offset, /* Offset from the start of data to return bytes from. */ | - | ||||||||||||||||||
1141 | u32 amt, /* Number of bytes to return. */ | - | ||||||||||||||||||
1142 | Mem *pMem /* OUT: Return data in this Mem structure. */ | - | ||||||||||||||||||
1143 | ){ | - | ||||||||||||||||||
1144 | char *zData; /* Data from the btree layer */ | - | ||||||||||||||||||
1145 | u32 available = 0; /* Number of bytes available on the local btree page */ | - | ||||||||||||||||||
1146 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||
1147 | - | |||||||||||||||||||
1148 | assert( sqlite3BtreeCursorIsValid(pCur) ); | - | ||||||||||||||||||
1149 | assert( !VdbeMemDynamic(pMem) ); | - | ||||||||||||||||||
1150 | - | |||||||||||||||||||
1151 | /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() | - | ||||||||||||||||||
1152 | ** that both the BtShared and database handle mutexes are held. */ | - | ||||||||||||||||||
1153 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); | - | ||||||||||||||||||
1154 | zData = (char *)sqlite3BtreePayloadFetch(pCur, &available); | - | ||||||||||||||||||
1155 | assert( zData!=0 ); | - | ||||||||||||||||||
1156 | - | |||||||||||||||||||
1157 | if( offset+amt<=available ){
| 496729-4702149 | ||||||||||||||||||
1158 | pMem->z = &zData[offset]; | - | ||||||||||||||||||
1159 | pMem->flags = MEM_Blob|MEM_Ephem; | - | ||||||||||||||||||
1160 | pMem->n = (int)amt; | - | ||||||||||||||||||
1161 | }else{ executed 4702149 times by 5 tests: end of block Executed by:
| 4702149 | ||||||||||||||||||
1162 | rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem); | - | ||||||||||||||||||
1163 | } executed 496729 times by 9 tests: end of block Executed by:
| 496729 | ||||||||||||||||||
1164 | - | |||||||||||||||||||
1165 | return rc; executed 5198878 times by 13 tests: return rc; Executed by:
| 5198878 | ||||||||||||||||||
1166 | } | - | ||||||||||||||||||
1167 | - | |||||||||||||||||||
1168 | /* | - | ||||||||||||||||||
1169 | ** The pVal argument is known to be a value other than NULL. | - | ||||||||||||||||||
1170 | ** Convert it into a string with encoding enc and return a pointer | - | ||||||||||||||||||
1171 | ** to a zero-terminated version of that string. | - | ||||||||||||||||||
1172 | */ | - | ||||||||||||||||||
1173 | static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ | - | ||||||||||||||||||
1174 | assert( pVal!=0 ); | - | ||||||||||||||||||
1175 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); | - | ||||||||||||||||||
1176 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); | - | ||||||||||||||||||
1177 | assert( !sqlite3VdbeMemIsRowSet(pVal) ); | - | ||||||||||||||||||
1178 | assert( (pVal->flags & (MEM_Null))==0 ); | - | ||||||||||||||||||
1179 | if( pVal->flags & (MEM_Blob|MEM_Str) ){
| 22246-144258 | ||||||||||||||||||
1180 | if( ExpandBlob(pVal) ) return 0; never executed: return 0;
| 0-22246 | ||||||||||||||||||
1181 | pVal->flags |= MEM_Str; | - | ||||||||||||||||||
1182 | if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
| 1021-21225 | ||||||||||||||||||
1183 | sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); | - | ||||||||||||||||||
1184 | } executed 1021 times by 1 test: end of block Executed by:
| 1021 | ||||||||||||||||||
1185 | if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
| 416-21184 | ||||||||||||||||||
1186 | assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); | - | ||||||||||||||||||
1187 | if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
| 0-416 | ||||||||||||||||||
1188 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
1189 | } | - | ||||||||||||||||||
1190 | } executed 416 times by 1 test: end of block Executed by:
| 416 | ||||||||||||||||||
1191 | sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */ | - | ||||||||||||||||||
1192 | }else{ executed 22246 times by 1 test: end of block Executed by:
| 22246 | ||||||||||||||||||
1193 | sqlite3VdbeMemStringify(pVal, enc, 0); | - | ||||||||||||||||||
1194 | assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); | - | ||||||||||||||||||
1195 | } executed 144258 times by 434 tests: end of block Executed by:
| 144258 | ||||||||||||||||||
1196 | assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 | - | ||||||||||||||||||
1197 | || pVal->db->mallocFailed ); | - | ||||||||||||||||||
1198 | if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
| 17-166487 | ||||||||||||||||||
1199 | assert( sqlite3VdbeMemConsistentDualRep(pVal) ); | - | ||||||||||||||||||
1200 | return pVal->z; executed 166487 times by 434 tests: return pVal->z; Executed by:
| 166487 | ||||||||||||||||||
1201 | }else{ | - | ||||||||||||||||||
1202 | return 0; executed 17 times by 1 test: return 0; Executed by:
| 17 | ||||||||||||||||||
1203 | } | - | ||||||||||||||||||
1204 | } | - | ||||||||||||||||||
1205 | - | |||||||||||||||||||
1206 | /* This function is only available internally, it is not part of the | - | ||||||||||||||||||
1207 | ** external API. It works in a similar way to sqlite3_value_text(), | - | ||||||||||||||||||
1208 | ** except the data returned is in the encoding specified by the second | - | ||||||||||||||||||
1209 | ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or | - | ||||||||||||||||||
1210 | ** SQLITE_UTF8. | - | ||||||||||||||||||
1211 | ** | - | ||||||||||||||||||
1212 | ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. | - | ||||||||||||||||||
1213 | ** If that is the case, then the result must be aligned on an even byte | - | ||||||||||||||||||
1214 | ** boundary. | - | ||||||||||||||||||
1215 | */ | - | ||||||||||||||||||
1216 | const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ | - | ||||||||||||||||||
1217 | if( !pVal ) return 0; executed 2915 times by 10 tests: return 0; Executed by:
| 2915-3951312 | ||||||||||||||||||
1218 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); | - | ||||||||||||||||||
1219 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); | - | ||||||||||||||||||
1220 | assert( !sqlite3VdbeMemIsRowSet(pVal) ); | - | ||||||||||||||||||
1221 | if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
| 1087-3768816 | ||||||||||||||||||
1222 | assert( sqlite3VdbeMemConsistentDualRep(pVal) ); | - | ||||||||||||||||||
1223 | return pVal->z; executed 3767729 times by 436 tests: return pVal->z; Executed by:
| 3767729 | ||||||||||||||||||
1224 | } | - | ||||||||||||||||||
1225 | if( pVal->flags&MEM_Null ){
| 17245-166338 | ||||||||||||||||||
1226 | return 0; executed 17245 times by 376 tests: return 0; Executed by:
| 17245 | ||||||||||||||||||
1227 | } | - | ||||||||||||||||||
1228 | return valueToText(pVal, enc); executed 166338 times by 434 tests: return valueToText(pVal, enc); Executed by:
| 166338 | ||||||||||||||||||
1229 | } | - | ||||||||||||||||||
1230 | - | |||||||||||||||||||
1231 | /* | - | ||||||||||||||||||
1232 | ** Create a new sqlite3_value object. | - | ||||||||||||||||||
1233 | */ | - | ||||||||||||||||||
1234 | sqlite3_value *sqlite3ValueNew(sqlite3 *db){ | - | ||||||||||||||||||
1235 | Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); | - | ||||||||||||||||||
1236 | if( p ){
| 126-2854 | ||||||||||||||||||
1237 | p->flags = MEM_Null; | - | ||||||||||||||||||
1238 | p->db = db; | - | ||||||||||||||||||
1239 | } executed 2854 times by 19 tests: end of block Executed by:
| 2854 | ||||||||||||||||||
1240 | return p; executed 2980 times by 19 tests: return p; Executed by:
| 2980 | ||||||||||||||||||
1241 | } | - | ||||||||||||||||||
1242 | - | |||||||||||||||||||
1243 | /* | - | ||||||||||||||||||
1244 | ** Context object passed by sqlite3Stat4ProbeSetValue() through to | - | ||||||||||||||||||
1245 | ** valueNew(). See comments above valueNew() for details. | - | ||||||||||||||||||
1246 | */ | - | ||||||||||||||||||
1247 | struct ValueNewStat4Ctx { | - | ||||||||||||||||||
1248 | Parse *pParse; | - | ||||||||||||||||||
1249 | Index *pIdx; | - | ||||||||||||||||||
1250 | UnpackedRecord **ppRec; | - | ||||||||||||||||||
1251 | int iVal; | - | ||||||||||||||||||
1252 | }; | - | ||||||||||||||||||
1253 | - | |||||||||||||||||||
1254 | /* | - | ||||||||||||||||||
1255 | ** Allocate and return a pointer to a new sqlite3_value object. If | - | ||||||||||||||||||
1256 | ** the second argument to this function is NULL, the object is allocated | - | ||||||||||||||||||
1257 | ** by calling sqlite3ValueNew(). | - | ||||||||||||||||||
1258 | ** | - | ||||||||||||||||||
1259 | ** Otherwise, if the second argument is non-zero, then this function is | - | ||||||||||||||||||
1260 | ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not | - | ||||||||||||||||||
1261 | ** already been allocated, allocate the UnpackedRecord structure that | - | ||||||||||||||||||
1262 | ** that function will return to its caller here. Then return a pointer to | - | ||||||||||||||||||
1263 | ** an sqlite3_value within the UnpackedRecord.a[] array. | - | ||||||||||||||||||
1264 | */ | - | ||||||||||||||||||
1265 | static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ | - | ||||||||||||||||||
1266 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1267 | if( p ){ | - | ||||||||||||||||||
1268 | UnpackedRecord *pRec = p->ppRec[0]; | - | ||||||||||||||||||
1269 | - | |||||||||||||||||||
1270 | if( pRec==0 ){ | - | ||||||||||||||||||
1271 | Index *pIdx = p->pIdx; /* Index being probed */ | - | ||||||||||||||||||
1272 | int nByte; /* Bytes of space to allocate */ | - | ||||||||||||||||||
1273 | int i; /* Counter variable */ | - | ||||||||||||||||||
1274 | int nCol = pIdx->nColumn; /* Number of index columns including rowid */ | - | ||||||||||||||||||
1275 | - | |||||||||||||||||||
1276 | nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord)); | - | ||||||||||||||||||
1277 | pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte); | - | ||||||||||||||||||
1278 | if( pRec ){ | - | ||||||||||||||||||
1279 | pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); | - | ||||||||||||||||||
1280 | if( pRec->pKeyInfo ){ | - | ||||||||||||||||||
1281 | assert( pRec->pKeyInfo->nAllField==nCol ); | - | ||||||||||||||||||
1282 | assert( pRec->pKeyInfo->enc==ENC(db) ); | - | ||||||||||||||||||
1283 | pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); | - | ||||||||||||||||||
1284 | for(i=0; i<nCol; i++){ | - | ||||||||||||||||||
1285 | pRec->aMem[i].flags = MEM_Null; | - | ||||||||||||||||||
1286 | pRec->aMem[i].db = db; | - | ||||||||||||||||||
1287 | } | - | ||||||||||||||||||
1288 | }else{ | - | ||||||||||||||||||
1289 | sqlite3DbFreeNN(db, pRec); | - | ||||||||||||||||||
1290 | pRec = 0; | - | ||||||||||||||||||
1291 | } | - | ||||||||||||||||||
1292 | } | - | ||||||||||||||||||
1293 | if( pRec==0 ) return 0; | - | ||||||||||||||||||
1294 | p->ppRec[0] = pRec; | - | ||||||||||||||||||
1295 | } | - | ||||||||||||||||||
1296 | - | |||||||||||||||||||
1297 | pRec->nField = p->iVal+1; | - | ||||||||||||||||||
1298 | return &pRec->aMem[p->iVal]; | - | ||||||||||||||||||
1299 | } | - | ||||||||||||||||||
1300 | #else | - | ||||||||||||||||||
1301 | UNUSED_PARAMETER(p); | - | ||||||||||||||||||
1302 | #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ | - | ||||||||||||||||||
1303 | return sqlite3ValueNew(db); executed 678 times by 1 test: return sqlite3ValueNew(db); Executed by:
| 678 | ||||||||||||||||||
1304 | } | - | ||||||||||||||||||
1305 | - | |||||||||||||||||||
1306 | /* | - | ||||||||||||||||||
1307 | ** The expression object indicated by the second argument is guaranteed | - | ||||||||||||||||||
1308 | ** to be a scalar SQL function. If | - | ||||||||||||||||||
1309 | ** | - | ||||||||||||||||||
1310 | ** * all function arguments are SQL literals, | - | ||||||||||||||||||
1311 | ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and | - | ||||||||||||||||||
1312 | ** * the SQLITE_FUNC_NEEDCOLL function flag is not set, | - | ||||||||||||||||||
1313 | ** | - | ||||||||||||||||||
1314 | ** then this routine attempts to invoke the SQL function. Assuming no | - | ||||||||||||||||||
1315 | ** error occurs, output parameter (*ppVal) is set to point to a value | - | ||||||||||||||||||
1316 | ** object containing the result before returning SQLITE_OK. | - | ||||||||||||||||||
1317 | ** | - | ||||||||||||||||||
1318 | ** Affinity aff is applied to the result of the function before returning. | - | ||||||||||||||||||
1319 | ** If the result is a text value, the sqlite3_value object uses encoding | - | ||||||||||||||||||
1320 | ** enc. | - | ||||||||||||||||||
1321 | ** | - | ||||||||||||||||||
1322 | ** If the conditions above are not met, this function returns SQLITE_OK | - | ||||||||||||||||||
1323 | ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to | - | ||||||||||||||||||
1324 | ** NULL and an SQLite error code returned. | - | ||||||||||||||||||
1325 | */ | - | ||||||||||||||||||
1326 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1327 | static int valueFromFunction( | - | ||||||||||||||||||
1328 | sqlite3 *db, /* The database connection */ | - | ||||||||||||||||||
1329 | Expr *p, /* The expression to evaluate */ | - | ||||||||||||||||||
1330 | u8 enc, /* Encoding to use */ | - | ||||||||||||||||||
1331 | u8 aff, /* Affinity to use */ | - | ||||||||||||||||||
1332 | sqlite3_value **ppVal, /* Write the new value here */ | - | ||||||||||||||||||
1333 | struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ | - | ||||||||||||||||||
1334 | ){ | - | ||||||||||||||||||
1335 | sqlite3_context ctx; /* Context object for function invocation */ | - | ||||||||||||||||||
1336 | sqlite3_value **apVal = 0; /* Function arguments */ | - | ||||||||||||||||||
1337 | int nVal = 0; /* Size of apVal[] array */ | - | ||||||||||||||||||
1338 | FuncDef *pFunc = 0; /* Function definition */ | - | ||||||||||||||||||
1339 | sqlite3_value *pVal = 0; /* New value */ | - | ||||||||||||||||||
1340 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||
1341 | ExprList *pList = 0; /* Function arguments */ | - | ||||||||||||||||||
1342 | int i; /* Iterator variable */ | - | ||||||||||||||||||
1343 | - | |||||||||||||||||||
1344 | assert( pCtx!=0 ); | - | ||||||||||||||||||
1345 | assert( (p->flags & EP_TokenOnly)==0 ); | - | ||||||||||||||||||
1346 | pList = p->x.pList; | - | ||||||||||||||||||
1347 | if( pList ) nVal = pList->nExpr; | - | ||||||||||||||||||
1348 | pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0); | - | ||||||||||||||||||
1349 | assert( pFunc ); | - | ||||||||||||||||||
1350 | if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 | - | ||||||||||||||||||
1351 | || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) | - | ||||||||||||||||||
1352 | ){ | - | ||||||||||||||||||
1353 | return SQLITE_OK; | - | ||||||||||||||||||
1354 | } | - | ||||||||||||||||||
1355 | - | |||||||||||||||||||
1356 | if( pList ){ | - | ||||||||||||||||||
1357 | apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal); | - | ||||||||||||||||||
1358 | if( apVal==0 ){ | - | ||||||||||||||||||
1359 | rc = SQLITE_NOMEM_BKPT; | - | ||||||||||||||||||
1360 | goto value_from_function_out; | - | ||||||||||||||||||
1361 | } | - | ||||||||||||||||||
1362 | for(i=0; i<nVal; i++){ | - | ||||||||||||||||||
1363 | rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]); | - | ||||||||||||||||||
1364 | if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out; | - | ||||||||||||||||||
1365 | } | - | ||||||||||||||||||
1366 | } | - | ||||||||||||||||||
1367 | - | |||||||||||||||||||
1368 | pVal = valueNew(db, pCtx); | - | ||||||||||||||||||
1369 | if( pVal==0 ){ | - | ||||||||||||||||||
1370 | rc = SQLITE_NOMEM_BKPT; | - | ||||||||||||||||||
1371 | goto value_from_function_out; | - | ||||||||||||||||||
1372 | } | - | ||||||||||||||||||
1373 | - | |||||||||||||||||||
1374 | assert( pCtx->pParse->rc==SQLITE_OK ); | - | ||||||||||||||||||
1375 | memset(&ctx, 0, sizeof(ctx)); | - | ||||||||||||||||||
1376 | ctx.pOut = pVal; | - | ||||||||||||||||||
1377 | ctx.pFunc = pFunc; | - | ||||||||||||||||||
1378 | pFunc->xSFunc(&ctx, nVal, apVal); | - | ||||||||||||||||||
1379 | if( ctx.isError ){ | - | ||||||||||||||||||
1380 | rc = ctx.isError; | - | ||||||||||||||||||
1381 | sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); | - | ||||||||||||||||||
1382 | }else{ | - | ||||||||||||||||||
1383 | sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); | - | ||||||||||||||||||
1384 | assert( rc==SQLITE_OK ); | - | ||||||||||||||||||
1385 | rc = sqlite3VdbeChangeEncoding(pVal, enc); | - | ||||||||||||||||||
1386 | if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ | - | ||||||||||||||||||
1387 | rc = SQLITE_TOOBIG; | - | ||||||||||||||||||
1388 | pCtx->pParse->nErr++; | - | ||||||||||||||||||
1389 | } | - | ||||||||||||||||||
1390 | } | - | ||||||||||||||||||
1391 | pCtx->pParse->rc = rc; | - | ||||||||||||||||||
1392 | - | |||||||||||||||||||
1393 | value_from_function_out: | - | ||||||||||||||||||
1394 | if( rc!=SQLITE_OK ){ | - | ||||||||||||||||||
1395 | pVal = 0; | - | ||||||||||||||||||
1396 | } | - | ||||||||||||||||||
1397 | if( apVal ){ | - | ||||||||||||||||||
1398 | for(i=0; i<nVal; i++){ | - | ||||||||||||||||||
1399 | sqlite3ValueFree(apVal[i]); | - | ||||||||||||||||||
1400 | } | - | ||||||||||||||||||
1401 | sqlite3DbFreeNN(db, apVal); | - | ||||||||||||||||||
1402 | } | - | ||||||||||||||||||
1403 | - | |||||||||||||||||||
1404 | *ppVal = pVal; | - | ||||||||||||||||||
1405 | return rc; | - | ||||||||||||||||||
1406 | } | - | ||||||||||||||||||
1407 | #else | - | ||||||||||||||||||
1408 | # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK | - | ||||||||||||||||||
1409 | #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ | - | ||||||||||||||||||
1410 | - | |||||||||||||||||||
1411 | /* | - | ||||||||||||||||||
1412 | ** Extract a value from the supplied expression in the manner described | - | ||||||||||||||||||
1413 | ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object | - | ||||||||||||||||||
1414 | ** using valueNew(). | - | ||||||||||||||||||
1415 | ** | - | ||||||||||||||||||
1416 | ** If pCtx is NULL and an error occurs after the sqlite3_value object | - | ||||||||||||||||||
1417 | ** has been allocated, it is freed before returning. Or, if pCtx is not | - | ||||||||||||||||||
1418 | ** NULL, it is assumed that the caller will free any allocated object | - | ||||||||||||||||||
1419 | ** in all cases. | - | ||||||||||||||||||
1420 | */ | - | ||||||||||||||||||
1421 | static int valueFromExpr( | - | ||||||||||||||||||
1422 | sqlite3 *db, /* The database connection */ | - | ||||||||||||||||||
1423 | Expr *pExpr, /* The expression to evaluate */ | - | ||||||||||||||||||
1424 | u8 enc, /* Encoding to use */ | - | ||||||||||||||||||
1425 | u8 affinity, /* Affinity to use */ | - | ||||||||||||||||||
1426 | sqlite3_value **ppVal, /* Write the new value here */ | - | ||||||||||||||||||
1427 | struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ | - | ||||||||||||||||||
1428 | ){ | - | ||||||||||||||||||
1429 | int op; | - | ||||||||||||||||||
1430 | char *zVal = 0; | - | ||||||||||||||||||
1431 | sqlite3_value *pVal = 0; | - | ||||||||||||||||||
1432 | int negInt = 1; | - | ||||||||||||||||||
1433 | const char *zNeg = ""; | - | ||||||||||||||||||
1434 | int rc = SQLITE_OK; | - | ||||||||||||||||||
1435 | - | |||||||||||||||||||
1436 | assert( pExpr!=0 ); | - | ||||||||||||||||||
1437 | while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; executed 672 times by 1 test: pExpr = pExpr->pLeft; Executed by:
| 0-1401 | ||||||||||||||||||
1438 | #if defined(SQLITE_ENABLE_STAT3_OR_STAT4) | - | ||||||||||||||||||
1439 | if( op==TK_REGISTER ) op = pExpr->op2; | - | ||||||||||||||||||
1440 | #else | - | ||||||||||||||||||
1441 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; never executed: op = pExpr->op2;
| 0-729 | ||||||||||||||||||
1442 | #endif | - | ||||||||||||||||||
1443 | - | |||||||||||||||||||
1444 | /* Compressed expressions only appear when parsing the DEFAULT clause | - | ||||||||||||||||||
1445 | ** on a table column definition, and hence only when pCtx==0. This | - | ||||||||||||||||||
1446 | ** check ensures that an EP_TokenOnly expression is never passed down | - | ||||||||||||||||||
1447 | ** into valueFromFunction(). */ | - | ||||||||||||||||||
1448 | assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 ); | - | ||||||||||||||||||
1449 | - | |||||||||||||||||||
1450 | if( op==TK_CAST ){
| 0-729 | ||||||||||||||||||
1451 | u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); | - | ||||||||||||||||||
1452 | rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); | - | ||||||||||||||||||
1453 | testcase( rc!=SQLITE_OK ); | - | ||||||||||||||||||
1454 | if( *ppVal ){
| 0 | ||||||||||||||||||
1455 | sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8); | - | ||||||||||||||||||
1456 | sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8); | - | ||||||||||||||||||
1457 | } never executed: end of block | 0 | ||||||||||||||||||
1458 | return rc; never executed: return rc; | 0 | ||||||||||||||||||
1459 | } | - | ||||||||||||||||||
1460 | - | |||||||||||||||||||
1461 | /* Handle negative integers in a single step. This is needed in the | - | ||||||||||||||||||
1462 | ** case when the value is -9223372036854775808. | - | ||||||||||||||||||
1463 | */ | - | ||||||||||||||||||
1464 | if( op==TK_UMINUS
| 83-646 | ||||||||||||||||||
1465 | && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
| 8-42 | ||||||||||||||||||
1466 | pExpr = pExpr->pLeft; | - | ||||||||||||||||||
1467 | op = pExpr->op; | - | ||||||||||||||||||
1468 | negInt = -1; | - | ||||||||||||||||||
1469 | zNeg = "-"; | - | ||||||||||||||||||
1470 | } executed 75 times by 1 test: end of block Executed by:
| 75 | ||||||||||||||||||
1471 | - | |||||||||||||||||||
1472 | if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
| 61-596 | ||||||||||||||||||
1473 | pVal = valueNew(db, pCtx); | - | ||||||||||||||||||
1474 | if( pVal==0 ) goto no_mem; never executed: goto no_mem;
| 0-652 | ||||||||||||||||||
1475 | if( ExprHasProperty(pExpr, EP_IntValue) ){
| 236-416 | ||||||||||||||||||
1476 | sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); | - | ||||||||||||||||||
1477 | }else{ executed 416 times by 1 test: end of block Executed by:
| 416 | ||||||||||||||||||
1478 | zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); | - | ||||||||||||||||||
1479 | if( zVal==0 ) goto no_mem; never executed: goto no_mem;
| 0-236 | ||||||||||||||||||
1480 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); | - | ||||||||||||||||||
1481 | } executed 236 times by 1 test: end of block Executed by:
| 236 | ||||||||||||||||||
1482 | if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
| 61-458 | ||||||||||||||||||
1483 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); | - | ||||||||||||||||||
1484 | }else{ executed 319 times by 1 test: end of block Executed by:
| 319 | ||||||||||||||||||
1485 | sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); | - | ||||||||||||||||||
1486 | } executed 333 times by 1 test: end of block Executed by:
| 333 | ||||||||||||||||||
1487 | if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; executed 516 times by 1 test: pVal->flags &= ~0x0002; Executed by:
| 136-516 | ||||||||||||||||||
1488 | if( enc!=SQLITE_UTF8 ){
| 6-646 | ||||||||||||||||||
1489 | rc = sqlite3VdbeChangeEncoding(pVal, enc); | - | ||||||||||||||||||
1490 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||
1491 | }else if( op==TK_UMINUS ) { executed 652 times by 1 test: end of block Executed by:
| 8-652 | ||||||||||||||||||
1492 | /* This branch happens for multiple negative signs. Ex: -(-5) */ | - | ||||||||||||||||||
1493 | if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
| 0-8 | ||||||||||||||||||
1494 | && pVal!=0
| 0-8 | ||||||||||||||||||
1495 | ){ | - | ||||||||||||||||||
1496 | sqlite3VdbeMemNumerify(pVal); | - | ||||||||||||||||||
1497 | if( pVal->flags & MEM_Real ){
| 0-8 | ||||||||||||||||||
1498 | pVal->u.r = -pVal->u.r; | - | ||||||||||||||||||
1499 | }else if( pVal->u.i==SMALLEST_INT64 ){ never executed: end of block
| 0-5 | ||||||||||||||||||
1500 | pVal->u.r = -(double)SMALLEST_INT64; | - | ||||||||||||||||||
1501 | MemSetTypeFlag(pVal, MEM_Real); | - | ||||||||||||||||||
1502 | }else{ executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||
1503 | pVal->u.i = -pVal->u.i; | - | ||||||||||||||||||
1504 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
1505 | sqlite3ValueApplyAffinity(pVal, affinity, enc); | - | ||||||||||||||||||
1506 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||||||||||||||
1507 | }else if( op==TK_NULL ){ executed 8 times by 1 test: end of block Executed by:
| 8-60 | ||||||||||||||||||
1508 | pVal = valueNew(db, pCtx); | - | ||||||||||||||||||
1509 | if( pVal==0 ) goto no_mem; never executed: goto no_mem;
| 0-9 | ||||||||||||||||||
1510 | sqlite3VdbeMemNumerify(pVal); | - | ||||||||||||||||||
1511 | } executed 9 times by 1 test: end of block Executed by:
| 9 | ||||||||||||||||||
1512 | #ifndef SQLITE_OMIT_BLOB_LITERAL | - | ||||||||||||||||||
1513 | else if( op==TK_BLOB ){
| 7-53 | ||||||||||||||||||
1514 | int nVal; | - | ||||||||||||||||||
1515 | assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); | - | ||||||||||||||||||
1516 | assert( pExpr->u.zToken[1]=='\'' ); | - | ||||||||||||||||||
1517 | pVal = valueNew(db, pCtx); | - | ||||||||||||||||||
1518 | if( !pVal ) goto no_mem; never executed: goto no_mem;
| 0-7 | ||||||||||||||||||
1519 | zVal = &pExpr->u.zToken[2]; | - | ||||||||||||||||||
1520 | nVal = sqlite3Strlen30(zVal)-1; | - | ||||||||||||||||||
1521 | assert( zVal[nVal]=='\'' ); | - | ||||||||||||||||||
1522 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, | - | ||||||||||||||||||
1523 | 0, SQLITE_DYNAMIC); | - | ||||||||||||||||||
1524 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||
1525 | #endif | - | ||||||||||||||||||
1526 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1527 | else if( op==TK_FUNCTION && pCtx!=0 ){ | - | ||||||||||||||||||
1528 | rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); | - | ||||||||||||||||||
1529 | } | - | ||||||||||||||||||
1530 | #endif | - | ||||||||||||||||||
1531 | else if( op==TK_TRUEFALSE ){
| 10-43 | ||||||||||||||||||
1532 | pVal = valueNew(db, pCtx); | - | ||||||||||||||||||
1533 | pVal->flags = MEM_Int; | - | ||||||||||||||||||
1534 | pVal->u.i = pExpr->u.zToken[4]==0; | - | ||||||||||||||||||
1535 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||
1536 | - | |||||||||||||||||||
1537 | *ppVal = pVal; | - | ||||||||||||||||||
1538 | return rc; executed 729 times by 1 test: return rc; Executed by:
| 729 | ||||||||||||||||||
1539 | - | |||||||||||||||||||
1540 | no_mem: | - | ||||||||||||||||||
1541 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1542 | if( pCtx==0 || pCtx->pParse->nErr==0 ) | - | ||||||||||||||||||
1543 | #endif | - | ||||||||||||||||||
1544 | sqlite3OomFault(db); | - | ||||||||||||||||||
1545 | sqlite3DbFree(db, zVal); | - | ||||||||||||||||||
1546 | assert( *ppVal==0 ); | - | ||||||||||||||||||
1547 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1548 | if( pCtx==0 ) sqlite3ValueFree(pVal); | - | ||||||||||||||||||
1549 | #else | - | ||||||||||||||||||
1550 | assert( pCtx==0 ); sqlite3ValueFree(pVal); | - | ||||||||||||||||||
1551 | #endif | - | ||||||||||||||||||
1552 | return SQLITE_NOMEM_BKPT; never executed: return 7; | 0 | ||||||||||||||||||
1553 | } | - | ||||||||||||||||||
1554 | - | |||||||||||||||||||
1555 | /* | - | ||||||||||||||||||
1556 | ** Create a new sqlite3_value object, containing the value of pExpr. | - | ||||||||||||||||||
1557 | ** | - | ||||||||||||||||||
1558 | ** This only works for very simple expressions that consist of one constant | - | ||||||||||||||||||
1559 | ** token (i.e. "5", "5.1", "'a string'"). If the expression can | - | ||||||||||||||||||
1560 | ** be converted directly into a value, then the value is allocated and | - | ||||||||||||||||||
1561 | ** a pointer written to *ppVal. The caller is responsible for deallocating | - | ||||||||||||||||||
1562 | ** the value by passing it to sqlite3ValueFree() later on. If the expression | - | ||||||||||||||||||
1563 | ** cannot be converted to a value, then *ppVal is set to NULL. | - | ||||||||||||||||||
1564 | */ | - | ||||||||||||||||||
1565 | int sqlite3ValueFromExpr( | - | ||||||||||||||||||
1566 | sqlite3 *db, /* The database connection */ | - | ||||||||||||||||||
1567 | Expr *pExpr, /* The expression to evaluate */ | - | ||||||||||||||||||
1568 | u8 enc, /* Encoding to use */ | - | ||||||||||||||||||
1569 | u8 affinity, /* Affinity to use */ | - | ||||||||||||||||||
1570 | sqlite3_value **ppVal /* Write the new value here */ | - | ||||||||||||||||||
1571 | ){ | - | ||||||||||||||||||
1572 | return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0; executed 559721 times by 435 tests: return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0; Executed by:
| 721-559721 | ||||||||||||||||||
1573 | } | - | ||||||||||||||||||
1574 | - | |||||||||||||||||||
1575 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||
1576 | /* | - | ||||||||||||||||||
1577 | ** The implementation of the sqlite_record() function. This function accepts | - | ||||||||||||||||||
1578 | ** a single argument of any type. The return value is a formatted database | - | ||||||||||||||||||
1579 | ** record (a blob) containing the argument value. | - | ||||||||||||||||||
1580 | ** | - | ||||||||||||||||||
1581 | ** This is used to convert the value stored in the 'sample' column of the | - | ||||||||||||||||||
1582 | ** sqlite_stat3 table to the record format SQLite uses internally. | - | ||||||||||||||||||
1583 | */ | - | ||||||||||||||||||
1584 | static void recordFunc( | - | ||||||||||||||||||
1585 | sqlite3_context *context, | - | ||||||||||||||||||
1586 | int argc, | - | ||||||||||||||||||
1587 | sqlite3_value **argv | - | ||||||||||||||||||
1588 | ){ | - | ||||||||||||||||||
1589 | const int file_format = 1; | - | ||||||||||||||||||
1590 | u32 iSerial; /* Serial type */ | - | ||||||||||||||||||
1591 | int nSerial; /* Bytes of space for iSerial as varint */ | - | ||||||||||||||||||
1592 | u32 nVal; /* Bytes of space required for argv[0] */ | - | ||||||||||||||||||
1593 | int nRet; | - | ||||||||||||||||||
1594 | sqlite3 *db; | - | ||||||||||||||||||
1595 | u8 *aRet; | - | ||||||||||||||||||
1596 | - | |||||||||||||||||||
1597 | UNUSED_PARAMETER( argc ); | - | ||||||||||||||||||
1598 | iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal); | - | ||||||||||||||||||
1599 | nSerial = sqlite3VarintLen(iSerial); | - | ||||||||||||||||||
1600 | db = sqlite3_context_db_handle(context); | - | ||||||||||||||||||
1601 | - | |||||||||||||||||||
1602 | nRet = 1 + nSerial + nVal; | - | ||||||||||||||||||
1603 | aRet = sqlite3DbMallocRawNN(db, nRet); | - | ||||||||||||||||||
1604 | if( aRet==0 ){ | - | ||||||||||||||||||
1605 | sqlite3_result_error_nomem(context); | - | ||||||||||||||||||
1606 | }else{ | - | ||||||||||||||||||
1607 | aRet[0] = nSerial+1; | - | ||||||||||||||||||
1608 | putVarint32(&aRet[1], iSerial); | - | ||||||||||||||||||
1609 | sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial); | - | ||||||||||||||||||
1610 | sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT); | - | ||||||||||||||||||
1611 | sqlite3DbFreeNN(db, aRet); | - | ||||||||||||||||||
1612 | } | - | ||||||||||||||||||
1613 | } | - | ||||||||||||||||||
1614 | - | |||||||||||||||||||
1615 | /* | - | ||||||||||||||||||
1616 | ** Register built-in functions used to help read ANALYZE data. | - | ||||||||||||||||||
1617 | */ | - | ||||||||||||||||||
1618 | void sqlite3AnalyzeFunctions(void){ | - | ||||||||||||||||||
1619 | static FuncDef aAnalyzeTableFuncs[] = { | - | ||||||||||||||||||
1620 | FUNCTION(sqlite_record, 1, 0, 0, recordFunc), | - | ||||||||||||||||||
1621 | }; | - | ||||||||||||||||||
1622 | sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs)); | - | ||||||||||||||||||
1623 | } | - | ||||||||||||||||||
1624 | - | |||||||||||||||||||
1625 | /* | - | ||||||||||||||||||
1626 | ** Attempt to extract a value from pExpr and use it to construct *ppVal. | - | ||||||||||||||||||
1627 | ** | - | ||||||||||||||||||
1628 | ** If pAlloc is not NULL, then an UnpackedRecord object is created for | - | ||||||||||||||||||
1629 | ** pAlloc if one does not exist and the new value is added to the | - | ||||||||||||||||||
1630 | ** UnpackedRecord object. | - | ||||||||||||||||||
1631 | ** | - | ||||||||||||||||||
1632 | ** A value is extracted in the following cases: | - | ||||||||||||||||||
1633 | ** | - | ||||||||||||||||||
1634 | ** * (pExpr==0). In this case the value is assumed to be an SQL NULL, | - | ||||||||||||||||||
1635 | ** | - | ||||||||||||||||||
1636 | ** * The expression is a bound variable, and this is a reprepare, or | - | ||||||||||||||||||
1637 | ** | - | ||||||||||||||||||
1638 | ** * The expression is a literal value. | - | ||||||||||||||||||
1639 | ** | - | ||||||||||||||||||
1640 | ** On success, *ppVal is made to point to the extracted value. The caller | - | ||||||||||||||||||
1641 | ** is responsible for ensuring that the value is eventually freed. | - | ||||||||||||||||||
1642 | */ | - | ||||||||||||||||||
1643 | static int stat4ValueFromExpr( | - | ||||||||||||||||||
1644 | Parse *pParse, /* Parse context */ | - | ||||||||||||||||||
1645 | Expr *pExpr, /* The expression to extract a value from */ | - | ||||||||||||||||||
1646 | u8 affinity, /* Affinity to use */ | - | ||||||||||||||||||
1647 | struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */ | - | ||||||||||||||||||
1648 | sqlite3_value **ppVal /* OUT: New value object (or NULL) */ | - | ||||||||||||||||||
1649 | ){ | - | ||||||||||||||||||
1650 | int rc = SQLITE_OK; | - | ||||||||||||||||||
1651 | sqlite3_value *pVal = 0; | - | ||||||||||||||||||
1652 | sqlite3 *db = pParse->db; | - | ||||||||||||||||||
1653 | - | |||||||||||||||||||
1654 | /* Skip over any TK_COLLATE nodes */ | - | ||||||||||||||||||
1655 | pExpr = sqlite3ExprSkipCollate(pExpr); | - | ||||||||||||||||||
1656 | - | |||||||||||||||||||
1657 | assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE ); | - | ||||||||||||||||||
1658 | if( !pExpr ){ | - | ||||||||||||||||||
1659 | pVal = valueNew(db, pAlloc); | - | ||||||||||||||||||
1660 | if( pVal ){ | - | ||||||||||||||||||
1661 | sqlite3VdbeMemSetNull((Mem*)pVal); | - | ||||||||||||||||||
1662 | } | - | ||||||||||||||||||
1663 | }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ | - | ||||||||||||||||||
1664 | Vdbe *v; | - | ||||||||||||||||||
1665 | int iBindVar = pExpr->iColumn; | - | ||||||||||||||||||
1666 | sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar); | - | ||||||||||||||||||
1667 | if( (v = pParse->pReprepare)!=0 ){ | - | ||||||||||||||||||
1668 | pVal = valueNew(db, pAlloc); | - | ||||||||||||||||||
1669 | if( pVal ){ | - | ||||||||||||||||||
1670 | rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); | - | ||||||||||||||||||
1671 | sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); | - | ||||||||||||||||||
1672 | pVal->db = pParse->db; | - | ||||||||||||||||||
1673 | } | - | ||||||||||||||||||
1674 | } | - | ||||||||||||||||||
1675 | }else{ | - | ||||||||||||||||||
1676 | rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc); | - | ||||||||||||||||||
1677 | } | - | ||||||||||||||||||
1678 | - | |||||||||||||||||||
1679 | assert( pVal==0 || pVal->db==db ); | - | ||||||||||||||||||
1680 | *ppVal = pVal; | - | ||||||||||||||||||
1681 | return rc; | - | ||||||||||||||||||
1682 | } | - | ||||||||||||||||||
1683 | - | |||||||||||||||||||
1684 | /* | - | ||||||||||||||||||
1685 | ** This function is used to allocate and populate UnpackedRecord | - | ||||||||||||||||||
1686 | ** structures intended to be compared against sample index keys stored | - | ||||||||||||||||||
1687 | ** in the sqlite_stat4 table. | - | ||||||||||||||||||
1688 | ** | - | ||||||||||||||||||
1689 | ** A single call to this function populates zero or more fields of the | - | ||||||||||||||||||
1690 | ** record starting with field iVal (fields are numbered from left to | - | ||||||||||||||||||
1691 | ** right starting with 0). A single field is populated if: | - | ||||||||||||||||||
1692 | ** | - | ||||||||||||||||||
1693 | ** * (pExpr==0). In this case the value is assumed to be an SQL NULL, | - | ||||||||||||||||||
1694 | ** | - | ||||||||||||||||||
1695 | ** * The expression is a bound variable, and this is a reprepare, or | - | ||||||||||||||||||
1696 | ** | - | ||||||||||||||||||
1697 | ** * The sqlite3ValueFromExpr() function is able to extract a value | - | ||||||||||||||||||
1698 | ** from the expression (i.e. the expression is a literal value). | - | ||||||||||||||||||
1699 | ** | - | ||||||||||||||||||
1700 | ** Or, if pExpr is a TK_VECTOR, one field is populated for each of the | - | ||||||||||||||||||
1701 | ** vector components that match either of the two latter criteria listed | - | ||||||||||||||||||
1702 | ** above. | - | ||||||||||||||||||
1703 | ** | - | ||||||||||||||||||
1704 | ** Before any value is appended to the record, the affinity of the | - | ||||||||||||||||||
1705 | ** corresponding column within index pIdx is applied to it. Before | - | ||||||||||||||||||
1706 | ** this function returns, output parameter *pnExtract is set to the | - | ||||||||||||||||||
1707 | ** number of values appended to the record. | - | ||||||||||||||||||
1708 | ** | - | ||||||||||||||||||
1709 | ** When this function is called, *ppRec must either point to an object | - | ||||||||||||||||||
1710 | ** allocated by an earlier call to this function, or must be NULL. If it | - | ||||||||||||||||||
1711 | ** is NULL and a value can be successfully extracted, a new UnpackedRecord | - | ||||||||||||||||||
1712 | ** is allocated (and *ppRec set to point to it) before returning. | - | ||||||||||||||||||
1713 | ** | - | ||||||||||||||||||
1714 | ** Unless an error is encountered, SQLITE_OK is returned. It is not an | - | ||||||||||||||||||
1715 | ** error if a value cannot be extracted from pExpr. If an error does | - | ||||||||||||||||||
1716 | ** occur, an SQLite error code is returned. | - | ||||||||||||||||||
1717 | */ | - | ||||||||||||||||||
1718 | int sqlite3Stat4ProbeSetValue( | - | ||||||||||||||||||
1719 | Parse *pParse, /* Parse context */ | - | ||||||||||||||||||
1720 | Index *pIdx, /* Index being probed */ | - | ||||||||||||||||||
1721 | UnpackedRecord **ppRec, /* IN/OUT: Probe record */ | - | ||||||||||||||||||
1722 | Expr *pExpr, /* The expression to extract a value from */ | - | ||||||||||||||||||
1723 | int nElem, /* Maximum number of values to append */ | - | ||||||||||||||||||
1724 | int iVal, /* Array element to populate */ | - | ||||||||||||||||||
1725 | int *pnExtract /* OUT: Values appended to the record */ | - | ||||||||||||||||||
1726 | ){ | - | ||||||||||||||||||
1727 | int rc = SQLITE_OK; | - | ||||||||||||||||||
1728 | int nExtract = 0; | - | ||||||||||||||||||
1729 | - | |||||||||||||||||||
1730 | if( pExpr==0 || pExpr->op!=TK_SELECT ){ | - | ||||||||||||||||||
1731 | int i; | - | ||||||||||||||||||
1732 | struct ValueNewStat4Ctx alloc; | - | ||||||||||||||||||
1733 | - | |||||||||||||||||||
1734 | alloc.pParse = pParse; | - | ||||||||||||||||||
1735 | alloc.pIdx = pIdx; | - | ||||||||||||||||||
1736 | alloc.ppRec = ppRec; | - | ||||||||||||||||||
1737 | - | |||||||||||||||||||
1738 | for(i=0; i<nElem; i++){ | - | ||||||||||||||||||
1739 | sqlite3_value *pVal = 0; | - | ||||||||||||||||||
1740 | Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0); | - | ||||||||||||||||||
1741 | u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i); | - | ||||||||||||||||||
1742 | alloc.iVal = iVal+i; | - | ||||||||||||||||||
1743 | rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal); | - | ||||||||||||||||||
1744 | if( !pVal ) break; | - | ||||||||||||||||||
1745 | nExtract++; | - | ||||||||||||||||||
1746 | } | - | ||||||||||||||||||
1747 | } | - | ||||||||||||||||||
1748 | - | |||||||||||||||||||
1749 | *pnExtract = nExtract; | - | ||||||||||||||||||
1750 | return rc; | - | ||||||||||||||||||
1751 | } | - | ||||||||||||||||||
1752 | - | |||||||||||||||||||
1753 | /* | - | ||||||||||||||||||
1754 | ** Attempt to extract a value from expression pExpr using the methods | - | ||||||||||||||||||
1755 | ** as described for sqlite3Stat4ProbeSetValue() above. | - | ||||||||||||||||||
1756 | ** | - | ||||||||||||||||||
1757 | ** If successful, set *ppVal to point to a new value object and return | - | ||||||||||||||||||
1758 | ** SQLITE_OK. If no value can be extracted, but no other error occurs | - | ||||||||||||||||||
1759 | ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error | - | ||||||||||||||||||
1760 | ** does occur, return an SQLite error code. The final value of *ppVal | - | ||||||||||||||||||
1761 | ** is undefined in this case. | - | ||||||||||||||||||
1762 | */ | - | ||||||||||||||||||
1763 | int sqlite3Stat4ValueFromExpr( | - | ||||||||||||||||||
1764 | Parse *pParse, /* Parse context */ | - | ||||||||||||||||||
1765 | Expr *pExpr, /* The expression to extract a value from */ | - | ||||||||||||||||||
1766 | u8 affinity, /* Affinity to use */ | - | ||||||||||||||||||
1767 | sqlite3_value **ppVal /* OUT: New value object (or NULL) */ | - | ||||||||||||||||||
1768 | ){ | - | ||||||||||||||||||
1769 | return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal); | - | ||||||||||||||||||
1770 | } | - | ||||||||||||||||||
1771 | - | |||||||||||||||||||
1772 | /* | - | ||||||||||||||||||
1773 | ** Extract the iCol-th column from the nRec-byte record in pRec. Write | - | ||||||||||||||||||
1774 | ** the column value into *ppVal. If *ppVal is initially NULL then a new | - | ||||||||||||||||||
1775 | ** sqlite3_value object is allocated. | - | ||||||||||||||||||
1776 | ** | - | ||||||||||||||||||
1777 | ** If *ppVal is initially NULL then the caller is responsible for | - | ||||||||||||||||||
1778 | ** ensuring that the value written into *ppVal is eventually freed. | - | ||||||||||||||||||
1779 | */ | - | ||||||||||||||||||
1780 | int sqlite3Stat4Column( | - | ||||||||||||||||||
1781 | sqlite3 *db, /* Database handle */ | - | ||||||||||||||||||
1782 | const void *pRec, /* Pointer to buffer containing record */ | - | ||||||||||||||||||
1783 | int nRec, /* Size of buffer pRec in bytes */ | - | ||||||||||||||||||
1784 | int iCol, /* Column to extract */ | - | ||||||||||||||||||
1785 | sqlite3_value **ppVal /* OUT: Extracted value */ | - | ||||||||||||||||||
1786 | ){ | - | ||||||||||||||||||
1787 | u32 t = 0; /* a column type code */ | - | ||||||||||||||||||
1788 | int nHdr; /* Size of the header in the record */ | - | ||||||||||||||||||
1789 | int iHdr; /* Next unread header byte */ | - | ||||||||||||||||||
1790 | int iField; /* Next unread data byte */ | - | ||||||||||||||||||
1791 | int szField = 0; /* Size of the current data field */ | - | ||||||||||||||||||
1792 | int i; /* Column index */ | - | ||||||||||||||||||
1793 | u8 *a = (u8*)pRec; /* Typecast byte array */ | - | ||||||||||||||||||
1794 | Mem *pMem = *ppVal; /* Write result into this Mem object */ | - | ||||||||||||||||||
1795 | - | |||||||||||||||||||
1796 | assert( iCol>0 ); | - | ||||||||||||||||||
1797 | iHdr = getVarint32(a, nHdr); | - | ||||||||||||||||||
1798 | if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT; | - | ||||||||||||||||||
1799 | iField = nHdr; | - | ||||||||||||||||||
1800 | for(i=0; i<=iCol; i++){ | - | ||||||||||||||||||
1801 | iHdr += getVarint32(&a[iHdr], t); | - | ||||||||||||||||||
1802 | testcase( iHdr==nHdr ); | - | ||||||||||||||||||
1803 | testcase( iHdr==nHdr+1 ); | - | ||||||||||||||||||
1804 | if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT; | - | ||||||||||||||||||
1805 | szField = sqlite3VdbeSerialTypeLen(t); | - | ||||||||||||||||||
1806 | iField += szField; | - | ||||||||||||||||||
1807 | } | - | ||||||||||||||||||
1808 | testcase( iField==nRec ); | - | ||||||||||||||||||
1809 | testcase( iField==nRec+1 ); | - | ||||||||||||||||||
1810 | if( iField>nRec ) return SQLITE_CORRUPT_BKPT; | - | ||||||||||||||||||
1811 | if( pMem==0 ){ | - | ||||||||||||||||||
1812 | pMem = *ppVal = sqlite3ValueNew(db); | - | ||||||||||||||||||
1813 | if( pMem==0 ) return SQLITE_NOMEM_BKPT; | - | ||||||||||||||||||
1814 | } | - | ||||||||||||||||||
1815 | sqlite3VdbeSerialGet(&a[iField-szField], t, pMem); | - | ||||||||||||||||||
1816 | pMem->enc = ENC(db); | - | ||||||||||||||||||
1817 | return SQLITE_OK; | - | ||||||||||||||||||
1818 | } | - | ||||||||||||||||||
1819 | - | |||||||||||||||||||
1820 | /* | - | ||||||||||||||||||
1821 | ** Unless it is NULL, the argument must be an UnpackedRecord object returned | - | ||||||||||||||||||
1822 | ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes | - | ||||||||||||||||||
1823 | ** the object. | - | ||||||||||||||||||
1824 | */ | - | ||||||||||||||||||
1825 | void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){ | - | ||||||||||||||||||
1826 | if( pRec ){ | - | ||||||||||||||||||
1827 | int i; | - | ||||||||||||||||||
1828 | int nCol = pRec->pKeyInfo->nAllField; | - | ||||||||||||||||||
1829 | Mem *aMem = pRec->aMem; | - | ||||||||||||||||||
1830 | sqlite3 *db = aMem[0].db; | - | ||||||||||||||||||
1831 | for(i=0; i<nCol; i++){ | - | ||||||||||||||||||
1832 | sqlite3VdbeMemRelease(&aMem[i]); | - | ||||||||||||||||||
1833 | } | - | ||||||||||||||||||
1834 | sqlite3KeyInfoUnref(pRec->pKeyInfo); | - | ||||||||||||||||||
1835 | sqlite3DbFreeNN(db, pRec); | - | ||||||||||||||||||
1836 | } | - | ||||||||||||||||||
1837 | } | - | ||||||||||||||||||
1838 | #endif /* ifdef SQLITE_ENABLE_STAT4 */ | - | ||||||||||||||||||
1839 | - | |||||||||||||||||||
1840 | /* | - | ||||||||||||||||||
1841 | ** Change the string value of an sqlite3_value object | - | ||||||||||||||||||
1842 | */ | - | ||||||||||||||||||
1843 | void sqlite3ValueSetStr( | - | ||||||||||||||||||
1844 | sqlite3_value *v, /* Value to be set */ | - | ||||||||||||||||||
1845 | int n, /* Length of string z */ | - | ||||||||||||||||||
1846 | const void *z, /* Text of the new string */ | - | ||||||||||||||||||
1847 | u8 enc, /* Encoding to use */ | - | ||||||||||||||||||
1848 | void (*xDel)(void*) /* Destructor for the string */ | - | ||||||||||||||||||
1849 | ){ | - | ||||||||||||||||||
1850 | if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); executed 16791 times by 19 tests: sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); Executed by:
| 0-16791 | ||||||||||||||||||
1851 | } executed 16791 times by 19 tests: end of block Executed by:
| 16791 | ||||||||||||||||||
1852 | - | |||||||||||||||||||
1853 | /* | - | ||||||||||||||||||
1854 | ** Free an sqlite3_value object | - | ||||||||||||||||||
1855 | */ | - | ||||||||||||||||||
1856 | void sqlite3ValueFree(sqlite3_value *v){ | - | ||||||||||||||||||
1857 | if( !v ) return; executed 47071 times by 50 tests: return; Executed by:
| 22819-47071 | ||||||||||||||||||
1858 | sqlite3VdbeMemRelease((Mem *)v); | - | ||||||||||||||||||
1859 | sqlite3DbFreeNN(((Mem*)v)->db, v); | - | ||||||||||||||||||
1860 | } executed 22819 times by 19 tests: end of block Executed by:
| 22819 | ||||||||||||||||||
1861 | - | |||||||||||||||||||
1862 | /* | - | ||||||||||||||||||
1863 | ** The sqlite3ValueBytes() routine returns the number of bytes in the | - | ||||||||||||||||||
1864 | ** sqlite3_value object assuming that it uses the encoding "enc". | - | ||||||||||||||||||
1865 | ** The valueBytes() routine is a helper function. | - | ||||||||||||||||||
1866 | */ | - | ||||||||||||||||||
1867 | static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){ | - | ||||||||||||||||||
1868 | return valueToText(pVal, enc)!=0 ? pVal->n : 0; executed 166 times by 1 test: return valueToText(pVal, enc)!=0 ? pVal->n : 0; Executed by:
| 0-166 | ||||||||||||||||||
1869 | } | - | ||||||||||||||||||
1870 | int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ | - | ||||||||||||||||||
1871 | Mem *p = (Mem*)pVal; | - | ||||||||||||||||||
1872 | assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 ); | - | ||||||||||||||||||
1873 | if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
| 147-231763 | ||||||||||||||||||
1874 | return p->n; executed 231616 times by 1 test: return p->n; Executed by:
| 231616 | ||||||||||||||||||
1875 | } | - | ||||||||||||||||||
1876 | if( (p->flags & MEM_Blob)!=0 ){
| 940-170055 | ||||||||||||||||||
1877 | if( p->flags & MEM_Zero ){
| 9-170046 | ||||||||||||||||||
1878 | return p->n + p->u.nZero; executed 9 times by 1 test: return p->n + p->u.nZero; Executed by:
| 9 | ||||||||||||||||||
1879 | }else{ | - | ||||||||||||||||||
1880 | return p->n; executed 170046 times by 1 test: return p->n; Executed by:
| 170046 | ||||||||||||||||||
1881 | } | - | ||||||||||||||||||
1882 | } | - | ||||||||||||||||||
1883 | if( p->flags & MEM_Null ) return 0; executed 774 times by 1 test: return 0; Executed by:
| 166-774 | ||||||||||||||||||
1884 | return valueBytes(pVal, enc); executed 166 times by 1 test: return valueBytes(pVal, enc); Executed by:
| 166 | ||||||||||||||||||
1885 | } | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |