blob: e2b235bc09b78bc543db33dbf91a29031e3be60e [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**
drh0acb7e42008-06-25 00:12:41 +000017** $Id: vdbeaux.c,v 1.393 2008/06/25 00:12:42 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 }
drh17435752007-08-16 04:30:38 +0000305 sqlite3_free(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*/
435static void freeEphemeralFunction(FuncDef *pDef){
436 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh17435752007-08-16 04:30:38 +0000437 sqlite3_free(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*/
drh0acb7e42008-06-25 00:12:41 +0000444static void freeP4(int p4type, void *p4){
445 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: {
drh0acb7e42008-06-25 00:12:41 +0000454 sqlite3_free(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;
drhb7f6f682006-07-08 17:06:43 +0000459 freeEphemeralFunction(pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000460 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh17435752007-08-16 04:30:38 +0000461 sqlite3_free(pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000462 break;
463 }
drh66a51672008-01-03 00:01:23 +0000464 case P4_FUNCDEF: {
drh0acb7e42008-06-25 00:12:41 +0000465 freeEphemeralFunction((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];
483 while( N-- ){
drh66a51672008-01-03 00:01:23 +0000484 freeP4(pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000485 memset(pOp, 0, sizeof(pOp[0]));
486 pOp->opcode = OP_Noop;
487 pOp++;
488 }
drhf8875402006-03-17 13:56:34 +0000489 }
490}
491
492/*
drh66a51672008-01-03 00:01:23 +0000493** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000494** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000495** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000496** few minor changes to the program.
497**
drh66a51672008-01-03 00:01:23 +0000498** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000499** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000500** A value of n==0 means copy bytes of zP4 up to and including the
501** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000502**
drh66a51672008-01-03 00:01:23 +0000503** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000504** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000505** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000506** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000507** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000508** caller should not free the allocation, it will be freed when the Vdbe is
509** finalized.
510**
drh66a51672008-01-03 00:01:23 +0000511** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000512** to a string or structure that is guaranteed to exist for the lifetime of
513** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000514**
drh66a51672008-01-03 00:01:23 +0000515** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000516*/
drh66a51672008-01-03 00:01:23 +0000517void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000518 Op *pOp;
drh91fd4d42008-01-19 20:11:25 +0000519 assert( p!=0 );
520 assert( p->magic==VDBE_MAGIC_INIT );
521 if( p->aOp==0 || p->db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000522 if (n != P4_KEYINFO) {
523 freeP4(n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000524 }
danielk1977d5d56522005-03-16 12:15:20 +0000525 return;
526 }
drh91fd4d42008-01-19 20:11:25 +0000527 assert( addr<p->nOp );
528 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000529 addr = p->nOp - 1;
530 if( addr<0 ) return;
531 }
532 pOp = &p->aOp[addr];
drh66a51672008-01-03 00:01:23 +0000533 freeP4(pOp->p4type, pOp->p4.p);
534 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000535 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000536 /* Note: this cast is safe, because the origin data point was an int
537 ** that was cast to a (const char *). */
drh7209c692008-04-27 18:40:11 +0000538 pOp->p4.i = (int)zP4;
drh98757152008-01-09 23:04:12 +0000539 pOp->p4type = n;
540 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000541 pOp->p4.p = 0;
542 pOp->p4type = P4_NOTUSED;
543 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000544 KeyInfo *pKeyInfo;
545 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000546
drh66a51672008-01-03 00:01:23 +0000547 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000548 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhe5ae5732008-06-15 02:51:47 +0000549 pKeyInfo = sqlite3Malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000550 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000551 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000552 u8 *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000553 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000554 aSortOrder = pKeyInfo->aSortOrder;
555 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000556 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000557 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
558 }
drh66a51672008-01-03 00:01:23 +0000559 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000560 }else{
drh17435752007-08-16 04:30:38 +0000561 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000562 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000563 }
drh66a51672008-01-03 00:01:23 +0000564 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000565 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000566 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000567 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000568 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000569 pOp->p4type = n;
drh9a324642003-09-06 20:12:01 +0000570 }else{
drh66a51672008-01-03 00:01:23 +0000571 if( n==0 ) n = strlen(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000572 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000573 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000574 }
575}
576
drhad6d9462004-09-19 02:15:24 +0000577#ifndef NDEBUG
578/*
drh16ee60f2008-06-20 18:13:25 +0000579** Change the comment on the the most recently coded instruction. Or
580** insert a No-op and add the comment to that new instruction. This
581** makes the code easier to read during debugging. None of this happens
582** in a production build.
drhad6d9462004-09-19 02:15:24 +0000583*/
584void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
585 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000586 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000587 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000588 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000589 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000590 va_start(ap, zFormat);
drh8cc74322008-01-15 02:22:24 +0000591 sqlite3_free(*pz);
592 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000593 va_end(ap);
594 }
drhad6d9462004-09-19 02:15:24 +0000595}
drh16ee60f2008-06-20 18:13:25 +0000596void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
597 va_list ap;
598 sqlite3VdbeAddOp0(p, OP_Noop);
599 assert( p->nOp>0 || p->aOp==0 );
600 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
601 if( p->nOp ){
602 char **pz = &p->aOp[p->nOp-1].zComment;
603 va_start(ap, zFormat);
604 sqlite3_free(*pz);
605 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
606 va_end(ap);
607 }
608}
609#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000610
drh9a324642003-09-06 20:12:01 +0000611/*
drh9a324642003-09-06 20:12:01 +0000612** Return the opcode for a given address.
613*/
danielk19774adee202004-05-08 08:23:19 +0000614VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000615 assert( p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000616 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
danielk197701256832007-04-18 14:24:32 +0000617 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000618}
619
drhb7f91642004-10-31 02:22:47 +0000620#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
621 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000622/*
drh66a51672008-01-03 00:01:23 +0000623** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000624** Use zTemp for any required temporary buffer space.
625*/
drh66a51672008-01-03 00:01:23 +0000626static char *displayP4(Op *pOp, char *zTemp, int nTemp){
627 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000628 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000629 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000630 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000631 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000632 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000633 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000634 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhd3d39e92004-05-20 22:16:29 +0000635 i = strlen(zTemp);
636 for(j=0; j<pKeyInfo->nField; j++){
637 CollSeq *pColl = pKeyInfo->aColl[j];
638 if( pColl ){
639 int n = strlen(pColl->zName);
640 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000641 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000642 break;
643 }
644 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000645 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000646 zTemp[i++] = '-';
647 }
drh5bb3eb92007-05-04 13:15:55 +0000648 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000649 i += n;
650 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000651 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000652 i += 4;
653 }
654 }
655 zTemp[i++] = ')';
656 zTemp[i] = 0;
657 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000658 break;
659 }
drh66a51672008-01-03 00:01:23 +0000660 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000661 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000662 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000663 break;
664 }
drh66a51672008-01-03 00:01:23 +0000665 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000666 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000667 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000668 break;
669 }
drh66a51672008-01-03 00:01:23 +0000670 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000671 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000672 break;
673 }
drh66a51672008-01-03 00:01:23 +0000674 case P4_INT32: {
675 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000676 break;
677 }
drh66a51672008-01-03 00:01:23 +0000678 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000679 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000680 break;
681 }
drh66a51672008-01-03 00:01:23 +0000682 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000683 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000684 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000685 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000686 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000687 }else if( pMem->flags & MEM_Int ){
688 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
689 }else if( pMem->flags & MEM_Real ){
690 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhd4e70eb2008-01-02 00:34:36 +0000691 }
drh598f1342007-10-23 15:39:45 +0000692 break;
693 }
drha967e882006-06-13 01:04:52 +0000694#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000695 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000696 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000697 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000698 break;
699 }
700#endif
drh0acb7e42008-06-25 00:12:41 +0000701 case P4_INTARRAY: {
702 sqlite3_snprintf(nTemp, zTemp, "intarray");
703 break;
704 }
drhd3d39e92004-05-20 22:16:29 +0000705 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000706 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000707 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000708 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000709 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000710 }
711 }
712 }
drh66a51672008-01-03 00:01:23 +0000713 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000714 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000715}
drhb7f91642004-10-31 02:22:47 +0000716#endif
drhd3d39e92004-05-20 22:16:29 +0000717
drh900b31e2007-08-28 02:27:51 +0000718/*
drhd0679ed2007-08-28 22:24:34 +0000719** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
720**
drh900b31e2007-08-28 02:27:51 +0000721*/
drhfb982642007-08-30 01:19:59 +0000722void sqlite3VdbeUsesBtree(Vdbe *p, int i){
723 int mask;
drhd0679ed2007-08-28 22:24:34 +0000724 assert( i>=0 && i<p->db->nDb );
725 assert( i<sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000726 mask = 1<<i;
727 if( (p->btreeMask & mask)==0 ){
728 p->btreeMask |= mask;
729 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
730 }
drh900b31e2007-08-28 02:27:51 +0000731}
732
drhd3d39e92004-05-20 22:16:29 +0000733
danielk19778b60e0f2005-01-12 09:10:39 +0000734#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000735/*
736** Print a single opcode. This routine is used for debugging only.
737*/
danielk19774adee202004-05-08 08:23:19 +0000738void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000739 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000740 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000741 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000742 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000743 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000744 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000745 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
746#ifdef SQLITE_DEBUG
747 pOp->zComment ? pOp->zComment : ""
748#else
749 ""
750#endif
751 );
drh9a324642003-09-06 20:12:01 +0000752 fflush(pOut);
753}
754#endif
755
756/*
drh76ff3a02004-09-24 22:32:30 +0000757** Release an array of N Mem elements
758*/
danielk1977dfb316d2008-03-26 18:34:43 +0000759static void releaseMemArray(Mem *p, int N, int freebuffers){
danielk1977a7a8e142008-02-13 18:25:27 +0000760 if( p && N ){
761 sqlite3 *db = p->db;
762 int malloc_failed = db->mallocFailed;
drh76ff3a02004-09-24 22:32:30 +0000763 while( N-->0 ){
drhb21c8cd2007-08-21 19:33:56 +0000764 assert( N<2 || p[0].db==p[1].db );
danielk19775f096132008-03-28 15:44:09 +0000765 if( freebuffers ){
danielk1977dfb316d2008-03-26 18:34:43 +0000766 sqlite3VdbeMemRelease(p);
danielk19775f096132008-03-28 15:44:09 +0000767 }else{
768 sqlite3VdbeMemReleaseExternal(p);
danielk1977dfb316d2008-03-26 18:34:43 +0000769 }
danielk19775f096132008-03-28 15:44:09 +0000770 p->flags = MEM_Null;
danielk1977dfb316d2008-03-26 18:34:43 +0000771 p++;
drh76ff3a02004-09-24 22:32:30 +0000772 }
danielk1977a7a8e142008-02-13 18:25:27 +0000773 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +0000774 }
775}
776
danielk1977dfb316d2008-03-26 18:34:43 +0000777#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
778int sqlite3VdbeReleaseBuffers(Vdbe *p){
779 int ii;
780 int nFree = 0;
781 assert( sqlite3_mutex_held(p->db->mutex) );
782 for(ii=1; ii<=p->nMem; ii++){
783 Mem *pMem = &p->aMem[ii];
784 if( pMem->z && pMem->flags&MEM_Dyn ){
785 assert( !pMem->xDel );
786 nFree += sqlite3MallocSize(pMem->z);
787 sqlite3VdbeMemRelease(pMem);
788 }
789 }
790 return nFree;
791}
792#endif
793
drhb7f91642004-10-31 02:22:47 +0000794#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000795/*
drh9a324642003-09-06 20:12:01 +0000796** Give a listing of the program in the virtual machine.
797**
danielk19774adee202004-05-08 08:23:19 +0000798** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000799** running the code, it invokes the callback once for each instruction.
800** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +0000801**
802** When p->explain==1, each instruction is listed. When
803** p->explain==2, only OP_Explain instructions are listed and these
804** are shown in a different format. p->explain==2 is used to implement
805** EXPLAIN QUERY PLAN.
drh9a324642003-09-06 20:12:01 +0000806*/
danielk19774adee202004-05-08 08:23:19 +0000807int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000808 Vdbe *p /* The VDBE */
809){
drh9bb575f2004-09-06 17:24:11 +0000810 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000811 int i;
drh826fb5a2004-02-14 23:59:57 +0000812 int rc = SQLITE_OK;
drh9cbf3422008-01-17 16:22:13 +0000813 Mem *pMem = p->pResultSet = &p->aMem[1];
drh9a324642003-09-06 20:12:01 +0000814
drh9a324642003-09-06 20:12:01 +0000815 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000816 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
817 assert( db->magic==SQLITE_MAGIC_BUSY );
818 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000819
drh9cbf3422008-01-17 16:22:13 +0000820 /* Even though this opcode does not use dynamic strings for
821 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000822 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000823 */
danielk1977dfb316d2008-03-26 18:34:43 +0000824 releaseMemArray(pMem, p->nMem, 1);
danielk197718f41892004-05-22 07:27:46 +0000825
drhecc92422005-09-10 16:46:12 +0000826 do{
827 i = p->pc++;
828 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000829 if( i>=p->nOp ){
830 p->rc = SQLITE_OK;
831 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000832 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000833 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000834 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000835 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000836 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000837 char *z;
drhd3d39e92004-05-20 22:16:29 +0000838 Op *pOp = &p->aOp[i];
danielk19770d78bae2008-01-03 07:09:48 +0000839 if( p->explain==1 ){
840 pMem->flags = MEM_Int;
841 pMem->type = SQLITE_INTEGER;
842 pMem->u.i = i; /* Program counter */
843 pMem++;
844
845 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
846 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
847 assert( pMem->z!=0 );
848 pMem->n = strlen(pMem->z);
849 pMem->type = SQLITE_TEXT;
850 pMem->enc = SQLITE_UTF8;
851 pMem++;
852 }
drheb2e1762004-05-27 01:53:56 +0000853
854 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000855 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000856 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000857 pMem++;
858
859 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000860 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000861 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000862 pMem++;
863
danielk19770d78bae2008-01-03 07:09:48 +0000864 if( p->explain==1 ){
865 pMem->flags = MEM_Int;
866 pMem->u.i = pOp->p3; /* P3 */
867 pMem->type = SQLITE_INTEGER;
868 pMem++;
869 }
870
danielk1977a7a8e142008-02-13 18:25:27 +0000871 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
872 p->db->mallocFailed = 1;
873 return SQLITE_NOMEM;
874 }
875 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
876 z = displayP4(pOp, pMem->z, 32);
877 if( z!=pMem->z ){
878 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
879 }else{
880 assert( pMem->z!=0 );
881 pMem->n = strlen(pMem->z);
882 pMem->enc = SQLITE_UTF8;
883 }
drh9c054832004-05-31 18:51:57 +0000884 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +0000885 pMem++;
drheb2e1762004-05-27 01:53:56 +0000886
danielk19770d78bae2008-01-03 07:09:48 +0000887 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +0000888 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +0000889 p->db->mallocFailed = 1;
890 return SQLITE_NOMEM;
891 }
892 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +0000893 pMem->n = 2;
894 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +0000895 pMem->type = SQLITE_TEXT;
896 pMem->enc = SQLITE_UTF8;
897 pMem++;
898
drhaa9b8962008-01-08 02:57:55 +0000899#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000900 if( pOp->zComment ){
901 pMem->flags = MEM_Str|MEM_Term;
902 pMem->z = pOp->zComment;
903 pMem->n = strlen(pMem->z);
904 pMem->enc = SQLITE_UTF8;
drh52391cb2008-02-14 23:44:13 +0000905 }else
drhaa9b8962008-01-08 02:57:55 +0000906#endif
drh52391cb2008-02-14 23:44:13 +0000907 {
908 pMem->flags = MEM_Null; /* Comment */
909 pMem->type = SQLITE_NULL;
910 }
danielk19770d78bae2008-01-03 07:09:48 +0000911 }
912
913 p->nResColumn = 8 - 5*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +0000914 p->rc = SQLITE_OK;
915 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000916 }
drh826fb5a2004-02-14 23:59:57 +0000917 return rc;
drh9a324642003-09-06 20:12:01 +0000918}
drhb7f91642004-10-31 02:22:47 +0000919#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000920
drh7c4ac0c2007-04-05 11:25:58 +0000921#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000922/*
drh3f7d4e42004-07-24 14:35:58 +0000923** Print the SQL that was used to generate a VDBE program.
924*/
925void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000926 int nOp = p->nOp;
927 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000928 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000929 pOp = &p->aOp[0];
930 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000931 const char *z = pOp->p4.z;
drh4c755c02004-08-08 20:22:17 +0000932 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000933 printf("SQL: [%s]\n", z);
934 }
drh3f7d4e42004-07-24 14:35:58 +0000935}
drh7c4ac0c2007-04-05 11:25:58 +0000936#endif
drh3f7d4e42004-07-24 14:35:58 +0000937
drh602c2372007-03-01 00:29:13 +0000938#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
939/*
940** Print an IOTRACE message showing SQL content.
941*/
942void sqlite3VdbeIOTraceSql(Vdbe *p){
943 int nOp = p->nOp;
944 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +0000945 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +0000946 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000947 pOp = &p->aOp[0];
948 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +0000949 int i, j;
drh00a18e42007-08-13 11:10:34 +0000950 char z[1000];
drh949f9cd2008-01-12 21:35:57 +0000951 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk19772be2be92007-05-16 17:50:45 +0000952 for(i=0; isspace((unsigned char)z[i]); i++){}
drh602c2372007-03-01 00:29:13 +0000953 for(j=0; z[i]; i++){
danielk19772be2be92007-05-16 17:50:45 +0000954 if( isspace((unsigned char)z[i]) ){
drh602c2372007-03-01 00:29:13 +0000955 if( z[i-1]!=' ' ){
956 z[j++] = ' ';
957 }
958 }else{
959 z[j++] = z[i];
960 }
961 }
962 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000963 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +0000964 }
965}
966#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
967
968
drh3f7d4e42004-07-24 14:35:58 +0000969/*
drh9a324642003-09-06 20:12:01 +0000970** Prepare a virtual machine for execution. This involves things such
971** as allocating stack space and initializing the program counter.
972** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000973** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000974**
975** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
976** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000977*/
danielk19774adee202004-05-08 08:23:19 +0000978void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000979 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000980 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000981 int nMem, /* Number of memory cells to allocate */
982 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000983 int isExplain /* True if the EXPLAIN keywords is present */
984){
985 int n;
danielk19771e536952007-08-16 10:09:01 +0000986 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000987
988 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000989 assert( p->magic==VDBE_MAGIC_INIT );
990
drhc16a03b2004-09-15 13:38:10 +0000991 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000992 */
drhc16a03b2004-09-15 13:38:10 +0000993 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000994
danielk1977634f2982005-03-28 08:44:07 +0000995 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
996 * is because the call to resizeOpArray() below may shrink the
997 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
998 * state.
999 */
1000 p->magic = VDBE_MAGIC_RUN;
1001
danielk1977cd3e8f72008-03-25 09:47:35 +00001002 /* For each cursor required, also allocate a memory cell. Memory
1003 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1004 ** the vdbe program. Instead they are used to allocate space for
1005 ** Cursor/BtCursor structures. The blob of memory associated with
1006 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1007 ** stores the blob of memory associated with cursor 1, etc.
1008 **
1009 ** See also: allocateCursor().
1010 */
1011 nMem += nCursor;
1012
drh9cbf3422008-01-17 16:22:13 +00001013 /*
1014 ** Allocation space for registers.
drh9a324642003-09-06 20:12:01 +00001015 */
drh9cbf3422008-01-17 16:22:13 +00001016 if( p->aMem==0 ){
danielk1977634f2982005-03-28 08:44:07 +00001017 int nArg; /* Maximum number of args passed to a user function. */
drh9cbf3422008-01-17 16:22:13 +00001018 resolveP2Values(p, &nArg);
drh26c9b5e2008-04-11 14:56:53 +00001019 /*resizeOpArray(p, p->nOp);*/
drh82a48512003-09-06 22:45:20 +00001020 assert( nVar>=0 );
drh9cbf3422008-01-17 16:22:13 +00001021 if( isExplain && nMem<10 ){
1022 p->nMem = nMem = 10;
drh0f7eb612006-08-08 13:51:43 +00001023 }
drh9cbf3422008-01-17 16:22:13 +00001024 p->aMem = sqlite3DbMallocZero(db,
1025 nMem*sizeof(Mem) /* aMem */
drh86f43302004-10-05 17:37:36 +00001026 + nVar*sizeof(Mem) /* aVar */
drh9cbf3422008-01-17 16:22:13 +00001027 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +00001028 + nVar*sizeof(char*) /* azVar */
drh0a07c102008-01-03 18:03:08 +00001029 + nCursor*sizeof(Cursor*) + 1 /* apCsr */
drh82a48512003-09-06 22:45:20 +00001030 );
drh17435752007-08-16 04:30:38 +00001031 if( !db->mallocFailed ){
drh9cbf3422008-01-17 16:22:13 +00001032 p->aMem--; /* aMem[] goes from 1..nMem */
1033 p->nMem = nMem; /* not from 0..nMem-1 */
drh0a07c102008-01-03 18:03:08 +00001034 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +00001035 p->nVar = nVar;
1036 p->okVar = 0;
1037 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +00001038 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +00001039 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +00001040 p->nCursor = nCursor;
1041 for(n=0; n<nVar; n++){
1042 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001043 p->aVar[n].db = db;
1044 }
drh9cbf3422008-01-17 16:22:13 +00001045 for(n=1; n<=nMem; n++){
1046 p->aMem[n].flags = MEM_Null;
1047 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +00001048 }
danielk197754db47e2004-05-19 10:36:43 +00001049 }
drh82a48512003-09-06 22:45:20 +00001050 }
drh9cbf3422008-01-17 16:22:13 +00001051#ifdef SQLITE_DEBUG
1052 for(n=1; n<p->nMem; n++){
1053 assert( p->aMem[n].db==db );
danielk1977b3bce662005-01-29 08:32:43 +00001054 }
drh9cbf3422008-01-17 16:22:13 +00001055#endif
drh9a324642003-09-06 20:12:01 +00001056
danielk19771d850a72004-05-31 08:26:49 +00001057 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001058 p->rc = SQLITE_OK;
1059 p->uniqueCnt = 0;
drh9a324642003-09-06 20:12:01 +00001060 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001061 p->explain |= isExplain;
1062 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001063 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001064 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001065 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +00001066 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +00001067#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001068 {
1069 int i;
1070 for(i=0; i<p->nOp; i++){
1071 p->aOp[i].cnt = 0;
1072 p->aOp[i].cycles = 0;
1073 }
drh9a324642003-09-06 20:12:01 +00001074 }
1075#endif
1076}
1077
drh9a324642003-09-06 20:12:01 +00001078/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001079** Close a VDBE cursor and release all the resources that cursor
1080** happens to hold.
drh9a324642003-09-06 20:12:01 +00001081*/
danielk1977be718892006-06-23 08:05:19 +00001082void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
drh4774b132004-06-12 20:12:51 +00001083 if( pCx==0 ){
1084 return;
1085 }
drh9a324642003-09-06 20:12:01 +00001086 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +00001087 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001088 }
1089 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001090 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +00001091 }
drh9eff6162006-06-12 21:59:13 +00001092#ifndef SQLITE_OMIT_VIRTUALTABLE
1093 if( pCx->pVtabCursor ){
1094 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001095 const sqlite3_module *pModule = pCx->pModule;
1096 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001097 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001098 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001099 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001100 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001101 }
1102#endif
danielk19779882d992008-03-27 17:59:01 +00001103 if( !pCx->ephemPseudoTable ){
1104 sqlite3_free(pCx->pData);
1105 }
drh5c070532008-04-11 15:36:03 +00001106 /* memset(pCx, 0, sizeof(Cursor)); */
danielk1977cd3e8f72008-03-25 09:47:35 +00001107 /* sqlite3_free(pCx->aType); */
1108 /* sqlite3_free(pCx); */
drh9a324642003-09-06 20:12:01 +00001109}
1110
1111/*
drhff0587c2007-08-29 17:43:19 +00001112** Close all cursors except for VTab cursors that are currently
1113** in use.
drh9a324642003-09-06 20:12:01 +00001114*/
drhff0587c2007-08-29 17:43:19 +00001115static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001116 int i;
drh290c1942004-08-21 17:54:45 +00001117 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001118 for(i=0; i<p->nCursor; i++){
drhff0587c2007-08-29 17:43:19 +00001119 Cursor *pC = p->apCsr[i];
1120 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1121 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001122 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001123 }
drh9a324642003-09-06 20:12:01 +00001124 }
drh9a324642003-09-06 20:12:01 +00001125}
1126
1127/*
drh9a324642003-09-06 20:12:01 +00001128** Clean up the VM after execution.
1129**
1130** This routine will automatically close any cursors, lists, and/or
1131** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001132** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001133*/
danielk1977dfb316d2008-03-26 18:34:43 +00001134static void Cleanup(Vdbe *p, int freebuffers){
drh9a324642003-09-06 20:12:01 +00001135 int i;
drhff0587c2007-08-29 17:43:19 +00001136 closeAllCursorsExceptActiveVtabs(p);
danielk1977a7a8e142008-02-13 18:25:27 +00001137 for(i=1; i<=p->nMem; i++){
1138 MemSetTypeFlag(&p->aMem[i], MEM_Null);
1139 }
danielk1977dfb316d2008-03-26 18:34:43 +00001140 releaseMemArray(&p->aMem[1], p->nMem, freebuffers);
drha01f79d2005-07-08 13:07:59 +00001141 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001142 if( p->contextStack ){
1143 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001144 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001145 }
drh17435752007-08-16 04:30:38 +00001146 sqlite3_free(p->contextStack);
drh344737f2004-09-19 00:50:20 +00001147 }
drh5f968432004-02-21 19:02:30 +00001148 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001149 p->contextStackDepth = 0;
1150 p->contextStackTop = 0;
drh17435752007-08-16 04:30:38 +00001151 sqlite3_free(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001152 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001153 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001154}
1155
1156/*
danielk197722322fd2004-05-25 23:35:17 +00001157** Set the number of result columns that will be returned by this SQL
1158** statement. This is now set at compile time, rather than during
1159** execution of the vdbe program so that sqlite3_column_count() can
1160** be called on an SQL statement before sqlite3_step().
1161*/
1162void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001163 Mem *pColName;
1164 int n;
drh4a50aac2007-08-23 02:47:53 +00001165
danielk1977dfb316d2008-03-26 18:34:43 +00001166 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1);
drh17435752007-08-16 04:30:38 +00001167 sqlite3_free(p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001168 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001169 p->nResColumn = nResColumn;
danielk19771e536952007-08-16 10:09:01 +00001170 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001171 if( p->aColName==0 ) return;
1172 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001173 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001174 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001175 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001176 }
danielk197722322fd2004-05-25 23:35:17 +00001177}
1178
1179/*
danielk19773cf86062004-05-26 10:11:05 +00001180** Set the name of the idx'th column to be returned by the SQL statement.
1181** zName must be a pointer to a nul terminated string.
1182**
1183** This call must be made after a call to sqlite3VdbeSetNumCols().
1184**
drh66a51672008-01-03 00:01:23 +00001185** If N==P4_STATIC it means that zName is a pointer to a constant static
1186** string and we can just copy the pointer. If it is P4_DYNAMIC, then
drh17435752007-08-16 04:30:38 +00001187** the string is freed using sqlite3_free() when the vdbe is finished with
danielk1977d8123362004-06-12 09:25:12 +00001188** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001189*/
danielk1977955de522006-02-10 02:27:42 +00001190int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001191 int rc;
1192 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001193 assert( idx<p->nResColumn );
1194 assert( var<COLNAME_N );
drh17435752007-08-16 04:30:38 +00001195 if( p->db->mallocFailed ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001196 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001197 pColName = &(p->aColName[idx+var*p->nResColumn]);
drh66a51672008-01-03 00:01:23 +00001198 if( N==P4_DYNAMIC || N==P4_STATIC ){
drhb21c8cd2007-08-21 19:33:56 +00001199 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001200 }else{
drhb21c8cd2007-08-21 19:33:56 +00001201 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001202 }
drh66a51672008-01-03 00:01:23 +00001203 if( rc==SQLITE_OK && N==P4_DYNAMIC ){
danielk19775f096132008-03-28 15:44:09 +00001204 pColName->flags &= (~MEM_Static);
1205 pColName->zMalloc = pColName->z;
danielk19773cf86062004-05-26 10:11:05 +00001206 }
1207 return rc;
1208}
1209
danielk197713adf8a2004-06-03 16:08:41 +00001210/*
1211** A read or write transaction may or may not be active on database handle
1212** db. If a transaction is active, commit it. If there is a
1213** write-transaction spanning more than one database file, this routine
1214** takes care of the master journal trickery.
1215*/
drh9bb575f2004-09-06 17:24:11 +00001216static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001217 int i;
1218 int nTrans = 0; /* Number of databases with an active write-transaction */
1219 int rc = SQLITE_OK;
1220 int needXcommit = 0;
1221
danielk19775bd270b2006-07-25 15:14:52 +00001222 /* Before doing anything else, call the xSync() callback for any
1223 ** virtual module tables written in this transaction. This has to
1224 ** be done before determining whether a master journal file is
1225 ** required, as an xSync() callback may add an attached database
1226 ** to the transaction.
1227 */
1228 rc = sqlite3VtabSync(db, rc);
1229 if( rc!=SQLITE_OK ){
1230 return rc;
1231 }
1232
1233 /* This loop determines (a) if the commit hook should be invoked and
1234 ** (b) how many database files have open write transactions, not
1235 ** including the temp database. (b) is important because if more than
1236 ** one database file has an open write transaction, a master journal
1237 ** file is required for an atomic commit.
1238 */
danielk197713adf8a2004-06-03 16:08:41 +00001239 for(i=0; i<db->nDb; i++){
1240 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001241 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001242 needXcommit = 1;
1243 if( i!=1 ) nTrans++;
1244 }
1245 }
1246
1247 /* If there are any write-transactions at all, invoke the commit hook */
1248 if( needXcommit && db->xCommitCallback ){
drh7e8b8482008-01-23 03:03:05 +00001249 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001250 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001251 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001252 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001253 return SQLITE_CONSTRAINT;
1254 }
1255 }
1256
danielk197740b38dc2004-06-26 08:38:24 +00001257 /* The simple case - no more than one database file (not counting the
1258 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001259 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001260 **
danielk197740b38dc2004-06-26 08:38:24 +00001261 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001262 ** string, it means the main database is :memory: or a temp file. In
1263 ** that case we do not support atomic multi-file commits, so use the
1264 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001265 */
danielk197740b38dc2004-06-26 08:38:24 +00001266 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001267 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001268 Btree *pBt = db->aDb[i].pBt;
1269 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001270 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001271 }
1272 }
1273
drh80e35f42007-03-30 14:06:34 +00001274 /* Do the commit only if all databases successfully complete phase 1.
1275 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1276 ** IO error while deleting or truncating a journal file. It is unlikely,
1277 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001278 */
1279 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1280 Btree *pBt = db->aDb[i].pBt;
1281 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001282 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001283 }
danielk1977979f38e2007-03-27 16:19:51 +00001284 }
1285 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001286 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001287 }
1288 }
1289
1290 /* The complex case - There is a multi-file write-transaction active.
1291 ** This requires a master journal file to ensure the transaction is
1292 ** committed atomicly.
1293 */
danielk197744ee5bf2005-05-27 09:41:12 +00001294#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001295 else{
danielk1977b4b47412007-08-17 15:53:36 +00001296 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001297 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001298 char *zMaster = 0; /* File-name for the master journal */
1299 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001300 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001301 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001302 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001303
1304 /* Select a master journal file name */
1305 do {
drha6abd042004-06-09 17:37:22 +00001306 u32 random;
drh17435752007-08-16 04:30:38 +00001307 sqlite3_free(zMaster);
drh2fa18682008-03-19 14:15:34 +00001308 sqlite3_randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001309 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001310 if( !zMaster ){
1311 return SQLITE_NOMEM;
1312 }
danielk1977861f7452008-06-05 11:39:11 +00001313 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1314 }while( rc==SQLITE_OK && res );
1315 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001316 /* Open the master journal. */
1317 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1318 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1319 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1320 );
1321 }
danielk197713adf8a2004-06-03 16:08:41 +00001322 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001323 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001324 return rc;
1325 }
1326
1327 /* Write the name of each database file in the transaction into the new
1328 ** master journal file. If an error occurs at this point close
1329 ** and delete the master journal file. All the individual journal files
1330 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001331 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001332 */
danielk19771e536952007-08-16 10:09:01 +00001333 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001334 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001335 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001336 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001337 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001338 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001339 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1340 needSync = 1;
1341 }
danielk1977b4b47412007-08-17 15:53:36 +00001342 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001343 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001344 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001345 sqlite3OsCloseFree(pMaster);
1346 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001347 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001348 return rc;
1349 }
1350 }
1351 }
1352
danielk19779663b8f2007-08-24 11:52:28 +00001353 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1354 ** flag is set this is not required.
1355 */
danielk19775865e3d2004-06-14 06:03:57 +00001356 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001357 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001358 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001359 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001360 sqlite3OsCloseFree(pMaster);
1361 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001362 sqlite3_free(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001363 return rc;
1364 }
drhc9e06862004-06-09 20:03:08 +00001365
danielk197713adf8a2004-06-03 16:08:41 +00001366 /* Sync all the db files involved in the transaction. The same call
1367 ** sets the master journal pointer in each individual journal. If
1368 ** an error occurs here, do not delete the master journal file.
1369 **
drh80e35f42007-03-30 14:06:34 +00001370 ** If the error occurs during the first call to
1371 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1372 ** master journal file will be orphaned. But we cannot delete it,
1373 ** in case the master journal file name was written into the journal
1374 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001375 */
danielk19775bd270b2006-07-25 15:14:52 +00001376 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001377 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001378 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001379 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001380 }
1381 }
danielk1977fee2d252007-08-18 10:59:19 +00001382 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001383 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001384 sqlite3_free(zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001385 return rc;
1386 }
danielk197713adf8a2004-06-03 16:08:41 +00001387
danielk1977962398d2004-06-14 09:35:16 +00001388 /* Delete the master journal file. This commits the transaction. After
1389 ** doing this the directory is synced again before any individual
1390 ** transaction files are deleted.
1391 */
danielk1977fee2d252007-08-18 10:59:19 +00001392 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh17435752007-08-16 04:30:38 +00001393 sqlite3_free(zMaster);
drhc416ba92007-03-30 18:42:55 +00001394 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001395 if( rc ){
1396 return rc;
1397 }
danielk197713adf8a2004-06-03 16:08:41 +00001398
1399 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001400 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1401 ** deleting or truncating journals. If something goes wrong while
1402 ** this is happening we don't really care. The integrity of the
1403 ** transaction is already guaranteed, but some stray 'cold' journals
1404 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001405 */
danielk1977979f38e2007-03-27 16:19:51 +00001406 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001407 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001408 for(i=0; i<db->nDb; i++){
1409 Btree *pBt = db->aDb[i].pBt;
1410 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001411 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001412 }
1413 }
danielk19772d1d86f2008-06-20 14:59:51 +00001414 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001415 enable_simulated_io_errors();
1416
danielk1977f9e7dda2006-06-16 16:08:53 +00001417 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001418 }
danielk197744ee5bf2005-05-27 09:41:12 +00001419#endif
danielk1977026d2702004-06-14 13:14:59 +00001420
drh2ac3ee92004-06-07 16:27:46 +00001421 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001422}
1423
danielk19771d850a72004-05-31 08:26:49 +00001424/*
1425** This routine checks that the sqlite3.activeVdbeCnt count variable
1426** matches the number of vdbe's in the list sqlite3.pVdbe that are
1427** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001428** This is an internal self-check only - it is not an essential processing
1429** step.
danielk19771d850a72004-05-31 08:26:49 +00001430**
1431** This is a no-op if NDEBUG is defined.
1432*/
1433#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001434static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001435 Vdbe *p;
1436 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001437 p = db->pVdbe;
1438 while( p ){
drh92f02c32004-09-02 14:57:08 +00001439 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001440 cnt++;
1441 }
1442 p = p->pNext;
1443 }
danielk19771d850a72004-05-31 08:26:49 +00001444 assert( cnt==db->activeVdbeCnt );
1445}
1446#else
1447#define checkActiveVdbeCnt(x)
1448#endif
1449
danielk19773cf86062004-05-26 10:11:05 +00001450/*
drhfb982642007-08-30 01:19:59 +00001451** For every Btree that in database connection db which
1452** has been modified, "trip" or invalidate each cursor in
1453** that Btree might have been modified so that the cursor
1454** can never be used again. This happens when a rollback
1455*** occurs. We have to trip all the other cursors, even
1456** cursor from other VMs in different database connections,
1457** so that none of them try to use the data at which they
1458** were pointing and which now may have been changed due
1459** to the rollback.
1460**
1461** Remember that a rollback can delete tables complete and
1462** reorder rootpages. So it is not sufficient just to save
1463** the state of the cursor. We have to invalidate the cursor
1464** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001465*/
drhade6c9c2007-11-24 10:23:44 +00001466static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001467 int i;
1468 for(i=0; i<db->nDb; i++){
1469 Btree *p = db->aDb[i].pBt;
1470 if( p && sqlite3BtreeIsInTrans(p) ){
1471 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1472 }
danielk1977be718892006-06-23 08:05:19 +00001473 }
1474}
1475
1476/*
drh92f02c32004-09-02 14:57:08 +00001477** This routine is called the when a VDBE tries to halt. If the VDBE
1478** has made changes and is in autocommit mode, then commit those
1479** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001480**
drh92f02c32004-09-02 14:57:08 +00001481** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001482** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1483** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001484**
1485** Return an error code. If the commit could not complete because of
1486** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1487** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001488*/
drhff0587c2007-08-29 17:43:19 +00001489int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001490 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001491 int i;
danielk19771d850a72004-05-31 08:26:49 +00001492 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001493 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1494
1495 /* This function contains the logic that determines if a statement or
1496 ** transaction will be committed or rolled back as a result of the
1497 ** execution of this virtual machine.
1498 **
drh71b890a2007-10-03 15:30:52 +00001499 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001500 **
drh71b890a2007-10-03 15:30:52 +00001501 ** SQLITE_NOMEM
1502 ** SQLITE_IOERR
1503 ** SQLITE_FULL
1504 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001505 **
drh71b890a2007-10-03 15:30:52 +00001506 ** Then the internal cache might have been left in an inconsistent
1507 ** state. We need to rollback the statement transaction, if there is
1508 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001509 */
drh9a324642003-09-06 20:12:01 +00001510
drh17435752007-08-16 04:30:38 +00001511 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001512 p->rc = SQLITE_NOMEM;
1513 }
drhff0587c2007-08-29 17:43:19 +00001514 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001515 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001516 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001517 }
danielk19771d850a72004-05-31 08:26:49 +00001518 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001519
danielk197707cb5602006-01-20 10:55:05 +00001520 /* No commit or rollback needed if the program never started */
1521 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001522 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001523
1524 /* Lock all btrees used by the statement */
1525 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1526
drh71b890a2007-10-03 15:30:52 +00001527 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001528 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001529 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001530 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001531 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001532 /* This loop does static analysis of the query to see which of the
1533 ** following three categories it falls into:
1534 **
1535 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001536 ** Query with statement journal
1537 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001538 **
1539 ** We could do something more elegant than this static analysis (i.e.
1540 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001541 ** handling malloc() or IO failure is a fairly obscure edge case so
1542 ** this is probably easier. Todo: Might be an opportunity to reduce
1543 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001544 */
drhd1817042007-10-03 18:45:04 +00001545 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001546 int isStatement = 0;
1547 assert(p->aOp || p->nOp==0);
1548 for(i=0; i<p->nOp; i++){
1549 switch( p->aOp[i].opcode ){
1550 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001551 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001552 break;
1553 case OP_Statement:
1554 isStatement = 1;
1555 break;
1556 }
1557 }
drhff0587c2007-08-29 17:43:19 +00001558
1559
danielk197707cb5602006-01-20 10:55:05 +00001560 /* If the query was read-only, we need do no rollback at all. Otherwise,
1561 ** proceed with the special handling.
1562 */
drhd1817042007-10-03 18:45:04 +00001563 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001564 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1565 xFunc = sqlite3BtreeRollbackStmt;
1566 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001567 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001568 xFunc = sqlite3BtreeRollbackStmt;
1569 }else{
1570 /* We are forced to roll back the active transaction. Before doing
1571 ** so, abort any other statements this handle currently has active.
1572 */
drhfb982642007-08-30 01:19:59 +00001573 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001574 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001575 db->autoCommit = 1;
1576 }
danielk1977261919c2005-12-06 12:52:59 +00001577 }
1578 }
danielk197707cb5602006-01-20 10:55:05 +00001579
1580 /* If the auto-commit flag is set and this is the only active vdbe, then
1581 ** we do either a commit or rollback of the current transaction.
1582 **
1583 ** Note: This block also runs if one of the special errors handled
1584 ** above has occured.
1585 */
1586 if( db->autoCommit && db->activeVdbeCnt==1 ){
1587 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001588 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001589 ** successful or hit an 'OR FAIL' constraint. This means a commit
1590 ** is required.
1591 */
1592 int rc = vdbeCommit(db);
1593 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001594 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001595 return SQLITE_BUSY;
1596 }else if( rc!=SQLITE_OK ){
1597 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001598 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001599 }else{
1600 sqlite3CommitInternalChanges(db);
1601 }
1602 }else{
danielk197797a227c2006-01-20 16:32:04 +00001603 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001604 }
1605 }else if( !xFunc ){
1606 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001607 if( p->openedStatement ){
1608 xFunc = sqlite3BtreeCommitStmt;
1609 }
danielk197707cb5602006-01-20 10:55:05 +00001610 }else if( p->errorAction==OE_Abort ){
1611 xFunc = sqlite3BtreeRollbackStmt;
1612 }else{
drhfb982642007-08-30 01:19:59 +00001613 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001614 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001615 db->autoCommit = 1;
1616 }
danielk19771d850a72004-05-31 08:26:49 +00001617 }
danielk197707cb5602006-01-20 10:55:05 +00001618
1619 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1620 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1621 ** and the return code is still SQLITE_OK, set the return code to the new
1622 ** error value.
1623 */
1624 assert(!xFunc ||
1625 xFunc==sqlite3BtreeCommitStmt ||
1626 xFunc==sqlite3BtreeRollbackStmt
1627 );
1628 for(i=0; xFunc && i<db->nDb; i++){
1629 int rc;
1630 Btree *pBt = db->aDb[i].pBt;
1631 if( pBt ){
1632 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001633 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1634 p->rc = rc;
1635 sqlite3SetString(&p->zErrMsg, 0);
1636 }
danielk197707cb5602006-01-20 10:55:05 +00001637 }
danielk197777d83ba2004-05-31 10:08:14 +00001638 }
danielk197707cb5602006-01-20 10:55:05 +00001639
1640 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1641 ** set the change counter.
1642 */
1643 if( p->changeCntOn && p->pc>=0 ){
1644 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1645 sqlite3VdbeSetChanges(db, p->nChange);
1646 }else{
1647 sqlite3VdbeSetChanges(db, 0);
1648 }
1649 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001650 }
danielk197707cb5602006-01-20 10:55:05 +00001651
1652 /* Rollback or commit any schema changes that occurred. */
1653 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1654 sqlite3ResetInternalSchema(db, 0);
1655 db->flags = (db->flags | SQLITE_InternChanges);
1656 }
drhff0587c2007-08-29 17:43:19 +00001657
1658 /* Release the locks */
1659 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001660 }
danielk19771d850a72004-05-31 08:26:49 +00001661
danielk197765fd59f2006-06-24 11:51:33 +00001662 /* We have successfully halted and closed the VM. Record this fact. */
1663 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001664 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001665 }
drh92f02c32004-09-02 14:57:08 +00001666 p->magic = VDBE_MAGIC_HALT;
1667 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001668 if( p->db->mallocFailed ){
1669 p->rc = SQLITE_NOMEM;
1670 }
danielk19771d850a72004-05-31 08:26:49 +00001671
drh92f02c32004-09-02 14:57:08 +00001672 return SQLITE_OK;
1673}
drh4cf7c7f2007-08-28 23:28:07 +00001674
drh92f02c32004-09-02 14:57:08 +00001675
1676/*
drh3c23a882007-01-09 14:01:13 +00001677** Each VDBE holds the result of the most recent sqlite3_step() call
1678** in p->rc. This routine sets that result back to SQLITE_OK.
1679*/
1680void sqlite3VdbeResetStepResult(Vdbe *p){
1681 p->rc = SQLITE_OK;
1682}
1683
1684/*
drh92f02c32004-09-02 14:57:08 +00001685** Clean up a VDBE after execution but do not delete the VDBE just yet.
1686** Write any error messages into *pzErrMsg. Return the result code.
1687**
1688** After this routine is run, the VDBE should be ready to be executed
1689** again.
1690**
1691** To look at it another way, this routine resets the state of the
1692** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1693** VDBE_MAGIC_INIT.
1694*/
danielk1977dfb316d2008-03-26 18:34:43 +00001695int sqlite3VdbeReset(Vdbe *p, int freebuffers){
drh4ac285a2006-09-15 07:28:50 +00001696 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001697 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001698
1699 /* If the VM did not run to completion or if it encountered an
1700 ** error, then it might not have been halted properly. So halt
1701 ** it now.
1702 */
drh7e8b8482008-01-23 03:03:05 +00001703 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001704 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001705 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001706
drhfb7e7652005-01-24 00:28:42 +00001707 /* If the VDBE has be run even partially, then transfer the error code
1708 ** and error message from the VDBE into the main database structure. But
1709 ** if the VDBE has just been set to run but has not actually executed any
1710 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001711 */
drhfb7e7652005-01-24 00:28:42 +00001712 if( p->pc>=0 ){
1713 if( p->zErrMsg ){
drhb21c8cd2007-08-21 19:33:56 +00001714 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
danielk197797a227c2006-01-20 16:32:04 +00001715 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001716 p->zErrMsg = 0;
1717 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001718 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001719 }else{
drh4ac285a2006-09-15 07:28:50 +00001720 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001721 }
danielk1977a21c6b62005-01-24 10:25:59 +00001722 }else if( p->rc && p->expired ){
1723 /* The expired flag was set on the VDBE before the first call
1724 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1725 ** called), set the database error in this case as well.
1726 */
drh4ac285a2006-09-15 07:28:50 +00001727 sqlite3Error(db, p->rc, 0);
danielk19778e556522007-11-13 10:30:24 +00001728 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1729 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001730 }
1731
1732 /* Reclaim all memory used by the VDBE
1733 */
danielk1977dfb316d2008-03-26 18:34:43 +00001734 Cleanup(p, freebuffers);
drh92f02c32004-09-02 14:57:08 +00001735
1736 /* Save profiling information from this VDBE run.
1737 */
drh9a324642003-09-06 20:12:01 +00001738#ifdef VDBE_PROFILE
1739 {
1740 FILE *out = fopen("vdbe_profile.out", "a");
1741 if( out ){
1742 int i;
1743 fprintf(out, "---- ");
1744 for(i=0; i<p->nOp; i++){
1745 fprintf(out, "%02x", p->aOp[i].opcode);
1746 }
1747 fprintf(out, "\n");
1748 for(i=0; i<p->nOp; i++){
1749 fprintf(out, "%6d %10lld %8lld ",
1750 p->aOp[i].cnt,
1751 p->aOp[i].cycles,
1752 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1753 );
danielk19774adee202004-05-08 08:23:19 +00001754 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001755 }
1756 fclose(out);
1757 }
1758 }
1759#endif
1760 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001761 p->aborted = 0;
drh4ac285a2006-09-15 07:28:50 +00001762 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001763}
drh92f02c32004-09-02 14:57:08 +00001764
drh9a324642003-09-06 20:12:01 +00001765/*
1766** Clean up and delete a VDBE after execution. Return an integer which is
1767** the result code. Write any error message text into *pzErrMsg.
1768*/
danielk19779e6db7d2004-06-21 08:18:51 +00001769int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001770 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001771 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
danielk1977dfb316d2008-03-26 18:34:43 +00001772 rc = sqlite3VdbeReset(p, 1);
drh4ac285a2006-09-15 07:28:50 +00001773 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001774 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001775 return SQLITE_MISUSE;
1776 }
danielk1977dfb316d2008-03-26 18:34:43 +00001777 releaseMemArray(&p->aMem[1], p->nMem, 1);
danielk19774adee202004-05-08 08:23:19 +00001778 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001779 return rc;
1780}
1781
1782/*
drhf92c7ff2004-06-19 15:40:23 +00001783** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001784** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001785** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001786** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001787*/
1788void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1789 int i;
1790 for(i=0; i<pVdbeFunc->nAux; i++){
1791 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1792 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1793 if( pAux->xDelete ){
1794 pAux->xDelete(pAux->pAux);
1795 }
1796 pAux->pAux = 0;
1797 }
1798 }
1799}
1800
1801/*
drh9a324642003-09-06 20:12:01 +00001802** Delete an entire VDBE.
1803*/
danielk19774adee202004-05-08 08:23:19 +00001804void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001805 int i;
1806 if( p==0 ) return;
danielk1977dfb316d2008-03-26 18:34:43 +00001807 Cleanup(p, 1);
drh9a324642003-09-06 20:12:01 +00001808 if( p->pPrev ){
1809 p->pPrev->pNext = p->pNext;
1810 }else{
1811 assert( p->db->pVdbe==p );
1812 p->db->pVdbe = p->pNext;
1813 }
1814 if( p->pNext ){
1815 p->pNext->pPrev = p->pPrev;
1816 }
drh76ff3a02004-09-24 22:32:30 +00001817 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001818 Op *pOp = p->aOp;
1819 for(i=0; i<p->nOp; i++, pOp++){
drh66a51672008-01-03 00:01:23 +00001820 freeP4(pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001821#ifdef SQLITE_DEBUG
1822 sqlite3_free(pOp->zComment);
1823#endif
drh9a324642003-09-06 20:12:01 +00001824 }
drh17435752007-08-16 04:30:38 +00001825 sqlite3_free(p->aOp);
drh9a324642003-09-06 20:12:01 +00001826 }
danielk1977dfb316d2008-03-26 18:34:43 +00001827 releaseMemArray(p->aVar, p->nVar, 1);
drh17435752007-08-16 04:30:38 +00001828 sqlite3_free(p->aLabel);
drh9cbf3422008-01-17 16:22:13 +00001829 if( p->aMem ){
1830 sqlite3_free(&p->aMem[1]);
1831 }
danielk1977dfb316d2008-03-26 18:34:43 +00001832 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1);
drh17435752007-08-16 04:30:38 +00001833 sqlite3_free(p->aColName);
1834 sqlite3_free(p->zSql);
drh9a324642003-09-06 20:12:01 +00001835 p->magic = VDBE_MAGIC_DEAD;
drh17435752007-08-16 04:30:38 +00001836 sqlite3_free(p);
drh9a324642003-09-06 20:12:01 +00001837}
drha11846b2004-01-07 18:52:56 +00001838
1839/*
drha11846b2004-01-07 18:52:56 +00001840** If a MoveTo operation is pending on the given cursor, then do that
1841** MoveTo now. Return an error code. If no MoveTo is pending, this
1842** routine does nothing and returns SQLITE_OK.
1843*/
danielk19774adee202004-05-08 08:23:19 +00001844int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001845 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001846 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001847#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001848 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001849#endif
drhf0863fe2005-06-12 21:35:51 +00001850 assert( p->isTable );
drhe14006d2008-03-25 17:23:32 +00001851 rc = sqlite3BtreeMoveto(p->pCursor, 0, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001852 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001853 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001854 p->lastRowid = keyToInt(p->movetoTarget);
1855 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001856 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001857 rc = sqlite3BtreeNext(p->pCursor, &res);
1858 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001859 }
drh10cfdd52006-08-08 15:42:59 +00001860#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001861 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001862#endif
drha11846b2004-01-07 18:52:56 +00001863 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001864 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001865 }
1866 return SQLITE_OK;
1867}
danielk19774adee202004-05-08 08:23:19 +00001868
drhab9f7f12004-05-08 10:56:11 +00001869/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001870** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001871**
danielk1977cfcdaef2004-05-12 07:33:33 +00001872** sqlite3VdbeSerialType()
1873** sqlite3VdbeSerialTypeLen()
1874** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001875** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001876** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001877**
1878** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001879** data and index records. Each serialized value consists of a
1880** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1881** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001882**
danielk1977cfcdaef2004-05-12 07:33:33 +00001883** In an SQLite index record, the serial type is stored directly before
1884** the blob of data that it corresponds to. In a table record, all serial
1885** types are stored at the start of the record, and the blobs of data at
1886** the end. Hence these functions allow the caller to handle the
1887** serial-type and data blob seperately.
1888**
1889** The following table describes the various storage classes for data:
1890**
1891** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001892** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001893** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001894** 1 1 signed integer
1895** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001896** 3 3 signed integer
1897** 4 4 signed integer
1898** 5 6 signed integer
1899** 6 8 signed integer
1900** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001901** 8 0 Integer constant 0
1902** 9 0 Integer constant 1
1903** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001904** N>=12 and even (N-12)/2 BLOB
1905** N>=13 and odd (N-13)/2 text
1906**
drh35a59652006-01-02 18:24:40 +00001907** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1908** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001909*/
1910
1911/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001912** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001913*/
drhd946db02005-12-29 19:23:06 +00001914u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001915 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001916 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001917
1918 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001919 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001920 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001921 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001922 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00001923# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001924 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001925 u64 u;
1926 if( file_format>=4 && (i&1)==i ){
1927 return 8+i;
1928 }
1929 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001930 if( u<=127 ) return 1;
1931 if( u<=32767 ) return 2;
1932 if( u<=8388607 ) return 3;
1933 if( u<=2147483647 ) return 4;
1934 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001935 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001936 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001937 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001938 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001939 }
drhfdf972a2007-05-02 13:30:27 +00001940 assert( flags&(MEM_Str|MEM_Blob) );
1941 n = pMem->n;
1942 if( flags & MEM_Zero ){
1943 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001944 }
drhfdf972a2007-05-02 13:30:27 +00001945 assert( n>=0 );
1946 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001947}
1948
1949/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001950** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001951*/
drh25aa1b42004-05-28 01:39:01 +00001952int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001953 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001954 return (serial_type-12)/2;
1955 }else{
drh57196282004-10-06 15:41:16 +00001956 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001957 return aSize[serial_type];
1958 }
danielk1977192ac1d2004-05-10 07:17:30 +00001959}
1960
1961/*
drh110daac2007-05-04 11:59:31 +00001962** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001963** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001964** upper 4 bytes. Return the result.
1965**
drh7a4f5022007-05-23 07:20:08 +00001966** For most architectures, this is a no-op.
1967**
1968** (later): It is reported to me that the mixed-endian problem
1969** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1970** that early versions of GCC stored the two words of a 64-bit
1971** float in the wrong order. And that error has been propagated
1972** ever since. The blame is not necessarily with GCC, though.
1973** GCC might have just copying the problem from a prior compiler.
1974** I am also told that newer versions of GCC that follow a different
1975** ABI get the byte order right.
1976**
1977** Developers using SQLite on an ARM7 should compile and run their
1978** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1979** enabled, some asserts below will ensure that the byte order of
1980** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00001981**
1982** (2007-08-30) Frank van Vugt has studied this problem closely
1983** and has send his findings to the SQLite developers. Frank
1984** writes that some Linux kernels offer floating point hardware
1985** emulation that uses only 32-bit mantissas instead of a full
1986** 48-bits as required by the IEEE standard. (This is the
1987** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1988** byte swapping becomes very complicated. To avoid problems,
1989** the necessary byte swapping is carried out using a 64-bit integer
1990** rather than a 64-bit float. Frank assures us that the code here
1991** works for him. We, the developers, have no way to independently
1992** verify this, but Frank seems to know what he is talking about
1993** so we trust him.
drh110daac2007-05-04 11:59:31 +00001994*/
1995#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00001996static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00001997 union {
drh60d09a72007-08-30 15:05:08 +00001998 u64 r;
drh110daac2007-05-04 11:59:31 +00001999 u32 i[2];
2000 } u;
2001 u32 t;
2002
2003 u.r = in;
2004 t = u.i[0];
2005 u.i[0] = u.i[1];
2006 u.i[1] = t;
2007 return u.r;
2008}
2009# define swapMixedEndianFloat(X) X = floatSwap(X)
2010#else
2011# define swapMixedEndianFloat(X)
2012#endif
2013
2014/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002015** Write the serialized data blob for the value stored in pMem into
2016** buf. It is assumed that the caller has allocated sufficient space.
2017** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002018**
2019** nBuf is the amount of space left in buf[]. nBuf must always be
2020** large enough to hold the entire field. Except, if the field is
2021** a blob with a zero-filled tail, then buf[] might be just the right
2022** size to hold everything except for the zero-filled tail. If buf[]
2023** is only big enough to hold the non-zero prefix, then only write that
2024** prefix into buf[]. But if buf[] is large enough to hold both the
2025** prefix and the tail then write the prefix and set the tail to all
2026** zeros.
2027**
2028** Return the number of bytes actually written into buf[]. The number
2029** of bytes in the zero-filled tail is included in the return value only
2030** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002031*/
drhfdf972a2007-05-02 13:30:27 +00002032int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002033 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00002034 int len;
danielk1977183f9f72004-05-13 05:20:26 +00002035
drh1483e142004-05-21 21:12:42 +00002036 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002037 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002038 u64 v;
2039 int i;
drha19b7752004-05-30 21:14:58 +00002040 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002041 assert( sizeof(v)==sizeof(pMem->r) );
2042 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002043 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002044 }else{
drh3c024d62007-03-30 11:23:45 +00002045 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002046 }
drh1483e142004-05-21 21:12:42 +00002047 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002048 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00002049 while( i-- ){
2050 buf[i] = (v&0xFF);
2051 v >>= 8;
2052 }
2053 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002054 }
drhd946db02005-12-29 19:23:06 +00002055
danielk1977cfcdaef2004-05-12 07:33:33 +00002056 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002057 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00002058 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
2059 == sqlite3VdbeSerialTypeLen(serial_type) );
2060 assert( pMem->n<=nBuf );
2061 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002062 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002063 if( pMem->flags & MEM_Zero ){
2064 len += pMem->u.i;
2065 if( len>nBuf ){
2066 len = nBuf;
2067 }
2068 memset(&buf[pMem->n], 0, len-pMem->n);
2069 }
drhd946db02005-12-29 19:23:06 +00002070 return len;
2071 }
2072
2073 /* NULL or constants 0 or 1 */
2074 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002075}
2076
2077/*
2078** Deserialize the data blob pointed to by buf as serial type serial_type
2079** and store the result in pMem. Return the number of bytes read.
2080*/
danielk1977b1bc9532004-05-22 03:05:33 +00002081int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002082 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002083 u32 serial_type, /* Serial type to deserialize */
2084 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002085){
drh3c685822005-05-21 18:32:18 +00002086 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002087 case 10: /* Reserved for future use */
2088 case 11: /* Reserved for future use */
2089 case 0: { /* NULL */
2090 pMem->flags = MEM_Null;
2091 break;
2092 }
2093 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002094 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002095 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002096 return 1;
drh1483e142004-05-21 21:12:42 +00002097 }
drh3c685822005-05-21 18:32:18 +00002098 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002099 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002100 pMem->flags = MEM_Int;
2101 return 2;
2102 }
2103 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002104 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002105 pMem->flags = MEM_Int;
2106 return 3;
2107 }
2108 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002109 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002110 pMem->flags = MEM_Int;
2111 return 4;
2112 }
2113 case 5: { /* 6-byte signed integer */
2114 u64 x = (((signed char)buf[0])<<8) | buf[1];
2115 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2116 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002117 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002118 pMem->flags = MEM_Int;
2119 return 6;
2120 }
drh91124b32005-08-18 18:15:05 +00002121 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002122 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002123 u64 x;
2124 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002125#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002126 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002127 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2128 ** defined that 64-bit floating point values really are mixed
2129 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002130 */
drhde941c62005-08-28 01:34:21 +00002131 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002132 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002133 u64 t2 = t1;
2134 swapMixedEndianFloat(t2);
2135 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002136#endif
drhbfd6b032005-08-28 01:38:44 +00002137
drhd81bd4e2005-09-05 20:06:49 +00002138 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2139 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002140 x = (x<<32) | y;
2141 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002142 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002143 pMem->flags = MEM_Int;
2144 }else{
drh4f0c5872007-03-26 22:05:01 +00002145 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002146 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002147 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002148 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002149 }
2150 return 8;
2151 }
drhd946db02005-12-29 19:23:06 +00002152 case 8: /* Integer 0 */
2153 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002154 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002155 pMem->flags = MEM_Int;
2156 return 0;
2157 }
drh3c685822005-05-21 18:32:18 +00002158 default: {
2159 int len = (serial_type-12)/2;
2160 pMem->z = (char *)buf;
2161 pMem->n = len;
2162 pMem->xDel = 0;
2163 if( serial_type&0x01 ){
2164 pMem->flags = MEM_Str | MEM_Ephem;
2165 }else{
2166 pMem->flags = MEM_Blob | MEM_Ephem;
2167 }
2168 return len;
drh696b32f2004-05-30 01:51:52 +00002169 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002170 }
drh3c685822005-05-21 18:32:18 +00002171 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002172}
2173
drh0e6082e2006-01-12 20:28:35 +00002174
drh1e968a02008-03-25 00:22:21 +00002175/*
2176** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002177** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002178** that structure.
2179**
2180** The calling function might provide szSpace bytes of memory
2181** space at pSpace. This space can be used to hold the returned
2182** VDbeParsedRecord structure if it is large enough. If it is
2183** not big enough, space is obtained from sqlite3_malloc().
2184**
2185** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002186** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002187*/
drhe14006d2008-03-25 17:23:32 +00002188UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002189 KeyInfo *pKeyInfo, /* Information about the record format */
2190 int nKey, /* Size of the binary record */
2191 const void *pKey, /* The binary record */
2192 void *pSpace, /* Space available to hold resulting object */
2193 int szSpace /* Size of pSpace[] in bytes */
2194){
2195 const unsigned char *aKey = (const unsigned char *)pKey;
drhe14006d2008-03-25 17:23:32 +00002196 UnpackedRecord *p;
drh1e968a02008-03-25 00:22:21 +00002197 int nByte;
2198 int i, idx, d;
2199 u32 szHdr;
2200 Mem *pMem;
2201
drhfab69592008-04-10 14:57:24 +00002202 assert( sizeof(Mem)>sizeof(*p) );
2203 nByte = sizeof(Mem)*(pKeyInfo->nField+2);
drh1e968a02008-03-25 00:22:21 +00002204 if( nByte>szSpace ){
2205 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2206 if( p==0 ) return 0;
2207 p->needFree = 1;
2208 }else{
2209 p = pSpace;
2210 p->needFree = 0;
2211 }
2212 p->pKeyInfo = pKeyInfo;
2213 p->nField = pKeyInfo->nField + 1;
2214 p->needDestroy = 1;
drhfab69592008-04-10 14:57:24 +00002215 p->aMem = pMem = &((Mem*)p)[1];
shane3f8d5cf2008-04-24 19:15:09 +00002216 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002217 d = szHdr;
2218 i = 0;
2219 while( idx<szHdr && i<p->nField ){
2220 u32 serial_type;
2221
shane3f8d5cf2008-04-24 19:15:09 +00002222 idx += getVarint32( aKey+idx, serial_type);
drh1e968a02008-03-25 00:22:21 +00002223 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
2224 pMem->enc = pKeyInfo->enc;
2225 pMem->db = pKeyInfo->db;
2226 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002227 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002228 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002229 pMem++;
2230 i++;
drh1e968a02008-03-25 00:22:21 +00002231 }
2232 p->nField = i;
2233 return (void*)p;
2234}
2235
2236/*
drhe14006d2008-03-25 17:23:32 +00002237** This routine destroys a UnpackedRecord object
drh1e968a02008-03-25 00:22:21 +00002238*/
drhe14006d2008-03-25 17:23:32 +00002239void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh1e968a02008-03-25 00:22:21 +00002240 if( p ){
2241 if( p->needDestroy ){
2242 int i;
drhe14006d2008-03-25 17:23:32 +00002243 Mem *pMem;
2244 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
danielk19775f096132008-03-28 15:44:09 +00002245 if( pMem->zMalloc ){
drhe14006d2008-03-25 17:23:32 +00002246 sqlite3VdbeMemRelease(pMem);
drh1e968a02008-03-25 00:22:21 +00002247 }
2248 }
2249 }
2250 if( p->needFree ){
2251 sqlite3_free(p);
2252 }
2253 }
2254}
2255
2256/*
2257** This function compares the two table rows or index records
2258** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
2259** or positive integer if {nKey1, pKey1} is less than, equal to or
2260** greater than pPKey2. The {nKey1, pKey1} key must be a blob
2261** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2262** key must be a parsed key such as obtained from
2263** sqlite3VdbeParseRecord.
2264**
2265** Key1 and Key2 do not have to contain the same number of fields.
2266** But if the lengths differ, Key2 must be the shorter of the two.
2267**
2268** Historical note: In earlier versions of this routine both Key1
2269** and Key2 were blobs obtained from OP_MakeRecord. But we found
2270** that in typical use the same Key2 would be submitted multiple times
2271** in a row. So an optimization was added to parse the Key2 key
2272** separately and submit the parsed version. In this way, we avoid
2273** parsing the same Key2 multiple times in a row.
2274*/
drhe14006d2008-03-25 17:23:32 +00002275int sqlite3VdbeRecordCompare(
drh1e968a02008-03-25 00:22:21 +00002276 int nKey1, const void *pKey1,
drhe14006d2008-03-25 17:23:32 +00002277 UnpackedRecord *pPKey2
drh1e968a02008-03-25 00:22:21 +00002278){
2279 u32 d1; /* Offset into aKey[] of next data element */
2280 u32 idx1; /* Offset into aKey[] of next header element */
2281 u32 szHdr1; /* Number of bytes in header */
2282 int i = 0;
2283 int nField;
2284 int rc = 0;
2285 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2286 KeyInfo *pKeyInfo;
2287 Mem mem1;
2288
2289 pKeyInfo = pPKey2->pKeyInfo;
2290 mem1.enc = pKeyInfo->enc;
2291 mem1.db = pKeyInfo->db;
2292 mem1.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002293 mem1.zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002294
shane3f8d5cf2008-04-24 19:15:09 +00002295 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002296 d1 = szHdr1;
2297 nField = pKeyInfo->nField;
2298 while( idx1<szHdr1 && i<pPKey2->nField ){
2299 u32 serial_type1;
2300
2301 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002302 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002303 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2304
2305 /* Extract the values to be compared.
2306 */
2307 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2308
2309 /* Do the comparison
2310 */
drhe14006d2008-03-25 17:23:32 +00002311 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002312 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002313 if( rc!=0 ){
2314 break;
2315 }
2316 i++;
2317 }
danielk19775f096132008-03-28 15:44:09 +00002318 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1);
drh1e968a02008-03-25 00:22:21 +00002319
2320 /* One of the keys ran out of fields, but all the fields up to that point
2321 ** were equal. If the incrKey flag is true, then the second key is
2322 ** treated as larger.
2323 */
2324 if( rc==0 ){
2325 if( pKeyInfo->incrKey ){
2326 rc = -1;
2327 }else if( !pKeyInfo->prefixIsEqual ){
2328 if( d1<nKey1 ){
2329 rc = 1;
2330 }
2331 }
2332 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2333 && pKeyInfo->aSortOrder[i] ){
2334 rc = -rc;
2335 }
2336
2337 return rc;
2338}
drhd5788202004-05-28 08:21:05 +00002339
2340/*
drh7a224de2004-06-02 01:22:02 +00002341** The argument is an index entry composed using the OP_MakeRecord opcode.
2342** The last entry in this record should be an integer (specifically
2343** an integer rowid). This routine returns the number of bytes in
2344** that integer.
drhd5788202004-05-28 08:21:05 +00002345*/
drh74161702006-02-24 02:53:49 +00002346int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002347 u32 szHdr; /* Size of the header */
2348 u32 typeRowid; /* Serial type of the rowid */
2349
shane3f8d5cf2008-04-24 19:15:09 +00002350 (void)getVarint32(aKey, szHdr);
2351 (void)getVarint32(&aKey[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002352 return sqlite3VdbeSerialTypeLen(typeRowid);
2353}
danielk1977eb015e02004-05-18 01:31:14 +00002354
2355
2356/*
drh7a224de2004-06-02 01:22:02 +00002357** pCur points at an index entry created using the OP_MakeRecord opcode.
2358** Read the rowid (the last field in the record) and store it in *rowid.
2359** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002360*/
drhb21c8cd2007-08-21 19:33:56 +00002361int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002362 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002363 int rc;
drhd5788202004-05-28 08:21:05 +00002364 u32 szHdr; /* Size of the header */
2365 u32 typeRowid; /* Serial type of the rowid */
2366 u32 lenRowid; /* Size of the rowid */
2367 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002368
drhd5788202004-05-28 08:21:05 +00002369 sqlite3BtreeKeySize(pCur, &nCellKey);
2370 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002371 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002372 }
danielk1977a7a8e142008-02-13 18:25:27 +00002373 m.flags = 0;
2374 m.db = 0;
danielk19775f096132008-03-28 15:44:09 +00002375 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002376 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002377 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002378 return rc;
2379 }
shane3f8d5cf2008-04-24 19:15:09 +00002380 (void)getVarint32((u8*)m.z, szHdr);
2381 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002382 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002383 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002384 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002385 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002386 return SQLITE_OK;
2387}
2388
drh7cf6e4d2004-05-19 14:56:55 +00002389/*
drhd3d39e92004-05-20 22:16:29 +00002390** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002391** the key string in pKey (of length nKey). Write into *pRes a number
2392** that is negative, zero, or positive if pC is less than, equal to,
2393** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002394**
drhd5788202004-05-28 08:21:05 +00002395** pKey is either created without a rowid or is truncated so that it
2396** omits the rowid at the end. The rowid at the end of the index entry
2397** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002398*/
danielk1977183f9f72004-05-13 05:20:26 +00002399int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002400 Cursor *pC, /* The cursor to compare against */
danielk1977751de562008-04-18 09:01:15 +00002401 UnpackedRecord *pUnpacked,
drh7cf6e4d2004-05-19 14:56:55 +00002402 int nKey, const u8 *pKey, /* The key to compare */
2403 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002404){
drh61fc5952007-04-01 23:49:51 +00002405 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002406 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002407 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002408 int lenRowid;
2409 Mem m;
drhe14006d2008-03-25 17:23:32 +00002410 UnpackedRecord *pRec;
drh1e968a02008-03-25 00:22:21 +00002411 char zSpace[200];
danielk1977183f9f72004-05-13 05:20:26 +00002412
2413 sqlite3BtreeKeySize(pCur, &nCellKey);
2414 if( nCellKey<=0 ){
2415 *res = 0;
2416 return SQLITE_OK;
2417 }
danielk1977a7a8e142008-02-13 18:25:27 +00002418 m.db = 0;
2419 m.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002420 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002421 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002422 if( rc ){
2423 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002424 }
drh74161702006-02-24 02:53:49 +00002425 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
danielk1977751de562008-04-18 09:01:15 +00002426 if( !pUnpacked ){
2427 pRec = sqlite3VdbeRecordUnpack(pC->pKeyInfo, nKey, pKey,
drh1e968a02008-03-25 00:22:21 +00002428 zSpace, sizeof(zSpace));
danielk1977751de562008-04-18 09:01:15 +00002429 }else{
2430 pRec = pUnpacked;
2431 }
drh1e968a02008-03-25 00:22:21 +00002432 if( pRec==0 ){
2433 return SQLITE_NOMEM;
2434 }
drhe14006d2008-03-25 17:23:32 +00002435 *res = sqlite3VdbeRecordCompare(m.n-lenRowid, m.z, pRec);
danielk1977751de562008-04-18 09:01:15 +00002436 if( !pUnpacked ){
2437 sqlite3VdbeDeleteUnpackedRecord(pRec);
2438 }
danielk1977d8123362004-06-12 09:25:12 +00002439 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002440 return SQLITE_OK;
2441}
danielk1977b28af712004-06-21 06:50:26 +00002442
2443/*
2444** This routine sets the value to be returned by subsequent calls to
2445** sqlite3_changes() on the database handle 'db'.
2446*/
2447void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002448 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002449 db->nChange = nChange;
2450 db->nTotalChange += nChange;
2451}
2452
2453/*
2454** Set a flag in the vdbe to update the change counter when it is finalised
2455** or reset.
2456*/
drh4794f732004-11-05 17:17:50 +00002457void sqlite3VdbeCountChanges(Vdbe *v){
2458 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002459}
drhd89bd002005-01-22 03:03:54 +00002460
2461/*
2462** Mark every prepared statement associated with a database connection
2463** as expired.
2464**
2465** An expired statement means that recompilation of the statement is
2466** recommend. Statements expire when things happen that make their
2467** programs obsolete. Removing user-defined functions or collating
2468** sequences, or changing an authorization function are the types of
2469** things that make prepared statements obsolete.
2470*/
2471void sqlite3ExpirePreparedStatements(sqlite3 *db){
2472 Vdbe *p;
2473 for(p = db->pVdbe; p; p=p->pNext){
2474 p->expired = 1;
2475 }
2476}
danielk1977aee18ef2005-03-09 12:26:50 +00002477
2478/*
2479** Return the database associated with the Vdbe.
2480*/
2481sqlite3 *sqlite3VdbeDb(Vdbe *v){
2482 return v->db;
2483}