blob: b3f903c34cb09c49993f1258f54d6b546ca409d1 [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.
16*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <ctype.h>
20#include "vdbeInt.h"
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
danielk1977132872b2004-05-10 10:37:18 +000025** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000026** as they are added to the instruction stream.
27*/
drh8d904f02005-06-14 17:47:58 +000028#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +000029int sqlite3_vdbe_addop_trace = 0;
drh9a324642003-09-06 20:12:01 +000030#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
drh9bb575f2004-09-06 17:24:11 +000036Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
drhb900aaf2006-11-09 00:24:53 +000052** Remember the SQL string for a prepared statement.
53*/
54void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
55 if( p==0 ) return;
56 assert( p->zSql==0 );
57 p->zSql = sqlite3StrNDup(z, n);
58}
59
60/*
61** Return the SQL associated with a prepared statement
62*/
63const char *sqlite3VdbeGetSql(Vdbe *p){
64 return p->zSql;
65}
66
67/*
drhc5155252007-01-08 21:07:17 +000068** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000069*/
drhc5155252007-01-08 21:07:17 +000070void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
71 Vdbe tmp, *pTmp;
72 char *zTmp;
73 int nTmp;
74 tmp = *pA;
75 *pA = *pB;
76 *pB = tmp;
77 pTmp = pA->pNext;
78 pA->pNext = pB->pNext;
79 pB->pNext = pTmp;
80 pTmp = pA->pPrev;
81 pA->pPrev = pB->pPrev;
82 pB->pPrev = pTmp;
83 zTmp = pA->zSql;
84 pA->zSql = pB->zSql;
85 pB->zSql = zTmp;
86 nTmp = pA->nSql;
87 pA->nSql = pB->nSql;
88 pB->nSql = nTmp;
drhb900aaf2006-11-09 00:24:53 +000089}
90
91/*
drh9a324642003-09-06 20:12:01 +000092** Turn tracing on or off
93*/
danielk19774adee202004-05-08 08:23:19 +000094void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000095 p->trace = trace;
96}
97
98/*
drh76ff3a02004-09-24 22:32:30 +000099** Resize the Vdbe.aOp array so that it contains at least N
danielk1977634f2982005-03-28 08:44:07 +0000100** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
danielk1977ace3eb22006-01-26 10:35:04 +0000101** the Vdbe.aOp array will be sized to contain exactly N
102** elements. Vdbe.nOpAlloc is set to reflect the new size of
103** the array.
104**
105** If an out-of-memory error occurs while resizing the array,
106** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
107** any opcodes already allocated can be correctly deallocated
108** along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000109*/
110static void resizeOpArray(Vdbe *p, int N){
drh53f733c2005-09-16 02:38:09 +0000111 int runMode = p->magic==VDBE_MAGIC_RUN;
112 if( runMode || p->nOpAlloc<N ){
drhb38ad992005-09-16 00:27:01 +0000113 VdbeOp *pNew;
drh53f733c2005-09-16 02:38:09 +0000114 int nNew = N + 100*(!runMode);
115 int oldSize = p->nOpAlloc;
116 pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
drhb38ad992005-09-16 00:27:01 +0000117 if( pNew ){
drh53f733c2005-09-16 02:38:09 +0000118 p->nOpAlloc = nNew;
drhb38ad992005-09-16 00:27:01 +0000119 p->aOp = pNew;
drh53f733c2005-09-16 02:38:09 +0000120 if( nNew>oldSize ){
121 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
122 }
drh76ff3a02004-09-24 22:32:30 +0000123 }
124 }
125}
126
127/*
drh9a324642003-09-06 20:12:01 +0000128** Add a new instruction to the list of instructions current in the
129** VDBE. Return the address of the new instruction.
130**
131** Parameters:
132**
133** p Pointer to the VDBE
134**
135** op The opcode for this instruction
136**
137** p1, p2 First two of the three possible operands.
138**
danielk19774adee202004-05-08 08:23:19 +0000139** Use the sqlite3VdbeResolveLabel() function to fix an address and
140** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +0000141** operand.
142*/
danielk19774adee202004-05-08 08:23:19 +0000143int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +0000144 int i;
drh701a0ae2004-02-22 20:05:00 +0000145 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000146
147 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000148 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000149 if( p->nOpAlloc<=i ){
150 resizeOpArray(p, i+1);
151 if( sqlite3MallocFailed() ){
152 return 0;
153 }
drh9a324642003-09-06 20:12:01 +0000154 }
danielk197701256832007-04-18 14:24:32 +0000155 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000156 pOp = &p->aOp[i];
157 pOp->opcode = op;
158 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000159 pOp->p2 = p2;
160 pOp->p3 = 0;
161 pOp->p3type = P3_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000162 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000163#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000164 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000165#endif
166 return i;
167}
168
169/*
drh701a0ae2004-02-22 20:05:00 +0000170** Add an opcode that includes the p3 value.
171*/
drheb2e1762004-05-27 01:53:56 +0000172int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000173 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
174 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000175 return addr;
176}
177
178/*
drh9a324642003-09-06 20:12:01 +0000179** Create a new symbolic label for an instruction that has yet to be
180** coded. The symbolic label is really just a negative number. The
181** label can be used as the P2 value of an operation. Later, when
182** the label is resolved to a specific address, the VDBE will scan
183** through its operation list and change all values of P2 which match
184** the label into the resolved address.
185**
186** The VDBE knows that a P2 value is a label because labels are
187** always negative and P2 values are suppose to be non-negative.
188** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000189**
190** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000191*/
danielk19774adee202004-05-08 08:23:19 +0000192int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000193 int i;
194 i = p->nLabel++;
195 assert( p->magic==VDBE_MAGIC_INIT );
196 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000197 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
drhcf643722007-03-27 13:36:37 +0000198 p->aLabel = sqliteReallocOrFree(p->aLabel,
199 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000200 }
drh76ff3a02004-09-24 22:32:30 +0000201 if( p->aLabel ){
202 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000203 }
drh9a324642003-09-06 20:12:01 +0000204 return -1-i;
205}
206
207/*
208** Resolve label "x" to be the address of the next instruction to
209** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000210** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000211*/
danielk19774adee202004-05-08 08:23:19 +0000212void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000213 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000214 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000215 assert( j>=0 && j<p->nLabel );
216 if( p->aLabel ){
217 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000218 }
219}
220
221/*
danielk1977bc04f852005-03-29 08:26:13 +0000222** Return non-zero if opcode 'op' is guarenteed not to push more values
223** onto the VDBE stack than it pops off.
224*/
danielk19777a5147c2005-03-29 13:07:00 +0000225static int opcodeNoPush(u8 op){
226 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000227 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
228 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000229 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000230 ** in a comment on the same line as the "case OP_XXX:" in
231 ** sqlite3VdbeExec() in vdbe.c.
232 **
233 ** If the bit is true, then the corresponding opcode is guarenteed not
234 ** to grow the stack when it is executed. Otherwise, it may grow the
235 ** stack by at most one entry.
236 **
danielk19777a5147c2005-03-29 13:07:00 +0000237 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000238 ** one bit for opcodes 16 to 31, and so on.
239 **
240 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
241 ** because the file is generated by an awk program. Awk manipulates
242 ** all numbers as floating-point and we don't want to risk a rounding
243 ** error if someone builds with an awk that uses (for example) 32-bit
244 ** IEEE floats.
245 */
drh9a7e6082005-03-31 22:26:19 +0000246 static const u32 masks[5] = {
drh580eeaf2006-02-24 03:09:37 +0000247 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
248 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
249 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
250 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
251 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000252 };
drh9cbe6352005-11-29 03:13:21 +0000253 assert( op<32*5 );
danielk1977bc04f852005-03-29 08:26:13 +0000254 return (masks[op>>5] & (1<<(op&0x1F)));
255}
256
257#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000258int sqlite3VdbeOpcodeNoPush(u8 op){
259 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000260}
261#endif
262
263/*
drh76ff3a02004-09-24 22:32:30 +0000264** Loop through the program looking for P2 values that are negative.
265** Each such value is a label. Resolve the label by setting the P2
266** value to its correct non-zero value.
267**
268** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000269**
drh13449892005-09-07 21:22:45 +0000270** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000271** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000272** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000273**
274** The integer *pMaxStack is set to the maximum number of vdbe stack
275** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000276**
277** This routine also does the following optimization: It scans for
278** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
drhf0863fe2005-06-12 21:35:51 +0000279** IdxInsert instructions where P2!=0. If no such instruction is
drh38449902005-06-07 01:43:41 +0000280** found, then every Statement instruction is changed to a Noop. In
281** this way, we avoid creating the statement journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000282*/
danielk1977bc04f852005-03-29 08:26:13 +0000283static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000284 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000285 int nMaxArgs = 0;
286 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000287 Op *pOp;
288 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000289 int doesStatementRollback = 0;
290 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000291 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000292 u8 opcode = pOp->opcode;
293
danielk19774b2688a2006-06-20 11:01:07 +0000294 if( opcode==OP_Function || opcode==OP_AggStep
danielk1977399918f2006-06-14 13:03:23 +0000295#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19774b2688a2006-06-20 11:01:07 +0000296 || opcode==OP_VUpdate
danielk1977399918f2006-06-14 13:03:23 +0000297#endif
298 ){
danielk1977bc04f852005-03-29 08:26:13 +0000299 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh38449902005-06-07 01:43:41 +0000300 }else if( opcode==OP_Halt ){
301 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
302 doesStatementRollback = 1;
303 }
drh38449902005-06-07 01:43:41 +0000304 }else if( opcode==OP_Statement ){
305 hasStatementBegin = 1;
drh4be8b512006-06-13 23:51:34 +0000306 }else if( opcode==OP_VFilter ){
307 int n;
308 assert( p->nOp - i >= 3 );
309 assert( pOp[-2].opcode==OP_Integer );
310 n = pOp[-2].p1;
311 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977bc04f852005-03-29 08:26:13 +0000312 }
danielk19777a5147c2005-03-29 13:07:00 +0000313 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000314 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000315 }
316
drh76ff3a02004-09-24 22:32:30 +0000317 if( pOp->p2>=0 ) continue;
318 assert( -1-pOp->p2<p->nLabel );
319 pOp->p2 = aLabel[-1-pOp->p2];
320 }
321 sqliteFree(p->aLabel);
322 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000323
324 *pMaxFuncArgs = nMaxArgs;
325 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000326
327 /* If we never rollback a statement transaction, then statement
328 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000329 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000330 ** which can be expensive on some platforms.
331 */
332 if( hasStatementBegin && !doesStatementRollback ){
333 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
334 if( pOp->opcode==OP_Statement ){
335 pOp->opcode = OP_Noop;
336 }
337 }
338 }
drh76ff3a02004-09-24 22:32:30 +0000339}
340
341/*
drh9a324642003-09-06 20:12:01 +0000342** Return the address of the next instruction to be inserted.
343*/
danielk19774adee202004-05-08 08:23:19 +0000344int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000345 assert( p->magic==VDBE_MAGIC_INIT );
346 return p->nOp;
347}
348
349/*
350** Add a whole list of operations to the operation stack. Return the
351** address of the first operation added.
352*/
danielk19774adee202004-05-08 08:23:19 +0000353int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000354 int addr;
355 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000356 resizeOpArray(p, p->nOp + nOp);
danielk19779e128002006-01-18 16:51:35 +0000357 if( sqlite3MallocFailed() ){
drh76ff3a02004-09-24 22:32:30 +0000358 return 0;
drh9a324642003-09-06 20:12:01 +0000359 }
360 addr = p->nOp;
361 if( nOp>0 ){
362 int i;
drh905793e2004-02-21 13:31:09 +0000363 VdbeOpList const *pIn = aOp;
364 for(i=0; i<nOp; i++, pIn++){
365 int p2 = pIn->p2;
366 VdbeOp *pOut = &p->aOp[i+addr];
367 pOut->opcode = pIn->opcode;
368 pOut->p1 = pIn->p1;
369 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
370 pOut->p3 = pIn->p3;
371 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000372#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000373 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000375 }
376#endif
377 }
378 p->nOp += nOp;
379 }
380 return addr;
381}
382
383/*
384** Change the value of the P1 operand for a specific instruction.
385** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000386** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000387** few minor changes to the program.
388*/
danielk19774adee202004-05-08 08:23:19 +0000389void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000390 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000391 if( p && addr>=0 && p->nOp>addr && p->aOp ){
392 p->aOp[addr].p1 = val;
393 }
394}
395
396/*
397** Change the value of the P2 operand for a specific instruction.
398** This routine is useful for setting a jump destination.
399*/
danielk19774adee202004-05-08 08:23:19 +0000400void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000401 assert( val>=0 );
drh8aa34ae2006-03-13 12:54:09 +0000402 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000403 if( p && addr>=0 && p->nOp>addr && p->aOp ){
404 p->aOp[addr].p2 = val;
405 }
406}
407
drhd654be82005-09-20 17:42:23 +0000408/*
drhf8875402006-03-17 13:56:34 +0000409** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000410** the address of the next instruction to be coded.
411*/
412void sqlite3VdbeJumpHere(Vdbe *p, int addr){
413 sqlite3VdbeChangeP2(p, addr, p->nOp);
414}
drhb38ad992005-09-16 00:27:01 +0000415
drhb7f6f682006-07-08 17:06:43 +0000416
417/*
418** If the input FuncDef structure is ephemeral, then free it. If
419** the FuncDef is not ephermal, then do nothing.
420*/
421static void freeEphemeralFunction(FuncDef *pDef){
422 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
423 sqliteFree(pDef);
424 }
425}
426
drhb38ad992005-09-16 00:27:01 +0000427/*
428** Delete a P3 value if necessary.
429*/
430static void freeP3(int p3type, void *p3){
431 if( p3 ){
drhac1733d2005-09-17 17:58:22 +0000432 switch( p3type ){
433 case P3_DYNAMIC:
434 case P3_KEYINFO:
435 case P3_KEYINFO_HANDOFF: {
436 sqliteFree(p3);
437 break;
438 }
drh4be8b512006-06-13 23:51:34 +0000439 case P3_MPRINTF: {
440 sqlite3_free(p3);
441 break;
442 }
drhac1733d2005-09-17 17:58:22 +0000443 case P3_VDBEFUNC: {
444 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
drhb7f6f682006-07-08 17:06:43 +0000445 freeEphemeralFunction(pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000446 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
447 sqliteFree(pVdbeFunc);
448 break;
449 }
drhb7f6f682006-07-08 17:06:43 +0000450 case P3_FUNCDEF: {
451 freeEphemeralFunction((FuncDef*)p3);
452 break;
453 }
drhac1733d2005-09-17 17:58:22 +0000454 case P3_MEM: {
455 sqlite3ValueFree((sqlite3_value*)p3);
456 break;
457 }
drhb38ad992005-09-16 00:27:01 +0000458 }
459 }
460}
461
462
drh9a324642003-09-06 20:12:01 +0000463/*
drhf8875402006-03-17 13:56:34 +0000464** Change N opcodes starting at addr to No-ops.
465*/
466void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
467 VdbeOp *pOp = &p->aOp[addr];
468 while( N-- ){
469 freeP3(pOp->p3type, pOp->p3);
470 memset(pOp, 0, sizeof(pOp[0]));
471 pOp->opcode = OP_Noop;
472 pOp++;
473 }
474}
475
476/*
drh9a324642003-09-06 20:12:01 +0000477** Change the value of the P3 operand for a specific instruction.
478** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000479** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000480** few minor changes to the program.
481**
482** If n>=0 then the P3 operand is dynamic, meaning that a copy of
483** the string is made into memory obtained from sqliteMalloc().
484** A value of n==0 means copy bytes of zP3 up to and including the
485** first null byte. If n>0 then copy n+1 bytes of zP3.
486**
danielk19771f55c052005-05-19 08:42:59 +0000487** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
488** A copy is made of the KeyInfo structure into memory obtained from
489** sqliteMalloc, to be freed when the Vdbe is finalized.
490** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
491** stored in memory that the caller has obtained from sqliteMalloc. The
492** caller should not free the allocation, it will be freed when the Vdbe is
493** finalized.
494**
495** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
496** to a string or structure that is guaranteed to exist for the lifetime of
497** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000498**
499** If addr<0 then change P3 on the most recently inserted instruction.
500*/
danielk19774adee202004-05-08 08:23:19 +0000501void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000502 Op *pOp;
drh8aa34ae2006-03-13 12:54:09 +0000503 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
danielk19779e128002006-01-18 16:51:35 +0000504 if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000505 if (n != P3_KEYINFO) {
506 freeP3(n, (void*)*(char**)&zP3);
507 }
danielk1977d5d56522005-03-16 12:15:20 +0000508 return;
509 }
drh9a324642003-09-06 20:12:01 +0000510 if( addr<0 || addr>=p->nOp ){
511 addr = p->nOp - 1;
512 if( addr<0 ) return;
513 }
514 pOp = &p->aOp[addr];
drhb38ad992005-09-16 00:27:01 +0000515 freeP3(pOp->p3type, pOp->p3);
516 pOp->p3 = 0;
drh9a324642003-09-06 20:12:01 +0000517 if( zP3==0 ){
518 pOp->p3 = 0;
519 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000520 }else if( n==P3_KEYINFO ){
521 KeyInfo *pKeyInfo;
522 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000523
drhd3d39e92004-05-20 22:16:29 +0000524 nField = ((KeyInfo*)zP3)->nField;
drhfdd6e852005-12-16 01:06:16 +0000525 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drheafe05b2004-06-13 00:54:01 +0000526 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000527 pOp->p3 = (char*)pKeyInfo;
528 if( pKeyInfo ){
danielk1977bab45c62006-01-16 15:14:27 +0000529 unsigned char *aSortOrder;
drhd3d39e92004-05-20 22:16:29 +0000530 memcpy(pKeyInfo, zP3, nByte);
drhfdd6e852005-12-16 01:06:16 +0000531 aSortOrder = pKeyInfo->aSortOrder;
532 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000533 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000534 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
535 }
drhd3d39e92004-05-20 22:16:29 +0000536 pOp->p3type = P3_KEYINFO;
537 }else{
538 pOp->p3type = P3_NOTUSED;
539 }
drhffbc3082004-05-21 01:29:06 +0000540 }else if( n==P3_KEYINFO_HANDOFF ){
541 pOp->p3 = (char*)zP3;
542 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000543 }else if( n<0 ){
544 pOp->p3 = (char*)zP3;
545 pOp->p3type = n;
546 }else{
drhae29ffb2004-09-25 14:39:18 +0000547 if( n==0 ) n = strlen(zP3);
548 pOp->p3 = sqliteStrNDup(zP3, n);
drh9a324642003-09-06 20:12:01 +0000549 pOp->p3type = P3_DYNAMIC;
550 }
551}
552
drhad6d9462004-09-19 02:15:24 +0000553#ifndef NDEBUG
554/*
555** Replace the P3 field of the most recently coded instruction with
556** comment text.
557*/
558void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
559 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000560 assert( p->nOp>0 || p->aOp==0 );
561 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || sqlite3MallocFailed() );
drhad6d9462004-09-19 02:15:24 +0000562 va_start(ap, zFormat);
563 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
564 va_end(ap);
565}
566#endif
567
drh9a324642003-09-06 20:12:01 +0000568/*
drh9a324642003-09-06 20:12:01 +0000569** Return the opcode for a given address.
570*/
danielk19774adee202004-05-08 08:23:19 +0000571VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000572 assert( p->magic==VDBE_MAGIC_INIT );
danielk197701256832007-04-18 14:24:32 +0000573 assert( (addr>=0 && addr<p->nOp) || sqlite3MallocFailed() );
574 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000575}
576
drhb7f91642004-10-31 02:22:47 +0000577#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
578 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000579/*
drhd3d39e92004-05-20 22:16:29 +0000580** Compute a string that describes the P3 parameter for an opcode.
581** Use zTemp for any required temporary buffer space.
582*/
583static char *displayP3(Op *pOp, char *zTemp, int nTemp){
584 char *zP3;
585 assert( nTemp>=20 );
586 switch( pOp->p3type ){
drhd3d39e92004-05-20 22:16:29 +0000587 case P3_KEYINFO: {
588 int i, j;
589 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
590 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
591 i = strlen(zTemp);
592 for(j=0; j<pKeyInfo->nField; j++){
593 CollSeq *pColl = pKeyInfo->aColl[j];
594 if( pColl ){
595 int n = strlen(pColl->zName);
596 if( i+n>nTemp-6 ){
597 strcpy(&zTemp[i],",...");
598 break;
599 }
600 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000601 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000602 zTemp[i++] = '-';
603 }
604 strcpy(&zTemp[i], pColl->zName);
605 i += n;
606 }else if( i+4<nTemp-6 ){
607 strcpy(&zTemp[i],",nil");
608 i += 4;
609 }
610 }
611 zTemp[i++] = ')';
612 zTemp[i] = 0;
613 assert( i<nTemp );
614 zP3 = zTemp;
615 break;
616 }
617 case P3_COLLSEQ: {
618 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000619 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000620 zP3 = zTemp;
621 break;
622 }
drhf9b596e2004-05-26 16:54:42 +0000623 case P3_FUNCDEF: {
624 FuncDef *pDef = (FuncDef*)pOp->p3;
drha967e882006-06-13 01:04:52 +0000625 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000626 zP3 = zTemp;
627 break;
628 }
drha967e882006-06-13 01:04:52 +0000629#ifndef SQLITE_OMIT_VIRTUALTABLE
630 case P3_VTAB: {
631 sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
drh19146192006-06-26 19:10:32 +0000632 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000633 zP3 = zTemp;
634 break;
635 }
636#endif
drhd3d39e92004-05-20 22:16:29 +0000637 default: {
638 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000639 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000640 zP3 = "";
641 }
642 }
643 }
drhbdd88bd2006-06-15 13:22:22 +0000644 assert( zP3!=0 );
drhd3d39e92004-05-20 22:16:29 +0000645 return zP3;
646}
drhb7f91642004-10-31 02:22:47 +0000647#endif
drhd3d39e92004-05-20 22:16:29 +0000648
649
danielk19778b60e0f2005-01-12 09:10:39 +0000650#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000651/*
652** Print a single opcode. This routine is used for debugging only.
653*/
danielk19774adee202004-05-08 08:23:19 +0000654void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000655 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000656 char zPtr[50];
657 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000658 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000659 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000660 fprintf(pOut, zFormat1,
661 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000662 fflush(pOut);
663}
664#endif
665
666/*
drh76ff3a02004-09-24 22:32:30 +0000667** Release an array of N Mem elements
668*/
669static void releaseMemArray(Mem *p, int N){
670 if( p ){
671 while( N-->0 ){
672 sqlite3VdbeMemRelease(p++);
673 }
674 }
675}
676
drhb7f91642004-10-31 02:22:47 +0000677#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000678/*
drh9a324642003-09-06 20:12:01 +0000679** Give a listing of the program in the virtual machine.
680**
danielk19774adee202004-05-08 08:23:19 +0000681** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000682** running the code, it invokes the callback once for each instruction.
683** This feature is used to implement "EXPLAIN".
684*/
danielk19774adee202004-05-08 08:23:19 +0000685int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000686 Vdbe *p /* The VDBE */
687){
drh9bb575f2004-09-06 17:24:11 +0000688 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000689 int i;
drh826fb5a2004-02-14 23:59:57 +0000690 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000691
drh9a324642003-09-06 20:12:01 +0000692 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000693 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
694 assert( db->magic==SQLITE_MAGIC_BUSY );
695 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000696
697 /* Even though this opcode does not put dynamic strings onto the
698 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000699 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000700 */
701 if( p->pTos==&p->aStack[4] ){
drh76ff3a02004-09-24 22:32:30 +0000702 releaseMemArray(p->aStack, 5);
danielk197718f41892004-05-22 07:27:46 +0000703 }
danielk197718f41892004-05-22 07:27:46 +0000704 p->resOnStack = 0;
705
drhecc92422005-09-10 16:46:12 +0000706 do{
707 i = p->pc++;
708 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000709 if( i>=p->nOp ){
710 p->rc = SQLITE_OK;
711 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000712 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000713 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000714 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000715 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000716 }else{
drhd3d39e92004-05-20 22:16:29 +0000717 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000718 Mem *pMem = p->aStack;
719 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000720 pMem->type = SQLITE_INTEGER;
drh3c024d62007-03-30 11:23:45 +0000721 pMem->u.i = i; /* Program counter */
drheb2e1762004-05-27 01:53:56 +0000722 pMem++;
723
724 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
drh4f0c5872007-03-26 22:05:01 +0000725 pMem->z = (char*)sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
drhbdd88bd2006-06-15 13:22:22 +0000726 assert( pMem->z!=0 );
drheb2e1762004-05-27 01:53:56 +0000727 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000728 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000729 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000730 pMem++;
731
732 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000733 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000734 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000735 pMem++;
736
737 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000738 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000739 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000740 pMem++;
741
drhb8067982006-03-03 21:38:03 +0000742 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
drheb2e1762004-05-27 01:53:56 +0000743 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drhbdd88bd2006-06-15 13:22:22 +0000744 assert( pMem->z!=0 );
drhb8067982006-03-03 21:38:03 +0000745 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000746 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000747 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000748
drhecc92422005-09-10 16:46:12 +0000749 p->nResColumn = 5 - 2*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000750 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000751 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000752 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000753 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000754 }
drh826fb5a2004-02-14 23:59:57 +0000755 return rc;
drh9a324642003-09-06 20:12:01 +0000756}
drhb7f91642004-10-31 02:22:47 +0000757#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000758
drh7c4ac0c2007-04-05 11:25:58 +0000759#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000760/*
drh3f7d4e42004-07-24 14:35:58 +0000761** Print the SQL that was used to generate a VDBE program.
762*/
763void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000764 int nOp = p->nOp;
765 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000766 if( nOp<1 ) return;
767 pOp = &p->aOp[nOp-1];
drh3f7d4e42004-07-24 14:35:58 +0000768 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
769 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000770 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000771 printf("SQL: [%s]\n", z);
772 }
drh3f7d4e42004-07-24 14:35:58 +0000773}
drh7c4ac0c2007-04-05 11:25:58 +0000774#endif
drh3f7d4e42004-07-24 14:35:58 +0000775
drh602c2372007-03-01 00:29:13 +0000776#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
777/*
778** Print an IOTRACE message showing SQL content.
779*/
780void sqlite3VdbeIOTraceSql(Vdbe *p){
781 int nOp = p->nOp;
782 VdbeOp *pOp;
783 if( sqlite3_io_trace==0 ) return;
784 if( nOp<1 ) return;
785 pOp = &p->aOp[nOp-1];
786 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
787 char *z = sqlite3StrDup(pOp->p3);
788 int i, j;
789 for(i=0; isspace(z[i]); i++){}
790 for(j=0; z[i]; i++){
791 if( isspace(z[i]) ){
792 if( z[i-1]!=' ' ){
793 z[j++] = ' ';
794 }
795 }else{
796 z[j++] = z[i];
797 }
798 }
799 z[j] = 0;
800 sqlite3_io_trace("SQL %s\n", z);
801 sqliteFree(z);
802 }
803}
804#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
805
806
drh3f7d4e42004-07-24 14:35:58 +0000807/*
drh9a324642003-09-06 20:12:01 +0000808** Prepare a virtual machine for execution. This involves things such
809** as allocating stack space and initializing the program counter.
810** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000811** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000812**
813** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
814** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000815*/
danielk19774adee202004-05-08 08:23:19 +0000816void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000817 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000818 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000819 int nMem, /* Number of memory cells to allocate */
820 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000821 int isExplain /* True if the EXPLAIN keywords is present */
822){
823 int n;
824
825 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000826 assert( p->magic==VDBE_MAGIC_INIT );
827
drhc16a03b2004-09-15 13:38:10 +0000828 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000829 */
drhc16a03b2004-09-15 13:38:10 +0000830 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000831
danielk1977634f2982005-03-28 08:44:07 +0000832 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
833 * is because the call to resizeOpArray() below may shrink the
834 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
835 * state.
836 */
837 p->magic = VDBE_MAGIC_RUN;
838
drh9a324642003-09-06 20:12:01 +0000839 /* No instruction ever pushes more than a single element onto the
840 ** stack. And the stack never grows on successive executions of the
841 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000842 ** on the maximum stack depth required. (Added later:) The
843 ** resolveP2Values() call computes a tighter upper bound on the
844 ** stack size.
drh9a324642003-09-06 20:12:01 +0000845 **
846 ** Allocation all the stack space we will ever need.
847 */
drh82a48512003-09-06 22:45:20 +0000848 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000849 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000850 int nStack; /* Maximum number of stack entries required */
851 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000852 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000853 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000854 assert( nStack<p->nOp );
drh0f7eb612006-08-08 13:51:43 +0000855 if( isExplain ){
856 nStack = 10;
857 }
drh82a48512003-09-06 22:45:20 +0000858 p->aStack = sqliteMalloc(
danielk1977bc04f852005-03-29 08:26:13 +0000859 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000860 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000861 + nVar*sizeof(Mem) /* aVar */
862 + nVar*sizeof(char*) /* azVar */
863 + nMem*sizeof(Mem) /* aMem */
864 + nCursor*sizeof(Cursor*) /* apCsr */
drh82a48512003-09-06 22:45:20 +0000865 );
danielk19779e128002006-01-18 16:51:35 +0000866 if( !sqlite3MallocFailed() ){
danielk1977bc04f852005-03-29 08:26:13 +0000867 p->aMem = &p->aStack[nStack];
drh290c1942004-08-21 17:54:45 +0000868 p->nMem = nMem;
drh86f43302004-10-05 17:37:36 +0000869 p->aVar = &p->aMem[nMem];
870 p->nVar = nVar;
871 p->okVar = 0;
872 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000873 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000874 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000875 p->nCursor = nCursor;
876 for(n=0; n<nVar; n++){
877 p->aVar[n].flags = MEM_Null;
878 }
danielk197754db47e2004-05-19 10:36:43 +0000879 }
drh82a48512003-09-06 22:45:20 +0000880 }
danielk1977b3bce662005-01-29 08:32:43 +0000881 for(n=0; n<p->nMem; n++){
882 p->aMem[n].flags = MEM_Null;
883 }
drh9a324642003-09-06 20:12:01 +0000884
drh6810ce62004-01-31 19:22:56 +0000885 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000886 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000887 p->rc = SQLITE_OK;
888 p->uniqueCnt = 0;
889 p->returnDepth = 0;
890 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000891 p->popStack = 0;
892 p->explain |= isExplain;
893 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000894 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +0000895 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +0000896 p->minWriteFileFormat = 255;
drh9a324642003-09-06 20:12:01 +0000897#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000898 {
899 int i;
900 for(i=0; i<p->nOp; i++){
901 p->aOp[i].cnt = 0;
902 p->aOp[i].cycles = 0;
903 }
drh9a324642003-09-06 20:12:01 +0000904 }
905#endif
906}
907
drh9a324642003-09-06 20:12:01 +0000908/*
drh9a324642003-09-06 20:12:01 +0000909** Close a cursor and release all the resources that cursor happens
910** to hold.
911*/
danielk1977be718892006-06-23 08:05:19 +0000912void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
drh4774b132004-06-12 20:12:51 +0000913 if( pCx==0 ){
914 return;
915 }
drh9a324642003-09-06 20:12:01 +0000916 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000917 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000918 }
919 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000920 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000921 }
drh9eff6162006-06-12 21:59:13 +0000922#ifndef SQLITE_OMIT_VIRTUALTABLE
923 if( pCx->pVtabCursor ){
924 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +0000925 const sqlite3_module *pModule = pCx->pModule;
926 p->inVtabMethod = 1;
danielk19775bd270b2006-07-25 15:14:52 +0000927 sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +0000928 pModule->xClose(pVtabCursor);
danielk19775bd270b2006-07-25 15:14:52 +0000929 sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +0000930 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +0000931 }
932#endif
drh9a324642003-09-06 20:12:01 +0000933 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000934 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000935 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000936}
937
938/*
939** Close all cursors
940*/
941static void closeAllCursors(Vdbe *p){
942 int i;
drh290c1942004-08-21 17:54:45 +0000943 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000944 for(i=0; i<p->nCursor; i++){
danielk1977be718892006-06-23 08:05:19 +0000945 if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){
946 sqlite3VdbeFreeCursor(p, p->apCsr[i]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000947 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +0000948 }
drh9a324642003-09-06 20:12:01 +0000949 }
drh9a324642003-09-06 20:12:01 +0000950}
951
952/*
drh9a324642003-09-06 20:12:01 +0000953** Clean up the VM after execution.
954**
955** This routine will automatically close any cursors, lists, and/or
956** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000957** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000958*/
959static void Cleanup(Vdbe *p){
960 int i;
drh6810ce62004-01-31 19:22:56 +0000961 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +0000962 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
963 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +0000964 }
drh9a324642003-09-06 20:12:01 +0000965 closeAllCursors(p);
drh76ff3a02004-09-24 22:32:30 +0000966 releaseMemArray(p->aMem, p->nMem);
drha01f79d2005-07-08 13:07:59 +0000967 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +0000968 if( p->contextStack ){
969 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +0000970 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +0000971 }
972 sqliteFree(p->contextStack);
drh344737f2004-09-19 00:50:20 +0000973 }
drh5f968432004-02-21 19:02:30 +0000974 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +0000975 p->contextStackDepth = 0;
976 p->contextStackTop = 0;
drh9a324642003-09-06 20:12:01 +0000977 sqliteFree(p->zErrMsg);
978 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000979}
980
981/*
danielk197722322fd2004-05-25 23:35:17 +0000982** Set the number of result columns that will be returned by this SQL
983** statement. This is now set at compile time, rather than during
984** execution of the vdbe program so that sqlite3_column_count() can
985** be called on an SQL statement before sqlite3_step().
986*/
987void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +0000988 Mem *pColName;
989 int n;
danielk1977955de522006-02-10 02:27:42 +0000990 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drhcc43cab2005-10-05 11:35:09 +0000991 sqliteFree(p->aColName);
danielk1977955de522006-02-10 02:27:42 +0000992 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +0000993 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +0000994 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
995 if( p->aColName==0 ) return;
996 while( n-- > 0 ){
997 (pColName++)->flags = MEM_Null;
998 }
danielk197722322fd2004-05-25 23:35:17 +0000999}
1000
1001/*
danielk19773cf86062004-05-26 10:11:05 +00001002** Set the name of the idx'th column to be returned by the SQL statement.
1003** zName must be a pointer to a nul terminated string.
1004**
1005** This call must be made after a call to sqlite3VdbeSetNumCols().
1006**
danielk1977d8123362004-06-12 09:25:12 +00001007** If N==P3_STATIC it means that zName is a pointer to a constant static
1008** string and we can just copy the pointer. If it is P3_DYNAMIC, then
1009** the string is freed using sqliteFree() when the vdbe is finished with
1010** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001011*/
danielk1977955de522006-02-10 02:27:42 +00001012int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001013 int rc;
1014 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001015 assert( idx<p->nResColumn );
1016 assert( var<COLNAME_N );
danielk19779e128002006-01-18 16:51:35 +00001017 if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001018 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001019 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk1977d8123362004-06-12 09:25:12 +00001020 if( N==P3_DYNAMIC || N==P3_STATIC ){
1021 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001022 }else{
danielk1977d8123362004-06-12 09:25:12 +00001023 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001024 }
1025 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
1026 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00001027 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +00001028 }
1029 return rc;
1030}
1031
danielk197713adf8a2004-06-03 16:08:41 +00001032/*
1033** A read or write transaction may or may not be active on database handle
1034** db. If a transaction is active, commit it. If there is a
1035** write-transaction spanning more than one database file, this routine
1036** takes care of the master journal trickery.
1037*/
drh9bb575f2004-09-06 17:24:11 +00001038static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001039 int i;
1040 int nTrans = 0; /* Number of databases with an active write-transaction */
1041 int rc = SQLITE_OK;
1042 int needXcommit = 0;
1043
danielk19775bd270b2006-07-25 15:14:52 +00001044 /* Before doing anything else, call the xSync() callback for any
1045 ** virtual module tables written in this transaction. This has to
1046 ** be done before determining whether a master journal file is
1047 ** required, as an xSync() callback may add an attached database
1048 ** to the transaction.
1049 */
1050 rc = sqlite3VtabSync(db, rc);
1051 if( rc!=SQLITE_OK ){
1052 return rc;
1053 }
1054
1055 /* This loop determines (a) if the commit hook should be invoked and
1056 ** (b) how many database files have open write transactions, not
1057 ** including the temp database. (b) is important because if more than
1058 ** one database file has an open write transaction, a master journal
1059 ** file is required for an atomic commit.
1060 */
danielk197713adf8a2004-06-03 16:08:41 +00001061 for(i=0; i<db->nDb; i++){
1062 Btree *pBt = db->aDb[i].pBt;
1063 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1064 needXcommit = 1;
1065 if( i!=1 ) nTrans++;
1066 }
1067 }
1068
1069 /* If there are any write-transactions at all, invoke the commit hook */
1070 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001071 sqlite3SafetyOff(db);
1072 rc = db->xCommitCallback(db->pCommitArg);
1073 sqlite3SafetyOn(db);
1074 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001075 return SQLITE_CONSTRAINT;
1076 }
1077 }
1078
danielk197740b38dc2004-06-26 08:38:24 +00001079 /* The simple case - no more than one database file (not counting the
1080 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001081 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001082 **
danielk197740b38dc2004-06-26 08:38:24 +00001083 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1084 ** string, it means the main database is :memory:. In that case we do
1085 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +00001086 ** too.
danielk197713adf8a2004-06-03 16:08:41 +00001087 */
danielk197740b38dc2004-06-26 08:38:24 +00001088 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001089 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001090 Btree *pBt = db->aDb[i].pBt;
1091 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001092 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001093 }
1094 }
1095
drh80e35f42007-03-30 14:06:34 +00001096 /* Do the commit only if all databases successfully complete phase 1.
1097 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1098 ** IO error while deleting or truncating a journal file. It is unlikely,
1099 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001100 */
1101 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1102 Btree *pBt = db->aDb[i].pBt;
1103 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001104 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001105 }
danielk1977979f38e2007-03-27 16:19:51 +00001106 }
1107 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001108 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001109 }
1110 }
1111
1112 /* The complex case - There is a multi-file write-transaction active.
1113 ** This requires a master journal file to ensure the transaction is
1114 ** committed atomicly.
1115 */
danielk197744ee5bf2005-05-27 09:41:12 +00001116#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001117 else{
drh2c8997b2005-08-27 16:36:48 +00001118 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001119 char *zMaster = 0; /* File-name for the master journal */
1120 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
drh9cbe6352005-11-29 03:13:21 +00001121 OsFile *master = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001122
1123 /* Select a master journal file name */
1124 do {
drha6abd042004-06-09 17:37:22 +00001125 u32 random;
1126 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001127 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +00001128 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001129 if( !zMaster ){
1130 return SQLITE_NOMEM;
1131 }
drh66560ad2006-01-06 14:32:19 +00001132 }while( sqlite3OsFileExists(zMaster) );
danielk197713adf8a2004-06-03 16:08:41 +00001133
1134 /* Open the master journal. */
drh66560ad2006-01-06 14:32:19 +00001135 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001136 if( rc!=SQLITE_OK ){
1137 sqliteFree(zMaster);
1138 return rc;
1139 }
1140
1141 /* Write the name of each database file in the transaction into the new
1142 ** master journal file. If an error occurs at this point close
1143 ** and delete the master journal file. All the individual journal files
1144 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001145 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001146 */
1147 for(i=0; i<db->nDb; i++){
1148 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001149 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001150 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001151 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001152 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001153 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1154 needSync = 1;
1155 }
drh054889e2005-11-30 03:20:31 +00001156 rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001157 if( rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001158 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001159 sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001160 sqliteFree(zMaster);
1161 return rc;
1162 }
1163 }
1164 }
1165
danielk197713adf8a2004-06-03 16:08:41 +00001166
danielk19775865e3d2004-06-14 06:03:57 +00001167 /* Sync the master journal file. Before doing this, open the directory
1168 ** the master journal file is store in so that it gets synced too.
1169 */
1170 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
drh054889e2005-11-30 03:20:31 +00001171 rc = sqlite3OsOpenDirectory(master, zMainFile);
drheb796a72005-09-08 12:38:41 +00001172 if( rc!=SQLITE_OK ||
drh054889e2005-11-30 03:20:31 +00001173 (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
1174 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001175 sqlite3OsDelete(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001176 sqliteFree(zMaster);
1177 return rc;
1178 }
drhc9e06862004-06-09 20:03:08 +00001179
danielk197713adf8a2004-06-03 16:08:41 +00001180 /* Sync all the db files involved in the transaction. The same call
1181 ** sets the master journal pointer in each individual journal. If
1182 ** an error occurs here, do not delete the master journal file.
1183 **
drh80e35f42007-03-30 14:06:34 +00001184 ** If the error occurs during the first call to
1185 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1186 ** master journal file will be orphaned. But we cannot delete it,
1187 ** in case the master journal file name was written into the journal
1188 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001189 */
danielk19775bd270b2006-07-25 15:14:52 +00001190 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001191 Btree *pBt = db->aDb[i].pBt;
1192 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
drh80e35f42007-03-30 14:06:34 +00001193 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001194 }
1195 }
drh054889e2005-11-30 03:20:31 +00001196 sqlite3OsClose(&master);
danielk19775bd270b2006-07-25 15:14:52 +00001197 if( rc!=SQLITE_OK ){
1198 sqliteFree(zMaster);
1199 return rc;
1200 }
danielk197713adf8a2004-06-03 16:08:41 +00001201
danielk1977962398d2004-06-14 09:35:16 +00001202 /* Delete the master journal file. This commits the transaction. After
1203 ** doing this the directory is synced again before any individual
1204 ** transaction files are deleted.
1205 */
drh66560ad2006-01-06 14:32:19 +00001206 rc = sqlite3OsDelete(zMaster);
drhc416ba92007-03-30 18:42:55 +00001207 sqliteFree(zMaster);
1208 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001209 if( rc ){
1210 return rc;
1211 }
drh66560ad2006-01-06 14:32:19 +00001212 rc = sqlite3OsSyncDirectory(zMainFile);
danielk1977962398d2004-06-14 09:35:16 +00001213 if( rc!=SQLITE_OK ){
1214 /* This is not good. The master journal file has been deleted, but
1215 ** the directory sync failed. There is no completely safe course of
1216 ** action from here. The individual journals contain the name of the
1217 ** master journal file, but there is no way of knowing if that
1218 ** master journal exists now or if it will exist after the operating
1219 ** system crash that may follow the fsync() failure.
1220 */
danielk1977962398d2004-06-14 09:35:16 +00001221 return rc;
1222 }
danielk197713adf8a2004-06-03 16:08:41 +00001223
1224 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001225 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1226 ** deleting or truncating journals. If something goes wrong while
1227 ** this is happening we don't really care. The integrity of the
1228 ** transaction is already guaranteed, but some stray 'cold' journals
1229 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001230 */
danielk1977979f38e2007-03-27 16:19:51 +00001231 disable_simulated_io_errors();
danielk197713adf8a2004-06-03 16:08:41 +00001232 for(i=0; i<db->nDb; i++){
1233 Btree *pBt = db->aDb[i].pBt;
1234 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001235 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001236 }
1237 }
danielk1977979f38e2007-03-27 16:19:51 +00001238 enable_simulated_io_errors();
1239
danielk1977f9e7dda2006-06-16 16:08:53 +00001240 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001241 }
danielk197744ee5bf2005-05-27 09:41:12 +00001242#endif
danielk1977026d2702004-06-14 13:14:59 +00001243
drh2ac3ee92004-06-07 16:27:46 +00001244 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001245}
1246
danielk19771d850a72004-05-31 08:26:49 +00001247/*
1248** This routine checks that the sqlite3.activeVdbeCnt count variable
1249** matches the number of vdbe's in the list sqlite3.pVdbe that are
1250** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001251** This is an internal self-check only - it is not an essential processing
1252** step.
danielk19771d850a72004-05-31 08:26:49 +00001253**
1254** This is a no-op if NDEBUG is defined.
1255*/
1256#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001257static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001258 Vdbe *p;
1259 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001260 p = db->pVdbe;
1261 while( p ){
drh92f02c32004-09-02 14:57:08 +00001262 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001263 cnt++;
1264 }
1265 p = p->pNext;
1266 }
danielk19771d850a72004-05-31 08:26:49 +00001267 assert( cnt==db->activeVdbeCnt );
1268}
1269#else
1270#define checkActiveVdbeCnt(x)
1271#endif
1272
danielk19773cf86062004-05-26 10:11:05 +00001273/*
danielk1977be718892006-06-23 08:05:19 +00001274** Find every active VM other than pVdbe and change its status to
1275** aborted. This happens when one VM causes a rollback due to an
1276** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1277** aborted so that they do not have data rolled out from underneath
1278** them leading to a segfault.
1279*/
1280void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
1281 Vdbe *pOther;
1282 for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
1283 if( pOther==pExcept ) continue;
1284 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1285 checkActiveVdbeCnt(db);
1286 closeAllCursors(pOther);
1287 checkActiveVdbeCnt(db);
1288 pOther->aborted = 1;
1289 }
1290}
1291
1292/*
drh92f02c32004-09-02 14:57:08 +00001293** This routine is called the when a VDBE tries to halt. If the VDBE
1294** has made changes and is in autocommit mode, then commit those
1295** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001296**
drh92f02c32004-09-02 14:57:08 +00001297** This routine is the only way to move the state of a VM from
1298** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1299**
1300** Return an error code. If the commit could not complete because of
1301** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1302** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001303*/
drh92f02c32004-09-02 14:57:08 +00001304int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001305 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001306 int i;
danielk19771d850a72004-05-31 08:26:49 +00001307 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001308 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1309
1310 /* This function contains the logic that determines if a statement or
1311 ** transaction will be committed or rolled back as a result of the
1312 ** execution of this virtual machine.
1313 **
1314 ** Special errors:
1315 **
1316 ** If an SQLITE_NOMEM error has occured in a statement that writes to
1317 ** the database, then either a statement or transaction must be rolled
1318 ** back to ensure the tree-structures are in a consistent state. A
1319 ** statement transaction is rolled back if one is open, otherwise the
1320 ** entire transaction must be rolled back.
1321 **
1322 ** If an SQLITE_IOERR error has occured in a statement that writes to
1323 ** the database, then the entire transaction must be rolled back. The
1324 ** I/O error may have caused garbage to be written to the journal
1325 ** file. Were the transaction to continue and eventually be rolled
1326 ** back that garbage might end up in the database file.
1327 **
1328 ** In both of the above cases, the Vdbe.errorAction variable is
1329 ** ignored. If the sqlite3.autoCommit flag is false and a transaction
1330 ** is rolled back, it will be set to true.
1331 **
1332 ** Other errors:
1333 **
1334 ** No error:
1335 **
1336 */
drh9a324642003-09-06 20:12:01 +00001337
danielk19779e128002006-01-18 16:51:35 +00001338 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +00001339 p->rc = SQLITE_NOMEM;
1340 }
drh92f02c32004-09-02 14:57:08 +00001341 if( p->magic!=VDBE_MAGIC_RUN ){
1342 /* Already halted. Nothing to do. */
1343 assert( p->magic==VDBE_MAGIC_HALT );
danielk1977b7a2f2e2006-06-23 11:34:54 +00001344#ifndef SQLITE_OMIT_VIRTUALTABLE
1345 closeAllCursors(p);
1346#endif
drh92f02c32004-09-02 14:57:08 +00001347 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001348 }
drh92f02c32004-09-02 14:57:08 +00001349 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001350 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001351
danielk197707cb5602006-01-20 10:55:05 +00001352 /* No commit or rollback needed if the program never started */
1353 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001354 int mrc; /* Primary error code from p->rc */
danielk197707cb5602006-01-20 10:55:05 +00001355 /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
drhaac2f552006-09-23 21:44:23 +00001356 mrc = p->rc & 0xff;
1357 isSpecialError = ((mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR)?1:0);
danielk197707cb5602006-01-20 10:55:05 +00001358 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001359 /* This loop does static analysis of the query to see which of the
1360 ** following three categories it falls into:
1361 **
1362 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001363 ** Query with statement journal
1364 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001365 **
1366 ** We could do something more elegant than this static analysis (i.e.
1367 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001368 ** handling malloc() or IO failure is a fairly obscure edge case so
1369 ** this is probably easier. Todo: Might be an opportunity to reduce
1370 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001371 */
1372 int isReadOnly = 1;
1373 int isStatement = 0;
1374 assert(p->aOp || p->nOp==0);
1375 for(i=0; i<p->nOp; i++){
1376 switch( p->aOp[i].opcode ){
1377 case OP_Transaction:
1378 isReadOnly = 0;
1379 break;
1380 case OP_Statement:
1381 isStatement = 1;
1382 break;
1383 }
1384 }
danielk197707cb5602006-01-20 10:55:05 +00001385
1386 /* If the query was read-only, we need do no rollback at all. Otherwise,
1387 ** proceed with the special handling.
1388 */
1389 if( !isReadOnly ){
1390 if( p->rc==SQLITE_NOMEM && isStatement ){
1391 xFunc = sqlite3BtreeRollbackStmt;
1392 }else{
1393 /* We are forced to roll back the active transaction. Before doing
1394 ** so, abort any other statements this handle currently has active.
1395 */
danielk19778d34dfd2006-01-24 16:37:57 +00001396 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001397 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001398 db->autoCommit = 1;
1399 }
danielk1977261919c2005-12-06 12:52:59 +00001400 }
1401 }
danielk197707cb5602006-01-20 10:55:05 +00001402
1403 /* If the auto-commit flag is set and this is the only active vdbe, then
1404 ** we do either a commit or rollback of the current transaction.
1405 **
1406 ** Note: This block also runs if one of the special errors handled
1407 ** above has occured.
1408 */
1409 if( db->autoCommit && db->activeVdbeCnt==1 ){
1410 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1411 /* The auto-commit flag is true, and the vdbe program was
1412 ** successful or hit an 'OR FAIL' constraint. This means a commit
1413 ** is required.
1414 */
1415 int rc = vdbeCommit(db);
1416 if( rc==SQLITE_BUSY ){
1417 return SQLITE_BUSY;
1418 }else if( rc!=SQLITE_OK ){
1419 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001420 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001421 }else{
1422 sqlite3CommitInternalChanges(db);
1423 }
1424 }else{
danielk197797a227c2006-01-20 16:32:04 +00001425 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001426 }
1427 }else if( !xFunc ){
1428 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1429 xFunc = sqlite3BtreeCommitStmt;
1430 }else if( p->errorAction==OE_Abort ){
1431 xFunc = sqlite3BtreeRollbackStmt;
1432 }else{
danielk19778d34dfd2006-01-24 16:37:57 +00001433 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001434 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001435 db->autoCommit = 1;
1436 }
danielk19771d850a72004-05-31 08:26:49 +00001437 }
danielk197707cb5602006-01-20 10:55:05 +00001438
1439 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1440 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1441 ** and the return code is still SQLITE_OK, set the return code to the new
1442 ** error value.
1443 */
1444 assert(!xFunc ||
1445 xFunc==sqlite3BtreeCommitStmt ||
1446 xFunc==sqlite3BtreeRollbackStmt
1447 );
1448 for(i=0; xFunc && i<db->nDb; i++){
1449 int rc;
1450 Btree *pBt = db->aDb[i].pBt;
1451 if( pBt ){
1452 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001453 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1454 p->rc = rc;
1455 sqlite3SetString(&p->zErrMsg, 0);
1456 }
danielk197707cb5602006-01-20 10:55:05 +00001457 }
danielk197777d83ba2004-05-31 10:08:14 +00001458 }
danielk197707cb5602006-01-20 10:55:05 +00001459
1460 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1461 ** set the change counter.
1462 */
1463 if( p->changeCntOn && p->pc>=0 ){
1464 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1465 sqlite3VdbeSetChanges(db, p->nChange);
1466 }else{
1467 sqlite3VdbeSetChanges(db, 0);
1468 }
1469 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001470 }
danielk197707cb5602006-01-20 10:55:05 +00001471
1472 /* Rollback or commit any schema changes that occurred. */
1473 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1474 sqlite3ResetInternalSchema(db, 0);
1475 db->flags = (db->flags | SQLITE_InternChanges);
1476 }
drh9a324642003-09-06 20:12:01 +00001477 }
danielk19771d850a72004-05-31 08:26:49 +00001478
danielk197765fd59f2006-06-24 11:51:33 +00001479 /* We have successfully halted and closed the VM. Record this fact. */
1480 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001481 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001482 }
drh92f02c32004-09-02 14:57:08 +00001483 p->magic = VDBE_MAGIC_HALT;
1484 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001485
drh92f02c32004-09-02 14:57:08 +00001486 return SQLITE_OK;
1487}
1488
1489/*
drh3c23a882007-01-09 14:01:13 +00001490** Each VDBE holds the result of the most recent sqlite3_step() call
1491** in p->rc. This routine sets that result back to SQLITE_OK.
1492*/
1493void sqlite3VdbeResetStepResult(Vdbe *p){
1494 p->rc = SQLITE_OK;
1495}
1496
1497/*
drh92f02c32004-09-02 14:57:08 +00001498** Clean up a VDBE after execution but do not delete the VDBE just yet.
1499** Write any error messages into *pzErrMsg. Return the result code.
1500**
1501** After this routine is run, the VDBE should be ready to be executed
1502** again.
1503**
1504** To look at it another way, this routine resets the state of the
1505** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1506** VDBE_MAGIC_INIT.
1507*/
1508int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001509 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001510 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001511
1512 /* If the VM did not run to completion or if it encountered an
1513 ** error, then it might not have been halted properly. So halt
1514 ** it now.
1515 */
drh4ac285a2006-09-15 07:28:50 +00001516 sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001517 sqlite3VdbeHalt(p);
drh4ac285a2006-09-15 07:28:50 +00001518 sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001519
drhfb7e7652005-01-24 00:28:42 +00001520 /* If the VDBE has be run even partially, then transfer the error code
1521 ** and error message from the VDBE into the main database structure. But
1522 ** if the VDBE has just been set to run but has not actually executed any
1523 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001524 */
drhfb7e7652005-01-24 00:28:42 +00001525 if( p->pc>=0 ){
1526 if( p->zErrMsg ){
danielk197797a227c2006-01-20 16:32:04 +00001527 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
1528 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001529 p->zErrMsg = 0;
1530 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001531 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001532 }else{
drh4ac285a2006-09-15 07:28:50 +00001533 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001534 }
danielk1977a21c6b62005-01-24 10:25:59 +00001535 }else if( p->rc && p->expired ){
1536 /* The expired flag was set on the VDBE before the first call
1537 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1538 ** called), set the database error in this case as well.
1539 */
drh4ac285a2006-09-15 07:28:50 +00001540 sqlite3Error(db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001541 }
1542
1543 /* Reclaim all memory used by the VDBE
1544 */
1545 Cleanup(p);
1546
1547 /* Save profiling information from this VDBE run.
1548 */
danielk1977261919c2005-12-06 12:52:59 +00001549 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
drh9a324642003-09-06 20:12:01 +00001550#ifdef VDBE_PROFILE
1551 {
1552 FILE *out = fopen("vdbe_profile.out", "a");
1553 if( out ){
1554 int i;
1555 fprintf(out, "---- ");
1556 for(i=0; i<p->nOp; i++){
1557 fprintf(out, "%02x", p->aOp[i].opcode);
1558 }
1559 fprintf(out, "\n");
1560 for(i=0; i<p->nOp; i++){
1561 fprintf(out, "%6d %10lld %8lld ",
1562 p->aOp[i].cnt,
1563 p->aOp[i].cycles,
1564 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1565 );
danielk19774adee202004-05-08 08:23:19 +00001566 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001567 }
1568 fclose(out);
1569 }
1570 }
1571#endif
1572 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001573 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001574 if( p->rc==SQLITE_SCHEMA ){
drh4ac285a2006-09-15 07:28:50 +00001575 sqlite3ResetInternalSchema(db, 0);
drh178286b2005-01-23 13:14:55 +00001576 }
drh4ac285a2006-09-15 07:28:50 +00001577 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001578}
drh92f02c32004-09-02 14:57:08 +00001579
drh9a324642003-09-06 20:12:01 +00001580/*
1581** Clean up and delete a VDBE after execution. Return an integer which is
1582** the result code. Write any error message text into *pzErrMsg.
1583*/
danielk19779e6db7d2004-06-21 08:18:51 +00001584int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001585 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001586 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1587 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001588 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001589 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001590 return SQLITE_MISUSE;
1591 }
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001593 return rc;
1594}
1595
1596/*
drhf92c7ff2004-06-19 15:40:23 +00001597** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001598** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001599** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001600** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001601*/
1602void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1603 int i;
1604 for(i=0; i<pVdbeFunc->nAux; i++){
1605 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1606 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1607 if( pAux->xDelete ){
1608 pAux->xDelete(pAux->pAux);
1609 }
1610 pAux->pAux = 0;
1611 }
1612 }
1613}
1614
1615/*
drh9a324642003-09-06 20:12:01 +00001616** Delete an entire VDBE.
1617*/
danielk19774adee202004-05-08 08:23:19 +00001618void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001619 int i;
1620 if( p==0 ) return;
1621 Cleanup(p);
1622 if( p->pPrev ){
1623 p->pPrev->pNext = p->pNext;
1624 }else{
1625 assert( p->db->pVdbe==p );
1626 p->db->pVdbe = p->pNext;
1627 }
1628 if( p->pNext ){
1629 p->pNext->pPrev = p->pPrev;
1630 }
drh76ff3a02004-09-24 22:32:30 +00001631 if( p->aOp ){
1632 for(i=0; i<p->nOp; i++){
1633 Op *pOp = &p->aOp[i];
drhb38ad992005-09-16 00:27:01 +00001634 freeP3(pOp->p3type, pOp->p3);
drh9a324642003-09-06 20:12:01 +00001635 }
drh76ff3a02004-09-24 22:32:30 +00001636 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001637 }
drh76ff3a02004-09-24 22:32:30 +00001638 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001639 sqliteFree(p->aLabel);
1640 sqliteFree(p->aStack);
danielk1977955de522006-02-10 02:27:42 +00001641 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh76ff3a02004-09-24 22:32:30 +00001642 sqliteFree(p->aColName);
drhb900aaf2006-11-09 00:24:53 +00001643 sqliteFree(p->zSql);
drh9a324642003-09-06 20:12:01 +00001644 p->magic = VDBE_MAGIC_DEAD;
1645 sqliteFree(p);
1646}
drha11846b2004-01-07 18:52:56 +00001647
1648/*
drha11846b2004-01-07 18:52:56 +00001649** If a MoveTo operation is pending on the given cursor, then do that
1650** MoveTo now. Return an error code. If no MoveTo is pending, this
1651** routine does nothing and returns SQLITE_OK.
1652*/
danielk19774adee202004-05-08 08:23:19 +00001653int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001654 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001655 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001656#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001657 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001658#endif
drhf0863fe2005-06-12 21:35:51 +00001659 assert( p->isTable );
drh5f9c1a22007-04-04 01:27:44 +00001660 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001661 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001662 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001663 p->lastRowid = keyToInt(p->movetoTarget);
1664 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001665 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001666 rc = sqlite3BtreeNext(p->pCursor, &res);
1667 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001668 }
drh10cfdd52006-08-08 15:42:59 +00001669#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001670 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001671#endif
drha11846b2004-01-07 18:52:56 +00001672 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001673 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001674 }
1675 return SQLITE_OK;
1676}
danielk19774adee202004-05-08 08:23:19 +00001677
drhab9f7f12004-05-08 10:56:11 +00001678/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001679** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001680**
danielk1977cfcdaef2004-05-12 07:33:33 +00001681** sqlite3VdbeSerialType()
1682** sqlite3VdbeSerialTypeLen()
1683** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001684** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001685** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001686**
1687** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001688** data and index records. Each serialized value consists of a
1689** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1690** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001691**
danielk1977cfcdaef2004-05-12 07:33:33 +00001692** In an SQLite index record, the serial type is stored directly before
1693** the blob of data that it corresponds to. In a table record, all serial
1694** types are stored at the start of the record, and the blobs of data at
1695** the end. Hence these functions allow the caller to handle the
1696** serial-type and data blob seperately.
1697**
1698** The following table describes the various storage classes for data:
1699**
1700** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001701** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001702** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001703** 1 1 signed integer
1704** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001705** 3 3 signed integer
1706** 4 4 signed integer
1707** 5 6 signed integer
1708** 6 8 signed integer
1709** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001710** 8 0 Integer constant 0
1711** 9 0 Integer constant 1
1712** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001713** N>=12 and even (N-12)/2 BLOB
1714** N>=13 and odd (N-13)/2 text
1715**
drh35a59652006-01-02 18:24:40 +00001716** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1717** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001718*/
1719
1720/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001721** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001722*/
drhd946db02005-12-29 19:23:06 +00001723u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001724 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001725 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001726
1727 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001728 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001729 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001730 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001731 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001732# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001733 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001734 u64 u;
1735 if( file_format>=4 && (i&1)==i ){
1736 return 8+i;
1737 }
1738 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001739 if( u<=127 ) return 1;
1740 if( u<=32767 ) return 2;
1741 if( u<=8388607 ) return 3;
1742 if( u<=2147483647 ) return 4;
1743 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001744 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001745 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001746 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001747 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001748 }
drhfdf972a2007-05-02 13:30:27 +00001749 assert( flags&(MEM_Str|MEM_Blob) );
1750 n = pMem->n;
1751 if( flags & MEM_Zero ){
1752 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001753 }
drhfdf972a2007-05-02 13:30:27 +00001754 assert( n>=0 );
1755 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001756}
1757
1758/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001759** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001760*/
drh25aa1b42004-05-28 01:39:01 +00001761int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001762 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001763 return (serial_type-12)/2;
1764 }else{
drh57196282004-10-06 15:41:16 +00001765 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001766 return aSize[serial_type];
1767 }
danielk1977192ac1d2004-05-10 07:17:30 +00001768}
1769
1770/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001771** Write the serialized data blob for the value stored in pMem into
1772** buf. It is assumed that the caller has allocated sufficient space.
1773** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00001774**
1775** nBuf is the amount of space left in buf[]. nBuf must always be
1776** large enough to hold the entire field. Except, if the field is
1777** a blob with a zero-filled tail, then buf[] might be just the right
1778** size to hold everything except for the zero-filled tail. If buf[]
1779** is only big enough to hold the non-zero prefix, then only write that
1780** prefix into buf[]. But if buf[] is large enough to hold both the
1781** prefix and the tail then write the prefix and set the tail to all
1782** zeros.
1783**
1784** Return the number of bytes actually written into buf[]. The number
1785** of bytes in the zero-filled tail is included in the return value only
1786** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00001787*/
drhfdf972a2007-05-02 13:30:27 +00001788int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00001789 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001790 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001791
drh1483e142004-05-21 21:12:42 +00001792 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001793 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001794 u64 v;
1795 int i;
drha19b7752004-05-30 21:14:58 +00001796 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00001797 assert( sizeof(v)==sizeof(pMem->r) );
1798 memcpy(&v, &pMem->r, sizeof(v));
drh1483e142004-05-21 21:12:42 +00001799 }else{
drh3c024d62007-03-30 11:23:45 +00001800 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001801 }
drh1483e142004-05-21 21:12:42 +00001802 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00001803 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00001804 while( i-- ){
1805 buf[i] = (v&0xFF);
1806 v >>= 8;
1807 }
1808 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001809 }
drhd946db02005-12-29 19:23:06 +00001810
danielk1977cfcdaef2004-05-12 07:33:33 +00001811 /* String or blob */
drhd946db02005-12-29 19:23:06 +00001812 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00001813 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1814 == sqlite3VdbeSerialTypeLen(serial_type) );
1815 assert( pMem->n<=nBuf );
1816 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00001817 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00001818 if( pMem->flags & MEM_Zero ){
1819 len += pMem->u.i;
1820 if( len>nBuf ){
1821 len = nBuf;
1822 }
1823 memset(&buf[pMem->n], 0, len-pMem->n);
1824 }
drhd946db02005-12-29 19:23:06 +00001825 return len;
1826 }
1827
1828 /* NULL or constants 0 or 1 */
1829 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001830}
1831
1832/*
1833** Deserialize the data blob pointed to by buf as serial type serial_type
1834** and store the result in pMem. Return the number of bytes read.
1835*/
danielk1977b1bc9532004-05-22 03:05:33 +00001836int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001837 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001838 u32 serial_type, /* Serial type to deserialize */
1839 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001840){
drh3c685822005-05-21 18:32:18 +00001841 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00001842 case 10: /* Reserved for future use */
1843 case 11: /* Reserved for future use */
1844 case 0: { /* NULL */
1845 pMem->flags = MEM_Null;
1846 break;
1847 }
1848 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00001849 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001850 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001851 return 1;
drh1483e142004-05-21 21:12:42 +00001852 }
drh3c685822005-05-21 18:32:18 +00001853 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00001854 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00001855 pMem->flags = MEM_Int;
1856 return 2;
1857 }
1858 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00001859 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00001860 pMem->flags = MEM_Int;
1861 return 3;
1862 }
1863 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00001864 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00001865 pMem->flags = MEM_Int;
1866 return 4;
1867 }
1868 case 5: { /* 6-byte signed integer */
1869 u64 x = (((signed char)buf[0])<<8) | buf[1];
1870 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1871 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00001872 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00001873 pMem->flags = MEM_Int;
1874 return 6;
1875 }
drh91124b32005-08-18 18:15:05 +00001876 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00001877 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00001878 u64 x;
1879 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00001880#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00001881 /* Verify that integers and floating point values use the same
drhbfd6b032005-08-28 01:38:44 +00001882 ** byte order. The byte order differs on some (broken) architectures.
1883 */
drhde941c62005-08-28 01:34:21 +00001884 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00001885 static const double r1 = 1.0;
1886 assert( sizeof(r1)==sizeof(t1) && memcmp(&r1, &t1, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00001887#endif
drhbfd6b032005-08-28 01:38:44 +00001888
drhd81bd4e2005-09-05 20:06:49 +00001889 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1890 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00001891 x = (x<<32) | y;
1892 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00001893 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00001894 pMem->flags = MEM_Int;
1895 }else{
drh4f0c5872007-03-26 22:05:01 +00001896 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
1897 memcpy(&pMem->r, &x, sizeof(x));
1898 /* pMem->r = *(double*)&x; */
drh3c685822005-05-21 18:32:18 +00001899 pMem->flags = MEM_Real;
1900 }
1901 return 8;
1902 }
drhd946db02005-12-29 19:23:06 +00001903 case 8: /* Integer 0 */
1904 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00001905 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00001906 pMem->flags = MEM_Int;
1907 return 0;
1908 }
drh3c685822005-05-21 18:32:18 +00001909 default: {
1910 int len = (serial_type-12)/2;
1911 pMem->z = (char *)buf;
1912 pMem->n = len;
1913 pMem->xDel = 0;
1914 if( serial_type&0x01 ){
1915 pMem->flags = MEM_Str | MEM_Ephem;
1916 }else{
1917 pMem->flags = MEM_Blob | MEM_Ephem;
1918 }
1919 return len;
drh696b32f2004-05-30 01:51:52 +00001920 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001921 }
drh3c685822005-05-21 18:32:18 +00001922 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001923}
1924
1925/*
drh0e6082e2006-01-12 20:28:35 +00001926** The header of a record consists of a sequence variable-length integers.
1927** These integers are almost always small and are encoded as a single byte.
1928** The following macro takes advantage this fact to provide a fast decode
1929** of the integers in a record header. It is faster for the common case
1930** where the integer is a single byte. It is a little slower when the
1931** integer is two or more bytes. But overall it is faster.
1932**
1933** The following expressions are equivalent:
1934**
1935** x = sqlite3GetVarint32( A, &B );
1936**
1937** x = GetVarint( A, B );
1938**
1939*/
1940#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
1941
1942/*
drh7a224de2004-06-02 01:22:02 +00001943** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001944** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1945** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001946** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1947** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001948*/
drh7a224de2004-06-02 01:22:02 +00001949int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001950 void *userData,
1951 int nKey1, const void *pKey1,
1952 int nKey2, const void *pKey2
1953){
drhd3d39e92004-05-20 22:16:29 +00001954 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001955 u32 d1, d2; /* Offset into aKey[] of next data element */
1956 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1957 u32 szHdr1, szHdr2; /* Number of bytes in header */
1958 int i = 0;
1959 int nField;
1960 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001961 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1962 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001963
1964 Mem mem1;
1965 Mem mem2;
1966 mem1.enc = pKeyInfo->enc;
1967 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001968
drh0e6082e2006-01-12 20:28:35 +00001969 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00001970 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00001971 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00001972 d2 = szHdr2;
1973 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001974 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001975 u32 serial_type1;
1976 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001977
1978 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00001979 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00001980 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00001981 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00001982 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001983
drh0660e262006-10-27 14:06:57 +00001984 /* Extract the values to be compared.
danielk197784ac9d02004-05-18 09:58:06 +00001985 */
drh25aa1b42004-05-28 01:39:01 +00001986 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1987 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001988
drh0660e262006-10-27 14:06:57 +00001989 /* Do the comparison
1990 */
drhd5788202004-05-28 08:21:05 +00001991 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001992 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1993 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001994 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001995 break;
1996 }
1997 i++;
1998 }
1999
2000 /* One of the keys ran out of fields, but all the fields up to that point
2001 ** were equal. If the incrKey flag is true, then the second key is
2002 ** treated as larger.
2003 */
2004 if( rc==0 ){
2005 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00002006 rc = -1;
2007 }else if( d1<nKey1 ){
2008 rc = 1;
2009 }else if( d2<nKey2 ){
2010 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00002011 }
drh0b2f3162005-12-21 18:36:45 +00002012 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2013 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00002014 rc = -rc;
2015 }
2016
2017 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00002018}
drhd5788202004-05-28 08:21:05 +00002019
2020/*
drh7a224de2004-06-02 01:22:02 +00002021** The argument is an index entry composed using the OP_MakeRecord opcode.
2022** The last entry in this record should be an integer (specifically
2023** an integer rowid). This routine returns the number of bytes in
2024** that integer.
drhd5788202004-05-28 08:21:05 +00002025*/
drh74161702006-02-24 02:53:49 +00002026int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002027 u32 szHdr; /* Size of the header */
2028 u32 typeRowid; /* Serial type of the rowid */
2029
2030 sqlite3GetVarint32(aKey, &szHdr);
2031 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2032 return sqlite3VdbeSerialTypeLen(typeRowid);
2033}
danielk1977eb015e02004-05-18 01:31:14 +00002034
2035
2036/*
drh7a224de2004-06-02 01:22:02 +00002037** pCur points at an index entry created using the OP_MakeRecord opcode.
2038** Read the rowid (the last field in the record) and store it in *rowid.
2039** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002040*/
2041int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002042 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002043 int rc;
drhd5788202004-05-28 08:21:05 +00002044 u32 szHdr; /* Size of the header */
2045 u32 typeRowid; /* Serial type of the rowid */
2046 u32 lenRowid; /* Size of the rowid */
2047 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002048
drhd5788202004-05-28 08:21:05 +00002049 sqlite3BtreeKeySize(pCur, &nCellKey);
2050 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002051 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002052 }
2053 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
2054 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002055 return rc;
2056 }
drh2646da72005-12-09 20:02:05 +00002057 sqlite3GetVarint32((u8*)m.z, &szHdr);
2058 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00002059 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002060 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002061 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002062 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002063 return SQLITE_OK;
2064}
2065
drh7cf6e4d2004-05-19 14:56:55 +00002066/*
drhd3d39e92004-05-20 22:16:29 +00002067** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002068** the key string in pKey (of length nKey). Write into *pRes a number
2069** that is negative, zero, or positive if pC is less than, equal to,
2070** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002071**
drhd5788202004-05-28 08:21:05 +00002072** pKey is either created without a rowid or is truncated so that it
2073** omits the rowid at the end. The rowid at the end of the index entry
2074** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002075*/
danielk1977183f9f72004-05-13 05:20:26 +00002076int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002077 Cursor *pC, /* The cursor to compare against */
2078 int nKey, const u8 *pKey, /* The key to compare */
2079 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002080){
drh61fc5952007-04-01 23:49:51 +00002081 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002082 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002083 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002084 int lenRowid;
2085 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002086
2087 sqlite3BtreeKeySize(pCur, &nCellKey);
2088 if( nCellKey<=0 ){
2089 *res = 0;
2090 return SQLITE_OK;
2091 }
drhd5788202004-05-28 08:21:05 +00002092 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2093 if( rc ){
2094 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002095 }
drh74161702006-02-24 02:53:49 +00002096 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00002097 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00002098 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002099 return SQLITE_OK;
2100}
danielk1977b28af712004-06-21 06:50:26 +00002101
2102/*
2103** This routine sets the value to be returned by subsequent calls to
2104** sqlite3_changes() on the database handle 'db'.
2105*/
2106void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
2107 db->nChange = nChange;
2108 db->nTotalChange += nChange;
2109}
2110
2111/*
2112** Set a flag in the vdbe to update the change counter when it is finalised
2113** or reset.
2114*/
drh4794f732004-11-05 17:17:50 +00002115void sqlite3VdbeCountChanges(Vdbe *v){
2116 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002117}
drhd89bd002005-01-22 03:03:54 +00002118
2119/*
2120** Mark every prepared statement associated with a database connection
2121** as expired.
2122**
2123** An expired statement means that recompilation of the statement is
2124** recommend. Statements expire when things happen that make their
2125** programs obsolete. Removing user-defined functions or collating
2126** sequences, or changing an authorization function are the types of
2127** things that make prepared statements obsolete.
2128*/
2129void sqlite3ExpirePreparedStatements(sqlite3 *db){
2130 Vdbe *p;
2131 for(p = db->pVdbe; p; p=p->pNext){
2132 p->expired = 1;
2133 }
2134}
danielk1977aee18ef2005-03-09 12:26:50 +00002135
2136/*
2137** Return the database associated with the Vdbe.
2138*/
2139sqlite3 *sqlite3VdbeDb(Vdbe *v){
2140 return v->db;
2141}