blob: c7acbcb6466c8c247208d5fc47903d7d913010b5 [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;
drh55ef4d92005-08-14 01:20:37 +0000159 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000160#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000161 pOp->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000162 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000163#endif
drh26c9b5e2008-04-11 14:56:53 +0000164#ifdef VDBE_PROFILE
165 pOp->cycles = 0;
166 pOp->cnt = 0;
167#endif
drh9a324642003-09-06 20:12:01 +0000168 return i;
169}
drh66a51672008-01-03 00:01:23 +0000170int sqlite3VdbeAddOp0(Vdbe *p, int op){
171 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
172}
173int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
174 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
175}
176int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
177 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000178}
179
drh66a51672008-01-03 00:01:23 +0000180
drh701a0ae2004-02-22 20:05:00 +0000181/*
drh66a51672008-01-03 00:01:23 +0000182** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000183*/
drh66a51672008-01-03 00:01:23 +0000184int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000185 Vdbe *p, /* Add the opcode to this VM */
186 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000187 int p1, /* The P1 operand */
188 int p2, /* The P2 operand */
189 int p3, /* The P3 operand */
190 const char *zP4, /* The P4 operand */
191 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000192){
drh66a51672008-01-03 00:01:23 +0000193 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
194 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000195 return addr;
196}
197
198/*
drh8cff69d2009-11-12 19:59:44 +0000199** Add an opcode that includes the p4 value as an integer.
200*/
201int sqlite3VdbeAddOp4Int(
202 Vdbe *p, /* Add the opcode to this VM */
203 int op, /* The new opcode */
204 int p1, /* The P1 operand */
205 int p2, /* The P2 operand */
206 int p3, /* The P3 operand */
207 int p4 /* The P4 operand as an integer */
208){
209 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
210 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
211 return addr;
212}
213
214/*
drh9a324642003-09-06 20:12:01 +0000215** Create a new symbolic label for an instruction that has yet to be
216** coded. The symbolic label is really just a negative number. The
217** label can be used as the P2 value of an operation. Later, when
218** the label is resolved to a specific address, the VDBE will scan
219** through its operation list and change all values of P2 which match
220** the label into the resolved address.
221**
222** The VDBE knows that a P2 value is a label because labels are
223** always negative and P2 values are suppose to be non-negative.
224** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000225**
226** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000227*/
danielk19774adee202004-05-08 08:23:19 +0000228int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000229 int i;
230 i = p->nLabel++;
231 assert( p->magic==VDBE_MAGIC_INIT );
232 if( i>=p->nLabelAlloc ){
drh6a1e0712008-12-05 15:24:15 +0000233 int n = p->nLabelAlloc*2 + 5;
danielk19771e536952007-08-16 10:09:01 +0000234 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drh6a1e0712008-12-05 15:24:15 +0000235 n*sizeof(p->aLabel[0]));
236 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
drh9a324642003-09-06 20:12:01 +0000237 }
drh76ff3a02004-09-24 22:32:30 +0000238 if( p->aLabel ){
239 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000240 }
drh9a324642003-09-06 20:12:01 +0000241 return -1-i;
242}
243
244/*
245** Resolve label "x" to be the address of the next instruction to
246** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000247** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000248*/
danielk19774adee202004-05-08 08:23:19 +0000249void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000250 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000251 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000252 assert( j>=0 && j<p->nLabel );
253 if( p->aLabel ){
254 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000255 }
256}
257
drh4611d922010-02-25 14:47:01 +0000258/*
259** Mark the VDBE as one that can only be run one time.
260*/
261void sqlite3VdbeRunOnlyOnce(Vdbe *p){
262 p->runOnlyOnce = 1;
263}
264
drhff738bc2009-09-24 00:09:58 +0000265#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
dan144926d2009-09-09 11:37:20 +0000266
267/*
268** The following type and function are used to iterate through all opcodes
269** in a Vdbe main program and each of the sub-programs (triggers) it may
270** invoke directly or indirectly. It should be used as follows:
271**
272** Op *pOp;
273** VdbeOpIter sIter;
274**
275** memset(&sIter, 0, sizeof(sIter));
276** sIter.v = v; // v is of type Vdbe*
277** while( (pOp = opIterNext(&sIter)) ){
278** // Do something with pOp
279** }
280** sqlite3DbFree(v->db, sIter.apSub);
281**
282*/
283typedef struct VdbeOpIter VdbeOpIter;
284struct VdbeOpIter {
285 Vdbe *v; /* Vdbe to iterate through the opcodes of */
286 SubProgram **apSub; /* Array of subprograms */
287 int nSub; /* Number of entries in apSub */
288 int iAddr; /* Address of next instruction to return */
289 int iSub; /* 0 = main program, 1 = first sub-program etc. */
290};
291static Op *opIterNext(VdbeOpIter *p){
292 Vdbe *v = p->v;
293 Op *pRet = 0;
294 Op *aOp;
295 int nOp;
296
297 if( p->iSub<=p->nSub ){
298
299 if( p->iSub==0 ){
300 aOp = v->aOp;
301 nOp = v->nOp;
302 }else{
303 aOp = p->apSub[p->iSub-1]->aOp;
304 nOp = p->apSub[p->iSub-1]->nOp;
305 }
306 assert( p->iAddr<nOp );
307
308 pRet = &aOp[p->iAddr];
309 p->iAddr++;
310 if( p->iAddr==nOp ){
311 p->iSub++;
312 p->iAddr = 0;
313 }
314
315 if( pRet->p4type==P4_SUBPROGRAM ){
316 int nByte = (p->nSub+1)*sizeof(SubProgram*);
317 int j;
318 for(j=0; j<p->nSub; j++){
319 if( p->apSub[j]==pRet->p4.pProgram ) break;
320 }
321 if( j==p->nSub ){
322 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
323 if( !p->apSub ){
324 pRet = 0;
325 }else{
326 p->apSub[p->nSub++] = pRet->p4.pProgram;
327 }
328 }
329 }
330 }
331
332 return pRet;
333}
334
335/*
danf3677212009-09-10 16:14:50 +0000336** Check if the program stored in the VM associated with pParse may
drhff738bc2009-09-24 00:09:58 +0000337** throw an ABORT exception (causing the statement, but not entire transaction
dan144926d2009-09-09 11:37:20 +0000338** to be rolled back). This condition is true if the main program or any
339** sub-programs contains any of the following:
340**
341** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
342** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
343** * OP_Destroy
344** * OP_VUpdate
345** * OP_VRename
dan32b09f22009-09-23 17:29:59 +0000346** * OP_FkCounter with P2==0 (immediate foreign key constraint)
dan144926d2009-09-09 11:37:20 +0000347**
danf3677212009-09-10 16:14:50 +0000348** Then check that the value of Parse.mayAbort is true if an
349** ABORT may be thrown, or false otherwise. Return true if it does
350** match, or false otherwise. This function is intended to be used as
351** part of an assert statement in the compiler. Similar to:
352**
353** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
dan144926d2009-09-09 11:37:20 +0000354*/
danf3677212009-09-10 16:14:50 +0000355int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
356 int hasAbort = 0;
dan144926d2009-09-09 11:37:20 +0000357 Op *pOp;
358 VdbeOpIter sIter;
359 memset(&sIter, 0, sizeof(sIter));
360 sIter.v = v;
361
362 while( (pOp = opIterNext(&sIter))!=0 ){
363 int opcode = pOp->opcode;
364 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
dan32b09f22009-09-23 17:29:59 +0000365#ifndef SQLITE_OMIT_FOREIGN_KEY
dan0ff297e2009-09-25 17:03:14 +0000366 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
dan32b09f22009-09-23 17:29:59 +0000367#endif
dan144926d2009-09-09 11:37:20 +0000368 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
369 && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
370 ){
danf3677212009-09-10 16:14:50 +0000371 hasAbort = 1;
dan144926d2009-09-09 11:37:20 +0000372 break;
373 }
374 }
dan144926d2009-09-09 11:37:20 +0000375 sqlite3DbFree(v->db, sIter.apSub);
danf3677212009-09-10 16:14:50 +0000376
377 /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
378 ** If malloc failed, then the while() loop above may not have iterated
379 ** through all opcodes and hasAbort may be set incorrectly. Return
380 ** true for this case to prevent the assert() in the callers frame
381 ** from failing. */
382 return ( v->db->mallocFailed || hasAbort==mayAbort );
dan144926d2009-09-09 11:37:20 +0000383}
drhff738bc2009-09-24 00:09:58 +0000384#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
dan144926d2009-09-09 11:37:20 +0000385
drh9a324642003-09-06 20:12:01 +0000386/*
drh9cbf3422008-01-17 16:22:13 +0000387** Loop through the program looking for P2 values that are negative
388** on jump instructions. Each such value is a label. Resolve the
389** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000390**
391** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000392**
drh13449892005-09-07 21:22:45 +0000393** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000394** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000395** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
drha6c2ed92009-11-14 23:22:23 +0000396**
397** The Op.opflags field is set on all opcodes.
drh76ff3a02004-09-24 22:32:30 +0000398*/
drh9cbf3422008-01-17 16:22:13 +0000399static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000400 int i;
dan165921a2009-08-28 18:53:45 +0000401 int nMaxArgs = *pMaxFuncArgs;
drh76ff3a02004-09-24 22:32:30 +0000402 Op *pOp;
403 int *aLabel = p->aLabel;
drhad4a4b82008-11-05 16:37:34 +0000404 p->readOnly = 1;
drh76ff3a02004-09-24 22:32:30 +0000405 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000406 u8 opcode = pOp->opcode;
407
drha6c2ed92009-11-14 23:22:23 +0000408 pOp->opflags = sqlite3OpcodeProperty[opcode];
drha2baf3a2008-06-18 15:34:09 +0000409 if( opcode==OP_Function || opcode==OP_AggStep ){
drh98757152008-01-09 23:04:12 +0000410 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
drh10fc7272010-12-08 18:30:19 +0000411 }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
drhad4a4b82008-11-05 16:37:34 +0000412 p->readOnly = 0;
danielk1977182c4ba2007-06-27 15:53:34 +0000413#ifndef SQLITE_OMIT_VIRTUALTABLE
drha6c2ed92009-11-14 23:22:23 +0000414 }else if( opcode==OP_VUpdate ){
415 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh4be8b512006-06-13 23:51:34 +0000416 }else if( opcode==OP_VFilter ){
417 int n;
418 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000419 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000420 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000421 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000422#endif
danielk1977bc04f852005-03-29 08:26:13 +0000423 }
danielk1977634f2982005-03-28 08:44:07 +0000424
drha6c2ed92009-11-14 23:22:23 +0000425 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
drhd2981512008-01-04 19:33:49 +0000426 assert( -1-pOp->p2<p->nLabel );
427 pOp->p2 = aLabel[-1-pOp->p2];
428 }
drh76ff3a02004-09-24 22:32:30 +0000429 }
drh633e6d52008-07-28 19:34:53 +0000430 sqlite3DbFree(p->db, p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000431 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000432
433 *pMaxFuncArgs = nMaxArgs;
drh76ff3a02004-09-24 22:32:30 +0000434}
435
436/*
drh9a324642003-09-06 20:12:01 +0000437** Return the address of the next instruction to be inserted.
438*/
danielk19774adee202004-05-08 08:23:19 +0000439int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000440 assert( p->magic==VDBE_MAGIC_INIT );
441 return p->nOp;
442}
443
dan65a7cd12009-09-01 12:16:01 +0000444/*
445** This function returns a pointer to the array of opcodes associated with
446** the Vdbe passed as the first argument. It is the callers responsibility
447** to arrange for the returned array to be eventually freed using the
448** vdbeFreeOpArray() function.
449**
450** Before returning, *pnOp is set to the number of entries in the returned
451** array. Also, *pnMaxArg is set to the larger of its current value and
452** the number of entries in the Vdbe.apArg[] array required to execute the
453** returned program.
454*/
dan165921a2009-08-28 18:53:45 +0000455VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
456 VdbeOp *aOp = p->aOp;
dan523a0872009-08-31 05:23:32 +0000457 assert( aOp && !p->db->mallocFailed );
dan65a7cd12009-09-01 12:16:01 +0000458
459 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
460 assert( p->aMutex.nMutex==0 );
461
dan165921a2009-08-28 18:53:45 +0000462 resolveP2Values(p, pnMaxArg);
463 *pnOp = p->nOp;
464 p->aOp = 0;
465 return aOp;
466}
467
drh9a324642003-09-06 20:12:01 +0000468/*
469** Add a whole list of operations to the operation stack. Return the
470** address of the first operation added.
471*/
danielk19774adee202004-05-08 08:23:19 +0000472int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000473 int addr;
474 assert( p->magic==VDBE_MAGIC_INIT );
danielk197700e13612008-11-17 19:18:54 +0000475 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000476 return 0;
drh9a324642003-09-06 20:12:01 +0000477 }
478 addr = p->nOp;
drh7b746032009-06-26 12:15:22 +0000479 if( ALWAYS(nOp>0) ){
drh9a324642003-09-06 20:12:01 +0000480 int i;
drh905793e2004-02-21 13:31:09 +0000481 VdbeOpList const *pIn = aOp;
482 for(i=0; i<nOp; i++, pIn++){
483 int p2 = pIn->p2;
484 VdbeOp *pOut = &p->aOp[i+addr];
485 pOut->opcode = pIn->opcode;
486 pOut->p1 = pIn->p1;
drha6c2ed92009-11-14 23:22:23 +0000487 if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
drh8558cde2008-01-05 05:20:10 +0000488 pOut->p2 = addr + ADDR(p2);
489 }else{
490 pOut->p2 = p2;
491 }
drh24003452008-01-03 01:28:59 +0000492 pOut->p3 = pIn->p3;
493 pOut->p4type = P4_NOTUSED;
494 pOut->p4.p = 0;
495 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000496#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000497 pOut->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000498 if( sqlite3VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000499 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000500 }
501#endif
502 }
503 p->nOp += nOp;
504 }
505 return addr;
506}
507
508/*
509** Change the value of the P1 operand for a specific instruction.
510** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000511** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000512** few minor changes to the program.
513*/
danielk19774adee202004-05-08 08:23:19 +0000514void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh7b746032009-06-26 12:15:22 +0000515 assert( p!=0 );
516 assert( addr>=0 );
517 if( p->nOp>addr ){
drh9a324642003-09-06 20:12:01 +0000518 p->aOp[addr].p1 = val;
519 }
520}
521
522/*
523** Change the value of the P2 operand for a specific instruction.
524** This routine is useful for setting a jump destination.
525*/
danielk19774adee202004-05-08 08:23:19 +0000526void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh7b746032009-06-26 12:15:22 +0000527 assert( p!=0 );
528 assert( addr>=0 );
529 if( p->nOp>addr ){
drh9a324642003-09-06 20:12:01 +0000530 p->aOp[addr].p2 = val;
531 }
532}
533
drhd654be82005-09-20 17:42:23 +0000534/*
danielk19771f4aa332008-01-03 09:51:55 +0000535** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000536*/
537void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
drh7b746032009-06-26 12:15:22 +0000538 assert( p!=0 );
539 assert( addr>=0 );
540 if( p->nOp>addr ){
danielk1977207872a2008-01-03 07:54:23 +0000541 p->aOp[addr].p3 = val;
542 }
543}
544
545/*
drh35573352008-01-08 23:54:25 +0000546** Change the value of the P5 operand for the most recently
547** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000548*/
drh35573352008-01-08 23:54:25 +0000549void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
drh7b746032009-06-26 12:15:22 +0000550 assert( p!=0 );
551 if( p->aOp ){
drh35573352008-01-08 23:54:25 +0000552 assert( p->nOp>0 );
553 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000554 }
555}
556
557/*
drhf8875402006-03-17 13:56:34 +0000558** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000559** the address of the next instruction to be coded.
560*/
561void sqlite3VdbeJumpHere(Vdbe *p, int addr){
562 sqlite3VdbeChangeP2(p, addr, p->nOp);
563}
drhb38ad992005-09-16 00:27:01 +0000564
drhb7f6f682006-07-08 17:06:43 +0000565
566/*
567** If the input FuncDef structure is ephemeral, then free it. If
568** the FuncDef is not ephermal, then do nothing.
569*/
drh633e6d52008-07-28 19:34:53 +0000570static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drh7b746032009-06-26 12:15:22 +0000571 if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000572 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000573 }
574}
575
dand46def72010-07-24 11:28:28 +0000576static void vdbeFreeOpArray(sqlite3 *, Op *, int);
577
drhb38ad992005-09-16 00:27:01 +0000578/*
drh66a51672008-01-03 00:01:23 +0000579** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000580*/
drh633e6d52008-07-28 19:34:53 +0000581static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000582 if( p4 ){
dand46def72010-07-24 11:28:28 +0000583 assert( db );
drh66a51672008-01-03 00:01:23 +0000584 switch( p4type ){
585 case P4_REAL:
586 case P4_INT64:
drh66a51672008-01-03 00:01:23 +0000587 case P4_DYNAMIC:
588 case P4_KEYINFO:
drh0acb7e42008-06-25 00:12:41 +0000589 case P4_INTARRAY:
drh66a51672008-01-03 00:01:23 +0000590 case P4_KEYINFO_HANDOFF: {
drh633e6d52008-07-28 19:34:53 +0000591 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000592 break;
593 }
drhb9755982010-07-24 16:34:37 +0000594 case P4_MPRINTF: {
drh7043db92010-07-26 12:38:12 +0000595 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
drhb9755982010-07-24 16:34:37 +0000596 break;
597 }
drh66a51672008-01-03 00:01:23 +0000598 case P4_VDBEFUNC: {
drh0acb7e42008-06-25 00:12:41 +0000599 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
drh633e6d52008-07-28 19:34:53 +0000600 freeEphemeralFunction(db, pVdbeFunc->pFunc);
dand46def72010-07-24 11:28:28 +0000601 if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh633e6d52008-07-28 19:34:53 +0000602 sqlite3DbFree(db, pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000603 break;
604 }
drh66a51672008-01-03 00:01:23 +0000605 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000606 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000607 break;
608 }
drh66a51672008-01-03 00:01:23 +0000609 case P4_MEM: {
drhc176c272010-07-26 13:57:59 +0000610 if( db->pnBytesFreed==0 ){
611 sqlite3ValueFree((sqlite3_value*)p4);
612 }else{
drhf37c68e2010-07-26 14:20:06 +0000613 Mem *p = (Mem*)p4;
614 sqlite3DbFree(db, p->zMalloc);
615 sqlite3DbFree(db, p);
drhc176c272010-07-26 13:57:59 +0000616 }
drhac1733d2005-09-17 17:58:22 +0000617 break;
618 }
danielk1977595a5232009-07-24 17:58:53 +0000619 case P4_VTAB : {
dand46def72010-07-24 11:28:28 +0000620 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
danielk1977595a5232009-07-24 17:58:53 +0000621 break;
622 }
drhb38ad992005-09-16 00:27:01 +0000623 }
624 }
625}
626
dan65a7cd12009-09-01 12:16:01 +0000627/*
628** Free the space allocated for aOp and any p4 values allocated for the
629** opcodes contained within. If aOp is not NULL it is assumed to contain
630** nOp entries.
631*/
dan165921a2009-08-28 18:53:45 +0000632static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
633 if( aOp ){
634 Op *pOp;
635 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
636 freeP4(db, pOp->p4type, pOp->p4.p);
637#ifdef SQLITE_DEBUG
638 sqlite3DbFree(db, pOp->zComment);
639#endif
640 }
641 }
642 sqlite3DbFree(db, aOp);
643}
644
dan65a7cd12009-09-01 12:16:01 +0000645/*
dand19c9332010-07-26 12:05:17 +0000646** Link the SubProgram object passed as the second argument into the linked
647** list at Vdbe.pSubProgram. This list is used to delete all sub-program
648** objects when the VM is no longer required.
dan65a7cd12009-09-01 12:16:01 +0000649*/
dand19c9332010-07-26 12:05:17 +0000650void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
651 p->pNext = pVdbe->pProgram;
652 pVdbe->pProgram = p;
dan165921a2009-08-28 18:53:45 +0000653}
654
drh9a324642003-09-06 20:12:01 +0000655/*
drhf8875402006-03-17 13:56:34 +0000656** Change N opcodes starting at addr to No-ops.
657*/
658void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
drh7b746032009-06-26 12:15:22 +0000659 if( p->aOp ){
danielk197792d4d7a2007-05-04 12:05:56 +0000660 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000661 sqlite3 *db = p->db;
danielk197792d4d7a2007-05-04 12:05:56 +0000662 while( N-- ){
drh633e6d52008-07-28 19:34:53 +0000663 freeP4(db, pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000664 memset(pOp, 0, sizeof(pOp[0]));
665 pOp->opcode = OP_Noop;
666 pOp++;
667 }
drhf8875402006-03-17 13:56:34 +0000668 }
669}
670
671/*
drh66a51672008-01-03 00:01:23 +0000672** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000673** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000674** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000675** few minor changes to the program.
676**
drh66a51672008-01-03 00:01:23 +0000677** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000678** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000679** A value of n==0 means copy bytes of zP4 up to and including the
680** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000681**
drh66a51672008-01-03 00:01:23 +0000682** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000683** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000684** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000685** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000686** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000687** caller should not free the allocation, it will be freed when the Vdbe is
688** finalized.
689**
drh66a51672008-01-03 00:01:23 +0000690** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000691** to a string or structure that is guaranteed to exist for the lifetime of
692** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000693**
drh66a51672008-01-03 00:01:23 +0000694** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000695*/
drh66a51672008-01-03 00:01:23 +0000696void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000697 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000698 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000699 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000700 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000701 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000702 if( p->aOp==0 || db->mallocFailed ){
danielk1977595a5232009-07-24 17:58:53 +0000703 if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
drh633e6d52008-07-28 19:34:53 +0000704 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000705 }
danielk1977d5d56522005-03-16 12:15:20 +0000706 return;
707 }
drh7b746032009-06-26 12:15:22 +0000708 assert( p->nOp>0 );
drh91fd4d42008-01-19 20:11:25 +0000709 assert( addr<p->nOp );
710 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000711 addr = p->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000712 }
713 pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000714 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000715 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000716 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000717 /* Note: this cast is safe, because the origin data point was an int
718 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000719 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000720 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000721 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000722 pOp->p4.p = 0;
723 pOp->p4type = P4_NOTUSED;
724 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000725 KeyInfo *pKeyInfo;
726 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000727
drh66a51672008-01-03 00:01:23 +0000728 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000729 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhb9755982010-07-24 16:34:37 +0000730 pKeyInfo = sqlite3DbMallocRaw(0, nByte);
danielk19772dca4ac2008-01-03 11:50:29 +0000731 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000732 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000733 u8 *aSortOrder;
drha378c562010-04-02 12:55:38 +0000734 memcpy((char*)pKeyInfo, zP4, nByte - nField);
drhfdd6e852005-12-16 01:06:16 +0000735 aSortOrder = pKeyInfo->aSortOrder;
736 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000737 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000738 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
739 }
drh66a51672008-01-03 00:01:23 +0000740 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000741 }else{
drh17435752007-08-16 04:30:38 +0000742 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000743 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000744 }
drh66a51672008-01-03 00:01:23 +0000745 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000746 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000747 pOp->p4type = P4_KEYINFO;
danielk1977595a5232009-07-24 17:58:53 +0000748 }else if( n==P4_VTAB ){
749 pOp->p4.p = (void*)zP4;
750 pOp->p4type = P4_VTAB;
751 sqlite3VtabLock((VTable *)zP4);
752 assert( ((VTable *)zP4)->db==p->db );
drh9a324642003-09-06 20:12:01 +0000753 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000754 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000755 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000756 }else{
drhea678832008-12-10 19:26:22 +0000757 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000758 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000759 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000760 }
761}
762
drhad6d9462004-09-19 02:15:24 +0000763#ifndef NDEBUG
764/*
drh16ee60f2008-06-20 18:13:25 +0000765** Change the comment on the the most recently coded instruction. Or
766** insert a No-op and add the comment to that new instruction. This
767** makes the code easier to read during debugging. None of this happens
768** in a production build.
drhad6d9462004-09-19 02:15:24 +0000769*/
770void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
771 va_list ap;
dan165921a2009-08-28 18:53:45 +0000772 if( !p ) return;
danielk197701256832007-04-18 14:24:32 +0000773 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000774 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000775 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000776 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000777 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000778 sqlite3DbFree(p->db, *pz);
drh8cc74322008-01-15 02:22:24 +0000779 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000780 va_end(ap);
781 }
drhad6d9462004-09-19 02:15:24 +0000782}
drh16ee60f2008-06-20 18:13:25 +0000783void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
784 va_list ap;
dan165921a2009-08-28 18:53:45 +0000785 if( !p ) return;
drh16ee60f2008-06-20 18:13:25 +0000786 sqlite3VdbeAddOp0(p, OP_Noop);
787 assert( p->nOp>0 || p->aOp==0 );
788 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
789 if( p->nOp ){
790 char **pz = &p->aOp[p->nOp-1].zComment;
791 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000792 sqlite3DbFree(p->db, *pz);
drh16ee60f2008-06-20 18:13:25 +0000793 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
794 va_end(ap);
795 }
796}
797#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000798
drh9a324642003-09-06 20:12:01 +0000799/*
drh20411ea2009-05-29 19:00:12 +0000800** Return the opcode for a given address. If the address is -1, then
801** return the most recently inserted opcode.
802**
803** If a memory allocation error has occurred prior to the calling of this
804** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
drhf83dc1e2010-06-03 12:09:52 +0000805** is readable but not writable, though it is cast to a writable value.
806** The return of a dummy opcode allows the call to continue functioning
807** after a OOM fault without having to check to see if the return from
808** this routine is a valid pointer. But because the dummy.opcode is 0,
809** dummy will never be written to. This is verified by code inspection and
810** by running with Valgrind.
drh37b89a02009-06-19 00:33:31 +0000811**
812** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
813** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
814** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
815** a new VDBE is created. So we are free to set addr to p->nOp-1 without
816** having to double-check to make sure that the result is non-negative. But
817** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
818** check the value of p->nOp-1 before continuing.
drh9a324642003-09-06 20:12:01 +0000819*/
danielk19774adee202004-05-08 08:23:19 +0000820VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drha0b75da2010-07-02 18:44:37 +0000821 /* C89 specifies that the constant "dummy" will be initialized to all
822 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
823 static const VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
drh9a324642003-09-06 20:12:01 +0000824 assert( p->magic==VDBE_MAGIC_INIT );
drh37b89a02009-06-19 00:33:31 +0000825 if( addr<0 ){
826#ifdef SQLITE_OMIT_TRACE
drhf83dc1e2010-06-03 12:09:52 +0000827 if( p->nOp==0 ) return (VdbeOp*)&dummy;
drh37b89a02009-06-19 00:33:31 +0000828#endif
829 addr = p->nOp - 1;
830 }
drh17435752007-08-16 04:30:38 +0000831 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000832 if( p->db->mallocFailed ){
drhf83dc1e2010-06-03 12:09:52 +0000833 return (VdbeOp*)&dummy;
drh20411ea2009-05-29 19:00:12 +0000834 }else{
835 return &p->aOp[addr];
836 }
drh9a324642003-09-06 20:12:01 +0000837}
838
drhb7f91642004-10-31 02:22:47 +0000839#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
840 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000841/*
drh66a51672008-01-03 00:01:23 +0000842** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000843** Use zTemp for any required temporary buffer space.
844*/
drh66a51672008-01-03 00:01:23 +0000845static char *displayP4(Op *pOp, char *zTemp, int nTemp){
846 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000847 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000848 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000849 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000850 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000851 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000852 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000853 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +0000854 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +0000855 for(j=0; j<pKeyInfo->nField; j++){
856 CollSeq *pColl = pKeyInfo->aColl[j];
857 if( pColl ){
drhea678832008-12-10 19:26:22 +0000858 int n = sqlite3Strlen30(pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000859 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000860 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000861 break;
862 }
863 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000864 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000865 zTemp[i++] = '-';
866 }
drh5bb3eb92007-05-04 13:15:55 +0000867 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000868 i += n;
869 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000870 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000871 i += 4;
872 }
873 }
874 zTemp[i++] = ')';
875 zTemp[i] = 0;
876 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000877 break;
878 }
drh66a51672008-01-03 00:01:23 +0000879 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000880 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000881 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000882 break;
883 }
drh66a51672008-01-03 00:01:23 +0000884 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000885 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000886 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000887 break;
888 }
drh66a51672008-01-03 00:01:23 +0000889 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000890 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000891 break;
892 }
drh66a51672008-01-03 00:01:23 +0000893 case P4_INT32: {
894 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000895 break;
896 }
drh66a51672008-01-03 00:01:23 +0000897 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000898 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000899 break;
900 }
drh66a51672008-01-03 00:01:23 +0000901 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000902 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000903 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000904 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000905 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000906 }else if( pMem->flags & MEM_Int ){
907 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
908 }else if( pMem->flags & MEM_Real ){
909 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drh56016892009-08-25 14:24:04 +0000910 }else{
911 assert( pMem->flags & MEM_Blob );
912 zP4 = "(blob)";
drhd4e70eb2008-01-02 00:34:36 +0000913 }
drh598f1342007-10-23 15:39:45 +0000914 break;
915 }
drha967e882006-06-13 01:04:52 +0000916#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000917 case P4_VTAB: {
danielk1977595a5232009-07-24 17:58:53 +0000918 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
drh19146192006-06-26 19:10:32 +0000919 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000920 break;
921 }
922#endif
drh0acb7e42008-06-25 00:12:41 +0000923 case P4_INTARRAY: {
924 sqlite3_snprintf(nTemp, zTemp, "intarray");
925 break;
926 }
dan165921a2009-08-28 18:53:45 +0000927 case P4_SUBPROGRAM: {
928 sqlite3_snprintf(nTemp, zTemp, "program");
929 break;
930 }
drhd3d39e92004-05-20 22:16:29 +0000931 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000932 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000933 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000934 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000935 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000936 }
937 }
938 }
drh66a51672008-01-03 00:01:23 +0000939 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000940 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000941}
drhb7f91642004-10-31 02:22:47 +0000942#endif
drhd3d39e92004-05-20 22:16:29 +0000943
drh900b31e2007-08-28 02:27:51 +0000944/*
drhd0679ed2007-08-28 22:24:34 +0000945** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
drh3ebaee92010-05-06 21:37:22 +0000946**
947** The prepared statement has to know in advance which Btree objects
948** will be used so that it can acquire mutexes on them all in sorted
949** order (via sqlite3VdbeMutexArrayEnter(). Mutexes are acquired
950** in order (and released in reverse order) to avoid deadlocks.
drh900b31e2007-08-28 02:27:51 +0000951*/
drhfb982642007-08-30 01:19:59 +0000952void sqlite3VdbeUsesBtree(Vdbe *p, int i){
953 int mask;
drh3500ed62009-05-05 15:46:43 +0000954 assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
danielk197700e13612008-11-17 19:18:54 +0000955 assert( i<(int)sizeof(p->btreeMask)*8 );
drh3500ed62009-05-05 15:46:43 +0000956 mask = ((u32)1)<<i;
drhfb982642007-08-30 01:19:59 +0000957 if( (p->btreeMask & mask)==0 ){
958 p->btreeMask |= mask;
959 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
960 }
drh900b31e2007-08-28 02:27:51 +0000961}
962
drhd3d39e92004-05-20 22:16:29 +0000963
danielk19778b60e0f2005-01-12 09:10:39 +0000964#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000965/*
966** Print a single opcode. This routine is used for debugging only.
967*/
danielk19774adee202004-05-08 08:23:19 +0000968void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000969 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000970 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000971 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000972 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000973 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000974 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000975 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
976#ifdef SQLITE_DEBUG
977 pOp->zComment ? pOp->zComment : ""
978#else
979 ""
980#endif
981 );
drh9a324642003-09-06 20:12:01 +0000982 fflush(pOut);
983}
984#endif
985
986/*
drh76ff3a02004-09-24 22:32:30 +0000987** Release an array of N Mem elements
988*/
drhc890fec2008-08-01 20:10:08 +0000989static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +0000990 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +0000991 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +0000992 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +0000993 u8 malloc_failed = db->mallocFailed;
dand46def72010-07-24 11:28:28 +0000994 if( db->pnBytesFreed ){
995 for(pEnd=&p[N]; p<pEnd; p++){
996 sqlite3DbFree(db, p->zMalloc);
997 }
drhc176c272010-07-26 13:57:59 +0000998 return;
999 }
danielk1977e972e032008-09-19 18:32:26 +00001000 for(pEnd=&p[N]; p<pEnd; p++){
1001 assert( (&p[1])==pEnd || p[0].db==p[1].db );
1002
1003 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1004 ** that takes advantage of the fact that the memory cell value is
1005 ** being set to NULL after releasing any dynamic resources.
1006 **
1007 ** The justification for duplicating code is that according to
1008 ** callgrind, this causes a certain test case to hit the CPU 4.7
1009 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1010 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1011 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1012 ** with no indexes using a single prepared INSERT statement, bind()
1013 ** and reset(). Inserts are grouped into a transaction.
1014 */
dan165921a2009-08-28 18:53:45 +00001015 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001016 sqlite3VdbeMemRelease(p);
1017 }else if( p->zMalloc ){
1018 sqlite3DbFree(db, p->zMalloc);
1019 p->zMalloc = 0;
1020 }
1021
danielk19775f096132008-03-28 15:44:09 +00001022 p->flags = MEM_Null;
drh76ff3a02004-09-24 22:32:30 +00001023 }
danielk1977a7a8e142008-02-13 18:25:27 +00001024 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001025 }
1026}
1027
dan65a7cd12009-09-01 12:16:01 +00001028/*
1029** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1030** allocated by the OP_Program opcode in sqlite3VdbeExec().
1031*/
dan165921a2009-08-28 18:53:45 +00001032void sqlite3VdbeFrameDelete(VdbeFrame *p){
1033 int i;
1034 Mem *aMem = VdbeFrameMem(p);
1035 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1036 for(i=0; i<p->nChildCsr; i++){
1037 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1038 }
1039 releaseMemArray(aMem, p->nChildMem);
1040 sqlite3DbFree(p->v->db, p);
1041}
1042
drhb7f91642004-10-31 02:22:47 +00001043#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001044/*
drh9a324642003-09-06 20:12:01 +00001045** Give a listing of the program in the virtual machine.
1046**
danielk19774adee202004-05-08 08:23:19 +00001047** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001048** running the code, it invokes the callback once for each instruction.
1049** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001050**
1051** When p->explain==1, each instruction is listed. When
1052** p->explain==2, only OP_Explain instructions are listed and these
1053** are shown in a different format. p->explain==2 is used to implement
1054** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001055**
1056** When p->explain==1, first the main program is listed, then each of
1057** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001058*/
danielk19774adee202004-05-08 08:23:19 +00001059int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001060 Vdbe *p /* The VDBE */
1061){
drh5cfa5842009-12-31 20:35:08 +00001062 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001063 int nSub = 0; /* Number of sub-vdbes seen so far */
1064 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001065 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1066 sqlite3 *db = p->db; /* The database connection */
1067 int i; /* Loop counter */
1068 int rc = SQLITE_OK; /* Return code */
1069 Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001070
drh9a324642003-09-06 20:12:01 +00001071 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001072 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001073 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001074
drh9cbf3422008-01-17 16:22:13 +00001075 /* Even though this opcode does not use dynamic strings for
1076 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001077 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001078 */
dan165921a2009-08-28 18:53:45 +00001079 releaseMemArray(pMem, 8);
danielk197718f41892004-05-22 07:27:46 +00001080
danielk19776c359f02008-11-21 16:58:03 +00001081 if( p->rc==SQLITE_NOMEM ){
1082 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1083 ** sqlite3_column_text16() failed. */
1084 db->mallocFailed = 1;
1085 return SQLITE_ERROR;
1086 }
1087
drh5cfa5842009-12-31 20:35:08 +00001088 /* When the number of output rows reaches nRow, that means the
1089 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1090 ** nRow is the sum of the number of rows in the main program, plus
1091 ** the sum of the number of rows in all trigger subprograms encountered
1092 ** so far. The nRow value will increase as new trigger subprograms are
1093 ** encountered, but p->pc will eventually catch up to nRow.
1094 */
dan165921a2009-08-28 18:53:45 +00001095 nRow = p->nOp;
1096 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001097 /* The first 8 memory cells are used for the result set. So we will
1098 ** commandeer the 9th cell to use as storage for an array of pointers
1099 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1100 ** cells. */
1101 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001102 pSub = &p->aMem[9];
1103 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001104 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1105 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001106 nSub = pSub->n/sizeof(Vdbe*);
1107 apSub = (SubProgram **)pSub->z;
1108 }
1109 for(i=0; i<nSub; i++){
1110 nRow += apSub[i]->nOp;
1111 }
1112 }
1113
drhecc92422005-09-10 16:46:12 +00001114 do{
1115 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001116 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1117 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001118 p->rc = SQLITE_OK;
1119 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001120 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001121 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001122 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001123 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001124 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001125 char *z;
dan165921a2009-08-28 18:53:45 +00001126 Op *pOp;
1127 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001128 /* The output line number is small enough that we are still in the
1129 ** main program. */
dan165921a2009-08-28 18:53:45 +00001130 pOp = &p->aOp[i];
1131 }else{
drh5cfa5842009-12-31 20:35:08 +00001132 /* We are currently listing subprograms. Figure out which one and
1133 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001134 int j;
1135 i -= p->nOp;
1136 for(j=0; i>=apSub[j]->nOp; j++){
1137 i -= apSub[j]->nOp;
1138 }
1139 pOp = &apSub[j]->aOp[i];
1140 }
danielk19770d78bae2008-01-03 07:09:48 +00001141 if( p->explain==1 ){
1142 pMem->flags = MEM_Int;
1143 pMem->type = SQLITE_INTEGER;
1144 pMem->u.i = i; /* Program counter */
1145 pMem++;
1146
1147 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
1148 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
1149 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001150 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001151 pMem->type = SQLITE_TEXT;
1152 pMem->enc = SQLITE_UTF8;
1153 pMem++;
dan165921a2009-08-28 18:53:45 +00001154
drh5cfa5842009-12-31 20:35:08 +00001155 /* When an OP_Program opcode is encounter (the only opcode that has
1156 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1157 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1158 ** has not already been seen.
1159 */
dan165921a2009-08-28 18:53:45 +00001160 if( pOp->p4type==P4_SUBPROGRAM ){
1161 int nByte = (nSub+1)*sizeof(SubProgram*);
1162 int j;
1163 for(j=0; j<nSub; j++){
1164 if( apSub[j]==pOp->p4.pProgram ) break;
1165 }
1166 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
1167 apSub = (SubProgram **)pSub->z;
1168 apSub[nSub++] = pOp->p4.pProgram;
1169 pSub->flags |= MEM_Blob;
1170 pSub->n = nSub*sizeof(SubProgram*);
1171 }
1172 }
danielk19770d78bae2008-01-03 07:09:48 +00001173 }
drheb2e1762004-05-27 01:53:56 +00001174
1175 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001176 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +00001177 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +00001178 pMem++;
1179
1180 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001181 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +00001182 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +00001183 pMem++;
1184
dan2ce22452010-11-08 19:01:16 +00001185 pMem->flags = MEM_Int;
1186 pMem->u.i = pOp->p3; /* P3 */
1187 pMem->type = SQLITE_INTEGER;
1188 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001189
danielk1977a7a8e142008-02-13 18:25:27 +00001190 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001191 assert( p->db->mallocFailed );
1192 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001193 }
1194 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
1195 z = displayP4(pOp, pMem->z, 32);
1196 if( z!=pMem->z ){
1197 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
1198 }else{
1199 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001200 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001201 pMem->enc = SQLITE_UTF8;
1202 }
drh9c054832004-05-31 18:51:57 +00001203 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +00001204 pMem++;
drheb2e1762004-05-27 01:53:56 +00001205
danielk19770d78bae2008-01-03 07:09:48 +00001206 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001207 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001208 assert( p->db->mallocFailed );
1209 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001210 }
1211 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001212 pMem->n = 2;
1213 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +00001214 pMem->type = SQLITE_TEXT;
1215 pMem->enc = SQLITE_UTF8;
1216 pMem++;
1217
drhaa9b8962008-01-08 02:57:55 +00001218#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +00001219 if( pOp->zComment ){
1220 pMem->flags = MEM_Str|MEM_Term;
1221 pMem->z = pOp->zComment;
drhea678832008-12-10 19:26:22 +00001222 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001223 pMem->enc = SQLITE_UTF8;
danielk19771e522b42008-09-16 09:09:19 +00001224 pMem->type = SQLITE_TEXT;
drh52391cb2008-02-14 23:44:13 +00001225 }else
drhaa9b8962008-01-08 02:57:55 +00001226#endif
drh52391cb2008-02-14 23:44:13 +00001227 {
1228 pMem->flags = MEM_Null; /* Comment */
1229 pMem->type = SQLITE_NULL;
1230 }
danielk19770d78bae2008-01-03 07:09:48 +00001231 }
1232
dan2ce22452010-11-08 19:01:16 +00001233 p->nResColumn = 8 - 4*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +00001234 p->rc = SQLITE_OK;
1235 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001236 }
drh826fb5a2004-02-14 23:59:57 +00001237 return rc;
drh9a324642003-09-06 20:12:01 +00001238}
drhb7f91642004-10-31 02:22:47 +00001239#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001240
drh7c4ac0c2007-04-05 11:25:58 +00001241#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001242/*
drh3f7d4e42004-07-24 14:35:58 +00001243** Print the SQL that was used to generate a VDBE program.
1244*/
1245void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +00001246 int nOp = p->nOp;
1247 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +00001248 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001249 pOp = &p->aOp[0];
1250 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +00001251 const char *z = pOp->p4.z;
danielk197778ca0e72009-01-20 16:53:39 +00001252 while( sqlite3Isspace(*z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +00001253 printf("SQL: [%s]\n", z);
1254 }
drh3f7d4e42004-07-24 14:35:58 +00001255}
drh7c4ac0c2007-04-05 11:25:58 +00001256#endif
drh3f7d4e42004-07-24 14:35:58 +00001257
drh602c2372007-03-01 00:29:13 +00001258#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1259/*
1260** Print an IOTRACE message showing SQL content.
1261*/
1262void sqlite3VdbeIOTraceSql(Vdbe *p){
1263 int nOp = p->nOp;
1264 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001265 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001266 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001267 pOp = &p->aOp[0];
1268 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001269 int i, j;
drh00a18e42007-08-13 11:10:34 +00001270 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001271 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001272 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001273 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001274 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001275 if( z[i-1]!=' ' ){
1276 z[j++] = ' ';
1277 }
1278 }else{
1279 z[j++] = z[i];
1280 }
1281 }
1282 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001283 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001284 }
1285}
1286#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1287
drhb2771ce2009-02-20 01:28:59 +00001288/*
drh4800b2e2009-12-08 15:35:22 +00001289** Allocate space from a fixed size buffer and return a pointer to
1290** that space. If insufficient space is available, return NULL.
1291**
1292** The pBuf parameter is the initial value of a pointer which will
1293** receive the new memory. pBuf is normally NULL. If pBuf is not
1294** NULL, it means that memory space has already been allocated and that
1295** this routine should not allocate any new memory. When pBuf is not
1296** NULL simply return pBuf. Only allocate new memory space when pBuf
1297** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001298**
1299** nByte is the number of bytes of space needed.
1300**
drh19875c82009-12-08 19:58:19 +00001301** *ppFrom points to available space and pEnd points to the end of the
1302** available space. When space is allocated, *ppFrom is advanced past
1303** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001304**
1305** *pnByte is a counter of the number of bytes of space that have failed
1306** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001307** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001308*/
drh4800b2e2009-12-08 15:35:22 +00001309static void *allocSpace(
1310 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001311 int nByte, /* Number of bytes to allocate */
1312 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001313 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001314 int *pnByte /* If allocation cannot be made, increment *pnByte */
1315){
drhea598cb2009-04-05 12:22:08 +00001316 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001317 if( pBuf ) return pBuf;
1318 nByte = ROUND8(nByte);
1319 if( &(*ppFrom)[nByte] <= pEnd ){
1320 pBuf = (void*)*ppFrom;
1321 *ppFrom += nByte;
1322 }else{
1323 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001324 }
drh4800b2e2009-12-08 15:35:22 +00001325 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001326}
drh602c2372007-03-01 00:29:13 +00001327
drh3f7d4e42004-07-24 14:35:58 +00001328/*
drh9a324642003-09-06 20:12:01 +00001329** Prepare a virtual machine for execution. This involves things such
1330** as allocating stack space and initializing the program counter.
1331** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +00001332** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +00001333**
1334** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
1335** VDBE_MAGIC_RUN.
danielk19776ab3a2e2009-02-19 14:39:25 +00001336**
1337** This function may be called more than once on a single virtual machine.
1338** The first call is made while compiling the SQL statement. Subsequent
1339** calls are made as part of the process of resetting a statement to be
1340** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor
1341** and isExplain parameters are only passed correct values the first time
1342** the function is called. On subsequent calls, from sqlite3_reset(), nVar
1343** is passed -1 and nMem, nCursor and isExplain are all passed zero.
drh9a324642003-09-06 20:12:01 +00001344*/
danielk19774adee202004-05-08 08:23:19 +00001345void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +00001346 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +00001347 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +00001348 int nMem, /* Number of memory cells to allocate */
1349 int nCursor, /* Number of cursors to allocate */
dan165921a2009-08-28 18:53:45 +00001350 int nArg, /* Maximum number of args in SubPrograms */
dane0af83a2009-09-08 19:15:01 +00001351 int isExplain, /* True if the EXPLAIN keywords is present */
shaneabc6b892009-09-10 19:09:03 +00001352 int usesStmtJournal /* True to set Vdbe.usesStmtJournal */
drh9a324642003-09-06 20:12:01 +00001353){
1354 int n;
danielk19771e536952007-08-16 10:09:01 +00001355 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001356
1357 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001358 assert( p->magic==VDBE_MAGIC_INIT );
1359
drhc16a03b2004-09-15 13:38:10 +00001360 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001361 */
drhc16a03b2004-09-15 13:38:10 +00001362 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001363
danielk197700e13612008-11-17 19:18:54 +00001364 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001365 p->magic = VDBE_MAGIC_RUN;
1366
danielk1977cd3e8f72008-03-25 09:47:35 +00001367 /* For each cursor required, also allocate a memory cell. Memory
1368 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1369 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001370 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001371 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1372 ** stores the blob of memory associated with cursor 1, etc.
1373 **
1374 ** See also: allocateCursor().
1375 */
1376 nMem += nCursor;
1377
danielk19776ab3a2e2009-02-19 14:39:25 +00001378 /* Allocate space for memory registers, SQL variables, VDBE cursors and
1379 ** an array to marshal SQL function arguments in. This is only done the
1380 ** first time this function is called for a given VDBE, not when it is
1381 ** being called from sqlite3_reset() to reset the virtual machine.
drh9a324642003-09-06 20:12:01 +00001382 */
drh5f82e3c2009-07-06 00:44:08 +00001383 if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){
drh19875c82009-12-08 19:58:19 +00001384 u8 *zCsr = (u8 *)&p->aOp[p->nOp]; /* Memory avaliable for alloation */
1385 u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc]; /* First byte past available mem */
1386 int nByte; /* How much extra memory needed */
1387
drh9cbf3422008-01-17 16:22:13 +00001388 resolveP2Values(p, &nArg);
shaneabc6b892009-09-10 19:09:03 +00001389 p->usesStmtJournal = (u8)usesStmtJournal;
drh9cbf3422008-01-17 16:22:13 +00001390 if( isExplain && nMem<10 ){
drhc46f5202008-11-04 14:25:06 +00001391 nMem = 10;
drh0f7eb612006-08-08 13:51:43 +00001392 }
drhbdd71912009-07-25 17:42:21 +00001393 memset(zCsr, 0, zEnd-zCsr);
drhea598cb2009-04-05 12:22:08 +00001394 zCsr += (zCsr - (u8*)0)&7;
1395 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhb2771ce2009-02-20 01:28:59 +00001396
drh19875c82009-12-08 19:58:19 +00001397 /* Memory for registers, parameters, cursor, etc, is allocated in two
1398 ** passes. On the first pass, we try to reuse unused space at the
1399 ** end of the opcode array. If we are unable to satisfy all memory
1400 ** requirements by reusing the opcode array tail, then the second
1401 ** pass will fill in the rest using a fresh allocation.
1402 **
1403 ** This two-pass approach that reuses as much memory as possible from
1404 ** the leftover space at the end of the opcode array can significantly
1405 ** reduce the amount of memory held by a prepared statement.
1406 */
drhb2771ce2009-02-20 01:28:59 +00001407 do {
drhb2771ce2009-02-20 01:28:59 +00001408 nByte = 0;
drh4800b2e2009-12-08 15:35:22 +00001409 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1410 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1411 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1412 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1413 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1414 &zCsr, zEnd, &nByte);
drhb2771ce2009-02-20 01:28:59 +00001415 if( nByte ){
drhbdd71912009-07-25 17:42:21 +00001416 p->pFree = sqlite3DbMallocZero(db, nByte);
drhb2771ce2009-02-20 01:28:59 +00001417 }
1418 zCsr = p->pFree;
1419 zEnd = &zCsr[nByte];
1420 }while( nByte && !db->mallocFailed );
1421
shane36840fd2009-06-26 16:32:13 +00001422 p->nCursor = (u16)nCursor;
drhb2771ce2009-02-20 01:28:59 +00001423 if( p->aVar ){
drh8677d302009-11-04 13:17:14 +00001424 p->nVar = (ynVar)nVar;
drh290c1942004-08-21 17:54:45 +00001425 for(n=0; n<nVar; n++){
1426 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001427 p->aVar[n].db = db;
1428 }
drhb2771ce2009-02-20 01:28:59 +00001429 }
1430 if( p->aMem ){
1431 p->aMem--; /* aMem[] goes from 1..nMem */
1432 p->nMem = nMem; /* not from 0..nMem-1 */
drh9cbf3422008-01-17 16:22:13 +00001433 for(n=1; n<=nMem; n++){
1434 p->aMem[n].flags = MEM_Null;
1435 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +00001436 }
danielk197754db47e2004-05-19 10:36:43 +00001437 }
drh82a48512003-09-06 22:45:20 +00001438 }
drh9cbf3422008-01-17 16:22:13 +00001439#ifdef SQLITE_DEBUG
1440 for(n=1; n<p->nMem; n++){
1441 assert( p->aMem[n].db==db );
danielk1977b3bce662005-01-29 08:32:43 +00001442 }
drh9cbf3422008-01-17 16:22:13 +00001443#endif
drh9a324642003-09-06 20:12:01 +00001444
danielk19771d850a72004-05-31 08:26:49 +00001445 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001446 p->rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001447 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001448 p->explain |= isExplain;
1449 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001450 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001451 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001452 p->minWriteFileFormat = 255;
danielk1977bd434552009-03-18 10:33:00 +00001453 p->iStatement = 0;
dan69083432010-04-29 22:57:56 +00001454 p->nFkConstraint = 0;
drh9a324642003-09-06 20:12:01 +00001455#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001456 {
1457 int i;
1458 for(i=0; i<p->nOp; i++){
1459 p->aOp[i].cnt = 0;
1460 p->aOp[i].cycles = 0;
1461 }
drh9a324642003-09-06 20:12:01 +00001462 }
1463#endif
1464}
1465
drh9a324642003-09-06 20:12:01 +00001466/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001467** Close a VDBE cursor and release all the resources that cursor
1468** happens to hold.
drh9a324642003-09-06 20:12:01 +00001469*/
drhdfe88ec2008-11-03 20:55:06 +00001470void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001471 if( pCx==0 ){
1472 return;
1473 }
drh9a324642003-09-06 20:12:01 +00001474 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001475 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001476 /* The pCx->pCursor will be close automatically, if it exists, by
1477 ** the call above. */
1478 }else if( pCx->pCursor ){
1479 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001480 }
drh9eff6162006-06-12 21:59:13 +00001481#ifndef SQLITE_OMIT_VIRTUALTABLE
1482 if( pCx->pVtabCursor ){
1483 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001484 const sqlite3_module *pModule = pCx->pModule;
1485 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001486 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001487 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001488 }
1489#endif
drh9a324642003-09-06 20:12:01 +00001490}
1491
dan65a7cd12009-09-01 12:16:01 +00001492/*
1493** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1494** is used, for example, when a trigger sub-program is halted to restore
1495** control to the main program.
1496*/
dan165921a2009-08-28 18:53:45 +00001497int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1498 Vdbe *v = pFrame->v;
1499 v->aOp = pFrame->aOp;
1500 v->nOp = pFrame->nOp;
1501 v->aMem = pFrame->aMem;
1502 v->nMem = pFrame->nMem;
1503 v->apCsr = pFrame->apCsr;
1504 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001505 v->db->lastRowid = pFrame->lastRowid;
1506 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001507 return pFrame->pc;
1508}
1509
drh9a324642003-09-06 20:12:01 +00001510/*
drh5f82e3c2009-07-06 00:44:08 +00001511** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001512**
1513** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1514** cell array. This is necessary as the memory cell array may contain
1515** pointers to VdbeFrame objects, which may in turn contain pointers to
1516** open cursors.
drh9a324642003-09-06 20:12:01 +00001517*/
drh5f82e3c2009-07-06 00:44:08 +00001518static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001519 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001520 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001521 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1522 sqlite3VdbeFrameRestore(pFrame);
1523 }
1524 p->pFrame = 0;
1525 p->nFrame = 0;
1526
dan523a0872009-08-31 05:23:32 +00001527 if( p->apCsr ){
1528 int i;
1529 for(i=0; i<p->nCursor; i++){
1530 VdbeCursor *pC = p->apCsr[i];
1531 if( pC ){
1532 sqlite3VdbeFreeCursor(p, pC);
1533 p->apCsr[i] = 0;
1534 }
danielk1977be718892006-06-23 08:05:19 +00001535 }
drh9a324642003-09-06 20:12:01 +00001536 }
dan523a0872009-08-31 05:23:32 +00001537 if( p->aMem ){
1538 releaseMemArray(&p->aMem[1], p->nMem);
1539 }
dan27106572010-12-01 08:04:47 +00001540 while( p->pDelFrame ){
1541 VdbeFrame *pDel = p->pDelFrame;
1542 p->pDelFrame = pDel->pParent;
1543 sqlite3VdbeFrameDelete(pDel);
1544 }
drh9a324642003-09-06 20:12:01 +00001545}
1546
1547/*
drh9a324642003-09-06 20:12:01 +00001548** Clean up the VM after execution.
1549**
1550** This routine will automatically close any cursors, lists, and/or
1551** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001552** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001553*/
drhc890fec2008-08-01 20:10:08 +00001554static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001555 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001556
1557#ifdef SQLITE_DEBUG
1558 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1559 ** Vdbe.aMem[] arrays have already been cleaned up. */
1560 int i;
dan523a0872009-08-31 05:23:32 +00001561 for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
1562 for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
dan165921a2009-08-28 18:53:45 +00001563#endif
1564
drh633e6d52008-07-28 19:34:53 +00001565 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001566 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001567 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001568}
1569
1570/*
danielk197722322fd2004-05-25 23:35:17 +00001571** Set the number of result columns that will be returned by this SQL
1572** statement. This is now set at compile time, rather than during
1573** execution of the vdbe program so that sqlite3_column_count() can
1574** be called on an SQL statement before sqlite3_step().
1575*/
1576void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001577 Mem *pColName;
1578 int n;
drh633e6d52008-07-28 19:34:53 +00001579 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001580
drhc890fec2008-08-01 20:10:08 +00001581 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001582 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001583 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001584 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001585 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001586 if( p->aColName==0 ) return;
1587 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001588 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001589 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001590 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001591 }
danielk197722322fd2004-05-25 23:35:17 +00001592}
1593
1594/*
danielk19773cf86062004-05-26 10:11:05 +00001595** Set the name of the idx'th column to be returned by the SQL statement.
1596** zName must be a pointer to a nul terminated string.
1597**
1598** This call must be made after a call to sqlite3VdbeSetNumCols().
1599**
danielk197710fb7492008-10-31 10:53:22 +00001600** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1601** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1602** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001603*/
danielk197710fb7492008-10-31 10:53:22 +00001604int sqlite3VdbeSetColName(
1605 Vdbe *p, /* Vdbe being configured */
1606 int idx, /* Index of column zName applies to */
1607 int var, /* One of the COLNAME_* constants */
1608 const char *zName, /* Pointer to buffer containing name */
1609 void (*xDel)(void*) /* Memory management strategy for zName */
1610){
danielk19773cf86062004-05-26 10:11:05 +00001611 int rc;
1612 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001613 assert( idx<p->nResColumn );
1614 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001615 if( p->db->mallocFailed ){
1616 assert( !zName || xDel!=SQLITE_DYNAMIC );
1617 return SQLITE_NOMEM;
1618 }
drh76ff3a02004-09-24 22:32:30 +00001619 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001620 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001621 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001622 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001623 return rc;
1624}
1625
danielk197713adf8a2004-06-03 16:08:41 +00001626/*
1627** A read or write transaction may or may not be active on database handle
1628** db. If a transaction is active, commit it. If there is a
1629** write-transaction spanning more than one database file, this routine
1630** takes care of the master journal trickery.
1631*/
danielk19773e3a84d2008-08-01 17:37:40 +00001632static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001633 int i;
1634 int nTrans = 0; /* Number of databases with an active write-transaction */
1635 int rc = SQLITE_OK;
1636 int needXcommit = 0;
1637
shane36840fd2009-06-26 16:32:13 +00001638#ifdef SQLITE_OMIT_VIRTUALTABLE
1639 /* With this option, sqlite3VtabSync() is defined to be simply
1640 ** SQLITE_OK so p is not used.
1641 */
1642 UNUSED_PARAMETER(p);
1643#endif
1644
danielk19775bd270b2006-07-25 15:14:52 +00001645 /* Before doing anything else, call the xSync() callback for any
1646 ** virtual module tables written in this transaction. This has to
1647 ** be done before determining whether a master journal file is
1648 ** required, as an xSync() callback may add an attached database
1649 ** to the transaction.
1650 */
danielk19773e3a84d2008-08-01 17:37:40 +00001651 rc = sqlite3VtabSync(db, &p->zErrMsg);
danielk19775bd270b2006-07-25 15:14:52 +00001652
1653 /* This loop determines (a) if the commit hook should be invoked and
1654 ** (b) how many database files have open write transactions, not
1655 ** including the temp database. (b) is important because if more than
1656 ** one database file has an open write transaction, a master journal
1657 ** file is required for an atomic commit.
1658 */
drhabfb62f2010-07-30 11:20:35 +00001659 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001660 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001661 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001662 needXcommit = 1;
1663 if( i!=1 ) nTrans++;
drhabfb62f2010-07-30 11:20:35 +00001664 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
danielk197713adf8a2004-06-03 16:08:41 +00001665 }
1666 }
drhabfb62f2010-07-30 11:20:35 +00001667 if( rc!=SQLITE_OK ){
1668 return rc;
1669 }
danielk197713adf8a2004-06-03 16:08:41 +00001670
1671 /* If there are any write-transactions at all, invoke the commit hook */
1672 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001673 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001674 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001675 return SQLITE_CONSTRAINT;
1676 }
1677 }
1678
danielk197740b38dc2004-06-26 08:38:24 +00001679 /* The simple case - no more than one database file (not counting the
1680 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001681 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001682 **
danielk197740b38dc2004-06-26 08:38:24 +00001683 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001684 ** string, it means the main database is :memory: or a temp file. In
1685 ** that case we do not support atomic multi-file commits, so use the
1686 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001687 */
drhea678832008-12-10 19:26:22 +00001688 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1689 || nTrans<=1
1690 ){
danielk197704103022009-02-03 16:51:24 +00001691 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001692 Btree *pBt = db->aDb[i].pBt;
1693 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001694 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001695 }
1696 }
1697
drh80e35f42007-03-30 14:06:34 +00001698 /* Do the commit only if all databases successfully complete phase 1.
1699 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1700 ** IO error while deleting or truncating a journal file. It is unlikely,
1701 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001702 */
1703 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1704 Btree *pBt = db->aDb[i].pBt;
1705 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001706 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001707 }
danielk1977979f38e2007-03-27 16:19:51 +00001708 }
1709 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001710 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001711 }
1712 }
1713
1714 /* The complex case - There is a multi-file write-transaction active.
1715 ** This requires a master journal file to ensure the transaction is
1716 ** committed atomicly.
1717 */
danielk197744ee5bf2005-05-27 09:41:12 +00001718#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001719 else{
danielk1977b4b47412007-08-17 15:53:36 +00001720 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001721 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001722 char *zMaster = 0; /* File-name for the master journal */
1723 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001724 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001725 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001726 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001727
1728 /* Select a master journal file name */
1729 do {
drhdc5ea5c2008-12-10 17:19:59 +00001730 u32 iRandom;
drh633e6d52008-07-28 19:34:53 +00001731 sqlite3DbFree(db, zMaster);
drhdc5ea5c2008-12-10 17:19:59 +00001732 sqlite3_randomness(sizeof(iRandom), &iRandom);
1733 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001734 if( !zMaster ){
1735 return SQLITE_NOMEM;
1736 }
danielk1977861f7452008-06-05 11:39:11 +00001737 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1738 }while( rc==SQLITE_OK && res );
1739 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001740 /* Open the master journal. */
1741 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1742 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1743 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1744 );
1745 }
danielk197713adf8a2004-06-03 16:08:41 +00001746 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001747 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001748 return rc;
1749 }
1750
1751 /* Write the name of each database file in the transaction into the new
1752 ** master journal file. If an error occurs at this point close
1753 ** and delete the master journal file. All the individual journal files
1754 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001755 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001756 */
danielk19771e536952007-08-16 10:09:01 +00001757 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001758 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001759 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001760 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00001761 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00001762 continue; /* Ignore TEMP and :memory: databases */
1763 }
drh8c96a6e2010-08-31 01:09:15 +00001764 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00001765 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1766 needSync = 1;
1767 }
drhea678832008-12-10 19:26:22 +00001768 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
1769 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001770 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001771 sqlite3OsCloseFree(pMaster);
1772 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001773 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001774 return rc;
1775 }
1776 }
1777 }
1778
danielk19779663b8f2007-08-24 11:52:28 +00001779 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1780 ** flag is set this is not required.
1781 */
danielk1977bea2a942009-01-20 17:06:27 +00001782 if( needSync
1783 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
1784 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
1785 ){
danielk1977fee2d252007-08-18 10:59:19 +00001786 sqlite3OsCloseFree(pMaster);
1787 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001788 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001789 return rc;
1790 }
drhc9e06862004-06-09 20:03:08 +00001791
danielk197713adf8a2004-06-03 16:08:41 +00001792 /* Sync all the db files involved in the transaction. The same call
1793 ** sets the master journal pointer in each individual journal. If
1794 ** an error occurs here, do not delete the master journal file.
1795 **
drh80e35f42007-03-30 14:06:34 +00001796 ** If the error occurs during the first call to
1797 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1798 ** master journal file will be orphaned. But we cannot delete it,
1799 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00001800 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00001801 */
danielk19775bd270b2006-07-25 15:14:52 +00001802 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001803 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001804 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001805 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001806 }
1807 }
danielk1977fee2d252007-08-18 10:59:19 +00001808 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00001809 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00001810 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001811 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001812 return rc;
1813 }
danielk197713adf8a2004-06-03 16:08:41 +00001814
danielk1977962398d2004-06-14 09:35:16 +00001815 /* Delete the master journal file. This commits the transaction. After
1816 ** doing this the directory is synced again before any individual
1817 ** transaction files are deleted.
1818 */
danielk1977fee2d252007-08-18 10:59:19 +00001819 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00001820 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00001821 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001822 if( rc ){
1823 return rc;
1824 }
danielk197713adf8a2004-06-03 16:08:41 +00001825
1826 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001827 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1828 ** deleting or truncating journals. If something goes wrong while
1829 ** this is happening we don't really care. The integrity of the
1830 ** transaction is already guaranteed, but some stray 'cold' journals
1831 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001832 */
danielk1977979f38e2007-03-27 16:19:51 +00001833 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001834 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001835 for(i=0; i<db->nDb; i++){
1836 Btree *pBt = db->aDb[i].pBt;
1837 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001838 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001839 }
1840 }
danielk19772d1d86f2008-06-20 14:59:51 +00001841 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001842 enable_simulated_io_errors();
1843
danielk1977f9e7dda2006-06-16 16:08:53 +00001844 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001845 }
danielk197744ee5bf2005-05-27 09:41:12 +00001846#endif
danielk1977026d2702004-06-14 13:14:59 +00001847
drh2ac3ee92004-06-07 16:27:46 +00001848 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001849}
1850
danielk19771d850a72004-05-31 08:26:49 +00001851/*
1852** This routine checks that the sqlite3.activeVdbeCnt count variable
1853** matches the number of vdbe's in the list sqlite3.pVdbe that are
1854** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001855** This is an internal self-check only - it is not an essential processing
1856** step.
danielk19771d850a72004-05-31 08:26:49 +00001857**
1858** This is a no-op if NDEBUG is defined.
1859*/
1860#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001861static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001862 Vdbe *p;
1863 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00001864 int nWrite = 0;
danielk19771d850a72004-05-31 08:26:49 +00001865 p = db->pVdbe;
1866 while( p ){
drh92f02c32004-09-02 14:57:08 +00001867 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001868 cnt++;
drhad4a4b82008-11-05 16:37:34 +00001869 if( p->readOnly==0 ) nWrite++;
danielk19771d850a72004-05-31 08:26:49 +00001870 }
1871 p = p->pNext;
1872 }
danielk19771d850a72004-05-31 08:26:49 +00001873 assert( cnt==db->activeVdbeCnt );
drhad4a4b82008-11-05 16:37:34 +00001874 assert( nWrite==db->writeVdbeCnt );
danielk19771d850a72004-05-31 08:26:49 +00001875}
1876#else
1877#define checkActiveVdbeCnt(x)
1878#endif
1879
danielk19773cf86062004-05-26 10:11:05 +00001880/*
drhfb982642007-08-30 01:19:59 +00001881** For every Btree that in database connection db which
1882** has been modified, "trip" or invalidate each cursor in
1883** that Btree might have been modified so that the cursor
1884** can never be used again. This happens when a rollback
1885*** occurs. We have to trip all the other cursors, even
1886** cursor from other VMs in different database connections,
1887** so that none of them try to use the data at which they
1888** were pointing and which now may have been changed due
1889** to the rollback.
1890**
1891** Remember that a rollback can delete tables complete and
1892** reorder rootpages. So it is not sufficient just to save
1893** the state of the cursor. We have to invalidate the cursor
1894** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001895*/
drhade6c9c2007-11-24 10:23:44 +00001896static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001897 int i;
1898 for(i=0; i<db->nDb; i++){
1899 Btree *p = db->aDb[i].pBt;
1900 if( p && sqlite3BtreeIsInTrans(p) ){
1901 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1902 }
danielk1977be718892006-06-23 08:05:19 +00001903 }
1904}
1905
1906/*
danielk1977bd434552009-03-18 10:33:00 +00001907** If the Vdbe passed as the first argument opened a statement-transaction,
1908** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
1909** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
1910** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
1911** statement transaction is commtted.
1912**
1913** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
1914** Otherwise SQLITE_OK.
1915*/
1916int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00001917 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00001918 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00001919
danielk1977e4948172009-07-17 17:25:43 +00001920 /* If p->iStatement is greater than zero, then this Vdbe opened a
1921 ** statement transaction that should be closed here. The only exception
1922 ** is that an IO error may have occured, causing an emergency rollback.
1923 ** In this case (db->nStatement==0), and there is nothing to do.
1924 */
1925 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00001926 int i;
1927 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00001928
1929 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
1930 assert( db->nStatement>0 );
1931 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
1932
1933 for(i=0; i<db->nDb; i++){
1934 int rc2 = SQLITE_OK;
1935 Btree *pBt = db->aDb[i].pBt;
1936 if( pBt ){
1937 if( eOp==SAVEPOINT_ROLLBACK ){
1938 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
1939 }
1940 if( rc2==SQLITE_OK ){
1941 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
1942 }
1943 if( rc==SQLITE_OK ){
1944 rc = rc2;
1945 }
1946 }
1947 }
1948 db->nStatement--;
1949 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00001950
1951 /* If the statement transaction is being rolled back, also restore the
1952 ** database handles deferred constraint counter to the value it had when
1953 ** the statement transaction was opened. */
1954 if( eOp==SAVEPOINT_ROLLBACK ){
1955 db->nDeferredCons = p->nStmtDefCons;
1956 }
danielk1977bd434552009-03-18 10:33:00 +00001957 }
1958 return rc;
1959}
1960
1961/*
danielk1977f7590db2009-04-10 12:55:16 +00001962** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1963** this routine obtains the mutex associated with each BtShared structure
1964** that may be accessed by the VM passed as an argument. In doing so it
1965** sets the BtShared.db member of each of the BtShared structures, ensuring
1966** that the correct busy-handler callback is invoked if required.
1967**
1968** If SQLite is not threadsafe but does support shared-cache mode, then
1969** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
1970** of all of BtShared structures accessible via the database handle
1971** associated with the VM. Of course only a subset of these structures
1972** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
1973** that subset out, but there is no advantage to doing so.
1974**
1975** If SQLite is not threadsafe and does not support shared-cache mode, this
1976** function is a no-op.
1977*/
1978#ifndef SQLITE_OMIT_SHARED_CACHE
1979void sqlite3VdbeMutexArrayEnter(Vdbe *p){
1980#if SQLITE_THREADSAFE
1981 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1982#else
1983 sqlite3BtreeEnterAll(p->db);
1984#endif
1985}
1986#endif
1987
1988/*
dan1da40a32009-09-19 17:00:31 +00001989** This function is called when a transaction opened by the database
1990** handle associated with the VM passed as an argument is about to be
1991** committed. If there are outstanding deferred foreign key constraint
1992** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
1993**
1994** If there are outstanding FK violations and this function returns
1995** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
1996** an error message to it. Then return SQLITE_ERROR.
1997*/
1998#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00001999int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002000 sqlite3 *db = p->db;
dan32b09f22009-09-23 17:29:59 +00002001 if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
dan1da40a32009-09-19 17:00:31 +00002002 p->rc = SQLITE_CONSTRAINT;
dan32b09f22009-09-23 17:29:59 +00002003 p->errorAction = OE_Abort;
dan1da40a32009-09-19 17:00:31 +00002004 sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
2005 return SQLITE_ERROR;
2006 }
2007 return SQLITE_OK;
2008}
2009#endif
2010
2011/*
drh92f02c32004-09-02 14:57:08 +00002012** This routine is called the when a VDBE tries to halt. If the VDBE
2013** has made changes and is in autocommit mode, then commit those
2014** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002015**
drh92f02c32004-09-02 14:57:08 +00002016** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002017** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2018** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002019**
2020** Return an error code. If the commit could not complete because of
2021** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2022** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002023*/
drhff0587c2007-08-29 17:43:19 +00002024int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002025 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002026 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002027
2028 /* This function contains the logic that determines if a statement or
2029 ** transaction will be committed or rolled back as a result of the
2030 ** execution of this virtual machine.
2031 **
drh71b890a2007-10-03 15:30:52 +00002032 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002033 **
drh71b890a2007-10-03 15:30:52 +00002034 ** SQLITE_NOMEM
2035 ** SQLITE_IOERR
2036 ** SQLITE_FULL
2037 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002038 **
drh71b890a2007-10-03 15:30:52 +00002039 ** Then the internal cache might have been left in an inconsistent
2040 ** state. We need to rollback the statement transaction, if there is
2041 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002042 */
drh9a324642003-09-06 20:12:01 +00002043
drh17435752007-08-16 04:30:38 +00002044 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002045 p->rc = SQLITE_NOMEM;
2046 }
drh5f82e3c2009-07-06 00:44:08 +00002047 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002048 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002049 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002050 }
danielk19771d850a72004-05-31 08:26:49 +00002051 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002052
danielk197707cb5602006-01-20 10:55:05 +00002053 /* No commit or rollback needed if the program never started */
2054 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00002055 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002056 int eStatementOp = 0;
2057 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002058
2059 /* Lock all btrees used by the statement */
danielk1977f7590db2009-04-10 12:55:16 +00002060 sqlite3VdbeMutexArrayEnter(p);
drhff0587c2007-08-29 17:43:19 +00002061
drh71b890a2007-10-03 15:30:52 +00002062 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002063 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002064 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002065 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002066 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002067 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002068 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2069 ** no rollback is necessary. Otherwise, at least a savepoint
2070 ** transaction must be rolled back to restore the database to a
2071 ** consistent state.
2072 **
2073 ** Even if the statement is read-only, it is important to perform
2074 ** a statement or transaction rollback operation. If the error
2075 ** occured while writing to the journal, sub-journal or database
2076 ** file as part of an effort to free up cache space (see function
2077 ** pagerStress() in pager.c), the rollback is required to restore
2078 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002079 */
drhad4a4b82008-11-05 16:37:34 +00002080 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002081 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002082 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002083 }else{
2084 /* We are forced to roll back the active transaction. Before doing
2085 ** so, abort any other statements this handle currently has active.
2086 */
drhfb982642007-08-30 01:19:59 +00002087 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00002088 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00002089 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002090 db->autoCommit = 1;
2091 }
danielk1977261919c2005-12-06 12:52:59 +00002092 }
2093 }
dan32b09f22009-09-23 17:29:59 +00002094
2095 /* Check for immediate foreign key violations. */
2096 if( p->rc==SQLITE_OK ){
2097 sqlite3VdbeCheckFk(p, 0);
2098 }
danielk197707cb5602006-01-20 10:55:05 +00002099
danielk1977bd434552009-03-18 10:33:00 +00002100 /* If the auto-commit flag is set and this is the only active writer
2101 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002102 **
2103 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002104 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002105 */
danielk1977093e0f62008-11-13 18:00:14 +00002106 if( !sqlite3VtabInSync(db)
2107 && db->autoCommit
2108 && db->writeVdbeCnt==(p->readOnly==0)
2109 ){
danielk197707cb5602006-01-20 10:55:05 +00002110 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002111 rc = sqlite3VdbeCheckFk(p, 1);
2112 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002113 if( NEVER(p->readOnly) ){
dan19611b12011-01-24 16:00:58 +00002114 sqlite3BtreeMutexArrayLeave(&p->aMutex);
2115 return SQLITE_ERROR;
2116 }
2117 rc = SQLITE_CONSTRAINT;
2118 }else{
2119 /* The auto-commit flag is true, the vdbe program was successful
2120 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2121 ** key constraints to hold up the transaction. This means a commit
2122 ** is required. */
2123 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002124 }
dan19611b12011-01-24 16:00:58 +00002125 if( rc==SQLITE_BUSY && p->readOnly ){
drhff0587c2007-08-29 17:43:19 +00002126 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00002127 return SQLITE_BUSY;
2128 }else if( rc!=SQLITE_OK ){
2129 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00002130 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00002131 }else{
dan1da40a32009-09-19 17:00:31 +00002132 db->nDeferredCons = 0;
danielk197707cb5602006-01-20 10:55:05 +00002133 sqlite3CommitInternalChanges(db);
2134 }
2135 }else{
danielk197797a227c2006-01-20 16:32:04 +00002136 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00002137 }
danielk1977bd434552009-03-18 10:33:00 +00002138 db->nStatement = 0;
2139 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002140 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002141 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002142 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002143 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002144 }else{
drhfb982642007-08-30 01:19:59 +00002145 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00002146 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00002147 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002148 db->autoCommit = 1;
2149 }
danielk19771d850a72004-05-31 08:26:49 +00002150 }
danielk197707cb5602006-01-20 10:55:05 +00002151
danielk1977bd434552009-03-18 10:33:00 +00002152 /* If eStatementOp is non-zero, then a statement transaction needs to
2153 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2154 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002155 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2156 ** current statement error code.
2157 **
2158 ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp
2159 ** is SAVEPOINT_ROLLBACK. But if p->rc==SQLITE_OK then eStatementOp
2160 ** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in
2161 ** the following code.
danielk197707cb5602006-01-20 10:55:05 +00002162 */
danielk1977bd434552009-03-18 10:33:00 +00002163 if( eStatementOp ){
2164 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002165 if( rc ){
2166 assert( eStatementOp==SAVEPOINT_ROLLBACK );
2167 if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){
2168 p->rc = rc;
2169 sqlite3DbFree(db, p->zErrMsg);
2170 p->zErrMsg = 0;
2171 }
2172 invalidateCursorsOnModifiedBtrees(db);
2173 sqlite3RollbackAll(db);
2174 sqlite3CloseSavepoints(db);
2175 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002176 }
danielk197777d83ba2004-05-31 10:08:14 +00002177 }
danielk197707cb5602006-01-20 10:55:05 +00002178
danielk1977bd434552009-03-18 10:33:00 +00002179 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2180 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002181 */
drh6be240e2009-07-14 02:33:02 +00002182 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002183 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002184 sqlite3VdbeSetChanges(db, p->nChange);
2185 }else{
2186 sqlite3VdbeSetChanges(db, 0);
2187 }
2188 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002189 }
danielk197707cb5602006-01-20 10:55:05 +00002190
2191 /* Rollback or commit any schema changes that occurred. */
2192 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
2193 sqlite3ResetInternalSchema(db, 0);
2194 db->flags = (db->flags | SQLITE_InternChanges);
2195 }
drhff0587c2007-08-29 17:43:19 +00002196
2197 /* Release the locks */
2198 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00002199 }
danielk19771d850a72004-05-31 08:26:49 +00002200
danielk197765fd59f2006-06-24 11:51:33 +00002201 /* We have successfully halted and closed the VM. Record this fact. */
2202 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002203 db->activeVdbeCnt--;
drhad4a4b82008-11-05 16:37:34 +00002204 if( !p->readOnly ){
2205 db->writeVdbeCnt--;
2206 }
2207 assert( db->activeVdbeCnt>=db->writeVdbeCnt );
drh9a324642003-09-06 20:12:01 +00002208 }
drh92f02c32004-09-02 14:57:08 +00002209 p->magic = VDBE_MAGIC_HALT;
2210 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002211 if( p->db->mallocFailed ){
2212 p->rc = SQLITE_NOMEM;
2213 }
danielk19771d850a72004-05-31 08:26:49 +00002214
danielk1977404ca072009-03-16 13:19:36 +00002215 /* If the auto-commit flag is set to true, then any locks that were held
2216 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2217 ** to invoke any required unlock-notify callbacks.
2218 */
2219 if( db->autoCommit ){
2220 sqlite3ConnectionUnlocked(db);
2221 }
2222
danielk1977bd434552009-03-18 10:33:00 +00002223 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002224 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002225}
drh4cf7c7f2007-08-28 23:28:07 +00002226
drh92f02c32004-09-02 14:57:08 +00002227
2228/*
drh3c23a882007-01-09 14:01:13 +00002229** Each VDBE holds the result of the most recent sqlite3_step() call
2230** in p->rc. This routine sets that result back to SQLITE_OK.
2231*/
2232void sqlite3VdbeResetStepResult(Vdbe *p){
2233 p->rc = SQLITE_OK;
2234}
2235
2236/*
drh92f02c32004-09-02 14:57:08 +00002237** Clean up a VDBE after execution but do not delete the VDBE just yet.
2238** Write any error messages into *pzErrMsg. Return the result code.
2239**
2240** After this routine is run, the VDBE should be ready to be executed
2241** again.
2242**
2243** To look at it another way, this routine resets the state of the
2244** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2245** VDBE_MAGIC_INIT.
2246*/
drhc890fec2008-08-01 20:10:08 +00002247int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002248 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002249 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002250
2251 /* If the VM did not run to completion or if it encountered an
2252 ** error, then it might not have been halted properly. So halt
2253 ** it now.
2254 */
2255 sqlite3VdbeHalt(p);
2256
drhfb7e7652005-01-24 00:28:42 +00002257 /* If the VDBE has be run even partially, then transfer the error code
2258 ** and error message from the VDBE into the main database structure. But
2259 ** if the VDBE has just been set to run but has not actually executed any
2260 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002261 */
drhfb7e7652005-01-24 00:28:42 +00002262 if( p->pc>=0 ){
2263 if( p->zErrMsg ){
danielk19779ff3f3f2008-10-11 17:51:38 +00002264 sqlite3BeginBenignMalloc();
drh633e6d52008-07-28 19:34:53 +00002265 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19779ff3f3f2008-10-11 17:51:38 +00002266 sqlite3EndBenignMalloc();
danielk197797a227c2006-01-20 16:32:04 +00002267 db->errCode = p->rc;
drh633e6d52008-07-28 19:34:53 +00002268 sqlite3DbFree(db, p->zErrMsg);
drhfb7e7652005-01-24 00:28:42 +00002269 p->zErrMsg = 0;
2270 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00002271 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00002272 }else{
drh4ac285a2006-09-15 07:28:50 +00002273 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00002274 }
drh4611d922010-02-25 14:47:01 +00002275 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002276 }else if( p->rc && p->expired ){
2277 /* The expired flag was set on the VDBE before the first call
2278 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2279 ** called), set the database error in this case as well.
2280 */
drh4ac285a2006-09-15 07:28:50 +00002281 sqlite3Error(db, p->rc, 0);
drh633e6d52008-07-28 19:34:53 +00002282 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2283 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002284 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002285 }
2286
2287 /* Reclaim all memory used by the VDBE
2288 */
drhc890fec2008-08-01 20:10:08 +00002289 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002290
2291 /* Save profiling information from this VDBE run.
2292 */
drh9a324642003-09-06 20:12:01 +00002293#ifdef VDBE_PROFILE
2294 {
2295 FILE *out = fopen("vdbe_profile.out", "a");
2296 if( out ){
2297 int i;
2298 fprintf(out, "---- ");
2299 for(i=0; i<p->nOp; i++){
2300 fprintf(out, "%02x", p->aOp[i].opcode);
2301 }
2302 fprintf(out, "\n");
2303 for(i=0; i<p->nOp; i++){
2304 fprintf(out, "%6d %10lld %8lld ",
2305 p->aOp[i].cnt,
2306 p->aOp[i].cycles,
2307 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2308 );
danielk19774adee202004-05-08 08:23:19 +00002309 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002310 }
2311 fclose(out);
2312 }
2313 }
2314#endif
2315 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002316 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002317}
drh92f02c32004-09-02 14:57:08 +00002318
drh9a324642003-09-06 20:12:01 +00002319/*
2320** Clean up and delete a VDBE after execution. Return an integer which is
2321** the result code. Write any error message text into *pzErrMsg.
2322*/
danielk19779e6db7d2004-06-21 08:18:51 +00002323int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002324 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002325 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002326 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002327 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002328 }
danielk19774adee202004-05-08 08:23:19 +00002329 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002330 return rc;
2331}
2332
2333/*
drhf92c7ff2004-06-19 15:40:23 +00002334** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00002335** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00002336** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00002337** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00002338*/
2339void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
2340 int i;
2341 for(i=0; i<pVdbeFunc->nAux; i++){
2342 struct AuxData *pAux = &pVdbeFunc->apAux[i];
drh3500ed62009-05-05 15:46:43 +00002343 if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
drhf92c7ff2004-06-19 15:40:23 +00002344 if( pAux->xDelete ){
2345 pAux->xDelete(pAux->pAux);
2346 }
2347 pAux->pAux = 0;
2348 }
2349 }
2350}
2351
2352/*
dand46def72010-07-24 11:28:28 +00002353** Free all memory associated with the Vdbe passed as the second argument.
2354** The difference between this function and sqlite3VdbeDelete() is that
2355** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
2356** the database connection.
2357*/
2358void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002359 SubProgram *pSub, *pNext;
dand46def72010-07-24 11:28:28 +00002360 assert( p->db==0 || p->db==db );
2361 releaseMemArray(p->aVar, p->nVar);
2362 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002363 for(pSub=p->pProgram; pSub; pSub=pNext){
2364 pNext = pSub->pNext;
2365 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2366 sqlite3DbFree(db, pSub);
2367 }
dand46def72010-07-24 11:28:28 +00002368 vdbeFreeOpArray(db, p->aOp, p->nOp);
2369 sqlite3DbFree(db, p->aLabel);
2370 sqlite3DbFree(db, p->aColName);
2371 sqlite3DbFree(db, p->zSql);
2372 sqlite3DbFree(db, p->pFree);
2373 sqlite3DbFree(db, p);
2374}
2375
2376/*
drh9a324642003-09-06 20:12:01 +00002377** Delete an entire VDBE.
2378*/
danielk19774adee202004-05-08 08:23:19 +00002379void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002380 sqlite3 *db;
2381
drhfa3be902009-07-07 02:44:07 +00002382 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002383 db = p->db;
drh9a324642003-09-06 20:12:01 +00002384 if( p->pPrev ){
2385 p->pPrev->pNext = p->pNext;
2386 }else{
drh633e6d52008-07-28 19:34:53 +00002387 assert( db->pVdbe==p );
2388 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002389 }
2390 if( p->pNext ){
2391 p->pNext->pPrev = p->pPrev;
2392 }
drh9a324642003-09-06 20:12:01 +00002393 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002394 p->db = 0;
dand46def72010-07-24 11:28:28 +00002395 sqlite3VdbeDeleteObject(db, p);
drh9a324642003-09-06 20:12:01 +00002396}
drha11846b2004-01-07 18:52:56 +00002397
2398/*
drh9a65f2c2009-06-22 19:05:40 +00002399** Make sure the cursor p is ready to read or write the row to which it
2400** was last positioned. Return an error code if an OOM fault or I/O error
2401** prevents us from positioning the cursor to its correct position.
2402**
drha11846b2004-01-07 18:52:56 +00002403** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002404** MoveTo now. If no move is pending, check to see if the row has been
2405** deleted out from under the cursor and if it has, mark the row as
2406** a NULL row.
2407**
2408** If the cursor is already pointing to the correct row and that row has
2409** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002410*/
drhdfe88ec2008-11-03 20:55:06 +00002411int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002412 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002413 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002414#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002415 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002416#endif
drhf0863fe2005-06-12 21:35:51 +00002417 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002418 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002419 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002420 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002421 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2422 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002423#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002424 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002425#endif
drha11846b2004-01-07 18:52:56 +00002426 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002427 p->cacheStatus = CACHE_STALE;
drh6be240e2009-07-14 02:33:02 +00002428 }else if( ALWAYS(p->pCursor) ){
drha3460582008-07-11 21:02:53 +00002429 int hasMoved;
2430 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2431 if( rc ) return rc;
2432 if( hasMoved ){
2433 p->cacheStatus = CACHE_STALE;
2434 p->nullRow = 1;
2435 }
drha11846b2004-01-07 18:52:56 +00002436 }
2437 return SQLITE_OK;
2438}
danielk19774adee202004-05-08 08:23:19 +00002439
drhab9f7f12004-05-08 10:56:11 +00002440/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002441** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002442**
danielk1977cfcdaef2004-05-12 07:33:33 +00002443** sqlite3VdbeSerialType()
2444** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002445** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002446** sqlite3VdbeSerialPut()
2447** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002448**
2449** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002450** data and index records. Each serialized value consists of a
2451** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2452** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002453**
danielk1977cfcdaef2004-05-12 07:33:33 +00002454** In an SQLite index record, the serial type is stored directly before
2455** the blob of data that it corresponds to. In a table record, all serial
2456** types are stored at the start of the record, and the blobs of data at
2457** the end. Hence these functions allow the caller to handle the
2458** serial-type and data blob seperately.
2459**
2460** The following table describes the various storage classes for data:
2461**
2462** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002463** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002464** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002465** 1 1 signed integer
2466** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002467** 3 3 signed integer
2468** 4 4 signed integer
2469** 5 6 signed integer
2470** 6 8 signed integer
2471** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002472** 8 0 Integer constant 0
2473** 9 0 Integer constant 1
2474** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002475** N>=12 and even (N-12)/2 BLOB
2476** N>=13 and odd (N-13)/2 text
2477**
drh35a59652006-01-02 18:24:40 +00002478** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2479** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002480*/
2481
2482/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002483** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002484*/
drhd946db02005-12-29 19:23:06 +00002485u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002486 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002487 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002488
2489 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002490 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002491 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002492 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002493 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002494# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002495 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002496 u64 u;
2497 if( file_format>=4 && (i&1)==i ){
drh8df32842008-12-09 02:51:23 +00002498 return 8+(u32)i;
drhd946db02005-12-29 19:23:06 +00002499 }
drhcfd654b2011-03-05 13:54:15 +00002500 if( i<0 ){
2501 if( i<(-MAX_6BYTE) ) return 6;
2502 /* Previous test prevents: u = -(-9223372036854775808) */
2503 u = -i;
2504 }else{
2505 u = i;
2506 }
drh5742b632005-01-26 17:47:02 +00002507 if( u<=127 ) return 1;
2508 if( u<=32767 ) return 2;
2509 if( u<=8388607 ) return 3;
2510 if( u<=2147483647 ) return 4;
2511 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002512 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002513 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002514 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002515 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002516 }
danielk1977e4359752008-11-03 09:39:45 +00002517 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002518 n = pMem->n;
2519 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002520 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002521 }
drhfdf972a2007-05-02 13:30:27 +00002522 assert( n>=0 );
2523 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002524}
2525
2526/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002527** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002528*/
drh35cd6432009-06-05 14:17:21 +00002529u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002530 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002531 return (serial_type-12)/2;
2532 }else{
drh57196282004-10-06 15:41:16 +00002533 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002534 return aSize[serial_type];
2535 }
danielk1977192ac1d2004-05-10 07:17:30 +00002536}
2537
2538/*
drh110daac2007-05-04 11:59:31 +00002539** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002540** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002541** upper 4 bytes. Return the result.
2542**
drh7a4f5022007-05-23 07:20:08 +00002543** For most architectures, this is a no-op.
2544**
2545** (later): It is reported to me that the mixed-endian problem
2546** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2547** that early versions of GCC stored the two words of a 64-bit
2548** float in the wrong order. And that error has been propagated
2549** ever since. The blame is not necessarily with GCC, though.
2550** GCC might have just copying the problem from a prior compiler.
2551** I am also told that newer versions of GCC that follow a different
2552** ABI get the byte order right.
2553**
2554** Developers using SQLite on an ARM7 should compile and run their
2555** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2556** enabled, some asserts below will ensure that the byte order of
2557** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002558**
2559** (2007-08-30) Frank van Vugt has studied this problem closely
2560** and has send his findings to the SQLite developers. Frank
2561** writes that some Linux kernels offer floating point hardware
2562** emulation that uses only 32-bit mantissas instead of a full
2563** 48-bits as required by the IEEE standard. (This is the
2564** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2565** byte swapping becomes very complicated. To avoid problems,
2566** the necessary byte swapping is carried out using a 64-bit integer
2567** rather than a 64-bit float. Frank assures us that the code here
2568** works for him. We, the developers, have no way to independently
2569** verify this, but Frank seems to know what he is talking about
2570** so we trust him.
drh110daac2007-05-04 11:59:31 +00002571*/
2572#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002573static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002574 union {
drh60d09a72007-08-30 15:05:08 +00002575 u64 r;
drh110daac2007-05-04 11:59:31 +00002576 u32 i[2];
2577 } u;
2578 u32 t;
2579
2580 u.r = in;
2581 t = u.i[0];
2582 u.i[0] = u.i[1];
2583 u.i[1] = t;
2584 return u.r;
2585}
2586# define swapMixedEndianFloat(X) X = floatSwap(X)
2587#else
2588# define swapMixedEndianFloat(X)
2589#endif
2590
2591/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002592** Write the serialized data blob for the value stored in pMem into
2593** buf. It is assumed that the caller has allocated sufficient space.
2594** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002595**
2596** nBuf is the amount of space left in buf[]. nBuf must always be
2597** large enough to hold the entire field. Except, if the field is
2598** a blob with a zero-filled tail, then buf[] might be just the right
2599** size to hold everything except for the zero-filled tail. If buf[]
2600** is only big enough to hold the non-zero prefix, then only write that
2601** prefix into buf[]. But if buf[] is large enough to hold both the
2602** prefix and the tail then write the prefix and set the tail to all
2603** zeros.
2604**
2605** Return the number of bytes actually written into buf[]. The number
2606** of bytes in the zero-filled tail is included in the return value only
2607** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002608*/
drh35cd6432009-06-05 14:17:21 +00002609u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002610 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
drh35cd6432009-06-05 14:17:21 +00002611 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002612
drh1483e142004-05-21 21:12:42 +00002613 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002614 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002615 u64 v;
drh35cd6432009-06-05 14:17:21 +00002616 u32 i;
drha19b7752004-05-30 21:14:58 +00002617 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002618 assert( sizeof(v)==sizeof(pMem->r) );
2619 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002620 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002621 }else{
drh3c024d62007-03-30 11:23:45 +00002622 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002623 }
drh1483e142004-05-21 21:12:42 +00002624 len = i = sqlite3VdbeSerialTypeLen(serial_type);
shane75ac1de2009-06-09 18:58:52 +00002625 assert( len<=(u32)nBuf );
drh1483e142004-05-21 21:12:42 +00002626 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002627 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002628 v >>= 8;
2629 }
2630 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002631 }
drhd946db02005-12-29 19:23:06 +00002632
danielk1977cfcdaef2004-05-12 07:33:33 +00002633 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002634 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002635 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002636 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002637 assert( pMem->n<=nBuf );
2638 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002639 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002640 if( pMem->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002641 len += pMem->u.nZero;
drh35cd6432009-06-05 14:17:21 +00002642 assert( nBuf>=0 );
2643 if( len > (u32)nBuf ){
2644 len = (u32)nBuf;
drhfdf972a2007-05-02 13:30:27 +00002645 }
2646 memset(&buf[pMem->n], 0, len-pMem->n);
2647 }
drhd946db02005-12-29 19:23:06 +00002648 return len;
2649 }
2650
2651 /* NULL or constants 0 or 1 */
2652 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002653}
2654
2655/*
2656** Deserialize the data blob pointed to by buf as serial type serial_type
2657** and store the result in pMem. Return the number of bytes read.
2658*/
drh35cd6432009-06-05 14:17:21 +00002659u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002660 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002661 u32 serial_type, /* Serial type to deserialize */
2662 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002663){
drh3c685822005-05-21 18:32:18 +00002664 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002665 case 10: /* Reserved for future use */
2666 case 11: /* Reserved for future use */
2667 case 0: { /* NULL */
2668 pMem->flags = MEM_Null;
2669 break;
2670 }
2671 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002672 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002673 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002674 return 1;
drh1483e142004-05-21 21:12:42 +00002675 }
drh3c685822005-05-21 18:32:18 +00002676 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002677 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002678 pMem->flags = MEM_Int;
2679 return 2;
2680 }
2681 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002682 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002683 pMem->flags = MEM_Int;
2684 return 3;
2685 }
2686 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002687 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002688 pMem->flags = MEM_Int;
2689 return 4;
2690 }
2691 case 5: { /* 6-byte signed integer */
2692 u64 x = (((signed char)buf[0])<<8) | buf[1];
2693 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2694 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002695 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002696 pMem->flags = MEM_Int;
2697 return 6;
2698 }
drh91124b32005-08-18 18:15:05 +00002699 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002700 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002701 u64 x;
2702 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002703#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002704 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002705 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2706 ** defined that 64-bit floating point values really are mixed
2707 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002708 */
drhde941c62005-08-28 01:34:21 +00002709 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002710 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002711 u64 t2 = t1;
2712 swapMixedEndianFloat(t2);
2713 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002714#endif
drhbfd6b032005-08-28 01:38:44 +00002715
drhd81bd4e2005-09-05 20:06:49 +00002716 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2717 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002718 x = (x<<32) | y;
2719 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002720 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002721 pMem->flags = MEM_Int;
2722 }else{
drh4f0c5872007-03-26 22:05:01 +00002723 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002724 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002725 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002726 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002727 }
2728 return 8;
2729 }
drhd946db02005-12-29 19:23:06 +00002730 case 8: /* Integer 0 */
2731 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002732 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002733 pMem->flags = MEM_Int;
2734 return 0;
2735 }
drh3c685822005-05-21 18:32:18 +00002736 default: {
drh35cd6432009-06-05 14:17:21 +00002737 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00002738 pMem->z = (char *)buf;
2739 pMem->n = len;
2740 pMem->xDel = 0;
2741 if( serial_type&0x01 ){
2742 pMem->flags = MEM_Str | MEM_Ephem;
2743 }else{
2744 pMem->flags = MEM_Blob | MEM_Ephem;
2745 }
2746 return len;
drh696b32f2004-05-30 01:51:52 +00002747 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002748 }
drh3c685822005-05-21 18:32:18 +00002749 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002750}
2751
drh0e6082e2006-01-12 20:28:35 +00002752
drh1e968a02008-03-25 00:22:21 +00002753/*
2754** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002755** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002756** that structure.
2757**
2758** The calling function might provide szSpace bytes of memory
2759** space at pSpace. This space can be used to hold the returned
2760** VDbeParsedRecord structure if it is large enough. If it is
2761** not big enough, space is obtained from sqlite3_malloc().
2762**
2763** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002764** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002765*/
drhe14006d2008-03-25 17:23:32 +00002766UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002767 KeyInfo *pKeyInfo, /* Information about the record format */
2768 int nKey, /* Size of the binary record */
2769 const void *pKey, /* The binary record */
drh8c5d1522009-04-10 00:56:28 +00002770 char *pSpace, /* Unaligned space available to hold the object */
drh1e968a02008-03-25 00:22:21 +00002771 int szSpace /* Size of pSpace[] in bytes */
2772){
2773 const unsigned char *aKey = (const unsigned char *)pKey;
drh8c5d1522009-04-10 00:56:28 +00002774 UnpackedRecord *p; /* The unpacked record that we will return */
2775 int nByte; /* Memory space needed to hold p, in bytes */
2776 int d;
danielk197700e13612008-11-17 19:18:54 +00002777 u32 idx;
drh8c5d1522009-04-10 00:56:28 +00002778 u16 u; /* Unsigned loop counter */
drh1e968a02008-03-25 00:22:21 +00002779 u32 szHdr;
2780 Mem *pMem;
drh8c5d1522009-04-10 00:56:28 +00002781 int nOff; /* Increase pSpace by this much to 8-byte align it */
drh1e968a02008-03-25 00:22:21 +00002782
shane80167bf2009-04-10 15:42:36 +00002783 /*
2784 ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
2785 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
2786 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
2787 */
2788 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00002789 pSpace += nOff;
2790 szSpace -= nOff;
2791 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
drh1e968a02008-03-25 00:22:21 +00002792 if( nByte>szSpace ){
2793 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2794 if( p==0 ) return 0;
drhe63d9992008-08-13 19:11:48 +00002795 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002796 }else{
drh8c5d1522009-04-10 00:56:28 +00002797 p = (UnpackedRecord*)pSpace;
drhe63d9992008-08-13 19:11:48 +00002798 p->flags = UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002799 }
2800 p->pKeyInfo = pKeyInfo;
2801 p->nField = pKeyInfo->nField + 1;
drh8c5d1522009-04-10 00:56:28 +00002802 p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
2803 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00002804 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002805 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00002806 u = 0;
drh2fa34d32009-07-15 16:30:50 +00002807 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00002808 u32 serial_type;
2809
danielk197700e13612008-11-17 19:18:54 +00002810 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00002811 pMem->enc = pKeyInfo->enc;
2812 pMem->db = pKeyInfo->db;
2813 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002814 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002815 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002816 pMem++;
shane0b8d2762008-07-22 05:18:00 +00002817 u++;
drh1e968a02008-03-25 00:22:21 +00002818 }
drh7d10d5a2008-08-20 16:35:10 +00002819 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00002820 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00002821 return (void*)p;
2822}
2823
2824/*
drh7b746032009-06-26 12:15:22 +00002825** This routine destroys a UnpackedRecord object.
drh1e968a02008-03-25 00:22:21 +00002826*/
drhe14006d2008-03-25 17:23:32 +00002827void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh7b746032009-06-26 12:15:22 +00002828 int i;
2829 Mem *pMem;
2830
2831 assert( p!=0 );
2832 assert( p->flags & UNPACKED_NEED_DESTROY );
2833 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
drh6be240e2009-07-14 02:33:02 +00002834 /* The unpacked record is always constructed by the
2835 ** sqlite3VdbeUnpackRecord() function above, which makes all
2836 ** strings and blobs static. And none of the elements are
2837 ** ever transformed, so there is never anything to delete.
2838 */
2839 if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
drh7b746032009-06-26 12:15:22 +00002840 }
2841 if( p->flags & UNPACKED_NEED_FREE ){
2842 sqlite3DbFree(p->pKeyInfo->db, p);
drh1e968a02008-03-25 00:22:21 +00002843 }
2844}
2845
2846/*
2847** This function compares the two table rows or index records
2848** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00002849** or positive integer if key1 is less than, equal to or
2850** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00002851** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2852** key must be a parsed key such as obtained from
2853** sqlite3VdbeParseRecord.
2854**
2855** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00002856** The key with fewer fields is usually compares less than the
2857** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
2858** and the common prefixes are equal, then key1 is less than key2.
2859** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
2860** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00002861** the parts beyond the common prefix are ignored.
2862**
drhe63d9992008-08-13 19:11:48 +00002863** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
2864** the header of pKey1 is ignored. It is assumed that pKey1 is
2865** an index key, and thus ends with a rowid value. The last byte
2866** of the header will therefore be the serial type of the rowid:
2867** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
2868** The serial type of the final rowid will always be a single byte.
2869** By ignoring this last byte of the header, we force the comparison
2870** to ignore the rowid at the end of key1.
drh1e968a02008-03-25 00:22:21 +00002871*/
drhe14006d2008-03-25 17:23:32 +00002872int sqlite3VdbeRecordCompare(
drhec1fc802008-08-13 14:07:40 +00002873 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00002874 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00002875){
danielk197700e13612008-11-17 19:18:54 +00002876 int d1; /* Offset into aKey[] of next data element */
drh1e968a02008-03-25 00:22:21 +00002877 u32 idx1; /* Offset into aKey[] of next header element */
2878 u32 szHdr1; /* Number of bytes in header */
2879 int i = 0;
2880 int nField;
2881 int rc = 0;
2882 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2883 KeyInfo *pKeyInfo;
2884 Mem mem1;
2885
2886 pKeyInfo = pPKey2->pKeyInfo;
2887 mem1.enc = pKeyInfo->enc;
drh37272632009-11-16 21:28:45 +00002888 mem1.db = pKeyInfo->db;
drhd93a8b22009-11-16 03:13:40 +00002889 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
2890 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
drh8b249a82009-11-16 02:14:00 +00002891
2892 /* Compilers may complain that mem1.u.i is potentially uninitialized.
2893 ** We could initialize it, as shown here, to silence those complaints.
2894 ** But in fact, mem1.u.i will never actually be used initialized, and doing
2895 ** the unnecessary initialization has a measurable negative performance
2896 ** impact, since this routine is a very high runner. And so, we choose
2897 ** to ignore the compiler warnings and leave this variable uninitialized.
2898 */
2899 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
drh1e968a02008-03-25 00:22:21 +00002900
shane3f8d5cf2008-04-24 19:15:09 +00002901 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002902 d1 = szHdr1;
drhe63d9992008-08-13 19:11:48 +00002903 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
2904 szHdr1--;
2905 }
drh1e968a02008-03-25 00:22:21 +00002906 nField = pKeyInfo->nField;
2907 while( idx1<szHdr1 && i<pPKey2->nField ){
2908 u32 serial_type1;
2909
2910 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002911 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002912 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2913
2914 /* Extract the values to be compared.
2915 */
2916 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2917
2918 /* Do the comparison
2919 */
drhe14006d2008-03-25 17:23:32 +00002920 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002921 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002922 if( rc!=0 ){
drh8b249a82009-11-16 02:14:00 +00002923 assert( mem1.zMalloc==0 ); /* See comment below */
2924
2925 /* Invert the result if we are using DESC sort order. */
2926 if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
2927 rc = -rc;
2928 }
2929
2930 /* If the PREFIX_SEARCH flag is set and all fields except the final
2931 ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
2932 ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
2933 ** This is used by the OP_IsUnique opcode.
2934 */
2935 if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
2936 assert( idx1==szHdr1 && rc );
2937 assert( mem1.flags & MEM_Int );
2938 pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
2939 pPKey2->rowid = mem1.u.i;
2940 }
2941
2942 return rc;
drh1e968a02008-03-25 00:22:21 +00002943 }
2944 i++;
2945 }
drh407414c2009-07-14 14:15:27 +00002946
drh8b249a82009-11-16 02:14:00 +00002947 /* No memory allocation is ever used on mem1. Prove this using
2948 ** the following assert(). If the assert() fails, it indicates a
2949 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
danielk1977de630352009-05-04 11:42:29 +00002950 */
drh8b249a82009-11-16 02:14:00 +00002951 assert( mem1.zMalloc==0 );
danielk1977de630352009-05-04 11:42:29 +00002952
drh8b249a82009-11-16 02:14:00 +00002953 /* rc==0 here means that one of the keys ran out of fields and
2954 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
2955 ** flag is set, then break the tie by treating key2 as larger.
2956 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
2957 ** are considered to be equal. Otherwise, the longer key is the
2958 ** larger. As it happens, the pPKey2 will always be the longer
2959 ** if there is a difference.
2960 */
2961 assert( rc==0 );
2962 if( pPKey2->flags & UNPACKED_INCRKEY ){
2963 rc = -1;
2964 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
2965 /* Leave rc==0 */
2966 }else if( idx1<szHdr1 ){
2967 rc = 1;
drh1e968a02008-03-25 00:22:21 +00002968 }
drh1e968a02008-03-25 00:22:21 +00002969 return rc;
2970}
drhec1fc802008-08-13 14:07:40 +00002971
danielk1977eb015e02004-05-18 01:31:14 +00002972
2973/*
drh7a224de2004-06-02 01:22:02 +00002974** pCur points at an index entry created using the OP_MakeRecord opcode.
2975** Read the rowid (the last field in the record) and store it in *rowid.
2976** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00002977**
2978** pCur might be pointing to text obtained from a corrupt database file.
2979** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00002980*/
drh35f6b932009-06-23 14:15:04 +00002981int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002982 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002983 int rc;
drhd5788202004-05-28 08:21:05 +00002984 u32 szHdr; /* Size of the header */
2985 u32 typeRowid; /* Serial type of the rowid */
2986 u32 lenRowid; /* Size of the rowid */
2987 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002988
shanecea72b22009-09-07 04:38:36 +00002989 UNUSED_PARAMETER(db);
2990
drh88a003e2008-12-11 16:17:03 +00002991 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00002992 ** than 2GiB are support - anything large must be database corruption.
2993 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00002994 ** this code can safely assume that nCellKey is 32-bits
2995 */
drhea8ffdf2009-07-22 00:35:23 +00002996 assert( sqlite3BtreeCursorIsValid(pCur) );
drhc27ae612009-07-14 18:35:44 +00002997 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
2998 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00002999 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003000
3001 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003002 memset(&m, 0, sizeof(m));
drh8df32842008-12-09 02:51:23 +00003003 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003004 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003005 return rc;
3006 }
drh88a003e2008-12-11 16:17:03 +00003007
3008 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003009 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003010 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003011 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003012 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003013 goto idx_rowid_corruption;
3014 }
3015
3016 /* The last field of the index should be an integer - the ROWID.
3017 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003018 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003019 testcase( typeRowid==1 );
3020 testcase( typeRowid==2 );
3021 testcase( typeRowid==3 );
3022 testcase( typeRowid==4 );
3023 testcase( typeRowid==5 );
3024 testcase( typeRowid==6 );
3025 testcase( typeRowid==8 );
3026 testcase( typeRowid==9 );
3027 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3028 goto idx_rowid_corruption;
3029 }
drhd5788202004-05-28 08:21:05 +00003030 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003031 testcase( (u32)m.n==szHdr+lenRowid );
3032 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003033 goto idx_rowid_corruption;
3034 }
3035
3036 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003037 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003038 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003039 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003040 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003041
3042 /* Jump here if database corruption is detected after m has been
3043 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3044idx_rowid_corruption:
3045 testcase( m.zMalloc!=0 );
3046 sqlite3VdbeMemRelease(&m);
3047 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003048}
3049
drh7cf6e4d2004-05-19 14:56:55 +00003050/*
drh5f82e3c2009-07-06 00:44:08 +00003051** Compare the key of the index entry that cursor pC is pointing to against
3052** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003053** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003054** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003055**
drh5f82e3c2009-07-06 00:44:08 +00003056** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003057** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003058** is ignored as well. Hence, this routine only compares the prefixes
3059** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003060*/
danielk1977183f9f72004-05-13 05:20:26 +00003061int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00003062 VdbeCursor *pC, /* The cursor to compare against */
drh5f82e3c2009-07-06 00:44:08 +00003063 UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */
drh7cf6e4d2004-05-19 14:56:55 +00003064 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003065){
drh61fc5952007-04-01 23:49:51 +00003066 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003067 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003068 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003069 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003070
drhea8ffdf2009-07-22 00:35:23 +00003071 assert( sqlite3BtreeCursorIsValid(pCur) );
drhc27ae612009-07-14 18:35:44 +00003072 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
3073 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh407414c2009-07-14 14:15:27 +00003074 /* nCellKey will always be between 0 and 0xffffffff because of the say
3075 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003076 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003077 *res = 0;
drh9978c972010-02-23 17:36:32 +00003078 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003079 }
drhfd3ca1c2009-08-25 12:11:00 +00003080 memset(&m, 0, sizeof(m));
drh8df32842008-12-09 02:51:23 +00003081 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003082 if( rc ){
drhd5788202004-05-28 08:21:05 +00003083 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003084 }
drhe63d9992008-08-13 19:11:48 +00003085 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
3086 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00003087 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003088 return SQLITE_OK;
3089}
danielk1977b28af712004-06-21 06:50:26 +00003090
3091/*
3092** This routine sets the value to be returned by subsequent calls to
3093** sqlite3_changes() on the database handle 'db'.
3094*/
3095void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003096 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003097 db->nChange = nChange;
3098 db->nTotalChange += nChange;
3099}
3100
3101/*
3102** Set a flag in the vdbe to update the change counter when it is finalised
3103** or reset.
3104*/
drh4794f732004-11-05 17:17:50 +00003105void sqlite3VdbeCountChanges(Vdbe *v){
3106 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003107}
drhd89bd002005-01-22 03:03:54 +00003108
3109/*
3110** Mark every prepared statement associated with a database connection
3111** as expired.
3112**
3113** An expired statement means that recompilation of the statement is
3114** recommend. Statements expire when things happen that make their
3115** programs obsolete. Removing user-defined functions or collating
3116** sequences, or changing an authorization function are the types of
3117** things that make prepared statements obsolete.
3118*/
3119void sqlite3ExpirePreparedStatements(sqlite3 *db){
3120 Vdbe *p;
3121 for(p = db->pVdbe; p; p=p->pNext){
3122 p->expired = 1;
3123 }
3124}
danielk1977aee18ef2005-03-09 12:26:50 +00003125
3126/*
3127** Return the database associated with the Vdbe.
3128*/
3129sqlite3 *sqlite3VdbeDb(Vdbe *v){
3130 return v->db;
3131}
dan937d0de2009-10-15 18:35:38 +00003132
3133/*
3134** Return a pointer to an sqlite3_value structure containing the value bound
3135** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3136** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3137** constants) to the value before returning it.
3138**
3139** The returned value must be freed by the caller using sqlite3ValueFree().
3140*/
3141sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
3142 assert( iVar>0 );
3143 if( v ){
3144 Mem *pMem = &v->aVar[iVar-1];
3145 if( 0==(pMem->flags & MEM_Null) ){
3146 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3147 if( pRet ){
3148 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3149 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
3150 sqlite3VdbeMemStoreType((Mem *)pRet);
3151 }
3152 return pRet;
3153 }
3154 }
3155 return 0;
3156}
3157
3158/*
3159** Configure SQL variable iVar so that binding a new value to it signals
3160** to sqlite3_reoptimize() that re-preparing the statement may result
3161** in a better query plan.
3162*/
dan1d2ce4f2009-10-19 18:11:09 +00003163void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003164 assert( iVar>0 );
3165 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00003166 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00003167 }else{
dan1d2ce4f2009-10-19 18:11:09 +00003168 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00003169 }
3170}