blob: b7b73fd72d8bab725cec5f38b3b17148259ba10a [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <ctype.h>
20#include "vdbeInt.h"
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
danielk1977132872b2004-05-10 10:37:18 +000025** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000026** as they are added to the instruction stream.
27*/
28#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +000029int sqlite3_vdbe_addop_trace = 0;
drh9a324642003-09-06 20:12:01 +000030#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
danielk19774adee202004-05-08 08:23:19 +000036Vdbe *sqlite3VdbeCreate(sqlite *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
59** Add a new instruction to the list of instructions current in the
60** VDBE. Return the address of the new instruction.
61**
62** Parameters:
63**
64** p Pointer to the VDBE
65**
66** op The opcode for this instruction
67**
68** p1, p2 First two of the three possible operands.
69**
danielk19774adee202004-05-08 08:23:19 +000070** Use the sqlite3VdbeResolveLabel() function to fix an address and
71** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000072** operand.
73*/
danielk19774adee202004-05-08 08:23:19 +000074int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000075 int i;
drh701a0ae2004-02-22 20:05:00 +000076 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +000077
78 i = p->nOp;
79 p->nOp++;
80 assert( p->magic==VDBE_MAGIC_INIT );
81 if( i>=p->nOpAlloc ){
82 int oldSize = p->nOpAlloc;
83 Op *aNew;
84 p->nOpAlloc = p->nOpAlloc*2 + 100;
85 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
86 if( aNew==0 ){
87 p->nOpAlloc = oldSize;
88 return 0;
89 }
90 p->aOp = aNew;
91 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
92 }
drh701a0ae2004-02-22 20:05:00 +000093 pOp = &p->aOp[i];
94 pOp->opcode = op;
95 pOp->p1 = p1;
drh9a324642003-09-06 20:12:01 +000096 if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){
97 p2 = p->aLabel[-1-p2];
98 }
drh701a0ae2004-02-22 20:05:00 +000099 pOp->p2 = p2;
100 pOp->p3 = 0;
101 pOp->p3type = P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000102#ifndef NDEBUG
drhd3d39e92004-05-20 22:16:29 +0000103 pOp->zComment = 0;
danielk1977132872b2004-05-10 10:37:18 +0000104 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000105#endif
106 return i;
107}
108
109/*
drh701a0ae2004-02-22 20:05:00 +0000110** Add an opcode that includes the p3 value.
111*/
drheb2e1762004-05-27 01:53:56 +0000112int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000113 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
114 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000115 return addr;
116}
117
118/*
drh9a324642003-09-06 20:12:01 +0000119** Create a new symbolic label for an instruction that has yet to be
120** coded. The symbolic label is really just a negative number. The
121** label can be used as the P2 value of an operation. Later, when
122** the label is resolved to a specific address, the VDBE will scan
123** through its operation list and change all values of P2 which match
124** the label into the resolved address.
125**
126** The VDBE knows that a P2 value is a label because labels are
127** always negative and P2 values are suppose to be non-negative.
128** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000129**
130** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000131*/
danielk19774adee202004-05-08 08:23:19 +0000132int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000133 int i;
134 i = p->nLabel++;
135 assert( p->magic==VDBE_MAGIC_INIT );
136 if( i>=p->nLabelAlloc ){
137 int *aNew;
138 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
139 aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
140 if( aNew==0 ){
141 sqliteFree(p->aLabel);
142 }
143 p->aLabel = aNew;
144 }
145 if( p->aLabel==0 ){
146 p->nLabel = 0;
147 p->nLabelAlloc = 0;
148 return 0;
149 }
150 p->aLabel[i] = -1;
151 return -1-i;
152}
153
154/*
155** Resolve label "x" to be the address of the next instruction to
156** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000157** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000158*/
danielk19774adee202004-05-08 08:23:19 +0000159void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh9a324642003-09-06 20:12:01 +0000160 int j;
161 assert( p->magic==VDBE_MAGIC_INIT );
162 if( x<0 && (-x)<=p->nLabel && p->aOp ){
163 if( p->aLabel[-1-x]==p->nOp ) return;
164 assert( p->aLabel[-1-x]<0 );
165 p->aLabel[-1-x] = p->nOp;
166 for(j=0; j<p->nOp; j++){
167 if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp;
168 }
169 }
170}
171
172/*
173** Return the address of the next instruction to be inserted.
174*/
danielk19774adee202004-05-08 08:23:19 +0000175int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000176 assert( p->magic==VDBE_MAGIC_INIT );
177 return p->nOp;
178}
179
180/*
181** Add a whole list of operations to the operation stack. Return the
182** address of the first operation added.
183*/
danielk19774adee202004-05-08 08:23:19 +0000184int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000185 int addr;
186 assert( p->magic==VDBE_MAGIC_INIT );
187 if( p->nOp + nOp >= p->nOpAlloc ){
188 int oldSize = p->nOpAlloc;
189 Op *aNew;
190 p->nOpAlloc = p->nOpAlloc*2 + nOp + 10;
191 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
192 if( aNew==0 ){
193 p->nOpAlloc = oldSize;
194 return 0;
195 }
196 p->aOp = aNew;
197 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
198 }
199 addr = p->nOp;
200 if( nOp>0 ){
201 int i;
drh905793e2004-02-21 13:31:09 +0000202 VdbeOpList const *pIn = aOp;
203 for(i=0; i<nOp; i++, pIn++){
204 int p2 = pIn->p2;
205 VdbeOp *pOut = &p->aOp[i+addr];
206 pOut->opcode = pIn->opcode;
207 pOut->p1 = pIn->p1;
208 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
209 pOut->p3 = pIn->p3;
210 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000211#ifndef NDEBUG
drhd3d39e92004-05-20 22:16:29 +0000212 pOut->zComment = 0;
danielk1977132872b2004-05-10 10:37:18 +0000213 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000215 }
216#endif
217 }
218 p->nOp += nOp;
219 }
220 return addr;
221}
222
223/*
224** Change the value of the P1 operand for a specific instruction.
225** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000226** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000227** few minor changes to the program.
228*/
danielk19774adee202004-05-08 08:23:19 +0000229void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000230 assert( p->magic==VDBE_MAGIC_INIT );
231 if( p && addr>=0 && p->nOp>addr && p->aOp ){
232 p->aOp[addr].p1 = val;
233 }
234}
235
236/*
237** Change the value of the P2 operand for a specific instruction.
238** This routine is useful for setting a jump destination.
239*/
danielk19774adee202004-05-08 08:23:19 +0000240void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000241 assert( val>=0 );
242 assert( p->magic==VDBE_MAGIC_INIT );
243 if( p && addr>=0 && p->nOp>addr && p->aOp ){
244 p->aOp[addr].p2 = val;
245 }
246}
247
248/*
249** Change the value of the P3 operand for a specific instruction.
250** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000251** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000252** few minor changes to the program.
253**
254** If n>=0 then the P3 operand is dynamic, meaning that a copy of
255** the string is made into memory obtained from sqliteMalloc().
256** A value of n==0 means copy bytes of zP3 up to and including the
257** first null byte. If n>0 then copy n+1 bytes of zP3.
258**
259** If n==P3_STATIC it means that zP3 is a pointer to a constant static
260** string and we can just copy the pointer. n==P3_POINTER means zP3 is
drhd3d39e92004-05-20 22:16:29 +0000261** a pointer to some object other than a string. n==P3_COLLSEQ and
262** n==P3_KEYINFO mean that zP3 is a pointer to a CollSeq or KeyInfo
263** structure. A copy is made of KeyInfo structures into memory obtained
264** from sqliteMalloc.
drh9a324642003-09-06 20:12:01 +0000265**
266** If addr<0 then change P3 on the most recently inserted instruction.
267*/
danielk19774adee202004-05-08 08:23:19 +0000268void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000269 Op *pOp;
270 assert( p->magic==VDBE_MAGIC_INIT );
271 if( p==0 || p->aOp==0 ) return;
272 if( addr<0 || addr>=p->nOp ){
273 addr = p->nOp - 1;
274 if( addr<0 ) return;
275 }
276 pOp = &p->aOp[addr];
277 if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
278 sqliteFree(pOp->p3);
279 pOp->p3 = 0;
280 }
281 if( zP3==0 ){
282 pOp->p3 = 0;
283 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000284 }else if( n==P3_KEYINFO ){
285 KeyInfo *pKeyInfo;
286 int nField, nByte;
287 nField = ((KeyInfo*)zP3)->nField;
288 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]);
drheafe05b2004-06-13 00:54:01 +0000289 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000290 pOp->p3 = (char*)pKeyInfo;
291 if( pKeyInfo ){
292 memcpy(pKeyInfo, zP3, nByte);
293 pOp->p3type = P3_KEYINFO;
294 }else{
295 pOp->p3type = P3_NOTUSED;
296 }
drhffbc3082004-05-21 01:29:06 +0000297 }else if( n==P3_KEYINFO_HANDOFF ){
298 pOp->p3 = (char*)zP3;
299 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000300 }else if( n<0 ){
301 pOp->p3 = (char*)zP3;
302 pOp->p3type = n;
303 }else{
danielk19774adee202004-05-08 08:23:19 +0000304 sqlite3SetNString(&pOp->p3, zP3, n, 0);
drh9a324642003-09-06 20:12:01 +0000305 pOp->p3type = P3_DYNAMIC;
306 }
307}
308
309/*
310** If the P3 operand to the specified instruction appears
311** to be a quoted string token, then this procedure removes
312** the quotes.
313**
314** The quoting operator can be either a grave ascent (ASCII 0x27)
315** or a double quote character (ASCII 0x22). Two quotes in a row
316** resolve to be a single actual quote character within the string.
317*/
danielk19774adee202004-05-08 08:23:19 +0000318void sqlite3VdbeDequoteP3(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000319 Op *pOp;
320 assert( p->magic==VDBE_MAGIC_INIT );
drh51e9a442004-01-16 16:42:53 +0000321 if( p->aOp==0 ) return;
322 if( addr<0 || addr>=p->nOp ){
323 addr = p->nOp - 1;
324 if( addr<0 ) return;
325 }
drh9a324642003-09-06 20:12:01 +0000326 pOp = &p->aOp[addr];
327 if( pOp->p3==0 || pOp->p3[0]==0 ) return;
drhd3d39e92004-05-20 22:16:29 +0000328 if( pOp->p3type==P3_STATIC ){
drh9a324642003-09-06 20:12:01 +0000329 pOp->p3 = sqliteStrDup(pOp->p3);
330 pOp->p3type = P3_DYNAMIC;
331 }
drhd3d39e92004-05-20 22:16:29 +0000332 assert( pOp->p3type==P3_DYNAMIC );
danielk19774adee202004-05-08 08:23:19 +0000333 sqlite3Dequote(pOp->p3);
drh9a324642003-09-06 20:12:01 +0000334}
335
336/*
337** On the P3 argument of the given instruction, change all
338** strings of whitespace characters into a single space and
339** delete leading and trailing whitespace.
340*/
danielk19774adee202004-05-08 08:23:19 +0000341void sqlite3VdbeCompressSpace(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000342 unsigned char *z;
343 int i, j;
344 Op *pOp;
345 assert( p->magic==VDBE_MAGIC_INIT );
346 if( p->aOp==0 || addr<0 || addr>=p->nOp ) return;
347 pOp = &p->aOp[addr];
drhd3d39e92004-05-20 22:16:29 +0000348 if( pOp->p3type==P3_STATIC ){
drh9a324642003-09-06 20:12:01 +0000349 pOp->p3 = sqliteStrDup(pOp->p3);
350 pOp->p3type = P3_DYNAMIC;
351 }
drhd3d39e92004-05-20 22:16:29 +0000352 assert( pOp->p3type==P3_DYNAMIC );
drh9a324642003-09-06 20:12:01 +0000353 z = (unsigned char*)pOp->p3;
354 if( z==0 ) return;
355 i = j = 0;
356 while( isspace(z[i]) ){ i++; }
357 while( z[i] ){
358 if( isspace(z[i]) ){
359 z[j++] = ' ';
360 while( isspace(z[++i]) ){}
361 }else{
362 z[j++] = z[i++];
363 }
364 }
365 while( j>0 && isspace(z[j-1]) ){ j--; }
366 z[j] = 0;
367}
368
drhd3d39e92004-05-20 22:16:29 +0000369#ifndef NDEBUG
370/*
371** Add comment text to the most recently inserted opcode
372*/
373void sqlite3VdbeAddComment(Vdbe *p, const char *zFormat, ...){
374 va_list ap;
375 VdbeOp *pOp;
376 char *zText;
377 va_start(ap, zFormat);
378 zText = sqlite3_vmprintf(zFormat, ap);
379 va_end(ap);
380 pOp = &p->aOp[p->nOp-1];
381 sqliteFree(pOp->zComment);
382 pOp->zComment = zText;
383}
384#endif
385
drh9a324642003-09-06 20:12:01 +0000386/*
danielk197784ac9d02004-05-18 09:58:06 +0000387** Search the current program starting at instruction addr for the given
388** opcode and P2 value. Return the address plus 1 if found and 0 if not
389** found.
drh9a324642003-09-06 20:12:01 +0000390*/
danielk197784ac9d02004-05-18 09:58:06 +0000391int sqlite3VdbeFindOp(Vdbe *p, int addr, int op, int p2){
drh9a324642003-09-06 20:12:01 +0000392 int i;
393 assert( p->magic==VDBE_MAGIC_INIT );
danielk197784ac9d02004-05-18 09:58:06 +0000394 for(i=addr; i<p->nOp; i++){
drh9a324642003-09-06 20:12:01 +0000395 if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1;
396 }
397 return 0;
398}
399
400/*
401** Return the opcode for a given address.
402*/
danielk19774adee202004-05-08 08:23:19 +0000403VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000404 assert( p->magic==VDBE_MAGIC_INIT );
405 assert( addr>=0 && addr<p->nOp );
406 return &p->aOp[addr];
407}
408
409/*
drhd3d39e92004-05-20 22:16:29 +0000410** Compute a string that describes the P3 parameter for an opcode.
411** Use zTemp for any required temporary buffer space.
412*/
413static char *displayP3(Op *pOp, char *zTemp, int nTemp){
414 char *zP3;
415 assert( nTemp>=20 );
416 switch( pOp->p3type ){
417 case P3_POINTER: {
418 sprintf(zTemp, "ptr(%#x)", (int)pOp->p3);
419 zP3 = zTemp;
420 break;
421 }
422 case P3_KEYINFO: {
423 int i, j;
424 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
425 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
426 i = strlen(zTemp);
427 for(j=0; j<pKeyInfo->nField; j++){
428 CollSeq *pColl = pKeyInfo->aColl[j];
429 if( pColl ){
430 int n = strlen(pColl->zName);
431 if( i+n>nTemp-6 ){
432 strcpy(&zTemp[i],",...");
433 break;
434 }
435 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000436 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000437 zTemp[i++] = '-';
438 }
439 strcpy(&zTemp[i], pColl->zName);
440 i += n;
441 }else if( i+4<nTemp-6 ){
442 strcpy(&zTemp[i],",nil");
443 i += 4;
444 }
445 }
446 zTemp[i++] = ')';
447 zTemp[i] = 0;
448 assert( i<nTemp );
449 zP3 = zTemp;
450 break;
451 }
452 case P3_COLLSEQ: {
453 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000454 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000455 zP3 = zTemp;
456 break;
457 }
drhf9b596e2004-05-26 16:54:42 +0000458 case P3_FUNCDEF: {
459 FuncDef *pDef = (FuncDef*)pOp->p3;
460 char zNum[30];
461 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
462 sprintf(zNum,"(%d)", pDef->nArg);
463 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
464 strcat(zTemp, zNum);
465 }
466 zP3 = zTemp;
467 break;
468 }
drhd3d39e92004-05-20 22:16:29 +0000469 default: {
470 zP3 = pOp->p3;
471 if( zP3==0 ){
472 zP3 = "";
473 }
474 }
475 }
476 return zP3;
477}
478
479
drh9a324642003-09-06 20:12:01 +0000480#if !defined(NDEBUG) || defined(VDBE_PROFILE)
481/*
482** Print a single opcode. This routine is used for debugging only.
483*/
danielk19774adee202004-05-08 08:23:19 +0000484void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000485 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000486 char zPtr[50];
487 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
488 static const char *zFormat2 = "%4d %-13s %4d %4d %-20s -- %s\n";
drh9a324642003-09-06 20:12:01 +0000489 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000490 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
491#ifdef NDEBUG
492 fprintf(pOut, zFormat1,
493 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
494#else
495 fprintf(pOut, pOp->zComment ? zFormat2 : zFormat1,
496 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3,pOp->zComment);
497#endif
drh9a324642003-09-06 20:12:01 +0000498 fflush(pOut);
499}
500#endif
501
502/*
503** Give a listing of the program in the virtual machine.
504**
danielk19774adee202004-05-08 08:23:19 +0000505** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000506** running the code, it invokes the callback once for each instruction.
507** This feature is used to implement "EXPLAIN".
508*/
danielk19774adee202004-05-08 08:23:19 +0000509int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000510 Vdbe *p /* The VDBE */
511){
512 sqlite *db = p->db;
513 int i;
drh826fb5a2004-02-14 23:59:57 +0000514 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000515
drh9a324642003-09-06 20:12:01 +0000516 assert( p->explain );
danielk197718f41892004-05-22 07:27:46 +0000517
518 /* Even though this opcode does not put dynamic strings onto the
519 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000520 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000521 */
522 if( p->pTos==&p->aStack[4] ){
523 for(i=0; i<5; i++){
danielk1977d8123362004-06-12 09:25:12 +0000524 sqlite3VdbeMemRelease(&p->aStack[i]);
danielk197718f41892004-05-22 07:27:46 +0000525 p->aStack[i].flags = 0;
526 }
527 }
danielk197718f41892004-05-22 07:27:46 +0000528 p->resOnStack = 0;
529
530 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000531 if( i>=p->nOp ){
532 p->rc = SQLITE_OK;
533 rc = SQLITE_DONE;
534 }else if( db->flags & SQLITE_Interrupt ){
535 db->flags &= ~SQLITE_Interrupt;
536 if( db->magic!=SQLITE_MAGIC_BUSY ){
537 p->rc = SQLITE_MISUSE;
538 }else{
539 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000540 }
drh826fb5a2004-02-14 23:59:57 +0000541 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000542 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000543 }else{
drhd3d39e92004-05-20 22:16:29 +0000544 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000545 Mem *pMem = p->aStack;
546 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000547 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000548 pMem->i = i; /* Program counter */
549 pMem++;
550
551 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
552 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
553 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000554 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000555 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000556 pMem++;
557
558 pMem->flags = MEM_Int;
559 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000560 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000561 pMem++;
562
563 pMem->flags = MEM_Int;
564 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000565 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000566 pMem++;
567
568 pMem->flags = MEM_Short|MEM_Str|MEM_Term; /* P3 */
569 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drh9c054832004-05-31 18:51:57 +0000570 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000571 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000572
drh826fb5a2004-02-14 23:59:57 +0000573 p->nResColumn = 5;
drheb2e1762004-05-27 01:53:56 +0000574 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000575 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000576 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000577 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000578 }
drh826fb5a2004-02-14 23:59:57 +0000579 return rc;
drh9a324642003-09-06 20:12:01 +0000580}
581
582/*
583** Prepare a virtual machine for execution. This involves things such
584** as allocating stack space and initializing the program counter.
585** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000586** calls to sqlite3VdbeExec().
drh9a324642003-09-06 20:12:01 +0000587*/
danielk19774adee202004-05-08 08:23:19 +0000588void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000589 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000590 int nVar, /* Number of '?' see in the SQL statement */
drh9a324642003-09-06 20:12:01 +0000591 int isExplain /* True if the EXPLAIN keywords is present */
592){
593 int n;
594
595 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000596 assert( p->magic==VDBE_MAGIC_INIT );
597
598 /* Add a HALT instruction to the very end of the program.
599 */
600 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000601 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000602 }
603
604 /* No instruction ever pushes more than a single element onto the
605 ** stack. And the stack never grows on successive executions of the
606 ** same loop. So the total number of instructions is an upper bound
607 ** on the maximum stack depth required.
608 **
609 ** Allocation all the stack space we will ever need.
610 */
drh82a48512003-09-06 22:45:20 +0000611 if( p->aStack==0 ){
612 p->nVar = nVar;
613 assert( nVar>=0 );
614 n = isExplain ? 10 : p->nOp;
615 p->aStack = sqliteMalloc(
danielk1977b20e56b2004-06-15 13:36:30 +0000616 n*(sizeof(p->aStack[0])+sizeof(Mem*)) /* aStack, apArg */
drh5a12e682004-05-19 11:24:25 +0000617 + p->nVar*sizeof(Mem) /* apVar */
drh82a48512003-09-06 22:45:20 +0000618 );
danielk19776ddcca52004-05-24 23:48:25 +0000619 p->apArg = (Mem **)&p->aStack[n];
danielk1977b20e56b2004-06-15 13:36:30 +0000620 p->apVar = (Mem *)&p->apArg[n];
danielk197754db47e2004-05-19 10:36:43 +0000621 for(n=0; n<p->nVar; n++){
drh5a12e682004-05-19 11:24:25 +0000622 p->apVar[n].flags = MEM_Null;
danielk197754db47e2004-05-19 10:36:43 +0000623 }
drh82a48512003-09-06 22:45:20 +0000624 }
drh9a324642003-09-06 20:12:01 +0000625
drhfaa57ac2004-06-09 14:01:51 +0000626#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000627 if( (p->db->flags & SQLITE_VdbeListing)!=0
628 || sqlite3OsFileExists("vdbe_explain")
629 ){
drh80242052004-06-09 00:48:12 +0000630 int i;
631 printf("VDBE Program Listing:\n");
632 for(i=0; i<p->nOp; i++){
633 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
634 }
635 }
danielk19774adee202004-05-08 08:23:19 +0000636 if( sqlite3OsFileExists("vdbe_trace") ){
drh80242052004-06-09 00:48:12 +0000637 printf("VDBE Execution Trace:\n");
drh9a324642003-09-06 20:12:01 +0000638 p->trace = stdout;
639 }
640#endif
drh6810ce62004-01-31 19:22:56 +0000641 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000642 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000643 p->rc = SQLITE_OK;
644 p->uniqueCnt = 0;
645 p->returnDepth = 0;
646 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000647 p->popStack = 0;
648 p->explain |= isExplain;
649 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000650 p->nChange = 0;
drh9a324642003-09-06 20:12:01 +0000651#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000652 {
653 int i;
654 for(i=0; i<p->nOp; i++){
655 p->aOp[i].cnt = 0;
656 p->aOp[i].cycles = 0;
657 }
drh9a324642003-09-06 20:12:01 +0000658 }
659#endif
660}
661
662
663/*
664** Remove any elements that remain on the sorter for the VDBE given.
665*/
danielk19774adee202004-05-08 08:23:19 +0000666void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000667 while( p->pSort ){
668 Sorter *pSorter = p->pSort;
669 p->pSort = pSorter->pNext;
670 sqliteFree(pSorter->zKey);
danielk1977369f27e2004-06-15 11:40:04 +0000671 sqlite3VdbeMemRelease(&pSorter->data);
drh9a324642003-09-06 20:12:01 +0000672 sqliteFree(pSorter);
673 }
674}
675
676/*
danielk1977e159fdf2004-06-21 10:45:06 +0000677** Free all resources allociated with AggElem pElem, an element of
678** aggregate pAgg.
679*/
danielk1977e3026632004-06-22 11:29:02 +0000680void freeAggElem(AggElem *pElem, Agg *pAgg){
danielk1977e159fdf2004-06-21 10:45:06 +0000681 int i;
682 for(i=0; i<pAgg->nMem; i++){
683 Mem *pMem = &pElem->aMem[i];
drh645f63e2004-06-22 13:22:40 +0000684 if( pAgg->apFunc && pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk1977e159fdf2004-06-21 10:45:06 +0000685 sqlite3_context ctx;
686 ctx.pFunc = pAgg->apFunc[i];
687 ctx.s.flags = MEM_Null;
688 ctx.pAgg = pMem->z;
689 ctx.cnt = pMem->i;
690 ctx.isStep = 0;
691 ctx.isError = 0;
692 (*pAgg->apFunc[i]->xFinalize)(&ctx);
693 pMem->z = ctx.pAgg;
694 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
695 sqliteFree(pMem->z);
696 }
697 sqlite3VdbeMemRelease(&ctx.s);
698 }else{
699 sqlite3VdbeMemRelease(pMem);
700 }
701 }
702 sqliteFree(pElem);
703}
704
705/*
danielk1977ce2663c2004-06-11 13:19:21 +0000706** Reset an Agg structure. Delete all its contents.
707**
708** For installable aggregate functions, if the step function has been
709** called, make sure the finalizer function has also been called. The
710** finalizer might need to free memory that was allocated as part of its
711** private context. If the finalizer has not been called yet, call it
712** now.
713**
714** If db is NULL, then this is being called from sqliteVdbeReset(). In
715** this case clean up all references to the temp-table used for
716** aggregates (if it was ever opened).
717**
718** If db is not NULL, then this is being called from with an OP_AggReset
719** opcode. Open the temp-table, if it has not already been opened and
720** delete the contents of the table used for aggregate information, ready
721** for the next round of aggregate processing.
722*/
723int sqlite3VdbeAggReset(sqlite *db, Agg *pAgg, KeyInfo *pKeyInfo){
danielk1977ce2663c2004-06-11 13:19:21 +0000724 int rc = 0;
725 BtCursor *pCsr = pAgg->pCsr;
726
727 assert( (pCsr && pAgg->nTab>0) || (!pCsr && pAgg->nTab==0)
728 || sqlite3_malloc_failed );
729
730 /* If pCsr is not NULL, then the table used for aggregate information
731 ** is open. Loop through it and free the AggElem* structure pointed at
732 ** by each entry. If the finalizer has not been called for an AggElem,
733 ** do that too. Finally, clear the btree table itself.
734 */
735 if( pCsr ){
736 int res;
737 assert( pAgg->pBtree );
738 assert( pAgg->nTab>0 );
739
740 rc=sqlite3BtreeFirst(pCsr, &res);
741 while( res==0 && rc==SQLITE_OK ){
742 AggElem *pElem;
743 rc = sqlite3BtreeData(pCsr, 0, sizeof(AggElem*), (char *)&pElem);
744 if( res!=SQLITE_OK ){
745 return rc;
746 }
747 assert( pAgg->apFunc!=0 );
danielk1977e159fdf2004-06-21 10:45:06 +0000748 freeAggElem(pElem, pAgg);
danielk1977ce2663c2004-06-11 13:19:21 +0000749 rc=sqlite3BtreeNext(pCsr, &res);
750 }
751 if( rc!=SQLITE_OK ){
752 return rc;
753 }
754
755 sqlite3BtreeCloseCursor(pCsr);
756 sqlite3BtreeClearTable(pAgg->pBtree, pAgg->nTab);
danielk1977e159fdf2004-06-21 10:45:06 +0000757 }else{
758 /* The cursor may not be open because the aggregator was never used,
759 ** or it could be that it was used but there was no GROUP BY clause.
760 */
761 if( pAgg->pCurrent ){
762 freeAggElem(pAgg->pCurrent, pAgg);
763 }
danielk1977ce2663c2004-06-11 13:19:21 +0000764 }
765
766 /* If db is not NULL and we have not yet and we have not yet opened
767 ** the temporary btree then do so and create the table to store aggregate
768 ** information.
769 **
770 ** If db is NULL, then close the temporary btree if it is open.
771 */
772 if( db ){
773 if( !pAgg->pBtree ){
774 assert( pAgg->nTab==0 );
drhe1632b22004-06-12 20:42:29 +0000775 rc = sqlite3BtreeFactory(db, ":memory:", 0, TEMP_PAGES, &pAgg->pBtree);
danielk1977ce2663c2004-06-11 13:19:21 +0000776 if( rc!=SQLITE_OK ) return rc;
danielk197740b38dc2004-06-26 08:38:24 +0000777 sqlite3BtreeBeginTrans(pAgg->pBtree, 1);
danielk1977ce2663c2004-06-11 13:19:21 +0000778 rc = sqlite3BtreeCreateTable(pAgg->pBtree, &pAgg->nTab, 0);
779 if( rc!=SQLITE_OK ) return rc;
780 }
781 assert( pAgg->nTab!=0 );
782
783 rc = sqlite3BtreeCursor(pAgg->pBtree, pAgg->nTab, 1,
784 sqlite3VdbeRecordCompare, pKeyInfo, &pAgg->pCsr);
785 if( rc!=SQLITE_OK ) return rc;
786 }else{
787 if( pAgg->pBtree ){
788 sqlite3BtreeClose(pAgg->pBtree);
789 pAgg->pBtree = 0;
790 pAgg->nTab = 0;
791 }
792 pAgg->pCsr = 0;
793 }
794
795 if( pAgg->apFunc ){
796 sqliteFree(pAgg->apFunc);
797 pAgg->apFunc = 0;
798 }
799 pAgg->pCurrent = 0;
800 pAgg->nMem = 0;
801 pAgg->searching = 0;
802 return SQLITE_OK;
803}
804
drh9a324642003-09-06 20:12:01 +0000805
806/*
807** Delete a keylist
808*/
danielk19774adee202004-05-08 08:23:19 +0000809void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000810 while( p ){
811 Keylist *pNext = p->pNext;
812 sqliteFree(p);
813 p = pNext;
814 }
815}
816
817/*
818** Close a cursor and release all the resources that cursor happens
819** to hold.
820*/
drh4774b132004-06-12 20:12:51 +0000821void sqlite3VdbeFreeCursor(Cursor *pCx){
822 if( pCx==0 ){
823 return;
824 }
drh9a324642003-09-06 20:12:01 +0000825 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000826 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000827 }
828 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000829 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000830 }
831 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000832 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000833 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000834}
835
836/*
837** Close all cursors
838*/
839static void closeAllCursors(Vdbe *p){
840 int i;
841 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000842 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh9a324642003-09-06 20:12:01 +0000843 }
drhd7556d22004-05-14 21:59:40 +0000844 sqliteFree(p->apCsr);
845 p->apCsr = 0;
drh9a324642003-09-06 20:12:01 +0000846 p->nCursor = 0;
847}
848
849/*
drh9a324642003-09-06 20:12:01 +0000850** Clean up the VM after execution.
851**
852** This routine will automatically close any cursors, lists, and/or
853** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000854** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000855*/
856static void Cleanup(Vdbe *p){
857 int i;
drh6810ce62004-01-31 19:22:56 +0000858 if( p->aStack ){
859 Mem *pTos = p->pTos;
860 while( pTos>=p->aStack ){
danielk1977d8123362004-06-12 09:25:12 +0000861 sqlite3VdbeMemRelease(pTos);
drh6810ce62004-01-31 19:22:56 +0000862 pTos--;
863 }
864 p->pTos = pTos;
865 }
drh9a324642003-09-06 20:12:01 +0000866 closeAllCursors(p);
867 if( p->aMem ){
868 for(i=0; i<p->nMem; i++){
danielk1977d8123362004-06-12 09:25:12 +0000869 sqlite3VdbeMemRelease(&p->aMem[i]);
drh9a324642003-09-06 20:12:01 +0000870 }
871 }
872 sqliteFree(p->aMem);
873 p->aMem = 0;
874 p->nMem = 0;
875 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000876 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000877 p->pList = 0;
878 }
danielk19774adee202004-05-08 08:23:19 +0000879 sqlite3VdbeSorterReset(p);
drh9a324642003-09-06 20:12:01 +0000880 if( p->pFile ){
881 if( p->pFile!=stdin ) fclose(p->pFile);
882 p->pFile = 0;
883 }
884 if( p->azField ){
885 sqliteFree(p->azField);
886 p->azField = 0;
887 }
888 p->nField = 0;
889 if( p->zLine ){
890 sqliteFree(p->zLine);
891 p->zLine = 0;
892 }
893 p->nLineAlloc = 0;
danielk1977ce2663c2004-06-11 13:19:21 +0000894 sqlite3VdbeAggReset(0, &p->agg, 0);
drh9a324642003-09-06 20:12:01 +0000895 if( p->keylistStack ){
896 int ii;
897 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000898 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000899 }
900 sqliteFree(p->keylistStack);
901 p->keylistStackDepth = 0;
902 p->keylistStack = 0;
903 }
drh5f968432004-02-21 19:02:30 +0000904 sqliteFree(p->contextStack);
905 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000906 sqliteFree(p->zErrMsg);
907 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000908}
909
910/*
danielk197722322fd2004-05-25 23:35:17 +0000911** Set the number of result columns that will be returned by this SQL
912** statement. This is now set at compile time, rather than during
913** execution of the vdbe program so that sqlite3_column_count() can
914** be called on an SQL statement before sqlite3_step().
915*/
916void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
danielk19773cf86062004-05-26 10:11:05 +0000917 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000918 p->nResColumn = nResColumn;
919}
920
921/*
danielk19773cf86062004-05-26 10:11:05 +0000922** Set the name of the idx'th column to be returned by the SQL statement.
923** zName must be a pointer to a nul terminated string.
924**
925** This call must be made after a call to sqlite3VdbeSetNumCols().
926**
danielk1977d8123362004-06-12 09:25:12 +0000927** If N==P3_STATIC it means that zName is a pointer to a constant static
928** string and we can just copy the pointer. If it is P3_DYNAMIC, then
929** the string is freed using sqliteFree() when the vdbe is finished with
930** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000931*/
932int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
933 int rc;
934 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +0000935 assert( idx<(2*p->nResColumn) );
danielk19773cf86062004-05-26 10:11:05 +0000936
937 /* If the Vdbe.aColName array has not yet been allocated, allocate
938 ** it now.
939 */
940 if( !p->aColName ){
941 int i;
danielk197776d505b2004-05-28 13:13:02 +0000942 p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn*2);
danielk19773cf86062004-05-26 10:11:05 +0000943 if( !p->aColName ){
944 return SQLITE_NOMEM;
945 }
danielk197776d505b2004-05-28 13:13:02 +0000946 for(i=0; i<(2*p->nResColumn); i++){
danielk19773cf86062004-05-26 10:11:05 +0000947 p->aColName[i].flags = MEM_Null;
948 }
949 }
950
951 pColName = &(p->aColName[idx]);
danielk1977d8123362004-06-12 09:25:12 +0000952 if( N==P3_DYNAMIC || N==P3_STATIC ){
953 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000954 }else{
danielk1977d8123362004-06-12 09:25:12 +0000955 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000956 }
957 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
958 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000959 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000960 }
961 return rc;
962}
963
danielk197713adf8a2004-06-03 16:08:41 +0000964/*
965** A read or write transaction may or may not be active on database handle
966** db. If a transaction is active, commit it. If there is a
967** write-transaction spanning more than one database file, this routine
968** takes care of the master journal trickery.
969*/
970static int vdbeCommit(sqlite *db){
971 int i;
972 int nTrans = 0; /* Number of databases with an active write-transaction */
973 int rc = SQLITE_OK;
974 int needXcommit = 0;
975
976 for(i=0; i<db->nDb; i++){
977 Btree *pBt = db->aDb[i].pBt;
978 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
979 needXcommit = 1;
980 if( i!=1 ) nTrans++;
981 }
982 }
983
984 /* If there are any write-transactions at all, invoke the commit hook */
985 if( needXcommit && db->xCommitCallback ){
986 if( db->xCommitCallback(db->pCommitArg) ){
987 return SQLITE_CONSTRAINT;
988 }
989 }
990
danielk197740b38dc2004-06-26 08:38:24 +0000991 /* The simple case - no more than one database file (not counting the
992 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000993 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000994 **
danielk197740b38dc2004-06-26 08:38:24 +0000995 ** If the return value of sqlite3BtreeGetFilename() is a zero length
996 ** string, it means the main database is :memory:. In that case we do
997 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000998 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000999 */
danielk197740b38dc2004-06-26 08:38:24 +00001000 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001001 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001002 Btree *pBt = db->aDb[i].pBt;
1003 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +00001004 rc = sqlite3BtreeSync(pBt, 0);
1005 }
1006 }
1007
1008 /* Do the commit only if all databases successfully synced */
1009 if( rc==SQLITE_OK ){
1010 for(i=0; i<db->nDb; i++){
1011 Btree *pBt = db->aDb[i].pBt;
1012 if( pBt ){
1013 sqlite3BtreeCommit(pBt);
1014 }
danielk197713adf8a2004-06-03 16:08:41 +00001015 }
1016 }
1017 }
1018
1019 /* The complex case - There is a multi-file write-transaction active.
1020 ** This requires a master journal file to ensure the transaction is
1021 ** committed atomicly.
1022 */
1023 else{
1024 char *zMaster = 0; /* File-name for the master journal */
1025 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1026 OsFile master;
1027
1028 /* Select a master journal file name */
1029 do {
drha6abd042004-06-09 17:37:22 +00001030 u32 random;
1031 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001032 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +00001033 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001034 if( !zMaster ){
1035 return SQLITE_NOMEM;
1036 }
1037 }while( sqlite3OsFileExists(zMaster) );
1038
1039 /* Open the master journal. */
drhda71ce12004-06-21 18:14:45 +00001040 memset(&master, 0, sizeof(master));
danielk197713adf8a2004-06-03 16:08:41 +00001041 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
1042 if( rc!=SQLITE_OK ){
1043 sqliteFree(zMaster);
1044 return rc;
1045 }
1046
1047 /* Write the name of each database file in the transaction into the new
1048 ** master journal file. If an error occurs at this point close
1049 ** and delete the master journal file. All the individual journal files
1050 ** still have 'null' as the master journal pointer, so they will roll
1051 ** back independantly if a failure occurs.
1052 */
1053 for(i=0; i<db->nDb; i++){
1054 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001055 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001056 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001057 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001058 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1059 rc = sqlite3OsWrite(&master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001060 if( rc!=SQLITE_OK ){
1061 sqlite3OsClose(&master);
1062 sqlite3OsDelete(zMaster);
1063 sqliteFree(zMaster);
1064 return rc;
1065 }
1066 }
1067 }
1068
danielk197713adf8a2004-06-03 16:08:41 +00001069
danielk19775865e3d2004-06-14 06:03:57 +00001070 /* Sync the master journal file. Before doing this, open the directory
1071 ** the master journal file is store in so that it gets synced too.
1072 */
1073 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1074 rc = sqlite3OsOpenDirectory(zMainFile, &master);
1075 if( rc!=SQLITE_OK ){
1076 sqlite3OsClose(&master);
1077 sqlite3OsDelete(zMaster);
1078 sqliteFree(zMaster);
1079 return rc;
1080 }
1081 rc = sqlite3OsSync(&master);
1082 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001083 sqlite3OsClose(&master);
danielk19775865e3d2004-06-14 06:03:57 +00001084 sqliteFree(zMaster);
1085 return rc;
1086 }
drhc9e06862004-06-09 20:03:08 +00001087
danielk197713adf8a2004-06-03 16:08:41 +00001088 /* Sync all the db files involved in the transaction. The same call
1089 ** sets the master journal pointer in each individual journal. If
1090 ** an error occurs here, do not delete the master journal file.
1091 **
1092 ** If the error occurs during the first call to sqlite3BtreeSync(),
1093 ** then there is a chance that the master journal file will be
1094 ** orphaned. But we cannot delete it, in case the master journal
1095 ** file name was written into the journal file before the failure
1096 ** occured.
1097 */
1098 for(i=0; i<db->nDb; i++){
1099 Btree *pBt = db->aDb[i].pBt;
1100 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1101 rc = sqlite3BtreeSync(pBt, zMaster);
1102 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001103 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001104 sqliteFree(zMaster);
1105 return rc;
1106 }
1107 }
1108 }
danielk1977962398d2004-06-14 09:35:16 +00001109 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001110
danielk1977962398d2004-06-14 09:35:16 +00001111 /* Delete the master journal file. This commits the transaction. After
1112 ** doing this the directory is synced again before any individual
1113 ** transaction files are deleted.
1114 */
danielk197713adf8a2004-06-03 16:08:41 +00001115 rc = sqlite3OsDelete(zMaster);
1116 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001117 sqliteFree(zMaster);
1118 zMaster = 0;
danielk1977962398d2004-06-14 09:35:16 +00001119 rc = sqlite3OsSyncDirectory(zMainFile);
1120 if( rc!=SQLITE_OK ){
1121 /* This is not good. The master journal file has been deleted, but
1122 ** the directory sync failed. There is no completely safe course of
1123 ** action from here. The individual journals contain the name of the
1124 ** master journal file, but there is no way of knowing if that
1125 ** master journal exists now or if it will exist after the operating
1126 ** system crash that may follow the fsync() failure.
1127 */
1128 assert(0);
1129 sqliteFree(zMaster);
1130 return rc;
1131 }
danielk197713adf8a2004-06-03 16:08:41 +00001132
1133 /* All files and directories have already been synced, so the following
1134 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1135 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001136 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001137 ** but some stray 'cold' journals may be lying around. Returning an
1138 ** error code won't help matters.
1139 */
1140 for(i=0; i<db->nDb; i++){
1141 Btree *pBt = db->aDb[i].pBt;
1142 if( pBt ){
1143 sqlite3BtreeCommit(pBt);
1144 }
1145 }
1146 }
danielk1977026d2702004-06-14 13:14:59 +00001147
drh2ac3ee92004-06-07 16:27:46 +00001148 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001149}
1150
danielk19771d850a72004-05-31 08:26:49 +00001151/*
1152** This routine checks that the sqlite3.activeVdbeCnt count variable
1153** matches the number of vdbe's in the list sqlite3.pVdbe that are
1154** currently active. An assertion fails if the two counts do not match.
1155**
1156** This is a no-op if NDEBUG is defined.
1157*/
1158#ifndef NDEBUG
1159static void checkActiveVdbeCnt(sqlite *db){
1160 Vdbe *p;
1161 int cnt = 0;
1162
1163 p = db->pVdbe;
1164 while( p ){
1165 if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
1166 cnt++;
1167 }
1168 p = p->pNext;
1169 }
1170
1171 assert( cnt==db->activeVdbeCnt );
1172}
1173#else
1174#define checkActiveVdbeCnt(x)
1175#endif
1176
danielk19773cf86062004-05-26 10:11:05 +00001177/*
drh9a324642003-09-06 20:12:01 +00001178** Clean up a VDBE after execution but do not delete the VDBE just yet.
1179** Write any error messages into *pzErrMsg. Return the result code.
1180**
1181** After this routine is run, the VDBE should be ready to be executed
1182** again.
1183*/
danielk19779e6db7d2004-06-21 08:18:51 +00001184int sqlite3VdbeReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001185 sqlite *db = p->db;
1186 int i;
danielk19771d850a72004-05-31 08:26:49 +00001187 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
drh9a324642003-09-06 20:12:01 +00001188
1189 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
danielk19771d850a72004-05-31 08:26:49 +00001190 sqlite3Error(p->db, SQLITE_MISUSE, 0 ,0);
drh9a324642003-09-06 20:12:01 +00001191 return SQLITE_MISUSE;
1192 }
1193 if( p->zErrMsg ){
danielk1977106bb232004-05-21 10:08:53 +00001194 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
danielk19779e6db7d2004-06-21 08:18:51 +00001195 sqliteFree(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001196 p->zErrMsg = 0;
drha1f9b5e2004-02-14 16:31:02 +00001197 }else if( p->rc ){
danielk19771d850a72004-05-31 08:26:49 +00001198 sqlite3Error(p->db, p->rc, 0);
danielk1977106bb232004-05-21 10:08:53 +00001199 }else{
1200 sqlite3Error(p->db, SQLITE_OK, 0);
drh9a324642003-09-06 20:12:01 +00001201 }
1202 Cleanup(p);
danielk19771d850a72004-05-31 08:26:49 +00001203
danielk197713adf8a2004-06-03 16:08:41 +00001204 /* What is done now depends on the exit status of the vdbe, the value of
1205 ** the sqlite.autoCommit flag and whether or not there are any other
1206 ** queries in progress. A transaction or statement transaction may need
1207 ** to be committed or rolled back on each open database file.
danielk19771d850a72004-05-31 08:26:49 +00001208 */
1209 checkActiveVdbeCnt(db);
1210 if( db->autoCommit && db->activeVdbeCnt==1 ){
1211 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk197713adf8a2004-06-03 16:08:41 +00001212 /* The auto-commit flag is true, there are no other active queries
1213 ** using this handle and the vdbe program was successful or hit an
1214 ** 'OR FAIL' constraint. This means a commit is required, which is
1215 ** handled a little differently from the other options.
1216 */
1217 p->rc = vdbeCommit(db);
1218 if( p->rc!=SQLITE_OK ){
1219 sqlite3Error(p->db, p->rc, 0);
drhff13c7d2004-06-09 21:01:11 +00001220 if( p->rc==SQLITE_BUSY && p->autoCommitOn ){
1221 /* If we just now have turned autocommit on (meaning we just have
1222 ** finished executing a COMMIT command) but the commit fails due
1223 ** to lock contention, autocommit back off. This gives the user
1224 ** the opportunity to try again after the lock that was preventing
1225 ** the commit has cleared. */
1226 db->autoCommit = 0;
1227 }else{
1228 /* If the command just executed was not a COMMIT command, then
1229 ** rollback whatever the results of that command were */
1230 xFunc = sqlite3BtreeRollback;
1231 }
danielk197713adf8a2004-06-03 16:08:41 +00001232 }
danielk19771d850a72004-05-31 08:26:49 +00001233 }else{
1234 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +00001235 }
danielk19771d850a72004-05-31 08:26:49 +00001236 }else{
1237 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1238 xFunc = sqlite3BtreeCommitStmt;
1239 }else if( p->errorAction==OE_Abort ){
1240 xFunc = sqlite3BtreeRollbackStmt;
1241 }else{
1242 xFunc = sqlite3BtreeRollback;
1243 db->autoCommit = 1;
1244 }
1245 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001246 p->autoCommitOn = 0;
danielk19771d850a72004-05-31 08:26:49 +00001247
danielk197713adf8a2004-06-03 16:08:41 +00001248 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
1249 ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
1250 ** each backend. If an error occurs and the return code is still
1251 ** SQLITE_OK, set the return code to the new error value.
1252 */
1253 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +00001254 int rc;
danielk19771d850a72004-05-31 08:26:49 +00001255 Btree *pBt = db->aDb[i].pBt;
danielk197777d83ba2004-05-31 10:08:14 +00001256 if( pBt ){
1257 rc = xFunc(pBt);
1258 if( p->rc==SQLITE_OK ) p->rc = rc;
1259 }
danielk19771d850a72004-05-31 08:26:49 +00001260 }
1261
danielk1977b28af712004-06-21 06:50:26 +00001262 /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
1263 if( p->changeCntOn ){
1264 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1265 sqlite3VdbeSetChanges(db, p->nChange);
1266 }else{
1267 sqlite3VdbeSetChanges(db, 0);
1268 }
1269 p->nChange = 0;
1270 }
danielk19771d850a72004-05-31 08:26:49 +00001271
1272 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001273 sqlite3RollbackInternalChanges(db);
danielk1977026d2702004-06-14 13:14:59 +00001274 }else if( db->flags & SQLITE_InternChanges ){
danielk1977ec8450f2004-06-19 09:35:36 +00001275 sqlite3CommitInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001276 }
danielk19771d850a72004-05-31 08:26:49 +00001277
1278 if( (p->magic==VDBE_MAGIC_RUN && p->pc>=0) || p->magic==VDBE_MAGIC_HALT ){
1279 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001280 }
danielk19771d850a72004-05-31 08:26:49 +00001281
1282 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001283#ifdef VDBE_PROFILE
1284 {
1285 FILE *out = fopen("vdbe_profile.out", "a");
1286 if( out ){
1287 int i;
1288 fprintf(out, "---- ");
1289 for(i=0; i<p->nOp; i++){
1290 fprintf(out, "%02x", p->aOp[i].opcode);
1291 }
1292 fprintf(out, "\n");
1293 for(i=0; i<p->nOp; i++){
1294 fprintf(out, "%6d %10lld %8lld ",
1295 p->aOp[i].cnt,
1296 p->aOp[i].cycles,
1297 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1298 );
danielk19774adee202004-05-08 08:23:19 +00001299 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001300 }
1301 fclose(out);
1302 }
1303 }
1304#endif
1305 p->magic = VDBE_MAGIC_INIT;
1306 return p->rc;
1307}
1308
1309/*
1310** Clean up and delete a VDBE after execution. Return an integer which is
1311** the result code. Write any error message text into *pzErrMsg.
1312*/
danielk19779e6db7d2004-06-21 08:18:51 +00001313int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001314 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001315 sqlite *db;
1316
danielk1977b5548a82004-06-26 13:51:33 +00001317 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1318 rc = sqlite3VdbeReset(p);
1319 }else if( p->magic!=VDBE_MAGIC_INIT ){
1320 /* sqlite3Error(p->db, SQLITE_MISUSE, 0); */
drh9a324642003-09-06 20:12:01 +00001321 return SQLITE_MISUSE;
1322 }
danielk19774adee202004-05-08 08:23:19 +00001323 sqlite3VdbeDelete(p);
drha1f9b5e2004-02-14 16:31:02 +00001324 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001325 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001326 }
drh9a324642003-09-06 20:12:01 +00001327 return rc;
1328}
1329
1330/*
drhf92c7ff2004-06-19 15:40:23 +00001331** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001332** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001333** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001334** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001335*/
1336void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1337 int i;
1338 for(i=0; i<pVdbeFunc->nAux; i++){
1339 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1340 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1341 if( pAux->xDelete ){
1342 pAux->xDelete(pAux->pAux);
1343 }
1344 pAux->pAux = 0;
1345 }
1346 }
1347}
1348
1349/*
drh9a324642003-09-06 20:12:01 +00001350** Delete an entire VDBE.
1351*/
danielk19774adee202004-05-08 08:23:19 +00001352void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001353 int i;
1354 if( p==0 ) return;
1355 Cleanup(p);
1356 if( p->pPrev ){
1357 p->pPrev->pNext = p->pNext;
1358 }else{
1359 assert( p->db->pVdbe==p );
1360 p->db->pVdbe = p->pNext;
1361 }
1362 if( p->pNext ){
1363 p->pNext->pPrev = p->pPrev;
1364 }
1365 p->pPrev = p->pNext = 0;
1366 if( p->nOpAlloc==0 ){
1367 p->aOp = 0;
1368 p->nOp = 0;
1369 }
1370 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001371 Op *pOp = &p->aOp[i];
1372 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1373 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001374 }
danielk1977682f68b2004-06-05 10:22:17 +00001375 if( pOp->p3type==P3_VDBEFUNC ){
1376 VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
danielk1977e159fdf2004-06-21 10:45:06 +00001377 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
danielk1977682f68b2004-06-05 10:22:17 +00001378 sqliteFree(pVdbeFunc);
1379 }
drhd3d39e92004-05-20 22:16:29 +00001380#ifndef NDEBUG
1381 sqliteFree(pOp->zComment);
1382#endif
drh9a324642003-09-06 20:12:01 +00001383 }
drh7c972de2003-09-06 22:18:07 +00001384 for(i=0; i<p->nVar; i++){
danielk1977d8123362004-06-12 09:25:12 +00001385 sqlite3VdbeMemRelease(&p->apVar[i]);
drh7c972de2003-09-06 22:18:07 +00001386 }
drh9a324642003-09-06 20:12:01 +00001387 sqliteFree(p->aOp);
1388 sqliteFree(p->aLabel);
1389 sqliteFree(p->aStack);
danielk1977b20e56b2004-06-15 13:36:30 +00001390 if( p->aColName ){
1391 for(i=0; i<(p->nResColumn)*2; i++){
1392 sqlite3VdbeMemRelease(&(p->aColName[i]));
1393 }
1394 sqliteFree(p->aColName);
1395 }
drh9a324642003-09-06 20:12:01 +00001396 p->magic = VDBE_MAGIC_DEAD;
1397 sqliteFree(p);
1398}
drha11846b2004-01-07 18:52:56 +00001399
1400/*
drha11846b2004-01-07 18:52:56 +00001401** If a MoveTo operation is pending on the given cursor, then do that
1402** MoveTo now. Return an error code. If no MoveTo is pending, this
1403** routine does nothing and returns SQLITE_OK.
1404*/
danielk19774adee202004-05-08 08:23:19 +00001405int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001406 if( p->deferredMoveto ){
1407 int res;
danielk1977132872b2004-05-10 10:37:18 +00001408 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001409 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001410 if( p->intKey ){
1411 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1412 }else{
1413 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1414 }
drhd3d39e92004-05-20 22:16:29 +00001415 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001416 p->lastRecno = keyToInt(p->movetoTarget);
1417 p->recnoIsValid = res==0;
1418 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001419 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001420 }
danielk1977132872b2004-05-10 10:37:18 +00001421 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001422 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001423 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001424 }
1425 return SQLITE_OK;
1426}
danielk19774adee202004-05-08 08:23:19 +00001427
drhab9f7f12004-05-08 10:56:11 +00001428/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001429** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001430**
danielk1977cfcdaef2004-05-12 07:33:33 +00001431** sqlite3VdbeSerialType()
1432** sqlite3VdbeSerialTypeLen()
1433** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001434** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001435** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001436**
1437** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001438** data and index records. Each serialized value consists of a
1439** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1440** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001441**
danielk1977cfcdaef2004-05-12 07:33:33 +00001442** In an SQLite index record, the serial type is stored directly before
1443** the blob of data that it corresponds to. In a table record, all serial
1444** types are stored at the start of the record, and the blobs of data at
1445** the end. Hence these functions allow the caller to handle the
1446** serial-type and data blob seperately.
1447**
1448** The following table describes the various storage classes for data:
1449**
1450** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001451** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001452** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001453** 1 1 signed integer
1454** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001455** 3 3 signed integer
1456** 4 4 signed integer
1457** 5 6 signed integer
1458** 6 8 signed integer
1459** 7 8 IEEE float
1460** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001461** N>=12 and even (N-12)/2 BLOB
1462** N>=13 and odd (N-13)/2 text
1463**
1464*/
1465
1466/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001467** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001468*/
drh25aa1b42004-05-28 01:39:01 +00001469u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001470 int flags = pMem->flags;
1471
1472 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001473 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001474 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001475 if( flags&MEM_Int ){
1476 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1477 i64 i = pMem->i;
1478 if( i>=-127 && i<=127 ) return 1;
1479 if( i>=-32767 && i<=32767 ) return 2;
drha19b7752004-05-30 21:14:58 +00001480 if( i>=-8388607 && i<=8388607 ) return 3;
1481 if( i>=-2147483647 && i<=2147483647 ) return 4;
1482 if( i>=-140737488355328L && i<=140737488355328L ) return 5;
1483 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001484 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001485 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001486 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001487 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001488 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001489 int n = pMem->n;
1490 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001491 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001492 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001493 if( flags&MEM_Blob ){
1494 return (pMem->n*2 + 12);
1495 }
1496 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001497}
1498
1499/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001500** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001501*/
drh25aa1b42004-05-28 01:39:01 +00001502int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001503 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001504 return (serial_type-12)/2;
1505 }else{
drha19b7752004-05-30 21:14:58 +00001506 static u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001507 return aSize[serial_type];
1508 }
danielk1977192ac1d2004-05-10 07:17:30 +00001509}
1510
1511/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001512** Write the serialized data blob for the value stored in pMem into
1513** buf. It is assumed that the caller has allocated sufficient space.
1514** Return the number of bytes written.
1515*/
danielk1977b1bc9532004-05-22 03:05:33 +00001516int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001517 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001518 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001519
danielk1977cfcdaef2004-05-12 07:33:33 +00001520 /* NULL */
drha19b7752004-05-30 21:14:58 +00001521 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001522 return 0;
1523 }
1524
drh1483e142004-05-21 21:12:42 +00001525 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001526 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001527 u64 v;
1528 int i;
drha19b7752004-05-30 21:14:58 +00001529 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001530 v = *(u64*)&pMem->r;
1531 }else{
1532 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001533 }
drh1483e142004-05-21 21:12:42 +00001534 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1535 while( i-- ){
1536 buf[i] = (v&0xFF);
1537 v >>= 8;
1538 }
1539 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001540 }
1541
1542 /* String or blob */
1543 assert( serial_type>=12 );
1544 len = sqlite3VdbeSerialTypeLen(serial_type);
1545 memcpy(buf, pMem->z, len);
1546 return len;
1547}
1548
1549/*
1550** Deserialize the data blob pointed to by buf as serial type serial_type
1551** and store the result in pMem. Return the number of bytes read.
1552*/
danielk1977b1bc9532004-05-22 03:05:33 +00001553int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001554 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001555 u32 serial_type, /* Serial type to deserialize */
1556 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001557){
danielk197790e4d952004-05-10 10:05:53 +00001558 int len;
1559
drha19b7752004-05-30 21:14:58 +00001560 if( serial_type==0 ){
1561 /* NULL */
1562 pMem->flags = MEM_Null;
1563 return 0;
1564 }
drh696b32f2004-05-30 01:51:52 +00001565 len = sqlite3VdbeSerialTypeLen(serial_type);
drha19b7752004-05-30 21:14:58 +00001566 if( serial_type<=7 ){
drh696b32f2004-05-30 01:51:52 +00001567 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001568 if( serial_type<=4 ){
drhe51c44f2004-05-30 20:46:09 +00001569 /* 32-bit integer type. This is handled by a special case for
1570 ** performance reasons. */
1571 int v = buf[0];
1572 int n;
1573 if( v&0x80 ){
1574 v |= -256;
1575 }
1576 for(n=1; n<len; n++){
1577 v = (v<<8) | buf[n];
1578 }
drh1483e142004-05-21 21:12:42 +00001579 pMem->flags = MEM_Int;
drhe51c44f2004-05-30 20:46:09 +00001580 pMem->i = v;
1581 return n;
1582 }else{
1583 u64 v = 0;
1584 int n;
1585
1586 if( buf[0]&0x80 ){
1587 v = -1;
1588 }
1589 for(n=0; n<len; n++){
1590 v = (v<<8) | buf[n];
1591 }
drha19b7752004-05-30 21:14:58 +00001592 if( serial_type==7 ){
drhe51c44f2004-05-30 20:46:09 +00001593 pMem->flags = MEM_Real;
1594 pMem->r = *(double*)&v;
1595 }else{
1596 pMem->flags = MEM_Int;
1597 pMem->i = *(i64*)&v;
1598 }
drh1483e142004-05-21 21:12:42 +00001599 }
drha19b7752004-05-30 21:14:58 +00001600 }else{
drh696b32f2004-05-30 01:51:52 +00001601 /* String or blob */
drha19b7752004-05-30 21:14:58 +00001602 assert( serial_type>=12 );
drh696b32f2004-05-30 01:51:52 +00001603 pMem->z = (char *)buf;
1604 pMem->n = len;
drh1b743be2004-06-22 22:04:46 +00001605 pMem->xDel = 0;
drh696b32f2004-05-30 01:51:52 +00001606 if( serial_type&0x01 ){
1607 pMem->flags = MEM_Str | MEM_Ephem;
1608 }else{
1609 pMem->flags = MEM_Blob | MEM_Ephem;
1610 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001611 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001612 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001613}
1614
1615/*
drh7a224de2004-06-02 01:22:02 +00001616** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001617** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1618** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001619** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1620** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001621*/
drh7a224de2004-06-02 01:22:02 +00001622int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001623 void *userData,
1624 int nKey1, const void *pKey1,
1625 int nKey2, const void *pKey2
1626){
drhd3d39e92004-05-20 22:16:29 +00001627 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001628 u32 d1, d2; /* Offset into aKey[] of next data element */
1629 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1630 u32 szHdr1, szHdr2; /* Number of bytes in header */
1631 int i = 0;
1632 int nField;
1633 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001634 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1635 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001636
1637 Mem mem1;
1638 Mem mem2;
1639 mem1.enc = pKeyInfo->enc;
1640 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001641
1642 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1643 d1 = szHdr1;
1644 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1645 d2 = szHdr2;
1646 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001647 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001648 u32 serial_type1;
1649 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001650
1651 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001652 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001653 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001654 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001655 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001656
1657 /* Assert that there is enough space left in each key for the blob of
1658 ** data to go with the serial type just read. This assert may fail if
1659 ** the file is corrupted. Then read the value from each key into mem1
1660 ** and mem2 respectively.
1661 */
drh25aa1b42004-05-28 01:39:01 +00001662 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1663 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001664
drhd5788202004-05-28 08:21:05 +00001665 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
danielk1977d8123362004-06-12 09:25:12 +00001666 sqlite3VdbeMemRelease(&mem1);
1667 sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001668 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001669 break;
1670 }
1671 i++;
1672 }
1673
1674 /* One of the keys ran out of fields, but all the fields up to that point
1675 ** were equal. If the incrKey flag is true, then the second key is
1676 ** treated as larger.
1677 */
1678 if( rc==0 ){
1679 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001680 rc = -1;
1681 }else if( d1<nKey1 ){
1682 rc = 1;
1683 }else if( d2<nKey2 ){
1684 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001685 }
1686 }
1687
drhd3194f52004-05-27 19:59:32 +00001688 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1689 rc = -rc;
1690 }
1691
1692 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001693}
drhd5788202004-05-28 08:21:05 +00001694
1695/*
drh7a224de2004-06-02 01:22:02 +00001696** The argument is an index entry composed using the OP_MakeRecord opcode.
1697** The last entry in this record should be an integer (specifically
1698** an integer rowid). This routine returns the number of bytes in
1699** that integer.
drhd5788202004-05-28 08:21:05 +00001700*/
1701int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1702 u32 szHdr; /* Size of the header */
1703 u32 typeRowid; /* Serial type of the rowid */
1704
1705 sqlite3GetVarint32(aKey, &szHdr);
1706 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1707 return sqlite3VdbeSerialTypeLen(typeRowid);
1708}
danielk1977eb015e02004-05-18 01:31:14 +00001709
1710
1711/*
drh7a224de2004-06-02 01:22:02 +00001712** pCur points at an index entry created using the OP_MakeRecord opcode.
1713** Read the rowid (the last field in the record) and store it in *rowid.
1714** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001715*/
1716int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drhd5788202004-05-28 08:21:05 +00001717 u64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001718 int rc;
drhd5788202004-05-28 08:21:05 +00001719 u32 szHdr; /* Size of the header */
1720 u32 typeRowid; /* Serial type of the rowid */
1721 u32 lenRowid; /* Size of the rowid */
1722 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001723
drhd5788202004-05-28 08:21:05 +00001724 sqlite3BtreeKeySize(pCur, &nCellKey);
1725 if( nCellKey<=0 ){
1726 return SQLITE_CORRUPT;
1727 }
1728 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1729 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001730 return rc;
1731 }
drhd5788202004-05-28 08:21:05 +00001732 sqlite3GetVarint32(m.z, &szHdr);
1733 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1734 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1735 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1736 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001737 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001738 return SQLITE_OK;
1739}
1740
drh7cf6e4d2004-05-19 14:56:55 +00001741/*
drhd3d39e92004-05-20 22:16:29 +00001742** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001743** the key string in pKey (of length nKey). Write into *pRes a number
1744** that is negative, zero, or positive if pC is less than, equal to,
1745** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001746**
drhd5788202004-05-28 08:21:05 +00001747** pKey is either created without a rowid or is truncated so that it
1748** omits the rowid at the end. The rowid at the end of the index entry
1749** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001750*/
danielk1977183f9f72004-05-13 05:20:26 +00001751int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001752 Cursor *pC, /* The cursor to compare against */
1753 int nKey, const u8 *pKey, /* The key to compare */
1754 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001755){
danielk1977183f9f72004-05-13 05:20:26 +00001756 u64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001757 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001758 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001759 int lenRowid;
1760 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001761
1762 sqlite3BtreeKeySize(pCur, &nCellKey);
1763 if( nCellKey<=0 ){
1764 *res = 0;
1765 return SQLITE_OK;
1766 }
drhd5788202004-05-28 08:21:05 +00001767 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1768 if( rc ){
1769 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001770 }
drhd5788202004-05-28 08:21:05 +00001771 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
drh7a224de2004-06-02 01:22:02 +00001772 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001773 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001774 return SQLITE_OK;
1775}
danielk1977b28af712004-06-21 06:50:26 +00001776
1777/*
1778** This routine sets the value to be returned by subsequent calls to
1779** sqlite3_changes() on the database handle 'db'.
1780*/
1781void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1782 db->nChange = nChange;
1783 db->nTotalChange += nChange;
1784}
1785
1786/*
1787** Set a flag in the vdbe to update the change counter when it is finalised
1788** or reset.
1789*/
1790void sqlite3VdbeCountChanges(Vdbe *p){
1791 p->changeCntOn = 1;
1792}