blob: 3e8785fc3b1face097bd2a159608c383934870f0 [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**
drh6a1e0712008-12-05 15:24:15 +000017** $Id: vdbeaux.c,v 1.423 2008/12/05 15:24:17 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/*
danielk197700e13612008-11-17 19:18:54 +0000103** Resize the Vdbe.aOp array so that it is at least one op larger than
104** it was.
danielk1977ace3eb22006-01-26 10:35:04 +0000105**
danielk197700e13612008-11-17 19:18:54 +0000106** If an out-of-memory error occurs while resizing the array, return
107** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
108** unchanged (this is so that any opcodes already allocated can be
109** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000110*/
danielk197700e13612008-11-17 19:18:54 +0000111static int growOpArray(Vdbe *p){
drha4e5d582007-10-20 15:41:57 +0000112 VdbeOp *pNew;
danielk197700e13612008-11-17 19:18:54 +0000113 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
114 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000115 if( pNew ){
danielk197700e13612008-11-17 19:18:54 +0000116 p->nOpAlloc = nNew;
drha4e5d582007-10-20 15:41:57 +0000117 p->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000118 }
danielk197700e13612008-11-17 19:18:54 +0000119 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000120}
121
122/*
drh9a324642003-09-06 20:12:01 +0000123** Add a new instruction to the list of instructions current in the
124** VDBE. Return the address of the new instruction.
125**
126** Parameters:
127**
128** p Pointer to the VDBE
129**
130** op The opcode for this instruction
131**
drh66a51672008-01-03 00:01:23 +0000132** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000133**
danielk19774adee202004-05-08 08:23:19 +0000134** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000135** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000136** operand.
137*/
drh66a51672008-01-03 00:01:23 +0000138int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000139 int i;
drh701a0ae2004-02-22 20:05:00 +0000140 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000141
142 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000143 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000144 if( p->nOpAlloc<=i ){
danielk197700e13612008-11-17 19:18:54 +0000145 if( growOpArray(p) ){
drhfd2d26b2006-03-15 22:44:36 +0000146 return 0;
147 }
drh9a324642003-09-06 20:12:01 +0000148 }
danielk197701256832007-04-18 14:24:32 +0000149 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000150 pOp = &p->aOp[i];
151 pOp->opcode = op;
drh26c9b5e2008-04-11 14:56:53 +0000152 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000153 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000154 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000155 pOp->p3 = p3;
156 pOp->p4.p = 0;
157 pOp->p4type = P4_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000158 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000159#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000160 pOp->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000161 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000162#endif
drh26c9b5e2008-04-11 14:56:53 +0000163#ifdef VDBE_PROFILE
164 pOp->cycles = 0;
165 pOp->cnt = 0;
166#endif
drh9a324642003-09-06 20:12:01 +0000167 return i;
168}
drh66a51672008-01-03 00:01:23 +0000169int sqlite3VdbeAddOp0(Vdbe *p, int op){
170 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
171}
172int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
173 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
174}
175int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
176 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000177}
178
drh66a51672008-01-03 00:01:23 +0000179
drh701a0ae2004-02-22 20:05:00 +0000180/*
drh66a51672008-01-03 00:01:23 +0000181** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000182*/
drh66a51672008-01-03 00:01:23 +0000183int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000184 Vdbe *p, /* Add the opcode to this VM */
185 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000186 int p1, /* The P1 operand */
187 int p2, /* The P2 operand */
188 int p3, /* The P3 operand */
189 const char *zP4, /* The P4 operand */
190 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000191){
drh66a51672008-01-03 00:01:23 +0000192 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
193 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000194 return addr;
195}
196
197/*
drh9a324642003-09-06 20:12:01 +0000198** Create a new symbolic label for an instruction that has yet to be
199** coded. The symbolic label is really just a negative number. The
200** label can be used as the P2 value of an operation. Later, when
201** the label is resolved to a specific address, the VDBE will scan
202** through its operation list and change all values of P2 which match
203** the label into the resolved address.
204**
205** The VDBE knows that a P2 value is a label because labels are
206** always negative and P2 values are suppose to be non-negative.
207** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000208**
209** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000210*/
danielk19774adee202004-05-08 08:23:19 +0000211int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000212 int i;
213 i = p->nLabel++;
214 assert( p->magic==VDBE_MAGIC_INIT );
215 if( i>=p->nLabelAlloc ){
drh6a1e0712008-12-05 15:24:15 +0000216 int n = p->nLabelAlloc*2 + 5;
danielk19771e536952007-08-16 10:09:01 +0000217 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drh6a1e0712008-12-05 15:24:15 +0000218 n*sizeof(p->aLabel[0]));
219 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
drh9a324642003-09-06 20:12:01 +0000220 }
drh76ff3a02004-09-24 22:32:30 +0000221 if( p->aLabel ){
222 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000223 }
drh9a324642003-09-06 20:12:01 +0000224 return -1-i;
225}
226
227/*
228** Resolve label "x" to be the address of the next instruction to
229** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000230** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000231*/
danielk19774adee202004-05-08 08:23:19 +0000232void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000233 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000234 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000235 assert( j>=0 && j<p->nLabel );
236 if( p->aLabel ){
237 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000238 }
239}
240
241/*
drh9cbf3422008-01-17 16:22:13 +0000242** Loop through the program looking for P2 values that are negative
243** on jump instructions. Each such value is a label. Resolve the
244** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000245**
246** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000247**
drh13449892005-09-07 21:22:45 +0000248** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000249** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000250** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000251**
drh38449902005-06-07 01:43:41 +0000252** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000253** instructions that might cause a statement rollback. Such instructions
254** are:
255**
256** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
257** * OP_Destroy
258** * OP_VUpdate
259** * OP_VRename
260**
261** If no such instruction is found, then every Statement instruction
262** is changed to a Noop. In this way, we avoid creating the statement
263** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000264*/
drh9cbf3422008-01-17 16:22:13 +0000265static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000266 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000267 int nMaxArgs = 0;
drh76ff3a02004-09-24 22:32:30 +0000268 Op *pOp;
269 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000270 int doesStatementRollback = 0;
271 int hasStatementBegin = 0;
drhad4a4b82008-11-05 16:37:34 +0000272 p->readOnly = 1;
273 p->usesStmtJournal = 0;
drh76ff3a02004-09-24 22:32:30 +0000274 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000275 u8 opcode = pOp->opcode;
276
drha2baf3a2008-06-18 15:34:09 +0000277 if( opcode==OP_Function || opcode==OP_AggStep ){
drh98757152008-01-09 23:04:12 +0000278 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
danielk1977399918f2006-06-14 13:03:23 +0000279#ifndef SQLITE_OMIT_VIRTUALTABLE
drha2baf3a2008-06-18 15:34:09 +0000280 }else if( opcode==OP_VUpdate ){
danielk1977bc04f852005-03-29 08:26:13 +0000281 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drha2baf3a2008-06-18 15:34:09 +0000282#endif
danielk19775dfecba2008-06-23 13:57:21 +0000283 }
danielk1977182c4ba2007-06-27 15:53:34 +0000284 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000285 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
286 doesStatementRollback = 1;
287 }
drh38449902005-06-07 01:43:41 +0000288 }else if( opcode==OP_Statement ){
289 hasStatementBegin = 1;
drhad4a4b82008-11-05 16:37:34 +0000290 p->usesStmtJournal = 1;
drh77658e22007-12-04 16:54:52 +0000291 }else if( opcode==OP_Destroy ){
292 doesStatementRollback = 1;
drhad4a4b82008-11-05 16:37:34 +0000293 }else if( opcode==OP_Transaction && pOp->p2!=0 ){
294 p->readOnly = 0;
danielk1977182c4ba2007-06-27 15:53:34 +0000295#ifndef SQLITE_OMIT_VIRTUALTABLE
296 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
297 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000298 }else if( opcode==OP_VFilter ){
299 int n;
300 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000301 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000302 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000303 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000304#endif
danielk1977bc04f852005-03-29 08:26:13 +0000305 }
danielk1977634f2982005-03-28 08:44:07 +0000306
drhd2981512008-01-04 19:33:49 +0000307 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
308 assert( -1-pOp->p2<p->nLabel );
309 pOp->p2 = aLabel[-1-pOp->p2];
310 }
drh76ff3a02004-09-24 22:32:30 +0000311 }
drh633e6d52008-07-28 19:34:53 +0000312 sqlite3DbFree(p->db, p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000313 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000314
315 *pMaxFuncArgs = nMaxArgs;
drh38449902005-06-07 01:43:41 +0000316
317 /* If we never rollback a statement transaction, then statement
318 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000319 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000320 ** which can be expensive on some platforms.
321 */
322 if( hasStatementBegin && !doesStatementRollback ){
drhad4a4b82008-11-05 16:37:34 +0000323 p->usesStmtJournal = 0;
drh38449902005-06-07 01:43:41 +0000324 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
325 if( pOp->opcode==OP_Statement ){
326 pOp->opcode = OP_Noop;
327 }
328 }
329 }
drh76ff3a02004-09-24 22:32:30 +0000330}
331
332/*
drh9a324642003-09-06 20:12:01 +0000333** Return the address of the next instruction to be inserted.
334*/
danielk19774adee202004-05-08 08:23:19 +0000335int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000336 assert( p->magic==VDBE_MAGIC_INIT );
337 return p->nOp;
338}
339
340/*
341** Add a whole list of operations to the operation stack. Return the
342** address of the first operation added.
343*/
danielk19774adee202004-05-08 08:23:19 +0000344int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000345 int addr;
346 assert( p->magic==VDBE_MAGIC_INIT );
danielk197700e13612008-11-17 19:18:54 +0000347 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000348 return 0;
drh9a324642003-09-06 20:12:01 +0000349 }
350 addr = p->nOp;
351 if( nOp>0 ){
352 int i;
drh905793e2004-02-21 13:31:09 +0000353 VdbeOpList const *pIn = aOp;
354 for(i=0; i<nOp; i++, pIn++){
355 int p2 = pIn->p2;
356 VdbeOp *pOut = &p->aOp[i+addr];
357 pOut->opcode = pIn->opcode;
358 pOut->p1 = pIn->p1;
drh8558cde2008-01-05 05:20:10 +0000359 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
360 pOut->p2 = addr + ADDR(p2);
361 }else{
362 pOut->p2 = p2;
363 }
drh24003452008-01-03 01:28:59 +0000364 pOut->p3 = pIn->p3;
365 pOut->p4type = P4_NOTUSED;
366 pOut->p4.p = 0;
367 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000368#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000369 pOut->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000370 if( sqlite3VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000371 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000372 }
373#endif
374 }
375 p->nOp += nOp;
376 }
377 return addr;
378}
379
380/*
381** Change the value of the P1 operand for a specific instruction.
382** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000383** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000384** few minor changes to the program.
385*/
danielk19774adee202004-05-08 08:23:19 +0000386void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000387 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000388 if( p && addr>=0 && p->nOp>addr && p->aOp ){
389 p->aOp[addr].p1 = val;
390 }
391}
392
393/*
394** Change the value of the P2 operand for a specific instruction.
395** This routine is useful for setting a jump destination.
396*/
danielk19774adee202004-05-08 08:23:19 +0000397void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000398 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000399 if( p && addr>=0 && p->nOp>addr && p->aOp ){
400 p->aOp[addr].p2 = val;
401 }
402}
403
drhd654be82005-09-20 17:42:23 +0000404/*
danielk19771f4aa332008-01-03 09:51:55 +0000405** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000406*/
407void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
408 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
409 if( p && addr>=0 && p->nOp>addr && p->aOp ){
410 p->aOp[addr].p3 = val;
411 }
412}
413
414/*
drh35573352008-01-08 23:54:25 +0000415** Change the value of the P5 operand for the most recently
416** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000417*/
drh35573352008-01-08 23:54:25 +0000418void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
danielk19771f4aa332008-01-03 09:51:55 +0000419 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh35573352008-01-08 23:54:25 +0000420 if( p && p->aOp ){
421 assert( p->nOp>0 );
422 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000423 }
424}
425
426/*
drhf8875402006-03-17 13:56:34 +0000427** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000428** the address of the next instruction to be coded.
429*/
430void sqlite3VdbeJumpHere(Vdbe *p, int addr){
431 sqlite3VdbeChangeP2(p, addr, p->nOp);
432}
drhb38ad992005-09-16 00:27:01 +0000433
drhb7f6f682006-07-08 17:06:43 +0000434
435/*
436** If the input FuncDef structure is ephemeral, then free it. If
437** the FuncDef is not ephermal, then do nothing.
438*/
drh633e6d52008-07-28 19:34:53 +0000439static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhb7f6f682006-07-08 17:06:43 +0000440 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000441 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000442 }
443}
444
drhb38ad992005-09-16 00:27:01 +0000445/*
drh66a51672008-01-03 00:01:23 +0000446** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000447*/
drh633e6d52008-07-28 19:34:53 +0000448static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000449 if( p4 ){
drh66a51672008-01-03 00:01:23 +0000450 switch( p4type ){
451 case P4_REAL:
452 case P4_INT64:
453 case P4_MPRINTF:
454 case P4_DYNAMIC:
455 case P4_KEYINFO:
drh0acb7e42008-06-25 00:12:41 +0000456 case P4_INTARRAY:
drh66a51672008-01-03 00:01:23 +0000457 case P4_KEYINFO_HANDOFF: {
drh633e6d52008-07-28 19:34:53 +0000458 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000459 break;
460 }
drh66a51672008-01-03 00:01:23 +0000461 case P4_VDBEFUNC: {
drh0acb7e42008-06-25 00:12:41 +0000462 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
drh633e6d52008-07-28 19:34:53 +0000463 freeEphemeralFunction(db, pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000464 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh633e6d52008-07-28 19:34:53 +0000465 sqlite3DbFree(db, pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000466 break;
467 }
drh66a51672008-01-03 00:01:23 +0000468 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000469 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000470 break;
471 }
drh66a51672008-01-03 00:01:23 +0000472 case P4_MEM: {
drh0acb7e42008-06-25 00:12:41 +0000473 sqlite3ValueFree((sqlite3_value*)p4);
drhac1733d2005-09-17 17:58:22 +0000474 break;
475 }
drhb38ad992005-09-16 00:27:01 +0000476 }
477 }
478}
479
480
drh9a324642003-09-06 20:12:01 +0000481/*
drhf8875402006-03-17 13:56:34 +0000482** Change N opcodes starting at addr to No-ops.
483*/
484void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000485 if( p && p->aOp ){
486 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000487 sqlite3 *db = p->db;
danielk197792d4d7a2007-05-04 12:05:56 +0000488 while( N-- ){
drh633e6d52008-07-28 19:34:53 +0000489 freeP4(db, pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000490 memset(pOp, 0, sizeof(pOp[0]));
491 pOp->opcode = OP_Noop;
492 pOp++;
493 }
drhf8875402006-03-17 13:56:34 +0000494 }
495}
496
497/*
drh66a51672008-01-03 00:01:23 +0000498** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000499** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000500** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000501** few minor changes to the program.
502**
drh66a51672008-01-03 00:01:23 +0000503** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000504** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000505** A value of n==0 means copy bytes of zP4 up to and including the
506** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000507**
drh66a51672008-01-03 00:01:23 +0000508** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000509** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000510** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000511** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000512** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000513** caller should not free the allocation, it will be freed when the Vdbe is
514** finalized.
515**
drh66a51672008-01-03 00:01:23 +0000516** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000517** to a string or structure that is guaranteed to exist for the lifetime of
518** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000519**
drh66a51672008-01-03 00:01:23 +0000520** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000521*/
drh66a51672008-01-03 00:01:23 +0000522void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000523 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000524 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000525 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000526 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000527 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000528 if( p->aOp==0 || db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000529 if (n != P4_KEYINFO) {
drh633e6d52008-07-28 19:34:53 +0000530 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000531 }
danielk1977d5d56522005-03-16 12:15:20 +0000532 return;
533 }
drh91fd4d42008-01-19 20:11:25 +0000534 assert( addr<p->nOp );
535 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000536 addr = p->nOp - 1;
537 if( addr<0 ) return;
538 }
539 pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000540 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000541 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000542 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000543 /* Note: this cast is safe, because the origin data point was an int
544 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000545 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh98757152008-01-09 23:04:12 +0000546 pOp->p4type = n;
547 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000548 pOp->p4.p = 0;
549 pOp->p4type = P4_NOTUSED;
550 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000551 KeyInfo *pKeyInfo;
552 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000553
drh66a51672008-01-03 00:01:23 +0000554 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000555 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhe5ae5732008-06-15 02:51:47 +0000556 pKeyInfo = sqlite3Malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000557 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000558 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000559 u8 *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000560 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000561 aSortOrder = pKeyInfo->aSortOrder;
562 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000563 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000564 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
565 }
drh66a51672008-01-03 00:01:23 +0000566 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000567 }else{
drh17435752007-08-16 04:30:38 +0000568 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000569 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000570 }
drh66a51672008-01-03 00:01:23 +0000571 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000572 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000573 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000574 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000575 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000576 pOp->p4type = n;
drh9a324642003-09-06 20:12:01 +0000577 }else{
drh66a51672008-01-03 00:01:23 +0000578 if( n==0 ) n = strlen(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000579 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000580 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000581 }
582}
583
drhad6d9462004-09-19 02:15:24 +0000584#ifndef NDEBUG
585/*
drh16ee60f2008-06-20 18:13:25 +0000586** Change the comment on the the most recently coded instruction. Or
587** insert a No-op and add the comment to that new instruction. This
588** makes the code easier to read during debugging. None of this happens
589** in a production build.
drhad6d9462004-09-19 02:15:24 +0000590*/
591void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
592 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000593 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000594 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000595 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000596 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000597 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000598 sqlite3DbFree(p->db, *pz);
drh8cc74322008-01-15 02:22:24 +0000599 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000600 va_end(ap);
601 }
drhad6d9462004-09-19 02:15:24 +0000602}
drh16ee60f2008-06-20 18:13:25 +0000603void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
604 va_list ap;
605 sqlite3VdbeAddOp0(p, OP_Noop);
606 assert( p->nOp>0 || p->aOp==0 );
607 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
608 if( p->nOp ){
609 char **pz = &p->aOp[p->nOp-1].zComment;
610 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000611 sqlite3DbFree(p->db, *pz);
drh16ee60f2008-06-20 18:13:25 +0000612 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
613 va_end(ap);
614 }
615}
616#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000617
drh9a324642003-09-06 20:12:01 +0000618/*
drh9a324642003-09-06 20:12:01 +0000619** Return the opcode for a given address.
620*/
danielk19774adee202004-05-08 08:23:19 +0000621VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000622 assert( p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000623 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
danielk197701256832007-04-18 14:24:32 +0000624 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000625}
626
drhb7f91642004-10-31 02:22:47 +0000627#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
628 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000629/*
drh66a51672008-01-03 00:01:23 +0000630** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000631** Use zTemp for any required temporary buffer space.
632*/
drh66a51672008-01-03 00:01:23 +0000633static char *displayP4(Op *pOp, char *zTemp, int nTemp){
634 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000635 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000636 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000637 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000638 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000639 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000640 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000641 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhd3d39e92004-05-20 22:16:29 +0000642 i = strlen(zTemp);
643 for(j=0; j<pKeyInfo->nField; j++){
644 CollSeq *pColl = pKeyInfo->aColl[j];
645 if( pColl ){
646 int n = strlen(pColl->zName);
647 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000648 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000649 break;
650 }
651 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000652 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000653 zTemp[i++] = '-';
654 }
drh5bb3eb92007-05-04 13:15:55 +0000655 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000656 i += n;
657 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000658 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000659 i += 4;
660 }
661 }
662 zTemp[i++] = ')';
663 zTemp[i] = 0;
664 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000665 break;
666 }
drh66a51672008-01-03 00:01:23 +0000667 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000668 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000669 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000670 break;
671 }
drh66a51672008-01-03 00:01:23 +0000672 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000673 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000674 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000675 break;
676 }
drh66a51672008-01-03 00:01:23 +0000677 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000678 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000679 break;
680 }
drh66a51672008-01-03 00:01:23 +0000681 case P4_INT32: {
682 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000683 break;
684 }
drh66a51672008-01-03 00:01:23 +0000685 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000686 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000687 break;
688 }
drh66a51672008-01-03 00:01:23 +0000689 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000690 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000691 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000692 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000693 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000694 }else if( pMem->flags & MEM_Int ){
695 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
696 }else if( pMem->flags & MEM_Real ){
697 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhd4e70eb2008-01-02 00:34:36 +0000698 }
drh598f1342007-10-23 15:39:45 +0000699 break;
700 }
drha967e882006-06-13 01:04:52 +0000701#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000702 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000703 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000704 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000705 break;
706 }
707#endif
drh0acb7e42008-06-25 00:12:41 +0000708 case P4_INTARRAY: {
709 sqlite3_snprintf(nTemp, zTemp, "intarray");
710 break;
711 }
drhd3d39e92004-05-20 22:16:29 +0000712 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000713 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000714 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000715 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000716 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000717 }
718 }
719 }
drh66a51672008-01-03 00:01:23 +0000720 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000721 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000722}
drhb7f91642004-10-31 02:22:47 +0000723#endif
drhd3d39e92004-05-20 22:16:29 +0000724
drh900b31e2007-08-28 02:27:51 +0000725/*
drhd0679ed2007-08-28 22:24:34 +0000726** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
727**
drh900b31e2007-08-28 02:27:51 +0000728*/
drhfb982642007-08-30 01:19:59 +0000729void sqlite3VdbeUsesBtree(Vdbe *p, int i){
730 int mask;
drhd0679ed2007-08-28 22:24:34 +0000731 assert( i>=0 && i<p->db->nDb );
danielk197700e13612008-11-17 19:18:54 +0000732 assert( i<(int)sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000733 mask = 1<<i;
734 if( (p->btreeMask & mask)==0 ){
735 p->btreeMask |= mask;
736 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
737 }
drh900b31e2007-08-28 02:27:51 +0000738}
739
drhd3d39e92004-05-20 22:16:29 +0000740
danielk19778b60e0f2005-01-12 09:10:39 +0000741#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000742/*
743** Print a single opcode. This routine is used for debugging only.
744*/
danielk19774adee202004-05-08 08:23:19 +0000745void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000746 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000747 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000748 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000749 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000750 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000751 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000752 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
753#ifdef SQLITE_DEBUG
754 pOp->zComment ? pOp->zComment : ""
755#else
756 ""
757#endif
758 );
drh9a324642003-09-06 20:12:01 +0000759 fflush(pOut);
760}
761#endif
762
763/*
drh76ff3a02004-09-24 22:32:30 +0000764** Release an array of N Mem elements
765*/
drhc890fec2008-08-01 20:10:08 +0000766static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +0000767 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +0000768 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +0000769 sqlite3 *db = p->db;
770 int malloc_failed = db->mallocFailed;
danielk1977e972e032008-09-19 18:32:26 +0000771 for(pEnd=&p[N]; p<pEnd; p++){
772 assert( (&p[1])==pEnd || p[0].db==p[1].db );
773
774 /* This block is really an inlined version of sqlite3VdbeMemRelease()
775 ** that takes advantage of the fact that the memory cell value is
776 ** being set to NULL after releasing any dynamic resources.
777 **
778 ** The justification for duplicating code is that according to
779 ** callgrind, this causes a certain test case to hit the CPU 4.7
780 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
781 ** sqlite3MemRelease() were called from here. With -O2, this jumps
782 ** to 6.6 percent. The test case is inserting 1000 rows into a table
783 ** with no indexes using a single prepared INSERT statement, bind()
784 ** and reset(). Inserts are grouped into a transaction.
785 */
786 if( p->flags&(MEM_Agg|MEM_Dyn) ){
787 sqlite3VdbeMemRelease(p);
788 }else if( p->zMalloc ){
789 sqlite3DbFree(db, p->zMalloc);
790 p->zMalloc = 0;
791 }
792
danielk19775f096132008-03-28 15:44:09 +0000793 p->flags = MEM_Null;
drh76ff3a02004-09-24 22:32:30 +0000794 }
danielk1977a7a8e142008-02-13 18:25:27 +0000795 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +0000796 }
797}
798
danielk1977dfb316d2008-03-26 18:34:43 +0000799#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
800int sqlite3VdbeReleaseBuffers(Vdbe *p){
801 int ii;
802 int nFree = 0;
803 assert( sqlite3_mutex_held(p->db->mutex) );
804 for(ii=1; ii<=p->nMem; ii++){
805 Mem *pMem = &p->aMem[ii];
drh3d4501e2008-12-04 20:40:10 +0000806 if( pMem->flags & MEM_RowSet ){
807 sqlite3RowSetClear(pMem->u.pRowSet);
808 }
danielk1977dfb316d2008-03-26 18:34:43 +0000809 if( pMem->z && pMem->flags&MEM_Dyn ){
810 assert( !pMem->xDel );
drh633e6d52008-07-28 19:34:53 +0000811 nFree += sqlite3DbMallocSize(pMem->db, pMem->z);
danielk1977dfb316d2008-03-26 18:34:43 +0000812 sqlite3VdbeMemRelease(pMem);
813 }
814 }
815 return nFree;
816}
817#endif
818
drhb7f91642004-10-31 02:22:47 +0000819#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000820/*
drh9a324642003-09-06 20:12:01 +0000821** Give a listing of the program in the virtual machine.
822**
danielk19774adee202004-05-08 08:23:19 +0000823** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000824** running the code, it invokes the callback once for each instruction.
825** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +0000826**
827** When p->explain==1, each instruction is listed. When
828** p->explain==2, only OP_Explain instructions are listed and these
829** are shown in a different format. p->explain==2 is used to implement
830** EXPLAIN QUERY PLAN.
drh9a324642003-09-06 20:12:01 +0000831*/
danielk19774adee202004-05-08 08:23:19 +0000832int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000833 Vdbe *p /* The VDBE */
834){
drh9bb575f2004-09-06 17:24:11 +0000835 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000836 int i;
drh826fb5a2004-02-14 23:59:57 +0000837 int rc = SQLITE_OK;
drh9cbf3422008-01-17 16:22:13 +0000838 Mem *pMem = p->pResultSet = &p->aMem[1];
drh9a324642003-09-06 20:12:01 +0000839
drh9a324642003-09-06 20:12:01 +0000840 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000841 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
842 assert( db->magic==SQLITE_MAGIC_BUSY );
danielk19776c359f02008-11-21 16:58:03 +0000843 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +0000844
drh9cbf3422008-01-17 16:22:13 +0000845 /* Even though this opcode does not use dynamic strings for
846 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000847 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000848 */
drhc890fec2008-08-01 20:10:08 +0000849 releaseMemArray(pMem, p->nMem);
danielk197718f41892004-05-22 07:27:46 +0000850
danielk19776c359f02008-11-21 16:58:03 +0000851 if( p->rc==SQLITE_NOMEM ){
852 /* This happens if a malloc() inside a call to sqlite3_column_text() or
853 ** sqlite3_column_text16() failed. */
854 db->mallocFailed = 1;
855 return SQLITE_ERROR;
856 }
857
drhecc92422005-09-10 16:46:12 +0000858 do{
859 i = p->pc++;
860 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000861 if( i>=p->nOp ){
862 p->rc = SQLITE_OK;
863 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000864 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000865 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000866 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +0000867 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +0000868 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000869 char *z;
drhd3d39e92004-05-20 22:16:29 +0000870 Op *pOp = &p->aOp[i];
danielk19770d78bae2008-01-03 07:09:48 +0000871 if( p->explain==1 ){
872 pMem->flags = MEM_Int;
873 pMem->type = SQLITE_INTEGER;
874 pMem->u.i = i; /* Program counter */
875 pMem++;
876
877 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
878 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
879 assert( pMem->z!=0 );
880 pMem->n = strlen(pMem->z);
881 pMem->type = SQLITE_TEXT;
882 pMem->enc = SQLITE_UTF8;
883 pMem++;
884 }
drheb2e1762004-05-27 01:53:56 +0000885
886 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000887 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000888 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000889 pMem++;
890
891 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000892 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000893 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000894 pMem++;
895
danielk19770d78bae2008-01-03 07:09:48 +0000896 if( p->explain==1 ){
897 pMem->flags = MEM_Int;
898 pMem->u.i = pOp->p3; /* P3 */
899 pMem->type = SQLITE_INTEGER;
900 pMem++;
901 }
902
danielk1977a7a8e142008-02-13 18:25:27 +0000903 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
904 p->db->mallocFailed = 1;
905 return SQLITE_NOMEM;
906 }
907 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
908 z = displayP4(pOp, pMem->z, 32);
909 if( z!=pMem->z ){
910 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
911 }else{
912 assert( pMem->z!=0 );
913 pMem->n = strlen(pMem->z);
914 pMem->enc = SQLITE_UTF8;
915 }
drh9c054832004-05-31 18:51:57 +0000916 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +0000917 pMem++;
drheb2e1762004-05-27 01:53:56 +0000918
danielk19770d78bae2008-01-03 07:09:48 +0000919 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +0000920 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +0000921 p->db->mallocFailed = 1;
922 return SQLITE_NOMEM;
923 }
924 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +0000925 pMem->n = 2;
926 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +0000927 pMem->type = SQLITE_TEXT;
928 pMem->enc = SQLITE_UTF8;
929 pMem++;
930
drhaa9b8962008-01-08 02:57:55 +0000931#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000932 if( pOp->zComment ){
933 pMem->flags = MEM_Str|MEM_Term;
934 pMem->z = pOp->zComment;
935 pMem->n = strlen(pMem->z);
936 pMem->enc = SQLITE_UTF8;
danielk19771e522b42008-09-16 09:09:19 +0000937 pMem->type = SQLITE_TEXT;
drh52391cb2008-02-14 23:44:13 +0000938 }else
drhaa9b8962008-01-08 02:57:55 +0000939#endif
drh52391cb2008-02-14 23:44:13 +0000940 {
941 pMem->flags = MEM_Null; /* Comment */
942 pMem->type = SQLITE_NULL;
943 }
danielk19770d78bae2008-01-03 07:09:48 +0000944 }
945
946 p->nResColumn = 8 - 5*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +0000947 p->rc = SQLITE_OK;
948 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000949 }
drh826fb5a2004-02-14 23:59:57 +0000950 return rc;
drh9a324642003-09-06 20:12:01 +0000951}
drhb7f91642004-10-31 02:22:47 +0000952#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000953
drh7c4ac0c2007-04-05 11:25:58 +0000954#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000955/*
drh3f7d4e42004-07-24 14:35:58 +0000956** Print the SQL that was used to generate a VDBE program.
957*/
958void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000959 int nOp = p->nOp;
960 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000961 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000962 pOp = &p->aOp[0];
963 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000964 const char *z = pOp->p4.z;
drh4c755c02004-08-08 20:22:17 +0000965 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000966 printf("SQL: [%s]\n", z);
967 }
drh3f7d4e42004-07-24 14:35:58 +0000968}
drh7c4ac0c2007-04-05 11:25:58 +0000969#endif
drh3f7d4e42004-07-24 14:35:58 +0000970
drh602c2372007-03-01 00:29:13 +0000971#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
972/*
973** Print an IOTRACE message showing SQL content.
974*/
975void sqlite3VdbeIOTraceSql(Vdbe *p){
976 int nOp = p->nOp;
977 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +0000978 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +0000979 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000980 pOp = &p->aOp[0];
981 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +0000982 int i, j;
drh00a18e42007-08-13 11:10:34 +0000983 char z[1000];
drh949f9cd2008-01-12 21:35:57 +0000984 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk19772be2be92007-05-16 17:50:45 +0000985 for(i=0; isspace((unsigned char)z[i]); i++){}
drh602c2372007-03-01 00:29:13 +0000986 for(j=0; z[i]; i++){
danielk19772be2be92007-05-16 17:50:45 +0000987 if( isspace((unsigned char)z[i]) ){
drh602c2372007-03-01 00:29:13 +0000988 if( z[i-1]!=' ' ){
989 z[j++] = ' ';
990 }
991 }else{
992 z[j++] = z[i];
993 }
994 }
995 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000996 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +0000997 }
998}
999#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1000
1001
drh3f7d4e42004-07-24 14:35:58 +00001002/*
drh9a324642003-09-06 20:12:01 +00001003** Prepare a virtual machine for execution. This involves things such
1004** as allocating stack space and initializing the program counter.
1005** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +00001006** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +00001007**
1008** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
1009** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +00001010*/
danielk19774adee202004-05-08 08:23:19 +00001011void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +00001012 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +00001013 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +00001014 int nMem, /* Number of memory cells to allocate */
1015 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +00001016 int isExplain /* True if the EXPLAIN keywords is present */
1017){
1018 int n;
danielk19771e536952007-08-16 10:09:01 +00001019 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001020
1021 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001022 assert( p->magic==VDBE_MAGIC_INIT );
1023
drhc16a03b2004-09-15 13:38:10 +00001024 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001025 */
drhc16a03b2004-09-15 13:38:10 +00001026 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001027
danielk197700e13612008-11-17 19:18:54 +00001028 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001029 p->magic = VDBE_MAGIC_RUN;
1030
danielk1977cd3e8f72008-03-25 09:47:35 +00001031 /* For each cursor required, also allocate a memory cell. Memory
1032 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1033 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001034 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001035 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1036 ** stores the blob of memory associated with cursor 1, etc.
1037 **
1038 ** See also: allocateCursor().
1039 */
1040 nMem += nCursor;
1041
drh9cbf3422008-01-17 16:22:13 +00001042 /*
1043 ** Allocation space for registers.
drh9a324642003-09-06 20:12:01 +00001044 */
drh9cbf3422008-01-17 16:22:13 +00001045 if( p->aMem==0 ){
danielk1977634f2982005-03-28 08:44:07 +00001046 int nArg; /* Maximum number of args passed to a user function. */
drh9cbf3422008-01-17 16:22:13 +00001047 resolveP2Values(p, &nArg);
drh82a48512003-09-06 22:45:20 +00001048 assert( nVar>=0 );
drh9cbf3422008-01-17 16:22:13 +00001049 if( isExplain && nMem<10 ){
drhc46f5202008-11-04 14:25:06 +00001050 nMem = 10;
drh0f7eb612006-08-08 13:51:43 +00001051 }
drh9cbf3422008-01-17 16:22:13 +00001052 p->aMem = sqlite3DbMallocZero(db,
1053 nMem*sizeof(Mem) /* aMem */
drh86f43302004-10-05 17:37:36 +00001054 + nVar*sizeof(Mem) /* aVar */
drh9cbf3422008-01-17 16:22:13 +00001055 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +00001056 + nVar*sizeof(char*) /* azVar */
drhdfe88ec2008-11-03 20:55:06 +00001057 + nCursor*sizeof(VdbeCursor*)+1 /* apCsr */
drh82a48512003-09-06 22:45:20 +00001058 );
drh17435752007-08-16 04:30:38 +00001059 if( !db->mallocFailed ){
drh9cbf3422008-01-17 16:22:13 +00001060 p->aMem--; /* aMem[] goes from 1..nMem */
1061 p->nMem = nMem; /* not from 0..nMem-1 */
drh0a07c102008-01-03 18:03:08 +00001062 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +00001063 p->nVar = nVar;
1064 p->okVar = 0;
1065 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +00001066 p->azVar = (char**)&p->apArg[nArg];
drhdfe88ec2008-11-03 20:55:06 +00001067 p->apCsr = (VdbeCursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +00001068 p->nCursor = nCursor;
1069 for(n=0; n<nVar; n++){
1070 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001071 p->aVar[n].db = db;
1072 }
drh9cbf3422008-01-17 16:22:13 +00001073 for(n=1; n<=nMem; n++){
1074 p->aMem[n].flags = MEM_Null;
1075 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +00001076 }
danielk197754db47e2004-05-19 10:36:43 +00001077 }
drh82a48512003-09-06 22:45:20 +00001078 }
drh9cbf3422008-01-17 16:22:13 +00001079#ifdef SQLITE_DEBUG
1080 for(n=1; n<p->nMem; n++){
1081 assert( p->aMem[n].db==db );
danielk1977b3bce662005-01-29 08:32:43 +00001082 }
drh9cbf3422008-01-17 16:22:13 +00001083#endif
drh9a324642003-09-06 20:12:01 +00001084
danielk19771d850a72004-05-31 08:26:49 +00001085 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001086 p->rc = SQLITE_OK;
1087 p->uniqueCnt = 0;
drh9a324642003-09-06 20:12:01 +00001088 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001089 p->explain |= isExplain;
1090 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001091 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001092 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001093 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +00001094 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +00001095#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001096 {
1097 int i;
1098 for(i=0; i<p->nOp; i++){
1099 p->aOp[i].cnt = 0;
1100 p->aOp[i].cycles = 0;
1101 }
drh9a324642003-09-06 20:12:01 +00001102 }
1103#endif
1104}
1105
drh9a324642003-09-06 20:12:01 +00001106/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001107** Close a VDBE cursor and release all the resources that cursor
1108** happens to hold.
drh9a324642003-09-06 20:12:01 +00001109*/
drhdfe88ec2008-11-03 20:55:06 +00001110void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001111 if( pCx==0 ){
1112 return;
1113 }
drh9a324642003-09-06 20:12:01 +00001114 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001115 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001116 /* The pCx->pCursor will be close automatically, if it exists, by
1117 ** the call above. */
1118 }else if( pCx->pCursor ){
1119 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001120 }
drh9eff6162006-06-12 21:59:13 +00001121#ifndef SQLITE_OMIT_VIRTUALTABLE
1122 if( pCx->pVtabCursor ){
1123 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001124 const sqlite3_module *pModule = pCx->pModule;
1125 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001126 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001127 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001128 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001129 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001130 }
1131#endif
danielk19779882d992008-03-27 17:59:01 +00001132 if( !pCx->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00001133 sqlite3DbFree(p->db, pCx->pData);
danielk19779882d992008-03-27 17:59:01 +00001134 }
drh9a324642003-09-06 20:12:01 +00001135}
1136
1137/*
drhff0587c2007-08-29 17:43:19 +00001138** Close all cursors except for VTab cursors that are currently
1139** in use.
drh9a324642003-09-06 20:12:01 +00001140*/
drhff0587c2007-08-29 17:43:19 +00001141static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001142 int i;
drh290c1942004-08-21 17:54:45 +00001143 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001144 for(i=0; i<p->nCursor; i++){
drhdfe88ec2008-11-03 20:55:06 +00001145 VdbeCursor *pC = p->apCsr[i];
drhff0587c2007-08-29 17:43:19 +00001146 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1147 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001148 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001149 }
drh9a324642003-09-06 20:12:01 +00001150 }
drh9a324642003-09-06 20:12:01 +00001151}
1152
1153/*
drh9a324642003-09-06 20:12:01 +00001154** Clean up the VM after execution.
1155**
1156** This routine will automatically close any cursors, lists, and/or
1157** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001158** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001159*/
drhc890fec2008-08-01 20:10:08 +00001160static void Cleanup(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001161 int i;
drh633e6d52008-07-28 19:34:53 +00001162 sqlite3 *db = p->db;
drh3d4501e2008-12-04 20:40:10 +00001163 Mem *pMem;
drhff0587c2007-08-29 17:43:19 +00001164 closeAllCursorsExceptActiveVtabs(p);
drh3d4501e2008-12-04 20:40:10 +00001165 for(pMem=&p->aMem[1], i=1; i<=p->nMem; i++, pMem++){
1166 if( pMem->flags & MEM_RowSet ){
1167 sqlite3RowSetClear(pMem->u.pRowSet);
1168 }
1169 MemSetTypeFlag(pMem, MEM_Null);
danielk1977a7a8e142008-02-13 18:25:27 +00001170 }
drhc890fec2008-08-01 20:10:08 +00001171 releaseMemArray(&p->aMem[1], p->nMem);
drh76ff3a02004-09-24 22:32:30 +00001172 if( p->contextStack ){
drh633e6d52008-07-28 19:34:53 +00001173 sqlite3DbFree(db, p->contextStack);
drh344737f2004-09-19 00:50:20 +00001174 }
drh5f968432004-02-21 19:02:30 +00001175 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001176 p->contextStackDepth = 0;
1177 p->contextStackTop = 0;
drh633e6d52008-07-28 19:34:53 +00001178 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001179 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001180 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001181}
1182
1183/*
danielk197722322fd2004-05-25 23:35:17 +00001184** Set the number of result columns that will be returned by this SQL
1185** statement. This is now set at compile time, rather than during
1186** execution of the vdbe program so that sqlite3_column_count() can
1187** be called on an SQL statement before sqlite3_step().
1188*/
1189void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001190 Mem *pColName;
1191 int n;
drh633e6d52008-07-28 19:34:53 +00001192 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001193
drhc890fec2008-08-01 20:10:08 +00001194 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001195 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001196 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001197 p->nResColumn = nResColumn;
drh633e6d52008-07-28 19:34:53 +00001198 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001199 if( p->aColName==0 ) return;
1200 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001201 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001202 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001203 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001204 }
danielk197722322fd2004-05-25 23:35:17 +00001205}
1206
1207/*
danielk19773cf86062004-05-26 10:11:05 +00001208** Set the name of the idx'th column to be returned by the SQL statement.
1209** zName must be a pointer to a nul terminated string.
1210**
1211** This call must be made after a call to sqlite3VdbeSetNumCols().
1212**
danielk197710fb7492008-10-31 10:53:22 +00001213** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1214** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1215** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001216*/
danielk197710fb7492008-10-31 10:53:22 +00001217int sqlite3VdbeSetColName(
1218 Vdbe *p, /* Vdbe being configured */
1219 int idx, /* Index of column zName applies to */
1220 int var, /* One of the COLNAME_* constants */
1221 const char *zName, /* Pointer to buffer containing name */
1222 void (*xDel)(void*) /* Memory management strategy for zName */
1223){
danielk19773cf86062004-05-26 10:11:05 +00001224 int rc;
1225 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001226 assert( idx<p->nResColumn );
1227 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001228 if( p->db->mallocFailed ){
1229 assert( !zName || xDel!=SQLITE_DYNAMIC );
1230 return SQLITE_NOMEM;
1231 }
drh76ff3a02004-09-24 22:32:30 +00001232 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001233 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001234 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001235 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001236 return rc;
1237}
1238
danielk197713adf8a2004-06-03 16:08:41 +00001239/*
1240** A read or write transaction may or may not be active on database handle
1241** db. If a transaction is active, commit it. If there is a
1242** write-transaction spanning more than one database file, this routine
1243** takes care of the master journal trickery.
1244*/
danielk19773e3a84d2008-08-01 17:37:40 +00001245static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001246 int i;
1247 int nTrans = 0; /* Number of databases with an active write-transaction */
1248 int rc = SQLITE_OK;
1249 int needXcommit = 0;
1250
danielk19775bd270b2006-07-25 15:14:52 +00001251 /* Before doing anything else, call the xSync() callback for any
1252 ** virtual module tables written in this transaction. This has to
1253 ** be done before determining whether a master journal file is
1254 ** required, as an xSync() callback may add an attached database
1255 ** to the transaction.
1256 */
danielk19773e3a84d2008-08-01 17:37:40 +00001257 rc = sqlite3VtabSync(db, &p->zErrMsg);
danielk19775bd270b2006-07-25 15:14:52 +00001258 if( rc!=SQLITE_OK ){
1259 return rc;
1260 }
1261
1262 /* This loop determines (a) if the commit hook should be invoked and
1263 ** (b) how many database files have open write transactions, not
1264 ** including the temp database. (b) is important because if more than
1265 ** one database file has an open write transaction, a master journal
1266 ** file is required for an atomic commit.
1267 */
danielk197713adf8a2004-06-03 16:08:41 +00001268 for(i=0; i<db->nDb; i++){
1269 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001270 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001271 needXcommit = 1;
1272 if( i!=1 ) nTrans++;
1273 }
1274 }
1275
1276 /* If there are any write-transactions at all, invoke the commit hook */
1277 if( needXcommit && db->xCommitCallback ){
drh7e8b8482008-01-23 03:03:05 +00001278 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001279 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001280 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001281 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001282 return SQLITE_CONSTRAINT;
1283 }
1284 }
1285
danielk197740b38dc2004-06-26 08:38:24 +00001286 /* The simple case - no more than one database file (not counting the
1287 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001288 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001289 **
danielk197740b38dc2004-06-26 08:38:24 +00001290 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001291 ** string, it means the main database is :memory: or a temp file. In
1292 ** that case we do not support atomic multi-file commits, so use the
1293 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001294 */
danielk197740b38dc2004-06-26 08:38:24 +00001295 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001296 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001297 Btree *pBt = db->aDb[i].pBt;
1298 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001299 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001300 }
1301 }
1302
drh80e35f42007-03-30 14:06:34 +00001303 /* Do the commit only if all databases successfully complete phase 1.
1304 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1305 ** IO error while deleting or truncating a journal file. It is unlikely,
1306 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001307 */
1308 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1309 Btree *pBt = db->aDb[i].pBt;
1310 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001311 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001312 }
danielk1977979f38e2007-03-27 16:19:51 +00001313 }
1314 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001315 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001316 }
1317 }
1318
1319 /* The complex case - There is a multi-file write-transaction active.
1320 ** This requires a master journal file to ensure the transaction is
1321 ** committed atomicly.
1322 */
danielk197744ee5bf2005-05-27 09:41:12 +00001323#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001324 else{
danielk1977b4b47412007-08-17 15:53:36 +00001325 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001326 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001327 char *zMaster = 0; /* File-name for the master journal */
1328 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001329 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001330 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001331 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001332
1333 /* Select a master journal file name */
1334 do {
drha6abd042004-06-09 17:37:22 +00001335 u32 random;
drh633e6d52008-07-28 19:34:53 +00001336 sqlite3DbFree(db, zMaster);
drh2fa18682008-03-19 14:15:34 +00001337 sqlite3_randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001338 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001339 if( !zMaster ){
1340 return SQLITE_NOMEM;
1341 }
danielk1977861f7452008-06-05 11:39:11 +00001342 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1343 }while( rc==SQLITE_OK && res );
1344 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001345 /* Open the master journal. */
1346 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1347 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1348 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1349 );
1350 }
danielk197713adf8a2004-06-03 16:08:41 +00001351 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001352 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001353 return rc;
1354 }
1355
1356 /* Write the name of each database file in the transaction into the new
1357 ** master journal file. If an error occurs at this point close
1358 ** and delete the master journal file. All the individual journal files
1359 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001360 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001361 */
danielk19771e536952007-08-16 10:09:01 +00001362 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001363 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001364 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001365 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001366 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001367 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001368 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1369 needSync = 1;
1370 }
danielk1977b4b47412007-08-17 15:53:36 +00001371 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001372 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001373 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001374 sqlite3OsCloseFree(pMaster);
1375 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001376 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001377 return rc;
1378 }
1379 }
1380 }
1381
danielk19779663b8f2007-08-24 11:52:28 +00001382 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1383 ** flag is set this is not required.
1384 */
danielk19775865e3d2004-06-14 06:03:57 +00001385 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001386 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001387 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001388 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001389 sqlite3OsCloseFree(pMaster);
1390 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001391 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001392 return rc;
1393 }
drhc9e06862004-06-09 20:03:08 +00001394
danielk197713adf8a2004-06-03 16:08:41 +00001395 /* Sync all the db files involved in the transaction. The same call
1396 ** sets the master journal pointer in each individual journal. If
1397 ** an error occurs here, do not delete the master journal file.
1398 **
drh80e35f42007-03-30 14:06:34 +00001399 ** If the error occurs during the first call to
1400 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1401 ** master journal file will be orphaned. But we cannot delete it,
1402 ** in case the master journal file name was written into the journal
1403 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001404 */
danielk19775bd270b2006-07-25 15:14:52 +00001405 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001406 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001407 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001408 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001409 }
1410 }
danielk1977fee2d252007-08-18 10:59:19 +00001411 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001412 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001413 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001414 return rc;
1415 }
danielk197713adf8a2004-06-03 16:08:41 +00001416
danielk1977962398d2004-06-14 09:35:16 +00001417 /* Delete the master journal file. This commits the transaction. After
1418 ** doing this the directory is synced again before any individual
1419 ** transaction files are deleted.
1420 */
danielk1977fee2d252007-08-18 10:59:19 +00001421 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00001422 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00001423 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001424 if( rc ){
1425 return rc;
1426 }
danielk197713adf8a2004-06-03 16:08:41 +00001427
1428 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001429 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1430 ** deleting or truncating journals. If something goes wrong while
1431 ** this is happening we don't really care. The integrity of the
1432 ** transaction is already guaranteed, but some stray 'cold' journals
1433 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001434 */
danielk1977979f38e2007-03-27 16:19:51 +00001435 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001436 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001437 for(i=0; i<db->nDb; i++){
1438 Btree *pBt = db->aDb[i].pBt;
1439 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001440 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001441 }
1442 }
danielk19772d1d86f2008-06-20 14:59:51 +00001443 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001444 enable_simulated_io_errors();
1445
danielk1977f9e7dda2006-06-16 16:08:53 +00001446 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001447 }
danielk197744ee5bf2005-05-27 09:41:12 +00001448#endif
danielk1977026d2702004-06-14 13:14:59 +00001449
drh2ac3ee92004-06-07 16:27:46 +00001450 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001451}
1452
danielk19771d850a72004-05-31 08:26:49 +00001453/*
1454** This routine checks that the sqlite3.activeVdbeCnt count variable
1455** matches the number of vdbe's in the list sqlite3.pVdbe that are
1456** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001457** This is an internal self-check only - it is not an essential processing
1458** step.
danielk19771d850a72004-05-31 08:26:49 +00001459**
1460** This is a no-op if NDEBUG is defined.
1461*/
1462#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001463static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001464 Vdbe *p;
1465 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00001466 int nWrite = 0;
danielk19771d850a72004-05-31 08:26:49 +00001467 p = db->pVdbe;
1468 while( p ){
drh92f02c32004-09-02 14:57:08 +00001469 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001470 cnt++;
drhad4a4b82008-11-05 16:37:34 +00001471 if( p->readOnly==0 ) nWrite++;
danielk19771d850a72004-05-31 08:26:49 +00001472 }
1473 p = p->pNext;
1474 }
danielk19771d850a72004-05-31 08:26:49 +00001475 assert( cnt==db->activeVdbeCnt );
drhad4a4b82008-11-05 16:37:34 +00001476 assert( nWrite==db->writeVdbeCnt );
danielk19771d850a72004-05-31 08:26:49 +00001477}
1478#else
1479#define checkActiveVdbeCnt(x)
1480#endif
1481
danielk19773cf86062004-05-26 10:11:05 +00001482/*
drhfb982642007-08-30 01:19:59 +00001483** For every Btree that in database connection db which
1484** has been modified, "trip" or invalidate each cursor in
1485** that Btree might have been modified so that the cursor
1486** can never be used again. This happens when a rollback
1487*** occurs. We have to trip all the other cursors, even
1488** cursor from other VMs in different database connections,
1489** so that none of them try to use the data at which they
1490** were pointing and which now may have been changed due
1491** to the rollback.
1492**
1493** Remember that a rollback can delete tables complete and
1494** reorder rootpages. So it is not sufficient just to save
1495** the state of the cursor. We have to invalidate the cursor
1496** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001497*/
drhade6c9c2007-11-24 10:23:44 +00001498static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001499 int i;
1500 for(i=0; i<db->nDb; i++){
1501 Btree *p = db->aDb[i].pBt;
1502 if( p && sqlite3BtreeIsInTrans(p) ){
1503 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1504 }
danielk1977be718892006-06-23 08:05:19 +00001505 }
1506}
1507
1508/*
drh92f02c32004-09-02 14:57:08 +00001509** This routine is called the when a VDBE tries to halt. If the VDBE
1510** has made changes and is in autocommit mode, then commit those
1511** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001512**
drh92f02c32004-09-02 14:57:08 +00001513** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001514** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1515** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001516**
1517** Return an error code. If the commit could not complete because of
1518** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1519** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001520*/
drhff0587c2007-08-29 17:43:19 +00001521int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001522 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001523 int i;
danielk19771d850a72004-05-31 08:26:49 +00001524 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001525 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1526
1527 /* This function contains the logic that determines if a statement or
1528 ** transaction will be committed or rolled back as a result of the
1529 ** execution of this virtual machine.
1530 **
drh71b890a2007-10-03 15:30:52 +00001531 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001532 **
drh71b890a2007-10-03 15:30:52 +00001533 ** SQLITE_NOMEM
1534 ** SQLITE_IOERR
1535 ** SQLITE_FULL
1536 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001537 **
drh71b890a2007-10-03 15:30:52 +00001538 ** Then the internal cache might have been left in an inconsistent
1539 ** state. We need to rollback the statement transaction, if there is
1540 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001541 */
drh9a324642003-09-06 20:12:01 +00001542
drh17435752007-08-16 04:30:38 +00001543 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001544 p->rc = SQLITE_NOMEM;
1545 }
drhff0587c2007-08-29 17:43:19 +00001546 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001547 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001548 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001549 }
danielk19771d850a72004-05-31 08:26:49 +00001550 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001551
danielk197707cb5602006-01-20 10:55:05 +00001552 /* No commit or rollback needed if the program never started */
1553 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001554 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001555
1556 /* Lock all btrees used by the statement */
1557 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1558
drh71b890a2007-10-03 15:30:52 +00001559 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001560 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001561 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001562 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001563 if( isSpecialError ){
danielk197707cb5602006-01-20 10:55:05 +00001564 /* If the query was read-only, we need do no rollback at all. Otherwise,
1565 ** proceed with the special handling.
1566 */
drhad4a4b82008-11-05 16:37:34 +00001567 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
1568 if( p->rc==SQLITE_IOERR_BLOCKED && p->usesStmtJournal ){
danielk1977e965ac72007-06-13 15:22:28 +00001569 xFunc = sqlite3BtreeRollbackStmt;
1570 p->rc = SQLITE_BUSY;
drhad4a4b82008-11-05 16:37:34 +00001571 }else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL)
1572 && p->usesStmtJournal ){
danielk197707cb5602006-01-20 10:55:05 +00001573 xFunc = sqlite3BtreeRollbackStmt;
1574 }else{
1575 /* We are forced to roll back the active transaction. Before doing
1576 ** so, abort any other statements this handle currently has active.
1577 */
drhfb982642007-08-30 01:19:59 +00001578 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001579 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001580 db->autoCommit = 1;
1581 }
danielk1977261919c2005-12-06 12:52:59 +00001582 }
1583 }
danielk197707cb5602006-01-20 10:55:05 +00001584
1585 /* If the auto-commit flag is set and this is the only active vdbe, then
1586 ** we do either a commit or rollback of the current transaction.
1587 **
1588 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00001589 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00001590 */
danielk1977093e0f62008-11-13 18:00:14 +00001591 if( !sqlite3VtabInSync(db)
1592 && db->autoCommit
1593 && db->writeVdbeCnt==(p->readOnly==0)
1594 ){
danielk197707cb5602006-01-20 10:55:05 +00001595 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001596 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001597 ** successful or hit an 'OR FAIL' constraint. This means a commit
1598 ** is required.
1599 */
danielk19773e3a84d2008-08-01 17:37:40 +00001600 int rc = vdbeCommit(db, p);
danielk197707cb5602006-01-20 10:55:05 +00001601 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001602 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001603 return SQLITE_BUSY;
1604 }else if( rc!=SQLITE_OK ){
1605 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001606 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001607 }else{
1608 sqlite3CommitInternalChanges(db);
1609 }
1610 }else{
danielk197797a227c2006-01-20 16:32:04 +00001611 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001612 }
1613 }else if( !xFunc ){
1614 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001615 if( p->openedStatement ){
1616 xFunc = sqlite3BtreeCommitStmt;
1617 }
danielk197707cb5602006-01-20 10:55:05 +00001618 }else if( p->errorAction==OE_Abort ){
1619 xFunc = sqlite3BtreeRollbackStmt;
1620 }else{
drhfb982642007-08-30 01:19:59 +00001621 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001622 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001623 db->autoCommit = 1;
1624 }
danielk19771d850a72004-05-31 08:26:49 +00001625 }
danielk197707cb5602006-01-20 10:55:05 +00001626
1627 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1628 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1629 ** and the return code is still SQLITE_OK, set the return code to the new
1630 ** error value.
1631 */
1632 assert(!xFunc ||
1633 xFunc==sqlite3BtreeCommitStmt ||
1634 xFunc==sqlite3BtreeRollbackStmt
1635 );
1636 for(i=0; xFunc && i<db->nDb; i++){
1637 int rc;
1638 Btree *pBt = db->aDb[i].pBt;
1639 if( pBt ){
1640 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001641 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1642 p->rc = rc;
drh633e6d52008-07-28 19:34:53 +00001643 sqlite3DbFree(db, p->zErrMsg);
drhf089aa42008-07-08 19:34:06 +00001644 p->zErrMsg = 0;
danielk19778a7aea32006-01-23 15:25:48 +00001645 }
danielk197707cb5602006-01-20 10:55:05 +00001646 }
danielk197777d83ba2004-05-31 10:08:14 +00001647 }
danielk197707cb5602006-01-20 10:55:05 +00001648
1649 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1650 ** set the change counter.
1651 */
1652 if( p->changeCntOn && p->pc>=0 ){
1653 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1654 sqlite3VdbeSetChanges(db, p->nChange);
1655 }else{
1656 sqlite3VdbeSetChanges(db, 0);
1657 }
1658 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001659 }
danielk197707cb5602006-01-20 10:55:05 +00001660
1661 /* Rollback or commit any schema changes that occurred. */
1662 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1663 sqlite3ResetInternalSchema(db, 0);
1664 db->flags = (db->flags | SQLITE_InternChanges);
1665 }
drhff0587c2007-08-29 17:43:19 +00001666
1667 /* Release the locks */
1668 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001669 }
danielk19771d850a72004-05-31 08:26:49 +00001670
danielk197765fd59f2006-06-24 11:51:33 +00001671 /* We have successfully halted and closed the VM. Record this fact. */
1672 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001673 db->activeVdbeCnt--;
drhad4a4b82008-11-05 16:37:34 +00001674 if( !p->readOnly ){
1675 db->writeVdbeCnt--;
1676 }
1677 assert( db->activeVdbeCnt>=db->writeVdbeCnt );
drh9a324642003-09-06 20:12:01 +00001678 }
drh92f02c32004-09-02 14:57:08 +00001679 p->magic = VDBE_MAGIC_HALT;
1680 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001681 if( p->db->mallocFailed ){
1682 p->rc = SQLITE_NOMEM;
1683 }
danielk19771d850a72004-05-31 08:26:49 +00001684
drh92f02c32004-09-02 14:57:08 +00001685 return SQLITE_OK;
1686}
drh4cf7c7f2007-08-28 23:28:07 +00001687
drh92f02c32004-09-02 14:57:08 +00001688
1689/*
drh3c23a882007-01-09 14:01:13 +00001690** Each VDBE holds the result of the most recent sqlite3_step() call
1691** in p->rc. This routine sets that result back to SQLITE_OK.
1692*/
1693void sqlite3VdbeResetStepResult(Vdbe *p){
1694 p->rc = SQLITE_OK;
1695}
1696
1697/*
drh92f02c32004-09-02 14:57:08 +00001698** Clean up a VDBE after execution but do not delete the VDBE just yet.
1699** Write any error messages into *pzErrMsg. Return the result code.
1700**
1701** After this routine is run, the VDBE should be ready to be executed
1702** again.
1703**
1704** To look at it another way, this routine resets the state of the
1705** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1706** VDBE_MAGIC_INIT.
1707*/
drhc890fec2008-08-01 20:10:08 +00001708int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001709 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001710 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001711
1712 /* If the VM did not run to completion or if it encountered an
1713 ** error, then it might not have been halted properly. So halt
1714 ** it now.
1715 */
drh7e8b8482008-01-23 03:03:05 +00001716 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001717 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001718 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001719
drhfb7e7652005-01-24 00:28:42 +00001720 /* If the VDBE has be run even partially, then transfer the error code
1721 ** and error message from the VDBE into the main database structure. But
1722 ** if the VDBE has just been set to run but has not actually executed any
1723 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001724 */
drhfb7e7652005-01-24 00:28:42 +00001725 if( p->pc>=0 ){
1726 if( p->zErrMsg ){
danielk19779ff3f3f2008-10-11 17:51:38 +00001727 sqlite3BeginBenignMalloc();
drh633e6d52008-07-28 19:34:53 +00001728 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19779ff3f3f2008-10-11 17:51:38 +00001729 sqlite3EndBenignMalloc();
danielk197797a227c2006-01-20 16:32:04 +00001730 db->errCode = p->rc;
drh633e6d52008-07-28 19:34:53 +00001731 sqlite3DbFree(db, p->zErrMsg);
drhfb7e7652005-01-24 00:28:42 +00001732 p->zErrMsg = 0;
1733 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001734 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001735 }else{
drh4ac285a2006-09-15 07:28:50 +00001736 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001737 }
danielk1977a21c6b62005-01-24 10:25:59 +00001738 }else if( p->rc && p->expired ){
1739 /* The expired flag was set on the VDBE before the first call
1740 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1741 ** called), set the database error in this case as well.
1742 */
drh4ac285a2006-09-15 07:28:50 +00001743 sqlite3Error(db, p->rc, 0);
drh633e6d52008-07-28 19:34:53 +00001744 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
1745 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00001746 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001747 }
1748
1749 /* Reclaim all memory used by the VDBE
1750 */
drhc890fec2008-08-01 20:10:08 +00001751 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00001752
1753 /* Save profiling information from this VDBE run.
1754 */
drh9a324642003-09-06 20:12:01 +00001755#ifdef VDBE_PROFILE
1756 {
1757 FILE *out = fopen("vdbe_profile.out", "a");
1758 if( out ){
1759 int i;
1760 fprintf(out, "---- ");
1761 for(i=0; i<p->nOp; i++){
1762 fprintf(out, "%02x", p->aOp[i].opcode);
1763 }
1764 fprintf(out, "\n");
1765 for(i=0; i<p->nOp; i++){
1766 fprintf(out, "%6d %10lld %8lld ",
1767 p->aOp[i].cnt,
1768 p->aOp[i].cycles,
1769 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1770 );
danielk19774adee202004-05-08 08:23:19 +00001771 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001772 }
1773 fclose(out);
1774 }
1775 }
1776#endif
1777 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00001778 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001779}
drh92f02c32004-09-02 14:57:08 +00001780
drh9a324642003-09-06 20:12:01 +00001781/*
1782** Clean up and delete a VDBE after execution. Return an integer which is
1783** the result code. Write any error message text into *pzErrMsg.
1784*/
danielk19779e6db7d2004-06-21 08:18:51 +00001785int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001786 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001787 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00001788 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001789 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001790 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001791 return SQLITE_MISUSE;
1792 }
danielk19774adee202004-05-08 08:23:19 +00001793 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001794 return rc;
1795}
1796
1797/*
drhf92c7ff2004-06-19 15:40:23 +00001798** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001799** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001800** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001801** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001802*/
1803void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1804 int i;
1805 for(i=0; i<pVdbeFunc->nAux; i++){
1806 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1807 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1808 if( pAux->xDelete ){
1809 pAux->xDelete(pAux->pAux);
1810 }
1811 pAux->pAux = 0;
1812 }
1813 }
1814}
1815
1816/*
drh9a324642003-09-06 20:12:01 +00001817** Delete an entire VDBE.
1818*/
danielk19774adee202004-05-08 08:23:19 +00001819void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001820 int i;
drh633e6d52008-07-28 19:34:53 +00001821 sqlite3 *db;
1822
drh9a324642003-09-06 20:12:01 +00001823 if( p==0 ) return;
drh633e6d52008-07-28 19:34:53 +00001824 db = p->db;
drh9a324642003-09-06 20:12:01 +00001825 if( p->pPrev ){
1826 p->pPrev->pNext = p->pNext;
1827 }else{
drh633e6d52008-07-28 19:34:53 +00001828 assert( db->pVdbe==p );
1829 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00001830 }
1831 if( p->pNext ){
1832 p->pNext->pPrev = p->pPrev;
1833 }
drh76ff3a02004-09-24 22:32:30 +00001834 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001835 Op *pOp = p->aOp;
1836 for(i=0; i<p->nOp; i++, pOp++){
drh633e6d52008-07-28 19:34:53 +00001837 freeP4(db, pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001838#ifdef SQLITE_DEBUG
drh633e6d52008-07-28 19:34:53 +00001839 sqlite3DbFree(db, pOp->zComment);
drhd4e70eb2008-01-02 00:34:36 +00001840#endif
drh9a324642003-09-06 20:12:01 +00001841 }
drh633e6d52008-07-28 19:34:53 +00001842 sqlite3DbFree(db, p->aOp);
drh9a324642003-09-06 20:12:01 +00001843 }
drhc890fec2008-08-01 20:10:08 +00001844 releaseMemArray(p->aVar, p->nVar);
drh633e6d52008-07-28 19:34:53 +00001845 sqlite3DbFree(db, p->aLabel);
drh9cbf3422008-01-17 16:22:13 +00001846 if( p->aMem ){
drh633e6d52008-07-28 19:34:53 +00001847 sqlite3DbFree(db, &p->aMem[1]);
drh9cbf3422008-01-17 16:22:13 +00001848 }
drhc890fec2008-08-01 20:10:08 +00001849 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001850 sqlite3DbFree(db, p->aColName);
1851 sqlite3DbFree(db, p->zSql);
drh9a324642003-09-06 20:12:01 +00001852 p->magic = VDBE_MAGIC_DEAD;
drh633e6d52008-07-28 19:34:53 +00001853 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00001854}
drha11846b2004-01-07 18:52:56 +00001855
1856/*
drha11846b2004-01-07 18:52:56 +00001857** If a MoveTo operation is pending on the given cursor, then do that
1858** MoveTo now. Return an error code. If no MoveTo is pending, this
1859** routine does nothing and returns SQLITE_OK.
1860*/
drhdfe88ec2008-11-03 20:55:06 +00001861int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00001862 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001863 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001864#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001865 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001866#endif
drhf0863fe2005-06-12 21:35:51 +00001867 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00001868 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001869 if( rc ) return rc;
drhf0863fe2005-06-12 21:35:51 +00001870 p->lastRowid = keyToInt(p->movetoTarget);
1871 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001872 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001873 rc = sqlite3BtreeNext(p->pCursor, &res);
1874 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001875 }
drh10cfdd52006-08-08 15:42:59 +00001876#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001877 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001878#endif
drha11846b2004-01-07 18:52:56 +00001879 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001880 p->cacheStatus = CACHE_STALE;
drha3460582008-07-11 21:02:53 +00001881 }else if( p->pCursor ){
1882 int hasMoved;
1883 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
1884 if( rc ) return rc;
1885 if( hasMoved ){
1886 p->cacheStatus = CACHE_STALE;
1887 p->nullRow = 1;
1888 }
drha11846b2004-01-07 18:52:56 +00001889 }
1890 return SQLITE_OK;
1891}
danielk19774adee202004-05-08 08:23:19 +00001892
drhab9f7f12004-05-08 10:56:11 +00001893/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001894** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001895**
danielk1977cfcdaef2004-05-12 07:33:33 +00001896** sqlite3VdbeSerialType()
1897** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00001898** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00001899** sqlite3VdbeSerialPut()
1900** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00001901**
1902** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001903** data and index records. Each serialized value consists of a
1904** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1905** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001906**
danielk1977cfcdaef2004-05-12 07:33:33 +00001907** In an SQLite index record, the serial type is stored directly before
1908** the blob of data that it corresponds to. In a table record, all serial
1909** types are stored at the start of the record, and the blobs of data at
1910** the end. Hence these functions allow the caller to handle the
1911** serial-type and data blob seperately.
1912**
1913** The following table describes the various storage classes for data:
1914**
1915** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001916** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001917** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001918** 1 1 signed integer
1919** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001920** 3 3 signed integer
1921** 4 4 signed integer
1922** 5 6 signed integer
1923** 6 8 signed integer
1924** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001925** 8 0 Integer constant 0
1926** 9 0 Integer constant 1
1927** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001928** N>=12 and even (N-12)/2 BLOB
1929** N>=13 and odd (N-13)/2 text
1930**
drh35a59652006-01-02 18:24:40 +00001931** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1932** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001933*/
1934
1935/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001936** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001937*/
drhd946db02005-12-29 19:23:06 +00001938u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001939 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001940 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001941
1942 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001943 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001944 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001945 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001946 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00001947# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001948 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001949 u64 u;
1950 if( file_format>=4 && (i&1)==i ){
1951 return 8+i;
1952 }
1953 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001954 if( u<=127 ) return 1;
1955 if( u<=32767 ) return 2;
1956 if( u<=8388607 ) return 3;
1957 if( u<=2147483647 ) return 4;
1958 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001959 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001960 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001961 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001962 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001963 }
danielk1977e4359752008-11-03 09:39:45 +00001964 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00001965 n = pMem->n;
1966 if( flags & MEM_Zero ){
1967 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001968 }
drhfdf972a2007-05-02 13:30:27 +00001969 assert( n>=0 );
1970 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001971}
1972
1973/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001974** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001975*/
drh25aa1b42004-05-28 01:39:01 +00001976int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001977 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001978 return (serial_type-12)/2;
1979 }else{
drh57196282004-10-06 15:41:16 +00001980 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001981 return aSize[serial_type];
1982 }
danielk1977192ac1d2004-05-10 07:17:30 +00001983}
1984
1985/*
drh110daac2007-05-04 11:59:31 +00001986** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001987** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001988** upper 4 bytes. Return the result.
1989**
drh7a4f5022007-05-23 07:20:08 +00001990** For most architectures, this is a no-op.
1991**
1992** (later): It is reported to me that the mixed-endian problem
1993** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1994** that early versions of GCC stored the two words of a 64-bit
1995** float in the wrong order. And that error has been propagated
1996** ever since. The blame is not necessarily with GCC, though.
1997** GCC might have just copying the problem from a prior compiler.
1998** I am also told that newer versions of GCC that follow a different
1999** ABI get the byte order right.
2000**
2001** Developers using SQLite on an ARM7 should compile and run their
2002** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2003** enabled, some asserts below will ensure that the byte order of
2004** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002005**
2006** (2007-08-30) Frank van Vugt has studied this problem closely
2007** and has send his findings to the SQLite developers. Frank
2008** writes that some Linux kernels offer floating point hardware
2009** emulation that uses only 32-bit mantissas instead of a full
2010** 48-bits as required by the IEEE standard. (This is the
2011** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2012** byte swapping becomes very complicated. To avoid problems,
2013** the necessary byte swapping is carried out using a 64-bit integer
2014** rather than a 64-bit float. Frank assures us that the code here
2015** works for him. We, the developers, have no way to independently
2016** verify this, but Frank seems to know what he is talking about
2017** so we trust him.
drh110daac2007-05-04 11:59:31 +00002018*/
2019#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002020static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002021 union {
drh60d09a72007-08-30 15:05:08 +00002022 u64 r;
drh110daac2007-05-04 11:59:31 +00002023 u32 i[2];
2024 } u;
2025 u32 t;
2026
2027 u.r = in;
2028 t = u.i[0];
2029 u.i[0] = u.i[1];
2030 u.i[1] = t;
2031 return u.r;
2032}
2033# define swapMixedEndianFloat(X) X = floatSwap(X)
2034#else
2035# define swapMixedEndianFloat(X)
2036#endif
2037
2038/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002039** Write the serialized data blob for the value stored in pMem into
2040** buf. It is assumed that the caller has allocated sufficient space.
2041** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002042**
2043** nBuf is the amount of space left in buf[]. nBuf must always be
2044** large enough to hold the entire field. Except, if the field is
2045** a blob with a zero-filled tail, then buf[] might be just the right
2046** size to hold everything except for the zero-filled tail. If buf[]
2047** is only big enough to hold the non-zero prefix, then only write that
2048** prefix into buf[]. But if buf[] is large enough to hold both the
2049** prefix and the tail then write the prefix and set the tail to all
2050** zeros.
2051**
2052** Return the number of bytes actually written into buf[]. The number
2053** of bytes in the zero-filled tail is included in the return value only
2054** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002055*/
drhfdf972a2007-05-02 13:30:27 +00002056int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002057 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00002058 int len;
danielk1977183f9f72004-05-13 05:20:26 +00002059
drh1483e142004-05-21 21:12:42 +00002060 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002061 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002062 u64 v;
2063 int i;
drha19b7752004-05-30 21:14:58 +00002064 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002065 assert( sizeof(v)==sizeof(pMem->r) );
2066 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002067 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002068 }else{
drh3c024d62007-03-30 11:23:45 +00002069 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002070 }
drh1483e142004-05-21 21:12:42 +00002071 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002072 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00002073 while( i-- ){
2074 buf[i] = (v&0xFF);
2075 v >>= 8;
2076 }
2077 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002078 }
drhd946db02005-12-29 19:23:06 +00002079
danielk1977cfcdaef2004-05-12 07:33:33 +00002080 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002081 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00002082 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
2083 == sqlite3VdbeSerialTypeLen(serial_type) );
2084 assert( pMem->n<=nBuf );
2085 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002086 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002087 if( pMem->flags & MEM_Zero ){
2088 len += pMem->u.i;
2089 if( len>nBuf ){
2090 len = nBuf;
2091 }
2092 memset(&buf[pMem->n], 0, len-pMem->n);
2093 }
drhd946db02005-12-29 19:23:06 +00002094 return len;
2095 }
2096
2097 /* NULL or constants 0 or 1 */
2098 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002099}
2100
2101/*
2102** Deserialize the data blob pointed to by buf as serial type serial_type
2103** and store the result in pMem. Return the number of bytes read.
2104*/
danielk1977b1bc9532004-05-22 03:05:33 +00002105int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002106 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002107 u32 serial_type, /* Serial type to deserialize */
2108 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002109){
drh3c685822005-05-21 18:32:18 +00002110 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002111 case 10: /* Reserved for future use */
2112 case 11: /* Reserved for future use */
2113 case 0: { /* NULL */
2114 pMem->flags = MEM_Null;
2115 break;
2116 }
2117 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002118 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002119 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002120 return 1;
drh1483e142004-05-21 21:12:42 +00002121 }
drh3c685822005-05-21 18:32:18 +00002122 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002123 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002124 pMem->flags = MEM_Int;
2125 return 2;
2126 }
2127 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002128 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002129 pMem->flags = MEM_Int;
2130 return 3;
2131 }
2132 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002133 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002134 pMem->flags = MEM_Int;
2135 return 4;
2136 }
2137 case 5: { /* 6-byte signed integer */
2138 u64 x = (((signed char)buf[0])<<8) | buf[1];
2139 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2140 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002141 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002142 pMem->flags = MEM_Int;
2143 return 6;
2144 }
drh91124b32005-08-18 18:15:05 +00002145 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002146 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002147 u64 x;
2148 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002149#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002150 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002151 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2152 ** defined that 64-bit floating point values really are mixed
2153 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002154 */
drhde941c62005-08-28 01:34:21 +00002155 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002156 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002157 u64 t2 = t1;
2158 swapMixedEndianFloat(t2);
2159 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002160#endif
drhbfd6b032005-08-28 01:38:44 +00002161
drhd81bd4e2005-09-05 20:06:49 +00002162 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2163 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002164 x = (x<<32) | y;
2165 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002166 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002167 pMem->flags = MEM_Int;
2168 }else{
drh4f0c5872007-03-26 22:05:01 +00002169 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002170 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002171 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002172 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002173 }
2174 return 8;
2175 }
drhd946db02005-12-29 19:23:06 +00002176 case 8: /* Integer 0 */
2177 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002178 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002179 pMem->flags = MEM_Int;
2180 return 0;
2181 }
drh3c685822005-05-21 18:32:18 +00002182 default: {
2183 int len = (serial_type-12)/2;
2184 pMem->z = (char *)buf;
2185 pMem->n = len;
2186 pMem->xDel = 0;
2187 if( serial_type&0x01 ){
2188 pMem->flags = MEM_Str | MEM_Ephem;
2189 }else{
2190 pMem->flags = MEM_Blob | MEM_Ephem;
2191 }
2192 return len;
drh696b32f2004-05-30 01:51:52 +00002193 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002194 }
drh3c685822005-05-21 18:32:18 +00002195 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002196}
2197
drh0e6082e2006-01-12 20:28:35 +00002198
drh1e968a02008-03-25 00:22:21 +00002199/*
2200** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002201** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002202** that structure.
2203**
2204** The calling function might provide szSpace bytes of memory
2205** space at pSpace. This space can be used to hold the returned
2206** VDbeParsedRecord structure if it is large enough. If it is
2207** not big enough, space is obtained from sqlite3_malloc().
2208**
2209** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002210** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002211*/
drhe14006d2008-03-25 17:23:32 +00002212UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002213 KeyInfo *pKeyInfo, /* Information about the record format */
2214 int nKey, /* Size of the binary record */
2215 const void *pKey, /* The binary record */
drh23f79d02008-08-20 22:06:47 +00002216 UnpackedRecord *pSpace,/* Space available to hold resulting object */
drh1e968a02008-03-25 00:22:21 +00002217 int szSpace /* Size of pSpace[] in bytes */
2218){
2219 const unsigned char *aKey = (const unsigned char *)pKey;
drhe14006d2008-03-25 17:23:32 +00002220 UnpackedRecord *p;
danielk197700e13612008-11-17 19:18:54 +00002221 int nByte, d;
2222 u32 idx;
shane0b8d2762008-07-22 05:18:00 +00002223 u16 u; /* Unsigned loop counter */
drh1e968a02008-03-25 00:22:21 +00002224 u32 szHdr;
2225 Mem *pMem;
2226
drhfab69592008-04-10 14:57:24 +00002227 assert( sizeof(Mem)>sizeof(*p) );
2228 nByte = sizeof(Mem)*(pKeyInfo->nField+2);
drh1e968a02008-03-25 00:22:21 +00002229 if( nByte>szSpace ){
2230 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2231 if( p==0 ) return 0;
drhe63d9992008-08-13 19:11:48 +00002232 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002233 }else{
2234 p = pSpace;
drhe63d9992008-08-13 19:11:48 +00002235 p->flags = UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002236 }
2237 p->pKeyInfo = pKeyInfo;
2238 p->nField = pKeyInfo->nField + 1;
drhfab69592008-04-10 14:57:24 +00002239 p->aMem = pMem = &((Mem*)p)[1];
shane3f8d5cf2008-04-24 19:15:09 +00002240 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002241 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00002242 u = 0;
2243 while( idx<szHdr && u<p->nField ){
drh1e968a02008-03-25 00:22:21 +00002244 u32 serial_type;
2245
danielk197700e13612008-11-17 19:18:54 +00002246 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00002247 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
2248 pMem->enc = pKeyInfo->enc;
2249 pMem->db = pKeyInfo->db;
2250 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002251 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002252 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002253 pMem++;
shane0b8d2762008-07-22 05:18:00 +00002254 u++;
drh1e968a02008-03-25 00:22:21 +00002255 }
drh7d10d5a2008-08-20 16:35:10 +00002256 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00002257 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00002258 return (void*)p;
2259}
2260
2261/*
drhe14006d2008-03-25 17:23:32 +00002262** This routine destroys a UnpackedRecord object
drh1e968a02008-03-25 00:22:21 +00002263*/
drhe14006d2008-03-25 17:23:32 +00002264void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh1e968a02008-03-25 00:22:21 +00002265 if( p ){
drhe63d9992008-08-13 19:11:48 +00002266 if( p->flags & UNPACKED_NEED_DESTROY ){
drh1e968a02008-03-25 00:22:21 +00002267 int i;
drhe14006d2008-03-25 17:23:32 +00002268 Mem *pMem;
2269 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
danielk19775f096132008-03-28 15:44:09 +00002270 if( pMem->zMalloc ){
drhe14006d2008-03-25 17:23:32 +00002271 sqlite3VdbeMemRelease(pMem);
drh1e968a02008-03-25 00:22:21 +00002272 }
2273 }
2274 }
drhe63d9992008-08-13 19:11:48 +00002275 if( p->flags & UNPACKED_NEED_FREE ){
drh633e6d52008-07-28 19:34:53 +00002276 sqlite3DbFree(p->pKeyInfo->db, p);
drh1e968a02008-03-25 00:22:21 +00002277 }
2278 }
2279}
2280
2281/*
2282** This function compares the two table rows or index records
2283** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00002284** or positive integer if key1 is less than, equal to or
2285** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00002286** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2287** key must be a parsed key such as obtained from
2288** sqlite3VdbeParseRecord.
2289**
2290** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00002291** The key with fewer fields is usually compares less than the
2292** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
2293** and the common prefixes are equal, then key1 is less than key2.
2294** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
2295** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00002296** the parts beyond the common prefix are ignored.
2297**
drhe63d9992008-08-13 19:11:48 +00002298** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
2299** the header of pKey1 is ignored. It is assumed that pKey1 is
2300** an index key, and thus ends with a rowid value. The last byte
2301** of the header will therefore be the serial type of the rowid:
2302** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
2303** The serial type of the final rowid will always be a single byte.
2304** By ignoring this last byte of the header, we force the comparison
2305** to ignore the rowid at the end of key1.
drh1e968a02008-03-25 00:22:21 +00002306*/
drhe14006d2008-03-25 17:23:32 +00002307int sqlite3VdbeRecordCompare(
drhec1fc802008-08-13 14:07:40 +00002308 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00002309 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00002310){
danielk197700e13612008-11-17 19:18:54 +00002311 int d1; /* Offset into aKey[] of next data element */
drh1e968a02008-03-25 00:22:21 +00002312 u32 idx1; /* Offset into aKey[] of next header element */
2313 u32 szHdr1; /* Number of bytes in header */
2314 int i = 0;
2315 int nField;
2316 int rc = 0;
2317 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2318 KeyInfo *pKeyInfo;
2319 Mem mem1;
2320
2321 pKeyInfo = pPKey2->pKeyInfo;
2322 mem1.enc = pKeyInfo->enc;
2323 mem1.db = pKeyInfo->db;
2324 mem1.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002325 mem1.zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002326
shane3f8d5cf2008-04-24 19:15:09 +00002327 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002328 d1 = szHdr1;
drhe63d9992008-08-13 19:11:48 +00002329 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
2330 szHdr1--;
2331 }
drh1e968a02008-03-25 00:22:21 +00002332 nField = pKeyInfo->nField;
2333 while( idx1<szHdr1 && i<pPKey2->nField ){
2334 u32 serial_type1;
2335
2336 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002337 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002338 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2339
2340 /* Extract the values to be compared.
2341 */
2342 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2343
2344 /* Do the comparison
2345 */
drhe14006d2008-03-25 17:23:32 +00002346 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002347 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002348 if( rc!=0 ){
2349 break;
2350 }
2351 i++;
2352 }
danielk19775f096132008-03-28 15:44:09 +00002353 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1);
drh1e968a02008-03-25 00:22:21 +00002354
drh1e968a02008-03-25 00:22:21 +00002355 if( rc==0 ){
drhec1fc802008-08-13 14:07:40 +00002356 /* rc==0 here means that one of the keys ran out of fields and
drhe63d9992008-08-13 19:11:48 +00002357 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
2358 ** flag is set, then break the tie by treating key2 as larger.
2359 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
drhec1fc802008-08-13 14:07:40 +00002360 ** are considered to be equal. Otherwise, the longer key is the
2361 ** larger. As it happens, the pPKey2 will always be the longer
2362 ** if there is a difference.
2363 */
drhe63d9992008-08-13 19:11:48 +00002364 if( pPKey2->flags & UNPACKED_INCRKEY ){
drh1e968a02008-03-25 00:22:21 +00002365 rc = -1;
drhe63d9992008-08-13 19:11:48 +00002366 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
drhec1fc802008-08-13 14:07:40 +00002367 /* Leave rc==0 */
2368 }else if( idx1<szHdr1 ){
2369 rc = 1;
drh1e968a02008-03-25 00:22:21 +00002370 }
2371 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2372 && pKeyInfo->aSortOrder[i] ){
2373 rc = -rc;
2374 }
2375
2376 return rc;
2377}
drhec1fc802008-08-13 14:07:40 +00002378
danielk1977eb015e02004-05-18 01:31:14 +00002379
2380/*
drh7a224de2004-06-02 01:22:02 +00002381** pCur points at an index entry created using the OP_MakeRecord opcode.
2382** Read the rowid (the last field in the record) and store it in *rowid.
2383** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002384*/
drhb21c8cd2007-08-21 19:33:56 +00002385int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002386 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002387 int rc;
drhd5788202004-05-28 08:21:05 +00002388 u32 szHdr; /* Size of the header */
2389 u32 typeRowid; /* Serial type of the rowid */
2390 u32 lenRowid; /* Size of the rowid */
2391 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002392
drhd5788202004-05-28 08:21:05 +00002393 sqlite3BtreeKeySize(pCur, &nCellKey);
2394 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002395 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002396 }
danielk1977a7a8e142008-02-13 18:25:27 +00002397 m.flags = 0;
2398 m.db = 0;
danielk19775f096132008-03-28 15:44:09 +00002399 m.zMalloc = 0;
drhb21c8cd2007-08-21 19:33:56 +00002400 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002401 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002402 return rc;
2403 }
shane3f8d5cf2008-04-24 19:15:09 +00002404 (void)getVarint32((u8*)m.z, szHdr);
2405 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drhd5788202004-05-28 08:21:05 +00002406 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002407 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002408 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002409 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002410 return SQLITE_OK;
2411}
2412
drh7cf6e4d2004-05-19 14:56:55 +00002413/*
drhd3d39e92004-05-20 22:16:29 +00002414** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002415** the key string in pKey (of length nKey). Write into *pRes a number
2416** that is negative, zero, or positive if pC is less than, equal to,
2417** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002418**
drhd5788202004-05-28 08:21:05 +00002419** pKey is either created without a rowid or is truncated so that it
2420** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00002421** is ignored as well. Hence, this routine only compares the prefixes
2422** of the keys prior to the final rowid, not the entire key.
2423**
2424** pUnpacked may be an unpacked version of pKey,nKey. If pUnpacked is
2425** supplied it is used in place of pKey,nKey.
drh7cf6e4d2004-05-19 14:56:55 +00002426*/
danielk1977183f9f72004-05-13 05:20:26 +00002427int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00002428 VdbeCursor *pC, /* The cursor to compare against */
drhec1fc802008-08-13 14:07:40 +00002429 UnpackedRecord *pUnpacked, /* Unpacked version of pKey and nKey */
drh7cf6e4d2004-05-19 14:56:55 +00002430 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002431){
drh61fc5952007-04-01 23:49:51 +00002432 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002433 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002434 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002435 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002436
2437 sqlite3BtreeKeySize(pCur, &nCellKey);
2438 if( nCellKey<=0 ){
2439 *res = 0;
2440 return SQLITE_OK;
2441 }
danielk1977a7a8e142008-02-13 18:25:27 +00002442 m.db = 0;
2443 m.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002444 m.zMalloc = 0;
drhec1fc802008-08-13 14:07:40 +00002445 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2446 if( rc ){
drhd5788202004-05-28 08:21:05 +00002447 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002448 }
drhe63d9992008-08-13 19:11:48 +00002449 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
2450 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00002451 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002452 return SQLITE_OK;
2453}
danielk1977b28af712004-06-21 06:50:26 +00002454
2455/*
2456** This routine sets the value to be returned by subsequent calls to
2457** sqlite3_changes() on the database handle 'db'.
2458*/
2459void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002460 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002461 db->nChange = nChange;
2462 db->nTotalChange += nChange;
2463}
2464
2465/*
2466** Set a flag in the vdbe to update the change counter when it is finalised
2467** or reset.
2468*/
drh4794f732004-11-05 17:17:50 +00002469void sqlite3VdbeCountChanges(Vdbe *v){
2470 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002471}
drhd89bd002005-01-22 03:03:54 +00002472
2473/*
2474** Mark every prepared statement associated with a database connection
2475** as expired.
2476**
2477** An expired statement means that recompilation of the statement is
2478** recommend. Statements expire when things happen that make their
2479** programs obsolete. Removing user-defined functions or collating
2480** sequences, or changing an authorization function are the types of
2481** things that make prepared statements obsolete.
2482*/
2483void sqlite3ExpirePreparedStatements(sqlite3 *db){
2484 Vdbe *p;
2485 for(p = db->pVdbe; p; p=p->pNext){
2486 p->expired = 1;
2487 }
2488}
danielk1977aee18ef2005-03-09 12:26:50 +00002489
2490/*
2491** Return the database associated with the Vdbe.
2492*/
2493sqlite3 *sqlite3VdbeDb(Vdbe *v){
2494 return v->db;
2495}