blob: 776821d398086f22f50ded4f133811b6d867ce9a [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 );
drh75fd0542014-03-01 16:24:44 +00001223 assert( sqlite3VdbeCheckMemInvariants(p) );
danielk1977e972e032008-09-19 18:32:26 +00001224
1225 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1226 ** that takes advantage of the fact that the memory cell value is
1227 ** being set to NULL after releasing any dynamic resources.
1228 **
1229 ** The justification for duplicating code is that according to
1230 ** callgrind, this causes a certain test case to hit the CPU 4.7
1231 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1232 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1233 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1234 ** with no indexes using a single prepared INSERT statement, bind()
1235 ** and reset(). Inserts are grouped into a transaction.
1236 */
dan165921a2009-08-28 18:53:45 +00001237 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001238 sqlite3VdbeMemRelease(p);
1239 }else if( p->zMalloc ){
1240 sqlite3DbFree(db, p->zMalloc);
1241 p->zMalloc = 0;
1242 }
1243
drha5750cf2014-02-07 13:20:31 +00001244 p->flags = MEM_Undefined;
drh76ff3a02004-09-24 22:32:30 +00001245 }
danielk1977a7a8e142008-02-13 18:25:27 +00001246 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001247 }
1248}
1249
dan65a7cd12009-09-01 12:16:01 +00001250/*
1251** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1252** allocated by the OP_Program opcode in sqlite3VdbeExec().
1253*/
dan165921a2009-08-28 18:53:45 +00001254void sqlite3VdbeFrameDelete(VdbeFrame *p){
1255 int i;
1256 Mem *aMem = VdbeFrameMem(p);
1257 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1258 for(i=0; i<p->nChildCsr; i++){
1259 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1260 }
1261 releaseMemArray(aMem, p->nChildMem);
1262 sqlite3DbFree(p->v->db, p);
1263}
1264
drhb7f91642004-10-31 02:22:47 +00001265#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001266/*
drh9a324642003-09-06 20:12:01 +00001267** Give a listing of the program in the virtual machine.
1268**
danielk19774adee202004-05-08 08:23:19 +00001269** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001270** running the code, it invokes the callback once for each instruction.
1271** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001272**
1273** When p->explain==1, each instruction is listed. When
1274** p->explain==2, only OP_Explain instructions are listed and these
1275** are shown in a different format. p->explain==2 is used to implement
1276** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001277**
1278** When p->explain==1, first the main program is listed, then each of
1279** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001280*/
danielk19774adee202004-05-08 08:23:19 +00001281int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001282 Vdbe *p /* The VDBE */
1283){
drh5cfa5842009-12-31 20:35:08 +00001284 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001285 int nSub = 0; /* Number of sub-vdbes seen so far */
1286 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001287 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1288 sqlite3 *db = p->db; /* The database connection */
1289 int i; /* Loop counter */
1290 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001291 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001292
drh9a324642003-09-06 20:12:01 +00001293 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001294 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001295 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001296
drh9cbf3422008-01-17 16:22:13 +00001297 /* Even though this opcode does not use dynamic strings for
1298 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001299 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001300 */
dan165921a2009-08-28 18:53:45 +00001301 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001302 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001303
danielk19776c359f02008-11-21 16:58:03 +00001304 if( p->rc==SQLITE_NOMEM ){
1305 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1306 ** sqlite3_column_text16() failed. */
1307 db->mallocFailed = 1;
1308 return SQLITE_ERROR;
1309 }
1310
drh5cfa5842009-12-31 20:35:08 +00001311 /* When the number of output rows reaches nRow, that means the
1312 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1313 ** nRow is the sum of the number of rows in the main program, plus
1314 ** the sum of the number of rows in all trigger subprograms encountered
1315 ** so far. The nRow value will increase as new trigger subprograms are
1316 ** encountered, but p->pc will eventually catch up to nRow.
1317 */
dan165921a2009-08-28 18:53:45 +00001318 nRow = p->nOp;
1319 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001320 /* The first 8 memory cells are used for the result set. So we will
1321 ** commandeer the 9th cell to use as storage for an array of pointers
1322 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1323 ** cells. */
1324 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001325 pSub = &p->aMem[9];
1326 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001327 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1328 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001329 nSub = pSub->n/sizeof(Vdbe*);
1330 apSub = (SubProgram **)pSub->z;
1331 }
1332 for(i=0; i<nSub; i++){
1333 nRow += apSub[i]->nOp;
1334 }
1335 }
1336
drhecc92422005-09-10 16:46:12 +00001337 do{
1338 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001339 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1340 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001341 p->rc = SQLITE_OK;
1342 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001343 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001344 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001345 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +00001346 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001347 }else{
drh81316f82013-10-29 20:40:47 +00001348 char *zP4;
dan165921a2009-08-28 18:53:45 +00001349 Op *pOp;
1350 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001351 /* The output line number is small enough that we are still in the
1352 ** main program. */
dan165921a2009-08-28 18:53:45 +00001353 pOp = &p->aOp[i];
1354 }else{
drh5cfa5842009-12-31 20:35:08 +00001355 /* We are currently listing subprograms. Figure out which one and
1356 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001357 int j;
1358 i -= p->nOp;
1359 for(j=0; i>=apSub[j]->nOp; j++){
1360 i -= apSub[j]->nOp;
1361 }
1362 pOp = &apSub[j]->aOp[i];
1363 }
danielk19770d78bae2008-01-03 07:09:48 +00001364 if( p->explain==1 ){
1365 pMem->flags = MEM_Int;
drh1b27b8c2014-02-10 03:21:57 +00001366 pMem->memType = MEM_Int;
danielk19770d78bae2008-01-03 07:09:48 +00001367 pMem->u.i = i; /* Program counter */
1368 pMem++;
1369
1370 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001371 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
danielk19770d78bae2008-01-03 07:09:48 +00001372 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001373 pMem->n = sqlite3Strlen30(pMem->z);
drh1b27b8c2014-02-10 03:21:57 +00001374 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001375 pMem->enc = SQLITE_UTF8;
1376 pMem++;
dan165921a2009-08-28 18:53:45 +00001377
drh5cfa5842009-12-31 20:35:08 +00001378 /* When an OP_Program opcode is encounter (the only opcode that has
1379 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1380 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1381 ** has not already been seen.
1382 */
dan165921a2009-08-28 18:53:45 +00001383 if( pOp->p4type==P4_SUBPROGRAM ){
1384 int nByte = (nSub+1)*sizeof(SubProgram*);
1385 int j;
1386 for(j=0; j<nSub; j++){
1387 if( apSub[j]==pOp->p4.pProgram ) break;
1388 }
dan2b9ee772012-03-31 09:59:44 +00001389 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
dan165921a2009-08-28 18:53:45 +00001390 apSub = (SubProgram **)pSub->z;
1391 apSub[nSub++] = pOp->p4.pProgram;
1392 pSub->flags |= MEM_Blob;
1393 pSub->n = nSub*sizeof(SubProgram*);
1394 }
1395 }
danielk19770d78bae2008-01-03 07:09:48 +00001396 }
drheb2e1762004-05-27 01:53:56 +00001397
1398 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001399 pMem->u.i = pOp->p1; /* P1 */
drh1b27b8c2014-02-10 03:21:57 +00001400 pMem->memType = MEM_Int;
drheb2e1762004-05-27 01:53:56 +00001401 pMem++;
1402
1403 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001404 pMem->u.i = pOp->p2; /* P2 */
drh1b27b8c2014-02-10 03:21:57 +00001405 pMem->memType = MEM_Int;
drheb2e1762004-05-27 01:53:56 +00001406 pMem++;
1407
dan2ce22452010-11-08 19:01:16 +00001408 pMem->flags = MEM_Int;
1409 pMem->u.i = pOp->p3; /* P3 */
drh1b27b8c2014-02-10 03:21:57 +00001410 pMem->memType = MEM_Int;
dan2ce22452010-11-08 19:01:16 +00001411 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001412
danielk1977a7a8e142008-02-13 18:25:27 +00001413 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001414 assert( p->db->mallocFailed );
1415 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001416 }
drhc91b2fd2014-03-01 18:13:23 +00001417 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001418 zP4 = displayP4(pOp, pMem->z, 32);
1419 if( zP4!=pMem->z ){
1420 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
danielk1977a7a8e142008-02-13 18:25:27 +00001421 }else{
1422 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001423 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001424 pMem->enc = SQLITE_UTF8;
1425 }
drh1b27b8c2014-02-10 03:21:57 +00001426 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001427 pMem++;
drheb2e1762004-05-27 01:53:56 +00001428
danielk19770d78bae2008-01-03 07:09:48 +00001429 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +00001430 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +00001431 assert( p->db->mallocFailed );
1432 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001433 }
drhc91b2fd2014-03-01 18:13:23 +00001434 pMem->flags = MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001435 pMem->n = 2;
1436 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
drh1b27b8c2014-02-10 03:21:57 +00001437 pMem->memType = MEM_Str;
danielk19770d78bae2008-01-03 07:09:48 +00001438 pMem->enc = SQLITE_UTF8;
1439 pMem++;
1440
drhc7379ce2013-10-30 02:28:23 +00001441#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001442 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
1443 assert( p->db->mallocFailed );
1444 return SQLITE_ERROR;
drh52391cb2008-02-14 23:44:13 +00001445 }
drhc91b2fd2014-03-01 18:13:23 +00001446 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001447 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
drh1b27b8c2014-02-10 03:21:57 +00001448 pMem->memType = MEM_Str;
drh81316f82013-10-29 20:40:47 +00001449 pMem->enc = SQLITE_UTF8;
1450#else
1451 pMem->flags = MEM_Null; /* Comment */
drh1b27b8c2014-02-10 03:21:57 +00001452 pMem->memType = MEM_Null;
drh81316f82013-10-29 20:40:47 +00001453#endif
danielk19770d78bae2008-01-03 07:09:48 +00001454 }
1455
dan2ce22452010-11-08 19:01:16 +00001456 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001457 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001458 p->rc = SQLITE_OK;
1459 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001460 }
drh826fb5a2004-02-14 23:59:57 +00001461 return rc;
drh9a324642003-09-06 20:12:01 +00001462}
drhb7f91642004-10-31 02:22:47 +00001463#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001464
drh7c4ac0c2007-04-05 11:25:58 +00001465#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001466/*
drh3f7d4e42004-07-24 14:35:58 +00001467** Print the SQL that was used to generate a VDBE program.
1468*/
1469void sqlite3VdbePrintSql(Vdbe *p){
drh84e55a82013-11-13 17:58:23 +00001470 const char *z = 0;
1471 if( p->zSql ){
1472 z = p->zSql;
1473 }else if( p->nOp>=1 ){
1474 const VdbeOp *pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001475 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh84e55a82013-11-13 17:58:23 +00001476 z = pOp->p4.z;
1477 while( sqlite3Isspace(*z) ) z++;
1478 }
drh3f7d4e42004-07-24 14:35:58 +00001479 }
drh84e55a82013-11-13 17:58:23 +00001480 if( z ) printf("SQL: [%s]\n", z);
drh3f7d4e42004-07-24 14:35:58 +00001481}
drh7c4ac0c2007-04-05 11:25:58 +00001482#endif
drh3f7d4e42004-07-24 14:35:58 +00001483
drh602c2372007-03-01 00:29:13 +00001484#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1485/*
1486** Print an IOTRACE message showing SQL content.
1487*/
1488void sqlite3VdbeIOTraceSql(Vdbe *p){
1489 int nOp = p->nOp;
1490 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001491 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001492 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001493 pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001494 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001495 int i, j;
drh00a18e42007-08-13 11:10:34 +00001496 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001497 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001498 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001499 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001500 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001501 if( z[i-1]!=' ' ){
1502 z[j++] = ' ';
1503 }
1504 }else{
1505 z[j++] = z[i];
1506 }
1507 }
1508 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001509 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001510 }
1511}
1512#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1513
drhb2771ce2009-02-20 01:28:59 +00001514/*
drh4800b2e2009-12-08 15:35:22 +00001515** Allocate space from a fixed size buffer and return a pointer to
1516** that space. If insufficient space is available, return NULL.
1517**
1518** The pBuf parameter is the initial value of a pointer which will
1519** receive the new memory. pBuf is normally NULL. If pBuf is not
1520** NULL, it means that memory space has already been allocated and that
1521** this routine should not allocate any new memory. When pBuf is not
1522** NULL simply return pBuf. Only allocate new memory space when pBuf
1523** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001524**
1525** nByte is the number of bytes of space needed.
1526**
drh19875c82009-12-08 19:58:19 +00001527** *ppFrom points to available space and pEnd points to the end of the
1528** available space. When space is allocated, *ppFrom is advanced past
1529** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001530**
1531** *pnByte is a counter of the number of bytes of space that have failed
1532** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001533** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001534*/
drh4800b2e2009-12-08 15:35:22 +00001535static void *allocSpace(
1536 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001537 int nByte, /* Number of bytes to allocate */
1538 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001539 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001540 int *pnByte /* If allocation cannot be made, increment *pnByte */
1541){
drhea598cb2009-04-05 12:22:08 +00001542 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001543 if( pBuf ) return pBuf;
1544 nByte = ROUND8(nByte);
1545 if( &(*ppFrom)[nByte] <= pEnd ){
1546 pBuf = (void*)*ppFrom;
1547 *ppFrom += nByte;
1548 }else{
1549 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001550 }
drh4800b2e2009-12-08 15:35:22 +00001551 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001552}
drh602c2372007-03-01 00:29:13 +00001553
drh3f7d4e42004-07-24 14:35:58 +00001554/*
drh124c0b42011-06-01 18:15:55 +00001555** Rewind the VDBE back to the beginning in preparation for
1556** running it.
drh9a324642003-09-06 20:12:01 +00001557*/
drh124c0b42011-06-01 18:15:55 +00001558void sqlite3VdbeRewind(Vdbe *p){
1559#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1560 int i;
1561#endif
drh9a324642003-09-06 20:12:01 +00001562 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001563 assert( p->magic==VDBE_MAGIC_INIT );
1564
drhc16a03b2004-09-15 13:38:10 +00001565 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001566 */
drhc16a03b2004-09-15 13:38:10 +00001567 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001568
danielk197700e13612008-11-17 19:18:54 +00001569 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001570 p->magic = VDBE_MAGIC_RUN;
1571
drh124c0b42011-06-01 18:15:55 +00001572#ifdef SQLITE_DEBUG
1573 for(i=1; i<p->nMem; i++){
1574 assert( p->aMem[i].db==p->db );
1575 }
1576#endif
1577 p->pc = -1;
1578 p->rc = SQLITE_OK;
1579 p->errorAction = OE_Abort;
1580 p->magic = VDBE_MAGIC_RUN;
1581 p->nChange = 0;
1582 p->cacheCtr = 1;
1583 p->minWriteFileFormat = 255;
1584 p->iStatement = 0;
1585 p->nFkConstraint = 0;
1586#ifdef VDBE_PROFILE
1587 for(i=0; i<p->nOp; i++){
1588 p->aOp[i].cnt = 0;
1589 p->aOp[i].cycles = 0;
1590 }
1591#endif
1592}
1593
1594/*
1595** Prepare a virtual machine for execution for the first time after
1596** creating the virtual machine. This involves things such
1597** as allocating stack space and initializing the program counter.
1598** After the VDBE has be prepped, it can be executed by one or more
1599** calls to sqlite3VdbeExec().
1600**
1601** This function may be called exact once on a each virtual machine.
1602** After this routine is called the VM has been "packaged" and is ready
1603** to run. After this routine is called, futher calls to
1604** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1605** the Vdbe from the Parse object that helped generate it so that the
1606** the Vdbe becomes an independent entity and the Parse object can be
1607** destroyed.
1608**
1609** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1610** to its initial state after it has been run.
1611*/
1612void sqlite3VdbeMakeReady(
1613 Vdbe *p, /* The VDBE */
1614 Parse *pParse /* Parsing context */
1615){
1616 sqlite3 *db; /* The database connection */
1617 int nVar; /* Number of parameters */
1618 int nMem; /* Number of VM memory registers */
1619 int nCursor; /* Number of cursors required */
1620 int nArg; /* Number of arguments in subprograms */
dan1d8cb212011-12-09 13:24:16 +00001621 int nOnce; /* Number of OP_Once instructions */
drh124c0b42011-06-01 18:15:55 +00001622 int n; /* Loop counter */
1623 u8 *zCsr; /* Memory available for allocation */
1624 u8 *zEnd; /* First byte past allocated memory */
1625 int nByte; /* How much extra memory is needed */
1626
1627 assert( p!=0 );
1628 assert( p->nOp>0 );
1629 assert( pParse!=0 );
1630 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +00001631 assert( pParse==p->pParse );
drh124c0b42011-06-01 18:15:55 +00001632 db = p->db;
1633 assert( db->mallocFailed==0 );
1634 nVar = pParse->nVar;
1635 nMem = pParse->nMem;
1636 nCursor = pParse->nTab;
1637 nArg = pParse->nMaxArg;
dan1d8cb212011-12-09 13:24:16 +00001638 nOnce = pParse->nOnce;
drh20e226d2012-01-01 13:58:53 +00001639 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
drh124c0b42011-06-01 18:15:55 +00001640
danielk1977cd3e8f72008-03-25 09:47:35 +00001641 /* For each cursor required, also allocate a memory cell. Memory
1642 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1643 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001644 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001645 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1646 ** stores the blob of memory associated with cursor 1, etc.
1647 **
1648 ** See also: allocateCursor().
1649 */
1650 nMem += nCursor;
1651
danielk19776ab3a2e2009-02-19 14:39:25 +00001652 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001653 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001654 */
drh73d5b8f2013-12-23 19:09:07 +00001655 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1656 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001657
drh124c0b42011-06-01 18:15:55 +00001658 resolveP2Values(p, &nArg);
1659 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1660 if( pParse->explain && nMem<10 ){
1661 nMem = 10;
1662 }
1663 memset(zCsr, 0, zEnd-zCsr);
1664 zCsr += (zCsr - (u8*)0)&7;
1665 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001666 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001667
1668 /* Memory for registers, parameters, cursor, etc, is allocated in two
1669 ** passes. On the first pass, we try to reuse unused space at the
1670 ** end of the opcode array. If we are unable to satisfy all memory
1671 ** requirements by reusing the opcode array tail, then the second
1672 ** pass will fill in the rest using a fresh allocation.
1673 **
1674 ** This two-pass approach that reuses as much memory as possible from
1675 ** the leftover space at the end of the opcode array can significantly
1676 ** reduce the amount of memory held by a prepared statement.
1677 */
1678 do {
1679 nByte = 0;
1680 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1681 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1682 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1683 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1684 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1685 &zCsr, zEnd, &nByte);
drhb8475df2011-12-09 16:21:19 +00001686 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
drh124c0b42011-06-01 18:15:55 +00001687 if( nByte ){
1688 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001689 }
drh124c0b42011-06-01 18:15:55 +00001690 zCsr = p->pFree;
1691 zEnd = &zCsr[nByte];
1692 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001693
drhd2a56232013-01-28 19:00:20 +00001694 p->nCursor = nCursor;
dan1d8cb212011-12-09 13:24:16 +00001695 p->nOnceFlag = nOnce;
drh124c0b42011-06-01 18:15:55 +00001696 if( p->aVar ){
1697 p->nVar = (ynVar)nVar;
1698 for(n=0; n<nVar; n++){
1699 p->aVar[n].flags = MEM_Null;
1700 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001701 }
drh82a48512003-09-06 22:45:20 +00001702 }
drh124c0b42011-06-01 18:15:55 +00001703 if( p->azVar ){
1704 p->nzVar = pParse->nzVar;
1705 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1706 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001707 }
drh124c0b42011-06-01 18:15:55 +00001708 if( p->aMem ){
1709 p->aMem--; /* aMem[] goes from 1..nMem */
1710 p->nMem = nMem; /* not from 0..nMem-1 */
1711 for(n=1; n<=nMem; n++){
drha5750cf2014-02-07 13:20:31 +00001712 p->aMem[n].flags = MEM_Undefined;
drh124c0b42011-06-01 18:15:55 +00001713 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001714 }
drh9a324642003-09-06 20:12:01 +00001715 }
drh124c0b42011-06-01 18:15:55 +00001716 p->explain = pParse->explain;
1717 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001718}
1719
drh9a324642003-09-06 20:12:01 +00001720/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001721** Close a VDBE cursor and release all the resources that cursor
1722** happens to hold.
drh9a324642003-09-06 20:12:01 +00001723*/
drhdfe88ec2008-11-03 20:55:06 +00001724void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001725 if( pCx==0 ){
1726 return;
1727 }
dana20fde62011-07-12 14:28:05 +00001728 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001729 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001730 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001731 /* The pCx->pCursor will be close automatically, if it exists, by
1732 ** the call above. */
1733 }else if( pCx->pCursor ){
1734 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001735 }
drh9eff6162006-06-12 21:59:13 +00001736#ifndef SQLITE_OMIT_VIRTUALTABLE
1737 if( pCx->pVtabCursor ){
1738 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
drh5cc10232013-11-21 01:04:02 +00001739 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
danielk1977be718892006-06-23 08:05:19 +00001740 p->inVtabMethod = 1;
drh9eff6162006-06-12 21:59:13 +00001741 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00001742 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001743 }
1744#endif
drh9a324642003-09-06 20:12:01 +00001745}
1746
dan65a7cd12009-09-01 12:16:01 +00001747/*
1748** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1749** is used, for example, when a trigger sub-program is halted to restore
1750** control to the main program.
1751*/
dan165921a2009-08-28 18:53:45 +00001752int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1753 Vdbe *v = pFrame->v;
dan1d8cb212011-12-09 13:24:16 +00001754 v->aOnceFlag = pFrame->aOnceFlag;
1755 v->nOnceFlag = pFrame->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00001756 v->aOp = pFrame->aOp;
1757 v->nOp = pFrame->nOp;
1758 v->aMem = pFrame->aMem;
1759 v->nMem = pFrame->nMem;
1760 v->apCsr = pFrame->apCsr;
1761 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001762 v->db->lastRowid = pFrame->lastRowid;
1763 v->nChange = pFrame->nChange;
dan165921a2009-08-28 18:53:45 +00001764 return pFrame->pc;
1765}
1766
drh9a324642003-09-06 20:12:01 +00001767/*
drh5f82e3c2009-07-06 00:44:08 +00001768** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001769**
1770** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1771** cell array. This is necessary as the memory cell array may contain
1772** pointers to VdbeFrame objects, which may in turn contain pointers to
1773** open cursors.
drh9a324642003-09-06 20:12:01 +00001774*/
drh5f82e3c2009-07-06 00:44:08 +00001775static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001776 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001777 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001778 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1779 sqlite3VdbeFrameRestore(pFrame);
1780 }
1781 p->pFrame = 0;
1782 p->nFrame = 0;
1783
dan523a0872009-08-31 05:23:32 +00001784 if( p->apCsr ){
1785 int i;
1786 for(i=0; i<p->nCursor; i++){
1787 VdbeCursor *pC = p->apCsr[i];
1788 if( pC ){
1789 sqlite3VdbeFreeCursor(p, pC);
1790 p->apCsr[i] = 0;
1791 }
danielk1977be718892006-06-23 08:05:19 +00001792 }
drh9a324642003-09-06 20:12:01 +00001793 }
dan523a0872009-08-31 05:23:32 +00001794 if( p->aMem ){
1795 releaseMemArray(&p->aMem[1], p->nMem);
1796 }
dan27106572010-12-01 08:04:47 +00001797 while( p->pDelFrame ){
1798 VdbeFrame *pDel = p->pDelFrame;
1799 p->pDelFrame = pDel->pParent;
1800 sqlite3VdbeFrameDelete(pDel);
1801 }
dan0c547792013-07-18 17:12:08 +00001802
1803 /* Delete any auxdata allocations made by the VM */
1804 sqlite3VdbeDeleteAuxData(p, -1, 0);
1805 assert( p->pAuxData==0 );
drh9a324642003-09-06 20:12:01 +00001806}
1807
1808/*
drh9a324642003-09-06 20:12:01 +00001809** Clean up the VM after execution.
1810**
1811** This routine will automatically close any cursors, lists, and/or
1812** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001813** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001814*/
drhc890fec2008-08-01 20:10:08 +00001815static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001816 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001817
1818#ifdef SQLITE_DEBUG
1819 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1820 ** Vdbe.aMem[] arrays have already been cleaned up. */
1821 int i;
drhb8475df2011-12-09 16:21:19 +00001822 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1823 if( p->aMem ){
drha5750cf2014-02-07 13:20:31 +00001824 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
drhb8475df2011-12-09 16:21:19 +00001825 }
dan165921a2009-08-28 18:53:45 +00001826#endif
1827
drh633e6d52008-07-28 19:34:53 +00001828 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001829 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001830 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001831}
1832
1833/*
danielk197722322fd2004-05-25 23:35:17 +00001834** Set the number of result columns that will be returned by this SQL
1835** statement. This is now set at compile time, rather than during
1836** execution of the vdbe program so that sqlite3_column_count() can
1837** be called on an SQL statement before sqlite3_step().
1838*/
1839void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001840 Mem *pColName;
1841 int n;
drh633e6d52008-07-28 19:34:53 +00001842 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001843
drhc890fec2008-08-01 20:10:08 +00001844 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001845 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001846 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001847 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001848 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001849 if( p->aColName==0 ) return;
1850 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001851 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001852 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001853 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001854 }
danielk197722322fd2004-05-25 23:35:17 +00001855}
1856
1857/*
danielk19773cf86062004-05-26 10:11:05 +00001858** Set the name of the idx'th column to be returned by the SQL statement.
1859** zName must be a pointer to a nul terminated string.
1860**
1861** This call must be made after a call to sqlite3VdbeSetNumCols().
1862**
danielk197710fb7492008-10-31 10:53:22 +00001863** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1864** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1865** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001866*/
danielk197710fb7492008-10-31 10:53:22 +00001867int sqlite3VdbeSetColName(
1868 Vdbe *p, /* Vdbe being configured */
1869 int idx, /* Index of column zName applies to */
1870 int var, /* One of the COLNAME_* constants */
1871 const char *zName, /* Pointer to buffer containing name */
1872 void (*xDel)(void*) /* Memory management strategy for zName */
1873){
danielk19773cf86062004-05-26 10:11:05 +00001874 int rc;
1875 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001876 assert( idx<p->nResColumn );
1877 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001878 if( p->db->mallocFailed ){
1879 assert( !zName || xDel!=SQLITE_DYNAMIC );
1880 return SQLITE_NOMEM;
1881 }
drh76ff3a02004-09-24 22:32:30 +00001882 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001883 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001884 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001885 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001886 return rc;
1887}
1888
danielk197713adf8a2004-06-03 16:08:41 +00001889/*
1890** A read or write transaction may or may not be active on database handle
1891** db. If a transaction is active, commit it. If there is a
1892** write-transaction spanning more than one database file, this routine
1893** takes care of the master journal trickery.
1894*/
danielk19773e3a84d2008-08-01 17:37:40 +00001895static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001896 int i;
1897 int nTrans = 0; /* Number of databases with an active write-transaction */
1898 int rc = SQLITE_OK;
1899 int needXcommit = 0;
1900
shane36840fd2009-06-26 16:32:13 +00001901#ifdef SQLITE_OMIT_VIRTUALTABLE
1902 /* With this option, sqlite3VtabSync() is defined to be simply
1903 ** SQLITE_OK so p is not used.
1904 */
1905 UNUSED_PARAMETER(p);
1906#endif
1907
danielk19775bd270b2006-07-25 15:14:52 +00001908 /* Before doing anything else, call the xSync() callback for any
1909 ** virtual module tables written in this transaction. This has to
1910 ** be done before determining whether a master journal file is
1911 ** required, as an xSync() callback may add an attached database
1912 ** to the transaction.
1913 */
dan016f7812013-08-21 17:35:48 +00001914 rc = sqlite3VtabSync(db, p);
danielk19775bd270b2006-07-25 15:14:52 +00001915
1916 /* This loop determines (a) if the commit hook should be invoked and
1917 ** (b) how many database files have open write transactions, not
1918 ** including the temp database. (b) is important because if more than
1919 ** one database file has an open write transaction, a master journal
1920 ** file is required for an atomic commit.
1921 */
drhabfb62f2010-07-30 11:20:35 +00001922 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001923 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001924 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001925 needXcommit = 1;
1926 if( i!=1 ) nTrans++;
dan6b9bb592012-10-05 19:43:02 +00001927 sqlite3BtreeEnter(pBt);
drhabfb62f2010-07-30 11:20:35 +00001928 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
dan6b9bb592012-10-05 19:43:02 +00001929 sqlite3BtreeLeave(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001930 }
1931 }
drhabfb62f2010-07-30 11:20:35 +00001932 if( rc!=SQLITE_OK ){
1933 return rc;
1934 }
danielk197713adf8a2004-06-03 16:08:41 +00001935
1936 /* If there are any write-transactions at all, invoke the commit hook */
1937 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001938 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00001939 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00001940 return SQLITE_CONSTRAINT_COMMITHOOK;
danielk197713adf8a2004-06-03 16:08:41 +00001941 }
1942 }
1943
danielk197740b38dc2004-06-26 08:38:24 +00001944 /* The simple case - no more than one database file (not counting the
1945 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001946 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001947 **
danielk197740b38dc2004-06-26 08:38:24 +00001948 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001949 ** string, it means the main database is :memory: or a temp file. In
1950 ** that case we do not support atomic multi-file commits, so use the
1951 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001952 */
drhea678832008-12-10 19:26:22 +00001953 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1954 || nTrans<=1
1955 ){
danielk197704103022009-02-03 16:51:24 +00001956 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001957 Btree *pBt = db->aDb[i].pBt;
1958 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001959 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001960 }
1961 }
1962
drh80e35f42007-03-30 14:06:34 +00001963 /* Do the commit only if all databases successfully complete phase 1.
1964 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1965 ** IO error while deleting or truncating a journal file. It is unlikely,
1966 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001967 */
1968 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1969 Btree *pBt = db->aDb[i].pBt;
1970 if( pBt ){
dan60939d02011-03-29 15:40:55 +00001971 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001972 }
danielk1977979f38e2007-03-27 16:19:51 +00001973 }
1974 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001975 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001976 }
1977 }
1978
1979 /* The complex case - There is a multi-file write-transaction active.
1980 ** This requires a master journal file to ensure the transaction is
1981 ** committed atomicly.
1982 */
danielk197744ee5bf2005-05-27 09:41:12 +00001983#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001984 else{
danielk1977b4b47412007-08-17 15:53:36 +00001985 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001986 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001987 char *zMaster = 0; /* File-name for the master journal */
1988 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001989 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001990 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001991 int res;
drhf5808602011-12-16 00:33:04 +00001992 int retryCount = 0;
drh5c531a42011-12-16 01:21:31 +00001993 int nMainFile;
danielk197713adf8a2004-06-03 16:08:41 +00001994
1995 /* Select a master journal file name */
drh5c531a42011-12-16 01:21:31 +00001996 nMainFile = sqlite3Strlen30(zMainFile);
drh52bcde02012-01-03 14:50:45 +00001997 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
drh5c531a42011-12-16 01:21:31 +00001998 if( zMaster==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00001999 do {
drhdc5ea5c2008-12-10 17:19:59 +00002000 u32 iRandom;
drh84968c02011-12-16 15:11:39 +00002001 if( retryCount ){
2002 if( retryCount>100 ){
2003 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2004 sqlite3OsDelete(pVfs, zMaster, 0);
2005 break;
2006 }else if( retryCount==1 ){
2007 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2008 }
danielk197713adf8a2004-06-03 16:08:41 +00002009 }
drh84968c02011-12-16 15:11:39 +00002010 retryCount++;
danielk197713adf8a2004-06-03 16:08:41 +00002011 sqlite3_randomness(sizeof(iRandom), &iRandom);
drh5c531a42011-12-16 01:21:31 +00002012 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
drhf5808602011-12-16 00:33:04 +00002013 (iRandom>>8)&0xffffff, iRandom&0xff);
drhf5808602011-12-16 00:33:04 +00002014 /* The antipenultimate character of the master journal name must
2015 ** be "9" to avoid name collisions when using 8+3 filenames. */
drh5c531a42011-12-16 01:21:31 +00002016 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
drh81cc5162011-05-17 20:36:21 +00002017 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00002018 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2019 }while( rc==SQLITE_OK && res );
2020 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00002021 /* Open the master journal. */
2022 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2023 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2024 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2025 );
2026 }
danielk197713adf8a2004-06-03 16:08:41 +00002027 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002028 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002029 return rc;
2030 }
2031
2032 /* Write the name of each database file in the transaction into the new
2033 ** master journal file. If an error occurs at this point close
2034 ** and delete the master journal file. All the individual journal files
2035 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00002036 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00002037 */
danielk19771e536952007-08-16 10:09:01 +00002038 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002039 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002040 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00002041 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00002042 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00002043 continue; /* Ignore TEMP and :memory: databases */
2044 }
drh8c96a6e2010-08-31 01:09:15 +00002045 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00002046 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2047 needSync = 1;
2048 }
drhea678832008-12-10 19:26:22 +00002049 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2050 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00002051 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00002052 sqlite3OsCloseFree(pMaster);
2053 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002054 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002055 return rc;
2056 }
2057 }
2058 }
2059
danielk19779663b8f2007-08-24 11:52:28 +00002060 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2061 ** flag is set this is not required.
2062 */
danielk1977bea2a942009-01-20 17:06:27 +00002063 if( needSync
2064 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2065 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2066 ){
danielk1977fee2d252007-08-18 10:59:19 +00002067 sqlite3OsCloseFree(pMaster);
2068 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002069 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00002070 return rc;
2071 }
drhc9e06862004-06-09 20:03:08 +00002072
danielk197713adf8a2004-06-03 16:08:41 +00002073 /* Sync all the db files involved in the transaction. The same call
2074 ** sets the master journal pointer in each individual journal. If
2075 ** an error occurs here, do not delete the master journal file.
2076 **
drh80e35f42007-03-30 14:06:34 +00002077 ** If the error occurs during the first call to
2078 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2079 ** master journal file will be orphaned. But we cannot delete it,
2080 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00002081 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00002082 */
danielk19775bd270b2006-07-25 15:14:52 +00002083 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002084 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002085 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002086 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002087 }
2088 }
danielk1977fee2d252007-08-18 10:59:19 +00002089 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00002090 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00002091 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002092 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00002093 return rc;
2094 }
danielk197713adf8a2004-06-03 16:08:41 +00002095
danielk1977962398d2004-06-14 09:35:16 +00002096 /* Delete the master journal file. This commits the transaction. After
2097 ** doing this the directory is synced again before any individual
2098 ** transaction files are deleted.
2099 */
danielk1977fee2d252007-08-18 10:59:19 +00002100 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00002101 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00002102 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00002103 if( rc ){
2104 return rc;
2105 }
danielk197713adf8a2004-06-03 16:08:41 +00002106
2107 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00002108 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2109 ** deleting or truncating journals. If something goes wrong while
2110 ** this is happening we don't really care. The integrity of the
2111 ** transaction is already guaranteed, but some stray 'cold' journals
2112 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00002113 */
danielk1977979f38e2007-03-27 16:19:51 +00002114 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002115 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00002116 for(i=0; i<db->nDb; i++){
2117 Btree *pBt = db->aDb[i].pBt;
2118 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002119 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00002120 }
2121 }
danielk19772d1d86f2008-06-20 14:59:51 +00002122 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00002123 enable_simulated_io_errors();
2124
danielk1977f9e7dda2006-06-16 16:08:53 +00002125 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002126 }
danielk197744ee5bf2005-05-27 09:41:12 +00002127#endif
danielk1977026d2702004-06-14 13:14:59 +00002128
drh2ac3ee92004-06-07 16:27:46 +00002129 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002130}
2131
danielk19771d850a72004-05-31 08:26:49 +00002132/*
drh4f7d3a52013-06-27 23:54:02 +00002133** This routine checks that the sqlite3.nVdbeActive count variable
danielk19771d850a72004-05-31 08:26:49 +00002134** matches the number of vdbe's in the list sqlite3.pVdbe that are
2135** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00002136** This is an internal self-check only - it is not an essential processing
2137** step.
danielk19771d850a72004-05-31 08:26:49 +00002138**
2139** This is a no-op if NDEBUG is defined.
2140*/
2141#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00002142static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00002143 Vdbe *p;
2144 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00002145 int nWrite = 0;
drh4f7d3a52013-06-27 23:54:02 +00002146 int nRead = 0;
danielk19771d850a72004-05-31 08:26:49 +00002147 p = db->pVdbe;
2148 while( p ){
drh92f02c32004-09-02 14:57:08 +00002149 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002150 cnt++;
drhad4a4b82008-11-05 16:37:34 +00002151 if( p->readOnly==0 ) nWrite++;
drh1713afb2013-06-28 01:24:57 +00002152 if( p->bIsReader ) nRead++;
danielk19771d850a72004-05-31 08:26:49 +00002153 }
2154 p = p->pNext;
2155 }
drh4f7d3a52013-06-27 23:54:02 +00002156 assert( cnt==db->nVdbeActive );
2157 assert( nWrite==db->nVdbeWrite );
2158 assert( nRead==db->nVdbeRead );
danielk19771d850a72004-05-31 08:26:49 +00002159}
2160#else
2161#define checkActiveVdbeCnt(x)
2162#endif
2163
danielk19773cf86062004-05-26 10:11:05 +00002164/*
danielk1977bd434552009-03-18 10:33:00 +00002165** If the Vdbe passed as the first argument opened a statement-transaction,
2166** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2167** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2168** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
drhf7b54962013-05-28 12:11:54 +00002169** statement transaction is committed.
danielk1977bd434552009-03-18 10:33:00 +00002170**
2171** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2172** Otherwise SQLITE_OK.
2173*/
2174int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002175 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002176 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002177
danielk1977e4948172009-07-17 17:25:43 +00002178 /* If p->iStatement is greater than zero, then this Vdbe opened a
2179 ** statement transaction that should be closed here. The only exception
mistachkin48864df2013-03-21 21:20:32 +00002180 ** is that an IO error may have occurred, causing an emergency rollback.
danielk1977e4948172009-07-17 17:25:43 +00002181 ** In this case (db->nStatement==0), and there is nothing to do.
2182 */
2183 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002184 int i;
2185 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002186
2187 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2188 assert( db->nStatement>0 );
2189 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2190
2191 for(i=0; i<db->nDb; i++){
2192 int rc2 = SQLITE_OK;
2193 Btree *pBt = db->aDb[i].pBt;
2194 if( pBt ){
2195 if( eOp==SAVEPOINT_ROLLBACK ){
2196 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2197 }
2198 if( rc2==SQLITE_OK ){
2199 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2200 }
2201 if( rc==SQLITE_OK ){
2202 rc = rc2;
2203 }
2204 }
2205 }
2206 db->nStatement--;
2207 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002208
dana311b802011-04-26 19:21:34 +00002209 if( rc==SQLITE_OK ){
2210 if( eOp==SAVEPOINT_ROLLBACK ){
2211 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2212 }
2213 if( rc==SQLITE_OK ){
2214 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2215 }
2216 }
2217
dan1da40a32009-09-19 17:00:31 +00002218 /* If the statement transaction is being rolled back, also restore the
2219 ** database handles deferred constraint counter to the value it had when
2220 ** the statement transaction was opened. */
2221 if( eOp==SAVEPOINT_ROLLBACK ){
2222 db->nDeferredCons = p->nStmtDefCons;
drh648e2642013-07-11 15:03:32 +00002223 db->nDeferredImmCons = p->nStmtDefImmCons;
dan1da40a32009-09-19 17:00:31 +00002224 }
danielk1977bd434552009-03-18 10:33:00 +00002225 }
2226 return rc;
2227}
2228
2229/*
dan1da40a32009-09-19 17:00:31 +00002230** This function is called when a transaction opened by the database
2231** handle associated with the VM passed as an argument is about to be
2232** committed. If there are outstanding deferred foreign key constraint
2233** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2234**
2235** If there are outstanding FK violations and this function returns
drhd91c1a12013-02-09 13:58:25 +00002236** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2237** and write an error message to it. Then return SQLITE_ERROR.
dan1da40a32009-09-19 17:00:31 +00002238*/
2239#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002240int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002241 sqlite3 *db = p->db;
drh648e2642013-07-11 15:03:32 +00002242 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2243 || (!deferred && p->nFkConstraint>0)
2244 ){
drhd91c1a12013-02-09 13:58:25 +00002245 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan32b09f22009-09-23 17:29:59 +00002246 p->errorAction = OE_Abort;
drhf9c8ce32013-11-05 13:33:55 +00002247 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
dan1da40a32009-09-19 17:00:31 +00002248 return SQLITE_ERROR;
2249 }
2250 return SQLITE_OK;
2251}
2252#endif
2253
2254/*
drh92f02c32004-09-02 14:57:08 +00002255** This routine is called the when a VDBE tries to halt. If the VDBE
2256** has made changes and is in autocommit mode, then commit those
2257** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002258**
drh92f02c32004-09-02 14:57:08 +00002259** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002260** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2261** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002262**
2263** Return an error code. If the commit could not complete because of
2264** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2265** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002266*/
drhff0587c2007-08-29 17:43:19 +00002267int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002268 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002269 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002270
2271 /* This function contains the logic that determines if a statement or
2272 ** transaction will be committed or rolled back as a result of the
2273 ** execution of this virtual machine.
2274 **
drh71b890a2007-10-03 15:30:52 +00002275 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002276 **
drh71b890a2007-10-03 15:30:52 +00002277 ** SQLITE_NOMEM
2278 ** SQLITE_IOERR
2279 ** SQLITE_FULL
2280 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002281 **
drh71b890a2007-10-03 15:30:52 +00002282 ** Then the internal cache might have been left in an inconsistent
2283 ** state. We need to rollback the statement transaction, if there is
2284 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002285 */
drh9a324642003-09-06 20:12:01 +00002286
drh17435752007-08-16 04:30:38 +00002287 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002288 p->rc = SQLITE_NOMEM;
2289 }
drh6e856bc2011-12-09 18:06:44 +00002290 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
drh5f82e3c2009-07-06 00:44:08 +00002291 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002292 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002293 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002294 }
danielk19771d850a72004-05-31 08:26:49 +00002295 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002296
danc0537fe2013-06-28 19:41:43 +00002297 /* No commit or rollback needed if the program never started or if the
2298 ** SQL statement does not read or write a database file. */
2299 if( p->pc>=0 && p->bIsReader ){
drhaac2f552006-09-23 21:44:23 +00002300 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002301 int eStatementOp = 0;
2302 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002303
2304 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002305 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002306
drh71b890a2007-10-03 15:30:52 +00002307 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002308 mrc = p->rc & 0xff;
drhfa3be902009-07-07 02:44:07 +00002309 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
drh71b890a2007-10-03 15:30:52 +00002310 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002311 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002312 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002313 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2314 ** no rollback is necessary. Otherwise, at least a savepoint
2315 ** transaction must be rolled back to restore the database to a
2316 ** consistent state.
2317 **
2318 ** Even if the statement is read-only, it is important to perform
2319 ** a statement or transaction rollback operation. If the error
mistachkin48864df2013-03-21 21:20:32 +00002320 ** occurred while writing to the journal, sub-journal or database
dan5653e4d2010-08-12 11:25:47 +00002321 ** file as part of an effort to free up cache space (see function
2322 ** pagerStress() in pager.c), the rollback is required to restore
2323 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002324 */
drhad4a4b82008-11-05 16:37:34 +00002325 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002326 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002327 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002328 }else{
2329 /* We are forced to roll back the active transaction. Before doing
2330 ** so, abort any other statements this handle currently has active.
2331 */
drh21021a52012-02-13 17:01:51 +00002332 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002333 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002334 db->autoCommit = 1;
2335 }
danielk1977261919c2005-12-06 12:52:59 +00002336 }
2337 }
dan32b09f22009-09-23 17:29:59 +00002338
2339 /* Check for immediate foreign key violations. */
2340 if( p->rc==SQLITE_OK ){
2341 sqlite3VdbeCheckFk(p, 0);
2342 }
danielk197707cb5602006-01-20 10:55:05 +00002343
danielk1977bd434552009-03-18 10:33:00 +00002344 /* If the auto-commit flag is set and this is the only active writer
2345 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002346 **
2347 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002348 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002349 */
danielk1977093e0f62008-11-13 18:00:14 +00002350 if( !sqlite3VtabInSync(db)
2351 && db->autoCommit
drh4f7d3a52013-06-27 23:54:02 +00002352 && db->nVdbeWrite==(p->readOnly==0)
danielk1977093e0f62008-11-13 18:00:14 +00002353 ){
danielk197707cb5602006-01-20 10:55:05 +00002354 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002355 rc = sqlite3VdbeCheckFk(p, 1);
2356 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002357 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002358 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002359 return SQLITE_ERROR;
2360 }
drhd91c1a12013-02-09 13:58:25 +00002361 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan19611b12011-01-24 16:00:58 +00002362 }else{
2363 /* The auto-commit flag is true, the vdbe program was successful
2364 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2365 ** key constraints to hold up the transaction. This means a commit
2366 ** is required. */
2367 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002368 }
dan19611b12011-01-24 16:00:58 +00002369 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002370 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002371 return SQLITE_BUSY;
2372 }else if( rc!=SQLITE_OK ){
2373 p->rc = rc;
drh0f198a72012-02-13 16:43:16 +00002374 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002375 }else{
dan1da40a32009-09-19 17:00:31 +00002376 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00002377 db->nDeferredImmCons = 0;
2378 db->flags &= ~SQLITE_DeferFKs;
danielk197707cb5602006-01-20 10:55:05 +00002379 sqlite3CommitInternalChanges(db);
2380 }
2381 }else{
drh0f198a72012-02-13 16:43:16 +00002382 sqlite3RollbackAll(db, SQLITE_OK);
danielk197707cb5602006-01-20 10:55:05 +00002383 }
danielk1977bd434552009-03-18 10:33:00 +00002384 db->nStatement = 0;
2385 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002386 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002387 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002388 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002389 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002390 }else{
drh21021a52012-02-13 17:01:51 +00002391 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002392 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002393 db->autoCommit = 1;
2394 }
danielk19771d850a72004-05-31 08:26:49 +00002395 }
danielk197707cb5602006-01-20 10:55:05 +00002396
danielk1977bd434552009-03-18 10:33:00 +00002397 /* If eStatementOp is non-zero, then a statement transaction needs to
2398 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2399 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002400 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2401 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002402 */
danielk1977bd434552009-03-18 10:33:00 +00002403 if( eStatementOp ){
2404 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002405 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002406 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002407 p->rc = rc;
2408 sqlite3DbFree(db, p->zErrMsg);
2409 p->zErrMsg = 0;
2410 }
drh21021a52012-02-13 17:01:51 +00002411 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
dan40ad9d22010-06-03 09:17:38 +00002412 sqlite3CloseSavepoints(db);
2413 db->autoCommit = 1;
danielk197707cb5602006-01-20 10:55:05 +00002414 }
danielk197777d83ba2004-05-31 10:08:14 +00002415 }
danielk197707cb5602006-01-20 10:55:05 +00002416
danielk1977bd434552009-03-18 10:33:00 +00002417 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2418 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002419 */
drh6be240e2009-07-14 02:33:02 +00002420 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002421 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002422 sqlite3VdbeSetChanges(db, p->nChange);
2423 }else{
2424 sqlite3VdbeSetChanges(db, 0);
2425 }
2426 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002427 }
drhff0587c2007-08-29 17:43:19 +00002428
2429 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002430 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002431 }
danielk19771d850a72004-05-31 08:26:49 +00002432
danielk197765fd59f2006-06-24 11:51:33 +00002433 /* We have successfully halted and closed the VM. Record this fact. */
2434 if( p->pc>=0 ){
drh4f7d3a52013-06-27 23:54:02 +00002435 db->nVdbeActive--;
2436 if( !p->readOnly ) db->nVdbeWrite--;
drh1713afb2013-06-28 01:24:57 +00002437 if( p->bIsReader ) db->nVdbeRead--;
drh4f7d3a52013-06-27 23:54:02 +00002438 assert( db->nVdbeActive>=db->nVdbeRead );
2439 assert( db->nVdbeRead>=db->nVdbeWrite );
2440 assert( db->nVdbeWrite>=0 );
drh9a324642003-09-06 20:12:01 +00002441 }
drh92f02c32004-09-02 14:57:08 +00002442 p->magic = VDBE_MAGIC_HALT;
2443 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002444 if( p->db->mallocFailed ){
2445 p->rc = SQLITE_NOMEM;
2446 }
danielk19771d850a72004-05-31 08:26:49 +00002447
danielk1977404ca072009-03-16 13:19:36 +00002448 /* If the auto-commit flag is set to true, then any locks that were held
2449 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2450 ** to invoke any required unlock-notify callbacks.
2451 */
2452 if( db->autoCommit ){
2453 sqlite3ConnectionUnlocked(db);
2454 }
2455
drh4f7d3a52013-06-27 23:54:02 +00002456 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002457 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002458}
drh4cf7c7f2007-08-28 23:28:07 +00002459
drh92f02c32004-09-02 14:57:08 +00002460
2461/*
drh3c23a882007-01-09 14:01:13 +00002462** Each VDBE holds the result of the most recent sqlite3_step() call
2463** in p->rc. This routine sets that result back to SQLITE_OK.
2464*/
2465void sqlite3VdbeResetStepResult(Vdbe *p){
2466 p->rc = SQLITE_OK;
2467}
2468
2469/*
dan029ead62011-10-27 15:19:58 +00002470** Copy the error code and error message belonging to the VDBE passed
2471** as the first argument to its database handle (so that they will be
2472** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2473**
2474** This function does not clear the VDBE error code or message, just
2475** copies them to the database handle.
2476*/
2477int sqlite3VdbeTransferError(Vdbe *p){
2478 sqlite3 *db = p->db;
2479 int rc = p->rc;
2480 if( p->zErrMsg ){
drh81bdd6d2011-10-29 01:33:24 +00002481 u8 mallocFailed = db->mallocFailed;
dan029ead62011-10-27 15:19:58 +00002482 sqlite3BeginBenignMalloc();
drha3cc0072013-12-13 16:23:55 +00002483 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
dan029ead62011-10-27 15:19:58 +00002484 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2485 sqlite3EndBenignMalloc();
drh81bdd6d2011-10-29 01:33:24 +00002486 db->mallocFailed = mallocFailed;
dan029ead62011-10-27 15:19:58 +00002487 db->errCode = rc;
2488 }else{
2489 sqlite3Error(db, rc, 0);
2490 }
2491 return rc;
2492}
2493
danac455932012-11-26 19:50:41 +00002494#ifdef SQLITE_ENABLE_SQLLOG
2495/*
2496** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2497** invoke it.
2498*/
2499static void vdbeInvokeSqllog(Vdbe *v){
2500 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2501 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2502 assert( v->db->init.busy==0 );
2503 if( zExpanded ){
2504 sqlite3GlobalConfig.xSqllog(
2505 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2506 );
2507 sqlite3DbFree(v->db, zExpanded);
2508 }
2509 }
2510}
2511#else
2512# define vdbeInvokeSqllog(x)
2513#endif
2514
dan029ead62011-10-27 15:19:58 +00002515/*
drh92f02c32004-09-02 14:57:08 +00002516** Clean up a VDBE after execution but do not delete the VDBE just yet.
2517** Write any error messages into *pzErrMsg. Return the result code.
2518**
2519** After this routine is run, the VDBE should be ready to be executed
2520** again.
2521**
2522** To look at it another way, this routine resets the state of the
2523** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2524** VDBE_MAGIC_INIT.
2525*/
drhc890fec2008-08-01 20:10:08 +00002526int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002527 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002528 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002529
2530 /* If the VM did not run to completion or if it encountered an
2531 ** error, then it might not have been halted properly. So halt
2532 ** it now.
2533 */
2534 sqlite3VdbeHalt(p);
2535
drhfb7e7652005-01-24 00:28:42 +00002536 /* If the VDBE has be run even partially, then transfer the error code
2537 ** and error message from the VDBE into the main database structure. But
2538 ** if the VDBE has just been set to run but has not actually executed any
2539 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002540 */
drhfb7e7652005-01-24 00:28:42 +00002541 if( p->pc>=0 ){
danac455932012-11-26 19:50:41 +00002542 vdbeInvokeSqllog(p);
dan029ead62011-10-27 15:19:58 +00002543 sqlite3VdbeTransferError(p);
2544 sqlite3DbFree(db, p->zErrMsg);
2545 p->zErrMsg = 0;
drh4611d922010-02-25 14:47:01 +00002546 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002547 }else if( p->rc && p->expired ){
2548 /* The expired flag was set on the VDBE before the first call
2549 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2550 ** called), set the database error in this case as well.
2551 */
drha3cc0072013-12-13 16:23:55 +00002552 sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002553 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002554 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002555 }
2556
2557 /* Reclaim all memory used by the VDBE
2558 */
drhc890fec2008-08-01 20:10:08 +00002559 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002560
2561 /* Save profiling information from this VDBE run.
2562 */
drh9a324642003-09-06 20:12:01 +00002563#ifdef VDBE_PROFILE
2564 {
2565 FILE *out = fopen("vdbe_profile.out", "a");
2566 if( out ){
2567 int i;
2568 fprintf(out, "---- ");
2569 for(i=0; i<p->nOp; i++){
2570 fprintf(out, "%02x", p->aOp[i].opcode);
2571 }
2572 fprintf(out, "\n");
drh2926f962014-02-17 01:13:28 +00002573 if( p->zSql ){
2574 char c, pc = 0;
2575 fprintf(out, "-- ");
2576 for(i=0; (c = p->zSql[i])!=0; i++){
2577 if( pc=='\n' ) fprintf(out, "-- ");
2578 putc(c, out);
2579 pc = c;
2580 }
2581 if( pc!='\n' ) fprintf(out, "\n");
2582 }
drh9a324642003-09-06 20:12:01 +00002583 for(i=0; i<p->nOp; i++){
drh15ab9412014-02-24 14:24:01 +00002584 char zHdr[100];
2585 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
drh9a324642003-09-06 20:12:01 +00002586 p->aOp[i].cnt,
2587 p->aOp[i].cycles,
2588 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2589 );
drh15ab9412014-02-24 14:24:01 +00002590 fprintf(out, "%s", zHdr);
danielk19774adee202004-05-08 08:23:19 +00002591 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002592 }
2593 fclose(out);
2594 }
2595 }
2596#endif
drh7fa20922013-09-17 23:36:33 +00002597 p->iCurrentTime = 0;
drh9a324642003-09-06 20:12:01 +00002598 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002599 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002600}
drh92f02c32004-09-02 14:57:08 +00002601
drh9a324642003-09-06 20:12:01 +00002602/*
2603** Clean up and delete a VDBE after execution. Return an integer which is
2604** the result code. Write any error message text into *pzErrMsg.
2605*/
danielk19779e6db7d2004-06-21 08:18:51 +00002606int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002607 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002608 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002609 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002610 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002611 }
danielk19774adee202004-05-08 08:23:19 +00002612 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002613 return rc;
2614}
2615
2616/*
dan0c547792013-07-18 17:12:08 +00002617** If parameter iOp is less than zero, then invoke the destructor for
2618** all auxiliary data pointers currently cached by the VM passed as
2619** the first argument.
2620**
2621** Or, if iOp is greater than or equal to zero, then the destructor is
2622** only invoked for those auxiliary data pointers created by the user
2623** function invoked by the OP_Function opcode at instruction iOp of
2624** VM pVdbe, and only then if:
2625**
2626** * the associated function parameter is the 32nd or later (counting
2627** from left to right), or
2628**
2629** * the corresponding bit in argument mask is clear (where the first
2630** function parameter corrsponds to bit 0 etc.).
drhf92c7ff2004-06-19 15:40:23 +00002631*/
dan0c547792013-07-18 17:12:08 +00002632void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2633 AuxData **pp = &pVdbe->pAuxData;
2634 while( *pp ){
2635 AuxData *pAux = *pp;
2636 if( (iOp<0)
drh693e6712014-01-24 22:58:00 +00002637 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
dan0c547792013-07-18 17:12:08 +00002638 ){
drh693e6712014-01-24 22:58:00 +00002639 testcase( pAux->iArg==31 );
drhf92c7ff2004-06-19 15:40:23 +00002640 if( pAux->xDelete ){
2641 pAux->xDelete(pAux->pAux);
2642 }
dan0c547792013-07-18 17:12:08 +00002643 *pp = pAux->pNext;
2644 sqlite3DbFree(pVdbe->db, pAux);
2645 }else{
2646 pp= &pAux->pNext;
drhf92c7ff2004-06-19 15:40:23 +00002647 }
2648 }
2649}
2650
2651/*
drhcb103b92012-10-26 00:11:23 +00002652** Free all memory associated with the Vdbe passed as the second argument,
2653** except for object itself, which is preserved.
2654**
dand46def72010-07-24 11:28:28 +00002655** The difference between this function and sqlite3VdbeDelete() is that
2656** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
drhcb103b92012-10-26 00:11:23 +00002657** the database connection and frees the object itself.
dand46def72010-07-24 11:28:28 +00002658*/
drhcb103b92012-10-26 00:11:23 +00002659void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002660 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002661 int i;
dand46def72010-07-24 11:28:28 +00002662 assert( p->db==0 || p->db==db );
2663 releaseMemArray(p->aVar, p->nVar);
2664 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002665 for(pSub=p->pProgram; pSub; pSub=pNext){
2666 pNext = pSub->pNext;
2667 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2668 sqlite3DbFree(db, pSub);
2669 }
drh124c0b42011-06-01 18:15:55 +00002670 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002671 vdbeFreeOpArray(db, p->aOp, p->nOp);
dand46def72010-07-24 11:28:28 +00002672 sqlite3DbFree(db, p->aColName);
2673 sqlite3DbFree(db, p->zSql);
2674 sqlite3DbFree(db, p->pFree);
drh678a9aa2011-12-10 15:55:01 +00002675#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
drh25fe97a2013-01-23 18:44:22 +00002676 sqlite3DbFree(db, p->zExplain);
drh678a9aa2011-12-10 15:55:01 +00002677 sqlite3DbFree(db, p->pExplain);
drh7e02e5e2011-12-06 19:44:51 +00002678#endif
dand46def72010-07-24 11:28:28 +00002679}
2680
2681/*
drh9a324642003-09-06 20:12:01 +00002682** Delete an entire VDBE.
2683*/
danielk19774adee202004-05-08 08:23:19 +00002684void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002685 sqlite3 *db;
2686
drhfa3be902009-07-07 02:44:07 +00002687 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002688 db = p->db;
drh4245c402012-06-02 14:32:21 +00002689 assert( sqlite3_mutex_held(db->mutex) );
drhcb103b92012-10-26 00:11:23 +00002690 sqlite3VdbeClearObject(db, p);
drh9a324642003-09-06 20:12:01 +00002691 if( p->pPrev ){
2692 p->pPrev->pNext = p->pNext;
2693 }else{
drh633e6d52008-07-28 19:34:53 +00002694 assert( db->pVdbe==p );
2695 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002696 }
2697 if( p->pNext ){
2698 p->pNext->pPrev = p->pPrev;
2699 }
drh9a324642003-09-06 20:12:01 +00002700 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002701 p->db = 0;
drhcb103b92012-10-26 00:11:23 +00002702 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002703}
drha11846b2004-01-07 18:52:56 +00002704
2705/*
drh9a65f2c2009-06-22 19:05:40 +00002706** Make sure the cursor p is ready to read or write the row to which it
2707** was last positioned. Return an error code if an OOM fault or I/O error
2708** prevents us from positioning the cursor to its correct position.
2709**
drha11846b2004-01-07 18:52:56 +00002710** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002711** MoveTo now. If no move is pending, check to see if the row has been
2712** deleted out from under the cursor and if it has, mark the row as
2713** a NULL row.
2714**
2715** If the cursor is already pointing to the correct row and that row has
2716** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002717*/
drhdfe88ec2008-11-03 20:55:06 +00002718int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002719 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002720 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002721#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002722 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002723#endif
drhf0863fe2005-06-12 21:35:51 +00002724 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002725 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002726 if( rc ) return rc;
drhaa736092009-06-22 00:55:30 +00002727 p->lastRowid = p->movetoTarget;
drhbe0b2372010-07-30 18:40:55 +00002728 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2729 p->rowidIsValid = 1;
drh10cfdd52006-08-08 15:42:59 +00002730#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002731 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002732#endif
drha11846b2004-01-07 18:52:56 +00002733 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002734 p->cacheStatus = CACHE_STALE;
drh399af1d2013-11-20 17:25:55 +00002735 }else if( p->pCursor ){
drha3460582008-07-11 21:02:53 +00002736 int hasMoved;
2737 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2738 if( rc ) return rc;
2739 if( hasMoved ){
2740 p->cacheStatus = CACHE_STALE;
2741 p->nullRow = 1;
2742 }
drha11846b2004-01-07 18:52:56 +00002743 }
2744 return SQLITE_OK;
2745}
danielk19774adee202004-05-08 08:23:19 +00002746
drhab9f7f12004-05-08 10:56:11 +00002747/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002748** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002749**
danielk1977cfcdaef2004-05-12 07:33:33 +00002750** sqlite3VdbeSerialType()
2751** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002752** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002753** sqlite3VdbeSerialPut()
2754** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002755**
2756** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002757** data and index records. Each serialized value consists of a
2758** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2759** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002760**
danielk1977cfcdaef2004-05-12 07:33:33 +00002761** In an SQLite index record, the serial type is stored directly before
2762** the blob of data that it corresponds to. In a table record, all serial
2763** types are stored at the start of the record, and the blobs of data at
2764** the end. Hence these functions allow the caller to handle the
mistachkin48864df2013-03-21 21:20:32 +00002765** serial-type and data blob separately.
danielk1977cfcdaef2004-05-12 07:33:33 +00002766**
2767** The following table describes the various storage classes for data:
2768**
2769** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002770** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002771** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002772** 1 1 signed integer
2773** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002774** 3 3 signed integer
2775** 4 4 signed integer
2776** 5 6 signed integer
2777** 6 8 signed integer
2778** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002779** 8 0 Integer constant 0
2780** 9 0 Integer constant 1
2781** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002782** N>=12 and even (N-12)/2 BLOB
2783** N>=13 and odd (N-13)/2 text
2784**
drh35a59652006-01-02 18:24:40 +00002785** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2786** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002787*/
2788
2789/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002790** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002791*/
drhd946db02005-12-29 19:23:06 +00002792u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002793 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002794 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002795
2796 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002797 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002798 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002799 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002800 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002801# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002802 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002803 u64 u;
drhcfd654b2011-03-05 13:54:15 +00002804 if( i<0 ){
2805 if( i<(-MAX_6BYTE) ) return 6;
2806 /* Previous test prevents: u = -(-9223372036854775808) */
2807 u = -i;
2808 }else{
2809 u = i;
2810 }
drh56690b32012-09-17 15:36:31 +00002811 if( u<=127 ){
2812 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2813 }
drh5742b632005-01-26 17:47:02 +00002814 if( u<=32767 ) return 2;
2815 if( u<=8388607 ) return 3;
2816 if( u<=2147483647 ) return 4;
2817 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002818 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002819 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002820 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002821 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002822 }
danielk1977e4359752008-11-03 09:39:45 +00002823 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002824 n = pMem->n;
2825 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002826 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002827 }
drhfdf972a2007-05-02 13:30:27 +00002828 assert( n>=0 );
2829 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002830}
2831
2832/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002833** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002834*/
drh35cd6432009-06-05 14:17:21 +00002835u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002836 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002837 return (serial_type-12)/2;
2838 }else{
drh57196282004-10-06 15:41:16 +00002839 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002840 return aSize[serial_type];
2841 }
danielk1977192ac1d2004-05-10 07:17:30 +00002842}
2843
2844/*
drh110daac2007-05-04 11:59:31 +00002845** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002846** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002847** upper 4 bytes. Return the result.
2848**
drh7a4f5022007-05-23 07:20:08 +00002849** For most architectures, this is a no-op.
2850**
2851** (later): It is reported to me that the mixed-endian problem
2852** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2853** that early versions of GCC stored the two words of a 64-bit
2854** float in the wrong order. And that error has been propagated
2855** ever since. The blame is not necessarily with GCC, though.
2856** GCC might have just copying the problem from a prior compiler.
2857** I am also told that newer versions of GCC that follow a different
2858** ABI get the byte order right.
2859**
2860** Developers using SQLite on an ARM7 should compile and run their
2861** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2862** enabled, some asserts below will ensure that the byte order of
2863** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002864**
2865** (2007-08-30) Frank van Vugt has studied this problem closely
2866** and has send his findings to the SQLite developers. Frank
2867** writes that some Linux kernels offer floating point hardware
2868** emulation that uses only 32-bit mantissas instead of a full
2869** 48-bits as required by the IEEE standard. (This is the
2870** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2871** byte swapping becomes very complicated. To avoid problems,
2872** the necessary byte swapping is carried out using a 64-bit integer
2873** rather than a 64-bit float. Frank assures us that the code here
2874** works for him. We, the developers, have no way to independently
2875** verify this, but Frank seems to know what he is talking about
2876** so we trust him.
drh110daac2007-05-04 11:59:31 +00002877*/
2878#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002879static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002880 union {
drh60d09a72007-08-30 15:05:08 +00002881 u64 r;
drh110daac2007-05-04 11:59:31 +00002882 u32 i[2];
2883 } u;
2884 u32 t;
2885
2886 u.r = in;
2887 t = u.i[0];
2888 u.i[0] = u.i[1];
2889 u.i[1] = t;
2890 return u.r;
2891}
2892# define swapMixedEndianFloat(X) X = floatSwap(X)
2893#else
2894# define swapMixedEndianFloat(X)
2895#endif
2896
2897/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002898** Write the serialized data blob for the value stored in pMem into
2899** buf. It is assumed that the caller has allocated sufficient space.
2900** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002901**
drh038b7bc2013-12-09 23:17:22 +00002902** nBuf is the amount of space left in buf[]. The caller is responsible
2903** for allocating enough space to buf[] to hold the entire field, exclusive
2904** of the pMem->u.nZero bytes for a MEM_Zero value.
drhfdf972a2007-05-02 13:30:27 +00002905**
2906** Return the number of bytes actually written into buf[]. The number
2907** of bytes in the zero-filled tail is included in the return value only
2908** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002909*/
drha9ab4812013-12-11 11:00:44 +00002910u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
drh35cd6432009-06-05 14:17:21 +00002911 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002912
drh1483e142004-05-21 21:12:42 +00002913 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002914 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002915 u64 v;
drh35cd6432009-06-05 14:17:21 +00002916 u32 i;
drha19b7752004-05-30 21:14:58 +00002917 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002918 assert( sizeof(v)==sizeof(pMem->r) );
2919 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002920 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002921 }else{
drh3c024d62007-03-30 11:23:45 +00002922 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002923 }
drh1483e142004-05-21 21:12:42 +00002924 len = i = sqlite3VdbeSerialTypeLen(serial_type);
2925 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002926 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002927 v >>= 8;
2928 }
2929 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002930 }
drhd946db02005-12-29 19:23:06 +00002931
danielk1977cfcdaef2004-05-12 07:33:33 +00002932 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002933 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002934 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002935 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002936 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002937 memcpy(buf, pMem->z, len);
2938 return len;
2939 }
2940
2941 /* NULL or constants 0 or 1 */
2942 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002943}
2944
drhf926d1e2014-03-04 04:04:33 +00002945/* Input "x" is a sequence of unsigned characters that represent a
2946** big-endian integer. Return the equivalent native integer
2947*/
2948#define ONE_BYTE_INT(x) ((i8)(x)[0])
2949#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
2950#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
2951#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
2952
danielk1977cfcdaef2004-05-12 07:33:33 +00002953/*
2954** Deserialize the data blob pointed to by buf as serial type serial_type
2955** and store the result in pMem. Return the number of bytes read.
2956*/
drh35cd6432009-06-05 14:17:21 +00002957u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002958 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002959 u32 serial_type, /* Serial type to deserialize */
2960 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002961){
drh693e6712014-01-24 22:58:00 +00002962 u64 x;
2963 u32 y;
drh3c685822005-05-21 18:32:18 +00002964 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002965 case 10: /* Reserved for future use */
2966 case 11: /* Reserved for future use */
2967 case 0: { /* NULL */
2968 pMem->flags = MEM_Null;
2969 break;
2970 }
2971 case 1: { /* 1-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002972 pMem->u.i = ONE_BYTE_INT(buf);
drh1483e142004-05-21 21:12:42 +00002973 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002974 return 1;
drh1483e142004-05-21 21:12:42 +00002975 }
drh3c685822005-05-21 18:32:18 +00002976 case 2: { /* 2-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002977 pMem->u.i = TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002978 pMem->flags = MEM_Int;
2979 return 2;
2980 }
2981 case 3: { /* 3-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002982 pMem->u.i = THREE_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002983 pMem->flags = MEM_Int;
2984 return 3;
2985 }
2986 case 4: { /* 4-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002987 y = FOUR_BYTE_UINT(buf);
drh693e6712014-01-24 22:58:00 +00002988 pMem->u.i = (i64)*(int*)&y;
drh3c685822005-05-21 18:32:18 +00002989 pMem->flags = MEM_Int;
2990 return 4;
2991 }
2992 case 5: { /* 6-byte signed integer */
drhf926d1e2014-03-04 04:04:33 +00002993 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00002994 pMem->flags = MEM_Int;
2995 return 6;
2996 }
drh91124b32005-08-18 18:15:05 +00002997 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002998 case 7: { /* IEEE floating point */
drh2a3e4a72006-01-23 21:44:53 +00002999#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00003000 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00003001 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3002 ** defined that 64-bit floating point values really are mixed
3003 ** endian.
drhbfd6b032005-08-28 01:38:44 +00003004 */
drhde941c62005-08-28 01:34:21 +00003005 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00003006 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00003007 u64 t2 = t1;
3008 swapMixedEndianFloat(t2);
3009 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00003010#endif
drhf926d1e2014-03-04 04:04:33 +00003011 x = FOUR_BYTE_UINT(buf);
3012 y = FOUR_BYTE_UINT(buf+4);
drh3c685822005-05-21 18:32:18 +00003013 x = (x<<32) | y;
3014 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00003015 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00003016 pMem->flags = MEM_Int;
3017 }else{
drh4f0c5872007-03-26 22:05:01 +00003018 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00003019 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00003020 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00003021 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00003022 }
3023 return 8;
3024 }
drhd946db02005-12-29 19:23:06 +00003025 case 8: /* Integer 0 */
3026 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00003027 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00003028 pMem->flags = MEM_Int;
3029 return 0;
3030 }
drh3c685822005-05-21 18:32:18 +00003031 default: {
drhc138daf2013-11-19 13:55:34 +00003032 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
drh35cd6432009-06-05 14:17:21 +00003033 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00003034 pMem->z = (char *)buf;
3035 pMem->n = len;
3036 pMem->xDel = 0;
drhc138daf2013-11-19 13:55:34 +00003037 pMem->flags = aFlag[serial_type&1];
drh3c685822005-05-21 18:32:18 +00003038 return len;
drh696b32f2004-05-30 01:51:52 +00003039 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003040 }
drh3c685822005-05-21 18:32:18 +00003041 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00003042}
3043
drh1e968a02008-03-25 00:22:21 +00003044/*
dan03e9cfc2011-09-05 14:20:27 +00003045** This routine is used to allocate sufficient space for an UnpackedRecord
3046** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3047** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00003048**
dan03e9cfc2011-09-05 14:20:27 +00003049** The space is either allocated using sqlite3DbMallocRaw() or from within
3050** the unaligned buffer passed via the second and third arguments (presumably
3051** stack space). If the former, then *ppFree is set to a pointer that should
3052** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3053** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3054** before returning.
drh1e968a02008-03-25 00:22:21 +00003055**
dan03e9cfc2011-09-05 14:20:27 +00003056** If an OOM error occurs, NULL is returned.
3057*/
3058UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3059 KeyInfo *pKeyInfo, /* Description of the record */
3060 char *pSpace, /* Unaligned space available */
3061 int szSpace, /* Size of pSpace[] in bytes */
3062 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00003063){
dan03e9cfc2011-09-05 14:20:27 +00003064 UnpackedRecord *p; /* Unpacked record to return */
3065 int nOff; /* Increment pSpace by nOff to align it */
3066 int nByte; /* Number of bytes required for *p */
3067
3068 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00003069 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3070 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3071 */
3072 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00003073 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00003074 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00003075 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3076 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00003077 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00003078 }else{
dan42acb3e2011-09-05 20:16:38 +00003079 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00003080 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00003081 }
dan42acb3e2011-09-05 20:16:38 +00003082
3083 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drhe1a022e2012-09-17 17:16:53 +00003084 assert( pKeyInfo->aSortOrder!=0 );
drh1e968a02008-03-25 00:22:21 +00003085 p->pKeyInfo = pKeyInfo;
3086 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00003087 return p;
3088}
3089
3090/*
3091** Given the nKey-byte encoding of a record in pKey[], populate the
3092** UnpackedRecord structure indicated by the fourth argument with the
3093** contents of the decoded record.
3094*/
3095void sqlite3VdbeRecordUnpack(
3096 KeyInfo *pKeyInfo, /* Information about the record format */
3097 int nKey, /* Size of the binary record */
3098 const void *pKey, /* The binary record */
3099 UnpackedRecord *p /* Populate this structure before returning. */
3100){
3101 const unsigned char *aKey = (const unsigned char *)pKey;
3102 int d;
3103 u32 idx; /* Offset in aKey[] to read from */
3104 u16 u; /* Unsigned loop counter */
3105 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00003106 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00003107
dan1fed5da2014-02-25 21:01:25 +00003108 p->default_rc = 0;
drh8c5d1522009-04-10 00:56:28 +00003109 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00003110 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00003111 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00003112 u = 0;
drh2fa34d32009-07-15 16:30:50 +00003113 while( idx<szHdr && u<p->nField && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00003114 u32 serial_type;
3115
danielk197700e13612008-11-17 19:18:54 +00003116 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00003117 pMem->enc = pKeyInfo->enc;
3118 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00003119 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
danielk19775f096132008-03-28 15:44:09 +00003120 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00003121 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00003122 pMem++;
shane0b8d2762008-07-22 05:18:00 +00003123 u++;
drh1e968a02008-03-25 00:22:21 +00003124 }
drh7d10d5a2008-08-20 16:35:10 +00003125 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00003126 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00003127}
3128
dan3833e932014-03-01 19:44:56 +00003129#if SQLITE_DEBUG
dan3b9330f2014-02-27 20:44:18 +00003130/*
dan3833e932014-03-01 19:44:56 +00003131** This function compares two index or table record keys in the same way
3132** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3133** this function deserializes and compares values using the
3134** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3135** in assert() statements to ensure that the optimized code in
3136** sqlite3VdbeRecordCompare() returns results with these two primitives.
dan3b9330f2014-02-27 20:44:18 +00003137*/
dan3833e932014-03-01 19:44:56 +00003138static int vdbeRecordCompareDebug(
dan1fed5da2014-02-25 21:01:25 +00003139 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003140 const UnpackedRecord *pPKey2 /* Right key */
dan1fed5da2014-02-25 21:01:25 +00003141){
dan3b9330f2014-02-27 20:44:18 +00003142 u32 d1; /* Offset into aKey[] of next data element */
3143 u32 idx1; /* Offset into aKey[] of next header element */
3144 u32 szHdr1; /* Number of bytes in header */
3145 int i = 0;
3146 int rc = 0;
3147 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3148 KeyInfo *pKeyInfo;
3149 Mem mem1;
dan1fed5da2014-02-25 21:01:25 +00003150
dan3b9330f2014-02-27 20:44:18 +00003151 pKeyInfo = pPKey2->pKeyInfo;
3152 mem1.enc = pKeyInfo->enc;
3153 mem1.db = pKeyInfo->db;
3154 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
3155 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003156
dan3b9330f2014-02-27 20:44:18 +00003157 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3158 ** We could initialize it, as shown here, to silence those complaints.
3159 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3160 ** the unnecessary initialization has a measurable negative performance
3161 ** impact, since this routine is a very high runner. And so, we choose
3162 ** to ignore the compiler warnings and leave this variable uninitialized.
3163 */
3164 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
3165
3166 idx1 = getVarint32(aKey1, szHdr1);
3167 d1 = szHdr1;
3168 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3169 assert( pKeyInfo->aSortOrder!=0 );
3170 assert( pKeyInfo->nField>0 );
3171 assert( idx1<=szHdr1 || CORRUPT_DB );
3172 do{
3173 u32 serial_type1;
dan1fed5da2014-02-25 21:01:25 +00003174
dan3b9330f2014-02-27 20:44:18 +00003175 /* Read the serial types for the next element in each key. */
3176 idx1 += getVarint32( aKey1+idx1, serial_type1 );
dan1fed5da2014-02-25 21:01:25 +00003177
dan3b9330f2014-02-27 20:44:18 +00003178 /* Verify that there is enough key space remaining to avoid
3179 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3180 ** always be greater than or equal to the amount of required key space.
3181 ** Use that approximation to avoid the more expensive call to
3182 ** sqlite3VdbeSerialTypeLen() in the common case.
3183 */
3184 if( d1+serial_type1+2>(u32)nKey1
3185 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3186 ){
3187 break;
dan1fed5da2014-02-25 21:01:25 +00003188 }
dan1fed5da2014-02-25 21:01:25 +00003189
dan3b9330f2014-02-27 20:44:18 +00003190 /* Extract the values to be compared.
3191 */
3192 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
dan1fed5da2014-02-25 21:01:25 +00003193
dan3b9330f2014-02-27 20:44:18 +00003194 /* Do the comparison
3195 */
3196 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3197 if( rc!=0 ){
3198 assert( mem1.zMalloc==0 ); /* See comment below */
3199 if( pKeyInfo->aSortOrder[i] ){
3200 rc = -rc; /* Invert the result for DESC sort order. */
dan1fed5da2014-02-25 21:01:25 +00003201 }
dan3b9330f2014-02-27 20:44:18 +00003202 return rc;
dan1fed5da2014-02-25 21:01:25 +00003203 }
dan3b9330f2014-02-27 20:44:18 +00003204 i++;
3205 }while( idx1<szHdr1 && i<pPKey2->nField );
dan1fed5da2014-02-25 21:01:25 +00003206
dan3b9330f2014-02-27 20:44:18 +00003207 /* No memory allocation is ever used on mem1. Prove this using
3208 ** the following assert(). If the assert() fails, it indicates a
3209 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3210 */
3211 assert( mem1.zMalloc==0 );
3212
3213 /* rc==0 here means that one of the keys ran out of fields and
3214 ** all the fields up to that point were equal. Return the the default_rc
3215 ** value. */
3216 return pPKey2->default_rc;
dan1fed5da2014-02-25 21:01:25 +00003217}
dan3833e932014-03-01 19:44:56 +00003218#endif
dan1fed5da2014-02-25 21:01:25 +00003219
dan3833e932014-03-01 19:44:56 +00003220/*
3221** Both *pMem1 and *pMem2 contain string values. Compare the two values
3222** using the collation sequence pColl. As usual, return a negative , zero
3223** or positive value if *pMem1 is less than, equal to or greater than
3224** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3225*/
dan1fed5da2014-02-25 21:01:25 +00003226static int vdbeCompareMemString(
dan3833e932014-03-01 19:44:56 +00003227 const Mem *pMem1,
3228 const Mem *pMem2,
dan1fed5da2014-02-25 21:01:25 +00003229 const CollSeq *pColl
3230){
3231 if( pMem1->enc==pColl->enc ){
3232 /* The strings are already in the correct encoding. Call the
3233 ** comparison function directly */
3234 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3235 }else{
3236 int rc;
3237 const void *v1, *v2;
3238 int n1, n2;
3239 Mem c1;
3240 Mem c2;
3241 memset(&c1, 0, sizeof(c1));
3242 memset(&c2, 0, sizeof(c2));
3243 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3244 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3245 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3246 n1 = v1==0 ? 0 : c1.n;
3247 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3248 n2 = v2==0 ? 0 : c2.n;
3249 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3250 sqlite3VdbeMemRelease(&c1);
3251 sqlite3VdbeMemRelease(&c2);
3252 return rc;
3253 }
3254}
3255
3256/*
3257** Compare the values contained by the two memory cells, returning
3258** negative, zero or positive if pMem1 is less than, equal to, or greater
3259** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3260** and reals) sorted numerically, followed by text ordered by the collating
3261** sequence pColl and finally blob's ordered by memcmp().
3262**
3263** Two NULL values are considered equal by this function.
3264*/
3265int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3266 int rc;
3267 int f1, f2;
3268 int combined_flags;
3269
3270 f1 = pMem1->flags;
3271 f2 = pMem2->flags;
3272 combined_flags = f1|f2;
3273 assert( (combined_flags & MEM_RowSet)==0 );
3274
3275 /* If one value is NULL, it is less than the other. If both values
3276 ** are NULL, return 0.
3277 */
3278 if( combined_flags&MEM_Null ){
3279 return (f2&MEM_Null) - (f1&MEM_Null);
3280 }
3281
3282 /* If one value is a number and the other is not, the number is less.
3283 ** If both are numbers, compare as reals if one is a real, or as integers
3284 ** if both values are integers.
3285 */
3286 if( combined_flags&(MEM_Int|MEM_Real) ){
3287 double r1, r2;
3288 if( (f1 & f2 & MEM_Int)!=0 ){
3289 if( pMem1->u.i < pMem2->u.i ) return -1;
3290 if( pMem1->u.i > pMem2->u.i ) return 1;
3291 return 0;
3292 }
3293 if( (f1&MEM_Real)!=0 ){
3294 r1 = pMem1->r;
3295 }else if( (f1&MEM_Int)!=0 ){
3296 r1 = (double)pMem1->u.i;
3297 }else{
3298 return 1;
3299 }
3300 if( (f2&MEM_Real)!=0 ){
3301 r2 = pMem2->r;
3302 }else if( (f2&MEM_Int)!=0 ){
3303 r2 = (double)pMem2->u.i;
3304 }else{
3305 return -1;
3306 }
3307 if( r1<r2 ) return -1;
3308 if( r1>r2 ) return 1;
3309 return 0;
3310 }
3311
3312 /* If one value is a string and the other is a blob, the string is less.
3313 ** If both are strings, compare using the collating functions.
3314 */
3315 if( combined_flags&MEM_Str ){
3316 if( (f1 & MEM_Str)==0 ){
3317 return 1;
3318 }
3319 if( (f2 & MEM_Str)==0 ){
3320 return -1;
3321 }
3322
3323 assert( pMem1->enc==pMem2->enc );
3324 assert( pMem1->enc==SQLITE_UTF8 ||
3325 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3326
3327 /* The collation sequence must be defined at this point, even if
3328 ** the user deletes the collation sequence after the vdbe program is
3329 ** compiled (this was not always the case).
3330 */
3331 assert( !pColl || pColl->xCmp );
3332
3333 if( pColl ){
3334 return vdbeCompareMemString(pMem1, pMem2, pColl);
3335 }
3336 /* If a NULL pointer was passed as the collate function, fall through
3337 ** to the blob case and use memcmp(). */
3338 }
3339
3340 /* Both values must be blobs. Compare using memcmp(). */
3341 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
3342 if( rc==0 ){
3343 rc = pMem1->n - pMem2->n;
3344 }
3345 return rc;
3346}
3347
3348
dan3833e932014-03-01 19:44:56 +00003349/*
3350** The first argument passed to this function is a serial-type that
3351** corresponds to an integer - all values between 1 and 9 inclusive
3352** except 7. The second points to a buffer containing an integer value
3353** serialized according to serial_type. This function deserializes
3354** and returns the value.
3355*/
dan3b9330f2014-02-27 20:44:18 +00003356static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
drhf926d1e2014-03-04 04:04:33 +00003357 u32 y;
dan3833e932014-03-01 19:44:56 +00003358 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
dan3b9330f2014-02-27 20:44:18 +00003359 switch( serial_type ){
dan3833e932014-03-01 19:44:56 +00003360 case 0:
dan3b9330f2014-02-27 20:44:18 +00003361 case 1:
drhf926d1e2014-03-04 04:04:33 +00003362 return ONE_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003363 case 2:
drhf926d1e2014-03-04 04:04:33 +00003364 return TWO_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003365 case 3:
drhf926d1e2014-03-04 04:04:33 +00003366 return THREE_BYTE_INT(aKey);
3367 case 4: {
3368 y = FOUR_BYTE_UINT(aKey);
3369 return (i64)*(int*)&y;
3370 }
dan3b9330f2014-02-27 20:44:18 +00003371 case 5: {
drhf926d1e2014-03-04 04:04:33 +00003372 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhaf5b2af2013-08-05 15:32:09 +00003373 }
dan3b9330f2014-02-27 20:44:18 +00003374 case 6: {
drhf926d1e2014-03-04 04:04:33 +00003375 u64 x = FOUR_BYTE_UINT(aKey);
3376 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3377 return (i64)*(i64*)&x;
drh1e968a02008-03-25 00:22:21 +00003378 }
dan3b9330f2014-02-27 20:44:18 +00003379 }
drh407414c2009-07-14 14:15:27 +00003380
dan3b9330f2014-02-27 20:44:18 +00003381 return (serial_type - 8);
drh1e968a02008-03-25 00:22:21 +00003382}
danielk1977eb015e02004-05-18 01:31:14 +00003383
dan3833e932014-03-01 19:44:56 +00003384/*
3385** This function compares the two table rows or index records
3386** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
3387** or positive integer if key1 is less than, equal to or
3388** greater than key2. The {nKey1, pKey1} key must be a blob
3389** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
3390** key must be a parsed key such as obtained from
3391** sqlite3VdbeParseRecord.
3392**
3393** If argument bSkip is non-zero, it is assumed that the caller has already
3394** determined that the first fields of the keys are equal.
3395**
3396** Key1 and Key2 do not have to contain the same number of fields. If all
3397** fields that appear in both keys are equal, then pPKey2->default_rc is
3398** returned.
3399*/
3400int sqlite3VdbeRecordCompare(
3401 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003402 const UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003403 int bSkip /* If true, skip the first field */
dan1fed5da2014-02-25 21:01:25 +00003404){
dan3833e932014-03-01 19:44:56 +00003405 u32 d1; /* Offset into aKey[] of next data element */
3406 int i; /* Index of next field to compare */
3407 int szHdr1; /* Size of record header in bytes */
3408 u32 idx1; /* Offset of first type in header */
3409 int rc = 0; /* Return value */
3410 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
dan1fed5da2014-02-25 21:01:25 +00003411 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3412 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3413 Mem mem1;
3414
dan3833e932014-03-01 19:44:56 +00003415 /* If bSkip is true, then the caller has already determined that the first
3416 ** two elements in the keys are equal. Fix the various stack variables so
dan3b9330f2014-02-27 20:44:18 +00003417 ** that this routine begins comparing at the second field. */
dan3833e932014-03-01 19:44:56 +00003418 if( bSkip ){
dan3b9330f2014-02-27 20:44:18 +00003419 u32 s1;
dan3b9330f2014-02-27 20:44:18 +00003420 idx1 = 1 + getVarint32(&aKey1[1], s1);
dan3833e932014-03-01 19:44:56 +00003421 szHdr1 = aKey1[0];
3422 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
dan3b9330f2014-02-27 20:44:18 +00003423 i = 1;
3424 pRhs++;
dan3833e932014-03-01 19:44:56 +00003425 }else{
3426 idx1 = getVarint32(aKey1, szHdr1);
3427 d1 = szHdr1;
3428 i = 0;
dan3b9330f2014-02-27 20:44:18 +00003429 }
3430
dan1fed5da2014-02-25 21:01:25 +00003431 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003432 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3433 || CORRUPT_DB );
3434 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3435 assert( pPKey2->pKeyInfo->nField>0 );
3436 assert( idx1<=szHdr1 || CORRUPT_DB );
3437 do{
dan1fed5da2014-02-25 21:01:25 +00003438 u32 serial_type;
3439
3440 /* RHS is an integer */
3441 if( pRhs->flags & MEM_Int ){
3442 serial_type = aKey1[idx1];
3443 if( serial_type>=12 ){
3444 rc = +1;
3445 }else if( serial_type==0 ){
3446 rc = -1;
dan3b9330f2014-02-27 20:44:18 +00003447 }else if( serial_type==7 ){
3448 double rhs = (double)pRhs->u.i;
dan1fed5da2014-02-25 21:01:25 +00003449 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
dan3b9330f2014-02-27 20:44:18 +00003450 if( mem1.r<rhs ){
3451 rc = -1;
3452 }else if( mem1.r>rhs ){
3453 rc = +1;
3454 }
3455 }else{
3456 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3457 i64 rhs = pRhs->u.i;
3458 if( lhs<rhs ){
3459 rc = -1;
3460 }else if( lhs>rhs ){
3461 rc = +1;
dan1fed5da2014-02-25 21:01:25 +00003462 }
3463 }
3464 }
3465
3466 /* RHS is real */
3467 else if( pRhs->flags & MEM_Real ){
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 double rhs = pRhs->r;
3475 double lhs;
3476 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3477 if( serial_type==7 ){
3478 lhs = mem1.r;
3479 }else{
drh295aedf2014-03-03 18:25:24 +00003480 lhs = (double)mem1.u.i;
dan1fed5da2014-02-25 21:01:25 +00003481 }
3482 if( lhs<rhs ){
3483 rc = -1;
3484 }else if( lhs>rhs ){
3485 rc = +1;
3486 }
3487 }
3488 }
3489
3490 /* RHS is a string */
3491 else if( pRhs->flags & MEM_Str ){
3492 getVarint32(&aKey1[idx1], serial_type);
3493 if( serial_type<12 ){
3494 rc = -1;
3495 }else if( !(serial_type & 0x01) ){
3496 rc = +1;
3497 }else{
3498 mem1.n = (serial_type - 12) / 2;
drh295aedf2014-03-03 18:25:24 +00003499 if( (d1+mem1.n) > (unsigned)nKey1 ){
dan1fed5da2014-02-25 21:01:25 +00003500 rc = 1; /* Corruption */
3501 }else if( pKeyInfo->aColl[i] ){
3502 mem1.enc = pKeyInfo->enc;
3503 mem1.db = pKeyInfo->db;
3504 mem1.flags = MEM_Str;
drhfcb44a82014-03-03 15:13:27 +00003505 mem1.z = (char*)&aKey1[d1];
dan1fed5da2014-02-25 21:01:25 +00003506 rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
3507 }else{
3508 int nCmp = MIN(mem1.n, pRhs->n);
3509 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3510 if( rc==0 ) rc = mem1.n - pRhs->n;
3511 }
3512 }
3513 }
3514
3515 /* RHS is a blob */
3516 else if( pRhs->flags & MEM_Blob ){
3517 getVarint32(&aKey1[idx1], serial_type);
3518 if( serial_type<12 || (serial_type & 0x01) ){
3519 rc = -1;
3520 }else{
3521 int nStr = (serial_type - 12) / 2;
drh295aedf2014-03-03 18:25:24 +00003522 if( (d1+nStr) > (unsigned)nKey1 ){
dan1fed5da2014-02-25 21:01:25 +00003523 rc = 1; /* Corruption */
3524 }else{
3525 int nCmp = MIN(nStr, pRhs->n);
3526 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3527 if( rc==0 ) rc = nStr - pRhs->n;
3528 }
3529 }
3530 }
3531
3532 /* RHS is null */
3533 else{
3534 serial_type = aKey1[idx1];
3535 rc = (serial_type!=0);
3536 }
3537
3538 if( rc!=0 ){
dan1fed5da2014-02-25 21:01:25 +00003539 if( pKeyInfo->aSortOrder[i] ){
3540 rc = -rc;
dan1fed5da2014-02-25 21:01:25 +00003541 }
dan3833e932014-03-01 19:44:56 +00003542 assert( CORRUPT_DB
3543 || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3544 || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
3545 );
3546 assert( mem1.zMalloc==0 ); /* See comment below */
dan1fed5da2014-02-25 21:01:25 +00003547 return rc;
3548 }
3549
3550 i++;
dan3b9330f2014-02-27 20:44:18 +00003551 pRhs++;
dan1fed5da2014-02-25 21:01:25 +00003552 d1 += sqlite3VdbeSerialTypeLen(serial_type);
3553 idx1 += sqlite3VarintLen(serial_type);
drh295aedf2014-03-03 18:25:24 +00003554 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
dan1fed5da2014-02-25 21:01:25 +00003555
3556 /* No memory allocation is ever used on mem1. Prove this using
3557 ** the following assert(). If the assert() fails, it indicates a
dan3833e932014-03-01 19:44:56 +00003558 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
dan1fed5da2014-02-25 21:01:25 +00003559 assert( mem1.zMalloc==0 );
3560
3561 /* rc==0 here means that one or both of the keys ran out of fields and
3562 ** all the fields up to that point were equal. Return the the default_rc
3563 ** value. */
dan3833e932014-03-01 19:44:56 +00003564 assert( CORRUPT_DB
3565 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
3566 );
dan1fed5da2014-02-25 21:01:25 +00003567 return pPKey2->default_rc;
3568}
3569
dan3833e932014-03-01 19:44:56 +00003570/*
3571** This function is an optimized version of sqlite3VdbeRecordCompare()
3572** that (a) the first field of pPKey2 is an integer, and (b) the
3573** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3574** byte (i.e. is less than 128).
3575*/
dan3b9330f2014-02-27 20:44:18 +00003576static int vdbeRecordCompareInt(
3577 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003578 const UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003579 int bSkip /* Ignored */
dan3b9330f2014-02-27 20:44:18 +00003580){
dan9b8afef2014-03-03 20:48:50 +00003581 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
dan3b9330f2014-02-27 20:44:18 +00003582 int serial_type = ((const u8*)pKey1)[1];
3583 int res;
drhf926d1e2014-03-04 04:04:33 +00003584 u32 y;
3585 u64 x;
dan3b9330f2014-02-27 20:44:18 +00003586 i64 v = pPKey2->aMem[0].u.i;
3587 i64 lhs;
drh295aedf2014-03-03 18:25:24 +00003588 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003589
dan3833e932014-03-01 19:44:56 +00003590 assert( bSkip==0 );
dan3833e932014-03-01 19:44:56 +00003591 switch( serial_type ){
drhf926d1e2014-03-04 04:04:33 +00003592 case 1: { /* 1-byte signed integer */
3593 lhs = ONE_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003594 break;
3595 }
drhf926d1e2014-03-04 04:04:33 +00003596 case 2: { /* 2-byte signed integer */
3597 lhs = TWO_BYTE_INT(aKey);
3598 break;
3599 }
3600 case 3: { /* 3-byte signed integer */
3601 lhs = THREE_BYTE_INT(aKey);
3602 break;
3603 }
3604 case 4: { /* 4-byte signed integer */
3605 y = FOUR_BYTE_UINT(aKey);
3606 lhs = (i64)*(int*)&y;
3607 break;
3608 }
3609 case 5: { /* 6-byte signed integer */
3610 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
3611 break;
3612 }
3613 case 6: { /* 8-byte signed integer */
3614 x = FOUR_BYTE_UINT(aKey);
3615 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3616 lhs = *(i64*)&x;
dan3b9330f2014-02-27 20:44:18 +00003617 break;
3618 }
dan3b9330f2014-02-27 20:44:18 +00003619 case 8:
3620 lhs = 0;
3621 break;
dan3b9330f2014-02-27 20:44:18 +00003622 case 9:
3623 lhs = 1;
3624 break;
3625
dan063d4a02014-02-28 09:48:30 +00003626 /* This case could be removed without changing the results of running
3627 ** this code. Including it causes gcc to generate a faster switch
3628 ** statement (since the range of switch targets now starts at zero and
dan597515d2014-02-28 18:39:51 +00003629 ** is contiguous) but does not cause any duplicate code to be generated
dan063d4a02014-02-28 09:48:30 +00003630 ** (as gcc is clever enough to combine the two like cases). Other
3631 ** compilers might be similar. */
3632 case 0: case 7:
dan3833e932014-03-01 19:44:56 +00003633 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan063d4a02014-02-28 09:48:30 +00003634
dan3b9330f2014-02-27 20:44:18 +00003635 default:
dan3833e932014-03-01 19:44:56 +00003636 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
dan3b9330f2014-02-27 20:44:18 +00003637 }
3638
3639 if( v>lhs ){
3640 res = pPKey2->r1;
3641 }else if( v<lhs ){
3642 res = pPKey2->r2;
3643 }else if( pPKey2->nField>1 ){
dan063d4a02014-02-28 09:48:30 +00003644 /* The first fields of the two keys are equal. Compare the trailing
3645 ** fields. */
dan3833e932014-03-01 19:44:56 +00003646 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003647 }else{
dan063d4a02014-02-28 09:48:30 +00003648 /* The first fields of the two keys are equal and there are no trailing
3649 ** fields. Return pPKey2->default_rc in this case. */
dan3b9330f2014-02-27 20:44:18 +00003650 res = pPKey2->default_rc;
3651 }
3652
dan3833e932014-03-01 19:44:56 +00003653 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3654 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3655 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003656 || CORRUPT_DB
3657 );
3658 return res;
3659}
3660
dan3833e932014-03-01 19:44:56 +00003661/*
3662** This function is an optimized version of sqlite3VdbeRecordCompare()
3663** that (a) the first field of pPKey2 is a string, that (b) the first field
3664** uses the collation sequence BINARY and (c) that the size-of-header varint
3665** at the start of (pKey1/nKey1) fits in a single byte.
3666*/
dan3b9330f2014-02-27 20:44:18 +00003667static int vdbeRecordCompareString(
3668 int nKey1, const void *pKey1, /* Left key */
drh295aedf2014-03-03 18:25:24 +00003669 const UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003670 int bSkip
dan3b9330f2014-02-27 20:44:18 +00003671){
3672 const u8 *aKey1 = (const u8*)pKey1;
3673 int serial_type;
3674 int res;
drh295aedf2014-03-03 18:25:24 +00003675 UNUSED_PARAMETER(bSkip);
dan3b9330f2014-02-27 20:44:18 +00003676
dan3833e932014-03-01 19:44:56 +00003677 assert( bSkip==0 );
dan3b9330f2014-02-27 20:44:18 +00003678 getVarint32(&aKey1[1], serial_type);
3679
3680 if( serial_type<12 ){
3681 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
3682 }else if( !(serial_type & 0x01) ){
3683 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
3684 }else{
3685 int nCmp;
3686 int nStr;
dan3833e932014-03-01 19:44:56 +00003687 int szHdr = aKey1[0];
dan3b9330f2014-02-27 20:44:18 +00003688
3689 nStr = (serial_type-12) / 2;
3690 if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
3691 nCmp = MIN( pPKey2->aMem[0].n, nStr );
dan3833e932014-03-01 19:44:56 +00003692 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
dan3b9330f2014-02-27 20:44:18 +00003693
3694 if( res==0 ){
3695 res = nStr - pPKey2->aMem[0].n;
3696 if( res==0 ){
3697 if( pPKey2->nField>1 ){
dan3833e932014-03-01 19:44:56 +00003698 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003699 }else{
3700 res = pPKey2->default_rc;
3701 }
3702 }else if( res>0 ){
3703 res = pPKey2->r2;
3704 }else{
3705 res = pPKey2->r1;
3706 }
3707 }else if( res>0 ){
3708 res = pPKey2->r2;
3709 }else{
3710 res = pPKey2->r1;
3711 }
3712 }
3713
dan3833e932014-03-01 19:44:56 +00003714 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
3715 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
3716 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
dan3b9330f2014-02-27 20:44:18 +00003717 || CORRUPT_DB
3718 );
3719 return res;
3720}
3721
dan3833e932014-03-01 19:44:56 +00003722/*
3723** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3724** suitable for comparing serialized records to the unpacked record passed
3725** as the only argument.
3726*/
dan1fed5da2014-02-25 21:01:25 +00003727RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
dan9b8afef2014-03-03 20:48:50 +00003728 /* varintRecordCompareInt() and varintRecordCompareString() both assume
3729 ** that the size-of-header varint that occurs at the start of each record
3730 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
3731 ** also assumes that it is safe to overread a buffer by at least the
3732 ** maximum possible legal header size plus 8 bytes. Because there is
3733 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
3734 ** buffer passed to varintRecordCompareInt() this makes it convenient to
3735 ** limit the size of the header to 64 bytes in cases where the first field
3736 ** is an integer.
3737 **
3738 ** The easiest way to enforce this limit is to consider only records with
3739 ** 13 fields or less. If the first field is an integer, the maximum legal
3740 ** header size is (12*5 + 1 + 1) bytes. */
3741 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
dan1fed5da2014-02-25 21:01:25 +00003742 int flags = p->aMem[0].flags;
dan3b9330f2014-02-27 20:44:18 +00003743 if( p->pKeyInfo->aSortOrder[0] ){
3744 p->r1 = 1;
3745 p->r2 = -1;
3746 }else{
3747 p->r1 = -1;
3748 p->r2 = 1;
3749 }
dan1fed5da2014-02-25 21:01:25 +00003750 if( (flags & MEM_Int) ){
3751 return vdbeRecordCompareInt;
dan3b9330f2014-02-27 20:44:18 +00003752 }
3753 if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
3754 && p->pKeyInfo->aColl[0]==0
dan1fed5da2014-02-25 21:01:25 +00003755 ){
3756 return vdbeRecordCompareString;
3757 }
3758 }
dan3b9330f2014-02-27 20:44:18 +00003759
dan3833e932014-03-01 19:44:56 +00003760 return sqlite3VdbeRecordCompare;
dan3b9330f2014-02-27 20:44:18 +00003761}
dan1fed5da2014-02-25 21:01:25 +00003762
danielk1977eb015e02004-05-18 01:31:14 +00003763/*
drh7a224de2004-06-02 01:22:02 +00003764** pCur points at an index entry created using the OP_MakeRecord opcode.
3765** Read the rowid (the last field in the record) and store it in *rowid.
3766** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00003767**
3768** pCur might be pointing to text obtained from a corrupt database file.
3769** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00003770*/
drh35f6b932009-06-23 14:15:04 +00003771int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00003772 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003773 int rc;
drhd5788202004-05-28 08:21:05 +00003774 u32 szHdr; /* Size of the header */
3775 u32 typeRowid; /* Serial type of the rowid */
3776 u32 lenRowid; /* Size of the rowid */
3777 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00003778
shanecea72b22009-09-07 04:38:36 +00003779 UNUSED_PARAMETER(db);
3780
drh88a003e2008-12-11 16:17:03 +00003781 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00003782 ** than 2GiB are support - anything large must be database corruption.
3783 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00003784 ** this code can safely assume that nCellKey is 32-bits
3785 */
drhea8ffdf2009-07-22 00:35:23 +00003786 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003787 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003788 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00003789 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00003790
3791 /* Read in the complete content of the index entry */
drhff104c12009-08-25 13:10:27 +00003792 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003793 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00003794 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00003795 return rc;
3796 }
drh88a003e2008-12-11 16:17:03 +00003797
3798 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00003799 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00003800 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00003801 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00003802 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00003803 goto idx_rowid_corruption;
3804 }
3805
3806 /* The last field of the index should be an integer - the ROWID.
3807 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00003808 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00003809 testcase( typeRowid==1 );
3810 testcase( typeRowid==2 );
3811 testcase( typeRowid==3 );
3812 testcase( typeRowid==4 );
3813 testcase( typeRowid==5 );
3814 testcase( typeRowid==6 );
3815 testcase( typeRowid==8 );
3816 testcase( typeRowid==9 );
3817 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3818 goto idx_rowid_corruption;
3819 }
drhd5788202004-05-28 08:21:05 +00003820 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drheeb844a2009-08-08 18:01:07 +00003821 testcase( (u32)m.n==szHdr+lenRowid );
3822 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00003823 goto idx_rowid_corruption;
3824 }
3825
3826 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00003827 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00003828 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00003829 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003830 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00003831
3832 /* Jump here if database corruption is detected after m has been
3833 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3834idx_rowid_corruption:
3835 testcase( m.zMalloc!=0 );
3836 sqlite3VdbeMemRelease(&m);
3837 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003838}
3839
drh7cf6e4d2004-05-19 14:56:55 +00003840/*
drh5f82e3c2009-07-06 00:44:08 +00003841** Compare the key of the index entry that cursor pC is pointing to against
3842** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00003843** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00003844** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00003845**
drh5f82e3c2009-07-06 00:44:08 +00003846** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00003847** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00003848** is ignored as well. Hence, this routine only compares the prefixes
3849** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00003850*/
danielk1977183f9f72004-05-13 05:20:26 +00003851int sqlite3VdbeIdxKeyCompare(
drh295aedf2014-03-03 18:25:24 +00003852 VdbeCursor *pC, /* The cursor to compare against */
3853 const UnpackedRecord *pUnpacked, /* Unpacked version of key */
3854 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00003855){
drh61fc5952007-04-01 23:49:51 +00003856 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00003857 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00003858 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00003859 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00003860
drhea8ffdf2009-07-22 00:35:23 +00003861 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00003862 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00003863 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh56689692014-03-03 19:29:28 +00003864 /* nCellKey will always be between 0 and 0xffffffff because of the way
drh407414c2009-07-14 14:15:27 +00003865 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00003866 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00003867 *res = 0;
drh9978c972010-02-23 17:36:32 +00003868 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00003869 }
drhfd3ca1c2009-08-25 12:11:00 +00003870 memset(&m, 0, sizeof(m));
drh501932c2013-11-21 21:59:53 +00003871 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00003872 if( rc ){
drhd5788202004-05-28 08:21:05 +00003873 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00003874 }
dan3833e932014-03-01 19:44:56 +00003875 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
danielk1977d8123362004-06-12 09:25:12 +00003876 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00003877 return SQLITE_OK;
3878}
danielk1977b28af712004-06-21 06:50:26 +00003879
3880/*
3881** This routine sets the value to be returned by subsequent calls to
3882** sqlite3_changes() on the database handle 'db'.
3883*/
3884void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00003885 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00003886 db->nChange = nChange;
3887 db->nTotalChange += nChange;
3888}
3889
3890/*
3891** Set a flag in the vdbe to update the change counter when it is finalised
3892** or reset.
3893*/
drh4794f732004-11-05 17:17:50 +00003894void sqlite3VdbeCountChanges(Vdbe *v){
3895 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00003896}
drhd89bd002005-01-22 03:03:54 +00003897
3898/*
3899** Mark every prepared statement associated with a database connection
3900** as expired.
3901**
3902** An expired statement means that recompilation of the statement is
3903** recommend. Statements expire when things happen that make their
3904** programs obsolete. Removing user-defined functions or collating
3905** sequences, or changing an authorization function are the types of
3906** things that make prepared statements obsolete.
3907*/
3908void sqlite3ExpirePreparedStatements(sqlite3 *db){
3909 Vdbe *p;
3910 for(p = db->pVdbe; p; p=p->pNext){
3911 p->expired = 1;
3912 }
3913}
danielk1977aee18ef2005-03-09 12:26:50 +00003914
3915/*
3916** Return the database associated with the Vdbe.
3917*/
3918sqlite3 *sqlite3VdbeDb(Vdbe *v){
3919 return v->db;
3920}
dan937d0de2009-10-15 18:35:38 +00003921
3922/*
3923** Return a pointer to an sqlite3_value structure containing the value bound
3924** parameter iVar of VM v. Except, if the value is an SQL NULL, return
3925** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
3926** constants) to the value before returning it.
3927**
3928** The returned value must be freed by the caller using sqlite3ValueFree().
3929*/
drhcf0fd4a2013-08-01 12:21:58 +00003930sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
dan937d0de2009-10-15 18:35:38 +00003931 assert( iVar>0 );
3932 if( v ){
3933 Mem *pMem = &v->aVar[iVar-1];
3934 if( 0==(pMem->flags & MEM_Null) ){
3935 sqlite3_value *pRet = sqlite3ValueNew(v->db);
3936 if( pRet ){
3937 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
3938 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
3939 sqlite3VdbeMemStoreType((Mem *)pRet);
3940 }
3941 return pRet;
3942 }
3943 }
3944 return 0;
3945}
3946
3947/*
3948** Configure SQL variable iVar so that binding a new value to it signals
3949** to sqlite3_reoptimize() that re-preparing the statement may result
3950** in a better query plan.
3951*/
dan1d2ce4f2009-10-19 18:11:09 +00003952void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00003953 assert( iVar>0 );
3954 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00003955 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00003956 }else{
dan1d2ce4f2009-10-19 18:11:09 +00003957 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00003958 }
3959}
dan016f7812013-08-21 17:35:48 +00003960
3961#ifndef SQLITE_OMIT_VIRTUALTABLE
3962/*
3963** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
3964** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
3965** in memory obtained from sqlite3DbMalloc).
3966*/
3967void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
3968 sqlite3 *db = p->db;
3969 sqlite3DbFree(db, p->zErrMsg);
3970 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
3971 sqlite3_free(pVtab->zErrMsg);
3972 pVtab->zErrMsg = 0;
3973}
3974#endif /* SQLITE_OMIT_VIRTUALTABLE */