blob: c91d7a70a3471b22311aa146a6d51582b38a9222 [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
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*/
danielk19774adee202004-05-08 08:23:19 +0000112int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3, int p3type){
113 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/*
428** The following group or routines are employed by installable functions
429** to return their results.
430**
danielk1977132872b2004-05-10 10:37:18 +0000431** The sqlite3_set_result_string() routine can be used to return a string
drh9a324642003-09-06 20:12:01 +0000432** value or to return a NULL. To return a NULL, pass in NULL for zResult.
433** A copy is made of the string before this routine returns so it is safe
434** to pass in an ephemeral string.
435**
danielk1977132872b2004-05-10 10:37:18 +0000436** sqlite3_set_result_error() works like sqlite3_set_result_string() except
drh9a324642003-09-06 20:12:01 +0000437** that it signals a fatal error. The string argument, if any, is the
438** error message. If the argument is NULL a generic substitute error message
439** is used.
440**
danielk1977132872b2004-05-10 10:37:18 +0000441** The sqlite3_set_result_int() and sqlite3_set_result_double() set the return
drh9a324642003-09-06 20:12:01 +0000442** value of the user function to an integer or a double.
443**
444** These routines are defined here in vdbe.c because they depend on knowing
445** the internals of the sqlite_func structure which is only defined in
446** this source file.
447*/
danielk1977132872b2004-05-10 10:37:18 +0000448char *sqlite3_set_result_string(sqlite_func *p, const char *zResult, int n){
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 if( zResult==0 ){
drh00706be2004-01-30 14:49:16 +0000454 p->s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000455 n = 0;
drh00706be2004-01-30 14:49:16 +0000456 p->s.z = 0;
drh9a324642003-09-06 20:12:01 +0000457 p->s.n = 0;
458 }else{
459 if( n<0 ) n = strlen(zResult);
460 if( n<NBFS-1 ){
drh00706be2004-01-30 14:49:16 +0000461 memcpy(p->s.zShort, zResult, n);
462 p->s.zShort[n] = 0;
danielk1977106bb232004-05-21 10:08:53 +0000463 p->s.flags = MEM_Utf8 | MEM_Str | MEM_Short;
drh00706be2004-01-30 14:49:16 +0000464 p->s.z = p->s.zShort;
drh9a324642003-09-06 20:12:01 +0000465 }else{
drh00706be2004-01-30 14:49:16 +0000466 p->s.z = sqliteMallocRaw( n+1 );
467 if( p->s.z ){
468 memcpy(p->s.z, zResult, n);
469 p->s.z[n] = 0;
drh9a324642003-09-06 20:12:01 +0000470 }
danielk1977106bb232004-05-21 10:08:53 +0000471 p->s.flags = MEM_Utf8 | MEM_Str | MEM_Dyn;
drh9a324642003-09-06 20:12:01 +0000472 }
473 p->s.n = n+1;
474 }
drh00706be2004-01-30 14:49:16 +0000475 return p->s.z;
drh9a324642003-09-06 20:12:01 +0000476}
danielk1977132872b2004-05-10 10:37:18 +0000477void sqlite3_set_result_int(sqlite_func *p, int iResult){
drh9a324642003-09-06 20:12:01 +0000478 assert( !p->isStep );
drh00706be2004-01-30 14:49:16 +0000479 if( p->s.flags & MEM_Dyn ){
480 sqliteFree(p->s.z);
drh9a324642003-09-06 20:12:01 +0000481 }
482 p->s.i = iResult;
drh00706be2004-01-30 14:49:16 +0000483 p->s.flags = MEM_Int;
drh9a324642003-09-06 20:12:01 +0000484}
danielk1977132872b2004-05-10 10:37:18 +0000485void sqlite3_set_result_double(sqlite_func *p, double rResult){
drh9a324642003-09-06 20:12:01 +0000486 assert( !p->isStep );
drh00706be2004-01-30 14:49:16 +0000487 if( p->s.flags & MEM_Dyn ){
488 sqliteFree(p->s.z);
drh9a324642003-09-06 20:12:01 +0000489 }
490 p->s.r = rResult;
drh00706be2004-01-30 14:49:16 +0000491 p->s.flags = MEM_Real;
drh9a324642003-09-06 20:12:01 +0000492}
danielk1977132872b2004-05-10 10:37:18 +0000493void sqlite3_set_result_error(sqlite_func *p, const char *zMsg, int n){
drh9a324642003-09-06 20:12:01 +0000494 assert( !p->isStep );
danielk1977132872b2004-05-10 10:37:18 +0000495 sqlite3_set_result_string(p, zMsg, n);
drh9a324642003-09-06 20:12:01 +0000496 p->isError = 1;
497}
498
499/*
500** Extract the user data from a sqlite_func structure and return a
501** pointer to it.
drh9a324642003-09-06 20:12:01 +0000502*/
danielk1977132872b2004-05-10 10:37:18 +0000503void *sqlite3_user_data(sqlite_func *p){
drh9a324642003-09-06 20:12:01 +0000504 assert( p && p->pFunc );
505 return p->pFunc->pUserData;
506}
507
508/*
509** Allocate or return the aggregate context for a user function. A new
510** context is allocated on the first call. Subsequent calls return the
511** same context that was returned on prior calls.
512**
513** This routine is defined here in vdbe.c because it depends on knowing
514** the internals of the sqlite_func structure which is only defined in
515** this source file.
516*/
danielk1977132872b2004-05-10 10:37:18 +0000517void *sqlite3_aggregate_context(sqlite_func *p, int nByte){
drh9a324642003-09-06 20:12:01 +0000518 assert( p && p->pFunc && p->pFunc->xStep );
519 if( p->pAgg==0 ){
520 if( nByte<=NBFS ){
drh00706be2004-01-30 14:49:16 +0000521 p->pAgg = (void*)p->s.z;
522 memset(p->pAgg, 0, nByte);
drh9a324642003-09-06 20:12:01 +0000523 }else{
524 p->pAgg = sqliteMalloc( nByte );
525 }
526 }
527 return p->pAgg;
528}
529
530/*
531** Return the number of times the Step function of a aggregate has been
532** called.
533**
534** This routine is defined here in vdbe.c because it depends on knowing
535** the internals of the sqlite_func structure which is only defined in
536** this source file.
537*/
danielk1977132872b2004-05-10 10:37:18 +0000538int sqlite3_aggregate_count(sqlite_func *p){
drh9a324642003-09-06 20:12:01 +0000539 assert( p && p->pFunc && p->pFunc->xStep );
540 return p->cnt;
541}
542
drhd3d39e92004-05-20 22:16:29 +0000543/*
544** Compute a string that describes the P3 parameter for an opcode.
545** Use zTemp for any required temporary buffer space.
546*/
547static char *displayP3(Op *pOp, char *zTemp, int nTemp){
548 char *zP3;
549 assert( nTemp>=20 );
550 switch( pOp->p3type ){
551 case P3_POINTER: {
552 sprintf(zTemp, "ptr(%#x)", (int)pOp->p3);
553 zP3 = zTemp;
554 break;
555 }
556 case P3_KEYINFO: {
557 int i, j;
558 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
559 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
560 i = strlen(zTemp);
561 for(j=0; j<pKeyInfo->nField; j++){
562 CollSeq *pColl = pKeyInfo->aColl[j];
563 if( pColl ){
564 int n = strlen(pColl->zName);
565 if( i+n>nTemp-6 ){
566 strcpy(&zTemp[i],",...");
567 break;
568 }
569 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000570 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000571 zTemp[i++] = '-';
572 }
573 strcpy(&zTemp[i], pColl->zName);
574 i += n;
575 }else if( i+4<nTemp-6 ){
576 strcpy(&zTemp[i],",nil");
577 i += 4;
578 }
579 }
580 zTemp[i++] = ')';
581 zTemp[i] = 0;
582 assert( i<nTemp );
583 zP3 = zTemp;
584 break;
585 }
586 case P3_COLLSEQ: {
587 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000588 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000589 zP3 = zTemp;
590 break;
591 }
592 default: {
593 zP3 = pOp->p3;
594 if( zP3==0 ){
595 zP3 = "";
596 }
597 }
598 }
599 return zP3;
600}
601
602
drh9a324642003-09-06 20:12:01 +0000603#if !defined(NDEBUG) || defined(VDBE_PROFILE)
604/*
605** Print a single opcode. This routine is used for debugging only.
606*/
danielk19774adee202004-05-08 08:23:19 +0000607void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000608 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000609 char zPtr[50];
610 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
611 static const char *zFormat2 = "%4d %-13s %4d %4d %-20s -- %s\n";
drh9a324642003-09-06 20:12:01 +0000612 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000613 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
614#ifdef NDEBUG
615 fprintf(pOut, zFormat1,
616 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
617#else
618 fprintf(pOut, pOp->zComment ? zFormat2 : zFormat1,
619 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3,pOp->zComment);
620#endif
drh9a324642003-09-06 20:12:01 +0000621 fflush(pOut);
622}
623#endif
624
625/*
626** Give a listing of the program in the virtual machine.
627**
danielk19774adee202004-05-08 08:23:19 +0000628** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000629** running the code, it invokes the callback once for each instruction.
630** This feature is used to implement "EXPLAIN".
631*/
danielk19774adee202004-05-08 08:23:19 +0000632int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000633 Vdbe *p /* The VDBE */
634){
635 sqlite *db = p->db;
636 int i;
drh826fb5a2004-02-14 23:59:57 +0000637 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000638 static char *azColumnNames[] = {
639 "addr", "opcode", "p1", "p2", "p3",
640 "int", "text", "int", "int", "text",
641 0
642 };
643
drh9a324642003-09-06 20:12:01 +0000644 assert( p->explain );
danielk197718f41892004-05-22 07:27:46 +0000645
646 /* Even though this opcode does not put dynamic strings onto the
647 ** the stack, they may become dynamic if the user calls
648 ** sqlite3_column_data16(), causing a translation to UTF-16 encoding.
649 */
650 if( p->pTos==&p->aStack[4] ){
651 for(i=0; i<5; i++){
652 if( p->aStack[i].flags & MEM_Dyn ){
653 sqliteFree(p->aStack[i].z);
654 }
655 p->aStack[i].flags = 0;
656 }
657 }
658
drh9a324642003-09-06 20:12:01 +0000659 p->azColName = azColumnNames;
danielk197718f41892004-05-22 07:27:46 +0000660 p->resOnStack = 0;
661
662 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000663 if( i>=p->nOp ){
664 p->rc = SQLITE_OK;
665 rc = SQLITE_DONE;
666 }else if( db->flags & SQLITE_Interrupt ){
667 db->flags &= ~SQLITE_Interrupt;
668 if( db->magic!=SQLITE_MAGIC_BUSY ){
669 p->rc = SQLITE_MISUSE;
670 }else{
671 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000672 }
drh826fb5a2004-02-14 23:59:57 +0000673 rc = SQLITE_ERROR;
danielk1977132872b2004-05-10 10:37:18 +0000674 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000675 }else{
drhd3d39e92004-05-20 22:16:29 +0000676 Op *pOp = &p->aOp[i];
danielk197718f41892004-05-22 07:27:46 +0000677 p->aStack[0].flags = MEM_Int;
678 p->aStack[0].i = i; /* Program counter */
679 p->aStack[1].flags = MEM_Static|MEM_Str|MEM_Utf8|MEM_Term;
680 p->aStack[1].z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
681 p->aStack[2].flags = MEM_Int;
682 p->aStack[2].i = pOp->p1; /* P1 */
683 p->aStack[3].flags = MEM_Int;
684 p->aStack[3].i = pOp->p2; /* P2 */
685 p->aStack[4].flags = MEM_Str|MEM_Utf8|MEM_Term; /* P3 */
686 p->aStack[4].z = displayP3(pOp, p->aStack[4].zShort, NBFS);
687 if( p->aStack[4].z==p->aStack[4].zShort ){
688 p->aStack[4].flags |= MEM_Short;
689 }else{
690 p->aStack[4].flags |= MEM_Static;
691 }
drh826fb5a2004-02-14 23:59:57 +0000692 p->nResColumn = 5;
danielk197718f41892004-05-22 07:27:46 +0000693 p->pTos = &p->aStack[4];
drh826fb5a2004-02-14 23:59:57 +0000694 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000695 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000696 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000697 }
drh826fb5a2004-02-14 23:59:57 +0000698 return rc;
drh9a324642003-09-06 20:12:01 +0000699}
700
701/*
702** Prepare a virtual machine for execution. This involves things such
703** as allocating stack space and initializing the program counter.
704** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000705** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000706*/
danielk19774adee202004-05-08 08:23:19 +0000707void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000708 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000709 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000710 int isExplain /* True if the EXPLAIN keywords is present */
711){
712 int n;
713
714 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000715 assert( p->magic==VDBE_MAGIC_INIT );
716
717 /* Add a HALT instruction to the very end of the program.
718 */
719 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000720 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000721 }
722
723 /* No instruction ever pushes more than a single element onto the
724 ** stack. And the stack never grows on successive executions of the
725 ** same loop. So the total number of instructions is an upper bound
726 ** on the maximum stack depth required.
727 **
728 ** Allocation all the stack space we will ever need.
729 */
drh82a48512003-09-06 22:45:20 +0000730 if( p->aStack==0 ){
731 p->nVar = nVar;
732 assert( nVar>=0 );
733 n = isExplain ? 10 : p->nOp;
734 p->aStack = sqliteMalloc(
drh6810ce62004-01-31 19:22:56 +0000735 n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */
drh5a12e682004-05-19 11:24:25 +0000736 + p->nVar*sizeof(Mem) /* apVar */
drh82a48512003-09-06 22:45:20 +0000737 );
drh00706be2004-01-30 14:49:16 +0000738 p->zArgv = (char**)&p->aStack[n];
739 p->azColName = (char**)&p->zArgv[n];
drh5a12e682004-05-19 11:24:25 +0000740 p->apVar = (Mem *)&p->azColName[n];
danielk197754db47e2004-05-19 10:36:43 +0000741 for(n=0; n<p->nVar; n++){
drh5a12e682004-05-19 11:24:25 +0000742 p->apVar[n].flags = MEM_Null;
danielk197754db47e2004-05-19 10:36:43 +0000743 }
drh82a48512003-09-06 22:45:20 +0000744 }
drh9a324642003-09-06 20:12:01 +0000745
danielk19774adee202004-05-08 08:23:19 +0000746 sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
drh9a324642003-09-06 20:12:01 +0000747 p->agg.pSearch = 0;
748#ifdef MEMORY_DEBUG
danielk19774adee202004-05-08 08:23:19 +0000749 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000750 p->trace = stdout;
751 }
752#endif
drh6810ce62004-01-31 19:22:56 +0000753 p->pTos = &p->aStack[-1];
drh9a324642003-09-06 20:12:01 +0000754 p->pc = 0;
755 p->rc = SQLITE_OK;
756 p->uniqueCnt = 0;
757 p->returnDepth = 0;
758 p->errorAction = OE_Abort;
759 p->undoTransOnError = 0;
drh9a324642003-09-06 20:12:01 +0000760 p->popStack = 0;
761 p->explain |= isExplain;
762 p->magic = VDBE_MAGIC_RUN;
763#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000764 {
765 int i;
766 for(i=0; i<p->nOp; i++){
767 p->aOp[i].cnt = 0;
768 p->aOp[i].cycles = 0;
769 }
drh9a324642003-09-06 20:12:01 +0000770 }
771#endif
772}
773
774
775/*
776** Remove any elements that remain on the sorter for the VDBE given.
777*/
danielk19774adee202004-05-08 08:23:19 +0000778void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000779 while( p->pSort ){
780 Sorter *pSorter = p->pSort;
781 p->pSort = pSorter->pNext;
782 sqliteFree(pSorter->zKey);
783 sqliteFree(pSorter->pData);
784 sqliteFree(pSorter);
785 }
786}
787
788/*
drh9a324642003-09-06 20:12:01 +0000789** Reset an Agg structure. Delete all its contents.
790**
791** For installable aggregate functions, if the step function has been
792** called, make sure the finalizer function has also been called. The
793** finalizer might need to free memory that was allocated as part of its
794** private context. If the finalizer has not been called yet, call it
795** now.
796*/
danielk19774adee202004-05-08 08:23:19 +0000797void sqlite3VdbeAggReset(Agg *pAgg){
drh9a324642003-09-06 20:12:01 +0000798 int i;
799 HashElem *p;
800 for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
801 AggElem *pElem = sqliteHashData(p);
802 assert( pAgg->apFunc!=0 );
803 for(i=0; i<pAgg->nMem; i++){
804 Mem *pMem = &pElem->aMem[i];
drh00706be2004-01-30 14:49:16 +0000805 if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
drh9a324642003-09-06 20:12:01 +0000806 sqlite_func ctx;
807 ctx.pFunc = pAgg->apFunc[i];
drh00706be2004-01-30 14:49:16 +0000808 ctx.s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000809 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +0000810 ctx.cnt = pMem->i;
drh9a324642003-09-06 20:12:01 +0000811 ctx.isStep = 0;
812 ctx.isError = 0;
813 (*pAgg->apFunc[i]->xFinalize)(&ctx);
drh00706be2004-01-30 14:49:16 +0000814 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
drh9a324642003-09-06 20:12:01 +0000815 sqliteFree(pMem->z);
816 }
drh9cbe7ca2004-02-18 16:57:23 +0000817 if( ctx.s.flags & MEM_Dyn ){
818 sqliteFree(ctx.s.z);
819 }
drh00706be2004-01-30 14:49:16 +0000820 }else if( pMem->flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000821 sqliteFree(pMem->z);
822 }
823 }
824 sqliteFree(pElem);
825 }
danielk19774adee202004-05-08 08:23:19 +0000826 sqlite3HashClear(&pAgg->hash);
drh9a324642003-09-06 20:12:01 +0000827 sqliteFree(pAgg->apFunc);
828 pAgg->apFunc = 0;
829 pAgg->pCurrent = 0;
830 pAgg->pSearch = 0;
831 pAgg->nMem = 0;
832}
833
834/*
835** Delete a keylist
836*/
danielk19774adee202004-05-08 08:23:19 +0000837void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000838 while( p ){
839 Keylist *pNext = p->pNext;
840 sqliteFree(p);
841 p = pNext;
842 }
843}
844
845/*
846** Close a cursor and release all the resources that cursor happens
847** to hold.
848*/
danielk19774adee202004-05-08 08:23:19 +0000849void sqlite3VdbeCleanupCursor(Cursor *pCx){
drh9a324642003-09-06 20:12:01 +0000850 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000852 }
853 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000854 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000855 }
856 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000857 sqliteFree(pCx->aType);
drhd7556d22004-05-14 21:59:40 +0000858 memset(pCx, 0, sizeof(*pCx));
drh9a324642003-09-06 20:12:01 +0000859}
860
861/*
862** Close all cursors
863*/
864static void closeAllCursors(Vdbe *p){
865 int i;
866 for(i=0; i<p->nCursor; i++){
drhd7556d22004-05-14 21:59:40 +0000867 Cursor *pC = p->apCsr[i];
868 sqlite3VdbeCleanupCursor(pC);
869 sqliteFree(pC);
drh9a324642003-09-06 20:12:01 +0000870 }
drhd7556d22004-05-14 21:59:40 +0000871 sqliteFree(p->apCsr);
872 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000873 p->nCursor = 0;
874}
875
876/*
drh9a324642003-09-06 20:12:01 +0000877** Clean up the VM after execution.
878**
879** This routine will automatically close any cursors, lists, and/or
880** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000881** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000882*/
883static void Cleanup(Vdbe *p){
884 int i;
drh6810ce62004-01-31 19:22:56 +0000885 if( p->aStack ){
886 Mem *pTos = p->pTos;
887 while( pTos>=p->aStack ){
888 if( pTos->flags & MEM_Dyn ){
889 sqliteFree(pTos->z);
890 }
891 pTos--;
892 }
893 p->pTos = pTos;
894 }
drh9a324642003-09-06 20:12:01 +0000895 closeAllCursors(p);
896 if( p->aMem ){
897 for(i=0; i<p->nMem; i++){
drh00706be2004-01-30 14:49:16 +0000898 if( p->aMem[i].flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000899 sqliteFree(p->aMem[i].z);
900 }
901 }
902 }
903 sqliteFree(p->aMem);
904 p->aMem = 0;
905 p->nMem = 0;
906 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000907 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000908 p->pList = 0;
909 }
danielk19774adee202004-05-08 08:23:19 +0000910 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000911 if( p->pFile ){
912 if( p->pFile!=stdin ) fclose(p->pFile);
913 p->pFile = 0;
914 }
915 if( p->azField ){
916 sqliteFree(p->azField);
917 p->azField = 0;
918 }
919 p->nField = 0;
920 if( p->zLine ){
921 sqliteFree(p->zLine);
922 p->zLine = 0;
923 }
924 p->nLineAlloc = 0;
danielk19774adee202004-05-08 08:23:19 +0000925 sqlite3VdbeAggReset(&p->agg);
drh9a324642003-09-06 20:12:01 +0000926 if( p->aSet ){
927 for(i=0; i<p->nSet; i++){
danielk19774adee202004-05-08 08:23:19 +0000928 sqlite3HashClear(&p->aSet[i].hash);
drh9a324642003-09-06 20:12:01 +0000929 }
930 }
931 sqliteFree(p->aSet);
932 p->aSet = 0;
933 p->nSet = 0;
934 if( p->keylistStack ){
935 int ii;
936 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000938 }
939 sqliteFree(p->keylistStack);
940 p->keylistStackDepth = 0;
941 p->keylistStack = 0;
942 }
drh5f968432004-02-21 19:02:30 +0000943 sqliteFree(p->contextStack);
944 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000945 sqliteFree(p->zErrMsg);
946 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000947}
948
949/*
950** Clean up a VDBE after execution but do not delete the VDBE just yet.
951** Write any error messages into *pzErrMsg. Return the result code.
952**
953** After this routine is run, the VDBE should be ready to be executed
954** again.
955*/
danielk19774adee202004-05-08 08:23:19 +0000956int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000957 sqlite *db = p->db;
958 int i;
959
960 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000961 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000962 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
drh9a324642003-09-06 20:12:01 +0000963 return SQLITE_MISUSE;
964 }
965 if( p->zErrMsg ){
danielk1977106bb232004-05-21 10:08:53 +0000966 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
drh9a324642003-09-06 20:12:01 +0000967 if( pzErrMsg && *pzErrMsg==0 ){
968 *pzErrMsg = p->zErrMsg;
969 }else{
970 sqliteFree(p->zErrMsg);
971 }
972 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +0000973 }else if( p->rc ){
danielk1977132872b2004-05-10 10:37:18 +0000974 sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000975 sqlite3Error(p->db, p->rc, "%s", sqlite3_error_string(p->rc) , 0);
976 }else{
977 sqlite3Error(p->db, SQLITE_OK, 0);
drh9a324642003-09-06 20:12:01 +0000978 }
979 Cleanup(p);
980 if( p->rc!=SQLITE_OK ){
981 switch( p->errorAction ){
982 case OE_Abort: {
983 if( !p->undoTransOnError ){
984 for(i=0; i<db->nDb; i++){
985 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3BtreeRollbackStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000987 }
988 }
989 break;
990 }
991 /* Fall through to ROLLBACK */
992 }
993 case OE_Rollback: {
danielk19774adee202004-05-08 08:23:19 +0000994 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000995 db->flags &= ~SQLITE_InTrans;
996 db->onError = OE_Default;
997 break;
998 }
999 default: {
1000 if( p->undoTransOnError ){
danielk19774adee202004-05-08 08:23:19 +00001001 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +00001002 db->flags &= ~SQLITE_InTrans;
1003 db->onError = OE_Default;
1004 }
1005 break;
1006 }
1007 }
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3RollbackInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001009 }
1010 for(i=0; i<db->nDb; i++){
1011 if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){
danielk19774adee202004-05-08 08:23:19 +00001012 sqlite3BtreeCommitStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +00001013 db->aDb[i].inTrans = 1;
1014 }
1015 }
danielk1977132872b2004-05-10 10:37:18 +00001016 assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001017#ifdef VDBE_PROFILE
1018 {
1019 FILE *out = fopen("vdbe_profile.out", "a");
1020 if( out ){
1021 int i;
1022 fprintf(out, "---- ");
1023 for(i=0; i<p->nOp; i++){
1024 fprintf(out, "%02x", p->aOp[i].opcode);
1025 }
1026 fprintf(out, "\n");
1027 for(i=0; i<p->nOp; i++){
1028 fprintf(out, "%6d %10lld %8lld ",
1029 p->aOp[i].cnt,
1030 p->aOp[i].cycles,
1031 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1032 );
danielk19774adee202004-05-08 08:23:19 +00001033 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001034 }
1035 fclose(out);
1036 }
1037 }
1038#endif
1039 p->magic = VDBE_MAGIC_INIT;
1040 return p->rc;
1041}
1042
1043/*
1044** Clean up and delete a VDBE after execution. Return an integer which is
1045** the result code. Write any error message text into *pzErrMsg.
1046*/
danielk19774adee202004-05-08 08:23:19 +00001047int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +00001048 int rc;
1049 sqlite *db;
1050
1051 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +00001052 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +00001053 if( p->magic==VDBE_MAGIC_INIT ){
1054 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
1055 }
drh9a324642003-09-06 20:12:01 +00001056 return SQLITE_MISUSE;
1057 }
1058 db = p->db;
danielk19774adee202004-05-08 08:23:19 +00001059 rc = sqlite3VdbeReset(p, pzErrMsg);
1060 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001061 if( db->want_to_close && db->pVdbe==0 ){
danielk1977132872b2004-05-10 10:37:18 +00001062 sqlite3_close(db);
drh9a324642003-09-06 20:12:01 +00001063 }
drha1f9b5e2004-02-14 16:31:02 +00001064 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001065 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001066 }
drh9a324642003-09-06 20:12:01 +00001067 return rc;
1068}
1069
1070/*
danielk197754db47e2004-05-19 10:36:43 +00001071** Unbind the value bound to variable $i in virtual machine p. This is the
danielk19776622cce2004-05-20 11:00:52 +00001072** the same as binding a NULL value to the column. If the "i" parameter is
1073** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
1074**
1075** The error code stored in database p->db is overwritten with the return
1076** value in any case.
danielk197754db47e2004-05-19 10:36:43 +00001077*/
1078static int vdbeUnbind(Vdbe *p, int i){
1079 Mem *pVar;
1080 if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){
danielk19776622cce2004-05-20 11:00:52 +00001081 sqlite3Error(p->db, SQLITE_MISUSE, 0);
danielk197754db47e2004-05-19 10:36:43 +00001082 return SQLITE_MISUSE;
1083 }
1084 if( i<1 || i>p->nVar ){
danielk19776622cce2004-05-20 11:00:52 +00001085 sqlite3Error(p->db, SQLITE_RANGE, 0);
danielk197754db47e2004-05-19 10:36:43 +00001086 return SQLITE_RANGE;
1087 }
1088 i--;
drh5a12e682004-05-19 11:24:25 +00001089 pVar = &p->apVar[i];
danielk197754db47e2004-05-19 10:36:43 +00001090 if( pVar->flags&MEM_Dyn ){
1091 sqliteFree(pVar->z);
1092 }
1093 pVar->flags = MEM_Null;
danielk19776622cce2004-05-20 11:00:52 +00001094 sqlite3Error(p->db, SQLITE_OK, 0);
danielk197754db47e2004-05-19 10:36:43 +00001095 return SQLITE_OK;
1096}
1097
1098/*
1099** This routine is used to bind text or blob data to an SQL variable (a ?).
danielk19776622cce2004-05-20 11:00:52 +00001100** It may also be used to bind a NULL value, by setting zVal to 0. Any
1101** existing value is unbound.
1102**
1103** The error code stored in p->db is overwritten with the return value in
1104** all cases.
danielk197754db47e2004-05-19 10:36:43 +00001105*/
1106static int vdbeBindBlob(
1107 Vdbe *p, /* Virtual machine */
1108 int i, /* Var number to bind (numbered from 1 upward) */
1109 const char *zVal, /* Pointer to blob of data */
1110 int bytes, /* Number of bytes to copy */
1111 int copy, /* True to copy the memory, false to copy a pointer */
1112 int flags /* Valid combination of MEM_Blob, MEM_Str, MEM_UtfXX */
1113){
1114 Mem *pVar;
danielk19776622cce2004-05-20 11:00:52 +00001115 int rc;
danielk197754db47e2004-05-19 10:36:43 +00001116
danielk19776622cce2004-05-20 11:00:52 +00001117 rc = vdbeUnbind(p, i);
1118 if( rc!=SQLITE_OK ){
1119 return rc;
1120 }
drh5a12e682004-05-19 11:24:25 +00001121 pVar = &p->apVar[i-1];
danielk197754db47e2004-05-19 10:36:43 +00001122
1123 if( zVal ){
1124 pVar->n = bytes;
1125 pVar->flags = flags;
1126 if( !copy ){
1127 pVar->z = (char *)zVal;
1128 pVar->flags |= MEM_Static;
1129 }else{
1130 if( bytes>NBFS ){
1131 pVar->z = (char *)sqliteMalloc(bytes);
1132 if( !pVar->z ){
danielk19776622cce2004-05-20 11:00:52 +00001133 sqlite3Error(p->db, SQLITE_NOMEM, 0);
danielk197754db47e2004-05-19 10:36:43 +00001134 return SQLITE_NOMEM;
1135 }
1136 pVar->flags |= MEM_Dyn;
1137 }else{
1138 pVar->z = pVar->zShort;
1139 pVar->flags |= MEM_Short;
1140 }
1141 memcpy(pVar->z, zVal, bytes);
1142 }
1143 }
1144
1145 return SQLITE_OK;
1146}
1147
danielk1977e3209e42004-05-20 01:40:18 +00001148/*
1149** Bind a 64 bit integer to an SQL statement variable.
1150*/
danielk197754db47e2004-05-19 10:36:43 +00001151int sqlite3_bind_int64(sqlite3_stmt *p, int i, long long int iValue){
1152 int rc;
1153 Vdbe *v = (Vdbe *)p;
1154 rc = vdbeUnbind(v, i);
1155 if( rc==SQLITE_OK ){
drh5a12e682004-05-19 11:24:25 +00001156 Mem *pVar = &v->apVar[i-1];
danielk197754db47e2004-05-19 10:36:43 +00001157 pVar->flags = MEM_Int;
1158 pVar->i = iValue;
1159 }
danielk1977b1bc9532004-05-22 03:05:33 +00001160 return rc;
danielk197754db47e2004-05-19 10:36:43 +00001161}
1162
danielk1977e3209e42004-05-20 01:40:18 +00001163/*
1164** Bind a 32 bit integer to an SQL statement variable.
1165*/
danielk197754db47e2004-05-19 10:36:43 +00001166int sqlite3_bind_int32(sqlite3_stmt *p, int i, int iValue){
1167 return sqlite3_bind_int64(p, i, (long long int)iValue);
1168}
1169
danielk1977e3209e42004-05-20 01:40:18 +00001170/*
1171** Bind a double (real) to an SQL statement variable.
1172*/
danielk197754db47e2004-05-19 10:36:43 +00001173int sqlite3_bind_double(sqlite3_stmt *p, int i, double iValue){
1174 int rc;
1175 Vdbe *v = (Vdbe *)p;
1176 rc = vdbeUnbind(v, i);
1177 if( rc==SQLITE_OK ){
drh5a12e682004-05-19 11:24:25 +00001178 Mem *pVar = &v->apVar[i-1];
danielk197754db47e2004-05-19 10:36:43 +00001179 pVar->flags = MEM_Real;
1180 pVar->r = iValue;
1181 }
1182 return SQLITE_OK;
1183}
1184
danielk1977e3209e42004-05-20 01:40:18 +00001185/*
1186** Bind a NULL value to an SQL statement variable.
1187*/
danielk197754db47e2004-05-19 10:36:43 +00001188int sqlite3_bind_null(sqlite3_stmt* p, int i){
1189 return vdbeUnbind((Vdbe *)p, i);
1190}
1191
danielk1977e3209e42004-05-20 01:40:18 +00001192/*
1193** Bind a UTF-8 text value to an SQL statement variable.
1194*/
danielk197754db47e2004-05-19 10:36:43 +00001195int sqlite3_bind_text(
1196 sqlite3_stmt *p,
1197 int i,
1198 const char *zData,
1199 int nData,
1200 int eCopy
1201){
danielk197751e3d8e2004-05-20 01:12:34 +00001202 int flags = MEM_Str|MEM_Utf8;
1203 if( zData ){
1204 if( nData<0 ){
1205 nData = strlen(zData)+1;
1206 flags |= MEM_Term;
1207 }else if( !zData[nData-1] ){
1208 flags |= MEM_Term;
1209 }
danielk197754db47e2004-05-19 10:36:43 +00001210 }
danielk197751e3d8e2004-05-20 01:12:34 +00001211 return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, flags);
danielk197754db47e2004-05-19 10:36:43 +00001212}
1213
danielk1977e3209e42004-05-20 01:40:18 +00001214/*
1215** Bind a UTF-16 text value to an SQL statement variable.
1216*/
danielk197754db47e2004-05-19 10:36:43 +00001217int sqlite3_bind_text16(
1218 sqlite3_stmt *p,
1219 int i,
1220 const void *zData,
1221 int nData,
1222 int eCopy
1223){
danielk1977b1bc9532004-05-22 03:05:33 +00001224 int flags;
1225
1226 if( SQLITE3_BIGENDIAN ){
1227 flags = MEM_Str|MEM_Utf16be;
1228 }else{
1229 flags = MEM_Str|MEM_Utf16le;
1230 }
danielk197751e3d8e2004-05-20 01:12:34 +00001231
1232 if( zData ){
1233 /* If nData is less than zero, measure the length of the string.
1234 ** manually. In this case the variable will always be null terminated.
1235 */
1236 if( nData<0 ){
danielk19776622cce2004-05-20 11:00:52 +00001237 nData = sqlite3utf16ByteLen(zData, -1) + 2;
danielk197751e3d8e2004-05-20 01:12:34 +00001238 flags |= MEM_Term;
1239 }else{
1240 /* If nData is greater than zero, check if the final character appears
1241 ** to be a terminator.
1242 */
1243 if( !(((u8 *)zData)[nData-1]) && !(((u8 *)zData)[nData-2]) ){
1244 flags |= MEM_Term;
1245 }
1246 }
1247 }
danielk197754db47e2004-05-19 10:36:43 +00001248
danielk197751e3d8e2004-05-20 01:12:34 +00001249 return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, flags);
danielk197754db47e2004-05-19 10:36:43 +00001250}
1251
danielk1977e3209e42004-05-20 01:40:18 +00001252/*
1253** Bind a blob value to an SQL statement variable.
1254*/
danielk197754db47e2004-05-19 10:36:43 +00001255int sqlite3_bind_blob(
1256 sqlite3_stmt *p,
1257 int i,
1258 const void *zData,
1259 int nData,
1260 int eCopy
1261){
1262 return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, MEM_Blob);
1263}
1264
1265/*
drh9a324642003-09-06 20:12:01 +00001266** Set the values of all variables. Variable $1 in the original SQL will
1267** be the string azValue[0]. $2 will have the value azValue[1]. And
1268** so forth. If a value is out of range (for example $3 when nValue==2)
1269** then its value will be NULL.
1270**
1271** This routine overrides any prior call.
1272*/
danielk1977132872b2004-05-10 10:37:18 +00001273int sqlite3_bind(sqlite_vm *pVm, int i, const char *zVal, int len, int copy){
danielk197754db47e2004-05-19 10:36:43 +00001274 return sqlite3_bind_text(pVm, i, zVal, len, copy);
drh9a324642003-09-06 20:12:01 +00001275}
1276
drh9a324642003-09-06 20:12:01 +00001277/*
1278** Delete an entire VDBE.
1279*/
danielk19774adee202004-05-08 08:23:19 +00001280void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001281 int i;
1282 if( p==0 ) return;
1283 Cleanup(p);
1284 if( p->pPrev ){
1285 p->pPrev->pNext = p->pNext;
1286 }else{
1287 assert( p->db->pVdbe==p );
1288 p->db->pVdbe = p->pNext;
1289 }
1290 if( p->pNext ){
1291 p->pNext->pPrev = p->pPrev;
1292 }
1293 p->pPrev = p->pNext = 0;
1294 if( p->nOpAlloc==0 ){
1295 p->aOp = 0;
1296 p->nOp = 0;
1297 }
1298 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001299 Op *pOp = &p->aOp[i];
1300 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1301 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001302 }
drhd3d39e92004-05-20 22:16:29 +00001303#ifndef NDEBUG
1304 sqliteFree(pOp->zComment);
1305#endif
drh9a324642003-09-06 20:12:01 +00001306 }
drh7c972de2003-09-06 22:18:07 +00001307 for(i=0; i<p->nVar; i++){
drh5a12e682004-05-19 11:24:25 +00001308 if( p->apVar[i].flags&MEM_Dyn ){
1309 sqliteFree(p->apVar[i].z);
danielk197754db47e2004-05-19 10:36:43 +00001310 }
drh7c972de2003-09-06 22:18:07 +00001311 }
danielk1977ca6b2912004-05-21 10:49:47 +00001312 if( p->azColName16 ){
1313 for(i=0; i<p->nResColumn; i++){
1314 if( p->azColName16[i] ) sqliteFree(p->azColName16[i]);
1315 }
1316 sqliteFree(p->azColName16);
1317 }
drh9a324642003-09-06 20:12:01 +00001318 sqliteFree(p->aOp);
1319 sqliteFree(p->aLabel);
1320 sqliteFree(p->aStack);
1321 p->magic = VDBE_MAGIC_DEAD;
1322 sqliteFree(p);
1323}
drha11846b2004-01-07 18:52:56 +00001324
1325/*
drha11846b2004-01-07 18:52:56 +00001326** If a MoveTo operation is pending on the given cursor, then do that
1327** MoveTo now. Return an error code. If no MoveTo is pending, this
1328** routine does nothing and returns SQLITE_OK.
1329*/
danielk19774adee202004-05-08 08:23:19 +00001330int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001331 if( p->deferredMoveto ){
1332 int res;
danielk1977132872b2004-05-10 10:37:18 +00001333 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001334 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001335 if( p->intKey ){
1336 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1337 }else{
1338 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1339 }
drhd3d39e92004-05-20 22:16:29 +00001340 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001341 p->lastRecno = keyToInt(p->movetoTarget);
1342 p->recnoIsValid = res==0;
1343 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001344 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001345 }
danielk1977132872b2004-05-10 10:37:18 +00001346 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001347 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001348 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001349 }
1350 return SQLITE_OK;
1351}
danielk19774adee202004-05-08 08:23:19 +00001352
drhab9f7f12004-05-08 10:56:11 +00001353/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001354** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001355**
danielk1977cfcdaef2004-05-12 07:33:33 +00001356** sqlite3VdbeSerialType()
1357** sqlite3VdbeSerialTypeLen()
1358** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001359** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001360** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001361**
1362** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001363** data and index records. Each serialized value consists of a
1364** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1365** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001366**
danielk1977cfcdaef2004-05-12 07:33:33 +00001367** In an SQLite index record, the serial type is stored directly before
1368** the blob of data that it corresponds to. In a table record, all serial
1369** types are stored at the start of the record, and the blobs of data at
1370** the end. Hence these functions allow the caller to handle the
1371** serial-type and data blob seperately.
1372**
1373** The following table describes the various storage classes for data:
1374**
1375** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001376** -------------- --------------- ---------------
danielk1977183f9f72004-05-13 05:20:26 +00001377** 0 - Not a type.
danielk197790e4d952004-05-10 10:05:53 +00001378** 1 1 signed integer
1379** 2 2 signed integer
1380** 3 4 signed integer
1381** 4 8 signed integer
1382** 5 8 IEEE float
danielk1977183f9f72004-05-13 05:20:26 +00001383** 6 0 NULL
1384** 7..11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001385** N>=12 and even (N-12)/2 BLOB
1386** N>=13 and odd (N-13)/2 text
1387**
1388*/
1389
1390/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001391** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001392*/
danielk1977b1bc9532004-05-22 03:05:33 +00001393u64 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001394 int flags = pMem->flags;
1395
1396 if( flags&MEM_Null ){
danielk1977183f9f72004-05-13 05:20:26 +00001397 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001398 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001399 if( flags&MEM_Int ){
1400 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1401 i64 i = pMem->i;
1402 if( i>=-127 && i<=127 ) return 1;
1403 if( i>=-32767 && i<=32767 ) return 2;
1404 if( i>=-2147483647 && i<=2147483647 ) return 3;
1405 return 4;
danielk197790e4d952004-05-10 10:05:53 +00001406 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001407 if( flags&MEM_Real ){
1408 return 5;
danielk197790e4d952004-05-10 10:05:53 +00001409 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001410 if( flags&MEM_Str ){
danielk1977b1bc9532004-05-22 03:05:33 +00001411 u64 t;
danielk1977eb015e02004-05-18 01:31:14 +00001412 assert( pMem->n>0 );
danielk1977b1bc9532004-05-22 03:05:33 +00001413 t = (pMem->n*2) + 13;
1414 if( pMem->flags&MEM_Term ){
1415 t -= ((pMem->flags&MEM_Utf8)?2:4);
1416 }
1417 return t;
danielk197790e4d952004-05-10 10:05:53 +00001418 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001419 if( flags&MEM_Blob ){
1420 return (pMem->n*2 + 12);
1421 }
1422 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001423}
1424
1425/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001426** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001427*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001428int sqlite3VdbeSerialTypeLen(u64 serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001429 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001430 switch(serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001431 case 6: return 0; /* NULL */
danielk1977cfcdaef2004-05-12 07:33:33 +00001432 case 1: return 1; /* 1 byte integer */
1433 case 2: return 2; /* 2 byte integer */
1434 case 3: return 4; /* 4 byte integer */
1435 case 4: return 8; /* 8 byte integer */
1436 case 5: return 8; /* 8 byte float */
danielk197790e4d952004-05-10 10:05:53 +00001437 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001438 assert( serial_type>=12 );
1439 return ((serial_type-12)>>1); /* text or blob */
danielk1977192ac1d2004-05-10 07:17:30 +00001440}
1441
1442/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001443** Write the serialized data blob for the value stored in pMem into
1444** buf. It is assumed that the caller has allocated sufficient space.
1445** Return the number of bytes written.
1446*/
danielk1977b1bc9532004-05-22 03:05:33 +00001447int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001448 u64 serial_type = sqlite3VdbeSerialType(pMem);
1449 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001450
1451 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001452
1453 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001454 if( serial_type==6 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001455 return 0;
1456 }
1457
drh1483e142004-05-21 21:12:42 +00001458 /* Integer and Real */
1459 if( serial_type<=5 ){
1460 u64 v;
1461 int i;
1462 if( serial_type==5 ){
1463 v = *(u64*)&pMem->r;
1464 }else{
1465 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001466 }
drh1483e142004-05-21 21:12:42 +00001467 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1468 while( i-- ){
1469 buf[i] = (v&0xFF);
1470 v >>= 8;
1471 }
1472 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001473 }
1474
1475 /* String or blob */
1476 assert( serial_type>=12 );
1477 len = sqlite3VdbeSerialTypeLen(serial_type);
1478 memcpy(buf, pMem->z, len);
1479 return len;
1480}
1481
1482/*
1483** Deserialize the data blob pointed to by buf as serial type serial_type
1484** and store the result in pMem. Return the number of bytes read.
1485*/
danielk1977b1bc9532004-05-22 03:05:33 +00001486int sqlite3VdbeSerialGet(
1487 const unsigned char *buf,
1488 u64 serial_type,
1489 Mem *pMem,
1490 u8 enc
1491){
danielk197790e4d952004-05-10 10:05:53 +00001492 int len;
1493
danielk1977183f9f72004-05-13 05:20:26 +00001494 assert( serial_type!=0 );
1495
danielk1977cfcdaef2004-05-12 07:33:33 +00001496 /* memset(pMem, 0, sizeof(pMem)); */
1497 pMem->flags = 0;
1498 pMem->z = 0;
danielk197790e4d952004-05-10 10:05:53 +00001499
danielk1977cfcdaef2004-05-12 07:33:33 +00001500 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001501 if( serial_type==6 ){
danielk197790e4d952004-05-10 10:05:53 +00001502 pMem->flags = MEM_Null;
danielk1977cfcdaef2004-05-12 07:33:33 +00001503 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001504 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001505
drh1483e142004-05-21 21:12:42 +00001506 /* Integer and Real */
1507 if( serial_type<=5 ){
1508 u64 v = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001509 int n;
1510 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk197790e4d952004-05-10 10:05:53 +00001511
danielk1977cfcdaef2004-05-12 07:33:33 +00001512 if( buf[0]&0x80 ){
drh1483e142004-05-21 21:12:42 +00001513 v = -1;
danielk1977cfcdaef2004-05-12 07:33:33 +00001514 }
1515 for(n=0; n<len; n++){
drh1483e142004-05-21 21:12:42 +00001516 v = (v<<8) | buf[n];
danielk1977cfcdaef2004-05-12 07:33:33 +00001517 }
drh1483e142004-05-21 21:12:42 +00001518 if( serial_type==5 ){
1519 pMem->flags = MEM_Real;
1520 pMem->r = *(double*)&v;
1521 }else{
1522 pMem->flags = MEM_Int;
danielk1977b1bc9532004-05-22 03:05:33 +00001523 pMem->i = *(i64*)&v;
drh1483e142004-05-21 21:12:42 +00001524 }
1525 return len;
danielk197790e4d952004-05-10 10:05:53 +00001526 }
1527
danielk1977cfcdaef2004-05-12 07:33:33 +00001528 /* String or blob */
1529 assert( serial_type>=12 );
danielk1977eb015e02004-05-18 01:31:14 +00001530 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk1977cfcdaef2004-05-12 07:33:33 +00001531 if( serial_type&0x01 ){
danielk1977b1bc9532004-05-22 03:05:33 +00001532 switch( enc ){
1533 case TEXT_Utf8:
1534 pMem->flags = MEM_Str|MEM_Utf8|MEM_Term;
1535 break;
1536 case TEXT_Utf16le:
1537 pMem->flags = MEM_Str|MEM_Utf16le|MEM_Term;
1538 break;
1539 case TEXT_Utf16be:
1540 pMem->flags = MEM_Str|MEM_Utf16be|MEM_Term;
1541 break;
1542 assert(0);
1543 }
1544 pMem->n = len+(enc==TEXT_Utf8?1:2);
danielk1977cfcdaef2004-05-12 07:33:33 +00001545 }else{
1546 pMem->flags = MEM_Blob;
danielk1977eb015e02004-05-18 01:31:14 +00001547 pMem->n = len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001548 }
danielk1977eb015e02004-05-18 01:31:14 +00001549
1550 if( (pMem->n)>NBFS ){
1551 pMem->z = sqliteMallocRaw( pMem->n );
danielk197790e4d952004-05-10 10:05:53 +00001552 if( !pMem->z ){
1553 return -1;
1554 }
1555 pMem->flags |= MEM_Dyn;
1556 }else{
1557 pMem->z = pMem->zShort;
1558 pMem->flags |= MEM_Short;
1559 }
danielk1977eb015e02004-05-18 01:31:14 +00001560
danielk1977cfcdaef2004-05-12 07:33:33 +00001561 memcpy(pMem->z, buf, len);
danielk1977eb015e02004-05-18 01:31:14 +00001562 if( pMem->flags&MEM_Str ){
1563 pMem->z[len] = '\0';
danielk1977b1bc9532004-05-22 03:05:33 +00001564 if( enc!=TEXT_Utf8 ){
1565 pMem->z[len+1] = '\0';
1566 }
danielk1977eb015e02004-05-18 01:31:14 +00001567 }
danielk197790e4d952004-05-10 10:05:53 +00001568
danielk1977cfcdaef2004-05-12 07:33:33 +00001569 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001570}
1571
1572/*
danielk19778d059842004-05-12 11:24:02 +00001573** Compare the values contained by the two memory cells, returning
1574** negative, zero or positive if pMem1 is less than, equal to, or greater
1575** than pMem2. Sorting order is NULL's first, followed by numbers (integers
drhd3d39e92004-05-20 22:16:29 +00001576** and reals) sorted numerically, followed by text ordered by the collating
1577** sequence pColl and finally blob's ordered by memcmp().
danielk19778d059842004-05-12 11:24:02 +00001578**
1579** Two NULL values are considered equal by this function.
1580*/
drh53db1452004-05-20 13:54:53 +00001581int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
danielk19778d059842004-05-12 11:24:02 +00001582 int rc;
drh53db1452004-05-20 13:54:53 +00001583 int f1, f2;
1584 int combined_flags;
1585
1586 /* Interchange pMem1 and pMem2 if the collating sequence specifies
1587 ** DESC order.
1588 */
drh53db1452004-05-20 13:54:53 +00001589 f1 = pMem1->flags;
1590 f2 = pMem2->flags;
1591 combined_flags = f1|f2;
danielk19778d059842004-05-12 11:24:02 +00001592
1593 /* If one value is NULL, it is less than the other. If both values
1594 ** are NULL, return 0.
1595 */
1596 if( combined_flags&MEM_Null ){
drh53db1452004-05-20 13:54:53 +00001597 return (f2&MEM_Null) - (f1&MEM_Null);
danielk19778d059842004-05-12 11:24:02 +00001598 }
1599
1600 /* If one value is a number and the other is not, the number is less.
1601 ** If both are numbers, compare as reals if one is a real, or as integers
1602 ** if both values are integers.
1603 */
1604 if( combined_flags&(MEM_Int|MEM_Real) ){
drh53db1452004-05-20 13:54:53 +00001605 if( !(f1&(MEM_Int|MEM_Real)) ){
danielk19778d059842004-05-12 11:24:02 +00001606 return 1;
1607 }
drh53db1452004-05-20 13:54:53 +00001608 if( !(f2&(MEM_Int|MEM_Real)) ){
danielk19778d059842004-05-12 11:24:02 +00001609 return -1;
1610 }
drh53db1452004-05-20 13:54:53 +00001611 if( (f1 & f2 & MEM_Int)==0 ){
1612 double r1, r2;
1613 if( (f1&MEM_Real)==0 ){
1614 r1 = pMem1->i;
1615 }else{
1616 r1 = pMem1->r;
danielk19778d059842004-05-12 11:24:02 +00001617 }
drh53db1452004-05-20 13:54:53 +00001618 if( (f2&MEM_Real)==0 ){
1619 r2 = pMem2->i;
1620 }else{
1621 r2 = pMem2->r;
danielk19778d059842004-05-12 11:24:02 +00001622 }
drh53db1452004-05-20 13:54:53 +00001623 if( r1<r2 ) return -1;
1624 if( r1>r2 ) return 1;
1625 return 0;
1626 }else{
1627 assert( f1&MEM_Int );
1628 assert( f2&MEM_Int );
1629 if( pMem1->i < pMem2->i ) return -1;
1630 if( pMem1->i > pMem2->i ) return 1;
danielk19778d059842004-05-12 11:24:02 +00001631 return 0;
1632 }
danielk19773d1bfea2004-05-14 11:00:53 +00001633 }
1634
drh53db1452004-05-20 13:54:53 +00001635 /* If one value is a string and the other is a blob, the string is less.
1636 ** If both are strings, compare using the collating functions.
1637 */
1638 if( combined_flags&MEM_Str ){
1639 if( (f1 & MEM_Str)==0 ){
1640 return 1;
1641 }
1642 if( (f2 & MEM_Str)==0 ){
1643 return -1;
1644 }
1645 if( pColl && pColl->xCmp ){
1646 return pColl->xCmp(pColl->pUser, pMem1->n, pMem1->z, pMem2->n, pMem2->z);
1647 }else{
1648 /* If no collating sequence is defined, fall through into the
1649 ** blob case below and use memcmp() for the comparison. */
1650 }
danielk19778d059842004-05-12 11:24:02 +00001651 }
drh53db1452004-05-20 13:54:53 +00001652
1653 /* Both values must be blobs. Compare using memcmp().
danielk19778d059842004-05-12 11:24:02 +00001654 */
danielk19778d059842004-05-12 11:24:02 +00001655 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
drh53db1452004-05-20 13:54:53 +00001656 if( rc==0 ){
1657 rc = pMem1->n - pMem2->n;
danielk19778d059842004-05-12 11:24:02 +00001658 }
drh53db1452004-05-20 13:54:53 +00001659 return rc;
danielk19778d059842004-05-12 11:24:02 +00001660}
1661
1662/*
drhab9f7f12004-05-08 10:56:11 +00001663** The following is the comparison function for (non-integer)
1664** keys in the btrees. This function returns negative, zero, or
1665** positive if the first key is less than, equal to, or greater than
1666** the second.
1667**
danielk19778d059842004-05-12 11:24:02 +00001668** This function assumes that each key consists of one or more type/blob
danielk1977183f9f72004-05-13 05:20:26 +00001669** pairs, encoded using the sqlite3VdbeSerialXXX() functions above.
1670**
1671** Following the type/blob pairs, each key may have a single 0x00 byte
1672** followed by a varint. A key may only have this traling 0x00/varint
1673** pair if it has at least as many type/blob pairs as the key it is being
1674** compared to.
drhab9f7f12004-05-08 10:56:11 +00001675*/
drhab9f7f12004-05-08 10:56:11 +00001676int sqlite3VdbeKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001677 void *userData,
danielk1977183f9f72004-05-13 05:20:26 +00001678 int nKey1, const void *pKey1,
1679 int nKey2, const void *pKey2
drhab9f7f12004-05-08 10:56:11 +00001680){
drhd3d39e92004-05-20 22:16:29 +00001681 KeyInfo *pKeyInfo = (KeyInfo*)userData;
danielk19778d059842004-05-12 11:24:02 +00001682 int offset1 = 0;
1683 int offset2 = 0;
drhd3d39e92004-05-20 22:16:29 +00001684 int i = 0;
drhffbc3082004-05-21 01:29:06 +00001685 int rc = 0;
danielk1977b1bc9532004-05-22 03:05:33 +00001686 u8 enc = pKeyInfo->enc;
danielk1977183f9f72004-05-13 05:20:26 +00001687 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1688 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1689
drhd3d39e92004-05-20 22:16:29 +00001690 assert( pKeyInfo!=0 );
danielk19778d059842004-05-12 11:24:02 +00001691 while( offset1<nKey1 && offset2<nKey2 ){
1692 Mem mem1;
1693 Mem mem2;
1694 u64 serial_type1;
1695 u64 serial_type2;
danielk19778d059842004-05-12 11:24:02 +00001696
danielk1977183f9f72004-05-13 05:20:26 +00001697 /* Read the serial types for the next element in each key. */
danielk19778d059842004-05-12 11:24:02 +00001698 offset1 += sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1699 offset2 += sqlite3GetVarint(&aKey2[offset2], &serial_type2);
danielk1977183f9f72004-05-13 05:20:26 +00001700
1701 /* If either of the varints just read in are 0 (not a type), then
1702 ** this is the end of the keys. The remaining data in each key is
1703 ** the varint rowid. Compare these as signed integers and return
1704 ** the result.
1705 */
1706 if( !serial_type1 || !serial_type2 ){
1707 assert( !serial_type1 && !serial_type2 );
1708 sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1709 sqlite3GetVarint(&aKey2[offset2], &serial_type2);
drhffbc3082004-05-21 01:29:06 +00001710 if( serial_type1 < serial_type2 ){
1711 rc = -1;
1712 }else if( serial_type1 > serial_type2 ){
1713 rc = +1;
1714 }else{
1715 rc = 0;
1716 }
1717 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001718 }
1719
drhd3d39e92004-05-20 22:16:29 +00001720 assert( i<pKeyInfo->nField );
1721
danielk1977183f9f72004-05-13 05:20:26 +00001722 /* Assert that there is enough space left in each key for the blob of
1723 ** data to go with the serial type just read. This assert may fail if
1724 ** the file is corrupted. Then read the value from each key into mem1
1725 ** and mem2 respectively.
1726 */
danielk1977b1bc9532004-05-22 03:05:33 +00001727 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
1728 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);
danielk19778d059842004-05-12 11:24:02 +00001729
drhd3d39e92004-05-20 22:16:29 +00001730 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk19778d059842004-05-12 11:24:02 +00001731 if( mem1.flags&MEM_Dyn ){
1732 sqliteFree(mem1.z);
1733 }
1734 if( mem2.flags&MEM_Dyn ){
1735 sqliteFree(mem2.z);
1736 }
1737 if( rc!=0 ){
drhffbc3082004-05-21 01:29:06 +00001738 break;
danielk19778d059842004-05-12 11:24:02 +00001739 }
drhd3d39e92004-05-20 22:16:29 +00001740 i++;
danielk19778d059842004-05-12 11:24:02 +00001741 }
1742
danielk19773d1bfea2004-05-14 11:00:53 +00001743 /* One of the keys ran out of fields, but all the fields up to that point
1744 ** were equal. If the incrKey flag is true, then the second key is
1745 ** treated as larger.
1746 */
drhffbc3082004-05-21 01:29:06 +00001747 if( rc==0 ){
1748 if( pKeyInfo->incrKey ){
1749 assert( offset2==nKey2 );
1750 rc = -1;
1751 }else if( offset1<nKey1 ){
1752 rc = 1;
1753 }else if( offset2<nKey2 ){
1754 rc = -1;
1755 }
danielk19773d1bfea2004-05-14 11:00:53 +00001756 }
1757
drhffbc3082004-05-21 01:29:06 +00001758 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1759 rc = -rc;
drhab9f7f12004-05-08 10:56:11 +00001760 }
danielk19773d1bfea2004-05-14 11:00:53 +00001761
drhffbc3082004-05-21 01:29:06 +00001762 return rc;
drhab9f7f12004-05-08 10:56:11 +00001763}
danielk1977183f9f72004-05-13 05:20:26 +00001764
1765/*
danielk1977eb015e02004-05-18 01:31:14 +00001766** This function compares the two table row records specified by
1767** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1768** or positive integer if {nKey1, pKey1} is less than, equal to or
1769** greater than {nKey2, pKey2}.
1770**
danielk197784ac9d02004-05-18 09:58:06 +00001771** This function is pretty inefficient and will probably be replaced
danielk1977eb015e02004-05-18 01:31:14 +00001772** by something else in the near future. It is currently required
1773** by compound SELECT operators.
1774*/
1775int sqlite3VdbeRowCompare(
1776 void *userData,
1777 int nKey1, const void *pKey1,
1778 int nKey2, const void *pKey2
1779){
drhd3d39e92004-05-20 22:16:29 +00001780 KeyInfo *pKeyInfo = (KeyInfo*)userData;
danielk1977eb015e02004-05-18 01:31:14 +00001781 int offset1 = 0;
1782 int offset2 = 0;
danielk197784ac9d02004-05-18 09:58:06 +00001783 int toffset1 = 0;
1784 int toffset2 = 0;
1785 int i;
danielk1977b1bc9532004-05-22 03:05:33 +00001786 u8 enc = pKeyInfo->enc;
danielk1977eb015e02004-05-18 01:31:14 +00001787 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1788 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1789
drhd3d39e92004-05-20 22:16:29 +00001790 assert( pKeyInfo );
1791 assert( pKeyInfo->nField>0 );
danielk197784ac9d02004-05-18 09:58:06 +00001792
drhd3d39e92004-05-20 22:16:29 +00001793 for( i=0; i<pKeyInfo->nField; i++ ){
danielk197784ac9d02004-05-18 09:58:06 +00001794 u64 dummy;
1795 offset1 += sqlite3GetVarint(&aKey1[offset1], &dummy);
1796 offset2 += sqlite3GetVarint(&aKey1[offset1], &dummy);
1797 }
1798
drhd3d39e92004-05-20 22:16:29 +00001799 for( i=0; i<pKeyInfo->nField; i++ ){
danielk197784ac9d02004-05-18 09:58:06 +00001800 Mem mem1;
1801 Mem mem2;
1802 u64 serial_type1;
1803 u64 serial_type2;
1804 int rc;
1805
1806 /* Read the serial types for the next element in each key. */
1807 toffset1 += sqlite3GetVarint(&aKey1[toffset1], &serial_type1);
1808 toffset2 += sqlite3GetVarint(&aKey2[toffset2], &serial_type2);
1809
1810 assert( serial_type1 && serial_type2 );
1811
1812 /* Assert that there is enough space left in each key for the blob of
1813 ** data to go with the serial type just read. This assert may fail if
1814 ** the file is corrupted. Then read the value from each key into mem1
1815 ** and mem2 respectively.
1816 */
danielk1977b1bc9532004-05-22 03:05:33 +00001817 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
1818 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);
danielk197784ac9d02004-05-18 09:58:06 +00001819
drhd3d39e92004-05-20 22:16:29 +00001820 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk197784ac9d02004-05-18 09:58:06 +00001821 if( mem1.flags&MEM_Dyn ){
1822 sqliteFree(mem1.z);
1823 }
1824 if( mem2.flags&MEM_Dyn ){
1825 sqliteFree(mem2.z);
1826 }
1827 if( rc!=0 ){
1828 return rc;
1829 }
1830 }
1831
1832 return 0;
danielk1977eb015e02004-05-18 01:31:14 +00001833}
1834
1835
1836/*
danielk1977183f9f72004-05-13 05:20:26 +00001837** pCur points at an index entry. Read the rowid (varint occuring at
1838** the end of the entry and store it in *rowid. Return SQLITE_OK if
1839** everything works, or an error code otherwise.
1840*/
1841int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1842 i64 sz;
1843 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001844 char buf[10];
danielk1977183f9f72004-05-13 05:20:26 +00001845 int len;
1846 u64 r;
1847
1848 rc = sqlite3BtreeKeySize(pCur, &sz);
1849 if( rc!=SQLITE_OK ){
1850 return rc;
1851 }
danielk19773d1bfea2004-05-14 11:00:53 +00001852 len = ((sz>10)?10:sz);
1853
1854 /* If there are less than 2 bytes in the key, this cannot be
1855 ** a valid index entry. In practice this comes up for a query
1856 ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table
1857 ** with an index on x. In this case just call the rowid 0.
1858 */
1859 if( len<2 ){
1860 *rowid = 0;
1861 return SQLITE_OK;
1862 }
danielk1977183f9f72004-05-13 05:20:26 +00001863
1864 rc = sqlite3BtreeKey(pCur, sz-len, len, buf);
1865 if( rc!=SQLITE_OK ){
1866 return rc;
1867 }
1868
danielk19773d1bfea2004-05-14 11:00:53 +00001869 len--;
1870 while( buf[len-1] && --len );
danielk1977183f9f72004-05-13 05:20:26 +00001871
danielk19773d1bfea2004-05-14 11:00:53 +00001872 sqlite3GetVarint(&buf[len], &r);
danielk1977183f9f72004-05-13 05:20:26 +00001873 *rowid = r;
1874 return SQLITE_OK;
1875}
1876
drh7cf6e4d2004-05-19 14:56:55 +00001877/*
drhd3d39e92004-05-20 22:16:29 +00001878** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001879** the key string in pKey (of length nKey). Write into *pRes a number
1880** that is negative, zero, or positive if pC is less than, equal to,
1881** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001882**
1883** pKey might contain fewer terms than the cursor.
drh7cf6e4d2004-05-19 14:56:55 +00001884*/
danielk1977183f9f72004-05-13 05:20:26 +00001885int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001886 Cursor *pC, /* The cursor to compare against */
1887 int nKey, const u8 *pKey, /* The key to compare */
1888 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001889){
1890 unsigned char *pCellKey;
1891 u64 nCellKey;
1892 int freeCellKey = 0;
1893 int rc;
1894 int len;
danielk19773d1bfea2004-05-14 11:00:53 +00001895 BtCursor *pCur = pC->pCursor;
danielk1977183f9f72004-05-13 05:20:26 +00001896
1897 sqlite3BtreeKeySize(pCur, &nCellKey);
1898 if( nCellKey<=0 ){
1899 *res = 0;
1900 return SQLITE_OK;
1901 }
1902
1903 pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey);
1904 if( !pCellKey ){
drh10617cd2004-05-14 15:27:27 +00001905 pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey);
danielk1977183f9f72004-05-13 05:20:26 +00001906 if( !pCellKey ){
1907 return SQLITE_NOMEM;
1908 }
1909 freeCellKey = 1;
1910 rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
1911 if( rc!=SQLITE_OK ){
1912 sqliteFree(pCellKey);
1913 return rc;
1914 }
1915 }
1916
1917 len = nCellKey-2;
1918 while( pCellKey[len] && --len );
1919
drhd3d39e92004-05-20 22:16:29 +00001920 *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, len, pCellKey, nKey, pKey);
danielk1977183f9f72004-05-13 05:20:26 +00001921
1922 if( freeCellKey ){
1923 sqliteFree(pCellKey);
1924 }
1925 return SQLITE_OK;
1926}