vdbe.c

filter: Statementstmt 49.3% 947 / 1918 Branchbran 34.9% 430 / 1230 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 43.7% 1377 / 3148 SCAR 0
CC
0
Cov
0%
CRAP 0
« expr.c ↑ Summary select.c »
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** The code in this file implements execution method of the
13 ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
14 ** handles housekeeping details such as creating and deleting
15 ** VDBE instances. This file is solely interested in executing
16 ** the VDBE program.
17 **
18 ** In the external interface, an "sqlite_vm*" is an opaque pointer
19 ** to a VDBE.
20 **
21 ** The SQL parser generates a program which is then executed by
22 ** the VDBE to do the work of the SQL statement. VDBE programs are
23 ** similar in form to assembly language. The program consists of
24 ** a linear sequence of operations. Each operation has an opcode
25 ** and 3 operands. Operands P1 and P2 are integers. Operand P3
26 ** is a null-terminated string. The P2 operand must be non-negative.
27 ** Opcodes will typically ignore one or more operands. Many opcodes
28 ** ignore all three operands.
29 **
30 ** Computation results are stored on a stack. Each entry on the
31 ** stack is either an integer, a null-terminated string, a floating point
32 ** number, or the SQL "NULL" value. An inplicit conversion from one
33 ** type to the other occurs as necessary.
34 **
35 ** Most of the code in this file is taken up by the sqliteVdbeExec()
36 ** function which does the work of interpreting a VDBE program.
37 ** But other routines are also provided to help in building up
38 ** a program instruction by instruction.
39 **
40 ** Various scripts scan this source file in order to generate HTML
41 ** documentation, headers files, or other derived files. The formatting
42 ** of the code in this file is, therefore, important. See other comments
43 ** in this file for details. If in doubt, do not deviate from existing
44 ** commenting and indentation practices when changing or adding code.
45 **
46 ** $Id: vdbe.c,v 1.1.1.1 2004/08/08 15:03:58 matt Exp $
47 */
48 #include "sqliteInt.h"
49 #include "os.h"
50 #include <ctype.h>
51 #include "vdbeInt.h"
52  
53 /*
54 ** The following global variable is incremented every time a cursor
55 ** moves, either by the OP_MoveTo or the OP_Next opcode. The test
56 ** procedures use this information to make sure that indices are
57 ** working correctly. This variable has no function other than to
58 ** help verify the correct operation of the library.
59 */
60 int sqlite_search_count = 0;
61  
62 /*
63 ** When this global variable is positive, it gets decremented once before
64 ** each instruction in the VDBE. When reaches zero, the SQLITE_Interrupt
65 ** of the db.flags field is set in order to simulate an interrupt.
66 **
67 ** This facility is used for testing purposes only. It does not function
68 ** in an ordinary build.
69 */
70 int sqlite_interrupt_count = 0;
71  
72 /*
73 ** Advance the virtual machine to the next output row.
74 **
75 ** The return vale will be either SQLITE_BUSY, SQLITE_DONE,
76 ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
77 **
78 ** SQLITE_BUSY means that the virtual machine attempted to open
79 ** a locked database and there is no busy callback registered.
80 ** Call sqlite_step() again to retry the open. *pN is set to 0
81 ** and *pazColName and *pazValue are both set to NULL.
82 **
83 ** SQLITE_DONE means that the virtual machine has finished
84 ** executing. sqlite_step() should not be called again on this
85 ** virtual machine. *pN and *pazColName are set appropriately
86 ** but *pazValue is set to NULL.
87 **
88 ** SQLITE_ROW means that the virtual machine has generated another
89 ** row of the result set. *pN is set to the number of columns in
90 ** the row. *pazColName is set to the names of the columns followed
91 ** by the column datatypes. *pazValue is set to the values of each
92 ** column in the row. The value of the i-th column is (*pazValue)[i].
93 ** The name of the i-th column is (*pazColName)[i] and the datatype
94 ** of the i-th column is (*pazColName)[i+*pN].
95 **
96 ** SQLITE_ERROR means that a run-time error (such as a constraint
97 ** violation) has occurred. The details of the error will be returned
98 ** by the next call to sqlite_finalize(). sqlite_step() should not
99 ** be called again on the VM.
100 **
101 ** SQLITE_MISUSE means that the this routine was called inappropriately.
102 ** Perhaps it was called on a virtual machine that had already been
103 ** finalized or on one that had previously returned SQLITE_ERROR or
104 ** SQLITE_DONE. Or it could be the case the the same database connection
105 ** is being used simulataneously by two or more threads.
106 */
107 433 int sqlite_step(
108 sqlite_vm *pVm, /* The virtual machine to execute */
109 int *pN, /* OUT: Number of columns in result */
110 const char ***pazValue, /* OUT: Column data */
111 const char ***pazColName /* OUT: Column names and datatypes */
112 ){
113 433 Vdbe *p = (Vdbe*)pVm;
114 sqlite *db;
115 int rc;
116  
117 433 ▶ if( p->magic!=VDBE_MAGIC_RUN ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->magic!=VDBE_MAGIC_RUN ){ 0 433 433
118 0 return SQLITE_MISUSE;
119 }
120 433 db = p->db;
121 433 ▶ if( sqliteSafetyOn(db) ){
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOn(db) ){ 0 433 433
122 0 p->rc = SQLITE_MISUSE;
123 0 return SQLITE_MISUSE;
124 }
125 433 ▶ if( p->explain ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->explain ){ 0 433 433
126 0 rc = sqliteVdbeList(p);
127 }else{
128 433 rc = sqliteVdbeExec(p);
129 }
130 433 ▶ if( rc==SQLITE_DONE || rc==SQLITE_ROW ){
Branch: 2 branches
BranchTrueFalseTotal
if( rc==SQLITE_DONE || rc==SQLITE_ROW ){ 97 336 433
if( rc==SQLITE_DONE || rc==SQLITE_ROW ){ 95 2 97
131 431 ▶ if( pazColName ) *pazColName = (const char**)p->azColName;
Branch: 1 branch
BranchTrueFalseTotal
if( pazColName ) *pazColName = (const char**)p->azColName; 431 0 431
132 431 ▶ if( pN ) *pN = p->nResColumn;
Branch: 1 branch
BranchTrueFalseTotal
if( pN ) *pN = p->nResColumn; 431 0 431
133 }else{
134 2 ▶ if( pazColName) *pazColName = 0;
Branch: 1 branch
BranchTrueFalseTotal
if( pazColName) *pazColName = 0; 2 0 2
135 2 ▶ if( pN ) *pN = 0;
Branch: 1 branch
BranchTrueFalseTotal
if( pN ) *pN = 0; 2 0 2
136 }
137 433 ▶ if( pazValue ){
Branch: 1 branch
BranchTrueFalseTotal
if( pazValue ){ 433 0 433
138 433 ▶ if( rc==SQLITE_ROW ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_ROW ){ 95 338 433
139 95 *pazValue = (const char**)p->azResColumn;
140 }else{
141 338 *pazValue = 0;
142 }
143 }
144 433 ▶ if( sqliteSafetyOff(db) ){
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOff(db) ){ 0 433 433
145 0 return SQLITE_MISUSE;
146 }
147 433 return rc;
148 }
149  
150 /*
151 ** Insert a new aggregate element and make it the element that
152 ** has focus.
153 **
154 ** Return 0 on success and 1 if memory is exhausted.
155 */
156 16 static int AggInsert(Agg *p, char *zKey, int nKey){
157 AggElem *pElem, *pOld;
158 int i;
159 Mem *pMem;
160 16 pElem = sqliteMalloc( sizeof(AggElem) + nKey +
161 16 (p->nMem-1)*sizeof(pElem->aMem[0]) );
162 16 ▶ if( pElem==0 ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( pElem==0 ) return 1; 0 16 16
163 16 pElem->zKey = (char*)&pElem->aMem[p->nMem];
164 16 memcpy(pElem->zKey, zKey, nKey);
165 16 pElem->nKey = nKey;
166 16 pOld = sqliteHashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
167 16 ▶ if( pOld!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOld!=0 ){ 0 16 16
168 assert( pOld==pElem ); /* Malloc failed on insert */
169 0 sqliteFree(pOld);
170 0 return 0;
171 }
172 35 ▶ for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){ 19 16 35
173 19 pMem->flags = MEM_Null;
174 }
175 16 p->pCurrent = pElem;
176 16 return 0;
177 }
178  
179 /*
180 ** Get the AggElem currently in focus
181 */
182 #define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P)))
183 0 static AggElem *_AggInFocus(Agg *p){
184 0 HashElem *pElem = sqliteHashFirst(&p->hash);
185 0 ▶ if( pElem==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pElem==0 ){ 0 0 0
186 0 AggInsert(p,"",1);
187 0 pElem = sqliteHashFirst(&p->hash);
188 }
189 0 ▶ return pElem ? sqliteHashData(pElem) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return pElem ? sqliteHashData(pElem) : 0; 0 0 0
190 }
191  
192 /*
193 ** Convert the given stack entity into a string if it isn't one
194 ** already.
195 */
196 #define Stringify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);}
197 58 static int hardStringify(Mem *pStack){
198 58 int fg = pStack->flags;
199 58 ▶ if( fg & MEM_Real ){
Branch: 1 branch
BranchTrueFalseTotal
if( fg & MEM_Real ){ 2 56 58
200 2 sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r);
201 56 ▶ }else if( fg & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( fg & MEM_Int ){ 43 13 56
202 43 sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%d",pStack->i);
203 }else{
204 13 pStack->zShort[0] = 0;
205 }
206 58 pStack->z = pStack->zShort;
207 58 pStack->n = strlen(pStack->zShort)+1;
208 58 pStack->flags = MEM_Str | MEM_Short;
209 58 return 0;
210 }
211  
212 /*
213 ** Convert the given stack entity into a string that has been obtained
214 ** from sqliteMalloc(). This is different from Stringify() above in that
215 ** Stringify() will use the NBFS bytes of static string space if the string
216 ** will fit but this routine always mallocs for space.
217 ** Return non-zero if we run out of memory.
218 */
219 #define Dynamicify(P) (((P)->flags & MEM_Dyn)==0 ? hardDynamicify(P):0)
220 0 static int hardDynamicify(Mem *pStack){
221 0 int fg = pStack->flags;
222 char *z;
223 0 ▶ if( (fg & MEM_Str)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (fg & MEM_Str)==0 ){ 0 0 0
224 0 hardStringify(pStack);
225 }
226 assert( (fg & MEM_Dyn)==0 );
227 0 z = sqliteMallocRaw( pStack->n );
228 0 ▶ if( z==0 ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) return 1; 0 0 0
229 0 memcpy(z, pStack->z, pStack->n);
230 0 pStack->z = z;
231 0 pStack->flags |= MEM_Dyn;
232 0 return 0;
233 }
234  
235 /*
236 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
237 ** a pointer to a dynamically allocated string where some other entity
238 ** is responsible for deallocating that string. Because the stack entry
239 ** does not control the string, it might be deleted without the stack
240 ** entry knowing it.
241 **
242 ** This routine converts an ephemeral string into a dynamically allocated
243 ** string that the stack entry itself controls. In other words, it
244 ** converts an MEM_Ephem string into an MEM_Dyn string.
245 */
246 #define Deephemeralize(P) \
247 if( ((P)->flags&MEM_Ephem)!=0 && hardDeephem(P) ){ goto no_mem;}
248 0 static int hardDeephem(Mem *pStack){
249 char *z;
250 assert( (pStack->flags & MEM_Ephem)!=0 );
251 0 z = sqliteMallocRaw( pStack->n );
252 0 ▶ if( z==0 ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) return 1; 0 0 0
253 0 memcpy(z, pStack->z, pStack->n);
254 0 pStack->z = z;
255 0 pStack->flags &= ~MEM_Ephem;
256 0 pStack->flags |= MEM_Dyn;
257 0 return 0;
258 }
259  
260 /*
261 ** Release the memory associated with the given stack level. This
262 ** leaves the Mem.flags field in an inconsistent state.
263 */
264 #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); }
265  
266 /*
267 ** Pop the stack N times.
268 */
269 510 static void popStack(Mem **ppTos, int N){
270 510 Mem *pTos = *ppTos;
271 1608 ▶ while( N>0 ){
Branch: 1 branch
BranchTrueFalseTotal
while( N>0 ){ 1098 510 1608
272 1098 N--;
273 1098 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 102 996 1098
274 1098 pTos--;
275 }
276 510 *ppTos = pTos;
277 510 }
278  
279 /*
280 ** Return TRUE if zNum is a 32-bit signed integer and write
281 ** the value of the integer into *pNum. If zNum is not an integer
282 ** or is an integer that is too large to be expressed with just 32
283 ** bits, then return false.
284 **
285 ** Under Linux (RedHat 7.2) this routine is much faster than atoi()
286 ** for converting strings into integers.
287 */
288 27 static int toInt(const char *zNum, int *pNum){
289 27 int v = 0;
290 int neg;
291 int i, c;
292 27 ▶ if( *zNum=='-' ){
Branch: 1 branch
BranchTrueFalseTotal
if( *zNum=='-' ){ 0 27 27
293 0 neg = 1;
294 0 zNum++;
295 27 ▶ }else if( *zNum=='+' ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( *zNum=='+' ){ 0 27 27
296 0 neg = 0;
297 0 zNum++;
298 }else{
299 27 neg = 0;
300 }
301 54 ▶ for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
Branch: 2 branches
BranchTrueFalseTotal
for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ 27 27 54
for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ 27 0 27
302 27 v = v*10 + c - '0';
303 }
304 27 ▶ *pNum = neg ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
*pNum = neg ? -v : v; 0 27 27
305 27 ▶ return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0));
Branch: 5 branches
BranchTrueFalseTotal
return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0)); 27 0 27
return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0)); 27 0 27
return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0)); 0 27 27
return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0)); 0 0 0
return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0)); 0 0 0
306 }
307  
308 /*
309 ** Convert the given stack entity into a integer if it isn't one
310 ** already.
311 **
312 ** Any prior string or real representation is invalidated.
313 ** NULLs are converted into 0.
314 */
315 #define Integerify(P) if(((P)->flags&MEM_Int)==0){ hardIntegerify(P); }
316 0 static void hardIntegerify(Mem *pStack){
317 0 ▶ if( pStack->flags & MEM_Real ){
Branch: 1 branch
BranchTrueFalseTotal
if( pStack->flags & MEM_Real ){ 0 0 0
318 0 pStack->i = (int)pStack->r;
319 0 ▶ Release(pStack);
Branch: 1 branch
BranchTrueFalseTotal
Release(pStack); 0 0 0
320 0 ▶ }else if( pStack->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pStack->flags & MEM_Str ){ 0 0 0
321 0 toInt(pStack->z, &pStack->i);
322 0 ▶ Release(pStack);
Branch: 1 branch
BranchTrueFalseTotal
Release(pStack); 0 0 0
323 }else{
324 0 pStack->i = 0;
325 }
326 0 pStack->flags = MEM_Int;
327 0 }
328  
329 /*
330 ** Get a valid Real representation for the given stack element.
331 **
332 ** Any prior string or integer representation is retained.
333 ** NULLs are converted into 0.0.
334 */
335 #define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); }
336 0 static void hardRealify(Mem *pStack){
337 0 ▶ if( pStack->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
if( pStack->flags & MEM_Str ){ 0 0 0
338 0 pStack->r = sqliteAtoF(pStack->z, 0);
339 0 ▶ }else if( pStack->flags & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pStack->flags & MEM_Int ){ 0 0 0
340 0 pStack->r = pStack->i;
341 }else{
342 0 pStack->r = 0.0;
343 }
344 0 pStack->flags |= MEM_Real;
345 0 }
346  
347 /*
348 ** The parameters are pointers to the head of two sorted lists
349 ** of Sorter structures. Merge these two lists together and return
350 ** a single sorted list. This routine forms the core of the merge-sort
351 ** algorithm.
352 **
353 ** In the case of a tie, left sorts in front of right.
354 */
355 160 static Sorter *Merge(Sorter *pLeft, Sorter *pRight){
356 Sorter sHead;
357 Sorter *pTail;
358 160 pTail = &sHead;
359 160 pTail->pNext = 0;
360 181 ▶ while( pLeft && pRight ){
Branch: 2 branches
BranchTrueFalseTotal
while( pLeft && pRight ){ 31 150 181
while( pLeft && pRight ){ 21 10 31
361 21 int c = sqliteSortCompare(pLeft->zKey, pRight->zKey);
362 21 ▶ if( c<=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( c<=0 ){ 10 11 21
363 10 pTail->pNext = pLeft;
364 10 pLeft = pLeft->pNext;
365 }else{
366 11 pTail->pNext = pRight;
367 11 pRight = pRight->pNext;
368 }
369 21 pTail = pTail->pNext;
370 }
371 160 ▶ if( pLeft ){
Branch: 1 branch
BranchTrueFalseTotal
if( pLeft ){ 10 150 160
372 10 pTail->pNext = pLeft;
373 150 ▶ }else if( pRight ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pRight ){ 117 33 150
374 117 pTail->pNext = pRight;
375 }
376 160 return sHead.pNext;
377 }
378  
379 /*
380 ** The following routine works like a replacement for the standard
381 ** library routine fgets(). The difference is in how end-of-line (EOL)
382 ** is handled. Standard fgets() uses LF for EOL under unix, CRLF
383 ** under windows, and CR under mac. This routine accepts any of these
384 ** character sequences as an EOL mark. The EOL mark is replaced by
385 ** a single LF character in zBuf.
386 */
387 0 static char *vdbe_fgets(char *zBuf, int nBuf, FILE *in){
388 int i, c;
389 0 ▶ for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){
Branch: 2 branches
BranchTrueFalseTotal
for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){ 0 0 0
for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){ 0 0 0
390 0 zBuf[i] = c;
391 0 ▶ if( c=='\r' || c=='\n' ){
Branch: 2 branches
BranchTrueFalseTotal
if( c=='\r' || c=='\n' ){ 0 0 0
if( c=='\r' || c=='\n' ){ 0 0 0
392 0 ▶ if( c=='\r' ){
Branch: 1 branch
BranchTrueFalseTotal
if( c=='\r' ){ 0 0 0
393 0 zBuf[i] = '\n';
394 0 c = getc(in);
395 0 ▶ if( c!=EOF && c!='\n' ) ungetc(c, in);
Branch: 2 branches
BranchTrueFalseTotal
if( c!=EOF && c!='\n' ) ungetc(c, in); 0 0 0
if( c!=EOF && c!='\n' ) ungetc(c, in); 0 0 0
396 }
397 0 i++;
398 0 break;
399 }
400 }
401 0 zBuf[i] = 0;
402 0 ▶ return i>0 ? zBuf : 0;
Branch: 1 branch
BranchTrueFalseTotal
return i>0 ? zBuf : 0; 0 0 0
403 }
404  
405 /*
406 ** Make sure there is space in the Vdbe structure to hold at least
407 ** mxCursor cursors. If there is not currently enough space, then
408 ** allocate more.
409 **
410 ** If a memory allocation error occurs, return 1. Return 0 if
411 ** everything works.
412 */
413 218 static int expandCursorArraySize(Vdbe *p, int mxCursor){
414 218 ▶ if( mxCursor>=p->nCursor ){
Branch: 1 branch
BranchTrueFalseTotal
if( mxCursor>=p->nCursor ){ 201 17 218
415 201 Cursor *aCsr = sqliteRealloc( p->aCsr, (mxCursor+1)*sizeof(Cursor) );
416 201 ▶ if( aCsr==0 ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( aCsr==0 ) return 1; 0 201 201
417 201 p->aCsr = aCsr;
418 201 memset(&p->aCsr[p->nCursor], 0, sizeof(Cursor)*(mxCursor+1-p->nCursor));
419 201 p->nCursor = mxCursor+1;
420 }
421 218 return 0;
422 }
423  
424 #ifdef VDBE_PROFILE
425 /*
426 ** The following routine only works on pentium-class processors.
427 ** It uses the RDTSC opcode to read cycle count value out of the
428 ** processor and returns that value. This can be used for high-res
429 ** profiling.
430 */
431 __inline__ unsigned long long int hwtime(void){
432 unsigned long long int x;
433 __asm__("rdtsc\n\t"
434 "mov %%edx, %%ecx\n\t"
435 :"=A" (x));
436 return x;
437 }
438 #endif
439  
440 /*
441 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
442 ** sqlite_interrupt() routine has been called. If it has been, then
443 ** processing of the VDBE program is interrupted.
444 **
445 ** This macro added to every instruction that does a jump in order to
446 ** implement a loop. This test used to be on every single instruction,
447 ** but that meant we more testing that we needed. By only testing the
448 ** flag on jump instructions, we get a (small) speed improvement.
449 */
450 #define CHECK_FOR_INTERRUPT \
451 if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
452  
453  
454 /*
455 ** Execute as much of a VDBE program as we can then return.
456 **
457 ** sqliteVdbeMakeReady() must be called before this routine in order to
458 ** close the program with a final OP_Halt and to set up the callbacks
459 ** and the error message pointer.
460 **
461 ** Whenever a row or result data is available, this routine will either
462 ** invoke the result callback (if there is one) or return with
463 ** SQLITE_ROW.
464 **
465 ** If an attempt is made to open a locked database, then this routine
466 ** will either invoke the busy callback (if there is one) or it will
467 ** return SQLITE_BUSY.
468 **
469 ** If an error occurs, an error message is written to memory obtained
470 ** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
471 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
472 **
473 ** If the callback ever returns non-zero, then the program exits
474 ** immediately. There will be no error message but the p->rc field is
475 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
476 **
477 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
478 ** routine to return SQLITE_ERROR.
479 **
480 ** Other fatal errors return SQLITE_ERROR.
481 **
482 ** After this routine has finished, sqliteVdbeFinalize() should be
483 ** used to clean up the mess that was left behind.
484 */
485 433 int sqliteVdbeExec(
486 Vdbe *p /* The VDBE */
487 ){
488 int pc; /* The program counter */
489 Op *pOp; /* Current operation */
490 433 int rc = SQLITE_OK; /* Value to return */
491 433 sqlite *db = p->db; /* The database */
492 Mem *pTos; /* Top entry in the operand stack */
493 char zBuf[100]; /* Space to sprintf() an integer */
494 #ifdef VDBE_PROFILE
495 unsigned long long start; /* CPU clock count at start of opcode */
496 int origPc; /* Program counter at start of opcode */
497 #endif
498 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
499 433 int nProgressOps = 0; /* Opcodes executed since progress callback. */
500 #endif
501  
502 433 ▶ if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
Branch: 1 branch
BranchTrueFalseTotal
if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; 0 433 433
503 assert( db->magic==SQLITE_MAGIC_BUSY );
504 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
505 433 p->rc = SQLITE_OK;
506 assert( p->explain==0 );
507 433 ▶ if( sqlite_malloc_failed ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( sqlite_malloc_failed ) goto no_mem; 0 433 433
508 433 pTos = p->pTos;
509 433 ▶ if( p->popStack ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->popStack ){ 91 342 433
510 91 popStack(&pTos, p->popStack);
511 91 p->popStack = 0;
512 }
513 433 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 433 433
514 4576 ▶ for(pc=p->pc; rc==SQLITE_OK; pc++){
Branch: 1 branch
BranchTrueFalseTotal
for(pc=p->pc; rc==SQLITE_OK; pc++){ 4575 1 4576
515 assert( pc>=0 && pc<p->nOp );
516 assert( pTos<=&p->aStack[pc] );
517 #ifdef VDBE_PROFILE
518 origPc = pc;
519 start = hwtime();
520 #endif
521 4575 pOp = &p->aOp[pc];
522  
523 /* Only allow tracing if NDEBUG is not defined.
524 */
525 #ifndef NDEBUG
526 if( p->trace ){
527 sqliteVdbePrintOp(p->trace, pc, pOp);
528 }
529 #endif
530  
531 /* Check to see if we need to simulate an interrupt. This only happens
532 ** if we have a special test build.
533 */
534 #ifdef SQLITE_TEST
535 if( sqlite_interrupt_count>0 ){
536 sqlite_interrupt_count--;
537 if( sqlite_interrupt_count==0 ){
538 sqlite_interrupt(db);
539 }
540 }
541 #endif
542  
543 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
544 /* Call the progress callback if it is configured and the required number
545 ** of VDBE ops have been executed (either since this invocation of
546 ** sqliteVdbeExec() or since last time the progress callback was called).
547 ** If the progress callback returns non-zero, exit the virtual machine with
548 ** a return code SQLITE_ABORT.
549 */
550 4575 ▶ if( db->xProgress ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xProgress ){ 0 4575 4575
551 0 ▶ if( db->nProgressOps==nProgressOps ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->nProgressOps==nProgressOps ){ 0 0 0
552 0 ▶ if( db->xProgress(db->pProgressArg)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xProgress(db->pProgressArg)!=0 ){ 0 0 0
553 0 rc = SQLITE_ABORT;
554 0 continue; /* skip to the next iteration of the for loop */
555 }
556 0 nProgressOps = 0;
557 }
558 0 nProgressOps++;
559 }
560 #endif
561  
562 4575 switch( pOp->opcode ){
563  
564 /*****************************************************************************
565 ** What follows is a massive switch statement where each case implements a
566 ** separate instruction in the virtual machine. If we follow the usual
567 ** indentation conventions, each case should be indented by 6 spaces. But
568 ** that is a lot of wasted space on the left margin. So the code within
569 ** the switch statement will break with convention and be flush-left. Another
570 ** big comment (similar to this one) will mark the point in the code where
571 ** we transition back to normal indentation.
572 **
573 ** The formatting of each case is important. The makefile for SQLite
574 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
575 ** file looking for lines that begin with "case OP_". The opcodes.h files
576 ** will be filled with #defines that give unique integer values to each
577 ** opcode and the opcodes.c file is filled with an array of strings where
578 ** each string is the symbolic name for the corresponding opcode.
579 **
580 ** Documentation about VDBE opcodes is generated by scanning this file
581 ** for lines of that contain "Opcode:". That line and all subsequent
582 ** comment lines are used in the generation of the opcode.html documentation
583 ** file.
584 **
585 ** SUMMARY:
586 **
587 ** Formatting is important to scripts that scan this file.
588 ** Do not deviate from the formatting style currently in use.
589 **
590 *****************************************************************************/
591  
592 /* Opcode: Goto * P2 *
593 **
594 ** An unconditional jump to address P2.
595 ** The next instruction executed will be
596 ** the one at index P2 from the beginning of
597 ** the program.
598 */
599 37 case OP_Goto: {
600 37 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 37 37
601 37 pc = pOp->p2 - 1;
602 37 break;
603 }
604  
605 /* Opcode: Gosub * P2 *
606 **
607 ** Push the current address plus 1 onto the return address stack
608 ** and then jump to address P2.
609 **
610 ** The return address stack is of limited depth. If too many
611 ** OP_Gosub operations occur without intervening OP_Returns, then
612 ** the return address stack will fill up and processing will abort
613 ** with a fatal error.
614 */
615 0 case OP_Gosub: {
616 0 ▶ if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){ 0 0 0
617 0 sqliteSetString(&p->zErrMsg, "return address stack overflow", (char*)0);
618 0 p->rc = SQLITE_INTERNAL;
619 0 return SQLITE_ERROR;
620 }
621 0 p->returnStack[p->returnDepth++] = pc+1;
622 0 pc = pOp->p2 - 1;
623 0 break;
624 }
625  
626 /* Opcode: Return * * *
627 **
628 ** Jump immediately to the next instruction after the last unreturned
629 ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
630 ** processing aborts with a fatal error.
631 */
632 0 case OP_Return: {
633 0 ▶ if( p->returnDepth<=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->returnDepth<=0 ){ 0 0 0
634 0 sqliteSetString(&p->zErrMsg, "return address stack underflow", (char*)0);
635 0 p->rc = SQLITE_INTERNAL;
636 0 return SQLITE_ERROR;
637 }
638 0 p->returnDepth--;
639 0 pc = p->returnStack[p->returnDepth] - 1;
640 0 break;
641 }
642  
643 /* Opcode: Halt P1 P2 *
644 **
645 ** Exit immediately. All open cursors, Lists, Sorts, etc are closed
646 ** automatically.
647 **
648 ** P1 is the result code returned by sqlite_exec(). For a normal
649 ** halt, this should be SQLITE_OK (0). For errors, it can be some
650 ** other value. If P1!=0 then P2 will determine whether or not to
651 ** rollback the current transaction. Do not rollback if P2==OE_Fail.
652 ** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back
653 ** out all changes that have occurred during this execution of the
654 ** VDBE, but do not rollback the transaction.
655 **
656 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
657 ** every program. So a jump past the last instruction of the program
658 ** is the same as executing Halt.
659 */
660 337 case OP_Halt: {
661 337 p->magic = VDBE_MAGIC_HALT;
662 337 p->pTos = pTos;
663 337 ▶ if( pOp->p1!=SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1!=SQLITE_OK ){ 1 336 337
664 1 p->rc = pOp->p1;
665 1 p->errorAction = pOp->p2;
666 1 ▶ if( pOp->p3 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p3 ){ 1 0 1
667 1 sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
668 }
669 1 return SQLITE_ERROR;
670 }else{
671 336 p->rc = SQLITE_OK;
672 336 return SQLITE_DONE;
673 }
674 }
675  
676 /* Opcode: Integer P1 * P3
677 **
678 ** The integer value P1 is pushed onto the stack. If P3 is not zero
679 ** then it is assumed to be a string representation of the same integer.
680 */
681 386 case OP_Integer: {
682 386 pTos++;
683 386 pTos->i = pOp->p1;
684 386 pTos->flags = MEM_Int;
685 386 ▶ if( pOp->p3 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p3 ){ 94 292 386
686 94 pTos->z = pOp->p3;
687 94 pTos->flags |= MEM_Str | MEM_Static;
688 94 pTos->n = strlen(pOp->p3)+1;
689 }
690 386 break;
691 }
692  
693 /* Opcode: String * * P3
694 **
695 ** The string value P3 is pushed onto the stack. If P3==0 then a
696 ** NULL is pushed onto the stack.
697 */
698 309 case OP_String: {
699 309 char *z = pOp->p3;
700 309 pTos++;
701 309 ▶ if( z==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ){ 94 215 309
702 94 pTos->flags = MEM_Null;
703 }else{
704 215 pTos->z = z;
705 215 pTos->n = strlen(z) + 1;
706 215 pTos->flags = MEM_Str | MEM_Static;
707 }
708 309 break;
709 }
710  
711 /* Opcode: Variable P1 * *
712 **
713 ** Push the value of variable P1 onto the stack. A variable is
714 ** an unknown in the original SQL string as handed to sqlite_compile().
715 ** Any occurance of the '?' character in the original SQL is considered
716 ** a variable. Variables in the SQL string are number from left to
717 ** right beginning with 1. The values of variables are set using the
718 ** sqlite_bind() API.
719 */
720 0 case OP_Variable: {
721 0 int j = pOp->p1 - 1;
722 0 pTos++;
723 0 ▶ if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){
Branch: 3 branches
BranchTrueFalseTotal
if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){ 0 0 0
if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){ 0 0 0
if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){ 0 0 0
724 0 pTos->z = p->azVar[j];
725 0 pTos->n = p->anVar[j];
726 0 pTos->flags = MEM_Str | MEM_Static;
727 }else{
728 0 pTos->flags = MEM_Null;
729 }
730 0 break;
731 }
732  
733 /* Opcode: Pop P1 * *
734 **
735 ** P1 elements are popped off of the top of stack and discarded.
736 */
737 0 case OP_Pop: {
738 assert( pOp->p1>=0 );
739 0 popStack(&pTos, pOp->p1);
740 assert( pTos>=&p->aStack[-1] );
741 0 break;
742 }
743  
744 /* Opcode: Dup P1 P2 *
745 **
746 ** A copy of the P1-th element of the stack
747 ** is made and pushed onto the top of the stack.
748 ** The top of the stack is element 0. So the
749 ** instruction "Dup 0 0 0" will make a copy of the
750 ** top of the stack.
751 **
752 ** If the content of the P1-th element is a dynamically
753 ** allocated string, then a new copy of that string
754 ** is made if P2==0. If P2!=0, then just a pointer
755 ** to the string is copied.
756 **
757 ** Also see the Pull instruction.
758 */
759 80 case OP_Dup: {
760 80 Mem *pFrom = &pTos[-pOp->p1];
761 assert( pFrom<=pTos && pFrom>=p->aStack );
762 80 pTos++;
763 80 memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
764 80 ▶ if( pTos->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Str ){ 31 49 80
765 31 ▶ if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){ 31 0 31
if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){ 0 31 31
766 0 pTos->flags &= ~MEM_Dyn;
767 0 pTos->flags |= MEM_Ephem;
768 31 ▶ }else if( pTos->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pTos->flags & MEM_Short ){ 0 31 31
769 0 memcpy(pTos->zShort, pFrom->zShort, pTos->n);
770 0 pTos->z = pTos->zShort;
771 31 ▶ }else if( (pTos->flags & MEM_Static)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (pTos->flags & MEM_Static)==0 ){ 0 31 31
772 0 pTos->z = sqliteMallocRaw(pFrom->n);
773 0 ▶ if( sqlite_malloc_failed ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( sqlite_malloc_failed ) goto no_mem; 0 0 0
774 0 memcpy(pTos->z, pFrom->z, pFrom->n);
775 0 pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
776 0 pTos->flags |= MEM_Dyn;
777 }
778 }
779 80 break;
780 }
781  
782 /* Opcode: Pull P1 * *
783 **
784 ** The P1-th element is removed from its current location on
785 ** the stack and pushed back on top of the stack. The
786 ** top of the stack is element 0, so "Pull 0 0 0" is
787 ** a no-op. "Pull 1 0 0" swaps the top two elements of
788 ** the stack.
789 **
790 ** See also the Dup instruction.
791 */
792 48 case OP_Pull: {
793 48 Mem *pFrom = &pTos[-pOp->p1];
794 int i;
795 Mem ts;
796  
797 48 ts = *pFrom;
798 48 ▶ Deephemeralize(pTos);
Branch: 2 branches
BranchTrueFalseTotal
Deephemeralize(pTos); 0 48 48
Deephemeralize(pTos); 0 0 0
799 96 ▶ for(i=0; i<pOp->p1; i++, pFrom++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<pOp->p1; i++, pFrom++){ 48 48 96
800 48 ▶ Deephemeralize(&pFrom[1]);
Branch: 2 branches
BranchTrueFalseTotal
Deephemeralize(&pFrom[1]); 0 48 48
Deephemeralize(&pFrom[1]); 0 0 0
801 48 *pFrom = pFrom[1];
802 assert( (pFrom->flags & MEM_Ephem)==0 );
803 48 ▶ if( pFrom->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( pFrom->flags & MEM_Short ){ 0 48 48
804 assert( pFrom->flags & MEM_Str );
805 assert( pFrom->z==pFrom[1].zShort );
806 0 pFrom->z = pFrom->zShort;
807 }
808 }
809 48 *pTos = ts;
810 48 ▶ if( pTos->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Short ){ 3 45 48
811 assert( pTos->flags & MEM_Str );
812 assert( pTos->z==pTos[-pOp->p1].zShort );
813 3 pTos->z = pTos->zShort;
814 }
815 48 break;
816 }
817  
818 /* Opcode: Push P1 * *
819 **
820 ** Overwrite the value of the P1-th element down on the
821 ** stack (P1==0 is the top of the stack) with the value
822 ** of the top of the stack. Then pop the top of the stack.
823 */
824 0 case OP_Push: {
825 0 Mem *pTo = &pTos[-pOp->p1];
826  
827 assert( pTo>=p->aStack );
828 0 ▶ Deephemeralize(pTos);
Branch: 2 branches
BranchTrueFalseTotal
Deephemeralize(pTos); 0 0 0
Deephemeralize(pTos); 0 0 0
829 0 ▶ Release(pTo);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTo); 0 0 0
830 0 *pTo = *pTos;
831 0 ▶ if( pTo->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTo->flags & MEM_Short ){ 0 0 0
832 assert( pTo->z==pTos->zShort );
833 0 pTo->z = pTo->zShort;
834 }
835 0 pTos--;
836 0 break;
837 }
838  
839  
840 /* Opcode: ColumnName P1 P2 P3
841 **
842 ** P3 becomes the P1-th column name (first is 0). An array of pointers
843 ** to all column names is passed as the 4th parameter to the callback.
844 ** If P2==1 then this is the last column in the result set and thus the
845 ** number of columns in the result set will be P1. There must be at least
846 ** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
847 ** number of columns specified in OP_Callback must one more than the P1
848 ** value of the OP_ColumnName that has P2==1.
849 */
850 790 case OP_ColumnName: {
851 assert( pOp->p1>=0 && pOp->p1<p->nOp );
852 790 p->azColName[pOp->p1] = pOp->p3;
853 790 p->nCallback = 0;
854 790 ▶ if( pOp->p2 ) p->nResColumn = pOp->p1+1;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ) p->nResColumn = pOp->p1+1; 123 667 790
855 790 break;
856 }
857  
858 /* Opcode: Callback P1 * *
859 **
860 ** Pop P1 values off the stack and form them into an array. Then
861 ** invoke the callback function using the newly formed array as the
862 ** 3rd parameter.
863 */
864 80 case OP_Callback: {
865 int i;
866 80 char **azArgv = p->zArgv;
867 Mem *pCol;
868  
869 80 pCol = &pTos[1-pOp->p1];
870 assert( pCol>=p->aStack );
871 265 ▶ for(i=0; i<pOp->p1; i++, pCol++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<pOp->p1; i++, pCol++){ 185 80 265
872 185 ▶ if( pCol->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pCol->flags & MEM_Null ){ 17 168 185
873 17 azArgv[i] = 0;
874 }else{
875 168 ▶ Stringify(pCol);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pCol); 18 150 168
876 168 azArgv[i] = pCol->z;
877 }
878 }
879 80 azArgv[i] = 0;
880 80 p->nCallback++;
881 80 p->azResColumn = azArgv;
882 assert( p->nResColumn==pOp->p1 );
883 80 p->popStack = pOp->p1;
884 80 p->pc = pc + 1;
885 80 p->pTos = pTos;
886 80 return SQLITE_ROW;
887 }
888  
889 /* Opcode: Concat P1 P2 P3
890 **
891 ** Look at the first P1 elements of the stack. Append them all
892 ** together with the lowest element first. Use P3 as a separator.
893 ** Put the result on the top of the stack. The original P1 elements
894 ** are popped from the stack if P2==0 and retained if P2==1. If
895 ** any element of the stack is NULL, then the result is NULL.
896 **
897 ** If P3 is NULL, then use no separator. When P1==1, this routine
898 ** makes a copy of the top stack element into memory obtained
899 ** from sqliteMalloc().
900 */
901 0 case OP_Concat: {
902 char *zNew;
903 int nByte;
904 int nField;
905 int i, j;
906 char *zSep;
907 int nSep;
908 Mem *pTerm;
909  
910 0 nField = pOp->p1;
911 0 zSep = pOp->p3;
912 0 ▶ if( zSep==0 ) zSep = "";
Branch: 1 branch
BranchTrueFalseTotal
if( zSep==0 ) zSep = ""; 0 0 0
913 0 nSep = strlen(zSep);
914 assert( &pTos[1-nField] >= p->aStack );
915 0 nByte = 1 - nSep;
916 0 pTerm = &pTos[1-nField];
917 0 ▶ for(i=0; i<nField; i++, pTerm++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<nField; i++, pTerm++){ 0 0 0
918 0 ▶ if( pTerm->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTerm->flags & MEM_Null ){ 0 0 0
919 0 nByte = -1;
920 0 break;
921 }else{
922 0 ▶ Stringify(pTerm);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTerm); 0 0 0
923 0 nByte += pTerm->n - 1 + nSep;
924 }
925 }
926 0 ▶ if( nByte<0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte<0 ){ 0 0 0
927 0 ▶ if( pOp->p2==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2==0 ){ 0 0 0
928 0 popStack(&pTos, nField);
929 }
930 0 pTos++;
931 0 pTos->flags = MEM_Null;
932 0 break;
933 }
934 0 zNew = sqliteMallocRaw( nByte );
935 0 ▶ if( zNew==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( zNew==0 ) goto no_mem; 0 0 0
936 0 j = 0;
937 0 pTerm = &pTos[1-nField];
938 0 ▶ for(i=j=0; i<nField; i++, pTerm++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=j=0; i<nField; i++, pTerm++){ 0 0 0
939 assert( pTerm->flags & MEM_Str );
940 0 memcpy(&zNew[j], pTerm->z, pTerm->n-1);
941 0 j += pTerm->n-1;
942 0 ▶ if( nSep>0 && i<nField-1 ){
Branch: 2 branches
BranchTrueFalseTotal
if( nSep>0 && i<nField-1 ){ 0 0 0
if( nSep>0 && i<nField-1 ){ 0 0 0
943 0 memcpy(&zNew[j], zSep, nSep);
944 0 j += nSep;
945 }
946 }
947 0 zNew[j] = 0;
948 0 ▶ if( pOp->p2==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2==0 ){ 0 0 0
949 0 popStack(&pTos, nField);
950 }
951 0 pTos++;
952 0 pTos->n = nByte;
953 0 pTos->flags = MEM_Str|MEM_Dyn;
954 0 pTos->z = zNew;
955 0 break;
956 }
957  
958 /* Opcode: Add * * *
959 **
960 ** Pop the top two elements from the stack, add them together,
961 ** and push the result back onto the stack. If either element
962 ** is a string then it is converted to a double using the atof()
963 ** function before the addition.
964 ** If either operand is NULL, the result is NULL.
965 */
966 /* Opcode: Multiply * * *
967 **
968 ** Pop the top two elements from the stack, multiply them together,
969 ** and push the result back onto the stack. If either element
970 ** is a string then it is converted to a double using the atof()
971 ** function before the multiplication.
972 ** If either operand is NULL, the result is NULL.
973 */
974 /* Opcode: Subtract * * *
975 **
976 ** Pop the top two elements from the stack, subtract the
977 ** first (what was on top of the stack) from the second (the
978 ** next on stack)
979 ** and push the result back onto the stack. If either element
980 ** is a string then it is converted to a double using the atof()
981 ** function before the subtraction.
982 ** If either operand is NULL, the result is NULL.
983 */
984 /* Opcode: Divide * * *
985 **
986 ** Pop the top two elements from the stack, divide the
987 ** first (what was on top of the stack) from the second (the
988 ** next on stack)
989 ** and push the result back onto the stack. If either element
990 ** is a string then it is converted to a double using the atof()
991 ** function before the division. Division by zero returns NULL.
992 ** If either operand is NULL, the result is NULL.
993 */
994 /* Opcode: Remainder * * *
995 **
996 ** Pop the top two elements from the stack, divide the
997 ** first (what was on top of the stack) from the second (the
998 ** next on stack)
999 ** and push the remainder after division onto the stack. If either element
1000 ** is a string then it is converted to a double using the atof()
1001 ** function before the division. Division by zero returns NULL.
1002 ** If either operand is NULL, the result is NULL.
1003 */
1004 0 case OP_Add:
1005 case OP_Subtract:
1006 case OP_Multiply:
1007 case OP_Divide:
1008 case OP_Remainder: {
1009 0 Mem *pNos = &pTos[-1];
1010 assert( pNos>=p->aStack );
1011 0 ▶ if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){ 0 0 0
1012 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1013 0 pTos--;
1014 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1015 0 pTos->flags = MEM_Null;
1016 0 ▶ }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ 0 0 0
1017 int a, b;
1018 0 a = pTos->i;
1019 0 b = pNos->i;
1020 0 switch( pOp->opcode ){
1021 0 case OP_Add: b += a; break;
1022 0 case OP_Subtract: b -= a; break;
1023 0 case OP_Multiply: b *= a; break;
1024 0 case OP_Divide: {
1025 0 ▶ if( a==0 ) goto divide_by_zero;
Branch: 1 branch
BranchTrueFalseTotal
if( a==0 ) goto divide_by_zero; 0 0 0
1026 0 b /= a;
1027 0 break;
1028 }
1029 0 default: {
1030 0 ▶ if( a==0 ) goto divide_by_zero;
Branch: 1 branch
BranchTrueFalseTotal
if( a==0 ) goto divide_by_zero; 0 0 0
1031 0 b %= a;
1032 0 break;
1033 }
1034 }
1035 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1036 0 pTos--;
1037 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1038 0 pTos->i = b;
1039 0 pTos->flags = MEM_Int;
1040 }else{
1041 double a, b;
1042 0 ▶ Realify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Realify(pTos); 0 0 0
1043 0 ▶ Realify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Realify(pNos); 0 0 0
1044 0 a = pTos->r;
1045 0 b = pNos->r;
1046 0 switch( pOp->opcode ){
1047 0 case OP_Add: b += a; break;
1048 0 case OP_Subtract: b -= a; break;
1049 0 case OP_Multiply: b *= a; break;
1050 0 case OP_Divide: {
1051 0 ▶ if( a==0.0 ) goto divide_by_zero;
Branch: 1 branch
BranchTrueFalseTotal
if( a==0.0 ) goto divide_by_zero; 0 0 0
1052 0 b /= a;
1053 0 break;
1054 }
1055 0 default: {
1056 0 int ia = (int)a;
1057 0 int ib = (int)b;
1058 0 ▶ if( ia==0.0 ) goto divide_by_zero;
Branch: 1 branch
BranchTrueFalseTotal
if( ia==0.0 ) goto divide_by_zero; 0 0 0
1059 0 b = ib % ia;
1060 0 break;
1061 }
1062 }
1063 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1064 0 pTos--;
1065 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1066 0 pTos->r = b;
1067 0 pTos->flags = MEM_Real;
1068 }
1069 0 break;
1070  
1071 0 divide_by_zero:
1072 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1073 0 pTos--;
1074 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1075 0 pTos->flags = MEM_Null;
1076 0 break;
1077 }
1078  
1079 /* Opcode: Function P1 * P3
1080 **
1081 ** Invoke a user function (P3 is a pointer to a Function structure that
1082 ** defines the function) with P1 string arguments taken from the stack.
1083 ** Pop all arguments from the stack and push back the result.
1084 **
1085 ** See also: AggFunc
1086 */
1087 65 case OP_Function: {
1088 int n, i;
1089 Mem *pArg;
1090 char **azArgv;
1091 sqlite_func ctx;
1092  
1093 65 n = pOp->p1;
1094 65 pArg = &pTos[1-n];
1095 65 azArgv = p->zArgv;
1096 157 ▶ for(i=0; i<n; i++, pArg++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<n; i++, pArg++){ 92 65 157
1097 92 ▶ if( pArg->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pArg->flags & MEM_Null ){ 2 90 92
1098 2 azArgv[i] = 0;
1099 }else{
1100 90 ▶ Stringify(pArg);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pArg); 0 90 90
1101 90 azArgv[i] = pArg->z;
1102 }
1103 }
1104 65 ctx.pFunc = (FuncDef*)pOp->p3;
1105 65 ctx.s.flags = MEM_Null;
1106 65 ctx.s.z = 0;
1107 65 ctx.isError = 0;
1108 65 ctx.isStep = 0;
1109 65 ▶ if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 0 65 65
1110 65 (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
1111 65 ▶ if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; 0 65 65
1112 65 popStack(&pTos, n);
1113 65 pTos++;
1114 65 *pTos = ctx.s;
1115 65 ▶ if( pTos->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Short ){ 24 41 65
1116 24 pTos->z = pTos->zShort;
1117 }
1118 65 ▶ if( ctx.isError ){
Branch: 1 branch
BranchTrueFalseTotal
if( ctx.isError ){ 1 64 65
1119 1 sqliteSetString(&p->zErrMsg,
1120 1 ▶ (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
Branch: 1 branch
BranchTrueFalseTotal
(pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0); 1 0 1
1121 1 rc = SQLITE_ERROR;
1122 }
1123 65 break;
1124 }
1125  
1126 /* Opcode: BitAnd * * *
1127 **
1128 ** Pop the top two elements from the stack. Convert both elements
1129 ** to integers. Push back onto the stack the bit-wise AND of the
1130 ** two elements.
1131 ** If either operand is NULL, the result is NULL.
1132 */
1133 /* Opcode: BitOr * * *
1134 **
1135 ** Pop the top two elements from the stack. Convert both elements
1136 ** to integers. Push back onto the stack the bit-wise OR of the
1137 ** two elements.
1138 ** If either operand is NULL, the result is NULL.
1139 */
1140 /* Opcode: ShiftLeft * * *
1141 **
1142 ** Pop the top two elements from the stack. Convert both elements
1143 ** to integers. Push back onto the stack the top element shifted
1144 ** left by N bits where N is the second element on the stack.
1145 ** If either operand is NULL, the result is NULL.
1146 */
1147 /* Opcode: ShiftRight * * *
1148 **
1149 ** Pop the top two elements from the stack. Convert both elements
1150 ** to integers. Push back onto the stack the top element shifted
1151 ** right by N bits where N is the second element on the stack.
1152 ** If either operand is NULL, the result is NULL.
1153 */
1154 0 case OP_BitAnd:
1155 case OP_BitOr:
1156 case OP_ShiftLeft:
1157 case OP_ShiftRight: {
1158 0 Mem *pNos = &pTos[-1];
1159 int a, b;
1160  
1161 assert( pNos>=p->aStack );
1162 0 ▶ if( (pTos->flags | pNos->flags) & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pTos->flags | pNos->flags) & MEM_Null ){ 0 0 0
1163 0 popStack(&pTos, 2);
1164 0 pTos++;
1165 0 pTos->flags = MEM_Null;
1166 0 break;
1167 }
1168 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
1169 0 ▶ Integerify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pNos); 0 0 0
1170 0 a = pTos->i;
1171 0 b = pNos->i;
1172 0 switch( pOp->opcode ){
1173 0 case OP_BitAnd: a &= b; break;
1174 0 case OP_BitOr: a |= b; break;
1175 0 case OP_ShiftLeft: a <<= b; break;
1176 0 case OP_ShiftRight: a >>= b; break;
1177 0 default: /* CANT HAPPEN */ break;
1178 }
1179 assert( (pTos->flags & MEM_Dyn)==0 );
1180 assert( (pNos->flags & MEM_Dyn)==0 );
1181 0 pTos--;
1182 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1183 0 pTos->i = a;
1184 0 pTos->flags = MEM_Int;
1185 0 break;
1186 }
1187  
1188 /* Opcode: AddImm P1 * *
1189 **
1190 ** Add the value P1 to whatever is on top of the stack. The result
1191 ** is always an integer.
1192 **
1193 ** To force the top of the stack to be an integer, just add 0.
1194 */
1195 0 case OP_AddImm: {
1196 assert( pTos>=p->aStack );
1197 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
1198 0 pTos->i += pOp->p1;
1199 0 break;
1200 }
1201  
1202 /* Opcode: ForceInt P1 P2 *
1203 **
1204 ** Convert the top of the stack into an integer. If the current top of
1205 ** the stack is not numeric (meaning that is is a NULL or a string that
1206 ** does not look like an integer or floating point number) then pop the
1207 ** stack and jump to P2. If the top of the stack is numeric then
1208 ** convert it into the least integer that is greater than or equal to its
1209 ** current value if P1==0, or to the least integer that is strictly
1210 ** greater than its current value if P1==1.
1211 */
1212 0 case OP_ForceInt: {
1213 int v;
1214 assert( pTos>=p->aStack );
1215 0 ▶ if( (pTos->flags & (MEM_Int|MEM_Real))==0
Branch: 1 branch
BranchTrueFalseTotal
if( (pTos->flags & (MEM_Int|MEM_Real))==0 0 0 0
1216 0 ▶ && ((pTos->flags & MEM_Str)==0 || sqliteIsNumber(pTos->z)==0) ){
Branch: 2 branches
BranchTrueFalseTotal
&& ((pTos->flags & MEM_Str)==0 || sqliteIsNumber(pTos->z)==0) ){ 0 0 0
&& ((pTos->flags & MEM_Str)==0 || sqliteIsNumber(pTos->z)==0) ){ 0 0 0
1217 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1218 0 pTos--;
1219 0 pc = pOp->p2 - 1;
1220 0 break;
1221 }
1222 0 ▶ if( pTos->flags & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Int ){ 0 0 0
1223 0 v = pTos->i + (pOp->p1!=0);
1224 }else{
1225 0 ▶ Realify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Realify(pTos); 0 0 0
1226 0 v = (int)pTos->r;
1227 0 ▶ if( pTos->r>(double)v ) v++;
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->r>(double)v ) v++; 0 0 0
1228 0 ▶ if( pOp->p1 && pTos->r==(double)v ) v++;
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p1 && pTos->r==(double)v ) v++; 0 0 0
if( pOp->p1 && pTos->r==(double)v ) v++; 0 0 0
1229 }
1230 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1231 0 pTos->i = v;
1232 0 pTos->flags = MEM_Int;
1233 0 break;
1234 }
1235  
1236 /* Opcode: MustBeInt P1 P2 *
1237 **
1238 ** Force the top of the stack to be an integer. If the top of the
1239 ** stack is not an integer and cannot be converted into an integer
1240 ** with out data loss, then jump immediately to P2, or if P2==0
1241 ** raise an SQLITE_MISMATCH exception.
1242 **
1243 ** If the top of the stack is not an integer and P2 is not zero and
1244 ** P1 is 1, then the stack is popped. In all other cases, the depth
1245 ** of the stack is unchanged.
1246 */
1247 0 case OP_MustBeInt: {
1248 assert( pTos>=p->aStack );
1249 0 ▶ if( pTos->flags & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Int ){ 0 0 0
1250 /* Do nothing */
1251 0 ▶ }else if( pTos->flags & MEM_Real ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pTos->flags & MEM_Real ){ 0 0 0
1252 0 int i = (int)pTos->r;
1253 0 double r = (double)i;
1254 0 ▶ if( r!=pTos->r ){
Branch: 1 branch
BranchTrueFalseTotal
if( r!=pTos->r ){ 0 0 0
1255 0 goto mismatch;
1256 }
1257 0 pTos->i = i;
1258 0 ▶ }else if( pTos->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pTos->flags & MEM_Str ){ 0 0 0
1259 int v;
1260 0 ▶ if( !toInt(pTos->z, &v) ){
Branch: 1 branch
BranchTrueFalseTotal
if( !toInt(pTos->z, &v) ){ 0 0 0
1261 double r;
1262 0 ▶ if( !sqliteIsNumber(pTos->z) ){
Branch: 1 branch
BranchTrueFalseTotal
if( !sqliteIsNumber(pTos->z) ){ 0 0 0
1263 0 goto mismatch;
1264 }
1265 0 ▶ Realify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Realify(pTos); 0 0 0
1266 0 v = (int)pTos->r;
1267 0 r = (double)v;
1268 0 ▶ if( r!=pTos->r ){
Branch: 1 branch
BranchTrueFalseTotal
if( r!=pTos->r ){ 0 0 0
1269 0 goto mismatch;
1270 }
1271 }
1272 0 pTos->i = v;
1273 }else{
1274 0 goto mismatch;
1275 }
1276 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1277 0 pTos->flags = MEM_Int;
1278 0 break;
1279  
1280 0 mismatch:
1281 0 ▶ if( pOp->p2==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2==0 ){ 0 0 0
1282 0 rc = SQLITE_MISMATCH;
1283 0 goto abort_due_to_error;
1284 }else{
1285 0 ▶ if( pOp->p1 ) popStack(&pTos, 1);
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1 ) popStack(&pTos, 1); 0 0 0
1286 0 pc = pOp->p2 - 1;
1287 }
1288 0 break;
1289 }
1290  
1291 /* Opcode: Eq P1 P2 *
1292 **
1293 ** Pop the top two elements from the stack. If they are equal, then
1294 ** jump to instruction P2. Otherwise, continue to the next instruction.
1295 **
1296 ** If either operand is NULL (and thus if the result is unknown) then
1297 ** take the jump if P1 is true.
1298 **
1299 ** If both values are numeric, they are converted to doubles using atof()
1300 ** and compared for equality that way. Otherwise the strcmp() library
1301 ** routine is used for the comparison. For a pure text comparison
1302 ** use OP_StrEq.
1303 **
1304 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1305 ** stack if the jump would have been taken, or a 0 if not. Push a
1306 ** NULL if either operand was NULL.
1307 */
1308 /* Opcode: Ne P1 P2 *
1309 **
1310 ** Pop the top two elements from the stack. If they are not equal, then
1311 ** jump to instruction P2. Otherwise, continue to the next instruction.
1312 **
1313 ** If either operand is NULL (and thus if the result is unknown) then
1314 ** take the jump if P1 is true.
1315 **
1316 ** If both values are numeric, they are converted to doubles using atof()
1317 ** and compared in that format. Otherwise the strcmp() library
1318 ** routine is used for the comparison. For a pure text comparison
1319 ** use OP_StrNe.
1320 **
1321 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1322 ** stack if the jump would have been taken, or a 0 if not. Push a
1323 ** NULL if either operand was NULL.
1324 */
1325 /* Opcode: Lt P1 P2 *
1326 **
1327 ** Pop the top two elements from the stack. If second element (the
1328 ** next on stack) is less than the first (the top of stack), then
1329 ** jump to instruction P2. Otherwise, continue to the next instruction.
1330 ** In other words, jump if NOS<TOS.
1331 **
1332 ** If either operand is NULL (and thus if the result is unknown) then
1333 ** take the jump if P1 is true.
1334 **
1335 ** If both values are numeric, they are converted to doubles using atof()
1336 ** and compared in that format. Numeric values are always less than
1337 ** non-numeric values. If both operands are non-numeric, the strcmp() library
1338 ** routine is used for the comparison. For a pure text comparison
1339 ** use OP_StrLt.
1340 **
1341 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1342 ** stack if the jump would have been taken, or a 0 if not. Push a
1343 ** NULL if either operand was NULL.
1344 */
1345 /* Opcode: Le P1 P2 *
1346 **
1347 ** Pop the top two elements from the stack. If second element (the
1348 ** next on stack) is less than or equal to the first (the top of stack),
1349 ** then jump to instruction P2. In other words, jump if NOS<=TOS.
1350 **
1351 ** If either operand is NULL (and thus if the result is unknown) then
1352 ** take the jump if P1 is true.
1353 **
1354 ** If both values are numeric, they are converted to doubles using atof()
1355 ** and compared in that format. Numeric values are always less than
1356 ** non-numeric values. If both operands are non-numeric, the strcmp() library
1357 ** routine is used for the comparison. For a pure text comparison
1358 ** use OP_StrLe.
1359 **
1360 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1361 ** stack if the jump would have been taken, or a 0 if not. Push a
1362 ** NULL if either operand was NULL.
1363 */
1364 /* Opcode: Gt P1 P2 *
1365 **
1366 ** Pop the top two elements from the stack. If second element (the
1367 ** next on stack) is greater than the first (the top of stack),
1368 ** then jump to instruction P2. In other words, jump if NOS>TOS.
1369 **
1370 ** If either operand is NULL (and thus if the result is unknown) then
1371 ** take the jump if P1 is true.
1372 **
1373 ** If both values are numeric, they are converted to doubles using atof()
1374 ** and compared in that format. Numeric values are always less than
1375 ** non-numeric values. If both operands are non-numeric, the strcmp() library
1376 ** routine is used for the comparison. For a pure text comparison
1377 ** use OP_StrGt.
1378 **
1379 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1380 ** stack if the jump would have been taken, or a 0 if not. Push a
1381 ** NULL if either operand was NULL.
1382 */
1383 /* Opcode: Ge P1 P2 *
1384 **
1385 ** Pop the top two elements from the stack. If second element (the next
1386 ** on stack) is greater than or equal to the first (the top of stack),
1387 ** then jump to instruction P2. In other words, jump if NOS>=TOS.
1388 **
1389 ** If either operand is NULL (and thus if the result is unknown) then
1390 ** take the jump if P1 is true.
1391 **
1392 ** If both values are numeric, they are converted to doubles using atof()
1393 ** and compared in that format. Numeric values are always less than
1394 ** non-numeric values. If both operands are non-numeric, the strcmp() library
1395 ** routine is used for the comparison. For a pure text comparison
1396 ** use OP_StrGe.
1397 **
1398 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1399 ** stack if the jump would have been taken, or a 0 if not. Push a
1400 ** NULL if either operand was NULL.
1401 */
1402 46 case OP_Eq:
1403 case OP_Ne:
1404 case OP_Lt:
1405 case OP_Le:
1406 case OP_Gt:
1407 case OP_Ge: {
1408 46 Mem *pNos = &pTos[-1];
1409 int c, v;
1410 int ft, fn;
1411 assert( pNos>=p->aStack );
1412 46 ft = pTos->flags;
1413 46 fn = pNos->flags;
1414 46 ▶ if( (ft | fn) & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( (ft | fn) & MEM_Null ){ 4 42 46
1415 4 popStack(&pTos, 2);
1416 4 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 4 0 4
1417 4 ▶ if( pOp->p1 ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1 ) pc = pOp->p2-1; 4 0 4
1418 }else{
1419 0 pTos++;
1420 0 pTos->flags = MEM_Null;
1421 }
1422 4 break;
1423 42 ▶ }else if( (ft & fn & MEM_Int)==MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (ft & fn & MEM_Int)==MEM_Int ){ 0 42 42
1424 0 c = pNos->i - pTos->i;
1425 42 ▶ }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){
Branch: 3 branches
BranchTrueFalseTotal
}else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){ 27 15 42
}else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){ 27 0 27
}else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){ 27 0 27
1426 27 c = v - pTos->i;
1427 15 ▶ }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){
Branch: 3 branches
BranchTrueFalseTotal
}else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){ 0 15 15
}else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){ 0 0 0
}else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){ 0 0 0
1428 0 c = pNos->i - v;
1429 }else{
1430 15 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 15 15
1431 15 ▶ Stringify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pNos); 0 15 15
1432 15 c = sqliteCompare(pNos->z, pTos->z);
1433 }
1434 42 switch( pOp->opcode ){
1435 0 case OP_Eq: c = c==0; break;
1436 37 case OP_Ne: c = c!=0; break;
1437 5 case OP_Lt: c = c<0; break;
1438 0 case OP_Le: c = c<=0; break;
1439 0 case OP_Gt: c = c>0; break;
1440 0 default: c = c>=0; break;
1441 }
1442 42 popStack(&pTos, 2);
1443 42 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 42 0 42
1444 42 ▶ if( c ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( c ) pc = pOp->p2-1; 10 32 42
1445 }else{
1446 0 pTos++;
1447 0 pTos->i = c;
1448 0 pTos->flags = MEM_Int;
1449 }
1450 42 break;
1451 }
1452 /* INSERT NO CODE HERE!
1453 **
1454 ** The opcode numbers are extracted from this source file by doing
1455 **
1456 ** grep '^case OP_' vdbe.c | ... >opcodes.h
1457 **
1458 ** The opcodes are numbered in the order that they appear in this file.
1459 ** But in order for the expression generating code to work right, the
1460 ** string comparison operators that follow must be numbered exactly 6
1461 ** greater than the numeric comparison opcodes above. So no other
1462 ** cases can appear between the two.
1463 */
1464 /* Opcode: StrEq P1 P2 *
1465 **
1466 ** Pop the top two elements from the stack. If they are equal, then
1467 ** jump to instruction P2. Otherwise, continue to the next instruction.
1468 **
1469 ** If either operand is NULL (and thus if the result is unknown) then
1470 ** take the jump if P1 is true.
1471 **
1472 ** The strcmp() library routine is used for the comparison. For a
1473 ** numeric comparison, use OP_Eq.
1474 **
1475 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1476 ** stack if the jump would have been taken, or a 0 if not. Push a
1477 ** NULL if either operand was NULL.
1478 */
1479 /* Opcode: StrNe P1 P2 *
1480 **
1481 ** Pop the top two elements from the stack. If they are not equal, then
1482 ** jump to instruction P2. Otherwise, continue to the next instruction.
1483 **
1484 ** If either operand is NULL (and thus if the result is unknown) then
1485 ** take the jump if P1 is true.
1486 **
1487 ** The strcmp() library routine is used for the comparison. For a
1488 ** numeric comparison, use OP_Ne.
1489 **
1490 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1491 ** stack if the jump would have been taken, or a 0 if not. Push a
1492 ** NULL if either operand was NULL.
1493 */
1494 /* Opcode: StrLt P1 P2 *
1495 **
1496 ** Pop the top two elements from the stack. If second element (the
1497 ** next on stack) is less than the first (the top of stack), then
1498 ** jump to instruction P2. Otherwise, continue to the next instruction.
1499 ** In other words, jump if NOS<TOS.
1500 **
1501 ** If either operand is NULL (and thus if the result is unknown) then
1502 ** take the jump if P1 is true.
1503 **
1504 ** The strcmp() library routine is used for the comparison. For a
1505 ** numeric comparison, use OP_Lt.
1506 **
1507 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1508 ** stack if the jump would have been taken, or a 0 if not. Push a
1509 ** NULL if either operand was NULL.
1510 */
1511 /* Opcode: StrLe P1 P2 *
1512 **
1513 ** Pop the top two elements from the stack. If second element (the
1514 ** next on stack) is less than or equal to the first (the top of stack),
1515 ** then jump to instruction P2. In other words, jump if NOS<=TOS.
1516 **
1517 ** If either operand is NULL (and thus if the result is unknown) then
1518 ** take the jump if P1 is true.
1519 **
1520 ** The strcmp() library routine is used for the comparison. For a
1521 ** numeric comparison, use OP_Le.
1522 **
1523 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1524 ** stack if the jump would have been taken, or a 0 if not. Push a
1525 ** NULL if either operand was NULL.
1526 */
1527 /* Opcode: StrGt P1 P2 *
1528 **
1529 ** Pop the top two elements from the stack. If second element (the
1530 ** next on stack) is greater than the first (the top of stack),
1531 ** then jump to instruction P2. In other words, jump if NOS>TOS.
1532 **
1533 ** If either operand is NULL (and thus if the result is unknown) then
1534 ** take the jump if P1 is true.
1535 **
1536 ** The strcmp() library routine is used for the comparison. For a
1537 ** numeric comparison, use OP_Gt.
1538 **
1539 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1540 ** stack if the jump would have been taken, or a 0 if not. Push a
1541 ** NULL if either operand was NULL.
1542 */
1543 /* Opcode: StrGe P1 P2 *
1544 **
1545 ** Pop the top two elements from the stack. If second element (the next
1546 ** on stack) is greater than or equal to the first (the top of stack),
1547 ** then jump to instruction P2. In other words, jump if NOS>=TOS.
1548 **
1549 ** If either operand is NULL (and thus if the result is unknown) then
1550 ** take the jump if P1 is true.
1551 **
1552 ** The strcmp() library routine is used for the comparison. For a
1553 ** numeric comparison, use OP_Ge.
1554 **
1555 ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1556 ** stack if the jump would have been taken, or a 0 if not. Push a
1557 ** NULL if either operand was NULL.
1558 */
1559 0 case OP_StrEq:
1560 case OP_StrNe:
1561 case OP_StrLt:
1562 case OP_StrLe:
1563 case OP_StrGt:
1564 case OP_StrGe: {
1565 0 Mem *pNos = &pTos[-1];
1566 int c;
1567 assert( pNos>=p->aStack );
1568 0 ▶ if( (pNos->flags | pTos->flags) & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pNos->flags | pTos->flags) & MEM_Null ){ 0 0 0
1569 0 popStack(&pTos, 2);
1570 0 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 0 0 0
1571 0 ▶ if( pOp->p1 ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1 ) pc = pOp->p2-1; 0 0 0
1572 }else{
1573 0 pTos++;
1574 0 pTos->flags = MEM_Null;
1575 }
1576 0 break;
1577 }else{
1578 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
1579 0 ▶ Stringify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pNos); 0 0 0
1580 0 c = strcmp(pNos->z, pTos->z);
1581 }
1582 /* The asserts on each case of the following switch are there to verify
1583 ** that string comparison opcodes are always exactly 6 greater than the
1584 ** corresponding numeric comparison opcodes. The code generator depends
1585 ** on this fact.
1586 */
1587 0 switch( pOp->opcode ){
1588 0 case OP_StrEq: c = c==0; assert( pOp->opcode-6==OP_Eq ); break;
1589 0 case OP_StrNe: c = c!=0; assert( pOp->opcode-6==OP_Ne ); break;
1590 0 case OP_StrLt: c = c<0; assert( pOp->opcode-6==OP_Lt ); break;
1591 0 case OP_StrLe: c = c<=0; assert( pOp->opcode-6==OP_Le ); break;
1592 0 case OP_StrGt: c = c>0; assert( pOp->opcode-6==OP_Gt ); break;
1593 0 default: c = c>=0; assert( pOp->opcode-6==OP_Ge ); break;
1594 }
1595 0 popStack(&pTos, 2);
1596 0 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 0 0 0
1597 0 ▶ if( c ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( c ) pc = pOp->p2-1; 0 0 0
1598 }else{
1599 0 pTos++;
1600 0 pTos->flags = MEM_Int;
1601 0 pTos->i = c;
1602 }
1603 0 break;
1604 }
1605  
1606 /* Opcode: And * * *
1607 **
1608 ** Pop two values off the stack. Take the logical AND of the
1609 ** two values and push the resulting boolean value back onto the
1610 ** stack.
1611 */
1612 /* Opcode: Or * * *
1613 **
1614 ** Pop two values off the stack. Take the logical OR of the
1615 ** two values and push the resulting boolean value back onto the
1616 ** stack.
1617 */
1618 0 case OP_And:
1619 case OP_Or: {
1620 0 Mem *pNos = &pTos[-1];
1621 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1622  
1623 assert( pNos>=p->aStack );
1624 0 ▶ if( pTos->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Null ){ 0 0 0
1625 0 v1 = 2;
1626 }else{
1627 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
1628 0 v1 = pTos->i==0;
1629 }
1630 0 ▶ if( pNos->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pNos->flags & MEM_Null ){ 0 0 0
1631 0 v2 = 2;
1632 }else{
1633 0 ▶ Integerify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pNos); 0 0 0
1634 0 v2 = pNos->i==0;
1635 }
1636 0 ▶ if( pOp->opcode==OP_And ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_And ){ 0 0 0
1637 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1638 0 v1 = and_logic[v1*3+v2];
1639 }else{
1640 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1641 0 v1 = or_logic[v1*3+v2];
1642 }
1643 0 popStack(&pTos, 2);
1644 0 pTos++;
1645 0 ▶ if( v1==2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( v1==2 ){ 0 0 0
1646 0 pTos->flags = MEM_Null;
1647 }else{
1648 0 pTos->i = v1==0;
1649 0 pTos->flags = MEM_Int;
1650 }
1651 0 break;
1652 }
1653  
1654 /* Opcode: Negative * * *
1655 **
1656 ** Treat the top of the stack as a numeric quantity. Replace it
1657 ** with its additive inverse. If the top of the stack is NULL
1658 ** its value is unchanged.
1659 */
1660 /* Opcode: AbsValue * * *
1661 **
1662 ** Treat the top of the stack as a numeric quantity. Replace it
1663 ** with its absolute value. If the top of the stack is NULL
1664 ** its value is unchanged.
1665 */
1666 0 case OP_Negative:
1667 case OP_AbsValue: {
1668 assert( pTos>=p->aStack );
1669 0 ▶ if( pTos->flags & MEM_Real ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Real ){ 0 0 0
1670 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1671 0 ▶ if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ 0 0 0
if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ 0 0 0
1672 0 pTos->r = -pTos->r;
1673 }
1674 0 pTos->flags = MEM_Real;
1675 0 ▶ }else if( pTos->flags & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pTos->flags & MEM_Int ){ 0 0 0
1676 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1677 0 ▶ if( pOp->opcode==OP_Negative || pTos->i<0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->opcode==OP_Negative || pTos->i<0 ){ 0 0 0
if( pOp->opcode==OP_Negative || pTos->i<0 ){ 0 0 0
1678 0 pTos->i = -pTos->i;
1679 }
1680 0 pTos->flags = MEM_Int;
1681 0 ▶ }else if( pTos->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pTos->flags & MEM_Null ){ 0 0 0
1682 /* Do nothing */
1683 }else{
1684 0 ▶ Realify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Realify(pTos); 0 0 0
1685 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1686 0 ▶ if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ 0 0 0
if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ 0 0 0
1687 0 pTos->r = -pTos->r;
1688 }
1689 0 pTos->flags = MEM_Real;
1690 }
1691 0 break;
1692 }
1693  
1694 /* Opcode: Not * * *
1695 **
1696 ** Interpret the top of the stack as a boolean value. Replace it
1697 ** with its complement. If the top of the stack is NULL its value
1698 ** is unchanged.
1699 */
1700 0 case OP_Not: {
1701 assert( pTos>=p->aStack );
1702 0 ▶ if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ 0 0 0
1703 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
1704 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1705 0 pTos->i = !pTos->i;
1706 0 pTos->flags = MEM_Int;
1707 0 break;
1708 }
1709  
1710 /* Opcode: BitNot * * *
1711 **
1712 ** Interpret the top of the stack as an value. Replace it
1713 ** with its ones-complement. If the top of the stack is NULL its
1714 ** value is unchanged.
1715 */
1716 0 case OP_BitNot: {
1717 assert( pTos>=p->aStack );
1718 0 ▶ if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ 0 0 0
1719 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
1720 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
1721 0 pTos->i = ~pTos->i;
1722 0 pTos->flags = MEM_Int;
1723 0 break;
1724 }
1725  
1726 /* Opcode: Noop * * *
1727 **
1728 ** Do nothing. This instruction is often useful as a jump
1729 ** destination.
1730 */
1731 15 case OP_Noop: {
1732 15 break;
1733 }
1734  
1735 /* Opcode: If P1 P2 *
1736 **
1737 ** Pop a single boolean from the stack. If the boolean popped is
1738 ** true, then jump to p2. Otherwise continue to the next instruction.
1739 ** An integer is false if zero and true otherwise. A string is
1740 ** false if it has zero length and true otherwise.
1741 **
1742 ** If the value popped of the stack is NULL, then take the jump if P1
1743 ** is true and fall through if P1 is false.
1744 */
1745 /* Opcode: IfNot P1 P2 *
1746 **
1747 ** Pop a single boolean from the stack. If the boolean popped is
1748 ** false, then jump to p2. Otherwise continue to the next instruction.
1749 ** An integer is false if zero and true otherwise. A string is
1750 ** false if it has zero length and true otherwise.
1751 **
1752 ** If the value popped of the stack is NULL, then take the jump if P1
1753 ** is true and fall through if P1 is false.
1754 */
1755 26 case OP_If:
1756 case OP_IfNot: {
1757 int c;
1758 assert( pTos>=p->aStack );
1759 26 ▶ if( pTos->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Null ){ 0 26 26
1760 0 c = pOp->p1;
1761 }else{
1762 26 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 26 26
1763 26 c = pTos->i;
1764 26 ▶ if( pOp->opcode==OP_IfNot ) c = !c;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_IfNot ) c = !c; 26 0 26
1765 }
1766 assert( (pTos->flags & MEM_Dyn)==0 );
1767 26 pTos--;
1768 26 ▶ if( c ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( c ) pc = pOp->p2-1; 16 10 26
1769 26 break;
1770 }
1771  
1772 /* Opcode: IsNull P1 P2 *
1773 **
1774 ** If any of the top abs(P1) values on the stack are NULL, then jump
1775 ** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack
1776 ** unchanged.
1777 */
1778 0 case OP_IsNull: {
1779 int i, cnt;
1780 Mem *pTerm;
1781 0 cnt = pOp->p1;
1782 0 ▶ if( cnt<0 ) cnt = -cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( cnt<0 ) cnt = -cnt; 0 0 0
1783 0 pTerm = &pTos[1-cnt];
1784 assert( pTerm>=p->aStack );
1785 0 ▶ for(i=0; i<cnt; i++, pTerm++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<cnt; i++, pTerm++){ 0 0 0
1786 0 ▶ if( pTerm->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTerm->flags & MEM_Null ){ 0 0 0
1787 0 pc = pOp->p2-1;
1788 0 break;
1789 }
1790 }
1791 0 ▶ if( pOp->p1>0 ) popStack(&pTos, cnt);
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1>0 ) popStack(&pTos, cnt); 0 0 0
1792 0 break;
1793 }
1794  
1795 /* Opcode: NotNull P1 P2 *
1796 **
1797 ** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the
1798 ** stack if P1 times if P1 is greater than zero. If P1 is less than
1799 ** zero then leave the stack unchanged.
1800 */
1801 67 case OP_NotNull: {
1802 int i, cnt;
1803 67 cnt = pOp->p1;
1804 67 ▶ if( cnt<0 ) cnt = -cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( cnt<0 ) cnt = -cnt; 34 33 67
1805 assert( &pTos[1-cnt] >= p->aStack );
1806 130 ▶ for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
Branch: 2 branches
BranchTrueFalseTotal
for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){} 67 63 130
for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){} 63 4 67
1807 67 ▶ if( i>=cnt ) pc = pOp->p2-1;
Branch: 1 branch
BranchTrueFalseTotal
if( i>=cnt ) pc = pOp->p2-1; 63 4 67
1808 67 ▶ if( pOp->p1>0 ) popStack(&pTos, cnt);
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p1>0 ) popStack(&pTos, cnt); 33 34 67
1809 67 break;
1810 }
1811  
1812 /* Opcode: MakeRecord P1 P2 *
1813 **
1814 ** Convert the top P1 entries of the stack into a single entry
1815 ** suitable for use as a data record in a database table. The
1816 ** details of the format are irrelavant as long as the OP_Column
1817 ** opcode can decode the record later. Refer to source code
1818 ** comments for the details of the record format.
1819 **
1820 ** If P2 is true (non-zero) and one or more of the P1 entries
1821 ** that go into building the record is NULL, then add some extra
1822 ** bytes to the record to make it distinct for other entries created
1823 ** during the same run of the VDBE. The extra bytes added are a
1824 ** counter that is reset with each run of the VDBE, so records
1825 ** created this way will not necessarily be distinct across runs.
1826 ** But they should be distinct for transient tables (created using
1827 ** OP_OpenTemp) which is what they are intended for.
1828 **
1829 ** (Later:) The P2==1 option was intended to make NULLs distinct
1830 ** for the UNION operator. But I have since discovered that NULLs
1831 ** are indistinct for UNION. So this option is never used.
1832 */
1833 96 case OP_MakeRecord: {
1834 char *zNewRecord;
1835 int nByte;
1836 int nField;
1837 int i, j;
1838 int idxWidth;
1839 u32 addr;
1840 Mem *pRec;
1841 96 int addUnique = 0; /* True to cause bytes to be added to make the
1842 ** generated record distinct */
1843 char zTemp[NBFS]; /* Temp space for small records */
1844  
1845 /* Assuming the record contains N fields, the record format looks
1846 ** like this:
1847 **
1848 ** -------------------------------------------------------------------
1849 ** | idx0 | idx1 | ... | idx(N-1) | idx(N) | data0 | ... | data(N-1) |
1850 ** -------------------------------------------------------------------
1851 **
1852 ** All data fields are converted to strings before being stored and
1853 ** are stored with their null terminators. NULL entries omit the
1854 ** null terminator. Thus an empty string uses 1 byte and a NULL uses
1855 ** zero bytes. Data(0) is taken from the lowest element of the stack
1856 ** and data(N-1) is the top of the stack.
1857 **
1858 ** Each of the idx() entries is either 1, 2, or 3 bytes depending on
1859 ** how big the total record is. Idx(0) contains the offset to the start
1860 ** of data(0). Idx(k) contains the offset to the start of data(k).
1861 ** Idx(N) contains the total number of bytes in the record.
1862 */
1863 96 nField = pOp->p1;
1864 96 pRec = &pTos[1-nField];
1865 assert( pRec>=p->aStack );
1866 96 nByte = 0;
1867 402 ▶ for(i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<nField; i++, pRec++){ 306 96 402
1868 306 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 15 291 306
1869 15 addUnique = pOp->p2;
1870 }else{
1871 291 ▶ Stringify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pRec); 27 264 291
1872 291 nByte += pRec->n;
1873 }
1874 }
1875 96 ▶ if( addUnique ) nByte += sizeof(p->uniqueCnt);
Branch: 1 branch
BranchTrueFalseTotal
if( addUnique ) nByte += sizeof(p->uniqueCnt); 0 96 96
1876 96 ▶ if( nByte + nField + 1 < 256 ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte + nField + 1 < 256 ){ 95 1 96
1877 95 idxWidth = 1;
1878 1 ▶ }else if( nByte + 2*nField + 2 < 65536 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( nByte + 2*nField + 2 < 65536 ){ 1 0 1
1879 1 idxWidth = 2;
1880 }else{
1881 0 idxWidth = 3;
1882 }
1883 96 nByte += idxWidth*(nField + 1);
1884 96 ▶ if( nByte>MAX_BYTES_PER_ROW ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte>MAX_BYTES_PER_ROW ){ 0 96 96
1885 0 rc = SQLITE_TOOBIG;
1886 0 goto abort_due_to_error;
1887 }
1888 96 ▶ if( nByte<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte<=NBFS ){ 44 52 96
1889 44 zNewRecord = zTemp;
1890 }else{
1891 52 zNewRecord = sqliteMallocRaw( nByte );
1892 52 ▶ if( zNewRecord==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( zNewRecord==0 ) goto no_mem; 0 52 52
1893 }
1894 96 j = 0;
1895 96 addr = idxWidth*(nField+1) + addUnique*sizeof(p->uniqueCnt);
1896 402 ▶ for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){ 306 96 402
1897 306 zNewRecord[j++] = addr & 0xff;
1898 306 ▶ if( idxWidth>1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>1 ){ 2 304 306
1899 2 zNewRecord[j++] = (addr>>8)&0xff;
1900 2 ▶ if( idxWidth>2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>2 ){ 0 2 2
1901 0 zNewRecord[j++] = (addr>>16)&0xff;
1902 }
1903 }
1904 306 ▶ if( (pRec->flags & MEM_Null)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pRec->flags & MEM_Null)==0 ){ 291 15 306
1905 291 addr += pRec->n;
1906 }
1907 }
1908 96 zNewRecord[j++] = addr & 0xff;
1909 96 ▶ if( idxWidth>1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>1 ){ 1 95 96
1910 1 zNewRecord[j++] = (addr>>8)&0xff;
1911 1 ▶ if( idxWidth>2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>2 ){ 0 1 1
1912 0 zNewRecord[j++] = (addr>>16)&0xff;
1913 }
1914 }
1915 96 ▶ if( addUnique ){
Branch: 1 branch
BranchTrueFalseTotal
if( addUnique ){ 0 96 96
1916 0 memcpy(&zNewRecord[j], &p->uniqueCnt, sizeof(p->uniqueCnt));
1917 0 p->uniqueCnt++;
1918 0 j += sizeof(p->uniqueCnt);
1919 }
1920 402 ▶ for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){ 306 96 402
1921 306 ▶ if( (pRec->flags & MEM_Null)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pRec->flags & MEM_Null)==0 ){ 291 15 306
1922 291 memcpy(&zNewRecord[j], pRec->z, pRec->n);
1923 291 j += pRec->n;
1924 }
1925 }
1926 96 popStack(&pTos, nField);
1927 96 pTos++;
1928 96 pTos->n = nByte;
1929 96 ▶ if( nByte<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte<=NBFS ){ 44 52 96
1930 assert( zNewRecord==zTemp );
1931 44 memcpy(pTos->zShort, zTemp, nByte);
1932 44 pTos->z = pTos->zShort;
1933 44 pTos->flags = MEM_Str | MEM_Short;
1934 }else{
1935 assert( zNewRecord!=zTemp );
1936 52 pTos->z = zNewRecord;
1937 52 pTos->flags = MEM_Str | MEM_Dyn;
1938 }
1939 96 break;
1940 }
1941  
1942 /* Opcode: MakeKey P1 P2 P3
1943 **
1944 ** Convert the top P1 entries of the stack into a single entry suitable
1945 ** for use as the key in an index. The top P1 records are
1946 ** converted to strings and merged. The null-terminators
1947 ** are retained and used as separators.
1948 ** The lowest entry in the stack is the first field and the top of the
1949 ** stack becomes the last.
1950 **
1951 ** If P2 is not zero, then the original entries remain on the stack
1952 ** and the new key is pushed on top. If P2 is zero, the original
1953 ** data is popped off the stack first then the new key is pushed
1954 ** back in its place.
1955 **
1956 ** P3 is a string that is P1 characters long. Each character is either
1957 ** an 'n' or a 't' to indicates if the argument should be intepreted as
1958 ** numeric or text type. The first character of P3 corresponds to the
1959 ** lowest element on the stack. If P3 is NULL then all arguments are
1960 ** assumed to be of the numeric type.
1961 **
1962 ** The type makes a difference in that text-type fields may not be
1963 ** introduced by 'b' (as described in the next paragraph). The
1964 ** first character of a text-type field must be either 'a' (if it is NULL)
1965 ** or 'c'. Numeric fields will be introduced by 'b' if their content
1966 ** looks like a well-formed number. Otherwise the 'a' or 'c' will be
1967 ** used.
1968 **
1969 ** The key is a concatenation of fields. Each field is terminated by
1970 ** a single 0x00 character. A NULL field is introduced by an 'a' and
1971 ** is followed immediately by its 0x00 terminator. A numeric field is
1972 ** introduced by a single character 'b' and is followed by a sequence
1973 ** of characters that represent the number such that a comparison of
1974 ** the character string using memcpy() sorts the numbers in numerical
1975 ** order. The character strings for numbers are generated using the
1976 ** sqliteRealToSortable() function. A text field is introduced by a
1977 ** 'c' character and is followed by the exact text of the field. The
1978 ** use of an 'a', 'b', or 'c' character at the beginning of each field
1979 ** guarantees that NULLs sort before numbers and that numbers sort
1980 ** before text. 0x00 characters do not occur except as separators
1981 ** between fields.
1982 **
1983 ** See also: MakeIdxKey, SortMakeKey
1984 */
1985 /* Opcode: MakeIdxKey P1 P2 P3
1986 **
1987 ** Convert the top P1 entries of the stack into a single entry suitable
1988 ** for use as the key in an index. In addition, take one additional integer
1989 ** off of the stack, treat that integer as a four-byte record number, and
1990 ** append the four bytes to the key. Thus a total of P1+1 entries are
1991 ** popped from the stack for this instruction and a single entry is pushed
1992 ** back. The first P1 entries that are popped are strings and the last
1993 ** entry (the lowest on the stack) is an integer record number.
1994 **
1995 ** The converstion of the first P1 string entries occurs just like in
1996 ** MakeKey. Each entry is separated from the others by a null.
1997 ** The entire concatenation is null-terminated. The lowest entry
1998 ** in the stack is the first field and the top of the stack becomes the
1999 ** last.
2000 **
2001 ** If P2 is not zero and one or more of the P1 entries that go into the
2002 ** generated key is NULL, then jump to P2 after the new key has been
2003 ** pushed on the stack. In other words, jump to P2 if the key is
2004 ** guaranteed to be unique. This jump can be used to skip a subsequent
2005 ** uniqueness test.
2006 **
2007 ** P3 is a string that is P1 characters long. Each character is either
2008 ** an 'n' or a 't' to indicates if the argument should be numeric or
2009 ** text. The first character corresponds to the lowest element on the
2010 ** stack. If P3 is null then all arguments are assumed to be numeric.
2011 **
2012 ** See also: MakeKey, SortMakeKey
2013 */
2014 7 case OP_MakeIdxKey:
2015 case OP_MakeKey: {
2016 char *zNewKey;
2017 int nByte;
2018 int nField;
2019 int addRowid;
2020 int i, j;
2021 7 int containsNull = 0;
2022 Mem *pRec;
2023 char zTemp[NBFS];
2024  
2025 7 addRowid = pOp->opcode==OP_MakeIdxKey;
2026 7 nField = pOp->p1;
2027 7 pRec = &pTos[1-nField];
2028 assert( pRec>=p->aStack );
2029 7 nByte = 0;
2030 15 ▶ for(j=0, i=0; i<nField; i++, j++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(j=0, i=0; i<nField; i++, j++, pRec++){ 8 7 15
2031 8 int flags = pRec->flags;
2032 int len;
2033 char *z;
2034 8 ▶ if( flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( flags & MEM_Null ){ 1 7 8
2035 1 nByte += 2;
2036 1 containsNull = 1;
2037 7 ▶ }else if( pOp->p3 && pOp->p3[j]=='t' ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( pOp->p3 && pOp->p3[j]=='t' ){ 7 0 7
}else if( pOp->p3 && pOp->p3[j]=='t' ){ 2 5 7
2038 2 ▶ Stringify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pRec); 0 2 2
2039 2 pRec->flags &= ~(MEM_Int|MEM_Real);
2040 2 nByte += pRec->n+1;
2041 5 ▶ }else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(pRec->z) ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(pRec->z) ){ 4 1 5
}else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(pRec->z) ){ 3 1 4
2042 4 ▶ if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){ 1 3 4
2043 1 pRec->r = pRec->i;
2044 3 ▶ }else if( (flags & (MEM_Real|MEM_Int))==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (flags & (MEM_Real|MEM_Int))==0 ){ 3 0 3
2045 3 pRec->r = sqliteAtoF(pRec->z, 0);
2046 }
2047 4 ▶ Release(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Release(pRec); 0 4 4
2048 4 z = pRec->zShort;
2049 4 sqliteRealToSortable(pRec->r, z);
2050 4 len = strlen(z);
2051 4 pRec->z = 0;
2052 4 pRec->flags = MEM_Real;
2053 4 pRec->n = len+1;
2054 4 nByte += pRec->n+1;
2055 }else{
2056 1 nByte += pRec->n+1;
2057 }
2058 }
2059 7 ▶ if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){ 0 7 7
2060 0 rc = SQLITE_TOOBIG;
2061 0 goto abort_due_to_error;
2062 }
2063 7 ▶ if( addRowid ) nByte += sizeof(u32);
Branch: 1 branch
BranchTrueFalseTotal
if( addRowid ) nByte += sizeof(u32); 4 3 7
2064 7 ▶ if( nByte<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte<=NBFS ){ 7 0 7
2065 7 zNewKey = zTemp;
2066 }else{
2067 0 zNewKey = sqliteMallocRaw( nByte );
2068 0 ▶ if( zNewKey==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( zNewKey==0 ) goto no_mem; 0 0 0
2069 }
2070 7 j = 0;
2071 7 pRec = &pTos[1-nField];
2072 15 ▶ for(i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<nField; i++, pRec++){ 8 7 15
2073 8 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 1 7 8
2074 1 zNewKey[j++] = 'a';
2075 1 zNewKey[j++] = 0;
2076 7 ▶ }else if( pRec->flags==MEM_Real ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pRec->flags==MEM_Real ){ 4 3 7
2077 4 zNewKey[j++] = 'b';
2078 4 memcpy(&zNewKey[j], pRec->zShort, pRec->n);
2079 4 j += pRec->n;
2080 }else{
2081 assert( pRec->flags & MEM_Str );
2082 3 zNewKey[j++] = 'c';
2083 3 memcpy(&zNewKey[j], pRec->z, pRec->n);
2084 3 j += pRec->n;
2085 }
2086 }
2087 7 ▶ if( addRowid ){
Branch: 1 branch
BranchTrueFalseTotal
if( addRowid ){ 4 3 7
2088 u32 iKey;
2089 4 pRec = &pTos[-nField];
2090 assert( pRec>=p->aStack );
2091 4 ▶ Integerify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pRec); 0 4 4
2092 4 iKey = intToKey(pRec->i);
2093 4 memcpy(&zNewKey[j], &iKey, sizeof(u32));
2094 4 popStack(&pTos, nField+1);
2095 4 ▶ if( pOp->p2 && containsNull ) pc = pOp->p2 - 1;
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p2 && containsNull ) pc = pOp->p2 - 1; 2 2 4
if( pOp->p2 && containsNull ) pc = pOp->p2 - 1; 0 2 2
2096 }else{
2097 3 ▶ if( pOp->p2==0 ) popStack(&pTos, nField);
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2==0 ) popStack(&pTos, nField); 3 0 3
2098 }
2099 7 pTos++;
2100 7 pTos->n = nByte;
2101 7 ▶ if( nByte<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( nByte<=NBFS ){ 7 0 7
2102 assert( zNewKey==zTemp );
2103 7 pTos->z = pTos->zShort;
2104 7 memcpy(pTos->zShort, zTemp, nByte);
2105 7 pTos->flags = MEM_Str | MEM_Short;
2106 }else{
2107 0 pTos->z = zNewKey;
2108 0 pTos->flags = MEM_Str | MEM_Dyn;
2109 }
2110 7 break;
2111 }
2112  
2113 /* Opcode: IncrKey * * *
2114 **
2115 ** The top of the stack should contain an index key generated by
2116 ** The MakeKey opcode. This routine increases the least significant
2117 ** byte of that key by one. This is used so that the MoveTo opcode
2118 ** will move to the first entry greater than the key rather than to
2119 ** the key itself.
2120 */
2121 0 case OP_IncrKey: {
2122 assert( pTos>=p->aStack );
2123 /* The IncrKey opcode is only applied to keys generated by
2124 ** MakeKey or MakeIdxKey and the results of those operands
2125 ** are always dynamic strings or zShort[] strings. So we
2126 ** are always free to modify the string in place.
2127 */
2128 assert( pTos->flags & (MEM_Dyn|MEM_Short) );
2129 0 pTos->z[pTos->n-1]++;
2130 0 break;
2131 }
2132  
2133 /* Opcode: Checkpoint P1 * *
2134 **
2135 ** Begin a checkpoint. A checkpoint is the beginning of a operation that
2136 ** is part of a larger transaction but which might need to be rolled back
2137 ** itself without effecting the containing transaction. A checkpoint will
2138 ** be automatically committed or rollback when the VDBE halts.
2139 **
2140 ** The checkpoint is begun on the database file with index P1. The main
2141 ** database file has an index of 0 and the file used for temporary tables
2142 ** has an index of 1.
2143 */
2144 0 case OP_Checkpoint: {
2145 0 int i = pOp->p1;
2146 0 ▶ if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){
Branch: 4 branches
BranchTrueFalseTotal
if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){ 0 0 0
if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){ 0 0 0
if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){ 0 0 0
if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){ 0 0 0
2147 0 rc = sqliteBtreeBeginCkpt(db->aDb[i].pBt);
2148 0 ▶ if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2;
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2; 0 0 0
2149 }
2150 0 break;
2151 }
2152  
2153 /* Opcode: Transaction P1 * *
2154 **
2155 ** Begin a transaction. The transaction ends when a Commit or Rollback
2156 ** opcode is encountered. Depending on the ON CONFLICT setting, the
2157 ** transaction might also be rolled back if an error is encountered.
2158 **
2159 ** P1 is the index of the database file on which the transaction is
2160 ** started. Index 0 is the main database file and index 1 is the
2161 ** file used for temporary tables.
2162 **
2163 ** A write lock is obtained on the database file when a transaction is
2164 ** started. No other process can read or write the file while the
2165 ** transaction is underway. Starting a transaction also creates a
2166 ** rollback journal. A transaction must be started before any changes
2167 ** can be made to the database.
2168 */
2169 153 case OP_Transaction: {
2170 153 int busy = 1;
2171 153 int i = pOp->p1;
2172 assert( i>=0 && i<db->nDb );
2173 153 ▶ if( db->aDb[i].inTrans ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( db->aDb[i].inTrans ) break; 0 153 153
2174 306 ▶ while( db->aDb[i].pBt!=0 && busy ){
Branch: 2 branches
BranchTrueFalseTotal
while( db->aDb[i].pBt!=0 && busy ){ 306 0 306
while( db->aDb[i].pBt!=0 && busy ){ 153 153 306
2175 153 rc = sqliteBtreeBeginTrans(db->aDb[i].pBt);
2176 153 switch( rc ){
2177 0 case SQLITE_BUSY: {
2178 0 ▶ if( db->xBusyCallback==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xBusyCallback==0 ){ 0 0 0
2179 0 p->pc = pc;
2180 0 p->undoTransOnError = 1;
2181 0 p->rc = SQLITE_BUSY;
2182 0 p->pTos = pTos;
2183 0 return SQLITE_BUSY;
2184 0 ▶ }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){ 0 0 0
2185 0 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
2186 0 busy = 0;
2187 }
2188 0 break;
2189 }
2190 0 case SQLITE_READONLY: {
2191 0 rc = SQLITE_OK;
2192 /* Fall thru into the next case */
2193 }
2194 153 case SQLITE_OK: {
2195 153 p->inTempTrans = 0;
2196 153 busy = 0;
2197 153 break;
2198 }
2199 0 default: {
2200 0 goto abort_due_to_error;
2201 }
2202 }
2203 }
2204 153 db->aDb[i].inTrans = 1;
2205 153 p->undoTransOnError = 1;
2206 153 break;
2207 }
2208  
2209 /* Opcode: Commit * * *
2210 **
2211 ** Cause all modifications to the database that have been made since the
2212 ** last Transaction to actually take effect. No additional modifications
2213 ** are allowed until another transaction is started. The Commit instruction
2214 ** deletes the journal file and releases the write lock on the database.
2215 ** A read lock continues to be held if there are still cursors open.
2216 */
2217 73 case OP_Commit: {
2218 int i;
2219 73 ▶ if( db->xCommitCallback!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xCommitCallback!=0 ){ 0 73 73
2220 0 ▶ if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 0 0 0
2221 0 ▶ if( db->xCommitCallback(db->pCommitArg)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xCommitCallback(db->pCommitArg)!=0 ){ 0 0 0
2222 0 rc = SQLITE_CONSTRAINT;
2223 }
2224 0 ▶ if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; 0 0 0
2225 }
2226 219 ▶ for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
Branch: 2 branches
BranchTrueFalseTotal
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 219 0 219
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 146 73 219
2227 146 ▶ if( db->aDb[i].inTrans ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->aDb[i].inTrans ){ 143 3 146
2228 143 rc = sqliteBtreeCommit(db->aDb[i].pBt);
2229 143 db->aDb[i].inTrans = 0;
2230 }
2231 }
2232 73 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 73 0 73
2233 73 sqliteCommitInternalChanges(db);
2234 }else{
2235 0 sqliteRollbackAll(db);
2236 }
2237 73 break;
2238 }
2239  
2240 /* Opcode: Rollback P1 * *
2241 **
2242 ** Cause all modifications to the database that have been made since the
2243 ** last Transaction to be undone. The database is restored to its state
2244 ** before the Transaction opcode was executed. No additional modifications
2245 ** are allowed until another transaction is started.
2246 **
2247 ** P1 is the index of the database file that is committed. An index of 0
2248 ** is used for the main database and an index of 1 is used for the file used
2249 ** to hold temporary tables.
2250 **
2251 ** This instruction automatically closes all cursors and releases both
2252 ** the read and write locks on the indicated database.
2253 */
2254 4 case OP_Rollback: {
2255 4 sqliteRollbackAll(db);
2256 4 break;
2257 }
2258  
2259 /* Opcode: ReadCookie P1 P2 *
2260 **
2261 ** Read cookie number P2 from database P1 and push it onto the stack.
2262 ** P2==0 is the schema version. P2==1 is the database format.
2263 ** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2264 ** the main database file and P1==1 is the database file used to store
2265 ** temporary tables.
2266 **
2267 ** There must be a read-lock on the database (either a transaction
2268 ** must be started or there must be an open cursor) before
2269 ** executing this instruction.
2270 */
2271 0 case OP_ReadCookie: {
2272 int aMeta[SQLITE_N_BTREE_META];
2273 assert( pOp->p2<SQLITE_N_BTREE_META );
2274 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2275 assert( db->aDb[pOp->p1].pBt!=0 );
2276 0 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2277 0 pTos++;
2278 0 pTos->i = aMeta[1+pOp->p2];
2279 0 pTos->flags = MEM_Int;
2280 0 break;
2281 }
2282  
2283 /* Opcode: SetCookie P1 P2 *
2284 **
2285 ** Write the top of the stack into cookie number P2 of database P1.
2286 ** P2==0 is the schema version. P2==1 is the database format.
2287 ** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2288 ** the main database file and P1==1 is the database file used to store
2289 ** temporary tables.
2290 **
2291 ** A transaction must be started before executing this opcode.
2292 */
2293 53 case OP_SetCookie: {
2294 int aMeta[SQLITE_N_BTREE_META];
2295 assert( pOp->p2<SQLITE_N_BTREE_META );
2296 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2297 assert( db->aDb[pOp->p1].pBt!=0 );
2298 assert( pTos>=p->aStack );
2299 53 ▶ Integerify(pTos)
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos) 0 53 53
2300 53 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2301 53 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 53 0 53
2302 53 aMeta[1+pOp->p2] = pTos->i;
2303 53 rc = sqliteBtreeUpdateMeta(db->aDb[pOp->p1].pBt, aMeta);
2304 }
2305 53 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 53 53
2306 53 pTos--;
2307 53 break;
2308 }
2309  
2310 /* Opcode: VerifyCookie P1 P2 *
2311 **
2312 ** Check the value of global database parameter number 0 (the
2313 ** schema version) and make sure it is equal to P2.
2314 ** P1 is the database number which is 0 for the main database file
2315 ** and 1 for the file holding temporary tables and some higher number
2316 ** for auxiliary databases.
2317 **
2318 ** The cookie changes its value whenever the database schema changes.
2319 ** This operation is used to detect when that the cookie has changed
2320 ** and that the current process needs to reread the schema.
2321 **
2322 ** Either a transaction needs to have been started or an OP_Open needs
2323 ** to be executed (to establish a read lock) before this opcode is
2324 ** invoked.
2325 */
2326 147 case OP_VerifyCookie: {
2327 int aMeta[SQLITE_N_BTREE_META];
2328 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2329 147 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2330 147 ▶ if( rc==SQLITE_OK && aMeta[1]!=pOp->p2 ){
Branch: 2 branches
BranchTrueFalseTotal
if( rc==SQLITE_OK && aMeta[1]!=pOp->p2 ){ 147 0 147
if( rc==SQLITE_OK && aMeta[1]!=pOp->p2 ){ 0 147 147
2331 0 sqliteSetString(&p->zErrMsg, "database schema has changed", (char*)0);
2332 0 rc = SQLITE_SCHEMA;
2333 }
2334 147 break;
2335 }
2336  
2337 /* Opcode: OpenRead P1 P2 P3
2338 **
2339 ** Open a read-only cursor for the database table whose root page is
2340 ** P2 in a database file. The database file is determined by an
2341 ** integer from the top of the stack. 0 means the main database and
2342 ** 1 means the database used for temporary tables. Give the new
2343 ** cursor an identifier of P1. The P1 values need not be contiguous
2344 ** but all P1 values should be small integers. It is an error for
2345 ** P1 to be negative.
2346 **
2347 ** If P2==0 then take the root page number from the next of the stack.
2348 **
2349 ** There will be a read lock on the database whenever there is an
2350 ** open cursor. If the database was unlocked prior to this instruction
2351 ** then a read lock is acquired as part of this instruction. A read
2352 ** lock allows other processes to read the database but prohibits
2353 ** any other process from modifying the database. The read lock is
2354 ** released when all cursors are closed. If this instruction attempts
2355 ** to get a read lock but fails, the script terminates with an
2356 ** SQLITE_BUSY error code.
2357 **
2358 ** The P3 value is the name of the table or index being opened.
2359 ** The P3 value is not actually used by this opcode and may be
2360 ** omitted. But the code generator usually inserts the index or
2361 ** table name into P3 to make the code easier to read.
2362 **
2363 ** See also OpenWrite.
2364 */
2365 /* Opcode: OpenWrite P1 P2 P3
2366 **
2367 ** Open a read/write cursor named P1 on the table or index whose root
2368 ** page is P2. If P2==0 then take the root page number from the stack.
2369 **
2370 ** The P3 value is the name of the table or index being opened.
2371 ** The P3 value is not actually used by this opcode and may be
2372 ** omitted. But the code generator usually inserts the index or
2373 ** table name into P3 to make the code easier to read.
2374 **
2375 ** This instruction works just like OpenRead except that it opens the cursor
2376 ** in read/write mode. For a given table, there can be one or more read-only
2377 ** cursors or a single read/write cursor but not both.
2378 **
2379 ** See also OpenRead.
2380 */
2381 215 case OP_OpenRead:
2382 case OP_OpenWrite: {
2383 215 int busy = 0;
2384 215 int i = pOp->p1;
2385 215 int p2 = pOp->p2;
2386 int wrFlag;
2387 Btree *pX;
2388 int iDb;
2389
2390 assert( pTos>=p->aStack );
2391 215 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 215 215
2392 215 iDb = pTos->i;
2393 215 pTos--;
2394 assert( iDb>=0 && iDb<db->nDb );
2395 215 pX = db->aDb[iDb].pBt;
2396 assert( pX!=0 );
2397 215 wrFlag = pOp->opcode==OP_OpenWrite;
2398 215 ▶ if( p2<=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p2<=0 ){ 1 214 215
2399 assert( pTos>=p->aStack );
2400 1 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 1 1
2401 1 p2 = pTos->i;
2402 1 pTos--;
2403 1 ▶ if( p2<2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p2<2 ){ 0 1 1
2404 0 sqliteSetString(&p->zErrMsg, "root page number less than 2", (char*)0);
2405 0 rc = SQLITE_INTERNAL;
2406 0 break;
2407 }
2408 }
2409 assert( i>=0 );
2410 215 ▶ if( expandCursorArraySize(p, i) ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( expandCursorArraySize(p, i) ) goto no_mem; 0 215 215
2411 215 sqliteVdbeCleanupCursor(&p->aCsr[i]);
2412 215 memset(&p->aCsr[i], 0, sizeof(Cursor));
2413 215 p->aCsr[i].nullRow = 1;
2414 215 ▶ if( pX==0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( pX==0 ) break; 0 215 215
2415 do{
2416 215 rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor);
2417 215 switch( rc ){
2418 0 case SQLITE_BUSY: {
2419 0 ▶ if( db->xBusyCallback==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->xBusyCallback==0 ){ 0 0 0
2420 0 p->pc = pc;
2421 0 p->rc = SQLITE_BUSY;
2422 0 ▶ p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
Branch: 1 branch
BranchTrueFalseTotal
p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */ 0 0 0
2423 0 return SQLITE_BUSY;
2424 0 ▶ }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){ 0 0 0
2425 0 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
2426 0 busy = 0;
2427 }
2428 0 break;
2429 }
2430 215 case SQLITE_OK: {
2431 215 busy = 0;
2432 215 break;
2433 }
2434 0 default: {
2435 0 goto abort_due_to_error;
2436 }
2437 }
2438 215 ▶ }while( busy );
Branch: 1 branch
BranchTrueFalseTotal
}while( busy ); 0 215 215
2439 215 break;
2440 }
2441  
2442 /* Opcode: OpenTemp P1 P2 *
2443 **
2444 ** Open a new cursor to a transient table.
2445 ** The transient cursor is always opened read/write even if
2446 ** the main database is read-only. The transient table is deleted
2447 ** automatically when the cursor is closed.
2448 **
2449 ** The cursor points to a BTree table if P2==0 and to a BTree index
2450 ** if P2==1. A BTree table must have an integer key and can have arbitrary
2451 ** data. A BTree index has no data but can have an arbitrary key.
2452 **
2453 ** This opcode is used for tables that exist for the duration of a single
2454 ** SQL statement only. Tables created using CREATE TEMPORARY TABLE
2455 ** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the
2456 ** context of this opcode means for the duration of a single SQL statement
2457 ** whereas "Temporary" in the context of CREATE TABLE means for the duration
2458 ** of the connection to the database. Same word; different meanings.
2459 */
2460 3 case OP_OpenTemp: {
2461 3 int i = pOp->p1;
2462 Cursor *pCx;
2463 assert( i>=0 );
2464 3 ▶ if( expandCursorArraySize(p, i) ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( expandCursorArraySize(p, i) ) goto no_mem; 0 3 3
2465 3 pCx = &p->aCsr[i];
2466 3 sqliteVdbeCleanupCursor(pCx);
2467 3 memset(pCx, 0, sizeof(*pCx));
2468 3 pCx->nullRow = 1;
2469 3 rc = sqliteBtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
2470  
2471 3 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 3 0 3
2472 3 rc = sqliteBtreeBeginTrans(pCx->pBt);
2473 }
2474 3 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 3 0 3
2475 3 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 0 3 3
2476 int pgno;
2477 0 rc = sqliteBtreeCreateIndex(pCx->pBt, &pgno);
2478 0 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 0 0 0
2479 0 rc = sqliteBtreeCursor(pCx->pBt, pgno, 1, &pCx->pCursor);
2480 }
2481 }else{
2482 3 rc = sqliteBtreeCursor(pCx->pBt, 2, 1, &pCx->pCursor);
2483 }
2484 }
2485 3 break;
2486 }
2487  
2488 /* Opcode: OpenPseudo P1 * *
2489 **
2490 ** Open a new cursor that points to a fake table that contains a single
2491 ** row of data. Any attempt to write a second row of data causes the
2492 ** first row to be deleted. All data is deleted when the cursor is
2493 ** closed.
2494 **
2495 ** A pseudo-table created by this opcode is useful for holding the
2496 ** NEW or OLD tables in a trigger.
2497 */
2498 0 case OP_OpenPseudo: {
2499 0 int i = pOp->p1;
2500 Cursor *pCx;
2501 assert( i>=0 );
2502 0 ▶ if( expandCursorArraySize(p, i) ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( expandCursorArraySize(p, i) ) goto no_mem; 0 0 0
2503 0 pCx = &p->aCsr[i];
2504 0 sqliteVdbeCleanupCursor(pCx);
2505 0 memset(pCx, 0, sizeof(*pCx));
2506 0 pCx->nullRow = 1;
2507 0 pCx->pseudoTable = 1;
2508 0 break;
2509 }
2510  
2511 /* Opcode: Close P1 * *
2512 **
2513 ** Close a cursor previously opened as P1. If P1 is not
2514 ** currently open, this instruction is a no-op.
2515 */
2516 198 case OP_Close: {
2517 198 int i = pOp->p1;
2518 198 ▶ if( i>=0 && i<p->nCursor ){
Branch: 2 branches
BranchTrueFalseTotal
if( i>=0 && i<p->nCursor ){ 198 0 198
if( i>=0 && i<p->nCursor ){ 198 0 198
2519 198 sqliteVdbeCleanupCursor(&p->aCsr[i]);
2520 }
2521 198 break;
2522 }
2523  
2524 /* Opcode: MoveTo P1 P2 *
2525 **
2526 ** Pop the top of the stack and use its value as a key. Reposition
2527 ** cursor P1 so that it points to an entry with a matching key. If
2528 ** the table contains no record with a matching key, then the cursor
2529 ** is left pointing at the first record that is greater than the key.
2530 ** If there are no records greater than the key and P2 is not zero,
2531 ** then an immediate jump to P2 is made.
2532 **
2533 ** See also: Found, NotFound, Distinct, MoveLt
2534 */
2535 /* Opcode: MoveLt P1 P2 *
2536 **
2537 ** Pop the top of the stack and use its value as a key. Reposition
2538 ** cursor P1 so that it points to the entry with the largest key that is
2539 ** less than the key popped from the stack.
2540 ** If there are no records less than than the key and P2
2541 ** is not zero then an immediate jump to P2 is made.
2542 **
2543 ** See also: MoveTo
2544 */
2545 0 case OP_MoveLt:
2546 case OP_MoveTo: {
2547 0 int i = pOp->p1;
2548 Cursor *pC;
2549  
2550 assert( pTos>=p->aStack );
2551 assert( i>=0 && i<p->nCursor );
2552 0 pC = &p->aCsr[i];
2553 0 ▶ if( pC->pCursor!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->pCursor!=0 ){ 0 0 0
2554 int res, oc;
2555 0 pC->nullRow = 0;
2556 0 ▶ if( pTos->flags & MEM_Int ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Int ){ 0 0 0
2557 0 int iKey = intToKey(pTos->i);
2558 0 ▶ if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){ 0 0 0
if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){ 0 0 0
2559 0 pC->movetoTarget = iKey;
2560 0 pC->deferredMoveto = 1;
2561 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
2562 0 pTos--;
2563 0 break;
2564 }
2565 0 sqliteBtreeMoveto(pC->pCursor, (char*)&iKey, sizeof(int), &res);
2566 0 pC->lastRecno = pTos->i;
2567 0 pC->recnoIsValid = res==0;
2568 }else{
2569 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
2570 0 sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2571 0 pC->recnoIsValid = 0;
2572 }
2573 0 pC->deferredMoveto = 0;
2574 0 sqlite_search_count++;
2575 0 oc = pOp->opcode;
2576 0 ▶ if( oc==OP_MoveTo && res<0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( oc==OP_MoveTo && res<0 ){ 0 0 0
if( oc==OP_MoveTo && res<0 ){ 0 0 0
2577 0 sqliteBtreeNext(pC->pCursor, &res);
2578 0 pC->recnoIsValid = 0;
2579 0 ▶ if( res && pOp->p2>0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( res && pOp->p2>0 ){ 0 0 0
if( res && pOp->p2>0 ){ 0 0 0
2580 0 pc = pOp->p2 - 1;
2581 }
2582 0 ▶ }else if( oc==OP_MoveLt ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( oc==OP_MoveLt ){ 0 0 0
2583 0 ▶ if( res>=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res>=0 ){ 0 0 0
2584 0 sqliteBtreePrevious(pC->pCursor, &res);
2585 0 pC->recnoIsValid = 0;
2586 }else{
2587 /* res might be negative because the table is empty. Check to
2588 ** see if this is the case.
2589 */
2590 int keysize;
2591 0 ▶ res = sqliteBtreeKeySize(pC->pCursor,&keysize)!=0 || keysize==0;
Branch: 2 branches
BranchTrueFalseTotal
res = sqliteBtreeKeySize(pC->pCursor,&keysize)!=0 || keysize==0; 0 0 0
res = sqliteBtreeKeySize(pC->pCursor,&keysize)!=0 || keysize==0; 0 0 0
2592 }
2593 0 ▶ if( res && pOp->p2>0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( res && pOp->p2>0 ){ 0 0 0
if( res && pOp->p2>0 ){ 0 0 0
2594 0 pc = pOp->p2 - 1;
2595 }
2596 }
2597 }
2598 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
2599 0 pTos--;
2600 0 break;
2601 }
2602  
2603 /* Opcode: Distinct P1 P2 *
2604 **
2605 ** Use the top of the stack as a string key. If a record with that key does
2606 ** not exist in the table of cursor P1, then jump to P2. If the record
2607 ** does already exist, then fall thru. The cursor is left pointing
2608 ** at the record if it exists. The key is not popped from the stack.
2609 **
2610 ** This operation is similar to NotFound except that this operation
2611 ** does not pop the key from the stack.
2612 **
2613 ** See also: Found, NotFound, MoveTo, IsUnique, NotExists
2614 */
2615 /* Opcode: Found P1 P2 *
2616 **
2617 ** Use the top of the stack as a string key. If a record with that key
2618 ** does exist in table of P1, then jump to P2. If the record
2619 ** does not exist, then fall thru. The cursor is left pointing
2620 ** to the record if it exists. The key is popped from the stack.
2621 **
2622 ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
2623 */
2624 /* Opcode: NotFound P1 P2 *
2625 **
2626 ** Use the top of the stack as a string key. If a record with that key
2627 ** does not exist in table of P1, then jump to P2. If the record
2628 ** does exist, then fall thru. The cursor is left pointing to the
2629 ** record if it exists. The key is popped from the stack.
2630 **
2631 ** The difference between this operation and Distinct is that
2632 ** Distinct does not pop the key from the stack.
2633 **
2634 ** See also: Distinct, Found, MoveTo, NotExists, IsUnique
2635 */
2636 0 case OP_Distinct:
2637 case OP_NotFound:
2638 case OP_Found: {
2639 0 int i = pOp->p1;
2640 0 int alreadyExists = 0;
2641 Cursor *pC;
2642 assert( pTos>=p->aStack );
2643 assert( i>=0 && i<p->nCursor );
2644 0 ▶ if( (pC = &p->aCsr[i])->pCursor!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pC = &p->aCsr[i])->pCursor!=0 ){ 0 0 0
2645 int res, rx;
2646 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
2647 0 rx = sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2648 0 ▶ alreadyExists = rx==SQLITE_OK && res==0;
Branch: 2 branches
BranchTrueFalseTotal
alreadyExists = rx==SQLITE_OK && res==0; 0 0 0
alreadyExists = rx==SQLITE_OK && res==0; 0 0 0
2649 0 pC->deferredMoveto = 0;
2650 }
2651 0 ▶ if( pOp->opcode==OP_Found ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_Found ){ 0 0 0
2652 0 ▶ if( alreadyExists ) pc = pOp->p2 - 1;
Branch: 1 branch
BranchTrueFalseTotal
if( alreadyExists ) pc = pOp->p2 - 1; 0 0 0
2653 }else{
2654 0 ▶ if( !alreadyExists ) pc = pOp->p2 - 1;
Branch: 1 branch
BranchTrueFalseTotal
if( !alreadyExists ) pc = pOp->p2 - 1; 0 0 0
2655 }
2656 0 ▶ if( pOp->opcode!=OP_Distinct ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode!=OP_Distinct ){ 0 0 0
2657 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
2658 0 pTos--;
2659 }
2660 0 break;
2661 }
2662  
2663 /* Opcode: IsUnique P1 P2 *
2664 **
2665 ** The top of the stack is an integer record number. Call this
2666 ** record number R. The next on the stack is an index key created
2667 ** using MakeIdxKey. Call it K. This instruction pops R from the
2668 ** stack but it leaves K unchanged.
2669 **
2670 ** P1 is an index. So all but the last four bytes of K are an
2671 ** index string. The last four bytes of K are a record number.
2672 **
2673 ** This instruction asks if there is an entry in P1 where the
2674 ** index string matches K but the record number is different
2675 ** from R. If there is no such entry, then there is an immediate
2676 ** jump to P2. If any entry does exist where the index string
2677 ** matches K but the record number is not R, then the record
2678 ** number for that entry is pushed onto the stack and control
2679 ** falls through to the next instruction.
2680 **
2681 ** See also: Distinct, NotFound, NotExists, Found
2682 */
2683 2 case OP_IsUnique: {
2684 2 int i = pOp->p1;
2685 2 Mem *pNos = &pTos[-1];
2686 BtCursor *pCrsr;
2687 int R;
2688  
2689 /* Pop the value R off the top of the stack
2690 */
2691 assert( pNos>=p->aStack );
2692 2 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 2 2
2693 2 R = pTos->i;
2694 2 pTos--;
2695 assert( i>=0 && i<=p->nCursor );
2696 2 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 2 0 2
2697 int res, rc;
2698 int v; /* The record number on the P1 entry that matches K */
2699 char *zKey; /* The value of K */
2700 int nKey; /* Number of bytes in K */
2701  
2702 /* Make sure K is a string and make zKey point to K
2703 */
2704 2 ▶ Stringify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pNos); 0 2 2
2705 2 zKey = pNos->z;
2706 2 nKey = pNos->n;
2707 assert( nKey >= 4 );
2708  
2709 /* Search for an entry in P1 where all but the last four bytes match K.
2710 ** If there is no such entry, jump immediately to P2.
2711 */
2712 assert( p->aCsr[i].deferredMoveto==0 );
2713 2 rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
2714 2 ▶ if( rc!=SQLITE_OK ) goto abort_due_to_error;
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ) goto abort_due_to_error; 0 2 2
2715 2 ▶ if( res<0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res<0 ){ 1 1 2
2716 1 rc = sqliteBtreeNext(pCrsr, &res);
2717 1 ▶ if( res ){
Branch: 1 branch
BranchTrueFalseTotal
if( res ){ 1 0 1
2718 1 pc = pOp->p2 - 1;
2719 1 break;
2720 }
2721 }
2722 1 rc = sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &res);
2723 1 ▶ if( rc!=SQLITE_OK ) goto abort_due_to_error;
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ) goto abort_due_to_error; 0 1 1
2724 1 ▶ if( res>0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res>0 ){ 0 1 1
2725 0 pc = pOp->p2 - 1;
2726 0 break;
2727 }
2728  
2729 /* At this point, pCrsr is pointing to an entry in P1 where all but
2730 ** the last for bytes of the key match K. Check to see if the last
2731 ** four bytes of the key are different from R. If the last four
2732 ** bytes equal R then jump immediately to P2.
2733 */
2734 1 sqliteBtreeKey(pCrsr, nKey - 4, 4, (char*)&v);
2735 1 v = keyToInt(v);
2736 1 ▶ if( v==R ){
Branch: 1 branch
BranchTrueFalseTotal
if( v==R ){ 0 1 1
2737 0 pc = pOp->p2 - 1;
2738 0 break;
2739 }
2740  
2741 /* The last four bytes of the key are different from R. Convert the
2742 ** last four bytes of the key into an integer and push it onto the
2743 ** stack. (These bytes are the record number of an entry that
2744 ** violates a UNIQUE constraint.)
2745 */
2746 1 pTos++;
2747 1 pTos->i = v;
2748 1 pTos->flags = MEM_Int;
2749 }
2750 1 break;
2751 }
2752  
2753 /* Opcode: NotExists P1 P2 *
2754 **
2755 ** Use the top of the stack as a integer key. If a record with that key
2756 ** does not exist in table of P1, then jump to P2. If the record
2757 ** does exist, then fall thru. The cursor is left pointing to the
2758 ** record if it exists. The integer key is popped from the stack.
2759 **
2760 ** The difference between this operation and NotFound is that this
2761 ** operation assumes the key is an integer and NotFound assumes it
2762 ** is a string.
2763 **
2764 ** See also: Distinct, Found, MoveTo, NotFound, IsUnique
2765 */
2766 6 case OP_NotExists: {
2767 6 int i = pOp->p1;
2768 BtCursor *pCrsr;
2769 assert( pTos>=p->aStack );
2770 assert( i>=0 && i<p->nCursor );
2771 6 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 6 0 6
2772 int res, rx, iKey;
2773 assert( pTos->flags & MEM_Int );
2774 6 iKey = intToKey(pTos->i);
2775 6 rx = sqliteBtreeMoveto(pCrsr, (char*)&iKey, sizeof(int), &res);
2776 6 p->aCsr[i].lastRecno = pTos->i;
2777 6 p->aCsr[i].recnoIsValid = res==0;
2778 6 p->aCsr[i].nullRow = 0;
2779 6 ▶ if( rx!=SQLITE_OK || res!=0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( rx!=SQLITE_OK || res!=0 ){ 6 0 6
if( rx!=SQLITE_OK || res!=0 ){ 0 6 6
2780 0 pc = pOp->p2 - 1;
2781 0 p->aCsr[i].recnoIsValid = 0;
2782 }
2783 }
2784 6 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 6 6
2785 6 pTos--;
2786 6 break;
2787 }
2788  
2789 /* Opcode: NewRecno P1 * *
2790 **
2791 ** Get a new integer record number used as the key to a table.
2792 ** The record number is not previously used as a key in the database
2793 ** table that cursor P1 points to. The new record number is pushed
2794 ** onto the stack.
2795 */
2796 97 case OP_NewRecno: {
2797 97 int i = pOp->p1;
2798 97 int v = 0;
2799 Cursor *pC;
2800 assert( i>=0 && i<p->nCursor );
2801 97 ▶ if( (pC = &p->aCsr[i])->pCursor==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pC = &p->aCsr[i])->pCursor==0 ){ 0 97 97
2802 0 v = 0;
2803 }else{
2804 /* The next rowid or record number (different terms for the same
2805 ** thing) is obtained in a two-step algorithm.
2806 **
2807 ** First we attempt to find the largest existing rowid and add one
2808 ** to that. But if the largest existing rowid is already the maximum
2809 ** positive integer, we have to fall through to the second
2810 ** probabilistic algorithm
2811 **
2812 ** The second algorithm is to select a rowid at random and see if
2813 ** it already exists in the table. If it does not exist, we have
2814 ** succeeded. If the random rowid does exist, we select a new one
2815 ** and try again, up to 1000 times.
2816 **
2817 ** For a table with less than 2 billion entries, the probability
2818 ** of not finding a unused rowid is about 1.0e-300. This is a
2819 ** non-zero probability, but it is still vanishingly small and should
2820 ** never cause a problem. You are much, much more likely to have a
2821 ** hardware failure than for this algorithm to fail.
2822 **
2823 ** The analysis in the previous paragraph assumes that you have a good
2824 ** source of random numbers. Is a library function like lrand48()
2825 ** good enough? Maybe. Maybe not. It's hard to know whether there
2826 ** might be subtle bugs is some implementations of lrand48() that
2827 ** could cause problems. To avoid uncertainty, SQLite uses its own
2828 ** random number generator based on the RC4 algorithm.
2829 **
2830 ** To promote locality of reference for repetitive inserts, the
2831 ** first few attempts at chosing a random rowid pick values just a little
2832 ** larger than the previous rowid. This has been shown experimentally
2833 ** to double the speed of the COPY operation.
2834 */
2835 int res, rx, cnt, x;
2836 97 cnt = 0;
2837 97 ▶ if( !pC->useRandomRowid ){
Branch: 1 branch
BranchTrueFalseTotal
if( !pC->useRandomRowid ){ 97 0 97
2838 97 ▶ if( pC->nextRowidValid ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->nextRowidValid ){ 27 70 97
2839 27 v = pC->nextRowid;
2840 }else{
2841 70 rx = sqliteBtreeLast(pC->pCursor, &res);
2842 70 ▶ if( res ){
Branch: 1 branch
BranchTrueFalseTotal
if( res ){ 35 35 70
2843 35 v = 1;
2844 }else{
2845 35 sqliteBtreeKey(pC->pCursor, 0, sizeof(v), (void*)&v);
2846 35 v = keyToInt(v);
2847 35 ▶ if( v==0x7fffffff ){
Branch: 1 branch
BranchTrueFalseTotal
if( v==0x7fffffff ){ 0 35 35
2848 0 pC->useRandomRowid = 1;
2849 }else{
2850 35 v++;
2851 }
2852 }
2853 }
2854 97 ▶ if( v<0x7fffffff ){
Branch: 1 branch
BranchTrueFalseTotal
if( v<0x7fffffff ){ 97 0 97
2855 97 pC->nextRowidValid = 1;
2856 97 pC->nextRowid = v+1;
2857 }else{
2858 0 pC->nextRowidValid = 0;
2859 }
2860 }
2861 97 ▶ if( pC->useRandomRowid ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->useRandomRowid ){ 0 97 97
2862 0 v = db->priorNewRowid;
2863 0 cnt = 0;
2864 do{
2865 0 ▶ if( v==0 || cnt>2 ){
Branch: 2 branches
BranchTrueFalseTotal
if( v==0 || cnt>2 ){ 0 0 0
if( v==0 || cnt>2 ){ 0 0 0
2866 0 sqliteRandomness(sizeof(v), &v);
2867 0 ▶ if( cnt<5 ) v &= 0xffffff;
Branch: 1 branch
BranchTrueFalseTotal
if( cnt<5 ) v &= 0xffffff; 0 0 0
2868 }else{
2869 unsigned char r;
2870 0 sqliteRandomness(1, &r);
2871 0 v += r + 1;
2872 }
2873 0 ▶ if( v==0 ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( v==0 ) continue; 0 0 0
2874 0 x = intToKey(v);
2875 0 rx = sqliteBtreeMoveto(pC->pCursor, &x, sizeof(int), &res);
2876 0 cnt++;
2877 0 ▶ }while( cnt<1000 && rx==SQLITE_OK && res==0 );
Branch: 3 branches
BranchTrueFalseTotal
}while( cnt<1000 && rx==SQLITE_OK && res==0 ); 0 0 0
}while( cnt<1000 && rx==SQLITE_OK && res==0 ); 0 0 0
}while( cnt<1000 && rx==SQLITE_OK && res==0 ); 0 0 0
2878 0 db->priorNewRowid = v;
2879 0 ▶ if( rx==SQLITE_OK && res==0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( rx==SQLITE_OK && res==0 ){ 0 0 0
if( rx==SQLITE_OK && res==0 ){ 0 0 0
2880 0 rc = SQLITE_FULL;
2881 0 goto abort_due_to_error;
2882 }
2883 }
2884 97 pC->recnoIsValid = 0;
2885 97 pC->deferredMoveto = 0;
2886 }
2887 97 pTos++;
2888 97 pTos->i = v;
2889 97 pTos->flags = MEM_Int;
2890 97 break;
2891 }
2892  
2893 /* Opcode: PutIntKey P1 P2 *
2894 **
2895 ** Write an entry into the table of cursor P1. A new entry is
2896 ** created if it doesn't already exist or the data for an existing
2897 ** entry is overwritten. The data is the value on the top of the
2898 ** stack. The key is the next value down on the stack. The key must
2899 ** be an integer. The stack is popped twice by this instruction.
2900 **
2901 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
2902 ** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set,
2903 ** then the current statement change count is incremented (otherwise not).
2904 ** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
2905 ** stored for subsequent return by the sqlite_last_insert_rowid() function
2906 ** (otherwise it's unmodified).
2907 */
2908 /* Opcode: PutStrKey P1 * *
2909 **
2910 ** Write an entry into the table of cursor P1. A new entry is
2911 ** created if it doesn't already exist or the data for an existing
2912 ** entry is overwritten. The data is the value on the top of the
2913 ** stack. The key is the next value down on the stack. The key must
2914 ** be a string. The stack is popped twice by this instruction.
2915 **
2916 ** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
2917 */
2918 118 case OP_PutIntKey:
2919 case OP_PutStrKey: {
2920 118 Mem *pNos = &pTos[-1];
2921 118 int i = pOp->p1;
2922 Cursor *pC;
2923 assert( pNos>=p->aStack );
2924 assert( i>=0 && i<p->nCursor );
2925 118 ▶ if( ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){
Branch: 2 branches
BranchTrueFalseTotal
if( ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){ 0 118 118
if( ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){ 0 0 0
2926 char *zKey;
2927 int nKey, iKey;
2928 118 ▶ if( pOp->opcode==OP_PutStrKey ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_PutStrKey ){ 0 118 118
2929 0 ▶ Stringify(pNos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pNos); 0 0 0
2930 0 nKey = pNos->n;
2931 0 zKey = pNos->z;
2932 }else{
2933 assert( pNos->flags & MEM_Int );
2934 118 nKey = sizeof(int);
2935 118 iKey = intToKey(pNos->i);
2936 118 zKey = (char*)&iKey;
2937 118 ▶ if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; 43 75 118
2938 118 ▶ if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i; 43 75 118
2939 118 ▶ if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; 43 75 118
2940 118 ▶ if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
Branch: 2 branches
BranchTrueFalseTotal
if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){ 118 0 118
if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){ 1 117 118
2941 1 pC->nextRowidValid = 0;
2942 }
2943 }
2944 118 ▶ if( pTos->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Null ){ 22 96 118
2945 22 pTos->z = 0;
2946 22 pTos->n = 0;
2947 }else{
2948 assert( pTos->flags & MEM_Str );
2949 }
2950 118 ▶ if( pC->pseudoTable ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->pseudoTable ){ 0 118 118
2951 /* PutStrKey does not work for pseudo-tables.
2952 ** The following assert makes sure we are not trying to use
2953 ** PutStrKey on a pseudo-table
2954 */
2955 assert( pOp->opcode==OP_PutIntKey );
2956 0 sqliteFree(pC->pData);
2957 0 pC->iKey = iKey;
2958 0 pC->nData = pTos->n;
2959 0 ▶ if( pTos->flags & MEM_Dyn ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Dyn ){ 0 0 0
2960 0 pC->pData = pTos->z;
2961 0 pTos->flags = MEM_Null;
2962 }else{
2963 0 pC->pData = sqliteMallocRaw( pC->nData );
2964 0 ▶ if( pC->pData ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->pData ){ 0 0 0
2965 0 memcpy(pC->pData, pTos->z, pC->nData);
2966 }
2967 }
2968 0 pC->nullRow = 0;
2969 }else{
2970 118 rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
2971 }
2972 118 pC->recnoIsValid = 0;
2973 118 pC->deferredMoveto = 0;
2974 }
2975 118 popStack(&pTos, 2);
2976 118 break;
2977 }
2978  
2979 /* Opcode: Delete P1 P2 *
2980 **
2981 ** Delete the record at which the P1 cursor is currently pointing.
2982 **
2983 ** The cursor will be left pointing at either the next or the previous
2984 ** record in the table. If it is left pointing at the next record, then
2985 ** the next Next instruction will be a no-op. Hence it is OK to delete
2986 ** a record from within an Next loop.
2987 **
2988 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
2989 ** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set,
2990 ** then the current statement change count is incremented (otherwise not).
2991 **
2992 ** If P1 is a pseudo-table, then this instruction is a no-op.
2993 */
2994 17 case OP_Delete: {
2995 17 int i = pOp->p1;
2996 Cursor *pC;
2997 assert( i>=0 && i<p->nCursor );
2998 17 pC = &p->aCsr[i];
2999 17 ▶ if( pC->pCursor!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->pCursor!=0 ){ 17 0 17
3000 17 sqliteVdbeCursorMoveto(pC);
3001 17 rc = sqliteBtreeDelete(pC->pCursor);
3002 17 pC->nextRowidValid = 0;
3003 }
3004 17 ▶ if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; 6 11 17
3005 17 ▶ if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; 6 11 17
3006 17 break;
3007 }
3008  
3009 /* Opcode: SetCounts * * *
3010 **
3011 ** Called at end of statement. Updates lsChange (last statement change count)
3012 ** and resets csChange (current statement change count) to 0.
3013 */
3014 49 case OP_SetCounts: {
3015 49 db->lsChange=db->csChange;
3016 49 db->csChange=0;
3017 49 break;
3018 }
3019  
3020 /* Opcode: KeyAsData P1 P2 *
3021 **
3022 ** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
3023 ** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls
3024 ** data off of the key rather than the data. This is used for
3025 ** processing compound selects.
3026 */
3027 0 case OP_KeyAsData: {
3028 0 int i = pOp->p1;
3029 assert( i>=0 && i<p->nCursor );
3030 0 p->aCsr[i].keyAsData = pOp->p2;
3031 0 break;
3032 }
3033  
3034 /* Opcode: RowData P1 * *
3035 **
3036 ** Push onto the stack the complete row data for cursor P1.
3037 ** There is no interpretation of the data. It is just copied
3038 ** onto the stack exactly as it is found in the database file.
3039 **
3040 ** If the cursor is not pointing to a valid row, a NULL is pushed
3041 ** onto the stack.
3042 */
3043 /* Opcode: RowKey P1 * *
3044 **
3045 ** Push onto the stack the complete row key for cursor P1.
3046 ** There is no interpretation of the key. It is just copied
3047 ** onto the stack exactly as it is found in the database file.
3048 **
3049 ** If the cursor is not pointing to a valid row, a NULL is pushed
3050 ** onto the stack.
3051 */
3052 0 case OP_RowKey:
3053 case OP_RowData: {
3054 0 int i = pOp->p1;
3055 Cursor *pC;
3056 int n;
3057  
3058 0 pTos++;
3059 assert( i>=0 && i<p->nCursor );
3060 0 pC = &p->aCsr[i];
3061 0 ▶ if( pC->nullRow ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->nullRow ){ 0 0 0
3062 0 pTos->flags = MEM_Null;
3063 0 ▶ }else if( pC->pCursor!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->pCursor!=0 ){ 0 0 0
3064 0 BtCursor *pCrsr = pC->pCursor;
3065 0 sqliteVdbeCursorMoveto(pC);
3066 0 ▶ if( pC->nullRow ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->nullRow ){ 0 0 0
3067 0 pTos->flags = MEM_Null;
3068 0 break;
3069 0 ▶ }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( pC->keyAsData || pOp->opcode==OP_RowKey ){ 0 0 0
}else if( pC->keyAsData || pOp->opcode==OP_RowKey ){ 0 0 0
3070 0 sqliteBtreeKeySize(pCrsr, &n);
3071 }else{
3072 0 sqliteBtreeDataSize(pCrsr, &n);
3073 }
3074 0 pTos->n = n;
3075 0 ▶ if( n<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( n<=NBFS ){ 0 0 0
3076 0 pTos->flags = MEM_Str | MEM_Short;
3077 0 pTos->z = pTos->zShort;
3078 }else{
3079 0 char *z = sqliteMallocRaw( n );
3080 0 ▶ if( z==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) goto no_mem; 0 0 0
3081 0 pTos->flags = MEM_Str | MEM_Dyn;
3082 0 pTos->z = z;
3083 }
3084 0 ▶ if( pC->keyAsData || pOp->opcode==OP_RowKey ){
Branch: 2 branches
BranchTrueFalseTotal
if( pC->keyAsData || pOp->opcode==OP_RowKey ){ 0 0 0
if( pC->keyAsData || pOp->opcode==OP_RowKey ){ 0 0 0
3085 0 sqliteBtreeKey(pCrsr, 0, n, pTos->z);
3086 }else{
3087 0 sqliteBtreeData(pCrsr, 0, n, pTos->z);
3088 }
3089 0 ▶ }else if( pC->pseudoTable ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->pseudoTable ){ 0 0 0
3090 0 pTos->n = pC->nData;
3091 0 pTos->z = pC->pData;
3092 0 pTos->flags = MEM_Str|MEM_Ephem;
3093 }else{
3094 0 pTos->flags = MEM_Null;
3095 }
3096 0 break;
3097 }
3098  
3099 /* Opcode: Column P1 P2 *
3100 **
3101 ** Interpret the data that cursor P1 points to as
3102 ** a structure built using the MakeRecord instruction.
3103 ** (See the MakeRecord opcode for additional information about
3104 ** the format of the data.)
3105 ** Push onto the stack the value of the P2-th column contained
3106 ** in the data.
3107 **
3108 ** If the KeyAsData opcode has previously executed on this cursor,
3109 ** then the field might be extracted from the key rather than the
3110 ** data.
3111 **
3112 ** If P1 is negative, then the record is stored on the stack rather
3113 ** than in a table. For P1==-1, the top of the stack is used.
3114 ** For P1==-2, the next on the stack is used. And so forth. The
3115 ** value pushed is always just a pointer into the record which is
3116 ** stored further down on the stack. The column value is not copied.
3117 */
3118 386 case OP_Column: {
3119 int amt, offset, end, payloadSize;
3120 386 int i = pOp->p1;
3121 386 int p2 = pOp->p2;
3122 Cursor *pC;
3123 char *zRec;
3124 BtCursor *pCrsr;
3125 int idxWidth;
3126 unsigned char aHdr[10];
3127  
3128 assert( i<p->nCursor );
3129 386 pTos++;
3130 386 ▶ if( i<0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( i<0 ){ 0 386 386
3131 assert( &pTos[i]>=p->aStack );
3132 assert( pTos[i].flags & MEM_Str );
3133 0 zRec = pTos[i].z;
3134 0 payloadSize = pTos[i].n;
3135 386 ▶ }else if( (pC = &p->aCsr[i])->pCursor!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( (pC = &p->aCsr[i])->pCursor!=0 ){ 386 0 386
3136 386 sqliteVdbeCursorMoveto(pC);
3137 386 zRec = 0;
3138 386 pCrsr = pC->pCursor;
3139 386 ▶ if( pC->nullRow ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->nullRow ){ 0 386 386
3140 0 payloadSize = 0;
3141 386 ▶ }else if( pC->keyAsData ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->keyAsData ){ 0 386 386
3142 0 sqliteBtreeKeySize(pCrsr, &payloadSize);
3143 }else{
3144 386 sqliteBtreeDataSize(pCrsr, &payloadSize);
3145 }
3146 0 ▶ }else if( pC->pseudoTable ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->pseudoTable ){ 0 0 0
3147 0 payloadSize = pC->nData;
3148 0 zRec = pC->pData;
3149 assert( payloadSize==0 || zRec!=0 );
3150 }else{
3151 0 payloadSize = 0;
3152 }
3153  
3154 /* Figure out how many bytes in the column data and where the column
3155 ** data begins.
3156 */
3157 386 ▶ if( payloadSize==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( payloadSize==0 ){ 0 386 386
3158 0 pTos->flags = MEM_Null;
3159 386 break;
3160 386 ▶ }else if( payloadSize<256 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( payloadSize<256 ){ 383 3 386
3161 383 idxWidth = 1;
3162 3 ▶ }else if( payloadSize<65536 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( payloadSize<65536 ){ 3 0 3
3163 3 idxWidth = 2;
3164 }else{
3165 0 idxWidth = 3;
3166 }
3167  
3168 /* Figure out where the requested column is stored and how big it is.
3169 */
3170 386 ▶ if( payloadSize < idxWidth*(p2+1) ){
Branch: 1 branch
BranchTrueFalseTotal
if( payloadSize < idxWidth*(p2+1) ){ 0 386 386
3171 0 rc = SQLITE_CORRUPT;
3172 0 goto abort_due_to_error;
3173 }
3174 386 ▶ if( zRec ){
Branch: 1 branch
BranchTrueFalseTotal
if( zRec ){ 0 386 386
3175 0 memcpy(aHdr, &zRec[idxWidth*p2], idxWidth*2);
3176 386 ▶ }else if( pC->keyAsData ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->keyAsData ){ 0 386 386
3177 0 sqliteBtreeKey(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
3178 }else{
3179 386 sqliteBtreeData(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
3180 }
3181 386 offset = aHdr[0];
3182 386 end = aHdr[idxWidth];
3183 386 ▶ if( idxWidth>1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>1 ){ 3 383 386
3184 3 offset |= aHdr[1]<<8;
3185 3 end |= aHdr[idxWidth+1]<<8;
3186 3 ▶ if( idxWidth>2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( idxWidth>2 ){ 0 3 3
3187 0 offset |= aHdr[2]<<16;
3188 0 end |= aHdr[idxWidth+2]<<16;
3189 }
3190 }
3191 386 amt = end - offset;
3192 386 ▶ if( amt<0 || offset<0 || end>payloadSize ){
Branch: 3 branches
BranchTrueFalseTotal
if( amt<0 || offset<0 || end>payloadSize ){ 386 0 386
if( amt<0 || offset<0 || end>payloadSize ){ 386 0 386
if( amt<0 || offset<0 || end>payloadSize ){ 0 386 386
3193 0 rc = SQLITE_CORRUPT;
3194 0 goto abort_due_to_error;
3195 }
3196  
3197 /* amt and offset now hold the offset to the start of data and the
3198 ** amount of data. Go get the data and put it on the stack.
3199 */
3200 386 pTos->n = amt;
3201 386 ▶ if( amt==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( amt==0 ){ 18 368 386
3202 18 pTos->flags = MEM_Null;
3203 368 ▶ }else if( zRec ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( zRec ){ 0 368 368
3204 0 pTos->flags = MEM_Str | MEM_Ephem;
3205 0 pTos->z = &zRec[offset];
3206 }else{
3207 368 ▶ if( amt<=NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( amt<=NBFS ){ 333 35 368
3208 333 pTos->flags = MEM_Str | MEM_Short;
3209 333 pTos->z = pTos->zShort;
3210 }else{
3211 35 char *z = sqliteMallocRaw( amt );
3212 35 ▶ if( z==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) goto no_mem; 0 35 35
3213 35 pTos->flags = MEM_Str | MEM_Dyn;
3214 35 pTos->z = z;
3215 }
3216 368 ▶ if( pC->keyAsData ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->keyAsData ){ 0 368 368
3217 0 sqliteBtreeKey(pCrsr, offset, amt, pTos->z);
3218 }else{
3219 368 sqliteBtreeData(pCrsr, offset, amt, pTos->z);
3220 }
3221 }
3222 386 break;
3223 }
3224  
3225 /* Opcode: Recno P1 * *
3226 **
3227 ** Push onto the stack an integer which is the first 4 bytes of the
3228 ** the key to the current entry in a sequential scan of the database
3229 ** file P1. The sequential scan should have been started using the
3230 ** Next opcode.
3231 */
3232 8 case OP_Recno: {
3233 8 int i = pOp->p1;
3234 Cursor *pC;
3235 int v;
3236  
3237 assert( i>=0 && i<p->nCursor );
3238 8 pC = &p->aCsr[i];
3239 8 sqliteVdbeCursorMoveto(pC);
3240 8 pTos++;
3241 8 ▶ if( pC->recnoIsValid ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->recnoIsValid ){ 0 8 8
3242 0 v = pC->lastRecno;
3243 8 ▶ }else if( pC->pseudoTable ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pC->pseudoTable ){ 0 8 8
3244 0 v = keyToInt(pC->iKey);
3245 8 ▶ }else if( pC->nullRow || pC->pCursor==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( pC->nullRow || pC->pCursor==0 ){ 8 0 8
}else if( pC->nullRow || pC->pCursor==0 ){ 0 8 8
3246 0 pTos->flags = MEM_Null;
3247 0 break;
3248 }else{
3249 assert( pC->pCursor!=0 );
3250 8 sqliteBtreeKey(pC->pCursor, 0, sizeof(u32), (char*)&v);
3251 8 v = keyToInt(v);
3252 }
3253 8 pTos->i = v;
3254 8 pTos->flags = MEM_Int;
3255 8 break;
3256 }
3257  
3258 /* Opcode: FullKey P1 * *
3259 **
3260 ** Extract the complete key from the record that cursor P1 is currently
3261 ** pointing to and push the key onto the stack as a string.
3262 **
3263 ** Compare this opcode to Recno. The Recno opcode extracts the first
3264 ** 4 bytes of the key and pushes those bytes onto the stack as an
3265 ** integer. This instruction pushes the entire key as a string.
3266 **
3267 ** This opcode may not be used on a pseudo-table.
3268 */
3269 0 case OP_FullKey: {
3270 0 int i = pOp->p1;
3271 BtCursor *pCrsr;
3272  
3273 assert( p->aCsr[i].keyAsData );
3274 assert( !p->aCsr[i].pseudoTable );
3275 assert( i>=0 && i<p->nCursor );
3276 0 pTos++;
3277 0 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 0 0 0
3278 int amt;
3279 char *z;
3280  
3281 0 sqliteVdbeCursorMoveto(&p->aCsr[i]);
3282 0 sqliteBtreeKeySize(pCrsr, &amt);
3283 0 ▶ if( amt<=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( amt<=0 ){ 0 0 0
3284 0 rc = SQLITE_CORRUPT;
3285 0 goto abort_due_to_error;
3286 }
3287 0 ▶ if( amt>NBFS ){
Branch: 1 branch
BranchTrueFalseTotal
if( amt>NBFS ){ 0 0 0
3288 0 z = sqliteMallocRaw( amt );
3289 0 ▶ if( z==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) goto no_mem; 0 0 0
3290 0 pTos->flags = MEM_Str | MEM_Dyn;
3291 }else{
3292 0 z = pTos->zShort;
3293 0 pTos->flags = MEM_Str | MEM_Short;
3294 }
3295 0 sqliteBtreeKey(pCrsr, 0, amt, z);
3296 0 pTos->z = z;
3297 0 pTos->n = amt;
3298 }
3299 0 break;
3300 }
3301  
3302 /* Opcode: NullRow P1 * *
3303 **
3304 ** Move the cursor P1 to a null row. Any OP_Column operations
3305 ** that occur while the cursor is on the null row will always push
3306 ** a NULL onto the stack.
3307 */
3308 0 case OP_NullRow: {
3309 0 int i = pOp->p1;
3310  
3311 assert( i>=0 && i<p->nCursor );
3312 0 p->aCsr[i].nullRow = 1;
3313 0 p->aCsr[i].recnoIsValid = 0;
3314 0 break;
3315 }
3316  
3317 /* Opcode: Last P1 P2 *
3318 **
3319 ** The next use of the Recno or Column or Next instruction for P1
3320 ** will refer to the last entry in the database table or index.
3321 ** If the table or index is empty and P2>0, then jump immediately to P2.
3322 ** If P2 is 0 or if the table or index is not empty, fall through
3323 ** to the following instruction.
3324 */
3325 0 case OP_Last: {
3326 0 int i = pOp->p1;
3327 Cursor *pC;
3328 BtCursor *pCrsr;
3329  
3330 assert( i>=0 && i<p->nCursor );
3331 0 pC = &p->aCsr[i];
3332 0 ▶ if( (pCrsr = pC->pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = pC->pCursor)!=0 ){ 0 0 0
3333 int res;
3334 0 rc = sqliteBtreeLast(pCrsr, &res);
3335 0 pC->nullRow = res;
3336 0 pC->deferredMoveto = 0;
3337 0 ▶ if( res && pOp->p2>0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( res && pOp->p2>0 ){ 0 0 0
if( res && pOp->p2>0 ){ 0 0 0
3338 0 pc = pOp->p2 - 1;
3339 }
3340 }else{
3341 0 pC->nullRow = 0;
3342 }
3343 0 break;
3344 }
3345  
3346 /* Opcode: Rewind P1 P2 *
3347 **
3348 ** The next use of the Recno or Column or Next instruction for P1
3349 ** will refer to the first entry in the database table or index.
3350 ** If the table or index is empty and P2>0, then jump immediately to P2.
3351 ** If P2 is 0 or if the table or index is not empty, fall through
3352 ** to the following instruction.
3353 */
3354 141 case OP_Rewind: {
3355 141 int i = pOp->p1;
3356 Cursor *pC;
3357 BtCursor *pCrsr;
3358  
3359 assert( i>=0 && i<p->nCursor );
3360 141 pC = &p->aCsr[i];
3361 141 ▶ if( (pCrsr = pC->pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = pC->pCursor)!=0 ){ 141 0 141
3362 int res;
3363 141 rc = sqliteBtreeFirst(pCrsr, &res);
3364 141 pC->atFirst = res==0;
3365 141 pC->nullRow = res;
3366 141 pC->deferredMoveto = 0;
3367 141 ▶ if( res && pOp->p2>0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( res && pOp->p2>0 ){ 72 69 141
if( res && pOp->p2>0 ){ 72 0 72
3368 72 pc = pOp->p2 - 1;
3369 }
3370 }else{
3371 0 pC->nullRow = 0;
3372 }
3373 141 break;
3374 }
3375  
3376 /* Opcode: Next P1 P2 *
3377 **
3378 ** Advance cursor P1 so that it points to the next key/data pair in its
3379 ** table or index. If there are no more key/value pairs then fall through
3380 ** to the following instruction. But if the cursor advance was successful,
3381 ** jump immediately to P2.
3382 **
3383 ** See also: Prev
3384 */
3385 /* Opcode: Prev P1 P2 *
3386 **
3387 ** Back up cursor P1 so that it points to the previous key/data pair in its
3388 ** table or index. If there is no previous key/value pairs then fall through
3389 ** to the following instruction. But if the cursor backup was successful,
3390 ** jump immediately to P2.
3391 */
3392 167 case OP_Prev:
3393 case OP_Next: {
3394 Cursor *pC;
3395 BtCursor *pCrsr;
3396  
3397 167 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 167 167
3398 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3399 167 pC = &p->aCsr[pOp->p1];
3400 167 ▶ if( (pCrsr = pC->pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = pC->pCursor)!=0 ){ 167 0 167
3401 int res;
3402 167 ▶ if( pC->nullRow ){
Branch: 1 branch
BranchTrueFalseTotal
if( pC->nullRow ){ 0 167 167
3403 0 res = 1;
3404 }else{
3405 assert( pC->deferredMoveto==0 );
3406 167 ▶ rc = pOp->opcode==OP_Next ? sqliteBtreeNext(pCrsr, &res) :
Branch: 1 branch
BranchTrueFalseTotal
rc = pOp->opcode==OP_Next ? sqliteBtreeNext(pCrsr, &res) : 167 0 167
3407 0 sqliteBtreePrevious(pCrsr, &res);
3408 167 pC->nullRow = res;
3409 }
3410 167 ▶ if( res==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res==0 ){ 102 65 167
3411 102 pc = pOp->p2 - 1;
3412 102 sqlite_search_count++;
3413 }
3414 }else{
3415 0 pC->nullRow = 1;
3416 }
3417 167 pC->recnoIsValid = 0;
3418 167 break;
3419 }
3420  
3421 /* Opcode: IdxPut P1 P2 P3
3422 **
3423 ** The top of the stack holds a SQL index key made using the
3424 ** MakeIdxKey instruction. This opcode writes that key into the
3425 ** index P1. Data for the entry is nil.
3426 **
3427 ** If P2==1, then the key must be unique. If the key is not unique,
3428 ** the program aborts with a SQLITE_CONSTRAINT error and the database
3429 ** is rolled back. If P3 is not null, then it becomes part of the
3430 ** error message returned with the SQLITE_CONSTRAINT.
3431 */
3432 3 case OP_IdxPut: {
3433 3 int i = pOp->p1;
3434 BtCursor *pCrsr;
3435 assert( pTos>=p->aStack );
3436 assert( i>=0 && i<p->nCursor );
3437 assert( pTos->flags & MEM_Str );
3438 3 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 3 0 3
3439 3 int nKey = pTos->n;
3440 3 const char *zKey = pTos->z;
3441 3 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 2 1 3
3442 int res, n;
3443 assert( nKey >= 4 );
3444 2 rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
3445 2 ▶ if( rc!=SQLITE_OK ) goto abort_due_to_error;
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ) goto abort_due_to_error; 0 2 2
3446 4 ▶ while( res!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
while( res!=0 ){ 4 0 4
3447 int c;
3448 4 sqliteBtreeKeySize(pCrsr, &n);
3449 4 ▶ if( n==nKey
Branch: 1 branch
BranchTrueFalseTotal
if( n==nKey 1 3 4
3450 1 ▶ && sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &c)==SQLITE_OK
Branch: 1 branch
BranchTrueFalseTotal
&& sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &c)==SQLITE_OK 1 0 1
3451 1 ▶ && c==0
Branch: 1 branch
BranchTrueFalseTotal
&& c==0 0 1 1
3452 ){
3453 0 rc = SQLITE_CONSTRAINT;
3454 0 ▶ if( pOp->p3 && pOp->p3[0] ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p3 && pOp->p3[0] ){ 0 0 0
if( pOp->p3 && pOp->p3[0] ){ 0 0 0
3455 0 sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
3456 }
3457 0 goto abort_due_to_error;
3458 }
3459 4 ▶ if( res<0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res<0 ){ 2 2 4
3460 2 sqliteBtreeNext(pCrsr, &res);
3461 2 res = +1;
3462 }else{
3463 2 break;
3464 }
3465 }
3466 }
3467 3 rc = sqliteBtreeInsert(pCrsr, zKey, nKey, "", 0);
3468 assert( p->aCsr[i].deferredMoveto==0 );
3469 }
3470 3 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 3 3
3471 3 pTos--;
3472 3 break;
3473 }
3474  
3475 /* Opcode: IdxDelete P1 * *
3476 **
3477 ** The top of the stack is an index key built using the MakeIdxKey opcode.
3478 ** This opcode removes that entry from the index.
3479 */
3480 0 case OP_IdxDelete: {
3481 0 int i = pOp->p1;
3482 BtCursor *pCrsr;
3483 assert( pTos>=p->aStack );
3484 assert( pTos->flags & MEM_Str );
3485 assert( i>=0 && i<p->nCursor );
3486 0 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 0 0 0
3487 int rx, res;
3488 0 rx = sqliteBtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
3489 0 ▶ if( rx==SQLITE_OK && res==0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( rx==SQLITE_OK && res==0 ){ 0 0 0
if( rx==SQLITE_OK && res==0 ){ 0 0 0
3490 0 rc = sqliteBtreeDelete(pCrsr);
3491 }
3492 assert( p->aCsr[i].deferredMoveto==0 );
3493 }
3494 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
3495 0 pTos--;
3496 0 break;
3497 }
3498  
3499 /* Opcode: IdxRecno P1 * *
3500 **
3501 ** Push onto the stack an integer which is the last 4 bytes of the
3502 ** the key to the current entry in index P1. These 4 bytes should
3503 ** be the record number of the table entry to which this index entry
3504 ** points.
3505 **
3506 ** See also: Recno, MakeIdxKey.
3507 */
3508 0 case OP_IdxRecno: {
3509 0 int i = pOp->p1;
3510 BtCursor *pCrsr;
3511  
3512 assert( i>=0 && i<p->nCursor );
3513 0 pTos++;
3514 0 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 0 0 0
3515 int v;
3516 int sz;
3517 assert( p->aCsr[i].deferredMoveto==0 );
3518 0 sqliteBtreeKeySize(pCrsr, &sz);
3519 0 ▶ if( sz<sizeof(u32) ){
Branch: 1 branch
BranchTrueFalseTotal
if( sz<sizeof(u32) ){ 0 0 0
3520 0 pTos->flags = MEM_Null;
3521 }else{
3522 0 sqliteBtreeKey(pCrsr, sz - sizeof(u32), sizeof(u32), (char*)&v);
3523 0 v = keyToInt(v);
3524 0 pTos->i = v;
3525 0 pTos->flags = MEM_Int;
3526 }
3527 }else{
3528 0 pTos->flags = MEM_Null;
3529 }
3530 0 break;
3531 }
3532  
3533 /* Opcode: IdxGT P1 P2 *
3534 **
3535 ** Compare the top of the stack against the key on the index entry that
3536 ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3537 ** index entry. If the index entry is greater than the top of the stack
3538 ** then jump to P2. Otherwise fall through to the next instruction.
3539 ** In either case, the stack is popped once.
3540 */
3541 /* Opcode: IdxGE P1 P2 *
3542 **
3543 ** Compare the top of the stack against the key on the index entry that
3544 ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3545 ** index entry. If the index entry is greater than or equal to
3546 ** the top of the stack
3547 ** then jump to P2. Otherwise fall through to the next instruction.
3548 ** In either case, the stack is popped once.
3549 */
3550 /* Opcode: IdxLT P1 P2 *
3551 **
3552 ** Compare the top of the stack against the key on the index entry that
3553 ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3554 ** index entry. If the index entry is less than the top of the stack
3555 ** then jump to P2. Otherwise fall through to the next instruction.
3556 ** In either case, the stack is popped once.
3557 */
3558 0 case OP_IdxLT:
3559 case OP_IdxGT:
3560 case OP_IdxGE: {
3561 0 int i= pOp->p1;
3562 BtCursor *pCrsr;
3563  
3564 assert( i>=0 && i<p->nCursor );
3565 assert( pTos>=p->aStack );
3566 0 ▶ if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pCrsr = p->aCsr[i].pCursor)!=0 ){ 0 0 0
3567 int res, rc;
3568
3569 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
3570 assert( p->aCsr[i].deferredMoveto==0 );
3571 0 rc = sqliteBtreeKeyCompare(pCrsr, pTos->z, pTos->n, 4, &res);
3572 0 ▶ if( rc!=SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ){ 0 0 0
3573 0 break;
3574 }
3575 0 ▶ if( pOp->opcode==OP_IdxLT ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_IdxLT ){ 0 0 0
3576 0 res = -res;
3577 0 ▶ }else if( pOp->opcode==OP_IdxGE ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pOp->opcode==OP_IdxGE ){ 0 0 0
3578 0 res++;
3579 }
3580 0 ▶ if( res>0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( res>0 ){ 0 0 0
3581 0 pc = pOp->p2 - 1 ;
3582 }
3583 }
3584 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
3585 0 pTos--;
3586 0 break;
3587 }
3588  
3589 /* Opcode: IdxIsNull P1 P2 *
3590 **
3591 ** The top of the stack contains an index entry such as might be generated
3592 ** by the MakeIdxKey opcode. This routine looks at the first P1 fields of
3593 ** that key. If any of the first P1 fields are NULL, then a jump is made
3594 ** to address P2. Otherwise we fall straight through.
3595 **
3596 ** The index entry is always popped from the stack.
3597 */
3598 0 case OP_IdxIsNull: {
3599 0 int i = pOp->p1;
3600 int k, n;
3601 const char *z;
3602  
3603 assert( pTos>=p->aStack );
3604 assert( pTos->flags & MEM_Str );
3605 0 z = pTos->z;
3606 0 n = pTos->n;
3607 0 ▶ for(k=0; k<n && i>0; i--){
Branch: 2 branches
BranchTrueFalseTotal
for(k=0; k<n && i>0; i--){ 0 0 0
for(k=0; k<n && i>0; i--){ 0 0 0
3608 0 ▶ if( z[k]=='a' ){
Branch: 1 branch
BranchTrueFalseTotal
if( z[k]=='a' ){ 0 0 0
3609 0 pc = pOp->p2-1;
3610 0 break;
3611 }
3612 0 ▶ while( k<n && z[k] ){ k++; }
Branch: 2 branches
BranchTrueFalseTotal
while( k<n && z[k] ){ k++; } 0 0 0
while( k<n && z[k] ){ k++; } 0 0 0
3613 0 k++;
3614 }
3615 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
3616 0 pTos--;
3617 0 break;
3618 }
3619  
3620 /* Opcode: Destroy P1 P2 *
3621 **
3622 ** Delete an entire database table or index whose root page in the database
3623 ** file is given by P1.
3624 **
3625 ** The table being destroyed is in the main database file if P2==0. If
3626 ** P2==1 then the table to be clear is in the auxiliary database file
3627 ** that is used to store tables create using CREATE TEMPORARY TABLE.
3628 **
3629 ** See also: Clear
3630 */
3631 11 case OP_Destroy: {
3632 11 rc = sqliteBtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
3633 11 break;
3634 }
3635  
3636 /* Opcode: Clear P1 P2 *
3637 **
3638 ** Delete all contents of the database table or index whose root page
3639 ** in the database file is given by P1. But, unlike Destroy, do not
3640 ** remove the table or index from the database file.
3641 **
3642 ** The table being clear is in the main database file if P2==0. If
3643 ** P2==1 then the table to be clear is in the auxiliary database file
3644 ** that is used to store tables create using CREATE TEMPORARY TABLE.
3645 **
3646 ** See also: Destroy
3647 */
3648 0 case OP_Clear: {
3649 0 rc = sqliteBtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
3650 0 break;
3651 }
3652  
3653 /* Opcode: CreateTable * P2 P3
3654 **
3655 ** Allocate a new table in the main database file if P2==0 or in the
3656 ** auxiliary database file if P2==1. Push the page number
3657 ** for the root page of the new table onto the stack.
3658 **
3659 ** The root page number is also written to a memory location that P3
3660 ** points to. This is the mechanism is used to write the root page
3661 ** number into the parser's internal data structures that describe the
3662 ** new table.
3663 **
3664 ** The difference between a table and an index is this: A table must
3665 ** have a 4-byte integer key and can have arbitrary data. An index
3666 ** has an arbitrary key but no data.
3667 **
3668 ** See also: CreateIndex
3669 */
3670 /* Opcode: CreateIndex * P2 P3
3671 **
3672 ** Allocate a new index in the main database file if P2==0 or in the
3673 ** auxiliary database file if P2==1. Push the page number of the
3674 ** root page of the new index onto the stack.
3675 **
3676 ** See documentation on OP_CreateTable for additional information.
3677 */
3678 27 case OP_CreateIndex:
3679 case OP_CreateTable: {
3680 int pgno;
3681 assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
3682 assert( pOp->p2>=0 && pOp->p2<db->nDb );
3683 assert( db->aDb[pOp->p2].pBt!=0 );
3684 27 ▶ if( pOp->opcode==OP_CreateTable ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_CreateTable ){ 22 5 27
3685 22 rc = sqliteBtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno);
3686 }else{
3687 5 rc = sqliteBtreeCreateIndex(db->aDb[pOp->p2].pBt, &pgno);
3688 }
3689 27 pTos++;
3690 27 ▶ if( rc==SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc==SQLITE_OK ){ 27 0 27
3691 27 pTos->i = pgno;
3692 27 pTos->flags = MEM_Int;
3693 27 *(u32*)pOp->p3 = pgno;
3694 27 pOp->p3 = 0;
3695 }else{
3696 0 pTos->flags = MEM_Null;
3697 }
3698 27 break;
3699 }
3700  
3701 /* Opcode: IntegrityCk P1 P2 *
3702 **
3703 ** Do an analysis of the currently open database. Push onto the
3704 ** stack the text of an error message describing any problems.
3705 ** If there are no errors, push a "ok" onto the stack.
3706 **
3707 ** P1 is the index of a set that contains the root page numbers
3708 ** for all tables and indices in the main database file. The set
3709 ** is cleared by this opcode. In other words, after this opcode
3710 ** has executed, the set will be empty.
3711 **
3712 ** If P2 is not zero, the check is done on the auxiliary database
3713 ** file, not the main database file.
3714 **
3715 ** This opcode is used for testing purposes only.
3716 */
3717 0 case OP_IntegrityCk: {
3718 int nRoot;
3719 int *aRoot;
3720 0 int iSet = pOp->p1;
3721 Set *pSet;
3722 int j;
3723 HashElem *i;
3724 char *z;
3725  
3726 assert( iSet>=0 && iSet<p->nSet );
3727 0 pTos++;
3728 0 pSet = &p->aSet[iSet];
3729 0 nRoot = sqliteHashCount(&pSet->hash);
3730 0 aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) );
3731 0 ▶ if( aRoot==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( aRoot==0 ) goto no_mem; 0 0 0
3732 0 ▶ for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){
Branch: 1 branch
BranchTrueFalseTotal
for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){ 0 0 0
3733 0 toInt((char*)sqliteHashKey(i), &aRoot[j]);
3734 }
3735 0 aRoot[j] = 0;
3736 0 sqliteHashClear(&pSet->hash);
3737 0 pSet->prev = 0;
3738 0 z = sqliteBtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
3739 0 ▶ if( z==0 || z[0]==0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( z==0 || z[0]==0 ){ 0 0 0
if( z==0 || z[0]==0 ){ 0 0 0
3740 0 ▶ if( z ) sqliteFree(z);
Branch: 1 branch
BranchTrueFalseTotal
if( z ) sqliteFree(z); 0 0 0
3741 0 pTos->z = "ok";
3742 0 pTos->n = 3;
3743 0 pTos->flags = MEM_Str | MEM_Static;
3744 }else{
3745 0 pTos->z = z;
3746 0 pTos->n = strlen(z) + 1;
3747 0 pTos->flags = MEM_Str | MEM_Dyn;
3748 }
3749 0 sqliteFree(aRoot);
3750 0 break;
3751 }
3752  
3753 /* Opcode: ListWrite * * *
3754 **
3755 ** Write the integer on the top of the stack
3756 ** into the temporary storage list.
3757 */
3758 6 case OP_ListWrite: {
3759 Keylist *pKeylist;
3760 assert( pTos>=p->aStack );
3761 6 pKeylist = p->pList;
3762 6 ▶ if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
Branch: 2 branches
BranchTrueFalseTotal
if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){ 2 4 6
if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){ 0 2 2
3763 4 pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
3764 4 ▶ if( pKeylist==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( pKeylist==0 ) goto no_mem; 0 4 4
3765 4 pKeylist->nKey = 1000;
3766 4 pKeylist->nRead = 0;
3767 4 pKeylist->nUsed = 0;
3768 4 pKeylist->pNext = p->pList;
3769 4 p->pList = pKeylist;
3770 }
3771 6 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 6 6
3772 6 pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
3773 6 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 6 6
3774 6 pTos--;
3775 6 break;
3776 }
3777  
3778 /* Opcode: ListRewind * * *
3779 **
3780 ** Rewind the temporary buffer back to the beginning.
3781 */
3782 6 case OP_ListRewind: {
3783 /* What this opcode codes, really, is reverse the order of the
3784 ** linked list of Keylist structures so that they are read out
3785 ** in the same order that they were read in. */
3786 Keylist *pRev, *pTop;
3787 6 pRev = 0;
3788 10 ▶ while( p->pList ){
Branch: 1 branch
BranchTrueFalseTotal
while( p->pList ){ 4 6 10
3789 4 pTop = p->pList;
3790 4 p->pList = pTop->pNext;
3791 4 pTop->pNext = pRev;
3792 4 pRev = pTop;
3793 }
3794 6 p->pList = pRev;
3795 6 break;
3796 }
3797  
3798 /* Opcode: ListRead * P2 *
3799 **
3800 ** Attempt to read an integer from the temporary storage buffer
3801 ** and push it onto the stack. If the storage buffer is empty,
3802 ** push nothing but instead jump to P2.
3803 */
3804 12 case OP_ListRead: {
3805 Keylist *pKeylist;
3806 12 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 12 12
3807 12 pKeylist = p->pList;
3808 12 ▶ if( pKeylist!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pKeylist!=0 ){ 6 6 12
3809 assert( pKeylist->nRead>=0 );
3810 assert( pKeylist->nRead<pKeylist->nUsed );
3811 assert( pKeylist->nRead<pKeylist->nKey );
3812 6 pTos++;
3813 6 pTos->i = pKeylist->aKey[pKeylist->nRead++];
3814 6 pTos->flags = MEM_Int;
3815 6 ▶ if( pKeylist->nRead>=pKeylist->nUsed ){
Branch: 1 branch
BranchTrueFalseTotal
if( pKeylist->nRead>=pKeylist->nUsed ){ 4 2 6
3816 4 p->pList = pKeylist->pNext;
3817 4 sqliteFree(pKeylist);
3818 }
3819 }else{
3820 6 pc = pOp->p2 - 1;
3821 }
3822 12 break;
3823 }
3824  
3825 /* Opcode: ListReset * * *
3826 **
3827 ** Reset the temporary storage buffer so that it holds nothing.
3828 */
3829 6 case OP_ListReset: {
3830 6 ▶ if( p->pList ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->pList ){ 0 6 6
3831 0 sqliteVdbeKeylistFree(p->pList);
3832 0 p->pList = 0;
3833 }
3834 6 break;
3835 }
3836  
3837 /* Opcode: ListPush * * *
3838 **
3839 ** Save the current Vdbe list such that it can be restored by a ListPop
3840 ** opcode. The list is empty after this is executed.
3841 */
3842 0 case OP_ListPush: {
3843 0 p->keylistStackDepth++;
3844 assert(p->keylistStackDepth > 0);
3845 0 p->keylistStack = sqliteRealloc(p->keylistStack,
3846 0 sizeof(Keylist *) * p->keylistStackDepth);
3847 0 ▶ if( p->keylistStack==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( p->keylistStack==0 ) goto no_mem; 0 0 0
3848 0 p->keylistStack[p->keylistStackDepth - 1] = p->pList;
3849 0 p->pList = 0;
3850 0 break;
3851 }
3852  
3853 /* Opcode: ListPop * * *
3854 **
3855 ** Restore the Vdbe list to the state it was in when ListPush was last
3856 ** executed.
3857 */
3858 0 case OP_ListPop: {
3859 assert(p->keylistStackDepth > 0);
3860 0 p->keylistStackDepth--;
3861 0 sqliteVdbeKeylistFree(p->pList);
3862 0 p->pList = p->keylistStack[p->keylistStackDepth];
3863 0 p->keylistStack[p->keylistStackDepth] = 0;
3864 0 ▶ if( p->keylistStackDepth == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->keylistStackDepth == 0 ){ 0 0 0
3865 0 sqliteFree(p->keylistStack);
3866 0 p->keylistStack = 0;
3867 }
3868 0 break;
3869 }
3870  
3871 /* Opcode: ContextPush * * *
3872 **
3873 ** Save the current Vdbe context such that it can be restored by a ContextPop
3874 ** opcode. The context stores the last insert row id, the last statement change
3875 ** count, and the current statement change count.
3876 */
3877 0 case OP_ContextPush: {
3878 0 p->contextStackDepth++;
3879 assert(p->contextStackDepth > 0);
3880 0 p->contextStack = sqliteRealloc(p->contextStack,
3881 0 sizeof(Context) * p->contextStackDepth);
3882 0 ▶ if( p->contextStack==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( p->contextStack==0 ) goto no_mem; 0 0 0
3883 0 p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
3884 0 p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
3885 0 p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
3886 0 break;
3887 }
3888  
3889 /* Opcode: ContextPop * * *
3890 **
3891 ** Restore the Vdbe context to the state it was in when contextPush was last
3892 ** executed. The context stores the last insert row id, the last statement
3893 ** change count, and the current statement change count.
3894 */
3895 0 case OP_ContextPop: {
3896 assert(p->contextStackDepth > 0);
3897 0 p->contextStackDepth--;
3898 0 p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
3899 0 p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
3900 0 p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
3901 0 ▶ if( p->contextStackDepth == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->contextStackDepth == 0 ){ 0 0 0
3902 0 sqliteFree(p->contextStack);
3903 0 p->contextStack = 0;
3904 }
3905 0 break;
3906 }
3907  
3908 /* Opcode: SortPut * * *
3909 **
3910 ** The TOS is the key and the NOS is the data. Pop both from the stack
3911 ** and put them on the sorter. The key and data should have been
3912 ** made using SortMakeKey and SortMakeRec, respectively.
3913 */
3914 15 case OP_SortPut: {
3915 15 Mem *pNos = &pTos[-1];
3916 Sorter *pSorter;
3917 assert( pNos>=p->aStack );
3918 15 ▶ if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem;
Branch: 4 branches
BranchTrueFalseTotal
if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem; 0 15 15
if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem; 0 0 0
if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem; 0 15 15
if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem; 0 0 0
3919 15 pSorter = sqliteMallocRaw( sizeof(Sorter) );
3920 15 ▶ if( pSorter==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( pSorter==0 ) goto no_mem; 0 15 15
3921 15 pSorter->pNext = p->pSort;
3922 15 p->pSort = pSorter;
3923 assert( pTos->flags & MEM_Dyn );
3924 15 pSorter->nKey = pTos->n;
3925 15 pSorter->zKey = pTos->z;
3926 assert( pNos->flags & MEM_Dyn );
3927 15 pSorter->nData = pNos->n;
3928 15 pSorter->pData = pNos->z;
3929 15 pTos -= 2;
3930 15 break;
3931 }
3932  
3933 /* Opcode: SortMakeRec P1 * *
3934 **
3935 ** The top P1 elements are the arguments to a callback. Form these
3936 ** elements into a single data entry that can be stored on a sorter
3937 ** using SortPut and later fed to a callback using SortCallback.
3938 */
3939 15 case OP_SortMakeRec: {
3940 char *z;
3941 char **azArg;
3942 int nByte;
3943 int nField;
3944 int i;
3945 Mem *pRec;
3946  
3947 15 nField = pOp->p1;
3948 15 pRec = &pTos[1-nField];
3949 assert( pRec>=p->aStack );
3950 15 nByte = 0;
3951 85 ▶ for(i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<nField; i++, pRec++){ 70 15 85
3952 70 ▶ if( (pRec->flags & MEM_Null)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( (pRec->flags & MEM_Null)==0 ){ 37 33 70
3953 37 ▶ Stringify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pRec); 0 37 37
3954 37 nByte += pRec->n;
3955 }
3956 }
3957 15 nByte += sizeof(char*)*(nField+1);
3958 15 azArg = sqliteMallocRaw( nByte );
3959 15 ▶ if( azArg==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( azArg==0 ) goto no_mem; 0 15 15
3960 15 z = (char*)&azArg[nField+1];
3961 85 ▶ for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){ 70 15 85
3962 70 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 33 37 70
3963 33 azArg[i] = 0;
3964 }else{
3965 37 azArg[i] = z;
3966 37 memcpy(z, pRec->z, pRec->n);
3967 37 z += pRec->n;
3968 }
3969 }
3970 15 popStack(&pTos, nField);
3971 15 pTos++;
3972 15 pTos->n = nByte;
3973 15 pTos->z = (char*)azArg;
3974 15 pTos->flags = MEM_Str | MEM_Dyn;
3975 15 break;
3976 }
3977  
3978 /* Opcode: SortMakeKey * * P3
3979 **
3980 ** Convert the top few entries of the stack into a sort key. The
3981 ** number of stack entries consumed is the number of characters in
3982 ** the string P3. One character from P3 is prepended to each entry.
3983 ** The first character of P3 is prepended to the element lowest in
3984 ** the stack and the last character of P3 is prepended to the top of
3985 ** the stack. All stack entries are separated by a \000 character
3986 ** in the result. The whole key is terminated by two \000 characters
3987 ** in a row.
3988 **
3989 ** "N" is substituted in place of the P3 character for NULL values.
3990 **
3991 ** See also the MakeKey and MakeIdxKey opcodes.
3992 */
3993 15 case OP_SortMakeKey: {
3994 char *zNewKey;
3995 int nByte;
3996 int nField;
3997 int i, j, k;
3998 Mem *pRec;
3999  
4000 15 nField = strlen(pOp->p3);
4001 15 pRec = &pTos[1-nField];
4002 15 nByte = 1;
4003 50 ▶ for(i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<nField; i++, pRec++){ 35 15 50
4004 35 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 10 25 35
4005 10 nByte += 2;
4006 }else{
4007 25 ▶ Stringify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pRec); 0 25 25
4008 25 nByte += pRec->n+2;
4009 }
4010 }
4011 15 zNewKey = sqliteMallocRaw( nByte );
4012 15 ▶ if( zNewKey==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( zNewKey==0 ) goto no_mem; 0 15 15
4013 15 j = 0;
4014 15 k = 0;
4015 50 ▶ for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){ 35 15 50
4016 35 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 10 25 35
4017 10 zNewKey[j++] = 'N';
4018 10 zNewKey[j++] = 0;
4019 10 k++;
4020 }else{
4021 25 zNewKey[j++] = pOp->p3[k++];
4022 25 memcpy(&zNewKey[j], pRec->z, pRec->n-1);
4023 25 j += pRec->n-1;
4024 25 zNewKey[j++] = 0;
4025 }
4026 }
4027 15 zNewKey[j] = 0;
4028 assert( j<nByte );
4029 15 popStack(&pTos, nField);
4030 15 pTos++;
4031 15 pTos->n = nByte;
4032 15 pTos->flags = MEM_Str|MEM_Dyn;
4033 15 pTos->z = zNewKey;
4034 15 break;
4035 }
4036  
4037 /* Opcode: Sort * * *
4038 **
4039 ** Sort all elements on the sorter. The algorithm is a
4040 ** mergesort.
4041 */
4042 5 case OP_Sort: {
4043 int i;
4044 Sorter *pElem;
4045 Sorter *apSorter[NSORT];
4046 155 ▶ for(i=0; i<NSORT; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<NSORT; i++){ 150 5 155
4047 150 apSorter[i] = 0;
4048 }
4049 25 ▶ while( p->pSort ){
Branch: 1 branch
BranchTrueFalseTotal
while( p->pSort ){ 15 5 20
4050 15 pElem = p->pSort;
4051 15 p->pSort = pElem->pNext;
4052 15 pElem->pNext = 0;
4053 25 ▶ for(i=0; i<NSORT-1; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<NSORT-1; i++){ 25 0 25
4054 25 ▶ if( apSorter[i]==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( apSorter[i]==0 ){ 15 10 25
4055 15 apSorter[i] = pElem;
4056 15 break;
4057 }else{
4058 10 pElem = Merge(apSorter[i], pElem);
4059 10 apSorter[i] = 0;
4060 }
4061 }
4062 15 ▶ if( i>=NSORT-1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( i>=NSORT-1 ){ 0 15 15
4063 0 apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem);
4064 }
4065 }
4066 5 pElem = 0;
4067 155 ▶ for(i=0; i<NSORT; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<NSORT; i++){ 150 5 155
4068 150 pElem = Merge(apSorter[i], pElem);
4069 }
4070 5 p->pSort = pElem;
4071 5 break;
4072 }
4073  
4074 /* Opcode: SortNext * P2 *
4075 **
4076 ** Push the data for the topmost element in the sorter onto the
4077 ** stack, then remove the element from the sorter. If the sorter
4078 ** is empty, push nothing on the stack and instead jump immediately
4079 ** to instruction P2.
4080 */
4081 20 case OP_SortNext: {
4082 20 Sorter *pSorter = p->pSort;
4083 20 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 20 20
4084 20 ▶ if( pSorter!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pSorter!=0 ){ 15 5 20
4085 15 p->pSort = pSorter->pNext;
4086 15 pTos++;
4087 15 pTos->z = pSorter->pData;
4088 15 pTos->n = pSorter->nData;
4089 15 pTos->flags = MEM_Str|MEM_Dyn;
4090 15 sqliteFree(pSorter->zKey);
4091 15 sqliteFree(pSorter);
4092 }else{
4093 5 pc = pOp->p2 - 1;
4094 }
4095 20 break;
4096 }
4097  
4098 /* Opcode: SortCallback P1 * *
4099 **
4100 ** The top of the stack contains a callback record built using
4101 ** the SortMakeRec operation with the same P1 value as this
4102 ** instruction. Pop this record from the stack and invoke the
4103 ** callback on it.
4104 */
4105 15 case OP_SortCallback: {
4106 assert( pTos>=p->aStack );
4107 assert( pTos->flags & MEM_Str );
4108 15 p->nCallback++;
4109 15 p->pc = pc+1;
4110 15 p->azResColumn = (char**)pTos->z;
4111 assert( p->nResColumn==pOp->p1 );
4112 15 p->popStack = 1;
4113 15 p->pTos = pTos;
4114 15 return SQLITE_ROW;
4115 }
4116  
4117 /* Opcode: SortReset * * *
4118 **
4119 ** Remove any elements that remain on the sorter.
4120 */
4121 5 case OP_SortReset: {
4122 5 sqliteVdbeSorterReset(p);
4123 5 break;
4124 }
4125  
4126 /* Opcode: FileOpen * * P3
4127 **
4128 ** Open the file named by P3 for reading using the FileRead opcode.
4129 ** If P3 is "stdin" then open standard input for reading.
4130 */
4131 0 case OP_FileOpen: {
4132 assert( pOp->p3!=0 );
4133 0 ▶ if( p->pFile ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->pFile ){ 0 0 0
4134 0 ▶ if( p->pFile!=stdin ) fclose(p->pFile);
Branch: 1 branch
BranchTrueFalseTotal
if( p->pFile!=stdin ) fclose(p->pFile); 0 0 0
4135 0 p->pFile = 0;
4136 }
4137 0 ▶ if( sqliteStrICmp(pOp->p3,"stdin")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteStrICmp(pOp->p3,"stdin")==0 ){ 0 0 0
4138 0 p->pFile = stdin;
4139 }else{
4140 0 p->pFile = fopen(pOp->p3, "r");
4141 }
4142 0 ▶ if( p->pFile==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->pFile==0 ){ 0 0 0
4143 0 sqliteSetString(&p->zErrMsg,"unable to open file: ", pOp->p3, (char*)0);
4144 0 rc = SQLITE_ERROR;
4145 }
4146 0 break;
4147 }
4148  
4149 /* Opcode: FileRead P1 P2 P3
4150 **
4151 ** Read a single line of input from the open file (the file opened using
4152 ** FileOpen). If we reach end-of-file, jump immediately to P2. If
4153 ** we are able to get another line, split the line apart using P3 as
4154 ** a delimiter. There should be P1 fields. If the input line contains
4155 ** more than P1 fields, ignore the excess. If the input line contains
4156 ** fewer than P1 fields, assume the remaining fields contain NULLs.
4157 **
4158 ** Input ends if a line consists of just "\.". A field containing only
4159 ** "\N" is a null field. The backslash \ character can be used be used
4160 ** to escape newlines or the delimiter.
4161 */
4162 0 case OP_FileRead: {
4163 int n, eol, nField, i, c, nDelim;
4164 char *zDelim, *z;
4165 0 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 0 0
4166 0 ▶ if( p->pFile==0 ) goto fileread_jump;
Branch: 1 branch
BranchTrueFalseTotal
if( p->pFile==0 ) goto fileread_jump; 0 0 0
4167 0 nField = pOp->p1;
4168 0 ▶ if( nField<=0 ) goto fileread_jump;
Branch: 1 branch
BranchTrueFalseTotal
if( nField<=0 ) goto fileread_jump; 0 0 0
4169 0 ▶ if( nField!=p->nField || p->azField==0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( nField!=p->nField || p->azField==0 ){ 0 0 0
if( nField!=p->nField || p->azField==0 ){ 0 0 0
4170 0 char **azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1);
4171 0 ▶ if( azField==0 ){ goto no_mem; }
Branch: 1 branch
BranchTrueFalseTotal
if( azField==0 ){ goto no_mem; } 0 0 0
4172 0 p->azField = azField;
4173 0 p->nField = nField;
4174 }
4175 0 n = 0;
4176 0 eol = 0;
4177 0 ▶ while( eol==0 ){
Branch: 1 branch
BranchTrueFalseTotal
while( eol==0 ){ 0 0 0
4178 0 ▶ if( p->zLine==0 || n+200>p->nLineAlloc ){
Branch: 2 branches
BranchTrueFalseTotal
if( p->zLine==0 || n+200>p->nLineAlloc ){ 0 0 0
if( p->zLine==0 || n+200>p->nLineAlloc ){ 0 0 0
4179 char *zLine;
4180 0 p->nLineAlloc = p->nLineAlloc*2 + 300;
4181 0 zLine = sqliteRealloc(p->zLine, p->nLineAlloc);
4182 0 ▶ if( zLine==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( zLine==0 ){ 0 0 0
4183 0 p->nLineAlloc = 0;
4184 0 sqliteFree(p->zLine);
4185 0 p->zLine = 0;
4186 0 goto no_mem;
4187 }
4188 0 p->zLine = zLine;
4189 }
4190 0 ▶ if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){ 0 0 0
4191 0 eol = 1;
4192 0 p->zLine[n] = 0;
4193 }else{
4194 int c;
4195 0 ▶ while( (c = p->zLine[n])!=0 ){
Branch: 1 branch
BranchTrueFalseTotal
while( (c = p->zLine[n])!=0 ){ 0 0 0
4196 0 ▶ if( c=='\\' ){
Branch: 1 branch
BranchTrueFalseTotal
if( c=='\\' ){ 0 0 0
4197 0 ▶ if( p->zLine[n+1]==0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( p->zLine[n+1]==0 ) break; 0 0 0
4198 0 n += 2;
4199 0 ▶ }else if( c=='\n' ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( c=='\n' ){ 0 0 0
4200 0 p->zLine[n] = 0;
4201 0 eol = 1;
4202 0 break;
4203 }else{
4204 0 n++;
4205 }
4206 }
4207 }
4208 }
4209 0 ▶ if( n==0 ) goto fileread_jump;
Branch: 1 branch
BranchTrueFalseTotal
if( n==0 ) goto fileread_jump; 0 0 0
4210 0 z = p->zLine;
4211 0 ▶ if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){
Branch: 3 branches
BranchTrueFalseTotal
if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){ 0 0 0
if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){ 0 0 0
if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){ 0 0 0
4212 0 goto fileread_jump;
4213 }
4214 0 zDelim = pOp->p3;
4215 0 ▶ if( zDelim==0 ) zDelim = "\t";
Branch: 1 branch
BranchTrueFalseTotal
if( zDelim==0 ) zDelim = "\t"; 0 0 0
4216 0 c = zDelim[0];
4217 0 nDelim = strlen(zDelim);
4218 0 p->azField[0] = z;
4219 0 ▶ for(i=1; *z!=0 && i<=nField; i++){
Branch: 2 branches
BranchTrueFalseTotal
for(i=1; *z!=0 && i<=nField; i++){ 0 0 0
for(i=1; *z!=0 && i<=nField; i++){ 0 0 0
4220 int from, to;
4221 0 from = to = 0;
4222 0 ▶ if( z[0]=='\\' && z[1]=='N'
Branch: 2 branches
BranchTrueFalseTotal
if( z[0]=='\\' && z[1]=='N' 0 0 0
if( z[0]=='\\' && z[1]=='N' 0 0 0
4223 0 ▶ && (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){
Branch: 2 branches
BranchTrueFalseTotal
&& (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){ 0 0 0
&& (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){ 0 0 0
4224 0 ▶ if( i<=nField ) p->azField[i-1] = 0;
Branch: 1 branch
BranchTrueFalseTotal
if( i<=nField ) p->azField[i-1] = 0; 0 0 0
4225 0 z += 2 + nDelim;
4226 0 ▶ if( i<nField ) p->azField[i] = z;
Branch: 1 branch
BranchTrueFalseTotal
if( i<nField ) p->azField[i] = z; 0 0 0
4227 0 continue;
4228 }
4229 0 ▶ while( z[from] ){
Branch: 1 branch
BranchTrueFalseTotal
while( z[from] ){ 0 0 0
4230 0 ▶ if( z[from]=='\\' && z[from+1]!=0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( z[from]=='\\' && z[from+1]!=0 ){ 0 0 0
if( z[from]=='\\' && z[from+1]!=0 ){ 0 0 0
4231 0 int tx = z[from+1];
4232 0 switch( tx ){
4233 0 case 'b': tx = '\b'; break;
4234 0 case 'f': tx = '\f'; break;
4235 0 case 'n': tx = '\n'; break;
4236 0 case 'r': tx = '\r'; break;
4237 0 case 't': tx = '\t'; break;
4238 0 case 'v': tx = '\v'; break;
4239 0 default: break;
4240 }
4241 0 z[to++] = tx;
4242 0 from += 2;
4243 0 continue;
4244 }
4245 0 ▶ if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break;
Branch: 2 branches
BranchTrueFalseTotal
if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break; 0 0 0
if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break; 0 0 0
4246 0 z[to++] = z[from++];
4247 }
4248 0 ▶ if( z[from] ){
Branch: 1 branch
BranchTrueFalseTotal
if( z[from] ){ 0 0 0
4249 0 z[to] = 0;
4250 0 z += from + nDelim;
4251 0 ▶ if( i<nField ) p->azField[i] = z;
Branch: 1 branch
BranchTrueFalseTotal
if( i<nField ) p->azField[i] = z; 0 0 0
4252 }else{
4253 0 z[to] = 0;
4254 0 z = "";
4255 }
4256 }
4257 0 ▶ while( i<nField ){
Branch: 1 branch
BranchTrueFalseTotal
while( i<nField ){ 0 0 0
4258 0 p->azField[i++] = 0;
4259 }
4260 0 break;
4261  
4262 /* If we reach end-of-file, or if anything goes wrong, jump here.
4263 ** This code will cause a jump to P2 */
4264 0 fileread_jump:
4265 0 pc = pOp->p2 - 1;
4266 0 break;
4267 }
4268  
4269 /* Opcode: FileColumn P1 * *
4270 **
4271 ** Push onto the stack the P1-th column of the most recently read line
4272 ** from the input file.
4273 */
4274 0 case OP_FileColumn: {
4275 0 int i = pOp->p1;
4276 char *z;
4277 assert( i>=0 && i<p->nField );
4278 0 ▶ if( p->azField ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->azField ){ 0 0 0
4279 0 z = p->azField[i];
4280 }else{
4281 0 z = 0;
4282 }
4283 0 pTos++;
4284 0 ▶ if( z ){
Branch: 1 branch
BranchTrueFalseTotal
if( z ){ 0 0 0
4285 0 pTos->n = strlen(z) + 1;
4286 0 pTos->z = z;
4287 0 pTos->flags = MEM_Str | MEM_Ephem;
4288 }else{
4289 0 pTos->flags = MEM_Null;
4290 }
4291 0 break;
4292 }
4293  
4294 /* Opcode: MemStore P1 P2 *
4295 **
4296 ** Write the top of the stack into memory location P1.
4297 ** P1 should be a small integer since space is allocated
4298 ** for all memory locations between 0 and P1 inclusive.
4299 **
4300 ** After the data is stored in the memory location, the
4301 ** stack is popped once if P2 is 1. If P2 is zero, then
4302 ** the original data remains on the stack.
4303 */
4304 11 case OP_MemStore: {
4305 11 int i = pOp->p1;
4306 Mem *pMem;
4307 assert( pTos>=p->aStack );
4308 11 ▶ if( i>=p->nMem ){
Branch: 1 branch
BranchTrueFalseTotal
if( i>=p->nMem ){ 11 0 11
4309 11 int nOld = p->nMem;
4310 Mem *aMem;
4311 11 p->nMem = i + 5;
4312 11 aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
4313 11 ▶ if( aMem==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( aMem==0 ) goto no_mem; 0 11 11
4314 11 ▶ if( aMem!=p->aMem ){
Branch: 1 branch
BranchTrueFalseTotal
if( aMem!=p->aMem ){ 11 0 11
4315 int j;
4316 11 ▶ for(j=0; j<nOld; j++){
Branch: 1 branch
BranchTrueFalseTotal
for(j=0; j<nOld; j++){ 0 11 11
4317 0 ▶ if( aMem[j].flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( aMem[j].flags & MEM_Short ){ 0 0 0
4318 0 aMem[j].z = aMem[j].zShort;
4319 }
4320 }
4321 }
4322 11 p->aMem = aMem;
4323 11 ▶ if( nOld<p->nMem ){
Branch: 1 branch
BranchTrueFalseTotal
if( nOld<p->nMem ){ 11 0 11
4324 11 memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
4325 }
4326 }
4327 11 ▶ Deephemeralize(pTos);
Branch: 2 branches
BranchTrueFalseTotal
Deephemeralize(pTos); 0 11 11
Deephemeralize(pTos); 0 0 0
4328 11 pMem = &p->aMem[i];
4329 11 ▶ Release(pMem);
Branch: 1 branch
BranchTrueFalseTotal
Release(pMem); 0 11 11
4330 11 *pMem = *pTos;
4331 11 ▶ if( pMem->flags & MEM_Dyn ){
Branch: 1 branch
BranchTrueFalseTotal
if( pMem->flags & MEM_Dyn ){ 0 11 11
4332 0 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 0 0 0
4333 0 pTos->flags = MEM_Null;
4334 }else{
4335 0 pMem->z = sqliteMallocRaw( pMem->n );
4336 0 ▶ if( pMem->z==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( pMem->z==0 ) goto no_mem; 0 0 0
4337 0 memcpy(pMem->z, pTos->z, pMem->n);
4338 }
4339 11 ▶ }else if( pMem->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pMem->flags & MEM_Short ){ 0 11 11
4340 0 pMem->z = pMem->zShort;
4341 }
4342 11 ▶ if( pOp->p2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p2 ){ 11 0 11
4343 11 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 11 11
4344 11 pTos--;
4345 }
4346 11 break;
4347 }
4348  
4349 /* Opcode: MemLoad P1 * *
4350 **
4351 ** Push a copy of the value in memory location P1 onto the stack.
4352 **
4353 ** If the value is a string, then the value pushed is a pointer to
4354 ** the string that is stored in the memory location. If the memory
4355 ** location is subsequently changed (using OP_MemStore) then the
4356 ** value pushed onto the stack will change too.
4357 */
4358 11 case OP_MemLoad: {
4359 11 int i = pOp->p1;
4360 assert( i>=0 && i<p->nMem );
4361 11 pTos++;
4362 11 memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
4363 11 ▶ if( pTos->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Str ){ 11 0 11
4364 11 pTos->flags |= MEM_Ephem;
4365 11 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4366 }
4367 11 break;
4368 }
4369  
4370 /* Opcode: MemIncr P1 P2 *
4371 **
4372 ** Increment the integer valued memory cell P1 by 1. If P2 is not zero
4373 ** and the result after the increment is greater than zero, then jump
4374 ** to P2.
4375 **
4376 ** This instruction throws an error if the memory cell is not initially
4377 ** an integer.
4378 */
4379 0 case OP_MemIncr: {
4380 0 int i = pOp->p1;
4381 Mem *pMem;
4382 assert( i>=0 && i<p->nMem );
4383 0 pMem = &p->aMem[i];
4384 assert( pMem->flags==MEM_Int );
4385 0 pMem->i++;
4386 0 ▶ if( pOp->p2>0 && pMem->i>0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p2>0 && pMem->i>0 ){ 0 0 0
if( pOp->p2>0 && pMem->i>0 ){ 0 0 0
4387 0 pc = pOp->p2 - 1;
4388 }
4389 0 break;
4390 }
4391  
4392 /* Opcode: AggReset * P2 *
4393 **
4394 ** Reset the aggregator so that it no longer contains any data.
4395 ** Future aggregator elements will contain P2 values each.
4396 */
4397 15 case OP_AggReset: {
4398 15 sqliteVdbeAggReset(&p->agg);
4399 15 p->agg.nMem = pOp->p2;
4400 15 p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
4401 15 ▶ if( p->agg.apFunc==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( p->agg.apFunc==0 ) goto no_mem; 0 15 15
4402 15 break;
4403 }
4404  
4405 /* Opcode: AggInit * P2 P3
4406 **
4407 ** Initialize the function parameters for an aggregate function.
4408 ** The aggregate will operate out of aggregate column P2.
4409 ** P3 is a pointer to the FuncDef structure for the function.
4410 */
4411 15 case OP_AggInit: {
4412 15 int i = pOp->p2;
4413 assert( i>=0 && i<p->agg.nMem );
4414 15 p->agg.apFunc[i] = (FuncDef*)pOp->p3;
4415 15 break;
4416 }
4417  
4418 /* Opcode: AggFunc * P2 P3
4419 **
4420 ** Execute the step function for an aggregate. The
4421 ** function has P2 arguments. P3 is a pointer to the FuncDef
4422 ** structure that specifies the function.
4423 **
4424 ** The top of the stack must be an integer which is the index of
4425 ** the aggregate column that corresponds to this aggregate function.
4426 ** Ideally, this index would be another parameter, but there are
4427 ** no free parameters left. The integer is popped from the stack.
4428 */
4429 24 case OP_AggFunc: {
4430 24 int n = pOp->p2;
4431 int i;
4432 Mem *pMem, *pRec;
4433 24 char **azArgv = p->zArgv;
4434 sqlite_func ctx;
4435  
4436 assert( n>=0 );
4437 assert( pTos->flags==MEM_Int );
4438 24 pRec = &pTos[-n];
4439 assert( pRec>=p->aStack );
4440 30 ▶ for(i=0; i<n; i++, pRec++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<n; i++, pRec++){ 6 24 30
4441 6 ▶ if( pRec->flags & MEM_Null ){
Branch: 1 branch
BranchTrueFalseTotal
if( pRec->flags & MEM_Null ){ 2 4 6
4442 2 azArgv[i] = 0;
4443 }else{
4444 4 ▶ Stringify(pRec);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pRec); 0 4 4
4445 4 azArgv[i] = pRec->z;
4446 }
4447 }
4448 24 i = pTos->i;
4449 assert( i>=0 && i<p->agg.nMem );
4450 24 ctx.pFunc = (FuncDef*)pOp->p3;
4451 24 pMem = &p->agg.pCurrent->aMem[i];
4452 24 ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */
4453 24 ctx.pAgg = pMem->z;
4454 24 ctx.cnt = ++pMem->i;
4455 24 ctx.isError = 0;
4456 24 ctx.isStep = 1;
4457 24 (ctx.pFunc->xStep)(&ctx, n, (const char**)azArgv);
4458 24 pMem->z = ctx.pAgg;
4459 24 pMem->flags = MEM_AggCtx;
4460 24 popStack(&pTos, n+1);
4461 24 ▶ if( ctx.isError ){
Branch: 1 branch
BranchTrueFalseTotal
if( ctx.isError ){ 0 24 24
4462 0 rc = SQLITE_ERROR;
4463 }
4464 24 break;
4465 }
4466  
4467 /* Opcode: AggFocus * P2 *
4468 **
4469 ** Pop the top of the stack and use that as an aggregator key. If
4470 ** an aggregator with that same key already exists, then make the
4471 ** aggregator the current aggregator and jump to P2. If no aggregator
4472 ** with the given key exists, create one and make it current but
4473 ** do not jump.
4474 **
4475 ** The order of aggregator opcodes is important. The order is:
4476 ** AggReset AggFocus AggNext. In other words, you must execute
4477 ** AggReset first, then zero or more AggFocus operations, then
4478 ** zero or more AggNext operations. You must not execute an AggFocus
4479 ** in between an AggNext and an AggReset.
4480 */
4481 16 case OP_AggFocus: {
4482 AggElem *pElem;
4483 char *zKey;
4484 int nKey;
4485  
4486 assert( pTos>=p->aStack );
4487 16 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 13 3 16
4488 16 zKey = pTos->z;
4489 16 nKey = pTos->n;
4490 16 pElem = sqliteHashFind(&p->agg.hash, zKey, nKey);
4491 16 ▶ if( pElem ){
Branch: 1 branch
BranchTrueFalseTotal
if( pElem ){ 0 16 16
4492 0 p->agg.pCurrent = pElem;
4493 0 pc = pOp->p2 - 1;
4494 }else{
4495 16 AggInsert(&p->agg, zKey, nKey);
4496 16 ▶ if( sqlite_malloc_failed ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( sqlite_malloc_failed ) goto no_mem; 0 16 16
4497 }
4498 16 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 16 16
4499 16 pTos--;
4500 16 break;
4501 }
4502  
4503 /* Opcode: AggSet * P2 *
4504 **
4505 ** Move the top of the stack into the P2-th field of the current
4506 ** aggregate. String values are duplicated into new memory.
4507 */
4508 3 case OP_AggSet: {
4509 3 ▶ AggElem *pFocus = AggInFocus(p->agg);
Branch: 1 branch
BranchTrueFalseTotal
AggElem *pFocus = AggInFocus(p->agg); 3 0 3
4510 Mem *pMem;
4511 3 int i = pOp->p2;
4512 assert( pTos>=p->aStack );
4513 3 ▶ if( pFocus==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( pFocus==0 ) goto no_mem; 0 3 3
4514 assert( i>=0 && i<p->agg.nMem );
4515 3 ▶ Deephemeralize(pTos);
Branch: 2 branches
BranchTrueFalseTotal
Deephemeralize(pTos); 0 3 3
Deephemeralize(pTos); 0 0 0
4516 3 pMem = &pFocus->aMem[i];
4517 3 ▶ Release(pMem);
Branch: 1 branch
BranchTrueFalseTotal
Release(pMem); 0 3 3
4518 3 *pMem = *pTos;
4519 3 ▶ if( pMem->flags & MEM_Dyn ){
Branch: 1 branch
BranchTrueFalseTotal
if( pMem->flags & MEM_Dyn ){ 0 3 3
4520 0 pTos->flags = MEM_Null;
4521 3 ▶ }else if( pMem->flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( pMem->flags & MEM_Short ){ 2 1 3
4522 2 pMem->z = pMem->zShort;
4523 }
4524 3 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 3 3
4525 3 pTos--;
4526 3 break;
4527 }
4528  
4529 /* Opcode: AggGet * P2 *
4530 **
4531 ** Push a new entry onto the stack which is a copy of the P2-th field
4532 ** of the current aggregate. Strings are not duplicated so
4533 ** string values will be ephemeral.
4534 */
4535 16 case OP_AggGet: {
4536 16 ▶ AggElem *pFocus = AggInFocus(p->agg);
Branch: 1 branch
BranchTrueFalseTotal
AggElem *pFocus = AggInFocus(p->agg); 16 0 16
4537 Mem *pMem;
4538 16 int i = pOp->p2;
4539 16 ▶ if( pFocus==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( pFocus==0 ) goto no_mem; 0 16 16
4540 assert( i>=0 && i<p->agg.nMem );
4541 16 pTos++;
4542 16 pMem = &pFocus->aMem[i];
4543 16 *pTos = *pMem;
4544 16 ▶ if( pTos->flags & MEM_Str ){
Branch: 1 branch
BranchTrueFalseTotal
if( pTos->flags & MEM_Str ){ 0 16 16
4545 0 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4546 0 pTos->flags |= MEM_Ephem;
4547 }
4548 16 break;
4549 }
4550  
4551 /* Opcode: AggNext * P2 *
4552 **
4553 ** Make the next aggregate value the current aggregate. The prior
4554 ** aggregate is deleted. If all aggregate values have been consumed,
4555 ** jump to P2.
4556 **
4557 ** The order of aggregator opcodes is important. The order is:
4558 ** AggReset AggFocus AggNext. In other words, you must execute
4559 ** AggReset first, then zero or more AggFocus operations, then
4560 ** zero or more AggNext operations. You must not execute an AggFocus
4561 ** in between an AggNext and an AggReset.
4562 */
4563 31 case OP_AggNext: {
4564 31 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 31 31
4565 31 ▶ if( p->agg.pSearch==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->agg.pSearch==0 ){ 15 16 31
4566 15 p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
4567 }else{
4568 16 p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
4569 }
4570 31 ▶ if( p->agg.pSearch==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->agg.pSearch==0 ){ 15 16 31
4571 15 pc = pOp->p2 - 1;
4572 } else {
4573 int i;
4574 sqlite_func ctx;
4575 Mem *aMem;
4576 16 p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
4577 16 aMem = p->agg.pCurrent->aMem;
4578 35 ▶ for(i=0; i<p->agg.nMem; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<p->agg.nMem; i++){ 19 16 35
4579 int freeCtx;
4580 19 ▶ if( p->agg.apFunc[i]==0 ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( p->agg.apFunc[i]==0 ) continue; 3 16 19
4581 16 ▶ if( p->agg.apFunc[i]->xFinalize==0 ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( p->agg.apFunc[i]->xFinalize==0 ) continue; 0 16 16
4582 16 ctx.s.flags = MEM_Null;
4583 16 ctx.s.z = aMem[i].zShort;
4584 16 ctx.pAgg = (void*)aMem[i].z;
4585 16 ▶ freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
Branch: 2 branches
BranchTrueFalseTotal
freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort; 10 6 16
freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort; 0 10 10
4586 16 ctx.cnt = aMem[i].i;
4587 16 ctx.isStep = 0;
4588 16 ctx.pFunc = p->agg.apFunc[i];
4589 16 (*p->agg.apFunc[i]->xFinalize)(&ctx);
4590 16 ▶ if( freeCtx ){
Branch: 1 branch
BranchTrueFalseTotal
if( freeCtx ){ 0 16 16
4591 0 sqliteFree( aMem[i].z );
4592 }
4593 16 aMem[i] = ctx.s;
4594 16 ▶ if( aMem[i].flags & MEM_Short ){
Branch: 1 branch
BranchTrueFalseTotal
if( aMem[i].flags & MEM_Short ){ 0 16 16
4595 0 aMem[i].z = aMem[i].zShort;
4596 }
4597 }
4598 }
4599 31 break;
4600 }
4601  
4602 /* Opcode: SetInsert P1 * P3
4603 **
4604 ** If Set P1 does not exist then create it. Then insert value
4605 ** P3 into that set. If P3 is NULL, then insert the top of the
4606 ** stack into the set.
4607 */
4608 12 case OP_SetInsert: {
4609 12 int i = pOp->p1;
4610 12 ▶ if( p->nSet<=i ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->nSet<=i ){ 6 6 12
4611 int k;
4612 6 Set *aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) );
4613 6 ▶ if( aSet==0 ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( aSet==0 ) goto no_mem; 0 6 6
4614 6 p->aSet = aSet;
4615 12 ▶ for(k=p->nSet; k<=i; k++){
Branch: 1 branch
BranchTrueFalseTotal
for(k=p->nSet; k<=i; k++){ 6 6 12
4616 6 sqliteHashInit(&p->aSet[k].hash, SQLITE_HASH_BINARY, 1);
4617 }
4618 6 p->nSet = i+1;
4619 }
4620 12 ▶ if( pOp->p3 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->p3 ){ 12 0 12
4621 12 sqliteHashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p);
4622 }else{
4623 assert( pTos>=p->aStack );
4624 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
4625 0 sqliteHashInsert(&p->aSet[i].hash, pTos->z, pTos->n, p);
4626 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
4627 0 pTos--;
4628 }
4629 12 ▶ if( sqlite_malloc_failed ) goto no_mem;
Branch: 1 branch
BranchTrueFalseTotal
if( sqlite_malloc_failed ) goto no_mem; 0 12 12
4630 12 break;
4631 }
4632  
4633 /* Opcode: SetFound P1 P2 *
4634 **
4635 ** Pop the stack once and compare the value popped off with the
4636 ** contents of set P1. If the element popped exists in set P1,
4637 ** then jump to P2. Otherwise fall through.
4638 */
4639 0 case OP_SetFound: {
4640 0 int i = pOp->p1;
4641 assert( pTos>=p->aStack );
4642 0 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 0 0
4643 0 ▶ if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){
Branch: 3 branches
BranchTrueFalseTotal
if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){ 0 0 0
if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){ 0 0 0
if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){ 0 0 0
4644 0 pc = pOp->p2 - 1;
4645 }
4646 0 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 0 0
4647 0 pTos--;
4648 0 break;
4649 }
4650  
4651 /* Opcode: SetNotFound P1 P2 *
4652 **
4653 ** Pop the stack once and compare the value popped off with the
4654 ** contents of set P1. If the element popped does not exists in
4655 ** set P1, then jump to P2. Otherwise fall through.
4656 */
4657 34 case OP_SetNotFound: {
4658 34 int i = pOp->p1;
4659 assert( pTos>=p->aStack );
4660 34 ▶ Stringify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Stringify(pTos); 0 34 34
4661 68 ▶ if( i<0 || i>=p->nSet ||
Branch: 1 branch
BranchTrueFalseTotal
if( i<0 || i>=p->nSet || 34 0 34
4662 34 sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)==0 ){
4663 14 pc = pOp->p2 - 1;
4664 }
4665 34 ▶ Release(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Release(pTos); 0 34 34
4666 34 pTos--;
4667 34 break;
4668 }
4669  
4670 /* Opcode: SetFirst P1 P2 *
4671 **
4672 ** Read the first element from set P1 and push it onto the stack. If the
4673 ** set is empty, push nothing and jump immediately to P2. This opcode is
4674 ** used in combination with OP_SetNext to loop over all elements of a set.
4675 */
4676 /* Opcode: SetNext P1 P2 *
4677 **
4678 ** Read the next element from set P1 and push it onto the stack. If there
4679 ** are no more elements in the set, do not do the push and fall through.
4680 ** Otherwise, jump to P2 after pushing the next set element.
4681 */
4682 0 case OP_SetFirst:
4683 case OP_SetNext: {
4684 Set *pSet;
4685 0 ▶ CHECK_FOR_INTERRUPT;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT; 0 0 0
4686 0 ▶ if( pOp->p1<0 || pOp->p1>=p->nSet ){
Branch: 2 branches
BranchTrueFalseTotal
if( pOp->p1<0 || pOp->p1>=p->nSet ){ 0 0 0
if( pOp->p1<0 || pOp->p1>=p->nSet ){ 0 0 0
4687 0 ▶ if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1;
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1; 0 0 0
4688 0 break;
4689 }
4690 0 pSet = &p->aSet[pOp->p1];
4691 0 ▶ if( pOp->opcode==OP_SetFirst ){
Branch: 1 branch
BranchTrueFalseTotal
if( pOp->opcode==OP_SetFirst ){ 0 0 0
4692 0 pSet->prev = sqliteHashFirst(&pSet->hash);
4693 0 ▶ if( pSet->prev==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pSet->prev==0 ){ 0 0 0
4694 0 pc = pOp->p2 - 1;
4695 0 break;
4696 }
4697 }else{
4698 0 ▶ if( pSet->prev ){
Branch: 1 branch
BranchTrueFalseTotal
if( pSet->prev ){ 0 0 0
4699 0 pSet->prev = sqliteHashNext(pSet->prev);
4700 }
4701 0 ▶ if( pSet->prev==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( pSet->prev==0 ){ 0 0 0
4702 0 break;
4703 }else{
4704 0 pc = pOp->p2 - 1;
4705 }
4706 }
4707 0 pTos++;
4708 0 pTos->z = sqliteHashKey(pSet->prev);
4709 0 pTos->n = sqliteHashKeysize(pSet->prev);
4710 0 pTos->flags = MEM_Str | MEM_Ephem;
4711 0 break;
4712 }
4713  
4714 /* Opcode: Vacuum * * *
4715 **
4716 ** Vacuum the entire database. This opcode will cause other virtual
4717 ** machines to be created and run. It may not be called from within
4718 ** a transaction.
4719 */
4720 0 case OP_Vacuum: {
4721 0 ▶ if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 0 0 0
4722 0 rc = sqliteRunVacuum(&p->zErrMsg, db);
4723 0 ▶ if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteSafetyOn(db) ) goto abort_due_to_misuse; 0 0 0
4724 0 break;
4725 }
4726  
4727 /* Opcode: StackDepth * * *
4728 **
4729 ** Push an integer onto the stack which is the depth of the stack prior
4730 ** to that integer being pushed.
4731 */
4732 0 case OP_StackDepth: {
4733 0 int depth = (&pTos[1]) - p->aStack;
4734 0 pTos++;
4735 0 pTos->i = depth;
4736 0 pTos->flags = MEM_Int;
4737 0 break;
4738 }
4739  
4740 /* Opcode: StackReset * * *
4741 **
4742 ** Pop a single integer off of the stack. Then pop the stack
4743 ** as many times as necessary to get the depth of the stack down
4744 ** to the value of the integer that was popped.
4745 */
4746 0 case OP_StackReset: {
4747 int depth, goal;
4748 assert( pTos>=p->aStack );
4749 0 ▶ Integerify(pTos);
Branch: 1 branch
BranchTrueFalseTotal
Integerify(pTos); 0 0 0
4750 0 goal = pTos->i;
4751 0 depth = (&pTos[1]) - p->aStack;
4752 assert( goal<depth );
4753 0 popStack(&pTos, depth-goal);
4754 0 break;
4755 }
4756  
4757 /* An other opcode is illegal...
4758 */
4759 0 default: {
4760 0 sqlite_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
4761 0 sqliteSetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
4762 0 rc = SQLITE_INTERNAL;
4763 0 break;
4764 }
4765  
4766 /*****************************************************************************
4767 ** The cases of the switch statement above this line should all be indented
4768 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4769 ** readability. From this point on down, the normal indentation rules are
4770 ** restored.
4771 *****************************************************************************/
4772 }
4773  
4774 #ifdef VDBE_PROFILE
4775 {
4776 long long elapse = hwtime() - start;
4777 pOp->cycles += elapse;
4778 pOp->cnt++;
4779 #if 0
4780 fprintf(stdout, "%10lld ", elapse);
4781 sqliteVdbePrintOp(stdout, origPc, &p->aOp[origPc]);
4782 #endif
4783 }
4784 #endif
4785  
4786 /* The following code adds nothing to the actual functionality
4787 ** of the program. It is only here for testing and debugging.
4788 ** On the other hand, it does burn CPU cycles every time through
4789 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4790 */
4791 #ifndef NDEBUG
4792 /* Sanity checking on the top element of the stack */
4793 if( pTos>=p->aStack ){
4794 assert( pTos->flags!=0 ); /* Must define some type */
4795 if( pTos->flags & MEM_Str ){
4796 int x = pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
4797 assert( x!=0 ); /* Strings must define a string subtype */
4798 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
4799 assert( pTos->z!=0 ); /* Strings must have a value */
4800 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
4801 assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort );
4802 assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort );
4803 }else{
4804 /* Cannot define a string subtype for non-string objects */
4805 assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
4806 }
4807 /* MEM_Null excludes all other types */
4808 assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 );
4809 }
4810 if( pc<-1 || pc>=p->nOp ){
4811 sqliteSetString(&p->zErrMsg, "jump destination out of range", (char*)0);
4812 rc = SQLITE_INTERNAL;
4813 }
4814 if( p->trace && pTos>=p->aStack ){
4815 int i;
4816 fprintf(p->trace, "Stack:");
4817 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4818 if( pTos[i].flags & MEM_Null ){
4819 fprintf(p->trace, " NULL");
4820 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
4821 fprintf(p->trace, " si:%d", pTos[i].i);
4822 }else if( pTos[i].flags & MEM_Int ){
4823 fprintf(p->trace, " i:%d", pTos[i].i);
4824 }else if( pTos[i].flags & MEM_Real ){
4825 fprintf(p->trace, " r:%g", pTos[i].r);
4826 }else if( pTos[i].flags & MEM_Str ){
4827 int j, k;
4828 char zBuf[100];
4829 zBuf[0] = ' ';
4830 if( pTos[i].flags & MEM_Dyn ){
4831 zBuf[1] = 'z';
4832 assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 );
4833 }else if( pTos[i].flags & MEM_Static ){
4834 zBuf[1] = 't';
4835 assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
4836 }else if( pTos[i].flags & MEM_Ephem ){
4837 zBuf[1] = 'e';
4838 assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 );
4839 }else{
4840 zBuf[1] = 's';
4841 }
4842 zBuf[2] = '[';
4843 k = 3;
4844 for(j=0; j<20 && j<pTos[i].n; j++){
4845 int c = pTos[i].z[j];
4846 if( c==0 && j==pTos[i].n-1 ) break;
4847 if( isprint(c) && !isspace(c) ){
4848 zBuf[k++] = c;
4849 }else{
4850 zBuf[k++] = '.';
4851 }
4852 }
4853 zBuf[k++] = ']';
4854 zBuf[k++] = 0;
4855 fprintf(p->trace, "%s", zBuf);
4856 }else{
4857 fprintf(p->trace, " ???");
4858 }
4859 }
4860 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
4861 fprintf(p->trace,"\n");
4862 }
4863 #endif
4864 } /* The end of the for(;;) loop the loops through opcodes */
4865  
4866 /* If we reach this point, it means that execution is finished.
4867 */
4868 1 vdbe_halt:
4869 1 ▶ CHECK_FOR_INTERRUPT
Branch: 1 branch
BranchTrueFalseTotal
CHECK_FOR_INTERRUPT 0 1 1
4870 1 ▶ if( rc ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc ){ 1 0 1
4871 1 p->rc = rc;
4872 1 rc = SQLITE_ERROR;
4873 }else{
4874 0 rc = SQLITE_DONE;
4875 }
4876 1 p->magic = VDBE_MAGIC_HALT;
4877 1 p->pTos = pTos;
4878 1 return rc;
4879  
4880 /* Jump to here if a malloc() fails. It's hard to get a malloc()
4881 ** to fail on a modern VM computer, so this code is untested.
4882 */
4883 0 no_mem:
4884 0 sqliteSetString(&p->zErrMsg, "out of memory", (char*)0);
4885 0 rc = SQLITE_NOMEM;
4886 0 goto vdbe_halt;
4887  
4888 /* Jump to here for an SQLITE_MISUSE error.
4889 */
4890 0 abort_due_to_misuse:
4891 0 rc = SQLITE_MISUSE;
4892 /* Fall thru into abort_due_to_error */
4893  
4894 /* Jump to here for any other kind of fatal error. The "rc" variable
4895 ** should hold the error number.
4896 */
4897 0 abort_due_to_error:
4898 0 ▶ if( p->zErrMsg==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->zErrMsg==0 ){ 0 0 0
4899 0 ▶ if( sqlite_malloc_failed ) rc = SQLITE_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if( sqlite_malloc_failed ) rc = SQLITE_NOMEM; 0 0 0
4900 0 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
4901 }
4902 0 goto vdbe_halt;
4903  
4904 /* Jump to here if the sqlite_interrupt() API sets the interrupt
4905 ** flag.
4906 */
4907 0 abort_due_to_interrupt:
4908 assert( db->flags & SQLITE_Interrupt );
4909 0 db->flags &= ~SQLITE_Interrupt;
4910 0 ▶ if( db->magic!=SQLITE_MAGIC_BUSY ){
Branch: 1 branch
BranchTrueFalseTotal
if( db->magic!=SQLITE_MAGIC_BUSY ){ 0 0 0
4911 0 rc = SQLITE_MISUSE;
4912 }else{
4913 0 rc = SQLITE_INTERRUPT;
4914 }
4915 0 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
4916 0 goto vdbe_halt;
4917 }
« expr.c ↑ Summary select.c »