blob: ad4207b35e74c8abf6d31dd95a637afbe80ea38a [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
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** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <ctype.h>
20#include "vdbeInt.h"
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
danielk1977132872b2004-05-10 10:37:18 +000025** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000026** as they are added to the instruction stream.
27*/
28#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +000029int sqlite3_vdbe_addop_trace = 0;
drh9a324642003-09-06 20:12:01 +000030#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
danielk19774adee202004-05-08 08:23:19 +000036Vdbe *sqlite3VdbeCreate(sqlite *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
59** Add a new instruction to the list of instructions current in the
60** VDBE. Return the address of the new instruction.
61**
62** Parameters:
63**
64** p Pointer to the VDBE
65**
66** op The opcode for this instruction
67**
68** p1, p2 First two of the three possible operands.
69**
danielk19774adee202004-05-08 08:23:19 +000070** Use the sqlite3VdbeResolveLabel() function to fix an address and
71** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000072** operand.
73*/
danielk19774adee202004-05-08 08:23:19 +000074int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000075 int i;
drh701a0ae2004-02-22 20:05:00 +000076 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +000077
78 i = p->nOp;
79 p->nOp++;
80 assert( p->magic==VDBE_MAGIC_INIT );
81 if( i>=p->nOpAlloc ){
82 int oldSize = p->nOpAlloc;
83 Op *aNew;
84 p->nOpAlloc = p->nOpAlloc*2 + 100;
85 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
86 if( aNew==0 ){
87 p->nOpAlloc = oldSize;
88 return 0;
89 }
90 p->aOp = aNew;
91 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
92 }
drh701a0ae2004-02-22 20:05:00 +000093 pOp = &p->aOp[i];
94 pOp->opcode = op;
95 pOp->p1 = p1;
drh9a324642003-09-06 20:12:01 +000096 if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){
97 p2 = p->aLabel[-1-p2];
98 }
drh701a0ae2004-02-22 20:05:00 +000099 pOp->p2 = p2;
100 pOp->p3 = 0;
101 pOp->p3type = P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000102#ifndef NDEBUG
drhd3d39e92004-05-20 22:16:29 +0000103 pOp->zComment = 0;
danielk1977132872b2004-05-10 10:37:18 +0000104 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000105#endif
106 return i;
107}
108
109/*
drh701a0ae2004-02-22 20:05:00 +0000110** Add an opcode that includes the p3 value.
111*/
drheb2e1762004-05-27 01:53:56 +0000112int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000113 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
114 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000115 return addr;
116}
117
118/*
119** Add multiple opcodes. The list is terminated by an opcode of 0.
120*/
danielk19774adee202004-05-08 08:23:19 +0000121int sqlite3VdbeCode(Vdbe *p, ...){
drh701a0ae2004-02-22 20:05:00 +0000122 int addr;
123 va_list ap;
124 int opcode, p1, p2;
125 va_start(ap, p);
126 addr = p->nOp;
127 while( (opcode = va_arg(ap,int))!=0 ){
128 p1 = va_arg(ap,int);
129 p2 = va_arg(ap,int);
danielk19774adee202004-05-08 08:23:19 +0000130 sqlite3VdbeAddOp(p, opcode, p1, p2);
drh701a0ae2004-02-22 20:05:00 +0000131 }
132 va_end(ap);
133 return addr;
134}
135
136
137
138/*
drh9a324642003-09-06 20:12:01 +0000139** Create a new symbolic label for an instruction that has yet to be
140** coded. The symbolic label is really just a negative number. The
141** label can be used as the P2 value of an operation. Later, when
142** the label is resolved to a specific address, the VDBE will scan
143** through its operation list and change all values of P2 which match
144** the label into the resolved address.
145**
146** The VDBE knows that a P2 value is a label because labels are
147** always negative and P2 values are suppose to be non-negative.
148** Hence, a negative P2 value is a label that has yet to be resolved.
149*/
danielk19774adee202004-05-08 08:23:19 +0000150int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000151 int i;
152 i = p->nLabel++;
153 assert( p->magic==VDBE_MAGIC_INIT );
154 if( i>=p->nLabelAlloc ){
155 int *aNew;
156 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
157 aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
158 if( aNew==0 ){
159 sqliteFree(p->aLabel);
160 }
161 p->aLabel = aNew;
162 }
163 if( p->aLabel==0 ){
164 p->nLabel = 0;
165 p->nLabelAlloc = 0;
166 return 0;
167 }
168 p->aLabel[i] = -1;
169 return -1-i;
170}
171
172/*
173** Resolve label "x" to be the address of the next instruction to
174** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000175** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000176*/
danielk19774adee202004-05-08 08:23:19 +0000177void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh9a324642003-09-06 20:12:01 +0000178 int j;
179 assert( p->magic==VDBE_MAGIC_INIT );
180 if( x<0 && (-x)<=p->nLabel && p->aOp ){
181 if( p->aLabel[-1-x]==p->nOp ) return;
182 assert( p->aLabel[-1-x]<0 );
183 p->aLabel[-1-x] = p->nOp;
184 for(j=0; j<p->nOp; j++){
185 if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp;
186 }
187 }
188}
189
190/*
191** Return the address of the next instruction to be inserted.
192*/
danielk19774adee202004-05-08 08:23:19 +0000193int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000194 assert( p->magic==VDBE_MAGIC_INIT );
195 return p->nOp;
196}
197
198/*
199** Add a whole list of operations to the operation stack. Return the
200** address of the first operation added.
201*/
danielk19774adee202004-05-08 08:23:19 +0000202int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000203 int addr;
204 assert( p->magic==VDBE_MAGIC_INIT );
205 if( p->nOp + nOp >= p->nOpAlloc ){
206 int oldSize = p->nOpAlloc;
207 Op *aNew;
208 p->nOpAlloc = p->nOpAlloc*2 + nOp + 10;
209 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
210 if( aNew==0 ){
211 p->nOpAlloc = oldSize;
212 return 0;
213 }
214 p->aOp = aNew;
215 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
216 }
217 addr = p->nOp;
218 if( nOp>0 ){
219 int i;
drh905793e2004-02-21 13:31:09 +0000220 VdbeOpList const *pIn = aOp;
221 for(i=0; i<nOp; i++, pIn++){
222 int p2 = pIn->p2;
223 VdbeOp *pOut = &p->aOp[i+addr];
224 pOut->opcode = pIn->opcode;
225 pOut->p1 = pIn->p1;
226 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
227 pOut->p3 = pIn->p3;
228 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000229#ifndef NDEBUG
drhd3d39e92004-05-20 22:16:29 +0000230 pOut->zComment = 0;
danielk1977132872b2004-05-10 10:37:18 +0000231 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000232 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000233 }
234#endif
235 }
236 p->nOp += nOp;
237 }
238 return addr;
239}
240
241/*
242** Change the value of the P1 operand for a specific instruction.
243** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000244** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000245** few minor changes to the program.
246*/
danielk19774adee202004-05-08 08:23:19 +0000247void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000248 assert( p->magic==VDBE_MAGIC_INIT );
249 if( p && addr>=0 && p->nOp>addr && p->aOp ){
250 p->aOp[addr].p1 = val;
251 }
252}
253
254/*
255** Change the value of the P2 operand for a specific instruction.
256** This routine is useful for setting a jump destination.
257*/
danielk19774adee202004-05-08 08:23:19 +0000258void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000259 assert( val>=0 );
260 assert( p->magic==VDBE_MAGIC_INIT );
261 if( p && addr>=0 && p->nOp>addr && p->aOp ){
262 p->aOp[addr].p2 = val;
263 }
264}
265
266/*
267** Change the value of the P3 operand for a specific instruction.
268** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000269** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000270** few minor changes to the program.
271**
272** If n>=0 then the P3 operand is dynamic, meaning that a copy of
273** the string is made into memory obtained from sqliteMalloc().
274** A value of n==0 means copy bytes of zP3 up to and including the
275** first null byte. If n>0 then copy n+1 bytes of zP3.
276**
277** If n==P3_STATIC it means that zP3 is a pointer to a constant static
278** string and we can just copy the pointer. n==P3_POINTER means zP3 is
drhd3d39e92004-05-20 22:16:29 +0000279** a pointer to some object other than a string. n==P3_COLLSEQ and
280** n==P3_KEYINFO mean that zP3 is a pointer to a CollSeq or KeyInfo
281** structure. A copy is made of KeyInfo structures into memory obtained
282** from sqliteMalloc.
drh9a324642003-09-06 20:12:01 +0000283**
284** If addr<0 then change P3 on the most recently inserted instruction.
285*/
danielk19774adee202004-05-08 08:23:19 +0000286void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000287 Op *pOp;
288 assert( p->magic==VDBE_MAGIC_INIT );
289 if( p==0 || p->aOp==0 ) return;
290 if( addr<0 || addr>=p->nOp ){
291 addr = p->nOp - 1;
292 if( addr<0 ) return;
293 }
294 pOp = &p->aOp[addr];
295 if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
296 sqliteFree(pOp->p3);
297 pOp->p3 = 0;
298 }
299 if( zP3==0 ){
300 pOp->p3 = 0;
301 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000302 }else if( n==P3_KEYINFO ){
303 KeyInfo *pKeyInfo;
304 int nField, nByte;
305 nField = ((KeyInfo*)zP3)->nField;
306 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]);
307 pKeyInfo = sqliteMalloc( nByte );
308 pOp->p3 = (char*)pKeyInfo;
309 if( pKeyInfo ){
310 memcpy(pKeyInfo, zP3, nByte);
311 pOp->p3type = P3_KEYINFO;
312 }else{
313 pOp->p3type = P3_NOTUSED;
314 }
drhffbc3082004-05-21 01:29:06 +0000315 }else if( n==P3_KEYINFO_HANDOFF ){
316 pOp->p3 = (char*)zP3;
317 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000318 }else if( n<0 ){
319 pOp->p3 = (char*)zP3;
320 pOp->p3type = n;
321 }else{
danielk19774adee202004-05-08 08:23:19 +0000322 sqlite3SetNString(&pOp->p3, zP3, n, 0);
drh9a324642003-09-06 20:12:01 +0000323 pOp->p3type = P3_DYNAMIC;
324 }
325}
326
327/*
328** If the P3 operand to the specified instruction appears
329** to be a quoted string token, then this procedure removes
330** the quotes.
331**
332** The quoting operator can be either a grave ascent (ASCII 0x27)
333** or a double quote character (ASCII 0x22). Two quotes in a row
334** resolve to be a single actual quote character within the string.
335*/
danielk19774adee202004-05-08 08:23:19 +0000336void sqlite3VdbeDequoteP3(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000337 Op *pOp;
338 assert( p->magic==VDBE_MAGIC_INIT );
drh51e9a442004-01-16 16:42:53 +0000339 if( p->aOp==0 ) return;
340 if( addr<0 || addr>=p->nOp ){
341 addr = p->nOp - 1;
342 if( addr<0 ) return;
343 }
drh9a324642003-09-06 20:12:01 +0000344 pOp = &p->aOp[addr];
345 if( pOp->p3==0 || pOp->p3[0]==0 ) return;
drhd3d39e92004-05-20 22:16:29 +0000346 if( pOp->p3type==P3_STATIC ){
drh9a324642003-09-06 20:12:01 +0000347 pOp->p3 = sqliteStrDup(pOp->p3);
348 pOp->p3type = P3_DYNAMIC;
349 }
drhd3d39e92004-05-20 22:16:29 +0000350 assert( pOp->p3type==P3_DYNAMIC );
danielk19774adee202004-05-08 08:23:19 +0000351 sqlite3Dequote(pOp->p3);
drh9a324642003-09-06 20:12:01 +0000352}
353
354/*
355** On the P3 argument of the given instruction, change all
356** strings of whitespace characters into a single space and
357** delete leading and trailing whitespace.
358*/
danielk19774adee202004-05-08 08:23:19 +0000359void sqlite3VdbeCompressSpace(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000360 unsigned char *z;
361 int i, j;
362 Op *pOp;
363 assert( p->magic==VDBE_MAGIC_INIT );
364 if( p->aOp==0 || addr<0 || addr>=p->nOp ) return;
365 pOp = &p->aOp[addr];
drhd3d39e92004-05-20 22:16:29 +0000366 if( pOp->p3type==P3_STATIC ){
drh9a324642003-09-06 20:12:01 +0000367 pOp->p3 = sqliteStrDup(pOp->p3);
368 pOp->p3type = P3_DYNAMIC;
369 }
drhd3d39e92004-05-20 22:16:29 +0000370 assert( pOp->p3type==P3_DYNAMIC );
drh9a324642003-09-06 20:12:01 +0000371 z = (unsigned char*)pOp->p3;
372 if( z==0 ) return;
373 i = j = 0;
374 while( isspace(z[i]) ){ i++; }
375 while( z[i] ){
376 if( isspace(z[i]) ){
377 z[j++] = ' ';
378 while( isspace(z[++i]) ){}
379 }else{
380 z[j++] = z[i++];
381 }
382 }
383 while( j>0 && isspace(z[j-1]) ){ j--; }
384 z[j] = 0;
385}
386
drhd3d39e92004-05-20 22:16:29 +0000387#ifndef NDEBUG
388/*
389** Add comment text to the most recently inserted opcode
390*/
391void sqlite3VdbeAddComment(Vdbe *p, const char *zFormat, ...){
392 va_list ap;
393 VdbeOp *pOp;
394 char *zText;
395 va_start(ap, zFormat);
396 zText = sqlite3_vmprintf(zFormat, ap);
397 va_end(ap);
398 pOp = &p->aOp[p->nOp-1];
399 sqliteFree(pOp->zComment);
400 pOp->zComment = zText;
401}
402#endif
403
drh9a324642003-09-06 20:12:01 +0000404/*
danielk197784ac9d02004-05-18 09:58:06 +0000405** Search the current program starting at instruction addr for the given
406** opcode and P2 value. Return the address plus 1 if found and 0 if not
407** found.
drh9a324642003-09-06 20:12:01 +0000408*/
danielk197784ac9d02004-05-18 09:58:06 +0000409int sqlite3VdbeFindOp(Vdbe *p, int addr, int op, int p2){
drh9a324642003-09-06 20:12:01 +0000410 int i;
411 assert( p->magic==VDBE_MAGIC_INIT );
danielk197784ac9d02004-05-18 09:58:06 +0000412 for(i=addr; i<p->nOp; i++){
drh9a324642003-09-06 20:12:01 +0000413 if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1;
414 }
415 return 0;
416}
417
418/*
419** Return the opcode for a given address.
420*/
danielk19774adee202004-05-08 08:23:19 +0000421VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000422 assert( p->magic==VDBE_MAGIC_INIT );
423 assert( addr>=0 && addr<p->nOp );
424 return &p->aOp[addr];
425}
426
427/*
drhd3d39e92004-05-20 22:16:29 +0000428** Compute a string that describes the P3 parameter for an opcode.
429** Use zTemp for any required temporary buffer space.
430*/
431static char *displayP3(Op *pOp, char *zTemp, int nTemp){
432 char *zP3;
433 assert( nTemp>=20 );
434 switch( pOp->p3type ){
435 case P3_POINTER: {
436 sprintf(zTemp, "ptr(%#x)", (int)pOp->p3);
437 zP3 = zTemp;
438 break;
439 }
440 case P3_KEYINFO: {
441 int i, j;
442 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
443 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
444 i = strlen(zTemp);
445 for(j=0; j<pKeyInfo->nField; j++){
446 CollSeq *pColl = pKeyInfo->aColl[j];
447 if( pColl ){
448 int n = strlen(pColl->zName);
449 if( i+n>nTemp-6 ){
450 strcpy(&zTemp[i],",...");
451 break;
452 }
453 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000454 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000455 zTemp[i++] = '-';
456 }
457 strcpy(&zTemp[i], pColl->zName);
458 i += n;
459 }else if( i+4<nTemp-6 ){
460 strcpy(&zTemp[i],",nil");
461 i += 4;
462 }
463 }
464 zTemp[i++] = ')';
465 zTemp[i] = 0;
466 assert( i<nTemp );
467 zP3 = zTemp;
468 break;
469 }
470 case P3_COLLSEQ: {
471 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000472 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000473 zP3 = zTemp;
474 break;
475 }
drhf9b596e2004-05-26 16:54:42 +0000476 case P3_FUNCDEF: {
477 FuncDef *pDef = (FuncDef*)pOp->p3;
478 char zNum[30];
479 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
480 sprintf(zNum,"(%d)", pDef->nArg);
481 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
482 strcat(zTemp, zNum);
483 }
484 zP3 = zTemp;
485 break;
486 }
drhd3d39e92004-05-20 22:16:29 +0000487 default: {
488 zP3 = pOp->p3;
489 if( zP3==0 ){
490 zP3 = "";
491 }
492 }
493 }
494 return zP3;
495}
496
497
drh9a324642003-09-06 20:12:01 +0000498#if !defined(NDEBUG) || defined(VDBE_PROFILE)
499/*
500** Print a single opcode. This routine is used for debugging only.
501*/
danielk19774adee202004-05-08 08:23:19 +0000502void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000503 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000504 char zPtr[50];
505 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
506 static const char *zFormat2 = "%4d %-13s %4d %4d %-20s -- %s\n";
drh9a324642003-09-06 20:12:01 +0000507 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000508 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
509#ifdef NDEBUG
510 fprintf(pOut, zFormat1,
511 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
512#else
513 fprintf(pOut, pOp->zComment ? zFormat2 : zFormat1,
514 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3,pOp->zComment);
515#endif
drh9a324642003-09-06 20:12:01 +0000516 fflush(pOut);
517}
518#endif
519
520/*
521** Give a listing of the program in the virtual machine.
522**
danielk19774adee202004-05-08 08:23:19 +0000523** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000524** running the code, it invokes the callback once for each instruction.
525** This feature is used to implement "EXPLAIN".
526*/
danielk19774adee202004-05-08 08:23:19 +0000527int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000528 Vdbe *p /* The VDBE */
529){
530 sqlite *db = p->db;
531 int i;
drh826fb5a2004-02-14 23:59:57 +0000532 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000533 static char *azColumnNames[] = {
534 "addr", "opcode", "p1", "p2", "p3",
535 "int", "text", "int", "int", "text",
536 0
537 };
538
drh9a324642003-09-06 20:12:01 +0000539 assert( p->explain );
danielk197718f41892004-05-22 07:27:46 +0000540
541 /* Even though this opcode does not put dynamic strings onto the
542 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000543 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000544 */
545 if( p->pTos==&p->aStack[4] ){
546 for(i=0; i<5; i++){
547 if( p->aStack[i].flags & MEM_Dyn ){
548 sqliteFree(p->aStack[i].z);
549 }
550 p->aStack[i].flags = 0;
551 }
552 }
553
drh9a324642003-09-06 20:12:01 +0000554 p->azColName = azColumnNames;
danielk197718f41892004-05-22 07:27:46 +0000555 p->resOnStack = 0;
556
557 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000558 if( i>=p->nOp ){
559 p->rc = SQLITE_OK;
560 rc = SQLITE_DONE;
561 }else if( db->flags & SQLITE_Interrupt ){
562 db->flags &= ~SQLITE_Interrupt;
563 if( db->magic!=SQLITE_MAGIC_BUSY ){
564 p->rc = SQLITE_MISUSE;
565 }else{
566 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000567 }
drh826fb5a2004-02-14 23:59:57 +0000568 rc = SQLITE_ERROR;
danielk1977132872b2004-05-10 10:37:18 +0000569 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000570 }else{
drhd3d39e92004-05-20 22:16:29 +0000571 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000572 Mem *pMem = p->aStack;
573 pMem->flags = MEM_Int;
drhf4479502004-05-27 03:12:53 +0000574 pMem->type = SQLITE3_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000575 pMem->i = i; /* Program counter */
576 pMem++;
577
578 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
579 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
580 pMem->n = strlen(pMem->z);
581 pMem->type = SQLITE3_TEXT;
582 pMem->enc = TEXT_Utf8;
583 pMem++;
584
585 pMem->flags = MEM_Int;
586 pMem->i = pOp->p1; /* P1 */
drhf4479502004-05-27 03:12:53 +0000587 pMem->type = SQLITE3_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000588 pMem++;
589
590 pMem->flags = MEM_Int;
591 pMem->i = pOp->p2; /* P2 */
drhf4479502004-05-27 03:12:53 +0000592 pMem->type = SQLITE3_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000593 pMem++;
594
595 pMem->flags = MEM_Short|MEM_Str|MEM_Term; /* P3 */
596 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drhf4479502004-05-27 03:12:53 +0000597 pMem->type = SQLITE3_TEXT;
drheb2e1762004-05-27 01:53:56 +0000598 pMem->enc = TEXT_Utf8;
599
drh826fb5a2004-02-14 23:59:57 +0000600 p->nResColumn = 5;
drheb2e1762004-05-27 01:53:56 +0000601 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000602 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000603 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000604 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000605 }
drh826fb5a2004-02-14 23:59:57 +0000606 return rc;
drh9a324642003-09-06 20:12:01 +0000607}
608
609/*
610** Prepare a virtual machine for execution. This involves things such
611** as allocating stack space and initializing the program counter.
612** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000613** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000614*/
danielk19774adee202004-05-08 08:23:19 +0000615void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000616 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000617 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000618 int isExplain /* True if the EXPLAIN keywords is present */
619){
620 int n;
621
622 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000623 assert( p->magic==VDBE_MAGIC_INIT );
624
625 /* Add a HALT instruction to the very end of the program.
626 */
627 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000628 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000629 }
630
631 /* No instruction ever pushes more than a single element onto the
632 ** stack. And the stack never grows on successive executions of the
633 ** same loop. So the total number of instructions is an upper bound
634 ** on the maximum stack depth required.
635 **
636 ** Allocation all the stack space we will ever need.
637 */
drh82a48512003-09-06 22:45:20 +0000638 if( p->aStack==0 ){
639 p->nVar = nVar;
640 assert( nVar>=0 );
641 n = isExplain ? 10 : p->nOp;
642 p->aStack = sqliteMalloc(
danielk19776ddcca52004-05-24 23:48:25 +0000643 n*(sizeof(p->aStack[0])+sizeof(Mem*)+sizeof(char*)) /* aStack, apArg */
drh5a12e682004-05-19 11:24:25 +0000644 + p->nVar*sizeof(Mem) /* apVar */
drh82a48512003-09-06 22:45:20 +0000645 );
danielk19776ddcca52004-05-24 23:48:25 +0000646 p->apArg = (Mem **)&p->aStack[n];
647 p->azColName = (char**)&p->apArg[n];
drh5a12e682004-05-19 11:24:25 +0000648 p->apVar = (Mem *)&p->azColName[n];
danielk197754db47e2004-05-19 10:36:43 +0000649 for(n=0; n<p->nVar; n++){
drh5a12e682004-05-19 11:24:25 +0000650 p->apVar[n].flags = MEM_Null;
danielk197754db47e2004-05-19 10:36:43 +0000651 }
drh82a48512003-09-06 22:45:20 +0000652 }
drh9a324642003-09-06 20:12:01 +0000653
danielk19774adee202004-05-08 08:23:19 +0000654 sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
drh9a324642003-09-06 20:12:01 +0000655 p->agg.pSearch = 0;
656#ifdef MEMORY_DEBUG
danielk19774adee202004-05-08 08:23:19 +0000657 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000658 p->trace = stdout;
659 }
660#endif
drh6810ce62004-01-31 19:22:56 +0000661 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000662 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000663 p->rc = SQLITE_OK;
664 p->uniqueCnt = 0;
665 p->returnDepth = 0;
666 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000667 p->popStack = 0;
668 p->explain |= isExplain;
669 p->magic = VDBE_MAGIC_RUN;
670#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000671 {
672 int i;
673 for(i=0; i<p->nOp; i++){
674 p->aOp[i].cnt = 0;
675 p->aOp[i].cycles = 0;
676 }
drh9a324642003-09-06 20:12:01 +0000677 }
678#endif
679}
680
681
682/*
683** Remove any elements that remain on the sorter for the VDBE given.
684*/
danielk19774adee202004-05-08 08:23:19 +0000685void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000686 while( p->pSort ){
687 Sorter *pSorter = p->pSort;
688 p->pSort = pSorter->pNext;
689 sqliteFree(pSorter->zKey);
690 sqliteFree(pSorter->pData);
691 sqliteFree(pSorter);
692 }
693}
694
695/*
drh9a324642003-09-06 20:12:01 +0000696** Reset an Agg structure. Delete all its contents.
697**
698** For installable aggregate functions, if the step function has been
699** called, make sure the finalizer function has also been called. The
700** finalizer might need to free memory that was allocated as part of its
701** private context. If the finalizer has not been called yet, call it
702** now.
703*/
danielk19774adee202004-05-08 08:23:19 +0000704void sqlite3VdbeAggReset(Agg *pAgg){
drh9a324642003-09-06 20:12:01 +0000705 int i;
706 HashElem *p;
707 for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
708 AggElem *pElem = sqliteHashData(p);
709 assert( pAgg->apFunc!=0 );
710 for(i=0; i<pAgg->nMem; i++){
711 Mem *pMem = &pElem->aMem[i];
drh00706be2004-01-30 14:49:16 +0000712 if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk19770ae8b832004-05-25 12:05:56 +0000713 sqlite3_context ctx;
drh9a324642003-09-06 20:12:01 +0000714 ctx.pFunc = pAgg->apFunc[i];
drh00706be2004-01-30 14:49:16 +0000715 ctx.s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000716 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +0000717 ctx.cnt = pMem->i;
drh9a324642003-09-06 20:12:01 +0000718 ctx.isStep = 0;
719 ctx.isError = 0;
720 (*pAgg->apFunc[i]->xFinalize)(&ctx);
drh00706be2004-01-30 14:49:16 +0000721 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
drh9a324642003-09-06 20:12:01 +0000722 sqliteFree(pMem->z);
723 }
drh9cbe7ca2004-02-18 16:57:23 +0000724 if( ctx.s.flags & MEM_Dyn ){
725 sqliteFree(ctx.s.z);
726 }
drh00706be2004-01-30 14:49:16 +0000727 }else if( pMem->flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000728 sqliteFree(pMem->z);
729 }
730 }
731 sqliteFree(pElem);
732 }
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3HashClear(&pAgg->hash);
drh9a324642003-09-06 20:12:01 +0000734 sqliteFree(pAgg->apFunc);
735 pAgg->apFunc = 0;
736 pAgg->pCurrent = 0;
737 pAgg->pSearch = 0;
738 pAgg->nMem = 0;
739}
740
741/*
742** Delete a keylist
743*/
danielk19774adee202004-05-08 08:23:19 +0000744void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000745 while( p ){
746 Keylist *pNext = p->pNext;
747 sqliteFree(p);
748 p = pNext;
749 }
750}
751
752/*
753** Close a cursor and release all the resources that cursor happens
754** to hold.
755*/
danielk19774adee202004-05-08 08:23:19 +0000756void sqlite3VdbeCleanupCursor(Cursor *pCx){
drh9a324642003-09-06 20:12:01 +0000757 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000758 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000759 }
760 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000761 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000762 }
763 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000764 sqliteFree(pCx->aType);
drhd7556d22004-05-14 21:59:40 +0000765 memset(pCx, 0, sizeof(*pCx));
drh9a324642003-09-06 20:12:01 +0000766}
767
768/*
769** Close all cursors
770*/
771static void closeAllCursors(Vdbe *p){
772 int i;
773 for(i=0; i<p->nCursor; i++){
drhd7556d22004-05-14 21:59:40 +0000774 Cursor *pC = p->apCsr[i];
775 sqlite3VdbeCleanupCursor(pC);
776 sqliteFree(pC);
drh9a324642003-09-06 20:12:01 +0000777 }
drhd7556d22004-05-14 21:59:40 +0000778 sqliteFree(p->apCsr);
779 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000780 p->nCursor = 0;
781}
782
783/*
drh9a324642003-09-06 20:12:01 +0000784** Clean up the VM after execution.
785**
786** This routine will automatically close any cursors, lists, and/or
787** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000788** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000789*/
790static void Cleanup(Vdbe *p){
791 int i;
drh6810ce62004-01-31 19:22:56 +0000792 if( p->aStack ){
793 Mem *pTos = p->pTos;
794 while( pTos>=p->aStack ){
795 if( pTos->flags & MEM_Dyn ){
796 sqliteFree(pTos->z);
797 }
798 pTos--;
799 }
800 p->pTos = pTos;
801 }
drh9a324642003-09-06 20:12:01 +0000802 closeAllCursors(p);
803 if( p->aMem ){
804 for(i=0; i<p->nMem; i++){
drh00706be2004-01-30 14:49:16 +0000805 if( p->aMem[i].flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000806 sqliteFree(p->aMem[i].z);
807 }
808 }
809 }
810 sqliteFree(p->aMem);
811 p->aMem = 0;
812 p->nMem = 0;
813 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000814 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000815 p->pList = 0;
816 }
danielk19774adee202004-05-08 08:23:19 +0000817 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000818 if( p->pFile ){
819 if( p->pFile!=stdin ) fclose(p->pFile);
820 p->pFile = 0;
821 }
822 if( p->azField ){
823 sqliteFree(p->azField);
824 p->azField = 0;
825 }
826 p->nField = 0;
827 if( p->zLine ){
828 sqliteFree(p->zLine);
829 p->zLine = 0;
830 }
831 p->nLineAlloc = 0;
danielk19774adee202004-05-08 08:23:19 +0000832 sqlite3VdbeAggReset(&p->agg);
drh9a324642003-09-06 20:12:01 +0000833 if( p->keylistStack ){
834 int ii;
835 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000836 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000837 }
838 sqliteFree(p->keylistStack);
839 p->keylistStackDepth = 0;
840 p->keylistStack = 0;
841 }
drh5f968432004-02-21 19:02:30 +0000842 sqliteFree(p->contextStack);
843 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000844 sqliteFree(p->zErrMsg);
845 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000846}
847
848/*
danielk197722322fd2004-05-25 23:35:17 +0000849** Set the number of result columns that will be returned by this SQL
850** statement. This is now set at compile time, rather than during
851** execution of the vdbe program so that sqlite3_column_count() can
852** be called on an SQL statement before sqlite3_step().
853*/
854void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
danielk19773cf86062004-05-26 10:11:05 +0000855 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000856 p->nResColumn = nResColumn;
857}
858
859/*
danielk19773cf86062004-05-26 10:11:05 +0000860** Set the name of the idx'th column to be returned by the SQL statement.
861** zName must be a pointer to a nul terminated string.
862**
863** This call must be made after a call to sqlite3VdbeSetNumCols().
864**
865** Parameter N may be either P3_DYNAMIC or P3_STATIC.
866*/
867int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
868 int rc;
869 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +0000870 assert( idx<(2*p->nResColumn) );
danielk19773cf86062004-05-26 10:11:05 +0000871
872 /* If the Vdbe.aColName array has not yet been allocated, allocate
873 ** it now.
874 */
875 if( !p->aColName ){
876 int i;
danielk197776d505b2004-05-28 13:13:02 +0000877 p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn*2);
danielk19773cf86062004-05-26 10:11:05 +0000878 if( !p->aColName ){
879 return SQLITE_NOMEM;
880 }
danielk197776d505b2004-05-28 13:13:02 +0000881 for(i=0; i<(2*p->nResColumn); i++){
danielk19773cf86062004-05-26 10:11:05 +0000882 p->aColName[i].flags = MEM_Null;
883 }
884 }
885
886 pColName = &(p->aColName[idx]);
887 if( N==0 ){
drhf4479502004-05-27 03:12:53 +0000888 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, TEXT_Utf8, 1);
danielk19773cf86062004-05-26 10:11:05 +0000889 }else{
drhf4479502004-05-27 03:12:53 +0000890 rc = sqlite3VdbeMemSetStr(pColName, zName, N, TEXT_Utf8, N>0);
danielk19773cf86062004-05-26 10:11:05 +0000891 }
892 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
893 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
894 }
895 return rc;
896}
897
danielk19771d850a72004-05-31 08:26:49 +0000898/*
899** This routine checks that the sqlite3.activeVdbeCnt count variable
900** matches the number of vdbe's in the list sqlite3.pVdbe that are
901** currently active. An assertion fails if the two counts do not match.
902**
903** This is a no-op if NDEBUG is defined.
904*/
905#ifndef NDEBUG
906static void checkActiveVdbeCnt(sqlite *db){
907 Vdbe *p;
908 int cnt = 0;
909
910 p = db->pVdbe;
911 while( p ){
912 if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
913 cnt++;
914 }
915 p = p->pNext;
916 }
917
918 assert( cnt==db->activeVdbeCnt );
919}
920#else
921#define checkActiveVdbeCnt(x)
922#endif
923
danielk19773cf86062004-05-26 10:11:05 +0000924/*
drh9a324642003-09-06 20:12:01 +0000925** Clean up a VDBE after execution but do not delete the VDBE just yet.
926** Write any error messages into *pzErrMsg. Return the result code.
927**
928** After this routine is run, the VDBE should be ready to be executed
929** again.
930*/
danielk19774adee202004-05-08 08:23:19 +0000931int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000932 sqlite *db = p->db;
933 int i;
danielk19771d850a72004-05-31 08:26:49 +0000934 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
935 int needXcommit = 0;
drh9a324642003-09-06 20:12:01 +0000936
937 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000938 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk19771d850a72004-05-31 08:26:49 +0000939 sqlite3Error(p->db, SQLITE_MISUSE, 0 ,0);
940 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +0000941 return SQLITE_MISUSE;
942 }
943 if( p->zErrMsg ){
danielk1977106bb232004-05-21 10:08:53 +0000944 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
drh9a324642003-09-06 20:12:01 +0000945 if( pzErrMsg && *pzErrMsg==0 ){
946 *pzErrMsg = p->zErrMsg;
947 }else{
948 sqliteFree(p->zErrMsg);
949 }
950 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +0000951 }else if( p->rc ){
danielk1977132872b2004-05-10 10:37:18 +0000952 sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0);
danielk19771d850a72004-05-31 08:26:49 +0000953 sqlite3Error(p->db, p->rc, 0);
danielk1977106bb232004-05-21 10:08:53 +0000954 }else{
955 sqlite3Error(p->db, SQLITE_OK, 0);
drh9a324642003-09-06 20:12:01 +0000956 }
957 Cleanup(p);
danielk19771d850a72004-05-31 08:26:49 +0000958
959 /* Figure out which function to call on the btree backends that
960 ** have active transactions.
961 */
962 checkActiveVdbeCnt(db);
963 if( db->autoCommit && db->activeVdbeCnt==1 ){
964 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
965 xFunc = sqlite3BtreeCommit;
966 needXcommit = 1;
967 }else{
968 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +0000969 }
danielk19771d850a72004-05-31 08:26:49 +0000970 }else{
971 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
972 xFunc = sqlite3BtreeCommitStmt;
973 }else if( p->errorAction==OE_Abort ){
974 xFunc = sqlite3BtreeRollbackStmt;
975 }else{
976 xFunc = sqlite3BtreeRollback;
977 db->autoCommit = 1;
978 }
979 }
980
981 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +0000982 int rc;
danielk19771d850a72004-05-31 08:26:49 +0000983 Btree *pBt = db->aDb[i].pBt;
984 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19771d850a72004-05-31 08:26:49 +0000985 if( db->xCommitCallback && needXcommit ){
986 if( db->xCommitCallback(db->pCommitArg)!=0 ){
987 p->rc = SQLITE_CONSTRAINT;
988 sqlite3Error(db, SQLITE_CONSTRAINT, 0);
989 xFunc = sqlite3BtreeRollback;
990 }
991 needXcommit = 0;
992 }
danielk19771d850a72004-05-31 08:26:49 +0000993 }
danielk197777d83ba2004-05-31 10:08:14 +0000994 if( pBt ){
995 rc = xFunc(pBt);
996 if( p->rc==SQLITE_OK ) p->rc = rc;
997 }
danielk19771d850a72004-05-31 08:26:49 +0000998 }
999
1000
1001 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001002 sqlite3RollbackInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001003 }
danielk19771d850a72004-05-31 08:26:49 +00001004
1005 if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
1006 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001007 }
danielk19771d850a72004-05-31 08:26:49 +00001008
1009 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001010#ifdef VDBE_PROFILE
1011 {
1012 FILE *out = fopen("vdbe_profile.out", "a");
1013 if( out ){
1014 int i;
1015 fprintf(out, "---- ");
1016 for(i=0; i<p->nOp; i++){
1017 fprintf(out, "%02x", p->aOp[i].opcode);
1018 }
1019 fprintf(out, "\n");
1020 for(i=0; i<p->nOp; i++){
1021 fprintf(out, "%6d %10lld %8lld ",
1022 p->aOp[i].cnt,
1023 p->aOp[i].cycles,
1024 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1025 );
danielk19774adee202004-05-08 08:23:19 +00001026 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001027 }
1028 fclose(out);
1029 }
1030 }
1031#endif
1032 p->magic = VDBE_MAGIC_INIT;
1033 return p->rc;
1034}
1035
1036/*
1037** Clean up and delete a VDBE after execution. Return an integer which is
1038** the result code. Write any error message text into *pzErrMsg.
1039*/
danielk19774adee202004-05-08 08:23:19 +00001040int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +00001041 int rc;
1042 sqlite *db;
1043
1044 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +00001045 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +00001046 if( p->magic==VDBE_MAGIC_INIT ){
1047 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
1048 }
drh9a324642003-09-06 20:12:01 +00001049 return SQLITE_MISUSE;
1050 }
1051 db = p->db;
danielk19774adee202004-05-08 08:23:19 +00001052 rc = sqlite3VdbeReset(p, pzErrMsg);
1053 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001054 if( db->want_to_close && db->pVdbe==0 ){
danielk1977132872b2004-05-10 10:37:18 +00001055 sqlite3_close(db);
drh9a324642003-09-06 20:12:01 +00001056 }
drha1f9b5e2004-02-14 16:31:02 +00001057 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001059 }
drh9a324642003-09-06 20:12:01 +00001060 return rc;
1061}
1062
1063/*
drh9a324642003-09-06 20:12:01 +00001064** Delete an entire VDBE.
1065*/
danielk19774adee202004-05-08 08:23:19 +00001066void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001067 int i;
1068 if( p==0 ) return;
1069 Cleanup(p);
1070 if( p->pPrev ){
1071 p->pPrev->pNext = p->pNext;
1072 }else{
1073 assert( p->db->pVdbe==p );
1074 p->db->pVdbe = p->pNext;
1075 }
1076 if( p->pNext ){
1077 p->pNext->pPrev = p->pPrev;
1078 }
1079 p->pPrev = p->pNext = 0;
1080 if( p->nOpAlloc==0 ){
1081 p->aOp = 0;
1082 p->nOp = 0;
1083 }
1084 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001085 Op *pOp = &p->aOp[i];
1086 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1087 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001088 }
drhd3d39e92004-05-20 22:16:29 +00001089#ifndef NDEBUG
1090 sqliteFree(pOp->zComment);
1091#endif
drh9a324642003-09-06 20:12:01 +00001092 }
drh7c972de2003-09-06 22:18:07 +00001093 for(i=0; i<p->nVar; i++){
drh5a12e682004-05-19 11:24:25 +00001094 if( p->apVar[i].flags&MEM_Dyn ){
1095 sqliteFree(p->apVar[i].z);
danielk197754db47e2004-05-19 10:36:43 +00001096 }
drh7c972de2003-09-06 22:18:07 +00001097 }
danielk1977ca6b2912004-05-21 10:49:47 +00001098 if( p->azColName16 ){
1099 for(i=0; i<p->nResColumn; i++){
1100 if( p->azColName16[i] ) sqliteFree(p->azColName16[i]);
1101 }
1102 sqliteFree(p->azColName16);
1103 }
drh9a324642003-09-06 20:12:01 +00001104 sqliteFree(p->aOp);
1105 sqliteFree(p->aLabel);
1106 sqliteFree(p->aStack);
1107 p->magic = VDBE_MAGIC_DEAD;
1108 sqliteFree(p);
1109}
drha11846b2004-01-07 18:52:56 +00001110
1111/*
drha11846b2004-01-07 18:52:56 +00001112** If a MoveTo operation is pending on the given cursor, then do that
1113** MoveTo now. Return an error code. If no MoveTo is pending, this
1114** routine does nothing and returns SQLITE_OK.
1115*/
danielk19774adee202004-05-08 08:23:19 +00001116int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001117 if( p->deferredMoveto ){
1118 int res;
danielk1977132872b2004-05-10 10:37:18 +00001119 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001120 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001121 if( p->intKey ){
1122 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1123 }else{
1124 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1125 }
drhd3d39e92004-05-20 22:16:29 +00001126 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001127 p->lastRecno = keyToInt(p->movetoTarget);
1128 p->recnoIsValid = res==0;
1129 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001130 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001131 }
danielk1977132872b2004-05-10 10:37:18 +00001132 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001133 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001134 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001135 }
1136 return SQLITE_OK;
1137}
danielk19774adee202004-05-08 08:23:19 +00001138
drhab9f7f12004-05-08 10:56:11 +00001139/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001140** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001141**
danielk1977cfcdaef2004-05-12 07:33:33 +00001142** sqlite3VdbeSerialType()
1143** sqlite3VdbeSerialTypeLen()
1144** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001145** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001146** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001147**
1148** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001149** data and index records. Each serialized value consists of a
1150** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1151** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001152**
danielk1977cfcdaef2004-05-12 07:33:33 +00001153** In an SQLite index record, the serial type is stored directly before
1154** the blob of data that it corresponds to. In a table record, all serial
1155** types are stored at the start of the record, and the blobs of data at
1156** the end. Hence these functions allow the caller to handle the
1157** serial-type and data blob seperately.
1158**
1159** The following table describes the various storage classes for data:
1160**
1161** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001162** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001163** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001164** 1 1 signed integer
1165** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001166** 3 3 signed integer
1167** 4 4 signed integer
1168** 5 6 signed integer
1169** 6 8 signed integer
1170** 7 8 IEEE float
1171** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001172** N>=12 and even (N-12)/2 BLOB
1173** N>=13 and odd (N-13)/2 text
1174**
1175*/
1176
1177/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001178** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001179*/
drh25aa1b42004-05-28 01:39:01 +00001180u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001181 int flags = pMem->flags;
1182
1183 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001184 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001185 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001186 if( flags&MEM_Int ){
1187 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1188 i64 i = pMem->i;
1189 if( i>=-127 && i<=127 ) return 1;
1190 if( i>=-32767 && i<=32767 ) return 2;
drha19b7752004-05-30 21:14:58 +00001191 if( i>=-8388607 && i<=8388607 ) return 3;
1192 if( i>=-2147483647 && i<=2147483647 ) return 4;
1193 if( i>=-140737488355328L && i<=140737488355328L ) return 5;
1194 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001195 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001196 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001197 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001198 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001199 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001200 int n = pMem->n;
1201 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001202 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001203 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001204 if( flags&MEM_Blob ){
1205 return (pMem->n*2 + 12);
1206 }
1207 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001208}
1209
1210/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001211** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001212*/
drh25aa1b42004-05-28 01:39:01 +00001213int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001214 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001215 return (serial_type-12)/2;
1216 }else{
drha19b7752004-05-30 21:14:58 +00001217 static u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001218 return aSize[serial_type];
1219 }
danielk1977192ac1d2004-05-10 07:17:30 +00001220}
1221
1222/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001223** Write the serialized data blob for the value stored in pMem into
1224** buf. It is assumed that the caller has allocated sufficient space.
1225** Return the number of bytes written.
1226*/
danielk1977b1bc9532004-05-22 03:05:33 +00001227int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001228 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001229 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001230
danielk1977cfcdaef2004-05-12 07:33:33 +00001231 /* NULL */
drha19b7752004-05-30 21:14:58 +00001232 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001233 return 0;
1234 }
1235
drh1483e142004-05-21 21:12:42 +00001236 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001237 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001238 u64 v;
1239 int i;
drha19b7752004-05-30 21:14:58 +00001240 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001241 v = *(u64*)&pMem->r;
1242 }else{
1243 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001244 }
drh1483e142004-05-21 21:12:42 +00001245 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1246 while( i-- ){
1247 buf[i] = (v&0xFF);
1248 v >>= 8;
1249 }
1250 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001251 }
1252
1253 /* String or blob */
1254 assert( serial_type>=12 );
1255 len = sqlite3VdbeSerialTypeLen(serial_type);
1256 memcpy(buf, pMem->z, len);
1257 return len;
1258}
1259
1260/*
1261** Deserialize the data blob pointed to by buf as serial type serial_type
1262** and store the result in pMem. Return the number of bytes read.
1263*/
danielk1977b1bc9532004-05-22 03:05:33 +00001264int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001265 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001266 u32 serial_type, /* Serial type to deserialize */
1267 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001268){
danielk197790e4d952004-05-10 10:05:53 +00001269 int len;
1270
drha19b7752004-05-30 21:14:58 +00001271 if( serial_type==0 ){
1272 /* NULL */
1273 pMem->flags = MEM_Null;
1274 return 0;
1275 }
drh696b32f2004-05-30 01:51:52 +00001276 len = sqlite3VdbeSerialTypeLen(serial_type);
drha19b7752004-05-30 21:14:58 +00001277 if( serial_type<=7 ){
drh696b32f2004-05-30 01:51:52 +00001278 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001279 if( serial_type<=4 ){
drhe51c44f2004-05-30 20:46:09 +00001280 /* 32-bit integer type. This is handled by a special case for
1281 ** performance reasons. */
1282 int v = buf[0];
1283 int n;
1284 if( v&0x80 ){
1285 v |= -256;
1286 }
1287 for(n=1; n<len; n++){
1288 v = (v<<8) | buf[n];
1289 }
drh1483e142004-05-21 21:12:42 +00001290 pMem->flags = MEM_Int;
drhe51c44f2004-05-30 20:46:09 +00001291 pMem->i = v;
1292 return n;
1293 }else{
1294 u64 v = 0;
1295 int n;
1296
1297 if( buf[0]&0x80 ){
1298 v = -1;
1299 }
1300 for(n=0; n<len; n++){
1301 v = (v<<8) | buf[n];
1302 }
drha19b7752004-05-30 21:14:58 +00001303 if( serial_type==7 ){
drhe51c44f2004-05-30 20:46:09 +00001304 pMem->flags = MEM_Real;
1305 pMem->r = *(double*)&v;
1306 }else{
1307 pMem->flags = MEM_Int;
1308 pMem->i = *(i64*)&v;
1309 }
drh1483e142004-05-21 21:12:42 +00001310 }
drha19b7752004-05-30 21:14:58 +00001311 }else{
drh696b32f2004-05-30 01:51:52 +00001312 /* String or blob */
drha19b7752004-05-30 21:14:58 +00001313 assert( serial_type>=12 );
drh696b32f2004-05-30 01:51:52 +00001314 pMem->z = (char *)buf;
1315 pMem->n = len;
1316 if( serial_type&0x01 ){
1317 pMem->flags = MEM_Str | MEM_Ephem;
1318 }else{
1319 pMem->flags = MEM_Blob | MEM_Ephem;
1320 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001321 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001322 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001323}
1324
1325/*
drhab9f7f12004-05-08 10:56:11 +00001326** The following is the comparison function for (non-integer)
1327** keys in the btrees. This function returns negative, zero, or
1328** positive if the first key is less than, equal to, or greater than
1329** the second.
1330**
danielk19778d059842004-05-12 11:24:02 +00001331** This function assumes that each key consists of one or more type/blob
danielk1977183f9f72004-05-13 05:20:26 +00001332** pairs, encoded using the sqlite3VdbeSerialXXX() functions above.
1333**
1334** Following the type/blob pairs, each key may have a single 0x00 byte
1335** followed by a varint. A key may only have this traling 0x00/varint
1336** pair if it has at least as many type/blob pairs as the key it is being
1337** compared to.
drhab9f7f12004-05-08 10:56:11 +00001338*/
drhab9f7f12004-05-08 10:56:11 +00001339int sqlite3VdbeKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001340 void *userData,
danielk1977183f9f72004-05-13 05:20:26 +00001341 int nKey1, const void *pKey1,
1342 int nKey2, const void *pKey2
drhab9f7f12004-05-08 10:56:11 +00001343){
drhd5788202004-05-28 08:21:05 +00001344 return sqlite3VdbeRowCompare(userData,nKey1,pKey1,nKey2,pKey2);
drhab9f7f12004-05-08 10:56:11 +00001345}
danielk1977183f9f72004-05-13 05:20:26 +00001346
1347/*
danielk1977eb015e02004-05-18 01:31:14 +00001348** This function compares the two table row records specified by
1349** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1350** or positive integer if {nKey1, pKey1} is less than, equal to or
1351** greater than {nKey2, pKey2}.
1352**
danielk197784ac9d02004-05-18 09:58:06 +00001353** This function is pretty inefficient and will probably be replaced
danielk1977eb015e02004-05-18 01:31:14 +00001354** by something else in the near future. It is currently required
1355** by compound SELECT operators.
1356*/
1357int sqlite3VdbeRowCompare(
1358 void *userData,
1359 int nKey1, const void *pKey1,
1360 int nKey2, const void *pKey2
1361){
drhd3d39e92004-05-20 22:16:29 +00001362 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001363 u32 d1, d2; /* Offset into aKey[] of next data element */
1364 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1365 u32 szHdr1, szHdr2; /* Number of bytes in header */
1366 int i = 0;
1367 int nField;
1368 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001369 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1370 const unsigned char *aKey2 = (const unsigned char *)pKey2;
drhd3194f52004-05-27 19:59:32 +00001371
1372 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1373 d1 = szHdr1;
1374 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1375 d2 = szHdr2;
1376 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001377 while( idx1<szHdr1 && idx2<szHdr2 ){
danielk197784ac9d02004-05-18 09:58:06 +00001378 Mem mem1;
1379 Mem mem2;
drhd3194f52004-05-27 19:59:32 +00001380 u32 serial_type1;
1381 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001382
1383 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001384 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001385 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001386 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001387 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001388
1389 /* Assert that there is enough space left in each key for the blob of
1390 ** data to go with the serial type just read. This assert may fail if
1391 ** the file is corrupted. Then read the value from each key into mem1
1392 ** and mem2 respectively.
1393 */
drh25aa1b42004-05-28 01:39:01 +00001394 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1395 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001396
drhd5788202004-05-28 08:21:05 +00001397 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
danielk197784ac9d02004-05-18 09:58:06 +00001398 if( mem1.flags&MEM_Dyn ){
1399 sqliteFree(mem1.z);
1400 }
1401 if( mem2.flags&MEM_Dyn ){
1402 sqliteFree(mem2.z);
1403 }
1404 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001405 break;
1406 }
1407 i++;
1408 }
1409
1410 /* One of the keys ran out of fields, but all the fields up to that point
1411 ** were equal. If the incrKey flag is true, then the second key is
1412 ** treated as larger.
1413 */
1414 if( rc==0 ){
1415 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001416 rc = -1;
1417 }else if( d1<nKey1 ){
1418 rc = 1;
1419 }else if( d2<nKey2 ){
1420 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001421 }
1422 }
1423
drhd3194f52004-05-27 19:59:32 +00001424 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1425 rc = -rc;
1426 }
1427
1428 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001429}
drhd5788202004-05-28 08:21:05 +00001430
1431/*
1432** The argument is an index key that contains the ROWID at the end.
1433** Return the length of the rowid.
1434*/
1435int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1436 u32 szHdr; /* Size of the header */
1437 u32 typeRowid; /* Serial type of the rowid */
1438
1439 sqlite3GetVarint32(aKey, &szHdr);
1440 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1441 return sqlite3VdbeSerialTypeLen(typeRowid);
1442}
danielk1977eb015e02004-05-18 01:31:14 +00001443
1444
1445/*
danielk1977183f9f72004-05-13 05:20:26 +00001446** pCur points at an index entry. Read the rowid (varint occuring at
1447** the end of the entry and store it in *rowid. Return SQLITE_OK if
1448** everything works, or an error code otherwise.
1449*/
1450int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drhd5788202004-05-28 08:21:05 +00001451 u64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001452 int rc;
drhd5788202004-05-28 08:21:05 +00001453 u32 szHdr; /* Size of the header */
1454 u32 typeRowid; /* Serial type of the rowid */
1455 u32 lenRowid; /* Size of the rowid */
1456 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001457
drhd5788202004-05-28 08:21:05 +00001458 sqlite3BtreeKeySize(pCur, &nCellKey);
1459 if( nCellKey<=0 ){
1460 return SQLITE_CORRUPT;
1461 }
1462 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1463 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001464 return rc;
1465 }
drhd5788202004-05-28 08:21:05 +00001466 sqlite3GetVarint32(m.z, &szHdr);
1467 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1468 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1469 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1470 *rowid = v.i;
1471 if( m.flags & MEM_Dyn ){
1472 sqliteFree(m.z);
danielk19773d1bfea2004-05-14 11:00:53 +00001473 }
danielk1977183f9f72004-05-13 05:20:26 +00001474 return SQLITE_OK;
1475}
1476
drh7cf6e4d2004-05-19 14:56:55 +00001477/*
drhd3d39e92004-05-20 22:16:29 +00001478** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001479** the key string in pKey (of length nKey). Write into *pRes a number
1480** that is negative, zero, or positive if pC is less than, equal to,
1481** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001482**
drhd5788202004-05-28 08:21:05 +00001483** pKey is either created without a rowid or is truncated so that it
1484** omits the rowid at the end. The rowid at the end of the index entry
1485** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001486*/
danielk1977183f9f72004-05-13 05:20:26 +00001487int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001488 Cursor *pC, /* The cursor to compare against */
1489 int nKey, const u8 *pKey, /* The key to compare */
1490 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001491){
danielk1977183f9f72004-05-13 05:20:26 +00001492 u64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001493 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001494 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001495 int lenRowid;
1496 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001497
1498 sqlite3BtreeKeySize(pCur, &nCellKey);
1499 if( nCellKey<=0 ){
1500 *res = 0;
1501 return SQLITE_OK;
1502 }
drhd5788202004-05-28 08:21:05 +00001503 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1504 if( rc ){
1505 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001506 }
drhd5788202004-05-28 08:21:05 +00001507 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
1508 *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
1509 if( m.flags & MEM_Dyn ){
1510 sqliteFree(m.z);
danielk1977183f9f72004-05-13 05:20:26 +00001511 }
1512 return SQLITE_OK;
1513}