blob: 9de4fe04f251449724d3b8fb5a2b16b7d8e881c9 [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"
drh9a324642003-09-06 20:12:01 +000018#include "vdbeInt.h"
19
20
drh46c99e02007-08-27 23:26:59 +000021
drh9a324642003-09-06 20:12:01 +000022/*
23** When debugging the code generator in a symbolic debugger, one can
mlcreech3a00f902008-03-04 17:45:01 +000024** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000025** as they are added to the instruction stream.
26*/
drh8d904f02005-06-14 17:47:58 +000027#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +000028int sqlite3VdbeAddopTrace = 0;
drh9a324642003-09-06 20:12:01 +000029#endif
30
31
32/*
33** Create a new virtual database engine.
34*/
drh9bb575f2004-09-06 17:24:11 +000035Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000036 Vdbe *p;
drh17435752007-08-16 04:30:38 +000037 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000038 if( p==0 ) return 0;
39 p->db = db;
40 if( db->pVdbe ){
41 db->pVdbe->pPrev = p;
42 }
43 p->pNext = db->pVdbe;
44 p->pPrev = 0;
45 db->pVdbe = p;
46 p->magic = VDBE_MAGIC_INIT;
47 return p;
48}
49
50/*
drhb900aaf2006-11-09 00:24:53 +000051** Remember the SQL string for a prepared statement.
52*/
danielk19776ab3a2e2009-02-19 14:39:25 +000053void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
dan1d2ce4f2009-10-19 18:11:09 +000054 assert( isPrepareV2==1 || isPrepareV2==0 );
drhb900aaf2006-11-09 00:24:53 +000055 if( p==0 ) return;
danielk19776ab3a2e2009-02-19 14:39:25 +000056#ifdef SQLITE_OMIT_TRACE
57 if( !isPrepareV2 ) return;
58#endif
drhb900aaf2006-11-09 00:24:53 +000059 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000060 p->zSql = sqlite3DbStrNDup(p->db, z, n);
shanef639c402009-11-03 19:42:30 +000061 p->isPrepareV2 = (u8)isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000062}
63
64/*
65** Return the SQL associated with a prepared statement
66*/
danielk1977d0e2a852007-11-14 06:48:48 +000067const char *sqlite3_sql(sqlite3_stmt *pStmt){
danielk19776ab3a2e2009-02-19 14:39:25 +000068 Vdbe *p = (Vdbe *)pStmt;
drh87f5c5f2010-01-20 01:20:56 +000069 return (p && p->isPrepareV2) ? p->zSql : 0;
drhb900aaf2006-11-09 00:24:53 +000070}
71
72/*
drhc5155252007-01-08 21:07:17 +000073** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000074*/
drhc5155252007-01-08 21:07:17 +000075void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
76 Vdbe tmp, *pTmp;
77 char *zTmp;
drhc5155252007-01-08 21:07:17 +000078 tmp = *pA;
79 *pA = *pB;
80 *pB = tmp;
81 pTmp = pA->pNext;
82 pA->pNext = pB->pNext;
83 pB->pNext = pTmp;
84 pTmp = pA->pPrev;
85 pA->pPrev = pB->pPrev;
86 pB->pPrev = pTmp;
87 zTmp = pA->zSql;
88 pA->zSql = pB->zSql;
89 pB->zSql = zTmp;
danielk19776ab3a2e2009-02-19 14:39:25 +000090 pB->isPrepareV2 = pA->isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000091}
92
drhcf1023c2007-05-08 20:59:49 +000093#ifdef SQLITE_DEBUG
drhb900aaf2006-11-09 00:24:53 +000094/*
drh9a324642003-09-06 20:12:01 +000095** Turn tracing on or off
96*/
danielk19774adee202004-05-08 08:23:19 +000097void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000098 p->trace = trace;
99}
drhcf1023c2007-05-08 20:59:49 +0000100#endif
drh9a324642003-09-06 20:12:01 +0000101
102/*
danielk197700e13612008-11-17 19:18:54 +0000103** Resize the Vdbe.aOp array so that it is at least one op larger than
104** it was.
danielk1977ace3eb22006-01-26 10:35:04 +0000105**
danielk197700e13612008-11-17 19:18:54 +0000106** If an out-of-memory error occurs while resizing the array, return
107** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
108** unchanged (this is so that any opcodes already allocated can be
109** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000110*/
danielk197700e13612008-11-17 19:18:54 +0000111static int growOpArray(Vdbe *p){
drha4e5d582007-10-20 15:41:57 +0000112 VdbeOp *pNew;
danielk197700e13612008-11-17 19:18:54 +0000113 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
114 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000115 if( pNew ){
drhb45f65d2009-03-01 19:42:11 +0000116 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
drha4e5d582007-10-20 15:41:57 +0000117 p->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000118 }
danielk197700e13612008-11-17 19:18:54 +0000119 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000120}
121
122/*
drh9a324642003-09-06 20:12:01 +0000123** Add a new instruction to the list of instructions current in the
124** VDBE. Return the address of the new instruction.
125**
126** Parameters:
127**
128** p Pointer to the VDBE
129**
130** op The opcode for this instruction
131**
drh66a51672008-01-03 00:01:23 +0000132** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000133**
danielk19774adee202004-05-08 08:23:19 +0000134** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000135** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000136** operand.
137*/
drh66a51672008-01-03 00:01:23 +0000138int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000139 int i;
drh701a0ae2004-02-22 20:05:00 +0000140 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000141
142 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000143 assert( p->magic==VDBE_MAGIC_INIT );
drh8df32842008-12-09 02:51:23 +0000144 assert( op>0 && op<0xff );
drhfd2d26b2006-03-15 22:44:36 +0000145 if( p->nOpAlloc<=i ){
danielk197700e13612008-11-17 19:18:54 +0000146 if( growOpArray(p) ){
drhc42ed162009-06-26 14:04:51 +0000147 return 1;
drhfd2d26b2006-03-15 22:44:36 +0000148 }
drh9a324642003-09-06 20:12:01 +0000149 }
danielk197701256832007-04-18 14:24:32 +0000150 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000151 pOp = &p->aOp[i];
drh8df32842008-12-09 02:51:23 +0000152 pOp->opcode = (u8)op;
drh26c9b5e2008-04-11 14:56:53 +0000153 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000154 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000155 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000156 pOp->p3 = p3;
157 pOp->p4.p = 0;
158 pOp->p4type = P4_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000159#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000160 pOp->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000161 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000162#endif
drh26c9b5e2008-04-11 14:56:53 +0000163#ifdef VDBE_PROFILE
164 pOp->cycles = 0;
165 pOp->cnt = 0;
166#endif
drh9a324642003-09-06 20:12:01 +0000167 return i;
168}
drh66a51672008-01-03 00:01:23 +0000169int sqlite3VdbeAddOp0(Vdbe *p, int op){
170 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
171}
172int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
173 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
174}
175int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
176 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000177}
178
drh66a51672008-01-03 00:01:23 +0000179
drh701a0ae2004-02-22 20:05:00 +0000180/*
drh66a51672008-01-03 00:01:23 +0000181** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000182*/
drh66a51672008-01-03 00:01:23 +0000183int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000184 Vdbe *p, /* Add the opcode to this VM */
185 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000186 int p1, /* The P1 operand */
187 int p2, /* The P2 operand */
188 int p3, /* The P3 operand */
189 const char *zP4, /* The P4 operand */
190 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000191){
drh66a51672008-01-03 00:01:23 +0000192 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
193 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000194 return addr;
195}
196
197/*
drh5d9c9da2011-06-03 20:11:17 +0000198** Add an OP_ParseSchema opcode. This routine is broken out from
199** sqlite3VdbeAddOp4() since it needs to also local all btrees.
200**
201** The zWhere string must have been obtained from sqlite3_malloc().
202** This routine will take ownership of the allocated memory.
203*/
204void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
205 int j;
206 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
207 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
208 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
209}
210
211/*
drh8cff69d2009-11-12 19:59:44 +0000212** Add an opcode that includes the p4 value as an integer.
213*/
214int sqlite3VdbeAddOp4Int(
215 Vdbe *p, /* Add the opcode to this VM */
216 int op, /* The new opcode */
217 int p1, /* The P1 operand */
218 int p2, /* The P2 operand */
219 int p3, /* The P3 operand */
220 int p4 /* The P4 operand as an integer */
221){
222 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
223 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
224 return addr;
225}
226
227/*
drh9a324642003-09-06 20:12:01 +0000228** Create a new symbolic label for an instruction that has yet to be
229** coded. The symbolic label is really just a negative number. The
230** label can be used as the P2 value of an operation. Later, when
231** the label is resolved to a specific address, the VDBE will scan
232** through its operation list and change all values of P2 which match
233** the label into the resolved address.
234**
235** The VDBE knows that a P2 value is a label because labels are
236** always negative and P2 values are suppose to be non-negative.
237** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000238**
239** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000240*/
danielk19774adee202004-05-08 08:23:19 +0000241int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000242 int i;
243 i = p->nLabel++;
244 assert( p->magic==VDBE_MAGIC_INIT );
245 if( i>=p->nLabelAlloc ){
drh6a1e0712008-12-05 15:24:15 +0000246 int n = p->nLabelAlloc*2 + 5;
danielk19771e536952007-08-16 10:09:01 +0000247 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drh6a1e0712008-12-05 15:24:15 +0000248 n*sizeof(p->aLabel[0]));
249 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
drh9a324642003-09-06 20:12:01 +0000250 }
drh76ff3a02004-09-24 22:32:30 +0000251 if( p->aLabel ){
252 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000253 }
drh9a324642003-09-06 20:12:01 +0000254 return -1-i;
255}
256
257/*
258** Resolve label "x" to be the address of the next instruction to
259** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000260** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000261*/
danielk19774adee202004-05-08 08:23:19 +0000262void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000263 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000264 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000265 assert( j>=0 && j<p->nLabel );
266 if( p->aLabel ){
267 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000268 }
269}
270
drh4611d922010-02-25 14:47:01 +0000271/*
272** Mark the VDBE as one that can only be run one time.
273*/
274void sqlite3VdbeRunOnlyOnce(Vdbe *p){
275 p->runOnlyOnce = 1;
276}
277
drhff738bc2009-09-24 00:09:58 +0000278#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
dan144926d2009-09-09 11:37:20 +0000279
280/*
281** The following type and function are used to iterate through all opcodes
282** in a Vdbe main program and each of the sub-programs (triggers) it may
283** invoke directly or indirectly. It should be used as follows:
284**
285** Op *pOp;
286** VdbeOpIter sIter;
287**
288** memset(&sIter, 0, sizeof(sIter));
289** sIter.v = v; // v is of type Vdbe*
290** while( (pOp = opIterNext(&sIter)) ){
291** // Do something with pOp
292** }
293** sqlite3DbFree(v->db, sIter.apSub);
294**
295*/
296typedef struct VdbeOpIter VdbeOpIter;
297struct VdbeOpIter {
298 Vdbe *v; /* Vdbe to iterate through the opcodes of */
299 SubProgram **apSub; /* Array of subprograms */
300 int nSub; /* Number of entries in apSub */
301 int iAddr; /* Address of next instruction to return */
302 int iSub; /* 0 = main program, 1 = first sub-program etc. */
303};
304static Op *opIterNext(VdbeOpIter *p){
305 Vdbe *v = p->v;
306 Op *pRet = 0;
307 Op *aOp;
308 int nOp;
309
310 if( p->iSub<=p->nSub ){
311
312 if( p->iSub==0 ){
313 aOp = v->aOp;
314 nOp = v->nOp;
315 }else{
316 aOp = p->apSub[p->iSub-1]->aOp;
317 nOp = p->apSub[p->iSub-1]->nOp;
318 }
319 assert( p->iAddr<nOp );
320
321 pRet = &aOp[p->iAddr];
322 p->iAddr++;
323 if( p->iAddr==nOp ){
324 p->iSub++;
325 p->iAddr = 0;
326 }
327
328 if( pRet->p4type==P4_SUBPROGRAM ){
329 int nByte = (p->nSub+1)*sizeof(SubProgram*);
330 int j;
331 for(j=0; j<p->nSub; j++){
332 if( p->apSub[j]==pRet->p4.pProgram ) break;
333 }
334 if( j==p->nSub ){
335 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
336 if( !p->apSub ){
337 pRet = 0;
338 }else{
339 p->apSub[p->nSub++] = pRet->p4.pProgram;
340 }
341 }
342 }
343 }
344
345 return pRet;
346}
347
348/*
danf3677212009-09-10 16:14:50 +0000349** Check if the program stored in the VM associated with pParse may
drhff738bc2009-09-24 00:09:58 +0000350** throw an ABORT exception (causing the statement, but not entire transaction
dan144926d2009-09-09 11:37:20 +0000351** to be rolled back). This condition is true if the main program or any
352** sub-programs contains any of the following:
353**
354** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
355** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
356** * OP_Destroy
357** * OP_VUpdate
358** * OP_VRename
dan32b09f22009-09-23 17:29:59 +0000359** * OP_FkCounter with P2==0 (immediate foreign key constraint)
dan144926d2009-09-09 11:37:20 +0000360**
danf3677212009-09-10 16:14:50 +0000361** Then check that the value of Parse.mayAbort is true if an
362** ABORT may be thrown, or false otherwise. Return true if it does
363** match, or false otherwise. This function is intended to be used as
364** part of an assert statement in the compiler. Similar to:
365**
366** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
dan144926d2009-09-09 11:37:20 +0000367*/
danf3677212009-09-10 16:14:50 +0000368int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
369 int hasAbort = 0;
dan144926d2009-09-09 11:37:20 +0000370 Op *pOp;
371 VdbeOpIter sIter;
372 memset(&sIter, 0, sizeof(sIter));
373 sIter.v = v;
374
375 while( (pOp = opIterNext(&sIter))!=0 ){
376 int opcode = pOp->opcode;
377 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
dan32b09f22009-09-23 17:29:59 +0000378#ifndef SQLITE_OMIT_FOREIGN_KEY
dan0ff297e2009-09-25 17:03:14 +0000379 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
dan32b09f22009-09-23 17:29:59 +0000380#endif
dan144926d2009-09-09 11:37:20 +0000381 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
382 && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
383 ){
danf3677212009-09-10 16:14:50 +0000384 hasAbort = 1;
dan144926d2009-09-09 11:37:20 +0000385 break;
386 }
387 }
dan144926d2009-09-09 11:37:20 +0000388 sqlite3DbFree(v->db, sIter.apSub);
danf3677212009-09-10 16:14:50 +0000389
390 /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
391 ** If malloc failed, then the while() loop above may not have iterated
392 ** through all opcodes and hasAbort may be set incorrectly. Return
393 ** true for this case to prevent the assert() in the callers frame
394 ** from failing. */
395 return ( v->db->mallocFailed || hasAbort==mayAbort );
dan144926d2009-09-09 11:37:20 +0000396}
drhff738bc2009-09-24 00:09:58 +0000397#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
dan144926d2009-09-09 11:37:20 +0000398
drh9a324642003-09-06 20:12:01 +0000399/*
drh9cbf3422008-01-17 16:22:13 +0000400** Loop through the program looking for P2 values that are negative
401** on jump instructions. Each such value is a label. Resolve the
402** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000403**
404** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000405**
drh13449892005-09-07 21:22:45 +0000406** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000407** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000408** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
drha6c2ed92009-11-14 23:22:23 +0000409**
410** The Op.opflags field is set on all opcodes.
drh76ff3a02004-09-24 22:32:30 +0000411*/
drh9cbf3422008-01-17 16:22:13 +0000412static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000413 int i;
dan165921a2009-08-28 18:53:45 +0000414 int nMaxArgs = *pMaxFuncArgs;
drh76ff3a02004-09-24 22:32:30 +0000415 Op *pOp;
416 int *aLabel = p->aLabel;
drhad4a4b82008-11-05 16:37:34 +0000417 p->readOnly = 1;
drh76ff3a02004-09-24 22:32:30 +0000418 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000419 u8 opcode = pOp->opcode;
420
drha6c2ed92009-11-14 23:22:23 +0000421 pOp->opflags = sqlite3OpcodeProperty[opcode];
drha2baf3a2008-06-18 15:34:09 +0000422 if( opcode==OP_Function || opcode==OP_AggStep ){
drh98757152008-01-09 23:04:12 +0000423 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
drh10fc7272010-12-08 18:30:19 +0000424 }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
drhad4a4b82008-11-05 16:37:34 +0000425 p->readOnly = 0;
danielk1977182c4ba2007-06-27 15:53:34 +0000426#ifndef SQLITE_OMIT_VIRTUALTABLE
drha6c2ed92009-11-14 23:22:23 +0000427 }else if( opcode==OP_VUpdate ){
428 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh4be8b512006-06-13 23:51:34 +0000429 }else if( opcode==OP_VFilter ){
430 int n;
431 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000432 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000433 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000434 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000435#endif
drhc6aff302011-09-01 15:32:47 +0000436 }else if( opcode==OP_Next || opcode==OP_SorterNext ){
dana205a482011-08-27 18:48:57 +0000437 pOp->p4.xAdvance = sqlite3BtreeNext;
438 pOp->p4type = P4_ADVANCE;
439 }else if( opcode==OP_Prev ){
440 pOp->p4.xAdvance = sqlite3BtreePrevious;
441 pOp->p4type = P4_ADVANCE;
danielk1977bc04f852005-03-29 08:26:13 +0000442 }
danielk1977634f2982005-03-28 08:44:07 +0000443
drha6c2ed92009-11-14 23:22:23 +0000444 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
drhd2981512008-01-04 19:33:49 +0000445 assert( -1-pOp->p2<p->nLabel );
446 pOp->p2 = aLabel[-1-pOp->p2];
447 }
drh76ff3a02004-09-24 22:32:30 +0000448 }
drh633e6d52008-07-28 19:34:53 +0000449 sqlite3DbFree(p->db, p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000450 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000451
452 *pMaxFuncArgs = nMaxArgs;
drh76ff3a02004-09-24 22:32:30 +0000453}
454
455/*
drh9a324642003-09-06 20:12:01 +0000456** Return the address of the next instruction to be inserted.
457*/
danielk19774adee202004-05-08 08:23:19 +0000458int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000459 assert( p->magic==VDBE_MAGIC_INIT );
460 return p->nOp;
461}
462
dan65a7cd12009-09-01 12:16:01 +0000463/*
464** This function returns a pointer to the array of opcodes associated with
465** the Vdbe passed as the first argument. It is the callers responsibility
466** to arrange for the returned array to be eventually freed using the
467** vdbeFreeOpArray() function.
468**
469** Before returning, *pnOp is set to the number of entries in the returned
470** array. Also, *pnMaxArg is set to the larger of its current value and
471** the number of entries in the Vdbe.apArg[] array required to execute the
472** returned program.
473*/
dan165921a2009-08-28 18:53:45 +0000474VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
475 VdbeOp *aOp = p->aOp;
dan523a0872009-08-31 05:23:32 +0000476 assert( aOp && !p->db->mallocFailed );
dan65a7cd12009-09-01 12:16:01 +0000477
478 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
drhbdaec522011-04-04 00:14:43 +0000479 assert( p->btreeMask==0 );
dan65a7cd12009-09-01 12:16:01 +0000480
dan165921a2009-08-28 18:53:45 +0000481 resolveP2Values(p, pnMaxArg);
482 *pnOp = p->nOp;
483 p->aOp = 0;
484 return aOp;
485}
486
drh9a324642003-09-06 20:12:01 +0000487/*
488** Add a whole list of operations to the operation stack. Return the
489** address of the first operation added.
490*/
danielk19774adee202004-05-08 08:23:19 +0000491int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000492 int addr;
493 assert( p->magic==VDBE_MAGIC_INIT );
danielk197700e13612008-11-17 19:18:54 +0000494 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000495 return 0;
drh9a324642003-09-06 20:12:01 +0000496 }
497 addr = p->nOp;
drh7b746032009-06-26 12:15:22 +0000498 if( ALWAYS(nOp>0) ){
drh9a324642003-09-06 20:12:01 +0000499 int i;
drh905793e2004-02-21 13:31:09 +0000500 VdbeOpList const *pIn = aOp;
501 for(i=0; i<nOp; i++, pIn++){
502 int p2 = pIn->p2;
503 VdbeOp *pOut = &p->aOp[i+addr];
504 pOut->opcode = pIn->opcode;
505 pOut->p1 = pIn->p1;
drha6c2ed92009-11-14 23:22:23 +0000506 if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
drh8558cde2008-01-05 05:20:10 +0000507 pOut->p2 = addr + ADDR(p2);
508 }else{
509 pOut->p2 = p2;
510 }
drh24003452008-01-03 01:28:59 +0000511 pOut->p3 = pIn->p3;
512 pOut->p4type = P4_NOTUSED;
513 pOut->p4.p = 0;
514 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000515#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000516 pOut->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000517 if( sqlite3VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000519 }
520#endif
521 }
522 p->nOp += nOp;
523 }
524 return addr;
525}
526
527/*
528** Change the value of the P1 operand for a specific instruction.
529** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000530** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000531** few minor changes to the program.
532*/
drh88caeac2011-08-24 15:12:08 +0000533void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000534 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000535 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000536 p->aOp[addr].p1 = val;
537 }
538}
539
540/*
541** Change the value of the P2 operand for a specific instruction.
542** This routine is useful for setting a jump destination.
543*/
drh88caeac2011-08-24 15:12:08 +0000544void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000545 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000546 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000547 p->aOp[addr].p2 = val;
548 }
549}
550
drhd654be82005-09-20 17:42:23 +0000551/*
danielk19771f4aa332008-01-03 09:51:55 +0000552** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000553*/
drh88caeac2011-08-24 15:12:08 +0000554void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000555 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000556 if( ((u32)p->nOp)>addr ){
danielk1977207872a2008-01-03 07:54:23 +0000557 p->aOp[addr].p3 = val;
558 }
559}
560
561/*
drh35573352008-01-08 23:54:25 +0000562** Change the value of the P5 operand for the most recently
563** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000564*/
drh35573352008-01-08 23:54:25 +0000565void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
drh7b746032009-06-26 12:15:22 +0000566 assert( p!=0 );
567 if( p->aOp ){
drh35573352008-01-08 23:54:25 +0000568 assert( p->nOp>0 );
569 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000570 }
571}
572
573/*
drhf8875402006-03-17 13:56:34 +0000574** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000575** the address of the next instruction to be coded.
576*/
577void sqlite3VdbeJumpHere(Vdbe *p, int addr){
drh8c2cd5d2011-08-16 02:07:04 +0000578 assert( addr>=0 || p->db->mallocFailed );
579 if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
drhd654be82005-09-20 17:42:23 +0000580}
drhb38ad992005-09-16 00:27:01 +0000581
drhb7f6f682006-07-08 17:06:43 +0000582
583/*
584** If the input FuncDef structure is ephemeral, then free it. If
585** the FuncDef is not ephermal, then do nothing.
586*/
drh633e6d52008-07-28 19:34:53 +0000587static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drh7b746032009-06-26 12:15:22 +0000588 if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000589 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000590 }
591}
592
dand46def72010-07-24 11:28:28 +0000593static void vdbeFreeOpArray(sqlite3 *, Op *, int);
594
drhb38ad992005-09-16 00:27:01 +0000595/*
drh66a51672008-01-03 00:01:23 +0000596** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000597*/
drh633e6d52008-07-28 19:34:53 +0000598static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000599 if( p4 ){
dand46def72010-07-24 11:28:28 +0000600 assert( db );
drh66a51672008-01-03 00:01:23 +0000601 switch( p4type ){
602 case P4_REAL:
603 case P4_INT64:
drh66a51672008-01-03 00:01:23 +0000604 case P4_DYNAMIC:
605 case P4_KEYINFO:
drh0acb7e42008-06-25 00:12:41 +0000606 case P4_INTARRAY:
drh66a51672008-01-03 00:01:23 +0000607 case P4_KEYINFO_HANDOFF: {
drh633e6d52008-07-28 19:34:53 +0000608 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000609 break;
610 }
drhb9755982010-07-24 16:34:37 +0000611 case P4_MPRINTF: {
drh7043db92010-07-26 12:38:12 +0000612 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
drhb9755982010-07-24 16:34:37 +0000613 break;
614 }
drh66a51672008-01-03 00:01:23 +0000615 case P4_VDBEFUNC: {
drh0acb7e42008-06-25 00:12:41 +0000616 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
drh633e6d52008-07-28 19:34:53 +0000617 freeEphemeralFunction(db, pVdbeFunc->pFunc);
dand46def72010-07-24 11:28:28 +0000618 if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh633e6d52008-07-28 19:34:53 +0000619 sqlite3DbFree(db, pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000620 break;
621 }
drh66a51672008-01-03 00:01:23 +0000622 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000623 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000624 break;
625 }
drh66a51672008-01-03 00:01:23 +0000626 case P4_MEM: {
drhc176c272010-07-26 13:57:59 +0000627 if( db->pnBytesFreed==0 ){
628 sqlite3ValueFree((sqlite3_value*)p4);
629 }else{
drhf37c68e2010-07-26 14:20:06 +0000630 Mem *p = (Mem*)p4;
631 sqlite3DbFree(db, p->zMalloc);
632 sqlite3DbFree(db, p);
drhc176c272010-07-26 13:57:59 +0000633 }
drhac1733d2005-09-17 17:58:22 +0000634 break;
635 }
danielk1977595a5232009-07-24 17:58:53 +0000636 case P4_VTAB : {
dand46def72010-07-24 11:28:28 +0000637 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
danielk1977595a5232009-07-24 17:58:53 +0000638 break;
639 }
drhb38ad992005-09-16 00:27:01 +0000640 }
641 }
642}
643
dan65a7cd12009-09-01 12:16:01 +0000644/*
645** Free the space allocated for aOp and any p4 values allocated for the
646** opcodes contained within. If aOp is not NULL it is assumed to contain
647** nOp entries.
648*/
dan165921a2009-08-28 18:53:45 +0000649static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
650 if( aOp ){
651 Op *pOp;
652 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
653 freeP4(db, pOp->p4type, pOp->p4.p);
654#ifdef SQLITE_DEBUG
655 sqlite3DbFree(db, pOp->zComment);
656#endif
657 }
658 }
659 sqlite3DbFree(db, aOp);
660}
661
dan65a7cd12009-09-01 12:16:01 +0000662/*
dand19c9332010-07-26 12:05:17 +0000663** Link the SubProgram object passed as the second argument into the linked
664** list at Vdbe.pSubProgram. This list is used to delete all sub-program
665** objects when the VM is no longer required.
dan65a7cd12009-09-01 12:16:01 +0000666*/
dand19c9332010-07-26 12:05:17 +0000667void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
668 p->pNext = pVdbe->pProgram;
669 pVdbe->pProgram = p;
dan165921a2009-08-28 18:53:45 +0000670}
671
drh9a324642003-09-06 20:12:01 +0000672/*
drh48f2d3b2011-09-16 01:34:43 +0000673** Change the opcode at addr into OP_Noop
drhf8875402006-03-17 13:56:34 +0000674*/
drh48f2d3b2011-09-16 01:34:43 +0000675void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
drh7b746032009-06-26 12:15:22 +0000676 if( p->aOp ){
danielk197792d4d7a2007-05-04 12:05:56 +0000677 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000678 sqlite3 *db = p->db;
drh48f2d3b2011-09-16 01:34:43 +0000679 freeP4(db, pOp->p4type, pOp->p4.p);
680 memset(pOp, 0, sizeof(pOp[0]));
681 pOp->opcode = OP_Noop;
drhf8875402006-03-17 13:56:34 +0000682 }
683}
684
685/*
drh66a51672008-01-03 00:01:23 +0000686** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000687** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000688** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000689** few minor changes to the program.
690**
drh66a51672008-01-03 00:01:23 +0000691** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000692** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000693** A value of n==0 means copy bytes of zP4 up to and including the
694** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000695**
drh66a51672008-01-03 00:01:23 +0000696** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000697** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000698** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000699** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000700** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000701** caller should not free the allocation, it will be freed when the Vdbe is
702** finalized.
703**
drh66a51672008-01-03 00:01:23 +0000704** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000705** to a string or structure that is guaranteed to exist for the lifetime of
706** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000707**
drh66a51672008-01-03 00:01:23 +0000708** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000709*/
drh66a51672008-01-03 00:01:23 +0000710void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000711 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000712 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000713 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000714 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000715 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000716 if( p->aOp==0 || db->mallocFailed ){
danielk1977595a5232009-07-24 17:58:53 +0000717 if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
drh633e6d52008-07-28 19:34:53 +0000718 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000719 }
danielk1977d5d56522005-03-16 12:15:20 +0000720 return;
721 }
drh7b746032009-06-26 12:15:22 +0000722 assert( p->nOp>0 );
drh91fd4d42008-01-19 20:11:25 +0000723 assert( addr<p->nOp );
724 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000725 addr = p->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000726 }
727 pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000728 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000729 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000730 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000731 /* Note: this cast is safe, because the origin data point was an int
732 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000733 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000734 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000735 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000736 pOp->p4.p = 0;
737 pOp->p4type = P4_NOTUSED;
738 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000739 KeyInfo *pKeyInfo;
740 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000741
drh66a51672008-01-03 00:01:23 +0000742 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000743 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhb9755982010-07-24 16:34:37 +0000744 pKeyInfo = sqlite3DbMallocRaw(0, nByte);
danielk19772dca4ac2008-01-03 11:50:29 +0000745 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000746 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000747 u8 *aSortOrder;
drha378c562010-04-02 12:55:38 +0000748 memcpy((char*)pKeyInfo, zP4, nByte - nField);
drhfdd6e852005-12-16 01:06:16 +0000749 aSortOrder = pKeyInfo->aSortOrder;
750 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000751 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000752 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
753 }
drh66a51672008-01-03 00:01:23 +0000754 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000755 }else{
drh17435752007-08-16 04:30:38 +0000756 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000757 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000758 }
drh66a51672008-01-03 00:01:23 +0000759 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000760 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000761 pOp->p4type = P4_KEYINFO;
danielk1977595a5232009-07-24 17:58:53 +0000762 }else if( n==P4_VTAB ){
763 pOp->p4.p = (void*)zP4;
764 pOp->p4type = P4_VTAB;
765 sqlite3VtabLock((VTable *)zP4);
766 assert( ((VTable *)zP4)->db==p->db );
drh9a324642003-09-06 20:12:01 +0000767 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000768 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000769 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000770 }else{
drhea678832008-12-10 19:26:22 +0000771 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000772 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000773 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000774 }
775}
776
drhad6d9462004-09-19 02:15:24 +0000777#ifndef NDEBUG
778/*
drh16ee60f2008-06-20 18:13:25 +0000779** Change the comment on the the most recently coded instruction. Or
780** insert a No-op and add the comment to that new instruction. This
781** makes the code easier to read during debugging. None of this happens
782** in a production build.
drhad6d9462004-09-19 02:15:24 +0000783*/
784void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
785 va_list ap;
dan165921a2009-08-28 18:53:45 +0000786 if( !p ) return;
danielk197701256832007-04-18 14:24:32 +0000787 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000788 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000789 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000790 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000791 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000792 sqlite3DbFree(p->db, *pz);
drh8cc74322008-01-15 02:22:24 +0000793 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000794 va_end(ap);
795 }
drhad6d9462004-09-19 02:15:24 +0000796}
drh16ee60f2008-06-20 18:13:25 +0000797void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
798 va_list ap;
dan165921a2009-08-28 18:53:45 +0000799 if( !p ) return;
drh16ee60f2008-06-20 18:13:25 +0000800 sqlite3VdbeAddOp0(p, OP_Noop);
801 assert( p->nOp>0 || p->aOp==0 );
802 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
803 if( p->nOp ){
804 char **pz = &p->aOp[p->nOp-1].zComment;
805 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000806 sqlite3DbFree(p->db, *pz);
drh16ee60f2008-06-20 18:13:25 +0000807 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
808 va_end(ap);
809 }
810}
811#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000812
drh9a324642003-09-06 20:12:01 +0000813/*
drh20411ea2009-05-29 19:00:12 +0000814** Return the opcode for a given address. If the address is -1, then
815** return the most recently inserted opcode.
816**
817** If a memory allocation error has occurred prior to the calling of this
818** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
drhf83dc1e2010-06-03 12:09:52 +0000819** is readable but not writable, though it is cast to a writable value.
820** The return of a dummy opcode allows the call to continue functioning
821** after a OOM fault without having to check to see if the return from
822** this routine is a valid pointer. But because the dummy.opcode is 0,
823** dummy will never be written to. This is verified by code inspection and
824** by running with Valgrind.
drh37b89a02009-06-19 00:33:31 +0000825**
826** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
827** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
828** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
829** a new VDBE is created. So we are free to set addr to p->nOp-1 without
830** having to double-check to make sure that the result is non-negative. But
831** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
832** check the value of p->nOp-1 before continuing.
drh9a324642003-09-06 20:12:01 +0000833*/
danielk19774adee202004-05-08 08:23:19 +0000834VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drha0b75da2010-07-02 18:44:37 +0000835 /* C89 specifies that the constant "dummy" will be initialized to all
836 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
mistachkin0fe5f952011-09-14 18:19:08 +0000837 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
drh9a324642003-09-06 20:12:01 +0000838 assert( p->magic==VDBE_MAGIC_INIT );
drh37b89a02009-06-19 00:33:31 +0000839 if( addr<0 ){
840#ifdef SQLITE_OMIT_TRACE
drhf83dc1e2010-06-03 12:09:52 +0000841 if( p->nOp==0 ) return (VdbeOp*)&dummy;
drh37b89a02009-06-19 00:33:31 +0000842#endif
843 addr = p->nOp - 1;
844 }
drh17435752007-08-16 04:30:38 +0000845 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000846 if( p->db->mallocFailed ){
drhf83dc1e2010-06-03 12:09:52 +0000847 return (VdbeOp*)&dummy;
drh20411ea2009-05-29 19:00:12 +0000848 }else{
849 return &p->aOp[addr];
850 }
drh9a324642003-09-06 20:12:01 +0000851}
852
drhb7f91642004-10-31 02:22:47 +0000853#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
854 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000855/*
drh66a51672008-01-03 00:01:23 +0000856** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000857** Use zTemp for any required temporary buffer space.
858*/
drh66a51672008-01-03 00:01:23 +0000859static char *displayP4(Op *pOp, char *zTemp, int nTemp){
860 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000861 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000862 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000863 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000864 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000865 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000866 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000867 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +0000868 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +0000869 for(j=0; j<pKeyInfo->nField; j++){
870 CollSeq *pColl = pKeyInfo->aColl[j];
871 if( pColl ){
drhea678832008-12-10 19:26:22 +0000872 int n = sqlite3Strlen30(pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000873 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000874 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000875 break;
876 }
877 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000878 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000879 zTemp[i++] = '-';
880 }
drh5bb3eb92007-05-04 13:15:55 +0000881 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000882 i += n;
883 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000884 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000885 i += 4;
886 }
887 }
888 zTemp[i++] = ')';
889 zTemp[i] = 0;
890 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000891 break;
892 }
drh66a51672008-01-03 00:01:23 +0000893 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000894 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000895 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000896 break;
897 }
drh66a51672008-01-03 00:01:23 +0000898 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000899 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000900 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000901 break;
902 }
drh66a51672008-01-03 00:01:23 +0000903 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000904 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000905 break;
906 }
drh66a51672008-01-03 00:01:23 +0000907 case P4_INT32: {
908 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000909 break;
910 }
drh66a51672008-01-03 00:01:23 +0000911 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000912 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000913 break;
914 }
drh66a51672008-01-03 00:01:23 +0000915 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000916 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000917 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000918 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000919 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000920 }else if( pMem->flags & MEM_Int ){
921 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
922 }else if( pMem->flags & MEM_Real ){
923 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drh56016892009-08-25 14:24:04 +0000924 }else{
925 assert( pMem->flags & MEM_Blob );
926 zP4 = "(blob)";
drhd4e70eb2008-01-02 00:34:36 +0000927 }
drh598f1342007-10-23 15:39:45 +0000928 break;
929 }
drha967e882006-06-13 01:04:52 +0000930#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000931 case P4_VTAB: {
danielk1977595a5232009-07-24 17:58:53 +0000932 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
drh19146192006-06-26 19:10:32 +0000933 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000934 break;
935 }
936#endif
drh0acb7e42008-06-25 00:12:41 +0000937 case P4_INTARRAY: {
938 sqlite3_snprintf(nTemp, zTemp, "intarray");
939 break;
940 }
dan165921a2009-08-28 18:53:45 +0000941 case P4_SUBPROGRAM: {
942 sqlite3_snprintf(nTemp, zTemp, "program");
943 break;
944 }
drh4a6f3aa2011-08-28 00:19:26 +0000945 case P4_ADVANCE: {
946 zTemp[0] = 0;
947 break;
948 }
drhd3d39e92004-05-20 22:16:29 +0000949 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000950 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000951 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000952 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000953 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000954 }
955 }
956 }
drh66a51672008-01-03 00:01:23 +0000957 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000958 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000959}
drhb7f91642004-10-31 02:22:47 +0000960#endif
drhd3d39e92004-05-20 22:16:29 +0000961
drh900b31e2007-08-28 02:27:51 +0000962/*
drhd0679ed2007-08-28 22:24:34 +0000963** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
drh3ebaee92010-05-06 21:37:22 +0000964**
drhbdaec522011-04-04 00:14:43 +0000965** The prepared statements need to know in advance the complete set of
966** attached databases that they will be using. A mask of these databases
967** is maintained in p->btreeMask and is used for locking and other purposes.
drh900b31e2007-08-28 02:27:51 +0000968*/
drhfb982642007-08-30 01:19:59 +0000969void sqlite3VdbeUsesBtree(Vdbe *p, int i){
drhfcd71b62011-04-05 22:08:24 +0000970 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
danielk197700e13612008-11-17 19:18:54 +0000971 assert( i<(int)sizeof(p->btreeMask)*8 );
drhbdaec522011-04-04 00:14:43 +0000972 p->btreeMask |= ((yDbMask)1)<<i;
drhdc5b0472011-04-06 22:05:53 +0000973 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
974 p->lockMask |= ((yDbMask)1)<<i;
975 }
drh900b31e2007-08-28 02:27:51 +0000976}
977
drhe54e0512011-04-05 17:31:56 +0000978#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +0000979/*
980** If SQLite is compiled to support shared-cache mode and to be threadsafe,
981** this routine obtains the mutex associated with each BtShared structure
982** that may be accessed by the VM passed as an argument. In doing so it also
983** sets the BtShared.db member of each of the BtShared structures, ensuring
984** that the correct busy-handler callback is invoked if required.
985**
986** If SQLite is not threadsafe but does support shared-cache mode, then
987** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
988** of all of BtShared structures accessible via the database handle
989** associated with the VM.
990**
991** If SQLite is not threadsafe and does not support shared-cache mode, this
992** function is a no-op.
993**
994** The p->btreeMask field is a bitmask of all btrees that the prepared
995** statement p will ever use. Let N be the number of bits in p->btreeMask
996** corresponding to btrees that use shared cache. Then the runtime of
997** this routine is N*N. But as N is rarely more than 1, this should not
998** be a problem.
999*/
1000void sqlite3VdbeEnter(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001001 int i;
1002 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001003 sqlite3 *db;
1004 Db *aDb;
1005 int nDb;
1006 if( p->lockMask==0 ) return; /* The common case */
1007 db = p->db;
1008 aDb = db->aDb;
1009 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001010 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001011 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001012 sqlite3BtreeEnter(aDb[i].pBt);
1013 }
1014 }
drhbdaec522011-04-04 00:14:43 +00001015}
drhe54e0512011-04-05 17:31:56 +00001016#endif
drhbdaec522011-04-04 00:14:43 +00001017
drhe54e0512011-04-05 17:31:56 +00001018#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001019/*
1020** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1021*/
1022void sqlite3VdbeLeave(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001023 int i;
1024 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001025 sqlite3 *db;
1026 Db *aDb;
1027 int nDb;
1028 if( p->lockMask==0 ) return; /* The common case */
1029 db = p->db;
1030 aDb = db->aDb;
1031 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001032 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001033 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001034 sqlite3BtreeLeave(aDb[i].pBt);
1035 }
1036 }
drhbdaec522011-04-04 00:14:43 +00001037}
drhbdaec522011-04-04 00:14:43 +00001038#endif
drhd3d39e92004-05-20 22:16:29 +00001039
danielk19778b60e0f2005-01-12 09:10:39 +00001040#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +00001041/*
1042** Print a single opcode. This routine is used for debugging only.
1043*/
danielk19774adee202004-05-08 08:23:19 +00001044void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +00001045 char *zP4;
drhd3d39e92004-05-20 22:16:29 +00001046 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +00001047 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +00001048 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +00001049 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +00001050 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +00001051 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
1052#ifdef SQLITE_DEBUG
1053 pOp->zComment ? pOp->zComment : ""
1054#else
1055 ""
1056#endif
1057 );
drh9a324642003-09-06 20:12:01 +00001058 fflush(pOut);
1059}
1060#endif
1061
1062/*
drh76ff3a02004-09-24 22:32:30 +00001063** Release an array of N Mem elements
1064*/
drhc890fec2008-08-01 20:10:08 +00001065static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +00001066 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +00001067 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +00001068 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +00001069 u8 malloc_failed = db->mallocFailed;
dand46def72010-07-24 11:28:28 +00001070 if( db->pnBytesFreed ){
1071 for(pEnd=&p[N]; p<pEnd; p++){
1072 sqlite3DbFree(db, p->zMalloc);
1073 }
drhc176c272010-07-26 13:57:59 +00001074 return;
1075 }
danielk1977e972e032008-09-19 18:32:26 +00001076 for(pEnd=&p[N]; p<pEnd; p++){
1077 assert( (&p[1])==pEnd || p[0].db==p[1].db );
1078
1079 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1080 ** that takes advantage of the fact that the memory cell value is
1081 ** being set to NULL after releasing any dynamic resources.
1082 **
1083 ** The justification for duplicating code is that according to
1084 ** callgrind, this causes a certain test case to hit the CPU 4.7
1085 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1086 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1087 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1088 ** with no indexes using a single prepared INSERT statement, bind()
1089 ** and reset(). Inserts are grouped into a transaction.
1090 */
dan165921a2009-08-28 18:53:45 +00001091 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001092 sqlite3VdbeMemRelease(p);
1093 }else if( p->zMalloc ){
1094 sqlite3DbFree(db, p->zMalloc);
1095 p->zMalloc = 0;
1096 }
1097
danielk19775f096132008-03-28 15:44:09 +00001098 p->flags = MEM_Null;
drh76ff3a02004-09-24 22:32:30 +00001099 }
danielk1977a7a8e142008-02-13 18:25:27 +00001100 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001101 }
1102}
1103
dan65a7cd12009-09-01 12:16:01 +00001104/*
1105** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1106** allocated by the OP_Program opcode in sqlite3VdbeExec().
1107*/
dan165921a2009-08-28 18:53:45 +00001108void sqlite3VdbeFrameDelete(VdbeFrame *p){
1109 int i;
1110 Mem *aMem = VdbeFrameMem(p);
1111 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1112 for(i=0; i<p->nChildCsr; i++){
1113 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1114 }
1115 releaseMemArray(aMem, p->nChildMem);
1116 sqlite3DbFree(p->v->db, p);
1117}
1118
drhb7f91642004-10-31 02:22:47 +00001119#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001120/*
drh9a324642003-09-06 20:12:01 +00001121** Give a listing of the program in the virtual machine.
1122**
danielk19774adee202004-05-08 08:23:19 +00001123** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001124** running the code, it invokes the callback once for each instruction.
1125** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001126**
1127** When p->explain==1, each instruction is listed. When
1128** p->explain==2, only OP_Explain instructions are listed and these
1129** are shown in a different format. p->explain==2 is used to implement
1130** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001131**
1132** When p->explain==1, first the main program is listed, then each of
1133** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001134*/
danielk19774adee202004-05-08 08:23:19 +00001135int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001136 Vdbe *p /* The VDBE */
1137){
drh5cfa5842009-12-31 20:35:08 +00001138 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001139 int nSub = 0; /* Number of sub-vdbes seen so far */
1140 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001141 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1142 sqlite3 *db = p->db; /* The database connection */
1143 int i; /* Loop counter */
1144 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001145 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001146
drh9a324642003-09-06 20:12:01 +00001147 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001148 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001149 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001150
drh9cbf3422008-01-17 16:22:13 +00001151 /* Even though this opcode does not use dynamic strings for
1152 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001153 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001154 */
dan165921a2009-08-28 18:53:45 +00001155 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001156 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001157
danielk19776c359f02008-11-21 16:58:03 +00001158 if( p->rc==SQLITE_NOMEM ){
1159 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1160 ** sqlite3_column_text16() failed. */
1161 db->mallocFailed = 1;
1162 return SQLITE_ERROR;
1163 }
1164
drh5cfa5842009-12-31 20:35:08 +00001165 /* When the number of output rows reaches nRow, that means the
1166 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1167 ** nRow is the sum of the number of rows in the main program, plus
1168 ** the sum of the number of rows in all trigger subprograms encountered
1169 ** so far. The nRow value will increase as new trigger subprograms are
1170 ** encountered, but p->pc will eventually catch up to nRow.
1171 */
dan165921a2009-08-28 18:53:45 +00001172 nRow = p->nOp;
1173 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001174 /* The first 8 memory cells are used for the result set. So we will
1175 ** commandeer the 9th cell to use as storage for an array of pointers
1176 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1177 ** cells. */
1178 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001179 pSub = &p->aMem[9];
1180 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001181 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1182 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001183 nSub = pSub->n/sizeof(Vdbe*);
1184 apSub = (SubProgram **)pSub->z;
1185 }
1186 for(i=0; i<nSub; i++){
1187 nRow += apSub[i]->nOp;
1188 }
1189 }
1190
drhecc92422005-09-10 16:46:12 +00001191 do{
1192 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001193 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1194 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001195 p->rc = SQLITE_OK;
1196 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001197 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001198 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001199 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001200 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001201 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001202 char *z;
dan165921a2009-08-28 18:53:45 +00001203 Op *pOp;
1204 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001205 /* The output line number is small enough that we are still in the
1206 ** main program. */
dan165921a2009-08-28 18:53:45 +00001207 pOp = &p->aOp[i];
1208 }else{
drh5cfa5842009-12-31 20:35:08 +00001209 /* We are currently listing subprograms. Figure out which one and
1210 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001211 int j;
1212 i -= p->nOp;
1213 for(j=0; i>=apSub[j]->nOp; j++){
1214 i -= apSub[j]->nOp;
1215 }
1216 pOp = &apSub[j]->aOp[i];
1217 }
danielk19770d78bae2008-01-03 07:09:48 +00001218 if( p->explain==1 ){
1219 pMem->flags = MEM_Int;
1220 pMem->type = SQLITE_INTEGER;
1221 pMem->u.i = i; /* Program counter */
1222 pMem++;
1223
1224 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
1225 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
1226 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001227 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001228 pMem->type = SQLITE_TEXT;
1229 pMem->enc = SQLITE_UTF8;
1230 pMem++;
dan165921a2009-08-28 18:53:45 +00001231
drh5cfa5842009-12-31 20:35:08 +00001232 /* When an OP_Program opcode is encounter (the only opcode that has
1233 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1234 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1235 ** has not already been seen.
1236 */
dan165921a2009-08-28 18:53:45 +00001237 if( pOp->p4type==P4_SUBPROGRAM ){
1238 int nByte = (nSub+1)*sizeof(SubProgram*);
1239 int j;
1240 for(j=0; j<nSub; j++){
1241 if( apSub[j]==pOp->p4.pProgram ) break;
1242 }
1243 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
1244 apSub = (SubProgram **)pSub->z;
1245 apSub[nSub++] = pOp->p4.pProgram;
1246 pSub->flags |= MEM_Blob;
1247 pSub->n = nSub*sizeof(SubProgram*);
1248 }
1249 }
danielk19770d78bae2008-01-03 07:09:48 +00001250 }
drheb2e1762004-05-27 01:53:56 +00001251
1252 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001253 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +00001254 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +00001255 pMem++;
1256
1257 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001258 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +00001259 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +00001260 pMem++;
1261
dan2ce22452010-11-08 19:01:16 +00001262 pMem->flags = MEM_Int;
1263 pMem->u.i = pOp->p3; /* P3 */
1264 pMem->type = SQLITE_INTEGER;
1265 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001266
danielk1977a7a8e142008-02-13 18:25:27 +00001267 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001268 assert( p->db->mallocFailed );
1269 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001270 }
1271 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
1272 z = displayP4(pOp, pMem->z, 32);
1273 if( z!=pMem->z ){
1274 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
1275 }else{
1276 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001277 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001278 pMem->enc = SQLITE_UTF8;
1279 }
drh9c054832004-05-31 18:51:57 +00001280 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +00001281 pMem++;
drheb2e1762004-05-27 01:53:56 +00001282
danielk19770d78bae2008-01-03 07:09:48 +00001283 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001284 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001285 assert( p->db->mallocFailed );
1286 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001287 }
1288 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001289 pMem->n = 2;
1290 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +00001291 pMem->type = SQLITE_TEXT;
1292 pMem->enc = SQLITE_UTF8;
1293 pMem++;
1294
drhaa9b8962008-01-08 02:57:55 +00001295#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +00001296 if( pOp->zComment ){
1297 pMem->flags = MEM_Str|MEM_Term;
1298 pMem->z = pOp->zComment;
drhea678832008-12-10 19:26:22 +00001299 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001300 pMem->enc = SQLITE_UTF8;
danielk19771e522b42008-09-16 09:09:19 +00001301 pMem->type = SQLITE_TEXT;
drh52391cb2008-02-14 23:44:13 +00001302 }else
drhaa9b8962008-01-08 02:57:55 +00001303#endif
drh52391cb2008-02-14 23:44:13 +00001304 {
1305 pMem->flags = MEM_Null; /* Comment */
1306 pMem->type = SQLITE_NULL;
1307 }
danielk19770d78bae2008-01-03 07:09:48 +00001308 }
1309
dan2ce22452010-11-08 19:01:16 +00001310 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001311 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001312 p->rc = SQLITE_OK;
1313 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001314 }
drh826fb5a2004-02-14 23:59:57 +00001315 return rc;
drh9a324642003-09-06 20:12:01 +00001316}
drhb7f91642004-10-31 02:22:47 +00001317#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001318
drh7c4ac0c2007-04-05 11:25:58 +00001319#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001320/*
drh3f7d4e42004-07-24 14:35:58 +00001321** Print the SQL that was used to generate a VDBE program.
1322*/
1323void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +00001324 int nOp = p->nOp;
1325 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +00001326 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001327 pOp = &p->aOp[0];
1328 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +00001329 const char *z = pOp->p4.z;
danielk197778ca0e72009-01-20 16:53:39 +00001330 while( sqlite3Isspace(*z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +00001331 printf("SQL: [%s]\n", z);
1332 }
drh3f7d4e42004-07-24 14:35:58 +00001333}
drh7c4ac0c2007-04-05 11:25:58 +00001334#endif
drh3f7d4e42004-07-24 14:35:58 +00001335
drh602c2372007-03-01 00:29:13 +00001336#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1337/*
1338** Print an IOTRACE message showing SQL content.
1339*/
1340void sqlite3VdbeIOTraceSql(Vdbe *p){
1341 int nOp = p->nOp;
1342 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001343 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001344 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001345 pOp = &p->aOp[0];
1346 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001347 int i, j;
drh00a18e42007-08-13 11:10:34 +00001348 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001349 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001350 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001351 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001352 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001353 if( z[i-1]!=' ' ){
1354 z[j++] = ' ';
1355 }
1356 }else{
1357 z[j++] = z[i];
1358 }
1359 }
1360 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001361 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001362 }
1363}
1364#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1365
drhb2771ce2009-02-20 01:28:59 +00001366/*
drh4800b2e2009-12-08 15:35:22 +00001367** Allocate space from a fixed size buffer and return a pointer to
1368** that space. If insufficient space is available, return NULL.
1369**
1370** The pBuf parameter is the initial value of a pointer which will
1371** receive the new memory. pBuf is normally NULL. If pBuf is not
1372** NULL, it means that memory space has already been allocated and that
1373** this routine should not allocate any new memory. When pBuf is not
1374** NULL simply return pBuf. Only allocate new memory space when pBuf
1375** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001376**
1377** nByte is the number of bytes of space needed.
1378**
drh19875c82009-12-08 19:58:19 +00001379** *ppFrom points to available space and pEnd points to the end of the
1380** available space. When space is allocated, *ppFrom is advanced past
1381** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001382**
1383** *pnByte is a counter of the number of bytes of space that have failed
1384** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001385** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001386*/
drh4800b2e2009-12-08 15:35:22 +00001387static void *allocSpace(
1388 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001389 int nByte, /* Number of bytes to allocate */
1390 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001391 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001392 int *pnByte /* If allocation cannot be made, increment *pnByte */
1393){
drhea598cb2009-04-05 12:22:08 +00001394 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001395 if( pBuf ) return pBuf;
1396 nByte = ROUND8(nByte);
1397 if( &(*ppFrom)[nByte] <= pEnd ){
1398 pBuf = (void*)*ppFrom;
1399 *ppFrom += nByte;
1400 }else{
1401 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001402 }
drh4800b2e2009-12-08 15:35:22 +00001403 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001404}
drh602c2372007-03-01 00:29:13 +00001405
drh3f7d4e42004-07-24 14:35:58 +00001406/*
drh124c0b42011-06-01 18:15:55 +00001407** Rewind the VDBE back to the beginning in preparation for
1408** running it.
drh9a324642003-09-06 20:12:01 +00001409*/
drh124c0b42011-06-01 18:15:55 +00001410void sqlite3VdbeRewind(Vdbe *p){
1411#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1412 int i;
1413#endif
drh9a324642003-09-06 20:12:01 +00001414 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001415 assert( p->magic==VDBE_MAGIC_INIT );
1416
drhc16a03b2004-09-15 13:38:10 +00001417 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001418 */
drhc16a03b2004-09-15 13:38:10 +00001419 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001420
danielk197700e13612008-11-17 19:18:54 +00001421 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001422 p->magic = VDBE_MAGIC_RUN;
1423
drh124c0b42011-06-01 18:15:55 +00001424#ifdef SQLITE_DEBUG
1425 for(i=1; i<p->nMem; i++){
1426 assert( p->aMem[i].db==p->db );
1427 }
1428#endif
1429 p->pc = -1;
1430 p->rc = SQLITE_OK;
1431 p->errorAction = OE_Abort;
1432 p->magic = VDBE_MAGIC_RUN;
1433 p->nChange = 0;
1434 p->cacheCtr = 1;
1435 p->minWriteFileFormat = 255;
1436 p->iStatement = 0;
1437 p->nFkConstraint = 0;
1438#ifdef VDBE_PROFILE
1439 for(i=0; i<p->nOp; i++){
1440 p->aOp[i].cnt = 0;
1441 p->aOp[i].cycles = 0;
1442 }
1443#endif
1444}
1445
1446/*
1447** Prepare a virtual machine for execution for the first time after
1448** creating the virtual machine. This involves things such
1449** as allocating stack space and initializing the program counter.
1450** After the VDBE has be prepped, it can be executed by one or more
1451** calls to sqlite3VdbeExec().
1452**
1453** This function may be called exact once on a each virtual machine.
1454** After this routine is called the VM has been "packaged" and is ready
1455** to run. After this routine is called, futher calls to
1456** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1457** the Vdbe from the Parse object that helped generate it so that the
1458** the Vdbe becomes an independent entity and the Parse object can be
1459** destroyed.
1460**
1461** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1462** to its initial state after it has been run.
1463*/
1464void sqlite3VdbeMakeReady(
1465 Vdbe *p, /* The VDBE */
1466 Parse *pParse /* Parsing context */
1467){
1468 sqlite3 *db; /* The database connection */
1469 int nVar; /* Number of parameters */
1470 int nMem; /* Number of VM memory registers */
1471 int nCursor; /* Number of cursors required */
1472 int nArg; /* Number of arguments in subprograms */
1473 int n; /* Loop counter */
1474 u8 *zCsr; /* Memory available for allocation */
1475 u8 *zEnd; /* First byte past allocated memory */
1476 int nByte; /* How much extra memory is needed */
1477
1478 assert( p!=0 );
1479 assert( p->nOp>0 );
1480 assert( pParse!=0 );
1481 assert( p->magic==VDBE_MAGIC_INIT );
1482 db = p->db;
1483 assert( db->mallocFailed==0 );
1484 nVar = pParse->nVar;
1485 nMem = pParse->nMem;
1486 nCursor = pParse->nTab;
1487 nArg = pParse->nMaxArg;
1488
danielk1977cd3e8f72008-03-25 09:47:35 +00001489 /* For each cursor required, also allocate a memory cell. Memory
1490 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1491 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001492 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001493 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1494 ** stores the blob of memory associated with cursor 1, etc.
1495 **
1496 ** See also: allocateCursor().
1497 */
1498 nMem += nCursor;
1499
danielk19776ab3a2e2009-02-19 14:39:25 +00001500 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001501 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001502 */
drh124c0b42011-06-01 18:15:55 +00001503 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1504 zEnd = (u8*)&p->aOp[p->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001505
drh124c0b42011-06-01 18:15:55 +00001506 resolveP2Values(p, &nArg);
1507 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1508 if( pParse->explain && nMem<10 ){
1509 nMem = 10;
1510 }
1511 memset(zCsr, 0, zEnd-zCsr);
1512 zCsr += (zCsr - (u8*)0)&7;
1513 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001514 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001515
1516 /* Memory for registers, parameters, cursor, etc, is allocated in two
1517 ** passes. On the first pass, we try to reuse unused space at the
1518 ** end of the opcode array. If we are unable to satisfy all memory
1519 ** requirements by reusing the opcode array tail, then the second
1520 ** pass will fill in the rest using a fresh allocation.
1521 **
1522 ** This two-pass approach that reuses as much memory as possible from
1523 ** the leftover space at the end of the opcode array can significantly
1524 ** reduce the amount of memory held by a prepared statement.
1525 */
1526 do {
1527 nByte = 0;
1528 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1529 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1530 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1531 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1532 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1533 &zCsr, zEnd, &nByte);
1534 if( nByte ){
1535 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001536 }
drh124c0b42011-06-01 18:15:55 +00001537 zCsr = p->pFree;
1538 zEnd = &zCsr[nByte];
1539 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001540
drh124c0b42011-06-01 18:15:55 +00001541 p->nCursor = (u16)nCursor;
1542 if( p->aVar ){
1543 p->nVar = (ynVar)nVar;
1544 for(n=0; n<nVar; n++){
1545 p->aVar[n].flags = MEM_Null;
1546 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001547 }
drh82a48512003-09-06 22:45:20 +00001548 }
drh124c0b42011-06-01 18:15:55 +00001549 if( p->azVar ){
1550 p->nzVar = pParse->nzVar;
1551 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1552 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001553 }
drh124c0b42011-06-01 18:15:55 +00001554 if( p->aMem ){
1555 p->aMem--; /* aMem[] goes from 1..nMem */
1556 p->nMem = nMem; /* not from 0..nMem-1 */
1557 for(n=1; n<=nMem; n++){
1558 p->aMem[n].flags = MEM_Null;
1559 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001560 }
drh9a324642003-09-06 20:12:01 +00001561 }
drh124c0b42011-06-01 18:15:55 +00001562 p->explain = pParse->explain;
1563 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001564}
1565
drh9a324642003-09-06 20:12:01 +00001566/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001567** Close a VDBE cursor and release all the resources that cursor
1568** happens to hold.
drh9a324642003-09-06 20:12:01 +00001569*/
drhdfe88ec2008-11-03 20:55:06 +00001570void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001571 if( pCx==0 ){
1572 return;
1573 }
dana20fde62011-07-12 14:28:05 +00001574 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001575 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001576 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001577 /* The pCx->pCursor will be close automatically, if it exists, by
1578 ** the call above. */
1579 }else if( pCx->pCursor ){
1580 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001581 }
drh9eff6162006-06-12 21:59:13 +00001582#ifndef SQLITE_OMIT_VIRTUALTABLE
1583 if( pCx->pVtabCursor ){
1584 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001585 const sqlite3_module *pModule = pCx->pModule;
1586 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001587 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001588 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001589 }
1590#endif
drh9a324642003-09-06 20:12:01 +00001591}
1592
dan65a7cd12009-09-01 12:16:01 +00001593/*
1594** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1595** is used, for example, when a trigger sub-program is halted to restore
1596** control to the main program.
1597*/
dan165921a2009-08-28 18:53:45 +00001598int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1599 Vdbe *v = pFrame->v;
1600 v->aOp = pFrame->aOp;
1601 v->nOp = pFrame->nOp;
1602 v->aMem = pFrame->aMem;
1603 v->nMem = pFrame->nMem;
1604 v->apCsr = pFrame->apCsr;
1605 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001606 v->db->lastRowid = pFrame->lastRowid;
1607 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001608 return pFrame->pc;
1609}
1610
drh9a324642003-09-06 20:12:01 +00001611/*
drh5f82e3c2009-07-06 00:44:08 +00001612** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001613**
1614** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1615** cell array. This is necessary as the memory cell array may contain
1616** pointers to VdbeFrame objects, which may in turn contain pointers to
1617** open cursors.
drh9a324642003-09-06 20:12:01 +00001618*/
drh5f82e3c2009-07-06 00:44:08 +00001619static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001620 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001621 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001622 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1623 sqlite3VdbeFrameRestore(pFrame);
1624 }
1625 p->pFrame = 0;
1626 p->nFrame = 0;
1627
dan523a0872009-08-31 05:23:32 +00001628 if( p->apCsr ){
1629 int i;
1630 for(i=0; i<p->nCursor; i++){
1631 VdbeCursor *pC = p->apCsr[i];
1632 if( pC ){
1633 sqlite3VdbeFreeCursor(p, pC);
1634 p->apCsr[i] = 0;
1635 }
danielk1977be718892006-06-23 08:05:19 +00001636 }
drh9a324642003-09-06 20:12:01 +00001637 }
dan523a0872009-08-31 05:23:32 +00001638 if( p->aMem ){
1639 releaseMemArray(&p->aMem[1], p->nMem);
1640 }
dan27106572010-12-01 08:04:47 +00001641 while( p->pDelFrame ){
1642 VdbeFrame *pDel = p->pDelFrame;
1643 p->pDelFrame = pDel->pParent;
1644 sqlite3VdbeFrameDelete(pDel);
1645 }
drh9a324642003-09-06 20:12:01 +00001646}
1647
1648/*
drh9a324642003-09-06 20:12:01 +00001649** Clean up the VM after execution.
1650**
1651** This routine will automatically close any cursors, lists, and/or
1652** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001653** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001654*/
drhc890fec2008-08-01 20:10:08 +00001655static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001656 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001657
1658#ifdef SQLITE_DEBUG
1659 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1660 ** Vdbe.aMem[] arrays have already been cleaned up. */
1661 int i;
dan523a0872009-08-31 05:23:32 +00001662 for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
1663 for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
dan165921a2009-08-28 18:53:45 +00001664#endif
1665
drh633e6d52008-07-28 19:34:53 +00001666 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001667 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001668 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001669}
1670
1671/*
danielk197722322fd2004-05-25 23:35:17 +00001672** Set the number of result columns that will be returned by this SQL
1673** statement. This is now set at compile time, rather than during
1674** execution of the vdbe program so that sqlite3_column_count() can
1675** be called on an SQL statement before sqlite3_step().
1676*/
1677void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001678 Mem *pColName;
1679 int n;
drh633e6d52008-07-28 19:34:53 +00001680 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001681
drhc890fec2008-08-01 20:10:08 +00001682 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001683 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001684 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001685 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001686 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001687 if( p->aColName==0 ) return;
1688 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001689 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001690 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001691 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001692 }
danielk197722322fd2004-05-25 23:35:17 +00001693}
1694
1695/*
danielk19773cf86062004-05-26 10:11:05 +00001696** Set the name of the idx'th column to be returned by the SQL statement.
1697** zName must be a pointer to a nul terminated string.
1698**
1699** This call must be made after a call to sqlite3VdbeSetNumCols().
1700**
danielk197710fb7492008-10-31 10:53:22 +00001701** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1702** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1703** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001704*/
danielk197710fb7492008-10-31 10:53:22 +00001705int sqlite3VdbeSetColName(
1706 Vdbe *p, /* Vdbe being configured */
1707 int idx, /* Index of column zName applies to */
1708 int var, /* One of the COLNAME_* constants */
1709 const char *zName, /* Pointer to buffer containing name */
1710 void (*xDel)(void*) /* Memory management strategy for zName */
1711){
danielk19773cf86062004-05-26 10:11:05 +00001712 int rc;
1713 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001714 assert( idx<p->nResColumn );
1715 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001716 if( p->db->mallocFailed ){
1717 assert( !zName || xDel!=SQLITE_DYNAMIC );
1718 return SQLITE_NOMEM;
1719 }
drh76ff3a02004-09-24 22:32:30 +00001720 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001721 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001722 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001723 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001724 return rc;
1725}
1726
danielk197713adf8a2004-06-03 16:08:41 +00001727/*
1728** A read or write transaction may or may not be active on database handle
1729** db. If a transaction is active, commit it. If there is a
1730** write-transaction spanning more than one database file, this routine
1731** takes care of the master journal trickery.
1732*/
danielk19773e3a84d2008-08-01 17:37:40 +00001733static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001734 int i;
1735 int nTrans = 0; /* Number of databases with an active write-transaction */
1736 int rc = SQLITE_OK;
1737 int needXcommit = 0;
1738
shane36840fd2009-06-26 16:32:13 +00001739#ifdef SQLITE_OMIT_VIRTUALTABLE
1740 /* With this option, sqlite3VtabSync() is defined to be simply
1741 ** SQLITE_OK so p is not used.
1742 */
1743 UNUSED_PARAMETER(p);
1744#endif
1745
danielk19775bd270b2006-07-25 15:14:52 +00001746 /* Before doing anything else, call the xSync() callback for any
1747 ** virtual module tables written in this transaction. This has to
1748 ** be done before determining whether a master journal file is
1749 ** required, as an xSync() callback may add an attached database
1750 ** to the transaction.
1751 */
danielk19773e3a84d2008-08-01 17:37:40 +00001752 rc = sqlite3VtabSync(db, &p->zErrMsg);
danielk19775bd270b2006-07-25 15:14:52 +00001753
1754 /* This loop determines (a) if the commit hook should be invoked and
1755 ** (b) how many database files have open write transactions, not
1756 ** including the temp database. (b) is important because if more than
1757 ** one database file has an open write transaction, a master journal
1758 ** file is required for an atomic commit.
1759 */
drhabfb62f2010-07-30 11:20:35 +00001760 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001761 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001762 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001763 needXcommit = 1;
1764 if( i!=1 ) nTrans++;
drhabfb62f2010-07-30 11:20:35 +00001765 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
danielk197713adf8a2004-06-03 16:08:41 +00001766 }
1767 }
drhabfb62f2010-07-30 11:20:35 +00001768 if( rc!=SQLITE_OK ){
1769 return rc;
1770 }
danielk197713adf8a2004-06-03 16:08:41 +00001771
1772 /* If there are any write-transactions at all, invoke the commit hook */
1773 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001774 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001775 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001776 return SQLITE_CONSTRAINT;
1777 }
1778 }
1779
danielk197740b38dc2004-06-26 08:38:24 +00001780 /* The simple case - no more than one database file (not counting the
1781 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001782 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001783 **
danielk197740b38dc2004-06-26 08:38:24 +00001784 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001785 ** string, it means the main database is :memory: or a temp file. In
1786 ** that case we do not support atomic multi-file commits, so use the
1787 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001788 */
drhea678832008-12-10 19:26:22 +00001789 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1790 || nTrans<=1
1791 ){
danielk197704103022009-02-03 16:51:24 +00001792 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001793 Btree *pBt = db->aDb[i].pBt;
1794 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001795 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001796 }
1797 }
1798
drh80e35f42007-03-30 14:06:34 +00001799 /* Do the commit only if all databases successfully complete phase 1.
1800 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1801 ** IO error while deleting or truncating a journal file. It is unlikely,
1802 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001803 */
1804 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1805 Btree *pBt = db->aDb[i].pBt;
1806 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001807 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001808 }
danielk1977979f38e2007-03-27 16:19:51 +00001809 }
1810 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001811 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001812 }
1813 }
1814
1815 /* The complex case - There is a multi-file write-transaction active.
1816 ** This requires a master journal file to ensure the transaction is
1817 ** committed atomicly.
1818 */
danielk197744ee5bf2005-05-27 09:41:12 +00001819#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001820 else{
danielk1977b4b47412007-08-17 15:53:36 +00001821 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001822 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001823 char *zMaster = 0; /* File-name for the master journal */
1824 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001825 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001826 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001827 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001828
1829 /* Select a master journal file name */
1830 do {
drhdc5ea5c2008-12-10 17:19:59 +00001831 u32 iRandom;
drh633e6d52008-07-28 19:34:53 +00001832 sqlite3DbFree(db, zMaster);
drhdc5ea5c2008-12-10 17:19:59 +00001833 sqlite3_randomness(sizeof(iRandom), &iRandom);
1834 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001835 if( !zMaster ){
1836 return SQLITE_NOMEM;
1837 }
drh81cc5162011-05-17 20:36:21 +00001838 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00001839 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1840 }while( rc==SQLITE_OK && res );
1841 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001842 /* Open the master journal. */
1843 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1844 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1845 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1846 );
1847 }
danielk197713adf8a2004-06-03 16:08:41 +00001848 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001849 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001850 return rc;
1851 }
1852
1853 /* Write the name of each database file in the transaction into the new
1854 ** master journal file. If an error occurs at this point close
1855 ** and delete the master journal file. All the individual journal files
1856 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001857 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001858 */
danielk19771e536952007-08-16 10:09:01 +00001859 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001860 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001861 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001862 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00001863 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00001864 continue; /* Ignore TEMP and :memory: databases */
1865 }
drh8c96a6e2010-08-31 01:09:15 +00001866 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00001867 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1868 needSync = 1;
1869 }
drhea678832008-12-10 19:26:22 +00001870 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
1871 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001872 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001873 sqlite3OsCloseFree(pMaster);
1874 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001875 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001876 return rc;
1877 }
1878 }
1879 }
1880
danielk19779663b8f2007-08-24 11:52:28 +00001881 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1882 ** flag is set this is not required.
1883 */
danielk1977bea2a942009-01-20 17:06:27 +00001884 if( needSync
1885 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
1886 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
1887 ){
danielk1977fee2d252007-08-18 10:59:19 +00001888 sqlite3OsCloseFree(pMaster);
1889 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001890 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001891 return rc;
1892 }
drhc9e06862004-06-09 20:03:08 +00001893
danielk197713adf8a2004-06-03 16:08:41 +00001894 /* Sync all the db files involved in the transaction. The same call
1895 ** sets the master journal pointer in each individual journal. If
1896 ** an error occurs here, do not delete the master journal file.
1897 **
drh80e35f42007-03-30 14:06:34 +00001898 ** If the error occurs during the first call to
1899 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1900 ** master journal file will be orphaned. But we cannot delete it,
1901 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00001902 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00001903 */
danielk19775bd270b2006-07-25 15:14:52 +00001904 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001905 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001906 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001907 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001908 }
1909 }
danielk1977fee2d252007-08-18 10:59:19 +00001910 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00001911 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00001912 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001913 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001914 return rc;
1915 }
danielk197713adf8a2004-06-03 16:08:41 +00001916
danielk1977962398d2004-06-14 09:35:16 +00001917 /* Delete the master journal file. This commits the transaction. After
1918 ** doing this the directory is synced again before any individual
1919 ** transaction files are deleted.
1920 */
danielk1977fee2d252007-08-18 10:59:19 +00001921 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00001922 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00001923 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001924 if( rc ){
1925 return rc;
1926 }
danielk197713adf8a2004-06-03 16:08:41 +00001927
1928 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001929 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1930 ** deleting or truncating journals. If something goes wrong while
1931 ** this is happening we don't really care. The integrity of the
1932 ** transaction is already guaranteed, but some stray 'cold' journals
1933 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001934 */
danielk1977979f38e2007-03-27 16:19:51 +00001935 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001936 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001937 for(i=0; i<db->nDb; i++){
1938 Btree *pBt = db->aDb[i].pBt;
1939 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001940 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00001941 }
1942 }
danielk19772d1d86f2008-06-20 14:59:51 +00001943 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001944 enable_simulated_io_errors();
1945
danielk1977f9e7dda2006-06-16 16:08:53 +00001946 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001947 }
danielk197744ee5bf2005-05-27 09:41:12 +00001948#endif
danielk1977026d2702004-06-14 13:14:59 +00001949
drh2ac3ee92004-06-07 16:27:46 +00001950 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001951}
1952
danielk19771d850a72004-05-31 08:26:49 +00001953/*
1954** This routine checks that the sqlite3.activeVdbeCnt count variable
1955** matches the number of vdbe's in the list sqlite3.pVdbe that are
1956** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001957** This is an internal self-check only - it is not an essential processing
1958** step.
danielk19771d850a72004-05-31 08:26:49 +00001959**
1960** This is a no-op if NDEBUG is defined.
1961*/
1962#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001963static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001964 Vdbe *p;
1965 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00001966 int nWrite = 0;
danielk19771d850a72004-05-31 08:26:49 +00001967 p = db->pVdbe;
1968 while( p ){
drh92f02c32004-09-02 14:57:08 +00001969 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001970 cnt++;
drhad4a4b82008-11-05 16:37:34 +00001971 if( p->readOnly==0 ) nWrite++;
danielk19771d850a72004-05-31 08:26:49 +00001972 }
1973 p = p->pNext;
1974 }
danielk19771d850a72004-05-31 08:26:49 +00001975 assert( cnt==db->activeVdbeCnt );
drhad4a4b82008-11-05 16:37:34 +00001976 assert( nWrite==db->writeVdbeCnt );
danielk19771d850a72004-05-31 08:26:49 +00001977}
1978#else
1979#define checkActiveVdbeCnt(x)
1980#endif
1981
danielk19773cf86062004-05-26 10:11:05 +00001982/*
drhfb982642007-08-30 01:19:59 +00001983** For every Btree that in database connection db which
1984** has been modified, "trip" or invalidate each cursor in
1985** that Btree might have been modified so that the cursor
1986** can never be used again. This happens when a rollback
1987*** occurs. We have to trip all the other cursors, even
1988** cursor from other VMs in different database connections,
1989** so that none of them try to use the data at which they
1990** were pointing and which now may have been changed due
1991** to the rollback.
1992**
1993** Remember that a rollback can delete tables complete and
1994** reorder rootpages. So it is not sufficient just to save
1995** the state of the cursor. We have to invalidate the cursor
1996** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001997*/
drhade6c9c2007-11-24 10:23:44 +00001998static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001999 int i;
2000 for(i=0; i<db->nDb; i++){
2001 Btree *p = db->aDb[i].pBt;
2002 if( p && sqlite3BtreeIsInTrans(p) ){
2003 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
2004 }
danielk1977be718892006-06-23 08:05:19 +00002005 }
2006}
2007
2008/*
danielk1977bd434552009-03-18 10:33:00 +00002009** If the Vdbe passed as the first argument opened a statement-transaction,
2010** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2011** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2012** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
2013** statement transaction is commtted.
2014**
2015** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2016** Otherwise SQLITE_OK.
2017*/
2018int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002019 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002020 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002021
danielk1977e4948172009-07-17 17:25:43 +00002022 /* If p->iStatement is greater than zero, then this Vdbe opened a
2023 ** statement transaction that should be closed here. The only exception
2024 ** is that an IO error may have occured, causing an emergency rollback.
2025 ** In this case (db->nStatement==0), and there is nothing to do.
2026 */
2027 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002028 int i;
2029 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002030
2031 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2032 assert( db->nStatement>0 );
2033 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2034
2035 for(i=0; i<db->nDb; i++){
2036 int rc2 = SQLITE_OK;
2037 Btree *pBt = db->aDb[i].pBt;
2038 if( pBt ){
2039 if( eOp==SAVEPOINT_ROLLBACK ){
2040 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2041 }
2042 if( rc2==SQLITE_OK ){
2043 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2044 }
2045 if( rc==SQLITE_OK ){
2046 rc = rc2;
2047 }
2048 }
2049 }
2050 db->nStatement--;
2051 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002052
dana311b802011-04-26 19:21:34 +00002053 if( rc==SQLITE_OK ){
2054 if( eOp==SAVEPOINT_ROLLBACK ){
2055 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2056 }
2057 if( rc==SQLITE_OK ){
2058 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2059 }
2060 }
2061
dan1da40a32009-09-19 17:00:31 +00002062 /* If the statement transaction is being rolled back, also restore the
2063 ** database handles deferred constraint counter to the value it had when
2064 ** the statement transaction was opened. */
2065 if( eOp==SAVEPOINT_ROLLBACK ){
2066 db->nDeferredCons = p->nStmtDefCons;
2067 }
danielk1977bd434552009-03-18 10:33:00 +00002068 }
2069 return rc;
2070}
2071
2072/*
dan1da40a32009-09-19 17:00:31 +00002073** This function is called when a transaction opened by the database
2074** handle associated with the VM passed as an argument is about to be
2075** committed. If there are outstanding deferred foreign key constraint
2076** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2077**
2078** If there are outstanding FK violations and this function returns
2079** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
2080** an error message to it. Then return SQLITE_ERROR.
2081*/
2082#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002083int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002084 sqlite3 *db = p->db;
dan32b09f22009-09-23 17:29:59 +00002085 if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
dan1da40a32009-09-19 17:00:31 +00002086 p->rc = SQLITE_CONSTRAINT;
dan32b09f22009-09-23 17:29:59 +00002087 p->errorAction = OE_Abort;
dan1da40a32009-09-19 17:00:31 +00002088 sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
2089 return SQLITE_ERROR;
2090 }
2091 return SQLITE_OK;
2092}
2093#endif
2094
2095/*
drh92f02c32004-09-02 14:57:08 +00002096** This routine is called the when a VDBE tries to halt. If the VDBE
2097** has made changes and is in autocommit mode, then commit those
2098** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002099**
drh92f02c32004-09-02 14:57:08 +00002100** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002101** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2102** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002103**
2104** Return an error code. If the commit could not complete because of
2105** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2106** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002107*/
drhff0587c2007-08-29 17:43:19 +00002108int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002109 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002110 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002111
2112 /* This function contains the logic that determines if a statement or
2113 ** transaction will be committed or rolled back as a result of the
2114 ** execution of this virtual machine.
2115 **
drh71b890a2007-10-03 15:30:52 +00002116 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002117 **
drh71b890a2007-10-03 15:30:52 +00002118 ** SQLITE_NOMEM
2119 ** SQLITE_IOERR
2120 ** SQLITE_FULL
2121 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002122 **
drh71b890a2007-10-03 15:30:52 +00002123 ** Then the internal cache might have been left in an inconsistent
2124 ** state. We need to rollback the statement transaction, if there is
2125 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002126 */
drh9a324642003-09-06 20:12:01 +00002127
drh17435752007-08-16 04:30:38 +00002128 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002129 p->rc = SQLITE_NOMEM;
2130 }
drh5f82e3c2009-07-06 00:44:08 +00002131 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002132 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002133 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002134 }
danielk19771d850a72004-05-31 08:26:49 +00002135 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002136
danielk197707cb5602006-01-20 10:55:05 +00002137 /* No commit or rollback needed if the program never started */
2138 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00002139 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002140 int eStatementOp = 0;
2141 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002142
2143 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002144 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002145
drh71b890a2007-10-03 15:30:52 +00002146 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002147 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002148 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002149 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002150 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002151 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002152 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2153 ** no rollback is necessary. Otherwise, at least a savepoint
2154 ** transaction must be rolled back to restore the database to a
2155 ** consistent state.
2156 **
2157 ** Even if the statement is read-only, it is important to perform
2158 ** a statement or transaction rollback operation. If the error
2159 ** occured while writing to the journal, sub-journal or database
2160 ** file as part of an effort to free up cache space (see function
2161 ** pagerStress() in pager.c), the rollback is required to restore
2162 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002163 */
drhad4a4b82008-11-05 16:37:34 +00002164 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002165 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002166 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002167 }else{
2168 /* We are forced to roll back the active transaction. Before doing
2169 ** so, abort any other statements this handle currently has active.
2170 */
drhfb982642007-08-30 01:19:59 +00002171 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00002172 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00002173 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002174 db->autoCommit = 1;
2175 }
danielk1977261919c2005-12-06 12:52:59 +00002176 }
2177 }
dan32b09f22009-09-23 17:29:59 +00002178
2179 /* Check for immediate foreign key violations. */
2180 if( p->rc==SQLITE_OK ){
2181 sqlite3VdbeCheckFk(p, 0);
2182 }
danielk197707cb5602006-01-20 10:55:05 +00002183
danielk1977bd434552009-03-18 10:33:00 +00002184 /* If the auto-commit flag is set and this is the only active writer
2185 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002186 **
2187 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002188 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002189 */
danielk1977093e0f62008-11-13 18:00:14 +00002190 if( !sqlite3VtabInSync(db)
2191 && db->autoCommit
2192 && db->writeVdbeCnt==(p->readOnly==0)
2193 ){
danielk197707cb5602006-01-20 10:55:05 +00002194 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002195 rc = sqlite3VdbeCheckFk(p, 1);
2196 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002197 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002198 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002199 return SQLITE_ERROR;
2200 }
2201 rc = SQLITE_CONSTRAINT;
2202 }else{
2203 /* The auto-commit flag is true, the vdbe program was successful
2204 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2205 ** key constraints to hold up the transaction. This means a commit
2206 ** is required. */
2207 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002208 }
dan19611b12011-01-24 16:00:58 +00002209 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002210 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002211 return SQLITE_BUSY;
2212 }else if( rc!=SQLITE_OK ){
2213 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00002214 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00002215 }else{
dan1da40a32009-09-19 17:00:31 +00002216 db->nDeferredCons = 0;
danielk197707cb5602006-01-20 10:55:05 +00002217 sqlite3CommitInternalChanges(db);
2218 }
2219 }else{
danielk197797a227c2006-01-20 16:32:04 +00002220 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00002221 }
danielk1977bd434552009-03-18 10:33:00 +00002222 db->nStatement = 0;
2223 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002224 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002225 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002226 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002227 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002228 }else{
drhfb982642007-08-30 01:19:59 +00002229 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00002230 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00002231 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002232 db->autoCommit = 1;
2233 }
danielk19771d850a72004-05-31 08:26:49 +00002234 }
danielk197707cb5602006-01-20 10:55:05 +00002235
danielk1977bd434552009-03-18 10:33:00 +00002236 /* If eStatementOp is non-zero, then a statement transaction needs to
2237 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2238 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002239 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2240 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002241 */
danielk1977bd434552009-03-18 10:33:00 +00002242 if( eStatementOp ){
2243 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002244 if( rc ){
drh346506f2011-05-25 01:16:42 +00002245 if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002246 p->rc = rc;
2247 sqlite3DbFree(db, p->zErrMsg);
2248 p->zErrMsg = 0;
2249 }
2250 invalidateCursorsOnModifiedBtrees(db);
2251 sqlite3RollbackAll(db);
2252 sqlite3CloseSavepoints(db);
2253 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002254 }
danielk197777d83ba2004-05-31 10:08:14 +00002255 }
danielk197707cb5602006-01-20 10:55:05 +00002256
danielk1977bd434552009-03-18 10:33:00 +00002257 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2258 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002259 */
drh6be240e2009-07-14 02:33:02 +00002260 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002261 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002262 sqlite3VdbeSetChanges(db, p->nChange);
2263 }else{
2264 sqlite3VdbeSetChanges(db, 0);
2265 }
2266 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002267 }
danielk197707cb5602006-01-20 10:55:05 +00002268
2269 /* Rollback or commit any schema changes that occurred. */
2270 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
drhc7792fa2011-04-02 16:28:52 +00002271 sqlite3ResetInternalSchema(db, -1);
danielk197707cb5602006-01-20 10:55:05 +00002272 db->flags = (db->flags | SQLITE_InternChanges);
2273 }
drhff0587c2007-08-29 17:43:19 +00002274
2275 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002276 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002277 }
danielk19771d850a72004-05-31 08:26:49 +00002278
danielk197765fd59f2006-06-24 11:51:33 +00002279 /* We have successfully halted and closed the VM. Record this fact. */
2280 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002281 db->activeVdbeCnt--;
drhad4a4b82008-11-05 16:37:34 +00002282 if( !p->readOnly ){
2283 db->writeVdbeCnt--;
2284 }
2285 assert( db->activeVdbeCnt>=db->writeVdbeCnt );
drh9a324642003-09-06 20:12:01 +00002286 }
drh92f02c32004-09-02 14:57:08 +00002287 p->magic = VDBE_MAGIC_HALT;
2288 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002289 if( p->db->mallocFailed ){
2290 p->rc = SQLITE_NOMEM;
2291 }
danielk19771d850a72004-05-31 08:26:49 +00002292
danielk1977404ca072009-03-16 13:19:36 +00002293 /* If the auto-commit flag is set to true, then any locks that were held
2294 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2295 ** to invoke any required unlock-notify callbacks.
2296 */
2297 if( db->autoCommit ){
2298 sqlite3ConnectionUnlocked(db);
2299 }
2300
danielk1977bd434552009-03-18 10:33:00 +00002301 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002302 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002303}
drh4cf7c7f2007-08-28 23:28:07 +00002304
drh92f02c32004-09-02 14:57:08 +00002305
2306/*
drh3c23a882007-01-09 14:01:13 +00002307** Each VDBE holds the result of the most recent sqlite3_step() call
2308** in p->rc. This routine sets that result back to SQLITE_OK.
2309*/
2310void sqlite3VdbeResetStepResult(Vdbe *p){
2311 p->rc = SQLITE_OK;
2312}
2313
2314/*
drh92f02c32004-09-02 14:57:08 +00002315** Clean up a VDBE after execution but do not delete the VDBE just yet.
2316** Write any error messages into *pzErrMsg. Return the result code.
2317**
2318** After this routine is run, the VDBE should be ready to be executed
2319** again.
2320**
2321** To look at it another way, this routine resets the state of the
2322** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2323** VDBE_MAGIC_INIT.
2324*/
drhc890fec2008-08-01 20:10:08 +00002325int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002326 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002327 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002328
2329 /* If the VM did not run to completion or if it encountered an
2330 ** error, then it might not have been halted properly. So halt
2331 ** it now.
2332 */
2333 sqlite3VdbeHalt(p);
2334
drhfb7e7652005-01-24 00:28:42 +00002335 /* If the VDBE has be run even partially, then transfer the error code
2336 ** and error message from the VDBE into the main database structure. But
2337 ** if the VDBE has just been set to run but has not actually executed any
2338 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002339 */
drhfb7e7652005-01-24 00:28:42 +00002340 if( p->pc>=0 ){
2341 if( p->zErrMsg ){
danielk19779ff3f3f2008-10-11 17:51:38 +00002342 sqlite3BeginBenignMalloc();
drh633e6d52008-07-28 19:34:53 +00002343 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19779ff3f3f2008-10-11 17:51:38 +00002344 sqlite3EndBenignMalloc();
danielk197797a227c2006-01-20 16:32:04 +00002345 db->errCode = p->rc;
drh633e6d52008-07-28 19:34:53 +00002346 sqlite3DbFree(db, p->zErrMsg);
drhfb7e7652005-01-24 00:28:42 +00002347 p->zErrMsg = 0;
2348 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00002349 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00002350 }else{
drh4ac285a2006-09-15 07:28:50 +00002351 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00002352 }
drh4611d922010-02-25 14:47:01 +00002353 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002354 }else if( p->rc && p->expired ){
2355 /* The expired flag was set on the VDBE before the first call
2356 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2357 ** called), set the database error in this case as well.
2358 */
drh4ac285a2006-09-15 07:28:50 +00002359 sqlite3Error(db, p->rc, 0);
drh633e6d52008-07-28 19:34:53 +00002360 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2361 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002362 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002363 }
2364
2365 /* Reclaim all memory used by the VDBE
2366 */
drhc890fec2008-08-01 20:10:08 +00002367 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002368
2369 /* Save profiling information from this VDBE run.
2370 */
drh9a324642003-09-06 20:12:01 +00002371#ifdef VDBE_PROFILE
2372 {
2373 FILE *out = fopen("vdbe_profile.out", "a");
2374 if( out ){
2375 int i;
2376 fprintf(out, "---- ");
2377 for(i=0; i<p->nOp; i++){
2378 fprintf(out, "%02x", p->aOp[i].opcode);
2379 }
2380 fprintf(out, "\n");
2381 for(i=0; i<p->nOp; i++){
2382 fprintf(out, "%6d %10lld %8lld ",
2383 p->aOp[i].cnt,
2384 p->aOp[i].cycles,
2385 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2386 );
danielk19774adee202004-05-08 08:23:19 +00002387 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002388 }
2389 fclose(out);
2390 }
2391 }
2392#endif
2393 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002394 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002395}
drh92f02c32004-09-02 14:57:08 +00002396
drh9a324642003-09-06 20:12:01 +00002397/*
2398** Clean up and delete a VDBE after execution. Return an integer which is
2399** the result code. Write any error message text into *pzErrMsg.
2400*/
danielk19779e6db7d2004-06-21 08:18:51 +00002401int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002402 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002403 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002404 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002405 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002406 }
danielk19774adee202004-05-08 08:23:19 +00002407 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002408 return rc;
2409}
2410
2411/*
drhf92c7ff2004-06-19 15:40:23 +00002412** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00002413** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00002414** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00002415** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00002416*/
2417void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
2418 int i;
2419 for(i=0; i<pVdbeFunc->nAux; i++){
2420 struct AuxData *pAux = &pVdbeFunc->apAux[i];
drh3500ed62009-05-05 15:46:43 +00002421 if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
drhf92c7ff2004-06-19 15:40:23 +00002422 if( pAux->xDelete ){
2423 pAux->xDelete(pAux->pAux);
2424 }
2425 pAux->pAux = 0;
2426 }
2427 }
2428}
2429
2430/*
dand46def72010-07-24 11:28:28 +00002431** Free all memory associated with the Vdbe passed as the second argument.
2432** The difference between this function and sqlite3VdbeDelete() is that
2433** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
2434** the database connection.
2435*/
2436void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002437 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002438 int i;
dand46def72010-07-24 11:28:28 +00002439 assert( p->db==0 || p->db==db );
2440 releaseMemArray(p->aVar, p->nVar);
2441 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002442 for(pSub=p->pProgram; pSub; pSub=pNext){
2443 pNext = pSub->pNext;
2444 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2445 sqlite3DbFree(db, pSub);
2446 }
drh124c0b42011-06-01 18:15:55 +00002447 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002448 vdbeFreeOpArray(db, p->aOp, p->nOp);
2449 sqlite3DbFree(db, p->aLabel);
2450 sqlite3DbFree(db, p->aColName);
2451 sqlite3DbFree(db, p->zSql);
2452 sqlite3DbFree(db, p->pFree);
2453 sqlite3DbFree(db, p);
2454}
2455
2456/*
drh9a324642003-09-06 20:12:01 +00002457** Delete an entire VDBE.
2458*/
danielk19774adee202004-05-08 08:23:19 +00002459void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002460 sqlite3 *db;
2461
drhfa3be902009-07-07 02:44:07 +00002462 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002463 db = p->db;
drh9a324642003-09-06 20:12:01 +00002464 if( p->pPrev ){
2465 p->pPrev->pNext = p->pNext;
2466 }else{
drh633e6d52008-07-28 19:34:53 +00002467 assert( db->pVdbe==p );
2468 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002469 }
2470 if( p->pNext ){
2471 p->pNext->pPrev = p->pPrev;
2472 }
drh9a324642003-09-06 20:12:01 +00002473 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002474 p->db = 0;
dand46def72010-07-24 11:28:28 +00002475 sqlite3VdbeDeleteObject(db, p);
drh9a324642003-09-06 20:12:01 +00002476}
drha11846b2004-01-07 18:52:56 +00002477
2478/*
drh9a65f2c2009-06-22 19:05:40 +00002479** Make sure the cursor p is ready to read or write the row to which it
2480** was last positioned. Return an error code if an OOM fault or I/O error
2481** prevents us from positioning the cursor to its correct position.
2482**
drha11846b2004-01-07 18:52:56 +00002483** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002484** MoveTo now. If no move is pending, check to see if the row has been
2485** deleted out from under the cursor and if it has, mark the row as
2486** a NULL row.
2487**
2488** If the cursor is already pointing to the correct row and that row has
2489** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002490*/
drhdfe88ec2008-11-03 20:55:06 +00002491int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002492 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002493 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002494#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002495 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002496#endif
drhf0863fe2005-06-12 21:35:51 +00002497 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002498 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002499 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002500 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002501 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2502 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002503#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002504 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002505#endif
drha11846b2004-01-07 18:52:56 +00002506 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002507 p->cacheStatus = CACHE_STALE;
drh6be240e2009-07-14 02:33:02 +00002508 }else if( ALWAYS(p->pCursor) ){
drha3460582008-07-11 21:02:53 +00002509 int hasMoved;
2510 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2511 if( rc ) return rc;
2512 if( hasMoved ){
2513 p->cacheStatus = CACHE_STALE;
2514 p->nullRow = 1;
2515 }
drha11846b2004-01-07 18:52:56 +00002516 }
2517 return SQLITE_OK;
2518}
danielk19774adee202004-05-08 08:23:19 +00002519
drhab9f7f12004-05-08 10:56:11 +00002520/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002521** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002522**
danielk1977cfcdaef2004-05-12 07:33:33 +00002523** sqlite3VdbeSerialType()
2524** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002525** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002526** sqlite3VdbeSerialPut()
2527** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002528**
2529** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002530** data and index records. Each serialized value consists of a
2531** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2532** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002533**
danielk1977cfcdaef2004-05-12 07:33:33 +00002534** In an SQLite index record, the serial type is stored directly before
2535** the blob of data that it corresponds to. In a table record, all serial
2536** types are stored at the start of the record, and the blobs of data at
2537** the end. Hence these functions allow the caller to handle the
2538** serial-type and data blob seperately.
2539**
2540** The following table describes the various storage classes for data:
2541**
2542** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002543** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002544** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002545** 1 1 signed integer
2546** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002547** 3 3 signed integer
2548** 4 4 signed integer
2549** 5 6 signed integer
2550** 6 8 signed integer
2551** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002552** 8 0 Integer constant 0
2553** 9 0 Integer constant 1
2554** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002555** N>=12 and even (N-12)/2 BLOB
2556** N>=13 and odd (N-13)/2 text
2557**
drh35a59652006-01-02 18:24:40 +00002558** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2559** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002560*/
2561
2562/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002563** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002564*/
drhd946db02005-12-29 19:23:06 +00002565u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002566 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002567 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002568
2569 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002570 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002571 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002572 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002573 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002574# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002575 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002576 u64 u;
2577 if( file_format>=4 && (i&1)==i ){
drh8df32842008-12-09 02:51:23 +00002578 return 8+(u32)i;
drhd946db02005-12-29 19:23:06 +00002579 }
drhcfd654b2011-03-05 13:54:15 +00002580 if( i<0 ){
2581 if( i<(-MAX_6BYTE) ) return 6;
2582 /* Previous test prevents: u = -(-9223372036854775808) */
2583 u = -i;
2584 }else{
2585 u = i;
2586 }
drh5742b632005-01-26 17:47:02 +00002587 if( u<=127 ) return 1;
2588 if( u<=32767 ) return 2;
2589 if( u<=8388607 ) return 3;
2590 if( u<=2147483647 ) return 4;
2591 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002592 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002593 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002594 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002595 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002596 }
danielk1977e4359752008-11-03 09:39:45 +00002597 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002598 n = pMem->n;
2599 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002600 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002601 }
drhfdf972a2007-05-02 13:30:27 +00002602 assert( n>=0 );
2603 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002604}
2605
2606/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002607** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002608*/
drh35cd6432009-06-05 14:17:21 +00002609u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002610 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002611 return (serial_type-12)/2;
2612 }else{
drh57196282004-10-06 15:41:16 +00002613 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002614 return aSize[serial_type];
2615 }
danielk1977192ac1d2004-05-10 07:17:30 +00002616}
2617
2618/*
drh110daac2007-05-04 11:59:31 +00002619** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002620** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002621** upper 4 bytes. Return the result.
2622**
drh7a4f5022007-05-23 07:20:08 +00002623** For most architectures, this is a no-op.
2624**
2625** (later): It is reported to me that the mixed-endian problem
2626** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2627** that early versions of GCC stored the two words of a 64-bit
2628** float in the wrong order. And that error has been propagated
2629** ever since. The blame is not necessarily with GCC, though.
2630** GCC might have just copying the problem from a prior compiler.
2631** I am also told that newer versions of GCC that follow a different
2632** ABI get the byte order right.
2633**
2634** Developers using SQLite on an ARM7 should compile and run their
2635** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2636** enabled, some asserts below will ensure that the byte order of
2637** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002638**
2639** (2007-08-30) Frank van Vugt has studied this problem closely
2640** and has send his findings to the SQLite developers. Frank
2641** writes that some Linux kernels offer floating point hardware
2642** emulation that uses only 32-bit mantissas instead of a full
2643** 48-bits as required by the IEEE standard. (This is the
2644** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2645** byte swapping becomes very complicated. To avoid problems,
2646** the necessary byte swapping is carried out using a 64-bit integer
2647** rather than a 64-bit float. Frank assures us that the code here
2648** works for him. We, the developers, have no way to independently
2649** verify this, but Frank seems to know what he is talking about
2650** so we trust him.
drh110daac2007-05-04 11:59:31 +00002651*/
2652#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002653static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002654 union {
drh60d09a72007-08-30 15:05:08 +00002655 u64 r;
drh110daac2007-05-04 11:59:31 +00002656 u32 i[2];
2657 } u;
2658 u32 t;
2659
2660 u.r = in;
2661 t = u.i[0];
2662 u.i[0] = u.i[1];
2663 u.i[1] = t;
2664 return u.r;
2665}
2666# define swapMixedEndianFloat(X) X = floatSwap(X)
2667#else
2668# define swapMixedEndianFloat(X)
2669#endif
2670
2671/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002672** Write the serialized data blob for the value stored in pMem into
2673** buf. It is assumed that the caller has allocated sufficient space.
2674** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002675**
2676** nBuf is the amount of space left in buf[]. nBuf must always be
2677** large enough to hold the entire field. Except, if the field is
2678** a blob with a zero-filled tail, then buf[] might be just the right
2679** size to hold everything except for the zero-filled tail. If buf[]
2680** is only big enough to hold the non-zero prefix, then only write that
2681** prefix into buf[]. But if buf[] is large enough to hold both the
2682** prefix and the tail then write the prefix and set the tail to all
2683** zeros.
2684**
2685** Return the number of bytes actually written into buf[]. The number
2686** of bytes in the zero-filled tail is included in the return value only
2687** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002688*/
drh35cd6432009-06-05 14:17:21 +00002689u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002690 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
drh35cd6432009-06-05 14:17:21 +00002691 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002692
drh1483e142004-05-21 21:12:42 +00002693 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002694 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002695 u64 v;
drh35cd6432009-06-05 14:17:21 +00002696 u32 i;
drha19b7752004-05-30 21:14:58 +00002697 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002698 assert( sizeof(v)==sizeof(pMem->r) );
2699 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002700 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002701 }else{
drh3c024d62007-03-30 11:23:45 +00002702 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002703 }
drh1483e142004-05-21 21:12:42 +00002704 len = i = sqlite3VdbeSerialTypeLen(serial_type);
shane75ac1de2009-06-09 18:58:52 +00002705 assert( len<=(u32)nBuf );
drh1483e142004-05-21 21:12:42 +00002706 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002707 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002708 v >>= 8;
2709 }
2710 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002711 }
drhd946db02005-12-29 19:23:06 +00002712
danielk1977cfcdaef2004-05-12 07:33:33 +00002713 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002714 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002715 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002716 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002717 assert( pMem->n<=nBuf );
2718 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002719 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002720 if( pMem->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002721 len += pMem->u.nZero;
drh35cd6432009-06-05 14:17:21 +00002722 assert( nBuf>=0 );
2723 if( len > (u32)nBuf ){
2724 len = (u32)nBuf;
drhfdf972a2007-05-02 13:30:27 +00002725 }
2726 memset(&buf[pMem->n], 0, len-pMem->n);
2727 }
drhd946db02005-12-29 19:23:06 +00002728 return len;
2729 }
2730
2731 /* NULL or constants 0 or 1 */
2732 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002733}
2734
2735/*
2736** Deserialize the data blob pointed to by buf as serial type serial_type
2737** and store the result in pMem. Return the number of bytes read.
2738*/
drh35cd6432009-06-05 14:17:21 +00002739u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002740 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002741 u32 serial_type, /* Serial type to deserialize */
2742 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002743){
drh3c685822005-05-21 18:32:18 +00002744 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002745 case 10: /* Reserved for future use */
2746 case 11: /* Reserved for future use */
2747 case 0: { /* NULL */
2748 pMem->flags = MEM_Null;
2749 break;
2750 }
2751 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002752 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002753 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002754 return 1;
drh1483e142004-05-21 21:12:42 +00002755 }
drh3c685822005-05-21 18:32:18 +00002756 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002757 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002758 pMem->flags = MEM_Int;
2759 return 2;
2760 }
2761 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002762 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002763 pMem->flags = MEM_Int;
2764 return 3;
2765 }
2766 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002767 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002768 pMem->flags = MEM_Int;
2769 return 4;
2770 }
2771 case 5: { /* 6-byte signed integer */
2772 u64 x = (((signed char)buf[0])<<8) | buf[1];
2773 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2774 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002775 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002776 pMem->flags = MEM_Int;
2777 return 6;
2778 }
drh91124b32005-08-18 18:15:05 +00002779 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002780 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002781 u64 x;
2782 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002783#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002784 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002785 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2786 ** defined that 64-bit floating point values really are mixed
2787 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002788 */
drhde941c62005-08-28 01:34:21 +00002789 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002790 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002791 u64 t2 = t1;
2792 swapMixedEndianFloat(t2);
2793 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002794#endif
drhbfd6b032005-08-28 01:38:44 +00002795
drhd81bd4e2005-09-05 20:06:49 +00002796 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2797 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002798 x = (x<<32) | y;
2799 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002800 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002801 pMem->flags = MEM_Int;
2802 }else{
drh4f0c5872007-03-26 22:05:01 +00002803 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002804 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002805 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002806 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002807 }
2808 return 8;
2809 }
drhd946db02005-12-29 19:23:06 +00002810 case 8: /* Integer 0 */
2811 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002812 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002813 pMem->flags = MEM_Int;
2814 return 0;
2815 }
drh3c685822005-05-21 18:32:18 +00002816 default: {
drh35cd6432009-06-05 14:17:21 +00002817 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00002818 pMem->z = (char *)buf;
2819 pMem->n = len;
2820 pMem->xDel = 0;
2821 if( serial_type&0x01 ){
2822 pMem->flags = MEM_Str | MEM_Ephem;
2823 }else{
2824 pMem->flags = MEM_Blob | MEM_Ephem;
2825 }
2826 return len;
drh696b32f2004-05-30 01:51:52 +00002827 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002828 }
drh3c685822005-05-21 18:32:18 +00002829 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002830}
2831
drh1e968a02008-03-25 00:22:21 +00002832/*
dan03e9cfc2011-09-05 14:20:27 +00002833** This routine is used to allocate sufficient space for an UnpackedRecord
2834** structure large enough to be used with sqlite3VdbeRecordUnpack() if
2835** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00002836**
dan03e9cfc2011-09-05 14:20:27 +00002837** The space is either allocated using sqlite3DbMallocRaw() or from within
2838** the unaligned buffer passed via the second and third arguments (presumably
2839** stack space). If the former, then *ppFree is set to a pointer that should
2840** be eventually freed by the caller using sqlite3DbFree(). Or, if the
2841** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
2842** before returning.
drh1e968a02008-03-25 00:22:21 +00002843**
dan03e9cfc2011-09-05 14:20:27 +00002844** If an OOM error occurs, NULL is returned.
2845*/
2846UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
2847 KeyInfo *pKeyInfo, /* Description of the record */
2848 char *pSpace, /* Unaligned space available */
2849 int szSpace, /* Size of pSpace[] in bytes */
2850 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00002851){
dan03e9cfc2011-09-05 14:20:27 +00002852 UnpackedRecord *p; /* Unpacked record to return */
2853 int nOff; /* Increment pSpace by nOff to align it */
2854 int nByte; /* Number of bytes required for *p */
2855
2856 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00002857 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
2858 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
2859 */
2860 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00002861 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00002862 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00002863 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2864 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00002865 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00002866 }else{
dan42acb3e2011-09-05 20:16:38 +00002867 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00002868 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00002869 }
dan42acb3e2011-09-05 20:16:38 +00002870
2871 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drh1e968a02008-03-25 00:22:21 +00002872 p->pKeyInfo = pKeyInfo;
2873 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00002874 return p;
2875}
2876
2877/*
2878** Given the nKey-byte encoding of a record in pKey[], populate the
2879** UnpackedRecord structure indicated by the fourth argument with the
2880** contents of the decoded record.
2881*/
2882void sqlite3VdbeRecordUnpack(
2883 KeyInfo *pKeyInfo, /* Information about the record format */
2884 int nKey, /* Size of the binary record */
2885 const void *pKey, /* The binary record */
2886 UnpackedRecord *p /* Populate this structure before returning. */
2887){
2888 const unsigned char *aKey = (const unsigned char *)pKey;
2889 int d;
2890 u32 idx; /* Offset in aKey[] to read from */
2891 u16 u; /* Unsigned loop counter */
2892 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00002893 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00002894
2895 p->flags = 0;
drh8c5d1522009-04-10 00:56:28 +00002896 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00002897 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002898 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00002899 u = 0;
drh2fa34d32009-07-15 16:30:50 +00002900 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00002901 u32 serial_type;
2902
danielk197700e13612008-11-17 19:18:54 +00002903 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00002904 pMem->enc = pKeyInfo->enc;
2905 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00002906 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
danielk19775f096132008-03-28 15:44:09 +00002907 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002908 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002909 pMem++;
shane0b8d2762008-07-22 05:18:00 +00002910 u++;
drh1e968a02008-03-25 00:22:21 +00002911 }
drh7d10d5a2008-08-20 16:35:10 +00002912 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00002913 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00002914}
2915
2916/*
2917** This function compares the two table rows or index records
2918** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00002919** or positive integer if key1 is less than, equal to or
2920** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00002921** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2922** key must be a parsed key such as obtained from
2923** sqlite3VdbeParseRecord.
2924**
2925** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00002926** The key with fewer fields is usually compares less than the
2927** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
2928** and the common prefixes are equal, then key1 is less than key2.
2929** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
2930** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00002931** the parts beyond the common prefix are ignored.
2932**
drhe63d9992008-08-13 19:11:48 +00002933** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
2934** the header of pKey1 is ignored. It is assumed that pKey1 is
2935** an index key, and thus ends with a rowid value. The last byte
2936** of the header will therefore be the serial type of the rowid:
2937** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
2938** The serial type of the final rowid will always be a single byte.
2939** By ignoring this last byte of the header, we force the comparison
2940** to ignore the rowid at the end of key1.
drh1e968a02008-03-25 00:22:21 +00002941*/
drhe14006d2008-03-25 17:23:32 +00002942int sqlite3VdbeRecordCompare(
drhec1fc802008-08-13 14:07:40 +00002943 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00002944 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00002945){
danielk197700e13612008-11-17 19:18:54 +00002946 int d1; /* Offset into aKey[] of next data element */
drh1e968a02008-03-25 00:22:21 +00002947 u32 idx1; /* Offset into aKey[] of next header element */
2948 u32 szHdr1; /* Number of bytes in header */
2949 int i = 0;
2950 int nField;
2951 int rc = 0;
2952 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2953 KeyInfo *pKeyInfo;
2954 Mem mem1;
2955
2956 pKeyInfo = pPKey2->pKeyInfo;
2957 mem1.enc = pKeyInfo->enc;
drh37272632009-11-16 21:28:45 +00002958 mem1.db = pKeyInfo->db;
drhd93a8b22009-11-16 03:13:40 +00002959 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
2960 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
drh8b249a82009-11-16 02:14:00 +00002961
2962 /* Compilers may complain that mem1.u.i is potentially uninitialized.
2963 ** We could initialize it, as shown here, to silence those complaints.
drh5275d2e2011-04-27 01:00:17 +00002964 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
drh8b249a82009-11-16 02:14:00 +00002965 ** the unnecessary initialization has a measurable negative performance
2966 ** impact, since this routine is a very high runner. And so, we choose
2967 ** to ignore the compiler warnings and leave this variable uninitialized.
2968 */
2969 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
drh1e968a02008-03-25 00:22:21 +00002970
shane3f8d5cf2008-04-24 19:15:09 +00002971 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002972 d1 = szHdr1;
drhe63d9992008-08-13 19:11:48 +00002973 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
2974 szHdr1--;
2975 }
drh1e968a02008-03-25 00:22:21 +00002976 nField = pKeyInfo->nField;
2977 while( idx1<szHdr1 && i<pPKey2->nField ){
2978 u32 serial_type1;
2979
2980 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002981 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002982 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2983
2984 /* Extract the values to be compared.
2985 */
2986 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2987
2988 /* Do the comparison
2989 */
drhe14006d2008-03-25 17:23:32 +00002990 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002991 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002992 if( rc!=0 ){
drh8b249a82009-11-16 02:14:00 +00002993 assert( mem1.zMalloc==0 ); /* See comment below */
2994
2995 /* Invert the result if we are using DESC sort order. */
2996 if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
2997 rc = -rc;
2998 }
2999
3000 /* If the PREFIX_SEARCH flag is set and all fields except the final
3001 ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
3002 ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
3003 ** This is used by the OP_IsUnique opcode.
3004 */
3005 if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
3006 assert( idx1==szHdr1 && rc );
3007 assert( mem1.flags & MEM_Int );
3008 pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
3009 pPKey2->rowid = mem1.u.i;
3010 }
3011
3012 return rc;
drh1e968a02008-03-25 00:22:21 +00003013 }
3014 i++;
3015 }
drh407414c2009-07-14 14:15:27 +00003016
drh8b249a82009-11-16 02:14:00 +00003017 /* No memory allocation is ever used on mem1. Prove this using
3018 ** the following assert(). If the assert() fails, it indicates a
3019 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
danielk1977de630352009-05-04 11:42:29 +00003020 */
drh8b249a82009-11-16 02:14:00 +00003021 assert( mem1.zMalloc==0 );
danielk1977de630352009-05-04 11:42:29 +00003022
drh8b249a82009-11-16 02:14:00 +00003023 /* rc==0 here means that one of the keys ran out of fields and
3024 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
3025 ** flag is set, then break the tie by treating key2 as larger.
3026 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
3027 ** are considered to be equal. Otherwise, the longer key is the
3028 ** larger. As it happens, the pPKey2 will always be the longer
3029 ** if there is a difference.
3030 */
3031 assert( rc==0 );
3032 if( pPKey2->flags & UNPACKED_INCRKEY ){
3033 rc = -1;
3034 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
3035 /* Leave rc==0 */
3036 }else if( idx1<szHdr1 ){
3037 rc = 1;
drh1e968a02008-03-25 00:22:21 +00003038 }
drh1e968a02008-03-25 00:22:21 +00003039 return rc;
3040}
drhec1fc802008-08-13 14:07:40 +00003041
danielk1977eb015e02004-05-18 01:31:14 +00003042
3043/*
drh7a224de2004-06-02 01:22:02 +00003044** pCur points at an index entry created using the OP_MakeRecord opcode.
3045** Read the rowid (the last field in the record) and store it in *rowid.
3046** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00003047**
3048** pCur might be pointing to text obtained from a corrupt database file.
3049** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00003050*/
drh35f6b932009-06-23 14:15:04 +00003051int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00003052 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003053 int rc;
drhd5788202004-05-28 08:21:05 +00003054 u32 szHdr; /* Size of the header */
3055 u32 typeRowid; /* Serial type of the rowid */
3056 u32 lenRowid; /* Size of the rowid */
3057 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00003058
shanecea72b22009-09-07 04:38:36 +00003059 UNUSED_PARAMETER(db);
3060
drh88a003e2008-12-11 16:17:03 +00003061 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00003062 ** than 2GiB are support - anything large must be database corruption.
3063 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00003064 ** this code can safely assume that nCellKey is 32-bits
3065 */
drhea8ffdf2009-07-22 00:35:23 +00003066 assert( sqlite3BtreeCursorIsValid(pCur) );
drhc27ae612009-07-14 18:35:44 +00003067 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
3068 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00003069 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003070
3071 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003072 memset(&m, 0, sizeof(m));
drh8df32842008-12-09 02:51:23 +00003073 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003074 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003075 return rc;
3076 }
drh88a003e2008-12-11 16:17:03 +00003077
3078 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003079 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003080 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003081 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003082 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003083 goto idx_rowid_corruption;
3084 }
3085
3086 /* The last field of the index should be an integer - the ROWID.
3087 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003088 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003089 testcase( typeRowid==1 );
3090 testcase( typeRowid==2 );
3091 testcase( typeRowid==3 );
3092 testcase( typeRowid==4 );
3093 testcase( typeRowid==5 );
3094 testcase( typeRowid==6 );
3095 testcase( typeRowid==8 );
3096 testcase( typeRowid==9 );
3097 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3098 goto idx_rowid_corruption;
3099 }
drhd5788202004-05-28 08:21:05 +00003100 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003101 testcase( (u32)m.n==szHdr+lenRowid );
3102 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003103 goto idx_rowid_corruption;
3104 }
3105
3106 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003107 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003108 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003109 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003110 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003111
3112 /* Jump here if database corruption is detected after m has been
3113 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3114idx_rowid_corruption:
3115 testcase( m.zMalloc!=0 );
3116 sqlite3VdbeMemRelease(&m);
3117 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003118}
3119
drh7cf6e4d2004-05-19 14:56:55 +00003120/*
drh5f82e3c2009-07-06 00:44:08 +00003121** Compare the key of the index entry that cursor pC is pointing to against
3122** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003123** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003124** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003125**
drh5f82e3c2009-07-06 00:44:08 +00003126** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003127** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003128** is ignored as well. Hence, this routine only compares the prefixes
3129** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003130*/
danielk1977183f9f72004-05-13 05:20:26 +00003131int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00003132 VdbeCursor *pC, /* The cursor to compare against */
drh5f82e3c2009-07-06 00:44:08 +00003133 UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */
drh7cf6e4d2004-05-19 14:56:55 +00003134 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003135){
drh61fc5952007-04-01 23:49:51 +00003136 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003137 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003138 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003139 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003140
drhea8ffdf2009-07-22 00:35:23 +00003141 assert( sqlite3BtreeCursorIsValid(pCur) );
drhc27ae612009-07-14 18:35:44 +00003142 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
3143 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh407414c2009-07-14 14:15:27 +00003144 /* nCellKey will always be between 0 and 0xffffffff because of the say
3145 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003146 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003147 *res = 0;
drh9978c972010-02-23 17:36:32 +00003148 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003149 }
drhfd3ca1c2009-08-25 12:11:00 +00003150 memset(&m, 0, sizeof(m));
drh8df32842008-12-09 02:51:23 +00003151 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003152 if( rc ){
drhd5788202004-05-28 08:21:05 +00003153 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003154 }
drhe63d9992008-08-13 19:11:48 +00003155 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
3156 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00003157 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003158 return SQLITE_OK;
3159}
danielk1977b28af712004-06-21 06:50:26 +00003160
3161/*
3162** This routine sets the value to be returned by subsequent calls to
3163** sqlite3_changes() on the database handle 'db'.
3164*/
3165void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003166 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003167 db->nChange = nChange;
3168 db->nTotalChange += nChange;
3169}
3170
3171/*
3172** Set a flag in the vdbe to update the change counter when it is finalised
3173** or reset.
3174*/
drh4794f732004-11-05 17:17:50 +00003175void sqlite3VdbeCountChanges(Vdbe *v){
3176 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003177}
drhd89bd002005-01-22 03:03:54 +00003178
3179/*
3180** Mark every prepared statement associated with a database connection
3181** as expired.
3182**
3183** An expired statement means that recompilation of the statement is
3184** recommend. Statements expire when things happen that make their
3185** programs obsolete. Removing user-defined functions or collating
3186** sequences, or changing an authorization function are the types of
3187** things that make prepared statements obsolete.
3188*/
3189void sqlite3ExpirePreparedStatements(sqlite3 *db){
3190 Vdbe *p;
3191 for(p = db->pVdbe; p; p=p->pNext){
3192 p->expired = 1;
3193 }
3194}
danielk1977aee18ef2005-03-09 12:26:50 +00003195
3196/*
3197** Return the database associated with the Vdbe.
3198*/
3199sqlite3 *sqlite3VdbeDb(Vdbe *v){
3200 return v->db;
3201}
dan937d0de2009-10-15 18:35:38 +00003202
3203/*
3204** Return a pointer to an sqlite3_value structure containing the value bound
3205** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3206** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3207** constants) to the value before returning it.
3208**
3209** The returned value must be freed by the caller using sqlite3ValueFree().
3210*/
3211sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
3212 assert( iVar>0 );
3213 if( v ){
3214 Mem *pMem = &v->aVar[iVar-1];
3215 if( 0==(pMem->flags & MEM_Null) ){
3216 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3217 if( pRet ){
3218 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3219 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
3220 sqlite3VdbeMemStoreType((Mem *)pRet);
3221 }
3222 return pRet;
3223 }
3224 }
3225 return 0;
3226}
3227
3228/*
3229** Configure SQL variable iVar so that binding a new value to it signals
3230** to sqlite3_reoptimize() that re-preparing the statement may result
3231** in a better query plan.
3232*/
dan1d2ce4f2009-10-19 18:11:09 +00003233void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003234 assert( iVar>0 );
3235 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00003236 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00003237 }else{
dan1d2ce4f2009-10-19 18:11:09 +00003238 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00003239 }
3240}