blob: 5e3672f918ba1a5223f5e6951d2cff80e3e28b7b [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
13** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior
14** 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
danielk1977132872b2004-05-10 10:37:18 +0000103 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000104#endif
105 return i;
106}
107
108/*
drh701a0ae2004-02-22 20:05:00 +0000109** Add an opcode that includes the p3 value.
110*/
danielk19774adee202004-05-08 08:23:19 +0000111int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3, int p3type){
112 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
113 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000114 return addr;
115}
116
117/*
118** Add multiple opcodes. The list is terminated by an opcode of 0.
119*/
danielk19774adee202004-05-08 08:23:19 +0000120int sqlite3VdbeCode(Vdbe *p, ...){
drh701a0ae2004-02-22 20:05:00 +0000121 int addr;
122 va_list ap;
123 int opcode, p1, p2;
124 va_start(ap, p);
125 addr = p->nOp;
126 while( (opcode = va_arg(ap,int))!=0 ){
127 p1 = va_arg(ap,int);
128 p2 = va_arg(ap,int);
danielk19774adee202004-05-08 08:23:19 +0000129 sqlite3VdbeAddOp(p, opcode, p1, p2);
drh701a0ae2004-02-22 20:05:00 +0000130 }
131 va_end(ap);
132 return addr;
133}
134
135
136
137/*
drh9a324642003-09-06 20:12:01 +0000138** Create a new symbolic label for an instruction that has yet to be
139** coded. The symbolic label is really just a negative number. The
140** label can be used as the P2 value of an operation. Later, when
141** the label is resolved to a specific address, the VDBE will scan
142** through its operation list and change all values of P2 which match
143** the label into the resolved address.
144**
145** The VDBE knows that a P2 value is a label because labels are
146** always negative and P2 values are suppose to be non-negative.
147** Hence, a negative P2 value is a label that has yet to be resolved.
148*/
danielk19774adee202004-05-08 08:23:19 +0000149int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000150 int i;
151 i = p->nLabel++;
152 assert( p->magic==VDBE_MAGIC_INIT );
153 if( i>=p->nLabelAlloc ){
154 int *aNew;
155 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
156 aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
157 if( aNew==0 ){
158 sqliteFree(p->aLabel);
159 }
160 p->aLabel = aNew;
161 }
162 if( p->aLabel==0 ){
163 p->nLabel = 0;
164 p->nLabelAlloc = 0;
165 return 0;
166 }
167 p->aLabel[i] = -1;
168 return -1-i;
169}
170
171/*
172** Resolve label "x" to be the address of the next instruction to
173** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000174** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000175*/
danielk19774adee202004-05-08 08:23:19 +0000176void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh9a324642003-09-06 20:12:01 +0000177 int j;
178 assert( p->magic==VDBE_MAGIC_INIT );
179 if( x<0 && (-x)<=p->nLabel && p->aOp ){
180 if( p->aLabel[-1-x]==p->nOp ) return;
181 assert( p->aLabel[-1-x]<0 );
182 p->aLabel[-1-x] = p->nOp;
183 for(j=0; j<p->nOp; j++){
184 if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp;
185 }
186 }
187}
188
189/*
190** Return the address of the next instruction to be inserted.
191*/
danielk19774adee202004-05-08 08:23:19 +0000192int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000193 assert( p->magic==VDBE_MAGIC_INIT );
194 return p->nOp;
195}
196
197/*
198** Add a whole list of operations to the operation stack. Return the
199** address of the first operation added.
200*/
danielk19774adee202004-05-08 08:23:19 +0000201int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000202 int addr;
203 assert( p->magic==VDBE_MAGIC_INIT );
204 if( p->nOp + nOp >= p->nOpAlloc ){
205 int oldSize = p->nOpAlloc;
206 Op *aNew;
207 p->nOpAlloc = p->nOpAlloc*2 + nOp + 10;
208 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
209 if( aNew==0 ){
210 p->nOpAlloc = oldSize;
211 return 0;
212 }
213 p->aOp = aNew;
214 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
215 }
216 addr = p->nOp;
217 if( nOp>0 ){
218 int i;
drh905793e2004-02-21 13:31:09 +0000219 VdbeOpList const *pIn = aOp;
220 for(i=0; i<nOp; i++, pIn++){
221 int p2 = pIn->p2;
222 VdbeOp *pOut = &p->aOp[i+addr];
223 pOut->opcode = pIn->opcode;
224 pOut->p1 = pIn->p1;
225 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
226 pOut->p3 = pIn->p3;
227 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000228#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +0000229 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000230 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000231 }
232#endif
233 }
234 p->nOp += nOp;
235 }
236 return addr;
237}
238
239/*
240** Change the value of the P1 operand for a specific instruction.
241** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000242** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000243** few minor changes to the program.
244*/
danielk19774adee202004-05-08 08:23:19 +0000245void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000246 assert( p->magic==VDBE_MAGIC_INIT );
247 if( p && addr>=0 && p->nOp>addr && p->aOp ){
248 p->aOp[addr].p1 = val;
249 }
250}
251
252/*
253** Change the value of the P2 operand for a specific instruction.
254** This routine is useful for setting a jump destination.
255*/
danielk19774adee202004-05-08 08:23:19 +0000256void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000257 assert( val>=0 );
258 assert( p->magic==VDBE_MAGIC_INIT );
259 if( p && addr>=0 && p->nOp>addr && p->aOp ){
260 p->aOp[addr].p2 = val;
261 }
262}
263
264/*
265** Change the value of the P3 operand for a specific instruction.
266** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000267** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000268** few minor changes to the program.
269**
270** If n>=0 then the P3 operand is dynamic, meaning that a copy of
271** the string is made into memory obtained from sqliteMalloc().
272** A value of n==0 means copy bytes of zP3 up to and including the
273** first null byte. If n>0 then copy n+1 bytes of zP3.
274**
275** If n==P3_STATIC it means that zP3 is a pointer to a constant static
276** string and we can just copy the pointer. n==P3_POINTER means zP3 is
277** a pointer to some object other than a string.
278**
279** If addr<0 then change P3 on the most recently inserted instruction.
280*/
danielk19774adee202004-05-08 08:23:19 +0000281void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000282 Op *pOp;
283 assert( p->magic==VDBE_MAGIC_INIT );
284 if( p==0 || p->aOp==0 ) return;
285 if( addr<0 || addr>=p->nOp ){
286 addr = p->nOp - 1;
287 if( addr<0 ) return;
288 }
289 pOp = &p->aOp[addr];
290 if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
291 sqliteFree(pOp->p3);
292 pOp->p3 = 0;
293 }
294 if( zP3==0 ){
295 pOp->p3 = 0;
296 pOp->p3type = P3_NOTUSED;
297 }else if( n<0 ){
298 pOp->p3 = (char*)zP3;
299 pOp->p3type = n;
300 }else{
danielk19774adee202004-05-08 08:23:19 +0000301 sqlite3SetNString(&pOp->p3, zP3, n, 0);
drh9a324642003-09-06 20:12:01 +0000302 pOp->p3type = P3_DYNAMIC;
303 }
304}
305
306/*
307** If the P3 operand to the specified instruction appears
308** to be a quoted string token, then this procedure removes
309** the quotes.
310**
311** The quoting operator can be either a grave ascent (ASCII 0x27)
312** or a double quote character (ASCII 0x22). Two quotes in a row
313** resolve to be a single actual quote character within the string.
314*/
danielk19774adee202004-05-08 08:23:19 +0000315void sqlite3VdbeDequoteP3(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000316 Op *pOp;
317 assert( p->magic==VDBE_MAGIC_INIT );
drh51e9a442004-01-16 16:42:53 +0000318 if( p->aOp==0 ) return;
319 if( addr<0 || addr>=p->nOp ){
320 addr = p->nOp - 1;
321 if( addr<0 ) return;
322 }
drh9a324642003-09-06 20:12:01 +0000323 pOp = &p->aOp[addr];
324 if( pOp->p3==0 || pOp->p3[0]==0 ) return;
325 if( pOp->p3type==P3_POINTER ) return;
326 if( pOp->p3type!=P3_DYNAMIC ){
327 pOp->p3 = sqliteStrDup(pOp->p3);
328 pOp->p3type = P3_DYNAMIC;
329 }
danielk19774adee202004-05-08 08:23:19 +0000330 sqlite3Dequote(pOp->p3);
drh9a324642003-09-06 20:12:01 +0000331}
332
333/*
334** On the P3 argument of the given instruction, change all
335** strings of whitespace characters into a single space and
336** delete leading and trailing whitespace.
337*/
danielk19774adee202004-05-08 08:23:19 +0000338void sqlite3VdbeCompressSpace(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000339 unsigned char *z;
340 int i, j;
341 Op *pOp;
342 assert( p->magic==VDBE_MAGIC_INIT );
343 if( p->aOp==0 || addr<0 || addr>=p->nOp ) return;
344 pOp = &p->aOp[addr];
345 if( pOp->p3type==P3_POINTER ){
346 return;
347 }
348 if( pOp->p3type!=P3_DYNAMIC ){
349 pOp->p3 = sqliteStrDup(pOp->p3);
350 pOp->p3type = P3_DYNAMIC;
351 }
352 z = (unsigned char*)pOp->p3;
353 if( z==0 ) return;
354 i = j = 0;
355 while( isspace(z[i]) ){ i++; }
356 while( z[i] ){
357 if( isspace(z[i]) ){
358 z[j++] = ' ';
359 while( isspace(z[++i]) ){}
360 }else{
361 z[j++] = z[i++];
362 }
363 }
364 while( j>0 && isspace(z[j-1]) ){ j--; }
365 z[j] = 0;
366}
367
368/*
369** Search for the current program for the given opcode and P2
370** value. Return the address plus 1 if found and 0 if not found.
371*/
danielk19774adee202004-05-08 08:23:19 +0000372int sqlite3VdbeFindOp(Vdbe *p, int op, int p2){
drh9a324642003-09-06 20:12:01 +0000373 int i;
374 assert( p->magic==VDBE_MAGIC_INIT );
375 for(i=0; i<p->nOp; i++){
376 if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1;
377 }
378 return 0;
379}
380
381/*
382** Return the opcode for a given address.
383*/
danielk19774adee202004-05-08 08:23:19 +0000384VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000385 assert( p->magic==VDBE_MAGIC_INIT );
386 assert( addr>=0 && addr<p->nOp );
387 return &p->aOp[addr];
388}
389
390/*
391** The following group or routines are employed by installable functions
392** to return their results.
393**
danielk1977132872b2004-05-10 10:37:18 +0000394** The sqlite3_set_result_string() routine can be used to return a string
drh9a324642003-09-06 20:12:01 +0000395** value or to return a NULL. To return a NULL, pass in NULL for zResult.
396** A copy is made of the string before this routine returns so it is safe
397** to pass in an ephemeral string.
398**
danielk1977132872b2004-05-10 10:37:18 +0000399** sqlite3_set_result_error() works like sqlite3_set_result_string() except
drh9a324642003-09-06 20:12:01 +0000400** that it signals a fatal error. The string argument, if any, is the
401** error message. If the argument is NULL a generic substitute error message
402** is used.
403**
danielk1977132872b2004-05-10 10:37:18 +0000404** The sqlite3_set_result_int() and sqlite3_set_result_double() set the return
drh9a324642003-09-06 20:12:01 +0000405** value of the user function to an integer or a double.
406**
407** These routines are defined here in vdbe.c because they depend on knowing
408** the internals of the sqlite_func structure which is only defined in
409** this source file.
410*/
danielk1977132872b2004-05-10 10:37:18 +0000411char *sqlite3_set_result_string(sqlite_func *p, const char *zResult, int n){
drh9a324642003-09-06 20:12:01 +0000412 assert( !p->isStep );
drh00706be2004-01-30 14:49:16 +0000413 if( p->s.flags & MEM_Dyn ){
414 sqliteFree(p->s.z);
drh9a324642003-09-06 20:12:01 +0000415 }
416 if( zResult==0 ){
drh00706be2004-01-30 14:49:16 +0000417 p->s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000418 n = 0;
drh00706be2004-01-30 14:49:16 +0000419 p->s.z = 0;
drh9a324642003-09-06 20:12:01 +0000420 p->s.n = 0;
421 }else{
422 if( n<0 ) n = strlen(zResult);
423 if( n<NBFS-1 ){
drh00706be2004-01-30 14:49:16 +0000424 memcpy(p->s.zShort, zResult, n);
425 p->s.zShort[n] = 0;
drh6810ce62004-01-31 19:22:56 +0000426 p->s.flags = MEM_Str | MEM_Short;
drh00706be2004-01-30 14:49:16 +0000427 p->s.z = p->s.zShort;
drh9a324642003-09-06 20:12:01 +0000428 }else{
drh00706be2004-01-30 14:49:16 +0000429 p->s.z = sqliteMallocRaw( n+1 );
430 if( p->s.z ){
431 memcpy(p->s.z, zResult, n);
432 p->s.z[n] = 0;
drh9a324642003-09-06 20:12:01 +0000433 }
drh00706be2004-01-30 14:49:16 +0000434 p->s.flags = MEM_Str | MEM_Dyn;
drh9a324642003-09-06 20:12:01 +0000435 }
436 p->s.n = n+1;
437 }
drh00706be2004-01-30 14:49:16 +0000438 return p->s.z;
drh9a324642003-09-06 20:12:01 +0000439}
danielk1977132872b2004-05-10 10:37:18 +0000440void sqlite3_set_result_int(sqlite_func *p, int iResult){
drh9a324642003-09-06 20:12:01 +0000441 assert( !p->isStep );
drh00706be2004-01-30 14:49:16 +0000442 if( p->s.flags & MEM_Dyn ){
443 sqliteFree(p->s.z);
drh9a324642003-09-06 20:12:01 +0000444 }
445 p->s.i = iResult;
drh00706be2004-01-30 14:49:16 +0000446 p->s.flags = MEM_Int;
drh9a324642003-09-06 20:12:01 +0000447}
danielk1977132872b2004-05-10 10:37:18 +0000448void sqlite3_set_result_double(sqlite_func *p, double rResult){
drh9a324642003-09-06 20:12:01 +0000449 assert( !p->isStep );
drh00706be2004-01-30 14:49:16 +0000450 if( p->s.flags & MEM_Dyn ){
451 sqliteFree(p->s.z);
drh9a324642003-09-06 20:12:01 +0000452 }
453 p->s.r = rResult;
drh00706be2004-01-30 14:49:16 +0000454 p->s.flags = MEM_Real;
drh9a324642003-09-06 20:12:01 +0000455}
danielk1977132872b2004-05-10 10:37:18 +0000456void sqlite3_set_result_error(sqlite_func *p, const char *zMsg, int n){
drh9a324642003-09-06 20:12:01 +0000457 assert( !p->isStep );
danielk1977132872b2004-05-10 10:37:18 +0000458 sqlite3_set_result_string(p, zMsg, n);
drh9a324642003-09-06 20:12:01 +0000459 p->isError = 1;
460}
461
462/*
463** Extract the user data from a sqlite_func structure and return a
464** pointer to it.
drh9a324642003-09-06 20:12:01 +0000465*/
danielk1977132872b2004-05-10 10:37:18 +0000466void *sqlite3_user_data(sqlite_func *p){
drh9a324642003-09-06 20:12:01 +0000467 assert( p && p->pFunc );
468 return p->pFunc->pUserData;
469}
470
471/*
472** Allocate or return the aggregate context for a user function. A new
473** context is allocated on the first call. Subsequent calls return the
474** same context that was returned on prior calls.
475**
476** This routine is defined here in vdbe.c because it depends on knowing
477** the internals of the sqlite_func structure which is only defined in
478** this source file.
479*/
danielk1977132872b2004-05-10 10:37:18 +0000480void *sqlite3_aggregate_context(sqlite_func *p, int nByte){
drh9a324642003-09-06 20:12:01 +0000481 assert( p && p->pFunc && p->pFunc->xStep );
482 if( p->pAgg==0 ){
483 if( nByte<=NBFS ){
drh00706be2004-01-30 14:49:16 +0000484 p->pAgg = (void*)p->s.z;
485 memset(p->pAgg, 0, nByte);
drh9a324642003-09-06 20:12:01 +0000486 }else{
487 p->pAgg = sqliteMalloc( nByte );
488 }
489 }
490 return p->pAgg;
491}
492
493/*
494** Return the number of times the Step function of a aggregate has been
495** called.
496**
497** This routine is defined here in vdbe.c because it depends on knowing
498** the internals of the sqlite_func structure which is only defined in
499** this source file.
500*/
danielk1977132872b2004-05-10 10:37:18 +0000501int sqlite3_aggregate_count(sqlite_func *p){
drh9a324642003-09-06 20:12:01 +0000502 assert( p && p->pFunc && p->pFunc->xStep );
503 return p->cnt;
504}
505
506#if !defined(NDEBUG) || defined(VDBE_PROFILE)
507/*
508** Print a single opcode. This routine is used for debugging only.
509*/
danielk19774adee202004-05-08 08:23:19 +0000510void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000511 char *zP3;
512 char zPtr[40];
513 if( pOp->p3type==P3_POINTER ){
514 sprintf(zPtr, "ptr(%#x)", (int)pOp->p3);
515 zP3 = zPtr;
516 }else{
517 zP3 = pOp->p3;
518 }
519 if( pOut==0 ) pOut = stdout;
520 fprintf(pOut,"%4d %-12s %4d %4d %s\n",
danielk19774adee202004-05-08 08:23:19 +0000521 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3 ? zP3 : "");
drh9a324642003-09-06 20:12:01 +0000522 fflush(pOut);
523}
524#endif
525
526/*
527** Give a listing of the program in the virtual machine.
528**
danielk19774adee202004-05-08 08:23:19 +0000529** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000530** running the code, it invokes the callback once for each instruction.
531** This feature is used to implement "EXPLAIN".
532*/
danielk19774adee202004-05-08 08:23:19 +0000533int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000534 Vdbe *p /* The VDBE */
535){
536 sqlite *db = p->db;
537 int i;
drh826fb5a2004-02-14 23:59:57 +0000538 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000539 static char *azColumnNames[] = {
540 "addr", "opcode", "p1", "p2", "p3",
541 "int", "text", "int", "int", "text",
542 0
543 };
544
545 assert( p->popStack==0 );
546 assert( p->explain );
547 p->azColName = azColumnNames;
drh00706be2004-01-30 14:49:16 +0000548 p->azResColumn = p->zArgv;
549 for(i=0; i<5; i++) p->zArgv[i] = p->aStack[i].zShort;
drh826fb5a2004-02-14 23:59:57 +0000550 i = p->pc;
551 if( i>=p->nOp ){
552 p->rc = SQLITE_OK;
553 rc = SQLITE_DONE;
554 }else if( db->flags & SQLITE_Interrupt ){
555 db->flags &= ~SQLITE_Interrupt;
556 if( db->magic!=SQLITE_MAGIC_BUSY ){
557 p->rc = SQLITE_MISUSE;
558 }else{
559 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000560 }
drh826fb5a2004-02-14 23:59:57 +0000561 rc = SQLITE_ERROR;
danielk1977132872b2004-05-10 10:37:18 +0000562 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000563 }else{
drh00706be2004-01-30 14:49:16 +0000564 sprintf(p->zArgv[0],"%d",i);
565 sprintf(p->zArgv[2],"%d", p->aOp[i].p1);
566 sprintf(p->zArgv[3],"%d", p->aOp[i].p2);
drh9a324642003-09-06 20:12:01 +0000567 if( p->aOp[i].p3type==P3_POINTER ){
drh00706be2004-01-30 14:49:16 +0000568 sprintf(p->aStack[4].zShort, "ptr(%#x)", (int)p->aOp[i].p3);
569 p->zArgv[4] = p->aStack[4].zShort;
drh9a324642003-09-06 20:12:01 +0000570 }else{
drh00706be2004-01-30 14:49:16 +0000571 p->zArgv[4] = p->aOp[i].p3;
drh9a324642003-09-06 20:12:01 +0000572 }
danielk19774adee202004-05-08 08:23:19 +0000573 p->zArgv[1] = sqlite3OpcodeNames[p->aOp[i].opcode];
drh826fb5a2004-02-14 23:59:57 +0000574 p->pc = i+1;
575 p->azResColumn = p->zArgv;
576 p->nResColumn = 5;
577 p->rc = SQLITE_OK;
578 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000579 }
drh826fb5a2004-02-14 23:59:57 +0000580 return rc;
drh9a324642003-09-06 20:12:01 +0000581}
582
583/*
584** Prepare a virtual machine for execution. This involves things such
585** as allocating stack space and initializing the program counter.
586** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000587** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000588*/
danielk19774adee202004-05-08 08:23:19 +0000589void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000590 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000591 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000592 int isExplain /* True if the EXPLAIN keywords is present */
593){
594 int n;
595
596 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000597 assert( p->magic==VDBE_MAGIC_INIT );
598
599 /* Add a HALT instruction to the very end of the program.
600 */
601 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000602 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000603 }
604
605 /* No instruction ever pushes more than a single element onto the
606 ** stack. And the stack never grows on successive executions of the
607 ** same loop. So the total number of instructions is an upper bound
608 ** on the maximum stack depth required.
609 **
610 ** Allocation all the stack space we will ever need.
611 */
drh82a48512003-09-06 22:45:20 +0000612 if( p->aStack==0 ){
613 p->nVar = nVar;
614 assert( nVar>=0 );
615 n = isExplain ? 10 : p->nOp;
616 p->aStack = sqliteMalloc(
drh6810ce62004-01-31 19:22:56 +0000617 n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */
618 + p->nVar*(sizeof(char*)+sizeof(int)+1) /* azVar, anVar, abVar */
drh82a48512003-09-06 22:45:20 +0000619 );
drh00706be2004-01-30 14:49:16 +0000620 p->zArgv = (char**)&p->aStack[n];
621 p->azColName = (char**)&p->zArgv[n];
drh82a48512003-09-06 22:45:20 +0000622 p->azVar = (char**)&p->azColName[n];
623 p->anVar = (int*)&p->azVar[p->nVar];
624 p->abVar = (u8*)&p->anVar[p->nVar];
625 }
drh9a324642003-09-06 20:12:01 +0000626
danielk19774adee202004-05-08 08:23:19 +0000627 sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
drh9a324642003-09-06 20:12:01 +0000628 p->agg.pSearch = 0;
629#ifdef MEMORY_DEBUG
danielk19774adee202004-05-08 08:23:19 +0000630 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000631 p->trace = stdout;
632 }
633#endif
drh6810ce62004-01-31 19:22:56 +0000634 p->pTos = &p->aStack[-1];
drh9a324642003-09-06 20:12:01 +0000635 p->pc = 0;
636 p->rc = SQLITE_OK;
637 p->uniqueCnt = 0;
638 p->returnDepth = 0;
639 p->errorAction = OE_Abort;
640 p->undoTransOnError = 0;
drh9a324642003-09-06 20:12:01 +0000641 p->popStack = 0;
642 p->explain |= isExplain;
643 p->magic = VDBE_MAGIC_RUN;
644#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000645 {
646 int i;
647 for(i=0; i<p->nOp; i++){
648 p->aOp[i].cnt = 0;
649 p->aOp[i].cycles = 0;
650 }
drh9a324642003-09-06 20:12:01 +0000651 }
652#endif
653}
654
655
656/*
657** Remove any elements that remain on the sorter for the VDBE given.
658*/
danielk19774adee202004-05-08 08:23:19 +0000659void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000660 while( p->pSort ){
661 Sorter *pSorter = p->pSort;
662 p->pSort = pSorter->pNext;
663 sqliteFree(pSorter->zKey);
664 sqliteFree(pSorter->pData);
665 sqliteFree(pSorter);
666 }
667}
668
669/*
drh9a324642003-09-06 20:12:01 +0000670** Reset an Agg structure. Delete all its contents.
671**
672** For installable aggregate functions, if the step function has been
673** called, make sure the finalizer function has also been called. The
674** finalizer might need to free memory that was allocated as part of its
675** private context. If the finalizer has not been called yet, call it
676** now.
677*/
danielk19774adee202004-05-08 08:23:19 +0000678void sqlite3VdbeAggReset(Agg *pAgg){
drh9a324642003-09-06 20:12:01 +0000679 int i;
680 HashElem *p;
681 for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
682 AggElem *pElem = sqliteHashData(p);
683 assert( pAgg->apFunc!=0 );
684 for(i=0; i<pAgg->nMem; i++){
685 Mem *pMem = &pElem->aMem[i];
drh00706be2004-01-30 14:49:16 +0000686 if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
drh9a324642003-09-06 20:12:01 +0000687 sqlite_func ctx;
688 ctx.pFunc = pAgg->apFunc[i];
drh00706be2004-01-30 14:49:16 +0000689 ctx.s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000690 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +0000691 ctx.cnt = pMem->i;
drh9a324642003-09-06 20:12:01 +0000692 ctx.isStep = 0;
693 ctx.isError = 0;
694 (*pAgg->apFunc[i]->xFinalize)(&ctx);
drh00706be2004-01-30 14:49:16 +0000695 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
drh9a324642003-09-06 20:12:01 +0000696 sqliteFree(pMem->z);
697 }
drh9cbe7ca2004-02-18 16:57:23 +0000698 if( ctx.s.flags & MEM_Dyn ){
699 sqliteFree(ctx.s.z);
700 }
drh00706be2004-01-30 14:49:16 +0000701 }else if( pMem->flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000702 sqliteFree(pMem->z);
703 }
704 }
705 sqliteFree(pElem);
706 }
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3HashClear(&pAgg->hash);
drh9a324642003-09-06 20:12:01 +0000708 sqliteFree(pAgg->apFunc);
709 pAgg->apFunc = 0;
710 pAgg->pCurrent = 0;
711 pAgg->pSearch = 0;
712 pAgg->nMem = 0;
713}
714
715/*
716** Delete a keylist
717*/
danielk19774adee202004-05-08 08:23:19 +0000718void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000719 while( p ){
720 Keylist *pNext = p->pNext;
721 sqliteFree(p);
722 p = pNext;
723 }
724}
725
726/*
727** Close a cursor and release all the resources that cursor happens
728** to hold.
729*/
danielk19774adee202004-05-08 08:23:19 +0000730void sqlite3VdbeCleanupCursor(Cursor *pCx){
drh9a324642003-09-06 20:12:01 +0000731 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000733 }
734 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000735 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000736 }
737 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000738 sqliteFree(pCx->aType);
drhd7556d22004-05-14 21:59:40 +0000739 memset(pCx, 0, sizeof(*pCx));
drh9a324642003-09-06 20:12:01 +0000740}
741
742/*
743** Close all cursors
744*/
745static void closeAllCursors(Vdbe *p){
746 int i;
747 for(i=0; i<p->nCursor; i++){
drhd7556d22004-05-14 21:59:40 +0000748 Cursor *pC = p->apCsr[i];
749 sqlite3VdbeCleanupCursor(pC);
750 sqliteFree(pC);
drh9a324642003-09-06 20:12:01 +0000751 }
drhd7556d22004-05-14 21:59:40 +0000752 sqliteFree(p->apCsr);
753 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000754 p->nCursor = 0;
755}
756
757/*
drh9a324642003-09-06 20:12:01 +0000758** Clean up the VM after execution.
759**
760** This routine will automatically close any cursors, lists, and/or
761** sorters that were left open. It also deletes the values of
762** variables in the azVariable[] array.
763*/
764static void Cleanup(Vdbe *p){
765 int i;
drh6810ce62004-01-31 19:22:56 +0000766 if( p->aStack ){
767 Mem *pTos = p->pTos;
768 while( pTos>=p->aStack ){
769 if( pTos->flags & MEM_Dyn ){
770 sqliteFree(pTos->z);
771 }
772 pTos--;
773 }
774 p->pTos = pTos;
775 }
drh9a324642003-09-06 20:12:01 +0000776 closeAllCursors(p);
777 if( p->aMem ){
778 for(i=0; i<p->nMem; i++){
drh00706be2004-01-30 14:49:16 +0000779 if( p->aMem[i].flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000780 sqliteFree(p->aMem[i].z);
781 }
782 }
783 }
784 sqliteFree(p->aMem);
785 p->aMem = 0;
786 p->nMem = 0;
787 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000788 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000789 p->pList = 0;
790 }
danielk19774adee202004-05-08 08:23:19 +0000791 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000792 if( p->pFile ){
793 if( p->pFile!=stdin ) fclose(p->pFile);
794 p->pFile = 0;
795 }
796 if( p->azField ){
797 sqliteFree(p->azField);
798 p->azField = 0;
799 }
800 p->nField = 0;
801 if( p->zLine ){
802 sqliteFree(p->zLine);
803 p->zLine = 0;
804 }
805 p->nLineAlloc = 0;
danielk19774adee202004-05-08 08:23:19 +0000806 sqlite3VdbeAggReset(&p->agg);
drh9a324642003-09-06 20:12:01 +0000807 if( p->aSet ){
808 for(i=0; i<p->nSet; i++){
danielk19774adee202004-05-08 08:23:19 +0000809 sqlite3HashClear(&p->aSet[i].hash);
drh9a324642003-09-06 20:12:01 +0000810 }
811 }
812 sqliteFree(p->aSet);
813 p->aSet = 0;
814 p->nSet = 0;
815 if( p->keylistStack ){
816 int ii;
817 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000818 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000819 }
820 sqliteFree(p->keylistStack);
821 p->keylistStackDepth = 0;
822 p->keylistStack = 0;
823 }
drh5f968432004-02-21 19:02:30 +0000824 sqliteFree(p->contextStack);
825 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000826 sqliteFree(p->zErrMsg);
827 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000828}
829
830/*
831** Clean up a VDBE after execution but do not delete the VDBE just yet.
832** Write any error messages into *pzErrMsg. Return the result code.
833**
834** After this routine is run, the VDBE should be ready to be executed
835** again.
836*/
danielk19774adee202004-05-08 08:23:19 +0000837int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000838 sqlite *db = p->db;
839 int i;
840
841 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000842 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
drh9a324642003-09-06 20:12:01 +0000843 return SQLITE_MISUSE;
844 }
845 if( p->zErrMsg ){
846 if( pzErrMsg && *pzErrMsg==0 ){
847 *pzErrMsg = p->zErrMsg;
848 }else{
849 sqliteFree(p->zErrMsg);
850 }
851 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +0000852 }else if( p->rc ){
danielk1977132872b2004-05-10 10:37:18 +0000853 sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0);
drh9a324642003-09-06 20:12:01 +0000854 }
855 Cleanup(p);
856 if( p->rc!=SQLITE_OK ){
857 switch( p->errorAction ){
858 case OE_Abort: {
859 if( !p->undoTransOnError ){
860 for(i=0; i<db->nDb; i++){
861 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000862 sqlite3BtreeRollbackStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000863 }
864 }
865 break;
866 }
867 /* Fall through to ROLLBACK */
868 }
869 case OE_Rollback: {
danielk19774adee202004-05-08 08:23:19 +0000870 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000871 db->flags &= ~SQLITE_InTrans;
872 db->onError = OE_Default;
873 break;
874 }
875 default: {
876 if( p->undoTransOnError ){
danielk19774adee202004-05-08 08:23:19 +0000877 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000878 db->flags &= ~SQLITE_InTrans;
879 db->onError = OE_Default;
880 }
881 break;
882 }
883 }
danielk19774adee202004-05-08 08:23:19 +0000884 sqlite3RollbackInternalChanges(db);
drh9a324642003-09-06 20:12:01 +0000885 }
886 for(i=0; i<db->nDb; i++){
887 if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){
danielk19774adee202004-05-08 08:23:19 +0000888 sqlite3BtreeCommitStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000889 db->aDb[i].inTrans = 1;
890 }
891 }
danielk1977132872b2004-05-10 10:37:18 +0000892 assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +0000893#ifdef VDBE_PROFILE
894 {
895 FILE *out = fopen("vdbe_profile.out", "a");
896 if( out ){
897 int i;
898 fprintf(out, "---- ");
899 for(i=0; i<p->nOp; i++){
900 fprintf(out, "%02x", p->aOp[i].opcode);
901 }
902 fprintf(out, "\n");
903 for(i=0; i<p->nOp; i++){
904 fprintf(out, "%6d %10lld %8lld ",
905 p->aOp[i].cnt,
906 p->aOp[i].cycles,
907 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
908 );
danielk19774adee202004-05-08 08:23:19 +0000909 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000910 }
911 fclose(out);
912 }
913 }
914#endif
915 p->magic = VDBE_MAGIC_INIT;
916 return p->rc;
917}
918
919/*
920** Clean up and delete a VDBE after execution. Return an integer which is
921** the result code. Write any error message text into *pzErrMsg.
922*/
danielk19774adee202004-05-08 08:23:19 +0000923int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000924 int rc;
925 sqlite *db;
926
927 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000928 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
drh9a324642003-09-06 20:12:01 +0000929 return SQLITE_MISUSE;
930 }
931 db = p->db;
danielk19774adee202004-05-08 08:23:19 +0000932 rc = sqlite3VdbeReset(p, pzErrMsg);
933 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +0000934 if( db->want_to_close && db->pVdbe==0 ){
danielk1977132872b2004-05-10 10:37:18 +0000935 sqlite3_close(db);
drh9a324642003-09-06 20:12:01 +0000936 }
drha1f9b5e2004-02-14 16:31:02 +0000937 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +0000938 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +0000939 }
drh9a324642003-09-06 20:12:01 +0000940 return rc;
941}
942
943/*
944** Set the values of all variables. Variable $1 in the original SQL will
945** be the string azValue[0]. $2 will have the value azValue[1]. And
946** so forth. If a value is out of range (for example $3 when nValue==2)
947** then its value will be NULL.
948**
949** This routine overrides any prior call.
950*/
danielk1977132872b2004-05-10 10:37:18 +0000951int sqlite3_bind(sqlite_vm *pVm, int i, const char *zVal, int len, int copy){
drh7c972de2003-09-06 22:18:07 +0000952 Vdbe *p = (Vdbe*)pVm;
953 if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){
drh9a324642003-09-06 20:12:01 +0000954 return SQLITE_MISUSE;
955 }
drh7c972de2003-09-06 22:18:07 +0000956 if( i<1 || i>p->nVar ){
957 return SQLITE_RANGE;
drh9a324642003-09-06 20:12:01 +0000958 }
drh7c972de2003-09-06 22:18:07 +0000959 i--;
960 if( p->abVar[i] ){
961 sqliteFree(p->azVar[i]);
drh9a324642003-09-06 20:12:01 +0000962 }
drh7c972de2003-09-06 22:18:07 +0000963 if( zVal==0 ){
964 copy = 0;
965 len = 0;
drh9a324642003-09-06 20:12:01 +0000966 }
drh7c972de2003-09-06 22:18:07 +0000967 if( len<0 ){
968 len = strlen(zVal)+1;
drh9a324642003-09-06 20:12:01 +0000969 }
drh7c972de2003-09-06 22:18:07 +0000970 if( copy ){
971 p->azVar[i] = sqliteMalloc( len );
972 if( p->azVar[i] ) memcpy(p->azVar[i], zVal, len);
973 }else{
drh82a48512003-09-06 22:45:20 +0000974 p->azVar[i] = (char*)zVal;
drh7c972de2003-09-06 22:18:07 +0000975 }
976 p->abVar[i] = copy;
977 p->anVar[i] = len;
drh9a324642003-09-06 20:12:01 +0000978 return SQLITE_OK;
979}
980
981
982/*
983** Delete an entire VDBE.
984*/
danielk19774adee202004-05-08 08:23:19 +0000985void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000986 int i;
987 if( p==0 ) return;
988 Cleanup(p);
989 if( p->pPrev ){
990 p->pPrev->pNext = p->pNext;
991 }else{
992 assert( p->db->pVdbe==p );
993 p->db->pVdbe = p->pNext;
994 }
995 if( p->pNext ){
996 p->pNext->pPrev = p->pPrev;
997 }
998 p->pPrev = p->pNext = 0;
999 if( p->nOpAlloc==0 ){
1000 p->aOp = 0;
1001 p->nOp = 0;
1002 }
1003 for(i=0; i<p->nOp; i++){
1004 if( p->aOp[i].p3type==P3_DYNAMIC ){
1005 sqliteFree(p->aOp[i].p3);
1006 }
1007 }
drh7c972de2003-09-06 22:18:07 +00001008 for(i=0; i<p->nVar; i++){
1009 if( p->abVar[i] ) sqliteFree(p->azVar[i]);
1010 }
drh9a324642003-09-06 20:12:01 +00001011 sqliteFree(p->aOp);
1012 sqliteFree(p->aLabel);
1013 sqliteFree(p->aStack);
1014 p->magic = VDBE_MAGIC_DEAD;
1015 sqliteFree(p);
1016}
drha11846b2004-01-07 18:52:56 +00001017
1018/*
1019** Convert an integer in between the native integer format and
1020** the bigEndian format used as the record number for tables.
1021**
1022** The bigEndian format (most significant byte first) is used for
1023** record numbers so that records will sort into the correct order
1024** even though memcmp() is used to compare the keys. On machines
1025** whose native integer format is little endian (ex: i486) the
1026** order of bytes is reversed. On native big-endian machines
1027** (ex: Alpha, Sparc, Motorola) the byte order is the same.
1028**
1029** This function is its own inverse. In other words
1030**
1031** X == byteSwap(byteSwap(X))
1032*/
danielk19774adee202004-05-08 08:23:19 +00001033int sqlite3VdbeByteSwap(int x){
drha11846b2004-01-07 18:52:56 +00001034 union {
1035 char zBuf[sizeof(int)];
1036 int i;
1037 } ux;
1038 ux.zBuf[3] = x&0xff;
1039 ux.zBuf[2] = (x>>8)&0xff;
1040 ux.zBuf[1] = (x>>16)&0xff;
1041 ux.zBuf[0] = (x>>24)&0xff;
1042 return ux.i;
1043}
1044
1045/*
1046** If a MoveTo operation is pending on the given cursor, then do that
1047** MoveTo now. Return an error code. If no MoveTo is pending, this
1048** routine does nothing and returns SQLITE_OK.
1049*/
danielk19774adee202004-05-08 08:23:19 +00001050int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001051 if( p->deferredMoveto ){
1052 int res;
danielk1977132872b2004-05-10 10:37:18 +00001053 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001054 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001055 if( p->intKey ){
1056 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1057 }else{
1058 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1059 }
danielk19773d1bfea2004-05-14 11:00:53 +00001060 p->incrKey = 0;
drha11846b2004-01-07 18:52:56 +00001061 p->lastRecno = keyToInt(p->movetoTarget);
1062 p->recnoIsValid = res==0;
1063 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001064 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001065 }
danielk1977132872b2004-05-10 10:37:18 +00001066 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001067 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001068 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001069 }
1070 return SQLITE_OK;
1071}
danielk19774adee202004-05-08 08:23:19 +00001072
danielk1977189621d2004-05-09 23:23:56 +00001073/*
1074** FIX ME
1075**
1076** This function is included temporarily so that regression tests have
1077** a chance of passing. It always uses memcmp().
1078*/
1079int sqlite2BtreeKeyCompare(
1080 BtCursor *pCur, /* Pointer to entry to compare against */
1081 const void *pKey, /* Key to compare against entry that pCur points to */
1082 int nKey, /* Number of bytes in pKey */
1083 int nIgnore, /* Ignore this many bytes at the end of pCur */
1084 int *pResult /* Write the result here */
1085){
drh0e1c19e2004-05-11 00:58:56 +00001086 const void *pCellKey;
1087 void *pMallocedKey;
danielk1977189621d2004-05-09 23:23:56 +00001088 u64 nCellKey;
1089 int rc;
1090
1091 sqlite3BtreeKeySize(pCur, &nCellKey);
1092 nCellKey = nCellKey - nIgnore;
1093 if( nCellKey<=0 ){
1094 *pResult = 0;
1095 return SQLITE_OK;
1096 }
1097
drh0e1c19e2004-05-11 00:58:56 +00001098 pCellKey = sqlite3BtreeKeyFetch(pCur, nCellKey);
danielk1977189621d2004-05-09 23:23:56 +00001099 if( pCellKey ){
1100 *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey);
1101 return SQLITE_OK;
1102 }
1103
drh0e1c19e2004-05-11 00:58:56 +00001104 pMallocedKey = sqliteMalloc( nCellKey );
1105 if( pMallocedKey==0 ) return SQLITE_NOMEM;
danielk1977189621d2004-05-09 23:23:56 +00001106
drh0e1c19e2004-05-11 00:58:56 +00001107 rc = sqlite3BtreeKey(pCur, 0, nCellKey, pMallocedKey);
1108 *pResult = memcmp(pMallocedKey, pKey, nKey>nCellKey?nCellKey:nKey);
1109 sqliteFree(pMallocedKey);
danielk1977189621d2004-05-09 23:23:56 +00001110
1111 return rc;
1112}
danielk19774adee202004-05-08 08:23:19 +00001113
drhab9f7f12004-05-08 10:56:11 +00001114/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001115** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001116**
danielk1977cfcdaef2004-05-12 07:33:33 +00001117** sqlite3VdbeSerialType()
1118** sqlite3VdbeSerialTypeLen()
1119** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001120** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001121** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001122**
1123** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001124** data and index records. Each serialized value consists of a
1125** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1126** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001127**
danielk1977cfcdaef2004-05-12 07:33:33 +00001128** In an SQLite index record, the serial type is stored directly before
1129** the blob of data that it corresponds to. In a table record, all serial
1130** types are stored at the start of the record, and the blobs of data at
1131** the end. Hence these functions allow the caller to handle the
1132** serial-type and data blob seperately.
1133**
1134** The following table describes the various storage classes for data:
1135**
1136** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001137** -------------- --------------- ---------------
danielk1977183f9f72004-05-13 05:20:26 +00001138** 0 - Not a type.
danielk197790e4d952004-05-10 10:05:53 +00001139** 1 1 signed integer
1140** 2 2 signed integer
1141** 3 4 signed integer
1142** 4 8 signed integer
1143** 5 8 IEEE float
danielk1977183f9f72004-05-13 05:20:26 +00001144** 6 0 NULL
1145** 7..11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001146** N>=12 and even (N-12)/2 BLOB
1147** N>=13 and odd (N-13)/2 text
1148**
1149*/
1150
1151/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001152** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001153*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001154u64 sqlite3VdbeSerialType(const Mem *pMem){
1155 int flags = pMem->flags;
1156
1157 if( flags&MEM_Null ){
danielk1977183f9f72004-05-13 05:20:26 +00001158 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001159 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001160 if( flags&MEM_Int ){
1161 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1162 i64 i = pMem->i;
1163 if( i>=-127 && i<=127 ) return 1;
1164 if( i>=-32767 && i<=32767 ) return 2;
1165 if( i>=-2147483647 && i<=2147483647 ) return 3;
1166 return 4;
danielk197790e4d952004-05-10 10:05:53 +00001167 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001168 if( flags&MEM_Real ){
1169 return 5;
danielk197790e4d952004-05-10 10:05:53 +00001170 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001171 if( flags&MEM_Str ){
1172 return (pMem->n*2 + 13);
danielk197790e4d952004-05-10 10:05:53 +00001173 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001174 if( flags&MEM_Blob ){
1175 return (pMem->n*2 + 12);
1176 }
1177 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001178}
1179
1180/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001181** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001182*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001183int sqlite3VdbeSerialTypeLen(u64 serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001184 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001185 switch(serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001186 case 6: return 0; /* NULL */
danielk1977cfcdaef2004-05-12 07:33:33 +00001187 case 1: return 1; /* 1 byte integer */
1188 case 2: return 2; /* 2 byte integer */
1189 case 3: return 4; /* 4 byte integer */
1190 case 4: return 8; /* 8 byte integer */
1191 case 5: return 8; /* 8 byte float */
danielk197790e4d952004-05-10 10:05:53 +00001192 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001193 assert( serial_type>=12 );
1194 return ((serial_type-12)>>1); /* text or blob */
danielk1977192ac1d2004-05-10 07:17:30 +00001195}
1196
1197/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001198** Write the serialized data blob for the value stored in pMem into
1199** buf. It is assumed that the caller has allocated sufficient space.
1200** Return the number of bytes written.
1201*/
1202int sqlite3VdbeSerialPut(unsigned char *buf, const Mem *pMem){
1203 u64 serial_type = sqlite3VdbeSerialType(pMem);
1204 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001205
1206 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001207
1208 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001209 if( serial_type==6 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001210 return 0;
1211 }
1212
1213 /* Integer */
1214 if( serial_type<5 ){
1215 i64 i = pMem->i;
1216 len = sqlite3VdbeSerialTypeLen(serial_type);
1217 while( len-- ){
1218 buf[len] = (i&0xFF);
1219 i = i >> 8;
1220 }
1221 return sqlite3VdbeSerialTypeLen(serial_type);
1222 }
1223
1224 /* Float */
1225 if( serial_type==5 ){
1226 /* TODO: byte ordering? */
1227 assert( sizeof(double)==8 );
1228 memcpy(buf, &pMem->r, 8);
1229 return 8;
1230 }
1231
1232 /* String or blob */
1233 assert( serial_type>=12 );
1234 len = sqlite3VdbeSerialTypeLen(serial_type);
1235 memcpy(buf, pMem->z, len);
1236 return len;
1237}
1238
1239/*
1240** Deserialize the data blob pointed to by buf as serial type serial_type
1241** and store the result in pMem. Return the number of bytes read.
1242*/
1243int sqlite3VdbeSerialGet(const unsigned char *buf, u64 serial_type, Mem *pMem){
danielk197790e4d952004-05-10 10:05:53 +00001244 int len;
1245
danielk1977183f9f72004-05-13 05:20:26 +00001246 assert( serial_type!=0 );
1247
danielk1977cfcdaef2004-05-12 07:33:33 +00001248 /* memset(pMem, 0, sizeof(pMem)); */
1249 pMem->flags = 0;
1250 pMem->z = 0;
danielk197790e4d952004-05-10 10:05:53 +00001251
danielk1977cfcdaef2004-05-12 07:33:33 +00001252 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001253 if( serial_type==6 ){
danielk197790e4d952004-05-10 10:05:53 +00001254 pMem->flags = MEM_Null;
danielk1977cfcdaef2004-05-12 07:33:33 +00001255 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001256 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001257
1258 /* Integer */
1259 if( serial_type<5 ){
1260 i64 i = 0;
1261 int n;
1262 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk197790e4d952004-05-10 10:05:53 +00001263
danielk1977cfcdaef2004-05-12 07:33:33 +00001264 if( buf[0]&0x80 ){
1265 for(n=0; n<(8-len); n++){
1266 i = (i<<8)+0xFF;
1267 }
1268 }
1269 for(n=0; n<len; n++){
1270 i = i << 8;
1271 i = i + buf[n];
1272 }
danielk197790e4d952004-05-10 10:05:53 +00001273 pMem->flags = MEM_Int;
danielk1977cfcdaef2004-05-12 07:33:33 +00001274 pMem->i = i;
1275 return sqlite3VdbeSerialTypeLen(serial_type);
danielk197790e4d952004-05-10 10:05:53 +00001276 }
1277
danielk1977cfcdaef2004-05-12 07:33:33 +00001278 /* Float */
1279 if( serial_type==5 ){
1280 /* TODO: byte ordering? */
1281 assert( sizeof(double)==8 );
1282 memcpy(&pMem->r, buf, 8);
1283 pMem->flags = MEM_Real;
1284 return 8;
danielk197790e4d952004-05-10 10:05:53 +00001285 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001286
1287 /* String or blob */
1288 assert( serial_type>=12 );
1289 if( serial_type&0x01 ){
1290 pMem->flags = MEM_Str;
1291 }else{
1292 pMem->flags = MEM_Blob;
1293 }
1294 len = sqlite3VdbeSerialTypeLen(serial_type);
1295 pMem->n = len;
danielk197790e4d952004-05-10 10:05:53 +00001296 if( len>NBFS ){
drhfa1a98a2004-05-14 19:08:17 +00001297 pMem->z = sqliteMallocRaw( len );
danielk197790e4d952004-05-10 10:05:53 +00001298 if( !pMem->z ){
1299 return -1;
1300 }
1301 pMem->flags |= MEM_Dyn;
1302 }else{
1303 pMem->z = pMem->zShort;
1304 pMem->flags |= MEM_Short;
1305 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001306 memcpy(pMem->z, buf, len);
danielk197790e4d952004-05-10 10:05:53 +00001307
danielk1977cfcdaef2004-05-12 07:33:33 +00001308 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001309}
1310
1311/*
danielk19778d059842004-05-12 11:24:02 +00001312** Compare the values contained by the two memory cells, returning
1313** negative, zero or positive if pMem1 is less than, equal to, or greater
1314** than pMem2. Sorting order is NULL's first, followed by numbers (integers
1315** and reals) sorted numerically, followed by text ordered by memcmp() and
1316** finally blob's ordered by memcmp().
1317**
1318** Two NULL values are considered equal by this function.
1319*/
1320int compareMemCells(Mem *pMem1, Mem *pMem2){
1321 int rc;
1322 int combined_flags = pMem1->flags|pMem2->flags;
1323
1324 /* If one value is NULL, it is less than the other. If both values
1325 ** are NULL, return 0.
1326 */
1327 if( combined_flags&MEM_Null ){
1328 return (pMem2->flags&MEM_Null) - (pMem1->flags&MEM_Null);
1329 }
1330
1331 /* If one value is a number and the other is not, the number is less.
1332 ** If both are numbers, compare as reals if one is a real, or as integers
1333 ** if both values are integers.
1334 */
1335 if( combined_flags&(MEM_Int|MEM_Real) ){
1336 if( !(pMem1->flags&(MEM_Int|MEM_Real)) ){
1337 return 1;
1338 }
1339 if( !(pMem2->flags&(MEM_Int|MEM_Real)) ){
1340 return -1;
1341 }
1342
1343 if( combined_flags&MEM_Real ){
1344 if( pMem1->flags&MEM_Int ){
1345 pMem1->r = pMem1->i;
1346 }
1347 if( pMem2->flags&MEM_Int ){
1348 pMem2->r = pMem2->i;
1349 }
1350 if( pMem1->r < pMem2->r ) return -1;
1351 if( pMem1->r > pMem2->r ) return 1;
1352 return 0;
1353 }
1354
danielk19773d1bfea2004-05-14 11:00:53 +00001355 return (pMem1->i - pMem2->i);
1356 }
1357
1358 rc = (pMem2->flags&MEM_Null) - (pMem1->flags&MEM_Null);
1359 if( rc ){
1360 return rc;
danielk19778d059842004-05-12 11:24:02 +00001361 }
1362
1363 /* Both values must be strings or blobs. If only one is a string, then
1364 ** that value is less. Otherwise, compare with memcmp(). If memcmp()
1365 ** returns 0 and one value is longer than the other, then that value
1366 ** is greater.
1367 */
danielk19778d059842004-05-12 11:24:02 +00001368 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
1369 if( rc ){
1370 return rc;
1371 }
1372
danielk19773d1bfea2004-05-14 11:00:53 +00001373 return (pMem1->n - pMem2->n);
danielk19778d059842004-05-12 11:24:02 +00001374}
1375
1376/*
drhab9f7f12004-05-08 10:56:11 +00001377** The following is the comparison function for (non-integer)
1378** keys in the btrees. This function returns negative, zero, or
1379** positive if the first key is less than, equal to, or greater than
1380** the second.
1381**
danielk19778d059842004-05-12 11:24:02 +00001382** This function assumes that each key consists of one or more type/blob
danielk1977183f9f72004-05-13 05:20:26 +00001383** pairs, encoded using the sqlite3VdbeSerialXXX() functions above.
1384**
1385** Following the type/blob pairs, each key may have a single 0x00 byte
1386** followed by a varint. A key may only have this traling 0x00/varint
1387** pair if it has at least as many type/blob pairs as the key it is being
1388** compared to.
drhab9f7f12004-05-08 10:56:11 +00001389*/
drhab9f7f12004-05-08 10:56:11 +00001390int sqlite3VdbeKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001391 void *userData,
danielk1977183f9f72004-05-13 05:20:26 +00001392 int nKey1, const void *pKey1,
1393 int nKey2, const void *pKey2
drhab9f7f12004-05-08 10:56:11 +00001394){
danielk19773d1bfea2004-05-14 11:00:53 +00001395 Cursor *pC = (Cursor *)userData;
danielk19778d059842004-05-12 11:24:02 +00001396 int offset1 = 0;
1397 int offset2 = 0;
danielk1977183f9f72004-05-13 05:20:26 +00001398 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1399 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1400
danielk19778d059842004-05-12 11:24:02 +00001401 while( offset1<nKey1 && offset2<nKey2 ){
1402 Mem mem1;
1403 Mem mem2;
1404 u64 serial_type1;
1405 u64 serial_type2;
1406 int rc;
1407
danielk1977183f9f72004-05-13 05:20:26 +00001408 /* Read the serial types for the next element in each key. */
danielk19778d059842004-05-12 11:24:02 +00001409 offset1 += sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1410 offset2 += sqlite3GetVarint(&aKey2[offset2], &serial_type2);
danielk1977183f9f72004-05-13 05:20:26 +00001411
1412 /* If either of the varints just read in are 0 (not a type), then
1413 ** this is the end of the keys. The remaining data in each key is
1414 ** the varint rowid. Compare these as signed integers and return
1415 ** the result.
1416 */
1417 if( !serial_type1 || !serial_type2 ){
1418 assert( !serial_type1 && !serial_type2 );
danielk19773d1bfea2004-05-14 11:00:53 +00001419 assert( !pC || !pC->incrKey );
danielk1977183f9f72004-05-13 05:20:26 +00001420 sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1421 sqlite3GetVarint(&aKey2[offset2], &serial_type2);
1422 return ( (i64)serial_type1 - (i64)serial_type2 );
1423 }
1424
1425 /* Assert that there is enough space left in each key for the blob of
1426 ** data to go with the serial type just read. This assert may fail if
1427 ** the file is corrupted. Then read the value from each key into mem1
1428 ** and mem2 respectively.
1429 */
danielk19778d059842004-05-12 11:24:02 +00001430 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1);
1431 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2);
1432
1433 rc = compareMemCells(&mem1, &mem2);
1434 if( mem1.flags&MEM_Dyn ){
1435 sqliteFree(mem1.z);
1436 }
1437 if( mem2.flags&MEM_Dyn ){
1438 sqliteFree(mem2.z);
1439 }
1440 if( rc!=0 ){
1441 return rc;
1442 }
1443 }
1444
danielk19773d1bfea2004-05-14 11:00:53 +00001445 /* One of the keys ran out of fields, but all the fields up to that point
1446 ** were equal. If the incrKey flag is true, then the second key is
1447 ** treated as larger.
1448 */
1449 if( pC && pC->incrKey ){
1450 assert( offset2==nKey2 );
1451 return -1;
1452 }
1453
danielk19778d059842004-05-12 11:24:02 +00001454 if( offset1<nKey1 ){
1455 return 1;
1456 }
1457 if( offset2<nKey2 ){
1458 return -1;
drhab9f7f12004-05-08 10:56:11 +00001459 }
danielk19773d1bfea2004-05-14 11:00:53 +00001460
1461return_result:
1462
drhab9f7f12004-05-08 10:56:11 +00001463 return 0;
drhab9f7f12004-05-08 10:56:11 +00001464}
danielk1977183f9f72004-05-13 05:20:26 +00001465
1466/*
1467** pCur points at an index entry. Read the rowid (varint occuring at
1468** the end of the entry and store it in *rowid. Return SQLITE_OK if
1469** everything works, or an error code otherwise.
1470*/
1471int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1472 i64 sz;
1473 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001474 char buf[10];
danielk1977183f9f72004-05-13 05:20:26 +00001475 int len;
1476 u64 r;
1477
1478 rc = sqlite3BtreeKeySize(pCur, &sz);
1479 if( rc!=SQLITE_OK ){
1480 return rc;
1481 }
danielk19773d1bfea2004-05-14 11:00:53 +00001482 len = ((sz>10)?10:sz);
1483
1484 /* If there are less than 2 bytes in the key, this cannot be
1485 ** a valid index entry. In practice this comes up for a query
1486 ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table
1487 ** with an index on x. In this case just call the rowid 0.
1488 */
1489 if( len<2 ){
1490 *rowid = 0;
1491 return SQLITE_OK;
1492 }
danielk1977183f9f72004-05-13 05:20:26 +00001493
1494 rc = sqlite3BtreeKey(pCur, sz-len, len, buf);
1495 if( rc!=SQLITE_OK ){
1496 return rc;
1497 }
1498
danielk19773d1bfea2004-05-14 11:00:53 +00001499 len--;
1500 while( buf[len-1] && --len );
danielk1977183f9f72004-05-13 05:20:26 +00001501
danielk19773d1bfea2004-05-14 11:00:53 +00001502 sqlite3GetVarint(&buf[len], &r);
danielk1977183f9f72004-05-13 05:20:26 +00001503 *rowid = r;
1504 return SQLITE_OK;
1505}
1506
1507int sqlite3VdbeIdxKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001508 Cursor *pC,
danielk1977183f9f72004-05-13 05:20:26 +00001509 int nKey, const unsigned char *pKey,
1510 int ignorerowid,
1511 int *res
1512){
1513 unsigned char *pCellKey;
1514 u64 nCellKey;
1515 int freeCellKey = 0;
1516 int rc;
1517 int len;
danielk19773d1bfea2004-05-14 11:00:53 +00001518 BtCursor *pCur = pC->pCursor;
danielk1977183f9f72004-05-13 05:20:26 +00001519
1520 sqlite3BtreeKeySize(pCur, &nCellKey);
1521 if( nCellKey<=0 ){
1522 *res = 0;
1523 return SQLITE_OK;
1524 }
1525
1526 pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey);
1527 if( !pCellKey ){
drh10617cd2004-05-14 15:27:27 +00001528 pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey);
danielk1977183f9f72004-05-13 05:20:26 +00001529 if( !pCellKey ){
1530 return SQLITE_NOMEM;
1531 }
1532 freeCellKey = 1;
1533 rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
1534 if( rc!=SQLITE_OK ){
1535 sqliteFree(pCellKey);
1536 return rc;
1537 }
1538 }
1539
1540 len = nCellKey-2;
1541 while( pCellKey[len] && --len );
1542
1543 if( ignorerowid ){
1544 nKey--;
1545 while( pKey[nKey] && --nKey );
1546 }
danielk19773d1bfea2004-05-14 11:00:53 +00001547 *res = sqlite3VdbeKeyCompare(pC, len, pCellKey, nKey, pKey);
danielk1977183f9f72004-05-13 05:20:26 +00001548
1549 if( freeCellKey ){
1550 sqliteFree(pCellKey);
1551 }
1552 return SQLITE_OK;
1553}