Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.03.c

Issue 1636873003: Try for backport (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@zzsql_import3_10_2_websql_backport
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file vdbemem.c *****************************************/
2 /*
3 ** 2004 May 26
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 **
14 ** This file contains code use to manipulate "Mem" structure. A "Mem"
15 ** stores a single value in the VDBE. Mem is an opaque structure visible
16 ** only within the VDBE. Interface routines refer to a Mem using the
17 ** name sqlite_value
18 */
19 /* #include "sqliteInt.h" */
20 /* #include "vdbeInt.h" */
21
22 #ifdef SQLITE_DEBUG
23 /*
24 ** Check invariants on a Mem object.
25 **
26 ** This routine is intended for use inside of assert() statements, like
27 ** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
28 */
29 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
30 /* If MEM_Dyn is set then Mem.xDel!=0.
31 ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
32 */
33 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
34
35 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
36 ** ensure that if Mem.szMalloc>0 then it is safe to do
37 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
38 ** That saves a few cycles in inner loops. */
39 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
40
41 /* Cannot be both MEM_Int and MEM_Real at the same time */
42 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
43
44 /* The szMalloc field holds the correct memory allocation size */
45 assert( p->szMalloc==0
46 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
47
48 /* If p holds a string or blob, the Mem.z must point to exactly
49 ** one of the following:
50 **
51 ** (1) Memory in Mem.zMalloc and managed by the Mem object
52 ** (2) Memory to be freed using Mem.xDel
53 ** (3) An ephemeral string or blob
54 ** (4) A static string or blob
55 */
56 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
57 assert(
58 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
59 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
60 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
61 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
62 );
63 }
64 return 1;
65 }
66 #endif
67
68
69 /*
70 ** If pMem is an object with a valid string representation, this routine
71 ** ensures the internal encoding for the string representation is
72 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
73 **
74 ** If pMem is not a string object, or the encoding of the string
75 ** representation is already stored using the requested encoding, then this
76 ** routine is a no-op.
77 **
78 ** SQLITE_OK is returned if the conversion is successful (or not required).
79 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
80 ** between formats.
81 */
82 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
83 #ifndef SQLITE_OMIT_UTF16
84 int rc;
85 #endif
86 assert( (pMem->flags&MEM_RowSet)==0 );
87 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
88 || desiredEnc==SQLITE_UTF16BE );
89 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
90 return SQLITE_OK;
91 }
92 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
93 #ifdef SQLITE_OMIT_UTF16
94 return SQLITE_ERROR;
95 #else
96
97 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
98 ** then the encoding of the value may not have changed.
99 */
100 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
101 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
102 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
103 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
104 return rc;
105 #endif
106 }
107
108 /*
109 ** Make sure pMem->z points to a writable allocation of at least
110 ** min(n,32) bytes.
111 **
112 ** If the bPreserve argument is true, then copy of the content of
113 ** pMem->z into the new allocation. pMem must be either a string or
114 ** blob if bPreserve is true. If bPreserve is false, any prior content
115 ** in pMem->z is discarded.
116 */
117 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPre serve){
118 assert( sqlite3VdbeCheckMemInvariants(pMem) );
119 assert( (pMem->flags&MEM_RowSet)==0 );
120
121 /* If the bPreserve flag is set to true, then the memory cell must already
122 ** contain a valid string or blob value. */
123 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
124 testcase( bPreserve && pMem->z==0 );
125
126 assert( pMem->szMalloc==0
127 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
128 if( pMem->szMalloc<n ){
129 if( n<32 ) n = 32;
130 if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
131 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
132 bPreserve = 0;
133 }else{
134 if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
135 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
136 }
137 if( pMem->zMalloc==0 ){
138 sqlite3VdbeMemSetNull(pMem);
139 pMem->z = 0;
140 pMem->szMalloc = 0;
141 return SQLITE_NOMEM;
142 }else{
143 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
144 }
145 }
146
147 if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
148 memcpy(pMem->zMalloc, pMem->z, pMem->n);
149 }
150 if( (pMem->flags&MEM_Dyn)!=0 ){
151 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
152 pMem->xDel((void *)(pMem->z));
153 }
154
155 pMem->z = pMem->zMalloc;
156 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
157 return SQLITE_OK;
158 }
159
160 /*
161 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
162 ** If pMem->zMalloc already meets or exceeds the requested size, this
163 ** routine is a no-op.
164 **
165 ** Any prior string or blob content in the pMem object may be discarded.
166 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
167 ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
168 ** values are preserved.
169 **
170 ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
171 ** if unable to complete the resizing.
172 */
173 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
174 assert( szNew>0 );
175 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
176 if( pMem->szMalloc<szNew ){
177 return sqlite3VdbeMemGrow(pMem, szNew, 0);
178 }
179 assert( (pMem->flags & MEM_Dyn)==0 );
180 pMem->z = pMem->zMalloc;
181 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
182 return SQLITE_OK;
183 }
184
185 /*
186 ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
187 ** MEM.zMalloc, where it can be safely written.
188 **
189 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
190 */
191 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
192 int f;
193 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
194 assert( (pMem->flags&MEM_RowSet)==0 );
195 ExpandBlob(pMem);
196 f = pMem->flags;
197 if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
198 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
199 return SQLITE_NOMEM;
200 }
201 pMem->z[pMem->n] = 0;
202 pMem->z[pMem->n+1] = 0;
203 pMem->flags |= MEM_Term;
204 }
205 pMem->flags &= ~MEM_Ephem;
206 #ifdef SQLITE_DEBUG
207 pMem->pScopyFrom = 0;
208 #endif
209
210 return SQLITE_OK;
211 }
212
213 /*
214 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
215 ** blob stored in dynamically allocated space.
216 */
217 #ifndef SQLITE_OMIT_INCRBLOB
218 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
219 if( pMem->flags & MEM_Zero ){
220 int nByte;
221 assert( pMem->flags&MEM_Blob );
222 assert( (pMem->flags&MEM_RowSet)==0 );
223 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
224
225 /* Set nByte to the number of bytes required to store the expanded blob. */
226 nByte = pMem->n + pMem->u.nZero;
227 if( nByte<=0 ){
228 nByte = 1;
229 }
230 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
231 return SQLITE_NOMEM;
232 }
233
234 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
235 pMem->n += pMem->u.nZero;
236 pMem->flags &= ~(MEM_Zero|MEM_Term);
237 }
238 return SQLITE_OK;
239 }
240 #endif
241
242 /*
243 ** It is already known that pMem contains an unterminated string.
244 ** Add the zero terminator.
245 */
246 static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
247 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
248 return SQLITE_NOMEM;
249 }
250 pMem->z[pMem->n] = 0;
251 pMem->z[pMem->n+1] = 0;
252 pMem->flags |= MEM_Term;
253 return SQLITE_OK;
254 }
255
256 /*
257 ** Make sure the given Mem is \u0000 terminated.
258 */
259 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
260 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
261 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
262 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
263 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
264 return SQLITE_OK; /* Nothing to do */
265 }else{
266 return vdbeMemAddTerminator(pMem);
267 }
268 }
269
270 /*
271 ** Add MEM_Str to the set of representations for the given Mem. Numbers
272 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
273 ** is a no-op.
274 **
275 ** Existing representations MEM_Int and MEM_Real are invalidated if
276 ** bForce is true but are retained if bForce is false.
277 **
278 ** A MEM_Null value will never be passed to this function. This function is
279 ** used for converting values to text for returning to the user (i.e. via
280 ** sqlite3_value_text()), or for ensuring that values to be used as btree
281 ** keys are strings. In the former case a NULL pointer is returned the
282 ** user and the latter is an internal programming error.
283 */
284 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
285 int fg = pMem->flags;
286 const int nByte = 32;
287
288 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
289 assert( !(fg&MEM_Zero) );
290 assert( !(fg&(MEM_Str|MEM_Blob)) );
291 assert( fg&(MEM_Int|MEM_Real) );
292 assert( (pMem->flags&MEM_RowSet)==0 );
293 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
294
295
296 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
297 return SQLITE_NOMEM;
298 }
299
300 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
301 ** string representation of the value. Then, if the required encoding
302 ** is UTF-16le or UTF-16be do a translation.
303 **
304 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
305 */
306 if( fg & MEM_Int ){
307 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
308 }else{
309 assert( fg & MEM_Real );
310 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
311 }
312 pMem->n = sqlite3Strlen30(pMem->z);
313 pMem->enc = SQLITE_UTF8;
314 pMem->flags |= MEM_Str|MEM_Term;
315 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
316 sqlite3VdbeChangeEncoding(pMem, enc);
317 return SQLITE_OK;
318 }
319
320 /*
321 ** Memory cell pMem contains the context of an aggregate function.
322 ** This routine calls the finalize method for that function. The
323 ** result of the aggregate is stored back into pMem.
324 **
325 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
326 ** otherwise.
327 */
328 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
329 int rc = SQLITE_OK;
330 if( ALWAYS(pFunc && pFunc->xFinalize) ){
331 sqlite3_context ctx;
332 Mem t;
333 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
334 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
335 memset(&ctx, 0, sizeof(ctx));
336 memset(&t, 0, sizeof(t));
337 t.flags = MEM_Null;
338 t.db = pMem->db;
339 ctx.pOut = &t;
340 ctx.pMem = pMem;
341 ctx.pFunc = pFunc;
342 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
343 assert( (pMem->flags & MEM_Dyn)==0 );
344 if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
345 memcpy(pMem, &t, sizeof(t));
346 rc = ctx.isError;
347 }
348 return rc;
349 }
350
351 /*
352 ** If the memory cell contains a value that must be freed by
353 ** invoking the external callback in Mem.xDel, then this routine
354 ** will free that value. It also sets Mem.flags to MEM_Null.
355 **
356 ** This is a helper routine for sqlite3VdbeMemSetNull() and
357 ** for sqlite3VdbeMemRelease(). Use those other routines as the
358 ** entry point for releasing Mem resources.
359 */
360 static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
361 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
362 assert( VdbeMemDynamic(p) );
363 if( p->flags&MEM_Agg ){
364 sqlite3VdbeMemFinalize(p, p->u.pDef);
365 assert( (p->flags & MEM_Agg)==0 );
366 testcase( p->flags & MEM_Dyn );
367 }
368 if( p->flags&MEM_Dyn ){
369 assert( (p->flags&MEM_RowSet)==0 );
370 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
371 p->xDel((void *)p->z);
372 }else if( p->flags&MEM_RowSet ){
373 sqlite3RowSetClear(p->u.pRowSet);
374 }else if( p->flags&MEM_Frame ){
375 VdbeFrame *pFrame = p->u.pFrame;
376 pFrame->pParent = pFrame->v->pDelFrame;
377 pFrame->v->pDelFrame = pFrame;
378 }
379 p->flags = MEM_Null;
380 }
381
382 /*
383 ** Release memory held by the Mem p, both external memory cleared
384 ** by p->xDel and memory in p->zMalloc.
385 **
386 ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
387 ** the unusual case where there really is memory in p that needs
388 ** to be freed.
389 */
390 static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
391 if( VdbeMemDynamic(p) ){
392 vdbeMemClearExternAndSetNull(p);
393 }
394 if( p->szMalloc ){
395 sqlite3DbFree(p->db, p->zMalloc);
396 p->szMalloc = 0;
397 }
398 p->z = 0;
399 }
400
401 /*
402 ** Release any memory resources held by the Mem. Both the memory that is
403 ** free by Mem.xDel and the Mem.zMalloc allocation are freed.
404 **
405 ** Use this routine prior to clean up prior to abandoning a Mem, or to
406 ** reset a Mem back to its minimum memory utilization.
407 **
408 ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
409 ** prior to inserting new content into the Mem.
410 */
411 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
412 assert( sqlite3VdbeCheckMemInvariants(p) );
413 if( VdbeMemDynamic(p) || p->szMalloc ){
414 vdbeMemClear(p);
415 }
416 }
417
418 /*
419 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
420 ** If the double is out of range of a 64-bit signed integer then
421 ** return the closest available 64-bit signed integer.
422 */
423 static i64 doubleToInt64(double r){
424 #ifdef SQLITE_OMIT_FLOATING_POINT
425 /* When floating-point is omitted, double and int64 are the same thing */
426 return r;
427 #else
428 /*
429 ** Many compilers we encounter do not define constants for the
430 ** minimum and maximum 64-bit integers, or they define them
431 ** inconsistently. And many do not understand the "LL" notation.
432 ** So we define our own static constants here using nothing
433 ** larger than a 32-bit integer constant.
434 */
435 static const i64 maxInt = LARGEST_INT64;
436 static const i64 minInt = SMALLEST_INT64;
437
438 if( r<=(double)minInt ){
439 return minInt;
440 }else if( r>=(double)maxInt ){
441 return maxInt;
442 }else{
443 return (i64)r;
444 }
445 #endif
446 }
447
448 /*
449 ** Return some kind of integer value which is the best we can do
450 ** at representing the value that *pMem describes as an integer.
451 ** If pMem is an integer, then the value is exact. If pMem is
452 ** a floating-point then the value returned is the integer part.
453 ** If pMem is a string or blob, then we make an attempt to convert
454 ** it into an integer and return that. If pMem represents an
455 ** an SQL-NULL value, return 0.
456 **
457 ** If pMem represents a string value, its encoding might be changed.
458 */
459 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
460 int flags;
461 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
462 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
463 flags = pMem->flags;
464 if( flags & MEM_Int ){
465 return pMem->u.i;
466 }else if( flags & MEM_Real ){
467 return doubleToInt64(pMem->u.r);
468 }else if( flags & (MEM_Str|MEM_Blob) ){
469 i64 value = 0;
470 assert( pMem->z || pMem->n==0 );
471 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
472 return value;
473 }else{
474 return 0;
475 }
476 }
477
478 /*
479 ** Return the best representation of pMem that we can get into a
480 ** double. If pMem is already a double or an integer, return its
481 ** value. If it is a string or blob, try to convert it to a double.
482 ** If it is a NULL, return 0.0.
483 */
484 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
485 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
486 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
487 if( pMem->flags & MEM_Real ){
488 return pMem->u.r;
489 }else if( pMem->flags & MEM_Int ){
490 return (double)pMem->u.i;
491 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
492 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
493 double val = (double)0;
494 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
495 return val;
496 }else{
497 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
498 return (double)0;
499 }
500 }
501
502 /*
503 ** The MEM structure is already a MEM_Real. Try to also make it a
504 ** MEM_Int if we can.
505 */
506 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
507 i64 ix;
508 assert( pMem->flags & MEM_Real );
509 assert( (pMem->flags & MEM_RowSet)==0 );
510 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
511 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
512
513 ix = doubleToInt64(pMem->u.r);
514
515 /* Only mark the value as an integer if
516 **
517 ** (1) the round-trip conversion real->int->real is a no-op, and
518 ** (2) The integer is neither the largest nor the smallest
519 ** possible integer (ticket #3922)
520 **
521 ** The second and third terms in the following conditional enforces
522 ** the second condition under the assumption that addition overflow causes
523 ** values to wrap around.
524 */
525 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
526 pMem->u.i = ix;
527 MemSetTypeFlag(pMem, MEM_Int);
528 }
529 }
530
531 /*
532 ** Convert pMem to type integer. Invalidate any prior representations.
533 */
534 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
535 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
536 assert( (pMem->flags & MEM_RowSet)==0 );
537 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
538
539 pMem->u.i = sqlite3VdbeIntValue(pMem);
540 MemSetTypeFlag(pMem, MEM_Int);
541 return SQLITE_OK;
542 }
543
544 /*
545 ** Convert pMem so that it is of type MEM_Real.
546 ** Invalidate any prior representations.
547 */
548 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
549 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
550 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
551
552 pMem->u.r = sqlite3VdbeRealValue(pMem);
553 MemSetTypeFlag(pMem, MEM_Real);
554 return SQLITE_OK;
555 }
556
557 /*
558 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
559 ** Invalidate any prior representations.
560 **
561 ** Every effort is made to force the conversion, even if the input
562 ** is a string that does not look completely like a number. Convert
563 ** as much of the string as we can and ignore the rest.
564 */
565 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
566 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
567 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
568 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
569 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
570 MemSetTypeFlag(pMem, MEM_Int);
571 }else{
572 pMem->u.r = sqlite3VdbeRealValue(pMem);
573 MemSetTypeFlag(pMem, MEM_Real);
574 sqlite3VdbeIntegerAffinity(pMem);
575 }
576 }
577 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
578 pMem->flags &= ~(MEM_Str|MEM_Blob);
579 return SQLITE_OK;
580 }
581
582 /*
583 ** Cast the datatype of the value in pMem according to the affinity
584 ** "aff". Casting is different from applying affinity in that a cast
585 ** is forced. In other words, the value is converted into the desired
586 ** affinity even if that results in loss of data. This routine is
587 ** used (for example) to implement the SQL "cast()" operator.
588 */
589 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
590 if( pMem->flags & MEM_Null ) return;
591 switch( aff ){
592 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
593 if( (pMem->flags & MEM_Blob)==0 ){
594 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
595 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
596 MemSetTypeFlag(pMem, MEM_Blob);
597 }else{
598 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
599 }
600 break;
601 }
602 case SQLITE_AFF_NUMERIC: {
603 sqlite3VdbeMemNumerify(pMem);
604 break;
605 }
606 case SQLITE_AFF_INTEGER: {
607 sqlite3VdbeMemIntegerify(pMem);
608 break;
609 }
610 case SQLITE_AFF_REAL: {
611 sqlite3VdbeMemRealify(pMem);
612 break;
613 }
614 default: {
615 assert( aff==SQLITE_AFF_TEXT );
616 assert( MEM_Str==(MEM_Blob>>3) );
617 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
618 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
619 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
620 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
621 break;
622 }
623 }
624 }
625
626 /*
627 ** Initialize bulk memory to be a consistent Mem object.
628 **
629 ** The minimum amount of initialization feasible is performed.
630 */
631 SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
632 assert( (flags & ~MEM_TypeMask)==0 );
633 pMem->flags = flags;
634 pMem->db = db;
635 pMem->szMalloc = 0;
636 }
637
638
639 /*
640 ** Delete any previous value and set the value stored in *pMem to NULL.
641 **
642 ** This routine calls the Mem.xDel destructor to dispose of values that
643 ** require the destructor. But it preserves the Mem.zMalloc memory allocation.
644 ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
645 ** routine to invoke the destructor and deallocates Mem.zMalloc.
646 **
647 ** Use this routine to reset the Mem prior to insert a new value.
648 **
649 ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
650 */
651 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
652 if( VdbeMemDynamic(pMem) ){
653 vdbeMemClearExternAndSetNull(pMem);
654 }else{
655 pMem->flags = MEM_Null;
656 }
657 }
658 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
659 sqlite3VdbeMemSetNull((Mem*)p);
660 }
661
662 /*
663 ** Delete any previous value and set the value to be a BLOB of length
664 ** n containing all zeros.
665 */
666 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
667 sqlite3VdbeMemRelease(pMem);
668 pMem->flags = MEM_Blob|MEM_Zero;
669 pMem->n = 0;
670 if( n<0 ) n = 0;
671 pMem->u.nZero = n;
672 pMem->enc = SQLITE_UTF8;
673 pMem->z = 0;
674 }
675
676 /*
677 ** The pMem is known to contain content that needs to be destroyed prior
678 ** to a value change. So invoke the destructor, then set the value to
679 ** a 64-bit integer.
680 */
681 static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
682 sqlite3VdbeMemSetNull(pMem);
683 pMem->u.i = val;
684 pMem->flags = MEM_Int;
685 }
686
687 /*
688 ** Delete any previous value and set the value stored in *pMem to val,
689 ** manifest type INTEGER.
690 */
691 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
692 if( VdbeMemDynamic(pMem) ){
693 vdbeReleaseAndSetInt64(pMem, val);
694 }else{
695 pMem->u.i = val;
696 pMem->flags = MEM_Int;
697 }
698 }
699
700 #ifndef SQLITE_OMIT_FLOATING_POINT
701 /*
702 ** Delete any previous value and set the value stored in *pMem to val,
703 ** manifest type REAL.
704 */
705 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
706 sqlite3VdbeMemSetNull(pMem);
707 if( !sqlite3IsNaN(val) ){
708 pMem->u.r = val;
709 pMem->flags = MEM_Real;
710 }
711 }
712 #endif
713
714 /*
715 ** Delete any previous value and set the value of pMem to be an
716 ** empty boolean index.
717 */
718 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
719 sqlite3 *db = pMem->db;
720 assert( db!=0 );
721 assert( (pMem->flags & MEM_RowSet)==0 );
722 sqlite3VdbeMemRelease(pMem);
723 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
724 if( db->mallocFailed ){
725 pMem->flags = MEM_Null;
726 pMem->szMalloc = 0;
727 }else{
728 assert( pMem->zMalloc );
729 pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
730 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
731 assert( pMem->u.pRowSet!=0 );
732 pMem->flags = MEM_RowSet;
733 }
734 }
735
736 /*
737 ** Return true if the Mem object contains a TEXT or BLOB that is
738 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
739 */
740 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
741 assert( p->db!=0 );
742 if( p->flags & (MEM_Str|MEM_Blob) ){
743 int n = p->n;
744 if( p->flags & MEM_Zero ){
745 n += p->u.nZero;
746 }
747 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
748 }
749 return 0;
750 }
751
752 #ifdef SQLITE_DEBUG
753 /*
754 ** This routine prepares a memory cell for modification by breaking
755 ** its link to a shallow copy and by marking any current shallow
756 ** copies of this cell as invalid.
757 **
758 ** This is used for testing and debugging only - to make sure shallow
759 ** copies are not misused.
760 */
761 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
762 int i;
763 Mem *pX;
764 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
765 if( pX->pScopyFrom==pMem ){
766 pX->flags |= MEM_Undefined;
767 pX->pScopyFrom = 0;
768 }
769 }
770 pMem->pScopyFrom = 0;
771 }
772 #endif /* SQLITE_DEBUG */
773
774
775 /*
776 ** Make an shallow copy of pFrom into pTo. Prior contents of
777 ** pTo are freed. The pFrom->z field is not duplicated. If
778 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
779 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
780 */
781 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
782 vdbeMemClearExternAndSetNull(pTo);
783 assert( !VdbeMemDynamic(pTo) );
784 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
785 }
786 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int sr cType){
787 assert( (pFrom->flags & MEM_RowSet)==0 );
788 assert( pTo->db==pFrom->db );
789 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
790 memcpy(pTo, pFrom, MEMCELLSIZE);
791 if( (pFrom->flags&MEM_Static)==0 ){
792 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
793 assert( srcType==MEM_Ephem || srcType==MEM_Static );
794 pTo->flags |= srcType;
795 }
796 }
797
798 /*
799 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
800 ** freed before the copy is made.
801 */
802 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
803 int rc = SQLITE_OK;
804
805 /* The pFrom==0 case in the following assert() is when an sqlite3_value
806 ** from sqlite3_value_dup() is used as the argument
807 ** to sqlite3_result_value(). */
808 assert( pTo->db==pFrom->db || pFrom->db==0 );
809 assert( (pFrom->flags & MEM_RowSet)==0 );
810 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
811 memcpy(pTo, pFrom, MEMCELLSIZE);
812 pTo->flags &= ~MEM_Dyn;
813 if( pTo->flags&(MEM_Str|MEM_Blob) ){
814 if( 0==(pFrom->flags&MEM_Static) ){
815 pTo->flags |= MEM_Ephem;
816 rc = sqlite3VdbeMemMakeWriteable(pTo);
817 }
818 }
819
820 return rc;
821 }
822
823 /*
824 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
825 ** freed. If pFrom contains ephemeral data, a copy is made.
826 **
827 ** pFrom contains an SQL NULL when this routine returns.
828 */
829 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
830 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
831 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
832 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
833
834 sqlite3VdbeMemRelease(pTo);
835 memcpy(pTo, pFrom, sizeof(Mem));
836 pFrom->flags = MEM_Null;
837 pFrom->szMalloc = 0;
838 }
839
840 /*
841 ** Change the value of a Mem to be a string or a BLOB.
842 **
843 ** The memory management strategy depends on the value of the xDel
844 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
845 ** string is copied into a (possibly existing) buffer managed by the
846 ** Mem structure. Otherwise, any existing buffer is freed and the
847 ** pointer copied.
848 **
849 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
850 ** size limit) then no memory allocation occurs. If the string can be
851 ** stored without allocating memory, then it is. If a memory allocation
852 ** is required to store the string, then value of pMem is unchanged. In
853 ** either case, SQLITE_TOOBIG is returned.
854 */
855 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
856 Mem *pMem, /* Memory cell to set to string value */
857 const char *z, /* String pointer */
858 int n, /* Bytes in string, or negative */
859 u8 enc, /* Encoding of z. 0 for BLOBs */
860 void (*xDel)(void*) /* Destructor function */
861 ){
862 int nByte = n; /* New value for pMem->n */
863 int iLimit; /* Maximum allowed string or blob size */
864 u16 flags = 0; /* New value for pMem->flags */
865
866 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
867 assert( (pMem->flags & MEM_RowSet)==0 );
868
869 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
870 if( !z ){
871 sqlite3VdbeMemSetNull(pMem);
872 return SQLITE_OK;
873 }
874
875 if( pMem->db ){
876 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
877 }else{
878 iLimit = SQLITE_MAX_LENGTH;
879 }
880 flags = (enc==0?MEM_Blob:MEM_Str);
881 if( nByte<0 ){
882 assert( enc!=0 );
883 if( enc==SQLITE_UTF8 ){
884 nByte = sqlite3Strlen30(z);
885 if( nByte>iLimit ) nByte = iLimit+1;
886 }else{
887 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
888 }
889 flags |= MEM_Term;
890 }
891
892 /* The following block sets the new values of Mem.z and Mem.xDel. It
893 ** also sets a flag in local variable "flags" to indicate the memory
894 ** management (one of MEM_Dyn or MEM_Static).
895 */
896 if( xDel==SQLITE_TRANSIENT ){
897 int nAlloc = nByte;
898 if( flags&MEM_Term ){
899 nAlloc += (enc==SQLITE_UTF8?1:2);
900 }
901 if( nByte>iLimit ){
902 return SQLITE_TOOBIG;
903 }
904 testcase( nAlloc==0 );
905 testcase( nAlloc==31 );
906 testcase( nAlloc==32 );
907 if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
908 return SQLITE_NOMEM;
909 }
910 memcpy(pMem->z, z, nAlloc);
911 }else if( xDel==SQLITE_DYNAMIC ){
912 sqlite3VdbeMemRelease(pMem);
913 pMem->zMalloc = pMem->z = (char *)z;
914 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
915 }else{
916 sqlite3VdbeMemRelease(pMem);
917 pMem->z = (char *)z;
918 pMem->xDel = xDel;
919 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
920 }
921
922 pMem->n = nByte;
923 pMem->flags = flags;
924 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
925
926 #ifndef SQLITE_OMIT_UTF16
927 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
928 return SQLITE_NOMEM;
929 }
930 #endif
931
932 if( nByte>iLimit ){
933 return SQLITE_TOOBIG;
934 }
935
936 return SQLITE_OK;
937 }
938
939 /*
940 ** Move data out of a btree key or data field and into a Mem structure.
941 ** The data or key is taken from the entry that pCur is currently pointing
942 ** to. offset and amt determine what portion of the data or key to retrieve.
943 ** key is true to get the key or false to get data. The result is written
944 ** into the pMem element.
945 **
946 ** The pMem object must have been initialized. This routine will use
947 ** pMem->zMalloc to hold the content from the btree, if possible. New
948 ** pMem->zMalloc space will be allocated if necessary. The calling routine
949 ** is responsible for making sure that the pMem object is eventually
950 ** destroyed.
951 **
952 ** If this routine fails for any reason (malloc returns NULL or unable
953 ** to read from the disk) then the pMem is left in an inconsistent state.
954 */
955 static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
956 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
957 u32 offset, /* Offset from the start of data to return bytes from. */
958 u32 amt, /* Number of bytes to return. */
959 int key, /* If true, retrieve from the btree key, not data. */
960 Mem *pMem /* OUT: Return data in this Mem structure. */
961 ){
962 int rc;
963 pMem->flags = MEM_Null;
964 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
965 if( key ){
966 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
967 }else{
968 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
969 }
970 if( rc==SQLITE_OK ){
971 pMem->z[amt] = 0;
972 pMem->z[amt+1] = 0;
973 pMem->flags = MEM_Blob|MEM_Term;
974 pMem->n = (int)amt;
975 }else{
976 sqlite3VdbeMemRelease(pMem);
977 }
978 }
979 return rc;
980 }
981 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
982 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
983 u32 offset, /* Offset from the start of data to return bytes from. */
984 u32 amt, /* Number of bytes to return. */
985 int key, /* If true, retrieve from the btree key, not data. */
986 Mem *pMem /* OUT: Return data in this Mem structure. */
987 ){
988 char *zData; /* Data from the btree layer */
989 u32 available = 0; /* Number of bytes available on the local btree page */
990 int rc = SQLITE_OK; /* Return code */
991
992 assert( sqlite3BtreeCursorIsValid(pCur) );
993 assert( !VdbeMemDynamic(pMem) );
994
995 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
996 ** that both the BtShared and database handle mutexes are held. */
997 assert( (pMem->flags & MEM_RowSet)==0 );
998 if( key ){
999 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
1000 }else{
1001 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
1002 }
1003 assert( zData!=0 );
1004
1005 if( offset+amt<=available ){
1006 pMem->z = &zData[offset];
1007 pMem->flags = MEM_Blob|MEM_Ephem;
1008 pMem->n = (int)amt;
1009 }else{
1010 rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
1011 }
1012
1013 return rc;
1014 }
1015
1016 /*
1017 ** The pVal argument is known to be a value other than NULL.
1018 ** Convert it into a string with encoding enc and return a pointer
1019 ** to a zero-terminated version of that string.
1020 */
1021 static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
1022 assert( pVal!=0 );
1023 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
1024 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
1025 assert( (pVal->flags & MEM_RowSet)==0 );
1026 assert( (pVal->flags & (MEM_Null))==0 );
1027 if( pVal->flags & (MEM_Blob|MEM_Str) ){
1028 pVal->flags |= MEM_Str;
1029 if( pVal->flags & MEM_Zero ){
1030 sqlite3VdbeMemExpandBlob(pVal);
1031 }
1032 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
1033 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
1034 }
1035 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
1036 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
1037 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
1038 return 0;
1039 }
1040 }
1041 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
1042 }else{
1043 sqlite3VdbeMemStringify(pVal, enc, 0);
1044 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
1045 }
1046 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
1047 || pVal->db->mallocFailed );
1048 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
1049 return pVal->z;
1050 }else{
1051 return 0;
1052 }
1053 }
1054
1055 /* This function is only available internally, it is not part of the
1056 ** external API. It works in a similar way to sqlite3_value_text(),
1057 ** except the data returned is in the encoding specified by the second
1058 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
1059 ** SQLITE_UTF8.
1060 **
1061 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
1062 ** If that is the case, then the result must be aligned on an even byte
1063 ** boundary.
1064 */
1065 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
1066 if( !pVal ) return 0;
1067 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
1068 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
1069 assert( (pVal->flags & MEM_RowSet)==0 );
1070 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
1071 return pVal->z;
1072 }
1073 if( pVal->flags&MEM_Null ){
1074 return 0;
1075 }
1076 return valueToText(pVal, enc);
1077 }
1078
1079 /*
1080 ** Create a new sqlite3_value object.
1081 */
1082 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
1083 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
1084 if( p ){
1085 p->flags = MEM_Null;
1086 p->db = db;
1087 }
1088 return p;
1089 }
1090
1091 /*
1092 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
1093 ** valueNew(). See comments above valueNew() for details.
1094 */
1095 struct ValueNewStat4Ctx {
1096 Parse *pParse;
1097 Index *pIdx;
1098 UnpackedRecord **ppRec;
1099 int iVal;
1100 };
1101
1102 /*
1103 ** Allocate and return a pointer to a new sqlite3_value object. If
1104 ** the second argument to this function is NULL, the object is allocated
1105 ** by calling sqlite3ValueNew().
1106 **
1107 ** Otherwise, if the second argument is non-zero, then this function is
1108 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
1109 ** already been allocated, allocate the UnpackedRecord structure that
1110 ** that function will return to its caller here. Then return a pointer to
1111 ** an sqlite3_value within the UnpackedRecord.a[] array.
1112 */
1113 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
1114 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1115 if( p ){
1116 UnpackedRecord *pRec = p->ppRec[0];
1117
1118 if( pRec==0 ){
1119 Index *pIdx = p->pIdx; /* Index being probed */
1120 int nByte; /* Bytes of space to allocate */
1121 int i; /* Counter variable */
1122 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
1123
1124 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
1125 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
1126 if( pRec ){
1127 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
1128 if( pRec->pKeyInfo ){
1129 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
1130 assert( pRec->pKeyInfo->enc==ENC(db) );
1131 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
1132 for(i=0; i<nCol; i++){
1133 pRec->aMem[i].flags = MEM_Null;
1134 pRec->aMem[i].db = db;
1135 }
1136 }else{
1137 sqlite3DbFree(db, pRec);
1138 pRec = 0;
1139 }
1140 }
1141 if( pRec==0 ) return 0;
1142 p->ppRec[0] = pRec;
1143 }
1144
1145 pRec->nField = p->iVal+1;
1146 return &pRec->aMem[p->iVal];
1147 }
1148 #else
1149 UNUSED_PARAMETER(p);
1150 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
1151 return sqlite3ValueNew(db);
1152 }
1153
1154 /*
1155 ** The expression object indicated by the second argument is guaranteed
1156 ** to be a scalar SQL function. If
1157 **
1158 ** * all function arguments are SQL literals,
1159 ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
1160 ** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
1161 **
1162 ** then this routine attempts to invoke the SQL function. Assuming no
1163 ** error occurs, output parameter (*ppVal) is set to point to a value
1164 ** object containing the result before returning SQLITE_OK.
1165 **
1166 ** Affinity aff is applied to the result of the function before returning.
1167 ** If the result is a text value, the sqlite3_value object uses encoding
1168 ** enc.
1169 **
1170 ** If the conditions above are not met, this function returns SQLITE_OK
1171 ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
1172 ** NULL and an SQLite error code returned.
1173 */
1174 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1175 static int valueFromFunction(
1176 sqlite3 *db, /* The database connection */
1177 Expr *p, /* The expression to evaluate */
1178 u8 enc, /* Encoding to use */
1179 u8 aff, /* Affinity to use */
1180 sqlite3_value **ppVal, /* Write the new value here */
1181 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
1182 ){
1183 sqlite3_context ctx; /* Context object for function invocation */
1184 sqlite3_value **apVal = 0; /* Function arguments */
1185 int nVal = 0; /* Size of apVal[] array */
1186 FuncDef *pFunc = 0; /* Function definition */
1187 sqlite3_value *pVal = 0; /* New value */
1188 int rc = SQLITE_OK; /* Return code */
1189 int nName; /* Size of function name in bytes */
1190 ExprList *pList = 0; /* Function arguments */
1191 int i; /* Iterator variable */
1192
1193 assert( pCtx!=0 );
1194 assert( (p->flags & EP_TokenOnly)==0 );
1195 pList = p->x.pList;
1196 if( pList ) nVal = pList->nExpr;
1197 nName = sqlite3Strlen30(p->u.zToken);
1198 pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
1199 assert( pFunc );
1200 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
1201 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
1202 ){
1203 return SQLITE_OK;
1204 }
1205
1206 if( pList ){
1207 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
1208 if( apVal==0 ){
1209 rc = SQLITE_NOMEM;
1210 goto value_from_function_out;
1211 }
1212 for(i=0; i<nVal; i++){
1213 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
1214 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
1215 }
1216 }
1217
1218 pVal = valueNew(db, pCtx);
1219 if( pVal==0 ){
1220 rc = SQLITE_NOMEM;
1221 goto value_from_function_out;
1222 }
1223
1224 assert( pCtx->pParse->rc==SQLITE_OK );
1225 memset(&ctx, 0, sizeof(ctx));
1226 ctx.pOut = pVal;
1227 ctx.pFunc = pFunc;
1228 pFunc->xFunc(&ctx, nVal, apVal);
1229 if( ctx.isError ){
1230 rc = ctx.isError;
1231 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
1232 }else{
1233 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
1234 assert( rc==SQLITE_OK );
1235 rc = sqlite3VdbeChangeEncoding(pVal, enc);
1236 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
1237 rc = SQLITE_TOOBIG;
1238 pCtx->pParse->nErr++;
1239 }
1240 }
1241 pCtx->pParse->rc = rc;
1242
1243 value_from_function_out:
1244 if( rc!=SQLITE_OK ){
1245 pVal = 0;
1246 }
1247 if( apVal ){
1248 for(i=0; i<nVal; i++){
1249 sqlite3ValueFree(apVal[i]);
1250 }
1251 sqlite3DbFree(db, apVal);
1252 }
1253
1254 *ppVal = pVal;
1255 return rc;
1256 }
1257 #else
1258 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
1259 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
1260
1261 /*
1262 ** Extract a value from the supplied expression in the manner described
1263 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
1264 ** using valueNew().
1265 **
1266 ** If pCtx is NULL and an error occurs after the sqlite3_value object
1267 ** has been allocated, it is freed before returning. Or, if pCtx is not
1268 ** NULL, it is assumed that the caller will free any allocated object
1269 ** in all cases.
1270 */
1271 static int valueFromExpr(
1272 sqlite3 *db, /* The database connection */
1273 Expr *pExpr, /* The expression to evaluate */
1274 u8 enc, /* Encoding to use */
1275 u8 affinity, /* Affinity to use */
1276 sqlite3_value **ppVal, /* Write the new value here */
1277 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
1278 ){
1279 int op;
1280 char *zVal = 0;
1281 sqlite3_value *pVal = 0;
1282 int negInt = 1;
1283 const char *zNeg = "";
1284 int rc = SQLITE_OK;
1285
1286 if( !pExpr ){
1287 *ppVal = 0;
1288 return SQLITE_OK;
1289 }
1290 while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
1291 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
1292
1293 /* Compressed expressions only appear when parsing the DEFAULT clause
1294 ** on a table column definition, and hence only when pCtx==0. This
1295 ** check ensures that an EP_TokenOnly expression is never passed down
1296 ** into valueFromFunction(). */
1297 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
1298
1299 if( op==TK_CAST ){
1300 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
1301 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
1302 testcase( rc!=SQLITE_OK );
1303 if( *ppVal ){
1304 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
1305 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
1306 }
1307 return rc;
1308 }
1309
1310 /* Handle negative integers in a single step. This is needed in the
1311 ** case when the value is -9223372036854775808.
1312 */
1313 if( op==TK_UMINUS
1314 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
1315 pExpr = pExpr->pLeft;
1316 op = pExpr->op;
1317 negInt = -1;
1318 zNeg = "-";
1319 }
1320
1321 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
1322 pVal = valueNew(db, pCtx);
1323 if( pVal==0 ) goto no_mem;
1324 if( ExprHasProperty(pExpr, EP_IntValue) ){
1325 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
1326 }else{
1327 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
1328 if( zVal==0 ) goto no_mem;
1329 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
1330 }
1331 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
1332 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
1333 }else{
1334 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
1335 }
1336 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
1337 if( enc!=SQLITE_UTF8 ){
1338 rc = sqlite3VdbeChangeEncoding(pVal, enc);
1339 }
1340 }else if( op==TK_UMINUS ) {
1341 /* This branch happens for multiple negative signs. Ex: -(-5) */
1342 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
1343 && pVal!=0
1344 ){
1345 sqlite3VdbeMemNumerify(pVal);
1346 if( pVal->flags & MEM_Real ){
1347 pVal->u.r = -pVal->u.r;
1348 }else if( pVal->u.i==SMALLEST_INT64 ){
1349 pVal->u.r = -(double)SMALLEST_INT64;
1350 MemSetTypeFlag(pVal, MEM_Real);
1351 }else{
1352 pVal->u.i = -pVal->u.i;
1353 }
1354 sqlite3ValueApplyAffinity(pVal, affinity, enc);
1355 }
1356 }else if( op==TK_NULL ){
1357 pVal = valueNew(db, pCtx);
1358 if( pVal==0 ) goto no_mem;
1359 }
1360 #ifndef SQLITE_OMIT_BLOB_LITERAL
1361 else if( op==TK_BLOB ){
1362 int nVal;
1363 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
1364 assert( pExpr->u.zToken[1]=='\'' );
1365 pVal = valueNew(db, pCtx);
1366 if( !pVal ) goto no_mem;
1367 zVal = &pExpr->u.zToken[2];
1368 nVal = sqlite3Strlen30(zVal)-1;
1369 assert( zVal[nVal]=='\'' );
1370 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
1371 0, SQLITE_DYNAMIC);
1372 }
1373 #endif
1374
1375 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1376 else if( op==TK_FUNCTION && pCtx!=0 ){
1377 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
1378 }
1379 #endif
1380
1381 *ppVal = pVal;
1382 return rc;
1383
1384 no_mem:
1385 db->mallocFailed = 1;
1386 sqlite3DbFree(db, zVal);
1387 assert( *ppVal==0 );
1388 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1389 if( pCtx==0 ) sqlite3ValueFree(pVal);
1390 #else
1391 assert( pCtx==0 ); sqlite3ValueFree(pVal);
1392 #endif
1393 return SQLITE_NOMEM;
1394 }
1395
1396 /*
1397 ** Create a new sqlite3_value object, containing the value of pExpr.
1398 **
1399 ** This only works for very simple expressions that consist of one constant
1400 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
1401 ** be converted directly into a value, then the value is allocated and
1402 ** a pointer written to *ppVal. The caller is responsible for deallocating
1403 ** the value by passing it to sqlite3ValueFree() later on. If the expression
1404 ** cannot be converted to a value, then *ppVal is set to NULL.
1405 */
1406 SQLITE_PRIVATE int sqlite3ValueFromExpr(
1407 sqlite3 *db, /* The database connection */
1408 Expr *pExpr, /* The expression to evaluate */
1409 u8 enc, /* Encoding to use */
1410 u8 affinity, /* Affinity to use */
1411 sqlite3_value **ppVal /* Write the new value here */
1412 ){
1413 return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
1414 }
1415
1416 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1417 /*
1418 ** The implementation of the sqlite_record() function. This function accepts
1419 ** a single argument of any type. The return value is a formatted database
1420 ** record (a blob) containing the argument value.
1421 **
1422 ** This is used to convert the value stored in the 'sample' column of the
1423 ** sqlite_stat3 table to the record format SQLite uses internally.
1424 */
1425 static void recordFunc(
1426 sqlite3_context *context,
1427 int argc,
1428 sqlite3_value **argv
1429 ){
1430 const int file_format = 1;
1431 u32 iSerial; /* Serial type */
1432 int nSerial; /* Bytes of space for iSerial as varint */
1433 u32 nVal; /* Bytes of space required for argv[0] */
1434 int nRet;
1435 sqlite3 *db;
1436 u8 *aRet;
1437
1438 UNUSED_PARAMETER( argc );
1439 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
1440 nSerial = sqlite3VarintLen(iSerial);
1441 db = sqlite3_context_db_handle(context);
1442
1443 nRet = 1 + nSerial + nVal;
1444 aRet = sqlite3DbMallocRaw(db, nRet);
1445 if( aRet==0 ){
1446 sqlite3_result_error_nomem(context);
1447 }else{
1448 aRet[0] = nSerial+1;
1449 putVarint32(&aRet[1], iSerial);
1450 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
1451 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
1452 sqlite3DbFree(db, aRet);
1453 }
1454 }
1455
1456 /*
1457 ** Register built-in functions used to help read ANALYZE data.
1458 */
1459 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
1460 static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
1461 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
1462 };
1463 int i;
1464 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
1465 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
1466 for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
1467 sqlite3FuncDefInsert(pHash, &aFunc[i]);
1468 }
1469 }
1470
1471 /*
1472 ** Attempt to extract a value from pExpr and use it to construct *ppVal.
1473 **
1474 ** If pAlloc is not NULL, then an UnpackedRecord object is created for
1475 ** pAlloc if one does not exist and the new value is added to the
1476 ** UnpackedRecord object.
1477 **
1478 ** A value is extracted in the following cases:
1479 **
1480 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
1481 **
1482 ** * The expression is a bound variable, and this is a reprepare, or
1483 **
1484 ** * The expression is a literal value.
1485 **
1486 ** On success, *ppVal is made to point to the extracted value. The caller
1487 ** is responsible for ensuring that the value is eventually freed.
1488 */
1489 static int stat4ValueFromExpr(
1490 Parse *pParse, /* Parse context */
1491 Expr *pExpr, /* The expression to extract a value from */
1492 u8 affinity, /* Affinity to use */
1493 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
1494 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
1495 ){
1496 int rc = SQLITE_OK;
1497 sqlite3_value *pVal = 0;
1498 sqlite3 *db = pParse->db;
1499
1500 /* Skip over any TK_COLLATE nodes */
1501 pExpr = sqlite3ExprSkipCollate(pExpr);
1502
1503 if( !pExpr ){
1504 pVal = valueNew(db, pAlloc);
1505 if( pVal ){
1506 sqlite3VdbeMemSetNull((Mem*)pVal);
1507 }
1508 }else if( pExpr->op==TK_VARIABLE
1509 || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
1510 ){
1511 Vdbe *v;
1512 int iBindVar = pExpr->iColumn;
1513 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
1514 if( (v = pParse->pReprepare)!=0 ){
1515 pVal = valueNew(db, pAlloc);
1516 if( pVal ){
1517 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
1518 if( rc==SQLITE_OK ){
1519 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
1520 }
1521 pVal->db = pParse->db;
1522 }
1523 }
1524 }else{
1525 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
1526 }
1527
1528 assert( pVal==0 || pVal->db==db );
1529 *ppVal = pVal;
1530 return rc;
1531 }
1532
1533 /*
1534 ** This function is used to allocate and populate UnpackedRecord
1535 ** structures intended to be compared against sample index keys stored
1536 ** in the sqlite_stat4 table.
1537 **
1538 ** A single call to this function attempts to populates field iVal (leftmost
1539 ** is 0 etc.) of the unpacked record with a value extracted from expression
1540 ** pExpr. Extraction of values is possible if:
1541 **
1542 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
1543 **
1544 ** * The expression is a bound variable, and this is a reprepare, or
1545 **
1546 ** * The sqlite3ValueFromExpr() function is able to extract a value
1547 ** from the expression (i.e. the expression is a literal value).
1548 **
1549 ** If a value can be extracted, the affinity passed as the 5th argument
1550 ** is applied to it before it is copied into the UnpackedRecord. Output
1551 ** parameter *pbOk is set to true if a value is extracted, or false
1552 ** otherwise.
1553 **
1554 ** When this function is called, *ppRec must either point to an object
1555 ** allocated by an earlier call to this function, or must be NULL. If it
1556 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
1557 ** is allocated (and *ppRec set to point to it) before returning.
1558 **
1559 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
1560 ** error if a value cannot be extracted from pExpr. If an error does
1561 ** occur, an SQLite error code is returned.
1562 */
1563 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
1564 Parse *pParse, /* Parse context */
1565 Index *pIdx, /* Index being probed */
1566 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
1567 Expr *pExpr, /* The expression to extract a value from */
1568 u8 affinity, /* Affinity to use */
1569 int iVal, /* Array element to populate */
1570 int *pbOk /* OUT: True if value was extracted */
1571 ){
1572 int rc;
1573 sqlite3_value *pVal = 0;
1574 struct ValueNewStat4Ctx alloc;
1575
1576 alloc.pParse = pParse;
1577 alloc.pIdx = pIdx;
1578 alloc.ppRec = ppRec;
1579 alloc.iVal = iVal;
1580
1581 rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
1582 assert( pVal==0 || pVal->db==pParse->db );
1583 *pbOk = (pVal!=0);
1584 return rc;
1585 }
1586
1587 /*
1588 ** Attempt to extract a value from expression pExpr using the methods
1589 ** as described for sqlite3Stat4ProbeSetValue() above.
1590 **
1591 ** If successful, set *ppVal to point to a new value object and return
1592 ** SQLITE_OK. If no value can be extracted, but no other error occurs
1593 ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
1594 ** does occur, return an SQLite error code. The final value of *ppVal
1595 ** is undefined in this case.
1596 */
1597 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
1598 Parse *pParse, /* Parse context */
1599 Expr *pExpr, /* The expression to extract a value from */
1600 u8 affinity, /* Affinity to use */
1601 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
1602 ){
1603 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
1604 }
1605
1606 /*
1607 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
1608 ** the column value into *ppVal. If *ppVal is initially NULL then a new
1609 ** sqlite3_value object is allocated.
1610 **
1611 ** If *ppVal is initially NULL then the caller is responsible for
1612 ** ensuring that the value written into *ppVal is eventually freed.
1613 */
1614 SQLITE_PRIVATE int sqlite3Stat4Column(
1615 sqlite3 *db, /* Database handle */
1616 const void *pRec, /* Pointer to buffer containing record */
1617 int nRec, /* Size of buffer pRec in bytes */
1618 int iCol, /* Column to extract */
1619 sqlite3_value **ppVal /* OUT: Extracted value */
1620 ){
1621 u32 t; /* a column type code */
1622 int nHdr; /* Size of the header in the record */
1623 int iHdr; /* Next unread header byte */
1624 int iField; /* Next unread data byte */
1625 int szField; /* Size of the current data field */
1626 int i; /* Column index */
1627 u8 *a = (u8*)pRec; /* Typecast byte array */
1628 Mem *pMem = *ppVal; /* Write result into this Mem object */
1629
1630 assert( iCol>0 );
1631 iHdr = getVarint32(a, nHdr);
1632 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
1633 iField = nHdr;
1634 for(i=0; i<=iCol; i++){
1635 iHdr += getVarint32(&a[iHdr], t);
1636 testcase( iHdr==nHdr );
1637 testcase( iHdr==nHdr+1 );
1638 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
1639 szField = sqlite3VdbeSerialTypeLen(t);
1640 iField += szField;
1641 }
1642 testcase( iField==nRec );
1643 testcase( iField==nRec+1 );
1644 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
1645 if( pMem==0 ){
1646 pMem = *ppVal = sqlite3ValueNew(db);
1647 if( pMem==0 ) return SQLITE_NOMEM;
1648 }
1649 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
1650 pMem->enc = ENC(db);
1651 return SQLITE_OK;
1652 }
1653
1654 /*
1655 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
1656 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
1657 ** the object.
1658 */
1659 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
1660 if( pRec ){
1661 int i;
1662 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
1663 Mem *aMem = pRec->aMem;
1664 sqlite3 *db = aMem[0].db;
1665 for(i=0; i<nCol; i++){
1666 sqlite3VdbeMemRelease(&aMem[i]);
1667 }
1668 sqlite3KeyInfoUnref(pRec->pKeyInfo);
1669 sqlite3DbFree(db, pRec);
1670 }
1671 }
1672 #endif /* ifdef SQLITE_ENABLE_STAT4 */
1673
1674 /*
1675 ** Change the string value of an sqlite3_value object
1676 */
1677 SQLITE_PRIVATE void sqlite3ValueSetStr(
1678 sqlite3_value *v, /* Value to be set */
1679 int n, /* Length of string z */
1680 const void *z, /* Text of the new string */
1681 u8 enc, /* Encoding to use */
1682 void (*xDel)(void*) /* Destructor for the string */
1683 ){
1684 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
1685 }
1686
1687 /*
1688 ** Free an sqlite3_value object
1689 */
1690 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
1691 if( !v ) return;
1692 sqlite3VdbeMemRelease((Mem *)v);
1693 sqlite3DbFree(((Mem*)v)->db, v);
1694 }
1695
1696 /*
1697 ** The sqlite3ValueBytes() routine returns the number of bytes in the
1698 ** sqlite3_value object assuming that it uses the encoding "enc".
1699 ** The valueBytes() routine is a helper function.
1700 */
1701 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
1702 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
1703 }
1704 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1705 Mem *p = (Mem*)pVal;
1706 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
1707 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
1708 return p->n;
1709 }
1710 if( (p->flags & MEM_Blob)!=0 ){
1711 if( p->flags & MEM_Zero ){
1712 return p->n + p->u.nZero;
1713 }else{
1714 return p->n;
1715 }
1716 }
1717 if( p->flags & MEM_Null ) return 0;
1718 return valueBytes(pVal, enc);
1719 }
1720
1721 /************** End of vdbemem.c *********************************************/
1722 /************** Begin file vdbeaux.c *****************************************/
1723 /*
1724 ** 2003 September 6
1725 **
1726 ** The author disclaims copyright to this source code. In place of
1727 ** a legal notice, here is a blessing:
1728 **
1729 ** May you do good and not evil.
1730 ** May you find forgiveness for yourself and forgive others.
1731 ** May you share freely, never taking more than you give.
1732 **
1733 *************************************************************************
1734 ** This file contains code used for creating, destroying, and populating
1735 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
1736 */
1737 /* #include "sqliteInt.h" */
1738 /* #include "vdbeInt.h" */
1739
1740 /*
1741 ** Create a new virtual database engine.
1742 */
1743 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
1744 sqlite3 *db = pParse->db;
1745 Vdbe *p;
1746 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
1747 if( p==0 ) return 0;
1748 p->db = db;
1749 if( db->pVdbe ){
1750 db->pVdbe->pPrev = p;
1751 }
1752 p->pNext = db->pVdbe;
1753 p->pPrev = 0;
1754 db->pVdbe = p;
1755 p->magic = VDBE_MAGIC_INIT;
1756 p->pParse = pParse;
1757 assert( pParse->aLabel==0 );
1758 assert( pParse->nLabel==0 );
1759 assert( pParse->nOpAlloc==0 );
1760 assert( pParse->szOpAlloc==0 );
1761 return p;
1762 }
1763
1764 /*
1765 ** Change the error string stored in Vdbe.zErrMsg
1766 */
1767 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
1768 va_list ap;
1769 sqlite3DbFree(p->db, p->zErrMsg);
1770 va_start(ap, zFormat);
1771 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
1772 va_end(ap);
1773 }
1774
1775 /*
1776 ** Remember the SQL string for a prepared statement.
1777 */
1778 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepa reV2){
1779 assert( isPrepareV2==1 || isPrepareV2==0 );
1780 if( p==0 ) return;
1781 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
1782 if( !isPrepareV2 ) return;
1783 #endif
1784 assert( p->zSql==0 );
1785 p->zSql = sqlite3DbStrNDup(p->db, z, n);
1786 p->isPrepareV2 = (u8)isPrepareV2;
1787 }
1788
1789 /*
1790 ** Return the SQL associated with a prepared statement
1791 */
1792 SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
1793 Vdbe *p = (Vdbe *)pStmt;
1794 return p ? p->zSql : 0;
1795 }
1796
1797 /*
1798 ** Swap all content between two VDBE structures.
1799 */
1800 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
1801 Vdbe tmp, *pTmp;
1802 char *zTmp;
1803 tmp = *pA;
1804 *pA = *pB;
1805 *pB = tmp;
1806 pTmp = pA->pNext;
1807 pA->pNext = pB->pNext;
1808 pB->pNext = pTmp;
1809 pTmp = pA->pPrev;
1810 pA->pPrev = pB->pPrev;
1811 pB->pPrev = pTmp;
1812 zTmp = pA->zSql;
1813 pA->zSql = pB->zSql;
1814 pB->zSql = zTmp;
1815 pB->isPrepareV2 = pA->isPrepareV2;
1816 }
1817
1818 /*
1819 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
1820 ** than its current size. nOp is guaranteed to be less than or equal
1821 ** to 1024/sizeof(Op).
1822 **
1823 ** If an out-of-memory error occurs while resizing the array, return
1824 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
1825 ** unchanged (this is so that any opcodes already allocated can be
1826 ** correctly deallocated along with the rest of the Vdbe).
1827 */
1828 static int growOpArray(Vdbe *v, int nOp){
1829 VdbeOp *pNew;
1830 Parse *p = v->pParse;
1831
1832 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
1833 ** more frequent reallocs and hence provide more opportunities for
1834 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
1835 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
1836 ** by the minimum* amount required until the size reaches 512. Normal
1837 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
1838 ** size of the op array or add 1KB of space, whichever is smaller. */
1839 #ifdef SQLITE_TEST_REALLOC_STRESS
1840 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
1841 #else
1842 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
1843 UNUSED_PARAMETER(nOp);
1844 #endif
1845
1846 assert( nOp<=(1024/sizeof(Op)) );
1847 assert( nNew>=(p->nOpAlloc+nOp) );
1848 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
1849 if( pNew ){
1850 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
1851 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
1852 v->aOp = pNew;
1853 }
1854 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
1855 }
1856
1857 #ifdef SQLITE_DEBUG
1858 /* This routine is just a convenient place to set a breakpoint that will
1859 ** fire after each opcode is inserted and displayed using
1860 ** "PRAGMA vdbe_addoptrace=on".
1861 */
1862 static void test_addop_breakpoint(void){
1863 static int n = 0;
1864 n++;
1865 }
1866 #endif
1867
1868 /*
1869 ** Add a new instruction to the list of instructions current in the
1870 ** VDBE. Return the address of the new instruction.
1871 **
1872 ** Parameters:
1873 **
1874 ** p Pointer to the VDBE
1875 **
1876 ** op The opcode for this instruction
1877 **
1878 ** p1, p2, p3 Operands
1879 **
1880 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
1881 ** the sqlite3VdbeChangeP4() function to change the value of the P4
1882 ** operand.
1883 */
1884 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
1885 assert( p->pParse->nOpAlloc<=p->nOp );
1886 if( growOpArray(p, 1) ) return 1;
1887 assert( p->pParse->nOpAlloc>p->nOp );
1888 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
1889 }
1890 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
1891 int i;
1892 VdbeOp *pOp;
1893
1894 i = p->nOp;
1895 assert( p->magic==VDBE_MAGIC_INIT );
1896 assert( op>0 && op<0xff );
1897 if( p->pParse->nOpAlloc<=i ){
1898 return growOp3(p, op, p1, p2, p3);
1899 }
1900 p->nOp++;
1901 pOp = &p->aOp[i];
1902 pOp->opcode = (u8)op;
1903 pOp->p5 = 0;
1904 pOp->p1 = p1;
1905 pOp->p2 = p2;
1906 pOp->p3 = p3;
1907 pOp->p4.p = 0;
1908 pOp->p4type = P4_NOTUSED;
1909 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1910 pOp->zComment = 0;
1911 #endif
1912 #ifdef SQLITE_DEBUG
1913 if( p->db->flags & SQLITE_VdbeAddopTrace ){
1914 int jj, kk;
1915 Parse *pParse = p->pParse;
1916 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
1917 struct yColCache *x = pParse->aColCache + jj;
1918 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
1919 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
1920 kk++;
1921 }
1922 if( kk ) printf("\n");
1923 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
1924 test_addop_breakpoint();
1925 }
1926 #endif
1927 #ifdef VDBE_PROFILE
1928 pOp->cycles = 0;
1929 pOp->cnt = 0;
1930 #endif
1931 #ifdef SQLITE_VDBE_COVERAGE
1932 pOp->iSrcLine = 0;
1933 #endif
1934 return i;
1935 }
1936 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
1937 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
1938 }
1939 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
1940 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
1941 }
1942 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
1943 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
1944 }
1945
1946 /* Generate code for an unconditional jump to instruction iDest
1947 */
1948 SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
1949 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
1950 }
1951
1952 /* Generate code to cause the string zStr to be loaded into
1953 ** register iDest
1954 */
1955 SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
1956 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
1957 }
1958
1959 /*
1960 ** Generate code that initializes multiple registers to string or integer
1961 ** constants. The registers begin with iDest and increase consecutively.
1962 ** One register is initialized for each characgter in zTypes[]. For each
1963 ** "s" character in zTypes[], the register is a string if the argument is
1964 ** not NULL, or OP_Null if the value is a null pointer. For each "i" character
1965 ** in zTypes[], the register is initialized to an integer.
1966 */
1967 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
1968 va_list ap;
1969 int i;
1970 char c;
1971 va_start(ap, zTypes);
1972 for(i=0; (c = zTypes[i])!=0; i++){
1973 if( c=='s' ){
1974 const char *z = va_arg(ap, const char*);
1975 int addr = sqlite3VdbeAddOp2(p, z==0 ? OP_Null : OP_String8, 0, iDest++);
1976 if( z ) sqlite3VdbeChangeP4(p, addr, z, 0);
1977 }else{
1978 assert( c=='i' );
1979 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++);
1980 }
1981 }
1982 va_end(ap);
1983 }
1984
1985 /*
1986 ** Add an opcode that includes the p4 value as a pointer.
1987 */
1988 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
1989 Vdbe *p, /* Add the opcode to this VM */
1990 int op, /* The new opcode */
1991 int p1, /* The P1 operand */
1992 int p2, /* The P2 operand */
1993 int p3, /* The P3 operand */
1994 const char *zP4, /* The P4 operand */
1995 int p4type /* P4 operand type */
1996 ){
1997 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
1998 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
1999 return addr;
2000 }
2001
2002 /*
2003 ** Add an opcode that includes the p4 value with a P4_INT64 or
2004 ** P4_REAL type.
2005 */
2006 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
2007 Vdbe *p, /* Add the opcode to this VM */
2008 int op, /* The new opcode */
2009 int p1, /* The P1 operand */
2010 int p2, /* The P2 operand */
2011 int p3, /* The P3 operand */
2012 const u8 *zP4, /* The P4 operand */
2013 int p4type /* P4 operand type */
2014 ){
2015 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
2016 if( p4copy ) memcpy(p4copy, zP4, 8);
2017 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
2018 }
2019
2020 /*
2021 ** Add an OP_ParseSchema opcode. This routine is broken out from
2022 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
2023 ** as having been used.
2024 **
2025 ** The zWhere string must have been obtained from sqlite3_malloc().
2026 ** This routine will take ownership of the allocated memory.
2027 */
2028 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
2029 int j;
2030 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
2031 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
2032 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
2033 }
2034
2035 /*
2036 ** Add an opcode that includes the p4 value as an integer.
2037 */
2038 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
2039 Vdbe *p, /* Add the opcode to this VM */
2040 int op, /* The new opcode */
2041 int p1, /* The P1 operand */
2042 int p2, /* The P2 operand */
2043 int p3, /* The P3 operand */
2044 int p4 /* The P4 operand as an integer */
2045 ){
2046 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
2047 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
2048 return addr;
2049 }
2050
2051 /*
2052 ** Create a new symbolic label for an instruction that has yet to be
2053 ** coded. The symbolic label is really just a negative number. The
2054 ** label can be used as the P2 value of an operation. Later, when
2055 ** the label is resolved to a specific address, the VDBE will scan
2056 ** through its operation list and change all values of P2 which match
2057 ** the label into the resolved address.
2058 **
2059 ** The VDBE knows that a P2 value is a label because labels are
2060 ** always negative and P2 values are suppose to be non-negative.
2061 ** Hence, a negative P2 value is a label that has yet to be resolved.
2062 **
2063 ** Zero is returned if a malloc() fails.
2064 */
2065 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
2066 Parse *p = v->pParse;
2067 int i = p->nLabel++;
2068 assert( v->magic==VDBE_MAGIC_INIT );
2069 if( (i & (i-1))==0 ){
2070 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
2071 (i*2+1)*sizeof(p->aLabel[0]));
2072 }
2073 if( p->aLabel ){
2074 p->aLabel[i] = -1;
2075 }
2076 return ADDR(i);
2077 }
2078
2079 /*
2080 ** Resolve label "x" to be the address of the next instruction to
2081 ** be inserted. The parameter "x" must have been obtained from
2082 ** a prior call to sqlite3VdbeMakeLabel().
2083 */
2084 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
2085 Parse *p = v->pParse;
2086 int j = ADDR(x);
2087 assert( v->magic==VDBE_MAGIC_INIT );
2088 assert( j<p->nLabel );
2089 assert( j>=0 );
2090 if( p->aLabel ){
2091 p->aLabel[j] = v->nOp;
2092 }
2093 p->iFixedOp = v->nOp - 1;
2094 }
2095
2096 /*
2097 ** Mark the VDBE as one that can only be run one time.
2098 */
2099 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
2100 p->runOnlyOnce = 1;
2101 }
2102
2103 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
2104
2105 /*
2106 ** The following type and function are used to iterate through all opcodes
2107 ** in a Vdbe main program and each of the sub-programs (triggers) it may
2108 ** invoke directly or indirectly. It should be used as follows:
2109 **
2110 ** Op *pOp;
2111 ** VdbeOpIter sIter;
2112 **
2113 ** memset(&sIter, 0, sizeof(sIter));
2114 ** sIter.v = v; // v is of type Vdbe*
2115 ** while( (pOp = opIterNext(&sIter)) ){
2116 ** // Do something with pOp
2117 ** }
2118 ** sqlite3DbFree(v->db, sIter.apSub);
2119 **
2120 */
2121 typedef struct VdbeOpIter VdbeOpIter;
2122 struct VdbeOpIter {
2123 Vdbe *v; /* Vdbe to iterate through the opcodes of */
2124 SubProgram **apSub; /* Array of subprograms */
2125 int nSub; /* Number of entries in apSub */
2126 int iAddr; /* Address of next instruction to return */
2127 int iSub; /* 0 = main program, 1 = first sub-program etc. */
2128 };
2129 static Op *opIterNext(VdbeOpIter *p){
2130 Vdbe *v = p->v;
2131 Op *pRet = 0;
2132 Op *aOp;
2133 int nOp;
2134
2135 if( p->iSub<=p->nSub ){
2136
2137 if( p->iSub==0 ){
2138 aOp = v->aOp;
2139 nOp = v->nOp;
2140 }else{
2141 aOp = p->apSub[p->iSub-1]->aOp;
2142 nOp = p->apSub[p->iSub-1]->nOp;
2143 }
2144 assert( p->iAddr<nOp );
2145
2146 pRet = &aOp[p->iAddr];
2147 p->iAddr++;
2148 if( p->iAddr==nOp ){
2149 p->iSub++;
2150 p->iAddr = 0;
2151 }
2152
2153 if( pRet->p4type==P4_SUBPROGRAM ){
2154 int nByte = (p->nSub+1)*sizeof(SubProgram*);
2155 int j;
2156 for(j=0; j<p->nSub; j++){
2157 if( p->apSub[j]==pRet->p4.pProgram ) break;
2158 }
2159 if( j==p->nSub ){
2160 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
2161 if( !p->apSub ){
2162 pRet = 0;
2163 }else{
2164 p->apSub[p->nSub++] = pRet->p4.pProgram;
2165 }
2166 }
2167 }
2168 }
2169
2170 return pRet;
2171 }
2172
2173 /*
2174 ** Check if the program stored in the VM associated with pParse may
2175 ** throw an ABORT exception (causing the statement, but not entire transaction
2176 ** to be rolled back). This condition is true if the main program or any
2177 ** sub-programs contains any of the following:
2178 **
2179 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
2180 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
2181 ** * OP_Destroy
2182 ** * OP_VUpdate
2183 ** * OP_VRename
2184 ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
2185 ** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
2186 **
2187 ** Then check that the value of Parse.mayAbort is true if an
2188 ** ABORT may be thrown, or false otherwise. Return true if it does
2189 ** match, or false otherwise. This function is intended to be used as
2190 ** part of an assert statement in the compiler. Similar to:
2191 **
2192 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
2193 */
2194 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
2195 int hasAbort = 0;
2196 int hasFkCounter = 0;
2197 int hasCreateTable = 0;
2198 int hasInitCoroutine = 0;
2199 Op *pOp;
2200 VdbeOpIter sIter;
2201 memset(&sIter, 0, sizeof(sIter));
2202 sIter.v = v;
2203
2204 while( (pOp = opIterNext(&sIter))!=0 ){
2205 int opcode = pOp->opcode;
2206 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
2207 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
2208 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
2209 ){
2210 hasAbort = 1;
2211 break;
2212 }
2213 if( opcode==OP_CreateTable ) hasCreateTable = 1;
2214 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
2215 #ifndef SQLITE_OMIT_FOREIGN_KEY
2216 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
2217 hasFkCounter = 1;
2218 }
2219 #endif
2220 }
2221 sqlite3DbFree(v->db, sIter.apSub);
2222
2223 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
2224 ** If malloc failed, then the while() loop above may not have iterated
2225 ** through all opcodes and hasAbort may be set incorrectly. Return
2226 ** true for this case to prevent the assert() in the callers frame
2227 ** from failing. */
2228 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
2229 || (hasCreateTable && hasInitCoroutine) );
2230 }
2231 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
2232
2233 /*
2234 ** This routine is called after all opcodes have been inserted. It loops
2235 ** through all the opcodes and fixes up some details.
2236 **
2237 ** (1) For each jump instruction with a negative P2 value (a label)
2238 ** resolve the P2 value to an actual address.
2239 **
2240 ** (2) Compute the maximum number of arguments used by any SQL function
2241 ** and store that value in *pMaxFuncArgs.
2242 **
2243 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
2244 ** indicate what the prepared statement actually does.
2245 **
2246 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
2247 **
2248 ** (5) Reclaim the memory allocated for storing labels.
2249 */
2250 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
2251 int i;
2252 int nMaxArgs = *pMaxFuncArgs;
2253 Op *pOp;
2254 Parse *pParse = p->pParse;
2255 int *aLabel = pParse->aLabel;
2256 p->readOnly = 1;
2257 p->bIsReader = 0;
2258 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
2259 u8 opcode = pOp->opcode;
2260
2261 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
2262 ** cases from this switch! */
2263 switch( opcode ){
2264 case OP_Transaction: {
2265 if( pOp->p2!=0 ) p->readOnly = 0;
2266 /* fall thru */
2267 }
2268 case OP_AutoCommit:
2269 case OP_Savepoint: {
2270 p->bIsReader = 1;
2271 break;
2272 }
2273 #ifndef SQLITE_OMIT_WAL
2274 case OP_Checkpoint:
2275 #endif
2276 case OP_Vacuum:
2277 case OP_JournalMode: {
2278 p->readOnly = 0;
2279 p->bIsReader = 1;
2280 break;
2281 }
2282 #ifndef SQLITE_OMIT_VIRTUALTABLE
2283 case OP_VUpdate: {
2284 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
2285 break;
2286 }
2287 case OP_VFilter: {
2288 int n;
2289 assert( p->nOp - i >= 3 );
2290 assert( pOp[-1].opcode==OP_Integer );
2291 n = pOp[-1].p1;
2292 if( n>nMaxArgs ) nMaxArgs = n;
2293 break;
2294 }
2295 #endif
2296 case OP_Next:
2297 case OP_NextIfOpen:
2298 case OP_SorterNext: {
2299 pOp->p4.xAdvance = sqlite3BtreeNext;
2300 pOp->p4type = P4_ADVANCE;
2301 break;
2302 }
2303 case OP_Prev:
2304 case OP_PrevIfOpen: {
2305 pOp->p4.xAdvance = sqlite3BtreePrevious;
2306 pOp->p4type = P4_ADVANCE;
2307 break;
2308 }
2309 }
2310
2311 pOp->opflags = sqlite3OpcodeProperty[opcode];
2312 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
2313 assert( ADDR(pOp->p2)<pParse->nLabel );
2314 pOp->p2 = aLabel[ADDR(pOp->p2)];
2315 }
2316 }
2317 sqlite3DbFree(p->db, pParse->aLabel);
2318 pParse->aLabel = 0;
2319 pParse->nLabel = 0;
2320 *pMaxFuncArgs = nMaxArgs;
2321 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
2322 }
2323
2324 /*
2325 ** Return the address of the next instruction to be inserted.
2326 */
2327 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
2328 assert( p->magic==VDBE_MAGIC_INIT );
2329 return p->nOp;
2330 }
2331
2332 /*
2333 ** This function returns a pointer to the array of opcodes associated with
2334 ** the Vdbe passed as the first argument. It is the callers responsibility
2335 ** to arrange for the returned array to be eventually freed using the
2336 ** vdbeFreeOpArray() function.
2337 **
2338 ** Before returning, *pnOp is set to the number of entries in the returned
2339 ** array. Also, *pnMaxArg is set to the larger of its current value and
2340 ** the number of entries in the Vdbe.apArg[] array required to execute the
2341 ** returned program.
2342 */
2343 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg) {
2344 VdbeOp *aOp = p->aOp;
2345 assert( aOp && !p->db->mallocFailed );
2346
2347 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
2348 assert( DbMaskAllZero(p->btreeMask) );
2349
2350 resolveP2Values(p, pnMaxArg);
2351 *pnOp = p->nOp;
2352 p->aOp = 0;
2353 return aOp;
2354 }
2355
2356 /*
2357 ** Add a whole list of operations to the operation stack. Return the
2358 ** address of the first operation added.
2359 */
2360 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
2361 int addr, i;
2362 VdbeOp *pOut;
2363 assert( nOp>0 );
2364 assert( p->magic==VDBE_MAGIC_INIT );
2365 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
2366 return 0;
2367 }
2368 addr = p->nOp;
2369 pOut = &p->aOp[addr];
2370 for(i=0; i<nOp; i++, aOp++, pOut++){
2371 pOut->opcode = aOp->opcode;
2372 pOut->p1 = aOp->p1;
2373 pOut->p2 = aOp->p2;
2374 assert( aOp->p2>=0 );
2375 pOut->p3 = aOp->p3;
2376 pOut->p4type = P4_NOTUSED;
2377 pOut->p4.p = 0;
2378 pOut->p5 = 0;
2379 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
2380 pOut->zComment = 0;
2381 #endif
2382 #ifdef SQLITE_VDBE_COVERAGE
2383 pOut->iSrcLine = iLineno+i;
2384 #else
2385 (void)iLineno;
2386 #endif
2387 #ifdef SQLITE_DEBUG
2388 if( p->db->flags & SQLITE_VdbeAddopTrace ){
2389 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
2390 }
2391 #endif
2392 }
2393 p->nOp += nOp;
2394 return addr;
2395 }
2396
2397 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
2398 /*
2399 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
2400 */
2401 SQLITE_PRIVATE void sqlite3VdbeScanStatus(
2402 Vdbe *p, /* VM to add scanstatus() to */
2403 int addrExplain, /* Address of OP_Explain (or 0) */
2404 int addrLoop, /* Address of loop counter */
2405 int addrVisit, /* Address of rows visited counter */
2406 LogEst nEst, /* Estimated number of output rows */
2407 const char *zName /* Name of table or index being scanned */
2408 ){
2409 int nByte = (p->nScan+1) * sizeof(ScanStatus);
2410 ScanStatus *aNew;
2411 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
2412 if( aNew ){
2413 ScanStatus *pNew = &aNew[p->nScan++];
2414 pNew->addrExplain = addrExplain;
2415 pNew->addrLoop = addrLoop;
2416 pNew->addrVisit = addrVisit;
2417 pNew->nEst = nEst;
2418 pNew->zName = sqlite3DbStrDup(p->db, zName);
2419 p->aScan = aNew;
2420 }
2421 }
2422 #endif
2423
2424
2425 /*
2426 ** Change the value of the opcode, or P1, P2, P3, or P5 operands
2427 ** for a specific instruction.
2428 */
2429 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
2430 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
2431 }
2432 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
2433 sqlite3VdbeGetOp(p,addr)->p1 = val;
2434 }
2435 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
2436 sqlite3VdbeGetOp(p,addr)->p2 = val;
2437 }
2438 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
2439 sqlite3VdbeGetOp(p,addr)->p3 = val;
2440 }
2441 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
2442 sqlite3VdbeGetOp(p,-1)->p5 = p5;
2443 }
2444
2445 /*
2446 ** Change the P2 operand of instruction addr so that it points to
2447 ** the address of the next instruction to be coded.
2448 */
2449 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
2450 p->pParse->iFixedOp = p->nOp - 1;
2451 sqlite3VdbeChangeP2(p, addr, p->nOp);
2452 }
2453
2454
2455 /*
2456 ** If the input FuncDef structure is ephemeral, then free it. If
2457 ** the FuncDef is not ephermal, then do nothing.
2458 */
2459 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
2460 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
2461 sqlite3DbFree(db, pDef);
2462 }
2463 }
2464
2465 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
2466
2467 /*
2468 ** Delete a P4 value if necessary.
2469 */
2470 static void freeP4(sqlite3 *db, int p4type, void *p4){
2471 if( p4 ){
2472 assert( db );
2473 switch( p4type ){
2474 case P4_FUNCCTX: {
2475 freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
2476 /* Fall through into the next case */
2477 }
2478 case P4_REAL:
2479 case P4_INT64:
2480 case P4_DYNAMIC:
2481 case P4_INTARRAY: {
2482 sqlite3DbFree(db, p4);
2483 break;
2484 }
2485 case P4_KEYINFO: {
2486 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
2487 break;
2488 }
2489 #ifdef SQLITE_ENABLE_CURSOR_HINTS
2490 case P4_EXPR: {
2491 sqlite3ExprDelete(db, (Expr*)p4);
2492 break;
2493 }
2494 #endif
2495 case P4_MPRINTF: {
2496 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
2497 break;
2498 }
2499 case P4_FUNCDEF: {
2500 freeEphemeralFunction(db, (FuncDef*)p4);
2501 break;
2502 }
2503 case P4_MEM: {
2504 if( db->pnBytesFreed==0 ){
2505 sqlite3ValueFree((sqlite3_value*)p4);
2506 }else{
2507 Mem *p = (Mem*)p4;
2508 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
2509 sqlite3DbFree(db, p);
2510 }
2511 break;
2512 }
2513 case P4_VTAB : {
2514 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
2515 break;
2516 }
2517 }
2518 }
2519 }
2520
2521 /*
2522 ** Free the space allocated for aOp and any p4 values allocated for the
2523 ** opcodes contained within. If aOp is not NULL it is assumed to contain
2524 ** nOp entries.
2525 */
2526 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
2527 if( aOp ){
2528 Op *pOp;
2529 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
2530 freeP4(db, pOp->p4type, pOp->p4.p);
2531 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
2532 sqlite3DbFree(db, pOp->zComment);
2533 #endif
2534 }
2535 }
2536 sqlite3DbFree(db, aOp);
2537 }
2538
2539 /*
2540 ** Link the SubProgram object passed as the second argument into the linked
2541 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
2542 ** objects when the VM is no longer required.
2543 */
2544 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
2545 p->pNext = pVdbe->pProgram;
2546 pVdbe->pProgram = p;
2547 }
2548
2549 /*
2550 ** Change the opcode at addr into OP_Noop
2551 */
2552 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
2553 if( addr<p->nOp ){
2554 VdbeOp *pOp = &p->aOp[addr];
2555 sqlite3 *db = p->db;
2556 freeP4(db, pOp->p4type, pOp->p4.p);
2557 memset(pOp, 0, sizeof(pOp[0]));
2558 pOp->opcode = OP_Noop;
2559 }
2560 }
2561
2562 /*
2563 ** If the last opcode is "op" and it is not a jump destination,
2564 ** then remove it. Return true if and only if an opcode was removed.
2565 */
2566 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
2567 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
2568 sqlite3VdbeChangeToNoop(p, p->nOp-1);
2569 return 1;
2570 }else{
2571 return 0;
2572 }
2573 }
2574
2575 /*
2576 ** Change the value of the P4 operand for a specific instruction.
2577 ** This routine is useful when a large program is loaded from a
2578 ** static array using sqlite3VdbeAddOpList but we want to make a
2579 ** few minor changes to the program.
2580 **
2581 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
2582 ** the string is made into memory obtained from sqlite3_malloc().
2583 ** A value of n==0 means copy bytes of zP4 up to and including the
2584 ** first null byte. If n>0 then copy n+1 bytes of zP4.
2585 **
2586 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
2587 ** to a string or structure that is guaranteed to exist for the lifetime of
2588 ** the Vdbe. In these cases we can just copy the pointer.
2589 **
2590 ** If addr<0 then change P4 on the most recently inserted instruction.
2591 */
2592 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
2593 Op *pOp;
2594 sqlite3 *db;
2595 assert( p!=0 );
2596 db = p->db;
2597 assert( p->magic==VDBE_MAGIC_INIT );
2598 if( p->aOp==0 || db->mallocFailed ){
2599 if( n!=P4_VTAB ){
2600 freeP4(db, n, (void*)*(char**)&zP4);
2601 }
2602 return;
2603 }
2604 assert( p->nOp>0 );
2605 assert( addr<p->nOp );
2606 if( addr<0 ){
2607 addr = p->nOp - 1;
2608 }
2609 pOp = &p->aOp[addr];
2610 assert( pOp->p4type==P4_NOTUSED
2611 || pOp->p4type==P4_INT32
2612 || pOp->p4type==P4_KEYINFO );
2613 freeP4(db, pOp->p4type, pOp->p4.p);
2614 pOp->p4.p = 0;
2615 if( n==P4_INT32 ){
2616 /* Note: this cast is safe, because the origin data point was an int
2617 ** that was cast to a (const char *). */
2618 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
2619 pOp->p4type = P4_INT32;
2620 }else if( zP4==0 ){
2621 pOp->p4.p = 0;
2622 pOp->p4type = P4_NOTUSED;
2623 }else if( n==P4_KEYINFO ){
2624 pOp->p4.p = (void*)zP4;
2625 pOp->p4type = P4_KEYINFO;
2626 #ifdef SQLITE_ENABLE_CURSOR_HINTS
2627 }else if( n==P4_EXPR ){
2628 /* Responsibility for deleting the Expr tree is handed over to the
2629 ** VDBE by this operation. The caller should have already invoked
2630 ** sqlite3ExprDup() or whatever other routine is needed to make a
2631 ** private copy of the tree. */
2632 pOp->p4.pExpr = (Expr*)zP4;
2633 pOp->p4type = P4_EXPR;
2634 #endif
2635 }else if( n==P4_VTAB ){
2636 pOp->p4.p = (void*)zP4;
2637 pOp->p4type = P4_VTAB;
2638 sqlite3VtabLock((VTable *)zP4);
2639 assert( ((VTable *)zP4)->db==p->db );
2640 }else if( n<0 ){
2641 pOp->p4.p = (void*)zP4;
2642 pOp->p4type = (signed char)n;
2643 }else{
2644 if( n==0 ) n = sqlite3Strlen30(zP4);
2645 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
2646 pOp->p4type = P4_DYNAMIC;
2647 }
2648 }
2649
2650 /*
2651 ** Set the P4 on the most recently added opcode to the KeyInfo for the
2652 ** index given.
2653 */
2654 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
2655 Vdbe *v = pParse->pVdbe;
2656 assert( v!=0 );
2657 assert( pIdx!=0 );
2658 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
2659 P4_KEYINFO);
2660 }
2661
2662 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
2663 /*
2664 ** Change the comment on the most recently coded instruction. Or
2665 ** insert a No-op and add the comment to that new instruction. This
2666 ** makes the code easier to read during debugging. None of this happens
2667 ** in a production build.
2668 */
2669 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
2670 assert( p->nOp>0 || p->aOp==0 );
2671 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
2672 if( p->nOp ){
2673 assert( p->aOp );
2674 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
2675 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
2676 }
2677 }
2678 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
2679 va_list ap;
2680 if( p ){
2681 va_start(ap, zFormat);
2682 vdbeVComment(p, zFormat, ap);
2683 va_end(ap);
2684 }
2685 }
2686 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
2687 va_list ap;
2688 if( p ){
2689 sqlite3VdbeAddOp0(p, OP_Noop);
2690 va_start(ap, zFormat);
2691 vdbeVComment(p, zFormat, ap);
2692 va_end(ap);
2693 }
2694 }
2695 #endif /* NDEBUG */
2696
2697 #ifdef SQLITE_VDBE_COVERAGE
2698 /*
2699 ** Set the value if the iSrcLine field for the previously coded instruction.
2700 */
2701 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
2702 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
2703 }
2704 #endif /* SQLITE_VDBE_COVERAGE */
2705
2706 /*
2707 ** Return the opcode for a given address. If the address is -1, then
2708 ** return the most recently inserted opcode.
2709 **
2710 ** If a memory allocation error has occurred prior to the calling of this
2711 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
2712 ** is readable but not writable, though it is cast to a writable value.
2713 ** The return of a dummy opcode allows the call to continue functioning
2714 ** after an OOM fault without having to check to see if the return from
2715 ** this routine is a valid pointer. But because the dummy.opcode is 0,
2716 ** dummy will never be written to. This is verified by code inspection and
2717 ** by running with Valgrind.
2718 */
2719 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
2720 /* C89 specifies that the constant "dummy" will be initialized to all
2721 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
2722 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
2723 assert( p->magic==VDBE_MAGIC_INIT );
2724 if( addr<0 ){
2725 addr = p->nOp - 1;
2726 }
2727 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
2728 if( p->db->mallocFailed ){
2729 return (VdbeOp*)&dummy;
2730 }else{
2731 return &p->aOp[addr];
2732 }
2733 }
2734
2735 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
2736 /*
2737 ** Return an integer value for one of the parameters to the opcode pOp
2738 ** determined by character c.
2739 */
2740 static int translateP(char c, const Op *pOp){
2741 if( c=='1' ) return pOp->p1;
2742 if( c=='2' ) return pOp->p2;
2743 if( c=='3' ) return pOp->p3;
2744 if( c=='4' ) return pOp->p4.i;
2745 return pOp->p5;
2746 }
2747
2748 /*
2749 ** Compute a string for the "comment" field of a VDBE opcode listing.
2750 **
2751 ** The Synopsis: field in comments in the vdbe.c source file gets converted
2752 ** to an extra string that is appended to the sqlite3OpcodeName(). In the
2753 ** absence of other comments, this synopsis becomes the comment on the opcode.
2754 ** Some translation occurs:
2755 **
2756 ** "PX" -> "r[X]"
2757 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
2758 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
2759 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
2760 */
2761 static int displayComment(
2762 const Op *pOp, /* The opcode to be commented */
2763 const char *zP4, /* Previously obtained value for P4 */
2764 char *zTemp, /* Write result here */
2765 int nTemp /* Space available in zTemp[] */
2766 ){
2767 const char *zOpName;
2768 const char *zSynopsis;
2769 int nOpName;
2770 int ii, jj;
2771 zOpName = sqlite3OpcodeName(pOp->opcode);
2772 nOpName = sqlite3Strlen30(zOpName);
2773 if( zOpName[nOpName+1] ){
2774 int seenCom = 0;
2775 char c;
2776 zSynopsis = zOpName += nOpName + 1;
2777 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
2778 if( c=='P' ){
2779 c = zSynopsis[++ii];
2780 if( c=='4' ){
2781 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
2782 }else if( c=='X' ){
2783 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
2784 seenCom = 1;
2785 }else{
2786 int v1 = translateP(c, pOp);
2787 int v2;
2788 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
2789 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
2790 ii += 3;
2791 jj += sqlite3Strlen30(zTemp+jj);
2792 v2 = translateP(zSynopsis[ii], pOp);
2793 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
2794 ii += 2;
2795 v2++;
2796 }
2797 if( v2>1 ){
2798 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
2799 }
2800 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
2801 ii += 4;
2802 }
2803 }
2804 jj += sqlite3Strlen30(zTemp+jj);
2805 }else{
2806 zTemp[jj++] = c;
2807 }
2808 }
2809 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
2810 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
2811 jj += sqlite3Strlen30(zTemp+jj);
2812 }
2813 if( jj<nTemp ) zTemp[jj] = 0;
2814 }else if( pOp->zComment ){
2815 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
2816 jj = sqlite3Strlen30(zTemp);
2817 }else{
2818 zTemp[0] = 0;
2819 jj = 0;
2820 }
2821 return jj;
2822 }
2823 #endif /* SQLITE_DEBUG */
2824
2825 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
2826 /*
2827 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
2828 ** that can be displayed in the P4 column of EXPLAIN output.
2829 */
2830 static int displayP4Expr(int nTemp, char *zTemp, Expr *pExpr){
2831 const char *zOp = 0;
2832 int n;
2833 switch( pExpr->op ){
2834 case TK_STRING:
2835 sqlite3_snprintf(nTemp, zTemp, "%Q", pExpr->u.zToken);
2836 break;
2837 case TK_INTEGER:
2838 sqlite3_snprintf(nTemp, zTemp, "%d", pExpr->u.iValue);
2839 break;
2840 case TK_NULL:
2841 sqlite3_snprintf(nTemp, zTemp, "NULL");
2842 break;
2843 case TK_REGISTER: {
2844 sqlite3_snprintf(nTemp, zTemp, "r[%d]", pExpr->iTable);
2845 break;
2846 }
2847 case TK_COLUMN: {
2848 if( pExpr->iColumn<0 ){
2849 sqlite3_snprintf(nTemp, zTemp, "rowid");
2850 }else{
2851 sqlite3_snprintf(nTemp, zTemp, "c%d", (int)pExpr->iColumn);
2852 }
2853 break;
2854 }
2855 case TK_LT: zOp = "LT"; break;
2856 case TK_LE: zOp = "LE"; break;
2857 case TK_GT: zOp = "GT"; break;
2858 case TK_GE: zOp = "GE"; break;
2859 case TK_NE: zOp = "NE"; break;
2860 case TK_EQ: zOp = "EQ"; break;
2861 case TK_IS: zOp = "IS"; break;
2862 case TK_ISNOT: zOp = "ISNOT"; break;
2863 case TK_AND: zOp = "AND"; break;
2864 case TK_OR: zOp = "OR"; break;
2865 case TK_PLUS: zOp = "ADD"; break;
2866 case TK_STAR: zOp = "MUL"; break;
2867 case TK_MINUS: zOp = "SUB"; break;
2868 case TK_REM: zOp = "REM"; break;
2869 case TK_BITAND: zOp = "BITAND"; break;
2870 case TK_BITOR: zOp = "BITOR"; break;
2871 case TK_SLASH: zOp = "DIV"; break;
2872 case TK_LSHIFT: zOp = "LSHIFT"; break;
2873 case TK_RSHIFT: zOp = "RSHIFT"; break;
2874 case TK_CONCAT: zOp = "CONCAT"; break;
2875 case TK_UMINUS: zOp = "MINUS"; break;
2876 case TK_UPLUS: zOp = "PLUS"; break;
2877 case TK_BITNOT: zOp = "BITNOT"; break;
2878 case TK_NOT: zOp = "NOT"; break;
2879 case TK_ISNULL: zOp = "ISNULL"; break;
2880 case TK_NOTNULL: zOp = "NOTNULL"; break;
2881
2882 default:
2883 sqlite3_snprintf(nTemp, zTemp, "%s", "expr");
2884 break;
2885 }
2886
2887 if( zOp ){
2888 sqlite3_snprintf(nTemp, zTemp, "%s(", zOp);
2889 n = sqlite3Strlen30(zTemp);
2890 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pLeft);
2891 if( n<nTemp-1 && pExpr->pRight ){
2892 zTemp[n++] = ',';
2893 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pRight);
2894 }
2895 sqlite3_snprintf(nTemp-n, zTemp+n, ")");
2896 }
2897 return sqlite3Strlen30(zTemp);
2898 }
2899 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
2900
2901
2902 #if VDBE_DISPLAY_P4
2903 /*
2904 ** Compute a string that describes the P4 parameter for an opcode.
2905 ** Use zTemp for any required temporary buffer space.
2906 */
2907 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
2908 char *zP4 = zTemp;
2909 assert( nTemp>=20 );
2910 switch( pOp->p4type ){
2911 case P4_KEYINFO: {
2912 int i, j;
2913 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
2914 assert( pKeyInfo->aSortOrder!=0 );
2915 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
2916 i = sqlite3Strlen30(zTemp);
2917 for(j=0; j<pKeyInfo->nField; j++){
2918 CollSeq *pColl = pKeyInfo->aColl[j];
2919 const char *zColl = pColl ? pColl->zName : "nil";
2920 int n = sqlite3Strlen30(zColl);
2921 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
2922 zColl = "B";
2923 n = 1;
2924 }
2925 if( i+n>nTemp-7 ){
2926 memcpy(&zTemp[i],",...",4);
2927 i += 4;
2928 break;
2929 }
2930 zTemp[i++] = ',';
2931 if( pKeyInfo->aSortOrder[j] ){
2932 zTemp[i++] = '-';
2933 }
2934 memcpy(&zTemp[i], zColl, n+1);
2935 i += n;
2936 }
2937 zTemp[i++] = ')';
2938 zTemp[i] = 0;
2939 assert( i<nTemp );
2940 break;
2941 }
2942 #ifdef SQLITE_ENABLE_CURSOR_HINTS
2943 case P4_EXPR: {
2944 displayP4Expr(nTemp, zTemp, pOp->p4.pExpr);
2945 break;
2946 }
2947 #endif
2948 case P4_COLLSEQ: {
2949 CollSeq *pColl = pOp->p4.pColl;
2950 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
2951 break;
2952 }
2953 case P4_FUNCDEF: {
2954 FuncDef *pDef = pOp->p4.pFunc;
2955 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
2956 break;
2957 }
2958 #ifdef SQLITE_DEBUG
2959 case P4_FUNCCTX: {
2960 FuncDef *pDef = pOp->p4.pCtx->pFunc;
2961 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
2962 break;
2963 }
2964 #endif
2965 case P4_INT64: {
2966 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
2967 break;
2968 }
2969 case P4_INT32: {
2970 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
2971 break;
2972 }
2973 case P4_REAL: {
2974 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
2975 break;
2976 }
2977 case P4_MEM: {
2978 Mem *pMem = pOp->p4.pMem;
2979 if( pMem->flags & MEM_Str ){
2980 zP4 = pMem->z;
2981 }else if( pMem->flags & MEM_Int ){
2982 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
2983 }else if( pMem->flags & MEM_Real ){
2984 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
2985 }else if( pMem->flags & MEM_Null ){
2986 sqlite3_snprintf(nTemp, zTemp, "NULL");
2987 }else{
2988 assert( pMem->flags & MEM_Blob );
2989 zP4 = "(blob)";
2990 }
2991 break;
2992 }
2993 #ifndef SQLITE_OMIT_VIRTUALTABLE
2994 case P4_VTAB: {
2995 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
2996 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
2997 break;
2998 }
2999 #endif
3000 case P4_INTARRAY: {
3001 sqlite3_snprintf(nTemp, zTemp, "intarray");
3002 break;
3003 }
3004 case P4_SUBPROGRAM: {
3005 sqlite3_snprintf(nTemp, zTemp, "program");
3006 break;
3007 }
3008 case P4_ADVANCE: {
3009 zTemp[0] = 0;
3010 break;
3011 }
3012 default: {
3013 zP4 = pOp->p4.z;
3014 if( zP4==0 ){
3015 zP4 = zTemp;
3016 zTemp[0] = 0;
3017 }
3018 }
3019 }
3020 assert( zP4!=0 );
3021 return zP4;
3022 }
3023 #endif /* VDBE_DISPLAY_P4 */
3024
3025 /*
3026 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
3027 **
3028 ** The prepared statements need to know in advance the complete set of
3029 ** attached databases that will be use. A mask of these databases
3030 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
3031 ** p->btreeMask of databases that will require a lock.
3032 */
3033 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
3034 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
3035 assert( i<(int)sizeof(p->btreeMask)*8 );
3036 DbMaskSet(p->btreeMask, i);
3037 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
3038 DbMaskSet(p->lockMask, i);
3039 }
3040 }
3041
3042 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
3043 /*
3044 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
3045 ** this routine obtains the mutex associated with each BtShared structure
3046 ** that may be accessed by the VM passed as an argument. In doing so it also
3047 ** sets the BtShared.db member of each of the BtShared structures, ensuring
3048 ** that the correct busy-handler callback is invoked if required.
3049 **
3050 ** If SQLite is not threadsafe but does support shared-cache mode, then
3051 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
3052 ** of all of BtShared structures accessible via the database handle
3053 ** associated with the VM.
3054 **
3055 ** If SQLite is not threadsafe and does not support shared-cache mode, this
3056 ** function is a no-op.
3057 **
3058 ** The p->btreeMask field is a bitmask of all btrees that the prepared
3059 ** statement p will ever use. Let N be the number of bits in p->btreeMask
3060 ** corresponding to btrees that use shared cache. Then the runtime of
3061 ** this routine is N*N. But as N is rarely more than 1, this should not
3062 ** be a problem.
3063 */
3064 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
3065 int i;
3066 sqlite3 *db;
3067 Db *aDb;
3068 int nDb;
3069 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
3070 db = p->db;
3071 aDb = db->aDb;
3072 nDb = db->nDb;
3073 for(i=0; i<nDb; i++){
3074 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
3075 sqlite3BtreeEnter(aDb[i].pBt);
3076 }
3077 }
3078 }
3079 #endif
3080
3081 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
3082 /*
3083 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
3084 */
3085 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
3086 int i;
3087 sqlite3 *db;
3088 Db *aDb;
3089 int nDb;
3090 db = p->db;
3091 aDb = db->aDb;
3092 nDb = db->nDb;
3093 for(i=0; i<nDb; i++){
3094 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
3095 sqlite3BtreeLeave(aDb[i].pBt);
3096 }
3097 }
3098 }
3099 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
3100 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
3101 vdbeLeave(p);
3102 }
3103 #endif
3104
3105 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
3106 /*
3107 ** Print a single opcode. This routine is used for debugging only.
3108 */
3109 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
3110 char *zP4;
3111 char zPtr[50];
3112 char zCom[100];
3113 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
3114 if( pOut==0 ) pOut = stdout;
3115 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
3116 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
3117 displayComment(pOp, zP4, zCom, sizeof(zCom));
3118 #else
3119 zCom[0] = 0;
3120 #endif
3121 /* NB: The sqlite3OpcodeName() function is implemented by code created
3122 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
3123 ** information from the vdbe.c source text */
3124 fprintf(pOut, zFormat1, pc,
3125 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
3126 zCom
3127 );
3128 fflush(pOut);
3129 }
3130 #endif
3131
3132 /*
3133 ** Release an array of N Mem elements
3134 */
3135 static void releaseMemArray(Mem *p, int N){
3136 if( p && N ){
3137 Mem *pEnd = &p[N];
3138 sqlite3 *db = p->db;
3139 u8 malloc_failed = db->mallocFailed;
3140 if( db->pnBytesFreed ){
3141 do{
3142 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
3143 }while( (++p)<pEnd );
3144 return;
3145 }
3146 do{
3147 assert( (&p[1])==pEnd || p[0].db==p[1].db );
3148 assert( sqlite3VdbeCheckMemInvariants(p) );
3149
3150 /* This block is really an inlined version of sqlite3VdbeMemRelease()
3151 ** that takes advantage of the fact that the memory cell value is
3152 ** being set to NULL after releasing any dynamic resources.
3153 **
3154 ** The justification for duplicating code is that according to
3155 ** callgrind, this causes a certain test case to hit the CPU 4.7
3156 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
3157 ** sqlite3MemRelease() were called from here. With -O2, this jumps
3158 ** to 6.6 percent. The test case is inserting 1000 rows into a table
3159 ** with no indexes using a single prepared INSERT statement, bind()
3160 ** and reset(). Inserts are grouped into a transaction.
3161 */
3162 testcase( p->flags & MEM_Agg );
3163 testcase( p->flags & MEM_Dyn );
3164 testcase( p->flags & MEM_Frame );
3165 testcase( p->flags & MEM_RowSet );
3166 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
3167 sqlite3VdbeMemRelease(p);
3168 }else if( p->szMalloc ){
3169 sqlite3DbFree(db, p->zMalloc);
3170 p->szMalloc = 0;
3171 }
3172
3173 p->flags = MEM_Undefined;
3174 }while( (++p)<pEnd );
3175 db->mallocFailed = malloc_failed;
3176 }
3177 }
3178
3179 /*
3180 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
3181 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
3182 */
3183 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
3184 int i;
3185 Mem *aMem = VdbeFrameMem(p);
3186 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
3187 for(i=0; i<p->nChildCsr; i++){
3188 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
3189 }
3190 releaseMemArray(aMem, p->nChildMem);
3191 sqlite3DbFree(p->v->db, p);
3192 }
3193
3194 #ifndef SQLITE_OMIT_EXPLAIN
3195 /*
3196 ** Give a listing of the program in the virtual machine.
3197 **
3198 ** The interface is the same as sqlite3VdbeExec(). But instead of
3199 ** running the code, it invokes the callback once for each instruction.
3200 ** This feature is used to implement "EXPLAIN".
3201 **
3202 ** When p->explain==1, each instruction is listed. When
3203 ** p->explain==2, only OP_Explain instructions are listed and these
3204 ** are shown in a different format. p->explain==2 is used to implement
3205 ** EXPLAIN QUERY PLAN.
3206 **
3207 ** When p->explain==1, first the main program is listed, then each of
3208 ** the trigger subprograms are listed one by one.
3209 */
3210 SQLITE_PRIVATE int sqlite3VdbeList(
3211 Vdbe *p /* The VDBE */
3212 ){
3213 int nRow; /* Stop when row count reaches this */
3214 int nSub = 0; /* Number of sub-vdbes seen so far */
3215 SubProgram **apSub = 0; /* Array of sub-vdbes */
3216 Mem *pSub = 0; /* Memory cell hold array of subprogs */
3217 sqlite3 *db = p->db; /* The database connection */
3218 int i; /* Loop counter */
3219 int rc = SQLITE_OK; /* Return code */
3220 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
3221
3222 assert( p->explain );
3223 assert( p->magic==VDBE_MAGIC_RUN );
3224 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
3225
3226 /* Even though this opcode does not use dynamic strings for
3227 ** the result, result columns may become dynamic if the user calls
3228 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
3229 */
3230 releaseMemArray(pMem, 8);
3231 p->pResultSet = 0;
3232
3233 if( p->rc==SQLITE_NOMEM ){
3234 /* This happens if a malloc() inside a call to sqlite3_column_text() or
3235 ** sqlite3_column_text16() failed. */
3236 db->mallocFailed = 1;
3237 return SQLITE_ERROR;
3238 }
3239
3240 /* When the number of output rows reaches nRow, that means the
3241 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
3242 ** nRow is the sum of the number of rows in the main program, plus
3243 ** the sum of the number of rows in all trigger subprograms encountered
3244 ** so far. The nRow value will increase as new trigger subprograms are
3245 ** encountered, but p->pc will eventually catch up to nRow.
3246 */
3247 nRow = p->nOp;
3248 if( p->explain==1 ){
3249 /* The first 8 memory cells are used for the result set. So we will
3250 ** commandeer the 9th cell to use as storage for an array of pointers
3251 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
3252 ** cells. */
3253 assert( p->nMem>9 );
3254 pSub = &p->aMem[9];
3255 if( pSub->flags&MEM_Blob ){
3256 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
3257 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
3258 nSub = pSub->n/sizeof(Vdbe*);
3259 apSub = (SubProgram **)pSub->z;
3260 }
3261 for(i=0; i<nSub; i++){
3262 nRow += apSub[i]->nOp;
3263 }
3264 }
3265
3266 do{
3267 i = p->pc++;
3268 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
3269 if( i>=nRow ){
3270 p->rc = SQLITE_OK;
3271 rc = SQLITE_DONE;
3272 }else if( db->u1.isInterrupted ){
3273 p->rc = SQLITE_INTERRUPT;
3274 rc = SQLITE_ERROR;
3275 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
3276 }else{
3277 char *zP4;
3278 Op *pOp;
3279 if( i<p->nOp ){
3280 /* The output line number is small enough that we are still in the
3281 ** main program. */
3282 pOp = &p->aOp[i];
3283 }else{
3284 /* We are currently listing subprograms. Figure out which one and
3285 ** pick up the appropriate opcode. */
3286 int j;
3287 i -= p->nOp;
3288 for(j=0; i>=apSub[j]->nOp; j++){
3289 i -= apSub[j]->nOp;
3290 }
3291 pOp = &apSub[j]->aOp[i];
3292 }
3293 if( p->explain==1 ){
3294 pMem->flags = MEM_Int;
3295 pMem->u.i = i; /* Program counter */
3296 pMem++;
3297
3298 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
3299 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
3300 assert( pMem->z!=0 );
3301 pMem->n = sqlite3Strlen30(pMem->z);
3302 pMem->enc = SQLITE_UTF8;
3303 pMem++;
3304
3305 /* When an OP_Program opcode is encounter (the only opcode that has
3306 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
3307 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
3308 ** has not already been seen.
3309 */
3310 if( pOp->p4type==P4_SUBPROGRAM ){
3311 int nByte = (nSub+1)*sizeof(SubProgram*);
3312 int j;
3313 for(j=0; j<nSub; j++){
3314 if( apSub[j]==pOp->p4.pProgram ) break;
3315 }
3316 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
3317 apSub = (SubProgram **)pSub->z;
3318 apSub[nSub++] = pOp->p4.pProgram;
3319 pSub->flags |= MEM_Blob;
3320 pSub->n = nSub*sizeof(SubProgram*);
3321 }
3322 }
3323 }
3324
3325 pMem->flags = MEM_Int;
3326 pMem->u.i = pOp->p1; /* P1 */
3327 pMem++;
3328
3329 pMem->flags = MEM_Int;
3330 pMem->u.i = pOp->p2; /* P2 */
3331 pMem++;
3332
3333 pMem->flags = MEM_Int;
3334 pMem->u.i = pOp->p3; /* P3 */
3335 pMem++;
3336
3337 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
3338 assert( p->db->mallocFailed );
3339 return SQLITE_ERROR;
3340 }
3341 pMem->flags = MEM_Str|MEM_Term;
3342 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
3343 if( zP4!=pMem->z ){
3344 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
3345 }else{
3346 assert( pMem->z!=0 );
3347 pMem->n = sqlite3Strlen30(pMem->z);
3348 pMem->enc = SQLITE_UTF8;
3349 }
3350 pMem++;
3351
3352 if( p->explain==1 ){
3353 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
3354 assert( p->db->mallocFailed );
3355 return SQLITE_ERROR;
3356 }
3357 pMem->flags = MEM_Str|MEM_Term;
3358 pMem->n = 2;
3359 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
3360 pMem->enc = SQLITE_UTF8;
3361 pMem++;
3362
3363 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
3364 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
3365 assert( p->db->mallocFailed );
3366 return SQLITE_ERROR;
3367 }
3368 pMem->flags = MEM_Str|MEM_Term;
3369 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
3370 pMem->enc = SQLITE_UTF8;
3371 #else
3372 pMem->flags = MEM_Null; /* Comment */
3373 #endif
3374 }
3375
3376 p->nResColumn = 8 - 4*(p->explain-1);
3377 p->pResultSet = &p->aMem[1];
3378 p->rc = SQLITE_OK;
3379 rc = SQLITE_ROW;
3380 }
3381 return rc;
3382 }
3383 #endif /* SQLITE_OMIT_EXPLAIN */
3384
3385 #ifdef SQLITE_DEBUG
3386 /*
3387 ** Print the SQL that was used to generate a VDBE program.
3388 */
3389 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
3390 const char *z = 0;
3391 if( p->zSql ){
3392 z = p->zSql;
3393 }else if( p->nOp>=1 ){
3394 const VdbeOp *pOp = &p->aOp[0];
3395 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
3396 z = pOp->p4.z;
3397 while( sqlite3Isspace(*z) ) z++;
3398 }
3399 }
3400 if( z ) printf("SQL: [%s]\n", z);
3401 }
3402 #endif
3403
3404 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
3405 /*
3406 ** Print an IOTRACE message showing SQL content.
3407 */
3408 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
3409 int nOp = p->nOp;
3410 VdbeOp *pOp;
3411 if( sqlite3IoTrace==0 ) return;
3412 if( nOp<1 ) return;
3413 pOp = &p->aOp[0];
3414 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
3415 int i, j;
3416 char z[1000];
3417 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
3418 for(i=0; sqlite3Isspace(z[i]); i++){}
3419 for(j=0; z[i]; i++){
3420 if( sqlite3Isspace(z[i]) ){
3421 if( z[i-1]!=' ' ){
3422 z[j++] = ' ';
3423 }
3424 }else{
3425 z[j++] = z[i];
3426 }
3427 }
3428 z[j] = 0;
3429 sqlite3IoTrace("SQL %s\n", z);
3430 }
3431 }
3432 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
3433
3434 /*
3435 ** Allocate space from a fixed size buffer and return a pointer to
3436 ** that space. If insufficient space is available, return NULL.
3437 **
3438 ** The pBuf parameter is the initial value of a pointer which will
3439 ** receive the new memory. pBuf is normally NULL. If pBuf is not
3440 ** NULL, it means that memory space has already been allocated and that
3441 ** this routine should not allocate any new memory. When pBuf is not
3442 ** NULL simply return pBuf. Only allocate new memory space when pBuf
3443 ** is NULL.
3444 **
3445 ** nByte is the number of bytes of space needed.
3446 **
3447 ** pFrom points to *pnFrom bytes of available space. New space is allocated
3448 ** from the end of the pFrom buffer and *pnFrom is decremented.
3449 **
3450 ** *pnNeeded is a counter of the number of bytes of space that have failed
3451 ** to allocate. If there is insufficient space in pFrom to satisfy the
3452 ** request, then increment *pnNeeded by the amount of the request.
3453 */
3454 static void *allocSpace(
3455 void *pBuf, /* Where return pointer will be stored */
3456 int nByte, /* Number of bytes to allocate */
3457 u8 *pFrom, /* Memory available for allocation */
3458 int *pnFrom, /* IN/OUT: Space available at pFrom */
3459 int *pnNeeded /* If allocation cannot be made, increment *pnByte */
3460 ){
3461 assert( EIGHT_BYTE_ALIGNMENT(pFrom) );
3462 if( pBuf==0 ){
3463 nByte = ROUND8(nByte);
3464 if( nByte <= *pnFrom ){
3465 *pnFrom -= nByte;
3466 pBuf = &pFrom[*pnFrom];
3467 }else{
3468 *pnNeeded += nByte;
3469 }
3470 }
3471 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
3472 return pBuf;
3473 }
3474
3475 /*
3476 ** Rewind the VDBE back to the beginning in preparation for
3477 ** running it.
3478 */
3479 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
3480 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
3481 int i;
3482 #endif
3483 assert( p!=0 );
3484 assert( p->magic==VDBE_MAGIC_INIT );
3485
3486 /* There should be at least one opcode.
3487 */
3488 assert( p->nOp>0 );
3489
3490 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
3491 p->magic = VDBE_MAGIC_RUN;
3492
3493 #ifdef SQLITE_DEBUG
3494 for(i=1; i<p->nMem; i++){
3495 assert( p->aMem[i].db==p->db );
3496 }
3497 #endif
3498 p->pc = -1;
3499 p->rc = SQLITE_OK;
3500 p->errorAction = OE_Abort;
3501 p->magic = VDBE_MAGIC_RUN;
3502 p->nChange = 0;
3503 p->cacheCtr = 1;
3504 p->minWriteFileFormat = 255;
3505 p->iStatement = 0;
3506 p->nFkConstraint = 0;
3507 #ifdef VDBE_PROFILE
3508 for(i=0; i<p->nOp; i++){
3509 p->aOp[i].cnt = 0;
3510 p->aOp[i].cycles = 0;
3511 }
3512 #endif
3513 }
3514
3515 /*
3516 ** Prepare a virtual machine for execution for the first time after
3517 ** creating the virtual machine. This involves things such
3518 ** as allocating registers and initializing the program counter.
3519 ** After the VDBE has be prepped, it can be executed by one or more
3520 ** calls to sqlite3VdbeExec().
3521 **
3522 ** This function may be called exactly once on each virtual machine.
3523 ** After this routine is called the VM has been "packaged" and is ready
3524 ** to run. After this routine is called, further calls to
3525 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
3526 ** the Vdbe from the Parse object that helped generate it so that the
3527 ** the Vdbe becomes an independent entity and the Parse object can be
3528 ** destroyed.
3529 **
3530 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
3531 ** to its initial state after it has been run.
3532 */
3533 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
3534 Vdbe *p, /* The VDBE */
3535 Parse *pParse /* Parsing context */
3536 ){
3537 sqlite3 *db; /* The database connection */
3538 int nVar; /* Number of parameters */
3539 int nMem; /* Number of VM memory registers */
3540 int nCursor; /* Number of cursors required */
3541 int nArg; /* Number of arguments in subprograms */
3542 int nOnce; /* Number of OP_Once instructions */
3543 int n; /* Loop counter */
3544 int nFree; /* Available free space */
3545 u8 *zCsr; /* Memory available for allocation */
3546 int nByte; /* How much extra memory is needed */
3547
3548 assert( p!=0 );
3549 assert( p->nOp>0 );
3550 assert( pParse!=0 );
3551 assert( p->magic==VDBE_MAGIC_INIT );
3552 assert( pParse==p->pParse );
3553 db = p->db;
3554 assert( db->mallocFailed==0 );
3555 nVar = pParse->nVar;
3556 nMem = pParse->nMem;
3557 nCursor = pParse->nTab;
3558 nArg = pParse->nMaxArg;
3559 nOnce = pParse->nOnce;
3560 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
3561
3562 /* For each cursor required, also allocate a memory cell. Memory
3563 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
3564 ** the vdbe program. Instead they are used to allocate space for
3565 ** VdbeCursor/BtCursor structures. The blob of memory associated with
3566 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
3567 ** stores the blob of memory associated with cursor 1, etc.
3568 **
3569 ** See also: allocateCursor().
3570 */
3571 nMem += nCursor;
3572
3573 /* zCsr will initially point to nFree bytes of unused space at the
3574 ** end of the opcode array, p->aOp. The computation of nFree is
3575 ** conservative - it might be smaller than the true number of free
3576 ** bytes, but never larger. nFree must be a multiple of 8 - it is
3577 ** rounded down if is not.
3578 */
3579 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode space used */
3580 zCsr = &((u8*)p->aOp)[n]; /* Unused opcode space */
3581 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
3582 nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused space */
3583 assert( nFree>=0 );
3584 if( nFree>0 ){
3585 memset(zCsr, 0, nFree);
3586 assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) );
3587 }
3588
3589 resolveP2Values(p, &nArg);
3590 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
3591 if( pParse->explain && nMem<10 ){
3592 nMem = 10;
3593 }
3594 p->expired = 0;
3595
3596 /* Memory for registers, parameters, cursor, etc, is allocated in two
3597 ** passes. On the first pass, we try to reuse unused space at the
3598 ** end of the opcode array. If we are unable to satisfy all memory
3599 ** requirements by reusing the opcode array tail, then the second
3600 ** pass will fill in the rest using a fresh allocation.
3601 **
3602 ** This two-pass approach that reuses as much memory as possible from
3603 ** the leftover space at the end of the opcode array can significantly
3604 ** reduce the amount of memory held by a prepared statement.
3605 */
3606 do {
3607 nByte = 0;
3608 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte);
3609 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte);
3610 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte);
3611 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), zCsr, &nFree, &nByte);
3612 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
3613 zCsr, &nFree, &nByte);
3614 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte);
3615 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3616 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte);
3617 #endif
3618 if( nByte ){
3619 p->pFree = sqlite3DbMallocZero(db, nByte);
3620 }
3621 zCsr = p->pFree;
3622 nFree = nByte;
3623 }while( nByte && !db->mallocFailed );
3624
3625 p->nCursor = nCursor;
3626 p->nOnceFlag = nOnce;
3627 if( p->aVar ){
3628 p->nVar = (ynVar)nVar;
3629 for(n=0; n<nVar; n++){
3630 p->aVar[n].flags = MEM_Null;
3631 p->aVar[n].db = db;
3632 }
3633 }
3634 if( p->azVar && pParse->nzVar>0 ){
3635 p->nzVar = pParse->nzVar;
3636 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
3637 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
3638 }
3639 if( p->aMem ){
3640 p->aMem--; /* aMem[] goes from 1..nMem */
3641 p->nMem = nMem; /* not from 0..nMem-1 */
3642 for(n=1; n<=nMem; n++){
3643 p->aMem[n].flags = MEM_Undefined;
3644 p->aMem[n].db = db;
3645 }
3646 }
3647 p->explain = pParse->explain;
3648 sqlite3VdbeRewind(p);
3649 }
3650
3651 /*
3652 ** Close a VDBE cursor and release all the resources that cursor
3653 ** happens to hold.
3654 */
3655 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
3656 if( pCx==0 ){
3657 return;
3658 }
3659 assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE );
3660 switch( pCx->eCurType ){
3661 case CURTYPE_SORTER: {
3662 sqlite3VdbeSorterClose(p->db, pCx);
3663 break;
3664 }
3665 case CURTYPE_BTREE: {
3666 if( pCx->pBt ){
3667 sqlite3BtreeClose(pCx->pBt);
3668 /* The pCx->pCursor will be close automatically, if it exists, by
3669 ** the call above. */
3670 }else{
3671 assert( pCx->uc.pCursor!=0 );
3672 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
3673 }
3674 break;
3675 }
3676 #ifndef SQLITE_OMIT_VIRTUALTABLE
3677 case CURTYPE_VTAB: {
3678 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
3679 const sqlite3_module *pModule = pVCur->pVtab->pModule;
3680 assert( pVCur->pVtab->nRef>0 );
3681 pVCur->pVtab->nRef--;
3682 pModule->xClose(pVCur);
3683 break;
3684 }
3685 #endif
3686 }
3687 }
3688
3689 /*
3690 ** Close all cursors in the current frame.
3691 */
3692 static void closeCursorsInFrame(Vdbe *p){
3693 if( p->apCsr ){
3694 int i;
3695 for(i=0; i<p->nCursor; i++){
3696 VdbeCursor *pC = p->apCsr[i];
3697 if( pC ){
3698 sqlite3VdbeFreeCursor(p, pC);
3699 p->apCsr[i] = 0;
3700 }
3701 }
3702 }
3703 }
3704
3705 /*
3706 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
3707 ** is used, for example, when a trigger sub-program is halted to restore
3708 ** control to the main program.
3709 */
3710 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
3711 Vdbe *v = pFrame->v;
3712 closeCursorsInFrame(v);
3713 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3714 v->anExec = pFrame->anExec;
3715 #endif
3716 v->aOnceFlag = pFrame->aOnceFlag;
3717 v->nOnceFlag = pFrame->nOnceFlag;
3718 v->aOp = pFrame->aOp;
3719 v->nOp = pFrame->nOp;
3720 v->aMem = pFrame->aMem;
3721 v->nMem = pFrame->nMem;
3722 v->apCsr = pFrame->apCsr;
3723 v->nCursor = pFrame->nCursor;
3724 v->db->lastRowid = pFrame->lastRowid;
3725 v->nChange = pFrame->nChange;
3726 v->db->nChange = pFrame->nDbChange;
3727 return pFrame->pc;
3728 }
3729
3730 /*
3731 ** Close all cursors.
3732 **
3733 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
3734 ** cell array. This is necessary as the memory cell array may contain
3735 ** pointers to VdbeFrame objects, which may in turn contain pointers to
3736 ** open cursors.
3737 */
3738 static void closeAllCursors(Vdbe *p){
3739 if( p->pFrame ){
3740 VdbeFrame *pFrame;
3741 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
3742 sqlite3VdbeFrameRestore(pFrame);
3743 p->pFrame = 0;
3744 p->nFrame = 0;
3745 }
3746 assert( p->nFrame==0 );
3747 closeCursorsInFrame(p);
3748 if( p->aMem ){
3749 releaseMemArray(&p->aMem[1], p->nMem);
3750 }
3751 while( p->pDelFrame ){
3752 VdbeFrame *pDel = p->pDelFrame;
3753 p->pDelFrame = pDel->pParent;
3754 sqlite3VdbeFrameDelete(pDel);
3755 }
3756
3757 /* Delete any auxdata allocations made by the VM */
3758 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
3759 assert( p->pAuxData==0 );
3760 }
3761
3762 /*
3763 ** Clean up the VM after a single run.
3764 */
3765 static void Cleanup(Vdbe *p){
3766 sqlite3 *db = p->db;
3767
3768 #ifdef SQLITE_DEBUG
3769 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
3770 ** Vdbe.aMem[] arrays have already been cleaned up. */
3771 int i;
3772 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
3773 if( p->aMem ){
3774 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
3775 }
3776 #endif
3777
3778 sqlite3DbFree(db, p->zErrMsg);
3779 p->zErrMsg = 0;
3780 p->pResultSet = 0;
3781 }
3782
3783 /*
3784 ** Set the number of result columns that will be returned by this SQL
3785 ** statement. This is now set at compile time, rather than during
3786 ** execution of the vdbe program so that sqlite3_column_count() can
3787 ** be called on an SQL statement before sqlite3_step().
3788 */
3789 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
3790 Mem *pColName;
3791 int n;
3792 sqlite3 *db = p->db;
3793
3794 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
3795 sqlite3DbFree(db, p->aColName);
3796 n = nResColumn*COLNAME_N;
3797 p->nResColumn = (u16)nResColumn;
3798 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
3799 if( p->aColName==0 ) return;
3800 while( n-- > 0 ){
3801 pColName->flags = MEM_Null;
3802 pColName->db = p->db;
3803 pColName++;
3804 }
3805 }
3806
3807 /*
3808 ** Set the name of the idx'th column to be returned by the SQL statement.
3809 ** zName must be a pointer to a nul terminated string.
3810 **
3811 ** This call must be made after a call to sqlite3VdbeSetNumCols().
3812 **
3813 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
3814 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
3815 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
3816 */
3817 SQLITE_PRIVATE int sqlite3VdbeSetColName(
3818 Vdbe *p, /* Vdbe being configured */
3819 int idx, /* Index of column zName applies to */
3820 int var, /* One of the COLNAME_* constants */
3821 const char *zName, /* Pointer to buffer containing name */
3822 void (*xDel)(void*) /* Memory management strategy for zName */
3823 ){
3824 int rc;
3825 Mem *pColName;
3826 assert( idx<p->nResColumn );
3827 assert( var<COLNAME_N );
3828 if( p->db->mallocFailed ){
3829 assert( !zName || xDel!=SQLITE_DYNAMIC );
3830 return SQLITE_NOMEM;
3831 }
3832 assert( p->aColName!=0 );
3833 pColName = &(p->aColName[idx+var*p->nResColumn]);
3834 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
3835 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
3836 return rc;
3837 }
3838
3839 /*
3840 ** A read or write transaction may or may not be active on database handle
3841 ** db. If a transaction is active, commit it. If there is a
3842 ** write-transaction spanning more than one database file, this routine
3843 ** takes care of the master journal trickery.
3844 */
3845 static int vdbeCommit(sqlite3 *db, Vdbe *p){
3846 int i;
3847 int nTrans = 0; /* Number of databases with an active write-transaction */
3848 int rc = SQLITE_OK;
3849 int needXcommit = 0;
3850
3851 #ifdef SQLITE_OMIT_VIRTUALTABLE
3852 /* With this option, sqlite3VtabSync() is defined to be simply
3853 ** SQLITE_OK so p is not used.
3854 */
3855 UNUSED_PARAMETER(p);
3856 #endif
3857
3858 /* Before doing anything else, call the xSync() callback for any
3859 ** virtual module tables written in this transaction. This has to
3860 ** be done before determining whether a master journal file is
3861 ** required, as an xSync() callback may add an attached database
3862 ** to the transaction.
3863 */
3864 rc = sqlite3VtabSync(db, p);
3865
3866 /* This loop determines (a) if the commit hook should be invoked and
3867 ** (b) how many database files have open write transactions, not
3868 ** including the temp database. (b) is important because if more than
3869 ** one database file has an open write transaction, a master journal
3870 ** file is required for an atomic commit.
3871 */
3872 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
3873 Btree *pBt = db->aDb[i].pBt;
3874 if( sqlite3BtreeIsInTrans(pBt) ){
3875 needXcommit = 1;
3876 if( i!=1 ) nTrans++;
3877 sqlite3BtreeEnter(pBt);
3878 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
3879 sqlite3BtreeLeave(pBt);
3880 }
3881 }
3882 if( rc!=SQLITE_OK ){
3883 return rc;
3884 }
3885
3886 /* If there are any write-transactions at all, invoke the commit hook */
3887 if( needXcommit && db->xCommitCallback ){
3888 rc = db->xCommitCallback(db->pCommitArg);
3889 if( rc ){
3890 return SQLITE_CONSTRAINT_COMMITHOOK;
3891 }
3892 }
3893
3894 /* The simple case - no more than one database file (not counting the
3895 ** TEMP database) has a transaction active. There is no need for the
3896 ** master-journal.
3897 **
3898 ** If the return value of sqlite3BtreeGetFilename() is a zero length
3899 ** string, it means the main database is :memory: or a temp file. In
3900 ** that case we do not support atomic multi-file commits, so use the
3901 ** simple case then too.
3902 */
3903 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
3904 || nTrans<=1
3905 ){
3906 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
3907 Btree *pBt = db->aDb[i].pBt;
3908 if( pBt ){
3909 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
3910 }
3911 }
3912
3913 /* Do the commit only if all databases successfully complete phase 1.
3914 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
3915 ** IO error while deleting or truncating a journal file. It is unlikely,
3916 ** but could happen. In this case abandon processing and return the error.
3917 */
3918 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
3919 Btree *pBt = db->aDb[i].pBt;
3920 if( pBt ){
3921 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
3922 }
3923 }
3924 if( rc==SQLITE_OK ){
3925 sqlite3VtabCommit(db);
3926 }
3927 }
3928
3929 /* The complex case - There is a multi-file write-transaction active.
3930 ** This requires a master journal file to ensure the transaction is
3931 ** committed atomically.
3932 */
3933 #ifndef SQLITE_OMIT_DISKIO
3934 else{
3935 sqlite3_vfs *pVfs = db->pVfs;
3936 int needSync = 0;
3937 char *zMaster = 0; /* File-name for the master journal */
3938 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
3939 sqlite3_file *pMaster = 0;
3940 i64 offset = 0;
3941 int res;
3942 int retryCount = 0;
3943 int nMainFile;
3944
3945 /* Select a master journal file name */
3946 nMainFile = sqlite3Strlen30(zMainFile);
3947 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
3948 if( zMaster==0 ) return SQLITE_NOMEM;
3949 do {
3950 u32 iRandom;
3951 if( retryCount ){
3952 if( retryCount>100 ){
3953 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
3954 sqlite3OsDelete(pVfs, zMaster, 0);
3955 break;
3956 }else if( retryCount==1 ){
3957 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
3958 }
3959 }
3960 retryCount++;
3961 sqlite3_randomness(sizeof(iRandom), &iRandom);
3962 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
3963 (iRandom>>8)&0xffffff, iRandom&0xff);
3964 /* The antipenultimate character of the master journal name must
3965 ** be "9" to avoid name collisions when using 8+3 filenames. */
3966 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
3967 sqlite3FileSuffix3(zMainFile, zMaster);
3968 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
3969 }while( rc==SQLITE_OK && res );
3970 if( rc==SQLITE_OK ){
3971 /* Open the master journal. */
3972 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
3973 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
3974 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
3975 );
3976 }
3977 if( rc!=SQLITE_OK ){
3978 sqlite3DbFree(db, zMaster);
3979 return rc;
3980 }
3981
3982 /* Write the name of each database file in the transaction into the new
3983 ** master journal file. If an error occurs at this point close
3984 ** and delete the master journal file. All the individual journal files
3985 ** still have 'null' as the master journal pointer, so they will roll
3986 ** back independently if a failure occurs.
3987 */
3988 for(i=0; i<db->nDb; i++){
3989 Btree *pBt = db->aDb[i].pBt;
3990 if( sqlite3BtreeIsInTrans(pBt) ){
3991 char const *zFile = sqlite3BtreeGetJournalname(pBt);
3992 if( zFile==0 ){
3993 continue; /* Ignore TEMP and :memory: databases */
3994 }
3995 assert( zFile[0]!=0 );
3996 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
3997 needSync = 1;
3998 }
3999 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
4000 offset += sqlite3Strlen30(zFile)+1;
4001 if( rc!=SQLITE_OK ){
4002 sqlite3OsCloseFree(pMaster);
4003 sqlite3OsDelete(pVfs, zMaster, 0);
4004 sqlite3DbFree(db, zMaster);
4005 return rc;
4006 }
4007 }
4008 }
4009
4010 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
4011 ** flag is set this is not required.
4012 */
4013 if( needSync
4014 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
4015 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
4016 ){
4017 sqlite3OsCloseFree(pMaster);
4018 sqlite3OsDelete(pVfs, zMaster, 0);
4019 sqlite3DbFree(db, zMaster);
4020 return rc;
4021 }
4022
4023 /* Sync all the db files involved in the transaction. The same call
4024 ** sets the master journal pointer in each individual journal. If
4025 ** an error occurs here, do not delete the master journal file.
4026 **
4027 ** If the error occurs during the first call to
4028 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
4029 ** master journal file will be orphaned. But we cannot delete it,
4030 ** in case the master journal file name was written into the journal
4031 ** file before the failure occurred.
4032 */
4033 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
4034 Btree *pBt = db->aDb[i].pBt;
4035 if( pBt ){
4036 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
4037 }
4038 }
4039 sqlite3OsCloseFree(pMaster);
4040 assert( rc!=SQLITE_BUSY );
4041 if( rc!=SQLITE_OK ){
4042 sqlite3DbFree(db, zMaster);
4043 return rc;
4044 }
4045
4046 /* Delete the master journal file. This commits the transaction. After
4047 ** doing this the directory is synced again before any individual
4048 ** transaction files are deleted.
4049 */
4050 rc = sqlite3OsDelete(pVfs, zMaster, needSync);
4051 sqlite3DbFree(db, zMaster);
4052 zMaster = 0;
4053 if( rc ){
4054 return rc;
4055 }
4056
4057 /* All files and directories have already been synced, so the following
4058 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
4059 ** deleting or truncating journals. If something goes wrong while
4060 ** this is happening we don't really care. The integrity of the
4061 ** transaction is already guaranteed, but some stray 'cold' journals
4062 ** may be lying around. Returning an error code won't help matters.
4063 */
4064 disable_simulated_io_errors();
4065 sqlite3BeginBenignMalloc();
4066 for(i=0; i<db->nDb; i++){
4067 Btree *pBt = db->aDb[i].pBt;
4068 if( pBt ){
4069 sqlite3BtreeCommitPhaseTwo(pBt, 1);
4070 }
4071 }
4072 sqlite3EndBenignMalloc();
4073 enable_simulated_io_errors();
4074
4075 sqlite3VtabCommit(db);
4076 }
4077 #endif
4078
4079 return rc;
4080 }
4081
4082 /*
4083 ** This routine checks that the sqlite3.nVdbeActive count variable
4084 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
4085 ** currently active. An assertion fails if the two counts do not match.
4086 ** This is an internal self-check only - it is not an essential processing
4087 ** step.
4088 **
4089 ** This is a no-op if NDEBUG is defined.
4090 */
4091 #ifndef NDEBUG
4092 static void checkActiveVdbeCnt(sqlite3 *db){
4093 Vdbe *p;
4094 int cnt = 0;
4095 int nWrite = 0;
4096 int nRead = 0;
4097 p = db->pVdbe;
4098 while( p ){
4099 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
4100 cnt++;
4101 if( p->readOnly==0 ) nWrite++;
4102 if( p->bIsReader ) nRead++;
4103 }
4104 p = p->pNext;
4105 }
4106 assert( cnt==db->nVdbeActive );
4107 assert( nWrite==db->nVdbeWrite );
4108 assert( nRead==db->nVdbeRead );
4109 }
4110 #else
4111 #define checkActiveVdbeCnt(x)
4112 #endif
4113
4114 /*
4115 ** If the Vdbe passed as the first argument opened a statement-transaction,
4116 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
4117 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
4118 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
4119 ** statement transaction is committed.
4120 **
4121 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
4122 ** Otherwise SQLITE_OK.
4123 */
4124 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
4125 sqlite3 *const db = p->db;
4126 int rc = SQLITE_OK;
4127
4128 /* If p->iStatement is greater than zero, then this Vdbe opened a
4129 ** statement transaction that should be closed here. The only exception
4130 ** is that an IO error may have occurred, causing an emergency rollback.
4131 ** In this case (db->nStatement==0), and there is nothing to do.
4132 */
4133 if( db->nStatement && p->iStatement ){
4134 int i;
4135 const int iSavepoint = p->iStatement-1;
4136
4137 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
4138 assert( db->nStatement>0 );
4139 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
4140
4141 for(i=0; i<db->nDb; i++){
4142 int rc2 = SQLITE_OK;
4143 Btree *pBt = db->aDb[i].pBt;
4144 if( pBt ){
4145 if( eOp==SAVEPOINT_ROLLBACK ){
4146 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
4147 }
4148 if( rc2==SQLITE_OK ){
4149 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
4150 }
4151 if( rc==SQLITE_OK ){
4152 rc = rc2;
4153 }
4154 }
4155 }
4156 db->nStatement--;
4157 p->iStatement = 0;
4158
4159 if( rc==SQLITE_OK ){
4160 if( eOp==SAVEPOINT_ROLLBACK ){
4161 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
4162 }
4163 if( rc==SQLITE_OK ){
4164 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
4165 }
4166 }
4167
4168 /* If the statement transaction is being rolled back, also restore the
4169 ** database handles deferred constraint counter to the value it had when
4170 ** the statement transaction was opened. */
4171 if( eOp==SAVEPOINT_ROLLBACK ){
4172 db->nDeferredCons = p->nStmtDefCons;
4173 db->nDeferredImmCons = p->nStmtDefImmCons;
4174 }
4175 }
4176 return rc;
4177 }
4178
4179 /*
4180 ** This function is called when a transaction opened by the database
4181 ** handle associated with the VM passed as an argument is about to be
4182 ** committed. If there are outstanding deferred foreign key constraint
4183 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
4184 **
4185 ** If there are outstanding FK violations and this function returns
4186 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
4187 ** and write an error message to it. Then return SQLITE_ERROR.
4188 */
4189 #ifndef SQLITE_OMIT_FOREIGN_KEY
4190 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
4191 sqlite3 *db = p->db;
4192 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
4193 || (!deferred && p->nFkConstraint>0)
4194 ){
4195 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
4196 p->errorAction = OE_Abort;
4197 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
4198 return SQLITE_ERROR;
4199 }
4200 return SQLITE_OK;
4201 }
4202 #endif
4203
4204 /*
4205 ** This routine is called the when a VDBE tries to halt. If the VDBE
4206 ** has made changes and is in autocommit mode, then commit those
4207 ** changes. If a rollback is needed, then do the rollback.
4208 **
4209 ** This routine is the only way to move the state of a VM from
4210 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
4211 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
4212 **
4213 ** Return an error code. If the commit could not complete because of
4214 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
4215 ** means the close did not happen and needs to be repeated.
4216 */
4217 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
4218 int rc; /* Used to store transient return codes */
4219 sqlite3 *db = p->db;
4220
4221 /* This function contains the logic that determines if a statement or
4222 ** transaction will be committed or rolled back as a result of the
4223 ** execution of this virtual machine.
4224 **
4225 ** If any of the following errors occur:
4226 **
4227 ** SQLITE_NOMEM
4228 ** SQLITE_IOERR
4229 ** SQLITE_FULL
4230 ** SQLITE_INTERRUPT
4231 **
4232 ** Then the internal cache might have been left in an inconsistent
4233 ** state. We need to rollback the statement transaction, if there is
4234 ** one, or the complete transaction if there is no statement transaction.
4235 */
4236
4237 if( p->db->mallocFailed ){
4238 p->rc = SQLITE_NOMEM;
4239 }
4240 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
4241 closeAllCursors(p);
4242 if( p->magic!=VDBE_MAGIC_RUN ){
4243 return SQLITE_OK;
4244 }
4245 checkActiveVdbeCnt(db);
4246
4247 /* No commit or rollback needed if the program never started or if the
4248 ** SQL statement does not read or write a database file. */
4249 if( p->pc>=0 && p->bIsReader ){
4250 int mrc; /* Primary error code from p->rc */
4251 int eStatementOp = 0;
4252 int isSpecialError; /* Set to true if a 'special' error */
4253
4254 /* Lock all btrees used by the statement */
4255 sqlite3VdbeEnter(p);
4256
4257 /* Check for one of the special errors */
4258 mrc = p->rc & 0xff;
4259 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
4260 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
4261 if( isSpecialError ){
4262 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
4263 ** no rollback is necessary. Otherwise, at least a savepoint
4264 ** transaction must be rolled back to restore the database to a
4265 ** consistent state.
4266 **
4267 ** Even if the statement is read-only, it is important to perform
4268 ** a statement or transaction rollback operation. If the error
4269 ** occurred while writing to the journal, sub-journal or database
4270 ** file as part of an effort to free up cache space (see function
4271 ** pagerStress() in pager.c), the rollback is required to restore
4272 ** the pager to a consistent state.
4273 */
4274 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
4275 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
4276 eStatementOp = SAVEPOINT_ROLLBACK;
4277 }else{
4278 /* We are forced to roll back the active transaction. Before doing
4279 ** so, abort any other statements this handle currently has active.
4280 */
4281 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
4282 sqlite3CloseSavepoints(db);
4283 db->autoCommit = 1;
4284 p->nChange = 0;
4285 }
4286 }
4287 }
4288
4289 /* Check for immediate foreign key violations. */
4290 if( p->rc==SQLITE_OK ){
4291 sqlite3VdbeCheckFk(p, 0);
4292 }
4293
4294 /* If the auto-commit flag is set and this is the only active writer
4295 ** VM, then we do either a commit or rollback of the current transaction.
4296 **
4297 ** Note: This block also runs if one of the special errors handled
4298 ** above has occurred.
4299 */
4300 if( !sqlite3VtabInSync(db)
4301 && db->autoCommit
4302 && db->nVdbeWrite==(p->readOnly==0)
4303 ){
4304 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
4305 rc = sqlite3VdbeCheckFk(p, 1);
4306 if( rc!=SQLITE_OK ){
4307 if( NEVER(p->readOnly) ){
4308 sqlite3VdbeLeave(p);
4309 return SQLITE_ERROR;
4310 }
4311 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
4312 }else{
4313 /* The auto-commit flag is true, the vdbe program was successful
4314 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
4315 ** key constraints to hold up the transaction. This means a commit
4316 ** is required. */
4317 rc = vdbeCommit(db, p);
4318 }
4319 if( rc==SQLITE_BUSY && p->readOnly ){
4320 sqlite3VdbeLeave(p);
4321 return SQLITE_BUSY;
4322 }else if( rc!=SQLITE_OK ){
4323 p->rc = rc;
4324 sqlite3RollbackAll(db, SQLITE_OK);
4325 p->nChange = 0;
4326 }else{
4327 db->nDeferredCons = 0;
4328 db->nDeferredImmCons = 0;
4329 db->flags &= ~SQLITE_DeferFKs;
4330 sqlite3CommitInternalChanges(db);
4331 }
4332 }else{
4333 sqlite3RollbackAll(db, SQLITE_OK);
4334 p->nChange = 0;
4335 }
4336 db->nStatement = 0;
4337 }else if( eStatementOp==0 ){
4338 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
4339 eStatementOp = SAVEPOINT_RELEASE;
4340 }else if( p->errorAction==OE_Abort ){
4341 eStatementOp = SAVEPOINT_ROLLBACK;
4342 }else{
4343 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
4344 sqlite3CloseSavepoints(db);
4345 db->autoCommit = 1;
4346 p->nChange = 0;
4347 }
4348 }
4349
4350 /* If eStatementOp is non-zero, then a statement transaction needs to
4351 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
4352 ** do so. If this operation returns an error, and the current statement
4353 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
4354 ** current statement error code.
4355 */
4356 if( eStatementOp ){
4357 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
4358 if( rc ){
4359 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
4360 p->rc = rc;
4361 sqlite3DbFree(db, p->zErrMsg);
4362 p->zErrMsg = 0;
4363 }
4364 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
4365 sqlite3CloseSavepoints(db);
4366 db->autoCommit = 1;
4367 p->nChange = 0;
4368 }
4369 }
4370
4371 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
4372 ** has been rolled back, update the database connection change-counter.
4373 */
4374 if( p->changeCntOn ){
4375 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
4376 sqlite3VdbeSetChanges(db, p->nChange);
4377 }else{
4378 sqlite3VdbeSetChanges(db, 0);
4379 }
4380 p->nChange = 0;
4381 }
4382
4383 /* Release the locks */
4384 sqlite3VdbeLeave(p);
4385 }
4386
4387 /* We have successfully halted and closed the VM. Record this fact. */
4388 if( p->pc>=0 ){
4389 db->nVdbeActive--;
4390 if( !p->readOnly ) db->nVdbeWrite--;
4391 if( p->bIsReader ) db->nVdbeRead--;
4392 assert( db->nVdbeActive>=db->nVdbeRead );
4393 assert( db->nVdbeRead>=db->nVdbeWrite );
4394 assert( db->nVdbeWrite>=0 );
4395 }
4396 p->magic = VDBE_MAGIC_HALT;
4397 checkActiveVdbeCnt(db);
4398 if( p->db->mallocFailed ){
4399 p->rc = SQLITE_NOMEM;
4400 }
4401
4402 /* If the auto-commit flag is set to true, then any locks that were held
4403 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
4404 ** to invoke any required unlock-notify callbacks.
4405 */
4406 if( db->autoCommit ){
4407 sqlite3ConnectionUnlocked(db);
4408 }
4409
4410 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
4411 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
4412 }
4413
4414
4415 /*
4416 ** Each VDBE holds the result of the most recent sqlite3_step() call
4417 ** in p->rc. This routine sets that result back to SQLITE_OK.
4418 */
4419 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
4420 p->rc = SQLITE_OK;
4421 }
4422
4423 /*
4424 ** Copy the error code and error message belonging to the VDBE passed
4425 ** as the first argument to its database handle (so that they will be
4426 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
4427 **
4428 ** This function does not clear the VDBE error code or message, just
4429 ** copies them to the database handle.
4430 */
4431 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
4432 sqlite3 *db = p->db;
4433 int rc = p->rc;
4434 if( p->zErrMsg ){
4435 u8 mallocFailed = db->mallocFailed;
4436 sqlite3BeginBenignMalloc();
4437 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
4438 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
4439 sqlite3EndBenignMalloc();
4440 db->mallocFailed = mallocFailed;
4441 db->errCode = rc;
4442 }else{
4443 sqlite3Error(db, rc);
4444 }
4445 return rc;
4446 }
4447
4448 #ifdef SQLITE_ENABLE_SQLLOG
4449 /*
4450 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
4451 ** invoke it.
4452 */
4453 static void vdbeInvokeSqllog(Vdbe *v){
4454 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
4455 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
4456 assert( v->db->init.busy==0 );
4457 if( zExpanded ){
4458 sqlite3GlobalConfig.xSqllog(
4459 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
4460 );
4461 sqlite3DbFree(v->db, zExpanded);
4462 }
4463 }
4464 }
4465 #else
4466 # define vdbeInvokeSqllog(x)
4467 #endif
4468
4469 /*
4470 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
4471 ** Write any error messages into *pzErrMsg. Return the result code.
4472 **
4473 ** After this routine is run, the VDBE should be ready to be executed
4474 ** again.
4475 **
4476 ** To look at it another way, this routine resets the state of the
4477 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
4478 ** VDBE_MAGIC_INIT.
4479 */
4480 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
4481 sqlite3 *db;
4482 db = p->db;
4483
4484 /* If the VM did not run to completion or if it encountered an
4485 ** error, then it might not have been halted properly. So halt
4486 ** it now.
4487 */
4488 sqlite3VdbeHalt(p);
4489
4490 /* If the VDBE has be run even partially, then transfer the error code
4491 ** and error message from the VDBE into the main database structure. But
4492 ** if the VDBE has just been set to run but has not actually executed any
4493 ** instructions yet, leave the main database error information unchanged.
4494 */
4495 if( p->pc>=0 ){
4496 vdbeInvokeSqllog(p);
4497 sqlite3VdbeTransferError(p);
4498 sqlite3DbFree(db, p->zErrMsg);
4499 p->zErrMsg = 0;
4500 if( p->runOnlyOnce ) p->expired = 1;
4501 }else if( p->rc && p->expired ){
4502 /* The expired flag was set on the VDBE before the first call
4503 ** to sqlite3_step(). For consistency (since sqlite3_step() was
4504 ** called), set the database error in this case as well.
4505 */
4506 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
4507 sqlite3DbFree(db, p->zErrMsg);
4508 p->zErrMsg = 0;
4509 }
4510
4511 /* Reclaim all memory used by the VDBE
4512 */
4513 Cleanup(p);
4514
4515 /* Save profiling information from this VDBE run.
4516 */
4517 #ifdef VDBE_PROFILE
4518 {
4519 FILE *out = fopen("vdbe_profile.out", "a");
4520 if( out ){
4521 int i;
4522 fprintf(out, "---- ");
4523 for(i=0; i<p->nOp; i++){
4524 fprintf(out, "%02x", p->aOp[i].opcode);
4525 }
4526 fprintf(out, "\n");
4527 if( p->zSql ){
4528 char c, pc = 0;
4529 fprintf(out, "-- ");
4530 for(i=0; (c = p->zSql[i])!=0; i++){
4531 if( pc=='\n' ) fprintf(out, "-- ");
4532 putc(c, out);
4533 pc = c;
4534 }
4535 if( pc!='\n' ) fprintf(out, "\n");
4536 }
4537 for(i=0; i<p->nOp; i++){
4538 char zHdr[100];
4539 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
4540 p->aOp[i].cnt,
4541 p->aOp[i].cycles,
4542 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
4543 );
4544 fprintf(out, "%s", zHdr);
4545 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
4546 }
4547 fclose(out);
4548 }
4549 }
4550 #endif
4551 p->iCurrentTime = 0;
4552 p->magic = VDBE_MAGIC_INIT;
4553 return p->rc & db->errMask;
4554 }
4555
4556 /*
4557 ** Clean up and delete a VDBE after execution. Return an integer which is
4558 ** the result code. Write any error message text into *pzErrMsg.
4559 */
4560 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
4561 int rc = SQLITE_OK;
4562 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
4563 rc = sqlite3VdbeReset(p);
4564 assert( (rc & p->db->errMask)==rc );
4565 }
4566 sqlite3VdbeDelete(p);
4567 return rc;
4568 }
4569
4570 /*
4571 ** If parameter iOp is less than zero, then invoke the destructor for
4572 ** all auxiliary data pointers currently cached by the VM passed as
4573 ** the first argument.
4574 **
4575 ** Or, if iOp is greater than or equal to zero, then the destructor is
4576 ** only invoked for those auxiliary data pointers created by the user
4577 ** function invoked by the OP_Function opcode at instruction iOp of
4578 ** VM pVdbe, and only then if:
4579 **
4580 ** * the associated function parameter is the 32nd or later (counting
4581 ** from left to right), or
4582 **
4583 ** * the corresponding bit in argument mask is clear (where the first
4584 ** function parameter corresponds to bit 0 etc.).
4585 */
4586 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
4587 AuxData **pp = &pVdbe->pAuxData;
4588 while( *pp ){
4589 AuxData *pAux = *pp;
4590 if( (iOp<0)
4591 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
4592 ){
4593 testcase( pAux->iArg==31 );
4594 if( pAux->xDelete ){
4595 pAux->xDelete(pAux->pAux);
4596 }
4597 *pp = pAux->pNext;
4598 sqlite3DbFree(pVdbe->db, pAux);
4599 }else{
4600 pp= &pAux->pNext;
4601 }
4602 }
4603 }
4604
4605 /*
4606 ** Free all memory associated with the Vdbe passed as the second argument,
4607 ** except for object itself, which is preserved.
4608 **
4609 ** The difference between this function and sqlite3VdbeDelete() is that
4610 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
4611 ** the database connection and frees the object itself.
4612 */
4613 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
4614 SubProgram *pSub, *pNext;
4615 int i;
4616 assert( p->db==0 || p->db==db );
4617 releaseMemArray(p->aVar, p->nVar);
4618 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
4619 for(pSub=p->pProgram; pSub; pSub=pNext){
4620 pNext = pSub->pNext;
4621 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
4622 sqlite3DbFree(db, pSub);
4623 }
4624 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
4625 vdbeFreeOpArray(db, p->aOp, p->nOp);
4626 sqlite3DbFree(db, p->aColName);
4627 sqlite3DbFree(db, p->zSql);
4628 sqlite3DbFree(db, p->pFree);
4629 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
4630 for(i=0; i<p->nScan; i++){
4631 sqlite3DbFree(db, p->aScan[i].zName);
4632 }
4633 sqlite3DbFree(db, p->aScan);
4634 #endif
4635 }
4636
4637 /*
4638 ** Delete an entire VDBE.
4639 */
4640 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
4641 sqlite3 *db;
4642
4643 if( NEVER(p==0) ) return;
4644 db = p->db;
4645 assert( sqlite3_mutex_held(db->mutex) );
4646 sqlite3VdbeClearObject(db, p);
4647 if( p->pPrev ){
4648 p->pPrev->pNext = p->pNext;
4649 }else{
4650 assert( db->pVdbe==p );
4651 db->pVdbe = p->pNext;
4652 }
4653 if( p->pNext ){
4654 p->pNext->pPrev = p->pPrev;
4655 }
4656 p->magic = VDBE_MAGIC_DEAD;
4657 p->db = 0;
4658 sqlite3DbFree(db, p);
4659 }
4660
4661 /*
4662 ** The cursor "p" has a pending seek operation that has not yet been
4663 ** carried out. Seek the cursor now. If an error occurs, return
4664 ** the appropriate error code.
4665 */
4666 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
4667 int res, rc;
4668 #ifdef SQLITE_TEST
4669 extern int sqlite3_search_count;
4670 #endif
4671 assert( p->deferredMoveto );
4672 assert( p->isTable );
4673 assert( p->eCurType==CURTYPE_BTREE );
4674 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
4675 if( rc ) return rc;
4676 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
4677 #ifdef SQLITE_TEST
4678 sqlite3_search_count++;
4679 #endif
4680 p->deferredMoveto = 0;
4681 p->cacheStatus = CACHE_STALE;
4682 return SQLITE_OK;
4683 }
4684
4685 /*
4686 ** Something has moved cursor "p" out of place. Maybe the row it was
4687 ** pointed to was deleted out from under it. Or maybe the btree was
4688 ** rebalanced. Whatever the cause, try to restore "p" to the place it
4689 ** is supposed to be pointing. If the row was deleted out from under the
4690 ** cursor, set the cursor to point to a NULL row.
4691 */
4692 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
4693 int isDifferentRow, rc;
4694 assert( p->eCurType==CURTYPE_BTREE );
4695 assert( p->uc.pCursor!=0 );
4696 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
4697 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
4698 p->cacheStatus = CACHE_STALE;
4699 if( isDifferentRow ) p->nullRow = 1;
4700 return rc;
4701 }
4702
4703 /*
4704 ** Check to ensure that the cursor is valid. Restore the cursor
4705 ** if need be. Return any I/O error from the restore operation.
4706 */
4707 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
4708 assert( p->eCurType==CURTYPE_BTREE );
4709 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
4710 return handleMovedCursor(p);
4711 }
4712 return SQLITE_OK;
4713 }
4714
4715 /*
4716 ** Make sure the cursor p is ready to read or write the row to which it
4717 ** was last positioned. Return an error code if an OOM fault or I/O error
4718 ** prevents us from positioning the cursor to its correct position.
4719 **
4720 ** If a MoveTo operation is pending on the given cursor, then do that
4721 ** MoveTo now. If no move is pending, check to see if the row has been
4722 ** deleted out from under the cursor and if it has, mark the row as
4723 ** a NULL row.
4724 **
4725 ** If the cursor is already pointing to the correct row and that row has
4726 ** not been deleted out from under the cursor, then this routine is a no-op.
4727 */
4728 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
4729 if( p->eCurType==CURTYPE_BTREE ){
4730 if( p->deferredMoveto ){
4731 return handleDeferredMoveto(p);
4732 }
4733 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
4734 return handleMovedCursor(p);
4735 }
4736 }
4737 return SQLITE_OK;
4738 }
4739
4740 /*
4741 ** The following functions:
4742 **
4743 ** sqlite3VdbeSerialType()
4744 ** sqlite3VdbeSerialTypeLen()
4745 ** sqlite3VdbeSerialLen()
4746 ** sqlite3VdbeSerialPut()
4747 ** sqlite3VdbeSerialGet()
4748 **
4749 ** encapsulate the code that serializes values for storage in SQLite
4750 ** data and index records. Each serialized value consists of a
4751 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
4752 ** integer, stored as a varint.
4753 **
4754 ** In an SQLite index record, the serial type is stored directly before
4755 ** the blob of data that it corresponds to. In a table record, all serial
4756 ** types are stored at the start of the record, and the blobs of data at
4757 ** the end. Hence these functions allow the caller to handle the
4758 ** serial-type and data blob separately.
4759 **
4760 ** The following table describes the various storage classes for data:
4761 **
4762 ** serial type bytes of data type
4763 ** -------------- --------------- ---------------
4764 ** 0 0 NULL
4765 ** 1 1 signed integer
4766 ** 2 2 signed integer
4767 ** 3 3 signed integer
4768 ** 4 4 signed integer
4769 ** 5 6 signed integer
4770 ** 6 8 signed integer
4771 ** 7 8 IEEE float
4772 ** 8 0 Integer constant 0
4773 ** 9 0 Integer constant 1
4774 ** 10,11 reserved for expansion
4775 ** N>=12 and even (N-12)/2 BLOB
4776 ** N>=13 and odd (N-13)/2 text
4777 **
4778 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
4779 ** of SQLite will not understand those serial types.
4780 */
4781
4782 /*
4783 ** Return the serial-type for the value stored in pMem.
4784 */
4785 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
4786 int flags = pMem->flags;
4787 u32 n;
4788
4789 assert( pLen!=0 );
4790 if( flags&MEM_Null ){
4791 *pLen = 0;
4792 return 0;
4793 }
4794 if( flags&MEM_Int ){
4795 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
4796 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
4797 i64 i = pMem->u.i;
4798 u64 u;
4799 if( i<0 ){
4800 u = ~i;
4801 }else{
4802 u = i;
4803 }
4804 if( u<=127 ){
4805 if( (i&1)==i && file_format>=4 ){
4806 *pLen = 0;
4807 return 8+(u32)u;
4808 }else{
4809 *pLen = 1;
4810 return 1;
4811 }
4812 }
4813 if( u<=32767 ){ *pLen = 2; return 2; }
4814 if( u<=8388607 ){ *pLen = 3; return 3; }
4815 if( u<=2147483647 ){ *pLen = 4; return 4; }
4816 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
4817 *pLen = 8;
4818 return 6;
4819 }
4820 if( flags&MEM_Real ){
4821 *pLen = 8;
4822 return 7;
4823 }
4824 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
4825 assert( pMem->n>=0 );
4826 n = (u32)pMem->n;
4827 if( flags & MEM_Zero ){
4828 n += pMem->u.nZero;
4829 }
4830 *pLen = n;
4831 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
4832 }
4833
4834 /*
4835 ** The sizes for serial types less than 128
4836 */
4837 static const u8 sqlite3SmallTypeSizes[] = {
4838 /* 0 1 2 3 4 5 6 7 8 9 */
4839 /* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
4840 /* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4841 /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
4842 /* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
4843 /* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
4844 /* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
4845 /* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
4846 /* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
4847 /* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
4848 /* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
4849 /* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
4850 /* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
4851 /* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
4852 };
4853
4854 /*
4855 ** Return the length of the data corresponding to the supplied serial-type.
4856 */
4857 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
4858 if( serial_type>=128 ){
4859 return (serial_type-12)/2;
4860 }else{
4861 assert( serial_type<12
4862 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
4863 return sqlite3SmallTypeSizes[serial_type];
4864 }
4865 }
4866 SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
4867 assert( serial_type<128 );
4868 return sqlite3SmallTypeSizes[serial_type];
4869 }
4870
4871 /*
4872 ** If we are on an architecture with mixed-endian floating
4873 ** points (ex: ARM7) then swap the lower 4 bytes with the
4874 ** upper 4 bytes. Return the result.
4875 **
4876 ** For most architectures, this is a no-op.
4877 **
4878 ** (later): It is reported to me that the mixed-endian problem
4879 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
4880 ** that early versions of GCC stored the two words of a 64-bit
4881 ** float in the wrong order. And that error has been propagated
4882 ** ever since. The blame is not necessarily with GCC, though.
4883 ** GCC might have just copying the problem from a prior compiler.
4884 ** I am also told that newer versions of GCC that follow a different
4885 ** ABI get the byte order right.
4886 **
4887 ** Developers using SQLite on an ARM7 should compile and run their
4888 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
4889 ** enabled, some asserts below will ensure that the byte order of
4890 ** floating point values is correct.
4891 **
4892 ** (2007-08-30) Frank van Vugt has studied this problem closely
4893 ** and has send his findings to the SQLite developers. Frank
4894 ** writes that some Linux kernels offer floating point hardware
4895 ** emulation that uses only 32-bit mantissas instead of a full
4896 ** 48-bits as required by the IEEE standard. (This is the
4897 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
4898 ** byte swapping becomes very complicated. To avoid problems,
4899 ** the necessary byte swapping is carried out using a 64-bit integer
4900 ** rather than a 64-bit float. Frank assures us that the code here
4901 ** works for him. We, the developers, have no way to independently
4902 ** verify this, but Frank seems to know what he is talking about
4903 ** so we trust him.
4904 */
4905 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
4906 static u64 floatSwap(u64 in){
4907 union {
4908 u64 r;
4909 u32 i[2];
4910 } u;
4911 u32 t;
4912
4913 u.r = in;
4914 t = u.i[0];
4915 u.i[0] = u.i[1];
4916 u.i[1] = t;
4917 return u.r;
4918 }
4919 # define swapMixedEndianFloat(X) X = floatSwap(X)
4920 #else
4921 # define swapMixedEndianFloat(X)
4922 #endif
4923
4924 /*
4925 ** Write the serialized data blob for the value stored in pMem into
4926 ** buf. It is assumed that the caller has allocated sufficient space.
4927 ** Return the number of bytes written.
4928 **
4929 ** nBuf is the amount of space left in buf[]. The caller is responsible
4930 ** for allocating enough space to buf[] to hold the entire field, exclusive
4931 ** of the pMem->u.nZero bytes for a MEM_Zero value.
4932 **
4933 ** Return the number of bytes actually written into buf[]. The number
4934 ** of bytes in the zero-filled tail is included in the return value only
4935 ** if those bytes were zeroed in buf[].
4936 */
4937 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
4938 u32 len;
4939
4940 /* Integer and Real */
4941 if( serial_type<=7 && serial_type>0 ){
4942 u64 v;
4943 u32 i;
4944 if( serial_type==7 ){
4945 assert( sizeof(v)==sizeof(pMem->u.r) );
4946 memcpy(&v, &pMem->u.r, sizeof(v));
4947 swapMixedEndianFloat(v);
4948 }else{
4949 v = pMem->u.i;
4950 }
4951 len = i = sqlite3SmallTypeSizes[serial_type];
4952 assert( i>0 );
4953 do{
4954 buf[--i] = (u8)(v&0xFF);
4955 v >>= 8;
4956 }while( i );
4957 return len;
4958 }
4959
4960 /* String or blob */
4961 if( serial_type>=12 ){
4962 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
4963 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
4964 len = pMem->n;
4965 if( len>0 ) memcpy(buf, pMem->z, len);
4966 return len;
4967 }
4968
4969 /* NULL or constants 0 or 1 */
4970 return 0;
4971 }
4972
4973 /* Input "x" is a sequence of unsigned characters that represent a
4974 ** big-endian integer. Return the equivalent native integer
4975 */
4976 #define ONE_BYTE_INT(x) ((i8)(x)[0])
4977 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
4978 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
4979 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
4980 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
4981
4982 /*
4983 ** Deserialize the data blob pointed to by buf as serial type serial_type
4984 ** and store the result in pMem. Return the number of bytes read.
4985 **
4986 ** This function is implemented as two separate routines for performance.
4987 ** The few cases that require local variables are broken out into a separate
4988 ** routine so that in most cases the overhead of moving the stack pointer
4989 ** is avoided.
4990 */
4991 static u32 SQLITE_NOINLINE serialGet(
4992 const unsigned char *buf, /* Buffer to deserialize from */
4993 u32 serial_type, /* Serial type to deserialize */
4994 Mem *pMem /* Memory cell to write value into */
4995 ){
4996 u64 x = FOUR_BYTE_UINT(buf);
4997 u32 y = FOUR_BYTE_UINT(buf+4);
4998 x = (x<<32) + y;
4999 if( serial_type==6 ){
5000 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
5001 ** twos-complement integer. */
5002 pMem->u.i = *(i64*)&x;
5003 pMem->flags = MEM_Int;
5004 testcase( pMem->u.i<0 );
5005 }else{
5006 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
5007 ** floating point number. */
5008 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
5009 /* Verify that integers and floating point values use the same
5010 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
5011 ** defined that 64-bit floating point values really are mixed
5012 ** endian.
5013 */
5014 static const u64 t1 = ((u64)0x3ff00000)<<32;
5015 static const double r1 = 1.0;
5016 u64 t2 = t1;
5017 swapMixedEndianFloat(t2);
5018 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
5019 #endif
5020 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
5021 swapMixedEndianFloat(x);
5022 memcpy(&pMem->u.r, &x, sizeof(x));
5023 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
5024 }
5025 return 8;
5026 }
5027 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
5028 const unsigned char *buf, /* Buffer to deserialize from */
5029 u32 serial_type, /* Serial type to deserialize */
5030 Mem *pMem /* Memory cell to write value into */
5031 ){
5032 switch( serial_type ){
5033 case 10: /* Reserved for future use */
5034 case 11: /* Reserved for future use */
5035 case 0: { /* Null */
5036 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
5037 pMem->flags = MEM_Null;
5038 break;
5039 }
5040 case 1: {
5041 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
5042 ** integer. */
5043 pMem->u.i = ONE_BYTE_INT(buf);
5044 pMem->flags = MEM_Int;
5045 testcase( pMem->u.i<0 );
5046 return 1;
5047 }
5048 case 2: { /* 2-byte signed integer */
5049 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
5050 ** twos-complement integer. */
5051 pMem->u.i = TWO_BYTE_INT(buf);
5052 pMem->flags = MEM_Int;
5053 testcase( pMem->u.i<0 );
5054 return 2;
5055 }
5056 case 3: { /* 3-byte signed integer */
5057 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
5058 ** twos-complement integer. */
5059 pMem->u.i = THREE_BYTE_INT(buf);
5060 pMem->flags = MEM_Int;
5061 testcase( pMem->u.i<0 );
5062 return 3;
5063 }
5064 case 4: { /* 4-byte signed integer */
5065 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
5066 ** twos-complement integer. */
5067 pMem->u.i = FOUR_BYTE_INT(buf);
5068 #ifdef __HP_cc
5069 /* Work around a sign-extension bug in the HP compiler for HP/UX */
5070 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
5071 #endif
5072 pMem->flags = MEM_Int;
5073 testcase( pMem->u.i<0 );
5074 return 4;
5075 }
5076 case 5: { /* 6-byte signed integer */
5077 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
5078 ** twos-complement integer. */
5079 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
5080 pMem->flags = MEM_Int;
5081 testcase( pMem->u.i<0 );
5082 return 6;
5083 }
5084 case 6: /* 8-byte signed integer */
5085 case 7: { /* IEEE floating point */
5086 /* These use local variables, so do them in a separate routine
5087 ** to avoid having to move the frame pointer in the common case */
5088 return serialGet(buf,serial_type,pMem);
5089 }
5090 case 8: /* Integer 0 */
5091 case 9: { /* Integer 1 */
5092 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
5093 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
5094 pMem->u.i = serial_type-8;
5095 pMem->flags = MEM_Int;
5096 return 0;
5097 }
5098 default: {
5099 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
5100 ** length.
5101 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
5102 ** (N-13)/2 bytes in length. */
5103 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
5104 pMem->z = (char *)buf;
5105 pMem->n = (serial_type-12)/2;
5106 pMem->flags = aFlag[serial_type&1];
5107 return pMem->n;
5108 }
5109 }
5110 return 0;
5111 }
5112 /*
5113 ** This routine is used to allocate sufficient space for an UnpackedRecord
5114 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
5115 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
5116 **
5117 ** The space is either allocated using sqlite3DbMallocRaw() or from within
5118 ** the unaligned buffer passed via the second and third arguments (presumably
5119 ** stack space). If the former, then *ppFree is set to a pointer that should
5120 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
5121 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
5122 ** before returning.
5123 **
5124 ** If an OOM error occurs, NULL is returned.
5125 */
5126 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
5127 KeyInfo *pKeyInfo, /* Description of the record */
5128 char *pSpace, /* Unaligned space available */
5129 int szSpace, /* Size of pSpace[] in bytes */
5130 char **ppFree /* OUT: Caller should free this pointer */
5131 ){
5132 UnpackedRecord *p; /* Unpacked record to return */
5133 int nOff; /* Increment pSpace by nOff to align it */
5134 int nByte; /* Number of bytes required for *p */
5135
5136 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
5137 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
5138 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
5139 */
5140 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
5141 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
5142 if( nByte>szSpace+nOff ){
5143 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
5144 *ppFree = (char *)p;
5145 if( !p ) return 0;
5146 }else{
5147 p = (UnpackedRecord*)&pSpace[nOff];
5148 *ppFree = 0;
5149 }
5150
5151 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
5152 assert( pKeyInfo->aSortOrder!=0 );
5153 p->pKeyInfo = pKeyInfo;
5154 p->nField = pKeyInfo->nField + 1;
5155 return p;
5156 }
5157
5158 /*
5159 ** Given the nKey-byte encoding of a record in pKey[], populate the
5160 ** UnpackedRecord structure indicated by the fourth argument with the
5161 ** contents of the decoded record.
5162 */
5163 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
5164 KeyInfo *pKeyInfo, /* Information about the record format */
5165 int nKey, /* Size of the binary record */
5166 const void *pKey, /* The binary record */
5167 UnpackedRecord *p /* Populate this structure before returning. */
5168 ){
5169 const unsigned char *aKey = (const unsigned char *)pKey;
5170 int d;
5171 u32 idx; /* Offset in aKey[] to read from */
5172 u16 u; /* Unsigned loop counter */
5173 u32 szHdr;
5174 Mem *pMem = p->aMem;
5175
5176 p->default_rc = 0;
5177 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
5178 idx = getVarint32(aKey, szHdr);
5179 d = szHdr;
5180 u = 0;
5181 while( idx<szHdr && d<=nKey ){
5182 u32 serial_type;
5183
5184 idx += getVarint32(&aKey[idx], serial_type);
5185 pMem->enc = pKeyInfo->enc;
5186 pMem->db = pKeyInfo->db;
5187 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
5188 pMem->szMalloc = 0;
5189 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
5190 pMem++;
5191 if( (++u)>=p->nField ) break;
5192 }
5193 assert( u<=pKeyInfo->nField + 1 );
5194 p->nField = u;
5195 }
5196
5197 #if SQLITE_DEBUG
5198 /*
5199 ** This function compares two index or table record keys in the same way
5200 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
5201 ** this function deserializes and compares values using the
5202 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
5203 ** in assert() statements to ensure that the optimized code in
5204 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
5205 **
5206 ** Return true if the result of comparison is equivalent to desiredResult.
5207 ** Return false if there is a disagreement.
5208 */
5209 static int vdbeRecordCompareDebug(
5210 int nKey1, const void *pKey1, /* Left key */
5211 const UnpackedRecord *pPKey2, /* Right key */
5212 int desiredResult /* Correct answer */
5213 ){
5214 u32 d1; /* Offset into aKey[] of next data element */
5215 u32 idx1; /* Offset into aKey[] of next header element */
5216 u32 szHdr1; /* Number of bytes in header */
5217 int i = 0;
5218 int rc = 0;
5219 const unsigned char *aKey1 = (const unsigned char *)pKey1;
5220 KeyInfo *pKeyInfo;
5221 Mem mem1;
5222
5223 pKeyInfo = pPKey2->pKeyInfo;
5224 if( pKeyInfo->db==0 ) return 1;
5225 mem1.enc = pKeyInfo->enc;
5226 mem1.db = pKeyInfo->db;
5227 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
5228 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
5229
5230 /* Compilers may complain that mem1.u.i is potentially uninitialized.
5231 ** We could initialize it, as shown here, to silence those complaints.
5232 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
5233 ** the unnecessary initialization has a measurable negative performance
5234 ** impact, since this routine is a very high runner. And so, we choose
5235 ** to ignore the compiler warnings and leave this variable uninitialized.
5236 */
5237 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
5238
5239 idx1 = getVarint32(aKey1, szHdr1);
5240 if( szHdr1>98307 ) return SQLITE_CORRUPT;
5241 d1 = szHdr1;
5242 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
5243 assert( pKeyInfo->aSortOrder!=0 );
5244 assert( pKeyInfo->nField>0 );
5245 assert( idx1<=szHdr1 || CORRUPT_DB );
5246 do{
5247 u32 serial_type1;
5248
5249 /* Read the serial types for the next element in each key. */
5250 idx1 += getVarint32( aKey1+idx1, serial_type1 );
5251
5252 /* Verify that there is enough key space remaining to avoid
5253 ** a buffer overread. The "d1+serial_type1+2" subexpression will
5254 ** always be greater than or equal to the amount of required key space.
5255 ** Use that approximation to avoid the more expensive call to
5256 ** sqlite3VdbeSerialTypeLen() in the common case.
5257 */
5258 if( d1+serial_type1+2>(u32)nKey1
5259 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
5260 ){
5261 break;
5262 }
5263
5264 /* Extract the values to be compared.
5265 */
5266 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
5267
5268 /* Do the comparison
5269 */
5270 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
5271 if( rc!=0 ){
5272 assert( mem1.szMalloc==0 ); /* See comment below */
5273 if( pKeyInfo->aSortOrder[i] ){
5274 rc = -rc; /* Invert the result for DESC sort order. */
5275 }
5276 goto debugCompareEnd;
5277 }
5278 i++;
5279 }while( idx1<szHdr1 && i<pPKey2->nField );
5280
5281 /* No memory allocation is ever used on mem1. Prove this using
5282 ** the following assert(). If the assert() fails, it indicates a
5283 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
5284 */
5285 assert( mem1.szMalloc==0 );
5286
5287 /* rc==0 here means that one of the keys ran out of fields and
5288 ** all the fields up to that point were equal. Return the default_rc
5289 ** value. */
5290 rc = pPKey2->default_rc;
5291
5292 debugCompareEnd:
5293 if( desiredResult==0 && rc==0 ) return 1;
5294 if( desiredResult<0 && rc<0 ) return 1;
5295 if( desiredResult>0 && rc>0 ) return 1;
5296 if( CORRUPT_DB ) return 1;
5297 if( pKeyInfo->db->mallocFailed ) return 1;
5298 return 0;
5299 }
5300 #endif
5301
5302 #if SQLITE_DEBUG
5303 /*
5304 ** Count the number of fields (a.k.a. columns) in the record given by
5305 ** pKey,nKey. The verify that this count is less than or equal to the
5306 ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
5307 **
5308 ** If this constraint is not satisfied, it means that the high-speed
5309 ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
5310 ** not work correctly. If this assert() ever fires, it probably means
5311 ** that the KeyInfo.nField or KeyInfo.nXField values were computed
5312 ** incorrectly.
5313 */
5314 static void vdbeAssertFieldCountWithinLimits(
5315 int nKey, const void *pKey, /* The record to verify */
5316 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
5317 ){
5318 int nField = 0;
5319 u32 szHdr;
5320 u32 idx;
5321 u32 notUsed;
5322 const unsigned char *aKey = (const unsigned char*)pKey;
5323
5324 if( CORRUPT_DB ) return;
5325 idx = getVarint32(aKey, szHdr);
5326 assert( nKey>=0 );
5327 assert( szHdr<=(u32)nKey );
5328 while( idx<szHdr ){
5329 idx += getVarint32(aKey+idx, notUsed);
5330 nField++;
5331 }
5332 assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
5333 }
5334 #else
5335 # define vdbeAssertFieldCountWithinLimits(A,B,C)
5336 #endif
5337
5338 /*
5339 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
5340 ** using the collation sequence pColl. As usual, return a negative , zero
5341 ** or positive value if *pMem1 is less than, equal to or greater than
5342 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
5343 */
5344 static int vdbeCompareMemString(
5345 const Mem *pMem1,
5346 const Mem *pMem2,
5347 const CollSeq *pColl,
5348 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
5349 ){
5350 if( pMem1->enc==pColl->enc ){
5351 /* The strings are already in the correct encoding. Call the
5352 ** comparison function directly */
5353 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
5354 }else{
5355 int rc;
5356 const void *v1, *v2;
5357 int n1, n2;
5358 Mem c1;
5359 Mem c2;
5360 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
5361 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
5362 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
5363 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
5364 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
5365 n1 = v1==0 ? 0 : c1.n;
5366 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
5367 n2 = v2==0 ? 0 : c2.n;
5368 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
5369 sqlite3VdbeMemRelease(&c1);
5370 sqlite3VdbeMemRelease(&c2);
5371 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
5372 return rc;
5373 }
5374 }
5375
5376 /*
5377 ** Compare two blobs. Return negative, zero, or positive if the first
5378 ** is less than, equal to, or greater than the second, respectively.
5379 ** If one blob is a prefix of the other, then the shorter is the lessor.
5380 */
5381 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
5382 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
5383 if( c ) return c;
5384 return pB1->n - pB2->n;
5385 }
5386
5387 /*
5388 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
5389 ** number. Return negative, zero, or positive if the first (i64) is less than,
5390 ** equal to, or greater than the second (double).
5391 */
5392 static int sqlite3IntFloatCompare(i64 i, double r){
5393 if( sizeof(LONGDOUBLE_TYPE)>8 ){
5394 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
5395 if( x<r ) return -1;
5396 if( x>r ) return +1;
5397 return 0;
5398 }else{
5399 i64 y;
5400 double s;
5401 if( r<-9223372036854775808.0 ) return +1;
5402 if( r>9223372036854775807.0 ) return -1;
5403 y = (i64)r;
5404 if( i<y ) return -1;
5405 if( i>y ){
5406 if( y==SMALLEST_INT64 && r>0.0 ) return -1;
5407 return +1;
5408 }
5409 s = (double)i;
5410 if( s<r ) return -1;
5411 if( s>r ) return +1;
5412 return 0;
5413 }
5414 }
5415
5416 /*
5417 ** Compare the values contained by the two memory cells, returning
5418 ** negative, zero or positive if pMem1 is less than, equal to, or greater
5419 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
5420 ** and reals) sorted numerically, followed by text ordered by the collating
5421 ** sequence pColl and finally blob's ordered by memcmp().
5422 **
5423 ** Two NULL values are considered equal by this function.
5424 */
5425 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const C ollSeq *pColl){
5426 int f1, f2;
5427 int combined_flags;
5428
5429 f1 = pMem1->flags;
5430 f2 = pMem2->flags;
5431 combined_flags = f1|f2;
5432 assert( (combined_flags & MEM_RowSet)==0 );
5433
5434 /* If one value is NULL, it is less than the other. If both values
5435 ** are NULL, return 0.
5436 */
5437 if( combined_flags&MEM_Null ){
5438 return (f2&MEM_Null) - (f1&MEM_Null);
5439 }
5440
5441 /* At least one of the two values is a number
5442 */
5443 if( combined_flags&(MEM_Int|MEM_Real) ){
5444 if( (f1 & f2 & MEM_Int)!=0 ){
5445 if( pMem1->u.i < pMem2->u.i ) return -1;
5446 if( pMem1->u.i > pMem2->u.i ) return +1;
5447 return 0;
5448 }
5449 if( (f1 & f2 & MEM_Real)!=0 ){
5450 if( pMem1->u.r < pMem2->u.r ) return -1;
5451 if( pMem1->u.r > pMem2->u.r ) return +1;
5452 return 0;
5453 }
5454 if( (f1&MEM_Int)!=0 ){
5455 if( (f2&MEM_Real)!=0 ){
5456 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
5457 }else{
5458 return -1;
5459 }
5460 }
5461 if( (f1&MEM_Real)!=0 ){
5462 if( (f2&MEM_Int)!=0 ){
5463 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
5464 }else{
5465 return -1;
5466 }
5467 }
5468 return +1;
5469 }
5470
5471 /* If one value is a string and the other is a blob, the string is less.
5472 ** If both are strings, compare using the collating functions.
5473 */
5474 if( combined_flags&MEM_Str ){
5475 if( (f1 & MEM_Str)==0 ){
5476 return 1;
5477 }
5478 if( (f2 & MEM_Str)==0 ){
5479 return -1;
5480 }
5481
5482 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
5483 assert( pMem1->enc==SQLITE_UTF8 ||
5484 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
5485
5486 /* The collation sequence must be defined at this point, even if
5487 ** the user deletes the collation sequence after the vdbe program is
5488 ** compiled (this was not always the case).
5489 */
5490 assert( !pColl || pColl->xCmp );
5491
5492 if( pColl ){
5493 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
5494 }
5495 /* If a NULL pointer was passed as the collate function, fall through
5496 ** to the blob case and use memcmp(). */
5497 }
5498
5499 /* Both values must be blobs. Compare using memcmp(). */
5500 return sqlite3BlobCompare(pMem1, pMem2);
5501 }
5502
5503
5504 /*
5505 ** The first argument passed to this function is a serial-type that
5506 ** corresponds to an integer - all values between 1 and 9 inclusive
5507 ** except 7. The second points to a buffer containing an integer value
5508 ** serialized according to serial_type. This function deserializes
5509 ** and returns the value.
5510 */
5511 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
5512 u32 y;
5513 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
5514 switch( serial_type ){
5515 case 0:
5516 case 1:
5517 testcase( aKey[0]&0x80 );
5518 return ONE_BYTE_INT(aKey);
5519 case 2:
5520 testcase( aKey[0]&0x80 );
5521 return TWO_BYTE_INT(aKey);
5522 case 3:
5523 testcase( aKey[0]&0x80 );
5524 return THREE_BYTE_INT(aKey);
5525 case 4: {
5526 testcase( aKey[0]&0x80 );
5527 y = FOUR_BYTE_UINT(aKey);
5528 return (i64)*(int*)&y;
5529 }
5530 case 5: {
5531 testcase( aKey[0]&0x80 );
5532 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
5533 }
5534 case 6: {
5535 u64 x = FOUR_BYTE_UINT(aKey);
5536 testcase( aKey[0]&0x80 );
5537 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
5538 return (i64)*(i64*)&x;
5539 }
5540 }
5541
5542 return (serial_type - 8);
5543 }
5544
5545 /*
5546 ** This function compares the two table rows or index records
5547 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
5548 ** or positive integer if key1 is less than, equal to or
5549 ** greater than key2. The {nKey1, pKey1} key must be a blob
5550 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
5551 ** key must be a parsed key such as obtained from
5552 ** sqlite3VdbeParseRecord.
5553 **
5554 ** If argument bSkip is non-zero, it is assumed that the caller has already
5555 ** determined that the first fields of the keys are equal.
5556 **
5557 ** Key1 and Key2 do not have to contain the same number of fields. If all
5558 ** fields that appear in both keys are equal, then pPKey2->default_rc is
5559 ** returned.
5560 **
5561 ** If database corruption is discovered, set pPKey2->errCode to
5562 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
5563 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
5564 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
5565 */
5566 SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
5567 int nKey1, const void *pKey1, /* Left key */
5568 UnpackedRecord *pPKey2, /* Right key */
5569 int bSkip /* If true, skip the first field */
5570 ){
5571 u32 d1; /* Offset into aKey[] of next data element */
5572 int i; /* Index of next field to compare */
5573 u32 szHdr1; /* Size of record header in bytes */
5574 u32 idx1; /* Offset of first type in header */
5575 int rc = 0; /* Return value */
5576 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
5577 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
5578 const unsigned char *aKey1 = (const unsigned char *)pKey1;
5579 Mem mem1;
5580
5581 /* If bSkip is true, then the caller has already determined that the first
5582 ** two elements in the keys are equal. Fix the various stack variables so
5583 ** that this routine begins comparing at the second field. */
5584 if( bSkip ){
5585 u32 s1;
5586 idx1 = 1 + getVarint32(&aKey1[1], s1);
5587 szHdr1 = aKey1[0];
5588 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
5589 i = 1;
5590 pRhs++;
5591 }else{
5592 idx1 = getVarint32(aKey1, szHdr1);
5593 d1 = szHdr1;
5594 if( d1>(unsigned)nKey1 ){
5595 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
5596 return 0; /* Corruption */
5597 }
5598 i = 0;
5599 }
5600
5601 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
5602 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
5603 || CORRUPT_DB );
5604 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
5605 assert( pPKey2->pKeyInfo->nField>0 );
5606 assert( idx1<=szHdr1 || CORRUPT_DB );
5607 do{
5608 u32 serial_type;
5609
5610 /* RHS is an integer */
5611 if( pRhs->flags & MEM_Int ){
5612 serial_type = aKey1[idx1];
5613 testcase( serial_type==12 );
5614 if( serial_type>=10 ){
5615 rc = +1;
5616 }else if( serial_type==0 ){
5617 rc = -1;
5618 }else if( serial_type==7 ){
5619 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
5620 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
5621 }else{
5622 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
5623 i64 rhs = pRhs->u.i;
5624 if( lhs<rhs ){
5625 rc = -1;
5626 }else if( lhs>rhs ){
5627 rc = +1;
5628 }
5629 }
5630 }
5631
5632 /* RHS is real */
5633 else if( pRhs->flags & MEM_Real ){
5634 serial_type = aKey1[idx1];
5635 if( serial_type>=10 ){
5636 /* Serial types 12 or greater are strings and blobs (greater than
5637 ** numbers). Types 10 and 11 are currently "reserved for future
5638 ** use", so it doesn't really matter what the results of comparing
5639 ** them to numberic values are. */
5640 rc = +1;
5641 }else if( serial_type==0 ){
5642 rc = -1;
5643 }else{
5644 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
5645 if( serial_type==7 ){
5646 if( mem1.u.r<pRhs->u.r ){
5647 rc = -1;
5648 }else if( mem1.u.r>pRhs->u.r ){
5649 rc = +1;
5650 }
5651 }else{
5652 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
5653 }
5654 }
5655 }
5656
5657 /* RHS is a string */
5658 else if( pRhs->flags & MEM_Str ){
5659 getVarint32(&aKey1[idx1], serial_type);
5660 testcase( serial_type==12 );
5661 if( serial_type<12 ){
5662 rc = -1;
5663 }else if( !(serial_type & 0x01) ){
5664 rc = +1;
5665 }else{
5666 mem1.n = (serial_type - 12) / 2;
5667 testcase( (d1+mem1.n)==(unsigned)nKey1 );
5668 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
5669 if( (d1+mem1.n) > (unsigned)nKey1 ){
5670 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
5671 return 0; /* Corruption */
5672 }else if( pKeyInfo->aColl[i] ){
5673 mem1.enc = pKeyInfo->enc;
5674 mem1.db = pKeyInfo->db;
5675 mem1.flags = MEM_Str;
5676 mem1.z = (char*)&aKey1[d1];
5677 rc = vdbeCompareMemString(
5678 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
5679 );
5680 }else{
5681 int nCmp = MIN(mem1.n, pRhs->n);
5682 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
5683 if( rc==0 ) rc = mem1.n - pRhs->n;
5684 }
5685 }
5686 }
5687
5688 /* RHS is a blob */
5689 else if( pRhs->flags & MEM_Blob ){
5690 getVarint32(&aKey1[idx1], serial_type);
5691 testcase( serial_type==12 );
5692 if( serial_type<12 || (serial_type & 0x01) ){
5693 rc = -1;
5694 }else{
5695 int nStr = (serial_type - 12) / 2;
5696 testcase( (d1+nStr)==(unsigned)nKey1 );
5697 testcase( (d1+nStr+1)==(unsigned)nKey1 );
5698 if( (d1+nStr) > (unsigned)nKey1 ){
5699 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
5700 return 0; /* Corruption */
5701 }else{
5702 int nCmp = MIN(nStr, pRhs->n);
5703 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
5704 if( rc==0 ) rc = nStr - pRhs->n;
5705 }
5706 }
5707 }
5708
5709 /* RHS is null */
5710 else{
5711 serial_type = aKey1[idx1];
5712 rc = (serial_type!=0);
5713 }
5714
5715 if( rc!=0 ){
5716 if( pKeyInfo->aSortOrder[i] ){
5717 rc = -rc;
5718 }
5719 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
5720 assert( mem1.szMalloc==0 ); /* See comment below */
5721 return rc;
5722 }
5723
5724 i++;
5725 pRhs++;
5726 d1 += sqlite3VdbeSerialTypeLen(serial_type);
5727 idx1 += sqlite3VarintLen(serial_type);
5728 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
5729
5730 /* No memory allocation is ever used on mem1. Prove this using
5731 ** the following assert(). If the assert() fails, it indicates a
5732 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
5733 assert( mem1.szMalloc==0 );
5734
5735 /* rc==0 here means that one or both of the keys ran out of fields and
5736 ** all the fields up to that point were equal. Return the default_rc
5737 ** value. */
5738 assert( CORRUPT_DB
5739 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
5740 || pKeyInfo->db->mallocFailed
5741 );
5742 pPKey2->eqSeen = 1;
5743 return pPKey2->default_rc;
5744 }
5745 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
5746 int nKey1, const void *pKey1, /* Left key */
5747 UnpackedRecord *pPKey2 /* Right key */
5748 ){
5749 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
5750 }
5751
5752
5753 /*
5754 ** This function is an optimized version of sqlite3VdbeRecordCompare()
5755 ** that (a) the first field of pPKey2 is an integer, and (b) the
5756 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
5757 ** byte (i.e. is less than 128).
5758 **
5759 ** To avoid concerns about buffer overreads, this routine is only used
5760 ** on schemas where the maximum valid header size is 63 bytes or less.
5761 */
5762 static int vdbeRecordCompareInt(
5763 int nKey1, const void *pKey1, /* Left key */
5764 UnpackedRecord *pPKey2 /* Right key */
5765 ){
5766 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
5767 int serial_type = ((const u8*)pKey1)[1];
5768 int res;
5769 u32 y;
5770 u64 x;
5771 i64 v = pPKey2->aMem[0].u.i;
5772 i64 lhs;
5773
5774 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
5775 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
5776 switch( serial_type ){
5777 case 1: { /* 1-byte signed integer */
5778 lhs = ONE_BYTE_INT(aKey);
5779 testcase( lhs<0 );
5780 break;
5781 }
5782 case 2: { /* 2-byte signed integer */
5783 lhs = TWO_BYTE_INT(aKey);
5784 testcase( lhs<0 );
5785 break;
5786 }
5787 case 3: { /* 3-byte signed integer */
5788 lhs = THREE_BYTE_INT(aKey);
5789 testcase( lhs<0 );
5790 break;
5791 }
5792 case 4: { /* 4-byte signed integer */
5793 y = FOUR_BYTE_UINT(aKey);
5794 lhs = (i64)*(int*)&y;
5795 testcase( lhs<0 );
5796 break;
5797 }
5798 case 5: { /* 6-byte signed integer */
5799 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
5800 testcase( lhs<0 );
5801 break;
5802 }
5803 case 6: { /* 8-byte signed integer */
5804 x = FOUR_BYTE_UINT(aKey);
5805 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
5806 lhs = *(i64*)&x;
5807 testcase( lhs<0 );
5808 break;
5809 }
5810 case 8:
5811 lhs = 0;
5812 break;
5813 case 9:
5814 lhs = 1;
5815 break;
5816
5817 /* This case could be removed without changing the results of running
5818 ** this code. Including it causes gcc to generate a faster switch
5819 ** statement (since the range of switch targets now starts at zero and
5820 ** is contiguous) but does not cause any duplicate code to be generated
5821 ** (as gcc is clever enough to combine the two like cases). Other
5822 ** compilers might be similar. */
5823 case 0: case 7:
5824 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
5825
5826 default:
5827 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
5828 }
5829
5830 if( v>lhs ){
5831 res = pPKey2->r1;
5832 }else if( v<lhs ){
5833 res = pPKey2->r2;
5834 }else if( pPKey2->nField>1 ){
5835 /* The first fields of the two keys are equal. Compare the trailing
5836 ** fields. */
5837 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
5838 }else{
5839 /* The first fields of the two keys are equal and there are no trailing
5840 ** fields. Return pPKey2->default_rc in this case. */
5841 res = pPKey2->default_rc;
5842 pPKey2->eqSeen = 1;
5843 }
5844
5845 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
5846 return res;
5847 }
5848
5849 /*
5850 ** This function is an optimized version of sqlite3VdbeRecordCompare()
5851 ** that (a) the first field of pPKey2 is a string, that (b) the first field
5852 ** uses the collation sequence BINARY and (c) that the size-of-header varint
5853 ** at the start of (pKey1/nKey1) fits in a single byte.
5854 */
5855 static int vdbeRecordCompareString(
5856 int nKey1, const void *pKey1, /* Left key */
5857 UnpackedRecord *pPKey2 /* Right key */
5858 ){
5859 const u8 *aKey1 = (const u8*)pKey1;
5860 int serial_type;
5861 int res;
5862
5863 assert( pPKey2->aMem[0].flags & MEM_Str );
5864 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
5865 getVarint32(&aKey1[1], serial_type);
5866 if( serial_type<12 ){
5867 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
5868 }else if( !(serial_type & 0x01) ){
5869 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
5870 }else{
5871 int nCmp;
5872 int nStr;
5873 int szHdr = aKey1[0];
5874
5875 nStr = (serial_type-12) / 2;
5876 if( (szHdr + nStr) > nKey1 ){
5877 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
5878 return 0; /* Corruption */
5879 }
5880 nCmp = MIN( pPKey2->aMem[0].n, nStr );
5881 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
5882
5883 if( res==0 ){
5884 res = nStr - pPKey2->aMem[0].n;
5885 if( res==0 ){
5886 if( pPKey2->nField>1 ){
5887 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
5888 }else{
5889 res = pPKey2->default_rc;
5890 pPKey2->eqSeen = 1;
5891 }
5892 }else if( res>0 ){
5893 res = pPKey2->r2;
5894 }else{
5895 res = pPKey2->r1;
5896 }
5897 }else if( res>0 ){
5898 res = pPKey2->r2;
5899 }else{
5900 res = pPKey2->r1;
5901 }
5902 }
5903
5904 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
5905 || CORRUPT_DB
5906 || pPKey2->pKeyInfo->db->mallocFailed
5907 );
5908 return res;
5909 }
5910
5911 /*
5912 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
5913 ** suitable for comparing serialized records to the unpacked record passed
5914 ** as the only argument.
5915 */
5916 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
5917 /* varintRecordCompareInt() and varintRecordCompareString() both assume
5918 ** that the size-of-header varint that occurs at the start of each record
5919 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
5920 ** also assumes that it is safe to overread a buffer by at least the
5921 ** maximum possible legal header size plus 8 bytes. Because there is
5922 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
5923 ** buffer passed to varintRecordCompareInt() this makes it convenient to
5924 ** limit the size of the header to 64 bytes in cases where the first field
5925 ** is an integer.
5926 **
5927 ** The easiest way to enforce this limit is to consider only records with
5928 ** 13 fields or less. If the first field is an integer, the maximum legal
5929 ** header size is (12*5 + 1 + 1) bytes. */
5930 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
5931 int flags = p->aMem[0].flags;
5932 if( p->pKeyInfo->aSortOrder[0] ){
5933 p->r1 = 1;
5934 p->r2 = -1;
5935 }else{
5936 p->r1 = -1;
5937 p->r2 = 1;
5938 }
5939 if( (flags & MEM_Int) ){
5940 return vdbeRecordCompareInt;
5941 }
5942 testcase( flags & MEM_Real );
5943 testcase( flags & MEM_Null );
5944 testcase( flags & MEM_Blob );
5945 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
5946 assert( flags & MEM_Str );
5947 return vdbeRecordCompareString;
5948 }
5949 }
5950
5951 return sqlite3VdbeRecordCompare;
5952 }
5953
5954 /*
5955 ** pCur points at an index entry created using the OP_MakeRecord opcode.
5956 ** Read the rowid (the last field in the record) and store it in *rowid.
5957 ** Return SQLITE_OK if everything works, or an error code otherwise.
5958 **
5959 ** pCur might be pointing to text obtained from a corrupt database file.
5960 ** So the content cannot be trusted. Do appropriate checks on the content.
5961 */
5962 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
5963 i64 nCellKey = 0;
5964 int rc;
5965 u32 szHdr; /* Size of the header */
5966 u32 typeRowid; /* Serial type of the rowid */
5967 u32 lenRowid; /* Size of the rowid */
5968 Mem m, v;
5969
5970 /* Get the size of the index entry. Only indices entries of less
5971 ** than 2GiB are support - anything large must be database corruption.
5972 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
5973 ** this code can safely assume that nCellKey is 32-bits
5974 */
5975 assert( sqlite3BtreeCursorIsValid(pCur) );
5976 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
5977 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
5978 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
5979
5980 /* Read in the complete content of the index entry */
5981 sqlite3VdbeMemInit(&m, db, 0);
5982 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
5983 if( rc ){
5984 return rc;
5985 }
5986
5987 /* The index entry must begin with a header size */
5988 (void)getVarint32((u8*)m.z, szHdr);
5989 testcase( szHdr==3 );
5990 testcase( szHdr==m.n );
5991 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
5992 goto idx_rowid_corruption;
5993 }
5994
5995 /* The last field of the index should be an integer - the ROWID.
5996 ** Verify that the last entry really is an integer. */
5997 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
5998 testcase( typeRowid==1 );
5999 testcase( typeRowid==2 );
6000 testcase( typeRowid==3 );
6001 testcase( typeRowid==4 );
6002 testcase( typeRowid==5 );
6003 testcase( typeRowid==6 );
6004 testcase( typeRowid==8 );
6005 testcase( typeRowid==9 );
6006 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
6007 goto idx_rowid_corruption;
6008 }
6009 lenRowid = sqlite3SmallTypeSizes[typeRowid];
6010 testcase( (u32)m.n==szHdr+lenRowid );
6011 if( unlikely((u32)m.n<szHdr+lenRowid) ){
6012 goto idx_rowid_corruption;
6013 }
6014
6015 /* Fetch the integer off the end of the index record */
6016 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
6017 *rowid = v.u.i;
6018 sqlite3VdbeMemRelease(&m);
6019 return SQLITE_OK;
6020
6021 /* Jump here if database corruption is detected after m has been
6022 ** allocated. Free the m object and return SQLITE_CORRUPT. */
6023 idx_rowid_corruption:
6024 testcase( m.szMalloc!=0 );
6025 sqlite3VdbeMemRelease(&m);
6026 return SQLITE_CORRUPT_BKPT;
6027 }
6028
6029 /*
6030 ** Compare the key of the index entry that cursor pC is pointing to against
6031 ** the key string in pUnpacked. Write into *pRes a number
6032 ** that is negative, zero, or positive if pC is less than, equal to,
6033 ** or greater than pUnpacked. Return SQLITE_OK on success.
6034 **
6035 ** pUnpacked is either created without a rowid or is truncated so that it
6036 ** omits the rowid at the end. The rowid at the end of the index entry
6037 ** is ignored as well. Hence, this routine only compares the prefixes
6038 ** of the keys prior to the final rowid, not the entire key.
6039 */
6040 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
6041 sqlite3 *db, /* Database connection */
6042 VdbeCursor *pC, /* The cursor to compare against */
6043 UnpackedRecord *pUnpacked, /* Unpacked version of key */
6044 int *res /* Write the comparison result here */
6045 ){
6046 i64 nCellKey = 0;
6047 int rc;
6048 BtCursor *pCur;
6049 Mem m;
6050
6051 assert( pC->eCurType==CURTYPE_BTREE );
6052 pCur = pC->uc.pCursor;
6053 assert( sqlite3BtreeCursorIsValid(pCur) );
6054 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
6055 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
6056 /* nCellKey will always be between 0 and 0xffffffff because of the way
6057 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6058 if( nCellKey<=0 || nCellKey>0x7fffffff ){
6059 *res = 0;
6060 return SQLITE_CORRUPT_BKPT;
6061 }
6062 sqlite3VdbeMemInit(&m, db, 0);
6063 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
6064 if( rc ){
6065 return rc;
6066 }
6067 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
6068 sqlite3VdbeMemRelease(&m);
6069 return SQLITE_OK;
6070 }
6071
6072 /*
6073 ** This routine sets the value to be returned by subsequent calls to
6074 ** sqlite3_changes() on the database handle 'db'.
6075 */
6076 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
6077 assert( sqlite3_mutex_held(db->mutex) );
6078 db->nChange = nChange;
6079 db->nTotalChange += nChange;
6080 }
6081
6082 /*
6083 ** Set a flag in the vdbe to update the change counter when it is finalised
6084 ** or reset.
6085 */
6086 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
6087 v->changeCntOn = 1;
6088 }
6089
6090 /*
6091 ** Mark every prepared statement associated with a database connection
6092 ** as expired.
6093 **
6094 ** An expired statement means that recompilation of the statement is
6095 ** recommend. Statements expire when things happen that make their
6096 ** programs obsolete. Removing user-defined functions or collating
6097 ** sequences, or changing an authorization function are the types of
6098 ** things that make prepared statements obsolete.
6099 */
6100 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
6101 Vdbe *p;
6102 for(p = db->pVdbe; p; p=p->pNext){
6103 p->expired = 1;
6104 }
6105 }
6106
6107 /*
6108 ** Return the database associated with the Vdbe.
6109 */
6110 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
6111 return v->db;
6112 }
6113
6114 /*
6115 ** Return a pointer to an sqlite3_value structure containing the value bound
6116 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
6117 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
6118 ** constants) to the value before returning it.
6119 **
6120 ** The returned value must be freed by the caller using sqlite3ValueFree().
6121 */
6122 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff ){
6123 assert( iVar>0 );
6124 if( v ){
6125 Mem *pMem = &v->aVar[iVar-1];
6126 if( 0==(pMem->flags & MEM_Null) ){
6127 sqlite3_value *pRet = sqlite3ValueNew(v->db);
6128 if( pRet ){
6129 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
6130 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
6131 }
6132 return pRet;
6133 }
6134 }
6135 return 0;
6136 }
6137
6138 /*
6139 ** Configure SQL variable iVar so that binding a new value to it signals
6140 ** to sqlite3_reoptimize() that re-preparing the statement may result
6141 ** in a better query plan.
6142 */
6143 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
6144 assert( iVar>0 );
6145 if( iVar>32 ){
6146 v->expmask = 0xffffffff;
6147 }else{
6148 v->expmask |= ((u32)1 << (iVar-1));
6149 }
6150 }
6151
6152 #ifndef SQLITE_OMIT_VIRTUALTABLE
6153 /*
6154 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
6155 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
6156 ** in memory obtained from sqlite3DbMalloc).
6157 */
6158 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
6159 sqlite3 *db = p->db;
6160 sqlite3DbFree(db, p->zErrMsg);
6161 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
6162 sqlite3_free(pVtab->zErrMsg);
6163 pVtab->zErrMsg = 0;
6164 }
6165 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6166
6167 /************** End of vdbeaux.c *********************************************/
6168 /************** Begin file vdbeapi.c *****************************************/
6169 /*
6170 ** 2004 May 26
6171 **
6172 ** The author disclaims copyright to this source code. In place of
6173 ** a legal notice, here is a blessing:
6174 **
6175 ** May you do good and not evil.
6176 ** May you find forgiveness for yourself and forgive others.
6177 ** May you share freely, never taking more than you give.
6178 **
6179 *************************************************************************
6180 **
6181 ** This file contains code use to implement APIs that are part of the
6182 ** VDBE.
6183 */
6184 /* #include "sqliteInt.h" */
6185 /* #include "vdbeInt.h" */
6186
6187 #ifndef SQLITE_OMIT_DEPRECATED
6188 /*
6189 ** Return TRUE (non-zero) of the statement supplied as an argument needs
6190 ** to be recompiled. A statement needs to be recompiled whenever the
6191 ** execution environment changes in a way that would alter the program
6192 ** that sqlite3_prepare() generates. For example, if new functions or
6193 ** collating sequences are registered or if an authorizer function is
6194 ** added or changed.
6195 */
6196 SQLITE_API int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt *pStmt){
6197 Vdbe *p = (Vdbe*)pStmt;
6198 return p==0 || p->expired;
6199 }
6200 #endif
6201
6202 /*
6203 ** Check on a Vdbe to make sure it has not been finalized. Log
6204 ** an error and return true if it has been finalized (or is otherwise
6205 ** invalid). Return false if it is ok.
6206 */
6207 static int vdbeSafety(Vdbe *p){
6208 if( p->db==0 ){
6209 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
6210 return 1;
6211 }else{
6212 return 0;
6213 }
6214 }
6215 static int vdbeSafetyNotNull(Vdbe *p){
6216 if( p==0 ){
6217 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
6218 return 1;
6219 }else{
6220 return vdbeSafety(p);
6221 }
6222 }
6223
6224 #ifndef SQLITE_OMIT_TRACE
6225 /*
6226 ** Invoke the profile callback. This routine is only called if we already
6227 ** know that the profile callback is defined and needs to be invoked.
6228 */
6229 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
6230 sqlite3_int64 iNow;
6231 assert( p->startTime>0 );
6232 assert( db->xProfile!=0 );
6233 assert( db->init.busy==0 );
6234 assert( p->zSql!=0 );
6235 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
6236 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
6237 p->startTime = 0;
6238 }
6239 /*
6240 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
6241 ** is needed, and it invokes the callback if it is needed.
6242 */
6243 # define checkProfileCallback(DB,P) \
6244 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
6245 #else
6246 # define checkProfileCallback(DB,P) /*no-op*/
6247 #endif
6248
6249 /*
6250 ** The following routine destroys a virtual machine that is created by
6251 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
6252 ** success/failure code that describes the result of executing the virtual
6253 ** machine.
6254 **
6255 ** This routine sets the error code and string returned by
6256 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
6257 */
6258 SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){
6259 int rc;
6260 if( pStmt==0 ){
6261 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
6262 ** pointer is a harmless no-op. */
6263 rc = SQLITE_OK;
6264 }else{
6265 Vdbe *v = (Vdbe*)pStmt;
6266 sqlite3 *db = v->db;
6267 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
6268 sqlite3_mutex_enter(db->mutex);
6269 checkProfileCallback(db, v);
6270 rc = sqlite3VdbeFinalize(v);
6271 rc = sqlite3ApiExit(db, rc);
6272 sqlite3LeaveMutexAndCloseZombie(db);
6273 }
6274 return rc;
6275 }
6276
6277 /*
6278 ** Terminate the current execution of an SQL statement and reset it
6279 ** back to its starting state so that it can be reused. A success code from
6280 ** the prior execution is returned.
6281 **
6282 ** This routine sets the error code and string returned by
6283 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
6284 */
6285 SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt){
6286 int rc;
6287 if( pStmt==0 ){
6288 rc = SQLITE_OK;
6289 }else{
6290 Vdbe *v = (Vdbe*)pStmt;
6291 sqlite3 *db = v->db;
6292 sqlite3_mutex_enter(db->mutex);
6293 checkProfileCallback(db, v);
6294 rc = sqlite3VdbeReset(v);
6295 sqlite3VdbeRewind(v);
6296 assert( (rc & (db->errMask))==rc );
6297 rc = sqlite3ApiExit(db, rc);
6298 sqlite3_mutex_leave(db->mutex);
6299 }
6300 return rc;
6301 }
6302
6303 /*
6304 ** Set all the parameters in the compiled SQL statement to NULL.
6305 */
6306 SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){
6307 int i;
6308 int rc = SQLITE_OK;
6309 Vdbe *p = (Vdbe*)pStmt;
6310 #if SQLITE_THREADSAFE
6311 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
6312 #endif
6313 sqlite3_mutex_enter(mutex);
6314 for(i=0; i<p->nVar; i++){
6315 sqlite3VdbeMemRelease(&p->aVar[i]);
6316 p->aVar[i].flags = MEM_Null;
6317 }
6318 if( p->isPrepareV2 && p->expmask ){
6319 p->expired = 1;
6320 }
6321 sqlite3_mutex_leave(mutex);
6322 return rc;
6323 }
6324
6325
6326 /**************************** sqlite3_value_ *******************************
6327 ** The following routines extract information from a Mem or sqlite3_value
6328 ** structure.
6329 */
6330 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value *pVal){
6331 Mem *p = (Mem*)pVal;
6332 if( p->flags & (MEM_Blob|MEM_Str) ){
6333 if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){
6334 assert( p->flags==MEM_Null && p->z==0 );
6335 return 0;
6336 }
6337 p->flags |= MEM_Blob;
6338 return p->n ? p->z : 0;
6339 }else{
6340 return sqlite3_value_text(pVal);
6341 }
6342 }
6343 SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value *pVal){
6344 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
6345 }
6346 SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value *pVal){
6347 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
6348 }
6349 SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value *pVal){
6350 return sqlite3VdbeRealValue((Mem*)pVal);
6351 }
6352 SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value *pVal){
6353 return (int)sqlite3VdbeIntValue((Mem*)pVal);
6354 }
6355 SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
6356 return sqlite3VdbeIntValue((Mem*)pVal);
6357 }
6358 SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value *pVal ){
6359 return ((Mem*)pVal)->eSubtype;
6360 }
6361 SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
6362 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
6363 }
6364 #ifndef SQLITE_OMIT_UTF16
6365 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value* pVal){
6366 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
6367 }
6368 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value *pVal ){
6369 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
6370 }
6371 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value *pVal ){
6372 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
6373 }
6374 #endif /* SQLITE_OMIT_UTF16 */
6375 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
6376 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
6377 ** point number string BLOB NULL
6378 */
6379 SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value* pVal){
6380 static const u8 aType[] = {
6381 SQLITE_BLOB, /* 0x00 */
6382 SQLITE_NULL, /* 0x01 */
6383 SQLITE_TEXT, /* 0x02 */
6384 SQLITE_NULL, /* 0x03 */
6385 SQLITE_INTEGER, /* 0x04 */
6386 SQLITE_NULL, /* 0x05 */
6387 SQLITE_INTEGER, /* 0x06 */
6388 SQLITE_NULL, /* 0x07 */
6389 SQLITE_FLOAT, /* 0x08 */
6390 SQLITE_NULL, /* 0x09 */
6391 SQLITE_FLOAT, /* 0x0a */
6392 SQLITE_NULL, /* 0x0b */
6393 SQLITE_INTEGER, /* 0x0c */
6394 SQLITE_NULL, /* 0x0d */
6395 SQLITE_INTEGER, /* 0x0e */
6396 SQLITE_NULL, /* 0x0f */
6397 SQLITE_BLOB, /* 0x10 */
6398 SQLITE_NULL, /* 0x11 */
6399 SQLITE_TEXT, /* 0x12 */
6400 SQLITE_NULL, /* 0x13 */
6401 SQLITE_INTEGER, /* 0x14 */
6402 SQLITE_NULL, /* 0x15 */
6403 SQLITE_INTEGER, /* 0x16 */
6404 SQLITE_NULL, /* 0x17 */
6405 SQLITE_FLOAT, /* 0x18 */
6406 SQLITE_NULL, /* 0x19 */
6407 SQLITE_FLOAT, /* 0x1a */
6408 SQLITE_NULL, /* 0x1b */
6409 SQLITE_INTEGER, /* 0x1c */
6410 SQLITE_NULL, /* 0x1d */
6411 SQLITE_INTEGER, /* 0x1e */
6412 SQLITE_NULL, /* 0x1f */
6413 };
6414 return aType[pVal->flags&MEM_AffMask];
6415 }
6416
6417 /* Make a copy of an sqlite3_value object
6418 */
6419 SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value * pOrig){
6420 sqlite3_value *pNew;
6421 if( pOrig==0 ) return 0;
6422 pNew = sqlite3_malloc( sizeof(*pNew) );
6423 if( pNew==0 ) return 0;
6424 memset(pNew, 0, sizeof(*pNew));
6425 memcpy(pNew, pOrig, MEMCELLSIZE);
6426 pNew->flags &= ~MEM_Dyn;
6427 pNew->db = 0;
6428 if( pNew->flags&(MEM_Str|MEM_Blob) ){
6429 pNew->flags &= ~(MEM_Static|MEM_Dyn);
6430 pNew->flags |= MEM_Ephem;
6431 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
6432 sqlite3ValueFree(pNew);
6433 pNew = 0;
6434 }
6435 }
6436 return pNew;
6437 }
6438
6439 /* Destroy an sqlite3_value object previously obtained from
6440 ** sqlite3_value_dup().
6441 */
6442 SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value *pOld){
6443 sqlite3ValueFree(pOld);
6444 }
6445
6446
6447 /**************************** sqlite3_result_ *******************************
6448 ** The following routines are used by user-defined functions to specify
6449 ** the function result.
6450 **
6451 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
6452 ** result as a string or blob but if the string or blob is too large, it
6453 ** then sets the error code to SQLITE_TOOBIG
6454 **
6455 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
6456 ** on value P is not going to be used and need to be destroyed.
6457 */
6458 static void setResultStrOrError(
6459 sqlite3_context *pCtx, /* Function context */
6460 const char *z, /* String pointer */
6461 int n, /* Bytes in string, or negative */
6462 u8 enc, /* Encoding of z. 0 for BLOBs */
6463 void (*xDel)(void*) /* Destructor function */
6464 ){
6465 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
6466 sqlite3_result_error_toobig(pCtx);
6467 }
6468 }
6469 static int invokeValueDestructor(
6470 const void *p, /* Value to destroy */
6471 void (*xDel)(void*), /* The destructor */
6472 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
6473 ){
6474 assert( xDel!=SQLITE_DYNAMIC );
6475 if( xDel==0 ){
6476 /* noop */
6477 }else if( xDel==SQLITE_TRANSIENT ){
6478 /* noop */
6479 }else{
6480 xDel((void*)p);
6481 }
6482 if( pCtx ) sqlite3_result_error_toobig(pCtx);
6483 return SQLITE_TOOBIG;
6484 }
6485 SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(
6486 sqlite3_context *pCtx,
6487 const void *z,
6488 int n,
6489 void (*xDel)(void *)
6490 ){
6491 assert( n>=0 );
6492 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6493 setResultStrOrError(pCtx, z, n, 0, xDel);
6494 }
6495 SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(
6496 sqlite3_context *pCtx,
6497 const void *z,
6498 sqlite3_uint64 n,
6499 void (*xDel)(void *)
6500 ){
6501 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6502 assert( xDel!=SQLITE_DYNAMIC );
6503 if( n>0x7fffffff ){
6504 (void)invokeValueDestructor(z, xDel, pCtx);
6505 }else{
6506 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
6507 }
6508 }
6509 SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context *pCtx, doub le rVal){
6510 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6511 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
6512 }
6513 SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
6514 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6515 pCtx->isError = SQLITE_ERROR;
6516 pCtx->fErrorOrAux = 1;
6517 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
6518 }
6519 #ifndef SQLITE_OMIT_UTF16
6520 SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context *pCtx, con st void *z, int n){
6521 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6522 pCtx->isError = SQLITE_ERROR;
6523 pCtx->fErrorOrAux = 1;
6524 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
6525 }
6526 #endif
6527 SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context *pCtx, int iVa l){
6528 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6529 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
6530 }
6531 SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context *pCtx, i64 i Val){
6532 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6533 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
6534 }
6535 SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
6536 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6537 sqlite3VdbeMemSetNull(pCtx->pOut);
6538 }
6539 SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context *pCtx, uns igned int eSubtype){
6540 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6541 pCtx->pOut->eSubtype = eSubtype & 0xff;
6542 }
6543 SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
6544 sqlite3_context *pCtx,
6545 const char *z,
6546 int n,
6547 void (*xDel)(void *)
6548 ){
6549 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6550 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
6551 }
6552 SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(
6553 sqlite3_context *pCtx,
6554 const char *z,
6555 sqlite3_uint64 n,
6556 void (*xDel)(void *),
6557 unsigned char enc
6558 ){
6559 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6560 assert( xDel!=SQLITE_DYNAMIC );
6561 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
6562 if( n>0x7fffffff ){
6563 (void)invokeValueDestructor(z, xDel, pCtx);
6564 }else{
6565 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
6566 }
6567 }
6568 #ifndef SQLITE_OMIT_UTF16
6569 SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(
6570 sqlite3_context *pCtx,
6571 const void *z,
6572 int n,
6573 void (*xDel)(void *)
6574 ){
6575 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6576 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
6577 }
6578 SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(
6579 sqlite3_context *pCtx,
6580 const void *z,
6581 int n,
6582 void (*xDel)(void *)
6583 ){
6584 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6585 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
6586 }
6587 SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(
6588 sqlite3_context *pCtx,
6589 const void *z,
6590 int n,
6591 void (*xDel)(void *)
6592 ){
6593 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6594 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
6595 }
6596 #endif /* SQLITE_OMIT_UTF16 */
6597 SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context *pCtx, sqlit e3_value *pValue){
6598 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6599 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
6600 }
6601 SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context *pCtx, in t n){
6602 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6603 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
6604 }
6605 SQLITE_API int SQLITE_STDCALL sqlite3_result_zeroblob64(sqlite3_context *pCtx, u 64 n){
6606 Mem *pOut = pCtx->pOut;
6607 assert( sqlite3_mutex_held(pOut->db->mutex) );
6608 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
6609 return SQLITE_TOOBIG;
6610 }
6611 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
6612 return SQLITE_OK;
6613 }
6614 SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
6615 pCtx->isError = errCode;
6616 pCtx->fErrorOrAux = 1;
6617 #ifdef SQLITE_DEBUG
6618 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
6619 #endif
6620 if( pCtx->pOut->flags & MEM_Null ){
6621 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
6622 SQLITE_UTF8, SQLITE_STATIC);
6623 }
6624 }
6625
6626 /* Force an SQLITE_TOOBIG error. */
6627 SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context *pCtx ){
6628 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6629 pCtx->isError = SQLITE_TOOBIG;
6630 pCtx->fErrorOrAux = 1;
6631 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
6632 SQLITE_UTF8, SQLITE_STATIC);
6633 }
6634
6635 /* An SQLITE_NOMEM error. */
6636 SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx) {
6637 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6638 sqlite3VdbeMemSetNull(pCtx->pOut);
6639 pCtx->isError = SQLITE_NOMEM;
6640 pCtx->fErrorOrAux = 1;
6641 pCtx->pOut->db->mallocFailed = 1;
6642 }
6643
6644 /*
6645 ** This function is called after a transaction has been committed. It
6646 ** invokes callbacks registered with sqlite3_wal_hook() as required.
6647 */
6648 static int doWalCallbacks(sqlite3 *db){
6649 int rc = SQLITE_OK;
6650 #ifndef SQLITE_OMIT_WAL
6651 int i;
6652 for(i=0; i<db->nDb; i++){
6653 Btree *pBt = db->aDb[i].pBt;
6654 if( pBt ){
6655 int nEntry;
6656 sqlite3BtreeEnter(pBt);
6657 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
6658 sqlite3BtreeLeave(pBt);
6659 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
6660 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
6661 }
6662 }
6663 }
6664 #endif
6665 return rc;
6666 }
6667
6668
6669 /*
6670 ** Execute the statement pStmt, either until a row of data is ready, the
6671 ** statement is completely executed or an error occurs.
6672 **
6673 ** This routine implements the bulk of the logic behind the sqlite_step()
6674 ** API. The only thing omitted is the automatic recompile if a
6675 ** schema change has occurred. That detail is handled by the
6676 ** outer sqlite3_step() wrapper procedure.
6677 */
6678 static int sqlite3Step(Vdbe *p){
6679 sqlite3 *db;
6680 int rc;
6681
6682 assert(p);
6683 if( p->magic!=VDBE_MAGIC_RUN ){
6684 /* We used to require that sqlite3_reset() be called before retrying
6685 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
6686 ** with version 3.7.0, we changed this so that sqlite3_reset() would
6687 ** be called automatically instead of throwing the SQLITE_MISUSE error.
6688 ** This "automatic-reset" change is not technically an incompatibility,
6689 ** since any application that receives an SQLITE_MISUSE is broken by
6690 ** definition.
6691 **
6692 ** Nevertheless, some published applications that were originally written
6693 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
6694 ** returns, and those were broken by the automatic-reset change. As a
6695 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
6696 ** legacy behavior of returning SQLITE_MISUSE for cases where the
6697 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
6698 ** or SQLITE_BUSY error.
6699 */
6700 #ifdef SQLITE_OMIT_AUTORESET
6701 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
6702 sqlite3_reset((sqlite3_stmt*)p);
6703 }else{
6704 return SQLITE_MISUSE_BKPT;
6705 }
6706 #else
6707 sqlite3_reset((sqlite3_stmt*)p);
6708 #endif
6709 }
6710
6711 /* Check that malloc() has not failed. If it has, return early. */
6712 db = p->db;
6713 if( db->mallocFailed ){
6714 p->rc = SQLITE_NOMEM;
6715 return SQLITE_NOMEM;
6716 }
6717
6718 if( p->pc<=0 && p->expired ){
6719 p->rc = SQLITE_SCHEMA;
6720 rc = SQLITE_ERROR;
6721 goto end_of_step;
6722 }
6723 if( p->pc<0 ){
6724 /* If there are no other statements currently running, then
6725 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
6726 ** from interrupting a statement that has not yet started.
6727 */
6728 if( db->nVdbeActive==0 ){
6729 db->u1.isInterrupted = 0;
6730 }
6731
6732 assert( db->nVdbeWrite>0 || db->autoCommit==0
6733 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
6734 );
6735
6736 #ifndef SQLITE_OMIT_TRACE
6737 if( db->xProfile && !db->init.busy && p->zSql ){
6738 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
6739 }else{
6740 assert( p->startTime==0 );
6741 }
6742 #endif
6743
6744 db->nVdbeActive++;
6745 if( p->readOnly==0 ) db->nVdbeWrite++;
6746 if( p->bIsReader ) db->nVdbeRead++;
6747 p->pc = 0;
6748 }
6749 #ifdef SQLITE_DEBUG
6750 p->rcApp = SQLITE_OK;
6751 #endif
6752 #ifndef SQLITE_OMIT_EXPLAIN
6753 if( p->explain ){
6754 rc = sqlite3VdbeList(p);
6755 }else
6756 #endif /* SQLITE_OMIT_EXPLAIN */
6757 {
6758 db->nVdbeExec++;
6759 rc = sqlite3VdbeExec(p);
6760 db->nVdbeExec--;
6761 }
6762
6763 #ifndef SQLITE_OMIT_TRACE
6764 /* If the statement completed successfully, invoke the profile callback */
6765 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
6766 #endif
6767
6768 if( rc==SQLITE_DONE ){
6769 assert( p->rc==SQLITE_OK );
6770 p->rc = doWalCallbacks(db);
6771 if( p->rc!=SQLITE_OK ){
6772 rc = SQLITE_ERROR;
6773 }
6774 }
6775
6776 db->errCode = rc;
6777 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
6778 p->rc = SQLITE_NOMEM;
6779 }
6780 end_of_step:
6781 /* At this point local variable rc holds the value that should be
6782 ** returned if this statement was compiled using the legacy
6783 ** sqlite3_prepare() interface. According to the docs, this can only
6784 ** be one of the values in the first assert() below. Variable p->rc
6785 ** contains the value that would be returned if sqlite3_finalize()
6786 ** were called on statement p.
6787 */
6788 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
6789 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
6790 );
6791 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
6792 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
6793 /* If this statement was prepared using sqlite3_prepare_v2(), and an
6794 ** error has occurred, then return the error code in p->rc to the
6795 ** caller. Set the error code in the database handle to the same value.
6796 */
6797 rc = sqlite3VdbeTransferError(p);
6798 }
6799 return (rc&db->errMask);
6800 }
6801
6802 /*
6803 ** This is the top-level implementation of sqlite3_step(). Call
6804 ** sqlite3Step() to do most of the work. If a schema error occurs,
6805 ** call sqlite3Reprepare() and try again.
6806 */
6807 SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){
6808 int rc = SQLITE_OK; /* Result from sqlite3Step() */
6809 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
6810 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
6811 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
6812 sqlite3 *db; /* The database connection */
6813
6814 if( vdbeSafetyNotNull(v) ){
6815 return SQLITE_MISUSE_BKPT;
6816 }
6817 db = v->db;
6818 sqlite3_mutex_enter(db->mutex);
6819 v->doingRerun = 0;
6820 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
6821 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
6822 int savedPc = v->pc;
6823 rc2 = rc = sqlite3Reprepare(v);
6824 if( rc!=SQLITE_OK) break;
6825 sqlite3_reset(pStmt);
6826 if( savedPc>=0 ) v->doingRerun = 1;
6827 assert( v->expired==0 );
6828 }
6829 if( rc2!=SQLITE_OK ){
6830 /* This case occurs after failing to recompile an sql statement.
6831 ** The error message from the SQL compiler has already been loaded
6832 ** into the database handle. This block copies the error message
6833 ** from the database handle into the statement and sets the statement
6834 ** program counter to 0 to ensure that when the statement is
6835 ** finalized or reset the parser error message is available via
6836 ** sqlite3_errmsg() and sqlite3_errcode().
6837 */
6838 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
6839 sqlite3DbFree(db, v->zErrMsg);
6840 if( !db->mallocFailed ){
6841 v->zErrMsg = sqlite3DbStrDup(db, zErr);
6842 v->rc = rc2;
6843 } else {
6844 v->zErrMsg = 0;
6845 v->rc = rc = SQLITE_NOMEM;
6846 }
6847 }
6848 rc = sqlite3ApiExit(db, rc);
6849 sqlite3_mutex_leave(db->mutex);
6850 return rc;
6851 }
6852
6853
6854 /*
6855 ** Extract the user data from a sqlite3_context structure and return a
6856 ** pointer to it.
6857 */
6858 SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context *p){
6859 assert( p && p->pFunc );
6860 return p->pFunc->pUserData;
6861 }
6862
6863 /*
6864 ** Extract the user data from a sqlite3_context structure and return a
6865 ** pointer to it.
6866 **
6867 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
6868 ** returns a copy of the pointer to the database connection (the 1st
6869 ** parameter) of the sqlite3_create_function() and
6870 ** sqlite3_create_function16() routines that originally registered the
6871 ** application defined function.
6872 */
6873 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context *p) {
6874 assert( p && p->pOut );
6875 return p->pOut->db;
6876 }
6877
6878 /*
6879 ** Return the current time for a statement. If the current time
6880 ** is requested more than once within the same run of a single prepared
6881 ** statement, the exact same time is returned for each invocation regardless
6882 ** of the amount of time that elapses between invocations. In other words,
6883 ** the time returned is always the time of the first call.
6884 */
6885 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
6886 int rc;
6887 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
6888 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
6889 assert( p->pVdbe!=0 );
6890 #else
6891 sqlite3_int64 iTime = 0;
6892 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
6893 #endif
6894 if( *piTime==0 ){
6895 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
6896 if( rc ) *piTime = 0;
6897 }
6898 return *piTime;
6899 }
6900
6901 /*
6902 ** The following is the implementation of an SQL function that always
6903 ** fails with an error message stating that the function is used in the
6904 ** wrong context. The sqlite3_overload_function() API might construct
6905 ** SQL function that use this routine so that the functions will exist
6906 ** for name resolution but are actually overloaded by the xFindFunction
6907 ** method of virtual tables.
6908 */
6909 SQLITE_PRIVATE void sqlite3InvalidFunction(
6910 sqlite3_context *context, /* The function calling context */
6911 int NotUsed, /* Number of arguments to the function */
6912 sqlite3_value **NotUsed2 /* Value of each argument */
6913 ){
6914 const char *zName = context->pFunc->zName;
6915 char *zErr;
6916 UNUSED_PARAMETER2(NotUsed, NotUsed2);
6917 zErr = sqlite3_mprintf(
6918 "unable to use function %s in the requested context", zName);
6919 sqlite3_result_error(context, zErr, -1);
6920 sqlite3_free(zErr);
6921 }
6922
6923 /*
6924 ** Create a new aggregate context for p and return a pointer to
6925 ** its pMem->z element.
6926 */
6927 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
6928 Mem *pMem = p->pMem;
6929 assert( (pMem->flags & MEM_Agg)==0 );
6930 if( nByte<=0 ){
6931 sqlite3VdbeMemSetNull(pMem);
6932 pMem->z = 0;
6933 }else{
6934 sqlite3VdbeMemClearAndResize(pMem, nByte);
6935 pMem->flags = MEM_Agg;
6936 pMem->u.pDef = p->pFunc;
6937 if( pMem->z ){
6938 memset(pMem->z, 0, nByte);
6939 }
6940 }
6941 return (void*)pMem->z;
6942 }
6943
6944 /*
6945 ** Allocate or return the aggregate context for a user function. A new
6946 ** context is allocated on the first call. Subsequent calls return the
6947 ** same context that was returned on prior calls.
6948 */
6949 SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, in t nByte){
6950 assert( p && p->pFunc && p->pFunc->xStep );
6951 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
6952 testcase( nByte<0 );
6953 if( (p->pMem->flags & MEM_Agg)==0 ){
6954 return createAggContext(p, nByte);
6955 }else{
6956 return (void*)p->pMem->z;
6957 }
6958 }
6959
6960 /*
6961 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
6962 ** the user-function defined by pCtx.
6963 */
6964 SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context *pCtx, int i Arg){
6965 AuxData *pAuxData;
6966
6967 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6968 #if SQLITE_ENABLE_STAT3_OR_STAT4
6969 if( pCtx->pVdbe==0 ) return 0;
6970 #else
6971 assert( pCtx->pVdbe!=0 );
6972 #endif
6973 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
6974 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
6975 }
6976
6977 return (pAuxData ? pAuxData->pAux : 0);
6978 }
6979
6980 /*
6981 ** Set the auxiliary data pointer and delete function, for the iArg'th
6982 ** argument to the user-function defined by pCtx. Any previous value is
6983 ** deleted by calling the delete function specified when it was set.
6984 */
6985 SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(
6986 sqlite3_context *pCtx,
6987 int iArg,
6988 void *pAux,
6989 void (*xDelete)(void*)
6990 ){
6991 AuxData *pAuxData;
6992 Vdbe *pVdbe = pCtx->pVdbe;
6993
6994 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6995 if( iArg<0 ) goto failed;
6996 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
6997 if( pVdbe==0 ) goto failed;
6998 #else
6999 assert( pVdbe!=0 );
7000 #endif
7001
7002 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
7003 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
7004 }
7005 if( pAuxData==0 ){
7006 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
7007 if( !pAuxData ) goto failed;
7008 pAuxData->iOp = pCtx->iOp;
7009 pAuxData->iArg = iArg;
7010 pAuxData->pNext = pVdbe->pAuxData;
7011 pVdbe->pAuxData = pAuxData;
7012 if( pCtx->fErrorOrAux==0 ){
7013 pCtx->isError = 0;
7014 pCtx->fErrorOrAux = 1;
7015 }
7016 }else if( pAuxData->xDelete ){
7017 pAuxData->xDelete(pAuxData->pAux);
7018 }
7019
7020 pAuxData->pAux = pAux;
7021 pAuxData->xDelete = xDelete;
7022 return;
7023
7024 failed:
7025 if( xDelete ){
7026 xDelete(pAux);
7027 }
7028 }
7029
7030 #ifndef SQLITE_OMIT_DEPRECATED
7031 /*
7032 ** Return the number of times the Step function of an aggregate has been
7033 ** called.
7034 **
7035 ** This function is deprecated. Do not use it for new code. It is
7036 ** provide only to avoid breaking legacy code. New aggregate function
7037 ** implementations should keep their own counts within their aggregate
7038 ** context.
7039 */
7040 SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){
7041 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
7042 return p->pMem->n;
7043 }
7044 #endif
7045
7046 /*
7047 ** Return the number of columns in the result set for the statement pStmt.
7048 */
7049 SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt){
7050 Vdbe *pVm = (Vdbe *)pStmt;
7051 return pVm ? pVm->nResColumn : 0;
7052 }
7053
7054 /*
7055 ** Return the number of values available from the current row of the
7056 ** currently executing statement pStmt.
7057 */
7058 SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt){
7059 Vdbe *pVm = (Vdbe *)pStmt;
7060 if( pVm==0 || pVm->pResultSet==0 ) return 0;
7061 return pVm->nResColumn;
7062 }
7063
7064 /*
7065 ** Return a pointer to static memory containing an SQL NULL value.
7066 */
7067 static const Mem *columnNullValue(void){
7068 /* Even though the Mem structure contains an element
7069 ** of type i64, on certain architectures (x86) with certain compiler
7070 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
7071 ** instead of an 8-byte one. This all works fine, except that when
7072 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
7073 ** that a Mem structure is located on an 8-byte boundary. To prevent
7074 ** these assert()s from failing, when building with SQLITE_DEBUG defined
7075 ** using gcc, we force nullMem to be 8-byte aligned using the magical
7076 ** __attribute__((aligned(8))) macro. */
7077 static const Mem nullMem
7078 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
7079 __attribute__((aligned(8)))
7080 #endif
7081 = {
7082 /* .u = */ {0},
7083 /* .flags = */ (u16)MEM_Null,
7084 /* .enc = */ (u8)0,
7085 /* .eSubtype = */ (u8)0,
7086 /* .n = */ (int)0,
7087 /* .z = */ (char*)0,
7088 /* .zMalloc = */ (char*)0,
7089 /* .szMalloc = */ (int)0,
7090 /* .uTemp = */ (u32)0,
7091 /* .db = */ (sqlite3*)0,
7092 /* .xDel = */ (void(*)(void*))0,
7093 #ifdef SQLITE_DEBUG
7094 /* .pScopyFrom = */ (Mem*)0,
7095 /* .pFiller = */ (void*)0,
7096 #endif
7097 };
7098 return &nullMem;
7099 }
7100
7101 /*
7102 ** Check to see if column iCol of the given statement is valid. If
7103 ** it is, return a pointer to the Mem for the value of that column.
7104 ** If iCol is not valid, return a pointer to a Mem which has a value
7105 ** of NULL.
7106 */
7107 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
7108 Vdbe *pVm;
7109 Mem *pOut;
7110
7111 pVm = (Vdbe *)pStmt;
7112 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
7113 sqlite3_mutex_enter(pVm->db->mutex);
7114 pOut = &pVm->pResultSet[i];
7115 }else{
7116 if( pVm && ALWAYS(pVm->db) ){
7117 sqlite3_mutex_enter(pVm->db->mutex);
7118 sqlite3Error(pVm->db, SQLITE_RANGE);
7119 }
7120 pOut = (Mem*)columnNullValue();
7121 }
7122 return pOut;
7123 }
7124
7125 /*
7126 ** This function is called after invoking an sqlite3_value_XXX function on a
7127 ** column value (i.e. a value returned by evaluating an SQL expression in the
7128 ** select list of a SELECT statement) that may cause a malloc() failure. If
7129 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
7130 ** code of statement pStmt set to SQLITE_NOMEM.
7131 **
7132 ** Specifically, this is called from within:
7133 **
7134 ** sqlite3_column_int()
7135 ** sqlite3_column_int64()
7136 ** sqlite3_column_text()
7137 ** sqlite3_column_text16()
7138 ** sqlite3_column_real()
7139 ** sqlite3_column_bytes()
7140 ** sqlite3_column_bytes16()
7141 ** sqiite3_column_blob()
7142 */
7143 static void columnMallocFailure(sqlite3_stmt *pStmt)
7144 {
7145 /* If malloc() failed during an encoding conversion within an
7146 ** sqlite3_column_XXX API, then set the return code of the statement to
7147 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
7148 ** and _finalize() will return NOMEM.
7149 */
7150 Vdbe *p = (Vdbe *)pStmt;
7151 if( p ){
7152 p->rc = sqlite3ApiExit(p->db, p->rc);
7153 sqlite3_mutex_leave(p->db->mutex);
7154 }
7155 }
7156
7157 /**************************** sqlite3_column_ *******************************
7158 ** The following routines are used to access elements of the current row
7159 ** in the result set.
7160 */
7161 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt *pStmt, i nt i){
7162 const void *val;
7163 val = sqlite3_value_blob( columnMem(pStmt,i) );
7164 /* Even though there is no encoding conversion, value_blob() might
7165 ** need to call malloc() to expand the result of a zeroblob()
7166 ** expression.
7167 */
7168 columnMallocFailure(pStmt);
7169 return val;
7170 }
7171 SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
7172 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
7173 columnMallocFailure(pStmt);
7174 return val;
7175 }
7176 SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i) {
7177 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
7178 columnMallocFailure(pStmt);
7179 return val;
7180 }
7181 SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt *pStmt, int i){
7182 double val = sqlite3_value_double( columnMem(pStmt,i) );
7183 columnMallocFailure(pStmt);
7184 return val;
7185 }
7186 SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt *pStmt, int i){
7187 int val = sqlite3_value_int( columnMem(pStmt,i) );
7188 columnMallocFailure(pStmt);
7189 return val;
7190 }
7191 SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
7192 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
7193 columnMallocFailure(pStmt);
7194 return val;
7195 }
7196 SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt *pStmt, int i){
7197 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
7198 columnMallocFailure(pStmt);
7199 return val;
7200 }
7201 SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt *pStm t, int i){
7202 Mem *pOut = columnMem(pStmt, i);
7203 if( pOut->flags&MEM_Static ){
7204 pOut->flags &= ~MEM_Static;
7205 pOut->flags |= MEM_Ephem;
7206 }
7207 columnMallocFailure(pStmt);
7208 return (sqlite3_value *)pOut;
7209 }
7210 #ifndef SQLITE_OMIT_UTF16
7211 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
7212 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
7213 columnMallocFailure(pStmt);
7214 return val;
7215 }
7216 #endif /* SQLITE_OMIT_UTF16 */
7217 SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt *pStmt, int i){
7218 int iType = sqlite3_value_type( columnMem(pStmt,i) );
7219 columnMallocFailure(pStmt);
7220 return iType;
7221 }
7222
7223 /*
7224 ** Convert the N-th element of pStmt->pColName[] into a string using
7225 ** xFunc() then return that string. If N is out of range, return 0.
7226 **
7227 ** There are up to 5 names for each column. useType determines which
7228 ** name is returned. Here are the names:
7229 **
7230 ** 0 The column name as it should be displayed for output
7231 ** 1 The datatype name for the column
7232 ** 2 The name of the database that the column derives from
7233 ** 3 The name of the table that the column derives from
7234 ** 4 The name of the table column that the result column derives from
7235 **
7236 ** If the result is not a simple column reference (if it is an expression
7237 ** or a constant) then useTypes 2, 3, and 4 return NULL.
7238 */
7239 static const void *columnName(
7240 sqlite3_stmt *pStmt,
7241 int N,
7242 const void *(*xFunc)(Mem*),
7243 int useType
7244 ){
7245 const void *ret;
7246 Vdbe *p;
7247 int n;
7248 sqlite3 *db;
7249 #ifdef SQLITE_ENABLE_API_ARMOR
7250 if( pStmt==0 ){
7251 (void)SQLITE_MISUSE_BKPT;
7252 return 0;
7253 }
7254 #endif
7255 ret = 0;
7256 p = (Vdbe *)pStmt;
7257 db = p->db;
7258 assert( db!=0 );
7259 n = sqlite3_column_count(pStmt);
7260 if( N<n && N>=0 ){
7261 N += useType*n;
7262 sqlite3_mutex_enter(db->mutex);
7263 assert( db->mallocFailed==0 );
7264 ret = xFunc(&p->aColName[N]);
7265 /* A malloc may have failed inside of the xFunc() call. If this
7266 ** is the case, clear the mallocFailed flag and return NULL.
7267 */
7268 if( db->mallocFailed ){
7269 db->mallocFailed = 0;
7270 ret = 0;
7271 }
7272 sqlite3_mutex_leave(db->mutex);
7273 }
7274 return ret;
7275 }
7276
7277 /*
7278 ** Return the name of the Nth column of the result set returned by SQL
7279 ** statement pStmt.
7280 */
7281 SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt *pStmt, i nt N){
7282 return columnName(
7283 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
7284 }
7285 #ifndef SQLITE_OMIT_UTF16
7286 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
7287 return columnName(
7288 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
7289 }
7290 #endif
7291
7292 /*
7293 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
7294 ** not define OMIT_DECLTYPE.
7295 */
7296 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
7297 # error "Must not define both SQLITE_OMIT_DECLTYPE \
7298 and SQLITE_ENABLE_COLUMN_METADATA"
7299 #endif
7300
7301 #ifndef SQLITE_OMIT_DECLTYPE
7302 /*
7303 ** Return the column declaration type (if applicable) of the 'i'th column
7304 ** of the result set of SQL statement pStmt.
7305 */
7306 SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt *pStm t, int N){
7307 return columnName(
7308 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
7309 }
7310 #ifndef SQLITE_OMIT_UTF16
7311 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt *pS tmt, int N){
7312 return columnName(
7313 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
7314 }
7315 #endif /* SQLITE_OMIT_UTF16 */
7316 #endif /* SQLITE_OMIT_DECLTYPE */
7317
7318 #ifdef SQLITE_ENABLE_COLUMN_METADATA
7319 /*
7320 ** Return the name of the database from which a result column derives.
7321 ** NULL is returned if the result column is an expression or constant or
7322 ** anything else which is not an unambiguous reference to a database column.
7323 */
7324 SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
7325 return columnName(
7326 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
7327 }
7328 #ifndef SQLITE_OMIT_UTF16
7329 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stm t *pStmt, int N){
7330 return columnName(
7331 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
7332 }
7333 #endif /* SQLITE_OMIT_UTF16 */
7334
7335 /*
7336 ** Return the name of the table from which a result column derives.
7337 ** NULL is returned if the result column is an expression or constant or
7338 ** anything else which is not an unambiguous reference to a database column.
7339 */
7340 SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt *pS tmt, int N){
7341 return columnName(
7342 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
7343 }
7344 #ifndef SQLITE_OMIT_UTF16
7345 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt * pStmt, int N){
7346 return columnName(
7347 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
7348 }
7349 #endif /* SQLITE_OMIT_UTF16 */
7350
7351 /*
7352 ** Return the name of the table column from which a result column derives.
7353 ** NULL is returned if the result column is an expression or constant or
7354 ** anything else which is not an unambiguous reference to a database column.
7355 */
7356 SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt *p Stmt, int N){
7357 return columnName(
7358 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
7359 }
7360 #ifndef SQLITE_OMIT_UTF16
7361 SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
7362 return columnName(
7363 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
7364 }
7365 #endif /* SQLITE_OMIT_UTF16 */
7366 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
7367
7368
7369 /******************************* sqlite3_bind_ ***************************
7370 **
7371 ** Routines used to attach values to wildcards in a compiled SQL statement.
7372 */
7373 /*
7374 ** Unbind the value bound to variable i in virtual machine p. This is the
7375 ** the same as binding a NULL value to the column. If the "i" parameter is
7376 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
7377 **
7378 ** A successful evaluation of this routine acquires the mutex on p.
7379 ** the mutex is released if any kind of error occurs.
7380 **
7381 ** The error code stored in database p->db is overwritten with the return
7382 ** value in any case.
7383 */
7384 static int vdbeUnbind(Vdbe *p, int i){
7385 Mem *pVar;
7386 if( vdbeSafetyNotNull(p) ){
7387 return SQLITE_MISUSE_BKPT;
7388 }
7389 sqlite3_mutex_enter(p->db->mutex);
7390 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
7391 sqlite3Error(p->db, SQLITE_MISUSE);
7392 sqlite3_mutex_leave(p->db->mutex);
7393 sqlite3_log(SQLITE_MISUSE,
7394 "bind on a busy prepared statement: [%s]", p->zSql);
7395 return SQLITE_MISUSE_BKPT;
7396 }
7397 if( i<1 || i>p->nVar ){
7398 sqlite3Error(p->db, SQLITE_RANGE);
7399 sqlite3_mutex_leave(p->db->mutex);
7400 return SQLITE_RANGE;
7401 }
7402 i--;
7403 pVar = &p->aVar[i];
7404 sqlite3VdbeMemRelease(pVar);
7405 pVar->flags = MEM_Null;
7406 sqlite3Error(p->db, SQLITE_OK);
7407
7408 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
7409 ** binding a new value to this variable invalidates the current query plan.
7410 **
7411 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
7412 ** parameter in the WHERE clause might influence the choice of query plan
7413 ** for a statement, then the statement will be automatically recompiled,
7414 ** as if there had been a schema change, on the first sqlite3_step() call
7415 ** following any change to the bindings of that parameter.
7416 */
7417 if( p->isPrepareV2 &&
7418 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
7419 ){
7420 p->expired = 1;
7421 }
7422 return SQLITE_OK;
7423 }
7424
7425 /*
7426 ** Bind a text or BLOB value.
7427 */
7428 static int bindText(
7429 sqlite3_stmt *pStmt, /* The statement to bind against */
7430 int i, /* Index of the parameter to bind */
7431 const void *zData, /* Pointer to the data to be bound */
7432 int nData, /* Number of bytes of data to be bound */
7433 void (*xDel)(void*), /* Destructor for the data */
7434 u8 encoding /* Encoding for the data */
7435 ){
7436 Vdbe *p = (Vdbe *)pStmt;
7437 Mem *pVar;
7438 int rc;
7439
7440 rc = vdbeUnbind(p, i);
7441 if( rc==SQLITE_OK ){
7442 if( zData!=0 ){
7443 pVar = &p->aVar[i-1];
7444 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
7445 if( rc==SQLITE_OK && encoding!=0 ){
7446 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
7447 }
7448 sqlite3Error(p->db, rc);
7449 rc = sqlite3ApiExit(p->db, rc);
7450 }
7451 sqlite3_mutex_leave(p->db->mutex);
7452 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
7453 xDel((void*)zData);
7454 }
7455 return rc;
7456 }
7457
7458
7459 /*
7460 ** Bind a blob value to an SQL statement variable.
7461 */
7462 SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(
7463 sqlite3_stmt *pStmt,
7464 int i,
7465 const void *zData,
7466 int nData,
7467 void (*xDel)(void*)
7468 ){
7469 return bindText(pStmt, i, zData, nData, xDel, 0);
7470 }
7471 SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(
7472 sqlite3_stmt *pStmt,
7473 int i,
7474 const void *zData,
7475 sqlite3_uint64 nData,
7476 void (*xDel)(void*)
7477 ){
7478 assert( xDel!=SQLITE_DYNAMIC );
7479 if( nData>0x7fffffff ){
7480 return invokeValueDestructor(zData, xDel, 0);
7481 }else{
7482 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
7483 }
7484 }
7485 SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt *pStmt, int i, do uble rValue){
7486 int rc;
7487 Vdbe *p = (Vdbe *)pStmt;
7488 rc = vdbeUnbind(p, i);
7489 if( rc==SQLITE_OK ){
7490 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
7491 sqlite3_mutex_leave(p->db->mutex);
7492 }
7493 return rc;
7494 }
7495 SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt *p, int i, int iValu e){
7496 return sqlite3_bind_int64(p, i, (i64)iValue);
7497 }
7498 SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sql ite_int64 iValue){
7499 int rc;
7500 Vdbe *p = (Vdbe *)pStmt;
7501 rc = vdbeUnbind(p, i);
7502 if( rc==SQLITE_OK ){
7503 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
7504 sqlite3_mutex_leave(p->db->mutex);
7505 }
7506 return rc;
7507 }
7508 SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
7509 int rc;
7510 Vdbe *p = (Vdbe*)pStmt;
7511 rc = vdbeUnbind(p, i);
7512 if( rc==SQLITE_OK ){
7513 sqlite3_mutex_leave(p->db->mutex);
7514 }
7515 return rc;
7516 }
7517 SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(
7518 sqlite3_stmt *pStmt,
7519 int i,
7520 const char *zData,
7521 int nData,
7522 void (*xDel)(void*)
7523 ){
7524 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
7525 }
7526 SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(
7527 sqlite3_stmt *pStmt,
7528 int i,
7529 const char *zData,
7530 sqlite3_uint64 nData,
7531 void (*xDel)(void*),
7532 unsigned char enc
7533 ){
7534 assert( xDel!=SQLITE_DYNAMIC );
7535 if( nData>0x7fffffff ){
7536 return invokeValueDestructor(zData, xDel, 0);
7537 }else{
7538 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
7539 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
7540 }
7541 }
7542 #ifndef SQLITE_OMIT_UTF16
7543 SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(
7544 sqlite3_stmt *pStmt,
7545 int i,
7546 const void *zData,
7547 int nData,
7548 void (*xDel)(void*)
7549 ){
7550 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
7551 }
7552 #endif /* SQLITE_OMIT_UTF16 */
7553 SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt *pStmt, int i, con st sqlite3_value *pValue){
7554 int rc;
7555 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
7556 case SQLITE_INTEGER: {
7557 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
7558 break;
7559 }
7560 case SQLITE_FLOAT: {
7561 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
7562 break;
7563 }
7564 case SQLITE_BLOB: {
7565 if( pValue->flags & MEM_Zero ){
7566 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
7567 }else{
7568 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
7569 }
7570 break;
7571 }
7572 case SQLITE_TEXT: {
7573 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
7574 pValue->enc);
7575 break;
7576 }
7577 default: {
7578 rc = sqlite3_bind_null(pStmt, i);
7579 break;
7580 }
7581 }
7582 return rc;
7583 }
7584 SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
7585 int rc;
7586 Vdbe *p = (Vdbe *)pStmt;
7587 rc = vdbeUnbind(p, i);
7588 if( rc==SQLITE_OK ){
7589 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
7590 sqlite3_mutex_leave(p->db->mutex);
7591 }
7592 return rc;
7593 }
7594 SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i , sqlite3_uint64 n){
7595 int rc;
7596 Vdbe *p = (Vdbe *)pStmt;
7597 sqlite3_mutex_enter(p->db->mutex);
7598 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
7599 rc = SQLITE_TOOBIG;
7600 }else{
7601 assert( (n & 0x7FFFFFFF)==n );
7602 rc = sqlite3_bind_zeroblob(pStmt, i, n);
7603 }
7604 rc = sqlite3ApiExit(p->db, rc);
7605 sqlite3_mutex_leave(p->db->mutex);
7606 return rc;
7607 }
7608
7609 /*
7610 ** Return the number of wildcards that can be potentially bound to.
7611 ** This routine is added to support DBD::SQLite.
7612 */
7613 SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
7614 Vdbe *p = (Vdbe*)pStmt;
7615 return p ? p->nVar : 0;
7616 }
7617
7618 /*
7619 ** Return the name of a wildcard parameter. Return NULL if the index
7620 ** is out of range or if the wildcard is unnamed.
7621 **
7622 ** The result is always UTF-8.
7623 */
7624 SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt * pStmt, int i){
7625 Vdbe *p = (Vdbe*)pStmt;
7626 if( p==0 || i<1 || i>p->nzVar ){
7627 return 0;
7628 }
7629 return p->azVar[i-1];
7630 }
7631
7632 /*
7633 ** Given a wildcard parameter name, return the index of the variable
7634 ** with that name. If there is no variable with the given name,
7635 ** return 0.
7636 */
7637 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nNa me){
7638 int i;
7639 if( p==0 ){
7640 return 0;
7641 }
7642 if( zName ){
7643 for(i=0; i<p->nzVar; i++){
7644 const char *z = p->azVar[i];
7645 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
7646 return i+1;
7647 }
7648 }
7649 }
7650 return 0;
7651 }
7652 SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
7653 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
7654 }
7655
7656 /*
7657 ** Transfer all bindings from the first statement over to the second.
7658 */
7659 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
7660 Vdbe *pFrom = (Vdbe*)pFromStmt;
7661 Vdbe *pTo = (Vdbe*)pToStmt;
7662 int i;
7663 assert( pTo->db==pFrom->db );
7664 assert( pTo->nVar==pFrom->nVar );
7665 sqlite3_mutex_enter(pTo->db->mutex);
7666 for(i=0; i<pFrom->nVar; i++){
7667 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
7668 }
7669 sqlite3_mutex_leave(pTo->db->mutex);
7670 return SQLITE_OK;
7671 }
7672
7673 #ifndef SQLITE_OMIT_DEPRECATED
7674 /*
7675 ** Deprecated external interface. Internal/core SQLite code
7676 ** should call sqlite3TransferBindings.
7677 **
7678 ** It is misuse to call this routine with statements from different
7679 ** database connections. But as this is a deprecated interface, we
7680 ** will not bother to check for that condition.
7681 **
7682 ** If the two statements contain a different number of bindings, then
7683 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
7684 ** SQLITE_OK is returned.
7685 */
7686 SQLITE_API int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
7687 Vdbe *pFrom = (Vdbe*)pFromStmt;
7688 Vdbe *pTo = (Vdbe*)pToStmt;
7689 if( pFrom->nVar!=pTo->nVar ){
7690 return SQLITE_ERROR;
7691 }
7692 if( pTo->isPrepareV2 && pTo->expmask ){
7693 pTo->expired = 1;
7694 }
7695 if( pFrom->isPrepareV2 && pFrom->expmask ){
7696 pFrom->expired = 1;
7697 }
7698 return sqlite3TransferBindings(pFromStmt, pToStmt);
7699 }
7700 #endif
7701
7702 /*
7703 ** Return the sqlite3* database handle to which the prepared statement given
7704 ** in the argument belongs. This is the same database handle that was
7705 ** the first argument to the sqlite3_prepare() that was used to create
7706 ** the statement in the first place.
7707 */
7708 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt *pStmt){
7709 return pStmt ? ((Vdbe*)pStmt)->db : 0;
7710 }
7711
7712 /*
7713 ** Return true if the prepared statement is guaranteed to not modify the
7714 ** database.
7715 */
7716 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
7717 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
7718 }
7719
7720 /*
7721 ** Return true if the prepared statement is in need of being reset.
7722 */
7723 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt *pStmt){
7724 Vdbe *v = (Vdbe*)pStmt;
7725 return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
7726 }
7727
7728 /*
7729 ** Return a pointer to the next prepared statement after pStmt associated
7730 ** with database connection pDb. If pStmt is NULL, return the first
7731 ** prepared statement for the database connection. Return NULL if there
7732 ** are no more.
7733 */
7734 SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_ stmt *pStmt){
7735 sqlite3_stmt *pNext;
7736 #ifdef SQLITE_ENABLE_API_ARMOR
7737 if( !sqlite3SafetyCheckOk(pDb) ){
7738 (void)SQLITE_MISUSE_BKPT;
7739 return 0;
7740 }
7741 #endif
7742 sqlite3_mutex_enter(pDb->mutex);
7743 if( pStmt==0 ){
7744 pNext = (sqlite3_stmt*)pDb->pVdbe;
7745 }else{
7746 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
7747 }
7748 sqlite3_mutex_leave(pDb->mutex);
7749 return pNext;
7750 }
7751
7752 /*
7753 ** Return the value of a status counter for a prepared statement
7754 */
7755 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, i nt resetFlag){
7756 Vdbe *pVdbe = (Vdbe*)pStmt;
7757 u32 v;
7758 #ifdef SQLITE_ENABLE_API_ARMOR
7759 if( !pStmt ){
7760 (void)SQLITE_MISUSE_BKPT;
7761 return 0;
7762 }
7763 #endif
7764 v = pVdbe->aCounter[op];
7765 if( resetFlag ) pVdbe->aCounter[op] = 0;
7766 return (int)v;
7767 }
7768
7769 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
7770 /*
7771 ** Return status data for a single loop within query pStmt.
7772 */
7773 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7774 sqlite3_stmt *pStmt, /* Prepared statement being queried */
7775 int idx, /* Index of loop to report on */
7776 int iScanStatusOp, /* Which metric to return */
7777 void *pOut /* OUT: Write the answer here */
7778 ){
7779 Vdbe *p = (Vdbe*)pStmt;
7780 ScanStatus *pScan;
7781 if( idx<0 || idx>=p->nScan ) return 1;
7782 pScan = &p->aScan[idx];
7783 switch( iScanStatusOp ){
7784 case SQLITE_SCANSTAT_NLOOP: {
7785 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
7786 break;
7787 }
7788 case SQLITE_SCANSTAT_NVISIT: {
7789 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
7790 break;
7791 }
7792 case SQLITE_SCANSTAT_EST: {
7793 double r = 1.0;
7794 LogEst x = pScan->nEst;
7795 while( x<100 ){
7796 x += 10;
7797 r *= 0.5;
7798 }
7799 *(double*)pOut = r*sqlite3LogEstToInt(x);
7800 break;
7801 }
7802 case SQLITE_SCANSTAT_NAME: {
7803 *(const char**)pOut = pScan->zName;
7804 break;
7805 }
7806 case SQLITE_SCANSTAT_EXPLAIN: {
7807 if( pScan->addrExplain ){
7808 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
7809 }else{
7810 *(const char**)pOut = 0;
7811 }
7812 break;
7813 }
7814 case SQLITE_SCANSTAT_SELECTID: {
7815 if( pScan->addrExplain ){
7816 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
7817 }else{
7818 *(int*)pOut = -1;
7819 }
7820 break;
7821 }
7822 default: {
7823 return 1;
7824 }
7825 }
7826 return 0;
7827 }
7828
7829 /*
7830 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
7831 */
7832 SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt ){
7833 Vdbe *p = (Vdbe*)pStmt;
7834 memset(p->anExec, 0, p->nOp * sizeof(i64));
7835 }
7836 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
7837
7838 /************** End of vdbeapi.c *********************************************/
7839 /************** Begin file vdbetrace.c ***************************************/
7840 /*
7841 ** 2009 November 25
7842 **
7843 ** The author disclaims copyright to this source code. In place of
7844 ** a legal notice, here is a blessing:
7845 **
7846 ** May you do good and not evil.
7847 ** May you find forgiveness for yourself and forgive others.
7848 ** May you share freely, never taking more than you give.
7849 **
7850 *************************************************************************
7851 **
7852 ** This file contains code used to insert the values of host parameters
7853 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
7854 **
7855 ** The Vdbe parse-tree explainer is also found here.
7856 */
7857 /* #include "sqliteInt.h" */
7858 /* #include "vdbeInt.h" */
7859
7860 #ifndef SQLITE_OMIT_TRACE
7861
7862 /*
7863 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
7864 ** bytes in this text up to but excluding the first character in
7865 ** a host parameter. If the text contains no host parameters, return
7866 ** the total number of bytes in the text.
7867 */
7868 static int findNextHostParameter(const char *zSql, int *pnToken){
7869 int tokenType;
7870 int nTotal = 0;
7871 int n;
7872
7873 *pnToken = 0;
7874 while( zSql[0] ){
7875 n = sqlite3GetToken((u8*)zSql, &tokenType);
7876 assert( n>0 && tokenType!=TK_ILLEGAL );
7877 if( tokenType==TK_VARIABLE ){
7878 *pnToken = n;
7879 break;
7880 }
7881 nTotal += n;
7882 zSql += n;
7883 }
7884 return nTotal;
7885 }
7886
7887 /*
7888 ** This function returns a pointer to a nul-terminated string in memory
7889 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
7890 ** string contains a copy of zRawSql but with host parameters expanded to
7891 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
7892 ** then the returned string holds a copy of zRawSql with "-- " prepended
7893 ** to each line of text.
7894 **
7895 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
7896 ** then long strings and blobs are truncated to that many bytes. This
7897 ** can be used to prevent unreasonably large trace strings when dealing
7898 ** with large (multi-megabyte) strings and blobs.
7899 **
7900 ** The calling function is responsible for making sure the memory returned
7901 ** is eventually freed.
7902 **
7903 ** ALGORITHM: Scan the input string looking for host parameters in any of
7904 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
7905 ** string literals, quoted identifier names, and comments. For text forms,
7906 ** the host parameter index is found by scanning the prepared
7907 ** statement for the corresponding OP_Variable opcode. Once the host
7908 ** parameter index is known, locate the value in p->aVar[]. Then render
7909 ** the value as a literal in place of the host parameter name.
7910 */
7911 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
7912 Vdbe *p, /* The prepared statement being evaluated */
7913 const char *zRawSql /* Raw text of the SQL statement */
7914 ){
7915 sqlite3 *db; /* The database connection */
7916 int idx = 0; /* Index of a host parameter */
7917 int nextIndex = 1; /* Index of next ? host parameter */
7918 int n; /* Length of a token prefix */
7919 int nToken; /* Length of the parameter token */
7920 int i; /* Loop counter */
7921 Mem *pVar; /* Value of a host parameter */
7922 StrAccum out; /* Accumulate the output here */
7923 char zBase[100]; /* Initial working space */
7924
7925 db = p->db;
7926 sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase),
7927 db->aLimit[SQLITE_LIMIT_LENGTH]);
7928 if( db->nVdbeExec>1 ){
7929 while( *zRawSql ){
7930 const char *zStart = zRawSql;
7931 while( *(zRawSql++)!='\n' && *zRawSql );
7932 sqlite3StrAccumAppend(&out, "-- ", 3);
7933 assert( (zRawSql - zStart) > 0 );
7934 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
7935 }
7936 }else if( p->nVar==0 ){
7937 sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
7938 }else{
7939 while( zRawSql[0] ){
7940 n = findNextHostParameter(zRawSql, &nToken);
7941 assert( n>0 );
7942 sqlite3StrAccumAppend(&out, zRawSql, n);
7943 zRawSql += n;
7944 assert( zRawSql[0] || nToken==0 );
7945 if( nToken==0 ) break;
7946 if( zRawSql[0]=='?' ){
7947 if( nToken>1 ){
7948 assert( sqlite3Isdigit(zRawSql[1]) );
7949 sqlite3GetInt32(&zRawSql[1], &idx);
7950 }else{
7951 idx = nextIndex;
7952 }
7953 }else{
7954 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
7955 zRawSql[0]=='@' || zRawSql[0]=='#' );
7956 testcase( zRawSql[0]==':' );
7957 testcase( zRawSql[0]=='$' );
7958 testcase( zRawSql[0]=='@' );
7959 testcase( zRawSql[0]=='#' );
7960 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
7961 assert( idx>0 );
7962 }
7963 zRawSql += nToken;
7964 nextIndex = idx + 1;
7965 assert( idx>0 && idx<=p->nVar );
7966 pVar = &p->aVar[idx-1];
7967 if( pVar->flags & MEM_Null ){
7968 sqlite3StrAccumAppend(&out, "NULL", 4);
7969 }else if( pVar->flags & MEM_Int ){
7970 sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
7971 }else if( pVar->flags & MEM_Real ){
7972 sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
7973 }else if( pVar->flags & MEM_Str ){
7974 int nOut; /* Number of bytes of the string text to include in output */
7975 #ifndef SQLITE_OMIT_UTF16
7976 u8 enc = ENC(db);
7977 Mem utf8;
7978 if( enc!=SQLITE_UTF8 ){
7979 memset(&utf8, 0, sizeof(utf8));
7980 utf8.db = db;
7981 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
7982 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
7983 pVar = &utf8;
7984 }
7985 #endif
7986 nOut = pVar->n;
7987 #ifdef SQLITE_TRACE_SIZE_LIMIT
7988 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
7989 nOut = SQLITE_TRACE_SIZE_LIMIT;
7990 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
7991 }
7992 #endif
7993 sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
7994 #ifdef SQLITE_TRACE_SIZE_LIMIT
7995 if( nOut<pVar->n ){
7996 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
7997 }
7998 #endif
7999 #ifndef SQLITE_OMIT_UTF16
8000 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
8001 #endif
8002 }else if( pVar->flags & MEM_Zero ){
8003 sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
8004 }else{
8005 int nOut; /* Number of bytes of the blob to include in output */
8006 assert( pVar->flags & MEM_Blob );
8007 sqlite3StrAccumAppend(&out, "x'", 2);
8008 nOut = pVar->n;
8009 #ifdef SQLITE_TRACE_SIZE_LIMIT
8010 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
8011 #endif
8012 for(i=0; i<nOut; i++){
8013 sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
8014 }
8015 sqlite3StrAccumAppend(&out, "'", 1);
8016 #ifdef SQLITE_TRACE_SIZE_LIMIT
8017 if( nOut<pVar->n ){
8018 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
8019 }
8020 #endif
8021 }
8022 }
8023 }
8024 return sqlite3StrAccumFinish(&out);
8025 }
8026
8027 #endif /* #ifndef SQLITE_OMIT_TRACE */
8028
8029 /************** End of vdbetrace.c *******************************************/
8030 /************** Begin file vdbe.c ********************************************/
8031 /*
8032 ** 2001 September 15
8033 **
8034 ** The author disclaims copyright to this source code. In place of
8035 ** a legal notice, here is a blessing:
8036 **
8037 ** May you do good and not evil.
8038 ** May you find forgiveness for yourself and forgive others.
8039 ** May you share freely, never taking more than you give.
8040 **
8041 *************************************************************************
8042 ** The code in this file implements the function that runs the
8043 ** bytecode of a prepared statement.
8044 **
8045 ** Various scripts scan this source file in order to generate HTML
8046 ** documentation, headers files, or other derived files. The formatting
8047 ** of the code in this file is, therefore, important. See other comments
8048 ** in this file for details. If in doubt, do not deviate from existing
8049 ** commenting and indentation practices when changing or adding code.
8050 */
8051 /* #include "sqliteInt.h" */
8052 /* #include "vdbeInt.h" */
8053
8054 /*
8055 ** Invoke this macro on memory cells just prior to changing the
8056 ** value of the cell. This macro verifies that shallow copies are
8057 ** not misused. A shallow copy of a string or blob just copies a
8058 ** pointer to the string or blob, not the content. If the original
8059 ** is changed while the copy is still in use, the string or blob might
8060 ** be changed out from under the copy. This macro verifies that nothing
8061 ** like that ever happens.
8062 */
8063 #ifdef SQLITE_DEBUG
8064 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
8065 #else
8066 # define memAboutToChange(P,M)
8067 #endif
8068
8069 /*
8070 ** The following global variable is incremented every time a cursor
8071 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
8072 ** procedures use this information to make sure that indices are
8073 ** working correctly. This variable has no function other than to
8074 ** help verify the correct operation of the library.
8075 */
8076 #ifdef SQLITE_TEST
8077 SQLITE_API int sqlite3_search_count = 0;
8078 #endif
8079
8080 /*
8081 ** When this global variable is positive, it gets decremented once before
8082 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
8083 ** field of the sqlite3 structure is set in order to simulate an interrupt.
8084 **
8085 ** This facility is used for testing purposes only. It does not function
8086 ** in an ordinary build.
8087 */
8088 #ifdef SQLITE_TEST
8089 SQLITE_API int sqlite3_interrupt_count = 0;
8090 #endif
8091
8092 /*
8093 ** The next global variable is incremented each type the OP_Sort opcode
8094 ** is executed. The test procedures use this information to make sure that
8095 ** sorting is occurring or not occurring at appropriate times. This variable
8096 ** has no function other than to help verify the correct operation of the
8097 ** library.
8098 */
8099 #ifdef SQLITE_TEST
8100 SQLITE_API int sqlite3_sort_count = 0;
8101 #endif
8102
8103 /*
8104 ** The next global variable records the size of the largest MEM_Blob
8105 ** or MEM_Str that has been used by a VDBE opcode. The test procedures
8106 ** use this information to make sure that the zero-blob functionality
8107 ** is working correctly. This variable has no function other than to
8108 ** help verify the correct operation of the library.
8109 */
8110 #ifdef SQLITE_TEST
8111 SQLITE_API int sqlite3_max_blobsize = 0;
8112 static void updateMaxBlobsize(Mem *p){
8113 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
8114 sqlite3_max_blobsize = p->n;
8115 }
8116 }
8117 #endif
8118
8119 /*
8120 ** The next global variable is incremented each time the OP_Found opcode
8121 ** is executed. This is used to test whether or not the foreign key
8122 ** operation implemented using OP_FkIsZero is working. This variable
8123 ** has no function other than to help verify the correct operation of the
8124 ** library.
8125 */
8126 #ifdef SQLITE_TEST
8127 SQLITE_API int sqlite3_found_count = 0;
8128 #endif
8129
8130 /*
8131 ** Test a register to see if it exceeds the current maximum blob size.
8132 ** If it does, record the new maximum blob size.
8133 */
8134 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
8135 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
8136 #else
8137 # define UPDATE_MAX_BLOBSIZE(P)
8138 #endif
8139
8140 /*
8141 ** Invoke the VDBE coverage callback, if that callback is defined. This
8142 ** feature is used for test suite validation only and does not appear an
8143 ** production builds.
8144 **
8145 ** M is an integer, 2 or 3, that indices how many different ways the
8146 ** branch can go. It is usually 2. "I" is the direction the branch
8147 ** goes. 0 means falls through. 1 means branch is taken. 2 means the
8148 ** second alternative branch is taken.
8149 **
8150 ** iSrcLine is the source code line (from the __LINE__ macro) that
8151 ** generated the VDBE instruction. This instrumentation assumes that all
8152 ** source code is in a single file (the amalgamation). Special values 1
8153 ** and 2 for the iSrcLine parameter mean that this particular branch is
8154 ** always taken or never taken, respectively.
8155 */
8156 #if !defined(SQLITE_VDBE_COVERAGE)
8157 # define VdbeBranchTaken(I,M)
8158 #else
8159 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
8160 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
8161 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
8162 M = iSrcLine;
8163 /* Assert the truth of VdbeCoverageAlwaysTaken() and
8164 ** VdbeCoverageNeverTaken() */
8165 assert( (M & I)==I );
8166 }else{
8167 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
8168 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
8169 iSrcLine,I,M);
8170 }
8171 }
8172 #endif
8173
8174 /*
8175 ** Convert the given register into a string if it isn't one
8176 ** already. Return non-zero if a malloc() fails.
8177 */
8178 #define Stringify(P, enc) \
8179 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
8180 { goto no_mem; }
8181
8182 /*
8183 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
8184 ** a pointer to a dynamically allocated string where some other entity
8185 ** is responsible for deallocating that string. Because the register
8186 ** does not control the string, it might be deleted without the register
8187 ** knowing it.
8188 **
8189 ** This routine converts an ephemeral string into a dynamically allocated
8190 ** string that the register itself controls. In other words, it
8191 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
8192 */
8193 #define Deephemeralize(P) \
8194 if( ((P)->flags&MEM_Ephem)!=0 \
8195 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
8196
8197 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
8198 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
8199
8200 /*
8201 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
8202 ** if we run out of memory.
8203 */
8204 static VdbeCursor *allocateCursor(
8205 Vdbe *p, /* The virtual machine */
8206 int iCur, /* Index of the new VdbeCursor */
8207 int nField, /* Number of fields in the table or index */
8208 int iDb, /* Database the cursor belongs to, or -1 */
8209 u8 eCurType /* Type of the new cursor */
8210 ){
8211 /* Find the memory cell that will be used to store the blob of memory
8212 ** required for this VdbeCursor structure. It is convenient to use a
8213 ** vdbe memory cell to manage the memory allocation required for a
8214 ** VdbeCursor structure for the following reasons:
8215 **
8216 ** * Sometimes cursor numbers are used for a couple of different
8217 ** purposes in a vdbe program. The different uses might require
8218 ** different sized allocations. Memory cells provide growable
8219 ** allocations.
8220 **
8221 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
8222 ** be freed lazily via the sqlite3_release_memory() API. This
8223 ** minimizes the number of malloc calls made by the system.
8224 **
8225 ** Memory cells for cursors are allocated at the top of the address
8226 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
8227 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
8228 */
8229 Mem *pMem = &p->aMem[p->nMem-iCur];
8230
8231 int nByte;
8232 VdbeCursor *pCx = 0;
8233 nByte =
8234 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
8235 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
8236
8237 assert( iCur<p->nCursor );
8238 if( p->apCsr[iCur] ){
8239 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
8240 p->apCsr[iCur] = 0;
8241 }
8242 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
8243 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
8244 memset(pCx, 0, sizeof(VdbeCursor));
8245 pCx->eCurType = eCurType;
8246 pCx->iDb = iDb;
8247 pCx->nField = nField;
8248 pCx->aOffset = &pCx->aType[nField];
8249 if( eCurType==CURTYPE_BTREE ){
8250 pCx->uc.pCursor = (BtCursor*)
8251 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
8252 sqlite3BtreeCursorZero(pCx->uc.pCursor);
8253 }
8254 }
8255 return pCx;
8256 }
8257
8258 /*
8259 ** Try to convert a value into a numeric representation if we can
8260 ** do so without loss of information. In other words, if the string
8261 ** looks like a number, convert it into a number. If it does not
8262 ** look like a number, leave it alone.
8263 **
8264 ** If the bTryForInt flag is true, then extra effort is made to give
8265 ** an integer representation. Strings that look like floating point
8266 ** values but which have no fractional component (example: '48.00')
8267 ** will have a MEM_Int representation when bTryForInt is true.
8268 **
8269 ** If bTryForInt is false, then if the input string contains a decimal
8270 ** point or exponential notation, the result is only MEM_Real, even
8271 ** if there is an exact integer representation of the quantity.
8272 */
8273 static void applyNumericAffinity(Mem *pRec, int bTryForInt){
8274 double rValue;
8275 i64 iValue;
8276 u8 enc = pRec->enc;
8277 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
8278 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
8279 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
8280 pRec->u.i = iValue;
8281 pRec->flags |= MEM_Int;
8282 }else{
8283 pRec->u.r = rValue;
8284 pRec->flags |= MEM_Real;
8285 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
8286 }
8287 }
8288
8289 /*
8290 ** Processing is determine by the affinity parameter:
8291 **
8292 ** SQLITE_AFF_INTEGER:
8293 ** SQLITE_AFF_REAL:
8294 ** SQLITE_AFF_NUMERIC:
8295 ** Try to convert pRec to an integer representation or a
8296 ** floating-point representation if an integer representation
8297 ** is not possible. Note that the integer representation is
8298 ** always preferred, even if the affinity is REAL, because
8299 ** an integer representation is more space efficient on disk.
8300 **
8301 ** SQLITE_AFF_TEXT:
8302 ** Convert pRec to a text representation.
8303 **
8304 ** SQLITE_AFF_BLOB:
8305 ** No-op. pRec is unchanged.
8306 */
8307 static void applyAffinity(
8308 Mem *pRec, /* The value to apply affinity to */
8309 char affinity, /* The affinity to be applied */
8310 u8 enc /* Use this text encoding */
8311 ){
8312 if( affinity>=SQLITE_AFF_NUMERIC ){
8313 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
8314 || affinity==SQLITE_AFF_NUMERIC );
8315 if( (pRec->flags & MEM_Int)==0 ){
8316 if( (pRec->flags & MEM_Real)==0 ){
8317 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
8318 }else{
8319 sqlite3VdbeIntegerAffinity(pRec);
8320 }
8321 }
8322 }else if( affinity==SQLITE_AFF_TEXT ){
8323 /* Only attempt the conversion to TEXT if there is an integer or real
8324 ** representation (blob and NULL do not get converted) but no string
8325 ** representation.
8326 */
8327 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
8328 sqlite3VdbeMemStringify(pRec, enc, 1);
8329 }
8330 pRec->flags &= ~(MEM_Real|MEM_Int);
8331 }
8332 }
8333
8334 /*
8335 ** Try to convert the type of a function argument or a result column
8336 ** into a numeric representation. Use either INTEGER or REAL whichever
8337 ** is appropriate. But only do the conversion if it is possible without
8338 ** loss of information and return the revised type of the argument.
8339 */
8340 SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value *pVal){
8341 int eType = sqlite3_value_type(pVal);
8342 if( eType==SQLITE_TEXT ){
8343 Mem *pMem = (Mem*)pVal;
8344 applyNumericAffinity(pMem, 0);
8345 eType = sqlite3_value_type(pVal);
8346 }
8347 return eType;
8348 }
8349
8350 /*
8351 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
8352 ** not the internal Mem* type.
8353 */
8354 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
8355 sqlite3_value *pVal,
8356 u8 affinity,
8357 u8 enc
8358 ){
8359 applyAffinity((Mem *)pVal, affinity, enc);
8360 }
8361
8362 /*
8363 ** pMem currently only holds a string type (or maybe a BLOB that we can
8364 ** interpret as a string if we want to). Compute its corresponding
8365 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
8366 ** accordingly.
8367 */
8368 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
8369 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
8370 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
8371 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
8372 return 0;
8373 }
8374 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
8375 return MEM_Int;
8376 }
8377 return MEM_Real;
8378 }
8379
8380 /*
8381 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
8382 ** none.
8383 **
8384 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
8385 ** But it does set pMem->u.r and pMem->u.i appropriately.
8386 */
8387 static u16 numericType(Mem *pMem){
8388 if( pMem->flags & (MEM_Int|MEM_Real) ){
8389 return pMem->flags & (MEM_Int|MEM_Real);
8390 }
8391 if( pMem->flags & (MEM_Str|MEM_Blob) ){
8392 return computeNumericType(pMem);
8393 }
8394 return 0;
8395 }
8396
8397 #ifdef SQLITE_DEBUG
8398 /*
8399 ** Write a nice string representation of the contents of cell pMem
8400 ** into buffer zBuf, length nBuf.
8401 */
8402 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
8403 char *zCsr = zBuf;
8404 int f = pMem->flags;
8405
8406 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
8407
8408 if( f&MEM_Blob ){
8409 int i;
8410 char c;
8411 if( f & MEM_Dyn ){
8412 c = 'z';
8413 assert( (f & (MEM_Static|MEM_Ephem))==0 );
8414 }else if( f & MEM_Static ){
8415 c = 't';
8416 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
8417 }else if( f & MEM_Ephem ){
8418 c = 'e';
8419 assert( (f & (MEM_Static|MEM_Dyn))==0 );
8420 }else{
8421 c = 's';
8422 }
8423
8424 sqlite3_snprintf(100, zCsr, "%c", c);
8425 zCsr += sqlite3Strlen30(zCsr);
8426 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
8427 zCsr += sqlite3Strlen30(zCsr);
8428 for(i=0; i<16 && i<pMem->n; i++){
8429 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
8430 zCsr += sqlite3Strlen30(zCsr);
8431 }
8432 for(i=0; i<16 && i<pMem->n; i++){
8433 char z = pMem->z[i];
8434 if( z<32 || z>126 ) *zCsr++ = '.';
8435 else *zCsr++ = z;
8436 }
8437
8438 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
8439 zCsr += sqlite3Strlen30(zCsr);
8440 if( f & MEM_Zero ){
8441 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
8442 zCsr += sqlite3Strlen30(zCsr);
8443 }
8444 *zCsr = '\0';
8445 }else if( f & MEM_Str ){
8446 int j, k;
8447 zBuf[0] = ' ';
8448 if( f & MEM_Dyn ){
8449 zBuf[1] = 'z';
8450 assert( (f & (MEM_Static|MEM_Ephem))==0 );
8451 }else if( f & MEM_Static ){
8452 zBuf[1] = 't';
8453 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
8454 }else if( f & MEM_Ephem ){
8455 zBuf[1] = 'e';
8456 assert( (f & (MEM_Static|MEM_Dyn))==0 );
8457 }else{
8458 zBuf[1] = 's';
8459 }
8460 k = 2;
8461 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
8462 k += sqlite3Strlen30(&zBuf[k]);
8463 zBuf[k++] = '[';
8464 for(j=0; j<15 && j<pMem->n; j++){
8465 u8 c = pMem->z[j];
8466 if( c>=0x20 && c<0x7f ){
8467 zBuf[k++] = c;
8468 }else{
8469 zBuf[k++] = '.';
8470 }
8471 }
8472 zBuf[k++] = ']';
8473 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
8474 k += sqlite3Strlen30(&zBuf[k]);
8475 zBuf[k++] = 0;
8476 }
8477 }
8478 #endif
8479
8480 #ifdef SQLITE_DEBUG
8481 /*
8482 ** Print the value of a register for tracing purposes:
8483 */
8484 static void memTracePrint(Mem *p){
8485 if( p->flags & MEM_Undefined ){
8486 printf(" undefined");
8487 }else if( p->flags & MEM_Null ){
8488 printf(" NULL");
8489 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
8490 printf(" si:%lld", p->u.i);
8491 }else if( p->flags & MEM_Int ){
8492 printf(" i:%lld", p->u.i);
8493 #ifndef SQLITE_OMIT_FLOATING_POINT
8494 }else if( p->flags & MEM_Real ){
8495 printf(" r:%g", p->u.r);
8496 #endif
8497 }else if( p->flags & MEM_RowSet ){
8498 printf(" (rowset)");
8499 }else{
8500 char zBuf[200];
8501 sqlite3VdbeMemPrettyPrint(p, zBuf);
8502 printf(" %s", zBuf);
8503 }
8504 }
8505 static void registerTrace(int iReg, Mem *p){
8506 printf("REG[%d] = ", iReg);
8507 memTracePrint(p);
8508 printf("\n");
8509 }
8510 #endif
8511
8512 #ifdef SQLITE_DEBUG
8513 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
8514 #else
8515 # define REGISTER_TRACE(R,M)
8516 #endif
8517
8518
8519 #ifdef VDBE_PROFILE
8520
8521 /*
8522 ** hwtime.h contains inline assembler code for implementing
8523 ** high-performance timing routines.
8524 */
8525 /************** Include hwtime.h in the middle of vdbe.c *********************/
8526 /************** Begin file hwtime.h ******************************************/
8527 /*
8528 ** 2008 May 27
8529 **
8530 ** The author disclaims copyright to this source code. In place of
8531 ** a legal notice, here is a blessing:
8532 **
8533 ** May you do good and not evil.
8534 ** May you find forgiveness for yourself and forgive others.
8535 ** May you share freely, never taking more than you give.
8536 **
8537 ******************************************************************************
8538 **
8539 ** This file contains inline asm code for retrieving "high-performance"
8540 ** counters for x86 class CPUs.
8541 */
8542 #ifndef _HWTIME_H_
8543 #define _HWTIME_H_
8544
8545 /*
8546 ** The following routine only works on pentium-class (or newer) processors.
8547 ** It uses the RDTSC opcode to read the cycle count value out of the
8548 ** processor and returns that value. This can be used for high-res
8549 ** profiling.
8550 */
8551 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
8552 (defined(i386) || defined(__i386__) || defined(_M_IX86))
8553
8554 #if defined(__GNUC__)
8555
8556 __inline__ sqlite_uint64 sqlite3Hwtime(void){
8557 unsigned int lo, hi;
8558 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
8559 return (sqlite_uint64)hi << 32 | lo;
8560 }
8561
8562 #elif defined(_MSC_VER)
8563
8564 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
8565 __asm {
8566 rdtsc
8567 ret ; return value at EDX:EAX
8568 }
8569 }
8570
8571 #endif
8572
8573 #elif (defined(__GNUC__) && defined(__x86_64__))
8574
8575 __inline__ sqlite_uint64 sqlite3Hwtime(void){
8576 unsigned long val;
8577 __asm__ __volatile__ ("rdtsc" : "=A" (val));
8578 return val;
8579 }
8580
8581 #elif (defined(__GNUC__) && defined(__ppc__))
8582
8583 __inline__ sqlite_uint64 sqlite3Hwtime(void){
8584 unsigned long long retval;
8585 unsigned long junk;
8586 __asm__ __volatile__ ("\n\
8587 1: mftbu %1\n\
8588 mftb %L0\n\
8589 mftbu %0\n\
8590 cmpw %0,%1\n\
8591 bne 1b"
8592 : "=r" (retval), "=r" (junk));
8593 return retval;
8594 }
8595
8596 #else
8597
8598 #error Need implementation of sqlite3Hwtime() for your platform.
8599
8600 /*
8601 ** To compile without implementing sqlite3Hwtime() for your platform,
8602 ** you can remove the above #error and use the following
8603 ** stub function. You will lose timing support for many
8604 ** of the debugging and testing utilities, but it should at
8605 ** least compile and run.
8606 */
8607 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
8608
8609 #endif
8610
8611 #endif /* !defined(_HWTIME_H_) */
8612
8613 /************** End of hwtime.h **********************************************/
8614 /************** Continuing where we left off in vdbe.c ***********************/
8615
8616 #endif
8617
8618 #ifndef NDEBUG
8619 /*
8620 ** This function is only called from within an assert() expression. It
8621 ** checks that the sqlite3.nTransaction variable is correctly set to
8622 ** the number of non-transaction savepoints currently in the
8623 ** linked list starting at sqlite3.pSavepoint.
8624 **
8625 ** Usage:
8626 **
8627 ** assert( checkSavepointCount(db) );
8628 */
8629 static int checkSavepointCount(sqlite3 *db){
8630 int n = 0;
8631 Savepoint *p;
8632 for(p=db->pSavepoint; p; p=p->pNext) n++;
8633 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
8634 return 1;
8635 }
8636 #endif
8637
8638 /*
8639 ** Return the register of pOp->p2 after first preparing it to be
8640 ** overwritten with an integer value.
8641 */
8642 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
8643 sqlite3VdbeMemSetNull(pOut);
8644 pOut->flags = MEM_Int;
8645 return pOut;
8646 }
8647 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
8648 Mem *pOut;
8649 assert( pOp->p2>0 );
8650 assert( pOp->p2<=(p->nMem-p->nCursor) );
8651 pOut = &p->aMem[pOp->p2];
8652 memAboutToChange(p, pOut);
8653 if( VdbeMemDynamic(pOut) ){
8654 return out2PrereleaseWithClear(pOut);
8655 }else{
8656 pOut->flags = MEM_Int;
8657 return pOut;
8658 }
8659 }
8660
8661
8662 /*
8663 ** Execute as much of a VDBE program as we can.
8664 ** This is the core of sqlite3_step().
8665 */
8666 SQLITE_PRIVATE int sqlite3VdbeExec(
8667 Vdbe *p /* The VDBE */
8668 ){
8669 Op *aOp = p->aOp; /* Copy of p->aOp */
8670 Op *pOp = aOp; /* Current operation */
8671 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
8672 Op *pOrigOp; /* Value of pOp at the top of the loop */
8673 #endif
8674 int rc = SQLITE_OK; /* Value to return */
8675 sqlite3 *db = p->db; /* The database */
8676 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
8677 u8 encoding = ENC(db); /* The database encoding */
8678 int iCompare = 0; /* Result of last OP_Compare operation */
8679 unsigned nVmStep = 0; /* Number of virtual machine steps */
8680 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8681 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
8682 #endif
8683 Mem *aMem = p->aMem; /* Copy of p->aMem */
8684 Mem *pIn1 = 0; /* 1st input operand */
8685 Mem *pIn2 = 0; /* 2nd input operand */
8686 Mem *pIn3 = 0; /* 3rd input operand */
8687 Mem *pOut = 0; /* Output operand */
8688 int *aPermute = 0; /* Permutation of columns for OP_Compare */
8689 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
8690 #ifdef VDBE_PROFILE
8691 u64 start; /* CPU clock count at start of opcode */
8692 #endif
8693 /*** INSERT STACK UNION HERE ***/
8694
8695 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
8696 sqlite3VdbeEnter(p);
8697 if( p->rc==SQLITE_NOMEM ){
8698 /* This happens if a malloc() inside a call to sqlite3_column_text() or
8699 ** sqlite3_column_text16() failed. */
8700 goto no_mem;
8701 }
8702 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
8703 assert( p->bIsReader || p->readOnly!=0 );
8704 p->rc = SQLITE_OK;
8705 p->iCurrentTime = 0;
8706 assert( p->explain==0 );
8707 p->pResultSet = 0;
8708 db->busyHandler.nBusy = 0;
8709 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
8710 sqlite3VdbeIOTraceSql(p);
8711 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8712 if( db->xProgress ){
8713 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
8714 assert( 0 < db->nProgressOps );
8715 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
8716 }
8717 #endif
8718 #ifdef SQLITE_DEBUG
8719 sqlite3BeginBenignMalloc();
8720 if( p->pc==0
8721 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
8722 ){
8723 int i;
8724 int once = 1;
8725 sqlite3VdbePrintSql(p);
8726 if( p->db->flags & SQLITE_VdbeListing ){
8727 printf("VDBE Program Listing:\n");
8728 for(i=0; i<p->nOp; i++){
8729 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
8730 }
8731 }
8732 if( p->db->flags & SQLITE_VdbeEQP ){
8733 for(i=0; i<p->nOp; i++){
8734 if( aOp[i].opcode==OP_Explain ){
8735 if( once ) printf("VDBE Query Plan:\n");
8736 printf("%s\n", aOp[i].p4.z);
8737 once = 0;
8738 }
8739 }
8740 }
8741 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
8742 }
8743 sqlite3EndBenignMalloc();
8744 #endif
8745 for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
8746 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
8747 if( db->mallocFailed ) goto no_mem;
8748 #ifdef VDBE_PROFILE
8749 start = sqlite3Hwtime();
8750 #endif
8751 nVmStep++;
8752 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
8753 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
8754 #endif
8755
8756 /* Only allow tracing if SQLITE_DEBUG is defined.
8757 */
8758 #ifdef SQLITE_DEBUG
8759 if( db->flags & SQLITE_VdbeTrace ){
8760 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
8761 }
8762 #endif
8763
8764
8765 /* Check to see if we need to simulate an interrupt. This only happens
8766 ** if we have a special test build.
8767 */
8768 #ifdef SQLITE_TEST
8769 if( sqlite3_interrupt_count>0 ){
8770 sqlite3_interrupt_count--;
8771 if( sqlite3_interrupt_count==0 ){
8772 sqlite3_interrupt(db);
8773 }
8774 }
8775 #endif
8776
8777 /* Sanity checking on other operands */
8778 #ifdef SQLITE_DEBUG
8779 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
8780 if( (pOp->opflags & OPFLG_IN1)!=0 ){
8781 assert( pOp->p1>0 );
8782 assert( pOp->p1<=(p->nMem-p->nCursor) );
8783 assert( memIsValid(&aMem[pOp->p1]) );
8784 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
8785 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
8786 }
8787 if( (pOp->opflags & OPFLG_IN2)!=0 ){
8788 assert( pOp->p2>0 );
8789 assert( pOp->p2<=(p->nMem-p->nCursor) );
8790 assert( memIsValid(&aMem[pOp->p2]) );
8791 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
8792 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
8793 }
8794 if( (pOp->opflags & OPFLG_IN3)!=0 ){
8795 assert( pOp->p3>0 );
8796 assert( pOp->p3<=(p->nMem-p->nCursor) );
8797 assert( memIsValid(&aMem[pOp->p3]) );
8798 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
8799 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
8800 }
8801 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
8802 assert( pOp->p2>0 );
8803 assert( pOp->p2<=(p->nMem-p->nCursor) );
8804 memAboutToChange(p, &aMem[pOp->p2]);
8805 }
8806 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
8807 assert( pOp->p3>0 );
8808 assert( pOp->p3<=(p->nMem-p->nCursor) );
8809 memAboutToChange(p, &aMem[pOp->p3]);
8810 }
8811 #endif
8812 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
8813 pOrigOp = pOp;
8814 #endif
8815
8816 switch( pOp->opcode ){
8817
8818 /*****************************************************************************
8819 ** What follows is a massive switch statement where each case implements a
8820 ** separate instruction in the virtual machine. If we follow the usual
8821 ** indentation conventions, each case should be indented by 6 spaces. But
8822 ** that is a lot of wasted space on the left margin. So the code within
8823 ** the switch statement will break with convention and be flush-left. Another
8824 ** big comment (similar to this one) will mark the point in the code where
8825 ** we transition back to normal indentation.
8826 **
8827 ** The formatting of each case is important. The makefile for SQLite
8828 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
8829 ** file looking for lines that begin with "case OP_". The opcodes.h files
8830 ** will be filled with #defines that give unique integer values to each
8831 ** opcode and the opcodes.c file is filled with an array of strings where
8832 ** each string is the symbolic name for the corresponding opcode. If the
8833 ** case statement is followed by a comment of the form "/# same as ... #/"
8834 ** that comment is used to determine the particular value of the opcode.
8835 **
8836 ** Other keywords in the comment that follows each case are used to
8837 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
8838 ** Keywords include: in1, in2, in3, out2, out3. See
8839 ** the mkopcodeh.awk script for additional information.
8840 **
8841 ** Documentation about VDBE opcodes is generated by scanning this file
8842 ** for lines of that contain "Opcode:". That line and all subsequent
8843 ** comment lines are used in the generation of the opcode.html documentation
8844 ** file.
8845 **
8846 ** SUMMARY:
8847 **
8848 ** Formatting is important to scripts that scan this file.
8849 ** Do not deviate from the formatting style currently in use.
8850 **
8851 *****************************************************************************/
8852
8853 /* Opcode: Goto * P2 * * *
8854 **
8855 ** An unconditional jump to address P2.
8856 ** The next instruction executed will be
8857 ** the one at index P2 from the beginning of
8858 ** the program.
8859 **
8860 ** The P1 parameter is not actually used by this opcode. However, it
8861 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
8862 ** that this Goto is the bottom of a loop and that the lines from P2 down
8863 ** to the current line should be indented for EXPLAIN output.
8864 */
8865 case OP_Goto: { /* jump */
8866 jump_to_p2_and_check_for_interrupt:
8867 pOp = &aOp[pOp->p2 - 1];
8868
8869 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
8870 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
8871 ** completion. Check to see if sqlite3_interrupt() has been called
8872 ** or if the progress callback needs to be invoked.
8873 **
8874 ** This code uses unstructured "goto" statements and does not look clean.
8875 ** But that is not due to sloppy coding habits. The code is written this
8876 ** way for performance, to avoid having to run the interrupt and progress
8877 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
8878 ** faster according to "valgrind --tool=cachegrind" */
8879 check_for_interrupt:
8880 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
8881 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8882 /* Call the progress callback if it is configured and the required number
8883 ** of VDBE ops have been executed (either since this invocation of
8884 ** sqlite3VdbeExec() or since last time the progress callback was called).
8885 ** If the progress callback returns non-zero, exit the virtual machine with
8886 ** a return code SQLITE_ABORT.
8887 */
8888 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
8889 assert( db->nProgressOps!=0 );
8890 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
8891 if( db->xProgress(db->pProgressArg) ){
8892 rc = SQLITE_INTERRUPT;
8893 goto vdbe_error_halt;
8894 }
8895 }
8896 #endif
8897
8898 break;
8899 }
8900
8901 /* Opcode: Gosub P1 P2 * * *
8902 **
8903 ** Write the current address onto register P1
8904 ** and then jump to address P2.
8905 */
8906 case OP_Gosub: { /* jump */
8907 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
8908 pIn1 = &aMem[pOp->p1];
8909 assert( VdbeMemDynamic(pIn1)==0 );
8910 memAboutToChange(p, pIn1);
8911 pIn1->flags = MEM_Int;
8912 pIn1->u.i = (int)(pOp-aOp);
8913 REGISTER_TRACE(pOp->p1, pIn1);
8914
8915 /* Most jump operations do a goto to this spot in order to update
8916 ** the pOp pointer. */
8917 jump_to_p2:
8918 pOp = &aOp[pOp->p2 - 1];
8919 break;
8920 }
8921
8922 /* Opcode: Return P1 * * * *
8923 **
8924 ** Jump to the next instruction after the address in register P1. After
8925 ** the jump, register P1 becomes undefined.
8926 */
8927 case OP_Return: { /* in1 */
8928 pIn1 = &aMem[pOp->p1];
8929 assert( pIn1->flags==MEM_Int );
8930 pOp = &aOp[pIn1->u.i];
8931 pIn1->flags = MEM_Undefined;
8932 break;
8933 }
8934
8935 /* Opcode: InitCoroutine P1 P2 P3 * *
8936 **
8937 ** Set up register P1 so that it will Yield to the coroutine
8938 ** located at address P3.
8939 **
8940 ** If P2!=0 then the coroutine implementation immediately follows
8941 ** this opcode. So jump over the coroutine implementation to
8942 ** address P2.
8943 **
8944 ** See also: EndCoroutine
8945 */
8946 case OP_InitCoroutine: { /* jump */
8947 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
8948 assert( pOp->p2>=0 && pOp->p2<p->nOp );
8949 assert( pOp->p3>=0 && pOp->p3<p->nOp );
8950 pOut = &aMem[pOp->p1];
8951 assert( !VdbeMemDynamic(pOut) );
8952 pOut->u.i = pOp->p3 - 1;
8953 pOut->flags = MEM_Int;
8954 if( pOp->p2 ) goto jump_to_p2;
8955 break;
8956 }
8957
8958 /* Opcode: EndCoroutine P1 * * * *
8959 **
8960 ** The instruction at the address in register P1 is a Yield.
8961 ** Jump to the P2 parameter of that Yield.
8962 ** After the jump, register P1 becomes undefined.
8963 **
8964 ** See also: InitCoroutine
8965 */
8966 case OP_EndCoroutine: { /* in1 */
8967 VdbeOp *pCaller;
8968 pIn1 = &aMem[pOp->p1];
8969 assert( pIn1->flags==MEM_Int );
8970 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
8971 pCaller = &aOp[pIn1->u.i];
8972 assert( pCaller->opcode==OP_Yield );
8973 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
8974 pOp = &aOp[pCaller->p2 - 1];
8975 pIn1->flags = MEM_Undefined;
8976 break;
8977 }
8978
8979 /* Opcode: Yield P1 P2 * * *
8980 **
8981 ** Swap the program counter with the value in register P1. This
8982 ** has the effect of yielding to a coroutine.
8983 **
8984 ** If the coroutine that is launched by this instruction ends with
8985 ** Yield or Return then continue to the next instruction. But if
8986 ** the coroutine launched by this instruction ends with
8987 ** EndCoroutine, then jump to P2 rather than continuing with the
8988 ** next instruction.
8989 **
8990 ** See also: InitCoroutine
8991 */
8992 case OP_Yield: { /* in1, jump */
8993 int pcDest;
8994 pIn1 = &aMem[pOp->p1];
8995 assert( VdbeMemDynamic(pIn1)==0 );
8996 pIn1->flags = MEM_Int;
8997 pcDest = (int)pIn1->u.i;
8998 pIn1->u.i = (int)(pOp - aOp);
8999 REGISTER_TRACE(pOp->p1, pIn1);
9000 pOp = &aOp[pcDest];
9001 break;
9002 }
9003
9004 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
9005 ** Synopsis: if r[P3]=null halt
9006 **
9007 ** Check the value in register P3. If it is NULL then Halt using
9008 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
9009 ** value in register P3 is not NULL, then this routine is a no-op.
9010 ** The P5 parameter should be 1.
9011 */
9012 case OP_HaltIfNull: { /* in3 */
9013 pIn3 = &aMem[pOp->p3];
9014 if( (pIn3->flags & MEM_Null)==0 ) break;
9015 /* Fall through into OP_Halt */
9016 }
9017
9018 /* Opcode: Halt P1 P2 * P4 P5
9019 **
9020 ** Exit immediately. All open cursors, etc are closed
9021 ** automatically.
9022 **
9023 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
9024 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
9025 ** For errors, it can be some other value. If P1!=0 then P2 will determine
9026 ** whether or not to rollback the current transaction. Do not rollback
9027 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
9028 ** then back out all changes that have occurred during this execution of the
9029 ** VDBE, but do not rollback the transaction.
9030 **
9031 ** If P4 is not null then it is an error message string.
9032 **
9033 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
9034 **
9035 ** 0: (no change)
9036 ** 1: NOT NULL contraint failed: P4
9037 ** 2: UNIQUE constraint failed: P4
9038 ** 3: CHECK constraint failed: P4
9039 ** 4: FOREIGN KEY constraint failed: P4
9040 **
9041 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
9042 ** omitted.
9043 **
9044 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
9045 ** every program. So a jump past the last instruction of the program
9046 ** is the same as executing Halt.
9047 */
9048 case OP_Halt: {
9049 const char *zType;
9050 const char *zLogFmt;
9051 VdbeFrame *pFrame;
9052 int pcx;
9053
9054 pcx = (int)(pOp - aOp);
9055 if( pOp->p1==SQLITE_OK && p->pFrame ){
9056 /* Halt the sub-program. Return control to the parent frame. */
9057 pFrame = p->pFrame;
9058 p->pFrame = pFrame->pParent;
9059 p->nFrame--;
9060 sqlite3VdbeSetChanges(db, p->nChange);
9061 pcx = sqlite3VdbeFrameRestore(pFrame);
9062 lastRowid = db->lastRowid;
9063 if( pOp->p2==OE_Ignore ){
9064 /* Instruction pcx is the OP_Program that invoked the sub-program
9065 ** currently being halted. If the p2 instruction of this OP_Halt
9066 ** instruction is set to OE_Ignore, then the sub-program is throwing
9067 ** an IGNORE exception. In this case jump to the address specified
9068 ** as the p2 of the calling OP_Program. */
9069 pcx = p->aOp[pcx].p2-1;
9070 }
9071 aOp = p->aOp;
9072 aMem = p->aMem;
9073 pOp = &aOp[pcx];
9074 break;
9075 }
9076 p->rc = pOp->p1;
9077 p->errorAction = (u8)pOp->p2;
9078 p->pc = pcx;
9079 if( p->rc ){
9080 if( pOp->p5 ){
9081 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
9082 "FOREIGN KEY" };
9083 assert( pOp->p5>=1 && pOp->p5<=4 );
9084 testcase( pOp->p5==1 );
9085 testcase( pOp->p5==2 );
9086 testcase( pOp->p5==3 );
9087 testcase( pOp->p5==4 );
9088 zType = azType[pOp->p5-1];
9089 }else{
9090 zType = 0;
9091 }
9092 assert( zType!=0 || pOp->p4.z!=0 );
9093 zLogFmt = "abort at %d in [%s]: %s";
9094 if( zType && pOp->p4.z ){
9095 sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
9096 }else if( pOp->p4.z ){
9097 sqlite3VdbeError(p, "%s", pOp->p4.z);
9098 }else{
9099 sqlite3VdbeError(p, "%s constraint failed", zType);
9100 }
9101 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
9102 }
9103 rc = sqlite3VdbeHalt(p);
9104 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
9105 if( rc==SQLITE_BUSY ){
9106 p->rc = rc = SQLITE_BUSY;
9107 }else{
9108 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
9109 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
9110 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
9111 }
9112 goto vdbe_return;
9113 }
9114
9115 /* Opcode: Integer P1 P2 * * *
9116 ** Synopsis: r[P2]=P1
9117 **
9118 ** The 32-bit integer value P1 is written into register P2.
9119 */
9120 case OP_Integer: { /* out2 */
9121 pOut = out2Prerelease(p, pOp);
9122 pOut->u.i = pOp->p1;
9123 break;
9124 }
9125
9126 /* Opcode: Int64 * P2 * P4 *
9127 ** Synopsis: r[P2]=P4
9128 **
9129 ** P4 is a pointer to a 64-bit integer value.
9130 ** Write that value into register P2.
9131 */
9132 case OP_Int64: { /* out2 */
9133 pOut = out2Prerelease(p, pOp);
9134 assert( pOp->p4.pI64!=0 );
9135 pOut->u.i = *pOp->p4.pI64;
9136 break;
9137 }
9138
9139 #ifndef SQLITE_OMIT_FLOATING_POINT
9140 /* Opcode: Real * P2 * P4 *
9141 ** Synopsis: r[P2]=P4
9142 **
9143 ** P4 is a pointer to a 64-bit floating point value.
9144 ** Write that value into register P2.
9145 */
9146 case OP_Real: { /* same as TK_FLOAT, out2 */
9147 pOut = out2Prerelease(p, pOp);
9148 pOut->flags = MEM_Real;
9149 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
9150 pOut->u.r = *pOp->p4.pReal;
9151 break;
9152 }
9153 #endif
9154
9155 /* Opcode: String8 * P2 * P4 *
9156 ** Synopsis: r[P2]='P4'
9157 **
9158 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
9159 ** into a String opcode before it is executed for the first time. During
9160 ** this transformation, the length of string P4 is computed and stored
9161 ** as the P1 parameter.
9162 */
9163 case OP_String8: { /* same as TK_STRING, out2 */
9164 assert( pOp->p4.z!=0 );
9165 pOut = out2Prerelease(p, pOp);
9166 pOp->opcode = OP_String;
9167 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
9168
9169 #ifndef SQLITE_OMIT_UTF16
9170 if( encoding!=SQLITE_UTF8 ){
9171 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
9172 if( rc==SQLITE_TOOBIG ) goto too_big;
9173 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
9174 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
9175 assert( VdbeMemDynamic(pOut)==0 );
9176 pOut->szMalloc = 0;
9177 pOut->flags |= MEM_Static;
9178 if( pOp->p4type==P4_DYNAMIC ){
9179 sqlite3DbFree(db, pOp->p4.z);
9180 }
9181 pOp->p4type = P4_DYNAMIC;
9182 pOp->p4.z = pOut->z;
9183 pOp->p1 = pOut->n;
9184 }
9185 #endif
9186 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
9187 goto too_big;
9188 }
9189 /* Fall through to the next case, OP_String */
9190 }
9191
9192 /* Opcode: String P1 P2 P3 P4 P5
9193 ** Synopsis: r[P2]='P4' (len=P1)
9194 **
9195 ** The string value P4 of length P1 (bytes) is stored in register P2.
9196 **
9197 ** If P5!=0 and the content of register P3 is greater than zero, then
9198 ** the datatype of the register P2 is converted to BLOB. The content is
9199 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
9200 ** of a string, as if it had been CAST.
9201 */
9202 case OP_String: { /* out2 */
9203 assert( pOp->p4.z!=0 );
9204 pOut = out2Prerelease(p, pOp);
9205 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
9206 pOut->z = pOp->p4.z;
9207 pOut->n = pOp->p1;
9208 pOut->enc = encoding;
9209 UPDATE_MAX_BLOBSIZE(pOut);
9210 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
9211 if( pOp->p5 ){
9212 assert( pOp->p3>0 );
9213 assert( pOp->p3<=(p->nMem-p->nCursor) );
9214 pIn3 = &aMem[pOp->p3];
9215 assert( pIn3->flags & MEM_Int );
9216 if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
9217 }
9218 #endif
9219 break;
9220 }
9221
9222 /* Opcode: Null P1 P2 P3 * *
9223 ** Synopsis: r[P2..P3]=NULL
9224 **
9225 ** Write a NULL into registers P2. If P3 greater than P2, then also write
9226 ** NULL into register P3 and every register in between P2 and P3. If P3
9227 ** is less than P2 (typically P3 is zero) then only register P2 is
9228 ** set to NULL.
9229 **
9230 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
9231 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
9232 ** OP_Ne or OP_Eq.
9233 */
9234 case OP_Null: { /* out2 */
9235 int cnt;
9236 u16 nullFlag;
9237 pOut = out2Prerelease(p, pOp);
9238 cnt = pOp->p3-pOp->p2;
9239 assert( pOp->p3<=(p->nMem-p->nCursor) );
9240 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
9241 while( cnt>0 ){
9242 pOut++;
9243 memAboutToChange(p, pOut);
9244 sqlite3VdbeMemSetNull(pOut);
9245 pOut->flags = nullFlag;
9246 cnt--;
9247 }
9248 break;
9249 }
9250
9251 /* Opcode: SoftNull P1 * * * *
9252 ** Synopsis: r[P1]=NULL
9253 **
9254 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
9255 ** instruction, but do not free any string or blob memory associated with
9256 ** the register, so that if the value was a string or blob that was
9257 ** previously copied using OP_SCopy, the copies will continue to be valid.
9258 */
9259 case OP_SoftNull: {
9260 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
9261 pOut = &aMem[pOp->p1];
9262 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
9263 break;
9264 }
9265
9266 /* Opcode: Blob P1 P2 * P4 *
9267 ** Synopsis: r[P2]=P4 (len=P1)
9268 **
9269 ** P4 points to a blob of data P1 bytes long. Store this
9270 ** blob in register P2.
9271 */
9272 case OP_Blob: { /* out2 */
9273 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
9274 pOut = out2Prerelease(p, pOp);
9275 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
9276 pOut->enc = encoding;
9277 UPDATE_MAX_BLOBSIZE(pOut);
9278 break;
9279 }
9280
9281 /* Opcode: Variable P1 P2 * P4 *
9282 ** Synopsis: r[P2]=parameter(P1,P4)
9283 **
9284 ** Transfer the values of bound parameter P1 into register P2
9285 **
9286 ** If the parameter is named, then its name appears in P4.
9287 ** The P4 value is used by sqlite3_bind_parameter_name().
9288 */
9289 case OP_Variable: { /* out2 */
9290 Mem *pVar; /* Value being transferred */
9291
9292 assert( pOp->p1>0 && pOp->p1<=p->nVar );
9293 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
9294 pVar = &p->aVar[pOp->p1 - 1];
9295 if( sqlite3VdbeMemTooBig(pVar) ){
9296 goto too_big;
9297 }
9298 pOut = out2Prerelease(p, pOp);
9299 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
9300 UPDATE_MAX_BLOBSIZE(pOut);
9301 break;
9302 }
9303
9304 /* Opcode: Move P1 P2 P3 * *
9305 ** Synopsis: r[P2@P3]=r[P1@P3]
9306 **
9307 ** Move the P3 values in register P1..P1+P3-1 over into
9308 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
9309 ** left holding a NULL. It is an error for register ranges
9310 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
9311 ** for P3 to be less than 1.
9312 */
9313 case OP_Move: {
9314 int n; /* Number of registers left to copy */
9315 int p1; /* Register to copy from */
9316 int p2; /* Register to copy to */
9317
9318 n = pOp->p3;
9319 p1 = pOp->p1;
9320 p2 = pOp->p2;
9321 assert( n>0 && p1>0 && p2>0 );
9322 assert( p1+n<=p2 || p2+n<=p1 );
9323
9324 pIn1 = &aMem[p1];
9325 pOut = &aMem[p2];
9326 do{
9327 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
9328 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
9329 assert( memIsValid(pIn1) );
9330 memAboutToChange(p, pOut);
9331 sqlite3VdbeMemMove(pOut, pIn1);
9332 #ifdef SQLITE_DEBUG
9333 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
9334 pOut->pScopyFrom += pOp->p2 - p1;
9335 }
9336 #endif
9337 Deephemeralize(pOut);
9338 REGISTER_TRACE(p2++, pOut);
9339 pIn1++;
9340 pOut++;
9341 }while( --n );
9342 break;
9343 }
9344
9345 /* Opcode: Copy P1 P2 P3 * *
9346 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
9347 **
9348 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
9349 **
9350 ** This instruction makes a deep copy of the value. A duplicate
9351 ** is made of any string or blob constant. See also OP_SCopy.
9352 */
9353 case OP_Copy: {
9354 int n;
9355
9356 n = pOp->p3;
9357 pIn1 = &aMem[pOp->p1];
9358 pOut = &aMem[pOp->p2];
9359 assert( pOut!=pIn1 );
9360 while( 1 ){
9361 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
9362 Deephemeralize(pOut);
9363 #ifdef SQLITE_DEBUG
9364 pOut->pScopyFrom = 0;
9365 #endif
9366 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
9367 if( (n--)==0 ) break;
9368 pOut++;
9369 pIn1++;
9370 }
9371 break;
9372 }
9373
9374 /* Opcode: SCopy P1 P2 * * *
9375 ** Synopsis: r[P2]=r[P1]
9376 **
9377 ** Make a shallow copy of register P1 into register P2.
9378 **
9379 ** This instruction makes a shallow copy of the value. If the value
9380 ** is a string or blob, then the copy is only a pointer to the
9381 ** original and hence if the original changes so will the copy.
9382 ** Worse, if the original is deallocated, the copy becomes invalid.
9383 ** Thus the program must guarantee that the original will not change
9384 ** during the lifetime of the copy. Use OP_Copy to make a complete
9385 ** copy.
9386 */
9387 case OP_SCopy: { /* out2 */
9388 pIn1 = &aMem[pOp->p1];
9389 pOut = &aMem[pOp->p2];
9390 assert( pOut!=pIn1 );
9391 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
9392 #ifdef SQLITE_DEBUG
9393 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
9394 #endif
9395 break;
9396 }
9397
9398 /* Opcode: IntCopy P1 P2 * * *
9399 ** Synopsis: r[P2]=r[P1]
9400 **
9401 ** Transfer the integer value held in register P1 into register P2.
9402 **
9403 ** This is an optimized version of SCopy that works only for integer
9404 ** values.
9405 */
9406 case OP_IntCopy: { /* out2 */
9407 pIn1 = &aMem[pOp->p1];
9408 assert( (pIn1->flags & MEM_Int)!=0 );
9409 pOut = &aMem[pOp->p2];
9410 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
9411 break;
9412 }
9413
9414 /* Opcode: ResultRow P1 P2 * * *
9415 ** Synopsis: output=r[P1@P2]
9416 **
9417 ** The registers P1 through P1+P2-1 contain a single row of
9418 ** results. This opcode causes the sqlite3_step() call to terminate
9419 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
9420 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
9421 ** the result row.
9422 */
9423 case OP_ResultRow: {
9424 Mem *pMem;
9425 int i;
9426 assert( p->nResColumn==pOp->p2 );
9427 assert( pOp->p1>0 );
9428 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
9429
9430 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9431 /* Run the progress counter just before returning.
9432 */
9433 if( db->xProgress!=0
9434 && nVmStep>=nProgressLimit
9435 && db->xProgress(db->pProgressArg)!=0
9436 ){
9437 rc = SQLITE_INTERRUPT;
9438 goto vdbe_error_halt;
9439 }
9440 #endif
9441
9442 /* If this statement has violated immediate foreign key constraints, do
9443 ** not return the number of rows modified. And do not RELEASE the statement
9444 ** transaction. It needs to be rolled back. */
9445 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
9446 assert( db->flags&SQLITE_CountRows );
9447 assert( p->usesStmtJournal );
9448 break;
9449 }
9450
9451 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
9452 ** DML statements invoke this opcode to return the number of rows
9453 ** modified to the user. This is the only way that a VM that
9454 ** opens a statement transaction may invoke this opcode.
9455 **
9456 ** In case this is such a statement, close any statement transaction
9457 ** opened by this VM before returning control to the user. This is to
9458 ** ensure that statement-transactions are always nested, not overlapping.
9459 ** If the open statement-transaction is not closed here, then the user
9460 ** may step another VM that opens its own statement transaction. This
9461 ** may lead to overlapping statement transactions.
9462 **
9463 ** The statement transaction is never a top-level transaction. Hence
9464 ** the RELEASE call below can never fail.
9465 */
9466 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
9467 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
9468 if( NEVER(rc!=SQLITE_OK) ){
9469 break;
9470 }
9471
9472 /* Invalidate all ephemeral cursor row caches */
9473 p->cacheCtr = (p->cacheCtr + 2)|1;
9474
9475 /* Make sure the results of the current row are \000 terminated
9476 ** and have an assigned type. The results are de-ephemeralized as
9477 ** a side effect.
9478 */
9479 pMem = p->pResultSet = &aMem[pOp->p1];
9480 for(i=0; i<pOp->p2; i++){
9481 assert( memIsValid(&pMem[i]) );
9482 Deephemeralize(&pMem[i]);
9483 assert( (pMem[i].flags & MEM_Ephem)==0
9484 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
9485 sqlite3VdbeMemNulTerminate(&pMem[i]);
9486 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
9487 }
9488 if( db->mallocFailed ) goto no_mem;
9489
9490 /* Return SQLITE_ROW
9491 */
9492 p->pc = (int)(pOp - aOp) + 1;
9493 rc = SQLITE_ROW;
9494 goto vdbe_return;
9495 }
9496
9497 /* Opcode: Concat P1 P2 P3 * *
9498 ** Synopsis: r[P3]=r[P2]+r[P1]
9499 **
9500 ** Add the text in register P1 onto the end of the text in
9501 ** register P2 and store the result in register P3.
9502 ** If either the P1 or P2 text are NULL then store NULL in P3.
9503 **
9504 ** P3 = P2 || P1
9505 **
9506 ** It is illegal for P1 and P3 to be the same register. Sometimes,
9507 ** if P3 is the same register as P2, the implementation is able
9508 ** to avoid a memcpy().
9509 */
9510 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
9511 i64 nByte;
9512
9513 pIn1 = &aMem[pOp->p1];
9514 pIn2 = &aMem[pOp->p2];
9515 pOut = &aMem[pOp->p3];
9516 assert( pIn1!=pOut );
9517 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
9518 sqlite3VdbeMemSetNull(pOut);
9519 break;
9520 }
9521 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
9522 Stringify(pIn1, encoding);
9523 Stringify(pIn2, encoding);
9524 nByte = pIn1->n + pIn2->n;
9525 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
9526 goto too_big;
9527 }
9528 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
9529 goto no_mem;
9530 }
9531 MemSetTypeFlag(pOut, MEM_Str);
9532 if( pOut!=pIn2 ){
9533 memcpy(pOut->z, pIn2->z, pIn2->n);
9534 }
9535 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
9536 pOut->z[nByte]=0;
9537 pOut->z[nByte+1] = 0;
9538 pOut->flags |= MEM_Term;
9539 pOut->n = (int)nByte;
9540 pOut->enc = encoding;
9541 UPDATE_MAX_BLOBSIZE(pOut);
9542 break;
9543 }
9544
9545 /* Opcode: Add P1 P2 P3 * *
9546 ** Synopsis: r[P3]=r[P1]+r[P2]
9547 **
9548 ** Add the value in register P1 to the value in register P2
9549 ** and store the result in register P3.
9550 ** If either input is NULL, the result is NULL.
9551 */
9552 /* Opcode: Multiply P1 P2 P3 * *
9553 ** Synopsis: r[P3]=r[P1]*r[P2]
9554 **
9555 **
9556 ** Multiply the value in register P1 by the value in register P2
9557 ** and store the result in register P3.
9558 ** If either input is NULL, the result is NULL.
9559 */
9560 /* Opcode: Subtract P1 P2 P3 * *
9561 ** Synopsis: r[P3]=r[P2]-r[P1]
9562 **
9563 ** Subtract the value in register P1 from the value in register P2
9564 ** and store the result in register P3.
9565 ** If either input is NULL, the result is NULL.
9566 */
9567 /* Opcode: Divide P1 P2 P3 * *
9568 ** Synopsis: r[P3]=r[P2]/r[P1]
9569 **
9570 ** Divide the value in register P1 by the value in register P2
9571 ** and store the result in register P3 (P3=P2/P1). If the value in
9572 ** register P1 is zero, then the result is NULL. If either input is
9573 ** NULL, the result is NULL.
9574 */
9575 /* Opcode: Remainder P1 P2 P3 * *
9576 ** Synopsis: r[P3]=r[P2]%r[P1]
9577 **
9578 ** Compute the remainder after integer register P2 is divided by
9579 ** register P1 and store the result in register P3.
9580 ** If the value in register P1 is zero the result is NULL.
9581 ** If either operand is NULL, the result is NULL.
9582 */
9583 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
9584 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
9585 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
9586 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
9587 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
9588 char bIntint; /* Started out as two integer operands */
9589 u16 flags; /* Combined MEM_* flags from both inputs */
9590 u16 type1; /* Numeric type of left operand */
9591 u16 type2; /* Numeric type of right operand */
9592 i64 iA; /* Integer value of left operand */
9593 i64 iB; /* Integer value of right operand */
9594 double rA; /* Real value of left operand */
9595 double rB; /* Real value of right operand */
9596
9597 pIn1 = &aMem[pOp->p1];
9598 type1 = numericType(pIn1);
9599 pIn2 = &aMem[pOp->p2];
9600 type2 = numericType(pIn2);
9601 pOut = &aMem[pOp->p3];
9602 flags = pIn1->flags | pIn2->flags;
9603 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
9604 if( (type1 & type2 & MEM_Int)!=0 ){
9605 iA = pIn1->u.i;
9606 iB = pIn2->u.i;
9607 bIntint = 1;
9608 switch( pOp->opcode ){
9609 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
9610 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
9611 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
9612 case OP_Divide: {
9613 if( iA==0 ) goto arithmetic_result_is_null;
9614 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
9615 iB /= iA;
9616 break;
9617 }
9618 default: {
9619 if( iA==0 ) goto arithmetic_result_is_null;
9620 if( iA==-1 ) iA = 1;
9621 iB %= iA;
9622 break;
9623 }
9624 }
9625 pOut->u.i = iB;
9626 MemSetTypeFlag(pOut, MEM_Int);
9627 }else{
9628 bIntint = 0;
9629 fp_math:
9630 rA = sqlite3VdbeRealValue(pIn1);
9631 rB = sqlite3VdbeRealValue(pIn2);
9632 switch( pOp->opcode ){
9633 case OP_Add: rB += rA; break;
9634 case OP_Subtract: rB -= rA; break;
9635 case OP_Multiply: rB *= rA; break;
9636 case OP_Divide: {
9637 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
9638 if( rA==(double)0 ) goto arithmetic_result_is_null;
9639 rB /= rA;
9640 break;
9641 }
9642 default: {
9643 iA = (i64)rA;
9644 iB = (i64)rB;
9645 if( iA==0 ) goto arithmetic_result_is_null;
9646 if( iA==-1 ) iA = 1;
9647 rB = (double)(iB % iA);
9648 break;
9649 }
9650 }
9651 #ifdef SQLITE_OMIT_FLOATING_POINT
9652 pOut->u.i = rB;
9653 MemSetTypeFlag(pOut, MEM_Int);
9654 #else
9655 if( sqlite3IsNaN(rB) ){
9656 goto arithmetic_result_is_null;
9657 }
9658 pOut->u.r = rB;
9659 MemSetTypeFlag(pOut, MEM_Real);
9660 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
9661 sqlite3VdbeIntegerAffinity(pOut);
9662 }
9663 #endif
9664 }
9665 break;
9666
9667 arithmetic_result_is_null:
9668 sqlite3VdbeMemSetNull(pOut);
9669 break;
9670 }
9671
9672 /* Opcode: CollSeq P1 * * P4
9673 **
9674 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
9675 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
9676 ** be returned. This is used by the built-in min(), max() and nullif()
9677 ** functions.
9678 **
9679 ** If P1 is not zero, then it is a register that a subsequent min() or
9680 ** max() aggregate will set to 1 if the current row is not the minimum or
9681 ** maximum. The P1 register is initialized to 0 by this instruction.
9682 **
9683 ** The interface used by the implementation of the aforementioned functions
9684 ** to retrieve the collation sequence set by this opcode is not available
9685 ** publicly. Only built-in functions have access to this feature.
9686 */
9687 case OP_CollSeq: {
9688 assert( pOp->p4type==P4_COLLSEQ );
9689 if( pOp->p1 ){
9690 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
9691 }
9692 break;
9693 }
9694
9695 /* Opcode: Function0 P1 P2 P3 P4 P5
9696 ** Synopsis: r[P3]=func(r[P2@P5])
9697 **
9698 ** Invoke a user function (P4 is a pointer to a FuncDef object that
9699 ** defines the function) with P5 arguments taken from register P2 and
9700 ** successors. The result of the function is stored in register P3.
9701 ** Register P3 must not be one of the function inputs.
9702 **
9703 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
9704 ** function was determined to be constant at compile time. If the first
9705 ** argument was constant then bit 0 of P1 is set. This is used to determine
9706 ** whether meta data associated with a user function argument using the
9707 ** sqlite3_set_auxdata() API may be safely retained until the next
9708 ** invocation of this opcode.
9709 **
9710 ** See also: Function, AggStep, AggFinal
9711 */
9712 /* Opcode: Function P1 P2 P3 P4 P5
9713 ** Synopsis: r[P3]=func(r[P2@P5])
9714 **
9715 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
9716 ** contains a pointer to the function to be run) with P5 arguments taken
9717 ** from register P2 and successors. The result of the function is stored
9718 ** in register P3. Register P3 must not be one of the function inputs.
9719 **
9720 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
9721 ** function was determined to be constant at compile time. If the first
9722 ** argument was constant then bit 0 of P1 is set. This is used to determine
9723 ** whether meta data associated with a user function argument using the
9724 ** sqlite3_set_auxdata() API may be safely retained until the next
9725 ** invocation of this opcode.
9726 **
9727 ** SQL functions are initially coded as OP_Function0 with P4 pointing
9728 ** to a FuncDef object. But on first evaluation, the P4 operand is
9729 ** automatically converted into an sqlite3_context object and the operation
9730 ** changed to this OP_Function opcode. In this way, the initialization of
9731 ** the sqlite3_context object occurs only once, rather than once for each
9732 ** evaluation of the function.
9733 **
9734 ** See also: Function0, AggStep, AggFinal
9735 */
9736 case OP_Function0: {
9737 int n;
9738 sqlite3_context *pCtx;
9739
9740 assert( pOp->p4type==P4_FUNCDEF );
9741 n = pOp->p5;
9742 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
9743 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
9744 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
9745 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
9746 if( pCtx==0 ) goto no_mem;
9747 pCtx->pOut = 0;
9748 pCtx->pFunc = pOp->p4.pFunc;
9749 pCtx->iOp = (int)(pOp - aOp);
9750 pCtx->pVdbe = p;
9751 pCtx->argc = n;
9752 pOp->p4type = P4_FUNCCTX;
9753 pOp->p4.pCtx = pCtx;
9754 pOp->opcode = OP_Function;
9755 /* Fall through into OP_Function */
9756 }
9757 case OP_Function: {
9758 int i;
9759 sqlite3_context *pCtx;
9760
9761 assert( pOp->p4type==P4_FUNCCTX );
9762 pCtx = pOp->p4.pCtx;
9763
9764 /* If this function is inside of a trigger, the register array in aMem[]
9765 ** might change from one evaluation to the next. The next block of code
9766 ** checks to see if the register array has changed, and if so it
9767 ** reinitializes the relavant parts of the sqlite3_context object */
9768 pOut = &aMem[pOp->p3];
9769 if( pCtx->pOut != pOut ){
9770 pCtx->pOut = pOut;
9771 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
9772 }
9773
9774 memAboutToChange(p, pCtx->pOut);
9775 #ifdef SQLITE_DEBUG
9776 for(i=0; i<pCtx->argc; i++){
9777 assert( memIsValid(pCtx->argv[i]) );
9778 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
9779 }
9780 #endif
9781 MemSetTypeFlag(pCtx->pOut, MEM_Null);
9782 pCtx->fErrorOrAux = 0;
9783 db->lastRowid = lastRowid;
9784 (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
9785 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
9786
9787 /* If the function returned an error, throw an exception */
9788 if( pCtx->fErrorOrAux ){
9789 if( pCtx->isError ){
9790 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
9791 rc = pCtx->isError;
9792 }
9793 sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
9794 }
9795
9796 /* Copy the result of the function into register P3 */
9797 if( pOut->flags & (MEM_Str|MEM_Blob) ){
9798 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
9799 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
9800 }
9801
9802 REGISTER_TRACE(pOp->p3, pCtx->pOut);
9803 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
9804 break;
9805 }
9806
9807 /* Opcode: BitAnd P1 P2 P3 * *
9808 ** Synopsis: r[P3]=r[P1]&r[P2]
9809 **
9810 ** Take the bit-wise AND of the values in register P1 and P2 and
9811 ** store the result in register P3.
9812 ** If either input is NULL, the result is NULL.
9813 */
9814 /* Opcode: BitOr P1 P2 P3 * *
9815 ** Synopsis: r[P3]=r[P1]|r[P2]
9816 **
9817 ** Take the bit-wise OR of the values in register P1 and P2 and
9818 ** store the result in register P3.
9819 ** If either input is NULL, the result is NULL.
9820 */
9821 /* Opcode: ShiftLeft P1 P2 P3 * *
9822 ** Synopsis: r[P3]=r[P2]<<r[P1]
9823 **
9824 ** Shift the integer value in register P2 to the left by the
9825 ** number of bits specified by the integer in register P1.
9826 ** Store the result in register P3.
9827 ** If either input is NULL, the result is NULL.
9828 */
9829 /* Opcode: ShiftRight P1 P2 P3 * *
9830 ** Synopsis: r[P3]=r[P2]>>r[P1]
9831 **
9832 ** Shift the integer value in register P2 to the right by the
9833 ** number of bits specified by the integer in register P1.
9834 ** Store the result in register P3.
9835 ** If either input is NULL, the result is NULL.
9836 */
9837 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
9838 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
9839 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
9840 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
9841 i64 iA;
9842 u64 uA;
9843 i64 iB;
9844 u8 op;
9845
9846 pIn1 = &aMem[pOp->p1];
9847 pIn2 = &aMem[pOp->p2];
9848 pOut = &aMem[pOp->p3];
9849 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
9850 sqlite3VdbeMemSetNull(pOut);
9851 break;
9852 }
9853 iA = sqlite3VdbeIntValue(pIn2);
9854 iB = sqlite3VdbeIntValue(pIn1);
9855 op = pOp->opcode;
9856 if( op==OP_BitAnd ){
9857 iA &= iB;
9858 }else if( op==OP_BitOr ){
9859 iA |= iB;
9860 }else if( iB!=0 ){
9861 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
9862
9863 /* If shifting by a negative amount, shift in the other direction */
9864 if( iB<0 ){
9865 assert( OP_ShiftRight==OP_ShiftLeft+1 );
9866 op = 2*OP_ShiftLeft + 1 - op;
9867 iB = iB>(-64) ? -iB : 64;
9868 }
9869
9870 if( iB>=64 ){
9871 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
9872 }else{
9873 memcpy(&uA, &iA, sizeof(uA));
9874 if( op==OP_ShiftLeft ){
9875 uA <<= iB;
9876 }else{
9877 uA >>= iB;
9878 /* Sign-extend on a right shift of a negative number */
9879 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
9880 }
9881 memcpy(&iA, &uA, sizeof(iA));
9882 }
9883 }
9884 pOut->u.i = iA;
9885 MemSetTypeFlag(pOut, MEM_Int);
9886 break;
9887 }
9888
9889 /* Opcode: AddImm P1 P2 * * *
9890 ** Synopsis: r[P1]=r[P1]+P2
9891 **
9892 ** Add the constant P2 to the value in register P1.
9893 ** The result is always an integer.
9894 **
9895 ** To force any register to be an integer, just add 0.
9896 */
9897 case OP_AddImm: { /* in1 */
9898 pIn1 = &aMem[pOp->p1];
9899 memAboutToChange(p, pIn1);
9900 sqlite3VdbeMemIntegerify(pIn1);
9901 pIn1->u.i += pOp->p2;
9902 break;
9903 }
9904
9905 /* Opcode: MustBeInt P1 P2 * * *
9906 **
9907 ** Force the value in register P1 to be an integer. If the value
9908 ** in P1 is not an integer and cannot be converted into an integer
9909 ** without data loss, then jump immediately to P2, or if P2==0
9910 ** raise an SQLITE_MISMATCH exception.
9911 */
9912 case OP_MustBeInt: { /* jump, in1 */
9913 pIn1 = &aMem[pOp->p1];
9914 if( (pIn1->flags & MEM_Int)==0 ){
9915 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
9916 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
9917 if( (pIn1->flags & MEM_Int)==0 ){
9918 if( pOp->p2==0 ){
9919 rc = SQLITE_MISMATCH;
9920 goto abort_due_to_error;
9921 }else{
9922 goto jump_to_p2;
9923 }
9924 }
9925 }
9926 MemSetTypeFlag(pIn1, MEM_Int);
9927 break;
9928 }
9929
9930 #ifndef SQLITE_OMIT_FLOATING_POINT
9931 /* Opcode: RealAffinity P1 * * * *
9932 **
9933 ** If register P1 holds an integer convert it to a real value.
9934 **
9935 ** This opcode is used when extracting information from a column that
9936 ** has REAL affinity. Such column values may still be stored as
9937 ** integers, for space efficiency, but after extraction we want them
9938 ** to have only a real value.
9939 */
9940 case OP_RealAffinity: { /* in1 */
9941 pIn1 = &aMem[pOp->p1];
9942 if( pIn1->flags & MEM_Int ){
9943 sqlite3VdbeMemRealify(pIn1);
9944 }
9945 break;
9946 }
9947 #endif
9948
9949 #ifndef SQLITE_OMIT_CAST
9950 /* Opcode: Cast P1 P2 * * *
9951 ** Synopsis: affinity(r[P1])
9952 **
9953 ** Force the value in register P1 to be the type defined by P2.
9954 **
9955 ** <ul>
9956 ** <li value="97"> TEXT
9957 ** <li value="98"> BLOB
9958 ** <li value="99"> NUMERIC
9959 ** <li value="100"> INTEGER
9960 ** <li value="101"> REAL
9961 ** </ul>
9962 **
9963 ** A NULL value is not changed by this routine. It remains NULL.
9964 */
9965 case OP_Cast: { /* in1 */
9966 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
9967 testcase( pOp->p2==SQLITE_AFF_TEXT );
9968 testcase( pOp->p2==SQLITE_AFF_BLOB );
9969 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
9970 testcase( pOp->p2==SQLITE_AFF_INTEGER );
9971 testcase( pOp->p2==SQLITE_AFF_REAL );
9972 pIn1 = &aMem[pOp->p1];
9973 memAboutToChange(p, pIn1);
9974 rc = ExpandBlob(pIn1);
9975 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
9976 UPDATE_MAX_BLOBSIZE(pIn1);
9977 break;
9978 }
9979 #endif /* SQLITE_OMIT_CAST */
9980
9981 /* Opcode: Lt P1 P2 P3 P4 P5
9982 ** Synopsis: if r[P1]<r[P3] goto P2
9983 **
9984 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
9985 ** jump to address P2.
9986 **
9987 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
9988 ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
9989 ** bit is clear then fall through if either operand is NULL.
9990 **
9991 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
9992 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
9993 ** to coerce both inputs according to this affinity before the
9994 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
9995 ** affinity is used. Note that the affinity conversions are stored
9996 ** back into the input registers P1 and P3. So this opcode can cause
9997 ** persistent changes to registers P1 and P3.
9998 **
9999 ** Once any conversions have taken place, and neither value is NULL,
10000 ** the values are compared. If both values are blobs then memcmp() is
10001 ** used to determine the results of the comparison. If both values
10002 ** are text, then the appropriate collating function specified in
10003 ** P4 is used to do the comparison. If P4 is not specified then
10004 ** memcmp() is used to compare text string. If both values are
10005 ** numeric, then a numeric comparison is used. If the two values
10006 ** are of different types, then numbers are considered less than
10007 ** strings and strings are considered less than blobs.
10008 **
10009 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
10010 ** store a boolean result (either 0, or 1, or NULL) in register P2.
10011 **
10012 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
10013 ** equal to one another, provided that they do not have their MEM_Cleared
10014 ** bit set.
10015 */
10016 /* Opcode: Ne P1 P2 P3 P4 P5
10017 ** Synopsis: if r[P1]!=r[P3] goto P2
10018 **
10019 ** This works just like the Lt opcode except that the jump is taken if
10020 ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
10021 ** additional information.
10022 **
10023 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
10024 ** true or false and is never NULL. If both operands are NULL then the result
10025 ** of comparison is false. If either operand is NULL then the result is true.
10026 ** If neither operand is NULL the result is the same as it would be if
10027 ** the SQLITE_NULLEQ flag were omitted from P5.
10028 */
10029 /* Opcode: Eq P1 P2 P3 P4 P5
10030 ** Synopsis: if r[P1]==r[P3] goto P2
10031 **
10032 ** This works just like the Lt opcode except that the jump is taken if
10033 ** the operands in registers P1 and P3 are equal.
10034 ** See the Lt opcode for additional information.
10035 **
10036 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
10037 ** true or false and is never NULL. If both operands are NULL then the result
10038 ** of comparison is true. If either operand is NULL then the result is false.
10039 ** If neither operand is NULL the result is the same as it would be if
10040 ** the SQLITE_NULLEQ flag were omitted from P5.
10041 */
10042 /* Opcode: Le P1 P2 P3 P4 P5
10043 ** Synopsis: if r[P1]<=r[P3] goto P2
10044 **
10045 ** This works just like the Lt opcode except that the jump is taken if
10046 ** the content of register P3 is less than or equal to the content of
10047 ** register P1. See the Lt opcode for additional information.
10048 */
10049 /* Opcode: Gt P1 P2 P3 P4 P5
10050 ** Synopsis: if r[P1]>r[P3] goto P2
10051 **
10052 ** This works just like the Lt opcode except that the jump is taken if
10053 ** the content of register P3 is greater than the content of
10054 ** register P1. See the Lt opcode for additional information.
10055 */
10056 /* Opcode: Ge P1 P2 P3 P4 P5
10057 ** Synopsis: if r[P1]>=r[P3] goto P2
10058 **
10059 ** This works just like the Lt opcode except that the jump is taken if
10060 ** the content of register P3 is greater than or equal to the content of
10061 ** register P1. See the Lt opcode for additional information.
10062 */
10063 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
10064 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
10065 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
10066 case OP_Le: /* same as TK_LE, jump, in1, in3 */
10067 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
10068 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
10069 int res; /* Result of the comparison of pIn1 against pIn3 */
10070 char affinity; /* Affinity to use for comparison */
10071 u16 flags1; /* Copy of initial value of pIn1->flags */
10072 u16 flags3; /* Copy of initial value of pIn3->flags */
10073
10074 pIn1 = &aMem[pOp->p1];
10075 pIn3 = &aMem[pOp->p3];
10076 flags1 = pIn1->flags;
10077 flags3 = pIn3->flags;
10078 if( (flags1 | flags3)&MEM_Null ){
10079 /* One or both operands are NULL */
10080 if( pOp->p5 & SQLITE_NULLEQ ){
10081 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
10082 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
10083 ** or not both operands are null.
10084 */
10085 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
10086 assert( (flags1 & MEM_Cleared)==0 );
10087 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
10088 if( (flags1&MEM_Null)!=0
10089 && (flags3&MEM_Null)!=0
10090 && (flags3&MEM_Cleared)==0
10091 ){
10092 res = 0; /* Results are equal */
10093 }else{
10094 res = 1; /* Results are not equal */
10095 }
10096 }else{
10097 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
10098 ** then the result is always NULL.
10099 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
10100 */
10101 if( pOp->p5 & SQLITE_STOREP2 ){
10102 pOut = &aMem[pOp->p2];
10103 memAboutToChange(p, pOut);
10104 MemSetTypeFlag(pOut, MEM_Null);
10105 REGISTER_TRACE(pOp->p2, pOut);
10106 }else{
10107 VdbeBranchTaken(2,3);
10108 if( pOp->p5 & SQLITE_JUMPIFNULL ){
10109 goto jump_to_p2;
10110 }
10111 }
10112 break;
10113 }
10114 }else{
10115 /* Neither operand is NULL. Do a comparison. */
10116 affinity = pOp->p5 & SQLITE_AFF_MASK;
10117 if( affinity>=SQLITE_AFF_NUMERIC ){
10118 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
10119 applyNumericAffinity(pIn1,0);
10120 }
10121 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
10122 applyNumericAffinity(pIn3,0);
10123 }
10124 }else if( affinity==SQLITE_AFF_TEXT ){
10125 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
10126 testcase( pIn1->flags & MEM_Int );
10127 testcase( pIn1->flags & MEM_Real );
10128 sqlite3VdbeMemStringify(pIn1, encoding, 1);
10129 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
10130 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
10131 }
10132 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
10133 testcase( pIn3->flags & MEM_Int );
10134 testcase( pIn3->flags & MEM_Real );
10135 sqlite3VdbeMemStringify(pIn3, encoding, 1);
10136 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
10137 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
10138 }
10139 }
10140 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
10141 if( flags1 & MEM_Zero ){
10142 sqlite3VdbeMemExpandBlob(pIn1);
10143 flags1 &= ~MEM_Zero;
10144 }
10145 if( flags3 & MEM_Zero ){
10146 sqlite3VdbeMemExpandBlob(pIn3);
10147 flags3 &= ~MEM_Zero;
10148 }
10149 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
10150 }
10151 switch( pOp->opcode ){
10152 case OP_Eq: res = res==0; break;
10153 case OP_Ne: res = res!=0; break;
10154 case OP_Lt: res = res<0; break;
10155 case OP_Le: res = res<=0; break;
10156 case OP_Gt: res = res>0; break;
10157 default: res = res>=0; break;
10158 }
10159
10160 /* Undo any changes made by applyAffinity() to the input registers. */
10161 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
10162 pIn1->flags = flags1;
10163 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
10164 pIn3->flags = flags3;
10165
10166 if( pOp->p5 & SQLITE_STOREP2 ){
10167 pOut = &aMem[pOp->p2];
10168 memAboutToChange(p, pOut);
10169 MemSetTypeFlag(pOut, MEM_Int);
10170 pOut->u.i = res;
10171 REGISTER_TRACE(pOp->p2, pOut);
10172 }else{
10173 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
10174 if( res ){
10175 goto jump_to_p2;
10176 }
10177 }
10178 break;
10179 }
10180
10181 /* Opcode: Permutation * * * P4 *
10182 **
10183 ** Set the permutation used by the OP_Compare operator to be the array
10184 ** of integers in P4.
10185 **
10186 ** The permutation is only valid until the next OP_Compare that has
10187 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
10188 ** occur immediately prior to the OP_Compare.
10189 */
10190 case OP_Permutation: {
10191 assert( pOp->p4type==P4_INTARRAY );
10192 assert( pOp->p4.ai );
10193 aPermute = pOp->p4.ai;
10194 break;
10195 }
10196
10197 /* Opcode: Compare P1 P2 P3 P4 P5
10198 ** Synopsis: r[P1@P3] <-> r[P2@P3]
10199 **
10200 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
10201 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
10202 ** the comparison for use by the next OP_Jump instruct.
10203 **
10204 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
10205 ** determined by the most recent OP_Permutation operator. If the
10206 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
10207 ** order.
10208 **
10209 ** P4 is a KeyInfo structure that defines collating sequences and sort
10210 ** orders for the comparison. The permutation applies to registers
10211 ** only. The KeyInfo elements are used sequentially.
10212 **
10213 ** The comparison is a sort comparison, so NULLs compare equal,
10214 ** NULLs are less than numbers, numbers are less than strings,
10215 ** and strings are less than blobs.
10216 */
10217 case OP_Compare: {
10218 int n;
10219 int i;
10220 int p1;
10221 int p2;
10222 const KeyInfo *pKeyInfo;
10223 int idx;
10224 CollSeq *pColl; /* Collating sequence to use on this term */
10225 int bRev; /* True for DESCENDING sort order */
10226
10227 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
10228 n = pOp->p3;
10229 pKeyInfo = pOp->p4.pKeyInfo;
10230 assert( n>0 );
10231 assert( pKeyInfo!=0 );
10232 p1 = pOp->p1;
10233 p2 = pOp->p2;
10234 #if SQLITE_DEBUG
10235 if( aPermute ){
10236 int k, mx = 0;
10237 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
10238 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
10239 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
10240 }else{
10241 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
10242 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
10243 }
10244 #endif /* SQLITE_DEBUG */
10245 for(i=0; i<n; i++){
10246 idx = aPermute ? aPermute[i] : i;
10247 assert( memIsValid(&aMem[p1+idx]) );
10248 assert( memIsValid(&aMem[p2+idx]) );
10249 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
10250 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
10251 assert( i<pKeyInfo->nField );
10252 pColl = pKeyInfo->aColl[i];
10253 bRev = pKeyInfo->aSortOrder[i];
10254 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
10255 if( iCompare ){
10256 if( bRev ) iCompare = -iCompare;
10257 break;
10258 }
10259 }
10260 aPermute = 0;
10261 break;
10262 }
10263
10264 /* Opcode: Jump P1 P2 P3 * *
10265 **
10266 ** Jump to the instruction at address P1, P2, or P3 depending on whether
10267 ** in the most recent OP_Compare instruction the P1 vector was less than
10268 ** equal to, or greater than the P2 vector, respectively.
10269 */
10270 case OP_Jump: { /* jump */
10271 if( iCompare<0 ){
10272 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
10273 }else if( iCompare==0 ){
10274 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
10275 }else{
10276 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
10277 }
10278 break;
10279 }
10280
10281 /* Opcode: And P1 P2 P3 * *
10282 ** Synopsis: r[P3]=(r[P1] && r[P2])
10283 **
10284 ** Take the logical AND of the values in registers P1 and P2 and
10285 ** write the result into register P3.
10286 **
10287 ** If either P1 or P2 is 0 (false) then the result is 0 even if
10288 ** the other input is NULL. A NULL and true or two NULLs give
10289 ** a NULL output.
10290 */
10291 /* Opcode: Or P1 P2 P3 * *
10292 ** Synopsis: r[P3]=(r[P1] || r[P2])
10293 **
10294 ** Take the logical OR of the values in register P1 and P2 and
10295 ** store the answer in register P3.
10296 **
10297 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
10298 ** even if the other input is NULL. A NULL and false or two NULLs
10299 ** give a NULL output.
10300 */
10301 case OP_And: /* same as TK_AND, in1, in2, out3 */
10302 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
10303 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
10304 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
10305
10306 pIn1 = &aMem[pOp->p1];
10307 if( pIn1->flags & MEM_Null ){
10308 v1 = 2;
10309 }else{
10310 v1 = sqlite3VdbeIntValue(pIn1)!=0;
10311 }
10312 pIn2 = &aMem[pOp->p2];
10313 if( pIn2->flags & MEM_Null ){
10314 v2 = 2;
10315 }else{
10316 v2 = sqlite3VdbeIntValue(pIn2)!=0;
10317 }
10318 if( pOp->opcode==OP_And ){
10319 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
10320 v1 = and_logic[v1*3+v2];
10321 }else{
10322 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
10323 v1 = or_logic[v1*3+v2];
10324 }
10325 pOut = &aMem[pOp->p3];
10326 if( v1==2 ){
10327 MemSetTypeFlag(pOut, MEM_Null);
10328 }else{
10329 pOut->u.i = v1;
10330 MemSetTypeFlag(pOut, MEM_Int);
10331 }
10332 break;
10333 }
10334
10335 /* Opcode: Not P1 P2 * * *
10336 ** Synopsis: r[P2]= !r[P1]
10337 **
10338 ** Interpret the value in register P1 as a boolean value. Store the
10339 ** boolean complement in register P2. If the value in register P1 is
10340 ** NULL, then a NULL is stored in P2.
10341 */
10342 case OP_Not: { /* same as TK_NOT, in1, out2 */
10343 pIn1 = &aMem[pOp->p1];
10344 pOut = &aMem[pOp->p2];
10345 sqlite3VdbeMemSetNull(pOut);
10346 if( (pIn1->flags & MEM_Null)==0 ){
10347 pOut->flags = MEM_Int;
10348 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
10349 }
10350 break;
10351 }
10352
10353 /* Opcode: BitNot P1 P2 * * *
10354 ** Synopsis: r[P1]= ~r[P1]
10355 **
10356 ** Interpret the content of register P1 as an integer. Store the
10357 ** ones-complement of the P1 value into register P2. If P1 holds
10358 ** a NULL then store a NULL in P2.
10359 */
10360 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
10361 pIn1 = &aMem[pOp->p1];
10362 pOut = &aMem[pOp->p2];
10363 sqlite3VdbeMemSetNull(pOut);
10364 if( (pIn1->flags & MEM_Null)==0 ){
10365 pOut->flags = MEM_Int;
10366 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
10367 }
10368 break;
10369 }
10370
10371 /* Opcode: Once P1 P2 * * *
10372 **
10373 ** Check the "once" flag number P1. If it is set, jump to instruction P2.
10374 ** Otherwise, set the flag and fall through to the next instruction.
10375 ** In other words, this opcode causes all following opcodes up through P2
10376 ** (but not including P2) to run just once and to be skipped on subsequent
10377 ** times through the loop.
10378 **
10379 ** All "once" flags are initially cleared whenever a prepared statement
10380 ** first begins to run.
10381 */
10382 case OP_Once: { /* jump */
10383 assert( pOp->p1<p->nOnceFlag );
10384 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
10385 if( p->aOnceFlag[pOp->p1] ){
10386 goto jump_to_p2;
10387 }else{
10388 p->aOnceFlag[pOp->p1] = 1;
10389 }
10390 break;
10391 }
10392
10393 /* Opcode: If P1 P2 P3 * *
10394 **
10395 ** Jump to P2 if the value in register P1 is true. The value
10396 ** is considered true if it is numeric and non-zero. If the value
10397 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
10398 */
10399 /* Opcode: IfNot P1 P2 P3 * *
10400 **
10401 ** Jump to P2 if the value in register P1 is False. The value
10402 ** is considered false if it has a numeric value of zero. If the value
10403 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
10404 */
10405 case OP_If: /* jump, in1 */
10406 case OP_IfNot: { /* jump, in1 */
10407 int c;
10408 pIn1 = &aMem[pOp->p1];
10409 if( pIn1->flags & MEM_Null ){
10410 c = pOp->p3;
10411 }else{
10412 #ifdef SQLITE_OMIT_FLOATING_POINT
10413 c = sqlite3VdbeIntValue(pIn1)!=0;
10414 #else
10415 c = sqlite3VdbeRealValue(pIn1)!=0.0;
10416 #endif
10417 if( pOp->opcode==OP_IfNot ) c = !c;
10418 }
10419 VdbeBranchTaken(c!=0, 2);
10420 if( c ){
10421 goto jump_to_p2;
10422 }
10423 break;
10424 }
10425
10426 /* Opcode: IsNull P1 P2 * * *
10427 ** Synopsis: if r[P1]==NULL goto P2
10428 **
10429 ** Jump to P2 if the value in register P1 is NULL.
10430 */
10431 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
10432 pIn1 = &aMem[pOp->p1];
10433 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
10434 if( (pIn1->flags & MEM_Null)!=0 ){
10435 goto jump_to_p2;
10436 }
10437 break;
10438 }
10439
10440 /* Opcode: NotNull P1 P2 * * *
10441 ** Synopsis: if r[P1]!=NULL goto P2
10442 **
10443 ** Jump to P2 if the value in register P1 is not NULL.
10444 */
10445 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
10446 pIn1 = &aMem[pOp->p1];
10447 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
10448 if( (pIn1->flags & MEM_Null)==0 ){
10449 goto jump_to_p2;
10450 }
10451 break;
10452 }
10453
10454 /* Opcode: Column P1 P2 P3 P4 P5
10455 ** Synopsis: r[P3]=PX
10456 **
10457 ** Interpret the data that cursor P1 points to as a structure built using
10458 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
10459 ** information about the format of the data.) Extract the P2-th column
10460 ** from this record. If there are less that (P2+1)
10461 ** values in the record, extract a NULL.
10462 **
10463 ** The value extracted is stored in register P3.
10464 **
10465 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
10466 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
10467 ** the result.
10468 **
10469 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
10470 ** then the cache of the cursor is reset prior to extracting the column.
10471 ** The first OP_Column against a pseudo-table after the value of the content
10472 ** register has changed should have this bit set.
10473 **
10474 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
10475 ** the result is guaranteed to only be used as the argument of a length()
10476 ** or typeof() function, respectively. The loading of large blobs can be
10477 ** skipped for length() and all content loading can be skipped for typeof().
10478 */
10479 case OP_Column: {
10480 i64 payloadSize64; /* Number of bytes in the record */
10481 int p2; /* column number to retrieve */
10482 VdbeCursor *pC; /* The VDBE cursor */
10483 BtCursor *pCrsr; /* The BTree cursor */
10484 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
10485 int len; /* The length of the serialized data for the column */
10486 int i; /* Loop counter */
10487 Mem *pDest; /* Where to write the extracted value */
10488 Mem sMem; /* For storing the record being decoded */
10489 const u8 *zData; /* Part of the record being decoded */
10490 const u8 *zHdr; /* Next unparsed byte of the header */
10491 const u8 *zEndHdr; /* Pointer to first byte after the header */
10492 u32 offset; /* Offset into the data */
10493 u64 offset64; /* 64-bit offset */
10494 u32 avail; /* Number of bytes of available data */
10495 u32 t; /* A type code from the record header */
10496 u16 fx; /* pDest->flags value */
10497 Mem *pReg; /* PseudoTable input register */
10498
10499 p2 = pOp->p2;
10500 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
10501 pDest = &aMem[pOp->p3];
10502 memAboutToChange(p, pDest);
10503 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
10504 pC = p->apCsr[pOp->p1];
10505 assert( pC!=0 );
10506 assert( p2<pC->nField );
10507 aOffset = pC->aOffset;
10508 assert( pC->eCurType!=CURTYPE_VTAB );
10509 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
10510 assert( pC->eCurType!=CURTYPE_SORTER );
10511 pCrsr = pC->uc.pCursor;
10512
10513 /* If the cursor cache is stale, bring it up-to-date */
10514 rc = sqlite3VdbeCursorMoveto(pC);
10515 if( rc ) goto abort_due_to_error;
10516 if( pC->cacheStatus!=p->cacheCtr ){
10517 if( pC->nullRow ){
10518 if( pC->eCurType==CURTYPE_PSEUDO ){
10519 assert( pC->uc.pseudoTableReg>0 );
10520 pReg = &aMem[pC->uc.pseudoTableReg];
10521 assert( pReg->flags & MEM_Blob );
10522 assert( memIsValid(pReg) );
10523 pC->payloadSize = pC->szRow = avail = pReg->n;
10524 pC->aRow = (u8*)pReg->z;
10525 }else{
10526 sqlite3VdbeMemSetNull(pDest);
10527 goto op_column_out;
10528 }
10529 }else{
10530 assert( pC->eCurType==CURTYPE_BTREE );
10531 assert( pCrsr );
10532 if( pC->isTable==0 ){
10533 assert( sqlite3BtreeCursorIsValid(pCrsr) );
10534 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
10535 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
10536 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
10537 ** payload size, so it is impossible for payloadSize64 to be
10538 ** larger than 32 bits. */
10539 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
10540 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
10541 pC->payloadSize = (u32)payloadSize64;
10542 }else{
10543 assert( sqlite3BtreeCursorIsValid(pCrsr) );
10544 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
10545 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
10546 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
10547 }
10548 assert( avail<=65536 ); /* Maximum page size is 64KiB */
10549 if( pC->payloadSize <= (u32)avail ){
10550 pC->szRow = pC->payloadSize;
10551 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
10552 goto too_big;
10553 }else{
10554 pC->szRow = avail;
10555 }
10556 }
10557 pC->cacheStatus = p->cacheCtr;
10558 pC->iHdrOffset = getVarint32(pC->aRow, offset);
10559 pC->nHdrParsed = 0;
10560 aOffset[0] = offset;
10561
10562
10563 if( avail<offset ){
10564 /* pC->aRow does not have to hold the entire row, but it does at least
10565 ** need to cover the header of the record. If pC->aRow does not contain
10566 ** the complete header, then set it to zero, forcing the header to be
10567 ** dynamically allocated. */
10568 pC->aRow = 0;
10569 pC->szRow = 0;
10570
10571 /* Make sure a corrupt database has not given us an oversize header.
10572 ** Do this now to avoid an oversize memory allocation.
10573 **
10574 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
10575 ** types use so much data space that there can only be 4096 and 32 of
10576 ** them, respectively. So the maximum header length results from a
10577 ** 3-byte type for each of the maximum of 32768 columns plus three
10578 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
10579 */
10580 if( offset > 98307 || offset > pC->payloadSize ){
10581 rc = SQLITE_CORRUPT_BKPT;
10582 goto op_column_error;
10583 }
10584 }
10585
10586 /* The following goto is an optimization. It can be omitted and
10587 ** everything will still work. But OP_Column is measurably faster
10588 ** by skipping the subsequent conditional, which is always true.
10589 */
10590 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
10591 goto op_column_read_header;
10592 }
10593
10594 /* Make sure at least the first p2+1 entries of the header have been
10595 ** parsed and valid information is in aOffset[] and pC->aType[].
10596 */
10597 if( pC->nHdrParsed<=p2 ){
10598 /* If there is more header available for parsing in the record, try
10599 ** to extract additional fields up through the p2+1-th field
10600 */
10601 op_column_read_header:
10602 if( pC->iHdrOffset<aOffset[0] ){
10603 /* Make sure zData points to enough of the record to cover the header. */
10604 if( pC->aRow==0 ){
10605 memset(&sMem, 0, sizeof(sMem));
10606 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem);
10607 if( rc!=SQLITE_OK ) goto op_column_error;
10608 zData = (u8*)sMem.z;
10609 }else{
10610 zData = pC->aRow;
10611 }
10612
10613 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
10614 i = pC->nHdrParsed;
10615 offset64 = aOffset[i];
10616 zHdr = zData + pC->iHdrOffset;
10617 zEndHdr = zData + aOffset[0];
10618 assert( i<=p2 && zHdr<zEndHdr );
10619 do{
10620 if( (t = zHdr[0])<0x80 ){
10621 zHdr++;
10622 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
10623 }else{
10624 zHdr += sqlite3GetVarint32(zHdr, &t);
10625 offset64 += sqlite3VdbeSerialTypeLen(t);
10626 }
10627 pC->aType[i++] = t;
10628 aOffset[i] = (u32)(offset64 & 0xffffffff);
10629 }while( i<=p2 && zHdr<zEndHdr );
10630 pC->nHdrParsed = i;
10631 pC->iHdrOffset = (u32)(zHdr - zData);
10632 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
10633
10634 /* The record is corrupt if any of the following are true:
10635 ** (1) the bytes of the header extend past the declared header size
10636 ** (2) the entire header was used but not all data was used
10637 ** (3) the end of the data extends beyond the end of the record.
10638 */
10639 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
10640 || (offset64 > pC->payloadSize)
10641 ){
10642 rc = SQLITE_CORRUPT_BKPT;
10643 goto op_column_error;
10644 }
10645 }else{
10646 t = 0;
10647 }
10648
10649 /* If after trying to extract new entries from the header, nHdrParsed is
10650 ** still not up to p2, that means that the record has fewer than p2
10651 ** columns. So the result will be either the default value or a NULL.
10652 */
10653 if( pC->nHdrParsed<=p2 ){
10654 if( pOp->p4type==P4_MEM ){
10655 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
10656 }else{
10657 sqlite3VdbeMemSetNull(pDest);
10658 }
10659 goto op_column_out;
10660 }
10661 }else{
10662 t = pC->aType[p2];
10663 }
10664
10665 /* Extract the content for the p2+1-th column. Control can only
10666 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
10667 ** all valid.
10668 */
10669 assert( p2<pC->nHdrParsed );
10670 assert( rc==SQLITE_OK );
10671 assert( sqlite3VdbeCheckMemInvariants(pDest) );
10672 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
10673 assert( t==pC->aType[p2] );
10674 if( pC->szRow>=aOffset[p2+1] ){
10675 /* This is the common case where the desired content fits on the original
10676 ** page - where the content is not on an overflow page */
10677 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
10678 }else{
10679 /* This branch happens only when content is on overflow pages */
10680 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
10681 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
10682 || (len = sqlite3VdbeSerialTypeLen(t))==0
10683 ){
10684 /* Content is irrelevant for
10685 ** 1. the typeof() function,
10686 ** 2. the length(X) function if X is a blob, and
10687 ** 3. if the content length is zero.
10688 ** So we might as well use bogus content rather than reading
10689 ** content from disk. NULL will work for the value for strings
10690 ** and blobs and whatever is in the payloadSize64 variable
10691 ** will work for everything else. */
10692 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
10693 }else{
10694 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
10695 pDest);
10696 if( rc!=SQLITE_OK ){
10697 goto op_column_error;
10698 }
10699 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
10700 pDest->flags &= ~MEM_Ephem;
10701 }
10702 }
10703 pDest->enc = encoding;
10704
10705 op_column_out:
10706 /* If the column value is an ephemeral string, go ahead and persist
10707 ** that string in case the cursor moves before the column value is
10708 ** used. The following code does the equivalent of Deephemeralize()
10709 ** but does it faster. */
10710 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
10711 fx = pDest->flags & (MEM_Str|MEM_Blob);
10712 assert( fx!=0 );
10713 zData = (const u8*)pDest->z;
10714 len = pDest->n;
10715 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
10716 memcpy(pDest->z, zData, len);
10717 pDest->z[len] = 0;
10718 pDest->z[len+1] = 0;
10719 pDest->flags = fx|MEM_Term;
10720 }
10721 op_column_error:
10722 UPDATE_MAX_BLOBSIZE(pDest);
10723 REGISTER_TRACE(pOp->p3, pDest);
10724 break;
10725 }
10726
10727 /* Opcode: Affinity P1 P2 * P4 *
10728 ** Synopsis: affinity(r[P1@P2])
10729 **
10730 ** Apply affinities to a range of P2 registers starting with P1.
10731 **
10732 ** P4 is a string that is P2 characters long. The nth character of the
10733 ** string indicates the column affinity that should be used for the nth
10734 ** memory cell in the range.
10735 */
10736 case OP_Affinity: {
10737 const char *zAffinity; /* The affinity to be applied */
10738 char cAff; /* A single character of affinity */
10739
10740 zAffinity = pOp->p4.z;
10741 assert( zAffinity!=0 );
10742 assert( zAffinity[pOp->p2]==0 );
10743 pIn1 = &aMem[pOp->p1];
10744 while( (cAff = *(zAffinity++))!=0 ){
10745 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
10746 assert( memIsValid(pIn1) );
10747 applyAffinity(pIn1, cAff, encoding);
10748 pIn1++;
10749 }
10750 break;
10751 }
10752
10753 /* Opcode: MakeRecord P1 P2 P3 P4 *
10754 ** Synopsis: r[P3]=mkrec(r[P1@P2])
10755 **
10756 ** Convert P2 registers beginning with P1 into the [record format]
10757 ** use as a data record in a database table or as a key
10758 ** in an index. The OP_Column opcode can decode the record later.
10759 **
10760 ** P4 may be a string that is P2 characters long. The nth character of the
10761 ** string indicates the column affinity that should be used for the nth
10762 ** field of the index key.
10763 **
10764 ** The mapping from character to affinity is given by the SQLITE_AFF_
10765 ** macros defined in sqliteInt.h.
10766 **
10767 ** If P4 is NULL then all index fields have the affinity BLOB.
10768 */
10769 case OP_MakeRecord: {
10770 u8 *zNewRecord; /* A buffer to hold the data for the new record */
10771 Mem *pRec; /* The new record */
10772 u64 nData; /* Number of bytes of data space */
10773 int nHdr; /* Number of bytes of header space */
10774 i64 nByte; /* Data space required for this record */
10775 i64 nZero; /* Number of zero bytes at the end of the record */
10776 int nVarint; /* Number of bytes in a varint */
10777 u32 serial_type; /* Type field */
10778 Mem *pData0; /* First field to be combined into the record */
10779 Mem *pLast; /* Last field of the record */
10780 int nField; /* Number of fields in the record */
10781 char *zAffinity; /* The affinity string for the record */
10782 int file_format; /* File format to use for encoding */
10783 int i; /* Space used in zNewRecord[] header */
10784 int j; /* Space used in zNewRecord[] content */
10785 u32 len; /* Length of a field */
10786
10787 /* Assuming the record contains N fields, the record format looks
10788 ** like this:
10789 **
10790 ** ------------------------------------------------------------------------
10791 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
10792 ** ------------------------------------------------------------------------
10793 **
10794 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
10795 ** and so forth.
10796 **
10797 ** Each type field is a varint representing the serial type of the
10798 ** corresponding data element (see sqlite3VdbeSerialType()). The
10799 ** hdr-size field is also a varint which is the offset from the beginning
10800 ** of the record to data0.
10801 */
10802 nData = 0; /* Number of bytes of data space */
10803 nHdr = 0; /* Number of bytes of header space */
10804 nZero = 0; /* Number of zero bytes at the end of the record */
10805 nField = pOp->p1;
10806 zAffinity = pOp->p4.z;
10807 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
10808 pData0 = &aMem[nField];
10809 nField = pOp->p2;
10810 pLast = &pData0[nField-1];
10811 file_format = p->minWriteFileFormat;
10812
10813 /* Identify the output register */
10814 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
10815 pOut = &aMem[pOp->p3];
10816 memAboutToChange(p, pOut);
10817
10818 /* Apply the requested affinity to all inputs
10819 */
10820 assert( pData0<=pLast );
10821 if( zAffinity ){
10822 pRec = pData0;
10823 do{
10824 applyAffinity(pRec++, *(zAffinity++), encoding);
10825 assert( zAffinity[0]==0 || pRec<=pLast );
10826 }while( zAffinity[0] );
10827 }
10828
10829 /* Loop through the elements that will make up the record to figure
10830 ** out how much space is required for the new record.
10831 */
10832 pRec = pLast;
10833 do{
10834 assert( memIsValid(pRec) );
10835 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
10836 if( pRec->flags & MEM_Zero ){
10837 if( nData ){
10838 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
10839 }else{
10840 nZero += pRec->u.nZero;
10841 len -= pRec->u.nZero;
10842 }
10843 }
10844 nData += len;
10845 testcase( serial_type==127 );
10846 testcase( serial_type==128 );
10847 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
10848 }while( (--pRec)>=pData0 );
10849
10850 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
10851 ** which determines the total number of bytes in the header. The varint
10852 ** value is the size of the header in bytes including the size varint
10853 ** itself. */
10854 testcase( nHdr==126 );
10855 testcase( nHdr==127 );
10856 if( nHdr<=126 ){
10857 /* The common case */
10858 nHdr += 1;
10859 }else{
10860 /* Rare case of a really large header */
10861 nVarint = sqlite3VarintLen(nHdr);
10862 nHdr += nVarint;
10863 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
10864 }
10865 nByte = nHdr+nData;
10866 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
10867 goto too_big;
10868 }
10869
10870 /* Make sure the output register has a buffer large enough to store
10871 ** the new record. The output register (pOp->p3) is not allowed to
10872 ** be one of the input registers (because the following call to
10873 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
10874 */
10875 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
10876 goto no_mem;
10877 }
10878 zNewRecord = (u8 *)pOut->z;
10879
10880 /* Write the record */
10881 i = putVarint32(zNewRecord, nHdr);
10882 j = nHdr;
10883 assert( pData0<=pLast );
10884 pRec = pData0;
10885 do{
10886 serial_type = pRec->uTemp;
10887 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
10888 ** additional varints, one per column. */
10889 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
10890 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
10891 ** immediately follow the header. */
10892 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
10893 }while( (++pRec)<=pLast );
10894 assert( i==nHdr );
10895 assert( j==nByte );
10896
10897 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
10898 pOut->n = (int)nByte;
10899 pOut->flags = MEM_Blob;
10900 if( nZero ){
10901 pOut->u.nZero = nZero;
10902 pOut->flags |= MEM_Zero;
10903 }
10904 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
10905 REGISTER_TRACE(pOp->p3, pOut);
10906 UPDATE_MAX_BLOBSIZE(pOut);
10907 break;
10908 }
10909
10910 /* Opcode: Count P1 P2 * * *
10911 ** Synopsis: r[P2]=count()
10912 **
10913 ** Store the number of entries (an integer value) in the table or index
10914 ** opened by cursor P1 in register P2
10915 */
10916 #ifndef SQLITE_OMIT_BTREECOUNT
10917 case OP_Count: { /* out2 */
10918 i64 nEntry;
10919 BtCursor *pCrsr;
10920
10921 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
10922 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
10923 assert( pCrsr );
10924 nEntry = 0; /* Not needed. Only used to silence a warning. */
10925 rc = sqlite3BtreeCount(pCrsr, &nEntry);
10926 pOut = out2Prerelease(p, pOp);
10927 pOut->u.i = nEntry;
10928 break;
10929 }
10930 #endif
10931
10932 /* Opcode: Savepoint P1 * * P4 *
10933 **
10934 ** Open, release or rollback the savepoint named by parameter P4, depending
10935 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
10936 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
10937 */
10938 case OP_Savepoint: {
10939 int p1; /* Value of P1 operand */
10940 char *zName; /* Name of savepoint */
10941 int nName;
10942 Savepoint *pNew;
10943 Savepoint *pSavepoint;
10944 Savepoint *pTmp;
10945 int iSavepoint;
10946 int ii;
10947
10948 p1 = pOp->p1;
10949 zName = pOp->p4.z;
10950
10951 /* Assert that the p1 parameter is valid. Also that if there is no open
10952 ** transaction, then there cannot be any savepoints.
10953 */
10954 assert( db->pSavepoint==0 || db->autoCommit==0 );
10955 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
10956 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
10957 assert( checkSavepointCount(db) );
10958 assert( p->bIsReader );
10959
10960 if( p1==SAVEPOINT_BEGIN ){
10961 if( db->nVdbeWrite>0 ){
10962 /* A new savepoint cannot be created if there are active write
10963 ** statements (i.e. open read/write incremental blob handles).
10964 */
10965 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
10966 rc = SQLITE_BUSY;
10967 }else{
10968 nName = sqlite3Strlen30(zName);
10969
10970 #ifndef SQLITE_OMIT_VIRTUALTABLE
10971 /* This call is Ok even if this savepoint is actually a transaction
10972 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
10973 ** If this is a transaction savepoint being opened, it is guaranteed
10974 ** that the db->aVTrans[] array is empty. */
10975 assert( db->autoCommit==0 || db->nVTrans==0 );
10976 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
10977 db->nStatement+db->nSavepoint);
10978 if( rc!=SQLITE_OK ) goto abort_due_to_error;
10979 #endif
10980
10981 /* Create a new savepoint structure. */
10982 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
10983 if( pNew ){
10984 pNew->zName = (char *)&pNew[1];
10985 memcpy(pNew->zName, zName, nName+1);
10986
10987 /* If there is no open transaction, then mark this as a special
10988 ** "transaction savepoint". */
10989 if( db->autoCommit ){
10990 db->autoCommit = 0;
10991 db->isTransactionSavepoint = 1;
10992 }else{
10993 db->nSavepoint++;
10994 }
10995
10996 /* Link the new savepoint into the database handle's list. */
10997 pNew->pNext = db->pSavepoint;
10998 db->pSavepoint = pNew;
10999 pNew->nDeferredCons = db->nDeferredCons;
11000 pNew->nDeferredImmCons = db->nDeferredImmCons;
11001 }
11002 }
11003 }else{
11004 iSavepoint = 0;
11005
11006 /* Find the named savepoint. If there is no such savepoint, then an
11007 ** an error is returned to the user. */
11008 for(
11009 pSavepoint = db->pSavepoint;
11010 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
11011 pSavepoint = pSavepoint->pNext
11012 ){
11013 iSavepoint++;
11014 }
11015 if( !pSavepoint ){
11016 sqlite3VdbeError(p, "no such savepoint: %s", zName);
11017 rc = SQLITE_ERROR;
11018 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
11019 /* It is not possible to release (commit) a savepoint if there are
11020 ** active write statements.
11021 */
11022 sqlite3VdbeError(p, "cannot release savepoint - "
11023 "SQL statements in progress");
11024 rc = SQLITE_BUSY;
11025 }else{
11026
11027 /* Determine whether or not this is a transaction savepoint. If so,
11028 ** and this is a RELEASE command, then the current transaction
11029 ** is committed.
11030 */
11031 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
11032 if( isTransaction && p1==SAVEPOINT_RELEASE ){
11033 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
11034 goto vdbe_return;
11035 }
11036 db->autoCommit = 1;
11037 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
11038 p->pc = (int)(pOp - aOp);
11039 db->autoCommit = 0;
11040 p->rc = rc = SQLITE_BUSY;
11041 goto vdbe_return;
11042 }
11043 db->isTransactionSavepoint = 0;
11044 rc = p->rc;
11045 }else{
11046 int isSchemaChange;
11047 iSavepoint = db->nSavepoint - iSavepoint - 1;
11048 if( p1==SAVEPOINT_ROLLBACK ){
11049 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
11050 for(ii=0; ii<db->nDb; ii++){
11051 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
11052 SQLITE_ABORT_ROLLBACK,
11053 isSchemaChange==0);
11054 if( rc!=SQLITE_OK ) goto abort_due_to_error;
11055 }
11056 }else{
11057 isSchemaChange = 0;
11058 }
11059 for(ii=0; ii<db->nDb; ii++){
11060 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
11061 if( rc!=SQLITE_OK ){
11062 goto abort_due_to_error;
11063 }
11064 }
11065 if( isSchemaChange ){
11066 sqlite3ExpirePreparedStatements(db);
11067 sqlite3ResetAllSchemasOfConnection(db);
11068 db->flags = (db->flags | SQLITE_InternChanges);
11069 }
11070 }
11071
11072 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
11073 ** savepoints nested inside of the savepoint being operated on. */
11074 while( db->pSavepoint!=pSavepoint ){
11075 pTmp = db->pSavepoint;
11076 db->pSavepoint = pTmp->pNext;
11077 sqlite3DbFree(db, pTmp);
11078 db->nSavepoint--;
11079 }
11080
11081 /* If it is a RELEASE, then destroy the savepoint being operated on
11082 ** too. If it is a ROLLBACK TO, then set the number of deferred
11083 ** constraint violations present in the database to the value stored
11084 ** when the savepoint was created. */
11085 if( p1==SAVEPOINT_RELEASE ){
11086 assert( pSavepoint==db->pSavepoint );
11087 db->pSavepoint = pSavepoint->pNext;
11088 sqlite3DbFree(db, pSavepoint);
11089 if( !isTransaction ){
11090 db->nSavepoint--;
11091 }
11092 }else{
11093 db->nDeferredCons = pSavepoint->nDeferredCons;
11094 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
11095 }
11096
11097 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
11098 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
11099 if( rc!=SQLITE_OK ) goto abort_due_to_error;
11100 }
11101 }
11102 }
11103
11104 break;
11105 }
11106
11107 /* Opcode: AutoCommit P1 P2 * * *
11108 **
11109 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
11110 ** back any currently active btree transactions. If there are any active
11111 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
11112 ** there are active writing VMs or active VMs that use shared cache.
11113 **
11114 ** This instruction causes the VM to halt.
11115 */
11116 case OP_AutoCommit: {
11117 int desiredAutoCommit;
11118 int iRollback;
11119 int turnOnAC;
11120
11121 desiredAutoCommit = pOp->p1;
11122 iRollback = pOp->p2;
11123 turnOnAC = desiredAutoCommit && !db->autoCommit;
11124 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
11125 assert( desiredAutoCommit==1 || iRollback==0 );
11126 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
11127 assert( p->bIsReader );
11128
11129 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
11130 /* If this instruction implements a COMMIT and other VMs are writing
11131 ** return an error indicating that the other VMs must complete first.
11132 */
11133 sqlite3VdbeError(p, "cannot commit transaction - "
11134 "SQL statements in progress");
11135 rc = SQLITE_BUSY;
11136 }else if( desiredAutoCommit!=db->autoCommit ){
11137 if( iRollback ){
11138 assert( desiredAutoCommit==1 );
11139 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
11140 db->autoCommit = 1;
11141 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
11142 goto vdbe_return;
11143 }else{
11144 db->autoCommit = (u8)desiredAutoCommit;
11145 }
11146 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
11147 p->pc = (int)(pOp - aOp);
11148 db->autoCommit = (u8)(1-desiredAutoCommit);
11149 p->rc = rc = SQLITE_BUSY;
11150 goto vdbe_return;
11151 }
11152 assert( db->nStatement==0 );
11153 sqlite3CloseSavepoints(db);
11154 if( p->rc==SQLITE_OK ){
11155 rc = SQLITE_DONE;
11156 }else{
11157 rc = SQLITE_ERROR;
11158 }
11159 goto vdbe_return;
11160 }else{
11161 sqlite3VdbeError(p,
11162 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
11163 (iRollback)?"cannot rollback - no transaction is active":
11164 "cannot commit - no transaction is active"));
11165
11166 rc = SQLITE_ERROR;
11167 }
11168 break;
11169 }
11170
11171 /* Opcode: Transaction P1 P2 P3 P4 P5
11172 **
11173 ** Begin a transaction on database P1 if a transaction is not already
11174 ** active.
11175 ** If P2 is non-zero, then a write-transaction is started, or if a
11176 ** read-transaction is already active, it is upgraded to a write-transaction.
11177 ** If P2 is zero, then a read-transaction is started.
11178 **
11179 ** P1 is the index of the database file on which the transaction is
11180 ** started. Index 0 is the main database file and index 1 is the
11181 ** file used for temporary tables. Indices of 2 or more are used for
11182 ** attached databases.
11183 **
11184 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
11185 ** true (this flag is set if the Vdbe may modify more than one row and may
11186 ** throw an ABORT exception), a statement transaction may also be opened.
11187 ** More specifically, a statement transaction is opened iff the database
11188 ** connection is currently not in autocommit mode, or if there are other
11189 ** active statements. A statement transaction allows the changes made by this
11190 ** VDBE to be rolled back after an error without having to roll back the
11191 ** entire transaction. If no error is encountered, the statement transaction
11192 ** will automatically commit when the VDBE halts.
11193 **
11194 ** If P5!=0 then this opcode also checks the schema cookie against P3
11195 ** and the schema generation counter against P4.
11196 ** The cookie changes its value whenever the database schema changes.
11197 ** This operation is used to detect when that the cookie has changed
11198 ** and that the current process needs to reread the schema. If the schema
11199 ** cookie in P3 differs from the schema cookie in the database header or
11200 ** if the schema generation counter in P4 differs from the current
11201 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
11202 ** halts. The sqlite3_step() wrapper function might then reprepare the
11203 ** statement and rerun it from the beginning.
11204 */
11205 case OP_Transaction: {
11206 Btree *pBt;
11207 int iMeta;
11208 int iGen;
11209
11210 assert( p->bIsReader );
11211 assert( p->readOnly==0 || pOp->p2==0 );
11212 assert( pOp->p1>=0 && pOp->p1<db->nDb );
11213 assert( DbMaskTest(p->btreeMask, pOp->p1) );
11214 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
11215 rc = SQLITE_READONLY;
11216 goto abort_due_to_error;
11217 }
11218 pBt = db->aDb[pOp->p1].pBt;
11219
11220 if( pBt ){
11221 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
11222 testcase( rc==SQLITE_BUSY_SNAPSHOT );
11223 testcase( rc==SQLITE_BUSY_RECOVERY );
11224 if( (rc&0xff)==SQLITE_BUSY ){
11225 p->pc = (int)(pOp - aOp);
11226 p->rc = rc;
11227 goto vdbe_return;
11228 }
11229 if( rc!=SQLITE_OK ){
11230 goto abort_due_to_error;
11231 }
11232
11233 if( pOp->p2 && p->usesStmtJournal
11234 && (db->autoCommit==0 || db->nVdbeRead>1)
11235 ){
11236 assert( sqlite3BtreeIsInTrans(pBt) );
11237 if( p->iStatement==0 ){
11238 assert( db->nStatement>=0 && db->nSavepoint>=0 );
11239 db->nStatement++;
11240 p->iStatement = db->nSavepoint + db->nStatement;
11241 }
11242
11243 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
11244 if( rc==SQLITE_OK ){
11245 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
11246 }
11247
11248 /* Store the current value of the database handles deferred constraint
11249 ** counter. If the statement transaction needs to be rolled back,
11250 ** the value of this counter needs to be restored too. */
11251 p->nStmtDefCons = db->nDeferredCons;
11252 p->nStmtDefImmCons = db->nDeferredImmCons;
11253 }
11254
11255 /* Gather the schema version number for checking:
11256 ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
11257 ** each time a query is executed to ensure that the internal cache of the
11258 ** schema used when compiling the SQL query matches the schema of the
11259 ** database against which the compiled query is actually executed.
11260 */
11261 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
11262 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
11263 }else{
11264 iGen = iMeta = 0;
11265 }
11266 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
11267 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
11268 sqlite3DbFree(db, p->zErrMsg);
11269 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
11270 /* If the schema-cookie from the database file matches the cookie
11271 ** stored with the in-memory representation of the schema, do
11272 ** not reload the schema from the database file.
11273 **
11274 ** If virtual-tables are in use, this is not just an optimization.
11275 ** Often, v-tables store their data in other SQLite tables, which
11276 ** are queried from within xNext() and other v-table methods using
11277 ** prepared queries. If such a query is out-of-date, we do not want to
11278 ** discard the database schema, as the user code implementing the
11279 ** v-table would have to be ready for the sqlite3_vtab structure itself
11280 ** to be invalidated whenever sqlite3_step() is called from within
11281 ** a v-table method.
11282 */
11283 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
11284 sqlite3ResetOneSchema(db, pOp->p1);
11285 }
11286 p->expired = 1;
11287 rc = SQLITE_SCHEMA;
11288 }
11289 break;
11290 }
11291
11292 /* Opcode: ReadCookie P1 P2 P3 * *
11293 **
11294 ** Read cookie number P3 from database P1 and write it into register P2.
11295 ** P3==1 is the schema version. P3==2 is the database format.
11296 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
11297 ** the main database file and P1==1 is the database file used to store
11298 ** temporary tables.
11299 **
11300 ** There must be a read-lock on the database (either a transaction
11301 ** must be started or there must be an open cursor) before
11302 ** executing this instruction.
11303 */
11304 case OP_ReadCookie: { /* out2 */
11305 int iMeta;
11306 int iDb;
11307 int iCookie;
11308
11309 assert( p->bIsReader );
11310 iDb = pOp->p1;
11311 iCookie = pOp->p3;
11312 assert( pOp->p3<SQLITE_N_BTREE_META );
11313 assert( iDb>=0 && iDb<db->nDb );
11314 assert( db->aDb[iDb].pBt!=0 );
11315 assert( DbMaskTest(p->btreeMask, iDb) );
11316
11317 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
11318 pOut = out2Prerelease(p, pOp);
11319 pOut->u.i = iMeta;
11320 break;
11321 }
11322
11323 /* Opcode: SetCookie P1 P2 P3 * *
11324 **
11325 ** Write the content of register P3 (interpreted as an integer)
11326 ** into cookie number P2 of database P1. P2==1 is the schema version.
11327 ** P2==2 is the database format. P2==3 is the recommended pager cache
11328 ** size, and so forth. P1==0 is the main database file and P1==1 is the
11329 ** database file used to store temporary tables.
11330 **
11331 ** A transaction must be started before executing this opcode.
11332 */
11333 case OP_SetCookie: { /* in3 */
11334 Db *pDb;
11335 assert( pOp->p2<SQLITE_N_BTREE_META );
11336 assert( pOp->p1>=0 && pOp->p1<db->nDb );
11337 assert( DbMaskTest(p->btreeMask, pOp->p1) );
11338 assert( p->readOnly==0 );
11339 pDb = &db->aDb[pOp->p1];
11340 assert( pDb->pBt!=0 );
11341 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
11342 pIn3 = &aMem[pOp->p3];
11343 sqlite3VdbeMemIntegerify(pIn3);
11344 /* See note about index shifting on OP_ReadCookie */
11345 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
11346 if( pOp->p2==BTREE_SCHEMA_VERSION ){
11347 /* When the schema cookie changes, record the new cookie internally */
11348 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
11349 db->flags |= SQLITE_InternChanges;
11350 }else if( pOp->p2==BTREE_FILE_FORMAT ){
11351 /* Record changes in the file format */
11352 pDb->pSchema->file_format = (u8)pIn3->u.i;
11353 }
11354 if( pOp->p1==1 ){
11355 /* Invalidate all prepared statements whenever the TEMP database
11356 ** schema is changed. Ticket #1644 */
11357 sqlite3ExpirePreparedStatements(db);
11358 p->expired = 0;
11359 }
11360 break;
11361 }
11362
11363 /* Opcode: OpenRead P1 P2 P3 P4 P5
11364 ** Synopsis: root=P2 iDb=P3
11365 **
11366 ** Open a read-only cursor for the database table whose root page is
11367 ** P2 in a database file. The database file is determined by P3.
11368 ** P3==0 means the main database, P3==1 means the database used for
11369 ** temporary tables, and P3>1 means used the corresponding attached
11370 ** database. Give the new cursor an identifier of P1. The P1
11371 ** values need not be contiguous but all P1 values should be small integers.
11372 ** It is an error for P1 to be negative.
11373 **
11374 ** If P5!=0 then use the content of register P2 as the root page, not
11375 ** the value of P2 itself.
11376 **
11377 ** There will be a read lock on the database whenever there is an
11378 ** open cursor. If the database was unlocked prior to this instruction
11379 ** then a read lock is acquired as part of this instruction. A read
11380 ** lock allows other processes to read the database but prohibits
11381 ** any other process from modifying the database. The read lock is
11382 ** released when all cursors are closed. If this instruction attempts
11383 ** to get a read lock but fails, the script terminates with an
11384 ** SQLITE_BUSY error code.
11385 **
11386 ** The P4 value may be either an integer (P4_INT32) or a pointer to
11387 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
11388 ** structure, then said structure defines the content and collating
11389 ** sequence of the index being opened. Otherwise, if P4 is an integer
11390 ** value, it is set to the number of columns in the table.
11391 **
11392 ** See also: OpenWrite, ReopenIdx
11393 */
11394 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
11395 ** Synopsis: root=P2 iDb=P3
11396 **
11397 ** The ReopenIdx opcode works exactly like ReadOpen except that it first
11398 ** checks to see if the cursor on P1 is already open with a root page
11399 ** number of P2 and if it is this opcode becomes a no-op. In other words,
11400 ** if the cursor is already open, do not reopen it.
11401 **
11402 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being
11403 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
11404 ** every other ReopenIdx or OpenRead for the same cursor number.
11405 **
11406 ** See the OpenRead opcode documentation for additional information.
11407 */
11408 /* Opcode: OpenWrite P1 P2 P3 P4 P5
11409 ** Synopsis: root=P2 iDb=P3
11410 **
11411 ** Open a read/write cursor named P1 on the table or index whose root
11412 ** page is P2. Or if P5!=0 use the content of register P2 to find the
11413 ** root page.
11414 **
11415 ** The P4 value may be either an integer (P4_INT32) or a pointer to
11416 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
11417 ** structure, then said structure defines the content and collating
11418 ** sequence of the index being opened. Otherwise, if P4 is an integer
11419 ** value, it is set to the number of columns in the table, or to the
11420 ** largest index of any column of the table that is actually used.
11421 **
11422 ** This instruction works just like OpenRead except that it opens the cursor
11423 ** in read/write mode. For a given table, there can be one or more read-only
11424 ** cursors or a single read/write cursor but not both.
11425 **
11426 ** See also OpenRead.
11427 */
11428 case OP_ReopenIdx: {
11429 int nField;
11430 KeyInfo *pKeyInfo;
11431 int p2;
11432 int iDb;
11433 int wrFlag;
11434 Btree *pX;
11435 VdbeCursor *pCur;
11436 Db *pDb;
11437
11438 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
11439 assert( pOp->p4type==P4_KEYINFO );
11440 pCur = p->apCsr[pOp->p1];
11441 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
11442 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
11443 goto open_cursor_set_hints;
11444 }
11445 /* If the cursor is not currently open or is open on a different
11446 ** index, then fall through into OP_OpenRead to force a reopen */
11447 case OP_OpenRead:
11448 case OP_OpenWrite:
11449
11450 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
11451 assert( p->bIsReader );
11452 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
11453 || p->readOnly==0 );
11454
11455 if( p->expired ){
11456 rc = SQLITE_ABORT_ROLLBACK;
11457 break;
11458 }
11459
11460 nField = 0;
11461 pKeyInfo = 0;
11462 p2 = pOp->p2;
11463 iDb = pOp->p3;
11464 assert( iDb>=0 && iDb<db->nDb );
11465 assert( DbMaskTest(p->btreeMask, iDb) );
11466 pDb = &db->aDb[iDb];
11467 pX = pDb->pBt;
11468 assert( pX!=0 );
11469 if( pOp->opcode==OP_OpenWrite ){
11470 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
11471 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
11472 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
11473 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
11474 p->minWriteFileFormat = pDb->pSchema->file_format;
11475 }
11476 }else{
11477 wrFlag = 0;
11478 }
11479 if( pOp->p5 & OPFLAG_P2ISREG ){
11480 assert( p2>0 );
11481 assert( p2<=(p->nMem-p->nCursor) );
11482 pIn2 = &aMem[p2];
11483 assert( memIsValid(pIn2) );
11484 assert( (pIn2->flags & MEM_Int)!=0 );
11485 sqlite3VdbeMemIntegerify(pIn2);
11486 p2 = (int)pIn2->u.i;
11487 /* The p2 value always comes from a prior OP_CreateTable opcode and
11488 ** that opcode will always set the p2 value to 2 or more or else fail.
11489 ** If there were a failure, the prepared statement would have halted
11490 ** before reaching this instruction. */
11491 if( NEVER(p2<2) ) {
11492 rc = SQLITE_CORRUPT_BKPT;
11493 goto abort_due_to_error;
11494 }
11495 }
11496 if( pOp->p4type==P4_KEYINFO ){
11497 pKeyInfo = pOp->p4.pKeyInfo;
11498 assert( pKeyInfo->enc==ENC(db) );
11499 assert( pKeyInfo->db==db );
11500 nField = pKeyInfo->nField+pKeyInfo->nXField;
11501 }else if( pOp->p4type==P4_INT32 ){
11502 nField = pOp->p4.i;
11503 }
11504 assert( pOp->p1>=0 );
11505 assert( nField>=0 );
11506 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
11507 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
11508 if( pCur==0 ) goto no_mem;
11509 pCur->nullRow = 1;
11510 pCur->isOrdered = 1;
11511 pCur->pgnoRoot = p2;
11512 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
11513 pCur->pKeyInfo = pKeyInfo;
11514 /* Set the VdbeCursor.isTable variable. Previous versions of
11515 ** SQLite used to check if the root-page flags were sane at this point
11516 ** and report database corruption if they were not, but this check has
11517 ** since moved into the btree layer. */
11518 pCur->isTable = pOp->p4type!=P4_KEYINFO;
11519
11520 open_cursor_set_hints:
11521 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
11522 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
11523 testcase( pOp->p5 & OPFLAG_BULKCSR );
11524 #ifdef SQLITE_ENABLE_CURSOR_HINTS
11525 testcase( pOp->p2 & OPFLAG_SEEKEQ );
11526 #endif
11527 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
11528 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
11529 break;
11530 }
11531
11532 /* Opcode: OpenEphemeral P1 P2 * P4 P5
11533 ** Synopsis: nColumn=P2
11534 **
11535 ** Open a new cursor P1 to a transient table.
11536 ** The cursor is always opened read/write even if
11537 ** the main database is read-only. The ephemeral
11538 ** table is deleted automatically when the cursor is closed.
11539 **
11540 ** P2 is the number of columns in the ephemeral table.
11541 ** The cursor points to a BTree table if P4==0 and to a BTree index
11542 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
11543 ** that defines the format of keys in the index.
11544 **
11545 ** The P5 parameter can be a mask of the BTREE_* flags defined
11546 ** in btree.h. These flags control aspects of the operation of
11547 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
11548 ** added automatically.
11549 */
11550 /* Opcode: OpenAutoindex P1 P2 * P4 *
11551 ** Synopsis: nColumn=P2
11552 **
11553 ** This opcode works the same as OP_OpenEphemeral. It has a
11554 ** different name to distinguish its use. Tables created using
11555 ** by this opcode will be used for automatically created transient
11556 ** indices in joins.
11557 */
11558 case OP_OpenAutoindex:
11559 case OP_OpenEphemeral: {
11560 VdbeCursor *pCx;
11561 KeyInfo *pKeyInfo;
11562
11563 static const int vfsFlags =
11564 SQLITE_OPEN_READWRITE |
11565 SQLITE_OPEN_CREATE |
11566 SQLITE_OPEN_EXCLUSIVE |
11567 SQLITE_OPEN_DELETEONCLOSE |
11568 SQLITE_OPEN_TRANSIENT_DB;
11569 assert( pOp->p1>=0 );
11570 assert( pOp->p2>=0 );
11571 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
11572 if( pCx==0 ) goto no_mem;
11573 pCx->nullRow = 1;
11574 pCx->isEphemeral = 1;
11575 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
11576 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
11577 if( rc==SQLITE_OK ){
11578 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
11579 }
11580 if( rc==SQLITE_OK ){
11581 /* If a transient index is required, create it by calling
11582 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
11583 ** opening it. If a transient table is required, just use the
11584 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
11585 */
11586 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
11587 int pgno;
11588 assert( pOp->p4type==P4_KEYINFO );
11589 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
11590 if( rc==SQLITE_OK ){
11591 assert( pgno==MASTER_ROOT+1 );
11592 assert( pKeyInfo->db==db );
11593 assert( pKeyInfo->enc==ENC(db) );
11594 pCx->pKeyInfo = pKeyInfo;
11595 rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR,
11596 pKeyInfo, pCx->uc.pCursor);
11597 }
11598 pCx->isTable = 0;
11599 }else{
11600 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR,
11601 0, pCx->uc.pCursor);
11602 pCx->isTable = 1;
11603 }
11604 }
11605 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
11606 break;
11607 }
11608
11609 /* Opcode: SorterOpen P1 P2 P3 P4 *
11610 **
11611 ** This opcode works like OP_OpenEphemeral except that it opens
11612 ** a transient index that is specifically designed to sort large
11613 ** tables using an external merge-sort algorithm.
11614 **
11615 ** If argument P3 is non-zero, then it indicates that the sorter may
11616 ** assume that a stable sort considering the first P3 fields of each
11617 ** key is sufficient to produce the required results.
11618 */
11619 case OP_SorterOpen: {
11620 VdbeCursor *pCx;
11621
11622 assert( pOp->p1>=0 );
11623 assert( pOp->p2>=0 );
11624 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
11625 if( pCx==0 ) goto no_mem;
11626 pCx->pKeyInfo = pOp->p4.pKeyInfo;
11627 assert( pCx->pKeyInfo->db==db );
11628 assert( pCx->pKeyInfo->enc==ENC(db) );
11629 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
11630 break;
11631 }
11632
11633 /* Opcode: SequenceTest P1 P2 * * *
11634 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
11635 **
11636 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
11637 ** to P2. Regardless of whether or not the jump is taken, increment the
11638 ** the sequence value.
11639 */
11640 case OP_SequenceTest: {
11641 VdbeCursor *pC;
11642 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
11643 pC = p->apCsr[pOp->p1];
11644 assert( isSorter(pC) );
11645 if( (pC->seqCount++)==0 ){
11646 goto jump_to_p2;
11647 }
11648 break;
11649 }
11650
11651 /* Opcode: OpenPseudo P1 P2 P3 * *
11652 ** Synopsis: P3 columns in r[P2]
11653 **
11654 ** Open a new cursor that points to a fake table that contains a single
11655 ** row of data. The content of that one row is the content of memory
11656 ** register P2. In other words, cursor P1 becomes an alias for the
11657 ** MEM_Blob content contained in register P2.
11658 **
11659 ** A pseudo-table created by this opcode is used to hold a single
11660 ** row output from the sorter so that the row can be decomposed into
11661 ** individual columns using the OP_Column opcode. The OP_Column opcode
11662 ** is the only cursor opcode that works with a pseudo-table.
11663 **
11664 ** P3 is the number of fields in the records that will be stored by
11665 ** the pseudo-table.
11666 */
11667 case OP_OpenPseudo: {
11668 VdbeCursor *pCx;
11669
11670 assert( pOp->p1>=0 );
11671 assert( pOp->p3>=0 );
11672 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
11673 if( pCx==0 ) goto no_mem;
11674 pCx->nullRow = 1;
11675 pCx->uc.pseudoTableReg = pOp->p2;
11676 pCx->isTable = 1;
11677 assert( pOp->p5==0 );
11678 break;
11679 }
11680
11681 /* Opcode: Close P1 * * * *
11682 **
11683 ** Close a cursor previously opened as P1. If P1 is not
11684 ** currently open, this instruction is a no-op.
11685 */
11686 case OP_Close: {
11687 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
11688 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
11689 p->apCsr[pOp->p1] = 0;
11690 break;
11691 }
11692
11693 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
11694 /* Opcode: ColumnsUsed P1 * * P4 *
11695 **
11696 ** This opcode (which only exists if SQLite was compiled with
11697 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
11698 ** table or index for cursor P1 are used. P4 is a 64-bit integer
11699 ** (P4_INT64) in which the first 63 bits are one for each of the
11700 ** first 63 columns of the table or index that are actually used
11701 ** by the cursor. The high-order bit is set if any column after
11702 ** the 64th is used.
11703 */
11704 case OP_ColumnsUsed: {
11705 VdbeCursor *pC;
11706 pC = p->apCsr[pOp->p1];
11707 assert( pC->eCurType==CURTYPE_BTREE );
11708 pC->maskUsed = *(u64*)pOp->p4.pI64;
11709 break;
11710 }
11711 #endif
11712
11713 /* Opcode: SeekGE P1 P2 P3 P4 *
11714 ** Synopsis: key=r[P3@P4]
11715 **
11716 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
11717 ** use the value in register P3 as the key. If cursor P1 refers
11718 ** to an SQL index, then P3 is the first in an array of P4 registers
11719 ** that are used as an unpacked index key.
11720 **
11721 ** Reposition cursor P1 so that it points to the smallest entry that
11722 ** is greater than or equal to the key value. If there are no records
11723 ** greater than or equal to the key and P2 is not zero, then jump to P2.
11724 **
11725 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
11726 ** opcode will always land on a record that equally equals the key, or
11727 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
11728 ** opcode must be followed by an IdxLE opcode with the same arguments.
11729 ** The IdxLE opcode will be skipped if this opcode succeeds, but the
11730 ** IdxLE opcode will be used on subsequent loop iterations.
11731 **
11732 ** This opcode leaves the cursor configured to move in forward order,
11733 ** from the beginning toward the end. In other words, the cursor is
11734 ** configured to use Next, not Prev.
11735 **
11736 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
11737 */
11738 /* Opcode: SeekGT P1 P2 P3 P4 *
11739 ** Synopsis: key=r[P3@P4]
11740 **
11741 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
11742 ** use the value in register P3 as a key. If cursor P1 refers
11743 ** to an SQL index, then P3 is the first in an array of P4 registers
11744 ** that are used as an unpacked index key.
11745 **
11746 ** Reposition cursor P1 so that it points to the smallest entry that
11747 ** is greater than the key value. If there are no records greater than
11748 ** the key and P2 is not zero, then jump to P2.
11749 **
11750 ** This opcode leaves the cursor configured to move in forward order,
11751 ** from the beginning toward the end. In other words, the cursor is
11752 ** configured to use Next, not Prev.
11753 **
11754 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
11755 */
11756 /* Opcode: SeekLT P1 P2 P3 P4 *
11757 ** Synopsis: key=r[P3@P4]
11758 **
11759 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
11760 ** use the value in register P3 as a key. If cursor P1 refers
11761 ** to an SQL index, then P3 is the first in an array of P4 registers
11762 ** that are used as an unpacked index key.
11763 **
11764 ** Reposition cursor P1 so that it points to the largest entry that
11765 ** is less than the key value. If there are no records less than
11766 ** the key and P2 is not zero, then jump to P2.
11767 **
11768 ** This opcode leaves the cursor configured to move in reverse order,
11769 ** from the end toward the beginning. In other words, the cursor is
11770 ** configured to use Prev, not Next.
11771 **
11772 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
11773 */
11774 /* Opcode: SeekLE P1 P2 P3 P4 *
11775 ** Synopsis: key=r[P3@P4]
11776 **
11777 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
11778 ** use the value in register P3 as a key. If cursor P1 refers
11779 ** to an SQL index, then P3 is the first in an array of P4 registers
11780 ** that are used as an unpacked index key.
11781 **
11782 ** Reposition cursor P1 so that it points to the largest entry that
11783 ** is less than or equal to the key value. If there are no records
11784 ** less than or equal to the key and P2 is not zero, then jump to P2.
11785 **
11786 ** This opcode leaves the cursor configured to move in reverse order,
11787 ** from the end toward the beginning. In other words, the cursor is
11788 ** configured to use Prev, not Next.
11789 **
11790 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
11791 ** opcode will always land on a record that equally equals the key, or
11792 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
11793 ** opcode must be followed by an IdxGE opcode with the same arguments.
11794 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
11795 ** IdxGE opcode will be used on subsequent loop iterations.
11796 **
11797 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
11798 */
11799 case OP_SeekLT: /* jump, in3 */
11800 case OP_SeekLE: /* jump, in3 */
11801 case OP_SeekGE: /* jump, in3 */
11802 case OP_SeekGT: { /* jump, in3 */
11803 int res; /* Comparison result */
11804 int oc; /* Opcode */
11805 VdbeCursor *pC; /* The cursor to seek */
11806 UnpackedRecord r; /* The key to seek for */
11807 int nField; /* Number of columns or fields in the key */
11808 i64 iKey; /* The rowid we are to seek to */
11809 int eqOnly; /* Only interested in == results */
11810
11811 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
11812 assert( pOp->p2!=0 );
11813 pC = p->apCsr[pOp->p1];
11814 assert( pC!=0 );
11815 assert( pC->eCurType==CURTYPE_BTREE );
11816 assert( OP_SeekLE == OP_SeekLT+1 );
11817 assert( OP_SeekGE == OP_SeekLT+2 );
11818 assert( OP_SeekGT == OP_SeekLT+3 );
11819 assert( pC->isOrdered );
11820 assert( pC->uc.pCursor!=0 );
11821 oc = pOp->opcode;
11822 eqOnly = 0;
11823 pC->nullRow = 0;
11824 #ifdef SQLITE_DEBUG
11825 pC->seekOp = pOp->opcode;
11826 #endif
11827
11828 if( pC->isTable ){
11829 /* The BTREE_SEEK_EQ flag is only set on index cursors */
11830 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 );
11831
11832 /* The input value in P3 might be of any type: integer, real, string,
11833 ** blob, or NULL. But it needs to be an integer before we can do
11834 ** the seek, so convert it. */
11835 pIn3 = &aMem[pOp->p3];
11836 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
11837 applyNumericAffinity(pIn3, 0);
11838 }
11839 iKey = sqlite3VdbeIntValue(pIn3);
11840
11841 /* If the P3 value could not be converted into an integer without
11842 ** loss of information, then special processing is required... */
11843 if( (pIn3->flags & MEM_Int)==0 ){
11844 if( (pIn3->flags & MEM_Real)==0 ){
11845 /* If the P3 value cannot be converted into any kind of a number,
11846 ** then the seek is not possible, so jump to P2 */
11847 VdbeBranchTaken(1,2); goto jump_to_p2;
11848 break;
11849 }
11850
11851 /* If the approximation iKey is larger than the actual real search
11852 ** term, substitute >= for > and < for <=. e.g. if the search term
11853 ** is 4.9 and the integer approximation 5:
11854 **
11855 ** (x > 4.9) -> (x >= 5)
11856 ** (x <= 4.9) -> (x < 5)
11857 */
11858 if( pIn3->u.r<(double)iKey ){
11859 assert( OP_SeekGE==(OP_SeekGT-1) );
11860 assert( OP_SeekLT==(OP_SeekLE-1) );
11861 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
11862 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
11863 }
11864
11865 /* If the approximation iKey is smaller than the actual real search
11866 ** term, substitute <= for < and > for >=. */
11867 else if( pIn3->u.r>(double)iKey ){
11868 assert( OP_SeekLE==(OP_SeekLT+1) );
11869 assert( OP_SeekGT==(OP_SeekGE+1) );
11870 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
11871 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
11872 }
11873 }
11874 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
11875 pC->movetoTarget = iKey; /* Used by OP_Delete */
11876 if( rc!=SQLITE_OK ){
11877 goto abort_due_to_error;
11878 }
11879 }else{
11880 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
11881 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
11882 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
11883 */
11884 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
11885 eqOnly = 1;
11886 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
11887 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
11888 assert( pOp[1].p1==pOp[0].p1 );
11889 assert( pOp[1].p2==pOp[0].p2 );
11890 assert( pOp[1].p3==pOp[0].p3 );
11891 assert( pOp[1].p4.i==pOp[0].p4.i );
11892 }
11893
11894 nField = pOp->p4.i;
11895 assert( pOp->p4type==P4_INT32 );
11896 assert( nField>0 );
11897 r.pKeyInfo = pC->pKeyInfo;
11898 r.nField = (u16)nField;
11899
11900 /* The next line of code computes as follows, only faster:
11901 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
11902 ** r.default_rc = -1;
11903 ** }else{
11904 ** r.default_rc = +1;
11905 ** }
11906 */
11907 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
11908 assert( oc!=OP_SeekGT || r.default_rc==-1 );
11909 assert( oc!=OP_SeekLE || r.default_rc==-1 );
11910 assert( oc!=OP_SeekGE || r.default_rc==+1 );
11911 assert( oc!=OP_SeekLT || r.default_rc==+1 );
11912
11913 r.aMem = &aMem[pOp->p3];
11914 #ifdef SQLITE_DEBUG
11915 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
11916 #endif
11917 ExpandBlob(r.aMem);
11918 r.eqSeen = 0;
11919 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
11920 if( rc!=SQLITE_OK ){
11921 goto abort_due_to_error;
11922 }
11923 if( eqOnly && r.eqSeen==0 ){
11924 assert( res!=0 );
11925 goto seek_not_found;
11926 }
11927 }
11928 pC->deferredMoveto = 0;
11929 pC->cacheStatus = CACHE_STALE;
11930 #ifdef SQLITE_TEST
11931 sqlite3_search_count++;
11932 #endif
11933 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
11934 if( res<0 || (res==0 && oc==OP_SeekGT) ){
11935 res = 0;
11936 rc = sqlite3BtreeNext(pC->uc.pCursor, &res);
11937 if( rc!=SQLITE_OK ) goto abort_due_to_error;
11938 }else{
11939 res = 0;
11940 }
11941 }else{
11942 assert( oc==OP_SeekLT || oc==OP_SeekLE );
11943 if( res>0 || (res==0 && oc==OP_SeekLT) ){
11944 res = 0;
11945 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res);
11946 if( rc!=SQLITE_OK ) goto abort_due_to_error;
11947 }else{
11948 /* res might be negative because the table is empty. Check to
11949 ** see if this is the case.
11950 */
11951 res = sqlite3BtreeEof(pC->uc.pCursor);
11952 }
11953 }
11954 seek_not_found:
11955 assert( pOp->p2>0 );
11956 VdbeBranchTaken(res!=0,2);
11957 if( res ){
11958 goto jump_to_p2;
11959 }else if( eqOnly ){
11960 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
11961 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
11962 }
11963 break;
11964 }
11965
11966 /* Opcode: Seek P1 P2 * * *
11967 ** Synopsis: intkey=r[P2]
11968 **
11969 ** P1 is an open table cursor and P2 is a rowid integer. Arrange
11970 ** for P1 to move so that it points to the rowid given by P2.
11971 **
11972 ** This is actually a deferred seek. Nothing actually happens until
11973 ** the cursor is used to read a record. That way, if no reads
11974 ** occur, no unnecessary I/O happens.
11975 */
11976 case OP_Seek: { /* in2 */
11977 VdbeCursor *pC;
11978
11979 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
11980 pC = p->apCsr[pOp->p1];
11981 assert( pC!=0 );
11982 assert( pC->eCurType==CURTYPE_BTREE );
11983 assert( pC->uc.pCursor!=0 );
11984 assert( pC->isTable );
11985 pC->nullRow = 0;
11986 pIn2 = &aMem[pOp->p2];
11987 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
11988 pC->deferredMoveto = 1;
11989 break;
11990 }
11991
11992
11993 /* Opcode: Found P1 P2 P3 P4 *
11994 ** Synopsis: key=r[P3@P4]
11995 **
11996 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
11997 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
11998 ** record.
11999 **
12000 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
12001 ** is a prefix of any entry in P1 then a jump is made to P2 and
12002 ** P1 is left pointing at the matching entry.
12003 **
12004 ** This operation leaves the cursor in a state where it can be
12005 ** advanced in the forward direction. The Next instruction will work,
12006 ** but not the Prev instruction.
12007 **
12008 ** See also: NotFound, NoConflict, NotExists. SeekGe
12009 */
12010 /* Opcode: NotFound P1 P2 P3 P4 *
12011 ** Synopsis: key=r[P3@P4]
12012 **
12013 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
12014 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
12015 ** record.
12016 **
12017 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
12018 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
12019 ** does contain an entry whose prefix matches the P3/P4 record then control
12020 ** falls through to the next instruction and P1 is left pointing at the
12021 ** matching entry.
12022 **
12023 ** This operation leaves the cursor in a state where it cannot be
12024 ** advanced in either direction. In other words, the Next and Prev
12025 ** opcodes do not work after this operation.
12026 **
12027 ** See also: Found, NotExists, NoConflict
12028 */
12029 /* Opcode: NoConflict P1 P2 P3 P4 *
12030 ** Synopsis: key=r[P3@P4]
12031 **
12032 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
12033 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
12034 ** record.
12035 **
12036 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
12037 ** contains any NULL value, jump immediately to P2. If all terms of the
12038 ** record are not-NULL then a check is done to determine if any row in the
12039 ** P1 index btree has a matching key prefix. If there are no matches, jump
12040 ** immediately to P2. If there is a match, fall through and leave the P1
12041 ** cursor pointing to the matching row.
12042 **
12043 ** This opcode is similar to OP_NotFound with the exceptions that the
12044 ** branch is always taken if any part of the search key input is NULL.
12045 **
12046 ** This operation leaves the cursor in a state where it cannot be
12047 ** advanced in either direction. In other words, the Next and Prev
12048 ** opcodes do not work after this operation.
12049 **
12050 ** See also: NotFound, Found, NotExists
12051 */
12052 case OP_NoConflict: /* jump, in3 */
12053 case OP_NotFound: /* jump, in3 */
12054 case OP_Found: { /* jump, in3 */
12055 int alreadyExists;
12056 int takeJump;
12057 int ii;
12058 VdbeCursor *pC;
12059 int res;
12060 char *pFree;
12061 UnpackedRecord *pIdxKey;
12062 UnpackedRecord r;
12063 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
12064
12065 #ifdef SQLITE_TEST
12066 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
12067 #endif
12068
12069 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12070 assert( pOp->p4type==P4_INT32 );
12071 pC = p->apCsr[pOp->p1];
12072 assert( pC!=0 );
12073 #ifdef SQLITE_DEBUG
12074 pC->seekOp = pOp->opcode;
12075 #endif
12076 pIn3 = &aMem[pOp->p3];
12077 assert( pC->eCurType==CURTYPE_BTREE );
12078 assert( pC->uc.pCursor!=0 );
12079 assert( pC->isTable==0 );
12080 pFree = 0;
12081 if( pOp->p4.i>0 ){
12082 r.pKeyInfo = pC->pKeyInfo;
12083 r.nField = (u16)pOp->p4.i;
12084 r.aMem = pIn3;
12085 for(ii=0; ii<r.nField; ii++){
12086 assert( memIsValid(&r.aMem[ii]) );
12087 ExpandBlob(&r.aMem[ii]);
12088 #ifdef SQLITE_DEBUG
12089 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
12090 #endif
12091 }
12092 pIdxKey = &r;
12093 }else{
12094 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
12095 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
12096 );
12097 if( pIdxKey==0 ) goto no_mem;
12098 assert( pIn3->flags & MEM_Blob );
12099 ExpandBlob(pIn3);
12100 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
12101 }
12102 pIdxKey->default_rc = 0;
12103 takeJump = 0;
12104 if( pOp->opcode==OP_NoConflict ){
12105 /* For the OP_NoConflict opcode, take the jump if any of the
12106 ** input fields are NULL, since any key with a NULL will not
12107 ** conflict */
12108 for(ii=0; ii<pIdxKey->nField; ii++){
12109 if( pIdxKey->aMem[ii].flags & MEM_Null ){
12110 takeJump = 1;
12111 break;
12112 }
12113 }
12114 }
12115 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
12116 sqlite3DbFree(db, pFree);
12117 if( rc!=SQLITE_OK ){
12118 break;
12119 }
12120 pC->seekResult = res;
12121 alreadyExists = (res==0);
12122 pC->nullRow = 1-alreadyExists;
12123 pC->deferredMoveto = 0;
12124 pC->cacheStatus = CACHE_STALE;
12125 if( pOp->opcode==OP_Found ){
12126 VdbeBranchTaken(alreadyExists!=0,2);
12127 if( alreadyExists ) goto jump_to_p2;
12128 }else{
12129 VdbeBranchTaken(takeJump||alreadyExists==0,2);
12130 if( takeJump || !alreadyExists ) goto jump_to_p2;
12131 }
12132 break;
12133 }
12134
12135 /* Opcode: NotExists P1 P2 P3 * *
12136 ** Synopsis: intkey=r[P3]
12137 **
12138 ** P1 is the index of a cursor open on an SQL table btree (with integer
12139 ** keys). P3 is an integer rowid. If P1 does not contain a record with
12140 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
12141 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
12142 ** leave the cursor pointing at that record and fall through to the next
12143 ** instruction.
12144 **
12145 ** The OP_NotFound opcode performs the same operation on index btrees
12146 ** (with arbitrary multi-value keys).
12147 **
12148 ** This opcode leaves the cursor in a state where it cannot be advanced
12149 ** in either direction. In other words, the Next and Prev opcodes will
12150 ** not work following this opcode.
12151 **
12152 ** See also: Found, NotFound, NoConflict
12153 */
12154 case OP_NotExists: { /* jump, in3 */
12155 VdbeCursor *pC;
12156 BtCursor *pCrsr;
12157 int res;
12158 u64 iKey;
12159
12160 pIn3 = &aMem[pOp->p3];
12161 assert( pIn3->flags & MEM_Int );
12162 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12163 pC = p->apCsr[pOp->p1];
12164 assert( pC!=0 );
12165 #ifdef SQLITE_DEBUG
12166 pC->seekOp = 0;
12167 #endif
12168 assert( pC->isTable );
12169 assert( pC->eCurType==CURTYPE_BTREE );
12170 pCrsr = pC->uc.pCursor;
12171 assert( pCrsr!=0 );
12172 res = 0;
12173 iKey = pIn3->u.i;
12174 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
12175 assert( rc==SQLITE_OK || res==0 );
12176 pC->movetoTarget = iKey; /* Used by OP_Delete */
12177 pC->nullRow = 0;
12178 pC->cacheStatus = CACHE_STALE;
12179 pC->deferredMoveto = 0;
12180 VdbeBranchTaken(res!=0,2);
12181 pC->seekResult = res;
12182 if( res!=0 ){
12183 assert( rc==SQLITE_OK );
12184 if( pOp->p2==0 ){
12185 rc = SQLITE_CORRUPT_BKPT;
12186 }else{
12187 goto jump_to_p2;
12188 }
12189 }
12190 break;
12191 }
12192
12193 /* Opcode: Sequence P1 P2 * * *
12194 ** Synopsis: r[P2]=cursor[P1].ctr++
12195 **
12196 ** Find the next available sequence number for cursor P1.
12197 ** Write the sequence number into register P2.
12198 ** The sequence number on the cursor is incremented after this
12199 ** instruction.
12200 */
12201 case OP_Sequence: { /* out2 */
12202 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12203 assert( p->apCsr[pOp->p1]!=0 );
12204 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
12205 pOut = out2Prerelease(p, pOp);
12206 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
12207 break;
12208 }
12209
12210
12211 /* Opcode: NewRowid P1 P2 P3 * *
12212 ** Synopsis: r[P2]=rowid
12213 **
12214 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
12215 ** The record number is not previously used as a key in the database
12216 ** table that cursor P1 points to. The new record number is written
12217 ** written to register P2.
12218 **
12219 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
12220 ** the largest previously generated record number. No new record numbers are
12221 ** allowed to be less than this value. When this value reaches its maximum,
12222 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
12223 ** generated record number. This P3 mechanism is used to help implement the
12224 ** AUTOINCREMENT feature.
12225 */
12226 case OP_NewRowid: { /* out2 */
12227 i64 v; /* The new rowid */
12228 VdbeCursor *pC; /* Cursor of table to get the new rowid */
12229 int res; /* Result of an sqlite3BtreeLast() */
12230 int cnt; /* Counter to limit the number of searches */
12231 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
12232 VdbeFrame *pFrame; /* Root frame of VDBE */
12233
12234 v = 0;
12235 res = 0;
12236 pOut = out2Prerelease(p, pOp);
12237 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12238 pC = p->apCsr[pOp->p1];
12239 assert( pC!=0 );
12240 assert( pC->eCurType==CURTYPE_BTREE );
12241 assert( pC->uc.pCursor!=0 );
12242 {
12243 /* The next rowid or record number (different terms for the same
12244 ** thing) is obtained in a two-step algorithm.
12245 **
12246 ** First we attempt to find the largest existing rowid and add one
12247 ** to that. But if the largest existing rowid is already the maximum
12248 ** positive integer, we have to fall through to the second
12249 ** probabilistic algorithm
12250 **
12251 ** The second algorithm is to select a rowid at random and see if
12252 ** it already exists in the table. If it does not exist, we have
12253 ** succeeded. If the random rowid does exist, we select a new one
12254 ** and try again, up to 100 times.
12255 */
12256 assert( pC->isTable );
12257
12258 #ifdef SQLITE_32BIT_ROWID
12259 # define MAX_ROWID 0x7fffffff
12260 #else
12261 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
12262 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
12263 ** to provide the constant while making all compilers happy.
12264 */
12265 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
12266 #endif
12267
12268 if( !pC->useRandomRowid ){
12269 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
12270 if( rc!=SQLITE_OK ){
12271 goto abort_due_to_error;
12272 }
12273 if( res ){
12274 v = 1; /* IMP: R-61914-48074 */
12275 }else{
12276 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
12277 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
12278 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
12279 if( v>=MAX_ROWID ){
12280 pC->useRandomRowid = 1;
12281 }else{
12282 v++; /* IMP: R-29538-34987 */
12283 }
12284 }
12285 }
12286
12287 #ifndef SQLITE_OMIT_AUTOINCREMENT
12288 if( pOp->p3 ){
12289 /* Assert that P3 is a valid memory cell. */
12290 assert( pOp->p3>0 );
12291 if( p->pFrame ){
12292 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
12293 /* Assert that P3 is a valid memory cell. */
12294 assert( pOp->p3<=pFrame->nMem );
12295 pMem = &pFrame->aMem[pOp->p3];
12296 }else{
12297 /* Assert that P3 is a valid memory cell. */
12298 assert( pOp->p3<=(p->nMem-p->nCursor) );
12299 pMem = &aMem[pOp->p3];
12300 memAboutToChange(p, pMem);
12301 }
12302 assert( memIsValid(pMem) );
12303
12304 REGISTER_TRACE(pOp->p3, pMem);
12305 sqlite3VdbeMemIntegerify(pMem);
12306 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
12307 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
12308 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
12309 goto abort_due_to_error;
12310 }
12311 if( v<pMem->u.i+1 ){
12312 v = pMem->u.i + 1;
12313 }
12314 pMem->u.i = v;
12315 }
12316 #endif
12317 if( pC->useRandomRowid ){
12318 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
12319 ** largest possible integer (9223372036854775807) then the database
12320 ** engine starts picking positive candidate ROWIDs at random until
12321 ** it finds one that is not previously used. */
12322 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
12323 ** an AUTOINCREMENT table. */
12324 cnt = 0;
12325 do{
12326 sqlite3_randomness(sizeof(v), &v);
12327 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
12328 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
12329 0, &res))==SQLITE_OK)
12330 && (res==0)
12331 && (++cnt<100));
12332 if( rc==SQLITE_OK && res==0 ){
12333 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
12334 goto abort_due_to_error;
12335 }
12336 assert( v>0 ); /* EV: R-40812-03570 */
12337 }
12338 pC->deferredMoveto = 0;
12339 pC->cacheStatus = CACHE_STALE;
12340 }
12341 pOut->u.i = v;
12342 break;
12343 }
12344
12345 /* Opcode: Insert P1 P2 P3 P4 P5
12346 ** Synopsis: intkey=r[P3] data=r[P2]
12347 **
12348 ** Write an entry into the table of cursor P1. A new entry is
12349 ** created if it doesn't already exist or the data for an existing
12350 ** entry is overwritten. The data is the value MEM_Blob stored in register
12351 ** number P2. The key is stored in register P3. The key must
12352 ** be a MEM_Int.
12353 **
12354 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
12355 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
12356 ** then rowid is stored for subsequent return by the
12357 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
12358 **
12359 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
12360 ** the last seek operation (OP_NotExists) was a success, then this
12361 ** operation will not attempt to find the appropriate row before doing
12362 ** the insert but will instead overwrite the row that the cursor is
12363 ** currently pointing to. Presumably, the prior OP_NotExists opcode
12364 ** has already positioned the cursor correctly. This is an optimization
12365 ** that boosts performance by avoiding redundant seeks.
12366 **
12367 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
12368 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
12369 ** is part of an INSERT operation. The difference is only important to
12370 ** the update hook.
12371 **
12372 ** Parameter P4 may point to a string containing the table-name, or
12373 ** may be NULL. If it is not NULL, then the update-hook
12374 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
12375 **
12376 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
12377 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
12378 ** and register P2 becomes ephemeral. If the cursor is changed, the
12379 ** value of register P2 will then change. Make sure this does not
12380 ** cause any problems.)
12381 **
12382 ** This instruction only works on tables. The equivalent instruction
12383 ** for indices is OP_IdxInsert.
12384 */
12385 /* Opcode: InsertInt P1 P2 P3 P4 P5
12386 ** Synopsis: intkey=P3 data=r[P2]
12387 **
12388 ** This works exactly like OP_Insert except that the key is the
12389 ** integer value P3, not the value of the integer stored in register P3.
12390 */
12391 case OP_Insert:
12392 case OP_InsertInt: {
12393 Mem *pData; /* MEM cell holding data for the record to be inserted */
12394 Mem *pKey; /* MEM cell holding key for the record */
12395 i64 iKey; /* The integer ROWID or key for the record to be inserted */
12396 VdbeCursor *pC; /* Cursor to table into which insert is written */
12397 int nZero; /* Number of zero-bytes to append */
12398 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
12399 const char *zDb; /* database name - used by the update hook */
12400 const char *zTbl; /* Table name - used by the opdate hook */
12401 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
12402
12403 pData = &aMem[pOp->p2];
12404 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12405 assert( memIsValid(pData) );
12406 pC = p->apCsr[pOp->p1];
12407 assert( pC!=0 );
12408 assert( pC->eCurType==CURTYPE_BTREE );
12409 assert( pC->uc.pCursor!=0 );
12410 assert( pC->isTable );
12411 REGISTER_TRACE(pOp->p2, pData);
12412
12413 if( pOp->opcode==OP_Insert ){
12414 pKey = &aMem[pOp->p3];
12415 assert( pKey->flags & MEM_Int );
12416 assert( memIsValid(pKey) );
12417 REGISTER_TRACE(pOp->p3, pKey);
12418 iKey = pKey->u.i;
12419 }else{
12420 assert( pOp->opcode==OP_InsertInt );
12421 iKey = pOp->p3;
12422 }
12423
12424 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
12425 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
12426 if( pData->flags & MEM_Null ){
12427 pData->z = 0;
12428 pData->n = 0;
12429 }else{
12430 assert( pData->flags & (MEM_Blob|MEM_Str) );
12431 }
12432 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
12433 if( pData->flags & MEM_Zero ){
12434 nZero = pData->u.nZero;
12435 }else{
12436 nZero = 0;
12437 }
12438 rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey,
12439 pData->z, pData->n, nZero,
12440 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
12441 );
12442 pC->deferredMoveto = 0;
12443 pC->cacheStatus = CACHE_STALE;
12444
12445 /* Invoke the update-hook if required. */
12446 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
12447 zDb = db->aDb[pC->iDb].zName;
12448 zTbl = pOp->p4.z;
12449 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
12450 assert( pC->isTable );
12451 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
12452 assert( pC->iDb>=0 );
12453 }
12454 break;
12455 }
12456
12457 /* Opcode: Delete P1 P2 * P4 P5
12458 **
12459 ** Delete the record at which the P1 cursor is currently pointing.
12460 **
12461 ** If the P5 parameter is non-zero, the cursor will be left pointing at
12462 ** either the next or the previous record in the table. If it is left
12463 ** pointing at the next record, then the next Next instruction will be a
12464 ** no-op. As a result, in this case it is OK to delete a record from within a
12465 ** Next loop. If P5 is zero, then the cursor is left in an undefined state.
12466 **
12467 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
12468 ** incremented (otherwise not).
12469 **
12470 ** P1 must not be pseudo-table. It has to be a real table with
12471 ** multiple rows.
12472 **
12473 ** If P4 is not NULL, then it is the name of the table that P1 is
12474 ** pointing to. The update hook will be invoked, if it exists.
12475 ** If P4 is not NULL then the P1 cursor must have been positioned
12476 ** using OP_NotFound prior to invoking this opcode.
12477 */
12478 case OP_Delete: {
12479 VdbeCursor *pC;
12480 u8 hasUpdateCallback;
12481
12482 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12483 pC = p->apCsr[pOp->p1];
12484 assert( pC!=0 );
12485 assert( pC->eCurType==CURTYPE_BTREE );
12486 assert( pC->uc.pCursor!=0 );
12487 assert( pC->deferredMoveto==0 );
12488
12489 hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable;
12490 if( pOp->p5 && hasUpdateCallback ){
12491 sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget);
12492 }
12493
12494 #ifdef SQLITE_DEBUG
12495 /* The seek operation that positioned the cursor prior to OP_Delete will
12496 ** have also set the pC->movetoTarget field to the rowid of the row that
12497 ** is being deleted */
12498 if( pOp->p4.z && pC->isTable && pOp->p5==0 ){
12499 i64 iKey = 0;
12500 sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
12501 assert( pC->movetoTarget==iKey );
12502 }
12503 #endif
12504
12505 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
12506 pC->cacheStatus = CACHE_STALE;
12507
12508 /* Invoke the update-hook if required. */
12509 if( rc==SQLITE_OK && hasUpdateCallback ){
12510 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
12511 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
12512 assert( pC->iDb>=0 );
12513 }
12514 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
12515 break;
12516 }
12517 /* Opcode: ResetCount * * * * *
12518 **
12519 ** The value of the change counter is copied to the database handle
12520 ** change counter (returned by subsequent calls to sqlite3_changes()).
12521 ** Then the VMs internal change counter resets to 0.
12522 ** This is used by trigger programs.
12523 */
12524 case OP_ResetCount: {
12525 sqlite3VdbeSetChanges(db, p->nChange);
12526 p->nChange = 0;
12527 break;
12528 }
12529
12530 /* Opcode: SorterCompare P1 P2 P3 P4
12531 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
12532 **
12533 ** P1 is a sorter cursor. This instruction compares a prefix of the
12534 ** record blob in register P3 against a prefix of the entry that
12535 ** the sorter cursor currently points to. Only the first P4 fields
12536 ** of r[P3] and the sorter record are compared.
12537 **
12538 ** If either P3 or the sorter contains a NULL in one of their significant
12539 ** fields (not counting the P4 fields at the end which are ignored) then
12540 ** the comparison is assumed to be equal.
12541 **
12542 ** Fall through to next instruction if the two records compare equal to
12543 ** each other. Jump to P2 if they are different.
12544 */
12545 case OP_SorterCompare: {
12546 VdbeCursor *pC;
12547 int res;
12548 int nKeyCol;
12549
12550 pC = p->apCsr[pOp->p1];
12551 assert( isSorter(pC) );
12552 assert( pOp->p4type==P4_INT32 );
12553 pIn3 = &aMem[pOp->p3];
12554 nKeyCol = pOp->p4.i;
12555 res = 0;
12556 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
12557 VdbeBranchTaken(res!=0,2);
12558 if( res ) goto jump_to_p2;
12559 break;
12560 };
12561
12562 /* Opcode: SorterData P1 P2 P3 * *
12563 ** Synopsis: r[P2]=data
12564 **
12565 ** Write into register P2 the current sorter data for sorter cursor P1.
12566 ** Then clear the column header cache on cursor P3.
12567 **
12568 ** This opcode is normally use to move a record out of the sorter and into
12569 ** a register that is the source for a pseudo-table cursor created using
12570 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
12571 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
12572 ** us from having to issue a separate NullRow instruction to clear that cache.
12573 */
12574 case OP_SorterData: {
12575 VdbeCursor *pC;
12576
12577 pOut = &aMem[pOp->p2];
12578 pC = p->apCsr[pOp->p1];
12579 assert( isSorter(pC) );
12580 rc = sqlite3VdbeSorterRowkey(pC, pOut);
12581 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
12582 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12583 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
12584 break;
12585 }
12586
12587 /* Opcode: RowData P1 P2 * * *
12588 ** Synopsis: r[P2]=data
12589 **
12590 ** Write into register P2 the complete row data for cursor P1.
12591 ** There is no interpretation of the data.
12592 ** It is just copied onto the P2 register exactly as
12593 ** it is found in the database file.
12594 **
12595 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
12596 ** of a real table, not a pseudo-table.
12597 */
12598 /* Opcode: RowKey P1 P2 * * *
12599 ** Synopsis: r[P2]=key
12600 **
12601 ** Write into register P2 the complete row key for cursor P1.
12602 ** There is no interpretation of the data.
12603 ** The key is copied onto the P2 register exactly as
12604 ** it is found in the database file.
12605 **
12606 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
12607 ** of a real table, not a pseudo-table.
12608 */
12609 case OP_RowKey:
12610 case OP_RowData: {
12611 VdbeCursor *pC;
12612 BtCursor *pCrsr;
12613 u32 n;
12614 i64 n64;
12615
12616 pOut = &aMem[pOp->p2];
12617 memAboutToChange(p, pOut);
12618
12619 /* Note that RowKey and RowData are really exactly the same instruction */
12620 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12621 pC = p->apCsr[pOp->p1];
12622 assert( pC!=0 );
12623 assert( pC->eCurType==CURTYPE_BTREE );
12624 assert( isSorter(pC)==0 );
12625 assert( pC->isTable || pOp->opcode!=OP_RowData );
12626 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
12627 assert( pC->nullRow==0 );
12628 assert( pC->uc.pCursor!=0 );
12629 pCrsr = pC->uc.pCursor;
12630
12631 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
12632 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
12633 ** the cursor. If this where not the case, on of the following assert()s
12634 ** would fail. Should this ever change (because of changes in the code
12635 ** generator) then the fix would be to insert a call to
12636 ** sqlite3VdbeCursorMoveto().
12637 */
12638 assert( pC->deferredMoveto==0 );
12639 assert( sqlite3BtreeCursorIsValid(pCrsr) );
12640 #if 0 /* Not required due to the previous to assert() statements */
12641 rc = sqlite3VdbeCursorMoveto(pC);
12642 if( rc!=SQLITE_OK ) goto abort_due_to_error;
12643 #endif
12644
12645 if( pC->isTable==0 ){
12646 assert( !pC->isTable );
12647 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
12648 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
12649 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
12650 goto too_big;
12651 }
12652 n = (u32)n64;
12653 }else{
12654 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
12655 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
12656 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
12657 goto too_big;
12658 }
12659 }
12660 testcase( n==0 );
12661 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
12662 goto no_mem;
12663 }
12664 pOut->n = n;
12665 MemSetTypeFlag(pOut, MEM_Blob);
12666 if( pC->isTable==0 ){
12667 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
12668 }else{
12669 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
12670 }
12671 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
12672 UPDATE_MAX_BLOBSIZE(pOut);
12673 REGISTER_TRACE(pOp->p2, pOut);
12674 break;
12675 }
12676
12677 /* Opcode: Rowid P1 P2 * * *
12678 ** Synopsis: r[P2]=rowid
12679 **
12680 ** Store in register P2 an integer which is the key of the table entry that
12681 ** P1 is currently point to.
12682 **
12683 ** P1 can be either an ordinary table or a virtual table. There used to
12684 ** be a separate OP_VRowid opcode for use with virtual tables, but this
12685 ** one opcode now works for both table types.
12686 */
12687 case OP_Rowid: { /* out2 */
12688 VdbeCursor *pC;
12689 i64 v;
12690 sqlite3_vtab *pVtab;
12691 const sqlite3_module *pModule;
12692
12693 pOut = out2Prerelease(p, pOp);
12694 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12695 pC = p->apCsr[pOp->p1];
12696 assert( pC!=0 );
12697 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
12698 if( pC->nullRow ){
12699 pOut->flags = MEM_Null;
12700 break;
12701 }else if( pC->deferredMoveto ){
12702 v = pC->movetoTarget;
12703 #ifndef SQLITE_OMIT_VIRTUALTABLE
12704 }else if( pC->eCurType==CURTYPE_VTAB ){
12705 assert( pC->uc.pVCur!=0 );
12706 pVtab = pC->uc.pVCur->pVtab;
12707 pModule = pVtab->pModule;
12708 assert( pModule->xRowid );
12709 rc = pModule->xRowid(pC->uc.pVCur, &v);
12710 sqlite3VtabImportErrmsg(p, pVtab);
12711 #endif /* SQLITE_OMIT_VIRTUALTABLE */
12712 }else{
12713 assert( pC->eCurType==CURTYPE_BTREE );
12714 assert( pC->uc.pCursor!=0 );
12715 rc = sqlite3VdbeCursorRestore(pC);
12716 if( rc ) goto abort_due_to_error;
12717 if( pC->nullRow ){
12718 pOut->flags = MEM_Null;
12719 break;
12720 }
12721 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
12722 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */
12723 }
12724 pOut->u.i = v;
12725 break;
12726 }
12727
12728 /* Opcode: NullRow P1 * * * *
12729 **
12730 ** Move the cursor P1 to a null row. Any OP_Column operations
12731 ** that occur while the cursor is on the null row will always
12732 ** write a NULL.
12733 */
12734 case OP_NullRow: {
12735 VdbeCursor *pC;
12736
12737 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12738 pC = p->apCsr[pOp->p1];
12739 assert( pC!=0 );
12740 pC->nullRow = 1;
12741 pC->cacheStatus = CACHE_STALE;
12742 if( pC->eCurType==CURTYPE_BTREE ){
12743 assert( pC->uc.pCursor!=0 );
12744 sqlite3BtreeClearCursor(pC->uc.pCursor);
12745 }
12746 break;
12747 }
12748
12749 /* Opcode: Last P1 P2 P3 * *
12750 **
12751 ** The next use of the Rowid or Column or Prev instruction for P1
12752 ** will refer to the last entry in the database table or index.
12753 ** If the table or index is empty and P2>0, then jump immediately to P2.
12754 ** If P2 is 0 or if the table or index is not empty, fall through
12755 ** to the following instruction.
12756 **
12757 ** This opcode leaves the cursor configured to move in reverse order,
12758 ** from the end toward the beginning. In other words, the cursor is
12759 ** configured to use Prev, not Next.
12760 */
12761 case OP_Last: { /* jump */
12762 VdbeCursor *pC;
12763 BtCursor *pCrsr;
12764 int res;
12765
12766 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12767 pC = p->apCsr[pOp->p1];
12768 assert( pC!=0 );
12769 assert( pC->eCurType==CURTYPE_BTREE );
12770 pCrsr = pC->uc.pCursor;
12771 res = 0;
12772 assert( pCrsr!=0 );
12773 rc = sqlite3BtreeLast(pCrsr, &res);
12774 pC->nullRow = (u8)res;
12775 pC->deferredMoveto = 0;
12776 pC->cacheStatus = CACHE_STALE;
12777 pC->seekResult = pOp->p3;
12778 #ifdef SQLITE_DEBUG
12779 pC->seekOp = OP_Last;
12780 #endif
12781 if( pOp->p2>0 ){
12782 VdbeBranchTaken(res!=0,2);
12783 if( res ) goto jump_to_p2;
12784 }
12785 break;
12786 }
12787
12788
12789 /* Opcode: Sort P1 P2 * * *
12790 **
12791 ** This opcode does exactly the same thing as OP_Rewind except that
12792 ** it increments an undocumented global variable used for testing.
12793 **
12794 ** Sorting is accomplished by writing records into a sorting index,
12795 ** then rewinding that index and playing it back from beginning to
12796 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
12797 ** rewinding so that the global variable will be incremented and
12798 ** regression tests can determine whether or not the optimizer is
12799 ** correctly optimizing out sorts.
12800 */
12801 case OP_SorterSort: /* jump */
12802 case OP_Sort: { /* jump */
12803 #ifdef SQLITE_TEST
12804 sqlite3_sort_count++;
12805 sqlite3_search_count--;
12806 #endif
12807 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
12808 /* Fall through into OP_Rewind */
12809 }
12810 /* Opcode: Rewind P1 P2 * * *
12811 **
12812 ** The next use of the Rowid or Column or Next instruction for P1
12813 ** will refer to the first entry in the database table or index.
12814 ** If the table or index is empty, jump immediately to P2.
12815 ** If the table or index is not empty, fall through to the following
12816 ** instruction.
12817 **
12818 ** This opcode leaves the cursor configured to move in forward order,
12819 ** from the beginning toward the end. In other words, the cursor is
12820 ** configured to use Next, not Prev.
12821 */
12822 case OP_Rewind: { /* jump */
12823 VdbeCursor *pC;
12824 BtCursor *pCrsr;
12825 int res;
12826
12827 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12828 pC = p->apCsr[pOp->p1];
12829 assert( pC!=0 );
12830 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
12831 res = 1;
12832 #ifdef SQLITE_DEBUG
12833 pC->seekOp = OP_Rewind;
12834 #endif
12835 if( isSorter(pC) ){
12836 rc = sqlite3VdbeSorterRewind(pC, &res);
12837 }else{
12838 assert( pC->eCurType==CURTYPE_BTREE );
12839 pCrsr = pC->uc.pCursor;
12840 assert( pCrsr );
12841 rc = sqlite3BtreeFirst(pCrsr, &res);
12842 pC->deferredMoveto = 0;
12843 pC->cacheStatus = CACHE_STALE;
12844 }
12845 pC->nullRow = (u8)res;
12846 assert( pOp->p2>0 && pOp->p2<p->nOp );
12847 VdbeBranchTaken(res!=0,2);
12848 if( res ) goto jump_to_p2;
12849 break;
12850 }
12851
12852 /* Opcode: Next P1 P2 P3 P4 P5
12853 **
12854 ** Advance cursor P1 so that it points to the next key/data pair in its
12855 ** table or index. If there are no more key/value pairs then fall through
12856 ** to the following instruction. But if the cursor advance was successful,
12857 ** jump immediately to P2.
12858 **
12859 ** The Next opcode is only valid following an SeekGT, SeekGE, or
12860 ** OP_Rewind opcode used to position the cursor. Next is not allowed
12861 ** to follow SeekLT, SeekLE, or OP_Last.
12862 **
12863 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
12864 ** been opened prior to this opcode or the program will segfault.
12865 **
12866 ** The P3 value is a hint to the btree implementation. If P3==1, that
12867 ** means P1 is an SQL index and that this instruction could have been
12868 ** omitted if that index had been unique. P3 is usually 0. P3 is
12869 ** always either 0 or 1.
12870 **
12871 ** P4 is always of type P4_ADVANCE. The function pointer points to
12872 ** sqlite3BtreeNext().
12873 **
12874 ** If P5 is positive and the jump is taken, then event counter
12875 ** number P5-1 in the prepared statement is incremented.
12876 **
12877 ** See also: Prev, NextIfOpen
12878 */
12879 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
12880 **
12881 ** This opcode works just like Next except that if cursor P1 is not
12882 ** open it behaves a no-op.
12883 */
12884 /* Opcode: Prev P1 P2 P3 P4 P5
12885 **
12886 ** Back up cursor P1 so that it points to the previous key/data pair in its
12887 ** table or index. If there is no previous key/value pairs then fall through
12888 ** to the following instruction. But if the cursor backup was successful,
12889 ** jump immediately to P2.
12890 **
12891 **
12892 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
12893 ** OP_Last opcode used to position the cursor. Prev is not allowed
12894 ** to follow SeekGT, SeekGE, or OP_Rewind.
12895 **
12896 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
12897 ** not open then the behavior is undefined.
12898 **
12899 ** The P3 value is a hint to the btree implementation. If P3==1, that
12900 ** means P1 is an SQL index and that this instruction could have been
12901 ** omitted if that index had been unique. P3 is usually 0. P3 is
12902 ** always either 0 or 1.
12903 **
12904 ** P4 is always of type P4_ADVANCE. The function pointer points to
12905 ** sqlite3BtreePrevious().
12906 **
12907 ** If P5 is positive and the jump is taken, then event counter
12908 ** number P5-1 in the prepared statement is incremented.
12909 */
12910 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
12911 **
12912 ** This opcode works just like Prev except that if cursor P1 is not
12913 ** open it behaves a no-op.
12914 */
12915 case OP_SorterNext: { /* jump */
12916 VdbeCursor *pC;
12917 int res;
12918
12919 pC = p->apCsr[pOp->p1];
12920 assert( isSorter(pC) );
12921 res = 0;
12922 rc = sqlite3VdbeSorterNext(db, pC, &res);
12923 goto next_tail;
12924 case OP_PrevIfOpen: /* jump */
12925 case OP_NextIfOpen: /* jump */
12926 if( p->apCsr[pOp->p1]==0 ) break;
12927 /* Fall through */
12928 case OP_Prev: /* jump */
12929 case OP_Next: /* jump */
12930 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12931 assert( pOp->p5<ArraySize(p->aCounter) );
12932 pC = p->apCsr[pOp->p1];
12933 res = pOp->p3;
12934 assert( pC!=0 );
12935 assert( pC->deferredMoveto==0 );
12936 assert( pC->eCurType==CURTYPE_BTREE );
12937 assert( res==0 || (res==1 && pC->isTable==0) );
12938 testcase( res==1 );
12939 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
12940 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
12941 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
12942 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
12943
12944 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
12945 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
12946 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
12947 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
12948 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
12949 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
12950 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
12951 || pC->seekOp==OP_Last );
12952
12953 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
12954 next_tail:
12955 pC->cacheStatus = CACHE_STALE;
12956 VdbeBranchTaken(res==0,2);
12957 if( res==0 ){
12958 pC->nullRow = 0;
12959 p->aCounter[pOp->p5]++;
12960 #ifdef SQLITE_TEST
12961 sqlite3_search_count++;
12962 #endif
12963 goto jump_to_p2_and_check_for_interrupt;
12964 }else{
12965 pC->nullRow = 1;
12966 }
12967 goto check_for_interrupt;
12968 }
12969
12970 /* Opcode: IdxInsert P1 P2 P3 * P5
12971 ** Synopsis: key=r[P2]
12972 **
12973 ** Register P2 holds an SQL index key made using the
12974 ** MakeRecord instructions. This opcode writes that key
12975 ** into the index P1. Data for the entry is nil.
12976 **
12977 ** P3 is a flag that provides a hint to the b-tree layer that this
12978 ** insert is likely to be an append.
12979 **
12980 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
12981 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
12982 ** then the change counter is unchanged.
12983 **
12984 ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
12985 ** just done a seek to the spot where the new entry is to be inserted.
12986 ** This flag avoids doing an extra seek.
12987 **
12988 ** This instruction only works for indices. The equivalent instruction
12989 ** for tables is OP_Insert.
12990 */
12991 case OP_SorterInsert: /* in2 */
12992 case OP_IdxInsert: { /* in2 */
12993 VdbeCursor *pC;
12994 int nKey;
12995 const char *zKey;
12996
12997 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
12998 pC = p->apCsr[pOp->p1];
12999 assert( pC!=0 );
13000 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
13001 pIn2 = &aMem[pOp->p2];
13002 assert( pIn2->flags & MEM_Blob );
13003 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
13004 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
13005 assert( pC->isTable==0 );
13006 rc = ExpandBlob(pIn2);
13007 if( rc==SQLITE_OK ){
13008 if( pOp->opcode==OP_SorterInsert ){
13009 rc = sqlite3VdbeSorterWrite(pC, pIn2);
13010 }else{
13011 nKey = pIn2->n;
13012 zKey = pIn2->z;
13013 rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3,
13014 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
13015 );
13016 assert( pC->deferredMoveto==0 );
13017 pC->cacheStatus = CACHE_STALE;
13018 }
13019 }
13020 break;
13021 }
13022
13023 /* Opcode: IdxDelete P1 P2 P3 * *
13024 ** Synopsis: key=r[P2@P3]
13025 **
13026 ** The content of P3 registers starting at register P2 form
13027 ** an unpacked index key. This opcode removes that entry from the
13028 ** index opened by cursor P1.
13029 */
13030 case OP_IdxDelete: {
13031 VdbeCursor *pC;
13032 BtCursor *pCrsr;
13033 int res;
13034 UnpackedRecord r;
13035
13036 assert( pOp->p3>0 );
13037 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
13038 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
13039 pC = p->apCsr[pOp->p1];
13040 assert( pC!=0 );
13041 assert( pC->eCurType==CURTYPE_BTREE );
13042 pCrsr = pC->uc.pCursor;
13043 assert( pCrsr!=0 );
13044 assert( pOp->p5==0 );
13045 r.pKeyInfo = pC->pKeyInfo;
13046 r.nField = (u16)pOp->p3;
13047 r.default_rc = 0;
13048 r.aMem = &aMem[pOp->p2];
13049 #ifdef SQLITE_DEBUG
13050 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
13051 #endif
13052 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
13053 if( rc==SQLITE_OK && res==0 ){
13054 rc = sqlite3BtreeDelete(pCrsr, 0);
13055 }
13056 assert( pC->deferredMoveto==0 );
13057 pC->cacheStatus = CACHE_STALE;
13058 break;
13059 }
13060
13061 /* Opcode: IdxRowid P1 P2 * * *
13062 ** Synopsis: r[P2]=rowid
13063 **
13064 ** Write into register P2 an integer which is the last entry in the record at
13065 ** the end of the index key pointed to by cursor P1. This integer should be
13066 ** the rowid of the table entry to which this index entry points.
13067 **
13068 ** See also: Rowid, MakeRecord.
13069 */
13070 case OP_IdxRowid: { /* out2 */
13071 BtCursor *pCrsr;
13072 VdbeCursor *pC;
13073 i64 rowid;
13074
13075 pOut = out2Prerelease(p, pOp);
13076 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
13077 pC = p->apCsr[pOp->p1];
13078 assert( pC!=0 );
13079 assert( pC->eCurType==CURTYPE_BTREE );
13080 pCrsr = pC->uc.pCursor;
13081 assert( pCrsr!=0 );
13082 pOut->flags = MEM_Null;
13083 assert( pC->isTable==0 );
13084 assert( pC->deferredMoveto==0 );
13085
13086 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
13087 ** out from under the cursor. That will never happend for an IdxRowid
13088 ** opcode, hence the NEVER() arround the check of the return value.
13089 */
13090 rc = sqlite3VdbeCursorRestore(pC);
13091 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
13092
13093 if( !pC->nullRow ){
13094 rowid = 0; /* Not needed. Only used to silence a warning. */
13095 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
13096 if( rc!=SQLITE_OK ){
13097 goto abort_due_to_error;
13098 }
13099 pOut->u.i = rowid;
13100 pOut->flags = MEM_Int;
13101 }
13102 break;
13103 }
13104
13105 /* Opcode: IdxGE P1 P2 P3 P4 P5
13106 ** Synopsis: key=r[P3@P4]
13107 **
13108 ** The P4 register values beginning with P3 form an unpacked index
13109 ** key that omits the PRIMARY KEY. Compare this key value against the index
13110 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
13111 ** fields at the end.
13112 **
13113 ** If the P1 index entry is greater than or equal to the key value
13114 ** then jump to P2. Otherwise fall through to the next instruction.
13115 */
13116 /* Opcode: IdxGT P1 P2 P3 P4 P5
13117 ** Synopsis: key=r[P3@P4]
13118 **
13119 ** The P4 register values beginning with P3 form an unpacked index
13120 ** key that omits the PRIMARY KEY. Compare this key value against the index
13121 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
13122 ** fields at the end.
13123 **
13124 ** If the P1 index entry is greater than the key value
13125 ** then jump to P2. Otherwise fall through to the next instruction.
13126 */
13127 /* Opcode: IdxLT P1 P2 P3 P4 P5
13128 ** Synopsis: key=r[P3@P4]
13129 **
13130 ** The P4 register values beginning with P3 form an unpacked index
13131 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
13132 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
13133 ** ROWID on the P1 index.
13134 **
13135 ** If the P1 index entry is less than the key value then jump to P2.
13136 ** Otherwise fall through to the next instruction.
13137 */
13138 /* Opcode: IdxLE P1 P2 P3 P4 P5
13139 ** Synopsis: key=r[P3@P4]
13140 **
13141 ** The P4 register values beginning with P3 form an unpacked index
13142 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
13143 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
13144 ** ROWID on the P1 index.
13145 **
13146 ** If the P1 index entry is less than or equal to the key value then jump
13147 ** to P2. Otherwise fall through to the next instruction.
13148 */
13149 case OP_IdxLE: /* jump */
13150 case OP_IdxGT: /* jump */
13151 case OP_IdxLT: /* jump */
13152 case OP_IdxGE: { /* jump */
13153 VdbeCursor *pC;
13154 int res;
13155 UnpackedRecord r;
13156
13157 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
13158 pC = p->apCsr[pOp->p1];
13159 assert( pC!=0 );
13160 assert( pC->isOrdered );
13161 assert( pC->eCurType==CURTYPE_BTREE );
13162 assert( pC->uc.pCursor!=0);
13163 assert( pC->deferredMoveto==0 );
13164 assert( pOp->p5==0 || pOp->p5==1 );
13165 assert( pOp->p4type==P4_INT32 );
13166 r.pKeyInfo = pC->pKeyInfo;
13167 r.nField = (u16)pOp->p4.i;
13168 if( pOp->opcode<OP_IdxLT ){
13169 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
13170 r.default_rc = -1;
13171 }else{
13172 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
13173 r.default_rc = 0;
13174 }
13175 r.aMem = &aMem[pOp->p3];
13176 #ifdef SQLITE_DEBUG
13177 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
13178 #endif
13179 res = 0; /* Not needed. Only used to silence a warning. */
13180 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
13181 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
13182 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
13183 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
13184 res = -res;
13185 }else{
13186 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
13187 res++;
13188 }
13189 VdbeBranchTaken(res>0,2);
13190 if( res>0 ) goto jump_to_p2;
13191 break;
13192 }
13193
13194 /* Opcode: Destroy P1 P2 P3 * *
13195 **
13196 ** Delete an entire database table or index whose root page in the database
13197 ** file is given by P1.
13198 **
13199 ** The table being destroyed is in the main database file if P3==0. If
13200 ** P3==1 then the table to be clear is in the auxiliary database file
13201 ** that is used to store tables create using CREATE TEMPORARY TABLE.
13202 **
13203 ** If AUTOVACUUM is enabled then it is possible that another root page
13204 ** might be moved into the newly deleted root page in order to keep all
13205 ** root pages contiguous at the beginning of the database. The former
13206 ** value of the root page that moved - its value before the move occurred -
13207 ** is stored in register P2. If no page
13208 ** movement was required (because the table being dropped was already
13209 ** the last one in the database) then a zero is stored in register P2.
13210 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
13211 **
13212 ** See also: Clear
13213 */
13214 case OP_Destroy: { /* out2 */
13215 int iMoved;
13216 int iDb;
13217
13218 assert( p->readOnly==0 );
13219 pOut = out2Prerelease(p, pOp);
13220 pOut->flags = MEM_Null;
13221 if( db->nVdbeRead > db->nVDestroy+1 ){
13222 rc = SQLITE_LOCKED;
13223 p->errorAction = OE_Abort;
13224 }else{
13225 iDb = pOp->p3;
13226 assert( DbMaskTest(p->btreeMask, iDb) );
13227 iMoved = 0; /* Not needed. Only to silence a warning. */
13228 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
13229 pOut->flags = MEM_Int;
13230 pOut->u.i = iMoved;
13231 #ifndef SQLITE_OMIT_AUTOVACUUM
13232 if( rc==SQLITE_OK && iMoved!=0 ){
13233 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
13234 /* All OP_Destroy operations occur on the same btree */
13235 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
13236 resetSchemaOnFault = iDb+1;
13237 }
13238 #endif
13239 }
13240 break;
13241 }
13242
13243 /* Opcode: Clear P1 P2 P3
13244 **
13245 ** Delete all contents of the database table or index whose root page
13246 ** in the database file is given by P1. But, unlike Destroy, do not
13247 ** remove the table or index from the database file.
13248 **
13249 ** The table being clear is in the main database file if P2==0. If
13250 ** P2==1 then the table to be clear is in the auxiliary database file
13251 ** that is used to store tables create using CREATE TEMPORARY TABLE.
13252 **
13253 ** If the P3 value is non-zero, then the table referred to must be an
13254 ** intkey table (an SQL table, not an index). In this case the row change
13255 ** count is incremented by the number of rows in the table being cleared.
13256 ** If P3 is greater than zero, then the value stored in register P3 is
13257 ** also incremented by the number of rows in the table being cleared.
13258 **
13259 ** See also: Destroy
13260 */
13261 case OP_Clear: {
13262 int nChange;
13263
13264 nChange = 0;
13265 assert( p->readOnly==0 );
13266 assert( DbMaskTest(p->btreeMask, pOp->p2) );
13267 rc = sqlite3BtreeClearTable(
13268 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
13269 );
13270 if( pOp->p3 ){
13271 p->nChange += nChange;
13272 if( pOp->p3>0 ){
13273 assert( memIsValid(&aMem[pOp->p3]) );
13274 memAboutToChange(p, &aMem[pOp->p3]);
13275 aMem[pOp->p3].u.i += nChange;
13276 }
13277 }
13278 break;
13279 }
13280
13281 /* Opcode: ResetSorter P1 * * * *
13282 **
13283 ** Delete all contents from the ephemeral table or sorter
13284 ** that is open on cursor P1.
13285 **
13286 ** This opcode only works for cursors used for sorting and
13287 ** opened with OP_OpenEphemeral or OP_SorterOpen.
13288 */
13289 case OP_ResetSorter: {
13290 VdbeCursor *pC;
13291
13292 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
13293 pC = p->apCsr[pOp->p1];
13294 assert( pC!=0 );
13295 if( isSorter(pC) ){
13296 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
13297 }else{
13298 assert( pC->eCurType==CURTYPE_BTREE );
13299 assert( pC->isEphemeral );
13300 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
13301 }
13302 break;
13303 }
13304
13305 /* Opcode: CreateTable P1 P2 * * *
13306 ** Synopsis: r[P2]=root iDb=P1
13307 **
13308 ** Allocate a new table in the main database file if P1==0 or in the
13309 ** auxiliary database file if P1==1 or in an attached database if
13310 ** P1>1. Write the root page number of the new table into
13311 ** register P2
13312 **
13313 ** The difference between a table and an index is this: A table must
13314 ** have a 4-byte integer key and can have arbitrary data. An index
13315 ** has an arbitrary key but no data.
13316 **
13317 ** See also: CreateIndex
13318 */
13319 /* Opcode: CreateIndex P1 P2 * * *
13320 ** Synopsis: r[P2]=root iDb=P1
13321 **
13322 ** Allocate a new index in the main database file if P1==0 or in the
13323 ** auxiliary database file if P1==1 or in an attached database if
13324 ** P1>1. Write the root page number of the new table into
13325 ** register P2.
13326 **
13327 ** See documentation on OP_CreateTable for additional information.
13328 */
13329 case OP_CreateIndex: /* out2 */
13330 case OP_CreateTable: { /* out2 */
13331 int pgno;
13332 int flags;
13333 Db *pDb;
13334
13335 pOut = out2Prerelease(p, pOp);
13336 pgno = 0;
13337 assert( pOp->p1>=0 && pOp->p1<db->nDb );
13338 assert( DbMaskTest(p->btreeMask, pOp->p1) );
13339 assert( p->readOnly==0 );
13340 pDb = &db->aDb[pOp->p1];
13341 assert( pDb->pBt!=0 );
13342 if( pOp->opcode==OP_CreateTable ){
13343 /* flags = BTREE_INTKEY; */
13344 flags = BTREE_INTKEY;
13345 }else{
13346 flags = BTREE_BLOBKEY;
13347 }
13348 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
13349 pOut->u.i = pgno;
13350 break;
13351 }
13352
13353 /* Opcode: ParseSchema P1 * * P4 *
13354 **
13355 ** Read and parse all entries from the SQLITE_MASTER table of database P1
13356 ** that match the WHERE clause P4.
13357 **
13358 ** This opcode invokes the parser to create a new virtual machine,
13359 ** then runs the new virtual machine. It is thus a re-entrant opcode.
13360 */
13361 case OP_ParseSchema: {
13362 int iDb;
13363 const char *zMaster;
13364 char *zSql;
13365 InitData initData;
13366
13367 /* Any prepared statement that invokes this opcode will hold mutexes
13368 ** on every btree. This is a prerequisite for invoking
13369 ** sqlite3InitCallback().
13370 */
13371 #ifdef SQLITE_DEBUG
13372 for(iDb=0; iDb<db->nDb; iDb++){
13373 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
13374 }
13375 #endif
13376
13377 iDb = pOp->p1;
13378 assert( iDb>=0 && iDb<db->nDb );
13379 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
13380 /* Used to be a conditional */ {
13381 zMaster = SCHEMA_TABLE(iDb);
13382 initData.db = db;
13383 initData.iDb = pOp->p1;
13384 initData.pzErrMsg = &p->zErrMsg;
13385 zSql = sqlite3MPrintf(db,
13386 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
13387 db->aDb[iDb].zName, zMaster, pOp->p4.z);
13388 if( zSql==0 ){
13389 rc = SQLITE_NOMEM;
13390 }else{
13391 assert( db->init.busy==0 );
13392 db->init.busy = 1;
13393 initData.rc = SQLITE_OK;
13394 assert( !db->mallocFailed );
13395 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
13396 if( rc==SQLITE_OK ) rc = initData.rc;
13397 sqlite3DbFree(db, zSql);
13398 db->init.busy = 0;
13399 }
13400 }
13401 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
13402 if( rc==SQLITE_NOMEM ){
13403 goto no_mem;
13404 }
13405 break;
13406 }
13407
13408 #if !defined(SQLITE_OMIT_ANALYZE)
13409 /* Opcode: LoadAnalysis P1 * * * *
13410 **
13411 ** Read the sqlite_stat1 table for database P1 and load the content
13412 ** of that table into the internal index hash table. This will cause
13413 ** the analysis to be used when preparing all subsequent queries.
13414 */
13415 case OP_LoadAnalysis: {
13416 assert( pOp->p1>=0 && pOp->p1<db->nDb );
13417 rc = sqlite3AnalysisLoad(db, pOp->p1);
13418 break;
13419 }
13420 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
13421
13422 /* Opcode: DropTable P1 * * P4 *
13423 **
13424 ** Remove the internal (in-memory) data structures that describe
13425 ** the table named P4 in database P1. This is called after a table
13426 ** is dropped from disk (using the Destroy opcode) in order to keep
13427 ** the internal representation of the
13428 ** schema consistent with what is on disk.
13429 */
13430 case OP_DropTable: {
13431 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
13432 break;
13433 }
13434
13435 /* Opcode: DropIndex P1 * * P4 *
13436 **
13437 ** Remove the internal (in-memory) data structures that describe
13438 ** the index named P4 in database P1. This is called after an index
13439 ** is dropped from disk (using the Destroy opcode)
13440 ** in order to keep the internal representation of the
13441 ** schema consistent with what is on disk.
13442 */
13443 case OP_DropIndex: {
13444 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
13445 break;
13446 }
13447
13448 /* Opcode: DropTrigger P1 * * P4 *
13449 **
13450 ** Remove the internal (in-memory) data structures that describe
13451 ** the trigger named P4 in database P1. This is called after a trigger
13452 ** is dropped from disk (using the Destroy opcode) in order to keep
13453 ** the internal representation of the
13454 ** schema consistent with what is on disk.
13455 */
13456 case OP_DropTrigger: {
13457 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
13458 break;
13459 }
13460
13461
13462 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
13463 /* Opcode: IntegrityCk P1 P2 P3 * P5
13464 **
13465 ** Do an analysis of the currently open database. Store in
13466 ** register P1 the text of an error message describing any problems.
13467 ** If no problems are found, store a NULL in register P1.
13468 **
13469 ** The register P3 contains the maximum number of allowed errors.
13470 ** At most reg(P3) errors will be reported.
13471 ** In other words, the analysis stops as soon as reg(P1) errors are
13472 ** seen. Reg(P1) is updated with the number of errors remaining.
13473 **
13474 ** The root page numbers of all tables in the database are integer
13475 ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
13476 ** total.
13477 **
13478 ** If P5 is not zero, the check is done on the auxiliary database
13479 ** file, not the main database file.
13480 **
13481 ** This opcode is used to implement the integrity_check pragma.
13482 */
13483 case OP_IntegrityCk: {
13484 int nRoot; /* Number of tables to check. (Number of root pages.) */
13485 int *aRoot; /* Array of rootpage numbers for tables to be checked */
13486 int j; /* Loop counter */
13487 int nErr; /* Number of errors reported */
13488 char *z; /* Text of the error report */
13489 Mem *pnErr; /* Register keeping track of errors remaining */
13490
13491 assert( p->bIsReader );
13492 nRoot = pOp->p2;
13493 assert( nRoot>0 );
13494 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
13495 if( aRoot==0 ) goto no_mem;
13496 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
13497 pnErr = &aMem[pOp->p3];
13498 assert( (pnErr->flags & MEM_Int)!=0 );
13499 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
13500 pIn1 = &aMem[pOp->p1];
13501 for(j=0; j<nRoot; j++){
13502 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
13503 }
13504 aRoot[j] = 0;
13505 assert( pOp->p5<db->nDb );
13506 assert( DbMaskTest(p->btreeMask, pOp->p5) );
13507 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
13508 (int)pnErr->u.i, &nErr);
13509 sqlite3DbFree(db, aRoot);
13510 pnErr->u.i -= nErr;
13511 sqlite3VdbeMemSetNull(pIn1);
13512 if( nErr==0 ){
13513 assert( z==0 );
13514 }else if( z==0 ){
13515 goto no_mem;
13516 }else{
13517 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
13518 }
13519 UPDATE_MAX_BLOBSIZE(pIn1);
13520 sqlite3VdbeChangeEncoding(pIn1, encoding);
13521 break;
13522 }
13523 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
13524
13525 /* Opcode: RowSetAdd P1 P2 * * *
13526 ** Synopsis: rowset(P1)=r[P2]
13527 **
13528 ** Insert the integer value held by register P2 into a boolean index
13529 ** held in register P1.
13530 **
13531 ** An assertion fails if P2 is not an integer.
13532 */
13533 case OP_RowSetAdd: { /* in1, in2 */
13534 pIn1 = &aMem[pOp->p1];
13535 pIn2 = &aMem[pOp->p2];
13536 assert( (pIn2->flags & MEM_Int)!=0 );
13537 if( (pIn1->flags & MEM_RowSet)==0 ){
13538 sqlite3VdbeMemSetRowSet(pIn1);
13539 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
13540 }
13541 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
13542 break;
13543 }
13544
13545 /* Opcode: RowSetRead P1 P2 P3 * *
13546 ** Synopsis: r[P3]=rowset(P1)
13547 **
13548 ** Extract the smallest value from boolean index P1 and put that value into
13549 ** register P3. Or, if boolean index P1 is initially empty, leave P3
13550 ** unchanged and jump to instruction P2.
13551 */
13552 case OP_RowSetRead: { /* jump, in1, out3 */
13553 i64 val;
13554
13555 pIn1 = &aMem[pOp->p1];
13556 if( (pIn1->flags & MEM_RowSet)==0
13557 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
13558 ){
13559 /* The boolean index is empty */
13560 sqlite3VdbeMemSetNull(pIn1);
13561 VdbeBranchTaken(1,2);
13562 goto jump_to_p2_and_check_for_interrupt;
13563 }else{
13564 /* A value was pulled from the index */
13565 VdbeBranchTaken(0,2);
13566 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
13567 }
13568 goto check_for_interrupt;
13569 }
13570
13571 /* Opcode: RowSetTest P1 P2 P3 P4
13572 ** Synopsis: if r[P3] in rowset(P1) goto P2
13573 **
13574 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
13575 ** contains a RowSet object and that RowSet object contains
13576 ** the value held in P3, jump to register P2. Otherwise, insert the
13577 ** integer in P3 into the RowSet and continue on to the
13578 ** next opcode.
13579 **
13580 ** The RowSet object is optimized for the case where successive sets
13581 ** of integers, where each set contains no duplicates. Each set
13582 ** of values is identified by a unique P4 value. The first set
13583 ** must have P4==0, the final set P4=-1. P4 must be either -1 or
13584 ** non-negative. For non-negative values of P4 only the lower 4
13585 ** bits are significant.
13586 **
13587 ** This allows optimizations: (a) when P4==0 there is no need to test
13588 ** the rowset object for P3, as it is guaranteed not to contain it,
13589 ** (b) when P4==-1 there is no need to insert the value, as it will
13590 ** never be tested for, and (c) when a value that is part of set X is
13591 ** inserted, there is no need to search to see if the same value was
13592 ** previously inserted as part of set X (only if it was previously
13593 ** inserted as part of some other set).
13594 */
13595 case OP_RowSetTest: { /* jump, in1, in3 */
13596 int iSet;
13597 int exists;
13598
13599 pIn1 = &aMem[pOp->p1];
13600 pIn3 = &aMem[pOp->p3];
13601 iSet = pOp->p4.i;
13602 assert( pIn3->flags&MEM_Int );
13603
13604 /* If there is anything other than a rowset object in memory cell P1,
13605 ** delete it now and initialize P1 with an empty rowset
13606 */
13607 if( (pIn1->flags & MEM_RowSet)==0 ){
13608 sqlite3VdbeMemSetRowSet(pIn1);
13609 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
13610 }
13611
13612 assert( pOp->p4type==P4_INT32 );
13613 assert( iSet==-1 || iSet>=0 );
13614 if( iSet ){
13615 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
13616 VdbeBranchTaken(exists!=0,2);
13617 if( exists ) goto jump_to_p2;
13618 }
13619 if( iSet>=0 ){
13620 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
13621 }
13622 break;
13623 }
13624
13625
13626 #ifndef SQLITE_OMIT_TRIGGER
13627
13628 /* Opcode: Program P1 P2 P3 P4 P5
13629 **
13630 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
13631 **
13632 ** P1 contains the address of the memory cell that contains the first memory
13633 ** cell in an array of values used as arguments to the sub-program. P2
13634 ** contains the address to jump to if the sub-program throws an IGNORE
13635 ** exception using the RAISE() function. Register P3 contains the address
13636 ** of a memory cell in this (the parent) VM that is used to allocate the
13637 ** memory required by the sub-vdbe at runtime.
13638 **
13639 ** P4 is a pointer to the VM containing the trigger program.
13640 **
13641 ** If P5 is non-zero, then recursive program invocation is enabled.
13642 */
13643 case OP_Program: { /* jump */
13644 int nMem; /* Number of memory registers for sub-program */
13645 int nByte; /* Bytes of runtime space required for sub-program */
13646 Mem *pRt; /* Register to allocate runtime space */
13647 Mem *pMem; /* Used to iterate through memory cells */
13648 Mem *pEnd; /* Last memory cell in new array */
13649 VdbeFrame *pFrame; /* New vdbe frame to execute in */
13650 SubProgram *pProgram; /* Sub-program to execute */
13651 void *t; /* Token identifying trigger */
13652
13653 pProgram = pOp->p4.pProgram;
13654 pRt = &aMem[pOp->p3];
13655 assert( pProgram->nOp>0 );
13656
13657 /* If the p5 flag is clear, then recursive invocation of triggers is
13658 ** disabled for backwards compatibility (p5 is set if this sub-program
13659 ** is really a trigger, not a foreign key action, and the flag set
13660 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
13661 **
13662 ** It is recursive invocation of triggers, at the SQL level, that is
13663 ** disabled. In some cases a single trigger may generate more than one
13664 ** SubProgram (if the trigger may be executed with more than one different
13665 ** ON CONFLICT algorithm). SubProgram structures associated with a
13666 ** single trigger all have the same value for the SubProgram.token
13667 ** variable. */
13668 if( pOp->p5 ){
13669 t = pProgram->token;
13670 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
13671 if( pFrame ) break;
13672 }
13673
13674 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
13675 rc = SQLITE_ERROR;
13676 sqlite3VdbeError(p, "too many levels of trigger recursion");
13677 break;
13678 }
13679
13680 /* Register pRt is used to store the memory required to save the state
13681 ** of the current program, and the memory required at runtime to execute
13682 ** the trigger program. If this trigger has been fired before, then pRt
13683 ** is already allocated. Otherwise, it must be initialized. */
13684 if( (pRt->flags&MEM_Frame)==0 ){
13685 /* SubProgram.nMem is set to the number of memory cells used by the
13686 ** program stored in SubProgram.aOp. As well as these, one memory
13687 ** cell is required for each cursor used by the program. Set local
13688 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
13689 */
13690 nMem = pProgram->nMem + pProgram->nCsr;
13691 nByte = ROUND8(sizeof(VdbeFrame))
13692 + nMem * sizeof(Mem)
13693 + pProgram->nCsr * sizeof(VdbeCursor *)
13694 + pProgram->nOnce * sizeof(u8);
13695 pFrame = sqlite3DbMallocZero(db, nByte);
13696 if( !pFrame ){
13697 goto no_mem;
13698 }
13699 sqlite3VdbeMemRelease(pRt);
13700 pRt->flags = MEM_Frame;
13701 pRt->u.pFrame = pFrame;
13702
13703 pFrame->v = p;
13704 pFrame->nChildMem = nMem;
13705 pFrame->nChildCsr = pProgram->nCsr;
13706 pFrame->pc = (int)(pOp - aOp);
13707 pFrame->aMem = p->aMem;
13708 pFrame->nMem = p->nMem;
13709 pFrame->apCsr = p->apCsr;
13710 pFrame->nCursor = p->nCursor;
13711 pFrame->aOp = p->aOp;
13712 pFrame->nOp = p->nOp;
13713 pFrame->token = pProgram->token;
13714 pFrame->aOnceFlag = p->aOnceFlag;
13715 pFrame->nOnceFlag = p->nOnceFlag;
13716 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
13717 pFrame->anExec = p->anExec;
13718 #endif
13719
13720 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
13721 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
13722 pMem->flags = MEM_Undefined;
13723 pMem->db = db;
13724 }
13725 }else{
13726 pFrame = pRt->u.pFrame;
13727 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
13728 assert( pProgram->nCsr==pFrame->nChildCsr );
13729 assert( (int)(pOp - aOp)==pFrame->pc );
13730 }
13731
13732 p->nFrame++;
13733 pFrame->pParent = p->pFrame;
13734 pFrame->lastRowid = lastRowid;
13735 pFrame->nChange = p->nChange;
13736 pFrame->nDbChange = p->db->nChange;
13737 p->nChange = 0;
13738 p->pFrame = pFrame;
13739 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
13740 p->nMem = pFrame->nChildMem;
13741 p->nCursor = (u16)pFrame->nChildCsr;
13742 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
13743 p->aOp = aOp = pProgram->aOp;
13744 p->nOp = pProgram->nOp;
13745 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
13746 p->nOnceFlag = pProgram->nOnce;
13747 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
13748 p->anExec = 0;
13749 #endif
13750 pOp = &aOp[-1];
13751 memset(p->aOnceFlag, 0, p->nOnceFlag);
13752
13753 break;
13754 }
13755
13756 /* Opcode: Param P1 P2 * * *
13757 **
13758 ** This opcode is only ever present in sub-programs called via the
13759 ** OP_Program instruction. Copy a value currently stored in a memory
13760 ** cell of the calling (parent) frame to cell P2 in the current frames
13761 ** address space. This is used by trigger programs to access the new.*
13762 ** and old.* values.
13763 **
13764 ** The address of the cell in the parent frame is determined by adding
13765 ** the value of the P1 argument to the value of the P1 argument to the
13766 ** calling OP_Program instruction.
13767 */
13768 case OP_Param: { /* out2 */
13769 VdbeFrame *pFrame;
13770 Mem *pIn;
13771 pOut = out2Prerelease(p, pOp);
13772 pFrame = p->pFrame;
13773 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
13774 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
13775 break;
13776 }
13777
13778 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
13779
13780 #ifndef SQLITE_OMIT_FOREIGN_KEY
13781 /* Opcode: FkCounter P1 P2 * * *
13782 ** Synopsis: fkctr[P1]+=P2
13783 **
13784 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
13785 ** If P1 is non-zero, the database constraint counter is incremented
13786 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
13787 ** statement counter is incremented (immediate foreign key constraints).
13788 */
13789 case OP_FkCounter: {
13790 if( db->flags & SQLITE_DeferFKs ){
13791 db->nDeferredImmCons += pOp->p2;
13792 }else if( pOp->p1 ){
13793 db->nDeferredCons += pOp->p2;
13794 }else{
13795 p->nFkConstraint += pOp->p2;
13796 }
13797 break;
13798 }
13799
13800 /* Opcode: FkIfZero P1 P2 * * *
13801 ** Synopsis: if fkctr[P1]==0 goto P2
13802 **
13803 ** This opcode tests if a foreign key constraint-counter is currently zero.
13804 ** If so, jump to instruction P2. Otherwise, fall through to the next
13805 ** instruction.
13806 **
13807 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
13808 ** is zero (the one that counts deferred constraint violations). If P1 is
13809 ** zero, the jump is taken if the statement constraint-counter is zero
13810 ** (immediate foreign key constraint violations).
13811 */
13812 case OP_FkIfZero: { /* jump */
13813 if( pOp->p1 ){
13814 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
13815 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
13816 }else{
13817 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
13818 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
13819 }
13820 break;
13821 }
13822 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
13823
13824 #ifndef SQLITE_OMIT_AUTOINCREMENT
13825 /* Opcode: MemMax P1 P2 * * *
13826 ** Synopsis: r[P1]=max(r[P1],r[P2])
13827 **
13828 ** P1 is a register in the root frame of this VM (the root frame is
13829 ** different from the current frame if this instruction is being executed
13830 ** within a sub-program). Set the value of register P1 to the maximum of
13831 ** its current value and the value in register P2.
13832 **
13833 ** This instruction throws an error if the memory cell is not initially
13834 ** an integer.
13835 */
13836 case OP_MemMax: { /* in2 */
13837 VdbeFrame *pFrame;
13838 if( p->pFrame ){
13839 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
13840 pIn1 = &pFrame->aMem[pOp->p1];
13841 }else{
13842 pIn1 = &aMem[pOp->p1];
13843 }
13844 assert( memIsValid(pIn1) );
13845 sqlite3VdbeMemIntegerify(pIn1);
13846 pIn2 = &aMem[pOp->p2];
13847 sqlite3VdbeMemIntegerify(pIn2);
13848 if( pIn1->u.i<pIn2->u.i){
13849 pIn1->u.i = pIn2->u.i;
13850 }
13851 break;
13852 }
13853 #endif /* SQLITE_OMIT_AUTOINCREMENT */
13854
13855 /* Opcode: IfPos P1 P2 P3 * *
13856 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
13857 **
13858 ** Register P1 must contain an integer.
13859 ** If the value of register P1 is 1 or greater, subtract P3 from the
13860 ** value in P1 and jump to P2.
13861 **
13862 ** If the initial value of register P1 is less than 1, then the
13863 ** value is unchanged and control passes through to the next instruction.
13864 */
13865 case OP_IfPos: { /* jump, in1 */
13866 pIn1 = &aMem[pOp->p1];
13867 assert( pIn1->flags&MEM_Int );
13868 VdbeBranchTaken( pIn1->u.i>0, 2);
13869 if( pIn1->u.i>0 ){
13870 pIn1->u.i -= pOp->p3;
13871 goto jump_to_p2;
13872 }
13873 break;
13874 }
13875
13876 /* Opcode: SetIfNotPos P1 P2 P3 * *
13877 ** Synopsis: if r[P1]<=0 then r[P2]=P3
13878 **
13879 ** Register P1 must contain an integer.
13880 ** If the value of register P1 is not positive (if it is less than 1) then
13881 ** set the value of register P2 to be the integer P3.
13882 */
13883 case OP_SetIfNotPos: { /* in1, in2 */
13884 pIn1 = &aMem[pOp->p1];
13885 assert( pIn1->flags&MEM_Int );
13886 if( pIn1->u.i<=0 ){
13887 pOut = out2Prerelease(p, pOp);
13888 pOut->u.i = pOp->p3;
13889 }
13890 break;
13891 }
13892
13893 /* Opcode: IfNotZero P1 P2 P3 * *
13894 ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
13895 **
13896 ** Register P1 must contain an integer. If the content of register P1 is
13897 ** initially nonzero, then subtract P3 from the value in register P1 and
13898 ** jump to P2. If register P1 is initially zero, leave it unchanged
13899 ** and fall through.
13900 */
13901 case OP_IfNotZero: { /* jump, in1 */
13902 pIn1 = &aMem[pOp->p1];
13903 assert( pIn1->flags&MEM_Int );
13904 VdbeBranchTaken(pIn1->u.i<0, 2);
13905 if( pIn1->u.i ){
13906 pIn1->u.i -= pOp->p3;
13907 goto jump_to_p2;
13908 }
13909 break;
13910 }
13911
13912 /* Opcode: DecrJumpZero P1 P2 * * *
13913 ** Synopsis: if (--r[P1])==0 goto P2
13914 **
13915 ** Register P1 must hold an integer. Decrement the value in register P1
13916 ** then jump to P2 if the new value is exactly zero.
13917 */
13918 case OP_DecrJumpZero: { /* jump, in1 */
13919 pIn1 = &aMem[pOp->p1];
13920 assert( pIn1->flags&MEM_Int );
13921 pIn1->u.i--;
13922 VdbeBranchTaken(pIn1->u.i==0, 2);
13923 if( pIn1->u.i==0 ) goto jump_to_p2;
13924 break;
13925 }
13926
13927
13928 /* Opcode: JumpZeroIncr P1 P2 * * *
13929 ** Synopsis: if (r[P1]++)==0 ) goto P2
13930 **
13931 ** The register P1 must contain an integer. If register P1 is initially
13932 ** zero, then jump to P2. Increment register P1 regardless of whether or
13933 ** not the jump is taken.
13934 */
13935 case OP_JumpZeroIncr: { /* jump, in1 */
13936 pIn1 = &aMem[pOp->p1];
13937 assert( pIn1->flags&MEM_Int );
13938 VdbeBranchTaken(pIn1->u.i==0, 2);
13939 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
13940 break;
13941 }
13942
13943 /* Opcode: AggStep0 * P2 P3 P4 P5
13944 ** Synopsis: accum=r[P3] step(r[P2@P5])
13945 **
13946 ** Execute the step function for an aggregate. The
13947 ** function has P5 arguments. P4 is a pointer to the FuncDef
13948 ** structure that specifies the function. Register P3 is the
13949 ** accumulator.
13950 **
13951 ** The P5 arguments are taken from register P2 and its
13952 ** successors.
13953 */
13954 /* Opcode: AggStep * P2 P3 P4 P5
13955 ** Synopsis: accum=r[P3] step(r[P2@P5])
13956 **
13957 ** Execute the step function for an aggregate. The
13958 ** function has P5 arguments. P4 is a pointer to an sqlite3_context
13959 ** object that is used to run the function. Register P3 is
13960 ** as the accumulator.
13961 **
13962 ** The P5 arguments are taken from register P2 and its
13963 ** successors.
13964 **
13965 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
13966 ** the FuncDef stored in P4 is converted into an sqlite3_context and
13967 ** the opcode is changed. In this way, the initialization of the
13968 ** sqlite3_context only happens once, instead of on each call to the
13969 ** step function.
13970 */
13971 case OP_AggStep0: {
13972 int n;
13973 sqlite3_context *pCtx;
13974
13975 assert( pOp->p4type==P4_FUNCDEF );
13976 n = pOp->p5;
13977 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
13978 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
13979 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
13980 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
13981 if( pCtx==0 ) goto no_mem;
13982 pCtx->pMem = 0;
13983 pCtx->pFunc = pOp->p4.pFunc;
13984 pCtx->iOp = (int)(pOp - aOp);
13985 pCtx->pVdbe = p;
13986 pCtx->argc = n;
13987 pOp->p4type = P4_FUNCCTX;
13988 pOp->p4.pCtx = pCtx;
13989 pOp->opcode = OP_AggStep;
13990 /* Fall through into OP_AggStep */
13991 }
13992 case OP_AggStep: {
13993 int i;
13994 sqlite3_context *pCtx;
13995 Mem *pMem;
13996 Mem t;
13997
13998 assert( pOp->p4type==P4_FUNCCTX );
13999 pCtx = pOp->p4.pCtx;
14000 pMem = &aMem[pOp->p3];
14001
14002 /* If this function is inside of a trigger, the register array in aMem[]
14003 ** might change from one evaluation to the next. The next block of code
14004 ** checks to see if the register array has changed, and if so it
14005 ** reinitializes the relavant parts of the sqlite3_context object */
14006 if( pCtx->pMem != pMem ){
14007 pCtx->pMem = pMem;
14008 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
14009 }
14010
14011 #ifdef SQLITE_DEBUG
14012 for(i=0; i<pCtx->argc; i++){
14013 assert( memIsValid(pCtx->argv[i]) );
14014 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
14015 }
14016 #endif
14017
14018 pMem->n++;
14019 sqlite3VdbeMemInit(&t, db, MEM_Null);
14020 pCtx->pOut = &t;
14021 pCtx->fErrorOrAux = 0;
14022 pCtx->skipFlag = 0;
14023 (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
14024 if( pCtx->fErrorOrAux ){
14025 if( pCtx->isError ){
14026 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
14027 rc = pCtx->isError;
14028 }
14029 sqlite3VdbeMemRelease(&t);
14030 }else{
14031 assert( t.flags==MEM_Null );
14032 }
14033 if( pCtx->skipFlag ){
14034 assert( pOp[-1].opcode==OP_CollSeq );
14035 i = pOp[-1].p1;
14036 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
14037 }
14038 break;
14039 }
14040
14041 /* Opcode: AggFinal P1 P2 * P4 *
14042 ** Synopsis: accum=r[P1] N=P2
14043 **
14044 ** Execute the finalizer function for an aggregate. P1 is
14045 ** the memory location that is the accumulator for the aggregate.
14046 **
14047 ** P2 is the number of arguments that the step function takes and
14048 ** P4 is a pointer to the FuncDef for this function. The P2
14049 ** argument is not used by this opcode. It is only there to disambiguate
14050 ** functions that can take varying numbers of arguments. The
14051 ** P4 argument is only needed for the degenerate case where
14052 ** the step function was not previously called.
14053 */
14054 case OP_AggFinal: {
14055 Mem *pMem;
14056 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
14057 pMem = &aMem[pOp->p1];
14058 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
14059 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
14060 if( rc ){
14061 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
14062 }
14063 sqlite3VdbeChangeEncoding(pMem, encoding);
14064 UPDATE_MAX_BLOBSIZE(pMem);
14065 if( sqlite3VdbeMemTooBig(pMem) ){
14066 goto too_big;
14067 }
14068 break;
14069 }
14070
14071 #ifndef SQLITE_OMIT_WAL
14072 /* Opcode: Checkpoint P1 P2 P3 * *
14073 **
14074 ** Checkpoint database P1. This is a no-op if P1 is not currently in
14075 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
14076 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
14077 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
14078 ** WAL after the checkpoint into mem[P3+1] and the number of pages
14079 ** in the WAL that have been checkpointed after the checkpoint
14080 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
14081 ** mem[P3+2] are initialized to -1.
14082 */
14083 case OP_Checkpoint: {
14084 int i; /* Loop counter */
14085 int aRes[3]; /* Results */
14086 Mem *pMem; /* Write results here */
14087
14088 assert( p->readOnly==0 );
14089 aRes[0] = 0;
14090 aRes[1] = aRes[2] = -1;
14091 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
14092 || pOp->p2==SQLITE_CHECKPOINT_FULL
14093 || pOp->p2==SQLITE_CHECKPOINT_RESTART
14094 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
14095 );
14096 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
14097 if( rc==SQLITE_BUSY ){
14098 rc = SQLITE_OK;
14099 aRes[0] = 1;
14100 }
14101 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
14102 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
14103 }
14104 break;
14105 };
14106 #endif
14107
14108 #ifndef SQLITE_OMIT_PRAGMA
14109 /* Opcode: JournalMode P1 P2 P3 * *
14110 **
14111 ** Change the journal mode of database P1 to P3. P3 must be one of the
14112 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
14113 ** modes (delete, truncate, persist, off and memory), this is a simple
14114 ** operation. No IO is required.
14115 **
14116 ** If changing into or out of WAL mode the procedure is more complicated.
14117 **
14118 ** Write a string containing the final journal-mode to register P2.
14119 */
14120 case OP_JournalMode: { /* out2 */
14121 Btree *pBt; /* Btree to change journal mode of */
14122 Pager *pPager; /* Pager associated with pBt */
14123 int eNew; /* New journal mode */
14124 int eOld; /* The old journal mode */
14125 #ifndef SQLITE_OMIT_WAL
14126 const char *zFilename; /* Name of database file for pPager */
14127 #endif
14128
14129 pOut = out2Prerelease(p, pOp);
14130 eNew = pOp->p3;
14131 assert( eNew==PAGER_JOURNALMODE_DELETE
14132 || eNew==PAGER_JOURNALMODE_TRUNCATE
14133 || eNew==PAGER_JOURNALMODE_PERSIST
14134 || eNew==PAGER_JOURNALMODE_OFF
14135 || eNew==PAGER_JOURNALMODE_MEMORY
14136 || eNew==PAGER_JOURNALMODE_WAL
14137 || eNew==PAGER_JOURNALMODE_QUERY
14138 );
14139 assert( pOp->p1>=0 && pOp->p1<db->nDb );
14140 assert( p->readOnly==0 );
14141
14142 pBt = db->aDb[pOp->p1].pBt;
14143 pPager = sqlite3BtreePager(pBt);
14144 eOld = sqlite3PagerGetJournalMode(pPager);
14145 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
14146 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
14147
14148 #ifndef SQLITE_OMIT_WAL
14149 zFilename = sqlite3PagerFilename(pPager, 1);
14150
14151 /* Do not allow a transition to journal_mode=WAL for a database
14152 ** in temporary storage or if the VFS does not support shared memory
14153 */
14154 if( eNew==PAGER_JOURNALMODE_WAL
14155 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
14156 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
14157 ){
14158 eNew = eOld;
14159 }
14160
14161 if( (eNew!=eOld)
14162 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
14163 ){
14164 if( !db->autoCommit || db->nVdbeRead>1 ){
14165 rc = SQLITE_ERROR;
14166 sqlite3VdbeError(p,
14167 "cannot change %s wal mode from within a transaction",
14168 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
14169 );
14170 break;
14171 }else{
14172
14173 if( eOld==PAGER_JOURNALMODE_WAL ){
14174 /* If leaving WAL mode, close the log file. If successful, the call
14175 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
14176 ** file. An EXCLUSIVE lock may still be held on the database file
14177 ** after a successful return.
14178 */
14179 rc = sqlite3PagerCloseWal(pPager);
14180 if( rc==SQLITE_OK ){
14181 sqlite3PagerSetJournalMode(pPager, eNew);
14182 }
14183 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
14184 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
14185 ** as an intermediate */
14186 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
14187 }
14188
14189 /* Open a transaction on the database file. Regardless of the journal
14190 ** mode, this transaction always uses a rollback journal.
14191 */
14192 assert( sqlite3BtreeIsInTrans(pBt)==0 );
14193 if( rc==SQLITE_OK ){
14194 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
14195 }
14196 }
14197 }
14198 #endif /* ifndef SQLITE_OMIT_WAL */
14199
14200 if( rc ){
14201 eNew = eOld;
14202 }
14203 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
14204
14205 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
14206 pOut->z = (char *)sqlite3JournalModename(eNew);
14207 pOut->n = sqlite3Strlen30(pOut->z);
14208 pOut->enc = SQLITE_UTF8;
14209 sqlite3VdbeChangeEncoding(pOut, encoding);
14210 break;
14211 };
14212 #endif /* SQLITE_OMIT_PRAGMA */
14213
14214 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
14215 /* Opcode: Vacuum * * * * *
14216 **
14217 ** Vacuum the entire database. This opcode will cause other virtual
14218 ** machines to be created and run. It may not be called from within
14219 ** a transaction.
14220 */
14221 case OP_Vacuum: {
14222 assert( p->readOnly==0 );
14223 rc = sqlite3RunVacuum(&p->zErrMsg, db);
14224 break;
14225 }
14226 #endif
14227
14228 #if !defined(SQLITE_OMIT_AUTOVACUUM)
14229 /* Opcode: IncrVacuum P1 P2 * * *
14230 **
14231 ** Perform a single step of the incremental vacuum procedure on
14232 ** the P1 database. If the vacuum has finished, jump to instruction
14233 ** P2. Otherwise, fall through to the next instruction.
14234 */
14235 case OP_IncrVacuum: { /* jump */
14236 Btree *pBt;
14237
14238 assert( pOp->p1>=0 && pOp->p1<db->nDb );
14239 assert( DbMaskTest(p->btreeMask, pOp->p1) );
14240 assert( p->readOnly==0 );
14241 pBt = db->aDb[pOp->p1].pBt;
14242 rc = sqlite3BtreeIncrVacuum(pBt);
14243 VdbeBranchTaken(rc==SQLITE_DONE,2);
14244 if( rc==SQLITE_DONE ){
14245 rc = SQLITE_OK;
14246 goto jump_to_p2;
14247 }
14248 break;
14249 }
14250 #endif
14251
14252 /* Opcode: Expire P1 * * * *
14253 **
14254 ** Cause precompiled statements to expire. When an expired statement
14255 ** is executed using sqlite3_step() it will either automatically
14256 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
14257 ** or it will fail with SQLITE_SCHEMA.
14258 **
14259 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
14260 ** then only the currently executing statement is expired.
14261 */
14262 case OP_Expire: {
14263 if( !pOp->p1 ){
14264 sqlite3ExpirePreparedStatements(db);
14265 }else{
14266 p->expired = 1;
14267 }
14268 break;
14269 }
14270
14271 #ifndef SQLITE_OMIT_SHARED_CACHE
14272 /* Opcode: TableLock P1 P2 P3 P4 *
14273 ** Synopsis: iDb=P1 root=P2 write=P3
14274 **
14275 ** Obtain a lock on a particular table. This instruction is only used when
14276 ** the shared-cache feature is enabled.
14277 **
14278 ** P1 is the index of the database in sqlite3.aDb[] of the database
14279 ** on which the lock is acquired. A readlock is obtained if P3==0 or
14280 ** a write lock if P3==1.
14281 **
14282 ** P2 contains the root-page of the table to lock.
14283 **
14284 ** P4 contains a pointer to the name of the table being locked. This is only
14285 ** used to generate an error message if the lock cannot be obtained.
14286 */
14287 case OP_TableLock: {
14288 u8 isWriteLock = (u8)pOp->p3;
14289 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
14290 int p1 = pOp->p1;
14291 assert( p1>=0 && p1<db->nDb );
14292 assert( DbMaskTest(p->btreeMask, p1) );
14293 assert( isWriteLock==0 || isWriteLock==1 );
14294 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
14295 if( (rc&0xFF)==SQLITE_LOCKED ){
14296 const char *z = pOp->p4.z;
14297 sqlite3VdbeError(p, "database table is locked: %s", z);
14298 }
14299 }
14300 break;
14301 }
14302 #endif /* SQLITE_OMIT_SHARED_CACHE */
14303
14304 #ifndef SQLITE_OMIT_VIRTUALTABLE
14305 /* Opcode: VBegin * * * P4 *
14306 **
14307 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
14308 ** xBegin method for that table.
14309 **
14310 ** Also, whether or not P4 is set, check that this is not being called from
14311 ** within a callback to a virtual table xSync() method. If it is, the error
14312 ** code will be set to SQLITE_LOCKED.
14313 */
14314 case OP_VBegin: {
14315 VTable *pVTab;
14316 pVTab = pOp->p4.pVtab;
14317 rc = sqlite3VtabBegin(db, pVTab);
14318 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
14319 break;
14320 }
14321 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14322
14323 #ifndef SQLITE_OMIT_VIRTUALTABLE
14324 /* Opcode: VCreate P1 P2 * * *
14325 **
14326 ** P2 is a register that holds the name of a virtual table in database
14327 ** P1. Call the xCreate method for that table.
14328 */
14329 case OP_VCreate: {
14330 Mem sMem; /* For storing the record being decoded */
14331 const char *zTab; /* Name of the virtual table */
14332
14333 memset(&sMem, 0, sizeof(sMem));
14334 sMem.db = db;
14335 /* Because P2 is always a static string, it is impossible for the
14336 ** sqlite3VdbeMemCopy() to fail */
14337 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
14338 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
14339 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
14340 assert( rc==SQLITE_OK );
14341 zTab = (const char*)sqlite3_value_text(&sMem);
14342 assert( zTab || db->mallocFailed );
14343 if( zTab ){
14344 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
14345 }
14346 sqlite3VdbeMemRelease(&sMem);
14347 break;
14348 }
14349 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14350
14351 #ifndef SQLITE_OMIT_VIRTUALTABLE
14352 /* Opcode: VDestroy P1 * * P4 *
14353 **
14354 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
14355 ** of that table.
14356 */
14357 case OP_VDestroy: {
14358 db->nVDestroy++;
14359 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
14360 db->nVDestroy--;
14361 break;
14362 }
14363 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14364
14365 #ifndef SQLITE_OMIT_VIRTUALTABLE
14366 /* Opcode: VOpen P1 * * P4 *
14367 **
14368 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
14369 ** P1 is a cursor number. This opcode opens a cursor to the virtual
14370 ** table and stores that cursor in P1.
14371 */
14372 case OP_VOpen: {
14373 VdbeCursor *pCur;
14374 sqlite3_vtab_cursor *pVCur;
14375 sqlite3_vtab *pVtab;
14376 const sqlite3_module *pModule;
14377
14378 assert( p->bIsReader );
14379 pCur = 0;
14380 pVCur = 0;
14381 pVtab = pOp->p4.pVtab->pVtab;
14382 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
14383 rc = SQLITE_LOCKED;
14384 break;
14385 }
14386 pModule = pVtab->pModule;
14387 rc = pModule->xOpen(pVtab, &pVCur);
14388 sqlite3VtabImportErrmsg(p, pVtab);
14389 if( SQLITE_OK==rc ){
14390 /* Initialize sqlite3_vtab_cursor base class */
14391 pVCur->pVtab = pVtab;
14392
14393 /* Initialize vdbe cursor object */
14394 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
14395 if( pCur ){
14396 pCur->uc.pVCur = pVCur;
14397 pVtab->nRef++;
14398 }else{
14399 assert( db->mallocFailed );
14400 pModule->xClose(pVCur);
14401 goto no_mem;
14402 }
14403 }
14404 break;
14405 }
14406 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14407
14408 #ifndef SQLITE_OMIT_VIRTUALTABLE
14409 /* Opcode: VFilter P1 P2 P3 P4 *
14410 ** Synopsis: iplan=r[P3] zplan='P4'
14411 **
14412 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
14413 ** the filtered result set is empty.
14414 **
14415 ** P4 is either NULL or a string that was generated by the xBestIndex
14416 ** method of the module. The interpretation of the P4 string is left
14417 ** to the module implementation.
14418 **
14419 ** This opcode invokes the xFilter method on the virtual table specified
14420 ** by P1. The integer query plan parameter to xFilter is stored in register
14421 ** P3. Register P3+1 stores the argc parameter to be passed to the
14422 ** xFilter method. Registers P3+2..P3+1+argc are the argc
14423 ** additional parameters which are passed to
14424 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
14425 **
14426 ** A jump is made to P2 if the result set after filtering would be empty.
14427 */
14428 case OP_VFilter: { /* jump */
14429 int nArg;
14430 int iQuery;
14431 const sqlite3_module *pModule;
14432 Mem *pQuery;
14433 Mem *pArgc;
14434 sqlite3_vtab_cursor *pVCur;
14435 sqlite3_vtab *pVtab;
14436 VdbeCursor *pCur;
14437 int res;
14438 int i;
14439 Mem **apArg;
14440
14441 pQuery = &aMem[pOp->p3];
14442 pArgc = &pQuery[1];
14443 pCur = p->apCsr[pOp->p1];
14444 assert( memIsValid(pQuery) );
14445 REGISTER_TRACE(pOp->p3, pQuery);
14446 assert( pCur->eCurType==CURTYPE_VTAB );
14447 pVCur = pCur->uc.pVCur;
14448 pVtab = pVCur->pVtab;
14449 pModule = pVtab->pModule;
14450
14451 /* Grab the index number and argc parameters */
14452 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
14453 nArg = (int)pArgc->u.i;
14454 iQuery = (int)pQuery->u.i;
14455
14456 /* Invoke the xFilter method */
14457 res = 0;
14458 apArg = p->apArg;
14459 for(i = 0; i<nArg; i++){
14460 apArg[i] = &pArgc[i+1];
14461 }
14462 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
14463 sqlite3VtabImportErrmsg(p, pVtab);
14464 if( rc==SQLITE_OK ){
14465 res = pModule->xEof(pVCur);
14466 }
14467 pCur->nullRow = 0;
14468 VdbeBranchTaken(res!=0,2);
14469 if( res ) goto jump_to_p2;
14470 break;
14471 }
14472 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14473
14474 #ifndef SQLITE_OMIT_VIRTUALTABLE
14475 /* Opcode: VColumn P1 P2 P3 * *
14476 ** Synopsis: r[P3]=vcolumn(P2)
14477 **
14478 ** Store the value of the P2-th column of
14479 ** the row of the virtual-table that the
14480 ** P1 cursor is pointing to into register P3.
14481 */
14482 case OP_VColumn: {
14483 sqlite3_vtab *pVtab;
14484 const sqlite3_module *pModule;
14485 Mem *pDest;
14486 sqlite3_context sContext;
14487
14488 VdbeCursor *pCur = p->apCsr[pOp->p1];
14489 assert( pCur->eCurType==CURTYPE_VTAB );
14490 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
14491 pDest = &aMem[pOp->p3];
14492 memAboutToChange(p, pDest);
14493 if( pCur->nullRow ){
14494 sqlite3VdbeMemSetNull(pDest);
14495 break;
14496 }
14497 pVtab = pCur->uc.pVCur->pVtab;
14498 pModule = pVtab->pModule;
14499 assert( pModule->xColumn );
14500 memset(&sContext, 0, sizeof(sContext));
14501 sContext.pOut = pDest;
14502 MemSetTypeFlag(pDest, MEM_Null);
14503 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
14504 sqlite3VtabImportErrmsg(p, pVtab);
14505 if( sContext.isError ){
14506 rc = sContext.isError;
14507 }
14508 sqlite3VdbeChangeEncoding(pDest, encoding);
14509 REGISTER_TRACE(pOp->p3, pDest);
14510 UPDATE_MAX_BLOBSIZE(pDest);
14511
14512 if( sqlite3VdbeMemTooBig(pDest) ){
14513 goto too_big;
14514 }
14515 break;
14516 }
14517 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14518
14519 #ifndef SQLITE_OMIT_VIRTUALTABLE
14520 /* Opcode: VNext P1 P2 * * *
14521 **
14522 ** Advance virtual table P1 to the next row in its result set and
14523 ** jump to instruction P2. Or, if the virtual table has reached
14524 ** the end of its result set, then fall through to the next instruction.
14525 */
14526 case OP_VNext: { /* jump */
14527 sqlite3_vtab *pVtab;
14528 const sqlite3_module *pModule;
14529 int res;
14530 VdbeCursor *pCur;
14531
14532 res = 0;
14533 pCur = p->apCsr[pOp->p1];
14534 assert( pCur->eCurType==CURTYPE_VTAB );
14535 if( pCur->nullRow ){
14536 break;
14537 }
14538 pVtab = pCur->uc.pVCur->pVtab;
14539 pModule = pVtab->pModule;
14540 assert( pModule->xNext );
14541
14542 /* Invoke the xNext() method of the module. There is no way for the
14543 ** underlying implementation to return an error if one occurs during
14544 ** xNext(). Instead, if an error occurs, true is returned (indicating that
14545 ** data is available) and the error code returned when xColumn or
14546 ** some other method is next invoked on the save virtual table cursor.
14547 */
14548 rc = pModule->xNext(pCur->uc.pVCur);
14549 sqlite3VtabImportErrmsg(p, pVtab);
14550 if( rc==SQLITE_OK ){
14551 res = pModule->xEof(pCur->uc.pVCur);
14552 }
14553 VdbeBranchTaken(!res,2);
14554 if( !res ){
14555 /* If there is data, jump to P2 */
14556 goto jump_to_p2_and_check_for_interrupt;
14557 }
14558 goto check_for_interrupt;
14559 }
14560 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14561
14562 #ifndef SQLITE_OMIT_VIRTUALTABLE
14563 /* Opcode: VRename P1 * * P4 *
14564 **
14565 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
14566 ** This opcode invokes the corresponding xRename method. The value
14567 ** in register P1 is passed as the zName argument to the xRename method.
14568 */
14569 case OP_VRename: {
14570 sqlite3_vtab *pVtab;
14571 Mem *pName;
14572
14573 pVtab = pOp->p4.pVtab->pVtab;
14574 pName = &aMem[pOp->p1];
14575 assert( pVtab->pModule->xRename );
14576 assert( memIsValid(pName) );
14577 assert( p->readOnly==0 );
14578 REGISTER_TRACE(pOp->p1, pName);
14579 assert( pName->flags & MEM_Str );
14580 testcase( pName->enc==SQLITE_UTF8 );
14581 testcase( pName->enc==SQLITE_UTF16BE );
14582 testcase( pName->enc==SQLITE_UTF16LE );
14583 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
14584 if( rc==SQLITE_OK ){
14585 rc = pVtab->pModule->xRename(pVtab, pName->z);
14586 sqlite3VtabImportErrmsg(p, pVtab);
14587 p->expired = 0;
14588 }
14589 break;
14590 }
14591 #endif
14592
14593 #ifndef SQLITE_OMIT_VIRTUALTABLE
14594 /* Opcode: VUpdate P1 P2 P3 P4 P5
14595 ** Synopsis: data=r[P3@P2]
14596 **
14597 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
14598 ** This opcode invokes the corresponding xUpdate method. P2 values
14599 ** are contiguous memory cells starting at P3 to pass to the xUpdate
14600 ** invocation. The value in register (P3+P2-1) corresponds to the
14601 ** p2th element of the argv array passed to xUpdate.
14602 **
14603 ** The xUpdate method will do a DELETE or an INSERT or both.
14604 ** The argv[0] element (which corresponds to memory cell P3)
14605 ** is the rowid of a row to delete. If argv[0] is NULL then no
14606 ** deletion occurs. The argv[1] element is the rowid of the new
14607 ** row. This can be NULL to have the virtual table select the new
14608 ** rowid for itself. The subsequent elements in the array are
14609 ** the values of columns in the new row.
14610 **
14611 ** If P2==1 then no insert is performed. argv[0] is the rowid of
14612 ** a row to delete.
14613 **
14614 ** P1 is a boolean flag. If it is set to true and the xUpdate call
14615 ** is successful, then the value returned by sqlite3_last_insert_rowid()
14616 ** is set to the value of the rowid for the row just inserted.
14617 **
14618 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
14619 ** apply in the case of a constraint failure on an insert or update.
14620 */
14621 case OP_VUpdate: {
14622 sqlite3_vtab *pVtab;
14623 const sqlite3_module *pModule;
14624 int nArg;
14625 int i;
14626 sqlite_int64 rowid;
14627 Mem **apArg;
14628 Mem *pX;
14629
14630 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
14631 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
14632 );
14633 assert( p->readOnly==0 );
14634 pVtab = pOp->p4.pVtab->pVtab;
14635 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
14636 rc = SQLITE_LOCKED;
14637 break;
14638 }
14639 pModule = pVtab->pModule;
14640 nArg = pOp->p2;
14641 assert( pOp->p4type==P4_VTAB );
14642 if( ALWAYS(pModule->xUpdate) ){
14643 u8 vtabOnConflict = db->vtabOnConflict;
14644 apArg = p->apArg;
14645 pX = &aMem[pOp->p3];
14646 for(i=0; i<nArg; i++){
14647 assert( memIsValid(pX) );
14648 memAboutToChange(p, pX);
14649 apArg[i] = pX;
14650 pX++;
14651 }
14652 db->vtabOnConflict = pOp->p5;
14653 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
14654 db->vtabOnConflict = vtabOnConflict;
14655 sqlite3VtabImportErrmsg(p, pVtab);
14656 if( rc==SQLITE_OK && pOp->p1 ){
14657 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
14658 db->lastRowid = lastRowid = rowid;
14659 }
14660 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
14661 if( pOp->p5==OE_Ignore ){
14662 rc = SQLITE_OK;
14663 }else{
14664 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
14665 }
14666 }else{
14667 p->nChange++;
14668 }
14669 }
14670 break;
14671 }
14672 #endif /* SQLITE_OMIT_VIRTUALTABLE */
14673
14674 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
14675 /* Opcode: Pagecount P1 P2 * * *
14676 **
14677 ** Write the current number of pages in database P1 to memory cell P2.
14678 */
14679 case OP_Pagecount: { /* out2 */
14680 pOut = out2Prerelease(p, pOp);
14681 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
14682 break;
14683 }
14684 #endif
14685
14686
14687 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
14688 /* Opcode: MaxPgcnt P1 P2 P3 * *
14689 **
14690 ** Try to set the maximum page count for database P1 to the value in P3.
14691 ** Do not let the maximum page count fall below the current page count and
14692 ** do not change the maximum page count value if P3==0.
14693 **
14694 ** Store the maximum page count after the change in register P2.
14695 */
14696 case OP_MaxPgcnt: { /* out2 */
14697 unsigned int newMax;
14698 Btree *pBt;
14699
14700 pOut = out2Prerelease(p, pOp);
14701 pBt = db->aDb[pOp->p1].pBt;
14702 newMax = 0;
14703 if( pOp->p3 ){
14704 newMax = sqlite3BtreeLastPage(pBt);
14705 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
14706 }
14707 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
14708 break;
14709 }
14710 #endif
14711
14712
14713 /* Opcode: Init * P2 * P4 *
14714 ** Synopsis: Start at P2
14715 **
14716 ** Programs contain a single instance of this opcode as the very first
14717 ** opcode.
14718 **
14719 ** If tracing is enabled (by the sqlite3_trace()) interface, then
14720 ** the UTF-8 string contained in P4 is emitted on the trace callback.
14721 ** Or if P4 is blank, use the string returned by sqlite3_sql().
14722 **
14723 ** If P2 is not zero, jump to instruction P2.
14724 */
14725 case OP_Init: { /* jump */
14726 char *zTrace;
14727 char *z;
14728
14729 #ifndef SQLITE_OMIT_TRACE
14730 if( db->xTrace
14731 && !p->doingRerun
14732 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
14733 ){
14734 z = sqlite3VdbeExpandSql(p, zTrace);
14735 db->xTrace(db->pTraceArg, z);
14736 sqlite3DbFree(db, z);
14737 }
14738 #ifdef SQLITE_USE_FCNTL_TRACE
14739 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
14740 if( zTrace ){
14741 int i;
14742 for(i=0; i<db->nDb; i++){
14743 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
14744 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
14745 }
14746 }
14747 #endif /* SQLITE_USE_FCNTL_TRACE */
14748 #ifdef SQLITE_DEBUG
14749 if( (db->flags & SQLITE_SqlTrace)!=0
14750 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
14751 ){
14752 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
14753 }
14754 #endif /* SQLITE_DEBUG */
14755 #endif /* SQLITE_OMIT_TRACE */
14756 if( pOp->p2 ) goto jump_to_p2;
14757 break;
14758 }
14759
14760 #ifdef SQLITE_ENABLE_CURSOR_HINTS
14761 /* Opcode: CursorHint P1 * * P4 *
14762 **
14763 ** Provide a hint to cursor P1 that it only needs to return rows that
14764 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
14765 ** to values currently held in registers. TK_COLUMN terms in the P4
14766 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
14767 */
14768 case OP_CursorHint: {
14769 VdbeCursor *pC;
14770
14771 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
14772 assert( pOp->p4type==P4_EXPR );
14773 pC = p->apCsr[pOp->p1];
14774 if( pC ){
14775 assert( pC->eCurType==CURTYPE_BTREE );
14776 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
14777 pOp->p4.pExpr, aMem);
14778 }
14779 break;
14780 }
14781 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
14782
14783 /* Opcode: Noop * * * * *
14784 **
14785 ** Do nothing. This instruction is often useful as a jump
14786 ** destination.
14787 */
14788 /*
14789 ** The magic Explain opcode are only inserted when explain==2 (which
14790 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
14791 ** This opcode records information from the optimizer. It is the
14792 ** the same as a no-op. This opcodesnever appears in a real VM program.
14793 */
14794 default: { /* This is really OP_Noop and OP_Explain */
14795 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
14796 break;
14797 }
14798
14799 /*****************************************************************************
14800 ** The cases of the switch statement above this line should all be indented
14801 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
14802 ** readability. From this point on down, the normal indentation rules are
14803 ** restored.
14804 *****************************************************************************/
14805 }
14806
14807 #ifdef VDBE_PROFILE
14808 {
14809 u64 endTime = sqlite3Hwtime();
14810 if( endTime>start ) pOrigOp->cycles += endTime - start;
14811 pOrigOp->cnt++;
14812 }
14813 #endif
14814
14815 /* The following code adds nothing to the actual functionality
14816 ** of the program. It is only here for testing and debugging.
14817 ** On the other hand, it does burn CPU cycles every time through
14818 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
14819 */
14820 #ifndef NDEBUG
14821 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
14822
14823 #ifdef SQLITE_DEBUG
14824 if( db->flags & SQLITE_VdbeTrace ){
14825 if( rc!=0 ) printf("rc=%d\n",rc);
14826 if( pOrigOp->opflags & (OPFLG_OUT2) ){
14827 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
14828 }
14829 if( pOrigOp->opflags & OPFLG_OUT3 ){
14830 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
14831 }
14832 }
14833 #endif /* SQLITE_DEBUG */
14834 #endif /* NDEBUG */
14835 } /* The end of the for(;;) loop the loops through opcodes */
14836
14837 /* If we reach this point, it means that execution is finished with
14838 ** an error of some kind.
14839 */
14840 vdbe_error_halt:
14841 assert( rc );
14842 p->rc = rc;
14843 testcase( sqlite3GlobalConfig.xLog!=0 );
14844 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
14845 (int)(pOp - aOp), p->zSql, p->zErrMsg);
14846 sqlite3VdbeHalt(p);
14847 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
14848 rc = SQLITE_ERROR;
14849 if( resetSchemaOnFault>0 ){
14850 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
14851 }
14852
14853 /* This is the only way out of this procedure. We have to
14854 ** release the mutexes on btrees that were acquired at the
14855 ** top. */
14856 vdbe_return:
14857 db->lastRowid = lastRowid;
14858 testcase( nVmStep>0 );
14859 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
14860 sqlite3VdbeLeave(p);
14861 return rc;
14862
14863 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
14864 ** is encountered.
14865 */
14866 too_big:
14867 sqlite3VdbeError(p, "string or blob too big");
14868 rc = SQLITE_TOOBIG;
14869 goto vdbe_error_halt;
14870
14871 /* Jump to here if a malloc() fails.
14872 */
14873 no_mem:
14874 db->mallocFailed = 1;
14875 sqlite3VdbeError(p, "out of memory");
14876 rc = SQLITE_NOMEM;
14877 goto vdbe_error_halt;
14878
14879 /* Jump to here for any other kind of fatal error. The "rc" variable
14880 ** should hold the error number.
14881 */
14882 abort_due_to_error:
14883 assert( p->zErrMsg==0 );
14884 if( db->mallocFailed ) rc = SQLITE_NOMEM;
14885 if( rc!=SQLITE_IOERR_NOMEM ){
14886 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
14887 }
14888 goto vdbe_error_halt;
14889
14890 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
14891 ** flag.
14892 */
14893 abort_due_to_interrupt:
14894 assert( db->u1.isInterrupted );
14895 rc = SQLITE_INTERRUPT;
14896 p->rc = rc;
14897 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
14898 goto vdbe_error_halt;
14899 }
14900
14901
14902 /************** End of vdbe.c ************************************************/
14903 /************** Begin file vdbeblob.c ****************************************/
14904 /*
14905 ** 2007 May 1
14906 **
14907 ** The author disclaims copyright to this source code. In place of
14908 ** a legal notice, here is a blessing:
14909 **
14910 ** May you do good and not evil.
14911 ** May you find forgiveness for yourself and forgive others.
14912 ** May you share freely, never taking more than you give.
14913 **
14914 *************************************************************************
14915 **
14916 ** This file contains code used to implement incremental BLOB I/O.
14917 */
14918
14919 /* #include "sqliteInt.h" */
14920 /* #include "vdbeInt.h" */
14921
14922 #ifndef SQLITE_OMIT_INCRBLOB
14923
14924 /*
14925 ** Valid sqlite3_blob* handles point to Incrblob structures.
14926 */
14927 typedef struct Incrblob Incrblob;
14928 struct Incrblob {
14929 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
14930 int nByte; /* Size of open blob, in bytes */
14931 int iOffset; /* Byte offset of blob in cursor data */
14932 int iCol; /* Table column this handle is open on */
14933 BtCursor *pCsr; /* Cursor pointing at blob row */
14934 sqlite3_stmt *pStmt; /* Statement holding cursor open */
14935 sqlite3 *db; /* The associated database */
14936 };
14937
14938
14939 /*
14940 ** This function is used by both blob_open() and blob_reopen(). It seeks
14941 ** the b-tree cursor associated with blob handle p to point to row iRow.
14942 ** If successful, SQLITE_OK is returned and subsequent calls to
14943 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
14944 **
14945 ** If an error occurs, or if the specified row does not exist or does not
14946 ** contain a value of type TEXT or BLOB in the column nominated when the
14947 ** blob handle was opened, then an error code is returned and *pzErr may
14948 ** be set to point to a buffer containing an error message. It is the
14949 ** responsibility of the caller to free the error message buffer using
14950 ** sqlite3DbFree().
14951 **
14952 ** If an error does occur, then the b-tree cursor is closed. All subsequent
14953 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
14954 ** immediately return SQLITE_ABORT.
14955 */
14956 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
14957 int rc; /* Error code */
14958 char *zErr = 0; /* Error message */
14959 Vdbe *v = (Vdbe *)p->pStmt;
14960
14961 /* Set the value of the SQL statements only variable to integer iRow.
14962 ** This is done directly instead of using sqlite3_bind_int64() to avoid
14963 ** triggering asserts related to mutexes.
14964 */
14965 assert( v->aVar[0].flags&MEM_Int );
14966 v->aVar[0].u.i = iRow;
14967
14968 rc = sqlite3_step(p->pStmt);
14969 if( rc==SQLITE_ROW ){
14970 VdbeCursor *pC = v->apCsr[0];
14971 u32 type = pC->aType[p->iCol];
14972 if( type<12 ){
14973 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
14974 type==0?"null": type==7?"real": "integer"
14975 );
14976 rc = SQLITE_ERROR;
14977 sqlite3_finalize(p->pStmt);
14978 p->pStmt = 0;
14979 }else{
14980 p->iOffset = pC->aType[p->iCol + pC->nField];
14981 p->nByte = sqlite3VdbeSerialTypeLen(type);
14982 p->pCsr = pC->uc.pCursor;
14983 sqlite3BtreeIncrblobCursor(p->pCsr);
14984 }
14985 }
14986
14987 if( rc==SQLITE_ROW ){
14988 rc = SQLITE_OK;
14989 }else if( p->pStmt ){
14990 rc = sqlite3_finalize(p->pStmt);
14991 p->pStmt = 0;
14992 if( rc==SQLITE_OK ){
14993 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
14994 rc = SQLITE_ERROR;
14995 }else{
14996 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
14997 }
14998 }
14999
15000 assert( rc!=SQLITE_OK || zErr==0 );
15001 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
15002
15003 *pzErr = zErr;
15004 return rc;
15005 }
15006
15007 /*
15008 ** Open a blob handle.
15009 */
15010 SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
15011 sqlite3* db, /* The database connection */
15012 const char *zDb, /* The attached database containing the blob */
15013 const char *zTable, /* The table containing the blob */
15014 const char *zColumn, /* The column containing the blob */
15015 sqlite_int64 iRow, /* The row containing the glob */
15016 int flags, /* True -> read/write access, false -> read-only */
15017 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
15018 ){
15019 int nAttempt = 0;
15020 int iCol; /* Index of zColumn in row-record */
15021
15022 /* This VDBE program seeks a btree cursor to the identified
15023 ** db/table/row entry. The reason for using a vdbe program instead
15024 ** of writing code to use the b-tree layer directly is that the
15025 ** vdbe program will take advantage of the various transaction,
15026 ** locking and error handling infrastructure built into the vdbe.
15027 **
15028 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
15029 ** Code external to the Vdbe then "borrows" the b-tree cursor and
15030 ** uses it to implement the blob_read(), blob_write() and
15031 ** blob_bytes() functions.
15032 **
15033 ** The sqlite3_blob_close() function finalizes the vdbe program,
15034 ** which closes the b-tree cursor and (possibly) commits the
15035 ** transaction.
15036 */
15037 static const int iLn = VDBE_OFFSET_LINENO(4);
15038 static const VdbeOpList openBlob[] = {
15039 /* {OP_Transaction, 0, 0, 0}, // 0: Inserted separately */
15040 {OP_TableLock, 0, 0, 0}, /* 1: Acquire a read or write lock */
15041 /* One of the following two instructions is replaced by an OP_Noop. */
15042 {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */
15043 {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */
15044 {OP_Variable, 1, 1, 1}, /* 4: Push the rowid to the stack */
15045 {OP_NotExists, 0, 10, 1}, /* 5: Seek the cursor */
15046 {OP_Column, 0, 0, 1}, /* 6 */
15047 {OP_ResultRow, 1, 0, 0}, /* 7 */
15048 {OP_Goto, 0, 4, 0}, /* 8 */
15049 {OP_Close, 0, 0, 0}, /* 9 */
15050 {OP_Halt, 0, 0, 0}, /* 10 */
15051 };
15052
15053 int rc = SQLITE_OK;
15054 char *zErr = 0;
15055 Table *pTab;
15056 Parse *pParse = 0;
15057 Incrblob *pBlob = 0;
15058
15059 #ifdef SQLITE_ENABLE_API_ARMOR
15060 if( ppBlob==0 ){
15061 return SQLITE_MISUSE_BKPT;
15062 }
15063 #endif
15064 *ppBlob = 0;
15065 #ifdef SQLITE_ENABLE_API_ARMOR
15066 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
15067 return SQLITE_MISUSE_BKPT;
15068 }
15069 #endif
15070 flags = !!flags; /* flags = (flags ? 1 : 0); */
15071
15072 sqlite3_mutex_enter(db->mutex);
15073
15074 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
15075 if( !pBlob ) goto blob_open_out;
15076 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
15077 if( !pParse ) goto blob_open_out;
15078
15079 do {
15080 memset(pParse, 0, sizeof(Parse));
15081 pParse->db = db;
15082 sqlite3DbFree(db, zErr);
15083 zErr = 0;
15084
15085 sqlite3BtreeEnterAll(db);
15086 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
15087 if( pTab && IsVirtual(pTab) ){
15088 pTab = 0;
15089 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
15090 }
15091 if( pTab && !HasRowid(pTab) ){
15092 pTab = 0;
15093 sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
15094 }
15095 #ifndef SQLITE_OMIT_VIEW
15096 if( pTab && pTab->pSelect ){
15097 pTab = 0;
15098 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
15099 }
15100 #endif
15101 if( !pTab ){
15102 if( pParse->zErrMsg ){
15103 sqlite3DbFree(db, zErr);
15104 zErr = pParse->zErrMsg;
15105 pParse->zErrMsg = 0;
15106 }
15107 rc = SQLITE_ERROR;
15108 sqlite3BtreeLeaveAll(db);
15109 goto blob_open_out;
15110 }
15111
15112 /* Now search pTab for the exact column. */
15113 for(iCol=0; iCol<pTab->nCol; iCol++) {
15114 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
15115 break;
15116 }
15117 }
15118 if( iCol==pTab->nCol ){
15119 sqlite3DbFree(db, zErr);
15120 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
15121 rc = SQLITE_ERROR;
15122 sqlite3BtreeLeaveAll(db);
15123 goto blob_open_out;
15124 }
15125
15126 /* If the value is being opened for writing, check that the
15127 ** column is not indexed, and that it is not part of a foreign key.
15128 ** It is against the rules to open a column to which either of these
15129 ** descriptions applies for writing. */
15130 if( flags ){
15131 const char *zFault = 0;
15132 Index *pIdx;
15133 #ifndef SQLITE_OMIT_FOREIGN_KEY
15134 if( db->flags&SQLITE_ForeignKeys ){
15135 /* Check that the column is not part of an FK child key definition. It
15136 ** is not necessary to check if it is part of a parent key, as parent
15137 ** key columns must be indexed. The check below will pick up this
15138 ** case. */
15139 FKey *pFKey;
15140 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
15141 int j;
15142 for(j=0; j<pFKey->nCol; j++){
15143 if( pFKey->aCol[j].iFrom==iCol ){
15144 zFault = "foreign key";
15145 }
15146 }
15147 }
15148 }
15149 #endif
15150 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
15151 int j;
15152 for(j=0; j<pIdx->nKeyCol; j++){
15153 /* FIXME: Be smarter about indexes that use expressions */
15154 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
15155 zFault = "indexed";
15156 }
15157 }
15158 }
15159 if( zFault ){
15160 sqlite3DbFree(db, zErr);
15161 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
15162 rc = SQLITE_ERROR;
15163 sqlite3BtreeLeaveAll(db);
15164 goto blob_open_out;
15165 }
15166 }
15167
15168 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
15169 assert( pBlob->pStmt || db->mallocFailed );
15170 if( pBlob->pStmt ){
15171 Vdbe *v = (Vdbe *)pBlob->pStmt;
15172 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
15173
15174
15175 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
15176 pTab->pSchema->schema_cookie,
15177 pTab->pSchema->iGeneration);
15178 sqlite3VdbeChangeP5(v, 1);
15179 sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
15180
15181 /* Make sure a mutex is held on the table to be accessed */
15182 sqlite3VdbeUsesBtree(v, iDb);
15183
15184 /* Configure the OP_TableLock instruction */
15185 #ifdef SQLITE_OMIT_SHARED_CACHE
15186 sqlite3VdbeChangeToNoop(v, 1);
15187 #else
15188 sqlite3VdbeChangeP1(v, 1, iDb);
15189 sqlite3VdbeChangeP2(v, 1, pTab->tnum);
15190 sqlite3VdbeChangeP3(v, 1, flags);
15191 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
15192 #endif
15193
15194 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
15195 ** parameter of the other to pTab->tnum. */
15196 sqlite3VdbeChangeToNoop(v, 3 - flags);
15197 sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
15198 sqlite3VdbeChangeP3(v, 2 + flags, iDb);
15199
15200 /* Configure the number of columns. Configure the cursor to
15201 ** think that the table has one more column than it really
15202 ** does. An OP_Column to retrieve this imaginary column will
15203 ** always return an SQL NULL. This is useful because it means
15204 ** we can invoke OP_Column to fill in the vdbe cursors type
15205 ** and offset cache without causing any IO.
15206 */
15207 sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
15208 sqlite3VdbeChangeP2(v, 6, pTab->nCol);
15209 if( !db->mallocFailed ){
15210 pParse->nVar = 1;
15211 pParse->nMem = 1;
15212 pParse->nTab = 1;
15213 sqlite3VdbeMakeReady(v, pParse);
15214 }
15215 }
15216
15217 pBlob->flags = flags;
15218 pBlob->iCol = iCol;
15219 pBlob->db = db;
15220 sqlite3BtreeLeaveAll(db);
15221 if( db->mallocFailed ){
15222 goto blob_open_out;
15223 }
15224 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
15225 rc = blobSeekToRow(pBlob, iRow, &zErr);
15226 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
15227
15228 blob_open_out:
15229 if( rc==SQLITE_OK && db->mallocFailed==0 ){
15230 *ppBlob = (sqlite3_blob *)pBlob;
15231 }else{
15232 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
15233 sqlite3DbFree(db, pBlob);
15234 }
15235 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
15236 sqlite3DbFree(db, zErr);
15237 sqlite3ParserReset(pParse);
15238 sqlite3StackFree(db, pParse);
15239 rc = sqlite3ApiExit(db, rc);
15240 sqlite3_mutex_leave(db->mutex);
15241 return rc;
15242 }
15243
15244 /*
15245 ** Close a blob handle that was previously created using
15246 ** sqlite3_blob_open().
15247 */
15248 SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *pBlob){
15249 Incrblob *p = (Incrblob *)pBlob;
15250 int rc;
15251 sqlite3 *db;
15252
15253 if( p ){
15254 db = p->db;
15255 sqlite3_mutex_enter(db->mutex);
15256 rc = sqlite3_finalize(p->pStmt);
15257 sqlite3DbFree(db, p);
15258 sqlite3_mutex_leave(db->mutex);
15259 }else{
15260 rc = SQLITE_OK;
15261 }
15262 return rc;
15263 }
15264
15265 /*
15266 ** Perform a read or write operation on a blob
15267 */
15268 static int blobReadWrite(
15269 sqlite3_blob *pBlob,
15270 void *z,
15271 int n,
15272 int iOffset,
15273 int (*xCall)(BtCursor*, u32, u32, void*)
15274 ){
15275 int rc;
15276 Incrblob *p = (Incrblob *)pBlob;
15277 Vdbe *v;
15278 sqlite3 *db;
15279
15280 if( p==0 ) return SQLITE_MISUSE_BKPT;
15281 db = p->db;
15282 sqlite3_mutex_enter(db->mutex);
15283 v = (Vdbe*)p->pStmt;
15284
15285 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
15286 /* Request is out of range. Return a transient error. */
15287 rc = SQLITE_ERROR;
15288 }else if( v==0 ){
15289 /* If there is no statement handle, then the blob-handle has
15290 ** already been invalidated. Return SQLITE_ABORT in this case.
15291 */
15292 rc = SQLITE_ABORT;
15293 }else{
15294 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
15295 ** returned, clean-up the statement handle.
15296 */
15297 assert( db == v->db );
15298 sqlite3BtreeEnterCursor(p->pCsr);
15299 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
15300 sqlite3BtreeLeaveCursor(p->pCsr);
15301 if( rc==SQLITE_ABORT ){
15302 sqlite3VdbeFinalize(v);
15303 p->pStmt = 0;
15304 }else{
15305 v->rc = rc;
15306 }
15307 }
15308 sqlite3Error(db, rc);
15309 rc = sqlite3ApiExit(db, rc);
15310 sqlite3_mutex_leave(db->mutex);
15311 return rc;
15312 }
15313
15314 /*
15315 ** Read data from a blob handle.
15316 */
15317 SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *pBlob, void *z, in t n, int iOffset){
15318 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
15319 }
15320
15321 /*
15322 ** Write data to a blob handle.
15323 */
15324 SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
15325 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
15326 }
15327
15328 /*
15329 ** Query a blob handle for the size of the data.
15330 **
15331 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
15332 ** so no mutex is required for access.
15333 */
15334 SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *pBlob){
15335 Incrblob *p = (Incrblob *)pBlob;
15336 return (p && p->pStmt) ? p->nByte : 0;
15337 }
15338
15339 /*
15340 ** Move an existing blob handle to point to a different row of the same
15341 ** database table.
15342 **
15343 ** If an error occurs, or if the specified row does not exist or does not
15344 ** contain a blob or text value, then an error code is returned and the
15345 ** database handle error code and message set. If this happens, then all
15346 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
15347 ** immediately return SQLITE_ABORT.
15348 */
15349 SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_i nt64 iRow){
15350 int rc;
15351 Incrblob *p = (Incrblob *)pBlob;
15352 sqlite3 *db;
15353
15354 if( p==0 ) return SQLITE_MISUSE_BKPT;
15355 db = p->db;
15356 sqlite3_mutex_enter(db->mutex);
15357
15358 if( p->pStmt==0 ){
15359 /* If there is no statement handle, then the blob-handle has
15360 ** already been invalidated. Return SQLITE_ABORT in this case.
15361 */
15362 rc = SQLITE_ABORT;
15363 }else{
15364 char *zErr;
15365 rc = blobSeekToRow(p, iRow, &zErr);
15366 if( rc!=SQLITE_OK ){
15367 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
15368 sqlite3DbFree(db, zErr);
15369 }
15370 assert( rc!=SQLITE_SCHEMA );
15371 }
15372
15373 rc = sqlite3ApiExit(db, rc);
15374 assert( rc==SQLITE_OK || p->pStmt==0 );
15375 sqlite3_mutex_leave(db->mutex);
15376 return rc;
15377 }
15378
15379 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
15380
15381 /************** End of vdbeblob.c ********************************************/
15382 /************** Begin file vdbesort.c ****************************************/
15383 /*
15384 ** 2011-07-09
15385 **
15386 ** The author disclaims copyright to this source code. In place of
15387 ** a legal notice, here is a blessing:
15388 **
15389 ** May you do good and not evil.
15390 ** May you find forgiveness for yourself and forgive others.
15391 ** May you share freely, never taking more than you give.
15392 **
15393 *************************************************************************
15394 ** This file contains code for the VdbeSorter object, used in concert with
15395 ** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
15396 ** or by SELECT statements with ORDER BY clauses that cannot be satisfied
15397 ** using indexes and without LIMIT clauses.
15398 **
15399 ** The VdbeSorter object implements a multi-threaded external merge sort
15400 ** algorithm that is efficient even if the number of elements being sorted
15401 ** exceeds the available memory.
15402 **
15403 ** Here is the (internal, non-API) interface between this module and the
15404 ** rest of the SQLite system:
15405 **
15406 ** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
15407 **
15408 ** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
15409 ** object. The row is a binary blob in the
15410 ** OP_MakeRecord format that contains both
15411 ** the ORDER BY key columns and result columns
15412 ** in the case of a SELECT w/ ORDER BY, or
15413 ** the complete record for an index entry
15414 ** in the case of a CREATE INDEX.
15415 **
15416 ** sqlite3VdbeSorterRewind() Sort all content previously added.
15417 ** Position the read cursor on the
15418 ** first sorted element.
15419 **
15420 ** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
15421 ** element.
15422 **
15423 ** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
15424 ** row currently under the read cursor.
15425 **
15426 ** sqlite3VdbeSorterCompare() Compare the binary blob for the row
15427 ** currently under the read cursor against
15428 ** another binary blob X and report if
15429 ** X is strictly less than the read cursor.
15430 ** Used to enforce uniqueness in a
15431 ** CREATE UNIQUE INDEX statement.
15432 **
15433 ** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
15434 ** all resources.
15435 **
15436 ** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
15437 ** is like Close() followed by Init() only
15438 ** much faster.
15439 **
15440 ** The interfaces above must be called in a particular order. Write() can
15441 ** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
15442 ** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
15443 **
15444 ** Init()
15445 ** for each record: Write()
15446 ** Rewind()
15447 ** Rowkey()/Compare()
15448 ** Next()
15449 ** Close()
15450 **
15451 ** Algorithm:
15452 **
15453 ** Records passed to the sorter via calls to Write() are initially held
15454 ** unsorted in main memory. Assuming the amount of memory used never exceeds
15455 ** a threshold, when Rewind() is called the set of records is sorted using
15456 ** an in-memory merge sort. In this case, no temporary files are required
15457 ** and subsequent calls to Rowkey(), Next() and Compare() read records
15458 ** directly from main memory.
15459 **
15460 ** If the amount of space used to store records in main memory exceeds the
15461 ** threshold, then the set of records currently in memory are sorted and
15462 ** written to a temporary file in "Packed Memory Array" (PMA) format.
15463 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
15464 ** of PMAs may be created by merging existing PMAs together - for example
15465 ** merging two or more level-0 PMAs together creates a level-1 PMA.
15466 **
15467 ** The threshold for the amount of main memory to use before flushing
15468 ** records to a PMA is roughly the same as the limit configured for the
15469 ** page-cache of the main database. Specifically, the threshold is set to
15470 ** the value returned by "PRAGMA main.page_size" multipled by
15471 ** that returned by "PRAGMA main.cache_size", in bytes.
15472 **
15473 ** If the sorter is running in single-threaded mode, then all PMAs generated
15474 ** are appended to a single temporary file. Or, if the sorter is running in
15475 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
15476 ** N is the configured number of worker threads. In this case, instead of
15477 ** sorting the records and writing the PMA to a temporary file itself, the
15478 ** calling thread usually launches a worker thread to do so. Except, if
15479 ** there are already N worker threads running, the main thread does the work
15480 ** itself.
15481 **
15482 ** The sorter is running in multi-threaded mode if (a) the library was built
15483 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
15484 ** than zero, and (b) worker threads have been enabled at runtime by calling
15485 ** "PRAGMA threads=N" with some value of N greater than 0.
15486 **
15487 ** When Rewind() is called, any data remaining in memory is flushed to a
15488 ** final PMA. So at this point the data is stored in some number of sorted
15489 ** PMAs within temporary files on disk.
15490 **
15491 ** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
15492 ** sorter is running in single-threaded mode, then these PMAs are merged
15493 ** incrementally as keys are retreived from the sorter by the VDBE. The
15494 ** MergeEngine object, described in further detail below, performs this
15495 ** merge.
15496 **
15497 ** Or, if running in multi-threaded mode, then a background thread is
15498 ** launched to merge the existing PMAs. Once the background thread has
15499 ** merged T bytes of data into a single sorted PMA, the main thread
15500 ** begins reading keys from that PMA while the background thread proceeds
15501 ** with merging the next T bytes of data. And so on.
15502 **
15503 ** Parameter T is set to half the value of the memory threshold used
15504 ** by Write() above to determine when to create a new PMA.
15505 **
15506 ** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
15507 ** Rewind() is called, then a hierarchy of incremental-merges is used.
15508 ** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
15509 ** disk are merged together. Then T bytes of data from the second set, and
15510 ** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
15511 ** PMAs at a time. This done is to improve locality.
15512 **
15513 ** If running in multi-threaded mode and there are more than
15514 ** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
15515 ** than one background thread may be created. Specifically, there may be
15516 ** one background thread for each temporary file on disk, and one background
15517 ** thread to merge the output of each of the others to a single PMA for
15518 ** the main thread to read from.
15519 */
15520 /* #include "sqliteInt.h" */
15521 /* #include "vdbeInt.h" */
15522
15523 /*
15524 ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
15525 ** messages to stderr that may be helpful in understanding the performance
15526 ** characteristics of the sorter in multi-threaded mode.
15527 */
15528 #if 0
15529 # define SQLITE_DEBUG_SORTER_THREADS 1
15530 #endif
15531
15532 /*
15533 ** Hard-coded maximum amount of data to accumulate in memory before flushing
15534 ** to a level 0 PMA. The purpose of this limit is to prevent various integer
15535 ** overflows. 512MiB.
15536 */
15537 #define SQLITE_MAX_PMASZ (1<<29)
15538
15539 /*
15540 ** Private objects used by the sorter
15541 */
15542 typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
15543 typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
15544 typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
15545 typedef struct SorterRecord SorterRecord; /* A record being sorted */
15546 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
15547 typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
15548 typedef struct SorterList SorterList; /* In-memory list of records */
15549 typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
15550
15551 /*
15552 ** A container for a temp file handle and the current amount of data
15553 ** stored in the file.
15554 */
15555 struct SorterFile {
15556 sqlite3_file *pFd; /* File handle */
15557 i64 iEof; /* Bytes of data stored in pFd */
15558 };
15559
15560 /*
15561 ** An in-memory list of objects to be sorted.
15562 **
15563 ** If aMemory==0 then each object is allocated separately and the objects
15564 ** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
15565 ** are stored in the aMemory[] bulk memory, one right after the other, and
15566 ** are connected using SorterRecord.u.iNext.
15567 */
15568 struct SorterList {
15569 SorterRecord *pList; /* Linked list of records */
15570 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
15571 int szPMA; /* Size of pList as PMA in bytes */
15572 };
15573
15574 /*
15575 ** The MergeEngine object is used to combine two or more smaller PMAs into
15576 ** one big PMA using a merge operation. Separate PMAs all need to be
15577 ** combined into one big PMA in order to be able to step through the sorted
15578 ** records in order.
15579 **
15580 ** The aReadr[] array contains a PmaReader object for each of the PMAs being
15581 ** merged. An aReadr[] object either points to a valid key or else is at EOF.
15582 ** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
15583 ** For the purposes of the paragraphs below, we assume that the array is
15584 ** actually N elements in size, where N is the smallest power of 2 greater
15585 ** to or equal to the number of PMAs being merged. The extra aReadr[] elements
15586 ** are treated as if they are empty (always at EOF).
15587 **
15588 ** The aTree[] array is also N elements in size. The value of N is stored in
15589 ** the MergeEngine.nTree variable.
15590 **
15591 ** The final (N/2) elements of aTree[] contain the results of comparing
15592 ** pairs of PMA keys together. Element i contains the result of
15593 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
15594 ** aTree element is set to the index of it.
15595 **
15596 ** For the purposes of this comparison, EOF is considered greater than any
15597 ** other key value. If the keys are equal (only possible with two EOF
15598 ** values), it doesn't matter which index is stored.
15599 **
15600 ** The (N/4) elements of aTree[] that precede the final (N/2) described
15601 ** above contains the index of the smallest of each block of 4 PmaReaders
15602 ** And so on. So that aTree[1] contains the index of the PmaReader that
15603 ** currently points to the smallest key value. aTree[0] is unused.
15604 **
15605 ** Example:
15606 **
15607 ** aReadr[0] -> Banana
15608 ** aReadr[1] -> Feijoa
15609 ** aReadr[2] -> Elderberry
15610 ** aReadr[3] -> Currant
15611 ** aReadr[4] -> Grapefruit
15612 ** aReadr[5] -> Apple
15613 ** aReadr[6] -> Durian
15614 ** aReadr[7] -> EOF
15615 **
15616 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
15617 **
15618 ** The current element is "Apple" (the value of the key indicated by
15619 ** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
15620 ** be advanced to the next key in its segment. Say the next key is
15621 ** "Eggplant":
15622 **
15623 ** aReadr[5] -> Eggplant
15624 **
15625 ** The contents of aTree[] are updated first by comparing the new PmaReader
15626 ** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
15627 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
15628 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
15629 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
15630 ** so the value written into element 1 of the array is 0. As follows:
15631 **
15632 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
15633 **
15634 ** In other words, each time we advance to the next sorter element, log2(N)
15635 ** key comparison operations are required, where N is the number of segments
15636 ** being merged (rounded up to the next power of 2).
15637 */
15638 struct MergeEngine {
15639 int nTree; /* Used size of aTree/aReadr (power of 2) */
15640 SortSubtask *pTask; /* Used by this thread only */
15641 int *aTree; /* Current state of incremental merge */
15642 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
15643 };
15644
15645 /*
15646 ** This object represents a single thread of control in a sort operation.
15647 ** Exactly VdbeSorter.nTask instances of this object are allocated
15648 ** as part of each VdbeSorter object. Instances are never allocated any
15649 ** other way. VdbeSorter.nTask is set to the number of worker threads allowed
15650 ** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
15651 ** single-threaded operation, there is exactly one instance of this object
15652 ** and for multi-threaded operation there are two or more instances.
15653 **
15654 ** Essentially, this structure contains all those fields of the VdbeSorter
15655 ** structure for which each thread requires a separate instance. For example,
15656 ** each thread requries its own UnpackedRecord object to unpack records in
15657 ** as part of comparison operations.
15658 **
15659 ** Before a background thread is launched, variable bDone is set to 0. Then,
15660 ** right before it exits, the thread itself sets bDone to 1. This is used for
15661 ** two purposes:
15662 **
15663 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
15664 ** attempt to select a SortSubtask for which there is not already an
15665 ** active background thread (since doing so causes the main thread
15666 ** to block until it finishes).
15667 **
15668 ** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
15669 ** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
15670 ** block provoke debugging output.
15671 **
15672 ** In both cases, the effects of the main thread seeing (bDone==0) even
15673 ** after the thread has finished are not dire. So we don't worry about
15674 ** memory barriers and such here.
15675 */
15676 typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
15677 struct SortSubtask {
15678 SQLiteThread *pThread; /* Background thread, if any */
15679 int bDone; /* Set if thread is finished but not joined */
15680 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
15681 UnpackedRecord *pUnpacked; /* Space to unpack a record */
15682 SorterList list; /* List for thread to write to a PMA */
15683 int nPMA; /* Number of PMAs currently in file */
15684 SorterCompare xCompare; /* Compare function to use */
15685 SorterFile file; /* Temp file for level-0 PMAs */
15686 SorterFile file2; /* Space for other PMAs */
15687 };
15688
15689
15690 /*
15691 ** Main sorter structure. A single instance of this is allocated for each
15692 ** sorter cursor created by the VDBE.
15693 **
15694 ** mxKeysize:
15695 ** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
15696 ** this variable is updated so as to be set to the size on disk of the
15697 ** largest record in the sorter.
15698 */
15699 struct VdbeSorter {
15700 int mnPmaSize; /* Minimum PMA size, in bytes */
15701 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
15702 int mxKeysize; /* Largest serialized key seen so far */
15703 int pgsz; /* Main database page size */
15704 PmaReader *pReader; /* Readr data from here after Rewind() */
15705 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
15706 sqlite3 *db; /* Database connection */
15707 KeyInfo *pKeyInfo; /* How to compare records */
15708 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
15709 SorterList list; /* List of in-memory records */
15710 int iMemory; /* Offset of free space in list.aMemory */
15711 int nMemory; /* Size of list.aMemory allocation in bytes */
15712 u8 bUsePMA; /* True if one or more PMAs created */
15713 u8 bUseThreads; /* True to use background threads */
15714 u8 iPrev; /* Previous thread used to flush PMA */
15715 u8 nTask; /* Size of aTask[] array */
15716 u8 typeMask;
15717 SortSubtask aTask[1]; /* One or more subtasks */
15718 };
15719
15720 #define SORTER_TYPE_INTEGER 0x01
15721 #define SORTER_TYPE_TEXT 0x02
15722
15723 /*
15724 ** An instance of the following object is used to read records out of a
15725 ** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
15726 ** aKey might point into aMap or into aBuffer. If neither of those locations
15727 ** contain a contiguous representation of the key, then aAlloc is allocated
15728 ** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
15729 **
15730 ** pFd==0 at EOF.
15731 */
15732 struct PmaReader {
15733 i64 iReadOff; /* Current read offset */
15734 i64 iEof; /* 1 byte past EOF for this PmaReader */
15735 int nAlloc; /* Bytes of space at aAlloc */
15736 int nKey; /* Number of bytes in key */
15737 sqlite3_file *pFd; /* File handle we are reading from */
15738 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
15739 u8 *aKey; /* Pointer to current key */
15740 u8 *aBuffer; /* Current read buffer */
15741 int nBuffer; /* Size of read buffer in bytes */
15742 u8 *aMap; /* Pointer to mapping of entire file */
15743 IncrMerger *pIncr; /* Incremental merger */
15744 };
15745
15746 /*
15747 ** Normally, a PmaReader object iterates through an existing PMA stored
15748 ** within a temp file. However, if the PmaReader.pIncr variable points to
15749 ** an object of the following type, it may be used to iterate/merge through
15750 ** multiple PMAs simultaneously.
15751 **
15752 ** There are two types of IncrMerger object - single (bUseThread==0) and
15753 ** multi-threaded (bUseThread==1).
15754 **
15755 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
15756 ** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
15757 ** size. When the IncrMerger is initialized, it reads enough data from
15758 ** pMerger to populate aFile[0]. It then sets variables within the
15759 ** corresponding PmaReader object to read from that file and kicks off
15760 ** a background thread to populate aFile[1] with the next mxSz bytes of
15761 ** sorted record data from pMerger.
15762 **
15763 ** When the PmaReader reaches the end of aFile[0], it blocks until the
15764 ** background thread has finished populating aFile[1]. It then exchanges
15765 ** the contents of the aFile[0] and aFile[1] variables within this structure,
15766 ** sets the PmaReader fields to read from the new aFile[0] and kicks off
15767 ** another background thread to populate the new aFile[1]. And so on, until
15768 ** the contents of pMerger are exhausted.
15769 **
15770 ** A single-threaded IncrMerger does not open any temporary files of its
15771 ** own. Instead, it has exclusive access to mxSz bytes of space beginning
15772 ** at offset iStartOff of file pTask->file2. And instead of using a
15773 ** background thread to prepare data for the PmaReader, with a single
15774 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
15775 ** keys from pMerger by the calling thread whenever the PmaReader runs out
15776 ** of data.
15777 */
15778 struct IncrMerger {
15779 SortSubtask *pTask; /* Task that owns this merger */
15780 MergeEngine *pMerger; /* Merge engine thread reads data from */
15781 i64 iStartOff; /* Offset to start writing file at */
15782 int mxSz; /* Maximum bytes of data to store */
15783 int bEof; /* Set to true when merge is finished */
15784 int bUseThread; /* True to use a bg thread for this object */
15785 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
15786 };
15787
15788 /*
15789 ** An instance of this object is used for writing a PMA.
15790 **
15791 ** The PMA is written one record at a time. Each record is of an arbitrary
15792 ** size. But I/O is more efficient if it occurs in page-sized blocks where
15793 ** each block is aligned on a page boundary. This object caches writes to
15794 ** the PMA so that aligned, page-size blocks are written.
15795 */
15796 struct PmaWriter {
15797 int eFWErr; /* Non-zero if in an error state */
15798 u8 *aBuffer; /* Pointer to write buffer */
15799 int nBuffer; /* Size of write buffer in bytes */
15800 int iBufStart; /* First byte of buffer to write */
15801 int iBufEnd; /* Last byte of buffer to write */
15802 i64 iWriteOff; /* Offset of start of buffer in file */
15803 sqlite3_file *pFd; /* File handle to write to */
15804 };
15805
15806 /*
15807 ** This object is the header on a single record while that record is being
15808 ** held in memory and prior to being written out as part of a PMA.
15809 **
15810 ** How the linked list is connected depends on how memory is being managed
15811 ** by this module. If using a separate allocation for each in-memory record
15812 ** (VdbeSorter.list.aMemory==0), then the list is always connected using the
15813 ** SorterRecord.u.pNext pointers.
15814 **
15815 ** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
15816 ** then while records are being accumulated the list is linked using the
15817 ** SorterRecord.u.iNext offset. This is because the aMemory[] array may
15818 ** be sqlite3Realloc()ed while records are being accumulated. Once the VM
15819 ** has finished passing records to the sorter, or when the in-memory buffer
15820 ** is full, the list is sorted. As part of the sorting process, it is
15821 ** converted to use the SorterRecord.u.pNext pointers. See function
15822 ** vdbeSorterSort() for details.
15823 */
15824 struct SorterRecord {
15825 int nVal; /* Size of the record in bytes */
15826 union {
15827 SorterRecord *pNext; /* Pointer to next record in list */
15828 int iNext; /* Offset within aMemory of next record */
15829 } u;
15830 /* The data for the record immediately follows this header */
15831 };
15832
15833 /* Return a pointer to the buffer containing the record data for SorterRecord
15834 ** object p. Should be used as if:
15835 **
15836 ** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
15837 */
15838 #define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
15839
15840
15841 /* Maximum number of PMAs that a single MergeEngine can merge */
15842 #define SORTER_MAX_MERGE_COUNT 16
15843
15844 static int vdbeIncrSwap(IncrMerger*);
15845 static void vdbeIncrFree(IncrMerger *);
15846
15847 /*
15848 ** Free all memory belonging to the PmaReader object passed as the
15849 ** argument. All structure fields are set to zero before returning.
15850 */
15851 static void vdbePmaReaderClear(PmaReader *pReadr){
15852 sqlite3_free(pReadr->aAlloc);
15853 sqlite3_free(pReadr->aBuffer);
15854 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
15855 vdbeIncrFree(pReadr->pIncr);
15856 memset(pReadr, 0, sizeof(PmaReader));
15857 }
15858
15859 /*
15860 ** Read the next nByte bytes of data from the PMA p.
15861 ** If successful, set *ppOut to point to a buffer containing the data
15862 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
15863 ** error code.
15864 **
15865 ** The buffer returned in *ppOut is only valid until the
15866 ** next call to this function.
15867 */
15868 static int vdbePmaReadBlob(
15869 PmaReader *p, /* PmaReader from which to take the blob */
15870 int nByte, /* Bytes of data to read */
15871 u8 **ppOut /* OUT: Pointer to buffer containing data */
15872 ){
15873 int iBuf; /* Offset within buffer to read from */
15874 int nAvail; /* Bytes of data available in buffer */
15875
15876 if( p->aMap ){
15877 *ppOut = &p->aMap[p->iReadOff];
15878 p->iReadOff += nByte;
15879 return SQLITE_OK;
15880 }
15881
15882 assert( p->aBuffer );
15883
15884 /* If there is no more data to be read from the buffer, read the next
15885 ** p->nBuffer bytes of data from the file into it. Or, if there are less
15886 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
15887 iBuf = p->iReadOff % p->nBuffer;
15888 if( iBuf==0 ){
15889 int nRead; /* Bytes to read from disk */
15890 int rc; /* sqlite3OsRead() return code */
15891
15892 /* Determine how many bytes of data to read. */
15893 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
15894 nRead = p->nBuffer;
15895 }else{
15896 nRead = (int)(p->iEof - p->iReadOff);
15897 }
15898 assert( nRead>0 );
15899
15900 /* Readr data from the file. Return early if an error occurs. */
15901 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
15902 assert( rc!=SQLITE_IOERR_SHORT_READ );
15903 if( rc!=SQLITE_OK ) return rc;
15904 }
15905 nAvail = p->nBuffer - iBuf;
15906
15907 if( nByte<=nAvail ){
15908 /* The requested data is available in the in-memory buffer. In this
15909 ** case there is no need to make a copy of the data, just return a
15910 ** pointer into the buffer to the caller. */
15911 *ppOut = &p->aBuffer[iBuf];
15912 p->iReadOff += nByte;
15913 }else{
15914 /* The requested data is not all available in the in-memory buffer.
15915 ** In this case, allocate space at p->aAlloc[] to copy the requested
15916 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
15917 int nRem; /* Bytes remaining to copy */
15918
15919 /* Extend the p->aAlloc[] allocation if required. */
15920 if( p->nAlloc<nByte ){
15921 u8 *aNew;
15922 int nNew = MAX(128, p->nAlloc*2);
15923 while( nByte>nNew ) nNew = nNew*2;
15924 aNew = sqlite3Realloc(p->aAlloc, nNew);
15925 if( !aNew ) return SQLITE_NOMEM;
15926 p->nAlloc = nNew;
15927 p->aAlloc = aNew;
15928 }
15929
15930 /* Copy as much data as is available in the buffer into the start of
15931 ** p->aAlloc[]. */
15932 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
15933 p->iReadOff += nAvail;
15934 nRem = nByte - nAvail;
15935
15936 /* The following loop copies up to p->nBuffer bytes per iteration into
15937 ** the p->aAlloc[] buffer. */
15938 while( nRem>0 ){
15939 int rc; /* vdbePmaReadBlob() return code */
15940 int nCopy; /* Number of bytes to copy */
15941 u8 *aNext; /* Pointer to buffer to copy data from */
15942
15943 nCopy = nRem;
15944 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
15945 rc = vdbePmaReadBlob(p, nCopy, &aNext);
15946 if( rc!=SQLITE_OK ) return rc;
15947 assert( aNext!=p->aAlloc );
15948 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
15949 nRem -= nCopy;
15950 }
15951
15952 *ppOut = p->aAlloc;
15953 }
15954
15955 return SQLITE_OK;
15956 }
15957
15958 /*
15959 ** Read a varint from the stream of data accessed by p. Set *pnOut to
15960 ** the value read.
15961 */
15962 static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
15963 int iBuf;
15964
15965 if( p->aMap ){
15966 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
15967 }else{
15968 iBuf = p->iReadOff % p->nBuffer;
15969 if( iBuf && (p->nBuffer-iBuf)>=9 ){
15970 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
15971 }else{
15972 u8 aVarint[16], *a;
15973 int i = 0, rc;
15974 do{
15975 rc = vdbePmaReadBlob(p, 1, &a);
15976 if( rc ) return rc;
15977 aVarint[(i++)&0xf] = a[0];
15978 }while( (a[0]&0x80)!=0 );
15979 sqlite3GetVarint(aVarint, pnOut);
15980 }
15981 }
15982
15983 return SQLITE_OK;
15984 }
15985
15986 /*
15987 ** Attempt to memory map file pFile. If successful, set *pp to point to the
15988 ** new mapping and return SQLITE_OK. If the mapping is not attempted
15989 ** (because the file is too large or the VFS layer is configured not to use
15990 ** mmap), return SQLITE_OK and set *pp to NULL.
15991 **
15992 ** Or, if an error occurs, return an SQLite error code. The final value of
15993 ** *pp is undefined in this case.
15994 */
15995 static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
15996 int rc = SQLITE_OK;
15997 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
15998 sqlite3_file *pFd = pFile->pFd;
15999 if( pFd->pMethods->iVersion>=3 ){
16000 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
16001 testcase( rc!=SQLITE_OK );
16002 }
16003 }
16004 return rc;
16005 }
16006
16007 /*
16008 ** Attach PmaReader pReadr to file pFile (if it is not already attached to
16009 ** that file) and seek it to offset iOff within the file. Return SQLITE_OK
16010 ** if successful, or an SQLite error code if an error occurs.
16011 */
16012 static int vdbePmaReaderSeek(
16013 SortSubtask *pTask, /* Task context */
16014 PmaReader *pReadr, /* Reader whose cursor is to be moved */
16015 SorterFile *pFile, /* Sorter file to read from */
16016 i64 iOff /* Offset in pFile */
16017 ){
16018 int rc = SQLITE_OK;
16019
16020 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
16021
16022 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
16023 if( pReadr->aMap ){
16024 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
16025 pReadr->aMap = 0;
16026 }
16027 pReadr->iReadOff = iOff;
16028 pReadr->iEof = pFile->iEof;
16029 pReadr->pFd = pFile->pFd;
16030
16031 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
16032 if( rc==SQLITE_OK && pReadr->aMap==0 ){
16033 int pgsz = pTask->pSorter->pgsz;
16034 int iBuf = pReadr->iReadOff % pgsz;
16035 if( pReadr->aBuffer==0 ){
16036 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
16037 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM;
16038 pReadr->nBuffer = pgsz;
16039 }
16040 if( rc==SQLITE_OK && iBuf ){
16041 int nRead = pgsz - iBuf;
16042 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
16043 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
16044 }
16045 rc = sqlite3OsRead(
16046 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
16047 );
16048 testcase( rc!=SQLITE_OK );
16049 }
16050 }
16051
16052 return rc;
16053 }
16054
16055 /*
16056 ** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
16057 ** no error occurs, or an SQLite error code if one does.
16058 */
16059 static int vdbePmaReaderNext(PmaReader *pReadr){
16060 int rc = SQLITE_OK; /* Return Code */
16061 u64 nRec = 0; /* Size of record in bytes */
16062
16063
16064 if( pReadr->iReadOff>=pReadr->iEof ){
16065 IncrMerger *pIncr = pReadr->pIncr;
16066 int bEof = 1;
16067 if( pIncr ){
16068 rc = vdbeIncrSwap(pIncr);
16069 if( rc==SQLITE_OK && pIncr->bEof==0 ){
16070 rc = vdbePmaReaderSeek(
16071 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
16072 );
16073 bEof = 0;
16074 }
16075 }
16076
16077 if( bEof ){
16078 /* This is an EOF condition */
16079 vdbePmaReaderClear(pReadr);
16080 testcase( rc!=SQLITE_OK );
16081 return rc;
16082 }
16083 }
16084
16085 if( rc==SQLITE_OK ){
16086 rc = vdbePmaReadVarint(pReadr, &nRec);
16087 }
16088 if( rc==SQLITE_OK ){
16089 pReadr->nKey = (int)nRec;
16090 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
16091 testcase( rc!=SQLITE_OK );
16092 }
16093
16094 return rc;
16095 }
16096
16097 /*
16098 ** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
16099 ** starting at offset iStart and ending at offset iEof-1. This function
16100 ** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
16101 ** PMA is empty).
16102 **
16103 ** If the pnByte parameter is NULL, then it is assumed that the file
16104 ** contains a single PMA, and that that PMA omits the initial length varint.
16105 */
16106 static int vdbePmaReaderInit(
16107 SortSubtask *pTask, /* Task context */
16108 SorterFile *pFile, /* Sorter file to read from */
16109 i64 iStart, /* Start offset in pFile */
16110 PmaReader *pReadr, /* PmaReader to populate */
16111 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
16112 ){
16113 int rc;
16114
16115 assert( pFile->iEof>iStart );
16116 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
16117 assert( pReadr->aBuffer==0 );
16118 assert( pReadr->aMap==0 );
16119
16120 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
16121 if( rc==SQLITE_OK ){
16122 u64 nByte; /* Size of PMA in bytes */
16123 rc = vdbePmaReadVarint(pReadr, &nByte);
16124 pReadr->iEof = pReadr->iReadOff + nByte;
16125 *pnByte += nByte;
16126 }
16127
16128 if( rc==SQLITE_OK ){
16129 rc = vdbePmaReaderNext(pReadr);
16130 }
16131 return rc;
16132 }
16133
16134 /*
16135 ** A version of vdbeSorterCompare() that assumes that it has already been
16136 ** determined that the first field of key1 is equal to the first field of
16137 ** key2.
16138 */
16139 static int vdbeSorterCompareTail(
16140 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
16141 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
16142 const void *pKey1, int nKey1, /* Left side of comparison */
16143 const void *pKey2, int nKey2 /* Right side of comparison */
16144 ){
16145 UnpackedRecord *r2 = pTask->pUnpacked;
16146 if( *pbKey2Cached==0 ){
16147 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
16148 *pbKey2Cached = 1;
16149 }
16150 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
16151 }
16152
16153 /*
16154 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
16155 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
16156 ** used by the comparison. Return the result of the comparison.
16157 **
16158 ** If IN/OUT parameter *pbKey2Cached is true when this function is called,
16159 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
16160 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
16161 ** version of key2 and *pbKey2Cached set to true before returning.
16162 **
16163 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
16164 ** to SQLITE_NOMEM.
16165 */
16166 static int vdbeSorterCompare(
16167 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
16168 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
16169 const void *pKey1, int nKey1, /* Left side of comparison */
16170 const void *pKey2, int nKey2 /* Right side of comparison */
16171 ){
16172 UnpackedRecord *r2 = pTask->pUnpacked;
16173 if( !*pbKey2Cached ){
16174 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
16175 *pbKey2Cached = 1;
16176 }
16177 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
16178 }
16179
16180 /*
16181 ** A specially optimized version of vdbeSorterCompare() that assumes that
16182 ** the first field of each key is a TEXT value and that the collation
16183 ** sequence to compare them with is BINARY.
16184 */
16185 static int vdbeSorterCompareText(
16186 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
16187 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
16188 const void *pKey1, int nKey1, /* Left side of comparison */
16189 const void *pKey2, int nKey2 /* Right side of comparison */
16190 ){
16191 const u8 * const p1 = (const u8 * const)pKey1;
16192 const u8 * const p2 = (const u8 * const)pKey2;
16193 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
16194 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
16195
16196 int n1;
16197 int n2;
16198 int res;
16199
16200 getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
16201 getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
16202 res = memcmp(v1, v2, MIN(n1, n2));
16203 if( res==0 ){
16204 res = n1 - n2;
16205 }
16206
16207 if( res==0 ){
16208 if( pTask->pSorter->pKeyInfo->nField>1 ){
16209 res = vdbeSorterCompareTail(
16210 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
16211 );
16212 }
16213 }else{
16214 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
16215 res = res * -1;
16216 }
16217 }
16218
16219 return res;
16220 }
16221
16222 /*
16223 ** A specially optimized version of vdbeSorterCompare() that assumes that
16224 ** the first field of each key is an INTEGER value.
16225 */
16226 static int vdbeSorterCompareInt(
16227 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
16228 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
16229 const void *pKey1, int nKey1, /* Left side of comparison */
16230 const void *pKey2, int nKey2 /* Right side of comparison */
16231 ){
16232 const u8 * const p1 = (const u8 * const)pKey1;
16233 const u8 * const p2 = (const u8 * const)pKey2;
16234 const int s1 = p1[1]; /* Left hand serial type */
16235 const int s2 = p2[1]; /* Right hand serial type */
16236 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
16237 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
16238 int res; /* Return value */
16239
16240 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
16241 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
16242
16243 if( s1>7 && s2>7 ){
16244 res = s1 - s2;
16245 }else{
16246 if( s1==s2 ){
16247 if( (*v1 ^ *v2) & 0x80 ){
16248 /* The two values have different signs */
16249 res = (*v1 & 0x80) ? -1 : +1;
16250 }else{
16251 /* The two values have the same sign. Compare using memcmp(). */
16252 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
16253 int i;
16254 res = 0;
16255 for(i=0; i<aLen[s1]; i++){
16256 if( (res = v1[i] - v2[i]) ) break;
16257 }
16258 }
16259 }else{
16260 if( s2>7 ){
16261 res = +1;
16262 }else if( s1>7 ){
16263 res = -1;
16264 }else{
16265 res = s1 - s2;
16266 }
16267 assert( res!=0 );
16268
16269 if( res>0 ){
16270 if( *v1 & 0x80 ) res = -1;
16271 }else{
16272 if( *v2 & 0x80 ) res = +1;
16273 }
16274 }
16275 }
16276
16277 if( res==0 ){
16278 if( pTask->pSorter->pKeyInfo->nField>1 ){
16279 res = vdbeSorterCompareTail(
16280 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
16281 );
16282 }
16283 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
16284 res = res * -1;
16285 }
16286
16287 return res;
16288 }
16289
16290 /*
16291 ** Initialize the temporary index cursor just opened as a sorter cursor.
16292 **
16293 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
16294 ** to determine the number of fields that should be compared from the
16295 ** records being sorted. However, if the value passed as argument nField
16296 ** is non-zero and the sorter is able to guarantee a stable sort, nField
16297 ** is used instead. This is used when sorting records for a CREATE INDEX
16298 ** statement. In this case, keys are always delivered to the sorter in
16299 ** order of the primary key, which happens to be make up the final part
16300 ** of the records being sorted. So if the sort is stable, there is never
16301 ** any reason to compare PK fields and they can be ignored for a small
16302 ** performance boost.
16303 **
16304 ** The sorter can guarantee a stable sort when running in single-threaded
16305 ** mode, but not in multi-threaded mode.
16306 **
16307 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
16308 */
16309 SQLITE_PRIVATE int sqlite3VdbeSorterInit(
16310 sqlite3 *db, /* Database connection (for malloc()) */
16311 int nField, /* Number of key fields in each record */
16312 VdbeCursor *pCsr /* Cursor that holds the new sorter */
16313 ){
16314 int pgsz; /* Page size of main database */
16315 int i; /* Used to iterate through aTask[] */
16316 int mxCache; /* Cache size */
16317 VdbeSorter *pSorter; /* The new sorter */
16318 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
16319 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
16320 int sz; /* Size of pSorter in bytes */
16321 int rc = SQLITE_OK;
16322 #if SQLITE_MAX_WORKER_THREADS==0
16323 # define nWorker 0
16324 #else
16325 int nWorker;
16326 #endif
16327
16328 /* Initialize the upper limit on the number of worker threads */
16329 #if SQLITE_MAX_WORKER_THREADS>0
16330 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
16331 nWorker = 0;
16332 }else{
16333 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
16334 }
16335 #endif
16336
16337 /* Do not allow the total number of threads (main thread + all workers)
16338 ** to exceed the maximum merge count */
16339 #if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
16340 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
16341 nWorker = SORTER_MAX_MERGE_COUNT-1;
16342 }
16343 #endif
16344
16345 assert( pCsr->pKeyInfo && pCsr->pBt==0 );
16346 assert( pCsr->eCurType==CURTYPE_SORTER );
16347 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
16348 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
16349
16350 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
16351 pCsr->uc.pSorter = pSorter;
16352 if( pSorter==0 ){
16353 rc = SQLITE_NOMEM;
16354 }else{
16355 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
16356 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
16357 pKeyInfo->db = 0;
16358 if( nField && nWorker==0 ){
16359 pKeyInfo->nXField += (pKeyInfo->nField - nField);
16360 pKeyInfo->nField = nField;
16361 }
16362 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
16363 pSorter->nTask = nWorker + 1;
16364 pSorter->iPrev = (u8)(nWorker - 1);
16365 pSorter->bUseThreads = (pSorter->nTask>1);
16366 pSorter->db = db;
16367 for(i=0; i<pSorter->nTask; i++){
16368 SortSubtask *pTask = &pSorter->aTask[i];
16369 pTask->pSorter = pSorter;
16370 }
16371
16372 if( !sqlite3TempInMemory(db) ){
16373 u32 szPma = sqlite3GlobalConfig.szPma;
16374 pSorter->mnPmaSize = szPma * pgsz;
16375 mxCache = db->aDb[0].pSchema->cache_size;
16376 if( mxCache<(int)szPma ) mxCache = (int)szPma;
16377 pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_PMASZ);
16378
16379 /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
16380 ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
16381 ** large heap allocations.
16382 */
16383 if( sqlite3GlobalConfig.pScratch==0 ){
16384 assert( pSorter->iMemory==0 );
16385 pSorter->nMemory = pgsz;
16386 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
16387 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
16388 }
16389 }
16390
16391 if( (pKeyInfo->nField+pKeyInfo->nXField)<13
16392 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
16393 ){
16394 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
16395 }
16396 }
16397
16398 return rc;
16399 }
16400 #undef nWorker /* Defined at the top of this function */
16401
16402 /*
16403 ** Free the list of sorted records starting at pRecord.
16404 */
16405 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
16406 SorterRecord *p;
16407 SorterRecord *pNext;
16408 for(p=pRecord; p; p=pNext){
16409 pNext = p->u.pNext;
16410 sqlite3DbFree(db, p);
16411 }
16412 }
16413
16414 /*
16415 ** Free all resources owned by the object indicated by argument pTask. All
16416 ** fields of *pTask are zeroed before returning.
16417 */
16418 static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
16419 sqlite3DbFree(db, pTask->pUnpacked);
16420 #if SQLITE_MAX_WORKER_THREADS>0
16421 /* pTask->list.aMemory can only be non-zero if it was handed memory
16422 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
16423 if( pTask->list.aMemory ){
16424 sqlite3_free(pTask->list.aMemory);
16425 }else
16426 #endif
16427 {
16428 assert( pTask->list.aMemory==0 );
16429 vdbeSorterRecordFree(0, pTask->list.pList);
16430 }
16431 if( pTask->file.pFd ){
16432 sqlite3OsCloseFree(pTask->file.pFd);
16433 }
16434 if( pTask->file2.pFd ){
16435 sqlite3OsCloseFree(pTask->file2.pFd);
16436 }
16437 memset(pTask, 0, sizeof(SortSubtask));
16438 }
16439
16440 #ifdef SQLITE_DEBUG_SORTER_THREADS
16441 static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
16442 i64 t;
16443 int iTask = (pTask - pTask->pSorter->aTask);
16444 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
16445 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
16446 }
16447 static void vdbeSorterRewindDebug(const char *zEvent){
16448 i64 t;
16449 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
16450 fprintf(stderr, "%lld:X %s\n", t, zEvent);
16451 }
16452 static void vdbeSorterPopulateDebug(
16453 SortSubtask *pTask,
16454 const char *zEvent
16455 ){
16456 i64 t;
16457 int iTask = (pTask - pTask->pSorter->aTask);
16458 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
16459 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
16460 }
16461 static void vdbeSorterBlockDebug(
16462 SortSubtask *pTask,
16463 int bBlocked,
16464 const char *zEvent
16465 ){
16466 if( bBlocked ){
16467 i64 t;
16468 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
16469 fprintf(stderr, "%lld:main %s\n", t, zEvent);
16470 }
16471 }
16472 #else
16473 # define vdbeSorterWorkDebug(x,y)
16474 # define vdbeSorterRewindDebug(y)
16475 # define vdbeSorterPopulateDebug(x,y)
16476 # define vdbeSorterBlockDebug(x,y,z)
16477 #endif
16478
16479 #if SQLITE_MAX_WORKER_THREADS>0
16480 /*
16481 ** Join thread pTask->thread.
16482 */
16483 static int vdbeSorterJoinThread(SortSubtask *pTask){
16484 int rc = SQLITE_OK;
16485 if( pTask->pThread ){
16486 #ifdef SQLITE_DEBUG_SORTER_THREADS
16487 int bDone = pTask->bDone;
16488 #endif
16489 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
16490 vdbeSorterBlockDebug(pTask, !bDone, "enter");
16491 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
16492 vdbeSorterBlockDebug(pTask, !bDone, "exit");
16493 rc = SQLITE_PTR_TO_INT(pRet);
16494 assert( pTask->bDone==1 );
16495 pTask->bDone = 0;
16496 pTask->pThread = 0;
16497 }
16498 return rc;
16499 }
16500
16501 /*
16502 ** Launch a background thread to run xTask(pIn).
16503 */
16504 static int vdbeSorterCreateThread(
16505 SortSubtask *pTask, /* Thread will use this task object */
16506 void *(*xTask)(void*), /* Routine to run in a separate thread */
16507 void *pIn /* Argument passed into xTask() */
16508 ){
16509 assert( pTask->pThread==0 && pTask->bDone==0 );
16510 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
16511 }
16512
16513 /*
16514 ** Join all outstanding threads launched by SorterWrite() to create
16515 ** level-0 PMAs.
16516 */
16517 static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
16518 int rc = rcin;
16519 int i;
16520
16521 /* This function is always called by the main user thread.
16522 **
16523 ** If this function is being called after SorterRewind() has been called,
16524 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
16525 ** is currently attempt to join one of the other threads. To avoid a race
16526 ** condition where this thread also attempts to join the same object, join
16527 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
16528 for(i=pSorter->nTask-1; i>=0; i--){
16529 SortSubtask *pTask = &pSorter->aTask[i];
16530 int rc2 = vdbeSorterJoinThread(pTask);
16531 if( rc==SQLITE_OK ) rc = rc2;
16532 }
16533 return rc;
16534 }
16535 #else
16536 # define vdbeSorterJoinAll(x,rcin) (rcin)
16537 # define vdbeSorterJoinThread(pTask) SQLITE_OK
16538 #endif
16539
16540 /*
16541 ** Allocate a new MergeEngine object capable of handling up to
16542 ** nReader PmaReader inputs.
16543 **
16544 ** nReader is automatically rounded up to the next power of two.
16545 ** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
16546 */
16547 static MergeEngine *vdbeMergeEngineNew(int nReader){
16548 int N = 2; /* Smallest power of two >= nReader */
16549 int nByte; /* Total bytes of space to allocate */
16550 MergeEngine *pNew; /* Pointer to allocated object to return */
16551
16552 assert( nReader<=SORTER_MAX_MERGE_COUNT );
16553
16554 while( N<nReader ) N += N;
16555 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
16556
16557 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
16558 if( pNew ){
16559 pNew->nTree = N;
16560 pNew->pTask = 0;
16561 pNew->aReadr = (PmaReader*)&pNew[1];
16562 pNew->aTree = (int*)&pNew->aReadr[N];
16563 }
16564 return pNew;
16565 }
16566
16567 /*
16568 ** Free the MergeEngine object passed as the only argument.
16569 */
16570 static void vdbeMergeEngineFree(MergeEngine *pMerger){
16571 int i;
16572 if( pMerger ){
16573 for(i=0; i<pMerger->nTree; i++){
16574 vdbePmaReaderClear(&pMerger->aReadr[i]);
16575 }
16576 }
16577 sqlite3_free(pMerger);
16578 }
16579
16580 /*
16581 ** Free all resources associated with the IncrMerger object indicated by
16582 ** the first argument.
16583 */
16584 static void vdbeIncrFree(IncrMerger *pIncr){
16585 if( pIncr ){
16586 #if SQLITE_MAX_WORKER_THREADS>0
16587 if( pIncr->bUseThread ){
16588 vdbeSorterJoinThread(pIncr->pTask);
16589 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
16590 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
16591 }
16592 #endif
16593 vdbeMergeEngineFree(pIncr->pMerger);
16594 sqlite3_free(pIncr);
16595 }
16596 }
16597
16598 /*
16599 ** Reset a sorting cursor back to its original empty state.
16600 */
16601 SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
16602 int i;
16603 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
16604 assert( pSorter->bUseThreads || pSorter->pReader==0 );
16605 #if SQLITE_MAX_WORKER_THREADS>0
16606 if( pSorter->pReader ){
16607 vdbePmaReaderClear(pSorter->pReader);
16608 sqlite3DbFree(db, pSorter->pReader);
16609 pSorter->pReader = 0;
16610 }
16611 #endif
16612 vdbeMergeEngineFree(pSorter->pMerger);
16613 pSorter->pMerger = 0;
16614 for(i=0; i<pSorter->nTask; i++){
16615 SortSubtask *pTask = &pSorter->aTask[i];
16616 vdbeSortSubtaskCleanup(db, pTask);
16617 pTask->pSorter = pSorter;
16618 }
16619 if( pSorter->list.aMemory==0 ){
16620 vdbeSorterRecordFree(0, pSorter->list.pList);
16621 }
16622 pSorter->list.pList = 0;
16623 pSorter->list.szPMA = 0;
16624 pSorter->bUsePMA = 0;
16625 pSorter->iMemory = 0;
16626 pSorter->mxKeysize = 0;
16627 sqlite3DbFree(db, pSorter->pUnpacked);
16628 pSorter->pUnpacked = 0;
16629 }
16630
16631 /*
16632 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
16633 */
16634 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
16635 VdbeSorter *pSorter;
16636 assert( pCsr->eCurType==CURTYPE_SORTER );
16637 pSorter = pCsr->uc.pSorter;
16638 if( pSorter ){
16639 sqlite3VdbeSorterReset(db, pSorter);
16640 sqlite3_free(pSorter->list.aMemory);
16641 sqlite3DbFree(db, pSorter);
16642 pCsr->uc.pSorter = 0;
16643 }
16644 }
16645
16646 #if SQLITE_MAX_MMAP_SIZE>0
16647 /*
16648 ** The first argument is a file-handle open on a temporary file. The file
16649 ** is guaranteed to be nByte bytes or smaller in size. This function
16650 ** attempts to extend the file to nByte bytes in size and to ensure that
16651 ** the VFS has memory mapped it.
16652 **
16653 ** Whether or not the file does end up memory mapped of course depends on
16654 ** the specific VFS implementation.
16655 */
16656 static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
16657 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
16658 void *p = 0;
16659 int chunksize = 4*1024;
16660 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
16661 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
16662 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
16663 sqlite3OsUnfetch(pFd, 0, p);
16664 }
16665 }
16666 #else
16667 # define vdbeSorterExtendFile(x,y,z)
16668 #endif
16669
16670 /*
16671 ** Allocate space for a file-handle and open a temporary file. If successful,
16672 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
16673 ** Otherwise, set *ppFd to 0 and return an SQLite error code.
16674 */
16675 static int vdbeSorterOpenTempFile(
16676 sqlite3 *db, /* Database handle doing sort */
16677 i64 nExtend, /* Attempt to extend file to this size */
16678 sqlite3_file **ppFd
16679 ){
16680 int rc;
16681 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
16682 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
16683 SQLITE_OPEN_TEMP_JOURNAL |
16684 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
16685 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
16686 );
16687 if( rc==SQLITE_OK ){
16688 i64 max = SQLITE_MAX_MMAP_SIZE;
16689 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
16690 if( nExtend>0 ){
16691 vdbeSorterExtendFile(db, *ppFd, nExtend);
16692 }
16693 }
16694 return rc;
16695 }
16696
16697 /*
16698 ** If it has not already been allocated, allocate the UnpackedRecord
16699 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
16700 ** if no allocation was required), or SQLITE_NOMEM otherwise.
16701 */
16702 static int vdbeSortAllocUnpacked(SortSubtask *pTask){
16703 if( pTask->pUnpacked==0 ){
16704 char *pFree;
16705 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(
16706 pTask->pSorter->pKeyInfo, 0, 0, &pFree
16707 );
16708 assert( pTask->pUnpacked==(UnpackedRecord*)pFree );
16709 if( pFree==0 ) return SQLITE_NOMEM;
16710 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
16711 pTask->pUnpacked->errCode = 0;
16712 }
16713 return SQLITE_OK;
16714 }
16715
16716
16717 /*
16718 ** Merge the two sorted lists p1 and p2 into a single list.
16719 ** Set *ppOut to the head of the new list.
16720 */
16721 static void vdbeSorterMerge(
16722 SortSubtask *pTask, /* Calling thread context */
16723 SorterRecord *p1, /* First list to merge */
16724 SorterRecord *p2, /* Second list to merge */
16725 SorterRecord **ppOut /* OUT: Head of merged list */
16726 ){
16727 SorterRecord *pFinal = 0;
16728 SorterRecord **pp = &pFinal;
16729 int bCached = 0;
16730
16731 while( p1 && p2 ){
16732 int res;
16733 res = pTask->xCompare(
16734 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
16735 );
16736
16737 if( res<=0 ){
16738 *pp = p1;
16739 pp = &p1->u.pNext;
16740 p1 = p1->u.pNext;
16741 }else{
16742 *pp = p2;
16743 pp = &p2->u.pNext;
16744 p2 = p2->u.pNext;
16745 bCached = 0;
16746 }
16747 }
16748 *pp = p1 ? p1 : p2;
16749 *ppOut = pFinal;
16750 }
16751
16752 /*
16753 ** Return the SorterCompare function to compare values collected by the
16754 ** sorter object passed as the only argument.
16755 */
16756 static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
16757 if( p->typeMask==SORTER_TYPE_INTEGER ){
16758 return vdbeSorterCompareInt;
16759 }else if( p->typeMask==SORTER_TYPE_TEXT ){
16760 return vdbeSorterCompareText;
16761 }
16762 return vdbeSorterCompare;
16763 }
16764
16765 /*
16766 ** Sort the linked list of records headed at pTask->pList. Return
16767 ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
16768 ** an error occurs.
16769 */
16770 static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
16771 int i;
16772 SorterRecord **aSlot;
16773 SorterRecord *p;
16774 int rc;
16775
16776 rc = vdbeSortAllocUnpacked(pTask);
16777 if( rc!=SQLITE_OK ) return rc;
16778
16779 p = pList->pList;
16780 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
16781
16782 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
16783 if( !aSlot ){
16784 return SQLITE_NOMEM;
16785 }
16786
16787 while( p ){
16788 SorterRecord *pNext;
16789 if( pList->aMemory ){
16790 if( (u8*)p==pList->aMemory ){
16791 pNext = 0;
16792 }else{
16793 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
16794 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
16795 }
16796 }else{
16797 pNext = p->u.pNext;
16798 }
16799
16800 p->u.pNext = 0;
16801 for(i=0; aSlot[i]; i++){
16802 vdbeSorterMerge(pTask, p, aSlot[i], &p);
16803 aSlot[i] = 0;
16804 }
16805 aSlot[i] = p;
16806 p = pNext;
16807 }
16808
16809 p = 0;
16810 for(i=0; i<64; i++){
16811 vdbeSorterMerge(pTask, p, aSlot[i], &p);
16812 }
16813 pList->pList = p;
16814
16815 sqlite3_free(aSlot);
16816 assert( pTask->pUnpacked->errCode==SQLITE_OK
16817 || pTask->pUnpacked->errCode==SQLITE_NOMEM
16818 );
16819 return pTask->pUnpacked->errCode;
16820 }
16821
16822 /*
16823 ** Initialize a PMA-writer object.
16824 */
16825 static void vdbePmaWriterInit(
16826 sqlite3_file *pFd, /* File handle to write to */
16827 PmaWriter *p, /* Object to populate */
16828 int nBuf, /* Buffer size */
16829 i64 iStart /* Offset of pFd to begin writing at */
16830 ){
16831 memset(p, 0, sizeof(PmaWriter));
16832 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
16833 if( !p->aBuffer ){
16834 p->eFWErr = SQLITE_NOMEM;
16835 }else{
16836 p->iBufEnd = p->iBufStart = (iStart % nBuf);
16837 p->iWriteOff = iStart - p->iBufStart;
16838 p->nBuffer = nBuf;
16839 p->pFd = pFd;
16840 }
16841 }
16842
16843 /*
16844 ** Write nData bytes of data to the PMA. Return SQLITE_OK
16845 ** if successful, or an SQLite error code if an error occurs.
16846 */
16847 static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
16848 int nRem = nData;
16849 while( nRem>0 && p->eFWErr==0 ){
16850 int nCopy = nRem;
16851 if( nCopy>(p->nBuffer - p->iBufEnd) ){
16852 nCopy = p->nBuffer - p->iBufEnd;
16853 }
16854
16855 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
16856 p->iBufEnd += nCopy;
16857 if( p->iBufEnd==p->nBuffer ){
16858 p->eFWErr = sqlite3OsWrite(p->pFd,
16859 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
16860 p->iWriteOff + p->iBufStart
16861 );
16862 p->iBufStart = p->iBufEnd = 0;
16863 p->iWriteOff += p->nBuffer;
16864 }
16865 assert( p->iBufEnd<p->nBuffer );
16866
16867 nRem -= nCopy;
16868 }
16869 }
16870
16871 /*
16872 ** Flush any buffered data to disk and clean up the PMA-writer object.
16873 ** The results of using the PMA-writer after this call are undefined.
16874 ** Return SQLITE_OK if flushing the buffered data succeeds or is not
16875 ** required. Otherwise, return an SQLite error code.
16876 **
16877 ** Before returning, set *piEof to the offset immediately following the
16878 ** last byte written to the file.
16879 */
16880 static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
16881 int rc;
16882 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
16883 p->eFWErr = sqlite3OsWrite(p->pFd,
16884 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
16885 p->iWriteOff + p->iBufStart
16886 );
16887 }
16888 *piEof = (p->iWriteOff + p->iBufEnd);
16889 sqlite3_free(p->aBuffer);
16890 rc = p->eFWErr;
16891 memset(p, 0, sizeof(PmaWriter));
16892 return rc;
16893 }
16894
16895 /*
16896 ** Write value iVal encoded as a varint to the PMA. Return
16897 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
16898 */
16899 static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
16900 int nByte;
16901 u8 aByte[10];
16902 nByte = sqlite3PutVarint(aByte, iVal);
16903 vdbePmaWriteBlob(p, aByte, nByte);
16904 }
16905
16906 /*
16907 ** Write the current contents of in-memory linked-list pList to a level-0
16908 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
16909 ** successful, or an SQLite error code otherwise.
16910 **
16911 ** The format of a PMA is:
16912 **
16913 ** * A varint. This varint contains the total number of bytes of content
16914 ** in the PMA (not including the varint itself).
16915 **
16916 ** * One or more records packed end-to-end in order of ascending keys.
16917 ** Each record consists of a varint followed by a blob of data (the
16918 ** key). The varint is the number of bytes in the blob of data.
16919 */
16920 static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
16921 sqlite3 *db = pTask->pSorter->db;
16922 int rc = SQLITE_OK; /* Return code */
16923 PmaWriter writer; /* Object used to write to the file */
16924
16925 #ifdef SQLITE_DEBUG
16926 /* Set iSz to the expected size of file pTask->file after writing the PMA.
16927 ** This is used by an assert() statement at the end of this function. */
16928 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
16929 #endif
16930
16931 vdbeSorterWorkDebug(pTask, "enter");
16932 memset(&writer, 0, sizeof(PmaWriter));
16933 assert( pList->szPMA>0 );
16934
16935 /* If the first temporary PMA file has not been opened, open it now. */
16936 if( pTask->file.pFd==0 ){
16937 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
16938 assert( rc!=SQLITE_OK || pTask->file.pFd );
16939 assert( pTask->file.iEof==0 );
16940 assert( pTask->nPMA==0 );
16941 }
16942
16943 /* Try to get the file to memory map */
16944 if( rc==SQLITE_OK ){
16945 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
16946 }
16947
16948 /* Sort the list */
16949 if( rc==SQLITE_OK ){
16950 rc = vdbeSorterSort(pTask, pList);
16951 }
16952
16953 if( rc==SQLITE_OK ){
16954 SorterRecord *p;
16955 SorterRecord *pNext = 0;
16956
16957 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
16958 pTask->file.iEof);
16959 pTask->nPMA++;
16960 vdbePmaWriteVarint(&writer, pList->szPMA);
16961 for(p=pList->pList; p; p=pNext){
16962 pNext = p->u.pNext;
16963 vdbePmaWriteVarint(&writer, p->nVal);
16964 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
16965 if( pList->aMemory==0 ) sqlite3_free(p);
16966 }
16967 pList->pList = p;
16968 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
16969 }
16970
16971 vdbeSorterWorkDebug(pTask, "exit");
16972 assert( rc!=SQLITE_OK || pList->pList==0 );
16973 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
16974 return rc;
16975 }
16976
16977 /*
16978 ** Advance the MergeEngine to its next entry.
16979 ** Set *pbEof to true there is no next entry because
16980 ** the MergeEngine has reached the end of all its inputs.
16981 **
16982 ** Return SQLITE_OK if successful or an error code if an error occurs.
16983 */
16984 static int vdbeMergeEngineStep(
16985 MergeEngine *pMerger, /* The merge engine to advance to the next row */
16986 int *pbEof /* Set TRUE at EOF. Set false for more content */
16987 ){
16988 int rc;
16989 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
16990 SortSubtask *pTask = pMerger->pTask;
16991
16992 /* Advance the current PmaReader */
16993 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
16994
16995 /* Update contents of aTree[] */
16996 if( rc==SQLITE_OK ){
16997 int i; /* Index of aTree[] to recalculate */
16998 PmaReader *pReadr1; /* First PmaReader to compare */
16999 PmaReader *pReadr2; /* Second PmaReader to compare */
17000 int bCached = 0;
17001
17002 /* Find the first two PmaReaders to compare. The one that was just
17003 ** advanced (iPrev) and the one next to it in the array. */
17004 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
17005 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
17006
17007 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
17008 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
17009 int iRes;
17010 if( pReadr1->pFd==0 ){
17011 iRes = +1;
17012 }else if( pReadr2->pFd==0 ){
17013 iRes = -1;
17014 }else{
17015 iRes = pTask->xCompare(pTask, &bCached,
17016 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
17017 );
17018 }
17019
17020 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
17021 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
17022 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
17023 ** pKey2 to point to the record belonging to pReadr2.
17024 **
17025 ** Alternatively, if pReadr2 contains the smaller of the two values,
17026 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
17027 ** was actually called above, then pTask->pUnpacked now contains
17028 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
17029 ** vdbeSorterCompare() from decoding pReadr2 again.
17030 **
17031 ** If the two values were equal, then the value from the oldest
17032 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
17033 ** is sorted from oldest to newest, so pReadr1 contains older values
17034 ** than pReadr2 iff (pReadr1<pReadr2). */
17035 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
17036 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
17037 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
17038 bCached = 0;
17039 }else{
17040 if( pReadr1->pFd ) bCached = 0;
17041 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
17042 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
17043 }
17044 }
17045 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
17046 }
17047
17048 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
17049 }
17050
17051 #if SQLITE_MAX_WORKER_THREADS>0
17052 /*
17053 ** The main routine for background threads that write level-0 PMAs.
17054 */
17055 static void *vdbeSorterFlushThread(void *pCtx){
17056 SortSubtask *pTask = (SortSubtask*)pCtx;
17057 int rc; /* Return code */
17058 assert( pTask->bDone==0 );
17059 rc = vdbeSorterListToPMA(pTask, &pTask->list);
17060 pTask->bDone = 1;
17061 return SQLITE_INT_TO_PTR(rc);
17062 }
17063 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
17064
17065 /*
17066 ** Flush the current contents of VdbeSorter.list to a new PMA, possibly
17067 ** using a background thread.
17068 */
17069 static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
17070 #if SQLITE_MAX_WORKER_THREADS==0
17071 pSorter->bUsePMA = 1;
17072 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
17073 #else
17074 int rc = SQLITE_OK;
17075 int i;
17076 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
17077 int nWorker = (pSorter->nTask-1);
17078
17079 /* Set the flag to indicate that at least one PMA has been written.
17080 ** Or will be, anyhow. */
17081 pSorter->bUsePMA = 1;
17082
17083 /* Select a sub-task to sort and flush the current list of in-memory
17084 ** records to disk. If the sorter is running in multi-threaded mode,
17085 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
17086 ** the background thread from a sub-tasks previous turn is still running,
17087 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
17088 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
17089 ** sub-tasks are prefered as they use background threads - the final
17090 ** sub-task uses the main thread. */
17091 for(i=0; i<nWorker; i++){
17092 int iTest = (pSorter->iPrev + i + 1) % nWorker;
17093 pTask = &pSorter->aTask[iTest];
17094 if( pTask->bDone ){
17095 rc = vdbeSorterJoinThread(pTask);
17096 }
17097 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
17098 }
17099
17100 if( rc==SQLITE_OK ){
17101 if( i==nWorker ){
17102 /* Use the foreground thread for this operation */
17103 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
17104 }else{
17105 /* Launch a background thread for this operation */
17106 u8 *aMem = pTask->list.aMemory;
17107 void *pCtx = (void*)pTask;
17108
17109 assert( pTask->pThread==0 && pTask->bDone==0 );
17110 assert( pTask->list.pList==0 );
17111 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
17112
17113 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
17114 pTask->list = pSorter->list;
17115 pSorter->list.pList = 0;
17116 pSorter->list.szPMA = 0;
17117 if( aMem ){
17118 pSorter->list.aMemory = aMem;
17119 pSorter->nMemory = sqlite3MallocSize(aMem);
17120 }else if( pSorter->list.aMemory ){
17121 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
17122 if( !pSorter->list.aMemory ) return SQLITE_NOMEM;
17123 }
17124
17125 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
17126 }
17127 }
17128
17129 return rc;
17130 #endif /* SQLITE_MAX_WORKER_THREADS!=0 */
17131 }
17132
17133 /*
17134 ** Add a record to the sorter.
17135 */
17136 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
17137 const VdbeCursor *pCsr, /* Sorter cursor */
17138 Mem *pVal /* Memory cell containing record */
17139 ){
17140 VdbeSorter *pSorter;
17141 int rc = SQLITE_OK; /* Return Code */
17142 SorterRecord *pNew; /* New list element */
17143 int bFlush; /* True to flush contents of memory to PMA */
17144 int nReq; /* Bytes of memory required */
17145 int nPMA; /* Bytes of PMA space required */
17146 int t; /* serial type of first record field */
17147
17148 assert( pCsr->eCurType==CURTYPE_SORTER );
17149 pSorter = pCsr->uc.pSorter;
17150 getVarint32((const u8*)&pVal->z[1], t);
17151 if( t>0 && t<10 && t!=7 ){
17152 pSorter->typeMask &= SORTER_TYPE_INTEGER;
17153 }else if( t>10 && (t & 0x01) ){
17154 pSorter->typeMask &= SORTER_TYPE_TEXT;
17155 }else{
17156 pSorter->typeMask = 0;
17157 }
17158
17159 assert( pSorter );
17160
17161 /* Figure out whether or not the current contents of memory should be
17162 ** flushed to a PMA before continuing. If so, do so.
17163 **
17164 ** If using the single large allocation mode (pSorter->aMemory!=0), then
17165 ** flush the contents of memory to a new PMA if (a) at least one value is
17166 ** already in memory and (b) the new value will not fit in memory.
17167 **
17168 ** Or, if using separate allocations for each record, flush the contents
17169 ** of memory to a PMA if either of the following are true:
17170 **
17171 ** * The total memory allocated for the in-memory list is greater
17172 ** than (page-size * cache-size), or
17173 **
17174 ** * The total memory allocated for the in-memory list is greater
17175 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
17176 */
17177 nReq = pVal->n + sizeof(SorterRecord);
17178 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
17179 if( pSorter->mxPmaSize ){
17180 if( pSorter->list.aMemory ){
17181 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
17182 }else{
17183 bFlush = (
17184 (pSorter->list.szPMA > pSorter->mxPmaSize)
17185 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
17186 );
17187 }
17188 if( bFlush ){
17189 rc = vdbeSorterFlushPMA(pSorter);
17190 pSorter->list.szPMA = 0;
17191 pSorter->iMemory = 0;
17192 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
17193 }
17194 }
17195
17196 pSorter->list.szPMA += nPMA;
17197 if( nPMA>pSorter->mxKeysize ){
17198 pSorter->mxKeysize = nPMA;
17199 }
17200
17201 if( pSorter->list.aMemory ){
17202 int nMin = pSorter->iMemory + nReq;
17203
17204 if( nMin>pSorter->nMemory ){
17205 u8 *aNew;
17206 int nNew = pSorter->nMemory * 2;
17207 while( nNew < nMin ) nNew = nNew*2;
17208 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
17209 if( nNew < nMin ) nNew = nMin;
17210
17211 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
17212 if( !aNew ) return SQLITE_NOMEM;
17213 pSorter->list.pList = (SorterRecord*)(
17214 aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
17215 );
17216 pSorter->list.aMemory = aNew;
17217 pSorter->nMemory = nNew;
17218 }
17219
17220 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
17221 pSorter->iMemory += ROUND8(nReq);
17222 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
17223 }else{
17224 pNew = (SorterRecord *)sqlite3Malloc(nReq);
17225 if( pNew==0 ){
17226 return SQLITE_NOMEM;
17227 }
17228 pNew->u.pNext = pSorter->list.pList;
17229 }
17230
17231 memcpy(SRVAL(pNew), pVal->z, pVal->n);
17232 pNew->nVal = pVal->n;
17233 pSorter->list.pList = pNew;
17234
17235 return rc;
17236 }
17237
17238 /*
17239 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
17240 ** of the data stored in aFile[1] is the same as that used by regular PMAs,
17241 ** except that the number-of-bytes varint is omitted from the start.
17242 */
17243 static int vdbeIncrPopulate(IncrMerger *pIncr){
17244 int rc = SQLITE_OK;
17245 int rc2;
17246 i64 iStart = pIncr->iStartOff;
17247 SorterFile *pOut = &pIncr->aFile[1];
17248 SortSubtask *pTask = pIncr->pTask;
17249 MergeEngine *pMerger = pIncr->pMerger;
17250 PmaWriter writer;
17251 assert( pIncr->bEof==0 );
17252
17253 vdbeSorterPopulateDebug(pTask, "enter");
17254
17255 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
17256 while( rc==SQLITE_OK ){
17257 int dummy;
17258 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
17259 int nKey = pReader->nKey;
17260 i64 iEof = writer.iWriteOff + writer.iBufEnd;
17261
17262 /* Check if the output file is full or if the input has been exhausted.
17263 ** In either case exit the loop. */
17264 if( pReader->pFd==0 ) break;
17265 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
17266
17267 /* Write the next key to the output. */
17268 vdbePmaWriteVarint(&writer, nKey);
17269 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
17270 assert( pIncr->pMerger->pTask==pTask );
17271 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
17272 }
17273
17274 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
17275 if( rc==SQLITE_OK ) rc = rc2;
17276 vdbeSorterPopulateDebug(pTask, "exit");
17277 return rc;
17278 }
17279
17280 #if SQLITE_MAX_WORKER_THREADS>0
17281 /*
17282 ** The main routine for background threads that populate aFile[1] of
17283 ** multi-threaded IncrMerger objects.
17284 */
17285 static void *vdbeIncrPopulateThread(void *pCtx){
17286 IncrMerger *pIncr = (IncrMerger*)pCtx;
17287 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
17288 pIncr->pTask->bDone = 1;
17289 return pRet;
17290 }
17291
17292 /*
17293 ** Launch a background thread to populate aFile[1] of pIncr.
17294 */
17295 static int vdbeIncrBgPopulate(IncrMerger *pIncr){
17296 void *p = (void*)pIncr;
17297 assert( pIncr->bUseThread );
17298 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
17299 }
17300 #endif
17301
17302 /*
17303 ** This function is called when the PmaReader corresponding to pIncr has
17304 ** finished reading the contents of aFile[0]. Its purpose is to "refill"
17305 ** aFile[0] such that the PmaReader should start rereading it from the
17306 ** beginning.
17307 **
17308 ** For single-threaded objects, this is accomplished by literally reading
17309 ** keys from pIncr->pMerger and repopulating aFile[0].
17310 **
17311 ** For multi-threaded objects, all that is required is to wait until the
17312 ** background thread is finished (if it is not already) and then swap
17313 ** aFile[0] and aFile[1] in place. If the contents of pMerger have not
17314 ** been exhausted, this function also launches a new background thread
17315 ** to populate the new aFile[1].
17316 **
17317 ** SQLITE_OK is returned on success, or an SQLite error code otherwise.
17318 */
17319 static int vdbeIncrSwap(IncrMerger *pIncr){
17320 int rc = SQLITE_OK;
17321
17322 #if SQLITE_MAX_WORKER_THREADS>0
17323 if( pIncr->bUseThread ){
17324 rc = vdbeSorterJoinThread(pIncr->pTask);
17325
17326 if( rc==SQLITE_OK ){
17327 SorterFile f0 = pIncr->aFile[0];
17328 pIncr->aFile[0] = pIncr->aFile[1];
17329 pIncr->aFile[1] = f0;
17330 }
17331
17332 if( rc==SQLITE_OK ){
17333 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
17334 pIncr->bEof = 1;
17335 }else{
17336 rc = vdbeIncrBgPopulate(pIncr);
17337 }
17338 }
17339 }else
17340 #endif
17341 {
17342 rc = vdbeIncrPopulate(pIncr);
17343 pIncr->aFile[0] = pIncr->aFile[1];
17344 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
17345 pIncr->bEof = 1;
17346 }
17347 }
17348
17349 return rc;
17350 }
17351
17352 /*
17353 ** Allocate and return a new IncrMerger object to read data from pMerger.
17354 **
17355 ** If an OOM condition is encountered, return NULL. In this case free the
17356 ** pMerger argument before returning.
17357 */
17358 static int vdbeIncrMergerNew(
17359 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
17360 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
17361 IncrMerger **ppOut /* Write the new IncrMerger here */
17362 ){
17363 int rc = SQLITE_OK;
17364 IncrMerger *pIncr = *ppOut = (IncrMerger*)
17365 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
17366 if( pIncr ){
17367 pIncr->pMerger = pMerger;
17368 pIncr->pTask = pTask;
17369 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
17370 pTask->file2.iEof += pIncr->mxSz;
17371 }else{
17372 vdbeMergeEngineFree(pMerger);
17373 rc = SQLITE_NOMEM;
17374 }
17375 return rc;
17376 }
17377
17378 #if SQLITE_MAX_WORKER_THREADS>0
17379 /*
17380 ** Set the "use-threads" flag on object pIncr.
17381 */
17382 static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
17383 pIncr->bUseThread = 1;
17384 pIncr->pTask->file2.iEof -= pIncr->mxSz;
17385 }
17386 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
17387
17388
17389
17390 /*
17391 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
17392 ** two PmaReaders that feed that entry. Neither of the PmaReaders
17393 ** are advanced. This routine merely does the comparison.
17394 */
17395 static void vdbeMergeEngineCompare(
17396 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
17397 int iOut /* Store the result in pMerger->aTree[iOut] */
17398 ){
17399 int i1;
17400 int i2;
17401 int iRes;
17402 PmaReader *p1;
17403 PmaReader *p2;
17404
17405 assert( iOut<pMerger->nTree && iOut>0 );
17406
17407 if( iOut>=(pMerger->nTree/2) ){
17408 i1 = (iOut - pMerger->nTree/2) * 2;
17409 i2 = i1 + 1;
17410 }else{
17411 i1 = pMerger->aTree[iOut*2];
17412 i2 = pMerger->aTree[iOut*2+1];
17413 }
17414
17415 p1 = &pMerger->aReadr[i1];
17416 p2 = &pMerger->aReadr[i2];
17417
17418 if( p1->pFd==0 ){
17419 iRes = i2;
17420 }else if( p2->pFd==0 ){
17421 iRes = i1;
17422 }else{
17423 SortSubtask *pTask = pMerger->pTask;
17424 int bCached = 0;
17425 int res;
17426 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
17427 res = pTask->xCompare(
17428 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
17429 );
17430 if( res<=0 ){
17431 iRes = i1;
17432 }else{
17433 iRes = i2;
17434 }
17435 }
17436
17437 pMerger->aTree[iOut] = iRes;
17438 }
17439
17440 /*
17441 ** Allowed values for the eMode parameter to vdbeMergeEngineInit()
17442 ** and vdbePmaReaderIncrMergeInit().
17443 **
17444 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
17445 ** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
17446 ** when there exists one or more separate worker threads.
17447 */
17448 #define INCRINIT_NORMAL 0
17449 #define INCRINIT_TASK 1
17450 #define INCRINIT_ROOT 2
17451
17452 /*
17453 ** Forward reference required as the vdbeIncrMergeInit() and
17454 ** vdbePmaReaderIncrInit() routines are called mutually recursively when
17455 ** building a merge tree.
17456 */
17457 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
17458
17459 /*
17460 ** Initialize the MergeEngine object passed as the second argument. Once this
17461 ** function returns, the first key of merged data may be read from the
17462 ** MergeEngine object in the usual fashion.
17463 **
17464 ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
17465 ** objects attached to the PmaReader objects that the merger reads from have
17466 ** already been populated, but that they have not yet populated aFile[0] and
17467 ** set the PmaReader objects up to read from it. In this case all that is
17468 ** required is to call vdbePmaReaderNext() on each PmaReader to point it at
17469 ** its first key.
17470 **
17471 ** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
17472 ** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
17473 ** to pMerger.
17474 **
17475 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
17476 */
17477 static int vdbeMergeEngineInit(
17478 SortSubtask *pTask, /* Thread that will run pMerger */
17479 MergeEngine *pMerger, /* MergeEngine to initialize */
17480 int eMode /* One of the INCRINIT_XXX constants */
17481 ){
17482 int rc = SQLITE_OK; /* Return code */
17483 int i; /* For looping over PmaReader objects */
17484 int nTree = pMerger->nTree;
17485
17486 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
17487 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
17488
17489 /* Verify that the MergeEngine is assigned to a single thread */
17490 assert( pMerger->pTask==0 );
17491 pMerger->pTask = pTask;
17492
17493 for(i=0; i<nTree; i++){
17494 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
17495 /* PmaReaders should be normally initialized in order, as if they are
17496 ** reading from the same temp file this makes for more linear file IO.
17497 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
17498 ** in use it will block the vdbePmaReaderNext() call while it uses
17499 ** the main thread to fill its buffer. So calling PmaReaderNext()
17500 ** on this PmaReader before any of the multi-threaded PmaReaders takes
17501 ** better advantage of multi-processor hardware. */
17502 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
17503 }else{
17504 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
17505 }
17506 if( rc!=SQLITE_OK ) return rc;
17507 }
17508
17509 for(i=pMerger->nTree-1; i>0; i--){
17510 vdbeMergeEngineCompare(pMerger, i);
17511 }
17512 return pTask->pUnpacked->errCode;
17513 }
17514
17515 /*
17516 ** The PmaReader passed as the first argument is guaranteed to be an
17517 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
17518 ** and/or initialize the temp file related fields of the IncrMerge
17519 ** object at (pReadr->pIncr).
17520 **
17521 ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
17522 ** in the sub-tree headed by pReadr are also initialized. Data is then
17523 ** loaded into the buffers belonging to pReadr and it is set to point to
17524 ** the first key in its range.
17525 **
17526 ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
17527 ** to be a multi-threaded PmaReader and this function is being called in a
17528 ** background thread. In this case all PmaReaders in the sub-tree are
17529 ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
17530 ** pReadr is populated. However, pReadr itself is not set up to point
17531 ** to its first key. A call to vdbePmaReaderNext() is still required to do
17532 ** that.
17533 **
17534 ** The reason this function does not call vdbePmaReaderNext() immediately
17535 ** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
17536 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
17537 ** this entire function is being run by thread (pTask->thread), that will
17538 ** lead to the current background thread attempting to join itself.
17539 **
17540 ** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
17541 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
17542 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
17543 ** In this case vdbePmaReaderNext() is called on all child PmaReaders and
17544 ** the current PmaReader set to point to the first key in its range.
17545 **
17546 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
17547 */
17548 static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
17549 int rc = SQLITE_OK;
17550 IncrMerger *pIncr = pReadr->pIncr;
17551 SortSubtask *pTask = pIncr->pTask;
17552 sqlite3 *db = pTask->pSorter->db;
17553
17554 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
17555 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
17556
17557 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
17558
17559 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
17560 ** requires two temp files to itself, whereas a single-threaded object
17561 ** only requires a region of pTask->file2. */
17562 if( rc==SQLITE_OK ){
17563 int mxSz = pIncr->mxSz;
17564 #if SQLITE_MAX_WORKER_THREADS>0
17565 if( pIncr->bUseThread ){
17566 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
17567 if( rc==SQLITE_OK ){
17568 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
17569 }
17570 }else
17571 #endif
17572 /*if( !pIncr->bUseThread )*/{
17573 if( pTask->file2.pFd==0 ){
17574 assert( pTask->file2.iEof>0 );
17575 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
17576 pTask->file2.iEof = 0;
17577 }
17578 if( rc==SQLITE_OK ){
17579 pIncr->aFile[1].pFd = pTask->file2.pFd;
17580 pIncr->iStartOff = pTask->file2.iEof;
17581 pTask->file2.iEof += mxSz;
17582 }
17583 }
17584 }
17585
17586 #if SQLITE_MAX_WORKER_THREADS>0
17587 if( rc==SQLITE_OK && pIncr->bUseThread ){
17588 /* Use the current thread to populate aFile[1], even though this
17589 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
17590 ** then this function is already running in background thread
17591 ** pIncr->pTask->thread.
17592 **
17593 ** If this is the INCRINIT_ROOT object, then it is running in the
17594 ** main VDBE thread. But that is Ok, as that thread cannot return
17595 ** control to the VDBE or proceed with anything useful until the
17596 ** first results are ready from this merger object anyway.
17597 */
17598 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
17599 rc = vdbeIncrPopulate(pIncr);
17600 }
17601 #endif
17602
17603 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
17604 rc = vdbePmaReaderNext(pReadr);
17605 }
17606
17607 return rc;
17608 }
17609
17610 #if SQLITE_MAX_WORKER_THREADS>0
17611 /*
17612 ** The main routine for vdbePmaReaderIncrMergeInit() operations run in
17613 ** background threads.
17614 */
17615 static void *vdbePmaReaderBgIncrInit(void *pCtx){
17616 PmaReader *pReader = (PmaReader*)pCtx;
17617 void *pRet = SQLITE_INT_TO_PTR(
17618 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
17619 );
17620 pReader->pIncr->pTask->bDone = 1;
17621 return pRet;
17622 }
17623 #endif
17624
17625 /*
17626 ** If the PmaReader passed as the first argument is not an incremental-reader
17627 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
17628 ** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
17629 ** this routine to initialize the incremental merge.
17630 **
17631 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
17632 ** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
17633 ** Or, if the IncrMerger is single threaded, the same function is called
17634 ** using the current thread.
17635 */
17636 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
17637 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
17638 int rc = SQLITE_OK; /* Return code */
17639 if( pIncr ){
17640 #if SQLITE_MAX_WORKER_THREADS>0
17641 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
17642 if( pIncr->bUseThread ){
17643 void *pCtx = (void*)pReadr;
17644 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
17645 }else
17646 #endif
17647 {
17648 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
17649 }
17650 }
17651 return rc;
17652 }
17653
17654 /*
17655 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
17656 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
17657 ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
17658 ** to NULL and return an SQLite error code.
17659 **
17660 ** When this function is called, *piOffset is set to the offset of the
17661 ** first PMA to read from pTask->file. Assuming no error occurs, it is
17662 ** set to the offset immediately following the last byte of the last
17663 ** PMA before returning. If an error does occur, then the final value of
17664 ** *piOffset is undefined.
17665 */
17666 static int vdbeMergeEngineLevel0(
17667 SortSubtask *pTask, /* Sorter task to read from */
17668 int nPMA, /* Number of PMAs to read */
17669 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
17670 MergeEngine **ppOut /* OUT: New merge-engine */
17671 ){
17672 MergeEngine *pNew; /* Merge engine to return */
17673 i64 iOff = *piOffset;
17674 int i;
17675 int rc = SQLITE_OK;
17676
17677 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
17678 if( pNew==0 ) rc = SQLITE_NOMEM;
17679
17680 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
17681 i64 nDummy;
17682 PmaReader *pReadr = &pNew->aReadr[i];
17683 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
17684 iOff = pReadr->iEof;
17685 }
17686
17687 if( rc!=SQLITE_OK ){
17688 vdbeMergeEngineFree(pNew);
17689 *ppOut = 0;
17690 }
17691 *piOffset = iOff;
17692 return rc;
17693 }
17694
17695 /*
17696 ** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
17697 ** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
17698 **
17699 ** i.e.
17700 **
17701 ** nPMA<=16 -> TreeDepth() == 0
17702 ** nPMA<=256 -> TreeDepth() == 1
17703 ** nPMA<=65536 -> TreeDepth() == 2
17704 */
17705 static int vdbeSorterTreeDepth(int nPMA){
17706 int nDepth = 0;
17707 i64 nDiv = SORTER_MAX_MERGE_COUNT;
17708 while( nDiv < (i64)nPMA ){
17709 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
17710 nDepth++;
17711 }
17712 return nDepth;
17713 }
17714
17715 /*
17716 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
17717 ** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
17718 ** tree, counting from zero. This function adds pLeaf to the tree.
17719 **
17720 ** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
17721 ** code is returned and pLeaf is freed.
17722 */
17723 static int vdbeSorterAddToTree(
17724 SortSubtask *pTask, /* Task context */
17725 int nDepth, /* Depth of tree according to TreeDepth() */
17726 int iSeq, /* Sequence number of leaf within tree */
17727 MergeEngine *pRoot, /* Root of tree */
17728 MergeEngine *pLeaf /* Leaf to add to tree */
17729 ){
17730 int rc = SQLITE_OK;
17731 int nDiv = 1;
17732 int i;
17733 MergeEngine *p = pRoot;
17734 IncrMerger *pIncr;
17735
17736 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
17737
17738 for(i=1; i<nDepth; i++){
17739 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
17740 }
17741
17742 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
17743 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
17744 PmaReader *pReadr = &p->aReadr[iIter];
17745
17746 if( pReadr->pIncr==0 ){
17747 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
17748 if( pNew==0 ){
17749 rc = SQLITE_NOMEM;
17750 }else{
17751 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
17752 }
17753 }
17754 if( rc==SQLITE_OK ){
17755 p = pReadr->pIncr->pMerger;
17756 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
17757 }
17758 }
17759
17760 if( rc==SQLITE_OK ){
17761 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
17762 }else{
17763 vdbeIncrFree(pIncr);
17764 }
17765 return rc;
17766 }
17767
17768 /*
17769 ** This function is called as part of a SorterRewind() operation on a sorter
17770 ** that has already written two or more level-0 PMAs to one or more temp
17771 ** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
17772 ** can be used to incrementally merge all PMAs on disk.
17773 **
17774 ** If successful, SQLITE_OK is returned and *ppOut set to point to the
17775 ** MergeEngine object at the root of the tree before returning. Or, if an
17776 ** error occurs, an SQLite error code is returned and the final value
17777 ** of *ppOut is undefined.
17778 */
17779 static int vdbeSorterMergeTreeBuild(
17780 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
17781 MergeEngine **ppOut /* Write the MergeEngine here */
17782 ){
17783 MergeEngine *pMain = 0;
17784 int rc = SQLITE_OK;
17785 int iTask;
17786
17787 #if SQLITE_MAX_WORKER_THREADS>0
17788 /* If the sorter uses more than one task, then create the top-level
17789 ** MergeEngine here. This MergeEngine will read data from exactly
17790 ** one PmaReader per sub-task. */
17791 assert( pSorter->bUseThreads || pSorter->nTask==1 );
17792 if( pSorter->nTask>1 ){
17793 pMain = vdbeMergeEngineNew(pSorter->nTask);
17794 if( pMain==0 ) rc = SQLITE_NOMEM;
17795 }
17796 #endif
17797
17798 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
17799 SortSubtask *pTask = &pSorter->aTask[iTask];
17800 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
17801 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
17802 MergeEngine *pRoot = 0; /* Root node of tree for this task */
17803 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
17804 i64 iReadOff = 0;
17805
17806 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
17807 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
17808 }else{
17809 int i;
17810 int iSeq = 0;
17811 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
17812 if( pRoot==0 ) rc = SQLITE_NOMEM;
17813 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
17814 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
17815 int nReader; /* Number of level-0 PMAs to merge */
17816
17817 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
17818 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
17819 if( rc==SQLITE_OK ){
17820 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
17821 }
17822 }
17823 }
17824
17825 if( rc==SQLITE_OK ){
17826 #if SQLITE_MAX_WORKER_THREADS>0
17827 if( pMain!=0 ){
17828 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
17829 }else
17830 #endif
17831 {
17832 assert( pMain==0 );
17833 pMain = pRoot;
17834 }
17835 }else{
17836 vdbeMergeEngineFree(pRoot);
17837 }
17838 }
17839 }
17840
17841 if( rc!=SQLITE_OK ){
17842 vdbeMergeEngineFree(pMain);
17843 pMain = 0;
17844 }
17845 *ppOut = pMain;
17846 return rc;
17847 }
17848
17849 /*
17850 ** This function is called as part of an sqlite3VdbeSorterRewind() operation
17851 ** on a sorter that has written two or more PMAs to temporary files. It sets
17852 ** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
17853 ** (for multi-threaded sorters) so that it can be used to iterate through
17854 ** all records stored in the sorter.
17855 **
17856 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
17857 */
17858 static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
17859 int rc; /* Return code */
17860 SortSubtask *pTask0 = &pSorter->aTask[0];
17861 MergeEngine *pMain = 0;
17862 #if SQLITE_MAX_WORKER_THREADS
17863 sqlite3 *db = pTask0->pSorter->db;
17864 int i;
17865 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
17866 for(i=0; i<pSorter->nTask; i++){
17867 pSorter->aTask[i].xCompare = xCompare;
17868 }
17869 #endif
17870
17871 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
17872 if( rc==SQLITE_OK ){
17873 #if SQLITE_MAX_WORKER_THREADS
17874 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
17875 if( pSorter->bUseThreads ){
17876 int iTask;
17877 PmaReader *pReadr = 0;
17878 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
17879 rc = vdbeSortAllocUnpacked(pLast);
17880 if( rc==SQLITE_OK ){
17881 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
17882 pSorter->pReader = pReadr;
17883 if( pReadr==0 ) rc = SQLITE_NOMEM;
17884 }
17885 if( rc==SQLITE_OK ){
17886 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
17887 if( rc==SQLITE_OK ){
17888 vdbeIncrMergerSetThreads(pReadr->pIncr);
17889 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
17890 IncrMerger *pIncr;
17891 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
17892 vdbeIncrMergerSetThreads(pIncr);
17893 assert( pIncr->pTask!=pLast );
17894 }
17895 }
17896 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
17897 /* Check that:
17898 **
17899 ** a) The incremental merge object is configured to use the
17900 ** right task, and
17901 ** b) If it is using task (nTask-1), it is configured to run
17902 ** in single-threaded mode. This is important, as the
17903 ** root merge (INCRINIT_ROOT) will be using the same task
17904 ** object.
17905 */
17906 PmaReader *p = &pMain->aReadr[iTask];
17907 assert( p->pIncr==0 || (
17908 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
17909 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
17910 ));
17911 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
17912 }
17913 }
17914 pMain = 0;
17915 }
17916 if( rc==SQLITE_OK ){
17917 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
17918 }
17919 }else
17920 #endif
17921 {
17922 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
17923 pSorter->pMerger = pMain;
17924 pMain = 0;
17925 }
17926 }
17927
17928 if( rc!=SQLITE_OK ){
17929 vdbeMergeEngineFree(pMain);
17930 }
17931 return rc;
17932 }
17933
17934
17935 /*
17936 ** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
17937 ** this function is called to prepare for iterating through the records
17938 ** in sorted order.
17939 */
17940 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
17941 VdbeSorter *pSorter;
17942 int rc = SQLITE_OK; /* Return code */
17943
17944 assert( pCsr->eCurType==CURTYPE_SORTER );
17945 pSorter = pCsr->uc.pSorter;
17946 assert( pSorter );
17947
17948 /* If no data has been written to disk, then do not do so now. Instead,
17949 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
17950 ** from the in-memory list. */
17951 if( pSorter->bUsePMA==0 ){
17952 if( pSorter->list.pList ){
17953 *pbEof = 0;
17954 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
17955 }else{
17956 *pbEof = 1;
17957 }
17958 return rc;
17959 }
17960
17961 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
17962 ** function flushes the contents of memory to disk, it immediately always
17963 ** creates a new list consisting of a single key immediately afterwards.
17964 ** So the list is never empty at this point. */
17965 assert( pSorter->list.pList );
17966 rc = vdbeSorterFlushPMA(pSorter);
17967
17968 /* Join all threads */
17969 rc = vdbeSorterJoinAll(pSorter, rc);
17970
17971 vdbeSorterRewindDebug("rewind");
17972
17973 /* Assuming no errors have occurred, set up a merger structure to
17974 ** incrementally read and merge all remaining PMAs. */
17975 assert( pSorter->pReader==0 );
17976 if( rc==SQLITE_OK ){
17977 rc = vdbeSorterSetupMerge(pSorter);
17978 *pbEof = 0;
17979 }
17980
17981 vdbeSorterRewindDebug("rewinddone");
17982 return rc;
17983 }
17984
17985 /*
17986 ** Advance to the next element in the sorter.
17987 */
17988 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, in t *pbEof){
17989 VdbeSorter *pSorter;
17990 int rc; /* Return code */
17991
17992 assert( pCsr->eCurType==CURTYPE_SORTER );
17993 pSorter = pCsr->uc.pSorter;
17994 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
17995 if( pSorter->bUsePMA ){
17996 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
17997 assert( pSorter->bUseThreads==0 || pSorter->pReader );
17998 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
17999 #if SQLITE_MAX_WORKER_THREADS>0
18000 if( pSorter->bUseThreads ){
18001 rc = vdbePmaReaderNext(pSorter->pReader);
18002 *pbEof = (pSorter->pReader->pFd==0);
18003 }else
18004 #endif
18005 /*if( !pSorter->bUseThreads )*/ {
18006 assert( pSorter->pMerger!=0 );
18007 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
18008 rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof);
18009 }
18010 }else{
18011 SorterRecord *pFree = pSorter->list.pList;
18012 pSorter->list.pList = pFree->u.pNext;
18013 pFree->u.pNext = 0;
18014 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
18015 *pbEof = !pSorter->list.pList;
18016 rc = SQLITE_OK;
18017 }
18018 return rc;
18019 }
18020
18021 /*
18022 ** Return a pointer to a buffer owned by the sorter that contains the
18023 ** current key.
18024 */
18025 static void *vdbeSorterRowkey(
18026 const VdbeSorter *pSorter, /* Sorter object */
18027 int *pnKey /* OUT: Size of current key in bytes */
18028 ){
18029 void *pKey;
18030 if( pSorter->bUsePMA ){
18031 PmaReader *pReader;
18032 #if SQLITE_MAX_WORKER_THREADS>0
18033 if( pSorter->bUseThreads ){
18034 pReader = pSorter->pReader;
18035 }else
18036 #endif
18037 /*if( !pSorter->bUseThreads )*/{
18038 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
18039 }
18040 *pnKey = pReader->nKey;
18041 pKey = pReader->aKey;
18042 }else{
18043 *pnKey = pSorter->list.pList->nVal;
18044 pKey = SRVAL(pSorter->list.pList);
18045 }
18046 return pKey;
18047 }
18048
18049 /*
18050 ** Copy the current sorter key into the memory cell pOut.
18051 */
18052 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
18053 VdbeSorter *pSorter;
18054 void *pKey; int nKey; /* Sorter key to copy into pOut */
18055
18056 assert( pCsr->eCurType==CURTYPE_SORTER );
18057 pSorter = pCsr->uc.pSorter;
18058 pKey = vdbeSorterRowkey(pSorter, &nKey);
18059 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
18060 return SQLITE_NOMEM;
18061 }
18062 pOut->n = nKey;
18063 MemSetTypeFlag(pOut, MEM_Blob);
18064 memcpy(pOut->z, pKey, nKey);
18065
18066 return SQLITE_OK;
18067 }
18068
18069 /*
18070 ** Compare the key in memory cell pVal with the key that the sorter cursor
18071 ** passed as the first argument currently points to. For the purposes of
18072 ** the comparison, ignore the rowid field at the end of each record.
18073 **
18074 ** If the sorter cursor key contains any NULL values, consider it to be
18075 ** less than pVal. Even if pVal also contains NULL values.
18076 **
18077 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
18078 ** Otherwise, set *pRes to a negative, zero or positive value if the
18079 ** key in pVal is smaller than, equal to or larger than the current sorter
18080 ** key.
18081 **
18082 ** This routine forms the core of the OP_SorterCompare opcode, which in
18083 ** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
18084 */
18085 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
18086 const VdbeCursor *pCsr, /* Sorter cursor */
18087 Mem *pVal, /* Value to compare to current sorter key */
18088 int nKeyCol, /* Compare this many columns */
18089 int *pRes /* OUT: Result of comparison */
18090 ){
18091 VdbeSorter *pSorter;
18092 UnpackedRecord *r2;
18093 KeyInfo *pKeyInfo;
18094 int i;
18095 void *pKey; int nKey; /* Sorter key to compare pVal with */
18096
18097 assert( pCsr->eCurType==CURTYPE_SORTER );
18098 pSorter = pCsr->uc.pSorter;
18099 r2 = pSorter->pUnpacked;
18100 pKeyInfo = pCsr->pKeyInfo;
18101 if( r2==0 ){
18102 char *p;
18103 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p);
18104 assert( pSorter->pUnpacked==(UnpackedRecord*)p );
18105 if( r2==0 ) return SQLITE_NOMEM;
18106 r2->nField = nKeyCol;
18107 }
18108 assert( r2->nField==nKeyCol );
18109
18110 pKey = vdbeSorterRowkey(pSorter, &nKey);
18111 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
18112 for(i=0; i<nKeyCol; i++){
18113 if( r2->aMem[i].flags & MEM_Null ){
18114 *pRes = -1;
18115 return SQLITE_OK;
18116 }
18117 }
18118
18119 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
18120 return SQLITE_OK;
18121 }
18122
18123 /************** End of vdbesort.c ********************************************/
18124 /************** Begin file journal.c *****************************************/
18125 /*
18126 ** 2007 August 22
18127 **
18128 ** The author disclaims copyright to this source code. In place of
18129 ** a legal notice, here is a blessing:
18130 **
18131 ** May you do good and not evil.
18132 ** May you find forgiveness for yourself and forgive others.
18133 ** May you share freely, never taking more than you give.
18134 **
18135 *************************************************************************
18136 **
18137 ** This file implements a special kind of sqlite3_file object used
18138 ** by SQLite to create journal files if the atomic-write optimization
18139 ** is enabled.
18140 **
18141 ** The distinctive characteristic of this sqlite3_file is that the
18142 ** actual on disk file is created lazily. When the file is created,
18143 ** the caller specifies a buffer size for an in-memory buffer to
18144 ** be used to service read() and write() requests. The actual file
18145 ** on disk is not created or populated until either:
18146 **
18147 ** 1) The in-memory representation grows too large for the allocated
18148 ** buffer, or
18149 ** 2) The sqlite3JournalCreate() function is called.
18150 */
18151 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
18152 /* #include "sqliteInt.h" */
18153
18154
18155 /*
18156 ** A JournalFile object is a subclass of sqlite3_file used by
18157 ** as an open file handle for journal files.
18158 */
18159 struct JournalFile {
18160 sqlite3_io_methods *pMethod; /* I/O methods on journal files */
18161 int nBuf; /* Size of zBuf[] in bytes */
18162 char *zBuf; /* Space to buffer journal writes */
18163 int iSize; /* Amount of zBuf[] currently used */
18164 int flags; /* xOpen flags */
18165 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
18166 sqlite3_file *pReal; /* The "real" underlying file descriptor */
18167 const char *zJournal; /* Name of the journal file */
18168 };
18169 typedef struct JournalFile JournalFile;
18170
18171 /*
18172 ** If it does not already exists, create and populate the on-disk file
18173 ** for JournalFile p.
18174 */
18175 static int createFile(JournalFile *p){
18176 int rc = SQLITE_OK;
18177 if( !p->pReal ){
18178 sqlite3_file *pReal = (sqlite3_file *)&p[1];
18179 rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
18180 if( rc==SQLITE_OK ){
18181 p->pReal = pReal;
18182 if( p->iSize>0 ){
18183 assert(p->iSize<=p->nBuf);
18184 rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
18185 }
18186 if( rc!=SQLITE_OK ){
18187 /* If an error occurred while writing to the file, close it before
18188 ** returning. This way, SQLite uses the in-memory journal data to
18189 ** roll back changes made to the internal page-cache before this
18190 ** function was called. */
18191 sqlite3OsClose(pReal);
18192 p->pReal = 0;
18193 }
18194 }
18195 }
18196 return rc;
18197 }
18198
18199 /*
18200 ** Close the file.
18201 */
18202 static int jrnlClose(sqlite3_file *pJfd){
18203 JournalFile *p = (JournalFile *)pJfd;
18204 if( p->pReal ){
18205 sqlite3OsClose(p->pReal);
18206 }
18207 sqlite3_free(p->zBuf);
18208 return SQLITE_OK;
18209 }
18210
18211 /*
18212 ** Read data from the file.
18213 */
18214 static int jrnlRead(
18215 sqlite3_file *pJfd, /* The journal file from which to read */
18216 void *zBuf, /* Put the results here */
18217 int iAmt, /* Number of bytes to read */
18218 sqlite_int64 iOfst /* Begin reading at this offset */
18219 ){
18220 int rc = SQLITE_OK;
18221 JournalFile *p = (JournalFile *)pJfd;
18222 if( p->pReal ){
18223 rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
18224 }else if( (iAmt+iOfst)>p->iSize ){
18225 rc = SQLITE_IOERR_SHORT_READ;
18226 }else{
18227 memcpy(zBuf, &p->zBuf[iOfst], iAmt);
18228 }
18229 return rc;
18230 }
18231
18232 /*
18233 ** Write data to the file.
18234 */
18235 static int jrnlWrite(
18236 sqlite3_file *pJfd, /* The journal file into which to write */
18237 const void *zBuf, /* Take data to be written from here */
18238 int iAmt, /* Number of bytes to write */
18239 sqlite_int64 iOfst /* Begin writing at this offset into the file */
18240 ){
18241 int rc = SQLITE_OK;
18242 JournalFile *p = (JournalFile *)pJfd;
18243 if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
18244 rc = createFile(p);
18245 }
18246 if( rc==SQLITE_OK ){
18247 if( p->pReal ){
18248 rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
18249 }else{
18250 memcpy(&p->zBuf[iOfst], zBuf, iAmt);
18251 if( p->iSize<(iOfst+iAmt) ){
18252 p->iSize = (iOfst+iAmt);
18253 }
18254 }
18255 }
18256 return rc;
18257 }
18258
18259 /*
18260 ** Truncate the file.
18261 */
18262 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
18263 int rc = SQLITE_OK;
18264 JournalFile *p = (JournalFile *)pJfd;
18265 if( p->pReal ){
18266 rc = sqlite3OsTruncate(p->pReal, size);
18267 }else if( size<p->iSize ){
18268 p->iSize = size;
18269 }
18270 return rc;
18271 }
18272
18273 /*
18274 ** Sync the file.
18275 */
18276 static int jrnlSync(sqlite3_file *pJfd, int flags){
18277 int rc;
18278 JournalFile *p = (JournalFile *)pJfd;
18279 if( p->pReal ){
18280 rc = sqlite3OsSync(p->pReal, flags);
18281 }else{
18282 rc = SQLITE_OK;
18283 }
18284 return rc;
18285 }
18286
18287 /*
18288 ** Query the size of the file in bytes.
18289 */
18290 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
18291 int rc = SQLITE_OK;
18292 JournalFile *p = (JournalFile *)pJfd;
18293 if( p->pReal ){
18294 rc = sqlite3OsFileSize(p->pReal, pSize);
18295 }else{
18296 *pSize = (sqlite_int64) p->iSize;
18297 }
18298 return rc;
18299 }
18300
18301 /*
18302 ** Table of methods for JournalFile sqlite3_file object.
18303 */
18304 static struct sqlite3_io_methods JournalFileMethods = {
18305 1, /* iVersion */
18306 jrnlClose, /* xClose */
18307 jrnlRead, /* xRead */
18308 jrnlWrite, /* xWrite */
18309 jrnlTruncate, /* xTruncate */
18310 jrnlSync, /* xSync */
18311 jrnlFileSize, /* xFileSize */
18312 0, /* xLock */
18313 0, /* xUnlock */
18314 0, /* xCheckReservedLock */
18315 0, /* xFileControl */
18316 0, /* xSectorSize */
18317 0, /* xDeviceCharacteristics */
18318 0, /* xShmMap */
18319 0, /* xShmLock */
18320 0, /* xShmBarrier */
18321 0 /* xShmUnmap */
18322 };
18323
18324 /*
18325 ** Open a journal file.
18326 */
18327 SQLITE_PRIVATE int sqlite3JournalOpen(
18328 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
18329 const char *zName, /* Name of the journal file */
18330 sqlite3_file *pJfd, /* Preallocated, blank file handle */
18331 int flags, /* Opening flags */
18332 int nBuf /* Bytes buffered before opening the file */
18333 ){
18334 JournalFile *p = (JournalFile *)pJfd;
18335 memset(p, 0, sqlite3JournalSize(pVfs));
18336 if( nBuf>0 ){
18337 p->zBuf = sqlite3MallocZero(nBuf);
18338 if( !p->zBuf ){
18339 return SQLITE_NOMEM;
18340 }
18341 }else{
18342 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
18343 }
18344 p->pMethod = &JournalFileMethods;
18345 p->nBuf = nBuf;
18346 p->flags = flags;
18347 p->zJournal = zName;
18348 p->pVfs = pVfs;
18349 return SQLITE_OK;
18350 }
18351
18352 /*
18353 ** If the argument p points to a JournalFile structure, and the underlying
18354 ** file has not yet been created, create it now.
18355 */
18356 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
18357 if( p->pMethods!=&JournalFileMethods ){
18358 return SQLITE_OK;
18359 }
18360 return createFile((JournalFile *)p);
18361 }
18362
18363 /*
18364 ** The file-handle passed as the only argument is guaranteed to be an open
18365 ** file. It may or may not be of class JournalFile. If the file is a
18366 ** JournalFile, and the underlying file on disk has not yet been opened,
18367 ** return 0. Otherwise, return 1.
18368 */
18369 SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
18370 return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
18371 }
18372
18373 /*
18374 ** Return the number of bytes required to store a JournalFile that uses vfs
18375 ** pVfs to create the underlying on-disk files.
18376 */
18377 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
18378 return (pVfs->szOsFile+sizeof(JournalFile));
18379 }
18380 #endif
18381
18382 /************** End of journal.c *********************************************/
18383 /************** Begin file memjournal.c **************************************/
18384 /*
18385 ** 2008 October 7
18386 **
18387 ** The author disclaims copyright to this source code. In place of
18388 ** a legal notice, here is a blessing:
18389 **
18390 ** May you do good and not evil.
18391 ** May you find forgiveness for yourself and forgive others.
18392 ** May you share freely, never taking more than you give.
18393 **
18394 *************************************************************************
18395 **
18396 ** This file contains code use to implement an in-memory rollback journal.
18397 ** The in-memory rollback journal is used to journal transactions for
18398 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
18399 */
18400 /* #include "sqliteInt.h" */
18401
18402 /* Forward references to internal structures */
18403 typedef struct MemJournal MemJournal;
18404 typedef struct FilePoint FilePoint;
18405 typedef struct FileChunk FileChunk;
18406
18407 /* Space to hold the rollback journal is allocated in increments of
18408 ** this many bytes.
18409 **
18410 ** The size chosen is a little less than a power of two. That way,
18411 ** the FileChunk object will have a size that almost exactly fills
18412 ** a power-of-two allocation. This minimizes wasted space in power-of-two
18413 ** memory allocators.
18414 */
18415 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
18416
18417 /*
18418 ** The rollback journal is composed of a linked list of these structures.
18419 */
18420 struct FileChunk {
18421 FileChunk *pNext; /* Next chunk in the journal */
18422 u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */
18423 };
18424
18425 /*
18426 ** An instance of this object serves as a cursor into the rollback journal.
18427 ** The cursor can be either for reading or writing.
18428 */
18429 struct FilePoint {
18430 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
18431 FileChunk *pChunk; /* Specific chunk into which cursor points */
18432 };
18433
18434 /*
18435 ** This subclass is a subclass of sqlite3_file. Each open memory-journal
18436 ** is an instance of this class.
18437 */
18438 struct MemJournal {
18439 sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
18440 FileChunk *pFirst; /* Head of in-memory chunk-list */
18441 FilePoint endpoint; /* Pointer to the end of the file */
18442 FilePoint readpoint; /* Pointer to the end of the last xRead() */
18443 };
18444
18445 /*
18446 ** Read data from the in-memory journal file. This is the implementation
18447 ** of the sqlite3_vfs.xRead method.
18448 */
18449 static int memjrnlRead(
18450 sqlite3_file *pJfd, /* The journal file from which to read */
18451 void *zBuf, /* Put the results here */
18452 int iAmt, /* Number of bytes to read */
18453 sqlite_int64 iOfst /* Begin reading at this offset */
18454 ){
18455 MemJournal *p = (MemJournal *)pJfd;
18456 u8 *zOut = zBuf;
18457 int nRead = iAmt;
18458 int iChunkOffset;
18459 FileChunk *pChunk;
18460
18461 /* SQLite never tries to read past the end of a rollback journal file */
18462 assert( iOfst+iAmt<=p->endpoint.iOffset );
18463
18464 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
18465 sqlite3_int64 iOff = 0;
18466 for(pChunk=p->pFirst;
18467 ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
18468 pChunk=pChunk->pNext
18469 ){
18470 iOff += JOURNAL_CHUNKSIZE;
18471 }
18472 }else{
18473 pChunk = p->readpoint.pChunk;
18474 }
18475
18476 iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
18477 do {
18478 int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
18479 int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
18480 memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
18481 zOut += nCopy;
18482 nRead -= iSpace;
18483 iChunkOffset = 0;
18484 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
18485 p->readpoint.iOffset = iOfst+iAmt;
18486 p->readpoint.pChunk = pChunk;
18487
18488 return SQLITE_OK;
18489 }
18490
18491 /*
18492 ** Write data to the file.
18493 */
18494 static int memjrnlWrite(
18495 sqlite3_file *pJfd, /* The journal file into which to write */
18496 const void *zBuf, /* Take data to be written from here */
18497 int iAmt, /* Number of bytes to write */
18498 sqlite_int64 iOfst /* Begin writing at this offset into the file */
18499 ){
18500 MemJournal *p = (MemJournal *)pJfd;
18501 int nWrite = iAmt;
18502 u8 *zWrite = (u8 *)zBuf;
18503
18504 /* An in-memory journal file should only ever be appended to. Random
18505 ** access writes are not required by sqlite.
18506 */
18507 assert( iOfst==p->endpoint.iOffset );
18508 UNUSED_PARAMETER(iOfst);
18509
18510 while( nWrite>0 ){
18511 FileChunk *pChunk = p->endpoint.pChunk;
18512 int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
18513 int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
18514
18515 if( iChunkOffset==0 ){
18516 /* New chunk is required to extend the file. */
18517 FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
18518 if( !pNew ){
18519 return SQLITE_IOERR_NOMEM;
18520 }
18521 pNew->pNext = 0;
18522 if( pChunk ){
18523 assert( p->pFirst );
18524 pChunk->pNext = pNew;
18525 }else{
18526 assert( !p->pFirst );
18527 p->pFirst = pNew;
18528 }
18529 p->endpoint.pChunk = pNew;
18530 }
18531
18532 memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
18533 zWrite += iSpace;
18534 nWrite -= iSpace;
18535 p->endpoint.iOffset += iSpace;
18536 }
18537
18538 return SQLITE_OK;
18539 }
18540
18541 /*
18542 ** Truncate the file.
18543 */
18544 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
18545 MemJournal *p = (MemJournal *)pJfd;
18546 FileChunk *pChunk;
18547 assert(size==0);
18548 UNUSED_PARAMETER(size);
18549 pChunk = p->pFirst;
18550 while( pChunk ){
18551 FileChunk *pTmp = pChunk;
18552 pChunk = pChunk->pNext;
18553 sqlite3_free(pTmp);
18554 }
18555 sqlite3MemJournalOpen(pJfd);
18556 return SQLITE_OK;
18557 }
18558
18559 /*
18560 ** Close the file.
18561 */
18562 static int memjrnlClose(sqlite3_file *pJfd){
18563 memjrnlTruncate(pJfd, 0);
18564 return SQLITE_OK;
18565 }
18566
18567
18568 /*
18569 ** Sync the file.
18570 **
18571 ** Syncing an in-memory journal is a no-op. And, in fact, this routine
18572 ** is never called in a working implementation. This implementation
18573 ** exists purely as a contingency, in case some malfunction in some other
18574 ** part of SQLite causes Sync to be called by mistake.
18575 */
18576 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
18577 UNUSED_PARAMETER2(NotUsed, NotUsed2);
18578 return SQLITE_OK;
18579 }
18580
18581 /*
18582 ** Query the size of the file in bytes.
18583 */
18584 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
18585 MemJournal *p = (MemJournal *)pJfd;
18586 *pSize = (sqlite_int64) p->endpoint.iOffset;
18587 return SQLITE_OK;
18588 }
18589
18590 /*
18591 ** Table of methods for MemJournal sqlite3_file object.
18592 */
18593 static const struct sqlite3_io_methods MemJournalMethods = {
18594 1, /* iVersion */
18595 memjrnlClose, /* xClose */
18596 memjrnlRead, /* xRead */
18597 memjrnlWrite, /* xWrite */
18598 memjrnlTruncate, /* xTruncate */
18599 memjrnlSync, /* xSync */
18600 memjrnlFileSize, /* xFileSize */
18601 0, /* xLock */
18602 0, /* xUnlock */
18603 0, /* xCheckReservedLock */
18604 0, /* xFileControl */
18605 0, /* xSectorSize */
18606 0, /* xDeviceCharacteristics */
18607 0, /* xShmMap */
18608 0, /* xShmLock */
18609 0, /* xShmBarrier */
18610 0, /* xShmUnmap */
18611 0, /* xFetch */
18612 0 /* xUnfetch */
18613 };
18614
18615 /*
18616 ** Open a journal file.
18617 */
18618 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
18619 MemJournal *p = (MemJournal *)pJfd;
18620 assert( EIGHT_BYTE_ALIGNMENT(p) );
18621 memset(p, 0, sqlite3MemJournalSize());
18622 p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
18623 }
18624
18625 /*
18626 ** Return true if the file-handle passed as an argument is
18627 ** an in-memory journal
18628 */
18629 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
18630 return pJfd->pMethods==&MemJournalMethods;
18631 }
18632
18633 /*
18634 ** Return the number of bytes required to store a MemJournal file descriptor.
18635 */
18636 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
18637 return sizeof(MemJournal);
18638 }
18639
18640 /************** End of memjournal.c ******************************************/
18641 /************** Begin file walker.c ******************************************/
18642 /*
18643 ** 2008 August 16
18644 **
18645 ** The author disclaims copyright to this source code. In place of
18646 ** a legal notice, here is a blessing:
18647 **
18648 ** May you do good and not evil.
18649 ** May you find forgiveness for yourself and forgive others.
18650 ** May you share freely, never taking more than you give.
18651 **
18652 *************************************************************************
18653 ** This file contains routines used for walking the parser tree for
18654 ** an SQL statement.
18655 */
18656 /* #include "sqliteInt.h" */
18657 /* #include <stdlib.h> */
18658 /* #include <string.h> */
18659
18660
18661 /*
18662 ** Walk an expression tree. Invoke the callback once for each node
18663 ** of the expression, while descending. (In other words, the callback
18664 ** is invoked before visiting children.)
18665 **
18666 ** The return value from the callback should be one of the WRC_*
18667 ** constants to specify how to proceed with the walk.
18668 **
18669 ** WRC_Continue Continue descending down the tree.
18670 **
18671 ** WRC_Prune Do not descend into child nodes. But allow
18672 ** the walk to continue with sibling nodes.
18673 **
18674 ** WRC_Abort Do no more callbacks. Unwind the stack and
18675 ** return the top-level walk call.
18676 **
18677 ** The return value from this routine is WRC_Abort to abandon the tree walk
18678 ** and WRC_Continue to continue.
18679 */
18680 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
18681 int rc;
18682 if( pExpr==0 ) return WRC_Continue;
18683 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
18684 testcase( ExprHasProperty(pExpr, EP_Reduced) );
18685 rc = pWalker->xExprCallback(pWalker, pExpr);
18686 if( rc==WRC_Continue
18687 && !ExprHasProperty(pExpr,EP_TokenOnly) ){
18688 if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
18689 if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
18690 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
18691 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
18692 }else{
18693 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
18694 }
18695 }
18696 return rc & WRC_Abort;
18697 }
18698
18699 /*
18700 ** Call sqlite3WalkExpr() for every expression in list p or until
18701 ** an abort request is seen.
18702 */
18703 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
18704 int i;
18705 struct ExprList_item *pItem;
18706 if( p ){
18707 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
18708 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
18709 }
18710 }
18711 return WRC_Continue;
18712 }
18713
18714 /*
18715 ** Walk all expressions associated with SELECT statement p. Do
18716 ** not invoke the SELECT callback on p, but do (of course) invoke
18717 ** any expr callbacks and SELECT callbacks that come from subqueries.
18718 ** Return WRC_Abort or WRC_Continue.
18719 */
18720 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
18721 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
18722 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
18723 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
18724 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
18725 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
18726 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
18727 if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
18728 return WRC_Continue;
18729 }
18730
18731 /*
18732 ** Walk the parse trees associated with all subqueries in the
18733 ** FROM clause of SELECT statement p. Do not invoke the select
18734 ** callback on p, but do invoke it on each FROM clause subquery
18735 ** and on any subqueries further down in the tree. Return
18736 ** WRC_Abort or WRC_Continue;
18737 */
18738 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
18739 SrcList *pSrc;
18740 int i;
18741 struct SrcList_item *pItem;
18742
18743 pSrc = p->pSrc;
18744 if( ALWAYS(pSrc) ){
18745 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
18746 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
18747 return WRC_Abort;
18748 }
18749 if( pItem->fg.isTabFunc
18750 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
18751 ){
18752 return WRC_Abort;
18753 }
18754 }
18755 }
18756 return WRC_Continue;
18757 }
18758
18759 /*
18760 ** Call sqlite3WalkExpr() for every expression in Select statement p.
18761 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
18762 ** on the compound select chain, p->pPrior.
18763 **
18764 ** If it is not NULL, the xSelectCallback() callback is invoked before
18765 ** the walk of the expressions and FROM clause. The xSelectCallback2()
18766 ** method, if it is not NULL, is invoked following the walk of the
18767 ** expressions and FROM clause.
18768 **
18769 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
18770 ** there is an abort request.
18771 **
18772 ** If the Walker does not have an xSelectCallback() then this routine
18773 ** is a no-op returning WRC_Continue.
18774 */
18775 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
18776 int rc;
18777 if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
18778 return WRC_Continue;
18779 }
18780 rc = WRC_Continue;
18781 pWalker->walkerDepth++;
18782 while( p ){
18783 if( pWalker->xSelectCallback ){
18784 rc = pWalker->xSelectCallback(pWalker, p);
18785 if( rc ) break;
18786 }
18787 if( sqlite3WalkSelectExpr(pWalker, p)
18788 || sqlite3WalkSelectFrom(pWalker, p)
18789 ){
18790 pWalker->walkerDepth--;
18791 return WRC_Abort;
18792 }
18793 if( pWalker->xSelectCallback2 ){
18794 pWalker->xSelectCallback2(pWalker, p);
18795 }
18796 p = p->pPrior;
18797 }
18798 pWalker->walkerDepth--;
18799 return rc & WRC_Abort;
18800 }
18801
18802 /************** End of walker.c **********************************************/
18803 /************** Begin file resolve.c *****************************************/
18804 /*
18805 ** 2008 August 18
18806 **
18807 ** The author disclaims copyright to this source code. In place of
18808 ** a legal notice, here is a blessing:
18809 **
18810 ** May you do good and not evil.
18811 ** May you find forgiveness for yourself and forgive others.
18812 ** May you share freely, never taking more than you give.
18813 **
18814 *************************************************************************
18815 **
18816 ** This file contains routines used for walking the parser tree and
18817 ** resolve all identifiers by associating them with a particular
18818 ** table and column.
18819 */
18820 /* #include "sqliteInt.h" */
18821 /* #include <stdlib.h> */
18822 /* #include <string.h> */
18823
18824 /*
18825 ** Walk the expression tree pExpr and increase the aggregate function
18826 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
18827 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
18828 ** outer query into an inner subquery.
18829 **
18830 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
18831 ** is a helper function - a callback for the tree walker.
18832 */
18833 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
18834 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
18835 return WRC_Continue;
18836 }
18837 static void incrAggFunctionDepth(Expr *pExpr, int N){
18838 if( N>0 ){
18839 Walker w;
18840 memset(&w, 0, sizeof(w));
18841 w.xExprCallback = incrAggDepth;
18842 w.u.n = N;
18843 sqlite3WalkExpr(&w, pExpr);
18844 }
18845 }
18846
18847 /*
18848 ** Turn the pExpr expression into an alias for the iCol-th column of the
18849 ** result set in pEList.
18850 **
18851 ** If the reference is followed by a COLLATE operator, then make sure
18852 ** the COLLATE operator is preserved. For example:
18853 **
18854 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
18855 **
18856 ** Should be transformed into:
18857 **
18858 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
18859 **
18860 ** The nSubquery parameter specifies how many levels of subquery the
18861 ** alias is removed from the original expression. The usual value is
18862 ** zero but it might be more if the alias is contained within a subquery
18863 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
18864 ** structures must be increased by the nSubquery amount.
18865 */
18866 static void resolveAlias(
18867 Parse *pParse, /* Parsing context */
18868 ExprList *pEList, /* A result set */
18869 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
18870 Expr *pExpr, /* Transform this into an alias to the result set */
18871 const char *zType, /* "GROUP" or "ORDER" or "" */
18872 int nSubquery /* Number of subqueries that the label is moving */
18873 ){
18874 Expr *pOrig; /* The iCol-th column of the result set */
18875 Expr *pDup; /* Copy of pOrig */
18876 sqlite3 *db; /* The database connection */
18877
18878 assert( iCol>=0 && iCol<pEList->nExpr );
18879 pOrig = pEList->a[iCol].pExpr;
18880 assert( pOrig!=0 );
18881 db = pParse->db;
18882 pDup = sqlite3ExprDup(db, pOrig, 0);
18883 if( pDup==0 ) return;
18884 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
18885 if( pExpr->op==TK_COLLATE ){
18886 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
18887 }
18888 ExprSetProperty(pDup, EP_Alias);
18889
18890 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
18891 ** prevents ExprDelete() from deleting the Expr structure itself,
18892 ** allowing it to be repopulated by the memcpy() on the following line.
18893 ** The pExpr->u.zToken might point into memory that will be freed by the
18894 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
18895 ** make a copy of the token before doing the sqlite3DbFree().
18896 */
18897 ExprSetProperty(pExpr, EP_Static);
18898 sqlite3ExprDelete(db, pExpr);
18899 memcpy(pExpr, pDup, sizeof(*pExpr));
18900 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
18901 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
18902 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
18903 pExpr->flags |= EP_MemToken;
18904 }
18905 sqlite3DbFree(db, pDup);
18906 }
18907
18908
18909 /*
18910 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
18911 **
18912 ** Return FALSE if the USING clause is NULL or if it does not contain
18913 ** zCol.
18914 */
18915 static int nameInUsingClause(IdList *pUsing, const char *zCol){
18916 if( pUsing ){
18917 int k;
18918 for(k=0; k<pUsing->nId; k++){
18919 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
18920 }
18921 }
18922 return 0;
18923 }
18924
18925 /*
18926 ** Subqueries stores the original database, table and column names for their
18927 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
18928 ** Check to see if the zSpan given to this routine matches the zDb, zTab,
18929 ** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
18930 ** match anything.
18931 */
18932 SQLITE_PRIVATE int sqlite3MatchSpanName(
18933 const char *zSpan,
18934 const char *zCol,
18935 const char *zTab,
18936 const char *zDb
18937 ){
18938 int n;
18939 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
18940 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
18941 return 0;
18942 }
18943 zSpan += n+1;
18944 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
18945 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
18946 return 0;
18947 }
18948 zSpan += n+1;
18949 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
18950 return 0;
18951 }
18952 return 1;
18953 }
18954
18955 /*
18956 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
18957 ** that name in the set of source tables in pSrcList and make the pExpr
18958 ** expression node refer back to that source column. The following changes
18959 ** are made to pExpr:
18960 **
18961 ** pExpr->iDb Set the index in db->aDb[] of the database X
18962 ** (even if X is implied).
18963 ** pExpr->iTable Set to the cursor number for the table obtained
18964 ** from pSrcList.
18965 ** pExpr->pTab Points to the Table structure of X.Y (even if
18966 ** X and/or Y are implied.)
18967 ** pExpr->iColumn Set to the column number within the table.
18968 ** pExpr->op Set to TK_COLUMN.
18969 ** pExpr->pLeft Any expression this points to is deleted
18970 ** pExpr->pRight Any expression this points to is deleted.
18971 **
18972 ** The zDb variable is the name of the database (the "X"). This value may be
18973 ** NULL meaning that name is of the form Y.Z or Z. Any available database
18974 ** can be used. The zTable variable is the name of the table (the "Y"). This
18975 ** value can be NULL if zDb is also NULL. If zTable is NULL it
18976 ** means that the form of the name is Z and that columns from any table
18977 ** can be used.
18978 **
18979 ** If the name cannot be resolved unambiguously, leave an error message
18980 ** in pParse and return WRC_Abort. Return WRC_Prune on success.
18981 */
18982 static int lookupName(
18983 Parse *pParse, /* The parsing context */
18984 const char *zDb, /* Name of the database containing table, or NULL */
18985 const char *zTab, /* Name of table containing column, or NULL */
18986 const char *zCol, /* Name of the column. */
18987 NameContext *pNC, /* The name context used to resolve the name */
18988 Expr *pExpr /* Make this EXPR node point to the selected column */
18989 ){
18990 int i, j; /* Loop counters */
18991 int cnt = 0; /* Number of matching column names */
18992 int cntTab = 0; /* Number of matching table names */
18993 int nSubquery = 0; /* How many levels of subquery */
18994 sqlite3 *db = pParse->db; /* The database connection */
18995 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
18996 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
18997 NameContext *pTopNC = pNC; /* First namecontext in the list */
18998 Schema *pSchema = 0; /* Schema of the expression */
18999 int isTrigger = 0; /* True if resolved to a trigger column */
19000 Table *pTab = 0; /* Table hold the row */
19001 Column *pCol; /* A column of pTab */
19002
19003 assert( pNC ); /* the name context cannot be NULL. */
19004 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
19005 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
19006
19007 /* Initialize the node to no-match */
19008 pExpr->iTable = -1;
19009 pExpr->pTab = 0;
19010 ExprSetVVAProperty(pExpr, EP_NoReduce);
19011
19012 /* Translate the schema name in zDb into a pointer to the corresponding
19013 ** schema. If not found, pSchema will remain NULL and nothing will match
19014 ** resulting in an appropriate error message toward the end of this routine
19015 */
19016 if( zDb ){
19017 testcase( pNC->ncFlags & NC_PartIdx );
19018 testcase( pNC->ncFlags & NC_IsCheck );
19019 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
19020 /* Silently ignore database qualifiers inside CHECK constraints and
19021 ** partial indices. Do not raise errors because that might break
19022 ** legacy and because it does not hurt anything to just ignore the
19023 ** database name. */
19024 zDb = 0;
19025 }else{
19026 for(i=0; i<db->nDb; i++){
19027 assert( db->aDb[i].zName );
19028 if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
19029 pSchema = db->aDb[i].pSchema;
19030 break;
19031 }
19032 }
19033 }
19034 }
19035
19036 /* Start at the inner-most context and move outward until a match is found */
19037 while( pNC && cnt==0 ){
19038 ExprList *pEList;
19039 SrcList *pSrcList = pNC->pSrcList;
19040
19041 if( pSrcList ){
19042 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
19043 pTab = pItem->pTab;
19044 assert( pTab!=0 && pTab->zName!=0 );
19045 assert( pTab->nCol>0 );
19046 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
19047 int hit = 0;
19048 pEList = pItem->pSelect->pEList;
19049 for(j=0; j<pEList->nExpr; j++){
19050 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
19051 cnt++;
19052 cntTab = 2;
19053 pMatch = pItem;
19054 pExpr->iColumn = j;
19055 hit = 1;
19056 }
19057 }
19058 if( hit || zTab==0 ) continue;
19059 }
19060 if( zDb && pTab->pSchema!=pSchema ){
19061 continue;
19062 }
19063 if( zTab ){
19064 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
19065 assert( zTabName!=0 );
19066 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
19067 continue;
19068 }
19069 }
19070 if( 0==(cntTab++) ){
19071 pMatch = pItem;
19072 }
19073 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
19074 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
19075 /* If there has been exactly one prior match and this match
19076 ** is for the right-hand table of a NATURAL JOIN or is in a
19077 ** USING clause, then skip this match.
19078 */
19079 if( cnt==1 ){
19080 if( pItem->fg.jointype & JT_NATURAL ) continue;
19081 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
19082 }
19083 cnt++;
19084 pMatch = pItem;
19085 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
19086 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
19087 break;
19088 }
19089 }
19090 }
19091 if( pMatch ){
19092 pExpr->iTable = pMatch->iCursor;
19093 pExpr->pTab = pMatch->pTab;
19094 /* RIGHT JOIN not (yet) supported */
19095 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
19096 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
19097 ExprSetProperty(pExpr, EP_CanBeNull);
19098 }
19099 pSchema = pExpr->pTab->pSchema;
19100 }
19101 } /* if( pSrcList ) */
19102
19103 #ifndef SQLITE_OMIT_TRIGGER
19104 /* If we have not already resolved the name, then maybe
19105 ** it is a new.* or old.* trigger argument reference
19106 */
19107 if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
19108 int op = pParse->eTriggerOp;
19109 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
19110 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
19111 pExpr->iTable = 1;
19112 pTab = pParse->pTriggerTab;
19113 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
19114 pExpr->iTable = 0;
19115 pTab = pParse->pTriggerTab;
19116 }else{
19117 pTab = 0;
19118 }
19119
19120 if( pTab ){
19121 int iCol;
19122 pSchema = pTab->pSchema;
19123 cntTab++;
19124 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
19125 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
19126 if( iCol==pTab->iPKey ){
19127 iCol = -1;
19128 }
19129 break;
19130 }
19131 }
19132 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
19133 /* IMP: R-51414-32910 */
19134 iCol = -1;
19135 }
19136 if( iCol<pTab->nCol ){
19137 cnt++;
19138 if( iCol<0 ){
19139 pExpr->affinity = SQLITE_AFF_INTEGER;
19140 }else if( pExpr->iTable==0 ){
19141 testcase( iCol==31 );
19142 testcase( iCol==32 );
19143 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
19144 }else{
19145 testcase( iCol==31 );
19146 testcase( iCol==32 );
19147 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
19148 }
19149 pExpr->iColumn = (i16)iCol;
19150 pExpr->pTab = pTab;
19151 isTrigger = 1;
19152 }
19153 }
19154 }
19155 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
19156
19157 /*
19158 ** Perhaps the name is a reference to the ROWID
19159 */
19160 if( cnt==0
19161 && cntTab==1
19162 && pMatch
19163 && (pNC->ncFlags & NC_IdxExpr)==0
19164 && sqlite3IsRowid(zCol)
19165 && VisibleRowid(pMatch->pTab)
19166 ){
19167 cnt = 1;
19168 pExpr->iColumn = -1;
19169 pExpr->affinity = SQLITE_AFF_INTEGER;
19170 }
19171
19172 /*
19173 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
19174 ** might refer to an result-set alias. This happens, for example, when
19175 ** we are resolving names in the WHERE clause of the following command:
19176 **
19177 ** SELECT a+b AS x FROM table WHERE x<10;
19178 **
19179 ** In cases like this, replace pExpr with a copy of the expression that
19180 ** forms the result set entry ("a+b" in the example) and return immediately.
19181 ** Note that the expression in the result set should have already been
19182 ** resolved by the time the WHERE clause is resolved.
19183 **
19184 ** The ability to use an output result-set column in the WHERE, GROUP BY,
19185 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
19186 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
19187 ** is supported for backwards compatibility only. Hence, we issue a warning
19188 ** on sqlite3_log() whenever the capability is used.
19189 */
19190 if( (pEList = pNC->pEList)!=0
19191 && zTab==0
19192 && cnt==0
19193 ){
19194 for(j=0; j<pEList->nExpr; j++){
19195 char *zAs = pEList->a[j].zName;
19196 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
19197 Expr *pOrig;
19198 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
19199 assert( pExpr->x.pList==0 );
19200 assert( pExpr->x.pSelect==0 );
19201 pOrig = pEList->a[j].pExpr;
19202 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
19203 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
19204 return WRC_Abort;
19205 }
19206 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
19207 cnt = 1;
19208 pMatch = 0;
19209 assert( zTab==0 && zDb==0 );
19210 goto lookupname_end;
19211 }
19212 }
19213 }
19214
19215 /* Advance to the next name context. The loop will exit when either
19216 ** we have a match (cnt>0) or when we run out of name contexts.
19217 */
19218 if( cnt==0 ){
19219 pNC = pNC->pNext;
19220 nSubquery++;
19221 }
19222 }
19223
19224 /*
19225 ** If X and Y are NULL (in other words if only the column name Z is
19226 ** supplied) and the value of Z is enclosed in double-quotes, then
19227 ** Z is a string literal if it doesn't match any column names. In that
19228 ** case, we need to return right away and not make any changes to
19229 ** pExpr.
19230 **
19231 ** Because no reference was made to outer contexts, the pNC->nRef
19232 ** fields are not changed in any context.
19233 */
19234 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
19235 pExpr->op = TK_STRING;
19236 pExpr->pTab = 0;
19237 return WRC_Prune;
19238 }
19239
19240 /*
19241 ** cnt==0 means there was not match. cnt>1 means there were two or
19242 ** more matches. Either way, we have an error.
19243 */
19244 if( cnt!=1 ){
19245 const char *zErr;
19246 zErr = cnt==0 ? "no such column" : "ambiguous column name";
19247 if( zDb ){
19248 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
19249 }else if( zTab ){
19250 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
19251 }else{
19252 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
19253 }
19254 pParse->checkSchema = 1;
19255 pTopNC->nErr++;
19256 }
19257
19258 /* If a column from a table in pSrcList is referenced, then record
19259 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
19260 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
19261 ** column number is greater than the number of bits in the bitmask
19262 ** then set the high-order bit of the bitmask.
19263 */
19264 if( pExpr->iColumn>=0 && pMatch!=0 ){
19265 int n = pExpr->iColumn;
19266 testcase( n==BMS-1 );
19267 if( n>=BMS ){
19268 n = BMS-1;
19269 }
19270 assert( pMatch->iCursor==pExpr->iTable );
19271 pMatch->colUsed |= ((Bitmask)1)<<n;
19272 }
19273
19274 /* Clean up and return
19275 */
19276 sqlite3ExprDelete(db, pExpr->pLeft);
19277 pExpr->pLeft = 0;
19278 sqlite3ExprDelete(db, pExpr->pRight);
19279 pExpr->pRight = 0;
19280 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
19281 lookupname_end:
19282 if( cnt==1 ){
19283 assert( pNC!=0 );
19284 if( !ExprHasProperty(pExpr, EP_Alias) ){
19285 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
19286 }
19287 /* Increment the nRef value on all name contexts from TopNC up to
19288 ** the point where the name matched. */
19289 for(;;){
19290 assert( pTopNC!=0 );
19291 pTopNC->nRef++;
19292 if( pTopNC==pNC ) break;
19293 pTopNC = pTopNC->pNext;
19294 }
19295 return WRC_Prune;
19296 } else {
19297 return WRC_Abort;
19298 }
19299 }
19300
19301 /*
19302 ** Allocate and return a pointer to an expression to load the column iCol
19303 ** from datasource iSrc in SrcList pSrc.
19304 */
19305 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSr c, int iCol){
19306 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
19307 if( p ){
19308 struct SrcList_item *pItem = &pSrc->a[iSrc];
19309 p->pTab = pItem->pTab;
19310 p->iTable = pItem->iCursor;
19311 if( p->pTab->iPKey==iCol ){
19312 p->iColumn = -1;
19313 }else{
19314 p->iColumn = (ynVar)iCol;
19315 testcase( iCol==BMS );
19316 testcase( iCol==BMS-1 );
19317 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
19318 }
19319 ExprSetProperty(p, EP_Resolved);
19320 }
19321 return p;
19322 }
19323
19324 /*
19325 ** Report an error that an expression is not valid for some set of
19326 ** pNC->ncFlags values determined by validMask.
19327 */
19328 static void notValid(
19329 Parse *pParse, /* Leave error message here */
19330 NameContext *pNC, /* The name context */
19331 const char *zMsg, /* Type of error */
19332 int validMask /* Set of contexts for which prohibited */
19333 ){
19334 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
19335 if( (pNC->ncFlags & validMask)!=0 ){
19336 const char *zIn = "partial index WHERE clauses";
19337 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
19338 #ifndef SQLITE_OMIT_CHECK
19339 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
19340 #endif
19341 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
19342 }
19343 }
19344
19345 /*
19346 ** Expression p should encode a floating point value between 1.0 and 0.0.
19347 ** Return 1024 times this value. Or return -1 if p is not a floating point
19348 ** value between 1.0 and 0.0.
19349 */
19350 static int exprProbability(Expr *p){
19351 double r = -1.0;
19352 if( p->op!=TK_FLOAT ) return -1;
19353 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
19354 assert( r>=0.0 );
19355 if( r>1.0 ) return -1;
19356 return (int)(r*134217728.0);
19357 }
19358
19359 /*
19360 ** This routine is callback for sqlite3WalkExpr().
19361 **
19362 ** Resolve symbolic names into TK_COLUMN operators for the current
19363 ** node in the expression tree. Return 0 to continue the search down
19364 ** the tree or 2 to abort the tree walk.
19365 **
19366 ** This routine also does error checking and name resolution for
19367 ** function names. The operator for aggregate functions is changed
19368 ** to TK_AGG_FUNCTION.
19369 */
19370 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
19371 NameContext *pNC;
19372 Parse *pParse;
19373
19374 pNC = pWalker->u.pNC;
19375 assert( pNC!=0 );
19376 pParse = pNC->pParse;
19377 assert( pParse==pWalker->pParse );
19378
19379 if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
19380 ExprSetProperty(pExpr, EP_Resolved);
19381 #ifndef NDEBUG
19382 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
19383 SrcList *pSrcList = pNC->pSrcList;
19384 int i;
19385 for(i=0; i<pNC->pSrcList->nSrc; i++){
19386 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
19387 }
19388 }
19389 #endif
19390 switch( pExpr->op ){
19391
19392 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
19393 /* The special operator TK_ROW means use the rowid for the first
19394 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
19395 ** clause processing on UPDATE and DELETE statements.
19396 */
19397 case TK_ROW: {
19398 SrcList *pSrcList = pNC->pSrcList;
19399 struct SrcList_item *pItem;
19400 assert( pSrcList && pSrcList->nSrc==1 );
19401 pItem = pSrcList->a;
19402 pExpr->op = TK_COLUMN;
19403 pExpr->pTab = pItem->pTab;
19404 pExpr->iTable = pItem->iCursor;
19405 pExpr->iColumn = -1;
19406 pExpr->affinity = SQLITE_AFF_INTEGER;
19407 break;
19408 }
19409 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
19410 && !defined(SQLITE_OMIT_SUBQUERY) */
19411
19412 /* A lone identifier is the name of a column.
19413 */
19414 case TK_ID: {
19415 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
19416 }
19417
19418 /* A table name and column name: ID.ID
19419 ** Or a database, table and column: ID.ID.ID
19420 */
19421 case TK_DOT: {
19422 const char *zColumn;
19423 const char *zTable;
19424 const char *zDb;
19425 Expr *pRight;
19426
19427 /* if( pSrcList==0 ) break; */
19428 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
19429 /*notValid(pParse, pNC, "the \".\" operator", NC_PartIdx|NC_IsCheck, 1);*/
19430 pRight = pExpr->pRight;
19431 if( pRight->op==TK_ID ){
19432 zDb = 0;
19433 zTable = pExpr->pLeft->u.zToken;
19434 zColumn = pRight->u.zToken;
19435 }else{
19436 assert( pRight->op==TK_DOT );
19437 zDb = pExpr->pLeft->u.zToken;
19438 zTable = pRight->pLeft->u.zToken;
19439 zColumn = pRight->pRight->u.zToken;
19440 }
19441 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
19442 }
19443
19444 /* Resolve function names
19445 */
19446 case TK_FUNCTION: {
19447 ExprList *pList = pExpr->x.pList; /* The argument list */
19448 int n = pList ? pList->nExpr : 0; /* Number of arguments */
19449 int no_such_func = 0; /* True if no such function exists */
19450 int wrong_num_args = 0; /* True if wrong number of arguments */
19451 int is_agg = 0; /* True if is an aggregate function */
19452 int auth; /* Authorization to use the function */
19453 int nId; /* Number of characters in function name */
19454 const char *zId; /* The function name. */
19455 FuncDef *pDef; /* Information about the function */
19456 u8 enc = ENC(pParse->db); /* The database encoding */
19457
19458 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
19459 notValid(pParse, pNC, "functions", NC_PartIdx);
19460 zId = pExpr->u.zToken;
19461 nId = sqlite3Strlen30(zId);
19462 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
19463 if( pDef==0 ){
19464 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
19465 if( pDef==0 ){
19466 no_such_func = 1;
19467 }else{
19468 wrong_num_args = 1;
19469 }
19470 }else{
19471 is_agg = pDef->xFunc==0;
19472 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
19473 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
19474 if( n==2 ){
19475 pExpr->iTable = exprProbability(pList->a[1].pExpr);
19476 if( pExpr->iTable<0 ){
19477 sqlite3ErrorMsg(pParse,
19478 "second argument to likelihood() must be a "
19479 "constant between 0.0 and 1.0");
19480 pNC->nErr++;
19481 }
19482 }else{
19483 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
19484 ** equivalent to likelihood(X, 0.0625).
19485 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
19486 ** short-hand for likelihood(X,0.0625).
19487 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
19488 ** for likelihood(X,0.9375).
19489 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
19490 ** to likelihood(X,0.9375). */
19491 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
19492 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
19493 }
19494 }
19495 #ifndef SQLITE_OMIT_AUTHORIZATION
19496 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
19497 if( auth!=SQLITE_OK ){
19498 if( auth==SQLITE_DENY ){
19499 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
19500 pDef->zName);
19501 pNC->nErr++;
19502 }
19503 pExpr->op = TK_NULL;
19504 return WRC_Prune;
19505 }
19506 #endif
19507 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
19508 /* For the purposes of the EP_ConstFunc flag, date and time
19509 ** functions and other functions that change slowly are considered
19510 ** constant because they are constant for the duration of one query */
19511 ExprSetProperty(pExpr,EP_ConstFunc);
19512 }
19513 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
19514 /* Date/time functions that use 'now', and other functions like
19515 ** sqlite_version() that might change over time cannot be used
19516 ** in an index. */
19517 notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr);
19518 }
19519 }
19520 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
19521 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
19522 pNC->nErr++;
19523 is_agg = 0;
19524 }else if( no_such_func && pParse->db->init.busy==0 ){
19525 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
19526 pNC->nErr++;
19527 }else if( wrong_num_args ){
19528 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
19529 nId, zId);
19530 pNC->nErr++;
19531 }
19532 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
19533 sqlite3WalkExprList(pWalker, pList);
19534 if( is_agg ){
19535 NameContext *pNC2 = pNC;
19536 pExpr->op = TK_AGG_FUNCTION;
19537 pExpr->op2 = 0;
19538 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
19539 pExpr->op2++;
19540 pNC2 = pNC2->pNext;
19541 }
19542 assert( pDef!=0 );
19543 if( pNC2 ){
19544 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
19545 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
19546 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
19547
19548 }
19549 pNC->ncFlags |= NC_AllowAgg;
19550 }
19551 /* FIX ME: Compute pExpr->affinity based on the expected return
19552 ** type of the function
19553 */
19554 return WRC_Prune;
19555 }
19556 #ifndef SQLITE_OMIT_SUBQUERY
19557 case TK_SELECT:
19558 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
19559 #endif
19560 case TK_IN: {
19561 testcase( pExpr->op==TK_IN );
19562 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
19563 int nRef = pNC->nRef;
19564 notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
19565 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
19566 assert( pNC->nRef>=nRef );
19567 if( nRef!=pNC->nRef ){
19568 ExprSetProperty(pExpr, EP_VarSelect);
19569 }
19570 }
19571 break;
19572 }
19573 case TK_VARIABLE: {
19574 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
19575 break;
19576 }
19577 }
19578 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
19579 }
19580
19581 /*
19582 ** pEList is a list of expressions which are really the result set of the
19583 ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
19584 ** This routine checks to see if pE is a simple identifier which corresponds
19585 ** to the AS-name of one of the terms of the expression list. If it is,
19586 ** this routine return an integer between 1 and N where N is the number of
19587 ** elements in pEList, corresponding to the matching entry. If there is
19588 ** no match, or if pE is not a simple identifier, then this routine
19589 ** return 0.
19590 **
19591 ** pEList has been resolved. pE has not.
19592 */
19593 static int resolveAsName(
19594 Parse *pParse, /* Parsing context for error messages */
19595 ExprList *pEList, /* List of expressions to scan */
19596 Expr *pE /* Expression we are trying to match */
19597 ){
19598 int i; /* Loop counter */
19599
19600 UNUSED_PARAMETER(pParse);
19601
19602 if( pE->op==TK_ID ){
19603 char *zCol = pE->u.zToken;
19604 for(i=0; i<pEList->nExpr; i++){
19605 char *zAs = pEList->a[i].zName;
19606 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
19607 return i+1;
19608 }
19609 }
19610 }
19611 return 0;
19612 }
19613
19614 /*
19615 ** pE is a pointer to an expression which is a single term in the
19616 ** ORDER BY of a compound SELECT. The expression has not been
19617 ** name resolved.
19618 **
19619 ** At the point this routine is called, we already know that the
19620 ** ORDER BY term is not an integer index into the result set. That
19621 ** case is handled by the calling routine.
19622 **
19623 ** Attempt to match pE against result set columns in the left-most
19624 ** SELECT statement. Return the index i of the matching column,
19625 ** as an indication to the caller that it should sort by the i-th column.
19626 ** The left-most column is 1. In other words, the value returned is the
19627 ** same integer value that would be used in the SQL statement to indicate
19628 ** the column.
19629 **
19630 ** If there is no match, return 0. Return -1 if an error occurs.
19631 */
19632 static int resolveOrderByTermToExprList(
19633 Parse *pParse, /* Parsing context for error messages */
19634 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
19635 Expr *pE /* The specific ORDER BY term */
19636 ){
19637 int i; /* Loop counter */
19638 ExprList *pEList; /* The columns of the result set */
19639 NameContext nc; /* Name context for resolving pE */
19640 sqlite3 *db; /* Database connection */
19641 int rc; /* Return code from subprocedures */
19642 u8 savedSuppErr; /* Saved value of db->suppressErr */
19643
19644 assert( sqlite3ExprIsInteger(pE, &i)==0 );
19645 pEList = pSelect->pEList;
19646
19647 /* Resolve all names in the ORDER BY term expression
19648 */
19649 memset(&nc, 0, sizeof(nc));
19650 nc.pParse = pParse;
19651 nc.pSrcList = pSelect->pSrc;
19652 nc.pEList = pEList;
19653 nc.ncFlags = NC_AllowAgg;
19654 nc.nErr = 0;
19655 db = pParse->db;
19656 savedSuppErr = db->suppressErr;
19657 db->suppressErr = 1;
19658 rc = sqlite3ResolveExprNames(&nc, pE);
19659 db->suppressErr = savedSuppErr;
19660 if( rc ) return 0;
19661
19662 /* Try to match the ORDER BY expression against an expression
19663 ** in the result set. Return an 1-based index of the matching
19664 ** result-set entry.
19665 */
19666 for(i=0; i<pEList->nExpr; i++){
19667 if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
19668 return i+1;
19669 }
19670 }
19671
19672 /* If no match, return 0. */
19673 return 0;
19674 }
19675
19676 /*
19677 ** Generate an ORDER BY or GROUP BY term out-of-range error.
19678 */
19679 static void resolveOutOfRangeError(
19680 Parse *pParse, /* The error context into which to write the error */
19681 const char *zType, /* "ORDER" or "GROUP" */
19682 int i, /* The index (1-based) of the term out of range */
19683 int mx /* Largest permissible value of i */
19684 ){
19685 sqlite3ErrorMsg(pParse,
19686 "%r %s BY term out of range - should be "
19687 "between 1 and %d", i, zType, mx);
19688 }
19689
19690 /*
19691 ** Analyze the ORDER BY clause in a compound SELECT statement. Modify
19692 ** each term of the ORDER BY clause is a constant integer between 1
19693 ** and N where N is the number of columns in the compound SELECT.
19694 **
19695 ** ORDER BY terms that are already an integer between 1 and N are
19696 ** unmodified. ORDER BY terms that are integers outside the range of
19697 ** 1 through N generate an error. ORDER BY terms that are expressions
19698 ** are matched against result set expressions of compound SELECT
19699 ** beginning with the left-most SELECT and working toward the right.
19700 ** At the first match, the ORDER BY expression is transformed into
19701 ** the integer column number.
19702 **
19703 ** Return the number of errors seen.
19704 */
19705 static int resolveCompoundOrderBy(
19706 Parse *pParse, /* Parsing context. Leave error messages here */
19707 Select *pSelect /* The SELECT statement containing the ORDER BY */
19708 ){
19709 int i;
19710 ExprList *pOrderBy;
19711 ExprList *pEList;
19712 sqlite3 *db;
19713 int moreToDo = 1;
19714
19715 pOrderBy = pSelect->pOrderBy;
19716 if( pOrderBy==0 ) return 0;
19717 db = pParse->db;
19718 #if SQLITE_MAX_COLUMN
19719 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
19720 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
19721 return 1;
19722 }
19723 #endif
19724 for(i=0; i<pOrderBy->nExpr; i++){
19725 pOrderBy->a[i].done = 0;
19726 }
19727 pSelect->pNext = 0;
19728 while( pSelect->pPrior ){
19729 pSelect->pPrior->pNext = pSelect;
19730 pSelect = pSelect->pPrior;
19731 }
19732 while( pSelect && moreToDo ){
19733 struct ExprList_item *pItem;
19734 moreToDo = 0;
19735 pEList = pSelect->pEList;
19736 assert( pEList!=0 );
19737 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
19738 int iCol = -1;
19739 Expr *pE, *pDup;
19740 if( pItem->done ) continue;
19741 pE = sqlite3ExprSkipCollate(pItem->pExpr);
19742 if( sqlite3ExprIsInteger(pE, &iCol) ){
19743 if( iCol<=0 || iCol>pEList->nExpr ){
19744 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
19745 return 1;
19746 }
19747 }else{
19748 iCol = resolveAsName(pParse, pEList, pE);
19749 if( iCol==0 ){
19750 pDup = sqlite3ExprDup(db, pE, 0);
19751 if( !db->mallocFailed ){
19752 assert(pDup);
19753 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
19754 }
19755 sqlite3ExprDelete(db, pDup);
19756 }
19757 }
19758 if( iCol>0 ){
19759 /* Convert the ORDER BY term into an integer column number iCol,
19760 ** taking care to preserve the COLLATE clause if it exists */
19761 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
19762 if( pNew==0 ) return 1;
19763 pNew->flags |= EP_IntValue;
19764 pNew->u.iValue = iCol;
19765 if( pItem->pExpr==pE ){
19766 pItem->pExpr = pNew;
19767 }else{
19768 Expr *pParent = pItem->pExpr;
19769 assert( pParent->op==TK_COLLATE );
19770 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
19771 assert( pParent->pLeft==pE );
19772 pParent->pLeft = pNew;
19773 }
19774 sqlite3ExprDelete(db, pE);
19775 pItem->u.x.iOrderByCol = (u16)iCol;
19776 pItem->done = 1;
19777 }else{
19778 moreToDo = 1;
19779 }
19780 }
19781 pSelect = pSelect->pNext;
19782 }
19783 for(i=0; i<pOrderBy->nExpr; i++){
19784 if( pOrderBy->a[i].done==0 ){
19785 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
19786 "column in the result set", i+1);
19787 return 1;
19788 }
19789 }
19790 return 0;
19791 }
19792
19793 /*
19794 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
19795 ** the SELECT statement pSelect. If any term is reference to a
19796 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
19797 ** field) then convert that term into a copy of the corresponding result set
19798 ** column.
19799 **
19800 ** If any errors are detected, add an error message to pParse and
19801 ** return non-zero. Return zero if no errors are seen.
19802 */
19803 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
19804 Parse *pParse, /* Parsing context. Leave error messages here */
19805 Select *pSelect, /* The SELECT statement containing the clause */
19806 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
19807 const char *zType /* "ORDER" or "GROUP" */
19808 ){
19809 int i;
19810 sqlite3 *db = pParse->db;
19811 ExprList *pEList;
19812 struct ExprList_item *pItem;
19813
19814 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
19815 #if SQLITE_MAX_COLUMN
19816 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
19817 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
19818 return 1;
19819 }
19820 #endif
19821 pEList = pSelect->pEList;
19822 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
19823 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
19824 if( pItem->u.x.iOrderByCol ){
19825 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
19826 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
19827 return 1;
19828 }
19829 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
19830 zType,0);
19831 }
19832 }
19833 return 0;
19834 }
19835
19836 /*
19837 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
19838 ** The Name context of the SELECT statement is pNC. zType is either
19839 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
19840 **
19841 ** This routine resolves each term of the clause into an expression.
19842 ** If the order-by term is an integer I between 1 and N (where N is the
19843 ** number of columns in the result set of the SELECT) then the expression
19844 ** in the resolution is a copy of the I-th result-set expression. If
19845 ** the order-by term is an identifier that corresponds to the AS-name of
19846 ** a result-set expression, then the term resolves to a copy of the
19847 ** result-set expression. Otherwise, the expression is resolved in
19848 ** the usual way - using sqlite3ResolveExprNames().
19849 **
19850 ** This routine returns the number of errors. If errors occur, then
19851 ** an appropriate error message might be left in pParse. (OOM errors
19852 ** excepted.)
19853 */
19854 static int resolveOrderGroupBy(
19855 NameContext *pNC, /* The name context of the SELECT statement */
19856 Select *pSelect, /* The SELECT statement holding pOrderBy */
19857 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
19858 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
19859 ){
19860 int i, j; /* Loop counters */
19861 int iCol; /* Column number */
19862 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
19863 Parse *pParse; /* Parsing context */
19864 int nResult; /* Number of terms in the result set */
19865
19866 if( pOrderBy==0 ) return 0;
19867 nResult = pSelect->pEList->nExpr;
19868 pParse = pNC->pParse;
19869 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
19870 Expr *pE = pItem->pExpr;
19871 Expr *pE2 = sqlite3ExprSkipCollate(pE);
19872 if( zType[0]!='G' ){
19873 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
19874 if( iCol>0 ){
19875 /* If an AS-name match is found, mark this ORDER BY column as being
19876 ** a copy of the iCol-th result-set column. The subsequent call to
19877 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
19878 ** copy of the iCol-th result-set expression. */
19879 pItem->u.x.iOrderByCol = (u16)iCol;
19880 continue;
19881 }
19882 }
19883 if( sqlite3ExprIsInteger(pE2, &iCol) ){
19884 /* The ORDER BY term is an integer constant. Again, set the column
19885 ** number so that sqlite3ResolveOrderGroupBy() will convert the
19886 ** order-by term to a copy of the result-set expression */
19887 if( iCol<1 || iCol>0xffff ){
19888 resolveOutOfRangeError(pParse, zType, i+1, nResult);
19889 return 1;
19890 }
19891 pItem->u.x.iOrderByCol = (u16)iCol;
19892 continue;
19893 }
19894
19895 /* Otherwise, treat the ORDER BY term as an ordinary expression */
19896 pItem->u.x.iOrderByCol = 0;
19897 if( sqlite3ResolveExprNames(pNC, pE) ){
19898 return 1;
19899 }
19900 for(j=0; j<pSelect->pEList->nExpr; j++){
19901 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
19902 pItem->u.x.iOrderByCol = j+1;
19903 }
19904 }
19905 }
19906 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
19907 }
19908
19909 /*
19910 ** Resolve names in the SELECT statement p and all of its descendants.
19911 */
19912 static int resolveSelectStep(Walker *pWalker, Select *p){
19913 NameContext *pOuterNC; /* Context that contains this SELECT */
19914 NameContext sNC; /* Name context of this SELECT */
19915 int isCompound; /* True if p is a compound select */
19916 int nCompound; /* Number of compound terms processed so far */
19917 Parse *pParse; /* Parsing context */
19918 int i; /* Loop counter */
19919 ExprList *pGroupBy; /* The GROUP BY clause */
19920 Select *pLeftmost; /* Left-most of SELECT of a compound */
19921 sqlite3 *db; /* Database connection */
19922
19923
19924 assert( p!=0 );
19925 if( p->selFlags & SF_Resolved ){
19926 return WRC_Prune;
19927 }
19928 pOuterNC = pWalker->u.pNC;
19929 pParse = pWalker->pParse;
19930 db = pParse->db;
19931
19932 /* Normally sqlite3SelectExpand() will be called first and will have
19933 ** already expanded this SELECT. However, if this is a subquery within
19934 ** an expression, sqlite3ResolveExprNames() will be called without a
19935 ** prior call to sqlite3SelectExpand(). When that happens, let
19936 ** sqlite3SelectPrep() do all of the processing for this SELECT.
19937 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
19938 ** this routine in the correct order.
19939 */
19940 if( (p->selFlags & SF_Expanded)==0 ){
19941 sqlite3SelectPrep(pParse, p, pOuterNC);
19942 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
19943 }
19944
19945 isCompound = p->pPrior!=0;
19946 nCompound = 0;
19947 pLeftmost = p;
19948 while( p ){
19949 assert( (p->selFlags & SF_Expanded)!=0 );
19950 assert( (p->selFlags & SF_Resolved)==0 );
19951 p->selFlags |= SF_Resolved;
19952
19953 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
19954 ** are not allowed to refer to any names, so pass an empty NameContext.
19955 */
19956 memset(&sNC, 0, sizeof(sNC));
19957 sNC.pParse = pParse;
19958 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
19959 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
19960 return WRC_Abort;
19961 }
19962
19963 /* If the SF_Converted flags is set, then this Select object was
19964 ** was created by the convertCompoundSelectToSubquery() function.
19965 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
19966 ** as if it were part of the sub-query, not the parent. This block
19967 ** moves the pOrderBy down to the sub-query. It will be moved back
19968 ** after the names have been resolved. */
19969 if( p->selFlags & SF_Converted ){
19970 Select *pSub = p->pSrc->a[0].pSelect;
19971 assert( p->pSrc->nSrc==1 && p->pOrderBy );
19972 assert( pSub->pPrior && pSub->pOrderBy==0 );
19973 pSub->pOrderBy = p->pOrderBy;
19974 p->pOrderBy = 0;
19975 }
19976
19977 /* Recursively resolve names in all subqueries
19978 */
19979 for(i=0; i<p->pSrc->nSrc; i++){
19980 struct SrcList_item *pItem = &p->pSrc->a[i];
19981 if( pItem->pSelect ){
19982 NameContext *pNC; /* Used to iterate name contexts */
19983 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
19984 const char *zSavedContext = pParse->zAuthContext;
19985
19986 /* Count the total number of references to pOuterNC and all of its
19987 ** parent contexts. After resolving references to expressions in
19988 ** pItem->pSelect, check if this value has changed. If so, then
19989 ** SELECT statement pItem->pSelect must be correlated. Set the
19990 ** pItem->fg.isCorrelated flag if this is the case. */
19991 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
19992
19993 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
19994 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
19995 pParse->zAuthContext = zSavedContext;
19996 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
19997
19998 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
19999 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
20000 pItem->fg.isCorrelated = (nRef!=0);
20001 }
20002 }
20003
20004 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
20005 ** resolve the result-set expression list.
20006 */
20007 sNC.ncFlags = NC_AllowAgg;
20008 sNC.pSrcList = p->pSrc;
20009 sNC.pNext = pOuterNC;
20010
20011 /* Resolve names in the result set. */
20012 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
20013
20014 /* If there are no aggregate functions in the result-set, and no GROUP BY
20015 ** expression, do not allow aggregates in any of the other expressions.
20016 */
20017 assert( (p->selFlags & SF_Aggregate)==0 );
20018 pGroupBy = p->pGroupBy;
20019 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
20020 assert( NC_MinMaxAgg==SF_MinMaxAgg );
20021 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
20022 }else{
20023 sNC.ncFlags &= ~NC_AllowAgg;
20024 }
20025
20026 /* If a HAVING clause is present, then there must be a GROUP BY clause.
20027 */
20028 if( p->pHaving && !pGroupBy ){
20029 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
20030 return WRC_Abort;
20031 }
20032
20033 /* Add the output column list to the name-context before parsing the
20034 ** other expressions in the SELECT statement. This is so that
20035 ** expressions in the WHERE clause (etc.) can refer to expressions by
20036 ** aliases in the result set.
20037 **
20038 ** Minor point: If this is the case, then the expression will be
20039 ** re-evaluated for each reference to it.
20040 */
20041 sNC.pEList = p->pEList;
20042 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
20043 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
20044
20045 /* Resolve names in table-valued-function arguments */
20046 for(i=0; i<p->pSrc->nSrc; i++){
20047 struct SrcList_item *pItem = &p->pSrc->a[i];
20048 if( pItem->fg.isTabFunc
20049 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
20050 ){
20051 return WRC_Abort;
20052 }
20053 }
20054
20055 /* The ORDER BY and GROUP BY clauses may not refer to terms in
20056 ** outer queries
20057 */
20058 sNC.pNext = 0;
20059 sNC.ncFlags |= NC_AllowAgg;
20060
20061 /* If this is a converted compound query, move the ORDER BY clause from
20062 ** the sub-query back to the parent query. At this point each term
20063 ** within the ORDER BY clause has been transformed to an integer value.
20064 ** These integers will be replaced by copies of the corresponding result
20065 ** set expressions by the call to resolveOrderGroupBy() below. */
20066 if( p->selFlags & SF_Converted ){
20067 Select *pSub = p->pSrc->a[0].pSelect;
20068 p->pOrderBy = pSub->pOrderBy;
20069 pSub->pOrderBy = 0;
20070 }
20071
20072 /* Process the ORDER BY clause for singleton SELECT statements.
20073 ** The ORDER BY clause for compounds SELECT statements is handled
20074 ** below, after all of the result-sets for all of the elements of
20075 ** the compound have been resolved.
20076 **
20077 ** If there is an ORDER BY clause on a term of a compound-select other
20078 ** than the right-most term, then that is a syntax error. But the error
20079 ** is not detected until much later, and so we need to go ahead and
20080 ** resolve those symbols on the incorrect ORDER BY for consistency.
20081 */
20082 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
20083 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
20084 ){
20085 return WRC_Abort;
20086 }
20087 if( db->mallocFailed ){
20088 return WRC_Abort;
20089 }
20090
20091 /* Resolve the GROUP BY clause. At the same time, make sure
20092 ** the GROUP BY clause does not contain aggregate functions.
20093 */
20094 if( pGroupBy ){
20095 struct ExprList_item *pItem;
20096
20097 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
20098 return WRC_Abort;
20099 }
20100 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
20101 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
20102 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
20103 "the GROUP BY clause");
20104 return WRC_Abort;
20105 }
20106 }
20107 }
20108
20109 /* If this is part of a compound SELECT, check that it has the right
20110 ** number of expressions in the select list. */
20111 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
20112 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
20113 return WRC_Abort;
20114 }
20115
20116 /* Advance to the next term of the compound
20117 */
20118 p = p->pPrior;
20119 nCompound++;
20120 }
20121
20122 /* Resolve the ORDER BY on a compound SELECT after all terms of
20123 ** the compound have been resolved.
20124 */
20125 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
20126 return WRC_Abort;
20127 }
20128
20129 return WRC_Prune;
20130 }
20131
20132 /*
20133 ** This routine walks an expression tree and resolves references to
20134 ** table columns and result-set columns. At the same time, do error
20135 ** checking on function usage and set a flag if any aggregate functions
20136 ** are seen.
20137 **
20138 ** To resolve table columns references we look for nodes (or subtrees) of the
20139 ** form X.Y.Z or Y.Z or just Z where
20140 **
20141 ** X: The name of a database. Ex: "main" or "temp" or
20142 ** the symbolic name assigned to an ATTACH-ed database.
20143 **
20144 ** Y: The name of a table in a FROM clause. Or in a trigger
20145 ** one of the special names "old" or "new".
20146 **
20147 ** Z: The name of a column in table Y.
20148 **
20149 ** The node at the root of the subtree is modified as follows:
20150 **
20151 ** Expr.op Changed to TK_COLUMN
20152 ** Expr.pTab Points to the Table object for X.Y
20153 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
20154 ** Expr.iTable The VDBE cursor number for X.Y
20155 **
20156 **
20157 ** To resolve result-set references, look for expression nodes of the
20158 ** form Z (with no X and Y prefix) where the Z matches the right-hand
20159 ** size of an AS clause in the result-set of a SELECT. The Z expression
20160 ** is replaced by a copy of the left-hand side of the result-set expression.
20161 ** Table-name and function resolution occurs on the substituted expression
20162 ** tree. For example, in:
20163 **
20164 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
20165 **
20166 ** The "x" term of the order by is replaced by "a+b" to render:
20167 **
20168 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
20169 **
20170 ** Function calls are checked to make sure that the function is
20171 ** defined and that the correct number of arguments are specified.
20172 ** If the function is an aggregate function, then the NC_HasAgg flag is
20173 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
20174 ** If an expression contains aggregate functions then the EP_Agg
20175 ** property on the expression is set.
20176 **
20177 ** An error message is left in pParse if anything is amiss. The number
20178 ** if errors is returned.
20179 */
20180 SQLITE_PRIVATE int sqlite3ResolveExprNames(
20181 NameContext *pNC, /* Namespace to resolve expressions in. */
20182 Expr *pExpr /* The expression to be analyzed. */
20183 ){
20184 u16 savedHasAgg;
20185 Walker w;
20186
20187 if( pExpr==0 ) return 0;
20188 #if SQLITE_MAX_EXPR_DEPTH>0
20189 {
20190 Parse *pParse = pNC->pParse;
20191 if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
20192 return 1;
20193 }
20194 pParse->nHeight += pExpr->nHeight;
20195 }
20196 #endif
20197 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
20198 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
20199 memset(&w, 0, sizeof(w));
20200 w.xExprCallback = resolveExprStep;
20201 w.xSelectCallback = resolveSelectStep;
20202 w.pParse = pNC->pParse;
20203 w.u.pNC = pNC;
20204 sqlite3WalkExpr(&w, pExpr);
20205 #if SQLITE_MAX_EXPR_DEPTH>0
20206 pNC->pParse->nHeight -= pExpr->nHeight;
20207 #endif
20208 if( pNC->nErr>0 || w.pParse->nErr>0 ){
20209 ExprSetProperty(pExpr, EP_Error);
20210 }
20211 if( pNC->ncFlags & NC_HasAgg ){
20212 ExprSetProperty(pExpr, EP_Agg);
20213 }
20214 pNC->ncFlags |= savedHasAgg;
20215 return ExprHasProperty(pExpr, EP_Error);
20216 }
20217
20218 /*
20219 ** Resolve all names for all expression in an expression list. This is
20220 ** just like sqlite3ResolveExprNames() except that it works for an expression
20221 ** list rather than a single expression.
20222 */
20223 SQLITE_PRIVATE int sqlite3ResolveExprListNames(
20224 NameContext *pNC, /* Namespace to resolve expressions in. */
20225 ExprList *pList /* The expression list to be analyzed. */
20226 ){
20227 int i;
20228 if( pList ){
20229 for(i=0; i<pList->nExpr; i++){
20230 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
20231 }
20232 }
20233 return WRC_Continue;
20234 }
20235
20236 /*
20237 ** Resolve all names in all expressions of a SELECT and in all
20238 ** decendents of the SELECT, including compounds off of p->pPrior,
20239 ** subqueries in expressions, and subqueries used as FROM clause
20240 ** terms.
20241 **
20242 ** See sqlite3ResolveExprNames() for a description of the kinds of
20243 ** transformations that occur.
20244 **
20245 ** All SELECT statements should have been expanded using
20246 ** sqlite3SelectExpand() prior to invoking this routine.
20247 */
20248 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
20249 Parse *pParse, /* The parser context */
20250 Select *p, /* The SELECT statement being coded. */
20251 NameContext *pOuterNC /* Name context for parent SELECT statement */
20252 ){
20253 Walker w;
20254
20255 assert( p!=0 );
20256 memset(&w, 0, sizeof(w));
20257 w.xExprCallback = resolveExprStep;
20258 w.xSelectCallback = resolveSelectStep;
20259 w.pParse = pParse;
20260 w.u.pNC = pOuterNC;
20261 sqlite3WalkSelect(&w, p);
20262 }
20263
20264 /*
20265 ** Resolve names in expressions that can only reference a single table:
20266 **
20267 ** * CHECK constraints
20268 ** * WHERE clauses on partial indices
20269 **
20270 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
20271 ** is set to -1 and the Expr.iColumn value is set to the column number.
20272 **
20273 ** Any errors cause an error message to be set in pParse.
20274 */
20275 SQLITE_PRIVATE void sqlite3ResolveSelfReference(
20276 Parse *pParse, /* Parsing context */
20277 Table *pTab, /* The table being referenced */
20278 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
20279 Expr *pExpr, /* Expression to resolve. May be NULL. */
20280 ExprList *pList /* Expression list to resolve. May be NUL. */
20281 ){
20282 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
20283 NameContext sNC; /* Name context for pParse->pNewTable */
20284
20285 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
20286 memset(&sNC, 0, sizeof(sNC));
20287 memset(&sSrc, 0, sizeof(sSrc));
20288 sSrc.nSrc = 1;
20289 sSrc.a[0].zName = pTab->zName;
20290 sSrc.a[0].pTab = pTab;
20291 sSrc.a[0].iCursor = -1;
20292 sNC.pParse = pParse;
20293 sNC.pSrcList = &sSrc;
20294 sNC.ncFlags = type;
20295 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
20296 if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
20297 }
20298
20299 /************** End of resolve.c *********************************************/
20300 /************** Begin file expr.c ********************************************/
20301 /*
20302 ** 2001 September 15
20303 **
20304 ** The author disclaims copyright to this source code. In place of
20305 ** a legal notice, here is a blessing:
20306 **
20307 ** May you do good and not evil.
20308 ** May you find forgiveness for yourself and forgive others.
20309 ** May you share freely, never taking more than you give.
20310 **
20311 *************************************************************************
20312 ** This file contains routines used for analyzing expressions and
20313 ** for generating VDBE code that evaluates expressions in SQLite.
20314 */
20315 /* #include "sqliteInt.h" */
20316
20317 /*
20318 ** Return the 'affinity' of the expression pExpr if any.
20319 **
20320 ** If pExpr is a column, a reference to a column via an 'AS' alias,
20321 ** or a sub-select with a column as the return value, then the
20322 ** affinity of that column is returned. Otherwise, 0x00 is returned,
20323 ** indicating no affinity for the expression.
20324 **
20325 ** i.e. the WHERE clause expressions in the following statements all
20326 ** have an affinity:
20327 **
20328 ** CREATE TABLE t1(a);
20329 ** SELECT * FROM t1 WHERE a;
20330 ** SELECT a AS b FROM t1 WHERE b;
20331 ** SELECT * FROM t1 WHERE (select a from t1);
20332 */
20333 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
20334 int op;
20335 pExpr = sqlite3ExprSkipCollate(pExpr);
20336 if( pExpr->flags & EP_Generic ) return 0;
20337 op = pExpr->op;
20338 if( op==TK_SELECT ){
20339 assert( pExpr->flags&EP_xIsSelect );
20340 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
20341 }
20342 #ifndef SQLITE_OMIT_CAST
20343 if( op==TK_CAST ){
20344 assert( !ExprHasProperty(pExpr, EP_IntValue) );
20345 return sqlite3AffinityType(pExpr->u.zToken, 0);
20346 }
20347 #endif
20348 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
20349 && pExpr->pTab!=0
20350 ){
20351 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
20352 ** a TK_COLUMN but was previously evaluated and cached in a register */
20353 int j = pExpr->iColumn;
20354 if( j<0 ) return SQLITE_AFF_INTEGER;
20355 assert( pExpr->pTab && j<pExpr->pTab->nCol );
20356 return pExpr->pTab->aCol[j].affinity;
20357 }
20358 return pExpr->affinity;
20359 }
20360
20361 /*
20362 ** Set the collating sequence for expression pExpr to be the collating
20363 ** sequence named by pToken. Return a pointer to a new Expr node that
20364 ** implements the COLLATE operator.
20365 **
20366 ** If a memory allocation error occurs, that fact is recorded in pParse->db
20367 ** and the pExpr parameter is returned unchanged.
20368 */
20369 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
20370 Parse *pParse, /* Parsing context */
20371 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
20372 const Token *pCollName, /* Name of collating sequence */
20373 int dequote /* True to dequote pCollName */
20374 ){
20375 if( pCollName->n>0 ){
20376 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
20377 if( pNew ){
20378 pNew->pLeft = pExpr;
20379 pNew->flags |= EP_Collate|EP_Skip;
20380 pExpr = pNew;
20381 }
20382 }
20383 return pExpr;
20384 }
20385 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, con st char *zC){
20386 Token s;
20387 assert( zC!=0 );
20388 s.z = zC;
20389 s.n = sqlite3Strlen30(s.z);
20390 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
20391 }
20392
20393 /*
20394 ** Skip over any TK_COLLATE operators and any unlikely()
20395 ** or likelihood() function at the root of an expression.
20396 */
20397 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
20398 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
20399 if( ExprHasProperty(pExpr, EP_Unlikely) ){
20400 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
20401 assert( pExpr->x.pList->nExpr>0 );
20402 assert( pExpr->op==TK_FUNCTION );
20403 pExpr = pExpr->x.pList->a[0].pExpr;
20404 }else{
20405 assert( pExpr->op==TK_COLLATE );
20406 pExpr = pExpr->pLeft;
20407 }
20408 }
20409 return pExpr;
20410 }
20411
20412 /*
20413 ** Return the collation sequence for the expression pExpr. If
20414 ** there is no defined collating sequence, return NULL.
20415 **
20416 ** The collating sequence might be determined by a COLLATE operator
20417 ** or by the presence of a column with a defined collating sequence.
20418 ** COLLATE operators take first precedence. Left operands take
20419 ** precedence over right operands.
20420 */
20421 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
20422 sqlite3 *db = pParse->db;
20423 CollSeq *pColl = 0;
20424 Expr *p = pExpr;
20425 while( p ){
20426 int op = p->op;
20427 if( p->flags & EP_Generic ) break;
20428 if( op==TK_CAST || op==TK_UPLUS ){
20429 p = p->pLeft;
20430 continue;
20431 }
20432 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
20433 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
20434 break;
20435 }
20436 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
20437 || op==TK_REGISTER || op==TK_TRIGGER)
20438 && p->pTab!=0
20439 ){
20440 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
20441 ** a TK_COLUMN but was previously evaluated and cached in a register */
20442 int j = p->iColumn;
20443 if( j>=0 ){
20444 const char *zColl = p->pTab->aCol[j].zColl;
20445 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
20446 }
20447 break;
20448 }
20449 if( p->flags & EP_Collate ){
20450 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
20451 p = p->pLeft;
20452 }else{
20453 Expr *pNext = p->pRight;
20454 /* The Expr.x union is never used at the same time as Expr.pRight */
20455 assert( p->x.pList==0 || p->pRight==0 );
20456 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
20457 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
20458 ** least one EP_Collate. Thus the following two ALWAYS. */
20459 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
20460 int i;
20461 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
20462 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
20463 pNext = p->x.pList->a[i].pExpr;
20464 break;
20465 }
20466 }
20467 }
20468 p = pNext;
20469 }
20470 }else{
20471 break;
20472 }
20473 }
20474 if( sqlite3CheckCollSeq(pParse, pColl) ){
20475 pColl = 0;
20476 }
20477 return pColl;
20478 }
20479
20480 /*
20481 ** pExpr is an operand of a comparison operator. aff2 is the
20482 ** type affinity of the other operand. This routine returns the
20483 ** type affinity that should be used for the comparison operator.
20484 */
20485 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
20486 char aff1 = sqlite3ExprAffinity(pExpr);
20487 if( aff1 && aff2 ){
20488 /* Both sides of the comparison are columns. If one has numeric
20489 ** affinity, use that. Otherwise use no affinity.
20490 */
20491 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
20492 return SQLITE_AFF_NUMERIC;
20493 }else{
20494 return SQLITE_AFF_BLOB;
20495 }
20496 }else if( !aff1 && !aff2 ){
20497 /* Neither side of the comparison is a column. Compare the
20498 ** results directly.
20499 */
20500 return SQLITE_AFF_BLOB;
20501 }else{
20502 /* One side is a column, the other is not. Use the columns affinity. */
20503 assert( aff1==0 || aff2==0 );
20504 return (aff1 + aff2);
20505 }
20506 }
20507
20508 /*
20509 ** pExpr is a comparison operator. Return the type affinity that should
20510 ** be applied to both operands prior to doing the comparison.
20511 */
20512 static char comparisonAffinity(Expr *pExpr){
20513 char aff;
20514 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
20515 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
20516 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
20517 assert( pExpr->pLeft );
20518 aff = sqlite3ExprAffinity(pExpr->pLeft);
20519 if( pExpr->pRight ){
20520 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
20521 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
20522 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
20523 }else if( !aff ){
20524 aff = SQLITE_AFF_BLOB;
20525 }
20526 return aff;
20527 }
20528
20529 /*
20530 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
20531 ** idx_affinity is the affinity of an indexed column. Return true
20532 ** if the index with affinity idx_affinity may be used to implement
20533 ** the comparison in pExpr.
20534 */
20535 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
20536 char aff = comparisonAffinity(pExpr);
20537 switch( aff ){
20538 case SQLITE_AFF_BLOB:
20539 return 1;
20540 case SQLITE_AFF_TEXT:
20541 return idx_affinity==SQLITE_AFF_TEXT;
20542 default:
20543 return sqlite3IsNumericAffinity(idx_affinity);
20544 }
20545 }
20546
20547 /*
20548 ** Return the P5 value that should be used for a binary comparison
20549 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
20550 */
20551 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
20552 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
20553 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
20554 return aff;
20555 }
20556
20557 /*
20558 ** Return a pointer to the collation sequence that should be used by
20559 ** a binary comparison operator comparing pLeft and pRight.
20560 **
20561 ** If the left hand expression has a collating sequence type, then it is
20562 ** used. Otherwise the collation sequence for the right hand expression
20563 ** is used, or the default (BINARY) if neither expression has a collating
20564 ** type.
20565 **
20566 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
20567 ** it is not considered.
20568 */
20569 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
20570 Parse *pParse,
20571 Expr *pLeft,
20572 Expr *pRight
20573 ){
20574 CollSeq *pColl;
20575 assert( pLeft );
20576 if( pLeft->flags & EP_Collate ){
20577 pColl = sqlite3ExprCollSeq(pParse, pLeft);
20578 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
20579 pColl = sqlite3ExprCollSeq(pParse, pRight);
20580 }else{
20581 pColl = sqlite3ExprCollSeq(pParse, pLeft);
20582 if( !pColl ){
20583 pColl = sqlite3ExprCollSeq(pParse, pRight);
20584 }
20585 }
20586 return pColl;
20587 }
20588
20589 /*
20590 ** Generate code for a comparison operator.
20591 */
20592 static int codeCompare(
20593 Parse *pParse, /* The parsing (and code generating) context */
20594 Expr *pLeft, /* The left operand */
20595 Expr *pRight, /* The right operand */
20596 int opcode, /* The comparison opcode */
20597 int in1, int in2, /* Register holding operands */
20598 int dest, /* Jump here if true. */
20599 int jumpIfNull /* If true, jump if either operand is NULL */
20600 ){
20601 int p5;
20602 int addr;
20603 CollSeq *p4;
20604
20605 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
20606 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
20607 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
20608 (void*)p4, P4_COLLSEQ);
20609 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
20610 return addr;
20611 }
20612
20613 #if SQLITE_MAX_EXPR_DEPTH>0
20614 /*
20615 ** Check that argument nHeight is less than or equal to the maximum
20616 ** expression depth allowed. If it is not, leave an error message in
20617 ** pParse.
20618 */
20619 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
20620 int rc = SQLITE_OK;
20621 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
20622 if( nHeight>mxHeight ){
20623 sqlite3ErrorMsg(pParse,
20624 "Expression tree is too large (maximum depth %d)", mxHeight
20625 );
20626 rc = SQLITE_ERROR;
20627 }
20628 return rc;
20629 }
20630
20631 /* The following three functions, heightOfExpr(), heightOfExprList()
20632 ** and heightOfSelect(), are used to determine the maximum height
20633 ** of any expression tree referenced by the structure passed as the
20634 ** first argument.
20635 **
20636 ** If this maximum height is greater than the current value pointed
20637 ** to by pnHeight, the second parameter, then set *pnHeight to that
20638 ** value.
20639 */
20640 static void heightOfExpr(Expr *p, int *pnHeight){
20641 if( p ){
20642 if( p->nHeight>*pnHeight ){
20643 *pnHeight = p->nHeight;
20644 }
20645 }
20646 }
20647 static void heightOfExprList(ExprList *p, int *pnHeight){
20648 if( p ){
20649 int i;
20650 for(i=0; i<p->nExpr; i++){
20651 heightOfExpr(p->a[i].pExpr, pnHeight);
20652 }
20653 }
20654 }
20655 static void heightOfSelect(Select *p, int *pnHeight){
20656 if( p ){
20657 heightOfExpr(p->pWhere, pnHeight);
20658 heightOfExpr(p->pHaving, pnHeight);
20659 heightOfExpr(p->pLimit, pnHeight);
20660 heightOfExpr(p->pOffset, pnHeight);
20661 heightOfExprList(p->pEList, pnHeight);
20662 heightOfExprList(p->pGroupBy, pnHeight);
20663 heightOfExprList(p->pOrderBy, pnHeight);
20664 heightOfSelect(p->pPrior, pnHeight);
20665 }
20666 }
20667
20668 /*
20669 ** Set the Expr.nHeight variable in the structure passed as an
20670 ** argument. An expression with no children, Expr.pList or
20671 ** Expr.pSelect member has a height of 1. Any other expression
20672 ** has a height equal to the maximum height of any other
20673 ** referenced Expr plus one.
20674 **
20675 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
20676 ** if appropriate.
20677 */
20678 static void exprSetHeight(Expr *p){
20679 int nHeight = 0;
20680 heightOfExpr(p->pLeft, &nHeight);
20681 heightOfExpr(p->pRight, &nHeight);
20682 if( ExprHasProperty(p, EP_xIsSelect) ){
20683 heightOfSelect(p->x.pSelect, &nHeight);
20684 }else if( p->x.pList ){
20685 heightOfExprList(p->x.pList, &nHeight);
20686 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
20687 }
20688 p->nHeight = nHeight + 1;
20689 }
20690
20691 /*
20692 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
20693 ** the height is greater than the maximum allowed expression depth,
20694 ** leave an error in pParse.
20695 **
20696 ** Also propagate all EP_Propagate flags from the Expr.x.pList into
20697 ** Expr.flags.
20698 */
20699 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
20700 if( pParse->nErr ) return;
20701 exprSetHeight(p);
20702 sqlite3ExprCheckHeight(pParse, p->nHeight);
20703 }
20704
20705 /*
20706 ** Return the maximum height of any expression tree referenced
20707 ** by the select statement passed as an argument.
20708 */
20709 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
20710 int nHeight = 0;
20711 heightOfSelect(p, &nHeight);
20712 return nHeight;
20713 }
20714 #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
20715 /*
20716 ** Propagate all EP_Propagate flags from the Expr.x.pList into
20717 ** Expr.flags.
20718 */
20719 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
20720 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
20721 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
20722 }
20723 }
20724 #define exprSetHeight(y)
20725 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
20726
20727 /*
20728 ** This routine is the core allocator for Expr nodes.
20729 **
20730 ** Construct a new expression node and return a pointer to it. Memory
20731 ** for this node and for the pToken argument is a single allocation
20732 ** obtained from sqlite3DbMalloc(). The calling function
20733 ** is responsible for making sure the node eventually gets freed.
20734 **
20735 ** If dequote is true, then the token (if it exists) is dequoted.
20736 ** If dequote is false, no dequoting is performed. The deQuote
20737 ** parameter is ignored if pToken is NULL or if the token does not
20738 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
20739 ** then the EP_DblQuoted flag is set on the expression node.
20740 **
20741 ** Special case: If op==TK_INTEGER and pToken points to a string that
20742 ** can be translated into a 32-bit integer, then the token is not
20743 ** stored in u.zToken. Instead, the integer values is written
20744 ** into u.iValue and the EP_IntValue flag is set. No extra storage
20745 ** is allocated to hold the integer text and the dequote flag is ignored.
20746 */
20747 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
20748 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
20749 int op, /* Expression opcode */
20750 const Token *pToken, /* Token argument. Might be NULL */
20751 int dequote /* True to dequote */
20752 ){
20753 Expr *pNew;
20754 int nExtra = 0;
20755 int iValue = 0;
20756
20757 if( pToken ){
20758 if( op!=TK_INTEGER || pToken->z==0
20759 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
20760 nExtra = pToken->n+1;
20761 assert( iValue>=0 );
20762 }
20763 }
20764 pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
20765 if( pNew ){
20766 pNew->op = (u8)op;
20767 pNew->iAgg = -1;
20768 if( pToken ){
20769 if( nExtra==0 ){
20770 pNew->flags |= EP_IntValue;
20771 pNew->u.iValue = iValue;
20772 }else{
20773 int c;
20774 pNew->u.zToken = (char*)&pNew[1];
20775 assert( pToken->z!=0 || pToken->n==0 );
20776 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
20777 pNew->u.zToken[pToken->n] = 0;
20778 if( dequote && nExtra>=3
20779 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
20780 sqlite3Dequote(pNew->u.zToken);
20781 if( c=='"' ) pNew->flags |= EP_DblQuoted;
20782 }
20783 }
20784 }
20785 #if SQLITE_MAX_EXPR_DEPTH>0
20786 pNew->nHeight = 1;
20787 #endif
20788 }
20789 return pNew;
20790 }
20791
20792 /*
20793 ** Allocate a new expression node from a zero-terminated token that has
20794 ** already been dequoted.
20795 */
20796 SQLITE_PRIVATE Expr *sqlite3Expr(
20797 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
20798 int op, /* Expression opcode */
20799 const char *zToken /* Token argument. Might be NULL */
20800 ){
20801 Token x;
20802 x.z = zToken;
20803 x.n = zToken ? sqlite3Strlen30(zToken) : 0;
20804 return sqlite3ExprAlloc(db, op, &x, 0);
20805 }
20806
20807 /*
20808 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
20809 **
20810 ** If pRoot==NULL that means that a memory allocation error has occurred.
20811 ** In that case, delete the subtrees pLeft and pRight.
20812 */
20813 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
20814 sqlite3 *db,
20815 Expr *pRoot,
20816 Expr *pLeft,
20817 Expr *pRight
20818 ){
20819 if( pRoot==0 ){
20820 assert( db->mallocFailed );
20821 sqlite3ExprDelete(db, pLeft);
20822 sqlite3ExprDelete(db, pRight);
20823 }else{
20824 if( pRight ){
20825 pRoot->pRight = pRight;
20826 pRoot->flags |= EP_Propagate & pRight->flags;
20827 }
20828 if( pLeft ){
20829 pRoot->pLeft = pLeft;
20830 pRoot->flags |= EP_Propagate & pLeft->flags;
20831 }
20832 exprSetHeight(pRoot);
20833 }
20834 }
20835
20836 /*
20837 ** Allocate an Expr node which joins as many as two subtrees.
20838 **
20839 ** One or both of the subtrees can be NULL. Return a pointer to the new
20840 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
20841 ** free the subtrees and return NULL.
20842 */
20843 SQLITE_PRIVATE Expr *sqlite3PExpr(
20844 Parse *pParse, /* Parsing context */
20845 int op, /* Expression opcode */
20846 Expr *pLeft, /* Left operand */
20847 Expr *pRight, /* Right operand */
20848 const Token *pToken /* Argument token */
20849 ){
20850 Expr *p;
20851 if( op==TK_AND && pParse->nErr==0 ){
20852 /* Take advantage of short-circuit false optimization for AND */
20853 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
20854 }else{
20855 p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1);
20856 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
20857 }
20858 if( p ) {
20859 sqlite3ExprCheckHeight(pParse, p->nHeight);
20860 }
20861 return p;
20862 }
20863
20864 /*
20865 ** If the expression is always either TRUE or FALSE (respectively),
20866 ** then return 1. If one cannot determine the truth value of the
20867 ** expression at compile-time return 0.
20868 **
20869 ** This is an optimization. If is OK to return 0 here even if
20870 ** the expression really is always false or false (a false negative).
20871 ** But it is a bug to return 1 if the expression might have different
20872 ** boolean values in different circumstances (a false positive.)
20873 **
20874 ** Note that if the expression is part of conditional for a
20875 ** LEFT JOIN, then we cannot determine at compile-time whether or not
20876 ** is it true or false, so always return 0.
20877 */
20878 static int exprAlwaysTrue(Expr *p){
20879 int v = 0;
20880 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
20881 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
20882 return v!=0;
20883 }
20884 static int exprAlwaysFalse(Expr *p){
20885 int v = 0;
20886 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
20887 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
20888 return v==0;
20889 }
20890
20891 /*
20892 ** Join two expressions using an AND operator. If either expression is
20893 ** NULL, then just return the other expression.
20894 **
20895 ** If one side or the other of the AND is known to be false, then instead
20896 ** of returning an AND expression, just return a constant expression with
20897 ** a value of false.
20898 */
20899 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
20900 if( pLeft==0 ){
20901 return pRight;
20902 }else if( pRight==0 ){
20903 return pLeft;
20904 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
20905 sqlite3ExprDelete(db, pLeft);
20906 sqlite3ExprDelete(db, pRight);
20907 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
20908 }else{
20909 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
20910 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
20911 return pNew;
20912 }
20913 }
20914
20915 /*
20916 ** Construct a new expression node for a function with multiple
20917 ** arguments.
20918 */
20919 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token * pToken){
20920 Expr *pNew;
20921 sqlite3 *db = pParse->db;
20922 assert( pToken );
20923 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
20924 if( pNew==0 ){
20925 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
20926 return 0;
20927 }
20928 pNew->x.pList = pList;
20929 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
20930 sqlite3ExprSetHeightAndFlags(pParse, pNew);
20931 return pNew;
20932 }
20933
20934 /*
20935 ** Assign a variable number to an expression that encodes a wildcard
20936 ** in the original SQL statement.
20937 **
20938 ** Wildcards consisting of a single "?" are assigned the next sequential
20939 ** variable number.
20940 **
20941 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make
20942 ** sure "nnn" is not too be to avoid a denial of service attack when
20943 ** the SQL statement comes from an external source.
20944 **
20945 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
20946 ** as the previous instance of the same wildcard. Or if this is the first
20947 ** instance of the wildcard, the next sequential variable number is
20948 ** assigned.
20949 */
20950 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
20951 sqlite3 *db = pParse->db;
20952 const char *z;
20953
20954 if( pExpr==0 ) return;
20955 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
20956 z = pExpr->u.zToken;
20957 assert( z!=0 );
20958 assert( z[0]!=0 );
20959 if( z[1]==0 ){
20960 /* Wildcard of the form "?". Assign the next variable number */
20961 assert( z[0]=='?' );
20962 pExpr->iColumn = (ynVar)(++pParse->nVar);
20963 }else{
20964 ynVar x = 0;
20965 u32 n = sqlite3Strlen30(z);
20966 if( z[0]=='?' ){
20967 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
20968 ** use it as the variable number */
20969 i64 i;
20970 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
20971 pExpr->iColumn = x = (ynVar)i;
20972 testcase( i==0 );
20973 testcase( i==1 );
20974 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
20975 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
20976 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
20977 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
20978 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
20979 x = 0;
20980 }
20981 if( i>pParse->nVar ){
20982 pParse->nVar = (int)i;
20983 }
20984 }else{
20985 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
20986 ** number as the prior appearance of the same name, or if the name
20987 ** has never appeared before, reuse the same variable number
20988 */
20989 ynVar i;
20990 for(i=0; i<pParse->nzVar; i++){
20991 if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
20992 pExpr->iColumn = x = (ynVar)i+1;
20993 break;
20994 }
20995 }
20996 if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
20997 }
20998 if( x>0 ){
20999 if( x>pParse->nzVar ){
21000 char **a;
21001 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
21002 if( a==0 ) return; /* Error reported through db->mallocFailed */
21003 pParse->azVar = a;
21004 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
21005 pParse->nzVar = x;
21006 }
21007 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
21008 sqlite3DbFree(db, pParse->azVar[x-1]);
21009 pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
21010 }
21011 }
21012 }
21013 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
21014 sqlite3ErrorMsg(pParse, "too many SQL variables");
21015 }
21016 }
21017
21018 /*
21019 ** Recursively delete an expression tree.
21020 */
21021 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
21022 if( p==0 ) return;
21023 /* Sanity check: Assert that the IntValue is non-negative if it exists */
21024 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
21025 if( !ExprHasProperty(p, EP_TokenOnly) ){
21026 /* The Expr.x union is never used at the same time as Expr.pRight */
21027 assert( p->x.pList==0 || p->pRight==0 );
21028 sqlite3ExprDelete(db, p->pLeft);
21029 sqlite3ExprDelete(db, p->pRight);
21030 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
21031 if( ExprHasProperty(p, EP_xIsSelect) ){
21032 sqlite3SelectDelete(db, p->x.pSelect);
21033 }else{
21034 sqlite3ExprListDelete(db, p->x.pList);
21035 }
21036 }
21037 if( !ExprHasProperty(p, EP_Static) ){
21038 sqlite3DbFree(db, p);
21039 }
21040 }
21041
21042 /*
21043 ** Return the number of bytes allocated for the expression structure
21044 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
21045 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
21046 */
21047 static int exprStructSize(Expr *p){
21048 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
21049 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
21050 return EXPR_FULLSIZE;
21051 }
21052
21053 /*
21054 ** The dupedExpr*Size() routines each return the number of bytes required
21055 ** to store a copy of an expression or expression tree. They differ in
21056 ** how much of the tree is measured.
21057 **
21058 ** dupedExprStructSize() Size of only the Expr structure
21059 ** dupedExprNodeSize() Size of Expr + space for token
21060 ** dupedExprSize() Expr + token + subtree components
21061 **
21062 ***************************************************************************
21063 **
21064 ** The dupedExprStructSize() function returns two values OR-ed together:
21065 ** (1) the space required for a copy of the Expr structure only and
21066 ** (2) the EP_xxx flags that indicate what the structure size should be.
21067 ** The return values is always one of:
21068 **
21069 ** EXPR_FULLSIZE
21070 ** EXPR_REDUCEDSIZE | EP_Reduced
21071 ** EXPR_TOKENONLYSIZE | EP_TokenOnly
21072 **
21073 ** The size of the structure can be found by masking the return value
21074 ** of this routine with 0xfff. The flags can be found by masking the
21075 ** return value with EP_Reduced|EP_TokenOnly.
21076 **
21077 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
21078 ** (unreduced) Expr objects as they or originally constructed by the parser.
21079 ** During expression analysis, extra information is computed and moved into
21080 ** later parts of teh Expr object and that extra information might get chopped
21081 ** off if the expression is reduced. Note also that it does not work to
21082 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
21083 ** to reduce a pristine expression tree from the parser. The implementation
21084 ** of dupedExprStructSize() contain multiple assert() statements that attempt
21085 ** to enforce this constraint.
21086 */
21087 static int dupedExprStructSize(Expr *p, int flags){
21088 int nSize;
21089 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
21090 assert( EXPR_FULLSIZE<=0xfff );
21091 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
21092 if( 0==(flags&EXPRDUP_REDUCE) ){
21093 nSize = EXPR_FULLSIZE;
21094 }else{
21095 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
21096 assert( !ExprHasProperty(p, EP_FromJoin) );
21097 assert( !ExprHasProperty(p, EP_MemToken) );
21098 assert( !ExprHasProperty(p, EP_NoReduce) );
21099 if( p->pLeft || p->x.pList ){
21100 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
21101 }else{
21102 assert( p->pRight==0 );
21103 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
21104 }
21105 }
21106 return nSize;
21107 }
21108
21109 /*
21110 ** This function returns the space in bytes required to store the copy
21111 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
21112 ** string is defined.)
21113 */
21114 static int dupedExprNodeSize(Expr *p, int flags){
21115 int nByte = dupedExprStructSize(p, flags) & 0xfff;
21116 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
21117 nByte += sqlite3Strlen30(p->u.zToken)+1;
21118 }
21119 return ROUND8(nByte);
21120 }
21121
21122 /*
21123 ** Return the number of bytes required to create a duplicate of the
21124 ** expression passed as the first argument. The second argument is a
21125 ** mask containing EXPRDUP_XXX flags.
21126 **
21127 ** The value returned includes space to create a copy of the Expr struct
21128 ** itself and the buffer referred to by Expr.u.zToken, if any.
21129 **
21130 ** If the EXPRDUP_REDUCE flag is set, then the return value includes
21131 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
21132 ** and Expr.pRight variables (but not for any structures pointed to or
21133 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
21134 */
21135 static int dupedExprSize(Expr *p, int flags){
21136 int nByte = 0;
21137 if( p ){
21138 nByte = dupedExprNodeSize(p, flags);
21139 if( flags&EXPRDUP_REDUCE ){
21140 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
21141 }
21142 }
21143 return nByte;
21144 }
21145
21146 /*
21147 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
21148 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
21149 ** to store the copy of expression p, the copies of p->u.zToken
21150 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
21151 ** if any. Before returning, *pzBuffer is set to the first byte past the
21152 ** portion of the buffer copied into by this function.
21153 */
21154 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
21155 Expr *pNew = 0; /* Value to return */
21156 assert( flags==0 || flags==EXPRDUP_REDUCE );
21157 if( p ){
21158 const int isReduced = (flags&EXPRDUP_REDUCE);
21159 u8 *zAlloc;
21160 u32 staticFlag = 0;
21161
21162 assert( pzBuffer==0 || isReduced );
21163
21164 /* Figure out where to write the new Expr structure. */
21165 if( pzBuffer ){
21166 zAlloc = *pzBuffer;
21167 staticFlag = EP_Static;
21168 }else{
21169 zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
21170 }
21171 pNew = (Expr *)zAlloc;
21172
21173 if( pNew ){
21174 /* Set nNewSize to the size allocated for the structure pointed to
21175 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
21176 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
21177 ** by the copy of the p->u.zToken string (if any).
21178 */
21179 const unsigned nStructSize = dupedExprStructSize(p, flags);
21180 const int nNewSize = nStructSize & 0xfff;
21181 int nToken;
21182 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
21183 nToken = sqlite3Strlen30(p->u.zToken) + 1;
21184 }else{
21185 nToken = 0;
21186 }
21187 if( isReduced ){
21188 assert( ExprHasProperty(p, EP_Reduced)==0 );
21189 memcpy(zAlloc, p, nNewSize);
21190 }else{
21191 u32 nSize = (u32)exprStructSize(p);
21192 memcpy(zAlloc, p, nSize);
21193 if( nSize<EXPR_FULLSIZE ){
21194 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
21195 }
21196 }
21197
21198 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
21199 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
21200 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
21201 pNew->flags |= staticFlag;
21202
21203 /* Copy the p->u.zToken string, if any. */
21204 if( nToken ){
21205 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
21206 memcpy(zToken, p->u.zToken, nToken);
21207 }
21208
21209 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
21210 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
21211 if( ExprHasProperty(p, EP_xIsSelect) ){
21212 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
21213 }else{
21214 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
21215 }
21216 }
21217
21218 /* Fill in pNew->pLeft and pNew->pRight. */
21219 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
21220 zAlloc += dupedExprNodeSize(p, flags);
21221 if( ExprHasProperty(pNew, EP_Reduced) ){
21222 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
21223 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
21224 }
21225 if( pzBuffer ){
21226 *pzBuffer = zAlloc;
21227 }
21228 }else{
21229 if( !ExprHasProperty(p, EP_TokenOnly) ){
21230 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
21231 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
21232 }
21233 }
21234
21235 }
21236 }
21237 return pNew;
21238 }
21239
21240 /*
21241 ** Create and return a deep copy of the object passed as the second
21242 ** argument. If an OOM condition is encountered, NULL is returned
21243 ** and the db->mallocFailed flag set.
21244 */
21245 #ifndef SQLITE_OMIT_CTE
21246 static With *withDup(sqlite3 *db, With *p){
21247 With *pRet = 0;
21248 if( p ){
21249 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
21250 pRet = sqlite3DbMallocZero(db, nByte);
21251 if( pRet ){
21252 int i;
21253 pRet->nCte = p->nCte;
21254 for(i=0; i<p->nCte; i++){
21255 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
21256 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
21257 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
21258 }
21259 }
21260 }
21261 return pRet;
21262 }
21263 #else
21264 # define withDup(x,y) 0
21265 #endif
21266
21267 /*
21268 ** The following group of routines make deep copies of expressions,
21269 ** expression lists, ID lists, and select statements. The copies can
21270 ** be deleted (by being passed to their respective ...Delete() routines)
21271 ** without effecting the originals.
21272 **
21273 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
21274 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
21275 ** by subsequent calls to sqlite*ListAppend() routines.
21276 **
21277 ** Any tables that the SrcList might point to are not duplicated.
21278 **
21279 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
21280 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
21281 ** truncated version of the usual Expr structure that will be stored as
21282 ** part of the in-memory representation of the database schema.
21283 */
21284 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
21285 assert( flags==0 || flags==EXPRDUP_REDUCE );
21286 return exprDup(db, p, flags, 0);
21287 }
21288 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags) {
21289 ExprList *pNew;
21290 struct ExprList_item *pItem, *pOldItem;
21291 int i;
21292 if( p==0 ) return 0;
21293 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
21294 if( pNew==0 ) return 0;
21295 pNew->nExpr = i = p->nExpr;
21296 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
21297 pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) );
21298 if( pItem==0 ){
21299 sqlite3DbFree(db, pNew);
21300 return 0;
21301 }
21302 pOldItem = p->a;
21303 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
21304 Expr *pOldExpr = pOldItem->pExpr;
21305 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
21306 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
21307 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
21308 pItem->sortOrder = pOldItem->sortOrder;
21309 pItem->done = 0;
21310 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
21311 pItem->u = pOldItem->u;
21312 }
21313 return pNew;
21314 }
21315
21316 /*
21317 ** If cursors, triggers, views and subqueries are all omitted from
21318 ** the build, then none of the following routines, except for
21319 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
21320 ** called with a NULL argument.
21321 */
21322 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
21323 || !defined(SQLITE_OMIT_SUBQUERY)
21324 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
21325 SrcList *pNew;
21326 int i;
21327 int nByte;
21328 if( p==0 ) return 0;
21329 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
21330 pNew = sqlite3DbMallocRaw(db, nByte );
21331 if( pNew==0 ) return 0;
21332 pNew->nSrc = pNew->nAlloc = p->nSrc;
21333 for(i=0; i<p->nSrc; i++){
21334 struct SrcList_item *pNewItem = &pNew->a[i];
21335 struct SrcList_item *pOldItem = &p->a[i];
21336 Table *pTab;
21337 pNewItem->pSchema = pOldItem->pSchema;
21338 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
21339 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
21340 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
21341 pNewItem->fg = pOldItem->fg;
21342 pNewItem->iCursor = pOldItem->iCursor;
21343 pNewItem->addrFillSub = pOldItem->addrFillSub;
21344 pNewItem->regReturn = pOldItem->regReturn;
21345 if( pNewItem->fg.isIndexedBy ){
21346 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
21347 }
21348 pNewItem->pIBIndex = pOldItem->pIBIndex;
21349 if( pNewItem->fg.isTabFunc ){
21350 pNewItem->u1.pFuncArg =
21351 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
21352 }
21353 pTab = pNewItem->pTab = pOldItem->pTab;
21354 if( pTab ){
21355 pTab->nRef++;
21356 }
21357 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
21358 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
21359 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
21360 pNewItem->colUsed = pOldItem->colUsed;
21361 }
21362 return pNew;
21363 }
21364 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
21365 IdList *pNew;
21366 int i;
21367 if( p==0 ) return 0;
21368 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
21369 if( pNew==0 ) return 0;
21370 pNew->nId = p->nId;
21371 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
21372 if( pNew->a==0 ){
21373 sqlite3DbFree(db, pNew);
21374 return 0;
21375 }
21376 /* Note that because the size of the allocation for p->a[] is not
21377 ** necessarily a power of two, sqlite3IdListAppend() may not be called
21378 ** on the duplicate created by this function. */
21379 for(i=0; i<p->nId; i++){
21380 struct IdList_item *pNewItem = &pNew->a[i];
21381 struct IdList_item *pOldItem = &p->a[i];
21382 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
21383 pNewItem->idx = pOldItem->idx;
21384 }
21385 return pNew;
21386 }
21387 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
21388 Select *pNew, *pPrior;
21389 if( p==0 ) return 0;
21390 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
21391 if( pNew==0 ) return 0;
21392 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
21393 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
21394 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
21395 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
21396 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
21397 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
21398 pNew->op = p->op;
21399 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
21400 if( pPrior ) pPrior->pNext = pNew;
21401 pNew->pNext = 0;
21402 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
21403 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
21404 pNew->iLimit = 0;
21405 pNew->iOffset = 0;
21406 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
21407 pNew->addrOpenEphm[0] = -1;
21408 pNew->addrOpenEphm[1] = -1;
21409 pNew->nSelectRow = p->nSelectRow;
21410 pNew->pWith = withDup(db, p->pWith);
21411 sqlite3SelectSetName(pNew, p->zSelName);
21412 return pNew;
21413 }
21414 #else
21415 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
21416 assert( p==0 );
21417 return 0;
21418 }
21419 #endif
21420
21421
21422 /*
21423 ** Add a new element to the end of an expression list. If pList is
21424 ** initially NULL, then create a new expression list.
21425 **
21426 ** If a memory allocation error occurs, the entire list is freed and
21427 ** NULL is returned. If non-NULL is returned, then it is guaranteed
21428 ** that the new entry was successfully appended.
21429 */
21430 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
21431 Parse *pParse, /* Parsing context */
21432 ExprList *pList, /* List to which to append. Might be NULL */
21433 Expr *pExpr /* Expression to be appended. Might be NULL */
21434 ){
21435 sqlite3 *db = pParse->db;
21436 if( pList==0 ){
21437 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
21438 if( pList==0 ){
21439 goto no_mem;
21440 }
21441 pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
21442 if( pList->a==0 ) goto no_mem;
21443 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
21444 struct ExprList_item *a;
21445 assert( pList->nExpr>0 );
21446 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
21447 if( a==0 ){
21448 goto no_mem;
21449 }
21450 pList->a = a;
21451 }
21452 assert( pList->a!=0 );
21453 if( 1 ){
21454 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
21455 memset(pItem, 0, sizeof(*pItem));
21456 pItem->pExpr = pExpr;
21457 }
21458 return pList;
21459
21460 no_mem:
21461 /* Avoid leaking memory if malloc has failed. */
21462 sqlite3ExprDelete(db, pExpr);
21463 sqlite3ExprListDelete(db, pList);
21464 return 0;
21465 }
21466
21467 /*
21468 ** Set the sort order for the last element on the given ExprList.
21469 */
21470 SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
21471 if( p==0 ) return;
21472 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
21473 assert( p->nExpr>0 );
21474 if( iSortOrder<0 ){
21475 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
21476 return;
21477 }
21478 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
21479 }
21480
21481 /*
21482 ** Set the ExprList.a[].zName element of the most recently added item
21483 ** on the expression list.
21484 **
21485 ** pList might be NULL following an OOM error. But pName should never be
21486 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
21487 ** is set.
21488 */
21489 SQLITE_PRIVATE void sqlite3ExprListSetName(
21490 Parse *pParse, /* Parsing context */
21491 ExprList *pList, /* List to which to add the span. */
21492 Token *pName, /* Name to be added */
21493 int dequote /* True to cause the name to be dequoted */
21494 ){
21495 assert( pList!=0 || pParse->db->mallocFailed!=0 );
21496 if( pList ){
21497 struct ExprList_item *pItem;
21498 assert( pList->nExpr>0 );
21499 pItem = &pList->a[pList->nExpr-1];
21500 assert( pItem->zName==0 );
21501 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
21502 if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
21503 }
21504 }
21505
21506 /*
21507 ** Set the ExprList.a[].zSpan element of the most recently added item
21508 ** on the expression list.
21509 **
21510 ** pList might be NULL following an OOM error. But pSpan should never be
21511 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
21512 ** is set.
21513 */
21514 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
21515 Parse *pParse, /* Parsing context */
21516 ExprList *pList, /* List to which to add the span. */
21517 ExprSpan *pSpan /* The span to be added */
21518 ){
21519 sqlite3 *db = pParse->db;
21520 assert( pList!=0 || db->mallocFailed!=0 );
21521 if( pList ){
21522 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
21523 assert( pList->nExpr>0 );
21524 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
21525 sqlite3DbFree(db, pItem->zSpan);
21526 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
21527 (int)(pSpan->zEnd - pSpan->zStart));
21528 }
21529 }
21530
21531 /*
21532 ** If the expression list pEList contains more than iLimit elements,
21533 ** leave an error message in pParse.
21534 */
21535 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
21536 Parse *pParse,
21537 ExprList *pEList,
21538 const char *zObject
21539 ){
21540 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
21541 testcase( pEList && pEList->nExpr==mx );
21542 testcase( pEList && pEList->nExpr==mx+1 );
21543 if( pEList && pEList->nExpr>mx ){
21544 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
21545 }
21546 }
21547
21548 /*
21549 ** Delete an entire expression list.
21550 */
21551 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
21552 int i;
21553 struct ExprList_item *pItem;
21554 if( pList==0 ) return;
21555 assert( pList->a!=0 || pList->nExpr==0 );
21556 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
21557 sqlite3ExprDelete(db, pItem->pExpr);
21558 sqlite3DbFree(db, pItem->zName);
21559 sqlite3DbFree(db, pItem->zSpan);
21560 }
21561 sqlite3DbFree(db, pList->a);
21562 sqlite3DbFree(db, pList);
21563 }
21564
21565 /*
21566 ** Return the bitwise-OR of all Expr.flags fields in the given
21567 ** ExprList.
21568 */
21569 SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
21570 int i;
21571 u32 m = 0;
21572 if( pList ){
21573 for(i=0; i<pList->nExpr; i++){
21574 Expr *pExpr = pList->a[i].pExpr;
21575 if( ALWAYS(pExpr) ) m |= pExpr->flags;
21576 }
21577 }
21578 return m;
21579 }
21580
21581 /*
21582 ** These routines are Walker callbacks used to check expressions to
21583 ** see if they are "constant" for some definition of constant. The
21584 ** Walker.eCode value determines the type of "constant" we are looking
21585 ** for.
21586 **
21587 ** These callback routines are used to implement the following:
21588 **
21589 ** sqlite3ExprIsConstant() pWalker->eCode==1
21590 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
21591 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
21592 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
21593 **
21594 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
21595 ** is found to not be a constant.
21596 **
21597 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
21598 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
21599 ** an existing schema and 4 when processing a new statement. A bound
21600 ** parameter raises an error for new statements, but is silently converted
21601 ** to NULL for existing schemas. This allows sqlite_master tables that
21602 ** contain a bound parameter because they were generated by older versions
21603 ** of SQLite to be parsed by newer versions of SQLite without raising a
21604 ** malformed schema error.
21605 */
21606 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
21607
21608 /* If pWalker->eCode is 2 then any term of the expression that comes from
21609 ** the ON or USING clauses of a left join disqualifies the expression
21610 ** from being considered constant. */
21611 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
21612 pWalker->eCode = 0;
21613 return WRC_Abort;
21614 }
21615
21616 switch( pExpr->op ){
21617 /* Consider functions to be constant if all their arguments are constant
21618 ** and either pWalker->eCode==4 or 5 or the function has the
21619 ** SQLITE_FUNC_CONST flag. */
21620 case TK_FUNCTION:
21621 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
21622 return WRC_Continue;
21623 }else{
21624 pWalker->eCode = 0;
21625 return WRC_Abort;
21626 }
21627 case TK_ID:
21628 case TK_COLUMN:
21629 case TK_AGG_FUNCTION:
21630 case TK_AGG_COLUMN:
21631 testcase( pExpr->op==TK_ID );
21632 testcase( pExpr->op==TK_COLUMN );
21633 testcase( pExpr->op==TK_AGG_FUNCTION );
21634 testcase( pExpr->op==TK_AGG_COLUMN );
21635 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
21636 return WRC_Continue;
21637 }else{
21638 pWalker->eCode = 0;
21639 return WRC_Abort;
21640 }
21641 case TK_VARIABLE:
21642 if( pWalker->eCode==5 ){
21643 /* Silently convert bound parameters that appear inside of CREATE
21644 ** statements into a NULL when parsing the CREATE statement text out
21645 ** of the sqlite_master table */
21646 pExpr->op = TK_NULL;
21647 }else if( pWalker->eCode==4 ){
21648 /* A bound parameter in a CREATE statement that originates from
21649 ** sqlite3_prepare() causes an error */
21650 pWalker->eCode = 0;
21651 return WRC_Abort;
21652 }
21653 /* Fall through */
21654 default:
21655 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
21656 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
21657 return WRC_Continue;
21658 }
21659 }
21660 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
21661 UNUSED_PARAMETER(NotUsed);
21662 pWalker->eCode = 0;
21663 return WRC_Abort;
21664 }
21665 static int exprIsConst(Expr *p, int initFlag, int iCur){
21666 Walker w;
21667 memset(&w, 0, sizeof(w));
21668 w.eCode = initFlag;
21669 w.xExprCallback = exprNodeIsConstant;
21670 w.xSelectCallback = selectNodeIsConstant;
21671 w.u.iCur = iCur;
21672 sqlite3WalkExpr(&w, p);
21673 return w.eCode;
21674 }
21675
21676 /*
21677 ** Walk an expression tree. Return non-zero if the expression is constant
21678 ** and 0 if it involves variables or function calls.
21679 **
21680 ** For the purposes of this function, a double-quoted string (ex: "abc")
21681 ** is considered a variable but a single-quoted string (ex: 'abc') is
21682 ** a constant.
21683 */
21684 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
21685 return exprIsConst(p, 1, 0);
21686 }
21687
21688 /*
21689 ** Walk an expression tree. Return non-zero if the expression is constant
21690 ** that does no originate from the ON or USING clauses of a join.
21691 ** Return 0 if it involves variables or function calls or terms from
21692 ** an ON or USING clause.
21693 */
21694 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
21695 return exprIsConst(p, 2, 0);
21696 }
21697
21698 /*
21699 ** Walk an expression tree. Return non-zero if the expression is constant
21700 ** for any single row of the table with cursor iCur. In other words, the
21701 ** expression must not refer to any non-deterministic function nor any
21702 ** table other than iCur.
21703 */
21704 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
21705 return exprIsConst(p, 3, iCur);
21706 }
21707
21708 /*
21709 ** Walk an expression tree. Return non-zero if the expression is constant
21710 ** or a function call with constant arguments. Return and 0 if there
21711 ** are any variables.
21712 **
21713 ** For the purposes of this function, a double-quoted string (ex: "abc")
21714 ** is considered a variable but a single-quoted string (ex: 'abc') is
21715 ** a constant.
21716 */
21717 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
21718 assert( isInit==0 || isInit==1 );
21719 return exprIsConst(p, 4+isInit, 0);
21720 }
21721
21722 #ifdef SQLITE_ENABLE_CURSOR_HINTS
21723 /*
21724 ** Walk an expression tree. Return 1 if the expression contains a
21725 ** subquery of some kind. Return 0 if there are no subqueries.
21726 */
21727 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
21728 Walker w;
21729 memset(&w, 0, sizeof(w));
21730 w.eCode = 1;
21731 w.xExprCallback = sqlite3ExprWalkNoop;
21732 w.xSelectCallback = selectNodeIsConstant;
21733 sqlite3WalkExpr(&w, p);
21734 return w.eCode==0;
21735 }
21736 #endif
21737
21738 /*
21739 ** If the expression p codes a constant integer that is small enough
21740 ** to fit in a 32-bit integer, return 1 and put the value of the integer
21741 ** in *pValue. If the expression is not an integer or if it is too big
21742 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
21743 */
21744 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
21745 int rc = 0;
21746
21747 /* If an expression is an integer literal that fits in a signed 32-bit
21748 ** integer, then the EP_IntValue flag will have already been set */
21749 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
21750 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
21751
21752 if( p->flags & EP_IntValue ){
21753 *pValue = p->u.iValue;
21754 return 1;
21755 }
21756 switch( p->op ){
21757 case TK_UPLUS: {
21758 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
21759 break;
21760 }
21761 case TK_UMINUS: {
21762 int v;
21763 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
21764 assert( v!=(-2147483647-1) );
21765 *pValue = -v;
21766 rc = 1;
21767 }
21768 break;
21769 }
21770 default: break;
21771 }
21772 return rc;
21773 }
21774
21775 /*
21776 ** Return FALSE if there is no chance that the expression can be NULL.
21777 **
21778 ** If the expression might be NULL or if the expression is too complex
21779 ** to tell return TRUE.
21780 **
21781 ** This routine is used as an optimization, to skip OP_IsNull opcodes
21782 ** when we know that a value cannot be NULL. Hence, a false positive
21783 ** (returning TRUE when in fact the expression can never be NULL) might
21784 ** be a small performance hit but is otherwise harmless. On the other
21785 ** hand, a false negative (returning FALSE when the result could be NULL)
21786 ** will likely result in an incorrect answer. So when in doubt, return
21787 ** TRUE.
21788 */
21789 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
21790 u8 op;
21791 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
21792 op = p->op;
21793 if( op==TK_REGISTER ) op = p->op2;
21794 switch( op ){
21795 case TK_INTEGER:
21796 case TK_STRING:
21797 case TK_FLOAT:
21798 case TK_BLOB:
21799 return 0;
21800 case TK_COLUMN:
21801 assert( p->pTab!=0 );
21802 return ExprHasProperty(p, EP_CanBeNull) ||
21803 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
21804 default:
21805 return 1;
21806 }
21807 }
21808
21809 /*
21810 ** Return TRUE if the given expression is a constant which would be
21811 ** unchanged by OP_Affinity with the affinity given in the second
21812 ** argument.
21813 **
21814 ** This routine is used to determine if the OP_Affinity operation
21815 ** can be omitted. When in doubt return FALSE. A false negative
21816 ** is harmless. A false positive, however, can result in the wrong
21817 ** answer.
21818 */
21819 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
21820 u8 op;
21821 if( aff==SQLITE_AFF_BLOB ) return 1;
21822 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
21823 op = p->op;
21824 if( op==TK_REGISTER ) op = p->op2;
21825 switch( op ){
21826 case TK_INTEGER: {
21827 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
21828 }
21829 case TK_FLOAT: {
21830 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
21831 }
21832 case TK_STRING: {
21833 return aff==SQLITE_AFF_TEXT;
21834 }
21835 case TK_BLOB: {
21836 return 1;
21837 }
21838 case TK_COLUMN: {
21839 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
21840 return p->iColumn<0
21841 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
21842 }
21843 default: {
21844 return 0;
21845 }
21846 }
21847 }
21848
21849 /*
21850 ** Return TRUE if the given string is a row-id column name.
21851 */
21852 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
21853 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
21854 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
21855 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
21856 return 0;
21857 }
21858
21859 /*
21860 ** Return true if we are able to the IN operator optimization on a
21861 ** query of the form
21862 **
21863 ** x IN (SELECT ...)
21864 **
21865 ** Where the SELECT... clause is as specified by the parameter to this
21866 ** routine.
21867 **
21868 ** The Select object passed in has already been preprocessed and no
21869 ** errors have been found.
21870 */
21871 #ifndef SQLITE_OMIT_SUBQUERY
21872 static int isCandidateForInOpt(Select *p){
21873 SrcList *pSrc;
21874 ExprList *pEList;
21875 Table *pTab;
21876 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
21877 if( p->pPrior ) return 0; /* Not a compound SELECT */
21878 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
21879 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
21880 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
21881 return 0; /* No DISTINCT keyword and no aggregate functions */
21882 }
21883 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
21884 if( p->pLimit ) return 0; /* Has no LIMIT clause */
21885 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
21886 if( p->pWhere ) return 0; /* Has no WHERE clause */
21887 pSrc = p->pSrc;
21888 assert( pSrc!=0 );
21889 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
21890 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
21891 pTab = pSrc->a[0].pTab;
21892 if( NEVER(pTab==0) ) return 0;
21893 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
21894 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
21895 pEList = p->pEList;
21896 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
21897 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
21898 return 1;
21899 }
21900 #endif /* SQLITE_OMIT_SUBQUERY */
21901
21902 /*
21903 ** Code an OP_Once instruction and allocate space for its flag. Return the
21904 ** address of the new instruction.
21905 */
21906 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
21907 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
21908 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
21909 }
21910
21911 /*
21912 ** Generate code that checks the left-most column of index table iCur to see if
21913 ** it contains any NULL entries. Cause the register at regHasNull to be set
21914 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
21915 ** to be set to NULL if iCur contains one or more NULL values.
21916 */
21917 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
21918 int addr1;
21919 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
21920 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
21921 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
21922 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
21923 VdbeComment((v, "first_entry_in(%d)", iCur));
21924 sqlite3VdbeJumpHere(v, addr1);
21925 }
21926
21927
21928 #ifndef SQLITE_OMIT_SUBQUERY
21929 /*
21930 ** The argument is an IN operator with a list (not a subquery) on the
21931 ** right-hand side. Return TRUE if that list is constant.
21932 */
21933 static int sqlite3InRhsIsConstant(Expr *pIn){
21934 Expr *pLHS;
21935 int res;
21936 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
21937 pLHS = pIn->pLeft;
21938 pIn->pLeft = 0;
21939 res = sqlite3ExprIsConstant(pIn);
21940 pIn->pLeft = pLHS;
21941 return res;
21942 }
21943 #endif
21944
21945 /*
21946 ** This function is used by the implementation of the IN (...) operator.
21947 ** The pX parameter is the expression on the RHS of the IN operator, which
21948 ** might be either a list of expressions or a subquery.
21949 **
21950 ** The job of this routine is to find or create a b-tree object that can
21951 ** be used either to test for membership in the RHS set or to iterate through
21952 ** all members of the RHS set, skipping duplicates.
21953 **
21954 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
21955 ** and pX->iTable is set to the index of that cursor.
21956 **
21957 ** The returned value of this function indicates the b-tree type, as follows:
21958 **
21959 ** IN_INDEX_ROWID - The cursor was opened on a database table.
21960 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
21961 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
21962 ** IN_INDEX_EPH - The cursor was opened on a specially created and
21963 ** populated epheremal table.
21964 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
21965 ** implemented as a sequence of comparisons.
21966 **
21967 ** An existing b-tree might be used if the RHS expression pX is a simple
21968 ** subquery such as:
21969 **
21970 ** SELECT <column> FROM <table>
21971 **
21972 ** If the RHS of the IN operator is a list or a more complex subquery, then
21973 ** an ephemeral table might need to be generated from the RHS and then
21974 ** pX->iTable made to point to the ephemeral table instead of an
21975 ** existing table.
21976 **
21977 ** The inFlags parameter must contain exactly one of the bits
21978 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
21979 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
21980 ** fast membership test. When the IN_INDEX_LOOP bit is set, the
21981 ** IN index will be used to loop over all values of the RHS of the
21982 ** IN operator.
21983 **
21984 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
21985 ** through the set members) then the b-tree must not contain duplicates.
21986 ** An epheremal table must be used unless the selected <column> is guaranteed
21987 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
21988 ** has a UNIQUE constraint or UNIQUE index.
21989 **
21990 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
21991 ** for fast set membership tests) then an epheremal table must
21992 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
21993 ** be found with <column> as its left-most column.
21994 **
21995 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
21996 ** if the RHS of the IN operator is a list (not a subquery) then this
21997 ** routine might decide that creating an ephemeral b-tree for membership
21998 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the
21999 ** calling routine should implement the IN operator using a sequence
22000 ** of Eq or Ne comparison operations.
22001 **
22002 ** When the b-tree is being used for membership tests, the calling function
22003 ** might need to know whether or not the RHS side of the IN operator
22004 ** contains a NULL. If prRhsHasNull is not a NULL pointer and
22005 ** if there is any chance that the (...) might contain a NULL value at
22006 ** runtime, then a register is allocated and the register number written
22007 ** to *prRhsHasNull. If there is no chance that the (...) contains a
22008 ** NULL value, then *prRhsHasNull is left unchanged.
22009 **
22010 ** If a register is allocated and its location stored in *prRhsHasNull, then
22011 ** the value in that register will be NULL if the b-tree contains one or more
22012 ** NULL values, and it will be some non-NULL value if the b-tree contains no
22013 ** NULL values.
22014 */
22015 #ifndef SQLITE_OMIT_SUBQUERY
22016 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
22017 Select *p; /* SELECT to the right of IN operator */
22018 int eType = 0; /* Type of RHS table. IN_INDEX_* */
22019 int iTab = pParse->nTab++; /* Cursor of the RHS table */
22020 int mustBeUnique; /* True if RHS must be unique */
22021 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
22022
22023 assert( pX->op==TK_IN );
22024 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
22025
22026 /* Check to see if an existing table or index can be used to
22027 ** satisfy the query. This is preferable to generating a new
22028 ** ephemeral table.
22029 */
22030 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
22031 if( pParse->nErr==0 && isCandidateForInOpt(p) ){
22032 sqlite3 *db = pParse->db; /* Database connection */
22033 Table *pTab; /* Table <table>. */
22034 Expr *pExpr; /* Expression <column> */
22035 i16 iCol; /* Index of column <column> */
22036 i16 iDb; /* Database idx for pTab */
22037
22038 assert( p ); /* Because of isCandidateForInOpt(p) */
22039 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
22040 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
22041 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
22042 pTab = p->pSrc->a[0].pTab;
22043 pExpr = p->pEList->a[0].pExpr;
22044 iCol = (i16)pExpr->iColumn;
22045
22046 /* Code an OP_Transaction and OP_TableLock for <table>. */
22047 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
22048 sqlite3CodeVerifySchema(pParse, iDb);
22049 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
22050
22051 /* This function is only called from two places. In both cases the vdbe
22052 ** has already been allocated. So assume sqlite3GetVdbe() is always
22053 ** successful here.
22054 */
22055 assert(v);
22056 if( iCol<0 ){
22057 int iAddr = sqlite3CodeOnce(pParse);
22058 VdbeCoverage(v);
22059
22060 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
22061 eType = IN_INDEX_ROWID;
22062
22063 sqlite3VdbeJumpHere(v, iAddr);
22064 }else{
22065 Index *pIdx; /* Iterator variable */
22066
22067 /* The collation sequence used by the comparison. If an index is to
22068 ** be used in place of a temp-table, it must be ordered according
22069 ** to this collation sequence. */
22070 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
22071
22072 /* Check that the affinity that will be used to perform the
22073 ** comparison is the same as the affinity of the column. If
22074 ** it is not, it is not possible to use any index.
22075 */
22076 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
22077
22078 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
22079 if( (pIdx->aiColumn[0]==iCol)
22080 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
22081 && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
22082 ){
22083 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
22084 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
22085 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
22086 VdbeComment((v, "%s", pIdx->zName));
22087 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
22088 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
22089
22090 if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
22091 *prRhsHasNull = ++pParse->nMem;
22092 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
22093 }
22094 sqlite3VdbeJumpHere(v, iAddr);
22095 }
22096 }
22097 }
22098 }
22099
22100 /* If no preexisting index is available for the IN clause
22101 ** and IN_INDEX_NOOP is an allowed reply
22102 ** and the RHS of the IN operator is a list, not a subquery
22103 ** and the RHS is not contant or has two or fewer terms,
22104 ** then it is not worth creating an ephemeral table to evaluate
22105 ** the IN operator so return IN_INDEX_NOOP.
22106 */
22107 if( eType==0
22108 && (inFlags & IN_INDEX_NOOP_OK)
22109 && !ExprHasProperty(pX, EP_xIsSelect)
22110 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
22111 ){
22112 eType = IN_INDEX_NOOP;
22113 }
22114
22115
22116 if( eType==0 ){
22117 /* Could not find an existing table or index to use as the RHS b-tree.
22118 ** We will have to generate an ephemeral table to do the job.
22119 */
22120 u32 savedNQueryLoop = pParse->nQueryLoop;
22121 int rMayHaveNull = 0;
22122 eType = IN_INDEX_EPH;
22123 if( inFlags & IN_INDEX_LOOP ){
22124 pParse->nQueryLoop = 0;
22125 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
22126 eType = IN_INDEX_ROWID;
22127 }
22128 }else if( prRhsHasNull ){
22129 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
22130 }
22131 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
22132 pParse->nQueryLoop = savedNQueryLoop;
22133 }else{
22134 pX->iTable = iTab;
22135 }
22136 return eType;
22137 }
22138 #endif
22139
22140 /*
22141 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
22142 ** or IN operators. Examples:
22143 **
22144 ** (SELECT a FROM b) -- subquery
22145 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
22146 ** x IN (4,5,11) -- IN operator with list on right-hand side
22147 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
22148 **
22149 ** The pExpr parameter describes the expression that contains the IN
22150 ** operator or subquery.
22151 **
22152 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
22153 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
22154 ** to some integer key column of a table B-Tree. In this case, use an
22155 ** intkey B-Tree to store the set of IN(...) values instead of the usual
22156 ** (slower) variable length keys B-Tree.
22157 **
22158 ** If rMayHaveNull is non-zero, that means that the operation is an IN
22159 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
22160 ** All this routine does is initialize the register given by rMayHaveNull
22161 ** to NULL. Calling routines will take care of changing this register
22162 ** value to non-NULL if the RHS is NULL-free.
22163 **
22164 ** For a SELECT or EXISTS operator, return the register that holds the
22165 ** result. For IN operators or if an error occurs, the return value is 0.
22166 */
22167 #ifndef SQLITE_OMIT_SUBQUERY
22168 SQLITE_PRIVATE int sqlite3CodeSubselect(
22169 Parse *pParse, /* Parsing context */
22170 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
22171 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
22172 int isRowid /* If true, LHS of IN operator is a rowid */
22173 ){
22174 int jmpIfDynamic = -1; /* One-time test address */
22175 int rReg = 0; /* Register storing resulting */
22176 Vdbe *v = sqlite3GetVdbe(pParse);
22177 if( NEVER(v==0) ) return 0;
22178 sqlite3ExprCachePush(pParse);
22179
22180 /* This code must be run in its entirety every time it is encountered
22181 ** if any of the following is true:
22182 **
22183 ** * The right-hand side is a correlated subquery
22184 ** * The right-hand side is an expression list containing variables
22185 ** * We are inside a trigger
22186 **
22187 ** If all of the above are false, then we can run this code just once
22188 ** save the results, and reuse the same result on subsequent invocations.
22189 */
22190 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
22191 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
22192 }
22193
22194 #ifndef SQLITE_OMIT_EXPLAIN
22195 if( pParse->explain==2 ){
22196 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
22197 jmpIfDynamic>=0?"":"CORRELATED ",
22198 pExpr->op==TK_IN?"LIST":"SCALAR",
22199 pParse->iNextSelectId
22200 );
22201 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
22202 }
22203 #endif
22204
22205 switch( pExpr->op ){
22206 case TK_IN: {
22207 char affinity; /* Affinity of the LHS of the IN */
22208 int addr; /* Address of OP_OpenEphemeral instruction */
22209 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
22210 KeyInfo *pKeyInfo = 0; /* Key information */
22211
22212 affinity = sqlite3ExprAffinity(pLeft);
22213
22214 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
22215 ** expression it is handled the same way. An ephemeral table is
22216 ** filled with single-field index keys representing the results
22217 ** from the SELECT or the <exprlist>.
22218 **
22219 ** If the 'x' expression is a column value, or the SELECT...
22220 ** statement returns a column value, then the affinity of that
22221 ** column is used to build the index keys. If both 'x' and the
22222 ** SELECT... statement are columns, then numeric affinity is used
22223 ** if either column has NUMERIC or INTEGER affinity. If neither
22224 ** 'x' nor the SELECT... statement are columns, then numeric affinity
22225 ** is used.
22226 */
22227 pExpr->iTable = pParse->nTab++;
22228 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
22229 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
22230
22231 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
22232 /* Case 1: expr IN (SELECT ...)
22233 **
22234 ** Generate code to write the results of the select into the temporary
22235 ** table allocated and opened above.
22236 */
22237 Select *pSelect = pExpr->x.pSelect;
22238 SelectDest dest;
22239 ExprList *pEList;
22240
22241 assert( !isRowid );
22242 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
22243 dest.affSdst = (u8)affinity;
22244 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
22245 pSelect->iLimit = 0;
22246 testcase( pSelect->selFlags & SF_Distinct );
22247 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
22248 if( sqlite3Select(pParse, pSelect, &dest) ){
22249 sqlite3KeyInfoUnref(pKeyInfo);
22250 return 0;
22251 }
22252 pEList = pSelect->pEList;
22253 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
22254 assert( pEList!=0 );
22255 assert( pEList->nExpr>0 );
22256 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
22257 pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
22258 pEList->a[0].pExpr);
22259 }else if( ALWAYS(pExpr->x.pList!=0) ){
22260 /* Case 2: expr IN (exprlist)
22261 **
22262 ** For each expression, build an index key from the evaluation and
22263 ** store it in the temporary table. If <expr> is a column, then use
22264 ** that columns affinity when building index keys. If <expr> is not
22265 ** a column, use numeric affinity.
22266 */
22267 int i;
22268 ExprList *pList = pExpr->x.pList;
22269 struct ExprList_item *pItem;
22270 int r1, r2, r3;
22271
22272 if( !affinity ){
22273 affinity = SQLITE_AFF_BLOB;
22274 }
22275 if( pKeyInfo ){
22276 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
22277 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
22278 }
22279
22280 /* Loop through each expression in <exprlist>. */
22281 r1 = sqlite3GetTempReg(pParse);
22282 r2 = sqlite3GetTempReg(pParse);
22283 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
22284 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
22285 Expr *pE2 = pItem->pExpr;
22286 int iValToIns;
22287
22288 /* If the expression is not constant then we will need to
22289 ** disable the test that was generated above that makes sure
22290 ** this code only executes once. Because for a non-constant
22291 ** expression we need to rerun this code each time.
22292 */
22293 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
22294 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
22295 jmpIfDynamic = -1;
22296 }
22297
22298 /* Evaluate the expression and insert it into the temp table */
22299 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
22300 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
22301 }else{
22302 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
22303 if( isRowid ){
22304 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
22305 sqlite3VdbeCurrentAddr(v)+2);
22306 VdbeCoverage(v);
22307 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
22308 }else{
22309 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
22310 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
22311 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
22312 }
22313 }
22314 }
22315 sqlite3ReleaseTempReg(pParse, r1);
22316 sqlite3ReleaseTempReg(pParse, r2);
22317 }
22318 if( pKeyInfo ){
22319 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
22320 }
22321 break;
22322 }
22323
22324 case TK_EXISTS:
22325 case TK_SELECT:
22326 default: {
22327 /* If this has to be a scalar SELECT. Generate code to put the
22328 ** value of this select in a memory cell and record the number
22329 ** of the memory cell in iColumn. If this is an EXISTS, write
22330 ** an integer 0 (not exists) or 1 (exists) into a memory cell
22331 ** and record that memory cell in iColumn.
22332 */
22333 Select *pSel; /* SELECT statement to encode */
22334 SelectDest dest; /* How to deal with SELECt result */
22335
22336 testcase( pExpr->op==TK_EXISTS );
22337 testcase( pExpr->op==TK_SELECT );
22338 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
22339
22340 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
22341 pSel = pExpr->x.pSelect;
22342 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
22343 if( pExpr->op==TK_SELECT ){
22344 dest.eDest = SRT_Mem;
22345 dest.iSdst = dest.iSDParm;
22346 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
22347 VdbeComment((v, "Init subquery result"));
22348 }else{
22349 dest.eDest = SRT_Exists;
22350 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
22351 VdbeComment((v, "Init EXISTS result"));
22352 }
22353 sqlite3ExprDelete(pParse->db, pSel->pLimit);
22354 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
22355 &sqlite3IntTokens[1]);
22356 pSel->iLimit = 0;
22357 pSel->selFlags &= ~SF_MultiValue;
22358 if( sqlite3Select(pParse, pSel, &dest) ){
22359 return 0;
22360 }
22361 rReg = dest.iSDParm;
22362 ExprSetVVAProperty(pExpr, EP_NoReduce);
22363 break;
22364 }
22365 }
22366
22367 if( rHasNullFlag ){
22368 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
22369 }
22370
22371 if( jmpIfDynamic>=0 ){
22372 sqlite3VdbeJumpHere(v, jmpIfDynamic);
22373 }
22374 sqlite3ExprCachePop(pParse);
22375
22376 return rReg;
22377 }
22378 #endif /* SQLITE_OMIT_SUBQUERY */
22379
22380 #ifndef SQLITE_OMIT_SUBQUERY
22381 /*
22382 ** Generate code for an IN expression.
22383 **
22384 ** x IN (SELECT ...)
22385 ** x IN (value, value, ...)
22386 **
22387 ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS)
22388 ** is an array of zero or more values. The expression is true if the LHS is
22389 ** contained within the RHS. The value of the expression is unknown (NULL)
22390 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
22391 ** RHS contains one or more NULL values.
22392 **
22393 ** This routine generates code that jumps to destIfFalse if the LHS is not
22394 ** contained within the RHS. If due to NULLs we cannot determine if the LHS
22395 ** is contained in the RHS then jump to destIfNull. If the LHS is contained
22396 ** within the RHS then fall through.
22397 */
22398 static void sqlite3ExprCodeIN(
22399 Parse *pParse, /* Parsing and code generating context */
22400 Expr *pExpr, /* The IN expression */
22401 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
22402 int destIfNull /* Jump here if the results are unknown due to NULLs */
22403 ){
22404 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
22405 char affinity; /* Comparison affinity to use */
22406 int eType; /* Type of the RHS */
22407 int r1; /* Temporary use register */
22408 Vdbe *v; /* Statement under construction */
22409
22410 /* Compute the RHS. After this step, the table with cursor
22411 ** pExpr->iTable will contains the values that make up the RHS.
22412 */
22413 v = pParse->pVdbe;
22414 assert( v!=0 ); /* OOM detected prior to this routine */
22415 VdbeNoopComment((v, "begin IN expr"));
22416 eType = sqlite3FindInIndex(pParse, pExpr,
22417 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
22418 destIfFalse==destIfNull ? 0 : &rRhsHasNull);
22419
22420 /* Figure out the affinity to use to create a key from the results
22421 ** of the expression. affinityStr stores a static string suitable for
22422 ** P4 of OP_MakeRecord.
22423 */
22424 affinity = comparisonAffinity(pExpr);
22425
22426 /* Code the LHS, the <expr> from "<expr> IN (...)".
22427 */
22428 sqlite3ExprCachePush(pParse);
22429 r1 = sqlite3GetTempReg(pParse);
22430 sqlite3ExprCode(pParse, pExpr->pLeft, r1);
22431
22432 /* If sqlite3FindInIndex() did not find or create an index that is
22433 ** suitable for evaluating the IN operator, then evaluate using a
22434 ** sequence of comparisons.
22435 */
22436 if( eType==IN_INDEX_NOOP ){
22437 ExprList *pList = pExpr->x.pList;
22438 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
22439 int labelOk = sqlite3VdbeMakeLabel(v);
22440 int r2, regToFree;
22441 int regCkNull = 0;
22442 int ii;
22443 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
22444 if( destIfNull!=destIfFalse ){
22445 regCkNull = sqlite3GetTempReg(pParse);
22446 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
22447 }
22448 for(ii=0; ii<pList->nExpr; ii++){
22449 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
22450 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
22451 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
22452 }
22453 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
22454 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
22455 (void*)pColl, P4_COLLSEQ);
22456 VdbeCoverageIf(v, ii<pList->nExpr-1);
22457 VdbeCoverageIf(v, ii==pList->nExpr-1);
22458 sqlite3VdbeChangeP5(v, affinity);
22459 }else{
22460 assert( destIfNull==destIfFalse );
22461 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
22462 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
22463 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
22464 }
22465 sqlite3ReleaseTempReg(pParse, regToFree);
22466 }
22467 if( regCkNull ){
22468 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
22469 sqlite3VdbeGoto(v, destIfFalse);
22470 }
22471 sqlite3VdbeResolveLabel(v, labelOk);
22472 sqlite3ReleaseTempReg(pParse, regCkNull);
22473 }else{
22474
22475 /* If the LHS is NULL, then the result is either false or NULL depending
22476 ** on whether the RHS is empty or not, respectively.
22477 */
22478 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
22479 if( destIfNull==destIfFalse ){
22480 /* Shortcut for the common case where the false and NULL outcomes are
22481 ** the same. */
22482 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
22483 }else{
22484 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
22485 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
22486 VdbeCoverage(v);
22487 sqlite3VdbeGoto(v, destIfNull);
22488 sqlite3VdbeJumpHere(v, addr1);
22489 }
22490 }
22491
22492 if( eType==IN_INDEX_ROWID ){
22493 /* In this case, the RHS is the ROWID of table b-tree
22494 */
22495 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
22496 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
22497 VdbeCoverage(v);
22498 }else{
22499 /* In this case, the RHS is an index b-tree.
22500 */
22501 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
22502
22503 /* If the set membership test fails, then the result of the
22504 ** "x IN (...)" expression must be either 0 or NULL. If the set
22505 ** contains no NULL values, then the result is 0. If the set
22506 ** contains one or more NULL values, then the result of the
22507 ** expression is also NULL.
22508 */
22509 assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
22510 if( rRhsHasNull==0 ){
22511 /* This branch runs if it is known at compile time that the RHS
22512 ** cannot contain NULL values. This happens as the result
22513 ** of a "NOT NULL" constraint in the database schema.
22514 **
22515 ** Also run this branch if NULL is equivalent to FALSE
22516 ** for this particular IN operator.
22517 */
22518 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
22519 VdbeCoverage(v);
22520 }else{
22521 /* In this branch, the RHS of the IN might contain a NULL and
22522 ** the presence of a NULL on the RHS makes a difference in the
22523 ** outcome.
22524 */
22525 int addr1;
22526
22527 /* First check to see if the LHS is contained in the RHS. If so,
22528 ** then the answer is TRUE the presence of NULLs in the RHS does
22529 ** not matter. If the LHS is not contained in the RHS, then the
22530 ** answer is NULL if the RHS contains NULLs and the answer is
22531 ** FALSE if the RHS is NULL-free.
22532 */
22533 addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
22534 VdbeCoverage(v);
22535 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
22536 VdbeCoverage(v);
22537 sqlite3VdbeGoto(v, destIfFalse);
22538 sqlite3VdbeJumpHere(v, addr1);
22539 }
22540 }
22541 }
22542 sqlite3ReleaseTempReg(pParse, r1);
22543 sqlite3ExprCachePop(pParse);
22544 VdbeComment((v, "end IN expr"));
22545 }
22546 #endif /* SQLITE_OMIT_SUBQUERY */
22547
22548 #ifndef SQLITE_OMIT_FLOATING_POINT
22549 /*
22550 ** Generate an instruction that will put the floating point
22551 ** value described by z[0..n-1] into register iMem.
22552 **
22553 ** The z[] string will probably not be zero-terminated. But the
22554 ** z[n] character is guaranteed to be something that does not look
22555 ** like the continuation of the number.
22556 */
22557 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
22558 if( ALWAYS(z!=0) ){
22559 double value;
22560 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
22561 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
22562 if( negateFlag ) value = -value;
22563 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
22564 }
22565 }
22566 #endif
22567
22568
22569 /*
22570 ** Generate an instruction that will put the integer describe by
22571 ** text z[0..n-1] into register iMem.
22572 **
22573 ** Expr.u.zToken is always UTF8 and zero-terminated.
22574 */
22575 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
22576 Vdbe *v = pParse->pVdbe;
22577 if( pExpr->flags & EP_IntValue ){
22578 int i = pExpr->u.iValue;
22579 assert( i>=0 );
22580 if( negFlag ) i = -i;
22581 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
22582 }else{
22583 int c;
22584 i64 value;
22585 const char *z = pExpr->u.zToken;
22586 assert( z!=0 );
22587 c = sqlite3DecOrHexToI64(z, &value);
22588 if( c==0 || (c==2 && negFlag) ){
22589 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
22590 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
22591 }else{
22592 #ifdef SQLITE_OMIT_FLOATING_POINT
22593 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
22594 #else
22595 #ifndef SQLITE_OMIT_HEX_INTEGER
22596 if( sqlite3_strnicmp(z,"0x",2)==0 ){
22597 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
22598 }else
22599 #endif
22600 {
22601 codeReal(v, z, negFlag, iMem);
22602 }
22603 #endif
22604 }
22605 }
22606 }
22607
22608 /*
22609 ** Clear a cache entry.
22610 */
22611 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
22612 if( p->tempReg ){
22613 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
22614 pParse->aTempReg[pParse->nTempReg++] = p->iReg;
22615 }
22616 p->tempReg = 0;
22617 }
22618 }
22619
22620
22621 /*
22622 ** Record in the column cache that a particular column from a
22623 ** particular table is stored in a particular register.
22624 */
22625 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
22626 int i;
22627 int minLru;
22628 int idxLru;
22629 struct yColCache *p;
22630
22631 /* Unless an error has occurred, register numbers are always positive. */
22632 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
22633 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
22634
22635 /* The SQLITE_ColumnCache flag disables the column cache. This is used
22636 ** for testing only - to verify that SQLite always gets the same answer
22637 ** with and without the column cache.
22638 */
22639 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
22640
22641 /* First replace any existing entry.
22642 **
22643 ** Actually, the way the column cache is currently used, we are guaranteed
22644 ** that the object will never already be in cache. Verify this guarantee.
22645 */
22646 #ifndef NDEBUG
22647 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22648 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
22649 }
22650 #endif
22651
22652 /* Find an empty slot and replace it */
22653 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22654 if( p->iReg==0 ){
22655 p->iLevel = pParse->iCacheLevel;
22656 p->iTable = iTab;
22657 p->iColumn = iCol;
22658 p->iReg = iReg;
22659 p->tempReg = 0;
22660 p->lru = pParse->iCacheCnt++;
22661 return;
22662 }
22663 }
22664
22665 /* Replace the last recently used */
22666 minLru = 0x7fffffff;
22667 idxLru = -1;
22668 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22669 if( p->lru<minLru ){
22670 idxLru = i;
22671 minLru = p->lru;
22672 }
22673 }
22674 if( ALWAYS(idxLru>=0) ){
22675 p = &pParse->aColCache[idxLru];
22676 p->iLevel = pParse->iCacheLevel;
22677 p->iTable = iTab;
22678 p->iColumn = iCol;
22679 p->iReg = iReg;
22680 p->tempReg = 0;
22681 p->lru = pParse->iCacheCnt++;
22682 return;
22683 }
22684 }
22685
22686 /*
22687 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
22688 ** Purge the range of registers from the column cache.
22689 */
22690 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
22691 int i;
22692 int iLast = iReg + nReg - 1;
22693 struct yColCache *p;
22694 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22695 int r = p->iReg;
22696 if( r>=iReg && r<=iLast ){
22697 cacheEntryClear(pParse, p);
22698 p->iReg = 0;
22699 }
22700 }
22701 }
22702
22703 /*
22704 ** Remember the current column cache context. Any new entries added
22705 ** added to the column cache after this call are removed when the
22706 ** corresponding pop occurs.
22707 */
22708 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
22709 pParse->iCacheLevel++;
22710 #ifdef SQLITE_DEBUG
22711 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
22712 printf("PUSH to %d\n", pParse->iCacheLevel);
22713 }
22714 #endif
22715 }
22716
22717 /*
22718 ** Remove from the column cache any entries that were added since the
22719 ** the previous sqlite3ExprCachePush operation. In other words, restore
22720 ** the cache to the state it was in prior the most recent Push.
22721 */
22722 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
22723 int i;
22724 struct yColCache *p;
22725 assert( pParse->iCacheLevel>=1 );
22726 pParse->iCacheLevel--;
22727 #ifdef SQLITE_DEBUG
22728 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
22729 printf("POP to %d\n", pParse->iCacheLevel);
22730 }
22731 #endif
22732 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22733 if( p->iReg && p->iLevel>pParse->iCacheLevel ){
22734 cacheEntryClear(pParse, p);
22735 p->iReg = 0;
22736 }
22737 }
22738 }
22739
22740 /*
22741 ** When a cached column is reused, make sure that its register is
22742 ** no longer available as a temp register. ticket #3879: that same
22743 ** register might be in the cache in multiple places, so be sure to
22744 ** get them all.
22745 */
22746 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
22747 int i;
22748 struct yColCache *p;
22749 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22750 if( p->iReg==iReg ){
22751 p->tempReg = 0;
22752 }
22753 }
22754 }
22755
22756 /* Generate code that will load into register regOut a value that is
22757 ** appropriate for the iIdxCol-th column of index pIdx.
22758 */
22759 SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
22760 Parse *pParse, /* The parsing context */
22761 Index *pIdx, /* The index whose column is to be loaded */
22762 int iTabCur, /* Cursor pointing to a table row */
22763 int iIdxCol, /* The column of the index to be loaded */
22764 int regOut /* Store the index column value in this register */
22765 ){
22766 i16 iTabCol = pIdx->aiColumn[iIdxCol];
22767 if( iTabCol==XN_EXPR ){
22768 assert( pIdx->aColExpr );
22769 assert( pIdx->aColExpr->nExpr>iIdxCol );
22770 pParse->iSelfTab = iTabCur;
22771 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
22772 }else{
22773 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
22774 iTabCol, regOut);
22775 }
22776 }
22777
22778 /*
22779 ** Generate code to extract the value of the iCol-th column of a table.
22780 */
22781 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
22782 Vdbe *v, /* The VDBE under construction */
22783 Table *pTab, /* The table containing the value */
22784 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
22785 int iCol, /* Index of the column to extract */
22786 int regOut /* Extract the value into this register */
22787 ){
22788 if( iCol<0 || iCol==pTab->iPKey ){
22789 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
22790 }else{
22791 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
22792 int x = iCol;
22793 if( !HasRowid(pTab) ){
22794 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
22795 }
22796 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
22797 }
22798 if( iCol>=0 ){
22799 sqlite3ColumnDefault(v, pTab, iCol, regOut);
22800 }
22801 }
22802
22803 /*
22804 ** Generate code that will extract the iColumn-th column from
22805 ** table pTab and store the column value in a register.
22806 **
22807 ** An effort is made to store the column value in register iReg. This
22808 ** is not garanteeed for GetColumn() - the result can be stored in
22809 ** any register. But the result is guaranteed to land in register iReg
22810 ** for GetColumnToReg().
22811 **
22812 ** There must be an open cursor to pTab in iTable when this routine
22813 ** is called. If iColumn<0 then code is generated that extracts the rowid.
22814 */
22815 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
22816 Parse *pParse, /* Parsing and code generating context */
22817 Table *pTab, /* Description of the table we are reading from */
22818 int iColumn, /* Index of the table column */
22819 int iTable, /* The cursor pointing to the table */
22820 int iReg, /* Store results here */
22821 u8 p5 /* P5 value for OP_Column + FLAGS */
22822 ){
22823 Vdbe *v = pParse->pVdbe;
22824 int i;
22825 struct yColCache *p;
22826
22827 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22828 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
22829 p->lru = pParse->iCacheCnt++;
22830 sqlite3ExprCachePinRegister(pParse, p->iReg);
22831 return p->iReg;
22832 }
22833 }
22834 assert( v!=0 );
22835 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
22836 if( p5 ){
22837 sqlite3VdbeChangeP5(v, p5);
22838 }else{
22839 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
22840 }
22841 return iReg;
22842 }
22843 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnToReg(
22844 Parse *pParse, /* Parsing and code generating context */
22845 Table *pTab, /* Description of the table we are reading from */
22846 int iColumn, /* Index of the table column */
22847 int iTable, /* The cursor pointing to the table */
22848 int iReg /* Store results here */
22849 ){
22850 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
22851 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
22852 }
22853
22854
22855 /*
22856 ** Clear all column cache entries.
22857 */
22858 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
22859 int i;
22860 struct yColCache *p;
22861
22862 #if SQLITE_DEBUG
22863 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
22864 printf("CLEAR\n");
22865 }
22866 #endif
22867 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22868 if( p->iReg ){
22869 cacheEntryClear(pParse, p);
22870 p->iReg = 0;
22871 }
22872 }
22873 }
22874
22875 /*
22876 ** Record the fact that an affinity change has occurred on iCount
22877 ** registers starting with iStart.
22878 */
22879 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, in t iCount){
22880 sqlite3ExprCacheRemove(pParse, iStart, iCount);
22881 }
22882
22883 /*
22884 ** Generate code to move content from registers iFrom...iFrom+nReg-1
22885 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
22886 */
22887 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int n Reg){
22888 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
22889 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
22890 sqlite3ExprCacheRemove(pParse, iFrom, nReg);
22891 }
22892
22893 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
22894 /*
22895 ** Return true if any register in the range iFrom..iTo (inclusive)
22896 ** is used as part of the column cache.
22897 **
22898 ** This routine is used within assert() and testcase() macros only
22899 ** and does not appear in a normal build.
22900 */
22901 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
22902 int i;
22903 struct yColCache *p;
22904 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
22905 int r = p->iReg;
22906 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
22907 }
22908 return 0;
22909 }
22910 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
22911
22912 /*
22913 ** Convert an expression node to a TK_REGISTER
22914 */
22915 static void exprToRegister(Expr *p, int iReg){
22916 p->op2 = p->op;
22917 p->op = TK_REGISTER;
22918 p->iTable = iReg;
22919 ExprClearProperty(p, EP_Skip);
22920 }
22921
22922 /*
22923 ** Generate code into the current Vdbe to evaluate the given
22924 ** expression. Attempt to store the results in register "target".
22925 ** Return the register where results are stored.
22926 **
22927 ** With this routine, there is no guarantee that results will
22928 ** be stored in target. The result might be stored in some other
22929 ** register if it is convenient to do so. The calling function
22930 ** must check the return code and move the results to the desired
22931 ** register.
22932 */
22933 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target) {
22934 Vdbe *v = pParse->pVdbe; /* The VM under construction */
22935 int op; /* The opcode being coded */
22936 int inReg = target; /* Results stored in register inReg */
22937 int regFree1 = 0; /* If non-zero free this temporary register */
22938 int regFree2 = 0; /* If non-zero free this temporary register */
22939 int r1, r2, r3, r4; /* Various register numbers */
22940 sqlite3 *db = pParse->db; /* The database connection */
22941 Expr tempX; /* Temporary expression node */
22942
22943 assert( target>0 && target<=pParse->nMem );
22944 if( v==0 ){
22945 assert( pParse->db->mallocFailed );
22946 return 0;
22947 }
22948
22949 if( pExpr==0 ){
22950 op = TK_NULL;
22951 }else{
22952 op = pExpr->op;
22953 }
22954 switch( op ){
22955 case TK_AGG_COLUMN: {
22956 AggInfo *pAggInfo = pExpr->pAggInfo;
22957 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
22958 if( !pAggInfo->directMode ){
22959 assert( pCol->iMem>0 );
22960 inReg = pCol->iMem;
22961 break;
22962 }else if( pAggInfo->useSortingIdx ){
22963 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
22964 pCol->iSorterColumn, target);
22965 break;
22966 }
22967 /* Otherwise, fall thru into the TK_COLUMN case */
22968 }
22969 case TK_COLUMN: {
22970 int iTab = pExpr->iTable;
22971 if( iTab<0 ){
22972 if( pParse->ckBase>0 ){
22973 /* Generating CHECK constraints or inserting into partial index */
22974 inReg = pExpr->iColumn + pParse->ckBase;
22975 break;
22976 }else{
22977 /* Coding an expression that is part of an index where column names
22978 ** in the index refer to the table to which the index belongs */
22979 iTab = pParse->iSelfTab;
22980 }
22981 }
22982 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
22983 pExpr->iColumn, iTab, target,
22984 pExpr->op2);
22985 break;
22986 }
22987 case TK_INTEGER: {
22988 codeInteger(pParse, pExpr, 0, target);
22989 break;
22990 }
22991 #ifndef SQLITE_OMIT_FLOATING_POINT
22992 case TK_FLOAT: {
22993 assert( !ExprHasProperty(pExpr, EP_IntValue) );
22994 codeReal(v, pExpr->u.zToken, 0, target);
22995 break;
22996 }
22997 #endif
22998 case TK_STRING: {
22999 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23000 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
23001 break;
23002 }
23003 case TK_NULL: {
23004 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
23005 break;
23006 }
23007 #ifndef SQLITE_OMIT_BLOB_LITERAL
23008 case TK_BLOB: {
23009 int n;
23010 const char *z;
23011 char *zBlob;
23012 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23013 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
23014 assert( pExpr->u.zToken[1]=='\'' );
23015 z = &pExpr->u.zToken[2];
23016 n = sqlite3Strlen30(z) - 1;
23017 assert( z[n]=='\'' );
23018 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
23019 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
23020 break;
23021 }
23022 #endif
23023 case TK_VARIABLE: {
23024 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23025 assert( pExpr->u.zToken!=0 );
23026 assert( pExpr->u.zToken[0]!=0 );
23027 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
23028 if( pExpr->u.zToken[1]!=0 ){
23029 assert( pExpr->u.zToken[0]=='?'
23030 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
23031 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
23032 }
23033 break;
23034 }
23035 case TK_REGISTER: {
23036 inReg = pExpr->iTable;
23037 break;
23038 }
23039 #ifndef SQLITE_OMIT_CAST
23040 case TK_CAST: {
23041 /* Expressions of the form: CAST(pLeft AS token) */
23042 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
23043 if( inReg!=target ){
23044 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
23045 inReg = target;
23046 }
23047 sqlite3VdbeAddOp2(v, OP_Cast, target,
23048 sqlite3AffinityType(pExpr->u.zToken, 0));
23049 testcase( usedAsColumnCache(pParse, inReg, inReg) );
23050 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
23051 break;
23052 }
23053 #endif /* SQLITE_OMIT_CAST */
23054 case TK_LT:
23055 case TK_LE:
23056 case TK_GT:
23057 case TK_GE:
23058 case TK_NE:
23059 case TK_EQ: {
23060 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23061 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
23062 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
23063 r1, r2, inReg, SQLITE_STOREP2);
23064 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
23065 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
23066 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
23067 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
23068 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
23069 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
23070 testcase( regFree1==0 );
23071 testcase( regFree2==0 );
23072 break;
23073 }
23074 case TK_IS:
23075 case TK_ISNOT: {
23076 testcase( op==TK_IS );
23077 testcase( op==TK_ISNOT );
23078 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23079 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
23080 op = (op==TK_IS) ? TK_EQ : TK_NE;
23081 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
23082 r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
23083 VdbeCoverageIf(v, op==TK_EQ);
23084 VdbeCoverageIf(v, op==TK_NE);
23085 testcase( regFree1==0 );
23086 testcase( regFree2==0 );
23087 break;
23088 }
23089 case TK_AND:
23090 case TK_OR:
23091 case TK_PLUS:
23092 case TK_STAR:
23093 case TK_MINUS:
23094 case TK_REM:
23095 case TK_BITAND:
23096 case TK_BITOR:
23097 case TK_SLASH:
23098 case TK_LSHIFT:
23099 case TK_RSHIFT:
23100 case TK_CONCAT: {
23101 assert( TK_AND==OP_And ); testcase( op==TK_AND );
23102 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
23103 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
23104 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
23105 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
23106 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
23107 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
23108 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
23109 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
23110 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
23111 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
23112 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23113 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
23114 sqlite3VdbeAddOp3(v, op, r2, r1, target);
23115 testcase( regFree1==0 );
23116 testcase( regFree2==0 );
23117 break;
23118 }
23119 case TK_UMINUS: {
23120 Expr *pLeft = pExpr->pLeft;
23121 assert( pLeft );
23122 if( pLeft->op==TK_INTEGER ){
23123 codeInteger(pParse, pLeft, 1, target);
23124 #ifndef SQLITE_OMIT_FLOATING_POINT
23125 }else if( pLeft->op==TK_FLOAT ){
23126 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23127 codeReal(v, pLeft->u.zToken, 1, target);
23128 #endif
23129 }else{
23130 tempX.op = TK_INTEGER;
23131 tempX.flags = EP_IntValue|EP_TokenOnly;
23132 tempX.u.iValue = 0;
23133 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
23134 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
23135 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
23136 testcase( regFree2==0 );
23137 }
23138 inReg = target;
23139 break;
23140 }
23141 case TK_BITNOT:
23142 case TK_NOT: {
23143 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
23144 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
23145 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23146 testcase( regFree1==0 );
23147 inReg = target;
23148 sqlite3VdbeAddOp2(v, op, r1, inReg);
23149 break;
23150 }
23151 case TK_ISNULL:
23152 case TK_NOTNULL: {
23153 int addr;
23154 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
23155 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
23156 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
23157 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23158 testcase( regFree1==0 );
23159 addr = sqlite3VdbeAddOp1(v, op, r1);
23160 VdbeCoverageIf(v, op==TK_ISNULL);
23161 VdbeCoverageIf(v, op==TK_NOTNULL);
23162 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
23163 sqlite3VdbeJumpHere(v, addr);
23164 break;
23165 }
23166 case TK_AGG_FUNCTION: {
23167 AggInfo *pInfo = pExpr->pAggInfo;
23168 if( pInfo==0 ){
23169 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23170 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
23171 }else{
23172 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
23173 }
23174 break;
23175 }
23176 case TK_FUNCTION: {
23177 ExprList *pFarg; /* List of function arguments */
23178 int nFarg; /* Number of function arguments */
23179 FuncDef *pDef; /* The function definition object */
23180 int nId; /* Length of the function name in bytes */
23181 const char *zId; /* The function name */
23182 u32 constMask = 0; /* Mask of function arguments that are constant */
23183 int i; /* Loop counter */
23184 u8 enc = ENC(db); /* The text encoding used by this database */
23185 CollSeq *pColl = 0; /* A collating sequence */
23186
23187 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
23188 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
23189 pFarg = 0;
23190 }else{
23191 pFarg = pExpr->x.pList;
23192 }
23193 nFarg = pFarg ? pFarg->nExpr : 0;
23194 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23195 zId = pExpr->u.zToken;
23196 nId = sqlite3Strlen30(zId);
23197 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
23198 if( pDef==0 || pDef->xFunc==0 ){
23199 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
23200 break;
23201 }
23202
23203 /* Attempt a direct implementation of the built-in COALESCE() and
23204 ** IFNULL() functions. This avoids unnecessary evaluation of
23205 ** arguments past the first non-NULL argument.
23206 */
23207 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
23208 int endCoalesce = sqlite3VdbeMakeLabel(v);
23209 assert( nFarg>=2 );
23210 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
23211 for(i=1; i<nFarg; i++){
23212 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
23213 VdbeCoverage(v);
23214 sqlite3ExprCacheRemove(pParse, target, 1);
23215 sqlite3ExprCachePush(pParse);
23216 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
23217 sqlite3ExprCachePop(pParse);
23218 }
23219 sqlite3VdbeResolveLabel(v, endCoalesce);
23220 break;
23221 }
23222
23223 /* The UNLIKELY() function is a no-op. The result is the value
23224 ** of the first argument.
23225 */
23226 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
23227 assert( nFarg>=1 );
23228 inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
23229 break;
23230 }
23231
23232 for(i=0; i<nFarg; i++){
23233 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
23234 testcase( i==31 );
23235 constMask |= MASKBIT32(i);
23236 }
23237 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
23238 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
23239 }
23240 }
23241 if( pFarg ){
23242 if( constMask ){
23243 r1 = pParse->nMem+1;
23244 pParse->nMem += nFarg;
23245 }else{
23246 r1 = sqlite3GetTempRange(pParse, nFarg);
23247 }
23248
23249 /* For length() and typeof() functions with a column argument,
23250 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
23251 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
23252 ** loading.
23253 */
23254 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
23255 u8 exprOp;
23256 assert( nFarg==1 );
23257 assert( pFarg->a[0].pExpr!=0 );
23258 exprOp = pFarg->a[0].pExpr->op;
23259 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
23260 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
23261 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
23262 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
23263 pFarg->a[0].pExpr->op2 =
23264 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
23265 }
23266 }
23267
23268 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
23269 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
23270 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
23271 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
23272 }else{
23273 r1 = 0;
23274 }
23275 #ifndef SQLITE_OMIT_VIRTUALTABLE
23276 /* Possibly overload the function if the first argument is
23277 ** a virtual table column.
23278 **
23279 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
23280 ** second argument, not the first, as the argument to test to
23281 ** see if it is a column in a virtual table. This is done because
23282 ** the left operand of infix functions (the operand we want to
23283 ** control overloading) ends up as the second argument to the
23284 ** function. The expression "A glob B" is equivalent to
23285 ** "glob(B,A). We want to use the A in "A glob B" to test
23286 ** for function overloading. But we use the B term in "glob(B,A)".
23287 */
23288 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
23289 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
23290 }else if( nFarg>0 ){
23291 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
23292 }
23293 #endif
23294 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
23295 if( !pColl ) pColl = db->pDfltColl;
23296 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
23297 }
23298 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
23299 (char*)pDef, P4_FUNCDEF);
23300 sqlite3VdbeChangeP5(v, (u8)nFarg);
23301 if( nFarg && constMask==0 ){
23302 sqlite3ReleaseTempRange(pParse, r1, nFarg);
23303 }
23304 break;
23305 }
23306 #ifndef SQLITE_OMIT_SUBQUERY
23307 case TK_EXISTS:
23308 case TK_SELECT: {
23309 testcase( op==TK_EXISTS );
23310 testcase( op==TK_SELECT );
23311 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
23312 break;
23313 }
23314 case TK_IN: {
23315 int destIfFalse = sqlite3VdbeMakeLabel(v);
23316 int destIfNull = sqlite3VdbeMakeLabel(v);
23317 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
23318 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
23319 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
23320 sqlite3VdbeResolveLabel(v, destIfFalse);
23321 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
23322 sqlite3VdbeResolveLabel(v, destIfNull);
23323 break;
23324 }
23325 #endif /* SQLITE_OMIT_SUBQUERY */
23326
23327
23328 /*
23329 ** x BETWEEN y AND z
23330 **
23331 ** This is equivalent to
23332 **
23333 ** x>=y AND x<=z
23334 **
23335 ** X is stored in pExpr->pLeft.
23336 ** Y is stored in pExpr->pList->a[0].pExpr.
23337 ** Z is stored in pExpr->pList->a[1].pExpr.
23338 */
23339 case TK_BETWEEN: {
23340 Expr *pLeft = pExpr->pLeft;
23341 struct ExprList_item *pLItem = pExpr->x.pList->a;
23342 Expr *pRight = pLItem->pExpr;
23343
23344 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
23345 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
23346 testcase( regFree1==0 );
23347 testcase( regFree2==0 );
23348 r3 = sqlite3GetTempReg(pParse);
23349 r4 = sqlite3GetTempReg(pParse);
23350 codeCompare(pParse, pLeft, pRight, OP_Ge,
23351 r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v);
23352 pLItem++;
23353 pRight = pLItem->pExpr;
23354 sqlite3ReleaseTempReg(pParse, regFree2);
23355 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
23356 testcase( regFree2==0 );
23357 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
23358 VdbeCoverage(v);
23359 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
23360 sqlite3ReleaseTempReg(pParse, r3);
23361 sqlite3ReleaseTempReg(pParse, r4);
23362 break;
23363 }
23364 case TK_COLLATE:
23365 case TK_UPLUS: {
23366 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
23367 break;
23368 }
23369
23370 case TK_TRIGGER: {
23371 /* If the opcode is TK_TRIGGER, then the expression is a reference
23372 ** to a column in the new.* or old.* pseudo-tables available to
23373 ** trigger programs. In this case Expr.iTable is set to 1 for the
23374 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
23375 ** is set to the column of the pseudo-table to read, or to -1 to
23376 ** read the rowid field.
23377 **
23378 ** The expression is implemented using an OP_Param opcode. The p1
23379 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
23380 ** to reference another column of the old.* pseudo-table, where
23381 ** i is the index of the column. For a new.rowid reference, p1 is
23382 ** set to (n+1), where n is the number of columns in each pseudo-table.
23383 ** For a reference to any other column in the new.* pseudo-table, p1
23384 ** is set to (n+2+i), where n and i are as defined previously. For
23385 ** example, if the table on which triggers are being fired is
23386 ** declared as:
23387 **
23388 ** CREATE TABLE t1(a, b);
23389 **
23390 ** Then p1 is interpreted as follows:
23391 **
23392 ** p1==0 -> old.rowid p1==3 -> new.rowid
23393 ** p1==1 -> old.a p1==4 -> new.a
23394 ** p1==2 -> old.b p1==5 -> new.b
23395 */
23396 Table *pTab = pExpr->pTab;
23397 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
23398
23399 assert( pExpr->iTable==0 || pExpr->iTable==1 );
23400 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
23401 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
23402 assert( p1>=0 && p1<(pTab->nCol*2+2) );
23403
23404 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
23405 VdbeComment((v, "%s.%s -> $%d",
23406 (pExpr->iTable ? "new" : "old"),
23407 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
23408 target
23409 ));
23410
23411 #ifndef SQLITE_OMIT_FLOATING_POINT
23412 /* If the column has REAL affinity, it may currently be stored as an
23413 ** integer. Use OP_RealAffinity to make sure it is really real.
23414 **
23415 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
23416 ** floating point when extracting it from the record. */
23417 if( pExpr->iColumn>=0
23418 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
23419 ){
23420 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
23421 }
23422 #endif
23423 break;
23424 }
23425
23426
23427 /*
23428 ** Form A:
23429 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
23430 **
23431 ** Form B:
23432 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
23433 **
23434 ** Form A is can be transformed into the equivalent form B as follows:
23435 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
23436 ** WHEN x=eN THEN rN ELSE y END
23437 **
23438 ** X (if it exists) is in pExpr->pLeft.
23439 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
23440 ** odd. The Y is also optional. If the number of elements in x.pList
23441 ** is even, then Y is omitted and the "otherwise" result is NULL.
23442 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
23443 **
23444 ** The result of the expression is the Ri for the first matching Ei,
23445 ** or if there is no matching Ei, the ELSE term Y, or if there is
23446 ** no ELSE term, NULL.
23447 */
23448 default: assert( op==TK_CASE ); {
23449 int endLabel; /* GOTO label for end of CASE stmt */
23450 int nextCase; /* GOTO label for next WHEN clause */
23451 int nExpr; /* 2x number of WHEN terms */
23452 int i; /* Loop counter */
23453 ExprList *pEList; /* List of WHEN terms */
23454 struct ExprList_item *aListelem; /* Array of WHEN terms */
23455 Expr opCompare; /* The X==Ei expression */
23456 Expr *pX; /* The X expression */
23457 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
23458 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
23459
23460 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
23461 assert(pExpr->x.pList->nExpr > 0);
23462 pEList = pExpr->x.pList;
23463 aListelem = pEList->a;
23464 nExpr = pEList->nExpr;
23465 endLabel = sqlite3VdbeMakeLabel(v);
23466 if( (pX = pExpr->pLeft)!=0 ){
23467 tempX = *pX;
23468 testcase( pX->op==TK_COLUMN );
23469 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
23470 testcase( regFree1==0 );
23471 opCompare.op = TK_EQ;
23472 opCompare.pLeft = &tempX;
23473 pTest = &opCompare;
23474 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
23475 ** The value in regFree1 might get SCopy-ed into the file result.
23476 ** So make sure that the regFree1 register is not reused for other
23477 ** purposes and possibly overwritten. */
23478 regFree1 = 0;
23479 }
23480 for(i=0; i<nExpr-1; i=i+2){
23481 sqlite3ExprCachePush(pParse);
23482 if( pX ){
23483 assert( pTest!=0 );
23484 opCompare.pRight = aListelem[i].pExpr;
23485 }else{
23486 pTest = aListelem[i].pExpr;
23487 }
23488 nextCase = sqlite3VdbeMakeLabel(v);
23489 testcase( pTest->op==TK_COLUMN );
23490 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
23491 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
23492 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
23493 sqlite3VdbeGoto(v, endLabel);
23494 sqlite3ExprCachePop(pParse);
23495 sqlite3VdbeResolveLabel(v, nextCase);
23496 }
23497 if( (nExpr&1)!=0 ){
23498 sqlite3ExprCachePush(pParse);
23499 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
23500 sqlite3ExprCachePop(pParse);
23501 }else{
23502 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
23503 }
23504 assert( db->mallocFailed || pParse->nErr>0
23505 || pParse->iCacheLevel==iCacheLevel );
23506 sqlite3VdbeResolveLabel(v, endLabel);
23507 break;
23508 }
23509 #ifndef SQLITE_OMIT_TRIGGER
23510 case TK_RAISE: {
23511 assert( pExpr->affinity==OE_Rollback
23512 || pExpr->affinity==OE_Abort
23513 || pExpr->affinity==OE_Fail
23514 || pExpr->affinity==OE_Ignore
23515 );
23516 if( !pParse->pTriggerTab ){
23517 sqlite3ErrorMsg(pParse,
23518 "RAISE() may only be used within a trigger-program");
23519 return 0;
23520 }
23521 if( pExpr->affinity==OE_Abort ){
23522 sqlite3MayAbort(pParse);
23523 }
23524 assert( !ExprHasProperty(pExpr, EP_IntValue) );
23525 if( pExpr->affinity==OE_Ignore ){
23526 sqlite3VdbeAddOp4(
23527 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
23528 VdbeCoverage(v);
23529 }else{
23530 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
23531 pExpr->affinity, pExpr->u.zToken, 0, 0);
23532 }
23533
23534 break;
23535 }
23536 #endif
23537 }
23538 sqlite3ReleaseTempReg(pParse, regFree1);
23539 sqlite3ReleaseTempReg(pParse, regFree2);
23540 return inReg;
23541 }
23542
23543 /*
23544 ** Factor out the code of the given expression to initialization time.
23545 */
23546 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
23547 Parse *pParse, /* Parsing context */
23548 Expr *pExpr, /* The expression to code when the VDBE initializes */
23549 int regDest, /* Store the value in this register */
23550 u8 reusable /* True if this expression is reusable */
23551 ){
23552 ExprList *p;
23553 assert( ConstFactorOk(pParse) );
23554 p = pParse->pConstExpr;
23555 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
23556 p = sqlite3ExprListAppend(pParse, p, pExpr);
23557 if( p ){
23558 struct ExprList_item *pItem = &p->a[p->nExpr-1];
23559 pItem->u.iConstExprReg = regDest;
23560 pItem->reusable = reusable;
23561 }
23562 pParse->pConstExpr = p;
23563 }
23564
23565 /*
23566 ** Generate code to evaluate an expression and store the results
23567 ** into a register. Return the register number where the results
23568 ** are stored.
23569 **
23570 ** If the register is a temporary register that can be deallocated,
23571 ** then write its number into *pReg. If the result register is not
23572 ** a temporary, then set *pReg to zero.
23573 **
23574 ** If pExpr is a constant, then this routine might generate this
23575 ** code to fill the register in the initialization section of the
23576 ** VDBE program, in order to factor it out of the evaluation loop.
23577 */
23578 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
23579 int r2;
23580 pExpr = sqlite3ExprSkipCollate(pExpr);
23581 if( ConstFactorOk(pParse)
23582 && pExpr->op!=TK_REGISTER
23583 && sqlite3ExprIsConstantNotJoin(pExpr)
23584 ){
23585 ExprList *p = pParse->pConstExpr;
23586 int i;
23587 *pReg = 0;
23588 if( p ){
23589 struct ExprList_item *pItem;
23590 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
23591 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
23592 return pItem->u.iConstExprReg;
23593 }
23594 }
23595 }
23596 r2 = ++pParse->nMem;
23597 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
23598 }else{
23599 int r1 = sqlite3GetTempReg(pParse);
23600 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
23601 if( r2==r1 ){
23602 *pReg = r1;
23603 }else{
23604 sqlite3ReleaseTempReg(pParse, r1);
23605 *pReg = 0;
23606 }
23607 }
23608 return r2;
23609 }
23610
23611 /*
23612 ** Generate code that will evaluate expression pExpr and store the
23613 ** results in register target. The results are guaranteed to appear
23614 ** in register target.
23615 */
23616 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
23617 int inReg;
23618
23619 assert( target>0 && target<=pParse->nMem );
23620 if( pExpr && pExpr->op==TK_REGISTER ){
23621 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
23622 }else{
23623 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
23624 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
23625 if( inReg!=target && pParse->pVdbe ){
23626 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
23627 }
23628 }
23629 }
23630
23631 /*
23632 ** Make a transient copy of expression pExpr and then code it using
23633 ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
23634 ** except that the input expression is guaranteed to be unchanged.
23635 */
23636 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
23637 sqlite3 *db = pParse->db;
23638 pExpr = sqlite3ExprDup(db, pExpr, 0);
23639 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
23640 sqlite3ExprDelete(db, pExpr);
23641 }
23642
23643 /*
23644 ** Generate code that will evaluate expression pExpr and store the
23645 ** results in register target. The results are guaranteed to appear
23646 ** in register target. If the expression is constant, then this routine
23647 ** might choose to code the expression at initialization time.
23648 */
23649 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int ta rget){
23650 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
23651 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
23652 }else{
23653 sqlite3ExprCode(pParse, pExpr, target);
23654 }
23655 }
23656
23657 /*
23658 ** Generate code that evaluates the given expression and puts the result
23659 ** in register target.
23660 **
23661 ** Also make a copy of the expression results into another "cache" register
23662 ** and modify the expression so that the next time it is evaluated,
23663 ** the result is a copy of the cache register.
23664 **
23665 ** This routine is used for expressions that are used multiple
23666 ** times. They are evaluated once and the results of the expression
23667 ** are reused.
23668 */
23669 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int targ et){
23670 Vdbe *v = pParse->pVdbe;
23671 int iMem;
23672
23673 assert( target>0 );
23674 assert( pExpr->op!=TK_REGISTER );
23675 sqlite3ExprCode(pParse, pExpr, target);
23676 iMem = ++pParse->nMem;
23677 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
23678 exprToRegister(pExpr, iMem);
23679 }
23680
23681 /*
23682 ** Generate code that pushes the value of every element of the given
23683 ** expression list into a sequence of registers beginning at target.
23684 **
23685 ** Return the number of elements evaluated.
23686 **
23687 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
23688 ** filled using OP_SCopy. OP_Copy must be used instead.
23689 **
23690 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
23691 ** factored out into initialization code.
23692 **
23693 ** The SQLITE_ECEL_REF flag means that expressions in the list with
23694 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
23695 ** in registers at srcReg, and so the value can be copied from there.
23696 */
23697 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
23698 Parse *pParse, /* Parsing context */
23699 ExprList *pList, /* The expression list to be coded */
23700 int target, /* Where to write results */
23701 int srcReg, /* Source registers if SQLITE_ECEL_REF */
23702 u8 flags /* SQLITE_ECEL_* flags */
23703 ){
23704 struct ExprList_item *pItem;
23705 int i, j, n;
23706 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
23707 Vdbe *v = pParse->pVdbe;
23708 assert( pList!=0 );
23709 assert( target>0 );
23710 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
23711 n = pList->nExpr;
23712 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
23713 for(pItem=pList->a, i=0; i<n; i++, pItem++){
23714 Expr *pExpr = pItem->pExpr;
23715 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){
23716 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
23717 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
23718 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
23719 }else{
23720 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
23721 if( inReg!=target+i ){
23722 VdbeOp *pOp;
23723 if( copyOp==OP_Copy
23724 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
23725 && pOp->p1+pOp->p3+1==inReg
23726 && pOp->p2+pOp->p3+1==target+i
23727 ){
23728 pOp->p3++;
23729 }else{
23730 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
23731 }
23732 }
23733 }
23734 }
23735 return n;
23736 }
23737
23738 /*
23739 ** Generate code for a BETWEEN operator.
23740 **
23741 ** x BETWEEN y AND z
23742 **
23743 ** The above is equivalent to
23744 **
23745 ** x>=y AND x<=z
23746 **
23747 ** Code it as such, taking care to do the common subexpression
23748 ** elimination of x.
23749 */
23750 static void exprCodeBetween(
23751 Parse *pParse, /* Parsing and code generating context */
23752 Expr *pExpr, /* The BETWEEN expression */
23753 int dest, /* Jump here if the jump is taken */
23754 int jumpIfTrue, /* Take the jump if the BETWEEN is true */
23755 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
23756 ){
23757 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
23758 Expr compLeft; /* The x>=y term */
23759 Expr compRight; /* The x<=z term */
23760 Expr exprX; /* The x subexpression */
23761 int regFree1 = 0; /* Temporary use register */
23762
23763 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
23764 exprX = *pExpr->pLeft;
23765 exprAnd.op = TK_AND;
23766 exprAnd.pLeft = &compLeft;
23767 exprAnd.pRight = &compRight;
23768 compLeft.op = TK_GE;
23769 compLeft.pLeft = &exprX;
23770 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
23771 compRight.op = TK_LE;
23772 compRight.pLeft = &exprX;
23773 compRight.pRight = pExpr->x.pList->a[1].pExpr;
23774 exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
23775 if( jumpIfTrue ){
23776 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
23777 }else{
23778 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
23779 }
23780 sqlite3ReleaseTempReg(pParse, regFree1);
23781
23782 /* Ensure adequate test coverage */
23783 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
23784 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
23785 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
23786 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
23787 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
23788 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
23789 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
23790 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
23791 }
23792
23793 /*
23794 ** Generate code for a boolean expression such that a jump is made
23795 ** to the label "dest" if the expression is true but execution
23796 ** continues straight thru if the expression is false.
23797 **
23798 ** If the expression evaluates to NULL (neither true nor false), then
23799 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
23800 **
23801 ** This code depends on the fact that certain token values (ex: TK_EQ)
23802 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
23803 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
23804 ** the make process cause these values to align. Assert()s in the code
23805 ** below verify that the numbers are aligned correctly.
23806 */
23807 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
23808 Vdbe *v = pParse->pVdbe;
23809 int op = 0;
23810 int regFree1 = 0;
23811 int regFree2 = 0;
23812 int r1, r2;
23813
23814 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
23815 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
23816 if( NEVER(pExpr==0) ) return; /* No way this can happen */
23817 op = pExpr->op;
23818 switch( op ){
23819 case TK_AND: {
23820 int d2 = sqlite3VdbeMakeLabel(v);
23821 testcase( jumpIfNull==0 );
23822 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
23823 sqlite3ExprCachePush(pParse);
23824 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
23825 sqlite3VdbeResolveLabel(v, d2);
23826 sqlite3ExprCachePop(pParse);
23827 break;
23828 }
23829 case TK_OR: {
23830 testcase( jumpIfNull==0 );
23831 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
23832 sqlite3ExprCachePush(pParse);
23833 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
23834 sqlite3ExprCachePop(pParse);
23835 break;
23836 }
23837 case TK_NOT: {
23838 testcase( jumpIfNull==0 );
23839 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
23840 break;
23841 }
23842 case TK_LT:
23843 case TK_LE:
23844 case TK_GT:
23845 case TK_GE:
23846 case TK_NE:
23847 case TK_EQ: {
23848 testcase( jumpIfNull==0 );
23849 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23850 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
23851 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
23852 r1, r2, dest, jumpIfNull);
23853 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
23854 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
23855 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
23856 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
23857 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
23858 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
23859 testcase( regFree1==0 );
23860 testcase( regFree2==0 );
23861 break;
23862 }
23863 case TK_IS:
23864 case TK_ISNOT: {
23865 testcase( op==TK_IS );
23866 testcase( op==TK_ISNOT );
23867 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23868 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
23869 op = (op==TK_IS) ? TK_EQ : TK_NE;
23870 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
23871 r1, r2, dest, SQLITE_NULLEQ);
23872 VdbeCoverageIf(v, op==TK_EQ);
23873 VdbeCoverageIf(v, op==TK_NE);
23874 testcase( regFree1==0 );
23875 testcase( regFree2==0 );
23876 break;
23877 }
23878 case TK_ISNULL:
23879 case TK_NOTNULL: {
23880 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
23881 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
23882 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
23883 sqlite3VdbeAddOp2(v, op, r1, dest);
23884 VdbeCoverageIf(v, op==TK_ISNULL);
23885 VdbeCoverageIf(v, op==TK_NOTNULL);
23886 testcase( regFree1==0 );
23887 break;
23888 }
23889 case TK_BETWEEN: {
23890 testcase( jumpIfNull==0 );
23891 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
23892 break;
23893 }
23894 #ifndef SQLITE_OMIT_SUBQUERY
23895 case TK_IN: {
23896 int destIfFalse = sqlite3VdbeMakeLabel(v);
23897 int destIfNull = jumpIfNull ? dest : destIfFalse;
23898 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
23899 sqlite3VdbeGoto(v, dest);
23900 sqlite3VdbeResolveLabel(v, destIfFalse);
23901 break;
23902 }
23903 #endif
23904 default: {
23905 if( exprAlwaysTrue(pExpr) ){
23906 sqlite3VdbeGoto(v, dest);
23907 }else if( exprAlwaysFalse(pExpr) ){
23908 /* No-op */
23909 }else{
23910 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
23911 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
23912 VdbeCoverage(v);
23913 testcase( regFree1==0 );
23914 testcase( jumpIfNull==0 );
23915 }
23916 break;
23917 }
23918 }
23919 sqlite3ReleaseTempReg(pParse, regFree1);
23920 sqlite3ReleaseTempReg(pParse, regFree2);
23921 }
23922
23923 /*
23924 ** Generate code for a boolean expression such that a jump is made
23925 ** to the label "dest" if the expression is false but execution
23926 ** continues straight thru if the expression is true.
23927 **
23928 ** If the expression evaluates to NULL (neither true nor false) then
23929 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
23930 ** is 0.
23931 */
23932 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
23933 Vdbe *v = pParse->pVdbe;
23934 int op = 0;
23935 int regFree1 = 0;
23936 int regFree2 = 0;
23937 int r1, r2;
23938
23939 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
23940 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
23941 if( pExpr==0 ) return;
23942
23943 /* The value of pExpr->op and op are related as follows:
23944 **
23945 ** pExpr->op op
23946 ** --------- ----------
23947 ** TK_ISNULL OP_NotNull
23948 ** TK_NOTNULL OP_IsNull
23949 ** TK_NE OP_Eq
23950 ** TK_EQ OP_Ne
23951 ** TK_GT OP_Le
23952 ** TK_LE OP_Gt
23953 ** TK_GE OP_Lt
23954 ** TK_LT OP_Ge
23955 **
23956 ** For other values of pExpr->op, op is undefined and unused.
23957 ** The value of TK_ and OP_ constants are arranged such that we
23958 ** can compute the mapping above using the following expression.
23959 ** Assert()s verify that the computation is correct.
23960 */
23961 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
23962
23963 /* Verify correct alignment of TK_ and OP_ constants
23964 */
23965 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
23966 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
23967 assert( pExpr->op!=TK_NE || op==OP_Eq );
23968 assert( pExpr->op!=TK_EQ || op==OP_Ne );
23969 assert( pExpr->op!=TK_LT || op==OP_Ge );
23970 assert( pExpr->op!=TK_LE || op==OP_Gt );
23971 assert( pExpr->op!=TK_GT || op==OP_Le );
23972 assert( pExpr->op!=TK_GE || op==OP_Lt );
23973
23974 switch( pExpr->op ){
23975 case TK_AND: {
23976 testcase( jumpIfNull==0 );
23977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
23978 sqlite3ExprCachePush(pParse);
23979 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
23980 sqlite3ExprCachePop(pParse);
23981 break;
23982 }
23983 case TK_OR: {
23984 int d2 = sqlite3VdbeMakeLabel(v);
23985 testcase( jumpIfNull==0 );
23986 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
23987 sqlite3ExprCachePush(pParse);
23988 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
23989 sqlite3VdbeResolveLabel(v, d2);
23990 sqlite3ExprCachePop(pParse);
23991 break;
23992 }
23993 case TK_NOT: {
23994 testcase( jumpIfNull==0 );
23995 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
23996 break;
23997 }
23998 case TK_LT:
23999 case TK_LE:
24000 case TK_GT:
24001 case TK_GE:
24002 case TK_NE:
24003 case TK_EQ: {
24004 testcase( jumpIfNull==0 );
24005 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
24006 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
24007 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
24008 r1, r2, dest, jumpIfNull);
24009 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
24010 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
24011 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
24012 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
24013 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
24014 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
24015 testcase( regFree1==0 );
24016 testcase( regFree2==0 );
24017 break;
24018 }
24019 case TK_IS:
24020 case TK_ISNOT: {
24021 testcase( pExpr->op==TK_IS );
24022 testcase( pExpr->op==TK_ISNOT );
24023 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
24024 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
24025 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
24026 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
24027 r1, r2, dest, SQLITE_NULLEQ);
24028 VdbeCoverageIf(v, op==TK_EQ);
24029 VdbeCoverageIf(v, op==TK_NE);
24030 testcase( regFree1==0 );
24031 testcase( regFree2==0 );
24032 break;
24033 }
24034 case TK_ISNULL:
24035 case TK_NOTNULL: {
24036 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
24037 sqlite3VdbeAddOp2(v, op, r1, dest);
24038 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
24039 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
24040 testcase( regFree1==0 );
24041 break;
24042 }
24043 case TK_BETWEEN: {
24044 testcase( jumpIfNull==0 );
24045 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
24046 break;
24047 }
24048 #ifndef SQLITE_OMIT_SUBQUERY
24049 case TK_IN: {
24050 if( jumpIfNull ){
24051 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
24052 }else{
24053 int destIfNull = sqlite3VdbeMakeLabel(v);
24054 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
24055 sqlite3VdbeResolveLabel(v, destIfNull);
24056 }
24057 break;
24058 }
24059 #endif
24060 default: {
24061 if( exprAlwaysFalse(pExpr) ){
24062 sqlite3VdbeGoto(v, dest);
24063 }else if( exprAlwaysTrue(pExpr) ){
24064 /* no-op */
24065 }else{
24066 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
24067 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
24068 VdbeCoverage(v);
24069 testcase( regFree1==0 );
24070 testcase( jumpIfNull==0 );
24071 }
24072 break;
24073 }
24074 }
24075 sqlite3ReleaseTempReg(pParse, regFree1);
24076 sqlite3ReleaseTempReg(pParse, regFree2);
24077 }
24078
24079 /*
24080 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
24081 ** code generation, and that copy is deleted after code generation. This
24082 ** ensures that the original pExpr is unchanged.
24083 */
24084 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,i nt jumpIfNull){
24085 sqlite3 *db = pParse->db;
24086 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
24087 if( db->mallocFailed==0 ){
24088 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
24089 }
24090 sqlite3ExprDelete(db, pCopy);
24091 }
24092
24093
24094 /*
24095 ** Do a deep comparison of two expression trees. Return 0 if the two
24096 ** expressions are completely identical. Return 1 if they differ only
24097 ** by a COLLATE operator at the top level. Return 2 if there are differences
24098 ** other than the top-level COLLATE operator.
24099 **
24100 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
24101 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
24102 **
24103 ** The pA side might be using TK_REGISTER. If that is the case and pB is
24104 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
24105 **
24106 ** Sometimes this routine will return 2 even if the two expressions
24107 ** really are equivalent. If we cannot prove that the expressions are
24108 ** identical, we return 2 just to be safe. So if this routine
24109 ** returns 2, then you do not really know for certain if the two
24110 ** expressions are the same. But if you get a 0 or 1 return, then you
24111 ** can be sure the expressions are the same. In the places where
24112 ** this routine is used, it does not hurt to get an extra 2 - that
24113 ** just might result in some slightly slower code. But returning
24114 ** an incorrect 0 or 1 could lead to a malfunction.
24115 */
24116 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
24117 u32 combinedFlags;
24118 if( pA==0 || pB==0 ){
24119 return pB==pA ? 0 : 2;
24120 }
24121 combinedFlags = pA->flags | pB->flags;
24122 if( combinedFlags & EP_IntValue ){
24123 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
24124 return 0;
24125 }
24126 return 2;
24127 }
24128 if( pA->op!=pB->op ){
24129 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
24130 return 1;
24131 }
24132 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
24133 return 1;
24134 }
24135 return 2;
24136 }
24137 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
24138 if( pA->op==TK_FUNCTION ){
24139 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
24140 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
24141 return pA->op==TK_COLLATE ? 1 : 2;
24142 }
24143 }
24144 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
24145 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
24146 if( combinedFlags & EP_xIsSelect ) return 2;
24147 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
24148 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
24149 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
24150 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
24151 if( pA->iColumn!=pB->iColumn ) return 2;
24152 if( pA->iTable!=pB->iTable
24153 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
24154 }
24155 }
24156 return 0;
24157 }
24158
24159 /*
24160 ** Compare two ExprList objects. Return 0 if they are identical and
24161 ** non-zero if they differ in any way.
24162 **
24163 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
24164 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
24165 **
24166 ** This routine might return non-zero for equivalent ExprLists. The
24167 ** only consequence will be disabled optimizations. But this routine
24168 ** must never return 0 if the two ExprList objects are different, or
24169 ** a malfunction will result.
24170 **
24171 ** Two NULL pointers are considered to be the same. But a NULL pointer
24172 ** always differs from a non-NULL pointer.
24173 */
24174 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
24175 int i;
24176 if( pA==0 && pB==0 ) return 0;
24177 if( pA==0 || pB==0 ) return 1;
24178 if( pA->nExpr!=pB->nExpr ) return 1;
24179 for(i=0; i<pA->nExpr; i++){
24180 Expr *pExprA = pA->a[i].pExpr;
24181 Expr *pExprB = pB->a[i].pExpr;
24182 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
24183 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
24184 }
24185 return 0;
24186 }
24187
24188 /*
24189 ** Return true if we can prove the pE2 will always be true if pE1 is
24190 ** true. Return false if we cannot complete the proof or if pE2 might
24191 ** be false. Examples:
24192 **
24193 ** pE1: x==5 pE2: x==5 Result: true
24194 ** pE1: x>0 pE2: x==5 Result: false
24195 ** pE1: x=21 pE2: x=21 OR y=43 Result: true
24196 ** pE1: x!=123 pE2: x IS NOT NULL Result: true
24197 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true
24198 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false
24199 ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
24200 **
24201 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
24202 ** Expr.iTable<0 then assume a table number given by iTab.
24203 **
24204 ** When in doubt, return false. Returning true might give a performance
24205 ** improvement. Returning false might cause a performance reduction, but
24206 ** it will always give the correct answer and is hence always safe.
24207 */
24208 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
24209 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
24210 return 1;
24211 }
24212 if( pE2->op==TK_OR
24213 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
24214 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
24215 ){
24216 return 1;
24217 }
24218 if( pE2->op==TK_NOTNULL
24219 && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
24220 && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
24221 ){
24222 return 1;
24223 }
24224 return 0;
24225 }
24226
24227 /*
24228 ** An instance of the following structure is used by the tree walker
24229 ** to count references to table columns in the arguments of an
24230 ** aggregate function, in order to implement the
24231 ** sqlite3FunctionThisSrc() routine.
24232 */
24233 struct SrcCount {
24234 SrcList *pSrc; /* One particular FROM clause in a nested query */
24235 int nThis; /* Number of references to columns in pSrcList */
24236 int nOther; /* Number of references to columns in other FROM clauses */
24237 };
24238
24239 /*
24240 ** Count the number of references to columns.
24241 */
24242 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
24243 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
24244 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
24245 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
24246 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
24247 ** NEVER() will need to be removed. */
24248 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
24249 int i;
24250 struct SrcCount *p = pWalker->u.pSrcCount;
24251 SrcList *pSrc = p->pSrc;
24252 int nSrc = pSrc ? pSrc->nSrc : 0;
24253 for(i=0; i<nSrc; i++){
24254 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
24255 }
24256 if( i<nSrc ){
24257 p->nThis++;
24258 }else{
24259 p->nOther++;
24260 }
24261 }
24262 return WRC_Continue;
24263 }
24264
24265 /*
24266 ** Determine if any of the arguments to the pExpr Function reference
24267 ** pSrcList. Return true if they do. Also return true if the function
24268 ** has no arguments or has only constant arguments. Return false if pExpr
24269 ** references columns but not columns of tables found in pSrcList.
24270 */
24271 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
24272 Walker w;
24273 struct SrcCount cnt;
24274 assert( pExpr->op==TK_AGG_FUNCTION );
24275 memset(&w, 0, sizeof(w));
24276 w.xExprCallback = exprSrcCount;
24277 w.u.pSrcCount = &cnt;
24278 cnt.pSrc = pSrcList;
24279 cnt.nThis = 0;
24280 cnt.nOther = 0;
24281 sqlite3WalkExprList(&w, pExpr->x.pList);
24282 return cnt.nThis>0 || cnt.nOther==0;
24283 }
24284
24285 /*
24286 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
24287 ** the new element. Return a negative number if malloc fails.
24288 */
24289 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
24290 int i;
24291 pInfo->aCol = sqlite3ArrayAllocate(
24292 db,
24293 pInfo->aCol,
24294 sizeof(pInfo->aCol[0]),
24295 &pInfo->nColumn,
24296 &i
24297 );
24298 return i;
24299 }
24300
24301 /*
24302 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
24303 ** the new element. Return a negative number if malloc fails.
24304 */
24305 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
24306 int i;
24307 pInfo->aFunc = sqlite3ArrayAllocate(
24308 db,
24309 pInfo->aFunc,
24310 sizeof(pInfo->aFunc[0]),
24311 &pInfo->nFunc,
24312 &i
24313 );
24314 return i;
24315 }
24316
24317 /*
24318 ** This is the xExprCallback for a tree walker. It is used to
24319 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
24320 ** for additional information.
24321 */
24322 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
24323 int i;
24324 NameContext *pNC = pWalker->u.pNC;
24325 Parse *pParse = pNC->pParse;
24326 SrcList *pSrcList = pNC->pSrcList;
24327 AggInfo *pAggInfo = pNC->pAggInfo;
24328
24329 switch( pExpr->op ){
24330 case TK_AGG_COLUMN:
24331 case TK_COLUMN: {
24332 testcase( pExpr->op==TK_AGG_COLUMN );
24333 testcase( pExpr->op==TK_COLUMN );
24334 /* Check to see if the column is in one of the tables in the FROM
24335 ** clause of the aggregate query */
24336 if( ALWAYS(pSrcList!=0) ){
24337 struct SrcList_item *pItem = pSrcList->a;
24338 for(i=0; i<pSrcList->nSrc; i++, pItem++){
24339 struct AggInfo_col *pCol;
24340 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
24341 if( pExpr->iTable==pItem->iCursor ){
24342 /* If we reach this point, it means that pExpr refers to a table
24343 ** that is in the FROM clause of the aggregate query.
24344 **
24345 ** Make an entry for the column in pAggInfo->aCol[] if there
24346 ** is not an entry there already.
24347 */
24348 int k;
24349 pCol = pAggInfo->aCol;
24350 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
24351 if( pCol->iTable==pExpr->iTable &&
24352 pCol->iColumn==pExpr->iColumn ){
24353 break;
24354 }
24355 }
24356 if( (k>=pAggInfo->nColumn)
24357 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
24358 ){
24359 pCol = &pAggInfo->aCol[k];
24360 pCol->pTab = pExpr->pTab;
24361 pCol->iTable = pExpr->iTable;
24362 pCol->iColumn = pExpr->iColumn;
24363 pCol->iMem = ++pParse->nMem;
24364 pCol->iSorterColumn = -1;
24365 pCol->pExpr = pExpr;
24366 if( pAggInfo->pGroupBy ){
24367 int j, n;
24368 ExprList *pGB = pAggInfo->pGroupBy;
24369 struct ExprList_item *pTerm = pGB->a;
24370 n = pGB->nExpr;
24371 for(j=0; j<n; j++, pTerm++){
24372 Expr *pE = pTerm->pExpr;
24373 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
24374 pE->iColumn==pExpr->iColumn ){
24375 pCol->iSorterColumn = j;
24376 break;
24377 }
24378 }
24379 }
24380 if( pCol->iSorterColumn<0 ){
24381 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
24382 }
24383 }
24384 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
24385 ** because it was there before or because we just created it).
24386 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
24387 ** pAggInfo->aCol[] entry.
24388 */
24389 ExprSetVVAProperty(pExpr, EP_NoReduce);
24390 pExpr->pAggInfo = pAggInfo;
24391 pExpr->op = TK_AGG_COLUMN;
24392 pExpr->iAgg = (i16)k;
24393 break;
24394 } /* endif pExpr->iTable==pItem->iCursor */
24395 } /* end loop over pSrcList */
24396 }
24397 return WRC_Prune;
24398 }
24399 case TK_AGG_FUNCTION: {
24400 if( (pNC->ncFlags & NC_InAggFunc)==0
24401 && pWalker->walkerDepth==pExpr->op2
24402 ){
24403 /* Check to see if pExpr is a duplicate of another aggregate
24404 ** function that is already in the pAggInfo structure
24405 */
24406 struct AggInfo_func *pItem = pAggInfo->aFunc;
24407 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
24408 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
24409 break;
24410 }
24411 }
24412 if( i>=pAggInfo->nFunc ){
24413 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
24414 */
24415 u8 enc = ENC(pParse->db);
24416 i = addAggInfoFunc(pParse->db, pAggInfo);
24417 if( i>=0 ){
24418 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
24419 pItem = &pAggInfo->aFunc[i];
24420 pItem->pExpr = pExpr;
24421 pItem->iMem = ++pParse->nMem;
24422 assert( !ExprHasProperty(pExpr, EP_IntValue) );
24423 pItem->pFunc = sqlite3FindFunction(pParse->db,
24424 pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
24425 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
24426 if( pExpr->flags & EP_Distinct ){
24427 pItem->iDistinct = pParse->nTab++;
24428 }else{
24429 pItem->iDistinct = -1;
24430 }
24431 }
24432 }
24433 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
24434 */
24435 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
24436 ExprSetVVAProperty(pExpr, EP_NoReduce);
24437 pExpr->iAgg = (i16)i;
24438 pExpr->pAggInfo = pAggInfo;
24439 return WRC_Prune;
24440 }else{
24441 return WRC_Continue;
24442 }
24443 }
24444 }
24445 return WRC_Continue;
24446 }
24447 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
24448 UNUSED_PARAMETER(pWalker);
24449 UNUSED_PARAMETER(pSelect);
24450 return WRC_Continue;
24451 }
24452
24453 /*
24454 ** Analyze the pExpr expression looking for aggregate functions and
24455 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
24456 ** points to. Additional entries are made on the AggInfo object as
24457 ** necessary.
24458 **
24459 ** This routine should only be called after the expression has been
24460 ** analyzed by sqlite3ResolveExprNames().
24461 */
24462 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
24463 Walker w;
24464 memset(&w, 0, sizeof(w));
24465 w.xExprCallback = analyzeAggregate;
24466 w.xSelectCallback = analyzeAggregatesInSelect;
24467 w.u.pNC = pNC;
24468 assert( pNC->pSrcList!=0 );
24469 sqlite3WalkExpr(&w, pExpr);
24470 }
24471
24472 /*
24473 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
24474 ** expression list. Return the number of errors.
24475 **
24476 ** If an error is found, the analysis is cut short.
24477 */
24478 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList) {
24479 struct ExprList_item *pItem;
24480 int i;
24481 if( pList ){
24482 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
24483 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
24484 }
24485 }
24486 }
24487
24488 /*
24489 ** Allocate a single new register for use to hold some intermediate result.
24490 */
24491 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
24492 if( pParse->nTempReg==0 ){
24493 return ++pParse->nMem;
24494 }
24495 return pParse->aTempReg[--pParse->nTempReg];
24496 }
24497
24498 /*
24499 ** Deallocate a register, making available for reuse for some other
24500 ** purpose.
24501 **
24502 ** If a register is currently being used by the column cache, then
24503 ** the deallocation is deferred until the column cache line that uses
24504 ** the register becomes stale.
24505 */
24506 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
24507 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
24508 int i;
24509 struct yColCache *p;
24510 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
24511 if( p->iReg==iReg ){
24512 p->tempReg = 1;
24513 return;
24514 }
24515 }
24516 pParse->aTempReg[pParse->nTempReg++] = iReg;
24517 }
24518 }
24519
24520 /*
24521 ** Allocate or deallocate a block of nReg consecutive registers
24522 */
24523 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
24524 int i, n;
24525 i = pParse->iRangeReg;
24526 n = pParse->nRangeReg;
24527 if( nReg<=n ){
24528 assert( !usedAsColumnCache(pParse, i, i+n-1) );
24529 pParse->iRangeReg += nReg;
24530 pParse->nRangeReg -= nReg;
24531 }else{
24532 i = pParse->nMem+1;
24533 pParse->nMem += nReg;
24534 }
24535 return i;
24536 }
24537 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
24538 sqlite3ExprCacheRemove(pParse, iReg, nReg);
24539 if( nReg>pParse->nRangeReg ){
24540 pParse->nRangeReg = nReg;
24541 pParse->iRangeReg = iReg;
24542 }
24543 }
24544
24545 /*
24546 ** Mark all temporary registers as being unavailable for reuse.
24547 */
24548 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
24549 pParse->nTempReg = 0;
24550 pParse->nRangeReg = 0;
24551 }
24552
24553 /************** End of expr.c ************************************************/
24554 /************** Begin file alter.c *******************************************/
24555 /*
24556 ** 2005 February 15
24557 **
24558 ** The author disclaims copyright to this source code. In place of
24559 ** a legal notice, here is a blessing:
24560 **
24561 ** May you do good and not evil.
24562 ** May you find forgiveness for yourself and forgive others.
24563 ** May you share freely, never taking more than you give.
24564 **
24565 *************************************************************************
24566 ** This file contains C code routines that used to generate VDBE code
24567 ** that implements the ALTER TABLE command.
24568 */
24569 /* #include "sqliteInt.h" */
24570
24571 /*
24572 ** The code in this file only exists if we are not omitting the
24573 ** ALTER TABLE logic from the build.
24574 */
24575 #ifndef SQLITE_OMIT_ALTERTABLE
24576
24577
24578 /*
24579 ** This function is used by SQL generated to implement the
24580 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
24581 ** CREATE INDEX command. The second is a table name. The table name in
24582 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
24583 ** argument and the result returned. Examples:
24584 **
24585 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
24586 ** -> 'CREATE TABLE def(a, b, c)'
24587 **
24588 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
24589 ** -> 'CREATE INDEX i ON def(a, b, c)'
24590 */
24591 static void renameTableFunc(
24592 sqlite3_context *context,
24593 int NotUsed,
24594 sqlite3_value **argv
24595 ){
24596 unsigned char const *zSql = sqlite3_value_text(argv[0]);
24597 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
24598
24599 int token;
24600 Token tname;
24601 unsigned char const *zCsr = zSql;
24602 int len = 0;
24603 char *zRet;
24604
24605 sqlite3 *db = sqlite3_context_db_handle(context);
24606
24607 UNUSED_PARAMETER(NotUsed);
24608
24609 /* The principle used to locate the table name in the CREATE TABLE
24610 ** statement is that the table name is the first non-space token that
24611 ** is immediately followed by a TK_LP or TK_USING token.
24612 */
24613 if( zSql ){
24614 do {
24615 if( !*zCsr ){
24616 /* Ran out of input before finding an opening bracket. Return NULL. */
24617 return;
24618 }
24619
24620 /* Store the token that zCsr points to in tname. */
24621 tname.z = (char*)zCsr;
24622 tname.n = len;
24623
24624 /* Advance zCsr to the next token. Store that token type in 'token',
24625 ** and its length in 'len' (to be used next iteration of this loop).
24626 */
24627 do {
24628 zCsr += len;
24629 len = sqlite3GetToken(zCsr, &token);
24630 } while( token==TK_SPACE );
24631 assert( len>0 );
24632 } while( token!=TK_LP && token!=TK_USING );
24633
24634 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
24635 zSql, zTableName, tname.z+tname.n);
24636 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
24637 }
24638 }
24639
24640 /*
24641 ** This C function implements an SQL user function that is used by SQL code
24642 ** generated by the ALTER TABLE ... RENAME command to modify the definition
24643 ** of any foreign key constraints that use the table being renamed as the
24644 ** parent table. It is passed three arguments:
24645 **
24646 ** 1) The complete text of the CREATE TABLE statement being modified,
24647 ** 2) The old name of the table being renamed, and
24648 ** 3) The new name of the table being renamed.
24649 **
24650 ** It returns the new CREATE TABLE statement. For example:
24651 **
24652 ** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
24653 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
24654 */
24655 #ifndef SQLITE_OMIT_FOREIGN_KEY
24656 static void renameParentFunc(
24657 sqlite3_context *context,
24658 int NotUsed,
24659 sqlite3_value **argv
24660 ){
24661 sqlite3 *db = sqlite3_context_db_handle(context);
24662 char *zOutput = 0;
24663 char *zResult;
24664 unsigned char const *zInput = sqlite3_value_text(argv[0]);
24665 unsigned char const *zOld = sqlite3_value_text(argv[1]);
24666 unsigned char const *zNew = sqlite3_value_text(argv[2]);
24667
24668 unsigned const char *z; /* Pointer to token */
24669 int n; /* Length of token z */
24670 int token; /* Type of token */
24671
24672 UNUSED_PARAMETER(NotUsed);
24673 if( zInput==0 || zOld==0 ) return;
24674 for(z=zInput; *z; z=z+n){
24675 n = sqlite3GetToken(z, &token);
24676 if( token==TK_REFERENCES ){
24677 char *zParent;
24678 do {
24679 z += n;
24680 n = sqlite3GetToken(z, &token);
24681 }while( token==TK_SPACE );
24682
24683 if( token==TK_ILLEGAL ) break;
24684 zParent = sqlite3DbStrNDup(db, (const char *)z, n);
24685 if( zParent==0 ) break;
24686 sqlite3Dequote(zParent);
24687 if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
24688 char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
24689 (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
24690 );
24691 sqlite3DbFree(db, zOutput);
24692 zOutput = zOut;
24693 zInput = &z[n];
24694 }
24695 sqlite3DbFree(db, zParent);
24696 }
24697 }
24698
24699 zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
24700 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
24701 sqlite3DbFree(db, zOutput);
24702 }
24703 #endif
24704
24705 #ifndef SQLITE_OMIT_TRIGGER
24706 /* This function is used by SQL generated to implement the
24707 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
24708 ** statement. The second is a table name. The table name in the CREATE
24709 ** TRIGGER statement is replaced with the third argument and the result
24710 ** returned. This is analagous to renameTableFunc() above, except for CREATE
24711 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
24712 */
24713 static void renameTriggerFunc(
24714 sqlite3_context *context,
24715 int NotUsed,
24716 sqlite3_value **argv
24717 ){
24718 unsigned char const *zSql = sqlite3_value_text(argv[0]);
24719 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
24720
24721 int token;
24722 Token tname;
24723 int dist = 3;
24724 unsigned char const *zCsr = zSql;
24725 int len = 0;
24726 char *zRet;
24727 sqlite3 *db = sqlite3_context_db_handle(context);
24728
24729 UNUSED_PARAMETER(NotUsed);
24730
24731 /* The principle used to locate the table name in the CREATE TRIGGER
24732 ** statement is that the table name is the first token that is immediately
24733 ** preceded by either TK_ON or TK_DOT and immediately followed by one
24734 ** of TK_WHEN, TK_BEGIN or TK_FOR.
24735 */
24736 if( zSql ){
24737 do {
24738
24739 if( !*zCsr ){
24740 /* Ran out of input before finding the table name. Return NULL. */
24741 return;
24742 }
24743
24744 /* Store the token that zCsr points to in tname. */
24745 tname.z = (char*)zCsr;
24746 tname.n = len;
24747
24748 /* Advance zCsr to the next token. Store that token type in 'token',
24749 ** and its length in 'len' (to be used next iteration of this loop).
24750 */
24751 do {
24752 zCsr += len;
24753 len = sqlite3GetToken(zCsr, &token);
24754 }while( token==TK_SPACE );
24755 assert( len>0 );
24756
24757 /* Variable 'dist' stores the number of tokens read since the most
24758 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
24759 ** token is read and 'dist' equals 2, the condition stated above
24760 ** to be met.
24761 **
24762 ** Note that ON cannot be a database, table or column name, so
24763 ** there is no need to worry about syntax like
24764 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
24765 */
24766 dist++;
24767 if( token==TK_DOT || token==TK_ON ){
24768 dist = 0;
24769 }
24770 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
24771
24772 /* Variable tname now contains the token that is the old table-name
24773 ** in the CREATE TRIGGER statement.
24774 */
24775 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
24776 zSql, zTableName, tname.z+tname.n);
24777 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
24778 }
24779 }
24780 #endif /* !SQLITE_OMIT_TRIGGER */
24781
24782 /*
24783 ** Register built-in functions used to help implement ALTER TABLE
24784 */
24785 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
24786 static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
24787 FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
24788 #ifndef SQLITE_OMIT_TRIGGER
24789 FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
24790 #endif
24791 #ifndef SQLITE_OMIT_FOREIGN_KEY
24792 FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
24793 #endif
24794 };
24795 int i;
24796 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
24797 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
24798
24799 for(i=0; i<ArraySize(aAlterTableFuncs); i++){
24800 sqlite3FuncDefInsert(pHash, &aFunc[i]);
24801 }
24802 }
24803
24804 /*
24805 ** This function is used to create the text of expressions of the form:
24806 **
24807 ** name=<constant1> OR name=<constant2> OR ...
24808 **
24809 ** If argument zWhere is NULL, then a pointer string containing the text
24810 ** "name=<constant>" is returned, where <constant> is the quoted version
24811 ** of the string passed as argument zConstant. The returned buffer is
24812 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
24813 ** caller to ensure that it is eventually freed.
24814 **
24815 ** If argument zWhere is not NULL, then the string returned is
24816 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
24817 ** In this case zWhere is passed to sqlite3DbFree() before returning.
24818 **
24819 */
24820 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
24821 char *zNew;
24822 if( !zWhere ){
24823 zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
24824 }else{
24825 zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
24826 sqlite3DbFree(db, zWhere);
24827 }
24828 return zNew;
24829 }
24830
24831 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
24832 /*
24833 ** Generate the text of a WHERE expression which can be used to select all
24834 ** tables that have foreign key constraints that refer to table pTab (i.e.
24835 ** constraints for which pTab is the parent table) from the sqlite_master
24836 ** table.
24837 */
24838 static char *whereForeignKeys(Parse *pParse, Table *pTab){
24839 FKey *p;
24840 char *zWhere = 0;
24841 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
24842 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
24843 }
24844 return zWhere;
24845 }
24846 #endif
24847
24848 /*
24849 ** Generate the text of a WHERE expression which can be used to select all
24850 ** temporary triggers on table pTab from the sqlite_temp_master table. If
24851 ** table pTab has no temporary triggers, or is itself stored in the
24852 ** temporary database, NULL is returned.
24853 */
24854 static char *whereTempTriggers(Parse *pParse, Table *pTab){
24855 Trigger *pTrig;
24856 char *zWhere = 0;
24857 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
24858
24859 /* If the table is not located in the temp-db (in which case NULL is
24860 ** returned, loop through the tables list of triggers. For each trigger
24861 ** that is not part of the temp-db schema, add a clause to the WHERE
24862 ** expression being built up in zWhere.
24863 */
24864 if( pTab->pSchema!=pTempSchema ){
24865 sqlite3 *db = pParse->db;
24866 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
24867 if( pTrig->pSchema==pTempSchema ){
24868 zWhere = whereOrName(db, zWhere, pTrig->zName);
24869 }
24870 }
24871 }
24872 if( zWhere ){
24873 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
24874 sqlite3DbFree(pParse->db, zWhere);
24875 zWhere = zNew;
24876 }
24877 return zWhere;
24878 }
24879
24880 /*
24881 ** Generate code to drop and reload the internal representation of table
24882 ** pTab from the database, including triggers and temporary triggers.
24883 ** Argument zName is the name of the table in the database schema at
24884 ** the time the generated code is executed. This can be different from
24885 ** pTab->zName if this function is being called to code part of an
24886 ** "ALTER TABLE RENAME TO" statement.
24887 */
24888 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
24889 Vdbe *v;
24890 char *zWhere;
24891 int iDb; /* Index of database containing pTab */
24892 #ifndef SQLITE_OMIT_TRIGGER
24893 Trigger *pTrig;
24894 #endif
24895
24896 v = sqlite3GetVdbe(pParse);
24897 if( NEVER(v==0) ) return;
24898 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
24899 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
24900 assert( iDb>=0 );
24901
24902 #ifndef SQLITE_OMIT_TRIGGER
24903 /* Drop any table triggers from the internal schema. */
24904 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
24905 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
24906 assert( iTrigDb==iDb || iTrigDb==1 );
24907 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
24908 }
24909 #endif
24910
24911 /* Drop the table and index from the internal schema. */
24912 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
24913
24914 /* Reload the table, index and permanent trigger schemas. */
24915 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
24916 if( !zWhere ) return;
24917 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
24918
24919 #ifndef SQLITE_OMIT_TRIGGER
24920 /* Now, if the table is not stored in the temp database, reload any temp
24921 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
24922 */
24923 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
24924 sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
24925 }
24926 #endif
24927 }
24928
24929 /*
24930 ** Parameter zName is the name of a table that is about to be altered
24931 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
24932 ** If the table is a system table, this function leaves an error message
24933 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
24934 **
24935 ** Or, if zName is not a system table, zero is returned.
24936 */
24937 static int isSystemTable(Parse *pParse, const char *zName){
24938 if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
24939 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
24940 return 1;
24941 }
24942 return 0;
24943 }
24944
24945 /*
24946 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
24947 ** command.
24948 */
24949 SQLITE_PRIVATE void sqlite3AlterRenameTable(
24950 Parse *pParse, /* Parser context. */
24951 SrcList *pSrc, /* The table to rename. */
24952 Token *pName /* The new table name. */
24953 ){
24954 int iDb; /* Database that contains the table */
24955 char *zDb; /* Name of database iDb */
24956 Table *pTab; /* Table being renamed */
24957 char *zName = 0; /* NULL-terminated version of pName */
24958 sqlite3 *db = pParse->db; /* Database connection */
24959 int nTabName; /* Number of UTF-8 characters in zTabName */
24960 const char *zTabName; /* Original name of the table */
24961 Vdbe *v;
24962 #ifndef SQLITE_OMIT_TRIGGER
24963 char *zWhere = 0; /* Where clause to locate temp triggers */
24964 #endif
24965 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
24966 int savedDbFlags; /* Saved value of db->flags */
24967
24968 savedDbFlags = db->flags;
24969 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
24970 assert( pSrc->nSrc==1 );
24971 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
24972
24973 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
24974 if( !pTab ) goto exit_rename_table;
24975 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
24976 zDb = db->aDb[iDb].zName;
24977 db->flags |= SQLITE_PreferBuiltin;
24978
24979 /* Get a NULL terminated version of the new table name. */
24980 zName = sqlite3NameFromToken(db, pName);
24981 if( !zName ) goto exit_rename_table;
24982
24983 /* Check that a table or index named 'zName' does not already exist
24984 ** in database iDb. If so, this is an error.
24985 */
24986 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
24987 sqlite3ErrorMsg(pParse,
24988 "there is already another table or index with this name: %s", zName);
24989 goto exit_rename_table;
24990 }
24991
24992 /* Make sure it is not a system table being altered, or a reserved name
24993 ** that the table is being renamed to.
24994 */
24995 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
24996 goto exit_rename_table;
24997 }
24998 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
24999 exit_rename_table;
25000 }
25001
25002 #ifndef SQLITE_OMIT_VIEW
25003 if( pTab->pSelect ){
25004 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
25005 goto exit_rename_table;
25006 }
25007 #endif
25008
25009 #ifndef SQLITE_OMIT_AUTHORIZATION
25010 /* Invoke the authorization callback. */
25011 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
25012 goto exit_rename_table;
25013 }
25014 #endif
25015
25016 #ifndef SQLITE_OMIT_VIRTUALTABLE
25017 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
25018 goto exit_rename_table;
25019 }
25020 if( IsVirtual(pTab) ){
25021 pVTab = sqlite3GetVTable(db, pTab);
25022 if( pVTab->pVtab->pModule->xRename==0 ){
25023 pVTab = 0;
25024 }
25025 }
25026 #endif
25027
25028 /* Begin a transaction for database iDb.
25029 ** Then modify the schema cookie (since the ALTER TABLE modifies the
25030 ** schema). Open a statement transaction if the table is a virtual
25031 ** table.
25032 */
25033 v = sqlite3GetVdbe(pParse);
25034 if( v==0 ){
25035 goto exit_rename_table;
25036 }
25037 sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
25038 sqlite3ChangeCookie(pParse, iDb);
25039
25040 /* If this is a virtual table, invoke the xRename() function if
25041 ** one is defined. The xRename() callback will modify the names
25042 ** of any resources used by the v-table implementation (including other
25043 ** SQLite tables) that are identified by the name of the virtual table.
25044 */
25045 #ifndef SQLITE_OMIT_VIRTUALTABLE
25046 if( pVTab ){
25047 int i = ++pParse->nMem;
25048 sqlite3VdbeLoadString(v, i, zName);
25049 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
25050 sqlite3MayAbort(pParse);
25051 }
25052 #endif
25053
25054 /* figure out how many UTF-8 characters are in zName */
25055 zTabName = pTab->zName;
25056 nTabName = sqlite3Utf8CharLen(zTabName, -1);
25057
25058 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
25059 if( db->flags&SQLITE_ForeignKeys ){
25060 /* If foreign-key support is enabled, rewrite the CREATE TABLE
25061 ** statements corresponding to all child tables of foreign key constraints
25062 ** for which the renamed table is the parent table. */
25063 if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
25064 sqlite3NestedParse(pParse,
25065 "UPDATE \"%w\".%s SET "
25066 "sql = sqlite_rename_parent(sql, %Q, %Q) "
25067 "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
25068 sqlite3DbFree(db, zWhere);
25069 }
25070 }
25071 #endif
25072
25073 /* Modify the sqlite_master table to use the new table name. */
25074 sqlite3NestedParse(pParse,
25075 "UPDATE %Q.%s SET "
25076 #ifdef SQLITE_OMIT_TRIGGER
25077 "sql = sqlite_rename_table(sql, %Q), "
25078 #else
25079 "sql = CASE "
25080 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
25081 "ELSE sqlite_rename_table(sql, %Q) END, "
25082 #endif
25083 "tbl_name = %Q, "
25084 "name = CASE "
25085 "WHEN type='table' THEN %Q "
25086 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
25087 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
25088 "ELSE name END "
25089 "WHERE tbl_name=%Q COLLATE nocase AND "
25090 "(type='table' OR type='index' OR type='trigger');",
25091 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
25092 #ifndef SQLITE_OMIT_TRIGGER
25093 zName,
25094 #endif
25095 zName, nTabName, zTabName
25096 );
25097
25098 #ifndef SQLITE_OMIT_AUTOINCREMENT
25099 /* If the sqlite_sequence table exists in this database, then update
25100 ** it with the new table name.
25101 */
25102 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
25103 sqlite3NestedParse(pParse,
25104 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
25105 zDb, zName, pTab->zName);
25106 }
25107 #endif
25108
25109 #ifndef SQLITE_OMIT_TRIGGER
25110 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
25111 ** table. Don't do this if the table being ALTERed is itself located in
25112 ** the temp database.
25113 */
25114 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
25115 sqlite3NestedParse(pParse,
25116 "UPDATE sqlite_temp_master SET "
25117 "sql = sqlite_rename_trigger(sql, %Q), "
25118 "tbl_name = %Q "
25119 "WHERE %s;", zName, zName, zWhere);
25120 sqlite3DbFree(db, zWhere);
25121 }
25122 #endif
25123
25124 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
25125 if( db->flags&SQLITE_ForeignKeys ){
25126 FKey *p;
25127 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
25128 Table *pFrom = p->pFrom;
25129 if( pFrom!=pTab ){
25130 reloadTableSchema(pParse, p->pFrom, pFrom->zName);
25131 }
25132 }
25133 }
25134 #endif
25135
25136 /* Drop and reload the internal table schema. */
25137 reloadTableSchema(pParse, pTab, zName);
25138
25139 exit_rename_table:
25140 sqlite3SrcListDelete(db, pSrc);
25141 sqlite3DbFree(db, zName);
25142 db->flags = savedDbFlags;
25143 }
25144
25145
25146 /*
25147 ** Generate code to make sure the file format number is at least minFormat.
25148 ** The generated code will increase the file format number if necessary.
25149 */
25150 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minForm at){
25151 Vdbe *v;
25152 v = sqlite3GetVdbe(pParse);
25153 /* The VDBE should have been allocated before this routine is called.
25154 ** If that allocation failed, we would have quit before reaching this
25155 ** point */
25156 if( ALWAYS(v) ){
25157 int r1 = sqlite3GetTempReg(pParse);
25158 int r2 = sqlite3GetTempReg(pParse);
25159 int addr1;
25160 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
25161 sqlite3VdbeUsesBtree(v, iDb);
25162 sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
25163 addr1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
25164 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
25165 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
25166 sqlite3VdbeJumpHere(v, addr1);
25167 sqlite3ReleaseTempReg(pParse, r1);
25168 sqlite3ReleaseTempReg(pParse, r2);
25169 }
25170 }
25171
25172 /*
25173 ** This function is called after an "ALTER TABLE ... ADD" statement
25174 ** has been parsed. Argument pColDef contains the text of the new
25175 ** column definition.
25176 **
25177 ** The Table structure pParse->pNewTable was extended to include
25178 ** the new column during parsing.
25179 */
25180 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
25181 Table *pNew; /* Copy of pParse->pNewTable */
25182 Table *pTab; /* Table being altered */
25183 int iDb; /* Database number */
25184 const char *zDb; /* Database name */
25185 const char *zTab; /* Table name */
25186 char *zCol; /* Null-terminated column definition */
25187 Column *pCol; /* The new column */
25188 Expr *pDflt; /* Default value for the new column */
25189 sqlite3 *db; /* The database connection; */
25190
25191 db = pParse->db;
25192 if( pParse->nErr || db->mallocFailed ) return;
25193 pNew = pParse->pNewTable;
25194 assert( pNew );
25195
25196 assert( sqlite3BtreeHoldsAllMutexes(db) );
25197 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
25198 zDb = db->aDb[iDb].zName;
25199 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
25200 pCol = &pNew->aCol[pNew->nCol-1];
25201 pDflt = pCol->pDflt;
25202 pTab = sqlite3FindTable(db, zTab, zDb);
25203 assert( pTab );
25204
25205 #ifndef SQLITE_OMIT_AUTHORIZATION
25206 /* Invoke the authorization callback. */
25207 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
25208 return;
25209 }
25210 #endif
25211
25212 /* If the default value for the new column was specified with a
25213 ** literal NULL, then set pDflt to 0. This simplifies checking
25214 ** for an SQL NULL default below.
25215 */
25216 if( pDflt && pDflt->op==TK_NULL ){
25217 pDflt = 0;
25218 }
25219
25220 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
25221 ** If there is a NOT NULL constraint, then the default value for the
25222 ** column must not be NULL.
25223 */
25224 if( pCol->colFlags & COLFLAG_PRIMKEY ){
25225 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
25226 return;
25227 }
25228 if( pNew->pIndex ){
25229 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
25230 return;
25231 }
25232 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
25233 sqlite3ErrorMsg(pParse,
25234 "Cannot add a REFERENCES column with non-NULL default value");
25235 return;
25236 }
25237 if( pCol->notNull && !pDflt ){
25238 sqlite3ErrorMsg(pParse,
25239 "Cannot add a NOT NULL column with default value NULL");
25240 return;
25241 }
25242
25243 /* Ensure the default expression is something that sqlite3ValueFromExpr()
25244 ** can handle (i.e. not CURRENT_TIME etc.)
25245 */
25246 if( pDflt ){
25247 sqlite3_value *pVal = 0;
25248 int rc;
25249 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
25250 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
25251 if( rc!=SQLITE_OK ){
25252 db->mallocFailed = 1;
25253 return;
25254 }
25255 if( !pVal ){
25256 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
25257 return;
25258 }
25259 sqlite3ValueFree(pVal);
25260 }
25261
25262 /* Modify the CREATE TABLE statement. */
25263 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
25264 if( zCol ){
25265 char *zEnd = &zCol[pColDef->n-1];
25266 int savedDbFlags = db->flags;
25267 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
25268 *zEnd-- = '\0';
25269 }
25270 db->flags |= SQLITE_PreferBuiltin;
25271 sqlite3NestedParse(pParse,
25272 "UPDATE \"%w\".%s SET "
25273 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
25274 "WHERE type = 'table' AND name = %Q",
25275 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
25276 zTab
25277 );
25278 sqlite3DbFree(db, zCol);
25279 db->flags = savedDbFlags;
25280 }
25281
25282 /* If the default value of the new column is NULL, then set the file
25283 ** format to 2. If the default value of the new column is not NULL,
25284 ** the file format becomes 3.
25285 */
25286 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
25287
25288 /* Reload the schema of the modified table. */
25289 reloadTableSchema(pParse, pTab, pTab->zName);
25290 }
25291
25292 /*
25293 ** This function is called by the parser after the table-name in
25294 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
25295 ** pSrc is the full-name of the table being altered.
25296 **
25297 ** This routine makes a (partial) copy of the Table structure
25298 ** for the table being altered and sets Parse.pNewTable to point
25299 ** to it. Routines called by the parser as the column definition
25300 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
25301 ** the copy. The copy of the Table structure is deleted by tokenize.c
25302 ** after parsing is finished.
25303 **
25304 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
25305 ** coding the "ALTER TABLE ... ADD" statement.
25306 */
25307 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
25308 Table *pNew;
25309 Table *pTab;
25310 Vdbe *v;
25311 int iDb;
25312 int i;
25313 int nAlloc;
25314 sqlite3 *db = pParse->db;
25315
25316 /* Look up the table being altered. */
25317 assert( pParse->pNewTable==0 );
25318 assert( sqlite3BtreeHoldsAllMutexes(db) );
25319 if( db->mallocFailed ) goto exit_begin_add_column;
25320 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
25321 if( !pTab ) goto exit_begin_add_column;
25322
25323 #ifndef SQLITE_OMIT_VIRTUALTABLE
25324 if( IsVirtual(pTab) ){
25325 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
25326 goto exit_begin_add_column;
25327 }
25328 #endif
25329
25330 /* Make sure this is not an attempt to ALTER a view. */
25331 if( pTab->pSelect ){
25332 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
25333 goto exit_begin_add_column;
25334 }
25335 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
25336 goto exit_begin_add_column;
25337 }
25338
25339 assert( pTab->addColOffset>0 );
25340 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
25341
25342 /* Put a copy of the Table struct in Parse.pNewTable for the
25343 ** sqlite3AddColumn() function and friends to modify. But modify
25344 ** the name by adding an "sqlite_altertab_" prefix. By adding this
25345 ** prefix, we insure that the name will not collide with an existing
25346 ** table because user table are not allowed to have the "sqlite_"
25347 ** prefix on their name.
25348 */
25349 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
25350 if( !pNew ) goto exit_begin_add_column;
25351 pParse->pNewTable = pNew;
25352 pNew->nRef = 1;
25353 pNew->nCol = pTab->nCol;
25354 assert( pNew->nCol>0 );
25355 nAlloc = (((pNew->nCol-1)/8)*8)+8;
25356 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
25357 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
25358 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
25359 if( !pNew->aCol || !pNew->zName ){
25360 db->mallocFailed = 1;
25361 goto exit_begin_add_column;
25362 }
25363 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
25364 for(i=0; i<pNew->nCol; i++){
25365 Column *pCol = &pNew->aCol[i];
25366 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
25367 pCol->zColl = 0;
25368 pCol->zType = 0;
25369 pCol->pDflt = 0;
25370 pCol->zDflt = 0;
25371 }
25372 pNew->pSchema = db->aDb[iDb].pSchema;
25373 pNew->addColOffset = pTab->addColOffset;
25374 pNew->nRef = 1;
25375
25376 /* Begin a transaction and increment the schema cookie. */
25377 sqlite3BeginWriteOperation(pParse, 0, iDb);
25378 v = sqlite3GetVdbe(pParse);
25379 if( !v ) goto exit_begin_add_column;
25380 sqlite3ChangeCookie(pParse, iDb);
25381
25382 exit_begin_add_column:
25383 sqlite3SrcListDelete(db, pSrc);
25384 return;
25385 }
25386 #endif /* SQLITE_ALTER_TABLE */
25387
25388 /************** End of alter.c ***********************************************/
25389
25390 /* Chain include. */
25391 #include "sqlite3.04.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.02.c ('k') | third_party/sqlite/amalgamation/sqlite3.04.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698