blob: 7194dea36ade13b66044d7cf3ead2aa60ccf2a31 [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
drh9a324642003-09-06 20:12:01 +000020/*
21** Create a new virtual database engine.
22*/
drh9ac79622013-12-18 15:11:47 +000023Vdbe *sqlite3VdbeCreate(Parse *pParse){
24 sqlite3 *db = pParse->db;
drh9a324642003-09-06 20:12:01 +000025 Vdbe *p;
drh17435752007-08-16 04:30:38 +000026 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000027 if( p==0 ) return 0;
28 p->db = db;
29 if( db->pVdbe ){
30 db->pVdbe->pPrev = p;
31 }
32 p->pNext = db->pVdbe;
33 p->pPrev = 0;
34 db->pVdbe = p;
35 p->magic = VDBE_MAGIC_INIT;
drh9ac79622013-12-18 15:11:47 +000036 p->pParse = pParse;
drh73d5b8f2013-12-23 19:09:07 +000037 assert( pParse->aLabel==0 );
38 assert( pParse->nLabel==0 );
39 assert( pParse->nOpAlloc==0 );
drh9a324642003-09-06 20:12:01 +000040 return p;
41}
42
43/*
drhb900aaf2006-11-09 00:24:53 +000044** Remember the SQL string for a prepared statement.
45*/
danielk19776ab3a2e2009-02-19 14:39:25 +000046void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
dan1d2ce4f2009-10-19 18:11:09 +000047 assert( isPrepareV2==1 || isPrepareV2==0 );
drhb900aaf2006-11-09 00:24:53 +000048 if( p==0 ) return;
danac455932012-11-26 19:50:41 +000049#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
danielk19776ab3a2e2009-02-19 14:39:25 +000050 if( !isPrepareV2 ) return;
51#endif
drhb900aaf2006-11-09 00:24:53 +000052 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000053 p->zSql = sqlite3DbStrNDup(p->db, z, n);
shanef639c402009-11-03 19:42:30 +000054 p->isPrepareV2 = (u8)isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000055}
56
57/*
58** Return the SQL associated with a prepared statement
59*/
danielk1977d0e2a852007-11-14 06:48:48 +000060const char *sqlite3_sql(sqlite3_stmt *pStmt){
danielk19776ab3a2e2009-02-19 14:39:25 +000061 Vdbe *p = (Vdbe *)pStmt;
drh87f5c5f2010-01-20 01:20:56 +000062 return (p && p->isPrepareV2) ? p->zSql : 0;
drhb900aaf2006-11-09 00:24:53 +000063}
64
65/*
drhc5155252007-01-08 21:07:17 +000066** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000067*/
drhc5155252007-01-08 21:07:17 +000068void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
69 Vdbe tmp, *pTmp;
70 char *zTmp;
drhc5155252007-01-08 21:07:17 +000071 tmp = *pA;
72 *pA = *pB;
73 *pB = tmp;
74 pTmp = pA->pNext;
75 pA->pNext = pB->pNext;
76 pB->pNext = pTmp;
77 pTmp = pA->pPrev;
78 pA->pPrev = pB->pPrev;
79 pB->pPrev = pTmp;
80 zTmp = pA->zSql;
81 pA->zSql = pB->zSql;
82 pB->zSql = zTmp;
danielk19776ab3a2e2009-02-19 14:39:25 +000083 pB->isPrepareV2 = pA->isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000084}
85
drh9a324642003-09-06 20:12:01 +000086/*
danielk197700e13612008-11-17 19:18:54 +000087** Resize the Vdbe.aOp array so that it is at least one op larger than
88** it was.
danielk1977ace3eb22006-01-26 10:35:04 +000089**
danielk197700e13612008-11-17 19:18:54 +000090** If an out-of-memory error occurs while resizing the array, return
91** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
92** unchanged (this is so that any opcodes already allocated can be
93** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +000094*/
drh73d5b8f2013-12-23 19:09:07 +000095static int growOpArray(Vdbe *v){
drha4e5d582007-10-20 15:41:57 +000096 VdbeOp *pNew;
drh73d5b8f2013-12-23 19:09:07 +000097 Parse *p = v->pParse;
danielk197700e13612008-11-17 19:18:54 +000098 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
drh73d5b8f2013-12-23 19:09:07 +000099 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000100 if( pNew ){
drhb45f65d2009-03-01 19:42:11 +0000101 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
drh73d5b8f2013-12-23 19:09:07 +0000102 v->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000103 }
danielk197700e13612008-11-17 19:18:54 +0000104 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000105}
106
drh313619f2013-10-31 20:34:06 +0000107#ifdef SQLITE_DEBUG
108/* This routine is just a convenient place to set a breakpoint that will
109** fire after each opcode is inserted and displayed using
110** "PRAGMA vdbe_addoptrace=on".
111*/
112static void test_addop_breakpoint(void){
113 static int n = 0;
114 n++;
115}
116#endif
117
drh76ff3a02004-09-24 22:32:30 +0000118/*
drh9a324642003-09-06 20:12:01 +0000119** Add a new instruction to the list of instructions current in the
120** VDBE. Return the address of the new instruction.
121**
122** Parameters:
123**
124** p Pointer to the VDBE
125**
126** op The opcode for this instruction
127**
drh66a51672008-01-03 00:01:23 +0000128** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000129**
danielk19774adee202004-05-08 08:23:19 +0000130** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000131** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000132** operand.
133*/
drh66a51672008-01-03 00:01:23 +0000134int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000135 int i;
drh701a0ae2004-02-22 20:05:00 +0000136 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000137
138 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000139 assert( p->magic==VDBE_MAGIC_INIT );
drh8df32842008-12-09 02:51:23 +0000140 assert( op>0 && op<0xff );
drh73d5b8f2013-12-23 19:09:07 +0000141 if( p->pParse->nOpAlloc<=i ){
danielk197700e13612008-11-17 19:18:54 +0000142 if( growOpArray(p) ){
drhc42ed162009-06-26 14:04:51 +0000143 return 1;
drhfd2d26b2006-03-15 22:44:36 +0000144 }
drh9a324642003-09-06 20:12:01 +0000145 }
danielk197701256832007-04-18 14:24:32 +0000146 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000147 pOp = &p->aOp[i];
drh8df32842008-12-09 02:51:23 +0000148 pOp->opcode = (u8)op;
drh26c9b5e2008-04-11 14:56:53 +0000149 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000150 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000151 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000152 pOp->p3 = p3;
153 pOp->p4.p = 0;
154 pOp->p4type = P4_NOTUSED;
drhc7379ce2013-10-30 02:28:23 +0000155#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000156 pOp->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000157#endif
158#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000159 if( p->db->flags & SQLITE_VdbeAddopTrace ){
drh9ac79622013-12-18 15:11:47 +0000160 int jj, kk;
161 Parse *pParse = p->pParse;
162 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
163 struct yColCache *x = pParse->aColCache + jj;
164 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
165 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
166 kk++;
167 }
168 if( kk ) printf("\n");
drhe0962052013-01-29 19:14:31 +0000169 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh313619f2013-10-31 20:34:06 +0000170 test_addop_breakpoint();
drhe0962052013-01-29 19:14:31 +0000171 }
drh9a324642003-09-06 20:12:01 +0000172#endif
drh26c9b5e2008-04-11 14:56:53 +0000173#ifdef VDBE_PROFILE
174 pOp->cycles = 0;
175 pOp->cnt = 0;
176#endif
drh688852a2014-02-17 22:40:43 +0000177#ifdef SQLITE_VDBE_COVERAGE
178 pOp->iSrcLine = 0;
179#endif
drh9a324642003-09-06 20:12:01 +0000180 return i;
181}
drh66a51672008-01-03 00:01:23 +0000182int sqlite3VdbeAddOp0(Vdbe *p, int op){
183 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
184}
185int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
186 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
187}
188int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
189 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000190}
191
drh66a51672008-01-03 00:01:23 +0000192
drh701a0ae2004-02-22 20:05:00 +0000193/*
drh66a51672008-01-03 00:01:23 +0000194** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000195*/
drh66a51672008-01-03 00:01:23 +0000196int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000197 Vdbe *p, /* Add the opcode to this VM */
198 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000199 int p1, /* The P1 operand */
200 int p2, /* The P2 operand */
201 int p3, /* The P3 operand */
202 const char *zP4, /* The P4 operand */
203 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000204){
drh66a51672008-01-03 00:01:23 +0000205 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
206 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000207 return addr;
208}
209
210/*
drh5d9c9da2011-06-03 20:11:17 +0000211** Add an OP_ParseSchema opcode. This routine is broken out from
drhe4c88c02012-01-04 12:57:45 +0000212** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
213** as having been used.
drh5d9c9da2011-06-03 20:11:17 +0000214**
215** The zWhere string must have been obtained from sqlite3_malloc().
216** This routine will take ownership of the allocated memory.
217*/
218void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
219 int j;
220 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
221 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
222 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
223}
224
225/*
drh8cff69d2009-11-12 19:59:44 +0000226** Add an opcode that includes the p4 value as an integer.
227*/
228int sqlite3VdbeAddOp4Int(
229 Vdbe *p, /* Add the opcode to this VM */
230 int op, /* The new opcode */
231 int p1, /* The P1 operand */
232 int p2, /* The P2 operand */
233 int p3, /* The P3 operand */
234 int p4 /* The P4 operand as an integer */
235){
236 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
237 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
238 return addr;
239}
240
241/*
drh9a324642003-09-06 20:12:01 +0000242** Create a new symbolic label for an instruction that has yet to be
243** coded. The symbolic label is really just a negative number. The
244** label can be used as the P2 value of an operation. Later, when
245** the label is resolved to a specific address, the VDBE will scan
246** through its operation list and change all values of P2 which match
247** the label into the resolved address.
248**
249** The VDBE knows that a P2 value is a label because labels are
250** always negative and P2 values are suppose to be non-negative.
251** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000252**
253** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000254*/
drh73d5b8f2013-12-23 19:09:07 +0000255int sqlite3VdbeMakeLabel(Vdbe *v){
256 Parse *p = v->pParse;
drhc35f3d52012-02-01 19:03:38 +0000257 int i = p->nLabel++;
drh73d5b8f2013-12-23 19:09:07 +0000258 assert( v->magic==VDBE_MAGIC_INIT );
drhc35f3d52012-02-01 19:03:38 +0000259 if( (i & (i-1))==0 ){
260 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
261 (i*2+1)*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000262 }
drh76ff3a02004-09-24 22:32:30 +0000263 if( p->aLabel ){
264 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000265 }
drh9a324642003-09-06 20:12:01 +0000266 return -1-i;
267}
268
269/*
270** Resolve label "x" to be the address of the next instruction to
271** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000272** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000273*/
drh73d5b8f2013-12-23 19:09:07 +0000274void sqlite3VdbeResolveLabel(Vdbe *v, int x){
275 Parse *p = v->pParse;
drh76ff3a02004-09-24 22:32:30 +0000276 int j = -1-x;
drh73d5b8f2013-12-23 19:09:07 +0000277 assert( v->magic==VDBE_MAGIC_INIT );
drhb2b9d3d2013-08-01 01:14:43 +0000278 assert( j<p->nLabel );
279 if( j>=0 && p->aLabel ){
drh73d5b8f2013-12-23 19:09:07 +0000280 p->aLabel[j] = v->nOp;
drh9a324642003-09-06 20:12:01 +0000281 }
drh61019c72014-01-04 16:49:02 +0000282 p->iFixedOp = v->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000283}
284
drh4611d922010-02-25 14:47:01 +0000285/*
286** Mark the VDBE as one that can only be run one time.
287*/
288void sqlite3VdbeRunOnlyOnce(Vdbe *p){
289 p->runOnlyOnce = 1;
290}
291
drhff738bc2009-09-24 00:09:58 +0000292#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
dan144926d2009-09-09 11:37:20 +0000293
294/*
295** The following type and function are used to iterate through all opcodes
296** in a Vdbe main program and each of the sub-programs (triggers) it may
297** invoke directly or indirectly. It should be used as follows:
298**
299** Op *pOp;
300** VdbeOpIter sIter;
301**
302** memset(&sIter, 0, sizeof(sIter));
303** sIter.v = v; // v is of type Vdbe*
304** while( (pOp = opIterNext(&sIter)) ){
305** // Do something with pOp
306** }
307** sqlite3DbFree(v->db, sIter.apSub);
308**
309*/
310typedef struct VdbeOpIter VdbeOpIter;
311struct VdbeOpIter {
312 Vdbe *v; /* Vdbe to iterate through the opcodes of */
313 SubProgram **apSub; /* Array of subprograms */
314 int nSub; /* Number of entries in apSub */
315 int iAddr; /* Address of next instruction to return */
316 int iSub; /* 0 = main program, 1 = first sub-program etc. */
317};
318static Op *opIterNext(VdbeOpIter *p){
319 Vdbe *v = p->v;
320 Op *pRet = 0;
321 Op *aOp;
322 int nOp;
323
324 if( p->iSub<=p->nSub ){
325
326 if( p->iSub==0 ){
327 aOp = v->aOp;
328 nOp = v->nOp;
329 }else{
330 aOp = p->apSub[p->iSub-1]->aOp;
331 nOp = p->apSub[p->iSub-1]->nOp;
332 }
333 assert( p->iAddr<nOp );
334
335 pRet = &aOp[p->iAddr];
336 p->iAddr++;
337 if( p->iAddr==nOp ){
338 p->iSub++;
339 p->iAddr = 0;
340 }
341
342 if( pRet->p4type==P4_SUBPROGRAM ){
343 int nByte = (p->nSub+1)*sizeof(SubProgram*);
344 int j;
345 for(j=0; j<p->nSub; j++){
346 if( p->apSub[j]==pRet->p4.pProgram ) break;
347 }
348 if( j==p->nSub ){
349 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
350 if( !p->apSub ){
351 pRet = 0;
352 }else{
353 p->apSub[p->nSub++] = pRet->p4.pProgram;
354 }
355 }
356 }
357 }
358
359 return pRet;
360}
361
362/*
danf3677212009-09-10 16:14:50 +0000363** Check if the program stored in the VM associated with pParse may
drhff738bc2009-09-24 00:09:58 +0000364** throw an ABORT exception (causing the statement, but not entire transaction
dan144926d2009-09-09 11:37:20 +0000365** to be rolled back). This condition is true if the main program or any
366** sub-programs contains any of the following:
367**
368** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
369** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
370** * OP_Destroy
371** * OP_VUpdate
372** * OP_VRename
dan32b09f22009-09-23 17:29:59 +0000373** * OP_FkCounter with P2==0 (immediate foreign key constraint)
dan144926d2009-09-09 11:37:20 +0000374**
danf3677212009-09-10 16:14:50 +0000375** Then check that the value of Parse.mayAbort is true if an
376** ABORT may be thrown, or false otherwise. Return true if it does
377** match, or false otherwise. This function is intended to be used as
378** part of an assert statement in the compiler. Similar to:
379**
380** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
dan144926d2009-09-09 11:37:20 +0000381*/
danf3677212009-09-10 16:14:50 +0000382int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
383 int hasAbort = 0;
dan144926d2009-09-09 11:37:20 +0000384 Op *pOp;
385 VdbeOpIter sIter;
386 memset(&sIter, 0, sizeof(sIter));
387 sIter.v = v;
388
389 while( (pOp = opIterNext(&sIter))!=0 ){
390 int opcode = pOp->opcode;
391 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
dan32b09f22009-09-23 17:29:59 +0000392#ifndef SQLITE_OMIT_FOREIGN_KEY
dan0ff297e2009-09-25 17:03:14 +0000393 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
dan32b09f22009-09-23 17:29:59 +0000394#endif
dan144926d2009-09-09 11:37:20 +0000395 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
drhd91c1a12013-02-09 13:58:25 +0000396 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
dan144926d2009-09-09 11:37:20 +0000397 ){
danf3677212009-09-10 16:14:50 +0000398 hasAbort = 1;
dan144926d2009-09-09 11:37:20 +0000399 break;
400 }
401 }
dan144926d2009-09-09 11:37:20 +0000402 sqlite3DbFree(v->db, sIter.apSub);
danf3677212009-09-10 16:14:50 +0000403
mistachkin48864df2013-03-21 21:20:32 +0000404 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
danf3677212009-09-10 16:14:50 +0000405 ** If malloc failed, then the while() loop above may not have iterated
406 ** through all opcodes and hasAbort may be set incorrectly. Return
407 ** true for this case to prevent the assert() in the callers frame
408 ** from failing. */
409 return ( v->db->mallocFailed || hasAbort==mayAbort );
dan144926d2009-09-09 11:37:20 +0000410}
drhff738bc2009-09-24 00:09:58 +0000411#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
dan144926d2009-09-09 11:37:20 +0000412
drh9a324642003-09-06 20:12:01 +0000413/*
drh9cbf3422008-01-17 16:22:13 +0000414** Loop through the program looking for P2 values that are negative
415** on jump instructions. Each such value is a label. Resolve the
416** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000417**
418** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000419**
drh13449892005-09-07 21:22:45 +0000420** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000421** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000422** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
drha6c2ed92009-11-14 23:22:23 +0000423**
424** The Op.opflags field is set on all opcodes.
drh76ff3a02004-09-24 22:32:30 +0000425*/
drh9cbf3422008-01-17 16:22:13 +0000426static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000427 int i;
dan165921a2009-08-28 18:53:45 +0000428 int nMaxArgs = *pMaxFuncArgs;
drh76ff3a02004-09-24 22:32:30 +0000429 Op *pOp;
drh73d5b8f2013-12-23 19:09:07 +0000430 Parse *pParse = p->pParse;
431 int *aLabel = pParse->aLabel;
drhad4a4b82008-11-05 16:37:34 +0000432 p->readOnly = 1;
drh1713afb2013-06-28 01:24:57 +0000433 p->bIsReader = 0;
drh76ff3a02004-09-24 22:32:30 +0000434 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000435 u8 opcode = pOp->opcode;
436
drh8c8a8c42013-08-06 07:45:08 +0000437 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
438 ** cases from this switch! */
439 switch( opcode ){
440 case OP_Function:
441 case OP_AggStep: {
442 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
443 break;
444 }
445 case OP_Transaction: {
446 if( pOp->p2!=0 ) p->readOnly = 0;
447 /* fall thru */
448 }
449 case OP_AutoCommit:
450 case OP_Savepoint: {
451 p->bIsReader = 1;
452 break;
453 }
dand9031542013-07-05 16:54:30 +0000454#ifndef SQLITE_OMIT_WAL
drh8c8a8c42013-08-06 07:45:08 +0000455 case OP_Checkpoint:
drh9e92a472013-06-27 17:40:30 +0000456#endif
drh8c8a8c42013-08-06 07:45:08 +0000457 case OP_Vacuum:
458 case OP_JournalMode: {
459 p->readOnly = 0;
460 p->bIsReader = 1;
461 break;
462 }
danielk1977182c4ba2007-06-27 15:53:34 +0000463#ifndef SQLITE_OMIT_VIRTUALTABLE
drh8c8a8c42013-08-06 07:45:08 +0000464 case OP_VUpdate: {
465 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
466 break;
467 }
468 case OP_VFilter: {
469 int n;
470 assert( p->nOp - i >= 3 );
471 assert( pOp[-1].opcode==OP_Integer );
472 n = pOp[-1].p1;
473 if( n>nMaxArgs ) nMaxArgs = n;
474 break;
475 }
danielk1977182c4ba2007-06-27 15:53:34 +0000476#endif
drh8c8a8c42013-08-06 07:45:08 +0000477 case OP_Next:
drhf93cd942013-11-21 03:12:25 +0000478 case OP_NextIfOpen:
drh8c8a8c42013-08-06 07:45:08 +0000479 case OP_SorterNext: {
480 pOp->p4.xAdvance = sqlite3BtreeNext;
481 pOp->p4type = P4_ADVANCE;
482 break;
483 }
drhf93cd942013-11-21 03:12:25 +0000484 case OP_Prev:
485 case OP_PrevIfOpen: {
drh8c8a8c42013-08-06 07:45:08 +0000486 pOp->p4.xAdvance = sqlite3BtreePrevious;
487 pOp->p4type = P4_ADVANCE;
488 break;
489 }
danielk1977bc04f852005-03-29 08:26:13 +0000490 }
danielk1977634f2982005-03-28 08:44:07 +0000491
drh8c8a8c42013-08-06 07:45:08 +0000492 pOp->opflags = sqlite3OpcodeProperty[opcode];
drha6c2ed92009-11-14 23:22:23 +0000493 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
drh73d5b8f2013-12-23 19:09:07 +0000494 assert( -1-pOp->p2<pParse->nLabel );
drhd2981512008-01-04 19:33:49 +0000495 pOp->p2 = aLabel[-1-pOp->p2];
496 }
drh76ff3a02004-09-24 22:32:30 +0000497 }
drh73d5b8f2013-12-23 19:09:07 +0000498 sqlite3DbFree(p->db, pParse->aLabel);
499 pParse->aLabel = 0;
500 pParse->nLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000501 *pMaxFuncArgs = nMaxArgs;
danc0537fe2013-06-28 19:41:43 +0000502 assert( p->bIsReader!=0 || p->btreeMask==0 );
drh76ff3a02004-09-24 22:32:30 +0000503}
504
505/*
drh9a324642003-09-06 20:12:01 +0000506** Return the address of the next instruction to be inserted.
507*/
danielk19774adee202004-05-08 08:23:19 +0000508int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000509 assert( p->magic==VDBE_MAGIC_INIT );
510 return p->nOp;
511}
512
dan65a7cd12009-09-01 12:16:01 +0000513/*
514** This function returns a pointer to the array of opcodes associated with
515** the Vdbe passed as the first argument. It is the callers responsibility
516** to arrange for the returned array to be eventually freed using the
517** vdbeFreeOpArray() function.
518**
519** Before returning, *pnOp is set to the number of entries in the returned
520** array. Also, *pnMaxArg is set to the larger of its current value and
521** the number of entries in the Vdbe.apArg[] array required to execute the
522** returned program.
523*/
dan165921a2009-08-28 18:53:45 +0000524VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
525 VdbeOp *aOp = p->aOp;
dan523a0872009-08-31 05:23:32 +0000526 assert( aOp && !p->db->mallocFailed );
dan65a7cd12009-09-01 12:16:01 +0000527
528 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
drhbdaec522011-04-04 00:14:43 +0000529 assert( p->btreeMask==0 );
dan65a7cd12009-09-01 12:16:01 +0000530
dan165921a2009-08-28 18:53:45 +0000531 resolveP2Values(p, pnMaxArg);
532 *pnOp = p->nOp;
533 p->aOp = 0;
534 return aOp;
535}
536
drh9a324642003-09-06 20:12:01 +0000537/*
538** Add a whole list of operations to the operation stack. Return the
539** address of the first operation added.
540*/
drh688852a2014-02-17 22:40:43 +0000541int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
drh9a324642003-09-06 20:12:01 +0000542 int addr;
543 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +0000544 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000545 return 0;
drh9a324642003-09-06 20:12:01 +0000546 }
547 addr = p->nOp;
drh7b746032009-06-26 12:15:22 +0000548 if( ALWAYS(nOp>0) ){
drh9a324642003-09-06 20:12:01 +0000549 int i;
drh905793e2004-02-21 13:31:09 +0000550 VdbeOpList const *pIn = aOp;
551 for(i=0; i<nOp; i++, pIn++){
552 int p2 = pIn->p2;
553 VdbeOp *pOut = &p->aOp[i+addr];
554 pOut->opcode = pIn->opcode;
555 pOut->p1 = pIn->p1;
drh4308e342013-11-11 16:55:52 +0000556 if( p2<0 ){
557 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
drh8558cde2008-01-05 05:20:10 +0000558 pOut->p2 = addr + ADDR(p2);
559 }else{
560 pOut->p2 = p2;
561 }
drh24003452008-01-03 01:28:59 +0000562 pOut->p3 = pIn->p3;
563 pOut->p4type = P4_NOTUSED;
564 pOut->p4.p = 0;
565 pOut->p5 = 0;
drhc7379ce2013-10-30 02:28:23 +0000566#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000567 pOut->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000568#endif
drh688852a2014-02-17 22:40:43 +0000569#ifdef SQLITE_VDBE_COVERAGE
570 pOut->iSrcLine = iLineno+i;
571#else
572 (void)iLineno;
573#endif
drhc7379ce2013-10-30 02:28:23 +0000574#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000575 if( p->db->flags & SQLITE_VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000577 }
578#endif
579 }
580 p->nOp += nOp;
581 }
582 return addr;
583}
584
585/*
586** Change the value of the P1 operand for a specific instruction.
587** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000588** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000589** few minor changes to the program.
590*/
drh88caeac2011-08-24 15:12:08 +0000591void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000592 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000593 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000594 p->aOp[addr].p1 = val;
595 }
596}
597
598/*
599** Change the value of the P2 operand for a specific instruction.
600** This routine is useful for setting a jump destination.
601*/
drh88caeac2011-08-24 15:12:08 +0000602void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000603 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000604 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000605 p->aOp[addr].p2 = val;
606 }
607}
608
drhd654be82005-09-20 17:42:23 +0000609/*
danielk19771f4aa332008-01-03 09:51:55 +0000610** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000611*/
drh88caeac2011-08-24 15:12:08 +0000612void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000613 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000614 if( ((u32)p->nOp)>addr ){
danielk1977207872a2008-01-03 07:54:23 +0000615 p->aOp[addr].p3 = val;
616 }
617}
618
619/*
drh35573352008-01-08 23:54:25 +0000620** Change the value of the P5 operand for the most recently
621** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000622*/
drh35573352008-01-08 23:54:25 +0000623void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
drh7b746032009-06-26 12:15:22 +0000624 assert( p!=0 );
625 if( p->aOp ){
drh35573352008-01-08 23:54:25 +0000626 assert( p->nOp>0 );
627 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000628 }
629}
630
631/*
drhf8875402006-03-17 13:56:34 +0000632** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000633** the address of the next instruction to be coded.
634*/
635void sqlite3VdbeJumpHere(Vdbe *p, int addr){
drh61019c72014-01-04 16:49:02 +0000636 sqlite3VdbeChangeP2(p, addr, p->nOp);
637 p->pParse->iFixedOp = p->nOp - 1;
drhd654be82005-09-20 17:42:23 +0000638}
drhb38ad992005-09-16 00:27:01 +0000639
drhb7f6f682006-07-08 17:06:43 +0000640
641/*
642** If the input FuncDef structure is ephemeral, then free it. If
643** the FuncDef is not ephermal, then do nothing.
644*/
drh633e6d52008-07-28 19:34:53 +0000645static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhd36e1042013-09-06 13:10:12 +0000646 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000647 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000648 }
649}
650
dand46def72010-07-24 11:28:28 +0000651static void vdbeFreeOpArray(sqlite3 *, Op *, int);
652
drhb38ad992005-09-16 00:27:01 +0000653/*
drh66a51672008-01-03 00:01:23 +0000654** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000655*/
drh633e6d52008-07-28 19:34:53 +0000656static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000657 if( p4 ){
dand46def72010-07-24 11:28:28 +0000658 assert( db );
drh66a51672008-01-03 00:01:23 +0000659 switch( p4type ){
660 case P4_REAL:
661 case P4_INT64:
drh66a51672008-01-03 00:01:23 +0000662 case P4_DYNAMIC:
drh2ec2fb22013-11-06 19:59:23 +0000663 case P4_INTARRAY: {
drh633e6d52008-07-28 19:34:53 +0000664 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000665 break;
666 }
drh2ec2fb22013-11-06 19:59:23 +0000667 case P4_KEYINFO: {
668 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
669 break;
670 }
drhb9755982010-07-24 16:34:37 +0000671 case P4_MPRINTF: {
drh7043db92010-07-26 12:38:12 +0000672 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
drhb9755982010-07-24 16:34:37 +0000673 break;
674 }
drh66a51672008-01-03 00:01:23 +0000675 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000676 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000677 break;
678 }
drh66a51672008-01-03 00:01:23 +0000679 case P4_MEM: {
drhc176c272010-07-26 13:57:59 +0000680 if( db->pnBytesFreed==0 ){
681 sqlite3ValueFree((sqlite3_value*)p4);
682 }else{
drhf37c68e2010-07-26 14:20:06 +0000683 Mem *p = (Mem*)p4;
684 sqlite3DbFree(db, p->zMalloc);
685 sqlite3DbFree(db, p);
drhc176c272010-07-26 13:57:59 +0000686 }
drhac1733d2005-09-17 17:58:22 +0000687 break;
688 }
danielk1977595a5232009-07-24 17:58:53 +0000689 case P4_VTAB : {
dand46def72010-07-24 11:28:28 +0000690 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
danielk1977595a5232009-07-24 17:58:53 +0000691 break;
692 }
drhb38ad992005-09-16 00:27:01 +0000693 }
694 }
695}
696
dan65a7cd12009-09-01 12:16:01 +0000697/*
698** Free the space allocated for aOp and any p4 values allocated for the
699** opcodes contained within. If aOp is not NULL it is assumed to contain
700** nOp entries.
701*/
dan165921a2009-08-28 18:53:45 +0000702static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
703 if( aOp ){
704 Op *pOp;
705 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
706 freeP4(db, pOp->p4type, pOp->p4.p);
drhc7379ce2013-10-30 02:28:23 +0000707#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000708 sqlite3DbFree(db, pOp->zComment);
709#endif
710 }
711 }
712 sqlite3DbFree(db, aOp);
713}
714
dan65a7cd12009-09-01 12:16:01 +0000715/*
dand19c9332010-07-26 12:05:17 +0000716** Link the SubProgram object passed as the second argument into the linked
717** list at Vdbe.pSubProgram. This list is used to delete all sub-program
718** objects when the VM is no longer required.
dan65a7cd12009-09-01 12:16:01 +0000719*/
dand19c9332010-07-26 12:05:17 +0000720void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
721 p->pNext = pVdbe->pProgram;
722 pVdbe->pProgram = p;
dan165921a2009-08-28 18:53:45 +0000723}
724
drh9a324642003-09-06 20:12:01 +0000725/*
drh48f2d3b2011-09-16 01:34:43 +0000726** Change the opcode at addr into OP_Noop
drhf8875402006-03-17 13:56:34 +0000727*/
drh48f2d3b2011-09-16 01:34:43 +0000728void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
drh7b746032009-06-26 12:15:22 +0000729 if( p->aOp ){
danielk197792d4d7a2007-05-04 12:05:56 +0000730 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000731 sqlite3 *db = p->db;
drh48f2d3b2011-09-16 01:34:43 +0000732 freeP4(db, pOp->p4type, pOp->p4.p);
733 memset(pOp, 0, sizeof(pOp[0]));
734 pOp->opcode = OP_Noop;
drh313619f2013-10-31 20:34:06 +0000735 if( addr==p->nOp-1 ) p->nOp--;
drhf8875402006-03-17 13:56:34 +0000736 }
737}
738
739/*
drh762c1c42014-01-02 19:35:30 +0000740** Remove the last opcode inserted
741*/
drh61019c72014-01-04 16:49:02 +0000742int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
743 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
744 sqlite3VdbeChangeToNoop(p, p->nOp-1);
745 return 1;
746 }else{
747 return 0;
748 }
drh762c1c42014-01-02 19:35:30 +0000749}
750
751/*
drh66a51672008-01-03 00:01:23 +0000752** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000753** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000754** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000755** few minor changes to the program.
756**
drh66a51672008-01-03 00:01:23 +0000757** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000758** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000759** A value of n==0 means copy bytes of zP4 up to and including the
760** first null byte. If n>0 then copy n+1 bytes of zP4.
danielk19771f55c052005-05-19 08:42:59 +0000761**
drh66a51672008-01-03 00:01:23 +0000762** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000763** to a string or structure that is guaranteed to exist for the lifetime of
764** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000765**
drh66a51672008-01-03 00:01:23 +0000766** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000767*/
drh66a51672008-01-03 00:01:23 +0000768void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000769 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000770 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000771 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000772 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000773 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000774 if( p->aOp==0 || db->mallocFailed ){
drh2ec2fb22013-11-06 19:59:23 +0000775 if( n!=P4_VTAB ){
drh633e6d52008-07-28 19:34:53 +0000776 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000777 }
danielk1977d5d56522005-03-16 12:15:20 +0000778 return;
779 }
drh7b746032009-06-26 12:15:22 +0000780 assert( p->nOp>0 );
drh91fd4d42008-01-19 20:11:25 +0000781 assert( addr<p->nOp );
782 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000783 addr = p->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000784 }
785 pOp = &p->aOp[addr];
drh079a3072014-03-19 14:10:55 +0000786 assert( pOp->p4type==P4_NOTUSED
787 || pOp->p4type==P4_INT32
788 || pOp->p4type==P4_KEYINFO );
drh633e6d52008-07-28 19:34:53 +0000789 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000790 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000791 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000792 /* Note: this cast is safe, because the origin data point was an int
793 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000794 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000795 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000796 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000797 pOp->p4.p = 0;
798 pOp->p4type = P4_NOTUSED;
799 }else if( n==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +0000800 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000801 pOp->p4type = P4_KEYINFO;
danielk1977595a5232009-07-24 17:58:53 +0000802 }else if( n==P4_VTAB ){
803 pOp->p4.p = (void*)zP4;
804 pOp->p4type = P4_VTAB;
805 sqlite3VtabLock((VTable *)zP4);
806 assert( ((VTable *)zP4)->db==p->db );
drh9a324642003-09-06 20:12:01 +0000807 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000808 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000809 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000810 }else{
drhea678832008-12-10 19:26:22 +0000811 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000812 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000813 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000814 }
815}
816
drh2ec2fb22013-11-06 19:59:23 +0000817/*
818** Set the P4 on the most recently added opcode to the KeyInfo for the
819** index given.
820*/
821void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
822 Vdbe *v = pParse->pVdbe;
823 assert( v!=0 );
824 assert( pIdx!=0 );
825 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
826 P4_KEYINFO);
827}
828
drhc7379ce2013-10-30 02:28:23 +0000829#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drhad6d9462004-09-19 02:15:24 +0000830/*
mistachkind5578432012-08-25 10:01:29 +0000831** Change the comment on the most recently coded instruction. Or
drh16ee60f2008-06-20 18:13:25 +0000832** insert a No-op and add the comment to that new instruction. This
833** makes the code easier to read during debugging. None of this happens
834** in a production build.
drhad6d9462004-09-19 02:15:24 +0000835*/
drhb07028f2011-10-14 21:49:18 +0000836static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
danielk197701256832007-04-18 14:24:32 +0000837 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000838 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000839 if( p->nOp ){
drhb07028f2011-10-14 21:49:18 +0000840 assert( p->aOp );
841 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
842 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
843 }
844}
845void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
846 va_list ap;
847 if( p ){
danielk1977dba01372008-01-05 18:44:29 +0000848 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000849 vdbeVComment(p, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000850 va_end(ap);
851 }
drhad6d9462004-09-19 02:15:24 +0000852}
drh16ee60f2008-06-20 18:13:25 +0000853void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
854 va_list ap;
drhb07028f2011-10-14 21:49:18 +0000855 if( p ){
856 sqlite3VdbeAddOp0(p, OP_Noop);
drh16ee60f2008-06-20 18:13:25 +0000857 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000858 vdbeVComment(p, zFormat, ap);
drh16ee60f2008-06-20 18:13:25 +0000859 va_end(ap);
860 }
861}
862#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000863
drh688852a2014-02-17 22:40:43 +0000864#ifdef SQLITE_VDBE_COVERAGE
865/*
866** Set the value if the iSrcLine field for the previously coded instruction.
867*/
868void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
869 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
870}
871#endif /* SQLITE_VDBE_COVERAGE */
872
drh9a324642003-09-06 20:12:01 +0000873/*
drh20411ea2009-05-29 19:00:12 +0000874** Return the opcode for a given address. If the address is -1, then
875** return the most recently inserted opcode.
876**
877** If a memory allocation error has occurred prior to the calling of this
878** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
drhf83dc1e2010-06-03 12:09:52 +0000879** is readable but not writable, though it is cast to a writable value.
880** The return of a dummy opcode allows the call to continue functioning
881** after a OOM fault without having to check to see if the return from
882** this routine is a valid pointer. But because the dummy.opcode is 0,
883** dummy will never be written to. This is verified by code inspection and
884** by running with Valgrind.
drh9a324642003-09-06 20:12:01 +0000885*/
danielk19774adee202004-05-08 08:23:19 +0000886VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drha0b75da2010-07-02 18:44:37 +0000887 /* C89 specifies that the constant "dummy" will be initialized to all
888 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
mistachkin0fe5f952011-09-14 18:19:08 +0000889 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
drh9a324642003-09-06 20:12:01 +0000890 assert( p->magic==VDBE_MAGIC_INIT );
drh37b89a02009-06-19 00:33:31 +0000891 if( addr<0 ){
drh37b89a02009-06-19 00:33:31 +0000892 addr = p->nOp - 1;
893 }
drh17435752007-08-16 04:30:38 +0000894 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000895 if( p->db->mallocFailed ){
drhf83dc1e2010-06-03 12:09:52 +0000896 return (VdbeOp*)&dummy;
drh20411ea2009-05-29 19:00:12 +0000897 }else{
898 return &p->aOp[addr];
899 }
drh9a324642003-09-06 20:12:01 +0000900}
901
drhc7379ce2013-10-30 02:28:23 +0000902#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
drh81316f82013-10-29 20:40:47 +0000903/*
drhf63552b2013-10-30 00:25:03 +0000904** Return an integer value for one of the parameters to the opcode pOp
905** determined by character c.
906*/
907static int translateP(char c, const Op *pOp){
908 if( c=='1' ) return pOp->p1;
909 if( c=='2' ) return pOp->p2;
910 if( c=='3' ) return pOp->p3;
911 if( c=='4' ) return pOp->p4.i;
912 return pOp->p5;
913}
914
drh81316f82013-10-29 20:40:47 +0000915/*
drh4eded602013-12-20 15:59:20 +0000916** Compute a string for the "comment" field of a VDBE opcode listing.
917**
918** The Synopsis: field in comments in the vdbe.c source file gets converted
919** to an extra string that is appended to the sqlite3OpcodeName(). In the
920** absence of other comments, this synopsis becomes the comment on the opcode.
921** Some translation occurs:
922**
923** "PX" -> "r[X]"
924** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
925** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
926** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
drh81316f82013-10-29 20:40:47 +0000927*/
drhf63552b2013-10-30 00:25:03 +0000928static int displayComment(
929 const Op *pOp, /* The opcode to be commented */
930 const char *zP4, /* Previously obtained value for P4 */
931 char *zTemp, /* Write result here */
932 int nTemp /* Space available in zTemp[] */
933){
drh81316f82013-10-29 20:40:47 +0000934 const char *zOpName;
935 const char *zSynopsis;
936 int nOpName;
937 int ii, jj;
938 zOpName = sqlite3OpcodeName(pOp->opcode);
939 nOpName = sqlite3Strlen30(zOpName);
940 if( zOpName[nOpName+1] ){
941 int seenCom = 0;
drhf63552b2013-10-30 00:25:03 +0000942 char c;
drh81316f82013-10-29 20:40:47 +0000943 zSynopsis = zOpName += nOpName + 1;
drhf63552b2013-10-30 00:25:03 +0000944 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
945 if( c=='P' ){
946 c = zSynopsis[++ii];
947 if( c=='4' ){
948 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
949 }else if( c=='X' ){
950 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
951 seenCom = 1;
drh81316f82013-10-29 20:40:47 +0000952 }else{
drhf63552b2013-10-30 00:25:03 +0000953 int v1 = translateP(c, pOp);
954 int v2;
955 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
956 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
957 ii += 3;
958 jj += sqlite3Strlen30(zTemp+jj);
959 v2 = translateP(zSynopsis[ii], pOp);
drh4eded602013-12-20 15:59:20 +0000960 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
961 ii += 2;
962 v2++;
963 }
964 if( v2>1 ){
965 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
966 }
drhf63552b2013-10-30 00:25:03 +0000967 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
968 ii += 4;
969 }
drh81316f82013-10-29 20:40:47 +0000970 }
971 jj += sqlite3Strlen30(zTemp+jj);
972 }else{
drhf63552b2013-10-30 00:25:03 +0000973 zTemp[jj++] = c;
drh81316f82013-10-29 20:40:47 +0000974 }
975 }
976 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
977 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
978 jj += sqlite3Strlen30(zTemp+jj);
979 }
980 if( jj<nTemp ) zTemp[jj] = 0;
981 }else if( pOp->zComment ){
982 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
983 jj = sqlite3Strlen30(zTemp);
984 }else{
985 zTemp[0] = 0;
986 jj = 0;
987 }
988 return jj;
989}
990#endif /* SQLITE_DEBUG */
991
992
drhb7f91642004-10-31 02:22:47 +0000993#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
994 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000995/*
drh66a51672008-01-03 00:01:23 +0000996** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000997** Use zTemp for any required temporary buffer space.
998*/
drh66a51672008-01-03 00:01:23 +0000999static char *displayP4(Op *pOp, char *zTemp, int nTemp){
1000 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +00001001 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +00001002 switch( pOp->p4type ){
1003 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +00001004 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +00001005 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drhe1a022e2012-09-17 17:16:53 +00001006 assert( pKeyInfo->aSortOrder!=0 );
drh5b843aa2013-10-30 13:46:01 +00001007 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +00001008 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +00001009 for(j=0; j<pKeyInfo->nField; j++){
1010 CollSeq *pColl = pKeyInfo->aColl[j];
drh261d8a52012-12-08 21:36:26 +00001011 const char *zColl = pColl ? pColl->zName : "nil";
1012 int n = sqlite3Strlen30(zColl);
drh5b843aa2013-10-30 13:46:01 +00001013 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
1014 zColl = "B";
1015 n = 1;
1016 }
drh261d8a52012-12-08 21:36:26 +00001017 if( i+n>nTemp-6 ){
1018 memcpy(&zTemp[i],",...",4);
1019 break;
drhd3d39e92004-05-20 22:16:29 +00001020 }
drh261d8a52012-12-08 21:36:26 +00001021 zTemp[i++] = ',';
1022 if( pKeyInfo->aSortOrder[j] ){
1023 zTemp[i++] = '-';
1024 }
1025 memcpy(&zTemp[i], zColl, n+1);
1026 i += n;
drhd3d39e92004-05-20 22:16:29 +00001027 }
1028 zTemp[i++] = ')';
1029 zTemp[i] = 0;
1030 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +00001031 break;
1032 }
drh66a51672008-01-03 00:01:23 +00001033 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +00001034 CollSeq *pColl = pOp->p4.pColl;
drh5e6790c2013-11-12 20:18:14 +00001035 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +00001036 break;
1037 }
drh66a51672008-01-03 00:01:23 +00001038 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +00001039 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +00001040 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +00001041 break;
1042 }
drh66a51672008-01-03 00:01:23 +00001043 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +00001044 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +00001045 break;
1046 }
drh66a51672008-01-03 00:01:23 +00001047 case P4_INT32: {
1048 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +00001049 break;
1050 }
drh66a51672008-01-03 00:01:23 +00001051 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +00001052 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +00001053 break;
1054 }
drh66a51672008-01-03 00:01:23 +00001055 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +00001056 Mem *pMem = pOp->p4.pMem;
drhd4e70eb2008-01-02 00:34:36 +00001057 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +00001058 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +00001059 }else if( pMem->flags & MEM_Int ){
1060 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
1061 }else if( pMem->flags & MEM_Real ){
1062 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhb8475df2011-12-09 16:21:19 +00001063 }else if( pMem->flags & MEM_Null ){
1064 sqlite3_snprintf(nTemp, zTemp, "NULL");
drh56016892009-08-25 14:24:04 +00001065 }else{
1066 assert( pMem->flags & MEM_Blob );
1067 zP4 = "(blob)";
drhd4e70eb2008-01-02 00:34:36 +00001068 }
drh598f1342007-10-23 15:39:45 +00001069 break;
1070 }
drha967e882006-06-13 01:04:52 +00001071#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +00001072 case P4_VTAB: {
danielk1977595a5232009-07-24 17:58:53 +00001073 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
drh19146192006-06-26 19:10:32 +00001074 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +00001075 break;
1076 }
1077#endif
drh0acb7e42008-06-25 00:12:41 +00001078 case P4_INTARRAY: {
1079 sqlite3_snprintf(nTemp, zTemp, "intarray");
1080 break;
1081 }
dan165921a2009-08-28 18:53:45 +00001082 case P4_SUBPROGRAM: {
1083 sqlite3_snprintf(nTemp, zTemp, "program");
1084 break;
1085 }
drh4a6f3aa2011-08-28 00:19:26 +00001086 case P4_ADVANCE: {
1087 zTemp[0] = 0;
1088 break;
1089 }
drhd3d39e92004-05-20 22:16:29 +00001090 default: {
danielk19772dca4ac2008-01-03 11:50:29 +00001091 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +00001092 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +00001093 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +00001094 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +00001095 }
1096 }
1097 }
drh66a51672008-01-03 00:01:23 +00001098 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +00001099 return zP4;
drhd3d39e92004-05-20 22:16:29 +00001100}
drhb7f91642004-10-31 02:22:47 +00001101#endif
drhd3d39e92004-05-20 22:16:29 +00001102
drh900b31e2007-08-28 02:27:51 +00001103/*
drhd0679ed2007-08-28 22:24:34 +00001104** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
drh3ebaee92010-05-06 21:37:22 +00001105**
drhbdaec522011-04-04 00:14:43 +00001106** The prepared statements need to know in advance the complete set of
drhe4c88c02012-01-04 12:57:45 +00001107** attached databases that will be use. A mask of these databases
1108** is maintained in p->btreeMask. The p->lockMask value is the subset of
1109** p->btreeMask of databases that will require a lock.
drh900b31e2007-08-28 02:27:51 +00001110*/
drhfb982642007-08-30 01:19:59 +00001111void sqlite3VdbeUsesBtree(Vdbe *p, int i){
drhfcd71b62011-04-05 22:08:24 +00001112 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
danielk197700e13612008-11-17 19:18:54 +00001113 assert( i<(int)sizeof(p->btreeMask)*8 );
drhbdaec522011-04-04 00:14:43 +00001114 p->btreeMask |= ((yDbMask)1)<<i;
drhdc5b0472011-04-06 22:05:53 +00001115 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
1116 p->lockMask |= ((yDbMask)1)<<i;
1117 }
drh900b31e2007-08-28 02:27:51 +00001118}
1119
drhe54e0512011-04-05 17:31:56 +00001120#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001121/*
1122** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1123** this routine obtains the mutex associated with each BtShared structure
1124** that may be accessed by the VM passed as an argument. In doing so it also
1125** sets the BtShared.db member of each of the BtShared structures, ensuring
1126** that the correct busy-handler callback is invoked if required.
1127**
1128** If SQLite is not threadsafe but does support shared-cache mode, then
1129** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1130** of all of BtShared structures accessible via the database handle
1131** associated with the VM.
1132**
1133** If SQLite is not threadsafe and does not support shared-cache mode, this
1134** function is a no-op.
1135**
1136** The p->btreeMask field is a bitmask of all btrees that the prepared
1137** statement p will ever use. Let N be the number of bits in p->btreeMask
1138** corresponding to btrees that use shared cache. Then the runtime of
1139** this routine is N*N. But as N is rarely more than 1, this should not
1140** be a problem.
1141*/
1142void sqlite3VdbeEnter(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001143 int i;
1144 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001145 sqlite3 *db;
1146 Db *aDb;
1147 int nDb;
1148 if( p->lockMask==0 ) return; /* The common case */
1149 db = p->db;
1150 aDb = db->aDb;
1151 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001152 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001153 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001154 sqlite3BtreeEnter(aDb[i].pBt);
1155 }
1156 }
drhbdaec522011-04-04 00:14:43 +00001157}
drhe54e0512011-04-05 17:31:56 +00001158#endif
drhbdaec522011-04-04 00:14:43 +00001159
drhe54e0512011-04-05 17:31:56 +00001160#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001161/*
1162** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1163*/
1164void sqlite3VdbeLeave(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001165 int i;
1166 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001167 sqlite3 *db;
1168 Db *aDb;
1169 int nDb;
1170 if( p->lockMask==0 ) return; /* The common case */
1171 db = p->db;
1172 aDb = db->aDb;
1173 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001174 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001175 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001176 sqlite3BtreeLeave(aDb[i].pBt);
1177 }
1178 }
drhbdaec522011-04-04 00:14:43 +00001179}
drhbdaec522011-04-04 00:14:43 +00001180#endif
drhd3d39e92004-05-20 22:16:29 +00001181
danielk19778b60e0f2005-01-12 09:10:39 +00001182#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +00001183/*
1184** Print a single opcode. This routine is used for debugging only.
1185*/
danielk19774adee202004-05-08 08:23:19 +00001186void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +00001187 char *zP4;
drhd3d39e92004-05-20 22:16:29 +00001188 char zPtr[50];
drh81316f82013-10-29 20:40:47 +00001189 char zCom[100];
drh26198bb2013-10-31 11:15:09 +00001190 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +00001191 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +00001192 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
drhc7379ce2013-10-30 02:28:23 +00001193#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001194 displayComment(pOp, zP4, zCom, sizeof(zCom));
1195#else
drh2926f962014-02-17 01:13:28 +00001196 zCom[0] = 0;
drh81316f82013-10-29 20:40:47 +00001197#endif
drh4eded602013-12-20 15:59:20 +00001198 /* NB: The sqlite3OpcodeName() function is implemented by code created
1199 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1200 ** information from the vdbe.c source text */
danielk197711641c12008-01-03 08:18:30 +00001201 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +00001202 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
drh81316f82013-10-29 20:40:47 +00001203 zCom
drh1db639c2008-01-17 02:36:28 +00001204 );
drh9a324642003-09-06 20:12:01 +00001205 fflush(pOut);
1206}
1207#endif
1208
1209/*
drh76ff3a02004-09-24 22:32:30 +00001210** Release an array of N Mem elements
1211*/
drhc890fec2008-08-01 20:10:08 +00001212static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +00001213 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +00001214 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +00001215 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +00001216 u8 malloc_failed = db->mallocFailed;
dand46def72010-07-24 11:28:28 +00001217 if( db->pnBytesFreed ){
1218 for(pEnd=&p[N]; p<pEnd; p++){
1219 sqlite3DbFree(db, p->zMalloc);
1220 }
drhc176c272010-07-26 13:57:59 +00001221 return;
1222 }
danielk1977e972e032008-09-19 18:32:26 +00001223 for(pEnd=&p[N]; p<pEnd; p++){
1224 assert( (&p[1])==pEnd || p[0].db==p[1].db );
drh75fd0542014-03-01 16:24:44 +00001225 assert( sqlite3VdbeCheckMemInvariants(p) );
danielk1977e972e032008-09-19 18:32:26 +00001226
1227 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1228 ** that takes advantage of the fact that the memory cell value is
1229 ** being set to NULL after releasing any dynamic resources.
1230 **
1231 ** The justification for duplicating code is that according to
1232 ** callgrind, this causes a certain test case to hit the CPU 4.7
1233 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1234 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1235 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1236 ** with no indexes using a single prepared INSERT statement, bind()
1237 ** and reset(). Inserts are grouped into a transaction.
1238 */
drhb6e8fd12014-03-06 01:56:33 +00001239 testcase( p->flags & MEM_Agg );
1240 testcase( p->flags & MEM_Dyn );
1241 testcase( p->flags & MEM_Frame );
1242 testcase( p->flags & MEM_RowSet );
dan165921a2009-08-28 18:53:45 +00001243 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001244 sqlite3VdbeMemRelease(p);
1245 }else if( p->zMalloc ){
1246 sqlite3DbFree(db, p->zMalloc);
1247 p->zMalloc = 0;
1248 }
1249
drha5750cf2014-02-07 13:20:31 +00001250 p->flags = MEM_Undefined;
drh76ff3a02004-09-24 22:32:30 +00001251 }
danielk1977a7a8e142008-02-13 18:25:27 +00001252 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001253 }
1254}
1255
dan65a7cd12009-09-01 12:16:01 +00001256/*
1257** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1258** allocated by the OP_Program opcode in sqlite3VdbeExec().
1259*/
dan165921a2009-08-28 18:53:45 +00001260void sqlite3VdbeFrameDelete(VdbeFrame *p){
1261 int i;
1262 Mem *aMem = VdbeFrameMem(p);
1263 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1264 for(i=0; i<p->nChildCsr; i++){
1265 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1266 }
1267 releaseMemArray(aMem, p->nChildMem);
1268 sqlite3DbFree(p->v->db, p);
1269}
1270
drhb7f91642004-10-31 02:22:47 +00001271#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001272/*
drh9a324642003-09-06 20:12:01 +00001273** Give a listing of the program in the virtual machine.
1274**
danielk19774adee202004-05-08 08:23:19 +00001275** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001276** running the code, it invokes the callback once for each instruction.
1277** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001278**
1279** When p->explain==1, each instruction is listed. When
1280** p->explain==2, only OP_Explain instructions are listed and these
1281** are shown in a different format. p->explain==2 is used to implement
1282** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001283**
1284** When p->explain==1, first the main program is listed, then each of
1285** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001286*/
danielk19774adee202004-05-08 08:23:19 +00001287int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001288 Vdbe *p /* The VDBE */
1289){
drh5cfa5842009-12-31 20:35:08 +00001290 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001291 int nSub = 0; /* Number of sub-vdbes seen so far */
1292 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001293 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1294 sqlite3 *db = p->db; /* The database connection */
1295 int i; /* Loop counter */
1296 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001297 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001298
drh9a324642003-09-06 20:12:01 +00001299 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001300 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001301 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001302
drh9cbf3422008-01-17 16:22:13 +00001303 /* Even though this opcode does not use dynamic strings for
1304 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001305 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001306 */
dan165921a2009-08-28 18:53:45 +00001307 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001308 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001309
danielk19776c359f02008-11-21 16:58:03 +00001310 if( p->rc==SQLITE_NOMEM ){
1311 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1312 ** sqlite3_column_text16() failed. */
1313 db->mallocFailed = 1;
1314 return SQLITE_ERROR;
1315 }
1316
drh5cfa5842009-12-31 20:35:08 +00001317 /* When the number of output rows reaches nRow, that means the
1318 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1319 ** nRow is the sum of the number of rows in the main program, plus
1320 ** the sum of the number of rows in all trigger subprograms encountered
1321 ** so far. The nRow value will increase as new trigger subprograms are
1322 ** encountered, but p->pc will eventually catch up to nRow.
1323 */
dan165921a2009-08-28 18:53:45 +00001324 nRow = p->nOp;
1325 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001326 /* The first 8 memory cells are used for the result set. So we will
1327 ** commandeer the 9th cell to use as storage for an array of pointers
1328 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1329 ** cells. */
1330 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001331 pSub = &p->aMem[9];
1332 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001333 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1334 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001335 nSub = pSub->n/sizeof(Vdbe*);
1336 apSub = (SubProgram **)pSub->z;
1337 }
1338 for(i=0; i<nSub; i++){
1339 nRow += apSub[i]->nOp;
1340 }
1341 }
1342
drhecc92422005-09-10 16:46:12 +00001343 do{
1344 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001345 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1346 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001347 p->rc = SQLITE_OK;
1348 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001349 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001350 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001351 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001352 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001353 }else{
drh81316f82013-10-29 20:40:47 +00001354 char *zP4;
dan165921a2009-08-28 18:53:45 +00001355 Op *pOp;
1356 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001357 /* The output line number is small enough that we are still in the
1358 ** main program. */
dan165921a2009-08-28 18:53:45 +00001359 pOp = &p->aOp[i];
1360 }else{
drh5cfa5842009-12-31 20:35:08 +00001361 /* We are currently listing subprograms. Figure out which one and
1362 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001363 int j;
1364 i -= p->nOp;
1365 for(j=0; i>=apSub[j]->nOp; j++){
1366 i -= apSub[j]->nOp;
1367 }
1368 pOp = &apSub[j]->aOp[i];
1369 }
danielk19770d78bae2008-01-03 07:09:48 +00001370 if( p->explain==1 ){
1371 pMem->flags = MEM_Int;
danielk19770d78bae2008-01-03 07:09:48 +00001372 pMem->u.i = i; /* Program counter */
1373 pMem++;
1374
1375 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001376 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
danielk19770d78bae2008-01-03 07:09:48 +00001377 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001378 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001379 pMem->enc = SQLITE_UTF8;
1380 pMem++;
dan165921a2009-08-28 18:53:45 +00001381
drh5cfa5842009-12-31 20:35:08 +00001382 /* When an OP_Program opcode is encounter (the only opcode that has
1383 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1384 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1385 ** has not already been seen.
1386 */
dan165921a2009-08-28 18:53:45 +00001387 if( pOp->p4type==P4_SUBPROGRAM ){
1388 int nByte = (nSub+1)*sizeof(SubProgram*);
1389 int j;
1390 for(j=0; j<nSub; j++){
1391 if( apSub[j]==pOp->p4.pProgram ) break;
1392 }
dan2b9ee772012-03-31 09:59:44 +00001393 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
dan165921a2009-08-28 18:53:45 +00001394 apSub = (SubProgram **)pSub->z;
1395 apSub[nSub++] = pOp->p4.pProgram;
1396 pSub->flags |= MEM_Blob;
1397 pSub->n = nSub*sizeof(SubProgram*);
1398 }
1399 }
danielk19770d78bae2008-01-03 07:09:48 +00001400 }
drheb2e1762004-05-27 01:53:56 +00001401
1402 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001403 pMem->u.i = pOp->p1; /* P1 */
drheb2e1762004-05-27 01:53:56 +00001404 pMem++;
1405
1406 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001407 pMem->u.i = pOp->p2; /* P2 */
drheb2e1762004-05-27 01:53:56 +00001408 pMem++;
1409
dan2ce22452010-11-08 19:01:16 +00001410 pMem->flags = MEM_Int;
1411 pMem->u.i = pOp->p3; /* P3 */
dan2ce22452010-11-08 19:01:16 +00001412 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001413
danielk1977a7a8e142008-02-13 18:25:27 +00001414 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001415 assert( p->db->mallocFailed );
1416 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001417 }
drhc91b2fd2014-03-01 18:13:23 +00001418 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001419 zP4 = displayP4(pOp, pMem->z, 32);
1420 if( zP4!=pMem->z ){
1421 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
danielk1977a7a8e142008-02-13 18:25:27 +00001422 }else{
1423 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001424 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001425 pMem->enc = SQLITE_UTF8;
1426 }
danielk19770d78bae2008-01-03 07:09:48 +00001427 pMem++;
drheb2e1762004-05-27 01:53:56 +00001428
danielk19770d78bae2008-01-03 07:09:48 +00001429 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001430 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001431 assert( p->db->mallocFailed );
1432 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001433 }
drhc91b2fd2014-03-01 18:13:23 +00001434 pMem->flags = MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001435 pMem->n = 2;
1436 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +00001437 pMem->enc = SQLITE_UTF8;
1438 pMem++;
1439
drhc7379ce2013-10-30 02:28:23 +00001440#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001441 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
1442 assert( p->db->mallocFailed );
1443 return SQLITE_ERROR;
drh52391cb2008-02-14 23:44:13 +00001444 }
drhc91b2fd2014-03-01 18:13:23 +00001445 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001446 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
drh81316f82013-10-29 20:40:47 +00001447 pMem->enc = SQLITE_UTF8;
1448#else
1449 pMem->flags = MEM_Null; /* Comment */
drh81316f82013-10-29 20:40:47 +00001450#endif
danielk19770d78bae2008-01-03 07:09:48 +00001451 }
1452
dan2ce22452010-11-08 19:01:16 +00001453 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001454 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001455 p->rc = SQLITE_OK;
1456 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001457 }
drh826fb5a2004-02-14 23:59:57 +00001458 return rc;
drh9a324642003-09-06 20:12:01 +00001459}
drhb7f91642004-10-31 02:22:47 +00001460#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001461
drh7c4ac0c2007-04-05 11:25:58 +00001462#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001463/*
drh3f7d4e42004-07-24 14:35:58 +00001464** Print the SQL that was used to generate a VDBE program.
1465*/
1466void sqlite3VdbePrintSql(Vdbe *p){
drh84e55a82013-11-13 17:58:23 +00001467 const char *z = 0;
1468 if( p->zSql ){
1469 z = p->zSql;
1470 }else if( p->nOp>=1 ){
1471 const VdbeOp *pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001472 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh84e55a82013-11-13 17:58:23 +00001473 z = pOp->p4.z;
1474 while( sqlite3Isspace(*z) ) z++;
1475 }
drh3f7d4e42004-07-24 14:35:58 +00001476 }
drh84e55a82013-11-13 17:58:23 +00001477 if( z ) printf("SQL: [%s]\n", z);
drh3f7d4e42004-07-24 14:35:58 +00001478}
drh7c4ac0c2007-04-05 11:25:58 +00001479#endif
drh3f7d4e42004-07-24 14:35:58 +00001480
drh602c2372007-03-01 00:29:13 +00001481#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1482/*
1483** Print an IOTRACE message showing SQL content.
1484*/
1485void sqlite3VdbeIOTraceSql(Vdbe *p){
1486 int nOp = p->nOp;
1487 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001488 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001489 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001490 pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001491 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001492 int i, j;
drh00a18e42007-08-13 11:10:34 +00001493 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001494 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001495 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001496 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001497 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001498 if( z[i-1]!=' ' ){
1499 z[j++] = ' ';
1500 }
1501 }else{
1502 z[j++] = z[i];
1503 }
1504 }
1505 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001506 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001507 }
1508}
1509#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1510
drhb2771ce2009-02-20 01:28:59 +00001511/*
drh4800b2e2009-12-08 15:35:22 +00001512** Allocate space from a fixed size buffer and return a pointer to
1513** that space. If insufficient space is available, return NULL.
1514**
1515** The pBuf parameter is the initial value of a pointer which will
1516** receive the new memory. pBuf is normally NULL. If pBuf is not
1517** NULL, it means that memory space has already been allocated and that
1518** this routine should not allocate any new memory. When pBuf is not
1519** NULL simply return pBuf. Only allocate new memory space when pBuf
1520** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001521**
1522** nByte is the number of bytes of space needed.
1523**
drh19875c82009-12-08 19:58:19 +00001524** *ppFrom points to available space and pEnd points to the end of the
1525** available space. When space is allocated, *ppFrom is advanced past
1526** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001527**
1528** *pnByte is a counter of the number of bytes of space that have failed
1529** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001530** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001531*/
drh4800b2e2009-12-08 15:35:22 +00001532static void *allocSpace(
1533 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001534 int nByte, /* Number of bytes to allocate */
1535 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001536 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001537 int *pnByte /* If allocation cannot be made, increment *pnByte */
1538){
drhea598cb2009-04-05 12:22:08 +00001539 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001540 if( pBuf ) return pBuf;
1541 nByte = ROUND8(nByte);
1542 if( &(*ppFrom)[nByte] <= pEnd ){
1543 pBuf = (void*)*ppFrom;
1544 *ppFrom += nByte;
1545 }else{
1546 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001547 }
drh4800b2e2009-12-08 15:35:22 +00001548 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001549}
drh602c2372007-03-01 00:29:13 +00001550
drh3f7d4e42004-07-24 14:35:58 +00001551/*
drh124c0b42011-06-01 18:15:55 +00001552** Rewind the VDBE back to the beginning in preparation for
1553** running it.
drh9a324642003-09-06 20:12:01 +00001554*/
drh124c0b42011-06-01 18:15:55 +00001555void sqlite3VdbeRewind(Vdbe *p){
1556#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1557 int i;
1558#endif
drh9a324642003-09-06 20:12:01 +00001559 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001560 assert( p->magic==VDBE_MAGIC_INIT );
1561
drhc16a03b2004-09-15 13:38:10 +00001562 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001563 */
drhc16a03b2004-09-15 13:38:10 +00001564 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001565
danielk197700e13612008-11-17 19:18:54 +00001566 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001567 p->magic = VDBE_MAGIC_RUN;
1568
drh124c0b42011-06-01 18:15:55 +00001569#ifdef SQLITE_DEBUG
1570 for(i=1; i<p->nMem; i++){
1571 assert( p->aMem[i].db==p->db );
1572 }
1573#endif
1574 p->pc = -1;
1575 p->rc = SQLITE_OK;
1576 p->errorAction = OE_Abort;
1577 p->magic = VDBE_MAGIC_RUN;
1578 p->nChange = 0;
1579 p->cacheCtr = 1;
1580 p->minWriteFileFormat = 255;
1581 p->iStatement = 0;
1582 p->nFkConstraint = 0;
1583#ifdef VDBE_PROFILE
1584 for(i=0; i<p->nOp; i++){
1585 p->aOp[i].cnt = 0;
1586 p->aOp[i].cycles = 0;
1587 }
1588#endif
1589}
1590
1591/*
1592** Prepare a virtual machine for execution for the first time after
1593** creating the virtual machine. This involves things such
1594** as allocating stack space and initializing the program counter.
1595** After the VDBE has be prepped, it can be executed by one or more
1596** calls to sqlite3VdbeExec().
1597**
1598** This function may be called exact once on a each virtual machine.
1599** After this routine is called the VM has been "packaged" and is ready
1600** to run. After this routine is called, futher calls to
1601** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1602** the Vdbe from the Parse object that helped generate it so that the
1603** the Vdbe becomes an independent entity and the Parse object can be
1604** destroyed.
1605**
1606** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1607** to its initial state after it has been run.
1608*/
1609void sqlite3VdbeMakeReady(
1610 Vdbe *p, /* The VDBE */
1611 Parse *pParse /* Parsing context */
1612){
1613 sqlite3 *db; /* The database connection */
1614 int nVar; /* Number of parameters */
1615 int nMem; /* Number of VM memory registers */
1616 int nCursor; /* Number of cursors required */
1617 int nArg; /* Number of arguments in subprograms */
dan1d8cb212011-12-09 13:24:16 +00001618 int nOnce; /* Number of OP_Once instructions */
drh124c0b42011-06-01 18:15:55 +00001619 int n; /* Loop counter */
1620 u8 *zCsr; /* Memory available for allocation */
1621 u8 *zEnd; /* First byte past allocated memory */
1622 int nByte; /* How much extra memory is needed */
1623
1624 assert( p!=0 );
1625 assert( p->nOp>0 );
1626 assert( pParse!=0 );
1627 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +00001628 assert( pParse==p->pParse );
drh124c0b42011-06-01 18:15:55 +00001629 db = p->db;
1630 assert( db->mallocFailed==0 );
1631 nVar = pParse->nVar;
1632 nMem = pParse->nMem;
1633 nCursor = pParse->nTab;
1634 nArg = pParse->nMaxArg;
dan1d8cb212011-12-09 13:24:16 +00001635 nOnce = pParse->nOnce;
drh20e226d2012-01-01 13:58:53 +00001636 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
drh124c0b42011-06-01 18:15:55 +00001637
danielk1977cd3e8f72008-03-25 09:47:35 +00001638 /* For each cursor required, also allocate a memory cell. Memory
1639 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1640 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001641 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001642 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1643 ** stores the blob of memory associated with cursor 1, etc.
1644 **
1645 ** See also: allocateCursor().
1646 */
1647 nMem += nCursor;
1648
danielk19776ab3a2e2009-02-19 14:39:25 +00001649 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001650 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001651 */
drh73d5b8f2013-12-23 19:09:07 +00001652 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1653 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001654
drh124c0b42011-06-01 18:15:55 +00001655 resolveP2Values(p, &nArg);
1656 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1657 if( pParse->explain && nMem<10 ){
1658 nMem = 10;
1659 }
1660 memset(zCsr, 0, zEnd-zCsr);
1661 zCsr += (zCsr - (u8*)0)&7;
1662 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001663 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001664
1665 /* Memory for registers, parameters, cursor, etc, is allocated in two
1666 ** passes. On the first pass, we try to reuse unused space at the
1667 ** end of the opcode array. If we are unable to satisfy all memory
1668 ** requirements by reusing the opcode array tail, then the second
1669 ** pass will fill in the rest using a fresh allocation.
1670 **
1671 ** This two-pass approach that reuses as much memory as possible from
1672 ** the leftover space at the end of the opcode array can significantly
1673 ** reduce the amount of memory held by a prepared statement.
1674 */
1675 do {
1676 nByte = 0;
1677 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1678 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1679 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1680 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1681 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1682 &zCsr, zEnd, &nByte);
drhb8475df2011-12-09 16:21:19 +00001683 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
drh124c0b42011-06-01 18:15:55 +00001684 if( nByte ){
1685 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001686 }
drh124c0b42011-06-01 18:15:55 +00001687 zCsr = p->pFree;
1688 zEnd = &zCsr[nByte];
1689 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001690
drhd2a56232013-01-28 19:00:20 +00001691 p->nCursor = nCursor;
dan1d8cb212011-12-09 13:24:16 +00001692 p->nOnceFlag = nOnce;
drh124c0b42011-06-01 18:15:55 +00001693 if( p->aVar ){
1694 p->nVar = (ynVar)nVar;
1695 for(n=0; n<nVar; n++){
1696 p->aVar[n].flags = MEM_Null;
1697 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001698 }
drh82a48512003-09-06 22:45:20 +00001699 }
drh124c0b42011-06-01 18:15:55 +00001700 if( p->azVar ){
1701 p->nzVar = pParse->nzVar;
1702 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1703 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001704 }
drh124c0b42011-06-01 18:15:55 +00001705 if( p->aMem ){
1706 p->aMem--; /* aMem[] goes from 1..nMem */
1707 p->nMem = nMem; /* not from 0..nMem-1 */
1708 for(n=1; n<=nMem; n++){
drha5750cf2014-02-07 13:20:31 +00001709 p->aMem[n].flags = MEM_Undefined;
drh124c0b42011-06-01 18:15:55 +00001710 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001711 }
drh9a324642003-09-06 20:12:01 +00001712 }
drh124c0b42011-06-01 18:15:55 +00001713 p->explain = pParse->explain;
1714 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001715}
1716
drh9a324642003-09-06 20:12:01 +00001717/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001718** Close a VDBE cursor and release all the resources that cursor
1719** happens to hold.
drh9a324642003-09-06 20:12:01 +00001720*/
drhdfe88ec2008-11-03 20:55:06 +00001721void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001722 if( pCx==0 ){
1723 return;
1724 }
dana20fde62011-07-12 14:28:05 +00001725 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001726 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001727 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001728 /* The pCx->pCursor will be close automatically, if it exists, by
1729 ** the call above. */
1730 }else if( pCx->pCursor ){
1731 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001732 }
drh9eff6162006-06-12 21:59:13 +00001733#ifndef SQLITE_OMIT_VIRTUALTABLE
1734 if( pCx->pVtabCursor ){
1735 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
drh5cc10232013-11-21 01:04:02 +00001736 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
danielk1977be718892006-06-23 08:05:19 +00001737 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001738 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001739 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001740 }
1741#endif
drh9a324642003-09-06 20:12:01 +00001742}
1743
dan65a7cd12009-09-01 12:16:01 +00001744/*
1745** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1746** is used, for example, when a trigger sub-program is halted to restore
1747** control to the main program.
1748*/
dan165921a2009-08-28 18:53:45 +00001749int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1750 Vdbe *v = pFrame->v;
dan1d8cb212011-12-09 13:24:16 +00001751 v->aOnceFlag = pFrame->aOnceFlag;
1752 v->nOnceFlag = pFrame->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00001753 v->aOp = pFrame->aOp;
1754 v->nOp = pFrame->nOp;
1755 v->aMem = pFrame->aMem;
1756 v->nMem = pFrame->nMem;
1757 v->apCsr = pFrame->apCsr;
1758 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001759 v->db->lastRowid = pFrame->lastRowid;
1760 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001761 return pFrame->pc;
1762}
1763
drh9a324642003-09-06 20:12:01 +00001764/*
drh5f82e3c2009-07-06 00:44:08 +00001765** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001766**
1767** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1768** cell array. This is necessary as the memory cell array may contain
1769** pointers to VdbeFrame objects, which may in turn contain pointers to
1770** open cursors.
drh9a324642003-09-06 20:12:01 +00001771*/
drh5f82e3c2009-07-06 00:44:08 +00001772static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001773 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001774 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001775 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1776 sqlite3VdbeFrameRestore(pFrame);
1777 }
1778 p->pFrame = 0;
1779 p->nFrame = 0;
1780
dan523a0872009-08-31 05:23:32 +00001781 if( p->apCsr ){
1782 int i;
1783 for(i=0; i<p->nCursor; i++){
1784 VdbeCursor *pC = p->apCsr[i];
1785 if( pC ){
1786 sqlite3VdbeFreeCursor(p, pC);
1787 p->apCsr[i] = 0;
1788 }
danielk1977be718892006-06-23 08:05:19 +00001789 }
drh9a324642003-09-06 20:12:01 +00001790 }
dan523a0872009-08-31 05:23:32 +00001791 if( p->aMem ){
1792 releaseMemArray(&p->aMem[1], p->nMem);
1793 }
dan27106572010-12-01 08:04:47 +00001794 while( p->pDelFrame ){
1795 VdbeFrame *pDel = p->pDelFrame;
1796 p->pDelFrame = pDel->pParent;
1797 sqlite3VdbeFrameDelete(pDel);
1798 }
dan0c547792013-07-18 17:12:08 +00001799
1800 /* Delete any auxdata allocations made by the VM */
1801 sqlite3VdbeDeleteAuxData(p, -1, 0);
1802 assert( p->pAuxData==0 );
drh9a324642003-09-06 20:12:01 +00001803}
1804
1805/*
drh9a324642003-09-06 20:12:01 +00001806** Clean up the VM after execution.
1807**
1808** This routine will automatically close any cursors, lists, and/or
1809** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001810** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001811*/
drhc890fec2008-08-01 20:10:08 +00001812static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001813 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001814
1815#ifdef SQLITE_DEBUG
1816 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1817 ** Vdbe.aMem[] arrays have already been cleaned up. */
1818 int i;
drhb8475df2011-12-09 16:21:19 +00001819 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1820 if( p->aMem ){
drha5750cf2014-02-07 13:20:31 +00001821 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
drhb8475df2011-12-09 16:21:19 +00001822 }
dan165921a2009-08-28 18:53:45 +00001823#endif
1824
drh633e6d52008-07-28 19:34:53 +00001825 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001826 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001827 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001828}
1829
1830/*
danielk197722322fd2004-05-25 23:35:17 +00001831** Set the number of result columns that will be returned by this SQL
1832** statement. This is now set at compile time, rather than during
1833** execution of the vdbe program so that sqlite3_column_count() can
1834** be called on an SQL statement before sqlite3_step().
1835*/
1836void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001837 Mem *pColName;
1838 int n;
drh633e6d52008-07-28 19:34:53 +00001839 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001840
drhc890fec2008-08-01 20:10:08 +00001841 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001842 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001843 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001844 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001845 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001846 if( p->aColName==0 ) return;
1847 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001848 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001849 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001850 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001851 }
danielk197722322fd2004-05-25 23:35:17 +00001852}
1853
1854/*
danielk19773cf86062004-05-26 10:11:05 +00001855** Set the name of the idx'th column to be returned by the SQL statement.
1856** zName must be a pointer to a nul terminated string.
1857**
1858** This call must be made after a call to sqlite3VdbeSetNumCols().
1859**
danielk197710fb7492008-10-31 10:53:22 +00001860** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1861** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1862** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001863*/
danielk197710fb7492008-10-31 10:53:22 +00001864int sqlite3VdbeSetColName(
1865 Vdbe *p, /* Vdbe being configured */
1866 int idx, /* Index of column zName applies to */
1867 int var, /* One of the COLNAME_* constants */
1868 const char *zName, /* Pointer to buffer containing name */
1869 void (*xDel)(void*) /* Memory management strategy for zName */
1870){
danielk19773cf86062004-05-26 10:11:05 +00001871 int rc;
1872 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001873 assert( idx<p->nResColumn );
1874 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001875 if( p->db->mallocFailed ){
1876 assert( !zName || xDel!=SQLITE_DYNAMIC );
1877 return SQLITE_NOMEM;
1878 }
drh76ff3a02004-09-24 22:32:30 +00001879 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001880 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001881 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001882 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001883 return rc;
1884}
1885
danielk197713adf8a2004-06-03 16:08:41 +00001886/*
1887** A read or write transaction may or may not be active on database handle
1888** db. If a transaction is active, commit it. If there is a
1889** write-transaction spanning more than one database file, this routine
1890** takes care of the master journal trickery.
1891*/
danielk19773e3a84d2008-08-01 17:37:40 +00001892static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001893 int i;
1894 int nTrans = 0; /* Number of databases with an active write-transaction */
1895 int rc = SQLITE_OK;
1896 int needXcommit = 0;
1897
shane36840fd2009-06-26 16:32:13 +00001898#ifdef SQLITE_OMIT_VIRTUALTABLE
1899 /* With this option, sqlite3VtabSync() is defined to be simply
1900 ** SQLITE_OK so p is not used.
1901 */
1902 UNUSED_PARAMETER(p);
1903#endif
1904
danielk19775bd270b2006-07-25 15:14:52 +00001905 /* Before doing anything else, call the xSync() callback for any
1906 ** virtual module tables written in this transaction. This has to
1907 ** be done before determining whether a master journal file is
1908 ** required, as an xSync() callback may add an attached database
1909 ** to the transaction.
1910 */
dan016f7812013-08-21 17:35:48 +00001911 rc = sqlite3VtabSync(db, p);
danielk19775bd270b2006-07-25 15:14:52 +00001912
1913 /* This loop determines (a) if the commit hook should be invoked and
1914 ** (b) how many database files have open write transactions, not
1915 ** including the temp database. (b) is important because if more than
1916 ** one database file has an open write transaction, a master journal
1917 ** file is required for an atomic commit.
1918 */
drhabfb62f2010-07-30 11:20:35 +00001919 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001920 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001921 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001922 needXcommit = 1;
1923 if( i!=1 ) nTrans++;
dan6b9bb592012-10-05 19:43:02 +00001924 sqlite3BtreeEnter(pBt);
drhabfb62f2010-07-30 11:20:35 +00001925 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
dan6b9bb592012-10-05 19:43:02 +00001926 sqlite3BtreeLeave(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001927 }
1928 }
drhabfb62f2010-07-30 11:20:35 +00001929 if( rc!=SQLITE_OK ){
1930 return rc;
1931 }
danielk197713adf8a2004-06-03 16:08:41 +00001932
1933 /* If there are any write-transactions at all, invoke the commit hook */
1934 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001935 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001936 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00001937 return SQLITE_CONSTRAINT_COMMITHOOK;
danielk197713adf8a2004-06-03 16:08:41 +00001938 }
1939 }
1940
danielk197740b38dc2004-06-26 08:38:24 +00001941 /* The simple case - no more than one database file (not counting the
1942 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001943 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001944 **
danielk197740b38dc2004-06-26 08:38:24 +00001945 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001946 ** string, it means the main database is :memory: or a temp file. In
1947 ** that case we do not support atomic multi-file commits, so use the
1948 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001949 */
drhea678832008-12-10 19:26:22 +00001950 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1951 || nTrans<=1
1952 ){
danielk197704103022009-02-03 16:51:24 +00001953 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001954 Btree *pBt = db->aDb[i].pBt;
1955 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001956 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001957 }
1958 }
1959
drh80e35f42007-03-30 14:06:34 +00001960 /* Do the commit only if all databases successfully complete phase 1.
1961 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1962 ** IO error while deleting or truncating a journal file. It is unlikely,
1963 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001964 */
1965 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1966 Btree *pBt = db->aDb[i].pBt;
1967 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001968 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001969 }
danielk1977979f38e2007-03-27 16:19:51 +00001970 }
1971 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001972 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001973 }
1974 }
1975
1976 /* The complex case - There is a multi-file write-transaction active.
1977 ** This requires a master journal file to ensure the transaction is
1978 ** committed atomicly.
1979 */
danielk197744ee5bf2005-05-27 09:41:12 +00001980#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001981 else{
danielk1977b4b47412007-08-17 15:53:36 +00001982 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001983 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001984 char *zMaster = 0; /* File-name for the master journal */
1985 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001986 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001987 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001988 int res;
drhf5808602011-12-16 00:33:04 +00001989 int retryCount = 0;
drh5c531a42011-12-16 01:21:31 +00001990 int nMainFile;
danielk197713adf8a2004-06-03 16:08:41 +00001991
1992 /* Select a master journal file name */
drh5c531a42011-12-16 01:21:31 +00001993 nMainFile = sqlite3Strlen30(zMainFile);
drh52bcde02012-01-03 14:50:45 +00001994 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
drh5c531a42011-12-16 01:21:31 +00001995 if( zMaster==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00001996 do {
drhdc5ea5c2008-12-10 17:19:59 +00001997 u32 iRandom;
drh84968c02011-12-16 15:11:39 +00001998 if( retryCount ){
1999 if( retryCount>100 ){
2000 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2001 sqlite3OsDelete(pVfs, zMaster, 0);
2002 break;
2003 }else if( retryCount==1 ){
2004 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2005 }
danielk197713adf8a2004-06-03 16:08:41 +00002006 }
drh84968c02011-12-16 15:11:39 +00002007 retryCount++;
danielk197713adf8a2004-06-03 16:08:41 +00002008 sqlite3_randomness(sizeof(iRandom), &iRandom);
drh5c531a42011-12-16 01:21:31 +00002009 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
drhf5808602011-12-16 00:33:04 +00002010 (iRandom>>8)&0xffffff, iRandom&0xff);
drhf5808602011-12-16 00:33:04 +00002011 /* The antipenultimate character of the master journal name must
2012 ** be "9" to avoid name collisions when using 8+3 filenames. */
drh5c531a42011-12-16 01:21:31 +00002013 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
drh81cc5162011-05-17 20:36:21 +00002014 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00002015 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2016 }while( rc==SQLITE_OK && res );
2017 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00002018 /* Open the master journal. */
2019 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2020 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2021 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2022 );
2023 }
danielk197713adf8a2004-06-03 16:08:41 +00002024 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002025 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002026 return rc;
2027 }
2028
2029 /* Write the name of each database file in the transaction into the new
2030 ** master journal file. If an error occurs at this point close
2031 ** and delete the master journal file. All the individual journal files
2032 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00002033 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00002034 */
danielk19771e536952007-08-16 10:09:01 +00002035 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002036 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002037 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00002038 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00002039 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00002040 continue; /* Ignore TEMP and :memory: databases */
2041 }
drh8c96a6e2010-08-31 01:09:15 +00002042 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00002043 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2044 needSync = 1;
2045 }
drhea678832008-12-10 19:26:22 +00002046 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2047 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00002048 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00002049 sqlite3OsCloseFree(pMaster);
2050 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002051 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002052 return rc;
2053 }
2054 }
2055 }
2056
danielk19779663b8f2007-08-24 11:52:28 +00002057 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2058 ** flag is set this is not required.
2059 */
danielk1977bea2a942009-01-20 17:06:27 +00002060 if( needSync
2061 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2062 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2063 ){
danielk1977fee2d252007-08-18 10:59:19 +00002064 sqlite3OsCloseFree(pMaster);
2065 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002066 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00002067 return rc;
2068 }
drhc9e06862004-06-09 20:03:08 +00002069
danielk197713adf8a2004-06-03 16:08:41 +00002070 /* Sync all the db files involved in the transaction. The same call
2071 ** sets the master journal pointer in each individual journal. If
2072 ** an error occurs here, do not delete the master journal file.
2073 **
drh80e35f42007-03-30 14:06:34 +00002074 ** If the error occurs during the first call to
2075 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2076 ** master journal file will be orphaned. But we cannot delete it,
2077 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00002078 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00002079 */
danielk19775bd270b2006-07-25 15:14:52 +00002080 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002081 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002082 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002083 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002084 }
2085 }
danielk1977fee2d252007-08-18 10:59:19 +00002086 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00002087 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00002088 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002089 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00002090 return rc;
2091 }
danielk197713adf8a2004-06-03 16:08:41 +00002092
danielk1977962398d2004-06-14 09:35:16 +00002093 /* Delete the master journal file. This commits the transaction. After
2094 ** doing this the directory is synced again before any individual
2095 ** transaction files are deleted.
2096 */
danielk1977fee2d252007-08-18 10:59:19 +00002097 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00002098 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00002099 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00002100 if( rc ){
2101 return rc;
2102 }
danielk197713adf8a2004-06-03 16:08:41 +00002103
2104 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00002105 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2106 ** deleting or truncating journals. If something goes wrong while
2107 ** this is happening we don't really care. The integrity of the
2108 ** transaction is already guaranteed, but some stray 'cold' journals
2109 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00002110 */
danielk1977979f38e2007-03-27 16:19:51 +00002111 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002112 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00002113 for(i=0; i<db->nDb; i++){
2114 Btree *pBt = db->aDb[i].pBt;
2115 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002116 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00002117 }
2118 }
danielk19772d1d86f2008-06-20 14:59:51 +00002119 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00002120 enable_simulated_io_errors();
2121
danielk1977f9e7dda2006-06-16 16:08:53 +00002122 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002123 }
danielk197744ee5bf2005-05-27 09:41:12 +00002124#endif
danielk1977026d2702004-06-14 13:14:59 +00002125
drh2ac3ee92004-06-07 16:27:46 +00002126 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002127}
2128
danielk19771d850a72004-05-31 08:26:49 +00002129/*
drh4f7d3a52013-06-27 23:54:02 +00002130** This routine checks that the sqlite3.nVdbeActive count variable
danielk19771d850a72004-05-31 08:26:49 +00002131** matches the number of vdbe's in the list sqlite3.pVdbe that are
2132** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00002133** This is an internal self-check only - it is not an essential processing
2134** step.
danielk19771d850a72004-05-31 08:26:49 +00002135**
2136** This is a no-op if NDEBUG is defined.
2137*/
2138#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00002139static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00002140 Vdbe *p;
2141 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00002142 int nWrite = 0;
drh4f7d3a52013-06-27 23:54:02 +00002143 int nRead = 0;
danielk19771d850a72004-05-31 08:26:49 +00002144 p = db->pVdbe;
2145 while( p ){
drh92f02c32004-09-02 14:57:08 +00002146 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002147 cnt++;
drhad4a4b82008-11-05 16:37:34 +00002148 if( p->readOnly==0 ) nWrite++;
drh1713afb2013-06-28 01:24:57 +00002149 if( p->bIsReader ) nRead++;
danielk19771d850a72004-05-31 08:26:49 +00002150 }
2151 p = p->pNext;
2152 }
drh4f7d3a52013-06-27 23:54:02 +00002153 assert( cnt==db->nVdbeActive );
2154 assert( nWrite==db->nVdbeWrite );
2155 assert( nRead==db->nVdbeRead );
danielk19771d850a72004-05-31 08:26:49 +00002156}
2157#else
2158#define checkActiveVdbeCnt(x)
2159#endif
2160
danielk19773cf86062004-05-26 10:11:05 +00002161/*
danielk1977bd434552009-03-18 10:33:00 +00002162** If the Vdbe passed as the first argument opened a statement-transaction,
2163** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2164** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2165** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
drhf7b54962013-05-28 12:11:54 +00002166** statement transaction is committed.
danielk1977bd434552009-03-18 10:33:00 +00002167**
2168** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2169** Otherwise SQLITE_OK.
2170*/
2171int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002172 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002173 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002174
danielk1977e4948172009-07-17 17:25:43 +00002175 /* If p->iStatement is greater than zero, then this Vdbe opened a
2176 ** statement transaction that should be closed here. The only exception
mistachkin48864df2013-03-21 21:20:32 +00002177 ** is that an IO error may have occurred, causing an emergency rollback.
danielk1977e4948172009-07-17 17:25:43 +00002178 ** In this case (db->nStatement==0), and there is nothing to do.
2179 */
2180 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002181 int i;
2182 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002183
2184 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2185 assert( db->nStatement>0 );
2186 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2187
2188 for(i=0; i<db->nDb; i++){
2189 int rc2 = SQLITE_OK;
2190 Btree *pBt = db->aDb[i].pBt;
2191 if( pBt ){
2192 if( eOp==SAVEPOINT_ROLLBACK ){
2193 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2194 }
2195 if( rc2==SQLITE_OK ){
2196 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2197 }
2198 if( rc==SQLITE_OK ){
2199 rc = rc2;
2200 }
2201 }
2202 }
2203 db->nStatement--;
2204 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002205
dana311b802011-04-26 19:21:34 +00002206 if( rc==SQLITE_OK ){
2207 if( eOp==SAVEPOINT_ROLLBACK ){
2208 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2209 }
2210 if( rc==SQLITE_OK ){
2211 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2212 }
2213 }
2214
dan1da40a32009-09-19 17:00:31 +00002215 /* If the statement transaction is being rolled back, also restore the
2216 ** database handles deferred constraint counter to the value it had when
2217 ** the statement transaction was opened. */
2218 if( eOp==SAVEPOINT_ROLLBACK ){
2219 db->nDeferredCons = p->nStmtDefCons;
drh648e2642013-07-11 15:03:32 +00002220 db->nDeferredImmCons = p->nStmtDefImmCons;
dan1da40a32009-09-19 17:00:31 +00002221 }
danielk1977bd434552009-03-18 10:33:00 +00002222 }
2223 return rc;
2224}
2225
2226/*
dan1da40a32009-09-19 17:00:31 +00002227** This function is called when a transaction opened by the database
2228** handle associated with the VM passed as an argument is about to be
2229** committed. If there are outstanding deferred foreign key constraint
2230** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2231**
2232** If there are outstanding FK violations and this function returns
drhd91c1a12013-02-09 13:58:25 +00002233** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2234** and write an error message to it. Then return SQLITE_ERROR.
dan1da40a32009-09-19 17:00:31 +00002235*/
2236#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002237int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002238 sqlite3 *db = p->db;
drh648e2642013-07-11 15:03:32 +00002239 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2240 || (!deferred && p->nFkConstraint>0)
2241 ){
drhd91c1a12013-02-09 13:58:25 +00002242 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan32b09f22009-09-23 17:29:59 +00002243 p->errorAction = OE_Abort;
drhf9c8ce32013-11-05 13:33:55 +00002244 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
dan1da40a32009-09-19 17:00:31 +00002245 return SQLITE_ERROR;
2246 }
2247 return SQLITE_OK;
2248}
2249#endif
2250
2251/*
drh92f02c32004-09-02 14:57:08 +00002252** This routine is called the when a VDBE tries to halt. If the VDBE
2253** has made changes and is in autocommit mode, then commit those
2254** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002255**
drh92f02c32004-09-02 14:57:08 +00002256** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002257** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2258** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002259**
2260** Return an error code. If the commit could not complete because of
2261** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2262** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002263*/
drhff0587c2007-08-29 17:43:19 +00002264int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002265 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002266 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002267
2268 /* This function contains the logic that determines if a statement or
2269 ** transaction will be committed or rolled back as a result of the
2270 ** execution of this virtual machine.
2271 **
drh71b890a2007-10-03 15:30:52 +00002272 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002273 **
drh71b890a2007-10-03 15:30:52 +00002274 ** SQLITE_NOMEM
2275 ** SQLITE_IOERR
2276 ** SQLITE_FULL
2277 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002278 **
drh71b890a2007-10-03 15:30:52 +00002279 ** Then the internal cache might have been left in an inconsistent
2280 ** state. We need to rollback the statement transaction, if there is
2281 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002282 */
drh9a324642003-09-06 20:12:01 +00002283
drh17435752007-08-16 04:30:38 +00002284 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002285 p->rc = SQLITE_NOMEM;
2286 }
drh6e856bc2011-12-09 18:06:44 +00002287 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
drh5f82e3c2009-07-06 00:44:08 +00002288 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002289 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002290 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002291 }
danielk19771d850a72004-05-31 08:26:49 +00002292 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002293
danc0537fe2013-06-28 19:41:43 +00002294 /* No commit or rollback needed if the program never started or if the
2295 ** SQL statement does not read or write a database file. */
2296 if( p->pc>=0 && p->bIsReader ){
drhaac2f552006-09-23 21:44:23 +00002297 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002298 int eStatementOp = 0;
2299 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002300
2301 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002302 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002303
drh71b890a2007-10-03 15:30:52 +00002304 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002305 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002306 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002307 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002308 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002309 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002310 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2311 ** no rollback is necessary. Otherwise, at least a savepoint
2312 ** transaction must be rolled back to restore the database to a
2313 ** consistent state.
2314 **
2315 ** Even if the statement is read-only, it is important to perform
2316 ** a statement or transaction rollback operation. If the error
mistachkin48864df2013-03-21 21:20:32 +00002317 ** occurred while writing to the journal, sub-journal or database
dan5653e4d2010-08-12 11:25:47 +00002318 ** file as part of an effort to free up cache space (see function
2319 ** pagerStress() in pager.c), the rollback is required to restore
2320 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002321 */
drhad4a4b82008-11-05 16:37:34 +00002322 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002323 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002324 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002325 }else{
2326 /* We are forced to roll back the active transaction. Before doing
2327 ** so, abort any other statements this handle currently has active.
2328 */
drh21021a52012-02-13 17:01:51 +00002329 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002330 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002331 db->autoCommit = 1;
2332 }
danielk1977261919c2005-12-06 12:52:59 +00002333 }
2334 }
dan32b09f22009-09-23 17:29:59 +00002335
2336 /* Check for immediate foreign key violations. */
2337 if( p->rc==SQLITE_OK ){
2338 sqlite3VdbeCheckFk(p, 0);
2339 }
danielk197707cb5602006-01-20 10:55:05 +00002340
danielk1977bd434552009-03-18 10:33:00 +00002341 /* If the auto-commit flag is set and this is the only active writer
2342 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002343 **
2344 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002345 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002346 */
danielk1977093e0f62008-11-13 18:00:14 +00002347 if( !sqlite3VtabInSync(db)
2348 && db->autoCommit
drh4f7d3a52013-06-27 23:54:02 +00002349 && db->nVdbeWrite==(p->readOnly==0)
danielk1977093e0f62008-11-13 18:00:14 +00002350 ){
danielk197707cb5602006-01-20 10:55:05 +00002351 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002352 rc = sqlite3VdbeCheckFk(p, 1);
2353 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002354 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002355 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002356 return SQLITE_ERROR;
2357 }
drhd91c1a12013-02-09 13:58:25 +00002358 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan19611b12011-01-24 16:00:58 +00002359 }else{
2360 /* The auto-commit flag is true, the vdbe program was successful
2361 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2362 ** key constraints to hold up the transaction. This means a commit
2363 ** is required. */
2364 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002365 }
dan19611b12011-01-24 16:00:58 +00002366 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002367 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002368 return SQLITE_BUSY;
2369 }else if( rc!=SQLITE_OK ){
2370 p->rc = rc;
drh0f198a72012-02-13 16:43:16 +00002371 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002372 }else{
dan1da40a32009-09-19 17:00:31 +00002373 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00002374 db->nDeferredImmCons = 0;
2375 db->flags &= ~SQLITE_DeferFKs;
danielk197707cb5602006-01-20 10:55:05 +00002376 sqlite3CommitInternalChanges(db);
2377 }
2378 }else{
drh0f198a72012-02-13 16:43:16 +00002379 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002380 }
danielk1977bd434552009-03-18 10:33:00 +00002381 db->nStatement = 0;
2382 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002383 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002384 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002385 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002386 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002387 }else{
drh21021a52012-02-13 17:01:51 +00002388 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002389 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002390 db->autoCommit = 1;
2391 }
danielk19771d850a72004-05-31 08:26:49 +00002392 }
danielk197707cb5602006-01-20 10:55:05 +00002393
danielk1977bd434552009-03-18 10:33:00 +00002394 /* If eStatementOp is non-zero, then a statement transaction needs to
2395 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2396 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002397 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2398 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002399 */
danielk1977bd434552009-03-18 10:33:00 +00002400 if( eStatementOp ){
2401 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002402 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002403 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002404 p->rc = rc;
2405 sqlite3DbFree(db, p->zErrMsg);
2406 p->zErrMsg = 0;
2407 }
drh21021a52012-02-13 17:01:51 +00002408 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
dan40ad9d22010-06-03 09:17:38 +00002409 sqlite3CloseSavepoints(db);
2410 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002411 }
danielk197777d83ba2004-05-31 10:08:14 +00002412 }
danielk197707cb5602006-01-20 10:55:05 +00002413
danielk1977bd434552009-03-18 10:33:00 +00002414 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2415 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002416 */
drh6be240e2009-07-14 02:33:02 +00002417 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002418 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002419 sqlite3VdbeSetChanges(db, p->nChange);
2420 }else{
2421 sqlite3VdbeSetChanges(db, 0);
2422 }
2423 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002424 }
drhff0587c2007-08-29 17:43:19 +00002425
2426 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002427 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002428 }
danielk19771d850a72004-05-31 08:26:49 +00002429
danielk197765fd59f2006-06-24 11:51:33 +00002430 /* We have successfully halted and closed the VM. Record this fact. */
2431 if( p->pc>=0 ){
drh4f7d3a52013-06-27 23:54:02 +00002432 db->nVdbeActive--;
2433 if( !p->readOnly ) db->nVdbeWrite--;
drh1713afb2013-06-28 01:24:57 +00002434 if( p->bIsReader ) db->nVdbeRead--;
drh4f7d3a52013-06-27 23:54:02 +00002435 assert( db->nVdbeActive>=db->nVdbeRead );
2436 assert( db->nVdbeRead>=db->nVdbeWrite );
2437 assert( db->nVdbeWrite>=0 );
drh9a324642003-09-06 20:12:01 +00002438 }
drh92f02c32004-09-02 14:57:08 +00002439 p->magic = VDBE_MAGIC_HALT;
2440 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002441 if( p->db->mallocFailed ){
2442 p->rc = SQLITE_NOMEM;
2443 }
danielk19771d850a72004-05-31 08:26:49 +00002444
danielk1977404ca072009-03-16 13:19:36 +00002445 /* If the auto-commit flag is set to true, then any locks that were held
2446 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2447 ** to invoke any required unlock-notify callbacks.
2448 */
2449 if( db->autoCommit ){
2450 sqlite3ConnectionUnlocked(db);
2451 }
2452
drh4f7d3a52013-06-27 23:54:02 +00002453 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002454 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002455}
drh4cf7c7f2007-08-28 23:28:07 +00002456
drh92f02c32004-09-02 14:57:08 +00002457
2458/*
drh3c23a882007-01-09 14:01:13 +00002459** Each VDBE holds the result of the most recent sqlite3_step() call
2460** in p->rc. This routine sets that result back to SQLITE_OK.
2461*/
2462void sqlite3VdbeResetStepResult(Vdbe *p){
2463 p->rc = SQLITE_OK;
2464}
2465
2466/*
dan029ead62011-10-27 15:19:58 +00002467** Copy the error code and error message belonging to the VDBE passed
2468** as the first argument to its database handle (so that they will be
2469** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2470**
2471** This function does not clear the VDBE error code or message, just
2472** copies them to the database handle.
2473*/
2474int sqlite3VdbeTransferError(Vdbe *p){
2475 sqlite3 *db = p->db;
2476 int rc = p->rc;
2477 if( p->zErrMsg ){
drh81bdd6d2011-10-29 01:33:24 +00002478 u8 mallocFailed = db->mallocFailed;
dan029ead62011-10-27 15:19:58 +00002479 sqlite3BeginBenignMalloc();
drha3cc0072013-12-13 16:23:55 +00002480 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
dan029ead62011-10-27 15:19:58 +00002481 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2482 sqlite3EndBenignMalloc();
drh81bdd6d2011-10-29 01:33:24 +00002483 db->mallocFailed = mallocFailed;
dan029ead62011-10-27 15:19:58 +00002484 db->errCode = rc;
2485 }else{
2486 sqlite3Error(db, rc, 0);
2487 }
2488 return rc;
2489}
2490
danac455932012-11-26 19:50:41 +00002491#ifdef SQLITE_ENABLE_SQLLOG
2492/*
2493** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2494** invoke it.
2495*/
2496static void vdbeInvokeSqllog(Vdbe *v){
2497 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2498 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2499 assert( v->db->init.busy==0 );
2500 if( zExpanded ){
2501 sqlite3GlobalConfig.xSqllog(
2502 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2503 );
2504 sqlite3DbFree(v->db, zExpanded);
2505 }
2506 }
2507}
2508#else
2509# define vdbeInvokeSqllog(x)
2510#endif
2511
dan029ead62011-10-27 15:19:58 +00002512/*
drh92f02c32004-09-02 14:57:08 +00002513** Clean up a VDBE after execution but do not delete the VDBE just yet.
2514** Write any error messages into *pzErrMsg. Return the result code.
2515**
2516** After this routine is run, the VDBE should be ready to be executed
2517** again.
2518**
2519** To look at it another way, this routine resets the state of the
2520** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2521** VDBE_MAGIC_INIT.
2522*/
drhc890fec2008-08-01 20:10:08 +00002523int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002524 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002525 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002526
2527 /* If the VM did not run to completion or if it encountered an
2528 ** error, then it might not have been halted properly. So halt
2529 ** it now.
2530 */
2531 sqlite3VdbeHalt(p);
2532
drhfb7e7652005-01-24 00:28:42 +00002533 /* If the VDBE has be run even partially, then transfer the error code
2534 ** and error message from the VDBE into the main database structure. But
2535 ** if the VDBE has just been set to run but has not actually executed any
2536 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002537 */
drhfb7e7652005-01-24 00:28:42 +00002538 if( p->pc>=0 ){
danac455932012-11-26 19:50:41 +00002539 vdbeInvokeSqllog(p);
dan029ead62011-10-27 15:19:58 +00002540 sqlite3VdbeTransferError(p);
2541 sqlite3DbFree(db, p->zErrMsg);
2542 p->zErrMsg = 0;
drh4611d922010-02-25 14:47:01 +00002543 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002544 }else if( p->rc && p->expired ){
2545 /* The expired flag was set on the VDBE before the first call
2546 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2547 ** called), set the database error in this case as well.
2548 */
drha3cc0072013-12-13 16:23:55 +00002549 sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002550 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002551 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002552 }
2553
2554 /* Reclaim all memory used by the VDBE
2555 */
drhc890fec2008-08-01 20:10:08 +00002556 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002557
2558 /* Save profiling information from this VDBE run.
2559 */
drh9a324642003-09-06 20:12:01 +00002560#ifdef VDBE_PROFILE
2561 {
2562 FILE *out = fopen("vdbe_profile.out", "a");
2563 if( out ){
2564 int i;
2565 fprintf(out, "---- ");
2566 for(i=0; i<p->nOp; i++){
2567 fprintf(out, "%02x", p->aOp[i].opcode);
2568 }
2569 fprintf(out, "\n");
drh2926f962014-02-17 01:13:28 +00002570 if( p->zSql ){
2571 char c, pc = 0;
2572 fprintf(out, "-- ");
2573 for(i=0; (c = p->zSql[i])!=0; i++){
2574 if( pc=='\n' ) fprintf(out, "-- ");
2575 putc(c, out);
2576 pc = c;
2577 }
2578 if( pc!='\n' ) fprintf(out, "\n");
2579 }
drh9a324642003-09-06 20:12:01 +00002580 for(i=0; i<p->nOp; i++){
drh15ab9412014-02-24 14:24:01 +00002581 char zHdr[100];
2582 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
drh9a324642003-09-06 20:12:01 +00002583 p->aOp[i].cnt,
2584 p->aOp[i].cycles,
2585 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2586 );
drh15ab9412014-02-24 14:24:01 +00002587 fprintf(out, "%s", zHdr);
danielk19774adee202004-05-08 08:23:19 +00002588 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002589 }
2590 fclose(out);
2591 }
2592 }
2593#endif
drh7fa20922013-09-17 23:36:33 +00002594 p->iCurrentTime = 0;
drh9a324642003-09-06 20:12:01 +00002595 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002596 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002597}
drh92f02c32004-09-02 14:57:08 +00002598
drh9a324642003-09-06 20:12:01 +00002599/*
2600** Clean up and delete a VDBE after execution. Return an integer which is
2601** the result code. Write any error message text into *pzErrMsg.
2602*/
danielk19779e6db7d2004-06-21 08:18:51 +00002603int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002604 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002605 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002606 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002607 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002608 }
danielk19774adee202004-05-08 08:23:19 +00002609 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002610 return rc;
2611}
2612
2613/*
dan0c547792013-07-18 17:12:08 +00002614** If parameter iOp is less than zero, then invoke the destructor for
2615** all auxiliary data pointers currently cached by the VM passed as
2616** the first argument.
2617**
2618** Or, if iOp is greater than or equal to zero, then the destructor is
2619** only invoked for those auxiliary data pointers created by the user
2620** function invoked by the OP_Function opcode at instruction iOp of
2621** VM pVdbe, and only then if:
2622**
2623** * the associated function parameter is the 32nd or later (counting
2624** from left to right), or
2625**
2626** * the corresponding bit in argument mask is clear (where the first
2627** function parameter corrsponds to bit 0 etc.).
drhf92c7ff2004-06-19 15:40:23 +00002628*/
dan0c547792013-07-18 17:12:08 +00002629void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2630 AuxData **pp = &pVdbe->pAuxData;
2631 while( *pp ){
2632 AuxData *pAux = *pp;
2633 if( (iOp<0)
drh693e6712014-01-24 22:58:00 +00002634 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
dan0c547792013-07-18 17:12:08 +00002635 ){
drh693e6712014-01-24 22:58:00 +00002636 testcase( pAux->iArg==31 );
drhf92c7ff2004-06-19 15:40:23 +00002637 if( pAux->xDelete ){
2638 pAux->xDelete(pAux->pAux);
2639 }
dan0c547792013-07-18 17:12:08 +00002640 *pp = pAux->pNext;
2641 sqlite3DbFree(pVdbe->db, pAux);
2642 }else{
2643 pp= &pAux->pNext;
drhf92c7ff2004-06-19 15:40:23 +00002644 }
2645 }
2646}
2647
2648/*
drhcb103b92012-10-26 00:11:23 +00002649** Free all memory associated with the Vdbe passed as the second argument,
2650** except for object itself, which is preserved.
2651**
dand46def72010-07-24 11:28:28 +00002652** The difference between this function and sqlite3VdbeDelete() is that
2653** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
drhcb103b92012-10-26 00:11:23 +00002654** the database connection and frees the object itself.
dand46def72010-07-24 11:28:28 +00002655*/
drhcb103b92012-10-26 00:11:23 +00002656void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002657 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002658 int i;
dand46def72010-07-24 11:28:28 +00002659 assert( p->db==0 || p->db==db );
2660 releaseMemArray(p->aVar, p->nVar);
2661 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002662 for(pSub=p->pProgram; pSub; pSub=pNext){
2663 pNext = pSub->pNext;
2664 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2665 sqlite3DbFree(db, pSub);
2666 }
drh124c0b42011-06-01 18:15:55 +00002667 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002668 vdbeFreeOpArray(db, p->aOp, p->nOp);
dand46def72010-07-24 11:28:28 +00002669 sqlite3DbFree(db, p->aColName);
2670 sqlite3DbFree(db, p->zSql);
2671 sqlite3DbFree(db, p->pFree);
drh678a9aa2011-12-10 15:55:01 +00002672#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
drh25fe97a2013-01-23 18:44:22 +00002673 sqlite3DbFree(db, p->zExplain);
drh678a9aa2011-12-10 15:55:01 +00002674 sqlite3DbFree(db, p->pExplain);
drh7e02e5e2011-12-06 19:44:51 +00002675#endif
dand46def72010-07-24 11:28:28 +00002676}
2677
2678/*
drh9a324642003-09-06 20:12:01 +00002679** Delete an entire VDBE.
2680*/
danielk19774adee202004-05-08 08:23:19 +00002681void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002682 sqlite3 *db;
2683
drhfa3be902009-07-07 02:44:07 +00002684 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002685 db = p->db;
drh4245c402012-06-02 14:32:21 +00002686 assert( sqlite3_mutex_held(db->mutex) );
drhcb103b92012-10-26 00:11:23 +00002687 sqlite3VdbeClearObject(db, p);
drh9a324642003-09-06 20:12:01 +00002688 if( p->pPrev ){
2689 p->pPrev->pNext = p->pNext;
2690 }else{
drh633e6d52008-07-28 19:34:53 +00002691 assert( db->pVdbe==p );
2692 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002693 }
2694 if( p->pNext ){
2695 p->pNext->pPrev = p->pPrev;
2696 }
drh9a324642003-09-06 20:12:01 +00002697 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002698 p->db = 0;
drhcb103b92012-10-26 00:11:23 +00002699 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002700}
drha11846b2004-01-07 18:52:56 +00002701
2702/*
drh9a65f2c2009-06-22 19:05:40 +00002703** Make sure the cursor p is ready to read or write the row to which it
2704** was last positioned. Return an error code if an OOM fault or I/O error
2705** prevents us from positioning the cursor to its correct position.
2706**
drha11846b2004-01-07 18:52:56 +00002707** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002708** MoveTo now. If no move is pending, check to see if the row has been
2709** deleted out from under the cursor and if it has, mark the row as
2710** a NULL row.
2711**
2712** If the cursor is already pointing to the correct row and that row has
2713** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002714*/
drhdfe88ec2008-11-03 20:55:06 +00002715int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002716 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002717 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002718#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002719 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002720#endif
drhf0863fe2005-06-12 21:35:51 +00002721 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002722 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002723 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002724 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002725 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2726 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002727#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002728 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002729#endif
drha11846b2004-01-07 18:52:56 +00002730 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002731 p->cacheStatus = CACHE_STALE;
drh399af1d2013-11-20 17:25:55 +00002732 }else if( p->pCursor ){
drha3460582008-07-11 21:02:53 +00002733 int hasMoved;
2734 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2735 if( rc ) return rc;
2736 if( hasMoved ){
2737 p->cacheStatus = CACHE_STALE;
drh86dd3712014-03-25 11:00:21 +00002738 if( hasMoved==2 ) p->nullRow = 1;
drha3460582008-07-11 21:02:53 +00002739 }
drha11846b2004-01-07 18:52:56 +00002740 }
2741 return SQLITE_OK;
2742}
danielk19774adee202004-05-08 08:23:19 +00002743
drhab9f7f12004-05-08 10:56:11 +00002744/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002745** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002746**
danielk1977cfcdaef2004-05-12 07:33:33 +00002747** sqlite3VdbeSerialType()
2748** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002749** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002750** sqlite3VdbeSerialPut()
2751** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002752**
2753** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002754** data and index records. Each serialized value consists of a
2755** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2756** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002757**
danielk1977cfcdaef2004-05-12 07:33:33 +00002758** In an SQLite index record, the serial type is stored directly before
2759** the blob of data that it corresponds to. In a table record, all serial
2760** types are stored at the start of the record, and the blobs of data at
2761** the end. Hence these functions allow the caller to handle the
mistachkin48864df2013-03-21 21:20:32 +00002762** serial-type and data blob separately.
danielk1977cfcdaef2004-05-12 07:33:33 +00002763**
2764** The following table describes the various storage classes for data:
2765**
2766** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002767** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002768** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002769** 1 1 signed integer
2770** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002771** 3 3 signed integer
2772** 4 4 signed integer
2773** 5 6 signed integer
2774** 6 8 signed integer
2775** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002776** 8 0 Integer constant 0
2777** 9 0 Integer constant 1
2778** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002779** N>=12 and even (N-12)/2 BLOB
2780** N>=13 and odd (N-13)/2 text
2781**
drh35a59652006-01-02 18:24:40 +00002782** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2783** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002784*/
2785
2786/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002787** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002788*/
drhd946db02005-12-29 19:23:06 +00002789u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002790 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002791 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002792
2793 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002794 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002795 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002796 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002797 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002798# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002799 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002800 u64 u;
drhcfd654b2011-03-05 13:54:15 +00002801 if( i<0 ){
2802 if( i<(-MAX_6BYTE) ) return 6;
2803 /* Previous test prevents: u = -(-9223372036854775808) */
2804 u = -i;
2805 }else{
2806 u = i;
2807 }
drh56690b32012-09-17 15:36:31 +00002808 if( u<=127 ){
2809 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2810 }
drh5742b632005-01-26 17:47:02 +00002811 if( u<=32767 ) return 2;
2812 if( u<=8388607 ) return 3;
2813 if( u<=2147483647 ) return 4;
2814 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002815 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002816 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002817 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002818 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002819 }
danielk1977e4359752008-11-03 09:39:45 +00002820 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002821 n = pMem->n;
2822 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002823 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002824 }
drhfdf972a2007-05-02 13:30:27 +00002825 assert( n>=0 );
2826 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002827}
2828
2829/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002830** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002831*/
drh35cd6432009-06-05 14:17:21 +00002832u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002833 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002834 return (serial_type-12)/2;
2835 }else{
drh57196282004-10-06 15:41:16 +00002836 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002837 return aSize[serial_type];
2838 }
danielk1977192ac1d2004-05-10 07:17:30 +00002839}
2840
2841/*
drh110daac2007-05-04 11:59:31 +00002842** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002843** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002844** upper 4 bytes. Return the result.
2845**
drh7a4f5022007-05-23 07:20:08 +00002846** For most architectures, this is a no-op.
2847**
2848** (later): It is reported to me that the mixed-endian problem
2849** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2850** that early versions of GCC stored the two words of a 64-bit
2851** float in the wrong order. And that error has been propagated
2852** ever since. The blame is not necessarily with GCC, though.
2853** GCC might have just copying the problem from a prior compiler.
2854** I am also told that newer versions of GCC that follow a different
2855** ABI get the byte order right.
2856**
2857** Developers using SQLite on an ARM7 should compile and run their
2858** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2859** enabled, some asserts below will ensure that the byte order of
2860** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002861**
2862** (2007-08-30) Frank van Vugt has studied this problem closely
2863** and has send his findings to the SQLite developers. Frank
2864** writes that some Linux kernels offer floating point hardware
2865** emulation that uses only 32-bit mantissas instead of a full
2866** 48-bits as required by the IEEE standard. (This is the
2867** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2868** byte swapping becomes very complicated. To avoid problems,
2869** the necessary byte swapping is carried out using a 64-bit integer
2870** rather than a 64-bit float. Frank assures us that the code here
2871** works for him. We, the developers, have no way to independently
2872** verify this, but Frank seems to know what he is talking about
2873** so we trust him.
drh110daac2007-05-04 11:59:31 +00002874*/
2875#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002876static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002877 union {
drh60d09a72007-08-30 15:05:08 +00002878 u64 r;
drh110daac2007-05-04 11:59:31 +00002879 u32 i[2];
2880 } u;
2881 u32 t;
2882
2883 u.r = in;
2884 t = u.i[0];
2885 u.i[0] = u.i[1];
2886 u.i[1] = t;
2887 return u.r;
2888}
2889# define swapMixedEndianFloat(X) X = floatSwap(X)
2890#else
2891# define swapMixedEndianFloat(X)
2892#endif
2893
2894/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002895** Write the serialized data blob for the value stored in pMem into
2896** buf. It is assumed that the caller has allocated sufficient space.
2897** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002898**
drh038b7bc2013-12-09 23:17:22 +00002899** nBuf is the amount of space left in buf[]. The caller is responsible
2900** for allocating enough space to buf[] to hold the entire field, exclusive
2901** of the pMem->u.nZero bytes for a MEM_Zero value.
drhfdf972a2007-05-02 13:30:27 +00002902**
2903** Return the number of bytes actually written into buf[]. The number
2904** of bytes in the zero-filled tail is included in the return value only
2905** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002906*/
drha9ab4812013-12-11 11:00:44 +00002907u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
drh35cd6432009-06-05 14:17:21 +00002908 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002909
drh1483e142004-05-21 21:12:42 +00002910 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002911 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002912 u64 v;
drh35cd6432009-06-05 14:17:21 +00002913 u32 i;
drha19b7752004-05-30 21:14:58 +00002914 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002915 assert( sizeof(v)==sizeof(pMem->r) );
2916 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002917 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002918 }else{
drh3c024d62007-03-30 11:23:45 +00002919 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002920 }
drh1483e142004-05-21 21:12:42 +00002921 len = i = sqlite3VdbeSerialTypeLen(serial_type);
2922 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002923 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002924 v >>= 8;
2925 }
2926 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002927 }
drhd946db02005-12-29 19:23:06 +00002928
danielk1977cfcdaef2004-05-12 07:33:33 +00002929 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002930 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002931 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002932 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002933 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002934 memcpy(buf, pMem->z, len);
2935 return len;
2936 }
2937
2938 /* NULL or constants 0 or 1 */
2939 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002940}
2941
drhf926d1e2014-03-04 04:04:33 +00002942/* Input "x" is a sequence of unsigned characters that represent a
2943** big-endian integer. Return the equivalent native integer
2944*/
2945#define ONE_BYTE_INT(x) ((i8)(x)[0])
2946#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
2947#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
2948#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
2949
danielk1977cfcdaef2004-05-12 07:33:33 +00002950/*
2951** Deserialize the data blob pointed to by buf as serial type serial_type
2952** and store the result in pMem. Return the number of bytes read.
2953*/
drh35cd6432009-06-05 14:17:21 +00002954u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002955 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002956 u32 serial_type, /* Serial type to deserialize */
2957 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002958){
drh693e6712014-01-24 22:58:00 +00002959 u64 x;
2960 u32 y;
drh3c685822005-05-21 18:32:18 +00002961 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002962 case 10: /* Reserved for future use */
2963 case 11: /* Reserved for future use */
2964 case 0: { /* NULL */
2965 pMem->flags = MEM_Null;
2966 break;
2967 }
2968 case 1: { /* 1-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002969 pMem->u.i = ONE_BYTE_INT(buf);
drh1483e142004-05-21 21:12:42 +00002970 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002971 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002972 return 1;
drh1483e142004-05-21 21:12:42 +00002973 }
drh3c685822005-05-21 18:32:18 +00002974 case 2: { /* 2-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002975 pMem->u.i = TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002976 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002977 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002978 return 2;
2979 }
2980 case 3: { /* 3-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002981 pMem->u.i = THREE_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002982 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002983 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002984 return 3;
2985 }
2986 case 4: { /* 4-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002987 y = FOUR_BYTE_UINT(buf);
drh693e6712014-01-24 22:58:00 +00002988 pMem->u.i = (i64)*(int*)&y;
drh3c685822005-05-21 18:32:18 +00002989 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002990 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002991 return 4;
2992 }
2993 case 5: { /* 6-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002994 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002995 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002996 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002997 return 6;
2998 }
drh91124b32005-08-18 18:15:05 +00002999 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00003000 case 7: { /* IEEE floating point */
drh2a3e4a72006-01-23 21:44:53 +00003001#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00003002 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00003003 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3004 ** defined that 64-bit floating point values really are mixed
3005 ** endian.
drhbfd6b032005-08-28 01:38:44 +00003006 */
drhde941c62005-08-28 01:34:21 +00003007 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00003008 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00003009 u64 t2 = t1;
3010 swapMixedEndianFloat(t2);
3011 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00003012#endif
drhf926d1e2014-03-04 04:04:33 +00003013 x = FOUR_BYTE_UINT(buf);
3014 y = FOUR_BYTE_UINT(buf+4);
drh3c685822005-05-21 18:32:18 +00003015 x = (x<<32) | y;
3016 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00003017 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00003018 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003019 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003020 }else{
drh4f0c5872007-03-26 22:05:01 +00003021 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00003022 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00003023 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00003024 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00003025 }
3026 return 8;
3027 }
drhd946db02005-12-29 19:23:06 +00003028 case 8: /* Integer 0 */
3029 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00003030 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00003031 pMem->flags = MEM_Int;
3032 return 0;
3033 }
drh3c685822005-05-21 18:32:18 +00003034 default: {
drhc138daf2013-11-19 13:55:34 +00003035 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
drh35cd6432009-06-05 14:17:21 +00003036 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00003037 pMem->z = (char *)buf;
3038 pMem->n = len;
3039 pMem->xDel = 0;
drhc138daf2013-11-19 13:55:34 +00003040 pMem->flags = aFlag[serial_type&1];
drh3c685822005-05-21 18:32:18 +00003041 return len;
drh696b32f2004-05-30 01:51:52 +00003042 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003043 }
drh3c685822005-05-21 18:32:18 +00003044 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00003045}
3046
drh1e968a02008-03-25 00:22:21 +00003047/*
dan03e9cfc2011-09-05 14:20:27 +00003048** This routine is used to allocate sufficient space for an UnpackedRecord
3049** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3050** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00003051**
dan03e9cfc2011-09-05 14:20:27 +00003052** The space is either allocated using sqlite3DbMallocRaw() or from within
3053** the unaligned buffer passed via the second and third arguments (presumably
3054** stack space). If the former, then *ppFree is set to a pointer that should
3055** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3056** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3057** before returning.
drh1e968a02008-03-25 00:22:21 +00003058**
dan03e9cfc2011-09-05 14:20:27 +00003059** If an OOM error occurs, NULL is returned.
3060*/
3061UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3062 KeyInfo *pKeyInfo, /* Description of the record */
3063 char *pSpace, /* Unaligned space available */
3064 int szSpace, /* Size of pSpace[] in bytes */
3065 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00003066){
dan03e9cfc2011-09-05 14:20:27 +00003067 UnpackedRecord *p; /* Unpacked record to return */
3068 int nOff; /* Increment pSpace by nOff to align it */
3069 int nByte; /* Number of bytes required for *p */
3070
3071 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00003072 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3073 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3074 */
3075 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00003076 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00003077 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00003078 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3079 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00003080 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00003081 }else{
dan42acb3e2011-09-05 20:16:38 +00003082 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00003083 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00003084 }
dan42acb3e2011-09-05 20:16:38 +00003085
3086 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drhe1a022e2012-09-17 17:16:53 +00003087 assert( pKeyInfo->aSortOrder!=0 );
drh1e968a02008-03-25 00:22:21 +00003088 p->pKeyInfo = pKeyInfo;
3089 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00003090 return p;
3091}
3092
3093/*
3094** Given the nKey-byte encoding of a record in pKey[], populate the
3095** UnpackedRecord structure indicated by the fourth argument with the
3096** contents of the decoded record.
3097*/
3098void sqlite3VdbeRecordUnpack(
3099 KeyInfo *pKeyInfo, /* Information about the record format */
3100 int nKey, /* Size of the binary record */
3101 const void *pKey, /* The binary record */
3102 UnpackedRecord *p /* Populate this structure before returning. */
3103){
3104 const unsigned char *aKey = (const unsigned char *)pKey;
3105 int d;
3106 u32 idx; /* Offset in aKey[] to read from */
3107 u16 u; /* Unsigned loop counter */
3108 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00003109 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00003110
dan1fed5da2014-02-25 21:01:25 +00003111 p->default_rc = 0;
drh8c5d1522009-04-10 00:56:28 +00003112 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00003113 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00003114 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00003115 u = 0;
drh2fa34d32009-07-15 16:30:50 +00003116 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00003117 u32 serial_type;
3118
danielk197700e13612008-11-17 19:18:54 +00003119 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00003120 pMem->enc = pKeyInfo->enc;
3121 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00003122 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
danielk19775f096132008-03-28 15:44:09 +00003123 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00003124 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00003125 pMem++;
shane0b8d2762008-07-22 05:18:00 +00003126 u++;
drh1e968a02008-03-25 00:22:21 +00003127 }
drh7d10d5a2008-08-20 16:35:10 +00003128 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00003129 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00003130}
3131
dan3833e932014-03-01 19:44:56 +00003132#if SQLITE_DEBUG
dan3b9330f2014-02-27 20:44:18 +00003133/*
dan3833e932014-03-01 19:44:56 +00003134** This function compares two index or table record keys in the same way
3135** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3136** this function deserializes and compares values using the
3137** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3138** in assert() statements to ensure that the optimized code in
3139** sqlite3VdbeRecordCompare() returns results with these two primitives.
dan3b9330f2014-02-27 20:44:18 +00003140*/
dan3833e932014-03-01 19:44:56 +00003141static int vdbeRecordCompareDebug(
dan1fed5da2014-02-25 21:01:25 +00003142 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003143 const UnpackedRecord *pPKey2 /* Right key */
dan1fed5da2014-02-25 21:01:25 +00003144){
dan3b9330f2014-02-27 20:44:18 +00003145 u32 d1; /* Offset into aKey[] of next data element */
3146 u32 idx1; /* Offset into aKey[] of next header element */
3147 u32 szHdr1; /* Number of bytes in header */
3148 int i = 0;
3149 int rc = 0;
3150 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3151 KeyInfo *pKeyInfo;
3152 Mem mem1;
dan1fed5da2014-02-25 21:01:25 +00003153
dan3b9330f2014-02-27 20:44:18 +00003154 pKeyInfo = pPKey2->pKeyInfo;
3155 mem1.enc = pKeyInfo->enc;
3156 mem1.db = pKeyInfo->db;
3157 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
3158 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003159
dan3b9330f2014-02-27 20:44:18 +00003160 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3161 ** We could initialize it, as shown here, to silence those complaints.
3162 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3163 ** the unnecessary initialization has a measurable negative performance
3164 ** impact, since this routine is a very high runner. And so, we choose
3165 ** to ignore the compiler warnings and leave this variable uninitialized.
3166 */
3167 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
3168
3169 idx1 = getVarint32(aKey1, szHdr1);
3170 d1 = szHdr1;
3171 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3172 assert( pKeyInfo->aSortOrder!=0 );
3173 assert( pKeyInfo->nField>0 );
3174 assert( idx1<=szHdr1 || CORRUPT_DB );
3175 do{
3176 u32 serial_type1;
dan1fed5da2014-02-25 21:01:25 +00003177
dan3b9330f2014-02-27 20:44:18 +00003178 /* Read the serial types for the next element in each key. */
3179 idx1 += getVarint32( aKey1+idx1, serial_type1 );
dan1fed5da2014-02-25 21:01:25 +00003180
dan3b9330f2014-02-27 20:44:18 +00003181 /* Verify that there is enough key space remaining to avoid
3182 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3183 ** always be greater than or equal to the amount of required key space.
3184 ** Use that approximation to avoid the more expensive call to
3185 ** sqlite3VdbeSerialTypeLen() in the common case.
3186 */
3187 if( d1+serial_type1+2>(u32)nKey1
3188 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3189 ){
3190 break;
dan1fed5da2014-02-25 21:01:25 +00003191 }
dan1fed5da2014-02-25 21:01:25 +00003192
dan3b9330f2014-02-27 20:44:18 +00003193 /* Extract the values to be compared.
3194 */
3195 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
dan1fed5da2014-02-25 21:01:25 +00003196
dan3b9330f2014-02-27 20:44:18 +00003197 /* Do the comparison
3198 */
3199 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3200 if( rc!=0 ){
3201 assert( mem1.zMalloc==0 ); /* See comment below */
3202 if( pKeyInfo->aSortOrder[i] ){
3203 rc = -rc; /* Invert the result for DESC sort order. */
dan1fed5da2014-02-25 21:01:25 +00003204 }
dan3b9330f2014-02-27 20:44:18 +00003205 return rc;
dan1fed5da2014-02-25 21:01:25 +00003206 }
dan3b9330f2014-02-27 20:44:18 +00003207 i++;
3208 }while( idx1<szHdr1 && i<pPKey2->nField );
dan1fed5da2014-02-25 21:01:25 +00003209
dan3b9330f2014-02-27 20:44:18 +00003210 /* No memory allocation is ever used on mem1. Prove this using
3211 ** the following assert(). If the assert() fails, it indicates a
3212 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3213 */
3214 assert( mem1.zMalloc==0 );
3215
3216 /* rc==0 here means that one of the keys ran out of fields and
3217 ** all the fields up to that point were equal. Return the the default_rc
3218 ** value. */
3219 return pPKey2->default_rc;
dan1fed5da2014-02-25 21:01:25 +00003220}
dan3833e932014-03-01 19:44:56 +00003221#endif
dan1fed5da2014-02-25 21:01:25 +00003222
dan3833e932014-03-01 19:44:56 +00003223/*
3224** Both *pMem1 and *pMem2 contain string values. Compare the two values
3225** using the collation sequence pColl. As usual, return a negative , zero
3226** or positive value if *pMem1 is less than, equal to or greater than
3227** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3228*/
dan1fed5da2014-02-25 21:01:25 +00003229static int vdbeCompareMemString(
dan3833e932014-03-01 19:44:56 +00003230 const Mem *pMem1,
3231 const Mem *pMem2,
dan38fdead2014-04-01 10:19:02 +00003232 const CollSeq *pColl,
3233 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
dan1fed5da2014-02-25 21:01:25 +00003234){
3235 if( pMem1->enc==pColl->enc ){
3236 /* The strings are already in the correct encoding. Call the
3237 ** comparison function directly */
3238 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3239 }else{
3240 int rc;
3241 const void *v1, *v2;
3242 int n1, n2;
3243 Mem c1;
3244 Mem c2;
3245 memset(&c1, 0, sizeof(c1));
3246 memset(&c2, 0, sizeof(c2));
3247 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3248 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3249 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3250 n1 = v1==0 ? 0 : c1.n;
3251 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3252 n2 = v2==0 ? 0 : c2.n;
3253 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3254 sqlite3VdbeMemRelease(&c1);
3255 sqlite3VdbeMemRelease(&c2);
dan38fdead2014-04-01 10:19:02 +00003256 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
dan1fed5da2014-02-25 21:01:25 +00003257 return rc;
3258 }
3259}
3260
3261/*
3262** Compare the values contained by the two memory cells, returning
3263** negative, zero or positive if pMem1 is less than, equal to, or greater
3264** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3265** and reals) sorted numerically, followed by text ordered by the collating
3266** sequence pColl and finally blob's ordered by memcmp().
3267**
3268** Two NULL values are considered equal by this function.
3269*/
3270int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3271 int rc;
3272 int f1, f2;
3273 int combined_flags;
3274
3275 f1 = pMem1->flags;
3276 f2 = pMem2->flags;
3277 combined_flags = f1|f2;
3278 assert( (combined_flags & MEM_RowSet)==0 );
3279
3280 /* If one value is NULL, it is less than the other. If both values
3281 ** are NULL, return 0.
3282 */
3283 if( combined_flags&MEM_Null ){
3284 return (f2&MEM_Null) - (f1&MEM_Null);
3285 }
3286
3287 /* If one value is a number and the other is not, the number is less.
3288 ** If both are numbers, compare as reals if one is a real, or as integers
3289 ** if both values are integers.
3290 */
3291 if( combined_flags&(MEM_Int|MEM_Real) ){
3292 double r1, r2;
3293 if( (f1 & f2 & MEM_Int)!=0 ){
3294 if( pMem1->u.i < pMem2->u.i ) return -1;
3295 if( pMem1->u.i > pMem2->u.i ) return 1;
3296 return 0;
3297 }
3298 if( (f1&MEM_Real)!=0 ){
3299 r1 = pMem1->r;
3300 }else if( (f1&MEM_Int)!=0 ){
3301 r1 = (double)pMem1->u.i;
3302 }else{
3303 return 1;
3304 }
3305 if( (f2&MEM_Real)!=0 ){
3306 r2 = pMem2->r;
3307 }else if( (f2&MEM_Int)!=0 ){
3308 r2 = (double)pMem2->u.i;
3309 }else{
3310 return -1;
3311 }
3312 if( r1<r2 ) return -1;
3313 if( r1>r2 ) return 1;
3314 return 0;
3315 }
3316
3317 /* If one value is a string and the other is a blob, the string is less.
3318 ** If both are strings, compare using the collating functions.
3319 */
3320 if( combined_flags&MEM_Str ){
3321 if( (f1 & MEM_Str)==0 ){
3322 return 1;
3323 }
3324 if( (f2 & MEM_Str)==0 ){
3325 return -1;
3326 }
3327
3328 assert( pMem1->enc==pMem2->enc );
3329 assert( pMem1->enc==SQLITE_UTF8 ||
3330 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3331
3332 /* The collation sequence must be defined at this point, even if
3333 ** the user deletes the collation sequence after the vdbe program is
3334 ** compiled (this was not always the case).
3335 */
3336 assert( !pColl || pColl->xCmp );
3337
3338 if( pColl ){
dan38fdead2014-04-01 10:19:02 +00003339 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
dan1fed5da2014-02-25 21:01:25 +00003340 }
3341 /* If a NULL pointer was passed as the collate function, fall through
3342 ** to the blob case and use memcmp(). */
3343 }
3344
3345 /* Both values must be blobs. Compare using memcmp(). */
3346 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
3347 if( rc==0 ){
3348 rc = pMem1->n - pMem2->n;
3349 }
3350 return rc;
3351}
3352
3353
dan3833e932014-03-01 19:44:56 +00003354/*
3355** The first argument passed to this function is a serial-type that
3356** corresponds to an integer - all values between 1 and 9 inclusive
3357** except 7. The second points to a buffer containing an integer value
3358** serialized according to serial_type. This function deserializes
3359** and returns the value.
3360*/
dan3b9330f2014-02-27 20:44:18 +00003361static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
drhf926d1e2014-03-04 04:04:33 +00003362 u32 y;
dan3833e932014-03-01 19:44:56 +00003363 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
dan3b9330f2014-02-27 20:44:18 +00003364 switch( serial_type ){
dan3833e932014-03-01 19:44:56 +00003365 case 0:
dan3b9330f2014-02-27 20:44:18 +00003366 case 1:
drhb6e8fd12014-03-06 01:56:33 +00003367 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003368 return ONE_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003369 case 2:
drhb6e8fd12014-03-06 01:56:33 +00003370 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003371 return TWO_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003372 case 3:
drhb6e8fd12014-03-06 01:56:33 +00003373 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003374 return THREE_BYTE_INT(aKey);
3375 case 4: {
drhb6e8fd12014-03-06 01:56:33 +00003376 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003377 y = FOUR_BYTE_UINT(aKey);
3378 return (i64)*(int*)&y;
3379 }
dan3b9330f2014-02-27 20:44:18 +00003380 case 5: {
drhb6e8fd12014-03-06 01:56:33 +00003381 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003382 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhaf5b2af2013-08-05 15:32:09 +00003383 }
dan3b9330f2014-02-27 20:44:18 +00003384 case 6: {
drhf926d1e2014-03-04 04:04:33 +00003385 u64 x = FOUR_BYTE_UINT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003386 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003387 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3388 return (i64)*(i64*)&x;
drh1e968a02008-03-25 00:22:21 +00003389 }
dan3b9330f2014-02-27 20:44:18 +00003390 }
drh407414c2009-07-14 14:15:27 +00003391
dan3b9330f2014-02-27 20:44:18 +00003392 return (serial_type - 8);
drh1e968a02008-03-25 00:22:21 +00003393}
danielk1977eb015e02004-05-18 01:31:14 +00003394
dan3833e932014-03-01 19:44:56 +00003395/*
3396** This function compares the two table rows or index records
3397** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
3398** or positive integer if key1 is less than, equal to or
3399** greater than key2. The {nKey1, pKey1} key must be a blob
3400** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
3401** key must be a parsed key such as obtained from
3402** sqlite3VdbeParseRecord.
3403**
3404** If argument bSkip is non-zero, it is assumed that the caller has already
3405** determined that the first fields of the keys are equal.
3406**
3407** Key1 and Key2 do not have to contain the same number of fields. If all
3408** fields that appear in both keys are equal, then pPKey2->default_rc is
3409** returned.
drha1f7c0a2014-03-28 03:12:48 +00003410**
dan38fdead2014-04-01 10:19:02 +00003411** If database corruption is discovered, set pPKey2->errCode to
3412** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
3413** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
3414** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
dan3833e932014-03-01 19:44:56 +00003415*/
3416int sqlite3VdbeRecordCompare(
3417 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003418 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003419 int bSkip /* If true, skip the first field */
dan1fed5da2014-02-25 21:01:25 +00003420){
dan3833e932014-03-01 19:44:56 +00003421 u32 d1; /* Offset into aKey[] of next data element */
3422 int i; /* Index of next field to compare */
mistachkinffe6bc22014-03-04 11:16:20 +00003423 u32 szHdr1; /* Size of record header in bytes */
dan3833e932014-03-01 19:44:56 +00003424 u32 idx1; /* Offset of first type in header */
3425 int rc = 0; /* Return value */
3426 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
dan1fed5da2014-02-25 21:01:25 +00003427 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3428 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3429 Mem mem1;
3430
dan3833e932014-03-01 19:44:56 +00003431 /* If bSkip is true, then the caller has already determined that the first
3432 ** two elements in the keys are equal. Fix the various stack variables so
dan3b9330f2014-02-27 20:44:18 +00003433 ** that this routine begins comparing at the second field. */
dan3833e932014-03-01 19:44:56 +00003434 if( bSkip ){
dan3b9330f2014-02-27 20:44:18 +00003435 u32 s1;
dan3b9330f2014-02-27 20:44:18 +00003436 idx1 = 1 + getVarint32(&aKey1[1], s1);
dan3833e932014-03-01 19:44:56 +00003437 szHdr1 = aKey1[0];
3438 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
dan3b9330f2014-02-27 20:44:18 +00003439 i = 1;
3440 pRhs++;
dan3833e932014-03-01 19:44:56 +00003441 }else{
3442 idx1 = getVarint32(aKey1, szHdr1);
3443 d1 = szHdr1;
drha1f7c0a2014-03-28 03:12:48 +00003444 if( d1>(unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003445 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003446 return 0; /* Corruption */
3447 }
dan3833e932014-03-01 19:44:56 +00003448 i = 0;
dan3b9330f2014-02-27 20:44:18 +00003449 }
3450
dan1fed5da2014-02-25 21:01:25 +00003451 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003452 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3453 || CORRUPT_DB );
3454 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3455 assert( pPKey2->pKeyInfo->nField>0 );
3456 assert( idx1<=szHdr1 || CORRUPT_DB );
3457 do{
dan1fed5da2014-02-25 21:01:25 +00003458 u32 serial_type;
3459
3460 /* RHS is an integer */
3461 if( pRhs->flags & MEM_Int ){
3462 serial_type = aKey1[idx1];
drhb6e8fd12014-03-06 01:56:33 +00003463 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003464 if( serial_type>=12 ){
3465 rc = +1;
3466 }else if( serial_type==0 ){
3467 rc = -1;
dan3b9330f2014-02-27 20:44:18 +00003468 }else if( serial_type==7 ){
3469 double rhs = (double)pRhs->u.i;
dan1fed5da2014-02-25 21:01:25 +00003470 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
dan3b9330f2014-02-27 20:44:18 +00003471 if( mem1.r<rhs ){
3472 rc = -1;
3473 }else if( mem1.r>rhs ){
3474 rc = +1;
3475 }
3476 }else{
3477 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3478 i64 rhs = pRhs->u.i;
3479 if( lhs<rhs ){
3480 rc = -1;
3481 }else if( lhs>rhs ){
3482 rc = +1;
dan1fed5da2014-02-25 21:01:25 +00003483 }
3484 }
3485 }
3486
3487 /* RHS is real */
3488 else if( pRhs->flags & MEM_Real ){
3489 serial_type = aKey1[idx1];
3490 if( serial_type>=12 ){
3491 rc = +1;
3492 }else if( serial_type==0 ){
3493 rc = -1;
3494 }else{
3495 double rhs = pRhs->r;
3496 double lhs;
3497 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3498 if( serial_type==7 ){
3499 lhs = mem1.r;
3500 }else{
drh295aedf2014-03-03 18:25:24 +00003501 lhs = (double)mem1.u.i;
dan1fed5da2014-02-25 21:01:25 +00003502 }
3503 if( lhs<rhs ){
3504 rc = -1;
3505 }else if( lhs>rhs ){
3506 rc = +1;
3507 }
3508 }
3509 }
3510
3511 /* RHS is a string */
3512 else if( pRhs->flags & MEM_Str ){
3513 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003514 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003515 if( serial_type<12 ){
3516 rc = -1;
3517 }else if( !(serial_type & 0x01) ){
3518 rc = +1;
3519 }else{
3520 mem1.n = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003521 testcase( (d1+mem1.n)==(unsigned)nKey1 );
3522 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003523 if( (d1+mem1.n) > (unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003524 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003525 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003526 }else if( pKeyInfo->aColl[i] ){
3527 mem1.enc = pKeyInfo->enc;
3528 mem1.db = pKeyInfo->db;
3529 mem1.flags = MEM_Str;
drhfcb44a82014-03-03 15:13:27 +00003530 mem1.z = (char*)&aKey1[d1];
dan38fdead2014-04-01 10:19:02 +00003531 rc = vdbeCompareMemString(
3532 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
3533 );
dan1fed5da2014-02-25 21:01:25 +00003534 }else{
3535 int nCmp = MIN(mem1.n, pRhs->n);
3536 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3537 if( rc==0 ) rc = mem1.n - pRhs->n;
3538 }
3539 }
3540 }
3541
3542 /* RHS is a blob */
3543 else if( pRhs->flags & MEM_Blob ){
3544 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003545 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003546 if( serial_type<12 || (serial_type & 0x01) ){
3547 rc = -1;
3548 }else{
3549 int nStr = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003550 testcase( (d1+nStr)==(unsigned)nKey1 );
3551 testcase( (d1+nStr+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003552 if( (d1+nStr) > (unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003553 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003554 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003555 }else{
3556 int nCmp = MIN(nStr, pRhs->n);
3557 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3558 if( rc==0 ) rc = nStr - pRhs->n;
3559 }
3560 }
3561 }
3562
3563 /* RHS is null */
3564 else{
3565 serial_type = aKey1[idx1];
3566 rc = (serial_type!=0);
3567 }
3568
3569 if( rc!=0 ){
dan1fed5da2014-02-25 21:01:25 +00003570 if( pKeyInfo->aSortOrder[i] ){
3571 rc = -rc;
dan1fed5da2014-02-25 21:01:25 +00003572 }
dan38fdead2014-04-01 10:19:02 +00003573 assert( CORRUPT_DB || pKeyInfo->db==0
dan3833e932014-03-01 19:44:56 +00003574 || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3575 || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
drhed79b452014-03-04 16:21:18 +00003576 || pKeyInfo->db->mallocFailed
dan3833e932014-03-01 19:44:56 +00003577 );
3578 assert( mem1.zMalloc==0 ); /* See comment below */
dan1fed5da2014-02-25 21:01:25 +00003579 return rc;
3580 }
3581
3582 i++;
dan3b9330f2014-02-27 20:44:18 +00003583 pRhs++;
dan1fed5da2014-02-25 21:01:25 +00003584 d1 += sqlite3VdbeSerialTypeLen(serial_type);
3585 idx1 += sqlite3VarintLen(serial_type);
drh295aedf2014-03-03 18:25:24 +00003586 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
dan1fed5da2014-02-25 21:01:25 +00003587
3588 /* No memory allocation is ever used on mem1. Prove this using
3589 ** the following assert(). If the assert() fails, it indicates a
dan3833e932014-03-01 19:44:56 +00003590 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
dan1fed5da2014-02-25 21:01:25 +00003591 assert( mem1.zMalloc==0 );
3592
3593 /* rc==0 here means that one or both of the keys ran out of fields and
3594 ** all the fields up to that point were equal. Return the the default_rc
3595 ** value. */
dan3833e932014-03-01 19:44:56 +00003596 assert( CORRUPT_DB
3597 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
3598 );
dan1fed5da2014-02-25 21:01:25 +00003599 return pPKey2->default_rc;
3600}
3601
dan3833e932014-03-01 19:44:56 +00003602/*
3603** This function is an optimized version of sqlite3VdbeRecordCompare()
3604** that (a) the first field of pPKey2 is an integer, and (b) the
3605** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3606** byte (i.e. is less than 128).
drhe2ac5062014-03-26 12:02:38 +00003607**
3608** To avoid concerns about buffer overreads, this routine is only used
3609** on schemas where the maximum valid header size is 63 bytes or less.
dan3833e932014-03-01 19:44:56 +00003610*/
dan3b9330f2014-02-27 20:44:18 +00003611static int vdbeRecordCompareInt(
3612 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003613 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003614 int bSkip /* Ignored */
dan3b9330f2014-02-27 20:44:18 +00003615){
dan9b8afef2014-03-03 20:48:50 +00003616 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
dan3b9330f2014-02-27 20:44:18 +00003617 int serial_type = ((const u8*)pKey1)[1];
3618 int res;
drhf926d1e2014-03-04 04:04:33 +00003619 u32 y;
3620 u64 x;
dan3b9330f2014-02-27 20:44:18 +00003621 i64 v = pPKey2->aMem[0].u.i;
3622 i64 lhs;
drh295aedf2014-03-03 18:25:24 +00003623 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003624
dan3833e932014-03-01 19:44:56 +00003625 assert( bSkip==0 );
drhe2ac5062014-03-26 12:02:38 +00003626 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
dan3833e932014-03-01 19:44:56 +00003627 switch( serial_type ){
drhf926d1e2014-03-04 04:04:33 +00003628 case 1: { /* 1-byte signed integer */
3629 lhs = ONE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003630 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003631 break;
3632 }
drhf926d1e2014-03-04 04:04:33 +00003633 case 2: { /* 2-byte signed integer */
3634 lhs = TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003635 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003636 break;
3637 }
3638 case 3: { /* 3-byte signed integer */
3639 lhs = THREE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003640 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003641 break;
3642 }
3643 case 4: { /* 4-byte signed integer */
3644 y = FOUR_BYTE_UINT(aKey);
3645 lhs = (i64)*(int*)&y;
drhb6e8fd12014-03-06 01:56:33 +00003646 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003647 break;
3648 }
3649 case 5: { /* 6-byte signed integer */
3650 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003651 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003652 break;
3653 }
3654 case 6: { /* 8-byte signed integer */
3655 x = FOUR_BYTE_UINT(aKey);
3656 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3657 lhs = *(i64*)&x;
drhb6e8fd12014-03-06 01:56:33 +00003658 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003659 break;
3660 }
dan3b9330f2014-02-27 20:44:18 +00003661 case 8:
3662 lhs = 0;
3663 break;
dan3b9330f2014-02-27 20:44:18 +00003664 case 9:
3665 lhs = 1;
3666 break;
3667
dan063d4a02014-02-28 09:48:30 +00003668 /* This case could be removed without changing the results of running
3669 ** this code. Including it causes gcc to generate a faster switch
3670 ** statement (since the range of switch targets now starts at zero and
dan597515d2014-02-28 18:39:51 +00003671 ** is contiguous) but does not cause any duplicate code to be generated
dan063d4a02014-02-28 09:48:30 +00003672 ** (as gcc is clever enough to combine the two like cases). Other
3673 ** compilers might be similar. */
3674 case 0: case 7:
dan3833e932014-03-01 19:44:56 +00003675 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan063d4a02014-02-28 09:48:30 +00003676
dan3b9330f2014-02-27 20:44:18 +00003677 default:
dan3833e932014-03-01 19:44:56 +00003678 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan3b9330f2014-02-27 20:44:18 +00003679 }
3680
3681 if( v>lhs ){
3682 res = pPKey2->r1;
3683 }else if( v<lhs ){
3684 res = pPKey2->r2;
3685 }else if( pPKey2->nField>1 ){
dan063d4a02014-02-28 09:48:30 +00003686 /* The first fields of the two keys are equal. Compare the trailing
3687 ** fields. */
dan3833e932014-03-01 19:44:56 +00003688 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003689 }else{
dan063d4a02014-02-28 09:48:30 +00003690 /* The first fields of the two keys are equal and there are no trailing
3691 ** fields. Return pPKey2->default_rc in this case. */
dan3b9330f2014-02-27 20:44:18 +00003692 res = pPKey2->default_rc;
3693 }
3694
dan3833e932014-03-01 19:44:56 +00003695 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3696 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3697 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003698 || CORRUPT_DB
3699 );
3700 return res;
3701}
3702
dan3833e932014-03-01 19:44:56 +00003703/*
3704** This function is an optimized version of sqlite3VdbeRecordCompare()
3705** that (a) the first field of pPKey2 is a string, that (b) the first field
3706** uses the collation sequence BINARY and (c) that the size-of-header varint
3707** at the start of (pKey1/nKey1) fits in a single byte.
3708*/
dan3b9330f2014-02-27 20:44:18 +00003709static int vdbeRecordCompareString(
3710 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003711 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003712 int bSkip
dan3b9330f2014-02-27 20:44:18 +00003713){
3714 const u8 *aKey1 = (const u8*)pKey1;
3715 int serial_type;
3716 int res;
drh295aedf2014-03-03 18:25:24 +00003717 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003718
dan3833e932014-03-01 19:44:56 +00003719 assert( bSkip==0 );
dan3b9330f2014-02-27 20:44:18 +00003720 getVarint32(&aKey1[1], serial_type);
3721
3722 if( serial_type<12 ){
3723 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
3724 }else if( !(serial_type & 0x01) ){
3725 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
3726 }else{
3727 int nCmp;
3728 int nStr;
dan3833e932014-03-01 19:44:56 +00003729 int szHdr = aKey1[0];
dan3b9330f2014-02-27 20:44:18 +00003730
3731 nStr = (serial_type-12) / 2;
drha1f7c0a2014-03-28 03:12:48 +00003732 if( (szHdr + nStr) > nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003733 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003734 return 0; /* Corruption */
3735 }
dan3b9330f2014-02-27 20:44:18 +00003736 nCmp = MIN( pPKey2->aMem[0].n, nStr );
dan3833e932014-03-01 19:44:56 +00003737 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
dan3b9330f2014-02-27 20:44:18 +00003738
3739 if( res==0 ){
3740 res = nStr - pPKey2->aMem[0].n;
3741 if( res==0 ){
3742 if( pPKey2->nField>1 ){
dan3833e932014-03-01 19:44:56 +00003743 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003744 }else{
3745 res = pPKey2->default_rc;
3746 }
3747 }else if( res>0 ){
3748 res = pPKey2->r2;
3749 }else{
3750 res = pPKey2->r1;
3751 }
3752 }else if( res>0 ){
3753 res = pPKey2->r2;
3754 }else{
3755 res = pPKey2->r1;
3756 }
3757 }
3758
dan3833e932014-03-01 19:44:56 +00003759 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3760 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3761 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003762 || CORRUPT_DB
3763 );
3764 return res;
3765}
3766
dan3833e932014-03-01 19:44:56 +00003767/*
3768** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3769** suitable for comparing serialized records to the unpacked record passed
3770** as the only argument.
3771*/
dan1fed5da2014-02-25 21:01:25 +00003772RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
dan9b8afef2014-03-03 20:48:50 +00003773 /* varintRecordCompareInt() and varintRecordCompareString() both assume
3774 ** that the size-of-header varint that occurs at the start of each record
3775 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
3776 ** also assumes that it is safe to overread a buffer by at least the
3777 ** maximum possible legal header size plus 8 bytes. Because there is
3778 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
3779 ** buffer passed to varintRecordCompareInt() this makes it convenient to
3780 ** limit the size of the header to 64 bytes in cases where the first field
3781 ** is an integer.
3782 **
3783 ** The easiest way to enforce this limit is to consider only records with
3784 ** 13 fields or less. If the first field is an integer, the maximum legal
3785 ** header size is (12*5 + 1 + 1) bytes. */
3786 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
dan1fed5da2014-02-25 21:01:25 +00003787 int flags = p->aMem[0].flags;
dan3b9330f2014-02-27 20:44:18 +00003788 if( p->pKeyInfo->aSortOrder[0] ){
3789 p->r1 = 1;
3790 p->r2 = -1;
3791 }else{
3792 p->r1 = -1;
3793 p->r2 = 1;
3794 }
dan1fed5da2014-02-25 21:01:25 +00003795 if( (flags & MEM_Int) ){
3796 return vdbeRecordCompareInt;
dan3b9330f2014-02-27 20:44:18 +00003797 }
drhb6e8fd12014-03-06 01:56:33 +00003798 testcase( flags & MEM_Real );
3799 testcase( flags & MEM_Null );
3800 testcase( flags & MEM_Blob );
3801 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
3802 assert( flags & MEM_Str );
dan1fed5da2014-02-25 21:01:25 +00003803 return vdbeRecordCompareString;
3804 }
3805 }
dan3b9330f2014-02-27 20:44:18 +00003806
dan3833e932014-03-01 19:44:56 +00003807 return sqlite3VdbeRecordCompare;
dan3b9330f2014-02-27 20:44:18 +00003808}
dan1fed5da2014-02-25 21:01:25 +00003809
danielk1977eb015e02004-05-18 01:31:14 +00003810/*
drh7a224de2004-06-02 01:22:02 +00003811** pCur points at an index entry created using the OP_MakeRecord opcode.
3812** Read the rowid (the last field in the record) and store it in *rowid.
3813** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00003814**
3815** pCur might be pointing to text obtained from a corrupt database file.
3816** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00003817*/
drh35f6b932009-06-23 14:15:04 +00003818int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00003819 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003820 int rc;
drhd5788202004-05-28 08:21:05 +00003821 u32 szHdr; /* Size of the header */
3822 u32 typeRowid; /* Serial type of the rowid */
3823 u32 lenRowid; /* Size of the rowid */
3824 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00003825
shanecea72b22009-09-07 04:38:36 +00003826 UNUSED_PARAMETER(db);
3827
drh88a003e2008-12-11 16:17:03 +00003828 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00003829 ** than 2GiB are support - anything large must be database corruption.
3830 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00003831 ** this code can safely assume that nCellKey is 32-bits
3832 */
drhea8ffdf2009-07-22 00:35:23 +00003833 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003834 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003835 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00003836 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003837
3838 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003839 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003840 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003841 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003842 return rc;
3843 }
drh88a003e2008-12-11 16:17:03 +00003844
3845 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003846 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003847 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003848 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003849 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003850 goto idx_rowid_corruption;
3851 }
3852
3853 /* The last field of the index should be an integer - the ROWID.
3854 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003855 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003856 testcase( typeRowid==1 );
3857 testcase( typeRowid==2 );
3858 testcase( typeRowid==3 );
3859 testcase( typeRowid==4 );
3860 testcase( typeRowid==5 );
3861 testcase( typeRowid==6 );
3862 testcase( typeRowid==8 );
3863 testcase( typeRowid==9 );
3864 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3865 goto idx_rowid_corruption;
3866 }
drhd5788202004-05-28 08:21:05 +00003867 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003868 testcase( (u32)m.n==szHdr+lenRowid );
3869 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003870 goto idx_rowid_corruption;
3871 }
3872
3873 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003874 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003875 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003876 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003877 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003878
3879 /* Jump here if database corruption is detected after m has been
3880 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3881idx_rowid_corruption:
3882 testcase( m.zMalloc!=0 );
3883 sqlite3VdbeMemRelease(&m);
3884 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003885}
3886
drh7cf6e4d2004-05-19 14:56:55 +00003887/*
drh5f82e3c2009-07-06 00:44:08 +00003888** Compare the key of the index entry that cursor pC is pointing to against
3889** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003890** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003891** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003892**
drh5f82e3c2009-07-06 00:44:08 +00003893** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003894** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003895** is ignored as well. Hence, this routine only compares the prefixes
3896** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003897*/
danielk1977183f9f72004-05-13 05:20:26 +00003898int sqlite3VdbeIdxKeyCompare(
drh295aedf2014-03-03 18:25:24 +00003899 VdbeCursor *pC, /* The cursor to compare against */
drha1f7c0a2014-03-28 03:12:48 +00003900 UnpackedRecord *pUnpacked, /* Unpacked version of key */
drh295aedf2014-03-03 18:25:24 +00003901 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003902){
drh61fc5952007-04-01 23:49:51 +00003903 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003904 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003905 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003906 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003907
drhea8ffdf2009-07-22 00:35:23 +00003908 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003909 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003910 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh56689692014-03-03 19:29:28 +00003911 /* nCellKey will always be between 0 and 0xffffffff because of the way
drh407414c2009-07-14 14:15:27 +00003912 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003913 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003914 *res = 0;
drh9978c972010-02-23 17:36:32 +00003915 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003916 }
drhfd3ca1c2009-08-25 12:11:00 +00003917 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003918 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003919 if( rc ){
drhd5788202004-05-28 08:21:05 +00003920 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003921 }
dan3833e932014-03-01 19:44:56 +00003922 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
danielk1977d8123362004-06-12 09:25:12 +00003923 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003924 return SQLITE_OK;
3925}
danielk1977b28af712004-06-21 06:50:26 +00003926
3927/*
3928** This routine sets the value to be returned by subsequent calls to
3929** sqlite3_changes() on the database handle 'db'.
3930*/
3931void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003932 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003933 db->nChange = nChange;
3934 db->nTotalChange += nChange;
3935}
3936
3937/*
3938** Set a flag in the vdbe to update the change counter when it is finalised
3939** or reset.
3940*/
drh4794f732004-11-05 17:17:50 +00003941void sqlite3VdbeCountChanges(Vdbe *v){
3942 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003943}
drhd89bd002005-01-22 03:03:54 +00003944
3945/*
3946** Mark every prepared statement associated with a database connection
3947** as expired.
3948**
3949** An expired statement means that recompilation of the statement is
3950** recommend. Statements expire when things happen that make their
3951** programs obsolete. Removing user-defined functions or collating
3952** sequences, or changing an authorization function are the types of
3953** things that make prepared statements obsolete.
3954*/
3955void sqlite3ExpirePreparedStatements(sqlite3 *db){
3956 Vdbe *p;
3957 for(p = db->pVdbe; p; p=p->pNext){
3958 p->expired = 1;
3959 }
3960}
danielk1977aee18ef2005-03-09 12:26:50 +00003961
3962/*
3963** Return the database associated with the Vdbe.
3964*/
3965sqlite3 *sqlite3VdbeDb(Vdbe *v){
3966 return v->db;
3967}
dan937d0de2009-10-15 18:35:38 +00003968
3969/*
3970** Return a pointer to an sqlite3_value structure containing the value bound
3971** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3972** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3973** constants) to the value before returning it.
3974**
3975** The returned value must be freed by the caller using sqlite3ValueFree().
3976*/
drhcf0fd4a2013-08-01 12:21:58 +00003977sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
dan937d0de2009-10-15 18:35:38 +00003978 assert( iVar>0 );
3979 if( v ){
3980 Mem *pMem = &v->aVar[iVar-1];
3981 if( 0==(pMem->flags & MEM_Null) ){
3982 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3983 if( pRet ){
3984 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3985 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
dan937d0de2009-10-15 18:35:38 +00003986 }
3987 return pRet;
3988 }
3989 }
3990 return 0;
3991}
3992
3993/*
3994** Configure SQL variable iVar so that binding a new value to it signals
3995** to sqlite3_reoptimize() that re-preparing the statement may result
3996** in a better query plan.
3997*/
dan1d2ce4f2009-10-19 18:11:09 +00003998void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003999 assert( iVar>0 );
4000 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00004001 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00004002 }else{
dan1d2ce4f2009-10-19 18:11:09 +00004003 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00004004 }
4005}
dan016f7812013-08-21 17:35:48 +00004006
4007#ifndef SQLITE_OMIT_VIRTUALTABLE
4008/*
4009** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4010** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4011** in memory obtained from sqlite3DbMalloc).
4012*/
4013void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
4014 sqlite3 *db = p->db;
4015 sqlite3DbFree(db, p->zErrMsg);
4016 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
4017 sqlite3_free(pVtab->zErrMsg);
4018 pVtab->zErrMsg = 0;
4019}
4020#endif /* SQLITE_OMIT_VIRTUALTABLE */