blob: bc9468e57b3ef8530e4a9b8bf7c649e0ee76ddc2 [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
2** 2003 September 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
danielk1977fc57d7b2004-05-26 02:04:57 +000013** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
drh9a324642003-09-06 20:12:01 +000014** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <ctype.h>
20#include "vdbeInt.h"
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
danielk1977132872b2004-05-10 10:37:18 +000025** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
drh9a324642003-09-06 20:12:01 +000026** as they are added to the instruction stream.
27*/
drh8d904f02005-06-14 17:47:58 +000028#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +000029int sqlite3_vdbe_addop_trace = 0;
drh9a324642003-09-06 20:12:01 +000030#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
drh9bb575f2004-09-06 17:24:11 +000036Vdbe *sqlite3VdbeCreate(sqlite3 *db){
drh9a324642003-09-06 20:12:01 +000037 Vdbe *p;
38 p = sqliteMalloc( sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Turn tracing on or off
53*/
danielk19774adee202004-05-08 08:23:19 +000054void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000055 p->trace = trace;
56}
57
58/*
drh76ff3a02004-09-24 22:32:30 +000059** Resize the Vdbe.aOp array so that it contains at least N
danielk1977634f2982005-03-28 08:44:07 +000060** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
danielk1977ace3eb22006-01-26 10:35:04 +000061** the Vdbe.aOp array will be sized to contain exactly N
62** elements. Vdbe.nOpAlloc is set to reflect the new size of
63** the array.
64**
65** If an out-of-memory error occurs while resizing the array,
66** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
67** any opcodes already allocated can be correctly deallocated
68** along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +000069*/
70static void resizeOpArray(Vdbe *p, int N){
drh53f733c2005-09-16 02:38:09 +000071 int runMode = p->magic==VDBE_MAGIC_RUN;
72 if( runMode || p->nOpAlloc<N ){
drhb38ad992005-09-16 00:27:01 +000073 VdbeOp *pNew;
drh53f733c2005-09-16 02:38:09 +000074 int nNew = N + 100*(!runMode);
75 int oldSize = p->nOpAlloc;
76 pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
drhb38ad992005-09-16 00:27:01 +000077 if( pNew ){
drh53f733c2005-09-16 02:38:09 +000078 p->nOpAlloc = nNew;
drhb38ad992005-09-16 00:27:01 +000079 p->aOp = pNew;
drh53f733c2005-09-16 02:38:09 +000080 if( nNew>oldSize ){
81 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
82 }
drh76ff3a02004-09-24 22:32:30 +000083 }
84 }
85}
86
87/*
drh9a324642003-09-06 20:12:01 +000088** Add a new instruction to the list of instructions current in the
89** VDBE. Return the address of the new instruction.
90**
91** Parameters:
92**
93** p Pointer to the VDBE
94**
95** op The opcode for this instruction
96**
97** p1, p2 First two of the three possible operands.
98**
danielk19774adee202004-05-08 08:23:19 +000099** Use the sqlite3VdbeResolveLabel() function to fix an address and
100** the sqlite3VdbeChangeP3() function to change the value of the P3
drh9a324642003-09-06 20:12:01 +0000101** operand.
102*/
danielk19774adee202004-05-08 08:23:19 +0000103int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
drh9a324642003-09-06 20:12:01 +0000104 int i;
drh701a0ae2004-02-22 20:05:00 +0000105 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000106
107 i = p->nOp;
108 p->nOp++;
109 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000110 if( p->nOpAlloc<=i ){
111 resizeOpArray(p, i+1);
112 if( sqlite3MallocFailed() ){
113 return 0;
114 }
drh9a324642003-09-06 20:12:01 +0000115 }
drh701a0ae2004-02-22 20:05:00 +0000116 pOp = &p->aOp[i];
117 pOp->opcode = op;
118 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000119 pOp->p2 = p2;
120 pOp->p3 = 0;
121 pOp->p3type = P3_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000122 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000123#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000124 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000125#endif
126 return i;
127}
128
129/*
drh701a0ae2004-02-22 20:05:00 +0000130** Add an opcode that includes the p3 value.
131*/
drheb2e1762004-05-27 01:53:56 +0000132int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
danielk19774adee202004-05-08 08:23:19 +0000133 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
134 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
drh701a0ae2004-02-22 20:05:00 +0000135 return addr;
136}
137
138/*
drh9a324642003-09-06 20:12:01 +0000139** Create a new symbolic label for an instruction that has yet to be
140** coded. The symbolic label is really just a negative number. The
141** label can be used as the P2 value of an operation. Later, when
142** the label is resolved to a specific address, the VDBE will scan
143** through its operation list and change all values of P2 which match
144** the label into the resolved address.
145**
146** The VDBE knows that a P2 value is a label because labels are
147** always negative and P2 values are suppose to be non-negative.
148** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000149**
150** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000151*/
danielk19774adee202004-05-08 08:23:19 +0000152int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000153 int i;
154 i = p->nLabel++;
155 assert( p->magic==VDBE_MAGIC_INIT );
156 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000157 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
danielk1977e7259292006-01-13 06:33:23 +0000158 sqliteReallocOrFree((void**)&p->aLabel,
drh53f733c2005-09-16 02:38:09 +0000159 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000160 }
drh76ff3a02004-09-24 22:32:30 +0000161 if( p->aLabel ){
162 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000163 }
drh9a324642003-09-06 20:12:01 +0000164 return -1-i;
165}
166
167/*
168** Resolve label "x" to be the address of the next instruction to
169** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000170** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000171*/
danielk19774adee202004-05-08 08:23:19 +0000172void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000173 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000174 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000175 assert( j>=0 && j<p->nLabel );
176 if( p->aLabel ){
177 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000178 }
179}
180
181/*
danielk1977bc04f852005-03-29 08:26:13 +0000182** Return non-zero if opcode 'op' is guarenteed not to push more values
183** onto the VDBE stack than it pops off.
184*/
danielk19777a5147c2005-03-29 13:07:00 +0000185static int opcodeNoPush(u8 op){
186 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000187 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
188 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000189 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000190 ** in a comment on the same line as the "case OP_XXX:" in
191 ** sqlite3VdbeExec() in vdbe.c.
192 **
193 ** If the bit is true, then the corresponding opcode is guarenteed not
194 ** to grow the stack when it is executed. Otherwise, it may grow the
195 ** stack by at most one entry.
196 **
danielk19777a5147c2005-03-29 13:07:00 +0000197 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000198 ** one bit for opcodes 16 to 31, and so on.
199 **
200 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
201 ** because the file is generated by an awk program. Awk manipulates
202 ** all numbers as floating-point and we don't want to risk a rounding
203 ** error if someone builds with an awk that uses (for example) 32-bit
204 ** IEEE floats.
205 */
drh9a7e6082005-03-31 22:26:19 +0000206 static const u32 masks[5] = {
drh580eeaf2006-02-24 03:09:37 +0000207 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
208 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
209 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
210 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
211 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000212 };
drh9cbe6352005-11-29 03:13:21 +0000213 assert( op<32*5 );
danielk1977bc04f852005-03-29 08:26:13 +0000214 return (masks[op>>5] & (1<<(op&0x1F)));
215}
216
217#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000218int sqlite3VdbeOpcodeNoPush(u8 op){
219 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000220}
221#endif
222
223/*
drh76ff3a02004-09-24 22:32:30 +0000224** Loop through the program looking for P2 values that are negative.
225** Each such value is a label. Resolve the label by setting the P2
226** value to its correct non-zero value.
227**
228** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000229**
drh13449892005-09-07 21:22:45 +0000230** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
231** to an OP_Function or OP_AggStep opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000232** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000233**
234** The integer *pMaxStack is set to the maximum number of vdbe stack
235** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000236**
237** This routine also does the following optimization: It scans for
238** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
drhf0863fe2005-06-12 21:35:51 +0000239** IdxInsert instructions where P2!=0. If no such instruction is
drh38449902005-06-07 01:43:41 +0000240** found, then every Statement instruction is changed to a Noop. In
241** this way, we avoid creating the statement journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000242*/
danielk1977bc04f852005-03-29 08:26:13 +0000243static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000244 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000245 int nMaxArgs = 0;
246 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000247 Op *pOp;
248 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000249 int doesStatementRollback = 0;
250 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000251 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000252 u8 opcode = pOp->opcode;
253
drh13449892005-09-07 21:22:45 +0000254 if( opcode==OP_Function || opcode==OP_AggStep ){
danielk1977bc04f852005-03-29 08:26:13 +0000255 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
drh38449902005-06-07 01:43:41 +0000256 }else if( opcode==OP_Halt ){
257 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
258 doesStatementRollback = 1;
259 }
drhf0863fe2005-06-12 21:35:51 +0000260 }else if( opcode==OP_IdxInsert ){
drh38449902005-06-07 01:43:41 +0000261 if( pOp->p2 ){
262 doesStatementRollback = 1;
263 }
264 }else if( opcode==OP_Statement ){
265 hasStatementBegin = 1;
drh4be8b512006-06-13 23:51:34 +0000266 }else if( opcode==OP_VFilter ){
267 int n;
268 assert( p->nOp - i >= 3 );
269 assert( pOp[-2].opcode==OP_Integer );
270 n = pOp[-2].p1;
271 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977bc04f852005-03-29 08:26:13 +0000272 }
danielk19777a5147c2005-03-29 13:07:00 +0000273 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000274 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000275 }
276
drh76ff3a02004-09-24 22:32:30 +0000277 if( pOp->p2>=0 ) continue;
278 assert( -1-pOp->p2<p->nLabel );
279 pOp->p2 = aLabel[-1-pOp->p2];
280 }
281 sqliteFree(p->aLabel);
282 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000283
284 *pMaxFuncArgs = nMaxArgs;
285 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000286
287 /* If we never rollback a statement transaction, then statement
288 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000289 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000290 ** which can be expensive on some platforms.
291 */
292 if( hasStatementBegin && !doesStatementRollback ){
293 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
294 if( pOp->opcode==OP_Statement ){
295 pOp->opcode = OP_Noop;
296 }
297 }
298 }
drh76ff3a02004-09-24 22:32:30 +0000299}
300
301/*
drh9a324642003-09-06 20:12:01 +0000302** Return the address of the next instruction to be inserted.
303*/
danielk19774adee202004-05-08 08:23:19 +0000304int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000305 assert( p->magic==VDBE_MAGIC_INIT );
306 return p->nOp;
307}
308
309/*
310** Add a whole list of operations to the operation stack. Return the
311** address of the first operation added.
312*/
danielk19774adee202004-05-08 08:23:19 +0000313int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000314 int addr;
315 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000316 resizeOpArray(p, p->nOp + nOp);
danielk19779e128002006-01-18 16:51:35 +0000317 if( sqlite3MallocFailed() ){
drh76ff3a02004-09-24 22:32:30 +0000318 return 0;
drh9a324642003-09-06 20:12:01 +0000319 }
320 addr = p->nOp;
321 if( nOp>0 ){
322 int i;
drh905793e2004-02-21 13:31:09 +0000323 VdbeOpList const *pIn = aOp;
324 for(i=0; i<nOp; i++, pIn++){
325 int p2 = pIn->p2;
326 VdbeOp *pOut = &p->aOp[i+addr];
327 pOut->opcode = pIn->opcode;
328 pOut->p1 = pIn->p1;
329 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
330 pOut->p3 = pIn->p3;
331 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
danielk19778b60e0f2005-01-12 09:10:39 +0000332#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000333 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000334 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000335 }
336#endif
337 }
338 p->nOp += nOp;
339 }
340 return addr;
341}
342
343/*
344** Change the value of the P1 operand for a specific instruction.
345** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000346** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000347** few minor changes to the program.
348*/
danielk19774adee202004-05-08 08:23:19 +0000349void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000350 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000351 if( p && addr>=0 && p->nOp>addr && p->aOp ){
352 p->aOp[addr].p1 = val;
353 }
354}
355
356/*
357** Change the value of the P2 operand for a specific instruction.
358** This routine is useful for setting a jump destination.
359*/
danielk19774adee202004-05-08 08:23:19 +0000360void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000361 assert( val>=0 );
drh8aa34ae2006-03-13 12:54:09 +0000362 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000363 if( p && addr>=0 && p->nOp>addr && p->aOp ){
364 p->aOp[addr].p2 = val;
365 }
366}
367
drhd654be82005-09-20 17:42:23 +0000368/*
drhf8875402006-03-17 13:56:34 +0000369** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000370** the address of the next instruction to be coded.
371*/
372void sqlite3VdbeJumpHere(Vdbe *p, int addr){
373 sqlite3VdbeChangeP2(p, addr, p->nOp);
374}
drhb38ad992005-09-16 00:27:01 +0000375
376/*
377** Delete a P3 value if necessary.
378*/
379static void freeP3(int p3type, void *p3){
380 if( p3 ){
drhac1733d2005-09-17 17:58:22 +0000381 switch( p3type ){
382 case P3_DYNAMIC:
383 case P3_KEYINFO:
384 case P3_KEYINFO_HANDOFF: {
385 sqliteFree(p3);
386 break;
387 }
drh4be8b512006-06-13 23:51:34 +0000388 case P3_MPRINTF: {
389 sqlite3_free(p3);
390 break;
391 }
drhac1733d2005-09-17 17:58:22 +0000392 case P3_VDBEFUNC: {
393 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
394 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
395 sqliteFree(pVdbeFunc);
396 break;
397 }
398 case P3_MEM: {
399 sqlite3ValueFree((sqlite3_value*)p3);
400 break;
401 }
drhb38ad992005-09-16 00:27:01 +0000402 }
403 }
404}
405
406
drh9a324642003-09-06 20:12:01 +0000407/*
drhf8875402006-03-17 13:56:34 +0000408** Change N opcodes starting at addr to No-ops.
409*/
410void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
411 VdbeOp *pOp = &p->aOp[addr];
412 while( N-- ){
413 freeP3(pOp->p3type, pOp->p3);
414 memset(pOp, 0, sizeof(pOp[0]));
415 pOp->opcode = OP_Noop;
416 pOp++;
417 }
418}
419
420/*
drh9a324642003-09-06 20:12:01 +0000421** Change the value of the P3 operand for a specific instruction.
422** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000423** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000424** few minor changes to the program.
425**
426** If n>=0 then the P3 operand is dynamic, meaning that a copy of
427** the string is made into memory obtained from sqliteMalloc().
428** A value of n==0 means copy bytes of zP3 up to and including the
429** first null byte. If n>0 then copy n+1 bytes of zP3.
430**
danielk19771f55c052005-05-19 08:42:59 +0000431** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
432** A copy is made of the KeyInfo structure into memory obtained from
433** sqliteMalloc, to be freed when the Vdbe is finalized.
434** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
435** stored in memory that the caller has obtained from sqliteMalloc. The
436** caller should not free the allocation, it will be freed when the Vdbe is
437** finalized.
438**
439** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
440** to a string or structure that is guaranteed to exist for the lifetime of
441** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000442**
443** If addr<0 then change P3 on the most recently inserted instruction.
444*/
danielk19774adee202004-05-08 08:23:19 +0000445void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
drh9a324642003-09-06 20:12:01 +0000446 Op *pOp;
drh8aa34ae2006-03-13 12:54:09 +0000447 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
danielk19779e128002006-01-18 16:51:35 +0000448 if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000449 if (n != P3_KEYINFO) {
450 freeP3(n, (void*)*(char**)&zP3);
451 }
danielk1977d5d56522005-03-16 12:15:20 +0000452 return;
453 }
drh9a324642003-09-06 20:12:01 +0000454 if( addr<0 || addr>=p->nOp ){
455 addr = p->nOp - 1;
456 if( addr<0 ) return;
457 }
458 pOp = &p->aOp[addr];
drhb38ad992005-09-16 00:27:01 +0000459 freeP3(pOp->p3type, pOp->p3);
460 pOp->p3 = 0;
drh9a324642003-09-06 20:12:01 +0000461 if( zP3==0 ){
462 pOp->p3 = 0;
463 pOp->p3type = P3_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000464 }else if( n==P3_KEYINFO ){
465 KeyInfo *pKeyInfo;
466 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000467
drhd3d39e92004-05-20 22:16:29 +0000468 nField = ((KeyInfo*)zP3)->nField;
drhfdd6e852005-12-16 01:06:16 +0000469 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drheafe05b2004-06-13 00:54:01 +0000470 pKeyInfo = sqliteMallocRaw( nByte );
drhd3d39e92004-05-20 22:16:29 +0000471 pOp->p3 = (char*)pKeyInfo;
472 if( pKeyInfo ){
danielk1977bab45c62006-01-16 15:14:27 +0000473 unsigned char *aSortOrder;
drhd3d39e92004-05-20 22:16:29 +0000474 memcpy(pKeyInfo, zP3, nByte);
drhfdd6e852005-12-16 01:06:16 +0000475 aSortOrder = pKeyInfo->aSortOrder;
476 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000477 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000478 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
479 }
drhd3d39e92004-05-20 22:16:29 +0000480 pOp->p3type = P3_KEYINFO;
481 }else{
482 pOp->p3type = P3_NOTUSED;
483 }
drhffbc3082004-05-21 01:29:06 +0000484 }else if( n==P3_KEYINFO_HANDOFF ){
485 pOp->p3 = (char*)zP3;
486 pOp->p3type = P3_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000487 }else if( n<0 ){
488 pOp->p3 = (char*)zP3;
489 pOp->p3type = n;
490 }else{
drhae29ffb2004-09-25 14:39:18 +0000491 if( n==0 ) n = strlen(zP3);
492 pOp->p3 = sqliteStrNDup(zP3, n);
drh9a324642003-09-06 20:12:01 +0000493 pOp->p3type = P3_DYNAMIC;
494 }
495}
496
drhad6d9462004-09-19 02:15:24 +0000497#ifndef NDEBUG
498/*
499** Replace the P3 field of the most recently coded instruction with
500** comment text.
501*/
502void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
503 va_list ap;
504 assert( p->nOp>0 );
drh6f7adc82006-01-11 21:41:20 +0000505 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0
danielk19779e128002006-01-18 16:51:35 +0000506 || sqlite3MallocFailed() );
drhad6d9462004-09-19 02:15:24 +0000507 va_start(ap, zFormat);
508 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
509 va_end(ap);
510}
511#endif
512
drh9a324642003-09-06 20:12:01 +0000513/*
drh9a324642003-09-06 20:12:01 +0000514** Return the opcode for a given address.
515*/
danielk19774adee202004-05-08 08:23:19 +0000516VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000517 assert( p->magic==VDBE_MAGIC_INIT );
518 assert( addr>=0 && addr<p->nOp );
519 return &p->aOp[addr];
520}
521
drhb7f91642004-10-31 02:22:47 +0000522#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
523 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000524/*
drhd3d39e92004-05-20 22:16:29 +0000525** Compute a string that describes the P3 parameter for an opcode.
526** Use zTemp for any required temporary buffer space.
527*/
528static char *displayP3(Op *pOp, char *zTemp, int nTemp){
529 char *zP3;
530 assert( nTemp>=20 );
531 switch( pOp->p3type ){
drhd3d39e92004-05-20 22:16:29 +0000532 case P3_KEYINFO: {
533 int i, j;
534 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
535 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
536 i = strlen(zTemp);
537 for(j=0; j<pKeyInfo->nField; j++){
538 CollSeq *pColl = pKeyInfo->aColl[j];
539 if( pColl ){
540 int n = strlen(pColl->zName);
541 if( i+n>nTemp-6 ){
542 strcpy(&zTemp[i],",...");
543 break;
544 }
545 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000546 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000547 zTemp[i++] = '-';
548 }
549 strcpy(&zTemp[i], pColl->zName);
550 i += n;
551 }else if( i+4<nTemp-6 ){
552 strcpy(&zTemp[i],",nil");
553 i += 4;
554 }
555 }
556 zTemp[i++] = ')';
557 zTemp[i] = 0;
558 assert( i<nTemp );
559 zP3 = zTemp;
560 break;
561 }
562 case P3_COLLSEQ: {
563 CollSeq *pColl = (CollSeq*)pOp->p3;
drhffbc3082004-05-21 01:29:06 +0000564 sprintf(zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000565 zP3 = zTemp;
566 break;
567 }
drhf9b596e2004-05-26 16:54:42 +0000568 case P3_FUNCDEF: {
569 FuncDef *pDef = (FuncDef*)pOp->p3;
drha967e882006-06-13 01:04:52 +0000570 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000571 zP3 = zTemp;
572 break;
573 }
drha967e882006-06-13 01:04:52 +0000574#ifndef SQLITE_OMIT_VIRTUALTABLE
575 case P3_VTAB: {
576 sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
577 sqlite3_snprintf(nTemp, zTemp, "%p:%s", pVtab, pVtab->pModule->zName);
578 zP3 = zTemp;
579 break;
580 }
581#endif
drhd3d39e92004-05-20 22:16:29 +0000582 default: {
583 zP3 = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +0000584 if( zP3==0 || pOp->opcode==OP_Noop ){
drhd3d39e92004-05-20 22:16:29 +0000585 zP3 = "";
586 }
587 }
588 }
589 return zP3;
590}
drhb7f91642004-10-31 02:22:47 +0000591#endif
drhd3d39e92004-05-20 22:16:29 +0000592
593
danielk19778b60e0f2005-01-12 09:10:39 +0000594#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000595/*
596** Print a single opcode. This routine is used for debugging only.
597*/
danielk19774adee202004-05-08 08:23:19 +0000598void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh9a324642003-09-06 20:12:01 +0000599 char *zP3;
drhd3d39e92004-05-20 22:16:29 +0000600 char zPtr[50];
601 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
drh9a324642003-09-06 20:12:01 +0000602 if( pOut==0 ) pOut = stdout;
drhd3d39e92004-05-20 22:16:29 +0000603 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
drhd3d39e92004-05-20 22:16:29 +0000604 fprintf(pOut, zFormat1,
605 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
drh9a324642003-09-06 20:12:01 +0000606 fflush(pOut);
607}
608#endif
609
610/*
drh76ff3a02004-09-24 22:32:30 +0000611** Release an array of N Mem elements
612*/
613static void releaseMemArray(Mem *p, int N){
614 if( p ){
615 while( N-->0 ){
616 sqlite3VdbeMemRelease(p++);
617 }
618 }
619}
620
drhb7f91642004-10-31 02:22:47 +0000621#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000622/*
drh9a324642003-09-06 20:12:01 +0000623** Give a listing of the program in the virtual machine.
624**
danielk19774adee202004-05-08 08:23:19 +0000625** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000626** running the code, it invokes the callback once for each instruction.
627** This feature is used to implement "EXPLAIN".
628*/
danielk19774adee202004-05-08 08:23:19 +0000629int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000630 Vdbe *p /* The VDBE */
631){
drh9bb575f2004-09-06 17:24:11 +0000632 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000633 int i;
drh826fb5a2004-02-14 23:59:57 +0000634 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000635
drh9a324642003-09-06 20:12:01 +0000636 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000637 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
638 assert( db->magic==SQLITE_MAGIC_BUSY );
639 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000640
641 /* Even though this opcode does not put dynamic strings onto the
642 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000643 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000644 */
645 if( p->pTos==&p->aStack[4] ){
drh76ff3a02004-09-24 22:32:30 +0000646 releaseMemArray(p->aStack, 5);
danielk197718f41892004-05-22 07:27:46 +0000647 }
danielk197718f41892004-05-22 07:27:46 +0000648 p->resOnStack = 0;
649
drhecc92422005-09-10 16:46:12 +0000650 do{
651 i = p->pc++;
652 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000653 if( i>=p->nOp ){
654 p->rc = SQLITE_OK;
655 rc = SQLITE_DONE;
656 }else if( db->flags & SQLITE_Interrupt ){
657 db->flags &= ~SQLITE_Interrupt;
drhc5cdca62005-01-11 16:54:14 +0000658 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000659 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000660 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000661 }else{
drhd3d39e92004-05-20 22:16:29 +0000662 Op *pOp = &p->aOp[i];
drheb2e1762004-05-27 01:53:56 +0000663 Mem *pMem = p->aStack;
664 pMem->flags = MEM_Int;
drh9c054832004-05-31 18:51:57 +0000665 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000666 pMem->i = i; /* Program counter */
667 pMem++;
668
669 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
670 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
671 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000672 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000673 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000674 pMem++;
675
676 pMem->flags = MEM_Int;
677 pMem->i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000678 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000679 pMem++;
680
681 pMem->flags = MEM_Int;
682 pMem->i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000683 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000684 pMem++;
685
drhb8067982006-03-03 21:38:03 +0000686 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
drheb2e1762004-05-27 01:53:56 +0000687 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
drhb8067982006-03-03 21:38:03 +0000688 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000689 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000690 pMem->enc = SQLITE_UTF8;
drheb2e1762004-05-27 01:53:56 +0000691
drhecc92422005-09-10 16:46:12 +0000692 p->nResColumn = 5 - 2*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000693 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000694 p->rc = SQLITE_OK;
danielk197718f41892004-05-22 07:27:46 +0000695 p->resOnStack = 1;
drh826fb5a2004-02-14 23:59:57 +0000696 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000697 }
drh826fb5a2004-02-14 23:59:57 +0000698 return rc;
drh9a324642003-09-06 20:12:01 +0000699}
drhb7f91642004-10-31 02:22:47 +0000700#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000701
702/*
drh3f7d4e42004-07-24 14:35:58 +0000703** Print the SQL that was used to generate a VDBE program.
704*/
705void sqlite3VdbePrintSql(Vdbe *p){
706#ifdef SQLITE_DEBUG
707 int nOp = p->nOp;
708 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000709 if( nOp<1 ) return;
710 pOp = &p->aOp[nOp-1];
drh3f7d4e42004-07-24 14:35:58 +0000711 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
712 const char *z = pOp->p3;
drh4c755c02004-08-08 20:22:17 +0000713 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000714 printf("SQL: [%s]\n", z);
715 }
716#endif
717}
718
719/*
drh9a324642003-09-06 20:12:01 +0000720** Prepare a virtual machine for execution. This involves things such
721** as allocating stack space and initializing the program counter.
722** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000723** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000724**
725** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
726** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000727*/
danielk19774adee202004-05-08 08:23:19 +0000728void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000729 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000730 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000731 int nMem, /* Number of memory cells to allocate */
732 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000733 int isExplain /* True if the EXPLAIN keywords is present */
734){
735 int n;
736
737 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000738 assert( p->magic==VDBE_MAGIC_INIT );
739
drhc16a03b2004-09-15 13:38:10 +0000740 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000741 */
drhc16a03b2004-09-15 13:38:10 +0000742 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000743
danielk1977634f2982005-03-28 08:44:07 +0000744 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
745 * is because the call to resizeOpArray() below may shrink the
746 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
747 * state.
748 */
749 p->magic = VDBE_MAGIC_RUN;
750
drh9a324642003-09-06 20:12:01 +0000751 /* No instruction ever pushes more than a single element onto the
752 ** stack. And the stack never grows on successive executions of the
753 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000754 ** on the maximum stack depth required. (Added later:) The
755 ** resolveP2Values() call computes a tighter upper bound on the
756 ** stack size.
drh9a324642003-09-06 20:12:01 +0000757 **
758 ** Allocation all the stack space we will ever need.
759 */
drh82a48512003-09-06 22:45:20 +0000760 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000761 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000762 int nStack; /* Maximum number of stack entries required */
763 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000764 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000765 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000766 assert( nStack<p->nOp );
767 nStack = isExplain ? 10 : nStack;
drh82a48512003-09-06 22:45:20 +0000768 p->aStack = sqliteMalloc(
danielk1977bc04f852005-03-29 08:26:13 +0000769 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000770 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000771 + nVar*sizeof(Mem) /* aVar */
772 + nVar*sizeof(char*) /* azVar */
773 + nMem*sizeof(Mem) /* aMem */
774 + nCursor*sizeof(Cursor*) /* apCsr */
drh82a48512003-09-06 22:45:20 +0000775 );
danielk19779e128002006-01-18 16:51:35 +0000776 if( !sqlite3MallocFailed() ){
danielk1977bc04f852005-03-29 08:26:13 +0000777 p->aMem = &p->aStack[nStack];
drh290c1942004-08-21 17:54:45 +0000778 p->nMem = nMem;
drh86f43302004-10-05 17:37:36 +0000779 p->aVar = &p->aMem[nMem];
780 p->nVar = nVar;
781 p->okVar = 0;
782 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000783 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000784 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000785 p->nCursor = nCursor;
786 for(n=0; n<nVar; n++){
787 p->aVar[n].flags = MEM_Null;
788 }
danielk197754db47e2004-05-19 10:36:43 +0000789 }
drh82a48512003-09-06 22:45:20 +0000790 }
danielk1977b3bce662005-01-29 08:32:43 +0000791 for(n=0; n<p->nMem; n++){
792 p->aMem[n].flags = MEM_Null;
793 }
drh9a324642003-09-06 20:12:01 +0000794
drhfaa57ac2004-06-09 14:01:51 +0000795#ifdef SQLITE_DEBUG
drh35d4c2f2004-06-10 01:30:59 +0000796 if( (p->db->flags & SQLITE_VdbeListing)!=0
drh66560ad2006-01-06 14:32:19 +0000797 || sqlite3OsFileExists("vdbe_explain")
drh35d4c2f2004-06-10 01:30:59 +0000798 ){
drh80242052004-06-09 00:48:12 +0000799 int i;
800 printf("VDBE Program Listing:\n");
drh3f7d4e42004-07-24 14:35:58 +0000801 sqlite3VdbePrintSql(p);
drh80242052004-06-09 00:48:12 +0000802 for(i=0; i<p->nOp; i++){
803 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
804 }
805 }
drh66560ad2006-01-06 14:32:19 +0000806 if( sqlite3OsFileExists("vdbe_trace") ){
drh9a324642003-09-06 20:12:01 +0000807 p->trace = stdout;
808 }
809#endif
drh6810ce62004-01-31 19:22:56 +0000810 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000811 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000812 p->rc = SQLITE_OK;
813 p->uniqueCnt = 0;
814 p->returnDepth = 0;
815 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000816 p->popStack = 0;
817 p->explain |= isExplain;
818 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000819 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +0000820 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +0000821 p->minWriteFileFormat = 255;
drh9a324642003-09-06 20:12:01 +0000822#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000823 {
824 int i;
825 for(i=0; i<p->nOp; i++){
826 p->aOp[i].cnt = 0;
827 p->aOp[i].cycles = 0;
828 }
drh9a324642003-09-06 20:12:01 +0000829 }
830#endif
831}
832
drh9a324642003-09-06 20:12:01 +0000833/*
drh9a324642003-09-06 20:12:01 +0000834** Close a cursor and release all the resources that cursor happens
835** to hold.
836*/
drh4774b132004-06-12 20:12:51 +0000837void sqlite3VdbeFreeCursor(Cursor *pCx){
838 if( pCx==0 ){
839 return;
840 }
drh9a324642003-09-06 20:12:01 +0000841 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +0000842 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +0000843 }
844 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000845 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +0000846 }
drh9eff6162006-06-12 21:59:13 +0000847#ifndef SQLITE_OMIT_VIRTUALTABLE
848 if( pCx->pVtabCursor ){
849 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
850 sqlite3_vtab *pVtab = pVtabCursor->pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000851 const sqlite3_module *pModule = pVtab->pModule;
drh9eff6162006-06-12 21:59:13 +0000852 pModule->xClose(pVtabCursor);
853 }
854#endif
drh9a324642003-09-06 20:12:01 +0000855 sqliteFree(pCx->pData);
drh9188b382004-05-14 21:12:22 +0000856 sqliteFree(pCx->aType);
drh4774b132004-06-12 20:12:51 +0000857 sqliteFree(pCx);
drh9a324642003-09-06 20:12:01 +0000858}
859
860/*
861** Close all cursors
862*/
863static void closeAllCursors(Vdbe *p){
864 int i;
drh290c1942004-08-21 17:54:45 +0000865 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +0000866 for(i=0; i<p->nCursor; i++){
drh4774b132004-06-12 20:12:51 +0000867 sqlite3VdbeFreeCursor(p->apCsr[i]);
drh290c1942004-08-21 17:54:45 +0000868 p->apCsr[i] = 0;
drh9a324642003-09-06 20:12:01 +0000869 }
drh9a324642003-09-06 20:12:01 +0000870}
871
872/*
drh9a324642003-09-06 20:12:01 +0000873** Clean up the VM after execution.
874**
875** This routine will automatically close any cursors, lists, and/or
876** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +0000877** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +0000878*/
879static void Cleanup(Vdbe *p){
880 int i;
drh6810ce62004-01-31 19:22:56 +0000881 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +0000882 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
883 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +0000884 }
drh9a324642003-09-06 20:12:01 +0000885 closeAllCursors(p);
drh76ff3a02004-09-24 22:32:30 +0000886 releaseMemArray(p->aMem, p->nMem);
drha01f79d2005-07-08 13:07:59 +0000887 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +0000888 if( p->contextStack ){
889 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +0000890 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +0000891 }
892 sqliteFree(p->contextStack);
drh344737f2004-09-19 00:50:20 +0000893 }
drh5f968432004-02-21 19:02:30 +0000894 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +0000895 p->contextStackDepth = 0;
896 p->contextStackTop = 0;
drh9a324642003-09-06 20:12:01 +0000897 sqliteFree(p->zErrMsg);
898 p->zErrMsg = 0;
drh9a324642003-09-06 20:12:01 +0000899}
900
901/*
danielk197722322fd2004-05-25 23:35:17 +0000902** Set the number of result columns that will be returned by this SQL
903** statement. This is now set at compile time, rather than during
904** execution of the vdbe program so that sqlite3_column_count() can
905** be called on an SQL statement before sqlite3_step().
906*/
907void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +0000908 Mem *pColName;
909 int n;
danielk1977955de522006-02-10 02:27:42 +0000910 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drhcc43cab2005-10-05 11:35:09 +0000911 sqliteFree(p->aColName);
danielk1977955de522006-02-10 02:27:42 +0000912 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +0000913 p->nResColumn = nResColumn;
drh76ff3a02004-09-24 22:32:30 +0000914 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
915 if( p->aColName==0 ) return;
916 while( n-- > 0 ){
917 (pColName++)->flags = MEM_Null;
918 }
danielk197722322fd2004-05-25 23:35:17 +0000919}
920
921/*
danielk19773cf86062004-05-26 10:11:05 +0000922** Set the name of the idx'th column to be returned by the SQL statement.
923** zName must be a pointer to a nul terminated string.
924**
925** This call must be made after a call to sqlite3VdbeSetNumCols().
926**
danielk1977d8123362004-06-12 09:25:12 +0000927** If N==P3_STATIC it means that zName is a pointer to a constant static
928** string and we can just copy the pointer. If it is P3_DYNAMIC, then
929** the string is freed using sqliteFree() when the vdbe is finished with
930** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +0000931*/
danielk1977955de522006-02-10 02:27:42 +0000932int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +0000933 int rc;
934 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +0000935 assert( idx<p->nResColumn );
936 assert( var<COLNAME_N );
danielk19779e128002006-01-18 16:51:35 +0000937 if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +0000938 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +0000939 pColName = &(p->aColName[idx+var*p->nResColumn]);
danielk1977d8123362004-06-12 09:25:12 +0000940 if( N==P3_DYNAMIC || N==P3_STATIC ){
941 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +0000942 }else{
danielk1977d8123362004-06-12 09:25:12 +0000943 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +0000944 }
945 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
946 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +0000947 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +0000948 }
949 return rc;
950}
951
danielk197713adf8a2004-06-03 16:08:41 +0000952/*
953** A read or write transaction may or may not be active on database handle
954** db. If a transaction is active, commit it. If there is a
955** write-transaction spanning more than one database file, this routine
956** takes care of the master journal trickery.
957*/
drh9bb575f2004-09-06 17:24:11 +0000958static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +0000959 int i;
960 int nTrans = 0; /* Number of databases with an active write-transaction */
961 int rc = SQLITE_OK;
962 int needXcommit = 0;
963
964 for(i=0; i<db->nDb; i++){
965 Btree *pBt = db->aDb[i].pBt;
966 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
967 needXcommit = 1;
968 if( i!=1 ) nTrans++;
969 }
970 }
971
972 /* If there are any write-transactions at all, invoke the commit hook */
973 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +0000974 sqlite3SafetyOff(db);
975 rc = db->xCommitCallback(db->pCommitArg);
976 sqlite3SafetyOn(db);
977 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +0000978 return SQLITE_CONSTRAINT;
979 }
980 }
981
danielk197740b38dc2004-06-26 08:38:24 +0000982 /* The simple case - no more than one database file (not counting the
983 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +0000984 ** master-journal.
drhc9e06862004-06-09 20:03:08 +0000985 **
danielk197740b38dc2004-06-26 08:38:24 +0000986 ** If the return value of sqlite3BtreeGetFilename() is a zero length
987 ** string, it means the main database is :memory:. In that case we do
988 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +0000989 ** too.
danielk197713adf8a2004-06-03 16:08:41 +0000990 */
danielk197740b38dc2004-06-26 08:38:24 +0000991 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +0000992 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +0000993 Btree *pBt = db->aDb[i].pBt;
994 if( pBt ){
drh2ac3ee92004-06-07 16:27:46 +0000995 rc = sqlite3BtreeSync(pBt, 0);
996 }
997 }
998
999 /* Do the commit only if all databases successfully synced */
1000 if( rc==SQLITE_OK ){
1001 for(i=0; i<db->nDb; i++){
1002 Btree *pBt = db->aDb[i].pBt;
1003 if( pBt ){
1004 sqlite3BtreeCommit(pBt);
1005 }
danielk197713adf8a2004-06-03 16:08:41 +00001006 }
1007 }
1008 }
1009
1010 /* The complex case - There is a multi-file write-transaction active.
1011 ** This requires a master journal file to ensure the transaction is
1012 ** committed atomicly.
1013 */
danielk197744ee5bf2005-05-27 09:41:12 +00001014#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001015 else{
drh2c8997b2005-08-27 16:36:48 +00001016 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001017 char *zMaster = 0; /* File-name for the master journal */
1018 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
drh9cbe6352005-11-29 03:13:21 +00001019 OsFile *master = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001020
1021 /* Select a master journal file name */
1022 do {
drha6abd042004-06-09 17:37:22 +00001023 u32 random;
1024 sqliteFree(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001025 sqlite3Randomness(sizeof(random), &random);
drhff13c7d2004-06-09 21:01:11 +00001026 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001027 if( !zMaster ){
1028 return SQLITE_NOMEM;
1029 }
drh66560ad2006-01-06 14:32:19 +00001030 }while( sqlite3OsFileExists(zMaster) );
danielk197713adf8a2004-06-03 16:08:41 +00001031
1032 /* Open the master journal. */
drh66560ad2006-01-06 14:32:19 +00001033 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001034 if( rc!=SQLITE_OK ){
1035 sqliteFree(zMaster);
1036 return rc;
1037 }
1038
1039 /* Write the name of each database file in the transaction into the new
1040 ** master journal file. If an error occurs at this point close
1041 ** and delete the master journal file. All the individual journal files
1042 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001043 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001044 */
1045 for(i=0; i<db->nDb; i++){
1046 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001047 if( i==1 ) continue; /* Ignore the TEMP database */
danielk197713adf8a2004-06-03 16:08:41 +00001048 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001049 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001050 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001051 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1052 needSync = 1;
1053 }
drh054889e2005-11-30 03:20:31 +00001054 rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001055 if( rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001056 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001057 sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001058 sqliteFree(zMaster);
1059 return rc;
1060 }
1061 }
1062 }
1063
danielk197713adf8a2004-06-03 16:08:41 +00001064
danielk19775865e3d2004-06-14 06:03:57 +00001065 /* Sync the master journal file. Before doing this, open the directory
1066 ** the master journal file is store in so that it gets synced too.
1067 */
1068 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
drh054889e2005-11-30 03:20:31 +00001069 rc = sqlite3OsOpenDirectory(master, zMainFile);
drheb796a72005-09-08 12:38:41 +00001070 if( rc!=SQLITE_OK ||
drh054889e2005-11-30 03:20:31 +00001071 (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
1072 sqlite3OsClose(&master);
drh66560ad2006-01-06 14:32:19 +00001073 sqlite3OsDelete(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001074 sqliteFree(zMaster);
1075 return rc;
1076 }
drhc9e06862004-06-09 20:03:08 +00001077
danielk197713adf8a2004-06-03 16:08:41 +00001078 /* Sync all the db files involved in the transaction. The same call
1079 ** sets the master journal pointer in each individual journal. If
1080 ** an error occurs here, do not delete the master journal file.
1081 **
1082 ** If the error occurs during the first call to sqlite3BtreeSync(),
1083 ** then there is a chance that the master journal file will be
1084 ** orphaned. But we cannot delete it, in case the master journal
1085 ** file name was written into the journal file before the failure
1086 ** occured.
1087 */
1088 for(i=0; i<db->nDb; i++){
1089 Btree *pBt = db->aDb[i].pBt;
1090 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1091 rc = sqlite3BtreeSync(pBt, zMaster);
1092 if( rc!=SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001093 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001094 sqliteFree(zMaster);
1095 return rc;
1096 }
1097 }
1098 }
drh054889e2005-11-30 03:20:31 +00001099 sqlite3OsClose(&master);
danielk197713adf8a2004-06-03 16:08:41 +00001100
danielk1977962398d2004-06-14 09:35:16 +00001101 /* Delete the master journal file. This commits the transaction. After
1102 ** doing this the directory is synced again before any individual
1103 ** transaction files are deleted.
1104 */
drh66560ad2006-01-06 14:32:19 +00001105 rc = sqlite3OsDelete(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001106 assert( rc==SQLITE_OK );
danielk19773fe83ac2004-06-14 09:41:17 +00001107 sqliteFree(zMaster);
1108 zMaster = 0;
drh66560ad2006-01-06 14:32:19 +00001109 rc = sqlite3OsSyncDirectory(zMainFile);
danielk1977962398d2004-06-14 09:35:16 +00001110 if( rc!=SQLITE_OK ){
1111 /* This is not good. The master journal file has been deleted, but
1112 ** the directory sync failed. There is no completely safe course of
1113 ** action from here. The individual journals contain the name of the
1114 ** master journal file, but there is no way of knowing if that
1115 ** master journal exists now or if it will exist after the operating
1116 ** system crash that may follow the fsync() failure.
1117 */
danielk1977962398d2004-06-14 09:35:16 +00001118 return rc;
1119 }
danielk197713adf8a2004-06-03 16:08:41 +00001120
1121 /* All files and directories have already been synced, so the following
1122 ** calls to sqlite3BtreeCommit() are only closing files and deleting
1123 ** journals. If something goes wrong while this is happening we don't
danielk1977962398d2004-06-14 09:35:16 +00001124 ** really care. The integrity of the transaction is already guaranteed,
danielk197713adf8a2004-06-03 16:08:41 +00001125 ** but some stray 'cold' journals may be lying around. Returning an
1126 ** error code won't help matters.
1127 */
1128 for(i=0; i<db->nDb; i++){
1129 Btree *pBt = db->aDb[i].pBt;
1130 if( pBt ){
1131 sqlite3BtreeCommit(pBt);
1132 }
1133 }
1134 }
danielk197744ee5bf2005-05-27 09:41:12 +00001135#endif
danielk1977026d2702004-06-14 13:14:59 +00001136
drh2ac3ee92004-06-07 16:27:46 +00001137 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001138}
1139
drh91b48aa2004-06-30 11:14:18 +00001140/*
1141** Find every active VM other than pVdbe and change its status to
drh376deb12004-06-30 11:41:55 +00001142** aborted. This happens when one VM causes a rollback due to an
1143** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1144** aborted so that they do not have data rolled out from underneath
1145** them leading to a segfault.
drh91b48aa2004-06-30 11:14:18 +00001146*/
danielk19778d34dfd2006-01-24 16:37:57 +00001147void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
drh91b48aa2004-06-30 11:14:18 +00001148 Vdbe *pOther;
danielk19778d34dfd2006-01-24 16:37:57 +00001149 for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
1150 if( pOther==pExcept ) continue;
drh91b48aa2004-06-30 11:14:18 +00001151 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1152 closeAllCursors(pOther);
1153 pOther->aborted = 1;
1154 }
1155}
1156
danielk19771d850a72004-05-31 08:26:49 +00001157/*
1158** This routine checks that the sqlite3.activeVdbeCnt count variable
1159** matches the number of vdbe's in the list sqlite3.pVdbe that are
1160** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001161** This is an internal self-check only - it is not an essential processing
1162** step.
danielk19771d850a72004-05-31 08:26:49 +00001163**
1164** This is a no-op if NDEBUG is defined.
1165*/
1166#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001167static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001168 Vdbe *p;
1169 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001170 p = db->pVdbe;
1171 while( p ){
drh92f02c32004-09-02 14:57:08 +00001172 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001173 cnt++;
1174 }
1175 p = p->pNext;
1176 }
danielk19771d850a72004-05-31 08:26:49 +00001177 assert( cnt==db->activeVdbeCnt );
1178}
1179#else
1180#define checkActiveVdbeCnt(x)
1181#endif
1182
danielk19773cf86062004-05-26 10:11:05 +00001183/*
drh92f02c32004-09-02 14:57:08 +00001184** This routine is called the when a VDBE tries to halt. If the VDBE
1185** has made changes and is in autocommit mode, then commit those
1186** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001187**
drh92f02c32004-09-02 14:57:08 +00001188** This routine is the only way to move the state of a VM from
1189** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1190**
1191** Return an error code. If the commit could not complete because of
1192** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1193** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001194*/
drh92f02c32004-09-02 14:57:08 +00001195int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001196 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001197 int i;
danielk19771d850a72004-05-31 08:26:49 +00001198 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001199 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1200
1201 /* This function contains the logic that determines if a statement or
1202 ** transaction will be committed or rolled back as a result of the
1203 ** execution of this virtual machine.
1204 **
1205 ** Special errors:
1206 **
1207 ** If an SQLITE_NOMEM error has occured in a statement that writes to
1208 ** the database, then either a statement or transaction must be rolled
1209 ** back to ensure the tree-structures are in a consistent state. A
1210 ** statement transaction is rolled back if one is open, otherwise the
1211 ** entire transaction must be rolled back.
1212 **
1213 ** If an SQLITE_IOERR error has occured in a statement that writes to
1214 ** the database, then the entire transaction must be rolled back. The
1215 ** I/O error may have caused garbage to be written to the journal
1216 ** file. Were the transaction to continue and eventually be rolled
1217 ** back that garbage might end up in the database file.
1218 **
1219 ** In both of the above cases, the Vdbe.errorAction variable is
1220 ** ignored. If the sqlite3.autoCommit flag is false and a transaction
1221 ** is rolled back, it will be set to true.
1222 **
1223 ** Other errors:
1224 **
1225 ** No error:
1226 **
1227 */
drh9a324642003-09-06 20:12:01 +00001228
danielk19779e128002006-01-18 16:51:35 +00001229 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +00001230 p->rc = SQLITE_NOMEM;
1231 }
drh92f02c32004-09-02 14:57:08 +00001232 if( p->magic!=VDBE_MAGIC_RUN ){
1233 /* Already halted. Nothing to do. */
1234 assert( p->magic==VDBE_MAGIC_HALT );
1235 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001236 }
drh92f02c32004-09-02 14:57:08 +00001237 closeAllCursors(p);
danielk19771d850a72004-05-31 08:26:49 +00001238 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001239
danielk197707cb5602006-01-20 10:55:05 +00001240 /* No commit or rollback needed if the program never started */
1241 if( p->pc>=0 ){
danielk1977261919c2005-12-06 12:52:59 +00001242
danielk197707cb5602006-01-20 10:55:05 +00001243 /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
1244 isSpecialError = ((p->rc==SQLITE_NOMEM || p->rc==SQLITE_IOERR)?1:0);
1245 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001246 /* This loop does static analysis of the query to see which of the
1247 ** following three categories it falls into:
1248 **
1249 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001250 ** Query with statement journal
1251 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001252 **
1253 ** We could do something more elegant than this static analysis (i.e.
1254 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001255 ** handling malloc() or IO failure is a fairly obscure edge case so
1256 ** this is probably easier. Todo: Might be an opportunity to reduce
1257 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001258 */
1259 int isReadOnly = 1;
1260 int isStatement = 0;
1261 assert(p->aOp || p->nOp==0);
1262 for(i=0; i<p->nOp; i++){
1263 switch( p->aOp[i].opcode ){
1264 case OP_Transaction:
1265 isReadOnly = 0;
1266 break;
1267 case OP_Statement:
1268 isStatement = 1;
1269 break;
1270 }
1271 }
danielk197707cb5602006-01-20 10:55:05 +00001272
1273 /* If the query was read-only, we need do no rollback at all. Otherwise,
1274 ** proceed with the special handling.
1275 */
1276 if( !isReadOnly ){
1277 if( p->rc==SQLITE_NOMEM && isStatement ){
1278 xFunc = sqlite3BtreeRollbackStmt;
1279 }else{
1280 /* We are forced to roll back the active transaction. Before doing
1281 ** so, abort any other statements this handle currently has active.
1282 */
danielk19778d34dfd2006-01-24 16:37:57 +00001283 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001284 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001285 db->autoCommit = 1;
1286 }
danielk1977261919c2005-12-06 12:52:59 +00001287 }
1288 }
danielk197707cb5602006-01-20 10:55:05 +00001289
1290 /* If the auto-commit flag is set and this is the only active vdbe, then
1291 ** we do either a commit or rollback of the current transaction.
1292 **
1293 ** Note: This block also runs if one of the special errors handled
1294 ** above has occured.
1295 */
1296 if( db->autoCommit && db->activeVdbeCnt==1 ){
1297 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1298 /* The auto-commit flag is true, and the vdbe program was
1299 ** successful or hit an 'OR FAIL' constraint. This means a commit
1300 ** is required.
1301 */
1302 int rc = vdbeCommit(db);
1303 if( rc==SQLITE_BUSY ){
1304 return SQLITE_BUSY;
1305 }else if( rc!=SQLITE_OK ){
1306 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001307 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001308 }else{
1309 sqlite3CommitInternalChanges(db);
1310 }
1311 }else{
danielk197797a227c2006-01-20 16:32:04 +00001312 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001313 }
1314 }else if( !xFunc ){
1315 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1316 xFunc = sqlite3BtreeCommitStmt;
1317 }else if( p->errorAction==OE_Abort ){
1318 xFunc = sqlite3BtreeRollbackStmt;
1319 }else{
danielk19778d34dfd2006-01-24 16:37:57 +00001320 sqlite3AbortOtherActiveVdbes(db, p);
danielk197797a227c2006-01-20 16:32:04 +00001321 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001322 db->autoCommit = 1;
1323 }
danielk19771d850a72004-05-31 08:26:49 +00001324 }
danielk197707cb5602006-01-20 10:55:05 +00001325
1326 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1327 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1328 ** and the return code is still SQLITE_OK, set the return code to the new
1329 ** error value.
1330 */
1331 assert(!xFunc ||
1332 xFunc==sqlite3BtreeCommitStmt ||
1333 xFunc==sqlite3BtreeRollbackStmt
1334 );
1335 for(i=0; xFunc && i<db->nDb; i++){
1336 int rc;
1337 Btree *pBt = db->aDb[i].pBt;
1338 if( pBt ){
1339 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001340 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1341 p->rc = rc;
1342 sqlite3SetString(&p->zErrMsg, 0);
1343 }
danielk197707cb5602006-01-20 10:55:05 +00001344 }
danielk197777d83ba2004-05-31 10:08:14 +00001345 }
danielk197707cb5602006-01-20 10:55:05 +00001346
1347 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1348 ** set the change counter.
1349 */
1350 if( p->changeCntOn && p->pc>=0 ){
1351 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1352 sqlite3VdbeSetChanges(db, p->nChange);
1353 }else{
1354 sqlite3VdbeSetChanges(db, 0);
1355 }
1356 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001357 }
danielk197707cb5602006-01-20 10:55:05 +00001358
1359 /* Rollback or commit any schema changes that occurred. */
1360 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1361 sqlite3ResetInternalSchema(db, 0);
1362 db->flags = (db->flags | SQLITE_InternChanges);
1363 }
drh9a324642003-09-06 20:12:01 +00001364 }
danielk19771d850a72004-05-31 08:26:49 +00001365
drh92f02c32004-09-02 14:57:08 +00001366 /* We have successfully halted and closed the VM. Record this fact. */
1367 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001368 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001369 }
drh92f02c32004-09-02 14:57:08 +00001370 p->magic = VDBE_MAGIC_HALT;
1371 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001372
drh92f02c32004-09-02 14:57:08 +00001373 return SQLITE_OK;
1374}
1375
1376/*
1377** Clean up a VDBE after execution but do not delete the VDBE just yet.
1378** Write any error messages into *pzErrMsg. Return the result code.
1379**
1380** After this routine is run, the VDBE should be ready to be executed
1381** again.
1382**
1383** To look at it another way, this routine resets the state of the
1384** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1385** VDBE_MAGIC_INIT.
1386*/
1387int sqlite3VdbeReset(Vdbe *p){
1388 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
drhc60d0442004-09-30 13:43:13 +00001389 sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh92f02c32004-09-02 14:57:08 +00001390 return SQLITE_MISUSE;
1391 }
1392
1393 /* If the VM did not run to completion or if it encountered an
1394 ** error, then it might not have been halted properly. So halt
1395 ** it now.
1396 */
1397 sqlite3VdbeHalt(p);
1398
drhfb7e7652005-01-24 00:28:42 +00001399 /* If the VDBE has be run even partially, then transfer the error code
1400 ** and error message from the VDBE into the main database structure. But
1401 ** if the VDBE has just been set to run but has not actually executed any
1402 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001403 */
drhfb7e7652005-01-24 00:28:42 +00001404 if( p->pc>=0 ){
1405 if( p->zErrMsg ){
danielk197797a227c2006-01-20 16:32:04 +00001406 sqlite3* db = p->db;
1407 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
1408 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001409 p->zErrMsg = 0;
1410 }else if( p->rc ){
1411 sqlite3Error(p->db, p->rc, 0);
1412 }else{
1413 sqlite3Error(p->db, SQLITE_OK, 0);
1414 }
danielk1977a21c6b62005-01-24 10:25:59 +00001415 }else if( p->rc && p->expired ){
1416 /* The expired flag was set on the VDBE before the first call
1417 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1418 ** called), set the database error in this case as well.
1419 */
1420 sqlite3Error(p->db, p->rc, 0);
drh92f02c32004-09-02 14:57:08 +00001421 }
1422
1423 /* Reclaim all memory used by the VDBE
1424 */
1425 Cleanup(p);
1426
1427 /* Save profiling information from this VDBE run.
1428 */
danielk1977261919c2005-12-06 12:52:59 +00001429 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
drh9a324642003-09-06 20:12:01 +00001430#ifdef VDBE_PROFILE
1431 {
1432 FILE *out = fopen("vdbe_profile.out", "a");
1433 if( out ){
1434 int i;
1435 fprintf(out, "---- ");
1436 for(i=0; i<p->nOp; i++){
1437 fprintf(out, "%02x", p->aOp[i].opcode);
1438 }
1439 fprintf(out, "\n");
1440 for(i=0; i<p->nOp; i++){
1441 fprintf(out, "%6d %10lld %8lld ",
1442 p->aOp[i].cnt,
1443 p->aOp[i].cycles,
1444 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1445 );
danielk19774adee202004-05-08 08:23:19 +00001446 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001447 }
1448 fclose(out);
1449 }
1450 }
1451#endif
1452 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001453 p->aborted = 0;
drh178286b2005-01-23 13:14:55 +00001454 if( p->rc==SQLITE_SCHEMA ){
1455 sqlite3ResetInternalSchema(p->db, 0);
1456 }
drh9a324642003-09-06 20:12:01 +00001457 return p->rc;
1458}
drh92f02c32004-09-02 14:57:08 +00001459
drh9a324642003-09-06 20:12:01 +00001460/*
1461** Clean up and delete a VDBE after execution. Return an integer which is
1462** the result code. Write any error message text into *pzErrMsg.
1463*/
danielk19779e6db7d2004-06-21 08:18:51 +00001464int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001465 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001466
danielk1977b5548a82004-06-26 13:51:33 +00001467 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1468 rc = sqlite3VdbeReset(p);
1469 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001470 return SQLITE_MISUSE;
1471 }
danielk19774adee202004-05-08 08:23:19 +00001472 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001473 return rc;
1474}
1475
1476/*
drhf92c7ff2004-06-19 15:40:23 +00001477** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001478** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001479** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001480** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001481*/
1482void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1483 int i;
1484 for(i=0; i<pVdbeFunc->nAux; i++){
1485 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1486 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1487 if( pAux->xDelete ){
1488 pAux->xDelete(pAux->pAux);
1489 }
1490 pAux->pAux = 0;
1491 }
1492 }
1493}
1494
1495/*
drh9a324642003-09-06 20:12:01 +00001496** Delete an entire VDBE.
1497*/
danielk19774adee202004-05-08 08:23:19 +00001498void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001499 int i;
1500 if( p==0 ) return;
1501 Cleanup(p);
1502 if( p->pPrev ){
1503 p->pPrev->pNext = p->pNext;
1504 }else{
1505 assert( p->db->pVdbe==p );
1506 p->db->pVdbe = p->pNext;
1507 }
1508 if( p->pNext ){
1509 p->pNext->pPrev = p->pPrev;
1510 }
drh76ff3a02004-09-24 22:32:30 +00001511 if( p->aOp ){
1512 for(i=0; i<p->nOp; i++){
1513 Op *pOp = &p->aOp[i];
drhb38ad992005-09-16 00:27:01 +00001514 freeP3(pOp->p3type, pOp->p3);
drh9a324642003-09-06 20:12:01 +00001515 }
drh76ff3a02004-09-24 22:32:30 +00001516 sqliteFree(p->aOp);
drh9a324642003-09-06 20:12:01 +00001517 }
drh76ff3a02004-09-24 22:32:30 +00001518 releaseMemArray(p->aVar, p->nVar);
drh9a324642003-09-06 20:12:01 +00001519 sqliteFree(p->aLabel);
1520 sqliteFree(p->aStack);
danielk1977955de522006-02-10 02:27:42 +00001521 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh76ff3a02004-09-24 22:32:30 +00001522 sqliteFree(p->aColName);
drh9a324642003-09-06 20:12:01 +00001523 p->magic = VDBE_MAGIC_DEAD;
1524 sqliteFree(p);
1525}
drha11846b2004-01-07 18:52:56 +00001526
1527/*
drha11846b2004-01-07 18:52:56 +00001528** If a MoveTo operation is pending on the given cursor, then do that
1529** MoveTo now. Return an error code. If no MoveTo is pending, this
1530** routine does nothing and returns SQLITE_OK.
1531*/
danielk19774adee202004-05-08 08:23:19 +00001532int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001533 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001534 int res, rc;
danielk1977132872b2004-05-10 10:37:18 +00001535 extern int sqlite3_search_count;
drhf0863fe2005-06-12 21:35:51 +00001536 assert( p->isTable );
1537 if( p->isTable ){
drh536065a2005-01-26 21:55:31 +00001538 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
danielk19776490beb2004-05-11 06:17:21 +00001539 }else{
drh536065a2005-01-26 21:55:31 +00001540 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1541 sizeof(i64),&res);
danielk19776490beb2004-05-11 06:17:21 +00001542 }
drh536065a2005-01-26 21:55:31 +00001543 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001544 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001545 p->lastRowid = keyToInt(p->movetoTarget);
1546 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001547 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001548 rc = sqlite3BtreeNext(p->pCursor, &res);
1549 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001550 }
danielk1977132872b2004-05-10 10:37:18 +00001551 sqlite3_search_count++;
drha11846b2004-01-07 18:52:56 +00001552 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001553 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001554 }
1555 return SQLITE_OK;
1556}
danielk19774adee202004-05-08 08:23:19 +00001557
drhab9f7f12004-05-08 10:56:11 +00001558/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001559** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001560**
danielk1977cfcdaef2004-05-12 07:33:33 +00001561** sqlite3VdbeSerialType()
1562** sqlite3VdbeSerialTypeLen()
1563** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001564** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001565** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001566**
1567** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001568** data and index records. Each serialized value consists of a
1569** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1570** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001571**
danielk1977cfcdaef2004-05-12 07:33:33 +00001572** In an SQLite index record, the serial type is stored directly before
1573** the blob of data that it corresponds to. In a table record, all serial
1574** types are stored at the start of the record, and the blobs of data at
1575** the end. Hence these functions allow the caller to handle the
1576** serial-type and data blob seperately.
1577**
1578** The following table describes the various storage classes for data:
1579**
1580** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001581** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001582** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001583** 1 1 signed integer
1584** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001585** 3 3 signed integer
1586** 4 4 signed integer
1587** 5 6 signed integer
1588** 6 8 signed integer
1589** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001590** 8 0 Integer constant 0
1591** 9 0 Integer constant 1
1592** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001593** N>=12 and even (N-12)/2 BLOB
1594** N>=13 and odd (N-13)/2 text
1595**
drh35a59652006-01-02 18:24:40 +00001596** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1597** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001598*/
1599
1600/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001601** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001602*/
drhd946db02005-12-29 19:23:06 +00001603u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001604 int flags = pMem->flags;
1605
1606 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001607 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001608 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001609 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001610 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001611# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
danielk1977cfcdaef2004-05-12 07:33:33 +00001612 i64 i = pMem->i;
drhd946db02005-12-29 19:23:06 +00001613 u64 u;
1614 if( file_format>=4 && (i&1)==i ){
1615 return 8+i;
1616 }
1617 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001618 if( u<=127 ) return 1;
1619 if( u<=32767 ) return 2;
1620 if( u<=8388607 ) return 3;
1621 if( u<=2147483647 ) return 4;
1622 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001623 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001624 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001625 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001626 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001627 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001628 if( flags&MEM_Str ){
danielk197793d46752004-05-23 13:30:58 +00001629 int n = pMem->n;
1630 assert( n>=0 );
danielk197793d46752004-05-23 13:30:58 +00001631 return ((n*2) + 13);
danielk197790e4d952004-05-10 10:05:53 +00001632 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001633 if( flags&MEM_Blob ){
1634 return (pMem->n*2 + 12);
1635 }
1636 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001637}
1638
1639/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001640** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001641*/
drh25aa1b42004-05-28 01:39:01 +00001642int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001643 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001644 return (serial_type-12)/2;
1645 }else{
drh57196282004-10-06 15:41:16 +00001646 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001647 return aSize[serial_type];
1648 }
danielk1977192ac1d2004-05-10 07:17:30 +00001649}
1650
1651/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001652** Write the serialized data blob for the value stored in pMem into
1653** buf. It is assumed that the caller has allocated sufficient space.
1654** Return the number of bytes written.
1655*/
drhd946db02005-12-29 19:23:06 +00001656int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem, int file_format){
1657 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001658 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001659
drh1483e142004-05-21 21:12:42 +00001660 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001661 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001662 u64 v;
1663 int i;
drha19b7752004-05-30 21:14:58 +00001664 if( serial_type==7 ){
drh1483e142004-05-21 21:12:42 +00001665 v = *(u64*)&pMem->r;
1666 }else{
1667 v = *(u64*)&pMem->i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001668 }
drh1483e142004-05-21 21:12:42 +00001669 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1670 while( i-- ){
1671 buf[i] = (v&0xFF);
1672 v >>= 8;
1673 }
1674 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001675 }
drhd946db02005-12-29 19:23:06 +00001676
danielk1977cfcdaef2004-05-12 07:33:33 +00001677 /* String or blob */
drhd946db02005-12-29 19:23:06 +00001678 if( serial_type>=12 ){
1679 len = sqlite3VdbeSerialTypeLen(serial_type);
1680 memcpy(buf, pMem->z, len);
1681 return len;
1682 }
1683
1684 /* NULL or constants 0 or 1 */
1685 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001686}
1687
1688/*
1689** Deserialize the data blob pointed to by buf as serial type serial_type
1690** and store the result in pMem. Return the number of bytes read.
1691*/
danielk1977b1bc9532004-05-22 03:05:33 +00001692int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001693 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001694 u32 serial_type, /* Serial type to deserialize */
1695 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00001696){
drh3c685822005-05-21 18:32:18 +00001697 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00001698 case 10: /* Reserved for future use */
1699 case 11: /* Reserved for future use */
1700 case 0: { /* NULL */
1701 pMem->flags = MEM_Null;
1702 break;
1703 }
1704 case 1: { /* 1-byte signed integer */
1705 pMem->i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00001706 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00001707 return 1;
drh1483e142004-05-21 21:12:42 +00001708 }
drh3c685822005-05-21 18:32:18 +00001709 case 2: { /* 2-byte signed integer */
1710 pMem->i = (((signed char)buf[0])<<8) | buf[1];
1711 pMem->flags = MEM_Int;
1712 return 2;
1713 }
1714 case 3: { /* 3-byte signed integer */
1715 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1716 pMem->flags = MEM_Int;
1717 return 3;
1718 }
1719 case 4: { /* 4-byte signed integer */
1720 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1721 pMem->flags = MEM_Int;
1722 return 4;
1723 }
1724 case 5: { /* 6-byte signed integer */
1725 u64 x = (((signed char)buf[0])<<8) | buf[1];
1726 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1727 x = (x<<32) | y;
1728 pMem->i = *(i64*)&x;
1729 pMem->flags = MEM_Int;
1730 return 6;
1731 }
drh91124b32005-08-18 18:15:05 +00001732 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00001733 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00001734 u64 x;
1735 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00001736#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00001737 /* Verify that integers and floating point values use the same
drhbfd6b032005-08-28 01:38:44 +00001738 ** byte order. The byte order differs on some (broken) architectures.
1739 */
drhde941c62005-08-28 01:34:21 +00001740 static const u64 t1 = ((u64)0x3ff00000)<<32;
1741 assert( 1.0==*(double*)&t1 );
1742#endif
drhbfd6b032005-08-28 01:38:44 +00001743
drhd81bd4e2005-09-05 20:06:49 +00001744 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1745 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00001746 x = (x<<32) | y;
1747 if( serial_type==6 ){
1748 pMem->i = *(i64*)&x;
1749 pMem->flags = MEM_Int;
1750 }else{
1751 pMem->r = *(double*)&x;
1752 pMem->flags = MEM_Real;
1753 }
1754 return 8;
1755 }
drhd946db02005-12-29 19:23:06 +00001756 case 8: /* Integer 0 */
1757 case 9: { /* Integer 1 */
1758 pMem->i = serial_type-8;
1759 pMem->flags = MEM_Int;
1760 return 0;
1761 }
drh3c685822005-05-21 18:32:18 +00001762 default: {
1763 int len = (serial_type-12)/2;
1764 pMem->z = (char *)buf;
1765 pMem->n = len;
1766 pMem->xDel = 0;
1767 if( serial_type&0x01 ){
1768 pMem->flags = MEM_Str | MEM_Ephem;
1769 }else{
1770 pMem->flags = MEM_Blob | MEM_Ephem;
1771 }
1772 return len;
drh696b32f2004-05-30 01:51:52 +00001773 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001774 }
drh3c685822005-05-21 18:32:18 +00001775 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001776}
1777
1778/*
drh0e6082e2006-01-12 20:28:35 +00001779** The header of a record consists of a sequence variable-length integers.
1780** These integers are almost always small and are encoded as a single byte.
1781** The following macro takes advantage this fact to provide a fast decode
1782** of the integers in a record header. It is faster for the common case
1783** where the integer is a single byte. It is a little slower when the
1784** integer is two or more bytes. But overall it is faster.
1785**
1786** The following expressions are equivalent:
1787**
1788** x = sqlite3GetVarint32( A, &B );
1789**
1790** x = GetVarint( A, B );
1791**
1792*/
1793#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
1794
1795/*
drh7a224de2004-06-02 01:22:02 +00001796** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00001797** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1798** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00001799** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1800** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00001801*/
drh7a224de2004-06-02 01:22:02 +00001802int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00001803 void *userData,
1804 int nKey1, const void *pKey1,
1805 int nKey2, const void *pKey2
1806){
drhd3d39e92004-05-20 22:16:29 +00001807 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00001808 u32 d1, d2; /* Offset into aKey[] of next data element */
1809 u32 idx1, idx2; /* Offset into aKey[] of next header element */
1810 u32 szHdr1, szHdr2; /* Number of bytes in header */
1811 int i = 0;
1812 int nField;
1813 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00001814 const unsigned char *aKey1 = (const unsigned char *)pKey1;
1815 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00001816
1817 Mem mem1;
1818 Mem mem2;
1819 mem1.enc = pKeyInfo->enc;
1820 mem2.enc = pKeyInfo->enc;
drhd3194f52004-05-27 19:59:32 +00001821
drh0e6082e2006-01-12 20:28:35 +00001822 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00001823 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00001824 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00001825 d2 = szHdr2;
1826 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00001827 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00001828 u32 serial_type1;
1829 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00001830
1831 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00001832 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00001833 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00001834 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00001835 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00001836
1837 /* Assert that there is enough space left in each key for the blob of
1838 ** data to go with the serial type just read. This assert may fail if
1839 ** the file is corrupted. Then read the value from each key into mem1
1840 ** and mem2 respectively.
1841 */
drh25aa1b42004-05-28 01:39:01 +00001842 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1843 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001844
drhd5788202004-05-28 08:21:05 +00001845 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00001846 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1847 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00001848 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00001849 break;
1850 }
1851 i++;
1852 }
1853
1854 /* One of the keys ran out of fields, but all the fields up to that point
1855 ** were equal. If the incrKey flag is true, then the second key is
1856 ** treated as larger.
1857 */
1858 if( rc==0 ){
1859 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00001860 rc = -1;
1861 }else if( d1<nKey1 ){
1862 rc = 1;
1863 }else if( d2<nKey2 ){
1864 rc = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001865 }
drh0b2f3162005-12-21 18:36:45 +00001866 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
1867 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00001868 rc = -rc;
1869 }
1870
1871 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00001872}
drhd5788202004-05-28 08:21:05 +00001873
1874/*
drh7a224de2004-06-02 01:22:02 +00001875** The argument is an index entry composed using the OP_MakeRecord opcode.
1876** The last entry in this record should be an integer (specifically
1877** an integer rowid). This routine returns the number of bytes in
1878** that integer.
drhd5788202004-05-28 08:21:05 +00001879*/
drh74161702006-02-24 02:53:49 +00001880int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00001881 u32 szHdr; /* Size of the header */
1882 u32 typeRowid; /* Serial type of the rowid */
1883
1884 sqlite3GetVarint32(aKey, &szHdr);
1885 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1886 return sqlite3VdbeSerialTypeLen(typeRowid);
1887}
danielk1977eb015e02004-05-18 01:31:14 +00001888
1889
1890/*
drh7a224de2004-06-02 01:22:02 +00001891** pCur points at an index entry created using the OP_MakeRecord opcode.
1892** Read the rowid (the last field in the record) and store it in *rowid.
1893** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00001894*/
1895int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
danielk1977e0d4b062004-06-28 01:11:46 +00001896 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001897 int rc;
drhd5788202004-05-28 08:21:05 +00001898 u32 szHdr; /* Size of the header */
1899 u32 typeRowid; /* Serial type of the rowid */
1900 u32 lenRowid; /* Size of the rowid */
1901 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00001902
drhd5788202004-05-28 08:21:05 +00001903 sqlite3BtreeKeySize(pCur, &nCellKey);
1904 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00001905 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00001906 }
1907 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1908 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00001909 return rc;
1910 }
drh2646da72005-12-09 20:02:05 +00001911 sqlite3GetVarint32((u8*)m.z, &szHdr);
1912 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00001913 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00001914 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drhd5788202004-05-28 08:21:05 +00001915 *rowid = v.i;
danielk1977d8123362004-06-12 09:25:12 +00001916 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001917 return SQLITE_OK;
1918}
1919
drh7cf6e4d2004-05-19 14:56:55 +00001920/*
drhd3d39e92004-05-20 22:16:29 +00001921** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00001922** the key string in pKey (of length nKey). Write into *pRes a number
1923** that is negative, zero, or positive if pC is less than, equal to,
1924** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00001925**
drhd5788202004-05-28 08:21:05 +00001926** pKey is either created without a rowid or is truncated so that it
1927** omits the rowid at the end. The rowid at the end of the index entry
1928** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00001929*/
danielk1977183f9f72004-05-13 05:20:26 +00001930int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00001931 Cursor *pC, /* The cursor to compare against */
1932 int nKey, const u8 *pKey, /* The key to compare */
1933 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00001934){
danielk1977e0d4b062004-06-28 01:11:46 +00001935 i64 nCellKey;
danielk1977183f9f72004-05-13 05:20:26 +00001936 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00001937 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00001938 int lenRowid;
1939 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00001940
1941 sqlite3BtreeKeySize(pCur, &nCellKey);
1942 if( nCellKey<=0 ){
1943 *res = 0;
1944 return SQLITE_OK;
1945 }
drhd5788202004-05-28 08:21:05 +00001946 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1947 if( rc ){
1948 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00001949 }
drh74161702006-02-24 02:53:49 +00001950 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00001951 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00001952 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00001953 return SQLITE_OK;
1954}
danielk1977b28af712004-06-21 06:50:26 +00001955
1956/*
1957** This routine sets the value to be returned by subsequent calls to
1958** sqlite3_changes() on the database handle 'db'.
1959*/
1960void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1961 db->nChange = nChange;
1962 db->nTotalChange += nChange;
1963}
1964
1965/*
1966** Set a flag in the vdbe to update the change counter when it is finalised
1967** or reset.
1968*/
drh4794f732004-11-05 17:17:50 +00001969void sqlite3VdbeCountChanges(Vdbe *v){
1970 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00001971}
drhd89bd002005-01-22 03:03:54 +00001972
1973/*
1974** Mark every prepared statement associated with a database connection
1975** as expired.
1976**
1977** An expired statement means that recompilation of the statement is
1978** recommend. Statements expire when things happen that make their
1979** programs obsolete. Removing user-defined functions or collating
1980** sequences, or changing an authorization function are the types of
1981** things that make prepared statements obsolete.
1982*/
1983void sqlite3ExpirePreparedStatements(sqlite3 *db){
1984 Vdbe *p;
1985 for(p = db->pVdbe; p; p=p->pNext){
1986 p->expired = 1;
1987 }
1988}
danielk1977aee18ef2005-03-09 12:26:50 +00001989
1990/*
1991** Return the database associated with the Vdbe.
1992*/
1993sqlite3 *sqlite3VdbeDb(Vdbe *v){
1994 return v->db;
1995}