blob: cba9c096d6182968b01189bc0ed8ca50205e9483 [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){
drh53f733c2005-09-16 02:38:09 +000065 int runMode = p->magic==VDBE_MAGIC_RUN;
66 if( runMode || p->nOpAlloc<N ){
drhb38ad992005-09-16 00:27:01 +000067 VdbeOp *pNew;
drh53f733c2005-09-16 02:38:09 +000068 int nNew = N + 100*(!runMode);
69 int oldSize = p->nOpAlloc;
70 pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
drhb38ad992005-09-16 00:27:01 +000071 if( pNew ){
drh53f733c2005-09-16 02:38:09 +000072 p->nOpAlloc = nNew;
drhb38ad992005-09-16 00:27:01 +000073 p->aOp = pNew;
drh53f733c2005-09-16 02:38:09 +000074 if( nNew>oldSize ){
75 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
76 }
drh76ff3a02004-09-24 22:32:30 +000077 }
78 }
79}
80
81/*
drh9a324642003-09-06 20:12:01 +000082** Add a new instruction to the list of instructions current in the
83** VDBE. Return the address of the new instruction.
84**
85** Parameters:
86**
87** p Pointer to the VDBE
88**
89** op The opcode for this instruction
90**
91** p1, p2 First two of the three possible operands.
92**
danielk19774adee202004-05-08 08:23:19 +000093** Use the sqlite3VdbeResolveLabel() function to fix an address and
94** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +000095** operand.
96*/
danielk19774adee202004-05-08 08:23:19 +000097int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +000098 int i;
drh701a0ae2004-02-22 20:05:00 +000099 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000100
101 i = p->nOp;
102 p->nOp++;
103 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000104 resizeOpArray(p, i+1);
drh53f733c2005-09-16 02:38:09 +0000105 if( sqlite3_malloc_failed ){
drh76ff3a02004-09-24 22:32:30 +0000106 return 0;
drh9a324642003-09-06 20:12:01 +0000107 }
drh701a0ae2004-02-22 20:05:00 +0000108 pOp = &p->aOp[i];
109 pOp->opcode = op;
110 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000111 pOp->p2 = p2;
112 pOp->p3 = 0;
113 pOp->p3type = P3_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000114 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000115#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000116 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000117#endif
118 return i;
119}
120
121/*
drh701a0ae2004-02-22 20:05:00 +0000122** Add an opcode that includes the p3 value.
123*/
drheb2e1762004-05-27 01:53:56 +0000124int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000125 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
126 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000127 return addr;
128}
129
130/*
drh9a324642003-09-06 20:12:01 +0000131** Create a new symbolic label for an instruction that has yet to be
132** coded. The symbolic label is really just a negative number. The
133** label can be used as the P2 value of an operation. Later, when
134** the label is resolved to a specific address, the VDBE will scan
135** through its operation list and change all values of P2 which match
136** the label into the resolved address.
137**
138** The VDBE knows that a P2 value is a label because labels are
139** always negative and P2 values are suppose to be non-negative.
140** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000141**
142** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000143*/
danielk19774adee202004-05-08 08:23:19 +0000144int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000145 int i;
146 i = p->nLabel++;
147 assert( p->magic==VDBE_MAGIC_INIT );
148 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000149 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
drh53f733c2005-09-16 02:38:09 +0000150 sqlite3ReallocOrFree((void**)&p->aLabel,
151 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000152 }
drh76ff3a02004-09-24 22:32:30 +0000153 if( p->aLabel ){
154 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000155 }
drh9a324642003-09-06 20:12:01 +0000156 return -1-i;
157}
158
159/*
160** Resolve label "x" to be the address of the next instruction to
161** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000162** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000163*/
danielk19774adee202004-05-08 08:23:19 +0000164void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000165 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000166 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000167 assert( j>=0 && j<p->nLabel );
168 if( p->aLabel ){
169 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000170 }
171}
172
173/*
danielk1977bc04f852005-03-29 08:26:13 +0000174** Return non-zero if opcode 'op' is guarenteed not to push more values
175** onto the VDBE stack than it pops off.
176*/
danielk19777a5147c2005-03-29 13:07:00 +0000177static int opcodeNoPush(u8 op){
178 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000179 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
180 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000181 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000182 ** in a comment on the same line as the "case OP_XXX:" in
183 ** sqlite3VdbeExec() in vdbe.c.
184 **
185 ** If the bit is true, then the corresponding opcode is guarenteed not
186 ** to grow the stack when it is executed. Otherwise, it may grow the
187 ** stack by at most one entry.
188 **
danielk19777a5147c2005-03-29 13:07:00 +0000189 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000190 ** one bit for opcodes 16 to 31, and so on.
191 **
192 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
193 ** because the file is generated by an awk program. Awk manipulates
194 ** all numbers as floating-point and we don't want to risk a rounding
195 ** error if someone builds with an awk that uses (for example) 32-bit
196 ** IEEE floats.
197 */
drh9a7e6082005-03-31 22:26:19 +0000198 static const u32 masks[5] = {
danielk19777a5147c2005-03-29 13:07:00 +0000199 NOPUSH_MASK_0 + (NOPUSH_MASK_1<<16),
200 NOPUSH_MASK_2 + (NOPUSH_MASK_3<<16),
201 NOPUSH_MASK_4 + (NOPUSH_MASK_5<<16),
202 NOPUSH_MASK_6 + (NOPUSH_MASK_7<<16),
203 NOPUSH_MASK_8 + (NOPUSH_MASK_9<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000204 };
205 return (masks[op>>5] & (1<<(op&0x1F)));
206}
207
208#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000209int sqlite3VdbeOpcodeNoPush(u8 op){
210 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000211}
212#endif
213
214/*
drh76ff3a02004-09-24 22:32:30 +0000215** Loop through the program looking for P2 values that are negative.
216** Each such value is a label. Resolve the label by setting the P2
217** value to its correct non-zero value.
218**
219** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000220**
drh13449892005-09-07 21:22:45 +0000221** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
222** to an OP_Function or OP_AggStep opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000223** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000224**
225** The integer *pMaxStack is set to the maximum number of vdbe stack
226** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000227**
228** This routine also does the following optimization: It scans for
229** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
drhf0863fe2005-06-12 21:35:51 +0000230** IdxInsert instructions where P2!=0. If no such instruction is
drh38449902005-06-07 01:43:41 +0000231** found, then every Statement instruction is changed to a Noop. In
232** this way, we avoid creating the statement journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000233*/
danielk1977bc04f852005-03-29 08:26:13 +0000234static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000235 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000236 int nMaxArgs = 0;
237 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000238 Op *pOp;
239 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000240 int doesStatementRollback = 0;
241 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000242 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000243 u8 opcode = pOp->opcode;
244
drh13449892005-09-07 21:22:45 +0000245 if( opcode==OP_Function || opcode==OP_AggStep ){
danielk1977bc04f852005-03-29 08:26:13 +0000246 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh38449902005-06-07 01:43:41 +0000247 }else if( opcode==OP_Halt ){
248 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
249 doesStatementRollback = 1;
250 }
drhf0863fe2005-06-12 21:35:51 +0000251 }else if( opcode==OP_IdxInsert ){
drh38449902005-06-07 01:43:41 +0000252 if( pOp->p2 ){
253 doesStatementRollback = 1;
254 }
255 }else if( opcode==OP_Statement ){
256 hasStatementBegin = 1;
danielk1977bc04f852005-03-29 08:26:13 +0000257 }
258
danielk19777a5147c2005-03-29 13:07:00 +0000259 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000260 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000261 }
262
drh76ff3a02004-09-24 22:32:30 +0000263 if( pOp->p2>=0 ) continue;
264 assert( -1-pOp->p2<p->nLabel );
265 pOp->p2 = aLabel[-1-pOp->p2];
266 }
267 sqliteFree(p->aLabel);
268 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000269
270 *pMaxFuncArgs = nMaxArgs;
271 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000272
273 /* If we never rollback a statement transaction, then statement
274 ** transactions are not needed. So change every OP_Statement
275 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
276 ** which can be expensive on some platforms.
277 */
278 if( hasStatementBegin && !doesStatementRollback ){
279 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
280 if( pOp->opcode==OP_Statement ){
281 pOp->opcode = OP_Noop;
282 }
283 }
284 }
drh76ff3a02004-09-24 22:32:30 +0000285}
286
287/*
drh9a324642003-09-06 20:12:01 +0000288** Return the address of the next instruction to be inserted.
289*/
danielk19774adee202004-05-08 08:23:19 +0000290int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000291 assert( p->magic==VDBE_MAGIC_INIT );
292 return p->nOp;
293}
294
295/*
296** Add a whole list of operations to the operation stack. Return the
297** address of the first operation added.
298*/
danielk19774adee202004-05-08 08:23:19 +0000299int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000300 int addr;
301 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000302 resizeOpArray(p, p->nOp + nOp);
drh53f733c2005-09-16 02:38:09 +0000303 if( sqlite3_malloc_failed ){
drh76ff3a02004-09-24 22:32:30 +0000304 return 0;
drh9a324642003-09-06 20:12:01 +0000305 }
306 addr = p->nOp;
307 if( nOp>0 ){
308 int i;
drh905793e2004-02-21 13:31:09 +0000309 VdbeOpList const *pIn = aOp;
310 for(i=0; i<nOp; i++, pIn++){
311 int p2 = pIn->p2;
312 VdbeOp *pOut = &p->aOp[i+addr];
313 pOut->opcode = pIn->opcode;
314 pOut->p1 = pIn->p1;
315 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
316 pOut->p3 = pIn->p3;
317 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000318#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000319 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000321 }
322#endif
323 }
324 p->nOp += nOp;
325 }
326 return addr;
327}
328
329/*
330** Change the value of the P1 operand for a specific instruction.
331** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000332** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000333** few minor changes to the program.
334*/
danielk19774adee202004-05-08 08:23:19 +0000335void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000336 assert( p->magic==VDBE_MAGIC_INIT );
337 if( p && addr>=0 && p->nOp>addr && p->aOp ){
338 p->aOp[addr].p1 = val;
339 }
340}
341
342/*
343** Change the value of the P2 operand for a specific instruction.
344** This routine is useful for setting a jump destination.
345*/
danielk19774adee202004-05-08 08:23:19 +0000346void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000347 assert( val>=0 );
348 assert( p->magic==VDBE_MAGIC_INIT );
349 if( p && addr>=0 && p->nOp>addr && p->aOp ){
350 p->aOp[addr].p2 = val;
351 }
352}
353
drhd654be82005-09-20 17:42:23 +0000354/*
355** Change teh P2 operand of instruction addr so that it points to
356** the address of the next instruction to be coded.
357*/
358void sqlite3VdbeJumpHere(Vdbe *p, int addr){
359 sqlite3VdbeChangeP2(p, addr, p->nOp);
360}
drhb38ad992005-09-16 00:27:01 +0000361
362/*
363** Delete a P3 value if necessary.
364*/
365static void freeP3(int p3type, void *p3){
366 if( p3 ){
drhac1733d2005-09-17 17:58:22 +0000367 switch( p3type ){
368 case P3_DYNAMIC:
369 case P3_KEYINFO:
370 case P3_KEYINFO_HANDOFF: {
371 sqliteFree(p3);
372 break;
373 }
374 case P3_VDBEFUNC: {
375 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
376 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
377 sqliteFree(pVdbeFunc);
378 break;
379 }
380 case P3_MEM: {
381 sqlite3ValueFree((sqlite3_value*)p3);
382 break;
383 }
drhb38ad992005-09-16 00:27:01 +0000384 }
385 }
386}
387
388
drh9a324642003-09-06 20:12:01 +0000389/*
390** Change the value of the P3 operand for a specific instruction.
391** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000392** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000393** few minor changes to the program.
394**
395** If n>=0 then the P3 operand is dynamic, meaning that a copy of
396** the string is made into memory obtained from sqliteMalloc().
397** A value of n==0 means copy bytes of zP3 up to and including the
398** first null byte. If n>0 then copy n+1 bytes of zP3.
399**
danielk19771f55c052005-05-19 08:42:59 +0000400** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
401** A copy is made of the KeyInfo structure into memory obtained from
402** sqliteMalloc, to be freed when the Vdbe is finalized.
403** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
404** stored in memory that the caller has obtained from sqliteMalloc. The
405** caller should not free the allocation, it will be freed when the Vdbe is
406** finalized.
407**
408** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
409** to a string or structure that is guaranteed to exist for the lifetime of
410** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000411**
412** If addr<0 then change P3 on the most recently inserted instruction.
413*/
danielk19774adee202004-05-08 08:23:19 +0000414void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000415 Op *pOp;
416 assert( p->magic==VDBE_MAGIC_INIT );
danielk1977d5d56522005-03-16 12:15:20 +0000417 if( p==0 || p->aOp==0 ){
drhb38ad992005-09-16 00:27:01 +0000418 freeP3(n, (void*)*(char**)&zP3);
danielk1977d5d56522005-03-16 12:15:20 +0000419 return;
420 }
drh9a324642003-09-06 20:12:01 +0000421 if( addr<0 || addr>=p->nOp ){
422 addr = p->nOp - 1;
423 if( addr<0 ) return;
424 }
425 pOp = &p->aOp[addr];
drhb38ad992005-09-16 00:27:01 +0000426 freeP3(pOp->p3type, pOp->p3);
427 pOp->p3 = 0;
drh9a324642003-09-06 20:12:01 +0000428 if( zP3==0 ){
429 pOp->p3 = 0;
430 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000431 }else if( n==P3_KEYINFO ){
432 KeyInfo *pKeyInfo;
433 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000434
435 /* KeyInfo structures that include an KeyInfo.aSortOrder are always
436 ** sent in using P3_KEYINFO_HANDOFF. The KeyInfo.aSortOrder array
437 ** is not duplicated when P3_KEYINFO is used. */
438 /* assert( pKeyInfo->aSortOrder==0 ); */
drhd3d39e92004-05-20 22:16:29 +0000439 nField = ((KeyInfo*)zP3)->nField;
440 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]);
drheafe05b2004-06-13 00:54:01 +0000441 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000442 pOp->p3 = (char*)pKeyInfo;
443 if( pKeyInfo ){
444 memcpy(pKeyInfo, zP3, nByte);
445 pOp->p3type = P3_KEYINFO;
446 }else{
447 pOp->p3type = P3_NOTUSED;
448 }
drhffbc3082004-05-21 01:29:06 +0000449 }else if( n==P3_KEYINFO_HANDOFF ){
450 pOp->p3 = (char*)zP3;
451 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000452 }else if( n<0 ){
453 pOp->p3 = (char*)zP3;
454 pOp->p3type = n;
455 }else{
drhae29ffb2004-09-25 14:39:18 +0000456 if( n==0 ) n = strlen(zP3);
457 pOp->p3 = sqliteStrNDup(zP3, n);
drh9a324642003-09-06 20:12:01 +0000458 pOp->p3type = P3_DYNAMIC;
459 }
460}
461
drhad6d9462004-09-19 02:15:24 +0000462#ifndef NDEBUG
463/*
464** Replace the P3 field of the most recently coded instruction with
465** comment text.
466*/
467void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
468 va_list ap;
469 assert( p->nOp>0 );
drh76ff3a02004-09-24 22:32:30 +0000470 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 );
drhad6d9462004-09-19 02:15:24 +0000471 va_start(ap, zFormat);
472 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
473 va_end(ap);
474}
475#endif
476
drh9a324642003-09-06 20:12:01 +0000477/*
drh9a324642003-09-06 20:12:01 +0000478** Return the opcode for a given address.
479*/
danielk19774adee202004-05-08 08:23:19 +0000480VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000481 assert( p->magic==VDBE_MAGIC_INIT );
482 assert( addr>=0 && addr<p->nOp );
483 return &p->aOp[addr];
484}
485
drhb7f91642004-10-31 02:22:47 +0000486#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
487 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000488/*
drhd3d39e92004-05-20 22:16:29 +0000489** Compute a string that describes the P3 parameter for an opcode.
490** Use zTemp for any required temporary buffer space.
491*/
492static char *displayP3(Op *pOp, char *zTemp, int nTemp){
493 char *zP3;
494 assert( nTemp>=20 );
495 switch( pOp->p3type ){
drhd3d39e92004-05-20 22:16:29 +0000496 case P3_KEYINFO: {
497 int i, j;
498 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
499 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
500 i = strlen(zTemp);
501 for(j=0; j<pKeyInfo->nField; j++){
502 CollSeq *pColl = pKeyInfo->aColl[j];
503 if( pColl ){
504 int n = strlen(pColl->zName);
505 if( i+n>nTemp-6 ){
506 strcpy(&zTemp[i],",...");
507 break;
508 }
509 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000510 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000511 zTemp[i++] = '-';
512 }
513 strcpy(&zTemp[i], pColl->zName);
514 i += n;
515 }else if( i+4<nTemp-6 ){
516 strcpy(&zTemp[i],",nil");
517 i += 4;
518 }
519 }
520 zTemp[i++] = ')';
521 zTemp[i] = 0;
522 assert( i<nTemp );
523 zP3 = zTemp;
524 break;
525 }
526 case P3_COLLSEQ: {
527 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000528 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000529 zP3 = zTemp;
530 break;
531 }
drhf9b596e2004-05-26 16:54:42 +0000532 case P3_FUNCDEF: {
533 FuncDef *pDef = (FuncDef*)pOp->p3;
534 char zNum[30];
535 sprintf(zTemp, "%.*s", nTemp, pDef->zName);
536 sprintf(zNum,"(%d)", pDef->nArg);
537 if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
538 strcat(zTemp, zNum);
539 }
540 zP3 = zTemp;
541 break;
542 }
drhd3d39e92004-05-20 22:16:29 +0000543 default: {
544 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000545 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000546 zP3 = "";
547 }
548 }
549 }
550 return zP3;
551}
drhb7f91642004-10-31 02:22:47 +0000552#endif
drhd3d39e92004-05-20 22:16:29 +0000553
554
danielk19778b60e0f2005-01-12 09:10:39 +0000555#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000556/*
557** Print a single opcode. This routine is used for debugging only.
558*/
danielk19774adee202004-05-08 08:23:19 +0000559void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000560 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000561 char zPtr[50];
562 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000563 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000564 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000565 fprintf(pOut, zFormat1,
566 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000567 fflush(pOut);
568}
569#endif
570
571/*
drh76ff3a02004-09-24 22:32:30 +0000572** Release an array of N Mem elements
573*/
574static void releaseMemArray(Mem *p, int N){
575 if( p ){
576 while( N-->0 ){
577 sqlite3VdbeMemRelease(p++);
578 }
579 }
580}
581
drhb7f91642004-10-31 02:22:47 +0000582#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000583/*
drh9a324642003-09-06 20:12:01 +0000584** Give a listing of the program in the virtual machine.
585**
danielk19774adee202004-05-08 08:23:19 +0000586** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000587** running the code, it invokes the callback once for each instruction.
588** This feature is used to implement "EXPLAIN".
589*/
danielk19774adee202004-05-08 08:23:19 +0000590int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000591 Vdbe *p /* The VDBE */
592){
drh9bb575f2004-09-06 17:24:11 +0000593 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000594 int i;
drh826fb5a2004-02-14 23:59:57 +0000595 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000596
drh9a324642003-09-06 20:12:01 +0000597 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000598 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
599 assert( db->magic==SQLITE_MAGIC_BUSY );
600 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000601
602 /* Even though this opcode does not put dynamic strings onto the
603 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000604 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000605 */
606 if( p->pTos==&p->aStack[4] ){
drh76ff3a02004-09-24 22:32:30 +0000607 releaseMemArray(p->aStack, 5);
danielk197718f41892004-05-22 07:27:46 +0000608 }
danielk197718f41892004-05-22 07:27:46 +0000609 p->resOnStack = 0;
610
drhecc92422005-09-10 16:46:12 +0000611 do{
612 i = p->pc++;
613 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000614 if( i>=p->nOp ){
615 p->rc = SQLITE_OK;
616 rc = SQLITE_DONE;
617 }else if( db->flags & SQLITE_Interrupt ){
618 db->flags &= ~SQLITE_Interrupt;
drhc5cdca62005-01-11 16:54:14 +0000619 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000620 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000621 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000622 }else{
drhd3d39e92004-05-20 22:16:29 +0000623 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000624 Mem *pMem = p->aStack;
625 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000626 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000627 pMem->i = i; /* Program counter */
628 pMem++;
629
630 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
631 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
632 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000633 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000634 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000635 pMem++;
636
637 pMem->flags = MEM_Int;
638 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000639 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000640 pMem++;
641
642 pMem->flags = MEM_Int;
643 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000644 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000645 pMem++;
646
647 pMem->flags = MEM_Short|MEM_Str|MEM_Term; /* P3 */
648 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drh9c054832004-05-31 18:51:57 +0000649 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000650 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000651
drhecc92422005-09-10 16:46:12 +0000652 p->nResColumn = 5 - 2*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000653 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000654 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000655 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000656 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000657 }
drh826fb5a2004-02-14 23:59:57 +0000658 return rc;
drh9a324642003-09-06 20:12:01 +0000659}
drhb7f91642004-10-31 02:22:47 +0000660#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000661
662/*
drh3f7d4e42004-07-24 14:35:58 +0000663** Print the SQL that was used to generate a VDBE program.
664*/
665void sqlite3VdbePrintSql(Vdbe *p){
666#ifdef SQLITE_DEBUG
667 int nOp = p->nOp;
668 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000669 if( nOp<1 ) return;
670 pOp = &p->aOp[nOp-1];
drh3f7d4e42004-07-24 14:35:58 +0000671 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
672 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000673 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000674 printf("SQL: [%s]\n", z);
675 }
676#endif
677}
678
679/*
drh9a324642003-09-06 20:12:01 +0000680** Prepare a virtual machine for execution. This involves things such
681** as allocating stack space and initializing the program counter.
682** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000683** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000684**
685** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
686** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000687*/
danielk19774adee202004-05-08 08:23:19 +0000688void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000689 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000690 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000691 int nMem, /* Number of memory cells to allocate */
692 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000693 int isExplain /* True if the EXPLAIN keywords is present */
694){
695 int n;
696
697 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000698 assert( p->magic==VDBE_MAGIC_INIT );
699
drhc16a03b2004-09-15 13:38:10 +0000700 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000701 */
drhc16a03b2004-09-15 13:38:10 +0000702 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000703
danielk1977634f2982005-03-28 08:44:07 +0000704 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
705 * is because the call to resizeOpArray() below may shrink the
706 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
707 * state.
708 */
709 p->magic = VDBE_MAGIC_RUN;
710
drh9a324642003-09-06 20:12:01 +0000711 /* No instruction ever pushes more than a single element onto the
712 ** stack. And the stack never grows on successive executions of the
713 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000714 ** on the maximum stack depth required. (Added later:) The
715 ** resolveP2Values() call computes a tighter upper bound on the
716 ** stack size.
drh9a324642003-09-06 20:12:01 +0000717 **
718 ** Allocation all the stack space we will ever need.
719 */
drh82a48512003-09-06 22:45:20 +0000720 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000721 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000722 int nStack; /* Maximum number of stack entries required */
723 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000724 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000725 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000726 assert( nStack<p->nOp );
727 nStack = isExplain ? 10 : nStack;
drh82a48512003-09-06 22:45:20 +0000728 p->aStack = sqliteMalloc(
danielk1977bc04f852005-03-29 08:26:13 +0000729 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000730 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000731 + nVar*sizeof(Mem) /* aVar */
732 + nVar*sizeof(char*) /* azVar */
733 + nMem*sizeof(Mem) /* aMem */
734 + nCursor*sizeof(Cursor*) /* apCsr */
drh82a48512003-09-06 22:45:20 +0000735 );
drh290c1942004-08-21 17:54:45 +0000736 if( !sqlite3_malloc_failed ){
danielk1977bc04f852005-03-29 08:26:13 +0000737 p->aMem = &p->aStack[nStack];
drh290c1942004-08-21 17:54:45 +0000738 p->nMem = nMem;
drh86f43302004-10-05 17:37:36 +0000739 p->aVar = &p->aMem[nMem];
740 p->nVar = nVar;
741 p->okVar = 0;
742 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000743 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000744 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000745 p->nCursor = nCursor;
746 for(n=0; n<nVar; n++){
747 p->aVar[n].flags = MEM_Null;
748 }
danielk197754db47e2004-05-19 10:36:43 +0000749 }
drh82a48512003-09-06 22:45:20 +0000750 }
danielk1977b3bce662005-01-29 08:32:43 +0000751 for(n=0; n<p->nMem; n++){
752 p->aMem[n].flags = MEM_Null;
753 }
drh9a324642003-09-06 20:12:01 +0000754
drhfaa57ac2004-06-09 14:01:51 +0000755#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000756 if( (p->db->flags & SQLITE_VdbeListing)!=0
757 || sqlite3OsFileExists("vdbe_explain")
758 ){
drh80242052004-06-09 00:48:12 +0000759 int i;
760 printf("VDBE Program Listing:\n");
drh3f7d4e42004-07-24 14:35:58 +0000761 sqlite3VdbePrintSql(p);
drh80242052004-06-09 00:48:12 +0000762 for(i=0; i<p->nOp; i++){
763 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
764 }
765 }
danielk19774adee202004-05-08 08:23:19 +0000766 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000767 p->trace = stdout;
768 }
769#endif
drh6810ce62004-01-31 19:22:56 +0000770 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000771 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000772 p->rc = SQLITE_OK;
773 p->uniqueCnt = 0;
774 p->returnDepth = 0;
775 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000776 p->popStack = 0;
777 p->explain |= isExplain;
778 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000779 p->nChange = 0;
drh9a324642003-09-06 20:12:01 +0000780#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000781 {
782 int i;
783 for(i=0; i<p->nOp; i++){
784 p->aOp[i].cnt = 0;
785 p->aOp[i].cycles = 0;
786 }
drh9a324642003-09-06 20:12:01 +0000787 }
788#endif
789}
790
drh9a324642003-09-06 20:12:01 +0000791/*
drh9a324642003-09-06 20:12:01 +0000792** Close a cursor and release all the resources that cursor happens
793** to hold.
794*/
drh4774b132004-06-12 20:12:51 +0000795void sqlite3VdbeFreeCursor(Cursor *pCx){
796 if( pCx==0 ){
797 return;
798 }
drh9a324642003-09-06 20:12:01 +0000799 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000800 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000801 }
802 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000803 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000804 }
805 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000806 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000807 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000808}
809
810/*
811** Close all cursors
812*/
813static void closeAllCursors(Vdbe *p){
814 int i;
drh290c1942004-08-21 17:54:45 +0000815 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000816 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000817 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh290c1942004-08-21 17:54:45 +0000818 p->apCsr[i] = 0;
drh9a324642003-09-06 20:12:01 +0000819 }
drh9a324642003-09-06 20:12:01 +0000820}
821
822/*
drh9a324642003-09-06 20:12:01 +0000823** Clean up the VM after execution.
824**
825** This routine will automatically close any cursors, lists, and/or
826** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000827** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000828*/
829static void Cleanup(Vdbe *p){
830 int i;
drh6810ce62004-01-31 19:22:56 +0000831 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +0000832 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
833 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +0000834 }
drh9a324642003-09-06 20:12:01 +0000835 closeAllCursors(p);
drh76ff3a02004-09-24 22:32:30 +0000836 releaseMemArray(p->aMem, p->nMem);
drha01f79d2005-07-08 13:07:59 +0000837 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +0000838 if( p->contextStack ){
839 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +0000840 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +0000841 }
842 sqliteFree(p->contextStack);
drh344737f2004-09-19 00:50:20 +0000843 }
drh5f968432004-02-21 19:02:30 +0000844 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +0000845 p->contextStackDepth = 0;
846 p->contextStackTop = 0;
drh9a324642003-09-06 20:12:01 +0000847 sqliteFree(p->zErrMsg);
848 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000849}
850
851/*
danielk197722322fd2004-05-25 23:35:17 +0000852** Set the number of result columns that will be returned by this SQL
853** statement. This is now set at compile time, rather than during
854** execution of the vdbe program so that sqlite3_column_count() can
855** be called on an SQL statement before sqlite3_step().
856*/
857void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +0000858 Mem *pColName;
859 int n;
danielk19773cf86062004-05-26 10:11:05 +0000860 assert( 0==p->nResColumn );
danielk197722322fd2004-05-25 23:35:17 +0000861 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +0000862 n = nResColumn*2;
863 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
864 if( p->aColName==0 ) return;
865 while( n-- > 0 ){
866 (pColName++)->flags = MEM_Null;
867 }
danielk197722322fd2004-05-25 23:35:17 +0000868}
869
870/*
danielk19773cf86062004-05-26 10:11:05 +0000871** Set the name of the idx'th column to be returned by the SQL statement.
872** zName must be a pointer to a nul terminated string.
873**
874** This call must be made after a call to sqlite3VdbeSetNumCols().
875**
danielk1977d8123362004-06-12 09:25:12 +0000876** If N==P3_STATIC it means that zName is a pointer to a constant static
877** string and we can just copy the pointer. If it is P3_DYNAMIC, then
878** the string is freed using sqliteFree() when the vdbe is finished with
879** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000880*/
881int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
882 int rc;
883 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +0000884 assert( idx<(2*p->nResColumn) );
drh76ff3a02004-09-24 22:32:30 +0000885 if( sqlite3_malloc_failed ) return SQLITE_NOMEM;
886 assert( p->aColName!=0 );
danielk19773cf86062004-05-26 10:11:05 +0000887 pColName = &(p->aColName[idx]);
danielk1977d8123362004-06-12 09:25:12 +0000888 if( N==P3_DYNAMIC || N==P3_STATIC ){
889 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000890 }else{
danielk1977d8123362004-06-12 09:25:12 +0000891 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000892 }
893 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
894 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000895 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000896 }
897 return rc;
898}
899
danielk197713adf8a2004-06-03 16:08:41 +0000900/*
901** A read or write transaction may or may not be active on database handle
902** db. If a transaction is active, commit it. If there is a
903** write-transaction spanning more than one database file, this routine
904** takes care of the master journal trickery.
905*/
drh9bb575f2004-09-06 17:24:11 +0000906static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +0000907 int i;
908 int nTrans = 0; /* Number of databases with an active write-transaction */
909 int rc = SQLITE_OK;
910 int needXcommit = 0;
911
912 for(i=0; i<db->nDb; i++){
913 Btree *pBt = db->aDb[i].pBt;
914 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
915 needXcommit = 1;
916 if( i!=1 ) nTrans++;
917 }
918 }
919
920 /* If there are any write-transactions at all, invoke the commit hook */
921 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +0000922 int rc;
923 sqlite3SafetyOff(db);
924 rc = db->xCommitCallback(db->pCommitArg);
925 sqlite3SafetyOn(db);
926 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +0000927 return SQLITE_CONSTRAINT;
928 }
929 }
930
danielk197740b38dc2004-06-26 08:38:24 +0000931 /* The simple case - no more than one database file (not counting the
932 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000933 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000934 **
danielk197740b38dc2004-06-26 08:38:24 +0000935 ** If the return value of sqlite3BtreeGetFilename() is a zero length
936 ** string, it means the main database is :memory:. In that case we do
937 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000938 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000939 */
danielk197740b38dc2004-06-26 08:38:24 +0000940 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +0000941 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +0000942 Btree *pBt = db->aDb[i].pBt;
943 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +0000944 rc = sqlite3BtreeSync(pBt, 0);
945 }
946 }
947
948 /* Do the commit only if all databases successfully synced */
949 if( rc==SQLITE_OK ){
950 for(i=0; i<db->nDb; i++){
951 Btree *pBt = db->aDb[i].pBt;
952 if( pBt ){
953 sqlite3BtreeCommit(pBt);
954 }
danielk197713adf8a2004-06-03 16:08:41 +0000955 }
956 }
957 }
958
959 /* The complex case - There is a multi-file write-transaction active.
960 ** This requires a master journal file to ensure the transaction is
961 ** committed atomicly.
962 */
danielk197744ee5bf2005-05-27 09:41:12 +0000963#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +0000964 else{
drh2c8997b2005-08-27 16:36:48 +0000965 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +0000966 char *zMaster = 0; /* File-name for the master journal */
967 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
968 OsFile master;
969
970 /* Select a master journal file name */
971 do {
drha6abd042004-06-09 17:37:22 +0000972 u32 random;
973 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +0000974 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +0000975 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +0000976 if( !zMaster ){
977 return SQLITE_NOMEM;
978 }
979 }while( sqlite3OsFileExists(zMaster) );
980
981 /* Open the master journal. */
drhda71ce12004-06-21 18:14:45 +0000982 memset(&master, 0, sizeof(master));
danielk197713adf8a2004-06-03 16:08:41 +0000983 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
984 if( rc!=SQLITE_OK ){
985 sqliteFree(zMaster);
986 return rc;
987 }
988
989 /* Write the name of each database file in the transaction into the new
990 ** master journal file. If an error occurs at this point close
991 ** and delete the master journal file. All the individual journal files
992 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +0000993 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +0000994 */
995 for(i=0; i<db->nDb; i++){
996 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +0000997 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +0000998 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +0000999 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001000 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001001 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1002 needSync = 1;
1003 }
drhc9e06862004-06-09 20:03:08 +00001004 rc = sqlite3OsWrite(&master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001005 if( rc!=SQLITE_OK ){
1006 sqlite3OsClose(&master);
1007 sqlite3OsDelete(zMaster);
1008 sqliteFree(zMaster);
1009 return rc;
1010 }
1011 }
1012 }
1013
danielk197713adf8a2004-06-03 16:08:41 +00001014
danielk19775865e3d2004-06-14 06:03:57 +00001015 /* Sync the master journal file. Before doing this, open the directory
1016 ** the master journal file is store in so that it gets synced too.
1017 */
1018 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1019 rc = sqlite3OsOpenDirectory(zMainFile, &master);
drheb796a72005-09-08 12:38:41 +00001020 if( rc!=SQLITE_OK ||
1021 (needSync && (rc=sqlite3OsSync(&master,0))!=SQLITE_OK) ){
danielk19775865e3d2004-06-14 06:03:57 +00001022 sqlite3OsClose(&master);
1023 sqlite3OsDelete(zMaster);
1024 sqliteFree(zMaster);
1025 return rc;
1026 }
drhc9e06862004-06-09 20:03:08 +00001027
danielk197713adf8a2004-06-03 16:08:41 +00001028 /* Sync all the db files involved in the transaction. The same call
1029 ** sets the master journal pointer in each individual journal. If
1030 ** an error occurs here, do not delete the master journal file.
1031 **
1032 ** If the error occurs during the first call to sqlite3BtreeSync(),
1033 ** then there is a chance that the master journal file will be
1034 ** orphaned. But we cannot delete it, in case the master journal
1035 ** file name was written into the journal file before the failure
1036 ** occured.
1037 */
1038 for(i=0; i<db->nDb; i++){
1039 Btree *pBt = db->aDb[i].pBt;
1040 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1041 rc = sqlite3BtreeSync(pBt, zMaster);
1042 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001043 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001044 sqliteFree(zMaster);
1045 return rc;
1046 }
1047 }
1048 }
danielk1977962398d2004-06-14 09:35:16 +00001049 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001050
danielk1977962398d2004-06-14 09:35:16 +00001051 /* Delete the master journal file. This commits the transaction. After
1052 ** doing this the directory is synced again before any individual
1053 ** transaction files are deleted.
1054 */
danielk197713adf8a2004-06-03 16:08:41 +00001055 rc = sqlite3OsDelete(zMaster);
1056 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001057 sqliteFree(zMaster);
1058 zMaster = 0;
danielk1977962398d2004-06-14 09:35:16 +00001059 rc = sqlite3OsSyncDirectory(zMainFile);
1060 if( rc!=SQLITE_OK ){
1061 /* This is not good. The master journal file has been deleted, but
1062 ** the directory sync failed. There is no completely safe course of
1063 ** action from here. The individual journals contain the name of the
1064 ** master journal file, but there is no way of knowing if that
1065 ** master journal exists now or if it will exist after the operating
1066 ** system crash that may follow the fsync() failure.
1067 */
danielk1977962398d2004-06-14 09:35:16 +00001068 return rc;
1069 }
danielk197713adf8a2004-06-03 16:08:41 +00001070
1071 /* All files and directories have already been synced, so the following
1072 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1073 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001074 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001075 ** but some stray 'cold' journals may be lying around. Returning an
1076 ** error code won't help matters.
1077 */
1078 for(i=0; i<db->nDb; i++){
1079 Btree *pBt = db->aDb[i].pBt;
1080 if( pBt ){
1081 sqlite3BtreeCommit(pBt);
1082 }
1083 }
1084 }
danielk197744ee5bf2005-05-27 09:41:12 +00001085#endif
danielk1977026d2702004-06-14 13:14:59 +00001086
drh2ac3ee92004-06-07 16:27:46 +00001087 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001088}
1089
drh91b48aa2004-06-30 11:14:18 +00001090/*
1091** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001092** aborted. This happens when one VM causes a rollback due to an
1093** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1094** aborted so that they do not have data rolled out from underneath
1095** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001096*/
1097static void abortOtherActiveVdbes(Vdbe *pVdbe){
1098 Vdbe *pOther;
1099 for(pOther=pVdbe->db->pVdbe; pOther; pOther=pOther->pNext){
1100 if( pOther==pVdbe ) continue;
1101 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1102 closeAllCursors(pOther);
1103 pOther->aborted = 1;
1104 }
1105}
1106
danielk19771d850a72004-05-31 08:26:49 +00001107/*
1108** This routine checks that the sqlite3.activeVdbeCnt count variable
1109** matches the number of vdbe's in the list sqlite3.pVdbe that are
1110** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001111** This is an internal self-check only - it is not an essential processing
1112** step.
danielk19771d850a72004-05-31 08:26:49 +00001113**
1114** This is a no-op if NDEBUG is defined.
1115*/
1116#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001117static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001118 Vdbe *p;
1119 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001120 p = db->pVdbe;
1121 while( p ){
drh92f02c32004-09-02 14:57:08 +00001122 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001123 cnt++;
1124 }
1125 p = p->pNext;
1126 }
danielk19771d850a72004-05-31 08:26:49 +00001127 assert( cnt==db->activeVdbeCnt );
1128}
1129#else
1130#define checkActiveVdbeCnt(x)
1131#endif
1132
danielk19773cf86062004-05-26 10:11:05 +00001133/*
drh92f02c32004-09-02 14:57:08 +00001134** This routine is called the when a VDBE tries to halt. If the VDBE
1135** has made changes and is in autocommit mode, then commit those
1136** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001137**
drh92f02c32004-09-02 14:57:08 +00001138** This routine is the only way to move the state of a VM from
1139** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1140**
1141** Return an error code. If the commit could not complete because of
1142** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1143** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001144*/
drh92f02c32004-09-02 14:57:08 +00001145int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001146 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001147 int i;
danielk19771d850a72004-05-31 08:26:49 +00001148 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
drh9a324642003-09-06 20:12:01 +00001149
drh92f02c32004-09-02 14:57:08 +00001150 if( p->magic!=VDBE_MAGIC_RUN ){
1151 /* Already halted. Nothing to do. */
1152 assert( p->magic==VDBE_MAGIC_HALT );
1153 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001154 }
drh92f02c32004-09-02 14:57:08 +00001155 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001156 checkActiveVdbeCnt(db);
drh178286b2005-01-23 13:14:55 +00001157 if( p->pc<0 ){
1158 /* No commit or rollback needed if the program never started */
1159 }else if( db->autoCommit && db->activeVdbeCnt==1 ){
danielk19771d850a72004-05-31 08:26:49 +00001160 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk197713adf8a2004-06-03 16:08:41 +00001161 /* The auto-commit flag is true, there are no other active queries
1162 ** using this handle and the vdbe program was successful or hit an
drh92f02c32004-09-02 14:57:08 +00001163 ** 'OR FAIL' constraint. This means a commit is required.
danielk197713adf8a2004-06-03 16:08:41 +00001164 */
drh92f02c32004-09-02 14:57:08 +00001165 int rc = vdbeCommit(db);
1166 if( rc==SQLITE_BUSY ){
1167 return SQLITE_BUSY;
1168 }else if( rc!=SQLITE_OK ){
1169 p->rc = rc;
1170 xFunc = sqlite3BtreeRollback;
danielk197713adf8a2004-06-03 16:08:41 +00001171 }
danielk19771d850a72004-05-31 08:26:49 +00001172 }else{
1173 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +00001174 }
danielk19771d850a72004-05-31 08:26:49 +00001175 }else{
1176 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1177 xFunc = sqlite3BtreeCommitStmt;
1178 }else if( p->errorAction==OE_Abort ){
1179 xFunc = sqlite3BtreeRollbackStmt;
1180 }else{
1181 xFunc = sqlite3BtreeRollback;
1182 db->autoCommit = 1;
drh91b48aa2004-06-30 11:14:18 +00001183 abortOtherActiveVdbes(p);
danielk19771d850a72004-05-31 08:26:49 +00001184 }
1185 }
1186
danielk197713adf8a2004-06-03 16:08:41 +00001187 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
1188 ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
1189 ** each backend. If an error occurs and the return code is still
1190 ** SQLITE_OK, set the return code to the new error value.
1191 */
1192 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +00001193 int rc;
danielk19771d850a72004-05-31 08:26:49 +00001194 Btree *pBt = db->aDb[i].pBt;
danielk197777d83ba2004-05-31 10:08:14 +00001195 if( pBt ){
1196 rc = xFunc(pBt);
1197 if( p->rc==SQLITE_OK ) p->rc = rc;
1198 }
danielk19771d850a72004-05-31 08:26:49 +00001199 }
1200
danielk1977b28af712004-06-21 06:50:26 +00001201 /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
drhf4d173a2005-01-23 19:04:42 +00001202 if( p->changeCntOn && p->pc>=0 ){
danielk1977b28af712004-06-21 06:50:26 +00001203 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1204 sqlite3VdbeSetChanges(db, p->nChange);
1205 }else{
1206 sqlite3VdbeSetChanges(db, 0);
1207 }
1208 p->nChange = 0;
1209 }
danielk19771d850a72004-05-31 08:26:49 +00001210
drh92f02c32004-09-02 14:57:08 +00001211 /* Rollback or commit any schema changes that occurred. */
danielk19771d850a72004-05-31 08:26:49 +00001212 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3RollbackInternalChanges(db);
danielk1977026d2702004-06-14 13:14:59 +00001214 }else if( db->flags & SQLITE_InternChanges ){
danielk1977ec8450f2004-06-19 09:35:36 +00001215 sqlite3CommitInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001216 }
danielk19771d850a72004-05-31 08:26:49 +00001217
drh92f02c32004-09-02 14:57:08 +00001218 /* We have successfully halted and closed the VM. Record this fact. */
1219 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001220 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001221 }
drh92f02c32004-09-02 14:57:08 +00001222 p->magic = VDBE_MAGIC_HALT;
1223 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001224
drh92f02c32004-09-02 14:57:08 +00001225 return SQLITE_OK;
1226}
1227
1228/*
1229** Clean up a VDBE after execution but do not delete the VDBE just yet.
1230** Write any error messages into *pzErrMsg. Return the result code.
1231**
1232** After this routine is run, the VDBE should be ready to be executed
1233** again.
1234**
1235** To look at it another way, this routine resets the state of the
1236** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1237** VDBE_MAGIC_INIT.
1238*/
1239int sqlite3VdbeReset(Vdbe *p){
1240 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
drhc60d0442004-09-30 13:43:13 +00001241 sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh92f02c32004-09-02 14:57:08 +00001242 return SQLITE_MISUSE;
1243 }
1244
1245 /* If the VM did not run to completion or if it encountered an
1246 ** error, then it might not have been halted properly. So halt
1247 ** it now.
1248 */
1249 sqlite3VdbeHalt(p);
1250
drhfb7e7652005-01-24 00:28:42 +00001251 /* If the VDBE has be run even partially, then transfer the error code
1252 ** and error message from the VDBE into the main database structure. But
1253 ** if the VDBE has just been set to run but has not actually executed any
1254 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001255 */
drhfb7e7652005-01-24 00:28:42 +00001256 if( p->pc>=0 ){
1257 if( p->zErrMsg ){
1258 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg);
1259 sqliteFree(p->zErrMsg);
1260 p->zErrMsg = 0;
1261 }else if( p->rc ){
1262 sqlite3Error(p->db, p->rc, 0);
1263 }else{
1264 sqlite3Error(p->db, SQLITE_OK, 0);
1265 }
danielk1977a21c6b62005-01-24 10:25:59 +00001266 }else if( p->rc && p->expired ){
1267 /* The expired flag was set on the VDBE before the first call
1268 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1269 ** called), set the database error in this case as well.
1270 */
1271 sqlite3Error(p->db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001272 }
1273
1274 /* Reclaim all memory used by the VDBE
1275 */
1276 Cleanup(p);
1277
1278 /* Save profiling information from this VDBE run.
1279 */
danielk19771d850a72004-05-31 08:26:49 +00001280 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001281#ifdef VDBE_PROFILE
1282 {
1283 FILE *out = fopen("vdbe_profile.out", "a");
1284 if( out ){
1285 int i;
1286 fprintf(out, "---- ");
1287 for(i=0; i<p->nOp; i++){
1288 fprintf(out, "%02x", p->aOp[i].opcode);
1289 }
1290 fprintf(out, "\n");
1291 for(i=0; i<p->nOp; i++){
1292 fprintf(out, "%6d %10lld %8lld ",
1293 p->aOp[i].cnt,
1294 p->aOp[i].cycles,
1295 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1296 );
danielk19774adee202004-05-08 08:23:19 +00001297 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001298 }
1299 fclose(out);
1300 }
1301 }
1302#endif
1303 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001304 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001305 if( p->rc==SQLITE_SCHEMA ){
1306 sqlite3ResetInternalSchema(p->db, 0);
1307 }
drh9a324642003-09-06 20:12:01 +00001308 return p->rc;
1309}
drh92f02c32004-09-02 14:57:08 +00001310
drh9a324642003-09-06 20:12:01 +00001311/*
1312** Clean up and delete a VDBE after execution. Return an integer which is
1313** the result code. Write any error message text into *pzErrMsg.
1314*/
danielk19779e6db7d2004-06-21 08:18:51 +00001315int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001316 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001317
danielk1977b5548a82004-06-26 13:51:33 +00001318 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1319 rc = sqlite3VdbeReset(p);
1320 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001321 return SQLITE_MISUSE;
1322 }
danielk19774adee202004-05-08 08:23:19 +00001323 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001324 return rc;
1325}
1326
1327/*
drhf92c7ff2004-06-19 15:40:23 +00001328** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001329** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001330** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001331** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001332*/
1333void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1334 int i;
1335 for(i=0; i<pVdbeFunc->nAux; i++){
1336 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1337 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1338 if( pAux->xDelete ){
1339 pAux->xDelete(pAux->pAux);
1340 }
1341 pAux->pAux = 0;
1342 }
1343 }
1344}
1345
1346/*
drh9a324642003-09-06 20:12:01 +00001347** Delete an entire VDBE.
1348*/
danielk19774adee202004-05-08 08:23:19 +00001349void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001350 int i;
1351 if( p==0 ) return;
1352 Cleanup(p);
1353 if( p->pPrev ){
1354 p->pPrev->pNext = p->pNext;
1355 }else{
1356 assert( p->db->pVdbe==p );
1357 p->db->pVdbe = p->pNext;
1358 }
1359 if( p->pNext ){
1360 p->pNext->pPrev = p->pPrev;
1361 }
drh76ff3a02004-09-24 22:32:30 +00001362 if( p->aOp ){
1363 for(i=0; i<p->nOp; i++){
1364 Op *pOp = &p->aOp[i];
drhb38ad992005-09-16 00:27:01 +00001365 freeP3(pOp->p3type, pOp->p3);
drh9a324642003-09-06 20:12:01 +00001366 }
drh76ff3a02004-09-24 22:32:30 +00001367 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001368 }
drh76ff3a02004-09-24 22:32:30 +00001369 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001370 sqliteFree(p->aLabel);
1371 sqliteFree(p->aStack);
drh76ff3a02004-09-24 22:32:30 +00001372 releaseMemArray(p->aColName, p->nResColumn*2);
1373 sqliteFree(p->aColName);
drh9a324642003-09-06 20:12:01 +00001374 p->magic = VDBE_MAGIC_DEAD;
1375 sqliteFree(p);
1376}
drha11846b2004-01-07 18:52:56 +00001377
1378/*
drha11846b2004-01-07 18:52:56 +00001379** If a MoveTo operation is pending on the given cursor, then do that
1380** MoveTo now. Return an error code. If no MoveTo is pending, this
1381** routine does nothing and returns SQLITE_OK.
1382*/
danielk19774adee202004-05-08 08:23:19 +00001383int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001384 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001385 int res, rc;
danielk1977132872b2004-05-10 10:37:18 +00001386 extern int sqlite3_search_count;
drhf0863fe2005-06-12 21:35:51 +00001387 assert( p->isTable );
1388 if( p->isTable ){
drh536065a2005-01-26 21:55:31 +00001389 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
danielk19776490beb2004-05-11 06:17:21 +00001390 }else{
drh536065a2005-01-26 21:55:31 +00001391 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1392 sizeof(i64),&res);
danielk19776490beb2004-05-11 06:17:21 +00001393 }
drh536065a2005-01-26 21:55:31 +00001394 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001395 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001396 p->lastRowid = keyToInt(p->movetoTarget);
1397 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001398 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001399 rc = sqlite3BtreeNext(p->pCursor, &res);
1400 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001401 }
danielk1977132872b2004-05-10 10:37:18 +00001402 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001403 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001404 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001405 }
1406 return SQLITE_OK;
1407}
danielk19774adee202004-05-08 08:23:19 +00001408
drhab9f7f12004-05-08 10:56:11 +00001409/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001410** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001411**
danielk1977cfcdaef2004-05-12 07:33:33 +00001412** sqlite3VdbeSerialType()
1413** sqlite3VdbeSerialTypeLen()
1414** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001415** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001416** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001417**
1418** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001419** data and index records. Each serialized value consists of a
1420** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1421** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001422**
danielk1977cfcdaef2004-05-12 07:33:33 +00001423** In an SQLite index record, the serial type is stored directly before
1424** the blob of data that it corresponds to. In a table record, all serial
1425** types are stored at the start of the record, and the blobs of data at
1426** the end. Hence these functions allow the caller to handle the
1427** serial-type and data blob seperately.
1428**
1429** The following table describes the various storage classes for data:
1430**
1431** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001432** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001433** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001434** 1 1 signed integer
1435** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001436** 3 3 signed integer
1437** 4 4 signed integer
1438** 5 6 signed integer
1439** 6 8 signed integer
1440** 7 8 IEEE float
1441** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001442** N>=12 and even (N-12)/2 BLOB
1443** N>=13 and odd (N-13)/2 text
1444**
1445*/
1446
1447/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001448** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001449*/
drh25aa1b42004-05-28 01:39:01 +00001450u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001451 int flags = pMem->flags;
1452
1453 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001454 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001455 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001456 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001457 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001458# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
danielk1977cfcdaef2004-05-12 07:33:33 +00001459 i64 i = pMem->i;
drh5742b632005-01-26 17:47:02 +00001460 u64 u = i<0 ? -i : i;
1461 if( u<=127 ) return 1;
1462 if( u<=32767 ) return 2;
1463 if( u<=8388607 ) return 3;
1464 if( u<=2147483647 ) return 4;
1465 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001466 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001467 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001468 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001469 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001470 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001471 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001472 int n = pMem->n;
1473 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001474 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001475 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001476 if( flags&MEM_Blob ){
1477 return (pMem->n*2 + 12);
1478 }
1479 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001480}
1481
1482/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001483** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001484*/
drh25aa1b42004-05-28 01:39:01 +00001485int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001486 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001487 return (serial_type-12)/2;
1488 }else{
drh57196282004-10-06 15:41:16 +00001489 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001490 return aSize[serial_type];
1491 }
danielk1977192ac1d2004-05-10 07:17:30 +00001492}
1493
1494/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001495** Write the serialized data blob for the value stored in pMem into
1496** buf. It is assumed that the caller has allocated sufficient space.
1497** Return the number of bytes written.
1498*/
danielk1977b1bc9532004-05-22 03:05:33 +00001499int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001500 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001501 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001502
danielk1977cfcdaef2004-05-12 07:33:33 +00001503 /* NULL */
drha19b7752004-05-30 21:14:58 +00001504 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001505 return 0;
1506 }
1507
drh1483e142004-05-21 21:12:42 +00001508 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001509 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001510 u64 v;
1511 int i;
drha19b7752004-05-30 21:14:58 +00001512 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001513 v = *(u64*)&pMem->r;
1514 }else{
1515 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001516 }
drh1483e142004-05-21 21:12:42 +00001517 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1518 while( i-- ){
1519 buf[i] = (v&0xFF);
1520 v >>= 8;
1521 }
1522 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001523 }
1524
1525 /* String or blob */
1526 assert( serial_type>=12 );
1527 len = sqlite3VdbeSerialTypeLen(serial_type);
1528 memcpy(buf, pMem->z, len);
1529 return len;
1530}
1531
1532/*
1533** Deserialize the data blob pointed to by buf as serial type serial_type
1534** and store the result in pMem. Return the number of bytes read.
1535*/
danielk1977b1bc9532004-05-22 03:05:33 +00001536int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001537 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001538 u32 serial_type, /* Serial type to deserialize */
1539 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001540){
drh3c685822005-05-21 18:32:18 +00001541 switch( serial_type ){
1542 case 8: /* Reserved for future use */
1543 case 9: /* Reserved for future use */
1544 case 10: /* Reserved for future use */
1545 case 11: /* Reserved for future use */
1546 case 0: { /* NULL */
1547 pMem->flags = MEM_Null;
1548 break;
1549 }
1550 case 1: { /* 1-byte signed integer */
1551 pMem->i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001552 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001553 return 1;
drh1483e142004-05-21 21:12:42 +00001554 }
drh3c685822005-05-21 18:32:18 +00001555 case 2: { /* 2-byte signed integer */
1556 pMem->i = (((signed char)buf[0])<<8) | buf[1];
1557 pMem->flags = MEM_Int;
1558 return 2;
1559 }
1560 case 3: { /* 3-byte signed integer */
1561 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1562 pMem->flags = MEM_Int;
1563 return 3;
1564 }
1565 case 4: { /* 4-byte signed integer */
1566 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1567 pMem->flags = MEM_Int;
1568 return 4;
1569 }
1570 case 5: { /* 6-byte signed integer */
1571 u64 x = (((signed char)buf[0])<<8) | buf[1];
1572 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1573 x = (x<<32) | y;
1574 pMem->i = *(i64*)&x;
1575 pMem->flags = MEM_Int;
1576 return 6;
1577 }
drh91124b32005-08-18 18:15:05 +00001578 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00001579 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00001580 u64 x;
1581 u32 y;
drhde941c62005-08-28 01:34:21 +00001582#ifndef NDEBUG
1583 /* Verify that integers and floating point values use the same
drhbfd6b032005-08-28 01:38:44 +00001584 ** byte order. The byte order differs on some (broken) architectures.
1585 */
drhde941c62005-08-28 01:34:21 +00001586 static const u64 t1 = ((u64)0x3ff00000)<<32;
1587 assert( 1.0==*(double*)&t1 );
1588#endif
drhbfd6b032005-08-28 01:38:44 +00001589
drhd81bd4e2005-09-05 20:06:49 +00001590 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1591 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00001592 x = (x<<32) | y;
1593 if( serial_type==6 ){
1594 pMem->i = *(i64*)&x;
1595 pMem->flags = MEM_Int;
1596 }else{
1597 pMem->r = *(double*)&x;
1598 pMem->flags = MEM_Real;
1599 }
1600 return 8;
1601 }
1602 default: {
1603 int len = (serial_type-12)/2;
1604 pMem->z = (char *)buf;
1605 pMem->n = len;
1606 pMem->xDel = 0;
1607 if( serial_type&0x01 ){
1608 pMem->flags = MEM_Str | MEM_Ephem;
1609 }else{
1610 pMem->flags = MEM_Blob | MEM_Ephem;
1611 }
1612 return len;
drh696b32f2004-05-30 01:51:52 +00001613 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001614 }
drh3c685822005-05-21 18:32:18 +00001615 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001616}
1617
1618/*
drh7a224de2004-06-02 01:22:02 +00001619** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001620** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1621** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001622** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1623** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001624*/
drh7a224de2004-06-02 01:22:02 +00001625int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001626 void *userData,
1627 int nKey1, const void *pKey1,
1628 int nKey2, const void *pKey2
1629){
drhd3d39e92004-05-20 22:16:29 +00001630 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001631 u32 d1, d2; /* Offset into aKey[] of next data element */
1632 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1633 u32 szHdr1, szHdr2; /* Number of bytes in header */
1634 int i = 0;
1635 int nField;
1636 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001637 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1638 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001639
1640 Mem mem1;
1641 Mem mem2;
1642 mem1.enc = pKeyInfo->enc;
1643 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001644
1645 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1646 d1 = szHdr1;
1647 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1648 d2 = szHdr2;
1649 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001650 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001651 u32 serial_type1;
1652 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001653
1654 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001655 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001656 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001657 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001658 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001659
1660 /* Assert that there is enough space left in each key for the blob of
1661 ** data to go with the serial type just read. This assert may fail if
1662 ** the file is corrupted. Then read the value from each key into mem1
1663 ** and mem2 respectively.
1664 */
drh25aa1b42004-05-28 01:39:01 +00001665 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1666 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001667
drhd5788202004-05-28 08:21:05 +00001668 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001669 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1670 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001671 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001672 break;
1673 }
1674 i++;
1675 }
1676
1677 /* One of the keys ran out of fields, but all the fields up to that point
1678 ** were equal. If the incrKey flag is true, then the second key is
1679 ** treated as larger.
1680 */
1681 if( rc==0 ){
1682 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001683 rc = -1;
1684 }else if( d1<nKey1 ){
1685 rc = 1;
1686 }else if( d2<nKey2 ){
1687 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001688 }
1689 }
1690
drhd3194f52004-05-27 19:59:32 +00001691 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1692 rc = -rc;
1693 }
1694
1695 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001696}
drhd5788202004-05-28 08:21:05 +00001697
1698/*
drh7a224de2004-06-02 01:22:02 +00001699** The argument is an index entry composed using the OP_MakeRecord opcode.
1700** The last entry in this record should be an integer (specifically
1701** an integer rowid). This routine returns the number of bytes in
1702** that integer.
drhd5788202004-05-28 08:21:05 +00001703*/
1704int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1705 u32 szHdr; /* Size of the header */
1706 u32 typeRowid; /* Serial type of the rowid */
1707
1708 sqlite3GetVarint32(aKey, &szHdr);
1709 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1710 return sqlite3VdbeSerialTypeLen(typeRowid);
1711}
danielk1977eb015e02004-05-18 01:31:14 +00001712
1713
1714/*
drh7a224de2004-06-02 01:22:02 +00001715** pCur points at an index entry created using the OP_MakeRecord opcode.
1716** Read the rowid (the last field in the record) and store it in *rowid.
1717** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001718*/
1719int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001720 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001721 int rc;
drhd5788202004-05-28 08:21:05 +00001722 u32 szHdr; /* Size of the header */
1723 u32 typeRowid; /* Serial type of the rowid */
1724 u32 lenRowid; /* Size of the rowid */
1725 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001726
drhd5788202004-05-28 08:21:05 +00001727 sqlite3BtreeKeySize(pCur, &nCellKey);
1728 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00001729 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00001730 }
1731 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1732 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001733 return rc;
1734 }
drhd5788202004-05-28 08:21:05 +00001735 sqlite3GetVarint32(m.z, &szHdr);
1736 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1737 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1738 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1739 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001740 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001741 return SQLITE_OK;
1742}
1743
drh7cf6e4d2004-05-19 14:56:55 +00001744/*
drhd3d39e92004-05-20 22:16:29 +00001745** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001746** the key string in pKey (of length nKey). Write into *pRes a number
1747** that is negative, zero, or positive if pC is less than, equal to,
1748** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001749**
drhd5788202004-05-28 08:21:05 +00001750** pKey is either created without a rowid or is truncated so that it
1751** omits the rowid at the end. The rowid at the end of the index entry
1752** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001753*/
danielk1977183f9f72004-05-13 05:20:26 +00001754int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001755 Cursor *pC, /* The cursor to compare against */
1756 int nKey, const u8 *pKey, /* The key to compare */
1757 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001758){
danielk1977e0d4b062004-06-28 01:11:46 +00001759 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001760 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001761 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001762 int lenRowid;
1763 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001764
1765 sqlite3BtreeKeySize(pCur, &nCellKey);
1766 if( nCellKey<=0 ){
1767 *res = 0;
1768 return SQLITE_OK;
1769 }
drhd5788202004-05-28 08:21:05 +00001770 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1771 if( rc ){
1772 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001773 }
drhd5788202004-05-28 08:21:05 +00001774 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
drh7a224de2004-06-02 01:22:02 +00001775 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001776 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001777 return SQLITE_OK;
1778}
danielk1977b28af712004-06-21 06:50:26 +00001779
1780/*
1781** This routine sets the value to be returned by subsequent calls to
1782** sqlite3_changes() on the database handle 'db'.
1783*/
1784void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1785 db->nChange = nChange;
1786 db->nTotalChange += nChange;
1787}
1788
1789/*
1790** Set a flag in the vdbe to update the change counter when it is finalised
1791** or reset.
1792*/
drh4794f732004-11-05 17:17:50 +00001793void sqlite3VdbeCountChanges(Vdbe *v){
1794 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00001795}
drhd89bd002005-01-22 03:03:54 +00001796
1797/*
1798** Mark every prepared statement associated with a database connection
1799** as expired.
1800**
1801** An expired statement means that recompilation of the statement is
1802** recommend. Statements expire when things happen that make their
1803** programs obsolete. Removing user-defined functions or collating
1804** sequences, or changing an authorization function are the types of
1805** things that make prepared statements obsolete.
1806*/
1807void sqlite3ExpirePreparedStatements(sqlite3 *db){
1808 Vdbe *p;
1809 for(p = db->pVdbe; p; p=p->pNext){
1810 p->expired = 1;
1811 }
1812}
danielk1977aee18ef2005-03-09 12:26:50 +00001813
1814/*
1815** Return the database associated with the Vdbe.
1816*/
1817sqlite3 *sqlite3VdbeDb(Vdbe *v){
1818 return v->db;
1819}