blob: 04d400614a02543a2b346a5ec5adcb79598472db [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
danielk19779a9b1562008-04-24 08:31:51 +000016**
drhdfe88ec2008-11-03 20:55:06 +000017** $Id: vdbeaux.c,v 1.415 2008/11/03 20:55:07 drh Exp $
drh9a324642003-09-06 20:12:01 +000018*/
19#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000020#include <ctype.h>
21#include "vdbeInt.h"
22
23
drh46c99e02007-08-27 23:26:59 +000024
drh9a324642003-09-06 20:12:01 +000025/*
26** When debugging the code generator in a symbolic debugger, one can
mlcreech3a00f902008-03-04 17:45:01 +000027** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000028** as they are added to the instruction stream.
29*/
drh8d904f02005-06-14 17:47:58 +000030#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +000031int sqlite3VdbeAddopTrace = 0;
drh9a324642003-09-06 20:12:01 +000032#endif
33
34
35/*
36** Create a new virtual database engine.
37*/
drh9bb575f2004-09-06 17:24:11 +000038Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000039 Vdbe *p;
drh17435752007-08-16 04:30:38 +000040 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000041 if( p==0 ) return 0;
42 p->db = db;
43 if( db->pVdbe ){
44 db->pVdbe->pPrev = p;
45 }
46 p->pNext = db->pVdbe;
47 p->pPrev = 0;
48 db->pVdbe = p;
49 p->magic = VDBE_MAGIC_INIT;
50 return p;
51}
52
53/*
drhb900aaf2006-11-09 00:24:53 +000054** Remember the SQL string for a prepared statement.
55*/
56void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
57 if( p==0 ) return;
58 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000059 p->zSql = sqlite3DbStrNDup(p->db, z, n);
drhb900aaf2006-11-09 00:24:53 +000060}
61
62/*
63** Return the SQL associated with a prepared statement
64*/
danielk1977d0e2a852007-11-14 06:48:48 +000065const char *sqlite3_sql(sqlite3_stmt *pStmt){
66 return ((Vdbe *)pStmt)->zSql;
drhb900aaf2006-11-09 00:24:53 +000067}
68
69/*
drhc5155252007-01-08 21:07:17 +000070** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000071*/
drhc5155252007-01-08 21:07:17 +000072void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
73 Vdbe tmp, *pTmp;
74 char *zTmp;
75 int nTmp;
76 tmp = *pA;
77 *pA = *pB;
78 *pB = tmp;
79 pTmp = pA->pNext;
80 pA->pNext = pB->pNext;
81 pB->pNext = pTmp;
82 pTmp = pA->pPrev;
83 pA->pPrev = pB->pPrev;
84 pB->pPrev = pTmp;
85 zTmp = pA->zSql;
86 pA->zSql = pB->zSql;
87 pB->zSql = zTmp;
88 nTmp = pA->nSql;
89 pA->nSql = pB->nSql;
90 pB->nSql = nTmp;
drhb900aaf2006-11-09 00:24:53 +000091}
92
drhcf1023c2007-05-08 20:59:49 +000093#ifdef SQLITE_DEBUG
drhb900aaf2006-11-09 00:24:53 +000094/*
drh9a324642003-09-06 20:12:01 +000095** Turn tracing on or off
96*/
danielk19774adee202004-05-08 08:23:19 +000097void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000098 p->trace = trace;
99}
drhcf1023c2007-05-08 20:59:49 +0000100#endif
drh9a324642003-09-06 20:12:01 +0000101
102/*
drh76ff3a02004-09-24 22:32:30 +0000103** Resize the Vdbe.aOp array so that it contains at least N
drha4e5d582007-10-20 15:41:57 +0000104** elements.
danielk1977ace3eb22006-01-26 10:35:04 +0000105**
106** If an out-of-memory error occurs while resizing the array,
107** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
108** any opcodes already allocated can be correctly deallocated
109** along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000110*/
111static void resizeOpArray(Vdbe *p, int N){
drha4e5d582007-10-20 15:41:57 +0000112 VdbeOp *pNew;
drha4e5d582007-10-20 15:41:57 +0000113 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
114 if( pNew ){
115 p->nOpAlloc = N;
116 p->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000117 }
118}
119
120/*
drh9a324642003-09-06 20:12:01 +0000121** Add a new instruction to the list of instructions current in the
122** VDBE. Return the address of the new instruction.
123**
124** Parameters:
125**
126** p Pointer to the VDBE
127**
128** op The opcode for this instruction
129**
drh66a51672008-01-03 00:01:23 +0000130** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000131**
danielk19774adee202004-05-08 08:23:19 +0000132** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000133** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000134** operand.
135*/
drh66a51672008-01-03 00:01:23 +0000136int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000137 int i;
drh701a0ae2004-02-22 20:05:00 +0000138 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000139
140 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000141 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000142 if( p->nOpAlloc<=i ){
drheee4c8c2008-02-18 22:24:57 +0000143 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
drh17435752007-08-16 04:30:38 +0000144 if( p->db->mallocFailed ){
drhfd2d26b2006-03-15 22:44:36 +0000145 return 0;
146 }
drh9a324642003-09-06 20:12:01 +0000147 }
danielk197701256832007-04-18 14:24:32 +0000148 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000149 pOp = &p->aOp[i];
150 pOp->opcode = op;
drh26c9b5e2008-04-11 14:56:53 +0000151 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000152 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000153 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000154 pOp->p3 = p3;
155 pOp->p4.p = 0;
156 pOp->p4type = P4_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000157 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000158#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000159 pOp->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000160 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000161#endif
drh26c9b5e2008-04-11 14:56:53 +0000162#ifdef VDBE_PROFILE
163 pOp->cycles = 0;
164 pOp->cnt = 0;
165#endif
drh9a324642003-09-06 20:12:01 +0000166 return i;
167}
drh66a51672008-01-03 00:01:23 +0000168int sqlite3VdbeAddOp0(Vdbe *p, int op){
169 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
170}
171int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
172 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
173}
174int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
175 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000176}
177
drh66a51672008-01-03 00:01:23 +0000178
drh701a0ae2004-02-22 20:05:00 +0000179/*
drh66a51672008-01-03 00:01:23 +0000180** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000181*/
drh66a51672008-01-03 00:01:23 +0000182int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000183 Vdbe *p, /* Add the opcode to this VM */
184 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000185 int p1, /* The P1 operand */
186 int p2, /* The P2 operand */
187 int p3, /* The P3 operand */
188 const char *zP4, /* The P4 operand */
189 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000190){
drh66a51672008-01-03 00:01:23 +0000191 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
192 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000193 return addr;
194}
195
196/*
drh9a324642003-09-06 20:12:01 +0000197** Create a new symbolic label for an instruction that has yet to be
198** coded. The symbolic label is really just a negative number. The
199** label can be used as the P2 value of an operation. Later, when
200** the label is resolved to a specific address, the VDBE will scan
201** through its operation list and change all values of P2 which match
202** the label into the resolved address.
203**
204** The VDBE knows that a P2 value is a label because labels are
205** always negative and P2 values are suppose to be non-negative.
206** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000207**
208** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000209*/
danielk19774adee202004-05-08 08:23:19 +0000210int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000211 int i;
212 i = p->nLabel++;
213 assert( p->magic==VDBE_MAGIC_INIT );
214 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000215 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
danielk19771e536952007-08-16 10:09:01 +0000216 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drhcf643722007-03-27 13:36:37 +0000217 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000218 }
drh76ff3a02004-09-24 22:32:30 +0000219 if( p->aLabel ){
220 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000221 }
drh9a324642003-09-06 20:12:01 +0000222 return -1-i;
223}
224
225/*
226** Resolve label "x" to be the address of the next instruction to
227** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000228** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000229*/
danielk19774adee202004-05-08 08:23:19 +0000230void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000231 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000232 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000233 assert( j>=0 && j<p->nLabel );
234 if( p->aLabel ){
235 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000236 }
237}
238
239/*
drh9cbf3422008-01-17 16:22:13 +0000240** Loop through the program looking for P2 values that are negative
241** on jump instructions. Each such value is a label. Resolve the
242** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000243**
244** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000245**
drh13449892005-09-07 21:22:45 +0000246** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000247** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000248** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000249**
drh38449902005-06-07 01:43:41 +0000250** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000251** instructions that might cause a statement rollback. Such instructions
252** are:
253**
254** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
255** * OP_Destroy
256** * OP_VUpdate
257** * OP_VRename
258**
259** If no such instruction is found, then every Statement instruction
260** is changed to a Noop. In this way, we avoid creating the statement
261** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000262*/
drh9cbf3422008-01-17 16:22:13 +0000263static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000264 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000265 int nMaxArgs = 0;
drh76ff3a02004-09-24 22:32:30 +0000266 Op *pOp;
267 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000268 int doesStatementRollback = 0;
269 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000270 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000271 u8 opcode = pOp->opcode;
272
drha2baf3a2008-06-18 15:34:09 +0000273 if( opcode==OP_Function || opcode==OP_AggStep ){
drh98757152008-01-09 23:04:12 +0000274 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
danielk1977399918f2006-06-14 13:03:23 +0000275#ifndef SQLITE_OMIT_VIRTUALTABLE
drha2baf3a2008-06-18 15:34:09 +0000276 }else if( opcode==OP_VUpdate ){
danielk1977bc04f852005-03-29 08:26:13 +0000277 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drha2baf3a2008-06-18 15:34:09 +0000278#endif
danielk19775dfecba2008-06-23 13:57:21 +0000279 }
danielk1977182c4ba2007-06-27 15:53:34 +0000280 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000281 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
282 doesStatementRollback = 1;
283 }
drh38449902005-06-07 01:43:41 +0000284 }else if( opcode==OP_Statement ){
285 hasStatementBegin = 1;
drh77658e22007-12-04 16:54:52 +0000286 }else if( opcode==OP_Destroy ){
287 doesStatementRollback = 1;
danielk1977182c4ba2007-06-27 15:53:34 +0000288#ifndef SQLITE_OMIT_VIRTUALTABLE
289 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
290 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000291 }else if( opcode==OP_VFilter ){
292 int n;
293 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000294 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000295 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000296 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000297#endif
danielk1977bc04f852005-03-29 08:26:13 +0000298 }
danielk1977634f2982005-03-28 08:44:07 +0000299
drhd2981512008-01-04 19:33:49 +0000300 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
301 assert( -1-pOp->p2<p->nLabel );
302 pOp->p2 = aLabel[-1-pOp->p2];
303 }
drh76ff3a02004-09-24 22:32:30 +0000304 }
drh633e6d52008-07-28 19:34:53 +0000305 sqlite3DbFree(p->db, p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000306 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000307
308 *pMaxFuncArgs = nMaxArgs;
drh38449902005-06-07 01:43:41 +0000309
310 /* If we never rollback a statement transaction, then statement
311 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000312 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000313 ** which can be expensive on some platforms.
314 */
315 if( hasStatementBegin && !doesStatementRollback ){
316 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
317 if( pOp->opcode==OP_Statement ){
318 pOp->opcode = OP_Noop;
319 }
320 }
321 }
drh76ff3a02004-09-24 22:32:30 +0000322}
323
324/*
drh9a324642003-09-06 20:12:01 +0000325** Return the address of the next instruction to be inserted.
326*/
danielk19774adee202004-05-08 08:23:19 +0000327int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000328 assert( p->magic==VDBE_MAGIC_INIT );
329 return p->nOp;
330}
331
332/*
333** Add a whole list of operations to the operation stack. Return the
334** address of the first operation added.
335*/
danielk19774adee202004-05-08 08:23:19 +0000336int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000337 int addr;
338 assert( p->magic==VDBE_MAGIC_INIT );
drha4e5d582007-10-20 15:41:57 +0000339 if( p->nOp + nOp > p->nOpAlloc ){
drheee4c8c2008-02-18 22:24:57 +0000340 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
341 assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed );
drha4e5d582007-10-20 15:41:57 +0000342 }
drh17435752007-08-16 04:30:38 +0000343 if( p->db->mallocFailed ){
drh76ff3a02004-09-24 22:32:30 +0000344 return 0;
drh9a324642003-09-06 20:12:01 +0000345 }
346 addr = p->nOp;
347 if( nOp>0 ){
348 int i;
drh905793e2004-02-21 13:31:09 +0000349 VdbeOpList const *pIn = aOp;
350 for(i=0; i<nOp; i++, pIn++){
351 int p2 = pIn->p2;
352 VdbeOp *pOut = &p->aOp[i+addr];
353 pOut->opcode = pIn->opcode;
354 pOut->p1 = pIn->p1;
drh8558cde2008-01-05 05:20:10 +0000355 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
356 pOut->p2 = addr + ADDR(p2);
357 }else{
358 pOut->p2 = p2;
359 }
drh24003452008-01-03 01:28:59 +0000360 pOut->p3 = pIn->p3;
361 pOut->p4type = P4_NOTUSED;
362 pOut->p4.p = 0;
363 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000364#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000365 pOut->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000366 if( sqlite3VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000367 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000368 }
369#endif
370 }
371 p->nOp += nOp;
372 }
373 return addr;
374}
375
376/*
377** Change the value of the P1 operand for a specific instruction.
378** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000379** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000380** few minor changes to the program.
381*/
danielk19774adee202004-05-08 08:23:19 +0000382void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000383 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000384 if( p && addr>=0 && p->nOp>addr && p->aOp ){
385 p->aOp[addr].p1 = val;
386 }
387}
388
389/*
390** Change the value of the P2 operand for a specific instruction.
391** This routine is useful for setting a jump destination.
392*/
danielk19774adee202004-05-08 08:23:19 +0000393void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000394 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000395 if( p && addr>=0 && p->nOp>addr && p->aOp ){
396 p->aOp[addr].p2 = val;
397 }
398}
399
drhd654be82005-09-20 17:42:23 +0000400/*
danielk19771f4aa332008-01-03 09:51:55 +0000401** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000402*/
403void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
404 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
405 if( p && addr>=0 && p->nOp>addr && p->aOp ){
406 p->aOp[addr].p3 = val;
407 }
408}
409
410/*
drh35573352008-01-08 23:54:25 +0000411** Change the value of the P5 operand for the most recently
412** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000413*/
drh35573352008-01-08 23:54:25 +0000414void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
danielk19771f4aa332008-01-03 09:51:55 +0000415 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh35573352008-01-08 23:54:25 +0000416 if( p && p->aOp ){
417 assert( p->nOp>0 );
418 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000419 }
420}
421
422/*
drhf8875402006-03-17 13:56:34 +0000423** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000424** the address of the next instruction to be coded.
425*/
426void sqlite3VdbeJumpHere(Vdbe *p, int addr){
427 sqlite3VdbeChangeP2(p, addr, p->nOp);
428}
drhb38ad992005-09-16 00:27:01 +0000429
drhb7f6f682006-07-08 17:06:43 +0000430
431/*
432** If the input FuncDef structure is ephemeral, then free it. If
433** the FuncDef is not ephermal, then do nothing.
434*/
drh633e6d52008-07-28 19:34:53 +0000435static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhb7f6f682006-07-08 17:06:43 +0000436 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000437 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000438 }
439}
440
drhb38ad992005-09-16 00:27:01 +0000441/*
drh66a51672008-01-03 00:01:23 +0000442** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000443*/
drh633e6d52008-07-28 19:34:53 +0000444static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000445 if( p4 ){
drh66a51672008-01-03 00:01:23 +0000446 switch( p4type ){
447 case P4_REAL:
448 case P4_INT64:
449 case P4_MPRINTF:
450 case P4_DYNAMIC:
451 case P4_KEYINFO:
drh0acb7e42008-06-25 00:12:41 +0000452 case P4_INTARRAY:
drh66a51672008-01-03 00:01:23 +0000453 case P4_KEYINFO_HANDOFF: {
drh633e6d52008-07-28 19:34:53 +0000454 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000455 break;
456 }
drh66a51672008-01-03 00:01:23 +0000457 case P4_VDBEFUNC: {
drh0acb7e42008-06-25 00:12:41 +0000458 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
drh633e6d52008-07-28 19:34:53 +0000459 freeEphemeralFunction(db, pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000460 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh633e6d52008-07-28 19:34:53 +0000461 sqlite3DbFree(db, pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000462 break;
463 }
drh66a51672008-01-03 00:01:23 +0000464 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000465 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000466 break;
467 }
drh66a51672008-01-03 00:01:23 +0000468 case P4_MEM: {
drh0acb7e42008-06-25 00:12:41 +0000469 sqlite3ValueFree((sqlite3_value*)p4);
drhac1733d2005-09-17 17:58:22 +0000470 break;
471 }
drhb38ad992005-09-16 00:27:01 +0000472 }
473 }
474}
475
476
drh9a324642003-09-06 20:12:01 +0000477/*
drhf8875402006-03-17 13:56:34 +0000478** Change N opcodes starting at addr to No-ops.
479*/
480void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000481 if( p && p->aOp ){
482 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000483 sqlite3 *db = p->db;
danielk197792d4d7a2007-05-04 12:05:56 +0000484 while( N-- ){
drh633e6d52008-07-28 19:34:53 +0000485 freeP4(db, pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000486 memset(pOp, 0, sizeof(pOp[0]));
487 pOp->opcode = OP_Noop;
488 pOp++;
489 }
drhf8875402006-03-17 13:56:34 +0000490 }
491}
492
493/*
drh66a51672008-01-03 00:01:23 +0000494** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000495** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000496** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000497** few minor changes to the program.
498**
drh66a51672008-01-03 00:01:23 +0000499** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000500** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000501** A value of n==0 means copy bytes of zP4 up to and including the
502** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000503**
drh66a51672008-01-03 00:01:23 +0000504** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000505** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000506** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000507** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000508** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000509** caller should not free the allocation, it will be freed when the Vdbe is
510** finalized.
511**
drh66a51672008-01-03 00:01:23 +0000512** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000513** to a string or structure that is guaranteed to exist for the lifetime of
514** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000515**
drh66a51672008-01-03 00:01:23 +0000516** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000517*/
drh66a51672008-01-03 00:01:23 +0000518void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000519 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000520 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000521 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000522 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000523 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000524 if( p->aOp==0 || db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000525 if (n != P4_KEYINFO) {
drh633e6d52008-07-28 19:34:53 +0000526 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000527 }
danielk1977d5d56522005-03-16 12:15:20 +0000528 return;
529 }
drh91fd4d42008-01-19 20:11:25 +0000530 assert( addr<p->nOp );
531 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000532 addr = p->nOp - 1;
533 if( addr<0 ) return;
534 }
535 pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000536 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000537 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000538 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000539 /* Note: this cast is safe, because the origin data point was an int
540 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000541 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh98757152008-01-09 23:04:12 +0000542 pOp->p4type = n;
543 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000544 pOp->p4.p = 0;
545 pOp->p4type = P4_NOTUSED;
546 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000547 KeyInfo *pKeyInfo;
548 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000549
drh66a51672008-01-03 00:01:23 +0000550 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000551 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhe5ae5732008-06-15 02:51:47 +0000552 pKeyInfo = sqlite3Malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000553 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000554 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000555 u8 *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000556 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000557 aSortOrder = pKeyInfo->aSortOrder;
558 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000559 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000560 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
561 }
drh66a51672008-01-03 00:01:23 +0000562 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000563 }else{
drh17435752007-08-16 04:30:38 +0000564 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000565 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000566 }
drh66a51672008-01-03 00:01:23 +0000567 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000568 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000569 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000570 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000571 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000572 pOp->p4type = n;
drh9a324642003-09-06 20:12:01 +0000573 }else{
drh66a51672008-01-03 00:01:23 +0000574 if( n==0 ) n = strlen(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000575 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000576 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000577 }
578}
579
drhad6d9462004-09-19 02:15:24 +0000580#ifndef NDEBUG
581/*
drh16ee60f2008-06-20 18:13:25 +0000582** Change the comment on the the most recently coded instruction. Or
583** insert a No-op and add the comment to that new instruction. This
584** makes the code easier to read during debugging. None of this happens
585** in a production build.
drhad6d9462004-09-19 02:15:24 +0000586*/
587void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
588 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000589 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000590 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000591 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000592 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000593 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000594 sqlite3DbFree(p->db, *pz);
drh8cc74322008-01-15 02:22:24 +0000595 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000596 va_end(ap);
597 }
drhad6d9462004-09-19 02:15:24 +0000598}
drh16ee60f2008-06-20 18:13:25 +0000599void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
600 va_list ap;
601 sqlite3VdbeAddOp0(p, OP_Noop);
602 assert( p->nOp>0 || p->aOp==0 );
603 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
604 if( p->nOp ){
605 char **pz = &p->aOp[p->nOp-1].zComment;
606 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000607 sqlite3DbFree(p->db, *pz);
drh16ee60f2008-06-20 18:13:25 +0000608 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
609 va_end(ap);
610 }
611}
612#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000613
drh9a324642003-09-06 20:12:01 +0000614/*
drh9a324642003-09-06 20:12:01 +0000615** Return the opcode for a given address.
616*/
danielk19774adee202004-05-08 08:23:19 +0000617VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000618 assert( p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000619 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
danielk197701256832007-04-18 14:24:32 +0000620 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000621}
622
drhb7f91642004-10-31 02:22:47 +0000623#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
624 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000625/*
drh66a51672008-01-03 00:01:23 +0000626** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000627** Use zTemp for any required temporary buffer space.
628*/
drh66a51672008-01-03 00:01:23 +0000629static char *displayP4(Op *pOp, char *zTemp, int nTemp){
630 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000631 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000632 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000633 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000634 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000635 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000636 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000637 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhd3d39e92004-05-20 22:16:29 +0000638 i = strlen(zTemp);
639 for(j=0; j<pKeyInfo->nField; j++){
640 CollSeq *pColl = pKeyInfo->aColl[j];
641 if( pColl ){
642 int n = strlen(pColl->zName);
643 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000644 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000645 break;
646 }
647 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000648 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000649 zTemp[i++] = '-';
650 }
drh5bb3eb92007-05-04 13:15:55 +0000651 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000652 i += n;
653 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000654 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000655 i += 4;
656 }
657 }
658 zTemp[i++] = ')';
659 zTemp[i] = 0;
660 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000661 break;
662 }
drh66a51672008-01-03 00:01:23 +0000663 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000664 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000665 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000666 break;
667 }
drh66a51672008-01-03 00:01:23 +0000668 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000669 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000670 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000671 break;
672 }
drh66a51672008-01-03 00:01:23 +0000673 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000674 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000675 break;
676 }
drh66a51672008-01-03 00:01:23 +0000677 case P4_INT32: {
678 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000679 break;
680 }
drh66a51672008-01-03 00:01:23 +0000681 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000682 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000683 break;
684 }
drh66a51672008-01-03 00:01:23 +0000685 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000686 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000687 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000688 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000689 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000690 }else if( pMem->flags & MEM_Int ){
691 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
692 }else if( pMem->flags & MEM_Real ){
693 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhd4e70eb2008-01-02 00:34:36 +0000694 }
drh598f1342007-10-23 15:39:45 +0000695 break;
696 }
drha967e882006-06-13 01:04:52 +0000697#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000698 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000699 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000700 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000701 break;
702 }
703#endif
drh0acb7e42008-06-25 00:12:41 +0000704 case P4_INTARRAY: {
705 sqlite3_snprintf(nTemp, zTemp, "intarray");
706 break;
707 }
drhd3d39e92004-05-20 22:16:29 +0000708 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000709 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000710 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000711 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000712 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000713 }
714 }
715 }
drh66a51672008-01-03 00:01:23 +0000716 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000717 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000718}
drhb7f91642004-10-31 02:22:47 +0000719#endif
drhd3d39e92004-05-20 22:16:29 +0000720
drh900b31e2007-08-28 02:27:51 +0000721/*
drhd0679ed2007-08-28 22:24:34 +0000722** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
723**
drh900b31e2007-08-28 02:27:51 +0000724*/
drhfb982642007-08-30 01:19:59 +0000725void sqlite3VdbeUsesBtree(Vdbe *p, int i){
726 int mask;
drhd0679ed2007-08-28 22:24:34 +0000727 assert( i>=0 && i<p->db->nDb );
728 assert( i<sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000729 mask = 1<<i;
730 if( (p->btreeMask & mask)==0 ){
731 p->btreeMask |= mask;
732 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
733 }
drh900b31e2007-08-28 02:27:51 +0000734}
735
drhd3d39e92004-05-20 22:16:29 +0000736
danielk19778b60e0f2005-01-12 09:10:39 +0000737#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000738/*
739** Print a single opcode. This routine is used for debugging only.
740*/
danielk19774adee202004-05-08 08:23:19 +0000741void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000742 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000743 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000744 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000745 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000746 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000747 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000748 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
749#ifdef SQLITE_DEBUG
750 pOp->zComment ? pOp->zComment : ""
751#else
752 ""
753#endif
754 );
drh9a324642003-09-06 20:12:01 +0000755 fflush(pOut);
756}
757#endif
758
759/*
drh76ff3a02004-09-24 22:32:30 +0000760** Release an array of N Mem elements
761*/
drhc890fec2008-08-01 20:10:08 +0000762static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +0000763 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +0000764 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +0000765 sqlite3 *db = p->db;
766 int malloc_failed = db->mallocFailed;
danielk1977e972e032008-09-19 18:32:26 +0000767 for(pEnd=&p[N]; p<pEnd; p++){
768 assert( (&p[1])==pEnd || p[0].db==p[1].db );
769
770 /* This block is really an inlined version of sqlite3VdbeMemRelease()
771 ** that takes advantage of the fact that the memory cell value is
772 ** being set to NULL after releasing any dynamic resources.
773 **
774 ** The justification for duplicating code is that according to
775 ** callgrind, this causes a certain test case to hit the CPU 4.7
776 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
777 ** sqlite3MemRelease() were called from here. With -O2, this jumps
778 ** to 6.6 percent. The test case is inserting 1000 rows into a table
779 ** with no indexes using a single prepared INSERT statement, bind()
780 ** and reset(). Inserts are grouped into a transaction.
781 */
782 if( p->flags&(MEM_Agg|MEM_Dyn) ){
783 sqlite3VdbeMemRelease(p);
784 }else if( p->zMalloc ){
785 sqlite3DbFree(db, p->zMalloc);
786 p->zMalloc = 0;
787 }
788
danielk19775f096132008-03-28 15:44:09 +0000789 p->flags = MEM_Null;
drh76ff3a02004-09-24 22:32:30 +0000790 }
danielk1977a7a8e142008-02-13 18:25:27 +0000791 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +0000792 }
793}
794
danielk1977dfb316d2008-03-26 18:34:43 +0000795#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
796int sqlite3VdbeReleaseBuffers(Vdbe *p){
797 int ii;
798 int nFree = 0;
799 assert( sqlite3_mutex_held(p->db->mutex) );
800 for(ii=1; ii<=p->nMem; ii++){
801 Mem *pMem = &p->aMem[ii];
802 if( pMem->z && pMem->flags&MEM_Dyn ){
803 assert( !pMem->xDel );
drh633e6d52008-07-28 19:34:53 +0000804 nFree += sqlite3DbMallocSize(pMem->db, pMem->z);
danielk1977dfb316d2008-03-26 18:34:43 +0000805 sqlite3VdbeMemRelease(pMem);
806 }
807 }
808 return nFree;
809}
810#endif
811
drhb7f91642004-10-31 02:22:47 +0000812#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000813/*
drh9a324642003-09-06 20:12:01 +0000814** Give a listing of the program in the virtual machine.
815**
danielk19774adee202004-05-08 08:23:19 +0000816** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000817** running the code, it invokes the callback once for each instruction.
818** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +0000819**
820** When p->explain==1, each instruction is listed. When
821** p->explain==2, only OP_Explain instructions are listed and these
822** are shown in a different format. p->explain==2 is used to implement
823** EXPLAIN QUERY PLAN.
drh9a324642003-09-06 20:12:01 +0000824*/
danielk19774adee202004-05-08 08:23:19 +0000825int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000826 Vdbe *p /* The VDBE */
827){
drh9bb575f2004-09-06 17:24:11 +0000828 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000829 int i;
drh826fb5a2004-02-14 23:59:57 +0000830 int rc = SQLITE_OK;
drh9cbf3422008-01-17 16:22:13 +0000831 Mem *pMem = p->pResultSet = &p->aMem[1];
drh9a324642003-09-06 20:12:01 +0000832
drh9a324642003-09-06 20:12:01 +0000833 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000834 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
835 assert( db->magic==SQLITE_MAGIC_BUSY );
836 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000837
drh9cbf3422008-01-17 16:22:13 +0000838 /* Even though this opcode does not use dynamic strings for
839 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000840 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000841 */
drhc890fec2008-08-01 20:10:08 +0000842 releaseMemArray(pMem, p->nMem);
danielk197718f41892004-05-22 07:27:46 +0000843
drhecc92422005-09-10 16:46:12 +0000844 do{
845 i = p->pc++;
846 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000847 if( i>=p->nOp ){
848 p->rc = SQLITE_OK;
849 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000850 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000851 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000852 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +0000853 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +0000854 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000855 char *z;
drhd3d39e92004-05-20 22:16:29 +0000856 Op *pOp = &p->aOp[i];
danielk19770d78bae2008-01-03 07:09:48 +0000857 if( p->explain==1 ){
858 pMem->flags = MEM_Int;
859 pMem->type = SQLITE_INTEGER;
860 pMem->u.i = i; /* Program counter */
861 pMem++;
862
863 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
864 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
865 assert( pMem->z!=0 );
866 pMem->n = strlen(pMem->z);
867 pMem->type = SQLITE_TEXT;
868 pMem->enc = SQLITE_UTF8;
869 pMem++;
870 }
drheb2e1762004-05-27 01:53:56 +0000871
872 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000873 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000874 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000875 pMem++;
876
877 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000878 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000879 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000880 pMem++;
881
danielk19770d78bae2008-01-03 07:09:48 +0000882 if( p->explain==1 ){
883 pMem->flags = MEM_Int;
884 pMem->u.i = pOp->p3; /* P3 */
885 pMem->type = SQLITE_INTEGER;
886 pMem++;
887 }
888
danielk1977a7a8e142008-02-13 18:25:27 +0000889 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
890 p->db->mallocFailed = 1;
891 return SQLITE_NOMEM;
892 }
893 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
894 z = displayP4(pOp, pMem->z, 32);
895 if( z!=pMem->z ){
896 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
897 }else{
898 assert( pMem->z!=0 );
899 pMem->n = strlen(pMem->z);
900 pMem->enc = SQLITE_UTF8;
901 }
drh9c054832004-05-31 18:51:57 +0000902 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +0000903 pMem++;
drheb2e1762004-05-27 01:53:56 +0000904
danielk19770d78bae2008-01-03 07:09:48 +0000905 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +0000906 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +0000907 p->db->mallocFailed = 1;
908 return SQLITE_NOMEM;
909 }
910 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +0000911 pMem->n = 2;
912 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +0000913 pMem->type = SQLITE_TEXT;
914 pMem->enc = SQLITE_UTF8;
915 pMem++;
916
drhaa9b8962008-01-08 02:57:55 +0000917#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000918 if( pOp->zComment ){
919 pMem->flags = MEM_Str|MEM_Term;
920 pMem->z = pOp->zComment;
921 pMem->n = strlen(pMem->z);
922 pMem->enc = SQLITE_UTF8;
danielk19771e522b42008-09-16 09:09:19 +0000923 pMem->type = SQLITE_TEXT;
drh52391cb2008-02-14 23:44:13 +0000924 }else
drhaa9b8962008-01-08 02:57:55 +0000925#endif
drh52391cb2008-02-14 23:44:13 +0000926 {
927 pMem->flags = MEM_Null; /* Comment */
928 pMem->type = SQLITE_NULL;
929 }
danielk19770d78bae2008-01-03 07:09:48 +0000930 }
931
932 p->nResColumn = 8 - 5*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +0000933 p->rc = SQLITE_OK;
934 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000935 }
drh826fb5a2004-02-14 23:59:57 +0000936 return rc;
drh9a324642003-09-06 20:12:01 +0000937}
drhb7f91642004-10-31 02:22:47 +0000938#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000939
drh7c4ac0c2007-04-05 11:25:58 +0000940#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000941/*
drh3f7d4e42004-07-24 14:35:58 +0000942** Print the SQL that was used to generate a VDBE program.
943*/
944void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000945 int nOp = p->nOp;
946 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000947 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000948 pOp = &p->aOp[0];
949 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000950 const char *z = pOp->p4.z;
drh4c755c02004-08-08 20:22:17 +0000951 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000952 printf("SQL: [%s]\n", z);
953 }
drh3f7d4e42004-07-24 14:35:58 +0000954}
drh7c4ac0c2007-04-05 11:25:58 +0000955#endif
drh3f7d4e42004-07-24 14:35:58 +0000956
drh602c2372007-03-01 00:29:13 +0000957#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
958/*
959** Print an IOTRACE message showing SQL content.
960*/
961void sqlite3VdbeIOTraceSql(Vdbe *p){
962 int nOp = p->nOp;
963 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +0000964 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +0000965 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000966 pOp = &p->aOp[0];
967 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +0000968 int i, j;
drh00a18e42007-08-13 11:10:34 +0000969 char z[1000];
drh949f9cd2008-01-12 21:35:57 +0000970 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk19772be2be92007-05-16 17:50:45 +0000971 for(i=0; isspace((unsigned char)z[i]); i++){}
drh602c2372007-03-01 00:29:13 +0000972 for(j=0; z[i]; i++){
danielk19772be2be92007-05-16 17:50:45 +0000973 if( isspace((unsigned char)z[i]) ){
drh602c2372007-03-01 00:29:13 +0000974 if( z[i-1]!=' ' ){
975 z[j++] = ' ';
976 }
977 }else{
978 z[j++] = z[i];
979 }
980 }
981 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000982 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +0000983 }
984}
985#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
986
987
drh3f7d4e42004-07-24 14:35:58 +0000988/*
drh9a324642003-09-06 20:12:01 +0000989** Prepare a virtual machine for execution. This involves things such
990** as allocating stack space and initializing the program counter.
991** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000992** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000993**
994** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
995** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000996*/
danielk19774adee202004-05-08 08:23:19 +0000997void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000998 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000999 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +00001000 int nMem, /* Number of memory cells to allocate */
1001 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +00001002 int isExplain /* True if the EXPLAIN keywords is present */
1003){
1004 int n;
danielk19771e536952007-08-16 10:09:01 +00001005 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001006
1007 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001008 assert( p->magic==VDBE_MAGIC_INIT );
1009
drhc16a03b2004-09-15 13:38:10 +00001010 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001011 */
drhc16a03b2004-09-15 13:38:10 +00001012 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001013
danielk1977634f2982005-03-28 08:44:07 +00001014 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
1015 * is because the call to resizeOpArray() below may shrink the
1016 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
1017 * state.
1018 */
1019 p->magic = VDBE_MAGIC_RUN;
1020
danielk1977cd3e8f72008-03-25 09:47:35 +00001021 /* For each cursor required, also allocate a memory cell. Memory
1022 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1023 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001024 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001025 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1026 ** stores the blob of memory associated with cursor 1, etc.
1027 **
1028 ** See also: allocateCursor().
1029 */
1030 nMem += nCursor;
1031
drh9cbf3422008-01-17 16:22:13 +00001032 /*
1033 ** Allocation space for registers.
drh9a324642003-09-06 20:12:01 +00001034 */
drh9cbf3422008-01-17 16:22:13 +00001035 if( p->aMem==0 ){
danielk1977634f2982005-03-28 08:44:07 +00001036 int nArg; /* Maximum number of args passed to a user function. */
drh9cbf3422008-01-17 16:22:13 +00001037 resolveP2Values(p, &nArg);
drh26c9b5e2008-04-11 14:56:53 +00001038 /*resizeOpArray(p, p->nOp);*/
drh82a48512003-09-06 22:45:20 +00001039 assert( nVar>=0 );
drh9cbf3422008-01-17 16:22:13 +00001040 if( isExplain && nMem<10 ){
1041 p->nMem = nMem = 10;
drh0f7eb612006-08-08 13:51:43 +00001042 }
drh9cbf3422008-01-17 16:22:13 +00001043 p->aMem = sqlite3DbMallocZero(db,
1044 nMem*sizeof(Mem) /* aMem */
drh86f43302004-10-05 17:37:36 +00001045 + nVar*sizeof(Mem) /* aVar */
drh9cbf3422008-01-17 16:22:13 +00001046 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +00001047 + nVar*sizeof(char*) /* azVar */
drhdfe88ec2008-11-03 20:55:06 +00001048 + nCursor*sizeof(VdbeCursor*)+1 /* apCsr */
drh82a48512003-09-06 22:45:20 +00001049 );
drh17435752007-08-16 04:30:38 +00001050 if( !db->mallocFailed ){
drh9cbf3422008-01-17 16:22:13 +00001051 p->aMem--; /* aMem[] goes from 1..nMem */
1052 p->nMem = nMem; /* not from 0..nMem-1 */
drh0a07c102008-01-03 18:03:08 +00001053 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +00001054 p->nVar = nVar;
1055 p->okVar = 0;
1056 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +00001057 p->azVar = (char**)&p->apArg[nArg];
drhdfe88ec2008-11-03 20:55:06 +00001058 p->apCsr = (VdbeCursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +00001059 p->nCursor = nCursor;
1060 for(n=0; n<nVar; n++){
1061 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001062 p->aVar[n].db = db;
1063 }
drh9cbf3422008-01-17 16:22:13 +00001064 for(n=1; n<=nMem; n++){
1065 p->aMem[n].flags = MEM_Null;
1066 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +00001067 }
danielk197754db47e2004-05-19 10:36:43 +00001068 }
drh82a48512003-09-06 22:45:20 +00001069 }
drh9cbf3422008-01-17 16:22:13 +00001070#ifdef SQLITE_DEBUG
1071 for(n=1; n<p->nMem; n++){
1072 assert( p->aMem[n].db==db );
danielk1977b3bce662005-01-29 08:32:43 +00001073 }
drh9cbf3422008-01-17 16:22:13 +00001074#endif
drh9a324642003-09-06 20:12:01 +00001075
danielk19771d850a72004-05-31 08:26:49 +00001076 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001077 p->rc = SQLITE_OK;
1078 p->uniqueCnt = 0;
drh9a324642003-09-06 20:12:01 +00001079 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001080 p->explain |= isExplain;
1081 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001082 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001083 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001084 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +00001085 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +00001086#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001087 {
1088 int i;
1089 for(i=0; i<p->nOp; i++){
1090 p->aOp[i].cnt = 0;
1091 p->aOp[i].cycles = 0;
1092 }
drh9a324642003-09-06 20:12:01 +00001093 }
1094#endif
1095}
1096
drh9a324642003-09-06 20:12:01 +00001097/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001098** Close a VDBE cursor and release all the resources that cursor
1099** happens to hold.
drh9a324642003-09-06 20:12:01 +00001100*/
drhdfe88ec2008-11-03 20:55:06 +00001101void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001102 if( pCx==0 ){
1103 return;
1104 }
drh9a324642003-09-06 20:12:01 +00001105 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001106 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001107 /* The pCx->pCursor will be close automatically, if it exists, by
1108 ** the call above. */
1109 }else if( pCx->pCursor ){
1110 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001111 }
drh9eff6162006-06-12 21:59:13 +00001112#ifndef SQLITE_OMIT_VIRTUALTABLE
1113 if( pCx->pVtabCursor ){
1114 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001115 const sqlite3_module *pModule = pCx->pModule;
1116 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001117 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001118 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001119 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001120 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001121 }
1122#endif
danielk19779882d992008-03-27 17:59:01 +00001123 if( !pCx->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00001124 sqlite3DbFree(p->db, pCx->pData);
danielk19779882d992008-03-27 17:59:01 +00001125 }
drh9a324642003-09-06 20:12:01 +00001126}
1127
1128/*
drhff0587c2007-08-29 17:43:19 +00001129** Close all cursors except for VTab cursors that are currently
1130** in use.
drh9a324642003-09-06 20:12:01 +00001131*/
drhff0587c2007-08-29 17:43:19 +00001132static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001133 int i;
drh290c1942004-08-21 17:54:45 +00001134 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001135 for(i=0; i<p->nCursor; i++){
drhdfe88ec2008-11-03 20:55:06 +00001136 VdbeCursor *pC = p->apCsr[i];
drhff0587c2007-08-29 17:43:19 +00001137 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1138 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001139 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001140 }
drh9a324642003-09-06 20:12:01 +00001141 }
drh9a324642003-09-06 20:12:01 +00001142}
1143
1144/*
drh9a324642003-09-06 20:12:01 +00001145** Clean up the VM after execution.
1146**
1147** This routine will automatically close any cursors, lists, and/or
1148** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001149** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001150*/
drhc890fec2008-08-01 20:10:08 +00001151static void Cleanup(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001152 int i;
drh633e6d52008-07-28 19:34:53 +00001153 sqlite3 *db = p->db;
drhff0587c2007-08-29 17:43:19 +00001154 closeAllCursorsExceptActiveVtabs(p);
danielk1977a7a8e142008-02-13 18:25:27 +00001155 for(i=1; i<=p->nMem; i++){
1156 MemSetTypeFlag(&p->aMem[i], MEM_Null);
1157 }
drhc890fec2008-08-01 20:10:08 +00001158 releaseMemArray(&p->aMem[1], p->nMem);
drha01f79d2005-07-08 13:07:59 +00001159 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001160 if( p->contextStack ){
1161 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001162 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001163 }
drh633e6d52008-07-28 19:34:53 +00001164 sqlite3DbFree(db, p->contextStack);
drh344737f2004-09-19 00:50:20 +00001165 }
drh5f968432004-02-21 19:02:30 +00001166 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001167 p->contextStackDepth = 0;
1168 p->contextStackTop = 0;
drh633e6d52008-07-28 19:34:53 +00001169 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001170 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001171 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001172}
1173
1174/*
danielk197722322fd2004-05-25 23:35:17 +00001175** Set the number of result columns that will be returned by this SQL
1176** statement. This is now set at compile time, rather than during
1177** execution of the vdbe program so that sqlite3_column_count() can
1178** be called on an SQL statement before sqlite3_step().
1179*/
1180void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001181 Mem *pColName;
1182 int n;
drh633e6d52008-07-28 19:34:53 +00001183 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001184
drhc890fec2008-08-01 20:10:08 +00001185 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001186 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001187 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001188 p->nResColumn = nResColumn;
drh633e6d52008-07-28 19:34:53 +00001189 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001190 if( p->aColName==0 ) return;
1191 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001192 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001193 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001194 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001195 }
danielk197722322fd2004-05-25 23:35:17 +00001196}
1197
1198/*
danielk19773cf86062004-05-26 10:11:05 +00001199** Set the name of the idx'th column to be returned by the SQL statement.
1200** zName must be a pointer to a nul terminated string.
1201**
1202** This call must be made after a call to sqlite3VdbeSetNumCols().
1203**
danielk197710fb7492008-10-31 10:53:22 +00001204** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1205** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1206** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001207*/
danielk197710fb7492008-10-31 10:53:22 +00001208int sqlite3VdbeSetColName(
1209 Vdbe *p, /* Vdbe being configured */
1210 int idx, /* Index of column zName applies to */
1211 int var, /* One of the COLNAME_* constants */
1212 const char *zName, /* Pointer to buffer containing name */
1213 void (*xDel)(void*) /* Memory management strategy for zName */
1214){
danielk19773cf86062004-05-26 10:11:05 +00001215 int rc;
1216 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001217 assert( idx<p->nResColumn );
1218 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001219 if( p->db->mallocFailed ){
1220 assert( !zName || xDel!=SQLITE_DYNAMIC );
1221 return SQLITE_NOMEM;
1222 }
drh76ff3a02004-09-24 22:32:30 +00001223 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001224 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001225 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
1226 assert( p->db->mallocFailed || !zName || pColName->flags&MEM_Term );
danielk19773cf86062004-05-26 10:11:05 +00001227 return rc;
1228}
1229
danielk197713adf8a2004-06-03 16:08:41 +00001230/*
1231** A read or write transaction may or may not be active on database handle
1232** db. If a transaction is active, commit it. If there is a
1233** write-transaction spanning more than one database file, this routine
1234** takes care of the master journal trickery.
1235*/
danielk19773e3a84d2008-08-01 17:37:40 +00001236static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001237 int i;
1238 int nTrans = 0; /* Number of databases with an active write-transaction */
1239 int rc = SQLITE_OK;
1240 int needXcommit = 0;
1241
danielk19775bd270b2006-07-25 15:14:52 +00001242 /* Before doing anything else, call the xSync() callback for any
1243 ** virtual module tables written in this transaction. This has to
1244 ** be done before determining whether a master journal file is
1245 ** required, as an xSync() callback may add an attached database
1246 ** to the transaction.
1247 */
danielk19773e3a84d2008-08-01 17:37:40 +00001248 rc = sqlite3VtabSync(db, &p->zErrMsg);
danielk19775bd270b2006-07-25 15:14:52 +00001249 if( rc!=SQLITE_OK ){
1250 return rc;
1251 }
1252
1253 /* This loop determines (a) if the commit hook should be invoked and
1254 ** (b) how many database files have open write transactions, not
1255 ** including the temp database. (b) is important because if more than
1256 ** one database file has an open write transaction, a master journal
1257 ** file is required for an atomic commit.
1258 */
danielk197713adf8a2004-06-03 16:08:41 +00001259 for(i=0; i<db->nDb; i++){
1260 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001261 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001262 needXcommit = 1;
1263 if( i!=1 ) nTrans++;
1264 }
1265 }
1266
1267 /* If there are any write-transactions at all, invoke the commit hook */
1268 if( needXcommit && db->xCommitCallback ){
drh7e8b8482008-01-23 03:03:05 +00001269 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001270 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001271 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001272 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001273 return SQLITE_CONSTRAINT;
1274 }
1275 }
1276
danielk197740b38dc2004-06-26 08:38:24 +00001277 /* The simple case - no more than one database file (not counting the
1278 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001279 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001280 **
danielk197740b38dc2004-06-26 08:38:24 +00001281 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001282 ** string, it means the main database is :memory: or a temp file. In
1283 ** that case we do not support atomic multi-file commits, so use the
1284 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001285 */
danielk197740b38dc2004-06-26 08:38:24 +00001286 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001287 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001288 Btree *pBt = db->aDb[i].pBt;
1289 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001290 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001291 }
1292 }
1293
drh80e35f42007-03-30 14:06:34 +00001294 /* Do the commit only if all databases successfully complete phase 1.
1295 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1296 ** IO error while deleting or truncating a journal file. It is unlikely,
1297 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001298 */
1299 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1300 Btree *pBt = db->aDb[i].pBt;
1301 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001302 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001303 }
danielk1977979f38e2007-03-27 16:19:51 +00001304 }
1305 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001306 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001307 }
1308 }
1309
1310 /* The complex case - There is a multi-file write-transaction active.
1311 ** This requires a master journal file to ensure the transaction is
1312 ** committed atomicly.
1313 */
danielk197744ee5bf2005-05-27 09:41:12 +00001314#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001315 else{
danielk1977b4b47412007-08-17 15:53:36 +00001316 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001317 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001318 char *zMaster = 0; /* File-name for the master journal */
1319 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001320 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001321 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001322 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001323
1324 /* Select a master journal file name */
1325 do {
drha6abd042004-06-09 17:37:22 +00001326 u32 random;
drh633e6d52008-07-28 19:34:53 +00001327 sqlite3DbFree(db, zMaster);
drh2fa18682008-03-19 14:15:34 +00001328 sqlite3_randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001329 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001330 if( !zMaster ){
1331 return SQLITE_NOMEM;
1332 }
danielk1977861f7452008-06-05 11:39:11 +00001333 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1334 }while( rc==SQLITE_OK && res );
1335 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001336 /* Open the master journal. */
1337 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1338 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1339 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1340 );
1341 }
danielk197713adf8a2004-06-03 16:08:41 +00001342 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001343 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001344 return rc;
1345 }
1346
1347 /* Write the name of each database file in the transaction into the new
1348 ** master journal file. If an error occurs at this point close
1349 ** and delete the master journal file. All the individual journal files
1350 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001351 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001352 */
danielk19771e536952007-08-16 10:09:01 +00001353 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001354 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001355 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001356 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001357 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001358 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001359 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1360 needSync = 1;
1361 }
danielk1977b4b47412007-08-17 15:53:36 +00001362 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001363 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001364 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001365 sqlite3OsCloseFree(pMaster);
1366 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001367 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001368 return rc;
1369 }
1370 }
1371 }
1372
danielk19779663b8f2007-08-24 11:52:28 +00001373 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1374 ** flag is set this is not required.
1375 */
danielk19775865e3d2004-06-14 06:03:57 +00001376 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001377 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001378 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001379 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001380 sqlite3OsCloseFree(pMaster);
1381 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001382 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001383 return rc;
1384 }
drhc9e06862004-06-09 20:03:08 +00001385
danielk197713adf8a2004-06-03 16:08:41 +00001386 /* Sync all the db files involved in the transaction. The same call
1387 ** sets the master journal pointer in each individual journal. If
1388 ** an error occurs here, do not delete the master journal file.
1389 **
drh80e35f42007-03-30 14:06:34 +00001390 ** If the error occurs during the first call to
1391 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1392 ** master journal file will be orphaned. But we cannot delete it,
1393 ** in case the master journal file name was written into the journal
1394 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001395 */
danielk19775bd270b2006-07-25 15:14:52 +00001396 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001397 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001398 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001399 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001400 }
1401 }
danielk1977fee2d252007-08-18 10:59:19 +00001402 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001403 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001404 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001405 return rc;
1406 }
danielk197713adf8a2004-06-03 16:08:41 +00001407
danielk1977962398d2004-06-14 09:35:16 +00001408 /* Delete the master journal file. This commits the transaction. After
1409 ** doing this the directory is synced again before any individual
1410 ** transaction files are deleted.
1411 */
danielk1977fee2d252007-08-18 10:59:19 +00001412 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00001413 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00001414 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001415 if( rc ){
1416 return rc;
1417 }
danielk197713adf8a2004-06-03 16:08:41 +00001418
1419 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001420 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1421 ** deleting or truncating journals. If something goes wrong while
1422 ** this is happening we don't really care. The integrity of the
1423 ** transaction is already guaranteed, but some stray 'cold' journals
1424 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001425 */
danielk1977979f38e2007-03-27 16:19:51 +00001426 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001427 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001428 for(i=0; i<db->nDb; i++){
1429 Btree *pBt = db->aDb[i].pBt;
1430 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001431 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001432 }
1433 }
danielk19772d1d86f2008-06-20 14:59:51 +00001434 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001435 enable_simulated_io_errors();
1436
danielk1977f9e7dda2006-06-16 16:08:53 +00001437 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001438 }
danielk197744ee5bf2005-05-27 09:41:12 +00001439#endif
danielk1977026d2702004-06-14 13:14:59 +00001440
drh2ac3ee92004-06-07 16:27:46 +00001441 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001442}
1443
danielk19771d850a72004-05-31 08:26:49 +00001444/*
1445** This routine checks that the sqlite3.activeVdbeCnt count variable
1446** matches the number of vdbe's in the list sqlite3.pVdbe that are
1447** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001448** This is an internal self-check only - it is not an essential processing
1449** step.
danielk19771d850a72004-05-31 08:26:49 +00001450**
1451** This is a no-op if NDEBUG is defined.
1452*/
1453#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001454static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001455 Vdbe *p;
1456 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001457 p = db->pVdbe;
1458 while( p ){
drh92f02c32004-09-02 14:57:08 +00001459 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001460 cnt++;
1461 }
1462 p = p->pNext;
1463 }
danielk19771d850a72004-05-31 08:26:49 +00001464 assert( cnt==db->activeVdbeCnt );
1465}
1466#else
1467#define checkActiveVdbeCnt(x)
1468#endif
1469
danielk19773cf86062004-05-26 10:11:05 +00001470/*
drhfb982642007-08-30 01:19:59 +00001471** For every Btree that in database connection db which
1472** has been modified, "trip" or invalidate each cursor in
1473** that Btree might have been modified so that the cursor
1474** can never be used again. This happens when a rollback
1475*** occurs. We have to trip all the other cursors, even
1476** cursor from other VMs in different database connections,
1477** so that none of them try to use the data at which they
1478** were pointing and which now may have been changed due
1479** to the rollback.
1480**
1481** Remember that a rollback can delete tables complete and
1482** reorder rootpages. So it is not sufficient just to save
1483** the state of the cursor. We have to invalidate the cursor
1484** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001485*/
drhade6c9c2007-11-24 10:23:44 +00001486static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001487 int i;
1488 for(i=0; i<db->nDb; i++){
1489 Btree *p = db->aDb[i].pBt;
1490 if( p && sqlite3BtreeIsInTrans(p) ){
1491 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1492 }
danielk1977be718892006-06-23 08:05:19 +00001493 }
1494}
1495
1496/*
drh92f02c32004-09-02 14:57:08 +00001497** This routine is called the when a VDBE tries to halt. If the VDBE
1498** has made changes and is in autocommit mode, then commit those
1499** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001500**
drh92f02c32004-09-02 14:57:08 +00001501** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001502** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1503** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001504**
1505** Return an error code. If the commit could not complete because of
1506** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1507** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001508*/
drhff0587c2007-08-29 17:43:19 +00001509int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001510 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001511 int i;
danielk19771d850a72004-05-31 08:26:49 +00001512 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001513 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1514
1515 /* This function contains the logic that determines if a statement or
1516 ** transaction will be committed or rolled back as a result of the
1517 ** execution of this virtual machine.
1518 **
drh71b890a2007-10-03 15:30:52 +00001519 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001520 **
drh71b890a2007-10-03 15:30:52 +00001521 ** SQLITE_NOMEM
1522 ** SQLITE_IOERR
1523 ** SQLITE_FULL
1524 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001525 **
drh71b890a2007-10-03 15:30:52 +00001526 ** Then the internal cache might have been left in an inconsistent
1527 ** state. We need to rollback the statement transaction, if there is
1528 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001529 */
drh9a324642003-09-06 20:12:01 +00001530
drh17435752007-08-16 04:30:38 +00001531 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001532 p->rc = SQLITE_NOMEM;
1533 }
drhff0587c2007-08-29 17:43:19 +00001534 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001535 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001536 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001537 }
danielk19771d850a72004-05-31 08:26:49 +00001538 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001539
danielk197707cb5602006-01-20 10:55:05 +00001540 /* No commit or rollback needed if the program never started */
1541 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001542 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001543
1544 /* Lock all btrees used by the statement */
1545 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1546
drh71b890a2007-10-03 15:30:52 +00001547 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001548 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001549 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001550 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001551 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001552 /* This loop does static analysis of the query to see which of the
1553 ** following three categories it falls into:
1554 **
1555 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001556 ** Query with statement journal
1557 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001558 **
1559 ** We could do something more elegant than this static analysis (i.e.
1560 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001561 ** handling malloc() or IO failure is a fairly obscure edge case so
1562 ** this is probably easier. Todo: Might be an opportunity to reduce
1563 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001564 */
drhd1817042007-10-03 18:45:04 +00001565 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001566 int isStatement = 0;
1567 assert(p->aOp || p->nOp==0);
1568 for(i=0; i<p->nOp; i++){
1569 switch( p->aOp[i].opcode ){
1570 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001571 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001572 break;
1573 case OP_Statement:
1574 isStatement = 1;
1575 break;
1576 }
1577 }
drhff0587c2007-08-29 17:43:19 +00001578
1579
danielk197707cb5602006-01-20 10:55:05 +00001580 /* If the query was read-only, we need do no rollback at all. Otherwise,
1581 ** proceed with the special handling.
1582 */
drhd1817042007-10-03 18:45:04 +00001583 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001584 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1585 xFunc = sqlite3BtreeRollbackStmt;
1586 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001587 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001588 xFunc = sqlite3BtreeRollbackStmt;
1589 }else{
1590 /* We are forced to roll back the active transaction. Before doing
1591 ** so, abort any other statements this handle currently has active.
1592 */
drhfb982642007-08-30 01:19:59 +00001593 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001594 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001595 db->autoCommit = 1;
1596 }
danielk1977261919c2005-12-06 12:52:59 +00001597 }
1598 }
danielk197707cb5602006-01-20 10:55:05 +00001599
1600 /* If the auto-commit flag is set and this is the only active vdbe, then
1601 ** we do either a commit or rollback of the current transaction.
1602 **
1603 ** Note: This block also runs if one of the special errors handled
1604 ** above has occured.
1605 */
1606 if( db->autoCommit && db->activeVdbeCnt==1 ){
1607 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001608 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001609 ** successful or hit an 'OR FAIL' constraint. This means a commit
1610 ** is required.
1611 */
danielk19773e3a84d2008-08-01 17:37:40 +00001612 int rc = vdbeCommit(db, p);
danielk197707cb5602006-01-20 10:55:05 +00001613 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001614 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001615 return SQLITE_BUSY;
1616 }else if( rc!=SQLITE_OK ){
1617 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001618 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001619 }else{
1620 sqlite3CommitInternalChanges(db);
1621 }
1622 }else{
danielk197797a227c2006-01-20 16:32:04 +00001623 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001624 }
1625 }else if( !xFunc ){
1626 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001627 if( p->openedStatement ){
1628 xFunc = sqlite3BtreeCommitStmt;
1629 }
danielk197707cb5602006-01-20 10:55:05 +00001630 }else if( p->errorAction==OE_Abort ){
1631 xFunc = sqlite3BtreeRollbackStmt;
1632 }else{
drhfb982642007-08-30 01:19:59 +00001633 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001634 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001635 db->autoCommit = 1;
1636 }
danielk19771d850a72004-05-31 08:26:49 +00001637 }
danielk197707cb5602006-01-20 10:55:05 +00001638
1639 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1640 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1641 ** and the return code is still SQLITE_OK, set the return code to the new
1642 ** error value.
1643 */
1644 assert(!xFunc ||
1645 xFunc==sqlite3BtreeCommitStmt ||
1646 xFunc==sqlite3BtreeRollbackStmt
1647 );
1648 for(i=0; xFunc && i<db->nDb; i++){
1649 int rc;
1650 Btree *pBt = db->aDb[i].pBt;
1651 if( pBt ){
1652 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001653 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1654 p->rc = rc;
drh633e6d52008-07-28 19:34:53 +00001655 sqlite3DbFree(db, p->zErrMsg);
drhf089aa42008-07-08 19:34:06 +00001656 p->zErrMsg = 0;
danielk19778a7aea32006-01-23 15:25:48 +00001657 }
danielk197707cb5602006-01-20 10:55:05 +00001658 }
danielk197777d83ba2004-05-31 10:08:14 +00001659 }
danielk197707cb5602006-01-20 10:55:05 +00001660
1661 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1662 ** set the change counter.
1663 */
1664 if( p->changeCntOn && p->pc>=0 ){
1665 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1666 sqlite3VdbeSetChanges(db, p->nChange);
1667 }else{
1668 sqlite3VdbeSetChanges(db, 0);
1669 }
1670 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001671 }
danielk197707cb5602006-01-20 10:55:05 +00001672
1673 /* Rollback or commit any schema changes that occurred. */
1674 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1675 sqlite3ResetInternalSchema(db, 0);
1676 db->flags = (db->flags | SQLITE_InternChanges);
1677 }
drhff0587c2007-08-29 17:43:19 +00001678
1679 /* Release the locks */
1680 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001681 }
danielk19771d850a72004-05-31 08:26:49 +00001682
danielk197765fd59f2006-06-24 11:51:33 +00001683 /* We have successfully halted and closed the VM. Record this fact. */
1684 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001685 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001686 }
drh92f02c32004-09-02 14:57:08 +00001687 p->magic = VDBE_MAGIC_HALT;
1688 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001689 if( p->db->mallocFailed ){
1690 p->rc = SQLITE_NOMEM;
1691 }
danielk19771d850a72004-05-31 08:26:49 +00001692
drh92f02c32004-09-02 14:57:08 +00001693 return SQLITE_OK;
1694}
drh4cf7c7f2007-08-28 23:28:07 +00001695
drh92f02c32004-09-02 14:57:08 +00001696
1697/*
drh3c23a882007-01-09 14:01:13 +00001698** Each VDBE holds the result of the most recent sqlite3_step() call
1699** in p->rc. This routine sets that result back to SQLITE_OK.
1700*/
1701void sqlite3VdbeResetStepResult(Vdbe *p){
1702 p->rc = SQLITE_OK;
1703}
1704
1705/*
drh92f02c32004-09-02 14:57:08 +00001706** Clean up a VDBE after execution but do not delete the VDBE just yet.
1707** Write any error messages into *pzErrMsg. Return the result code.
1708**
1709** After this routine is run, the VDBE should be ready to be executed
1710** again.
1711**
1712** To look at it another way, this routine resets the state of the
1713** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1714** VDBE_MAGIC_INIT.
1715*/
drhc890fec2008-08-01 20:10:08 +00001716int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001717 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001718 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001719
1720 /* If the VM did not run to completion or if it encountered an
1721 ** error, then it might not have been halted properly. So halt
1722 ** it now.
1723 */
drh7e8b8482008-01-23 03:03:05 +00001724 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001725 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001726 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001727
drhfb7e7652005-01-24 00:28:42 +00001728 /* If the VDBE has be run even partially, then transfer the error code
1729 ** and error message from the VDBE into the main database structure. But
1730 ** if the VDBE has just been set to run but has not actually executed any
1731 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001732 */
drhfb7e7652005-01-24 00:28:42 +00001733 if( p->pc>=0 ){
1734 if( p->zErrMsg ){
danielk19779ff3f3f2008-10-11 17:51:38 +00001735 sqlite3BeginBenignMalloc();
drh633e6d52008-07-28 19:34:53 +00001736 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19779ff3f3f2008-10-11 17:51:38 +00001737 sqlite3EndBenignMalloc();
danielk197797a227c2006-01-20 16:32:04 +00001738 db->errCode = p->rc;
drh633e6d52008-07-28 19:34:53 +00001739 sqlite3DbFree(db, p->zErrMsg);
drhfb7e7652005-01-24 00:28:42 +00001740 p->zErrMsg = 0;
1741 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001742 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001743 }else{
drh4ac285a2006-09-15 07:28:50 +00001744 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001745 }
danielk1977a21c6b62005-01-24 10:25:59 +00001746 }else if( p->rc && p->expired ){
1747 /* The expired flag was set on the VDBE before the first call
1748 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1749 ** called), set the database error in this case as well.
1750 */
drh4ac285a2006-09-15 07:28:50 +00001751 sqlite3Error(db, p->rc, 0);
drh633e6d52008-07-28 19:34:53 +00001752 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
1753 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00001754 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001755 }
1756
1757 /* Reclaim all memory used by the VDBE
1758 */
drhc890fec2008-08-01 20:10:08 +00001759 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00001760
1761 /* Save profiling information from this VDBE run.
1762 */
drh9a324642003-09-06 20:12:01 +00001763#ifdef VDBE_PROFILE
1764 {
1765 FILE *out = fopen("vdbe_profile.out", "a");
1766 if( out ){
1767 int i;
1768 fprintf(out, "---- ");
1769 for(i=0; i<p->nOp; i++){
1770 fprintf(out, "%02x", p->aOp[i].opcode);
1771 }
1772 fprintf(out, "\n");
1773 for(i=0; i<p->nOp; i++){
1774 fprintf(out, "%6d %10lld %8lld ",
1775 p->aOp[i].cnt,
1776 p->aOp[i].cycles,
1777 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1778 );
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001780 }
1781 fclose(out);
1782 }
1783 }
1784#endif
1785 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00001786 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001787}
drh92f02c32004-09-02 14:57:08 +00001788
drh9a324642003-09-06 20:12:01 +00001789/*
1790** Clean up and delete a VDBE after execution. Return an integer which is
1791** the result code. Write any error message text into *pzErrMsg.
1792*/
danielk19779e6db7d2004-06-21 08:18:51 +00001793int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001794 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001795 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00001796 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001797 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001798 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001799 return SQLITE_MISUSE;
1800 }
danielk19774adee202004-05-08 08:23:19 +00001801 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001802 return rc;
1803}
1804
1805/*
drhf92c7ff2004-06-19 15:40:23 +00001806** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001807** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001808** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001809** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001810*/
1811void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1812 int i;
1813 for(i=0; i<pVdbeFunc->nAux; i++){
1814 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1815 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1816 if( pAux->xDelete ){
1817 pAux->xDelete(pAux->pAux);
1818 }
1819 pAux->pAux = 0;
1820 }
1821 }
1822}
1823
1824/*
drh9a324642003-09-06 20:12:01 +00001825** Delete an entire VDBE.
1826*/
danielk19774adee202004-05-08 08:23:19 +00001827void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001828 int i;
drh633e6d52008-07-28 19:34:53 +00001829 sqlite3 *db;
1830
drh9a324642003-09-06 20:12:01 +00001831 if( p==0 ) return;
drh633e6d52008-07-28 19:34:53 +00001832 db = p->db;
drh9a324642003-09-06 20:12:01 +00001833 if( p->pPrev ){
1834 p->pPrev->pNext = p->pNext;
1835 }else{
drh633e6d52008-07-28 19:34:53 +00001836 assert( db->pVdbe==p );
1837 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00001838 }
1839 if( p->pNext ){
1840 p->pNext->pPrev = p->pPrev;
1841 }
drh76ff3a02004-09-24 22:32:30 +00001842 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001843 Op *pOp = p->aOp;
1844 for(i=0; i<p->nOp; i++, pOp++){
drh633e6d52008-07-28 19:34:53 +00001845 freeP4(db, pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001846#ifdef SQLITE_DEBUG
drh633e6d52008-07-28 19:34:53 +00001847 sqlite3DbFree(db, pOp->zComment);
drhd4e70eb2008-01-02 00:34:36 +00001848#endif
drh9a324642003-09-06 20:12:01 +00001849 }
drh633e6d52008-07-28 19:34:53 +00001850 sqlite3DbFree(db, p->aOp);
drh9a324642003-09-06 20:12:01 +00001851 }
drhc890fec2008-08-01 20:10:08 +00001852 releaseMemArray(p->aVar, p->nVar);
drh633e6d52008-07-28 19:34:53 +00001853 sqlite3DbFree(db, p->aLabel);
drh9cbf3422008-01-17 16:22:13 +00001854 if( p->aMem ){
drh633e6d52008-07-28 19:34:53 +00001855 sqlite3DbFree(db, &p->aMem[1]);
drh9cbf3422008-01-17 16:22:13 +00001856 }
drhc890fec2008-08-01 20:10:08 +00001857 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001858 sqlite3DbFree(db, p->aColName);
1859 sqlite3DbFree(db, p->zSql);
drh9a324642003-09-06 20:12:01 +00001860 p->magic = VDBE_MAGIC_DEAD;
drh633e6d52008-07-28 19:34:53 +00001861 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00001862}
drha11846b2004-01-07 18:52:56 +00001863
1864/*
drha11846b2004-01-07 18:52:56 +00001865** If a MoveTo operation is pending on the given cursor, then do that
1866** MoveTo now. Return an error code. If no MoveTo is pending, this
1867** routine does nothing and returns SQLITE_OK.
1868*/
drhdfe88ec2008-11-03 20:55:06 +00001869int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00001870 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001871 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001872#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001873 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001874#endif
drhf0863fe2005-06-12 21:35:51 +00001875 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00001876 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001877 if( rc ) return rc;
drhf0863fe2005-06-12 21:35:51 +00001878 p->lastRowid = keyToInt(p->movetoTarget);
1879 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001880 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001881 rc = sqlite3BtreeNext(p->pCursor, &res);
1882 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001883 }
drh10cfdd52006-08-08 15:42:59 +00001884#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001885 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001886#endif
drha11846b2004-01-07 18:52:56 +00001887 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001888 p->cacheStatus = CACHE_STALE;
drha3460582008-07-11 21:02:53 +00001889 }else if( p->pCursor ){
1890 int hasMoved;
1891 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
1892 if( rc ) return rc;
1893 if( hasMoved ){
1894 p->cacheStatus = CACHE_STALE;
1895 p->nullRow = 1;
1896 }
drha11846b2004-01-07 18:52:56 +00001897 }
1898 return SQLITE_OK;
1899}
danielk19774adee202004-05-08 08:23:19 +00001900
drhab9f7f12004-05-08 10:56:11 +00001901/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001902** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001903**
danielk1977cfcdaef2004-05-12 07:33:33 +00001904** sqlite3VdbeSerialType()
1905** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00001906** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00001907** sqlite3VdbeSerialPut()
1908** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00001909**
1910** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001911** data and index records. Each serialized value consists of a
1912** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1913** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001914**
danielk1977cfcdaef2004-05-12 07:33:33 +00001915** In an SQLite index record, the serial type is stored directly before
1916** the blob of data that it corresponds to. In a table record, all serial
1917** types are stored at the start of the record, and the blobs of data at
1918** the end. Hence these functions allow the caller to handle the
1919** serial-type and data blob seperately.
1920**
1921** The following table describes the various storage classes for data:
1922**
1923** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001924** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001925** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001926** 1 1 signed integer
1927** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001928** 3 3 signed integer
1929** 4 4 signed integer
1930** 5 6 signed integer
1931** 6 8 signed integer
1932** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001933** 8 0 Integer constant 0
1934** 9 0 Integer constant 1
1935** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001936** N>=12 and even (N-12)/2 BLOB
1937** N>=13 and odd (N-13)/2 text
1938**
drh35a59652006-01-02 18:24:40 +00001939** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1940** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001941*/
1942
1943/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001944** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001945*/
drhd946db02005-12-29 19:23:06 +00001946u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001947 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001948 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001949
1950 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001951 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001952 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001953 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001954 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00001955# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001956 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001957 u64 u;
1958 if( file_format>=4 && (i&1)==i ){
1959 return 8+i;
1960 }
1961 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001962 if( u<=127 ) return 1;
1963 if( u<=32767 ) return 2;
1964 if( u<=8388607 ) return 3;
1965 if( u<=2147483647 ) return 4;
1966 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001967 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001968 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001969 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001970 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001971 }
danielk1977e4359752008-11-03 09:39:45 +00001972 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00001973 n = pMem->n;
1974 if( flags & MEM_Zero ){
1975 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001976 }
drhfdf972a2007-05-02 13:30:27 +00001977 assert( n>=0 );
1978 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001979}
1980
1981/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001982** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001983*/
drh25aa1b42004-05-28 01:39:01 +00001984int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001985 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001986 return (serial_type-12)/2;
1987 }else{
drh57196282004-10-06 15:41:16 +00001988 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001989 return aSize[serial_type];
1990 }
danielk1977192ac1d2004-05-10 07:17:30 +00001991}
1992
1993/*
drh110daac2007-05-04 11:59:31 +00001994** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001995** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001996** upper 4 bytes. Return the result.
1997**
drh7a4f5022007-05-23 07:20:08 +00001998** For most architectures, this is a no-op.
1999**
2000** (later): It is reported to me that the mixed-endian problem
2001** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2002** that early versions of GCC stored the two words of a 64-bit
2003** float in the wrong order. And that error has been propagated
2004** ever since. The blame is not necessarily with GCC, though.
2005** GCC might have just copying the problem from a prior compiler.
2006** I am also told that newer versions of GCC that follow a different
2007** ABI get the byte order right.
2008**
2009** Developers using SQLite on an ARM7 should compile and run their
2010** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2011** enabled, some asserts below will ensure that the byte order of
2012** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002013**
2014** (2007-08-30) Frank van Vugt has studied this problem closely
2015** and has send his findings to the SQLite developers. Frank
2016** writes that some Linux kernels offer floating point hardware
2017** emulation that uses only 32-bit mantissas instead of a full
2018** 48-bits as required by the IEEE standard. (This is the
2019** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2020** byte swapping becomes very complicated. To avoid problems,
2021** the necessary byte swapping is carried out using a 64-bit integer
2022** rather than a 64-bit float. Frank assures us that the code here
2023** works for him. We, the developers, have no way to independently
2024** verify this, but Frank seems to know what he is talking about
2025** so we trust him.
drh110daac2007-05-04 11:59:31 +00002026*/
2027#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002028static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002029 union {
drh60d09a72007-08-30 15:05:08 +00002030 u64 r;
drh110daac2007-05-04 11:59:31 +00002031 u32 i[2];
2032 } u;
2033 u32 t;
2034
2035 u.r = in;
2036 t = u.i[0];
2037 u.i[0] = u.i[1];
2038 u.i[1] = t;
2039 return u.r;
2040}
2041# define swapMixedEndianFloat(X) X = floatSwap(X)
2042#else
2043# define swapMixedEndianFloat(X)
2044#endif
2045
2046/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002047** Write the serialized data blob for the value stored in pMem into
2048** buf. It is assumed that the caller has allocated sufficient space.
2049** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002050**
2051** nBuf is the amount of space left in buf[]. nBuf must always be
2052** large enough to hold the entire field. Except, if the field is
2053** a blob with a zero-filled tail, then buf[] might be just the right
2054** size to hold everything except for the zero-filled tail. If buf[]
2055** is only big enough to hold the non-zero prefix, then only write that
2056** prefix into buf[]. But if buf[] is large enough to hold both the
2057** prefix and the tail then write the prefix and set the tail to all
2058** zeros.
2059**
2060** Return the number of bytes actually written into buf[]. The number
2061** of bytes in the zero-filled tail is included in the return value only
2062** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002063*/
drhfdf972a2007-05-02 13:30:27 +00002064int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002065 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00002066 int len;
danielk1977183f9f72004-05-13 05:20:26 +00002067
drh1483e142004-05-21 21:12:42 +00002068 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002069 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002070 u64 v;
2071 int i;
drha19b7752004-05-30 21:14:58 +00002072 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002073 assert( sizeof(v)==sizeof(pMem->r) );
2074 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002075 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002076 }else{
drh3c024d62007-03-30 11:23:45 +00002077 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002078 }
drh1483e142004-05-21 21:12:42 +00002079 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002080 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00002081 while( i-- ){
2082 buf[i] = (v&0xFF);
2083 v >>= 8;
2084 }
2085 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002086 }
drhd946db02005-12-29 19:23:06 +00002087
danielk1977cfcdaef2004-05-12 07:33:33 +00002088 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002089 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00002090 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
2091 == sqlite3VdbeSerialTypeLen(serial_type) );
2092 assert( pMem->n<=nBuf );
2093 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002094 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002095 if( pMem->flags & MEM_Zero ){
2096 len += pMem->u.i;
2097 if( len>nBuf ){
2098 len = nBuf;
2099 }
2100 memset(&buf[pMem->n], 0, len-pMem->n);
2101 }
drhd946db02005-12-29 19:23:06 +00002102 return len;
2103 }
2104
2105 /* NULL or constants 0 or 1 */
2106 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002107}
2108
2109/*
2110** Deserialize the data blob pointed to by buf as serial type serial_type
2111** and store the result in pMem. Return the number of bytes read.
2112*/
danielk1977b1bc9532004-05-22 03:05:33 +00002113int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002114 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002115 u32 serial_type, /* Serial type to deserialize */
2116 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002117){
drh3c685822005-05-21 18:32:18 +00002118 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002119 case 10: /* Reserved for future use */
2120 case 11: /* Reserved for future use */
2121 case 0: { /* NULL */
2122 pMem->flags = MEM_Null;
2123 break;
2124 }
2125 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002126 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002127 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002128 return 1;
drh1483e142004-05-21 21:12:42 +00002129 }
drh3c685822005-05-21 18:32:18 +00002130 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002131 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002132 pMem->flags = MEM_Int;
2133 return 2;
2134 }
2135 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002136 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002137 pMem->flags = MEM_Int;
2138 return 3;
2139 }
2140 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002141 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002142 pMem->flags = MEM_Int;
2143 return 4;
2144 }
2145 case 5: { /* 6-byte signed integer */
2146 u64 x = (((signed char)buf[0])<<8) | buf[1];
2147 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2148 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002149 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002150 pMem->flags = MEM_Int;
2151 return 6;
2152 }
drh91124b32005-08-18 18:15:05 +00002153 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002154 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002155 u64 x;
2156 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002157#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002158 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002159 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2160 ** defined that 64-bit floating point values really are mixed
2161 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002162 */
drhde941c62005-08-28 01:34:21 +00002163 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002164 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002165 u64 t2 = t1;
2166 swapMixedEndianFloat(t2);
2167 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002168#endif
drhbfd6b032005-08-28 01:38:44 +00002169
drhd81bd4e2005-09-05 20:06:49 +00002170 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2171 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002172 x = (x<<32) | y;
2173 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002174 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002175 pMem->flags = MEM_Int;
2176 }else{
drh4f0c5872007-03-26 22:05:01 +00002177 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002178 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002179 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002180 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002181 }
2182 return 8;
2183 }
drhd946db02005-12-29 19:23:06 +00002184 case 8: /* Integer 0 */
2185 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002186 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002187 pMem->flags = MEM_Int;
2188 return 0;
2189 }
drh3c685822005-05-21 18:32:18 +00002190 default: {
2191 int len = (serial_type-12)/2;
2192 pMem->z = (char *)buf;
2193 pMem->n = len;
2194 pMem->xDel = 0;
2195 if( serial_type&0x01 ){
2196 pMem->flags = MEM_Str | MEM_Ephem;
2197 }else{
2198 pMem->flags = MEM_Blob | MEM_Ephem;
2199 }
2200 return len;
drh696b32f2004-05-30 01:51:52 +00002201 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002202 }
drh3c685822005-05-21 18:32:18 +00002203 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002204}
2205
drh0e6082e2006-01-12 20:28:35 +00002206
drh1e968a02008-03-25 00:22:21 +00002207/*
2208** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002209** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002210** that structure.
2211**
2212** The calling function might provide szSpace bytes of memory
2213** space at pSpace. This space can be used to hold the returned
2214** VDbeParsedRecord structure if it is large enough. If it is
2215** not big enough, space is obtained from sqlite3_malloc().
2216**
2217** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002218** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002219*/
drhe14006d2008-03-25 17:23:32 +00002220UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002221 KeyInfo *pKeyInfo, /* Information about the record format */
2222 int nKey, /* Size of the binary record */
2223 const void *pKey, /* The binary record */
drh23f79d02008-08-20 22:06:47 +00002224 UnpackedRecord *pSpace,/* Space available to hold resulting object */
drh1e968a02008-03-25 00:22:21 +00002225 int szSpace /* Size of pSpace[] in bytes */
2226){
2227 const unsigned char *aKey = (const unsigned char *)pKey;
drhe14006d2008-03-25 17:23:32 +00002228 UnpackedRecord *p;
drh1e968a02008-03-25 00:22:21 +00002229 int nByte;
shane0b8d2762008-07-22 05:18:00 +00002230 int idx, d;
2231 u16 u; /* Unsigned loop counter */
drh1e968a02008-03-25 00:22:21 +00002232 u32 szHdr;
2233 Mem *pMem;
2234
drhfab69592008-04-10 14:57:24 +00002235 assert( sizeof(Mem)>sizeof(*p) );
2236 nByte = sizeof(Mem)*(pKeyInfo->nField+2);
drh1e968a02008-03-25 00:22:21 +00002237 if( nByte>szSpace ){
2238 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2239 if( p==0 ) return 0;
drhe63d9992008-08-13 19:11:48 +00002240 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002241 }else{
2242 p = pSpace;
drhe63d9992008-08-13 19:11:48 +00002243 p->flags = UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002244 }
2245 p->pKeyInfo = pKeyInfo;
2246 p->nField = pKeyInfo->nField + 1;
drhfab69592008-04-10 14:57:24 +00002247 p->aMem = pMem = &((Mem*)p)[1];
shane3f8d5cf2008-04-24 19:15:09 +00002248 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002249 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00002250 u = 0;
2251 while( idx<szHdr && u<p->nField ){
drh1e968a02008-03-25 00:22:21 +00002252 u32 serial_type;
2253
shane3f8d5cf2008-04-24 19:15:09 +00002254 idx += getVarint32( aKey+idx, serial_type);
drh1e968a02008-03-25 00:22:21 +00002255 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
2256 pMem->enc = pKeyInfo->enc;
2257 pMem->db = pKeyInfo->db;
2258 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002259 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002260 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002261 pMem++;
shane0b8d2762008-07-22 05:18:00 +00002262 u++;
drh1e968a02008-03-25 00:22:21 +00002263 }
drh7d10d5a2008-08-20 16:35:10 +00002264 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00002265 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00002266 return (void*)p;
2267}
2268
2269/*
drhe14006d2008-03-25 17:23:32 +00002270** This routine destroys a UnpackedRecord object
drh1e968a02008-03-25 00:22:21 +00002271*/
drhe14006d2008-03-25 17:23:32 +00002272void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh1e968a02008-03-25 00:22:21 +00002273 if( p ){
drhe63d9992008-08-13 19:11:48 +00002274 if( p->flags & UNPACKED_NEED_DESTROY ){
drh1e968a02008-03-25 00:22:21 +00002275 int i;
drhe14006d2008-03-25 17:23:32 +00002276 Mem *pMem;
2277 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
danielk19775f096132008-03-28 15:44:09 +00002278 if( pMem->zMalloc ){
drhe14006d2008-03-25 17:23:32 +00002279 sqlite3VdbeMemRelease(pMem);
drh1e968a02008-03-25 00:22:21 +00002280 }
2281 }
2282 }
drhe63d9992008-08-13 19:11:48 +00002283 if( p->flags & UNPACKED_NEED_FREE ){
drh633e6d52008-07-28 19:34:53 +00002284 sqlite3DbFree(p->pKeyInfo->db, p);
drh1e968a02008-03-25 00:22:21 +00002285 }
2286 }
2287}
2288
2289/*
2290** This function compares the two table rows or index records
2291** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00002292** or positive integer if key1 is less than, equal to or
2293** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00002294** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2295** key must be a parsed key such as obtained from
2296** sqlite3VdbeParseRecord.
2297**
2298** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00002299** The key with fewer fields is usually compares less than the
2300** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
2301** and the common prefixes are equal, then key1 is less than key2.
2302** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
2303** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00002304** the parts beyond the common prefix are ignored.
2305**
drhe63d9992008-08-13 19:11:48 +00002306** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
2307** the header of pKey1 is ignored. It is assumed that pKey1 is
2308** an index key, and thus ends with a rowid value. The last byte
2309** of the header will therefore be the serial type of the rowid:
2310** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
2311** The serial type of the final rowid will always be a single byte.
2312** By ignoring this last byte of the header, we force the comparison
2313** to ignore the rowid at the end of key1.
drh1e968a02008-03-25 00:22:21 +00002314*/
drhe14006d2008-03-25 17:23:32 +00002315int sqlite3VdbeRecordCompare(
drhec1fc802008-08-13 14:07:40 +00002316 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00002317 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00002318){
2319 u32 d1; /* Offset into aKey[] of next data element */
2320 u32 idx1; /* Offset into aKey[] of next header element */
2321 u32 szHdr1; /* Number of bytes in header */
2322 int i = 0;
2323 int nField;
2324 int rc = 0;
2325 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2326 KeyInfo *pKeyInfo;
2327 Mem mem1;
2328
2329 pKeyInfo = pPKey2->pKeyInfo;
2330 mem1.enc = pKeyInfo->enc;
2331 mem1.db = pKeyInfo->db;
2332 mem1.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002333 mem1.zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002334
shane3f8d5cf2008-04-24 19:15:09 +00002335 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002336 d1 = szHdr1;
drhe63d9992008-08-13 19:11:48 +00002337 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
2338 szHdr1--;
2339 }
drh1e968a02008-03-25 00:22:21 +00002340 nField = pKeyInfo->nField;
2341 while( idx1<szHdr1 && i<pPKey2->nField ){
2342 u32 serial_type1;
2343
2344 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002345 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002346 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2347
2348 /* Extract the values to be compared.
2349 */
2350 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2351
2352 /* Do the comparison
2353 */
drhe14006d2008-03-25 17:23:32 +00002354 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002355 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002356 if( rc!=0 ){
2357 break;
2358 }
2359 i++;
2360 }
danielk19775f096132008-03-28 15:44:09 +00002361 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1);
drh1e968a02008-03-25 00:22:21 +00002362
drh1e968a02008-03-25 00:22:21 +00002363 if( rc==0 ){
drhec1fc802008-08-13 14:07:40 +00002364 /* rc==0 here means that one of the keys ran out of fields and
drhe63d9992008-08-13 19:11:48 +00002365 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
2366 ** flag is set, then break the tie by treating key2 as larger.
2367 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
drhec1fc802008-08-13 14:07:40 +00002368 ** are considered to be equal. Otherwise, the longer key is the
2369 ** larger. As it happens, the pPKey2 will always be the longer
2370 ** if there is a difference.
2371 */
drhe63d9992008-08-13 19:11:48 +00002372 if( pPKey2->flags & UNPACKED_INCRKEY ){
drh1e968a02008-03-25 00:22:21 +00002373 rc = -1;
drhe63d9992008-08-13 19:11:48 +00002374 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
drhec1fc802008-08-13 14:07:40 +00002375 /* Leave rc==0 */
2376 }else if( idx1<szHdr1 ){
2377 rc = 1;
drh1e968a02008-03-25 00:22:21 +00002378 }
2379 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2380 && pKeyInfo->aSortOrder[i] ){
2381 rc = -rc;
2382 }
2383
2384 return rc;
2385}
drhec1fc802008-08-13 14:07:40 +00002386
danielk1977eb015e02004-05-18 01:31:14 +00002387
2388/*
drh7a224de2004-06-02 01:22:02 +00002389** pCur points at an index entry created using the OP_MakeRecord opcode.
2390** Read the rowid (the last field in the record) and store it in *rowid.
2391** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002392*/
drhb21c8cd2007-08-21 19:33:56 +00002393int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002394 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002395 int rc;
drhd5788202004-05-28 08:21:05 +00002396 u32 szHdr; /* Size of the header */
2397 u32 typeRowid; /* Serial type of the rowid */
2398 u32 lenRowid; /* Size of the rowid */
2399 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002400
drhd5788202004-05-28 08:21:05 +00002401 sqlite3BtreeKeySize(pCur, &nCellKey);
2402 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002403 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002404 }
danielk1977a7a8e142008-02-13 18:25:27 +00002405 m.flags = 0;
2406 m.db = 0;
danielk19775f096132008-03-28 15:44:09 +00002407 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002408 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002409 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002410 return rc;
2411 }
shane3f8d5cf2008-04-24 19:15:09 +00002412 (void)getVarint32((u8*)m.z, szHdr);
2413 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002414 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002415 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002416 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002417 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002418 return SQLITE_OK;
2419}
2420
drh7cf6e4d2004-05-19 14:56:55 +00002421/*
drhd3d39e92004-05-20 22:16:29 +00002422** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002423** the key string in pKey (of length nKey). Write into *pRes a number
2424** that is negative, zero, or positive if pC is less than, equal to,
2425** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002426**
drhd5788202004-05-28 08:21:05 +00002427** pKey is either created without a rowid or is truncated so that it
2428** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00002429** is ignored as well. Hence, this routine only compares the prefixes
2430** of the keys prior to the final rowid, not the entire key.
2431**
2432** pUnpacked may be an unpacked version of pKey,nKey. If pUnpacked is
2433** supplied it is used in place of pKey,nKey.
drh7cf6e4d2004-05-19 14:56:55 +00002434*/
danielk1977183f9f72004-05-13 05:20:26 +00002435int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00002436 VdbeCursor *pC, /* The cursor to compare against */
drhec1fc802008-08-13 14:07:40 +00002437 UnpackedRecord *pUnpacked, /* Unpacked version of pKey and nKey */
drh7cf6e4d2004-05-19 14:56:55 +00002438 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002439){
drh61fc5952007-04-01 23:49:51 +00002440 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002441 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002442 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002443 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002444
2445 sqlite3BtreeKeySize(pCur, &nCellKey);
2446 if( nCellKey<=0 ){
2447 *res = 0;
2448 return SQLITE_OK;
2449 }
danielk1977a7a8e142008-02-13 18:25:27 +00002450 m.db = 0;
2451 m.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002452 m.zMalloc = 0;
drhec1fc802008-08-13 14:07:40 +00002453 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2454 if( rc ){
drhd5788202004-05-28 08:21:05 +00002455 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002456 }
drhe63d9992008-08-13 19:11:48 +00002457 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
2458 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00002459 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002460 return SQLITE_OK;
2461}
danielk1977b28af712004-06-21 06:50:26 +00002462
2463/*
2464** This routine sets the value to be returned by subsequent calls to
2465** sqlite3_changes() on the database handle 'db'.
2466*/
2467void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002468 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002469 db->nChange = nChange;
2470 db->nTotalChange += nChange;
2471}
2472
2473/*
2474** Set a flag in the vdbe to update the change counter when it is finalised
2475** or reset.
2476*/
drh4794f732004-11-05 17:17:50 +00002477void sqlite3VdbeCountChanges(Vdbe *v){
2478 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002479}
drhd89bd002005-01-22 03:03:54 +00002480
2481/*
2482** Mark every prepared statement associated with a database connection
2483** as expired.
2484**
2485** An expired statement means that recompilation of the statement is
2486** recommend. Statements expire when things happen that make their
2487** programs obsolete. Removing user-defined functions or collating
2488** sequences, or changing an authorization function are the types of
2489** things that make prepared statements obsolete.
2490*/
2491void sqlite3ExpirePreparedStatements(sqlite3 *db){
2492 Vdbe *p;
2493 for(p = db->pVdbe; p; p=p->pNext){
2494 p->expired = 1;
2495 }
2496}
danielk1977aee18ef2005-03-09 12:26:50 +00002497
2498/*
2499** Return the database associated with the Vdbe.
2500*/
2501sqlite3 *sqlite3VdbeDb(Vdbe *v){
2502 return v->db;
2503}