blob: cf81cfeef22b4c36ff6d65c0c25c4ec00c76fd63 [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
61** the Vdbe.aOp array will be sized to contain exactly N
drh76ff3a02004-09-24 22:32:30 +000062** elements.
63*/
64static void resizeOpArray(Vdbe *p, int N){
danielk1977634f2982005-03-28 08:44:07 +000065 if( p->magic==VDBE_MAGIC_RUN ){
66 assert( N==p->nOp );
67 p->nOpAlloc = N;
68 p->aOp = sqliteRealloc(p->aOp, N*sizeof(Op));
69 }else if( p->nOpAlloc<N ){
drh76ff3a02004-09-24 22:32:30 +000070 int oldSize = p->nOpAlloc;
71 p->nOpAlloc = N+100;
72 p->aOp = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
73 if( p->aOp ){
74 memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
75 }
76 }
77}
78
79/*
drh9a324642003-09-06 20:12:01 +000080** Add a new instruction to the list of instructions current in the
81** VDBE. Return the address of the new instruction.
82**
83** Parameters:
84**
85** p Pointer to the VDBE
86**
87** op The opcode for this instruction
88**
89** p1, p2 First two of the three possible operands.
90**
danielk19774adee202004-05-08 08:23:19 +000091** Use the sqlite3VdbeResolveLabel() function to fix an address and
92** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000093** operand.
94*/
danielk19774adee202004-05-08 08:23:19 +000095int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000096 int i;
drh701a0ae2004-02-22 20:05:00 +000097 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +000098
99 i = p->nOp;
100 p->nOp++;
101 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000102 resizeOpArray(p, i+1);
103 if( p->aOp==0 ){
104 return 0;
drh9a324642003-09-06 20:12:01 +0000105 }
drh701a0ae2004-02-22 20:05:00 +0000106 pOp = &p->aOp[i];
107 pOp->opcode = op;
108 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000109 pOp->p2 = p2;
110 pOp->p3 = 0;
111 pOp->p3type = P3_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000112 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000113#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000114 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000115#endif
116 return i;
117}
118
119/*
drh701a0ae2004-02-22 20:05:00 +0000120** Add an opcode that includes the p3 value.
121*/
drheb2e1762004-05-27 01:53:56 +0000122int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000123 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
124 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000125 return addr;
126}
127
128/*
drh9a324642003-09-06 20:12:01 +0000129** Create a new symbolic label for an instruction that has yet to be
130** coded. The symbolic label is really just a negative number. The
131** label can be used as the P2 value of an operation. Later, when
132** the label is resolved to a specific address, the VDBE will scan
133** through its operation list and change all values of P2 which match
134** the label into the resolved address.
135**
136** The VDBE knows that a P2 value is a label because labels are
137** always negative and P2 values are suppose to be non-negative.
138** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000139**
140** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000141*/
danielk19774adee202004-05-08 08:23:19 +0000142int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000143 int i;
144 i = p->nLabel++;
145 assert( p->magic==VDBE_MAGIC_INIT );
146 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000147 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
drh76ff3a02004-09-24 22:32:30 +0000148 p->aLabel = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000149 }
drh76ff3a02004-09-24 22:32:30 +0000150 if( p->aLabel ){
151 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000152 }
drh9a324642003-09-06 20:12:01 +0000153 return -1-i;
154}
155
156/*
157** Resolve label "x" to be the address of the next instruction to
158** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000159** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000160*/
danielk19774adee202004-05-08 08:23:19 +0000161void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000162 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000163 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000164 assert( j>=0 && j<p->nLabel );
165 if( p->aLabel ){
166 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000167 }
168}
169
170/*
danielk1977bc04f852005-03-29 08:26:13 +0000171** Return non-zero if opcode 'op' is guarenteed not to push more values
172** onto the VDBE stack than it pops off.
173*/
danielk19777a5147c2005-03-29 13:07:00 +0000174static int opcodeNoPush(u8 op){
175 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000176 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
177 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000178 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000179 ** in a comment on the same line as the "case OP_XXX:" in
180 ** sqlite3VdbeExec() in vdbe.c.
181 **
182 ** If the bit is true, then the corresponding opcode is guarenteed not
183 ** to grow the stack when it is executed. Otherwise, it may grow the
184 ** stack by at most one entry.
185 **
danielk19777a5147c2005-03-29 13:07:00 +0000186 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000187 ** one bit for opcodes 16 to 31, and so on.
188 **
189 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
190 ** because the file is generated by an awk program. Awk manipulates
191 ** all numbers as floating-point and we don't want to risk a rounding
192 ** error if someone builds with an awk that uses (for example) 32-bit
193 ** IEEE floats.
194 */
drh9a7e6082005-03-31 22:26:19 +0000195 static const u32 masks[5] = {
danielk19777a5147c2005-03-29 13:07:00 +0000196 NOPUSH_MASK_0 + (NOPUSH_MASK_1<<16),
197 NOPUSH_MASK_2 + (NOPUSH_MASK_3<<16),
198 NOPUSH_MASK_4 + (NOPUSH_MASK_5<<16),
199 NOPUSH_MASK_6 + (NOPUSH_MASK_7<<16),
200 NOPUSH_MASK_8 + (NOPUSH_MASK_9<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000201 };
202 return (masks[op>>5] & (1<<(op&0x1F)));
203}
204
205#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000206int sqlite3VdbeOpcodeNoPush(u8 op){
207 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000208}
209#endif
210
211/*
drh76ff3a02004-09-24 22:32:30 +0000212** Loop through the program looking for P2 values that are negative.
213** Each such value is a label. Resolve the label by setting the P2
214** value to its correct non-zero value.
215**
216** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000217**
218** Variable *pMaxFuncArgs is set to the maximum value of any P1 argument
219** to an OP_Function or P2 to an OP_AggFunc opcode. This is used by
220** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000221**
222** The integer *pMaxStack is set to the maximum number of vdbe stack
223** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000224**
225** This routine also does the following optimization: It scans for
226** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
drhf0863fe2005-06-12 21:35:51 +0000227** IdxInsert instructions where P2!=0. If no such instruction is
drh38449902005-06-07 01:43:41 +0000228** found, then every Statement instruction is changed to a Noop. In
229** this way, we avoid creating the statement journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000230*/
danielk1977bc04f852005-03-29 08:26:13 +0000231static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000232 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000233 int nMaxArgs = 0;
234 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000235 Op *pOp;
236 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000237 int doesStatementRollback = 0;
238 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000239 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000240 u8 opcode = pOp->opcode;
241
242 /* Todo: Maybe OP_AggFunc should change to use P1 in the same
danielk1977bc04f852005-03-29 08:26:13 +0000243 * way as OP_Function.
244 */
danielk1977634f2982005-03-28 08:44:07 +0000245 if( opcode==OP_Function ){
danielk1977bc04f852005-03-29 08:26:13 +0000246 if( pOp->p1>nMaxArgs ) nMaxArgs = pOp->p1;
danielk1977634f2982005-03-28 08:44:07 +0000247 }else if( opcode==OP_AggFunc ){
danielk1977bc04f852005-03-29 08:26:13 +0000248 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh38449902005-06-07 01:43:41 +0000249 }else if( opcode==OP_Halt ){
250 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
251 doesStatementRollback = 1;
252 }
drhf0863fe2005-06-12 21:35:51 +0000253 }else if( opcode==OP_IdxInsert ){
drh38449902005-06-07 01:43:41 +0000254 if( pOp->p2 ){
255 doesStatementRollback = 1;
256 }
257 }else if( opcode==OP_Statement ){
258 hasStatementBegin = 1;
danielk1977bc04f852005-03-29 08:26:13 +0000259 }
260
danielk19777a5147c2005-03-29 13:07:00 +0000261 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000262 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000263 }
264
drh76ff3a02004-09-24 22:32:30 +0000265 if( pOp->p2>=0 ) continue;
266 assert( -1-pOp->p2<p->nLabel );
267 pOp->p2 = aLabel[-1-pOp->p2];
268 }
269 sqliteFree(p->aLabel);
270 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000271
272 *pMaxFuncArgs = nMaxArgs;
273 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000274
275 /* If we never rollback a statement transaction, then statement
276 ** transactions are not needed. So change every OP_Statement
277 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
278 ** which can be expensive on some platforms.
279 */
280 if( hasStatementBegin && !doesStatementRollback ){
281 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
282 if( pOp->opcode==OP_Statement ){
283 pOp->opcode = OP_Noop;
284 }
285 }
286 }
drh76ff3a02004-09-24 22:32:30 +0000287}
288
289/*
drh9a324642003-09-06 20:12:01 +0000290** Return the address of the next instruction to be inserted.
291*/
danielk19774adee202004-05-08 08:23:19 +0000292int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000293 assert( p->magic==VDBE_MAGIC_INIT );
294 return p->nOp;
295}
296
297/*
298** Add a whole list of operations to the operation stack. Return the
299** address of the first operation added.
300*/
danielk19774adee202004-05-08 08:23:19 +0000301int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000302 int addr;
303 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000304 resizeOpArray(p, p->nOp + nOp);
305 if( p->aOp==0 ){
306 return 0;
drh9a324642003-09-06 20:12:01 +0000307 }
308 addr = p->nOp;
309 if( nOp>0 ){
310 int i;
drh905793e2004-02-21 13:31:09 +0000311 VdbeOpList const *pIn = aOp;
312 for(i=0; i<nOp; i++, pIn++){
313 int p2 = pIn->p2;
314 VdbeOp *pOut = &p->aOp[i+addr];
315 pOut->opcode = pIn->opcode;
316 pOut->p1 = pIn->p1;
317 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
318 pOut->p3 = pIn->p3;
319 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000320#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000321 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000322 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000323 }
324#endif
325 }
326 p->nOp += nOp;
327 }
328 return addr;
329}
330
331/*
332** Change the value of the P1 operand for a specific instruction.
333** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000334** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000335** few minor changes to the program.
336*/
danielk19774adee202004-05-08 08:23:19 +0000337void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000338 assert( p->magic==VDBE_MAGIC_INIT );
339 if( p && addr>=0 && p->nOp>addr && p->aOp ){
340 p->aOp[addr].p1 = val;
341 }
342}
343
344/*
345** Change the value of the P2 operand for a specific instruction.
346** This routine is useful for setting a jump destination.
347*/
danielk19774adee202004-05-08 08:23:19 +0000348void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000349 assert( val>=0 );
350 assert( p->magic==VDBE_MAGIC_INIT );
351 if( p && addr>=0 && p->nOp>addr && p->aOp ){
352 p->aOp[addr].p2 = val;
353 }
354}
355
356/*
357** Change the value of the P3 operand for a specific instruction.
358** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000359** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000360** few minor changes to the program.
361**
362** If n>=0 then the P3 operand is dynamic, meaning that a copy of
363** the string is made into memory obtained from sqliteMalloc().
364** A value of n==0 means copy bytes of zP3 up to and including the
365** first null byte. If n>0 then copy n+1 bytes of zP3.
366**
danielk19771f55c052005-05-19 08:42:59 +0000367** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
368** A copy is made of the KeyInfo structure into memory obtained from
369** sqliteMalloc, to be freed when the Vdbe is finalized.
370** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
371** stored in memory that the caller has obtained from sqliteMalloc. The
372** caller should not free the allocation, it will be freed when the Vdbe is
373** finalized.
374**
375** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
376** to a string or structure that is guaranteed to exist for the lifetime of
377** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000378**
379** If addr<0 then change P3 on the most recently inserted instruction.
380*/
danielk19774adee202004-05-08 08:23:19 +0000381void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000382 Op *pOp;
383 assert( p->magic==VDBE_MAGIC_INIT );
danielk1977d5d56522005-03-16 12:15:20 +0000384 if( p==0 || p->aOp==0 ){
385 if( n==P3_DYNAMIC || n==P3_KEYINFO_HANDOFF ){
drh47b4b292005-03-19 14:45:48 +0000386 sqliteFree((void*)zP3);
danielk1977d5d56522005-03-16 12:15:20 +0000387 }
danielk19771f723bd2005-05-26 12:37:29 +0000388 if( n==P3_MEM ){
389 sqlite3ValueFree((sqlite3_value *)zP3);
390 }
danielk1977d5d56522005-03-16 12:15:20 +0000391 return;
392 }
drh9a324642003-09-06 20:12:01 +0000393 if( addr<0 || addr>=p->nOp ){
394 addr = p->nOp - 1;
395 if( addr<0 ) return;
396 }
397 pOp = &p->aOp[addr];
398 if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
399 sqliteFree(pOp->p3);
400 pOp->p3 = 0;
401 }
402 if( zP3==0 ){
403 pOp->p3 = 0;
404 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000405 }else if( n==P3_KEYINFO ){
406 KeyInfo *pKeyInfo;
407 int nField, nByte;
408 nField = ((KeyInfo*)zP3)->nField;
409 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]);
drheafe05b2004-06-13 00:54:01 +0000410 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000411 pOp->p3 = (char*)pKeyInfo;
412 if( pKeyInfo ){
413 memcpy(pKeyInfo, zP3, nByte);
414 pOp->p3type = P3_KEYINFO;
415 }else{
416 pOp->p3type = P3_NOTUSED;
417 }
drhffbc3082004-05-21 01:29:06 +0000418 }else if( n==P3_KEYINFO_HANDOFF ){
419 pOp->p3 = (char*)zP3;
420 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000421 }else if( n<0 ){
422 pOp->p3 = (char*)zP3;
423 pOp->p3type = n;
424 }else{
drhae29ffb2004-09-25 14:39:18 +0000425 if( n==0 ) n = strlen(zP3);
426 pOp->p3 = sqliteStrNDup(zP3, n);
drh9a324642003-09-06 20:12:01 +0000427 pOp->p3type = P3_DYNAMIC;
428 }
429}
430
drhad6d9462004-09-19 02:15:24 +0000431#ifndef NDEBUG
432/*
433** Replace the P3 field of the most recently coded instruction with
434** comment text.
435*/
436void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
437 va_list ap;
438 assert( p->nOp>0 );
drh76ff3a02004-09-24 22:32:30 +0000439 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 );
drhad6d9462004-09-19 02:15:24 +0000440 va_start(ap, zFormat);
441 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
442 va_end(ap);
443}
444#endif
445
drh9a324642003-09-06 20:12:01 +0000446/*
danielk197784ac9d02004-05-18 09:58:06 +0000447** Search the current program starting at instruction addr for the given
448** opcode and P2 value. Return the address plus 1 if found and 0 if not
449** found.
drh9a324642003-09-06 20:12:01 +0000450*/
danielk197784ac9d02004-05-18 09:58:06 +0000451int sqlite3VdbeFindOp(Vdbe *p, int addr, int op, int p2){
drh9a324642003-09-06 20:12:01 +0000452 int i;
453 assert( p->magic==VDBE_MAGIC_INIT );
danielk197784ac9d02004-05-18 09:58:06 +0000454 for(i=addr; i<p->nOp; i++){
drh9a324642003-09-06 20:12:01 +0000455 if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1;
456 }
457 return 0;
458}
459
460/*
461** Return the opcode for a given address.
462*/
danielk19774adee202004-05-08 08:23:19 +0000463VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000464 assert( p->magic==VDBE_MAGIC_INIT );
465 assert( addr>=0 && addr<p->nOp );
466 return &p->aOp[addr];
467}
468
drhb7f91642004-10-31 02:22:47 +0000469#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
470 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000471/*
drhd3d39e92004-05-20 22:16:29 +0000472** Compute a string that describes the P3 parameter for an opcode.
473** Use zTemp for any required temporary buffer space.
474*/
475static char *displayP3(Op *pOp, char *zTemp, int nTemp){
476 char *zP3;
477 assert( nTemp>=20 );
478 switch( pOp->p3type ){
drhd3d39e92004-05-20 22:16:29 +0000479 case P3_KEYINFO: {
480 int i, j;
481 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
482 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
483 i = strlen(zTemp);
484 for(j=0; j<pKeyInfo->nField; j++){
485 CollSeq *pColl = pKeyInfo->aColl[j];
486 if( pColl ){
487 int n = strlen(pColl->zName);
488 if( i+n>nTemp-6 ){
489 strcpy(&zTemp[i],",...");
490 break;
491 }
492 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000493 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000494 zTemp[i++] = '-';
495 }
496 strcpy(&zTemp[i], pColl->zName);
497 i += n;
498 }else if( i+4<nTemp-6 ){
499 strcpy(&zTemp[i],",nil");
500 i += 4;
501 }
502 }
503 zTemp[i++] = ')';
504 zTemp[i] = 0;
505 assert( i<nTemp );
506 zP3 = zTemp;
507 break;
508 }
509 case P3_COLLSEQ: {
510 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000511 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000512 zP3 = zTemp;
513 break;
514 }
drhf9b596e2004-05-26 16:54:42 +0000515 case P3_FUNCDEF: {
516 FuncDef *pDef = (FuncDef*)pOp->p3;
517 char zNum[30];
518 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
519 sprintf(zNum,"(%d)", pDef->nArg);
520 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
521 strcat(zTemp, zNum);
522 }
523 zP3 = zTemp;
524 break;
525 }
drhd3d39e92004-05-20 22:16:29 +0000526 default: {
527 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000528 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000529 zP3 = "";
530 }
531 }
532 }
533 return zP3;
534}
drhb7f91642004-10-31 02:22:47 +0000535#endif
drhd3d39e92004-05-20 22:16:29 +0000536
537
danielk19778b60e0f2005-01-12 09:10:39 +0000538#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000539/*
540** Print a single opcode. This routine is used for debugging only.
541*/
danielk19774adee202004-05-08 08:23:19 +0000542void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000543 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000544 char zPtr[50];
545 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000546 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000547 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000548 fprintf(pOut, zFormat1,
549 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000550 fflush(pOut);
551}
552#endif
553
554/*
drh76ff3a02004-09-24 22:32:30 +0000555** Release an array of N Mem elements
556*/
557static void releaseMemArray(Mem *p, int N){
558 if( p ){
559 while( N-->0 ){
560 sqlite3VdbeMemRelease(p++);
561 }
562 }
563}
564
drhb7f91642004-10-31 02:22:47 +0000565#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000566/*
drh9a324642003-09-06 20:12:01 +0000567** Give a listing of the program in the virtual machine.
568**
danielk19774adee202004-05-08 08:23:19 +0000569** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000570** running the code, it invokes the callback once for each instruction.
571** This feature is used to implement "EXPLAIN".
572*/
danielk19774adee202004-05-08 08:23:19 +0000573int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000574 Vdbe *p /* The VDBE */
575){
drh9bb575f2004-09-06 17:24:11 +0000576 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000577 int i;
drh826fb5a2004-02-14 23:59:57 +0000578 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000579
drh9a324642003-09-06 20:12:01 +0000580 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000581 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
582 assert( db->magic==SQLITE_MAGIC_BUSY );
583 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000584
585 /* Even though this opcode does not put dynamic strings onto the
586 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000587 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000588 */
589 if( p->pTos==&p->aStack[4] ){
drh76ff3a02004-09-24 22:32:30 +0000590 releaseMemArray(p->aStack, 5);
danielk197718f41892004-05-22 07:27:46 +0000591 }
danielk197718f41892004-05-22 07:27:46 +0000592 p->resOnStack = 0;
593
drhc5cdca62005-01-11 16:54:14 +0000594
danielk197718f41892004-05-22 07:27:46 +0000595 i = p->pc++;
drh826fb5a2004-02-14 23:59:57 +0000596 if( i>=p->nOp ){
597 p->rc = SQLITE_OK;
598 rc = SQLITE_DONE;
599 }else if( db->flags & SQLITE_Interrupt ){
600 db->flags &= ~SQLITE_Interrupt;
drhc5cdca62005-01-11 16:54:14 +0000601 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000602 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000603 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000604 }else{
drhd3d39e92004-05-20 22:16:29 +0000605 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000606 Mem *pMem = p->aStack;
607 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000608 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000609 pMem->i = i; /* Program counter */
610 pMem++;
611
612 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
613 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
614 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000615 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000616 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000617 pMem++;
618
619 pMem->flags = MEM_Int;
620 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000621 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000622 pMem++;
623
624 pMem->flags = MEM_Int;
625 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000626 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000627 pMem++;
628
629 pMem->flags = MEM_Short|MEM_Str|MEM_Term; /* P3 */
630 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drh9c054832004-05-31 18:51:57 +0000631 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000632 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000633
drh826fb5a2004-02-14 23:59:57 +0000634 p->nResColumn = 5;
drheb2e1762004-05-27 01:53:56 +0000635 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000636 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000637 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000638 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000639 }
drh826fb5a2004-02-14 23:59:57 +0000640 return rc;
drh9a324642003-09-06 20:12:01 +0000641}
drhb7f91642004-10-31 02:22:47 +0000642#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000643
644/*
drh3f7d4e42004-07-24 14:35:58 +0000645** Print the SQL that was used to generate a VDBE program.
646*/
647void sqlite3VdbePrintSql(Vdbe *p){
648#ifdef SQLITE_DEBUG
649 int nOp = p->nOp;
650 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000651 if( nOp<1 ) return;
652 pOp = &p->aOp[nOp-1];
drh3f7d4e42004-07-24 14:35:58 +0000653 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
654 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000655 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000656 printf("SQL: [%s]\n", z);
657 }
658#endif
659}
660
661/*
drh9a324642003-09-06 20:12:01 +0000662** Prepare a virtual machine for execution. This involves things such
663** as allocating stack space and initializing the program counter.
664** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000665** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000666**
667** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
668** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000669*/
danielk19774adee202004-05-08 08:23:19 +0000670void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000671 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000672 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000673 int nMem, /* Number of memory cells to allocate */
674 int nCursor, /* Number of cursors to allocate */
danielk1977b3bce662005-01-29 08:32:43 +0000675 int nAgg, /* Number of aggregate contexts required */
drh9a324642003-09-06 20:12:01 +0000676 int isExplain /* True if the EXPLAIN keywords is present */
677){
678 int n;
679
680 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000681 assert( p->magic==VDBE_MAGIC_INIT );
682
drhc16a03b2004-09-15 13:38:10 +0000683 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000684 */
drhc16a03b2004-09-15 13:38:10 +0000685 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000686
danielk1977634f2982005-03-28 08:44:07 +0000687 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
688 * is because the call to resizeOpArray() below may shrink the
689 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
690 * state.
691 */
692 p->magic = VDBE_MAGIC_RUN;
693
drh9a324642003-09-06 20:12:01 +0000694 /* No instruction ever pushes more than a single element onto the
695 ** stack. And the stack never grows on successive executions of the
696 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000697 ** on the maximum stack depth required. (Added later:) The
698 ** resolveP2Values() call computes a tighter upper bound on the
699 ** stack size.
drh9a324642003-09-06 20:12:01 +0000700 **
701 ** Allocation all the stack space we will ever need.
702 */
drh82a48512003-09-06 22:45:20 +0000703 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000704 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000705 int nStack; /* Maximum number of stack entries required */
706 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000707 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000708 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000709 assert( nStack<p->nOp );
710 nStack = isExplain ? 10 : nStack;
drh82a48512003-09-06 22:45:20 +0000711 p->aStack = sqliteMalloc(
danielk1977bc04f852005-03-29 08:26:13 +0000712 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000713 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000714 + nVar*sizeof(Mem) /* aVar */
715 + nVar*sizeof(char*) /* azVar */
716 + nMem*sizeof(Mem) /* aMem */
717 + nCursor*sizeof(Cursor*) /* apCsr */
danielk1977b3bce662005-01-29 08:32:43 +0000718 + nAgg*sizeof(Agg) /* Aggregate contexts */
drh82a48512003-09-06 22:45:20 +0000719 );
drh290c1942004-08-21 17:54:45 +0000720 if( !sqlite3_malloc_failed ){
danielk1977bc04f852005-03-29 08:26:13 +0000721 p->aMem = &p->aStack[nStack];
drh290c1942004-08-21 17:54:45 +0000722 p->nMem = nMem;
drh86f43302004-10-05 17:37:36 +0000723 p->aVar = &p->aMem[nMem];
724 p->nVar = nVar;
725 p->okVar = 0;
726 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000727 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000728 p->apCsr = (Cursor**)&p->azVar[nVar];
danielk1977b3bce662005-01-29 08:32:43 +0000729 if( nAgg>0 ){
730 p->nAgg = nAgg;
731 p->apAgg = (Agg*)&p->apCsr[nCursor];
732 }
drh290c1942004-08-21 17:54:45 +0000733 p->nCursor = nCursor;
734 for(n=0; n<nVar; n++){
735 p->aVar[n].flags = MEM_Null;
736 }
danielk197754db47e2004-05-19 10:36:43 +0000737 }
drh82a48512003-09-06 22:45:20 +0000738 }
danielk1977b3bce662005-01-29 08:32:43 +0000739 p->pAgg = p->apAgg;
740 for(n=0; n<p->nMem; n++){
741 p->aMem[n].flags = MEM_Null;
742 }
drh9a324642003-09-06 20:12:01 +0000743
drhfaa57ac2004-06-09 14:01:51 +0000744#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000745 if( (p->db->flags & SQLITE_VdbeListing)!=0
746 || sqlite3OsFileExists("vdbe_explain")
747 ){
drh80242052004-06-09 00:48:12 +0000748 int i;
749 printf("VDBE Program Listing:\n");
drh3f7d4e42004-07-24 14:35:58 +0000750 sqlite3VdbePrintSql(p);
drh80242052004-06-09 00:48:12 +0000751 for(i=0; i<p->nOp; i++){
752 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
753 }
754 }
danielk19774adee202004-05-08 08:23:19 +0000755 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000756 p->trace = stdout;
757 }
758#endif
drh6810ce62004-01-31 19:22:56 +0000759 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000760 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000761 p->rc = SQLITE_OK;
762 p->uniqueCnt = 0;
763 p->returnDepth = 0;
764 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000765 p->popStack = 0;
766 p->explain |= isExplain;
767 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000768 p->nChange = 0;
drh9a324642003-09-06 20:12:01 +0000769#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000770 {
771 int i;
772 for(i=0; i<p->nOp; i++){
773 p->aOp[i].cnt = 0;
774 p->aOp[i].cycles = 0;
775 }
drh9a324642003-09-06 20:12:01 +0000776 }
777#endif
778}
779
780
781/*
782** Remove any elements that remain on the sorter for the VDBE given.
783*/
danielk19774adee202004-05-08 08:23:19 +0000784void sqlite3VdbeSorterReset(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000785 while( p->pSort ){
786 Sorter *pSorter = p->pSort;
787 p->pSort = pSorter->pNext;
788 sqliteFree(pSorter->zKey);
danielk1977369f27e2004-06-15 11:40:04 +0000789 sqlite3VdbeMemRelease(&pSorter->data);
drh9a324642003-09-06 20:12:01 +0000790 sqliteFree(pSorter);
791 }
drh495c09a2005-04-01 10:47:40 +0000792 p->pSortTail = 0;
drh9a324642003-09-06 20:12:01 +0000793}
794
795/*
danielk1977e159fdf2004-06-21 10:45:06 +0000796** Free all resources allociated with AggElem pElem, an element of
797** aggregate pAgg.
798*/
danielk19778b60e0f2005-01-12 09:10:39 +0000799static void freeAggElem(AggElem *pElem, Agg *pAgg){
danielk1977e159fdf2004-06-21 10:45:06 +0000800 int i;
801 for(i=0; i<pAgg->nMem; i++){
802 Mem *pMem = &pElem->aMem[i];
drh645f63e2004-06-22 13:22:40 +0000803 if( pAgg->apFunc && pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
danielk1977e159fdf2004-06-21 10:45:06 +0000804 sqlite3_context ctx;
805 ctx.pFunc = pAgg->apFunc[i];
806 ctx.s.flags = MEM_Null;
807 ctx.pAgg = pMem->z;
808 ctx.cnt = pMem->i;
danielk1977e159fdf2004-06-21 10:45:06 +0000809 ctx.isError = 0;
drh81db88e2004-12-07 12:29:17 +0000810 (*ctx.pFunc->xFinalize)(&ctx);
danielk1977e159fdf2004-06-21 10:45:06 +0000811 pMem->z = ctx.pAgg;
812 if( pMem->z!=0 && pMem->z!=pMem->zShort ){
813 sqliteFree(pMem->z);
814 }
815 sqlite3VdbeMemRelease(&ctx.s);
816 }else{
817 sqlite3VdbeMemRelease(pMem);
818 }
819 }
820 sqliteFree(pElem);
821}
822
823/*
danielk1977ce2663c2004-06-11 13:19:21 +0000824** Reset an Agg structure. Delete all its contents.
825**
826** For installable aggregate functions, if the step function has been
827** called, make sure the finalizer function has also been called. The
828** finalizer might need to free memory that was allocated as part of its
829** private context. If the finalizer has not been called yet, call it
830** now.
831**
832** If db is NULL, then this is being called from sqliteVdbeReset(). In
833** this case clean up all references to the temp-table used for
834** aggregates (if it was ever opened).
835**
836** If db is not NULL, then this is being called from with an OP_AggReset
837** opcode. Open the temp-table, if it has not already been opened and
838** delete the contents of the table used for aggregate information, ready
839** for the next round of aggregate processing.
840*/
drh9bb575f2004-09-06 17:24:11 +0000841int sqlite3VdbeAggReset(sqlite3 *db, Agg *pAgg, KeyInfo *pKeyInfo){
danielk1977ce2663c2004-06-11 13:19:21 +0000842 int rc = 0;
danielk1977b3bce662005-01-29 08:32:43 +0000843 BtCursor *pCsr;
danielk1977ce2663c2004-06-11 13:19:21 +0000844
danielk1977b3bce662005-01-29 08:32:43 +0000845 if( !pAgg ) return SQLITE_OK;
846 pCsr = pAgg->pCsr;
danielk1977ce2663c2004-06-11 13:19:21 +0000847 assert( (pCsr && pAgg->nTab>0) || (!pCsr && pAgg->nTab==0)
848 || sqlite3_malloc_failed );
849
850 /* If pCsr is not NULL, then the table used for aggregate information
851 ** is open. Loop through it and free the AggElem* structure pointed at
852 ** by each entry. If the finalizer has not been called for an AggElem,
853 ** do that too. Finally, clear the btree table itself.
854 */
855 if( pCsr ){
856 int res;
857 assert( pAgg->pBtree );
858 assert( pAgg->nTab>0 );
859
860 rc=sqlite3BtreeFirst(pCsr, &res);
861 while( res==0 && rc==SQLITE_OK ){
862 AggElem *pElem;
863 rc = sqlite3BtreeData(pCsr, 0, sizeof(AggElem*), (char *)&pElem);
danielk19778b60e0f2005-01-12 09:10:39 +0000864 if( rc!=SQLITE_OK ){
danielk1977ce2663c2004-06-11 13:19:21 +0000865 return rc;
866 }
867 assert( pAgg->apFunc!=0 );
danielk1977e159fdf2004-06-21 10:45:06 +0000868 freeAggElem(pElem, pAgg);
danielk1977ce2663c2004-06-11 13:19:21 +0000869 rc=sqlite3BtreeNext(pCsr, &res);
870 }
871 if( rc!=SQLITE_OK ){
872 return rc;
873 }
874
875 sqlite3BtreeCloseCursor(pCsr);
876 sqlite3BtreeClearTable(pAgg->pBtree, pAgg->nTab);
danielk1977e159fdf2004-06-21 10:45:06 +0000877 }else{
878 /* The cursor may not be open because the aggregator was never used,
879 ** or it could be that it was used but there was no GROUP BY clause.
880 */
881 if( pAgg->pCurrent ){
882 freeAggElem(pAgg->pCurrent, pAgg);
883 }
danielk1977ce2663c2004-06-11 13:19:21 +0000884 }
885
886 /* If db is not NULL and we have not yet and we have not yet opened
887 ** the temporary btree then do so and create the table to store aggregate
888 ** information.
889 **
890 ** If db is NULL, then close the temporary btree if it is open.
891 */
892 if( db ){
893 if( !pAgg->pBtree ){
894 assert( pAgg->nTab==0 );
danielk197703aded42004-11-22 05:26:27 +0000895#ifndef SQLITE_OMIT_MEMORYDB
drhe1632b22004-06-12 20:42:29 +0000896 rc = sqlite3BtreeFactory(db, ":memory:", 0, TEMP_PAGES, &pAgg->pBtree);
danielk197703aded42004-11-22 05:26:27 +0000897#else
898 rc = sqlite3BtreeFactory(db, 0, 0, TEMP_PAGES, &pAgg->pBtree);
899#endif
danielk1977ce2663c2004-06-11 13:19:21 +0000900 if( rc!=SQLITE_OK ) return rc;
danielk197740b38dc2004-06-26 08:38:24 +0000901 sqlite3BtreeBeginTrans(pAgg->pBtree, 1);
danielk1977ce2663c2004-06-11 13:19:21 +0000902 rc = sqlite3BtreeCreateTable(pAgg->pBtree, &pAgg->nTab, 0);
903 if( rc!=SQLITE_OK ) return rc;
904 }
905 assert( pAgg->nTab!=0 );
906
907 rc = sqlite3BtreeCursor(pAgg->pBtree, pAgg->nTab, 1,
908 sqlite3VdbeRecordCompare, pKeyInfo, &pAgg->pCsr);
909 if( rc!=SQLITE_OK ) return rc;
910 }else{
911 if( pAgg->pBtree ){
912 sqlite3BtreeClose(pAgg->pBtree);
913 pAgg->pBtree = 0;
914 pAgg->nTab = 0;
915 }
916 pAgg->pCsr = 0;
917 }
918
919 if( pAgg->apFunc ){
920 sqliteFree(pAgg->apFunc);
921 pAgg->apFunc = 0;
922 }
923 pAgg->pCurrent = 0;
924 pAgg->nMem = 0;
925 pAgg->searching = 0;
926 return SQLITE_OK;
927}
928
drh9a324642003-09-06 20:12:01 +0000929/*
930** Close a cursor and release all the resources that cursor happens
931** to hold.
932*/
drh4774b132004-06-12 20:12:51 +0000933void sqlite3VdbeFreeCursor(Cursor *pCx){
934 if( pCx==0 ){
935 return;
936 }
drh9a324642003-09-06 20:12:01 +0000937 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000938 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000939 }
940 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000941 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000942 }
943 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000944 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000945 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000946}
947
948/*
949** Close all cursors
950*/
951static void closeAllCursors(Vdbe *p){
952 int i;
drh290c1942004-08-21 17:54:45 +0000953 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000954 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000955 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh290c1942004-08-21 17:54:45 +0000956 p->apCsr[i] = 0;
drh9a324642003-09-06 20:12:01 +0000957 }
drh9a324642003-09-06 20:12:01 +0000958}
959
960/*
drh9a324642003-09-06 20:12:01 +0000961** Clean up the VM after execution.
962**
963** This routine will automatically close any cursors, lists, and/or
964** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000965** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000966*/
967static void Cleanup(Vdbe *p){
968 int i;
drh6810ce62004-01-31 19:22:56 +0000969 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +0000970 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
971 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +0000972 }
drh9a324642003-09-06 20:12:01 +0000973 closeAllCursors(p);
drh76ff3a02004-09-24 22:32:30 +0000974 releaseMemArray(p->aMem, p->nMem);
drha01f79d2005-07-08 13:07:59 +0000975 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +0000976 if( p->contextStack ){
977 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +0000978 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +0000979 }
980 sqliteFree(p->contextStack);
drh344737f2004-09-19 00:50:20 +0000981 }
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3VdbeSorterReset(p);
danielk1977b3bce662005-01-29 08:32:43 +0000983 for(i=0; i<p->nAgg; i++){
984 sqlite3VdbeAggReset(0, &p->apAgg[i], 0);
985 }
drh5f968432004-02-21 19:02:30 +0000986 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +0000987 p->contextStackDepth = 0;
988 p->contextStackTop = 0;
drh9a324642003-09-06 20:12:01 +0000989 sqliteFree(p->zErrMsg);
990 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000991}
992
993/*
danielk197722322fd2004-05-25 23:35:17 +0000994** Set the number of result columns that will be returned by this SQL
995** statement. This is now set at compile time, rather than during
996** execution of the vdbe program so that sqlite3_column_count() can
997** be called on an SQL statement before sqlite3_step().
998*/
999void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001000 Mem *pColName;
1001 int n;
danielk19773cf86062004-05-26 10:11:05 +00001002 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +00001003 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +00001004 n = nResColumn*2;
1005 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
1006 if( p->aColName==0 ) return;
1007 while( n-- > 0 ){
1008 (pColName++)->flags = MEM_Null;
1009 }
danielk197722322fd2004-05-25 23:35:17 +00001010}
1011
1012/*
danielk19773cf86062004-05-26 10:11:05 +00001013** Set the name of the idx'th column to be returned by the SQL statement.
1014** zName must be a pointer to a nul terminated string.
1015**
1016** This call must be made after a call to sqlite3VdbeSetNumCols().
1017**
danielk1977d8123362004-06-12 09:25:12 +00001018** If N==P3_STATIC it means that zName is a pointer to a constant static
1019** string and we can just copy the pointer. If it is P3_DYNAMIC, then
1020** the string is freed using sqliteFree() when the vdbe is finished with
1021** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001022*/
1023int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
1024 int rc;
1025 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +00001026 assert( idx<(2*p->nResColumn) );
drh76ff3a02004-09-24 22:32:30 +00001027 if( sqlite3_malloc_failed ) return SQLITE_NOMEM;
1028 assert( p->aColName!=0 );
danielk19773cf86062004-05-26 10:11:05 +00001029 pColName = &(p->aColName[idx]);
danielk1977d8123362004-06-12 09:25:12 +00001030 if( N==P3_DYNAMIC || N==P3_STATIC ){
1031 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001032 }else{
danielk1977d8123362004-06-12 09:25:12 +00001033 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001034 }
1035 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
1036 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00001037 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +00001038 }
1039 return rc;
1040}
1041
danielk197713adf8a2004-06-03 16:08:41 +00001042/*
1043** A read or write transaction may or may not be active on database handle
1044** db. If a transaction is active, commit it. If there is a
1045** write-transaction spanning more than one database file, this routine
1046** takes care of the master journal trickery.
1047*/
drh9bb575f2004-09-06 17:24:11 +00001048static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001049 int i;
1050 int nTrans = 0; /* Number of databases with an active write-transaction */
1051 int rc = SQLITE_OK;
1052 int needXcommit = 0;
1053
1054 for(i=0; i<db->nDb; i++){
1055 Btree *pBt = db->aDb[i].pBt;
1056 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1057 needXcommit = 1;
1058 if( i!=1 ) nTrans++;
1059 }
1060 }
1061
1062 /* If there are any write-transactions at all, invoke the commit hook */
1063 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001064 int rc;
1065 sqlite3SafetyOff(db);
1066 rc = db->xCommitCallback(db->pCommitArg);
1067 sqlite3SafetyOn(db);
1068 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001069 return SQLITE_CONSTRAINT;
1070 }
1071 }
1072
danielk197740b38dc2004-06-26 08:38:24 +00001073 /* The simple case - no more than one database file (not counting the
1074 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001075 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001076 **
danielk197740b38dc2004-06-26 08:38:24 +00001077 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1078 ** string, it means the main database is :memory:. In that case we do
1079 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +00001080 ** too.
danielk197713adf8a2004-06-03 16:08:41 +00001081 */
danielk197740b38dc2004-06-26 08:38:24 +00001082 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001083 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001084 Btree *pBt = db->aDb[i].pBt;
1085 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +00001086 rc = sqlite3BtreeSync(pBt, 0);
1087 }
1088 }
1089
1090 /* Do the commit only if all databases successfully synced */
1091 if( rc==SQLITE_OK ){
1092 for(i=0; i<db->nDb; i++){
1093 Btree *pBt = db->aDb[i].pBt;
1094 if( pBt ){
1095 sqlite3BtreeCommit(pBt);
1096 }
danielk197713adf8a2004-06-03 16:08:41 +00001097 }
1098 }
1099 }
1100
1101 /* The complex case - There is a multi-file write-transaction active.
1102 ** This requires a master journal file to ensure the transaction is
1103 ** committed atomicly.
1104 */
danielk197744ee5bf2005-05-27 09:41:12 +00001105#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001106 else{
1107 char *zMaster = 0; /* File-name for the master journal */
1108 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1109 OsFile master;
1110
1111 /* Select a master journal file name */
1112 do {
drha6abd042004-06-09 17:37:22 +00001113 u32 random;
1114 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001115 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +00001116 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001117 if( !zMaster ){
1118 return SQLITE_NOMEM;
1119 }
1120 }while( sqlite3OsFileExists(zMaster) );
1121
1122 /* Open the master journal. */
drhda71ce12004-06-21 18:14:45 +00001123 memset(&master, 0, sizeof(master));
danielk197713adf8a2004-06-03 16:08:41 +00001124 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
1125 if( rc!=SQLITE_OK ){
1126 sqliteFree(zMaster);
1127 return rc;
1128 }
1129
1130 /* Write the name of each database file in the transaction into the new
1131 ** master journal file. If an error occurs at this point close
1132 ** and delete the master journal file. All the individual journal files
1133 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001134 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001135 */
1136 for(i=0; i<db->nDb; i++){
1137 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001138 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001139 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001140 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001141 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1142 rc = sqlite3OsWrite(&master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001143 if( rc!=SQLITE_OK ){
1144 sqlite3OsClose(&master);
1145 sqlite3OsDelete(zMaster);
1146 sqliteFree(zMaster);
1147 return rc;
1148 }
1149 }
1150 }
1151
danielk197713adf8a2004-06-03 16:08:41 +00001152
danielk19775865e3d2004-06-14 06:03:57 +00001153 /* Sync the master journal file. Before doing this, open the directory
1154 ** the master journal file is store in so that it gets synced too.
1155 */
1156 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1157 rc = sqlite3OsOpenDirectory(zMainFile, &master);
danielk19778b60e0f2005-01-12 09:10:39 +00001158 if( rc!=SQLITE_OK || (rc = sqlite3OsSync(&master))!=SQLITE_OK ){
danielk19775865e3d2004-06-14 06:03:57 +00001159 sqlite3OsClose(&master);
1160 sqlite3OsDelete(zMaster);
1161 sqliteFree(zMaster);
1162 return rc;
1163 }
drhc9e06862004-06-09 20:03:08 +00001164
danielk197713adf8a2004-06-03 16:08:41 +00001165 /* Sync all the db files involved in the transaction. The same call
1166 ** sets the master journal pointer in each individual journal. If
1167 ** an error occurs here, do not delete the master journal file.
1168 **
1169 ** If the error occurs during the first call to sqlite3BtreeSync(),
1170 ** then there is a chance that the master journal file will be
1171 ** orphaned. But we cannot delete it, in case the master journal
1172 ** file name was written into the journal file before the failure
1173 ** occured.
1174 */
1175 for(i=0; i<db->nDb; i++){
1176 Btree *pBt = db->aDb[i].pBt;
1177 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1178 rc = sqlite3BtreeSync(pBt, zMaster);
1179 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001180 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001181 sqliteFree(zMaster);
1182 return rc;
1183 }
1184 }
1185 }
danielk1977962398d2004-06-14 09:35:16 +00001186 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001187
danielk1977962398d2004-06-14 09:35:16 +00001188 /* Delete the master journal file. This commits the transaction. After
1189 ** doing this the directory is synced again before any individual
1190 ** transaction files are deleted.
1191 */
danielk197713adf8a2004-06-03 16:08:41 +00001192 rc = sqlite3OsDelete(zMaster);
1193 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001194 sqliteFree(zMaster);
1195 zMaster = 0;
danielk1977962398d2004-06-14 09:35:16 +00001196 rc = sqlite3OsSyncDirectory(zMainFile);
1197 if( rc!=SQLITE_OK ){
1198 /* This is not good. The master journal file has been deleted, but
1199 ** the directory sync failed. There is no completely safe course of
1200 ** action from here. The individual journals contain the name of the
1201 ** master journal file, but there is no way of knowing if that
1202 ** master journal exists now or if it will exist after the operating
1203 ** system crash that may follow the fsync() failure.
1204 */
danielk1977962398d2004-06-14 09:35:16 +00001205 return rc;
1206 }
danielk197713adf8a2004-06-03 16:08:41 +00001207
1208 /* All files and directories have already been synced, so the following
1209 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1210 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001211 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001212 ** but some stray 'cold' journals may be lying around. Returning an
1213 ** error code won't help matters.
1214 */
1215 for(i=0; i<db->nDb; i++){
1216 Btree *pBt = db->aDb[i].pBt;
1217 if( pBt ){
1218 sqlite3BtreeCommit(pBt);
1219 }
1220 }
1221 }
danielk197744ee5bf2005-05-27 09:41:12 +00001222#endif
danielk1977026d2702004-06-14 13:14:59 +00001223
drh2ac3ee92004-06-07 16:27:46 +00001224 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001225}
1226
drh91b48aa2004-06-30 11:14:18 +00001227/*
1228** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001229** aborted. This happens when one VM causes a rollback due to an
1230** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1231** aborted so that they do not have data rolled out from underneath
1232** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001233*/
1234static void abortOtherActiveVdbes(Vdbe *pVdbe){
1235 Vdbe *pOther;
1236 for(pOther=pVdbe->db->pVdbe; pOther; pOther=pOther->pNext){
1237 if( pOther==pVdbe ) continue;
1238 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1239 closeAllCursors(pOther);
1240 pOther->aborted = 1;
1241 }
1242}
1243
danielk19771d850a72004-05-31 08:26:49 +00001244/*
1245** This routine checks that the sqlite3.activeVdbeCnt count variable
1246** matches the number of vdbe's in the list sqlite3.pVdbe that are
1247** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001248** This is an internal self-check only - it is not an essential processing
1249** step.
danielk19771d850a72004-05-31 08:26:49 +00001250**
1251** This is a no-op if NDEBUG is defined.
1252*/
1253#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001254static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001255 Vdbe *p;
1256 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001257 p = db->pVdbe;
1258 while( p ){
drh92f02c32004-09-02 14:57:08 +00001259 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001260 cnt++;
1261 }
1262 p = p->pNext;
1263 }
danielk19771d850a72004-05-31 08:26:49 +00001264 assert( cnt==db->activeVdbeCnt );
1265}
1266#else
1267#define checkActiveVdbeCnt(x)
1268#endif
1269
danielk19773cf86062004-05-26 10:11:05 +00001270/*
drh92f02c32004-09-02 14:57:08 +00001271** This routine is called the when a VDBE tries to halt. If the VDBE
1272** has made changes and is in autocommit mode, then commit those
1273** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001274**
drh92f02c32004-09-02 14:57:08 +00001275** This routine is the only way to move the state of a VM from
1276** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1277**
1278** Return an error code. If the commit could not complete because of
1279** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1280** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001281*/
drh92f02c32004-09-02 14:57:08 +00001282int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001283 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001284 int i;
danielk19771d850a72004-05-31 08:26:49 +00001285 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
drh9a324642003-09-06 20:12:01 +00001286
drh92f02c32004-09-02 14:57:08 +00001287 if( p->magic!=VDBE_MAGIC_RUN ){
1288 /* Already halted. Nothing to do. */
1289 assert( p->magic==VDBE_MAGIC_HALT );
1290 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001291 }
drh92f02c32004-09-02 14:57:08 +00001292 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001293 checkActiveVdbeCnt(db);
drh178286b2005-01-23 13:14:55 +00001294 if( p->pc<0 ){
1295 /* No commit or rollback needed if the program never started */
1296 }else if( db->autoCommit && db->activeVdbeCnt==1 ){
danielk19771d850a72004-05-31 08:26:49 +00001297 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk197713adf8a2004-06-03 16:08:41 +00001298 /* The auto-commit flag is true, there are no other active queries
1299 ** using this handle and the vdbe program was successful or hit an
drh92f02c32004-09-02 14:57:08 +00001300 ** 'OR FAIL' constraint. This means a commit is required.
danielk197713adf8a2004-06-03 16:08:41 +00001301 */
drh92f02c32004-09-02 14:57:08 +00001302 int rc = vdbeCommit(db);
1303 if( rc==SQLITE_BUSY ){
1304 return SQLITE_BUSY;
1305 }else if( rc!=SQLITE_OK ){
1306 p->rc = rc;
1307 xFunc = sqlite3BtreeRollback;
danielk197713adf8a2004-06-03 16:08:41 +00001308 }
danielk19771d850a72004-05-31 08:26:49 +00001309 }else{
1310 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +00001311 }
danielk19771d850a72004-05-31 08:26:49 +00001312 }else{
1313 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1314 xFunc = sqlite3BtreeCommitStmt;
1315 }else if( p->errorAction==OE_Abort ){
1316 xFunc = sqlite3BtreeRollbackStmt;
1317 }else{
1318 xFunc = sqlite3BtreeRollback;
1319 db->autoCommit = 1;
drh91b48aa2004-06-30 11:14:18 +00001320 abortOtherActiveVdbes(p);
danielk19771d850a72004-05-31 08:26:49 +00001321 }
1322 }
1323
danielk197713adf8a2004-06-03 16:08:41 +00001324 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
1325 ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
1326 ** each backend. If an error occurs and the return code is still
1327 ** SQLITE_OK, set the return code to the new error value.
1328 */
1329 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +00001330 int rc;
danielk19771d850a72004-05-31 08:26:49 +00001331 Btree *pBt = db->aDb[i].pBt;
danielk197777d83ba2004-05-31 10:08:14 +00001332 if( pBt ){
1333 rc = xFunc(pBt);
1334 if( p->rc==SQLITE_OK ) p->rc = rc;
1335 }
danielk19771d850a72004-05-31 08:26:49 +00001336 }
1337
danielk1977b28af712004-06-21 06:50:26 +00001338 /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
drhf4d173a2005-01-23 19:04:42 +00001339 if( p->changeCntOn && p->pc>=0 ){
danielk1977b28af712004-06-21 06:50:26 +00001340 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1341 sqlite3VdbeSetChanges(db, p->nChange);
1342 }else{
1343 sqlite3VdbeSetChanges(db, 0);
1344 }
1345 p->nChange = 0;
1346 }
danielk19771d850a72004-05-31 08:26:49 +00001347
drh92f02c32004-09-02 14:57:08 +00001348 /* Rollback or commit any schema changes that occurred. */
danielk19771d850a72004-05-31 08:26:49 +00001349 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001350 sqlite3RollbackInternalChanges(db);
danielk1977026d2702004-06-14 13:14:59 +00001351 }else if( db->flags & SQLITE_InternChanges ){
danielk1977ec8450f2004-06-19 09:35:36 +00001352 sqlite3CommitInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001353 }
danielk19771d850a72004-05-31 08:26:49 +00001354
drh92f02c32004-09-02 14:57:08 +00001355 /* We have successfully halted and closed the VM. Record this fact. */
1356 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001357 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001358 }
drh92f02c32004-09-02 14:57:08 +00001359 p->magic = VDBE_MAGIC_HALT;
1360 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001361
drh92f02c32004-09-02 14:57:08 +00001362 return SQLITE_OK;
1363}
1364
1365/*
1366** Clean up a VDBE after execution but do not delete the VDBE just yet.
1367** Write any error messages into *pzErrMsg. Return the result code.
1368**
1369** After this routine is run, the VDBE should be ready to be executed
1370** again.
1371**
1372** To look at it another way, this routine resets the state of the
1373** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1374** VDBE_MAGIC_INIT.
1375*/
1376int sqlite3VdbeReset(Vdbe *p){
1377 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
drhc60d0442004-09-30 13:43:13 +00001378 sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh92f02c32004-09-02 14:57:08 +00001379 return SQLITE_MISUSE;
1380 }
1381
1382 /* If the VM did not run to completion or if it encountered an
1383 ** error, then it might not have been halted properly. So halt
1384 ** it now.
1385 */
1386 sqlite3VdbeHalt(p);
1387
drhfb7e7652005-01-24 00:28:42 +00001388 /* If the VDBE has be run even partially, then transfer the error code
1389 ** and error message from the VDBE into the main database structure. But
1390 ** if the VDBE has just been set to run but has not actually executed any
1391 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001392 */
drhfb7e7652005-01-24 00:28:42 +00001393 if( p->pc>=0 ){
1394 if( p->zErrMsg ){
1395 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg);
1396 sqliteFree(p->zErrMsg);
1397 p->zErrMsg = 0;
1398 }else if( p->rc ){
1399 sqlite3Error(p->db, p->rc, 0);
1400 }else{
1401 sqlite3Error(p->db, SQLITE_OK, 0);
1402 }
danielk1977a21c6b62005-01-24 10:25:59 +00001403 }else if( p->rc && p->expired ){
1404 /* The expired flag was set on the VDBE before the first call
1405 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1406 ** called), set the database error in this case as well.
1407 */
1408 sqlite3Error(p->db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001409 }
1410
1411 /* Reclaim all memory used by the VDBE
1412 */
1413 Cleanup(p);
1414
1415 /* Save profiling information from this VDBE run.
1416 */
danielk19771d850a72004-05-31 08:26:49 +00001417 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001418#ifdef VDBE_PROFILE
1419 {
1420 FILE *out = fopen("vdbe_profile.out", "a");
1421 if( out ){
1422 int i;
1423 fprintf(out, "---- ");
1424 for(i=0; i<p->nOp; i++){
1425 fprintf(out, "%02x", p->aOp[i].opcode);
1426 }
1427 fprintf(out, "\n");
1428 for(i=0; i<p->nOp; i++){
1429 fprintf(out, "%6d %10lld %8lld ",
1430 p->aOp[i].cnt,
1431 p->aOp[i].cycles,
1432 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1433 );
danielk19774adee202004-05-08 08:23:19 +00001434 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001435 }
1436 fclose(out);
1437 }
1438 }
1439#endif
1440 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001441 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001442 if( p->rc==SQLITE_SCHEMA ){
1443 sqlite3ResetInternalSchema(p->db, 0);
1444 }
drh9a324642003-09-06 20:12:01 +00001445 return p->rc;
1446}
drh92f02c32004-09-02 14:57:08 +00001447
drh9a324642003-09-06 20:12:01 +00001448/*
1449** Clean up and delete a VDBE after execution. Return an integer which is
1450** the result code. Write any error message text into *pzErrMsg.
1451*/
danielk19779e6db7d2004-06-21 08:18:51 +00001452int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001453 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001454
danielk1977b5548a82004-06-26 13:51:33 +00001455 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1456 rc = sqlite3VdbeReset(p);
1457 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001458 return SQLITE_MISUSE;
1459 }
danielk19774adee202004-05-08 08:23:19 +00001460 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001461 return rc;
1462}
1463
1464/*
drhf92c7ff2004-06-19 15:40:23 +00001465** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001466** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001467** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001468** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001469*/
1470void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1471 int i;
1472 for(i=0; i<pVdbeFunc->nAux; i++){
1473 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1474 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1475 if( pAux->xDelete ){
1476 pAux->xDelete(pAux->pAux);
1477 }
1478 pAux->pAux = 0;
1479 }
1480 }
1481}
1482
1483/*
drh9a324642003-09-06 20:12:01 +00001484** Delete an entire VDBE.
1485*/
danielk19774adee202004-05-08 08:23:19 +00001486void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001487 int i;
1488 if( p==0 ) return;
1489 Cleanup(p);
1490 if( p->pPrev ){
1491 p->pPrev->pNext = p->pNext;
1492 }else{
1493 assert( p->db->pVdbe==p );
1494 p->db->pVdbe = p->pNext;
1495 }
1496 if( p->pNext ){
1497 p->pNext->pPrev = p->pPrev;
1498 }
drh76ff3a02004-09-24 22:32:30 +00001499 if( p->aOp ){
1500 for(i=0; i<p->nOp; i++){
1501 Op *pOp = &p->aOp[i];
1502 if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
1503 sqliteFree(pOp->p3);
1504 }
1505 if( pOp->p3type==P3_VDBEFUNC ){
1506 VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
1507 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
1508 sqliteFree(pVdbeFunc);
1509 }
danielk1977aee18ef2005-03-09 12:26:50 +00001510 if( pOp->p3type==P3_MEM ){
1511 sqlite3ValueFree((sqlite3_value*)pOp->p3);
1512 }
drh9a324642003-09-06 20:12:01 +00001513 }
drh76ff3a02004-09-24 22:32:30 +00001514 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001515 }
drh76ff3a02004-09-24 22:32:30 +00001516 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001517 sqliteFree(p->aLabel);
1518 sqliteFree(p->aStack);
drh76ff3a02004-09-24 22:32:30 +00001519 releaseMemArray(p->aColName, p->nResColumn*2);
1520 sqliteFree(p->aColName);
drh9a324642003-09-06 20:12:01 +00001521 p->magic = VDBE_MAGIC_DEAD;
1522 sqliteFree(p);
1523}
drha11846b2004-01-07 18:52:56 +00001524
1525/*
drha11846b2004-01-07 18:52:56 +00001526** If a MoveTo operation is pending on the given cursor, then do that
1527** MoveTo now. Return an error code. If no MoveTo is pending, this
1528** routine does nothing and returns SQLITE_OK.
1529*/
danielk19774adee202004-05-08 08:23:19 +00001530int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001531 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001532 int res, rc;
danielk1977132872b2004-05-10 10:37:18 +00001533 extern int sqlite3_search_count;
drhf0863fe2005-06-12 21:35:51 +00001534 assert( p->isTable );
1535 if( p->isTable ){
drh536065a2005-01-26 21:55:31 +00001536 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
danielk19776490beb2004-05-11 06:17:21 +00001537 }else{
drh536065a2005-01-26 21:55:31 +00001538 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1539 sizeof(i64),&res);
danielk19776490beb2004-05-11 06:17:21 +00001540 }
drh536065a2005-01-26 21:55:31 +00001541 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001542 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001543 p->lastRowid = keyToInt(p->movetoTarget);
1544 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001545 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001546 rc = sqlite3BtreeNext(p->pCursor, &res);
1547 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001548 }
danielk1977132872b2004-05-10 10:37:18 +00001549 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001550 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001551 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001552 }
1553 return SQLITE_OK;
1554}
danielk19774adee202004-05-08 08:23:19 +00001555
drhab9f7f12004-05-08 10:56:11 +00001556/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001557** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001558**
danielk1977cfcdaef2004-05-12 07:33:33 +00001559** sqlite3VdbeSerialType()
1560** sqlite3VdbeSerialTypeLen()
1561** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001562** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001563** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001564**
1565** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001566** data and index records. Each serialized value consists of a
1567** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1568** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001569**
danielk1977cfcdaef2004-05-12 07:33:33 +00001570** In an SQLite index record, the serial type is stored directly before
1571** the blob of data that it corresponds to. In a table record, all serial
1572** types are stored at the start of the record, and the blobs of data at
1573** the end. Hence these functions allow the caller to handle the
1574** serial-type and data blob seperately.
1575**
1576** The following table describes the various storage classes for data:
1577**
1578** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001579** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001580** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001581** 1 1 signed integer
1582** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001583** 3 3 signed integer
1584** 4 4 signed integer
1585** 5 6 signed integer
1586** 6 8 signed integer
1587** 7 8 IEEE float
1588** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001589** N>=12 and even (N-12)/2 BLOB
1590** N>=13 and odd (N-13)/2 text
1591**
1592*/
1593
1594/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001595** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001596*/
drh25aa1b42004-05-28 01:39:01 +00001597u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001598 int flags = pMem->flags;
1599
1600 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001601 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001602 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001603 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001604 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001605# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
danielk1977cfcdaef2004-05-12 07:33:33 +00001606 i64 i = pMem->i;
drh5742b632005-01-26 17:47:02 +00001607 u64 u = i<0 ? -i : i;
1608 if( u<=127 ) return 1;
1609 if( u<=32767 ) return 2;
1610 if( u<=8388607 ) return 3;
1611 if( u<=2147483647 ) return 4;
1612 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001613 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001614 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001615 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001616 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001617 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001618 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001619 int n = pMem->n;
1620 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001621 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001622 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001623 if( flags&MEM_Blob ){
1624 return (pMem->n*2 + 12);
1625 }
1626 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001627}
1628
1629/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001630** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001631*/
drh25aa1b42004-05-28 01:39:01 +00001632int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001633 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001634 return (serial_type-12)/2;
1635 }else{
drh57196282004-10-06 15:41:16 +00001636 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001637 return aSize[serial_type];
1638 }
danielk1977192ac1d2004-05-10 07:17:30 +00001639}
1640
1641/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001642** Write the serialized data blob for the value stored in pMem into
1643** buf. It is assumed that the caller has allocated sufficient space.
1644** Return the number of bytes written.
1645*/
danielk1977b1bc9532004-05-22 03:05:33 +00001646int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001647 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001648 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001649
danielk1977cfcdaef2004-05-12 07:33:33 +00001650 /* NULL */
drha19b7752004-05-30 21:14:58 +00001651 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001652 return 0;
1653 }
1654
drh1483e142004-05-21 21:12:42 +00001655 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001656 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001657 u64 v;
1658 int i;
drha19b7752004-05-30 21:14:58 +00001659 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001660 v = *(u64*)&pMem->r;
1661 }else{
1662 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001663 }
drh1483e142004-05-21 21:12:42 +00001664 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1665 while( i-- ){
1666 buf[i] = (v&0xFF);
1667 v >>= 8;
1668 }
1669 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001670 }
1671
1672 /* String or blob */
1673 assert( serial_type>=12 );
1674 len = sqlite3VdbeSerialTypeLen(serial_type);
1675 memcpy(buf, pMem->z, len);
1676 return len;
1677}
1678
1679/*
1680** Deserialize the data blob pointed to by buf as serial type serial_type
1681** and store the result in pMem. Return the number of bytes read.
1682*/
danielk1977b1bc9532004-05-22 03:05:33 +00001683int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001684 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001685 u32 serial_type, /* Serial type to deserialize */
1686 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001687){
drh3c685822005-05-21 18:32:18 +00001688 switch( serial_type ){
1689 case 8: /* Reserved for future use */
1690 case 9: /* Reserved for future use */
1691 case 10: /* Reserved for future use */
1692 case 11: /* Reserved for future use */
1693 case 0: { /* NULL */
1694 pMem->flags = MEM_Null;
1695 break;
1696 }
1697 case 1: { /* 1-byte signed integer */
1698 pMem->i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001699 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001700 return 1;
drh1483e142004-05-21 21:12:42 +00001701 }
drh3c685822005-05-21 18:32:18 +00001702 case 2: { /* 2-byte signed integer */
1703 pMem->i = (((signed char)buf[0])<<8) | buf[1];
1704 pMem->flags = MEM_Int;
1705 return 2;
1706 }
1707 case 3: { /* 3-byte signed integer */
1708 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1709 pMem->flags = MEM_Int;
1710 return 3;
1711 }
1712 case 4: { /* 4-byte signed integer */
1713 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1714 pMem->flags = MEM_Int;
1715 return 4;
1716 }
1717 case 5: { /* 6-byte signed integer */
1718 u64 x = (((signed char)buf[0])<<8) | buf[1];
1719 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1720 x = (x<<32) | y;
1721 pMem->i = *(i64*)&x;
1722 pMem->flags = MEM_Int;
1723 return 6;
1724 }
1725 case 6: /* 6-byte signed integer */
1726 case 7: { /* IEEE floating point */
1727 u64 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1728 u32 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
1729 x = (x<<32) | y;
1730 if( serial_type==6 ){
1731 pMem->i = *(i64*)&x;
1732 pMem->flags = MEM_Int;
1733 }else{
1734 pMem->r = *(double*)&x;
1735 pMem->flags = MEM_Real;
1736 }
1737 return 8;
1738 }
1739 default: {
1740 int len = (serial_type-12)/2;
1741 pMem->z = (char *)buf;
1742 pMem->n = len;
1743 pMem->xDel = 0;
1744 if( serial_type&0x01 ){
1745 pMem->flags = MEM_Str | MEM_Ephem;
1746 }else{
1747 pMem->flags = MEM_Blob | MEM_Ephem;
1748 }
1749 return len;
drh696b32f2004-05-30 01:51:52 +00001750 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001751 }
drh3c685822005-05-21 18:32:18 +00001752 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001753}
1754
1755/*
drh7a224de2004-06-02 01:22:02 +00001756** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001757** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1758** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001759** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1760** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001761*/
drh7a224de2004-06-02 01:22:02 +00001762int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001763 void *userData,
1764 int nKey1, const void *pKey1,
1765 int nKey2, const void *pKey2
1766){
drhd3d39e92004-05-20 22:16:29 +00001767 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001768 u32 d1, d2; /* Offset into aKey[] of next data element */
1769 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1770 u32 szHdr1, szHdr2; /* Number of bytes in header */
1771 int i = 0;
1772 int nField;
1773 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001774 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1775 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001776
1777 Mem mem1;
1778 Mem mem2;
1779 mem1.enc = pKeyInfo->enc;
1780 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001781
1782 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1783 d1 = szHdr1;
1784 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1785 d2 = szHdr2;
1786 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001787 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001788 u32 serial_type1;
1789 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001790
1791 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001792 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001793 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001794 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001795 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001796
1797 /* Assert that there is enough space left in each key for the blob of
1798 ** data to go with the serial type just read. This assert may fail if
1799 ** the file is corrupted. Then read the value from each key into mem1
1800 ** and mem2 respectively.
1801 */
drh25aa1b42004-05-28 01:39:01 +00001802 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1803 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001804
drhd5788202004-05-28 08:21:05 +00001805 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001806 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1807 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001808 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001809 break;
1810 }
1811 i++;
1812 }
1813
1814 /* One of the keys ran out of fields, but all the fields up to that point
1815 ** were equal. If the incrKey flag is true, then the second key is
1816 ** treated as larger.
1817 */
1818 if( rc==0 ){
1819 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001820 rc = -1;
1821 }else if( d1<nKey1 ){
1822 rc = 1;
1823 }else if( d2<nKey2 ){
1824 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001825 }
1826 }
1827
drhd3194f52004-05-27 19:59:32 +00001828 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1829 rc = -rc;
1830 }
1831
1832 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001833}
drhd5788202004-05-28 08:21:05 +00001834
1835/*
drh7a224de2004-06-02 01:22:02 +00001836** The argument is an index entry composed using the OP_MakeRecord opcode.
1837** The last entry in this record should be an integer (specifically
1838** an integer rowid). This routine returns the number of bytes in
1839** that integer.
drhd5788202004-05-28 08:21:05 +00001840*/
1841int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1842 u32 szHdr; /* Size of the header */
1843 u32 typeRowid; /* Serial type of the rowid */
1844
1845 sqlite3GetVarint32(aKey, &szHdr);
1846 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1847 return sqlite3VdbeSerialTypeLen(typeRowid);
1848}
danielk1977eb015e02004-05-18 01:31:14 +00001849
1850
1851/*
drh7a224de2004-06-02 01:22:02 +00001852** pCur points at an index entry created using the OP_MakeRecord opcode.
1853** Read the rowid (the last field in the record) and store it in *rowid.
1854** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001855*/
1856int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001857 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001858 int rc;
drhd5788202004-05-28 08:21:05 +00001859 u32 szHdr; /* Size of the header */
1860 u32 typeRowid; /* Serial type of the rowid */
1861 u32 lenRowid; /* Size of the rowid */
1862 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001863
drhd5788202004-05-28 08:21:05 +00001864 sqlite3BtreeKeySize(pCur, &nCellKey);
1865 if( nCellKey<=0 ){
1866 return SQLITE_CORRUPT;
1867 }
1868 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1869 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001870 return rc;
1871 }
drhd5788202004-05-28 08:21:05 +00001872 sqlite3GetVarint32(m.z, &szHdr);
1873 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1874 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1875 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1876 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001877 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001878 return SQLITE_OK;
1879}
1880
drh7cf6e4d2004-05-19 14:56:55 +00001881/*
drhd3d39e92004-05-20 22:16:29 +00001882** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001883** the key string in pKey (of length nKey). Write into *pRes a number
1884** that is negative, zero, or positive if pC is less than, equal to,
1885** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001886**
drhd5788202004-05-28 08:21:05 +00001887** pKey is either created without a rowid or is truncated so that it
1888** omits the rowid at the end. The rowid at the end of the index entry
1889** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001890*/
danielk1977183f9f72004-05-13 05:20:26 +00001891int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001892 Cursor *pC, /* The cursor to compare against */
1893 int nKey, const u8 *pKey, /* The key to compare */
1894 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001895){
danielk1977e0d4b062004-06-28 01:11:46 +00001896 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001897 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001898 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001899 int lenRowid;
1900 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001901
1902 sqlite3BtreeKeySize(pCur, &nCellKey);
1903 if( nCellKey<=0 ){
1904 *res = 0;
1905 return SQLITE_OK;
1906 }
drhd5788202004-05-28 08:21:05 +00001907 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1908 if( rc ){
1909 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001910 }
drhd5788202004-05-28 08:21:05 +00001911 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
drh7a224de2004-06-02 01:22:02 +00001912 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001913 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001914 return SQLITE_OK;
1915}
danielk1977b28af712004-06-21 06:50:26 +00001916
1917/*
1918** This routine sets the value to be returned by subsequent calls to
1919** sqlite3_changes() on the database handle 'db'.
1920*/
1921void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1922 db->nChange = nChange;
1923 db->nTotalChange += nChange;
1924}
1925
1926/*
1927** Set a flag in the vdbe to update the change counter when it is finalised
1928** or reset.
1929*/
drh4794f732004-11-05 17:17:50 +00001930void sqlite3VdbeCountChanges(Vdbe *v){
1931 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00001932}
drhd89bd002005-01-22 03:03:54 +00001933
1934/*
1935** Mark every prepared statement associated with a database connection
1936** as expired.
1937**
1938** An expired statement means that recompilation of the statement is
1939** recommend. Statements expire when things happen that make their
1940** programs obsolete. Removing user-defined functions or collating
1941** sequences, or changing an authorization function are the types of
1942** things that make prepared statements obsolete.
1943*/
1944void sqlite3ExpirePreparedStatements(sqlite3 *db){
1945 Vdbe *p;
1946 for(p = db->pVdbe; p; p=p->pNext){
1947 p->expired = 1;
1948 }
1949}
danielk1977aee18ef2005-03-09 12:26:50 +00001950
1951/*
1952** Return the database associated with the Vdbe.
1953*/
1954sqlite3 *sqlite3VdbeDb(Vdbe *v){
1955 return v->db;
1956}