blob: 26f829f07dd036e8d7de6612a4207246d9c4c7ac [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;
drhcc43cab2005-10-05 11:35:09 +0000860 releaseMemArray(p->aColName, p->nResColumn*2);
861 sqliteFree(p->aColName);
drh76ff3a02004-09-24 22:32:30 +0000862 n = nResColumn*2;
drhcc43cab2005-10-05 11:35:09 +0000863 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +0000864 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
865 if( p->aColName==0 ) return;
866 while( n-- > 0 ){
867 (pColName++)->flags = MEM_Null;
868 }
danielk197722322fd2004-05-25 23:35:17 +0000869}
870
871/*
danielk19773cf86062004-05-26 10:11:05 +0000872** Set the name of the idx'th column to be returned by the SQL statement.
873** zName must be a pointer to a nul terminated string.
874**
875** This call must be made after a call to sqlite3VdbeSetNumCols().
876**
danielk1977d8123362004-06-12 09:25:12 +0000877** If N==P3_STATIC it means that zName is a pointer to a constant static
878** string and we can just copy the pointer. If it is P3_DYNAMIC, then
879** the string is freed using sqliteFree() when the vdbe is finished with
880** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000881*/
882int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
883 int rc;
884 Mem *pColName;
danielk197776d505b2004-05-28 13:13:02 +0000885 assert( idx<(2*p->nResColumn) );
drh76ff3a02004-09-24 22:32:30 +0000886 if( sqlite3_malloc_failed ) return SQLITE_NOMEM;
887 assert( p->aColName!=0 );
danielk19773cf86062004-05-26 10:11:05 +0000888 pColName = &(p->aColName[idx]);
danielk1977d8123362004-06-12 09:25:12 +0000889 if( N==P3_DYNAMIC || N==P3_STATIC ){
890 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000891 }else{
danielk1977d8123362004-06-12 09:25:12 +0000892 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000893 }
894 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
895 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000896 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000897 }
898 return rc;
899}
900
danielk197713adf8a2004-06-03 16:08:41 +0000901/*
902** A read or write transaction may or may not be active on database handle
903** db. If a transaction is active, commit it. If there is a
904** write-transaction spanning more than one database file, this routine
905** takes care of the master journal trickery.
906*/
drh9bb575f2004-09-06 17:24:11 +0000907static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +0000908 int i;
909 int nTrans = 0; /* Number of databases with an active write-transaction */
910 int rc = SQLITE_OK;
911 int needXcommit = 0;
912
913 for(i=0; i<db->nDb; i++){
914 Btree *pBt = db->aDb[i].pBt;
915 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
916 needXcommit = 1;
917 if( i!=1 ) nTrans++;
918 }
919 }
920
921 /* If there are any write-transactions at all, invoke the commit hook */
922 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +0000923 int rc;
924 sqlite3SafetyOff(db);
925 rc = db->xCommitCallback(db->pCommitArg);
926 sqlite3SafetyOn(db);
927 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +0000928 return SQLITE_CONSTRAINT;
929 }
930 }
931
danielk197740b38dc2004-06-26 08:38:24 +0000932 /* The simple case - no more than one database file (not counting the
933 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000934 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000935 **
danielk197740b38dc2004-06-26 08:38:24 +0000936 ** If the return value of sqlite3BtreeGetFilename() is a zero length
937 ** string, it means the main database is :memory:. In that case we do
938 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000939 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000940 */
danielk197740b38dc2004-06-26 08:38:24 +0000941 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +0000942 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +0000943 Btree *pBt = db->aDb[i].pBt;
944 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +0000945 rc = sqlite3BtreeSync(pBt, 0);
946 }
947 }
948
949 /* Do the commit only if all databases successfully synced */
950 if( rc==SQLITE_OK ){
951 for(i=0; i<db->nDb; i++){
952 Btree *pBt = db->aDb[i].pBt;
953 if( pBt ){
954 sqlite3BtreeCommit(pBt);
955 }
danielk197713adf8a2004-06-03 16:08:41 +0000956 }
957 }
958 }
959
960 /* The complex case - There is a multi-file write-transaction active.
961 ** This requires a master journal file to ensure the transaction is
962 ** committed atomicly.
963 */
danielk197744ee5bf2005-05-27 09:41:12 +0000964#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +0000965 else{
drh2c8997b2005-08-27 16:36:48 +0000966 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +0000967 char *zMaster = 0; /* File-name for the master journal */
968 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
969 OsFile master;
970
971 /* Select a master journal file name */
972 do {
drha6abd042004-06-09 17:37:22 +0000973 u32 random;
974 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +0000975 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +0000976 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +0000977 if( !zMaster ){
978 return SQLITE_NOMEM;
979 }
980 }while( sqlite3OsFileExists(zMaster) );
981
982 /* Open the master journal. */
drhda71ce12004-06-21 18:14:45 +0000983 memset(&master, 0, sizeof(master));
danielk197713adf8a2004-06-03 16:08:41 +0000984 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
985 if( rc!=SQLITE_OK ){
986 sqliteFree(zMaster);
987 return rc;
988 }
989
990 /* Write the name of each database file in the transaction into the new
991 ** master journal file. If an error occurs at this point close
992 ** and delete the master journal file. All the individual journal files
993 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +0000994 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +0000995 */
996 for(i=0; i<db->nDb; i++){
997 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +0000998 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +0000999 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001000 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001001 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001002 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1003 needSync = 1;
1004 }
drhc9e06862004-06-09 20:03:08 +00001005 rc = sqlite3OsWrite(&master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001006 if( rc!=SQLITE_OK ){
1007 sqlite3OsClose(&master);
1008 sqlite3OsDelete(zMaster);
1009 sqliteFree(zMaster);
1010 return rc;
1011 }
1012 }
1013 }
1014
danielk197713adf8a2004-06-03 16:08:41 +00001015
danielk19775865e3d2004-06-14 06:03:57 +00001016 /* Sync the master journal file. Before doing this, open the directory
1017 ** the master journal file is store in so that it gets synced too.
1018 */
1019 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1020 rc = sqlite3OsOpenDirectory(zMainFile, &master);
drheb796a72005-09-08 12:38:41 +00001021 if( rc!=SQLITE_OK ||
1022 (needSync && (rc=sqlite3OsSync(&master,0))!=SQLITE_OK) ){
danielk19775865e3d2004-06-14 06:03:57 +00001023 sqlite3OsClose(&master);
1024 sqlite3OsDelete(zMaster);
1025 sqliteFree(zMaster);
1026 return rc;
1027 }
drhc9e06862004-06-09 20:03:08 +00001028
danielk197713adf8a2004-06-03 16:08:41 +00001029 /* Sync all the db files involved in the transaction. The same call
1030 ** sets the master journal pointer in each individual journal. If
1031 ** an error occurs here, do not delete the master journal file.
1032 **
1033 ** If the error occurs during the first call to sqlite3BtreeSync(),
1034 ** then there is a chance that the master journal file will be
1035 ** orphaned. But we cannot delete it, in case the master journal
1036 ** file name was written into the journal file before the failure
1037 ** occured.
1038 */
1039 for(i=0; i<db->nDb; i++){
1040 Btree *pBt = db->aDb[i].pBt;
1041 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1042 rc = sqlite3BtreeSync(pBt, zMaster);
1043 if( rc!=SQLITE_OK ){
danielk1977962398d2004-06-14 09:35:16 +00001044 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001045 sqliteFree(zMaster);
1046 return rc;
1047 }
1048 }
1049 }
danielk1977962398d2004-06-14 09:35:16 +00001050 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001051
danielk1977962398d2004-06-14 09:35:16 +00001052 /* Delete the master journal file. This commits the transaction. After
1053 ** doing this the directory is synced again before any individual
1054 ** transaction files are deleted.
1055 */
danielk197713adf8a2004-06-03 16:08:41 +00001056 rc = sqlite3OsDelete(zMaster);
1057 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001058 sqliteFree(zMaster);
1059 zMaster = 0;
danielk1977962398d2004-06-14 09:35:16 +00001060 rc = sqlite3OsSyncDirectory(zMainFile);
1061 if( rc!=SQLITE_OK ){
1062 /* This is not good. The master journal file has been deleted, but
1063 ** the directory sync failed. There is no completely safe course of
1064 ** action from here. The individual journals contain the name of the
1065 ** master journal file, but there is no way of knowing if that
1066 ** master journal exists now or if it will exist after the operating
1067 ** system crash that may follow the fsync() failure.
1068 */
danielk1977962398d2004-06-14 09:35:16 +00001069 return rc;
1070 }
danielk197713adf8a2004-06-03 16:08:41 +00001071
1072 /* All files and directories have already been synced, so the following
1073 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1074 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001075 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001076 ** but some stray 'cold' journals may be lying around. Returning an
1077 ** error code won't help matters.
1078 */
1079 for(i=0; i<db->nDb; i++){
1080 Btree *pBt = db->aDb[i].pBt;
1081 if( pBt ){
1082 sqlite3BtreeCommit(pBt);
1083 }
1084 }
1085 }
danielk197744ee5bf2005-05-27 09:41:12 +00001086#endif
danielk1977026d2702004-06-14 13:14:59 +00001087
drh2ac3ee92004-06-07 16:27:46 +00001088 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001089}
1090
drh91b48aa2004-06-30 11:14:18 +00001091/*
1092** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001093** aborted. This happens when one VM causes a rollback due to an
1094** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1095** aborted so that they do not have data rolled out from underneath
1096** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001097*/
1098static void abortOtherActiveVdbes(Vdbe *pVdbe){
1099 Vdbe *pOther;
1100 for(pOther=pVdbe->db->pVdbe; pOther; pOther=pOther->pNext){
1101 if( pOther==pVdbe ) continue;
1102 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1103 closeAllCursors(pOther);
1104 pOther->aborted = 1;
1105 }
1106}
1107
danielk19771d850a72004-05-31 08:26:49 +00001108/*
1109** This routine checks that the sqlite3.activeVdbeCnt count variable
1110** matches the number of vdbe's in the list sqlite3.pVdbe that are
1111** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001112** This is an internal self-check only - it is not an essential processing
1113** step.
danielk19771d850a72004-05-31 08:26:49 +00001114**
1115** This is a no-op if NDEBUG is defined.
1116*/
1117#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001118static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001119 Vdbe *p;
1120 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001121 p = db->pVdbe;
1122 while( p ){
drh92f02c32004-09-02 14:57:08 +00001123 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001124 cnt++;
1125 }
1126 p = p->pNext;
1127 }
danielk19771d850a72004-05-31 08:26:49 +00001128 assert( cnt==db->activeVdbeCnt );
1129}
1130#else
1131#define checkActiveVdbeCnt(x)
1132#endif
1133
danielk19773cf86062004-05-26 10:11:05 +00001134/*
drh92f02c32004-09-02 14:57:08 +00001135** This routine is called the when a VDBE tries to halt. If the VDBE
1136** has made changes and is in autocommit mode, then commit those
1137** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001138**
drh92f02c32004-09-02 14:57:08 +00001139** This routine is the only way to move the state of a VM from
1140** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1141**
1142** Return an error code. If the commit could not complete because of
1143** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1144** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001145*/
drh92f02c32004-09-02 14:57:08 +00001146int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001147 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001148 int i;
danielk19771d850a72004-05-31 08:26:49 +00001149 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
drh9a324642003-09-06 20:12:01 +00001150
drh92f02c32004-09-02 14:57:08 +00001151 if( p->magic!=VDBE_MAGIC_RUN ){
1152 /* Already halted. Nothing to do. */
1153 assert( p->magic==VDBE_MAGIC_HALT );
1154 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001155 }
drh92f02c32004-09-02 14:57:08 +00001156 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001157 checkActiveVdbeCnt(db);
drh178286b2005-01-23 13:14:55 +00001158 if( p->pc<0 ){
1159 /* No commit or rollback needed if the program never started */
1160 }else if( db->autoCommit && db->activeVdbeCnt==1 ){
danielk19771d850a72004-05-31 08:26:49 +00001161 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk197713adf8a2004-06-03 16:08:41 +00001162 /* The auto-commit flag is true, there are no other active queries
1163 ** using this handle and the vdbe program was successful or hit an
drh92f02c32004-09-02 14:57:08 +00001164 ** 'OR FAIL' constraint. This means a commit is required.
danielk197713adf8a2004-06-03 16:08:41 +00001165 */
drh92f02c32004-09-02 14:57:08 +00001166 int rc = vdbeCommit(db);
1167 if( rc==SQLITE_BUSY ){
1168 return SQLITE_BUSY;
1169 }else if( rc!=SQLITE_OK ){
1170 p->rc = rc;
1171 xFunc = sqlite3BtreeRollback;
danielk197713adf8a2004-06-03 16:08:41 +00001172 }
danielk19771d850a72004-05-31 08:26:49 +00001173 }else{
1174 xFunc = sqlite3BtreeRollback;
drh9a324642003-09-06 20:12:01 +00001175 }
danielk19771d850a72004-05-31 08:26:49 +00001176 }else{
1177 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1178 xFunc = sqlite3BtreeCommitStmt;
1179 }else if( p->errorAction==OE_Abort ){
1180 xFunc = sqlite3BtreeRollbackStmt;
1181 }else{
1182 xFunc = sqlite3BtreeRollback;
1183 db->autoCommit = 1;
drh91b48aa2004-06-30 11:14:18 +00001184 abortOtherActiveVdbes(p);
danielk19771d850a72004-05-31 08:26:49 +00001185 }
1186 }
1187
danielk197713adf8a2004-06-03 16:08:41 +00001188 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollback,
1189 ** sqlite3BtreeRollbackStmt or sqlite3BtreeCommitStmt. Call it once on
1190 ** each backend. If an error occurs and the return code is still
1191 ** SQLITE_OK, set the return code to the new error value.
1192 */
1193 for(i=0; xFunc && i<db->nDb; i++){
danielk1977ee5741e2004-05-31 10:01:34 +00001194 int rc;
danielk19771d850a72004-05-31 08:26:49 +00001195 Btree *pBt = db->aDb[i].pBt;
danielk197777d83ba2004-05-31 10:08:14 +00001196 if( pBt ){
1197 rc = xFunc(pBt);
1198 if( p->rc==SQLITE_OK ) p->rc = rc;
1199 }
danielk19771d850a72004-05-31 08:26:49 +00001200 }
1201
danielk1977b28af712004-06-21 06:50:26 +00001202 /* If this was an INSERT, UPDATE or DELETE, set the change counter. */
drhf4d173a2005-01-23 19:04:42 +00001203 if( p->changeCntOn && p->pc>=0 ){
danielk1977b28af712004-06-21 06:50:26 +00001204 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1205 sqlite3VdbeSetChanges(db, p->nChange);
1206 }else{
1207 sqlite3VdbeSetChanges(db, 0);
1208 }
1209 p->nChange = 0;
1210 }
danielk19771d850a72004-05-31 08:26:49 +00001211
drh92f02c32004-09-02 14:57:08 +00001212 /* Rollback or commit any schema changes that occurred. */
danielk19771d850a72004-05-31 08:26:49 +00001213 if( p->rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001214 sqlite3RollbackInternalChanges(db);
danielk1977026d2702004-06-14 13:14:59 +00001215 }else if( db->flags & SQLITE_InternChanges ){
danielk1977ec8450f2004-06-19 09:35:36 +00001216 sqlite3CommitInternalChanges(db);
drh9a324642003-09-06 20:12:01 +00001217 }
danielk19771d850a72004-05-31 08:26:49 +00001218
drh92f02c32004-09-02 14:57:08 +00001219 /* We have successfully halted and closed the VM. Record this fact. */
1220 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001221 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001222 }
drh92f02c32004-09-02 14:57:08 +00001223 p->magic = VDBE_MAGIC_HALT;
1224 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001225
drh92f02c32004-09-02 14:57:08 +00001226 return SQLITE_OK;
1227}
1228
1229/*
1230** Clean up a VDBE after execution but do not delete the VDBE just yet.
1231** Write any error messages into *pzErrMsg. Return the result code.
1232**
1233** After this routine is run, the VDBE should be ready to be executed
1234** again.
1235**
1236** To look at it another way, this routine resets the state of the
1237** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1238** VDBE_MAGIC_INIT.
1239*/
1240int sqlite3VdbeReset(Vdbe *p){
1241 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
drhc60d0442004-09-30 13:43:13 +00001242 sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh92f02c32004-09-02 14:57:08 +00001243 return SQLITE_MISUSE;
1244 }
1245
1246 /* If the VM did not run to completion or if it encountered an
1247 ** error, then it might not have been halted properly. So halt
1248 ** it now.
1249 */
1250 sqlite3VdbeHalt(p);
1251
drhfb7e7652005-01-24 00:28:42 +00001252 /* If the VDBE has be run even partially, then transfer the error code
1253 ** and error message from the VDBE into the main database structure. But
1254 ** if the VDBE has just been set to run but has not actually executed any
1255 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001256 */
drhfb7e7652005-01-24 00:28:42 +00001257 if( p->pc>=0 ){
1258 if( p->zErrMsg ){
1259 sqlite3Error(p->db, p->rc, "%s", p->zErrMsg);
1260 sqliteFree(p->zErrMsg);
1261 p->zErrMsg = 0;
1262 }else if( p->rc ){
1263 sqlite3Error(p->db, p->rc, 0);
1264 }else{
1265 sqlite3Error(p->db, SQLITE_OK, 0);
1266 }
danielk1977a21c6b62005-01-24 10:25:59 +00001267 }else if( p->rc && p->expired ){
1268 /* The expired flag was set on the VDBE before the first call
1269 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1270 ** called), set the database error in this case as well.
1271 */
1272 sqlite3Error(p->db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001273 }
1274
1275 /* Reclaim all memory used by the VDBE
1276 */
1277 Cleanup(p);
1278
1279 /* Save profiling information from this VDBE run.
1280 */
danielk19771d850a72004-05-31 08:26:49 +00001281 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || sqlite3_malloc_failed==1 );
drh9a324642003-09-06 20:12:01 +00001282#ifdef VDBE_PROFILE
1283 {
1284 FILE *out = fopen("vdbe_profile.out", "a");
1285 if( out ){
1286 int i;
1287 fprintf(out, "---- ");
1288 for(i=0; i<p->nOp; i++){
1289 fprintf(out, "%02x", p->aOp[i].opcode);
1290 }
1291 fprintf(out, "\n");
1292 for(i=0; i<p->nOp; i++){
1293 fprintf(out, "%6d %10lld %8lld ",
1294 p->aOp[i].cnt,
1295 p->aOp[i].cycles,
1296 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1297 );
danielk19774adee202004-05-08 08:23:19 +00001298 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001299 }
1300 fclose(out);
1301 }
1302 }
1303#endif
1304 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001305 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001306 if( p->rc==SQLITE_SCHEMA ){
1307 sqlite3ResetInternalSchema(p->db, 0);
1308 }
drh9a324642003-09-06 20:12:01 +00001309 return p->rc;
1310}
drh92f02c32004-09-02 14:57:08 +00001311
drh9a324642003-09-06 20:12:01 +00001312/*
1313** Clean up and delete a VDBE after execution. Return an integer which is
1314** the result code. Write any error message text into *pzErrMsg.
1315*/
danielk19779e6db7d2004-06-21 08:18:51 +00001316int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001317 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001318
danielk1977b5548a82004-06-26 13:51:33 +00001319 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1320 rc = sqlite3VdbeReset(p);
1321 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001322 return SQLITE_MISUSE;
1323 }
danielk19774adee202004-05-08 08:23:19 +00001324 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001325 return rc;
1326}
1327
1328/*
drhf92c7ff2004-06-19 15:40:23 +00001329** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001330** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001331** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001332** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001333*/
1334void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1335 int i;
1336 for(i=0; i<pVdbeFunc->nAux; i++){
1337 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1338 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1339 if( pAux->xDelete ){
1340 pAux->xDelete(pAux->pAux);
1341 }
1342 pAux->pAux = 0;
1343 }
1344 }
1345}
1346
1347/*
drh9a324642003-09-06 20:12:01 +00001348** Delete an entire VDBE.
1349*/
danielk19774adee202004-05-08 08:23:19 +00001350void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001351 int i;
1352 if( p==0 ) return;
1353 Cleanup(p);
1354 if( p->pPrev ){
1355 p->pPrev->pNext = p->pNext;
1356 }else{
1357 assert( p->db->pVdbe==p );
1358 p->db->pVdbe = p->pNext;
1359 }
1360 if( p->pNext ){
1361 p->pNext->pPrev = p->pPrev;
1362 }
drh76ff3a02004-09-24 22:32:30 +00001363 if( p->aOp ){
1364 for(i=0; i<p->nOp; i++){
1365 Op *pOp = &p->aOp[i];
drhb38ad992005-09-16 00:27:01 +00001366 freeP3(pOp->p3type, pOp->p3);
drh9a324642003-09-06 20:12:01 +00001367 }
drh76ff3a02004-09-24 22:32:30 +00001368 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001369 }
drh76ff3a02004-09-24 22:32:30 +00001370 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001371 sqliteFree(p->aLabel);
1372 sqliteFree(p->aStack);
drh76ff3a02004-09-24 22:32:30 +00001373 releaseMemArray(p->aColName, p->nResColumn*2);
1374 sqliteFree(p->aColName);
drh9a324642003-09-06 20:12:01 +00001375 p->magic = VDBE_MAGIC_DEAD;
1376 sqliteFree(p);
1377}
drha11846b2004-01-07 18:52:56 +00001378
1379/*
drha11846b2004-01-07 18:52:56 +00001380** If a MoveTo operation is pending on the given cursor, then do that
1381** MoveTo now. Return an error code. If no MoveTo is pending, this
1382** routine does nothing and returns SQLITE_OK.
1383*/
danielk19774adee202004-05-08 08:23:19 +00001384int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001385 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001386 int res, rc;
danielk1977132872b2004-05-10 10:37:18 +00001387 extern int sqlite3_search_count;
drhf0863fe2005-06-12 21:35:51 +00001388 assert( p->isTable );
1389 if( p->isTable ){
drh536065a2005-01-26 21:55:31 +00001390 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
danielk19776490beb2004-05-11 06:17:21 +00001391 }else{
drh536065a2005-01-26 21:55:31 +00001392 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1393 sizeof(i64),&res);
danielk19776490beb2004-05-11 06:17:21 +00001394 }
drh536065a2005-01-26 21:55:31 +00001395 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001396 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001397 p->lastRowid = keyToInt(p->movetoTarget);
1398 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001399 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001400 rc = sqlite3BtreeNext(p->pCursor, &res);
1401 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001402 }
danielk1977132872b2004-05-10 10:37:18 +00001403 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001404 p->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00001405 p->cacheValid = 0;
drha11846b2004-01-07 18:52:56 +00001406 }
1407 return SQLITE_OK;
1408}
danielk19774adee202004-05-08 08:23:19 +00001409
drhab9f7f12004-05-08 10:56:11 +00001410/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001411** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001412**
danielk1977cfcdaef2004-05-12 07:33:33 +00001413** sqlite3VdbeSerialType()
1414** sqlite3VdbeSerialTypeLen()
1415** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001416** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001417** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001418**
1419** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001420** data and index records. Each serialized value consists of a
1421** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1422** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001423**
danielk1977cfcdaef2004-05-12 07:33:33 +00001424** In an SQLite index record, the serial type is stored directly before
1425** the blob of data that it corresponds to. In a table record, all serial
1426** types are stored at the start of the record, and the blobs of data at
1427** the end. Hence these functions allow the caller to handle the
1428** serial-type and data blob seperately.
1429**
1430** The following table describes the various storage classes for data:
1431**
1432** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001433** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001434** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001435** 1 1 signed integer
1436** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001437** 3 3 signed integer
1438** 4 4 signed integer
1439** 5 6 signed integer
1440** 6 8 signed integer
1441** 7 8 IEEE float
1442** 8-11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001443** N>=12 and even (N-12)/2 BLOB
1444** N>=13 and odd (N-13)/2 text
1445**
1446*/
1447
1448/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001449** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001450*/
drh25aa1b42004-05-28 01:39:01 +00001451u32 sqlite3VdbeSerialType(Mem *pMem){
danielk1977cfcdaef2004-05-12 07:33:33 +00001452 int flags = pMem->flags;
1453
1454 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001455 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001456 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001457 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001458 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001459# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
danielk1977cfcdaef2004-05-12 07:33:33 +00001460 i64 i = pMem->i;
drh5742b632005-01-26 17:47:02 +00001461 u64 u = i<0 ? -i : i;
1462 if( u<=127 ) return 1;
1463 if( u<=32767 ) return 2;
1464 if( u<=8388607 ) return 3;
1465 if( u<=2147483647 ) return 4;
1466 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001467 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001468 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001469 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001470 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001471 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001472 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001473 int n = pMem->n;
1474 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001475 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001476 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001477 if( flags&MEM_Blob ){
1478 return (pMem->n*2 + 12);
1479 }
1480 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001481}
1482
1483/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001484** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001485*/
drh25aa1b42004-05-28 01:39:01 +00001486int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001487 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001488 return (serial_type-12)/2;
1489 }else{
drh57196282004-10-06 15:41:16 +00001490 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001491 return aSize[serial_type];
1492 }
danielk1977192ac1d2004-05-10 07:17:30 +00001493}
1494
1495/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001496** Write the serialized data blob for the value stored in pMem into
1497** buf. It is assumed that the caller has allocated sufficient space.
1498** Return the number of bytes written.
1499*/
danielk1977b1bc9532004-05-22 03:05:33 +00001500int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
drh25aa1b42004-05-28 01:39:01 +00001501 u32 serial_type = sqlite3VdbeSerialType(pMem);
danielk1977cfcdaef2004-05-12 07:33:33 +00001502 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001503
danielk1977cfcdaef2004-05-12 07:33:33 +00001504 /* NULL */
drha19b7752004-05-30 21:14:58 +00001505 if( serial_type==0 ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001506 return 0;
1507 }
1508
drh1483e142004-05-21 21:12:42 +00001509 /* Integer and Real */
drha19b7752004-05-30 21:14:58 +00001510 if( serial_type<=7 ){
drh1483e142004-05-21 21:12:42 +00001511 u64 v;
1512 int i;
drha19b7752004-05-30 21:14:58 +00001513 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001514 v = *(u64*)&pMem->r;
1515 }else{
1516 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001517 }
drh1483e142004-05-21 21:12:42 +00001518 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1519 while( i-- ){
1520 buf[i] = (v&0xFF);
1521 v >>= 8;
1522 }
1523 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001524 }
1525
1526 /* String or blob */
1527 assert( serial_type>=12 );
1528 len = sqlite3VdbeSerialTypeLen(serial_type);
1529 memcpy(buf, pMem->z, len);
1530 return len;
1531}
1532
1533/*
1534** Deserialize the data blob pointed to by buf as serial type serial_type
1535** and store the result in pMem. Return the number of bytes read.
1536*/
danielk1977b1bc9532004-05-22 03:05:33 +00001537int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001538 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001539 u32 serial_type, /* Serial type to deserialize */
1540 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001541){
drh3c685822005-05-21 18:32:18 +00001542 switch( serial_type ){
1543 case 8: /* Reserved for future use */
1544 case 9: /* Reserved for future use */
1545 case 10: /* Reserved for future use */
1546 case 11: /* Reserved for future use */
1547 case 0: { /* NULL */
1548 pMem->flags = MEM_Null;
1549 break;
1550 }
1551 case 1: { /* 1-byte signed integer */
1552 pMem->i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001553 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001554 return 1;
drh1483e142004-05-21 21:12:42 +00001555 }
drh3c685822005-05-21 18:32:18 +00001556 case 2: { /* 2-byte signed integer */
1557 pMem->i = (((signed char)buf[0])<<8) | buf[1];
1558 pMem->flags = MEM_Int;
1559 return 2;
1560 }
1561 case 3: { /* 3-byte signed integer */
1562 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1563 pMem->flags = MEM_Int;
1564 return 3;
1565 }
1566 case 4: { /* 4-byte signed integer */
1567 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1568 pMem->flags = MEM_Int;
1569 return 4;
1570 }
1571 case 5: { /* 6-byte signed integer */
1572 u64 x = (((signed char)buf[0])<<8) | buf[1];
1573 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1574 x = (x<<32) | y;
1575 pMem->i = *(i64*)&x;
1576 pMem->flags = MEM_Int;
1577 return 6;
1578 }
drh91124b32005-08-18 18:15:05 +00001579 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00001580 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00001581 u64 x;
1582 u32 y;
drhde941c62005-08-28 01:34:21 +00001583#ifndef NDEBUG
1584 /* Verify that integers and floating point values use the same
drhbfd6b032005-08-28 01:38:44 +00001585 ** byte order. The byte order differs on some (broken) architectures.
1586 */
drhde941c62005-08-28 01:34:21 +00001587 static const u64 t1 = ((u64)0x3ff00000)<<32;
1588 assert( 1.0==*(double*)&t1 );
1589#endif
drhbfd6b032005-08-28 01:38:44 +00001590
drhd81bd4e2005-09-05 20:06:49 +00001591 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1592 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00001593 x = (x<<32) | y;
1594 if( serial_type==6 ){
1595 pMem->i = *(i64*)&x;
1596 pMem->flags = MEM_Int;
1597 }else{
1598 pMem->r = *(double*)&x;
1599 pMem->flags = MEM_Real;
1600 }
1601 return 8;
1602 }
1603 default: {
1604 int len = (serial_type-12)/2;
1605 pMem->z = (char *)buf;
1606 pMem->n = len;
1607 pMem->xDel = 0;
1608 if( serial_type&0x01 ){
1609 pMem->flags = MEM_Str | MEM_Ephem;
1610 }else{
1611 pMem->flags = MEM_Blob | MEM_Ephem;
1612 }
1613 return len;
drh696b32f2004-05-30 01:51:52 +00001614 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001615 }
drh3c685822005-05-21 18:32:18 +00001616 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001617}
1618
1619/*
drh7a224de2004-06-02 01:22:02 +00001620** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001621** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1622** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001623** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1624** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001625*/
drh7a224de2004-06-02 01:22:02 +00001626int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001627 void *userData,
1628 int nKey1, const void *pKey1,
1629 int nKey2, const void *pKey2
1630){
drhd3d39e92004-05-20 22:16:29 +00001631 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001632 u32 d1, d2; /* Offset into aKey[] of next data element */
1633 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1634 u32 szHdr1, szHdr2; /* Number of bytes in header */
1635 int i = 0;
1636 int nField;
1637 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001638 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1639 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001640
1641 Mem mem1;
1642 Mem mem2;
1643 mem1.enc = pKeyInfo->enc;
1644 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001645
1646 idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
1647 d1 = szHdr1;
1648 idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
1649 d2 = szHdr2;
1650 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001651 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001652 u32 serial_type1;
1653 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001654
1655 /* Read the serial types for the next element in each key. */
drhd3194f52004-05-27 19:59:32 +00001656 idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
drhd5788202004-05-28 08:21:05 +00001657 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drhd3194f52004-05-27 19:59:32 +00001658 idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
drhd5788202004-05-28 08:21:05 +00001659 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001660
1661 /* Assert that there is enough space left in each key for the blob of
1662 ** data to go with the serial type just read. This assert may fail if
1663 ** the file is corrupted. Then read the value from each key into mem1
1664 ** and mem2 respectively.
1665 */
drh25aa1b42004-05-28 01:39:01 +00001666 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1667 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001668
drhd5788202004-05-28 08:21:05 +00001669 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001670 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1671 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001672 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001673 break;
1674 }
1675 i++;
1676 }
1677
1678 /* One of the keys ran out of fields, but all the fields up to that point
1679 ** were equal. If the incrKey flag is true, then the second key is
1680 ** treated as larger.
1681 */
1682 if( rc==0 ){
1683 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001684 rc = -1;
1685 }else if( d1<nKey1 ){
1686 rc = 1;
1687 }else if( d2<nKey2 ){
1688 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001689 }
1690 }
1691
drhd3194f52004-05-27 19:59:32 +00001692 if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){
1693 rc = -rc;
1694 }
1695
1696 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001697}
drhd5788202004-05-28 08:21:05 +00001698
1699/*
drh7a224de2004-06-02 01:22:02 +00001700** The argument is an index entry composed using the OP_MakeRecord opcode.
1701** The last entry in this record should be an integer (specifically
1702** an integer rowid). This routine returns the number of bytes in
1703** that integer.
drhd5788202004-05-28 08:21:05 +00001704*/
1705int sqlite3VdbeIdxRowidLen(int nKey, const u8 *aKey){
1706 u32 szHdr; /* Size of the header */
1707 u32 typeRowid; /* Serial type of the rowid */
1708
1709 sqlite3GetVarint32(aKey, &szHdr);
1710 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1711 return sqlite3VdbeSerialTypeLen(typeRowid);
1712}
danielk1977eb015e02004-05-18 01:31:14 +00001713
1714
1715/*
drh7a224de2004-06-02 01:22:02 +00001716** pCur points at an index entry created using the OP_MakeRecord opcode.
1717** Read the rowid (the last field in the record) and store it in *rowid.
1718** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001719*/
1720int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001721 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001722 int rc;
drhd5788202004-05-28 08:21:05 +00001723 u32 szHdr; /* Size of the header */
1724 u32 typeRowid; /* Serial type of the rowid */
1725 u32 lenRowid; /* Size of the rowid */
1726 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001727
drhd5788202004-05-28 08:21:05 +00001728 sqlite3BtreeKeySize(pCur, &nCellKey);
1729 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00001730 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00001731 }
1732 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1733 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001734 return rc;
1735 }
drhd5788202004-05-28 08:21:05 +00001736 sqlite3GetVarint32(m.z, &szHdr);
1737 sqlite3GetVarint32(&m.z[szHdr-1], &typeRowid);
1738 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1739 sqlite3VdbeSerialGet(&m.z[m.n-lenRowid], typeRowid, &v);
1740 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001741 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001742 return SQLITE_OK;
1743}
1744
drh7cf6e4d2004-05-19 14:56:55 +00001745/*
drhd3d39e92004-05-20 22:16:29 +00001746** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001747** the key string in pKey (of length nKey). Write into *pRes a number
1748** that is negative, zero, or positive if pC is less than, equal to,
1749** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001750**
drhd5788202004-05-28 08:21:05 +00001751** pKey is either created without a rowid or is truncated so that it
1752** omits the rowid at the end. The rowid at the end of the index entry
1753** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001754*/
danielk1977183f9f72004-05-13 05:20:26 +00001755int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001756 Cursor *pC, /* The cursor to compare against */
1757 int nKey, const u8 *pKey, /* The key to compare */
1758 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001759){
danielk1977e0d4b062004-06-28 01:11:46 +00001760 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001761 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001762 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001763 int lenRowid;
1764 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001765
1766 sqlite3BtreeKeySize(pCur, &nCellKey);
1767 if( nCellKey<=0 ){
1768 *res = 0;
1769 return SQLITE_OK;
1770 }
drhd5788202004-05-28 08:21:05 +00001771 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1772 if( rc ){
1773 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001774 }
drhd5788202004-05-28 08:21:05 +00001775 lenRowid = sqlite3VdbeIdxRowidLen(m.n, m.z);
drh7a224de2004-06-02 01:22:02 +00001776 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001777 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001778 return SQLITE_OK;
1779}
danielk1977b28af712004-06-21 06:50:26 +00001780
1781/*
1782** This routine sets the value to be returned by subsequent calls to
1783** sqlite3_changes() on the database handle 'db'.
1784*/
1785void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1786 db->nChange = nChange;
1787 db->nTotalChange += nChange;
1788}
1789
1790/*
1791** Set a flag in the vdbe to update the change counter when it is finalised
1792** or reset.
1793*/
drh4794f732004-11-05 17:17:50 +00001794void sqlite3VdbeCountChanges(Vdbe *v){
1795 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00001796}
drhd89bd002005-01-22 03:03:54 +00001797
1798/*
1799** Mark every prepared statement associated with a database connection
1800** as expired.
1801**
1802** An expired statement means that recompilation of the statement is
1803** recommend. Statements expire when things happen that make their
1804** programs obsolete. Removing user-defined functions or collating
1805** sequences, or changing an authorization function are the types of
1806** things that make prepared statements obsolete.
1807*/
1808void sqlite3ExpirePreparedStatements(sqlite3 *db){
1809 Vdbe *p;
1810 for(p = db->pVdbe; p; p=p->pNext){
1811 p->expired = 1;
1812 }
1813}
danielk1977aee18ef2005-03-09 12:26:50 +00001814
1815/*
1816** Return the database associated with the Vdbe.
1817*/
1818sqlite3 *sqlite3VdbeDb(Vdbe *v){
1819 return v->db;
1820}