blob: 4214352dbb70efbccbcc15a97f506f4071c2be25 [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/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
drh76ff3a02004-09-24 22:32:30 +000059** Resize the Vdbe.aOp array so that it contains at least N
danielk1977634f2982005-03-28 08:44:07 +000060** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
danielk1977ace3eb22006-01-26 10:35:04 +000061** the Vdbe.aOp array will be sized to contain exactly N
62** elements. Vdbe.nOpAlloc is set to reflect the new size of
63** the array.
64**
65** If an out-of-memory error occurs while resizing the array,
66** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
67** any opcodes already allocated can be correctly deallocated
68** along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +000069*/
70static void resizeOpArray(Vdbe *p, int N){
drh53f733c2005-09-16 02:38:09 +000071 int runMode = p->magic==VDBE_MAGIC_RUN;
72 if( runMode || p->nOpAlloc<N ){
drhb38ad992005-09-16 00:27:01 +000073 VdbeOp *pNew;
drh53f733c2005-09-16 02:38:09 +000074 int nNew = N + 100*(!runMode);
75 int oldSize = p->nOpAlloc;
76 pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
drhb38ad992005-09-16 00:27:01 +000077 if( pNew ){
drh53f733c2005-09-16 02:38:09 +000078 p->nOpAlloc = nNew;
drhb38ad992005-09-16 00:27:01 +000079 p->aOp = pNew;
drh53f733c2005-09-16 02:38:09 +000080 if( nNew>oldSize ){
81 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
82 }
drh76ff3a02004-09-24 22:32:30 +000083 }
84 }
85}
86
87/*
drh9a324642003-09-06 20:12:01 +000088** Add a new instruction to the list of instructions current in the
89** VDBE. Return the address of the new instruction.
90**
91** Parameters:
92**
93** p Pointer to the VDBE
94**
95** op The opcode for this instruction
96**
97** p1, p2 First two of the three possible operands.
98**
danielk19774adee202004-05-08 08:23:19 +000099** Use the sqlite3VdbeResolveLabel() function to fix an address and
100** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +0000101** operand.
102*/
danielk19774adee202004-05-08 08:23:19 +0000103int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +0000104 int i;
drh701a0ae2004-02-22 20:05:00 +0000105 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000106
107 i = p->nOp;
108 p->nOp++;
109 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000110 if( p->nOpAlloc<=i ){
111 resizeOpArray(p, i+1);
112 if( sqlite3MallocFailed() ){
113 return 0;
114 }
drh9a324642003-09-06 20:12:01 +0000115 }
drh701a0ae2004-02-22 20:05:00 +0000116 pOp = &p->aOp[i];
117 pOp->opcode = op;
118 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000119 pOp->p2 = p2;
120 pOp->p3 = 0;
121 pOp->p3type = P3_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000122 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000123#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000124 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000125#endif
126 return i;
127}
128
129/*
drh701a0ae2004-02-22 20:05:00 +0000130** Add an opcode that includes the p3 value.
131*/
drheb2e1762004-05-27 01:53:56 +0000132int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000133 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
134 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000135 return addr;
136}
137
138/*
drh9a324642003-09-06 20:12:01 +0000139** Create a new symbolic label for an instruction that has yet to be
140** coded. The symbolic label is really just a negative number. The
141** label can be used as the P2 value of an operation. Later, when
142** the label is resolved to a specific address, the VDBE will scan
143** through its operation list and change all values of P2 which match
144** the label into the resolved address.
145**
146** The VDBE knows that a P2 value is a label because labels are
147** always negative and P2 values are suppose to be non-negative.
148** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000149**
150** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000151*/
danielk19774adee202004-05-08 08:23:19 +0000152int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000153 int i;
154 i = p->nLabel++;
155 assert( p->magic==VDBE_MAGIC_INIT );
156 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000157 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
danielk1977e7259292006-01-13 06:33:23 +0000158 sqliteReallocOrFree((void**)&p->aLabel,
drh53f733c2005-09-16 02:38:09 +0000159 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000160 }
drh76ff3a02004-09-24 22:32:30 +0000161 if( p->aLabel ){
162 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000163 }
drh9a324642003-09-06 20:12:01 +0000164 return -1-i;
165}
166
167/*
168** Resolve label "x" to be the address of the next instruction to
169** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000170** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000171*/
danielk19774adee202004-05-08 08:23:19 +0000172void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000173 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000174 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000175 assert( j>=0 && j<p->nLabel );
176 if( p->aLabel ){
177 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000178 }
179}
180
181/*
danielk1977bc04f852005-03-29 08:26:13 +0000182** Return non-zero if opcode 'op' is guarenteed not to push more values
183** onto the VDBE stack than it pops off.
184*/
danielk19777a5147c2005-03-29 13:07:00 +0000185static int opcodeNoPush(u8 op){
186 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000187 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
188 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000189 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000190 ** in a comment on the same line as the "case OP_XXX:" in
191 ** sqlite3VdbeExec() in vdbe.c.
192 **
193 ** If the bit is true, then the corresponding opcode is guarenteed not
194 ** to grow the stack when it is executed. Otherwise, it may grow the
195 ** stack by at most one entry.
196 **
danielk19777a5147c2005-03-29 13:07:00 +0000197 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000198 ** one bit for opcodes 16 to 31, and so on.
199 **
200 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
201 ** because the file is generated by an awk program. Awk manipulates
202 ** all numbers as floating-point and we don't want to risk a rounding
203 ** error if someone builds with an awk that uses (for example) 32-bit
204 ** IEEE floats.
205 */
drh9a7e6082005-03-31 22:26:19 +0000206 static const u32 masks[5] = {
drh580eeaf2006-02-24 03:09:37 +0000207 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
208 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
209 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
210 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
211 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000212 };
drh9cbe6352005-11-29 03:13:21 +0000213 assert( op<32*5 );
danielk1977bc04f852005-03-29 08:26:13 +0000214 return (masks[op>>5] & (1<<(op&0x1F)));
215}
216
217#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000218int sqlite3VdbeOpcodeNoPush(u8 op){
219 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000220}
221#endif
222
223/*
drh76ff3a02004-09-24 22:32:30 +0000224** Loop through the program looking for P2 values that are negative.
225** Each such value is a label. Resolve the label by setting the P2
226** value to its correct non-zero value.
227**
228** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000229**
drh13449892005-09-07 21:22:45 +0000230** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
231** to an OP_Function or OP_AggStep opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000232** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000233**
234** The integer *pMaxStack is set to the maximum number of vdbe stack
235** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000236**
237** This routine also does the following optimization: It scans for
238** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
drhf0863fe2005-06-12 21:35:51 +0000239** IdxInsert instructions where P2!=0. If no such instruction is
drh38449902005-06-07 01:43:41 +0000240** found, then every Statement instruction is changed to a Noop. In
241** this way, we avoid creating the statement journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000242*/
danielk1977bc04f852005-03-29 08:26:13 +0000243static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000244 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000245 int nMaxArgs = 0;
246 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000247 Op *pOp;
248 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000249 int doesStatementRollback = 0;
250 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000251 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000252 u8 opcode = pOp->opcode;
253
drh13449892005-09-07 21:22:45 +0000254 if( opcode==OP_Function || opcode==OP_AggStep ){
danielk1977bc04f852005-03-29 08:26:13 +0000255 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh38449902005-06-07 01:43:41 +0000256 }else if( opcode==OP_Halt ){
257 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
258 doesStatementRollback = 1;
259 }
drhf0863fe2005-06-12 21:35:51 +0000260 }else if( opcode==OP_IdxInsert ){
drh38449902005-06-07 01:43:41 +0000261 if( pOp->p2 ){
262 doesStatementRollback = 1;
263 }
264 }else if( opcode==OP_Statement ){
265 hasStatementBegin = 1;
danielk1977bc04f852005-03-29 08:26:13 +0000266 }
267
danielk19777a5147c2005-03-29 13:07:00 +0000268 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000269 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000270 }
271
drh76ff3a02004-09-24 22:32:30 +0000272 if( pOp->p2>=0 ) continue;
273 assert( -1-pOp->p2<p->nLabel );
274 pOp->p2 = aLabel[-1-pOp->p2];
275 }
276 sqliteFree(p->aLabel);
277 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000278
279 *pMaxFuncArgs = nMaxArgs;
280 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000281
282 /* If we never rollback a statement transaction, then statement
283 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000284 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000285 ** which can be expensive on some platforms.
286 */
287 if( hasStatementBegin && !doesStatementRollback ){
288 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
289 if( pOp->opcode==OP_Statement ){
290 pOp->opcode = OP_Noop;
291 }
292 }
293 }
drh76ff3a02004-09-24 22:32:30 +0000294}
295
296/*
drh9a324642003-09-06 20:12:01 +0000297** Return the address of the next instruction to be inserted.
298*/
danielk19774adee202004-05-08 08:23:19 +0000299int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000300 assert( p->magic==VDBE_MAGIC_INIT );
301 return p->nOp;
302}
303
304/*
305** Add a whole list of operations to the operation stack. Return the
306** address of the first operation added.
307*/
danielk19774adee202004-05-08 08:23:19 +0000308int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000309 int addr;
310 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000311 resizeOpArray(p, p->nOp + nOp);
danielk19779e128002006-01-18 16:51:35 +0000312 if( sqlite3MallocFailed() ){
drh76ff3a02004-09-24 22:32:30 +0000313 return 0;
drh9a324642003-09-06 20:12:01 +0000314 }
315 addr = p->nOp;
316 if( nOp>0 ){
317 int i;
drh905793e2004-02-21 13:31:09 +0000318 VdbeOpList const *pIn = aOp;
319 for(i=0; i<nOp; i++, pIn++){
320 int p2 = pIn->p2;
321 VdbeOp *pOut = &p->aOp[i+addr];
322 pOut->opcode = pIn->opcode;
323 pOut->p1 = pIn->p1;
324 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
325 pOut->p3 = pIn->p3;
326 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000327#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000328 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000329 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000330 }
331#endif
332 }
333 p->nOp += nOp;
334 }
335 return addr;
336}
337
338/*
339** Change the value of the P1 operand for a specific instruction.
340** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000341** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000342** few minor changes to the program.
343*/
danielk19774adee202004-05-08 08:23:19 +0000344void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000345 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000346 if( p && addr>=0 && p->nOp>addr && p->aOp ){
347 p->aOp[addr].p1 = val;
348 }
349}
350
351/*
352** Change the value of the P2 operand for a specific instruction.
353** This routine is useful for setting a jump destination.
354*/
danielk19774adee202004-05-08 08:23:19 +0000355void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000356 assert( val>=0 );
drh8aa34ae2006-03-13 12:54:09 +0000357 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000358 if( p && addr>=0 && p->nOp>addr && p->aOp ){
359 p->aOp[addr].p2 = val;
360 }
361}
362
drhd654be82005-09-20 17:42:23 +0000363/*
364** Change teh P2 operand of instruction addr so that it points to
365** the address of the next instruction to be coded.
366*/
367void sqlite3VdbeJumpHere(Vdbe *p, int addr){
368 sqlite3VdbeChangeP2(p, addr, p->nOp);
369}
drhb38ad992005-09-16 00:27:01 +0000370
371/*
372** Delete a P3 value if necessary.
373*/
374static void freeP3(int p3type, void *p3){
375 if( p3 ){
drhac1733d2005-09-17 17:58:22 +0000376 switch( p3type ){
377 case P3_DYNAMIC:
378 case P3_KEYINFO:
379 case P3_KEYINFO_HANDOFF: {
380 sqliteFree(p3);
381 break;
382 }
383 case P3_VDBEFUNC: {
384 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
385 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
386 sqliteFree(pVdbeFunc);
387 break;
388 }
389 case P3_MEM: {
390 sqlite3ValueFree((sqlite3_value*)p3);
391 break;
392 }
drhb38ad992005-09-16 00:27:01 +0000393 }
394 }
395}
396
397
drh9a324642003-09-06 20:12:01 +0000398/*
399** Change the value of the P3 operand for a specific instruction.
400** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000401** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000402** few minor changes to the program.
403**
404** If n>=0 then the P3 operand is dynamic, meaning that a copy of
405** the string is made into memory obtained from sqliteMalloc().
406** A value of n==0 means copy bytes of zP3 up to and including the
407** first null byte. If n>0 then copy n+1 bytes of zP3.
408**
danielk19771f55c052005-05-19 08:42:59 +0000409** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
410** A copy is made of the KeyInfo structure into memory obtained from
411** sqliteMalloc, to be freed when the Vdbe is finalized.
412** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
413** stored in memory that the caller has obtained from sqliteMalloc. The
414** caller should not free the allocation, it will be freed when the Vdbe is
415** finalized.
416**
417** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
418** to a string or structure that is guaranteed to exist for the lifetime of
419** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000420**
421** If addr<0 then change P3 on the most recently inserted instruction.
422*/
danielk19774adee202004-05-08 08:23:19 +0000423void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000424 Op *pOp;
drh8aa34ae2006-03-13 12:54:09 +0000425 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
danielk19779e128002006-01-18 16:51:35 +0000426 if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000427 if (n != P3_KEYINFO) {
428 freeP3(n, (void*)*(char**)&zP3);
429 }
danielk1977d5d56522005-03-16 12:15:20 +0000430 return;
431 }
drh9a324642003-09-06 20:12:01 +0000432 if( addr<0 || addr>=p->nOp ){
433 addr = p->nOp - 1;
434 if( addr<0 ) return;
435 }
436 pOp = &p->aOp[addr];
drhb38ad992005-09-16 00:27:01 +0000437 freeP3(pOp->p3type, pOp->p3);
438 pOp->p3 = 0;
drh9a324642003-09-06 20:12:01 +0000439 if( zP3==0 ){
440 pOp->p3 = 0;
441 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000442 }else if( n==P3_KEYINFO ){
443 KeyInfo *pKeyInfo;
444 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000445
drhd3d39e92004-05-20 22:16:29 +0000446 nField = ((KeyInfo*)zP3)->nField;
drhfdd6e852005-12-16 01:06:16 +0000447 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drheafe05b2004-06-13 00:54:01 +0000448 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000449 pOp->p3 = (char*)pKeyInfo;
450 if( pKeyInfo ){
danielk1977bab45c62006-01-16 15:14:27 +0000451 unsigned char *aSortOrder;
drhd3d39e92004-05-20 22:16:29 +0000452 memcpy(pKeyInfo, zP3, nByte);
drhfdd6e852005-12-16 01:06:16 +0000453 aSortOrder = pKeyInfo->aSortOrder;
454 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000455 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000456 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
457 }
drhd3d39e92004-05-20 22:16:29 +0000458 pOp->p3type = P3_KEYINFO;
459 }else{
460 pOp->p3type = P3_NOTUSED;
461 }
drhffbc3082004-05-21 01:29:06 +0000462 }else if( n==P3_KEYINFO_HANDOFF ){
463 pOp->p3 = (char*)zP3;
464 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000465 }else if( n<0 ){
466 pOp->p3 = (char*)zP3;
467 pOp->p3type = n;
468 }else{
drhae29ffb2004-09-25 14:39:18 +0000469 if( n==0 ) n = strlen(zP3);
470 pOp->p3 = sqliteStrNDup(zP3, n);
drh9a324642003-09-06 20:12:01 +0000471 pOp->p3type = P3_DYNAMIC;
472 }
473}
474
drhad6d9462004-09-19 02:15:24 +0000475#ifndef NDEBUG
476/*
477** Replace the P3 field of the most recently coded instruction with
478** comment text.
479*/
480void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
481 va_list ap;
482 assert( p->nOp>0 );
drh6f7adc82006-01-11 21:41:20 +0000483 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0
danielk19779e128002006-01-18 16:51:35 +0000484 || sqlite3MallocFailed() );
drhad6d9462004-09-19 02:15:24 +0000485 va_start(ap, zFormat);
486 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
487 va_end(ap);
488}
489#endif
490
drh9a324642003-09-06 20:12:01 +0000491/*
drh9a324642003-09-06 20:12:01 +0000492** Return the opcode for a given address.
493*/
danielk19774adee202004-05-08 08:23:19 +0000494VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000495 assert( p->magic==VDBE_MAGIC_INIT );
496 assert( addr>=0 && addr<p->nOp );
497 return &p->aOp[addr];
498}
499
drhb7f91642004-10-31 02:22:47 +0000500#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
501 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000502/*
drhd3d39e92004-05-20 22:16:29 +0000503** Compute a string that describes the P3 parameter for an opcode.
504** Use zTemp for any required temporary buffer space.
505*/
506static char *displayP3(Op *pOp, char *zTemp, int nTemp){
507 char *zP3;
508 assert( nTemp>=20 );
509 switch( pOp->p3type ){
drhd3d39e92004-05-20 22:16:29 +0000510 case P3_KEYINFO: {
511 int i, j;
512 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
513 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
514 i = strlen(zTemp);
515 for(j=0; j<pKeyInfo->nField; j++){
516 CollSeq *pColl = pKeyInfo->aColl[j];
517 if( pColl ){
518 int n = strlen(pColl->zName);
519 if( i+n>nTemp-6 ){
520 strcpy(&zTemp[i],",...");
521 break;
522 }
523 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000524 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000525 zTemp[i++] = '-';
526 }
527 strcpy(&zTemp[i], pColl->zName);
528 i += n;
529 }else if( i+4<nTemp-6 ){
530 strcpy(&zTemp[i],",nil");
531 i += 4;
532 }
533 }
534 zTemp[i++] = ')';
535 zTemp[i] = 0;
536 assert( i<nTemp );
537 zP3 = zTemp;
538 break;
539 }
540 case P3_COLLSEQ: {
541 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000542 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000543 zP3 = zTemp;
544 break;
545 }
drhf9b596e2004-05-26 16:54:42 +0000546 case P3_FUNCDEF: {
547 FuncDef *pDef = (FuncDef*)pOp->p3;
548 char zNum[30];
549 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
550 sprintf(zNum,"(%d)", pDef->nArg);
551 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
552 strcat(zTemp, zNum);
553 }
554 zP3 = zTemp;
555 break;
556 }
drhd3d39e92004-05-20 22:16:29 +0000557 default: {
558 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000559 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000560 zP3 = "";
561 }
562 }
563 }
564 return zP3;
565}
drhb7f91642004-10-31 02:22:47 +0000566#endif
drhd3d39e92004-05-20 22:16:29 +0000567
568
danielk19778b60e0f2005-01-12 09:10:39 +0000569#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000570/*
571** Print a single opcode. This routine is used for debugging only.
572*/
danielk19774adee202004-05-08 08:23:19 +0000573void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000574 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000575 char zPtr[50];
576 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000577 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000578 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000579 fprintf(pOut, zFormat1,
580 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000581 fflush(pOut);
582}
583#endif
584
585/*
drh76ff3a02004-09-24 22:32:30 +0000586** Release an array of N Mem elements
587*/
588static void releaseMemArray(Mem *p, int N){
589 if( p ){
590 while( N-->0 ){
591 sqlite3VdbeMemRelease(p++);
592 }
593 }
594}
595
drhb7f91642004-10-31 02:22:47 +0000596#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000597/*
drh9a324642003-09-06 20:12:01 +0000598** Give a listing of the program in the virtual machine.
599**
danielk19774adee202004-05-08 08:23:19 +0000600** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000601** running the code, it invokes the callback once for each instruction.
602** This feature is used to implement "EXPLAIN".
603*/
danielk19774adee202004-05-08 08:23:19 +0000604int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000605 Vdbe *p /* The VDBE */
606){
drh9bb575f2004-09-06 17:24:11 +0000607 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000608 int i;
drh826fb5a2004-02-14 23:59:57 +0000609 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000610
drh9a324642003-09-06 20:12:01 +0000611 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000612 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
613 assert( db->magic==SQLITE_MAGIC_BUSY );
614 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000615
616 /* Even though this opcode does not put dynamic strings onto the
617 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000618 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000619 */
620 if( p->pTos==&p->aStack[4] ){
drh76ff3a02004-09-24 22:32:30 +0000621 releaseMemArray(p->aStack, 5);
danielk197718f41892004-05-22 07:27:46 +0000622 }
danielk197718f41892004-05-22 07:27:46 +0000623 p->resOnStack = 0;
624
drhecc92422005-09-10 16:46:12 +0000625 do{
626 i = p->pc++;
627 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000628 if( i>=p->nOp ){
629 p->rc = SQLITE_OK;
630 rc = SQLITE_DONE;
631 }else if( db->flags & SQLITE_Interrupt ){
632 db->flags &= ~SQLITE_Interrupt;
drhc5cdca62005-01-11 16:54:14 +0000633 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000634 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000635 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000636 }else{
drhd3d39e92004-05-20 22:16:29 +0000637 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000638 Mem *pMem = p->aStack;
639 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000640 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000641 pMem->i = i; /* Program counter */
642 pMem++;
643
644 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
645 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
646 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000647 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000648 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000649 pMem++;
650
651 pMem->flags = MEM_Int;
652 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000653 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000654 pMem++;
655
656 pMem->flags = MEM_Int;
657 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000658 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000659 pMem++;
660
drhb8067982006-03-03 21:38:03 +0000661 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
drheb2e1762004-05-27 01:53:56 +0000662 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drhb8067982006-03-03 21:38:03 +0000663 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000664 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000665 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000666
drhecc92422005-09-10 16:46:12 +0000667 p->nResColumn = 5 - 2*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000668 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000669 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000670 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000671 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000672 }
drh826fb5a2004-02-14 23:59:57 +0000673 return rc;
drh9a324642003-09-06 20:12:01 +0000674}
drhb7f91642004-10-31 02:22:47 +0000675#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000676
677/*
drh3f7d4e42004-07-24 14:35:58 +0000678** Print the SQL that was used to generate a VDBE program.
679*/
680void sqlite3VdbePrintSql(Vdbe *p){
681#ifdef SQLITE_DEBUG
682 int nOp = p->nOp;
683 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000684 if( nOp<1 ) return;
685 pOp = &p->aOp[nOp-1];
drh3f7d4e42004-07-24 14:35:58 +0000686 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
687 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000688 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000689 printf("SQL: [%s]\n", z);
690 }
691#endif
692}
693
694/*
drh9a324642003-09-06 20:12:01 +0000695** Prepare a virtual machine for execution. This involves things such
696** as allocating stack space and initializing the program counter.
697** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000698** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000699**
700** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
701** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000702*/
danielk19774adee202004-05-08 08:23:19 +0000703void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000704 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000705 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000706 int nMem, /* Number of memory cells to allocate */
707 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000708 int isExplain /* True if the EXPLAIN keywords is present */
709){
710 int n;
711
712 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000713 assert( p->magic==VDBE_MAGIC_INIT );
714
drhc16a03b2004-09-15 13:38:10 +0000715 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000716 */
drhc16a03b2004-09-15 13:38:10 +0000717 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000718
danielk1977634f2982005-03-28 08:44:07 +0000719 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
720 * is because the call to resizeOpArray() below may shrink the
721 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
722 * state.
723 */
724 p->magic = VDBE_MAGIC_RUN;
725
drh9a324642003-09-06 20:12:01 +0000726 /* No instruction ever pushes more than a single element onto the
727 ** stack. And the stack never grows on successive executions of the
728 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000729 ** on the maximum stack depth required. (Added later:) The
730 ** resolveP2Values() call computes a tighter upper bound on the
731 ** stack size.
drh9a324642003-09-06 20:12:01 +0000732 **
733 ** Allocation all the stack space we will ever need.
734 */
drh82a48512003-09-06 22:45:20 +0000735 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000736 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000737 int nStack; /* Maximum number of stack entries required */
738 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000739 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000740 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000741 assert( nStack<p->nOp );
742 nStack = isExplain ? 10 : nStack;
drh82a48512003-09-06 22:45:20 +0000743 p->aStack = sqliteMalloc(
danielk1977bc04f852005-03-29 08:26:13 +0000744 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000745 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000746 + nVar*sizeof(Mem) /* aVar */
747 + nVar*sizeof(char*) /* azVar */
748 + nMem*sizeof(Mem) /* aMem */
749 + nCursor*sizeof(Cursor*) /* apCsr */
drh82a48512003-09-06 22:45:20 +0000750 );
danielk19779e128002006-01-18 16:51:35 +0000751 if( !sqlite3MallocFailed() ){
danielk1977bc04f852005-03-29 08:26:13 +0000752 p->aMem = &p->aStack[nStack];
drh290c1942004-08-21 17:54:45 +0000753 p->nMem = nMem;
drh86f43302004-10-05 17:37:36 +0000754 p->aVar = &p->aMem[nMem];
755 p->nVar = nVar;
756 p->okVar = 0;
757 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000758 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000759 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000760 p->nCursor = nCursor;
761 for(n=0; n<nVar; n++){
762 p->aVar[n].flags = MEM_Null;
763 }
danielk197754db47e2004-05-19 10:36:43 +0000764 }
drh82a48512003-09-06 22:45:20 +0000765 }
danielk1977b3bce662005-01-29 08:32:43 +0000766 for(n=0; n<p->nMem; n++){
767 p->aMem[n].flags = MEM_Null;
768 }
drh9a324642003-09-06 20:12:01 +0000769
drhfaa57ac2004-06-09 14:01:51 +0000770#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000771 if( (p->db->flags & SQLITE_VdbeListing)!=0
drh66560ad2006-01-06 14:32:19 +0000772 || sqlite3OsFileExists("vdbe_explain")
drh35d4c2f2004-06-10 01:30:59 +0000773 ){
drh80242052004-06-09 00:48:12 +0000774 int i;
775 printf("VDBE Program Listing:\n");
drh3f7d4e42004-07-24 14:35:58 +0000776 sqlite3VdbePrintSql(p);
drh80242052004-06-09 00:48:12 +0000777 for(i=0; i<p->nOp; i++){
778 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
779 }
780 }
drh66560ad2006-01-06 14:32:19 +0000781 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000782 p->trace = stdout;
783 }
784#endif
drh6810ce62004-01-31 19:22:56 +0000785 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000786 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000787 p->rc = SQLITE_OK;
788 p->uniqueCnt = 0;
789 p->returnDepth = 0;
790 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000791 p->popStack = 0;
792 p->explain |= isExplain;
793 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000794 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +0000795 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +0000796 p->minWriteFileFormat = 255;
drh9a324642003-09-06 20:12:01 +0000797#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000798 {
799 int i;
800 for(i=0; i<p->nOp; i++){
801 p->aOp[i].cnt = 0;
802 p->aOp[i].cycles = 0;
803 }
drh9a324642003-09-06 20:12:01 +0000804 }
805#endif
806}
807
drh9a324642003-09-06 20:12:01 +0000808/*
drh9a324642003-09-06 20:12:01 +0000809** Close a cursor and release all the resources that cursor happens
810** to hold.
811*/
drh4774b132004-06-12 20:12:51 +0000812void sqlite3VdbeFreeCursor(Cursor *pCx){
813 if( pCx==0 ){
814 return;
815 }
drh9a324642003-09-06 20:12:01 +0000816 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000817 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000818 }
819 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000820 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000821 }
822 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000823 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000824 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000825}
826
827/*
828** Close all cursors
829*/
830static void closeAllCursors(Vdbe *p){
831 int i;
drh290c1942004-08-21 17:54:45 +0000832 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000833 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000834 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh290c1942004-08-21 17:54:45 +0000835 p->apCsr[i] = 0;
drh9a324642003-09-06 20:12:01 +0000836 }
drh9a324642003-09-06 20:12:01 +0000837}
838
839/*
drh9a324642003-09-06 20:12:01 +0000840** Clean up the VM after execution.
841**
842** This routine will automatically close any cursors, lists, and/or
843** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000844** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000845*/
846static void Cleanup(Vdbe *p){
847 int i;
drh6810ce62004-01-31 19:22:56 +0000848 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +0000849 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
850 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +0000851 }
drh9a324642003-09-06 20:12:01 +0000852 closeAllCursors(p);
drh76ff3a02004-09-24 22:32:30 +0000853 releaseMemArray(p->aMem, p->nMem);
drha01f79d2005-07-08 13:07:59 +0000854 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +0000855 if( p->contextStack ){
856 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +0000857 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +0000858 }
859 sqliteFree(p->contextStack);
drh344737f2004-09-19 00:50:20 +0000860 }
drh5f968432004-02-21 19:02:30 +0000861 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +0000862 p->contextStackDepth = 0;
863 p->contextStackTop = 0;
drh9a324642003-09-06 20:12:01 +0000864 sqliteFree(p->zErrMsg);
865 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000866}
867
868/*
danielk197722322fd2004-05-25 23:35:17 +0000869** Set the number of result columns that will be returned by this SQL
870** statement. This is now set at compile time, rather than during
871** execution of the vdbe program so that sqlite3_column_count() can
872** be called on an SQL statement before sqlite3_step().
873*/
874void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +0000875 Mem *pColName;
876 int n;
danielk1977955de522006-02-10 02:27:42 +0000877 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drhcc43cab2005-10-05 11:35:09 +0000878 sqliteFree(p->aColName);
danielk1977955de522006-02-10 02:27:42 +0000879 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +0000880 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +0000881 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
882 if( p->aColName==0 ) return;
883 while( n-- > 0 ){
884 (pColName++)->flags = MEM_Null;
885 }
danielk197722322fd2004-05-25 23:35:17 +0000886}
887
888/*
danielk19773cf86062004-05-26 10:11:05 +0000889** Set the name of the idx'th column to be returned by the SQL statement.
890** zName must be a pointer to a nul terminated string.
891**
892** This call must be made after a call to sqlite3VdbeSetNumCols().
893**
danielk1977d8123362004-06-12 09:25:12 +0000894** If N==P3_STATIC it means that zName is a pointer to a constant static
895** string and we can just copy the pointer. If it is P3_DYNAMIC, then
896** the string is freed using sqliteFree() when the vdbe is finished with
897** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000898*/
danielk1977955de522006-02-10 02:27:42 +0000899int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +0000900 int rc;
901 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +0000902 assert( idx<p->nResColumn );
903 assert( var<COLNAME_N );
danielk19779e128002006-01-18 16:51:35 +0000904 if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +0000905 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +0000906 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk1977d8123362004-06-12 09:25:12 +0000907 if( N==P3_DYNAMIC || N==P3_STATIC ){
908 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000909 }else{
danielk1977d8123362004-06-12 09:25:12 +0000910 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000911 }
912 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
913 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000914 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000915 }
916 return rc;
917}
918
danielk197713adf8a2004-06-03 16:08:41 +0000919/*
920** A read or write transaction may or may not be active on database handle
921** db. If a transaction is active, commit it. If there is a
922** write-transaction spanning more than one database file, this routine
923** takes care of the master journal trickery.
924*/
drh9bb575f2004-09-06 17:24:11 +0000925static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +0000926 int i;
927 int nTrans = 0; /* Number of databases with an active write-transaction */
928 int rc = SQLITE_OK;
929 int needXcommit = 0;
930
931 for(i=0; i<db->nDb; i++){
932 Btree *pBt = db->aDb[i].pBt;
933 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
934 needXcommit = 1;
935 if( i!=1 ) nTrans++;
936 }
937 }
938
939 /* If there are any write-transactions at all, invoke the commit hook */
940 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +0000941 sqlite3SafetyOff(db);
942 rc = db->xCommitCallback(db->pCommitArg);
943 sqlite3SafetyOn(db);
944 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +0000945 return SQLITE_CONSTRAINT;
946 }
947 }
948
danielk197740b38dc2004-06-26 08:38:24 +0000949 /* The simple case - no more than one database file (not counting the
950 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000951 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000952 **
danielk197740b38dc2004-06-26 08:38:24 +0000953 ** If the return value of sqlite3BtreeGetFilename() is a zero length
954 ** string, it means the main database is :memory:. In that case we do
955 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000956 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000957 */
danielk197740b38dc2004-06-26 08:38:24 +0000958 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +0000959 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +0000960 Btree *pBt = db->aDb[i].pBt;
961 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +0000962 rc = sqlite3BtreeSync(pBt, 0);
963 }
964 }
965
966 /* Do the commit only if all databases successfully synced */
967 if( rc==SQLITE_OK ){
968 for(i=0; i<db->nDb; i++){
969 Btree *pBt = db->aDb[i].pBt;
970 if( pBt ){
971 sqlite3BtreeCommit(pBt);
972 }
danielk197713adf8a2004-06-03 16:08:41 +0000973 }
974 }
975 }
976
977 /* The complex case - There is a multi-file write-transaction active.
978 ** This requires a master journal file to ensure the transaction is
979 ** committed atomicly.
980 */
danielk197744ee5bf2005-05-27 09:41:12 +0000981#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +0000982 else{
drh2c8997b2005-08-27 16:36:48 +0000983 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +0000984 char *zMaster = 0; /* File-name for the master journal */
985 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
drh9cbe6352005-11-29 03:13:21 +0000986 OsFile *master = 0;
danielk197713adf8a2004-06-03 16:08:41 +0000987
988 /* Select a master journal file name */
989 do {
drha6abd042004-06-09 17:37:22 +0000990 u32 random;
991 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +0000992 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +0000993 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +0000994 if( !zMaster ){
995 return SQLITE_NOMEM;
996 }
drh66560ad2006-01-06 14:32:19 +0000997 }while( sqlite3OsFileExists(zMaster) );
danielk197713adf8a2004-06-03 16:08:41 +0000998
999 /* Open the master journal. */
drh66560ad2006-01-06 14:32:19 +00001000 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001001 if( rc!=SQLITE_OK ){
1002 sqliteFree(zMaster);
1003 return rc;
1004 }
1005
1006 /* Write the name of each database file in the transaction into the new
1007 ** master journal file. If an error occurs at this point close
1008 ** and delete the master journal file. All the individual journal files
1009 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001010 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001011 */
1012 for(i=0; i<db->nDb; i++){
1013 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001014 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001015 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001016 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001017 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001018 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1019 needSync = 1;
1020 }
drh054889e2005-11-30 03:20:31 +00001021 rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001022 if( rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001023 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001024 sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001025 sqliteFree(zMaster);
1026 return rc;
1027 }
1028 }
1029 }
1030
danielk197713adf8a2004-06-03 16:08:41 +00001031
danielk19775865e3d2004-06-14 06:03:57 +00001032 /* Sync the master journal file. Before doing this, open the directory
1033 ** the master journal file is store in so that it gets synced too.
1034 */
1035 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
drh054889e2005-11-30 03:20:31 +00001036 rc = sqlite3OsOpenDirectory(master, zMainFile);
drheb796a72005-09-08 12:38:41 +00001037 if( rc!=SQLITE_OK ||
drh054889e2005-11-30 03:20:31 +00001038 (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
1039 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001040 sqlite3OsDelete(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001041 sqliteFree(zMaster);
1042 return rc;
1043 }
drhc9e06862004-06-09 20:03:08 +00001044
danielk197713adf8a2004-06-03 16:08:41 +00001045 /* Sync all the db files involved in the transaction. The same call
1046 ** sets the master journal pointer in each individual journal. If
1047 ** an error occurs here, do not delete the master journal file.
1048 **
1049 ** If the error occurs during the first call to sqlite3BtreeSync(),
1050 ** then there is a chance that the master journal file will be
1051 ** orphaned. But we cannot delete it, in case the master journal
1052 ** file name was written into the journal file before the failure
1053 ** occured.
1054 */
1055 for(i=0; i<db->nDb; i++){
1056 Btree *pBt = db->aDb[i].pBt;
1057 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1058 rc = sqlite3BtreeSync(pBt, zMaster);
1059 if( rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001060 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001061 sqliteFree(zMaster);
1062 return rc;
1063 }
1064 }
1065 }
drh054889e2005-11-30 03:20:31 +00001066 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001067
danielk1977962398d2004-06-14 09:35:16 +00001068 /* Delete the master journal file. This commits the transaction. After
1069 ** doing this the directory is synced again before any individual
1070 ** transaction files are deleted.
1071 */
drh66560ad2006-01-06 14:32:19 +00001072 rc = sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001073 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001074 sqliteFree(zMaster);
1075 zMaster = 0;
drh66560ad2006-01-06 14:32:19 +00001076 rc = sqlite3OsSyncDirectory(zMainFile);
danielk1977962398d2004-06-14 09:35:16 +00001077 if( rc!=SQLITE_OK ){
1078 /* This is not good. The master journal file has been deleted, but
1079 ** the directory sync failed. There is no completely safe course of
1080 ** action from here. The individual journals contain the name of the
1081 ** master journal file, but there is no way of knowing if that
1082 ** master journal exists now or if it will exist after the operating
1083 ** system crash that may follow the fsync() failure.
1084 */
danielk1977962398d2004-06-14 09:35:16 +00001085 return rc;
1086 }
danielk197713adf8a2004-06-03 16:08:41 +00001087
1088 /* All files and directories have already been synced, so the following
1089 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1090 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001091 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001092 ** but some stray 'cold' journals may be lying around. Returning an
1093 ** error code won't help matters.
1094 */
1095 for(i=0; i<db->nDb; i++){
1096 Btree *pBt = db->aDb[i].pBt;
1097 if( pBt ){
1098 sqlite3BtreeCommit(pBt);
1099 }
1100 }
1101 }
danielk197744ee5bf2005-05-27 09:41:12 +00001102#endif
danielk1977026d2702004-06-14 13:14:59 +00001103
drh2ac3ee92004-06-07 16:27:46 +00001104 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001105}
1106
drh91b48aa2004-06-30 11:14:18 +00001107/*
1108** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001109** aborted. This happens when one VM causes a rollback due to an
1110** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1111** aborted so that they do not have data rolled out from underneath
1112** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001113*/
danielk19778d34dfd2006-01-24 16:37:57 +00001114void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
drh91b48aa2004-06-30 11:14:18 +00001115 Vdbe *pOther;
danielk19778d34dfd2006-01-24 16:37:57 +00001116 for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
1117 if( pOther==pExcept ) continue;
drh91b48aa2004-06-30 11:14:18 +00001118 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1119 closeAllCursors(pOther);
1120 pOther->aborted = 1;
1121 }
1122}
1123
danielk19771d850a72004-05-31 08:26:49 +00001124/*
1125** This routine checks that the sqlite3.activeVdbeCnt count variable
1126** matches the number of vdbe's in the list sqlite3.pVdbe that are
1127** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001128** This is an internal self-check only - it is not an essential processing
1129** step.
danielk19771d850a72004-05-31 08:26:49 +00001130**
1131** This is a no-op if NDEBUG is defined.
1132*/
1133#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001134static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001135 Vdbe *p;
1136 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001137 p = db->pVdbe;
1138 while( p ){
drh92f02c32004-09-02 14:57:08 +00001139 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001140 cnt++;
1141 }
1142 p = p->pNext;
1143 }
danielk19771d850a72004-05-31 08:26:49 +00001144 assert( cnt==db->activeVdbeCnt );
1145}
1146#else
1147#define checkActiveVdbeCnt(x)
1148#endif
1149
danielk19773cf86062004-05-26 10:11:05 +00001150/*
drh92f02c32004-09-02 14:57:08 +00001151** This routine is called the when a VDBE tries to halt. If the VDBE
1152** has made changes and is in autocommit mode, then commit those
1153** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001154**
drh92f02c32004-09-02 14:57:08 +00001155** This routine is the only way to move the state of a VM from
1156** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1157**
1158** Return an error code. If the commit could not complete because of
1159** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1160** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001161*/
drh92f02c32004-09-02 14:57:08 +00001162int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001163 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001164 int i;
danielk19771d850a72004-05-31 08:26:49 +00001165 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001166 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1167
1168 /* This function contains the logic that determines if a statement or
1169 ** transaction will be committed or rolled back as a result of the
1170 ** execution of this virtual machine.
1171 **
1172 ** Special errors:
1173 **
1174 ** If an SQLITE_NOMEM error has occured in a statement that writes to
1175 ** the database, then either a statement or transaction must be rolled
1176 ** back to ensure the tree-structures are in a consistent state. A
1177 ** statement transaction is rolled back if one is open, otherwise the
1178 ** entire transaction must be rolled back.
1179 **
1180 ** If an SQLITE_IOERR error has occured in a statement that writes to
1181 ** the database, then the entire transaction must be rolled back. The
1182 ** I/O error may have caused garbage to be written to the journal
1183 ** file. Were the transaction to continue and eventually be rolled
1184 ** back that garbage might end up in the database file.
1185 **
1186 ** In both of the above cases, the Vdbe.errorAction variable is
1187 ** ignored. If the sqlite3.autoCommit flag is false and a transaction
1188 ** is rolled back, it will be set to true.
1189 **
1190 ** Other errors:
1191 **
1192 ** No error:
1193 **
1194 */
drh9a324642003-09-06 20:12:01 +00001195
danielk19779e128002006-01-18 16:51:35 +00001196 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +00001197 p->rc = SQLITE_NOMEM;
1198 }
drh92f02c32004-09-02 14:57:08 +00001199 if( p->magic!=VDBE_MAGIC_RUN ){
1200 /* Already halted. Nothing to do. */
1201 assert( p->magic==VDBE_MAGIC_HALT );
1202 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001203 }
drh92f02c32004-09-02 14:57:08 +00001204 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001205 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001206
danielk197707cb5602006-01-20 10:55:05 +00001207 /* No commit or rollback needed if the program never started */
1208 if( p->pc>=0 ){
danielk1977261919c2005-12-06 12:52:59 +00001209
danielk197707cb5602006-01-20 10:55:05 +00001210 /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
1211 isSpecialError = ((p->rc==SQLITE_NOMEM || p->rc==SQLITE_IOERR)?1:0);
1212 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001213 /* This loop does static analysis of the query to see which of the
1214 ** following three categories it falls into:
1215 **
1216 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001217 ** Query with statement journal
1218 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001219 **
1220 ** We could do something more elegant than this static analysis (i.e.
1221 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001222 ** handling malloc() or IO failure is a fairly obscure edge case so
1223 ** this is probably easier. Todo: Might be an opportunity to reduce
1224 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001225 */
1226 int isReadOnly = 1;
1227 int isStatement = 0;
1228 assert(p->aOp || p->nOp==0);
1229 for(i=0; i<p->nOp; i++){
1230 switch( p->aOp[i].opcode ){
1231 case OP_Transaction:
1232 isReadOnly = 0;
1233 break;
1234 case OP_Statement:
1235 isStatement = 1;
1236 break;
1237 }
1238 }
danielk197707cb5602006-01-20 10:55:05 +00001239
1240 /* If the query was read-only, we need do no rollback at all. Otherwise,
1241 ** proceed with the special handling.
1242 */
1243 if( !isReadOnly ){
1244 if( p->rc==SQLITE_NOMEM && isStatement ){
1245 xFunc = sqlite3BtreeRollbackStmt;
1246 }else{
1247 /* We are forced to roll back the active transaction. Before doing
1248 ** so, abort any other statements this handle currently has active.
1249 */
danielk19778d34dfd2006-01-24 16:37:57 +00001250 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001251 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001252 db->autoCommit = 1;
1253 }
danielk1977261919c2005-12-06 12:52:59 +00001254 }
1255 }
danielk197707cb5602006-01-20 10:55:05 +00001256
1257 /* If the auto-commit flag is set and this is the only active vdbe, then
1258 ** we do either a commit or rollback of the current transaction.
1259 **
1260 ** Note: This block also runs if one of the special errors handled
1261 ** above has occured.
1262 */
1263 if( db->autoCommit && db->activeVdbeCnt==1 ){
1264 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1265 /* The auto-commit flag is true, and the vdbe program was
1266 ** successful or hit an 'OR FAIL' constraint. This means a commit
1267 ** is required.
1268 */
1269 int rc = vdbeCommit(db);
1270 if( rc==SQLITE_BUSY ){
1271 return SQLITE_BUSY;
1272 }else if( rc!=SQLITE_OK ){
1273 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001274 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001275 }else{
1276 sqlite3CommitInternalChanges(db);
1277 }
1278 }else{
danielk197797a227c2006-01-20 16:32:04 +00001279 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001280 }
1281 }else if( !xFunc ){
1282 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1283 xFunc = sqlite3BtreeCommitStmt;
1284 }else if( p->errorAction==OE_Abort ){
1285 xFunc = sqlite3BtreeRollbackStmt;
1286 }else{
danielk19778d34dfd2006-01-24 16:37:57 +00001287 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001288 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001289 db->autoCommit = 1;
1290 }
danielk19771d850a72004-05-31 08:26:49 +00001291 }
danielk197707cb5602006-01-20 10:55:05 +00001292
1293 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1294 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1295 ** and the return code is still SQLITE_OK, set the return code to the new
1296 ** error value.
1297 */
1298 assert(!xFunc ||
1299 xFunc==sqlite3BtreeCommitStmt ||
1300 xFunc==sqlite3BtreeRollbackStmt
1301 );
1302 for(i=0; xFunc && i<db->nDb; i++){
1303 int rc;
1304 Btree *pBt = db->aDb[i].pBt;
1305 if( pBt ){
1306 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001307 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1308 p->rc = rc;
1309 sqlite3SetString(&p->zErrMsg, 0);
1310 }
danielk197707cb5602006-01-20 10:55:05 +00001311 }
danielk197777d83ba2004-05-31 10:08:14 +00001312 }
danielk197707cb5602006-01-20 10:55:05 +00001313
1314 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1315 ** set the change counter.
1316 */
1317 if( p->changeCntOn && p->pc>=0 ){
1318 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1319 sqlite3VdbeSetChanges(db, p->nChange);
1320 }else{
1321 sqlite3VdbeSetChanges(db, 0);
1322 }
1323 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001324 }
danielk197707cb5602006-01-20 10:55:05 +00001325
1326 /* Rollback or commit any schema changes that occurred. */
1327 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1328 sqlite3ResetInternalSchema(db, 0);
1329 db->flags = (db->flags | SQLITE_InternChanges);
1330 }
drh9a324642003-09-06 20:12:01 +00001331 }
danielk19771d850a72004-05-31 08:26:49 +00001332
drh92f02c32004-09-02 14:57:08 +00001333 /* We have successfully halted and closed the VM. Record this fact. */
1334 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001335 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001336 }
drh92f02c32004-09-02 14:57:08 +00001337 p->magic = VDBE_MAGIC_HALT;
1338 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001339
drh92f02c32004-09-02 14:57:08 +00001340 return SQLITE_OK;
1341}
1342
1343/*
1344** Clean up a VDBE after execution but do not delete the VDBE just yet.
1345** Write any error messages into *pzErrMsg. Return the result code.
1346**
1347** After this routine is run, the VDBE should be ready to be executed
1348** again.
1349**
1350** To look at it another way, this routine resets the state of the
1351** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1352** VDBE_MAGIC_INIT.
1353*/
1354int sqlite3VdbeReset(Vdbe *p){
1355 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
drhc60d0442004-09-30 13:43:13 +00001356 sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh92f02c32004-09-02 14:57:08 +00001357 return SQLITE_MISUSE;
1358 }
1359
1360 /* If the VM did not run to completion or if it encountered an
1361 ** error, then it might not have been halted properly. So halt
1362 ** it now.
1363 */
1364 sqlite3VdbeHalt(p);
1365
drhfb7e7652005-01-24 00:28:42 +00001366 /* If the VDBE has be run even partially, then transfer the error code
1367 ** and error message from the VDBE into the main database structure. But
1368 ** if the VDBE has just been set to run but has not actually executed any
1369 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001370 */
drhfb7e7652005-01-24 00:28:42 +00001371 if( p->pc>=0 ){
1372 if( p->zErrMsg ){
danielk197797a227c2006-01-20 16:32:04 +00001373 sqlite3* db = p->db;
1374 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
1375 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001376 p->zErrMsg = 0;
1377 }else if( p->rc ){
1378 sqlite3Error(p->db, p->rc, 0);
1379 }else{
1380 sqlite3Error(p->db, SQLITE_OK, 0);
1381 }
danielk1977a21c6b62005-01-24 10:25:59 +00001382 }else if( p->rc && p->expired ){
1383 /* The expired flag was set on the VDBE before the first call
1384 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1385 ** called), set the database error in this case as well.
1386 */
1387 sqlite3Error(p->db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001388 }
1389
1390 /* Reclaim all memory used by the VDBE
1391 */
1392 Cleanup(p);
1393
1394 /* Save profiling information from this VDBE run.
1395 */
danielk1977261919c2005-12-06 12:52:59 +00001396 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
drh9a324642003-09-06 20:12:01 +00001397#ifdef VDBE_PROFILE
1398 {
1399 FILE *out = fopen("vdbe_profile.out", "a");
1400 if( out ){
1401 int i;
1402 fprintf(out, "---- ");
1403 for(i=0; i<p->nOp; i++){
1404 fprintf(out, "%02x", p->aOp[i].opcode);
1405 }
1406 fprintf(out, "\n");
1407 for(i=0; i<p->nOp; i++){
1408 fprintf(out, "%6d %10lld %8lld ",
1409 p->aOp[i].cnt,
1410 p->aOp[i].cycles,
1411 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1412 );
danielk19774adee202004-05-08 08:23:19 +00001413 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001414 }
1415 fclose(out);
1416 }
1417 }
1418#endif
1419 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001420 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001421 if( p->rc==SQLITE_SCHEMA ){
1422 sqlite3ResetInternalSchema(p->db, 0);
1423 }
drh9a324642003-09-06 20:12:01 +00001424 return p->rc;
1425}
drh92f02c32004-09-02 14:57:08 +00001426
drh9a324642003-09-06 20:12:01 +00001427/*
1428** Clean up and delete a VDBE after execution. Return an integer which is
1429** the result code. Write any error message text into *pzErrMsg.
1430*/
danielk19779e6db7d2004-06-21 08:18:51 +00001431int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001432 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001433
danielk1977b5548a82004-06-26 13:51:33 +00001434 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1435 rc = sqlite3VdbeReset(p);
1436 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001437 return SQLITE_MISUSE;
1438 }
danielk19774adee202004-05-08 08:23:19 +00001439 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001440 return rc;
1441}
1442
1443/*
drhf92c7ff2004-06-19 15:40:23 +00001444** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001445** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001446** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001447** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001448*/
1449void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1450 int i;
1451 for(i=0; i<pVdbeFunc->nAux; i++){
1452 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1453 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1454 if( pAux->xDelete ){
1455 pAux->xDelete(pAux->pAux);
1456 }
1457 pAux->pAux = 0;
1458 }
1459 }
1460}
1461
1462/*
drh9a324642003-09-06 20:12:01 +00001463** Delete an entire VDBE.
1464*/
danielk19774adee202004-05-08 08:23:19 +00001465void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001466 int i;
1467 if( p==0 ) return;
1468 Cleanup(p);
1469 if( p->pPrev ){
1470 p->pPrev->pNext = p->pNext;
1471 }else{
1472 assert( p->db->pVdbe==p );
1473 p->db->pVdbe = p->pNext;
1474 }
1475 if( p->pNext ){
1476 p->pNext->pPrev = p->pPrev;
1477 }
drh76ff3a02004-09-24 22:32:30 +00001478 if( p->aOp ){
1479 for(i=0; i<p->nOp; i++){
1480 Op *pOp = &p->aOp[i];
drhb38ad992005-09-16 00:27:01 +00001481 freeP3(pOp->p3type, pOp->p3);
drh9a324642003-09-06 20:12:01 +00001482 }
drh76ff3a02004-09-24 22:32:30 +00001483 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001484 }
drh76ff3a02004-09-24 22:32:30 +00001485 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001486 sqliteFree(p->aLabel);
1487 sqliteFree(p->aStack);
danielk1977955de522006-02-10 02:27:42 +00001488 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh76ff3a02004-09-24 22:32:30 +00001489 sqliteFree(p->aColName);
drh9a324642003-09-06 20:12:01 +00001490 p->magic = VDBE_MAGIC_DEAD;
1491 sqliteFree(p);
1492}
drha11846b2004-01-07 18:52:56 +00001493
1494/*
drha11846b2004-01-07 18:52:56 +00001495** If a MoveTo operation is pending on the given cursor, then do that
1496** MoveTo now. Return an error code. If no MoveTo is pending, this
1497** routine does nothing and returns SQLITE_OK.
1498*/
danielk19774adee202004-05-08 08:23:19 +00001499int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001500 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001501 int res, rc;
danielk1977132872b2004-05-10 10:37:18 +00001502 extern int sqlite3_search_count;
drhf0863fe2005-06-12 21:35:51 +00001503 assert( p->isTable );
1504 if( p->isTable ){
drh536065a2005-01-26 21:55:31 +00001505 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
danielk19776490beb2004-05-11 06:17:21 +00001506 }else{
drh536065a2005-01-26 21:55:31 +00001507 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1508 sizeof(i64),&res);
danielk19776490beb2004-05-11 06:17:21 +00001509 }
drh536065a2005-01-26 21:55:31 +00001510 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001511 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001512 p->lastRowid = keyToInt(p->movetoTarget);
1513 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001514 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001515 rc = sqlite3BtreeNext(p->pCursor, &res);
1516 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001517 }
danielk1977132872b2004-05-10 10:37:18 +00001518 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001519 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001520 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001521 }
1522 return SQLITE_OK;
1523}
danielk19774adee202004-05-08 08:23:19 +00001524
drhab9f7f12004-05-08 10:56:11 +00001525/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001526** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001527**
danielk1977cfcdaef2004-05-12 07:33:33 +00001528** sqlite3VdbeSerialType()
1529** sqlite3VdbeSerialTypeLen()
1530** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001531** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001532** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001533**
1534** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001535** data and index records. Each serialized value consists of a
1536** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1537** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001538**
danielk1977cfcdaef2004-05-12 07:33:33 +00001539** In an SQLite index record, the serial type is stored directly before
1540** the blob of data that it corresponds to. In a table record, all serial
1541** types are stored at the start of the record, and the blobs of data at
1542** the end. Hence these functions allow the caller to handle the
1543** serial-type and data blob seperately.
1544**
1545** The following table describes the various storage classes for data:
1546**
1547** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001548** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001549** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001550** 1 1 signed integer
1551** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001552** 3 3 signed integer
1553** 4 4 signed integer
1554** 5 6 signed integer
1555** 6 8 signed integer
1556** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001557** 8 0 Integer constant 0
1558** 9 0 Integer constant 1
1559** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001560** N>=12 and even (N-12)/2 BLOB
1561** N>=13 and odd (N-13)/2 text
1562**
drh35a59652006-01-02 18:24:40 +00001563** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1564** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001565*/
1566
1567/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001568** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001569*/
drhd946db02005-12-29 19:23:06 +00001570u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001571 int flags = pMem->flags;
1572
1573 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001574 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001575 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001576 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001577 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001578# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
danielk1977cfcdaef2004-05-12 07:33:33 +00001579 i64 i = pMem->i;
drhd946db02005-12-29 19:23:06 +00001580 u64 u;
1581 if( file_format>=4 && (i&1)==i ){
1582 return 8+i;
1583 }
1584 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001585 if( u<=127 ) return 1;
1586 if( u<=32767 ) return 2;
1587 if( u<=8388607 ) return 3;
1588 if( u<=2147483647 ) return 4;
1589 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001590 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001591 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001592 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001593 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001594 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001595 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001596 int n = pMem->n;
1597 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001598 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001599 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001600 if( flags&MEM_Blob ){
1601 return (pMem->n*2 + 12);
1602 }
1603 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001604}
1605
1606/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001607** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001608*/
drh25aa1b42004-05-28 01:39:01 +00001609int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001610 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001611 return (serial_type-12)/2;
1612 }else{
drh57196282004-10-06 15:41:16 +00001613 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001614 return aSize[serial_type];
1615 }
danielk1977192ac1d2004-05-10 07:17:30 +00001616}
1617
1618/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001619** Write the serialized data blob for the value stored in pMem into
1620** buf. It is assumed that the caller has allocated sufficient space.
1621** Return the number of bytes written.
1622*/
drhd946db02005-12-29 19:23:06 +00001623int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem, int file_format){
1624 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001625 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001626
drh1483e142004-05-21 21:12:42 +00001627 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001628 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001629 u64 v;
1630 int i;
drha19b7752004-05-30 21:14:58 +00001631 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001632 v = *(u64*)&pMem->r;
1633 }else{
1634 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001635 }
drh1483e142004-05-21 21:12:42 +00001636 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1637 while( i-- ){
1638 buf[i] = (v&0xFF);
1639 v >>= 8;
1640 }
1641 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001642 }
drhd946db02005-12-29 19:23:06 +00001643
danielk1977cfcdaef2004-05-12 07:33:33 +00001644 /* String or blob */
drhd946db02005-12-29 19:23:06 +00001645 if( serial_type>=12 ){
1646 len = sqlite3VdbeSerialTypeLen(serial_type);
1647 memcpy(buf, pMem->z, len);
1648 return len;
1649 }
1650
1651 /* NULL or constants 0 or 1 */
1652 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001653}
1654
1655/*
1656** Deserialize the data blob pointed to by buf as serial type serial_type
1657** and store the result in pMem. Return the number of bytes read.
1658*/
danielk1977b1bc9532004-05-22 03:05:33 +00001659int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001660 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001661 u32 serial_type, /* Serial type to deserialize */
1662 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001663){
drh3c685822005-05-21 18:32:18 +00001664 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00001665 case 10: /* Reserved for future use */
1666 case 11: /* Reserved for future use */
1667 case 0: { /* NULL */
1668 pMem->flags = MEM_Null;
1669 break;
1670 }
1671 case 1: { /* 1-byte signed integer */
1672 pMem->i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001673 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001674 return 1;
drh1483e142004-05-21 21:12:42 +00001675 }
drh3c685822005-05-21 18:32:18 +00001676 case 2: { /* 2-byte signed integer */
1677 pMem->i = (((signed char)buf[0])<<8) | buf[1];
1678 pMem->flags = MEM_Int;
1679 return 2;
1680 }
1681 case 3: { /* 3-byte signed integer */
1682 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1683 pMem->flags = MEM_Int;
1684 return 3;
1685 }
1686 case 4: { /* 4-byte signed integer */
1687 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1688 pMem->flags = MEM_Int;
1689 return 4;
1690 }
1691 case 5: { /* 6-byte signed integer */
1692 u64 x = (((signed char)buf[0])<<8) | buf[1];
1693 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1694 x = (x<<32) | y;
1695 pMem->i = *(i64*)&x;
1696 pMem->flags = MEM_Int;
1697 return 6;
1698 }
drh91124b32005-08-18 18:15:05 +00001699 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00001700 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00001701 u64 x;
1702 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00001703#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00001704 /* Verify that integers and floating point values use the same
drhbfd6b032005-08-28 01:38:44 +00001705 ** byte order. The byte order differs on some (broken) architectures.
1706 */
drhde941c62005-08-28 01:34:21 +00001707 static const u64 t1 = ((u64)0x3ff00000)<<32;
1708 assert( 1.0==*(double*)&t1 );
1709#endif
drhbfd6b032005-08-28 01:38:44 +00001710
drhd81bd4e2005-09-05 20:06:49 +00001711 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1712 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00001713 x = (x<<32) | y;
1714 if( serial_type==6 ){
1715 pMem->i = *(i64*)&x;
1716 pMem->flags = MEM_Int;
1717 }else{
1718 pMem->r = *(double*)&x;
1719 pMem->flags = MEM_Real;
1720 }
1721 return 8;
1722 }
drhd946db02005-12-29 19:23:06 +00001723 case 8: /* Integer 0 */
1724 case 9: { /* Integer 1 */
1725 pMem->i = serial_type-8;
1726 pMem->flags = MEM_Int;
1727 return 0;
1728 }
drh3c685822005-05-21 18:32:18 +00001729 default: {
1730 int len = (serial_type-12)/2;
1731 pMem->z = (char *)buf;
1732 pMem->n = len;
1733 pMem->xDel = 0;
1734 if( serial_type&0x01 ){
1735 pMem->flags = MEM_Str | MEM_Ephem;
1736 }else{
1737 pMem->flags = MEM_Blob | MEM_Ephem;
1738 }
1739 return len;
drh696b32f2004-05-30 01:51:52 +00001740 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001741 }
drh3c685822005-05-21 18:32:18 +00001742 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001743}
1744
1745/*
drh0e6082e2006-01-12 20:28:35 +00001746** The header of a record consists of a sequence variable-length integers.
1747** These integers are almost always small and are encoded as a single byte.
1748** The following macro takes advantage this fact to provide a fast decode
1749** of the integers in a record header. It is faster for the common case
1750** where the integer is a single byte. It is a little slower when the
1751** integer is two or more bytes. But overall it is faster.
1752**
1753** The following expressions are equivalent:
1754**
1755** x = sqlite3GetVarint32( A, &B );
1756**
1757** x = GetVarint( A, B );
1758**
1759*/
1760#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
1761
1762/*
drh7a224de2004-06-02 01:22:02 +00001763** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001764** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1765** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001766** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1767** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001768*/
drh7a224de2004-06-02 01:22:02 +00001769int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001770 void *userData,
1771 int nKey1, const void *pKey1,
1772 int nKey2, const void *pKey2
1773){
drhd3d39e92004-05-20 22:16:29 +00001774 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001775 u32 d1, d2; /* Offset into aKey[] of next data element */
1776 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1777 u32 szHdr1, szHdr2; /* Number of bytes in header */
1778 int i = 0;
1779 int nField;
1780 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001781 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1782 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001783
1784 Mem mem1;
1785 Mem mem2;
1786 mem1.enc = pKeyInfo->enc;
1787 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001788
drh0e6082e2006-01-12 20:28:35 +00001789 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00001790 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00001791 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00001792 d2 = szHdr2;
1793 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001794 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001795 u32 serial_type1;
1796 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001797
1798 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00001799 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00001800 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00001801 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00001802 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001803
1804 /* Assert that there is enough space left in each key for the blob of
1805 ** data to go with the serial type just read. This assert may fail if
1806 ** the file is corrupted. Then read the value from each key into mem1
1807 ** and mem2 respectively.
1808 */
drh25aa1b42004-05-28 01:39:01 +00001809 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1810 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001811
drhd5788202004-05-28 08:21:05 +00001812 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001813 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1814 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001815 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001816 break;
1817 }
1818 i++;
1819 }
1820
1821 /* One of the keys ran out of fields, but all the fields up to that point
1822 ** were equal. If the incrKey flag is true, then the second key is
1823 ** treated as larger.
1824 */
1825 if( rc==0 ){
1826 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001827 rc = -1;
1828 }else if( d1<nKey1 ){
1829 rc = 1;
1830 }else if( d2<nKey2 ){
1831 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001832 }
drh0b2f3162005-12-21 18:36:45 +00001833 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
1834 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00001835 rc = -rc;
1836 }
1837
1838 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001839}
drhd5788202004-05-28 08:21:05 +00001840
1841/*
drh7a224de2004-06-02 01:22:02 +00001842** The argument is an index entry composed using the OP_MakeRecord opcode.
1843** The last entry in this record should be an integer (specifically
1844** an integer rowid). This routine returns the number of bytes in
1845** that integer.
drhd5788202004-05-28 08:21:05 +00001846*/
drh74161702006-02-24 02:53:49 +00001847int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00001848 u32 szHdr; /* Size of the header */
1849 u32 typeRowid; /* Serial type of the rowid */
1850
1851 sqlite3GetVarint32(aKey, &szHdr);
1852 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1853 return sqlite3VdbeSerialTypeLen(typeRowid);
1854}
danielk1977eb015e02004-05-18 01:31:14 +00001855
1856
1857/*
drh7a224de2004-06-02 01:22:02 +00001858** pCur points at an index entry created using the OP_MakeRecord opcode.
1859** Read the rowid (the last field in the record) and store it in *rowid.
1860** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001861*/
1862int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001863 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001864 int rc;
drhd5788202004-05-28 08:21:05 +00001865 u32 szHdr; /* Size of the header */
1866 u32 typeRowid; /* Serial type of the rowid */
1867 u32 lenRowid; /* Size of the rowid */
1868 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001869
drhd5788202004-05-28 08:21:05 +00001870 sqlite3BtreeKeySize(pCur, &nCellKey);
1871 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00001872 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00001873 }
1874 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1875 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001876 return rc;
1877 }
drh2646da72005-12-09 20:02:05 +00001878 sqlite3GetVarint32((u8*)m.z, &szHdr);
1879 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00001880 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00001881 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drhd5788202004-05-28 08:21:05 +00001882 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001883 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001884 return SQLITE_OK;
1885}
1886
drh7cf6e4d2004-05-19 14:56:55 +00001887/*
drhd3d39e92004-05-20 22:16:29 +00001888** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001889** the key string in pKey (of length nKey). Write into *pRes a number
1890** that is negative, zero, or positive if pC is less than, equal to,
1891** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001892**
drhd5788202004-05-28 08:21:05 +00001893** pKey is either created without a rowid or is truncated so that it
1894** omits the rowid at the end. The rowid at the end of the index entry
1895** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001896*/
danielk1977183f9f72004-05-13 05:20:26 +00001897int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001898 Cursor *pC, /* The cursor to compare against */
1899 int nKey, const u8 *pKey, /* The key to compare */
1900 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001901){
danielk1977e0d4b062004-06-28 01:11:46 +00001902 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001903 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001904 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001905 int lenRowid;
1906 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001907
1908 sqlite3BtreeKeySize(pCur, &nCellKey);
1909 if( nCellKey<=0 ){
1910 *res = 0;
1911 return SQLITE_OK;
1912 }
drhd5788202004-05-28 08:21:05 +00001913 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1914 if( rc ){
1915 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001916 }
drh74161702006-02-24 02:53:49 +00001917 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00001918 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001919 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001920 return SQLITE_OK;
1921}
danielk1977b28af712004-06-21 06:50:26 +00001922
1923/*
1924** This routine sets the value to be returned by subsequent calls to
1925** sqlite3_changes() on the database handle 'db'.
1926*/
1927void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1928 db->nChange = nChange;
1929 db->nTotalChange += nChange;
1930}
1931
1932/*
1933** Set a flag in the vdbe to update the change counter when it is finalised
1934** or reset.
1935*/
drh4794f732004-11-05 17:17:50 +00001936void sqlite3VdbeCountChanges(Vdbe *v){
1937 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00001938}
drhd89bd002005-01-22 03:03:54 +00001939
1940/*
1941** Mark every prepared statement associated with a database connection
1942** as expired.
1943**
1944** An expired statement means that recompilation of the statement is
1945** recommend. Statements expire when things happen that make their
1946** programs obsolete. Removing user-defined functions or collating
1947** sequences, or changing an authorization function are the types of
1948** things that make prepared statements obsolete.
1949*/
1950void sqlite3ExpirePreparedStatements(sqlite3 *db){
1951 Vdbe *p;
1952 for(p = db->pVdbe; p; p=p->pNext){
1953 p->expired = 1;
1954 }
1955}
danielk1977aee18ef2005-03-09 12:26:50 +00001956
1957/*
1958** Return the database associated with the Vdbe.
1959*/
1960sqlite3 *sqlite3VdbeDb(Vdbe *v){
1961 return v->db;
1962}