blob: 79fe89f009e9d508e9e38c9d36304689975dca38 [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**
drha3460582008-07-11 21:02:53 +000017** $Id: vdbeaux.c,v 1.397 2008/07/11 21:02:54 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 *). */
shane1fc41292008-07-08 22:28:48 +0000538 pOp->p4.i = SQLITE_PTR_TO_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;
drhf089aa42008-07-08 19:34:06 +0000835 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
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->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001087 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001088 /* The pCx->pCursor will be close automatically, if it exists, by
1089 ** the call above. */
1090 }else if( pCx->pCursor ){
1091 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001092 }
drh9eff6162006-06-12 21:59:13 +00001093#ifndef SQLITE_OMIT_VIRTUALTABLE
1094 if( pCx->pVtabCursor ){
1095 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001096 const sqlite3_module *pModule = pCx->pModule;
1097 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001098 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001099 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001100 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001101 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001102 }
1103#endif
danielk19779882d992008-03-27 17:59:01 +00001104 if( !pCx->ephemPseudoTable ){
1105 sqlite3_free(pCx->pData);
1106 }
drh5c070532008-04-11 15:36:03 +00001107 /* memset(pCx, 0, sizeof(Cursor)); */
danielk1977cd3e8f72008-03-25 09:47:35 +00001108 /* sqlite3_free(pCx->aType); */
1109 /* sqlite3_free(pCx); */
drh9a324642003-09-06 20:12:01 +00001110}
1111
1112/*
drhff0587c2007-08-29 17:43:19 +00001113** Close all cursors except for VTab cursors that are currently
1114** in use.
drh9a324642003-09-06 20:12:01 +00001115*/
drhff0587c2007-08-29 17:43:19 +00001116static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001117 int i;
drh290c1942004-08-21 17:54:45 +00001118 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001119 for(i=0; i<p->nCursor; i++){
drhff0587c2007-08-29 17:43:19 +00001120 Cursor *pC = p->apCsr[i];
1121 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1122 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001123 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001124 }
drh9a324642003-09-06 20:12:01 +00001125 }
drh9a324642003-09-06 20:12:01 +00001126}
1127
1128/*
drh9a324642003-09-06 20:12:01 +00001129** Clean up the VM after execution.
1130**
1131** This routine will automatically close any cursors, lists, and/or
1132** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001133** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001134*/
danielk1977dfb316d2008-03-26 18:34:43 +00001135static void Cleanup(Vdbe *p, int freebuffers){
drh9a324642003-09-06 20:12:01 +00001136 int i;
drhff0587c2007-08-29 17:43:19 +00001137 closeAllCursorsExceptActiveVtabs(p);
danielk1977a7a8e142008-02-13 18:25:27 +00001138 for(i=1; i<=p->nMem; i++){
1139 MemSetTypeFlag(&p->aMem[i], MEM_Null);
1140 }
danielk1977dfb316d2008-03-26 18:34:43 +00001141 releaseMemArray(&p->aMem[1], p->nMem, freebuffers);
drha01f79d2005-07-08 13:07:59 +00001142 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001143 if( p->contextStack ){
1144 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001145 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001146 }
drh17435752007-08-16 04:30:38 +00001147 sqlite3_free(p->contextStack);
drh344737f2004-09-19 00:50:20 +00001148 }
drh5f968432004-02-21 19:02:30 +00001149 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001150 p->contextStackDepth = 0;
1151 p->contextStackTop = 0;
drh17435752007-08-16 04:30:38 +00001152 sqlite3_free(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001153 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001154 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001155}
1156
1157/*
danielk197722322fd2004-05-25 23:35:17 +00001158** Set the number of result columns that will be returned by this SQL
1159** statement. This is now set at compile time, rather than during
1160** execution of the vdbe program so that sqlite3_column_count() can
1161** be called on an SQL statement before sqlite3_step().
1162*/
1163void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001164 Mem *pColName;
1165 int n;
drh4a50aac2007-08-23 02:47:53 +00001166
danielk1977dfb316d2008-03-26 18:34:43 +00001167 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1);
drh17435752007-08-16 04:30:38 +00001168 sqlite3_free(p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001169 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001170 p->nResColumn = nResColumn;
danielk19771e536952007-08-16 10:09:01 +00001171 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001172 if( p->aColName==0 ) return;
1173 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001174 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001175 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001176 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001177 }
danielk197722322fd2004-05-25 23:35:17 +00001178}
1179
1180/*
danielk19773cf86062004-05-26 10:11:05 +00001181** Set the name of the idx'th column to be returned by the SQL statement.
1182** zName must be a pointer to a nul terminated string.
1183**
1184** This call must be made after a call to sqlite3VdbeSetNumCols().
1185**
drh66a51672008-01-03 00:01:23 +00001186** If N==P4_STATIC it means that zName is a pointer to a constant static
1187** string and we can just copy the pointer. If it is P4_DYNAMIC, then
drh17435752007-08-16 04:30:38 +00001188** the string is freed using sqlite3_free() when the vdbe is finished with
danielk1977d8123362004-06-12 09:25:12 +00001189** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001190*/
danielk1977955de522006-02-10 02:27:42 +00001191int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001192 int rc;
1193 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001194 assert( idx<p->nResColumn );
1195 assert( var<COLNAME_N );
drh17435752007-08-16 04:30:38 +00001196 if( p->db->mallocFailed ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001197 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001198 pColName = &(p->aColName[idx+var*p->nResColumn]);
drh66a51672008-01-03 00:01:23 +00001199 if( N==P4_DYNAMIC || N==P4_STATIC ){
drhb21c8cd2007-08-21 19:33:56 +00001200 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001201 }else{
drhb21c8cd2007-08-21 19:33:56 +00001202 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001203 }
drh66a51672008-01-03 00:01:23 +00001204 if( rc==SQLITE_OK && N==P4_DYNAMIC ){
danielk19775f096132008-03-28 15:44:09 +00001205 pColName->flags &= (~MEM_Static);
1206 pColName->zMalloc = pColName->z;
danielk19773cf86062004-05-26 10:11:05 +00001207 }
1208 return rc;
1209}
1210
danielk197713adf8a2004-06-03 16:08:41 +00001211/*
1212** A read or write transaction may or may not be active on database handle
1213** db. If a transaction is active, commit it. If there is a
1214** write-transaction spanning more than one database file, this routine
1215** takes care of the master journal trickery.
1216*/
drh9bb575f2004-09-06 17:24:11 +00001217static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001218 int i;
1219 int nTrans = 0; /* Number of databases with an active write-transaction */
1220 int rc = SQLITE_OK;
1221 int needXcommit = 0;
1222
danielk19775bd270b2006-07-25 15:14:52 +00001223 /* Before doing anything else, call the xSync() callback for any
1224 ** virtual module tables written in this transaction. This has to
1225 ** be done before determining whether a master journal file is
1226 ** required, as an xSync() callback may add an attached database
1227 ** to the transaction.
1228 */
1229 rc = sqlite3VtabSync(db, rc);
1230 if( rc!=SQLITE_OK ){
1231 return rc;
1232 }
1233
1234 /* This loop determines (a) if the commit hook should be invoked and
1235 ** (b) how many database files have open write transactions, not
1236 ** including the temp database. (b) is important because if more than
1237 ** one database file has an open write transaction, a master journal
1238 ** file is required for an atomic commit.
1239 */
danielk197713adf8a2004-06-03 16:08:41 +00001240 for(i=0; i<db->nDb; i++){
1241 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001242 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001243 needXcommit = 1;
1244 if( i!=1 ) nTrans++;
1245 }
1246 }
1247
1248 /* If there are any write-transactions at all, invoke the commit hook */
1249 if( needXcommit && db->xCommitCallback ){
drh7e8b8482008-01-23 03:03:05 +00001250 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001251 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001252 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001253 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001254 return SQLITE_CONSTRAINT;
1255 }
1256 }
1257
danielk197740b38dc2004-06-26 08:38:24 +00001258 /* The simple case - no more than one database file (not counting the
1259 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001260 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001261 **
danielk197740b38dc2004-06-26 08:38:24 +00001262 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001263 ** string, it means the main database is :memory: or a temp file. In
1264 ** that case we do not support atomic multi-file commits, so use the
1265 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001266 */
danielk197740b38dc2004-06-26 08:38:24 +00001267 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001268 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001269 Btree *pBt = db->aDb[i].pBt;
1270 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001271 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001272 }
1273 }
1274
drh80e35f42007-03-30 14:06:34 +00001275 /* Do the commit only if all databases successfully complete phase 1.
1276 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1277 ** IO error while deleting or truncating a journal file. It is unlikely,
1278 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001279 */
1280 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1281 Btree *pBt = db->aDb[i].pBt;
1282 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001283 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001284 }
danielk1977979f38e2007-03-27 16:19:51 +00001285 }
1286 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001287 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001288 }
1289 }
1290
1291 /* The complex case - There is a multi-file write-transaction active.
1292 ** This requires a master journal file to ensure the transaction is
1293 ** committed atomicly.
1294 */
danielk197744ee5bf2005-05-27 09:41:12 +00001295#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001296 else{
danielk1977b4b47412007-08-17 15:53:36 +00001297 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001298 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001299 char *zMaster = 0; /* File-name for the master journal */
1300 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001301 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001302 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001303 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001304
1305 /* Select a master journal file name */
1306 do {
drha6abd042004-06-09 17:37:22 +00001307 u32 random;
drh17435752007-08-16 04:30:38 +00001308 sqlite3_free(zMaster);
drh2fa18682008-03-19 14:15:34 +00001309 sqlite3_randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001310 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001311 if( !zMaster ){
1312 return SQLITE_NOMEM;
1313 }
danielk1977861f7452008-06-05 11:39:11 +00001314 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1315 }while( rc==SQLITE_OK && res );
1316 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001317 /* Open the master journal. */
1318 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1319 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1320 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1321 );
1322 }
danielk197713adf8a2004-06-03 16:08:41 +00001323 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001324 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001325 return rc;
1326 }
1327
1328 /* Write the name of each database file in the transaction into the new
1329 ** master journal file. If an error occurs at this point close
1330 ** and delete the master journal file. All the individual journal files
1331 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001332 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001333 */
danielk19771e536952007-08-16 10:09:01 +00001334 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001335 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001336 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001337 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001338 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001339 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001340 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1341 needSync = 1;
1342 }
danielk1977b4b47412007-08-17 15:53:36 +00001343 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001344 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001345 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001346 sqlite3OsCloseFree(pMaster);
1347 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001348 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001349 return rc;
1350 }
1351 }
1352 }
1353
danielk19779663b8f2007-08-24 11:52:28 +00001354 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1355 ** flag is set this is not required.
1356 */
danielk19775865e3d2004-06-14 06:03:57 +00001357 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001358 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001359 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001360 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001361 sqlite3OsCloseFree(pMaster);
1362 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001363 sqlite3_free(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001364 return rc;
1365 }
drhc9e06862004-06-09 20:03:08 +00001366
danielk197713adf8a2004-06-03 16:08:41 +00001367 /* Sync all the db files involved in the transaction. The same call
1368 ** sets the master journal pointer in each individual journal. If
1369 ** an error occurs here, do not delete the master journal file.
1370 **
drh80e35f42007-03-30 14:06:34 +00001371 ** If the error occurs during the first call to
1372 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1373 ** master journal file will be orphaned. But we cannot delete it,
1374 ** in case the master journal file name was written into the journal
1375 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001376 */
danielk19775bd270b2006-07-25 15:14:52 +00001377 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001378 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001379 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001380 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001381 }
1382 }
danielk1977fee2d252007-08-18 10:59:19 +00001383 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001384 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001385 sqlite3_free(zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001386 return rc;
1387 }
danielk197713adf8a2004-06-03 16:08:41 +00001388
danielk1977962398d2004-06-14 09:35:16 +00001389 /* Delete the master journal file. This commits the transaction. After
1390 ** doing this the directory is synced again before any individual
1391 ** transaction files are deleted.
1392 */
danielk1977fee2d252007-08-18 10:59:19 +00001393 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh17435752007-08-16 04:30:38 +00001394 sqlite3_free(zMaster);
drhc416ba92007-03-30 18:42:55 +00001395 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001396 if( rc ){
1397 return rc;
1398 }
danielk197713adf8a2004-06-03 16:08:41 +00001399
1400 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001401 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1402 ** deleting or truncating journals. If something goes wrong while
1403 ** this is happening we don't really care. The integrity of the
1404 ** transaction is already guaranteed, but some stray 'cold' journals
1405 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001406 */
danielk1977979f38e2007-03-27 16:19:51 +00001407 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001408 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001409 for(i=0; i<db->nDb; i++){
1410 Btree *pBt = db->aDb[i].pBt;
1411 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001412 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001413 }
1414 }
danielk19772d1d86f2008-06-20 14:59:51 +00001415 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001416 enable_simulated_io_errors();
1417
danielk1977f9e7dda2006-06-16 16:08:53 +00001418 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001419 }
danielk197744ee5bf2005-05-27 09:41:12 +00001420#endif
danielk1977026d2702004-06-14 13:14:59 +00001421
drh2ac3ee92004-06-07 16:27:46 +00001422 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001423}
1424
danielk19771d850a72004-05-31 08:26:49 +00001425/*
1426** This routine checks that the sqlite3.activeVdbeCnt count variable
1427** matches the number of vdbe's in the list sqlite3.pVdbe that are
1428** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001429** This is an internal self-check only - it is not an essential processing
1430** step.
danielk19771d850a72004-05-31 08:26:49 +00001431**
1432** This is a no-op if NDEBUG is defined.
1433*/
1434#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001435static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001436 Vdbe *p;
1437 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001438 p = db->pVdbe;
1439 while( p ){
drh92f02c32004-09-02 14:57:08 +00001440 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001441 cnt++;
1442 }
1443 p = p->pNext;
1444 }
danielk19771d850a72004-05-31 08:26:49 +00001445 assert( cnt==db->activeVdbeCnt );
1446}
1447#else
1448#define checkActiveVdbeCnt(x)
1449#endif
1450
danielk19773cf86062004-05-26 10:11:05 +00001451/*
drhfb982642007-08-30 01:19:59 +00001452** For every Btree that in database connection db which
1453** has been modified, "trip" or invalidate each cursor in
1454** that Btree might have been modified so that the cursor
1455** can never be used again. This happens when a rollback
1456*** occurs. We have to trip all the other cursors, even
1457** cursor from other VMs in different database connections,
1458** so that none of them try to use the data at which they
1459** were pointing and which now may have been changed due
1460** to the rollback.
1461**
1462** Remember that a rollback can delete tables complete and
1463** reorder rootpages. So it is not sufficient just to save
1464** the state of the cursor. We have to invalidate the cursor
1465** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001466*/
drhade6c9c2007-11-24 10:23:44 +00001467static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001468 int i;
1469 for(i=0; i<db->nDb; i++){
1470 Btree *p = db->aDb[i].pBt;
1471 if( p && sqlite3BtreeIsInTrans(p) ){
1472 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1473 }
danielk1977be718892006-06-23 08:05:19 +00001474 }
1475}
1476
1477/*
drh92f02c32004-09-02 14:57:08 +00001478** This routine is called the when a VDBE tries to halt. If the VDBE
1479** has made changes and is in autocommit mode, then commit those
1480** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001481**
drh92f02c32004-09-02 14:57:08 +00001482** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001483** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1484** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001485**
1486** Return an error code. If the commit could not complete because of
1487** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1488** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001489*/
drhff0587c2007-08-29 17:43:19 +00001490int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001491 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001492 int i;
danielk19771d850a72004-05-31 08:26:49 +00001493 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001494 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1495
1496 /* This function contains the logic that determines if a statement or
1497 ** transaction will be committed or rolled back as a result of the
1498 ** execution of this virtual machine.
1499 **
drh71b890a2007-10-03 15:30:52 +00001500 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001501 **
drh71b890a2007-10-03 15:30:52 +00001502 ** SQLITE_NOMEM
1503 ** SQLITE_IOERR
1504 ** SQLITE_FULL
1505 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001506 **
drh71b890a2007-10-03 15:30:52 +00001507 ** Then the internal cache might have been left in an inconsistent
1508 ** state. We need to rollback the statement transaction, if there is
1509 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001510 */
drh9a324642003-09-06 20:12:01 +00001511
drh17435752007-08-16 04:30:38 +00001512 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001513 p->rc = SQLITE_NOMEM;
1514 }
drhff0587c2007-08-29 17:43:19 +00001515 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001516 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001517 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001518 }
danielk19771d850a72004-05-31 08:26:49 +00001519 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001520
danielk197707cb5602006-01-20 10:55:05 +00001521 /* No commit or rollback needed if the program never started */
1522 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001523 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001524
1525 /* Lock all btrees used by the statement */
1526 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1527
drh71b890a2007-10-03 15:30:52 +00001528 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001529 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001530 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001531 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001532 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001533 /* This loop does static analysis of the query to see which of the
1534 ** following three categories it falls into:
1535 **
1536 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001537 ** Query with statement journal
1538 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001539 **
1540 ** We could do something more elegant than this static analysis (i.e.
1541 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001542 ** handling malloc() or IO failure is a fairly obscure edge case so
1543 ** this is probably easier. Todo: Might be an opportunity to reduce
1544 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001545 */
drhd1817042007-10-03 18:45:04 +00001546 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001547 int isStatement = 0;
1548 assert(p->aOp || p->nOp==0);
1549 for(i=0; i<p->nOp; i++){
1550 switch( p->aOp[i].opcode ){
1551 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001552 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001553 break;
1554 case OP_Statement:
1555 isStatement = 1;
1556 break;
1557 }
1558 }
drhff0587c2007-08-29 17:43:19 +00001559
1560
danielk197707cb5602006-01-20 10:55:05 +00001561 /* If the query was read-only, we need do no rollback at all. Otherwise,
1562 ** proceed with the special handling.
1563 */
drhd1817042007-10-03 18:45:04 +00001564 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001565 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1566 xFunc = sqlite3BtreeRollbackStmt;
1567 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001568 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001569 xFunc = sqlite3BtreeRollbackStmt;
1570 }else{
1571 /* We are forced to roll back the active transaction. Before doing
1572 ** so, abort any other statements this handle currently has active.
1573 */
drhfb982642007-08-30 01:19:59 +00001574 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001575 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001576 db->autoCommit = 1;
1577 }
danielk1977261919c2005-12-06 12:52:59 +00001578 }
1579 }
danielk197707cb5602006-01-20 10:55:05 +00001580
1581 /* If the auto-commit flag is set and this is the only active vdbe, then
1582 ** we do either a commit or rollback of the current transaction.
1583 **
1584 ** Note: This block also runs if one of the special errors handled
1585 ** above has occured.
1586 */
1587 if( db->autoCommit && db->activeVdbeCnt==1 ){
1588 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001589 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001590 ** successful or hit an 'OR FAIL' constraint. This means a commit
1591 ** is required.
1592 */
1593 int rc = vdbeCommit(db);
1594 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001595 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001596 return SQLITE_BUSY;
1597 }else if( rc!=SQLITE_OK ){
1598 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001599 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001600 }else{
1601 sqlite3CommitInternalChanges(db);
1602 }
1603 }else{
danielk197797a227c2006-01-20 16:32:04 +00001604 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001605 }
1606 }else if( !xFunc ){
1607 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001608 if( p->openedStatement ){
1609 xFunc = sqlite3BtreeCommitStmt;
1610 }
danielk197707cb5602006-01-20 10:55:05 +00001611 }else if( p->errorAction==OE_Abort ){
1612 xFunc = sqlite3BtreeRollbackStmt;
1613 }else{
drhfb982642007-08-30 01:19:59 +00001614 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001615 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001616 db->autoCommit = 1;
1617 }
danielk19771d850a72004-05-31 08:26:49 +00001618 }
danielk197707cb5602006-01-20 10:55:05 +00001619
1620 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1621 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1622 ** and the return code is still SQLITE_OK, set the return code to the new
1623 ** error value.
1624 */
1625 assert(!xFunc ||
1626 xFunc==sqlite3BtreeCommitStmt ||
1627 xFunc==sqlite3BtreeRollbackStmt
1628 );
1629 for(i=0; xFunc && i<db->nDb; i++){
1630 int rc;
1631 Btree *pBt = db->aDb[i].pBt;
1632 if( pBt ){
1633 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001634 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1635 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00001636 sqlite3_free(p->zErrMsg);
1637 p->zErrMsg = 0;
danielk19778a7aea32006-01-23 15:25:48 +00001638 }
danielk197707cb5602006-01-20 10:55:05 +00001639 }
danielk197777d83ba2004-05-31 10:08:14 +00001640 }
danielk197707cb5602006-01-20 10:55:05 +00001641
1642 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1643 ** set the change counter.
1644 */
1645 if( p->changeCntOn && p->pc>=0 ){
1646 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1647 sqlite3VdbeSetChanges(db, p->nChange);
1648 }else{
1649 sqlite3VdbeSetChanges(db, 0);
1650 }
1651 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001652 }
danielk197707cb5602006-01-20 10:55:05 +00001653
1654 /* Rollback or commit any schema changes that occurred. */
1655 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1656 sqlite3ResetInternalSchema(db, 0);
1657 db->flags = (db->flags | SQLITE_InternChanges);
1658 }
drhff0587c2007-08-29 17:43:19 +00001659
1660 /* Release the locks */
1661 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001662 }
danielk19771d850a72004-05-31 08:26:49 +00001663
danielk197765fd59f2006-06-24 11:51:33 +00001664 /* We have successfully halted and closed the VM. Record this fact. */
1665 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001666 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001667 }
drh92f02c32004-09-02 14:57:08 +00001668 p->magic = VDBE_MAGIC_HALT;
1669 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001670 if( p->db->mallocFailed ){
1671 p->rc = SQLITE_NOMEM;
1672 }
danielk19771d850a72004-05-31 08:26:49 +00001673
drh92f02c32004-09-02 14:57:08 +00001674 return SQLITE_OK;
1675}
drh4cf7c7f2007-08-28 23:28:07 +00001676
drh92f02c32004-09-02 14:57:08 +00001677
1678/*
drh3c23a882007-01-09 14:01:13 +00001679** Each VDBE holds the result of the most recent sqlite3_step() call
1680** in p->rc. This routine sets that result back to SQLITE_OK.
1681*/
1682void sqlite3VdbeResetStepResult(Vdbe *p){
1683 p->rc = SQLITE_OK;
1684}
1685
1686/*
drh92f02c32004-09-02 14:57:08 +00001687** Clean up a VDBE after execution but do not delete the VDBE just yet.
1688** Write any error messages into *pzErrMsg. Return the result code.
1689**
1690** After this routine is run, the VDBE should be ready to be executed
1691** again.
1692**
1693** To look at it another way, this routine resets the state of the
1694** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1695** VDBE_MAGIC_INIT.
1696*/
danielk1977dfb316d2008-03-26 18:34:43 +00001697int sqlite3VdbeReset(Vdbe *p, int freebuffers){
drh4ac285a2006-09-15 07:28:50 +00001698 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001699 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001700
1701 /* If the VM did not run to completion or if it encountered an
1702 ** error, then it might not have been halted properly. So halt
1703 ** it now.
1704 */
drh7e8b8482008-01-23 03:03:05 +00001705 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001706 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001707 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001708
drhfb7e7652005-01-24 00:28:42 +00001709 /* If the VDBE has be run even partially, then transfer the error code
1710 ** and error message from the VDBE into the main database structure. But
1711 ** if the VDBE has just been set to run but has not actually executed any
1712 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001713 */
drhfb7e7652005-01-24 00:28:42 +00001714 if( p->pc>=0 ){
1715 if( p->zErrMsg ){
drhb21c8cd2007-08-21 19:33:56 +00001716 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
danielk197797a227c2006-01-20 16:32:04 +00001717 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001718 p->zErrMsg = 0;
1719 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001720 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001721 }else{
drh4ac285a2006-09-15 07:28:50 +00001722 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001723 }
danielk1977a21c6b62005-01-24 10:25:59 +00001724 }else if( p->rc && p->expired ){
1725 /* The expired flag was set on the VDBE before the first call
1726 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1727 ** called), set the database error in this case as well.
1728 */
drh4ac285a2006-09-15 07:28:50 +00001729 sqlite3Error(db, p->rc, 0);
danielk19778e556522007-11-13 10:30:24 +00001730 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1731 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001732 }
1733
1734 /* Reclaim all memory used by the VDBE
1735 */
danielk1977dfb316d2008-03-26 18:34:43 +00001736 Cleanup(p, freebuffers);
drh92f02c32004-09-02 14:57:08 +00001737
1738 /* Save profiling information from this VDBE run.
1739 */
drh9a324642003-09-06 20:12:01 +00001740#ifdef VDBE_PROFILE
1741 {
1742 FILE *out = fopen("vdbe_profile.out", "a");
1743 if( out ){
1744 int i;
1745 fprintf(out, "---- ");
1746 for(i=0; i<p->nOp; i++){
1747 fprintf(out, "%02x", p->aOp[i].opcode);
1748 }
1749 fprintf(out, "\n");
1750 for(i=0; i<p->nOp; i++){
1751 fprintf(out, "%6d %10lld %8lld ",
1752 p->aOp[i].cnt,
1753 p->aOp[i].cycles,
1754 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1755 );
danielk19774adee202004-05-08 08:23:19 +00001756 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001757 }
1758 fclose(out);
1759 }
1760 }
1761#endif
1762 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001763 p->aborted = 0;
drh4ac285a2006-09-15 07:28:50 +00001764 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001765}
drh92f02c32004-09-02 14:57:08 +00001766
drh9a324642003-09-06 20:12:01 +00001767/*
1768** Clean up and delete a VDBE after execution. Return an integer which is
1769** the result code. Write any error message text into *pzErrMsg.
1770*/
danielk19779e6db7d2004-06-21 08:18:51 +00001771int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001772 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001773 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
danielk1977dfb316d2008-03-26 18:34:43 +00001774 rc = sqlite3VdbeReset(p, 1);
drh4ac285a2006-09-15 07:28:50 +00001775 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001776 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001777 return SQLITE_MISUSE;
1778 }
danielk1977dfb316d2008-03-26 18:34:43 +00001779 releaseMemArray(&p->aMem[1], p->nMem, 1);
danielk19774adee202004-05-08 08:23:19 +00001780 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001781 return rc;
1782}
1783
1784/*
drhf92c7ff2004-06-19 15:40:23 +00001785** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001786** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001787** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001788** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001789*/
1790void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1791 int i;
1792 for(i=0; i<pVdbeFunc->nAux; i++){
1793 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1794 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1795 if( pAux->xDelete ){
1796 pAux->xDelete(pAux->pAux);
1797 }
1798 pAux->pAux = 0;
1799 }
1800 }
1801}
1802
1803/*
drh9a324642003-09-06 20:12:01 +00001804** Delete an entire VDBE.
1805*/
danielk19774adee202004-05-08 08:23:19 +00001806void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001807 int i;
1808 if( p==0 ) return;
danielk1977dfb316d2008-03-26 18:34:43 +00001809 Cleanup(p, 1);
drh9a324642003-09-06 20:12:01 +00001810 if( p->pPrev ){
1811 p->pPrev->pNext = p->pNext;
1812 }else{
1813 assert( p->db->pVdbe==p );
1814 p->db->pVdbe = p->pNext;
1815 }
1816 if( p->pNext ){
1817 p->pNext->pPrev = p->pPrev;
1818 }
drh76ff3a02004-09-24 22:32:30 +00001819 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001820 Op *pOp = p->aOp;
1821 for(i=0; i<p->nOp; i++, pOp++){
drh66a51672008-01-03 00:01:23 +00001822 freeP4(pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001823#ifdef SQLITE_DEBUG
1824 sqlite3_free(pOp->zComment);
1825#endif
drh9a324642003-09-06 20:12:01 +00001826 }
drh17435752007-08-16 04:30:38 +00001827 sqlite3_free(p->aOp);
drh9a324642003-09-06 20:12:01 +00001828 }
danielk1977dfb316d2008-03-26 18:34:43 +00001829 releaseMemArray(p->aVar, p->nVar, 1);
drh17435752007-08-16 04:30:38 +00001830 sqlite3_free(p->aLabel);
drh9cbf3422008-01-17 16:22:13 +00001831 if( p->aMem ){
1832 sqlite3_free(&p->aMem[1]);
1833 }
danielk1977dfb316d2008-03-26 18:34:43 +00001834 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1);
drh17435752007-08-16 04:30:38 +00001835 sqlite3_free(p->aColName);
1836 sqlite3_free(p->zSql);
drh9a324642003-09-06 20:12:01 +00001837 p->magic = VDBE_MAGIC_DEAD;
drh17435752007-08-16 04:30:38 +00001838 sqlite3_free(p);
drh9a324642003-09-06 20:12:01 +00001839}
drha11846b2004-01-07 18:52:56 +00001840
1841/*
drha11846b2004-01-07 18:52:56 +00001842** If a MoveTo operation is pending on the given cursor, then do that
1843** MoveTo now. Return an error code. If no MoveTo is pending, this
1844** routine does nothing and returns SQLITE_OK.
1845*/
danielk19774adee202004-05-08 08:23:19 +00001846int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001847 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001848 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001849#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001850 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001851#endif
drhf0863fe2005-06-12 21:35:51 +00001852 assert( p->isTable );
drhe14006d2008-03-25 17:23:32 +00001853 rc = sqlite3BtreeMoveto(p->pCursor, 0, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001854 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001855 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001856 p->lastRowid = keyToInt(p->movetoTarget);
1857 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001858 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001859 rc = sqlite3BtreeNext(p->pCursor, &res);
1860 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001861 }
drh10cfdd52006-08-08 15:42:59 +00001862#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001863 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001864#endif
drha11846b2004-01-07 18:52:56 +00001865 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001866 p->cacheStatus = CACHE_STALE;
drha3460582008-07-11 21:02:53 +00001867 }else if( p->pCursor ){
1868 int hasMoved;
1869 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
1870 if( rc ) return rc;
1871 if( hasMoved ){
1872 p->cacheStatus = CACHE_STALE;
1873 p->nullRow = 1;
1874 }
drha11846b2004-01-07 18:52:56 +00001875 }
1876 return SQLITE_OK;
1877}
danielk19774adee202004-05-08 08:23:19 +00001878
drhab9f7f12004-05-08 10:56:11 +00001879/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001880** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001881**
danielk1977cfcdaef2004-05-12 07:33:33 +00001882** sqlite3VdbeSerialType()
1883** sqlite3VdbeSerialTypeLen()
1884** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001885** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001886** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001887**
1888** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001889** data and index records. Each serialized value consists of a
1890** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1891** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001892**
danielk1977cfcdaef2004-05-12 07:33:33 +00001893** In an SQLite index record, the serial type is stored directly before
1894** the blob of data that it corresponds to. In a table record, all serial
1895** types are stored at the start of the record, and the blobs of data at
1896** the end. Hence these functions allow the caller to handle the
1897** serial-type and data blob seperately.
1898**
1899** The following table describes the various storage classes for data:
1900**
1901** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001902** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001903** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001904** 1 1 signed integer
1905** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001906** 3 3 signed integer
1907** 4 4 signed integer
1908** 5 6 signed integer
1909** 6 8 signed integer
1910** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001911** 8 0 Integer constant 0
1912** 9 0 Integer constant 1
1913** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001914** N>=12 and even (N-12)/2 BLOB
1915** N>=13 and odd (N-13)/2 text
1916**
drh35a59652006-01-02 18:24:40 +00001917** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1918** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001919*/
1920
1921/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001922** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001923*/
drhd946db02005-12-29 19:23:06 +00001924u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001925 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001926 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001927
1928 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001929 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001930 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001931 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001932 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00001933# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001934 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001935 u64 u;
1936 if( file_format>=4 && (i&1)==i ){
1937 return 8+i;
1938 }
1939 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001940 if( u<=127 ) return 1;
1941 if( u<=32767 ) return 2;
1942 if( u<=8388607 ) return 3;
1943 if( u<=2147483647 ) return 4;
1944 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001945 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001946 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001947 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001948 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001949 }
drhfdf972a2007-05-02 13:30:27 +00001950 assert( flags&(MEM_Str|MEM_Blob) );
1951 n = pMem->n;
1952 if( flags & MEM_Zero ){
1953 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001954 }
drhfdf972a2007-05-02 13:30:27 +00001955 assert( n>=0 );
1956 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001957}
1958
1959/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001960** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001961*/
drh25aa1b42004-05-28 01:39:01 +00001962int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001963 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001964 return (serial_type-12)/2;
1965 }else{
drh57196282004-10-06 15:41:16 +00001966 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001967 return aSize[serial_type];
1968 }
danielk1977192ac1d2004-05-10 07:17:30 +00001969}
1970
1971/*
drh110daac2007-05-04 11:59:31 +00001972** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001973** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001974** upper 4 bytes. Return the result.
1975**
drh7a4f5022007-05-23 07:20:08 +00001976** For most architectures, this is a no-op.
1977**
1978** (later): It is reported to me that the mixed-endian problem
1979** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1980** that early versions of GCC stored the two words of a 64-bit
1981** float in the wrong order. And that error has been propagated
1982** ever since. The blame is not necessarily with GCC, though.
1983** GCC might have just copying the problem from a prior compiler.
1984** I am also told that newer versions of GCC that follow a different
1985** ABI get the byte order right.
1986**
1987** Developers using SQLite on an ARM7 should compile and run their
1988** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1989** enabled, some asserts below will ensure that the byte order of
1990** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00001991**
1992** (2007-08-30) Frank van Vugt has studied this problem closely
1993** and has send his findings to the SQLite developers. Frank
1994** writes that some Linux kernels offer floating point hardware
1995** emulation that uses only 32-bit mantissas instead of a full
1996** 48-bits as required by the IEEE standard. (This is the
1997** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1998** byte swapping becomes very complicated. To avoid problems,
1999** the necessary byte swapping is carried out using a 64-bit integer
2000** rather than a 64-bit float. Frank assures us that the code here
2001** works for him. We, the developers, have no way to independently
2002** verify this, but Frank seems to know what he is talking about
2003** so we trust him.
drh110daac2007-05-04 11:59:31 +00002004*/
2005#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002006static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002007 union {
drh60d09a72007-08-30 15:05:08 +00002008 u64 r;
drh110daac2007-05-04 11:59:31 +00002009 u32 i[2];
2010 } u;
2011 u32 t;
2012
2013 u.r = in;
2014 t = u.i[0];
2015 u.i[0] = u.i[1];
2016 u.i[1] = t;
2017 return u.r;
2018}
2019# define swapMixedEndianFloat(X) X = floatSwap(X)
2020#else
2021# define swapMixedEndianFloat(X)
2022#endif
2023
2024/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002025** Write the serialized data blob for the value stored in pMem into
2026** buf. It is assumed that the caller has allocated sufficient space.
2027** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002028**
2029** nBuf is the amount of space left in buf[]. nBuf must always be
2030** large enough to hold the entire field. Except, if the field is
2031** a blob with a zero-filled tail, then buf[] might be just the right
2032** size to hold everything except for the zero-filled tail. If buf[]
2033** is only big enough to hold the non-zero prefix, then only write that
2034** prefix into buf[]. But if buf[] is large enough to hold both the
2035** prefix and the tail then write the prefix and set the tail to all
2036** zeros.
2037**
2038** Return the number of bytes actually written into buf[]. The number
2039** of bytes in the zero-filled tail is included in the return value only
2040** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002041*/
drhfdf972a2007-05-02 13:30:27 +00002042int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002043 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00002044 int len;
danielk1977183f9f72004-05-13 05:20:26 +00002045
drh1483e142004-05-21 21:12:42 +00002046 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002047 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002048 u64 v;
2049 int i;
drha19b7752004-05-30 21:14:58 +00002050 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002051 assert( sizeof(v)==sizeof(pMem->r) );
2052 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002053 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002054 }else{
drh3c024d62007-03-30 11:23:45 +00002055 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002056 }
drh1483e142004-05-21 21:12:42 +00002057 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002058 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00002059 while( i-- ){
2060 buf[i] = (v&0xFF);
2061 v >>= 8;
2062 }
2063 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002064 }
drhd946db02005-12-29 19:23:06 +00002065
danielk1977cfcdaef2004-05-12 07:33:33 +00002066 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002067 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00002068 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
2069 == sqlite3VdbeSerialTypeLen(serial_type) );
2070 assert( pMem->n<=nBuf );
2071 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002072 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002073 if( pMem->flags & MEM_Zero ){
2074 len += pMem->u.i;
2075 if( len>nBuf ){
2076 len = nBuf;
2077 }
2078 memset(&buf[pMem->n], 0, len-pMem->n);
2079 }
drhd946db02005-12-29 19:23:06 +00002080 return len;
2081 }
2082
2083 /* NULL or constants 0 or 1 */
2084 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002085}
2086
2087/*
2088** Deserialize the data blob pointed to by buf as serial type serial_type
2089** and store the result in pMem. Return the number of bytes read.
2090*/
danielk1977b1bc9532004-05-22 03:05:33 +00002091int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002092 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002093 u32 serial_type, /* Serial type to deserialize */
2094 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002095){
drh3c685822005-05-21 18:32:18 +00002096 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002097 case 10: /* Reserved for future use */
2098 case 11: /* Reserved for future use */
2099 case 0: { /* NULL */
2100 pMem->flags = MEM_Null;
2101 break;
2102 }
2103 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002104 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002105 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002106 return 1;
drh1483e142004-05-21 21:12:42 +00002107 }
drh3c685822005-05-21 18:32:18 +00002108 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002109 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002110 pMem->flags = MEM_Int;
2111 return 2;
2112 }
2113 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002114 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002115 pMem->flags = MEM_Int;
2116 return 3;
2117 }
2118 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002119 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002120 pMem->flags = MEM_Int;
2121 return 4;
2122 }
2123 case 5: { /* 6-byte signed integer */
2124 u64 x = (((signed char)buf[0])<<8) | buf[1];
2125 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2126 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002127 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002128 pMem->flags = MEM_Int;
2129 return 6;
2130 }
drh91124b32005-08-18 18:15:05 +00002131 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002132 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002133 u64 x;
2134 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002135#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002136 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002137 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2138 ** defined that 64-bit floating point values really are mixed
2139 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002140 */
drhde941c62005-08-28 01:34:21 +00002141 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002142 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002143 u64 t2 = t1;
2144 swapMixedEndianFloat(t2);
2145 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002146#endif
drhbfd6b032005-08-28 01:38:44 +00002147
drhd81bd4e2005-09-05 20:06:49 +00002148 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2149 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002150 x = (x<<32) | y;
2151 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002152 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002153 pMem->flags = MEM_Int;
2154 }else{
drh4f0c5872007-03-26 22:05:01 +00002155 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002156 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002157 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002158 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002159 }
2160 return 8;
2161 }
drhd946db02005-12-29 19:23:06 +00002162 case 8: /* Integer 0 */
2163 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002164 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002165 pMem->flags = MEM_Int;
2166 return 0;
2167 }
drh3c685822005-05-21 18:32:18 +00002168 default: {
2169 int len = (serial_type-12)/2;
2170 pMem->z = (char *)buf;
2171 pMem->n = len;
2172 pMem->xDel = 0;
2173 if( serial_type&0x01 ){
2174 pMem->flags = MEM_Str | MEM_Ephem;
2175 }else{
2176 pMem->flags = MEM_Blob | MEM_Ephem;
2177 }
2178 return len;
drh696b32f2004-05-30 01:51:52 +00002179 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002180 }
drh3c685822005-05-21 18:32:18 +00002181 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002182}
2183
drh0e6082e2006-01-12 20:28:35 +00002184
drh1e968a02008-03-25 00:22:21 +00002185/*
2186** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002187** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002188** that structure.
2189**
2190** The calling function might provide szSpace bytes of memory
2191** space at pSpace. This space can be used to hold the returned
2192** VDbeParsedRecord structure if it is large enough. If it is
2193** not big enough, space is obtained from sqlite3_malloc().
2194**
2195** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002196** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002197*/
drhe14006d2008-03-25 17:23:32 +00002198UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002199 KeyInfo *pKeyInfo, /* Information about the record format */
2200 int nKey, /* Size of the binary record */
2201 const void *pKey, /* The binary record */
2202 void *pSpace, /* Space available to hold resulting object */
2203 int szSpace /* Size of pSpace[] in bytes */
2204){
2205 const unsigned char *aKey = (const unsigned char *)pKey;
drhe14006d2008-03-25 17:23:32 +00002206 UnpackedRecord *p;
drh1e968a02008-03-25 00:22:21 +00002207 int nByte;
2208 int i, idx, d;
2209 u32 szHdr;
2210 Mem *pMem;
2211
drhfab69592008-04-10 14:57:24 +00002212 assert( sizeof(Mem)>sizeof(*p) );
2213 nByte = sizeof(Mem)*(pKeyInfo->nField+2);
drh1e968a02008-03-25 00:22:21 +00002214 if( nByte>szSpace ){
2215 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2216 if( p==0 ) return 0;
2217 p->needFree = 1;
2218 }else{
2219 p = pSpace;
2220 p->needFree = 0;
2221 }
2222 p->pKeyInfo = pKeyInfo;
2223 p->nField = pKeyInfo->nField + 1;
2224 p->needDestroy = 1;
drhfab69592008-04-10 14:57:24 +00002225 p->aMem = pMem = &((Mem*)p)[1];
shane3f8d5cf2008-04-24 19:15:09 +00002226 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002227 d = szHdr;
2228 i = 0;
2229 while( idx<szHdr && i<p->nField ){
2230 u32 serial_type;
2231
shane3f8d5cf2008-04-24 19:15:09 +00002232 idx += getVarint32( aKey+idx, serial_type);
drh1e968a02008-03-25 00:22:21 +00002233 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
2234 pMem->enc = pKeyInfo->enc;
2235 pMem->db = pKeyInfo->db;
2236 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002237 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002238 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002239 pMem++;
2240 i++;
drh1e968a02008-03-25 00:22:21 +00002241 }
2242 p->nField = i;
2243 return (void*)p;
2244}
2245
2246/*
drhe14006d2008-03-25 17:23:32 +00002247** This routine destroys a UnpackedRecord object
drh1e968a02008-03-25 00:22:21 +00002248*/
drhe14006d2008-03-25 17:23:32 +00002249void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh1e968a02008-03-25 00:22:21 +00002250 if( p ){
2251 if( p->needDestroy ){
2252 int i;
drhe14006d2008-03-25 17:23:32 +00002253 Mem *pMem;
2254 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
danielk19775f096132008-03-28 15:44:09 +00002255 if( pMem->zMalloc ){
drhe14006d2008-03-25 17:23:32 +00002256 sqlite3VdbeMemRelease(pMem);
drh1e968a02008-03-25 00:22:21 +00002257 }
2258 }
2259 }
2260 if( p->needFree ){
2261 sqlite3_free(p);
2262 }
2263 }
2264}
2265
2266/*
2267** This function compares the two table rows or index records
2268** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
2269** or positive integer if {nKey1, pKey1} is less than, equal to or
2270** greater than pPKey2. The {nKey1, pKey1} key must be a blob
2271** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2272** key must be a parsed key such as obtained from
2273** sqlite3VdbeParseRecord.
2274**
2275** Key1 and Key2 do not have to contain the same number of fields.
2276** But if the lengths differ, Key2 must be the shorter of the two.
2277**
2278** Historical note: In earlier versions of this routine both Key1
2279** and Key2 were blobs obtained from OP_MakeRecord. But we found
2280** that in typical use the same Key2 would be submitted multiple times
2281** in a row. So an optimization was added to parse the Key2 key
2282** separately and submit the parsed version. In this way, we avoid
2283** parsing the same Key2 multiple times in a row.
2284*/
drhe14006d2008-03-25 17:23:32 +00002285int sqlite3VdbeRecordCompare(
drh1e968a02008-03-25 00:22:21 +00002286 int nKey1, const void *pKey1,
drhe14006d2008-03-25 17:23:32 +00002287 UnpackedRecord *pPKey2
drh1e968a02008-03-25 00:22:21 +00002288){
2289 u32 d1; /* Offset into aKey[] of next data element */
2290 u32 idx1; /* Offset into aKey[] of next header element */
2291 u32 szHdr1; /* Number of bytes in header */
2292 int i = 0;
2293 int nField;
2294 int rc = 0;
2295 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2296 KeyInfo *pKeyInfo;
2297 Mem mem1;
2298
2299 pKeyInfo = pPKey2->pKeyInfo;
2300 mem1.enc = pKeyInfo->enc;
2301 mem1.db = pKeyInfo->db;
2302 mem1.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002303 mem1.zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002304
shane3f8d5cf2008-04-24 19:15:09 +00002305 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002306 d1 = szHdr1;
2307 nField = pKeyInfo->nField;
2308 while( idx1<szHdr1 && i<pPKey2->nField ){
2309 u32 serial_type1;
2310
2311 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002312 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002313 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2314
2315 /* Extract the values to be compared.
2316 */
2317 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2318
2319 /* Do the comparison
2320 */
drhe14006d2008-03-25 17:23:32 +00002321 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002322 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002323 if( rc!=0 ){
2324 break;
2325 }
2326 i++;
2327 }
danielk19775f096132008-03-28 15:44:09 +00002328 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1);
drh1e968a02008-03-25 00:22:21 +00002329
2330 /* One of the keys ran out of fields, but all the fields up to that point
2331 ** were equal. If the incrKey flag is true, then the second key is
2332 ** treated as larger.
2333 */
2334 if( rc==0 ){
2335 if( pKeyInfo->incrKey ){
2336 rc = -1;
2337 }else if( !pKeyInfo->prefixIsEqual ){
2338 if( d1<nKey1 ){
2339 rc = 1;
2340 }
2341 }
2342 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2343 && pKeyInfo->aSortOrder[i] ){
2344 rc = -rc;
2345 }
2346
2347 return rc;
2348}
drhd5788202004-05-28 08:21:05 +00002349
2350/*
drh7a224de2004-06-02 01:22:02 +00002351** The argument is an index entry composed using the OP_MakeRecord opcode.
2352** The last entry in this record should be an integer (specifically
2353** an integer rowid). This routine returns the number of bytes in
2354** that integer.
drhd5788202004-05-28 08:21:05 +00002355*/
drh74161702006-02-24 02:53:49 +00002356int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002357 u32 szHdr; /* Size of the header */
2358 u32 typeRowid; /* Serial type of the rowid */
2359
shane3f8d5cf2008-04-24 19:15:09 +00002360 (void)getVarint32(aKey, szHdr);
2361 (void)getVarint32(&aKey[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002362 return sqlite3VdbeSerialTypeLen(typeRowid);
2363}
danielk1977eb015e02004-05-18 01:31:14 +00002364
2365
2366/*
drh7a224de2004-06-02 01:22:02 +00002367** pCur points at an index entry created using the OP_MakeRecord opcode.
2368** Read the rowid (the last field in the record) and store it in *rowid.
2369** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002370*/
drhb21c8cd2007-08-21 19:33:56 +00002371int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002372 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002373 int rc;
drhd5788202004-05-28 08:21:05 +00002374 u32 szHdr; /* Size of the header */
2375 u32 typeRowid; /* Serial type of the rowid */
2376 u32 lenRowid; /* Size of the rowid */
2377 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002378
drhd5788202004-05-28 08:21:05 +00002379 sqlite3BtreeKeySize(pCur, &nCellKey);
2380 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002381 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002382 }
danielk1977a7a8e142008-02-13 18:25:27 +00002383 m.flags = 0;
2384 m.db = 0;
danielk19775f096132008-03-28 15:44:09 +00002385 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002386 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002387 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002388 return rc;
2389 }
shane3f8d5cf2008-04-24 19:15:09 +00002390 (void)getVarint32((u8*)m.z, szHdr);
2391 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002392 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002393 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002394 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002395 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002396 return SQLITE_OK;
2397}
2398
drh7cf6e4d2004-05-19 14:56:55 +00002399/*
drhd3d39e92004-05-20 22:16:29 +00002400** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002401** the key string in pKey (of length nKey). Write into *pRes a number
2402** that is negative, zero, or positive if pC is less than, equal to,
2403** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002404**
drhd5788202004-05-28 08:21:05 +00002405** pKey is either created without a rowid or is truncated so that it
2406** omits the rowid at the end. The rowid at the end of the index entry
2407** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002408*/
danielk1977183f9f72004-05-13 05:20:26 +00002409int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002410 Cursor *pC, /* The cursor to compare against */
danielk1977751de562008-04-18 09:01:15 +00002411 UnpackedRecord *pUnpacked,
drh7cf6e4d2004-05-19 14:56:55 +00002412 int nKey, const u8 *pKey, /* The key to compare */
2413 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002414){
drh61fc5952007-04-01 23:49:51 +00002415 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002416 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002417 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002418 int lenRowid;
2419 Mem m;
drhe14006d2008-03-25 17:23:32 +00002420 UnpackedRecord *pRec;
drh1e968a02008-03-25 00:22:21 +00002421 char zSpace[200];
danielk1977183f9f72004-05-13 05:20:26 +00002422
2423 sqlite3BtreeKeySize(pCur, &nCellKey);
2424 if( nCellKey<=0 ){
2425 *res = 0;
2426 return SQLITE_OK;
2427 }
danielk1977a7a8e142008-02-13 18:25:27 +00002428 m.db = 0;
2429 m.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002430 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002431 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002432 if( rc ){
2433 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002434 }
drh74161702006-02-24 02:53:49 +00002435 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
danielk1977751de562008-04-18 09:01:15 +00002436 if( !pUnpacked ){
2437 pRec = sqlite3VdbeRecordUnpack(pC->pKeyInfo, nKey, pKey,
drh1e968a02008-03-25 00:22:21 +00002438 zSpace, sizeof(zSpace));
danielk1977751de562008-04-18 09:01:15 +00002439 }else{
2440 pRec = pUnpacked;
2441 }
drh1e968a02008-03-25 00:22:21 +00002442 if( pRec==0 ){
2443 return SQLITE_NOMEM;
2444 }
drhe14006d2008-03-25 17:23:32 +00002445 *res = sqlite3VdbeRecordCompare(m.n-lenRowid, m.z, pRec);
danielk1977751de562008-04-18 09:01:15 +00002446 if( !pUnpacked ){
2447 sqlite3VdbeDeleteUnpackedRecord(pRec);
2448 }
danielk1977d8123362004-06-12 09:25:12 +00002449 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002450 return SQLITE_OK;
2451}
danielk1977b28af712004-06-21 06:50:26 +00002452
2453/*
2454** This routine sets the value to be returned by subsequent calls to
2455** sqlite3_changes() on the database handle 'db'.
2456*/
2457void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002458 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002459 db->nChange = nChange;
2460 db->nTotalChange += nChange;
2461}
2462
2463/*
2464** Set a flag in the vdbe to update the change counter when it is finalised
2465** or reset.
2466*/
drh4794f732004-11-05 17:17:50 +00002467void sqlite3VdbeCountChanges(Vdbe *v){
2468 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002469}
drhd89bd002005-01-22 03:03:54 +00002470
2471/*
2472** Mark every prepared statement associated with a database connection
2473** as expired.
2474**
2475** An expired statement means that recompilation of the statement is
2476** recommend. Statements expire when things happen that make their
2477** programs obsolete. Removing user-defined functions or collating
2478** sequences, or changing an authorization function are the types of
2479** things that make prepared statements obsolete.
2480*/
2481void sqlite3ExpirePreparedStatements(sqlite3 *db){
2482 Vdbe *p;
2483 for(p = db->pVdbe; p; p=p->pNext){
2484 p->expired = 1;
2485 }
2486}
danielk1977aee18ef2005-03-09 12:26:50 +00002487
2488/*
2489** Return the database associated with the Vdbe.
2490*/
2491sqlite3 *sqlite3VdbeDb(Vdbe *v){
2492 return v->db;
2493}