blob: e817ccb23f43d9aa72cf0ad31fdb7361f6fc746d [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000018#include "vdbeInt.h"
19
drh9a324642003-09-06 20:12:01 +000020/*
21** Create a new virtual database engine.
22*/
drh9ac79622013-12-18 15:11:47 +000023Vdbe *sqlite3VdbeCreate(Parse *pParse){
24 sqlite3 *db = pParse->db;
drh9a324642003-09-06 20:12:01 +000025 Vdbe *p;
drh17435752007-08-16 04:30:38 +000026 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000027 if( p==0 ) return 0;
28 p->db = db;
29 if( db->pVdbe ){
30 db->pVdbe->pPrev = p;
31 }
32 p->pNext = db->pVdbe;
33 p->pPrev = 0;
34 db->pVdbe = p;
35 p->magic = VDBE_MAGIC_INIT;
drh9ac79622013-12-18 15:11:47 +000036 p->pParse = pParse;
drh73d5b8f2013-12-23 19:09:07 +000037 assert( pParse->aLabel==0 );
38 assert( pParse->nLabel==0 );
39 assert( pParse->nOpAlloc==0 );
drh9a324642003-09-06 20:12:01 +000040 return p;
41}
42
43/*
drhb900aaf2006-11-09 00:24:53 +000044** Remember the SQL string for a prepared statement.
45*/
danielk19776ab3a2e2009-02-19 14:39:25 +000046void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
dan1d2ce4f2009-10-19 18:11:09 +000047 assert( isPrepareV2==1 || isPrepareV2==0 );
drhb900aaf2006-11-09 00:24:53 +000048 if( p==0 ) return;
danac455932012-11-26 19:50:41 +000049#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
danielk19776ab3a2e2009-02-19 14:39:25 +000050 if( !isPrepareV2 ) return;
51#endif
drhb900aaf2006-11-09 00:24:53 +000052 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000053 p->zSql = sqlite3DbStrNDup(p->db, z, n);
shanef639c402009-11-03 19:42:30 +000054 p->isPrepareV2 = (u8)isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000055}
56
57/*
58** Return the SQL associated with a prepared statement
59*/
danielk1977d0e2a852007-11-14 06:48:48 +000060const char *sqlite3_sql(sqlite3_stmt *pStmt){
danielk19776ab3a2e2009-02-19 14:39:25 +000061 Vdbe *p = (Vdbe *)pStmt;
drh87f5c5f2010-01-20 01:20:56 +000062 return (p && p->isPrepareV2) ? p->zSql : 0;
drhb900aaf2006-11-09 00:24:53 +000063}
64
65/*
drhc5155252007-01-08 21:07:17 +000066** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000067*/
drhc5155252007-01-08 21:07:17 +000068void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
69 Vdbe tmp, *pTmp;
70 char *zTmp;
drhc5155252007-01-08 21:07:17 +000071 tmp = *pA;
72 *pA = *pB;
73 *pB = tmp;
74 pTmp = pA->pNext;
75 pA->pNext = pB->pNext;
76 pB->pNext = pTmp;
77 pTmp = pA->pPrev;
78 pA->pPrev = pB->pPrev;
79 pB->pPrev = pTmp;
80 zTmp = pA->zSql;
81 pA->zSql = pB->zSql;
82 pB->zSql = zTmp;
danielk19776ab3a2e2009-02-19 14:39:25 +000083 pB->isPrepareV2 = pA->isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000084}
85
drh9a324642003-09-06 20:12:01 +000086/*
danielk197700e13612008-11-17 19:18:54 +000087** Resize the Vdbe.aOp array so that it is at least one op larger than
88** it was.
danielk1977ace3eb22006-01-26 10:35:04 +000089**
danielk197700e13612008-11-17 19:18:54 +000090** If an out-of-memory error occurs while resizing the array, return
91** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
92** unchanged (this is so that any opcodes already allocated can be
93** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +000094*/
drh73d5b8f2013-12-23 19:09:07 +000095static int growOpArray(Vdbe *v){
drha4e5d582007-10-20 15:41:57 +000096 VdbeOp *pNew;
drh73d5b8f2013-12-23 19:09:07 +000097 Parse *p = v->pParse;
danielk197700e13612008-11-17 19:18:54 +000098 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
drh73d5b8f2013-12-23 19:09:07 +000099 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000100 if( pNew ){
drhb45f65d2009-03-01 19:42:11 +0000101 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
drh73d5b8f2013-12-23 19:09:07 +0000102 v->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000103 }
danielk197700e13612008-11-17 19:18:54 +0000104 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000105}
106
drh313619f2013-10-31 20:34:06 +0000107#ifdef SQLITE_DEBUG
108/* This routine is just a convenient place to set a breakpoint that will
109** fire after each opcode is inserted and displayed using
110** "PRAGMA vdbe_addoptrace=on".
111*/
112static void test_addop_breakpoint(void){
113 static int n = 0;
114 n++;
115}
116#endif
117
drh76ff3a02004-09-24 22:32:30 +0000118/*
drh9a324642003-09-06 20:12:01 +0000119** Add a new instruction to the list of instructions current in the
120** VDBE. Return the address of the new instruction.
121**
122** Parameters:
123**
124** p Pointer to the VDBE
125**
126** op The opcode for this instruction
127**
drh66a51672008-01-03 00:01:23 +0000128** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000129**
danielk19774adee202004-05-08 08:23:19 +0000130** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000131** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000132** operand.
133*/
drh66a51672008-01-03 00:01:23 +0000134int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000135 int i;
drh701a0ae2004-02-22 20:05:00 +0000136 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000137
138 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000139 assert( p->magic==VDBE_MAGIC_INIT );
drh8df32842008-12-09 02:51:23 +0000140 assert( op>0 && op<0xff );
drh73d5b8f2013-12-23 19:09:07 +0000141 if( p->pParse->nOpAlloc<=i ){
danielk197700e13612008-11-17 19:18:54 +0000142 if( growOpArray(p) ){
drhc42ed162009-06-26 14:04:51 +0000143 return 1;
drhfd2d26b2006-03-15 22:44:36 +0000144 }
drh9a324642003-09-06 20:12:01 +0000145 }
danielk197701256832007-04-18 14:24:32 +0000146 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000147 pOp = &p->aOp[i];
drh8df32842008-12-09 02:51:23 +0000148 pOp->opcode = (u8)op;
drh26c9b5e2008-04-11 14:56:53 +0000149 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000150 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000151 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000152 pOp->p3 = p3;
153 pOp->p4.p = 0;
154 pOp->p4type = P4_NOTUSED;
drhc7379ce2013-10-30 02:28:23 +0000155#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000156 pOp->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000157#endif
158#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000159 if( p->db->flags & SQLITE_VdbeAddopTrace ){
drh9ac79622013-12-18 15:11:47 +0000160 int jj, kk;
161 Parse *pParse = p->pParse;
162 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
163 struct yColCache *x = pParse->aColCache + jj;
164 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
165 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
166 kk++;
167 }
168 if( kk ) printf("\n");
drhe0962052013-01-29 19:14:31 +0000169 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh313619f2013-10-31 20:34:06 +0000170 test_addop_breakpoint();
drhe0962052013-01-29 19:14:31 +0000171 }
drh9a324642003-09-06 20:12:01 +0000172#endif
drh26c9b5e2008-04-11 14:56:53 +0000173#ifdef VDBE_PROFILE
174 pOp->cycles = 0;
175 pOp->cnt = 0;
176#endif
drh688852a2014-02-17 22:40:43 +0000177#ifdef SQLITE_VDBE_COVERAGE
178 pOp->iSrcLine = 0;
179#endif
drh9a324642003-09-06 20:12:01 +0000180 return i;
181}
drh66a51672008-01-03 00:01:23 +0000182int sqlite3VdbeAddOp0(Vdbe *p, int op){
183 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
184}
185int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
186 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
187}
188int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
189 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000190}
191
drh66a51672008-01-03 00:01:23 +0000192
drh701a0ae2004-02-22 20:05:00 +0000193/*
drh66a51672008-01-03 00:01:23 +0000194** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000195*/
drh66a51672008-01-03 00:01:23 +0000196int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000197 Vdbe *p, /* Add the opcode to this VM */
198 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000199 int p1, /* The P1 operand */
200 int p2, /* The P2 operand */
201 int p3, /* The P3 operand */
202 const char *zP4, /* The P4 operand */
203 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000204){
drh66a51672008-01-03 00:01:23 +0000205 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
206 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000207 return addr;
208}
209
210/*
drh5d9c9da2011-06-03 20:11:17 +0000211** Add an OP_ParseSchema opcode. This routine is broken out from
drhe4c88c02012-01-04 12:57:45 +0000212** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
213** as having been used.
drh5d9c9da2011-06-03 20:11:17 +0000214**
215** The zWhere string must have been obtained from sqlite3_malloc().
216** This routine will take ownership of the allocated memory.
217*/
218void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
219 int j;
220 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
221 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
222 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
223}
224
225/*
drh8cff69d2009-11-12 19:59:44 +0000226** Add an opcode that includes the p4 value as an integer.
227*/
228int sqlite3VdbeAddOp4Int(
229 Vdbe *p, /* Add the opcode to this VM */
230 int op, /* The new opcode */
231 int p1, /* The P1 operand */
232 int p2, /* The P2 operand */
233 int p3, /* The P3 operand */
234 int p4 /* The P4 operand as an integer */
235){
236 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
237 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
238 return addr;
239}
240
241/*
drh9a324642003-09-06 20:12:01 +0000242** Create a new symbolic label for an instruction that has yet to be
243** coded. The symbolic label is really just a negative number. The
244** label can be used as the P2 value of an operation. Later, when
245** the label is resolved to a specific address, the VDBE will scan
246** through its operation list and change all values of P2 which match
247** the label into the resolved address.
248**
249** The VDBE knows that a P2 value is a label because labels are
250** always negative and P2 values are suppose to be non-negative.
251** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000252**
253** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000254*/
drh73d5b8f2013-12-23 19:09:07 +0000255int sqlite3VdbeMakeLabel(Vdbe *v){
256 Parse *p = v->pParse;
drhc35f3d52012-02-01 19:03:38 +0000257 int i = p->nLabel++;
drh73d5b8f2013-12-23 19:09:07 +0000258 assert( v->magic==VDBE_MAGIC_INIT );
drhc35f3d52012-02-01 19:03:38 +0000259 if( (i & (i-1))==0 ){
260 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
261 (i*2+1)*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000262 }
drh76ff3a02004-09-24 22:32:30 +0000263 if( p->aLabel ){
264 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000265 }
drh9a324642003-09-06 20:12:01 +0000266 return -1-i;
267}
268
269/*
270** Resolve label "x" to be the address of the next instruction to
271** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000272** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000273*/
drh73d5b8f2013-12-23 19:09:07 +0000274void sqlite3VdbeResolveLabel(Vdbe *v, int x){
275 Parse *p = v->pParse;
drh76ff3a02004-09-24 22:32:30 +0000276 int j = -1-x;
drh73d5b8f2013-12-23 19:09:07 +0000277 assert( v->magic==VDBE_MAGIC_INIT );
drhb2b9d3d2013-08-01 01:14:43 +0000278 assert( j<p->nLabel );
279 if( j>=0 && p->aLabel ){
drh73d5b8f2013-12-23 19:09:07 +0000280 p->aLabel[j] = v->nOp;
drh9a324642003-09-06 20:12:01 +0000281 }
drh61019c72014-01-04 16:49:02 +0000282 p->iFixedOp = v->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000283}
284
drh4611d922010-02-25 14:47:01 +0000285/*
286** Mark the VDBE as one that can only be run one time.
287*/
288void sqlite3VdbeRunOnlyOnce(Vdbe *p){
289 p->runOnlyOnce = 1;
290}
291
drhff738bc2009-09-24 00:09:58 +0000292#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
dan144926d2009-09-09 11:37:20 +0000293
294/*
295** The following type and function are used to iterate through all opcodes
296** in a Vdbe main program and each of the sub-programs (triggers) it may
297** invoke directly or indirectly. It should be used as follows:
298**
299** Op *pOp;
300** VdbeOpIter sIter;
301**
302** memset(&sIter, 0, sizeof(sIter));
303** sIter.v = v; // v is of type Vdbe*
304** while( (pOp = opIterNext(&sIter)) ){
305** // Do something with pOp
306** }
307** sqlite3DbFree(v->db, sIter.apSub);
308**
309*/
310typedef struct VdbeOpIter VdbeOpIter;
311struct VdbeOpIter {
312 Vdbe *v; /* Vdbe to iterate through the opcodes of */
313 SubProgram **apSub; /* Array of subprograms */
314 int nSub; /* Number of entries in apSub */
315 int iAddr; /* Address of next instruction to return */
316 int iSub; /* 0 = main program, 1 = first sub-program etc. */
317};
318static Op *opIterNext(VdbeOpIter *p){
319 Vdbe *v = p->v;
320 Op *pRet = 0;
321 Op *aOp;
322 int nOp;
323
324 if( p->iSub<=p->nSub ){
325
326 if( p->iSub==0 ){
327 aOp = v->aOp;
328 nOp = v->nOp;
329 }else{
330 aOp = p->apSub[p->iSub-1]->aOp;
331 nOp = p->apSub[p->iSub-1]->nOp;
332 }
333 assert( p->iAddr<nOp );
334
335 pRet = &aOp[p->iAddr];
336 p->iAddr++;
337 if( p->iAddr==nOp ){
338 p->iSub++;
339 p->iAddr = 0;
340 }
341
342 if( pRet->p4type==P4_SUBPROGRAM ){
343 int nByte = (p->nSub+1)*sizeof(SubProgram*);
344 int j;
345 for(j=0; j<p->nSub; j++){
346 if( p->apSub[j]==pRet->p4.pProgram ) break;
347 }
348 if( j==p->nSub ){
349 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
350 if( !p->apSub ){
351 pRet = 0;
352 }else{
353 p->apSub[p->nSub++] = pRet->p4.pProgram;
354 }
355 }
356 }
357 }
358
359 return pRet;
360}
361
362/*
danf3677212009-09-10 16:14:50 +0000363** Check if the program stored in the VM associated with pParse may
drhff738bc2009-09-24 00:09:58 +0000364** throw an ABORT exception (causing the statement, but not entire transaction
dan144926d2009-09-09 11:37:20 +0000365** to be rolled back). This condition is true if the main program or any
366** sub-programs contains any of the following:
367**
368** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
369** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
370** * OP_Destroy
371** * OP_VUpdate
372** * OP_VRename
dan32b09f22009-09-23 17:29:59 +0000373** * OP_FkCounter with P2==0 (immediate foreign key constraint)
dan144926d2009-09-09 11:37:20 +0000374**
danf3677212009-09-10 16:14:50 +0000375** Then check that the value of Parse.mayAbort is true if an
376** ABORT may be thrown, or false otherwise. Return true if it does
377** match, or false otherwise. This function is intended to be used as
378** part of an assert statement in the compiler. Similar to:
379**
380** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
dan144926d2009-09-09 11:37:20 +0000381*/
danf3677212009-09-10 16:14:50 +0000382int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
383 int hasAbort = 0;
dan144926d2009-09-09 11:37:20 +0000384 Op *pOp;
385 VdbeOpIter sIter;
386 memset(&sIter, 0, sizeof(sIter));
387 sIter.v = v;
388
389 while( (pOp = opIterNext(&sIter))!=0 ){
390 int opcode = pOp->opcode;
391 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
dan32b09f22009-09-23 17:29:59 +0000392#ifndef SQLITE_OMIT_FOREIGN_KEY
dan0ff297e2009-09-25 17:03:14 +0000393 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
dan32b09f22009-09-23 17:29:59 +0000394#endif
dan144926d2009-09-09 11:37:20 +0000395 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
drhd91c1a12013-02-09 13:58:25 +0000396 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
dan144926d2009-09-09 11:37:20 +0000397 ){
danf3677212009-09-10 16:14:50 +0000398 hasAbort = 1;
dan144926d2009-09-09 11:37:20 +0000399 break;
400 }
401 }
dan144926d2009-09-09 11:37:20 +0000402 sqlite3DbFree(v->db, sIter.apSub);
danf3677212009-09-10 16:14:50 +0000403
mistachkin48864df2013-03-21 21:20:32 +0000404 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
danf3677212009-09-10 16:14:50 +0000405 ** If malloc failed, then the while() loop above may not have iterated
406 ** through all opcodes and hasAbort may be set incorrectly. Return
407 ** true for this case to prevent the assert() in the callers frame
408 ** from failing. */
409 return ( v->db->mallocFailed || hasAbort==mayAbort );
dan144926d2009-09-09 11:37:20 +0000410}
drhff738bc2009-09-24 00:09:58 +0000411#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
dan144926d2009-09-09 11:37:20 +0000412
drh9a324642003-09-06 20:12:01 +0000413/*
drh9cbf3422008-01-17 16:22:13 +0000414** Loop through the program looking for P2 values that are negative
415** on jump instructions. Each such value is a label. Resolve the
416** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000417**
418** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000419**
drh13449892005-09-07 21:22:45 +0000420** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000421** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000422** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
drha6c2ed92009-11-14 23:22:23 +0000423**
424** The Op.opflags field is set on all opcodes.
drh76ff3a02004-09-24 22:32:30 +0000425*/
drh9cbf3422008-01-17 16:22:13 +0000426static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000427 int i;
dan165921a2009-08-28 18:53:45 +0000428 int nMaxArgs = *pMaxFuncArgs;
drh76ff3a02004-09-24 22:32:30 +0000429 Op *pOp;
drh73d5b8f2013-12-23 19:09:07 +0000430 Parse *pParse = p->pParse;
431 int *aLabel = pParse->aLabel;
drhad4a4b82008-11-05 16:37:34 +0000432 p->readOnly = 1;
drh1713afb2013-06-28 01:24:57 +0000433 p->bIsReader = 0;
drh76ff3a02004-09-24 22:32:30 +0000434 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000435 u8 opcode = pOp->opcode;
436
drh8c8a8c42013-08-06 07:45:08 +0000437 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
438 ** cases from this switch! */
439 switch( opcode ){
440 case OP_Function:
441 case OP_AggStep: {
442 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
443 break;
444 }
445 case OP_Transaction: {
446 if( pOp->p2!=0 ) p->readOnly = 0;
447 /* fall thru */
448 }
449 case OP_AutoCommit:
450 case OP_Savepoint: {
451 p->bIsReader = 1;
452 break;
453 }
dand9031542013-07-05 16:54:30 +0000454#ifndef SQLITE_OMIT_WAL
drh8c8a8c42013-08-06 07:45:08 +0000455 case OP_Checkpoint:
drh9e92a472013-06-27 17:40:30 +0000456#endif
drh8c8a8c42013-08-06 07:45:08 +0000457 case OP_Vacuum:
458 case OP_JournalMode: {
459 p->readOnly = 0;
460 p->bIsReader = 1;
461 break;
462 }
danielk1977182c4ba2007-06-27 15:53:34 +0000463#ifndef SQLITE_OMIT_VIRTUALTABLE
drh8c8a8c42013-08-06 07:45:08 +0000464 case OP_VUpdate: {
465 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
466 break;
467 }
468 case OP_VFilter: {
469 int n;
470 assert( p->nOp - i >= 3 );
471 assert( pOp[-1].opcode==OP_Integer );
472 n = pOp[-1].p1;
473 if( n>nMaxArgs ) nMaxArgs = n;
474 break;
475 }
danielk1977182c4ba2007-06-27 15:53:34 +0000476#endif
drh8c8a8c42013-08-06 07:45:08 +0000477 case OP_Next:
drhf93cd942013-11-21 03:12:25 +0000478 case OP_NextIfOpen:
drh8c8a8c42013-08-06 07:45:08 +0000479 case OP_SorterNext: {
480 pOp->p4.xAdvance = sqlite3BtreeNext;
481 pOp->p4type = P4_ADVANCE;
482 break;
483 }
drhf93cd942013-11-21 03:12:25 +0000484 case OP_Prev:
485 case OP_PrevIfOpen: {
drh8c8a8c42013-08-06 07:45:08 +0000486 pOp->p4.xAdvance = sqlite3BtreePrevious;
487 pOp->p4type = P4_ADVANCE;
488 break;
489 }
danielk1977bc04f852005-03-29 08:26:13 +0000490 }
danielk1977634f2982005-03-28 08:44:07 +0000491
drh8c8a8c42013-08-06 07:45:08 +0000492 pOp->opflags = sqlite3OpcodeProperty[opcode];
drha6c2ed92009-11-14 23:22:23 +0000493 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
drh73d5b8f2013-12-23 19:09:07 +0000494 assert( -1-pOp->p2<pParse->nLabel );
drhd2981512008-01-04 19:33:49 +0000495 pOp->p2 = aLabel[-1-pOp->p2];
496 }
drh76ff3a02004-09-24 22:32:30 +0000497 }
drh73d5b8f2013-12-23 19:09:07 +0000498 sqlite3DbFree(p->db, pParse->aLabel);
499 pParse->aLabel = 0;
500 pParse->nLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000501 *pMaxFuncArgs = nMaxArgs;
danc0537fe2013-06-28 19:41:43 +0000502 assert( p->bIsReader!=0 || p->btreeMask==0 );
drh76ff3a02004-09-24 22:32:30 +0000503}
504
505/*
drh9a324642003-09-06 20:12:01 +0000506** Return the address of the next instruction to be inserted.
507*/
danielk19774adee202004-05-08 08:23:19 +0000508int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000509 assert( p->magic==VDBE_MAGIC_INIT );
510 return p->nOp;
511}
512
dan65a7cd12009-09-01 12:16:01 +0000513/*
514** This function returns a pointer to the array of opcodes associated with
515** the Vdbe passed as the first argument. It is the callers responsibility
516** to arrange for the returned array to be eventually freed using the
517** vdbeFreeOpArray() function.
518**
519** Before returning, *pnOp is set to the number of entries in the returned
520** array. Also, *pnMaxArg is set to the larger of its current value and
521** the number of entries in the Vdbe.apArg[] array required to execute the
522** returned program.
523*/
dan165921a2009-08-28 18:53:45 +0000524VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
525 VdbeOp *aOp = p->aOp;
dan523a0872009-08-31 05:23:32 +0000526 assert( aOp && !p->db->mallocFailed );
dan65a7cd12009-09-01 12:16:01 +0000527
528 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
drhbdaec522011-04-04 00:14:43 +0000529 assert( p->btreeMask==0 );
dan65a7cd12009-09-01 12:16:01 +0000530
dan165921a2009-08-28 18:53:45 +0000531 resolveP2Values(p, pnMaxArg);
532 *pnOp = p->nOp;
533 p->aOp = 0;
534 return aOp;
535}
536
drh9a324642003-09-06 20:12:01 +0000537/*
538** Add a whole list of operations to the operation stack. Return the
539** address of the first operation added.
540*/
drh688852a2014-02-17 22:40:43 +0000541int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
drh9a324642003-09-06 20:12:01 +0000542 int addr;
543 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +0000544 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000545 return 0;
drh9a324642003-09-06 20:12:01 +0000546 }
547 addr = p->nOp;
drh7b746032009-06-26 12:15:22 +0000548 if( ALWAYS(nOp>0) ){
drh9a324642003-09-06 20:12:01 +0000549 int i;
drh905793e2004-02-21 13:31:09 +0000550 VdbeOpList const *pIn = aOp;
551 for(i=0; i<nOp; i++, pIn++){
552 int p2 = pIn->p2;
553 VdbeOp *pOut = &p->aOp[i+addr];
554 pOut->opcode = pIn->opcode;
555 pOut->p1 = pIn->p1;
drh4308e342013-11-11 16:55:52 +0000556 if( p2<0 ){
557 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
drh8558cde2008-01-05 05:20:10 +0000558 pOut->p2 = addr + ADDR(p2);
559 }else{
560 pOut->p2 = p2;
561 }
drh24003452008-01-03 01:28:59 +0000562 pOut->p3 = pIn->p3;
563 pOut->p4type = P4_NOTUSED;
564 pOut->p4.p = 0;
565 pOut->p5 = 0;
drhc7379ce2013-10-30 02:28:23 +0000566#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000567 pOut->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000568#endif
drh688852a2014-02-17 22:40:43 +0000569#ifdef SQLITE_VDBE_COVERAGE
570 pOut->iSrcLine = iLineno+i;
571#else
572 (void)iLineno;
573#endif
drhc7379ce2013-10-30 02:28:23 +0000574#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000575 if( p->db->flags & SQLITE_VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000577 }
578#endif
579 }
580 p->nOp += nOp;
581 }
582 return addr;
583}
584
585/*
586** Change the value of the P1 operand for a specific instruction.
587** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000588** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000589** few minor changes to the program.
590*/
drh88caeac2011-08-24 15:12:08 +0000591void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000592 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000593 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000594 p->aOp[addr].p1 = val;
595 }
596}
597
598/*
599** Change the value of the P2 operand for a specific instruction.
600** This routine is useful for setting a jump destination.
601*/
drh88caeac2011-08-24 15:12:08 +0000602void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000603 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000604 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000605 p->aOp[addr].p2 = val;
606 }
607}
608
drhd654be82005-09-20 17:42:23 +0000609/*
danielk19771f4aa332008-01-03 09:51:55 +0000610** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000611*/
drh88caeac2011-08-24 15:12:08 +0000612void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000613 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000614 if( ((u32)p->nOp)>addr ){
danielk1977207872a2008-01-03 07:54:23 +0000615 p->aOp[addr].p3 = val;
616 }
617}
618
619/*
drh35573352008-01-08 23:54:25 +0000620** Change the value of the P5 operand for the most recently
621** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000622*/
drh35573352008-01-08 23:54:25 +0000623void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
drh7b746032009-06-26 12:15:22 +0000624 assert( p!=0 );
625 if( p->aOp ){
drh35573352008-01-08 23:54:25 +0000626 assert( p->nOp>0 );
627 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000628 }
629}
630
631/*
drhf8875402006-03-17 13:56:34 +0000632** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000633** the address of the next instruction to be coded.
634*/
635void sqlite3VdbeJumpHere(Vdbe *p, int addr){
drh61019c72014-01-04 16:49:02 +0000636 sqlite3VdbeChangeP2(p, addr, p->nOp);
637 p->pParse->iFixedOp = p->nOp - 1;
drhd654be82005-09-20 17:42:23 +0000638}
drhb38ad992005-09-16 00:27:01 +0000639
drhb7f6f682006-07-08 17:06:43 +0000640
641/*
642** If the input FuncDef structure is ephemeral, then free it. If
643** the FuncDef is not ephermal, then do nothing.
644*/
drh633e6d52008-07-28 19:34:53 +0000645static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhd36e1042013-09-06 13:10:12 +0000646 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000647 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000648 }
649}
650
dand46def72010-07-24 11:28:28 +0000651static void vdbeFreeOpArray(sqlite3 *, Op *, int);
652
drhb38ad992005-09-16 00:27:01 +0000653/*
drh66a51672008-01-03 00:01:23 +0000654** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000655*/
drh633e6d52008-07-28 19:34:53 +0000656static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000657 if( p4 ){
dand46def72010-07-24 11:28:28 +0000658 assert( db );
drh66a51672008-01-03 00:01:23 +0000659 switch( p4type ){
660 case P4_REAL:
661 case P4_INT64:
drh66a51672008-01-03 00:01:23 +0000662 case P4_DYNAMIC:
drh2ec2fb22013-11-06 19:59:23 +0000663 case P4_INTARRAY: {
drh633e6d52008-07-28 19:34:53 +0000664 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000665 break;
666 }
drh2ec2fb22013-11-06 19:59:23 +0000667 case P4_KEYINFO: {
668 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
669 break;
670 }
drhb9755982010-07-24 16:34:37 +0000671 case P4_MPRINTF: {
drh7043db92010-07-26 12:38:12 +0000672 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
drhb9755982010-07-24 16:34:37 +0000673 break;
674 }
drh66a51672008-01-03 00:01:23 +0000675 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000676 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000677 break;
678 }
drh66a51672008-01-03 00:01:23 +0000679 case P4_MEM: {
drhc176c272010-07-26 13:57:59 +0000680 if( db->pnBytesFreed==0 ){
681 sqlite3ValueFree((sqlite3_value*)p4);
682 }else{
drhf37c68e2010-07-26 14:20:06 +0000683 Mem *p = (Mem*)p4;
684 sqlite3DbFree(db, p->zMalloc);
685 sqlite3DbFree(db, p);
drhc176c272010-07-26 13:57:59 +0000686 }
drhac1733d2005-09-17 17:58:22 +0000687 break;
688 }
danielk1977595a5232009-07-24 17:58:53 +0000689 case P4_VTAB : {
dand46def72010-07-24 11:28:28 +0000690 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
danielk1977595a5232009-07-24 17:58:53 +0000691 break;
692 }
drhb38ad992005-09-16 00:27:01 +0000693 }
694 }
695}
696
dan65a7cd12009-09-01 12:16:01 +0000697/*
698** Free the space allocated for aOp and any p4 values allocated for the
699** opcodes contained within. If aOp is not NULL it is assumed to contain
700** nOp entries.
701*/
dan165921a2009-08-28 18:53:45 +0000702static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
703 if( aOp ){
704 Op *pOp;
705 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
706 freeP4(db, pOp->p4type, pOp->p4.p);
drhc7379ce2013-10-30 02:28:23 +0000707#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000708 sqlite3DbFree(db, pOp->zComment);
709#endif
710 }
711 }
712 sqlite3DbFree(db, aOp);
713}
714
dan65a7cd12009-09-01 12:16:01 +0000715/*
dand19c9332010-07-26 12:05:17 +0000716** Link the SubProgram object passed as the second argument into the linked
717** list at Vdbe.pSubProgram. This list is used to delete all sub-program
718** objects when the VM is no longer required.
dan65a7cd12009-09-01 12:16:01 +0000719*/
dand19c9332010-07-26 12:05:17 +0000720void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
721 p->pNext = pVdbe->pProgram;
722 pVdbe->pProgram = p;
dan165921a2009-08-28 18:53:45 +0000723}
724
drh9a324642003-09-06 20:12:01 +0000725/*
drh48f2d3b2011-09-16 01:34:43 +0000726** Change the opcode at addr into OP_Noop
drhf8875402006-03-17 13:56:34 +0000727*/
drh48f2d3b2011-09-16 01:34:43 +0000728void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
drh7b746032009-06-26 12:15:22 +0000729 if( p->aOp ){
danielk197792d4d7a2007-05-04 12:05:56 +0000730 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000731 sqlite3 *db = p->db;
drh48f2d3b2011-09-16 01:34:43 +0000732 freeP4(db, pOp->p4type, pOp->p4.p);
733 memset(pOp, 0, sizeof(pOp[0]));
734 pOp->opcode = OP_Noop;
drh313619f2013-10-31 20:34:06 +0000735 if( addr==p->nOp-1 ) p->nOp--;
drhf8875402006-03-17 13:56:34 +0000736 }
737}
738
739/*
drh762c1c42014-01-02 19:35:30 +0000740** Remove the last opcode inserted
741*/
drh61019c72014-01-04 16:49:02 +0000742int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
743 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
744 sqlite3VdbeChangeToNoop(p, p->nOp-1);
745 return 1;
746 }else{
747 return 0;
748 }
drh762c1c42014-01-02 19:35:30 +0000749}
750
751/*
drh66a51672008-01-03 00:01:23 +0000752** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000753** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000754** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000755** few minor changes to the program.
756**
drh66a51672008-01-03 00:01:23 +0000757** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000758** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000759** A value of n==0 means copy bytes of zP4 up to and including the
760** first null byte. If n>0 then copy n+1 bytes of zP4.
danielk19771f55c052005-05-19 08:42:59 +0000761**
drh66a51672008-01-03 00:01:23 +0000762** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000763** to a string or structure that is guaranteed to exist for the lifetime of
764** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000765**
drh66a51672008-01-03 00:01:23 +0000766** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000767*/
drh66a51672008-01-03 00:01:23 +0000768void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000769 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000770 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000771 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000772 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000773 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000774 if( p->aOp==0 || db->mallocFailed ){
drh2ec2fb22013-11-06 19:59:23 +0000775 if( n!=P4_VTAB ){
drh633e6d52008-07-28 19:34:53 +0000776 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000777 }
danielk1977d5d56522005-03-16 12:15:20 +0000778 return;
779 }
drh7b746032009-06-26 12:15:22 +0000780 assert( p->nOp>0 );
drh91fd4d42008-01-19 20:11:25 +0000781 assert( addr<p->nOp );
782 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000783 addr = p->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000784 }
785 pOp = &p->aOp[addr];
drhfc5e5462012-12-03 17:04:40 +0000786 assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
drh633e6d52008-07-28 19:34:53 +0000787 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000788 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000789 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000790 /* Note: this cast is safe, because the origin data point was an int
791 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000792 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000793 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000794 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000795 pOp->p4.p = 0;
796 pOp->p4type = P4_NOTUSED;
797 }else if( n==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +0000798 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000799 pOp->p4type = P4_KEYINFO;
danielk1977595a5232009-07-24 17:58:53 +0000800 }else if( n==P4_VTAB ){
801 pOp->p4.p = (void*)zP4;
802 pOp->p4type = P4_VTAB;
803 sqlite3VtabLock((VTable *)zP4);
804 assert( ((VTable *)zP4)->db==p->db );
drh9a324642003-09-06 20:12:01 +0000805 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000806 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000807 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000808 }else{
drhea678832008-12-10 19:26:22 +0000809 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000810 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000811 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000812 }
813}
814
drh2ec2fb22013-11-06 19:59:23 +0000815/*
816** Set the P4 on the most recently added opcode to the KeyInfo for the
817** index given.
818*/
819void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
820 Vdbe *v = pParse->pVdbe;
821 assert( v!=0 );
822 assert( pIdx!=0 );
823 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
824 P4_KEYINFO);
825}
826
drhc7379ce2013-10-30 02:28:23 +0000827#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drhad6d9462004-09-19 02:15:24 +0000828/*
mistachkind5578432012-08-25 10:01:29 +0000829** Change the comment on the most recently coded instruction. Or
drh16ee60f2008-06-20 18:13:25 +0000830** insert a No-op and add the comment to that new instruction. This
831** makes the code easier to read during debugging. None of this happens
832** in a production build.
drhad6d9462004-09-19 02:15:24 +0000833*/
drhb07028f2011-10-14 21:49:18 +0000834static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
danielk197701256832007-04-18 14:24:32 +0000835 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000836 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000837 if( p->nOp ){
drhb07028f2011-10-14 21:49:18 +0000838 assert( p->aOp );
839 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
840 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
841 }
842}
843void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
844 va_list ap;
845 if( p ){
danielk1977dba01372008-01-05 18:44:29 +0000846 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000847 vdbeVComment(p, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000848 va_end(ap);
849 }
drhad6d9462004-09-19 02:15:24 +0000850}
drh16ee60f2008-06-20 18:13:25 +0000851void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
852 va_list ap;
drhb07028f2011-10-14 21:49:18 +0000853 if( p ){
854 sqlite3VdbeAddOp0(p, OP_Noop);
drh16ee60f2008-06-20 18:13:25 +0000855 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000856 vdbeVComment(p, zFormat, ap);
drh16ee60f2008-06-20 18:13:25 +0000857 va_end(ap);
858 }
859}
860#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000861
drh688852a2014-02-17 22:40:43 +0000862#ifdef SQLITE_VDBE_COVERAGE
863/*
864** Set the value if the iSrcLine field for the previously coded instruction.
865*/
866void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
867 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
868}
869#endif /* SQLITE_VDBE_COVERAGE */
870
drh9a324642003-09-06 20:12:01 +0000871/*
drh20411ea2009-05-29 19:00:12 +0000872** Return the opcode for a given address. If the address is -1, then
873** return the most recently inserted opcode.
874**
875** If a memory allocation error has occurred prior to the calling of this
876** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
drhf83dc1e2010-06-03 12:09:52 +0000877** is readable but not writable, though it is cast to a writable value.
878** The return of a dummy opcode allows the call to continue functioning
879** after a OOM fault without having to check to see if the return from
880** this routine is a valid pointer. But because the dummy.opcode is 0,
881** dummy will never be written to. This is verified by code inspection and
882** by running with Valgrind.
drh9a324642003-09-06 20:12:01 +0000883*/
danielk19774adee202004-05-08 08:23:19 +0000884VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drha0b75da2010-07-02 18:44:37 +0000885 /* C89 specifies that the constant "dummy" will be initialized to all
886 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
mistachkin0fe5f952011-09-14 18:19:08 +0000887 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
drh9a324642003-09-06 20:12:01 +0000888 assert( p->magic==VDBE_MAGIC_INIT );
drh37b89a02009-06-19 00:33:31 +0000889 if( addr<0 ){
drh37b89a02009-06-19 00:33:31 +0000890 addr = p->nOp - 1;
891 }
drh17435752007-08-16 04:30:38 +0000892 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000893 if( p->db->mallocFailed ){
drhf83dc1e2010-06-03 12:09:52 +0000894 return (VdbeOp*)&dummy;
drh20411ea2009-05-29 19:00:12 +0000895 }else{
896 return &p->aOp[addr];
897 }
drh9a324642003-09-06 20:12:01 +0000898}
899
drhc7379ce2013-10-30 02:28:23 +0000900#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
drh81316f82013-10-29 20:40:47 +0000901/*
drhf63552b2013-10-30 00:25:03 +0000902** Return an integer value for one of the parameters to the opcode pOp
903** determined by character c.
904*/
905static int translateP(char c, const Op *pOp){
906 if( c=='1' ) return pOp->p1;
907 if( c=='2' ) return pOp->p2;
908 if( c=='3' ) return pOp->p3;
909 if( c=='4' ) return pOp->p4.i;
910 return pOp->p5;
911}
912
drh81316f82013-10-29 20:40:47 +0000913/*
drh4eded602013-12-20 15:59:20 +0000914** Compute a string for the "comment" field of a VDBE opcode listing.
915**
916** The Synopsis: field in comments in the vdbe.c source file gets converted
917** to an extra string that is appended to the sqlite3OpcodeName(). In the
918** absence of other comments, this synopsis becomes the comment on the opcode.
919** Some translation occurs:
920**
921** "PX" -> "r[X]"
922** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
923** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
924** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
drh81316f82013-10-29 20:40:47 +0000925*/
drhf63552b2013-10-30 00:25:03 +0000926static int displayComment(
927 const Op *pOp, /* The opcode to be commented */
928 const char *zP4, /* Previously obtained value for P4 */
929 char *zTemp, /* Write result here */
930 int nTemp /* Space available in zTemp[] */
931){
drh81316f82013-10-29 20:40:47 +0000932 const char *zOpName;
933 const char *zSynopsis;
934 int nOpName;
935 int ii, jj;
936 zOpName = sqlite3OpcodeName(pOp->opcode);
937 nOpName = sqlite3Strlen30(zOpName);
938 if( zOpName[nOpName+1] ){
939 int seenCom = 0;
drhf63552b2013-10-30 00:25:03 +0000940 char c;
drh81316f82013-10-29 20:40:47 +0000941 zSynopsis = zOpName += nOpName + 1;
drhf63552b2013-10-30 00:25:03 +0000942 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
943 if( c=='P' ){
944 c = zSynopsis[++ii];
945 if( c=='4' ){
946 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
947 }else if( c=='X' ){
948 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
949 seenCom = 1;
drh81316f82013-10-29 20:40:47 +0000950 }else{
drhf63552b2013-10-30 00:25:03 +0000951 int v1 = translateP(c, pOp);
952 int v2;
953 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
954 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
955 ii += 3;
956 jj += sqlite3Strlen30(zTemp+jj);
957 v2 = translateP(zSynopsis[ii], pOp);
drh4eded602013-12-20 15:59:20 +0000958 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
959 ii += 2;
960 v2++;
961 }
962 if( v2>1 ){
963 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
964 }
drhf63552b2013-10-30 00:25:03 +0000965 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
966 ii += 4;
967 }
drh81316f82013-10-29 20:40:47 +0000968 }
969 jj += sqlite3Strlen30(zTemp+jj);
970 }else{
drhf63552b2013-10-30 00:25:03 +0000971 zTemp[jj++] = c;
drh81316f82013-10-29 20:40:47 +0000972 }
973 }
974 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
975 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
976 jj += sqlite3Strlen30(zTemp+jj);
977 }
978 if( jj<nTemp ) zTemp[jj] = 0;
979 }else if( pOp->zComment ){
980 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
981 jj = sqlite3Strlen30(zTemp);
982 }else{
983 zTemp[0] = 0;
984 jj = 0;
985 }
986 return jj;
987}
988#endif /* SQLITE_DEBUG */
989
990
drhb7f91642004-10-31 02:22:47 +0000991#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
992 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000993/*
drh66a51672008-01-03 00:01:23 +0000994** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000995** Use zTemp for any required temporary buffer space.
996*/
drh66a51672008-01-03 00:01:23 +0000997static char *displayP4(Op *pOp, char *zTemp, int nTemp){
998 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000999 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +00001000 switch( pOp->p4type ){
1001 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +00001002 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +00001003 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drhe1a022e2012-09-17 17:16:53 +00001004 assert( pKeyInfo->aSortOrder!=0 );
drh5b843aa2013-10-30 13:46:01 +00001005 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +00001006 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +00001007 for(j=0; j<pKeyInfo->nField; j++){
1008 CollSeq *pColl = pKeyInfo->aColl[j];
drh261d8a52012-12-08 21:36:26 +00001009 const char *zColl = pColl ? pColl->zName : "nil";
1010 int n = sqlite3Strlen30(zColl);
drh5b843aa2013-10-30 13:46:01 +00001011 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
1012 zColl = "B";
1013 n = 1;
1014 }
drh261d8a52012-12-08 21:36:26 +00001015 if( i+n>nTemp-6 ){
1016 memcpy(&zTemp[i],",...",4);
1017 break;
drhd3d39e92004-05-20 22:16:29 +00001018 }
drh261d8a52012-12-08 21:36:26 +00001019 zTemp[i++] = ',';
1020 if( pKeyInfo->aSortOrder[j] ){
1021 zTemp[i++] = '-';
1022 }
1023 memcpy(&zTemp[i], zColl, n+1);
1024 i += n;
drhd3d39e92004-05-20 22:16:29 +00001025 }
1026 zTemp[i++] = ')';
1027 zTemp[i] = 0;
1028 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +00001029 break;
1030 }
drh66a51672008-01-03 00:01:23 +00001031 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +00001032 CollSeq *pColl = pOp->p4.pColl;
drh5e6790c2013-11-12 20:18:14 +00001033 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +00001034 break;
1035 }
drh66a51672008-01-03 00:01:23 +00001036 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +00001037 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +00001038 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +00001039 break;
1040 }
drh66a51672008-01-03 00:01:23 +00001041 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +00001042 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +00001043 break;
1044 }
drh66a51672008-01-03 00:01:23 +00001045 case P4_INT32: {
1046 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +00001047 break;
1048 }
drh66a51672008-01-03 00:01:23 +00001049 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +00001050 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +00001051 break;
1052 }
drh66a51672008-01-03 00:01:23 +00001053 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +00001054 Mem *pMem = pOp->p4.pMem;
drhd4e70eb2008-01-02 00:34:36 +00001055 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +00001056 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +00001057 }else if( pMem->flags & MEM_Int ){
1058 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
1059 }else if( pMem->flags & MEM_Real ){
1060 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhb8475df2011-12-09 16:21:19 +00001061 }else if( pMem->flags & MEM_Null ){
1062 sqlite3_snprintf(nTemp, zTemp, "NULL");
drh56016892009-08-25 14:24:04 +00001063 }else{
1064 assert( pMem->flags & MEM_Blob );
1065 zP4 = "(blob)";
drhd4e70eb2008-01-02 00:34:36 +00001066 }
drh598f1342007-10-23 15:39:45 +00001067 break;
1068 }
drha967e882006-06-13 01:04:52 +00001069#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +00001070 case P4_VTAB: {
danielk1977595a5232009-07-24 17:58:53 +00001071 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
drh19146192006-06-26 19:10:32 +00001072 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +00001073 break;
1074 }
1075#endif
drh0acb7e42008-06-25 00:12:41 +00001076 case P4_INTARRAY: {
1077 sqlite3_snprintf(nTemp, zTemp, "intarray");
1078 break;
1079 }
dan165921a2009-08-28 18:53:45 +00001080 case P4_SUBPROGRAM: {
1081 sqlite3_snprintf(nTemp, zTemp, "program");
1082 break;
1083 }
drh4a6f3aa2011-08-28 00:19:26 +00001084 case P4_ADVANCE: {
1085 zTemp[0] = 0;
1086 break;
1087 }
drhd3d39e92004-05-20 22:16:29 +00001088 default: {
danielk19772dca4ac2008-01-03 11:50:29 +00001089 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +00001090 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +00001091 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +00001092 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +00001093 }
1094 }
1095 }
drh66a51672008-01-03 00:01:23 +00001096 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +00001097 return zP4;
drhd3d39e92004-05-20 22:16:29 +00001098}
drhb7f91642004-10-31 02:22:47 +00001099#endif
drhd3d39e92004-05-20 22:16:29 +00001100
drh900b31e2007-08-28 02:27:51 +00001101/*
drhd0679ed2007-08-28 22:24:34 +00001102** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
drh3ebaee92010-05-06 21:37:22 +00001103**
drhbdaec522011-04-04 00:14:43 +00001104** The prepared statements need to know in advance the complete set of
drhe4c88c02012-01-04 12:57:45 +00001105** attached databases that will be use. A mask of these databases
1106** is maintained in p->btreeMask. The p->lockMask value is the subset of
1107** p->btreeMask of databases that will require a lock.
drh900b31e2007-08-28 02:27:51 +00001108*/
drhfb982642007-08-30 01:19:59 +00001109void sqlite3VdbeUsesBtree(Vdbe *p, int i){
drhfcd71b62011-04-05 22:08:24 +00001110 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
danielk197700e13612008-11-17 19:18:54 +00001111 assert( i<(int)sizeof(p->btreeMask)*8 );
drhbdaec522011-04-04 00:14:43 +00001112 p->btreeMask |= ((yDbMask)1)<<i;
drhdc5b0472011-04-06 22:05:53 +00001113 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
1114 p->lockMask |= ((yDbMask)1)<<i;
1115 }
drh900b31e2007-08-28 02:27:51 +00001116}
1117
drhe54e0512011-04-05 17:31:56 +00001118#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001119/*
1120** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1121** this routine obtains the mutex associated with each BtShared structure
1122** that may be accessed by the VM passed as an argument. In doing so it also
1123** sets the BtShared.db member of each of the BtShared structures, ensuring
1124** that the correct busy-handler callback is invoked if required.
1125**
1126** If SQLite is not threadsafe but does support shared-cache mode, then
1127** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1128** of all of BtShared structures accessible via the database handle
1129** associated with the VM.
1130**
1131** If SQLite is not threadsafe and does not support shared-cache mode, this
1132** function is a no-op.
1133**
1134** The p->btreeMask field is a bitmask of all btrees that the prepared
1135** statement p will ever use. Let N be the number of bits in p->btreeMask
1136** corresponding to btrees that use shared cache. Then the runtime of
1137** this routine is N*N. But as N is rarely more than 1, this should not
1138** be a problem.
1139*/
1140void sqlite3VdbeEnter(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001141 int i;
1142 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001143 sqlite3 *db;
1144 Db *aDb;
1145 int nDb;
1146 if( p->lockMask==0 ) return; /* The common case */
1147 db = p->db;
1148 aDb = db->aDb;
1149 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001150 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001151 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001152 sqlite3BtreeEnter(aDb[i].pBt);
1153 }
1154 }
drhbdaec522011-04-04 00:14:43 +00001155}
drhe54e0512011-04-05 17:31:56 +00001156#endif
drhbdaec522011-04-04 00:14:43 +00001157
drhe54e0512011-04-05 17:31:56 +00001158#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001159/*
1160** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1161*/
1162void sqlite3VdbeLeave(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001163 int i;
1164 yDbMask mask;
drhdc5b0472011-04-06 22:05:53 +00001165 sqlite3 *db;
1166 Db *aDb;
1167 int nDb;
1168 if( p->lockMask==0 ) return; /* The common case */
1169 db = p->db;
1170 aDb = db->aDb;
1171 nDb = db->nDb;
drhbdaec522011-04-04 00:14:43 +00001172 for(i=0, mask=1; i<nDb; i++, mask += mask){
drhdc5b0472011-04-06 22:05:53 +00001173 if( i!=1 && (mask & p->lockMask)!=0 && 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 );
1223
1224 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1225 ** that takes advantage of the fact that the memory cell value is
1226 ** being set to NULL after releasing any dynamic resources.
1227 **
1228 ** The justification for duplicating code is that according to
1229 ** callgrind, this causes a certain test case to hit the CPU 4.7
1230 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1231 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1232 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1233 ** with no indexes using a single prepared INSERT statement, bind()
1234 ** and reset(). Inserts are grouped into a transaction.
1235 */
dan165921a2009-08-28 18:53:45 +00001236 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001237 sqlite3VdbeMemRelease(p);
1238 }else if( p->zMalloc ){
1239 sqlite3DbFree(db, p->zMalloc);
1240 p->zMalloc = 0;
1241 }
1242
drha5750cf2014-02-07 13:20:31 +00001243 p->flags = MEM_Undefined;
drh76ff3a02004-09-24 22:32:30 +00001244 }
danielk1977a7a8e142008-02-13 18:25:27 +00001245 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001246 }
1247}
1248
dan65a7cd12009-09-01 12:16:01 +00001249/*
1250** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1251** allocated by the OP_Program opcode in sqlite3VdbeExec().
1252*/
dan165921a2009-08-28 18:53:45 +00001253void sqlite3VdbeFrameDelete(VdbeFrame *p){
1254 int i;
1255 Mem *aMem = VdbeFrameMem(p);
1256 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1257 for(i=0; i<p->nChildCsr; i++){
1258 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1259 }
1260 releaseMemArray(aMem, p->nChildMem);
1261 sqlite3DbFree(p->v->db, p);
1262}
1263
drhb7f91642004-10-31 02:22:47 +00001264#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001265/*
drh9a324642003-09-06 20:12:01 +00001266** Give a listing of the program in the virtual machine.
1267**
danielk19774adee202004-05-08 08:23:19 +00001268** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001269** running the code, it invokes the callback once for each instruction.
1270** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001271**
1272** When p->explain==1, each instruction is listed. When
1273** p->explain==2, only OP_Explain instructions are listed and these
1274** are shown in a different format. p->explain==2 is used to implement
1275** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001276**
1277** When p->explain==1, first the main program is listed, then each of
1278** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001279*/
danielk19774adee202004-05-08 08:23:19 +00001280int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001281 Vdbe *p /* The VDBE */
1282){
drh5cfa5842009-12-31 20:35:08 +00001283 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001284 int nSub = 0; /* Number of sub-vdbes seen so far */
1285 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001286 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1287 sqlite3 *db = p->db; /* The database connection */
1288 int i; /* Loop counter */
1289 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001290 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001291
drh9a324642003-09-06 20:12:01 +00001292 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001293 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001294 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001295
drh9cbf3422008-01-17 16:22:13 +00001296 /* Even though this opcode does not use dynamic strings for
1297 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001298 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001299 */
dan165921a2009-08-28 18:53:45 +00001300 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001301 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001302
danielk19776c359f02008-11-21 16:58:03 +00001303 if( p->rc==SQLITE_NOMEM ){
1304 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1305 ** sqlite3_column_text16() failed. */
1306 db->mallocFailed = 1;
1307 return SQLITE_ERROR;
1308 }
1309
drh5cfa5842009-12-31 20:35:08 +00001310 /* When the number of output rows reaches nRow, that means the
1311 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1312 ** nRow is the sum of the number of rows in the main program, plus
1313 ** the sum of the number of rows in all trigger subprograms encountered
1314 ** so far. The nRow value will increase as new trigger subprograms are
1315 ** encountered, but p->pc will eventually catch up to nRow.
1316 */
dan165921a2009-08-28 18:53:45 +00001317 nRow = p->nOp;
1318 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001319 /* The first 8 memory cells are used for the result set. So we will
1320 ** commandeer the 9th cell to use as storage for an array of pointers
1321 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1322 ** cells. */
1323 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001324 pSub = &p->aMem[9];
1325 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001326 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1327 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001328 nSub = pSub->n/sizeof(Vdbe*);
1329 apSub = (SubProgram **)pSub->z;
1330 }
1331 for(i=0; i<nSub; i++){
1332 nRow += apSub[i]->nOp;
1333 }
1334 }
1335
drhecc92422005-09-10 16:46:12 +00001336 do{
1337 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001338 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1339 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001340 p->rc = SQLITE_OK;
1341 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001342 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001343 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001344 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001345 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001346 }else{
drh81316f82013-10-29 20:40:47 +00001347 char *zP4;
dan165921a2009-08-28 18:53:45 +00001348 Op *pOp;
1349 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001350 /* The output line number is small enough that we are still in the
1351 ** main program. */
dan165921a2009-08-28 18:53:45 +00001352 pOp = &p->aOp[i];
1353 }else{
drh5cfa5842009-12-31 20:35:08 +00001354 /* We are currently listing subprograms. Figure out which one and
1355 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001356 int j;
1357 i -= p->nOp;
1358 for(j=0; i>=apSub[j]->nOp; j++){
1359 i -= apSub[j]->nOp;
1360 }
1361 pOp = &apSub[j]->aOp[i];
1362 }
danielk19770d78bae2008-01-03 07:09:48 +00001363 if( p->explain==1 ){
1364 pMem->flags = MEM_Int;
drh1b27b8c2014-02-10 03:21:57 +00001365 pMem->memType = MEM_Int;
danielk19770d78bae2008-01-03 07:09:48 +00001366 pMem->u.i = i; /* Program counter */
1367 pMem++;
1368
1369 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001370 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
danielk19770d78bae2008-01-03 07:09:48 +00001371 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001372 pMem->n = sqlite3Strlen30(pMem->z);
drh1b27b8c2014-02-10 03:21:57 +00001373 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001374 pMem->enc = SQLITE_UTF8;
1375 pMem++;
dan165921a2009-08-28 18:53:45 +00001376
drh5cfa5842009-12-31 20:35:08 +00001377 /* When an OP_Program opcode is encounter (the only opcode that has
1378 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1379 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1380 ** has not already been seen.
1381 */
dan165921a2009-08-28 18:53:45 +00001382 if( pOp->p4type==P4_SUBPROGRAM ){
1383 int nByte = (nSub+1)*sizeof(SubProgram*);
1384 int j;
1385 for(j=0; j<nSub; j++){
1386 if( apSub[j]==pOp->p4.pProgram ) break;
1387 }
dan2b9ee772012-03-31 09:59:44 +00001388 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
dan165921a2009-08-28 18:53:45 +00001389 apSub = (SubProgram **)pSub->z;
1390 apSub[nSub++] = pOp->p4.pProgram;
1391 pSub->flags |= MEM_Blob;
1392 pSub->n = nSub*sizeof(SubProgram*);
1393 }
1394 }
danielk19770d78bae2008-01-03 07:09:48 +00001395 }
drheb2e1762004-05-27 01:53:56 +00001396
1397 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001398 pMem->u.i = pOp->p1; /* P1 */
drh1b27b8c2014-02-10 03:21:57 +00001399 pMem->memType = MEM_Int;
drheb2e1762004-05-27 01:53:56 +00001400 pMem++;
1401
1402 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001403 pMem->u.i = pOp->p2; /* P2 */
drh1b27b8c2014-02-10 03:21:57 +00001404 pMem->memType = MEM_Int;
drheb2e1762004-05-27 01:53:56 +00001405 pMem++;
1406
dan2ce22452010-11-08 19:01:16 +00001407 pMem->flags = MEM_Int;
1408 pMem->u.i = pOp->p3; /* P3 */
drh1b27b8c2014-02-10 03:21:57 +00001409 pMem->memType = MEM_Int;
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 }
1416 pMem->flags = MEM_Dyn|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 }
drh1b27b8c2014-02-10 03:21:57 +00001425 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001426 pMem++;
drheb2e1762004-05-27 01:53:56 +00001427
danielk19770d78bae2008-01-03 07:09:48 +00001428 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001429 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001430 assert( p->db->mallocFailed );
1431 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001432 }
1433 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001434 pMem->n = 2;
1435 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
drh1b27b8c2014-02-10 03:21:57 +00001436 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001437 pMem->enc = SQLITE_UTF8;
1438 pMem++;
1439
drhc7379ce2013-10-30 02:28:23 +00001440#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001441 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
1442 assert( p->db->mallocFailed );
1443 return SQLITE_ERROR;
drh52391cb2008-02-14 23:44:13 +00001444 }
drh81316f82013-10-29 20:40:47 +00001445 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
1446 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
drh1b27b8c2014-02-10 03:21:57 +00001447 pMem->memType = MEM_Str;
drh81316f82013-10-29 20:40:47 +00001448 pMem->enc = SQLITE_UTF8;
1449#else
1450 pMem->flags = MEM_Null; /* Comment */
drh1b27b8c2014-02-10 03:21:57 +00001451 pMem->memType = MEM_Null;
drh81316f82013-10-29 20:40:47 +00001452#endif
danielk19770d78bae2008-01-03 07:09:48 +00001453 }
1454
dan2ce22452010-11-08 19:01:16 +00001455 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001456 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001457 p->rc = SQLITE_OK;
1458 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001459 }
drh826fb5a2004-02-14 23:59:57 +00001460 return rc;
drh9a324642003-09-06 20:12:01 +00001461}
drhb7f91642004-10-31 02:22:47 +00001462#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001463
drh7c4ac0c2007-04-05 11:25:58 +00001464#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001465/*
drh3f7d4e42004-07-24 14:35:58 +00001466** Print the SQL that was used to generate a VDBE program.
1467*/
1468void sqlite3VdbePrintSql(Vdbe *p){
drh84e55a82013-11-13 17:58:23 +00001469 const char *z = 0;
1470 if( p->zSql ){
1471 z = p->zSql;
1472 }else if( p->nOp>=1 ){
1473 const VdbeOp *pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001474 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh84e55a82013-11-13 17:58:23 +00001475 z = pOp->p4.z;
1476 while( sqlite3Isspace(*z) ) z++;
1477 }
drh3f7d4e42004-07-24 14:35:58 +00001478 }
drh84e55a82013-11-13 17:58:23 +00001479 if( z ) printf("SQL: [%s]\n", z);
drh3f7d4e42004-07-24 14:35:58 +00001480}
drh7c4ac0c2007-04-05 11:25:58 +00001481#endif
drh3f7d4e42004-07-24 14:35:58 +00001482
drh602c2372007-03-01 00:29:13 +00001483#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1484/*
1485** Print an IOTRACE message showing SQL content.
1486*/
1487void sqlite3VdbeIOTraceSql(Vdbe *p){
1488 int nOp = p->nOp;
1489 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001490 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001491 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001492 pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001493 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001494 int i, j;
drh00a18e42007-08-13 11:10:34 +00001495 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001496 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001497 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001498 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001499 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001500 if( z[i-1]!=' ' ){
1501 z[j++] = ' ';
1502 }
1503 }else{
1504 z[j++] = z[i];
1505 }
1506 }
1507 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001508 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001509 }
1510}
1511#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1512
drhb2771ce2009-02-20 01:28:59 +00001513/*
drh4800b2e2009-12-08 15:35:22 +00001514** Allocate space from a fixed size buffer and return a pointer to
1515** that space. If insufficient space is available, return NULL.
1516**
1517** The pBuf parameter is the initial value of a pointer which will
1518** receive the new memory. pBuf is normally NULL. If pBuf is not
1519** NULL, it means that memory space has already been allocated and that
1520** this routine should not allocate any new memory. When pBuf is not
1521** NULL simply return pBuf. Only allocate new memory space when pBuf
1522** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001523**
1524** nByte is the number of bytes of space needed.
1525**
drh19875c82009-12-08 19:58:19 +00001526** *ppFrom points to available space and pEnd points to the end of the
1527** available space. When space is allocated, *ppFrom is advanced past
1528** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001529**
1530** *pnByte is a counter of the number of bytes of space that have failed
1531** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001532** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001533*/
drh4800b2e2009-12-08 15:35:22 +00001534static void *allocSpace(
1535 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001536 int nByte, /* Number of bytes to allocate */
1537 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001538 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001539 int *pnByte /* If allocation cannot be made, increment *pnByte */
1540){
drhea598cb2009-04-05 12:22:08 +00001541 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001542 if( pBuf ) return pBuf;
1543 nByte = ROUND8(nByte);
1544 if( &(*ppFrom)[nByte] <= pEnd ){
1545 pBuf = (void*)*ppFrom;
1546 *ppFrom += nByte;
1547 }else{
1548 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001549 }
drh4800b2e2009-12-08 15:35:22 +00001550 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001551}
drh602c2372007-03-01 00:29:13 +00001552
drh3f7d4e42004-07-24 14:35:58 +00001553/*
drh124c0b42011-06-01 18:15:55 +00001554** Rewind the VDBE back to the beginning in preparation for
1555** running it.
drh9a324642003-09-06 20:12:01 +00001556*/
drh124c0b42011-06-01 18:15:55 +00001557void sqlite3VdbeRewind(Vdbe *p){
1558#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1559 int i;
1560#endif
drh9a324642003-09-06 20:12:01 +00001561 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001562 assert( p->magic==VDBE_MAGIC_INIT );
1563
drhc16a03b2004-09-15 13:38:10 +00001564 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001565 */
drhc16a03b2004-09-15 13:38:10 +00001566 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001567
danielk197700e13612008-11-17 19:18:54 +00001568 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001569 p->magic = VDBE_MAGIC_RUN;
1570
drh124c0b42011-06-01 18:15:55 +00001571#ifdef SQLITE_DEBUG
1572 for(i=1; i<p->nMem; i++){
1573 assert( p->aMem[i].db==p->db );
1574 }
1575#endif
1576 p->pc = -1;
1577 p->rc = SQLITE_OK;
1578 p->errorAction = OE_Abort;
1579 p->magic = VDBE_MAGIC_RUN;
1580 p->nChange = 0;
1581 p->cacheCtr = 1;
1582 p->minWriteFileFormat = 255;
1583 p->iStatement = 0;
1584 p->nFkConstraint = 0;
1585#ifdef VDBE_PROFILE
1586 for(i=0; i<p->nOp; i++){
1587 p->aOp[i].cnt = 0;
1588 p->aOp[i].cycles = 0;
1589 }
1590#endif
1591}
1592
1593/*
1594** Prepare a virtual machine for execution for the first time after
1595** creating the virtual machine. This involves things such
1596** as allocating stack space and initializing the program counter.
1597** After the VDBE has be prepped, it can be executed by one or more
1598** calls to sqlite3VdbeExec().
1599**
1600** This function may be called exact once on a each virtual machine.
1601** After this routine is called the VM has been "packaged" and is ready
1602** to run. After this routine is called, futher calls to
1603** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1604** the Vdbe from the Parse object that helped generate it so that the
1605** the Vdbe becomes an independent entity and the Parse object can be
1606** destroyed.
1607**
1608** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1609** to its initial state after it has been run.
1610*/
1611void sqlite3VdbeMakeReady(
1612 Vdbe *p, /* The VDBE */
1613 Parse *pParse /* Parsing context */
1614){
1615 sqlite3 *db; /* The database connection */
1616 int nVar; /* Number of parameters */
1617 int nMem; /* Number of VM memory registers */
1618 int nCursor; /* Number of cursors required */
1619 int nArg; /* Number of arguments in subprograms */
dan1d8cb212011-12-09 13:24:16 +00001620 int nOnce; /* Number of OP_Once instructions */
drh124c0b42011-06-01 18:15:55 +00001621 int n; /* Loop counter */
1622 u8 *zCsr; /* Memory available for allocation */
1623 u8 *zEnd; /* First byte past allocated memory */
1624 int nByte; /* How much extra memory is needed */
1625
1626 assert( p!=0 );
1627 assert( p->nOp>0 );
1628 assert( pParse!=0 );
1629 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +00001630 assert( pParse==p->pParse );
drh124c0b42011-06-01 18:15:55 +00001631 db = p->db;
1632 assert( db->mallocFailed==0 );
1633 nVar = pParse->nVar;
1634 nMem = pParse->nMem;
1635 nCursor = pParse->nTab;
1636 nArg = pParse->nMaxArg;
dan1d8cb212011-12-09 13:24:16 +00001637 nOnce = pParse->nOnce;
drh20e226d2012-01-01 13:58:53 +00001638 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
drh124c0b42011-06-01 18:15:55 +00001639
danielk1977cd3e8f72008-03-25 09:47:35 +00001640 /* For each cursor required, also allocate a memory cell. Memory
1641 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1642 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001643 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001644 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1645 ** stores the blob of memory associated with cursor 1, etc.
1646 **
1647 ** See also: allocateCursor().
1648 */
1649 nMem += nCursor;
1650
danielk19776ab3a2e2009-02-19 14:39:25 +00001651 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001652 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001653 */
drh73d5b8f2013-12-23 19:09:07 +00001654 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1655 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001656
drh124c0b42011-06-01 18:15:55 +00001657 resolveP2Values(p, &nArg);
1658 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1659 if( pParse->explain && nMem<10 ){
1660 nMem = 10;
1661 }
1662 memset(zCsr, 0, zEnd-zCsr);
1663 zCsr += (zCsr - (u8*)0)&7;
1664 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001665 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001666
1667 /* Memory for registers, parameters, cursor, etc, is allocated in two
1668 ** passes. On the first pass, we try to reuse unused space at the
1669 ** end of the opcode array. If we are unable to satisfy all memory
1670 ** requirements by reusing the opcode array tail, then the second
1671 ** pass will fill in the rest using a fresh allocation.
1672 **
1673 ** This two-pass approach that reuses as much memory as possible from
1674 ** the leftover space at the end of the opcode array can significantly
1675 ** reduce the amount of memory held by a prepared statement.
1676 */
1677 do {
1678 nByte = 0;
1679 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1680 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1681 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1682 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1683 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1684 &zCsr, zEnd, &nByte);
drhb8475df2011-12-09 16:21:19 +00001685 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
drh124c0b42011-06-01 18:15:55 +00001686 if( nByte ){
1687 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001688 }
drh124c0b42011-06-01 18:15:55 +00001689 zCsr = p->pFree;
1690 zEnd = &zCsr[nByte];
1691 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001692
drhd2a56232013-01-28 19:00:20 +00001693 p->nCursor = nCursor;
dan1d8cb212011-12-09 13:24:16 +00001694 p->nOnceFlag = nOnce;
drh124c0b42011-06-01 18:15:55 +00001695 if( p->aVar ){
1696 p->nVar = (ynVar)nVar;
1697 for(n=0; n<nVar; n++){
1698 p->aVar[n].flags = MEM_Null;
1699 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001700 }
drh82a48512003-09-06 22:45:20 +00001701 }
drh124c0b42011-06-01 18:15:55 +00001702 if( p->azVar ){
1703 p->nzVar = pParse->nzVar;
1704 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1705 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001706 }
drh124c0b42011-06-01 18:15:55 +00001707 if( p->aMem ){
1708 p->aMem--; /* aMem[] goes from 1..nMem */
1709 p->nMem = nMem; /* not from 0..nMem-1 */
1710 for(n=1; n<=nMem; n++){
drha5750cf2014-02-07 13:20:31 +00001711 p->aMem[n].flags = MEM_Undefined;
drh124c0b42011-06-01 18:15:55 +00001712 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001713 }
drh9a324642003-09-06 20:12:01 +00001714 }
drh124c0b42011-06-01 18:15:55 +00001715 p->explain = pParse->explain;
1716 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001717}
1718
drh9a324642003-09-06 20:12:01 +00001719/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001720** Close a VDBE cursor and release all the resources that cursor
1721** happens to hold.
drh9a324642003-09-06 20:12:01 +00001722*/
drhdfe88ec2008-11-03 20:55:06 +00001723void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001724 if( pCx==0 ){
1725 return;
1726 }
dana20fde62011-07-12 14:28:05 +00001727 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001728 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001729 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001730 /* The pCx->pCursor will be close automatically, if it exists, by
1731 ** the call above. */
1732 }else if( pCx->pCursor ){
1733 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001734 }
drh9eff6162006-06-12 21:59:13 +00001735#ifndef SQLITE_OMIT_VIRTUALTABLE
1736 if( pCx->pVtabCursor ){
1737 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
drh5cc10232013-11-21 01:04:02 +00001738 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
danielk1977be718892006-06-23 08:05:19 +00001739 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001740 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001741 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001742 }
1743#endif
drh9a324642003-09-06 20:12:01 +00001744}
1745
dan65a7cd12009-09-01 12:16:01 +00001746/*
1747** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1748** is used, for example, when a trigger sub-program is halted to restore
1749** control to the main program.
1750*/
dan165921a2009-08-28 18:53:45 +00001751int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1752 Vdbe *v = pFrame->v;
dan1d8cb212011-12-09 13:24:16 +00001753 v->aOnceFlag = pFrame->aOnceFlag;
1754 v->nOnceFlag = pFrame->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00001755 v->aOp = pFrame->aOp;
1756 v->nOp = pFrame->nOp;
1757 v->aMem = pFrame->aMem;
1758 v->nMem = pFrame->nMem;
1759 v->apCsr = pFrame->apCsr;
1760 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001761 v->db->lastRowid = pFrame->lastRowid;
1762 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001763 return pFrame->pc;
1764}
1765
drh9a324642003-09-06 20:12:01 +00001766/*
drh5f82e3c2009-07-06 00:44:08 +00001767** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001768**
1769** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1770** cell array. This is necessary as the memory cell array may contain
1771** pointers to VdbeFrame objects, which may in turn contain pointers to
1772** open cursors.
drh9a324642003-09-06 20:12:01 +00001773*/
drh5f82e3c2009-07-06 00:44:08 +00001774static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001775 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001776 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001777 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1778 sqlite3VdbeFrameRestore(pFrame);
1779 }
1780 p->pFrame = 0;
1781 p->nFrame = 0;
1782
dan523a0872009-08-31 05:23:32 +00001783 if( p->apCsr ){
1784 int i;
1785 for(i=0; i<p->nCursor; i++){
1786 VdbeCursor *pC = p->apCsr[i];
1787 if( pC ){
1788 sqlite3VdbeFreeCursor(p, pC);
1789 p->apCsr[i] = 0;
1790 }
danielk1977be718892006-06-23 08:05:19 +00001791 }
drh9a324642003-09-06 20:12:01 +00001792 }
dan523a0872009-08-31 05:23:32 +00001793 if( p->aMem ){
1794 releaseMemArray(&p->aMem[1], p->nMem);
1795 }
dan27106572010-12-01 08:04:47 +00001796 while( p->pDelFrame ){
1797 VdbeFrame *pDel = p->pDelFrame;
1798 p->pDelFrame = pDel->pParent;
1799 sqlite3VdbeFrameDelete(pDel);
1800 }
dan0c547792013-07-18 17:12:08 +00001801
1802 /* Delete any auxdata allocations made by the VM */
1803 sqlite3VdbeDeleteAuxData(p, -1, 0);
1804 assert( p->pAuxData==0 );
drh9a324642003-09-06 20:12:01 +00001805}
1806
1807/*
drh9a324642003-09-06 20:12:01 +00001808** Clean up the VM after execution.
1809**
1810** This routine will automatically close any cursors, lists, and/or
1811** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001812** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001813*/
drhc890fec2008-08-01 20:10:08 +00001814static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001815 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001816
1817#ifdef SQLITE_DEBUG
1818 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1819 ** Vdbe.aMem[] arrays have already been cleaned up. */
1820 int i;
drhb8475df2011-12-09 16:21:19 +00001821 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1822 if( p->aMem ){
drha5750cf2014-02-07 13:20:31 +00001823 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
drhb8475df2011-12-09 16:21:19 +00001824 }
dan165921a2009-08-28 18:53:45 +00001825#endif
1826
drh633e6d52008-07-28 19:34:53 +00001827 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001828 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001829 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001830}
1831
1832/*
danielk197722322fd2004-05-25 23:35:17 +00001833** Set the number of result columns that will be returned by this SQL
1834** statement. This is now set at compile time, rather than during
1835** execution of the vdbe program so that sqlite3_column_count() can
1836** be called on an SQL statement before sqlite3_step().
1837*/
1838void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001839 Mem *pColName;
1840 int n;
drh633e6d52008-07-28 19:34:53 +00001841 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001842
drhc890fec2008-08-01 20:10:08 +00001843 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001844 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001845 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001846 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001847 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001848 if( p->aColName==0 ) return;
1849 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001850 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001851 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001852 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001853 }
danielk197722322fd2004-05-25 23:35:17 +00001854}
1855
1856/*
danielk19773cf86062004-05-26 10:11:05 +00001857** Set the name of the idx'th column to be returned by the SQL statement.
1858** zName must be a pointer to a nul terminated string.
1859**
1860** This call must be made after a call to sqlite3VdbeSetNumCols().
1861**
danielk197710fb7492008-10-31 10:53:22 +00001862** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1863** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1864** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001865*/
danielk197710fb7492008-10-31 10:53:22 +00001866int sqlite3VdbeSetColName(
1867 Vdbe *p, /* Vdbe being configured */
1868 int idx, /* Index of column zName applies to */
1869 int var, /* One of the COLNAME_* constants */
1870 const char *zName, /* Pointer to buffer containing name */
1871 void (*xDel)(void*) /* Memory management strategy for zName */
1872){
danielk19773cf86062004-05-26 10:11:05 +00001873 int rc;
1874 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001875 assert( idx<p->nResColumn );
1876 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001877 if( p->db->mallocFailed ){
1878 assert( !zName || xDel!=SQLITE_DYNAMIC );
1879 return SQLITE_NOMEM;
1880 }
drh76ff3a02004-09-24 22:32:30 +00001881 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001882 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001883 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001884 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001885 return rc;
1886}
1887
danielk197713adf8a2004-06-03 16:08:41 +00001888/*
1889** A read or write transaction may or may not be active on database handle
1890** db. If a transaction is active, commit it. If there is a
1891** write-transaction spanning more than one database file, this routine
1892** takes care of the master journal trickery.
1893*/
danielk19773e3a84d2008-08-01 17:37:40 +00001894static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001895 int i;
1896 int nTrans = 0; /* Number of databases with an active write-transaction */
1897 int rc = SQLITE_OK;
1898 int needXcommit = 0;
1899
shane36840fd2009-06-26 16:32:13 +00001900#ifdef SQLITE_OMIT_VIRTUALTABLE
1901 /* With this option, sqlite3VtabSync() is defined to be simply
1902 ** SQLITE_OK so p is not used.
1903 */
1904 UNUSED_PARAMETER(p);
1905#endif
1906
danielk19775bd270b2006-07-25 15:14:52 +00001907 /* Before doing anything else, call the xSync() callback for any
1908 ** virtual module tables written in this transaction. This has to
1909 ** be done before determining whether a master journal file is
1910 ** required, as an xSync() callback may add an attached database
1911 ** to the transaction.
1912 */
dan016f7812013-08-21 17:35:48 +00001913 rc = sqlite3VtabSync(db, p);
danielk19775bd270b2006-07-25 15:14:52 +00001914
1915 /* This loop determines (a) if the commit hook should be invoked and
1916 ** (b) how many database files have open write transactions, not
1917 ** including the temp database. (b) is important because if more than
1918 ** one database file has an open write transaction, a master journal
1919 ** file is required for an atomic commit.
1920 */
drhabfb62f2010-07-30 11:20:35 +00001921 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001922 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001923 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001924 needXcommit = 1;
1925 if( i!=1 ) nTrans++;
dan6b9bb592012-10-05 19:43:02 +00001926 sqlite3BtreeEnter(pBt);
drhabfb62f2010-07-30 11:20:35 +00001927 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
dan6b9bb592012-10-05 19:43:02 +00001928 sqlite3BtreeLeave(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001929 }
1930 }
drhabfb62f2010-07-30 11:20:35 +00001931 if( rc!=SQLITE_OK ){
1932 return rc;
1933 }
danielk197713adf8a2004-06-03 16:08:41 +00001934
1935 /* If there are any write-transactions at all, invoke the commit hook */
1936 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001937 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001938 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00001939 return SQLITE_CONSTRAINT_COMMITHOOK;
danielk197713adf8a2004-06-03 16:08:41 +00001940 }
1941 }
1942
danielk197740b38dc2004-06-26 08:38:24 +00001943 /* The simple case - no more than one database file (not counting the
1944 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001945 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001946 **
danielk197740b38dc2004-06-26 08:38:24 +00001947 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001948 ** string, it means the main database is :memory: or a temp file. In
1949 ** that case we do not support atomic multi-file commits, so use the
1950 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001951 */
drhea678832008-12-10 19:26:22 +00001952 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1953 || nTrans<=1
1954 ){
danielk197704103022009-02-03 16:51:24 +00001955 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001956 Btree *pBt = db->aDb[i].pBt;
1957 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001958 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001959 }
1960 }
1961
drh80e35f42007-03-30 14:06:34 +00001962 /* Do the commit only if all databases successfully complete phase 1.
1963 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1964 ** IO error while deleting or truncating a journal file. It is unlikely,
1965 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001966 */
1967 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1968 Btree *pBt = db->aDb[i].pBt;
1969 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001970 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001971 }
danielk1977979f38e2007-03-27 16:19:51 +00001972 }
1973 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001974 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001975 }
1976 }
1977
1978 /* The complex case - There is a multi-file write-transaction active.
1979 ** This requires a master journal file to ensure the transaction is
1980 ** committed atomicly.
1981 */
danielk197744ee5bf2005-05-27 09:41:12 +00001982#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001983 else{
danielk1977b4b47412007-08-17 15:53:36 +00001984 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001985 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001986 char *zMaster = 0; /* File-name for the master journal */
1987 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001988 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001989 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001990 int res;
drhf5808602011-12-16 00:33:04 +00001991 int retryCount = 0;
drh5c531a42011-12-16 01:21:31 +00001992 int nMainFile;
danielk197713adf8a2004-06-03 16:08:41 +00001993
1994 /* Select a master journal file name */
drh5c531a42011-12-16 01:21:31 +00001995 nMainFile = sqlite3Strlen30(zMainFile);
drh52bcde02012-01-03 14:50:45 +00001996 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
drh5c531a42011-12-16 01:21:31 +00001997 if( zMaster==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00001998 do {
drhdc5ea5c2008-12-10 17:19:59 +00001999 u32 iRandom;
drh84968c02011-12-16 15:11:39 +00002000 if( retryCount ){
2001 if( retryCount>100 ){
2002 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2003 sqlite3OsDelete(pVfs, zMaster, 0);
2004 break;
2005 }else if( retryCount==1 ){
2006 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2007 }
danielk197713adf8a2004-06-03 16:08:41 +00002008 }
drh84968c02011-12-16 15:11:39 +00002009 retryCount++;
danielk197713adf8a2004-06-03 16:08:41 +00002010 sqlite3_randomness(sizeof(iRandom), &iRandom);
drh5c531a42011-12-16 01:21:31 +00002011 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
drhf5808602011-12-16 00:33:04 +00002012 (iRandom>>8)&0xffffff, iRandom&0xff);
drhf5808602011-12-16 00:33:04 +00002013 /* The antipenultimate character of the master journal name must
2014 ** be "9" to avoid name collisions when using 8+3 filenames. */
drh5c531a42011-12-16 01:21:31 +00002015 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
drh81cc5162011-05-17 20:36:21 +00002016 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00002017 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2018 }while( rc==SQLITE_OK && res );
2019 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00002020 /* Open the master journal. */
2021 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2022 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2023 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2024 );
2025 }
danielk197713adf8a2004-06-03 16:08:41 +00002026 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002027 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002028 return rc;
2029 }
2030
2031 /* Write the name of each database file in the transaction into the new
2032 ** master journal file. If an error occurs at this point close
2033 ** and delete the master journal file. All the individual journal files
2034 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00002035 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00002036 */
danielk19771e536952007-08-16 10:09:01 +00002037 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002038 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002039 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00002040 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00002041 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00002042 continue; /* Ignore TEMP and :memory: databases */
2043 }
drh8c96a6e2010-08-31 01:09:15 +00002044 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00002045 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2046 needSync = 1;
2047 }
drhea678832008-12-10 19:26:22 +00002048 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2049 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00002050 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00002051 sqlite3OsCloseFree(pMaster);
2052 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002053 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002054 return rc;
2055 }
2056 }
2057 }
2058
danielk19779663b8f2007-08-24 11:52:28 +00002059 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2060 ** flag is set this is not required.
2061 */
danielk1977bea2a942009-01-20 17:06:27 +00002062 if( needSync
2063 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2064 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2065 ){
danielk1977fee2d252007-08-18 10:59:19 +00002066 sqlite3OsCloseFree(pMaster);
2067 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002068 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00002069 return rc;
2070 }
drhc9e06862004-06-09 20:03:08 +00002071
danielk197713adf8a2004-06-03 16:08:41 +00002072 /* Sync all the db files involved in the transaction. The same call
2073 ** sets the master journal pointer in each individual journal. If
2074 ** an error occurs here, do not delete the master journal file.
2075 **
drh80e35f42007-03-30 14:06:34 +00002076 ** If the error occurs during the first call to
2077 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2078 ** master journal file will be orphaned. But we cannot delete it,
2079 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00002080 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00002081 */
danielk19775bd270b2006-07-25 15:14:52 +00002082 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002083 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002084 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002085 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002086 }
2087 }
danielk1977fee2d252007-08-18 10:59:19 +00002088 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00002089 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00002090 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002091 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00002092 return rc;
2093 }
danielk197713adf8a2004-06-03 16:08:41 +00002094
danielk1977962398d2004-06-14 09:35:16 +00002095 /* Delete the master journal file. This commits the transaction. After
2096 ** doing this the directory is synced again before any individual
2097 ** transaction files are deleted.
2098 */
danielk1977fee2d252007-08-18 10:59:19 +00002099 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00002100 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00002101 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00002102 if( rc ){
2103 return rc;
2104 }
danielk197713adf8a2004-06-03 16:08:41 +00002105
2106 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00002107 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2108 ** deleting or truncating journals. If something goes wrong while
2109 ** this is happening we don't really care. The integrity of the
2110 ** transaction is already guaranteed, but some stray 'cold' journals
2111 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00002112 */
danielk1977979f38e2007-03-27 16:19:51 +00002113 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002114 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00002115 for(i=0; i<db->nDb; i++){
2116 Btree *pBt = db->aDb[i].pBt;
2117 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002118 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00002119 }
2120 }
danielk19772d1d86f2008-06-20 14:59:51 +00002121 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00002122 enable_simulated_io_errors();
2123
danielk1977f9e7dda2006-06-16 16:08:53 +00002124 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002125 }
danielk197744ee5bf2005-05-27 09:41:12 +00002126#endif
danielk1977026d2702004-06-14 13:14:59 +00002127
drh2ac3ee92004-06-07 16:27:46 +00002128 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002129}
2130
danielk19771d850a72004-05-31 08:26:49 +00002131/*
drh4f7d3a52013-06-27 23:54:02 +00002132** This routine checks that the sqlite3.nVdbeActive count variable
danielk19771d850a72004-05-31 08:26:49 +00002133** matches the number of vdbe's in the list sqlite3.pVdbe that are
2134** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00002135** This is an internal self-check only - it is not an essential processing
2136** step.
danielk19771d850a72004-05-31 08:26:49 +00002137**
2138** This is a no-op if NDEBUG is defined.
2139*/
2140#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00002141static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00002142 Vdbe *p;
2143 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00002144 int nWrite = 0;
drh4f7d3a52013-06-27 23:54:02 +00002145 int nRead = 0;
danielk19771d850a72004-05-31 08:26:49 +00002146 p = db->pVdbe;
2147 while( p ){
drh92f02c32004-09-02 14:57:08 +00002148 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002149 cnt++;
drhad4a4b82008-11-05 16:37:34 +00002150 if( p->readOnly==0 ) nWrite++;
drh1713afb2013-06-28 01:24:57 +00002151 if( p->bIsReader ) nRead++;
danielk19771d850a72004-05-31 08:26:49 +00002152 }
2153 p = p->pNext;
2154 }
drh4f7d3a52013-06-27 23:54:02 +00002155 assert( cnt==db->nVdbeActive );
2156 assert( nWrite==db->nVdbeWrite );
2157 assert( nRead==db->nVdbeRead );
danielk19771d850a72004-05-31 08:26:49 +00002158}
2159#else
2160#define checkActiveVdbeCnt(x)
2161#endif
2162
danielk19773cf86062004-05-26 10:11:05 +00002163/*
danielk1977bd434552009-03-18 10:33:00 +00002164** If the Vdbe passed as the first argument opened a statement-transaction,
2165** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2166** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2167** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
drhf7b54962013-05-28 12:11:54 +00002168** statement transaction is committed.
danielk1977bd434552009-03-18 10:33:00 +00002169**
2170** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2171** Otherwise SQLITE_OK.
2172*/
2173int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002174 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002175 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002176
danielk1977e4948172009-07-17 17:25:43 +00002177 /* If p->iStatement is greater than zero, then this Vdbe opened a
2178 ** statement transaction that should be closed here. The only exception
mistachkin48864df2013-03-21 21:20:32 +00002179 ** is that an IO error may have occurred, causing an emergency rollback.
danielk1977e4948172009-07-17 17:25:43 +00002180 ** In this case (db->nStatement==0), and there is nothing to do.
2181 */
2182 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002183 int i;
2184 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002185
2186 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2187 assert( db->nStatement>0 );
2188 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2189
2190 for(i=0; i<db->nDb; i++){
2191 int rc2 = SQLITE_OK;
2192 Btree *pBt = db->aDb[i].pBt;
2193 if( pBt ){
2194 if( eOp==SAVEPOINT_ROLLBACK ){
2195 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2196 }
2197 if( rc2==SQLITE_OK ){
2198 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2199 }
2200 if( rc==SQLITE_OK ){
2201 rc = rc2;
2202 }
2203 }
2204 }
2205 db->nStatement--;
2206 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002207
dana311b802011-04-26 19:21:34 +00002208 if( rc==SQLITE_OK ){
2209 if( eOp==SAVEPOINT_ROLLBACK ){
2210 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2211 }
2212 if( rc==SQLITE_OK ){
2213 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2214 }
2215 }
2216
dan1da40a32009-09-19 17:00:31 +00002217 /* If the statement transaction is being rolled back, also restore the
2218 ** database handles deferred constraint counter to the value it had when
2219 ** the statement transaction was opened. */
2220 if( eOp==SAVEPOINT_ROLLBACK ){
2221 db->nDeferredCons = p->nStmtDefCons;
drh648e2642013-07-11 15:03:32 +00002222 db->nDeferredImmCons = p->nStmtDefImmCons;
dan1da40a32009-09-19 17:00:31 +00002223 }
danielk1977bd434552009-03-18 10:33:00 +00002224 }
2225 return rc;
2226}
2227
2228/*
dan1da40a32009-09-19 17:00:31 +00002229** This function is called when a transaction opened by the database
2230** handle associated with the VM passed as an argument is about to be
2231** committed. If there are outstanding deferred foreign key constraint
2232** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2233**
2234** If there are outstanding FK violations and this function returns
drhd91c1a12013-02-09 13:58:25 +00002235** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2236** and write an error message to it. Then return SQLITE_ERROR.
dan1da40a32009-09-19 17:00:31 +00002237*/
2238#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002239int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002240 sqlite3 *db = p->db;
drh648e2642013-07-11 15:03:32 +00002241 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2242 || (!deferred && p->nFkConstraint>0)
2243 ){
drhd91c1a12013-02-09 13:58:25 +00002244 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan32b09f22009-09-23 17:29:59 +00002245 p->errorAction = OE_Abort;
drhf9c8ce32013-11-05 13:33:55 +00002246 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
dan1da40a32009-09-19 17:00:31 +00002247 return SQLITE_ERROR;
2248 }
2249 return SQLITE_OK;
2250}
2251#endif
2252
2253/*
drh92f02c32004-09-02 14:57:08 +00002254** This routine is called the when a VDBE tries to halt. If the VDBE
2255** has made changes and is in autocommit mode, then commit those
2256** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002257**
drh92f02c32004-09-02 14:57:08 +00002258** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002259** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2260** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002261**
2262** Return an error code. If the commit could not complete because of
2263** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2264** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002265*/
drhff0587c2007-08-29 17:43:19 +00002266int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002267 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002268 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002269
2270 /* This function contains the logic that determines if a statement or
2271 ** transaction will be committed or rolled back as a result of the
2272 ** execution of this virtual machine.
2273 **
drh71b890a2007-10-03 15:30:52 +00002274 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002275 **
drh71b890a2007-10-03 15:30:52 +00002276 ** SQLITE_NOMEM
2277 ** SQLITE_IOERR
2278 ** SQLITE_FULL
2279 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002280 **
drh71b890a2007-10-03 15:30:52 +00002281 ** Then the internal cache might have been left in an inconsistent
2282 ** state. We need to rollback the statement transaction, if there is
2283 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002284 */
drh9a324642003-09-06 20:12:01 +00002285
drh17435752007-08-16 04:30:38 +00002286 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002287 p->rc = SQLITE_NOMEM;
2288 }
drh6e856bc2011-12-09 18:06:44 +00002289 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
drh5f82e3c2009-07-06 00:44:08 +00002290 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002291 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002292 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002293 }
danielk19771d850a72004-05-31 08:26:49 +00002294 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002295
danc0537fe2013-06-28 19:41:43 +00002296 /* No commit or rollback needed if the program never started or if the
2297 ** SQL statement does not read or write a database file. */
2298 if( p->pc>=0 && p->bIsReader ){
drhaac2f552006-09-23 21:44:23 +00002299 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002300 int eStatementOp = 0;
2301 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002302
2303 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002304 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002305
drh71b890a2007-10-03 15:30:52 +00002306 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002307 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002308 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002309 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002310 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002311 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002312 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2313 ** no rollback is necessary. Otherwise, at least a savepoint
2314 ** transaction must be rolled back to restore the database to a
2315 ** consistent state.
2316 **
2317 ** Even if the statement is read-only, it is important to perform
2318 ** a statement or transaction rollback operation. If the error
mistachkin48864df2013-03-21 21:20:32 +00002319 ** occurred while writing to the journal, sub-journal or database
dan5653e4d2010-08-12 11:25:47 +00002320 ** file as part of an effort to free up cache space (see function
2321 ** pagerStress() in pager.c), the rollback is required to restore
2322 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002323 */
drhad4a4b82008-11-05 16:37:34 +00002324 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002325 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002326 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002327 }else{
2328 /* We are forced to roll back the active transaction. Before doing
2329 ** so, abort any other statements this handle currently has active.
2330 */
drh21021a52012-02-13 17:01:51 +00002331 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002332 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002333 db->autoCommit = 1;
2334 }
danielk1977261919c2005-12-06 12:52:59 +00002335 }
2336 }
dan32b09f22009-09-23 17:29:59 +00002337
2338 /* Check for immediate foreign key violations. */
2339 if( p->rc==SQLITE_OK ){
2340 sqlite3VdbeCheckFk(p, 0);
2341 }
danielk197707cb5602006-01-20 10:55:05 +00002342
danielk1977bd434552009-03-18 10:33:00 +00002343 /* If the auto-commit flag is set and this is the only active writer
2344 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002345 **
2346 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002347 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002348 */
danielk1977093e0f62008-11-13 18:00:14 +00002349 if( !sqlite3VtabInSync(db)
2350 && db->autoCommit
drh4f7d3a52013-06-27 23:54:02 +00002351 && db->nVdbeWrite==(p->readOnly==0)
danielk1977093e0f62008-11-13 18:00:14 +00002352 ){
danielk197707cb5602006-01-20 10:55:05 +00002353 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002354 rc = sqlite3VdbeCheckFk(p, 1);
2355 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002356 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002357 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002358 return SQLITE_ERROR;
2359 }
drhd91c1a12013-02-09 13:58:25 +00002360 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan19611b12011-01-24 16:00:58 +00002361 }else{
2362 /* The auto-commit flag is true, the vdbe program was successful
2363 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2364 ** key constraints to hold up the transaction. This means a commit
2365 ** is required. */
2366 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002367 }
dan19611b12011-01-24 16:00:58 +00002368 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002369 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002370 return SQLITE_BUSY;
2371 }else if( rc!=SQLITE_OK ){
2372 p->rc = rc;
drh0f198a72012-02-13 16:43:16 +00002373 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002374 }else{
dan1da40a32009-09-19 17:00:31 +00002375 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00002376 db->nDeferredImmCons = 0;
2377 db->flags &= ~SQLITE_DeferFKs;
danielk197707cb5602006-01-20 10:55:05 +00002378 sqlite3CommitInternalChanges(db);
2379 }
2380 }else{
drh0f198a72012-02-13 16:43:16 +00002381 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002382 }
danielk1977bd434552009-03-18 10:33:00 +00002383 db->nStatement = 0;
2384 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002385 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002386 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002387 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002388 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002389 }else{
drh21021a52012-02-13 17:01:51 +00002390 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002391 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002392 db->autoCommit = 1;
2393 }
danielk19771d850a72004-05-31 08:26:49 +00002394 }
danielk197707cb5602006-01-20 10:55:05 +00002395
danielk1977bd434552009-03-18 10:33:00 +00002396 /* If eStatementOp is non-zero, then a statement transaction needs to
2397 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2398 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002399 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2400 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002401 */
danielk1977bd434552009-03-18 10:33:00 +00002402 if( eStatementOp ){
2403 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002404 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002405 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002406 p->rc = rc;
2407 sqlite3DbFree(db, p->zErrMsg);
2408 p->zErrMsg = 0;
2409 }
drh21021a52012-02-13 17:01:51 +00002410 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
dan40ad9d22010-06-03 09:17:38 +00002411 sqlite3CloseSavepoints(db);
2412 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002413 }
danielk197777d83ba2004-05-31 10:08:14 +00002414 }
danielk197707cb5602006-01-20 10:55:05 +00002415
danielk1977bd434552009-03-18 10:33:00 +00002416 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2417 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002418 */
drh6be240e2009-07-14 02:33:02 +00002419 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002420 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002421 sqlite3VdbeSetChanges(db, p->nChange);
2422 }else{
2423 sqlite3VdbeSetChanges(db, 0);
2424 }
2425 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002426 }
drhff0587c2007-08-29 17:43:19 +00002427
2428 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002429 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002430 }
danielk19771d850a72004-05-31 08:26:49 +00002431
danielk197765fd59f2006-06-24 11:51:33 +00002432 /* We have successfully halted and closed the VM. Record this fact. */
2433 if( p->pc>=0 ){
drh4f7d3a52013-06-27 23:54:02 +00002434 db->nVdbeActive--;
2435 if( !p->readOnly ) db->nVdbeWrite--;
drh1713afb2013-06-28 01:24:57 +00002436 if( p->bIsReader ) db->nVdbeRead--;
drh4f7d3a52013-06-27 23:54:02 +00002437 assert( db->nVdbeActive>=db->nVdbeRead );
2438 assert( db->nVdbeRead>=db->nVdbeWrite );
2439 assert( db->nVdbeWrite>=0 );
drh9a324642003-09-06 20:12:01 +00002440 }
drh92f02c32004-09-02 14:57:08 +00002441 p->magic = VDBE_MAGIC_HALT;
2442 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002443 if( p->db->mallocFailed ){
2444 p->rc = SQLITE_NOMEM;
2445 }
danielk19771d850a72004-05-31 08:26:49 +00002446
danielk1977404ca072009-03-16 13:19:36 +00002447 /* If the auto-commit flag is set to true, then any locks that were held
2448 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2449 ** to invoke any required unlock-notify callbacks.
2450 */
2451 if( db->autoCommit ){
2452 sqlite3ConnectionUnlocked(db);
2453 }
2454
drh4f7d3a52013-06-27 23:54:02 +00002455 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002456 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002457}
drh4cf7c7f2007-08-28 23:28:07 +00002458
drh92f02c32004-09-02 14:57:08 +00002459
2460/*
drh3c23a882007-01-09 14:01:13 +00002461** Each VDBE holds the result of the most recent sqlite3_step() call
2462** in p->rc. This routine sets that result back to SQLITE_OK.
2463*/
2464void sqlite3VdbeResetStepResult(Vdbe *p){
2465 p->rc = SQLITE_OK;
2466}
2467
2468/*
dan029ead62011-10-27 15:19:58 +00002469** Copy the error code and error message belonging to the VDBE passed
2470** as the first argument to its database handle (so that they will be
2471** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2472**
2473** This function does not clear the VDBE error code or message, just
2474** copies them to the database handle.
2475*/
2476int sqlite3VdbeTransferError(Vdbe *p){
2477 sqlite3 *db = p->db;
2478 int rc = p->rc;
2479 if( p->zErrMsg ){
drh81bdd6d2011-10-29 01:33:24 +00002480 u8 mallocFailed = db->mallocFailed;
dan029ead62011-10-27 15:19:58 +00002481 sqlite3BeginBenignMalloc();
drha3cc0072013-12-13 16:23:55 +00002482 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
dan029ead62011-10-27 15:19:58 +00002483 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2484 sqlite3EndBenignMalloc();
drh81bdd6d2011-10-29 01:33:24 +00002485 db->mallocFailed = mallocFailed;
dan029ead62011-10-27 15:19:58 +00002486 db->errCode = rc;
2487 }else{
2488 sqlite3Error(db, rc, 0);
2489 }
2490 return rc;
2491}
2492
danac455932012-11-26 19:50:41 +00002493#ifdef SQLITE_ENABLE_SQLLOG
2494/*
2495** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2496** invoke it.
2497*/
2498static void vdbeInvokeSqllog(Vdbe *v){
2499 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2500 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2501 assert( v->db->init.busy==0 );
2502 if( zExpanded ){
2503 sqlite3GlobalConfig.xSqllog(
2504 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2505 );
2506 sqlite3DbFree(v->db, zExpanded);
2507 }
2508 }
2509}
2510#else
2511# define vdbeInvokeSqllog(x)
2512#endif
2513
dan029ead62011-10-27 15:19:58 +00002514/*
drh92f02c32004-09-02 14:57:08 +00002515** Clean up a VDBE after execution but do not delete the VDBE just yet.
2516** Write any error messages into *pzErrMsg. Return the result code.
2517**
2518** After this routine is run, the VDBE should be ready to be executed
2519** again.
2520**
2521** To look at it another way, this routine resets the state of the
2522** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2523** VDBE_MAGIC_INIT.
2524*/
drhc890fec2008-08-01 20:10:08 +00002525int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002526 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002527 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002528
2529 /* If the VM did not run to completion or if it encountered an
2530 ** error, then it might not have been halted properly. So halt
2531 ** it now.
2532 */
2533 sqlite3VdbeHalt(p);
2534
drhfb7e7652005-01-24 00:28:42 +00002535 /* If the VDBE has be run even partially, then transfer the error code
2536 ** and error message from the VDBE into the main database structure. But
2537 ** if the VDBE has just been set to run but has not actually executed any
2538 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002539 */
drhfb7e7652005-01-24 00:28:42 +00002540 if( p->pc>=0 ){
danac455932012-11-26 19:50:41 +00002541 vdbeInvokeSqllog(p);
dan029ead62011-10-27 15:19:58 +00002542 sqlite3VdbeTransferError(p);
2543 sqlite3DbFree(db, p->zErrMsg);
2544 p->zErrMsg = 0;
drh4611d922010-02-25 14:47:01 +00002545 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002546 }else if( p->rc && p->expired ){
2547 /* The expired flag was set on the VDBE before the first call
2548 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2549 ** called), set the database error in this case as well.
2550 */
drha3cc0072013-12-13 16:23:55 +00002551 sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002552 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002553 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002554 }
2555
2556 /* Reclaim all memory used by the VDBE
2557 */
drhc890fec2008-08-01 20:10:08 +00002558 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002559
2560 /* Save profiling information from this VDBE run.
2561 */
drh9a324642003-09-06 20:12:01 +00002562#ifdef VDBE_PROFILE
2563 {
2564 FILE *out = fopen("vdbe_profile.out", "a");
2565 if( out ){
2566 int i;
2567 fprintf(out, "---- ");
2568 for(i=0; i<p->nOp; i++){
2569 fprintf(out, "%02x", p->aOp[i].opcode);
2570 }
2571 fprintf(out, "\n");
drh2926f962014-02-17 01:13:28 +00002572 if( p->zSql ){
2573 char c, pc = 0;
2574 fprintf(out, "-- ");
2575 for(i=0; (c = p->zSql[i])!=0; i++){
2576 if( pc=='\n' ) fprintf(out, "-- ");
2577 putc(c, out);
2578 pc = c;
2579 }
2580 if( pc!='\n' ) fprintf(out, "\n");
2581 }
drh9a324642003-09-06 20:12:01 +00002582 for(i=0; i<p->nOp; i++){
drh15ab9412014-02-24 14:24:01 +00002583 char zHdr[100];
2584 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
drh9a324642003-09-06 20:12:01 +00002585 p->aOp[i].cnt,
2586 p->aOp[i].cycles,
2587 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2588 );
drh15ab9412014-02-24 14:24:01 +00002589 fprintf(out, "%s", zHdr);
danielk19774adee202004-05-08 08:23:19 +00002590 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002591 }
2592 fclose(out);
2593 }
2594 }
2595#endif
drh7fa20922013-09-17 23:36:33 +00002596 p->iCurrentTime = 0;
drh9a324642003-09-06 20:12:01 +00002597 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002598 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002599}
drh92f02c32004-09-02 14:57:08 +00002600
drh9a324642003-09-06 20:12:01 +00002601/*
2602** Clean up and delete a VDBE after execution. Return an integer which is
2603** the result code. Write any error message text into *pzErrMsg.
2604*/
danielk19779e6db7d2004-06-21 08:18:51 +00002605int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002606 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002607 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002608 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002609 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002610 }
danielk19774adee202004-05-08 08:23:19 +00002611 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002612 return rc;
2613}
2614
2615/*
dan0c547792013-07-18 17:12:08 +00002616** If parameter iOp is less than zero, then invoke the destructor for
2617** all auxiliary data pointers currently cached by the VM passed as
2618** the first argument.
2619**
2620** Or, if iOp is greater than or equal to zero, then the destructor is
2621** only invoked for those auxiliary data pointers created by the user
2622** function invoked by the OP_Function opcode at instruction iOp of
2623** VM pVdbe, and only then if:
2624**
2625** * the associated function parameter is the 32nd or later (counting
2626** from left to right), or
2627**
2628** * the corresponding bit in argument mask is clear (where the first
2629** function parameter corrsponds to bit 0 etc.).
drhf92c7ff2004-06-19 15:40:23 +00002630*/
dan0c547792013-07-18 17:12:08 +00002631void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2632 AuxData **pp = &pVdbe->pAuxData;
2633 while( *pp ){
2634 AuxData *pAux = *pp;
2635 if( (iOp<0)
drh693e6712014-01-24 22:58:00 +00002636 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
dan0c547792013-07-18 17:12:08 +00002637 ){
drh693e6712014-01-24 22:58:00 +00002638 testcase( pAux->iArg==31 );
drhf92c7ff2004-06-19 15:40:23 +00002639 if( pAux->xDelete ){
2640 pAux->xDelete(pAux->pAux);
2641 }
dan0c547792013-07-18 17:12:08 +00002642 *pp = pAux->pNext;
2643 sqlite3DbFree(pVdbe->db, pAux);
2644 }else{
2645 pp= &pAux->pNext;
drhf92c7ff2004-06-19 15:40:23 +00002646 }
2647 }
2648}
2649
2650/*
drhcb103b92012-10-26 00:11:23 +00002651** Free all memory associated with the Vdbe passed as the second argument,
2652** except for object itself, which is preserved.
2653**
dand46def72010-07-24 11:28:28 +00002654** The difference between this function and sqlite3VdbeDelete() is that
2655** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
drhcb103b92012-10-26 00:11:23 +00002656** the database connection and frees the object itself.
dand46def72010-07-24 11:28:28 +00002657*/
drhcb103b92012-10-26 00:11:23 +00002658void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002659 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002660 int i;
dand46def72010-07-24 11:28:28 +00002661 assert( p->db==0 || p->db==db );
2662 releaseMemArray(p->aVar, p->nVar);
2663 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002664 for(pSub=p->pProgram; pSub; pSub=pNext){
2665 pNext = pSub->pNext;
2666 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2667 sqlite3DbFree(db, pSub);
2668 }
drh124c0b42011-06-01 18:15:55 +00002669 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002670 vdbeFreeOpArray(db, p->aOp, p->nOp);
dand46def72010-07-24 11:28:28 +00002671 sqlite3DbFree(db, p->aColName);
2672 sqlite3DbFree(db, p->zSql);
2673 sqlite3DbFree(db, p->pFree);
drh678a9aa2011-12-10 15:55:01 +00002674#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
drh25fe97a2013-01-23 18:44:22 +00002675 sqlite3DbFree(db, p->zExplain);
drh678a9aa2011-12-10 15:55:01 +00002676 sqlite3DbFree(db, p->pExplain);
drh7e02e5e2011-12-06 19:44:51 +00002677#endif
dand46def72010-07-24 11:28:28 +00002678}
2679
2680/*
drh9a324642003-09-06 20:12:01 +00002681** Delete an entire VDBE.
2682*/
danielk19774adee202004-05-08 08:23:19 +00002683void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002684 sqlite3 *db;
2685
drhfa3be902009-07-07 02:44:07 +00002686 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002687 db = p->db;
drh4245c402012-06-02 14:32:21 +00002688 assert( sqlite3_mutex_held(db->mutex) );
drhcb103b92012-10-26 00:11:23 +00002689 sqlite3VdbeClearObject(db, p);
drh9a324642003-09-06 20:12:01 +00002690 if( p->pPrev ){
2691 p->pPrev->pNext = p->pNext;
2692 }else{
drh633e6d52008-07-28 19:34:53 +00002693 assert( db->pVdbe==p );
2694 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002695 }
2696 if( p->pNext ){
2697 p->pNext->pPrev = p->pPrev;
2698 }
drh9a324642003-09-06 20:12:01 +00002699 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002700 p->db = 0;
drhcb103b92012-10-26 00:11:23 +00002701 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002702}
drha11846b2004-01-07 18:52:56 +00002703
2704/*
drh9a65f2c2009-06-22 19:05:40 +00002705** Make sure the cursor p is ready to read or write the row to which it
2706** was last positioned. Return an error code if an OOM fault or I/O error
2707** prevents us from positioning the cursor to its correct position.
2708**
drha11846b2004-01-07 18:52:56 +00002709** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002710** MoveTo now. If no move is pending, check to see if the row has been
2711** deleted out from under the cursor and if it has, mark the row as
2712** a NULL row.
2713**
2714** If the cursor is already pointing to the correct row and that row has
2715** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002716*/
drhdfe88ec2008-11-03 20:55:06 +00002717int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002718 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002719 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002720#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002721 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002722#endif
drhf0863fe2005-06-12 21:35:51 +00002723 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002724 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002725 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002726 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002727 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2728 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002729#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002730 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002731#endif
drha11846b2004-01-07 18:52:56 +00002732 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002733 p->cacheStatus = CACHE_STALE;
drh399af1d2013-11-20 17:25:55 +00002734 }else if( p->pCursor ){
drha3460582008-07-11 21:02:53 +00002735 int hasMoved;
2736 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2737 if( rc ) return rc;
2738 if( hasMoved ){
2739 p->cacheStatus = CACHE_STALE;
2740 p->nullRow = 1;
2741 }
drha11846b2004-01-07 18:52:56 +00002742 }
2743 return SQLITE_OK;
2744}
danielk19774adee202004-05-08 08:23:19 +00002745
drhab9f7f12004-05-08 10:56:11 +00002746/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002747** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002748**
danielk1977cfcdaef2004-05-12 07:33:33 +00002749** sqlite3VdbeSerialType()
2750** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002751** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002752** sqlite3VdbeSerialPut()
2753** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002754**
2755** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002756** data and index records. Each serialized value consists of a
2757** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2758** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002759**
danielk1977cfcdaef2004-05-12 07:33:33 +00002760** In an SQLite index record, the serial type is stored directly before
2761** the blob of data that it corresponds to. In a table record, all serial
2762** types are stored at the start of the record, and the blobs of data at
2763** the end. Hence these functions allow the caller to handle the
mistachkin48864df2013-03-21 21:20:32 +00002764** serial-type and data blob separately.
danielk1977cfcdaef2004-05-12 07:33:33 +00002765**
2766** The following table describes the various storage classes for data:
2767**
2768** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002769** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002770** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002771** 1 1 signed integer
2772** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002773** 3 3 signed integer
2774** 4 4 signed integer
2775** 5 6 signed integer
2776** 6 8 signed integer
2777** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002778** 8 0 Integer constant 0
2779** 9 0 Integer constant 1
2780** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002781** N>=12 and even (N-12)/2 BLOB
2782** N>=13 and odd (N-13)/2 text
2783**
drh35a59652006-01-02 18:24:40 +00002784** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2785** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002786*/
2787
2788/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002789** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002790*/
drhd946db02005-12-29 19:23:06 +00002791u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002792 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002793 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002794
2795 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002796 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002797 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002798 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002799 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002800# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002801 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002802 u64 u;
drhcfd654b2011-03-05 13:54:15 +00002803 if( i<0 ){
2804 if( i<(-MAX_6BYTE) ) return 6;
2805 /* Previous test prevents: u = -(-9223372036854775808) */
2806 u = -i;
2807 }else{
2808 u = i;
2809 }
drh56690b32012-09-17 15:36:31 +00002810 if( u<=127 ){
2811 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2812 }
drh5742b632005-01-26 17:47:02 +00002813 if( u<=32767 ) return 2;
2814 if( u<=8388607 ) return 3;
2815 if( u<=2147483647 ) return 4;
2816 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002817 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002818 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002819 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002820 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002821 }
danielk1977e4359752008-11-03 09:39:45 +00002822 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002823 n = pMem->n;
2824 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002825 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002826 }
drhfdf972a2007-05-02 13:30:27 +00002827 assert( n>=0 );
2828 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002829}
2830
2831/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002832** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002833*/
drh35cd6432009-06-05 14:17:21 +00002834u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002835 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002836 return (serial_type-12)/2;
2837 }else{
drh57196282004-10-06 15:41:16 +00002838 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002839 return aSize[serial_type];
2840 }
danielk1977192ac1d2004-05-10 07:17:30 +00002841}
2842
2843/*
drh110daac2007-05-04 11:59:31 +00002844** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002845** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002846** upper 4 bytes. Return the result.
2847**
drh7a4f5022007-05-23 07:20:08 +00002848** For most architectures, this is a no-op.
2849**
2850** (later): It is reported to me that the mixed-endian problem
2851** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2852** that early versions of GCC stored the two words of a 64-bit
2853** float in the wrong order. And that error has been propagated
2854** ever since. The blame is not necessarily with GCC, though.
2855** GCC might have just copying the problem from a prior compiler.
2856** I am also told that newer versions of GCC that follow a different
2857** ABI get the byte order right.
2858**
2859** Developers using SQLite on an ARM7 should compile and run their
2860** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2861** enabled, some asserts below will ensure that the byte order of
2862** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002863**
2864** (2007-08-30) Frank van Vugt has studied this problem closely
2865** and has send his findings to the SQLite developers. Frank
2866** writes that some Linux kernels offer floating point hardware
2867** emulation that uses only 32-bit mantissas instead of a full
2868** 48-bits as required by the IEEE standard. (This is the
2869** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2870** byte swapping becomes very complicated. To avoid problems,
2871** the necessary byte swapping is carried out using a 64-bit integer
2872** rather than a 64-bit float. Frank assures us that the code here
2873** works for him. We, the developers, have no way to independently
2874** verify this, but Frank seems to know what he is talking about
2875** so we trust him.
drh110daac2007-05-04 11:59:31 +00002876*/
2877#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002878static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002879 union {
drh60d09a72007-08-30 15:05:08 +00002880 u64 r;
drh110daac2007-05-04 11:59:31 +00002881 u32 i[2];
2882 } u;
2883 u32 t;
2884
2885 u.r = in;
2886 t = u.i[0];
2887 u.i[0] = u.i[1];
2888 u.i[1] = t;
2889 return u.r;
2890}
2891# define swapMixedEndianFloat(X) X = floatSwap(X)
2892#else
2893# define swapMixedEndianFloat(X)
2894#endif
2895
2896/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002897** Write the serialized data blob for the value stored in pMem into
2898** buf. It is assumed that the caller has allocated sufficient space.
2899** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002900**
drh038b7bc2013-12-09 23:17:22 +00002901** nBuf is the amount of space left in buf[]. The caller is responsible
2902** for allocating enough space to buf[] to hold the entire field, exclusive
2903** of the pMem->u.nZero bytes for a MEM_Zero value.
drhfdf972a2007-05-02 13:30:27 +00002904**
2905** Return the number of bytes actually written into buf[]. The number
2906** of bytes in the zero-filled tail is included in the return value only
2907** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002908*/
drha9ab4812013-12-11 11:00:44 +00002909u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
drh35cd6432009-06-05 14:17:21 +00002910 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002911
drh1483e142004-05-21 21:12:42 +00002912 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002913 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002914 u64 v;
drh35cd6432009-06-05 14:17:21 +00002915 u32 i;
drha19b7752004-05-30 21:14:58 +00002916 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002917 assert( sizeof(v)==sizeof(pMem->r) );
2918 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002919 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002920 }else{
drh3c024d62007-03-30 11:23:45 +00002921 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002922 }
drh1483e142004-05-21 21:12:42 +00002923 len = i = sqlite3VdbeSerialTypeLen(serial_type);
2924 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002925 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002926 v >>= 8;
2927 }
2928 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002929 }
drhd946db02005-12-29 19:23:06 +00002930
danielk1977cfcdaef2004-05-12 07:33:33 +00002931 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002932 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002933 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002934 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002935 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002936 memcpy(buf, pMem->z, len);
2937 return len;
2938 }
2939
2940 /* NULL or constants 0 or 1 */
2941 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002942}
2943
2944/*
2945** Deserialize the data blob pointed to by buf as serial type serial_type
2946** and store the result in pMem. Return the number of bytes read.
2947*/
drh35cd6432009-06-05 14:17:21 +00002948u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002949 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002950 u32 serial_type, /* Serial type to deserialize */
2951 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002952){
drh693e6712014-01-24 22:58:00 +00002953 u64 x;
2954 u32 y;
2955 int i;
drh3c685822005-05-21 18:32:18 +00002956 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002957 case 10: /* Reserved for future use */
2958 case 11: /* Reserved for future use */
2959 case 0: { /* NULL */
2960 pMem->flags = MEM_Null;
2961 break;
2962 }
2963 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002964 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002965 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002966 return 1;
drh1483e142004-05-21 21:12:42 +00002967 }
drh3c685822005-05-21 18:32:18 +00002968 case 2: { /* 2-byte signed integer */
drh693e6712014-01-24 22:58:00 +00002969 i = 256*(signed char)buf[0] | buf[1];
2970 pMem->u.i = (i64)i;
drh3c685822005-05-21 18:32:18 +00002971 pMem->flags = MEM_Int;
2972 return 2;
2973 }
2974 case 3: { /* 3-byte signed integer */
drh693e6712014-01-24 22:58:00 +00002975 i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
2976 pMem->u.i = (i64)i;
drh3c685822005-05-21 18:32:18 +00002977 pMem->flags = MEM_Int;
2978 return 3;
2979 }
2980 case 4: { /* 4-byte signed integer */
drh693e6712014-01-24 22:58:00 +00002981 y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2982 pMem->u.i = (i64)*(int*)&y;
drh3c685822005-05-21 18:32:18 +00002983 pMem->flags = MEM_Int;
2984 return 4;
2985 }
2986 case 5: { /* 6-byte signed integer */
drh72967772014-01-27 13:58:58 +00002987 x = 256*(signed char)buf[0] + buf[1];
2988 y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
drh3c685822005-05-21 18:32:18 +00002989 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002990 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002991 pMem->flags = MEM_Int;
2992 return 6;
2993 }
drh91124b32005-08-18 18:15:05 +00002994 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002995 case 7: { /* IEEE floating point */
drh2a3e4a72006-01-23 21:44:53 +00002996#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002997 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002998 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2999 ** defined that 64-bit floating point values really are mixed
3000 ** endian.
drhbfd6b032005-08-28 01:38:44 +00003001 */
drhde941c62005-08-28 01:34:21 +00003002 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00003003 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00003004 u64 t2 = t1;
3005 swapMixedEndianFloat(t2);
3006 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00003007#endif
drh693e6712014-01-24 22:58:00 +00003008 x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
3009 y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00003010 x = (x<<32) | y;
3011 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00003012 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00003013 pMem->flags = MEM_Int;
3014 }else{
drh4f0c5872007-03-26 22:05:01 +00003015 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00003016 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00003017 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00003018 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00003019 }
3020 return 8;
3021 }
drhd946db02005-12-29 19:23:06 +00003022 case 8: /* Integer 0 */
3023 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00003024 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00003025 pMem->flags = MEM_Int;
3026 return 0;
3027 }
drh3c685822005-05-21 18:32:18 +00003028 default: {
drhc138daf2013-11-19 13:55:34 +00003029 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
drh35cd6432009-06-05 14:17:21 +00003030 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00003031 pMem->z = (char *)buf;
3032 pMem->n = len;
3033 pMem->xDel = 0;
drhc138daf2013-11-19 13:55:34 +00003034 pMem->flags = aFlag[serial_type&1];
drh3c685822005-05-21 18:32:18 +00003035 return len;
drh696b32f2004-05-30 01:51:52 +00003036 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003037 }
drh3c685822005-05-21 18:32:18 +00003038 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00003039}
3040
drh1e968a02008-03-25 00:22:21 +00003041/*
dan03e9cfc2011-09-05 14:20:27 +00003042** This routine is used to allocate sufficient space for an UnpackedRecord
3043** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3044** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00003045**
dan03e9cfc2011-09-05 14:20:27 +00003046** The space is either allocated using sqlite3DbMallocRaw() or from within
3047** the unaligned buffer passed via the second and third arguments (presumably
3048** stack space). If the former, then *ppFree is set to a pointer that should
3049** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3050** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3051** before returning.
drh1e968a02008-03-25 00:22:21 +00003052**
dan03e9cfc2011-09-05 14:20:27 +00003053** If an OOM error occurs, NULL is returned.
3054*/
3055UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3056 KeyInfo *pKeyInfo, /* Description of the record */
3057 char *pSpace, /* Unaligned space available */
3058 int szSpace, /* Size of pSpace[] in bytes */
3059 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00003060){
dan03e9cfc2011-09-05 14:20:27 +00003061 UnpackedRecord *p; /* Unpacked record to return */
3062 int nOff; /* Increment pSpace by nOff to align it */
3063 int nByte; /* Number of bytes required for *p */
3064
3065 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00003066 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3067 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3068 */
3069 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00003070 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00003071 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00003072 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3073 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00003074 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00003075 }else{
dan42acb3e2011-09-05 20:16:38 +00003076 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00003077 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00003078 }
dan42acb3e2011-09-05 20:16:38 +00003079
3080 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drhe1a022e2012-09-17 17:16:53 +00003081 assert( pKeyInfo->aSortOrder!=0 );
drh1e968a02008-03-25 00:22:21 +00003082 p->pKeyInfo = pKeyInfo;
3083 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00003084 return p;
3085}
3086
3087/*
3088** Given the nKey-byte encoding of a record in pKey[], populate the
3089** UnpackedRecord structure indicated by the fourth argument with the
3090** contents of the decoded record.
3091*/
3092void sqlite3VdbeRecordUnpack(
3093 KeyInfo *pKeyInfo, /* Information about the record format */
3094 int nKey, /* Size of the binary record */
3095 const void *pKey, /* The binary record */
3096 UnpackedRecord *p /* Populate this structure before returning. */
3097){
3098 const unsigned char *aKey = (const unsigned char *)pKey;
3099 int d;
3100 u32 idx; /* Offset in aKey[] to read from */
3101 u16 u; /* Unsigned loop counter */
3102 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00003103 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00003104
dan1fed5da2014-02-25 21:01:25 +00003105 p->default_rc = 0;
drh8c5d1522009-04-10 00:56:28 +00003106 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00003107 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00003108 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00003109 u = 0;
drh2fa34d32009-07-15 16:30:50 +00003110 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00003111 u32 serial_type;
3112
danielk197700e13612008-11-17 19:18:54 +00003113 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00003114 pMem->enc = pKeyInfo->enc;
3115 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00003116 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
danielk19775f096132008-03-28 15:44:09 +00003117 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00003118 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00003119 pMem++;
shane0b8d2762008-07-22 05:18:00 +00003120 u++;
drh1e968a02008-03-25 00:22:21 +00003121 }
drh7d10d5a2008-08-20 16:35:10 +00003122 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00003123 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00003124}
3125
dan1fed5da2014-02-25 21:01:25 +00003126static int vdbeRecordCompareString(
3127 int nKey1, const void *pKey1, /* Left key */
3128 UnpackedRecord *pPKey2 /* Right key */
3129){
3130 const u8 *aKey1 = (const u8*)pKey1;
3131 int szHdr;
3132 int serial_type;
3133 int res;
3134
3135 szHdr = aKey1[0];
3136 getVarint32(&aKey1[1], serial_type);
3137
3138 if( serial_type<12 ){
3139 res = -1; /* (pKey1/nKey1) is a number or a null */
3140 }else if( !(serial_type & 0x01) ){
3141 res = +1; /* (pKey1/nKey1) is a blob */
3142 }else{
3143 int nCmp;
3144 int nStr;
3145 aKey1 = &aKey1[szHdr];
3146
3147 nStr = (serial_type-12) / 2;
3148 if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
3149 nCmp = MIN( pPKey2->aMem[0].n, nStr );
3150 res = memcmp(aKey1, pPKey2->aMem[0].z, nCmp);
3151
3152 if( res==0 ){
3153 res = nStr - pPKey2->aMem[0].n;
3154 if( res==0 ) res = pPKey2->default_rc;
3155 }
3156 }
3157
3158 assert( (res==0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)==0)
3159 || (res<0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)<0)
3160 || (res>0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)>0)
3161 );
3162 return res;
3163}
3164
3165static int vdbeRecordCompareInt(
3166 int nKey1, const void *pKey1, /* Left key */
3167 UnpackedRecord *pPKey2 /* Right key */
3168){
3169 const u8 *aKey1 = (const u8*)pKey1;
3170 int szHdr;
3171 int serial_type;
3172 int res;
3173
3174 szHdr = aKey1[0];
3175 getVarint32(&aKey1[1], serial_type);
3176
3177 if( serial_type==0 ){
3178 res = -1; /* NULL values are smaller than integers */
3179 }else if( serial_type>=12 ){
3180 res = +1; /* text/blob values are greater */
3181 }else{
3182 Mem mem;
3183 sqlite3VdbeSerialGet(&aKey1[szHdr], serial_type, &mem);
3184 if( mem.flags & MEM_Int ){
3185 i64 v = pPKey2->aMem[0].u.i;
3186 if( v>mem.u.i ){
3187 res = -1;
3188 }else if( v<mem.u.i ){
3189 res = +1;
3190 }else{
3191 res = pPKey2->default_rc;
3192 }
3193 }else{
3194 double v = (double)pPKey2->aMem[0].u.i;
3195 if( v>mem.r ){
3196 res = -1;
3197 }else if( v<mem.r ){
3198 res = +1;
3199 }else{
3200 res = pPKey2->default_rc;
3201 }
3202 }
3203 }
3204
3205 assert( (res==0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)==0)
3206 || (res<0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)<0)
3207 || (res>0 && sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2)>0)
3208 );
3209 return res;
3210}
3211
3212static int vdbeCompareMemString(
3213 const Mem *pMem1,
3214 const Mem *pMem2,
3215 const CollSeq *pColl
3216){
3217 if( pMem1->enc==pColl->enc ){
3218 /* The strings are already in the correct encoding. Call the
3219 ** comparison function directly */
3220 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3221 }else{
3222 int rc;
3223 const void *v1, *v2;
3224 int n1, n2;
3225 Mem c1;
3226 Mem c2;
3227 memset(&c1, 0, sizeof(c1));
3228 memset(&c2, 0, sizeof(c2));
3229 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3230 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3231 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3232 n1 = v1==0 ? 0 : c1.n;
3233 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3234 n2 = v2==0 ? 0 : c2.n;
3235 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3236 sqlite3VdbeMemRelease(&c1);
3237 sqlite3VdbeMemRelease(&c2);
3238 return rc;
3239 }
3240}
3241
3242/*
3243** Compare the values contained by the two memory cells, returning
3244** negative, zero or positive if pMem1 is less than, equal to, or greater
3245** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3246** and reals) sorted numerically, followed by text ordered by the collating
3247** sequence pColl and finally blob's ordered by memcmp().
3248**
3249** Two NULL values are considered equal by this function.
3250*/
3251int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3252 int rc;
3253 int f1, f2;
3254 int combined_flags;
3255
3256 f1 = pMem1->flags;
3257 f2 = pMem2->flags;
3258 combined_flags = f1|f2;
3259 assert( (combined_flags & MEM_RowSet)==0 );
3260
3261 /* If one value is NULL, it is less than the other. If both values
3262 ** are NULL, return 0.
3263 */
3264 if( combined_flags&MEM_Null ){
3265 return (f2&MEM_Null) - (f1&MEM_Null);
3266 }
3267
3268 /* If one value is a number and the other is not, the number is less.
3269 ** If both are numbers, compare as reals if one is a real, or as integers
3270 ** if both values are integers.
3271 */
3272 if( combined_flags&(MEM_Int|MEM_Real) ){
3273 double r1, r2;
3274 if( (f1 & f2 & MEM_Int)!=0 ){
3275 if( pMem1->u.i < pMem2->u.i ) return -1;
3276 if( pMem1->u.i > pMem2->u.i ) return 1;
3277 return 0;
3278 }
3279 if( (f1&MEM_Real)!=0 ){
3280 r1 = pMem1->r;
3281 }else if( (f1&MEM_Int)!=0 ){
3282 r1 = (double)pMem1->u.i;
3283 }else{
3284 return 1;
3285 }
3286 if( (f2&MEM_Real)!=0 ){
3287 r2 = pMem2->r;
3288 }else if( (f2&MEM_Int)!=0 ){
3289 r2 = (double)pMem2->u.i;
3290 }else{
3291 return -1;
3292 }
3293 if( r1<r2 ) return -1;
3294 if( r1>r2 ) return 1;
3295 return 0;
3296 }
3297
3298 /* If one value is a string and the other is a blob, the string is less.
3299 ** If both are strings, compare using the collating functions.
3300 */
3301 if( combined_flags&MEM_Str ){
3302 if( (f1 & MEM_Str)==0 ){
3303 return 1;
3304 }
3305 if( (f2 & MEM_Str)==0 ){
3306 return -1;
3307 }
3308
3309 assert( pMem1->enc==pMem2->enc );
3310 assert( pMem1->enc==SQLITE_UTF8 ||
3311 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3312
3313 /* The collation sequence must be defined at this point, even if
3314 ** the user deletes the collation sequence after the vdbe program is
3315 ** compiled (this was not always the case).
3316 */
3317 assert( !pColl || pColl->xCmp );
3318
3319 if( pColl ){
3320 return vdbeCompareMemString(pMem1, pMem2, pColl);
3321 }
3322 /* If a NULL pointer was passed as the collate function, fall through
3323 ** to the blob case and use memcmp(). */
3324 }
3325
3326 /* Both values must be blobs. Compare using memcmp(). */
3327 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
3328 if( rc==0 ){
3329 rc = pMem1->n - pMem2->n;
3330 }
3331 return rc;
3332}
3333
3334
drh1e968a02008-03-25 00:22:21 +00003335/*
3336** This function compares the two table rows or index records
3337** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00003338** or positive integer if key1 is less than, equal to or
3339** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00003340** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
3341** key must be a parsed key such as obtained from
3342** sqlite3VdbeParseRecord.
3343**
3344** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00003345** The key with fewer fields is usually compares less than the
3346** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
3347** and the common prefixes are equal, then key1 is less than key2.
3348** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
3349** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00003350** the parts beyond the common prefix are ignored.
drh1e968a02008-03-25 00:22:21 +00003351*/
dan1fed5da2014-02-25 21:01:25 +00003352static int vdbeRecordComparePrev(
drhec1fc802008-08-13 14:07:40 +00003353 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00003354 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00003355){
drhdf003d62013-08-01 19:17:39 +00003356 u32 d1; /* Offset into aKey[] of next data element */
drh1e968a02008-03-25 00:22:21 +00003357 u32 idx1; /* Offset into aKey[] of next header element */
3358 u32 szHdr1; /* Number of bytes in header */
3359 int i = 0;
drh1e968a02008-03-25 00:22:21 +00003360 int rc = 0;
3361 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3362 KeyInfo *pKeyInfo;
3363 Mem mem1;
3364
3365 pKeyInfo = pPKey2->pKeyInfo;
3366 mem1.enc = pKeyInfo->enc;
drh37272632009-11-16 21:28:45 +00003367 mem1.db = pKeyInfo->db;
drhd93a8b22009-11-16 03:13:40 +00003368 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
3369 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
drh8b249a82009-11-16 02:14:00 +00003370
3371 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3372 ** We could initialize it, as shown here, to silence those complaints.
drh5275d2e2011-04-27 01:00:17 +00003373 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
drh8b249a82009-11-16 02:14:00 +00003374 ** the unnecessary initialization has a measurable negative performance
3375 ** impact, since this routine is a very high runner. And so, we choose
3376 ** to ignore the compiler warnings and leave this variable uninitialized.
3377 */
3378 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
drh1e968a02008-03-25 00:22:21 +00003379
shane3f8d5cf2008-04-24 19:15:09 +00003380 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00003381 d1 = szHdr1;
drhb2023662013-11-29 15:39:36 +00003382 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
drhe1a022e2012-09-17 17:16:53 +00003383 assert( pKeyInfo->aSortOrder!=0 );
dan89bc0212013-12-03 09:49:52 +00003384 assert( pKeyInfo->nField>0 );
3385 assert( idx1<=szHdr1 || CORRUPT_DB );
drh0b9dada2013-11-25 22:24:36 +00003386 do{
drh1e968a02008-03-25 00:22:21 +00003387 u32 serial_type1;
3388
3389 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00003390 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drhaf5b2af2013-08-05 15:32:09 +00003391
3392 /* Verify that there is enough key space remaining to avoid
3393 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3394 ** always be greater than or equal to the amount of required key space.
3395 ** Use that approximation to avoid the more expensive call to
3396 ** sqlite3VdbeSerialTypeLen() in the common case.
3397 */
3398 if( d1+serial_type1+2>(u32)nKey1
3399 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3400 ){
3401 break;
3402 }
drh1e968a02008-03-25 00:22:21 +00003403
3404 /* Extract the values to be compared.
3405 */
3406 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
3407
3408 /* Do the comparison
3409 */
drh323df792013-08-05 19:11:29 +00003410 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
drh1e968a02008-03-25 00:22:21 +00003411 if( rc!=0 ){
drh8b249a82009-11-16 02:14:00 +00003412 assert( mem1.zMalloc==0 ); /* See comment below */
drh323df792013-08-05 19:11:29 +00003413 if( pKeyInfo->aSortOrder[i] ){
drh6f225d02013-10-26 13:36:51 +00003414 rc = -rc; /* Invert the result for DESC sort order. */
drh8b249a82009-11-16 02:14:00 +00003415 }
drh8b249a82009-11-16 02:14:00 +00003416 return rc;
drh1e968a02008-03-25 00:22:21 +00003417 }
3418 i++;
drh0b9dada2013-11-25 22:24:36 +00003419 }while( idx1<szHdr1 && i<pPKey2->nField );
drh407414c2009-07-14 14:15:27 +00003420
drh8b249a82009-11-16 02:14:00 +00003421 /* No memory allocation is ever used on mem1. Prove this using
3422 ** the following assert(). If the assert() fails, it indicates a
3423 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
danielk1977de630352009-05-04 11:42:29 +00003424 */
drh8b249a82009-11-16 02:14:00 +00003425 assert( mem1.zMalloc==0 );
danielk1977de630352009-05-04 11:42:29 +00003426
drh8b249a82009-11-16 02:14:00 +00003427 /* rc==0 here means that one of the keys ran out of fields and
dan1fed5da2014-02-25 21:01:25 +00003428 ** all the fields up to that point were equal. Return the the default_rc
3429 ** value. */
3430 return pPKey2->default_rc;
drh1e968a02008-03-25 00:22:21 +00003431}
drhec1fc802008-08-13 14:07:40 +00003432
danielk1977eb015e02004-05-18 01:31:14 +00003433
dan1fed5da2014-02-25 21:01:25 +00003434int sqlite3VdbeRecordCompare(
3435 int nKey1, const void *pKey1, /* Left key */
3436 UnpackedRecord *pPKey2 /* Right key */
3437){
3438 u32 d1; /* Offset into aKey[] of next data element */
3439 u32 idx1; /* Offset into aKey[] of next header element */
3440 u32 szHdr1; /* Number of bytes in header */
3441 int i = 0;
3442 int rc = 0;
3443 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3444 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3445 Mem mem1;
3446
3447#ifdef SQLITE_DEBUG
3448 int expected = vdbeRecordComparePrev(nKey1, pKey1, pPKey2);
3449 static int nCall = 0;
3450 nCall++;
3451#endif
3452
3453 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
3454
3455 idx1 = getVarint32(aKey1, szHdr1);
3456 d1 = szHdr1;
3457 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3458 || CORRUPT_DB );
3459 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3460 assert( pPKey2->pKeyInfo->nField>0 );
3461 assert( idx1<=szHdr1 || CORRUPT_DB );
3462 do{
3463 Mem *pRhs = &pPKey2->aMem[i];
3464 u32 serial_type;
3465
3466 /* RHS is an integer */
3467 if( pRhs->flags & MEM_Int ){
3468 serial_type = aKey1[idx1];
3469 if( serial_type>=12 ){
3470 rc = +1;
3471 }else if( serial_type==0 ){
3472 rc = -1;
3473 }else{
3474 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3475 if( serial_type==7 ){
3476 double rhs = (double)pRhs->u.i;
3477 if( mem1.r<rhs ){
3478 rc = -1;
3479 }else if( mem1.r>rhs ){
3480 rc = +1;
3481 }
3482 }else{
3483 i64 rhs = pRhs->u.i;
3484 if( mem1.u.i<rhs ){
3485 rc = -1;
3486 }else if( mem1.u.i>rhs ){
3487 rc = +1;
3488 }
3489 }
3490 }
3491 }
3492
3493 /* RHS is real */
3494 else if( pRhs->flags & MEM_Real ){
3495 serial_type = aKey1[idx1];
3496 if( serial_type>=12 ){
3497 rc = +1;
3498 }else if( serial_type==0 ){
3499 rc = -1;
3500 }else{
3501 double rhs = pRhs->r;
3502 double lhs;
3503 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3504 if( serial_type==7 ){
3505 lhs = mem1.r;
3506 }else{
3507 lhs = mem1.u.i;
3508 }
3509 if( lhs<rhs ){
3510 rc = -1;
3511 }else if( lhs>rhs ){
3512 rc = +1;
3513 }
3514 }
3515 }
3516
3517 /* RHS is a string */
3518 else if( pRhs->flags & MEM_Str ){
3519 getVarint32(&aKey1[idx1], serial_type);
3520 if( serial_type<12 ){
3521 rc = -1;
3522 }else if( !(serial_type & 0x01) ){
3523 rc = +1;
3524 }else{
3525 mem1.n = (serial_type - 12) / 2;
3526 if( (d1+mem1.n) > nKey1 ){
3527 rc = 1; /* Corruption */
3528 }else if( pKeyInfo->aColl[i] ){
3529 mem1.enc = pKeyInfo->enc;
3530 mem1.db = pKeyInfo->db;
3531 mem1.flags = MEM_Str;
3532 mem1.z = &aKey1[d1];
3533 rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
3534 }else{
3535 int nCmp = MIN(mem1.n, pRhs->n);
3536 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3537 if( rc==0 ) rc = mem1.n - pRhs->n;
3538 }
3539 }
3540 }
3541
3542 /* RHS is a blob */
3543 else if( pRhs->flags & MEM_Blob ){
3544 getVarint32(&aKey1[idx1], serial_type);
3545 if( serial_type<12 || (serial_type & 0x01) ){
3546 rc = -1;
3547 }else{
3548 int nStr = (serial_type - 12) / 2;
3549 if( (d1+nStr) > nKey1 ){
3550 rc = 1; /* Corruption */
3551 }else{
3552 int nCmp = MIN(nStr, pRhs->n);
3553 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3554 if( rc==0 ) rc = nStr - pRhs->n;
3555 }
3556 }
3557 }
3558
3559 /* RHS is null */
3560 else{
3561 serial_type = aKey1[idx1];
3562 rc = (serial_type!=0);
3563 }
3564
3565 if( rc!=0 ){
3566 assert( mem1.zMalloc==0 ); /* See comment below */
3567 if( pKeyInfo->aSortOrder[i] ){
3568 rc = -rc;
3569#if 0
3570 assert( (rc>0 && (rc^(int)0x80000000)<0)
3571 || (rc<0 && (rc^(int)0x80000000)>0) );
3572 assert( sizeof(int)==4 );
3573 rc ^= (int)0x80000000; /* similar in spirit to: "rc = -rc;" */
3574 assert( rc!=0 );
3575#endif
3576 }
3577 assert( (rc<0 && expected<0) || (rc>0 && expected>0) || CORRUPT_DB );
3578 return rc;
3579 }
3580
3581 i++;
3582 d1 += sqlite3VdbeSerialTypeLen(serial_type);
3583 idx1 += sqlite3VarintLen(serial_type);
3584 }while( idx1<szHdr1 && i<pPKey2->nField && d1<=nKey1 );
3585
3586 /* No memory allocation is ever used on mem1. Prove this using
3587 ** the following assert(). If the assert() fails, it indicates a
3588 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3589 */
3590 assert( mem1.zMalloc==0 );
3591
3592 /* rc==0 here means that one or both of the keys ran out of fields and
3593 ** all the fields up to that point were equal. Return the the default_rc
3594 ** value. */
3595 assert( pPKey2->default_rc==expected );
3596 return pPKey2->default_rc;
3597}
3598
3599RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
3600 if( p->nField==1 && p->pKeyInfo->aSortOrder[0]==0 ){
3601 int flags = p->aMem[0].flags;
3602 if( (flags & MEM_Int) ){
3603 return vdbeRecordCompareInt;
3604 }else if( (p->aMem[0].flags&(MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
3605 && p->pKeyInfo->aColl[0]==0
3606 ){
3607 return vdbeRecordCompareString;
3608 }
3609 }
3610 return sqlite3VdbeRecordCompare;
3611}
3612
3613
danielk1977eb015e02004-05-18 01:31:14 +00003614/*
drh7a224de2004-06-02 01:22:02 +00003615** pCur points at an index entry created using the OP_MakeRecord opcode.
3616** Read the rowid (the last field in the record) and store it in *rowid.
3617** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00003618**
3619** pCur might be pointing to text obtained from a corrupt database file.
3620** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00003621*/
drh35f6b932009-06-23 14:15:04 +00003622int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00003623 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003624 int rc;
drhd5788202004-05-28 08:21:05 +00003625 u32 szHdr; /* Size of the header */
3626 u32 typeRowid; /* Serial type of the rowid */
3627 u32 lenRowid; /* Size of the rowid */
3628 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00003629
shanecea72b22009-09-07 04:38:36 +00003630 UNUSED_PARAMETER(db);
3631
drh88a003e2008-12-11 16:17:03 +00003632 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00003633 ** than 2GiB are support - anything large must be database corruption.
3634 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00003635 ** this code can safely assume that nCellKey is 32-bits
3636 */
drhea8ffdf2009-07-22 00:35:23 +00003637 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003638 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003639 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00003640 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003641
3642 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003643 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003644 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003645 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003646 return rc;
3647 }
drh88a003e2008-12-11 16:17:03 +00003648
3649 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003650 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003651 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003652 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003653 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003654 goto idx_rowid_corruption;
3655 }
3656
3657 /* The last field of the index should be an integer - the ROWID.
3658 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003659 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003660 testcase( typeRowid==1 );
3661 testcase( typeRowid==2 );
3662 testcase( typeRowid==3 );
3663 testcase( typeRowid==4 );
3664 testcase( typeRowid==5 );
3665 testcase( typeRowid==6 );
3666 testcase( typeRowid==8 );
3667 testcase( typeRowid==9 );
3668 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3669 goto idx_rowid_corruption;
3670 }
drhd5788202004-05-28 08:21:05 +00003671 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003672 testcase( (u32)m.n==szHdr+lenRowid );
3673 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003674 goto idx_rowid_corruption;
3675 }
3676
3677 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003678 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003679 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003680 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003681 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003682
3683 /* Jump here if database corruption is detected after m has been
3684 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3685idx_rowid_corruption:
3686 testcase( m.zMalloc!=0 );
3687 sqlite3VdbeMemRelease(&m);
3688 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003689}
3690
drh7cf6e4d2004-05-19 14:56:55 +00003691/*
drh5f82e3c2009-07-06 00:44:08 +00003692** Compare the key of the index entry that cursor pC is pointing to against
3693** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003694** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003695** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003696**
drh5f82e3c2009-07-06 00:44:08 +00003697** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003698** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003699** is ignored as well. Hence, this routine only compares the prefixes
3700** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003701*/
danielk1977183f9f72004-05-13 05:20:26 +00003702int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00003703 VdbeCursor *pC, /* The cursor to compare against */
drh5f82e3c2009-07-06 00:44:08 +00003704 UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */
drh7cf6e4d2004-05-19 14:56:55 +00003705 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003706){
drh61fc5952007-04-01 23:49:51 +00003707 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003708 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003709 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003710 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003711
drhea8ffdf2009-07-22 00:35:23 +00003712 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003713 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003714 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh407414c2009-07-14 14:15:27 +00003715 /* nCellKey will always be between 0 and 0xffffffff because of the say
3716 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003717 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003718 *res = 0;
drh9978c972010-02-23 17:36:32 +00003719 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003720 }
drhfd3ca1c2009-08-25 12:11:00 +00003721 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003722 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003723 if( rc ){
drhd5788202004-05-28 08:21:05 +00003724 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003725 }
drhe63d9992008-08-13 19:11:48 +00003726 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00003727 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003728 return SQLITE_OK;
3729}
danielk1977b28af712004-06-21 06:50:26 +00003730
3731/*
3732** This routine sets the value to be returned by subsequent calls to
3733** sqlite3_changes() on the database handle 'db'.
3734*/
3735void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003736 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003737 db->nChange = nChange;
3738 db->nTotalChange += nChange;
3739}
3740
3741/*
3742** Set a flag in the vdbe to update the change counter when it is finalised
3743** or reset.
3744*/
drh4794f732004-11-05 17:17:50 +00003745void sqlite3VdbeCountChanges(Vdbe *v){
3746 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003747}
drhd89bd002005-01-22 03:03:54 +00003748
3749/*
3750** Mark every prepared statement associated with a database connection
3751** as expired.
3752**
3753** An expired statement means that recompilation of the statement is
3754** recommend. Statements expire when things happen that make their
3755** programs obsolete. Removing user-defined functions or collating
3756** sequences, or changing an authorization function are the types of
3757** things that make prepared statements obsolete.
3758*/
3759void sqlite3ExpirePreparedStatements(sqlite3 *db){
3760 Vdbe *p;
3761 for(p = db->pVdbe; p; p=p->pNext){
3762 p->expired = 1;
3763 }
3764}
danielk1977aee18ef2005-03-09 12:26:50 +00003765
3766/*
3767** Return the database associated with the Vdbe.
3768*/
3769sqlite3 *sqlite3VdbeDb(Vdbe *v){
3770 return v->db;
3771}
dan937d0de2009-10-15 18:35:38 +00003772
3773/*
3774** Return a pointer to an sqlite3_value structure containing the value bound
3775** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3776** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3777** constants) to the value before returning it.
3778**
3779** The returned value must be freed by the caller using sqlite3ValueFree().
3780*/
drhcf0fd4a2013-08-01 12:21:58 +00003781sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
dan937d0de2009-10-15 18:35:38 +00003782 assert( iVar>0 );
3783 if( v ){
3784 Mem *pMem = &v->aVar[iVar-1];
3785 if( 0==(pMem->flags & MEM_Null) ){
3786 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3787 if( pRet ){
3788 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3789 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
3790 sqlite3VdbeMemStoreType((Mem *)pRet);
3791 }
3792 return pRet;
3793 }
3794 }
3795 return 0;
3796}
3797
3798/*
3799** Configure SQL variable iVar so that binding a new value to it signals
3800** to sqlite3_reoptimize() that re-preparing the statement may result
3801** in a better query plan.
3802*/
dan1d2ce4f2009-10-19 18:11:09 +00003803void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003804 assert( iVar>0 );
3805 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00003806 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00003807 }else{
dan1d2ce4f2009-10-19 18:11:09 +00003808 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00003809 }
3810}
dan016f7812013-08-21 17:35:48 +00003811
3812#ifndef SQLITE_OMIT_VIRTUALTABLE
3813/*
3814** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
3815** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
3816** in memory obtained from sqlite3DbMalloc).
3817*/
3818void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
3819 sqlite3 *db = p->db;
3820 sqlite3DbFree(db, p->zErrMsg);
3821 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
3822 sqlite3_free(pVtab->zErrMsg);
3823 pVtab->zErrMsg = 0;
3824}
3825#endif /* SQLITE_OMIT_VIRTUALTABLE */