blob: e866e9dcfeab041e9b5b8a524be33be1dd60f063 [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
drh7abda852014-09-19 16:02:06 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
drh9a324642003-09-06 20:12:01 +000014*/
15#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000016#include "vdbeInt.h"
17
drh9a324642003-09-06 20:12:01 +000018/*
19** Create a new virtual database engine.
20*/
drh9ac79622013-12-18 15:11:47 +000021Vdbe *sqlite3VdbeCreate(Parse *pParse){
22 sqlite3 *db = pParse->db;
drh9a324642003-09-06 20:12:01 +000023 Vdbe *p;
drh17435752007-08-16 04:30:38 +000024 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000025 if( p==0 ) return 0;
26 p->db = db;
27 if( db->pVdbe ){
28 db->pVdbe->pPrev = p;
29 }
30 p->pNext = db->pVdbe;
31 p->pPrev = 0;
32 db->pVdbe = p;
33 p->magic = VDBE_MAGIC_INIT;
drh9ac79622013-12-18 15:11:47 +000034 p->pParse = pParse;
drh73d5b8f2013-12-23 19:09:07 +000035 assert( pParse->aLabel==0 );
36 assert( pParse->nLabel==0 );
37 assert( pParse->nOpAlloc==0 );
drh9a324642003-09-06 20:12:01 +000038 return p;
39}
40
41/*
drh22c17b82015-05-15 04:13:15 +000042** Change the error string stored in Vdbe.zErrMsg
43*/
44void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
45 va_list ap;
46 sqlite3DbFree(p->db, p->zErrMsg);
47 va_start(ap, zFormat);
48 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
49 va_end(ap);
50}
51
52/*
drhb900aaf2006-11-09 00:24:53 +000053** Remember the SQL string for a prepared statement.
54*/
danielk19776ab3a2e2009-02-19 14:39:25 +000055void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
dan1d2ce4f2009-10-19 18:11:09 +000056 assert( isPrepareV2==1 || isPrepareV2==0 );
drhb900aaf2006-11-09 00:24:53 +000057 if( p==0 ) return;
danac455932012-11-26 19:50:41 +000058#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
danielk19776ab3a2e2009-02-19 14:39:25 +000059 if( !isPrepareV2 ) return;
60#endif
drhb900aaf2006-11-09 00:24:53 +000061 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000062 p->zSql = sqlite3DbStrNDup(p->db, z, n);
shanef639c402009-11-03 19:42:30 +000063 p->isPrepareV2 = (u8)isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000064}
65
66/*
67** Return the SQL associated with a prepared statement
68*/
danielk1977d0e2a852007-11-14 06:48:48 +000069const char *sqlite3_sql(sqlite3_stmt *pStmt){
danielk19776ab3a2e2009-02-19 14:39:25 +000070 Vdbe *p = (Vdbe *)pStmt;
drh87f5c5f2010-01-20 01:20:56 +000071 return (p && p->isPrepareV2) ? p->zSql : 0;
drhb900aaf2006-11-09 00:24:53 +000072}
73
74/*
drhc5155252007-01-08 21:07:17 +000075** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000076*/
drhc5155252007-01-08 21:07:17 +000077void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
78 Vdbe tmp, *pTmp;
79 char *zTmp;
drhc5155252007-01-08 21:07:17 +000080 tmp = *pA;
81 *pA = *pB;
82 *pB = tmp;
83 pTmp = pA->pNext;
84 pA->pNext = pB->pNext;
85 pB->pNext = pTmp;
86 pTmp = pA->pPrev;
87 pA->pPrev = pB->pPrev;
88 pB->pPrev = pTmp;
89 zTmp = pA->zSql;
90 pA->zSql = pB->zSql;
91 pB->zSql = zTmp;
danielk19776ab3a2e2009-02-19 14:39:25 +000092 pB->isPrepareV2 = pA->isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000093}
94
drh9a324642003-09-06 20:12:01 +000095/*
dan76ccd892014-08-12 13:38:52 +000096** Resize the Vdbe.aOp array so that it is at least nOp elements larger
drh81e069e2014-08-12 14:29:20 +000097** than its current size. nOp is guaranteed to be less than or equal
98** to 1024/sizeof(Op).
danielk1977ace3eb22006-01-26 10:35:04 +000099**
danielk197700e13612008-11-17 19:18:54 +0000100** If an out-of-memory error occurs while resizing the array, return
dan76ccd892014-08-12 13:38:52 +0000101** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
danielk197700e13612008-11-17 19:18:54 +0000102** unchanged (this is so that any opcodes already allocated can be
103** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000104*/
dan76ccd892014-08-12 13:38:52 +0000105static int growOpArray(Vdbe *v, int nOp){
drha4e5d582007-10-20 15:41:57 +0000106 VdbeOp *pNew;
drh73d5b8f2013-12-23 19:09:07 +0000107 Parse *p = v->pParse;
dan76ccd892014-08-12 13:38:52 +0000108
drh81e069e2014-08-12 14:29:20 +0000109 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
110 ** more frequent reallocs and hence provide more opportunities for
111 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
112 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
113 ** by the minimum* amount required until the size reaches 512. Normal
114 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
115 ** size of the op array or add 1KB of space, whichever is smaller. */
dan76ccd892014-08-12 13:38:52 +0000116#ifdef SQLITE_TEST_REALLOC_STRESS
117 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
118#else
danielk197700e13612008-11-17 19:18:54 +0000119 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
dan76ccd892014-08-12 13:38:52 +0000120 UNUSED_PARAMETER(nOp);
121#endif
122
drh81e069e2014-08-12 14:29:20 +0000123 assert( nOp<=(1024/sizeof(Op)) );
dan76ccd892014-08-12 13:38:52 +0000124 assert( nNew>=(p->nOpAlloc+nOp) );
drh73d5b8f2013-12-23 19:09:07 +0000125 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000126 if( pNew ){
drhb45f65d2009-03-01 19:42:11 +0000127 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
drh73d5b8f2013-12-23 19:09:07 +0000128 v->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000129 }
danielk197700e13612008-11-17 19:18:54 +0000130 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000131}
132
drh313619f2013-10-31 20:34:06 +0000133#ifdef SQLITE_DEBUG
134/* This routine is just a convenient place to set a breakpoint that will
135** fire after each opcode is inserted and displayed using
136** "PRAGMA vdbe_addoptrace=on".
137*/
138static void test_addop_breakpoint(void){
139 static int n = 0;
140 n++;
141}
142#endif
143
drh76ff3a02004-09-24 22:32:30 +0000144/*
drh9a324642003-09-06 20:12:01 +0000145** Add a new instruction to the list of instructions current in the
146** VDBE. Return the address of the new instruction.
147**
148** Parameters:
149**
150** p Pointer to the VDBE
151**
152** op The opcode for this instruction
153**
drh66a51672008-01-03 00:01:23 +0000154** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000155**
danielk19774adee202004-05-08 08:23:19 +0000156** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000157** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000158** operand.
159*/
drh66a51672008-01-03 00:01:23 +0000160int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000161 int i;
drh701a0ae2004-02-22 20:05:00 +0000162 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000163
164 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000165 assert( p->magic==VDBE_MAGIC_INIT );
drh8df32842008-12-09 02:51:23 +0000166 assert( op>0 && op<0xff );
drh73d5b8f2013-12-23 19:09:07 +0000167 if( p->pParse->nOpAlloc<=i ){
dan76ccd892014-08-12 13:38:52 +0000168 if( growOpArray(p, 1) ){
drhc42ed162009-06-26 14:04:51 +0000169 return 1;
drhfd2d26b2006-03-15 22:44:36 +0000170 }
drh9a324642003-09-06 20:12:01 +0000171 }
danielk197701256832007-04-18 14:24:32 +0000172 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000173 pOp = &p->aOp[i];
drh8df32842008-12-09 02:51:23 +0000174 pOp->opcode = (u8)op;
drh26c9b5e2008-04-11 14:56:53 +0000175 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000176 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000177 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000178 pOp->p3 = p3;
179 pOp->p4.p = 0;
180 pOp->p4type = P4_NOTUSED;
drhc7379ce2013-10-30 02:28:23 +0000181#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000182 pOp->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000183#endif
184#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000185 if( p->db->flags & SQLITE_VdbeAddopTrace ){
drh9ac79622013-12-18 15:11:47 +0000186 int jj, kk;
187 Parse *pParse = p->pParse;
188 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
189 struct yColCache *x = pParse->aColCache + jj;
190 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
191 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
192 kk++;
193 }
194 if( kk ) printf("\n");
drhe0962052013-01-29 19:14:31 +0000195 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh313619f2013-10-31 20:34:06 +0000196 test_addop_breakpoint();
drhe0962052013-01-29 19:14:31 +0000197 }
drh9a324642003-09-06 20:12:01 +0000198#endif
drh26c9b5e2008-04-11 14:56:53 +0000199#ifdef VDBE_PROFILE
200 pOp->cycles = 0;
201 pOp->cnt = 0;
202#endif
drh688852a2014-02-17 22:40:43 +0000203#ifdef SQLITE_VDBE_COVERAGE
204 pOp->iSrcLine = 0;
205#endif
drh9a324642003-09-06 20:12:01 +0000206 return i;
207}
drh66a51672008-01-03 00:01:23 +0000208int sqlite3VdbeAddOp0(Vdbe *p, int op){
209 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
210}
211int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
212 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
213}
214int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
215 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000216}
217
drh66a51672008-01-03 00:01:23 +0000218
drh701a0ae2004-02-22 20:05:00 +0000219/*
drh66a51672008-01-03 00:01:23 +0000220** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000221*/
drh66a51672008-01-03 00:01:23 +0000222int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000223 Vdbe *p, /* Add the opcode to this VM */
224 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000225 int p1, /* The P1 operand */
226 int p2, /* The P2 operand */
227 int p3, /* The P3 operand */
228 const char *zP4, /* The P4 operand */
229 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000230){
drh66a51672008-01-03 00:01:23 +0000231 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
232 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000233 return addr;
234}
235
236/*
drh97bae792015-06-05 15:59:57 +0000237** Add an opcode that includes the p4 value with a P4_INT64 type.
238*/
239int sqlite3VdbeAddOp4Dup8(
240 Vdbe *p, /* Add the opcode to this VM */
241 int op, /* The new opcode */
242 int p1, /* The P1 operand */
243 int p2, /* The P2 operand */
244 int p3, /* The P3 operand */
245 const u8 *zP4, /* The P4 operand */
246 int p4type /* P4 operand type */
247){
248 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
249 if( p4copy ) memcpy(p4copy, zP4, 8);
250 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
251}
252
253/*
drh5d9c9da2011-06-03 20:11:17 +0000254** Add an OP_ParseSchema opcode. This routine is broken out from
drhe4c88c02012-01-04 12:57:45 +0000255** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
256** as having been used.
drh5d9c9da2011-06-03 20:11:17 +0000257**
258** The zWhere string must have been obtained from sqlite3_malloc().
259** This routine will take ownership of the allocated memory.
260*/
261void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
262 int j;
263 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
264 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
265 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
266}
267
268/*
drh8cff69d2009-11-12 19:59:44 +0000269** Add an opcode that includes the p4 value as an integer.
270*/
271int sqlite3VdbeAddOp4Int(
272 Vdbe *p, /* Add the opcode to this VM */
273 int op, /* The new opcode */
274 int p1, /* The P1 operand */
275 int p2, /* The P2 operand */
276 int p3, /* The P3 operand */
277 int p4 /* The P4 operand as an integer */
278){
279 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
280 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
281 return addr;
282}
283
284/*
drh9a324642003-09-06 20:12:01 +0000285** Create a new symbolic label for an instruction that has yet to be
286** coded. The symbolic label is really just a negative number. The
287** label can be used as the P2 value of an operation. Later, when
288** the label is resolved to a specific address, the VDBE will scan
289** through its operation list and change all values of P2 which match
290** the label into the resolved address.
291**
292** The VDBE knows that a P2 value is a label because labels are
293** always negative and P2 values are suppose to be non-negative.
294** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000295**
296** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000297*/
drh73d5b8f2013-12-23 19:09:07 +0000298int sqlite3VdbeMakeLabel(Vdbe *v){
299 Parse *p = v->pParse;
drhc35f3d52012-02-01 19:03:38 +0000300 int i = p->nLabel++;
drh73d5b8f2013-12-23 19:09:07 +0000301 assert( v->magic==VDBE_MAGIC_INIT );
drhc35f3d52012-02-01 19:03:38 +0000302 if( (i & (i-1))==0 ){
303 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
304 (i*2+1)*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000305 }
drh76ff3a02004-09-24 22:32:30 +0000306 if( p->aLabel ){
307 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000308 }
drh9a324642003-09-06 20:12:01 +0000309 return -1-i;
310}
311
312/*
313** Resolve label "x" to be the address of the next instruction to
314** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000315** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000316*/
drh73d5b8f2013-12-23 19:09:07 +0000317void sqlite3VdbeResolveLabel(Vdbe *v, int x){
318 Parse *p = v->pParse;
drh76ff3a02004-09-24 22:32:30 +0000319 int j = -1-x;
drh73d5b8f2013-12-23 19:09:07 +0000320 assert( v->magic==VDBE_MAGIC_INIT );
drhb2b9d3d2013-08-01 01:14:43 +0000321 assert( j<p->nLabel );
drhd2490902014-04-13 19:28:15 +0000322 if( ALWAYS(j>=0) && p->aLabel ){
drh73d5b8f2013-12-23 19:09:07 +0000323 p->aLabel[j] = v->nOp;
drh9a324642003-09-06 20:12:01 +0000324 }
drh61019c72014-01-04 16:49:02 +0000325 p->iFixedOp = v->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000326}
327
drh4611d922010-02-25 14:47:01 +0000328/*
329** Mark the VDBE as one that can only be run one time.
330*/
331void sqlite3VdbeRunOnlyOnce(Vdbe *p){
332 p->runOnlyOnce = 1;
333}
334
drhff738bc2009-09-24 00:09:58 +0000335#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
dan144926d2009-09-09 11:37:20 +0000336
337/*
338** The following type and function are used to iterate through all opcodes
339** in a Vdbe main program and each of the sub-programs (triggers) it may
340** invoke directly or indirectly. It should be used as follows:
341**
342** Op *pOp;
343** VdbeOpIter sIter;
344**
345** memset(&sIter, 0, sizeof(sIter));
346** sIter.v = v; // v is of type Vdbe*
347** while( (pOp = opIterNext(&sIter)) ){
348** // Do something with pOp
349** }
350** sqlite3DbFree(v->db, sIter.apSub);
351**
352*/
353typedef struct VdbeOpIter VdbeOpIter;
354struct VdbeOpIter {
355 Vdbe *v; /* Vdbe to iterate through the opcodes of */
356 SubProgram **apSub; /* Array of subprograms */
357 int nSub; /* Number of entries in apSub */
358 int iAddr; /* Address of next instruction to return */
359 int iSub; /* 0 = main program, 1 = first sub-program etc. */
360};
361static Op *opIterNext(VdbeOpIter *p){
362 Vdbe *v = p->v;
363 Op *pRet = 0;
364 Op *aOp;
365 int nOp;
366
367 if( p->iSub<=p->nSub ){
368
369 if( p->iSub==0 ){
370 aOp = v->aOp;
371 nOp = v->nOp;
372 }else{
373 aOp = p->apSub[p->iSub-1]->aOp;
374 nOp = p->apSub[p->iSub-1]->nOp;
375 }
376 assert( p->iAddr<nOp );
377
378 pRet = &aOp[p->iAddr];
379 p->iAddr++;
380 if( p->iAddr==nOp ){
381 p->iSub++;
382 p->iAddr = 0;
383 }
384
385 if( pRet->p4type==P4_SUBPROGRAM ){
386 int nByte = (p->nSub+1)*sizeof(SubProgram*);
387 int j;
388 for(j=0; j<p->nSub; j++){
389 if( p->apSub[j]==pRet->p4.pProgram ) break;
390 }
391 if( j==p->nSub ){
392 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
393 if( !p->apSub ){
394 pRet = 0;
395 }else{
396 p->apSub[p->nSub++] = pRet->p4.pProgram;
397 }
398 }
399 }
400 }
401
402 return pRet;
403}
404
405/*
danf3677212009-09-10 16:14:50 +0000406** Check if the program stored in the VM associated with pParse may
drhff738bc2009-09-24 00:09:58 +0000407** throw an ABORT exception (causing the statement, but not entire transaction
dan144926d2009-09-09 11:37:20 +0000408** to be rolled back). This condition is true if the main program or any
409** sub-programs contains any of the following:
410**
411** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
412** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
413** * OP_Destroy
414** * OP_VUpdate
415** * OP_VRename
dan32b09f22009-09-23 17:29:59 +0000416** * OP_FkCounter with P2==0 (immediate foreign key constraint)
drh0dd5cda2015-06-16 16:39:01 +0000417** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
dan144926d2009-09-09 11:37:20 +0000418**
danf3677212009-09-10 16:14:50 +0000419** Then check that the value of Parse.mayAbort is true if an
420** ABORT may be thrown, or false otherwise. Return true if it does
421** match, or false otherwise. This function is intended to be used as
422** part of an assert statement in the compiler. Similar to:
423**
424** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
dan144926d2009-09-09 11:37:20 +0000425*/
danf3677212009-09-10 16:14:50 +0000426int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
427 int hasAbort = 0;
dan04668832014-12-16 20:13:30 +0000428 int hasFkCounter = 0;
drh0dd5cda2015-06-16 16:39:01 +0000429 int hasCreateTable = 0;
430 int hasInitCoroutine = 0;
dan144926d2009-09-09 11:37:20 +0000431 Op *pOp;
432 VdbeOpIter sIter;
433 memset(&sIter, 0, sizeof(sIter));
434 sIter.v = v;
435
436 while( (pOp = opIterNext(&sIter))!=0 ){
437 int opcode = pOp->opcode;
438 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
439 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
drhd91c1a12013-02-09 13:58:25 +0000440 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
dan144926d2009-09-09 11:37:20 +0000441 ){
danf3677212009-09-10 16:14:50 +0000442 hasAbort = 1;
dan144926d2009-09-09 11:37:20 +0000443 break;
444 }
drh0dd5cda2015-06-16 16:39:01 +0000445 if( opcode==OP_CreateTable ) hasCreateTable = 1;
446 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
dan04668832014-12-16 20:13:30 +0000447#ifndef SQLITE_OMIT_FOREIGN_KEY
448 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
449 hasFkCounter = 1;
450 }
451#endif
dan144926d2009-09-09 11:37:20 +0000452 }
dan144926d2009-09-09 11:37:20 +0000453 sqlite3DbFree(v->db, sIter.apSub);
danf3677212009-09-10 16:14:50 +0000454
mistachkin48864df2013-03-21 21:20:32 +0000455 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
danf3677212009-09-10 16:14:50 +0000456 ** If malloc failed, then the while() loop above may not have iterated
457 ** through all opcodes and hasAbort may be set incorrectly. Return
458 ** true for this case to prevent the assert() in the callers frame
459 ** from failing. */
drh0dd5cda2015-06-16 16:39:01 +0000460 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
461 || (hasCreateTable && hasInitCoroutine) );
dan144926d2009-09-09 11:37:20 +0000462}
drhff738bc2009-09-24 00:09:58 +0000463#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
dan144926d2009-09-09 11:37:20 +0000464
drh9a324642003-09-06 20:12:01 +0000465/*
drh9cbf3422008-01-17 16:22:13 +0000466** Loop through the program looking for P2 values that are negative
467** on jump instructions. Each such value is a label. Resolve the
468** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000469**
470** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000471**
drh13449892005-09-07 21:22:45 +0000472** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000473** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000474** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
drha6c2ed92009-11-14 23:22:23 +0000475**
476** The Op.opflags field is set on all opcodes.
drh76ff3a02004-09-24 22:32:30 +0000477*/
drh9cbf3422008-01-17 16:22:13 +0000478static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000479 int i;
dan165921a2009-08-28 18:53:45 +0000480 int nMaxArgs = *pMaxFuncArgs;
drh76ff3a02004-09-24 22:32:30 +0000481 Op *pOp;
drh73d5b8f2013-12-23 19:09:07 +0000482 Parse *pParse = p->pParse;
483 int *aLabel = pParse->aLabel;
drhad4a4b82008-11-05 16:37:34 +0000484 p->readOnly = 1;
drh1713afb2013-06-28 01:24:57 +0000485 p->bIsReader = 0;
drh76ff3a02004-09-24 22:32:30 +0000486 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000487 u8 opcode = pOp->opcode;
488
drh8c8a8c42013-08-06 07:45:08 +0000489 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
490 ** cases from this switch! */
491 switch( opcode ){
drh8c8a8c42013-08-06 07:45:08 +0000492 case OP_Transaction: {
493 if( pOp->p2!=0 ) p->readOnly = 0;
494 /* fall thru */
495 }
496 case OP_AutoCommit:
497 case OP_Savepoint: {
498 p->bIsReader = 1;
499 break;
500 }
dand9031542013-07-05 16:54:30 +0000501#ifndef SQLITE_OMIT_WAL
drh8c8a8c42013-08-06 07:45:08 +0000502 case OP_Checkpoint:
drh9e92a472013-06-27 17:40:30 +0000503#endif
drh8c8a8c42013-08-06 07:45:08 +0000504 case OP_Vacuum:
505 case OP_JournalMode: {
506 p->readOnly = 0;
507 p->bIsReader = 1;
508 break;
509 }
danielk1977182c4ba2007-06-27 15:53:34 +0000510#ifndef SQLITE_OMIT_VIRTUALTABLE
drh8c8a8c42013-08-06 07:45:08 +0000511 case OP_VUpdate: {
512 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
513 break;
514 }
515 case OP_VFilter: {
516 int n;
517 assert( p->nOp - i >= 3 );
518 assert( pOp[-1].opcode==OP_Integer );
519 n = pOp[-1].p1;
520 if( n>nMaxArgs ) nMaxArgs = n;
521 break;
522 }
danielk1977182c4ba2007-06-27 15:53:34 +0000523#endif
drh8c8a8c42013-08-06 07:45:08 +0000524 case OP_Next:
drhf93cd942013-11-21 03:12:25 +0000525 case OP_NextIfOpen:
drh8c8a8c42013-08-06 07:45:08 +0000526 case OP_SorterNext: {
527 pOp->p4.xAdvance = sqlite3BtreeNext;
528 pOp->p4type = P4_ADVANCE;
529 break;
530 }
drhf93cd942013-11-21 03:12:25 +0000531 case OP_Prev:
532 case OP_PrevIfOpen: {
drh8c8a8c42013-08-06 07:45:08 +0000533 pOp->p4.xAdvance = sqlite3BtreePrevious;
534 pOp->p4type = P4_ADVANCE;
535 break;
536 }
danielk1977bc04f852005-03-29 08:26:13 +0000537 }
danielk1977634f2982005-03-28 08:44:07 +0000538
drh8c8a8c42013-08-06 07:45:08 +0000539 pOp->opflags = sqlite3OpcodeProperty[opcode];
drha6c2ed92009-11-14 23:22:23 +0000540 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
drh73d5b8f2013-12-23 19:09:07 +0000541 assert( -1-pOp->p2<pParse->nLabel );
drhd2981512008-01-04 19:33:49 +0000542 pOp->p2 = aLabel[-1-pOp->p2];
543 }
drh76ff3a02004-09-24 22:32:30 +0000544 }
drh73d5b8f2013-12-23 19:09:07 +0000545 sqlite3DbFree(p->db, pParse->aLabel);
546 pParse->aLabel = 0;
547 pParse->nLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000548 *pMaxFuncArgs = nMaxArgs;
drha7ab6d82014-07-21 15:44:39 +0000549 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
drh76ff3a02004-09-24 22:32:30 +0000550}
551
552/*
drh9a324642003-09-06 20:12:01 +0000553** Return the address of the next instruction to be inserted.
554*/
danielk19774adee202004-05-08 08:23:19 +0000555int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000556 assert( p->magic==VDBE_MAGIC_INIT );
557 return p->nOp;
558}
559
dan65a7cd12009-09-01 12:16:01 +0000560/*
561** This function returns a pointer to the array of opcodes associated with
562** the Vdbe passed as the first argument. It is the callers responsibility
563** to arrange for the returned array to be eventually freed using the
564** vdbeFreeOpArray() function.
565**
566** Before returning, *pnOp is set to the number of entries in the returned
567** array. Also, *pnMaxArg is set to the larger of its current value and
568** the number of entries in the Vdbe.apArg[] array required to execute the
569** returned program.
570*/
dan165921a2009-08-28 18:53:45 +0000571VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
572 VdbeOp *aOp = p->aOp;
dan523a0872009-08-31 05:23:32 +0000573 assert( aOp && !p->db->mallocFailed );
dan65a7cd12009-09-01 12:16:01 +0000574
575 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
drha7ab6d82014-07-21 15:44:39 +0000576 assert( DbMaskAllZero(p->btreeMask) );
dan65a7cd12009-09-01 12:16:01 +0000577
dan165921a2009-08-28 18:53:45 +0000578 resolveP2Values(p, pnMaxArg);
579 *pnOp = p->nOp;
580 p->aOp = 0;
581 return aOp;
582}
583
drh9a324642003-09-06 20:12:01 +0000584/*
585** Add a whole list of operations to the operation stack. Return the
586** address of the first operation added.
587*/
drh688852a2014-02-17 22:40:43 +0000588int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
drh9a324642003-09-06 20:12:01 +0000589 int addr;
590 assert( p->magic==VDBE_MAGIC_INIT );
dan76ccd892014-08-12 13:38:52 +0000591 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
drh76ff3a02004-09-24 22:32:30 +0000592 return 0;
drh9a324642003-09-06 20:12:01 +0000593 }
594 addr = p->nOp;
drh7b746032009-06-26 12:15:22 +0000595 if( ALWAYS(nOp>0) ){
drh9a324642003-09-06 20:12:01 +0000596 int i;
drh905793e2004-02-21 13:31:09 +0000597 VdbeOpList const *pIn = aOp;
598 for(i=0; i<nOp; i++, pIn++){
599 int p2 = pIn->p2;
600 VdbeOp *pOut = &p->aOp[i+addr];
601 pOut->opcode = pIn->opcode;
602 pOut->p1 = pIn->p1;
drh4308e342013-11-11 16:55:52 +0000603 if( p2<0 ){
604 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
drh8558cde2008-01-05 05:20:10 +0000605 pOut->p2 = addr + ADDR(p2);
606 }else{
607 pOut->p2 = p2;
608 }
drh24003452008-01-03 01:28:59 +0000609 pOut->p3 = pIn->p3;
610 pOut->p4type = P4_NOTUSED;
611 pOut->p4.p = 0;
612 pOut->p5 = 0;
drhc7379ce2013-10-30 02:28:23 +0000613#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh26c9b5e2008-04-11 14:56:53 +0000614 pOut->zComment = 0;
drhc7379ce2013-10-30 02:28:23 +0000615#endif
drh688852a2014-02-17 22:40:43 +0000616#ifdef SQLITE_VDBE_COVERAGE
617 pOut->iSrcLine = iLineno+i;
618#else
619 (void)iLineno;
620#endif
drhc7379ce2013-10-30 02:28:23 +0000621#ifdef SQLITE_DEBUG
drhe0962052013-01-29 19:14:31 +0000622 if( p->db->flags & SQLITE_VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000623 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000624 }
625#endif
626 }
627 p->nOp += nOp;
628 }
629 return addr;
630}
631
dan6f9702e2014-11-01 20:38:06 +0000632#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
633/*
634** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
635*/
dan037b5322014-11-03 11:25:32 +0000636void sqlite3VdbeScanStatus(
dan6f9702e2014-11-01 20:38:06 +0000637 Vdbe *p, /* VM to add scanstatus() to */
638 int addrExplain, /* Address of OP_Explain (or 0) */
639 int addrLoop, /* Address of loop counter */
640 int addrVisit, /* Address of rows visited counter */
drh518140e2014-11-06 03:55:10 +0000641 LogEst nEst, /* Estimated number of output rows */
dan6f9702e2014-11-01 20:38:06 +0000642 const char *zName /* Name of table or index being scanned */
643){
dan037b5322014-11-03 11:25:32 +0000644 int nByte = (p->nScan+1) * sizeof(ScanStatus);
645 ScanStatus *aNew;
646 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
dan6f9702e2014-11-01 20:38:06 +0000647 if( aNew ){
dan037b5322014-11-03 11:25:32 +0000648 ScanStatus *pNew = &aNew[p->nScan++];
dan6f9702e2014-11-01 20:38:06 +0000649 pNew->addrExplain = addrExplain;
650 pNew->addrLoop = addrLoop;
651 pNew->addrVisit = addrVisit;
652 pNew->nEst = nEst;
653 pNew->zName = sqlite3DbStrDup(p->db, zName);
654 p->aScan = aNew;
655 }
656}
657#endif
658
659
drh9a324642003-09-06 20:12:01 +0000660/*
661** Change the value of the P1 operand for a specific instruction.
662** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000663** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000664** few minor changes to the program.
665*/
drh88caeac2011-08-24 15:12:08 +0000666void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000667 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000668 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000669 p->aOp[addr].p1 = val;
670 }
671}
672
673/*
674** Change the value of the P2 operand for a specific instruction.
675** This routine is useful for setting a jump destination.
676*/
drh88caeac2011-08-24 15:12:08 +0000677void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000678 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000679 if( ((u32)p->nOp)>addr ){
drh9a324642003-09-06 20:12:01 +0000680 p->aOp[addr].p2 = val;
681 }
682}
683
drhd654be82005-09-20 17:42:23 +0000684/*
danielk19771f4aa332008-01-03 09:51:55 +0000685** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000686*/
drh88caeac2011-08-24 15:12:08 +0000687void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
drh7b746032009-06-26 12:15:22 +0000688 assert( p!=0 );
drh88caeac2011-08-24 15:12:08 +0000689 if( ((u32)p->nOp)>addr ){
danielk1977207872a2008-01-03 07:54:23 +0000690 p->aOp[addr].p3 = val;
691 }
692}
693
694/*
drh35573352008-01-08 23:54:25 +0000695** Change the value of the P5 operand for the most recently
696** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000697*/
drh35573352008-01-08 23:54:25 +0000698void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
drh7b746032009-06-26 12:15:22 +0000699 assert( p!=0 );
700 if( p->aOp ){
drh35573352008-01-08 23:54:25 +0000701 assert( p->nOp>0 );
702 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000703 }
704}
705
706/*
drhf8875402006-03-17 13:56:34 +0000707** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000708** the address of the next instruction to be coded.
709*/
710void sqlite3VdbeJumpHere(Vdbe *p, int addr){
drh61019c72014-01-04 16:49:02 +0000711 sqlite3VdbeChangeP2(p, addr, p->nOp);
712 p->pParse->iFixedOp = p->nOp - 1;
drhd654be82005-09-20 17:42:23 +0000713}
drhb38ad992005-09-16 00:27:01 +0000714
drhb7f6f682006-07-08 17:06:43 +0000715
716/*
717** If the input FuncDef structure is ephemeral, then free it. If
718** the FuncDef is not ephermal, then do nothing.
719*/
drh633e6d52008-07-28 19:34:53 +0000720static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhd36e1042013-09-06 13:10:12 +0000721 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000722 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000723 }
724}
725
dand46def72010-07-24 11:28:28 +0000726static void vdbeFreeOpArray(sqlite3 *, Op *, int);
727
drhb38ad992005-09-16 00:27:01 +0000728/*
drh66a51672008-01-03 00:01:23 +0000729** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000730*/
drh633e6d52008-07-28 19:34:53 +0000731static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000732 if( p4 ){
dand46def72010-07-24 11:28:28 +0000733 assert( db );
drh66a51672008-01-03 00:01:23 +0000734 switch( p4type ){
drh9c7c9132015-06-26 18:16:52 +0000735 case P4_FUNCCTX: {
736 freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
737 /* Fall through into the next case */
738 }
drh66a51672008-01-03 00:01:23 +0000739 case P4_REAL:
740 case P4_INT64:
drh66a51672008-01-03 00:01:23 +0000741 case P4_DYNAMIC:
drh2ec2fb22013-11-06 19:59:23 +0000742 case P4_INTARRAY: {
drh633e6d52008-07-28 19:34:53 +0000743 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000744 break;
745 }
drh2ec2fb22013-11-06 19:59:23 +0000746 case P4_KEYINFO: {
747 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
748 break;
749 }
drhb9755982010-07-24 16:34:37 +0000750 case P4_MPRINTF: {
drh7043db92010-07-26 12:38:12 +0000751 if( db->pnBytesFreed==0 ) sqlite3_free(p4);
drhb9755982010-07-24 16:34:37 +0000752 break;
753 }
drh66a51672008-01-03 00:01:23 +0000754 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000755 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000756 break;
757 }
drh66a51672008-01-03 00:01:23 +0000758 case P4_MEM: {
drhc176c272010-07-26 13:57:59 +0000759 if( db->pnBytesFreed==0 ){
760 sqlite3ValueFree((sqlite3_value*)p4);
761 }else{
drhf37c68e2010-07-26 14:20:06 +0000762 Mem *p = (Mem*)p4;
drh17bcb102014-09-18 21:25:33 +0000763 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
drhf37c68e2010-07-26 14:20:06 +0000764 sqlite3DbFree(db, p);
drhc176c272010-07-26 13:57:59 +0000765 }
drhac1733d2005-09-17 17:58:22 +0000766 break;
767 }
danielk1977595a5232009-07-24 17:58:53 +0000768 case P4_VTAB : {
dand46def72010-07-24 11:28:28 +0000769 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
danielk1977595a5232009-07-24 17:58:53 +0000770 break;
771 }
drhb38ad992005-09-16 00:27:01 +0000772 }
773 }
774}
775
dan65a7cd12009-09-01 12:16:01 +0000776/*
777** Free the space allocated for aOp and any p4 values allocated for the
778** opcodes contained within. If aOp is not NULL it is assumed to contain
779** nOp entries.
780*/
dan165921a2009-08-28 18:53:45 +0000781static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
782 if( aOp ){
783 Op *pOp;
784 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
785 freeP4(db, pOp->p4type, pOp->p4.p);
drhc7379ce2013-10-30 02:28:23 +0000786#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000787 sqlite3DbFree(db, pOp->zComment);
788#endif
789 }
790 }
791 sqlite3DbFree(db, aOp);
792}
793
dan65a7cd12009-09-01 12:16:01 +0000794/*
dand19c9332010-07-26 12:05:17 +0000795** Link the SubProgram object passed as the second argument into the linked
796** list at Vdbe.pSubProgram. This list is used to delete all sub-program
797** objects when the VM is no longer required.
dan65a7cd12009-09-01 12:16:01 +0000798*/
dand19c9332010-07-26 12:05:17 +0000799void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
800 p->pNext = pVdbe->pProgram;
801 pVdbe->pProgram = p;
dan165921a2009-08-28 18:53:45 +0000802}
803
drh9a324642003-09-06 20:12:01 +0000804/*
drh48f2d3b2011-09-16 01:34:43 +0000805** Change the opcode at addr into OP_Noop
drhf8875402006-03-17 13:56:34 +0000806*/
drh48f2d3b2011-09-16 01:34:43 +0000807void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
dan76ccd892014-08-12 13:38:52 +0000808 if( addr<p->nOp ){
danielk197792d4d7a2007-05-04 12:05:56 +0000809 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000810 sqlite3 *db = p->db;
drh48f2d3b2011-09-16 01:34:43 +0000811 freeP4(db, pOp->p4type, pOp->p4.p);
812 memset(pOp, 0, sizeof(pOp[0]));
813 pOp->opcode = OP_Noop;
drh313619f2013-10-31 20:34:06 +0000814 if( addr==p->nOp-1 ) p->nOp--;
drhf8875402006-03-17 13:56:34 +0000815 }
816}
817
818/*
drh39c4b822014-09-29 15:42:01 +0000819** If the last opcode is "op" and it is not a jump destination,
820** then remove it. Return true if and only if an opcode was removed.
drh762c1c42014-01-02 19:35:30 +0000821*/
drh61019c72014-01-04 16:49:02 +0000822int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
823 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
824 sqlite3VdbeChangeToNoop(p, p->nOp-1);
825 return 1;
826 }else{
827 return 0;
828 }
drh762c1c42014-01-02 19:35:30 +0000829}
830
831/*
drh66a51672008-01-03 00:01:23 +0000832** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000833** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000834** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000835** few minor changes to the program.
836**
drh66a51672008-01-03 00:01:23 +0000837** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000838** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000839** A value of n==0 means copy bytes of zP4 up to and including the
840** first null byte. If n>0 then copy n+1 bytes of zP4.
danielk19771f55c052005-05-19 08:42:59 +0000841**
drh66a51672008-01-03 00:01:23 +0000842** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000843** to a string or structure that is guaranteed to exist for the lifetime of
844** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000845**
drh66a51672008-01-03 00:01:23 +0000846** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000847*/
drh66a51672008-01-03 00:01:23 +0000848void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000849 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000850 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000851 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000852 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000853 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000854 if( p->aOp==0 || db->mallocFailed ){
drh2ec2fb22013-11-06 19:59:23 +0000855 if( n!=P4_VTAB ){
drh633e6d52008-07-28 19:34:53 +0000856 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000857 }
danielk1977d5d56522005-03-16 12:15:20 +0000858 return;
859 }
drh7b746032009-06-26 12:15:22 +0000860 assert( p->nOp>0 );
drh91fd4d42008-01-19 20:11:25 +0000861 assert( addr<p->nOp );
862 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000863 addr = p->nOp - 1;
drh9a324642003-09-06 20:12:01 +0000864 }
865 pOp = &p->aOp[addr];
drh079a3072014-03-19 14:10:55 +0000866 assert( pOp->p4type==P4_NOTUSED
867 || pOp->p4type==P4_INT32
868 || pOp->p4type==P4_KEYINFO );
drh633e6d52008-07-28 19:34:53 +0000869 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000870 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000871 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000872 /* Note: this cast is safe, because the origin data point was an int
873 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000874 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000875 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000876 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000877 pOp->p4.p = 0;
878 pOp->p4type = P4_NOTUSED;
879 }else if( n==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +0000880 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000881 pOp->p4type = P4_KEYINFO;
danielk1977595a5232009-07-24 17:58:53 +0000882 }else if( n==P4_VTAB ){
883 pOp->p4.p = (void*)zP4;
884 pOp->p4type = P4_VTAB;
885 sqlite3VtabLock((VTable *)zP4);
886 assert( ((VTable *)zP4)->db==p->db );
drh9a324642003-09-06 20:12:01 +0000887 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000888 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000889 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000890 }else{
drhea678832008-12-10 19:26:22 +0000891 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000892 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000893 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000894 }
895}
896
drh2ec2fb22013-11-06 19:59:23 +0000897/*
898** Set the P4 on the most recently added opcode to the KeyInfo for the
899** index given.
900*/
901void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
902 Vdbe *v = pParse->pVdbe;
903 assert( v!=0 );
904 assert( pIdx!=0 );
905 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
906 P4_KEYINFO);
907}
908
drhc7379ce2013-10-30 02:28:23 +0000909#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drhad6d9462004-09-19 02:15:24 +0000910/*
mistachkind5578432012-08-25 10:01:29 +0000911** Change the comment on the most recently coded instruction. Or
drh16ee60f2008-06-20 18:13:25 +0000912** insert a No-op and add the comment to that new instruction. This
913** makes the code easier to read during debugging. None of this happens
914** in a production build.
drhad6d9462004-09-19 02:15:24 +0000915*/
drhb07028f2011-10-14 21:49:18 +0000916static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
danielk197701256832007-04-18 14:24:32 +0000917 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000918 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000919 if( p->nOp ){
drhb07028f2011-10-14 21:49:18 +0000920 assert( p->aOp );
921 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
922 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
923 }
924}
925void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
926 va_list ap;
927 if( p ){
danielk1977dba01372008-01-05 18:44:29 +0000928 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000929 vdbeVComment(p, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000930 va_end(ap);
931 }
drhad6d9462004-09-19 02:15:24 +0000932}
drh16ee60f2008-06-20 18:13:25 +0000933void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
934 va_list ap;
drhb07028f2011-10-14 21:49:18 +0000935 if( p ){
936 sqlite3VdbeAddOp0(p, OP_Noop);
drh16ee60f2008-06-20 18:13:25 +0000937 va_start(ap, zFormat);
drhb07028f2011-10-14 21:49:18 +0000938 vdbeVComment(p, zFormat, ap);
drh16ee60f2008-06-20 18:13:25 +0000939 va_end(ap);
940 }
941}
942#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000943
drh688852a2014-02-17 22:40:43 +0000944#ifdef SQLITE_VDBE_COVERAGE
945/*
946** Set the value if the iSrcLine field for the previously coded instruction.
947*/
948void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
949 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
950}
951#endif /* SQLITE_VDBE_COVERAGE */
952
drh9a324642003-09-06 20:12:01 +0000953/*
drh20411ea2009-05-29 19:00:12 +0000954** Return the opcode for a given address. If the address is -1, then
955** return the most recently inserted opcode.
956**
957** If a memory allocation error has occurred prior to the calling of this
958** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
drhf83dc1e2010-06-03 12:09:52 +0000959** is readable but not writable, though it is cast to a writable value.
960** The return of a dummy opcode allows the call to continue functioning
peter.d.reid60ec9142014-09-06 16:39:46 +0000961** after an OOM fault without having to check to see if the return from
drhf83dc1e2010-06-03 12:09:52 +0000962** this routine is a valid pointer. But because the dummy.opcode is 0,
963** dummy will never be written to. This is verified by code inspection and
964** by running with Valgrind.
drh9a324642003-09-06 20:12:01 +0000965*/
danielk19774adee202004-05-08 08:23:19 +0000966VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drha0b75da2010-07-02 18:44:37 +0000967 /* C89 specifies that the constant "dummy" will be initialized to all
968 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
mistachkin0fe5f952011-09-14 18:19:08 +0000969 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
drh9a324642003-09-06 20:12:01 +0000970 assert( p->magic==VDBE_MAGIC_INIT );
drh37b89a02009-06-19 00:33:31 +0000971 if( addr<0 ){
drh37b89a02009-06-19 00:33:31 +0000972 addr = p->nOp - 1;
973 }
drh17435752007-08-16 04:30:38 +0000974 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000975 if( p->db->mallocFailed ){
drhf83dc1e2010-06-03 12:09:52 +0000976 return (VdbeOp*)&dummy;
drh20411ea2009-05-29 19:00:12 +0000977 }else{
978 return &p->aOp[addr];
979 }
drh9a324642003-09-06 20:12:01 +0000980}
981
drhc7379ce2013-10-30 02:28:23 +0000982#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
drh81316f82013-10-29 20:40:47 +0000983/*
drhf63552b2013-10-30 00:25:03 +0000984** Return an integer value for one of the parameters to the opcode pOp
985** determined by character c.
986*/
987static int translateP(char c, const Op *pOp){
988 if( c=='1' ) return pOp->p1;
989 if( c=='2' ) return pOp->p2;
990 if( c=='3' ) return pOp->p3;
991 if( c=='4' ) return pOp->p4.i;
992 return pOp->p5;
993}
994
drh81316f82013-10-29 20:40:47 +0000995/*
drh4eded602013-12-20 15:59:20 +0000996** Compute a string for the "comment" field of a VDBE opcode listing.
997**
998** The Synopsis: field in comments in the vdbe.c source file gets converted
999** to an extra string that is appended to the sqlite3OpcodeName(). In the
1000** absence of other comments, this synopsis becomes the comment on the opcode.
1001** Some translation occurs:
1002**
1003** "PX" -> "r[X]"
1004** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
1005** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
1006** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
drh81316f82013-10-29 20:40:47 +00001007*/
drhf63552b2013-10-30 00:25:03 +00001008static int displayComment(
1009 const Op *pOp, /* The opcode to be commented */
1010 const char *zP4, /* Previously obtained value for P4 */
1011 char *zTemp, /* Write result here */
1012 int nTemp /* Space available in zTemp[] */
1013){
drh81316f82013-10-29 20:40:47 +00001014 const char *zOpName;
1015 const char *zSynopsis;
1016 int nOpName;
1017 int ii, jj;
1018 zOpName = sqlite3OpcodeName(pOp->opcode);
1019 nOpName = sqlite3Strlen30(zOpName);
1020 if( zOpName[nOpName+1] ){
1021 int seenCom = 0;
drhf63552b2013-10-30 00:25:03 +00001022 char c;
drh81316f82013-10-29 20:40:47 +00001023 zSynopsis = zOpName += nOpName + 1;
drhf63552b2013-10-30 00:25:03 +00001024 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
1025 if( c=='P' ){
1026 c = zSynopsis[++ii];
1027 if( c=='4' ){
1028 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
1029 }else if( c=='X' ){
1030 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
1031 seenCom = 1;
drh81316f82013-10-29 20:40:47 +00001032 }else{
drhf63552b2013-10-30 00:25:03 +00001033 int v1 = translateP(c, pOp);
1034 int v2;
1035 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
1036 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
1037 ii += 3;
1038 jj += sqlite3Strlen30(zTemp+jj);
1039 v2 = translateP(zSynopsis[ii], pOp);
drh4eded602013-12-20 15:59:20 +00001040 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
1041 ii += 2;
1042 v2++;
1043 }
1044 if( v2>1 ){
1045 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
1046 }
drhf63552b2013-10-30 00:25:03 +00001047 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
1048 ii += 4;
1049 }
drh81316f82013-10-29 20:40:47 +00001050 }
1051 jj += sqlite3Strlen30(zTemp+jj);
1052 }else{
drhf63552b2013-10-30 00:25:03 +00001053 zTemp[jj++] = c;
drh81316f82013-10-29 20:40:47 +00001054 }
1055 }
1056 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
1057 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
1058 jj += sqlite3Strlen30(zTemp+jj);
1059 }
1060 if( jj<nTemp ) zTemp[jj] = 0;
1061 }else if( pOp->zComment ){
1062 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
1063 jj = sqlite3Strlen30(zTemp);
1064 }else{
1065 zTemp[0] = 0;
1066 jj = 0;
1067 }
1068 return jj;
1069}
1070#endif /* SQLITE_DEBUG */
1071
1072
drhb7f91642004-10-31 02:22:47 +00001073#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
1074 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +00001075/*
drh66a51672008-01-03 00:01:23 +00001076** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +00001077** Use zTemp for any required temporary buffer space.
1078*/
drh66a51672008-01-03 00:01:23 +00001079static char *displayP4(Op *pOp, char *zTemp, int nTemp){
1080 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +00001081 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +00001082 switch( pOp->p4type ){
1083 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +00001084 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +00001085 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drhe1a022e2012-09-17 17:16:53 +00001086 assert( pKeyInfo->aSortOrder!=0 );
drh5b843aa2013-10-30 13:46:01 +00001087 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +00001088 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +00001089 for(j=0; j<pKeyInfo->nField; j++){
1090 CollSeq *pColl = pKeyInfo->aColl[j];
drh261d8a52012-12-08 21:36:26 +00001091 const char *zColl = pColl ? pColl->zName : "nil";
1092 int n = sqlite3Strlen30(zColl);
drh5b843aa2013-10-30 13:46:01 +00001093 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
1094 zColl = "B";
1095 n = 1;
1096 }
drhd5a74c82015-08-15 16:32:50 +00001097 if( i+n>nTemp-7 ){
drh261d8a52012-12-08 21:36:26 +00001098 memcpy(&zTemp[i],",...",4);
drhd5a74c82015-08-15 16:32:50 +00001099 i += 4;
drh261d8a52012-12-08 21:36:26 +00001100 break;
drhd3d39e92004-05-20 22:16:29 +00001101 }
drh261d8a52012-12-08 21:36:26 +00001102 zTemp[i++] = ',';
1103 if( pKeyInfo->aSortOrder[j] ){
1104 zTemp[i++] = '-';
1105 }
1106 memcpy(&zTemp[i], zColl, n+1);
1107 i += n;
drhd3d39e92004-05-20 22:16:29 +00001108 }
1109 zTemp[i++] = ')';
1110 zTemp[i] = 0;
1111 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +00001112 break;
1113 }
drh66a51672008-01-03 00:01:23 +00001114 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +00001115 CollSeq *pColl = pOp->p4.pColl;
drh5e6790c2013-11-12 20:18:14 +00001116 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +00001117 break;
1118 }
drh66a51672008-01-03 00:01:23 +00001119 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +00001120 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +00001121 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +00001122 break;
1123 }
drhe2d9e7c2015-06-26 18:47:53 +00001124#ifdef SQLITE_DEBUG
drh9c7c9132015-06-26 18:16:52 +00001125 case P4_FUNCCTX: {
1126 FuncDef *pDef = pOp->p4.pCtx->pFunc;
1127 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
1128 break;
1129 }
drhe2d9e7c2015-06-26 18:47:53 +00001130#endif
drh66a51672008-01-03 00:01:23 +00001131 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +00001132 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +00001133 break;
1134 }
drh66a51672008-01-03 00:01:23 +00001135 case P4_INT32: {
1136 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +00001137 break;
1138 }
drh66a51672008-01-03 00:01:23 +00001139 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +00001140 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +00001141 break;
1142 }
drh66a51672008-01-03 00:01:23 +00001143 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +00001144 Mem *pMem = pOp->p4.pMem;
drhd4e70eb2008-01-02 00:34:36 +00001145 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +00001146 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +00001147 }else if( pMem->flags & MEM_Int ){
1148 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
1149 }else if( pMem->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +00001150 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
drhb8475df2011-12-09 16:21:19 +00001151 }else if( pMem->flags & MEM_Null ){
1152 sqlite3_snprintf(nTemp, zTemp, "NULL");
drh56016892009-08-25 14:24:04 +00001153 }else{
1154 assert( pMem->flags & MEM_Blob );
1155 zP4 = "(blob)";
drhd4e70eb2008-01-02 00:34:36 +00001156 }
drh598f1342007-10-23 15:39:45 +00001157 break;
1158 }
drha967e882006-06-13 01:04:52 +00001159#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +00001160 case P4_VTAB: {
danielk1977595a5232009-07-24 17:58:53 +00001161 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
drh466fd812015-03-24 14:57:02 +00001162 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
drha967e882006-06-13 01:04:52 +00001163 break;
1164 }
1165#endif
drh0acb7e42008-06-25 00:12:41 +00001166 case P4_INTARRAY: {
1167 sqlite3_snprintf(nTemp, zTemp, "intarray");
1168 break;
1169 }
dan165921a2009-08-28 18:53:45 +00001170 case P4_SUBPROGRAM: {
1171 sqlite3_snprintf(nTemp, zTemp, "program");
1172 break;
1173 }
drh4a6f3aa2011-08-28 00:19:26 +00001174 case P4_ADVANCE: {
1175 zTemp[0] = 0;
1176 break;
1177 }
drhd3d39e92004-05-20 22:16:29 +00001178 default: {
danielk19772dca4ac2008-01-03 11:50:29 +00001179 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +00001180 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +00001181 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +00001182 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +00001183 }
1184 }
1185 }
drh66a51672008-01-03 00:01:23 +00001186 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +00001187 return zP4;
drhd3d39e92004-05-20 22:16:29 +00001188}
drhb7f91642004-10-31 02:22:47 +00001189#endif
drhd3d39e92004-05-20 22:16:29 +00001190
drh900b31e2007-08-28 02:27:51 +00001191/*
drhd0679ed2007-08-28 22:24:34 +00001192** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
drh3ebaee92010-05-06 21:37:22 +00001193**
drhbdaec522011-04-04 00:14:43 +00001194** The prepared statements need to know in advance the complete set of
drhe4c88c02012-01-04 12:57:45 +00001195** attached databases that will be use. A mask of these databases
1196** is maintained in p->btreeMask. The p->lockMask value is the subset of
1197** p->btreeMask of databases that will require a lock.
drh900b31e2007-08-28 02:27:51 +00001198*/
drhfb982642007-08-30 01:19:59 +00001199void sqlite3VdbeUsesBtree(Vdbe *p, int i){
drhfcd71b62011-04-05 22:08:24 +00001200 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
danielk197700e13612008-11-17 19:18:54 +00001201 assert( i<(int)sizeof(p->btreeMask)*8 );
drha7ab6d82014-07-21 15:44:39 +00001202 DbMaskSet(p->btreeMask, i);
drhdc5b0472011-04-06 22:05:53 +00001203 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
drha7ab6d82014-07-21 15:44:39 +00001204 DbMaskSet(p->lockMask, i);
drhdc5b0472011-04-06 22:05:53 +00001205 }
drh900b31e2007-08-28 02:27:51 +00001206}
1207
drhe54e0512011-04-05 17:31:56 +00001208#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001209/*
1210** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1211** this routine obtains the mutex associated with each BtShared structure
1212** that may be accessed by the VM passed as an argument. In doing so it also
1213** sets the BtShared.db member of each of the BtShared structures, ensuring
1214** that the correct busy-handler callback is invoked if required.
1215**
1216** If SQLite is not threadsafe but does support shared-cache mode, then
1217** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1218** of all of BtShared structures accessible via the database handle
1219** associated with the VM.
1220**
1221** If SQLite is not threadsafe and does not support shared-cache mode, this
1222** function is a no-op.
1223**
1224** The p->btreeMask field is a bitmask of all btrees that the prepared
1225** statement p will ever use. Let N be the number of bits in p->btreeMask
1226** corresponding to btrees that use shared cache. Then the runtime of
1227** this routine is N*N. But as N is rarely more than 1, this should not
1228** be a problem.
1229*/
1230void sqlite3VdbeEnter(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001231 int i;
drhdc5b0472011-04-06 22:05:53 +00001232 sqlite3 *db;
1233 Db *aDb;
1234 int nDb;
drha7ab6d82014-07-21 15:44:39 +00001235 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
drhdc5b0472011-04-06 22:05:53 +00001236 db = p->db;
1237 aDb = db->aDb;
1238 nDb = db->nDb;
drha7ab6d82014-07-21 15:44:39 +00001239 for(i=0; i<nDb; i++){
1240 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001241 sqlite3BtreeEnter(aDb[i].pBt);
1242 }
1243 }
drhbdaec522011-04-04 00:14:43 +00001244}
drhe54e0512011-04-05 17:31:56 +00001245#endif
drhbdaec522011-04-04 00:14:43 +00001246
drhe54e0512011-04-05 17:31:56 +00001247#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
drhbdaec522011-04-04 00:14:43 +00001248/*
1249** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1250*/
drhf1aabd62015-06-17 01:31:28 +00001251static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
drhbdaec522011-04-04 00:14:43 +00001252 int i;
drhdc5b0472011-04-06 22:05:53 +00001253 sqlite3 *db;
1254 Db *aDb;
1255 int nDb;
drhdc5b0472011-04-06 22:05:53 +00001256 db = p->db;
1257 aDb = db->aDb;
1258 nDb = db->nDb;
drha7ab6d82014-07-21 15:44:39 +00001259 for(i=0; i<nDb; i++){
1260 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
drhbdaec522011-04-04 00:14:43 +00001261 sqlite3BtreeLeave(aDb[i].pBt);
1262 }
1263 }
drhbdaec522011-04-04 00:14:43 +00001264}
drhf1aabd62015-06-17 01:31:28 +00001265void sqlite3VdbeLeave(Vdbe *p){
1266 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
1267 vdbeLeave(p);
1268}
drhbdaec522011-04-04 00:14:43 +00001269#endif
drhd3d39e92004-05-20 22:16:29 +00001270
danielk19778b60e0f2005-01-12 09:10:39 +00001271#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +00001272/*
1273** Print a single opcode. This routine is used for debugging only.
1274*/
danielk19774adee202004-05-08 08:23:19 +00001275void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +00001276 char *zP4;
drhd3d39e92004-05-20 22:16:29 +00001277 char zPtr[50];
drh81316f82013-10-29 20:40:47 +00001278 char zCom[100];
drh26198bb2013-10-31 11:15:09 +00001279 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +00001280 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +00001281 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
drhc7379ce2013-10-30 02:28:23 +00001282#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh81316f82013-10-29 20:40:47 +00001283 displayComment(pOp, zP4, zCom, sizeof(zCom));
1284#else
drh2926f962014-02-17 01:13:28 +00001285 zCom[0] = 0;
drh81316f82013-10-29 20:40:47 +00001286#endif
drh4eded602013-12-20 15:59:20 +00001287 /* NB: The sqlite3OpcodeName() function is implemented by code created
1288 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1289 ** information from the vdbe.c source text */
danielk197711641c12008-01-03 08:18:30 +00001290 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +00001291 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
drh81316f82013-10-29 20:40:47 +00001292 zCom
drh1db639c2008-01-17 02:36:28 +00001293 );
drh9a324642003-09-06 20:12:01 +00001294 fflush(pOut);
1295}
1296#endif
1297
1298/*
drh76ff3a02004-09-24 22:32:30 +00001299** Release an array of N Mem elements
1300*/
drhc890fec2008-08-01 20:10:08 +00001301static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +00001302 if( p && N ){
drh069c23c2014-09-19 16:13:12 +00001303 Mem *pEnd = &p[N];
danielk1977a7a8e142008-02-13 18:25:27 +00001304 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +00001305 u8 malloc_failed = db->mallocFailed;
dand46def72010-07-24 11:28:28 +00001306 if( db->pnBytesFreed ){
drh069c23c2014-09-19 16:13:12 +00001307 do{
drh17bcb102014-09-18 21:25:33 +00001308 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
drh069c23c2014-09-19 16:13:12 +00001309 }while( (++p)<pEnd );
drhc176c272010-07-26 13:57:59 +00001310 return;
1311 }
drh069c23c2014-09-19 16:13:12 +00001312 do{
danielk1977e972e032008-09-19 18:32:26 +00001313 assert( (&p[1])==pEnd || p[0].db==p[1].db );
drh75fd0542014-03-01 16:24:44 +00001314 assert( sqlite3VdbeCheckMemInvariants(p) );
danielk1977e972e032008-09-19 18:32:26 +00001315
1316 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1317 ** that takes advantage of the fact that the memory cell value is
1318 ** being set to NULL after releasing any dynamic resources.
1319 **
1320 ** The justification for duplicating code is that according to
1321 ** callgrind, this causes a certain test case to hit the CPU 4.7
1322 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1323 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1324 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1325 ** with no indexes using a single prepared INSERT statement, bind()
1326 ** and reset(). Inserts are grouped into a transaction.
1327 */
drhb6e8fd12014-03-06 01:56:33 +00001328 testcase( p->flags & MEM_Agg );
1329 testcase( p->flags & MEM_Dyn );
1330 testcase( p->flags & MEM_Frame );
1331 testcase( p->flags & MEM_RowSet );
dan165921a2009-08-28 18:53:45 +00001332 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
danielk1977e972e032008-09-19 18:32:26 +00001333 sqlite3VdbeMemRelease(p);
drh17bcb102014-09-18 21:25:33 +00001334 }else if( p->szMalloc ){
danielk1977e972e032008-09-19 18:32:26 +00001335 sqlite3DbFree(db, p->zMalloc);
drh17bcb102014-09-18 21:25:33 +00001336 p->szMalloc = 0;
danielk1977e972e032008-09-19 18:32:26 +00001337 }
1338
drha5750cf2014-02-07 13:20:31 +00001339 p->flags = MEM_Undefined;
drh069c23c2014-09-19 16:13:12 +00001340 }while( (++p)<pEnd );
danielk1977a7a8e142008-02-13 18:25:27 +00001341 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +00001342 }
1343}
1344
dan65a7cd12009-09-01 12:16:01 +00001345/*
1346** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1347** allocated by the OP_Program opcode in sqlite3VdbeExec().
1348*/
dan165921a2009-08-28 18:53:45 +00001349void sqlite3VdbeFrameDelete(VdbeFrame *p){
1350 int i;
1351 Mem *aMem = VdbeFrameMem(p);
1352 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1353 for(i=0; i<p->nChildCsr; i++){
1354 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1355 }
1356 releaseMemArray(aMem, p->nChildMem);
1357 sqlite3DbFree(p->v->db, p);
1358}
1359
drhb7f91642004-10-31 02:22:47 +00001360#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +00001361/*
drh9a324642003-09-06 20:12:01 +00001362** Give a listing of the program in the virtual machine.
1363**
danielk19774adee202004-05-08 08:23:19 +00001364** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +00001365** running the code, it invokes the callback once for each instruction.
1366** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +00001367**
1368** When p->explain==1, each instruction is listed. When
1369** p->explain==2, only OP_Explain instructions are listed and these
1370** are shown in a different format. p->explain==2 is used to implement
1371** EXPLAIN QUERY PLAN.
drh5cfa5842009-12-31 20:35:08 +00001372**
1373** When p->explain==1, first the main program is listed, then each of
1374** the trigger subprograms are listed one by one.
drh9a324642003-09-06 20:12:01 +00001375*/
danielk19774adee202004-05-08 08:23:19 +00001376int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +00001377 Vdbe *p /* The VDBE */
1378){
drh5cfa5842009-12-31 20:35:08 +00001379 int nRow; /* Stop when row count reaches this */
dan165921a2009-08-28 18:53:45 +00001380 int nSub = 0; /* Number of sub-vdbes seen so far */
1381 SubProgram **apSub = 0; /* Array of sub-vdbes */
drh5cfa5842009-12-31 20:35:08 +00001382 Mem *pSub = 0; /* Memory cell hold array of subprogs */
1383 sqlite3 *db = p->db; /* The database connection */
1384 int i; /* Loop counter */
1385 int rc = SQLITE_OK; /* Return code */
drh9734e6e2011-10-07 18:24:25 +00001386 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
drh9a324642003-09-06 20:12:01 +00001387
drh9a324642003-09-06 20:12:01 +00001388 assert( p->explain );
drh5f82e3c2009-07-06 00:44:08 +00001389 assert( p->magic==VDBE_MAGIC_RUN );
danielk19776c359f02008-11-21 16:58:03 +00001390 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +00001391
drh9cbf3422008-01-17 16:22:13 +00001392 /* Even though this opcode does not use dynamic strings for
1393 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +00001394 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +00001395 */
dan165921a2009-08-28 18:53:45 +00001396 releaseMemArray(pMem, 8);
drh9734e6e2011-10-07 18:24:25 +00001397 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +00001398
danielk19776c359f02008-11-21 16:58:03 +00001399 if( p->rc==SQLITE_NOMEM ){
1400 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1401 ** sqlite3_column_text16() failed. */
1402 db->mallocFailed = 1;
1403 return SQLITE_ERROR;
1404 }
1405
drh5cfa5842009-12-31 20:35:08 +00001406 /* When the number of output rows reaches nRow, that means the
1407 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1408 ** nRow is the sum of the number of rows in the main program, plus
1409 ** the sum of the number of rows in all trigger subprograms encountered
1410 ** so far. The nRow value will increase as new trigger subprograms are
1411 ** encountered, but p->pc will eventually catch up to nRow.
1412 */
dan165921a2009-08-28 18:53:45 +00001413 nRow = p->nOp;
1414 if( p->explain==1 ){
drh5cfa5842009-12-31 20:35:08 +00001415 /* The first 8 memory cells are used for the result set. So we will
1416 ** commandeer the 9th cell to use as storage for an array of pointers
1417 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1418 ** cells. */
1419 assert( p->nMem>9 );
dan165921a2009-08-28 18:53:45 +00001420 pSub = &p->aMem[9];
1421 if( pSub->flags&MEM_Blob ){
drh5cfa5842009-12-31 20:35:08 +00001422 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1423 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
dan165921a2009-08-28 18:53:45 +00001424 nSub = pSub->n/sizeof(Vdbe*);
1425 apSub = (SubProgram **)pSub->z;
1426 }
1427 for(i=0; i<nSub; i++){
1428 nRow += apSub[i]->nOp;
1429 }
1430 }
1431
drhecc92422005-09-10 16:46:12 +00001432 do{
1433 i = p->pc++;
dan165921a2009-08-28 18:53:45 +00001434 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1435 if( i>=nRow ){
drh826fb5a2004-02-14 23:59:57 +00001436 p->rc = SQLITE_OK;
1437 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +00001438 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +00001439 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +00001440 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00001441 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +00001442 }else{
drh81316f82013-10-29 20:40:47 +00001443 char *zP4;
dan165921a2009-08-28 18:53:45 +00001444 Op *pOp;
1445 if( i<p->nOp ){
drh5cfa5842009-12-31 20:35:08 +00001446 /* The output line number is small enough that we are still in the
1447 ** main program. */
dan165921a2009-08-28 18:53:45 +00001448 pOp = &p->aOp[i];
1449 }else{
drh5cfa5842009-12-31 20:35:08 +00001450 /* We are currently listing subprograms. Figure out which one and
1451 ** pick up the appropriate opcode. */
dan165921a2009-08-28 18:53:45 +00001452 int j;
1453 i -= p->nOp;
1454 for(j=0; i>=apSub[j]->nOp; j++){
1455 i -= apSub[j]->nOp;
1456 }
1457 pOp = &apSub[j]->aOp[i];
1458 }
danielk19770d78bae2008-01-03 07:09:48 +00001459 if( p->explain==1 ){
1460 pMem->flags = MEM_Int;
danielk19770d78bae2008-01-03 07:09:48 +00001461 pMem->u.i = i; /* Program counter */
1462 pMem++;
1463
1464 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001465 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
danielk19770d78bae2008-01-03 07:09:48 +00001466 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001467 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +00001468 pMem->enc = SQLITE_UTF8;
1469 pMem++;
dan165921a2009-08-28 18:53:45 +00001470
drh5cfa5842009-12-31 20:35:08 +00001471 /* When an OP_Program opcode is encounter (the only opcode that has
1472 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1473 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1474 ** has not already been seen.
1475 */
dan165921a2009-08-28 18:53:45 +00001476 if( pOp->p4type==P4_SUBPROGRAM ){
1477 int nByte = (nSub+1)*sizeof(SubProgram*);
1478 int j;
1479 for(j=0; j<nSub; j++){
1480 if( apSub[j]==pOp->p4.pProgram ) break;
1481 }
dan2b9ee772012-03-31 09:59:44 +00001482 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
dan165921a2009-08-28 18:53:45 +00001483 apSub = (SubProgram **)pSub->z;
1484 apSub[nSub++] = pOp->p4.pProgram;
1485 pSub->flags |= MEM_Blob;
1486 pSub->n = nSub*sizeof(SubProgram*);
1487 }
1488 }
danielk19770d78bae2008-01-03 07:09:48 +00001489 }
drheb2e1762004-05-27 01:53:56 +00001490
1491 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001492 pMem->u.i = pOp->p1; /* P1 */
drheb2e1762004-05-27 01:53:56 +00001493 pMem++;
1494
1495 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +00001496 pMem->u.i = pOp->p2; /* P2 */
drheb2e1762004-05-27 01:53:56 +00001497 pMem++;
1498
dan2ce22452010-11-08 19:01:16 +00001499 pMem->flags = MEM_Int;
1500 pMem->u.i = pOp->p3; /* P3 */
dan2ce22452010-11-08 19:01:16 +00001501 pMem++;
danielk19770d78bae2008-01-03 07:09:48 +00001502
drh322f2852014-09-19 00:43:39 +00001503 if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +00001504 assert( p->db->mallocFailed );
1505 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001506 }
drhc91b2fd2014-03-01 18:13:23 +00001507 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001508 zP4 = displayP4(pOp, pMem->z, 32);
1509 if( zP4!=pMem->z ){
1510 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
danielk1977a7a8e142008-02-13 18:25:27 +00001511 }else{
1512 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +00001513 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +00001514 pMem->enc = SQLITE_UTF8;
1515 }
danielk19770d78bae2008-01-03 07:09:48 +00001516 pMem++;
drheb2e1762004-05-27 01:53:56 +00001517
danielk19770d78bae2008-01-03 07:09:48 +00001518 if( p->explain==1 ){
drh322f2852014-09-19 00:43:39 +00001519 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
danielk1977357864e2009-03-25 15:43:08 +00001520 assert( p->db->mallocFailed );
1521 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +00001522 }
drhc91b2fd2014-03-01 18:13:23 +00001523 pMem->flags = MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +00001524 pMem->n = 2;
1525 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +00001526 pMem->enc = SQLITE_UTF8;
1527 pMem++;
1528
drhc7379ce2013-10-30 02:28:23 +00001529#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drh322f2852014-09-19 00:43:39 +00001530 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
drh81316f82013-10-29 20:40:47 +00001531 assert( p->db->mallocFailed );
1532 return SQLITE_ERROR;
drh52391cb2008-02-14 23:44:13 +00001533 }
drhc91b2fd2014-03-01 18:13:23 +00001534 pMem->flags = MEM_Str|MEM_Term;
drh81316f82013-10-29 20:40:47 +00001535 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
drh81316f82013-10-29 20:40:47 +00001536 pMem->enc = SQLITE_UTF8;
1537#else
1538 pMem->flags = MEM_Null; /* Comment */
drh81316f82013-10-29 20:40:47 +00001539#endif
danielk19770d78bae2008-01-03 07:09:48 +00001540 }
1541
dan2ce22452010-11-08 19:01:16 +00001542 p->nResColumn = 8 - 4*(p->explain-1);
drh9734e6e2011-10-07 18:24:25 +00001543 p->pResultSet = &p->aMem[1];
drh826fb5a2004-02-14 23:59:57 +00001544 p->rc = SQLITE_OK;
1545 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +00001546 }
drh826fb5a2004-02-14 23:59:57 +00001547 return rc;
drh9a324642003-09-06 20:12:01 +00001548}
drhb7f91642004-10-31 02:22:47 +00001549#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +00001550
drh7c4ac0c2007-04-05 11:25:58 +00001551#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +00001552/*
drh3f7d4e42004-07-24 14:35:58 +00001553** Print the SQL that was used to generate a VDBE program.
1554*/
1555void sqlite3VdbePrintSql(Vdbe *p){
drh84e55a82013-11-13 17:58:23 +00001556 const char *z = 0;
1557 if( p->zSql ){
1558 z = p->zSql;
1559 }else if( p->nOp>=1 ){
1560 const VdbeOp *pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001561 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh84e55a82013-11-13 17:58:23 +00001562 z = pOp->p4.z;
1563 while( sqlite3Isspace(*z) ) z++;
1564 }
drh3f7d4e42004-07-24 14:35:58 +00001565 }
drh84e55a82013-11-13 17:58:23 +00001566 if( z ) printf("SQL: [%s]\n", z);
drh3f7d4e42004-07-24 14:35:58 +00001567}
drh7c4ac0c2007-04-05 11:25:58 +00001568#endif
drh3f7d4e42004-07-24 14:35:58 +00001569
drh602c2372007-03-01 00:29:13 +00001570#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1571/*
1572** Print an IOTRACE message showing SQL content.
1573*/
1574void sqlite3VdbeIOTraceSql(Vdbe *p){
1575 int nOp = p->nOp;
1576 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +00001577 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +00001578 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +00001579 pOp = &p->aOp[0];
drhaceb31b2014-02-08 01:40:27 +00001580 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +00001581 int i, j;
drh00a18e42007-08-13 11:10:34 +00001582 char z[1000];
drh949f9cd2008-01-12 21:35:57 +00001583 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001584 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001585 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001586 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001587 if( z[i-1]!=' ' ){
1588 z[j++] = ' ';
1589 }
1590 }else{
1591 z[j++] = z[i];
1592 }
1593 }
1594 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001595 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001596 }
1597}
1598#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1599
drhb2771ce2009-02-20 01:28:59 +00001600/*
drh4800b2e2009-12-08 15:35:22 +00001601** Allocate space from a fixed size buffer and return a pointer to
1602** that space. If insufficient space is available, return NULL.
1603**
1604** The pBuf parameter is the initial value of a pointer which will
1605** receive the new memory. pBuf is normally NULL. If pBuf is not
1606** NULL, it means that memory space has already been allocated and that
1607** this routine should not allocate any new memory. When pBuf is not
1608** NULL simply return pBuf. Only allocate new memory space when pBuf
1609** is NULL.
drhb2771ce2009-02-20 01:28:59 +00001610**
1611** nByte is the number of bytes of space needed.
1612**
drh19875c82009-12-08 19:58:19 +00001613** *ppFrom points to available space and pEnd points to the end of the
1614** available space. When space is allocated, *ppFrom is advanced past
1615** the end of the allocated space.
drhb2771ce2009-02-20 01:28:59 +00001616**
1617** *pnByte is a counter of the number of bytes of space that have failed
1618** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001619** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001620*/
drh4800b2e2009-12-08 15:35:22 +00001621static void *allocSpace(
1622 void *pBuf, /* Where return pointer will be stored */
drhb2771ce2009-02-20 01:28:59 +00001623 int nByte, /* Number of bytes to allocate */
1624 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001625 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001626 int *pnByte /* If allocation cannot be made, increment *pnByte */
1627){
drhea598cb2009-04-05 12:22:08 +00001628 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drh4800b2e2009-12-08 15:35:22 +00001629 if( pBuf ) return pBuf;
1630 nByte = ROUND8(nByte);
1631 if( &(*ppFrom)[nByte] <= pEnd ){
1632 pBuf = (void*)*ppFrom;
1633 *ppFrom += nByte;
1634 }else{
1635 *pnByte += nByte;
drhb2771ce2009-02-20 01:28:59 +00001636 }
drh4800b2e2009-12-08 15:35:22 +00001637 return pBuf;
drhb2771ce2009-02-20 01:28:59 +00001638}
drh602c2372007-03-01 00:29:13 +00001639
drh3f7d4e42004-07-24 14:35:58 +00001640/*
drh124c0b42011-06-01 18:15:55 +00001641** Rewind the VDBE back to the beginning in preparation for
1642** running it.
drh9a324642003-09-06 20:12:01 +00001643*/
drh124c0b42011-06-01 18:15:55 +00001644void sqlite3VdbeRewind(Vdbe *p){
1645#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1646 int i;
1647#endif
drh9a324642003-09-06 20:12:01 +00001648 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001649 assert( p->magic==VDBE_MAGIC_INIT );
1650
drhc16a03b2004-09-15 13:38:10 +00001651 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001652 */
drhc16a03b2004-09-15 13:38:10 +00001653 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001654
danielk197700e13612008-11-17 19:18:54 +00001655 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001656 p->magic = VDBE_MAGIC_RUN;
1657
drh124c0b42011-06-01 18:15:55 +00001658#ifdef SQLITE_DEBUG
1659 for(i=1; i<p->nMem; i++){
1660 assert( p->aMem[i].db==p->db );
1661 }
1662#endif
1663 p->pc = -1;
1664 p->rc = SQLITE_OK;
1665 p->errorAction = OE_Abort;
1666 p->magic = VDBE_MAGIC_RUN;
1667 p->nChange = 0;
1668 p->cacheCtr = 1;
1669 p->minWriteFileFormat = 255;
1670 p->iStatement = 0;
1671 p->nFkConstraint = 0;
1672#ifdef VDBE_PROFILE
1673 for(i=0; i<p->nOp; i++){
1674 p->aOp[i].cnt = 0;
1675 p->aOp[i].cycles = 0;
1676 }
1677#endif
1678}
1679
1680/*
1681** Prepare a virtual machine for execution for the first time after
1682** creating the virtual machine. This involves things such
drh7abda852014-09-19 16:02:06 +00001683** as allocating registers and initializing the program counter.
drh124c0b42011-06-01 18:15:55 +00001684** After the VDBE has be prepped, it can be executed by one or more
1685** calls to sqlite3VdbeExec().
1686**
peter.d.reid60ec9142014-09-06 16:39:46 +00001687** This function may be called exactly once on each virtual machine.
drh124c0b42011-06-01 18:15:55 +00001688** After this routine is called the VM has been "packaged" and is ready
peter.d.reid60ec9142014-09-06 16:39:46 +00001689** to run. After this routine is called, further calls to
drh124c0b42011-06-01 18:15:55 +00001690** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1691** the Vdbe from the Parse object that helped generate it so that the
1692** the Vdbe becomes an independent entity and the Parse object can be
1693** destroyed.
1694**
1695** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1696** to its initial state after it has been run.
1697*/
1698void sqlite3VdbeMakeReady(
1699 Vdbe *p, /* The VDBE */
1700 Parse *pParse /* Parsing context */
1701){
1702 sqlite3 *db; /* The database connection */
1703 int nVar; /* Number of parameters */
1704 int nMem; /* Number of VM memory registers */
1705 int nCursor; /* Number of cursors required */
1706 int nArg; /* Number of arguments in subprograms */
dan1d8cb212011-12-09 13:24:16 +00001707 int nOnce; /* Number of OP_Once instructions */
drh124c0b42011-06-01 18:15:55 +00001708 int n; /* Loop counter */
1709 u8 *zCsr; /* Memory available for allocation */
1710 u8 *zEnd; /* First byte past allocated memory */
1711 int nByte; /* How much extra memory is needed */
1712
1713 assert( p!=0 );
1714 assert( p->nOp>0 );
1715 assert( pParse!=0 );
1716 assert( p->magic==VDBE_MAGIC_INIT );
drh73d5b8f2013-12-23 19:09:07 +00001717 assert( pParse==p->pParse );
drh124c0b42011-06-01 18:15:55 +00001718 db = p->db;
1719 assert( db->mallocFailed==0 );
1720 nVar = pParse->nVar;
1721 nMem = pParse->nMem;
1722 nCursor = pParse->nTab;
1723 nArg = pParse->nMaxArg;
dan1d8cb212011-12-09 13:24:16 +00001724 nOnce = pParse->nOnce;
drh20e226d2012-01-01 13:58:53 +00001725 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
drh124c0b42011-06-01 18:15:55 +00001726
danielk1977cd3e8f72008-03-25 09:47:35 +00001727 /* For each cursor required, also allocate a memory cell. Memory
1728 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1729 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001730 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001731 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1732 ** stores the blob of memory associated with cursor 1, etc.
1733 **
1734 ** See also: allocateCursor().
1735 */
1736 nMem += nCursor;
1737
danielk19776ab3a2e2009-02-19 14:39:25 +00001738 /* Allocate space for memory registers, SQL variables, VDBE cursors and
drh124c0b42011-06-01 18:15:55 +00001739 ** an array to marshal SQL function arguments in.
drh9a324642003-09-06 20:12:01 +00001740 */
drh73d5b8f2013-12-23 19:09:07 +00001741 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
1742 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */
drh19875c82009-12-08 19:58:19 +00001743
drh124c0b42011-06-01 18:15:55 +00001744 resolveP2Values(p, &nArg);
1745 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1746 if( pParse->explain && nMem<10 ){
1747 nMem = 10;
1748 }
1749 memset(zCsr, 0, zEnd-zCsr);
1750 zCsr += (zCsr - (u8*)0)&7;
1751 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drhaab910c2011-06-27 00:01:22 +00001752 p->expired = 0;
drh124c0b42011-06-01 18:15:55 +00001753
1754 /* Memory for registers, parameters, cursor, etc, is allocated in two
1755 ** passes. On the first pass, we try to reuse unused space at the
1756 ** end of the opcode array. If we are unable to satisfy all memory
1757 ** requirements by reusing the opcode array tail, then the second
1758 ** pass will fill in the rest using a fresh allocation.
1759 **
1760 ** This two-pass approach that reuses as much memory as possible from
1761 ** the leftover space at the end of the opcode array can significantly
1762 ** reduce the amount of memory held by a prepared statement.
1763 */
1764 do {
1765 nByte = 0;
1766 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1767 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1768 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1769 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1770 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1771 &zCsr, zEnd, &nByte);
drhb8475df2011-12-09 16:21:19 +00001772 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
dane2f771b2014-11-03 15:33:17 +00001773#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00001774 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
dane2f771b2014-11-03 15:33:17 +00001775#endif
drh124c0b42011-06-01 18:15:55 +00001776 if( nByte ){
1777 p->pFree = sqlite3DbMallocZero(db, nByte);
drh0f7eb612006-08-08 13:51:43 +00001778 }
drh124c0b42011-06-01 18:15:55 +00001779 zCsr = p->pFree;
1780 zEnd = &zCsr[nByte];
1781 }while( nByte && !db->mallocFailed );
drhb2771ce2009-02-20 01:28:59 +00001782
drhd2a56232013-01-28 19:00:20 +00001783 p->nCursor = nCursor;
dan1d8cb212011-12-09 13:24:16 +00001784 p->nOnceFlag = nOnce;
drh124c0b42011-06-01 18:15:55 +00001785 if( p->aVar ){
1786 p->nVar = (ynVar)nVar;
1787 for(n=0; n<nVar; n++){
1788 p->aVar[n].flags = MEM_Null;
1789 p->aVar[n].db = db;
danielk197754db47e2004-05-19 10:36:43 +00001790 }
drh82a48512003-09-06 22:45:20 +00001791 }
drh9b5444a2014-12-02 13:46:53 +00001792 if( p->azVar && pParse->nzVar>0 ){
drh124c0b42011-06-01 18:15:55 +00001793 p->nzVar = pParse->nzVar;
1794 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1795 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
danielk1977b3bce662005-01-29 08:32:43 +00001796 }
drh124c0b42011-06-01 18:15:55 +00001797 if( p->aMem ){
1798 p->aMem--; /* aMem[] goes from 1..nMem */
1799 p->nMem = nMem; /* not from 0..nMem-1 */
1800 for(n=1; n<=nMem; n++){
drha5750cf2014-02-07 13:20:31 +00001801 p->aMem[n].flags = MEM_Undefined;
drh124c0b42011-06-01 18:15:55 +00001802 p->aMem[n].db = db;
drhcf64d8b2003-12-31 17:57:10 +00001803 }
drh9a324642003-09-06 20:12:01 +00001804 }
drh124c0b42011-06-01 18:15:55 +00001805 p->explain = pParse->explain;
1806 sqlite3VdbeRewind(p);
drh9a324642003-09-06 20:12:01 +00001807}
1808
drh9a324642003-09-06 20:12:01 +00001809/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001810** Close a VDBE cursor and release all the resources that cursor
1811** happens to hold.
drh9a324642003-09-06 20:12:01 +00001812*/
drhdfe88ec2008-11-03 20:55:06 +00001813void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001814 if( pCx==0 ){
1815 return;
1816 }
dana20fde62011-07-12 14:28:05 +00001817 sqlite3VdbeSorterClose(p->db, pCx);
drh9a324642003-09-06 20:12:01 +00001818 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001819 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001820 /* The pCx->pCursor will be close automatically, if it exists, by
1821 ** the call above. */
1822 }else if( pCx->pCursor ){
1823 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001824 }
drh9eff6162006-06-12 21:59:13 +00001825#ifndef SQLITE_OMIT_VIRTUALTABLE
drhf526dca2014-10-13 17:42:05 +00001826 else if( pCx->pVtabCursor ){
drh9eff6162006-06-12 21:59:13 +00001827 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
drh5cc10232013-11-21 01:04:02 +00001828 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
drha68d6282015-03-24 13:32:53 +00001829 assert( pVtabCursor->pVtab->nRef>0 );
1830 pVtabCursor->pVtab->nRef--;
drh9eff6162006-06-12 21:59:13 +00001831 pModule->xClose(pVtabCursor);
1832 }
1833#endif
drh9a324642003-09-06 20:12:01 +00001834}
1835
dan65a7cd12009-09-01 12:16:01 +00001836/*
drhab4e7f32015-04-16 18:11:50 +00001837** Close all cursors in the current frame.
1838*/
1839static void closeCursorsInFrame(Vdbe *p){
1840 if( p->apCsr ){
1841 int i;
1842 for(i=0; i<p->nCursor; i++){
1843 VdbeCursor *pC = p->apCsr[i];
1844 if( pC ){
1845 sqlite3VdbeFreeCursor(p, pC);
1846 p->apCsr[i] = 0;
1847 }
1848 }
1849 }
1850}
1851
1852/*
dan65a7cd12009-09-01 12:16:01 +00001853** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1854** is used, for example, when a trigger sub-program is halted to restore
1855** control to the main program.
1856*/
dan165921a2009-08-28 18:53:45 +00001857int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1858 Vdbe *v = pFrame->v;
drhab4e7f32015-04-16 18:11:50 +00001859 closeCursorsInFrame(v);
dane2f771b2014-11-03 15:33:17 +00001860#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00001861 v->anExec = pFrame->anExec;
dane2f771b2014-11-03 15:33:17 +00001862#endif
dan1d8cb212011-12-09 13:24:16 +00001863 v->aOnceFlag = pFrame->aOnceFlag;
1864 v->nOnceFlag = pFrame->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00001865 v->aOp = pFrame->aOp;
1866 v->nOp = pFrame->nOp;
1867 v->aMem = pFrame->aMem;
1868 v->nMem = pFrame->nMem;
1869 v->apCsr = pFrame->apCsr;
1870 v->nCursor = pFrame->nCursor;
dan76d462e2009-08-30 11:42:51 +00001871 v->db->lastRowid = pFrame->lastRowid;
1872 v->nChange = pFrame->nChange;
danc3da6672014-10-28 18:24:16 +00001873 v->db->nChange = pFrame->nDbChange;
dan165921a2009-08-28 18:53:45 +00001874 return pFrame->pc;
1875}
1876
drh9a324642003-09-06 20:12:01 +00001877/*
drh5f82e3c2009-07-06 00:44:08 +00001878** Close all cursors.
dan165921a2009-08-28 18:53:45 +00001879**
1880** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1881** cell array. This is necessary as the memory cell array may contain
1882** pointers to VdbeFrame objects, which may in turn contain pointers to
1883** open cursors.
drh9a324642003-09-06 20:12:01 +00001884*/
drh5f82e3c2009-07-06 00:44:08 +00001885static void closeAllCursors(Vdbe *p){
dan165921a2009-08-28 18:53:45 +00001886 if( p->pFrame ){
drh23272752011-03-06 21:54:33 +00001887 VdbeFrame *pFrame;
dan165921a2009-08-28 18:53:45 +00001888 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1889 sqlite3VdbeFrameRestore(pFrame);
drhf526dca2014-10-13 17:42:05 +00001890 p->pFrame = 0;
1891 p->nFrame = 0;
dan165921a2009-08-28 18:53:45 +00001892 }
drhf526dca2014-10-13 17:42:05 +00001893 assert( p->nFrame==0 );
drhab4e7f32015-04-16 18:11:50 +00001894 closeCursorsInFrame(p);
dan523a0872009-08-31 05:23:32 +00001895 if( p->aMem ){
1896 releaseMemArray(&p->aMem[1], p->nMem);
1897 }
dan27106572010-12-01 08:04:47 +00001898 while( p->pDelFrame ){
1899 VdbeFrame *pDel = p->pDelFrame;
1900 p->pDelFrame = pDel->pParent;
1901 sqlite3VdbeFrameDelete(pDel);
1902 }
dan0c547792013-07-18 17:12:08 +00001903
1904 /* Delete any auxdata allocations made by the VM */
drhf526dca2014-10-13 17:42:05 +00001905 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
dan0c547792013-07-18 17:12:08 +00001906 assert( p->pAuxData==0 );
drh9a324642003-09-06 20:12:01 +00001907}
1908
1909/*
drh7abda852014-09-19 16:02:06 +00001910** Clean up the VM after a single run.
drh9a324642003-09-06 20:12:01 +00001911*/
drhc890fec2008-08-01 20:10:08 +00001912static void Cleanup(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00001913 sqlite3 *db = p->db;
dan165921a2009-08-28 18:53:45 +00001914
1915#ifdef SQLITE_DEBUG
1916 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1917 ** Vdbe.aMem[] arrays have already been cleaned up. */
1918 int i;
drhb8475df2011-12-09 16:21:19 +00001919 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1920 if( p->aMem ){
drha5750cf2014-02-07 13:20:31 +00001921 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
drhb8475df2011-12-09 16:21:19 +00001922 }
dan165921a2009-08-28 18:53:45 +00001923#endif
1924
drh633e6d52008-07-28 19:34:53 +00001925 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001926 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001927 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001928}
1929
1930/*
danielk197722322fd2004-05-25 23:35:17 +00001931** Set the number of result columns that will be returned by this SQL
1932** statement. This is now set at compile time, rather than during
1933** execution of the vdbe program so that sqlite3_column_count() can
1934** be called on an SQL statement before sqlite3_step().
1935*/
1936void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001937 Mem *pColName;
1938 int n;
drh633e6d52008-07-28 19:34:53 +00001939 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001940
drhc890fec2008-08-01 20:10:08 +00001941 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001942 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001943 n = nResColumn*COLNAME_N;
shane36840fd2009-06-26 16:32:13 +00001944 p->nResColumn = (u16)nResColumn;
drh633e6d52008-07-28 19:34:53 +00001945 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001946 if( p->aColName==0 ) return;
1947 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001948 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001949 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001950 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001951 }
danielk197722322fd2004-05-25 23:35:17 +00001952}
1953
1954/*
danielk19773cf86062004-05-26 10:11:05 +00001955** Set the name of the idx'th column to be returned by the SQL statement.
1956** zName must be a pointer to a nul terminated string.
1957**
1958** This call must be made after a call to sqlite3VdbeSetNumCols().
1959**
danielk197710fb7492008-10-31 10:53:22 +00001960** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1961** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1962** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001963*/
danielk197710fb7492008-10-31 10:53:22 +00001964int sqlite3VdbeSetColName(
1965 Vdbe *p, /* Vdbe being configured */
1966 int idx, /* Index of column zName applies to */
1967 int var, /* One of the COLNAME_* constants */
1968 const char *zName, /* Pointer to buffer containing name */
1969 void (*xDel)(void*) /* Memory management strategy for zName */
1970){
danielk19773cf86062004-05-26 10:11:05 +00001971 int rc;
1972 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001973 assert( idx<p->nResColumn );
1974 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001975 if( p->db->mallocFailed ){
1976 assert( !zName || xDel!=SQLITE_DYNAMIC );
1977 return SQLITE_NOMEM;
1978 }
drh76ff3a02004-09-24 22:32:30 +00001979 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001980 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001981 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001982 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001983 return rc;
1984}
1985
danielk197713adf8a2004-06-03 16:08:41 +00001986/*
1987** A read or write transaction may or may not be active on database handle
1988** db. If a transaction is active, commit it. If there is a
1989** write-transaction spanning more than one database file, this routine
1990** takes care of the master journal trickery.
1991*/
danielk19773e3a84d2008-08-01 17:37:40 +00001992static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001993 int i;
1994 int nTrans = 0; /* Number of databases with an active write-transaction */
1995 int rc = SQLITE_OK;
1996 int needXcommit = 0;
1997
shane36840fd2009-06-26 16:32:13 +00001998#ifdef SQLITE_OMIT_VIRTUALTABLE
1999 /* With this option, sqlite3VtabSync() is defined to be simply
2000 ** SQLITE_OK so p is not used.
2001 */
2002 UNUSED_PARAMETER(p);
2003#endif
2004
danielk19775bd270b2006-07-25 15:14:52 +00002005 /* Before doing anything else, call the xSync() callback for any
2006 ** virtual module tables written in this transaction. This has to
2007 ** be done before determining whether a master journal file is
2008 ** required, as an xSync() callback may add an attached database
2009 ** to the transaction.
2010 */
dan016f7812013-08-21 17:35:48 +00002011 rc = sqlite3VtabSync(db, p);
danielk19775bd270b2006-07-25 15:14:52 +00002012
2013 /* This loop determines (a) if the commit hook should be invoked and
2014 ** (b) how many database files have open write transactions, not
2015 ** including the temp database. (b) is important because if more than
2016 ** one database file has an open write transaction, a master journal
2017 ** file is required for an atomic commit.
2018 */
drhabfb62f2010-07-30 11:20:35 +00002019 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002020 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002021 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00002022 needXcommit = 1;
2023 if( i!=1 ) nTrans++;
dan6b9bb592012-10-05 19:43:02 +00002024 sqlite3BtreeEnter(pBt);
drhabfb62f2010-07-30 11:20:35 +00002025 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
dan6b9bb592012-10-05 19:43:02 +00002026 sqlite3BtreeLeave(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00002027 }
2028 }
drhabfb62f2010-07-30 11:20:35 +00002029 if( rc!=SQLITE_OK ){
2030 return rc;
2031 }
danielk197713adf8a2004-06-03 16:08:41 +00002032
2033 /* If there are any write-transactions at all, invoke the commit hook */
2034 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00002035 rc = db->xCommitCallback(db->pCommitArg);
drh92f02c32004-09-02 14:57:08 +00002036 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002037 return SQLITE_CONSTRAINT_COMMITHOOK;
danielk197713adf8a2004-06-03 16:08:41 +00002038 }
2039 }
2040
danielk197740b38dc2004-06-26 08:38:24 +00002041 /* The simple case - no more than one database file (not counting the
2042 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00002043 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00002044 **
danielk197740b38dc2004-06-26 08:38:24 +00002045 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00002046 ** string, it means the main database is :memory: or a temp file. In
2047 ** that case we do not support atomic multi-file commits, so use the
2048 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00002049 */
drhea678832008-12-10 19:26:22 +00002050 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
2051 || nTrans<=1
2052 ){
danielk197704103022009-02-03 16:51:24 +00002053 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002054 Btree *pBt = db->aDb[i].pBt;
2055 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002056 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00002057 }
2058 }
2059
drh80e35f42007-03-30 14:06:34 +00002060 /* Do the commit only if all databases successfully complete phase 1.
2061 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
2062 ** IO error while deleting or truncating a journal file. It is unlikely,
2063 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00002064 */
2065 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2066 Btree *pBt = db->aDb[i].pBt;
2067 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002068 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
danielk197713adf8a2004-06-03 16:08:41 +00002069 }
danielk1977979f38e2007-03-27 16:19:51 +00002070 }
2071 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00002072 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002073 }
2074 }
2075
2076 /* The complex case - There is a multi-file write-transaction active.
2077 ** This requires a master journal file to ensure the transaction is
peter.d.reid60ec9142014-09-06 16:39:46 +00002078 ** committed atomically.
danielk197713adf8a2004-06-03 16:08:41 +00002079 */
danielk197744ee5bf2005-05-27 09:41:12 +00002080#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00002081 else{
danielk1977b4b47412007-08-17 15:53:36 +00002082 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00002083 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00002084 char *zMaster = 0; /* File-name for the master journal */
2085 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00002086 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00002087 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00002088 int res;
drhf5808602011-12-16 00:33:04 +00002089 int retryCount = 0;
drh5c531a42011-12-16 01:21:31 +00002090 int nMainFile;
danielk197713adf8a2004-06-03 16:08:41 +00002091
2092 /* Select a master journal file name */
drh5c531a42011-12-16 01:21:31 +00002093 nMainFile = sqlite3Strlen30(zMainFile);
drh52bcde02012-01-03 14:50:45 +00002094 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
drh5c531a42011-12-16 01:21:31 +00002095 if( zMaster==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00002096 do {
drhdc5ea5c2008-12-10 17:19:59 +00002097 u32 iRandom;
drh84968c02011-12-16 15:11:39 +00002098 if( retryCount ){
2099 if( retryCount>100 ){
2100 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2101 sqlite3OsDelete(pVfs, zMaster, 0);
2102 break;
2103 }else if( retryCount==1 ){
2104 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2105 }
danielk197713adf8a2004-06-03 16:08:41 +00002106 }
drh84968c02011-12-16 15:11:39 +00002107 retryCount++;
danielk197713adf8a2004-06-03 16:08:41 +00002108 sqlite3_randomness(sizeof(iRandom), &iRandom);
drh5c531a42011-12-16 01:21:31 +00002109 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
drhf5808602011-12-16 00:33:04 +00002110 (iRandom>>8)&0xffffff, iRandom&0xff);
drhf5808602011-12-16 00:33:04 +00002111 /* The antipenultimate character of the master journal name must
2112 ** be "9" to avoid name collisions when using 8+3 filenames. */
drh5c531a42011-12-16 01:21:31 +00002113 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
drh81cc5162011-05-17 20:36:21 +00002114 sqlite3FileSuffix3(zMainFile, zMaster);
danielk1977861f7452008-06-05 11:39:11 +00002115 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2116 }while( rc==SQLITE_OK && res );
2117 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00002118 /* Open the master journal. */
2119 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2120 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2121 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2122 );
2123 }
danielk197713adf8a2004-06-03 16:08:41 +00002124 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002125 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002126 return rc;
2127 }
2128
2129 /* Write the name of each database file in the transaction into the new
2130 ** master journal file. If an error occurs at this point close
2131 ** and delete the master journal file. All the individual journal files
2132 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00002133 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00002134 */
danielk19771e536952007-08-16 10:09:01 +00002135 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002136 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002137 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00002138 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drh8c96a6e2010-08-31 01:09:15 +00002139 if( zFile==0 ){
drhb290e1c2009-12-08 13:36:55 +00002140 continue; /* Ignore TEMP and :memory: databases */
2141 }
drh8c96a6e2010-08-31 01:09:15 +00002142 assert( zFile[0]!=0 );
drh2c8997b2005-08-27 16:36:48 +00002143 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2144 needSync = 1;
2145 }
drhea678832008-12-10 19:26:22 +00002146 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2147 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00002148 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00002149 sqlite3OsCloseFree(pMaster);
2150 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002151 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002152 return rc;
2153 }
2154 }
2155 }
2156
danielk19779663b8f2007-08-24 11:52:28 +00002157 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2158 ** flag is set this is not required.
2159 */
danielk1977bea2a942009-01-20 17:06:27 +00002160 if( needSync
2161 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2162 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2163 ){
danielk1977fee2d252007-08-18 10:59:19 +00002164 sqlite3OsCloseFree(pMaster);
2165 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00002166 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00002167 return rc;
2168 }
drhc9e06862004-06-09 20:03:08 +00002169
danielk197713adf8a2004-06-03 16:08:41 +00002170 /* Sync all the db files involved in the transaction. The same call
2171 ** sets the master journal pointer in each individual journal. If
2172 ** an error occurs here, do not delete the master journal file.
2173 **
drh80e35f42007-03-30 14:06:34 +00002174 ** If the error occurs during the first call to
2175 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2176 ** master journal file will be orphaned. But we cannot delete it,
2177 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00002178 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00002179 */
danielk19775bd270b2006-07-25 15:14:52 +00002180 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00002181 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00002182 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00002183 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00002184 }
2185 }
danielk1977fee2d252007-08-18 10:59:19 +00002186 sqlite3OsCloseFree(pMaster);
drhabfb62f2010-07-30 11:20:35 +00002187 assert( rc!=SQLITE_BUSY );
danielk19775bd270b2006-07-25 15:14:52 +00002188 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00002189 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00002190 return rc;
2191 }
danielk197713adf8a2004-06-03 16:08:41 +00002192
danielk1977962398d2004-06-14 09:35:16 +00002193 /* Delete the master journal file. This commits the transaction. After
2194 ** doing this the directory is synced again before any individual
2195 ** transaction files are deleted.
2196 */
drh75a4d7c2015-03-16 16:44:55 +00002197 rc = sqlite3OsDelete(pVfs, zMaster, needSync);
drh633e6d52008-07-28 19:34:53 +00002198 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00002199 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00002200 if( rc ){
2201 return rc;
2202 }
danielk197713adf8a2004-06-03 16:08:41 +00002203
2204 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00002205 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2206 ** deleting or truncating journals. If something goes wrong while
2207 ** this is happening we don't really care. The integrity of the
2208 ** transaction is already guaranteed, but some stray 'cold' journals
2209 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00002210 */
danielk1977979f38e2007-03-27 16:19:51 +00002211 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00002212 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00002213 for(i=0; i<db->nDb; i++){
2214 Btree *pBt = db->aDb[i].pBt;
2215 if( pBt ){
dan60939d02011-03-29 15:40:55 +00002216 sqlite3BtreeCommitPhaseTwo(pBt, 1);
danielk197713adf8a2004-06-03 16:08:41 +00002217 }
2218 }
danielk19772d1d86f2008-06-20 14:59:51 +00002219 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00002220 enable_simulated_io_errors();
2221
danielk1977f9e7dda2006-06-16 16:08:53 +00002222 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00002223 }
danielk197744ee5bf2005-05-27 09:41:12 +00002224#endif
danielk1977026d2702004-06-14 13:14:59 +00002225
drh2ac3ee92004-06-07 16:27:46 +00002226 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00002227}
2228
danielk19771d850a72004-05-31 08:26:49 +00002229/*
drh4f7d3a52013-06-27 23:54:02 +00002230** This routine checks that the sqlite3.nVdbeActive count variable
danielk19771d850a72004-05-31 08:26:49 +00002231** matches the number of vdbe's in the list sqlite3.pVdbe that are
2232** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00002233** This is an internal self-check only - it is not an essential processing
2234** step.
danielk19771d850a72004-05-31 08:26:49 +00002235**
2236** This is a no-op if NDEBUG is defined.
2237*/
2238#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00002239static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00002240 Vdbe *p;
2241 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00002242 int nWrite = 0;
drh4f7d3a52013-06-27 23:54:02 +00002243 int nRead = 0;
danielk19771d850a72004-05-31 08:26:49 +00002244 p = db->pVdbe;
2245 while( p ){
dan857745c2014-07-19 17:57:10 +00002246 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
danielk19771d850a72004-05-31 08:26:49 +00002247 cnt++;
drhad4a4b82008-11-05 16:37:34 +00002248 if( p->readOnly==0 ) nWrite++;
drh1713afb2013-06-28 01:24:57 +00002249 if( p->bIsReader ) nRead++;
danielk19771d850a72004-05-31 08:26:49 +00002250 }
2251 p = p->pNext;
2252 }
drh4f7d3a52013-06-27 23:54:02 +00002253 assert( cnt==db->nVdbeActive );
2254 assert( nWrite==db->nVdbeWrite );
2255 assert( nRead==db->nVdbeRead );
danielk19771d850a72004-05-31 08:26:49 +00002256}
2257#else
2258#define checkActiveVdbeCnt(x)
2259#endif
2260
danielk19773cf86062004-05-26 10:11:05 +00002261/*
danielk1977bd434552009-03-18 10:33:00 +00002262** If the Vdbe passed as the first argument opened a statement-transaction,
2263** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2264** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2265** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
drhf7b54962013-05-28 12:11:54 +00002266** statement transaction is committed.
danielk1977bd434552009-03-18 10:33:00 +00002267**
2268** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2269** Otherwise SQLITE_OK.
2270*/
2271int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00002272 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00002273 int rc = SQLITE_OK;
danielk1977ecaecf92009-07-08 08:05:35 +00002274
danielk1977e4948172009-07-17 17:25:43 +00002275 /* If p->iStatement is greater than zero, then this Vdbe opened a
2276 ** statement transaction that should be closed here. The only exception
mistachkin48864df2013-03-21 21:20:32 +00002277 ** is that an IO error may have occurred, causing an emergency rollback.
danielk1977e4948172009-07-17 17:25:43 +00002278 ** In this case (db->nStatement==0), and there is nothing to do.
2279 */
2280 if( db->nStatement && p->iStatement ){
danielk1977bd434552009-03-18 10:33:00 +00002281 int i;
2282 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00002283
2284 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2285 assert( db->nStatement>0 );
2286 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2287
2288 for(i=0; i<db->nDb; i++){
2289 int rc2 = SQLITE_OK;
2290 Btree *pBt = db->aDb[i].pBt;
2291 if( pBt ){
2292 if( eOp==SAVEPOINT_ROLLBACK ){
2293 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2294 }
2295 if( rc2==SQLITE_OK ){
2296 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2297 }
2298 if( rc==SQLITE_OK ){
2299 rc = rc2;
2300 }
2301 }
2302 }
2303 db->nStatement--;
2304 p->iStatement = 0;
dan1da40a32009-09-19 17:00:31 +00002305
dana311b802011-04-26 19:21:34 +00002306 if( rc==SQLITE_OK ){
2307 if( eOp==SAVEPOINT_ROLLBACK ){
2308 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2309 }
2310 if( rc==SQLITE_OK ){
2311 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2312 }
2313 }
2314
dan1da40a32009-09-19 17:00:31 +00002315 /* If the statement transaction is being rolled back, also restore the
2316 ** database handles deferred constraint counter to the value it had when
2317 ** the statement transaction was opened. */
2318 if( eOp==SAVEPOINT_ROLLBACK ){
2319 db->nDeferredCons = p->nStmtDefCons;
drh648e2642013-07-11 15:03:32 +00002320 db->nDeferredImmCons = p->nStmtDefImmCons;
dan1da40a32009-09-19 17:00:31 +00002321 }
danielk1977bd434552009-03-18 10:33:00 +00002322 }
2323 return rc;
2324}
2325
2326/*
dan1da40a32009-09-19 17:00:31 +00002327** This function is called when a transaction opened by the database
2328** handle associated with the VM passed as an argument is about to be
2329** committed. If there are outstanding deferred foreign key constraint
2330** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2331**
2332** If there are outstanding FK violations and this function returns
drhd91c1a12013-02-09 13:58:25 +00002333** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2334** and write an error message to it. Then return SQLITE_ERROR.
dan1da40a32009-09-19 17:00:31 +00002335*/
2336#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00002337int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
dan1da40a32009-09-19 17:00:31 +00002338 sqlite3 *db = p->db;
drh648e2642013-07-11 15:03:32 +00002339 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2340 || (!deferred && p->nFkConstraint>0)
2341 ){
drhd91c1a12013-02-09 13:58:25 +00002342 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan32b09f22009-09-23 17:29:59 +00002343 p->errorAction = OE_Abort;
drh22c17b82015-05-15 04:13:15 +00002344 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
dan1da40a32009-09-19 17:00:31 +00002345 return SQLITE_ERROR;
2346 }
2347 return SQLITE_OK;
2348}
2349#endif
2350
2351/*
drh92f02c32004-09-02 14:57:08 +00002352** This routine is called the when a VDBE tries to halt. If the VDBE
2353** has made changes and is in autocommit mode, then commit those
2354** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00002355**
drh92f02c32004-09-02 14:57:08 +00002356** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00002357** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2358** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00002359**
2360** Return an error code. If the commit could not complete because of
2361** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2362** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00002363*/
drhff0587c2007-08-29 17:43:19 +00002364int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00002365 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00002366 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00002367
2368 /* This function contains the logic that determines if a statement or
2369 ** transaction will be committed or rolled back as a result of the
2370 ** execution of this virtual machine.
2371 **
drh71b890a2007-10-03 15:30:52 +00002372 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00002373 **
drh71b890a2007-10-03 15:30:52 +00002374 ** SQLITE_NOMEM
2375 ** SQLITE_IOERR
2376 ** SQLITE_FULL
2377 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00002378 **
drh71b890a2007-10-03 15:30:52 +00002379 ** Then the internal cache might have been left in an inconsistent
2380 ** state. We need to rollback the statement transaction, if there is
2381 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00002382 */
drh9a324642003-09-06 20:12:01 +00002383
drh17435752007-08-16 04:30:38 +00002384 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00002385 p->rc = SQLITE_NOMEM;
2386 }
drh6e856bc2011-12-09 18:06:44 +00002387 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
drh5f82e3c2009-07-06 00:44:08 +00002388 closeAllCursors(p);
drh92f02c32004-09-02 14:57:08 +00002389 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00002390 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00002391 }
danielk19771d850a72004-05-31 08:26:49 +00002392 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00002393
danc0537fe2013-06-28 19:41:43 +00002394 /* No commit or rollback needed if the program never started or if the
2395 ** SQL statement does not read or write a database file. */
2396 if( p->pc>=0 && p->bIsReader ){
drhaac2f552006-09-23 21:44:23 +00002397 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00002398 int eStatementOp = 0;
2399 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00002400
2401 /* Lock all btrees used by the statement */
drhbdaec522011-04-04 00:14:43 +00002402 sqlite3VdbeEnter(p);
drhff0587c2007-08-29 17:43:19 +00002403
drh71b890a2007-10-03 15:30:52 +00002404 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00002405 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00002406 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00002407 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00002408 if( isSpecialError ){
dan5653e4d2010-08-12 11:25:47 +00002409 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2410 ** no rollback is necessary. Otherwise, at least a savepoint
2411 ** transaction must be rolled back to restore the database to a
2412 ** consistent state.
2413 **
2414 ** Even if the statement is read-only, it is important to perform
2415 ** a statement or transaction rollback operation. If the error
mistachkin48864df2013-03-21 21:20:32 +00002416 ** occurred while writing to the journal, sub-journal or database
dan5653e4d2010-08-12 11:25:47 +00002417 ** file as part of an effort to free up cache space (see function
2418 ** pagerStress() in pager.c), the rollback is required to restore
2419 ** the pager to a consistent state.
danielk197707cb5602006-01-20 10:55:05 +00002420 */
drhad4a4b82008-11-05 16:37:34 +00002421 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
drhfa3be902009-07-07 02:44:07 +00002422 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00002423 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002424 }else{
2425 /* We are forced to roll back the active transaction. Before doing
2426 ** so, abort any other statements this handle currently has active.
2427 */
drh21021a52012-02-13 17:01:51 +00002428 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002429 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002430 db->autoCommit = 1;
danc3da6672014-10-28 18:24:16 +00002431 p->nChange = 0;
danielk197707cb5602006-01-20 10:55:05 +00002432 }
danielk1977261919c2005-12-06 12:52:59 +00002433 }
2434 }
dan32b09f22009-09-23 17:29:59 +00002435
2436 /* Check for immediate foreign key violations. */
2437 if( p->rc==SQLITE_OK ){
2438 sqlite3VdbeCheckFk(p, 0);
2439 }
danielk197707cb5602006-01-20 10:55:05 +00002440
danielk1977bd434552009-03-18 10:33:00 +00002441 /* If the auto-commit flag is set and this is the only active writer
2442 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00002443 **
2444 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00002445 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00002446 */
danielk1977093e0f62008-11-13 18:00:14 +00002447 if( !sqlite3VtabInSync(db)
2448 && db->autoCommit
drh4f7d3a52013-06-27 23:54:02 +00002449 && db->nVdbeWrite==(p->readOnly==0)
danielk1977093e0f62008-11-13 18:00:14 +00002450 ){
danielk197707cb5602006-01-20 10:55:05 +00002451 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
dan19611b12011-01-24 16:00:58 +00002452 rc = sqlite3VdbeCheckFk(p, 1);
2453 if( rc!=SQLITE_OK ){
drhe9ce5852011-02-11 22:54:28 +00002454 if( NEVER(p->readOnly) ){
drhbdaec522011-04-04 00:14:43 +00002455 sqlite3VdbeLeave(p);
dan19611b12011-01-24 16:00:58 +00002456 return SQLITE_ERROR;
2457 }
drhd91c1a12013-02-09 13:58:25 +00002458 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
dan19611b12011-01-24 16:00:58 +00002459 }else{
2460 /* The auto-commit flag is true, the vdbe program was successful
2461 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2462 ** key constraints to hold up the transaction. This means a commit
2463 ** is required. */
2464 rc = vdbeCommit(db, p);
dan1da40a32009-09-19 17:00:31 +00002465 }
dan19611b12011-01-24 16:00:58 +00002466 if( rc==SQLITE_BUSY && p->readOnly ){
drhbdaec522011-04-04 00:14:43 +00002467 sqlite3VdbeLeave(p);
danielk197707cb5602006-01-20 10:55:05 +00002468 return SQLITE_BUSY;
2469 }else if( rc!=SQLITE_OK ){
2470 p->rc = rc;
drh0f198a72012-02-13 16:43:16 +00002471 sqlite3RollbackAll(db, SQLITE_OK);
danc3da6672014-10-28 18:24:16 +00002472 p->nChange = 0;
danielk197707cb5602006-01-20 10:55:05 +00002473 }else{
dan1da40a32009-09-19 17:00:31 +00002474 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00002475 db->nDeferredImmCons = 0;
2476 db->flags &= ~SQLITE_DeferFKs;
danielk197707cb5602006-01-20 10:55:05 +00002477 sqlite3CommitInternalChanges(db);
2478 }
2479 }else{
drh0f198a72012-02-13 16:43:16 +00002480 sqlite3RollbackAll(db, SQLITE_OK);
danc3da6672014-10-28 18:24:16 +00002481 p->nChange = 0;
danielk197707cb5602006-01-20 10:55:05 +00002482 }
danielk1977bd434552009-03-18 10:33:00 +00002483 db->nStatement = 0;
2484 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00002485 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00002486 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00002487 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00002488 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00002489 }else{
drh21021a52012-02-13 17:01:51 +00002490 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977fc158bf2009-01-07 08:12:16 +00002491 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00002492 db->autoCommit = 1;
danc3da6672014-10-28 18:24:16 +00002493 p->nChange = 0;
danielk197707cb5602006-01-20 10:55:05 +00002494 }
danielk19771d850a72004-05-31 08:26:49 +00002495 }
danielk197707cb5602006-01-20 10:55:05 +00002496
danielk1977bd434552009-03-18 10:33:00 +00002497 /* If eStatementOp is non-zero, then a statement transaction needs to
2498 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2499 ** do so. If this operation returns an error, and the current statement
drh35173242010-03-08 21:40:13 +00002500 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2501 ** current statement error code.
danielk197707cb5602006-01-20 10:55:05 +00002502 */
danielk1977bd434552009-03-18 10:33:00 +00002503 if( eStatementOp ){
2504 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
dan40ad9d22010-06-03 09:17:38 +00002505 if( rc ){
drhd91c1a12013-02-09 13:58:25 +00002506 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
dan40ad9d22010-06-03 09:17:38 +00002507 p->rc = rc;
2508 sqlite3DbFree(db, p->zErrMsg);
2509 p->zErrMsg = 0;
2510 }
drh21021a52012-02-13 17:01:51 +00002511 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
dan40ad9d22010-06-03 09:17:38 +00002512 sqlite3CloseSavepoints(db);
2513 db->autoCommit = 1;
danc3da6672014-10-28 18:24:16 +00002514 p->nChange = 0;
danielk197707cb5602006-01-20 10:55:05 +00002515 }
danielk197777d83ba2004-05-31 10:08:14 +00002516 }
danielk197707cb5602006-01-20 10:55:05 +00002517
danielk1977bd434552009-03-18 10:33:00 +00002518 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2519 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00002520 */
drh6be240e2009-07-14 02:33:02 +00002521 if( p->changeCntOn ){
danielk1977bd434552009-03-18 10:33:00 +00002522 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00002523 sqlite3VdbeSetChanges(db, p->nChange);
2524 }else{
2525 sqlite3VdbeSetChanges(db, 0);
2526 }
2527 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00002528 }
drhff0587c2007-08-29 17:43:19 +00002529
2530 /* Release the locks */
drhbdaec522011-04-04 00:14:43 +00002531 sqlite3VdbeLeave(p);
drh9a324642003-09-06 20:12:01 +00002532 }
danielk19771d850a72004-05-31 08:26:49 +00002533
danielk197765fd59f2006-06-24 11:51:33 +00002534 /* We have successfully halted and closed the VM. Record this fact. */
2535 if( p->pc>=0 ){
drh4f7d3a52013-06-27 23:54:02 +00002536 db->nVdbeActive--;
2537 if( !p->readOnly ) db->nVdbeWrite--;
drh1713afb2013-06-28 01:24:57 +00002538 if( p->bIsReader ) db->nVdbeRead--;
drh4f7d3a52013-06-27 23:54:02 +00002539 assert( db->nVdbeActive>=db->nVdbeRead );
2540 assert( db->nVdbeRead>=db->nVdbeWrite );
2541 assert( db->nVdbeWrite>=0 );
drh9a324642003-09-06 20:12:01 +00002542 }
drh92f02c32004-09-02 14:57:08 +00002543 p->magic = VDBE_MAGIC_HALT;
2544 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00002545 if( p->db->mallocFailed ){
2546 p->rc = SQLITE_NOMEM;
2547 }
danielk19771d850a72004-05-31 08:26:49 +00002548
danielk1977404ca072009-03-16 13:19:36 +00002549 /* If the auto-commit flag is set to true, then any locks that were held
2550 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2551 ** to invoke any required unlock-notify callbacks.
2552 */
2553 if( db->autoCommit ){
2554 sqlite3ConnectionUnlocked(db);
2555 }
2556
drh4f7d3a52013-06-27 23:54:02 +00002557 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
dan19611b12011-01-24 16:00:58 +00002558 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
drh92f02c32004-09-02 14:57:08 +00002559}
drh4cf7c7f2007-08-28 23:28:07 +00002560
drh92f02c32004-09-02 14:57:08 +00002561
2562/*
drh3c23a882007-01-09 14:01:13 +00002563** Each VDBE holds the result of the most recent sqlite3_step() call
2564** in p->rc. This routine sets that result back to SQLITE_OK.
2565*/
2566void sqlite3VdbeResetStepResult(Vdbe *p){
2567 p->rc = SQLITE_OK;
2568}
2569
2570/*
dan029ead62011-10-27 15:19:58 +00002571** Copy the error code and error message belonging to the VDBE passed
2572** as the first argument to its database handle (so that they will be
2573** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2574**
2575** This function does not clear the VDBE error code or message, just
2576** copies them to the database handle.
2577*/
2578int sqlite3VdbeTransferError(Vdbe *p){
2579 sqlite3 *db = p->db;
2580 int rc = p->rc;
2581 if( p->zErrMsg ){
drh81bdd6d2011-10-29 01:33:24 +00002582 u8 mallocFailed = db->mallocFailed;
dan029ead62011-10-27 15:19:58 +00002583 sqlite3BeginBenignMalloc();
drha3cc0072013-12-13 16:23:55 +00002584 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
dan029ead62011-10-27 15:19:58 +00002585 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2586 sqlite3EndBenignMalloc();
drh81bdd6d2011-10-29 01:33:24 +00002587 db->mallocFailed = mallocFailed;
dan029ead62011-10-27 15:19:58 +00002588 db->errCode = rc;
2589 }else{
drh13f40da2014-08-22 18:00:11 +00002590 sqlite3Error(db, rc);
dan029ead62011-10-27 15:19:58 +00002591 }
2592 return rc;
2593}
2594
danac455932012-11-26 19:50:41 +00002595#ifdef SQLITE_ENABLE_SQLLOG
2596/*
2597** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2598** invoke it.
2599*/
2600static void vdbeInvokeSqllog(Vdbe *v){
2601 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2602 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2603 assert( v->db->init.busy==0 );
2604 if( zExpanded ){
2605 sqlite3GlobalConfig.xSqllog(
2606 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2607 );
2608 sqlite3DbFree(v->db, zExpanded);
2609 }
2610 }
2611}
2612#else
2613# define vdbeInvokeSqllog(x)
2614#endif
2615
dan029ead62011-10-27 15:19:58 +00002616/*
drh92f02c32004-09-02 14:57:08 +00002617** Clean up a VDBE after execution but do not delete the VDBE just yet.
2618** Write any error messages into *pzErrMsg. Return the result code.
2619**
2620** After this routine is run, the VDBE should be ready to be executed
2621** again.
2622**
2623** To look at it another way, this routine resets the state of the
2624** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2625** VDBE_MAGIC_INIT.
2626*/
drhc890fec2008-08-01 20:10:08 +00002627int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00002628 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002629 db = p->db;
drh92f02c32004-09-02 14:57:08 +00002630
2631 /* If the VM did not run to completion or if it encountered an
2632 ** error, then it might not have been halted properly. So halt
2633 ** it now.
2634 */
2635 sqlite3VdbeHalt(p);
2636
drhfb7e7652005-01-24 00:28:42 +00002637 /* If the VDBE has be run even partially, then transfer the error code
2638 ** and error message from the VDBE into the main database structure. But
2639 ** if the VDBE has just been set to run but has not actually executed any
2640 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00002641 */
drhfb7e7652005-01-24 00:28:42 +00002642 if( p->pc>=0 ){
danac455932012-11-26 19:50:41 +00002643 vdbeInvokeSqllog(p);
dan029ead62011-10-27 15:19:58 +00002644 sqlite3VdbeTransferError(p);
2645 sqlite3DbFree(db, p->zErrMsg);
2646 p->zErrMsg = 0;
drh4611d922010-02-25 14:47:01 +00002647 if( p->runOnlyOnce ) p->expired = 1;
danielk1977a21c6b62005-01-24 10:25:59 +00002648 }else if( p->rc && p->expired ){
2649 /* The expired flag was set on the VDBE before the first call
2650 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2651 ** called), set the database error in this case as well.
2652 */
drh13f40da2014-08-22 18:00:11 +00002653 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002654 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00002655 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00002656 }
2657
2658 /* Reclaim all memory used by the VDBE
2659 */
drhc890fec2008-08-01 20:10:08 +00002660 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00002661
2662 /* Save profiling information from this VDBE run.
2663 */
drh9a324642003-09-06 20:12:01 +00002664#ifdef VDBE_PROFILE
2665 {
2666 FILE *out = fopen("vdbe_profile.out", "a");
2667 if( out ){
2668 int i;
2669 fprintf(out, "---- ");
2670 for(i=0; i<p->nOp; i++){
2671 fprintf(out, "%02x", p->aOp[i].opcode);
2672 }
2673 fprintf(out, "\n");
drh2926f962014-02-17 01:13:28 +00002674 if( p->zSql ){
2675 char c, pc = 0;
2676 fprintf(out, "-- ");
2677 for(i=0; (c = p->zSql[i])!=0; i++){
2678 if( pc=='\n' ) fprintf(out, "-- ");
2679 putc(c, out);
2680 pc = c;
2681 }
2682 if( pc!='\n' ) fprintf(out, "\n");
2683 }
drh9a324642003-09-06 20:12:01 +00002684 for(i=0; i<p->nOp; i++){
drh15ab9412014-02-24 14:24:01 +00002685 char zHdr[100];
2686 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
drh9a324642003-09-06 20:12:01 +00002687 p->aOp[i].cnt,
2688 p->aOp[i].cycles,
2689 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2690 );
drh15ab9412014-02-24 14:24:01 +00002691 fprintf(out, "%s", zHdr);
danielk19774adee202004-05-08 08:23:19 +00002692 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00002693 }
2694 fclose(out);
2695 }
2696 }
2697#endif
drh7fa20922013-09-17 23:36:33 +00002698 p->iCurrentTime = 0;
drh9a324642003-09-06 20:12:01 +00002699 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00002700 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00002701}
drh92f02c32004-09-02 14:57:08 +00002702
drh9a324642003-09-06 20:12:01 +00002703/*
2704** Clean up and delete a VDBE after execution. Return an integer which is
2705** the result code. Write any error message text into *pzErrMsg.
2706*/
danielk19779e6db7d2004-06-21 08:18:51 +00002707int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00002708 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00002709 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00002710 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00002711 assert( (rc & p->db->errMask)==rc );
drh9a324642003-09-06 20:12:01 +00002712 }
danielk19774adee202004-05-08 08:23:19 +00002713 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00002714 return rc;
2715}
2716
2717/*
dan0c547792013-07-18 17:12:08 +00002718** If parameter iOp is less than zero, then invoke the destructor for
2719** all auxiliary data pointers currently cached by the VM passed as
2720** the first argument.
2721**
2722** Or, if iOp is greater than or equal to zero, then the destructor is
2723** only invoked for those auxiliary data pointers created by the user
2724** function invoked by the OP_Function opcode at instruction iOp of
2725** VM pVdbe, and only then if:
2726**
2727** * the associated function parameter is the 32nd or later (counting
2728** from left to right), or
2729**
2730** * the corresponding bit in argument mask is clear (where the first
peter.d.reid60ec9142014-09-06 16:39:46 +00002731** function parameter corresponds to bit 0 etc.).
drhf92c7ff2004-06-19 15:40:23 +00002732*/
dan0c547792013-07-18 17:12:08 +00002733void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2734 AuxData **pp = &pVdbe->pAuxData;
2735 while( *pp ){
2736 AuxData *pAux = *pp;
2737 if( (iOp<0)
drh693e6712014-01-24 22:58:00 +00002738 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
dan0c547792013-07-18 17:12:08 +00002739 ){
drh693e6712014-01-24 22:58:00 +00002740 testcase( pAux->iArg==31 );
drhf92c7ff2004-06-19 15:40:23 +00002741 if( pAux->xDelete ){
2742 pAux->xDelete(pAux->pAux);
2743 }
dan0c547792013-07-18 17:12:08 +00002744 *pp = pAux->pNext;
2745 sqlite3DbFree(pVdbe->db, pAux);
2746 }else{
2747 pp= &pAux->pNext;
drhf92c7ff2004-06-19 15:40:23 +00002748 }
2749 }
2750}
2751
2752/*
drhcb103b92012-10-26 00:11:23 +00002753** Free all memory associated with the Vdbe passed as the second argument,
2754** except for object itself, which is preserved.
2755**
dand46def72010-07-24 11:28:28 +00002756** The difference between this function and sqlite3VdbeDelete() is that
2757** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
drhcb103b92012-10-26 00:11:23 +00002758** the database connection and frees the object itself.
dand46def72010-07-24 11:28:28 +00002759*/
drhcb103b92012-10-26 00:11:23 +00002760void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
dand19c9332010-07-26 12:05:17 +00002761 SubProgram *pSub, *pNext;
drh124c0b42011-06-01 18:15:55 +00002762 int i;
dand46def72010-07-24 11:28:28 +00002763 assert( p->db==0 || p->db==db );
2764 releaseMemArray(p->aVar, p->nVar);
2765 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
dand19c9332010-07-26 12:05:17 +00002766 for(pSub=p->pProgram; pSub; pSub=pNext){
2767 pNext = pSub->pNext;
2768 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2769 sqlite3DbFree(db, pSub);
2770 }
drh124c0b42011-06-01 18:15:55 +00002771 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
dand46def72010-07-24 11:28:28 +00002772 vdbeFreeOpArray(db, p->aOp, p->nOp);
dand46def72010-07-24 11:28:28 +00002773 sqlite3DbFree(db, p->aColName);
2774 sqlite3DbFree(db, p->zSql);
2775 sqlite3DbFree(db, p->pFree);
dan6f9702e2014-11-01 20:38:06 +00002776#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan6f9702e2014-11-01 20:38:06 +00002777 for(i=0; i<p->nScan; i++){
2778 sqlite3DbFree(db, p->aScan[i].zName);
2779 }
2780 sqlite3DbFree(db, p->aScan);
2781#endif
dand46def72010-07-24 11:28:28 +00002782}
2783
2784/*
drh9a324642003-09-06 20:12:01 +00002785** Delete an entire VDBE.
2786*/
danielk19774adee202004-05-08 08:23:19 +00002787void sqlite3VdbeDelete(Vdbe *p){
drh633e6d52008-07-28 19:34:53 +00002788 sqlite3 *db;
2789
drhfa3be902009-07-07 02:44:07 +00002790 if( NEVER(p==0) ) return;
drh633e6d52008-07-28 19:34:53 +00002791 db = p->db;
drh4245c402012-06-02 14:32:21 +00002792 assert( sqlite3_mutex_held(db->mutex) );
drhcb103b92012-10-26 00:11:23 +00002793 sqlite3VdbeClearObject(db, p);
drh9a324642003-09-06 20:12:01 +00002794 if( p->pPrev ){
2795 p->pPrev->pNext = p->pNext;
2796 }else{
drh633e6d52008-07-28 19:34:53 +00002797 assert( db->pVdbe==p );
2798 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00002799 }
2800 if( p->pNext ){
2801 p->pNext->pPrev = p->pPrev;
2802 }
drh9a324642003-09-06 20:12:01 +00002803 p->magic = VDBE_MAGIC_DEAD;
drh87f5c5f2010-01-20 01:20:56 +00002804 p->db = 0;
drhcb103b92012-10-26 00:11:23 +00002805 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002806}
drha11846b2004-01-07 18:52:56 +00002807
2808/*
drh6848dad2014-08-22 23:33:03 +00002809** The cursor "p" has a pending seek operation that has not yet been
2810** carried out. Seek the cursor now. If an error occurs, return
2811** the appropriate error code.
2812*/
2813static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
2814 int res, rc;
2815#ifdef SQLITE_TEST
2816 extern int sqlite3_search_count;
2817#endif
2818 assert( p->deferredMoveto );
2819 assert( p->isTable );
2820 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
2821 if( rc ) return rc;
drh6848dad2014-08-22 23:33:03 +00002822 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
drh6848dad2014-08-22 23:33:03 +00002823#ifdef SQLITE_TEST
2824 sqlite3_search_count++;
2825#endif
2826 p->deferredMoveto = 0;
2827 p->cacheStatus = CACHE_STALE;
2828 return SQLITE_OK;
2829}
2830
2831/*
2832** Something has moved cursor "p" out of place. Maybe the row it was
2833** pointed to was deleted out from under it. Or maybe the btree was
2834** rebalanced. Whatever the cause, try to restore "p" to the place it
peter.d.reid60ec9142014-09-06 16:39:46 +00002835** is supposed to be pointing. If the row was deleted out from under the
drh6848dad2014-08-22 23:33:03 +00002836** cursor, set the cursor to point to a NULL row.
2837*/
2838static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
2839 int isDifferentRow, rc;
2840 assert( p->pCursor!=0 );
2841 assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
2842 rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
2843 p->cacheStatus = CACHE_STALE;
2844 if( isDifferentRow ) p->nullRow = 1;
2845 return rc;
2846}
2847
2848/*
drhc22284f2014-10-13 16:02:20 +00002849** Check to ensure that the cursor is valid. Restore the cursor
2850** if need be. Return any I/O error from the restore operation.
2851*/
2852int sqlite3VdbeCursorRestore(VdbeCursor *p){
2853 if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
2854 return handleMovedCursor(p);
2855 }
2856 return SQLITE_OK;
2857}
2858
2859/*
drh9a65f2c2009-06-22 19:05:40 +00002860** Make sure the cursor p is ready to read or write the row to which it
2861** was last positioned. Return an error code if an OOM fault or I/O error
2862** prevents us from positioning the cursor to its correct position.
2863**
drha11846b2004-01-07 18:52:56 +00002864** If a MoveTo operation is pending on the given cursor, then do that
drh9a65f2c2009-06-22 19:05:40 +00002865** MoveTo now. If no move is pending, check to see if the row has been
2866** deleted out from under the cursor and if it has, mark the row as
2867** a NULL row.
2868**
2869** If the cursor is already pointing to the correct row and that row has
2870** not been deleted out from under the cursor, then this routine is a no-op.
drha11846b2004-01-07 18:52:56 +00002871*/
drhdfe88ec2008-11-03 20:55:06 +00002872int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002873 if( p->deferredMoveto ){
drh6848dad2014-08-22 23:33:03 +00002874 return handleDeferredMoveto(p);
2875 }
drhc22284f2014-10-13 16:02:20 +00002876 if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){
drh6848dad2014-08-22 23:33:03 +00002877 return handleMovedCursor(p);
drha11846b2004-01-07 18:52:56 +00002878 }
2879 return SQLITE_OK;
2880}
danielk19774adee202004-05-08 08:23:19 +00002881
drhab9f7f12004-05-08 10:56:11 +00002882/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002883** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002884**
danielk1977cfcdaef2004-05-12 07:33:33 +00002885** sqlite3VdbeSerialType()
2886** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002887** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002888** sqlite3VdbeSerialPut()
2889** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002890**
2891** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002892** data and index records. Each serialized value consists of a
2893** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2894** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002895**
danielk1977cfcdaef2004-05-12 07:33:33 +00002896** In an SQLite index record, the serial type is stored directly before
2897** the blob of data that it corresponds to. In a table record, all serial
2898** types are stored at the start of the record, and the blobs of data at
2899** the end. Hence these functions allow the caller to handle the
mistachkin48864df2013-03-21 21:20:32 +00002900** serial-type and data blob separately.
danielk1977cfcdaef2004-05-12 07:33:33 +00002901**
2902** The following table describes the various storage classes for data:
2903**
2904** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002905** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002906** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002907** 1 1 signed integer
2908** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002909** 3 3 signed integer
2910** 4 4 signed integer
2911** 5 6 signed integer
2912** 6 8 signed integer
2913** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002914** 8 0 Integer constant 0
2915** 9 0 Integer constant 1
2916** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002917** N>=12 and even (N-12)/2 BLOB
2918** N>=13 and odd (N-13)/2 text
2919**
drh35a59652006-01-02 18:24:40 +00002920** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2921** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002922*/
2923
2924/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002925** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002926*/
drhd946db02005-12-29 19:23:06 +00002927u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002928 int flags = pMem->flags;
drheac5bd72014-07-25 21:35:39 +00002929 u32 n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002930
2931 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002932 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002933 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002934 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002935 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002936# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002937 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002938 u64 u;
drhcfd654b2011-03-05 13:54:15 +00002939 if( i<0 ){
drh1b40e632014-11-20 02:58:10 +00002940 u = ~i;
drhcfd654b2011-03-05 13:54:15 +00002941 }else{
2942 u = i;
2943 }
drh56690b32012-09-17 15:36:31 +00002944 if( u<=127 ){
2945 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2946 }
drh5742b632005-01-26 17:47:02 +00002947 if( u<=32767 ) return 2;
2948 if( u<=8388607 ) return 3;
2949 if( u<=2147483647 ) return 4;
2950 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002951 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002952 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002953 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002954 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002955 }
danielk1977e4359752008-11-03 09:39:45 +00002956 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drheac5bd72014-07-25 21:35:39 +00002957 assert( pMem->n>=0 );
2958 n = (u32)pMem->n;
drhfdf972a2007-05-02 13:30:27 +00002959 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002960 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002961 }
drhfdf972a2007-05-02 13:30:27 +00002962 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002963}
2964
2965/*
drhc5ef7152015-06-28 02:58:51 +00002966** The sizes for serial types less than 12
2967*/
2968static const u8 sqlite3SmallTypeSizes[] = {
2969 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0
2970};
2971
2972/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002973** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002974*/
drh35cd6432009-06-05 14:17:21 +00002975u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002976 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002977 return (serial_type-12)/2;
2978 }else{
drhc5ef7152015-06-28 02:58:51 +00002979 return sqlite3SmallTypeSizes[serial_type];
drh51846b52004-05-28 16:00:21 +00002980 }
danielk1977192ac1d2004-05-10 07:17:30 +00002981}
2982
2983/*
drh110daac2007-05-04 11:59:31 +00002984** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002985** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002986** upper 4 bytes. Return the result.
2987**
drh7a4f5022007-05-23 07:20:08 +00002988** For most architectures, this is a no-op.
2989**
2990** (later): It is reported to me that the mixed-endian problem
2991** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2992** that early versions of GCC stored the two words of a 64-bit
2993** float in the wrong order. And that error has been propagated
2994** ever since. The blame is not necessarily with GCC, though.
2995** GCC might have just copying the problem from a prior compiler.
2996** I am also told that newer versions of GCC that follow a different
2997** ABI get the byte order right.
2998**
2999** Developers using SQLite on an ARM7 should compile and run their
3000** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
3001** enabled, some asserts below will ensure that the byte order of
3002** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00003003**
3004** (2007-08-30) Frank van Vugt has studied this problem closely
3005** and has send his findings to the SQLite developers. Frank
3006** writes that some Linux kernels offer floating point hardware
3007** emulation that uses only 32-bit mantissas instead of a full
3008** 48-bits as required by the IEEE standard. (This is the
3009** CONFIG_FPE_FASTFPE option.) On such systems, floating point
3010** byte swapping becomes very complicated. To avoid problems,
3011** the necessary byte swapping is carried out using a 64-bit integer
3012** rather than a 64-bit float. Frank assures us that the code here
3013** works for him. We, the developers, have no way to independently
3014** verify this, but Frank seems to know what he is talking about
3015** so we trust him.
drh110daac2007-05-04 11:59:31 +00003016*/
3017#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00003018static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00003019 union {
drh60d09a72007-08-30 15:05:08 +00003020 u64 r;
drh110daac2007-05-04 11:59:31 +00003021 u32 i[2];
3022 } u;
3023 u32 t;
3024
3025 u.r = in;
3026 t = u.i[0];
3027 u.i[0] = u.i[1];
3028 u.i[1] = t;
3029 return u.r;
3030}
3031# define swapMixedEndianFloat(X) X = floatSwap(X)
3032#else
3033# define swapMixedEndianFloat(X)
3034#endif
3035
3036/*
danielk1977cfcdaef2004-05-12 07:33:33 +00003037** Write the serialized data blob for the value stored in pMem into
3038** buf. It is assumed that the caller has allocated sufficient space.
3039** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00003040**
drh038b7bc2013-12-09 23:17:22 +00003041** nBuf is the amount of space left in buf[]. The caller is responsible
3042** for allocating enough space to buf[] to hold the entire field, exclusive
3043** of the pMem->u.nZero bytes for a MEM_Zero value.
drhfdf972a2007-05-02 13:30:27 +00003044**
3045** Return the number of bytes actually written into buf[]. The number
3046** of bytes in the zero-filled tail is included in the return value only
3047** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00003048*/
drha9ab4812013-12-11 11:00:44 +00003049u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
drh35cd6432009-06-05 14:17:21 +00003050 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00003051
drh1483e142004-05-21 21:12:42 +00003052 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00003053 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00003054 u64 v;
drh35cd6432009-06-05 14:17:21 +00003055 u32 i;
drha19b7752004-05-30 21:14:58 +00003056 if( serial_type==7 ){
drh74eaba42014-09-18 17:52:15 +00003057 assert( sizeof(v)==sizeof(pMem->u.r) );
3058 memcpy(&v, &pMem->u.r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00003059 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00003060 }else{
drh3c024d62007-03-30 11:23:45 +00003061 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00003062 }
drhc5ef7152015-06-28 02:58:51 +00003063 len = i = sqlite3SmallTypeSizes[serial_type];
drh3f5b1992014-08-22 13:22:32 +00003064 assert( i>0 );
3065 do{
3066 buf[--i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00003067 v >>= 8;
drh3f5b1992014-08-22 13:22:32 +00003068 }while( i );
drh1483e142004-05-21 21:12:42 +00003069 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00003070 }
drhd946db02005-12-29 19:23:06 +00003071
danielk1977cfcdaef2004-05-12 07:33:33 +00003072 /* String or blob */
drhd946db02005-12-29 19:23:06 +00003073 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00003074 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00003075 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00003076 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00003077 memcpy(buf, pMem->z, len);
3078 return len;
3079 }
3080
3081 /* NULL or constants 0 or 1 */
3082 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00003083}
3084
drhf926d1e2014-03-04 04:04:33 +00003085/* Input "x" is a sequence of unsigned characters that represent a
3086** big-endian integer. Return the equivalent native integer
3087*/
3088#define ONE_BYTE_INT(x) ((i8)(x)[0])
3089#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
3090#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
3091#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
drh8932bec2014-08-22 14:56:13 +00003092#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
drhf926d1e2014-03-04 04:04:33 +00003093
danielk1977cfcdaef2004-05-12 07:33:33 +00003094/*
3095** Deserialize the data blob pointed to by buf as serial type serial_type
3096** and store the result in pMem. Return the number of bytes read.
drh14a924a2014-08-22 14:34:05 +00003097**
3098** This function is implemented as two separate routines for performance.
3099** The few cases that require local variables are broken out into a separate
3100** routine so that in most cases the overhead of moving the stack pointer
3101** is avoided.
danielk1977cfcdaef2004-05-12 07:33:33 +00003102*/
drh14a924a2014-08-22 14:34:05 +00003103static u32 SQLITE_NOINLINE serialGet(
danielk197793d46752004-05-23 13:30:58 +00003104 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00003105 u32 serial_type, /* Serial type to deserialize */
3106 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00003107){
drh8932bec2014-08-22 14:56:13 +00003108 u64 x = FOUR_BYTE_UINT(buf);
3109 u32 y = FOUR_BYTE_UINT(buf+4);
3110 x = (x<<32) + y;
drh14a924a2014-08-22 14:34:05 +00003111 if( serial_type==6 ){
drh654858d2014-11-20 02:18:14 +00003112 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
3113 ** twos-complement integer. */
drh14a924a2014-08-22 14:34:05 +00003114 pMem->u.i = *(i64*)&x;
3115 pMem->flags = MEM_Int;
3116 testcase( pMem->u.i<0 );
3117 }else{
drh654858d2014-11-20 02:18:14 +00003118 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
3119 ** floating point number. */
drh14a924a2014-08-22 14:34:05 +00003120#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
3121 /* Verify that integers and floating point values use the same
3122 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3123 ** defined that 64-bit floating point values really are mixed
3124 ** endian.
3125 */
3126 static const u64 t1 = ((u64)0x3ff00000)<<32;
3127 static const double r1 = 1.0;
3128 u64 t2 = t1;
3129 swapMixedEndianFloat(t2);
3130 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
3131#endif
drh74eaba42014-09-18 17:52:15 +00003132 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
drh14a924a2014-08-22 14:34:05 +00003133 swapMixedEndianFloat(x);
drh74eaba42014-09-18 17:52:15 +00003134 memcpy(&pMem->u.r, &x, sizeof(x));
3135 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
drh14a924a2014-08-22 14:34:05 +00003136 }
3137 return 8;
3138}
danielk1977b1bc9532004-05-22 03:05:33 +00003139u32 sqlite3VdbeSerialGet(
3140 const unsigned char *buf, /* Buffer to deserialize from */
3141 u32 serial_type, /* Serial type to deserialize */
3142 Mem *pMem /* Memory cell to write value into */
3143){
drh3c685822005-05-21 18:32:18 +00003144 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00003145 case 10: /* Reserved for future use */
3146 case 11: /* Reserved for future use */
drh654858d2014-11-20 02:18:14 +00003147 case 0: { /* Null */
3148 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
drh3c685822005-05-21 18:32:18 +00003149 pMem->flags = MEM_Null;
3150 break;
3151 }
drh654858d2014-11-20 02:18:14 +00003152 case 1: {
3153 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
3154 ** integer. */
drhf926d1e2014-03-04 04:04:33 +00003155 pMem->u.i = ONE_BYTE_INT(buf);
drh1483e142004-05-21 21:12:42 +00003156 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003157 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003158 return 1;
drh1483e142004-05-21 21:12:42 +00003159 }
drh3c685822005-05-21 18:32:18 +00003160 case 2: { /* 2-byte signed integer */
drh654858d2014-11-20 02:18:14 +00003161 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
3162 ** twos-complement integer. */
drhf926d1e2014-03-04 04:04:33 +00003163 pMem->u.i = TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00003164 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003165 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003166 return 2;
3167 }
3168 case 3: { /* 3-byte signed integer */
drh654858d2014-11-20 02:18:14 +00003169 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
3170 ** twos-complement integer. */
drhf926d1e2014-03-04 04:04:33 +00003171 pMem->u.i = THREE_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00003172 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003173 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003174 return 3;
3175 }
3176 case 4: { /* 4-byte signed integer */
drh654858d2014-11-20 02:18:14 +00003177 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
3178 ** twos-complement integer. */
drh8932bec2014-08-22 14:56:13 +00003179 pMem->u.i = FOUR_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00003180 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003181 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003182 return 4;
3183 }
3184 case 5: { /* 6-byte signed integer */
drh654858d2014-11-20 02:18:14 +00003185 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
3186 ** twos-complement integer. */
drhf926d1e2014-03-04 04:04:33 +00003187 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
drh3c685822005-05-21 18:32:18 +00003188 pMem->flags = MEM_Int;
drhb6e8fd12014-03-06 01:56:33 +00003189 testcase( pMem->u.i<0 );
drh3c685822005-05-21 18:32:18 +00003190 return 6;
3191 }
drh91124b32005-08-18 18:15:05 +00003192 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00003193 case 7: { /* IEEE floating point */
drh8932bec2014-08-22 14:56:13 +00003194 /* These use local variables, so do them in a separate routine
3195 ** to avoid having to move the frame pointer in the common case */
drh14a924a2014-08-22 14:34:05 +00003196 return serialGet(buf,serial_type,pMem);
drh3c685822005-05-21 18:32:18 +00003197 }
drhd946db02005-12-29 19:23:06 +00003198 case 8: /* Integer 0 */
3199 case 9: { /* Integer 1 */
drh654858d2014-11-20 02:18:14 +00003200 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
3201 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
drh3c024d62007-03-30 11:23:45 +00003202 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00003203 pMem->flags = MEM_Int;
3204 return 0;
3205 }
drh3c685822005-05-21 18:32:18 +00003206 default: {
drh654858d2014-11-20 02:18:14 +00003207 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
3208 ** length.
3209 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
3210 ** (N-13)/2 bytes in length. */
drhc138daf2013-11-19 13:55:34 +00003211 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
drh3c685822005-05-21 18:32:18 +00003212 pMem->z = (char *)buf;
drh14a924a2014-08-22 14:34:05 +00003213 pMem->n = (serial_type-12)/2;
drhc138daf2013-11-19 13:55:34 +00003214 pMem->flags = aFlag[serial_type&1];
drh14a924a2014-08-22 14:34:05 +00003215 return pMem->n;
drh696b32f2004-05-30 01:51:52 +00003216 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003217 }
drh3c685822005-05-21 18:32:18 +00003218 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00003219}
drh1e968a02008-03-25 00:22:21 +00003220/*
dan03e9cfc2011-09-05 14:20:27 +00003221** This routine is used to allocate sufficient space for an UnpackedRecord
3222** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3223** the first argument is a pointer to KeyInfo structure pKeyInfo.
drh1e968a02008-03-25 00:22:21 +00003224**
dan03e9cfc2011-09-05 14:20:27 +00003225** The space is either allocated using sqlite3DbMallocRaw() or from within
3226** the unaligned buffer passed via the second and third arguments (presumably
3227** stack space). If the former, then *ppFree is set to a pointer that should
3228** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3229** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3230** before returning.
drh1e968a02008-03-25 00:22:21 +00003231**
dan03e9cfc2011-09-05 14:20:27 +00003232** If an OOM error occurs, NULL is returned.
3233*/
3234UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3235 KeyInfo *pKeyInfo, /* Description of the record */
3236 char *pSpace, /* Unaligned space available */
3237 int szSpace, /* Size of pSpace[] in bytes */
3238 char **ppFree /* OUT: Caller should free this pointer */
drh1e968a02008-03-25 00:22:21 +00003239){
dan03e9cfc2011-09-05 14:20:27 +00003240 UnpackedRecord *p; /* Unpacked record to return */
3241 int nOff; /* Increment pSpace by nOff to align it */
3242 int nByte; /* Number of bytes required for *p */
3243
3244 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
shane80167bf2009-04-10 15:42:36 +00003245 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3246 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3247 */
3248 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00003249 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
dan42acb3e2011-09-05 20:16:38 +00003250 if( nByte>szSpace+nOff ){
dan03e9cfc2011-09-05 14:20:27 +00003251 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3252 *ppFree = (char *)p;
dan42acb3e2011-09-05 20:16:38 +00003253 if( !p ) return 0;
drh1e968a02008-03-25 00:22:21 +00003254 }else{
dan42acb3e2011-09-05 20:16:38 +00003255 p = (UnpackedRecord*)&pSpace[nOff];
dan03e9cfc2011-09-05 14:20:27 +00003256 *ppFree = 0;
drh1e968a02008-03-25 00:22:21 +00003257 }
dan42acb3e2011-09-05 20:16:38 +00003258
3259 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
drhe1a022e2012-09-17 17:16:53 +00003260 assert( pKeyInfo->aSortOrder!=0 );
drh1e968a02008-03-25 00:22:21 +00003261 p->pKeyInfo = pKeyInfo;
3262 p->nField = pKeyInfo->nField + 1;
dan03e9cfc2011-09-05 14:20:27 +00003263 return p;
3264}
3265
3266/*
3267** Given the nKey-byte encoding of a record in pKey[], populate the
3268** UnpackedRecord structure indicated by the fourth argument with the
3269** contents of the decoded record.
3270*/
3271void sqlite3VdbeRecordUnpack(
3272 KeyInfo *pKeyInfo, /* Information about the record format */
3273 int nKey, /* Size of the binary record */
3274 const void *pKey, /* The binary record */
3275 UnpackedRecord *p /* Populate this structure before returning. */
3276){
3277 const unsigned char *aKey = (const unsigned char *)pKey;
3278 int d;
3279 u32 idx; /* Offset in aKey[] to read from */
3280 u16 u; /* Unsigned loop counter */
3281 u32 szHdr;
dan42acb3e2011-09-05 20:16:38 +00003282 Mem *pMem = p->aMem;
dan03e9cfc2011-09-05 14:20:27 +00003283
dan1fed5da2014-02-25 21:01:25 +00003284 p->default_rc = 0;
drh8c5d1522009-04-10 00:56:28 +00003285 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00003286 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00003287 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00003288 u = 0;
drh7f4b19f2014-09-16 13:30:05 +00003289 while( idx<szHdr && d<=nKey ){
drh1e968a02008-03-25 00:22:21 +00003290 u32 serial_type;
3291
danielk197700e13612008-11-17 19:18:54 +00003292 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00003293 pMem->enc = pKeyInfo->enc;
3294 pMem->db = pKeyInfo->db;
drhc3f1d5f2011-05-30 23:42:16 +00003295 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
drh17bcb102014-09-18 21:25:33 +00003296 pMem->szMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00003297 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00003298 pMem++;
drh7f4b19f2014-09-16 13:30:05 +00003299 if( (++u)>=p->nField ) break;
drh1e968a02008-03-25 00:22:21 +00003300 }
drh7d10d5a2008-08-20 16:35:10 +00003301 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00003302 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00003303}
3304
dan3833e932014-03-01 19:44:56 +00003305#if SQLITE_DEBUG
dan3b9330f2014-02-27 20:44:18 +00003306/*
dan3833e932014-03-01 19:44:56 +00003307** This function compares two index or table record keys in the same way
3308** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3309** this function deserializes and compares values using the
3310** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3311** in assert() statements to ensure that the optimized code in
3312** sqlite3VdbeRecordCompare() returns results with these two primitives.
drh79211e12014-05-02 17:33:16 +00003313**
3314** Return true if the result of comparison is equivalent to desiredResult.
3315** Return false if there is a disagreement.
dan3b9330f2014-02-27 20:44:18 +00003316*/
dan3833e932014-03-01 19:44:56 +00003317static int vdbeRecordCompareDebug(
dan1fed5da2014-02-25 21:01:25 +00003318 int nKey1, const void *pKey1, /* Left key */
drh79211e12014-05-02 17:33:16 +00003319 const UnpackedRecord *pPKey2, /* Right key */
3320 int desiredResult /* Correct answer */
dan1fed5da2014-02-25 21:01:25 +00003321){
dan3b9330f2014-02-27 20:44:18 +00003322 u32 d1; /* Offset into aKey[] of next data element */
3323 u32 idx1; /* Offset into aKey[] of next header element */
3324 u32 szHdr1; /* Number of bytes in header */
3325 int i = 0;
3326 int rc = 0;
3327 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3328 KeyInfo *pKeyInfo;
3329 Mem mem1;
dan1fed5da2014-02-25 21:01:25 +00003330
dan3b9330f2014-02-27 20:44:18 +00003331 pKeyInfo = pPKey2->pKeyInfo;
drh84de6902014-05-02 18:46:52 +00003332 if( pKeyInfo->db==0 ) return 1;
dan3b9330f2014-02-27 20:44:18 +00003333 mem1.enc = pKeyInfo->enc;
3334 mem1.db = pKeyInfo->db;
3335 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
drh17bcb102014-09-18 21:25:33 +00003336 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003337
dan3b9330f2014-02-27 20:44:18 +00003338 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3339 ** We could initialize it, as shown here, to silence those complaints.
3340 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3341 ** the unnecessary initialization has a measurable negative performance
3342 ** impact, since this routine is a very high runner. And so, we choose
3343 ** to ignore the compiler warnings and leave this variable uninitialized.
3344 */
3345 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
3346
3347 idx1 = getVarint32(aKey1, szHdr1);
drh46981362015-07-08 12:25:38 +00003348 if( szHdr1>98307 ) return SQLITE_CORRUPT;
dan3b9330f2014-02-27 20:44:18 +00003349 d1 = szHdr1;
3350 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3351 assert( pKeyInfo->aSortOrder!=0 );
3352 assert( pKeyInfo->nField>0 );
3353 assert( idx1<=szHdr1 || CORRUPT_DB );
3354 do{
3355 u32 serial_type1;
dan1fed5da2014-02-25 21:01:25 +00003356
dan3b9330f2014-02-27 20:44:18 +00003357 /* Read the serial types for the next element in each key. */
3358 idx1 += getVarint32( aKey1+idx1, serial_type1 );
dan1fed5da2014-02-25 21:01:25 +00003359
dan3b9330f2014-02-27 20:44:18 +00003360 /* Verify that there is enough key space remaining to avoid
3361 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3362 ** always be greater than or equal to the amount of required key space.
3363 ** Use that approximation to avoid the more expensive call to
3364 ** sqlite3VdbeSerialTypeLen() in the common case.
3365 */
3366 if( d1+serial_type1+2>(u32)nKey1
3367 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3368 ){
3369 break;
dan1fed5da2014-02-25 21:01:25 +00003370 }
dan1fed5da2014-02-25 21:01:25 +00003371
dan3b9330f2014-02-27 20:44:18 +00003372 /* Extract the values to be compared.
3373 */
3374 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
dan1fed5da2014-02-25 21:01:25 +00003375
dan3b9330f2014-02-27 20:44:18 +00003376 /* Do the comparison
3377 */
3378 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3379 if( rc!=0 ){
drh17bcb102014-09-18 21:25:33 +00003380 assert( mem1.szMalloc==0 ); /* See comment below */
dan3b9330f2014-02-27 20:44:18 +00003381 if( pKeyInfo->aSortOrder[i] ){
3382 rc = -rc; /* Invert the result for DESC sort order. */
dan1fed5da2014-02-25 21:01:25 +00003383 }
drh79211e12014-05-02 17:33:16 +00003384 goto debugCompareEnd;
dan1fed5da2014-02-25 21:01:25 +00003385 }
dan3b9330f2014-02-27 20:44:18 +00003386 i++;
3387 }while( idx1<szHdr1 && i<pPKey2->nField );
dan1fed5da2014-02-25 21:01:25 +00003388
dan3b9330f2014-02-27 20:44:18 +00003389 /* No memory allocation is ever used on mem1. Prove this using
3390 ** the following assert(). If the assert() fails, it indicates a
3391 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3392 */
drh17bcb102014-09-18 21:25:33 +00003393 assert( mem1.szMalloc==0 );
dan3b9330f2014-02-27 20:44:18 +00003394
3395 /* rc==0 here means that one of the keys ran out of fields and
peter.d.reid60ec9142014-09-06 16:39:46 +00003396 ** all the fields up to that point were equal. Return the default_rc
dan3b9330f2014-02-27 20:44:18 +00003397 ** value. */
drh79211e12014-05-02 17:33:16 +00003398 rc = pPKey2->default_rc;
3399
3400debugCompareEnd:
3401 if( desiredResult==0 && rc==0 ) return 1;
3402 if( desiredResult<0 && rc<0 ) return 1;
3403 if( desiredResult>0 && rc>0 ) return 1;
3404 if( CORRUPT_DB ) return 1;
3405 if( pKeyInfo->db->mallocFailed ) return 1;
3406 return 0;
dan1fed5da2014-02-25 21:01:25 +00003407}
dan3833e932014-03-01 19:44:56 +00003408#endif
dan1fed5da2014-02-25 21:01:25 +00003409
drhe1bb8022015-01-19 19:48:52 +00003410#if SQLITE_DEBUG
3411/*
3412** Count the number of fields (a.k.a. columns) in the record given by
3413** pKey,nKey. The verify that this count is less than or equal to the
3414** limit given by pKeyInfo->nField + pKeyInfo->nXField.
3415**
3416** If this constraint is not satisfied, it means that the high-speed
3417** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
3418** not work correctly. If this assert() ever fires, it probably means
3419** that the KeyInfo.nField or KeyInfo.nXField values were computed
3420** incorrectly.
3421*/
3422static void vdbeAssertFieldCountWithinLimits(
3423 int nKey, const void *pKey, /* The record to verify */
3424 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
3425){
3426 int nField = 0;
3427 u32 szHdr;
3428 u32 idx;
3429 u32 notUsed;
3430 const unsigned char *aKey = (const unsigned char*)pKey;
3431
3432 if( CORRUPT_DB ) return;
3433 idx = getVarint32(aKey, szHdr);
mistachkin1b3ee492015-01-21 00:51:08 +00003434 assert( nKey>=0 );
3435 assert( szHdr<=(u32)nKey );
drhe1bb8022015-01-19 19:48:52 +00003436 while( idx<szHdr ){
3437 idx += getVarint32(aKey+idx, notUsed);
3438 nField++;
3439 }
3440 assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
3441}
drh1af3c642015-01-19 20:57:19 +00003442#else
3443# define vdbeAssertFieldCountWithinLimits(A,B,C)
drhe1bb8022015-01-19 19:48:52 +00003444#endif
3445
dan3833e932014-03-01 19:44:56 +00003446/*
3447** Both *pMem1 and *pMem2 contain string values. Compare the two values
3448** using the collation sequence pColl. As usual, return a negative , zero
3449** or positive value if *pMem1 is less than, equal to or greater than
3450** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3451*/
dan1fed5da2014-02-25 21:01:25 +00003452static int vdbeCompareMemString(
dan3833e932014-03-01 19:44:56 +00003453 const Mem *pMem1,
3454 const Mem *pMem2,
dan38fdead2014-04-01 10:19:02 +00003455 const CollSeq *pColl,
3456 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
dan1fed5da2014-02-25 21:01:25 +00003457){
3458 if( pMem1->enc==pColl->enc ){
3459 /* The strings are already in the correct encoding. Call the
3460 ** comparison function directly */
3461 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3462 }else{
3463 int rc;
3464 const void *v1, *v2;
3465 int n1, n2;
3466 Mem c1;
3467 Mem c2;
drh17bcb102014-09-18 21:25:33 +00003468 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
3469 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
dan1fed5da2014-02-25 21:01:25 +00003470 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3471 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3472 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3473 n1 = v1==0 ? 0 : c1.n;
3474 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3475 n2 = v2==0 ? 0 : c2.n;
3476 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3477 sqlite3VdbeMemRelease(&c1);
3478 sqlite3VdbeMemRelease(&c2);
dan38fdead2014-04-01 10:19:02 +00003479 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
dan1fed5da2014-02-25 21:01:25 +00003480 return rc;
3481 }
3482}
3483
3484/*
drh982ff722014-09-16 03:24:43 +00003485** Compare two blobs. Return negative, zero, or positive if the first
3486** is less than, equal to, or greater than the second, respectively.
3487** If one blob is a prefix of the other, then the shorter is the lessor.
3488*/
3489static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
3490 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
3491 if( c ) return c;
3492 return pB1->n - pB2->n;
3493}
3494
3495
3496/*
dan1fed5da2014-02-25 21:01:25 +00003497** Compare the values contained by the two memory cells, returning
3498** negative, zero or positive if pMem1 is less than, equal to, or greater
3499** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3500** and reals) sorted numerically, followed by text ordered by the collating
3501** sequence pColl and finally blob's ordered by memcmp().
3502**
3503** Two NULL values are considered equal by this function.
3504*/
3505int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
dan1fed5da2014-02-25 21:01:25 +00003506 int f1, f2;
3507 int combined_flags;
3508
3509 f1 = pMem1->flags;
3510 f2 = pMem2->flags;
3511 combined_flags = f1|f2;
3512 assert( (combined_flags & MEM_RowSet)==0 );
3513
3514 /* If one value is NULL, it is less than the other. If both values
3515 ** are NULL, return 0.
3516 */
3517 if( combined_flags&MEM_Null ){
3518 return (f2&MEM_Null) - (f1&MEM_Null);
3519 }
3520
3521 /* If one value is a number and the other is not, the number is less.
3522 ** If both are numbers, compare as reals if one is a real, or as integers
3523 ** if both values are integers.
3524 */
3525 if( combined_flags&(MEM_Int|MEM_Real) ){
3526 double r1, r2;
3527 if( (f1 & f2 & MEM_Int)!=0 ){
3528 if( pMem1->u.i < pMem2->u.i ) return -1;
3529 if( pMem1->u.i > pMem2->u.i ) return 1;
3530 return 0;
3531 }
3532 if( (f1&MEM_Real)!=0 ){
drh74eaba42014-09-18 17:52:15 +00003533 r1 = pMem1->u.r;
dan1fed5da2014-02-25 21:01:25 +00003534 }else if( (f1&MEM_Int)!=0 ){
3535 r1 = (double)pMem1->u.i;
3536 }else{
3537 return 1;
3538 }
3539 if( (f2&MEM_Real)!=0 ){
drh74eaba42014-09-18 17:52:15 +00003540 r2 = pMem2->u.r;
dan1fed5da2014-02-25 21:01:25 +00003541 }else if( (f2&MEM_Int)!=0 ){
3542 r2 = (double)pMem2->u.i;
3543 }else{
3544 return -1;
3545 }
3546 if( r1<r2 ) return -1;
3547 if( r1>r2 ) return 1;
3548 return 0;
3549 }
3550
3551 /* If one value is a string and the other is a blob, the string is less.
3552 ** If both are strings, compare using the collating functions.
3553 */
3554 if( combined_flags&MEM_Str ){
3555 if( (f1 & MEM_Str)==0 ){
3556 return 1;
3557 }
3558 if( (f2 & MEM_Str)==0 ){
3559 return -1;
3560 }
3561
3562 assert( pMem1->enc==pMem2->enc );
3563 assert( pMem1->enc==SQLITE_UTF8 ||
3564 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3565
3566 /* The collation sequence must be defined at this point, even if
3567 ** the user deletes the collation sequence after the vdbe program is
3568 ** compiled (this was not always the case).
3569 */
3570 assert( !pColl || pColl->xCmp );
3571
3572 if( pColl ){
dan38fdead2014-04-01 10:19:02 +00003573 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
dan1fed5da2014-02-25 21:01:25 +00003574 }
3575 /* If a NULL pointer was passed as the collate function, fall through
3576 ** to the blob case and use memcmp(). */
3577 }
3578
3579 /* Both values must be blobs. Compare using memcmp(). */
drh982ff722014-09-16 03:24:43 +00003580 return sqlite3BlobCompare(pMem1, pMem2);
dan1fed5da2014-02-25 21:01:25 +00003581}
3582
3583
dan3833e932014-03-01 19:44:56 +00003584/*
3585** The first argument passed to this function is a serial-type that
3586** corresponds to an integer - all values between 1 and 9 inclusive
3587** except 7. The second points to a buffer containing an integer value
3588** serialized according to serial_type. This function deserializes
3589** and returns the value.
3590*/
dan3b9330f2014-02-27 20:44:18 +00003591static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
drhf926d1e2014-03-04 04:04:33 +00003592 u32 y;
dan3833e932014-03-01 19:44:56 +00003593 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
dan3b9330f2014-02-27 20:44:18 +00003594 switch( serial_type ){
dan3833e932014-03-01 19:44:56 +00003595 case 0:
dan3b9330f2014-02-27 20:44:18 +00003596 case 1:
drhb6e8fd12014-03-06 01:56:33 +00003597 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003598 return ONE_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003599 case 2:
drhb6e8fd12014-03-06 01:56:33 +00003600 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003601 return TWO_BYTE_INT(aKey);
dan3b9330f2014-02-27 20:44:18 +00003602 case 3:
drhb6e8fd12014-03-06 01:56:33 +00003603 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003604 return THREE_BYTE_INT(aKey);
3605 case 4: {
drhb6e8fd12014-03-06 01:56:33 +00003606 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003607 y = FOUR_BYTE_UINT(aKey);
3608 return (i64)*(int*)&y;
3609 }
dan3b9330f2014-02-27 20:44:18 +00003610 case 5: {
drhb6e8fd12014-03-06 01:56:33 +00003611 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003612 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhaf5b2af2013-08-05 15:32:09 +00003613 }
dan3b9330f2014-02-27 20:44:18 +00003614 case 6: {
drhf926d1e2014-03-04 04:04:33 +00003615 u64 x = FOUR_BYTE_UINT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003616 testcase( aKey[0]&0x80 );
drhf926d1e2014-03-04 04:04:33 +00003617 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3618 return (i64)*(i64*)&x;
drh1e968a02008-03-25 00:22:21 +00003619 }
dan3b9330f2014-02-27 20:44:18 +00003620 }
drh407414c2009-07-14 14:15:27 +00003621
dan3b9330f2014-02-27 20:44:18 +00003622 return (serial_type - 8);
drh1e968a02008-03-25 00:22:21 +00003623}
danielk1977eb015e02004-05-18 01:31:14 +00003624
dan3833e932014-03-01 19:44:56 +00003625/*
3626** This function compares the two table rows or index records
3627** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
3628** or positive integer if key1 is less than, equal to or
3629** greater than key2. The {nKey1, pKey1} key must be a blob
peter.d.reid60ec9142014-09-06 16:39:46 +00003630** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
dan3833e932014-03-01 19:44:56 +00003631** key must be a parsed key such as obtained from
3632** sqlite3VdbeParseRecord.
3633**
3634** If argument bSkip is non-zero, it is assumed that the caller has already
3635** determined that the first fields of the keys are equal.
3636**
3637** Key1 and Key2 do not have to contain the same number of fields. If all
3638** fields that appear in both keys are equal, then pPKey2->default_rc is
3639** returned.
drha1f7c0a2014-03-28 03:12:48 +00003640**
dan38fdead2014-04-01 10:19:02 +00003641** If database corruption is discovered, set pPKey2->errCode to
3642** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
3643** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
3644** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
dan3833e932014-03-01 19:44:56 +00003645*/
dan7004f3f2015-03-30 12:06:26 +00003646int sqlite3VdbeRecordCompareWithSkip(
dan3833e932014-03-01 19:44:56 +00003647 int nKey1, const void *pKey1, /* Left key */
drha1f7c0a2014-03-28 03:12:48 +00003648 UnpackedRecord *pPKey2, /* Right key */
dan3833e932014-03-01 19:44:56 +00003649 int bSkip /* If true, skip the first field */
dan1fed5da2014-02-25 21:01:25 +00003650){
dan3833e932014-03-01 19:44:56 +00003651 u32 d1; /* Offset into aKey[] of next data element */
3652 int i; /* Index of next field to compare */
mistachkinffe6bc22014-03-04 11:16:20 +00003653 u32 szHdr1; /* Size of record header in bytes */
dan3833e932014-03-01 19:44:56 +00003654 u32 idx1; /* Offset of first type in header */
3655 int rc = 0; /* Return value */
3656 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
dan1fed5da2014-02-25 21:01:25 +00003657 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3658 const unsigned char *aKey1 = (const unsigned char *)pKey1;
3659 Mem mem1;
3660
dan3833e932014-03-01 19:44:56 +00003661 /* If bSkip is true, then the caller has already determined that the first
3662 ** two elements in the keys are equal. Fix the various stack variables so
dan3b9330f2014-02-27 20:44:18 +00003663 ** that this routine begins comparing at the second field. */
dan3833e932014-03-01 19:44:56 +00003664 if( bSkip ){
dan3b9330f2014-02-27 20:44:18 +00003665 u32 s1;
dan3b9330f2014-02-27 20:44:18 +00003666 idx1 = 1 + getVarint32(&aKey1[1], s1);
dan3833e932014-03-01 19:44:56 +00003667 szHdr1 = aKey1[0];
3668 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
dan3b9330f2014-02-27 20:44:18 +00003669 i = 1;
3670 pRhs++;
dan3833e932014-03-01 19:44:56 +00003671 }else{
3672 idx1 = getVarint32(aKey1, szHdr1);
3673 d1 = szHdr1;
drha1f7c0a2014-03-28 03:12:48 +00003674 if( d1>(unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003675 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003676 return 0; /* Corruption */
3677 }
dan3833e932014-03-01 19:44:56 +00003678 i = 0;
dan3b9330f2014-02-27 20:44:18 +00003679 }
3680
drh17bcb102014-09-18 21:25:33 +00003681 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
dan1fed5da2014-02-25 21:01:25 +00003682 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3683 || CORRUPT_DB );
3684 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3685 assert( pPKey2->pKeyInfo->nField>0 );
3686 assert( idx1<=szHdr1 || CORRUPT_DB );
3687 do{
dan1fed5da2014-02-25 21:01:25 +00003688 u32 serial_type;
3689
3690 /* RHS is an integer */
3691 if( pRhs->flags & MEM_Int ){
3692 serial_type = aKey1[idx1];
drhb6e8fd12014-03-06 01:56:33 +00003693 testcase( serial_type==12 );
danb95e1192015-05-26 20:31:20 +00003694 if( serial_type>=10 ){
dan1fed5da2014-02-25 21:01:25 +00003695 rc = +1;
3696 }else if( serial_type==0 ){
3697 rc = -1;
dan3b9330f2014-02-27 20:44:18 +00003698 }else if( serial_type==7 ){
3699 double rhs = (double)pRhs->u.i;
dan1fed5da2014-02-25 21:01:25 +00003700 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
drh74eaba42014-09-18 17:52:15 +00003701 if( mem1.u.r<rhs ){
dan3b9330f2014-02-27 20:44:18 +00003702 rc = -1;
drh74eaba42014-09-18 17:52:15 +00003703 }else if( mem1.u.r>rhs ){
dan3b9330f2014-02-27 20:44:18 +00003704 rc = +1;
3705 }
3706 }else{
3707 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3708 i64 rhs = pRhs->u.i;
3709 if( lhs<rhs ){
3710 rc = -1;
3711 }else if( lhs>rhs ){
3712 rc = +1;
dan1fed5da2014-02-25 21:01:25 +00003713 }
3714 }
3715 }
3716
3717 /* RHS is real */
3718 else if( pRhs->flags & MEM_Real ){
3719 serial_type = aKey1[idx1];
dancc7aa1f2015-05-26 20:07:32 +00003720 if( serial_type>=10 ){
3721 /* Serial types 12 or greater are strings and blobs (greater than
3722 ** numbers). Types 10 and 11 are currently "reserved for future
3723 ** use", so it doesn't really matter what the results of comparing
3724 ** them to numberic values are. */
dan1fed5da2014-02-25 21:01:25 +00003725 rc = +1;
3726 }else if( serial_type==0 ){
3727 rc = -1;
3728 }else{
drh74eaba42014-09-18 17:52:15 +00003729 double rhs = pRhs->u.r;
dan1fed5da2014-02-25 21:01:25 +00003730 double lhs;
3731 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3732 if( serial_type==7 ){
drh74eaba42014-09-18 17:52:15 +00003733 lhs = mem1.u.r;
dan1fed5da2014-02-25 21:01:25 +00003734 }else{
drh295aedf2014-03-03 18:25:24 +00003735 lhs = (double)mem1.u.i;
dan1fed5da2014-02-25 21:01:25 +00003736 }
3737 if( lhs<rhs ){
3738 rc = -1;
3739 }else if( lhs>rhs ){
3740 rc = +1;
3741 }
3742 }
3743 }
3744
3745 /* RHS is a string */
3746 else if( pRhs->flags & MEM_Str ){
3747 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003748 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003749 if( serial_type<12 ){
3750 rc = -1;
3751 }else if( !(serial_type & 0x01) ){
3752 rc = +1;
3753 }else{
3754 mem1.n = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003755 testcase( (d1+mem1.n)==(unsigned)nKey1 );
3756 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003757 if( (d1+mem1.n) > (unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003758 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003759 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003760 }else if( pKeyInfo->aColl[i] ){
3761 mem1.enc = pKeyInfo->enc;
3762 mem1.db = pKeyInfo->db;
3763 mem1.flags = MEM_Str;
drhfcb44a82014-03-03 15:13:27 +00003764 mem1.z = (char*)&aKey1[d1];
dan38fdead2014-04-01 10:19:02 +00003765 rc = vdbeCompareMemString(
3766 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
3767 );
dan1fed5da2014-02-25 21:01:25 +00003768 }else{
3769 int nCmp = MIN(mem1.n, pRhs->n);
3770 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3771 if( rc==0 ) rc = mem1.n - pRhs->n;
3772 }
3773 }
3774 }
3775
3776 /* RHS is a blob */
3777 else if( pRhs->flags & MEM_Blob ){
3778 getVarint32(&aKey1[idx1], serial_type);
drhb6e8fd12014-03-06 01:56:33 +00003779 testcase( serial_type==12 );
dan1fed5da2014-02-25 21:01:25 +00003780 if( serial_type<12 || (serial_type & 0x01) ){
3781 rc = -1;
3782 }else{
3783 int nStr = (serial_type - 12) / 2;
drhb6e8fd12014-03-06 01:56:33 +00003784 testcase( (d1+nStr)==(unsigned)nKey1 );
3785 testcase( (d1+nStr+1)==(unsigned)nKey1 );
drh295aedf2014-03-03 18:25:24 +00003786 if( (d1+nStr) > (unsigned)nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003787 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003788 return 0; /* Corruption */
dan1fed5da2014-02-25 21:01:25 +00003789 }else{
3790 int nCmp = MIN(nStr, pRhs->n);
3791 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3792 if( rc==0 ) rc = nStr - pRhs->n;
3793 }
3794 }
3795 }
3796
3797 /* RHS is null */
3798 else{
3799 serial_type = aKey1[idx1];
3800 rc = (serial_type!=0);
3801 }
3802
3803 if( rc!=0 ){
dan1fed5da2014-02-25 21:01:25 +00003804 if( pKeyInfo->aSortOrder[i] ){
3805 rc = -rc;
dan1fed5da2014-02-25 21:01:25 +00003806 }
drh79211e12014-05-02 17:33:16 +00003807 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
drh17bcb102014-09-18 21:25:33 +00003808 assert( mem1.szMalloc==0 ); /* See comment below */
dan1fed5da2014-02-25 21:01:25 +00003809 return rc;
3810 }
3811
3812 i++;
dan3b9330f2014-02-27 20:44:18 +00003813 pRhs++;
dan1fed5da2014-02-25 21:01:25 +00003814 d1 += sqlite3VdbeSerialTypeLen(serial_type);
3815 idx1 += sqlite3VarintLen(serial_type);
drh295aedf2014-03-03 18:25:24 +00003816 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
dan1fed5da2014-02-25 21:01:25 +00003817
3818 /* No memory allocation is ever used on mem1. Prove this using
3819 ** the following assert(). If the assert() fails, it indicates a
dan3833e932014-03-01 19:44:56 +00003820 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
drh17bcb102014-09-18 21:25:33 +00003821 assert( mem1.szMalloc==0 );
dan1fed5da2014-02-25 21:01:25 +00003822
3823 /* rc==0 here means that one or both of the keys ran out of fields and
peter.d.reid60ec9142014-09-06 16:39:46 +00003824 ** all the fields up to that point were equal. Return the default_rc
dan1fed5da2014-02-25 21:01:25 +00003825 ** value. */
dan3833e932014-03-01 19:44:56 +00003826 assert( CORRUPT_DB
drh66141812014-06-30 20:25:03 +00003827 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
dan6696ba32014-06-28 19:06:49 +00003828 || pKeyInfo->db->mallocFailed
dan3833e932014-03-01 19:44:56 +00003829 );
dan1fed5da2014-02-25 21:01:25 +00003830 return pPKey2->default_rc;
3831}
drh75179de2014-09-16 14:37:35 +00003832int sqlite3VdbeRecordCompare(
3833 int nKey1, const void *pKey1, /* Left key */
3834 UnpackedRecord *pPKey2 /* Right key */
3835){
dan7004f3f2015-03-30 12:06:26 +00003836 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
drh75179de2014-09-16 14:37:35 +00003837}
3838
dan1fed5da2014-02-25 21:01:25 +00003839
dan3833e932014-03-01 19:44:56 +00003840/*
3841** This function is an optimized version of sqlite3VdbeRecordCompare()
3842** that (a) the first field of pPKey2 is an integer, and (b) the
3843** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3844** byte (i.e. is less than 128).
drhe2ac5062014-03-26 12:02:38 +00003845**
3846** To avoid concerns about buffer overreads, this routine is only used
3847** on schemas where the maximum valid header size is 63 bytes or less.
dan3833e932014-03-01 19:44:56 +00003848*/
dan3b9330f2014-02-27 20:44:18 +00003849static int vdbeRecordCompareInt(
3850 int nKey1, const void *pKey1, /* Left key */
drh75179de2014-09-16 14:37:35 +00003851 UnpackedRecord *pPKey2 /* Right key */
dan3b9330f2014-02-27 20:44:18 +00003852){
dan9b8afef2014-03-03 20:48:50 +00003853 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
dan3b9330f2014-02-27 20:44:18 +00003854 int serial_type = ((const u8*)pKey1)[1];
3855 int res;
drhf926d1e2014-03-04 04:04:33 +00003856 u32 y;
3857 u64 x;
dan3b9330f2014-02-27 20:44:18 +00003858 i64 v = pPKey2->aMem[0].u.i;
3859 i64 lhs;
3860
drhe1bb8022015-01-19 19:48:52 +00003861 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
drhe2ac5062014-03-26 12:02:38 +00003862 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
dan3833e932014-03-01 19:44:56 +00003863 switch( serial_type ){
drhf926d1e2014-03-04 04:04:33 +00003864 case 1: { /* 1-byte signed integer */
3865 lhs = ONE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003866 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003867 break;
3868 }
drhf926d1e2014-03-04 04:04:33 +00003869 case 2: { /* 2-byte signed integer */
3870 lhs = TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003871 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003872 break;
3873 }
3874 case 3: { /* 3-byte signed integer */
3875 lhs = THREE_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003876 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003877 break;
3878 }
3879 case 4: { /* 4-byte signed integer */
3880 y = FOUR_BYTE_UINT(aKey);
3881 lhs = (i64)*(int*)&y;
drhb6e8fd12014-03-06 01:56:33 +00003882 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003883 break;
3884 }
3885 case 5: { /* 6-byte signed integer */
3886 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
drhb6e8fd12014-03-06 01:56:33 +00003887 testcase( lhs<0 );
drhf926d1e2014-03-04 04:04:33 +00003888 break;
3889 }
3890 case 6: { /* 8-byte signed integer */
3891 x = FOUR_BYTE_UINT(aKey);
3892 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3893 lhs = *(i64*)&x;
drhb6e8fd12014-03-06 01:56:33 +00003894 testcase( lhs<0 );
dan3b9330f2014-02-27 20:44:18 +00003895 break;
3896 }
dan3b9330f2014-02-27 20:44:18 +00003897 case 8:
3898 lhs = 0;
3899 break;
dan3b9330f2014-02-27 20:44:18 +00003900 case 9:
3901 lhs = 1;
3902 break;
3903
dan063d4a02014-02-28 09:48:30 +00003904 /* This case could be removed without changing the results of running
3905 ** this code. Including it causes gcc to generate a faster switch
3906 ** statement (since the range of switch targets now starts at zero and
dan597515d2014-02-28 18:39:51 +00003907 ** is contiguous) but does not cause any duplicate code to be generated
dan063d4a02014-02-28 09:48:30 +00003908 ** (as gcc is clever enough to combine the two like cases). Other
3909 ** compilers might be similar. */
3910 case 0: case 7:
drh75179de2014-09-16 14:37:35 +00003911 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
dan063d4a02014-02-28 09:48:30 +00003912
dan3b9330f2014-02-27 20:44:18 +00003913 default:
drh75179de2014-09-16 14:37:35 +00003914 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
dan3b9330f2014-02-27 20:44:18 +00003915 }
3916
3917 if( v>lhs ){
3918 res = pPKey2->r1;
3919 }else if( v<lhs ){
3920 res = pPKey2->r2;
3921 }else if( pPKey2->nField>1 ){
dan063d4a02014-02-28 09:48:30 +00003922 /* The first fields of the two keys are equal. Compare the trailing
3923 ** fields. */
dan7004f3f2015-03-30 12:06:26 +00003924 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003925 }else{
dan063d4a02014-02-28 09:48:30 +00003926 /* The first fields of the two keys are equal and there are no trailing
3927 ** fields. Return pPKey2->default_rc in this case. */
dan3b9330f2014-02-27 20:44:18 +00003928 res = pPKey2->default_rc;
3929 }
3930
drh79211e12014-05-02 17:33:16 +00003931 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
dan3b9330f2014-02-27 20:44:18 +00003932 return res;
3933}
3934
dan3833e932014-03-01 19:44:56 +00003935/*
3936** This function is an optimized version of sqlite3VdbeRecordCompare()
3937** that (a) the first field of pPKey2 is a string, that (b) the first field
3938** uses the collation sequence BINARY and (c) that the size-of-header varint
3939** at the start of (pKey1/nKey1) fits in a single byte.
3940*/
dan3b9330f2014-02-27 20:44:18 +00003941static int vdbeRecordCompareString(
3942 int nKey1, const void *pKey1, /* Left key */
drh75179de2014-09-16 14:37:35 +00003943 UnpackedRecord *pPKey2 /* Right key */
dan3b9330f2014-02-27 20:44:18 +00003944){
3945 const u8 *aKey1 = (const u8*)pKey1;
3946 int serial_type;
3947 int res;
3948
drhe1bb8022015-01-19 19:48:52 +00003949 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
dan3b9330f2014-02-27 20:44:18 +00003950 getVarint32(&aKey1[1], serial_type);
dan3b9330f2014-02-27 20:44:18 +00003951 if( serial_type<12 ){
3952 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
3953 }else if( !(serial_type & 0x01) ){
3954 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
3955 }else{
3956 int nCmp;
3957 int nStr;
dan3833e932014-03-01 19:44:56 +00003958 int szHdr = aKey1[0];
dan3b9330f2014-02-27 20:44:18 +00003959
3960 nStr = (serial_type-12) / 2;
drha1f7c0a2014-03-28 03:12:48 +00003961 if( (szHdr + nStr) > nKey1 ){
dan38fdead2014-04-01 10:19:02 +00003962 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
drha1f7c0a2014-03-28 03:12:48 +00003963 return 0; /* Corruption */
3964 }
dan3b9330f2014-02-27 20:44:18 +00003965 nCmp = MIN( pPKey2->aMem[0].n, nStr );
dan3833e932014-03-01 19:44:56 +00003966 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
dan3b9330f2014-02-27 20:44:18 +00003967
3968 if( res==0 ){
3969 res = nStr - pPKey2->aMem[0].n;
3970 if( res==0 ){
3971 if( pPKey2->nField>1 ){
dan7004f3f2015-03-30 12:06:26 +00003972 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
dan3b9330f2014-02-27 20:44:18 +00003973 }else{
3974 res = pPKey2->default_rc;
3975 }
3976 }else if( res>0 ){
3977 res = pPKey2->r2;
3978 }else{
3979 res = pPKey2->r1;
3980 }
3981 }else if( res>0 ){
3982 res = pPKey2->r2;
3983 }else{
3984 res = pPKey2->r1;
3985 }
3986 }
3987
drh66141812014-06-30 20:25:03 +00003988 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
dan3b9330f2014-02-27 20:44:18 +00003989 || CORRUPT_DB
dan6696ba32014-06-28 19:06:49 +00003990 || pPKey2->pKeyInfo->db->mallocFailed
dan3b9330f2014-02-27 20:44:18 +00003991 );
3992 return res;
3993}
3994
dan3833e932014-03-01 19:44:56 +00003995/*
3996** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3997** suitable for comparing serialized records to the unpacked record passed
3998** as the only argument.
3999*/
dan1fed5da2014-02-25 21:01:25 +00004000RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
dan9b8afef2014-03-03 20:48:50 +00004001 /* varintRecordCompareInt() and varintRecordCompareString() both assume
4002 ** that the size-of-header varint that occurs at the start of each record
4003 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
4004 ** also assumes that it is safe to overread a buffer by at least the
4005 ** maximum possible legal header size plus 8 bytes. Because there is
4006 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
4007 ** buffer passed to varintRecordCompareInt() this makes it convenient to
4008 ** limit the size of the header to 64 bytes in cases where the first field
4009 ** is an integer.
4010 **
4011 ** The easiest way to enforce this limit is to consider only records with
4012 ** 13 fields or less. If the first field is an integer, the maximum legal
4013 ** header size is (12*5 + 1 + 1) bytes. */
4014 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
dan1fed5da2014-02-25 21:01:25 +00004015 int flags = p->aMem[0].flags;
dan3b9330f2014-02-27 20:44:18 +00004016 if( p->pKeyInfo->aSortOrder[0] ){
4017 p->r1 = 1;
4018 p->r2 = -1;
4019 }else{
4020 p->r1 = -1;
4021 p->r2 = 1;
4022 }
dan1fed5da2014-02-25 21:01:25 +00004023 if( (flags & MEM_Int) ){
4024 return vdbeRecordCompareInt;
dan3b9330f2014-02-27 20:44:18 +00004025 }
drhb6e8fd12014-03-06 01:56:33 +00004026 testcase( flags & MEM_Real );
4027 testcase( flags & MEM_Null );
4028 testcase( flags & MEM_Blob );
4029 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
4030 assert( flags & MEM_Str );
dan1fed5da2014-02-25 21:01:25 +00004031 return vdbeRecordCompareString;
4032 }
4033 }
dan3b9330f2014-02-27 20:44:18 +00004034
dan3833e932014-03-01 19:44:56 +00004035 return sqlite3VdbeRecordCompare;
dan3b9330f2014-02-27 20:44:18 +00004036}
dan1fed5da2014-02-25 21:01:25 +00004037
danielk1977eb015e02004-05-18 01:31:14 +00004038/*
drh7a224de2004-06-02 01:22:02 +00004039** pCur points at an index entry created using the OP_MakeRecord opcode.
4040** Read the rowid (the last field in the record) and store it in *rowid.
4041** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00004042**
4043** pCur might be pointing to text obtained from a corrupt database file.
4044** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00004045*/
drhd3b74202014-09-17 16:41:15 +00004046int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00004047 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00004048 int rc;
drhd5788202004-05-28 08:21:05 +00004049 u32 szHdr; /* Size of the header */
4050 u32 typeRowid; /* Serial type of the rowid */
4051 u32 lenRowid; /* Size of the rowid */
4052 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00004053
drh88a003e2008-12-11 16:17:03 +00004054 /* Get the size of the index entry. Only indices entries of less
drh7b746032009-06-26 12:15:22 +00004055 ** than 2GiB are support - anything large must be database corruption.
4056 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
drhc27ae612009-07-14 18:35:44 +00004057 ** this code can safely assume that nCellKey is 32-bits
4058 */
drhea8ffdf2009-07-22 00:35:23 +00004059 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00004060 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00004061 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh7b746032009-06-26 12:15:22 +00004062 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
drh88a003e2008-12-11 16:17:03 +00004063
4064 /* Read in the complete content of the index entry */
drhd3b74202014-09-17 16:41:15 +00004065 sqlite3VdbeMemInit(&m, db, 0);
drh501932c2013-11-21 21:59:53 +00004066 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00004067 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00004068 return rc;
4069 }
drh88a003e2008-12-11 16:17:03 +00004070
4071 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00004072 (void)getVarint32((u8*)m.z, szHdr);
drh7b746032009-06-26 12:15:22 +00004073 testcase( szHdr==3 );
drh88a003e2008-12-11 16:17:03 +00004074 testcase( szHdr==m.n );
drh7b746032009-06-26 12:15:22 +00004075 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00004076 goto idx_rowid_corruption;
4077 }
4078
4079 /* The last field of the index should be an integer - the ROWID.
4080 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00004081 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00004082 testcase( typeRowid==1 );
4083 testcase( typeRowid==2 );
4084 testcase( typeRowid==3 );
4085 testcase( typeRowid==4 );
4086 testcase( typeRowid==5 );
4087 testcase( typeRowid==6 );
4088 testcase( typeRowid==8 );
4089 testcase( typeRowid==9 );
4090 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
4091 goto idx_rowid_corruption;
4092 }
drhc5ef7152015-06-28 02:58:51 +00004093 lenRowid = sqlite3SmallTypeSizes[typeRowid];
drheeb844a2009-08-08 18:01:07 +00004094 testcase( (u32)m.n==szHdr+lenRowid );
4095 if( unlikely((u32)m.n<szHdr+lenRowid) ){
drh88a003e2008-12-11 16:17:03 +00004096 goto idx_rowid_corruption;
4097 }
4098
4099 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00004100 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00004101 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00004102 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00004103 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00004104
4105 /* Jump here if database corruption is detected after m has been
4106 ** allocated. Free the m object and return SQLITE_CORRUPT. */
4107idx_rowid_corruption:
drh17bcb102014-09-18 21:25:33 +00004108 testcase( m.szMalloc!=0 );
drh88a003e2008-12-11 16:17:03 +00004109 sqlite3VdbeMemRelease(&m);
4110 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00004111}
4112
drh7cf6e4d2004-05-19 14:56:55 +00004113/*
drh5f82e3c2009-07-06 00:44:08 +00004114** Compare the key of the index entry that cursor pC is pointing to against
4115** the key string in pUnpacked. Write into *pRes a number
drh7cf6e4d2004-05-19 14:56:55 +00004116** that is negative, zero, or positive if pC is less than, equal to,
drh5f82e3c2009-07-06 00:44:08 +00004117** or greater than pUnpacked. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00004118**
drh5f82e3c2009-07-06 00:44:08 +00004119** pUnpacked is either created without a rowid or is truncated so that it
drhd5788202004-05-28 08:21:05 +00004120** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00004121** is ignored as well. Hence, this routine only compares the prefixes
4122** of the keys prior to the final rowid, not the entire key.
drh7cf6e4d2004-05-19 14:56:55 +00004123*/
danielk1977183f9f72004-05-13 05:20:26 +00004124int sqlite3VdbeIdxKeyCompare(
drhd3b74202014-09-17 16:41:15 +00004125 sqlite3 *db, /* Database connection */
drh295aedf2014-03-03 18:25:24 +00004126 VdbeCursor *pC, /* The cursor to compare against */
drha1f7c0a2014-03-28 03:12:48 +00004127 UnpackedRecord *pUnpacked, /* Unpacked version of key */
drh295aedf2014-03-03 18:25:24 +00004128 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00004129){
drh61fc5952007-04-01 23:49:51 +00004130 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00004131 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00004132 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00004133 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00004134
drhea8ffdf2009-07-22 00:35:23 +00004135 assert( sqlite3BtreeCursorIsValid(pCur) );
drhb07028f2011-10-14 21:49:18 +00004136 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
drhc27ae612009-07-14 18:35:44 +00004137 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
drh56689692014-03-03 19:29:28 +00004138 /* nCellKey will always be between 0 and 0xffffffff because of the way
drh407414c2009-07-14 14:15:27 +00004139 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
drhc27ae612009-07-14 18:35:44 +00004140 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00004141 *res = 0;
drh9978c972010-02-23 17:36:32 +00004142 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00004143 }
drhd3b74202014-09-17 16:41:15 +00004144 sqlite3VdbeMemInit(&m, db, 0);
drh501932c2013-11-21 21:59:53 +00004145 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00004146 if( rc ){
drhd5788202004-05-28 08:21:05 +00004147 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00004148 }
drh75179de2014-09-16 14:37:35 +00004149 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00004150 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00004151 return SQLITE_OK;
4152}
danielk1977b28af712004-06-21 06:50:26 +00004153
4154/*
4155** This routine sets the value to be returned by subsequent calls to
4156** sqlite3_changes() on the database handle 'db'.
4157*/
4158void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00004159 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00004160 db->nChange = nChange;
4161 db->nTotalChange += nChange;
4162}
4163
4164/*
4165** Set a flag in the vdbe to update the change counter when it is finalised
4166** or reset.
4167*/
drh4794f732004-11-05 17:17:50 +00004168void sqlite3VdbeCountChanges(Vdbe *v){
4169 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00004170}
drhd89bd002005-01-22 03:03:54 +00004171
4172/*
4173** Mark every prepared statement associated with a database connection
4174** as expired.
4175**
4176** An expired statement means that recompilation of the statement is
4177** recommend. Statements expire when things happen that make their
4178** programs obsolete. Removing user-defined functions or collating
4179** sequences, or changing an authorization function are the types of
4180** things that make prepared statements obsolete.
4181*/
4182void sqlite3ExpirePreparedStatements(sqlite3 *db){
4183 Vdbe *p;
4184 for(p = db->pVdbe; p; p=p->pNext){
4185 p->expired = 1;
4186 }
4187}
danielk1977aee18ef2005-03-09 12:26:50 +00004188
4189/*
4190** Return the database associated with the Vdbe.
4191*/
4192sqlite3 *sqlite3VdbeDb(Vdbe *v){
4193 return v->db;
4194}
dan937d0de2009-10-15 18:35:38 +00004195
4196/*
4197** Return a pointer to an sqlite3_value structure containing the value bound
4198** parameter iVar of VM v. Except, if the value is an SQL NULL, return
4199** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
4200** constants) to the value before returning it.
4201**
4202** The returned value must be freed by the caller using sqlite3ValueFree().
4203*/
drhcf0fd4a2013-08-01 12:21:58 +00004204sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
dan937d0de2009-10-15 18:35:38 +00004205 assert( iVar>0 );
4206 if( v ){
4207 Mem *pMem = &v->aVar[iVar-1];
4208 if( 0==(pMem->flags & MEM_Null) ){
4209 sqlite3_value *pRet = sqlite3ValueNew(v->db);
4210 if( pRet ){
4211 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
4212 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
dan937d0de2009-10-15 18:35:38 +00004213 }
4214 return pRet;
4215 }
4216 }
4217 return 0;
4218}
4219
4220/*
4221** Configure SQL variable iVar so that binding a new value to it signals
4222** to sqlite3_reoptimize() that re-preparing the statement may result
4223** in a better query plan.
4224*/
dan1d2ce4f2009-10-19 18:11:09 +00004225void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
dan937d0de2009-10-15 18:35:38 +00004226 assert( iVar>0 );
4227 if( iVar>32 ){
dan1d2ce4f2009-10-19 18:11:09 +00004228 v->expmask = 0xffffffff;
dan937d0de2009-10-15 18:35:38 +00004229 }else{
dan1d2ce4f2009-10-19 18:11:09 +00004230 v->expmask |= ((u32)1 << (iVar-1));
dan937d0de2009-10-15 18:35:38 +00004231 }
4232}
dan016f7812013-08-21 17:35:48 +00004233
4234#ifndef SQLITE_OMIT_VIRTUALTABLE
4235/*
4236** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4237** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4238** in memory obtained from sqlite3DbMalloc).
4239*/
4240void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
4241 sqlite3 *db = p->db;
4242 sqlite3DbFree(db, p->zErrMsg);
4243 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
4244 sqlite3_free(pVtab->zErrMsg);
4245 pVtab->zErrMsg = 0;
4246}
4247#endif /* SQLITE_OMIT_VIRTUALTABLE */