blob: 5e0b480be5693f036e9e94bd23787565dc2b5a86 [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*/
drh9bb575f2004-09-06 17:24:11 +000036Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
59** Add a new instruction to the list of instructions current in the
60** VDBE. Return the address of the new instruction.
61**
62** Parameters:
63**
64** p Pointer to the VDBE
65**
66** op The opcode for this instruction
67**
68** p1, p2 First two of the three possible operands.
69**
danielk19774adee202004-05-08 08:23:19 +000070** Use the sqlite3VdbeResolveLabel() function to fix an address and
71** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000072** operand.
73*/
danielk19774adee202004-05-08 08:23:19 +000074int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000075 int i;
drh701a0ae2004-02-22 20:05:00 +000076 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +000077
78 i = p->nOp;
79 p->nOp++;
80 assert( p->magic==VDBE_MAGIC_INIT );
81 if( i>=p->nOpAlloc ){
82 int oldSize = p->nOpAlloc;
83 Op *aNew;
84 p->nOpAlloc = p->nOpAlloc*2 + 100;
85 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
86 if( aNew==0 ){
87 p->nOpAlloc = oldSize;
88 return 0;
89 }
90 p->aOp = aNew;
91 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
92 }
drh701a0ae2004-02-22 20:05:00 +000093 pOp = &p->aOp[i];
94 pOp->opcode = op;
95 pOp->p1 = p1;
drh9a324642003-09-06 20:12:01 +000096 if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){
97 p2 = p->aLabel[-1-p2];
98 }
drh701a0ae2004-02-22 20:05:00 +000099 pOp->p2 = p2;
100 pOp->p3 = 0;
101 pOp->p3type = P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000102#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +0000103 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000104#endif
105 return i;
106}
107
108/*
drh701a0ae2004-02-22 20:05:00 +0000109** Add an opcode that includes the p3 value.
110*/
drheb2e1762004-05-27 01:53:56 +0000111int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000112 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
113 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000114 return addr;
115}
116
117/*
drh9a324642003-09-06 20:12:01 +0000118** Create a new symbolic label for an instruction that has yet to be
119** coded. The symbolic label is really just a negative number. The
120** label can be used as the P2 value of an operation. Later, when
121** the label is resolved to a specific address, the VDBE will scan
122** through its operation list and change all values of P2 which match
123** the label into the resolved address.
124**
125** The VDBE knows that a P2 value is a label because labels are
126** always negative and P2 values are suppose to be non-negative.
127** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000128**
129** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000130*/
danielk19774adee202004-05-08 08:23:19 +0000131int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000132 int i;
133 i = p->nLabel++;
134 assert( p->magic==VDBE_MAGIC_INIT );
135 if( i>=p->nLabelAlloc ){
136 int *aNew;
137 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
138 aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
139 if( aNew==0 ){
140 sqliteFree(p->aLabel);
141 }
142 p->aLabel = aNew;
143 }
144 if( p->aLabel==0 ){
145 p->nLabel = 0;
146 p->nLabelAlloc = 0;
147 return 0;
148 }
149 p->aLabel[i] = -1;
150 return -1-i;
151}
152
153/*
154** Resolve label "x" to be the address of the next instruction to
155** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000156** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000157*/
danielk19774adee202004-05-08 08:23:19 +0000158void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh9a324642003-09-06 20:12:01 +0000159 int j;
160 assert( p->magic==VDBE_MAGIC_INIT );
161 if( x<0 && (-x)<=p->nLabel && p->aOp ){
162 if( p->aLabel[-1-x]==p->nOp ) return;
163 assert( p->aLabel[-1-x]<0 );
164 p->aLabel[-1-x] = p->nOp;
165 for(j=0; j<p->nOp; j++){
166 if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp;
167 }
168 }
169}
170
171/*
172** Return the address of the next instruction to be inserted.
173*/
danielk19774adee202004-05-08 08:23:19 +0000174int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000175 assert( p->magic==VDBE_MAGIC_INIT );
176 return p->nOp;
177}
178
179/*
180** Add a whole list of operations to the operation stack. Return the
181** address of the first operation added.
182*/
danielk19774adee202004-05-08 08:23:19 +0000183int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000184 int addr;
185 assert( p->magic==VDBE_MAGIC_INIT );
186 if( p->nOp + nOp >= p->nOpAlloc ){
187 int oldSize = p->nOpAlloc;
188 Op *aNew;
189 p->nOpAlloc = p->nOpAlloc*2 + nOp + 10;
190 aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
191 if( aNew==0 ){
192 p->nOpAlloc = oldSize;
193 return 0;
194 }
195 p->aOp = aNew;
196 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
197 }
198 addr = p->nOp;
199 if( nOp>0 ){
200 int i;
drh905793e2004-02-21 13:31:09 +0000201 VdbeOpList const *pIn = aOp;
202 for(i=0; i<nOp; i++, pIn++){
203 int p2 = pIn->p2;
204 VdbeOp *pOut = &p->aOp[i+addr];
205 pOut->opcode = pIn->opcode;
206 pOut->p1 = pIn->p1;
207 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
208 pOut->p3 = pIn->p3;
209 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
drh9a324642003-09-06 20:12:01 +0000210#ifndef NDEBUG
danielk1977132872b2004-05-10 10:37:18 +0000211 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000212 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000213 }
214#endif
215 }
216 p->nOp += nOp;
217 }
218 return addr;
219}
220
221/*
222** Change the value of the P1 operand for a specific instruction.
223** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000224** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000225** few minor changes to the program.
226*/
danielk19774adee202004-05-08 08:23:19 +0000227void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000228 assert( p->magic==VDBE_MAGIC_INIT );
229 if( p && addr>=0 && p->nOp>addr && p->aOp ){
230 p->aOp[addr].p1 = val;
231 }
232}
233
234/*
235** Change the value of the P2 operand for a specific instruction.
236** This routine is useful for setting a jump destination.
237*/
danielk19774adee202004-05-08 08:23:19 +0000238void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000239 assert( val>=0 );
240 assert( p->magic==VDBE_MAGIC_INIT );
241 if( p && addr>=0 && p->nOp>addr && p->aOp ){
242 p->aOp[addr].p2 = val;
243 }
244}
245
246/*
247** Change the value of the P3 operand for a specific instruction.
248** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000249** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000250** few minor changes to the program.
251**
252** If n>=0 then the P3 operand is dynamic, meaning that a copy of
253** the string is made into memory obtained from sqliteMalloc().
254** A value of n==0 means copy bytes of zP3 up to and including the
255** first null byte. If n>0 then copy n+1 bytes of zP3.
256**
257** If n==P3_STATIC it means that zP3 is a pointer to a constant static
258** string and we can just copy the pointer. n==P3_POINTER means zP3 is
drhd3d39e92004-05-20 22:16:29 +0000259** a pointer to some object other than a string. n==P3_COLLSEQ and
260** n==P3_KEYINFO mean that zP3 is a pointer to a CollSeq or KeyInfo
261** structure. A copy is made of KeyInfo structures into memory obtained
262** from sqliteMalloc.
drh9a324642003-09-06 20:12:01 +0000263**
264** If addr<0 then change P3 on the most recently inserted instruction.
265*/
danielk19774adee202004-05-08 08:23:19 +0000266void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000267 Op *pOp;
268 assert( p->magic==VDBE_MAGIC_INIT );
269 if( p==0 || p->aOp==0 ) return;
270 if( addr<0 || addr>=p->nOp ){
271 addr = p->nOp - 1;
272 if( addr<0 ) return;
273 }
274 pOp = &p->aOp[addr];
275 if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
276 sqliteFree(pOp->p3);
277 pOp->p3 = 0;
278 }
279 if( zP3==0 ){
280 pOp->p3 = 0;
281 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000282 }else if( n==P3_KEYINFO ){
283 KeyInfo *pKeyInfo;
284 int nField, nByte;
285 nField = ((KeyInfo*)zP3)->nField;
286 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]);
drheafe05b2004-06-13 00:54:01 +0000287 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000288 pOp->p3 = (char*)pKeyInfo;
289 if( pKeyInfo ){
290 memcpy(pKeyInfo, zP3, nByte);
291 pOp->p3type = P3_KEYINFO;
292 }else{
293 pOp->p3type = P3_NOTUSED;
294 }
drhffbc3082004-05-21 01:29:06 +0000295 }else if( n==P3_KEYINFO_HANDOFF ){
296 pOp->p3 = (char*)zP3;
297 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000298 }else if( n<0 ){
299 pOp->p3 = (char*)zP3;
300 pOp->p3type = n;
301 }else{
danielk19774adee202004-05-08 08:23:19 +0000302 sqlite3SetNString(&pOp->p3, zP3, n, 0);
drh9a324642003-09-06 20:12:01 +0000303 pOp->p3type = P3_DYNAMIC;
304 }
305}
306
307/*
308** If the P3 operand to the specified instruction appears
309** to be a quoted string token, then this procedure removes
310** the quotes.
311**
312** The quoting operator can be either a grave ascent (ASCII 0x27)
313** or a double quote character (ASCII 0x22). Two quotes in a row
314** resolve to be a single actual quote character within the string.
315*/
danielk19774adee202004-05-08 08:23:19 +0000316void sqlite3VdbeDequoteP3(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000317 Op *pOp;
318 assert( p->magic==VDBE_MAGIC_INIT );
drh51e9a442004-01-16 16:42:53 +0000319 if( p->aOp==0 ) return;
320 if( addr<0 || addr>=p->nOp ){
321 addr = p->nOp - 1;
322 if( addr<0 ) return;
323 }
drh9a324642003-09-06 20:12:01 +0000324 pOp = &p->aOp[addr];
325 if( pOp->p3==0 || pOp->p3[0]==0 ) return;
drhd3d39e92004-05-20 22:16:29 +0000326 if( pOp->p3type==P3_STATIC ){
drh9a324642003-09-06 20:12:01 +0000327 pOp->p3 = sqliteStrDup(pOp->p3);
328 pOp->p3type = P3_DYNAMIC;
329 }
drhd3d39e92004-05-20 22:16:29 +0000330 assert( pOp->p3type==P3_DYNAMIC );
danielk19774adee202004-05-08 08:23:19 +0000331 sqlite3Dequote(pOp->p3);
drh9a324642003-09-06 20:12:01 +0000332}
333
334/*
danielk197784ac9d02004-05-18 09:58:06 +0000335** Search the current program starting at instruction addr for the given
336** opcode and P2 value. Return the address plus 1 if found and 0 if not
337** found.
drh9a324642003-09-06 20:12:01 +0000338*/
danielk197784ac9d02004-05-18 09:58:06 +0000339int sqlite3VdbeFindOp(Vdbe *p, int addr, int op, int p2){
drh9a324642003-09-06 20:12:01 +0000340 int i;
341 assert( p->magic==VDBE_MAGIC_INIT );
danielk197784ac9d02004-05-18 09:58:06 +0000342 for(i=addr; i<p->nOp; i++){
drh9a324642003-09-06 20:12:01 +0000343 if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1;
344 }
345 return 0;
346}
347
348/*
349** Return the opcode for a given address.
350*/
danielk19774adee202004-05-08 08:23:19 +0000351VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000352 assert( p->magic==VDBE_MAGIC_INIT );
353 assert( addr>=0 && addr<p->nOp );
354 return &p->aOp[addr];
355}
356
357/*
drhd3d39e92004-05-20 22:16:29 +0000358** Compute a string that describes the P3 parameter for an opcode.
359** Use zTemp for any required temporary buffer space.
360*/
361static char *displayP3(Op *pOp, char *zTemp, int nTemp){
362 char *zP3;
363 assert( nTemp>=20 );
364 switch( pOp->p3type ){
365 case P3_POINTER: {
366 sprintf(zTemp, "ptr(%#x)", (int)pOp->p3);
367 zP3 = zTemp;
368 break;
369 }
370 case P3_KEYINFO: {
371 int i, j;
372 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
373 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
374 i = strlen(zTemp);
375 for(j=0; j<pKeyInfo->nField; j++){
376 CollSeq *pColl = pKeyInfo->aColl[j];
377 if( pColl ){
378 int n = strlen(pColl->zName);
379 if( i+n>nTemp-6 ){
380 strcpy(&zTemp[i],",...");
381 break;
382 }
383 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000384 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000385 zTemp[i++] = '-';
386 }
387 strcpy(&zTemp[i], pColl->zName);
388 i += n;
389 }else if( i+4<nTemp-6 ){
390 strcpy(&zTemp[i],",nil");
391 i += 4;
392 }
393 }
394 zTemp[i++] = ')';
395 zTemp[i] = 0;
396 assert( i<nTemp );
397 zP3 = zTemp;
398 break;
399 }
400 case P3_COLLSEQ: {
401 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000402 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000403 zP3 = zTemp;
404 break;
405 }
drhf9b596e2004-05-26 16:54:42 +0000406 case P3_FUNCDEF: {
407 FuncDef *pDef = (FuncDef*)pOp->p3;
408 char zNum[30];
409 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
410 sprintf(zNum,"(%d)", pDef->nArg);
411 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
412 strcat(zTemp, zNum);
413 }
414 zP3 = zTemp;
415 break;
416 }
drhd3d39e92004-05-20 22:16:29 +0000417 default: {
418 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000419 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000420 zP3 = "";
421 }
422 }
423 }
424 return zP3;
425}
426
427
drh9a324642003-09-06 20:12:01 +0000428#if !defined(NDEBUG) || defined(VDBE_PROFILE)
429/*
430** Print a single opcode. This routine is used for debugging only.
431*/
danielk19774adee202004-05-08 08:23:19 +0000432void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000433 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000434 char zPtr[50];
435 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000436 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000437 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000438 fprintf(pOut, zFormat1,
439 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000440 fflush(pOut);
441}
442#endif
443
444/*
445** Give a listing of the program in the virtual machine.
446**
danielk19774adee202004-05-08 08:23:19 +0000447** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000448** running the code, it invokes the callback once for each instruction.
449** This feature is used to implement "EXPLAIN".
450*/
danielk19774adee202004-05-08 08:23:19 +0000451int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000452 Vdbe *p /* The VDBE */
453){
drh9bb575f2004-09-06 17:24:11 +0000454 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000455 int i;
drh826fb5a2004-02-14 23:59:57 +0000456 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000457
drh9a324642003-09-06 20:12:01 +0000458 assert( p->explain );
danielk197718f41892004-05-22 07:27:46 +0000459
460 /* Even though this opcode does not put dynamic strings onto the
461 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000462 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000463 */
464 if( p->pTos==&p->aStack[4] ){
465 for(i=0; i<5; i++){
danielk1977d8123362004-06-12 09:25:12 +0000466 sqlite3VdbeMemRelease(&p->aStack[i]);
danielk197718f41892004-05-22 07:27:46 +0000467 p->aStack[i].flags = 0;
468 }
469 }
danielk197718f41892004-05-22 07:27:46 +0000470 p->resOnStack = 0;
471
472 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000473 if( i>=p->nOp ){
474 p->rc = SQLITE_OK;
475 rc = SQLITE_DONE;
476 }else if( db->flags & SQLITE_Interrupt ){
477 db->flags &= ~SQLITE_Interrupt;
478 if( db->magic!=SQLITE_MAGIC_BUSY ){
479 p->rc = SQLITE_MISUSE;
480 }else{
481 p->rc = SQLITE_INTERRUPT;
drh9a324642003-09-06 20:12:01 +0000482 }
drh826fb5a2004-02-14 23:59:57 +0000483 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000484 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000485 }else{
drhd3d39e92004-05-20 22:16:29 +0000486 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000487 Mem *pMem = p->aStack;
488 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000489 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000490 pMem->i = i; /* Program counter */
491 pMem++;
492
493 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
494 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
495 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000496 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000497 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000498 pMem++;
499
500 pMem->flags = MEM_Int;
501 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000502 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000503 pMem++;
504
505 pMem->flags = MEM_Int;
506 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000507 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000508 pMem++;
509
510 pMem->flags = MEM_Short|MEM_Str|MEM_Term; /* P3 */
511 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drh9c054832004-05-31 18:51:57 +0000512 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000513 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000514
drh826fb5a2004-02-14 23:59:57 +0000515 p->nResColumn = 5;
drheb2e1762004-05-27 01:53:56 +0000516 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000517 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000518 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000519 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000520 }
drh826fb5a2004-02-14 23:59:57 +0000521 return rc;
drh9a324642003-09-06 20:12:01 +0000522}
523
524/*
drh3f7d4e42004-07-24 14:35:58 +0000525** Print the SQL that was used to generate a VDBE program.
526*/
527void sqlite3VdbePrintSql(Vdbe *p){
528#ifdef SQLITE_DEBUG
529 int nOp = p->nOp;
530 VdbeOp *pOp;
531 if( nOp<2 ) return;
532 pOp = &p->aOp[nOp-2];
533 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
534 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000535 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000536 printf("SQL: [%s]\n", z);
537 }
538#endif
539}
540
541/*
drh9a324642003-09-06 20:12:01 +0000542** Prepare a virtual machine for execution. This involves things such
543** as allocating stack space and initializing the program counter.
544** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000545** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000546**
547** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
548** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000549*/
danielk19774adee202004-05-08 08:23:19 +0000550void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000551 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000552 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000553 int nMem, /* Number of memory cells to allocate */
554 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000555 int isExplain /* True if the EXPLAIN keywords is present */
556){
557 int n;
558
559 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000560 assert( p->magic==VDBE_MAGIC_INIT );
561
562 /* Add a HALT instruction to the very end of the program.
563 */
564 if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){
danielk19774adee202004-05-08 08:23:19 +0000565 sqlite3VdbeAddOp(p, OP_Halt, 0, 0);
drh9a324642003-09-06 20:12:01 +0000566 }
567
568 /* No instruction ever pushes more than a single element onto the
569 ** stack. And the stack never grows on successive executions of the
570 ** same loop. So the total number of instructions is an upper bound
571 ** on the maximum stack depth required.
572 **
573 ** Allocation all the stack space we will ever need.
574 */
drh82a48512003-09-06 22:45:20 +0000575 if( p->aStack==0 ){
drh82a48512003-09-06 22:45:20 +0000576 assert( nVar>=0 );
577 n = isExplain ? 10 : p->nOp;
578 p->aStack = sqliteMalloc(
danielk1977b20e56b2004-06-15 13:36:30 +0000579 n*(sizeof(p->aStack[0])+sizeof(Mem*)) /* aStack, apArg */
drh290c1942004-08-21 17:54:45 +0000580 + nVar*sizeof(Mem) /* aVar */
581 + nVar*sizeof(char*) /* azVar */
582 + nMem*sizeof(Mem) /* aMem */
583 + nCursor*sizeof(Cursor*) /* apCsr */
drh82a48512003-09-06 22:45:20 +0000584 );
drh290c1942004-08-21 17:54:45 +0000585 if( !sqlite3_malloc_failed ){
586 p->apArg = (Mem **)&p->aStack[n];
587 p->aVar = (Mem *)&p->apArg[n];
588 p->azVar = (char**)&p->aVar[nVar];
589 p->okVar = 0;
590 p->nVar = nVar;
591 p->aMem = (Mem*)&p->azVar[nVar];
592 p->nMem = nMem;
593 p->apCsr = (Cursor**)&p->aMem[nMem];
594 p->nCursor = nCursor;
595 for(n=0; n<nVar; n++){
596 p->aVar[n].flags = MEM_Null;
597 }
598 for(n=0; n<nMem; n++){
599 p->aMem[n].flags = MEM_Null;
600 }
danielk197754db47e2004-05-19 10:36:43 +0000601 }
drh82a48512003-09-06 22:45:20 +0000602 }
drh9a324642003-09-06 20:12:01 +0000603
drhfaa57ac2004-06-09 14:01:51 +0000604#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000605 if( (p->db->flags & SQLITE_VdbeListing)!=0
606 || sqlite3OsFileExists("vdbe_explain")
607 ){
drh80242052004-06-09 00:48:12 +0000608 int i;
609 printf("VDBE Program Listing:\n");
drh3f7d4e42004-07-24 14:35:58 +0000610 sqlite3VdbePrintSql(p);
drh80242052004-06-09 00:48:12 +0000611 for(i=0; i<p->nOp; i++){
612 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
613 }
614 }
danielk19774adee202004-05-08 08:23:19 +0000615 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000616 p->trace = stdout;
617 }
618#endif
drh6810ce62004-01-31 19:22:56 +0000619 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000620 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000621 p->rc = SQLITE_OK;
622 p->uniqueCnt = 0;
623 p->returnDepth = 0;
624 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000625 p->popStack = 0;
626 p->explain |= isExplain;
627 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000628 p->nChange = 0;
drh9a324642003-09-06 20:12:01 +0000629#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000630 {
631 int i;
632 for(i=0; i<p->nOp; i++){
633 p->aOp[i].cnt = 0;
634 p->aOp[i].cycles = 0;
635 }
drh9a324642003-09-06 20:12:01 +0000636 }
637#endif
638}
639
640
641/*
642** Remove any elements that remain on the sorter for the VDBE given.
643*/
danielk19774adee202004-05-08 08:23:19 +0000644void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000645 while( p->pSort ){
646 Sorter *pSorter = p->pSort;
647 p->pSort = pSorter->pNext;
648 sqliteFree(pSorter->zKey);
danielk1977369f27e2004-06-15 11:40:04 +0000649 sqlite3VdbeMemRelease(&pSorter->data);
drh9a324642003-09-06 20:12:01 +0000650 sqliteFree(pSorter);
651 }
652}
653
654/*
danielk1977e159fdf2004-06-21 10:45:06 +0000655** Free all resources allociated with AggElem pElem, an element of
656** aggregate pAgg.
657*/
danielk1977e3026632004-06-22 11:29:02 +0000658void freeAggElem(AggElem *pElem, Agg *pAgg){
danielk1977e159fdf2004-06-21 10:45:06 +0000659 int i;
660 for(i=0; i<pAgg->nMem; i++){
661 Mem *pMem = &pElem->aMem[i];
drh645f63e2004-06-22 13:22:40 +0000662 if( pAgg->apFunc && pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk1977e159fdf2004-06-21 10:45:06 +0000663 sqlite3_context ctx;
664 ctx.pFunc = pAgg->apFunc[i];
665 ctx.s.flags = MEM_Null;
666 ctx.pAgg = pMem->z;
667 ctx.cnt = pMem->i;
668 ctx.isStep = 0;
669 ctx.isError = 0;
670 (*pAgg->apFunc[i]->xFinalize)(&ctx);
671 pMem->z = ctx.pAgg;
672 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
673 sqliteFree(pMem->z);
674 }
675 sqlite3VdbeMemRelease(&ctx.s);
676 }else{
677 sqlite3VdbeMemRelease(pMem);
678 }
679 }
680 sqliteFree(pElem);
681}
682
683/*
danielk1977ce2663c2004-06-11 13:19:21 +0000684** Reset an Agg structure. Delete all its contents.
685**
686** For installable aggregate functions, if the step function has been
687** called, make sure the finalizer function has also been called. The
688** finalizer might need to free memory that was allocated as part of its
689** private context. If the finalizer has not been called yet, call it
690** now.
691**
692** If db is NULL, then this is being called from sqliteVdbeReset(). In
693** this case clean up all references to the temp-table used for
694** aggregates (if it was ever opened).
695**
696** If db is not NULL, then this is being called from with an OP_AggReset
697** opcode. Open the temp-table, if it has not already been opened and
698** delete the contents of the table used for aggregate information, ready
699** for the next round of aggregate processing.
700*/
drh9bb575f2004-09-06 17:24:11 +0000701int sqlite3VdbeAggReset(sqlite3 *db, Agg *pAgg, KeyInfo *pKeyInfo){
danielk1977ce2663c2004-06-11 13:19:21 +0000702 int rc = 0;
703 BtCursor *pCsr = pAgg->pCsr;
704
705 assert( (pCsr && pAgg->nTab>0) || (!pCsr && pAgg->nTab==0)
706 || sqlite3_malloc_failed );
707
708 /* If pCsr is not NULL, then the table used for aggregate information
709 ** is open. Loop through it and free the AggElem* structure pointed at
710 ** by each entry. If the finalizer has not been called for an AggElem,
711 ** do that too. Finally, clear the btree table itself.
712 */
713 if( pCsr ){
714 int res;
715 assert( pAgg->pBtree );
716 assert( pAgg->nTab>0 );
717
718 rc=sqlite3BtreeFirst(pCsr, &res);
719 while( res==0 && rc==SQLITE_OK ){
720 AggElem *pElem;
721 rc = sqlite3BtreeData(pCsr, 0, sizeof(AggElem*), (char *)&pElem);
722 if( res!=SQLITE_OK ){
723 return rc;
724 }
725 assert( pAgg->apFunc!=0 );
danielk1977e159fdf2004-06-21 10:45:06 +0000726 freeAggElem(pElem, pAgg);
danielk1977ce2663c2004-06-11 13:19:21 +0000727 rc=sqlite3BtreeNext(pCsr, &res);
728 }
729 if( rc!=SQLITE_OK ){
730 return rc;
731 }
732
733 sqlite3BtreeCloseCursor(pCsr);
734 sqlite3BtreeClearTable(pAgg->pBtree, pAgg->nTab);
danielk1977e159fdf2004-06-21 10:45:06 +0000735 }else{
736 /* The cursor may not be open because the aggregator was never used,
737 ** or it could be that it was used but there was no GROUP BY clause.
738 */
739 if( pAgg->pCurrent ){
740 freeAggElem(pAgg->pCurrent, pAgg);
741 }
danielk1977ce2663c2004-06-11 13:19:21 +0000742 }
743
744 /* If db is not NULL and we have not yet and we have not yet opened
745 ** the temporary btree then do so and create the table to store aggregate
746 ** information.
747 **
748 ** If db is NULL, then close the temporary btree if it is open.
749 */
750 if( db ){
751 if( !pAgg->pBtree ){
752 assert( pAgg->nTab==0 );
drhe1632b22004-06-12 20:42:29 +0000753 rc = sqlite3BtreeFactory(db, ":memory:", 0, TEMP_PAGES, &pAgg->pBtree);
danielk1977ce2663c2004-06-11 13:19:21 +0000754 if( rc!=SQLITE_OK ) return rc;
danielk197740b38dc2004-06-26 08:38:24 +0000755 sqlite3BtreeBeginTrans(pAgg->pBtree, 1);
danielk1977ce2663c2004-06-11 13:19:21 +0000756 rc = sqlite3BtreeCreateTable(pAgg->pBtree, &pAgg->nTab, 0);
757 if( rc!=SQLITE_OK ) return rc;
758 }
759 assert( pAgg->nTab!=0 );
760
761 rc = sqlite3BtreeCursor(pAgg->pBtree, pAgg->nTab, 1,
762 sqlite3VdbeRecordCompare, pKeyInfo, &pAgg->pCsr);
763 if( rc!=SQLITE_OK ) return rc;
764 }else{
765 if( pAgg->pBtree ){
766 sqlite3BtreeClose(pAgg->pBtree);
767 pAgg->pBtree = 0;
768 pAgg->nTab = 0;
769 }
770 pAgg->pCsr = 0;
771 }
772
773 if( pAgg->apFunc ){
774 sqliteFree(pAgg->apFunc);
775 pAgg->apFunc = 0;
776 }
777 pAgg->pCurrent = 0;
778 pAgg->nMem = 0;
779 pAgg->searching = 0;
780 return SQLITE_OK;
781}
782
drh9a324642003-09-06 20:12:01 +0000783
784/*
785** Delete a keylist
786*/
danielk19774adee202004-05-08 08:23:19 +0000787void sqlite3VdbeKeylistFree(Keylist *p){
drh9a324642003-09-06 20:12:01 +0000788 while( p ){
789 Keylist *pNext = p->pNext;
790 sqliteFree(p);
791 p = pNext;
792 }
793}
794
795/*
796** Close a cursor and release all the resources that cursor happens
797** to hold.
798*/
drh4774b132004-06-12 20:12:51 +0000799void sqlite3VdbeFreeCursor(Cursor *pCx){
800 if( pCx==0 ){
801 return;
802 }
drh9a324642003-09-06 20:12:01 +0000803 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000804 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000805 }
806 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000808 }
809 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000810 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000811 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000812}
813
814/*
815** Close all cursors
816*/
817static void closeAllCursors(Vdbe *p){
818 int i;
drh290c1942004-08-21 17:54:45 +0000819 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000820 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000821 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh290c1942004-08-21 17:54:45 +0000822 p->apCsr[i] = 0;
drh9a324642003-09-06 20:12:01 +0000823 }
drh9a324642003-09-06 20:12:01 +0000824}
825
826/*
drh9a324642003-09-06 20:12:01 +0000827** Clean up the VM after execution.
828**
829** This routine will automatically close any cursors, lists, and/or
830** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000831** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000832*/
833static void Cleanup(Vdbe *p){
834 int i;
drh6810ce62004-01-31 19:22:56 +0000835 if( p->aStack ){
836 Mem *pTos = p->pTos;
837 while( pTos>=p->aStack ){
danielk1977d8123362004-06-12 09:25:12 +0000838 sqlite3VdbeMemRelease(pTos);
drh6810ce62004-01-31 19:22:56 +0000839 pTos--;
840 }
841 p->pTos = pTos;
842 }
drh9a324642003-09-06 20:12:01 +0000843 closeAllCursors(p);
drh290c1942004-08-21 17:54:45 +0000844 for(i=0; i<p->nMem; i++){
845 sqlite3VdbeMemRelease(&p->aMem[i]);
drh9a324642003-09-06 20:12:01 +0000846 }
drh9a324642003-09-06 20:12:01 +0000847 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +0000848 sqlite3VdbeKeylistFree(p->pList);
drh9a324642003-09-06 20:12:01 +0000849 p->pList = 0;
850 }
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3VdbeSorterReset(p);
danielk1977ce2663c2004-06-11 13:19:21 +0000852 sqlite3VdbeAggReset(0, &p->agg, 0);
drh9a324642003-09-06 20:12:01 +0000853 if( p->keylistStack ){
854 int ii;
855 for(ii = 0; ii < p->keylistStackDepth; ii++){
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3VdbeKeylistFree(p->keylistStack[ii]);
drh9a324642003-09-06 20:12:01 +0000857 }
858 sqliteFree(p->keylistStack);
859 p->keylistStackDepth = 0;
860 p->keylistStack = 0;
861 }
drh5f968432004-02-21 19:02:30 +0000862 sqliteFree(p->contextStack);
863 p->contextStack = 0;
drh9a324642003-09-06 20:12:01 +0000864 sqliteFree(p->zErrMsg);
865 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000866}
867
868/*
danielk197722322fd2004-05-25 23:35:17 +0000869** Set the number of result columns that will be returned by this SQL
870** statement. This is now set at compile time, rather than during
871** execution of the vdbe program so that sqlite3_column_count() can
872** be called on an SQL statement before sqlite3_step().
873*/
874void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
danielk19773cf86062004-05-26 10:11:05 +0000875 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000876 p->nResColumn = nResColumn;
877}
878
879/*
danielk19773cf86062004-05-26 10:11:05 +0000880** Set the name of the idx'th column to be returned by the SQL statement.
881** zName must be a pointer to a nul terminated string.
882**
883** This call must be made after a call to sqlite3VdbeSetNumCols().
884**
danielk1977d8123362004-06-12 09:25:12 +0000885** If N==P3_STATIC it means that zName is a pointer to a constant static
886** string and we can just copy the pointer. If it is P3_DYNAMIC, then
887** the string is freed using sqliteFree() when the vdbe is finished with
888** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000889*/
890int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
891 int rc;
892 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +0000893 assert( idx<(2*p->nResColumn) );
danielk19773cf86062004-05-26 10:11:05 +0000894
895 /* If the Vdbe.aColName array has not yet been allocated, allocate
896 ** it now.
897 */
898 if( !p->aColName ){
899 int i;
danielk197776d505b2004-05-28 13:13:02 +0000900 p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn*2);
danielk19773cf86062004-05-26 10:11:05 +0000901 if( !p->aColName ){
902 return SQLITE_NOMEM;
903 }
danielk197776d505b2004-05-28 13:13:02 +0000904 for(i=0; i<(2*p->nResColumn); i++){
danielk19773cf86062004-05-26 10:11:05 +0000905 p->aColName[i].flags = MEM_Null;
906 }
907 }
908
909 pColName = &(p->aColName[idx]);
danielk1977d8123362004-06-12 09:25:12 +0000910 if( N==P3_DYNAMIC || N==P3_STATIC ){
911 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000912 }else{
danielk1977d8123362004-06-12 09:25:12 +0000913 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000914 }
915 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
916 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000917 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000918 }
919 return rc;
920}
921
danielk197713adf8a2004-06-03 16:08:41 +0000922/*
923** A read or write transaction may or may not be active on database handle
924** db. If a transaction is active, commit it. If there is a
925** write-transaction spanning more than one database file, this routine
926** takes care of the master journal trickery.
927*/
drh9bb575f2004-09-06 17:24:11 +0000928static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +0000929 int i;
930 int nTrans = 0; /* Number of databases with an active write-transaction */
931 int rc = SQLITE_OK;
932 int needXcommit = 0;
933
934 for(i=0; i<db->nDb; i++){
935 Btree *pBt = db->aDb[i].pBt;
936 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
937 needXcommit = 1;
938 if( i!=1 ) nTrans++;
939 }
940 }
941
942 /* If there are any write-transactions at all, invoke the commit hook */
943 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +0000944 int rc;
945 sqlite3SafetyOff(db);
946 rc = db->xCommitCallback(db->pCommitArg);
947 sqlite3SafetyOn(db);
948 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +0000949 return SQLITE_CONSTRAINT;
950 }
951 }
952
danielk197740b38dc2004-06-26 08:38:24 +0000953 /* The simple case - no more than one database file (not counting the
954 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000955 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000956 **
danielk197740b38dc2004-06-26 08:38:24 +0000957 ** If the return value of sqlite3BtreeGetFilename() is a zero length
958 ** string, it means the main database is :memory:. In that case we do
959 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000960 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000961 */
danielk197740b38dc2004-06-26 08:38:24 +0000962 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +0000963 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +0000964 Btree *pBt = db->aDb[i].pBt;
965 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +0000966 rc = sqlite3BtreeSync(pBt, 0);
967 }
968 }
969
970 /* Do the commit only if all databases successfully synced */
971 if( rc==SQLITE_OK ){
972 for(i=0; i<db->nDb; i++){
973 Btree *pBt = db->aDb[i].pBt;
974 if( pBt ){
975 sqlite3BtreeCommit(pBt);
976 }
danielk197713adf8a2004-06-03 16:08:41 +0000977 }
978 }
979 }
980
981 /* The complex case - There is a multi-file write-transaction active.
982 ** This requires a master journal file to ensure the transaction is
983 ** committed atomicly.
984 */
985 else{
986 char *zMaster = 0; /* File-name for the master journal */
987 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
988 OsFile master;
989
990 /* Select a master journal file name */
991 do {
drha6abd042004-06-09 17:37:22 +0000992 u32 random;
993 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +0000994 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +0000995 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +0000996 if( !zMaster ){
997 return SQLITE_NOMEM;
998 }
999 }while( sqlite3OsFileExists(zMaster) );
1000
1001 /* Open the master journal. */
drhda71ce12004-06-21 18:14:45 +00001002 memset(&master, 0, sizeof(master));
danielk197713adf8a2004-06-03 16:08:41 +00001003 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
1004 if( rc!=SQLITE_OK ){
1005 sqliteFree(zMaster);
1006 return rc;
1007 }
1008
1009 /* Write the name of each database file in the transaction into the new
1010 ** master journal file. If an error occurs at this point close
1011 ** and delete the master journal file. All the individual journal files
1012 ** still have 'null' as the master journal pointer, so they will roll
1013 ** back independantly if a failure occurs.
1014 */
1015 for(i=0; i<db->nDb; i++){
1016 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001017 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001018 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001019 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001020 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1021 rc = sqlite3OsWrite(&master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001022 if( rc!=SQLITE_OK ){
1023 sqlite3OsClose(&master);
1024 sqlite3OsDelete(zMaster);
1025 sqliteFree(zMaster);
1026 return rc;
1027 }
1028 }
1029 }
1030
danielk197713adf8a2004-06-03 16:08:41 +00001031
danielk19775865e3d2004-06-14 06:03:57 +00001032 /* Sync the master journal file. Before doing this, open the directory
1033 ** the master journal file is store in so that it gets synced too.
1034 */
1035 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1036 rc = sqlite3OsOpenDirectory(zMainFile, &master);
1037 if( rc!=SQLITE_OK ){
1038 sqlite3OsClose(&master);
1039 sqlite3OsDelete(zMaster);
1040 sqliteFree(zMaster);
1041 return rc;
1042 }
1043 rc = sqlite3OsSync(&master);
1044 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001045 sqlite3OsClose(&master);
danielk19775865e3d2004-06-14 06:03:57 +00001046 sqliteFree(zMaster);
1047 return rc;
1048 }
drhc9e06862004-06-09 20:03:08 +00001049
danielk197713adf8a2004-06-03 16:08:41 +00001050 /* Sync all the db files involved in the transaction. The same call
1051 ** sets the master journal pointer in each individual journal. If
1052 ** an error occurs here, do not delete the master journal file.
1053 **
1054 ** If the error occurs during the first call to sqlite3BtreeSync(),
1055 ** then there is a chance that the master journal file will be
1056 ** orphaned. But we cannot delete it, in case the master journal
1057 ** file name was written into the journal file before the failure
1058 ** occured.
1059 */
1060 for(i=0; i<db->nDb; i++){
1061 Btree *pBt = db->aDb[i].pBt;
1062 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1063 rc = sqlite3BtreeSync(pBt, zMaster);
1064 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001065 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001066 sqliteFree(zMaster);
1067 return rc;
1068 }
1069 }
1070 }
danielk1977962398d2004-06-14 09:35:16 +00001071 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001072
danielk1977962398d2004-06-14 09:35:16 +00001073 /* Delete the master journal file. This commits the transaction. After
1074 ** doing this the directory is synced again before any individual
1075 ** transaction files are deleted.
1076 */
danielk197713adf8a2004-06-03 16:08:41 +00001077 rc = sqlite3OsDelete(zMaster);
1078 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001079 sqliteFree(zMaster);
1080 zMaster = 0;
danielk1977962398d2004-06-14 09:35:16 +00001081 rc = sqlite3OsSyncDirectory(zMainFile);
1082 if( rc!=SQLITE_OK ){
1083 /* This is not good. The master journal file has been deleted, but
1084 ** the directory sync failed. There is no completely safe course of
1085 ** action from here. The individual journals contain the name of the
1086 ** master journal file, but there is no way of knowing if that
1087 ** master journal exists now or if it will exist after the operating
1088 ** system crash that may follow the fsync() failure.
1089 */
1090 assert(0);
1091 sqliteFree(zMaster);
1092 return rc;
1093 }
danielk197713adf8a2004-06-03 16:08:41 +00001094
1095 /* All files and directories have already been synced, so the following
1096 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1097 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001098 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001099 ** but some stray 'cold' journals may be lying around. Returning an
1100 ** error code won't help matters.
1101 */
1102 for(i=0; i<db->nDb; i++){
1103 Btree *pBt = db->aDb[i].pBt;
1104 if( pBt ){
1105 sqlite3BtreeCommit(pBt);
1106 }
1107 }
1108 }
danielk1977026d2702004-06-14 13:14:59 +00001109
drh2ac3ee92004-06-07 16:27:46 +00001110 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001111}
1112
drh91b48aa2004-06-30 11:14:18 +00001113/*
1114** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001115** aborted. This happens when one VM causes a rollback due to an
1116** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1117** aborted so that they do not have data rolled out from underneath
1118** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001119*/
1120static void abortOtherActiveVdbes(Vdbe *pVdbe){
1121 Vdbe *pOther;
1122 for(pOther=pVdbe->db->pVdbe; pOther; pOther=pOther->pNext){
1123 if( pOther==pVdbe ) continue;
1124 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1125 closeAllCursors(pOther);
1126 pOther->aborted = 1;
1127 }
1128}
1129
danielk19771d850a72004-05-31 08:26:49 +00001130/*
1131** This routine checks that the sqlite3.activeVdbeCnt count variable
1132** matches the number of vdbe's in the list sqlite3.pVdbe that are
1133** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001134** This is an internal self-check only - it is not an essential processing
1135** step.
danielk19771d850a72004-05-31 08:26:49 +00001136**
1137** This is a no-op if NDEBUG is defined.
1138*/
1139#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001140static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001141 Vdbe *p;
1142 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001143 p = db->pVdbe;
1144 while( p ){
drh92f02c32004-09-02 14:57:08 +00001145 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001146 cnt++;
1147 }
1148 p = p->pNext;
1149 }
danielk19771d850a72004-05-31 08:26:49 +00001150 assert( cnt==db->activeVdbeCnt );
1151}
1152#else
1153#define checkActiveVdbeCnt(x)
1154#endif
1155
danielk19773cf86062004-05-26 10:11:05 +00001156/*
drh92f02c32004-09-02 14:57:08 +00001157** This routine is called the when a VDBE tries to halt. If the VDBE
1158** has made changes and is in autocommit mode, then commit those
1159** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001160**
drh92f02c32004-09-02 14:57:08 +00001161** This routine is the only way to move the state of a VM from
1162** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1163**
1164** Return an error code. If the commit could not complete because of
1165** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1166** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001167*/
drh92f02c32004-09-02 14:57:08 +00001168int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001169 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001170 int i;
danielk19771d850a72004-05-31 08:26:49 +00001171 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
drh9a324642003-09-06 20:12:01 +00001172
drh92f02c32004-09-02 14:57:08 +00001173 if( p->magic!=VDBE_MAGIC_RUN ){
1174 /* Already halted. Nothing to do. */
1175 assert( p->magic==VDBE_MAGIC_HALT );
1176 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001177 }
drh92f02c32004-09-02 14:57:08 +00001178 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001179 checkActiveVdbeCnt(db);
1180 if( db->autoCommit && db->activeVdbeCnt==1 ){
1181 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk197713adf8a2004-06-03 16:08:41 +00001182 /* The auto-commit flag is true, there are no other active queries
1183 ** using this handle and the vdbe program was successful or hit an
drh92f02c32004-09-02 14:57:08 +00001184 ** 'OR FAIL' constraint. This means a commit is required.
danielk197713adf8a2004-06-03 16:08:41 +00001185 */
drh92f02c32004-09-02 14:57:08 +00001186 int rc = vdbeCommit(db);
1187 if( rc==SQLITE_BUSY ){
1188 return SQLITE_BUSY;
1189 }else if( rc!=SQLITE_OK ){
1190 p->rc = rc;
1191 xFunc = sqlite3BtreeRollback;
danielk197713adf8a2004-06-03 16:08:41 +00001192 }
danielk19771d850a72004-05-31 08:26:49 +00001193 }else{
1194 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +00001195 }
danielk19771d850a72004-05-31 08:26:49 +00001196 }else{
1197 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1198 xFunc = sqlite3BtreeCommitStmt;
1199 }else if( p->errorAction==OE_Abort ){
1200 xFunc = sqlite3BtreeRollbackStmt;
1201 }else{
1202 xFunc = sqlite3BtreeRollback;
1203 db->autoCommit = 1;
drh91b48aa2004-06-30 11:14:18 +00001204 abortOtherActiveVdbes(p);
danielk19771d850a72004-05-31 08:26:49 +00001205 }
1206 }
1207
danielk197713adf8a2004-06-03 16:08:41 +00001208 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
1209 ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
1210 ** each backend. If an error occurs and the return code is still
1211 ** SQLITE_OK, set the return code to the new error value.
1212 */
1213 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +00001214 int rc;
danielk19771d850a72004-05-31 08:26:49 +00001215 Btree *pBt = db->aDb[i].pBt;
danielk197777d83ba2004-05-31 10:08:14 +00001216 if( pBt ){
1217 rc = xFunc(pBt);
1218 if( p->rc==SQLITE_OK ) p->rc = rc;
1219 }
danielk19771d850a72004-05-31 08:26:49 +00001220 }
1221
danielk1977b28af712004-06-21 06:50:26 +00001222 /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
1223 if( p->changeCntOn ){
1224 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1225 sqlite3VdbeSetChanges(db, p->nChange);
1226 }else{
1227 sqlite3VdbeSetChanges(db, 0);
1228 }
1229 p->nChange = 0;
1230 }
danielk19771d850a72004-05-31 08:26:49 +00001231
drh92f02c32004-09-02 14:57:08 +00001232 /* Rollback or commit any schema changes that occurred. */
danielk19771d850a72004-05-31 08:26:49 +00001233 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001234 sqlite3RollbackInternalChanges(db);
danielk1977026d2702004-06-14 13:14:59 +00001235 }else if( db->flags & SQLITE_InternChanges ){
danielk1977ec8450f2004-06-19 09:35:36 +00001236 sqlite3CommitInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001237 }
danielk19771d850a72004-05-31 08:26:49 +00001238
drh92f02c32004-09-02 14:57:08 +00001239 /* We have successfully halted and closed the VM. Record this fact. */
1240 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001241 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001242 }
drh92f02c32004-09-02 14:57:08 +00001243 p->magic = VDBE_MAGIC_HALT;
1244 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001245
drh92f02c32004-09-02 14:57:08 +00001246 return SQLITE_OK;
1247}
1248
1249/*
1250** Clean up a VDBE after execution but do not delete the VDBE just yet.
1251** Write any error messages into *pzErrMsg. Return the result code.
1252**
1253** After this routine is run, the VDBE should be ready to be executed
1254** again.
1255**
1256** To look at it another way, this routine resets the state of the
1257** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1258** VDBE_MAGIC_INIT.
1259*/
1260int sqlite3VdbeReset(Vdbe *p){
1261 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
1262 sqlite3Error(p->db, SQLITE_MISUSE, 0 ,0);
1263 return SQLITE_MISUSE;
1264 }
1265
1266 /* If the VM did not run to completion or if it encountered an
1267 ** error, then it might not have been halted properly. So halt
1268 ** it now.
1269 */
1270 sqlite3VdbeHalt(p);
1271
1272 /* Transfer the error code and error message from the VDBE into the
1273 ** main database structure.
1274 */
1275 if( p->zErrMsg ){
1276 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0);
1277 sqliteFree(p->zErrMsg);
1278 p->zErrMsg = 0;
1279 }else if( p->rc ){
1280 sqlite3Error(p->db, p->rc, 0);
1281 }else{
1282 sqlite3Error(p->db, SQLITE_OK, 0);
1283 }
1284
1285 /* Reclaim all memory used by the VDBE
1286 */
1287 Cleanup(p);
1288
1289 /* Save profiling information from this VDBE run.
1290 */
danielk19771d850a72004-05-31 08:26:49 +00001291 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001292#ifdef VDBE_PROFILE
1293 {
1294 FILE *out = fopen("vdbe_profile.out", "a");
1295 if( out ){
1296 int i;
1297 fprintf(out, "---- ");
1298 for(i=0; i<p->nOp; i++){
1299 fprintf(out, "%02x", p->aOp[i].opcode);
1300 }
1301 fprintf(out, "\n");
1302 for(i=0; i<p->nOp; i++){
1303 fprintf(out, "%6d %10lld %8lld ",
1304 p->aOp[i].cnt,
1305 p->aOp[i].cycles,
1306 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1307 );
danielk19774adee202004-05-08 08:23:19 +00001308 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001309 }
1310 fclose(out);
1311 }
1312 }
1313#endif
1314 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001315 p->aborted = 0;
drh9a324642003-09-06 20:12:01 +00001316 return p->rc;
1317}
drh92f02c32004-09-02 14:57:08 +00001318
drh9a324642003-09-06 20:12:01 +00001319/*
1320** Clean up and delete a VDBE after execution. Return an integer which is
1321** the result code. Write any error message text into *pzErrMsg.
1322*/
danielk19779e6db7d2004-06-21 08:18:51 +00001323int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001324 int rc = SQLITE_OK;
drh9bb575f2004-09-06 17:24:11 +00001325 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001326
danielk1977b5548a82004-06-26 13:51:33 +00001327 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1328 rc = sqlite3VdbeReset(p);
1329 }else if( p->magic!=VDBE_MAGIC_INIT ){
1330 /* sqlite3Error(p->db, SQLITE_MISUSE, 0); */
drh9a324642003-09-06 20:12:01 +00001331 return SQLITE_MISUSE;
1332 }
danielk19774adee202004-05-08 08:23:19 +00001333 sqlite3VdbeDelete(p);
drha1f9b5e2004-02-14 16:31:02 +00001334 if( rc==SQLITE_SCHEMA ){
danielk19774adee202004-05-08 08:23:19 +00001335 sqlite3ResetInternalSchema(db, 0);
drha1f9b5e2004-02-14 16:31:02 +00001336 }
drh9a324642003-09-06 20:12:01 +00001337 return rc;
1338}
1339
1340/*
drhf92c7ff2004-06-19 15:40:23 +00001341** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001342** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001343** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001344** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001345*/
1346void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1347 int i;
1348 for(i=0; i<pVdbeFunc->nAux; i++){
1349 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1350 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1351 if( pAux->xDelete ){
1352 pAux->xDelete(pAux->pAux);
1353 }
1354 pAux->pAux = 0;
1355 }
1356 }
1357}
1358
1359/*
drh9a324642003-09-06 20:12:01 +00001360** Delete an entire VDBE.
1361*/
danielk19774adee202004-05-08 08:23:19 +00001362void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001363 int i;
1364 if( p==0 ) return;
1365 Cleanup(p);
1366 if( p->pPrev ){
1367 p->pPrev->pNext = p->pNext;
1368 }else{
1369 assert( p->db->pVdbe==p );
1370 p->db->pVdbe = p->pNext;
1371 }
1372 if( p->pNext ){
1373 p->pNext->pPrev = p->pPrev;
1374 }
1375 p->pPrev = p->pNext = 0;
1376 if( p->nOpAlloc==0 ){
1377 p->aOp = 0;
1378 p->nOp = 0;
1379 }
1380 for(i=0; i<p->nOp; i++){
drhd3d39e92004-05-20 22:16:29 +00001381 Op *pOp = &p->aOp[i];
1382 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1383 sqliteFree(pOp->p3);
drh9a324642003-09-06 20:12:01 +00001384 }
danielk1977682f68b2004-06-05 10:22:17 +00001385 if( pOp->p3type==P3_VDBEFUNC ){
1386 VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
danielk1977e159fdf2004-06-21 10:45:06 +00001387 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
danielk1977682f68b2004-06-05 10:22:17 +00001388 sqliteFree(pVdbeFunc);
1389 }
drh9a324642003-09-06 20:12:01 +00001390 }
drh7c972de2003-09-06 22:18:07 +00001391 for(i=0; i<p->nVar; i++){
drh290c1942004-08-21 17:54:45 +00001392 sqlite3VdbeMemRelease(&p->aVar[i]);
drh7c972de2003-09-06 22:18:07 +00001393 }
drh9a324642003-09-06 20:12:01 +00001394 sqliteFree(p->aOp);
1395 sqliteFree(p->aLabel);
1396 sqliteFree(p->aStack);
danielk1977b20e56b2004-06-15 13:36:30 +00001397 if( p->aColName ){
1398 for(i=0; i<(p->nResColumn)*2; i++){
1399 sqlite3VdbeMemRelease(&(p->aColName[i]));
1400 }
1401 sqliteFree(p->aColName);
1402 }
drh9a324642003-09-06 20:12:01 +00001403 p->magic = VDBE_MAGIC_DEAD;
1404 sqliteFree(p);
1405}
drha11846b2004-01-07 18:52:56 +00001406
1407/*
drha11846b2004-01-07 18:52:56 +00001408** If a MoveTo operation is pending on the given cursor, then do that
1409** MoveTo now. Return an error code. If no MoveTo is pending, this
1410** routine does nothing and returns SQLITE_OK.
1411*/
danielk19774adee202004-05-08 08:23:19 +00001412int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001413 if( p->deferredMoveto ){
1414 int res;
danielk1977132872b2004-05-10 10:37:18 +00001415 extern int sqlite3_search_count;
drha3b321d2004-05-11 09:31:31 +00001416 assert( p->intKey );
danielk19776490beb2004-05-11 06:17:21 +00001417 if( p->intKey ){
1418 sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1419 }else{
1420 sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res);
1421 }
drhd3d39e92004-05-20 22:16:29 +00001422 *p->pIncrKey = 0;
drha11846b2004-01-07 18:52:56 +00001423 p->lastRecno = keyToInt(p->movetoTarget);
1424 p->recnoIsValid = res==0;
1425 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00001426 sqlite3BtreeNext(p->pCursor, &res);
drha11846b2004-01-07 18:52:56 +00001427 }
danielk1977132872b2004-05-10 10:37:18 +00001428 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001429 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001430 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001431 }
1432 return SQLITE_OK;
1433}
danielk19774adee202004-05-08 08:23:19 +00001434
drhab9f7f12004-05-08 10:56:11 +00001435/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001436** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001437**
danielk1977cfcdaef2004-05-12 07:33:33 +00001438** sqlite3VdbeSerialType()
1439** sqlite3VdbeSerialTypeLen()
1440** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001441** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001442** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001443**
1444** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001445** data and index records. Each serialized value consists of a
1446** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1447** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001448**
danielk1977cfcdaef2004-05-12 07:33:33 +00001449** In an SQLite index record, the serial type is stored directly before
1450** the blob of data that it corresponds to. In a table record, all serial
1451** types are stored at the start of the record, and the blobs of data at
1452** the end. Hence these functions allow the caller to handle the
1453** serial-type and data blob seperately.
1454**
1455** The following table describes the various storage classes for data:
1456**
1457** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001458** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001459** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001460** 1 1 signed integer
1461** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001462** 3 3 signed integer
1463** 4 4 signed integer
1464** 5 6 signed integer
1465** 6 8 signed integer
1466** 7 8 IEEE float
1467** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001468** N>=12 and even (N-12)/2 BLOB
1469** N>=13 and odd (N-13)/2 text
1470**
1471*/
1472
1473/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001474** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001475*/
drh25aa1b42004-05-28 01:39:01 +00001476u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001477 int flags = pMem->flags;
1478
1479 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001480 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001481 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001482 if( flags&MEM_Int ){
1483 /* Figure out whether to use 1, 2, 4 or 8 bytes. */
1484 i64 i = pMem->i;
1485 if( i>=-127 && i<=127 ) return 1;
1486 if( i>=-32767 && i<=32767 ) return 2;
drha19b7752004-05-30 21:14:58 +00001487 if( i>=-8388607 && i<=8388607 ) return 3;
1488 if( i>=-2147483647 && i<=2147483647 ) return 4;
1489 if( i>=-140737488355328L && i<=140737488355328L ) return 5;
1490 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001491 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001492 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001493 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001494 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001495 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001496 int n = pMem->n;
1497 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001498 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001499 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001500 if( flags&MEM_Blob ){
1501 return (pMem->n*2 + 12);
1502 }
1503 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001504}
1505
1506/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001507** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001508*/
drh25aa1b42004-05-28 01:39:01 +00001509int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001510 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001511 return (serial_type-12)/2;
1512 }else{
drha19b7752004-05-30 21:14:58 +00001513 static u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001514 return aSize[serial_type];
1515 }
danielk1977192ac1d2004-05-10 07:17:30 +00001516}
1517
1518/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001519** Write the serialized data blob for the value stored in pMem into
1520** buf. It is assumed that the caller has allocated sufficient space.
1521** Return the number of bytes written.
1522*/
danielk1977b1bc9532004-05-22 03:05:33 +00001523int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001524 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001525 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001526
danielk1977cfcdaef2004-05-12 07:33:33 +00001527 /* NULL */
drha19b7752004-05-30 21:14:58 +00001528 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001529 return 0;
1530 }
1531
drh1483e142004-05-21 21:12:42 +00001532 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001533 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001534 u64 v;
1535 int i;
drha19b7752004-05-30 21:14:58 +00001536 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001537 v = *(u64*)&pMem->r;
1538 }else{
1539 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001540 }
drh1483e142004-05-21 21:12:42 +00001541 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1542 while( i-- ){
1543 buf[i] = (v&0xFF);
1544 v >>= 8;
1545 }
1546 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001547 }
1548
1549 /* String or blob */
1550 assert( serial_type>=12 );
1551 len = sqlite3VdbeSerialTypeLen(serial_type);
1552 memcpy(buf, pMem->z, len);
1553 return len;
1554}
1555
1556/*
1557** Deserialize the data blob pointed to by buf as serial type serial_type
1558** and store the result in pMem. Return the number of bytes read.
1559*/
danielk1977b1bc9532004-05-22 03:05:33 +00001560int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001561 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001562 u32 serial_type, /* Serial type to deserialize */
1563 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001564){
danielk197790e4d952004-05-10 10:05:53 +00001565 int len;
1566
drha19b7752004-05-30 21:14:58 +00001567 if( serial_type==0 ){
1568 /* NULL */
1569 pMem->flags = MEM_Null;
1570 return 0;
1571 }
drh696b32f2004-05-30 01:51:52 +00001572 len = sqlite3VdbeSerialTypeLen(serial_type);
drha19b7752004-05-30 21:14:58 +00001573 if( serial_type<=7 ){
drh696b32f2004-05-30 01:51:52 +00001574 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001575 if( serial_type<=4 ){
drhe51c44f2004-05-30 20:46:09 +00001576 /* 32-bit integer type. This is handled by a special case for
1577 ** performance reasons. */
1578 int v = buf[0];
1579 int n;
1580 if( v&0x80 ){
1581 v |= -256;
1582 }
1583 for(n=1; n<len; n++){
1584 v = (v<<8) | buf[n];
1585 }
drh1483e142004-05-21 21:12:42 +00001586 pMem->flags = MEM_Int;
drhe51c44f2004-05-30 20:46:09 +00001587 pMem->i = v;
1588 return n;
1589 }else{
1590 u64 v = 0;
1591 int n;
1592
1593 if( buf[0]&0x80 ){
1594 v = -1;
1595 }
1596 for(n=0; n<len; n++){
1597 v = (v<<8) | buf[n];
1598 }
drha19b7752004-05-30 21:14:58 +00001599 if( serial_type==7 ){
drhe51c44f2004-05-30 20:46:09 +00001600 pMem->flags = MEM_Real;
1601 pMem->r = *(double*)&v;
1602 }else{
1603 pMem->flags = MEM_Int;
1604 pMem->i = *(i64*)&v;
1605 }
drh1483e142004-05-21 21:12:42 +00001606 }
drha19b7752004-05-30 21:14:58 +00001607 }else{
drh696b32f2004-05-30 01:51:52 +00001608 /* String or blob */
drha19b7752004-05-30 21:14:58 +00001609 assert( serial_type>=12 );
drh696b32f2004-05-30 01:51:52 +00001610 pMem->z = (char *)buf;
1611 pMem->n = len;
drh1b743be2004-06-22 22:04:46 +00001612 pMem->xDel = 0;
drh696b32f2004-05-30 01:51:52 +00001613 if( serial_type&0x01 ){
1614 pMem->flags = MEM_Str | MEM_Ephem;
1615 }else{
1616 pMem->flags = MEM_Blob | MEM_Ephem;
1617 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001618 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001619 return len;
danielk1977192ac1d2004-05-10 07:17:30 +00001620}
1621
1622/*
drh7a224de2004-06-02 01:22:02 +00001623** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001624** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1625** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001626** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1627** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001628*/
drh7a224de2004-06-02 01:22:02 +00001629int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001630 void *userData,
1631 int nKey1, const void *pKey1,
1632 int nKey2, const void *pKey2
1633){
drhd3d39e92004-05-20 22:16:29 +00001634 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001635 u32 d1, d2; /* Offset into aKey[] of next data element */
1636 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1637 u32 szHdr1, szHdr2; /* Number of bytes in header */
1638 int i = 0;
1639 int nField;
1640 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001641 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1642 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001643
1644 Mem mem1;
1645 Mem mem2;
1646 mem1.enc = pKeyInfo->enc;
1647 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001648
1649 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1650 d1 = szHdr1;
1651 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1652 d2 = szHdr2;
1653 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001654 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001655 u32 serial_type1;
1656 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001657
1658 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001659 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001660 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001661 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001662 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001663
1664 /* Assert that there is enough space left in each key for the blob of
1665 ** data to go with the serial type just read. This assert may fail if
1666 ** the file is corrupted. Then read the value from each key into mem1
1667 ** and mem2 respectively.
1668 */
drh25aa1b42004-05-28 01:39:01 +00001669 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1670 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001671
drhd5788202004-05-28 08:21:05 +00001672 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
danielk1977d8123362004-06-12 09:25:12 +00001673 sqlite3VdbeMemRelease(&mem1);
1674 sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001675 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001676 break;
1677 }
1678 i++;
1679 }
1680
1681 /* One of the keys ran out of fields, but all the fields up to that point
1682 ** were equal. If the incrKey flag is true, then the second key is
1683 ** treated as larger.
1684 */
1685 if( rc==0 ){
1686 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001687 rc = -1;
1688 }else if( d1<nKey1 ){
1689 rc = 1;
1690 }else if( d2<nKey2 ){
1691 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001692 }
1693 }
1694
drhd3194f52004-05-27 19:59:32 +00001695 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1696 rc = -rc;
1697 }
1698
1699 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001700}
drhd5788202004-05-28 08:21:05 +00001701
1702/*
drh7a224de2004-06-02 01:22:02 +00001703** The argument is an index entry composed using the OP_MakeRecord opcode.
1704** The last entry in this record should be an integer (specifically
1705** an integer rowid). This routine returns the number of bytes in
1706** that integer.
drhd5788202004-05-28 08:21:05 +00001707*/
1708int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1709 u32 szHdr; /* Size of the header */
1710 u32 typeRowid; /* Serial type of the rowid */
1711
1712 sqlite3GetVarint32(aKey, &szHdr);
1713 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1714 return sqlite3VdbeSerialTypeLen(typeRowid);
1715}
danielk1977eb015e02004-05-18 01:31:14 +00001716
1717
1718/*
drh7a224de2004-06-02 01:22:02 +00001719** pCur points at an index entry created using the OP_MakeRecord opcode.
1720** Read the rowid (the last field in the record) and store it in *rowid.
1721** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001722*/
1723int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001724 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001725 int rc;
drhd5788202004-05-28 08:21:05 +00001726 u32 szHdr; /* Size of the header */
1727 u32 typeRowid; /* Serial type of the rowid */
1728 u32 lenRowid; /* Size of the rowid */
1729 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001730
drhd5788202004-05-28 08:21:05 +00001731 sqlite3BtreeKeySize(pCur, &nCellKey);
1732 if( nCellKey<=0 ){
1733 return SQLITE_CORRUPT;
1734 }
1735 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1736 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001737 return rc;
1738 }
drhd5788202004-05-28 08:21:05 +00001739 sqlite3GetVarint32(m.z, &szHdr);
1740 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1741 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1742 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1743 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001744 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001745 return SQLITE_OK;
1746}
1747
drh7cf6e4d2004-05-19 14:56:55 +00001748/*
drhd3d39e92004-05-20 22:16:29 +00001749** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001750** the key string in pKey (of length nKey). Write into *pRes a number
1751** that is negative, zero, or positive if pC is less than, equal to,
1752** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001753**
drhd5788202004-05-28 08:21:05 +00001754** pKey is either created without a rowid or is truncated so that it
1755** omits the rowid at the end. The rowid at the end of the index entry
1756** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001757*/
danielk1977183f9f72004-05-13 05:20:26 +00001758int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001759 Cursor *pC, /* The cursor to compare against */
1760 int nKey, const u8 *pKey, /* The key to compare */
1761 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001762){
danielk1977e0d4b062004-06-28 01:11:46 +00001763 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001764 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001765 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001766 int lenRowid;
1767 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001768
1769 sqlite3BtreeKeySize(pCur, &nCellKey);
1770 if( nCellKey<=0 ){
1771 *res = 0;
1772 return SQLITE_OK;
1773 }
drhd5788202004-05-28 08:21:05 +00001774 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1775 if( rc ){
1776 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001777 }
drhd5788202004-05-28 08:21:05 +00001778 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
drh7a224de2004-06-02 01:22:02 +00001779 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001780 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001781 return SQLITE_OK;
1782}
danielk1977b28af712004-06-21 06:50:26 +00001783
1784/*
1785** This routine sets the value to be returned by subsequent calls to
1786** sqlite3_changes() on the database handle 'db'.
1787*/
1788void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1789 db->nChange = nChange;
1790 db->nTotalChange += nChange;
1791}
1792
1793/*
1794** Set a flag in the vdbe to update the change counter when it is finalised
1795** or reset.
1796*/
1797void sqlite3VdbeCountChanges(Vdbe *p){
1798 p->changeCntOn = 1;
1799}