blob: 0873dd0ae1ccc38a4ca0a7cf83c131bcbd4023d2 [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/*
danielk1977c572ef72004-05-27 09:28:41 +0000610** If pOp is an OP_HexBlob opcode, then transform it to an OP_Blob
611** opcode.
612*/
613static int translateOp(Op *pOp){
614 if( pOp->opcode==OP_HexBlob ){
danielk1977c572ef72004-05-27 09:28:41 +0000615 pOp->p1 = strlen(pOp->p3)/2;
danielk19773fd0a732004-05-27 13:35:19 +0000616 if( pOp->p1 ){
617 char *zBlob = sqlite3HexToBlob(pOp->p3);
618 if( !zBlob ) return SQLITE_NOMEM;
619 if( pOp->p3type==P3_DYNAMIC ){
620 sqliteFree(pOp->p3);
621 }
622 pOp->p3 = zBlob;
623 pOp->p3type = P3_DYNAMIC;
624 }else{
625 pOp->p3type = P3_STATIC;
626 pOp->p3 = "";
danielk1977c572ef72004-05-27 09:28:41 +0000627 }
danielk19773fd0a732004-05-27 13:35:19 +0000628 pOp->opcode = OP_Blob;
danielk1977c572ef72004-05-27 09:28:41 +0000629 }
danielk19773fd0a732004-05-27 13:35:19 +0000630 return SQLITE_OK;
danielk1977c572ef72004-05-27 09:28:41 +0000631}
632
633/*
drh9a324642003-09-06 20:12:01 +0000634** Prepare a virtual machine for execution. This involves things such
635** as allocating stack space and initializing the program counter.
636** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000637** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000638*/
danielk19774adee202004-05-08 08:23:19 +0000639void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000640 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000641 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000642 int isExplain /* True if the EXPLAIN keywords is present */
643){
644 int n;
645
646 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000647 assert( p->magic==VDBE_MAGIC_INIT );
648
649 /* Add a HALT instruction to the very end of the program.
650 */
651 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000652 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000653 }
654
655 /* No instruction ever pushes more than a single element onto the
656 ** stack. And the stack never grows on successive executions of the
657 ** same loop. So the total number of instructions is an upper bound
658 ** on the maximum stack depth required.
659 **
660 ** Allocation all the stack space we will ever need.
661 */
drh82a48512003-09-06 22:45:20 +0000662 if( p->aStack==0 ){
663 p->nVar = nVar;
664 assert( nVar>=0 );
665 n = isExplain ? 10 : p->nOp;
666 p->aStack = sqliteMalloc(
danielk19776ddcca52004-05-24 23:48:25 +0000667 n*(sizeof(p->aStack[0])+sizeof(Mem*)+sizeof(char*)) /* aStack, apArg */
drh5a12e682004-05-19 11:24:25 +0000668 + p->nVar*sizeof(Mem) /* apVar */
drh82a48512003-09-06 22:45:20 +0000669 );
danielk19776ddcca52004-05-24 23:48:25 +0000670 p->apArg = (Mem **)&p->aStack[n];
671 p->azColName = (char**)&p->apArg[n];
drh5a12e682004-05-19 11:24:25 +0000672 p->apVar = (Mem *)&p->azColName[n];
danielk197754db47e2004-05-19 10:36:43 +0000673 for(n=0; n<p->nVar; n++){
drh5a12e682004-05-19 11:24:25 +0000674 p->apVar[n].flags = MEM_Null;
danielk197754db47e2004-05-19 10:36:43 +0000675 }
drh82a48512003-09-06 22:45:20 +0000676 }
drh9a324642003-09-06 20:12:01 +0000677
danielk19774adee202004-05-08 08:23:19 +0000678 sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
drh9a324642003-09-06 20:12:01 +0000679 p->agg.pSearch = 0;
680#ifdef MEMORY_DEBUG
danielk19774adee202004-05-08 08:23:19 +0000681 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000682 p->trace = stdout;
683 }
684#endif
drh6810ce62004-01-31 19:22:56 +0000685 p->pTos = &p->aStack[-1];
drh9a324642003-09-06 20:12:01 +0000686 p->pc = 0;
687 p->rc = SQLITE_OK;
688 p->uniqueCnt = 0;
689 p->returnDepth = 0;
690 p->errorAction = OE_Abort;
691 p->undoTransOnError = 0;
drh9a324642003-09-06 20:12:01 +0000692 p->popStack = 0;
693 p->explain |= isExplain;
694 p->magic = VDBE_MAGIC_RUN;
695#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000696 {
697 int i;
698 for(i=0; i<p->nOp; i++){
699 p->aOp[i].cnt = 0;
700 p->aOp[i].cycles = 0;
701 }
drh9a324642003-09-06 20:12:01 +0000702 }
703#endif
danielk19773fd0a732004-05-27 13:35:19 +0000704 if( !isExplain ){
danielk1977c572ef72004-05-27 09:28:41 +0000705 int i;
706 for(i=0; i<p->nOp; i++){
707 translateOp(&p->aOp[i]);
708 }
709 }
drh9a324642003-09-06 20:12:01 +0000710}
711
712
713/*
714** Remove any elements that remain on the sorter for the VDBE given.
715*/
danielk19774adee202004-05-08 08:23:19 +0000716void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000717 while( p->pSort ){
718 Sorter *pSorter = p->pSort;
719 p->pSort = pSorter->pNext;
720 sqliteFree(pSorter->zKey);
721 sqliteFree(pSorter->pData);
722 sqliteFree(pSorter);
723 }
724}
725
726/*
drh9a324642003-09-06 20:12:01 +0000727** Reset an Agg structure. Delete all its contents.
728**
729** For installable aggregate functions, if the step function has been
730** called, make sure the finalizer function has also been called. The
731** finalizer might need to free memory that was allocated as part of its
732** private context. If the finalizer has not been called yet, call it
733** now.
734*/
danielk19774adee202004-05-08 08:23:19 +0000735void sqlite3VdbeAggReset(Agg *pAgg){
drh9a324642003-09-06 20:12:01 +0000736 int i;
737 HashElem *p;
738 for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
739 AggElem *pElem = sqliteHashData(p);
740 assert( pAgg->apFunc!=0 );
741 for(i=0; i<pAgg->nMem; i++){
742 Mem *pMem = &pElem->aMem[i];
drh00706be2004-01-30 14:49:16 +0000743 if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk19770ae8b832004-05-25 12:05:56 +0000744 sqlite3_context ctx;
drh9a324642003-09-06 20:12:01 +0000745 ctx.pFunc = pAgg->apFunc[i];
drh00706be2004-01-30 14:49:16 +0000746 ctx.s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000747 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +0000748 ctx.cnt = pMem->i;
drh9a324642003-09-06 20:12:01 +0000749 ctx.isStep = 0;
750 ctx.isError = 0;
751 (*pAgg->apFunc[i]->xFinalize)(&ctx);
drh00706be2004-01-30 14:49:16 +0000752 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
drh9a324642003-09-06 20:12:01 +0000753 sqliteFree(pMem->z);
754 }
drh9cbe7ca2004-02-18 16:57:23 +0000755 if( ctx.s.flags & MEM_Dyn ){
756 sqliteFree(ctx.s.z);
757 }
drh00706be2004-01-30 14:49:16 +0000758 }else if( pMem->flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000759 sqliteFree(pMem->z);
760 }
761 }
762 sqliteFree(pElem);
763 }
danielk19774adee202004-05-08 08:23:19 +0000764 sqlite3HashClear(&pAgg->hash);
drh9a324642003-09-06 20:12:01 +0000765 sqliteFree(pAgg->apFunc);
766 pAgg->apFunc = 0;
767 pAgg->pCurrent = 0;
768 pAgg->pSearch = 0;
769 pAgg->nMem = 0;
770}
771
772/*
773** Delete a keylist
774*/
danielk19774adee202004-05-08 08:23:19 +0000775void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000776 while( p ){
777 Keylist *pNext = p->pNext;
778 sqliteFree(p);
779 p = pNext;
780 }
781}
782
783/*
784** Close a cursor and release all the resources that cursor happens
785** to hold.
786*/
danielk19774adee202004-05-08 08:23:19 +0000787void sqlite3VdbeCleanupCursor(Cursor *pCx){
drh9a324642003-09-06 20:12:01 +0000788 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000789 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000790 }
791 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000792 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000793 }
794 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000795 sqliteFree(pCx->aType);
drhd7556d22004-05-14 21:59:40 +0000796 memset(pCx, 0, sizeof(*pCx));
drh9a324642003-09-06 20:12:01 +0000797}
798
799/*
800** Close all cursors
801*/
802static void closeAllCursors(Vdbe *p){
803 int i;
804 for(i=0; i<p->nCursor; i++){
drhd7556d22004-05-14 21:59:40 +0000805 Cursor *pC = p->apCsr[i];
806 sqlite3VdbeCleanupCursor(pC);
807 sqliteFree(pC);
drh9a324642003-09-06 20:12:01 +0000808 }
drhd7556d22004-05-14 21:59:40 +0000809 sqliteFree(p->apCsr);
810 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000811 p->nCursor = 0;
812}
813
814/*
drh9a324642003-09-06 20:12:01 +0000815** Clean up the VM after execution.
816**
817** This routine will automatically close any cursors, lists, and/or
818** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000819** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000820*/
821static void Cleanup(Vdbe *p){
822 int i;
drh6810ce62004-01-31 19:22:56 +0000823 if( p->aStack ){
824 Mem *pTos = p->pTos;
825 while( pTos>=p->aStack ){
826 if( pTos->flags & MEM_Dyn ){
827 sqliteFree(pTos->z);
828 }
829 pTos--;
830 }
831 p->pTos = pTos;
832 }
drh9a324642003-09-06 20:12:01 +0000833 closeAllCursors(p);
834 if( p->aMem ){
835 for(i=0; i<p->nMem; i++){
drh00706be2004-01-30 14:49:16 +0000836 if( p->aMem[i].flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000837 sqliteFree(p->aMem[i].z);
838 }
839 }
840 }
841 sqliteFree(p->aMem);
842 p->aMem = 0;
843 p->nMem = 0;
844 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000845 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000846 p->pList = 0;
847 }
danielk19774adee202004-05-08 08:23:19 +0000848 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000849 if( p->pFile ){
850 if( p->pFile!=stdin ) fclose(p->pFile);
851 p->pFile = 0;
852 }
853 if( p->azField ){
854 sqliteFree(p->azField);
855 p->azField = 0;
856 }
857 p->nField = 0;
858 if( p->zLine ){
859 sqliteFree(p->zLine);
860 p->zLine = 0;
861 }
862 p->nLineAlloc = 0;
danielk19774adee202004-05-08 08:23:19 +0000863 sqlite3VdbeAggReset(&p->agg);
drh9a324642003-09-06 20:12:01 +0000864 if( p->keylistStack ){
865 int ii;
866 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000867 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000868 }
869 sqliteFree(p->keylistStack);
870 p->keylistStackDepth = 0;
871 p->keylistStack = 0;
872 }
drh5f968432004-02-21 19:02:30 +0000873 sqliteFree(p->contextStack);
874 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000875 sqliteFree(p->zErrMsg);
876 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000877}
878
879/*
danielk197722322fd2004-05-25 23:35:17 +0000880** Set the number of result columns that will be returned by this SQL
881** statement. This is now set at compile time, rather than during
882** execution of the vdbe program so that sqlite3_column_count() can
883** be called on an SQL statement before sqlite3_step().
884*/
885void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
danielk19773cf86062004-05-26 10:11:05 +0000886 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000887 p->nResColumn = nResColumn;
888}
889
890/*
danielk19773cf86062004-05-26 10:11:05 +0000891** Set the name of the idx'th column to be returned by the SQL statement.
892** zName must be a pointer to a nul terminated string.
893**
894** This call must be made after a call to sqlite3VdbeSetNumCols().
895**
896** Parameter N may be either P3_DYNAMIC or P3_STATIC.
897*/
898int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
899 int rc;
900 Mem *pColName;
901 assert( idx<p->nResColumn );
902
903 /* If the Vdbe.aColName array has not yet been allocated, allocate
904 ** it now.
905 */
906 if( !p->aColName ){
907 int i;
908 p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn);
909 if( !p->aColName ){
910 return SQLITE_NOMEM;
911 }
912 for(i=0; i<p->nResColumn; i++){
913 p->aColName[i].flags = MEM_Null;
914 }
915 }
916
917 pColName = &(p->aColName[idx]);
918 if( N==0 ){
drhf4479502004-05-27 03:12:53 +0000919 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, TEXT_Utf8, 1);
danielk19773cf86062004-05-26 10:11:05 +0000920 }else{
drhf4479502004-05-27 03:12:53 +0000921 rc = sqlite3VdbeMemSetStr(pColName, zName, N, TEXT_Utf8, N>0);
danielk19773cf86062004-05-26 10:11:05 +0000922 }
923 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
924 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
925 }
926 return rc;
927}
928
929/*
drh9a324642003-09-06 20:12:01 +0000930** Clean up a VDBE after execution but do not delete the VDBE just yet.
931** Write any error messages into *pzErrMsg. Return the result code.
932**
933** After this routine is run, the VDBE should be ready to be executed
934** again.
935*/
danielk19774adee202004-05-08 08:23:19 +0000936int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000937 sqlite *db = p->db;
938 int i;
939
940 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000941 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000942 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
drh9a324642003-09-06 20:12:01 +0000943 return SQLITE_MISUSE;
944 }
945 if( p->zErrMsg ){
danielk1977106bb232004-05-21 10:08:53 +0000946 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
drh9a324642003-09-06 20:12:01 +0000947 if( pzErrMsg && *pzErrMsg==0 ){
948 *pzErrMsg = p->zErrMsg;
949 }else{
950 sqliteFree(p->zErrMsg);
951 }
952 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +0000953 }else if( p->rc ){
danielk1977132872b2004-05-10 10:37:18 +0000954 sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000955 sqlite3Error(p->db, p->rc, "%s", sqlite3_error_string(p->rc) , 0);
956 }else{
957 sqlite3Error(p->db, SQLITE_OK, 0);
drh9a324642003-09-06 20:12:01 +0000958 }
959 Cleanup(p);
960 if( p->rc!=SQLITE_OK ){
961 switch( p->errorAction ){
962 case OE_Abort: {
963 if( !p->undoTransOnError ){
964 for(i=0; i<db->nDb; i++){
965 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000966 sqlite3BtreeRollbackStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000967 }
968 }
969 break;
970 }
971 /* Fall through to ROLLBACK */
972 }
973 case OE_Rollback: {
danielk19774adee202004-05-08 08:23:19 +0000974 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000975 db->flags &= ~SQLITE_InTrans;
976 db->onError = OE_Default;
977 break;
978 }
979 default: {
980 if( p->undoTransOnError ){
danielk19774adee202004-05-08 08:23:19 +0000981 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000982 db->flags &= ~SQLITE_InTrans;
983 db->onError = OE_Default;
984 }
985 break;
986 }
987 }
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3RollbackInternalChanges(db);
drh9a324642003-09-06 20:12:01 +0000989 }
990 for(i=0; i<db->nDb; i++){
991 if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){
danielk19774adee202004-05-08 08:23:19 +0000992 sqlite3BtreeCommitStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000993 db->aDb[i].inTrans = 1;
994 }
995 }
danielk1977132872b2004-05-10 10:37:18 +0000996 assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +0000997#ifdef VDBE_PROFILE
998 {
999 FILE *out = fopen("vdbe_profile.out", "a");
1000 if( out ){
1001 int i;
1002 fprintf(out, "---- ");
1003 for(i=0; i<p->nOp; i++){
1004 fprintf(out, "%02x", p->aOp[i].opcode);
1005 }
1006 fprintf(out, "\n");
1007 for(i=0; i<p->nOp; i++){
1008 fprintf(out, "%6d %10lld %8lld ",
1009 p->aOp[i].cnt,
1010 p->aOp[i].cycles,
1011 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1012 );
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001014 }
1015 fclose(out);
1016 }
1017 }
1018#endif
1019 p->magic = VDBE_MAGIC_INIT;
1020 return p->rc;
1021}
1022
1023/*
1024** Clean up and delete a VDBE after execution. Return an integer which is
1025** the result code. Write any error message text into *pzErrMsg.
1026*/
danielk19774adee202004-05-08 08:23:19 +00001027int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +00001028 int rc;
1029 sqlite *db;
1030
1031 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +00001032 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +00001033 if( p->magic==VDBE_MAGIC_INIT ){
1034 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
1035 }
drh9a324642003-09-06 20:12:01 +00001036 return SQLITE_MISUSE;
1037 }
1038 db = p->db;
danielk19774adee202004-05-08 08:23:19 +00001039 rc = sqlite3VdbeReset(p, pzErrMsg);
1040 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001041 if( db->want_to_close && db->pVdbe==0 ){
danielk1977132872b2004-05-10 10:37:18 +00001042 sqlite3_close(db);
drh9a324642003-09-06 20:12:01 +00001043 }
drha1f9b5e2004-02-14 16:31:02 +00001044 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001045 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001046 }
drh9a324642003-09-06 20:12:01 +00001047 return rc;
1048}
1049
1050/*
drh9a324642003-09-06 20:12:01 +00001051** Delete an entire VDBE.
1052*/
danielk19774adee202004-05-08 08:23:19 +00001053void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001054 int i;
1055 if( p==0 ) return;
1056 Cleanup(p);
1057 if( p->pPrev ){
1058 p->pPrev->pNext = p->pNext;
1059 }else{
1060 assert( p->db->pVdbe==p );
1061 p->db->pVdbe = p->pNext;
1062 }
1063 if( p->pNext ){
1064 p->pNext->pPrev = p->pPrev;
1065 }
1066 p->pPrev = p->pNext = 0;
1067 if( p->nOpAlloc==0 ){
1068 p->aOp = 0;
1069 p->nOp = 0;
1070 }
1071 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001072 Op *pOp = &p->aOp[i];
1073 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1074 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001075 }
drhd3d39e92004-05-20 22:16:29 +00001076#ifndef NDEBUG
1077 sqliteFree(pOp->zComment);
1078#endif
drh9a324642003-09-06 20:12:01 +00001079 }
drh7c972de2003-09-06 22:18:07 +00001080 for(i=0; i<p->nVar; i++){
drh5a12e682004-05-19 11:24:25 +00001081 if( p->apVar[i].flags&MEM_Dyn ){
1082 sqliteFree(p->apVar[i].z);
danielk197754db47e2004-05-19 10:36:43 +00001083 }
drh7c972de2003-09-06 22:18:07 +00001084 }
danielk1977ca6b2912004-05-21 10:49:47 +00001085 if( p->azColName16 ){
1086 for(i=0; i<p->nResColumn; i++){
1087 if( p->azColName16[i] ) sqliteFree(p->azColName16[i]);
1088 }
1089 sqliteFree(p->azColName16);
1090 }
drh9a324642003-09-06 20:12:01 +00001091 sqliteFree(p->aOp);
1092 sqliteFree(p->aLabel);
1093 sqliteFree(p->aStack);
1094 p->magic = VDBE_MAGIC_DEAD;
1095 sqliteFree(p);
1096}
drha11846b2004-01-07 18:52:56 +00001097
1098/*
drha11846b2004-01-07 18:52:56 +00001099** If a MoveTo operation is pending on the given cursor, then do that
1100** MoveTo now. Return an error code. If no MoveTo is pending, this
1101** routine does nothing and returns SQLITE_OK.
1102*/
danielk19774adee202004-05-08 08:23:19 +00001103int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001104 if( p->deferredMoveto ){
1105 int res;
danielk1977132872b2004-05-10 10:37:18 +00001106 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001107 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001108 if( p->intKey ){
1109 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1110 }else{
1111 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1112 }
drhd3d39e92004-05-20 22:16:29 +00001113 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001114 p->lastRecno = keyToInt(p->movetoTarget);
1115 p->recnoIsValid = res==0;
1116 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001117 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001118 }
danielk1977132872b2004-05-10 10:37:18 +00001119 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001120 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001121 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001122 }
1123 return SQLITE_OK;
1124}
danielk19774adee202004-05-08 08:23:19 +00001125
drhab9f7f12004-05-08 10:56:11 +00001126/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001127** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001128**
danielk1977cfcdaef2004-05-12 07:33:33 +00001129** sqlite3VdbeSerialType()
1130** sqlite3VdbeSerialTypeLen()
1131** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001132** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001133** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001134**
1135** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001136** data and index records. Each serialized value consists of a
1137** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1138** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001139**
danielk1977cfcdaef2004-05-12 07:33:33 +00001140** In an SQLite index record, the serial type is stored directly before
1141** the blob of data that it corresponds to. In a table record, all serial
1142** types are stored at the start of the record, and the blobs of data at
1143** the end. Hence these functions allow the caller to handle the
1144** serial-type and data blob seperately.
1145**
1146** The following table describes the various storage classes for data:
1147**
1148** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001149** -------------- --------------- ---------------
danielk1977183f9f72004-05-13 05:20:26 +00001150** 0 - Not a type.
danielk197790e4d952004-05-10 10:05:53 +00001151** 1 1 signed integer
1152** 2 2 signed integer
1153** 3 4 signed integer
1154** 4 8 signed integer
1155** 5 8 IEEE float
danielk1977183f9f72004-05-13 05:20:26 +00001156** 6 0 NULL
1157** 7..11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001158** N>=12 and even (N-12)/2 BLOB
1159** N>=13 and odd (N-13)/2 text
1160**
1161*/
1162
1163/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001164** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001165*/
drh25aa1b42004-05-28 01:39:01 +00001166u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001167 int flags = pMem->flags;
1168
1169 if( flags&MEM_Null ){
danielk1977183f9f72004-05-13 05:20:26 +00001170 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001171 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001172 if( flags&MEM_Int ){
1173 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1174 i64 i = pMem->i;
1175 if( i>=-127 && i<=127 ) return 1;
1176 if( i>=-32767 && i<=32767 ) return 2;
1177 if( i>=-2147483647 && i<=2147483647 ) return 3;
1178 return 4;
danielk197790e4d952004-05-10 10:05:53 +00001179 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001180 if( flags&MEM_Real ){
1181 return 5;
danielk197790e4d952004-05-10 10:05:53 +00001182 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001183 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001184 int n = pMem->n;
1185 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001186 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001187 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001188 if( flags&MEM_Blob ){
1189 return (pMem->n*2 + 12);
1190 }
1191 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001192}
1193
1194/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001195** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001196*/
drh25aa1b42004-05-28 01:39:01 +00001197int sqlite3VdbeSerialTypeLen(u32 serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001198 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001199 switch(serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001200 case 6: return 0; /* NULL */
danielk1977cfcdaef2004-05-12 07:33:33 +00001201 case 1: return 1; /* 1 byte integer */
1202 case 2: return 2; /* 2 byte integer */
1203 case 3: return 4; /* 4 byte integer */
1204 case 4: return 8; /* 8 byte integer */
1205 case 5: return 8; /* 8 byte float */
danielk197790e4d952004-05-10 10:05:53 +00001206 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001207 assert( serial_type>=12 );
1208 return ((serial_type-12)>>1); /* text or blob */
danielk1977192ac1d2004-05-10 07:17:30 +00001209}
1210
1211/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001212** Write the serialized data blob for the value stored in pMem into
1213** buf. It is assumed that the caller has allocated sufficient space.
1214** Return the number of bytes written.
1215*/
danielk1977b1bc9532004-05-22 03:05:33 +00001216int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001217 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001218 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001219
1220 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001221
1222 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001223 if( serial_type==6 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001224 return 0;
1225 }
1226
drh1483e142004-05-21 21:12:42 +00001227 /* Integer and Real */
1228 if( serial_type<=5 ){
1229 u64 v;
1230 int i;
1231 if( serial_type==5 ){
1232 v = *(u64*)&pMem->r;
1233 }else{
1234 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001235 }
drh1483e142004-05-21 21:12:42 +00001236 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1237 while( i-- ){
1238 buf[i] = (v&0xFF);
1239 v >>= 8;
1240 }
1241 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001242 }
1243
1244 /* String or blob */
1245 assert( serial_type>=12 );
1246 len = sqlite3VdbeSerialTypeLen(serial_type);
1247 memcpy(buf, pMem->z, len);
1248 return len;
1249}
1250
1251/*
1252** Deserialize the data blob pointed to by buf as serial type serial_type
1253** and store the result in pMem. Return the number of bytes read.
1254*/
danielk1977b1bc9532004-05-22 03:05:33 +00001255int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001256 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001257 u32 serial_type, /* Serial type to deserialize */
1258 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001259){
danielk197790e4d952004-05-10 10:05:53 +00001260 int len;
1261
danielk1977183f9f72004-05-13 05:20:26 +00001262 assert( serial_type!=0 );
1263
danielk1977cfcdaef2004-05-12 07:33:33 +00001264 /* memset(pMem, 0, sizeof(pMem)); */
1265 pMem->flags = 0;
1266 pMem->z = 0;
danielk197790e4d952004-05-10 10:05:53 +00001267
danielk1977cfcdaef2004-05-12 07:33:33 +00001268 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001269 if( serial_type==6 ){
danielk197790e4d952004-05-10 10:05:53 +00001270 pMem->flags = MEM_Null;
drheb2e1762004-05-27 01:53:56 +00001271 pMem->type = SQLITE3_NULL;
danielk1977cfcdaef2004-05-12 07:33:33 +00001272 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001273 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001274
drh1483e142004-05-21 21:12:42 +00001275 /* Integer and Real */
1276 if( serial_type<=5 ){
1277 u64 v = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001278 int n;
1279 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk197790e4d952004-05-10 10:05:53 +00001280
danielk1977cfcdaef2004-05-12 07:33:33 +00001281 if( buf[0]&0x80 ){
drh1483e142004-05-21 21:12:42 +00001282 v = -1;
danielk1977cfcdaef2004-05-12 07:33:33 +00001283 }
1284 for(n=0; n<len; n++){
drh1483e142004-05-21 21:12:42 +00001285 v = (v<<8) | buf[n];
danielk1977cfcdaef2004-05-12 07:33:33 +00001286 }
drh1483e142004-05-21 21:12:42 +00001287 if( serial_type==5 ){
1288 pMem->flags = MEM_Real;
1289 pMem->r = *(double*)&v;
drheb2e1762004-05-27 01:53:56 +00001290 pMem->type = SQLITE3_FLOAT;
drh1483e142004-05-21 21:12:42 +00001291 }else{
1292 pMem->flags = MEM_Int;
danielk1977b1bc9532004-05-22 03:05:33 +00001293 pMem->i = *(i64*)&v;
drheb2e1762004-05-27 01:53:56 +00001294 pMem->type = SQLITE3_INTEGER;
drh1483e142004-05-21 21:12:42 +00001295 }
1296 return len;
danielk197790e4d952004-05-10 10:05:53 +00001297 }
1298
danielk1977cfcdaef2004-05-12 07:33:33 +00001299 /* String or blob */
1300 assert( serial_type>=12 );
danielk1977eb015e02004-05-18 01:31:14 +00001301 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk197761de0d12004-05-27 23:56:16 +00001302 pMem->z = (char *)buf;
danielk1977c572ef72004-05-27 09:28:41 +00001303 pMem->n = len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001304 if( serial_type&0x01 ){
drheb2e1762004-05-27 01:53:56 +00001305 pMem->flags = MEM_Str | MEM_Ephem;
danielk1977cfcdaef2004-05-12 07:33:33 +00001306 }else{
drheb2e1762004-05-27 01:53:56 +00001307 pMem->flags = MEM_Blob | MEM_Ephem;
danielk1977cfcdaef2004-05-12 07:33:33 +00001308 }
drheb2e1762004-05-27 01:53:56 +00001309 sqlite3VdbeMemMakeWriteable(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001310 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001311}
1312
1313/*
drhab9f7f12004-05-08 10:56:11 +00001314** The following is the comparison function for (non-integer)
1315** keys in the btrees. This function returns negative, zero, or
1316** positive if the first key is less than, equal to, or greater than
1317** the second.
1318**
danielk19778d059842004-05-12 11:24:02 +00001319** This function assumes that each key consists of one or more type/blob
danielk1977183f9f72004-05-13 05:20:26 +00001320** pairs, encoded using the sqlite3VdbeSerialXXX() functions above.
1321**
1322** Following the type/blob pairs, each key may have a single 0x00 byte
1323** followed by a varint. A key may only have this traling 0x00/varint
1324** pair if it has at least as many type/blob pairs as the key it is being
1325** compared to.
drhab9f7f12004-05-08 10:56:11 +00001326*/
drhab9f7f12004-05-08 10:56:11 +00001327int sqlite3VdbeKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001328 void *userData,
danielk1977183f9f72004-05-13 05:20:26 +00001329 int nKey1, const void *pKey1,
1330 int nKey2, const void *pKey2
drhab9f7f12004-05-08 10:56:11 +00001331){
drhd3d39e92004-05-20 22:16:29 +00001332 KeyInfo *pKeyInfo = (KeyInfo*)userData;
danielk19778d059842004-05-12 11:24:02 +00001333 int offset1 = 0;
1334 int offset2 = 0;
drhd3d39e92004-05-20 22:16:29 +00001335 int i = 0;
drhffbc3082004-05-21 01:29:06 +00001336 int rc = 0;
danielk1977183f9f72004-05-13 05:20:26 +00001337 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1338 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1339
drhd3d39e92004-05-20 22:16:29 +00001340 assert( pKeyInfo!=0 );
danielk19778d059842004-05-12 11:24:02 +00001341 while( offset1<nKey1 && offset2<nKey2 ){
1342 Mem mem1;
1343 Mem mem2;
drh25aa1b42004-05-28 01:39:01 +00001344 u32 serial_type1;
1345 u32 serial_type2;
danielk19778d059842004-05-12 11:24:02 +00001346
danielk1977183f9f72004-05-13 05:20:26 +00001347 /* Read the serial types for the next element in each key. */
drh25aa1b42004-05-28 01:39:01 +00001348 offset1 += sqlite3GetVarint32(&aKey1[offset1], &serial_type1);
1349 offset2 += sqlite3GetVarint32(&aKey2[offset2], &serial_type2);
danielk1977183f9f72004-05-13 05:20:26 +00001350
1351 /* If either of the varints just read in are 0 (not a type), then
1352 ** this is the end of the keys. The remaining data in each key is
1353 ** the varint rowid. Compare these as signed integers and return
1354 ** the result.
1355 */
1356 if( !serial_type1 || !serial_type2 ){
1357 assert( !serial_type1 && !serial_type2 );
drh25aa1b42004-05-28 01:39:01 +00001358 sqlite3GetVarint32(&aKey1[offset1], &serial_type1);
1359 sqlite3GetVarint32(&aKey2[offset2], &serial_type2);
drhffbc3082004-05-21 01:29:06 +00001360 if( serial_type1 < serial_type2 ){
1361 rc = -1;
1362 }else if( serial_type1 > serial_type2 ){
1363 rc = +1;
1364 }else{
1365 rc = 0;
1366 }
1367 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001368 }
1369
drhd3d39e92004-05-20 22:16:29 +00001370 assert( i<pKeyInfo->nField );
1371
danielk1977183f9f72004-05-13 05:20:26 +00001372 /* Assert that there is enough space left in each key for the blob of
1373 ** data to go with the serial type just read. This assert may fail if
1374 ** the file is corrupted. Then read the value from each key into mem1
1375 ** and mem2 respectively.
1376 */
drh25aa1b42004-05-28 01:39:01 +00001377 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1);
1378 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2);
danielk19778d059842004-05-12 11:24:02 +00001379
drhd3d39e92004-05-20 22:16:29 +00001380 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk19778d059842004-05-12 11:24:02 +00001381 if( mem1.flags&MEM_Dyn ){
1382 sqliteFree(mem1.z);
1383 }
1384 if( mem2.flags&MEM_Dyn ){
1385 sqliteFree(mem2.z);
1386 }
1387 if( rc!=0 ){
drhffbc3082004-05-21 01:29:06 +00001388 break;
danielk19778d059842004-05-12 11:24:02 +00001389 }
drhd3d39e92004-05-20 22:16:29 +00001390 i++;
danielk19778d059842004-05-12 11:24:02 +00001391 }
1392
danielk19773d1bfea2004-05-14 11:00:53 +00001393 /* One of the keys ran out of fields, but all the fields up to that point
1394 ** were equal. If the incrKey flag is true, then the second key is
1395 ** treated as larger.
1396 */
drhffbc3082004-05-21 01:29:06 +00001397 if( rc==0 ){
1398 if( pKeyInfo->incrKey ){
1399 assert( offset2==nKey2 );
1400 rc = -1;
1401 }else if( offset1<nKey1 ){
1402 rc = 1;
1403 }else if( offset2<nKey2 ){
1404 rc = -1;
1405 }
danielk19773d1bfea2004-05-14 11:00:53 +00001406 }
1407
drhffbc3082004-05-21 01:29:06 +00001408 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1409 rc = -rc;
drhab9f7f12004-05-08 10:56:11 +00001410 }
danielk19773d1bfea2004-05-14 11:00:53 +00001411
drhffbc3082004-05-21 01:29:06 +00001412 return rc;
drhab9f7f12004-05-08 10:56:11 +00001413}
danielk1977183f9f72004-05-13 05:20:26 +00001414
1415/*
danielk1977eb015e02004-05-18 01:31:14 +00001416** This function compares the two table row records specified by
1417** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1418** or positive integer if {nKey1, pKey1} is less than, equal to or
1419** greater than {nKey2, pKey2}.
1420**
danielk197784ac9d02004-05-18 09:58:06 +00001421** This function is pretty inefficient and will probably be replaced
danielk1977eb015e02004-05-18 01:31:14 +00001422** by something else in the near future. It is currently required
1423** by compound SELECT operators.
1424*/
1425int sqlite3VdbeRowCompare(
1426 void *userData,
1427 int nKey1, const void *pKey1,
1428 int nKey2, const void *pKey2
1429){
drhd3d39e92004-05-20 22:16:29 +00001430 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001431 u32 d1, d2; /* Offset into aKey[] of next data element */
1432 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1433 u32 szHdr1, szHdr2; /* Number of bytes in header */
1434 int i = 0;
1435 int nField;
1436 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001437 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1438 const unsigned char *aKey2 = (const unsigned char *)pKey2;
drhd3194f52004-05-27 19:59:32 +00001439
1440 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1441 d1 = szHdr1;
1442 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1443 d2 = szHdr2;
1444 nField = pKeyInfo->nField;
1445 while( idx1<szHdr1 && idx2<szHdr2 && d1<nKey1 && d2<nKey2 && i<nField ){
danielk197784ac9d02004-05-18 09:58:06 +00001446 Mem mem1;
1447 Mem mem2;
drhd3194f52004-05-27 19:59:32 +00001448 u32 serial_type1;
1449 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001450
1451 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001452 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
1453 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
danielk197784ac9d02004-05-18 09:58:06 +00001454
1455 /* Assert that there is enough space left in each key for the blob of
1456 ** data to go with the serial type just read. This assert may fail if
1457 ** the file is corrupted. Then read the value from each key into mem1
1458 ** and mem2 respectively.
1459 */
drh25aa1b42004-05-28 01:39:01 +00001460 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1461 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001462
drhd3d39e92004-05-20 22:16:29 +00001463 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk197784ac9d02004-05-18 09:58:06 +00001464 if( mem1.flags&MEM_Dyn ){
1465 sqliteFree(mem1.z);
1466 }
1467 if( mem2.flags&MEM_Dyn ){
1468 sqliteFree(mem2.z);
1469 }
1470 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001471 break;
1472 }
1473 i++;
1474 }
1475
1476 /* One of the keys ran out of fields, but all the fields up to that point
1477 ** were equal. If the incrKey flag is true, then the second key is
1478 ** treated as larger.
1479 */
1480 if( rc==0 ){
1481 if( pKeyInfo->incrKey ){
1482 assert( d2==nKey2 );
1483 rc = -1;
1484 }else if( d1<nKey1 ){
1485 rc = 1;
1486 }else if( d2<nKey2 ){
1487 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001488 }
1489 }
1490
drhd3194f52004-05-27 19:59:32 +00001491 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1492 rc = -rc;
1493 }
1494
1495 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001496}
1497
1498
1499/*
danielk1977183f9f72004-05-13 05:20:26 +00001500** pCur points at an index entry. Read the rowid (varint occuring at
1501** the end of the entry and store it in *rowid. Return SQLITE_OK if
1502** everything works, or an error code otherwise.
1503*/
1504int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1505 i64 sz;
1506 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001507 char buf[10];
danielk1977183f9f72004-05-13 05:20:26 +00001508 int len;
1509 u64 r;
1510
1511 rc = sqlite3BtreeKeySize(pCur, &sz);
1512 if( rc!=SQLITE_OK ){
1513 return rc;
1514 }
danielk19773d1bfea2004-05-14 11:00:53 +00001515 len = ((sz>10)?10:sz);
1516
1517 /* If there are less than 2 bytes in the key, this cannot be
1518 ** a valid index entry. In practice this comes up for a query
1519 ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table
1520 ** with an index on x. In this case just call the rowid 0.
1521 */
1522 if( len<2 ){
1523 *rowid = 0;
1524 return SQLITE_OK;
1525 }
danielk1977183f9f72004-05-13 05:20:26 +00001526
1527 rc = sqlite3BtreeKey(pCur, sz-len, len, buf);
1528 if( rc!=SQLITE_OK ){
1529 return rc;
1530 }
1531
danielk19773d1bfea2004-05-14 11:00:53 +00001532 len--;
1533 while( buf[len-1] && --len );
danielk1977183f9f72004-05-13 05:20:26 +00001534
danielk19773d1bfea2004-05-14 11:00:53 +00001535 sqlite3GetVarint(&buf[len], &r);
danielk1977183f9f72004-05-13 05:20:26 +00001536 *rowid = r;
1537 return SQLITE_OK;
1538}
1539
drh7cf6e4d2004-05-19 14:56:55 +00001540/*
drhd3d39e92004-05-20 22:16:29 +00001541** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001542** the key string in pKey (of length nKey). Write into *pRes a number
1543** that is negative, zero, or positive if pC is less than, equal to,
1544** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001545**
1546** pKey might contain fewer terms than the cursor.
drh7cf6e4d2004-05-19 14:56:55 +00001547*/
danielk1977183f9f72004-05-13 05:20:26 +00001548int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001549 Cursor *pC, /* The cursor to compare against */
1550 int nKey, const u8 *pKey, /* The key to compare */
1551 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001552){
1553 unsigned char *pCellKey;
1554 u64 nCellKey;
1555 int freeCellKey = 0;
1556 int rc;
1557 int len;
danielk19773d1bfea2004-05-14 11:00:53 +00001558 BtCursor *pCur = pC->pCursor;
danielk1977183f9f72004-05-13 05:20:26 +00001559
1560 sqlite3BtreeKeySize(pCur, &nCellKey);
1561 if( nCellKey<=0 ){
1562 *res = 0;
1563 return SQLITE_OK;
1564 }
1565
1566 pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey);
1567 if( !pCellKey ){
drh10617cd2004-05-14 15:27:27 +00001568 pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey);
danielk1977183f9f72004-05-13 05:20:26 +00001569 if( !pCellKey ){
1570 return SQLITE_NOMEM;
1571 }
1572 freeCellKey = 1;
1573 rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
1574 if( rc!=SQLITE_OK ){
1575 sqliteFree(pCellKey);
1576 return rc;
1577 }
1578 }
1579
1580 len = nCellKey-2;
1581 while( pCellKey[len] && --len );
1582
drhd3d39e92004-05-20 22:16:29 +00001583 *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, len, pCellKey, nKey, pKey);
danielk1977183f9f72004-05-13 05:20:26 +00001584
1585 if( freeCellKey ){
1586 sqliteFree(pCellKey);
1587 }
1588 return SQLITE_OK;
1589}