blob: b03e49372b7b04b601a79393109acc859bd50d46 [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <ctype.h>
20#include "vdbeInt.h"
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
danielk1977132872b2004-05-10 10:37:18 +000025** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000026** as they are added to the instruction stream.
27*/
28#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +000029int sqlite3_vdbe_addop_trace = 0;
drh9a324642003-09-06 20:12:01 +000030#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
danielk19774adee202004-05-08 08:23:19 +000036Vdbe *sqlite3VdbeCreate(sqlite *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
59** Add a new instruction to the list of instructions current in the
60** VDBE. Return the address of the new instruction.
61**
62** Parameters:
63**
64** p Pointer to the VDBE
65**
66** op The opcode for this instruction
67**
68** p1, p2 First two of the three possible operands.
69**
danielk19774adee202004-05-08 08:23:19 +000070** Use the sqlite3VdbeResolveLabel() function to fix an address and
71** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000072** operand.
73*/
danielk19774adee202004-05-08 08:23:19 +000074int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000075 int i;
drh701a0ae2004-02-22 20:05:00 +000076 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +000077
78 i = p->nOp;
79 p->nOp++;
80 assert( p->magic==VDBE_MAGIC_INIT );
81 if( i>=p->nOpAlloc ){
82 int oldSize = p->nOpAlloc;
83 Op *aNew;
84 p->nOpAlloc = p->nOpAlloc*2 + 100;
85 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
86 if( aNew==0 ){
87 p->nOpAlloc = oldSize;
88 return 0;
89 }
90 p->aOp = aNew;
91 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
92 }
drh701a0ae2004-02-22 20:05:00 +000093 pOp = &p->aOp[i];
94 pOp->opcode = op;
95 pOp->p1 = p1;
drh9a324642003-09-06 20:12:01 +000096 if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){
97 p2 = p->aLabel[-1-p2];
98 }
drh701a0ae2004-02-22 20:05:00 +000099 pOp->p2 = p2;
100 pOp->p3 = 0;
101 pOp->p3type = P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000102#ifndef NDEBUG
drhd3d39e92004-05-20 22:16:29 +0000103 pOp->zComment = 0;
danielk1977132872b2004-05-10 10:37:18 +0000104 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000105#endif
106 return i;
107}
108
109/*
drh701a0ae2004-02-22 20:05:00 +0000110** Add an opcode that includes the p3 value.
111*/
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/*
danielk19770ae8b832004-05-25 12:05:56 +0000428** Extract the user data from a sqlite3_context structure and return a
drh9a324642003-09-06 20:12:01 +0000429** pointer to it.
drh9a324642003-09-06 20:12:01 +0000430*/
danielk19777e18c252004-05-25 11:47:24 +0000431void *sqlite3_user_data(sqlite3_context *p){
drh9a324642003-09-06 20:12:01 +0000432 assert( p && p->pFunc );
433 return p->pFunc->pUserData;
434}
435
436/*
437** Allocate or return the aggregate context for a user function. A new
438** context is allocated on the first call. Subsequent calls return the
439** same context that was returned on prior calls.
440**
441** This routine is defined here in vdbe.c because it depends on knowing
danielk19770ae8b832004-05-25 12:05:56 +0000442** the internals of the sqlite3_context structure which is only defined in
drh9a324642003-09-06 20:12:01 +0000443** this source file.
444*/
danielk19770ae8b832004-05-25 12:05:56 +0000445void *sqlite3_get_context(sqlite3_context *p, int nByte){
drh9a324642003-09-06 20:12:01 +0000446 assert( p && p->pFunc && p->pFunc->xStep );
447 if( p->pAgg==0 ){
448 if( nByte<=NBFS ){
drh00706be2004-01-30 14:49:16 +0000449 p->pAgg = (void*)p->s.z;
450 memset(p->pAgg, 0, nByte);
drh9a324642003-09-06 20:12:01 +0000451 }else{
452 p->pAgg = sqliteMalloc( nByte );
453 }
454 }
455 return p->pAgg;
456}
457
458/*
459** Return the number of times the Step function of a aggregate has been
460** called.
461**
462** This routine is defined here in vdbe.c because it depends on knowing
danielk19770ae8b832004-05-25 12:05:56 +0000463** the internals of the sqlite3_context structure which is only defined in
drh9a324642003-09-06 20:12:01 +0000464** this source file.
465*/
danielk19770ae8b832004-05-25 12:05:56 +0000466int sqlite3_aggregate_count(sqlite3_context *p){
drh9a324642003-09-06 20:12:01 +0000467 assert( p && p->pFunc && p->pFunc->xStep );
468 return p->cnt;
469}
470
drhd3d39e92004-05-20 22:16:29 +0000471/*
472** Compute a string that describes the P3 parameter for an opcode.
473** Use zTemp for any required temporary buffer space.
474*/
475static char *displayP3(Op *pOp, char *zTemp, int nTemp){
476 char *zP3;
477 assert( nTemp>=20 );
478 switch( pOp->p3type ){
479 case P3_POINTER: {
480 sprintf(zTemp, "ptr(%#x)", (int)pOp->p3);
481 zP3 = zTemp;
482 break;
483 }
484 case P3_KEYINFO: {
485 int i, j;
486 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
487 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
488 i = strlen(zTemp);
489 for(j=0; j<pKeyInfo->nField; j++){
490 CollSeq *pColl = pKeyInfo->aColl[j];
491 if( pColl ){
492 int n = strlen(pColl->zName);
493 if( i+n>nTemp-6 ){
494 strcpy(&zTemp[i],",...");
495 break;
496 }
497 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000498 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000499 zTemp[i++] = '-';
500 }
501 strcpy(&zTemp[i], pColl->zName);
502 i += n;
503 }else if( i+4<nTemp-6 ){
504 strcpy(&zTemp[i],",nil");
505 i += 4;
506 }
507 }
508 zTemp[i++] = ')';
509 zTemp[i] = 0;
510 assert( i<nTemp );
511 zP3 = zTemp;
512 break;
513 }
514 case P3_COLLSEQ: {
515 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000516 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000517 zP3 = zTemp;
518 break;
519 }
drhf9b596e2004-05-26 16:54:42 +0000520 case P3_FUNCDEF: {
521 FuncDef *pDef = (FuncDef*)pOp->p3;
522 char zNum[30];
523 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
524 sprintf(zNum,"(%d)", pDef->nArg);
525 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
526 strcat(zTemp, zNum);
527 }
528 zP3 = zTemp;
529 break;
530 }
drhd3d39e92004-05-20 22:16:29 +0000531 default: {
532 zP3 = pOp->p3;
533 if( zP3==0 ){
534 zP3 = "";
535 }
536 }
537 }
538 return zP3;
539}
540
541
drh9a324642003-09-06 20:12:01 +0000542#if !defined(NDEBUG) || defined(VDBE_PROFILE)
543/*
544** Print a single opcode. This routine is used for debugging only.
545*/
danielk19774adee202004-05-08 08:23:19 +0000546void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000547 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000548 char zPtr[50];
549 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
550 static const char *zFormat2 = "%4d %-13s %4d %4d %-20s -- %s\n";
drh9a324642003-09-06 20:12:01 +0000551 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000552 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
553#ifdef NDEBUG
554 fprintf(pOut, zFormat1,
555 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
556#else
557 fprintf(pOut, pOp->zComment ? zFormat2 : zFormat1,
558 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3,pOp->zComment);
559#endif
drh9a324642003-09-06 20:12:01 +0000560 fflush(pOut);
561}
562#endif
563
564/*
565** Give a listing of the program in the virtual machine.
566**
danielk19774adee202004-05-08 08:23:19 +0000567** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000568** running the code, it invokes the callback once for each instruction.
569** This feature is used to implement "EXPLAIN".
570*/
danielk19774adee202004-05-08 08:23:19 +0000571int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000572 Vdbe *p /* The VDBE */
573){
574 sqlite *db = p->db;
575 int i;
drh826fb5a2004-02-14 23:59:57 +0000576 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000577 static char *azColumnNames[] = {
578 "addr", "opcode", "p1", "p2", "p3",
579 "int", "text", "int", "int", "text",
580 0
581 };
582
drh9a324642003-09-06 20:12:01 +0000583 assert( p->explain );
danielk197718f41892004-05-22 07:27:46 +0000584
585 /* Even though this opcode does not put dynamic strings onto the
586 ** the stack, they may become dynamic if the user calls
587 ** sqlite3_column_data16(), causing a translation to UTF-16 encoding.
588 */
589 if( p->pTos==&p->aStack[4] ){
590 for(i=0; i<5; i++){
591 if( p->aStack[i].flags & MEM_Dyn ){
592 sqliteFree(p->aStack[i].z);
593 }
594 p->aStack[i].flags = 0;
595 }
596 }
597
drh9a324642003-09-06 20:12:01 +0000598 p->azColName = azColumnNames;
danielk197718f41892004-05-22 07:27:46 +0000599 p->resOnStack = 0;
600
601 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000602 if( i>=p->nOp ){
603 p->rc = SQLITE_OK;
604 rc = SQLITE_DONE;
605 }else if( db->flags & SQLITE_Interrupt ){
606 db->flags &= ~SQLITE_Interrupt;
607 if( db->magic!=SQLITE_MAGIC_BUSY ){
608 p->rc = SQLITE_MISUSE;
609 }else{
610 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000611 }
drh826fb5a2004-02-14 23:59:57 +0000612 rc = SQLITE_ERROR;
danielk1977132872b2004-05-10 10:37:18 +0000613 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000614 }else{
drhd3d39e92004-05-20 22:16:29 +0000615 Op *pOp = &p->aOp[i];
danielk197718f41892004-05-22 07:27:46 +0000616 p->aStack[0].flags = MEM_Int;
617 p->aStack[0].i = i; /* Program counter */
618 p->aStack[1].flags = MEM_Static|MEM_Str|MEM_Utf8|MEM_Term;
619 p->aStack[1].z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
620 p->aStack[2].flags = MEM_Int;
621 p->aStack[2].i = pOp->p1; /* P1 */
622 p->aStack[3].flags = MEM_Int;
623 p->aStack[3].i = pOp->p2; /* P2 */
624 p->aStack[4].flags = MEM_Str|MEM_Utf8|MEM_Term; /* P3 */
625 p->aStack[4].z = displayP3(pOp, p->aStack[4].zShort, NBFS);
626 if( p->aStack[4].z==p->aStack[4].zShort ){
627 p->aStack[4].flags |= MEM_Short;
628 }else{
629 p->aStack[4].flags |= MEM_Static;
630 }
drh826fb5a2004-02-14 23:59:57 +0000631 p->nResColumn = 5;
danielk197718f41892004-05-22 07:27:46 +0000632 p->pTos = &p->aStack[4];
drh826fb5a2004-02-14 23:59:57 +0000633 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000634 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000635 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000636 }
drh826fb5a2004-02-14 23:59:57 +0000637 return rc;
drh9a324642003-09-06 20:12:01 +0000638}
639
640/*
641** Prepare a virtual machine for execution. This involves things such
642** as allocating stack space and initializing the program counter.
643** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000644** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000645*/
danielk19774adee202004-05-08 08:23:19 +0000646void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000647 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000648 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000649 int isExplain /* True if the EXPLAIN keywords is present */
650){
651 int n;
652
653 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000654 assert( p->magic==VDBE_MAGIC_INIT );
655
656 /* Add a HALT instruction to the very end of the program.
657 */
658 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000659 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000660 }
661
662 /* No instruction ever pushes more than a single element onto the
663 ** stack. And the stack never grows on successive executions of the
664 ** same loop. So the total number of instructions is an upper bound
665 ** on the maximum stack depth required.
666 **
667 ** Allocation all the stack space we will ever need.
668 */
drh82a48512003-09-06 22:45:20 +0000669 if( p->aStack==0 ){
670 p->nVar = nVar;
671 assert( nVar>=0 );
672 n = isExplain ? 10 : p->nOp;
673 p->aStack = sqliteMalloc(
danielk19776ddcca52004-05-24 23:48:25 +0000674 n*(sizeof(p->aStack[0])+sizeof(Mem*)+sizeof(char*)) /* aStack, apArg */
drh5a12e682004-05-19 11:24:25 +0000675 + p->nVar*sizeof(Mem) /* apVar */
drh82a48512003-09-06 22:45:20 +0000676 );
danielk19776ddcca52004-05-24 23:48:25 +0000677 p->apArg = (Mem **)&p->aStack[n];
678 p->azColName = (char**)&p->apArg[n];
drh5a12e682004-05-19 11:24:25 +0000679 p->apVar = (Mem *)&p->azColName[n];
danielk197754db47e2004-05-19 10:36:43 +0000680 for(n=0; n<p->nVar; n++){
drh5a12e682004-05-19 11:24:25 +0000681 p->apVar[n].flags = MEM_Null;
danielk197754db47e2004-05-19 10:36:43 +0000682 }
drh82a48512003-09-06 22:45:20 +0000683 }
drh9a324642003-09-06 20:12:01 +0000684
danielk19774adee202004-05-08 08:23:19 +0000685 sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
drh9a324642003-09-06 20:12:01 +0000686 p->agg.pSearch = 0;
687#ifdef MEMORY_DEBUG
danielk19774adee202004-05-08 08:23:19 +0000688 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000689 p->trace = stdout;
690 }
691#endif
drh6810ce62004-01-31 19:22:56 +0000692 p->pTos = &p->aStack[-1];
drh9a324642003-09-06 20:12:01 +0000693 p->pc = 0;
694 p->rc = SQLITE_OK;
695 p->uniqueCnt = 0;
696 p->returnDepth = 0;
697 p->errorAction = OE_Abort;
698 p->undoTransOnError = 0;
drh9a324642003-09-06 20:12:01 +0000699 p->popStack = 0;
700 p->explain |= isExplain;
701 p->magic = VDBE_MAGIC_RUN;
702#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000703 {
704 int i;
705 for(i=0; i<p->nOp; i++){
706 p->aOp[i].cnt = 0;
707 p->aOp[i].cycles = 0;
708 }
drh9a324642003-09-06 20:12:01 +0000709 }
710#endif
711}
712
713
714/*
715** Remove any elements that remain on the sorter for the VDBE given.
716*/
danielk19774adee202004-05-08 08:23:19 +0000717void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000718 while( p->pSort ){
719 Sorter *pSorter = p->pSort;
720 p->pSort = pSorter->pNext;
721 sqliteFree(pSorter->zKey);
722 sqliteFree(pSorter->pData);
723 sqliteFree(pSorter);
724 }
725}
726
727/*
drh9a324642003-09-06 20:12:01 +0000728** Reset an Agg structure. Delete all its contents.
729**
730** For installable aggregate functions, if the step function has been
731** called, make sure the finalizer function has also been called. The
732** finalizer might need to free memory that was allocated as part of its
733** private context. If the finalizer has not been called yet, call it
734** now.
735*/
danielk19774adee202004-05-08 08:23:19 +0000736void sqlite3VdbeAggReset(Agg *pAgg){
drh9a324642003-09-06 20:12:01 +0000737 int i;
738 HashElem *p;
739 for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
740 AggElem *pElem = sqliteHashData(p);
741 assert( pAgg->apFunc!=0 );
742 for(i=0; i<pAgg->nMem; i++){
743 Mem *pMem = &pElem->aMem[i];
drh00706be2004-01-30 14:49:16 +0000744 if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk19770ae8b832004-05-25 12:05:56 +0000745 sqlite3_context ctx;
drh9a324642003-09-06 20:12:01 +0000746 ctx.pFunc = pAgg->apFunc[i];
drh00706be2004-01-30 14:49:16 +0000747 ctx.s.flags = MEM_Null;
drh9a324642003-09-06 20:12:01 +0000748 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +0000749 ctx.cnt = pMem->i;
drh9a324642003-09-06 20:12:01 +0000750 ctx.isStep = 0;
751 ctx.isError = 0;
752 (*pAgg->apFunc[i]->xFinalize)(&ctx);
drh00706be2004-01-30 14:49:16 +0000753 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
drh9a324642003-09-06 20:12:01 +0000754 sqliteFree(pMem->z);
755 }
drh9cbe7ca2004-02-18 16:57:23 +0000756 if( ctx.s.flags & MEM_Dyn ){
757 sqliteFree(ctx.s.z);
758 }
drh00706be2004-01-30 14:49:16 +0000759 }else if( pMem->flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000760 sqliteFree(pMem->z);
761 }
762 }
763 sqliteFree(pElem);
764 }
danielk19774adee202004-05-08 08:23:19 +0000765 sqlite3HashClear(&pAgg->hash);
drh9a324642003-09-06 20:12:01 +0000766 sqliteFree(pAgg->apFunc);
767 pAgg->apFunc = 0;
768 pAgg->pCurrent = 0;
769 pAgg->pSearch = 0;
770 pAgg->nMem = 0;
771}
772
773/*
774** Delete a keylist
775*/
danielk19774adee202004-05-08 08:23:19 +0000776void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000777 while( p ){
778 Keylist *pNext = p->pNext;
779 sqliteFree(p);
780 p = pNext;
781 }
782}
783
784/*
785** Close a cursor and release all the resources that cursor happens
786** to hold.
787*/
danielk19774adee202004-05-08 08:23:19 +0000788void sqlite3VdbeCleanupCursor(Cursor *pCx){
drh9a324642003-09-06 20:12:01 +0000789 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000790 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000791 }
792 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000793 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000794 }
795 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000796 sqliteFree(pCx->aType);
drhd7556d22004-05-14 21:59:40 +0000797 memset(pCx, 0, sizeof(*pCx));
drh9a324642003-09-06 20:12:01 +0000798}
799
800/*
801** Close all cursors
802*/
803static void closeAllCursors(Vdbe *p){
804 int i;
805 for(i=0; i<p->nCursor; i++){
drhd7556d22004-05-14 21:59:40 +0000806 Cursor *pC = p->apCsr[i];
807 sqlite3VdbeCleanupCursor(pC);
808 sqliteFree(pC);
drh9a324642003-09-06 20:12:01 +0000809 }
drhd7556d22004-05-14 21:59:40 +0000810 sqliteFree(p->apCsr);
811 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000812 p->nCursor = 0;
813}
814
815/*
drh9a324642003-09-06 20:12:01 +0000816** Clean up the VM after execution.
817**
818** This routine will automatically close any cursors, lists, and/or
819** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000820** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000821*/
822static void Cleanup(Vdbe *p){
823 int i;
drh6810ce62004-01-31 19:22:56 +0000824 if( p->aStack ){
825 Mem *pTos = p->pTos;
826 while( pTos>=p->aStack ){
827 if( pTos->flags & MEM_Dyn ){
828 sqliteFree(pTos->z);
829 }
830 pTos--;
831 }
832 p->pTos = pTos;
833 }
drh9a324642003-09-06 20:12:01 +0000834 closeAllCursors(p);
835 if( p->aMem ){
836 for(i=0; i<p->nMem; i++){
drh00706be2004-01-30 14:49:16 +0000837 if( p->aMem[i].flags & MEM_Dyn ){
drh9a324642003-09-06 20:12:01 +0000838 sqliteFree(p->aMem[i].z);
839 }
840 }
841 }
842 sqliteFree(p->aMem);
843 p->aMem = 0;
844 p->nMem = 0;
845 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000846 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000847 p->pList = 0;
848 }
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000850 if( p->pFile ){
851 if( p->pFile!=stdin ) fclose(p->pFile);
852 p->pFile = 0;
853 }
854 if( p->azField ){
855 sqliteFree(p->azField);
856 p->azField = 0;
857 }
858 p->nField = 0;
859 if( p->zLine ){
860 sqliteFree(p->zLine);
861 p->zLine = 0;
862 }
863 p->nLineAlloc = 0;
danielk19774adee202004-05-08 08:23:19 +0000864 sqlite3VdbeAggReset(&p->agg);
drh9a324642003-09-06 20:12:01 +0000865 if( p->keylistStack ){
866 int ii;
867 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000868 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000869 }
870 sqliteFree(p->keylistStack);
871 p->keylistStackDepth = 0;
872 p->keylistStack = 0;
873 }
drh5f968432004-02-21 19:02:30 +0000874 sqliteFree(p->contextStack);
875 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000876 sqliteFree(p->zErrMsg);
877 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000878}
879
880/*
danielk197722322fd2004-05-25 23:35:17 +0000881** Set the number of result columns that will be returned by this SQL
882** statement. This is now set at compile time, rather than during
883** execution of the vdbe program so that sqlite3_column_count() can
884** be called on an SQL statement before sqlite3_step().
885*/
886void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
danielk19773cf86062004-05-26 10:11:05 +0000887 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000888 p->nResColumn = nResColumn;
889}
890
891/*
danielk19773cf86062004-05-26 10:11:05 +0000892** Set the name of the idx'th column to be returned by the SQL statement.
893** zName must be a pointer to a nul terminated string.
894**
895** This call must be made after a call to sqlite3VdbeSetNumCols().
896**
897** Parameter N may be either P3_DYNAMIC or P3_STATIC.
898*/
899int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
900 int rc;
901 Mem *pColName;
902 assert( idx<p->nResColumn );
903
904 /* If the Vdbe.aColName array has not yet been allocated, allocate
905 ** it now.
906 */
907 if( !p->aColName ){
908 int i;
909 p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn);
910 if( !p->aColName ){
911 return SQLITE_NOMEM;
912 }
913 for(i=0; i<p->nResColumn; i++){
914 p->aColName[i].flags = MEM_Null;
915 }
916 }
917
918 pColName = &(p->aColName[idx]);
919 if( N==0 ){
920 rc = MemSetStr(pColName, zName, -1, TEXT_Utf8, 1);
921 }else{
922 rc = MemSetStr(pColName, zName, N, TEXT_Utf8, N>0);
923 }
924 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
925 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
926 }
927 return rc;
928}
929
930/*
drh9a324642003-09-06 20:12:01 +0000931** Clean up a VDBE after execution but do not delete the VDBE just yet.
932** Write any error messages into *pzErrMsg. Return the result code.
933**
934** After this routine is run, the VDBE should be ready to be executed
935** again.
936*/
danielk19774adee202004-05-08 08:23:19 +0000937int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +0000938 sqlite *db = p->db;
939 int i;
940
941 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +0000942 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000943 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
drh9a324642003-09-06 20:12:01 +0000944 return SQLITE_MISUSE;
945 }
946 if( p->zErrMsg ){
danielk1977106bb232004-05-21 10:08:53 +0000947 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
drh9a324642003-09-06 20:12:01 +0000948 if( pzErrMsg && *pzErrMsg==0 ){
949 *pzErrMsg = p->zErrMsg;
950 }else{
951 sqliteFree(p->zErrMsg);
952 }
953 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +0000954 }else if( p->rc ){
danielk1977132872b2004-05-10 10:37:18 +0000955 sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +0000956 sqlite3Error(p->db, p->rc, "%s", sqlite3_error_string(p->rc) , 0);
957 }else{
958 sqlite3Error(p->db, SQLITE_OK, 0);
drh9a324642003-09-06 20:12:01 +0000959 }
960 Cleanup(p);
961 if( p->rc!=SQLITE_OK ){
962 switch( p->errorAction ){
963 case OE_Abort: {
964 if( !p->undoTransOnError ){
965 for(i=0; i<db->nDb; i++){
966 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000967 sqlite3BtreeRollbackStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000968 }
969 }
970 break;
971 }
972 /* Fall through to ROLLBACK */
973 }
974 case OE_Rollback: {
danielk19774adee202004-05-08 08:23:19 +0000975 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000976 db->flags &= ~SQLITE_InTrans;
977 db->onError = OE_Default;
978 break;
979 }
980 default: {
981 if( p->undoTransOnError ){
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3RollbackAll(db);
drh9a324642003-09-06 20:12:01 +0000983 db->flags &= ~SQLITE_InTrans;
984 db->onError = OE_Default;
985 }
986 break;
987 }
988 }
danielk19774adee202004-05-08 08:23:19 +0000989 sqlite3RollbackInternalChanges(db);
drh9a324642003-09-06 20:12:01 +0000990 }
991 for(i=0; i<db->nDb; i++){
992 if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3BtreeCommitStmt(db->aDb[i].pBt);
drh9a324642003-09-06 20:12:01 +0000994 db->aDb[i].inTrans = 1;
995 }
996 }
danielk1977132872b2004-05-10 10:37:18 +0000997 assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +0000998#ifdef VDBE_PROFILE
999 {
1000 FILE *out = fopen("vdbe_profile.out", "a");
1001 if( out ){
1002 int i;
1003 fprintf(out, "---- ");
1004 for(i=0; i<p->nOp; i++){
1005 fprintf(out, "%02x", p->aOp[i].opcode);
1006 }
1007 fprintf(out, "\n");
1008 for(i=0; i<p->nOp; i++){
1009 fprintf(out, "%6d %10lld %8lld ",
1010 p->aOp[i].cnt,
1011 p->aOp[i].cycles,
1012 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1013 );
danielk19774adee202004-05-08 08:23:19 +00001014 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001015 }
1016 fclose(out);
1017 }
1018 }
1019#endif
1020 p->magic = VDBE_MAGIC_INIT;
1021 return p->rc;
1022}
1023
1024/*
1025** Clean up and delete a VDBE after execution. Return an integer which is
1026** the result code. Write any error message text into *pzErrMsg.
1027*/
danielk19774adee202004-05-08 08:23:19 +00001028int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){
drh9a324642003-09-06 20:12:01 +00001029 int rc;
1030 sqlite *db;
1031
1032 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk1977132872b2004-05-10 10:37:18 +00001033 sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0);
danielk1977106bb232004-05-21 10:08:53 +00001034 if( p->magic==VDBE_MAGIC_INIT ){
1035 sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0);
1036 }
drh9a324642003-09-06 20:12:01 +00001037 return SQLITE_MISUSE;
1038 }
1039 db = p->db;
danielk19774adee202004-05-08 08:23:19 +00001040 rc = sqlite3VdbeReset(p, pzErrMsg);
1041 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001042 if( db->want_to_close && db->pVdbe==0 ){
danielk1977132872b2004-05-10 10:37:18 +00001043 sqlite3_close(db);
drh9a324642003-09-06 20:12:01 +00001044 }
drha1f9b5e2004-02-14 16:31:02 +00001045 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001046 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001047 }
drh9a324642003-09-06 20:12:01 +00001048 return rc;
1049}
1050
1051/*
drh9a324642003-09-06 20:12:01 +00001052** Delete an entire VDBE.
1053*/
danielk19774adee202004-05-08 08:23:19 +00001054void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001055 int i;
1056 if( p==0 ) return;
1057 Cleanup(p);
1058 if( p->pPrev ){
1059 p->pPrev->pNext = p->pNext;
1060 }else{
1061 assert( p->db->pVdbe==p );
1062 p->db->pVdbe = p->pNext;
1063 }
1064 if( p->pNext ){
1065 p->pNext->pPrev = p->pPrev;
1066 }
1067 p->pPrev = p->pNext = 0;
1068 if( p->nOpAlloc==0 ){
1069 p->aOp = 0;
1070 p->nOp = 0;
1071 }
1072 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001073 Op *pOp = &p->aOp[i];
1074 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1075 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001076 }
drhd3d39e92004-05-20 22:16:29 +00001077#ifndef NDEBUG
1078 sqliteFree(pOp->zComment);
1079#endif
drh9a324642003-09-06 20:12:01 +00001080 }
drh7c972de2003-09-06 22:18:07 +00001081 for(i=0; i<p->nVar; i++){
drh5a12e682004-05-19 11:24:25 +00001082 if( p->apVar[i].flags&MEM_Dyn ){
1083 sqliteFree(p->apVar[i].z);
danielk197754db47e2004-05-19 10:36:43 +00001084 }
drh7c972de2003-09-06 22:18:07 +00001085 }
danielk1977ca6b2912004-05-21 10:49:47 +00001086 if( p->azColName16 ){
1087 for(i=0; i<p->nResColumn; i++){
1088 if( p->azColName16[i] ) sqliteFree(p->azColName16[i]);
1089 }
1090 sqliteFree(p->azColName16);
1091 }
drh9a324642003-09-06 20:12:01 +00001092 sqliteFree(p->aOp);
1093 sqliteFree(p->aLabel);
1094 sqliteFree(p->aStack);
1095 p->magic = VDBE_MAGIC_DEAD;
1096 sqliteFree(p);
1097}
drha11846b2004-01-07 18:52:56 +00001098
1099/*
drha11846b2004-01-07 18:52:56 +00001100** If a MoveTo operation is pending on the given cursor, then do that
1101** MoveTo now. Return an error code. If no MoveTo is pending, this
1102** routine does nothing and returns SQLITE_OK.
1103*/
danielk19774adee202004-05-08 08:23:19 +00001104int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001105 if( p->deferredMoveto ){
1106 int res;
danielk1977132872b2004-05-10 10:37:18 +00001107 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001108 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001109 if( p->intKey ){
1110 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1111 }else{
1112 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1113 }
drhd3d39e92004-05-20 22:16:29 +00001114 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001115 p->lastRecno = keyToInt(p->movetoTarget);
1116 p->recnoIsValid = res==0;
1117 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001118 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001119 }
danielk1977132872b2004-05-10 10:37:18 +00001120 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001121 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001122 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001123 }
1124 return SQLITE_OK;
1125}
danielk19774adee202004-05-08 08:23:19 +00001126
drhab9f7f12004-05-08 10:56:11 +00001127/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001128** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001129**
danielk1977cfcdaef2004-05-12 07:33:33 +00001130** sqlite3VdbeSerialType()
1131** sqlite3VdbeSerialTypeLen()
1132** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001133** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001134** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001135**
1136** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001137** data and index records. Each serialized value consists of a
1138** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1139** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001140**
danielk1977cfcdaef2004-05-12 07:33:33 +00001141** In an SQLite index record, the serial type is stored directly before
1142** the blob of data that it corresponds to. In a table record, all serial
1143** types are stored at the start of the record, and the blobs of data at
1144** the end. Hence these functions allow the caller to handle the
1145** serial-type and data blob seperately.
1146**
1147** The following table describes the various storage classes for data:
1148**
1149** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001150** -------------- --------------- ---------------
danielk1977183f9f72004-05-13 05:20:26 +00001151** 0 - Not a type.
danielk197790e4d952004-05-10 10:05:53 +00001152** 1 1 signed integer
1153** 2 2 signed integer
1154** 3 4 signed integer
1155** 4 8 signed integer
1156** 5 8 IEEE float
danielk1977183f9f72004-05-13 05:20:26 +00001157** 6 0 NULL
1158** 7..11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001159** N>=12 and even (N-12)/2 BLOB
1160** N>=13 and odd (N-13)/2 text
1161**
1162*/
1163
1164/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001165** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001166*/
danielk1977b1bc9532004-05-22 03:05:33 +00001167u64 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001168 int flags = pMem->flags;
1169
1170 if( flags&MEM_Null ){
danielk1977183f9f72004-05-13 05:20:26 +00001171 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001172 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001173 if( flags&MEM_Int ){
1174 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1175 i64 i = pMem->i;
1176 if( i>=-127 && i<=127 ) return 1;
1177 if( i>=-32767 && i<=32767 ) return 2;
1178 if( i>=-2147483647 && i<=2147483647 ) return 3;
1179 return 4;
danielk197790e4d952004-05-10 10:05:53 +00001180 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001181 if( flags&MEM_Real ){
1182 return 5;
danielk197790e4d952004-05-10 10:05:53 +00001183 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001184 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001185 int n = pMem->n;
1186 assert( n>=0 );
danielk1977b1bc9532004-05-22 03:05:33 +00001187 if( pMem->flags&MEM_Term ){
danielk197793d46752004-05-23 13:30:58 +00001188 /* If the nul terminated flag is set we have to subtract something
1189 ** from the serial-type. Depending on the encoding there could be
1190 ** one or two 0x00 bytes at the end of the string. Check for these
1191 ** and subtract 2 from serial_
1192 */
1193 if( n>0 && !pMem->z[n-1] ) n--;
1194 if( n>0 && !pMem->z[n-1] ) n--;
danielk1977b1bc9532004-05-22 03:05:33 +00001195 }
danielk197793d46752004-05-23 13:30:58 +00001196 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001197 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001198 if( flags&MEM_Blob ){
1199 return (pMem->n*2 + 12);
1200 }
1201 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001202}
1203
1204/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001205** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001206*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001207int sqlite3VdbeSerialTypeLen(u64 serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001208 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001209 switch(serial_type){
danielk1977183f9f72004-05-13 05:20:26 +00001210 case 6: return 0; /* NULL */
danielk1977cfcdaef2004-05-12 07:33:33 +00001211 case 1: return 1; /* 1 byte integer */
1212 case 2: return 2; /* 2 byte integer */
1213 case 3: return 4; /* 4 byte integer */
1214 case 4: return 8; /* 8 byte integer */
1215 case 5: return 8; /* 8 byte float */
danielk197790e4d952004-05-10 10:05:53 +00001216 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001217 assert( serial_type>=12 );
1218 return ((serial_type-12)>>1); /* text or blob */
danielk1977192ac1d2004-05-10 07:17:30 +00001219}
1220
1221/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001222** Write the serialized data blob for the value stored in pMem into
1223** buf. It is assumed that the caller has allocated sufficient space.
1224** Return the number of bytes written.
1225*/
danielk1977b1bc9532004-05-22 03:05:33 +00001226int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001227 u64 serial_type = sqlite3VdbeSerialType(pMem);
1228 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001229
1230 assert( serial_type!=0 );
danielk1977cfcdaef2004-05-12 07:33:33 +00001231
1232 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001233 if( serial_type==6 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001234 return 0;
1235 }
1236
drh1483e142004-05-21 21:12:42 +00001237 /* Integer and Real */
1238 if( serial_type<=5 ){
1239 u64 v;
1240 int i;
1241 if( serial_type==5 ){
1242 v = *(u64*)&pMem->r;
1243 }else{
1244 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001245 }
drh1483e142004-05-21 21:12:42 +00001246 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1247 while( i-- ){
1248 buf[i] = (v&0xFF);
1249 v >>= 8;
1250 }
1251 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001252 }
1253
1254 /* String or blob */
1255 assert( serial_type>=12 );
1256 len = sqlite3VdbeSerialTypeLen(serial_type);
1257 memcpy(buf, pMem->z, len);
1258 return len;
1259}
1260
1261/*
1262** Deserialize the data blob pointed to by buf as serial type serial_type
1263** and store the result in pMem. Return the number of bytes read.
1264*/
danielk1977b1bc9532004-05-22 03:05:33 +00001265int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001266 const unsigned char *buf, /* Buffer to deserialize from */
1267 u64 serial_type, /* Serial type to deserialize */
1268 Mem *pMem, /* Memory cell to write value into */
1269 u8 enc /* Text encoding. Used to determine nul term. character */
danielk1977b1bc9532004-05-22 03:05:33 +00001270){
danielk197790e4d952004-05-10 10:05:53 +00001271 int len;
1272
danielk1977183f9f72004-05-13 05:20:26 +00001273 assert( serial_type!=0 );
1274
danielk1977cfcdaef2004-05-12 07:33:33 +00001275 /* memset(pMem, 0, sizeof(pMem)); */
1276 pMem->flags = 0;
1277 pMem->z = 0;
danielk197790e4d952004-05-10 10:05:53 +00001278
danielk1977cfcdaef2004-05-12 07:33:33 +00001279 /* NULL */
danielk1977183f9f72004-05-13 05:20:26 +00001280 if( serial_type==6 ){
danielk197790e4d952004-05-10 10:05:53 +00001281 pMem->flags = MEM_Null;
danielk1977cfcdaef2004-05-12 07:33:33 +00001282 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001283 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001284
drh1483e142004-05-21 21:12:42 +00001285 /* Integer and Real */
1286 if( serial_type<=5 ){
1287 u64 v = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001288 int n;
1289 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk197790e4d952004-05-10 10:05:53 +00001290
danielk1977cfcdaef2004-05-12 07:33:33 +00001291 if( buf[0]&0x80 ){
drh1483e142004-05-21 21:12:42 +00001292 v = -1;
danielk1977cfcdaef2004-05-12 07:33:33 +00001293 }
1294 for(n=0; n<len; n++){
drh1483e142004-05-21 21:12:42 +00001295 v = (v<<8) | buf[n];
danielk1977cfcdaef2004-05-12 07:33:33 +00001296 }
drh1483e142004-05-21 21:12:42 +00001297 if( serial_type==5 ){
1298 pMem->flags = MEM_Real;
1299 pMem->r = *(double*)&v;
1300 }else{
1301 pMem->flags = MEM_Int;
danielk1977b1bc9532004-05-22 03:05:33 +00001302 pMem->i = *(i64*)&v;
drh1483e142004-05-21 21:12:42 +00001303 }
1304 return len;
danielk197790e4d952004-05-10 10:05:53 +00001305 }
1306
danielk1977cfcdaef2004-05-12 07:33:33 +00001307 /* String or blob */
1308 assert( serial_type>=12 );
danielk1977eb015e02004-05-18 01:31:14 +00001309 len = sqlite3VdbeSerialTypeLen(serial_type);
danielk1977cfcdaef2004-05-12 07:33:33 +00001310 if( serial_type&0x01 ){
danielk1977b1bc9532004-05-22 03:05:33 +00001311 switch( enc ){
1312 case TEXT_Utf8:
1313 pMem->flags = MEM_Str|MEM_Utf8|MEM_Term;
1314 break;
1315 case TEXT_Utf16le:
1316 pMem->flags = MEM_Str|MEM_Utf16le|MEM_Term;
1317 break;
1318 case TEXT_Utf16be:
1319 pMem->flags = MEM_Str|MEM_Utf16be|MEM_Term;
1320 break;
1321 assert(0);
1322 }
1323 pMem->n = len+(enc==TEXT_Utf8?1:2);
danielk1977cfcdaef2004-05-12 07:33:33 +00001324 }else{
1325 pMem->flags = MEM_Blob;
danielk1977eb015e02004-05-18 01:31:14 +00001326 pMem->n = len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001327 }
danielk1977eb015e02004-05-18 01:31:14 +00001328
1329 if( (pMem->n)>NBFS ){
1330 pMem->z = sqliteMallocRaw( pMem->n );
danielk197790e4d952004-05-10 10:05:53 +00001331 if( !pMem->z ){
1332 return -1;
1333 }
1334 pMem->flags |= MEM_Dyn;
1335 }else{
1336 pMem->z = pMem->zShort;
1337 pMem->flags |= MEM_Short;
1338 }
danielk1977eb015e02004-05-18 01:31:14 +00001339
danielk1977cfcdaef2004-05-12 07:33:33 +00001340 memcpy(pMem->z, buf, len);
danielk1977eb015e02004-05-18 01:31:14 +00001341 if( pMem->flags&MEM_Str ){
1342 pMem->z[len] = '\0';
danielk1977b1bc9532004-05-22 03:05:33 +00001343 if( enc!=TEXT_Utf8 ){
1344 pMem->z[len+1] = '\0';
1345 }
danielk1977eb015e02004-05-18 01:31:14 +00001346 }
danielk197790e4d952004-05-10 10:05:53 +00001347
danielk1977cfcdaef2004-05-12 07:33:33 +00001348 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001349}
1350
1351/*
danielk19778d059842004-05-12 11:24:02 +00001352** Compare the values contained by the two memory cells, returning
1353** negative, zero or positive if pMem1 is less than, equal to, or greater
1354** than pMem2. Sorting order is NULL's first, followed by numbers (integers
drhd3d39e92004-05-20 22:16:29 +00001355** and reals) sorted numerically, followed by text ordered by the collating
1356** sequence pColl and finally blob's ordered by memcmp().
danielk19778d059842004-05-12 11:24:02 +00001357**
1358** Two NULL values are considered equal by this function.
1359*/
drh53db1452004-05-20 13:54:53 +00001360int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
danielk19778d059842004-05-12 11:24:02 +00001361 int rc;
drh53db1452004-05-20 13:54:53 +00001362 int f1, f2;
1363 int combined_flags;
1364
1365 /* Interchange pMem1 and pMem2 if the collating sequence specifies
1366 ** DESC order.
1367 */
drh53db1452004-05-20 13:54:53 +00001368 f1 = pMem1->flags;
1369 f2 = pMem2->flags;
1370 combined_flags = f1|f2;
danielk19778d059842004-05-12 11:24:02 +00001371
1372 /* If one value is NULL, it is less than the other. If both values
1373 ** are NULL, return 0.
1374 */
1375 if( combined_flags&MEM_Null ){
drh53db1452004-05-20 13:54:53 +00001376 return (f2&MEM_Null) - (f1&MEM_Null);
danielk19778d059842004-05-12 11:24:02 +00001377 }
1378
1379 /* If one value is a number and the other is not, the number is less.
1380 ** If both are numbers, compare as reals if one is a real, or as integers
1381 ** if both values are integers.
1382 */
1383 if( combined_flags&(MEM_Int|MEM_Real) ){
drh53db1452004-05-20 13:54:53 +00001384 if( !(f1&(MEM_Int|MEM_Real)) ){
danielk19778d059842004-05-12 11:24:02 +00001385 return 1;
1386 }
drh53db1452004-05-20 13:54:53 +00001387 if( !(f2&(MEM_Int|MEM_Real)) ){
danielk19778d059842004-05-12 11:24:02 +00001388 return -1;
1389 }
drh53db1452004-05-20 13:54:53 +00001390 if( (f1 & f2 & MEM_Int)==0 ){
1391 double r1, r2;
1392 if( (f1&MEM_Real)==0 ){
1393 r1 = pMem1->i;
1394 }else{
1395 r1 = pMem1->r;
danielk19778d059842004-05-12 11:24:02 +00001396 }
drh53db1452004-05-20 13:54:53 +00001397 if( (f2&MEM_Real)==0 ){
1398 r2 = pMem2->i;
1399 }else{
1400 r2 = pMem2->r;
danielk19778d059842004-05-12 11:24:02 +00001401 }
drh53db1452004-05-20 13:54:53 +00001402 if( r1<r2 ) return -1;
1403 if( r1>r2 ) return 1;
1404 return 0;
1405 }else{
1406 assert( f1&MEM_Int );
1407 assert( f2&MEM_Int );
1408 if( pMem1->i < pMem2->i ) return -1;
1409 if( pMem1->i > pMem2->i ) return 1;
danielk19778d059842004-05-12 11:24:02 +00001410 return 0;
1411 }
danielk19773d1bfea2004-05-14 11:00:53 +00001412 }
1413
drh53db1452004-05-20 13:54:53 +00001414 /* If one value is a string and the other is a blob, the string is less.
1415 ** If both are strings, compare using the collating functions.
1416 */
1417 if( combined_flags&MEM_Str ){
1418 if( (f1 & MEM_Str)==0 ){
1419 return 1;
1420 }
1421 if( (f2 & MEM_Str)==0 ){
1422 return -1;
1423 }
1424 if( pColl && pColl->xCmp ){
1425 return pColl->xCmp(pColl->pUser, pMem1->n, pMem1->z, pMem2->n, pMem2->z);
1426 }else{
1427 /* If no collating sequence is defined, fall through into the
1428 ** blob case below and use memcmp() for the comparison. */
1429 }
danielk19778d059842004-05-12 11:24:02 +00001430 }
drh53db1452004-05-20 13:54:53 +00001431
1432 /* Both values must be blobs. Compare using memcmp().
danielk19778d059842004-05-12 11:24:02 +00001433 */
danielk19778d059842004-05-12 11:24:02 +00001434 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
drh53db1452004-05-20 13:54:53 +00001435 if( rc==0 ){
1436 rc = pMem1->n - pMem2->n;
danielk19778d059842004-05-12 11:24:02 +00001437 }
drh53db1452004-05-20 13:54:53 +00001438 return rc;
danielk19778d059842004-05-12 11:24:02 +00001439}
1440
1441/*
drhab9f7f12004-05-08 10:56:11 +00001442** The following is the comparison function for (non-integer)
1443** keys in the btrees. This function returns negative, zero, or
1444** positive if the first key is less than, equal to, or greater than
1445** the second.
1446**
danielk19778d059842004-05-12 11:24:02 +00001447** This function assumes that each key consists of one or more type/blob
danielk1977183f9f72004-05-13 05:20:26 +00001448** pairs, encoded using the sqlite3VdbeSerialXXX() functions above.
1449**
1450** Following the type/blob pairs, each key may have a single 0x00 byte
1451** followed by a varint. A key may only have this traling 0x00/varint
1452** pair if it has at least as many type/blob pairs as the key it is being
1453** compared to.
drhab9f7f12004-05-08 10:56:11 +00001454*/
drhab9f7f12004-05-08 10:56:11 +00001455int sqlite3VdbeKeyCompare(
danielk19773d1bfea2004-05-14 11:00:53 +00001456 void *userData,
danielk1977183f9f72004-05-13 05:20:26 +00001457 int nKey1, const void *pKey1,
1458 int nKey2, const void *pKey2
drhab9f7f12004-05-08 10:56:11 +00001459){
drhd3d39e92004-05-20 22:16:29 +00001460 KeyInfo *pKeyInfo = (KeyInfo*)userData;
danielk19778d059842004-05-12 11:24:02 +00001461 int offset1 = 0;
1462 int offset2 = 0;
drhd3d39e92004-05-20 22:16:29 +00001463 int i = 0;
drhffbc3082004-05-21 01:29:06 +00001464 int rc = 0;
danielk1977b1bc9532004-05-22 03:05:33 +00001465 u8 enc = pKeyInfo->enc;
danielk1977183f9f72004-05-13 05:20:26 +00001466 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1467 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1468
drhd3d39e92004-05-20 22:16:29 +00001469 assert( pKeyInfo!=0 );
danielk19778d059842004-05-12 11:24:02 +00001470 while( offset1<nKey1 && offset2<nKey2 ){
1471 Mem mem1;
1472 Mem mem2;
1473 u64 serial_type1;
1474 u64 serial_type2;
danielk19778d059842004-05-12 11:24:02 +00001475
danielk1977183f9f72004-05-13 05:20:26 +00001476 /* Read the serial types for the next element in each key. */
danielk19778d059842004-05-12 11:24:02 +00001477 offset1 += sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1478 offset2 += sqlite3GetVarint(&aKey2[offset2], &serial_type2);
danielk1977183f9f72004-05-13 05:20:26 +00001479
1480 /* If either of the varints just read in are 0 (not a type), then
1481 ** this is the end of the keys. The remaining data in each key is
1482 ** the varint rowid. Compare these as signed integers and return
1483 ** the result.
1484 */
1485 if( !serial_type1 || !serial_type2 ){
1486 assert( !serial_type1 && !serial_type2 );
1487 sqlite3GetVarint(&aKey1[offset1], &serial_type1);
1488 sqlite3GetVarint(&aKey2[offset2], &serial_type2);
drhffbc3082004-05-21 01:29:06 +00001489 if( serial_type1 < serial_type2 ){
1490 rc = -1;
1491 }else if( serial_type1 > serial_type2 ){
1492 rc = +1;
1493 }else{
1494 rc = 0;
1495 }
1496 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001497 }
1498
drhd3d39e92004-05-20 22:16:29 +00001499 assert( i<pKeyInfo->nField );
1500
danielk1977183f9f72004-05-13 05:20:26 +00001501 /* Assert that there is enough space left in each key for the blob of
1502 ** data to go with the serial type just read. This assert may fail if
1503 ** the file is corrupted. Then read the value from each key into mem1
1504 ** and mem2 respectively.
1505 */
danielk1977b1bc9532004-05-22 03:05:33 +00001506 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
1507 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);
danielk19778d059842004-05-12 11:24:02 +00001508
drhd3d39e92004-05-20 22:16:29 +00001509 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk19778d059842004-05-12 11:24:02 +00001510 if( mem1.flags&MEM_Dyn ){
1511 sqliteFree(mem1.z);
1512 }
1513 if( mem2.flags&MEM_Dyn ){
1514 sqliteFree(mem2.z);
1515 }
1516 if( rc!=0 ){
drhffbc3082004-05-21 01:29:06 +00001517 break;
danielk19778d059842004-05-12 11:24:02 +00001518 }
drhd3d39e92004-05-20 22:16:29 +00001519 i++;
danielk19778d059842004-05-12 11:24:02 +00001520 }
1521
danielk19773d1bfea2004-05-14 11:00:53 +00001522 /* One of the keys ran out of fields, but all the fields up to that point
1523 ** were equal. If the incrKey flag is true, then the second key is
1524 ** treated as larger.
1525 */
drhffbc3082004-05-21 01:29:06 +00001526 if( rc==0 ){
1527 if( pKeyInfo->incrKey ){
1528 assert( offset2==nKey2 );
1529 rc = -1;
1530 }else if( offset1<nKey1 ){
1531 rc = 1;
1532 }else if( offset2<nKey2 ){
1533 rc = -1;
1534 }
danielk19773d1bfea2004-05-14 11:00:53 +00001535 }
1536
drhffbc3082004-05-21 01:29:06 +00001537 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1538 rc = -rc;
drhab9f7f12004-05-08 10:56:11 +00001539 }
danielk19773d1bfea2004-05-14 11:00:53 +00001540
drhffbc3082004-05-21 01:29:06 +00001541 return rc;
drhab9f7f12004-05-08 10:56:11 +00001542}
danielk1977183f9f72004-05-13 05:20:26 +00001543
1544/*
danielk1977eb015e02004-05-18 01:31:14 +00001545** This function compares the two table row records specified by
1546** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1547** or positive integer if {nKey1, pKey1} is less than, equal to or
1548** greater than {nKey2, pKey2}.
1549**
danielk197784ac9d02004-05-18 09:58:06 +00001550** This function is pretty inefficient and will probably be replaced
danielk1977eb015e02004-05-18 01:31:14 +00001551** by something else in the near future. It is currently required
1552** by compound SELECT operators.
1553*/
1554int sqlite3VdbeRowCompare(
1555 void *userData,
1556 int nKey1, const void *pKey1,
1557 int nKey2, const void *pKey2
1558){
drhd3d39e92004-05-20 22:16:29 +00001559 KeyInfo *pKeyInfo = (KeyInfo*)userData;
danielk1977eb015e02004-05-18 01:31:14 +00001560 int offset1 = 0;
1561 int offset2 = 0;
danielk197784ac9d02004-05-18 09:58:06 +00001562 int toffset1 = 0;
1563 int toffset2 = 0;
1564 int i;
danielk1977b1bc9532004-05-22 03:05:33 +00001565 u8 enc = pKeyInfo->enc;
danielk1977eb015e02004-05-18 01:31:14 +00001566 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1567 const unsigned char *aKey2 = (const unsigned char *)pKey2;
1568
drhd3d39e92004-05-20 22:16:29 +00001569 assert( pKeyInfo );
1570 assert( pKeyInfo->nField>0 );
danielk197784ac9d02004-05-18 09:58:06 +00001571
drhd3d39e92004-05-20 22:16:29 +00001572 for( i=0; i<pKeyInfo->nField; i++ ){
danielk197784ac9d02004-05-18 09:58:06 +00001573 u64 dummy;
1574 offset1 += sqlite3GetVarint(&aKey1[offset1], &dummy);
1575 offset2 += sqlite3GetVarint(&aKey1[offset1], &dummy);
1576 }
1577
drhd3d39e92004-05-20 22:16:29 +00001578 for( i=0; i<pKeyInfo->nField; i++ ){
danielk197784ac9d02004-05-18 09:58:06 +00001579 Mem mem1;
1580 Mem mem2;
1581 u64 serial_type1;
1582 u64 serial_type2;
1583 int rc;
1584
1585 /* Read the serial types for the next element in each key. */
1586 toffset1 += sqlite3GetVarint(&aKey1[toffset1], &serial_type1);
1587 toffset2 += sqlite3GetVarint(&aKey2[toffset2], &serial_type2);
1588
1589 assert( serial_type1 && serial_type2 );
1590
1591 /* Assert that there is enough space left in each key for the blob of
1592 ** data to go with the serial type just read. This assert may fail if
1593 ** the file is corrupted. Then read the value from each key into mem1
1594 ** and mem2 respectively.
1595 */
danielk1977b1bc9532004-05-22 03:05:33 +00001596 offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
1597 offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);
danielk197784ac9d02004-05-18 09:58:06 +00001598
drhd3d39e92004-05-20 22:16:29 +00001599 rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
danielk197784ac9d02004-05-18 09:58:06 +00001600 if( mem1.flags&MEM_Dyn ){
1601 sqliteFree(mem1.z);
1602 }
1603 if( mem2.flags&MEM_Dyn ){
1604 sqliteFree(mem2.z);
1605 }
1606 if( rc!=0 ){
1607 return rc;
1608 }
1609 }
1610
1611 return 0;
danielk1977eb015e02004-05-18 01:31:14 +00001612}
1613
1614
1615/*
danielk1977183f9f72004-05-13 05:20:26 +00001616** pCur points at an index entry. Read the rowid (varint occuring at
1617** the end of the entry and store it in *rowid. Return SQLITE_OK if
1618** everything works, or an error code otherwise.
1619*/
1620int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1621 i64 sz;
1622 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001623 char buf[10];
danielk1977183f9f72004-05-13 05:20:26 +00001624 int len;
1625 u64 r;
1626
1627 rc = sqlite3BtreeKeySize(pCur, &sz);
1628 if( rc!=SQLITE_OK ){
1629 return rc;
1630 }
danielk19773d1bfea2004-05-14 11:00:53 +00001631 len = ((sz>10)?10:sz);
1632
1633 /* If there are less than 2 bytes in the key, this cannot be
1634 ** a valid index entry. In practice this comes up for a query
1635 ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table
1636 ** with an index on x. In this case just call the rowid 0.
1637 */
1638 if( len<2 ){
1639 *rowid = 0;
1640 return SQLITE_OK;
1641 }
danielk1977183f9f72004-05-13 05:20:26 +00001642
1643 rc = sqlite3BtreeKey(pCur, sz-len, len, buf);
1644 if( rc!=SQLITE_OK ){
1645 return rc;
1646 }
1647
danielk19773d1bfea2004-05-14 11:00:53 +00001648 len--;
1649 while( buf[len-1] && --len );
danielk1977183f9f72004-05-13 05:20:26 +00001650
danielk19773d1bfea2004-05-14 11:00:53 +00001651 sqlite3GetVarint(&buf[len], &r);
danielk1977183f9f72004-05-13 05:20:26 +00001652 *rowid = r;
1653 return SQLITE_OK;
1654}
1655
drh7cf6e4d2004-05-19 14:56:55 +00001656/*
drhd3d39e92004-05-20 22:16:29 +00001657** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001658** the key string in pKey (of length nKey). Write into *pRes a number
1659** that is negative, zero, or positive if pC is less than, equal to,
1660** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001661**
1662** pKey might contain fewer terms than the cursor.
drh7cf6e4d2004-05-19 14:56:55 +00001663*/
danielk1977183f9f72004-05-13 05:20:26 +00001664int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001665 Cursor *pC, /* The cursor to compare against */
1666 int nKey, const u8 *pKey, /* The key to compare */
1667 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001668){
1669 unsigned char *pCellKey;
1670 u64 nCellKey;
1671 int freeCellKey = 0;
1672 int rc;
1673 int len;
danielk19773d1bfea2004-05-14 11:00:53 +00001674 BtCursor *pCur = pC->pCursor;
danielk1977183f9f72004-05-13 05:20:26 +00001675
1676 sqlite3BtreeKeySize(pCur, &nCellKey);
1677 if( nCellKey<=0 ){
1678 *res = 0;
1679 return SQLITE_OK;
1680 }
1681
1682 pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey);
1683 if( !pCellKey ){
drh10617cd2004-05-14 15:27:27 +00001684 pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey);
danielk1977183f9f72004-05-13 05:20:26 +00001685 if( !pCellKey ){
1686 return SQLITE_NOMEM;
1687 }
1688 freeCellKey = 1;
1689 rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
1690 if( rc!=SQLITE_OK ){
1691 sqliteFree(pCellKey);
1692 return rc;
1693 }
1694 }
1695
1696 len = nCellKey-2;
1697 while( pCellKey[len] && --len );
1698
drhd3d39e92004-05-20 22:16:29 +00001699 *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, len, pCellKey, nKey, pKey);
danielk1977183f9f72004-05-13 05:20:26 +00001700
1701 if( freeCellKey ){
1702 sqliteFree(pCellKey);
1703 }
1704 return SQLITE_OK;
1705}
danielk19777e18c252004-05-25 11:47:24 +00001706
1707/*
1708** Parameter "enc" is one of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be.
1709** Return the corresponding MEM_Utf* value.
1710*/
1711static int encToFlags(u8 enc){
1712 switch( enc ){
1713 case TEXT_Utf8: return MEM_Utf8;
1714 case TEXT_Utf16be: return MEM_Utf16be;
1715 case TEXT_Utf16le: return MEM_Utf16le;
1716 }
1717 assert(0);
1718}
1719static u8 flagsToEnc(int flags){
1720 switch( flags&(MEM_Utf8|MEM_Utf16be|MEM_Utf16le) ){
1721 case MEM_Utf8: return TEXT_Utf8;
1722 case MEM_Utf16le: return TEXT_Utf16le;
1723 case MEM_Utf16be: return TEXT_Utf16be;
1724 }
1725 return 0;
1726}
1727
1728/*
1729** Delete any previous value and set the value stored in *pMem to NULL.
1730*/
1731void sqlite3VdbeMemSetNull(Mem *pMem){
1732 if( pMem->flags&MEM_Dyn ){
1733 sqliteFree(pMem->z);
1734 }
1735 pMem->flags = MEM_Null;
1736}
1737
1738/*
1739** Delete any previous value and set the value stored in *pMem to val,
1740** manifest type INTEGER.
1741*/
1742void sqlite3VdbeMemSetInt(Mem *pMem, i64 val){
1743 MemSetNull(pMem);
1744 pMem->i = val;
1745 pMem->flags = MEM_Int;
1746}
1747
1748/*
1749** Delete any previous value and set the value stored in *pMem to val,
1750** manifest type REAL.
1751*/
1752void sqlite3VdbeMemSetReal(Mem *pMem, double val){
1753 MemSetNull(pMem);
1754 pMem->r = val;
1755 pMem->flags = MEM_Real;
1756}
1757
1758/*
1759** Copy the contents of memory cell pFrom into pTo.
1760*/
1761int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
1762 if( pTo->flags&MEM_Dyn ){
1763 sqliteFree(pTo->z);
1764 }
1765
1766 memcpy(pTo, pFrom, sizeof(*pFrom));
1767 if( pTo->flags&MEM_Short ){
1768 pTo->z = pTo->zShort;
1769 }
1770 else if( pTo->flags&(MEM_Ephem|MEM_Dyn) ){
1771 pTo->flags = pTo->flags&(~(MEM_Static|MEM_Ephem|MEM_Short|MEM_Dyn));
1772 if( pTo->n>NBFS ){
1773 pTo->z = sqliteMalloc(pTo->n);
1774 if( !pTo->z ) return SQLITE_NOMEM;
1775 pTo->flags |= MEM_Dyn;
1776 }else{
1777 pTo->z = pTo->zShort;
1778 pTo->flags |= MEM_Short;
1779 }
1780 memcpy(pTo->z, pFrom->z, pTo->n);
1781 }
1782 return SQLITE_OK;
1783}
1784
1785int sqlite3VdbeMemSetStr(
1786 Mem *pMem, /* Memory cell to set to string value */
1787 const char *z, /* String pointer */
1788 int n, /* Bytes in string, or negative */
1789 u8 enc, /* Encoding of z */
1790 int eCopy /* True if this function should make a copy of z */
1791){
1792 Mem tmp;
1793
1794 if( !z ){
1795 /* If z is NULL, just set *pMem to contain NULL. */
1796 MemSetNull(pMem);
1797 return SQLITE_OK;
1798 }
1799
1800 tmp.z = (char *)z;
1801 if( eCopy ){
1802 tmp.flags = MEM_Ephem|MEM_Str;
1803 }else{
1804 tmp.flags = MEM_Static|MEM_Str;
1805 }
1806 tmp.flags |= encToFlags(enc);
1807 tmp.n = n;
1808 switch( enc ){
1809 case 0:
1810 tmp.flags |= MEM_Blob;
1811 break;
1812
1813 case TEXT_Utf8:
1814 tmp.flags |= MEM_Utf8;
1815 if( n<0 ) tmp.n = strlen(z)+1;
1816 tmp.flags |= ((tmp.z[tmp.n-1])?0:MEM_Term);
1817 break;
1818
1819 case TEXT_Utf16le:
1820 case TEXT_Utf16be:
1821 tmp.flags |= (enc==TEXT_Utf16le?MEM_Utf16le:MEM_Utf16be);
1822 if( n<0 ) tmp.n = sqlite3utf16ByteLen(z,-1)+1;
1823 tmp.flags |= ((tmp.z[tmp.n-1]||tmp.z[tmp.n-2])?0:MEM_Term);
1824 break;
1825
1826 default:
1827 assert(0);
1828 }
1829 return sqlite3VdbeMemCopy(pMem, &tmp);
1830}
1831
1832int sqlite3VdbeMemNulTerminate(Mem *pMem){
1833 int nulTermLen;
1834 int f = pMem->flags;
1835
1836 assert( pMem->flags&MEM_Str && !pMem->flags&MEM_Term );
1837 assert( flagsToEnc(pMem->flags) );
1838
1839 nulTermLen = (flagsToEnc(f)==TEXT_Utf8?1:2);
1840
1841 if( pMem->n+nulTermLen<=NBFS ){
1842 /* If the string plus the nul terminator will fit in the Mem.zShort
1843 ** buffer, and it is not already stored there, copy it there.
1844 */
1845 if( !(f&MEM_Short) ){
1846 memcpy(pMem->z, pMem->zShort, pMem->n);
1847 if( f&MEM_Dyn ){
1848 sqliteFree(pMem->z);
1849 }
1850 pMem->z = pMem->zShort;
1851 pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Dyn);
1852 pMem->flags |= MEM_Short;
1853 }
1854 }else{
1855 /* Otherwise we have to malloc for memory. If the string is already
1856 ** dynamic, use sqliteRealloc(). Otherwise sqliteMalloc() enough
1857 ** space for the string and the nul terminator, and copy the string
1858 ** data there.
1859 */
1860 if( f&MEM_Dyn ){
1861 pMem->z = (char *)sqliteRealloc(pMem->z, pMem->n+nulTermLen);
1862 if( !pMem->z ){
1863 return SQLITE_NOMEM;
1864 }
1865 }else{
1866 char *z = (char *)sqliteMalloc(pMem->n+nulTermLen);
1867 memcpy(z, pMem->z, pMem->n);
1868 pMem->z = z;
1869 pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
1870 pMem->flags |= MEM_Dyn;
1871 }
1872 }
1873
1874 /* pMem->z now points at the string data, with enough space at the end
1875 ** to insert the nul nul terminator. pMem->n has not yet been updated.
1876 */
1877 memcpy(&pMem->z[pMem->n], "\0\0", nulTermLen);
1878 pMem->n += nulTermLen;
1879 pMem->flags |= MEM_Term;
1880}
1881
1882/*
drhf9b596e2004-05-26 16:54:42 +00001883** The following ten routines, named sqlite3_result_*(), are used to
danielk19777e18c252004-05-25 11:47:24 +00001884** return values or errors from user-defined functions and aggregate
1885** operations. They are commented in the header file sqlite.h (sqlite.h.in)
1886*/
drhf9b596e2004-05-26 16:54:42 +00001887void sqlite3_result(sqlite3_context *pCtx, sqlite3_value *pValue){
1888 sqlite3VdbeMemCopy(&pCtx->s, pValue);
1889}
danielk19777e18c252004-05-25 11:47:24 +00001890void sqlite3_result_int32(sqlite3_context *pCtx, int iVal){
1891 MemSetInt(&pCtx->s, iVal);
1892}
1893void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
1894 MemSetInt(&pCtx->s, iVal);
1895}
1896void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
1897 MemSetReal(&pCtx->s, rVal);
1898}
1899void sqlite3_result_null(sqlite3_context *pCtx){
1900 MemSetNull(&pCtx->s);
1901}
1902void sqlite3_result_text(
1903 sqlite3_context *pCtx,
1904 const char *z,
1905 int n,
1906 int eCopy
1907){
1908 MemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy);
1909}
1910void sqlite3_result_text16(
1911 sqlite3_context *pCtx,
1912 const void *z,
1913 int n,
1914 int eCopy
1915){
1916 MemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy);
1917}
1918void sqlite3_result_blob(
1919 sqlite3_context *pCtx,
1920 const void *z,
1921 int n,
1922 int eCopy
1923){
1924 assert( n>0 );
1925 MemSetStr(&pCtx->s, z, n, 0, eCopy);
1926}
1927void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
1928 pCtx->isError = 1;
1929 MemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1);
1930}
1931void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
1932 pCtx->isError = 1;
1933 MemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1);
1934}