blob: 240085bfbff33a1747bb9455c442374523d0ee14 [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 );
drhd2490902014-04-13 19:28:15 +0000279 if( ALWAYS(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;
drha7ab6d82014-07-21 15:44:39 +0000502 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
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 */
drha7ab6d82014-07-21 15:44:39 +0000529 assert( DbMaskAllZero(p->btreeMask) );
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 );
drha7ab6d82014-07-21 15:44:39 +00001114 DbMaskSet(p->btreeMask, i);
drhdc5b0472011-04-06 22:05:53 +00001115 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
drha7ab6d82014-07-21 15:44:39 +00001116 DbMaskSet(p->lockMask, i);
drhdc5b0472011-04-06 22:05:53 +00001117 }
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;
drhdc5b0472011-04-06 22:05:53 +00001144 sqlite3 *db;
1145 Db *aDb;
1146 int nDb;
drha7ab6d82014-07-21 15:44:39 +00001147 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
drhdc5b0472011-04-06 22:05:53 +00001148 db = p->db;
1149 aDb = db->aDb;
1150 nDb = db->nDb;
drha7ab6d82014-07-21 15:44:39 +00001151 for(i=0; i<nDb; i++){
1152 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001153 sqlite3BtreeEnter(aDb[i].pBt);
1154 }
1155 }
drhbdaec522011-04-04 00:14:43 +00001156}
drhe54e0512011-04-05 17:31:56 +00001157#endif
drhbdaec522011-04-04 00:14:43 +00001158
drhe54e0512011-04-05 17:31:56 +00001159#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001160/*
1161** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1162*/
1163void sqlite3VdbeLeave(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001164 int i;
drhdc5b0472011-04-06 22:05:53 +00001165 sqlite3 *db;
1166 Db *aDb;
1167 int nDb;
drha7ab6d82014-07-21 15:44:39 +00001168 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
drhdc5b0472011-04-06 22:05:53 +00001169 db = p->db;
1170 aDb = db->aDb;
1171 nDb = db->nDb;
drha7ab6d82014-07-21 15:44:39 +00001172 for(i=0; i<nDb; i++){
1173 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001174 sqlite3BtreeLeave(aDb[i].pBt);
1175 }
1176 }
drhbdaec522011-04-04 00:14:43 +00001177}
drhbdaec522011-04-04 00:14:43 +00001178#endif
drhd3d39e92004-05-20 22:16:29 +00001179
danielk19778b60e0f2005-01-12 09:10:39 +00001180#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +00001181/*
1182** Print a single opcode. This routine is used for debugging only.
1183*/
danielk19774adee202004-05-08 08:23:19 +00001184void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +00001185 char *zP4;
drhd3d39e92004-05-20 22:16:29 +00001186 char zPtr[50];
drh81316f82013-10-29 20:40:47 +00001187 char zCom[100];
drh26198bb2013-10-31 11:15:09 +00001188 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +00001189 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +00001190 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
drhc7379ce2013-10-30 02:28:23 +00001191#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001192 displayComment(pOp, zP4, zCom, sizeof(zCom));
1193#else
drh2926f962014-02-17 01:13:28 +00001194 zCom[0] = 0;
drh81316f82013-10-29 20:40:47 +00001195#endif
drh4eded602013-12-20 15:59:20 +00001196 /* NB: The sqlite3OpcodeName() function is implemented by code created
1197 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1198 ** information from the vdbe.c source text */
danielk197711641c12008-01-03 08:18:30 +00001199 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +00001200 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
drh81316f82013-10-29 20:40:47 +00001201 zCom
drh1db639c2008-01-17 02:36:28 +00001202 );
drh9a324642003-09-06 20:12:01 +00001203 fflush(pOut);
1204}
1205#endif
1206
1207/*
drh76ff3a02004-09-24 22:32:30 +00001208** Release an array of N Mem elements
1209*/
drhc890fec2008-08-01 20:10:08 +00001210static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +00001211 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +00001212 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +00001213 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +00001214 u8 malloc_failed = db->mallocFailed;
dand46def72010-07-24 11:28:28 +00001215 if( db->pnBytesFreed ){
1216 for(pEnd=&p[N]; p<pEnd; p++){
1217 sqlite3DbFree(db, p->zMalloc);
1218 }
drhc176c272010-07-26 13:57:59 +00001219 return;
1220 }
danielk1977e972e032008-09-19 18:32:26 +00001221 for(pEnd=&p[N]; p<pEnd; p++){
1222 assert( (&p[1])==pEnd || p[0].db==p[1].db );
drh75fd0542014-03-01 16:24:44 +00001223 assert( sqlite3VdbeCheckMemInvariants(p) );
danielk1977e972e032008-09-19 18:32:26 +00001224
1225 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1226 ** that takes advantage of the fact that the memory cell value is
1227 ** being set to NULL after releasing any dynamic resources.
1228 **
1229 ** The justification for duplicating code is that according to
1230 ** callgrind, this causes a certain test case to hit the CPU 4.7
1231 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1232 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1233 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1234 ** with no indexes using a single prepared INSERT statement, bind()
1235 ** and reset(). Inserts are grouped into a transaction.
1236 */
drhb6e8fd12014-03-06 01:56:33 +00001237 testcase( p->flags & MEM_Agg );
1238 testcase( p->flags & MEM_Dyn );
1239 testcase( p->flags & MEM_Frame );
1240 testcase( p->flags & MEM_RowSet );
dan165921a2009-08-28 18:53:45 +00001241 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001242 sqlite3VdbeMemRelease(p);
1243 }else if( p->zMalloc ){
1244 sqlite3DbFree(db, p->zMalloc);
1245 p->zMalloc = 0;
1246 }
1247
drha5750cf2014-02-07 13:20:31 +00001248 p->flags = MEM_Undefined;
drh76ff3a02004-09-24 22:32:30 +00001249 }
danielk1977a7a8e142008-02-13 18:25:27 +00001250 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001251 }
1252}
1253
dan65a7cd12009-09-01 12:16:01 +00001254/*
1255** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1256** allocated by the OP_Program opcode in sqlite3VdbeExec().
1257*/
dan165921a2009-08-28 18:53:45 +00001258void sqlite3VdbeFrameDelete(VdbeFrame *p){
1259 int i;
1260 Mem *aMem = VdbeFrameMem(p);
1261 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1262 for(i=0; i<p->nChildCsr; i++){
1263 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1264 }
1265 releaseMemArray(aMem, p->nChildMem);
1266 sqlite3DbFree(p->v->db, p);
1267}
1268
drhb7f91642004-10-31 02:22:47 +00001269#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001270/*
drh9a324642003-09-06 20:12:01 +00001271** Give a listing of the program in the virtual machine.
1272**
danielk19774adee202004-05-08 08:23:19 +00001273** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001274** running the code, it invokes the callback once for each instruction.
1275** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001276**
1277** When p->explain==1, each instruction is listed. When
1278** p->explain==2, only OP_Explain instructions are listed and these
1279** are shown in a different format. p->explain==2 is used to implement
1280** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001281**
1282** When p->explain==1, first the main program is listed, then each of
1283** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001284*/
danielk19774adee202004-05-08 08:23:19 +00001285int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001286 Vdbe *p /* The VDBE */
1287){
drh5cfa5842009-12-31 20:35:08 +00001288 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001289 int nSub = 0; /* Number of sub-vdbes seen so far */
1290 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001291 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1292 sqlite3 *db = p->db; /* The database connection */
1293 int i; /* Loop counter */
1294 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001295 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001296
drh9a324642003-09-06 20:12:01 +00001297 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001298 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001299 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001300
drh9cbf3422008-01-17 16:22:13 +00001301 /* Even though this opcode does not use dynamic strings for
1302 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001303 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001304 */
dan165921a2009-08-28 18:53:45 +00001305 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001306 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001307
danielk19776c359f02008-11-21 16:58:03 +00001308 if( p->rc==SQLITE_NOMEM ){
1309 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1310 ** sqlite3_column_text16() failed. */
1311 db->mallocFailed = 1;
1312 return SQLITE_ERROR;
1313 }
1314
drh5cfa5842009-12-31 20:35:08 +00001315 /* When the number of output rows reaches nRow, that means the
1316 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1317 ** nRow is the sum of the number of rows in the main program, plus
1318 ** the sum of the number of rows in all trigger subprograms encountered
1319 ** so far. The nRow value will increase as new trigger subprograms are
1320 ** encountered, but p->pc will eventually catch up to nRow.
1321 */
dan165921a2009-08-28 18:53:45 +00001322 nRow = p->nOp;
1323 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001324 /* The first 8 memory cells are used for the result set. So we will
1325 ** commandeer the 9th cell to use as storage for an array of pointers
1326 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1327 ** cells. */
1328 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001329 pSub = &p->aMem[9];
1330 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001331 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1332 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001333 nSub = pSub->n/sizeof(Vdbe*);
1334 apSub = (SubProgram **)pSub->z;
1335 }
1336 for(i=0; i<nSub; i++){
1337 nRow += apSub[i]->nOp;
1338 }
1339 }
1340
drhecc92422005-09-10 16:46:12 +00001341 do{
1342 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001343 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1344 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001345 p->rc = SQLITE_OK;
1346 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001347 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001348 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001349 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001350 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001351 }else{
drh81316f82013-10-29 20:40:47 +00001352 char *zP4;
dan165921a2009-08-28 18:53:45 +00001353 Op *pOp;
1354 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001355 /* The output line number is small enough that we are still in the
1356 ** main program. */
dan165921a2009-08-28 18:53:45 +00001357 pOp = &p->aOp[i];
1358 }else{
drh5cfa5842009-12-31 20:35:08 +00001359 /* We are currently listing subprograms. Figure out which one and
1360 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001361 int j;
1362 i -= p->nOp;
1363 for(j=0; i>=apSub[j]->nOp; j++){
1364 i -= apSub[j]->nOp;
1365 }
1366 pOp = &apSub[j]->aOp[i];
1367 }
danielk19770d78bae2008-01-03 07:09:48 +00001368 if( p->explain==1 ){
1369 pMem->flags = MEM_Int;
danielk19770d78bae2008-01-03 07:09:48 +00001370 pMem->u.i = i; /* Program counter */
1371 pMem++;
1372
1373 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001374 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
danielk19770d78bae2008-01-03 07:09:48 +00001375 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001376 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001377 pMem->enc = SQLITE_UTF8;
1378 pMem++;
dan165921a2009-08-28 18:53:45 +00001379
drh5cfa5842009-12-31 20:35:08 +00001380 /* When an OP_Program opcode is encounter (the only opcode that has
1381 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1382 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1383 ** has not already been seen.
1384 */
dan165921a2009-08-28 18:53:45 +00001385 if( pOp->p4type==P4_SUBPROGRAM ){
1386 int nByte = (nSub+1)*sizeof(SubProgram*);
1387 int j;
1388 for(j=0; j<nSub; j++){
1389 if( apSub[j]==pOp->p4.pProgram ) break;
1390 }
dan2b9ee772012-03-31 09:59:44 +00001391 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
dan165921a2009-08-28 18:53:45 +00001392 apSub = (SubProgram **)pSub->z;
1393 apSub[nSub++] = pOp->p4.pProgram;
1394 pSub->flags |= MEM_Blob;
1395 pSub->n = nSub*sizeof(SubProgram*);
1396 }
1397 }
danielk19770d78bae2008-01-03 07:09:48 +00001398 }
drheb2e1762004-05-27 01:53:56 +00001399
1400 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001401 pMem->u.i = pOp->p1; /* P1 */
drheb2e1762004-05-27 01:53:56 +00001402 pMem++;
1403
1404 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001405 pMem->u.i = pOp->p2; /* P2 */
drheb2e1762004-05-27 01:53:56 +00001406 pMem++;
1407
dan2ce22452010-11-08 19:01:16 +00001408 pMem->flags = MEM_Int;
1409 pMem->u.i = pOp->p3; /* P3 */
dan2ce22452010-11-08 19:01:16 +00001410 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001411
danielk1977a7a8e142008-02-13 18:25:27 +00001412 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001413 assert( p->db->mallocFailed );
1414 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001415 }
drhc91b2fd2014-03-01 18:13:23 +00001416 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001417 zP4 = displayP4(pOp, pMem->z, 32);
1418 if( zP4!=pMem->z ){
1419 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
danielk1977a7a8e142008-02-13 18:25:27 +00001420 }else{
1421 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001422 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001423 pMem->enc = SQLITE_UTF8;
1424 }
danielk19770d78bae2008-01-03 07:09:48 +00001425 pMem++;
drheb2e1762004-05-27 01:53:56 +00001426
danielk19770d78bae2008-01-03 07:09:48 +00001427 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001428 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001429 assert( p->db->mallocFailed );
1430 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001431 }
drhc91b2fd2014-03-01 18:13:23 +00001432 pMem->flags = MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001433 pMem->n = 2;
1434 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +00001435 pMem->enc = SQLITE_UTF8;
1436 pMem++;
1437
drhc7379ce2013-10-30 02:28:23 +00001438#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001439 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
1440 assert( p->db->mallocFailed );
1441 return SQLITE_ERROR;
drh52391cb2008-02-14 23:44:13 +00001442 }
drhc91b2fd2014-03-01 18:13:23 +00001443 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001444 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
drh81316f82013-10-29 20:40:47 +00001445 pMem->enc = SQLITE_UTF8;
1446#else
1447 pMem->flags = MEM_Null; /* Comment */
drh81316f82013-10-29 20:40:47 +00001448#endif
danielk19770d78bae2008-01-03 07:09:48 +00001449 }
1450
dan2ce22452010-11-08 19:01:16 +00001451 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001452 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001453 p->rc = SQLITE_OK;
1454 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001455 }
drh826fb5a2004-02-14 23:59:57 +00001456 return rc;
drh9a324642003-09-06 20:12:01 +00001457}
drhb7f91642004-10-31 02:22:47 +00001458#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001459
drh7c4ac0c2007-04-05 11:25:58 +00001460#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001461/*
drh3f7d4e42004-07-24 14:35:58 +00001462** Print the SQL that was used to generate a VDBE program.
1463*/
1464void sqlite3VdbePrintSql(Vdbe *p){
drh84e55a82013-11-13 17:58:23 +00001465 const char *z = 0;
1466 if( p->zSql ){
1467 z = p->zSql;
1468 }else if( p->nOp>=1 ){
1469 const VdbeOp *pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001470 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh84e55a82013-11-13 17:58:23 +00001471 z = pOp->p4.z;
1472 while( sqlite3Isspace(*z) ) z++;
1473 }
drh3f7d4e42004-07-24 14:35:58 +00001474 }
drh84e55a82013-11-13 17:58:23 +00001475 if( z ) printf("SQL: [%s]\n", z);
drh3f7d4e42004-07-24 14:35:58 +00001476}
drh7c4ac0c2007-04-05 11:25:58 +00001477#endif
drh3f7d4e42004-07-24 14:35:58 +00001478
drh602c2372007-03-01 00:29:13 +00001479#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1480/*
1481** Print an IOTRACE message showing SQL content.
1482*/
1483void sqlite3VdbeIOTraceSql(Vdbe *p){
1484 int nOp = p->nOp;
1485 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001486 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001487 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001488 pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001489 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001490 int i, j;
drh00a18e42007-08-13 11:10:34 +00001491 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001492 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001493 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001494 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001495 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001496 if( z[i-1]!=' ' ){
1497 z[j++] = ' ';
1498 }
1499 }else{
1500 z[j++] = z[i];
1501 }
1502 }
1503 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001504 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001505 }
1506}
1507#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1508
drhb2771ce2009-02-20 01:28:59 +00001509/*
drh4800b2e2009-12-08 15:35:22 +00001510** Allocate space from a fixed size buffer and return a pointer to
1511** that space. If insufficient space is available, return NULL.
1512**
1513** The pBuf parameter is the initial value of a pointer which will
1514** receive the new memory. pBuf is normally NULL. If pBuf is not
1515** NULL, it means that memory space has already been allocated and that
1516** this routine should not allocate any new memory. When pBuf is not
1517** NULL simply return pBuf. Only allocate new memory space when pBuf
1518** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001519**
1520** nByte is the number of bytes of space needed.
1521**
drh19875c82009-12-08 19:58:19 +00001522** *ppFrom points to available space and pEnd points to the end of the
1523** available space. When space is allocated, *ppFrom is advanced past
1524** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001525**
1526** *pnByte is a counter of the number of bytes of space that have failed
1527** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001528** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001529*/
drh4800b2e2009-12-08 15:35:22 +00001530static void *allocSpace(
1531 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001532 int nByte, /* Number of bytes to allocate */
1533 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001534 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001535 int *pnByte /* If allocation cannot be made, increment *pnByte */
1536){
drhea598cb2009-04-05 12:22:08 +00001537 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001538 if( pBuf ) return pBuf;
1539 nByte = ROUND8(nByte);
1540 if( &(*ppFrom)[nByte] <= pEnd ){
1541 pBuf = (void*)*ppFrom;
1542 *ppFrom += nByte;
1543 }else{
1544 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001545 }
drh4800b2e2009-12-08 15:35:22 +00001546 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001547}
drh602c2372007-03-01 00:29:13 +00001548
drh3f7d4e42004-07-24 14:35:58 +00001549/*
drh124c0b42011-06-01 18:15:55 +00001550** Rewind the VDBE back to the beginning in preparation for
1551** running it.
drh9a324642003-09-06 20:12:01 +00001552*/
drh124c0b42011-06-01 18:15:55 +00001553void sqlite3VdbeRewind(Vdbe *p){
1554#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1555 int i;
1556#endif
drh9a324642003-09-06 20:12:01 +00001557 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001558 assert( p->magic==VDBE_MAGIC_INIT );
1559
drhc16a03b2004-09-15 13:38:10 +00001560 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001561 */
drhc16a03b2004-09-15 13:38:10 +00001562 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001563
danielk197700e13612008-11-17 19:18:54 +00001564 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001565 p->magic = VDBE_MAGIC_RUN;
1566
drh124c0b42011-06-01 18:15:55 +00001567#ifdef SQLITE_DEBUG
1568 for(i=1; i<p->nMem; i++){
1569 assert( p->aMem[i].db==p->db );
1570 }
1571#endif
1572 p->pc = -1;
1573 p->rc = SQLITE_OK;
1574 p->errorAction = OE_Abort;
1575 p->magic = VDBE_MAGIC_RUN;
1576 p->nChange = 0;
1577 p->cacheCtr = 1;
1578 p->minWriteFileFormat = 255;
1579 p->iStatement = 0;
1580 p->nFkConstraint = 0;
1581#ifdef VDBE_PROFILE
1582 for(i=0; i<p->nOp; i++){
1583 p->aOp[i].cnt = 0;
1584 p->aOp[i].cycles = 0;
1585 }
1586#endif
1587}
1588
1589/*
1590** Prepare a virtual machine for execution for the first time after
1591** creating the virtual machine. This involves things such
1592** as allocating stack space and initializing the program counter.
1593** After the VDBE has be prepped, it can be executed by one or more
1594** calls to sqlite3VdbeExec().
1595**
1596** This function may be called exact once on a each virtual machine.
1597** After this routine is called the VM has been "packaged" and is ready
1598** to run. After this routine is called, futher calls to
1599** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1600** the Vdbe from the Parse object that helped generate it so that the
1601** the Vdbe becomes an independent entity and the Parse object can be
1602** destroyed.
1603**
1604** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1605** to its initial state after it has been run.
1606*/
1607void sqlite3VdbeMakeReady(
1608 Vdbe *p, /* The VDBE */
1609 Parse *pParse /* Parsing context */
1610){
1611 sqlite3 *db; /* The database connection */
1612 int nVar; /* Number of parameters */
1613 int nMem; /* Number of VM memory registers */
1614 int nCursor; /* Number of cursors required */
1615 int nArg; /* Number of arguments in subprograms */
dan1d8cb212011-12-09 13:24:16 +00001616 int nOnce; /* Number of OP_Once instructions */
drh124c0b42011-06-01 18:15:55 +00001617 int n; /* Loop counter */
1618 u8 *zCsr; /* Memory available for allocation */
1619 u8 *zEnd; /* First byte past allocated memory */
1620 int nByte; /* How much extra memory is needed */
1621
1622 assert( p!=0 );
1623 assert( p->nOp>0 );
1624 assert( pParse!=0 );
1625 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +00001626 assert( pParse==p->pParse );
drh124c0b42011-06-01 18:15:55 +00001627 db = p->db;
1628 assert( db->mallocFailed==0 );
1629 nVar = pParse->nVar;
1630 nMem = pParse->nMem;
1631 nCursor = pParse->nTab;
1632 nArg = pParse->nMaxArg;
dan1d8cb212011-12-09 13:24:16 +00001633 nOnce = pParse->nOnce;
drh20e226d2012-01-01 13:58:53 +00001634 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
drh124c0b42011-06-01 18:15:55 +00001635
danielk1977cd3e8f72008-03-25 09:47:35 +00001636 /* For each cursor required, also allocate a memory cell. Memory
1637 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1638 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001639 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001640 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1641 ** stores the blob of memory associated with cursor 1, etc.
1642 **
1643 ** See also: allocateCursor().
1644 */
1645 nMem += nCursor;
1646
danielk19776ab3a2e2009-02-19 14:39:25 +00001647 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001648 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001649 */
drh73d5b8f2013-12-23 19:09:07 +00001650 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1651 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001652
drh124c0b42011-06-01 18:15:55 +00001653 resolveP2Values(p, &nArg);
1654 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1655 if( pParse->explain && nMem<10 ){
1656 nMem = 10;
1657 }
1658 memset(zCsr, 0, zEnd-zCsr);
1659 zCsr += (zCsr - (u8*)0)&7;
1660 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001661 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001662
1663 /* Memory for registers, parameters, cursor, etc, is allocated in two
1664 ** passes. On the first pass, we try to reuse unused space at the
1665 ** end of the opcode array. If we are unable to satisfy all memory
1666 ** requirements by reusing the opcode array tail, then the second
1667 ** pass will fill in the rest using a fresh allocation.
1668 **
1669 ** This two-pass approach that reuses as much memory as possible from
1670 ** the leftover space at the end of the opcode array can significantly
1671 ** reduce the amount of memory held by a prepared statement.
1672 */
1673 do {
1674 nByte = 0;
1675 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1676 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1677 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1678 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1679 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1680 &zCsr, zEnd, &nByte);
drhb8475df2011-12-09 16:21:19 +00001681 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
drh124c0b42011-06-01 18:15:55 +00001682 if( nByte ){
1683 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001684 }
drh124c0b42011-06-01 18:15:55 +00001685 zCsr = p->pFree;
1686 zEnd = &zCsr[nByte];
1687 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001688
drhd2a56232013-01-28 19:00:20 +00001689 p->nCursor = nCursor;
dan1d8cb212011-12-09 13:24:16 +00001690 p->nOnceFlag = nOnce;
drh124c0b42011-06-01 18:15:55 +00001691 if( p->aVar ){
1692 p->nVar = (ynVar)nVar;
1693 for(n=0; n<nVar; n++){
1694 p->aVar[n].flags = MEM_Null;
1695 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001696 }
drh82a48512003-09-06 22:45:20 +00001697 }
drh124c0b42011-06-01 18:15:55 +00001698 if( p->azVar ){
1699 p->nzVar = pParse->nzVar;
1700 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1701 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001702 }
drh124c0b42011-06-01 18:15:55 +00001703 if( p->aMem ){
1704 p->aMem--; /* aMem[] goes from 1..nMem */
1705 p->nMem = nMem; /* not from 0..nMem-1 */
1706 for(n=1; n<=nMem; n++){
drha5750cf2014-02-07 13:20:31 +00001707 p->aMem[n].flags = MEM_Undefined;
drh124c0b42011-06-01 18:15:55 +00001708 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001709 }
drh9a324642003-09-06 20:12:01 +00001710 }
drh124c0b42011-06-01 18:15:55 +00001711 p->explain = pParse->explain;
1712 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001713}
1714
drh9a324642003-09-06 20:12:01 +00001715/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001716** Close a VDBE cursor and release all the resources that cursor
1717** happens to hold.
drh9a324642003-09-06 20:12:01 +00001718*/
drhdfe88ec2008-11-03 20:55:06 +00001719void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001720 if( pCx==0 ){
1721 return;
1722 }
dana20fde62011-07-12 14:28:05 +00001723 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001724 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001725 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001726 /* The pCx->pCursor will be close automatically, if it exists, by
1727 ** the call above. */
1728 }else if( pCx->pCursor ){
1729 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001730 }
drh9eff6162006-06-12 21:59:13 +00001731#ifndef SQLITE_OMIT_VIRTUALTABLE
1732 if( pCx->pVtabCursor ){
1733 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
drh5cc10232013-11-21 01:04:02 +00001734 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
danielk1977be718892006-06-23 08:05:19 +00001735 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001736 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001737 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001738 }
1739#endif
drh9a324642003-09-06 20:12:01 +00001740}
1741
dan65a7cd12009-09-01 12:16:01 +00001742/*
1743** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1744** is used, for example, when a trigger sub-program is halted to restore
1745** control to the main program.
1746*/
dan165921a2009-08-28 18:53:45 +00001747int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1748 Vdbe *v = pFrame->v;
dan1d8cb212011-12-09 13:24:16 +00001749 v->aOnceFlag = pFrame->aOnceFlag;
1750 v->nOnceFlag = pFrame->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00001751 v->aOp = pFrame->aOp;
1752 v->nOp = pFrame->nOp;
1753 v->aMem = pFrame->aMem;
1754 v->nMem = pFrame->nMem;
1755 v->apCsr = pFrame->apCsr;
1756 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001757 v->db->lastRowid = pFrame->lastRowid;
1758 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001759 return pFrame->pc;
1760}
1761
drh9a324642003-09-06 20:12:01 +00001762/*
drh5f82e3c2009-07-06 00:44:08 +00001763** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001764**
1765** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1766** cell array. This is necessary as the memory cell array may contain
1767** pointers to VdbeFrame objects, which may in turn contain pointers to
1768** open cursors.
drh9a324642003-09-06 20:12:01 +00001769*/
drh5f82e3c2009-07-06 00:44:08 +00001770static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001771 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001772 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001773 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1774 sqlite3VdbeFrameRestore(pFrame);
1775 }
1776 p->pFrame = 0;
1777 p->nFrame = 0;
1778
dan523a0872009-08-31 05:23:32 +00001779 if( p->apCsr ){
1780 int i;
1781 for(i=0; i<p->nCursor; i++){
1782 VdbeCursor *pC = p->apCsr[i];
1783 if( pC ){
1784 sqlite3VdbeFreeCursor(p, pC);
1785 p->apCsr[i] = 0;
1786 }
danielk1977be718892006-06-23 08:05:19 +00001787 }
drh9a324642003-09-06 20:12:01 +00001788 }
dan523a0872009-08-31 05:23:32 +00001789 if( p->aMem ){
1790 releaseMemArray(&p->aMem[1], p->nMem);
1791 }
dan27106572010-12-01 08:04:47 +00001792 while( p->pDelFrame ){
1793 VdbeFrame *pDel = p->pDelFrame;
1794 p->pDelFrame = pDel->pParent;
1795 sqlite3VdbeFrameDelete(pDel);
1796 }
dan0c547792013-07-18 17:12:08 +00001797
1798 /* Delete any auxdata allocations made by the VM */
1799 sqlite3VdbeDeleteAuxData(p, -1, 0);
1800 assert( p->pAuxData==0 );
drh9a324642003-09-06 20:12:01 +00001801}
1802
1803/*
drh9a324642003-09-06 20:12:01 +00001804** Clean up the VM after execution.
1805**
1806** This routine will automatically close any cursors, lists, and/or
1807** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001808** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001809*/
drhc890fec2008-08-01 20:10:08 +00001810static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001811 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001812
1813#ifdef SQLITE_DEBUG
1814 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1815 ** Vdbe.aMem[] arrays have already been cleaned up. */
1816 int i;
drhb8475df2011-12-09 16:21:19 +00001817 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1818 if( p->aMem ){
drha5750cf2014-02-07 13:20:31 +00001819 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
drhb8475df2011-12-09 16:21:19 +00001820 }
dan165921a2009-08-28 18:53:45 +00001821#endif
1822
drh633e6d52008-07-28 19:34:53 +00001823 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001824 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001825 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001826}
1827
1828/*
danielk197722322fd2004-05-25 23:35:17 +00001829** Set the number of result columns that will be returned by this SQL
1830** statement. This is now set at compile time, rather than during
1831** execution of the vdbe program so that sqlite3_column_count() can
1832** be called on an SQL statement before sqlite3_step().
1833*/
1834void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001835 Mem *pColName;
1836 int n;
drh633e6d52008-07-28 19:34:53 +00001837 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001838
drhc890fec2008-08-01 20:10:08 +00001839 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001840 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001841 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001842 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001843 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001844 if( p->aColName==0 ) return;
1845 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001846 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001847 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001848 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001849 }
danielk197722322fd2004-05-25 23:35:17 +00001850}
1851
1852/*
danielk19773cf86062004-05-26 10:11:05 +00001853** Set the name of the idx'th column to be returned by the SQL statement.
1854** zName must be a pointer to a nul terminated string.
1855**
1856** This call must be made after a call to sqlite3VdbeSetNumCols().
1857**
danielk197710fb7492008-10-31 10:53:22 +00001858** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1859** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1860** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001861*/
danielk197710fb7492008-10-31 10:53:22 +00001862int sqlite3VdbeSetColName(
1863 Vdbe *p, /* Vdbe being configured */
1864 int idx, /* Index of column zName applies to */
1865 int var, /* One of the COLNAME_* constants */
1866 const char *zName, /* Pointer to buffer containing name */
1867 void (*xDel)(void*) /* Memory management strategy for zName */
1868){
danielk19773cf86062004-05-26 10:11:05 +00001869 int rc;
1870 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001871 assert( idx<p->nResColumn );
1872 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001873 if( p->db->mallocFailed ){
1874 assert( !zName || xDel!=SQLITE_DYNAMIC );
1875 return SQLITE_NOMEM;
1876 }
drh76ff3a02004-09-24 22:32:30 +00001877 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001878 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001879 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001880 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001881 return rc;
1882}
1883
danielk197713adf8a2004-06-03 16:08:41 +00001884/*
1885** A read or write transaction may or may not be active on database handle
1886** db. If a transaction is active, commit it. If there is a
1887** write-transaction spanning more than one database file, this routine
1888** takes care of the master journal trickery.
1889*/
danielk19773e3a84d2008-08-01 17:37:40 +00001890static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001891 int i;
1892 int nTrans = 0; /* Number of databases with an active write-transaction */
1893 int rc = SQLITE_OK;
1894 int needXcommit = 0;
1895
shane36840fd2009-06-26 16:32:13 +00001896#ifdef SQLITE_OMIT_VIRTUALTABLE
1897 /* With this option, sqlite3VtabSync() is defined to be simply
1898 ** SQLITE_OK so p is not used.
1899 */
1900 UNUSED_PARAMETER(p);
1901#endif
1902
danielk19775bd270b2006-07-25 15:14:52 +00001903 /* Before doing anything else, call the xSync() callback for any
1904 ** virtual module tables written in this transaction. This has to
1905 ** be done before determining whether a master journal file is
1906 ** required, as an xSync() callback may add an attached database
1907 ** to the transaction.
1908 */
dan016f7812013-08-21 17:35:48 +00001909 rc = sqlite3VtabSync(db, p);
danielk19775bd270b2006-07-25 15:14:52 +00001910
1911 /* This loop determines (a) if the commit hook should be invoked and
1912 ** (b) how many database files have open write transactions, not
1913 ** including the temp database. (b) is important because if more than
1914 ** one database file has an open write transaction, a master journal
1915 ** file is required for an atomic commit.
1916 */
drhabfb62f2010-07-30 11:20:35 +00001917 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001918 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001919 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001920 needXcommit = 1;
1921 if( i!=1 ) nTrans++;
dan6b9bb592012-10-05 19:43:02 +00001922 sqlite3BtreeEnter(pBt);
drhabfb62f2010-07-30 11:20:35 +00001923 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
dan6b9bb592012-10-05 19:43:02 +00001924 sqlite3BtreeLeave(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001925 }
1926 }
drhabfb62f2010-07-30 11:20:35 +00001927 if( rc!=SQLITE_OK ){
1928 return rc;
1929 }
danielk197713adf8a2004-06-03 16:08:41 +00001930
1931 /* If there are any write-transactions at all, invoke the commit hook */
1932 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001933 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001934 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00001935 return SQLITE_CONSTRAINT_COMMITHOOK;
danielk197713adf8a2004-06-03 16:08:41 +00001936 }
1937 }
1938
danielk197740b38dc2004-06-26 08:38:24 +00001939 /* The simple case - no more than one database file (not counting the
1940 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001941 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001942 **
danielk197740b38dc2004-06-26 08:38:24 +00001943 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001944 ** string, it means the main database is :memory: or a temp file. In
1945 ** that case we do not support atomic multi-file commits, so use the
1946 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001947 */
drhea678832008-12-10 19:26:22 +00001948 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1949 || nTrans<=1
1950 ){
danielk197704103022009-02-03 16:51:24 +00001951 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001952 Btree *pBt = db->aDb[i].pBt;
1953 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001954 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001955 }
1956 }
1957
drh80e35f42007-03-30 14:06:34 +00001958 /* Do the commit only if all databases successfully complete phase 1.
1959 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1960 ** IO error while deleting or truncating a journal file. It is unlikely,
1961 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001962 */
1963 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1964 Btree *pBt = db->aDb[i].pBt;
1965 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001966 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001967 }
danielk1977979f38e2007-03-27 16:19:51 +00001968 }
1969 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001970 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001971 }
1972 }
1973
1974 /* The complex case - There is a multi-file write-transaction active.
1975 ** This requires a master journal file to ensure the transaction is
1976 ** committed atomicly.
1977 */
danielk197744ee5bf2005-05-27 09:41:12 +00001978#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001979 else{
danielk1977b4b47412007-08-17 15:53:36 +00001980 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001981 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001982 char *zMaster = 0; /* File-name for the master journal */
1983 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001984 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001985 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001986 int res;
drhf5808602011-12-16 00:33:04 +00001987 int retryCount = 0;
drh5c531a42011-12-16 01:21:31 +00001988 int nMainFile;
danielk197713adf8a2004-06-03 16:08:41 +00001989
1990 /* Select a master journal file name */
drh5c531a42011-12-16 01:21:31 +00001991 nMainFile = sqlite3Strlen30(zMainFile);
drh52bcde02012-01-03 14:50:45 +00001992 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
drh5c531a42011-12-16 01:21:31 +00001993 if( zMaster==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00001994 do {
drhdc5ea5c2008-12-10 17:19:59 +00001995 u32 iRandom;
drh84968c02011-12-16 15:11:39 +00001996 if( retryCount ){
1997 if( retryCount>100 ){
1998 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
1999 sqlite3OsDelete(pVfs, zMaster, 0);
2000 break;
2001 }else if( retryCount==1 ){
2002 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2003 }
danielk197713adf8a2004-06-03 16:08:41 +00002004 }
drh84968c02011-12-16 15:11:39 +00002005 retryCount++;
danielk197713adf8a2004-06-03 16:08:41 +00002006 sqlite3_randomness(sizeof(iRandom), &iRandom);
drh5c531a42011-12-16 01:21:31 +00002007 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
drhf5808602011-12-16 00:33:04 +00002008 (iRandom>>8)&0xffffff, iRandom&0xff);
drhf5808602011-12-16 00:33:04 +00002009 /* The antipenultimate character of the master journal name must
2010 ** be "9" to avoid name collisions when using 8+3 filenames. */
drh5c531a42011-12-16 01:21:31 +00002011 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
drh81cc5162011-05-17 20:36:21 +00002012 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00002013 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2014 }while( rc==SQLITE_OK && res );
2015 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00002016 /* Open the master journal. */
2017 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2018 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2019 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2020 );
2021 }
danielk197713adf8a2004-06-03 16:08:41 +00002022 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002023 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002024 return rc;
2025 }
2026
2027 /* Write the name of each database file in the transaction into the new
2028 ** master journal file. If an error occurs at this point close
2029 ** and delete the master journal file. All the individual journal files
2030 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00002031 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00002032 */
danielk19771e536952007-08-16 10:09:01 +00002033 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002034 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002035 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00002036 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00002037 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00002038 continue; /* Ignore TEMP and :memory: databases */
2039 }
drh8c96a6e2010-08-31 01:09:15 +00002040 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00002041 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2042 needSync = 1;
2043 }
drhea678832008-12-10 19:26:22 +00002044 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2045 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00002046 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00002047 sqlite3OsCloseFree(pMaster);
2048 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002049 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002050 return rc;
2051 }
2052 }
2053 }
2054
danielk19779663b8f2007-08-24 11:52:28 +00002055 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2056 ** flag is set this is not required.
2057 */
danielk1977bea2a942009-01-20 17:06:27 +00002058 if( needSync
2059 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2060 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2061 ){
danielk1977fee2d252007-08-18 10:59:19 +00002062 sqlite3OsCloseFree(pMaster);
2063 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002064 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00002065 return rc;
2066 }
drhc9e06862004-06-09 20:03:08 +00002067
danielk197713adf8a2004-06-03 16:08:41 +00002068 /* Sync all the db files involved in the transaction. The same call
2069 ** sets the master journal pointer in each individual journal. If
2070 ** an error occurs here, do not delete the master journal file.
2071 **
drh80e35f42007-03-30 14:06:34 +00002072 ** If the error occurs during the first call to
2073 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2074 ** master journal file will be orphaned. But we cannot delete it,
2075 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00002076 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00002077 */
danielk19775bd270b2006-07-25 15:14:52 +00002078 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002079 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002080 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002081 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002082 }
2083 }
danielk1977fee2d252007-08-18 10:59:19 +00002084 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00002085 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00002086 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002087 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00002088 return rc;
2089 }
danielk197713adf8a2004-06-03 16:08:41 +00002090
danielk1977962398d2004-06-14 09:35:16 +00002091 /* Delete the master journal file. This commits the transaction. After
2092 ** doing this the directory is synced again before any individual
2093 ** transaction files are deleted.
2094 */
danielk1977fee2d252007-08-18 10:59:19 +00002095 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00002096 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00002097 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00002098 if( rc ){
2099 return rc;
2100 }
danielk197713adf8a2004-06-03 16:08:41 +00002101
2102 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00002103 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2104 ** deleting or truncating journals. If something goes wrong while
2105 ** this is happening we don't really care. The integrity of the
2106 ** transaction is already guaranteed, but some stray 'cold' journals
2107 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00002108 */
danielk1977979f38e2007-03-27 16:19:51 +00002109 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002110 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00002111 for(i=0; i<db->nDb; i++){
2112 Btree *pBt = db->aDb[i].pBt;
2113 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002114 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00002115 }
2116 }
danielk19772d1d86f2008-06-20 14:59:51 +00002117 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00002118 enable_simulated_io_errors();
2119
danielk1977f9e7dda2006-06-16 16:08:53 +00002120 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002121 }
danielk197744ee5bf2005-05-27 09:41:12 +00002122#endif
danielk1977026d2702004-06-14 13:14:59 +00002123
drh2ac3ee92004-06-07 16:27:46 +00002124 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002125}
2126
danielk19771d850a72004-05-31 08:26:49 +00002127/*
drh4f7d3a52013-06-27 23:54:02 +00002128** This routine checks that the sqlite3.nVdbeActive count variable
danielk19771d850a72004-05-31 08:26:49 +00002129** matches the number of vdbe's in the list sqlite3.pVdbe that are
2130** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00002131** This is an internal self-check only - it is not an essential processing
2132** step.
danielk19771d850a72004-05-31 08:26:49 +00002133**
2134** This is a no-op if NDEBUG is defined.
2135*/
2136#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00002137static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00002138 Vdbe *p;
2139 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00002140 int nWrite = 0;
drh4f7d3a52013-06-27 23:54:02 +00002141 int nRead = 0;
danielk19771d850a72004-05-31 08:26:49 +00002142 p = db->pVdbe;
2143 while( p ){
dan857745c2014-07-19 17:57:10 +00002144 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
danielk19771d850a72004-05-31 08:26:49 +00002145 cnt++;
drhad4a4b82008-11-05 16:37:34 +00002146 if( p->readOnly==0 ) nWrite++;
drh1713afb2013-06-28 01:24:57 +00002147 if( p->bIsReader ) nRead++;
danielk19771d850a72004-05-31 08:26:49 +00002148 }
2149 p = p->pNext;
2150 }
drh4f7d3a52013-06-27 23:54:02 +00002151 assert( cnt==db->nVdbeActive );
2152 assert( nWrite==db->nVdbeWrite );
2153 assert( nRead==db->nVdbeRead );
danielk19771d850a72004-05-31 08:26:49 +00002154}
2155#else
2156#define checkActiveVdbeCnt(x)
2157#endif
2158
danielk19773cf86062004-05-26 10:11:05 +00002159/*
danielk1977bd434552009-03-18 10:33:00 +00002160** If the Vdbe passed as the first argument opened a statement-transaction,
2161** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2162** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2163** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
drhf7b54962013-05-28 12:11:54 +00002164** statement transaction is committed.
danielk1977bd434552009-03-18 10:33:00 +00002165**
2166** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2167** Otherwise SQLITE_OK.
2168*/
2169int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002170 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002171 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002172
danielk1977e4948172009-07-17 17:25:43 +00002173 /* If p->iStatement is greater than zero, then this Vdbe opened a
2174 ** statement transaction that should be closed here. The only exception
mistachkin48864df2013-03-21 21:20:32 +00002175 ** is that an IO error may have occurred, causing an emergency rollback.
danielk1977e4948172009-07-17 17:25:43 +00002176 ** In this case (db->nStatement==0), and there is nothing to do.
2177 */
2178 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002179 int i;
2180 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002181
2182 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2183 assert( db->nStatement>0 );
2184 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2185
2186 for(i=0; i<db->nDb; i++){
2187 int rc2 = SQLITE_OK;
2188 Btree *pBt = db->aDb[i].pBt;
2189 if( pBt ){
2190 if( eOp==SAVEPOINT_ROLLBACK ){
2191 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2192 }
2193 if( rc2==SQLITE_OK ){
2194 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2195 }
2196 if( rc==SQLITE_OK ){
2197 rc = rc2;
2198 }
2199 }
2200 }
2201 db->nStatement--;
2202 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002203
dana311b802011-04-26 19:21:34 +00002204 if( rc==SQLITE_OK ){
2205 if( eOp==SAVEPOINT_ROLLBACK ){
2206 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2207 }
2208 if( rc==SQLITE_OK ){
2209 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2210 }
2211 }
2212
dan1da40a32009-09-19 17:00:31 +00002213 /* If the statement transaction is being rolled back, also restore the
2214 ** database handles deferred constraint counter to the value it had when
2215 ** the statement transaction was opened. */
2216 if( eOp==SAVEPOINT_ROLLBACK ){
2217 db->nDeferredCons = p->nStmtDefCons;
drh648e2642013-07-11 15:03:32 +00002218 db->nDeferredImmCons = p->nStmtDefImmCons;
dan1da40a32009-09-19 17:00:31 +00002219 }
danielk1977bd434552009-03-18 10:33:00 +00002220 }
2221 return rc;
2222}
2223
2224/*
dan1da40a32009-09-19 17:00:31 +00002225** This function is called when a transaction opened by the database
2226** handle associated with the VM passed as an argument is about to be
2227** committed. If there are outstanding deferred foreign key constraint
2228** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2229**
2230** If there are outstanding FK violations and this function returns
drhd91c1a12013-02-09 13:58:25 +00002231** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2232** and write an error message to it. Then return SQLITE_ERROR.
dan1da40a32009-09-19 17:00:31 +00002233*/
2234#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002235int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002236 sqlite3 *db = p->db;
drh648e2642013-07-11 15:03:32 +00002237 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2238 || (!deferred && p->nFkConstraint>0)
2239 ){
drhd91c1a12013-02-09 13:58:25 +00002240 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan32b09f22009-09-23 17:29:59 +00002241 p->errorAction = OE_Abort;
drhf9c8ce32013-11-05 13:33:55 +00002242 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
dan1da40a32009-09-19 17:00:31 +00002243 return SQLITE_ERROR;
2244 }
2245 return SQLITE_OK;
2246}
2247#endif
2248
2249/*
drh92f02c32004-09-02 14:57:08 +00002250** This routine is called the when a VDBE tries to halt. If the VDBE
2251** has made changes and is in autocommit mode, then commit those
2252** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002253**
drh92f02c32004-09-02 14:57:08 +00002254** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002255** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2256** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002257**
2258** Return an error code. If the commit could not complete because of
2259** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2260** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002261*/
drhff0587c2007-08-29 17:43:19 +00002262int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002263 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002264 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002265
2266 /* This function contains the logic that determines if a statement or
2267 ** transaction will be committed or rolled back as a result of the
2268 ** execution of this virtual machine.
2269 **
drh71b890a2007-10-03 15:30:52 +00002270 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002271 **
drh71b890a2007-10-03 15:30:52 +00002272 ** SQLITE_NOMEM
2273 ** SQLITE_IOERR
2274 ** SQLITE_FULL
2275 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002276 **
drh71b890a2007-10-03 15:30:52 +00002277 ** Then the internal cache might have been left in an inconsistent
2278 ** state. We need to rollback the statement transaction, if there is
2279 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002280 */
drh9a324642003-09-06 20:12:01 +00002281
drh17435752007-08-16 04:30:38 +00002282 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002283 p->rc = SQLITE_NOMEM;
2284 }
drh6e856bc2011-12-09 18:06:44 +00002285 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
drh5f82e3c2009-07-06 00:44:08 +00002286 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002287 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002288 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002289 }
danielk19771d850a72004-05-31 08:26:49 +00002290 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002291
danc0537fe2013-06-28 19:41:43 +00002292 /* No commit or rollback needed if the program never started or if the
2293 ** SQL statement does not read or write a database file. */
2294 if( p->pc>=0 && p->bIsReader ){
drhaac2f552006-09-23 21:44:23 +00002295 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002296 int eStatementOp = 0;
2297 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002298
2299 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002300 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002301
drh71b890a2007-10-03 15:30:52 +00002302 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002303 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002304 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002305 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002306 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002307 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002308 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2309 ** no rollback is necessary. Otherwise, at least a savepoint
2310 ** transaction must be rolled back to restore the database to a
2311 ** consistent state.
2312 **
2313 ** Even if the statement is read-only, it is important to perform
2314 ** a statement or transaction rollback operation. If the error
mistachkin48864df2013-03-21 21:20:32 +00002315 ** occurred while writing to the journal, sub-journal or database
dan5653e4d2010-08-12 11:25:47 +00002316 ** file as part of an effort to free up cache space (see function
2317 ** pagerStress() in pager.c), the rollback is required to restore
2318 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002319 */
drhad4a4b82008-11-05 16:37:34 +00002320 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002321 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002322 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002323 }else{
2324 /* We are forced to roll back the active transaction. Before doing
2325 ** so, abort any other statements this handle currently has active.
2326 */
drh21021a52012-02-13 17:01:51 +00002327 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002328 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002329 db->autoCommit = 1;
2330 }
danielk1977261919c2005-12-06 12:52:59 +00002331 }
2332 }
dan32b09f22009-09-23 17:29:59 +00002333
2334 /* Check for immediate foreign key violations. */
2335 if( p->rc==SQLITE_OK ){
2336 sqlite3VdbeCheckFk(p, 0);
2337 }
danielk197707cb5602006-01-20 10:55:05 +00002338
danielk1977bd434552009-03-18 10:33:00 +00002339 /* If the auto-commit flag is set and this is the only active writer
2340 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002341 **
2342 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002343 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002344 */
danielk1977093e0f62008-11-13 18:00:14 +00002345 if( !sqlite3VtabInSync(db)
2346 && db->autoCommit
drh4f7d3a52013-06-27 23:54:02 +00002347 && db->nVdbeWrite==(p->readOnly==0)
danielk1977093e0f62008-11-13 18:00:14 +00002348 ){
danielk197707cb5602006-01-20 10:55:05 +00002349 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002350 rc = sqlite3VdbeCheckFk(p, 1);
2351 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002352 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002353 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002354 return SQLITE_ERROR;
2355 }
drhd91c1a12013-02-09 13:58:25 +00002356 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan19611b12011-01-24 16:00:58 +00002357 }else{
2358 /* The auto-commit flag is true, the vdbe program was successful
2359 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2360 ** key constraints to hold up the transaction. This means a commit
2361 ** is required. */
2362 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002363 }
dan19611b12011-01-24 16:00:58 +00002364 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002365 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002366 return SQLITE_BUSY;
2367 }else if( rc!=SQLITE_OK ){
2368 p->rc = rc;
drh0f198a72012-02-13 16:43:16 +00002369 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002370 }else{
dan1da40a32009-09-19 17:00:31 +00002371 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00002372 db->nDeferredImmCons = 0;
2373 db->flags &= ~SQLITE_DeferFKs;
danielk197707cb5602006-01-20 10:55:05 +00002374 sqlite3CommitInternalChanges(db);
2375 }
2376 }else{
drh0f198a72012-02-13 16:43:16 +00002377 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002378 }
danielk1977bd434552009-03-18 10:33:00 +00002379 db->nStatement = 0;
2380 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002381 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002382 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002383 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002384 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002385 }else{
drh21021a52012-02-13 17:01:51 +00002386 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002387 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002388 db->autoCommit = 1;
2389 }
danielk19771d850a72004-05-31 08:26:49 +00002390 }
danielk197707cb5602006-01-20 10:55:05 +00002391
danielk1977bd434552009-03-18 10:33:00 +00002392 /* If eStatementOp is non-zero, then a statement transaction needs to
2393 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2394 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002395 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2396 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002397 */
danielk1977bd434552009-03-18 10:33:00 +00002398 if( eStatementOp ){
2399 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002400 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002401 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002402 p->rc = rc;
2403 sqlite3DbFree(db, p->zErrMsg);
2404 p->zErrMsg = 0;
2405 }
drh21021a52012-02-13 17:01:51 +00002406 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
dan40ad9d22010-06-03 09:17:38 +00002407 sqlite3CloseSavepoints(db);
2408 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002409 }
danielk197777d83ba2004-05-31 10:08:14 +00002410 }
danielk197707cb5602006-01-20 10:55:05 +00002411
danielk1977bd434552009-03-18 10:33:00 +00002412 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2413 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002414 */
drh6be240e2009-07-14 02:33:02 +00002415 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002416 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002417 sqlite3VdbeSetChanges(db, p->nChange);
2418 }else{
2419 sqlite3VdbeSetChanges(db, 0);
2420 }
2421 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002422 }
drhff0587c2007-08-29 17:43:19 +00002423
2424 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002425 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002426 }
danielk19771d850a72004-05-31 08:26:49 +00002427
danielk197765fd59f2006-06-24 11:51:33 +00002428 /* We have successfully halted and closed the VM. Record this fact. */
2429 if( p->pc>=0 ){
drh4f7d3a52013-06-27 23:54:02 +00002430 db->nVdbeActive--;
2431 if( !p->readOnly ) db->nVdbeWrite--;
drh1713afb2013-06-28 01:24:57 +00002432 if( p->bIsReader ) db->nVdbeRead--;
drh4f7d3a52013-06-27 23:54:02 +00002433 assert( db->nVdbeActive>=db->nVdbeRead );
2434 assert( db->nVdbeRead>=db->nVdbeWrite );
2435 assert( db->nVdbeWrite>=0 );
drh9a324642003-09-06 20:12:01 +00002436 }
drh92f02c32004-09-02 14:57:08 +00002437 p->magic = VDBE_MAGIC_HALT;
2438 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002439 if( p->db->mallocFailed ){
2440 p->rc = SQLITE_NOMEM;
2441 }
danielk19771d850a72004-05-31 08:26:49 +00002442
danielk1977404ca072009-03-16 13:19:36 +00002443 /* If the auto-commit flag is set to true, then any locks that were held
2444 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2445 ** to invoke any required unlock-notify callbacks.
2446 */
2447 if( db->autoCommit ){
2448 sqlite3ConnectionUnlocked(db);
2449 }
2450
drh4f7d3a52013-06-27 23:54:02 +00002451 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002452 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002453}
drh4cf7c7f2007-08-28 23:28:07 +00002454
drh92f02c32004-09-02 14:57:08 +00002455
2456/*
drh3c23a882007-01-09 14:01:13 +00002457** Each VDBE holds the result of the most recent sqlite3_step() call
2458** in p->rc. This routine sets that result back to SQLITE_OK.
2459*/
2460void sqlite3VdbeResetStepResult(Vdbe *p){
2461 p->rc = SQLITE_OK;
2462}
2463
2464/*
dan029ead62011-10-27 15:19:58 +00002465** Copy the error code and error message belonging to the VDBE passed
2466** as the first argument to its database handle (so that they will be
2467** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2468**
2469** This function does not clear the VDBE error code or message, just
2470** copies them to the database handle.
2471*/
2472int sqlite3VdbeTransferError(Vdbe *p){
2473 sqlite3 *db = p->db;
2474 int rc = p->rc;
2475 if( p->zErrMsg ){
drh81bdd6d2011-10-29 01:33:24 +00002476 u8 mallocFailed = db->mallocFailed;
dan029ead62011-10-27 15:19:58 +00002477 sqlite3BeginBenignMalloc();
drha3cc0072013-12-13 16:23:55 +00002478 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
dan029ead62011-10-27 15:19:58 +00002479 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2480 sqlite3EndBenignMalloc();
drh81bdd6d2011-10-29 01:33:24 +00002481 db->mallocFailed = mallocFailed;
dan029ead62011-10-27 15:19:58 +00002482 db->errCode = rc;
2483 }else{
2484 sqlite3Error(db, rc, 0);
2485 }
2486 return rc;
2487}
2488
danac455932012-11-26 19:50:41 +00002489#ifdef SQLITE_ENABLE_SQLLOG
2490/*
2491** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2492** invoke it.
2493*/
2494static void vdbeInvokeSqllog(Vdbe *v){
2495 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2496 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2497 assert( v->db->init.busy==0 );
2498 if( zExpanded ){
2499 sqlite3GlobalConfig.xSqllog(
2500 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2501 );
2502 sqlite3DbFree(v->db, zExpanded);
2503 }
2504 }
2505}
2506#else
2507# define vdbeInvokeSqllog(x)
2508#endif
2509
dan029ead62011-10-27 15:19:58 +00002510/*
drh92f02c32004-09-02 14:57:08 +00002511** Clean up a VDBE after execution but do not delete the VDBE just yet.
2512** Write any error messages into *pzErrMsg. Return the result code.
2513**
2514** After this routine is run, the VDBE should be ready to be executed
2515** again.
2516**
2517** To look at it another way, this routine resets the state of the
2518** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2519** VDBE_MAGIC_INIT.
2520*/
drhc890fec2008-08-01 20:10:08 +00002521int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002522 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002523 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002524
2525 /* If the VM did not run to completion or if it encountered an
2526 ** error, then it might not have been halted properly. So halt
2527 ** it now.
2528 */
2529 sqlite3VdbeHalt(p);
2530
drhfb7e7652005-01-24 00:28:42 +00002531 /* If the VDBE has be run even partially, then transfer the error code
2532 ** and error message from the VDBE into the main database structure. But
2533 ** if the VDBE has just been set to run but has not actually executed any
2534 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002535 */
drhfb7e7652005-01-24 00:28:42 +00002536 if( p->pc>=0 ){
danac455932012-11-26 19:50:41 +00002537 vdbeInvokeSqllog(p);
dan029ead62011-10-27 15:19:58 +00002538 sqlite3VdbeTransferError(p);
2539 sqlite3DbFree(db, p->zErrMsg);
2540 p->zErrMsg = 0;
drh4611d922010-02-25 14:47:01 +00002541 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002542 }else if( p->rc && p->expired ){
2543 /* The expired flag was set on the VDBE before the first call
2544 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2545 ** called), set the database error in this case as well.
2546 */
drha3cc0072013-12-13 16:23:55 +00002547 sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002548 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002549 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002550 }
2551
2552 /* Reclaim all memory used by the VDBE
2553 */
drhc890fec2008-08-01 20:10:08 +00002554 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002555
2556 /* Save profiling information from this VDBE run.
2557 */
drh9a324642003-09-06 20:12:01 +00002558#ifdef VDBE_PROFILE
2559 {
2560 FILE *out = fopen("vdbe_profile.out", "a");
2561 if( out ){
2562 int i;
2563 fprintf(out, "---- ");
2564 for(i=0; i<p->nOp; i++){
2565 fprintf(out, "%02x", p->aOp[i].opcode);
2566 }
2567 fprintf(out, "\n");
drh2926f962014-02-17 01:13:28 +00002568 if( p->zSql ){
2569 char c, pc = 0;
2570 fprintf(out, "-- ");
2571 for(i=0; (c = p->zSql[i])!=0; i++){
2572 if( pc=='\n' ) fprintf(out, "-- ");
2573 putc(c, out);
2574 pc = c;
2575 }
2576 if( pc!='\n' ) fprintf(out, "\n");
2577 }
drh9a324642003-09-06 20:12:01 +00002578 for(i=0; i<p->nOp; i++){
drh15ab9412014-02-24 14:24:01 +00002579 char zHdr[100];
2580 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
drh9a324642003-09-06 20:12:01 +00002581 p->aOp[i].cnt,
2582 p->aOp[i].cycles,
2583 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2584 );
drh15ab9412014-02-24 14:24:01 +00002585 fprintf(out, "%s", zHdr);
danielk19774adee202004-05-08 08:23:19 +00002586 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002587 }
2588 fclose(out);
2589 }
2590 }
2591#endif
drh7fa20922013-09-17 23:36:33 +00002592 p->iCurrentTime = 0;
drh9a324642003-09-06 20:12:01 +00002593 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002594 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002595}
drh92f02c32004-09-02 14:57:08 +00002596
drh9a324642003-09-06 20:12:01 +00002597/*
2598** Clean up and delete a VDBE after execution. Return an integer which is
2599** the result code. Write any error message text into *pzErrMsg.
2600*/
danielk19779e6db7d2004-06-21 08:18:51 +00002601int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002602 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002603 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002604 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002605 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002606 }
danielk19774adee202004-05-08 08:23:19 +00002607 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002608 return rc;
2609}
2610
2611/*
dan0c547792013-07-18 17:12:08 +00002612** If parameter iOp is less than zero, then invoke the destructor for
2613** all auxiliary data pointers currently cached by the VM passed as
2614** the first argument.
2615**
2616** Or, if iOp is greater than or equal to zero, then the destructor is
2617** only invoked for those auxiliary data pointers created by the user
2618** function invoked by the OP_Function opcode at instruction iOp of
2619** VM pVdbe, and only then if:
2620**
2621** * the associated function parameter is the 32nd or later (counting
2622** from left to right), or
2623**
2624** * the corresponding bit in argument mask is clear (where the first
2625** function parameter corrsponds to bit 0 etc.).
drhf92c7ff2004-06-19 15:40:23 +00002626*/
dan0c547792013-07-18 17:12:08 +00002627void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2628 AuxData **pp = &pVdbe->pAuxData;
2629 while( *pp ){
2630 AuxData *pAux = *pp;
2631 if( (iOp<0)
drh693e6712014-01-24 22:58:00 +00002632 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
dan0c547792013-07-18 17:12:08 +00002633 ){
drh693e6712014-01-24 22:58:00 +00002634 testcase( pAux->iArg==31 );
drhf92c7ff2004-06-19 15:40:23 +00002635 if( pAux->xDelete ){
2636 pAux->xDelete(pAux->pAux);
2637 }
dan0c547792013-07-18 17:12:08 +00002638 *pp = pAux->pNext;
2639 sqlite3DbFree(pVdbe->db, pAux);
2640 }else{
2641 pp= &pAux->pNext;
drhf92c7ff2004-06-19 15:40:23 +00002642 }
2643 }
2644}
2645
2646/*
drhcb103b92012-10-26 00:11:23 +00002647** Free all memory associated with the Vdbe passed as the second argument,
2648** except for object itself, which is preserved.
2649**
dand46def72010-07-24 11:28:28 +00002650** The difference between this function and sqlite3VdbeDelete() is that
2651** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
drhcb103b92012-10-26 00:11:23 +00002652** the database connection and frees the object itself.
dand46def72010-07-24 11:28:28 +00002653*/
drhcb103b92012-10-26 00:11:23 +00002654void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002655 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002656 int i;
dand46def72010-07-24 11:28:28 +00002657 assert( p->db==0 || p->db==db );
2658 releaseMemArray(p->aVar, p->nVar);
2659 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002660 for(pSub=p->pProgram; pSub; pSub=pNext){
2661 pNext = pSub->pNext;
2662 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2663 sqlite3DbFree(db, pSub);
2664 }
drh124c0b42011-06-01 18:15:55 +00002665 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002666 vdbeFreeOpArray(db, p->aOp, p->nOp);
dand46def72010-07-24 11:28:28 +00002667 sqlite3DbFree(db, p->aColName);
2668 sqlite3DbFree(db, p->zSql);
2669 sqlite3DbFree(db, p->pFree);
drh678a9aa2011-12-10 15:55:01 +00002670#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
drh25fe97a2013-01-23 18:44:22 +00002671 sqlite3DbFree(db, p->zExplain);
drh678a9aa2011-12-10 15:55:01 +00002672 sqlite3DbFree(db, p->pExplain);
drh7e02e5e2011-12-06 19:44:51 +00002673#endif
dand46def72010-07-24 11:28:28 +00002674}
2675
2676/*
drh9a324642003-09-06 20:12:01 +00002677** Delete an entire VDBE.
2678*/
danielk19774adee202004-05-08 08:23:19 +00002679void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002680 sqlite3 *db;
2681
drhfa3be902009-07-07 02:44:07 +00002682 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002683 db = p->db;
drh4245c402012-06-02 14:32:21 +00002684 assert( sqlite3_mutex_held(db->mutex) );
drhcb103b92012-10-26 00:11:23 +00002685 sqlite3VdbeClearObject(db, p);
drh9a324642003-09-06 20:12:01 +00002686 if( p->pPrev ){
2687 p->pPrev->pNext = p->pNext;
2688 }else{
drh633e6d52008-07-28 19:34:53 +00002689 assert( db->pVdbe==p );
2690 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002691 }
2692 if( p->pNext ){
2693 p->pNext->pPrev = p->pPrev;
2694 }
drh9a324642003-09-06 20:12:01 +00002695 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002696 p->db = 0;
drhcb103b92012-10-26 00:11:23 +00002697 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002698}
drha11846b2004-01-07 18:52:56 +00002699
2700/*
drh9a65f2c2009-06-22 19:05:40 +00002701** Make sure the cursor p is ready to read or write the row to which it
2702** was last positioned. Return an error code if an OOM fault or I/O error
2703** prevents us from positioning the cursor to its correct position.
2704**
drha11846b2004-01-07 18:52:56 +00002705** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002706** MoveTo now. If no move is pending, check to see if the row has been
2707** deleted out from under the cursor and if it has, mark the row as
2708** a NULL row.
2709**
2710** If the cursor is already pointing to the correct row and that row has
2711** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002712*/
drhdfe88ec2008-11-03 20:55:06 +00002713int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002714 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002715 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002716#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002717 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002718#endif
drhf0863fe2005-06-12 21:35:51 +00002719 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002720 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002721 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002722 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002723 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2724 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002725#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002726 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002727#endif
drha11846b2004-01-07 18:52:56 +00002728 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002729 p->cacheStatus = CACHE_STALE;
drh399af1d2013-11-20 17:25:55 +00002730 }else if( p->pCursor ){
drha3460582008-07-11 21:02:53 +00002731 int hasMoved;
2732 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2733 if( rc ) return rc;
2734 if( hasMoved ){
2735 p->cacheStatus = CACHE_STALE;
drh86dd3712014-03-25 11:00:21 +00002736 if( hasMoved==2 ) p->nullRow = 1;
drha3460582008-07-11 21:02:53 +00002737 }
drha11846b2004-01-07 18:52:56 +00002738 }
2739 return SQLITE_OK;
2740}
danielk19774adee202004-05-08 08:23:19 +00002741
drhab9f7f12004-05-08 10:56:11 +00002742/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002743** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002744**
danielk1977cfcdaef2004-05-12 07:33:33 +00002745** sqlite3VdbeSerialType()
2746** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002747** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002748** sqlite3VdbeSerialPut()
2749** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002750**
2751** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002752** data and index records. Each serialized value consists of a
2753** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2754** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002755**
danielk1977cfcdaef2004-05-12 07:33:33 +00002756** In an SQLite index record, the serial type is stored directly before
2757** the blob of data that it corresponds to. In a table record, all serial
2758** types are stored at the start of the record, and the blobs of data at
2759** the end. Hence these functions allow the caller to handle the
mistachkin48864df2013-03-21 21:20:32 +00002760** serial-type and data blob separately.
danielk1977cfcdaef2004-05-12 07:33:33 +00002761**
2762** The following table describes the various storage classes for data:
2763**
2764** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002765** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002766** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002767** 1 1 signed integer
2768** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002769** 3 3 signed integer
2770** 4 4 signed integer
2771** 5 6 signed integer
2772** 6 8 signed integer
2773** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002774** 8 0 Integer constant 0
2775** 9 0 Integer constant 1
2776** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002777** N>=12 and even (N-12)/2 BLOB
2778** N>=13 and odd (N-13)/2 text
2779**
drh35a59652006-01-02 18:24:40 +00002780** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2781** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002782*/
2783
2784/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002785** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002786*/
drhd946db02005-12-29 19:23:06 +00002787u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002788 int flags = pMem->flags;
drheac5bd72014-07-25 21:35:39 +00002789 u32 n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002790
2791 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002792 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002793 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002794 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002795 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002796# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002797 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002798 u64 u;
drhcfd654b2011-03-05 13:54:15 +00002799 if( i<0 ){
2800 if( i<(-MAX_6BYTE) ) return 6;
2801 /* Previous test prevents: u = -(-9223372036854775808) */
2802 u = -i;
2803 }else{
2804 u = i;
2805 }
drh56690b32012-09-17 15:36:31 +00002806 if( u<=127 ){
2807 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2808 }
drh5742b632005-01-26 17:47:02 +00002809 if( u<=32767 ) return 2;
2810 if( u<=8388607 ) return 3;
2811 if( u<=2147483647 ) return 4;
2812 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002813 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002814 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002815 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002816 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002817 }
danielk1977e4359752008-11-03 09:39:45 +00002818 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drheac5bd72014-07-25 21:35:39 +00002819 assert( pMem->n>=0 );
2820 n = (u32)pMem->n;
drhfdf972a2007-05-02 13:30:27 +00002821 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002822 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002823 }
drhfdf972a2007-05-02 13:30:27 +00002824 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002825}
2826
2827/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002828** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002829*/
drh35cd6432009-06-05 14:17:21 +00002830u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002831 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002832 return (serial_type-12)/2;
2833 }else{
drh57196282004-10-06 15:41:16 +00002834 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002835 return aSize[serial_type];
2836 }
danielk1977192ac1d2004-05-10 07:17:30 +00002837}
2838
2839/*
drh110daac2007-05-04 11:59:31 +00002840** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002841** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002842** upper 4 bytes. Return the result.
2843**
drh7a4f5022007-05-23 07:20:08 +00002844** For most architectures, this is a no-op.
2845**
2846** (later): It is reported to me that the mixed-endian problem
2847** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2848** that early versions of GCC stored the two words of a 64-bit
2849** float in the wrong order. And that error has been propagated
2850** ever since. The blame is not necessarily with GCC, though.
2851** GCC might have just copying the problem from a prior compiler.
2852** I am also told that newer versions of GCC that follow a different
2853** ABI get the byte order right.
2854**
2855** Developers using SQLite on an ARM7 should compile and run their
2856** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2857** enabled, some asserts below will ensure that the byte order of
2858** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002859**
2860** (2007-08-30) Frank van Vugt has studied this problem closely
2861** and has send his findings to the SQLite developers. Frank
2862** writes that some Linux kernels offer floating point hardware
2863** emulation that uses only 32-bit mantissas instead of a full
2864** 48-bits as required by the IEEE standard. (This is the
2865** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2866** byte swapping becomes very complicated. To avoid problems,
2867** the necessary byte swapping is carried out using a 64-bit integer
2868** rather than a 64-bit float. Frank assures us that the code here
2869** works for him. We, the developers, have no way to independently
2870** verify this, but Frank seems to know what he is talking about
2871** so we trust him.
drh110daac2007-05-04 11:59:31 +00002872*/
2873#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002874static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002875 union {
drh60d09a72007-08-30 15:05:08 +00002876 u64 r;
drh110daac2007-05-04 11:59:31 +00002877 u32 i[2];
2878 } u;
2879 u32 t;
2880
2881 u.r = in;
2882 t = u.i[0];
2883 u.i[0] = u.i[1];
2884 u.i[1] = t;
2885 return u.r;
2886}
2887# define swapMixedEndianFloat(X) X = floatSwap(X)
2888#else
2889# define swapMixedEndianFloat(X)
2890#endif
2891
2892/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002893** Write the serialized data blob for the value stored in pMem into
2894** buf. It is assumed that the caller has allocated sufficient space.
2895** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002896**
drh038b7bc2013-12-09 23:17:22 +00002897** nBuf is the amount of space left in buf[]. The caller is responsible
2898** for allocating enough space to buf[] to hold the entire field, exclusive
2899** of the pMem->u.nZero bytes for a MEM_Zero value.
drhfdf972a2007-05-02 13:30:27 +00002900**
2901** Return the number of bytes actually written into buf[]. The number
2902** of bytes in the zero-filled tail is included in the return value only
2903** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002904*/
drha9ab4812013-12-11 11:00:44 +00002905u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
drh35cd6432009-06-05 14:17:21 +00002906 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002907
drh1483e142004-05-21 21:12:42 +00002908 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002909 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002910 u64 v;
drh35cd6432009-06-05 14:17:21 +00002911 u32 i;
drha19b7752004-05-30 21:14:58 +00002912 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002913 assert( sizeof(v)==sizeof(pMem->r) );
2914 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002915 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002916 }else{
drh3c024d62007-03-30 11:23:45 +00002917 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002918 }
drh1483e142004-05-21 21:12:42 +00002919 len = i = sqlite3VdbeSerialTypeLen(serial_type);
2920 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002921 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002922 v >>= 8;
2923 }
2924 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002925 }
drhd946db02005-12-29 19:23:06 +00002926
danielk1977cfcdaef2004-05-12 07:33:33 +00002927 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002928 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002929 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002930 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002931 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002932 memcpy(buf, pMem->z, len);
2933 return len;
2934 }
2935
2936 /* NULL or constants 0 or 1 */
2937 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002938}
2939
drhf926d1e2014-03-04 04:04:33 +00002940/* Input "x" is a sequence of unsigned characters that represent a
2941** big-endian integer. Return the equivalent native integer
2942*/
2943#define ONE_BYTE_INT(x) ((i8)(x)[0])
2944#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
2945#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
2946#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
2947
danielk1977cfcdaef2004-05-12 07:33:33 +00002948/*
2949** Deserialize the data blob pointed to by buf as serial type serial_type
2950** and store the result in pMem. Return the number of bytes read.
2951*/
drh35cd6432009-06-05 14:17:21 +00002952u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002953 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002954 u32 serial_type, /* Serial type to deserialize */
2955 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002956){
drh693e6712014-01-24 22:58:00 +00002957 u64 x;
2958 u32 y;
drh3c685822005-05-21 18:32:18 +00002959 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002960 case 10: /* Reserved for future use */
2961 case 11: /* Reserved for future use */
2962 case 0: { /* NULL */
2963 pMem->flags = MEM_Null;
2964 break;
2965 }
2966 case 1: { /* 1-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002967 pMem->u.i = ONE_BYTE_INT(buf);
drh1483e142004-05-21 21:12:42 +00002968 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002969 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002970 return 1;
drh1483e142004-05-21 21:12:42 +00002971 }
drh3c685822005-05-21 18:32:18 +00002972 case 2: { /* 2-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002973 pMem->u.i = TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002974 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002975 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002976 return 2;
2977 }
2978 case 3: { /* 3-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002979 pMem->u.i = THREE_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002980 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002981 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002982 return 3;
2983 }
2984 case 4: { /* 4-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002985 y = FOUR_BYTE_UINT(buf);
drh693e6712014-01-24 22:58:00 +00002986 pMem->u.i = (i64)*(int*)&y;
drh3c685822005-05-21 18:32:18 +00002987 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002988 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002989 return 4;
2990 }
2991 case 5: { /* 6-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002992 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002993 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00002994 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00002995 return 6;
2996 }
drh91124b32005-08-18 18:15:05 +00002997 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002998 case 7: { /* IEEE floating point */
drh2a3e4a72006-01-23 21:44:53 +00002999#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00003000 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00003001 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3002 ** defined that 64-bit floating point values really are mixed
3003 ** endian.
drhbfd6b032005-08-28 01:38:44 +00003004 */
drhde941c62005-08-28 01:34:21 +00003005 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00003006 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00003007 u64 t2 = t1;
3008 swapMixedEndianFloat(t2);
3009 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00003010#endif
drhf926d1e2014-03-04 04:04:33 +00003011 x = FOUR_BYTE_UINT(buf);
3012 y = FOUR_BYTE_UINT(buf+4);
drh3c685822005-05-21 18:32:18 +00003013 x = (x<<32) | y;
3014 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00003015 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00003016 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003017 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003018 }else{
drh4f0c5872007-03-26 22:05:01 +00003019 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00003020 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00003021 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00003022 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00003023 }
3024 return 8;
3025 }
drhd946db02005-12-29 19:23:06 +00003026 case 8: /* Integer 0 */
3027 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00003028 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00003029 pMem->flags = MEM_Int;
3030 return 0;
3031 }
drh3c685822005-05-21 18:32:18 +00003032 default: {
drhc138daf2013-11-19 13:55:34 +00003033 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
drh35cd6432009-06-05 14:17:21 +00003034 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00003035 pMem->z = (char *)buf;
3036 pMem->n = len;
3037 pMem->xDel = 0;
drhc138daf2013-11-19 13:55:34 +00003038 pMem->flags = aFlag[serial_type&1];
drh3c685822005-05-21 18:32:18 +00003039 return len;
drh696b32f2004-05-30 01:51:52 +00003040 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003041 }
drh3c685822005-05-21 18:32:18 +00003042 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00003043}
3044
drh1e968a02008-03-25 00:22:21 +00003045/*
dan03e9cfc2011-09-05 14:20:27 +00003046** This routine is used to allocate sufficient space for an UnpackedRecord
3047** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3048** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00003049**
dan03e9cfc2011-09-05 14:20:27 +00003050** The space is either allocated using sqlite3DbMallocRaw() or from within
3051** the unaligned buffer passed via the second and third arguments (presumably
3052** stack space). If the former, then *ppFree is set to a pointer that should
3053** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3054** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3055** before returning.
drh1e968a02008-03-25 00:22:21 +00003056**
dan03e9cfc2011-09-05 14:20:27 +00003057** If an OOM error occurs, NULL is returned.
3058*/
3059UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3060 KeyInfo *pKeyInfo, /* Description of the record */
3061 char *pSpace, /* Unaligned space available */
3062 int szSpace, /* Size of pSpace[] in bytes */
3063 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00003064){
dan03e9cfc2011-09-05 14:20:27 +00003065 UnpackedRecord *p; /* Unpacked record to return */
3066 int nOff; /* Increment pSpace by nOff to align it */
3067 int nByte; /* Number of bytes required for *p */
3068
3069 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00003070 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3071 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3072 */
3073 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00003074 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00003075 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00003076 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3077 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00003078 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00003079 }else{
dan42acb3e2011-09-05 20:16:38 +00003080 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00003081 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00003082 }
dan42acb3e2011-09-05 20:16:38 +00003083
3084 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drhe1a022e2012-09-17 17:16:53 +00003085 assert( pKeyInfo->aSortOrder!=0 );
drh1e968a02008-03-25 00:22:21 +00003086 p->pKeyInfo = pKeyInfo;
3087 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00003088 return p;
3089}
3090
3091/*
3092** Given the nKey-byte encoding of a record in pKey[], populate the
3093** UnpackedRecord structure indicated by the fourth argument with the
3094** contents of the decoded record.
3095*/
3096void sqlite3VdbeRecordUnpack(
3097 KeyInfo *pKeyInfo, /* Information about the record format */
3098 int nKey, /* Size of the binary record */
3099 const void *pKey, /* The binary record */
3100 UnpackedRecord *p /* Populate this structure before returning. */
3101){
3102 const unsigned char *aKey = (const unsigned char *)pKey;
3103 int d;
3104 u32 idx; /* Offset in aKey[] to read from */
3105 u16 u; /* Unsigned loop counter */
3106 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00003107 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00003108
dan1fed5da2014-02-25 21:01:25 +00003109 p->default_rc = 0;
drh8c5d1522009-04-10 00:56:28 +00003110 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00003111 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00003112 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00003113 u = 0;
drh2fa34d32009-07-15 16:30:50 +00003114 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00003115 u32 serial_type;
3116
danielk197700e13612008-11-17 19:18:54 +00003117 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00003118 pMem->enc = pKeyInfo->enc;
3119 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00003120 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
danielk19775f096132008-03-28 15:44:09 +00003121 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00003122 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00003123 pMem++;
shane0b8d2762008-07-22 05:18:00 +00003124 u++;
drh1e968a02008-03-25 00:22:21 +00003125 }
drh7d10d5a2008-08-20 16:35:10 +00003126 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00003127 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00003128}
3129
dan3833e932014-03-01 19:44:56 +00003130#if SQLITE_DEBUG
dan3b9330f2014-02-27 20:44:18 +00003131/*
dan3833e932014-03-01 19:44:56 +00003132** This function compares two index or table record keys in the same way
3133** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3134** this function deserializes and compares values using the
3135** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3136** in assert() statements to ensure that the optimized code in
3137** sqlite3VdbeRecordCompare() returns results with these two primitives.
dan3b9330f2014-02-27 20:44:18 +00003138*/
dan3833e932014-03-01 19:44:56 +00003139static int vdbeRecordCompareDebug(
dan1fed5da2014-02-25 21:01:25 +00003140 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003141 const UnpackedRecord *pPKey2 /* Right key */
dan1fed5da2014-02-25 21:01:25 +00003142){
dan3b9330f2014-02-27 20:44:18 +00003143 u32 d1; /* Offset into aKey[] of next data element */
3144 u32 idx1; /* Offset into aKey[] of next header element */
3145 u32 szHdr1; /* Number of bytes in header */
3146 int i = 0;
3147 int rc = 0;
3148 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3149 KeyInfo *pKeyInfo;
3150 Mem mem1;
dan1fed5da2014-02-25 21:01:25 +00003151
dan3b9330f2014-02-27 20:44:18 +00003152 pKeyInfo = pPKey2->pKeyInfo;
3153 mem1.enc = pKeyInfo->enc;
3154 mem1.db = pKeyInfo->db;
3155 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
3156 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003157
dan3b9330f2014-02-27 20:44:18 +00003158 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3159 ** We could initialize it, as shown here, to silence those complaints.
3160 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3161 ** the unnecessary initialization has a measurable negative performance
3162 ** impact, since this routine is a very high runner. And so, we choose
3163 ** to ignore the compiler warnings and leave this variable uninitialized.
3164 */
3165 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
3166
3167 idx1 = getVarint32(aKey1, szHdr1);
3168 d1 = szHdr1;
3169 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3170 assert( pKeyInfo->aSortOrder!=0 );
3171 assert( pKeyInfo->nField>0 );
3172 assert( idx1<=szHdr1 || CORRUPT_DB );
3173 do{
3174 u32 serial_type1;
dan1fed5da2014-02-25 21:01:25 +00003175
dan3b9330f2014-02-27 20:44:18 +00003176 /* Read the serial types for the next element in each key. */
3177 idx1 += getVarint32( aKey1+idx1, serial_type1 );
dan1fed5da2014-02-25 21:01:25 +00003178
dan3b9330f2014-02-27 20:44:18 +00003179 /* Verify that there is enough key space remaining to avoid
3180 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3181 ** always be greater than or equal to the amount of required key space.
3182 ** Use that approximation to avoid the more expensive call to
3183 ** sqlite3VdbeSerialTypeLen() in the common case.
3184 */
3185 if( d1+serial_type1+2>(u32)nKey1
3186 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3187 ){
3188 break;
dan1fed5da2014-02-25 21:01:25 +00003189 }
dan1fed5da2014-02-25 21:01:25 +00003190
dan3b9330f2014-02-27 20:44:18 +00003191 /* Extract the values to be compared.
3192 */
3193 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
dan1fed5da2014-02-25 21:01:25 +00003194
dan3b9330f2014-02-27 20:44:18 +00003195 /* Do the comparison
3196 */
3197 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3198 if( rc!=0 ){
3199 assert( mem1.zMalloc==0 ); /* See comment below */
3200 if( pKeyInfo->aSortOrder[i] ){
3201 rc = -rc; /* Invert the result for DESC sort order. */
dan1fed5da2014-02-25 21:01:25 +00003202 }
dan3b9330f2014-02-27 20:44:18 +00003203 return rc;
dan1fed5da2014-02-25 21:01:25 +00003204 }
dan3b9330f2014-02-27 20:44:18 +00003205 i++;
3206 }while( idx1<szHdr1 && i<pPKey2->nField );
dan1fed5da2014-02-25 21:01:25 +00003207
dan3b9330f2014-02-27 20:44:18 +00003208 /* No memory allocation is ever used on mem1. Prove this using
3209 ** the following assert(). If the assert() fails, it indicates a
3210 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3211 */
3212 assert( mem1.zMalloc==0 );
3213
3214 /* rc==0 here means that one of the keys ran out of fields and
3215 ** all the fields up to that point were equal. Return the the default_rc
3216 ** value. */
3217 return pPKey2->default_rc;
dan1fed5da2014-02-25 21:01:25 +00003218}
dan3833e932014-03-01 19:44:56 +00003219#endif
dan1fed5da2014-02-25 21:01:25 +00003220
dan3833e932014-03-01 19:44:56 +00003221/*
3222** Both *pMem1 and *pMem2 contain string values. Compare the two values
3223** using the collation sequence pColl. As usual, return a negative , zero
3224** or positive value if *pMem1 is less than, equal to or greater than
3225** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3226*/
dan1fed5da2014-02-25 21:01:25 +00003227static int vdbeCompareMemString(
dan3833e932014-03-01 19:44:56 +00003228 const Mem *pMem1,
3229 const Mem *pMem2,
dan1fed5da2014-02-25 21:01:25 +00003230 const CollSeq *pColl
3231){
3232 if( pMem1->enc==pColl->enc ){
3233 /* The strings are already in the correct encoding. Call the
3234 ** comparison function directly */
3235 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3236 }else{
3237 int rc;
3238 const void *v1, *v2;
3239 int n1, n2;
3240 Mem c1;
3241 Mem c2;
3242 memset(&c1, 0, sizeof(c1));
3243 memset(&c2, 0, sizeof(c2));
3244 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3245 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3246 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3247 n1 = v1==0 ? 0 : c1.n;
3248 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3249 n2 = v2==0 ? 0 : c2.n;
3250 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3251 sqlite3VdbeMemRelease(&c1);
3252 sqlite3VdbeMemRelease(&c2);
3253 return rc;
3254 }
3255}
3256
3257/*
3258** Compare the values contained by the two memory cells, returning
3259** negative, zero or positive if pMem1 is less than, equal to, or greater
3260** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3261** and reals) sorted numerically, followed by text ordered by the collating
3262** sequence pColl and finally blob's ordered by memcmp().
3263**
3264** Two NULL values are considered equal by this function.
3265*/
3266int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3267 int rc;
3268 int f1, f2;
3269 int combined_flags;
3270
3271 f1 = pMem1->flags;
3272 f2 = pMem2->flags;
3273 combined_flags = f1|f2;
3274 assert( (combined_flags & MEM_RowSet)==0 );
3275
3276 /* If one value is NULL, it is less than the other. If both values
3277 ** are NULL, return 0.
3278 */
3279 if( combined_flags&MEM_Null ){
3280 return (f2&MEM_Null) - (f1&MEM_Null);
3281 }
3282
3283 /* If one value is a number and the other is not, the number is less.
3284 ** If both are numbers, compare as reals if one is a real, or as integers
3285 ** if both values are integers.
3286 */
3287 if( combined_flags&(MEM_Int|MEM_Real) ){
3288 double r1, r2;
3289 if( (f1 & f2 & MEM_Int)!=0 ){
3290 if( pMem1->u.i < pMem2->u.i ) return -1;
3291 if( pMem1->u.i > pMem2->u.i ) return 1;
3292 return 0;
3293 }
3294 if( (f1&MEM_Real)!=0 ){
3295 r1 = pMem1->r;
3296 }else if( (f1&MEM_Int)!=0 ){
3297 r1 = (double)pMem1->u.i;
3298 }else{
3299 return 1;
3300 }
3301 if( (f2&MEM_Real)!=0 ){
3302 r2 = pMem2->r;
3303 }else if( (f2&MEM_Int)!=0 ){
3304 r2 = (double)pMem2->u.i;
3305 }else{
3306 return -1;
3307 }
3308 if( r1<r2 ) return -1;
3309 if( r1>r2 ) return 1;
3310 return 0;
3311 }
3312
3313 /* If one value is a string and the other is a blob, the string is less.
3314 ** If both are strings, compare using the collating functions.
3315 */
3316 if( combined_flags&MEM_Str ){
3317 if( (f1 & MEM_Str)==0 ){
3318 return 1;
3319 }
3320 if( (f2 & MEM_Str)==0 ){
3321 return -1;
3322 }
3323
3324 assert( pMem1->enc==pMem2->enc );
3325 assert( pMem1->enc==SQLITE_UTF8 ||
3326 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3327
3328 /* The collation sequence must be defined at this point, even if
3329 ** the user deletes the collation sequence after the vdbe program is
3330 ** compiled (this was not always the case).
3331 */
3332 assert( !pColl || pColl->xCmp );
3333
3334 if( pColl ){
3335 return vdbeCompareMemString(pMem1, pMem2, pColl);
3336 }
3337 /* If a NULL pointer was passed as the collate function, fall through
3338 ** to the blob case and use memcmp(). */
3339 }
3340
3341 /* Both values must be blobs. Compare using memcmp(). */
3342 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
3343 if( rc==0 ){
3344 rc = pMem1->n - pMem2->n;
3345 }
3346 return rc;
3347}
3348
3349
dan3833e932014-03-01 19:44:56 +00003350/*
3351** The first argument passed to this function is a serial-type that
3352** corresponds to an integer - all values between 1 and 9 inclusive
3353** except 7. The second points to a buffer containing an integer value
3354** serialized according to serial_type. This function deserializes
3355** and returns the value.
3356*/
dan3b9330f2014-02-27 20:44:18 +00003357static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
drhf926d1e2014-03-04 04:04:33 +00003358 u32 y;
dan3833e932014-03-01 19:44:56 +00003359 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
dan3b9330f2014-02-27 20:44:18 +00003360 switch( serial_type ){
dan3833e932014-03-01 19:44:56 +00003361 case 0:
dan3b9330f2014-02-27 20:44:18 +00003362 case 1:
drhb6e8fd12014-03-06 01:56:33 +00003363 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003364 return ONE_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003365 case 2:
drhb6e8fd12014-03-06 01:56:33 +00003366 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003367 return TWO_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003368 case 3:
drhb6e8fd12014-03-06 01:56:33 +00003369 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003370 return THREE_BYTE_INT(aKey);
3371 case 4: {
drhb6e8fd12014-03-06 01:56:33 +00003372 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003373 y = FOUR_BYTE_UINT(aKey);
3374 return (i64)*(int*)&y;
3375 }
dan3b9330f2014-02-27 20:44:18 +00003376 case 5: {
drhb6e8fd12014-03-06 01:56:33 +00003377 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003378 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhaf5b2af2013-08-05 15:32:09 +00003379 }
dan3b9330f2014-02-27 20:44:18 +00003380 case 6: {
drhf926d1e2014-03-04 04:04:33 +00003381 u64 x = FOUR_BYTE_UINT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003382 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003383 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3384 return (i64)*(i64*)&x;
drh1e968a02008-03-25 00:22:21 +00003385 }
dan3b9330f2014-02-27 20:44:18 +00003386 }
drh407414c2009-07-14 14:15:27 +00003387
dan3b9330f2014-02-27 20:44:18 +00003388 return (serial_type - 8);
drh1e968a02008-03-25 00:22:21 +00003389}
danielk1977eb015e02004-05-18 01:31:14 +00003390
dan3833e932014-03-01 19:44:56 +00003391/*
3392** This function compares the two table rows or index records
3393** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
3394** or positive integer if key1 is less than, equal to or
3395** greater than key2. The {nKey1, pKey1} key must be a blob
3396** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
3397** key must be a parsed key such as obtained from
3398** sqlite3VdbeParseRecord.
3399**
3400** If argument bSkip is non-zero, it is assumed that the caller has already
3401** determined that the first fields of the keys are equal.
3402**
3403** Key1 and Key2 do not have to contain the same number of fields. If all
3404** fields that appear in both keys are equal, then pPKey2->default_rc is
3405** returned.
drha1f7c0a2014-03-28 03:12:48 +00003406**
3407** If database corruption is discovered, set pPKey2->isCorrupt to non-zero
3408** and return 0.
dan3833e932014-03-01 19:44:56 +00003409*/
3410int sqlite3VdbeRecordCompare(
3411 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003412 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003413 int bSkip /* If true, skip the first field */
dan1fed5da2014-02-25 21:01:25 +00003414){
dan3833e932014-03-01 19:44:56 +00003415 u32 d1; /* Offset into aKey[] of next data element */
3416 int i; /* Index of next field to compare */
mistachkinffe6bc22014-03-04 11:16:20 +00003417 u32 szHdr1; /* Size of record header in bytes */
dan3833e932014-03-01 19:44:56 +00003418 u32 idx1; /* Offset of first type in header */
3419 int rc = 0; /* Return value */
3420 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
dan1fed5da2014-02-25 21:01:25 +00003421 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3422 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3423 Mem mem1;
3424
dan3833e932014-03-01 19:44:56 +00003425 /* If bSkip is true, then the caller has already determined that the first
3426 ** two elements in the keys are equal. Fix the various stack variables so
dan3b9330f2014-02-27 20:44:18 +00003427 ** that this routine begins comparing at the second field. */
dan3833e932014-03-01 19:44:56 +00003428 if( bSkip ){
dan3b9330f2014-02-27 20:44:18 +00003429 u32 s1;
dan3b9330f2014-02-27 20:44:18 +00003430 idx1 = 1 + getVarint32(&aKey1[1], s1);
dan3833e932014-03-01 19:44:56 +00003431 szHdr1 = aKey1[0];
3432 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
dan3b9330f2014-02-27 20:44:18 +00003433 i = 1;
3434 pRhs++;
dan3833e932014-03-01 19:44:56 +00003435 }else{
3436 idx1 = getVarint32(aKey1, szHdr1);
3437 d1 = szHdr1;
drha1f7c0a2014-03-28 03:12:48 +00003438 if( d1>(unsigned)nKey1 ){
3439 pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
3440 return 0; /* Corruption */
3441 }
dan3833e932014-03-01 19:44:56 +00003442 i = 0;
dan3b9330f2014-02-27 20:44:18 +00003443 }
3444
dan1fed5da2014-02-25 21:01:25 +00003445 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003446 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3447 || CORRUPT_DB );
3448 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3449 assert( pPKey2->pKeyInfo->nField>0 );
3450 assert( idx1<=szHdr1 || CORRUPT_DB );
3451 do{
dan1fed5da2014-02-25 21:01:25 +00003452 u32 serial_type;
3453
3454 /* RHS is an integer */
3455 if( pRhs->flags & MEM_Int ){
3456 serial_type = aKey1[idx1];
drhb6e8fd12014-03-06 01:56:33 +00003457 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003458 if( serial_type>=12 ){
3459 rc = +1;
3460 }else if( serial_type==0 ){
3461 rc = -1;
dan3b9330f2014-02-27 20:44:18 +00003462 }else if( serial_type==7 ){
3463 double rhs = (double)pRhs->u.i;
dan1fed5da2014-02-25 21:01:25 +00003464 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
dan3b9330f2014-02-27 20:44:18 +00003465 if( mem1.r<rhs ){
3466 rc = -1;
3467 }else if( mem1.r>rhs ){
3468 rc = +1;
3469 }
3470 }else{
3471 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3472 i64 rhs = pRhs->u.i;
3473 if( lhs<rhs ){
3474 rc = -1;
3475 }else if( lhs>rhs ){
3476 rc = +1;
dan1fed5da2014-02-25 21:01:25 +00003477 }
3478 }
3479 }
3480
3481 /* RHS is real */
3482 else if( pRhs->flags & MEM_Real ){
3483 serial_type = aKey1[idx1];
3484 if( serial_type>=12 ){
3485 rc = +1;
3486 }else if( serial_type==0 ){
3487 rc = -1;
3488 }else{
3489 double rhs = pRhs->r;
3490 double lhs;
3491 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3492 if( serial_type==7 ){
3493 lhs = mem1.r;
3494 }else{
drh295aedf2014-03-03 18:25:24 +00003495 lhs = (double)mem1.u.i;
dan1fed5da2014-02-25 21:01:25 +00003496 }
3497 if( lhs<rhs ){
3498 rc = -1;
3499 }else if( lhs>rhs ){
3500 rc = +1;
3501 }
3502 }
3503 }
3504
3505 /* RHS is a string */
3506 else if( pRhs->flags & MEM_Str ){
3507 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003508 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003509 if( serial_type<12 ){
3510 rc = -1;
3511 }else if( !(serial_type & 0x01) ){
3512 rc = +1;
3513 }else{
3514 mem1.n = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003515 testcase( (d1+mem1.n)==(unsigned)nKey1 );
3516 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003517 if( (d1+mem1.n) > (unsigned)nKey1 ){
drha1f7c0a2014-03-28 03:12:48 +00003518 pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
3519 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003520 }else if( pKeyInfo->aColl[i] ){
3521 mem1.enc = pKeyInfo->enc;
3522 mem1.db = pKeyInfo->db;
3523 mem1.flags = MEM_Str;
drhfcb44a82014-03-03 15:13:27 +00003524 mem1.z = (char*)&aKey1[d1];
dan1fed5da2014-02-25 21:01:25 +00003525 rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
3526 }else{
3527 int nCmp = MIN(mem1.n, pRhs->n);
3528 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3529 if( rc==0 ) rc = mem1.n - pRhs->n;
3530 }
3531 }
3532 }
3533
3534 /* RHS is a blob */
3535 else if( pRhs->flags & MEM_Blob ){
3536 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003537 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003538 if( serial_type<12 || (serial_type & 0x01) ){
3539 rc = -1;
3540 }else{
3541 int nStr = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003542 testcase( (d1+nStr)==(unsigned)nKey1 );
3543 testcase( (d1+nStr+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003544 if( (d1+nStr) > (unsigned)nKey1 ){
drha1f7c0a2014-03-28 03:12:48 +00003545 pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
3546 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003547 }else{
3548 int nCmp = MIN(nStr, pRhs->n);
3549 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3550 if( rc==0 ) rc = nStr - pRhs->n;
3551 }
3552 }
3553 }
3554
3555 /* RHS is null */
3556 else{
3557 serial_type = aKey1[idx1];
3558 rc = (serial_type!=0);
3559 }
3560
3561 if( rc!=0 ){
dan1fed5da2014-02-25 21:01:25 +00003562 if( pKeyInfo->aSortOrder[i] ){
3563 rc = -rc;
dan1fed5da2014-02-25 21:01:25 +00003564 }
drhed79b452014-03-04 16:21:18 +00003565 assert( CORRUPT_DB
dan3833e932014-03-01 19:44:56 +00003566 || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3567 || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
drhed79b452014-03-04 16:21:18 +00003568 || pKeyInfo->db->mallocFailed
dan3833e932014-03-01 19:44:56 +00003569 );
3570 assert( mem1.zMalloc==0 ); /* See comment below */
dan1fed5da2014-02-25 21:01:25 +00003571 return rc;
3572 }
3573
3574 i++;
dan3b9330f2014-02-27 20:44:18 +00003575 pRhs++;
dan1fed5da2014-02-25 21:01:25 +00003576 d1 += sqlite3VdbeSerialTypeLen(serial_type);
3577 idx1 += sqlite3VarintLen(serial_type);
drh295aedf2014-03-03 18:25:24 +00003578 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
dan1fed5da2014-02-25 21:01:25 +00003579
3580 /* No memory allocation is ever used on mem1. Prove this using
3581 ** the following assert(). If the assert() fails, it indicates a
dan3833e932014-03-01 19:44:56 +00003582 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
dan1fed5da2014-02-25 21:01:25 +00003583 assert( mem1.zMalloc==0 );
3584
3585 /* rc==0 here means that one or both of the keys ran out of fields and
3586 ** all the fields up to that point were equal. Return the the default_rc
3587 ** value. */
dan3833e932014-03-01 19:44:56 +00003588 assert( CORRUPT_DB
3589 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
dan6696ba32014-06-28 19:06:49 +00003590 || pKeyInfo->db->mallocFailed
dan3833e932014-03-01 19:44:56 +00003591 );
dan1fed5da2014-02-25 21:01:25 +00003592 return pPKey2->default_rc;
3593}
3594
dan3833e932014-03-01 19:44:56 +00003595/*
3596** This function is an optimized version of sqlite3VdbeRecordCompare()
3597** that (a) the first field of pPKey2 is an integer, and (b) the
3598** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3599** byte (i.e. is less than 128).
drhe2ac5062014-03-26 12:02:38 +00003600**
3601** To avoid concerns about buffer overreads, this routine is only used
3602** on schemas where the maximum valid header size is 63 bytes or less.
dan3833e932014-03-01 19:44:56 +00003603*/
dan3b9330f2014-02-27 20:44:18 +00003604static int vdbeRecordCompareInt(
3605 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003606 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003607 int bSkip /* Ignored */
dan3b9330f2014-02-27 20:44:18 +00003608){
dan9b8afef2014-03-03 20:48:50 +00003609 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
dan3b9330f2014-02-27 20:44:18 +00003610 int serial_type = ((const u8*)pKey1)[1];
3611 int res;
drhf926d1e2014-03-04 04:04:33 +00003612 u32 y;
3613 u64 x;
dan3b9330f2014-02-27 20:44:18 +00003614 i64 v = pPKey2->aMem[0].u.i;
3615 i64 lhs;
drh295aedf2014-03-03 18:25:24 +00003616 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003617
dan3833e932014-03-01 19:44:56 +00003618 assert( bSkip==0 );
drhe2ac5062014-03-26 12:02:38 +00003619 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
dan3833e932014-03-01 19:44:56 +00003620 switch( serial_type ){
drhf926d1e2014-03-04 04:04:33 +00003621 case 1: { /* 1-byte signed integer */
3622 lhs = ONE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003623 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003624 break;
3625 }
drhf926d1e2014-03-04 04:04:33 +00003626 case 2: { /* 2-byte signed integer */
3627 lhs = TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003628 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003629 break;
3630 }
3631 case 3: { /* 3-byte signed integer */
3632 lhs = THREE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003633 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003634 break;
3635 }
3636 case 4: { /* 4-byte signed integer */
3637 y = FOUR_BYTE_UINT(aKey);
3638 lhs = (i64)*(int*)&y;
drhb6e8fd12014-03-06 01:56:33 +00003639 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003640 break;
3641 }
3642 case 5: { /* 6-byte signed integer */
3643 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003644 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003645 break;
3646 }
3647 case 6: { /* 8-byte signed integer */
3648 x = FOUR_BYTE_UINT(aKey);
3649 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3650 lhs = *(i64*)&x;
drhb6e8fd12014-03-06 01:56:33 +00003651 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003652 break;
3653 }
dan3b9330f2014-02-27 20:44:18 +00003654 case 8:
3655 lhs = 0;
3656 break;
dan3b9330f2014-02-27 20:44:18 +00003657 case 9:
3658 lhs = 1;
3659 break;
3660
dan063d4a02014-02-28 09:48:30 +00003661 /* This case could be removed without changing the results of running
3662 ** this code. Including it causes gcc to generate a faster switch
3663 ** statement (since the range of switch targets now starts at zero and
dan597515d2014-02-28 18:39:51 +00003664 ** is contiguous) but does not cause any duplicate code to be generated
dan063d4a02014-02-28 09:48:30 +00003665 ** (as gcc is clever enough to combine the two like cases). Other
3666 ** compilers might be similar. */
3667 case 0: case 7:
dan3833e932014-03-01 19:44:56 +00003668 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan063d4a02014-02-28 09:48:30 +00003669
dan3b9330f2014-02-27 20:44:18 +00003670 default:
dan3833e932014-03-01 19:44:56 +00003671 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan3b9330f2014-02-27 20:44:18 +00003672 }
3673
3674 if( v>lhs ){
3675 res = pPKey2->r1;
3676 }else if( v<lhs ){
3677 res = pPKey2->r2;
3678 }else if( pPKey2->nField>1 ){
dan063d4a02014-02-28 09:48:30 +00003679 /* The first fields of the two keys are equal. Compare the trailing
3680 ** fields. */
dan3833e932014-03-01 19:44:56 +00003681 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003682 }else{
dan063d4a02014-02-28 09:48:30 +00003683 /* The first fields of the two keys are equal and there are no trailing
3684 ** fields. Return pPKey2->default_rc in this case. */
dan3b9330f2014-02-27 20:44:18 +00003685 res = pPKey2->default_rc;
3686 }
3687
dan3833e932014-03-01 19:44:56 +00003688 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3689 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3690 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003691 || CORRUPT_DB
3692 );
3693 return res;
3694}
3695
dan3833e932014-03-01 19:44:56 +00003696/*
3697** This function is an optimized version of sqlite3VdbeRecordCompare()
3698** that (a) the first field of pPKey2 is a string, that (b) the first field
3699** uses the collation sequence BINARY and (c) that the size-of-header varint
3700** at the start of (pKey1/nKey1) fits in a single byte.
3701*/
dan3b9330f2014-02-27 20:44:18 +00003702static int vdbeRecordCompareString(
3703 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003704 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003705 int bSkip
dan3b9330f2014-02-27 20:44:18 +00003706){
3707 const u8 *aKey1 = (const u8*)pKey1;
3708 int serial_type;
3709 int res;
drh295aedf2014-03-03 18:25:24 +00003710 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003711
dan3833e932014-03-01 19:44:56 +00003712 assert( bSkip==0 );
dan3b9330f2014-02-27 20:44:18 +00003713 getVarint32(&aKey1[1], serial_type);
3714
3715 if( serial_type<12 ){
3716 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
3717 }else if( !(serial_type & 0x01) ){
3718 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
3719 }else{
3720 int nCmp;
3721 int nStr;
dan3833e932014-03-01 19:44:56 +00003722 int szHdr = aKey1[0];
dan3b9330f2014-02-27 20:44:18 +00003723
3724 nStr = (serial_type-12) / 2;
drha1f7c0a2014-03-28 03:12:48 +00003725 if( (szHdr + nStr) > nKey1 ){
3726 pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
3727 return 0; /* Corruption */
3728 }
dan3b9330f2014-02-27 20:44:18 +00003729 nCmp = MIN( pPKey2->aMem[0].n, nStr );
dan3833e932014-03-01 19:44:56 +00003730 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
dan3b9330f2014-02-27 20:44:18 +00003731
3732 if( res==0 ){
3733 res = nStr - pPKey2->aMem[0].n;
3734 if( res==0 ){
3735 if( pPKey2->nField>1 ){
dan3833e932014-03-01 19:44:56 +00003736 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003737 }else{
3738 res = pPKey2->default_rc;
3739 }
3740 }else if( res>0 ){
3741 res = pPKey2->r2;
3742 }else{
3743 res = pPKey2->r1;
3744 }
3745 }else if( res>0 ){
3746 res = pPKey2->r2;
3747 }else{
3748 res = pPKey2->r1;
3749 }
3750 }
3751
dan3833e932014-03-01 19:44:56 +00003752 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3753 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3754 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003755 || CORRUPT_DB
dan6696ba32014-06-28 19:06:49 +00003756 || pPKey2->pKeyInfo->db->mallocFailed
dan3b9330f2014-02-27 20:44:18 +00003757 );
3758 return res;
3759}
3760
dan3833e932014-03-01 19:44:56 +00003761/*
3762** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3763** suitable for comparing serialized records to the unpacked record passed
3764** as the only argument.
3765*/
dan1fed5da2014-02-25 21:01:25 +00003766RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
dan9b8afef2014-03-03 20:48:50 +00003767 /* varintRecordCompareInt() and varintRecordCompareString() both assume
3768 ** that the size-of-header varint that occurs at the start of each record
3769 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
3770 ** also assumes that it is safe to overread a buffer by at least the
3771 ** maximum possible legal header size plus 8 bytes. Because there is
3772 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
3773 ** buffer passed to varintRecordCompareInt() this makes it convenient to
3774 ** limit the size of the header to 64 bytes in cases where the first field
3775 ** is an integer.
3776 **
3777 ** The easiest way to enforce this limit is to consider only records with
3778 ** 13 fields or less. If the first field is an integer, the maximum legal
3779 ** header size is (12*5 + 1 + 1) bytes. */
3780 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
dan1fed5da2014-02-25 21:01:25 +00003781 int flags = p->aMem[0].flags;
dan3b9330f2014-02-27 20:44:18 +00003782 if( p->pKeyInfo->aSortOrder[0] ){
3783 p->r1 = 1;
3784 p->r2 = -1;
3785 }else{
3786 p->r1 = -1;
3787 p->r2 = 1;
3788 }
dan1fed5da2014-02-25 21:01:25 +00003789 if( (flags & MEM_Int) ){
3790 return vdbeRecordCompareInt;
dan3b9330f2014-02-27 20:44:18 +00003791 }
drhb6e8fd12014-03-06 01:56:33 +00003792 testcase( flags & MEM_Real );
3793 testcase( flags & MEM_Null );
3794 testcase( flags & MEM_Blob );
3795 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
3796 assert( flags & MEM_Str );
dan1fed5da2014-02-25 21:01:25 +00003797 return vdbeRecordCompareString;
3798 }
3799 }
dan3b9330f2014-02-27 20:44:18 +00003800
dan3833e932014-03-01 19:44:56 +00003801 return sqlite3VdbeRecordCompare;
dan3b9330f2014-02-27 20:44:18 +00003802}
dan1fed5da2014-02-25 21:01:25 +00003803
danielk1977eb015e02004-05-18 01:31:14 +00003804/*
drh7a224de2004-06-02 01:22:02 +00003805** pCur points at an index entry created using the OP_MakeRecord opcode.
3806** Read the rowid (the last field in the record) and store it in *rowid.
3807** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00003808**
3809** pCur might be pointing to text obtained from a corrupt database file.
3810** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00003811*/
drh35f6b932009-06-23 14:15:04 +00003812int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00003813 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003814 int rc;
drhd5788202004-05-28 08:21:05 +00003815 u32 szHdr; /* Size of the header */
3816 u32 typeRowid; /* Serial type of the rowid */
3817 u32 lenRowid; /* Size of the rowid */
3818 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00003819
shanecea72b22009-09-07 04:38:36 +00003820 UNUSED_PARAMETER(db);
3821
drh88a003e2008-12-11 16:17:03 +00003822 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00003823 ** than 2GiB are support - anything large must be database corruption.
3824 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00003825 ** this code can safely assume that nCellKey is 32-bits
3826 */
drhea8ffdf2009-07-22 00:35:23 +00003827 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003828 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003829 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00003830 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003831
3832 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003833 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003834 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003835 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003836 return rc;
3837 }
drh88a003e2008-12-11 16:17:03 +00003838
3839 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003840 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003841 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003842 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003843 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003844 goto idx_rowid_corruption;
3845 }
3846
3847 /* The last field of the index should be an integer - the ROWID.
3848 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003849 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003850 testcase( typeRowid==1 );
3851 testcase( typeRowid==2 );
3852 testcase( typeRowid==3 );
3853 testcase( typeRowid==4 );
3854 testcase( typeRowid==5 );
3855 testcase( typeRowid==6 );
3856 testcase( typeRowid==8 );
3857 testcase( typeRowid==9 );
3858 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3859 goto idx_rowid_corruption;
3860 }
drhd5788202004-05-28 08:21:05 +00003861 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003862 testcase( (u32)m.n==szHdr+lenRowid );
3863 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003864 goto idx_rowid_corruption;
3865 }
3866
3867 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003868 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003869 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003870 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003871 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003872
3873 /* Jump here if database corruption is detected after m has been
3874 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3875idx_rowid_corruption:
3876 testcase( m.zMalloc!=0 );
3877 sqlite3VdbeMemRelease(&m);
3878 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003879}
3880
drh7cf6e4d2004-05-19 14:56:55 +00003881/*
drh5f82e3c2009-07-06 00:44:08 +00003882** Compare the key of the index entry that cursor pC is pointing to against
3883** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003884** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003885** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003886**
drh5f82e3c2009-07-06 00:44:08 +00003887** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003888** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003889** is ignored as well. Hence, this routine only compares the prefixes
3890** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003891*/
danielk1977183f9f72004-05-13 05:20:26 +00003892int sqlite3VdbeIdxKeyCompare(
drh295aedf2014-03-03 18:25:24 +00003893 VdbeCursor *pC, /* The cursor to compare against */
drha1f7c0a2014-03-28 03:12:48 +00003894 UnpackedRecord *pUnpacked, /* Unpacked version of key */
drh295aedf2014-03-03 18:25:24 +00003895 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003896){
drh61fc5952007-04-01 23:49:51 +00003897 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003898 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003899 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003900 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003901
drhea8ffdf2009-07-22 00:35:23 +00003902 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003903 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003904 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh56689692014-03-03 19:29:28 +00003905 /* nCellKey will always be between 0 and 0xffffffff because of the way
drh407414c2009-07-14 14:15:27 +00003906 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003907 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003908 *res = 0;
drh9978c972010-02-23 17:36:32 +00003909 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003910 }
drhfd3ca1c2009-08-25 12:11:00 +00003911 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003912 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003913 if( rc ){
drhd5788202004-05-28 08:21:05 +00003914 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003915 }
dan3833e932014-03-01 19:44:56 +00003916 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
danielk1977d8123362004-06-12 09:25:12 +00003917 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003918 return SQLITE_OK;
3919}
danielk1977b28af712004-06-21 06:50:26 +00003920
3921/*
3922** This routine sets the value to be returned by subsequent calls to
3923** sqlite3_changes() on the database handle 'db'.
3924*/
3925void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003926 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003927 db->nChange = nChange;
3928 db->nTotalChange += nChange;
3929}
3930
3931/*
3932** Set a flag in the vdbe to update the change counter when it is finalised
3933** or reset.
3934*/
drh4794f732004-11-05 17:17:50 +00003935void sqlite3VdbeCountChanges(Vdbe *v){
3936 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003937}
drhd89bd002005-01-22 03:03:54 +00003938
3939/*
3940** Mark every prepared statement associated with a database connection
3941** as expired.
3942**
3943** An expired statement means that recompilation of the statement is
3944** recommend. Statements expire when things happen that make their
3945** programs obsolete. Removing user-defined functions or collating
3946** sequences, or changing an authorization function are the types of
3947** things that make prepared statements obsolete.
3948*/
3949void sqlite3ExpirePreparedStatements(sqlite3 *db){
3950 Vdbe *p;
3951 for(p = db->pVdbe; p; p=p->pNext){
3952 p->expired = 1;
3953 }
3954}
danielk1977aee18ef2005-03-09 12:26:50 +00003955
3956/*
3957** Return the database associated with the Vdbe.
3958*/
3959sqlite3 *sqlite3VdbeDb(Vdbe *v){
3960 return v->db;
3961}
dan937d0de2009-10-15 18:35:38 +00003962
3963/*
3964** Return a pointer to an sqlite3_value structure containing the value bound
3965** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3966** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3967** constants) to the value before returning it.
3968**
3969** The returned value must be freed by the caller using sqlite3ValueFree().
3970*/
drhcf0fd4a2013-08-01 12:21:58 +00003971sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
dan937d0de2009-10-15 18:35:38 +00003972 assert( iVar>0 );
3973 if( v ){
3974 Mem *pMem = &v->aVar[iVar-1];
3975 if( 0==(pMem->flags & MEM_Null) ){
3976 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3977 if( pRet ){
3978 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3979 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
dan937d0de2009-10-15 18:35:38 +00003980 }
3981 return pRet;
3982 }
3983 }
3984 return 0;
3985}
3986
3987/*
3988** Configure SQL variable iVar so that binding a new value to it signals
3989** to sqlite3_reoptimize() that re-preparing the statement may result
3990** in a better query plan.
3991*/
dan1d2ce4f2009-10-19 18:11:09 +00003992void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003993 assert( iVar>0 );
3994 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00003995 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00003996 }else{
dan1d2ce4f2009-10-19 18:11:09 +00003997 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00003998 }
3999}
dan016f7812013-08-21 17:35:48 +00004000
4001#ifndef SQLITE_OMIT_VIRTUALTABLE
4002/*
4003** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4004** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4005** in memory obtained from sqlite3DbMalloc).
4006*/
4007void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
4008 sqlite3 *db = p->db;
4009 sqlite3DbFree(db, p->zErrMsg);
4010 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
4011 sqlite3_free(pVtab->zErrMsg);
4012 pVtab->zErrMsg = 0;
4013}
4014#endif /* SQLITE_OMIT_VIRTUALTABLE */