blob: 64b7e1b1ef2981bbe53ba54f1e0a87075666fee5 [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**
shane75ac1de2009-06-09 18:58:52 +000017** $Id: vdbeaux.c,v 1.460 2009/06/09 18:58:53 shane Exp $
drh9a324642003-09-06 20:12:01 +000018*/
19#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000020#include "vdbeInt.h"
21
22
drh46c99e02007-08-27 23:26:59 +000023
drh9a324642003-09-06 20:12:01 +000024/*
25** When debugging the code generator in a symbolic debugger, one can
mlcreech3a00f902008-03-04 17:45:01 +000026** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000027** as they are added to the instruction stream.
28*/
drh8d904f02005-06-14 17:47:58 +000029#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +000030int sqlite3VdbeAddopTrace = 0;
drh9a324642003-09-06 20:12:01 +000031#endif
32
33
34/*
35** Create a new virtual database engine.
36*/
drh9bb575f2004-09-06 17:24:11 +000037Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000038 Vdbe *p;
drh17435752007-08-16 04:30:38 +000039 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000040 if( p==0 ) return 0;
41 p->db = db;
42 if( db->pVdbe ){
43 db->pVdbe->pPrev = p;
44 }
45 p->pNext = db->pVdbe;
46 p->pPrev = 0;
47 db->pVdbe = p;
48 p->magic = VDBE_MAGIC_INIT;
49 return p;
50}
51
52/*
drhb900aaf2006-11-09 00:24:53 +000053** Remember the SQL string for a prepared statement.
54*/
danielk19776ab3a2e2009-02-19 14:39:25 +000055void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
drhb900aaf2006-11-09 00:24:53 +000056 if( p==0 ) return;
danielk19776ab3a2e2009-02-19 14:39:25 +000057#ifdef SQLITE_OMIT_TRACE
58 if( !isPrepareV2 ) return;
59#endif
drhb900aaf2006-11-09 00:24:53 +000060 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000061 p->zSql = sqlite3DbStrNDup(p->db, z, n);
shanec0688ea2009-03-05 03:48:06 +000062 p->isPrepareV2 = isPrepareV2 ? 1 : 0;
drhb900aaf2006-11-09 00:24:53 +000063}
64
65/*
66** Return the SQL associated with a prepared statement
67*/
danielk1977d0e2a852007-11-14 06:48:48 +000068const char *sqlite3_sql(sqlite3_stmt *pStmt){
danielk19776ab3a2e2009-02-19 14:39:25 +000069 Vdbe *p = (Vdbe *)pStmt;
70 return (p->isPrepareV2 ? p->zSql : 0);
drhb900aaf2006-11-09 00:24:53 +000071}
72
73/*
drhc5155252007-01-08 21:07:17 +000074** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000075*/
drhc5155252007-01-08 21:07:17 +000076void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
77 Vdbe tmp, *pTmp;
78 char *zTmp;
drhc5155252007-01-08 21:07:17 +000079 tmp = *pA;
80 *pA = *pB;
81 *pB = tmp;
82 pTmp = pA->pNext;
83 pA->pNext = pB->pNext;
84 pB->pNext = pTmp;
85 pTmp = pA->pPrev;
86 pA->pPrev = pB->pPrev;
87 pB->pPrev = pTmp;
88 zTmp = pA->zSql;
89 pA->zSql = pB->zSql;
90 pB->zSql = zTmp;
danielk19776ab3a2e2009-02-19 14:39:25 +000091 pB->isPrepareV2 = pA->isPrepareV2;
drhb900aaf2006-11-09 00:24:53 +000092}
93
drhcf1023c2007-05-08 20:59:49 +000094#ifdef SQLITE_DEBUG
drhb900aaf2006-11-09 00:24:53 +000095/*
drh9a324642003-09-06 20:12:01 +000096** Turn tracing on or off
97*/
danielk19774adee202004-05-08 08:23:19 +000098void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000099 p->trace = trace;
100}
drhcf1023c2007-05-08 20:59:49 +0000101#endif
drh9a324642003-09-06 20:12:01 +0000102
103/*
danielk197700e13612008-11-17 19:18:54 +0000104** Resize the Vdbe.aOp array so that it is at least one op larger than
105** it was.
danielk1977ace3eb22006-01-26 10:35:04 +0000106**
danielk197700e13612008-11-17 19:18:54 +0000107** If an out-of-memory error occurs while resizing the array, return
108** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
109** unchanged (this is so that any opcodes already allocated can be
110** correctly deallocated along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000111*/
danielk197700e13612008-11-17 19:18:54 +0000112static int growOpArray(Vdbe *p){
drha4e5d582007-10-20 15:41:57 +0000113 VdbeOp *pNew;
danielk197700e13612008-11-17 19:18:54 +0000114 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
115 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
drha4e5d582007-10-20 15:41:57 +0000116 if( pNew ){
drhb45f65d2009-03-01 19:42:11 +0000117 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
drha4e5d582007-10-20 15:41:57 +0000118 p->aOp = pNew;
drh76ff3a02004-09-24 22:32:30 +0000119 }
danielk197700e13612008-11-17 19:18:54 +0000120 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
drh76ff3a02004-09-24 22:32:30 +0000121}
122
123/*
drh9a324642003-09-06 20:12:01 +0000124** Add a new instruction to the list of instructions current in the
125** VDBE. Return the address of the new instruction.
126**
127** Parameters:
128**
129** p Pointer to the VDBE
130**
131** op The opcode for this instruction
132**
drh66a51672008-01-03 00:01:23 +0000133** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000134**
danielk19774adee202004-05-08 08:23:19 +0000135** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000136** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000137** operand.
138*/
drh66a51672008-01-03 00:01:23 +0000139int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000140 int i;
drh701a0ae2004-02-22 20:05:00 +0000141 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000142
143 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000144 assert( p->magic==VDBE_MAGIC_INIT );
drh8df32842008-12-09 02:51:23 +0000145 assert( op>0 && op<0xff );
drhfd2d26b2006-03-15 22:44:36 +0000146 if( p->nOpAlloc<=i ){
danielk197700e13612008-11-17 19:18:54 +0000147 if( growOpArray(p) ){
drhfd2d26b2006-03-15 22:44:36 +0000148 return 0;
149 }
drh9a324642003-09-06 20:12:01 +0000150 }
danielk197701256832007-04-18 14:24:32 +0000151 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000152 pOp = &p->aOp[i];
drh8df32842008-12-09 02:51:23 +0000153 pOp->opcode = (u8)op;
drh26c9b5e2008-04-11 14:56:53 +0000154 pOp->p5 = 0;
drh701a0ae2004-02-22 20:05:00 +0000155 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000156 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000157 pOp->p3 = p3;
158 pOp->p4.p = 0;
159 pOp->p4type = P4_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000160 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000161#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000162 pOp->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000163 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000164#endif
drh26c9b5e2008-04-11 14:56:53 +0000165#ifdef VDBE_PROFILE
166 pOp->cycles = 0;
167 pOp->cnt = 0;
168#endif
drh9a324642003-09-06 20:12:01 +0000169 return i;
170}
drh66a51672008-01-03 00:01:23 +0000171int sqlite3VdbeAddOp0(Vdbe *p, int op){
172 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
173}
174int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
175 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
176}
177int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
178 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000179}
180
drh66a51672008-01-03 00:01:23 +0000181
drh701a0ae2004-02-22 20:05:00 +0000182/*
drh66a51672008-01-03 00:01:23 +0000183** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000184*/
drh66a51672008-01-03 00:01:23 +0000185int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000186 Vdbe *p, /* Add the opcode to this VM */
187 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000188 int p1, /* The P1 operand */
189 int p2, /* The P2 operand */
190 int p3, /* The P3 operand */
191 const char *zP4, /* The P4 operand */
192 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000193){
drh66a51672008-01-03 00:01:23 +0000194 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
195 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000196 return addr;
197}
198
199/*
drh9a324642003-09-06 20:12:01 +0000200** Create a new symbolic label for an instruction that has yet to be
201** coded. The symbolic label is really just a negative number. The
202** label can be used as the P2 value of an operation. Later, when
203** the label is resolved to a specific address, the VDBE will scan
204** through its operation list and change all values of P2 which match
205** the label into the resolved address.
206**
207** The VDBE knows that a P2 value is a label because labels are
208** always negative and P2 values are suppose to be non-negative.
209** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000210**
211** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000212*/
danielk19774adee202004-05-08 08:23:19 +0000213int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000214 int i;
215 i = p->nLabel++;
216 assert( p->magic==VDBE_MAGIC_INIT );
217 if( i>=p->nLabelAlloc ){
drh6a1e0712008-12-05 15:24:15 +0000218 int n = p->nLabelAlloc*2 + 5;
danielk19771e536952007-08-16 10:09:01 +0000219 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drh6a1e0712008-12-05 15:24:15 +0000220 n*sizeof(p->aLabel[0]));
221 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
drh9a324642003-09-06 20:12:01 +0000222 }
drh76ff3a02004-09-24 22:32:30 +0000223 if( p->aLabel ){
224 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000225 }
drh9a324642003-09-06 20:12:01 +0000226 return -1-i;
227}
228
229/*
230** Resolve label "x" to be the address of the next instruction to
231** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000232** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000233*/
danielk19774adee202004-05-08 08:23:19 +0000234void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000235 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000236 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000237 assert( j>=0 && j<p->nLabel );
238 if( p->aLabel ){
239 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000240 }
241}
242
243/*
drh9cbf3422008-01-17 16:22:13 +0000244** Loop through the program looking for P2 values that are negative
245** on jump instructions. Each such value is a label. Resolve the
246** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000247**
248** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000249**
drh13449892005-09-07 21:22:45 +0000250** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000251** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000252** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000253**
drh38449902005-06-07 01:43:41 +0000254** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000255** instructions that might cause a statement rollback. Such instructions
256** are:
257**
258** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
259** * OP_Destroy
260** * OP_VUpdate
261** * OP_VRename
262**
263** If no such instruction is found, then every Statement instruction
264** is changed to a Noop. In this way, we avoid creating the statement
265** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000266*/
drh9cbf3422008-01-17 16:22:13 +0000267static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000268 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000269 int nMaxArgs = 0;
drh76ff3a02004-09-24 22:32:30 +0000270 Op *pOp;
271 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000272 int doesStatementRollback = 0;
273 int hasStatementBegin = 0;
drhad4a4b82008-11-05 16:37:34 +0000274 p->readOnly = 1;
275 p->usesStmtJournal = 0;
drh76ff3a02004-09-24 22:32:30 +0000276 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000277 u8 opcode = pOp->opcode;
278
drha2baf3a2008-06-18 15:34:09 +0000279 if( opcode==OP_Function || opcode==OP_AggStep ){
drh98757152008-01-09 23:04:12 +0000280 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
danielk1977399918f2006-06-14 13:03:23 +0000281#ifndef SQLITE_OMIT_VIRTUALTABLE
drha2baf3a2008-06-18 15:34:09 +0000282 }else if( opcode==OP_VUpdate ){
danielk1977bc04f852005-03-29 08:26:13 +0000283 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drha2baf3a2008-06-18 15:34:09 +0000284#endif
danielk19775dfecba2008-06-23 13:57:21 +0000285 }
danielk1977182c4ba2007-06-27 15:53:34 +0000286 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000287 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
288 doesStatementRollback = 1;
289 }
drh38449902005-06-07 01:43:41 +0000290 }else if( opcode==OP_Statement ){
291 hasStatementBegin = 1;
drhad4a4b82008-11-05 16:37:34 +0000292 p->usesStmtJournal = 1;
drh77658e22007-12-04 16:54:52 +0000293 }else if( opcode==OP_Destroy ){
294 doesStatementRollback = 1;
drhad4a4b82008-11-05 16:37:34 +0000295 }else if( opcode==OP_Transaction && pOp->p2!=0 ){
296 p->readOnly = 0;
danielk1977182c4ba2007-06-27 15:53:34 +0000297#ifndef SQLITE_OMIT_VIRTUALTABLE
298 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
299 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000300 }else if( opcode==OP_VFilter ){
301 int n;
302 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000303 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000304 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000305 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000306#endif
danielk1977bc04f852005-03-29 08:26:13 +0000307 }
danielk1977634f2982005-03-28 08:44:07 +0000308
drhd2981512008-01-04 19:33:49 +0000309 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
310 assert( -1-pOp->p2<p->nLabel );
311 pOp->p2 = aLabel[-1-pOp->p2];
312 }
drh76ff3a02004-09-24 22:32:30 +0000313 }
drh633e6d52008-07-28 19:34:53 +0000314 sqlite3DbFree(p->db, p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000315 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000316
317 *pMaxFuncArgs = nMaxArgs;
drh38449902005-06-07 01:43:41 +0000318
319 /* If we never rollback a statement transaction, then statement
320 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000321 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000322 ** which can be expensive on some platforms.
323 */
324 if( hasStatementBegin && !doesStatementRollback ){
drhad4a4b82008-11-05 16:37:34 +0000325 p->usesStmtJournal = 0;
drh38449902005-06-07 01:43:41 +0000326 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
327 if( pOp->opcode==OP_Statement ){
328 pOp->opcode = OP_Noop;
329 }
330 }
331 }
drh76ff3a02004-09-24 22:32:30 +0000332}
333
334/*
drh9a324642003-09-06 20:12:01 +0000335** Return the address of the next instruction to be inserted.
336*/
danielk19774adee202004-05-08 08:23:19 +0000337int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000338 assert( p->magic==VDBE_MAGIC_INIT );
339 return p->nOp;
340}
341
342/*
343** Add a whole list of operations to the operation stack. Return the
344** address of the first operation added.
345*/
danielk19774adee202004-05-08 08:23:19 +0000346int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000347 int addr;
348 assert( p->magic==VDBE_MAGIC_INIT );
danielk197700e13612008-11-17 19:18:54 +0000349 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
drh76ff3a02004-09-24 22:32:30 +0000350 return 0;
drh9a324642003-09-06 20:12:01 +0000351 }
352 addr = p->nOp;
353 if( nOp>0 ){
354 int i;
drh905793e2004-02-21 13:31:09 +0000355 VdbeOpList const *pIn = aOp;
356 for(i=0; i<nOp; i++, pIn++){
357 int p2 = pIn->p2;
358 VdbeOp *pOut = &p->aOp[i+addr];
359 pOut->opcode = pIn->opcode;
360 pOut->p1 = pIn->p1;
drh8558cde2008-01-05 05:20:10 +0000361 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
362 pOut->p2 = addr + ADDR(p2);
363 }else{
364 pOut->p2 = p2;
365 }
drh24003452008-01-03 01:28:59 +0000366 pOut->p3 = pIn->p3;
367 pOut->p4type = P4_NOTUSED;
368 pOut->p4.p = 0;
369 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000370#ifdef SQLITE_DEBUG
drh26c9b5e2008-04-11 14:56:53 +0000371 pOut->zComment = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000372 if( sqlite3VdbeAddopTrace ){
danielk19774adee202004-05-08 08:23:19 +0000373 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000374 }
375#endif
376 }
377 p->nOp += nOp;
378 }
379 return addr;
380}
381
382/*
383** Change the value of the P1 operand for a specific instruction.
384** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000385** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000386** few minor changes to the program.
387*/
danielk19774adee202004-05-08 08:23:19 +0000388void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000389 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000390 if( p && addr>=0 && p->nOp>addr && p->aOp ){
391 p->aOp[addr].p1 = val;
392 }
393}
394
395/*
396** Change the value of the P2 operand for a specific instruction.
397** This routine is useful for setting a jump destination.
398*/
danielk19774adee202004-05-08 08:23:19 +0000399void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000400 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000401 if( p && addr>=0 && p->nOp>addr && p->aOp ){
402 p->aOp[addr].p2 = val;
403 }
404}
405
drhd654be82005-09-20 17:42:23 +0000406/*
danielk19771f4aa332008-01-03 09:51:55 +0000407** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000408*/
409void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
410 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
411 if( p && addr>=0 && p->nOp>addr && p->aOp ){
412 p->aOp[addr].p3 = val;
413 }
414}
415
416/*
drh35573352008-01-08 23:54:25 +0000417** Change the value of the P5 operand for the most recently
418** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000419*/
drh35573352008-01-08 23:54:25 +0000420void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
danielk19771f4aa332008-01-03 09:51:55 +0000421 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh35573352008-01-08 23:54:25 +0000422 if( p && p->aOp ){
423 assert( p->nOp>0 );
424 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000425 }
426}
427
428/*
drhf8875402006-03-17 13:56:34 +0000429** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000430** the address of the next instruction to be coded.
431*/
432void sqlite3VdbeJumpHere(Vdbe *p, int addr){
433 sqlite3VdbeChangeP2(p, addr, p->nOp);
434}
drhb38ad992005-09-16 00:27:01 +0000435
drhb7f6f682006-07-08 17:06:43 +0000436
437/*
438** If the input FuncDef structure is ephemeral, then free it. If
439** the FuncDef is not ephermal, then do nothing.
440*/
drh633e6d52008-07-28 19:34:53 +0000441static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
drhb7f6f682006-07-08 17:06:43 +0000442 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh633e6d52008-07-28 19:34:53 +0000443 sqlite3DbFree(db, pDef);
drhb7f6f682006-07-08 17:06:43 +0000444 }
445}
446
drhb38ad992005-09-16 00:27:01 +0000447/*
drh66a51672008-01-03 00:01:23 +0000448** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000449*/
drh633e6d52008-07-28 19:34:53 +0000450static void freeP4(sqlite3 *db, int p4type, void *p4){
drh0acb7e42008-06-25 00:12:41 +0000451 if( p4 ){
drh66a51672008-01-03 00:01:23 +0000452 switch( p4type ){
453 case P4_REAL:
454 case P4_INT64:
455 case P4_MPRINTF:
456 case P4_DYNAMIC:
457 case P4_KEYINFO:
drh0acb7e42008-06-25 00:12:41 +0000458 case P4_INTARRAY:
drh66a51672008-01-03 00:01:23 +0000459 case P4_KEYINFO_HANDOFF: {
drh633e6d52008-07-28 19:34:53 +0000460 sqlite3DbFree(db, p4);
drhac1733d2005-09-17 17:58:22 +0000461 break;
462 }
drh66a51672008-01-03 00:01:23 +0000463 case P4_VDBEFUNC: {
drh0acb7e42008-06-25 00:12:41 +0000464 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
drh633e6d52008-07-28 19:34:53 +0000465 freeEphemeralFunction(db, pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000466 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh633e6d52008-07-28 19:34:53 +0000467 sqlite3DbFree(db, pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000468 break;
469 }
drh66a51672008-01-03 00:01:23 +0000470 case P4_FUNCDEF: {
drh633e6d52008-07-28 19:34:53 +0000471 freeEphemeralFunction(db, (FuncDef*)p4);
drhb7f6f682006-07-08 17:06:43 +0000472 break;
473 }
drh66a51672008-01-03 00:01:23 +0000474 case P4_MEM: {
drh0acb7e42008-06-25 00:12:41 +0000475 sqlite3ValueFree((sqlite3_value*)p4);
drhac1733d2005-09-17 17:58:22 +0000476 break;
477 }
drhb38ad992005-09-16 00:27:01 +0000478 }
479 }
480}
481
482
drh9a324642003-09-06 20:12:01 +0000483/*
drhf8875402006-03-17 13:56:34 +0000484** Change N opcodes starting at addr to No-ops.
485*/
486void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000487 if( p && p->aOp ){
488 VdbeOp *pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000489 sqlite3 *db = p->db;
danielk197792d4d7a2007-05-04 12:05:56 +0000490 while( N-- ){
drh633e6d52008-07-28 19:34:53 +0000491 freeP4(db, pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000492 memset(pOp, 0, sizeof(pOp[0]));
493 pOp->opcode = OP_Noop;
494 pOp++;
495 }
drhf8875402006-03-17 13:56:34 +0000496 }
497}
498
499/*
drh66a51672008-01-03 00:01:23 +0000500** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000501** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000502** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000503** few minor changes to the program.
504**
drh66a51672008-01-03 00:01:23 +0000505** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000506** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000507** A value of n==0 means copy bytes of zP4 up to and including the
508** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000509**
drh66a51672008-01-03 00:01:23 +0000510** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000511** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000512** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000513** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000514** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000515** caller should not free the allocation, it will be freed when the Vdbe is
516** finalized.
517**
drh66a51672008-01-03 00:01:23 +0000518** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000519** to a string or structure that is guaranteed to exist for the lifetime of
520** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000521**
drh66a51672008-01-03 00:01:23 +0000522** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000523*/
drh66a51672008-01-03 00:01:23 +0000524void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000525 Op *pOp;
drh633e6d52008-07-28 19:34:53 +0000526 sqlite3 *db;
drh91fd4d42008-01-19 20:11:25 +0000527 assert( p!=0 );
drh633e6d52008-07-28 19:34:53 +0000528 db = p->db;
drh91fd4d42008-01-19 20:11:25 +0000529 assert( p->magic==VDBE_MAGIC_INIT );
drh633e6d52008-07-28 19:34:53 +0000530 if( p->aOp==0 || db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000531 if (n != P4_KEYINFO) {
drh633e6d52008-07-28 19:34:53 +0000532 freeP4(db, n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000533 }
danielk1977d5d56522005-03-16 12:15:20 +0000534 return;
535 }
drh91fd4d42008-01-19 20:11:25 +0000536 assert( addr<p->nOp );
537 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000538 addr = p->nOp - 1;
539 if( addr<0 ) return;
540 }
541 pOp = &p->aOp[addr];
drh633e6d52008-07-28 19:34:53 +0000542 freeP4(db, pOp->p4type, pOp->p4.p);
drh66a51672008-01-03 00:01:23 +0000543 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000544 if( n==P4_INT32 ){
mlcreech12d40822008-03-06 07:35:21 +0000545 /* Note: this cast is safe, because the origin data point was an int
546 ** that was cast to a (const char *). */
shane1fc41292008-07-08 22:28:48 +0000547 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
drh8df32842008-12-09 02:51:23 +0000548 pOp->p4type = P4_INT32;
drh98757152008-01-09 23:04:12 +0000549 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000550 pOp->p4.p = 0;
551 pOp->p4type = P4_NOTUSED;
552 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000553 KeyInfo *pKeyInfo;
554 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000555
drh66a51672008-01-03 00:01:23 +0000556 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000557 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drhe5ae5732008-06-15 02:51:47 +0000558 pKeyInfo = sqlite3Malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000559 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000560 if( pKeyInfo ){
drhb21e7c72008-06-22 12:37:57 +0000561 u8 *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000562 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000563 aSortOrder = pKeyInfo->aSortOrder;
564 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000565 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000566 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
567 }
drh66a51672008-01-03 00:01:23 +0000568 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000569 }else{
drh17435752007-08-16 04:30:38 +0000570 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000571 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000572 }
drh66a51672008-01-03 00:01:23 +0000573 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000574 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000575 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000576 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000577 pOp->p4.p = (void*)zP4;
drh8df32842008-12-09 02:51:23 +0000578 pOp->p4type = (signed char)n;
drh9a324642003-09-06 20:12:01 +0000579 }else{
drhea678832008-12-10 19:26:22 +0000580 if( n==0 ) n = sqlite3Strlen30(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000581 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000582 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000583 }
584}
585
drhad6d9462004-09-19 02:15:24 +0000586#ifndef NDEBUG
587/*
drh16ee60f2008-06-20 18:13:25 +0000588** Change the comment on the the most recently coded instruction. Or
589** insert a No-op and add the comment to that new instruction. This
590** makes the code easier to read during debugging. None of this happens
591** in a production build.
drhad6d9462004-09-19 02:15:24 +0000592*/
593void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
594 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000595 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000596 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000597 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000598 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000599 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000600 sqlite3DbFree(p->db, *pz);
drh8cc74322008-01-15 02:22:24 +0000601 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000602 va_end(ap);
603 }
drhad6d9462004-09-19 02:15:24 +0000604}
drh16ee60f2008-06-20 18:13:25 +0000605void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
606 va_list ap;
607 sqlite3VdbeAddOp0(p, OP_Noop);
608 assert( p->nOp>0 || p->aOp==0 );
609 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
610 if( p->nOp ){
611 char **pz = &p->aOp[p->nOp-1].zComment;
612 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000613 sqlite3DbFree(p->db, *pz);
drh16ee60f2008-06-20 18:13:25 +0000614 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
615 va_end(ap);
616 }
617}
618#endif /* NDEBUG */
drhad6d9462004-09-19 02:15:24 +0000619
drh9a324642003-09-06 20:12:01 +0000620/*
drh20411ea2009-05-29 19:00:12 +0000621** Return the opcode for a given address. If the address is -1, then
622** return the most recently inserted opcode.
623**
624** If a memory allocation error has occurred prior to the calling of this
625** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
626** is readable and writable, but it has no effect. The return of a dummy
627** opcode allows the call to continue functioning after a OOM fault without
628** having to check to see if the return from this routine is a valid pointer.
drh9a324642003-09-06 20:12:01 +0000629*/
danielk19774adee202004-05-08 08:23:19 +0000630VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh20411ea2009-05-29 19:00:12 +0000631 static VdbeOp dummy;
drh9a324642003-09-06 20:12:01 +0000632 assert( p->magic==VDBE_MAGIC_INIT );
drh20411ea2009-05-29 19:00:12 +0000633 if( addr<0 ) addr = p->nOp - 1;
drh17435752007-08-16 04:30:38 +0000634 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +0000635 if( p->db->mallocFailed ){
636 return &dummy;
637 }else{
638 return &p->aOp[addr];
639 }
drh9a324642003-09-06 20:12:01 +0000640}
641
drhb7f91642004-10-31 02:22:47 +0000642#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
643 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000644/*
drh66a51672008-01-03 00:01:23 +0000645** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000646** Use zTemp for any required temporary buffer space.
647*/
drh66a51672008-01-03 00:01:23 +0000648static char *displayP4(Op *pOp, char *zTemp, int nTemp){
649 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000650 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000651 switch( pOp->p4type ){
drh16ee60f2008-06-20 18:13:25 +0000652 case P4_KEYINFO_STATIC:
drh66a51672008-01-03 00:01:23 +0000653 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000654 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000655 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000656 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhea678832008-12-10 19:26:22 +0000657 i = sqlite3Strlen30(zTemp);
drhd3d39e92004-05-20 22:16:29 +0000658 for(j=0; j<pKeyInfo->nField; j++){
659 CollSeq *pColl = pKeyInfo->aColl[j];
660 if( pColl ){
drhea678832008-12-10 19:26:22 +0000661 int n = sqlite3Strlen30(pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000662 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000663 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000664 break;
665 }
666 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000667 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000668 zTemp[i++] = '-';
669 }
drh5bb3eb92007-05-04 13:15:55 +0000670 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000671 i += n;
672 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000673 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000674 i += 4;
675 }
676 }
677 zTemp[i++] = ')';
678 zTemp[i] = 0;
679 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000680 break;
681 }
drh66a51672008-01-03 00:01:23 +0000682 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000683 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000684 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000685 break;
686 }
drh66a51672008-01-03 00:01:23 +0000687 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000688 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000689 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000690 break;
691 }
drh66a51672008-01-03 00:01:23 +0000692 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000693 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000694 break;
695 }
drh66a51672008-01-03 00:01:23 +0000696 case P4_INT32: {
697 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000698 break;
699 }
drh66a51672008-01-03 00:01:23 +0000700 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000701 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000702 break;
703 }
drh66a51672008-01-03 00:01:23 +0000704 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000705 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000706 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000707 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000708 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000709 }else if( pMem->flags & MEM_Int ){
710 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
711 }else if( pMem->flags & MEM_Real ){
712 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhd4e70eb2008-01-02 00:34:36 +0000713 }
drh598f1342007-10-23 15:39:45 +0000714 break;
715 }
drha967e882006-06-13 01:04:52 +0000716#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000717 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000718 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000719 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000720 break;
721 }
722#endif
drh0acb7e42008-06-25 00:12:41 +0000723 case P4_INTARRAY: {
724 sqlite3_snprintf(nTemp, zTemp, "intarray");
725 break;
726 }
drhd3d39e92004-05-20 22:16:29 +0000727 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000728 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000729 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000730 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000731 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000732 }
733 }
734 }
drh66a51672008-01-03 00:01:23 +0000735 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000736 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000737}
drhb7f91642004-10-31 02:22:47 +0000738#endif
drhd3d39e92004-05-20 22:16:29 +0000739
drh900b31e2007-08-28 02:27:51 +0000740/*
drhd0679ed2007-08-28 22:24:34 +0000741** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
742**
drh900b31e2007-08-28 02:27:51 +0000743*/
drhfb982642007-08-30 01:19:59 +0000744void sqlite3VdbeUsesBtree(Vdbe *p, int i){
745 int mask;
drh3500ed62009-05-05 15:46:43 +0000746 assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
danielk197700e13612008-11-17 19:18:54 +0000747 assert( i<(int)sizeof(p->btreeMask)*8 );
drh3500ed62009-05-05 15:46:43 +0000748 mask = ((u32)1)<<i;
drhfb982642007-08-30 01:19:59 +0000749 if( (p->btreeMask & mask)==0 ){
750 p->btreeMask |= mask;
751 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
752 }
drh900b31e2007-08-28 02:27:51 +0000753}
754
drhd3d39e92004-05-20 22:16:29 +0000755
danielk19778b60e0f2005-01-12 09:10:39 +0000756#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000757/*
758** Print a single opcode. This routine is used for debugging only.
759*/
danielk19774adee202004-05-08 08:23:19 +0000760void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000761 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000762 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000763 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000764 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000765 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000766 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000767 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
768#ifdef SQLITE_DEBUG
769 pOp->zComment ? pOp->zComment : ""
770#else
771 ""
772#endif
773 );
drh9a324642003-09-06 20:12:01 +0000774 fflush(pOut);
775}
776#endif
777
778/*
drh76ff3a02004-09-24 22:32:30 +0000779** Release an array of N Mem elements
780*/
drhc890fec2008-08-01 20:10:08 +0000781static void releaseMemArray(Mem *p, int N){
danielk1977a7a8e142008-02-13 18:25:27 +0000782 if( p && N ){
danielk1977e972e032008-09-19 18:32:26 +0000783 Mem *pEnd;
danielk1977a7a8e142008-02-13 18:25:27 +0000784 sqlite3 *db = p->db;
drh8df32842008-12-09 02:51:23 +0000785 u8 malloc_failed = db->mallocFailed;
danielk1977e972e032008-09-19 18:32:26 +0000786 for(pEnd=&p[N]; p<pEnd; p++){
787 assert( (&p[1])==pEnd || p[0].db==p[1].db );
788
789 /* This block is really an inlined version of sqlite3VdbeMemRelease()
790 ** that takes advantage of the fact that the memory cell value is
791 ** being set to NULL after releasing any dynamic resources.
792 **
793 ** The justification for duplicating code is that according to
794 ** callgrind, this causes a certain test case to hit the CPU 4.7
795 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
796 ** sqlite3MemRelease() were called from here. With -O2, this jumps
797 ** to 6.6 percent. The test case is inserting 1000 rows into a table
798 ** with no indexes using a single prepared INSERT statement, bind()
799 ** and reset(). Inserts are grouped into a transaction.
800 */
801 if( p->flags&(MEM_Agg|MEM_Dyn) ){
802 sqlite3VdbeMemRelease(p);
803 }else if( p->zMalloc ){
804 sqlite3DbFree(db, p->zMalloc);
805 p->zMalloc = 0;
806 }
807
danielk19775f096132008-03-28 15:44:09 +0000808 p->flags = MEM_Null;
drh76ff3a02004-09-24 22:32:30 +0000809 }
danielk1977a7a8e142008-02-13 18:25:27 +0000810 db->mallocFailed = malloc_failed;
drh76ff3a02004-09-24 22:32:30 +0000811 }
812}
813
danielk1977dfb316d2008-03-26 18:34:43 +0000814#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
815int sqlite3VdbeReleaseBuffers(Vdbe *p){
816 int ii;
817 int nFree = 0;
818 assert( sqlite3_mutex_held(p->db->mutex) );
819 for(ii=1; ii<=p->nMem; ii++){
820 Mem *pMem = &p->aMem[ii];
drh3d4501e2008-12-04 20:40:10 +0000821 if( pMem->flags & MEM_RowSet ){
822 sqlite3RowSetClear(pMem->u.pRowSet);
823 }
danielk1977dfb316d2008-03-26 18:34:43 +0000824 if( pMem->z && pMem->flags&MEM_Dyn ){
825 assert( !pMem->xDel );
drh633e6d52008-07-28 19:34:53 +0000826 nFree += sqlite3DbMallocSize(pMem->db, pMem->z);
danielk1977dfb316d2008-03-26 18:34:43 +0000827 sqlite3VdbeMemRelease(pMem);
828 }
829 }
830 return nFree;
831}
832#endif
833
drhb7f91642004-10-31 02:22:47 +0000834#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000835/*
drh9a324642003-09-06 20:12:01 +0000836** Give a listing of the program in the virtual machine.
837**
danielk19774adee202004-05-08 08:23:19 +0000838** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000839** running the code, it invokes the callback once for each instruction.
840** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +0000841**
842** When p->explain==1, each instruction is listed. When
843** p->explain==2, only OP_Explain instructions are listed and these
844** are shown in a different format. p->explain==2 is used to implement
845** EXPLAIN QUERY PLAN.
drh9a324642003-09-06 20:12:01 +0000846*/
danielk19774adee202004-05-08 08:23:19 +0000847int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000848 Vdbe *p /* The VDBE */
849){
drh9bb575f2004-09-06 17:24:11 +0000850 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000851 int i;
drh826fb5a2004-02-14 23:59:57 +0000852 int rc = SQLITE_OK;
drh9cbf3422008-01-17 16:22:13 +0000853 Mem *pMem = p->pResultSet = &p->aMem[1];
drh9a324642003-09-06 20:12:01 +0000854
drh9a324642003-09-06 20:12:01 +0000855 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000856 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
857 assert( db->magic==SQLITE_MAGIC_BUSY );
danielk19776c359f02008-11-21 16:58:03 +0000858 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
danielk197718f41892004-05-22 07:27:46 +0000859
drh9cbf3422008-01-17 16:22:13 +0000860 /* Even though this opcode does not use dynamic strings for
861 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000862 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000863 */
drhc890fec2008-08-01 20:10:08 +0000864 releaseMemArray(pMem, p->nMem);
danielk197718f41892004-05-22 07:27:46 +0000865
danielk19776c359f02008-11-21 16:58:03 +0000866 if( p->rc==SQLITE_NOMEM ){
867 /* This happens if a malloc() inside a call to sqlite3_column_text() or
868 ** sqlite3_column_text16() failed. */
869 db->mallocFailed = 1;
870 return SQLITE_ERROR;
871 }
872
drhecc92422005-09-10 16:46:12 +0000873 do{
874 i = p->pc++;
875 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000876 if( i>=p->nOp ){
877 p->rc = SQLITE_OK;
878 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000879 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000880 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000881 rc = SQLITE_ERROR;
drhf089aa42008-07-08 19:34:06 +0000882 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
drh826fb5a2004-02-14 23:59:57 +0000883 }else{
danielk1977a7a8e142008-02-13 18:25:27 +0000884 char *z;
drhd3d39e92004-05-20 22:16:29 +0000885 Op *pOp = &p->aOp[i];
danielk19770d78bae2008-01-03 07:09:48 +0000886 if( p->explain==1 ){
887 pMem->flags = MEM_Int;
888 pMem->type = SQLITE_INTEGER;
889 pMem->u.i = i; /* Program counter */
890 pMem++;
891
892 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
893 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
894 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +0000895 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +0000896 pMem->type = SQLITE_TEXT;
897 pMem->enc = SQLITE_UTF8;
898 pMem++;
899 }
drheb2e1762004-05-27 01:53:56 +0000900
901 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000902 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000903 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000904 pMem++;
905
906 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000907 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000908 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000909 pMem++;
910
danielk19770d78bae2008-01-03 07:09:48 +0000911 if( p->explain==1 ){
912 pMem->flags = MEM_Int;
913 pMem->u.i = pOp->p3; /* P3 */
914 pMem->type = SQLITE_INTEGER;
915 pMem++;
916 }
917
danielk1977a7a8e142008-02-13 18:25:27 +0000918 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
danielk1977357864e2009-03-25 15:43:08 +0000919 assert( p->db->mallocFailed );
920 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +0000921 }
922 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
923 z = displayP4(pOp, pMem->z, 32);
924 if( z!=pMem->z ){
925 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
926 }else{
927 assert( pMem->z!=0 );
drhea678832008-12-10 19:26:22 +0000928 pMem->n = sqlite3Strlen30(pMem->z);
danielk1977a7a8e142008-02-13 18:25:27 +0000929 pMem->enc = SQLITE_UTF8;
930 }
drh9c054832004-05-31 18:51:57 +0000931 pMem->type = SQLITE_TEXT;
danielk19770d78bae2008-01-03 07:09:48 +0000932 pMem++;
drheb2e1762004-05-27 01:53:56 +0000933
danielk19770d78bae2008-01-03 07:09:48 +0000934 if( p->explain==1 ){
drh85e5f0d2008-02-19 18:28:13 +0000935 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
danielk1977357864e2009-03-25 15:43:08 +0000936 assert( p->db->mallocFailed );
937 return SQLITE_ERROR;
danielk1977a7a8e142008-02-13 18:25:27 +0000938 }
939 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
drh85e5f0d2008-02-19 18:28:13 +0000940 pMem->n = 2;
941 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
danielk19770d78bae2008-01-03 07:09:48 +0000942 pMem->type = SQLITE_TEXT;
943 pMem->enc = SQLITE_UTF8;
944 pMem++;
945
drhaa9b8962008-01-08 02:57:55 +0000946#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000947 if( pOp->zComment ){
948 pMem->flags = MEM_Str|MEM_Term;
949 pMem->z = pOp->zComment;
drhea678832008-12-10 19:26:22 +0000950 pMem->n = sqlite3Strlen30(pMem->z);
danielk19770d78bae2008-01-03 07:09:48 +0000951 pMem->enc = SQLITE_UTF8;
danielk19771e522b42008-09-16 09:09:19 +0000952 pMem->type = SQLITE_TEXT;
drh52391cb2008-02-14 23:44:13 +0000953 }else
drhaa9b8962008-01-08 02:57:55 +0000954#endif
drh52391cb2008-02-14 23:44:13 +0000955 {
956 pMem->flags = MEM_Null; /* Comment */
957 pMem->type = SQLITE_NULL;
958 }
danielk19770d78bae2008-01-03 07:09:48 +0000959 }
960
961 p->nResColumn = 8 - 5*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +0000962 p->rc = SQLITE_OK;
963 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000964 }
drh826fb5a2004-02-14 23:59:57 +0000965 return rc;
drh9a324642003-09-06 20:12:01 +0000966}
drhb7f91642004-10-31 02:22:47 +0000967#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000968
drh7c4ac0c2007-04-05 11:25:58 +0000969#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000970/*
drh3f7d4e42004-07-24 14:35:58 +0000971** Print the SQL that was used to generate a VDBE program.
972*/
973void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000974 int nOp = p->nOp;
975 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000976 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000977 pOp = &p->aOp[0];
978 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000979 const char *z = pOp->p4.z;
danielk197778ca0e72009-01-20 16:53:39 +0000980 while( sqlite3Isspace(*z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000981 printf("SQL: [%s]\n", z);
982 }
drh3f7d4e42004-07-24 14:35:58 +0000983}
drh7c4ac0c2007-04-05 11:25:58 +0000984#endif
drh3f7d4e42004-07-24 14:35:58 +0000985
drh602c2372007-03-01 00:29:13 +0000986#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
987/*
988** Print an IOTRACE message showing SQL content.
989*/
990void sqlite3VdbeIOTraceSql(Vdbe *p){
991 int nOp = p->nOp;
992 VdbeOp *pOp;
mlcreech3a00f902008-03-04 17:45:01 +0000993 if( sqlite3IoTrace==0 ) return;
drh602c2372007-03-01 00:29:13 +0000994 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000995 pOp = &p->aOp[0];
996 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +0000997 int i, j;
drh00a18e42007-08-13 11:10:34 +0000998 char z[1000];
drh949f9cd2008-01-12 21:35:57 +0000999 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk197778ca0e72009-01-20 16:53:39 +00001000 for(i=0; sqlite3Isspace(z[i]); i++){}
drh602c2372007-03-01 00:29:13 +00001001 for(j=0; z[i]; i++){
danielk197778ca0e72009-01-20 16:53:39 +00001002 if( sqlite3Isspace(z[i]) ){
drh602c2372007-03-01 00:29:13 +00001003 if( z[i-1]!=' ' ){
1004 z[j++] = ' ';
1005 }
1006 }else{
1007 z[j++] = z[i];
1008 }
1009 }
1010 z[j] = 0;
mlcreech3a00f902008-03-04 17:45:01 +00001011 sqlite3IoTrace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +00001012 }
1013}
1014#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1015
drhb2771ce2009-02-20 01:28:59 +00001016/*
1017** Allocate space from a fixed size buffer. Make *pp point to the
1018** allocated space. (Note: pp is a char* rather than a void** to
1019** work around the pointer aliasing rules of C.) *pp should initially
1020** be zero. If *pp is not zero, that means that the space has already
1021** been allocated and this routine is a noop.
1022**
1023** nByte is the number of bytes of space needed.
1024**
1025** *ppFrom point to available space and pEnd points to the end of the
1026** available space.
1027**
1028** *pnByte is a counter of the number of bytes of space that have failed
1029** to allocate. If there is insufficient space in *ppFrom to satisfy the
danielk1977d336e222009-02-20 10:58:41 +00001030** request, then increment *pnByte by the amount of the request.
drhb2771ce2009-02-20 01:28:59 +00001031*/
1032static void allocSpace(
1033 char *pp, /* IN/OUT: Set *pp to point to allocated buffer */
1034 int nByte, /* Number of bytes to allocate */
1035 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
danielk1977d336e222009-02-20 10:58:41 +00001036 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
drhb2771ce2009-02-20 01:28:59 +00001037 int *pnByte /* If allocation cannot be made, increment *pnByte */
1038){
drhea598cb2009-04-05 12:22:08 +00001039 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
drhb2771ce2009-02-20 01:28:59 +00001040 if( (*(void**)pp)==0 ){
danielk1977bc739712009-03-23 04:33:32 +00001041 nByte = ROUND8(nByte);
drhb2771ce2009-02-20 01:28:59 +00001042 if( (pEnd - *ppFrom)>=nByte ){
1043 *(void**)pp = (void *)*ppFrom;
1044 *ppFrom += nByte;
1045 }else{
1046 *pnByte += nByte;
1047 }
1048 }
1049}
drh602c2372007-03-01 00:29:13 +00001050
drh3f7d4e42004-07-24 14:35:58 +00001051/*
drh9a324642003-09-06 20:12:01 +00001052** Prepare a virtual machine for execution. This involves things such
1053** as allocating stack space and initializing the program counter.
1054** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +00001055** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +00001056**
1057** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
1058** VDBE_MAGIC_RUN.
danielk19776ab3a2e2009-02-19 14:39:25 +00001059**
1060** This function may be called more than once on a single virtual machine.
1061** The first call is made while compiling the SQL statement. Subsequent
1062** calls are made as part of the process of resetting a statement to be
1063** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor
1064** and isExplain parameters are only passed correct values the first time
1065** the function is called. On subsequent calls, from sqlite3_reset(), nVar
1066** is passed -1 and nMem, nCursor and isExplain are all passed zero.
drh9a324642003-09-06 20:12:01 +00001067*/
danielk19774adee202004-05-08 08:23:19 +00001068void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +00001069 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +00001070 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +00001071 int nMem, /* Number of memory cells to allocate */
1072 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +00001073 int isExplain /* True if the EXPLAIN keywords is present */
1074){
1075 int n;
danielk19771e536952007-08-16 10:09:01 +00001076 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001077
1078 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +00001079 assert( p->magic==VDBE_MAGIC_INIT );
1080
drhc16a03b2004-09-15 13:38:10 +00001081 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +00001082 */
drhc16a03b2004-09-15 13:38:10 +00001083 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +00001084
danielk197700e13612008-11-17 19:18:54 +00001085 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
danielk1977634f2982005-03-28 08:44:07 +00001086 p->magic = VDBE_MAGIC_RUN;
1087
danielk1977cd3e8f72008-03-25 09:47:35 +00001088 /* For each cursor required, also allocate a memory cell. Memory
1089 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1090 ** the vdbe program. Instead they are used to allocate space for
drhdfe88ec2008-11-03 20:55:06 +00001091 ** VdbeCursor/BtCursor structures. The blob of memory associated with
danielk1977cd3e8f72008-03-25 09:47:35 +00001092 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1093 ** stores the blob of memory associated with cursor 1, etc.
1094 **
1095 ** See also: allocateCursor().
1096 */
1097 nMem += nCursor;
1098
danielk19776ab3a2e2009-02-19 14:39:25 +00001099 /* Allocate space for memory registers, SQL variables, VDBE cursors and
1100 ** an array to marshal SQL function arguments in. This is only done the
1101 ** first time this function is called for a given VDBE, not when it is
1102 ** being called from sqlite3_reset() to reset the virtual machine.
drh9a324642003-09-06 20:12:01 +00001103 */
drhb2771ce2009-02-20 01:28:59 +00001104 if( nVar>=0 && !db->mallocFailed ){
1105 u8 *zCsr = (u8 *)&p->aOp[p->nOp];
1106 u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc];
danielk19776ab3a2e2009-02-19 14:39:25 +00001107 int nByte;
danielk1977634f2982005-03-28 08:44:07 +00001108 int nArg; /* Maximum number of args passed to a user function. */
drh9cbf3422008-01-17 16:22:13 +00001109 resolveP2Values(p, &nArg);
drh9cbf3422008-01-17 16:22:13 +00001110 if( isExplain && nMem<10 ){
drhc46f5202008-11-04 14:25:06 +00001111 nMem = 10;
drh0f7eb612006-08-08 13:51:43 +00001112 }
drhea598cb2009-04-05 12:22:08 +00001113 zCsr += (zCsr - (u8*)0)&7;
1114 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
drh3c6d9a42009-04-06 11:11:42 +00001115 if( zEnd<zCsr ) zEnd = zCsr;
drhb2771ce2009-02-20 01:28:59 +00001116
1117 do {
1118 memset(zCsr, 0, zEnd-zCsr);
1119 nByte = 0;
1120 allocSpace((char*)&p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1121 allocSpace((char*)&p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1122 allocSpace((char*)&p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1123 allocSpace((char*)&p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1124 allocSpace((char*)&p->apCsr,
1125 nCursor*sizeof(VdbeCursor*), &zCsr, zEnd, &nByte
1126 );
1127 if( nByte ){
1128 p->pFree = sqlite3DbMallocRaw(db, nByte);
1129 }
1130 zCsr = p->pFree;
1131 zEnd = &zCsr[nByte];
1132 }while( nByte && !db->mallocFailed );
1133
1134 p->nCursor = nCursor;
1135 if( p->aVar ){
drh86f43302004-10-05 17:37:36 +00001136 p->nVar = nVar;
drh290c1942004-08-21 17:54:45 +00001137 for(n=0; n<nVar; n++){
1138 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001139 p->aVar[n].db = db;
1140 }
drhb2771ce2009-02-20 01:28:59 +00001141 }
1142 if( p->aMem ){
1143 p->aMem--; /* aMem[] goes from 1..nMem */
1144 p->nMem = nMem; /* not from 0..nMem-1 */
drh9cbf3422008-01-17 16:22:13 +00001145 for(n=1; n<=nMem; n++){
1146 p->aMem[n].flags = MEM_Null;
1147 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +00001148 }
danielk197754db47e2004-05-19 10:36:43 +00001149 }
drh82a48512003-09-06 22:45:20 +00001150 }
drh9cbf3422008-01-17 16:22:13 +00001151#ifdef SQLITE_DEBUG
1152 for(n=1; n<p->nMem; n++){
1153 assert( p->aMem[n].db==db );
danielk1977b3bce662005-01-29 08:32:43 +00001154 }
drh9cbf3422008-01-17 16:22:13 +00001155#endif
drh9a324642003-09-06 20:12:01 +00001156
danielk19771d850a72004-05-31 08:26:49 +00001157 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001158 p->rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001159 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001160 p->explain |= isExplain;
1161 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001162 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001163 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001164 p->minWriteFileFormat = 255;
danielk1977bd434552009-03-18 10:33:00 +00001165 p->iStatement = 0;
drh9a324642003-09-06 20:12:01 +00001166#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001167 {
1168 int i;
1169 for(i=0; i<p->nOp; i++){
1170 p->aOp[i].cnt = 0;
1171 p->aOp[i].cycles = 0;
1172 }
drh9a324642003-09-06 20:12:01 +00001173 }
1174#endif
1175}
1176
drh9a324642003-09-06 20:12:01 +00001177/*
danielk1977cd3e8f72008-03-25 09:47:35 +00001178** Close a VDBE cursor and release all the resources that cursor
1179** happens to hold.
drh9a324642003-09-06 20:12:01 +00001180*/
drhdfe88ec2008-11-03 20:55:06 +00001181void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
drh4774b132004-06-12 20:12:51 +00001182 if( pCx==0 ){
1183 return;
1184 }
drh9a324642003-09-06 20:12:01 +00001185 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3BtreeClose(pCx->pBt);
drh34004ce2008-07-11 16:15:17 +00001187 /* The pCx->pCursor will be close automatically, if it exists, by
1188 ** the call above. */
1189 }else if( pCx->pCursor ){
1190 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001191 }
drh9eff6162006-06-12 21:59:13 +00001192#ifndef SQLITE_OMIT_VIRTUALTABLE
1193 if( pCx->pVtabCursor ){
1194 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001195 const sqlite3_module *pModule = pCx->pModule;
1196 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001197 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001198 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001199 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001200 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001201 }
1202#endif
danielk19779882d992008-03-27 17:59:01 +00001203 if( !pCx->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00001204 sqlite3DbFree(p->db, pCx->pData);
danielk19779882d992008-03-27 17:59:01 +00001205 }
drh9a324642003-09-06 20:12:01 +00001206}
1207
1208/*
drhff0587c2007-08-29 17:43:19 +00001209** Close all cursors except for VTab cursors that are currently
1210** in use.
drh9a324642003-09-06 20:12:01 +00001211*/
drhff0587c2007-08-29 17:43:19 +00001212static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001213 int i;
drh290c1942004-08-21 17:54:45 +00001214 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001215 for(i=0; i<p->nCursor; i++){
drhdfe88ec2008-11-03 20:55:06 +00001216 VdbeCursor *pC = p->apCsr[i];
drhff0587c2007-08-29 17:43:19 +00001217 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1218 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001219 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001220 }
drh9a324642003-09-06 20:12:01 +00001221 }
drh9a324642003-09-06 20:12:01 +00001222}
1223
1224/*
drh9a324642003-09-06 20:12:01 +00001225** Clean up the VM after execution.
1226**
1227** This routine will automatically close any cursors, lists, and/or
1228** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001229** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001230*/
drhc890fec2008-08-01 20:10:08 +00001231static void Cleanup(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001232 int i;
drh633e6d52008-07-28 19:34:53 +00001233 sqlite3 *db = p->db;
drh3d4501e2008-12-04 20:40:10 +00001234 Mem *pMem;
drhff0587c2007-08-29 17:43:19 +00001235 closeAllCursorsExceptActiveVtabs(p);
drh3d4501e2008-12-04 20:40:10 +00001236 for(pMem=&p->aMem[1], i=1; i<=p->nMem; i++, pMem++){
1237 if( pMem->flags & MEM_RowSet ){
1238 sqlite3RowSetClear(pMem->u.pRowSet);
1239 }
1240 MemSetTypeFlag(pMem, MEM_Null);
danielk1977a7a8e142008-02-13 18:25:27 +00001241 }
drhc890fec2008-08-01 20:10:08 +00001242 releaseMemArray(&p->aMem[1], p->nMem);
drh76ff3a02004-09-24 22:32:30 +00001243 if( p->contextStack ){
drh633e6d52008-07-28 19:34:53 +00001244 sqlite3DbFree(db, p->contextStack);
drh344737f2004-09-19 00:50:20 +00001245 }
drh5f968432004-02-21 19:02:30 +00001246 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001247 p->contextStackDepth = 0;
1248 p->contextStackTop = 0;
drh633e6d52008-07-28 19:34:53 +00001249 sqlite3DbFree(db, p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001250 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001251 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001252}
1253
1254/*
danielk197722322fd2004-05-25 23:35:17 +00001255** Set the number of result columns that will be returned by this SQL
1256** statement. This is now set at compile time, rather than during
1257** execution of the vdbe program so that sqlite3_column_count() can
1258** be called on an SQL statement before sqlite3_step().
1259*/
1260void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001261 Mem *pColName;
1262 int n;
drh633e6d52008-07-28 19:34:53 +00001263 sqlite3 *db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001264
drhc890fec2008-08-01 20:10:08 +00001265 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001266 sqlite3DbFree(db, p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001267 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001268 p->nResColumn = nResColumn;
drh633e6d52008-07-28 19:34:53 +00001269 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001270 if( p->aColName==0 ) return;
1271 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001272 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001273 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001274 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001275 }
danielk197722322fd2004-05-25 23:35:17 +00001276}
1277
1278/*
danielk19773cf86062004-05-26 10:11:05 +00001279** Set the name of the idx'th column to be returned by the SQL statement.
1280** zName must be a pointer to a nul terminated string.
1281**
1282** This call must be made after a call to sqlite3VdbeSetNumCols().
1283**
danielk197710fb7492008-10-31 10:53:22 +00001284** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1285** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1286** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
danielk19773cf86062004-05-26 10:11:05 +00001287*/
danielk197710fb7492008-10-31 10:53:22 +00001288int sqlite3VdbeSetColName(
1289 Vdbe *p, /* Vdbe being configured */
1290 int idx, /* Index of column zName applies to */
1291 int var, /* One of the COLNAME_* constants */
1292 const char *zName, /* Pointer to buffer containing name */
1293 void (*xDel)(void*) /* Memory management strategy for zName */
1294){
danielk19773cf86062004-05-26 10:11:05 +00001295 int rc;
1296 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001297 assert( idx<p->nResColumn );
1298 assert( var<COLNAME_N );
danielk197710fb7492008-10-31 10:53:22 +00001299 if( p->db->mallocFailed ){
1300 assert( !zName || xDel!=SQLITE_DYNAMIC );
1301 return SQLITE_NOMEM;
1302 }
drh76ff3a02004-09-24 22:32:30 +00001303 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001304 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk197710fb7492008-10-31 10:53:22 +00001305 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
drh0793f1b2008-11-05 17:41:19 +00001306 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001307 return rc;
1308}
1309
danielk197713adf8a2004-06-03 16:08:41 +00001310/*
1311** A read or write transaction may or may not be active on database handle
1312** db. If a transaction is active, commit it. If there is a
1313** write-transaction spanning more than one database file, this routine
1314** takes care of the master journal trickery.
1315*/
danielk19773e3a84d2008-08-01 17:37:40 +00001316static int vdbeCommit(sqlite3 *db, Vdbe *p){
danielk197713adf8a2004-06-03 16:08:41 +00001317 int i;
1318 int nTrans = 0; /* Number of databases with an active write-transaction */
1319 int rc = SQLITE_OK;
1320 int needXcommit = 0;
1321
danielk19775bd270b2006-07-25 15:14:52 +00001322 /* Before doing anything else, call the xSync() callback for any
1323 ** virtual module tables written in this transaction. This has to
1324 ** be done before determining whether a master journal file is
1325 ** required, as an xSync() callback may add an attached database
1326 ** to the transaction.
1327 */
danielk19773e3a84d2008-08-01 17:37:40 +00001328 rc = sqlite3VtabSync(db, &p->zErrMsg);
danielk19775bd270b2006-07-25 15:14:52 +00001329 if( rc!=SQLITE_OK ){
1330 return rc;
1331 }
1332
1333 /* This loop determines (a) if the commit hook should be invoked and
1334 ** (b) how many database files have open write transactions, not
1335 ** including the temp database. (b) is important because if more than
1336 ** one database file has an open write transaction, a master journal
1337 ** file is required for an atomic commit.
1338 */
danielk197713adf8a2004-06-03 16:08:41 +00001339 for(i=0; i<db->nDb; i++){
1340 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001341 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001342 needXcommit = 1;
1343 if( i!=1 ) nTrans++;
1344 }
1345 }
1346
1347 /* If there are any write-transactions at all, invoke the commit hook */
1348 if( needXcommit && db->xCommitCallback ){
drh853799a2009-01-03 14:04:38 +00001349 assert( (db->flags & SQLITE_CommitBusy)==0 );
1350 db->flags |= SQLITE_CommitBusy;
drh7e8b8482008-01-23 03:03:05 +00001351 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001352 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001353 (void)sqlite3SafetyOn(db);
drh853799a2009-01-03 14:04:38 +00001354 db->flags &= ~SQLITE_CommitBusy;
drh92f02c32004-09-02 14:57:08 +00001355 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001356 return SQLITE_CONSTRAINT;
1357 }
1358 }
1359
danielk197740b38dc2004-06-26 08:38:24 +00001360 /* The simple case - no more than one database file (not counting the
1361 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001362 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001363 **
danielk197740b38dc2004-06-26 08:38:24 +00001364 ** If the return value of sqlite3BtreeGetFilename() is a zero length
danielk197717b90b52008-06-06 11:11:25 +00001365 ** string, it means the main database is :memory: or a temp file. In
1366 ** that case we do not support atomic multi-file commits, so use the
1367 ** simple case then too.
danielk197713adf8a2004-06-03 16:08:41 +00001368 */
drhea678832008-12-10 19:26:22 +00001369 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
1370 || nTrans<=1
1371 ){
danielk197704103022009-02-03 16:51:24 +00001372 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001373 Btree *pBt = db->aDb[i].pBt;
1374 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001375 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001376 }
1377 }
1378
drh80e35f42007-03-30 14:06:34 +00001379 /* Do the commit only if all databases successfully complete phase 1.
1380 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1381 ** IO error while deleting or truncating a journal file. It is unlikely,
1382 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001383 */
1384 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1385 Btree *pBt = db->aDb[i].pBt;
1386 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001387 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001388 }
danielk1977979f38e2007-03-27 16:19:51 +00001389 }
1390 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001391 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001392 }
1393 }
1394
1395 /* The complex case - There is a multi-file write-transaction active.
1396 ** This requires a master journal file to ensure the transaction is
1397 ** committed atomicly.
1398 */
danielk197744ee5bf2005-05-27 09:41:12 +00001399#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001400 else{
danielk1977b4b47412007-08-17 15:53:36 +00001401 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001402 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001403 char *zMaster = 0; /* File-name for the master journal */
1404 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001405 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001406 i64 offset = 0;
danielk1977861f7452008-06-05 11:39:11 +00001407 int res;
danielk197713adf8a2004-06-03 16:08:41 +00001408
1409 /* Select a master journal file name */
1410 do {
drhdc5ea5c2008-12-10 17:19:59 +00001411 u32 iRandom;
drh633e6d52008-07-28 19:34:53 +00001412 sqlite3DbFree(db, zMaster);
drhdc5ea5c2008-12-10 17:19:59 +00001413 sqlite3_randomness(sizeof(iRandom), &iRandom);
1414 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001415 if( !zMaster ){
1416 return SQLITE_NOMEM;
1417 }
danielk1977861f7452008-06-05 11:39:11 +00001418 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
1419 }while( rc==SQLITE_OK && res );
1420 if( rc==SQLITE_OK ){
drh19db9352008-03-27 22:42:51 +00001421 /* Open the master journal. */
1422 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1423 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1424 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1425 );
1426 }
danielk197713adf8a2004-06-03 16:08:41 +00001427 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001428 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001429 return rc;
1430 }
1431
1432 /* Write the name of each database file in the transaction into the new
1433 ** master journal file. If an error occurs at this point close
1434 ** and delete the master journal file. All the individual journal files
1435 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001436 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001437 */
danielk19771e536952007-08-16 10:09:01 +00001438 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001439 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001440 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001441 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001442 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001443 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001444 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1445 needSync = 1;
1446 }
drhea678832008-12-10 19:26:22 +00001447 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
1448 offset += sqlite3Strlen30(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001449 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001450 sqlite3OsCloseFree(pMaster);
1451 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001452 sqlite3DbFree(db, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001453 return rc;
1454 }
1455 }
1456 }
1457
danielk19779663b8f2007-08-24 11:52:28 +00001458 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1459 ** flag is set this is not required.
1460 */
danielk1977bea2a942009-01-20 17:06:27 +00001461 if( needSync
1462 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
1463 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
1464 ){
danielk1977fee2d252007-08-18 10:59:19 +00001465 sqlite3OsCloseFree(pMaster);
1466 sqlite3OsDelete(pVfs, zMaster, 0);
drh633e6d52008-07-28 19:34:53 +00001467 sqlite3DbFree(db, zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001468 return rc;
1469 }
drhc9e06862004-06-09 20:03:08 +00001470
danielk197713adf8a2004-06-03 16:08:41 +00001471 /* Sync all the db files involved in the transaction. The same call
1472 ** sets the master journal pointer in each individual journal. If
1473 ** an error occurs here, do not delete the master journal file.
1474 **
drh80e35f42007-03-30 14:06:34 +00001475 ** If the error occurs during the first call to
1476 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1477 ** master journal file will be orphaned. But we cannot delete it,
1478 ** in case the master journal file name was written into the journal
shanebe217792009-03-05 04:20:31 +00001479 ** file before the failure occurred.
danielk197713adf8a2004-06-03 16:08:41 +00001480 */
danielk19775bd270b2006-07-25 15:14:52 +00001481 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001482 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001483 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001484 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001485 }
1486 }
danielk1977fee2d252007-08-18 10:59:19 +00001487 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001488 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +00001489 sqlite3DbFree(db, zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001490 return rc;
1491 }
danielk197713adf8a2004-06-03 16:08:41 +00001492
danielk1977962398d2004-06-14 09:35:16 +00001493 /* Delete the master journal file. This commits the transaction. After
1494 ** doing this the directory is synced again before any individual
1495 ** transaction files are deleted.
1496 */
danielk1977fee2d252007-08-18 10:59:19 +00001497 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh633e6d52008-07-28 19:34:53 +00001498 sqlite3DbFree(db, zMaster);
drhc416ba92007-03-30 18:42:55 +00001499 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001500 if( rc ){
1501 return rc;
1502 }
danielk197713adf8a2004-06-03 16:08:41 +00001503
1504 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001505 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1506 ** deleting or truncating journals. If something goes wrong while
1507 ** this is happening we don't really care. The integrity of the
1508 ** transaction is already guaranteed, but some stray 'cold' journals
1509 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001510 */
danielk1977979f38e2007-03-27 16:19:51 +00001511 disable_simulated_io_errors();
danielk19772d1d86f2008-06-20 14:59:51 +00001512 sqlite3BeginBenignMalloc();
danielk197713adf8a2004-06-03 16:08:41 +00001513 for(i=0; i<db->nDb; i++){
1514 Btree *pBt = db->aDb[i].pBt;
1515 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001516 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001517 }
1518 }
danielk19772d1d86f2008-06-20 14:59:51 +00001519 sqlite3EndBenignMalloc();
danielk1977979f38e2007-03-27 16:19:51 +00001520 enable_simulated_io_errors();
1521
danielk1977f9e7dda2006-06-16 16:08:53 +00001522 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001523 }
danielk197744ee5bf2005-05-27 09:41:12 +00001524#endif
danielk1977026d2702004-06-14 13:14:59 +00001525
drh2ac3ee92004-06-07 16:27:46 +00001526 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001527}
1528
danielk19771d850a72004-05-31 08:26:49 +00001529/*
1530** This routine checks that the sqlite3.activeVdbeCnt count variable
1531** matches the number of vdbe's in the list sqlite3.pVdbe that are
1532** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001533** This is an internal self-check only - it is not an essential processing
1534** step.
danielk19771d850a72004-05-31 08:26:49 +00001535**
1536** This is a no-op if NDEBUG is defined.
1537*/
1538#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001539static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001540 Vdbe *p;
1541 int cnt = 0;
drhad4a4b82008-11-05 16:37:34 +00001542 int nWrite = 0;
danielk19771d850a72004-05-31 08:26:49 +00001543 p = db->pVdbe;
1544 while( p ){
drh92f02c32004-09-02 14:57:08 +00001545 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001546 cnt++;
drhad4a4b82008-11-05 16:37:34 +00001547 if( p->readOnly==0 ) nWrite++;
danielk19771d850a72004-05-31 08:26:49 +00001548 }
1549 p = p->pNext;
1550 }
danielk19771d850a72004-05-31 08:26:49 +00001551 assert( cnt==db->activeVdbeCnt );
drhad4a4b82008-11-05 16:37:34 +00001552 assert( nWrite==db->writeVdbeCnt );
danielk19771d850a72004-05-31 08:26:49 +00001553}
1554#else
1555#define checkActiveVdbeCnt(x)
1556#endif
1557
danielk19773cf86062004-05-26 10:11:05 +00001558/*
drhfb982642007-08-30 01:19:59 +00001559** For every Btree that in database connection db which
1560** has been modified, "trip" or invalidate each cursor in
1561** that Btree might have been modified so that the cursor
1562** can never be used again. This happens when a rollback
1563*** occurs. We have to trip all the other cursors, even
1564** cursor from other VMs in different database connections,
1565** so that none of them try to use the data at which they
1566** were pointing and which now may have been changed due
1567** to the rollback.
1568**
1569** Remember that a rollback can delete tables complete and
1570** reorder rootpages. So it is not sufficient just to save
1571** the state of the cursor. We have to invalidate the cursor
1572** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001573*/
drhade6c9c2007-11-24 10:23:44 +00001574static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001575 int i;
1576 for(i=0; i<db->nDb; i++){
1577 Btree *p = db->aDb[i].pBt;
1578 if( p && sqlite3BtreeIsInTrans(p) ){
1579 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1580 }
danielk1977be718892006-06-23 08:05:19 +00001581 }
1582}
1583
1584/*
danielk1977bd434552009-03-18 10:33:00 +00001585** If the Vdbe passed as the first argument opened a statement-transaction,
1586** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
1587** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
1588** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
1589** statement transaction is commtted.
1590**
1591** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
1592** Otherwise SQLITE_OK.
1593*/
1594int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
danielk1977c926b6a2009-03-20 14:42:11 +00001595 sqlite3 *const db = p->db;
danielk1977bd434552009-03-18 10:33:00 +00001596 int rc = SQLITE_OK;
danielk1977c926b6a2009-03-20 14:42:11 +00001597 if( p->iStatement && db->nStatement ){
danielk1977bd434552009-03-18 10:33:00 +00001598 int i;
1599 const int iSavepoint = p->iStatement-1;
danielk1977bd434552009-03-18 10:33:00 +00001600
1601 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
1602 assert( db->nStatement>0 );
1603 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
1604
1605 for(i=0; i<db->nDb; i++){
1606 int rc2 = SQLITE_OK;
1607 Btree *pBt = db->aDb[i].pBt;
1608 if( pBt ){
1609 if( eOp==SAVEPOINT_ROLLBACK ){
1610 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
1611 }
1612 if( rc2==SQLITE_OK ){
1613 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
1614 }
1615 if( rc==SQLITE_OK ){
1616 rc = rc2;
1617 }
1618 }
1619 }
1620 db->nStatement--;
1621 p->iStatement = 0;
1622 }
1623 return rc;
1624}
1625
1626/*
danielk1977f7590db2009-04-10 12:55:16 +00001627** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1628** this routine obtains the mutex associated with each BtShared structure
1629** that may be accessed by the VM passed as an argument. In doing so it
1630** sets the BtShared.db member of each of the BtShared structures, ensuring
1631** that the correct busy-handler callback is invoked if required.
1632**
1633** If SQLite is not threadsafe but does support shared-cache mode, then
1634** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
1635** of all of BtShared structures accessible via the database handle
1636** associated with the VM. Of course only a subset of these structures
1637** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
1638** that subset out, but there is no advantage to doing so.
1639**
1640** If SQLite is not threadsafe and does not support shared-cache mode, this
1641** function is a no-op.
1642*/
1643#ifndef SQLITE_OMIT_SHARED_CACHE
1644void sqlite3VdbeMutexArrayEnter(Vdbe *p){
1645#if SQLITE_THREADSAFE
1646 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1647#else
1648 sqlite3BtreeEnterAll(p->db);
1649#endif
1650}
1651#endif
1652
1653/*
drh92f02c32004-09-02 14:57:08 +00001654** This routine is called the when a VDBE tries to halt. If the VDBE
1655** has made changes and is in autocommit mode, then commit those
1656** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001657**
drh92f02c32004-09-02 14:57:08 +00001658** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001659** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1660** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001661**
1662** Return an error code. If the commit could not complete because of
1663** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1664** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001665*/
drhff0587c2007-08-29 17:43:19 +00001666int sqlite3VdbeHalt(Vdbe *p){
danielk1977bd434552009-03-18 10:33:00 +00001667 int rc; /* Used to store transient return codes */
drh9bb575f2004-09-06 17:24:11 +00001668 sqlite3 *db = p->db;
danielk197707cb5602006-01-20 10:55:05 +00001669
1670 /* This function contains the logic that determines if a statement or
1671 ** transaction will be committed or rolled back as a result of the
1672 ** execution of this virtual machine.
1673 **
drh71b890a2007-10-03 15:30:52 +00001674 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001675 **
drh71b890a2007-10-03 15:30:52 +00001676 ** SQLITE_NOMEM
1677 ** SQLITE_IOERR
1678 ** SQLITE_FULL
1679 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001680 **
drh71b890a2007-10-03 15:30:52 +00001681 ** Then the internal cache might have been left in an inconsistent
1682 ** state. We need to rollback the statement transaction, if there is
1683 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001684 */
drh9a324642003-09-06 20:12:01 +00001685
drh17435752007-08-16 04:30:38 +00001686 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001687 p->rc = SQLITE_NOMEM;
1688 }
drhff0587c2007-08-29 17:43:19 +00001689 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001690 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001691 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001692 }
danielk19771d850a72004-05-31 08:26:49 +00001693 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001694
danielk197707cb5602006-01-20 10:55:05 +00001695 /* No commit or rollback needed if the program never started */
1696 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001697 int mrc; /* Primary error code from p->rc */
danielk1977bd434552009-03-18 10:33:00 +00001698 int eStatementOp = 0;
1699 int isSpecialError; /* Set to true if a 'special' error */
drhff0587c2007-08-29 17:43:19 +00001700
1701 /* Lock all btrees used by the statement */
danielk1977f7590db2009-04-10 12:55:16 +00001702 sqlite3VdbeMutexArrayEnter(p);
drhff0587c2007-08-29 17:43:19 +00001703
drh71b890a2007-10-03 15:30:52 +00001704 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001705 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001706 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001707 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001708 if( isSpecialError ){
danielk197707cb5602006-01-20 10:55:05 +00001709 /* If the query was read-only, we need do no rollback at all. Otherwise,
1710 ** proceed with the special handling.
1711 */
drhad4a4b82008-11-05 16:37:34 +00001712 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
1713 if( p->rc==SQLITE_IOERR_BLOCKED && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00001714 eStatementOp = SAVEPOINT_ROLLBACK;
danielk1977e965ac72007-06-13 15:22:28 +00001715 p->rc = SQLITE_BUSY;
drhad4a4b82008-11-05 16:37:34 +00001716 }else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL)
1717 && p->usesStmtJournal ){
danielk1977bd434552009-03-18 10:33:00 +00001718 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00001719 }else{
1720 /* We are forced to roll back the active transaction. Before doing
1721 ** so, abort any other statements this handle currently has active.
1722 */
drhfb982642007-08-30 01:19:59 +00001723 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001724 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00001725 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00001726 db->autoCommit = 1;
1727 }
danielk1977261919c2005-12-06 12:52:59 +00001728 }
1729 }
danielk197707cb5602006-01-20 10:55:05 +00001730
danielk1977bd434552009-03-18 10:33:00 +00001731 /* If the auto-commit flag is set and this is the only active writer
1732 ** VM, then we do either a commit or rollback of the current transaction.
danielk197707cb5602006-01-20 10:55:05 +00001733 **
1734 ** Note: This block also runs if one of the special errors handled
drhad4a4b82008-11-05 16:37:34 +00001735 ** above has occurred.
danielk197707cb5602006-01-20 10:55:05 +00001736 */
danielk1977093e0f62008-11-13 18:00:14 +00001737 if( !sqlite3VtabInSync(db)
1738 && db->autoCommit
1739 && db->writeVdbeCnt==(p->readOnly==0)
drh853799a2009-01-03 14:04:38 +00001740 && (db->flags & SQLITE_CommitBusy)==0
danielk1977093e0f62008-11-13 18:00:14 +00001741 ){
danielk197707cb5602006-01-20 10:55:05 +00001742 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001743 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001744 ** successful or hit an 'OR FAIL' constraint. This means a commit
1745 ** is required.
1746 */
danielk1977bd434552009-03-18 10:33:00 +00001747 rc = vdbeCommit(db, p);
danielk197707cb5602006-01-20 10:55:05 +00001748 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001749 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001750 return SQLITE_BUSY;
1751 }else if( rc!=SQLITE_OK ){
1752 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001753 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001754 }else{
1755 sqlite3CommitInternalChanges(db);
1756 }
1757 }else{
danielk197797a227c2006-01-20 16:32:04 +00001758 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001759 }
danielk1977bd434552009-03-18 10:33:00 +00001760 db->nStatement = 0;
1761 }else if( eStatementOp==0 ){
danielk197707cb5602006-01-20 10:55:05 +00001762 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977bd434552009-03-18 10:33:00 +00001763 eStatementOp = SAVEPOINT_RELEASE;
danielk197707cb5602006-01-20 10:55:05 +00001764 }else if( p->errorAction==OE_Abort ){
danielk1977bd434552009-03-18 10:33:00 +00001765 eStatementOp = SAVEPOINT_ROLLBACK;
danielk197707cb5602006-01-20 10:55:05 +00001766 }else{
drhfb982642007-08-30 01:19:59 +00001767 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001768 sqlite3RollbackAll(db);
danielk1977fc158bf2009-01-07 08:12:16 +00001769 sqlite3CloseSavepoints(db);
danielk197707cb5602006-01-20 10:55:05 +00001770 db->autoCommit = 1;
1771 }
danielk19771d850a72004-05-31 08:26:49 +00001772 }
danielk197707cb5602006-01-20 10:55:05 +00001773
danielk1977bd434552009-03-18 10:33:00 +00001774 /* If eStatementOp is non-zero, then a statement transaction needs to
1775 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
1776 ** do so. If this operation returns an error, and the current statement
1777 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then set the error
1778 ** code to the new value.
danielk197707cb5602006-01-20 10:55:05 +00001779 */
danielk1977bd434552009-03-18 10:33:00 +00001780 if( eStatementOp ){
1781 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
1782 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1783 p->rc = rc;
1784 sqlite3DbFree(db, p->zErrMsg);
1785 p->zErrMsg = 0;
danielk197707cb5602006-01-20 10:55:05 +00001786 }
danielk197777d83ba2004-05-31 10:08:14 +00001787 }
danielk197707cb5602006-01-20 10:55:05 +00001788
danielk1977bd434552009-03-18 10:33:00 +00001789 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
1790 ** has been rolled back, update the database connection change-counter.
danielk197707cb5602006-01-20 10:55:05 +00001791 */
1792 if( p->changeCntOn && p->pc>=0 ){
danielk1977bd434552009-03-18 10:33:00 +00001793 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
danielk197707cb5602006-01-20 10:55:05 +00001794 sqlite3VdbeSetChanges(db, p->nChange);
1795 }else{
1796 sqlite3VdbeSetChanges(db, 0);
1797 }
1798 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001799 }
danielk197707cb5602006-01-20 10:55:05 +00001800
1801 /* Rollback or commit any schema changes that occurred. */
1802 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1803 sqlite3ResetInternalSchema(db, 0);
1804 db->flags = (db->flags | SQLITE_InternChanges);
1805 }
drhff0587c2007-08-29 17:43:19 +00001806
1807 /* Release the locks */
1808 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001809 }
danielk19771d850a72004-05-31 08:26:49 +00001810
danielk197765fd59f2006-06-24 11:51:33 +00001811 /* We have successfully halted and closed the VM. Record this fact. */
1812 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001813 db->activeVdbeCnt--;
drhad4a4b82008-11-05 16:37:34 +00001814 if( !p->readOnly ){
1815 db->writeVdbeCnt--;
1816 }
1817 assert( db->activeVdbeCnt>=db->writeVdbeCnt );
drh9a324642003-09-06 20:12:01 +00001818 }
drh92f02c32004-09-02 14:57:08 +00001819 p->magic = VDBE_MAGIC_HALT;
1820 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001821 if( p->db->mallocFailed ){
1822 p->rc = SQLITE_NOMEM;
1823 }
danielk19771d850a72004-05-31 08:26:49 +00001824
danielk1977404ca072009-03-16 13:19:36 +00001825 /* If the auto-commit flag is set to true, then any locks that were held
1826 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
1827 ** to invoke any required unlock-notify callbacks.
1828 */
1829 if( db->autoCommit ){
1830 sqlite3ConnectionUnlocked(db);
1831 }
1832
danielk1977bd434552009-03-18 10:33:00 +00001833 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
drh92f02c32004-09-02 14:57:08 +00001834 return SQLITE_OK;
1835}
drh4cf7c7f2007-08-28 23:28:07 +00001836
drh92f02c32004-09-02 14:57:08 +00001837
1838/*
drh3c23a882007-01-09 14:01:13 +00001839** Each VDBE holds the result of the most recent sqlite3_step() call
1840** in p->rc. This routine sets that result back to SQLITE_OK.
1841*/
1842void sqlite3VdbeResetStepResult(Vdbe *p){
1843 p->rc = SQLITE_OK;
1844}
1845
1846/*
drh92f02c32004-09-02 14:57:08 +00001847** Clean up a VDBE after execution but do not delete the VDBE just yet.
1848** Write any error messages into *pzErrMsg. Return the result code.
1849**
1850** After this routine is run, the VDBE should be ready to be executed
1851** again.
1852**
1853** To look at it another way, this routine resets the state of the
1854** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1855** VDBE_MAGIC_INIT.
1856*/
drhc890fec2008-08-01 20:10:08 +00001857int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001858 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001859 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001860
1861 /* If the VM did not run to completion or if it encountered an
1862 ** error, then it might not have been halted properly. So halt
1863 ** it now.
1864 */
drh7e8b8482008-01-23 03:03:05 +00001865 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001866 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001867 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001868
drhfb7e7652005-01-24 00:28:42 +00001869 /* If the VDBE has be run even partially, then transfer the error code
1870 ** and error message from the VDBE into the main database structure. But
1871 ** if the VDBE has just been set to run but has not actually executed any
1872 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001873 */
drhfb7e7652005-01-24 00:28:42 +00001874 if( p->pc>=0 ){
1875 if( p->zErrMsg ){
danielk19779ff3f3f2008-10-11 17:51:38 +00001876 sqlite3BeginBenignMalloc();
drh633e6d52008-07-28 19:34:53 +00001877 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19779ff3f3f2008-10-11 17:51:38 +00001878 sqlite3EndBenignMalloc();
danielk197797a227c2006-01-20 16:32:04 +00001879 db->errCode = p->rc;
drh633e6d52008-07-28 19:34:53 +00001880 sqlite3DbFree(db, p->zErrMsg);
drhfb7e7652005-01-24 00:28:42 +00001881 p->zErrMsg = 0;
1882 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001883 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001884 }else{
drh4ac285a2006-09-15 07:28:50 +00001885 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001886 }
danielk1977a21c6b62005-01-24 10:25:59 +00001887 }else if( p->rc && p->expired ){
1888 /* The expired flag was set on the VDBE before the first call
1889 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1890 ** called), set the database error in this case as well.
1891 */
drh4ac285a2006-09-15 07:28:50 +00001892 sqlite3Error(db, p->rc, 0);
drh633e6d52008-07-28 19:34:53 +00001893 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
1894 sqlite3DbFree(db, p->zErrMsg);
danielk19778e556522007-11-13 10:30:24 +00001895 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001896 }
1897
1898 /* Reclaim all memory used by the VDBE
1899 */
drhc890fec2008-08-01 20:10:08 +00001900 Cleanup(p);
drh92f02c32004-09-02 14:57:08 +00001901
1902 /* Save profiling information from this VDBE run.
1903 */
drh9a324642003-09-06 20:12:01 +00001904#ifdef VDBE_PROFILE
1905 {
1906 FILE *out = fopen("vdbe_profile.out", "a");
1907 if( out ){
1908 int i;
1909 fprintf(out, "---- ");
1910 for(i=0; i<p->nOp; i++){
1911 fprintf(out, "%02x", p->aOp[i].opcode);
1912 }
1913 fprintf(out, "\n");
1914 for(i=0; i<p->nOp; i++){
1915 fprintf(out, "%6d %10lld %8lld ",
1916 p->aOp[i].cnt,
1917 p->aOp[i].cycles,
1918 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1919 );
danielk19774adee202004-05-08 08:23:19 +00001920 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001921 }
1922 fclose(out);
1923 }
1924 }
1925#endif
1926 p->magic = VDBE_MAGIC_INIT;
drh4ac285a2006-09-15 07:28:50 +00001927 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001928}
drh92f02c32004-09-02 14:57:08 +00001929
drh9a324642003-09-06 20:12:01 +00001930/*
1931** Clean up and delete a VDBE after execution. Return an integer which is
1932** the result code. Write any error message text into *pzErrMsg.
1933*/
danielk19779e6db7d2004-06-21 08:18:51 +00001934int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001935 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001936 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
drhc890fec2008-08-01 20:10:08 +00001937 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001938 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001939 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001940 return SQLITE_MISUSE;
1941 }
danielk19774adee202004-05-08 08:23:19 +00001942 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001943 return rc;
1944}
1945
1946/*
drhf92c7ff2004-06-19 15:40:23 +00001947** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001948** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001949** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001950** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001951*/
1952void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1953 int i;
1954 for(i=0; i<pVdbeFunc->nAux; i++){
1955 struct AuxData *pAux = &pVdbeFunc->apAux[i];
drh3500ed62009-05-05 15:46:43 +00001956 if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
drhf92c7ff2004-06-19 15:40:23 +00001957 if( pAux->xDelete ){
1958 pAux->xDelete(pAux->pAux);
1959 }
1960 pAux->pAux = 0;
1961 }
1962 }
1963}
1964
1965/*
drh9a324642003-09-06 20:12:01 +00001966** Delete an entire VDBE.
1967*/
danielk19774adee202004-05-08 08:23:19 +00001968void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001969 int i;
drh633e6d52008-07-28 19:34:53 +00001970 sqlite3 *db;
1971
drh9a324642003-09-06 20:12:01 +00001972 if( p==0 ) return;
drh633e6d52008-07-28 19:34:53 +00001973 db = p->db;
drh9a324642003-09-06 20:12:01 +00001974 if( p->pPrev ){
1975 p->pPrev->pNext = p->pNext;
1976 }else{
drh633e6d52008-07-28 19:34:53 +00001977 assert( db->pVdbe==p );
1978 db->pVdbe = p->pNext;
drh9a324642003-09-06 20:12:01 +00001979 }
1980 if( p->pNext ){
1981 p->pNext->pPrev = p->pPrev;
1982 }
drh76ff3a02004-09-24 22:32:30 +00001983 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001984 Op *pOp = p->aOp;
1985 for(i=0; i<p->nOp; i++, pOp++){
drh633e6d52008-07-28 19:34:53 +00001986 freeP4(db, pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001987#ifdef SQLITE_DEBUG
drh633e6d52008-07-28 19:34:53 +00001988 sqlite3DbFree(db, pOp->zComment);
drhd4e70eb2008-01-02 00:34:36 +00001989#endif
drh9a324642003-09-06 20:12:01 +00001990 }
1991 }
drhc890fec2008-08-01 20:10:08 +00001992 releaseMemArray(p->aVar, p->nVar);
drh633e6d52008-07-28 19:34:53 +00001993 sqlite3DbFree(db, p->aLabel);
drhc890fec2008-08-01 20:10:08 +00001994 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh633e6d52008-07-28 19:34:53 +00001995 sqlite3DbFree(db, p->aColName);
1996 sqlite3DbFree(db, p->zSql);
drh9a324642003-09-06 20:12:01 +00001997 p->magic = VDBE_MAGIC_DEAD;
drhb2771ce2009-02-20 01:28:59 +00001998 sqlite3DbFree(db, p->aOp);
1999 sqlite3DbFree(db, p->pFree);
drh633e6d52008-07-28 19:34:53 +00002000 sqlite3DbFree(db, p);
drh9a324642003-09-06 20:12:01 +00002001}
drha11846b2004-01-07 18:52:56 +00002002
2003/*
drha11846b2004-01-07 18:52:56 +00002004** If a MoveTo operation is pending on the given cursor, then do that
2005** MoveTo now. Return an error code. If no MoveTo is pending, this
2006** routine does nothing and returns SQLITE_OK.
2007*/
drhdfe88ec2008-11-03 20:55:06 +00002008int sqlite3VdbeCursorMoveto(VdbeCursor *p){
drha11846b2004-01-07 18:52:56 +00002009 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00002010 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00002011#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002012 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00002013#endif
drhf0863fe2005-06-12 21:35:51 +00002014 assert( p->isTable );
drhe63d9992008-08-13 19:11:48 +00002015 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00002016 if( rc ) return rc;
drhf0863fe2005-06-12 21:35:51 +00002017 p->lastRowid = keyToInt(p->movetoTarget);
drh61495262009-04-22 15:32:59 +00002018 p->rowidIsValid = ALWAYS(res==0) ?1:0;
2019 if( NEVER(res<0) ){
drh536065a2005-01-26 21:55:31 +00002020 rc = sqlite3BtreeNext(p->pCursor, &res);
2021 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00002022 }
drh10cfdd52006-08-08 15:42:59 +00002023#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00002024 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00002025#endif
drha11846b2004-01-07 18:52:56 +00002026 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002027 p->cacheStatus = CACHE_STALE;
drha3460582008-07-11 21:02:53 +00002028 }else if( p->pCursor ){
2029 int hasMoved;
2030 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
2031 if( rc ) return rc;
2032 if( hasMoved ){
2033 p->cacheStatus = CACHE_STALE;
2034 p->nullRow = 1;
2035 }
drha11846b2004-01-07 18:52:56 +00002036 }
2037 return SQLITE_OK;
2038}
danielk19774adee202004-05-08 08:23:19 +00002039
drhab9f7f12004-05-08 10:56:11 +00002040/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002041** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00002042**
danielk1977cfcdaef2004-05-12 07:33:33 +00002043** sqlite3VdbeSerialType()
2044** sqlite3VdbeSerialTypeLen()
danielk197790e4d952004-05-10 10:05:53 +00002045** sqlite3VdbeSerialLen()
shane92003092008-07-31 01:43:13 +00002046** sqlite3VdbeSerialPut()
2047** sqlite3VdbeSerialGet()
danielk197790e4d952004-05-10 10:05:53 +00002048**
2049** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00002050** data and index records. Each serialized value consists of a
2051** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2052** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00002053**
danielk1977cfcdaef2004-05-12 07:33:33 +00002054** In an SQLite index record, the serial type is stored directly before
2055** the blob of data that it corresponds to. In a table record, all serial
2056** types are stored at the start of the record, and the blobs of data at
2057** the end. Hence these functions allow the caller to handle the
2058** serial-type and data blob seperately.
2059**
2060** The following table describes the various storage classes for data:
2061**
2062** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00002063** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00002064** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00002065** 1 1 signed integer
2066** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00002067** 3 3 signed integer
2068** 4 4 signed integer
2069** 5 6 signed integer
2070** 6 8 signed integer
2071** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00002072** 8 0 Integer constant 0
2073** 9 0 Integer constant 1
2074** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00002075** N>=12 and even (N-12)/2 BLOB
2076** N>=13 and odd (N-13)/2 text
2077**
drh35a59652006-01-02 18:24:40 +00002078** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2079** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00002080*/
2081
2082/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002083** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00002084*/
drhd946db02005-12-29 19:23:06 +00002085u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00002086 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00002087 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00002088
2089 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00002090 return 0;
danielk197790e4d952004-05-10 10:05:53 +00002091 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002092 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00002093 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drh5284a052008-05-08 15:18:10 +00002094# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00002095 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00002096 u64 u;
2097 if( file_format>=4 && (i&1)==i ){
drh8df32842008-12-09 02:51:23 +00002098 return 8+(u32)i;
drhd946db02005-12-29 19:23:06 +00002099 }
2100 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00002101 if( u<=127 ) return 1;
2102 if( u<=32767 ) return 2;
2103 if( u<=8388607 ) return 3;
2104 if( u<=2147483647 ) return 4;
2105 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00002106 return 6;
danielk197790e4d952004-05-10 10:05:53 +00002107 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002108 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00002109 return 7;
danielk197790e4d952004-05-10 10:05:53 +00002110 }
danielk1977e4359752008-11-03 09:39:45 +00002111 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
drhfdf972a2007-05-02 13:30:27 +00002112 n = pMem->n;
2113 if( flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002114 n += pMem->u.nZero;
danielk197790e4d952004-05-10 10:05:53 +00002115 }
drhfdf972a2007-05-02 13:30:27 +00002116 assert( n>=0 );
2117 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00002118}
2119
2120/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002121** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00002122*/
drh35cd6432009-06-05 14:17:21 +00002123u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00002124 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00002125 return (serial_type-12)/2;
2126 }else{
drh57196282004-10-06 15:41:16 +00002127 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00002128 return aSize[serial_type];
2129 }
danielk1977192ac1d2004-05-10 07:17:30 +00002130}
2131
2132/*
drh110daac2007-05-04 11:59:31 +00002133** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00002134** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00002135** upper 4 bytes. Return the result.
2136**
drh7a4f5022007-05-23 07:20:08 +00002137** For most architectures, this is a no-op.
2138**
2139** (later): It is reported to me that the mixed-endian problem
2140** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2141** that early versions of GCC stored the two words of a 64-bit
2142** float in the wrong order. And that error has been propagated
2143** ever since. The blame is not necessarily with GCC, though.
2144** GCC might have just copying the problem from a prior compiler.
2145** I am also told that newer versions of GCC that follow a different
2146** ABI get the byte order right.
2147**
2148** Developers using SQLite on an ARM7 should compile and run their
2149** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2150** enabled, some asserts below will ensure that the byte order of
2151** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00002152**
2153** (2007-08-30) Frank van Vugt has studied this problem closely
2154** and has send his findings to the SQLite developers. Frank
2155** writes that some Linux kernels offer floating point hardware
2156** emulation that uses only 32-bit mantissas instead of a full
2157** 48-bits as required by the IEEE standard. (This is the
2158** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2159** byte swapping becomes very complicated. To avoid problems,
2160** the necessary byte swapping is carried out using a 64-bit integer
2161** rather than a 64-bit float. Frank assures us that the code here
2162** works for him. We, the developers, have no way to independently
2163** verify this, but Frank seems to know what he is talking about
2164** so we trust him.
drh110daac2007-05-04 11:59:31 +00002165*/
2166#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00002167static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00002168 union {
drh60d09a72007-08-30 15:05:08 +00002169 u64 r;
drh110daac2007-05-04 11:59:31 +00002170 u32 i[2];
2171 } u;
2172 u32 t;
2173
2174 u.r = in;
2175 t = u.i[0];
2176 u.i[0] = u.i[1];
2177 u.i[1] = t;
2178 return u.r;
2179}
2180# define swapMixedEndianFloat(X) X = floatSwap(X)
2181#else
2182# define swapMixedEndianFloat(X)
2183#endif
2184
2185/*
danielk1977cfcdaef2004-05-12 07:33:33 +00002186** Write the serialized data blob for the value stored in pMem into
2187** buf. It is assumed that the caller has allocated sufficient space.
2188** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00002189**
2190** nBuf is the amount of space left in buf[]. nBuf must always be
2191** large enough to hold the entire field. Except, if the field is
2192** a blob with a zero-filled tail, then buf[] might be just the right
2193** size to hold everything except for the zero-filled tail. If buf[]
2194** is only big enough to hold the non-zero prefix, then only write that
2195** prefix into buf[]. But if buf[] is large enough to hold both the
2196** prefix and the tail then write the prefix and set the tail to all
2197** zeros.
2198**
2199** Return the number of bytes actually written into buf[]. The number
2200** of bytes in the zero-filled tail is included in the return value only
2201** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00002202*/
drh35cd6432009-06-05 14:17:21 +00002203u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00002204 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
drh35cd6432009-06-05 14:17:21 +00002205 u32 len;
danielk1977183f9f72004-05-13 05:20:26 +00002206
drh1483e142004-05-21 21:12:42 +00002207 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00002208 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00002209 u64 v;
drh35cd6432009-06-05 14:17:21 +00002210 u32 i;
drha19b7752004-05-30 21:14:58 +00002211 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00002212 assert( sizeof(v)==sizeof(pMem->r) );
2213 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00002214 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00002215 }else{
drh3c024d62007-03-30 11:23:45 +00002216 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00002217 }
drh1483e142004-05-21 21:12:42 +00002218 len = i = sqlite3VdbeSerialTypeLen(serial_type);
shane75ac1de2009-06-09 18:58:52 +00002219 assert( len<=(u32)nBuf );
drh1483e142004-05-21 21:12:42 +00002220 while( i-- ){
drh8df32842008-12-09 02:51:23 +00002221 buf[i] = (u8)(v&0xFF);
drh1483e142004-05-21 21:12:42 +00002222 v >>= 8;
2223 }
2224 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002225 }
drhd946db02005-12-29 19:23:06 +00002226
danielk1977cfcdaef2004-05-12 07:33:33 +00002227 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002228 if( serial_type>=12 ){
drh8df32842008-12-09 02:51:23 +00002229 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
shane75ac1de2009-06-09 18:58:52 +00002230 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
drhfdf972a2007-05-02 13:30:27 +00002231 assert( pMem->n<=nBuf );
2232 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002233 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002234 if( pMem->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00002235 len += pMem->u.nZero;
drh35cd6432009-06-05 14:17:21 +00002236 assert( nBuf>=0 );
2237 if( len > (u32)nBuf ){
2238 len = (u32)nBuf;
drhfdf972a2007-05-02 13:30:27 +00002239 }
2240 memset(&buf[pMem->n], 0, len-pMem->n);
2241 }
drhd946db02005-12-29 19:23:06 +00002242 return len;
2243 }
2244
2245 /* NULL or constants 0 or 1 */
2246 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002247}
2248
2249/*
2250** Deserialize the data blob pointed to by buf as serial type serial_type
2251** and store the result in pMem. Return the number of bytes read.
2252*/
drh35cd6432009-06-05 14:17:21 +00002253u32 sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002254 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002255 u32 serial_type, /* Serial type to deserialize */
2256 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002257){
drh3c685822005-05-21 18:32:18 +00002258 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002259 case 10: /* Reserved for future use */
2260 case 11: /* Reserved for future use */
2261 case 0: { /* NULL */
2262 pMem->flags = MEM_Null;
2263 break;
2264 }
2265 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002266 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002267 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002268 return 1;
drh1483e142004-05-21 21:12:42 +00002269 }
drh3c685822005-05-21 18:32:18 +00002270 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002271 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002272 pMem->flags = MEM_Int;
2273 return 2;
2274 }
2275 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002276 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002277 pMem->flags = MEM_Int;
2278 return 3;
2279 }
2280 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002281 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002282 pMem->flags = MEM_Int;
2283 return 4;
2284 }
2285 case 5: { /* 6-byte signed integer */
2286 u64 x = (((signed char)buf[0])<<8) | buf[1];
2287 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2288 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002289 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002290 pMem->flags = MEM_Int;
2291 return 6;
2292 }
drh91124b32005-08-18 18:15:05 +00002293 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002294 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002295 u64 x;
2296 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002297#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002298 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002299 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2300 ** defined that 64-bit floating point values really are mixed
2301 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002302 */
drhde941c62005-08-28 01:34:21 +00002303 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002304 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002305 u64 t2 = t1;
2306 swapMixedEndianFloat(t2);
2307 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002308#endif
drhbfd6b032005-08-28 01:38:44 +00002309
drhd81bd4e2005-09-05 20:06:49 +00002310 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2311 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002312 x = (x<<32) | y;
2313 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002314 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002315 pMem->flags = MEM_Int;
2316 }else{
drh4f0c5872007-03-26 22:05:01 +00002317 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002318 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002319 memcpy(&pMem->r, &x, sizeof(x));
drh2eaf93d2008-04-29 00:15:20 +00002320 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
drh3c685822005-05-21 18:32:18 +00002321 }
2322 return 8;
2323 }
drhd946db02005-12-29 19:23:06 +00002324 case 8: /* Integer 0 */
2325 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002326 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002327 pMem->flags = MEM_Int;
2328 return 0;
2329 }
drh3c685822005-05-21 18:32:18 +00002330 default: {
drh35cd6432009-06-05 14:17:21 +00002331 u32 len = (serial_type-12)/2;
drh3c685822005-05-21 18:32:18 +00002332 pMem->z = (char *)buf;
2333 pMem->n = len;
2334 pMem->xDel = 0;
2335 if( serial_type&0x01 ){
2336 pMem->flags = MEM_Str | MEM_Ephem;
2337 }else{
2338 pMem->flags = MEM_Blob | MEM_Ephem;
2339 }
2340 return len;
drh696b32f2004-05-30 01:51:52 +00002341 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002342 }
drh3c685822005-05-21 18:32:18 +00002343 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002344}
2345
drh0e6082e2006-01-12 20:28:35 +00002346
drh1e968a02008-03-25 00:22:21 +00002347/*
2348** Given the nKey-byte encoding of a record in pKey[], parse the
drhe14006d2008-03-25 17:23:32 +00002349** record into a UnpackedRecord structure. Return a pointer to
drh1e968a02008-03-25 00:22:21 +00002350** that structure.
2351**
2352** The calling function might provide szSpace bytes of memory
2353** space at pSpace. This space can be used to hold the returned
2354** VDbeParsedRecord structure if it is large enough. If it is
2355** not big enough, space is obtained from sqlite3_malloc().
2356**
2357** The returned structure should be closed by a call to
drhe14006d2008-03-25 17:23:32 +00002358** sqlite3VdbeDeleteUnpackedRecord().
drh1e968a02008-03-25 00:22:21 +00002359*/
drhe14006d2008-03-25 17:23:32 +00002360UnpackedRecord *sqlite3VdbeRecordUnpack(
drh1e968a02008-03-25 00:22:21 +00002361 KeyInfo *pKeyInfo, /* Information about the record format */
2362 int nKey, /* Size of the binary record */
2363 const void *pKey, /* The binary record */
drh8c5d1522009-04-10 00:56:28 +00002364 char *pSpace, /* Unaligned space available to hold the object */
drh1e968a02008-03-25 00:22:21 +00002365 int szSpace /* Size of pSpace[] in bytes */
2366){
2367 const unsigned char *aKey = (const unsigned char *)pKey;
drh8c5d1522009-04-10 00:56:28 +00002368 UnpackedRecord *p; /* The unpacked record that we will return */
2369 int nByte; /* Memory space needed to hold p, in bytes */
2370 int d;
danielk197700e13612008-11-17 19:18:54 +00002371 u32 idx;
drh8c5d1522009-04-10 00:56:28 +00002372 u16 u; /* Unsigned loop counter */
drh1e968a02008-03-25 00:22:21 +00002373 u32 szHdr;
2374 Mem *pMem;
drh8c5d1522009-04-10 00:56:28 +00002375 int nOff; /* Increase pSpace by this much to 8-byte align it */
drh1e968a02008-03-25 00:22:21 +00002376
shane80167bf2009-04-10 15:42:36 +00002377 /*
2378 ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
2379 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
2380 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
2381 */
2382 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
drh8c5d1522009-04-10 00:56:28 +00002383 pSpace += nOff;
2384 szSpace -= nOff;
2385 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
drh1e968a02008-03-25 00:22:21 +00002386 if( nByte>szSpace ){
2387 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
2388 if( p==0 ) return 0;
drhe63d9992008-08-13 19:11:48 +00002389 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002390 }else{
drh8c5d1522009-04-10 00:56:28 +00002391 p = (UnpackedRecord*)pSpace;
drhe63d9992008-08-13 19:11:48 +00002392 p->flags = UNPACKED_NEED_DESTROY;
drh1e968a02008-03-25 00:22:21 +00002393 }
2394 p->pKeyInfo = pKeyInfo;
2395 p->nField = pKeyInfo->nField + 1;
drh8c5d1522009-04-10 00:56:28 +00002396 p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
2397 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
shane3f8d5cf2008-04-24 19:15:09 +00002398 idx = getVarint32(aKey, szHdr);
drh1e968a02008-03-25 00:22:21 +00002399 d = szHdr;
shane0b8d2762008-07-22 05:18:00 +00002400 u = 0;
2401 while( idx<szHdr && u<p->nField ){
drh1e968a02008-03-25 00:22:21 +00002402 u32 serial_type;
2403
danielk197700e13612008-11-17 19:18:54 +00002404 idx += getVarint32(&aKey[idx], serial_type);
drh1e968a02008-03-25 00:22:21 +00002405 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
2406 pMem->enc = pKeyInfo->enc;
2407 pMem->db = pKeyInfo->db;
2408 pMem->flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002409 pMem->zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002410 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
drhe14006d2008-03-25 17:23:32 +00002411 pMem++;
shane0b8d2762008-07-22 05:18:00 +00002412 u++;
drh1e968a02008-03-25 00:22:21 +00002413 }
drh7d10d5a2008-08-20 16:35:10 +00002414 assert( u<=pKeyInfo->nField + 1 );
shane0b8d2762008-07-22 05:18:00 +00002415 p->nField = u;
drh1e968a02008-03-25 00:22:21 +00002416 return (void*)p;
2417}
2418
2419/*
drhe14006d2008-03-25 17:23:32 +00002420** This routine destroys a UnpackedRecord object
drh1e968a02008-03-25 00:22:21 +00002421*/
drhe14006d2008-03-25 17:23:32 +00002422void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
drh1e968a02008-03-25 00:22:21 +00002423 if( p ){
drhe63d9992008-08-13 19:11:48 +00002424 if( p->flags & UNPACKED_NEED_DESTROY ){
drh1e968a02008-03-25 00:22:21 +00002425 int i;
drhe14006d2008-03-25 17:23:32 +00002426 Mem *pMem;
2427 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
danielk19775f096132008-03-28 15:44:09 +00002428 if( pMem->zMalloc ){
drhe14006d2008-03-25 17:23:32 +00002429 sqlite3VdbeMemRelease(pMem);
drh1e968a02008-03-25 00:22:21 +00002430 }
2431 }
2432 }
drhe63d9992008-08-13 19:11:48 +00002433 if( p->flags & UNPACKED_NEED_FREE ){
drh633e6d52008-07-28 19:34:53 +00002434 sqlite3DbFree(p->pKeyInfo->db, p);
drh1e968a02008-03-25 00:22:21 +00002435 }
2436 }
2437}
2438
2439/*
2440** This function compares the two table rows or index records
2441** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
drhe63d9992008-08-13 19:11:48 +00002442** or positive integer if key1 is less than, equal to or
2443** greater than key2. The {nKey1, pKey1} key must be a blob
drh1e968a02008-03-25 00:22:21 +00002444** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
2445** key must be a parsed key such as obtained from
2446** sqlite3VdbeParseRecord.
2447**
2448** Key1 and Key2 do not have to contain the same number of fields.
drhe63d9992008-08-13 19:11:48 +00002449** The key with fewer fields is usually compares less than the
2450** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
2451** and the common prefixes are equal, then key1 is less than key2.
2452** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
2453** equal, then the keys are considered to be equal and
drhec1fc802008-08-13 14:07:40 +00002454** the parts beyond the common prefix are ignored.
2455**
drhe63d9992008-08-13 19:11:48 +00002456** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
2457** the header of pKey1 is ignored. It is assumed that pKey1 is
2458** an index key, and thus ends with a rowid value. The last byte
2459** of the header will therefore be the serial type of the rowid:
2460** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
2461** The serial type of the final rowid will always be a single byte.
2462** By ignoring this last byte of the header, we force the comparison
2463** to ignore the rowid at the end of key1.
drh1e968a02008-03-25 00:22:21 +00002464*/
drhe14006d2008-03-25 17:23:32 +00002465int sqlite3VdbeRecordCompare(
drhec1fc802008-08-13 14:07:40 +00002466 int nKey1, const void *pKey1, /* Left key */
drhec1fc802008-08-13 14:07:40 +00002467 UnpackedRecord *pPKey2 /* Right key */
drh1e968a02008-03-25 00:22:21 +00002468){
danielk197700e13612008-11-17 19:18:54 +00002469 int d1; /* Offset into aKey[] of next data element */
drh1e968a02008-03-25 00:22:21 +00002470 u32 idx1; /* Offset into aKey[] of next header element */
2471 u32 szHdr1; /* Number of bytes in header */
2472 int i = 0;
2473 int nField;
2474 int rc = 0;
2475 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2476 KeyInfo *pKeyInfo;
2477 Mem mem1;
2478
2479 pKeyInfo = pPKey2->pKeyInfo;
2480 mem1.enc = pKeyInfo->enc;
2481 mem1.db = pKeyInfo->db;
2482 mem1.flags = 0;
shane60a4b532009-05-06 18:57:09 +00002483 mem1.u.i = 0; /* not needed, here to silence compiler warning */
danielk19775f096132008-03-28 15:44:09 +00002484 mem1.zMalloc = 0;
drh1e968a02008-03-25 00:22:21 +00002485
shane3f8d5cf2008-04-24 19:15:09 +00002486 idx1 = getVarint32(aKey1, szHdr1);
drh1e968a02008-03-25 00:22:21 +00002487 d1 = szHdr1;
drhe63d9992008-08-13 19:11:48 +00002488 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
2489 szHdr1--;
2490 }
drh1e968a02008-03-25 00:22:21 +00002491 nField = pKeyInfo->nField;
2492 while( idx1<szHdr1 && i<pPKey2->nField ){
2493 u32 serial_type1;
2494
2495 /* Read the serial types for the next element in each key. */
shane3f8d5cf2008-04-24 19:15:09 +00002496 idx1 += getVarint32( aKey1+idx1, serial_type1 );
drh1e968a02008-03-25 00:22:21 +00002497 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2498
2499 /* Extract the values to be compared.
2500 */
2501 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2502
2503 /* Do the comparison
2504 */
drhe14006d2008-03-25 17:23:32 +00002505 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
drh1e968a02008-03-25 00:22:21 +00002506 i<nField ? pKeyInfo->aColl[i] : 0);
drh1e968a02008-03-25 00:22:21 +00002507 if( rc!=0 ){
2508 break;
2509 }
2510 i++;
2511 }
danielk19775f096132008-03-28 15:44:09 +00002512 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1);
drh1e968a02008-03-25 00:22:21 +00002513
danielk1977de630352009-05-04 11:42:29 +00002514 /* If the PREFIX_SEARCH flag is set and all fields except the final
2515 ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
2516 ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
2517 ** This is used by the OP_IsUnique opcode.
2518 */
2519 if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
2520 assert( idx1==szHdr1 && rc );
2521 assert( mem1.flags & MEM_Int );
2522 pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
2523 pPKey2->rowid = mem1.u.i;
2524 }
2525
drh1e968a02008-03-25 00:22:21 +00002526 if( rc==0 ){
drhec1fc802008-08-13 14:07:40 +00002527 /* rc==0 here means that one of the keys ran out of fields and
drhe63d9992008-08-13 19:11:48 +00002528 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
2529 ** flag is set, then break the tie by treating key2 as larger.
2530 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
drhec1fc802008-08-13 14:07:40 +00002531 ** are considered to be equal. Otherwise, the longer key is the
2532 ** larger. As it happens, the pPKey2 will always be the longer
2533 ** if there is a difference.
2534 */
drhe63d9992008-08-13 19:11:48 +00002535 if( pPKey2->flags & UNPACKED_INCRKEY ){
drh1e968a02008-03-25 00:22:21 +00002536 rc = -1;
drhe63d9992008-08-13 19:11:48 +00002537 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
drhec1fc802008-08-13 14:07:40 +00002538 /* Leave rc==0 */
2539 }else if( idx1<szHdr1 ){
2540 rc = 1;
drh1e968a02008-03-25 00:22:21 +00002541 }
2542 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2543 && pKeyInfo->aSortOrder[i] ){
2544 rc = -rc;
2545 }
2546
2547 return rc;
2548}
drhec1fc802008-08-13 14:07:40 +00002549
danielk1977eb015e02004-05-18 01:31:14 +00002550
2551/*
drh7a224de2004-06-02 01:22:02 +00002552** pCur points at an index entry created using the OP_MakeRecord opcode.
2553** Read the rowid (the last field in the record) and store it in *rowid.
2554** Return SQLITE_OK if everything works, or an error code otherwise.
drh88a003e2008-12-11 16:17:03 +00002555**
2556** pCur might be pointing to text obtained from a corrupt database file.
2557** So the content cannot be trusted. Do appropriate checks on the content.
danielk1977183f9f72004-05-13 05:20:26 +00002558*/
drhb21c8cd2007-08-21 19:33:56 +00002559int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002560 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002561 int rc;
drhd5788202004-05-28 08:21:05 +00002562 u32 szHdr; /* Size of the header */
2563 u32 typeRowid; /* Serial type of the rowid */
2564 u32 lenRowid; /* Size of the rowid */
2565 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002566
drh88a003e2008-12-11 16:17:03 +00002567 /* Get the size of the index entry. Only indices entries of less
2568 ** than 2GiB are support - anything large must be database corruption */
drhd5788202004-05-28 08:21:05 +00002569 sqlite3BtreeKeySize(pCur, &nCellKey);
drh88a003e2008-12-11 16:17:03 +00002570 if( unlikely(nCellKey<=0 || nCellKey>0x7fffffff) ){
drh49285702005-09-17 15:20:26 +00002571 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002572 }
drh88a003e2008-12-11 16:17:03 +00002573
2574 /* Read in the complete content of the index entry */
danielk1977a7a8e142008-02-13 18:25:27 +00002575 m.flags = 0;
2576 m.db = 0;
danielk19775f096132008-03-28 15:44:09 +00002577 m.zMalloc = 0;
drh8df32842008-12-09 02:51:23 +00002578 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002579 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002580 return rc;
2581 }
drh88a003e2008-12-11 16:17:03 +00002582
2583 /* The index entry must begin with a header size */
shane3f8d5cf2008-04-24 19:15:09 +00002584 (void)getVarint32((u8*)m.z, szHdr);
drh88a003e2008-12-11 16:17:03 +00002585 testcase( szHdr==2 );
2586 testcase( szHdr==m.n );
shane15301592008-12-16 17:20:38 +00002587 if( unlikely(szHdr<2 || (int)szHdr>m.n) ){
drh88a003e2008-12-11 16:17:03 +00002588 goto idx_rowid_corruption;
2589 }
2590
2591 /* The last field of the index should be an integer - the ROWID.
2592 ** Verify that the last entry really is an integer. */
shane3f8d5cf2008-04-24 19:15:09 +00002593 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
drh88a003e2008-12-11 16:17:03 +00002594 testcase( typeRowid==1 );
2595 testcase( typeRowid==2 );
2596 testcase( typeRowid==3 );
2597 testcase( typeRowid==4 );
2598 testcase( typeRowid==5 );
2599 testcase( typeRowid==6 );
2600 testcase( typeRowid==8 );
2601 testcase( typeRowid==9 );
2602 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
2603 goto idx_rowid_corruption;
2604 }
drhd5788202004-05-28 08:21:05 +00002605 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh88a003e2008-12-11 16:17:03 +00002606 testcase( m.n-lenRowid==szHdr );
2607 if( unlikely(m.n-lenRowid<szHdr) ){
2608 goto idx_rowid_corruption;
2609 }
2610
2611 /* Fetch the integer off the end of the index record */
drh2646da72005-12-09 20:02:05 +00002612 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002613 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002614 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002615 return SQLITE_OK;
drh88a003e2008-12-11 16:17:03 +00002616
2617 /* Jump here if database corruption is detected after m has been
2618 ** allocated. Free the m object and return SQLITE_CORRUPT. */
2619idx_rowid_corruption:
2620 testcase( m.zMalloc!=0 );
2621 sqlite3VdbeMemRelease(&m);
2622 return SQLITE_CORRUPT_BKPT;
danielk1977183f9f72004-05-13 05:20:26 +00002623}
2624
drh7cf6e4d2004-05-19 14:56:55 +00002625/*
drhd3d39e92004-05-20 22:16:29 +00002626** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002627** the key string in pKey (of length nKey). Write into *pRes a number
2628** that is negative, zero, or positive if pC is less than, equal to,
2629** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002630**
drhd5788202004-05-28 08:21:05 +00002631** pKey is either created without a rowid or is truncated so that it
2632** omits the rowid at the end. The rowid at the end of the index entry
drhec1fc802008-08-13 14:07:40 +00002633** is ignored as well. Hence, this routine only compares the prefixes
2634** of the keys prior to the final rowid, not the entire key.
2635**
2636** pUnpacked may be an unpacked version of pKey,nKey. If pUnpacked is
2637** supplied it is used in place of pKey,nKey.
drh7cf6e4d2004-05-19 14:56:55 +00002638*/
danielk1977183f9f72004-05-13 05:20:26 +00002639int sqlite3VdbeIdxKeyCompare(
drhdfe88ec2008-11-03 20:55:06 +00002640 VdbeCursor *pC, /* The cursor to compare against */
drhec1fc802008-08-13 14:07:40 +00002641 UnpackedRecord *pUnpacked, /* Unpacked version of pKey and nKey */
drh7cf6e4d2004-05-19 14:56:55 +00002642 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002643){
drh61fc5952007-04-01 23:49:51 +00002644 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002645 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002646 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002647 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002648
2649 sqlite3BtreeKeySize(pCur, &nCellKey);
drh8df32842008-12-09 02:51:23 +00002650 if( nCellKey<=0 || nCellKey>0x7fffffff ){
danielk1977183f9f72004-05-13 05:20:26 +00002651 *res = 0;
2652 return SQLITE_OK;
2653 }
danielk1977a7a8e142008-02-13 18:25:27 +00002654 m.db = 0;
2655 m.flags = 0;
danielk19775f096132008-03-28 15:44:09 +00002656 m.zMalloc = 0;
drh8df32842008-12-09 02:51:23 +00002657 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
drhec1fc802008-08-13 14:07:40 +00002658 if( rc ){
drhd5788202004-05-28 08:21:05 +00002659 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002660 }
drhe63d9992008-08-13 19:11:48 +00002661 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
2662 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
danielk1977d8123362004-06-12 09:25:12 +00002663 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002664 return SQLITE_OK;
2665}
danielk1977b28af712004-06-21 06:50:26 +00002666
2667/*
2668** This routine sets the value to be returned by subsequent calls to
2669** sqlite3_changes() on the database handle 'db'.
2670*/
2671void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002672 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002673 db->nChange = nChange;
2674 db->nTotalChange += nChange;
2675}
2676
2677/*
2678** Set a flag in the vdbe to update the change counter when it is finalised
2679** or reset.
2680*/
drh4794f732004-11-05 17:17:50 +00002681void sqlite3VdbeCountChanges(Vdbe *v){
2682 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002683}
drhd89bd002005-01-22 03:03:54 +00002684
2685/*
2686** Mark every prepared statement associated with a database connection
2687** as expired.
2688**
2689** An expired statement means that recompilation of the statement is
2690** recommend. Statements expire when things happen that make their
2691** programs obsolete. Removing user-defined functions or collating
2692** sequences, or changing an authorization function are the types of
2693** things that make prepared statements obsolete.
2694*/
2695void sqlite3ExpirePreparedStatements(sqlite3 *db){
2696 Vdbe *p;
2697 for(p = db->pVdbe; p; p=p->pNext){
2698 p->expired = 1;
2699 }
2700}
danielk1977aee18ef2005-03-09 12:26:50 +00002701
2702/*
2703** Return the database associated with the Vdbe.
2704*/
2705sqlite3 *sqlite3VdbeDb(Vdbe *v){
2706 return v->db;
2707}